]> git.proxmox.com Git - qemu.git/blob - qerror.c
error: Trim includes in qerror.c
[qemu.git] / qerror.c
1 /*
2 * QError: QEMU Error data-type.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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 */
12 #include "qjson.h"
13 #include "qerror.h"
14 #include "qemu-common.h"
15
16 static void qerror_destroy_obj(QObject *obj);
17
18 static const QType qerror_type = {
19 .code = QTYPE_QERROR,
20 .destroy = qerror_destroy_obj,
21 };
22
23 /**
24 * The 'desc' parameter is a printf-like string, the format of the format
25 * string is:
26 *
27 * %(KEY)
28 *
29 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
30 *
31 * Example:
32 *
33 * "foo error on device: %(device) slot: %(slot_nr)"
34 *
35 * A single percent sign can be printed if followed by a second one,
36 * for example:
37 *
38 * "running out of foo: %(foo)%%"
39 */
40 static const QErrorStringTable qerror_table[] = {
41 {
42 .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
43 .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
44 },
45 {
46 .error_fmt = QERR_BUS_NOT_FOUND,
47 .desc = "Bus '%(bus)' not found",
48 },
49 {
50 .error_fmt = QERR_BUS_NO_HOTPLUG,
51 .desc = "Bus '%(bus)' does not support hotplugging",
52 },
53 {
54 .error_fmt = QERR_COMMAND_NOT_FOUND,
55 .desc = "The command %(name) has not been found",
56 },
57 {
58 .error_fmt = QERR_DEVICE_ENCRYPTED,
59 .desc = "Device '%(device)' is encrypted",
60 },
61 {
62 .error_fmt = QERR_DEVICE_INIT_FAILED,
63 .desc = "Device '%(device)' could not be initialized",
64 },
65 {
66 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
67 .desc = "Device '%(device)' is not encrypted",
68 },
69 {
70 .error_fmt = QERR_DEVICE_LOCKED,
71 .desc = "Device '%(device)' is locked",
72 },
73 {
74 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
75 .desc = "Device '%(device)' has multiple child busses",
76 },
77 {
78 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
79 .desc = "Device '%(device)' has not been activated by the guest",
80 },
81 {
82 .error_fmt = QERR_DEVICE_NOT_FOUND,
83 .desc = "Device '%(device)' not found",
84 },
85 {
86 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
87 .desc = "Device '%(device)' is not removable",
88 },
89 {
90 .error_fmt = QERR_DEVICE_NO_BUS,
91 .desc = "Device '%(device)' has no child bus",
92 },
93 {
94 .error_fmt = QERR_FD_NOT_FOUND,
95 .desc = "File descriptor named '%(name)' not found",
96 },
97 {
98 .error_fmt = QERR_FD_NOT_SUPPLIED,
99 .desc = "No file descriptor supplied via SCM_RIGHTS",
100 },
101 {
102 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
103 .desc = "Invalid block format '%(name)'",
104 },
105 {
106 .error_fmt = QERR_INVALID_PARAMETER,
107 .desc = "Invalid parameter '%(name)'",
108 },
109 {
110 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
111 .desc = "Invalid parameter type, expected: %(expected)",
112 },
113 {
114 .error_fmt = QERR_INVALID_PASSWORD,
115 .desc = "Password incorrect",
116 },
117 {
118 .error_fmt = QERR_JSON_PARSING,
119 .desc = "Invalid JSON syntax",
120 },
121 {
122 .error_fmt = QERR_KVM_MISSING_CAP,
123 .desc = "Using KVM without %(capability), %(feature) unavailable",
124 },
125 {
126 .error_fmt = QERR_MISSING_PARAMETER,
127 .desc = "Parameter '%(name)' is missing",
128 },
129 {
130 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
131 .desc = "No '%(bus)' bus found for device '%(device)'",
132 },
133 {
134 .error_fmt = QERR_OPEN_FILE_FAILED,
135 .desc = "Could not open '%(filename)'",
136 },
137 {
138 .error_fmt = QERR_PROPERTY_NOT_FOUND,
139 .desc = "Property '%(device).%(property)' not found",
140 },
141 {
142 .error_fmt = QERR_PROPERTY_VALUE_BAD,
143 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
144 },
145 {
146 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
147 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
148 },
149 {
150 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
151 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
152 },
153 {
154 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
155 .desc = "Bad QMP input object",
156 },
157 {
158 .error_fmt = QERR_SET_PASSWD_FAILED,
159 .desc = "Could not set password",
160 },
161 {
162 .error_fmt = QERR_TOO_MANY_FILES,
163 .desc = "Too many open files",
164 },
165 {
166 .error_fmt = QERR_UNDEFINED_ERROR,
167 .desc = "An undefined error has ocurred",
168 },
169 {
170 .error_fmt = QERR_VNC_SERVER_FAILED,
171 .desc = "Could not start VNC server on %(target)",
172 },
173 {}
174 };
175
176 /**
177 * qerror_new(): Create a new QError
178 *
179 * Return strong reference.
180 */
181 QError *qerror_new(void)
182 {
183 QError *qerr;
184
185 qerr = qemu_mallocz(sizeof(*qerr));
186 QOBJECT_INIT(qerr, &qerror_type);
187
188 return qerr;
189 }
190
191 static void qerror_abort(const QError *qerr, const char *fmt, ...)
192 {
193 va_list ap;
194
195 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
196 fprintf(stderr, "qerror: -> ");
197
198 va_start(ap, fmt);
199 vfprintf(stderr, fmt, ap);
200 va_end(ap);
201
202 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
203 abort();
204 }
205
206 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
207 {
208 QObject *obj;
209
210 obj = qobject_from_jsonv(fmt, va);
211 if (!obj) {
212 qerror_abort(qerr, "invalid format '%s'", fmt);
213 }
214 if (qobject_type(obj) != QTYPE_QDICT) {
215 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
216 }
217
218 qerr->error = qobject_to_qdict(obj);
219
220 obj = qdict_get(qerr->error, "class");
221 if (!obj) {
222 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
223 }
224 if (qobject_type(obj) != QTYPE_QSTRING) {
225 qerror_abort(qerr, "'class' key value should be a QString");
226 }
227
228 obj = qdict_get(qerr->error, "data");
229 if (!obj) {
230 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
231 }
232 if (qobject_type(obj) != QTYPE_QDICT) {
233 qerror_abort(qerr, "'data' key value should be a QDICT");
234 }
235 }
236
237 static void qerror_set_desc(QError *qerr, const char *fmt)
238 {
239 int i;
240
241 // FIXME: inefficient loop
242
243 for (i = 0; qerror_table[i].error_fmt; i++) {
244 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
245 qerr->entry = &qerror_table[i];
246 return;
247 }
248 }
249
250 qerror_abort(qerr, "error format '%s' not found", fmt);
251 }
252
253 /**
254 * qerror_from_info(): Create a new QError from error information
255 *
256 * The information consists of:
257 *
258 * - file the file name of where the error occurred
259 * - linenr the line number of where the error occurred
260 * - func the function name of where the error occurred
261 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
262 * 'data'
263 * - va va_list of all arguments specified by fmt
264 *
265 * Return strong reference.
266 */
267 QError *qerror_from_info(const char *file, int linenr, const char *func,
268 const char *fmt, va_list *va)
269 {
270 QError *qerr;
271
272 qerr = qerror_new();
273 loc_save(&qerr->loc);
274 qerr->linenr = linenr;
275 qerr->file = file;
276 qerr->func = func;
277
278 if (!fmt) {
279 qerror_abort(qerr, "QDict not specified");
280 }
281
282 qerror_set_data(qerr, fmt, va);
283 qerror_set_desc(qerr, fmt);
284
285 return qerr;
286 }
287
288 static void parse_error(const QError *qerror, int c)
289 {
290 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
291 }
292
293 static const char *append_field(QString *outstr, const QError *qerror,
294 const char *start)
295 {
296 QObject *obj;
297 QDict *qdict;
298 QString *key_qs;
299 const char *end, *key;
300
301 if (*start != '%')
302 parse_error(qerror, '%');
303 start++;
304 if (*start != '(')
305 parse_error(qerror, '(');
306 start++;
307
308 end = strchr(start, ')');
309 if (!end)
310 parse_error(qerror, ')');
311
312 key_qs = qstring_from_substr(start, 0, end - start - 1);
313 key = qstring_get_str(key_qs);
314
315 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
316 obj = qdict_get(qdict, key);
317 if (!obj) {
318 qerror_abort(qerror, "key '%s' not found in QDict", key);
319 }
320
321 switch (qobject_type(obj)) {
322 case QTYPE_QSTRING:
323 qstring_append(outstr, qdict_get_str(qdict, key));
324 break;
325 case QTYPE_QINT:
326 qstring_append_int(outstr, qdict_get_int(qdict, key));
327 break;
328 default:
329 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
330 }
331
332 QDECREF(key_qs);
333 return ++end;
334 }
335
336 /**
337 * qerror_human(): Format QError data into human-readable string.
338 *
339 * Formats according to member 'desc' of the specified QError object.
340 */
341 QString *qerror_human(const QError *qerror)
342 {
343 const char *p;
344 QString *qstring;
345
346 assert(qerror->entry != NULL);
347
348 qstring = qstring_new();
349
350 for (p = qerror->entry->desc; *p != '\0';) {
351 if (*p != '%') {
352 qstring_append_chr(qstring, *p++);
353 } else if (*(p + 1) == '%') {
354 qstring_append_chr(qstring, '%');
355 p += 2;
356 } else {
357 p = append_field(qstring, qerror, p);
358 }
359 }
360
361 return qstring;
362 }
363
364 /**
365 * qerror_print(): Print QError data
366 *
367 * This function will print the member 'desc' of the specified QError object,
368 * it uses error_report() for this, so that the output is routed to the right
369 * place (ie. stderr or Monitor's device).
370 */
371 void qerror_print(QError *qerror)
372 {
373 QString *qstring = qerror_human(qerror);
374 loc_push_restore(&qerror->loc);
375 error_report("%s", qstring_get_str(qstring));
376 loc_pop(&qerror->loc);
377 QDECREF(qstring);
378 }
379
380 /**
381 * qobject_to_qerror(): Convert a QObject into a QError
382 */
383 QError *qobject_to_qerror(const QObject *obj)
384 {
385 if (qobject_type(obj) != QTYPE_QERROR) {
386 return NULL;
387 }
388
389 return container_of(obj, QError, base);
390 }
391
392 /**
393 * qerror_destroy_obj(): Free all memory allocated by a QError
394 */
395 static void qerror_destroy_obj(QObject *obj)
396 {
397 QError *qerr;
398
399 assert(obj != NULL);
400 qerr = qobject_to_qerror(obj);
401
402 QDECREF(qerr->error);
403 qemu_free(qerr);
404 }