2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
15 #include "qemu-common.h"
16 #include "qemu-error.h"
18 static void qerror_destroy_obj(QObject
*obj
);
20 static const QType qerror_type
= {
22 .destroy
= qerror_destroy_obj
,
26 * The 'desc' parameter is a printf-like string, the format of the format
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
40 * "running out of foo: %(foo)%%"
42 static const QErrorStringTable qerror_table
[] = {
44 .error_fmt
= QERR_COMMAND_NOT_FOUND
,
45 .desc
= "The command %(name) has not been found",
48 .error_fmt
= QERR_DEVICE_ENCRYPTED
,
49 .desc
= "Device '%(device)' is encrypted",
52 .error_fmt
= QERR_DEVICE_LOCKED
,
53 .desc
= "Device '%(device)' is locked",
56 .error_fmt
= QERR_DEVICE_NOT_ACTIVE
,
57 .desc
= "Device '%(device)' has not been activated by the guest",
60 .error_fmt
= QERR_DEVICE_NOT_FOUND
,
61 .desc
= "Device '%(device)' not found",
64 .error_fmt
= QERR_DEVICE_NOT_REMOVABLE
,
65 .desc
= "Device '%(device)' is not removable",
68 .error_fmt
= QERR_FD_NOT_FOUND
,
69 .desc
= "File descriptor named '%(name)' not found",
72 .error_fmt
= QERR_FD_NOT_SUPPLIED
,
73 .desc
= "No file descriptor supplied via SCM_RIGHTS",
76 .error_fmt
= QERR_INVALID_BLOCK_FORMAT
,
77 .desc
= "Invalid block format '%(name)'",
80 .error_fmt
= QERR_INVALID_PARAMETER
,
81 .desc
= "Invalid parameter '%(name)'",
84 .error_fmt
= QERR_INVALID_PARAMETER_TYPE
,
85 .desc
= "Invalid parameter type, expected: %(expected)",
88 .error_fmt
= QERR_INVALID_PASSWORD
,
89 .desc
= "Password incorrect",
92 .error_fmt
= QERR_JSON_PARSING
,
93 .desc
= "Invalid JSON syntax",
96 .error_fmt
= QERR_KVM_MISSING_CAP
,
97 .desc
= "Using KVM without %(capability), %(feature) unavailable",
100 .error_fmt
= QERR_MISSING_PARAMETER
,
101 .desc
= "Parameter '%(name)' is missing",
104 .error_fmt
= QERR_OPEN_FILE_FAILED
,
105 .desc
= "Could not open '%(filename)'",
108 .error_fmt
= QERR_PROPERTY_NOT_FOUND
,
109 .desc
= "Property '%(device).%(property)' not found",
112 .error_fmt
= QERR_QMP_BAD_INPUT_OBJECT
,
113 .desc
= "Bad QMP input object",
116 .error_fmt
= QERR_SET_PASSWD_FAILED
,
117 .desc
= "Could not set password",
120 .error_fmt
= QERR_TOO_MANY_FILES
,
121 .desc
= "Too many open files",
124 .error_fmt
= QERR_UNDEFINED_ERROR
,
125 .desc
= "An undefined error has ocurred",
128 .error_fmt
= QERR_VNC_SERVER_FAILED
,
129 .desc
= "Could not start VNC server on %(target)",
135 * qerror_new(): Create a new QError
137 * Return strong reference.
139 QError
*qerror_new(void)
143 qerr
= qemu_mallocz(sizeof(*qerr
));
144 QOBJECT_INIT(qerr
, &qerror_type
);
149 static void qerror_abort(const QError
*qerr
, const char *fmt
, ...)
153 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
154 fprintf(stderr
, "qerror: -> ");
157 vfprintf(stderr
, fmt
, ap
);
160 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
164 static void qerror_set_data(QError
*qerr
, const char *fmt
, va_list *va
)
168 obj
= qobject_from_jsonv(fmt
, va
);
170 qerror_abort(qerr
, "invalid format '%s'", fmt
);
172 if (qobject_type(obj
) != QTYPE_QDICT
) {
173 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
176 qerr
->error
= qobject_to_qdict(obj
);
178 obj
= qdict_get(qerr
->error
, "class");
180 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
182 if (qobject_type(obj
) != QTYPE_QSTRING
) {
183 qerror_abort(qerr
, "'class' key value should be a QString");
186 obj
= qdict_get(qerr
->error
, "data");
188 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
190 if (qobject_type(obj
) != QTYPE_QDICT
) {
191 qerror_abort(qerr
, "'data' key value should be a QDICT");
195 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
199 // FIXME: inefficient loop
201 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
202 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
203 qerr
->entry
= &qerror_table
[i
];
208 qerror_abort(qerr
, "error format '%s' not found", fmt
);
212 * qerror_from_info(): Create a new QError from error information
214 * The information consists of:
216 * - file the file name of where the error occurred
217 * - linenr the line number of where the error occurred
218 * - func the function name of where the error occurred
219 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
221 * - va va_list of all arguments specified by fmt
223 * Return strong reference.
225 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
226 const char *fmt
, va_list *va
)
231 loc_save(&qerr
->loc
);
232 qerr
->linenr
= linenr
;
237 qerror_abort(qerr
, "QDict not specified");
240 qerror_set_data(qerr
, fmt
, va
);
241 qerror_set_desc(qerr
, fmt
);
246 static void parse_error(const QError
*qerror
, int c
)
248 qerror_abort(qerror
, "expected '%c' in '%s'", c
, qerror
->entry
->desc
);
251 static const char *append_field(QString
*outstr
, const QError
*qerror
,
257 const char *end
, *key
;
260 parse_error(qerror
, '%');
263 parse_error(qerror
, '(');
266 end
= strchr(start
, ')');
268 parse_error(qerror
, ')');
270 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
271 key
= qstring_get_str(key_qs
);
273 qdict
= qobject_to_qdict(qdict_get(qerror
->error
, "data"));
274 obj
= qdict_get(qdict
, key
);
276 qerror_abort(qerror
, "key '%s' not found in QDict", key
);
279 switch (qobject_type(obj
)) {
281 qstring_append(outstr
, qdict_get_str(qdict
, key
));
284 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
287 qerror_abort(qerror
, "invalid type '%c'", qobject_type(obj
));
295 * qerror_human(): Format QError data into human-readable string.
297 * Formats according to member 'desc' of the specified QError object.
299 QString
*qerror_human(const QError
*qerror
)
304 assert(qerror
->entry
!= NULL
);
306 qstring
= qstring_new();
308 for (p
= qerror
->entry
->desc
; *p
!= '\0';) {
310 qstring_append_chr(qstring
, *p
++);
311 } else if (*(p
+ 1) == '%') {
312 qstring_append_chr(qstring
, '%');
315 p
= append_field(qstring
, qerror
, p
);
323 * qerror_print(): Print QError data
325 * This function will print the member 'desc' of the specified QError object,
326 * it uses error_report() for this, so that the output is routed to the right
327 * place (ie. stderr or Monitor's device).
329 void qerror_print(QError
*qerror
)
331 QString
*qstring
= qerror_human(qerror
);
332 loc_push_restore(&qerror
->loc
);
333 error_report("%s", qstring_get_str(qstring
));
334 loc_pop(&qerror
->loc
);
339 * qobject_to_qerror(): Convert a QObject into a QError
341 QError
*qobject_to_qerror(const QObject
*obj
)
343 if (qobject_type(obj
) != QTYPE_QERROR
) {
347 return container_of(obj
, QError
, base
);
351 * qerror_destroy_obj(): Free all memory allocated by a QError
353 static void qerror_destroy_obj(QObject
*obj
)
358 qerr
= qobject_to_qerror(obj
);
360 QDECREF(qerr
->error
);