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