diff mbox

[RFC,1/5] qdev: Create qdev_get_dev_path()

Message ID 20100614055119.879.92321.stgit@localhost.localdomain (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Williamson June 14, 2010, 5:51 a.m. UTC
None
diff mbox

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 9ffdba7..e30df88 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -453,6 +453,7 @@  PropertyInfo qdev_prop_macaddr = {
     .name  = "macaddr",
     .type  = PROP_TYPE_MACADDR,
     .size  = sizeof(MACAddr),
+    .flags = PROP_FLAG_PATH,
     .parse = parse_mac,
     .print = print_mac,
 };
@@ -496,6 +497,7 @@  PropertyInfo qdev_prop_pci_devfn = {
     .name  = "pci-devfn",
     .type  = PROP_TYPE_UINT32,
     .size  = sizeof(uint32_t),
+    .flags = PROP_FLAG_PATH,
     .parse = parse_pci_devfn,
     .print = print_pci_devfn,
 };
diff --git a/hw/qdev.c b/hw/qdev.c
index 36f29ea..dea44fe 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -120,6 +120,63 @@  DeviceState *qdev_create(BusState *bus, const char *name)
     return qdev_create_from_info(bus, info);
 }
 
+static int qdev_strprint_parent_path(DeviceState *dev, char *buf, size_t len)
+{
+    int offset = 0;
+
+    if (dev->parent_bus && dev->parent_bus->parent)
+        offset = qdev_strprint_parent_path(dev->parent_bus->parent, buf, len);
+
+    offset += snprintf(buf + offset, len - offset,
+                        "/%s", dev->parent_bus->name);
+    return offset;
+}
+
+static int qdev_strprint_path_props(DeviceState *dev, Property *props,
+                                    char *buf, size_t len)
+{
+    int offset = 0;
+    char pbuf[64];
+
+    if (!props)
+        return 0;
+
+    while (props->name) {
+        if (props->info->flags & PROP_FLAG_PATH) {
+            if (props->info->print) {
+                props->info->print(dev, props, pbuf, sizeof(pbuf));
+                offset += snprintf(buf + offset, len - offset, ",%s=%s",
+                                   props->name, pbuf);
+            }
+        }
+        props++;
+    }
+    return offset;
+}
+
+char *qdev_get_dev_path(DeviceState *dev)
+{
+    char buf[256] = "";
+    int offset;
+
+    if (!dev)
+        return NULL;
+
+    offset = qdev_strprint_parent_path(dev, buf, sizeof(buf));
+
+    offset += qdev_strprint_path_props(dev, dev->parent_bus->info->props,
+                                       buf + offset, sizeof(buf) - offset);
+
+    offset += snprintf(buf + offset, sizeof(buf) - offset, "/%s",
+                       dev->info->name);
+    if (dev->id)
+        offset += snprintf(buf + offset, sizeof(buf) - offset, "-%s", dev->id);
+
+    offset += qdev_strprint_path_props(dev, dev->info->props,
+                                       buf + offset, sizeof(buf) - offset);
+    return qemu_strdup(buf);
+}
+
 static void qdev_print_devinfo(DeviceInfo *info)
 {
     error_printf("name \"%s\", bus %s",
diff --git a/hw/qdev.h b/hw/qdev.h
index a44060e..2702384 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -96,6 +96,7 @@  struct PropertyInfo {
     const char *name;
     size_t size;
     enum PropertyType type;
+    int flags;
     int (*parse)(DeviceState *dev, Property *prop, const char *str);
     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
 };
@@ -201,6 +202,8 @@  extern PropertyInfo qdev_prop_netdev;
 extern PropertyInfo qdev_prop_vlan;
 extern PropertyInfo qdev_prop_pci_devfn;
 
+#define PROP_FLAG_PATH        (1<<0)
+
 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
         .name      = (_name),                                    \
         .info      = &(_prop),                                   \
@@ -280,6 +283,8 @@  void qdev_prop_set_defaults(DeviceState *dev, Property *props);
 void qdev_prop_register_global_list(GlobalProperty *props);
 void qdev_prop_set_globals(DeviceState *dev);
 
+char *qdev_get_dev_path(DeviceState *dev);
+
 /* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 extern struct BusInfo system_bus_info;