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