update bochs vbe interface
[qemu/mdroth.git] / qerror.c
blobeaa1deb28620e6dd4d8af1f324dc119b4ad56136
1 /*
2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "monitor.h"
14 #include "qjson.h"
15 #include "qerror.h"
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject *obj);
20 static const QType qerror_type = {
21 .code = QTYPE_QERROR,
22 .destroy = qerror_destroy_obj,
25 /**
26 * The 'desc' parameter is a printf-like string, the format of the format
27 * string is:
29 * %(KEY)
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
33 * Example:
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
38 * for example:
40 * "running out of foo: %(foo)%%"
42 static const QErrorStringTable qerror_table[] = {
44 .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
45 .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
48 .error_fmt = QERR_BUS_NOT_FOUND,
49 .desc = "Bus '%(bus)' not found",
52 .error_fmt = QERR_BUS_NO_HOTPLUG,
53 .desc = "Bus '%(bus)' does not support hotplugging",
56 .error_fmt = QERR_COMMAND_NOT_FOUND,
57 .desc = "The command %(name) has not been found",
60 .error_fmt = QERR_DEVICE_ENCRYPTED,
61 .desc = "Device '%(device)' is encrypted",
64 .error_fmt = QERR_DEVICE_INIT_FAILED,
65 .desc = "Device '%(device)' could not be initialized",
68 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
69 .desc = "Device '%(device)' is not encrypted",
72 .error_fmt = QERR_DEVICE_LOCKED,
73 .desc = "Device '%(device)' is locked",
76 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
77 .desc = "Device '%(device)' has multiple child busses",
80 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
81 .desc = "Device '%(device)' has not been activated by the guest",
84 .error_fmt = QERR_DEVICE_NOT_FOUND,
85 .desc = "Device '%(device)' not found",
88 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
89 .desc = "Device '%(device)' is not removable",
92 .error_fmt = QERR_DEVICE_NO_BUS,
93 .desc = "Device '%(device)' has no child bus",
96 .error_fmt = QERR_FD_NOT_FOUND,
97 .desc = "File descriptor named '%(name)' not found",
100 .error_fmt = QERR_FD_NOT_SUPPLIED,
101 .desc = "No file descriptor supplied via SCM_RIGHTS",
104 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
105 .desc = "Invalid block format '%(name)'",
108 .error_fmt = QERR_INVALID_PARAMETER,
109 .desc = "Invalid parameter '%(name)'",
112 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
113 .desc = "Invalid parameter type, expected: %(expected)",
116 .error_fmt = QERR_INVALID_PASSWORD,
117 .desc = "Password incorrect",
120 .error_fmt = QERR_JSON_PARSING,
121 .desc = "Invalid JSON syntax",
124 .error_fmt = QERR_KVM_MISSING_CAP,
125 .desc = "Using KVM without %(capability), %(feature) unavailable",
128 .error_fmt = QERR_MISSING_PARAMETER,
129 .desc = "Parameter '%(name)' is missing",
132 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
133 .desc = "No '%(bus)' bus found for device '%(device)'",
136 .error_fmt = QERR_OPEN_FILE_FAILED,
137 .desc = "Could not open '%(filename)'",
140 .error_fmt = QERR_PROPERTY_NOT_FOUND,
141 .desc = "Property '%(device).%(property)' not found",
144 .error_fmt = QERR_PROPERTY_VALUE_BAD,
145 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
148 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
149 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
152 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
153 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
156 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
157 .desc = "Bad QMP input object",
160 .error_fmt = QERR_SET_PASSWD_FAILED,
161 .desc = "Could not set password",
164 .error_fmt = QERR_TOO_MANY_FILES,
165 .desc = "Too many open files",
168 .error_fmt = QERR_UNDEFINED_ERROR,
169 .desc = "An undefined error has ocurred",
172 .error_fmt = QERR_VNC_SERVER_FAILED,
173 .desc = "Could not start VNC server on %(target)",
179 * qerror_new(): Create a new QError
181 * Return strong reference.
183 QError *qerror_new(void)
185 QError *qerr;
187 qerr = qemu_mallocz(sizeof(*qerr));
188 QOBJECT_INIT(qerr, &qerror_type);
190 return qerr;
193 static void qerror_abort(const QError *qerr, const char *fmt, ...)
195 va_list ap;
197 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
198 fprintf(stderr, "qerror: -> ");
200 va_start(ap, fmt);
201 vfprintf(stderr, fmt, ap);
202 va_end(ap);
204 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
205 abort();
208 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
210 QObject *obj;
212 obj = qobject_from_jsonv(fmt, va);
213 if (!obj) {
214 qerror_abort(qerr, "invalid format '%s'", fmt);
216 if (qobject_type(obj) != QTYPE_QDICT) {
217 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
220 qerr->error = qobject_to_qdict(obj);
222 obj = qdict_get(qerr->error, "class");
223 if (!obj) {
224 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
226 if (qobject_type(obj) != QTYPE_QSTRING) {
227 qerror_abort(qerr, "'class' key value should be a QString");
230 obj = qdict_get(qerr->error, "data");
231 if (!obj) {
232 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
234 if (qobject_type(obj) != QTYPE_QDICT) {
235 qerror_abort(qerr, "'data' key value should be a QDICT");
239 static void qerror_set_desc(QError *qerr, const char *fmt)
241 int i;
243 // FIXME: inefficient loop
245 for (i = 0; qerror_table[i].error_fmt; i++) {
246 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
247 qerr->entry = &qerror_table[i];
248 return;
252 qerror_abort(qerr, "error format '%s' not found", fmt);
256 * qerror_from_info(): Create a new QError from error information
258 * The information consists of:
260 * - file the file name of where the error occurred
261 * - linenr the line number of where the error occurred
262 * - func the function name of where the error occurred
263 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
264 * 'data'
265 * - va va_list of all arguments specified by fmt
267 * Return strong reference.
269 QError *qerror_from_info(const char *file, int linenr, const char *func,
270 const char *fmt, va_list *va)
272 QError *qerr;
274 qerr = qerror_new();
275 loc_save(&qerr->loc);
276 qerr->linenr = linenr;
277 qerr->file = file;
278 qerr->func = func;
280 if (!fmt) {
281 qerror_abort(qerr, "QDict not specified");
284 qerror_set_data(qerr, fmt, va);
285 qerror_set_desc(qerr, fmt);
287 return qerr;
290 static void parse_error(const QError *qerror, int c)
292 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
295 static const char *append_field(QString *outstr, const QError *qerror,
296 const char *start)
298 QObject *obj;
299 QDict *qdict;
300 QString *key_qs;
301 const char *end, *key;
303 if (*start != '%')
304 parse_error(qerror, '%');
305 start++;
306 if (*start != '(')
307 parse_error(qerror, '(');
308 start++;
310 end = strchr(start, ')');
311 if (!end)
312 parse_error(qerror, ')');
314 key_qs = qstring_from_substr(start, 0, end - start - 1);
315 key = qstring_get_str(key_qs);
317 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
318 obj = qdict_get(qdict, key);
319 if (!obj) {
320 qerror_abort(qerror, "key '%s' not found in QDict", key);
323 switch (qobject_type(obj)) {
324 case QTYPE_QSTRING:
325 qstring_append(outstr, qdict_get_str(qdict, key));
326 break;
327 case QTYPE_QINT:
328 qstring_append_int(outstr, qdict_get_int(qdict, key));
329 break;
330 default:
331 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
334 QDECREF(key_qs);
335 return ++end;
339 * qerror_human(): Format QError data into human-readable string.
341 * Formats according to member 'desc' of the specified QError object.
343 QString *qerror_human(const QError *qerror)
345 const char *p;
346 QString *qstring;
348 assert(qerror->entry != NULL);
350 qstring = qstring_new();
352 for (p = qerror->entry->desc; *p != '\0';) {
353 if (*p != '%') {
354 qstring_append_chr(qstring, *p++);
355 } else if (*(p + 1) == '%') {
356 qstring_append_chr(qstring, '%');
357 p += 2;
358 } else {
359 p = append_field(qstring, qerror, p);
363 return qstring;
367 * qerror_print(): Print QError data
369 * This function will print the member 'desc' of the specified QError object,
370 * it uses error_report() for this, so that the output is routed to the right
371 * place (ie. stderr or Monitor's device).
373 void qerror_print(QError *qerror)
375 QString *qstring = qerror_human(qerror);
376 loc_push_restore(&qerror->loc);
377 error_report("%s", qstring_get_str(qstring));
378 loc_pop(&qerror->loc);
379 QDECREF(qstring);
382 void qerror_report_internal(const char *file, int linenr, const char *func,
383 const char *fmt, ...)
385 va_list va;
386 QError *qerror;
388 va_start(va, fmt);
389 qerror = qerror_from_info(file, linenr, func, fmt, &va);
390 va_end(va);
392 if (monitor_cur_is_qmp()) {
393 monitor_set_error(cur_mon, qerror);
394 } else {
395 qerror_print(qerror);
396 QDECREF(qerror);
401 * qobject_to_qerror(): Convert a QObject into a QError
403 QError *qobject_to_qerror(const QObject *obj)
405 if (qobject_type(obj) != QTYPE_QERROR) {
406 return NULL;
409 return container_of(obj, QError, base);
413 * qerror_destroy_obj(): Free all memory allocated by a QError
415 static void qerror_destroy_obj(QObject *obj)
417 QError *qerr;
419 assert(obj != NULL);
420 qerr = qobject_to_qerror(obj);
422 QDECREF(qerr->error);
423 qemu_free(qerr);