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