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