error: Drop extra messages after qemu_opts_set() and qemu_opts_parse()
[qemu/aliguori-queue.git] / qerror.c
blob97e8d4ab936c47d6d14112e5ab93cc0f8a7d86b2
1 /*
2 * QError: QEMU Error data-type.
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_BUS_NOT_FOUND,
53 .desc = "Bus '%(bus)' not found",
56 .error_fmt = QERR_BUS_NO_HOTPLUG,
57 .desc = "Bus '%(bus)' does not support hotplugging",
60 .error_fmt = QERR_COMMAND_NOT_FOUND,
61 .desc = "The command %(name) has not been found",
64 .error_fmt = QERR_DEVICE_ENCRYPTED,
65 .desc = "Device '%(device)' is encrypted",
68 .error_fmt = QERR_DEVICE_INIT_FAILED,
69 .desc = "Device '%(device)' could not be initialized",
72 .error_fmt = QERR_DEVICE_LOCKED,
73 .desc = "Device '%(device)' is locked",
76 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
77 .desc = "Device '%(device)' has multiple child busses",
80 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
81 .desc = "Device '%(device)' has not been activated by the guest",
84 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
85 .desc = "Device '%(device)' is not encrypted",
88 .error_fmt = QERR_DEVICE_NOT_FOUND,
89 .desc = "Device '%(device)' not found",
92 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
93 .desc = "Device '%(device)' is not removable",
96 .error_fmt = QERR_DEVICE_NO_BUS,
97 .desc = "Device '%(device)' has no child bus",
100 .error_fmt = QERR_DUPLICATE_ID,
101 .desc = "Duplicate ID '%(id)' for %(object)",
104 .error_fmt = QERR_FD_NOT_FOUND,
105 .desc = "File descriptor named '%(name)' not found",
108 .error_fmt = QERR_FD_NOT_SUPPLIED,
109 .desc = "No file descriptor supplied via SCM_RIGHTS",
112 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
113 .desc = "Invalid block format '%(name)'",
116 .error_fmt = QERR_INVALID_PARAMETER,
117 .desc = "Invalid parameter '%(name)'",
120 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
121 .desc = "Invalid parameter type, expected: %(expected)",
124 .error_fmt = QERR_INVALID_PARAMETER_VALUE,
125 .desc = "Parameter '%(name)' expects %(expected)",
128 .error_fmt = QERR_INVALID_PASSWORD,
129 .desc = "Password incorrect",
132 .error_fmt = QERR_JSON_PARSING,
133 .desc = "Invalid JSON syntax",
136 .error_fmt = QERR_KVM_MISSING_CAP,
137 .desc = "Using KVM without %(capability), %(feature) unavailable",
140 .error_fmt = QERR_MISSING_PARAMETER,
141 .desc = "Parameter '%(name)' is missing",
144 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
145 .desc = "No '%(bus)' bus found for device '%(device)'",
148 .error_fmt = QERR_OPEN_FILE_FAILED,
149 .desc = "Could not open '%(filename)'",
152 .error_fmt = QERR_PROPERTY_NOT_FOUND,
153 .desc = "Property '%(device).%(property)' not found",
156 .error_fmt = QERR_PROPERTY_VALUE_BAD,
157 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
160 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
161 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
164 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
165 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
168 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
169 .desc = "Bad QMP input object",
172 .error_fmt = QERR_SET_PASSWD_FAILED,
173 .desc = "Could not set password",
176 .error_fmt = QERR_TOO_MANY_FILES,
177 .desc = "Too many open files",
180 .error_fmt = QERR_UNDEFINED_ERROR,
181 .desc = "An undefined error has ocurred",
184 .error_fmt = QERR_VNC_SERVER_FAILED,
185 .desc = "Could not start VNC server on %(target)",
191 * qerror_new(): Create a new QError
193 * Return strong reference.
195 QError *qerror_new(void)
197 QError *qerr;
199 qerr = qemu_mallocz(sizeof(*qerr));
200 QOBJECT_INIT(qerr, &qerror_type);
202 return qerr;
205 static void qerror_abort(const QError *qerr, const char *fmt, ...)
207 va_list ap;
209 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
210 fprintf(stderr, "qerror: -> ");
212 va_start(ap, fmt);
213 vfprintf(stderr, fmt, ap);
214 va_end(ap);
216 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
217 abort();
220 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
222 QObject *obj;
224 obj = qobject_from_jsonv(fmt, va);
225 if (!obj) {
226 qerror_abort(qerr, "invalid format '%s'", fmt);
228 if (qobject_type(obj) != QTYPE_QDICT) {
229 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
232 qerr->error = qobject_to_qdict(obj);
234 obj = qdict_get(qerr->error, "class");
235 if (!obj) {
236 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
238 if (qobject_type(obj) != QTYPE_QSTRING) {
239 qerror_abort(qerr, "'class' key value should be a QString");
242 obj = qdict_get(qerr->error, "data");
243 if (!obj) {
244 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
246 if (qobject_type(obj) != QTYPE_QDICT) {
247 qerror_abort(qerr, "'data' key value should be a QDICT");
251 static void qerror_set_desc(QError *qerr, const char *fmt)
253 int i;
255 // FIXME: inefficient loop
257 for (i = 0; qerror_table[i].error_fmt; i++) {
258 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
259 qerr->entry = &qerror_table[i];
260 return;
264 qerror_abort(qerr, "error format '%s' not found", fmt);
268 * qerror_from_info(): Create a new QError from error information
270 * The information consists of:
272 * - file the file name of where the error occurred
273 * - linenr the line number of where the error occurred
274 * - func the function name of where the error occurred
275 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
276 * 'data'
277 * - va va_list of all arguments specified by fmt
279 * Return strong reference.
281 QError *qerror_from_info(const char *file, int linenr, const char *func,
282 const char *fmt, va_list *va)
284 QError *qerr;
286 qerr = qerror_new();
287 loc_save(&qerr->loc);
288 qerr->linenr = linenr;
289 qerr->file = file;
290 qerr->func = func;
292 if (!fmt) {
293 qerror_abort(qerr, "QDict not specified");
296 qerror_set_data(qerr, fmt, va);
297 qerror_set_desc(qerr, fmt);
299 return qerr;
302 static void parse_error(const QError *qerror, int c)
304 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
307 static const char *append_field(QString *outstr, const QError *qerror,
308 const char *start)
310 QObject *obj;
311 QDict *qdict;
312 QString *key_qs;
313 const char *end, *key;
315 if (*start != '%')
316 parse_error(qerror, '%');
317 start++;
318 if (*start != '(')
319 parse_error(qerror, '(');
320 start++;
322 end = strchr(start, ')');
323 if (!end)
324 parse_error(qerror, ')');
326 key_qs = qstring_from_substr(start, 0, end - start - 1);
327 key = qstring_get_str(key_qs);
329 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
330 obj = qdict_get(qdict, key);
331 if (!obj) {
332 qerror_abort(qerror, "key '%s' not found in QDict", key);
335 switch (qobject_type(obj)) {
336 case QTYPE_QSTRING:
337 qstring_append(outstr, qdict_get_str(qdict, key));
338 break;
339 case QTYPE_QINT:
340 qstring_append_int(outstr, qdict_get_int(qdict, key));
341 break;
342 default:
343 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
346 QDECREF(key_qs);
347 return ++end;
351 * qerror_human(): Format QError data into human-readable string.
353 * Formats according to member 'desc' of the specified QError object.
355 QString *qerror_human(const QError *qerror)
357 const char *p;
358 QString *qstring;
360 assert(qerror->entry != NULL);
362 qstring = qstring_new();
364 for (p = qerror->entry->desc; *p != '\0';) {
365 if (*p != '%') {
366 qstring_append_chr(qstring, *p++);
367 } else if (*(p + 1) == '%') {
368 qstring_append_chr(qstring, '%');
369 p += 2;
370 } else {
371 p = append_field(qstring, qerror, p);
375 return qstring;
379 * qerror_print(): Print QError data
381 * This function will print the member 'desc' of the specified QError object,
382 * it uses error_report() for this, so that the output is routed to the right
383 * place (ie. stderr or Monitor's device).
385 void qerror_print(QError *qerror)
387 QString *qstring = qerror_human(qerror);
388 loc_push_restore(&qerror->loc);
389 error_report("%s", qstring_get_str(qstring));
390 loc_pop(&qerror->loc);
391 QDECREF(qstring);
394 void qerror_report_internal(const char *file, int linenr, const char *func,
395 const char *fmt, ...)
397 va_list va;
398 QError *qerror;
400 va_start(va, fmt);
401 qerror = qerror_from_info(file, linenr, func, fmt, &va);
402 va_end(va);
404 if (monitor_cur_is_qmp()) {
405 monitor_set_error(cur_mon, qerror);
406 } else {
407 qerror_print(qerror);
408 QDECREF(qerror);
413 * qobject_to_qerror(): Convert a QObject into a QError
415 QError *qobject_to_qerror(const QObject *obj)
417 if (qobject_type(obj) != QTYPE_QERROR) {
418 return NULL;
421 return container_of(obj, QError, base);
425 * qerror_destroy_obj(): Free all memory allocated by a QError
427 static void qerror_destroy_obj(QObject *obj)
429 QError *qerr;
431 assert(obj != NULL);
432 qerr = qobject_to_qerror(obj);
434 QDECREF(qerr->error);
435 qemu_free(qerr);