root / qerror.c @ b50a3a21
History | View | Annotate | Download (7.1 kB)
1 | 9f9daf9a | Luiz Capitulino | /*
|
---|---|---|---|
2 | 9f9daf9a | Luiz Capitulino | * QError: QEMU Error data-type.
|
3 | 9f9daf9a | Luiz Capitulino | *
|
4 | 9f9daf9a | Luiz Capitulino | * Copyright (C) 2009 Red Hat Inc.
|
5 | 9f9daf9a | Luiz Capitulino | *
|
6 | 9f9daf9a | Luiz Capitulino | * Authors:
|
7 | 9f9daf9a | Luiz Capitulino | * Luiz Capitulino <lcapitulino@redhat.com>
|
8 | 9f9daf9a | Luiz Capitulino | *
|
9 | 9f9daf9a | Luiz Capitulino | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
10 | 9f9daf9a | Luiz Capitulino | * See the COPYING.LIB file in the top-level directory.
|
11 | 9f9daf9a | Luiz Capitulino | */
|
12 | 9f9daf9a | Luiz Capitulino | #include "qjson.h" |
13 | 9f9daf9a | Luiz Capitulino | #include "qerror.h" |
14 | 9f9daf9a | Luiz Capitulino | #include "qstring.h" |
15 | 9f9daf9a | Luiz Capitulino | #include "sysemu.h" |
16 | 9f9daf9a | Luiz Capitulino | #include "qemu-common.h" |
17 | 9f9daf9a | Luiz Capitulino | |
18 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj); |
19 | 9f9daf9a | Luiz Capitulino | |
20 | 9f9daf9a | Luiz Capitulino | static const QType qerror_type = { |
21 | 9f9daf9a | Luiz Capitulino | .code = QTYPE_QERROR, |
22 | 9f9daf9a | Luiz Capitulino | .destroy = qerror_destroy_obj, |
23 | 9f9daf9a | Luiz Capitulino | }; |
24 | 9f9daf9a | Luiz Capitulino | |
25 | 9f9daf9a | Luiz Capitulino | /**
|
26 | 9f9daf9a | Luiz Capitulino | * The 'desc' parameter is a printf-like string, the format of the format
|
27 | 9f9daf9a | Luiz Capitulino | * string is:
|
28 | 9f9daf9a | Luiz Capitulino | *
|
29 | 9f9daf9a | Luiz Capitulino | * %(KEY)
|
30 | 9f9daf9a | Luiz Capitulino | *
|
31 | 9f9daf9a | Luiz Capitulino | * Where KEY is a QDict key, which has to be passed to qerror_from_info().
|
32 | 9f9daf9a | Luiz Capitulino | *
|
33 | 9f9daf9a | Luiz Capitulino | * Example:
|
34 | 9f9daf9a | Luiz Capitulino | *
|
35 | 9f9daf9a | Luiz Capitulino | * "foo error on device: %(device) slot: %(slot_nr)"
|
36 | 9f9daf9a | Luiz Capitulino | *
|
37 | 9f9daf9a | Luiz Capitulino | * A single percent sign can be printed if followed by a second one,
|
38 | 9f9daf9a | Luiz Capitulino | * for example:
|
39 | 9f9daf9a | Luiz Capitulino | *
|
40 | 9f9daf9a | Luiz Capitulino | * "running out of foo: %(foo)%%"
|
41 | 9f9daf9a | Luiz Capitulino | */
|
42 | 9f9daf9a | Luiz Capitulino | const QErrorStringTable qerror_table[] = {
|
43 | 357b6156 | Luiz Capitulino | { |
44 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_COMMAND_NOT_FOUND, |
45 | 4b9d4683 | Luiz Capitulino | .desc = "The command %(name) has not been found",
|
46 | 4b9d4683 | Luiz Capitulino | }, |
47 | 4b9d4683 | Luiz Capitulino | { |
48 | 357b6156 | Luiz Capitulino | .error_fmt = QERR_DEVICE_NOT_FOUND, |
49 | 357b6156 | Luiz Capitulino | .desc = "The %(device) device has not been found",
|
50 | 357b6156 | Luiz Capitulino | }, |
51 | 055f6122 | Luiz Capitulino | { |
52 | 055f6122 | Luiz Capitulino | .error_fmt = QERR_DEVICE_NOT_ACTIVE, |
53 | 055f6122 | Luiz Capitulino | .desc = "The %(device) device has not been activated by the guest",
|
54 | 055f6122 | Luiz Capitulino | }, |
55 | 82a60711 | Luiz Capitulino | { |
56 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_INVALID_PARAMETER_TYPE, |
57 | 4b9d4683 | Luiz Capitulino | .desc = "Invalid parameter type, expected: %(expected)",
|
58 | 4b9d4683 | Luiz Capitulino | }, |
59 | 4b9d4683 | Luiz Capitulino | { |
60 | 82a60711 | Luiz Capitulino | .error_fmt = QERR_KVM_MISSING_CAP, |
61 | 82a60711 | Luiz Capitulino | .desc = "Using KVM without %(capability), %(feature) unavailable",
|
62 | 82a60711 | Luiz Capitulino | }, |
63 | 4b9d4683 | Luiz Capitulino | { |
64 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_MISSING_PARAMETER, |
65 | 4b9d4683 | Luiz Capitulino | .desc = "Parameter %(name) is missing",
|
66 | 4b9d4683 | Luiz Capitulino | }, |
67 | 4b9d4683 | Luiz Capitulino | { |
68 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, |
69 | 4b9d4683 | Luiz Capitulino | .desc = "Bad QMP input object",
|
70 | 4b9d4683 | Luiz Capitulino | }, |
71 | 4b9d4683 | Luiz Capitulino | { |
72 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_JSON_PARSING, |
73 | 4b9d4683 | Luiz Capitulino | .desc = "Invalid JSON synaxt",
|
74 | 4b9d4683 | Luiz Capitulino | }, |
75 | 4b9d4683 | Luiz Capitulino | { |
76 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_UNDEFINED_ERROR, |
77 | 4b9d4683 | Luiz Capitulino | .desc = "An undefined error has ocurred",
|
78 | 4b9d4683 | Luiz Capitulino | }, |
79 | 9f9daf9a | Luiz Capitulino | {} |
80 | 9f9daf9a | Luiz Capitulino | }; |
81 | 9f9daf9a | Luiz Capitulino | |
82 | 9f9daf9a | Luiz Capitulino | /**
|
83 | 9f9daf9a | Luiz Capitulino | * qerror_new(): Create a new QError
|
84 | 9f9daf9a | Luiz Capitulino | *
|
85 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
86 | 9f9daf9a | Luiz Capitulino | */
|
87 | 9f9daf9a | Luiz Capitulino | QError *qerror_new(void)
|
88 | 9f9daf9a | Luiz Capitulino | { |
89 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
90 | 9f9daf9a | Luiz Capitulino | |
91 | 9f9daf9a | Luiz Capitulino | qerr = qemu_mallocz(sizeof(*qerr));
|
92 | 9f9daf9a | Luiz Capitulino | QOBJECT_INIT(qerr, &qerror_type); |
93 | 9f9daf9a | Luiz Capitulino | |
94 | 9f9daf9a | Luiz Capitulino | return qerr;
|
95 | 9f9daf9a | Luiz Capitulino | } |
96 | 9f9daf9a | Luiz Capitulino | |
97 | 9f9daf9a | Luiz Capitulino | static void qerror_abort(const QError *qerr, const char *fmt, ...) |
98 | 9f9daf9a | Luiz Capitulino | { |
99 | 9f9daf9a | Luiz Capitulino | va_list ap; |
100 | 9f9daf9a | Luiz Capitulino | |
101 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
|
102 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: -> ");
|
103 | 9f9daf9a | Luiz Capitulino | |
104 | 9f9daf9a | Luiz Capitulino | va_start(ap, fmt); |
105 | 9f9daf9a | Luiz Capitulino | vfprintf(stderr, fmt, ap); |
106 | 9f9daf9a | Luiz Capitulino | va_end(ap); |
107 | 9f9daf9a | Luiz Capitulino | |
108 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
|
109 | 9f9daf9a | Luiz Capitulino | abort(); |
110 | 9f9daf9a | Luiz Capitulino | } |
111 | 9f9daf9a | Luiz Capitulino | |
112 | 9f9daf9a | Luiz Capitulino | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) |
113 | 9f9daf9a | Luiz Capitulino | { |
114 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
115 | 9f9daf9a | Luiz Capitulino | |
116 | 9f9daf9a | Luiz Capitulino | obj = qobject_from_jsonv(fmt, va); |
117 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
118 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "invalid format '%s'", fmt);
|
119 | 9f9daf9a | Luiz Capitulino | } |
120 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
121 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
|
122 | 9f9daf9a | Luiz Capitulino | } |
123 | 9f9daf9a | Luiz Capitulino | |
124 | 9f9daf9a | Luiz Capitulino | qerr->error = qobject_to_qdict(obj); |
125 | 9f9daf9a | Luiz Capitulino | |
126 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "class");
|
127 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
128 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
|
129 | 9f9daf9a | Luiz Capitulino | } |
130 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QSTRING) {
|
131 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'class' key value should be a QString");
|
132 | 9f9daf9a | Luiz Capitulino | } |
133 | 9f9daf9a | Luiz Capitulino | |
134 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "data");
|
135 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
136 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
|
137 | 9f9daf9a | Luiz Capitulino | } |
138 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
139 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'data' key value should be a QDICT");
|
140 | 9f9daf9a | Luiz Capitulino | } |
141 | 9f9daf9a | Luiz Capitulino | } |
142 | 9f9daf9a | Luiz Capitulino | |
143 | 9f9daf9a | Luiz Capitulino | static void qerror_set_desc(QError *qerr, const char *fmt) |
144 | 9f9daf9a | Luiz Capitulino | { |
145 | 9f9daf9a | Luiz Capitulino | int i;
|
146 | 9f9daf9a | Luiz Capitulino | |
147 | 9f9daf9a | Luiz Capitulino | // FIXME: inefficient loop
|
148 | 9f9daf9a | Luiz Capitulino | |
149 | 9f9daf9a | Luiz Capitulino | for (i = 0; qerror_table[i].error_fmt; i++) { |
150 | 9f9daf9a | Luiz Capitulino | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
151 | 9f9daf9a | Luiz Capitulino | qerr->entry = &qerror_table[i]; |
152 | 9f9daf9a | Luiz Capitulino | return;
|
153 | 9f9daf9a | Luiz Capitulino | } |
154 | 9f9daf9a | Luiz Capitulino | } |
155 | 9f9daf9a | Luiz Capitulino | |
156 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format '%s' not found", fmt);
|
157 | 9f9daf9a | Luiz Capitulino | } |
158 | 9f9daf9a | Luiz Capitulino | |
159 | 9f9daf9a | Luiz Capitulino | /**
|
160 | 9f9daf9a | Luiz Capitulino | * qerror_from_info(): Create a new QError from error information
|
161 | 9f9daf9a | Luiz Capitulino | *
|
162 | 9f9daf9a | Luiz Capitulino | * The information consists of:
|
163 | 9f9daf9a | Luiz Capitulino | *
|
164 | 9f9daf9a | Luiz Capitulino | * - file the file name of where the error occurred
|
165 | 9f9daf9a | Luiz Capitulino | * - linenr the line number of where the error occurred
|
166 | 9f9daf9a | Luiz Capitulino | * - func the function name of where the error occurred
|
167 | 9f9daf9a | Luiz Capitulino | * - fmt JSON printf-like dictionary, there must exist keys 'class' and
|
168 | 9f9daf9a | Luiz Capitulino | * 'data'
|
169 | 9f9daf9a | Luiz Capitulino | * - va va_list of all arguments specified by fmt
|
170 | 9f9daf9a | Luiz Capitulino | *
|
171 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
172 | 9f9daf9a | Luiz Capitulino | */
|
173 | 9f9daf9a | Luiz Capitulino | QError *qerror_from_info(const char *file, int linenr, const char *func, |
174 | 9f9daf9a | Luiz Capitulino | const char *fmt, va_list *va) |
175 | 9f9daf9a | Luiz Capitulino | { |
176 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
177 | 9f9daf9a | Luiz Capitulino | |
178 | 9f9daf9a | Luiz Capitulino | qerr = qerror_new(); |
179 | 9f9daf9a | Luiz Capitulino | qerr->linenr = linenr; |
180 | 9f9daf9a | Luiz Capitulino | qerr->file = file; |
181 | 9f9daf9a | Luiz Capitulino | qerr->func = func; |
182 | 9f9daf9a | Luiz Capitulino | |
183 | 9f9daf9a | Luiz Capitulino | if (!fmt) {
|
184 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "QDict not specified");
|
185 | 9f9daf9a | Luiz Capitulino | } |
186 | 9f9daf9a | Luiz Capitulino | |
187 | 9f9daf9a | Luiz Capitulino | qerror_set_data(qerr, fmt, va); |
188 | 9f9daf9a | Luiz Capitulino | qerror_set_desc(qerr, fmt); |
189 | 9f9daf9a | Luiz Capitulino | |
190 | 9f9daf9a | Luiz Capitulino | return qerr;
|
191 | 9f9daf9a | Luiz Capitulino | } |
192 | 9f9daf9a | Luiz Capitulino | |
193 | 9f9daf9a | Luiz Capitulino | static void parse_error(const QError *qerror, int c) |
194 | 9f9daf9a | Luiz Capitulino | { |
195 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
|
196 | 9f9daf9a | Luiz Capitulino | } |
197 | 9f9daf9a | Luiz Capitulino | |
198 | 9f9daf9a | Luiz Capitulino | static const char *append_field(QString *outstr, const QError *qerror, |
199 | 9f9daf9a | Luiz Capitulino | const char *start) |
200 | 9f9daf9a | Luiz Capitulino | { |
201 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
202 | 9f9daf9a | Luiz Capitulino | QDict *qdict; |
203 | 9f9daf9a | Luiz Capitulino | QString *key_qs; |
204 | 9f9daf9a | Luiz Capitulino | const char *end, *key; |
205 | 9f9daf9a | Luiz Capitulino | |
206 | 9f9daf9a | Luiz Capitulino | if (*start != '%') |
207 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '%');
|
208 | 9f9daf9a | Luiz Capitulino | start++; |
209 | 9f9daf9a | Luiz Capitulino | if (*start != '(') |
210 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '(');
|
211 | 9f9daf9a | Luiz Capitulino | start++; |
212 | 9f9daf9a | Luiz Capitulino | |
213 | 9f9daf9a | Luiz Capitulino | end = strchr(start, ')');
|
214 | 9f9daf9a | Luiz Capitulino | if (!end)
|
215 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, ')');
|
216 | 9f9daf9a | Luiz Capitulino | |
217 | 9f9daf9a | Luiz Capitulino | key_qs = qstring_from_substr(start, 0, end - start - 1); |
218 | 9f9daf9a | Luiz Capitulino | key = qstring_get_str(key_qs); |
219 | 9f9daf9a | Luiz Capitulino | |
220 | 9f9daf9a | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
|
221 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qdict, key); |
222 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
223 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "key '%s' not found in QDict", key);
|
224 | 9f9daf9a | Luiz Capitulino | } |
225 | 9f9daf9a | Luiz Capitulino | |
226 | 9f9daf9a | Luiz Capitulino | switch (qobject_type(obj)) {
|
227 | 9f9daf9a | Luiz Capitulino | case QTYPE_QSTRING:
|
228 | 9f9daf9a | Luiz Capitulino | qstring_append(outstr, qdict_get_str(qdict, key)); |
229 | 9f9daf9a | Luiz Capitulino | break;
|
230 | 9f9daf9a | Luiz Capitulino | case QTYPE_QINT:
|
231 | 9f9daf9a | Luiz Capitulino | qstring_append_int(outstr, qdict_get_int(qdict, key)); |
232 | 9f9daf9a | Luiz Capitulino | break;
|
233 | 9f9daf9a | Luiz Capitulino | default:
|
234 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
|
235 | 9f9daf9a | Luiz Capitulino | } |
236 | 9f9daf9a | Luiz Capitulino | |
237 | 9f9daf9a | Luiz Capitulino | QDECREF(key_qs); |
238 | 9f9daf9a | Luiz Capitulino | return ++end;
|
239 | 9f9daf9a | Luiz Capitulino | } |
240 | 9f9daf9a | Luiz Capitulino | |
241 | 9f9daf9a | Luiz Capitulino | /**
|
242 | 9f9daf9a | Luiz Capitulino | * qerror_print(): Print QError data
|
243 | 9f9daf9a | Luiz Capitulino | *
|
244 | 9f9daf9a | Luiz Capitulino | * This function will print the member 'desc' of the specified QError object,
|
245 | 9f9daf9a | Luiz Capitulino | * it uses qemu_error() for this, so that the output is routed to the right
|
246 | 9f9daf9a | Luiz Capitulino | * place (ie. stderr or Monitor's device).
|
247 | 9f9daf9a | Luiz Capitulino | */
|
248 | 9f9daf9a | Luiz Capitulino | void qerror_print(const QError *qerror) |
249 | 9f9daf9a | Luiz Capitulino | { |
250 | 9f9daf9a | Luiz Capitulino | const char *p; |
251 | 9f9daf9a | Luiz Capitulino | QString *qstring; |
252 | 9f9daf9a | Luiz Capitulino | |
253 | 9f9daf9a | Luiz Capitulino | assert(qerror->entry != NULL);
|
254 | 9f9daf9a | Luiz Capitulino | |
255 | 9f9daf9a | Luiz Capitulino | qstring = qstring_new(); |
256 | 9f9daf9a | Luiz Capitulino | |
257 | 9f9daf9a | Luiz Capitulino | for (p = qerror->entry->desc; *p != '\0';) { |
258 | 9f9daf9a | Luiz Capitulino | if (*p != '%') { |
259 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, *p++); |
260 | 9f9daf9a | Luiz Capitulino | } else if (*(p + 1) == '%') { |
261 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, '%');
|
262 | 9f9daf9a | Luiz Capitulino | p += 2;
|
263 | 9f9daf9a | Luiz Capitulino | } else {
|
264 | 9f9daf9a | Luiz Capitulino | p = append_field(qstring, qerror, p); |
265 | 9f9daf9a | Luiz Capitulino | } |
266 | 9f9daf9a | Luiz Capitulino | } |
267 | 9f9daf9a | Luiz Capitulino | |
268 | 9f9daf9a | Luiz Capitulino | qemu_error("%s\n", qstring_get_str(qstring));
|
269 | 9f9daf9a | Luiz Capitulino | QDECREF(qstring); |
270 | 9f9daf9a | Luiz Capitulino | } |
271 | 9f9daf9a | Luiz Capitulino | |
272 | 9f9daf9a | Luiz Capitulino | /**
|
273 | 9f9daf9a | Luiz Capitulino | * qobject_to_qerror(): Convert a QObject into a QError
|
274 | 9f9daf9a | Luiz Capitulino | */
|
275 | 9f9daf9a | Luiz Capitulino | QError *qobject_to_qerror(const QObject *obj)
|
276 | 9f9daf9a | Luiz Capitulino | { |
277 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QERROR) {
|
278 | 9f9daf9a | Luiz Capitulino | return NULL; |
279 | 9f9daf9a | Luiz Capitulino | } |
280 | 9f9daf9a | Luiz Capitulino | |
281 | 9f9daf9a | Luiz Capitulino | return container_of(obj, QError, base);
|
282 | 9f9daf9a | Luiz Capitulino | } |
283 | 9f9daf9a | Luiz Capitulino | |
284 | 9f9daf9a | Luiz Capitulino | /**
|
285 | 9f9daf9a | Luiz Capitulino | * qerror_destroy_obj(): Free all memory allocated by a QError
|
286 | 9f9daf9a | Luiz Capitulino | */
|
287 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj) |
288 | 9f9daf9a | Luiz Capitulino | { |
289 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
290 | 9f9daf9a | Luiz Capitulino | |
291 | 9f9daf9a | Luiz Capitulino | assert(obj != NULL);
|
292 | 9f9daf9a | Luiz Capitulino | qerr = qobject_to_qerror(obj); |
293 | 9f9daf9a | Luiz Capitulino | |
294 | 9f9daf9a | Luiz Capitulino | QDECREF(qerr->error); |
295 | 9f9daf9a | Luiz Capitulino | qemu_free(qerr); |
296 | 9f9daf9a | Luiz Capitulino | } |