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