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