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