error: New QERR_DEVICE_INIT_FAILED
[qemu/aliguori-queue.git] / qerror.c
bloba3692045887fd54325a94eb6f9a3ad8f1238585a
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.
12 #include "qjson.h"
13 #include "qerror.h"
14 #include "qstring.h"
15 #include "qemu-common.h"
16 #include "qemu-error.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_LOCKED,
69 .desc = "Device '%(device)' is locked",
72 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
73 .desc = "Device '%(device)' has multiple child busses",
76 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
77 .desc = "Device '%(device)' has not been activated by the guest",
80 .error_fmt = QERR_DEVICE_NOT_FOUND,
81 .desc = "Device '%(device)' not found",
84 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
85 .desc = "Device '%(device)' is not removable",
88 .error_fmt = QERR_DEVICE_NO_BUS,
89 .desc = "Device '%(device)' has no child bus",
92 .error_fmt = QERR_FD_NOT_FOUND,
93 .desc = "File descriptor named '%(name)' not found",
96 .error_fmt = QERR_FD_NOT_SUPPLIED,
97 .desc = "No file descriptor supplied via SCM_RIGHTS",
100 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
101 .desc = "Invalid block format '%(name)'",
104 .error_fmt = QERR_INVALID_PARAMETER,
105 .desc = "Invalid parameter '%(name)'",
108 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
109 .desc = "Invalid parameter type, expected: %(expected)",
112 .error_fmt = QERR_INVALID_PASSWORD,
113 .desc = "Password incorrect",
116 .error_fmt = QERR_JSON_PARSING,
117 .desc = "Invalid JSON syntax",
120 .error_fmt = QERR_KVM_MISSING_CAP,
121 .desc = "Using KVM without %(capability), %(feature) unavailable",
124 .error_fmt = QERR_MISSING_PARAMETER,
125 .desc = "Parameter '%(name)' is missing",
128 .error_fmt = QERR_OPEN_FILE_FAILED,
129 .desc = "Could not open '%(filename)'",
132 .error_fmt = QERR_PROPERTY_NOT_FOUND,
133 .desc = "Property '%(device).%(property)' not found",
136 .error_fmt = QERR_PROPERTY_VALUE_BAD,
137 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
140 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
141 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
144 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
145 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
148 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
149 .desc = "Bad QMP input object",
152 .error_fmt = QERR_SET_PASSWD_FAILED,
153 .desc = "Could not set password",
156 .error_fmt = QERR_TOO_MANY_FILES,
157 .desc = "Too many open files",
160 .error_fmt = QERR_UNDEFINED_ERROR,
161 .desc = "An undefined error has ocurred",
164 .error_fmt = QERR_VNC_SERVER_FAILED,
165 .desc = "Could not start VNC server on %(target)",
171 * qerror_new(): Create a new QError
173 * Return strong reference.
175 QError *qerror_new(void)
177 QError *qerr;
179 qerr = qemu_mallocz(sizeof(*qerr));
180 QOBJECT_INIT(qerr, &qerror_type);
182 return qerr;
185 static void qerror_abort(const QError *qerr, const char *fmt, ...)
187 va_list ap;
189 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
190 fprintf(stderr, "qerror: -> ");
192 va_start(ap, fmt);
193 vfprintf(stderr, fmt, ap);
194 va_end(ap);
196 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
197 abort();
200 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
202 QObject *obj;
204 obj = qobject_from_jsonv(fmt, va);
205 if (!obj) {
206 qerror_abort(qerr, "invalid format '%s'", fmt);
208 if (qobject_type(obj) != QTYPE_QDICT) {
209 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
212 qerr->error = qobject_to_qdict(obj);
214 obj = qdict_get(qerr->error, "class");
215 if (!obj) {
216 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
218 if (qobject_type(obj) != QTYPE_QSTRING) {
219 qerror_abort(qerr, "'class' key value should be a QString");
222 obj = qdict_get(qerr->error, "data");
223 if (!obj) {
224 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
226 if (qobject_type(obj) != QTYPE_QDICT) {
227 qerror_abort(qerr, "'data' key value should be a QDICT");
231 static void qerror_set_desc(QError *qerr, const char *fmt)
233 int i;
235 // FIXME: inefficient loop
237 for (i = 0; qerror_table[i].error_fmt; i++) {
238 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
239 qerr->entry = &qerror_table[i];
240 return;
244 qerror_abort(qerr, "error format '%s' not found", fmt);
248 * qerror_from_info(): Create a new QError from error information
250 * The information consists of:
252 * - file the file name of where the error occurred
253 * - linenr the line number of where the error occurred
254 * - func the function name of where the error occurred
255 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
256 * 'data'
257 * - va va_list of all arguments specified by fmt
259 * Return strong reference.
261 QError *qerror_from_info(const char *file, int linenr, const char *func,
262 const char *fmt, va_list *va)
264 QError *qerr;
266 qerr = qerror_new();
267 loc_save(&qerr->loc);
268 qerr->linenr = linenr;
269 qerr->file = file;
270 qerr->func = func;
272 if (!fmt) {
273 qerror_abort(qerr, "QDict not specified");
276 qerror_set_data(qerr, fmt, va);
277 qerror_set_desc(qerr, fmt);
279 return qerr;
282 static void parse_error(const QError *qerror, int c)
284 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
287 static const char *append_field(QString *outstr, const QError *qerror,
288 const char *start)
290 QObject *obj;
291 QDict *qdict;
292 QString *key_qs;
293 const char *end, *key;
295 if (*start != '%')
296 parse_error(qerror, '%');
297 start++;
298 if (*start != '(')
299 parse_error(qerror, '(');
300 start++;
302 end = strchr(start, ')');
303 if (!end)
304 parse_error(qerror, ')');
306 key_qs = qstring_from_substr(start, 0, end - start - 1);
307 key = qstring_get_str(key_qs);
309 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
310 obj = qdict_get(qdict, key);
311 if (!obj) {
312 qerror_abort(qerror, "key '%s' not found in QDict", key);
315 switch (qobject_type(obj)) {
316 case QTYPE_QSTRING:
317 qstring_append(outstr, qdict_get_str(qdict, key));
318 break;
319 case QTYPE_QINT:
320 qstring_append_int(outstr, qdict_get_int(qdict, key));
321 break;
322 default:
323 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
326 QDECREF(key_qs);
327 return ++end;
331 * qerror_human(): Format QError data into human-readable string.
333 * Formats according to member 'desc' of the specified QError object.
335 QString *qerror_human(const QError *qerror)
337 const char *p;
338 QString *qstring;
340 assert(qerror->entry != NULL);
342 qstring = qstring_new();
344 for (p = qerror->entry->desc; *p != '\0';) {
345 if (*p != '%') {
346 qstring_append_chr(qstring, *p++);
347 } else if (*(p + 1) == '%') {
348 qstring_append_chr(qstring, '%');
349 p += 2;
350 } else {
351 p = append_field(qstring, qerror, p);
355 return qstring;
359 * qerror_print(): Print QError data
361 * This function will print the member 'desc' of the specified QError object,
362 * it uses error_report() for this, so that the output is routed to the right
363 * place (ie. stderr or Monitor's device).
365 void qerror_print(QError *qerror)
367 QString *qstring = qerror_human(qerror);
368 loc_push_restore(&qerror->loc);
369 error_report("%s", qstring_get_str(qstring));
370 loc_pop(&qerror->loc);
371 QDECREF(qstring);
375 * qobject_to_qerror(): Convert a QObject into a QError
377 QError *qobject_to_qerror(const QObject *obj)
379 if (qobject_type(obj) != QTYPE_QERROR) {
380 return NULL;
383 return container_of(obj, QError, base);
387 * qerror_destroy_obj(): Free all memory allocated by a QError
389 static void qerror_destroy_obj(QObject *obj)
391 QError *qerr;
393 assert(obj != NULL);
394 qerr = qobject_to_qerror(obj);
396 QDECREF(qerr->error);
397 qemu_free(qerr);