This would allow a user to be able to refer to the device when using commands like device_del.
Signed-off-by: Hani Benhabiles <kroo...@gmail.com> --- qdev-monitor.c | 64 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/qdev-monitor.c b/qdev-monitor.c index dc37a43..64b2b22 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -451,11 +451,54 @@ static BusState *qbus_find(const char *path) } } +static int qbus_device_count(const BusState *bus, const char *typename) +{ + BusChild *kid; + int count = 0; + + QTAILQ_FOREACH(kid, &bus->children, sibling) { + DeviceState *dev = kid->child; + BusState *dev_child; + + if (!strcmp(typename, object_get_typename(OBJECT(dev)))) { + count++; + } + + QLIST_FOREACH(dev_child, &dev->child_bus, sibling) { + count += qbus_device_count(dev_child, typename); + } + } + + return count; +} + +static gchar *assign_device_name(const char *typename, const DeviceState *dev) +{ + BusState *bus; + gchar *new_id; + int count = 0; + + bus = sysbus_get_default(); + count += qbus_device_count(bus, typename); + + while (1) { + new_id = g_strdup_printf("%s.%d", typename, count - 1); + /* To not use an existing ID, if the user previously specified it. */ + if (!qdev_find_recursive(sysbus_get_default(), new_id)) { + break; + } + count++; + g_free(new_id); + } + + return new_id; +} + DeviceState *qdev_device_add(QemuOpts *opts) { ObjectClass *oc; DeviceClass *dc; - const char *driver, *path, *id; + const char *driver, *path; DeviceState *dev; BusState *bus = NULL; Error *err = NULL; @@ -522,25 +565,18 @@ DeviceState *qdev_device_add(QemuOpts *opts) qdev_set_parent_bus(dev, bus); } - id = qemu_opts_id(opts); - if (id) { - dev->id = id; + dev->id = qemu_opts_id(opts); + if (!dev->id) { + qemu_opts_set_id(opts, assign_device_name(driver, dev)); + dev->id = qemu_opts_id(opts); } if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { object_unparent(OBJECT(dev)); object_unref(OBJECT(dev)); return NULL; } - if (dev->id) { - object_property_add_child(qdev_get_peripheral(), dev->id, - OBJECT(dev), NULL); - } else { - static int anon_count; - gchar *name = g_strdup_printf("device[%d]", anon_count++); - object_property_add_child(qdev_get_peripheral_anon(), name, - OBJECT(dev), NULL); - g_free(name); - } + object_property_add_child(qdev_get_peripheral(), dev->id, OBJECT(dev), + NULL); object_property_set_bool(OBJECT(dev), true, "realized", &err); if (err != NULL) { qerror_report_err(err); -- 1.8.3.2