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