QError: Don't abort on multiple faults
[qemu/kevin.git] / qerror.c
blob2f657f4f7b60906db0eeb6bc5f9aeeb6f479d272
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 "sysemu.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_COMMAND_NOT_FOUND,
45 .desc = "The command %(name) has not been found",
48 .error_fmt = QERR_DEVICE_ENCRYPTED,
49 .desc = "The %(device) is encrypted",
52 .error_fmt = QERR_DEVICE_LOCKED,
53 .desc = "Device %(device) is locked",
56 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
57 .desc = "The %(device) device has not been activated by the guest",
60 .error_fmt = QERR_DEVICE_NOT_FOUND,
61 .desc = "The %(device) device has not been found",
64 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
65 .desc = "Device %(device) is not removable",
68 .error_fmt = QERR_FD_NOT_FOUND,
69 .desc = "Failed to find file descriptor named %(name)",
72 .error_fmt = QERR_FD_NOT_SUPPLIED,
73 .desc = "No file descriptor supplied via SCM_RIGHTS",
76 .error_fmt = QERR_OPEN_FILE_FAILED,
77 .desc = "Could not open '%(filename)'",
80 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
81 .desc = "Invalid block format %(name)",
84 .error_fmt = QERR_INVALID_PARAMETER,
85 .desc = "Invalid parameter %(name)",
88 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
89 .desc = "Invalid parameter type, expected: %(expected)",
92 .error_fmt = QERR_INVALID_PASSWORD,
93 .desc = "The entered password is invalid",
96 .error_fmt = QERR_JSON_PARSING,
97 .desc = "Invalid JSON syntax",
100 .error_fmt = QERR_KVM_MISSING_CAP,
101 .desc = "Using KVM without %(capability), %(feature) unavailable",
104 .error_fmt = QERR_MISSING_PARAMETER,
105 .desc = "Parameter %(name) is missing",
108 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
109 .desc = "Bad QMP input object",
112 .error_fmt = QERR_SET_PASSWD_FAILED,
113 .desc = "Could not set password",
116 .error_fmt = QERR_TOO_MANY_FILES,
117 .desc = "Too many open files",
120 .error_fmt = QERR_UNDEFINED_ERROR,
121 .desc = "An undefined error has ocurred",
124 .error_fmt = QERR_VNC_SERVER_FAILED,
125 .desc = "Could not start VNC server on %(target)",
131 * qerror_new(): Create a new QError
133 * Return strong reference.
135 QError *qerror_new(void)
137 QError *qerr;
139 qerr = qemu_mallocz(sizeof(*qerr));
140 QOBJECT_INIT(qerr, &qerror_type);
142 return qerr;
145 static void qerror_abort(const QError *qerr, const char *fmt, ...)
147 va_list ap;
149 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
150 fprintf(stderr, "qerror: -> ");
152 va_start(ap, fmt);
153 vfprintf(stderr, fmt, ap);
154 va_end(ap);
156 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
157 abort();
160 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
162 QObject *obj;
164 obj = qobject_from_jsonv(fmt, va);
165 if (!obj) {
166 qerror_abort(qerr, "invalid format '%s'", fmt);
168 if (qobject_type(obj) != QTYPE_QDICT) {
169 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
172 qerr->error = qobject_to_qdict(obj);
174 obj = qdict_get(qerr->error, "class");
175 if (!obj) {
176 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
178 if (qobject_type(obj) != QTYPE_QSTRING) {
179 qerror_abort(qerr, "'class' key value should be a QString");
182 obj = qdict_get(qerr->error, "data");
183 if (!obj) {
184 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
186 if (qobject_type(obj) != QTYPE_QDICT) {
187 qerror_abort(qerr, "'data' key value should be a QDICT");
191 static void qerror_set_desc(QError *qerr, const char *fmt)
193 int i;
195 // FIXME: inefficient loop
197 for (i = 0; qerror_table[i].error_fmt; i++) {
198 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
199 qerr->entry = &qerror_table[i];
200 return;
204 qerror_abort(qerr, "error format '%s' not found", fmt);
208 * qerror_from_info(): Create a new QError from error information
210 * The information consists of:
212 * - file the file name of where the error occurred
213 * - linenr the line number of where the error occurred
214 * - func the function name of where the error occurred
215 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
216 * 'data'
217 * - va va_list of all arguments specified by fmt
219 * Return strong reference.
221 QError *qerror_from_info(const char *file, int linenr, const char *func,
222 const char *fmt, va_list *va)
224 QError *qerr;
226 qerr = qerror_new();
227 qerr->linenr = linenr;
228 qerr->file = file;
229 qerr->func = func;
231 if (!fmt) {
232 qerror_abort(qerr, "QDict not specified");
235 qerror_set_data(qerr, fmt, va);
236 qerror_set_desc(qerr, fmt);
238 return qerr;
241 static void parse_error(const QError *qerror, int c)
243 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
246 static const char *append_field(QString *outstr, const QError *qerror,
247 const char *start)
249 QObject *obj;
250 QDict *qdict;
251 QString *key_qs;
252 const char *end, *key;
254 if (*start != '%')
255 parse_error(qerror, '%');
256 start++;
257 if (*start != '(')
258 parse_error(qerror, '(');
259 start++;
261 end = strchr(start, ')');
262 if (!end)
263 parse_error(qerror, ')');
265 key_qs = qstring_from_substr(start, 0, end - start - 1);
266 key = qstring_get_str(key_qs);
268 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
269 obj = qdict_get(qdict, key);
270 if (!obj) {
271 qerror_abort(qerror, "key '%s' not found in QDict", key);
274 switch (qobject_type(obj)) {
275 case QTYPE_QSTRING:
276 qstring_append(outstr, qdict_get_str(qdict, key));
277 break;
278 case QTYPE_QINT:
279 qstring_append_int(outstr, qdict_get_int(qdict, key));
280 break;
281 default:
282 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
285 QDECREF(key_qs);
286 return ++end;
290 * qerror_human(): Format QError data into human-readable string.
292 * Formats according to member 'desc' of the specified QError object.
294 QString *qerror_human(const QError *qerror)
296 const char *p;
297 QString *qstring;
299 assert(qerror->entry != NULL);
301 qstring = qstring_new();
303 for (p = qerror->entry->desc; *p != '\0';) {
304 if (*p != '%') {
305 qstring_append_chr(qstring, *p++);
306 } else if (*(p + 1) == '%') {
307 qstring_append_chr(qstring, '%');
308 p += 2;
309 } else {
310 p = append_field(qstring, qerror, p);
314 return qstring;
318 * qerror_print(): Print QError data
320 * This function will print the member 'desc' of the specified QError object,
321 * it uses qemu_error() for this, so that the output is routed to the right
322 * place (ie. stderr or Monitor's device).
324 void qerror_print(const QError *qerror)
326 QString *qstring = qerror_human(qerror);
327 qemu_error("%s\n", qstring_get_str(qstring));
328 QDECREF(qstring);
332 * qobject_to_qerror(): Convert a QObject into a QError
334 QError *qobject_to_qerror(const QObject *obj)
336 if (qobject_type(obj) != QTYPE_QERROR) {
337 return NULL;
340 return container_of(obj, QError, base);
344 * qerror_destroy_obj(): Free all memory allocated by a QError
346 static void qerror_destroy_obj(QObject *obj)
348 QError *qerr;
350 assert(obj != NULL);
351 qerr = qobject_to_qerror(obj);
353 QDECREF(qerr->error);
354 qemu_free(qerr);