2 * HMP commands related to QOM
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.
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 "qom/object.h"
18 void hmp_qom_list(Monitor
*mon
, const QDict
*qdict
)
20 const char *path
= qdict_get_try_str(qdict
, "path");
21 ObjectPropertyInfoList
*list
;
25 monitor_printf(mon
, "/\n");
29 list
= qmp_qom_list(path
, &err
);
31 ObjectPropertyInfoList
*start
= list
;
32 while (list
!= NULL
) {
33 ObjectPropertyInfo
*value
= list
->value
;
35 monitor_printf(mon
, "%s (%s)\n",
36 value
->name
, value
->type
);
39 qapi_free_ObjectPropertyInfoList(start
);
41 hmp_handle_error(mon
, err
);
44 void hmp_qom_set(Monitor
*mon
, const QDict
*qdict
)
46 const bool json
= qdict_get_try_bool(qdict
, "json", false);
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");
53 Object
*obj
= object_resolve_path(path
, NULL
);
56 error_set(&err
, ERROR_CLASS_DEVICE_NOT_FOUND
,
57 "Device '%s' not found", path
);
59 object_property_parse(obj
, property
, value
, &err
);
62 QObject
*obj
= qobject_from_json(value
, &err
);
65 qmp_qom_set(path
, property
, obj
, &err
);
69 hmp_handle_error(mon
, err
);
72 void hmp_qom_get(Monitor
*mon
, const QDict
*qdict
)
74 const char *path
= qdict_get_str(qdict
, "path");
75 const char *property
= qdict_get_str(qdict
, "property");
77 QObject
*obj
= qmp_qom_get(path
, property
, &err
);
80 GString
*str
= qobject_to_json_pretty(obj
, true);
81 monitor_printf(mon
, "%s\n", str
->str
);
82 g_string_free(str
, true);
86 hmp_handle_error(mon
, err
);
89 typedef struct QOMCompositionState
{
92 } QOMCompositionState
;
94 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
);
96 static int qom_composition_compare(const void *a
, const void *b
)
98 return g_strcmp0(object_get_canonical_path_component(*(Object
**)a
),
99 object_get_canonical_path_component(*(Object
**)b
));
102 static int insert_qom_composition_child(Object
*obj
, void *opaque
)
104 g_array_append_val(opaque
, obj
);
108 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
)
110 GArray
*children
= g_array_new(false, false, sizeof(Object
*));
114 if (obj
== object_get_root()) {
117 name
= object_get_canonical_path_component(obj
);
119 monitor_printf(mon
, "%*s/%s (%s)\n", indent
, "", name
,
120 object_get_typename(obj
));
122 object_child_foreach(obj
, insert_qom_composition_child
, children
);
123 g_array_sort(children
, qom_composition_compare
);
125 for (i
= 0; i
< children
->len
; i
++) {
126 print_qom_composition(mon
, g_array_index(children
, Object
*, i
),
129 g_array_free(children
, TRUE
);
132 void hmp_info_qom_tree(Monitor
*mon
, const QDict
*dict
)
134 const char *path
= qdict_get_try_str(dict
, "path");
136 bool ambiguous
= false;
139 obj
= object_resolve_path(path
, &ambiguous
);
141 monitor_printf(mon
, "Path '%s' could not be resolved.\n", path
);
145 monitor_printf(mon
, "Warning: Path '%s' is ambiguous.\n", path
);
149 obj
= qdev_get_machine();
151 print_qom_composition(mon
, obj
, 0);