qemu/qdev-monitor.c
<<
>>
Prefs
   1/*
   2 *  Dynamic device configuration and creation.
   3 *
   4 *  Copyright (c) 2009 CodeSourcery
   5 *
   6 * This library is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU Lesser General Public
   8 * License as published by the Free Software Foundation; either
   9 * version 2 of the License, or (at your option) any later version.
  10 *
  11 * This library is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * Lesser General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU Lesser General Public
  17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19
  20#include "qemu/osdep.h"
  21#include "hw/qdev.h"
  22#include "hw/sysbus.h"
  23#include "monitor/monitor.h"
  24#include "monitor/qdev.h"
  25#include "qmp-commands.h"
  26#include "sysemu/arch_init.h"
  27#include "qapi/qmp/qerror.h"
  28#include "qemu/config-file.h"
  29#include "qemu/error-report.h"
  30#include "qemu/help_option.h"
  31
  32/*
  33 * Aliases were a bad idea from the start.  Let's keep them
  34 * from spreading further.
  35 */
  36typedef struct QDevAlias
  37{
  38    const char *typename;
  39    const char *alias;
  40    uint32_t arch_mask;
  41} QDevAlias;
  42
  43/* Please keep this table sorted by typename. */
  44static const QDevAlias qdev_alias_table[] = {
  45    { "e1000", "e1000-82540em" },
  46    { "ich9-ahci", "ahci" },
  47    { "kvm-pci-assign", "pci-assign" },
  48    { "lsi53c895a", "lsi" },
  49    { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
  50    { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  51    { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
  52    { "virtio-balloon-pci", "virtio-balloon",
  53            QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  54    { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
  55    { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  56    { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
  57    { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  58    { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
  59    { "virtio-input-host-pci", "virtio-input-host",
  60            QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  61    { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
  62    { "virtio-keyboard-pci", "virtio-keyboard",
  63            QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  64    { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
  65    { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  66    { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
  67    { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  68    { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
  69    { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  70    { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
  71    { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  72    { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
  73    { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  74    { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
  75    { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  76    { }
  77};
  78
  79static const char *qdev_class_get_alias(DeviceClass *dc)
  80{
  81    const char *typename = object_class_get_name(OBJECT_CLASS(dc));
  82    int i;
  83
  84    for (i = 0; qdev_alias_table[i].typename; i++) {
  85        if (qdev_alias_table[i].arch_mask &&
  86            !(qdev_alias_table[i].arch_mask & arch_type)) {
  87            continue;
  88        }
  89
  90        if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
  91            return qdev_alias_table[i].alias;
  92        }
  93    }
  94
  95    return NULL;
  96}
  97
  98static bool qdev_class_has_alias(DeviceClass *dc)
  99{
 100    return (qdev_class_get_alias(dc) != NULL);
 101}
 102
 103static void qdev_print_devinfo(DeviceClass *dc)
 104{
 105    error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
 106    if (dc->bus_type) {
 107        error_printf(", bus %s", dc->bus_type);
 108    }
 109    if (qdev_class_has_alias(dc)) {
 110        error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
 111    }
 112    if (dc->desc) {
 113        error_printf(", desc \"%s\"", dc->desc);
 114    }
 115    if (dc->cannot_instantiate_with_device_add_yet) {
 116        error_printf(", no-user");
 117    }
 118    error_printf("\n");
 119}
 120
 121static gint devinfo_cmp(gconstpointer a, gconstpointer b)
 122{
 123    return strcasecmp(object_class_get_name((ObjectClass *)a),
 124                      object_class_get_name((ObjectClass *)b));
 125}
 126
 127static void qdev_print_devinfos(bool show_no_user)
 128{
 129    static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
 130        [DEVICE_CATEGORY_BRIDGE]  = "Controller/Bridge/Hub",
 131        [DEVICE_CATEGORY_USB]     = "USB",
 132        [DEVICE_CATEGORY_STORAGE] = "Storage",
 133        [DEVICE_CATEGORY_NETWORK] = "Network",
 134        [DEVICE_CATEGORY_INPUT]   = "Input",
 135        [DEVICE_CATEGORY_DISPLAY] = "Display",
 136        [DEVICE_CATEGORY_SOUND]   = "Sound",
 137        [DEVICE_CATEGORY_MISC]    = "Misc",
 138        [DEVICE_CATEGORY_MAX]     = "Uncategorized",
 139    };
 140    GSList *list, *elt;
 141    int i;
 142    bool cat_printed;
 143
 144    list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
 145                        devinfo_cmp);
 146
 147    for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
 148        cat_printed = false;
 149        for (elt = list; elt; elt = elt->next) {
 150            DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
 151                                                 TYPE_DEVICE);
 152            if ((i < DEVICE_CATEGORY_MAX
 153                 ? !test_bit(i, dc->categories)
 154                 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
 155                || (!show_no_user
 156                    && dc->cannot_instantiate_with_device_add_yet)) {
 157                continue;
 158            }
 159            if (!cat_printed) {
 160                error_printf("%s%s devices:\n", i ? "\n" : "",
 161                             cat_name[i]);
 162                cat_printed = true;
 163            }
 164            qdev_print_devinfo(dc);
 165        }
 166    }
 167
 168    g_slist_free(list);
 169}
 170
 171static int set_property(void *opaque, const char *name, const char *value,
 172                        Error **errp)
 173{
 174    Object *obj = opaque;
 175    Error *err = NULL;
 176
 177    if (strcmp(name, "driver") == 0)
 178        return 0;
 179    if (strcmp(name, "bus") == 0)
 180        return 0;
 181
 182    object_property_parse(obj, value, name, &err);
 183    if (err != NULL) {
 184        error_propagate(errp, err);
 185        return -1;
 186    }
 187    return 0;
 188}
 189
 190static const char *find_typename_by_alias(const char *alias)
 191{
 192    int i;
 193
 194    for (i = 0; qdev_alias_table[i].alias; i++) {
 195        if (qdev_alias_table[i].arch_mask &&
 196            !(qdev_alias_table[i].arch_mask & arch_type)) {
 197            continue;
 198        }
 199
 200        if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
 201            return qdev_alias_table[i].typename;
 202        }
 203    }
 204
 205    return NULL;
 206}
 207
 208static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
 209{
 210    ObjectClass *oc;
 211    DeviceClass *dc;
 212    const char *original_name = *driver;
 213
 214    oc = object_class_by_name(*driver);
 215    if (!oc) {
 216        const char *typename = find_typename_by_alias(*driver);
 217
 218        if (typename) {
 219            *driver = typename;
 220            oc = object_class_by_name(*driver);
 221        }
 222    }
 223
 224    if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
 225        if (*driver != original_name) {
 226            error_setg(errp, "'%s' (alias '%s') is not a valid device model"
 227                       " name", original_name, *driver);
 228        } else {
 229            error_setg(errp, "'%s' is not a valid device model name", *driver);
 230        }
 231        return NULL;
 232    }
 233
 234    if (object_class_is_abstract(oc)) {
 235        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
 236                   "non-abstract device type");
 237        return NULL;
 238    }
 239
 240    dc = DEVICE_CLASS(oc);
 241    if (dc->cannot_instantiate_with_device_add_yet ||
 242        (qdev_hotplug && !dc->hotpluggable)) {
 243        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
 244                   "pluggable device type");
 245        return NULL;
 246    }
 247
 248    return dc;
 249}
 250
 251
 252int qdev_device_help(QemuOpts *opts)
 253{
 254    Error *local_err = NULL;
 255    const char *driver;
 256    DevicePropertyInfoList *prop_list;
 257    DevicePropertyInfoList *prop;
 258
 259    driver = qemu_opt_get(opts, "driver");
 260    if (driver && is_help_option(driver)) {
 261        qdev_print_devinfos(false);
 262        return 1;
 263    }
 264
 265    if (!driver || !qemu_opt_has_help_opt(opts)) {
 266        return 0;
 267    }
 268
 269    if (!object_class_by_name(driver)) {
 270        const char *typename = find_typename_by_alias(driver);
 271
 272        if (typename) {
 273            driver = typename;
 274        }
 275    }
 276
 277    prop_list = qmp_device_list_properties(driver, &local_err);
 278    if (local_err) {
 279        goto error;
 280    }
 281
 282    for (prop = prop_list; prop; prop = prop->next) {
 283        error_printf("%s.%s=%s", driver,
 284                     prop->value->name,
 285                     prop->value->type);
 286        if (prop->value->has_description) {
 287            error_printf(" (%s)\n", prop->value->description);
 288        } else {
 289            error_printf("\n");
 290        }
 291    }
 292
 293    qapi_free_DevicePropertyInfoList(prop_list);
 294    return 1;
 295
 296error:
 297    error_report_err(local_err);
 298    return 1;
 299}
 300
 301static Object *qdev_get_peripheral(void)
 302{
 303    static Object *dev;
 304
 305    if (dev == NULL) {
 306        dev = container_get(qdev_get_machine(), "/peripheral");
 307    }
 308
 309    return dev;
 310}
 311
 312static Object *qdev_get_peripheral_anon(void)
 313{
 314    static Object *dev;
 315
 316    if (dev == NULL) {
 317        dev = container_get(qdev_get_machine(), "/peripheral-anon");
 318    }
 319
 320    return dev;
 321}
 322
 323static void qbus_list_bus(DeviceState *dev, Error **errp)
 324{
 325    BusState *child;
 326    const char *sep = " ";
 327
 328    error_append_hint(errp, "child buses at \"%s\":",
 329                      dev->id ? dev->id : object_get_typename(OBJECT(dev)));
 330    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 331        error_append_hint(errp, "%s\"%s\"", sep, child->name);
 332        sep = ", ";
 333    }
 334    error_append_hint(errp, "\n");
 335}
 336
 337static void qbus_list_dev(BusState *bus, Error **errp)
 338{
 339    BusChild *kid;
 340    const char *sep = " ";
 341
 342    error_append_hint(errp, "devices at \"%s\":", bus->name);
 343    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 344        DeviceState *dev = kid->child;
 345        error_append_hint(errp, "%s\"%s\"", sep,
 346                          object_get_typename(OBJECT(dev)));
 347        if (dev->id) {
 348            error_append_hint(errp, "/\"%s\"", dev->id);
 349        }
 350        sep = ", ";
 351    }
 352    error_append_hint(errp, "\n");
 353}
 354
 355static BusState *qbus_find_bus(DeviceState *dev, char *elem)
 356{
 357    BusState *child;
 358
 359    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 360        if (strcmp(child->name, elem) == 0) {
 361            return child;
 362        }
 363    }
 364    return NULL;
 365}
 366
 367static DeviceState *qbus_find_dev(BusState *bus, char *elem)
 368{
 369    BusChild *kid;
 370
 371    /*
 372     * try to match in order:
 373     *   (1) instance id, if present
 374     *   (2) driver name
 375     *   (3) driver alias, if present
 376     */
 377    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 378        DeviceState *dev = kid->child;
 379        if (dev->id  &&  strcmp(dev->id, elem) == 0) {
 380            return dev;
 381        }
 382    }
 383    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 384        DeviceState *dev = kid->child;
 385        if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
 386            return dev;
 387        }
 388    }
 389    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 390        DeviceState *dev = kid->child;
 391        DeviceClass *dc = DEVICE_GET_CLASS(dev);
 392
 393        if (qdev_class_has_alias(dc) &&
 394            strcmp(qdev_class_get_alias(dc), elem) == 0) {
 395            return dev;
 396        }
 397    }
 398    return NULL;
 399}
 400
 401static inline bool qbus_is_full(BusState *bus)
 402{
 403    BusClass *bus_class = BUS_GET_CLASS(bus);
 404    return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
 405}
 406
 407/*
 408 * Search the tree rooted at @bus for a bus.
 409 * If @name, search for a bus with that name.  Note that bus names
 410 * need not be unique.  Yes, that's screwed up.
 411 * Else search for a bus that is a subtype of @bus_typename.
 412 * If more than one exists, prefer one that can take another device.
 413 * Return the bus if found, else %NULL.
 414 */
 415static BusState *qbus_find_recursive(BusState *bus, const char *name,
 416                                     const char *bus_typename)
 417{
 418    BusChild *kid;
 419    BusState *pick, *child, *ret;
 420    bool match;
 421
 422    assert(name || bus_typename);
 423    if (name) {
 424        match = !strcmp(bus->name, name);
 425    } else {
 426        match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
 427    }
 428
 429    if (match && !qbus_is_full(bus)) {
 430        return bus;             /* root matches and isn't full */
 431    }
 432
 433    pick = match ? bus : NULL;
 434
 435    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 436        DeviceState *dev = kid->child;
 437        QLIST_FOREACH(child, &dev->child_bus, sibling) {
 438            ret = qbus_find_recursive(child, name, bus_typename);
 439            if (ret && !qbus_is_full(ret)) {
 440                return ret;     /* a descendant matches and isn't full */
 441            }
 442            if (ret && !pick) {
 443                pick = ret;
 444            }
 445        }
 446    }
 447
 448    /* root or a descendant matches, but is full */
 449    return pick;
 450}
 451
 452static BusState *qbus_find(const char *path, Error **errp)
 453{
 454    DeviceState *dev;
 455    BusState *bus;
 456    char elem[128];
 457    int pos, len;
 458
 459    /* find start element */
 460    if (path[0] == '/') {
 461        bus = sysbus_get_default();
 462        pos = 0;
 463    } else {
 464        if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
 465            assert(!path[0]);
 466            elem[0] = len = 0;
 467        }
 468        bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
 469        if (!bus) {
 470            error_setg(errp, "Bus '%s' not found", elem);
 471            return NULL;
 472        }
 473        pos = len;
 474    }
 475
 476    for (;;) {
 477        assert(path[pos] == '/' || !path[pos]);
 478        while (path[pos] == '/') {
 479            pos++;
 480        }
 481        if (path[pos] == '\0') {
 482            break;
 483        }
 484
 485        /* find device */
 486        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
 487            g_assert_not_reached();
 488            elem[0] = len = 0;
 489        }
 490        pos += len;
 491        dev = qbus_find_dev(bus, elem);
 492        if (!dev) {
 493            error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 494                      "Device '%s' not found", elem);
 495            qbus_list_dev(bus, errp);
 496            return NULL;
 497        }
 498
 499        assert(path[pos] == '/' || !path[pos]);
 500        while (path[pos] == '/') {
 501            pos++;
 502        }
 503        if (path[pos] == '\0') {
 504            /* last specified element is a device.  If it has exactly
 505             * one child bus accept it nevertheless */
 506            if (dev->num_child_bus == 1) {
 507                bus = QLIST_FIRST(&dev->child_bus);
 508                break;
 509            }
 510            if (dev->num_child_bus) {
 511                error_setg(errp, "Device '%s' has multiple child buses",
 512                           elem);
 513                qbus_list_bus(dev, errp);
 514            } else {
 515                error_setg(errp, "Device '%s' has no child bus", elem);
 516            }
 517            return NULL;
 518        }
 519
 520        /* find bus */
 521        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
 522            g_assert_not_reached();
 523            elem[0] = len = 0;
 524        }
 525        pos += len;
 526        bus = qbus_find_bus(dev, elem);
 527        if (!bus) {
 528            error_setg(errp, "Bus '%s' not found", elem);
 529            qbus_list_bus(dev, errp);
 530            return NULL;
 531        }
 532    }
 533
 534    if (qbus_is_full(bus)) {
 535        error_setg(errp, "Bus '%s' is full", path);
 536        return NULL;
 537    }
 538    return bus;
 539}
 540
 541DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
 542{
 543    DeviceClass *dc;
 544    const char *driver, *path, *id;
 545    DeviceState *dev;
 546    BusState *bus = NULL;
 547    Error *err = NULL;
 548
 549    driver = qemu_opt_get(opts, "driver");
 550    if (!driver) {
 551        error_setg(errp, QERR_MISSING_PARAMETER, "driver");
 552        return NULL;
 553    }
 554
 555    /* find driver */
 556    dc = qdev_get_device_class(&driver, errp);
 557    if (!dc) {
 558        return NULL;
 559    }
 560
 561    /* find bus */
 562    path = qemu_opt_get(opts, "bus");
 563    if (path != NULL) {
 564        bus = qbus_find(path, errp);
 565        if (!bus) {
 566            return NULL;
 567        }
 568        if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
 569            error_setg(errp, "Device '%s' can't go on %s bus",
 570                       driver, object_get_typename(OBJECT(bus)));
 571            return NULL;
 572        }
 573    } else if (dc->bus_type != NULL) {
 574        bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
 575        if (!bus || qbus_is_full(bus)) {
 576            error_setg(errp, "No '%s' bus found for device '%s'",
 577                       dc->bus_type, driver);
 578            return NULL;
 579        }
 580    }
 581    if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
 582        error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
 583        return NULL;
 584    }
 585
 586    /* create device */
 587    dev = DEVICE(object_new(driver));
 588
 589    if (bus) {
 590        qdev_set_parent_bus(dev, bus);
 591    } else {
 592        if (dc->reset) {
 593            qemu_register_reset((void (*)(void *))dc->reset, dev);
 594        }
 595    }
 596
 597    id = qemu_opts_id(opts);
 598    if (id) {
 599        dev->id = (char *) id;
 600    }
 601
 602    if (dev->id) {
 603        object_property_add_child(qdev_get_peripheral(), dev->id,
 604                                  OBJECT(dev), NULL);
 605    } else {
 606        static int anon_count;
 607        gchar *name = g_strdup_printf("device[%d]", anon_count++);
 608        object_property_add_child(qdev_get_peripheral_anon(), name,
 609                                  OBJECT(dev), NULL);
 610        g_free(name);
 611    }
 612
 613    /* set properties */
 614    if (qemu_opt_foreach(opts, set_property, dev, &err)) {
 615        error_propagate(errp, err);
 616        object_unparent(OBJECT(dev));
 617        object_unref(OBJECT(dev));
 618        return NULL;
 619    }
 620
 621    dev->opts = opts;
 622    object_property_set_bool(OBJECT(dev), true, "realized", &err);
 623    if (err != NULL) {
 624        error_propagate(errp, err);
 625        dev->opts = NULL;
 626        object_unparent(OBJECT(dev));
 627        object_unref(OBJECT(dev));
 628        return NULL;
 629    }
 630    return dev;
 631}
 632
 633
 634#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
 635static void qbus_print(Monitor *mon, BusState *bus, int indent);
 636
 637static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
 638                             int indent)
 639{
 640    if (!props)
 641        return;
 642    for (; props->name; props++) {
 643        Error *err = NULL;
 644        char *value;
 645        char *legacy_name = g_strdup_printf("legacy-%s", props->name);
 646        if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
 647            value = object_property_get_str(OBJECT(dev), legacy_name, &err);
 648        } else {
 649            value = object_property_print(OBJECT(dev), props->name, true, &err);
 650        }
 651        g_free(legacy_name);
 652
 653        if (err) {
 654            error_free(err);
 655            continue;
 656        }
 657        qdev_printf("%s = %s\n", props->name,
 658                    value && *value ? value : "<null>");
 659        g_free(value);
 660    }
 661}
 662
 663static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
 664{
 665    BusClass *bc = BUS_GET_CLASS(bus);
 666
 667    if (bc->print_dev) {
 668        bc->print_dev(mon, dev, indent);
 669    }
 670}
 671
 672static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
 673{
 674    ObjectClass *class;
 675    BusState *child;
 676    NamedGPIOList *ngl;
 677
 678    qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
 679                dev->id ? dev->id : "");
 680    indent += 2;
 681    QLIST_FOREACH(ngl, &dev->gpios, node) {
 682        if (ngl->num_in) {
 683            qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
 684                        ngl->num_in);
 685        }
 686        if (ngl->num_out) {
 687            qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
 688                        ngl->num_out);
 689        }
 690    }
 691    class = object_get_class(OBJECT(dev));
 692    do {
 693        qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
 694        class = object_class_get_parent(class);
 695    } while (class != object_class_by_name(TYPE_DEVICE));
 696    bus_print_dev(dev->parent_bus, mon, dev, indent);
 697    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 698        qbus_print(mon, child, indent);
 699    }
 700}
 701
 702static void qbus_print(Monitor *mon, BusState *bus, int indent)
 703{
 704    BusChild *kid;
 705
 706    qdev_printf("bus: %s\n", bus->name);
 707    indent += 2;
 708    qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
 709    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 710        DeviceState *dev = kid->child;
 711        qdev_print(mon, dev, indent);
 712    }
 713}
 714#undef qdev_printf
 715
 716void hmp_info_qtree(Monitor *mon, const QDict *qdict)
 717{
 718    if (sysbus_get_default())
 719        qbus_print(mon, sysbus_get_default(), 0);
 720}
 721
 722void hmp_info_qdm(Monitor *mon, const QDict *qdict)
 723{
 724    qdev_print_devinfos(true);
 725}
 726
 727typedef struct QOMCompositionState {
 728    Monitor *mon;
 729    int indent;
 730} QOMCompositionState;
 731
 732static void print_qom_composition(Monitor *mon, Object *obj, int indent);
 733
 734static int print_qom_composition_child(Object *obj, void *opaque)
 735{
 736    QOMCompositionState *s = opaque;
 737
 738    print_qom_composition(s->mon, obj, s->indent);
 739
 740    return 0;
 741}
 742
 743static void print_qom_composition(Monitor *mon, Object *obj, int indent)
 744{
 745    QOMCompositionState s = {
 746        .mon = mon,
 747        .indent = indent + 2,
 748    };
 749    char *name;
 750
 751    if (obj == object_get_root()) {
 752        name = g_strdup("");
 753    } else {
 754        name = object_get_canonical_path_component(obj);
 755    }
 756    monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
 757                   object_get_typename(obj));
 758    g_free(name);
 759    object_child_foreach(obj, print_qom_composition_child, &s);
 760}
 761
 762void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
 763{
 764    const char *path = qdict_get_try_str(dict, "path");
 765    Object *obj;
 766    bool ambiguous = false;
 767
 768    if (path) {
 769        obj = object_resolve_path(path, &ambiguous);
 770        if (!obj) {
 771            monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
 772            return;
 773        }
 774        if (ambiguous) {
 775            monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
 776            return;
 777        }
 778    } else {
 779        obj = qdev_get_machine();
 780    }
 781    print_qom_composition(mon, obj, 0);
 782}
 783
 784void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
 785{
 786    Error *local_err = NULL;
 787    QemuOpts *opts;
 788    DeviceState *dev;
 789
 790    opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
 791    if (local_err) {
 792        error_propagate(errp, local_err);
 793        return;
 794    }
 795    if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
 796        qemu_opts_del(opts);
 797        return;
 798    }
 799    dev = qdev_device_add(opts, &local_err);
 800    if (!dev) {
 801        error_propagate(errp, local_err);
 802        qemu_opts_del(opts);
 803        return;
 804    }
 805    object_unref(OBJECT(dev));
 806}
 807
 808void qmp_device_del(const char *id, Error **errp)
 809{
 810    Object *obj;
 811
 812    if (id[0] == '/') {
 813        obj = object_resolve_path(id, NULL);
 814    } else {
 815        char *root_path = object_get_canonical_path(qdev_get_peripheral());
 816        char *path = g_strdup_printf("%s/%s", root_path, id);
 817
 818        g_free(root_path);
 819        obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
 820        g_free(path);
 821    }
 822
 823    if (!obj) {
 824        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 825                  "Device '%s' not found", id);
 826        return;
 827    }
 828
 829    if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
 830        error_setg(errp, "%s is not a hotpluggable device", id);
 831        return;
 832    }
 833
 834    qdev_unplug(DEVICE(obj), errp);
 835}
 836
 837void qdev_machine_init(void)
 838{
 839    qdev_get_peripheral_anon();
 840    qdev_get_peripheral();
 841}
 842
 843QemuOptsList qemu_device_opts = {
 844    .name = "device",
 845    .implied_opt_name = "driver",
 846    .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
 847    .desc = {
 848        /*
 849         * no elements => accept any
 850         * sanity checking will happen later
 851         * when setting device properties
 852         */
 853        { /* end of list */ }
 854    },
 855};
 856
 857QemuOptsList qemu_global_opts = {
 858    .name = "global",
 859    .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
 860    .desc = {
 861        {
 862            .name = "driver",
 863            .type = QEMU_OPT_STRING,
 864        },{
 865            .name = "property",
 866            .type = QEMU_OPT_STRING,
 867        },{
 868            .name = "value",
 869            .type = QEMU_OPT_STRING,
 870        },
 871        { /* end of list */ }
 872    },
 873};
 874
 875int qemu_global_option(const char *str)
 876{
 877    char driver[64], property[64];
 878    QemuOpts *opts;
 879    int rc, offset;
 880
 881    rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
 882    if (rc == 2 && str[offset] == '=') {
 883        opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
 884        qemu_opt_set(opts, "driver", driver, &error_abort);
 885        qemu_opt_set(opts, "property", property, &error_abort);
 886        qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
 887        return 0;
 888    }
 889
 890    opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
 891    if (!opts) {
 892        return -1;
 893    }
 894
 895    return 0;
 896}
 897