]>
Commit | Line | Data |
---|---|---|
9f9daf9a | 1 | /* |
41836a9f | 2 | * QError Module |
9f9daf9a LC |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
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 | ||
18 | static void qerror_destroy_obj(QObject *obj); | |
19 | ||
20 | static 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 | 45 | static 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 | }, |
8ff15d4a LC |
158 | { |
159 | .error_fmt = QERR_INVALID_OPTION_GROUP, | |
160 | .desc = "There is no option group '%(group)'", | |
161 | }, | |
7a046f5f MA |
162 | { |
163 | .error_fmt = QERR_INVALID_PARAMETER, | |
fc5469d8 | 164 | .desc = "Invalid parameter '%(name)'", |
7a046f5f | 165 | }, |
c1303596 SH |
166 | { |
167 | .error_fmt = QERR_INVALID_PARAMETER_COMBINATION, | |
168 | .desc = "Invalid parameter combination", | |
169 | }, | |
055f6122 | 170 | { |
e16a1812 | 171 | .error_fmt = QERR_INVALID_PARAMETER_TYPE, |
5eeee3fa | 172 | .desc = "Invalid parameter type for '%(name)', expected: %(expected)", |
055f6122 | 173 | }, |
985a3e52 MA |
174 | { |
175 | .error_fmt = QERR_INVALID_PARAMETER_VALUE, | |
176 | .desc = "Parameter '%(name)' expects %(expected)", | |
177 | }, | |
4b9d4683 | 178 | { |
e16a1812 | 179 | .error_fmt = QERR_INVALID_PASSWORD, |
fc5469d8 | 180 | .desc = "Password incorrect", |
4b9d4683 | 181 | }, |
58898873 LC |
182 | { |
183 | .error_fmt = QERR_IO_ERROR, | |
184 | .desc = "An IO error has occurred", | |
185 | }, | |
ac32c780 MR |
186 | { |
187 | .error_fmt = QERR_JSON_PARSE_ERROR, | |
188 | .desc = "JSON parse error, %(message)", | |
189 | ||
190 | }, | |
c1303596 SH |
191 | { |
192 | .error_fmt = QERR_JSON_PARSING, | |
193 | .desc = "Invalid JSON syntax", | |
194 | }, | |
82a60711 LC |
195 | { |
196 | .error_fmt = QERR_KVM_MISSING_CAP, | |
197 | .desc = "Using KVM without %(capability), %(feature) unavailable", | |
198 | }, | |
a4acc064 LC |
199 | { |
200 | .error_fmt = QERR_MIGRATION_ACTIVE, | |
201 | .desc = "There's a migration process in progress", | |
202 | }, | |
203 | { | |
204 | .error_fmt = QERR_MIGRATION_NOT_SUPPORTED, | |
205 | .desc = "State blocked by non-migratable device '%(device)'", | |
206 | }, | |
8e84865e AS |
207 | { |
208 | .error_fmt = QERR_MIGRATION_EXPECTED, | |
209 | .desc = "An incoming migration is expected before this command can be executed", | |
210 | }, | |
4b9d4683 LC |
211 | { |
212 | .error_fmt = QERR_MISSING_PARAMETER, | |
fc5469d8 MA |
213 | .desc = "Parameter '%(name)' is missing", |
214 | }, | |
fab5767f MA |
215 | { |
216 | .error_fmt = QERR_NO_BUS_FOR_DEVICE, | |
217 | .desc = "No '%(bus)' bus found for device '%(device)'", | |
218 | }, | |
12bd451f SH |
219 | { |
220 | .error_fmt = QERR_NOT_SUPPORTED, | |
221 | .desc = "Not supported", | |
222 | }, | |
fc5469d8 MA |
223 | { |
224 | .error_fmt = QERR_OPEN_FILE_FAILED, | |
225 | .desc = "Could not open '%(filename)'", | |
4b9d4683 | 226 | }, |
44677ded AL |
227 | { |
228 | .error_fmt = QERR_PERMISSION_DENIED, | |
229 | .desc = "Insufficient permission to perform this operation", | |
230 | }, | |
c58a35f8 MA |
231 | { |
232 | .error_fmt = QERR_PROPERTY_NOT_FOUND, | |
233 | .desc = "Property '%(device).%(property)' not found", | |
234 | }, | |
06b4a703 MA |
235 | { |
236 | .error_fmt = QERR_PROPERTY_VALUE_BAD, | |
237 | .desc = "Property '%(device).%(property)' doesn't take value '%(value)'", | |
238 | }, | |
9c5eff95 MA |
239 | { |
240 | .error_fmt = QERR_PROPERTY_VALUE_IN_USE, | |
241 | .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use", | |
242 | }, | |
84745d68 MA |
243 | { |
244 | .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND, | |
245 | .desc = "Property '%(device).%(property)' can't find value '%(value)'", | |
246 | }, | |
02fda01c SH |
247 | { |
248 | .error_fmt = QERR_PROPERTY_VALUE_NOT_POWER_OF_2, | |
249 | .desc = "Property '%(device).%(property)' doesn't take " | |
250 | "value '%(value)', it's not a power of 2", | |
251 | }, | |
6aced82c PB |
252 | { |
253 | .error_fmt = QERR_PROPERTY_VALUE_OUT_OF_RANGE, | |
254 | .desc = "Property '%(device).%(property)' doesn't take " | |
41453412 | 255 | "value %(value) (minimum: %(min), maximum: %(max))", |
6aced82c | 256 | }, |
c1303596 SH |
257 | { |
258 | .error_fmt = QERR_QGA_COMMAND_FAILED, | |
259 | .desc = "Guest agent command failed, error was '%(message)'", | |
260 | }, | |
261 | { | |
262 | .error_fmt = QERR_QGA_LOGGING_FAILED, | |
263 | .desc = "Guest agent failed to log non-optional log statement", | |
264 | }, | |
4b9d4683 LC |
265 | { |
266 | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, | |
abaf2f52 | 267 | .desc = "Expected '%(expected)' in QMP input", |
4b9d4683 | 268 | }, |
7dfb6123 LC |
269 | { |
270 | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER, | |
271 | .desc = "QMP input object member '%(member)' expects '%(expected)'", | |
272 | }, | |
60d76d7b LC |
273 | { |
274 | .error_fmt = QERR_QMP_EXTRA_MEMBER, | |
275 | .desc = "QMP input object member '%(member)' is unexpected", | |
276 | }, | |
6667b23f LC |
277 | { |
278 | .error_fmt = QERR_RESET_REQUIRED, | |
279 | .desc = "Resetting the Virtual Machine is required", | |
280 | }, | |
7a84cb23 MA |
281 | { |
282 | .error_fmt = QERR_SET_PASSWD_FAILED, | |
283 | .desc = "Could not set password", | |
284 | }, | |
a488be27 MA |
285 | { |
286 | .error_fmt = QERR_TOO_MANY_FILES, | |
287 | .desc = "Too many open files", | |
288 | }, | |
4b9d4683 | 289 | { |
e16a1812 | 290 | .error_fmt = QERR_UNDEFINED_ERROR, |
07f35073 | 291 | .desc = "An undefined error has occurred", |
4b9d4683 | 292 | }, |
f54e3641 KW |
293 | { |
294 | .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, | |
295 | .desc = "'%(device)' uses a %(format) feature which is not " | |
296 | "supported by this qemu version: %(feature)", | |
297 | }, | |
c1303596 SH |
298 | { |
299 | .error_fmt = QERR_UNSUPPORTED, | |
300 | .desc = "this feature or command is not currently supported", | |
301 | }, | |
e9a0152b AK |
302 | { |
303 | .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION, | |
304 | .desc = "Migration is disabled when VirtFS export path '%(path)' " | |
305 | "is mounted in the guest using mount_tag '%(tag)'", | |
306 | }, | |
a6906e31 MA |
307 | { |
308 | .error_fmt = QERR_VNC_SERVER_FAILED, | |
309 | .desc = "Could not start VNC server on %(target)", | |
310 | }, | |
aed3d11d AK |
311 | { |
312 | .error_fmt = QERR_SOCKET_CONNECT_IN_PROGRESS, | |
313 | .desc = "Connection can not be completed immediately", | |
314 | }, | |
315 | { | |
316 | .error_fmt = QERR_SOCKET_CONNECT_FAILED, | |
317 | .desc = "Failed to connect to socket", | |
318 | }, | |
319 | { | |
320 | .error_fmt = QERR_SOCKET_LISTEN_FAILED, | |
321 | .desc = "Failed to set socket to listening mode", | |
322 | }, | |
323 | { | |
324 | .error_fmt = QERR_SOCKET_BIND_FAILED, | |
325 | .desc = "Failed to bind socket", | |
326 | }, | |
327 | { | |
328 | .error_fmt = QERR_SOCKET_CREATE_FAILED, | |
329 | .desc = "Failed to create socket", | |
330 | }, | |
9f9daf9a LC |
331 | {} |
332 | }; | |
333 | ||
334 | /** | |
335 | * qerror_new(): Create a new QError | |
336 | * | |
337 | * Return strong reference. | |
338 | */ | |
339 | QError *qerror_new(void) | |
340 | { | |
341 | QError *qerr; | |
342 | ||
7267c094 | 343 | qerr = g_malloc0(sizeof(*qerr)); |
9f9daf9a LC |
344 | QOBJECT_INIT(qerr, &qerror_type); |
345 | ||
346 | return qerr; | |
347 | } | |
348 | ||
8b7968f7 SW |
349 | static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr, |
350 | const char *fmt, ...) | |
9f9daf9a LC |
351 | { |
352 | va_list ap; | |
353 | ||
354 | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func); | |
355 | fprintf(stderr, "qerror: -> "); | |
356 | ||
357 | va_start(ap, fmt); | |
358 | vfprintf(stderr, fmt, ap); | |
359 | va_end(ap); | |
360 | ||
361 | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr); | |
362 | abort(); | |
363 | } | |
364 | ||
8b7968f7 SW |
365 | static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr, |
366 | const char *fmt, va_list *va) | |
9f9daf9a LC |
367 | { |
368 | QObject *obj; | |
369 | ||
370 | obj = qobject_from_jsonv(fmt, va); | |
371 | if (!obj) { | |
372 | qerror_abort(qerr, "invalid format '%s'", fmt); | |
373 | } | |
374 | if (qobject_type(obj) != QTYPE_QDICT) { | |
375 | qerror_abort(qerr, "error format is not a QDict '%s'", fmt); | |
376 | } | |
377 | ||
378 | qerr->error = qobject_to_qdict(obj); | |
379 | ||
380 | obj = qdict_get(qerr->error, "class"); | |
381 | if (!obj) { | |
382 | qerror_abort(qerr, "missing 'class' key in '%s'", fmt); | |
383 | } | |
384 | if (qobject_type(obj) != QTYPE_QSTRING) { | |
385 | qerror_abort(qerr, "'class' key value should be a QString"); | |
386 | } | |
387 | ||
388 | obj = qdict_get(qerr->error, "data"); | |
389 | if (!obj) { | |
390 | qerror_abort(qerr, "missing 'data' key in '%s'", fmt); | |
391 | } | |
392 | if (qobject_type(obj) != QTYPE_QDICT) { | |
393 | qerror_abort(qerr, "'data' key value should be a QDICT"); | |
394 | } | |
395 | } | |
396 | ||
397 | static void qerror_set_desc(QError *qerr, const char *fmt) | |
398 | { | |
399 | int i; | |
400 | ||
401 | // FIXME: inefficient loop | |
402 | ||
403 | for (i = 0; qerror_table[i].error_fmt; i++) { | |
404 | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { | |
405 | qerr->entry = &qerror_table[i]; | |
406 | return; | |
407 | } | |
408 | } | |
409 | ||
410 | qerror_abort(qerr, "error format '%s' not found", fmt); | |
411 | } | |
412 | ||
413 | /** | |
414 | * qerror_from_info(): Create a new QError from error information | |
415 | * | |
416 | * The information consists of: | |
417 | * | |
418 | * - file the file name of where the error occurred | |
419 | * - linenr the line number of where the error occurred | |
420 | * - func the function name of where the error occurred | |
421 | * - fmt JSON printf-like dictionary, there must exist keys 'class' and | |
422 | * 'data' | |
423 | * - va va_list of all arguments specified by fmt | |
424 | * | |
425 | * Return strong reference. | |
426 | */ | |
427 | QError *qerror_from_info(const char *file, int linenr, const char *func, | |
428 | const char *fmt, va_list *va) | |
429 | { | |
430 | QError *qerr; | |
431 | ||
432 | qerr = qerror_new(); | |
827b0813 | 433 | loc_save(&qerr->loc); |
9f9daf9a LC |
434 | qerr->linenr = linenr; |
435 | qerr->file = file; | |
436 | qerr->func = func; | |
437 | ||
438 | if (!fmt) { | |
439 | qerror_abort(qerr, "QDict not specified"); | |
440 | } | |
441 | ||
442 | qerror_set_data(qerr, fmt, va); | |
443 | qerror_set_desc(qerr, fmt); | |
444 | ||
445 | return qerr; | |
446 | } | |
447 | ||
a12eeaaa | 448 | static void parse_error(const QErrorStringTable *entry, int c) |
9f9daf9a | 449 | { |
a12eeaaa LC |
450 | fprintf(stderr, "expected '%c' in '%s'", c, entry->desc); |
451 | abort(); | |
9f9daf9a LC |
452 | } |
453 | ||
a12eeaaa LC |
454 | static const char *append_field(QDict *error, QString *outstr, |
455 | const QErrorStringTable *entry, | |
9f9daf9a LC |
456 | const char *start) |
457 | { | |
458 | QObject *obj; | |
459 | QDict *qdict; | |
460 | QString *key_qs; | |
461 | const char *end, *key; | |
462 | ||
463 | if (*start != '%') | |
a12eeaaa | 464 | parse_error(entry, '%'); |
9f9daf9a LC |
465 | start++; |
466 | if (*start != '(') | |
a12eeaaa | 467 | parse_error(entry, '('); |
9f9daf9a LC |
468 | start++; |
469 | ||
470 | end = strchr(start, ')'); | |
471 | if (!end) | |
a12eeaaa | 472 | parse_error(entry, ')'); |
9f9daf9a LC |
473 | |
474 | key_qs = qstring_from_substr(start, 0, end - start - 1); | |
475 | key = qstring_get_str(key_qs); | |
476 | ||
a12eeaaa | 477 | qdict = qobject_to_qdict(qdict_get(error, "data")); |
9f9daf9a LC |
478 | obj = qdict_get(qdict, key); |
479 | if (!obj) { | |
a12eeaaa | 480 | abort(); |
9f9daf9a LC |
481 | } |
482 | ||
483 | switch (qobject_type(obj)) { | |
484 | case QTYPE_QSTRING: | |
485 | qstring_append(outstr, qdict_get_str(qdict, key)); | |
486 | break; | |
487 | case QTYPE_QINT: | |
488 | qstring_append_int(outstr, qdict_get_int(qdict, key)); | |
489 | break; | |
490 | default: | |
a12eeaaa | 491 | abort(); |
9f9daf9a LC |
492 | } |
493 | ||
494 | QDECREF(key_qs); | |
495 | return ++end; | |
496 | } | |
497 | ||
a12eeaaa LC |
498 | static QString *qerror_format_desc(QDict *error, |
499 | const QErrorStringTable *entry) | |
9f9daf9a | 500 | { |
9f9daf9a | 501 | QString *qstring; |
a12eeaaa | 502 | const char *p; |
9f9daf9a | 503 | |
a12eeaaa | 504 | assert(entry != NULL); |
9f9daf9a LC |
505 | |
506 | qstring = qstring_new(); | |
507 | ||
a12eeaaa | 508 | for (p = entry->desc; *p != '\0';) { |
9f9daf9a LC |
509 | if (*p != '%') { |
510 | qstring_append_chr(qstring, *p++); | |
511 | } else if (*(p + 1) == '%') { | |
512 | qstring_append_chr(qstring, '%'); | |
513 | p += 2; | |
514 | } else { | |
a12eeaaa | 515 | p = append_field(error, qstring, entry, p); |
9f9daf9a LC |
516 | } |
517 | } | |
518 | ||
77e595e7 MA |
519 | return qstring; |
520 | } | |
521 | ||
87c2f591 LC |
522 | QString *qerror_format(const char *fmt, QDict *error) |
523 | { | |
524 | const QErrorStringTable *entry = NULL; | |
525 | int i; | |
526 | ||
527 | for (i = 0; qerror_table[i].error_fmt; i++) { | |
528 | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { | |
529 | entry = &qerror_table[i]; | |
530 | break; | |
531 | } | |
532 | } | |
533 | ||
534 | return qerror_format_desc(error, entry); | |
535 | } | |
536 | ||
a12eeaaa LC |
537 | /** |
538 | * qerror_human(): Format QError data into human-readable string. | |
539 | */ | |
540 | QString *qerror_human(const QError *qerror) | |
541 | { | |
542 | return qerror_format_desc(qerror->error, qerror->entry); | |
543 | } | |
544 | ||
77e595e7 MA |
545 | /** |
546 | * qerror_print(): Print QError data | |
547 | * | |
548 | * This function will print the member 'desc' of the specified QError object, | |
1ecda02b | 549 | * it uses error_report() for this, so that the output is routed to the right |
77e595e7 MA |
550 | * place (ie. stderr or Monitor's device). |
551 | */ | |
827b0813 | 552 | void qerror_print(QError *qerror) |
77e595e7 MA |
553 | { |
554 | QString *qstring = qerror_human(qerror); | |
827b0813 | 555 | loc_push_restore(&qerror->loc); |
1ecda02b | 556 | error_report("%s", qstring_get_str(qstring)); |
827b0813 | 557 | loc_pop(&qerror->loc); |
9f9daf9a LC |
558 | QDECREF(qstring); |
559 | } | |
560 | ||
0167f772 MA |
561 | void qerror_report_internal(const char *file, int linenr, const char *func, |
562 | const char *fmt, ...) | |
563 | { | |
564 | va_list va; | |
565 | QError *qerror; | |
566 | ||
567 | va_start(va, fmt); | |
568 | qerror = qerror_from_info(file, linenr, func, fmt, &va); | |
569 | va_end(va); | |
570 | ||
571 | if (monitor_cur_is_qmp()) { | |
572 | monitor_set_error(cur_mon, qerror); | |
573 | } else { | |
574 | qerror_print(qerror); | |
575 | QDECREF(qerror); | |
576 | } | |
577 | } | |
578 | ||
2a82d936 AL |
579 | /* Evil... */ |
580 | struct Error | |
581 | { | |
582 | QDict *obj; | |
583 | const char *fmt; | |
584 | char *msg; | |
585 | }; | |
586 | ||
587 | void qerror_report_err(Error *err) | |
588 | { | |
589 | QError *qerr; | |
590 | int i; | |
591 | ||
592 | qerr = qerror_new(); | |
593 | loc_save(&qerr->loc); | |
594 | QINCREF(err->obj); | |
595 | qerr->error = err->obj; | |
596 | ||
597 | for (i = 0; qerror_table[i].error_fmt; i++) { | |
598 | if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) { | |
599 | qerr->entry = &qerror_table[i]; | |
600 | break; | |
601 | } | |
602 | } | |
603 | ||
604 | if (monitor_cur_is_qmp()) { | |
605 | monitor_set_error(cur_mon, qerr); | |
606 | } else { | |
607 | qerror_print(qerr); | |
608 | QDECREF(qerr); | |
609 | } | |
610 | } | |
611 | ||
59f971d4 PB |
612 | void assert_no_error(Error *err) |
613 | { | |
614 | if (err) { | |
615 | qerror_report_err(err); | |
616 | abort(); | |
617 | } | |
618 | } | |
619 | ||
9f9daf9a LC |
620 | /** |
621 | * qobject_to_qerror(): Convert a QObject into a QError | |
622 | */ | |
623 | QError *qobject_to_qerror(const QObject *obj) | |
624 | { | |
625 | if (qobject_type(obj) != QTYPE_QERROR) { | |
626 | return NULL; | |
627 | } | |
628 | ||
629 | return container_of(obj, QError, base); | |
630 | } | |
631 | ||
632 | /** | |
633 | * qerror_destroy_obj(): Free all memory allocated by a QError | |
634 | */ | |
635 | static void qerror_destroy_obj(QObject *obj) | |
636 | { | |
637 | QError *qerr; | |
638 | ||
639 | assert(obj != NULL); | |
640 | qerr = qobject_to_qerror(obj); | |
641 | ||
642 | QDECREF(qerr->error); | |
7267c094 | 643 | g_free(qerr); |
9f9daf9a | 644 | } |