ppc/pnv: assign pnv-phb-root-port chassis/slot earlier
[qemu.git] / qom / qom-hmp-cmds.c
blob453fbfeabce7ca3ee6b229670c807c12d4ed8e18
1 /*
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.
6 */
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;
22 Error *err = NULL;
24 if (path == NULL) {
25 monitor_printf(mon, "/\n");
26 return;
29 list = qmp_qom_list(path, &err);
30 if (err == NULL) {
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);
37 list = list->next;
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");
50 Error *err = NULL;
52 if (!json) {
53 Object *obj = object_resolve_path(path, NULL);
55 if (!obj) {
56 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
57 "Device '%s' not found", path);
58 } else {
59 object_property_parse(obj, property, value, &err);
61 } else {
62 QObject *obj = qobject_from_json(value, &err);
64 if (!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");
76 Error *err = NULL;
77 QObject *obj = qmp_qom_get(path, property, &err);
79 if (err == NULL) {
80 GString *str = qobject_to_json_pretty(obj, true);
81 monitor_printf(mon, "%s\n", str->str);
82 g_string_free(str, true);
85 qobject_unref(obj);
86 hmp_handle_error(mon, err);
89 typedef struct QOMCompositionState {
90 Monitor *mon;
91 int indent;
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);
105 return 0;
108 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
110 GArray *children = g_array_new(false, false, sizeof(Object *));
111 const char *name;
112 int i;
114 if (obj == object_get_root()) {
115 name = "";
116 } else {
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),
127 indent + 2);
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");
135 Object *obj;
136 bool ambiguous = false;
138 if (path) {
139 obj = object_resolve_path(path, &ambiguous);
140 if (!obj) {
141 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
142 return;
144 if (ambiguous) {
145 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
146 return;
148 } else {
149 obj = qdev_get_machine();
151 print_qom_composition(mon, obj, 0);