root / qerror.c @ 4f78c9ad
History | View | Annotate | Download (10.6 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 | 0167f772 | Markus Armbruster | |
13 | 0167f772 | Markus Armbruster | #include "monitor.h" |
14 | 9f9daf9a | Luiz Capitulino | #include "qjson.h" |
15 | 9f9daf9a | Luiz Capitulino | #include "qerror.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 | d05ac8fa | Blue Swirl | static const QErrorStringTable qerror_table[] = { |
43 | 357b6156 | Luiz Capitulino | { |
44 | 56e9f563 | Markus Armbruster | .error_fmt = QERR_BAD_BUS_FOR_DEVICE, |
45 | 56e9f563 | Markus Armbruster | .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
|
46 | 56e9f563 | Markus Armbruster | }, |
47 | 56e9f563 | Markus Armbruster | { |
48 | 7bbd8237 | Markus Armbruster | .error_fmt = QERR_BUS_NOT_FOUND, |
49 | 7bbd8237 | Markus Armbruster | .desc = "Bus '%(bus)' not found",
|
50 | 7bbd8237 | Markus Armbruster | }, |
51 | 7bbd8237 | Markus Armbruster | { |
52 | 8eae73b5 | Markus Armbruster | .error_fmt = QERR_BUS_NO_HOTPLUG, |
53 | 8eae73b5 | Markus Armbruster | .desc = "Bus '%(bus)' does not support hotplugging",
|
54 | 8eae73b5 | Markus Armbruster | }, |
55 | 8eae73b5 | Markus Armbruster | { |
56 | e16a1812 | Markus Armbruster | .error_fmt = QERR_COMMAND_NOT_FOUND, |
57 | e16a1812 | Markus Armbruster | .desc = "The command %(name) has not been found",
|
58 | 4b9d4683 | Luiz Capitulino | }, |
59 | 4b9d4683 | Luiz Capitulino | { |
60 | 0df37c41 | Luiz Capitulino | .error_fmt = QERR_DEVICE_ENCRYPTED, |
61 | fc5469d8 | Markus Armbruster | .desc = "Device '%(device)' is encrypted",
|
62 | 0df37c41 | Luiz Capitulino | }, |
63 | 0df37c41 | Luiz Capitulino | { |
64 | 4d9a1a15 | Markus Armbruster | .error_fmt = QERR_DEVICE_INIT_FAILED, |
65 | 4d9a1a15 | Markus Armbruster | .desc = "Device '%(device)' could not be initialized",
|
66 | 4d9a1a15 | Markus Armbruster | }, |
67 | 4d9a1a15 | Markus Armbruster | { |
68 | 25b28f01 | Shahar Havivi | .error_fmt = QERR_DEVICE_NOT_ENCRYPTED, |
69 | 25b28f01 | Shahar Havivi | .desc = "Device '%(device)' is not encrypted",
|
70 | 25b28f01 | Shahar Havivi | }, |
71 | 25b28f01 | Shahar Havivi | { |
72 | b0868380 | Markus Armbruster | .error_fmt = QERR_DEVICE_LOCKED, |
73 | fc5469d8 | Markus Armbruster | .desc = "Device '%(device)' is locked",
|
74 | b0868380 | Markus Armbruster | }, |
75 | 1ae78718 | Markus Armbruster | { |
76 | 1ae78718 | Markus Armbruster | .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES, |
77 | 1ae78718 | Markus Armbruster | .desc = "Device '%(device)' has multiple child busses",
|
78 | 1ae78718 | Markus Armbruster | }, |
79 | b0868380 | Markus Armbruster | { |
80 | e16a1812 | Markus Armbruster | .error_fmt = QERR_DEVICE_NOT_ACTIVE, |
81 | fc5469d8 | Markus Armbruster | .desc = "Device '%(device)' has not been activated by the guest",
|
82 | e16a1812 | Markus Armbruster | }, |
83 | e16a1812 | Markus Armbruster | { |
84 | 357b6156 | Luiz Capitulino | .error_fmt = QERR_DEVICE_NOT_FOUND, |
85 | fc5469d8 | Markus Armbruster | .desc = "Device '%(device)' not found",
|
86 | 357b6156 | Luiz Capitulino | }, |
87 | 055f6122 | Luiz Capitulino | { |
88 | 5cfe0264 | Markus Armbruster | .error_fmt = QERR_DEVICE_NOT_REMOVABLE, |
89 | fc5469d8 | Markus Armbruster | .desc = "Device '%(device)' is not removable",
|
90 | 5cfe0264 | Markus Armbruster | }, |
91 | 5cfe0264 | Markus Armbruster | { |
92 | 07574baf | Markus Armbruster | .error_fmt = QERR_DEVICE_NO_BUS, |
93 | 07574baf | Markus Armbruster | .desc = "Device '%(device)' has no child bus",
|
94 | 07574baf | Markus Armbruster | }, |
95 | 07574baf | Markus Armbruster | { |
96 | c7c338c4 | Markus Armbruster | .error_fmt = QERR_FD_NOT_FOUND, |
97 | fc5469d8 | Markus Armbruster | .desc = "File descriptor named '%(name)' not found",
|
98 | c7c338c4 | Markus Armbruster | }, |
99 | c7c338c4 | Markus Armbruster | { |
100 | 41471a23 | Markus Armbruster | .error_fmt = QERR_FD_NOT_SUPPLIED, |
101 | 41471a23 | Markus Armbruster | .desc = "No file descriptor supplied via SCM_RIGHTS",
|
102 | 41471a23 | Markus Armbruster | }, |
103 | 41471a23 | Markus Armbruster | { |
104 | 17901e75 | Markus Armbruster | .error_fmt = QERR_INVALID_BLOCK_FORMAT, |
105 | fc5469d8 | Markus Armbruster | .desc = "Invalid block format '%(name)'",
|
106 | 17901e75 | Markus Armbruster | }, |
107 | 17901e75 | Markus Armbruster | { |
108 | 7a046f5f | Markus Armbruster | .error_fmt = QERR_INVALID_PARAMETER, |
109 | fc5469d8 | Markus Armbruster | .desc = "Invalid parameter '%(name)'",
|
110 | 7a046f5f | Markus Armbruster | }, |
111 | 7a046f5f | Markus Armbruster | { |
112 | e16a1812 | Markus Armbruster | .error_fmt = QERR_INVALID_PARAMETER_TYPE, |
113 | e16a1812 | Markus Armbruster | .desc = "Invalid parameter type, expected: %(expected)",
|
114 | 055f6122 | Luiz Capitulino | }, |
115 | 82a60711 | Luiz Capitulino | { |
116 | e16a1812 | Markus Armbruster | .error_fmt = QERR_INVALID_PASSWORD, |
117 | fc5469d8 | Markus Armbruster | .desc = "Password incorrect",
|
118 | 4b9d4683 | Luiz Capitulino | }, |
119 | 4b9d4683 | Luiz Capitulino | { |
120 | e16a1812 | Markus Armbruster | .error_fmt = QERR_JSON_PARSING, |
121 | e16a1812 | Markus Armbruster | .desc = "Invalid JSON syntax",
|
122 | f6d855c5 | Luiz Capitulino | }, |
123 | f6d855c5 | Luiz Capitulino | { |
124 | 82a60711 | Luiz Capitulino | .error_fmt = QERR_KVM_MISSING_CAP, |
125 | 82a60711 | Luiz Capitulino | .desc = "Using KVM without %(capability), %(feature) unavailable",
|
126 | 82a60711 | Luiz Capitulino | }, |
127 | 4b9d4683 | Luiz Capitulino | { |
128 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_MISSING_PARAMETER, |
129 | fc5469d8 | Markus Armbruster | .desc = "Parameter '%(name)' is missing",
|
130 | fc5469d8 | Markus Armbruster | }, |
131 | fc5469d8 | Markus Armbruster | { |
132 | fab5767f | Markus Armbruster | .error_fmt = QERR_NO_BUS_FOR_DEVICE, |
133 | fab5767f | Markus Armbruster | .desc = "No '%(bus)' bus found for device '%(device)'",
|
134 | fab5767f | Markus Armbruster | }, |
135 | fab5767f | Markus Armbruster | { |
136 | fc5469d8 | Markus Armbruster | .error_fmt = QERR_OPEN_FILE_FAILED, |
137 | fc5469d8 | Markus Armbruster | .desc = "Could not open '%(filename)'",
|
138 | 4b9d4683 | Luiz Capitulino | }, |
139 | 4b9d4683 | Luiz Capitulino | { |
140 | c58a35f8 | Markus Armbruster | .error_fmt = QERR_PROPERTY_NOT_FOUND, |
141 | c58a35f8 | Markus Armbruster | .desc = "Property '%(device).%(property)' not found",
|
142 | c58a35f8 | Markus Armbruster | }, |
143 | c58a35f8 | Markus Armbruster | { |
144 | 06b4a703 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_BAD, |
145 | 06b4a703 | Markus Armbruster | .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
|
146 | 06b4a703 | Markus Armbruster | }, |
147 | 06b4a703 | Markus Armbruster | { |
148 | 9c5eff95 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_IN_USE, |
149 | 9c5eff95 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
|
150 | 9c5eff95 | Markus Armbruster | }, |
151 | 9c5eff95 | Markus Armbruster | { |
152 | 84745d68 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND, |
153 | 84745d68 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't find value '%(value)'",
|
154 | 84745d68 | Markus Armbruster | }, |
155 | 84745d68 | Markus Armbruster | { |
156 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, |
157 | 4b9d4683 | Luiz Capitulino | .desc = "Bad QMP input object",
|
158 | 4b9d4683 | Luiz Capitulino | }, |
159 | 4b9d4683 | Luiz Capitulino | { |
160 | 7a84cb23 | Markus Armbruster | .error_fmt = QERR_SET_PASSWD_FAILED, |
161 | 7a84cb23 | Markus Armbruster | .desc = "Could not set password",
|
162 | 7a84cb23 | Markus Armbruster | }, |
163 | 7a84cb23 | Markus Armbruster | { |
164 | a488be27 | Markus Armbruster | .error_fmt = QERR_TOO_MANY_FILES, |
165 | a488be27 | Markus Armbruster | .desc = "Too many open files",
|
166 | a488be27 | Markus Armbruster | }, |
167 | a488be27 | Markus Armbruster | { |
168 | e16a1812 | Markus Armbruster | .error_fmt = QERR_UNDEFINED_ERROR, |
169 | e16a1812 | Markus Armbruster | .desc = "An undefined error has ocurred",
|
170 | 4b9d4683 | Luiz Capitulino | }, |
171 | a6906e31 | Markus Armbruster | { |
172 | a6906e31 | Markus Armbruster | .error_fmt = QERR_VNC_SERVER_FAILED, |
173 | a6906e31 | Markus Armbruster | .desc = "Could not start VNC server on %(target)",
|
174 | a6906e31 | Markus Armbruster | }, |
175 | 9f9daf9a | Luiz Capitulino | {} |
176 | 9f9daf9a | Luiz Capitulino | }; |
177 | 9f9daf9a | Luiz Capitulino | |
178 | 9f9daf9a | Luiz Capitulino | /**
|
179 | 9f9daf9a | Luiz Capitulino | * qerror_new(): Create a new QError
|
180 | 9f9daf9a | Luiz Capitulino | *
|
181 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
182 | 9f9daf9a | Luiz Capitulino | */
|
183 | 9f9daf9a | Luiz Capitulino | QError *qerror_new(void)
|
184 | 9f9daf9a | Luiz Capitulino | { |
185 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
186 | 9f9daf9a | Luiz Capitulino | |
187 | 9f9daf9a | Luiz Capitulino | qerr = qemu_mallocz(sizeof(*qerr));
|
188 | 9f9daf9a | Luiz Capitulino | QOBJECT_INIT(qerr, &qerror_type); |
189 | 9f9daf9a | Luiz Capitulino | |
190 | 9f9daf9a | Luiz Capitulino | return qerr;
|
191 | 9f9daf9a | Luiz Capitulino | } |
192 | 9f9daf9a | Luiz Capitulino | |
193 | 9f9daf9a | Luiz Capitulino | static void qerror_abort(const QError *qerr, const char *fmt, ...) |
194 | 9f9daf9a | Luiz Capitulino | { |
195 | 9f9daf9a | Luiz Capitulino | va_list ap; |
196 | 9f9daf9a | Luiz Capitulino | |
197 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
|
198 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: -> ");
|
199 | 9f9daf9a | Luiz Capitulino | |
200 | 9f9daf9a | Luiz Capitulino | va_start(ap, fmt); |
201 | 9f9daf9a | Luiz Capitulino | vfprintf(stderr, fmt, ap); |
202 | 9f9daf9a | Luiz Capitulino | va_end(ap); |
203 | 9f9daf9a | Luiz Capitulino | |
204 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
|
205 | 9f9daf9a | Luiz Capitulino | abort(); |
206 | 9f9daf9a | Luiz Capitulino | } |
207 | 9f9daf9a | Luiz Capitulino | |
208 | 9f9daf9a | Luiz Capitulino | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) |
209 | 9f9daf9a | Luiz Capitulino | { |
210 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
211 | 9f9daf9a | Luiz Capitulino | |
212 | 9f9daf9a | Luiz Capitulino | obj = qobject_from_jsonv(fmt, va); |
213 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
214 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "invalid format '%s'", fmt);
|
215 | 9f9daf9a | Luiz Capitulino | } |
216 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
217 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
|
218 | 9f9daf9a | Luiz Capitulino | } |
219 | 9f9daf9a | Luiz Capitulino | |
220 | 9f9daf9a | Luiz Capitulino | qerr->error = qobject_to_qdict(obj); |
221 | 9f9daf9a | Luiz Capitulino | |
222 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "class");
|
223 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
224 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
|
225 | 9f9daf9a | Luiz Capitulino | } |
226 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QSTRING) {
|
227 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'class' key value should be a QString");
|
228 | 9f9daf9a | Luiz Capitulino | } |
229 | 9f9daf9a | Luiz Capitulino | |
230 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "data");
|
231 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
232 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
|
233 | 9f9daf9a | Luiz Capitulino | } |
234 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
235 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'data' key value should be a QDICT");
|
236 | 9f9daf9a | Luiz Capitulino | } |
237 | 9f9daf9a | Luiz Capitulino | } |
238 | 9f9daf9a | Luiz Capitulino | |
239 | 9f9daf9a | Luiz Capitulino | static void qerror_set_desc(QError *qerr, const char *fmt) |
240 | 9f9daf9a | Luiz Capitulino | { |
241 | 9f9daf9a | Luiz Capitulino | int i;
|
242 | 9f9daf9a | Luiz Capitulino | |
243 | 9f9daf9a | Luiz Capitulino | // FIXME: inefficient loop
|
244 | 9f9daf9a | Luiz Capitulino | |
245 | 9f9daf9a | Luiz Capitulino | for (i = 0; qerror_table[i].error_fmt; i++) { |
246 | 9f9daf9a | Luiz Capitulino | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
247 | 9f9daf9a | Luiz Capitulino | qerr->entry = &qerror_table[i]; |
248 | 9f9daf9a | Luiz Capitulino | return;
|
249 | 9f9daf9a | Luiz Capitulino | } |
250 | 9f9daf9a | Luiz Capitulino | } |
251 | 9f9daf9a | Luiz Capitulino | |
252 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format '%s' not found", fmt);
|
253 | 9f9daf9a | Luiz Capitulino | } |
254 | 9f9daf9a | Luiz Capitulino | |
255 | 9f9daf9a | Luiz Capitulino | /**
|
256 | 9f9daf9a | Luiz Capitulino | * qerror_from_info(): Create a new QError from error information
|
257 | 9f9daf9a | Luiz Capitulino | *
|
258 | 9f9daf9a | Luiz Capitulino | * The information consists of:
|
259 | 9f9daf9a | Luiz Capitulino | *
|
260 | 9f9daf9a | Luiz Capitulino | * - file the file name of where the error occurred
|
261 | 9f9daf9a | Luiz Capitulino | * - linenr the line number of where the error occurred
|
262 | 9f9daf9a | Luiz Capitulino | * - func the function name of where the error occurred
|
263 | 9f9daf9a | Luiz Capitulino | * - fmt JSON printf-like dictionary, there must exist keys 'class' and
|
264 | 9f9daf9a | Luiz Capitulino | * 'data'
|
265 | 9f9daf9a | Luiz Capitulino | * - va va_list of all arguments specified by fmt
|
266 | 9f9daf9a | Luiz Capitulino | *
|
267 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
268 | 9f9daf9a | Luiz Capitulino | */
|
269 | 9f9daf9a | Luiz Capitulino | QError *qerror_from_info(const char *file, int linenr, const char *func, |
270 | 9f9daf9a | Luiz Capitulino | const char *fmt, va_list *va) |
271 | 9f9daf9a | Luiz Capitulino | { |
272 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
273 | 9f9daf9a | Luiz Capitulino | |
274 | 9f9daf9a | Luiz Capitulino | qerr = qerror_new(); |
275 | 827b0813 | Markus Armbruster | loc_save(&qerr->loc); |
276 | 9f9daf9a | Luiz Capitulino | qerr->linenr = linenr; |
277 | 9f9daf9a | Luiz Capitulino | qerr->file = file; |
278 | 9f9daf9a | Luiz Capitulino | qerr->func = func; |
279 | 9f9daf9a | Luiz Capitulino | |
280 | 9f9daf9a | Luiz Capitulino | if (!fmt) {
|
281 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "QDict not specified");
|
282 | 9f9daf9a | Luiz Capitulino | } |
283 | 9f9daf9a | Luiz Capitulino | |
284 | 9f9daf9a | Luiz Capitulino | qerror_set_data(qerr, fmt, va); |
285 | 9f9daf9a | Luiz Capitulino | qerror_set_desc(qerr, fmt); |
286 | 9f9daf9a | Luiz Capitulino | |
287 | 9f9daf9a | Luiz Capitulino | return qerr;
|
288 | 9f9daf9a | Luiz Capitulino | } |
289 | 9f9daf9a | Luiz Capitulino | |
290 | 9f9daf9a | Luiz Capitulino | static void parse_error(const QError *qerror, int c) |
291 | 9f9daf9a | Luiz Capitulino | { |
292 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
|
293 | 9f9daf9a | Luiz Capitulino | } |
294 | 9f9daf9a | Luiz Capitulino | |
295 | 9f9daf9a | Luiz Capitulino | static const char *append_field(QString *outstr, const QError *qerror, |
296 | 9f9daf9a | Luiz Capitulino | const char *start) |
297 | 9f9daf9a | Luiz Capitulino | { |
298 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
299 | 9f9daf9a | Luiz Capitulino | QDict *qdict; |
300 | 9f9daf9a | Luiz Capitulino | QString *key_qs; |
301 | 9f9daf9a | Luiz Capitulino | const char *end, *key; |
302 | 9f9daf9a | Luiz Capitulino | |
303 | 9f9daf9a | Luiz Capitulino | if (*start != '%') |
304 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '%');
|
305 | 9f9daf9a | Luiz Capitulino | start++; |
306 | 9f9daf9a | Luiz Capitulino | if (*start != '(') |
307 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '(');
|
308 | 9f9daf9a | Luiz Capitulino | start++; |
309 | 9f9daf9a | Luiz Capitulino | |
310 | 9f9daf9a | Luiz Capitulino | end = strchr(start, ')');
|
311 | 9f9daf9a | Luiz Capitulino | if (!end)
|
312 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, ')');
|
313 | 9f9daf9a | Luiz Capitulino | |
314 | 9f9daf9a | Luiz Capitulino | key_qs = qstring_from_substr(start, 0, end - start - 1); |
315 | 9f9daf9a | Luiz Capitulino | key = qstring_get_str(key_qs); |
316 | 9f9daf9a | Luiz Capitulino | |
317 | 9f9daf9a | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
|
318 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qdict, key); |
319 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
320 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "key '%s' not found in QDict", key);
|
321 | 9f9daf9a | Luiz Capitulino | } |
322 | 9f9daf9a | Luiz Capitulino | |
323 | 9f9daf9a | Luiz Capitulino | switch (qobject_type(obj)) {
|
324 | 9f9daf9a | Luiz Capitulino | case QTYPE_QSTRING:
|
325 | 9f9daf9a | Luiz Capitulino | qstring_append(outstr, qdict_get_str(qdict, key)); |
326 | 9f9daf9a | Luiz Capitulino | break;
|
327 | 9f9daf9a | Luiz Capitulino | case QTYPE_QINT:
|
328 | 9f9daf9a | Luiz Capitulino | qstring_append_int(outstr, qdict_get_int(qdict, key)); |
329 | 9f9daf9a | Luiz Capitulino | break;
|
330 | 9f9daf9a | Luiz Capitulino | default:
|
331 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
|
332 | 9f9daf9a | Luiz Capitulino | } |
333 | 9f9daf9a | Luiz Capitulino | |
334 | 9f9daf9a | Luiz Capitulino | QDECREF(key_qs); |
335 | 9f9daf9a | Luiz Capitulino | return ++end;
|
336 | 9f9daf9a | Luiz Capitulino | } |
337 | 9f9daf9a | Luiz Capitulino | |
338 | 9f9daf9a | Luiz Capitulino | /**
|
339 | 77e595e7 | Markus Armbruster | * qerror_human(): Format QError data into human-readable string.
|
340 | 9f9daf9a | Luiz Capitulino | *
|
341 | 77e595e7 | Markus Armbruster | * Formats according to member 'desc' of the specified QError object.
|
342 | 9f9daf9a | Luiz Capitulino | */
|
343 | 77e595e7 | Markus Armbruster | QString *qerror_human(const QError *qerror)
|
344 | 9f9daf9a | Luiz Capitulino | { |
345 | 9f9daf9a | Luiz Capitulino | const char *p; |
346 | 9f9daf9a | Luiz Capitulino | QString *qstring; |
347 | 9f9daf9a | Luiz Capitulino | |
348 | 9f9daf9a | Luiz Capitulino | assert(qerror->entry != NULL);
|
349 | 9f9daf9a | Luiz Capitulino | |
350 | 9f9daf9a | Luiz Capitulino | qstring = qstring_new(); |
351 | 9f9daf9a | Luiz Capitulino | |
352 | 9f9daf9a | Luiz Capitulino | for (p = qerror->entry->desc; *p != '\0';) { |
353 | 9f9daf9a | Luiz Capitulino | if (*p != '%') { |
354 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, *p++); |
355 | 9f9daf9a | Luiz Capitulino | } else if (*(p + 1) == '%') { |
356 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, '%');
|
357 | 9f9daf9a | Luiz Capitulino | p += 2;
|
358 | 9f9daf9a | Luiz Capitulino | } else {
|
359 | 9f9daf9a | Luiz Capitulino | p = append_field(qstring, qerror, p); |
360 | 9f9daf9a | Luiz Capitulino | } |
361 | 9f9daf9a | Luiz Capitulino | } |
362 | 9f9daf9a | Luiz Capitulino | |
363 | 77e595e7 | Markus Armbruster | return qstring;
|
364 | 77e595e7 | Markus Armbruster | } |
365 | 77e595e7 | Markus Armbruster | |
366 | 77e595e7 | Markus Armbruster | /**
|
367 | 77e595e7 | Markus Armbruster | * qerror_print(): Print QError data
|
368 | 77e595e7 | Markus Armbruster | *
|
369 | 77e595e7 | Markus Armbruster | * This function will print the member 'desc' of the specified QError object,
|
370 | 1ecda02b | Markus Armbruster | * it uses error_report() for this, so that the output is routed to the right
|
371 | 77e595e7 | Markus Armbruster | * place (ie. stderr or Monitor's device).
|
372 | 77e595e7 | Markus Armbruster | */
|
373 | 827b0813 | Markus Armbruster | void qerror_print(QError *qerror)
|
374 | 77e595e7 | Markus Armbruster | { |
375 | 77e595e7 | Markus Armbruster | QString *qstring = qerror_human(qerror); |
376 | 827b0813 | Markus Armbruster | loc_push_restore(&qerror->loc); |
377 | 1ecda02b | Markus Armbruster | error_report("%s", qstring_get_str(qstring));
|
378 | 827b0813 | Markus Armbruster | loc_pop(&qerror->loc); |
379 | 9f9daf9a | Luiz Capitulino | QDECREF(qstring); |
380 | 9f9daf9a | Luiz Capitulino | } |
381 | 9f9daf9a | Luiz Capitulino | |
382 | 0167f772 | Markus Armbruster | void qerror_report_internal(const char *file, int linenr, const char *func, |
383 | 0167f772 | Markus Armbruster | const char *fmt, ...) |
384 | 0167f772 | Markus Armbruster | { |
385 | 0167f772 | Markus Armbruster | va_list va; |
386 | 0167f772 | Markus Armbruster | QError *qerror; |
387 | 0167f772 | Markus Armbruster | |
388 | 0167f772 | Markus Armbruster | va_start(va, fmt); |
389 | 0167f772 | Markus Armbruster | qerror = qerror_from_info(file, linenr, func, fmt, &va); |
390 | 0167f772 | Markus Armbruster | va_end(va); |
391 | 0167f772 | Markus Armbruster | |
392 | 0167f772 | Markus Armbruster | if (monitor_cur_is_qmp()) {
|
393 | 0167f772 | Markus Armbruster | monitor_set_error(cur_mon, qerror); |
394 | 0167f772 | Markus Armbruster | } else {
|
395 | 0167f772 | Markus Armbruster | qerror_print(qerror); |
396 | 0167f772 | Markus Armbruster | QDECREF(qerror); |
397 | 0167f772 | Markus Armbruster | } |
398 | 0167f772 | Markus Armbruster | } |
399 | 0167f772 | Markus Armbruster | |
400 | 9f9daf9a | Luiz Capitulino | /**
|
401 | 9f9daf9a | Luiz Capitulino | * qobject_to_qerror(): Convert a QObject into a QError
|
402 | 9f9daf9a | Luiz Capitulino | */
|
403 | 9f9daf9a | Luiz Capitulino | QError *qobject_to_qerror(const QObject *obj)
|
404 | 9f9daf9a | Luiz Capitulino | { |
405 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QERROR) {
|
406 | 9f9daf9a | Luiz Capitulino | return NULL; |
407 | 9f9daf9a | Luiz Capitulino | } |
408 | 9f9daf9a | Luiz Capitulino | |
409 | 9f9daf9a | Luiz Capitulino | return container_of(obj, QError, base);
|
410 | 9f9daf9a | Luiz Capitulino | } |
411 | 9f9daf9a | Luiz Capitulino | |
412 | 9f9daf9a | Luiz Capitulino | /**
|
413 | 9f9daf9a | Luiz Capitulino | * qerror_destroy_obj(): Free all memory allocated by a QError
|
414 | 9f9daf9a | Luiz Capitulino | */
|
415 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj) |
416 | 9f9daf9a | Luiz Capitulino | { |
417 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
418 | 9f9daf9a | Luiz Capitulino | |
419 | 9f9daf9a | Luiz Capitulino | assert(obj != NULL);
|
420 | 9f9daf9a | Luiz Capitulino | qerr = qobject_to_qerror(obj); |
421 | 9f9daf9a | Luiz Capitulino | |
422 | 9f9daf9a | Luiz Capitulino | QDECREF(qerr->error); |
423 | 9f9daf9a | Luiz Capitulino | qemu_free(qerr); |
424 | 9f9daf9a | Luiz Capitulino | } |