xref: /openbmc/qemu/qom/qom-hmp-cmds.c (revision f7160f32)
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 bool json = qdict_get_try_bool(qdict, "json", false);
48     const char *path = qdict_get_str(qdict, "path");
49     const char *property = qdict_get_str(qdict, "property");
50     const char *value = qdict_get_str(qdict, "value");
51     Error *err = NULL;
52 
53     if (!json) {
54         Object *obj = object_resolve_path(path, NULL);
55 
56         if (!obj) {
57             error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
58                       "Device '%s' not found", path);
59         } else {
60             object_property_parse(obj, property, value, &err);
61         }
62     } else {
63         QObject *obj = qobject_from_json(value, &err);
64 
65         if (!err) {
66             qmp_qom_set(path, property, obj, &err);
67         }
68     }
69 
70     hmp_handle_error(mon, err);
71 }
72 
73 void hmp_qom_get(Monitor *mon, const QDict *qdict)
74 {
75     const char *path = qdict_get_str(qdict, "path");
76     const char *property = qdict_get_str(qdict, "property");
77     Error *err = NULL;
78     QObject *obj = qmp_qom_get(path, property, &err);
79 
80     if (err == NULL) {
81         QString *str = qobject_to_json_pretty(obj);
82         monitor_printf(mon, "%s\n", qstring_get_str(str));
83         qobject_unref(str);
84     }
85 
86     qobject_unref(obj);
87     hmp_handle_error(mon, err);
88 }
89 
90 typedef struct QOMCompositionState {
91     Monitor *mon;
92     int indent;
93 } QOMCompositionState;
94 
95 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
96 
97 static int qom_composition_compare(const void *a, const void *b)
98 {
99     return g_strcmp0(object_get_canonical_path_component(*(Object **)a),
100                      object_get_canonical_path_component(*(Object **)b));
101 }
102 
103 static int insert_qom_composition_child(Object *obj, void *opaque)
104 {
105     g_array_append_val(opaque, obj);
106     return 0;
107 }
108 
109 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
110 {
111     GArray *children = g_array_new(false, false, sizeof(Object *));
112     const char *name;
113     int i;
114 
115     if (obj == object_get_root()) {
116         name = "";
117     } else {
118         name = object_get_canonical_path_component(obj);
119     }
120     monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
121                    object_get_typename(obj));
122 
123     object_child_foreach(obj, insert_qom_composition_child, children);
124     g_array_sort(children, qom_composition_compare);
125 
126     for (i = 0; i < children->len; i++) {
127         print_qom_composition(mon, g_array_index(children, Object *, i),
128                               indent + 2);
129     }
130     g_array_free(children, TRUE);
131 }
132 
133 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
134 {
135     const char *path = qdict_get_try_str(dict, "path");
136     Object *obj;
137     bool ambiguous = false;
138 
139     if (path) {
140         obj = object_resolve_path(path, &ambiguous);
141         if (!obj) {
142             monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
143             return;
144         }
145         if (ambiguous) {
146             monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
147             return;
148         }
149     } else {
150         obj = qdev_get_machine();
151     }
152     print_qom_composition(mon, obj, 0);
153 }
154