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