blob: d073ed7c5a75ee09852637773f3bf71364b67966 [file] [log] [blame]
Luiz Capitulino9f9daf92009-11-18 23:05:30 -02001/*
Luiz Capitulino41836a92010-05-12 16:34:42 -03002 * QError Module
Luiz Capitulino9f9daf92009-11-18 23:05:30 -02003 *
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 */
Markus Armbruster0167f772010-03-22 10:29:05 +010012
13#include "monitor.h"
Luiz Capitulino9f9daf92009-11-18 23:05:30 -020014#include "qjson.h"
15#include "qerror.h"
Luiz Capitulino9f9daf92009-11-18 23:05:30 -020016#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)%%"
Markus Armbruster9d494c42010-03-25 17:22:30 +010041 *
42 * Please keep the entries in alphabetical order.
Stefan Hajnoczi97373832012-01-04 22:23:32 +000043 * Use scripts/check-qerror.sh to check.
Luiz Capitulino9f9daf92009-11-18 23:05:30 -020044 */
Blue Swirld05ac8f2009-12-04 20:44:44 +000045static const QErrorStringTable qerror_table[] = {
Luiz Capitulino357b6152009-11-18 23:05:32 -020046 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +000047 .error_fmt = QERR_ADD_CLIENT_FAILED,
48 .desc = "Could not add client",
49 },
50 {
Paolo Bonzini11e35bf2012-02-02 12:37:53 +010051 .error_fmt = QERR_AMBIGUOUS_PATH,
Luiz Capitulino484051b2012-07-25 13:18:41 -030052 .desc = "Path '%(path)' does not uniquely identify an object"
Paolo Bonzini11e35bf2012-02-02 12:37:53 +010053 },
54 {
Markus Armbruster56e9f562010-02-19 19:52:45 +010055 .error_fmt = QERR_BAD_BUS_FOR_DEVICE,
56 .desc = "Device '%(device)' can't go on a %(bad_bus_type) bus",
57 },
58 {
Marcelo Tosatti019b8cb2012-01-18 14:40:52 +000059 .error_fmt = QERR_BASE_NOT_FOUND,
60 .desc = "Base '%(base)' not found",
61 },
62 {
Anthony Liguori1ed520c2011-11-14 15:09:47 -060063 .error_fmt = QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
64 .desc = "Block format '%(format)' used by device '%(name)' does not support feature '%(feature)'",
65 },
66 {
Markus Armbruster8eae73b2010-02-19 19:53:36 +010067 .error_fmt = QERR_BUS_NO_HOTPLUG,
68 .desc = "Bus '%(bus)' does not support hotplugging",
69 },
70 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +000071 .error_fmt = QERR_BUS_NOT_FOUND,
72 .desc = "Bus '%(bus)' not found",
Luiz Capitulino4b9d4682009-11-26 22:58:57 -020073 },
74 {
Michael Rothabd6cf62011-12-06 22:03:42 -060075 .error_fmt = QERR_COMMAND_DISABLED,
76 .desc = "The command %(name) has been disabled for this instance",
77 },
78 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +000079 .error_fmt = QERR_COMMAND_NOT_FOUND,
80 .desc = "The command %(name) has not been found",
81 },
82 {
Luiz Capitulino0df37c42009-12-07 21:36:59 +010083 .error_fmt = QERR_DEVICE_ENCRYPTED,
Luiz Capitulinob5d90f02012-07-25 13:16:53 -030084 .desc = "'%(device)' (%(filename)) is encrypted",
Luiz Capitulino0df37c42009-12-07 21:36:59 +010085 },
86 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +000087 .error_fmt = QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
88 .desc = "Migration is disabled when using feature '%(feature)' in device '%(device)'",
89 },
90 {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +000091 .error_fmt = QERR_DEVICE_HAS_NO_MEDIUM,
92 .desc = "Device '%(device)' has no medium",
93 },
94 {
Markus Armbruster4d9a1a12010-02-19 19:53:54 +010095 .error_fmt = QERR_DEVICE_INIT_FAILED,
96 .desc = "Device '%(device)' could not be initialized",
97 },
98 {
Markus Armbruster5124eb52010-03-25 17:22:39 +010099 .error_fmt = QERR_DEVICE_IN_USE,
100 .desc = "Device '%(device)' is in use",
101 },
102 {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +0000103 .error_fmt = QERR_DEVICE_IS_READ_ONLY,
104 .desc = "Device '%(device)' is read only",
105 },
106 {
Markus Armbrusterb0868382009-12-07 21:37:03 +0100107 .error_fmt = QERR_DEVICE_LOCKED,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100108 .desc = "Device '%(device)' is locked",
Markus Armbrusterb0868382009-12-07 21:37:03 +0100109 },
110 {
Markus Armbruster1ae78712010-02-19 18:05:59 +0100111 .error_fmt = QERR_DEVICE_MULTIPLE_BUSSES,
112 .desc = "Device '%(device)' has multiple child busses",
113 },
114 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +0000115 .error_fmt = QERR_DEVICE_NO_BUS,
116 .desc = "Device '%(device)' has no child bus",
117 },
118 {
119 .error_fmt = QERR_DEVICE_NO_HOTPLUG,
120 .desc = "Device '%(device)' does not support hotplugging",
121 },
122 {
Luiz Capitulino055f6122009-11-18 23:05:34 -0200123 .error_fmt = QERR_DEVICE_NOT_ACTIVE,
Luiz Capitulino8754c812010-06-23 12:37:47 -0300124 .desc = "Device '%(device)' has not been activated",
Luiz Capitulino055f6122009-11-18 23:05:34 -0200125 },
Luiz Capitulino82a60712009-11-18 23:05:35 -0200126 {
Markus Armbruster9d494c42010-03-25 17:22:30 +0100127 .error_fmt = QERR_DEVICE_NOT_ENCRYPTED,
128 .desc = "Device '%(device)' is not encrypted",
129 },
130 {
Markus Armbrustere16a1812009-12-07 21:37:02 +0100131 .error_fmt = QERR_DEVICE_NOT_FOUND,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100132 .desc = "Device '%(device)' not found",
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200133 },
134 {
Markus Armbruster5cfe0262009-12-07 21:37:04 +0100135 .error_fmt = QERR_DEVICE_NOT_REMOVABLE,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100136 .desc = "Device '%(device)' is not removable",
Markus Armbruster5cfe0262009-12-07 21:37:04 +0100137 },
138 {
Markus Armbruster7bc84012010-03-25 17:22:31 +0100139 .error_fmt = QERR_DUPLICATE_ID,
140 .desc = "Duplicate ID '%(id)' for %(object)",
141 },
142 {
Markus Armbrusterc7c338c2009-12-07 21:37:10 +0100143 .error_fmt = QERR_FD_NOT_FOUND,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100144 .desc = "File descriptor named '%(name)' not found",
Markus Armbrusterc7c338c2009-12-07 21:37:10 +0100145 },
146 {
Markus Armbruster41471a22009-12-07 21:37:12 +0100147 .error_fmt = QERR_FD_NOT_SUPPLIED,
148 .desc = "No file descriptor supplied via SCM_RIGHTS",
149 },
150 {
Luiz Capitulinobf826322011-10-19 15:15:57 -0200151 .error_fmt = QERR_FEATURE_DISABLED,
152 .desc = "The feature '%(name)' is not enabled",
153 },
154 {
Markus Armbruster17901e72009-12-07 21:37:06 +0100155 .error_fmt = QERR_INVALID_BLOCK_FORMAT,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100156 .desc = "Invalid block format '%(name)'",
Markus Armbruster17901e72009-12-07 21:37:06 +0100157 },
158 {
Luiz Capitulino8ff15d42012-03-28 14:01:15 -0300159 .error_fmt = QERR_INVALID_OPTION_GROUP,
160 .desc = "There is no option group '%(group)'",
161 },
162 {
Markus Armbruster7a046f52009-12-07 21:37:13 +0100163 .error_fmt = QERR_INVALID_PARAMETER,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100164 .desc = "Invalid parameter '%(name)'",
Markus Armbruster7a046f52009-12-07 21:37:13 +0100165 },
166 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +0000167 .error_fmt = QERR_INVALID_PARAMETER_COMBINATION,
168 .desc = "Invalid parameter combination",
169 },
170 {
Markus Armbrustere16a1812009-12-07 21:37:02 +0100171 .error_fmt = QERR_INVALID_PARAMETER_TYPE,
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600172 .desc = "Invalid parameter type for '%(name)', expected: %(expected)",
Markus Armbrustere16a1812009-12-07 21:37:02 +0100173 },
174 {
Markus Armbruster985a3e52010-03-25 17:22:33 +0100175 .error_fmt = QERR_INVALID_PARAMETER_VALUE,
176 .desc = "Parameter '%(name)' expects %(expected)",
177 },
178 {
Markus Armbrustere16a1812009-12-07 21:37:02 +0100179 .error_fmt = QERR_INVALID_PASSWORD,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100180 .desc = "Password incorrect",
Markus Armbrustere16a1812009-12-07 21:37:02 +0100181 },
182 {
Luiz Capitulino58898872011-11-22 16:29:52 -0200183 .error_fmt = QERR_IO_ERROR,
184 .desc = "An IO error has occurred",
185 },
186 {
Michael Rothac32c782011-07-19 15:41:52 -0500187 .error_fmt = QERR_JSON_PARSE_ERROR,
188 .desc = "JSON parse error, %(message)",
189
190 },
191 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +0000192 .error_fmt = QERR_JSON_PARSING,
193 .desc = "Invalid JSON syntax",
194 },
195 {
Luiz Capitulino82a60712009-11-18 23:05:35 -0200196 .error_fmt = QERR_KVM_MISSING_CAP,
197 .desc = "Using KVM without %(capability), %(feature) unavailable",
198 },
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200199 {
Luiz Capitulinoa4acc062011-12-02 14:58:12 -0200200 .error_fmt = QERR_MIGRATION_ACTIVE,
201 .desc = "There's a migration process in progress",
202 },
203 {
204 .error_fmt = QERR_MIGRATION_NOT_SUPPORTED,
205 .desc = "State blocked by non-migratable device '%(device)'",
206 },
207 {
Amit Shah8e848652010-07-27 15:49:19 +0530208 .error_fmt = QERR_MIGRATION_EXPECTED,
209 .desc = "An incoming migration is expected before this command can be executed",
210 },
211 {
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200212 .error_fmt = QERR_MISSING_PARAMETER,
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100213 .desc = "Parameter '%(name)' is missing",
214 },
215 {
Markus Armbrusterfab57672010-02-19 19:54:06 +0100216 .error_fmt = QERR_NO_BUS_FOR_DEVICE,
217 .desc = "No '%(bus)' bus found for device '%(device)'",
218 },
219 {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +0000220 .error_fmt = QERR_NOT_SUPPORTED,
221 .desc = "Not supported",
222 },
223 {
Markus Armbrusterfc5469d2010-02-19 17:42:46 +0100224 .error_fmt = QERR_OPEN_FILE_FAILED,
225 .desc = "Could not open '%(filename)'",
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200226 },
227 {
Anthony Liguori44677de2011-12-12 14:29:26 -0600228 .error_fmt = QERR_PERMISSION_DENIED,
229 .desc = "Insufficient permission to perform this operation",
230 },
231 {
Markus Armbrusterc58a35f2010-02-19 13:11:41 +0100232 .error_fmt = QERR_PROPERTY_NOT_FOUND,
233 .desc = "Property '%(device).%(property)' not found",
234 },
235 {
Markus Armbruster06b4a702010-02-19 13:17:58 +0100236 .error_fmt = QERR_PROPERTY_VALUE_BAD,
237 .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
238 },
239 {
Markus Armbruster9c5eff92010-03-16 17:40:48 +0100240 .error_fmt = QERR_PROPERTY_VALUE_IN_USE,
241 .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
242 },
243 {
Markus Armbruster84745d62010-03-16 17:44:38 +0100244 .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND,
245 .desc = "Property '%(device).%(property)' can't find value '%(value)'",
246 },
247 {
Stefan Hajnoczi02fda012012-03-14 15:57:05 +0000248 .error_fmt = QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
249 .desc = "Property '%(device).%(property)' doesn't take "
250 "value '%(value)', it's not a power of 2",
251 },
252 {
Paolo Bonzini6aced822011-12-18 17:05:08 +0100253 .error_fmt = QERR_PROPERTY_VALUE_OUT_OF_RANGE,
254 .desc = "Property '%(device).%(property)' doesn't take "
Stefan Hajnoczi41453412012-03-14 15:57:04 +0000255 "value %(value) (minimum: %(min), maximum: %(max))",
Paolo Bonzini6aced822011-12-18 17:05:08 +0100256 },
257 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +0000258 .error_fmt = QERR_QGA_COMMAND_FAILED,
259 .desc = "Guest agent command failed, error was '%(message)'",
260 },
261 {
262 .error_fmt = QERR_QGA_LOGGING_FAILED,
263 .desc = "Guest agent failed to log non-optional log statement",
264 },
265 {
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200266 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT,
Luiz Capitulinoabaf2f52010-04-07 14:53:49 -0300267 .desc = "Expected '%(expected)' in QMP input",
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200268 },
269 {
Luiz Capitulino7dfb6122010-04-07 14:46:33 -0300270 .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
271 .desc = "QMP input object member '%(member)' expects '%(expected)'",
272 },
273 {
Luiz Capitulino60d76d72010-06-01 16:15:23 -0300274 .error_fmt = QERR_QMP_EXTRA_MEMBER,
275 .desc = "QMP input object member '%(member)' is unexpected",
276 },
277 {
Luiz Capitulino6667b232011-07-29 15:57:54 -0300278 .error_fmt = QERR_RESET_REQUIRED,
279 .desc = "Resetting the Virtual Machine is required",
280 },
281 {
Markus Armbruster7a84cb22009-12-07 21:37:07 +0100282 .error_fmt = QERR_SET_PASSWD_FAILED,
283 .desc = "Could not set password",
284 },
285 {
Markus Armbrustera488be22009-12-07 21:37:14 +0100286 .error_fmt = QERR_TOO_MANY_FILES,
287 .desc = "Too many open files",
288 },
289 {
Markus Armbrustere16a1812009-12-07 21:37:02 +0100290 .error_fmt = QERR_UNDEFINED_ERROR,
Dong Xu Wang07f35072011-11-22 18:06:26 +0800291 .desc = "An undefined error has occurred",
Luiz Capitulino4b9d4682009-11-26 22:58:57 -0200292 },
Markus Armbrustera6906e32009-12-07 21:37:08 +0100293 {
Kevin Wolff54e3642011-02-09 11:09:38 +0100294 .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
295 .desc = "'%(device)' uses a %(format) feature which is not "
296 "supported by this qemu version: %(feature)",
297 },
298 {
Stefan Hajnoczic1303592012-01-04 17:38:22 +0000299 .error_fmt = QERR_UNSUPPORTED,
300 .desc = "this feature or command is not currently supported",
301 },
302 {
Aneesh Kumar K.Ve9a01522011-12-04 22:35:28 +0530303 .error_fmt = QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION,
304 .desc = "Migration is disabled when VirtFS export path '%(path)' "
305 "is mounted in the guest using mount_tag '%(tag)'",
306 },
307 {
Markus Armbrustera6906e32009-12-07 21:37:08 +0100308 .error_fmt = QERR_VNC_SERVER_FAILED,
309 .desc = "Could not start VNC server on %(target)",
310 },
Amos Kongaed3d112012-05-11 00:28:08 +0800311 {
312 .error_fmt = QERR_SOCKET_CONNECT_IN_PROGRESS,
313 .desc = "Connection can not be completed immediately",
314 },
315 {
316 .error_fmt = QERR_SOCKET_CONNECT_FAILED,
317 .desc = "Failed to connect to socket",
318 },
319 {
320 .error_fmt = QERR_SOCKET_LISTEN_FAILED,
321 .desc = "Failed to set socket to listening mode",
322 },
323 {
324 .error_fmt = QERR_SOCKET_BIND_FAILED,
325 .desc = "Failed to bind socket",
326 },
327 {
328 .error_fmt = QERR_SOCKET_CREATE_FAILED,
329 .desc = "Failed to create socket",
330 },
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200331 {}
332};
333
334/**
335 * qerror_new(): Create a new QError
336 *
337 * Return strong reference.
338 */
Luiz Capitulino2a744402012-07-19 17:29:34 -0300339static QError *qerror_new(void)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200340{
341 QError *qerr;
342
Anthony Liguori7267c092011-08-20 22:09:37 -0500343 qerr = g_malloc0(sizeof(*qerr));
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200344 QOBJECT_INIT(qerr, &qerror_type);
345
346 return qerr;
347}
348
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300349static QDict *error_obj_from_fmt_no_fail(const char *fmt, va_list *va)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200350{
351 QObject *obj;
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300352 QDict *ret;
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200353
354 obj = qobject_from_jsonv(fmt, va);
355 if (!obj) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300356 fprintf(stderr, "invalid json in error dict '%s'\n", fmt);
357 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200358 }
359 if (qobject_type(obj) != QTYPE_QDICT) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300360 fprintf(stderr, "error is not a dict '%s'\n", fmt);
361 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200362 }
363
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300364 ret = qobject_to_qdict(obj);
365 obj = qdict_get(ret, "class");
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200366 if (!obj) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300367 fprintf(stderr, "missing 'class' key in '%s'\n", fmt);
368 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200369 }
370 if (qobject_type(obj) != QTYPE_QSTRING) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300371 fprintf(stderr, "'class' key value should be a string in '%s'\n", fmt);
372 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200373 }
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300374
375 obj = qdict_get(ret, "data");
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200376 if (!obj) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300377 fprintf(stderr, "missing 'data' key in '%s'\n", fmt);
378 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200379 }
380 if (qobject_type(obj) != QTYPE_QDICT) {
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300381 fprintf(stderr, "'data' key value should be a dict in '%s'\n", fmt);
382 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200383 }
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300384
385 return ret;
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200386}
387
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300388static const QErrorStringTable *get_desc_no_fail(const char *fmt)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200389{
390 int i;
391
392 // FIXME: inefficient loop
393
394 for (i = 0; qerror_table[i].error_fmt; i++) {
395 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300396 return &qerror_table[i];
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200397 }
398 }
399
Luiz Capitulinofbe0a832012-07-20 10:35:18 -0300400 fprintf(stderr, "error format '%s' not found\n", fmt);
401 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200402}
403
404/**
405 * qerror_from_info(): Create a new QError from error information
406 *
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200407 * Return strong reference.
408 */
Luiz Capitulino5f0f0e12012-07-20 11:08:17 -0300409static QError *qerror_from_info(const char *fmt, va_list *va)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200410{
411 QError *qerr;
412
413 qerr = qerror_new();
Markus Armbruster827b0812010-02-18 19:46:49 +0100414 loc_save(&qerr->loc);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200415
Luiz Capitulinof2dd1d62012-07-31 15:41:13 -0300416 qerr->error = error_obj_from_fmt_no_fail(fmt, va);
417 qerr->entry = get_desc_no_fail(fmt);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200418
419 return qerr;
420}
421
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500422static void parse_error(const QErrorStringTable *entry, int c)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200423{
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500424 fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
425 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200426}
427
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500428static const char *append_field(QDict *error, QString *outstr,
429 const QErrorStringTable *entry,
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200430 const char *start)
431{
432 QObject *obj;
433 QDict *qdict;
434 QString *key_qs;
435 const char *end, *key;
436
437 if (*start != '%')
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500438 parse_error(entry, '%');
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200439 start++;
440 if (*start != '(')
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500441 parse_error(entry, '(');
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200442 start++;
443
444 end = strchr(start, ')');
445 if (!end)
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500446 parse_error(entry, ')');
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200447
448 key_qs = qstring_from_substr(start, 0, end - start - 1);
449 key = qstring_get_str(key_qs);
450
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500451 qdict = qobject_to_qdict(qdict_get(error, "data"));
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200452 obj = qdict_get(qdict, key);
453 if (!obj) {
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500454 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200455 }
456
457 switch (qobject_type(obj)) {
458 case QTYPE_QSTRING:
459 qstring_append(outstr, qdict_get_str(qdict, key));
460 break;
461 case QTYPE_QINT:
462 qstring_append_int(outstr, qdict_get_int(qdict, key));
463 break;
464 default:
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500465 abort();
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200466 }
467
468 QDECREF(key_qs);
469 return ++end;
470}
471
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500472static QString *qerror_format_desc(QDict *error,
473 const QErrorStringTable *entry)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200474{
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200475 QString *qstring;
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500476 const char *p;
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200477
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500478 assert(entry != NULL);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200479
480 qstring = qstring_new();
481
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500482 for (p = entry->desc; *p != '\0';) {
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200483 if (*p != '%') {
484 qstring_append_chr(qstring, *p++);
485 } else if (*(p + 1) == '%') {
486 qstring_append_chr(qstring, '%');
487 p += 2;
488 } else {
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500489 p = append_field(error, qstring, entry, p);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200490 }
491 }
492
Markus Armbruster77e595e2009-12-07 21:37:16 +0100493 return qstring;
494}
495
Luiz Capitulino2b38cf22012-07-20 13:30:18 -0300496char *qerror_format(const char *fmt, QDict *error)
Luiz Capitulino87c2f592011-06-01 12:14:48 -0500497{
498 const QErrorStringTable *entry = NULL;
Luiz Capitulino2b38cf22012-07-20 13:30:18 -0300499 QString *qstr;
500 char *ret;
Luiz Capitulino87c2f592011-06-01 12:14:48 -0500501 int i;
502
503 for (i = 0; qerror_table[i].error_fmt; i++) {
504 if (strcmp(qerror_table[i].error_fmt, fmt) == 0) {
505 entry = &qerror_table[i];
506 break;
507 }
508 }
509
Luiz Capitulino2b38cf22012-07-20 13:30:18 -0300510 qstr = qerror_format_desc(error, entry);
511 ret = g_strdup(qstring_get_str(qstr));
512 QDECREF(qstr);
513
514 return ret;
Luiz Capitulino87c2f592011-06-01 12:14:48 -0500515}
516
Markus Armbruster77e595e2009-12-07 21:37:16 +0100517/**
Luiz Capitulinoa12eeaa2011-06-01 12:14:47 -0500518 * qerror_human(): Format QError data into human-readable string.
519 */
520QString *qerror_human(const QError *qerror)
521{
522 return qerror_format_desc(qerror->error, qerror->entry);
523}
524
525/**
Markus Armbruster77e595e2009-12-07 21:37:16 +0100526 * qerror_print(): Print QError data
527 *
528 * This function will print the member 'desc' of the specified QError object,
Markus Armbruster1ecda022010-02-18 17:25:24 +0100529 * it uses error_report() for this, so that the output is routed to the right
Markus Armbruster77e595e2009-12-07 21:37:16 +0100530 * place (ie. stderr or Monitor's device).
531 */
Luiz Capitulino2a744402012-07-19 17:29:34 -0300532static void qerror_print(QError *qerror)
Markus Armbruster77e595e2009-12-07 21:37:16 +0100533{
534 QString *qstring = qerror_human(qerror);
Markus Armbruster827b0812010-02-18 19:46:49 +0100535 loc_push_restore(&qerror->loc);
Markus Armbruster1ecda022010-02-18 17:25:24 +0100536 error_report("%s", qstring_get_str(qstring));
Markus Armbruster827b0812010-02-18 19:46:49 +0100537 loc_pop(&qerror->loc);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200538 QDECREF(qstring);
539}
540
Luiz Capitulino5f0f0e12012-07-20 11:08:17 -0300541void qerror_report(const char *fmt, ...)
Markus Armbruster0167f772010-03-22 10:29:05 +0100542{
543 va_list va;
544 QError *qerror;
545
546 va_start(va, fmt);
Luiz Capitulino5f0f0e12012-07-20 11:08:17 -0300547 qerror = qerror_from_info(fmt, &va);
Markus Armbruster0167f772010-03-22 10:29:05 +0100548 va_end(va);
549
550 if (monitor_cur_is_qmp()) {
551 monitor_set_error(cur_mon, qerror);
552 } else {
553 qerror_print(qerror);
554 QDECREF(qerror);
555 }
556}
557
Anthony Liguori2a82d932011-09-02 12:34:45 -0500558/* Evil... */
559struct Error
560{
561 QDict *obj;
562 const char *fmt;
563 char *msg;
564};
565
566void qerror_report_err(Error *err)
567{
568 QError *qerr;
569 int i;
570
571 qerr = qerror_new();
572 loc_save(&qerr->loc);
573 QINCREF(err->obj);
574 qerr->error = err->obj;
575
576 for (i = 0; qerror_table[i].error_fmt; i++) {
577 if (strcmp(qerror_table[i].error_fmt, err->fmt) == 0) {
578 qerr->entry = &qerror_table[i];
579 break;
580 }
581 }
582
583 if (monitor_cur_is_qmp()) {
584 monitor_set_error(cur_mon, qerr);
585 } else {
586 qerror_print(qerr);
587 QDECREF(qerr);
588 }
589}
590
Paolo Bonzini59f971d2012-02-14 10:19:53 +0100591void assert_no_error(Error *err)
592{
593 if (err) {
594 qerror_report_err(err);
595 abort();
596 }
597}
598
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200599/**
600 * qobject_to_qerror(): Convert a QObject into a QError
601 */
Luiz Capitulino2a744402012-07-19 17:29:34 -0300602static QError *qobject_to_qerror(const QObject *obj)
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200603{
604 if (qobject_type(obj) != QTYPE_QERROR) {
605 return NULL;
606 }
607
608 return container_of(obj, QError, base);
609}
610
611/**
612 * qerror_destroy_obj(): Free all memory allocated by a QError
613 */
614static void qerror_destroy_obj(QObject *obj)
615{
616 QError *qerr;
617
618 assert(obj != NULL);
619 qerr = qobject_to_qerror(obj);
620
621 QDECREF(qerr->error);
Anthony Liguori7267c092011-08-20 22:09:37 -0500622 g_free(qerr);
Luiz Capitulino9f9daf92009-11-18 23:05:30 -0200623}