Received: from eggs.gnu.org ([2001:4830:134:3::10]:53838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bm0vU-0007tB-BW for qemu-devel@nongnu.org; Mon, 19 Sep 2016 11:56:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bm0vO-0006j6-84 for qemu-devel@nongnu.org; Mon, 19 Sep 2016 11:56:11 -0400 Received: from mx2.suse.de ([195.135.220.15]:47453) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bm0vN-0006hN-Lb for qemu-devel@nongnu.org; Mon, 19 Sep 2016 11:56:06 -0400 References: <20160911055301.26650-1-lma@suse.com> <20160911055301.26650-1-lma@suse.com> <87zindrup8.fsf@dusky.pond.sub.org> <57DDA45C02000062000996DE@prv-mh.provo.novell.com> <87fuowje3u.fsf@dusky.pond.sub.org> From: =?UTF-8?Q?Andreas_F=c3=a4rber?= Message-ID: <9ae54df6-e098-8ca9-53c8-1a65eba32dae@suse.de> Date: Mon, 19 Sep 2016 17:56:01 +0200 MIME-Version: 1.0 In-Reply-To: <87fuowje3u.fsf@dusky.pond.sub.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] =?utf-8?b?562U5aSN77yaIFJlOiBbUEFUQ0ggdjJdIG9iamVj?= =?utf-8?q?t=3A_Add_=27help=27_option_for_all_available_backends_and_prope?= =?utf-8?q?rties?= List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster , Lin Ma Cc: pbonzini@redhat.com, qemu-devel@nongnu.org Hi Lin and Markus, Am 19.09.2016 um 13:58 schrieb Markus Armbruster: > This is about QOM use. Cc: Andreas in case he has smart ideas. > Andreas, you may want to skip ahead to "EnumProperty". >=20 > "Lin Ma" writes: >=20 >>>>> Markus Armbruster 2016/9/12 =E6=98=9F=E6=9C=9F=E4= =B8=80 =E4=B8=8B=E5=8D=88 11:42 >>> >>> Lin Ma writes: >>> >>>> '-object help' prints available user creatable backends. >>>> '-object $typename,help' prints relevant properties. >>>> >>>> Signed-off-by: Lin Ma >>>> --- >>>> 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(-) >>>> > [...] >>>> diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c >>>> index bf59846..4ee8643 100644 >>>> --- a/qom/object_interfaces.c >>>> +++ b/qom/object_interfaces.c >>>> @@ -5,6 +5,7 @@ >>>> #include "qapi-visit.h" >>>> #include "qapi/qmp-output-visitor.h" >>>> #include "qapi/opts-visitor.h" >>>> +#include "qemu/help_option.h" >>>> =20 >>>> void user_creatable_complete(Object *obj, Error **errp) >>>> { >>>> @@ -212,6 +213,117 @@ void user_creatable_del(const char *id, Error = **errp) >>>> object_unparent(obj); >>>> } >>>> =20 >>>> +int user_creatable_help_func(void *opaque, QemuOpts *opts, Error **= errp) >>>> +{ >>>> + Visitor *v; >>>> + char *type =3D NULL; >>>> + Error *local_err =3D NULL; >>>> + >>> >>> Why this blank line? >>> >> I'll remove it. >> >>>> + int i; >>>> + char *values =3D NULL; >>>> + Object *obj; >>>> + ObjectPropertyInfoList *props =3D 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. >>> >> 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 s= tr 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? >=20 > See below. >=20 >>>> + >>>> + =20 >> v =3D 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 =3D 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. >> >>>> + printf("Available object backend types:\n"); >>>> + GSList *list =3D object_class_get_list(TYPE_USER_CREATABLE, fa= lse); >>>> + while (list) { >>>> + const char *name; >>>> + name =3D 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=3Dfoo >>> ** >>> ERROR:/work/armbru/qemu/qom/object.c:361:object_initialize_with_ty= pe: assertion failed (type->instance_size >=3D sizeof(Object)): (0 >=3D 4= 0) >>> Aborted (core dumped) >>> >>> Should this type be abstract? >>> >> Yes, The object type user-creatable is abstract, But obviously it miss= ed the abstract property. >> I'll add it in next patch(patch 1/2), Then I dont need to skip it at h= ere anymore. >=20 > Yes, please. >=20 >>>> + printf("%s\n", name); >>>> + } >>>> + list =3D list->next; >>>> + } >>> >>> Recommend to keep the loop control in one place: >>> >>> for (list =3D object_class_get_list(TYPE_USER_CREATABLE, false); >>> list; >>> list =3D list->next) { >>> ... >>> } >>> >>> If you hate multi-line for (...), you can do >>> >>> GSList *head =3D object_class_get_list(TYPE_USER_CREATABLE, fals= e); >>> >>> for (list =3D head; list; list->next) { >>> ... >>> } >>> >> Will do it. >> >>>> + 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 =3D object_new(type); >>>> + object_property_iter_init(&iter, obj); >>>> + >>>> + while ((prop =3D object_property_iter_next(&iter))) { >>>> + ObjectPropertyInfoList *entry =3D g_malloc0(sizeof(*entry)); >>> >>> Blank line between declarations and statements, please. >>> >> ok. >> >>>> + entry->value =3D g_malloc0(sizeof(ObjectPropertyInfo)); >>> >>> Either g_malloc0(sizeof(entry->value), or g_new0(ObjectPropertyInfo). >>> >> will use g_malloc0(sizeof(entry->value). >> >>>> + entry->next =3D props; >>>> + props =3D entry; >>>> + entry->value->name =3D g_strdup(prop->name); >>>> + i =3D 0; >>>> + enumprop =3D 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 obviou= s >>> to me... >>> >>>> + while (enumprop->strings[i] !=3D NULL) { >>>> + if (i !=3D 0) { >>>> + values =3D g_strdup_printf("%s/%s", >>>> + values, enumprop->strings[i]); >>>> + } else { >>>> + values =3D g_strdup_printf("%s", enumprop->strings[i]); >>>> + } >>>> + i++; >>>> + } >>>> + entry->value->type =3D g_strdup_printf("%s, available values:= %s", >>>> + prop->type, values); >>> >>> Looks like this is the enum case. But why is the condition true exac= tly >>> 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? >=20 > You're messing with struct EnumProperty because you want more help than > what ObjectPropertyInfo can provice. >=20 > Digression on the meaning of ObjectPropertyInfo. This is its > definition: >=20 > ## > # @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 'do= uble'. > # These types are mapped to the appropriate JSON type. > # > # 2) A child type in the form 'child' where subtype is = a qdev > # device type name. Child properties create the composition = tree. > # > # 3) A link type in the form 'link' where subtype is a = qdev > # device type name. Link properties form the device model gr= aph. > # > # Since: 1.2 > ## > { 'struct': 'ObjectPropertyInfo', > 'data': { 'name': 'str', 'type': 'str' } } >=20 > @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 t= o > mean? >=20 > End of digression. >=20 > 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, an= d > 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. >=20 > 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 --=20 SUSE Linux GmbH, Maxfeldstr. 5, 90409 N=C3=BCrnberg, Germany GF: Felix Imend=C3=B6rffer, Jane Smithard, Graham Norton HRB 21284 (AG N=C3=BCrnberg)