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 "qemu/readline.h"
17 #include "qom/object.h"
18 #include "qom/object_interfaces.h"
20 void hmp_qom_list(Monitor
*mon
, const QDict
*qdict
)
22 const char *path
= qdict_get_try_str(qdict
, "path");
23 ObjectPropertyInfoList
*list
;
27 monitor_printf(mon
, "/\n");
31 list
= qmp_qom_list(path
, &err
);
33 ObjectPropertyInfoList
*start
= list
;
34 while (list
!= NULL
) {
35 ObjectPropertyInfo
*value
= list
->value
;
37 monitor_printf(mon
, "%s (%s)\n",
38 value
->name
, value
->type
);
41 qapi_free_ObjectPropertyInfoList(start
);
43 hmp_handle_error(mon
, err
);
46 void hmp_qom_set(Monitor
*mon
, const QDict
*qdict
)
48 const bool json
= qdict_get_try_bool(qdict
, "json", false);
49 const char *path
= qdict_get_str(qdict
, "path");
50 const char *property
= qdict_get_str(qdict
, "property");
51 const char *value
= qdict_get_str(qdict
, "value");
55 Object
*obj
= object_resolve_path(path
, NULL
);
58 error_set(&err
, ERROR_CLASS_DEVICE_NOT_FOUND
,
59 "Device '%s' not found", path
);
61 object_property_parse(obj
, property
, value
, &err
);
64 QObject
*obj
= qobject_from_json(value
, &err
);
67 qmp_qom_set(path
, property
, obj
, &err
);
71 hmp_handle_error(mon
, err
);
74 void hmp_qom_get(Monitor
*mon
, const QDict
*qdict
)
76 const char *path
= qdict_get_str(qdict
, "path");
77 const char *property
= qdict_get_str(qdict
, "property");
79 QObject
*obj
= qmp_qom_get(path
, property
, &err
);
82 GString
*str
= qobject_to_json_pretty(obj
, true);
83 monitor_printf(mon
, "%s\n", str
->str
);
84 g_string_free(str
, true);
88 hmp_handle_error(mon
, err
);
91 typedef struct QOMCompositionState
{
94 } QOMCompositionState
;
96 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
);
98 static int qom_composition_compare(const void *a
, const void *b
)
100 return g_strcmp0(object_get_canonical_path_component(*(Object
**)a
),
101 object_get_canonical_path_component(*(Object
**)b
));
104 static int insert_qom_composition_child(Object
*obj
, void *opaque
)
106 g_array_append_val(opaque
, obj
);
110 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
)
112 GArray
*children
= g_array_new(false, false, sizeof(Object
*));
116 if (obj
== object_get_root()) {
119 name
= object_get_canonical_path_component(obj
);
121 monitor_printf(mon
, "%*s/%s (%s)\n", indent
, "", name
,
122 object_get_typename(obj
));
124 object_child_foreach(obj
, insert_qom_composition_child
, children
);
125 g_array_sort(children
, qom_composition_compare
);
127 for (i
= 0; i
< children
->len
; i
++) {
128 print_qom_composition(mon
, g_array_index(children
, Object
*, i
),
131 g_array_free(children
, TRUE
);
134 void hmp_info_qom_tree(Monitor
*mon
, const QDict
*dict
)
136 const char *path
= qdict_get_try_str(dict
, "path");
138 bool ambiguous
= false;
141 obj
= object_resolve_path(path
, &ambiguous
);
143 monitor_printf(mon
, "Path '%s' could not be resolved.\n", path
);
147 monitor_printf(mon
, "Warning: Path '%s' is ambiguous.\n", path
);
151 obj
= qdev_get_machine();
153 print_qom_composition(mon
, obj
, 0);
156 void hmp_object_add(Monitor
*mon
, const QDict
*qdict
)
158 const char *options
= qdict_get_str(qdict
, "object");
161 user_creatable_add_from_str(options
, &err
);
162 hmp_handle_error(mon
, err
);
165 void hmp_object_del(Monitor
*mon
, const QDict
*qdict
)
167 const char *id
= qdict_get_str(qdict
, "id");
170 user_creatable_del(id
, &err
);
171 hmp_handle_error(mon
, err
);
174 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
184 readline_set_completion_index(rs
, len
);
185 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
189 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
190 if (strcmp(name
, TYPE_USER_CREATABLE
)) {
191 readline_add_completion_of(rs
, str
, name
);
198 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
200 ObjectPropertyInfoList
*list
, *start
;
207 readline_set_completion_index(rs
, len
);
209 start
= list
= qmp_qom_list("/objects", NULL
);
211 ObjectPropertyInfo
*info
= list
->value
;
213 if (!strncmp(info
->type
, "child<", 5)) {
214 readline_add_completion_of(rs
, str
, info
->name
);
218 qapi_free_ObjectPropertyInfoList(start
);