Skip to contentSkip to conversation

object: Add 'help' option for all available backends and properties

PATCH Superseded Lin Ma 4 participants 16 messages Sep 11, 2016 – Sep 22, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lin Ma

author
#0 lore raw

PATCH v2 1/1 object: Add 'help' option for all available backends and properties

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx                 |   7 ++-
 qom/object_interfaces.c         | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c                            |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
include/qom/object_interfaces.h +2 -0
index 8b17f4d..197cd5f 100644
@@ -165,4 +165,6 @@ int user_creatable_add_opts_foreach(void *opaque,
  */
 void user_creatable_del(const char *id, Error **errp);
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp);
+
 #endif
qemu-options.hx +6 -1
index a71aaf8..fade53c 100644
@@ -3752,7 +3752,9 @@ DEF("object", HAS_ARG, QEMU_OPTION_object,
     "                create a new object of type TYPENAME setting properties\n"
     "                in the order they are specified.  Note that the 'id'\n"
     "                property must be set.  These objects are placed in the\n"
-    "                '/objects' path.\n",
+    "                '/objects' path.\n"
+    "                Use '-object help' to print available backend types and\n"
+    "                '-object typename,help' to print relevant properties.\n",
     QEMU_ARCH_ALL)
 STEXI
 @item -object @var{typename}[,@var{prop1}=@var{value1},...]
@@ -3762,6 +3764,9 @@ in the order they are specified.  Note that the 'id'
 property must be set.  These objects are placed in the
 '/objects' path.
 
+Use '-object help' to print available backend types and
+'-object typename,help' to print relevant properties.
+
 @table @option
 
 @item -object memory-backend-file,id=@var{id},size=@var{size},mem-path=@var{dir},share=@var{on|off}
qom/object_interfaces.c +112 -0

124 lines not on this page · raw message

vl.c +5 -0
index ee557a1..a2230c8 100644
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
     page_size_init();
     socket_init();
 
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+                          NULL, NULL)) {
+        exit(1);
+    }
+
     if (qemu_opts_foreach(qemu_find_opts("object"),
                           user_creatable_add_opts_foreach,
                           object_create_initial, NULL)) {
Signature · 1 line
2.9.2
#1 lore raw
Quoted 38 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx                 |   7 ++-
 qom/object_interfaces.c         | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c                            |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
include/qom/object_interfaces.h +2 -0
index 8b17f4d..197cd5f 100644
@@ -165,4 +165,6 @@ int user_creatable_add_opts_foreach(void *opaque,
  */
 void user_creatable_del(const char *id, Error **errp);
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp);
+
 #endif
qemu-options.hx +3 -1
index a71aaf8..fade53c 100644
@@ -3752,7 +3752,9 @@ DEF("object", HAS_ARG, QEMU_OPTION_object,
     "                create a new object of type TYPENAME setting properties\n"
     "                in the order they are specified.  Note that the 'id'\n"
     "                property must be set.  These objects are placed in the\n"
-    "                '/objects' path.\n",
+    "                '/objects' path.\n"
+    "                Use '-object help' to print available backend types and\n"
+    "                '-object typename,help' to print relevant properties.\n",
     QEMU_ARCH_ALL)
 STEXI
 @item -object @var{typename}[,@var{prop1}=@var{value1},...]

qemu-options.hx in the order they are specified. Note that the 'id' · line 3769

Quoted 6 lines
qemu-options.hx +3 -0
@@ -3762,6 +3764,9 @@ in the order they are specified.  Note that the 'id'
 property must be set.  These objects are placed in the
 '/objects' path.
 
+Use '-object help' to print available backend types and
+'-object typename,help' to print relevant properties.
+

Make that

+Use @code{-object help} to print available backend types and +@code{-object @var{typename},help} to print relevant properties. +

Quoted 16 lines
qemu-options.hx +0 -0
 @table @option
 
 @item -object memory-backend-file,id=@var{id},size=@var{size},mem-path=@var{dir},share=@var{on|off}
qom/object_interfaces.c +1 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 221

Quoted 9 lines
qom/object_interfaces.c +6 -0
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
     object_unparent(obj);
 }
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+

Why this blank line?

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 13 lines
qom/object_interfaces.c +13 -0
+    int i;
+    char *values = NULL;
+    Object *obj;
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
+    struct EnumProperty {
+        const char * const *strings;
+        int (*get)(Object *, Error **);
+        void (*set)(Object *, int, Error **);
+    } *enumprop;

Copied from object.c. Big no-no. Whatever you do with this probably belongs into object.c instead.

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 247

Quoted 13 lines
qom/object_interfaces.c +13 -0
+
+    v = opts_visitor_new(opts);
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+        goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {

I think the Options visitor is overkill. Much simpler:

       type = qemu_opt_get(opts, "qom-type");
       if (type && is_help_option(type)) {

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 253

Quoted 6 lines
qom/object_interfaces.c +6 -0
+        printf("Available object backend types:\n");
+        GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+        while (list) {
+            const char *name;
+            name = object_class_get_name(OBJECT_CLASS(list->data));
+            if (strcmp(name, TYPE_USER_CREATABLE)) {

Why do you need to skip TYPE_USER_CREATABLE?

Hmm...

    $ qemu-system-x86_64 -object user-creatable,id=foo
    **
    ERROR:/work/armbru/qemu/qom/object.c:361:object_initialize_with_type: assertion failed (type->instance_size >= sizeof(Object)): (0 >= 40)
    Aborted (core dumped)

Should this type be abstract?

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 257

Quoted 4 lines
qom/object_interfaces.c +4 -0
+                printf("%s\n", name);
+            }
+            list = list->next;
+        }

Recommend to keep the loop control in one place:

           for (list = object_class_get_list(TYPE_USER_CREATABLE, false);
                list;
                list = list->next) {
               ...
           }

If you hate multi-line for (...), you can do

           GSList *head = object_class_get_list(TYPE_USER_CREATABLE, false);

           for (list = head; list; list->next) {
               ...
           }
Quoted 8 lines
qom/object_interfaces.c +8 -0
+        g_slist_free(list);
+        goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+        visit_end_struct(v, NULL);
+        return 0;
+    }

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 275

Quoted 10 lines
qom/object_interfaces.c +10 -0
+
+    if (!object_class_by_name(type)) {
+        printf("invalid object type: %s\n", type);
+        goto out_visit;
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+        ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));

Blank line between declarations and statements, please.

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 276

Quoted 1 line
qom/object_interfaces.c +1 -0
+        entry->value = g_malloc0(sizeof(ObjectPropertyInfo));

Either g_malloc0(sizeof(entry->value), or g_new0(ObjectPropertyInfo).

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 13 lines
qom/object_interfaces.c +13 -0
+        entry->next = props;
+        props = entry;
+        entry->value->name = g_strdup(prop->name);
+        i = 0;
+        enumprop = prop->opaque;
+        if (!g_str_equal(prop->type, "string") && \
+            !g_str_equal(prop->type, "bool") && \
+            !g_str_equal(prop->type, "struct tm") && \
+            !g_str_equal(prop->type, "int") && \
+            !g_str_equal(prop->type, "uint8") && \
+            !g_str_equal(prop->type, "uint16") && \
+            !g_str_equal(prop->type, "uint32") && \
+            !g_str_equal(prop->type, "uint64")) {

Uh, what are you trying to test with this condition? It's not obvious to me...

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 300

Quoted 11 lines
qom/object_interfaces.c +11 -0
+            while (enumprop->strings[i] != NULL) {
+                if (i != 0) {
+                    values = g_strdup_printf("%s/%s",
+                                            values, enumprop->strings[i]);
+                } else {
+                    values = g_strdup_printf("%s", enumprop->strings[i]);
+                }
+                i++;
+            }
+            entry->value->type = g_strdup_printf("%s, available values: %s",
+                                                prop->type, values);

Looks like this is the enum case. But why is the condition true exactly for enums?

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 304

Quoted 4 lines
qom/object_interfaces.c +4 -0
+        } else {
+            entry->value->type = g_strdup(prop->type);
+        }
+    }

The loop above collects properties, and ...

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 312

Quoted 8 lines
qom/object_interfaces.c +8 -0
+
+    start = props;
+    while (props != NULL) {
+        ObjectPropertyInfo *value = props->value;
+        printf("%s (%s)\n", value->name, value->type);
+        props = props->next;
+    }
+    qapi_free_ObjectPropertyInfoList(start);

... this loop prints them. Have you considered fusing the two loops?

Quoted 22 lines
qom/object_interfaces.c +14 -0
+
+out_visit:
+    visit_end_struct(v, NULL);
+
+out:
+    g_free(values);
+    g_free(type);
+    object_unref(obj);
+    if (local_err) {
+        error_report_err(local_err);
+    }
+    return 1;
+}
+
 static void register_types(void)
 {
     static const TypeInfo uc_interface_info = {
vl.c +0 -0
index ee557a1..a2230c8 100644

vl.c int main(int argc, char **argv, char **envp) · line 4258

Quoted 8 lines
vl.c +5 -0
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
     page_size_init();
     socket_init();
 
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+                          NULL, NULL)) {
+        exit(1);
+    }
+

I think this should be done as early as possible. However, we already do -device help nearby. Not fair to ask you to clean up this mess.

Quoted 3 lines
vl.c +0 -0
     if (qemu_opts_foreach(qemu_find_opts("object"),
                           user_creatable_add_opts_foreach,
                           object_create_initial, NULL)) {
#14 lore raw
In reply to Lin Ma
Quoted 36 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx                 |   7 ++-
 qom/object_interfaces.c         | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c                            |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
qom/object_interfaces.c +10 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
     object_unparent(obj);
 }
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+
+    int i;
+    char *values = NULL;
+    Object *obj;

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 10 lines
qom/object_interfaces.c +10 -0
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
+    struct EnumProperty {
+        const char * const *strings;
+        int (*get)(Object *, Error **);
+        void (*set)(Object *, int, Error **);
+    } *enumprop;

Ewww, this struct is declared privately in object.c and you're declaring it here so you can poke at private data belonging to the object internal impl. This is not ok in any way.

Quoted 44 lines
qom/object_interfaces.c +44 -0
+    v = opts_visitor_new(opts);
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+        goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {
+        printf("Available object backend types:\n");
+        GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+        while (list) {
+            const char *name;
+            name = object_class_get_name(OBJECT_CLASS(list->data));
+            if (strcmp(name, TYPE_USER_CREATABLE)) {
+                printf("%s\n", name);
+            }
+            list = list->next;
+        }
+        g_slist_free(list);
+        goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+        visit_end_struct(v, NULL);
+        return 0;
+    }
+
+    if (!object_class_by_name(type)) {
+        printf("invalid object type: %s\n", type);
+        goto out_visit;
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+        ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
+        entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
+        entry->next = props;
+        props = entry;
+        entry->value->name = g_strdup(prop->name);

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 10 lines
qom/object_interfaces.c +10 -0
+        i = 0;
+        enumprop = prop->opaque;
+        if (!g_str_equal(prop->type, "string") && \
+            !g_str_equal(prop->type, "bool") && \
+            !g_str_equal(prop->type, "struct tm") && \
+            !g_str_equal(prop->type, "int") && \
+            !g_str_equal(prop->type, "uint8") && \
+            !g_str_equal(prop->type, "uint16") && \
+            !g_str_equal(prop->type, "uint32") && \
+            !g_str_equal(prop->type, "uint64")) {

It is absolutely *not* safe to assume that the result of this condition is an enum.

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 293

Quoted 4 lines
qom/object_interfaces.c +4 -0
+            while (enumprop->strings[i] != NULL) {
+                if (i != 0) {
+                    values = g_strdup_printf("%s/%s",
+                                            values, enumprop->strings[i]);

Leaking the old memory for 'values'.

Quoted 39 lines
qom/object_interfaces.c +33 -0
+                } else {
+                    values = g_strdup_printf("%s", enumprop->strings[i]);
+                }
+                i++;
+            }
+            entry->value->type = g_strdup_printf("%s, available values: %s",
+                                                prop->type, values);
+        } else {
+            entry->value->type = g_strdup(prop->type);
+        }
+    }
+
+    start = props;
+    while (props != NULL) {
+        ObjectPropertyInfo *value = props->value;
+        printf("%s (%s)\n", value->name, value->type);
+        props = props->next;
+    }
+    qapi_free_ObjectPropertyInfoList(start);
+
+out_visit:
+    visit_end_struct(v, NULL);
+
+out:
+    g_free(values);
+    g_free(type);
+    object_unref(obj);
+    if (local_err) {
+        error_report_err(local_err);
+    }
+    return 1;
+}
+
 
vl.c +0 -0
index ee557a1..a2230c8 100644

vl.c int main(int argc, char **argv, char **envp) · line 4257

Quoted 7 lines
vl.c +4 -0
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
     page_size_init();
     socket_init();
 
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+                          NULL, NULL)) {
+        exit(1);
+    }

Generally 'help' is dealt with in the main option parsing, rather than adding a second iteration over the options. IOW, it would normally be done in user_creatable_add_opts_foreach, avoiding duplicating code between user_creatable_add_opts_foreach and user_creatable_help_func.

vl.c int main(int argc, char **argv, char **envp) · line 4261

Quoted 4 lines
vl.c +1 -0
+
     if (qemu_opts_foreach(qemu_find_opts("object"),
                           user_creatable_add_opts_foreach,
                           object_create_initial, NULL)) {

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
author
#2 lore raw
In reply to Markus Armbruster
Quoted 52 lines
Quoted 3 lines
Quoted 3 lines

Markus Armbruster <[email protected]> 2016/9/12 星期一 下午 11:42 >>>

Quoted 38 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx   			  |   7 ++-
 qom/object_interfaces.c   	  | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c   						 |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
include/qom/object_interfaces.h +2 -0
index 8b17f4d..197cd5f 100644
@@ -165,4 +165,6 @@ int user_creatable_add_opts_foreach(void *opaque,
  */
 void user_creatable_del(const char *id, Error **errp);
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp);
+
 #endif
qemu-options.hx +3 -1
index a71aaf8..fade53c 100644
@@ -3752,7 +3752,9 @@ DEF("object", HAS_ARG, QEMU_OPTION_object,
 	  "   			 create a new object of type TYPENAME setting properties\n"
 	  "   			 in the order they are specified.  Note that the 'id'\n"
 	  "   			 property must be set.  These objects are placed in the\n"
-    " 			   '/objects' path.\n",
+    " 			   '/objects' path.\n"
+    " 			   Use '-object help' to print available backend types and\n"
+    " 			   '-object typename,help' to print relevant properties.\n",
 	  QEMU_ARCH_ALL)
 STEXI
 @item -object @var{typename}[,@var{prop1}=@var{value1},...]

qemu-options.hx in the order they are specified. Note that the 'id' · line 3769

Quoted 6 lines
qemu-options.hx +3 -0
@@ -3762,6 +3764,9 @@ in the order they are specified.  Note that the 'id'
 property must be set.  These objects are placed in the
 '/objects' path.
 
+Use '-object help' to print available backend types and
+'-object typename,help' to print relevant properties.
+

Make that

Quoted 4 lines

+Use @code{-object help} to print available backend types and +@code{-object @var{typename},help} to print relevant properties. +

Ok.

Quoted 19 lines
Quoted 19 lines
qemu-options.hx +0 -0
 @table @option
 
 @item -object memory-backend-file,id=@var{id},size=@var{size},mem-path=@var{dir},share=@var{on|off}
qom/object_interfaces.c +1 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
 	  object_unparent(obj);
 }
 
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 221

Quoted 6 lines
qom/object_interfaces.c +6 -0
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+

Why this blank line?

I'll remove it.

Quoted 8 lines
Quoted 8 lines
qom/object_interfaces.c +8 -0
+    int i;
+    char *values = NULL;
+    Object *obj;
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 5 lines
qom/object_interfaces.c +5 -0
+    struct EnumProperty {
+	    const char * const *strings;
+	    int (*get)(Object *, Error **);
+	    void (*set)(Object *, int, Error **);
+    } *enumprop;

Copied from object.c. Big no-no. Whatever you do with this probably belongs into object.c instead.

Yes, this way is ugly.
What I want to do is parsing the enum <-> string mappings through the EnumProperty to make the output more reasonable.
It should be parsed in object.c, But I can't assume the size of enum str list, then can't pre-alloc it in user_creatable_help_func. So far I havn't figured out a good way to return a string array that including the enum str list to user_creatable_help_func for printing. May I have your suggestions?

Quoted 2 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 236

Quoted 2 lines
qom/object_interfaces.c +2 -0
+
+

v = opts_visitor_new(opts);

Quoted 14 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 247

Quoted 11 lines
qom/object_interfaces.c +11 -0
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+	    goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+	    goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {

I think the Options visitor is overkill. Much simpler:

Quoted 3 lines
	   type = qemu_opt_get(opts, "qom-type");
	   if (type && is_help_option(type)) {

Yes, it is much simpler, I'll use this instead of visitor code.

Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 253

Quoted 6 lines
qom/object_interfaces.c +6 -0
+	    printf("Available object backend types:\n");
+	    GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+	    while (list) {
+		    const char *name;
+		    name = object_class_get_name(OBJECT_CLASS(list->data));
+		    if (strcmp(name, TYPE_USER_CREATABLE)) {

Why do you need to skip TYPE_USER_CREATABLE?

Quoted 9 lines

Hmm...

   $ qemu-system-x86_64 -object user-creatable,id=foo
   **
   ERROR:/work/armbru/qemu/qom/object.c:361:object_initialize_with_type: assertion failed (type->instance_size >= sizeof(Object)): (0 >= 40)
   Aborted (core dumped)

Should this type be abstract?

Yes, The object type user-creatable is abstract, But obviously it missed the abstract property. I'll add it in next patch(patch 1/2), Then I dont need to skip it at here anymore.

Quoted 13 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 257

Quoted 4 lines
qom/object_interfaces.c +4 -0
+			    printf("%s\n", name);
+		    }
+		    list = list->next;
+	    }

Recommend to keep the loop control in one place:

		   for (list = object_class_get_list(TYPE_USER_CREATABLE, false);
			    list;
			    list = list->next) {
			   ...
		   }
Quoted 8 lines

If you hate multi-line for (...), you can do

		   GSList *head = object_class_get_list(TYPE_USER_CREATABLE, false);

		   for (list = head; list; list->next) {
			   ...
		   }

Will do it.

Quoted 12 lines
Quoted 12 lines
qom/object_interfaces.c +12 -0
+	    g_slist_free(list);
+	    goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+	    visit_end_struct(v, NULL);
+	    return 0;
+    }
+
+    if (!object_class_by_name(type)) {
+	    printf("invalid object type: %s\n", type);
+	    goto out_visit;
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 275

Quoted 6 lines
qom/object_interfaces.c +6 -0
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+	    ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));

Blank line between declarations and statements, please.

ok.

Quoted 4 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 276

Quoted 1 line
qom/object_interfaces.c +1 -0
+	    entry->value = g_malloc0(sizeof(ObjectPropertyInfo));

Either g_malloc0(sizeof(entry->value), or g_new0(ObjectPropertyInfo).

will use g_malloc0(sizeof(entry->value).

Quoted 17 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 13 lines
qom/object_interfaces.c +13 -0
+	    entry->next = props;
+	    props = entry;
+	    entry->value->name = g_strdup(prop->name);
+	    i = 0;
+	    enumprop = prop->opaque;
+	    if (!g_str_equal(prop->type, "string") && \
+		    !g_str_equal(prop->type, "bool") && \
+		    !g_str_equal(prop->type, "struct tm") && \
+		    !g_str_equal(prop->type, "int") && \
+		    !g_str_equal(prop->type, "uint8") && \
+		    !g_str_equal(prop->type, "uint16") && \
+		    !g_str_equal(prop->type, "uint32") && \
+		    !g_str_equal(prop->type, "uint64")) {

Uh, what are you trying to test with this condition? It's not obvious to me...

Quoted 15 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 300

Quoted 11 lines
qom/object_interfaces.c +11 -0
+		    while (enumprop->strings[i] != NULL) {
+			    if (i != 0) {
+				    values = g_strdup_printf("%s/%s",
+										    values, enumprop->strings[i]);
+			    } else {
+				    values = g_strdup_printf("%s", enumprop->strings[i]);
+			    }
+			    i++;
+		    }
+		    entry->value->type = g_strdup_printf("%s, available values: %s",
+											    prop->type, values);

Looks like this is the enum case. But why is the condition true exactly for enums?

Yes, After filter all the other types above, I assume the result here is the enum case. I knew this way does not make sense, But I havn't figured out a proper way yet to judge whether the type is an enum or not.
May I have your ideas?

Quoted 1 line

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 301

Quoted 1 line
qom/object_interfaces.c +1 -0
+
	    } else {
Quoted 6 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 304

Quoted 3 lines
qom/object_interfaces.c +3 -0
+		    entry->value->type = g_strdup(prop->type);
+	    }
+    }

The loop above collects properties, and ...

Quoted 11 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 312

Quoted 8 lines
qom/object_interfaces.c +8 -0
+
+    start = props;
+    while (props != NULL) {
+	    ObjectPropertyInfo *value = props->value;
+	    printf("%s (%s)\n", value->name, value->type);
+	    props = props->next;
+    }
+    qapi_free_ObjectPropertyInfoList(start);

... this loop prints them. Have you considered fusing the two loops?

I agreed. will merge the two loops.

Quoted 25 lines
Quoted 25 lines
qom/object_interfaces.c +14 -0
+
+out_visit:
+    visit_end_struct(v, NULL);
+
+out:
+    g_free(values);
+    g_free(type);
+    object_unref(obj);
+    if (local_err) {
+	    error_report_err(local_err);
+    }
+    return 1;
+}
+
 static void register_types(void)
 {
 	  static const TypeInfo uc_interface_info = {
vl.c +0 -0
index ee557a1..a2230c8 100644
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
 	  page_size_init();
 	  socket_init();
 
Quoted 9 lines

vl.c int main(int argc, char **argv, char **envp) · line 4258

Quoted 5 lines
vl.c +5 -0
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+						  NULL, NULL)) {
+	    exit(1);
+    }
+

I think this should be done as early as possible. However, we already do -device help nearby. Not fair to ask you to clean up this mess.

How about move it to here?

	 object_property_add_child(object_get_root(), "machine",
							   OBJECT(current_machine), &error_abort);

+
+ if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+ NULL, NULL)) {
+ exit(1);
+ }
+

	 cpu_exec_init_all();
Quoted 3 lines

vl.c int main(int argc, char **argv, char **envp) · line 4261

Quoted 3 lines
vl.c +0 -0
 	  if (qemu_opts_foreach(qemu_find_opts("object"),
 						    user_creatable_add_opts_foreach,
 						    object_create_initial, NULL)) {

Thank you for reviewing the code.
Lin

author
#15 lore raw
In reply to Daniel P. Berrange
Quoted 47 lines
Quoted 3 lines
Quoted 3 lines

"Daniel P. Berrange" <[email protected]> 2016/9/12 星期一 下午 11:56 >>>

Quoted 43 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx   			  |   7 ++-
 qom/object_interfaces.c   	  | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c   						 |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)
qom/object_interfaces.c +17 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
 	  object_unparent(obj);
 }
 
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+
+    int i;
+    char *values = NULL;
+    Object *obj;
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
+    struct EnumProperty {
+	    const char * const *strings;
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 3 lines
qom/object_interfaces.c +3 -0
+	    int (*get)(Object *, Error **);
+	    void (*set)(Object *, int, Error **);
+    } *enumprop;

Ewww, this struct is declared privately in object.c and you're declaring it here so you can poke at private data belonging to the object internal impl. This is not ok in any way.

Yes, this way is ugly.
What I want to do is parsing the enum <-> string mappings through the EnumProperty to make the output more reasonable.
It should be parsed in object.c, But I can't assume the size of enum str list, then can't pre-alloc it in user_creatable_help_func. So far I havn't figured out a good way to return a string array that including the enum str list to user_creatable_help_func for printing. May I have your suggestions?

Quoted 43 lines
Quoted 43 lines
qom/object_interfaces.c +43 -0
+    v = opts_visitor_new(opts);
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+	    goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+	    goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {
+	    printf("Available object backend types:\n");
+	    GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+	    while (list) {
+		    const char *name;
+		    name = object_class_get_name(OBJECT_CLASS(list->data));
+		    if (strcmp(name, TYPE_USER_CREATABLE)) {
+			    printf("%s\n", name);
+		    }
+		    list = list->next;
+	    }
+	    g_slist_free(list);
+	    goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+	    visit_end_struct(v, NULL);
+	    return 0;
+    }
+
+    if (!object_class_by_name(type)) {
+	    printf("invalid object type: %s\n", type);
+	    goto out_visit;
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+	    ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
+	    entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
+	    entry->next = props;
+	    props = entry;
Quoted 8 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp)

Quoted 8 lines
qom/object_interfaces.c +8 -0
+	    entry->value->name = g_strdup(prop->name);
+	    i = 0;
+	    enumprop = prop->opaque;
+	    if (!g_str_equal(prop->type, "string") && \
+		    !g_str_equal(prop->type, "bool") && \
+		    !g_str_equal(prop->type, "struct tm") && \
+		    !g_str_equal(prop->type, "int") && \
+		    !g_str_equal(prop->

type, "uint8") && \

Quoted 7 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 3 lines
qom/object_interfaces.c +3 -0
+		    !g_str_equal(prop->type, "uint16") && \
+		    !g_str_equal(prop->type, "uint32") && \
+		    !g_str_equal(prop->type, "uint64")) {

It is absolutely *not* safe to assume that the result of this condition is an enum.

Yes, It does not make sense and is not safe. Writing this way becasue I can't figure out a way to judge whether the type is an enum or not. May I have your ideas?

Quoted 7 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 293

Quoted 4 lines
qom/object_interfaces.c +4 -0
+		    while (enumprop->strings[i] != NULL) {
+			    if (i != 0) {
+				    values = g_strdup_printf("%s/%s",
+										    values, enumprop->strings[i]);

Leaking the old memory for 'values'.

Ok, I'll fix it.

Quoted 42 lines
Quoted 42 lines
qom/object_interfaces.c +33 -0
+			    } else {
+				    values = g_strdup_printf("%s", enumprop->strings[i]);
+			    }
+			    i++;
+		    }
+		    entry->value->type = g_strdup_printf("%s, available values: %s",
+											    prop->type, values);
+	    } else {
+		    entry->value->type = g_strdup(prop->type);
+	    }
+    }
+
+    start = props;
+    while (props != NULL) {
+	    ObjectPropertyInfo *value = props->value;
+	    printf("%s (%s)\n", value->name, value->type);
+	    props = props->next;
+    }
+    qapi_free_ObjectPropertyInfoList(start);
+
+out_visit:
+    visit_end_struct(v, NULL);
+
+out:
+    g_free(values);
+    g_free(type);
+    object_unref(obj);
+    if (local_err) {
+	    error_report_err(local_err);
+    }
+    return 1;
+}
+
 
vl.c +0 -0
index ee557a1..a2230c8 100644
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
 	  page_size_init();
 	  socket_init();
 
Quoted 10 lines

vl.c int main(int argc, char **argv, char **envp) · line 4257

Quoted 4 lines
vl.c +4 -0
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+						  NULL, NULL)) {
+	    exit(1);
+    }

Generally 'help' is dealt with in the main option parsing, rather than adding a second iteration over the options. IOW, it would normally be done in user_creatable_add_opts_foreach, avoiding duplicating code between user_creatable_add_opts_foreach and user_creatable_help_func.

I used to consider to put it in user_creatable_add_opts_foreach, but I think that keeping it as a separate function maybe more clear. Meanwhile, I'd like to add these help output to monitor command 'object_add' later, So puting the code into user_creatable_help_func may help as well.

Quoted 7 lines

vl.c int main(int argc, char **argv, char **envp) · line 4261

Quoted 4 lines
vl.c +1 -0
+
 	  if (qemu_opts_foreach(qemu_find_opts("object"),
 						    user_creatable_add_opts_foreach,
 						    object_create_initial, NULL)) {

Regards,
Daniel

Thanks for helping to review the code.
Lin

#3 lore raw
In reply to Lin Ma

This is about QOM use. Cc: Andreas in case he has smart ideas. Andreas, you may want to skip ahead to "EnumProperty".

Quoted 14 lines · Lin Ma
Quoted 14 lines
Quoted 1 line
Quoted 1 line

Markus Armbruster <[email protected]> 2016/9/12 星期一 下午 11:42 >>>

Quoted 11 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx   			  |   7 ++-
 qom/object_interfaces.c   	  | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c   						 |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)

[...]

Quoted 44 lines
Quoted 16 lines
Quoted 16 lines
qom/object_interfaces.c +1 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
 	  object_unparent(obj);
 }
 
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 221

Quoted 6 lines
qom/object_interfaces.c +6 -0
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+

Why this blank line?

I'll remove it.

Quoted 8 lines
Quoted 8 lines
qom/object_interfaces.c +8 -0
+    int i;
+    char *values = NULL;
+    Object *obj;
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 5 lines
qom/object_interfaces.c +5 -0
+    struct EnumProperty {
+	    const char * const *strings;
+	    int (*get)(Object *, Error **);
+	    void (*set)(Object *, int, Error **);
+    } *enumprop;

Copied from object.c. Big no-no. Whatever you do with this probably belongs into object.c instead.

Quoted 7 lines

Yes, this way is ugly.
What I want to do is parsing the enum <-> string mappings through the EnumProperty to make the output more reasonable.
It should be parsed in object.c, But I can't assume the size of enum str list, then can't pre-alloc it in user_creatable_help_func. So far I havn't figured out a good way to return a string array that including the enum str list to user_creatable_help_func for printing. May I have your suggestions?

See below.

Quoted 38 lines
Quoted 2 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 236

Quoted 2 lines
qom/object_interfaces.c +2 -0
+
+   

v = opts_visitor_new(opts);

Quoted 14 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 247

Quoted 11 lines
qom/object_interfaces.c +11 -0
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+	    goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+	    goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {

I think the Options visitor is overkill. Much simpler:

Quoted 3 lines
	   type = qemu_opt_get(opts, "qom-type");
	   if (type && is_help_option(type)) {

Yes, it is much simpler, I'll use this instead of visitor code.

Quoted 16 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 253

Quoted 6 lines
qom/object_interfaces.c +6 -0
+	    printf("Available object backend types:\n");
+	    GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+	    while (list) {
+		    const char *name;
+		    name = object_class_get_name(OBJECT_CLASS(list->data));
+		    if (strcmp(name, TYPE_USER_CREATABLE)) {

Why do you need to skip TYPE_USER_CREATABLE?

Hmm...

   $ qemu-system-x86_64 -object user-creatable,id=foo
   **
   ERROR:/work/armbru/qemu/qom/object.c:361:object_initialize_with_type: assertion failed (type->instance_size >= sizeof(Object)): (0 >= 40)
   Aborted (core dumped)
Quoted 4 lines
Quoted 2 lines

Should this type be abstract?

Yes, The object type user-creatable is abstract, But obviously it missed the abstract property. I'll add it in next patch(patch 1/2), Then I dont need to skip it at here anymore.

Yes, please.

Quoted 84 lines
Quoted 13 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 257

Quoted 4 lines
qom/object_interfaces.c +4 -0
+			    printf("%s\n", name);
+		    }
+		    list = list->next;
+	    }

Recommend to keep the loop control in one place:

		   for (list = object_class_get_list(TYPE_USER_CREATABLE, false);
			    list;
			    list = list->next) {
			   ...
		   }
Quoted 8 lines

If you hate multi-line for (...), you can do

		   GSList *head = object_class_get_list(TYPE_USER_CREATABLE, false);

		   for (list = head; list; list->next) {
			   ...
		   }

Will do it.

Quoted 12 lines
Quoted 12 lines
qom/object_interfaces.c +12 -0
+	    g_slist_free(list);
+	    goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+	    visit_end_struct(v, NULL);
+	    return 0;
+    }
+
+    if (!object_class_by_name(type)) {
+	    printf("invalid object type: %s\n", type);
+	    goto out_visit;
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 275

Quoted 6 lines
qom/object_interfaces.c +6 -0
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+	    ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));

Blank line between declarations and statements, please.

ok.

Quoted 4 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 276

Quoted 1 line
qom/object_interfaces.c +1 -0
+	    entry->value = g_malloc0(sizeof(ObjectPropertyInfo));

Either g_malloc0(sizeof(entry->value), or g_new0(ObjectPropertyInfo).

will use g_malloc0(sizeof(entry->value).

Quoted 17 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 13 lines
qom/object_interfaces.c +13 -0
+	    entry->next = props;
+	    props = entry;
+	    entry->value->name = g_strdup(prop->name);
+	    i = 0;
+	    enumprop = prop->opaque;
+	    if (!g_str_equal(prop->type, "string") && \
+		    !g_str_equal(prop->type, "bool") && \
+		    !g_str_equal(prop->type, "struct tm") && \
+		    !g_str_equal(prop->type, "int") && \
+		    !g_str_equal(prop->type, "uint8") && \
+		    !g_str_equal(prop->type, "uint16") && \
+		    !g_str_equal(prop->type, "uint32") && \
+		    !g_str_equal(prop->type, "uint64")) {

Uh, what are you trying to test with this condition? It's not obvious to me...

Quoted 15 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 300

Quoted 11 lines
qom/object_interfaces.c +11 -0
+		    while (enumprop->strings[i] != NULL) {
+			    if (i != 0) {
+				    values = g_strdup_printf("%s/%s",
+										    values, enumprop->strings[i]);
+			    } else {
+				    values = g_strdup_printf("%s", enumprop->strings[i]);
+			    }
+			    i++;
+		    }
+		    entry->value->type = g_strdup_printf("%s, available values: %s",
+											    prop->type, values);

Looks like this is the enum case. But why is the condition true exactly for enums?

Quoted 4 lines

Yes, After filter all the other types above, I assume the result here is the enum case. I knew this way does not make sense, But I havn't figured out a proper way yet to judge whether the type is an enum or not.
May I have your ideas?

You're messing with struct EnumProperty because you want more help than what ObjectPropertyInfo can provice.

Digression on the meaning of ObjectPropertyInfo. This is its definition:

##
# @ObjectPropertyInfo:
#
# @name: the name of the property
#
# @type: the type of the property. This will typically come in one of four
# forms:
#
# 1) A primitive type such as 'u8', 'u16', 'bool', 'str', or 'double'.
# These types are mapped to the appropriate JSON type.
#
# 2) A child type in the form 'child<subtype>' where subtype is a qdev
# device type name. Child properties create the composition tree.
#
# 3) A link type in the form 'link<subtype>' where subtype is a qdev
# device type name. Link properties form the device model graph.
#
# Since: 1.2
##
{ 'struct': 'ObjectPropertyInfo',
'data': { 'name': 'str', 'type': 'str' } }

@type is documented to be either a primitive type, a child type or a link. "Primitive type" isn't defined. The examples given suggest it's a QAPI built-in type. If that's correct, clause 1) does not cover enumeration types. However, it doesn't seem to be correct: I observe 'string', not 'str'. Paolo, Andreas, any idea what @type is supposed to mean?

End of digression.

All ObjectPropertyInfo gives you is two strings: a name and a type. If you need more information than that, you have to add a proper interface to get it! Say a function that takes an object and a property name, and returns information about the property's type. Probably should be two functions, one that maps QOM object and property name to the property's QOM type, one that maps a QOM type to QOM type information.

In other words, you need QOM object and type introspection. Paolo, Andreas, if that already exists in some form, please point us to it.

Quoted 64 lines
Quoted 1 line

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 301

Quoted 1 line
qom/object_interfaces.c +1 -0
+
	    } else {
Quoted 6 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 304

Quoted 3 lines
qom/object_interfaces.c +3 -0
+		    entry->value->type = g_strdup(prop->type);
+	    }
+    }

The loop above collects properties, and ...

Quoted 11 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 312

Quoted 8 lines
qom/object_interfaces.c +8 -0
+
+    start = props;
+    while (props != NULL) {
+	    ObjectPropertyInfo *value = props->value;
+	    printf("%s (%s)\n", value->name, value->type);
+	    props = props->next;
+    }
+    qapi_free_ObjectPropertyInfoList(start);

... this loop prints them. Have you considered fusing the two loops?

I agreed. will merge the two loops.

Quoted 25 lines
Quoted 25 lines
qom/object_interfaces.c +14 -0
+
+out_visit:
+    visit_end_struct(v, NULL);
+
+out:
+    g_free(values);
+    g_free(type);
+    object_unref(obj);
+    if (local_err) {
+	    error_report_err(local_err);
+    }
+    return 1;
+}
+
 static void register_types(void)
 {
 	  static const TypeInfo uc_interface_info = {
vl.c +0 -0
index ee557a1..a2230c8 100644
@@ -4251,6 +4251,11 @@ int main(int argc, char **argv, char **envp)
 	  page_size_init();
 	  socket_init();
 
Quoted 9 lines

vl.c int main(int argc, char **argv, char **envp) · line 4258

Quoted 5 lines
vl.c +5 -0
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+						  NULL, NULL)) {
+	    exit(1);
+    }
+

I think this should be done as early as possible. However, we already do -device help nearby. Not fair to ask you to clean up this mess.

How about move it to here?

	 object_property_add_child(object_get_root(), "machine",
							   OBJECT(current_machine), &error_abort);
vl.c +6 -0
+
+    if (qemu_opts_foreach(qemu_find_opts("object"), user_creatable_help_func,
+						  NULL, NULL)) {
+	    exit(1);
+    }
+
Quoted 1 line
	 cpu_exec_init_all();

I wouldn't put it in the middle of the machine stuff. Perhaps before current_machine = MACHINE(...)?

Quoted 6 lines
Quoted 3 lines

vl.c int main(int argc, char **argv, char **envp) · line 4261

Quoted 3 lines
vl.c +0 -0
 	  if (qemu_opts_foreach(qemu_find_opts("object"),
 						    user_creatable_add_opts_foreach,
 						    object_create_initial, NULL)) {

Thank you for reviewing the code.
Lin

#4 lore raw

Hi Lin and Markus,

Am 19.09.2016 um 13:58 schrieb Markus Armbruster:

Quoted 252 lines

This is about QOM use. Cc: Andreas in case he has smart ideas. Andreas, you may want to skip ahead to "EnumProperty".

Quoted 14 lines · Lin Ma
Quoted 14 lines
Quoted 1 line
Quoted 1 line

Markus Armbruster <[email protected]> 2016/9/12 星期一 下午 11:42 >>>

Quoted 11 lines · Lin Ma

'-object help' prints available user creatable backends. '-object $typename,help' prints relevant properties.

---
 include/qom/object_interfaces.h |   2 +
 qemu-options.hx   			  |   7 ++-
 qom/object_interfaces.c   	  | 112 ++++++++++++++++++++++++++++++++++++++++
 vl.c   						 |   5 ++
 4 files changed, 125 insertions(+), 1 deletion(-)

[...]

Quoted 44 lines
Quoted 16 lines
Quoted 16 lines
qom/object_interfaces.c +1 -0
index bf59846..4ee8643 100644
@@ -5,6 +5,7 @@
 #include "qapi-visit.h"
 #include "qapi/qmp-output-visitor.h"
 #include "qapi/opts-visitor.h"
+#include "qemu/help_option.h"
 
 void user_creatable_complete(Object *obj, Error **errp)
 {
@@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error **errp)
 	  object_unparent(obj);
 }
 
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 221

Quoted 6 lines
qom/object_interfaces.c +6 -0
+int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **errp)
+{
+    Visitor *v;
+    char *type = NULL;
+    Error *local_err = NULL;
+

Why this blank line?

I'll remove it.

Quoted 8 lines
Quoted 8 lines
qom/object_interfaces.c +8 -0
+    int i;
+    char *values = NULL;
+    Object *obj;
+    ObjectPropertyInfoList *props = NULL;
+    ObjectProperty *prop;
+    ObjectPropertyIterator iter;
+    ObjectPropertyInfoList *start;
+
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 234

Quoted 5 lines
qom/object_interfaces.c +5 -0
+    struct EnumProperty {
+	    const char * const *strings;
+	    int (*get)(Object *, Error **);
+	    void (*set)(Object *, int, Error **);
+    } *enumprop;

Copied from object.c. Big no-no. Whatever you do with this probably belongs into object.c instead.

Quoted 7 lines

Yes, this way is ugly.
What I want to do is parsing the enum <-> string mappings through the EnumProperty to make the output more reasonable.
It should be parsed in object.c, But I can't assume the size of enum str list, then can't pre-alloc it in user_creatable_help_func. So far I havn't figured out a good way to return a string array that including the enum str list to user_creatable_help_func for printing. May I have your suggestions?

See below.

Quoted 38 lines
Quoted 2 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 236

Quoted 2 lines
qom/object_interfaces.c +2 -0
+
+   

v = opts_visitor_new(opts);

Quoted 14 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 247

Quoted 11 lines
qom/object_interfaces.c +11 -0
+    visit_start_struct(v, NULL, NULL, 0, &local_err);
+    if (local_err) {
+	    goto out;
+    }
+
+    visit_type_str(v, "qom-type", &type, &local_err);
+    if (local_err) {
+	    goto out_visit;
+    }
+
+    if (type && is_help_option(type)) {

I think the Options visitor is overkill. Much simpler:

Quoted 3 lines
	   type = qemu_opt_get(opts, "qom-type");
	   if (type && is_help_option(type)) {

Yes, it is much simpler, I'll use this instead of visitor code.

Quoted 16 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 253

Quoted 6 lines
qom/object_interfaces.c +6 -0
+	    printf("Available object backend types:\n");
+	    GSList *list = object_class_get_list(TYPE_USER_CREATABLE, false);
+	    while (list) {
+		    const char *name;
+		    name = object_class_get_name(OBJECT_CLASS(list->data));
+		    if (strcmp(name, TYPE_USER_CREATABLE)) {

Why do you need to skip TYPE_USER_CREATABLE?

Hmm...

   $ qemu-system-x86_64 -object user-creatable,id=foo
   **
   ERROR:/work/armbru/qemu/qom/object.c:361:object_initialize_with_type: assertion failed (type->instance_size >= sizeof(Object)): (0 >= 40)
   Aborted (core dumped)
Quoted 4 lines
Quoted 2 lines

Should this type be abstract?

Yes, The object type user-creatable is abstract, But obviously it missed the abstract property. I'll add it in next patch(patch 1/2), Then I dont need to skip it at here anymore.

Yes, please.

Quoted 84 lines
Quoted 13 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 257

Quoted 4 lines
qom/object_interfaces.c +4 -0
+			    printf("%s\n", name);
+		    }
+		    list = list->next;
+	    }

Recommend to keep the loop control in one place:

		   for (list = object_class_get_list(TYPE_USER_CREATABLE, false);
			    list;
			    list = list->next) {
			   ...
		   }
Quoted 8 lines

If you hate multi-line for (...), you can do

		   GSList *head = object_class_get_list(TYPE_USER_CREATABLE, false);

		   for (list = head; list; list->next) {
			   ...
		   }

Will do it.

Quoted 12 lines
Quoted 12 lines
qom/object_interfaces.c +12 -0
+	    g_slist_free(list);
+	    goto out_visit;
+    }
+
+    if (!type || !qemu_opt_has_help_opt(opts)) {
+	    visit_end_struct(v, NULL);
+	    return 0;
+    }
+
+    if (!object_class_by_name(type)) {
+	    printf("invalid object type: %s\n", type);
+	    goto out_visit;
Quoted 9 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 275

Quoted 6 lines
qom/object_interfaces.c +6 -0
+    }
+    obj = object_new(type);
+    object_property_iter_init(&iter, obj);
+
+    while ((prop = object_property_iter_next(&iter))) {
+	    ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));

Blank line between declarations and statements, please.

ok.

Quoted 4 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 276

Quoted 1 line
qom/object_interfaces.c +1 -0
+	    entry->value = g_malloc0(sizeof(ObjectPropertyInfo));

Either g_malloc0(sizeof(entry->value), or g_new0(ObjectPropertyInfo).

will use g_malloc0(sizeof(entry->value).

Quoted 17 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 289

Quoted 13 lines
qom/object_interfaces.c +13 -0
+	    entry->next = props;
+	    props = entry;
+	    entry->value->name = g_strdup(prop->name);
+	    i = 0;
+	    enumprop = prop->opaque;
+	    if (!g_str_equal(prop->type, "string") && \
+		    !g_str_equal(prop->type, "bool") && \
+		    !g_str_equal(prop->type, "struct tm") && \
+		    !g_str_equal(prop->type, "int") && \
+		    !g_str_equal(prop->type, "uint8") && \
+		    !g_str_equal(prop->type, "uint16") && \
+		    !g_str_equal(prop->type, "uint32") && \
+		    !g_str_equal(prop->type, "uint64")) {

Uh, what are you trying to test with this condition? It's not obvious to me...

Quoted 15 lines

qom/object_interfaces.c void user_creatable_del(const char *id, Error **errp) · line 300

Quoted 11 lines
qom/object_interfaces.c +11 -0
+		    while (enumprop->strings[i] != NULL) {
+			    if (i != 0) {
+				    values = g_strdup_printf("%s/%s",
+										    values, enumprop->strings[i]);
+			    } else {
+				    values = g_strdup_printf("%s", enumprop->strings[i]);
+			    }
+			    i++;
+		    }
+		    entry->value->type = g_strdup_printf("%s, available values: %s",
+											    prop->type, values);

Looks like this is the enum case. But why is the condition true exactly for enums?

Quoted 4 lines

Yes, After filter all the other types above, I assume the result here is the enum case. I knew this way does not make sense, But I havn't figured out a proper way yet to judge whether the type is an enum or not.
May I have your ideas?

You're messing with struct EnumProperty because you want more help than what ObjectPropertyInfo can provice.

Digression on the meaning of ObjectPropertyInfo. This is its definition:

##
# @ObjectPropertyInfo:
#
# @name: the name of the property
#
# @type: the type of the property. This will typically come in one of four
# forms:
#
# 1) A primitive type such as 'u8', 'u16', 'bool', 'str', or 'double'.
# These types are mapped to the appropriate JSON type.
#
# 2) A child type in the form 'child<subtype>' where subtype is a qdev
# device type name. Child properties create the composition tree.
#
# 3) A link type in the form 'link<subtype>' where subtype is a qdev
# device type name. Link properties form the device model graph.
#
# Since: 1.2
##
{ 'struct': 'ObjectPropertyInfo',
'data': { 'name': 'str', 'type': 'str' } }

@type is documented to be either a primitive type, a child type or a link. "Primitive type" isn't defined. The examples given suggest it's a QAPI built-in type. If that's correct, clause 1) does not cover enumeration types. However, it doesn't seem to be correct: I observ 'string', not 'str'. Paolo, Andreas, any idea what @type is supposed to mean?

End of digression.

All ObjectPropertyInfo gives you is two strings: a name and a type. If you need more information than that, you have to add a proper interface to get it! Say a function that takes an object and a property name, and returns information about the property's type. Probably should be two functions, one that maps QOM object and property name to the property's QOM type, one that maps a QOM type to QOM type information.

Quoted 2 lines

In other words, you need QOM object and type introspection. Paolo, Andreas, if that already exists in some form, please point us to it.

Could you say what exactly you want to introspect here?

Both ObjectClass and Object have a list of properties that together form the list of properties that can be set on an instance. So you'll need to instantiate the object like I think we do for devices. Then you can recursively enumerate the properties to get their names and types (and possibly put them into a new list for alphabetical sorting if desired).

Regards,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
#5 lore raw
Quoted 3 lines · Andreas Färber

Hi Lin and Markus,

Am 19.09.2016 um 13:58 schrieb Markus Armbruster:

[...]

Quoted 44 lines
Quoted 44 lines

You're messing with struct EnumProperty because you want more help than what ObjectPropertyInfo can provice.

Digression on the meaning of ObjectPropertyInfo. This is its definition:

##
# @ObjectPropertyInfo:
#
# @name: the name of the property
#
# @type: the type of the property. This will typically come in one of four
# forms:
#
# 1) A primitive type such as 'u8', 'u16', 'bool', 'str', or 'double'.
# These types are mapped to the appropriate JSON type.
#
# 2) A child type in the form 'child<subtype>' where subtype is a qdev
# device type name. Child properties create the composition tree.
#
# 3) A link type in the form 'link<subtype>' where subtype is a qdev
# device type name. Link properties form the device model graph.
#
# Since: 1.2
##
{ 'struct': 'ObjectPropertyInfo',
'data': { 'name': 'str', 'type': 'str' } }

@type is documented to be either a primitive type, a child type or a link. "Primitive type" isn't defined. The examples given suggest it's a QAPI built-in type. If that's correct, clause 1) does not cover enumeration types. However, it doesn't seem to be correct: I observ 'string', not 'str'. Paolo, Andreas, any idea what @type is supposed to mean?

End of digression.

All ObjectPropertyInfo gives you is two strings: a name and a type. If you need more information than that, you have to add a proper interface to get it! Say a function that takes an object and a property name, and returns information about the property's type. Probably should be two functions, one that maps QOM object and property name to the property's QOM type, one that maps a QOM type to QOM type information.

Quoted 4 lines
Quoted 2 lines

In other words, you need QOM object and type introspection. Paolo, Andreas, if that already exists in some form, please point us to it.

Could you say what exactly you want to introspect here?

Context: Lin wants to implement "-object TYPE-NAME,help", similar to "-device DRIVER-NAME,help", i.e. list the available properties of TYPE-NAME.

His proposed patch tries to give better help for enumeration types by listing the acceptable values. The code that does it is an unacceptable hack, though. We're trying to figure out a way to provide such help cleanly.

One way to do it would be introspecting QOM *types*. Find the property's type, figure out what kind of type it is, if it's an enumeration type, find its members, ...

Quoted 5 lines

Both ObjectClass and Object have a list of properties that together form the list of properties that can be set on an instance. So you'll need to instantiate the object like I think we do for devices. Then you can recursively enumerate the properties to get their names and types (and possibly put them into a new list for alphabetical sorting if desired).

Lin's code uses object_new() to instantiate a dummy object, object_property_iter_init() and object_property_iter_next() to find the properties. Sounds okay so far, doesn't it?

Hmm, ObjectProperty has members name, type, description. Could description be useful? I guess it's set with object_property_set_description(). I can see only a few dozen calls, which makes me suspect it's null more often than not.

When it's null we could still fall back to a description of the type. Does such a thing exist? Enumeration types could provide one listing their values.

Other ideas?

author
#6 lore raw
Quoted 77 lines
Quoted 3 lines
Quoted 3 lines

Markus Armbruster <[email protected]> 2016/9/20 星期二 上午 1:13 >>>

Quoted 3 lines · Andreas Färber

Hi Lin and Markus,

Am 19.09.2016 um 13:58 schrieb Markus Armbruster:

[...]

Quoted 44 lines
Quoted 44 lines

You're messing with struct EnumProperty because you want more help than what ObjectPropertyInfo can provice.

Digression on the meaning of ObjectPropertyInfo. This is its definition:

##
# @ObjectPropertyInfo:
#
# @name: the name of the property
#
# @type: the type of the property. This will typically come in one of four
# forms:
#
# 1) A primitive type such as 'u8', 'u16', 'bool', 'str', or 'double'.
# These types are mapped to the appropriate JSON type.
#
# 2) A child type in the form 'child<subtype>' where subtype is a qdev
# device type name. Child properties create the composition tree.
#
# 3) A link type in the form 'link<subtype>' where subtype is a qdev
# device type name. Link properties form the device model graph.
#
# Since: 1.2
##
{ 'struct': 'ObjectPropertyInfo',
'data': { 'name': 'str', 'type': 'str' } }

@type is documented to be either a primitive type, a child type or a link. "Primitive type" isn't defined. The examples given suggest it's a QAPI built-in type. If that's correct, clause 1) does not cover enumeration types. However, it doesn't seem to be correct: I observ 'string', not 'str'. Paolo, Andreas, any idea what @type is supposed to mean?

End of digression.

All ObjectPropertyInfo gives you is two strings: a name and a type. If you need more information than that, you have to add a proper interface to get it! Say a function that takes an object and a property name, and returns information about the property's type. Probably should be two functions, one that maps QOM object and property name to the property's QOM type, one that maps a QOM type to QOM type information.

Quoted 4 lines
Quoted 2 lines

In other words, you need QOM object and type introspection. Paolo, Andreas, if that already exists in some form, please point us to it.

Could you say what exactly you want to introspect here?

Context: Lin wants to implement "-object TYPE-NAME,help", similar to "-device DRIVER-NAME,help", i.e. list the available properties of TYPE-NAME.

His proposed patch tries to give better help for enumeration types by listing the acceptable values. The code that does it is an unacceptable hack, though. We're trying to figure out a way to provide such help cleanly.

One way to do it would be introspecting QOM *types*. Find the property's type, figure out what kind of type it is, if it's an enumeration type, find its members, ...

Quoted 5 lines

Both ObjectClass and Object have a list of properties that together form the list of properties that can be set on an instance. So you'll need to instantiate the object like I think we do for devices. Then you can recursively enumerate the properties to get their names and types (and possibly put them into a new list for alphabetical sorting if desired).

Quoted 8 lines

Lin's code uses object_new() to instantiate a dummy object, object_property_iter_init() and object_property_iter_next() to find the properties. Sounds okay so far, doesn't it?

Hmm, ObjectProperty has members name, type, description. Could description be useful? I guess it's set with object_property_set_description(). I can see only a few dozen calls, which makes me suspect it's null more often than not.

Saving acceptable values of enumeration types into member description of ObjectProperty is a good idea.

The member description of ObjectProperty instance of any user-creatable object is NULL so far, If I use object_property_set_description() to fill the acceptable values of enumeration type into the description in function object_property_add_enum and object_cl
ass_property_add_enum, Then I
can use this description to judge whether a ObjectProperty instance' type is enumeration or not in function user_creatable_help_func. In this case, If member description is not NULL, it means this ObjectProperty instance is an enumeration.
Any suggestions?

If this way makes sense, Then Should I add a member description for ObjectPropertyInfo as well?

Quoted 6 lines

When it's null we could still fall back to a description of the type. Does such a thing exist? Enumeration types could provide one listing their values.

Other ideas?

#7 lore raw
Quoted 84 lines · Lin Ma
Quoted 75 lines
Quoted 1 line
Quoted 1 line

Markus Armbruster <[email protected]> 2016/9/20 星期二 上午 1:13 >>>

Quoted 3 lines · Andreas Färber

Hi Lin and Markus,

Am 19.09.2016 um 13:58 schrieb Markus Armbruster:

[...]

Quoted 44 lines
Quoted 44 lines

You're messing with struct EnumProperty because you want more help than what ObjectPropertyInfo can provice.

Digression on the meaning of ObjectPropertyInfo. This is its definition:

##
# @ObjectPropertyInfo:
#
# @name: the name of the property
#
# @type: the type of the property. This will typically come in one of four
# forms:
#
# 1) A primitive type such as 'u8', 'u16', 'bool', 'str', or 'double'.
# These types are mapped to the appropriate JSON type.
#
# 2) A child type in the form 'child<subtype>' where subtype is a qdev
# device type name. Child properties create the composition tree.
#
# 3) A link type in the form 'link<subtype>' where subtype is a qdev
# device type name. Link properties form the device model graph.
#
# Since: 1.2
##
{ 'struct': 'ObjectPropertyInfo',
'data': { 'name': 'str', 'type': 'str' } }

@type is documented to be either a primitive type, a child type or a link. "Primitive type" isn't defined. The examples given suggest it's a QAPI built-in type. If that's correct, clause 1) does not cover enumeration types. However, it doesn't seem to be correct: I observ 'string', not 'str'. Paolo, Andreas, any idea what @type is supposed to mean?

End of digression.

All ObjectPropertyInfo gives you is two strings: a name and a type. If you need more information than that, you have to add a proper interface to get it! Say a function that takes an object and a property name, and returns information about the property's type. Probably should be two functions, one that maps QOM object and property name to the property's QOM type, one that maps a QOM type to QOM type information.

Quoted 4 lines
Quoted 2 lines

In other words, you need QOM object and type introspection. Paolo, Andreas, if that already exists in some form, please point us to it.

Could you say what exactly you want to introspect here?

Context: Lin wants to implement "-object TYPE-NAME,help", similar to "-device DRIVER-NAME,help", i.e. list the available properties of TYPE-NAME.

His proposed patch tries to give better help for enumeration types by listing the acceptable values. The code that does it is an unacceptable hack, though. We're trying to figure out a way to provide such help cleanly.

One way to do it would be introspecting QOM *types*. Find the property's type, figure out what kind of type it is, if it's an enumeration type, find its members, ...

Quoted 5 lines

Both ObjectClass and Object have a list of properties that together form the list of properties that can be set on an instance. So you'll need to instantiate the object like I think we do for devices. Then you can recursively enumerate the properties to get their names and types (and possibly put them into a new list for alphabetical sorting if desired).

Quoted 8 lines

Lin's code uses object_new() to instantiate a dummy object, object_property_iter_init() and object_property_iter_next() to find the properties. Sounds okay so far, doesn't it?

Hmm, ObjectProperty has members name, type, description. Could description be useful? I guess it's set with object_property_set_description(). I can see only a few dozen calls, which makes me suspect it's null more often than not.

Quoted 5 lines

Saving acceptable values of enumeration types into member description of ObjectProperty is a good idea.

The member description of ObjectProperty instance of any user-creatable object is NULL so far,

It's null until set with object_property_set_description(). We do that for a few properties, and may do it more.

Quoted 7 lines

If I use object_property_set_description() to fill the
acceptable values of enumeration type into the description in function object_property_add_enum and object_class_property_add_enum, Then I can use this description to judge whether a ObjectProperty instance' type is enumeration or not in function user_creatable_help_func. In this case, If member description is not NULL, it means this ObjectProperty instance is an enumeration.

No. If you need to decide in user_creatable_help_func() whether a property has an enumeration type, something's wrong at the infrastructure level.

Quoted 1 line

Any suggestions?

Yes:

Quoted 3 lines
Quoted 3 lines

When it's null we could still fall back to a description of the type. Does such a thing exist? Enumeration types could provide one listing their values.

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

What you could perhaps do is adding a *type* description. For enums, that would show the set of acceptable values. Then if the property has no description, fall back to the description of its type.

Unfortunately, I can't see a "property type" abstraction in the code. Perhaps Andreas or Paolo can provide some guidance.

Quoted 2 lines

If this way makes sense, Then Should I add a member description for ObjectPropertyInfo as well?

I suspect you won't even need ObjectPropertyInfo here once you've fused your loops.

ObjectPropertyInfo is a QAPI/QMP thing. Adding a description to it needs to make sense for QMP. I haven't thought about it.

Quoted 1 line
Quoted 1 line

Other ideas?

#8 lore raw
Quoted 13 lines · Markus Armbruster

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Quoted 7 lines

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

FWIW, we could easily optimize handling of description strings by applying the same trick that GLib has done for GObject property descriptions.

Almost certainly every call to object_property_set_description is going to be passing a string literal, not a dynamically allocated string. So we take advantage of that and in fact mandate that it is a string literal, and thus avoid the strdup() of description.

We can place a fun game to enforce this at compile time thus:

- Rename object_property_set_description() to
object_property_set_description_internal()

- Add the macro

  #define  object_property_set_description(obj, name, desc, errp) \
     object_property_set_description_internal(obj, name, "" desc "", errp)

None the less, we really should make an effort to switch things over to use class properties instead of instance properties, as its going to save us allocating a 64 byte struct per property per instance

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#9 lore raw
Quoted 33 lines · Daniel P. Berrange
Quoted 13 lines · Markus Armbruster

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Quoted 7 lines

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

FWIW, we could easily optimize handling of description strings by applying the same trick that GLib has done for GObject property descriptions.

Almost certainly every call to object_property_set_description is going to be passing a string literal, not a dynamically allocated string. So we take advantage of that and in fact mandate that it is a string literal, and thus avoid the strdup() of description.

We can place a fun game to enforce this at compile time thus:

Quoted 7 lines

- Rename object_property_set_description() to
object_property_set_description_internal()

- Add the macro

  #define  object_property_set_description(obj, name, desc, errp) \
     object_property_set_description_internal(obj, name, "" desc "", errp)

Cute :)

Quoted 4 lines

None the less, we really should make an effort to switch things over to use class properties instead of instance properties, as its going to save us allocating a 64 byte struct per property per instance

Yes, please.

Related: a way to define a bunch of properties as *data*, i.e. an array of property descriptions, commonly static. Reasoning about static data is so much easier than reasoning about code.

#10 lore raw
Quoted 50 lines · Markus Armbruster
Quoted 33 lines · Daniel P. Berrange
Quoted 13 lines · Markus Armbruster

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Quoted 7 lines

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

FWIW, we could easily optimize handling of description strings by applying the same trick that GLib has done for GObject property descriptions.

Almost certainly every call to object_property_set_description is going to be passing a string literal, not a dynamically allocated string. So we take advantage of that and in fact mandate that it is a string literal, and thus avoid the strdup() of description.

We can place a fun game to enforce this at compile time thus:

Quoted 7 lines

- Rename object_property_set_description() to
object_property_set_description_internal()

- Add the macro

  #define  object_property_set_description(obj, name, desc, errp) \
     object_property_set_description_internal(obj, name, "" desc "", errp)

Cute :)

Quoted 4 lines

None the less, we really should make an effort to switch things over to use class properties instead of instance properties, as its going to save us allocating a 64 byte struct per property per instance

Quoted 5 lines

Yes, please.

Related: a way to define a bunch of properties as *data*, i.e. an array of property descriptions, commonly static. Reasoning about static data is so much easier than reasoning about code.

IMHO we should go further and leverage QAPI schema to auto-generate all the tedious boilerplate code for QOM objects

eg, consider the crypto/secret.c object file.

We could declare it as

{ 'object': 'QCryptoSecret',
'parent': 'Object',

  'properties': {
     'format': 'QCryptoSecretFormat',
     'data': 'str',
     'file': 'str',
     'keyid': 'str',
     'iv': 'str'
     } }

Based on that it would have enough knowledge to generate

- struct QCryptoSecret definition + typedef
- struct QCryptoSecretClass definition + typedef
- TYPE_CRYPTO_SECRET macro
- QCRYPT_SECRET() cast macro
- Setters & getters aka
qcrypto_secret_prop_set_format
qcrypto_secret_prop_get_format
qcrypto_secret_prop_set_data
qcrypto_secret_prop_get_data
qcrypto_secret_prop_set_file
qcrypto_secret_prop_get_file
qcrypto_secret_prop_set_keyid
qcrypto_secret_prop_get_keyid
qcrypto_secret_prop_set_iv
qcrypto_secret_prop_get_iv
- qcrypto_secret_finalize
- qcrypto_secret_class_init
- TypeInfo qcrypto_secret_info variable
- qcrypto_secret_register_types() method
- type_init(qcrypto_secret_register_types);

That'd massively reduce the work to create new objects, to just filling in the semantically useful logic

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#11 lore raw
Quoted 97 lines · Daniel P. Berrange
Quoted 50 lines · Markus Armbruster
Quoted 33 lines · Daniel P. Berrange
Quoted 13 lines · Markus Armbruster

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Quoted 7 lines

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

FWIW, we could easily optimize handling of description strings by applying the same trick that GLib has done for GObject property descriptions.

Almost certainly every call to object_property_set_description is going to be passing a string literal, not a dynamically allocated string. So we take advantage of that and in fact mandate that it is a string literal, and thus avoid the strdup() of description.

We can place a fun game to enforce this at compile time thus:

Quoted 7 lines

- Rename object_property_set_description() to
object_property_set_description_internal()

- Add the macro

  #define  object_property_set_description(obj, name, desc, errp) \
     object_property_set_description_internal(obj, name, "" desc "", errp)

Cute :)

Quoted 4 lines

None the less, we really should make an effort to switch things over to use class properties instead of instance properties, as its going to save us allocating a 64 byte struct per property per instance

Quoted 5 lines

Yes, please.

Related: a way to define a bunch of properties as *data*, i.e. an array of property descriptions, commonly static. Reasoning about static data is so much easier than reasoning about code.

IMHO we should go further and leverage QAPI schema to auto-generate all the tedious boilerplate code for QOM objects

eg, consider the crypto/secret.c object file.

We could declare it as

{ 'object': 'QCryptoSecret',
'parent': 'Object',

  'properties': {
     'format': 'QCryptoSecretFormat',
     'data': 'str',
     'file': 'str',
     'keyid': 'str',
     'iv': 'str'
     } }

Based on that it would have enough knowledge to generate

- struct QCryptoSecret definition + typedef
- struct QCryptoSecretClass definition + typedef
- TYPE_CRYPTO_SECRET macro
- QCRYPT_SECRET() cast macro
- Setters & getters aka
qcrypto_secret_prop_set_format
qcrypto_secret_prop_get_format
qcrypto_secret_prop_set_data
qcrypto_secret_prop_get_data
qcrypto_secret_prop_set_file
qcrypto_secret_prop_get_file
qcrypto_secret_prop_set_keyid
qcrypto_secret_prop_get_keyid
qcrypto_secret_prop_set_iv
qcrypto_secret_prop_get_iv
- qcrypto_secret_finalize
- qcrypto_secret_class_init
- TypeInfo qcrypto_secret_info variable
- qcrypto_secret_register_types() method
- type_init(qcrypto_secret_register_types);

Quoted 2 lines

That'd massively reduce the work to create new objects, to just filling in the semantically useful logic

Promising idea. Sadly, the existing QAPI queue is eating all my QAPI cycles.

#13 lore raw
In reply to Markus Armbruster
Quoted 54 lines · Markus Armbruster
Quoted 7 lines · Lin Ma
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Markus Armbruster <[email protected]> 2016/9/20 星期二 上午 1:13 >>>

Andreas Färber <[email protected]> writes:

Saving acceptable values of enumeration types into member description of ObjectProperty is a good idea.

The member description of ObjectProperty instance of any user-creatable object is NULL so far,

It's null until set with object_property_set_description(). We do that for a few properties, and may do it more.

Quoted 7 lines

If I use object_property_set_description() to fill the
acceptable values of enumeration type into the description in function object_property_add_enum and object_class_property_add_enum, Then I can use this description to judge whether a ObjectProperty instance' type is enumeration or not in function user_creatable_help_func. In this case, If member description is not NULL, it means this ObjectProperty instance is an enumeration.

No. If you need to decide in user_creatable_help_func() whether a property has an enumeration type, something's wrong at the infrastructure level.

Quoted 1 line

Any suggestions?

Yes:

Quoted 3 lines
Quoted 3 lines

When it's null we could still fall back to a description of the type. Does such a thing exist? Enumeration types could provide one listing their values.

Don't make up a description in user_creatable_help_func(), improve the description infrastructure and its use so you get more useful ones there.

The existing description infrastructure is just Property member description and object_property_set_description(). Rarely used, so description is generally null.

Calling object_property_set_description() more often could be helpful, but to come up with a sensible description string, you need to know what the property does. Needs to be left to people actually familiar with the objects.

Aside: historically, we add properties to *instances*. All the property meta-data gets duplicated for every instance, including property descriptions. This is more flexible than adding the meta-data to the class. The flexibility is rarely needed, but the price in wasted memory is always paid. Only since commit 16bf7f5, we can add it to classes. Adding lots of helpful property descriptions would increase the cost of instance properties further.

Quoted 3 lines

What you could perhaps do is adding a *type* description. For enums, that would show the set of acceptable values. Then if the property has no description, fall back to the description of its type.

I don't think we need to invent anything new. We can use the existing property description facility and auto-generate the string containing the permitted values thus:

scripts/qapi-types.py +2 -0
index dabc42e..0446839 100644
@@ -202,9 +202,11 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
             self._btin += gen_enum(name, values, prefix)
             if do_builtins:
                 self.defn += gen_enum_lookup(name, values, prefix)
+                self._btin += gen_enum_value_str(name, values)
         else:
             self._fwdecl += gen_enum(name, values, prefix)
             self.defn += gen_enum_lookup(name, values, prefix)
+            self._fwdecl += gen_enum_value_str(name, values)
 
     def visit_array_type(self, name, info, element_type):
         if isinstance(element_type, QAPISchemaBuiltinType):
scripts/qapi.py +9 -0
index 21bc32f..d11c414 100644
@@ -1649,6 +1649,15 @@ const char *const %(c_name)s_lookup[] = {
     return ret
 
 
+def gen_enum_value_str(name, values):
+    return mcgen('''
+
+#define %(c_name)s_value_str "%(value_str)s"
+''',
+                c_name=c_name(name),
+                value_str=", ".join(["'%s'" % c for c in values]))
+
+
 def gen_enum(name, values, prefix=None):
     # append automatically generated _MAX value
     enum_values = values + ['_MAX']

Now, when registering an enum property do something like this:

    object_class_property_add_enum(oc, "format",
                                   "QCryptoSecretFormat",
                                   QCryptoSecretFormat_lookup,
                                   qcrypto_secret_prop_get_format,
                                   qcrypto_secret_prop_set_format,
                                   NULL);
    object_class_property_set_description(oc, "format",
                                          "Data format, one of "
                                          QCryptoSecretFormat_value_str,
                                          &error_abort);

So that description ends up being

    "Data format, one of 'base64', 'plain'"

It would be nicer if the object_property_add_* methods just accepted a 'const char *description' too. As well as being more efficient that a second method call which has to search for the ObjectProperty struct, potentially reporting errors, it will also encourage people to actually provide descriptions

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#12 lore raw
In reply to Markus Armbruster
Quoted 42 lines · Markus Armbruster
Quoted 41 lines · Daniel P. Berrange

IMHO we should go further and leverage QAPI schema to auto-generate all the tedious boilerplate code for QOM objects

eg, consider the crypto/secret.c object file.

We could declare it as

{ 'object': 'QCryptoSecret',
'parent': 'Object',

  'properties': {
     'format': 'QCryptoSecretFormat',
     'data': 'str',
     'file': 'str',
     'keyid': 'str',
     'iv': 'str'
     } }

Based on that it would have enough knowledge to generate

- struct QCryptoSecret definition + typedef
- struct QCryptoSecretClass definition + typedef
- TYPE_CRYPTO_SECRET macro
- QCRYPT_SECRET() cast macro
- Setters & getters aka
qcrypto_secret_prop_set_format
qcrypto_secret_prop_get_format
qcrypto_secret_prop_set_data
qcrypto_secret_prop_get_data
qcrypto_secret_prop_set_file
qcrypto_secret_prop_get_file
qcrypto_secret_prop_set_keyid
qcrypto_secret_prop_get_keyid
qcrypto_secret_prop_set_iv
qcrypto_secret_prop_get_iv
- qcrypto_secret_finalize
- qcrypto_secret_class_init
- TypeInfo qcrypto_secret_info variable
- qcrypto_secret_register_types() method
- type_init(qcrypto_secret_register_types);

Quoted 5 lines
Quoted 2 lines

That'd massively reduce the work to create new objects, to just filling in the semantically useful logic

Promising idea. Sadly, the existing QAPI queue is eating all my QAPI cycles.

Indeed, I've no time either. Perhaps this is something nice for a "lucky" GSoC / OutReachy student :-) Copying Stefan....

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|