Makefile: Remove generated headers on clean
[qemu/ar7.git] / qerror.c
blob9a75d063012a050d3570249e839cdf08d5d7484e
1 /*
2 * QError Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
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.
13 #include "monitor.h"
14 #include "qjson.h"
15 #include "qerror.h"
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject *obj);
20 static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
25 /**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
29 * %(KEY)
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
33 * Example:
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
40 * "running out of foo: %(foo)%%"
42 * Please keep the entries in alphabetical order.
43 * Use "sed -n '/^static.*qerror_table\[\]/,/^};/s/QERR_/&/gp' qerror.c | sort -c"
44 * to check.
46 static const QErrorStringTable qerror_table[] = {
48 .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
49 .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
52 .error_fmt = QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
53 .desc = "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
56 .error_fmt = QERR_BUS_NOT_FOUND,
57 .desc = "Bus '%(bus)' not found",
60 .error_fmt = QERR_BUS_NO_HOTPLUG,
61 .desc = "Bus '%(bus)' does not support hotplugging",
64 .error_fmt = QERR_COMMAND_NOT_FOUND,
65 .desc = "The command %(name) has not been found",
68 .error_fmt = QERR_COMMAND_DISABLED,
69 .desc = "The command %(name) has been disabled for this instance",
72 .error_fmt = QERR_DEVICE_ENCRYPTED,
73 .desc = "Device '%(device)' is encrypted",
76 .error_fmt = QERR_DEVICE_INIT_FAILED,
77 .desc = "Device '%(device)' could not be initialized",
80 .error_fmt = QERR_DEVICE_IN_USE,
81 .desc = "Device '%(device)' is in use",
84 .error_fmt = QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
85 .desc = "Migration is disabled when using feature '%(feature)' in device '%(device)'",
88 .error_fmt = QERR_DEVICE_LOCKED,
89 .desc = "Device '%(device)' is locked",
92 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
93 .desc = "Device '%(device)' has multiple child busses",
96 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
97 .desc = "Device '%(device)' has not been activated",
100 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
101 .desc = "Device '%(device)' is not encrypted",
104 .error_fmt = QERR_DEVICE_NOT_FOUND,
105 .desc = "Device '%(device)' not found",
108 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
109 .desc = "Device '%(device)' is not removable",
112 .error_fmt = QERR_DEVICE_NO_BUS,
113 .desc = "Device '%(device)' has no child bus",
116 .error_fmt = QERR_DEVICE_NO_HOTPLUG,
117 .desc = "Device '%(device)' does not support hotplugging",
120 .error_fmt = QERR_DUPLICATE_ID,
121 .desc = "Duplicate ID '%(id)' for %(object)",
124 .error_fmt = QERR_FD_NOT_FOUND,
125 .desc = "File descriptor named '%(name)' not found",
128 .error_fmt = QERR_FD_NOT_SUPPLIED,
129 .desc = "No file descriptor supplied via SCM_RIGHTS",
132 .error_fmt = QERR_FEATURE_DISABLED,
133 .desc = "The feature '%(name)' is not enabled",
136 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
137 .desc = "Invalid block format '%(name)'",
140 .error_fmt = QERR_INVALID_PARAMETER,
141 .desc = "Invalid parameter '%(name)'",
144 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
145 .desc = "Invalid parameter type, expected: %(expected)",
148 .error_fmt = QERR_INVALID_PARAMETER_VALUE,
149 .desc = "Parameter '%(name)' expects %(expected)",
152 .error_fmt = QERR_INVALID_PASSWORD,
153 .desc = "Password incorrect",
156 .error_fmt = QERR_IO_ERROR,
157 .desc = "An IO error has occurred",
160 .error_fmt = QERR_JSON_PARSING,
161 .desc = "Invalid JSON syntax",
164 .error_fmt = QERR_JSON_PARSE_ERROR,
165 .desc = "JSON parse error, %(message)",
169 .error_fmt = QERR_KVM_MISSING_CAP,
170 .desc = "Using KVM without %(capability), %(feature) unavailable",
173 .error_fmt = QERR_MIGRATION_EXPECTED,
174 .desc = "An incoming migration is expected before this command can be executed",
177 .error_fmt = QERR_MISSING_PARAMETER,
178 .desc = "Parameter '%(name)' is missing",
181 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
182 .desc = "No '%(bus)' bus found for device '%(device)'",
185 .error_fmt = QERR_OPEN_FILE_FAILED,
186 .desc = "Could not open '%(filename)'",
189 .error_fmt = QERR_PERMISSION_DENIED,
190 .desc = "Insufficient permission to perform this operation",
193 .error_fmt = QERR_PROPERTY_NOT_FOUND,
194 .desc = "Property '%(device).%(property)' not found",
197 .error_fmt = QERR_PROPERTY_VALUE_BAD,
198 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
201 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
202 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
205 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
206 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
209 .error_fmt = QERR_PROPERTY_VALUE_OUT_OF_RANGE,
210 .desc = "Property '%(device).%(property)' doesn't take "
211 "value %(value) (minimum: %(min), maximum: %(max)'",
214 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
215 .desc = "Expected '%(expected)' in QMP input",
218 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
219 .desc = "QMP input object member '%(member)' expects '%(expected)'",
222 .error_fmt = QERR_QMP_EXTRA_MEMBER,
223 .desc = "QMP input object member '%(member)' is unexpected",
226 .error_fmt = QERR_RESET_REQUIRED,
227 .desc = "Resetting the Virtual Machine is required",
230 .error_fmt = QERR_SET_PASSWD_FAILED,
231 .desc = "Could not set password",
234 .error_fmt = QERR_ADD_CLIENT_FAILED,
235 .desc = "Could not add client",
238 .error_fmt = QERR_TOO_MANY_FILES,
239 .desc = "Too many open files",
242 .error_fmt = QERR_UNDEFINED_ERROR,
243 .desc = "An undefined error has occurred",
246 .error_fmt = QERR_UNSUPPORTED,
247 .desc = "this feature or command is not currently supported",
250 .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
251 .desc = "'%(device)' uses a %(format) feature which is not "
252 "supported by this qemu version: %(feature)",
255 .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
256 .desc = "Migration is disabled when VirtFS export path '%(path)' "
257 "is mounted in the guest using mount_tag '%(tag)'",
260 .error_fmt = QERR_VNC_SERVER_FAILED,
261 .desc = "Could not start VNC server on %(target)",
264 .error_fmt = QERR_QGA_LOGGING_FAILED,
265 .desc = "Guest agent failed to log non-optional log statement",
268 .error_fmt = QERR_QGA_COMMAND_FAILED,
269 .desc = "Guest agent command failed, error was '%(message)'",
272 .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
273 .desc = "Invalid parameter combination",
279 * qerror_new(): Create a new QError
281 * Return strong reference.
283 QError *qerror_new(void)
285 QError *qerr;
287 qerr = g_malloc0(sizeof(*qerr));
288 QOBJECT_INIT(qerr, &qerror_type);
290 return qerr;
293 static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr,
294 const char *fmt, ...)
296 va_list ap;
298 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
299 fprintf(stderr, "qerror: -> ");
301 va_start(ap, fmt);
302 vfprintf(stderr, fmt, ap);
303 va_end(ap);
305 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
306 abort();
309 static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
310 const char *fmt, va_list *va)
312 QObject *obj;
314 obj = qobject_from_jsonv(fmt, va);
315 if (!obj) {
316 qerror_abort(qerr, "invalid format '%s'", fmt);
318 if (qobject_type(obj) != QTYPE_QDICT) {
319 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
322 qerr->error = qobject_to_qdict(obj);
324 obj = qdict_get(qerr->error, "class");
325 if (!obj) {
326 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
328 if (qobject_type(obj) != QTYPE_QSTRING) {
329 qerror_abort(qerr, "'class' key value should be a QString");
332 obj = qdict_get(qerr->error, "data");
333 if (!obj) {
334 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
336 if (qobject_type(obj) != QTYPE_QDICT) {
337 qerror_abort(qerr, "'data' key value should be a QDICT");
341 static void qerror_set_desc(QError *qerr, const char *fmt)
343 int i;
345 // FIXME: inefficient loop
347 for (i = 0; qerror_table[i].error_fmt; i++) {
348 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
349 qerr->entry = &qerror_table[i];
350 return;
354 qerror_abort(qerr, "error format '%s' not found", fmt);
358 * qerror_from_info(): Create a new QError from error information
360 * The information consists of:
362 * - file the file name of where the error occurred
363 * - linenr the line number of where the error occurred
364 * - func the function name of where the error occurred
365 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
366 * 'data'
367 * - va va_list of all arguments specified by fmt
369 * Return strong reference.
371 QError *qerror_from_info(const char *file, int linenr, const char *func,
372 const char *fmt, va_list *va)
374 QError *qerr;
376 qerr = qerror_new();
377 loc_save(&qerr->loc);
378 qerr->linenr = linenr;
379 qerr->file = file;
380 qerr->func = func;
382 if (!fmt) {
383 qerror_abort(qerr, "QDict not specified");
386 qerror_set_data(qerr, fmt, va);
387 qerror_set_desc(qerr, fmt);
389 return qerr;
392 static void parse_error(const QErrorStringTable *entry, int c)
394 fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
395 abort();
398 static const char *append_field(QDict *error, QString *outstr,
399 const QErrorStringTable *entry,
400 const char *start)
402 QObject *obj;
403 QDict *qdict;
404 QString *key_qs;
405 const char *end, *key;
407 if (*start != '%')
408 parse_error(entry, '%');
409 start++;
410 if (*start != '(')
411 parse_error(entry, '(');
412 start++;
414 end = strchr(start, ')');
415 if (!end)
416 parse_error(entry, ')');
418 key_qs = qstring_from_substr(start, 0, end - start - 1);
419 key = qstring_get_str(key_qs);
421 qdict = qobject_to_qdict(qdict_get(error, "data"));
422 obj = qdict_get(qdict, key);
423 if (!obj) {
424 abort();
427 switch (qobject_type(obj)) {
428 case QTYPE_QSTRING:
429 qstring_append(outstr, qdict_get_str(qdict, key));
430 break;
431 case QTYPE_QINT:
432 qstring_append_int(outstr, qdict_get_int(qdict, key));
433 break;
434 default:
435 abort();
438 QDECREF(key_qs);
439 return ++end;
442 static QString *qerror_format_desc(QDict *error,
443 const QErrorStringTable *entry)
445 QString *qstring;
446 const char *p;
448 assert(entry != NULL);
450 qstring = qstring_new();
452 for (p = entry->desc; *p != '\0';) {
453 if (*p != '%') {
454 qstring_append_chr(qstring, *p++);
455 } else if (*(p + 1) == '%') {
456 qstring_append_chr(qstring, '%');
457 p += 2;
458 } else {
459 p = append_field(error, qstring, entry, p);
463 return qstring;
466 QString *qerror_format(const char *fmt, QDict *error)
468 const QErrorStringTable *entry = NULL;
469 int i;
471 for (i = 0; qerror_table[i].error_fmt; i++) {
472 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
473 entry = &qerror_table[i];
474 break;
478 return qerror_format_desc(error, entry);
482 * qerror_human(): Format QError data into human-readable string.
484 QString *qerror_human(const QError *qerror)
486 return qerror_format_desc(qerror->error, qerror->entry);
490 * qerror_print(): Print QError data
492 * This function will print the member 'desc' of the specified QError object,
493 * it uses error_report() for this, so that the output is routed to the right
494 * place (ie. stderr or Monitor's device).
496 void qerror_print(QError *qerror)
498 QString *qstring = qerror_human(qerror);
499 loc_push_restore(&qerror->loc);
500 error_report("%s", qstring_get_str(qstring));
501 loc_pop(&qerror->loc);
502 QDECREF(qstring);
505 void qerror_report_internal(const char *file, int linenr, const char *func,
506 const char *fmt, ...)
508 va_list va;
509 QError *qerror;
511 va_start(va, fmt);
512 qerror = qerror_from_info(file, linenr, func, fmt, &va);
513 va_end(va);
515 if (monitor_cur_is_qmp()) {
516 monitor_set_error(cur_mon, qerror);
517 } else {
518 qerror_print(qerror);
519 QDECREF(qerror);
523 /* Evil... */
524 struct Error
526 QDict *obj;
527 const char *fmt;
528 char *msg;
531 void qerror_report_err(Error *err)
533 QError *qerr;
534 int i;
536 qerr = qerror_new();
537 loc_save(&qerr->loc);
538 QINCREF(err->obj);
539 qerr->error = err->obj;
541 for (i = 0; qerror_table[i].error_fmt; i++) {
542 if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
543 qerr->entry = &qerror_table[i];
544 break;
548 if (monitor_cur_is_qmp()) {
549 monitor_set_error(cur_mon, qerr);
550 } else {
551 qerror_print(qerr);
552 QDECREF(qerr);
557 * qobject_to_qerror(): Convert a QObject into a QError
559 QError *qobject_to_qerror(const QObject *obj)
561 if (qobject_type(obj) != QTYPE_QERROR) {
562 return NULL;
565 return container_of(obj, QError, base);
569 * qerror_destroy_obj(): Free all memory allocated by a QError
571 static void qerror_destroy_obj(QObject *obj)
573 QError *qerr;
575 assert(obj != NULL);
576 qerr = qobject_to_qerror(obj);
578 QDECREF(qerr->error);
579 g_free(qerr);