]> git.proxmox.com Git - qemu.git/blob - qerror.c
Merge remote-tracking branch 'stefanha/trivial-patches-next' 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_PROPERTY_NOT_FOUND,
190 .desc = "Property '%(device).%(property)' not found",
191 },
192 {
193 .error_fmt = QERR_PROPERTY_VALUE_BAD,
194 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
195 },
196 {
197 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
198 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
199 },
200 {
201 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
202 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
203 },
204 {
205 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
206 .desc = "Expected '%(expected)' in QMP input",
207 },
208 {
209 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
210 .desc = "QMP input object member '%(member)' expects '%(expected)'",
211 },
212 {
213 .error_fmt = QERR_QMP_EXTRA_MEMBER,
214 .desc = "QMP input object member '%(member)' is unexpected",
215 },
216 {
217 .error_fmt = QERR_RESET_REQUIRED,
218 .desc = "Resetting the Virtual Machine is required",
219 },
220 {
221 .error_fmt = QERR_SET_PASSWD_FAILED,
222 .desc = "Could not set password",
223 },
224 {
225 .error_fmt = QERR_ADD_CLIENT_FAILED,
226 .desc = "Could not add client",
227 },
228 {
229 .error_fmt = QERR_TOO_MANY_FILES,
230 .desc = "Too many open files",
231 },
232 {
233 .error_fmt = QERR_UNDEFINED_ERROR,
234 .desc = "An undefined error has occurred",
235 },
236 {
237 .error_fmt = QERR_UNSUPPORTED,
238 .desc = "this feature or command is not currently supported",
239 },
240 {
241 .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
242 .desc = "'%(device)' uses a %(format) feature which is not "
243 "supported by this qemu version: %(feature)",
244 },
245 {
246 .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
247 .desc = "Migration is disabled when VirtFS export path '%(path)' "
248 "is mounted in the guest using mount_tag '%(tag)'",
249 },
250 {
251 .error_fmt = QERR_VNC_SERVER_FAILED,
252 .desc = "Could not start VNC server on %(target)",
253 },
254 {
255 .error_fmt = QERR_QGA_LOGGING_FAILED,
256 .desc = "Guest agent failed to log non-optional log statement",
257 },
258 {
259 .error_fmt = QERR_QGA_COMMAND_FAILED,
260 .desc = "Guest agent command failed, error was '%(message)'",
261 },
262 {
263 .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
264 .desc = "Invalid parameter combination",
265 },
266 {}
267 };
268
269 /**
270 * qerror_new(): Create a new QError
271 *
272 * Return strong reference.
273 */
274 QError *qerror_new(void)
275 {
276 QError *qerr;
277
278 qerr = g_malloc0(sizeof(*qerr));
279 QOBJECT_INIT(qerr, &qerror_type);
280
281 return qerr;
282 }
283
284 static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr,
285 const char *fmt, ...)
286 {
287 va_list ap;
288
289 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
290 fprintf(stderr, "qerror: -> ");
291
292 va_start(ap, fmt);
293 vfprintf(stderr, fmt, ap);
294 va_end(ap);
295
296 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
297 abort();
298 }
299
300 static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr,
301 const char *fmt, va_list *va)
302 {
303 QObject *obj;
304
305 obj = qobject_from_jsonv(fmt, va);
306 if (!obj) {
307 qerror_abort(qerr, "invalid format '%s'", fmt);
308 }
309 if (qobject_type(obj) != QTYPE_QDICT) {
310 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
311 }
312
313 qerr->error = qobject_to_qdict(obj);
314
315 obj = qdict_get(qerr->error, "class");
316 if (!obj) {
317 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
318 }
319 if (qobject_type(obj) != QTYPE_QSTRING) {
320 qerror_abort(qerr, "'class' key value should be a QString");
321 }
322
323 obj = qdict_get(qerr->error, "data");
324 if (!obj) {
325 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
326 }
327 if (qobject_type(obj) != QTYPE_QDICT) {
328 qerror_abort(qerr, "'data' key value should be a QDICT");
329 }
330 }
331
332 static void qerror_set_desc(QError *qerr, const char *fmt)
333 {
334 int i;
335
336 // FIXME: inefficient loop
337
338 for (i = 0; qerror_table[i].error_fmt; i++) {
339 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
340 qerr->entry = &qerror_table[i];
341 return;
342 }
343 }
344
345 qerror_abort(qerr, "error format '%s' not found", fmt);
346 }
347
348 /**
349 * qerror_from_info(): Create a new QError from error information
350 *
351 * The information consists of:
352 *
353 * - file the file name of where the error occurred
354 * - linenr the line number of where the error occurred
355 * - func the function name of where the error occurred
356 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
357 * 'data'
358 * - va va_list of all arguments specified by fmt
359 *
360 * Return strong reference.
361 */
362 QError *qerror_from_info(const char *file, int linenr, const char *func,
363 const char *fmt, va_list *va)
364 {
365 QError *qerr;
366
367 qerr = qerror_new();
368 loc_save(&qerr->loc);
369 qerr->linenr = linenr;
370 qerr->file = file;
371 qerr->func = func;
372
373 if (!fmt) {
374 qerror_abort(qerr, "QDict not specified");
375 }
376
377 qerror_set_data(qerr, fmt, va);
378 qerror_set_desc(qerr, fmt);
379
380 return qerr;
381 }
382
383 static void parse_error(const QErrorStringTable *entry, int c)
384 {
385 fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
386 abort();
387 }
388
389 static const char *append_field(QDict *error, QString *outstr,
390 const QErrorStringTable *entry,
391 const char *start)
392 {
393 QObject *obj;
394 QDict *qdict;
395 QString *key_qs;
396 const char *end, *key;
397
398 if (*start != '%')
399 parse_error(entry, '%');
400 start++;
401 if (*start != '(')
402 parse_error(entry, '(');
403 start++;
404
405 end = strchr(start, ')');
406 if (!end)
407 parse_error(entry, ')');
408
409 key_qs = qstring_from_substr(start, 0, end - start - 1);
410 key = qstring_get_str(key_qs);
411
412 qdict = qobject_to_qdict(qdict_get(error, "data"));
413 obj = qdict_get(qdict, key);
414 if (!obj) {
415 abort();
416 }
417
418 switch (qobject_type(obj)) {
419 case QTYPE_QSTRING:
420 qstring_append(outstr, qdict_get_str(qdict, key));
421 break;
422 case QTYPE_QINT:
423 qstring_append_int(outstr, qdict_get_int(qdict, key));
424 break;
425 default:
426 abort();
427 }
428
429 QDECREF(key_qs);
430 return ++end;
431 }
432
433 static QString *qerror_format_desc(QDict *error,
434 const QErrorStringTable *entry)
435 {
436 QString *qstring;
437 const char *p;
438
439 assert(entry != NULL);
440
441 qstring = qstring_new();
442
443 for (p = entry->desc; *p != '\0';) {
444 if (*p != '%') {
445 qstring_append_chr(qstring, *p++);
446 } else if (*(p + 1) == '%') {
447 qstring_append_chr(qstring, '%');
448 p += 2;
449 } else {
450 p = append_field(error, qstring, entry, p);
451 }
452 }
453
454 return qstring;
455 }
456
457 QString *qerror_format(const char *fmt, QDict *error)
458 {
459 const QErrorStringTable *entry = NULL;
460 int i;
461
462 for (i = 0; qerror_table[i].error_fmt; i++) {
463 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
464 entry = &qerror_table[i];
465 break;
466 }
467 }
468
469 return qerror_format_desc(error, entry);
470 }
471
472 /**
473 * qerror_human(): Format QError data into human-readable string.
474 */
475 QString *qerror_human(const QError *qerror)
476 {
477 return qerror_format_desc(qerror->error, qerror->entry);
478 }
479
480 /**
481 * qerror_print(): Print QError data
482 *
483 * This function will print the member 'desc' of the specified QError object,
484 * it uses error_report() for this, so that the output is routed to the right
485 * place (ie. stderr or Monitor's device).
486 */
487 void qerror_print(QError *qerror)
488 {
489 QString *qstring = qerror_human(qerror);
490 loc_push_restore(&qerror->loc);
491 error_report("%s", qstring_get_str(qstring));
492 loc_pop(&qerror->loc);
493 QDECREF(qstring);
494 }
495
496 void qerror_report_internal(const char *file, int linenr, const char *func,
497 const char *fmt, ...)
498 {
499 va_list va;
500 QError *qerror;
501
502 va_start(va, fmt);
503 qerror = qerror_from_info(file, linenr, func, fmt, &va);
504 va_end(va);
505
506 if (monitor_cur_is_qmp()) {
507 monitor_set_error(cur_mon, qerror);
508 } else {
509 qerror_print(qerror);
510 QDECREF(qerror);
511 }
512 }
513
514 /* Evil... */
515 struct Error
516 {
517 QDict *obj;
518 const char *fmt;
519 char *msg;
520 };
521
522 void qerror_report_err(Error *err)
523 {
524 QError *qerr;
525 int i;
526
527 qerr = qerror_new();
528 loc_save(&qerr->loc);
529 QINCREF(err->obj);
530 qerr->error = err->obj;
531
532 for (i = 0; qerror_table[i].error_fmt; i++) {
533 if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
534 qerr->entry = &qerror_table[i];
535 break;
536 }
537 }
538
539 if (monitor_cur_is_qmp()) {
540 monitor_set_error(cur_mon, qerr);
541 } else {
542 qerror_print(qerr);
543 QDECREF(qerr);
544 }
545 }
546
547 /**
548 * qobject_to_qerror(): Convert a QObject into a QError
549 */
550 QError *qobject_to_qerror(const QObject *obj)
551 {
552 if (qobject_type(obj) != QTYPE_QERROR) {
553 return NULL;
554 }
555
556 return container_of(obj, QError, base);
557 }
558
559 /**
560 * qerror_destroy_obj(): Free all memory allocated by a QError
561 */
562 static void qerror_destroy_obj(QObject *obj)
563 {
564 QError *qerr;
565
566 assert(obj != NULL);
567 qerr = qobject_to_qerror(obj);
568
569 QDECREF(qerr->error);
570 g_free(qerr);
571 }