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