Received: from eggs.gnu.org ([208.118.235.92]:35866) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SciQe-0000xu-G5 for qemu-devel@nongnu.org; Thu, 07 Jun 2012 15:31:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SciQZ-0004xB-RK for qemu-devel@nongnu.org; Thu, 07 Jun 2012 15:31:32 -0400 Received: from cantor2.suse.de ([195.135.220.15]:53808 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SciQZ-0004wq-IM for qemu-devel@nongnu.org; Thu, 07 Jun 2012 15:31:27 -0400 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Thu, 7 Jun 2012 21:31:00 +0200 Message-Id: <1339097465-22977-3-git-send-email-afaerber@suse.de> In-Reply-To: <1339097465-22977-1-git-send-email-afaerber@suse.de> References: <1339097465-22977-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH qom-next 2/7] qom: Add get_id List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, =?UTF-8?q?Andreas=20F=C3=A4rber?= , anthony@codemonkey.ws From: Paolo Bonzini Some classes may present objects differently in errors, for example if th= ey are not part of the composition tree or if they are not assigned an id by the user. Let them do this with a get_id method on Object, and use the method consistently where a %(device) appears in the error. Signed-off-by: Paolo Bonzini [AF: Renamed _object_get_id() to object_instance_get_id(), avoid ?:.] [AF: Use object_property_is_child().] Signed-off-by: Andreas F=C3=A4rber --- hw/qdev-properties.c | 6 +++--- hw/qdev.c | 15 ++++++++++++++- include/qemu/object.h | 11 +++++++++++ qom/object.c | 26 +++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index fcc0bed..4dc03f6 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -937,16 +937,16 @@ void error_set_from_qdev_prop_error(Error **errp, i= nt ret, DeviceState *dev, switch (ret) { case -EEXIST: error_set(errp, QERR_PROPERTY_VALUE_IN_USE, - object_get_typename(OBJECT(dev)), prop->name, value); + object_get_id(OBJECT(dev)), prop->name, value); break; default: case -EINVAL: error_set(errp, QERR_PROPERTY_VALUE_BAD, - object_get_typename(OBJECT(dev)), prop->name, value); + object_get_id(OBJECT(dev)), prop->name, value); break; case -ENOENT: error_set(errp, QERR_PROPERTY_VALUE_NOT_FOUND, - object_get_typename(OBJECT(dev)), prop->name, value); + object_get_id(OBJECT(dev)), prop->name, value); break; case 0: break; diff --git a/hw/qdev.c b/hw/qdev.c index c12e151..7304e4c 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -259,7 +259,7 @@ void qdev_init_nofail(DeviceState *dev) { if (qdev_init(dev) < 0) { error_report("Initialization of device %s failed", - object_get_typename(OBJECT(dev))); + object_get_id(OBJECT(dev))); exit(1); } } @@ -716,6 +716,13 @@ static void device_finalize(Object *obj) } } =20 +static const char *qdev_get_id(Object *obj) +{ + DeviceState *dev =3D DEVICE(obj); + + return dev->id !=3D NULL ? dev->id : object_get_typename(obj); +} + static void device_class_base_init(ObjectClass *class, void *data) { DeviceClass *klass =3D DEVICE_CLASS(class); @@ -746,6 +753,11 @@ Object *qdev_get_machine(void) return dev; } =20 +static void device_class_init(ObjectClass *class, void *data) +{ + class->get_id =3D qdev_get_id; +} + static TypeInfo device_type_info =3D { .name =3D TYPE_DEVICE, .parent =3D TYPE_OBJECT, @@ -753,6 +765,7 @@ static TypeInfo device_type_info =3D { .instance_init =3D device_initfn, .instance_finalize =3D device_finalize, .class_base_init =3D device_class_base_init, + .class_init =3D device_class_init, .abstract =3D true, .class_size =3D sizeof(DeviceClass), }; diff --git a/include/qemu/object.h b/include/qemu/object.h index 1606777..81e0280 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -239,6 +239,9 @@ struct ObjectClass { /*< private >*/ Type type; + + /*< public >*/ + const char *(*get_id)(Object *); }; =20 typedef enum ObjectState { @@ -507,6 +510,14 @@ Object *object_dynamic_cast(Object *obj, const char = *typename); Object *object_dynamic_cast_assert(Object *obj, const char *typename); =20 /** + * object_get_id: + * @obj: A derivative of #Object + * + * Returns: A string that can be used to refer to @obj. + */ +const char *object_get_id(Object *obj); + +/** * object_get_class: * @obj: A derivative of #Object * diff --git a/qom/object.c b/qom/object.c index 93e0499..02464e1 100644 --- a/qom/object.c +++ b/qom/object.c @@ -346,6 +346,24 @@ static void object_property_del_child(Object *obj, O= bject *child, Error **errp) } } =20 +static const char *object_instance_get_id(Object *obj) +{ + ObjectProperty *prop; + + QTAILQ_FOREACH(prop, &obj->properties, node) { + if (object_property_is_child(prop) && prop->opaque =3D=3D obj) { + return prop->name; + } + } + + return ""; +} + +const char *object_get_id(Object *obj) +{ + return obj->class->get_id(obj); +} + void object_unparent(Object *obj) { if (obj->parent) { @@ -685,7 +703,7 @@ ObjectProperty *object_property_find(Object *obj, con= st char *name, } } =20 - error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); + error_set(errp, QERR_PROPERTY_NOT_FOUND, object_get_id(obj), name); return NULL; } =20 @@ -1249,6 +1267,11 @@ static void object_instance_init(Object *obj) object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); } =20 +static void object_class_init(ObjectClass *klass, void *class_data) +{ + klass->get_id =3D object_instance_get_id; +} + static void register_types(void) { static TypeInfo interface_info =3D { @@ -1261,6 +1284,7 @@ static void register_types(void) .name =3D TYPE_OBJECT, .instance_size =3D sizeof(Object), .instance_init =3D object_instance_init, + .class_init =3D object_class_init, .abstract =3D true, }; =20 --=20 1.7.7