]> git.proxmox.com Git - qemu.git/blob - qerror.c
Merge remote-tracking branch 'qemu-kvm/memory/mutators' into staging
[qemu.git] / qerror.c
1 /*
2 * QError Module
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
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.
11 */
12
13 #include "monitor.h"
14 #include "qjson.h"
15 #include "qerror.h"
16 #include "qemu-common.h"
17
18 static void qerror_destroy_obj(QObject *obj);
19
20 static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
23 };
24
25 /**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
28 *
29 * %(KEY)
30 *
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
32 *
33 * Example:
34 *
35 * "foo error on device: %(device) slot: %(slot_nr)"
36 *
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
39 *
40 * "running out of foo: %(foo)%%"
41 *
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.
45 */
46 static const QErrorStringTable qerror_table[] = {
47 {
48 .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
49 .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
50 },
51 {
52 .error_fmt = QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
53 .desc = "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
54 },
55 {
56 .error_fmt = QERR_BUS_NOT_FOUND,
57 .desc = "Bus '%(bus)' not found",
58 },
59 {
60 .error_fmt = QERR_BUS_NO_HOTPLUG,
61 .desc = "Bus '%(bus)' does not support hotplugging",
62 },
63 {
64 .error_fmt = QERR_COMMAND_NOT_FOUND,
65 .desc = "The command %(name) has not been found",
66 },
67 {
68 .error_fmt = QERR_COMMAND_DISABLED,
69 .desc = "The command %(name) has been disabled for this instance",
70 },
71 {
72 .error_fmt = QERR_DEVICE_ENCRYPTED,
73 .desc = "Device '%(device)' is encrypted",
74 },
75 {
76 .error_fmt = QERR_DEVICE_INIT_FAILED,
77 .desc = "Device '%(device)' could not be initialized",
78 },
79 {
80 .error_fmt = QERR_DEVICE_IN_USE,
81 .desc = "Device '%(device)' is in use",
82 },
83 {
84 .error_fmt = QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
85 .desc = "Migration is disabled when using feature '%(feature)' in device '%(device)'",
86 },
87 {
88 .error_fmt = QERR_DEVICE_LOCKED,
89 .desc = "Device '%(device)' is locked",
90 },
91 {
92 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
93 .desc = "Device '%(device)' has multiple child busses",
94 },
95 {
96 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
97 .desc = "Device '%(device)' has not been activated",
98 },
99 {
100 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
101 .desc = "Device '%(device)' is not encrypted",
102 },
103 {
104 .error_fmt = QERR_DEVICE_NOT_FOUND,
105 .desc = "Device '%(device)' not found",
106 },
107 {
108 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
109 .desc = "Device '%(device)' is not removable",
110 },
111 {
112 .error_fmt = QERR_DEVICE_NO_BUS,
113 .desc = "Device '%(device)' has no child bus",
114 },
115 {
116 .error_fmt = QERR_DEVICE_NO_HOTPLUG,
117 .desc = "Device '%(device)' does not support hotplugging",
118 },
119 {
120 .error_fmt = QERR_DUPLICATE_ID,
121 .desc = "Duplicate ID '%(id)' for %(object)",
122 },
123 {
124 .error_fmt = QERR_FD_NOT_FOUND,
125 .desc = "File descriptor named '%(name)' not found",
126 },
127 {
128 .error_fmt = QERR_FD_NOT_SUPPLIED,
129 .desc = "No file descriptor supplied via SCM_RIGHTS",
130 },
131 {
132 .error_fmt = QERR_FEATURE_DISABLED,
133 .desc = "The feature '%(name)' is not enabled",
134 },
135 {
136 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
137 .desc = "Invalid block format '%(name)'",
138 },
139 {
140 .error_fmt = QERR_INVALID_PARAMETER,
141 .desc = "Invalid parameter '%(name)'",
142 },
143 {
144 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
145 .desc = "Invalid parameter type, expected: %(expected)",
146 },
147 {
148 .error_fmt = QERR_INVALID_PARAMETER_VALUE,
149 .desc = "Parameter '%(name)' expects %(expected)",
150 },
151 {
152 .error_fmt = QERR_INVALID_PASSWORD,
153 .desc = "Password incorrect",
154 },
155 {
156 .error_fmt = QERR_IO_ERROR,
157 .desc = "An IO error has occurred",
158 },
159 {
160 .error_fmt = QERR_JSON_PARSING,
161 .desc = "Invalid JSON syntax",
162 },
163 {
164 .error_fmt = QERR_JSON_PARSE_ERROR,
165 .desc = "JSON parse error, %(message)",
166
167 },
168 {
169 .error_fmt = QERR_KVM_MISSING_CAP,
170 .desc = "Using KVM without %(capability), %(feature) unavailable",
171 },
172 {
173 .error_fmt = QERR_MIGRATION_EXPECTED,
174 .desc = "An incoming migration is expected before this command can be executed",
175 },
176 {
177 .error_fmt = QERR_MISSING_PARAMETER,
178 .desc = "Parameter '%(name)' is missing",
179 },
180 {
181 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
182 .desc = "No '%(bus)' bus found for device '%(device)'",
183 },
184 {
185 .error_fmt = QERR_OPEN_FILE_FAILED,
186 .desc = "Could not open '%(filename)'",
187 },
188 {
189 .error_fmt = QERR_PERMISSION_DENIED,
190 .desc = "Insufficient permission to perform this operation",
191 },
192 {
193 .error_fmt = QERR_PROPERTY_NOT_FOUND,
194 .desc = "Property '%(device).%(property)' not found",
195 },
196 {
197 .error_fmt = QERR_PROPERTY_VALUE_BAD,
198 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
199 },
200 {
201 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
202 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
203 },
204 {
205 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
206 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
207 },
208 {
209 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
210 .desc = "Expected '%(expected)' in QMP input",
211 },
212 {
213 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
214 .desc = "QMP input object member '%(member)' expects '%(expected)'",
215 },
216 {
217 .error_fmt = QERR_QMP_EXTRA_MEMBER,
218 .desc = "QMP input object member '%(member)' is unexpected",
219 },
220 {
221 .error_fmt = QERR_RESET_REQUIRED,
222 .desc = "Resetting the Virtual Machine is required",
223 },
224 {
225 .error_fmt = QERR_SET_PASSWD_FAILED,
226 .desc = "Could not set password",
227 },
228 {
229 .error_fmt = QERR_ADD_CLIENT_FAILED,
230 .desc = "Could not add client",
231 },
232 {
233 .error_fmt = QERR_TOO_MANY_FILES,
234 .desc = "Too many open files",
235 },
236 {
237 .error_fmt = QERR_UNDEFINED_ERROR,
238 .desc = "An undefined error has occurred",
239 },
240 {
241 .error_fmt = QERR_UNSUPPORTED,
242 .desc = "this feature or command is not currently supported",
243 },
244 {
245 .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
246 .desc = "'%(device)' uses a %(format) feature which is not "
247 "supported by this qemu version: %(feature)",
248 },
249 {
250 .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
251 .desc = "Migration is disabled when VirtFS export path '%(path)' "
252 "is mounted in the guest using mount_tag '%(tag)'",
253 },
254 {
255 .error_fmt = QERR_VNC_SERVER_FAILED,
256 .desc = "Could not start VNC server on %(target)",
257 },
258 {
259 .error_fmt = QERR_QGA_LOGGING_FAILED,
260 .desc = "Guest agent failed to log non-optional log statement",
261 },
262 {
263 .error_fmt = QERR_QGA_COMMAND_FAILED,
264 .desc = "Guest agent command failed, error was '%(message)'",
265 },
266 {
267 .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
268 .desc = "Invalid parameter combination",
269 },
270 {}
271 };
272
273 /**
274 * qerror_new(): Create a new QError
275 *
276 * Return strong reference.
277 */
278 QError *qerror_new(void)
279 {
280 QError *qerr;
281
282 qerr = g_malloc0(sizeof(*qerr));
283 QOBJECT_INIT(qerr, &qerror_type);
284
285 return qerr;
286 }
287
288 static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr,
289 const char *fmt, ...)
290 {
291 va_list ap;
292
293 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
294 fprintf(stderr, "qerror: -> ");
295
296 va_start(ap, fmt);
297 vfprintf(stderr, fmt, ap);
298 va_end(ap);
299
300 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
301 abort();
302 }
303
304 static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
305 const char *fmt, va_list *va)
306 {
307 QObject *obj;
308
309 obj = qobject_from_jsonv(fmt, va);
310 if (!obj) {
311 qerror_abort(qerr, "invalid format '%s'", fmt);
312 }
313 if (qobject_type(obj) != QTYPE_QDICT) {
314 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
315 }
316
317 qerr->error = qobject_to_qdict(obj);
318
319 obj = qdict_get(qerr->error, "class");
320 if (!obj) {
321 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
322 }
323 if (qobject_type(obj) != QTYPE_QSTRING) {
324 qerror_abort(qerr, "'class' key value should be a QString");
325 }
326
327 obj = qdict_get(qerr->error, "data");
328 if (!obj) {
329 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
330 }
331 if (qobject_type(obj) != QTYPE_QDICT) {
332 qerror_abort(qerr, "'data' key value should be a QDICT");
333 }
334 }
335
336 static void qerror_set_desc(QError *qerr, const char *fmt)
337 {
338 int i;
339
340 // FIXME: inefficient loop
341
342 for (i = 0; qerror_table[i].error_fmt; i++) {
343 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
344 qerr->entry = &qerror_table[i];
345 return;
346 }
347 }
348
349 qerror_abort(qerr, "error format '%s' not found", fmt);
350 }
351
352 /**
353 * qerror_from_info(): Create a new QError from error information
354 *
355 * The information consists of:
356 *
357 * - file the file name of where the error occurred
358 * - linenr the line number of where the error occurred
359 * - func the function name of where the error occurred
360 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
361 * 'data'
362 * - va va_list of all arguments specified by fmt
363 *
364 * Return strong reference.
365 */
366 QError *qerror_from_info(const char *file, int linenr, const char *func,
367 const char *fmt, va_list *va)
368 {
369 QError *qerr;
370
371 qerr = qerror_new();
372 loc_save(&qerr->loc);
373 qerr->linenr = linenr;
374 qerr->file = file;
375 qerr->func = func;
376
377 if (!fmt) {
378 qerror_abort(qerr, "QDict not specified");
379 }
380
381 qerror_set_data(qerr, fmt, va);
382 qerror_set_desc(qerr, fmt);
383
384 return qerr;
385 }
386
387 static void parse_error(const QErrorStringTable *entry, int c)
388 {
389 fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
390 abort();
391 }
392
393 static const char *append_field(QDict *error, QString *outstr,
394 const QErrorStringTable *entry,
395 const char *start)
396 {
397 QObject *obj;
398 QDict *qdict;
399 QString *key_qs;
400 const char *end, *key;
401
402 if (*start != '%')
403 parse_error(entry, '%');
404 start++;
405 if (*start != '(')
406 parse_error(entry, '(');
407 start++;
408
409 end = strchr(start, ')');
410 if (!end)
411 parse_error(entry, ')');
412
413 key_qs = qstring_from_substr(start, 0, end - start - 1);
414 key = qstring_get_str(key_qs);
415
416 qdict = qobject_to_qdict(qdict_get(error, "data"));
417 obj = qdict_get(qdict, key);
418 if (!obj) {
419 abort();
420 }
421
422 switch (qobject_type(obj)) {
423 case QTYPE_QSTRING:
424 qstring_append(outstr, qdict_get_str(qdict, key));
425 break;
426 case QTYPE_QINT:
427 qstring_append_int(outstr, qdict_get_int(qdict, key));
428 break;
429 default:
430 abort();
431 }
432
433 QDECREF(key_qs);
434 return ++end;
435 }
436
437 static QString *qerror_format_desc(QDict *error,
438 const QErrorStringTable *entry)
439 {
440 QString *qstring;
441 const char *p;
442
443 assert(entry != NULL);
444
445 qstring = qstring_new();
446
447 for (p = entry->desc; *p != '\0';) {
448 if (*p != '%') {
449 qstring_append_chr(qstring, *p++);
450 } else if (*(p + 1) == '%') {
451 qstring_append_chr(qstring, '%');
452 p += 2;
453 } else {
454 p = append_field(error, qstring, entry, p);
455 }
456 }
457
458 return qstring;
459 }
460
461 QString *qerror_format(const char *fmt, QDict *error)
462 {
463 const QErrorStringTable *entry = NULL;
464 int i;
465
466 for (i = 0; qerror_table[i].error_fmt; i++) {
467 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
468 entry = &qerror_table[i];
469 break;
470 }
471 }
472
473 return qerror_format_desc(error, entry);
474 }
475
476 /**
477 * qerror_human(): Format QError data into human-readable string.
478 */
479 QString *qerror_human(const QError *qerror)
480 {
481 return qerror_format_desc(qerror->error, qerror->entry);
482 }
483
484 /**
485 * qerror_print(): Print QError data
486 *
487 * This function will print the member 'desc' of the specified QError object,
488 * it uses error_report() for this, so that the output is routed to the right
489 * place (ie. stderr or Monitor's device).
490 */
491 void qerror_print(QError *qerror)
492 {
493 QString *qstring = qerror_human(qerror);
494 loc_push_restore(&qerror->loc);
495 error_report("%s", qstring_get_str(qstring));
496 loc_pop(&qerror->loc);
497 QDECREF(qstring);
498 }
499
500 void qerror_report_internal(const char *file, int linenr, const char *func,
501 const char *fmt, ...)
502 {
503 va_list va;
504 QError *qerror;
505
506 va_start(va, fmt);
507 qerror = qerror_from_info(file, linenr, func, fmt, &va);
508 va_end(va);
509
510 if (monitor_cur_is_qmp()) {
511 monitor_set_error(cur_mon, qerror);
512 } else {
513 qerror_print(qerror);
514 QDECREF(qerror);
515 }
516 }
517
518 /* Evil... */
519 struct Error
520 {
521 QDict *obj;
522 const char *fmt;
523 char *msg;
524 };
525
526 void qerror_report_err(Error *err)
527 {
528 QError *qerr;
529 int i;
530
531 qerr = qerror_new();
532 loc_save(&qerr->loc);
533 QINCREF(err->obj);
534 qerr->error = err->obj;
535
536 for (i = 0; qerror_table[i].error_fmt; i++) {
537 if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
538 qerr->entry = &qerror_table[i];
539 break;
540 }
541 }
542
543 if (monitor_cur_is_qmp()) {
544 monitor_set_error(cur_mon, qerr);
545 } else {
546 qerror_print(qerr);
547 QDECREF(qerr);
548 }
549 }
550
551 /**
552 * qobject_to_qerror(): Convert a QObject into a QError
553 */
554 QError *qobject_to_qerror(const QObject *obj)
555 {
556 if (qobject_type(obj) != QTYPE_QERROR) {
557 return NULL;
558 }
559
560 return container_of(obj, QError, base);
561 }
562
563 /**
564 * qerror_destroy_obj(): Free all memory allocated by a QError
565 */
566 static void qerror_destroy_obj(QObject *obj)
567 {
568 QError *qerr;
569
570 assert(obj != NULL);
571 qerr = qobject_to_qerror(obj);
572
573 QDECREF(qerr->error);
574 g_free(qerr);
575 }