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