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