2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu-common.h"
16 static void qerror_destroy_obj(QObject
*obj
);
18 static const QType qerror_type
= {
20 .destroy
= qerror_destroy_obj
,
24 * The 'desc' parameter is a printf-like string, the format of the format
29 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
33 * "foo error on device: %(device) slot: %(slot_nr)"
35 * A single percent sign can be printed if followed by a second one,
38 * "running out of foo: %(foo)%%"
40 static const QErrorStringTable qerror_table
[] = {
42 .error_fmt
= QERR_BAD_BUS_FOR_DEVICE
,
43 .desc
= "Device '%(device)' can't go on a %(bad_bus_type) bus",
46 .error_fmt
= QERR_BUS_NOT_FOUND
,
47 .desc
= "Bus '%(bus)' not found",
50 .error_fmt
= QERR_BUS_NO_HOTPLUG
,
51 .desc
= "Bus '%(bus)' does not support hotplugging",
54 .error_fmt
= QERR_COMMAND_NOT_FOUND
,
55 .desc
= "The command %(name) has not been found",
58 .error_fmt
= QERR_DEVICE_ENCRYPTED
,
59 .desc
= "Device '%(device)' is encrypted",
62 .error_fmt
= QERR_DEVICE_INIT_FAILED
,
63 .desc
= "Device '%(device)' could not be initialized",
66 .error_fmt
= QERR_DEVICE_NOT_ENCRYPTED
,
67 .desc
= "Device '%(device)' is not encrypted",
70 .error_fmt
= QERR_DEVICE_LOCKED
,
71 .desc
= "Device '%(device)' is locked",
74 .error_fmt
= QERR_DEVICE_MULTIPLE_BUSSES
,
75 .desc
= "Device '%(device)' has multiple child busses",
78 .error_fmt
= QERR_DEVICE_NOT_ACTIVE
,
79 .desc
= "Device '%(device)' has not been activated by the guest",
82 .error_fmt
= QERR_DEVICE_NOT_FOUND
,
83 .desc
= "Device '%(device)' not found",
86 .error_fmt
= QERR_DEVICE_NOT_REMOVABLE
,
87 .desc
= "Device '%(device)' is not removable",
90 .error_fmt
= QERR_DEVICE_NO_BUS
,
91 .desc
= "Device '%(device)' has no child bus",
94 .error_fmt
= QERR_FD_NOT_FOUND
,
95 .desc
= "File descriptor named '%(name)' not found",
98 .error_fmt
= QERR_FD_NOT_SUPPLIED
,
99 .desc
= "No file descriptor supplied via SCM_RIGHTS",
102 .error_fmt
= QERR_INVALID_BLOCK_FORMAT
,
103 .desc
= "Invalid block format '%(name)'",
106 .error_fmt
= QERR_INVALID_PARAMETER
,
107 .desc
= "Invalid parameter '%(name)'",
110 .error_fmt
= QERR_INVALID_PARAMETER_TYPE
,
111 .desc
= "Invalid parameter type, expected: %(expected)",
114 .error_fmt
= QERR_INVALID_PASSWORD
,
115 .desc
= "Password incorrect",
118 .error_fmt
= QERR_JSON_PARSING
,
119 .desc
= "Invalid JSON syntax",
122 .error_fmt
= QERR_KVM_MISSING_CAP
,
123 .desc
= "Using KVM without %(capability), %(feature) unavailable",
126 .error_fmt
= QERR_MISSING_PARAMETER
,
127 .desc
= "Parameter '%(name)' is missing",
130 .error_fmt
= QERR_NO_BUS_FOR_DEVICE
,
131 .desc
= "No '%(bus)' bus found for device '%(device)'",
134 .error_fmt
= QERR_OPEN_FILE_FAILED
,
135 .desc
= "Could not open '%(filename)'",
138 .error_fmt
= QERR_PROPERTY_NOT_FOUND
,
139 .desc
= "Property '%(device).%(property)' not found",
142 .error_fmt
= QERR_PROPERTY_VALUE_BAD
,
143 .desc
= "Property '%(device).%(property)' doesn't take value '%(value)'",
146 .error_fmt
= QERR_PROPERTY_VALUE_IN_USE
,
147 .desc
= "Property '%(device).%(property)' can't take value '%(value)', it's in use",
150 .error_fmt
= QERR_PROPERTY_VALUE_NOT_FOUND
,
151 .desc
= "Property '%(device).%(property)' can't find value '%(value)'",
154 .error_fmt
= QERR_QMP_BAD_INPUT_OBJECT
,
155 .desc
= "Bad QMP input object",
158 .error_fmt
= QERR_SET_PASSWD_FAILED
,
159 .desc
= "Could not set password",
162 .error_fmt
= QERR_TOO_MANY_FILES
,
163 .desc
= "Too many open files",
166 .error_fmt
= QERR_UNDEFINED_ERROR
,
167 .desc
= "An undefined error has ocurred",
170 .error_fmt
= QERR_VNC_SERVER_FAILED
,
171 .desc
= "Could not start VNC server on %(target)",
177 * qerror_new(): Create a new QError
179 * Return strong reference.
181 QError
*qerror_new(void)
185 qerr
= qemu_mallocz(sizeof(*qerr
));
186 QOBJECT_INIT(qerr
, &qerror_type
);
191 static void qerror_abort(const QError
*qerr
, const char *fmt
, ...)
195 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
196 fprintf(stderr
, "qerror: -> ");
199 vfprintf(stderr
, fmt
, ap
);
202 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
206 static void qerror_set_data(QError
*qerr
, const char *fmt
, va_list *va
)
210 obj
= qobject_from_jsonv(fmt
, va
);
212 qerror_abort(qerr
, "invalid format '%s'", fmt
);
214 if (qobject_type(obj
) != QTYPE_QDICT
) {
215 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
218 qerr
->error
= qobject_to_qdict(obj
);
220 obj
= qdict_get(qerr
->error
, "class");
222 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
224 if (qobject_type(obj
) != QTYPE_QSTRING
) {
225 qerror_abort(qerr
, "'class' key value should be a QString");
228 obj
= qdict_get(qerr
->error
, "data");
230 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
232 if (qobject_type(obj
) != QTYPE_QDICT
) {
233 qerror_abort(qerr
, "'data' key value should be a QDICT");
237 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
241 // FIXME: inefficient loop
243 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
244 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
245 qerr
->entry
= &qerror_table
[i
];
250 qerror_abort(qerr
, "error format '%s' not found", fmt
);
254 * qerror_from_info(): Create a new QError from error information
256 * The information consists of:
258 * - file the file name of where the error occurred
259 * - linenr the line number of where the error occurred
260 * - func the function name of where the error occurred
261 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
263 * - va va_list of all arguments specified by fmt
265 * Return strong reference.
267 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
268 const char *fmt
, va_list *va
)
273 loc_save(&qerr
->loc
);
274 qerr
->linenr
= linenr
;
279 qerror_abort(qerr
, "QDict not specified");
282 qerror_set_data(qerr
, fmt
, va
);
283 qerror_set_desc(qerr
, fmt
);
288 static void parse_error(const QError
*qerror
, int c
)
290 qerror_abort(qerror
, "expected '%c' in '%s'", c
, qerror
->entry
->desc
);
293 static const char *append_field(QString
*outstr
, const QError
*qerror
,
299 const char *end
, *key
;
302 parse_error(qerror
, '%');
305 parse_error(qerror
, '(');
308 end
= strchr(start
, ')');
310 parse_error(qerror
, ')');
312 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
313 key
= qstring_get_str(key_qs
);
315 qdict
= qobject_to_qdict(qdict_get(qerror
->error
, "data"));
316 obj
= qdict_get(qdict
, key
);
318 qerror_abort(qerror
, "key '%s' not found in QDict", key
);
321 switch (qobject_type(obj
)) {
323 qstring_append(outstr
, qdict_get_str(qdict
, key
));
326 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
329 qerror_abort(qerror
, "invalid type '%c'", qobject_type(obj
));
337 * qerror_human(): Format QError data into human-readable string.
339 * Formats according to member 'desc' of the specified QError object.
341 QString
*qerror_human(const QError
*qerror
)
346 assert(qerror
->entry
!= NULL
);
348 qstring
= qstring_new();
350 for (p
= qerror
->entry
->desc
; *p
!= '\0';) {
352 qstring_append_chr(qstring
, *p
++);
353 } else if (*(p
+ 1) == '%') {
354 qstring_append_chr(qstring
, '%');
357 p
= append_field(qstring
, qerror
, p
);
365 * qerror_print(): Print QError data
367 * This function will print the member 'desc' of the specified QError object,
368 * it uses error_report() for this, so that the output is routed to the right
369 * place (ie. stderr or Monitor's device).
371 void qerror_print(QError
*qerror
)
373 QString
*qstring
= qerror_human(qerror
);
374 loc_push_restore(&qerror
->loc
);
375 error_report("%s", qstring_get_str(qstring
));
376 loc_pop(&qerror
->loc
);
381 * qobject_to_qerror(): Convert a QObject into a QError
383 QError
*qobject_to_qerror(const QObject
*obj
)
385 if (qobject_type(obj
) != QTYPE_QERROR
) {
389 return container_of(obj
, QError
, base
);
393 * qerror_destroy_obj(): Free all memory allocated by a QError
395 static void qerror_destroy_obj(QObject
*obj
)
400 qerr
= qobject_to_qerror(obj
);
402 QDECREF(qerr
->error
);