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.
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject
*obj
);
20 static const QType qerror_type
= {
22 .destroy
= qerror_destroy_obj
,
26 * The 'desc' parameter is a printf-like string, the format of the format
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
40 * "running out of foo: %(foo)%%"
42 * Please keep the entries in alphabetical order.
43 * Use scripts/check-qerror.sh to check.
45 static const QErrorStringTable qerror_table
[] = {
47 .error_fmt
= QERR_ADD_CLIENT_FAILED
,
48 .desc
= "Could not add client",
51 .error_fmt
= QERR_BAD_BUS_FOR_DEVICE
,
52 .desc
= "Device '%(device)' can't go on a %(bad_bus_type) bus",
55 .error_fmt
= QERR_BASE_NOT_FOUND
,
56 .desc
= "Base '%(base)' not found",
59 .error_fmt
= QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
60 .desc
= "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
63 .error_fmt
= QERR_BUS_NO_HOTPLUG
,
64 .desc
= "Bus '%(bus)' does not support hotplugging",
67 .error_fmt
= QERR_BUS_NOT_FOUND
,
68 .desc
= "Bus '%(bus)' not found",
71 .error_fmt
= QERR_COMMAND_DISABLED
,
72 .desc
= "The command %(name) has been disabled for this instance",
75 .error_fmt
= QERR_COMMAND_NOT_FOUND
,
76 .desc
= "The command %(name) has not been found",
79 .error_fmt
= QERR_DEVICE_ENCRYPTED
,
80 .desc
= "Device '%(device)' is encrypted",
83 .error_fmt
= QERR_DEVICE_FEATURE_BLOCKS_MIGRATION
,
84 .desc
= "Migration is disabled when using feature '%(feature)' in device '%(device)'",
87 .error_fmt
= QERR_DEVICE_HAS_NO_MEDIUM
,
88 .desc
= "Device '%(device)' has no medium",
91 .error_fmt
= QERR_DEVICE_INIT_FAILED
,
92 .desc
= "Device '%(device)' could not be initialized",
95 .error_fmt
= QERR_DEVICE_IN_USE
,
96 .desc
= "Device '%(device)' is in use",
99 .error_fmt
= QERR_DEVICE_IS_READ_ONLY
,
100 .desc
= "Device '%(device)' is read only",
103 .error_fmt
= QERR_DEVICE_LOCKED
,
104 .desc
= "Device '%(device)' is locked",
107 .error_fmt
= QERR_DEVICE_MULTIPLE_BUSSES
,
108 .desc
= "Device '%(device)' has multiple child busses",
111 .error_fmt
= QERR_DEVICE_NO_BUS
,
112 .desc
= "Device '%(device)' has no child bus",
115 .error_fmt
= QERR_DEVICE_NO_HOTPLUG
,
116 .desc
= "Device '%(device)' does not support hotplugging",
119 .error_fmt
= QERR_DEVICE_NOT_ACTIVE
,
120 .desc
= "Device '%(device)' has not been activated",
123 .error_fmt
= QERR_DEVICE_NOT_ENCRYPTED
,
124 .desc
= "Device '%(device)' is not encrypted",
127 .error_fmt
= QERR_DEVICE_NOT_FOUND
,
128 .desc
= "Device '%(device)' not found",
131 .error_fmt
= QERR_DEVICE_NOT_REMOVABLE
,
132 .desc
= "Device '%(device)' is not removable",
135 .error_fmt
= QERR_DUPLICATE_ID
,
136 .desc
= "Duplicate ID '%(id)' for %(object)",
139 .error_fmt
= QERR_FD_NOT_FOUND
,
140 .desc
= "File descriptor named '%(name)' not found",
143 .error_fmt
= QERR_FD_NOT_SUPPLIED
,
144 .desc
= "No file descriptor supplied via SCM_RIGHTS",
147 .error_fmt
= QERR_FEATURE_DISABLED
,
148 .desc
= "The feature '%(name)' is not enabled",
151 .error_fmt
= QERR_INVALID_BLOCK_FORMAT
,
152 .desc
= "Invalid block format '%(name)'",
155 .error_fmt
= QERR_INVALID_PARAMETER
,
156 .desc
= "Invalid parameter '%(name)'",
159 .error_fmt
= QERR_INVALID_PARAMETER_COMBINATION
,
160 .desc
= "Invalid parameter combination",
163 .error_fmt
= QERR_INVALID_PARAMETER_TYPE
,
164 .desc
= "Invalid parameter type for '%(name)', expected: %(expected)",
167 .error_fmt
= QERR_INVALID_PARAMETER_VALUE
,
168 .desc
= "Parameter '%(name)' expects %(expected)",
171 .error_fmt
= QERR_INVALID_PASSWORD
,
172 .desc
= "Password incorrect",
175 .error_fmt
= QERR_IO_ERROR
,
176 .desc
= "An IO error has occurred",
179 .error_fmt
= QERR_JSON_PARSE_ERROR
,
180 .desc
= "JSON parse error, %(message)",
184 .error_fmt
= QERR_JSON_PARSING
,
185 .desc
= "Invalid JSON syntax",
188 .error_fmt
= QERR_KVM_MISSING_CAP
,
189 .desc
= "Using KVM without %(capability), %(feature) unavailable",
192 .error_fmt
= QERR_MIGRATION_EXPECTED
,
193 .desc
= "An incoming migration is expected before this command can be executed",
196 .error_fmt
= QERR_MISSING_PARAMETER
,
197 .desc
= "Parameter '%(name)' is missing",
200 .error_fmt
= QERR_NO_BUS_FOR_DEVICE
,
201 .desc
= "No '%(bus)' bus found for device '%(device)'",
204 .error_fmt
= QERR_NOT_SUPPORTED
,
205 .desc
= "Not supported",
208 .error_fmt
= QERR_OPEN_FILE_FAILED
,
209 .desc
= "Could not open '%(filename)'",
212 .error_fmt
= QERR_PERMISSION_DENIED
,
213 .desc
= "Insufficient permission to perform this operation",
216 .error_fmt
= QERR_PROPERTY_NOT_FOUND
,
217 .desc
= "Property '%(device).%(property)' not found",
220 .error_fmt
= QERR_PROPERTY_VALUE_BAD
,
221 .desc
= "Property '%(device).%(property)' doesn't take value '%(value)'",
224 .error_fmt
= QERR_PROPERTY_VALUE_IN_USE
,
225 .desc
= "Property '%(device).%(property)' can't take value '%(value)', it's in use",
228 .error_fmt
= QERR_PROPERTY_VALUE_NOT_FOUND
,
229 .desc
= "Property '%(device).%(property)' can't find value '%(value)'",
232 .error_fmt
= QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
233 .desc
= "Property '%(device).%(property)' doesn't take "
234 "value %(value) (minimum: %(min), maximum: %(max)'",
237 .error_fmt
= QERR_QGA_COMMAND_FAILED
,
238 .desc
= "Guest agent command failed, error was '%(message)'",
241 .error_fmt
= QERR_QGA_LOGGING_FAILED
,
242 .desc
= "Guest agent failed to log non-optional log statement",
245 .error_fmt
= QERR_QMP_BAD_INPUT_OBJECT
,
246 .desc
= "Expected '%(expected)' in QMP input",
249 .error_fmt
= QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
250 .desc
= "QMP input object member '%(member)' expects '%(expected)'",
253 .error_fmt
= QERR_QMP_EXTRA_MEMBER
,
254 .desc
= "QMP input object member '%(member)' is unexpected",
257 .error_fmt
= QERR_RESET_REQUIRED
,
258 .desc
= "Resetting the Virtual Machine is required",
261 .error_fmt
= QERR_SET_PASSWD_FAILED
,
262 .desc
= "Could not set password",
265 .error_fmt
= QERR_TOO_MANY_FILES
,
266 .desc
= "Too many open files",
269 .error_fmt
= QERR_UNDEFINED_ERROR
,
270 .desc
= "An undefined error has occurred",
273 .error_fmt
= QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
274 .desc
= "'%(device)' uses a %(format) feature which is not "
275 "supported by this qemu version: %(feature)",
278 .error_fmt
= QERR_UNSUPPORTED
,
279 .desc
= "this feature or command is not currently supported",
282 .error_fmt
= QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION
,
283 .desc
= "Migration is disabled when VirtFS export path '%(path)' "
284 "is mounted in the guest using mount_tag '%(tag)'",
287 .error_fmt
= QERR_VNC_SERVER_FAILED
,
288 .desc
= "Could not start VNC server on %(target)",
294 * qerror_new(): Create a new QError
296 * Return strong reference.
298 QError
*qerror_new(void)
302 qerr
= g_malloc0(sizeof(*qerr
));
303 QOBJECT_INIT(qerr
, &qerror_type
);
308 static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError
*qerr
,
309 const char *fmt
, ...)
313 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
314 fprintf(stderr
, "qerror: -> ");
317 vfprintf(stderr
, fmt
, ap
);
320 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
324 static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError
*qerr
,
325 const char *fmt
, va_list *va
)
329 obj
= qobject_from_jsonv(fmt
, va
);
331 qerror_abort(qerr
, "invalid format '%s'", fmt
);
333 if (qobject_type(obj
) != QTYPE_QDICT
) {
334 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
337 qerr
->error
= qobject_to_qdict(obj
);
339 obj
= qdict_get(qerr
->error
, "class");
341 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
343 if (qobject_type(obj
) != QTYPE_QSTRING
) {
344 qerror_abort(qerr
, "'class' key value should be a QString");
347 obj
= qdict_get(qerr
->error
, "data");
349 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
351 if (qobject_type(obj
) != QTYPE_QDICT
) {
352 qerror_abort(qerr
, "'data' key value should be a QDICT");
356 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
360 // FIXME: inefficient loop
362 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
363 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
364 qerr
->entry
= &qerror_table
[i
];
369 qerror_abort(qerr
, "error format '%s' not found", fmt
);
373 * qerror_from_info(): Create a new QError from error information
375 * The information consists of:
377 * - file the file name of where the error occurred
378 * - linenr the line number of where the error occurred
379 * - func the function name of where the error occurred
380 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
382 * - va va_list of all arguments specified by fmt
384 * Return strong reference.
386 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
387 const char *fmt
, va_list *va
)
392 loc_save(&qerr
->loc
);
393 qerr
->linenr
= linenr
;
398 qerror_abort(qerr
, "QDict not specified");
401 qerror_set_data(qerr
, fmt
, va
);
402 qerror_set_desc(qerr
, fmt
);
407 static void parse_error(const QErrorStringTable
*entry
, int c
)
409 fprintf(stderr
, "expected '%c' in '%s'", c
, entry
->desc
);
413 static const char *append_field(QDict
*error
, QString
*outstr
,
414 const QErrorStringTable
*entry
,
420 const char *end
, *key
;
423 parse_error(entry
, '%');
426 parse_error(entry
, '(');
429 end
= strchr(start
, ')');
431 parse_error(entry
, ')');
433 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
434 key
= qstring_get_str(key_qs
);
436 qdict
= qobject_to_qdict(qdict_get(error
, "data"));
437 obj
= qdict_get(qdict
, key
);
442 switch (qobject_type(obj
)) {
444 qstring_append(outstr
, qdict_get_str(qdict
, key
));
447 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
457 static QString
*qerror_format_desc(QDict
*error
,
458 const QErrorStringTable
*entry
)
463 assert(entry
!= NULL
);
465 qstring
= qstring_new();
467 for (p
= entry
->desc
; *p
!= '\0';) {
469 qstring_append_chr(qstring
, *p
++);
470 } else if (*(p
+ 1) == '%') {
471 qstring_append_chr(qstring
, '%');
474 p
= append_field(error
, qstring
, entry
, p
);
481 QString
*qerror_format(const char *fmt
, QDict
*error
)
483 const QErrorStringTable
*entry
= NULL
;
486 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
487 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
488 entry
= &qerror_table
[i
];
493 return qerror_format_desc(error
, entry
);
497 * qerror_human(): Format QError data into human-readable string.
499 QString
*qerror_human(const QError
*qerror
)
501 return qerror_format_desc(qerror
->error
, qerror
->entry
);
505 * qerror_print(): Print QError data
507 * This function will print the member 'desc' of the specified QError object,
508 * it uses error_report() for this, so that the output is routed to the right
509 * place (ie. stderr or Monitor's device).
511 void qerror_print(QError
*qerror
)
513 QString
*qstring
= qerror_human(qerror
);
514 loc_push_restore(&qerror
->loc
);
515 error_report("%s", qstring_get_str(qstring
));
516 loc_pop(&qerror
->loc
);
520 void qerror_report_internal(const char *file
, int linenr
, const char *func
,
521 const char *fmt
, ...)
527 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
530 if (monitor_cur_is_qmp()) {
531 monitor_set_error(cur_mon
, qerror
);
533 qerror_print(qerror
);
546 void qerror_report_err(Error
*err
)
552 loc_save(&qerr
->loc
);
554 qerr
->error
= err
->obj
;
556 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
557 if (strcmp(qerror_table
[i
].error_fmt
, err
->fmt
) == 0) {
558 qerr
->entry
= &qerror_table
[i
];
563 if (monitor_cur_is_qmp()) {
564 monitor_set_error(cur_mon
, qerr
);
572 * qobject_to_qerror(): Convert a QObject into a QError
574 QError
*qobject_to_qerror(const QObject
*obj
)
576 if (qobject_type(obj
) != QTYPE_QERROR
) {
580 return container_of(obj
, QError
, base
);
584 * qerror_destroy_obj(): Free all memory allocated by a QError
586 static void qerror_destroy_obj(QObject
*obj
)
591 qerr
= qobject_to_qerror(obj
);
593 QDECREF(qerr
->error
);