docs: Add to gdbstub documentation the PhyMemMode
[qemu.git] / qom / object_interfaces.c
blob15fff66c3c58e1914a00f27def1f959f44d49057
1 #include "qemu/osdep.h"
3 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qerror.h"
7 #include "qapi/qmp/qjson.h"
8 #include "qapi/qmp/qstring.h"
9 #include "qapi/qobject-input-visitor.h"
10 #include "qom/object_interfaces.h"
11 #include "qemu/help_option.h"
12 #include "qemu/module.h"
13 #include "qemu/option.h"
14 #include "qapi/opts-visitor.h"
15 #include "qemu/config-file.h"
17 bool user_creatable_complete(UserCreatable *uc, Error **errp)
19 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
20 Error *err = NULL;
22 if (ucc->complete) {
23 ucc->complete(uc, &err);
24 error_propagate(errp, err);
26 return !err;
29 bool user_creatable_can_be_deleted(UserCreatable *uc)
32 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
34 if (ucc->can_be_deleted) {
35 return ucc->can_be_deleted(uc);
36 } else {
37 return true;
41 Object *user_creatable_add_type(const char *type, const char *id,
42 const QDict *qdict,
43 Visitor *v, Error **errp)
45 Object *obj;
46 ObjectClass *klass;
47 const QDictEntry *e;
48 Error *local_err = NULL;
50 klass = object_class_by_name(type);
51 if (!klass) {
52 error_setg(errp, "invalid object type: %s", type);
53 return NULL;
56 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
57 error_setg(errp, "object type '%s' isn't supported by object-add",
58 type);
59 return NULL;
62 if (object_class_is_abstract(klass)) {
63 error_setg(errp, "object type '%s' is abstract", type);
64 return NULL;
67 assert(qdict);
68 obj = object_new(type);
69 if (!visit_start_struct(v, NULL, NULL, 0, &local_err)) {
70 goto out;
72 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
73 if (!object_property_set(obj, e->key, v, &local_err)) {
74 break;
77 if (!local_err) {
78 visit_check_struct(v, &local_err);
80 visit_end_struct(v, NULL);
81 if (local_err) {
82 goto out;
85 if (id != NULL) {
86 object_property_add_child(object_get_objects_root(),
87 id, obj);
90 if (!user_creatable_complete(USER_CREATABLE(obj), &local_err)) {
91 if (id != NULL) {
92 object_property_del(object_get_objects_root(), id);
94 goto out;
96 out:
97 if (local_err) {
98 error_propagate(errp, local_err);
99 object_unref(obj);
100 return NULL;
102 return obj;
105 bool user_creatable_add_dict(QDict *qdict, bool keyval, Error **errp)
107 Visitor *v;
108 Object *obj;
109 g_autofree char *type = NULL;
110 g_autofree char *id = NULL;
112 type = g_strdup(qdict_get_try_str(qdict, "qom-type"));
113 if (!type) {
114 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
115 return false;
117 qdict_del(qdict, "qom-type");
119 id = g_strdup(qdict_get_try_str(qdict, "id"));
120 if (!id) {
121 error_setg(errp, QERR_MISSING_PARAMETER, "id");
122 return false;
124 qdict_del(qdict, "id");
126 if (keyval) {
127 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
128 } else {
129 v = qobject_input_visitor_new(QOBJECT(qdict));
131 obj = user_creatable_add_type(type, id, qdict, v, errp);
132 visit_free(v);
133 object_unref(obj);
134 return !!obj;
137 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
139 Visitor *v;
140 QDict *pdict;
141 Object *obj;
142 const char *id = qemu_opts_id(opts);
143 char *type = qemu_opt_get_del(opts, "qom-type");
145 if (!type) {
146 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
147 return NULL;
149 if (!id) {
150 error_setg(errp, QERR_MISSING_PARAMETER, "id");
151 qemu_opt_set(opts, "qom-type", type, &error_abort);
152 g_free(type);
153 return NULL;
156 qemu_opts_set_id(opts, NULL);
157 pdict = qemu_opts_to_qdict(opts, NULL);
159 v = opts_visitor_new(opts);
160 obj = user_creatable_add_type(type, id, pdict, v, errp);
161 visit_free(v);
163 qemu_opts_set_id(opts, (char *) id);
164 qemu_opt_set(opts, "qom-type", type, &error_abort);
165 g_free(type);
166 qobject_unref(pdict);
167 return obj;
171 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
173 bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
174 Object *obj = NULL;
175 const char *type;
177 type = qemu_opt_get(opts, "qom-type");
178 if (type && type_opt_predicate &&
179 !type_opt_predicate(type, opts)) {
180 return 0;
183 obj = user_creatable_add_opts(opts, errp);
184 if (!obj) {
185 return -1;
187 object_unref(obj);
188 return 0;
191 char *object_property_help(const char *name, const char *type,
192 QObject *defval, const char *description)
194 GString *str = g_string_new(NULL);
196 g_string_append_printf(str, " %s=<%s>", name, type);
197 if (description || defval) {
198 if (str->len < 24) {
199 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
201 g_string_append(str, " - ");
203 if (description) {
204 g_string_append(str, description);
206 if (defval) {
207 g_autofree char *def_json = qstring_free(qobject_to_json(defval), TRUE);
208 g_string_append_printf(str, " (default: %s)", def_json);
211 return g_string_free(str, false);
214 bool user_creatable_print_help(const char *type, QemuOpts *opts)
216 ObjectClass *klass;
218 if (is_help_option(type)) {
219 GSList *l, *list;
221 printf("List of user creatable objects:\n");
222 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
223 for (l = list; l != NULL; l = l->next) {
224 ObjectClass *oc = OBJECT_CLASS(l->data);
225 printf(" %s\n", object_class_get_name(oc));
227 g_slist_free(list);
228 return true;
231 klass = object_class_by_name(type);
232 if (klass && qemu_opt_has_help_opt(opts)) {
233 ObjectPropertyIterator iter;
234 ObjectProperty *prop;
235 GPtrArray *array = g_ptr_array_new();
236 int i;
238 object_class_property_iter_init(&iter, klass);
239 while ((prop = object_property_iter_next(&iter))) {
240 if (!prop->set) {
241 continue;
244 g_ptr_array_add(array,
245 object_property_help(prop->name, prop->type,
246 prop->defval, prop->description));
248 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
249 if (array->len > 0) {
250 printf("%s options:\n", type);
251 } else {
252 printf("There are no options for %s.\n", type);
254 for (i = 0; i < array->len; i++) {
255 printf("%s\n", (char *)array->pdata[i]);
257 g_ptr_array_set_free_func(array, g_free);
258 g_ptr_array_free(array, true);
259 return true;
262 return false;
265 bool user_creatable_del(const char *id, Error **errp)
267 Object *container;
268 Object *obj;
270 container = object_get_objects_root();
271 obj = object_resolve_path_component(container, id);
272 if (!obj) {
273 error_setg(errp, "object '%s' not found", id);
274 return false;
277 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
278 error_setg(errp, "object '%s' is in use, can not be deleted", id);
279 return false;
283 * if object was defined on the command-line, remove its corresponding
284 * option group entry
286 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
287 id));
289 object_unparent(obj);
290 return true;
293 void user_creatable_cleanup(void)
295 object_unparent(object_get_objects_root());
298 static void register_types(void)
300 static const TypeInfo uc_interface_info = {
301 .name = TYPE_USER_CREATABLE,
302 .parent = TYPE_INTERFACE,
303 .class_size = sizeof(UserCreatableClass),
306 type_register_static(&uc_interface_info);
309 type_init(register_types)