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