]> git.proxmox.com Git - qemu.git/blob - qerror.c
Merge commit 'mst/pci' into staging
[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 "sysemu.h"
16 #include "qemu-common.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_COMMAND_NOT_FOUND,
45 .desc = "The command %(name) has not been found",
46 },
47 {
48 .error_fmt = QERR_DEVICE_NOT_FOUND,
49 .desc = "The %(device) device has not been found",
50 },
51 {
52 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
53 .desc = "The %(device) device has not been activated by the guest",
54 },
55 {
56 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
57 .desc = "Invalid parameter type, expected: %(expected)",
58 },
59 {
60 .error_fmt = QERR_INVALID_PASSWORD,
61 .desc = "The entered password is invalid",
62 },
63 {
64 .error_fmt = QERR_KVM_MISSING_CAP,
65 .desc = "Using KVM without %(capability), %(feature) unavailable",
66 },
67 {
68 .error_fmt = QERR_MISSING_PARAMETER,
69 .desc = "Parameter %(name) is missing",
70 },
71 {
72 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
73 .desc = "Bad QMP input object",
74 },
75 {
76 .error_fmt = QERR_JSON_PARSING,
77 .desc = "Invalid JSON synaxt",
78 },
79 {
80 .error_fmt = QERR_UNDEFINED_ERROR,
81 .desc = "An undefined error has ocurred",
82 },
83 {}
84 };
85
86 /**
87 * qerror_new(): Create a new QError
88 *
89 * Return strong reference.
90 */
91 QError *qerror_new(void)
92 {
93 QError *qerr;
94
95 qerr = qemu_mallocz(sizeof(*qerr));
96 QOBJECT_INIT(qerr, &qerror_type);
97
98 return qerr;
99 }
100
101 static void qerror_abort(const QError *qerr, const char *fmt, ...)
102 {
103 va_list ap;
104
105 fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
106 fprintf(stderr, "qerror: -> ");
107
108 va_start(ap, fmt);
109 vfprintf(stderr, fmt, ap);
110 va_end(ap);
111
112 fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
113 abort();
114 }
115
116 static void qerror_set_data(QError *qerr, const char *fmt, va_list *va)
117 {
118 QObject *obj;
119
120 obj = qobject_from_jsonv(fmt, va);
121 if (!obj) {
122 qerror_abort(qerr, "invalid format '%s'", fmt);
123 }
124 if (qobject_type(obj) != QTYPE_QDICT) {
125 qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
126 }
127
128 qerr->error = qobject_to_qdict(obj);
129
130 obj = qdict_get(qerr->error, "class");
131 if (!obj) {
132 qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
133 }
134 if (qobject_type(obj) != QTYPE_QSTRING) {
135 qerror_abort(qerr, "'class' key value should be a QString");
136 }
137
138 obj = qdict_get(qerr->error, "data");
139 if (!obj) {
140 qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
141 }
142 if (qobject_type(obj) != QTYPE_QDICT) {
143 qerror_abort(qerr, "'data' key value should be a QDICT");
144 }
145 }
146
147 static void qerror_set_desc(QError *qerr, const char *fmt)
148 {
149 int i;
150
151 // FIXME: inefficient loop
152
153 for (i = 0; qerror_table[i].error_fmt; i++) {
154 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
155 qerr->entry = &qerror_table[i];
156 return;
157 }
158 }
159
160 qerror_abort(qerr, "error format '%s' not found", fmt);
161 }
162
163 /**
164 * qerror_from_info(): Create a new QError from error information
165 *
166 * The information consists of:
167 *
168 * - file the file name of where the error occurred
169 * - linenr the line number of where the error occurred
170 * - func the function name of where the error occurred
171 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
172 * 'data'
173 * - va va_list of all arguments specified by fmt
174 *
175 * Return strong reference.
176 */
177 QError *qerror_from_info(const char *file, int linenr, const char *func,
178 const char *fmt, va_list *va)
179 {
180 QError *qerr;
181
182 qerr = qerror_new();
183 qerr->linenr = linenr;
184 qerr->file = file;
185 qerr->func = func;
186
187 if (!fmt) {
188 qerror_abort(qerr, "QDict not specified");
189 }
190
191 qerror_set_data(qerr, fmt, va);
192 qerror_set_desc(qerr, fmt);
193
194 return qerr;
195 }
196
197 static void parse_error(const QError *qerror, int c)
198 {
199 qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
200 }
201
202 static const char *append_field(QString *outstr, const QError *qerror,
203 const char *start)
204 {
205 QObject *obj;
206 QDict *qdict;
207 QString *key_qs;
208 const char *end, *key;
209
210 if (*start != '%')
211 parse_error(qerror, '%');
212 start++;
213 if (*start != '(')
214 parse_error(qerror, '(');
215 start++;
216
217 end = strchr(start, ')');
218 if (!end)
219 parse_error(qerror, ')');
220
221 key_qs = qstring_from_substr(start, 0, end - start - 1);
222 key = qstring_get_str(key_qs);
223
224 qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
225 obj = qdict_get(qdict, key);
226 if (!obj) {
227 qerror_abort(qerror, "key '%s' not found in QDict", key);
228 }
229
230 switch (qobject_type(obj)) {
231 case QTYPE_QSTRING:
232 qstring_append(outstr, qdict_get_str(qdict, key));
233 break;
234 case QTYPE_QINT:
235 qstring_append_int(outstr, qdict_get_int(qdict, key));
236 break;
237 default:
238 qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
239 }
240
241 QDECREF(key_qs);
242 return ++end;
243 }
244
245 /**
246 * qerror_print(): Print QError data
247 *
248 * This function will print the member 'desc' of the specified QError object,
249 * it uses qemu_error() for this, so that the output is routed to the right
250 * place (ie. stderr or Monitor's device).
251 */
252 void qerror_print(const QError *qerror)
253 {
254 const char *p;
255 QString *qstring;
256
257 assert(qerror->entry != NULL);
258
259 qstring = qstring_new();
260
261 for (p = qerror->entry->desc; *p != '\0';) {
262 if (*p != '%') {
263 qstring_append_chr(qstring, *p++);
264 } else if (*(p + 1) == '%') {
265 qstring_append_chr(qstring, '%');
266 p += 2;
267 } else {
268 p = append_field(qstring, qerror, p);
269 }
270 }
271
272 qemu_error("%s\n", qstring_get_str(qstring));
273 QDECREF(qstring);
274 }
275
276 /**
277 * qobject_to_qerror(): Convert a QObject into a QError
278 */
279 QError *qobject_to_qerror(const QObject *obj)
280 {
281 if (qobject_type(obj) != QTYPE_QERROR) {
282 return NULL;
283 }
284
285 return container_of(obj, QError, base);
286 }
287
288 /**
289 * qerror_destroy_obj(): Free all memory allocated by a QError
290 */
291 static void qerror_destroy_obj(QObject *obj)
292 {
293 QError *qerr;
294
295 assert(obj != NULL);
296 qerr = qobject_to_qerror(obj);
297
298 QDECREF(qerr->error);
299 qemu_free(qerr);
300 }