Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/qom/qom-qmp-cmds.c
Line
Count
Source
1
/*
2
 * QMP commands related to QOM
3
 *
4
 * Copyright IBM, Corp. 2011
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 * Contributions after 2012-01-13 are licensed under the terms of the
13
 * GNU GPL, version 2 or (at your option) any later version.
14
 */
15
16
#include "qemu/osdep.h"
17
#include "block/qdict.h"
18
#include "hw/core/qdev.h"
19
#include "qapi/error.h"
20
#include "qapi/qapi-commands-qdev.h"
21
#include "qapi/qapi-commands-qom.h"
22
#include "qapi/qapi-visit-qom.h"
23
#include "qobject/qdict.h"
24
#include "qapi/qmp/qerror.h"
25
#include "qapi/qobject-input-visitor.h"
26
#include "qapi/qobject-output-visitor.h"
27
#include "qemu/cutils.h"
28
#include "qapi/qapi-type-info.h"
29
#include "qom/object_interfaces.h"
30
#include "qom/qom-qobject.h"
31
32
static ObjectPropertyInfo *qom_property_info(ObjectProperty *prop)
33
0
{
34
0
    ObjectPropertyInfo *info = g_new0(ObjectPropertyInfo, 1);
35
36
0
    info->name = g_strdup(prop->name);
37
0
    info->type = g_strdup(prop->type);
38
0
    info->description = g_strdup(prop->description);
39
0
    info->default_value = qobject_ref(prop->defval);
40
0
    if (prop->qapi_type) {
41
0
        assert(prop->qapi_type->masked_name);
42
0
        info->qapi_type = g_strdup(prop->qapi_type->masked_name);
43
0
    }
44
0
    return info;
45
0
}
46
47
static Object *qom_resolve_path(const char *path, Error **errp)
48
0
{
49
0
    bool ambiguous = false;
50
0
    Object *obj = object_resolve_path(path, &ambiguous);
51
52
0
    if (obj == NULL) {
53
0
        if (ambiguous) {
54
0
            error_setg(errp, "Path '%s' is ambiguous", path);
55
0
        } else {
56
0
            error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
57
0
                      "Device '%s' not found", path);
58
0
        }
59
0
    }
60
0
    return obj;
61
0
}
62
63
ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
64
0
{
65
0
    Object *obj;
66
0
    ObjectPropertyInfoList *props = NULL;
67
0
    ObjectProperty *prop;
68
0
    ObjectPropertyIterator iter;
69
70
0
    obj = qom_resolve_path(path, errp);
71
0
    if (obj == NULL) {
72
0
        return NULL;
73
0
    }
74
75
0
    object_property_iter_init(&iter, obj);
76
0
    while ((prop = object_property_iter_next(&iter))) {
77
0
        QAPI_LIST_PREPEND(props, qom_property_info(prop));
78
0
    }
79
80
0
    return props;
81
0
}
82
83
static void qom_list_add_property_value(Object *obj, ObjectProperty *prop,
84
                                        ObjectPropertyValueList **props)
85
0
{
86
0
    ObjectPropertyValue *item = g_new0(ObjectPropertyValue, 1);
87
88
0
    QAPI_LIST_PREPEND(*props, item);
89
90
0
    item->name = g_strdup(prop->name);
91
0
    item->type = g_strdup(prop->type);
92
0
    if (prop->qapi_type) {
93
0
        assert(prop->qapi_type->masked_name);
94
0
        item->qapi_type = g_strdup(prop->qapi_type->masked_name);
95
0
    }
96
0
    item->value = object_property_get_qobject(obj, prop->name, NULL);
97
0
}
98
99
static ObjectPropertyValueList *qom_get_property_value_list(const char *path,
100
                                                            Error **errp)
101
0
{
102
0
    Object *obj;
103
0
    ObjectProperty *prop;
104
0
    ObjectPropertyIterator iter;
105
0
    ObjectPropertyValueList *props = NULL;
106
107
0
    obj = qom_resolve_path(path, errp);
108
0
    if (obj == NULL) {
109
0
        return NULL;
110
0
    }
111
112
0
    object_property_iter_init(&iter, obj);
113
0
    while ((prop = object_property_iter_next(&iter))) {
114
0
        qom_list_add_property_value(obj, prop, &props);
115
0
    }
116
117
0
    return props;
118
0
}
119
120
ObjectPropertiesValuesList *qmp_qom_list_get(strList *paths, Error **errp)
121
0
{
122
0
    ObjectPropertiesValuesList *head = NULL, **tail = &head;
123
0
    strList *path;
124
125
0
    for (path = paths; path; path = path->next) {
126
0
        ObjectPropertiesValues *item = g_new0(ObjectPropertiesValues, 1);
127
128
0
        QAPI_LIST_APPEND(tail, item);
129
130
0
        item->properties = qom_get_property_value_list(path->value, errp);
131
0
        if (!item->properties) {
132
0
            qapi_free_ObjectPropertiesValuesList(head);
133
0
            return NULL;
134
0
        }
135
0
    }
136
137
0
    return head;
138
0
}
139
140
void qmp_qom_set(const char *path, const char *property, QObject *value,
141
                 Error **errp)
142
0
{
143
0
    Object *obj;
144
145
0
    obj = object_resolve_path(path, NULL);
146
0
    if (!obj) {
147
0
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
148
0
                  "Device '%s' not found", path);
149
0
        return;
150
0
    }
151
152
0
    object_property_set_qobject(obj, property, value, errp);
153
0
}
154
155
QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
156
0
{
157
0
    Object *obj;
158
159
0
    obj = object_resolve_path(path, NULL);
160
0
    if (!obj) {
161
0
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
162
0
                  "Device '%s' not found", path);
163
0
        return NULL;
164
0
    }
165
166
0
    return object_property_get_qobject(obj, property, errp);
167
0
}
168
169
typedef struct {
170
    ObjectTypeInfoList *list;
171
    bool has_secure;
172
    bool secure;
173
} ObjectTypeInfoData;
174
175
static void qom_list_types_tramp(ObjectClass *klass, void *opaque)
176
0
{
177
0
    ObjectTypeInfoData *data = opaque;
178
0
    ObjectTypeInfo *info;
179
0
    ObjectClass *parent = object_class_get_parent(klass);
180
0
    bool secure = object_class_is_secure(klass);
181
182
0
    if (data->has_secure &&
183
0
        data->secure != secure) {
184
0
        return;
185
0
    }
186
187
0
    info = g_malloc0(sizeof(*info));
188
0
    info->name = g_strdup(object_class_get_name(klass));
189
0
    info->has_abstract = info->abstract = object_class_is_abstract(klass);
190
    /*
191
     * Set has_secure such that we omit the 'secure' attribute
192
     * from the QMP response for insecure types
193
     */
194
0
    info->has_secure = info->secure = secure;
195
0
    if (parent) {
196
0
        info->parent = g_strdup(object_class_get_name(parent));
197
0
    }
198
199
0
    QAPI_LIST_PREPEND(data->list, info);
200
0
}
201
202
ObjectTypeInfoList *qmp_qom_list_types(const char *implements,
203
                                       bool has_abstract,
204
                                       bool abstract,
205
                                       bool has_secure,
206
                                       bool secure,
207
                                       Error **errp)
208
0
{
209
0
    ObjectTypeInfoData data = {
210
0
        .list = NULL,
211
0
        .has_secure = has_secure,
212
0
        .secure = secure,
213
0
    };
214
215
0
    module_load_qom_all();
216
0
    object_class_foreach(qom_list_types_tramp, implements, abstract, &data);
217
218
0
    return data.list;
219
0
}
220
221
ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
222
                                                Error **errp)
223
0
{
224
0
    ObjectClass *klass;
225
0
    Object *obj;
226
0
    ObjectProperty *prop;
227
0
    ObjectPropertyIterator iter;
228
0
    ObjectPropertyInfoList *prop_list = NULL;
229
230
0
    klass = module_object_class_by_name(typename);
231
0
    if (klass == NULL) {
232
0
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
233
0
                  "Device '%s' not found", typename);
234
0
        return NULL;
235
0
    }
236
237
0
    if (!object_class_dynamic_cast(klass, TYPE_DEVICE)
238
0
        || object_class_is_abstract(klass)) {
239
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
240
0
                   "a non-abstract device type");
241
0
        return NULL;
242
0
    }
243
244
0
    obj = object_new_with_class(klass);
245
246
0
    object_property_iter_init(&iter, obj);
247
0
    while ((prop = object_property_iter_next(&iter))) {
248
        /* Skip Object and DeviceState properties */
249
0
        if (strcmp(prop->name, "type") == 0 ||
250
0
            strcmp(prop->name, "realized") == 0 ||
251
0
            strcmp(prop->name, "hotpluggable") == 0 ||
252
0
            strcmp(prop->name, "hotplugged") == 0 ||
253
0
            strcmp(prop->name, "parent_bus") == 0) {
254
0
            continue;
255
0
        }
256
257
0
        QAPI_LIST_PREPEND(prop_list, qom_property_info(prop));
258
0
    }
259
260
0
    object_unref(obj);
261
262
0
    return prop_list;
263
0
}
264
265
ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
266
                                             Error **errp)
267
0
{
268
0
    ObjectClass *klass;
269
0
    Object *obj = NULL;
270
0
    ObjectProperty *prop;
271
0
    ObjectPropertyIterator iter;
272
0
    ObjectPropertyInfoList *prop_list = NULL;
273
274
0
    klass = module_object_class_by_name(typename);
275
0
    if (klass == NULL) {
276
0
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
277
0
                  "Class '%s' not found", typename);
278
0
        return NULL;
279
0
    }
280
281
0
    if (!object_class_dynamic_cast(klass, TYPE_OBJECT)) {
282
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
283
0
                   "a QOM type");
284
0
        return NULL;
285
0
    }
286
287
0
    if (object_class_is_abstract(klass)) {
288
0
        object_class_property_iter_init(&iter, klass);
289
0
    } else {
290
0
        obj = object_new(typename);
291
0
        object_property_iter_init(&iter, obj);
292
0
    }
293
0
    while ((prop = object_property_iter_next(&iter))) {
294
0
        QAPI_LIST_PREPEND(prop_list, qom_property_info(prop));
295
0
    }
296
297
0
    object_unref(obj);
298
299
0
    return prop_list;
300
0
}
301
302
void qmp_object_add(ObjectOptions *options, Error **errp)
303
0
{
304
0
    user_creatable_add_qapi(options, errp);
305
0
}
306
307
void qmp_object_del(const char *id, Error **errp)
308
0
{
309
0
    user_creatable_del(id, errp);
310
0
}