]> Git Repo - qemu.git/blob - qom/qom-hmp-cmds.c
qom: Make "info qom-tree" show children sorted
[qemu.git] / qom / qom-hmp-cmds.c
1 /*
2  * HMP commands related to QOM
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or
5  * later.  See the COPYING file in the top-level directory.
6  */
7
8 #include "qemu/osdep.h"
9 #include "hw/qdev-core.h"
10 #include "monitor/hmp.h"
11 #include "monitor/monitor.h"
12 #include "qapi/error.h"
13 #include "qapi/qapi-commands-qom.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qjson.h"
16 #include "qapi/qmp/qstring.h"
17 #include "qom/object.h"
18
19 void hmp_qom_list(Monitor *mon, const QDict *qdict)
20 {
21     const char *path = qdict_get_try_str(qdict, "path");
22     ObjectPropertyInfoList *list;
23     Error *err = NULL;
24
25     if (path == NULL) {
26         monitor_printf(mon, "/\n");
27         return;
28     }
29
30     list = qmp_qom_list(path, &err);
31     if (err == NULL) {
32         ObjectPropertyInfoList *start = list;
33         while (list != NULL) {
34             ObjectPropertyInfo *value = list->value;
35
36             monitor_printf(mon, "%s (%s)\n",
37                            value->name, value->type);
38             list = list->next;
39         }
40         qapi_free_ObjectPropertyInfoList(start);
41     }
42     hmp_handle_error(mon, err);
43 }
44
45 void hmp_qom_set(Monitor *mon, const QDict *qdict)
46 {
47     const char *path = qdict_get_str(qdict, "path");
48     const char *property = qdict_get_str(qdict, "property");
49     const char *value = qdict_get_str(qdict, "value");
50     Error *err = NULL;
51     QObject *obj;
52
53     obj = qobject_from_json(value, &err);
54     if (err == NULL) {
55         qmp_qom_set(path, property, obj, &err);
56     }
57
58     hmp_handle_error(mon, err);
59 }
60
61 void hmp_qom_get(Monitor *mon, const QDict *qdict)
62 {
63     const char *path = qdict_get_str(qdict, "path");
64     const char *property = qdict_get_str(qdict, "property");
65     Error *err = NULL;
66     QObject *obj = qmp_qom_get(path, property, &err);
67
68     if (err == NULL) {
69         QString *str = qobject_to_json_pretty(obj);
70         monitor_printf(mon, "%s\n", qstring_get_str(str));
71         qobject_unref(str);
72     }
73
74     hmp_handle_error(mon, err);
75 }
76
77 typedef struct QOMCompositionState {
78     Monitor *mon;
79     int indent;
80 } QOMCompositionState;
81
82 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
83
84 static int qom_composition_compare(const void *a, const void *b, void *ignore)
85 {
86     return g_strcmp0(a ? object_get_canonical_path_component(a) : NULL,
87                      b ? object_get_canonical_path_component(b) : NULL);
88 }
89
90 static int insert_qom_composition_child(Object *obj, void *opaque)
91 {
92     GQueue *children = opaque;
93
94     g_queue_insert_sorted(children, obj, qom_composition_compare, NULL);
95     return 0;
96 }
97
98 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
99 {
100     char *name;
101     GQueue children;
102     Object *child;
103
104     if (obj == object_get_root()) {
105         name = g_strdup("");
106     } else {
107         name = object_get_canonical_path_component(obj);
108     }
109     monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
110                    object_get_typename(obj));
111     g_free(name);
112
113     g_queue_init(&children);
114     object_child_foreach(obj, insert_qom_composition_child, &children);
115     while ((child = g_queue_pop_head(&children))) {
116         print_qom_composition(mon, child, indent + 2);
117     }
118 }
119
120 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
121 {
122     const char *path = qdict_get_try_str(dict, "path");
123     Object *obj;
124     bool ambiguous = false;
125
126     if (path) {
127         obj = object_resolve_path(path, &ambiguous);
128         if (!obj) {
129             monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
130             return;
131         }
132         if (ambiguous) {
133             monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
134             return;
135         }
136     } else {
137         obj = qdev_get_machine();
138     }
139     print_qom_composition(mon, obj, 0);
140 }
This page took 0.037409 seconds and 4 git commands to generate.