/src/qemu/qom/qom-hmp-cmds.c
Line | Count | Source |
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/core/qdev.h" |
10 | | #include "monitor/hmp.h" |
11 | | #include "monitor/hmp-completion.h" |
12 | | #include "monitor/monitor.h" |
13 | | #include "qapi/error.h" |
14 | | #include "qapi/qapi-commands-qom.h" |
15 | | #include "qobject/qdict.h" |
16 | | #include "qobject/qjson.h" |
17 | | #include "qemu/readline.h" |
18 | | #include "qom/object.h" |
19 | | #include "qom/object_interfaces.h" |
20 | | |
21 | | void hmp_qom_list(MonitorHMP *hmp, const QDict *qdict) |
22 | 0 | { |
23 | 0 | const char *path = qdict_get_try_str(qdict, "path"); |
24 | 0 | ObjectPropertyInfoList *list; |
25 | 0 | Error *err = NULL; |
26 | |
|
27 | 0 | if (path == NULL) { |
28 | 0 | monitor_hmp_printf(hmp, "/\n"); |
29 | 0 | return; |
30 | 0 | } |
31 | | |
32 | 0 | list = qmp_qom_list(path, &err); |
33 | 0 | if (err == NULL) { |
34 | 0 | ObjectPropertyInfoList *start = list; |
35 | 0 | while (list != NULL) { |
36 | 0 | ObjectPropertyInfo *value = list->value; |
37 | |
|
38 | 0 | monitor_hmp_printf(hmp, "%s (%s)\n", |
39 | 0 | value->name, value->type); |
40 | 0 | list = list->next; |
41 | 0 | } |
42 | 0 | qapi_free_ObjectPropertyInfoList(start); |
43 | 0 | } |
44 | 0 | hmp_handle_error(hmp, err); |
45 | 0 | } |
46 | | |
47 | | void hmp_qom_set(MonitorHMP *hmp, const QDict *qdict) |
48 | 0 | { |
49 | 0 | const bool json = qdict_get_try_bool(qdict, "json", false); |
50 | 0 | const char *path = qdict_get_str(qdict, "path"); |
51 | 0 | const char *property = qdict_get_str(qdict, "property"); |
52 | 0 | const char *value = qdict_get_str(qdict, "value"); |
53 | 0 | Error *err = NULL; |
54 | |
|
55 | 0 | if (!json) { |
56 | 0 | Object *obj = object_resolve_path(path, NULL); |
57 | |
|
58 | 0 | if (!obj) { |
59 | 0 | error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND, |
60 | 0 | "Device '%s' not found", path); |
61 | 0 | } else { |
62 | 0 | object_property_parse(obj, property, value, &err); |
63 | 0 | } |
64 | 0 | } else { |
65 | 0 | QObject *obj = qobject_from_json(value, &err); |
66 | |
|
67 | 0 | if (!err) { |
68 | 0 | qmp_qom_set(path, property, obj, &err); |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | 0 | hmp_handle_error(hmp, err); |
73 | 0 | } |
74 | | |
75 | | void hmp_qom_get(MonitorHMP *hmp, const QDict *qdict) |
76 | 0 | { |
77 | 0 | const char *path = qdict_get_str(qdict, "path"); |
78 | 0 | const char *property = qdict_get_str(qdict, "property"); |
79 | 0 | Error *err = NULL; |
80 | 0 | QObject *obj = qmp_qom_get(path, property, &err); |
81 | |
|
82 | 0 | if (err == NULL) { |
83 | 0 | GString *str = qobject_to_json_pretty(obj, true); |
84 | 0 | monitor_hmp_printf(hmp, "%s\n", str->str); |
85 | 0 | g_string_free(str, true); |
86 | 0 | } |
87 | |
|
88 | 0 | qobject_unref(obj); |
89 | 0 | hmp_handle_error(hmp, err); |
90 | 0 | } |
91 | | |
92 | | typedef struct QOMCompositionState { |
93 | | Monitor *mon; |
94 | | int indent; |
95 | | } QOMCompositionState; |
96 | | |
97 | | static void print_qom_composition(MonitorHMP *hmp, Object *obj, int indent); |
98 | | |
99 | | static int qom_composition_compare(const void *a, const void *b) |
100 | 0 | { |
101 | 0 | return g_strcmp0(object_get_canonical_path_component(*(Object **)a), |
102 | 0 | object_get_canonical_path_component(*(Object **)b)); |
103 | 0 | } |
104 | | |
105 | | static int insert_qom_composition_child(Object *obj, void *opaque) |
106 | 0 | { |
107 | 0 | g_array_append_val(opaque, obj); |
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | | static void print_qom_composition(MonitorHMP *hmp, Object *obj, int indent) |
112 | 0 | { |
113 | 0 | GArray *children = g_array_new(false, false, sizeof(Object *)); |
114 | 0 | const char *name; |
115 | 0 | int i; |
116 | |
|
117 | 0 | if (obj == object_get_root()) { |
118 | 0 | name = ""; |
119 | 0 | } else { |
120 | 0 | name = object_get_canonical_path_component(obj); |
121 | 0 | } |
122 | 0 | monitor_hmp_printf(hmp, "%*s/%s (%s)\n", indent, "", name, |
123 | 0 | object_get_typename(obj)); |
124 | |
|
125 | 0 | object_child_foreach(obj, insert_qom_composition_child, children); |
126 | 0 | g_array_sort(children, qom_composition_compare); |
127 | |
|
128 | 0 | for (i = 0; i < children->len; i++) { |
129 | 0 | print_qom_composition(hmp, g_array_index(children, Object *, i), |
130 | 0 | indent + 2); |
131 | 0 | } |
132 | 0 | g_array_free(children, TRUE); |
133 | 0 | } |
134 | | |
135 | | void hmp_info_qom_tree(MonitorHMP *hmp, const QDict *dict) |
136 | 0 | { |
137 | 0 | const char *path = qdict_get_try_str(dict, "path"); |
138 | 0 | Object *obj; |
139 | 0 | bool ambiguous = false; |
140 | |
|
141 | 0 | if (path) { |
142 | 0 | obj = object_resolve_path(path, &ambiguous); |
143 | 0 | if (!obj) { |
144 | 0 | monitor_hmp_printf(hmp, "Path '%s' could not be resolved.\n", path); |
145 | 0 | return; |
146 | 0 | } |
147 | 0 | if (ambiguous) { |
148 | 0 | monitor_hmp_printf(hmp, "Warning: Path '%s' is ambiguous.\n", path); |
149 | 0 | return; |
150 | 0 | } |
151 | 0 | } else { |
152 | 0 | obj = qdev_get_machine(); |
153 | 0 | } |
154 | 0 | print_qom_composition(hmp, obj, 0); |
155 | 0 | } |
156 | | |
157 | | void hmp_object_add(MonitorHMP *hmp, const QDict *qdict) |
158 | 0 | { |
159 | 0 | const char *options = qdict_get_str(qdict, "object"); |
160 | 0 | Error *err = NULL; |
161 | |
|
162 | 0 | user_creatable_add_from_str(options, &err); |
163 | 0 | hmp_handle_error(hmp, err); |
164 | 0 | } |
165 | | |
166 | | void hmp_object_del(MonitorHMP *hmp, const QDict *qdict) |
167 | 0 | { |
168 | 0 | const char *id = qdict_get_str(qdict, "id"); |
169 | 0 | Error *err = NULL; |
170 | |
|
171 | 0 | user_creatable_del(id, &err); |
172 | 0 | hmp_handle_error(hmp, err); |
173 | 0 | } |
174 | | |
175 | | void object_add_completion(ReadLineState *rs, int nb_args, const char *str) |
176 | 0 | { |
177 | 0 | GSList *list, *elt; |
178 | 0 | size_t len; |
179 | |
|
180 | 0 | if (nb_args != 2) { |
181 | 0 | return; |
182 | 0 | } |
183 | | |
184 | 0 | len = strlen(str); |
185 | 0 | readline_set_completion_index(rs, len); |
186 | 0 | list = elt = object_class_get_list(TYPE_USER_CREATABLE, false); |
187 | 0 | while (elt) { |
188 | 0 | const char *name; |
189 | |
|
190 | 0 | name = object_class_get_name(OBJECT_CLASS(elt->data)); |
191 | 0 | if (strcmp(name, TYPE_USER_CREATABLE)) { |
192 | 0 | readline_add_completion_of(rs, str, name); |
193 | 0 | } |
194 | 0 | elt = elt->next; |
195 | 0 | } |
196 | 0 | g_slist_free(list); |
197 | 0 | } |
198 | | |
199 | | void object_del_completion(ReadLineState *rs, int nb_args, const char *str) |
200 | 0 | { |
201 | 0 | ObjectPropertyInfoList *list, *start; |
202 | 0 | size_t len; |
203 | |
|
204 | 0 | if (nb_args != 2) { |
205 | 0 | return; |
206 | 0 | } |
207 | 0 | len = strlen(str); |
208 | 0 | readline_set_completion_index(rs, len); |
209 | |
|
210 | 0 | start = list = qmp_qom_list("/objects", NULL); |
211 | 0 | while (list) { |
212 | 0 | ObjectPropertyInfo *info = list->value; |
213 | |
|
214 | 0 | if (!strncmp(info->type, "child<", 5)) { |
215 | 0 | readline_add_completion_of(rs, str, info->name); |
216 | 0 | } |
217 | 0 | list = list->next; |
218 | 0 | } |
219 | 0 | qapi_free_ObjectPropertyInfoList(start); |
220 | 0 | } |