root / qerror.c @ ac32c780
History | View | Annotate | Download (12.6 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 | ac32c780 | Michael Roth | .error_fmt = QERR_JSON_PARSE_ERROR, |
145 | ac32c780 | Michael Roth | .desc = "JSON parse error, %(message)",
|
146 | ac32c780 | Michael Roth | |
147 | ac32c780 | Michael Roth | }, |
148 | ac32c780 | Michael Roth | { |
149 | 82a60711 | Luiz Capitulino | .error_fmt = QERR_KVM_MISSING_CAP, |
150 | 82a60711 | Luiz Capitulino | .desc = "Using KVM without %(capability), %(feature) unavailable",
|
151 | 82a60711 | Luiz Capitulino | }, |
152 | 4b9d4683 | Luiz Capitulino | { |
153 | 8e84865e | Amit Shah | .error_fmt = QERR_MIGRATION_EXPECTED, |
154 | 8e84865e | Amit Shah | .desc = "An incoming migration is expected before this command can be executed",
|
155 | 8e84865e | Amit Shah | }, |
156 | 8e84865e | Amit Shah | { |
157 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_MISSING_PARAMETER, |
158 | fc5469d8 | Markus Armbruster | .desc = "Parameter '%(name)' is missing",
|
159 | fc5469d8 | Markus Armbruster | }, |
160 | fc5469d8 | Markus Armbruster | { |
161 | fab5767f | Markus Armbruster | .error_fmt = QERR_NO_BUS_FOR_DEVICE, |
162 | fab5767f | Markus Armbruster | .desc = "No '%(bus)' bus found for device '%(device)'",
|
163 | fab5767f | Markus Armbruster | }, |
164 | fab5767f | Markus Armbruster | { |
165 | fc5469d8 | Markus Armbruster | .error_fmt = QERR_OPEN_FILE_FAILED, |
166 | fc5469d8 | Markus Armbruster | .desc = "Could not open '%(filename)'",
|
167 | 4b9d4683 | Luiz Capitulino | }, |
168 | 4b9d4683 | Luiz Capitulino | { |
169 | c58a35f8 | Markus Armbruster | .error_fmt = QERR_PROPERTY_NOT_FOUND, |
170 | c58a35f8 | Markus Armbruster | .desc = "Property '%(device).%(property)' not found",
|
171 | c58a35f8 | Markus Armbruster | }, |
172 | c58a35f8 | Markus Armbruster | { |
173 | 06b4a703 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_BAD, |
174 | 06b4a703 | Markus Armbruster | .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
|
175 | 06b4a703 | Markus Armbruster | }, |
176 | 06b4a703 | Markus Armbruster | { |
177 | 9c5eff95 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_IN_USE, |
178 | 9c5eff95 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
|
179 | 9c5eff95 | Markus Armbruster | }, |
180 | 9c5eff95 | Markus Armbruster | { |
181 | 84745d68 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND, |
182 | 84745d68 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't find value '%(value)'",
|
183 | 84745d68 | Markus Armbruster | }, |
184 | 84745d68 | Markus Armbruster | { |
185 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, |
186 | abaf2f52 | Luiz Capitulino | .desc = "Expected '%(expected)' in QMP input",
|
187 | 4b9d4683 | Luiz Capitulino | }, |
188 | 4b9d4683 | Luiz Capitulino | { |
189 | 7dfb6123 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER, |
190 | 7dfb6123 | Luiz Capitulino | .desc = "QMP input object member '%(member)' expects '%(expected)'",
|
191 | 7dfb6123 | Luiz Capitulino | }, |
192 | 7dfb6123 | Luiz Capitulino | { |
193 | 60d76d7b | Luiz Capitulino | .error_fmt = QERR_QMP_EXTRA_MEMBER, |
194 | 60d76d7b | Luiz Capitulino | .desc = "QMP input object member '%(member)' is unexpected",
|
195 | 60d76d7b | Luiz Capitulino | }, |
196 | 60d76d7b | Luiz Capitulino | { |
197 | 7a84cb23 | Markus Armbruster | .error_fmt = QERR_SET_PASSWD_FAILED, |
198 | 7a84cb23 | Markus Armbruster | .desc = "Could not set password",
|
199 | 7a84cb23 | Markus Armbruster | }, |
200 | 7a84cb23 | Markus Armbruster | { |
201 | a488be27 | Markus Armbruster | .error_fmt = QERR_TOO_MANY_FILES, |
202 | a488be27 | Markus Armbruster | .desc = "Too many open files",
|
203 | a488be27 | Markus Armbruster | }, |
204 | a488be27 | Markus Armbruster | { |
205 | e16a1812 | Markus Armbruster | .error_fmt = QERR_UNDEFINED_ERROR, |
206 | e16a1812 | Markus Armbruster | .desc = "An undefined error has ocurred",
|
207 | 4b9d4683 | Luiz Capitulino | }, |
208 | a6906e31 | Markus Armbruster | { |
209 | 4c5a1e4d | Lai Jiangshan | .error_fmt = QERR_UNSUPPORTED, |
210 | 4c5a1e4d | Lai Jiangshan | .desc = "this feature or command is not currently supported",
|
211 | 4c5a1e4d | Lai Jiangshan | }, |
212 | 4c5a1e4d | Lai Jiangshan | { |
213 | f54e3641 | Kevin Wolf | .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, |
214 | f54e3641 | Kevin Wolf | .desc = "'%(device)' uses a %(format) feature which is not "
|
215 | f54e3641 | Kevin Wolf | "supported by this qemu version: %(feature)",
|
216 | f54e3641 | Kevin Wolf | }, |
217 | f54e3641 | Kevin Wolf | { |
218 | a6906e31 | Markus Armbruster | .error_fmt = QERR_VNC_SERVER_FAILED, |
219 | a6906e31 | Markus Armbruster | .desc = "Could not start VNC server on %(target)",
|
220 | a6906e31 | Markus Armbruster | }, |
221 | 9f9daf9a | Luiz Capitulino | {} |
222 | 9f9daf9a | Luiz Capitulino | }; |
223 | 9f9daf9a | Luiz Capitulino | |
224 | 9f9daf9a | Luiz Capitulino | /**
|
225 | 9f9daf9a | Luiz Capitulino | * qerror_new(): Create a new QError
|
226 | 9f9daf9a | Luiz Capitulino | *
|
227 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
228 | 9f9daf9a | Luiz Capitulino | */
|
229 | 9f9daf9a | Luiz Capitulino | QError *qerror_new(void)
|
230 | 9f9daf9a | Luiz Capitulino | { |
231 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
232 | 9f9daf9a | Luiz Capitulino | |
233 | 9f9daf9a | Luiz Capitulino | qerr = qemu_mallocz(sizeof(*qerr));
|
234 | 9f9daf9a | Luiz Capitulino | QOBJECT_INIT(qerr, &qerror_type); |
235 | 9f9daf9a | Luiz Capitulino | |
236 | 9f9daf9a | Luiz Capitulino | return qerr;
|
237 | 9f9daf9a | Luiz Capitulino | } |
238 | 9f9daf9a | Luiz Capitulino | |
239 | 8b7968f7 | Stefan Weil | static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr, |
240 | 8b7968f7 | Stefan Weil | const char *fmt, ...) |
241 | 9f9daf9a | Luiz Capitulino | { |
242 | 9f9daf9a | Luiz Capitulino | va_list ap; |
243 | 9f9daf9a | Luiz Capitulino | |
244 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
|
245 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: -> ");
|
246 | 9f9daf9a | Luiz Capitulino | |
247 | 9f9daf9a | Luiz Capitulino | va_start(ap, fmt); |
248 | 9f9daf9a | Luiz Capitulino | vfprintf(stderr, fmt, ap); |
249 | 9f9daf9a | Luiz Capitulino | va_end(ap); |
250 | 9f9daf9a | Luiz Capitulino | |
251 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
|
252 | 9f9daf9a | Luiz Capitulino | abort(); |
253 | 9f9daf9a | Luiz Capitulino | } |
254 | 9f9daf9a | Luiz Capitulino | |
255 | 8b7968f7 | Stefan Weil | static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr, |
256 | 8b7968f7 | Stefan Weil | const char *fmt, va_list *va) |
257 | 9f9daf9a | Luiz Capitulino | { |
258 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
259 | 9f9daf9a | Luiz Capitulino | |
260 | 9f9daf9a | Luiz Capitulino | obj = qobject_from_jsonv(fmt, va); |
261 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
262 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "invalid format '%s'", fmt);
|
263 | 9f9daf9a | Luiz Capitulino | } |
264 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
265 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
|
266 | 9f9daf9a | Luiz Capitulino | } |
267 | 9f9daf9a | Luiz Capitulino | |
268 | 9f9daf9a | Luiz Capitulino | qerr->error = qobject_to_qdict(obj); |
269 | 9f9daf9a | Luiz Capitulino | |
270 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "class");
|
271 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
272 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
|
273 | 9f9daf9a | Luiz Capitulino | } |
274 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QSTRING) {
|
275 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'class' key value should be a QString");
|
276 | 9f9daf9a | Luiz Capitulino | } |
277 | 9f9daf9a | Luiz Capitulino | |
278 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "data");
|
279 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
280 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
|
281 | 9f9daf9a | Luiz Capitulino | } |
282 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
283 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'data' key value should be a QDICT");
|
284 | 9f9daf9a | Luiz Capitulino | } |
285 | 9f9daf9a | Luiz Capitulino | } |
286 | 9f9daf9a | Luiz Capitulino | |
287 | 9f9daf9a | Luiz Capitulino | static void qerror_set_desc(QError *qerr, const char *fmt) |
288 | 9f9daf9a | Luiz Capitulino | { |
289 | 9f9daf9a | Luiz Capitulino | int i;
|
290 | 9f9daf9a | Luiz Capitulino | |
291 | 9f9daf9a | Luiz Capitulino | // FIXME: inefficient loop
|
292 | 9f9daf9a | Luiz Capitulino | |
293 | 9f9daf9a | Luiz Capitulino | for (i = 0; qerror_table[i].error_fmt; i++) { |
294 | 9f9daf9a | Luiz Capitulino | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
295 | 9f9daf9a | Luiz Capitulino | qerr->entry = &qerror_table[i]; |
296 | 9f9daf9a | Luiz Capitulino | return;
|
297 | 9f9daf9a | Luiz Capitulino | } |
298 | 9f9daf9a | Luiz Capitulino | } |
299 | 9f9daf9a | Luiz Capitulino | |
300 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format '%s' not found", fmt);
|
301 | 9f9daf9a | Luiz Capitulino | } |
302 | 9f9daf9a | Luiz Capitulino | |
303 | 9f9daf9a | Luiz Capitulino | /**
|
304 | 9f9daf9a | Luiz Capitulino | * qerror_from_info(): Create a new QError from error information
|
305 | 9f9daf9a | Luiz Capitulino | *
|
306 | 9f9daf9a | Luiz Capitulino | * The information consists of:
|
307 | 9f9daf9a | Luiz Capitulino | *
|
308 | 9f9daf9a | Luiz Capitulino | * - file the file name of where the error occurred
|
309 | 9f9daf9a | Luiz Capitulino | * - linenr the line number of where the error occurred
|
310 | 9f9daf9a | Luiz Capitulino | * - func the function name of where the error occurred
|
311 | 9f9daf9a | Luiz Capitulino | * - fmt JSON printf-like dictionary, there must exist keys 'class' and
|
312 | 9f9daf9a | Luiz Capitulino | * 'data'
|
313 | 9f9daf9a | Luiz Capitulino | * - va va_list of all arguments specified by fmt
|
314 | 9f9daf9a | Luiz Capitulino | *
|
315 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
316 | 9f9daf9a | Luiz Capitulino | */
|
317 | 9f9daf9a | Luiz Capitulino | QError *qerror_from_info(const char *file, int linenr, const char *func, |
318 | 9f9daf9a | Luiz Capitulino | const char *fmt, va_list *va) |
319 | 9f9daf9a | Luiz Capitulino | { |
320 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
321 | 9f9daf9a | Luiz Capitulino | |
322 | 9f9daf9a | Luiz Capitulino | qerr = qerror_new(); |
323 | 827b0813 | Markus Armbruster | loc_save(&qerr->loc); |
324 | 9f9daf9a | Luiz Capitulino | qerr->linenr = linenr; |
325 | 9f9daf9a | Luiz Capitulino | qerr->file = file; |
326 | 9f9daf9a | Luiz Capitulino | qerr->func = func; |
327 | 9f9daf9a | Luiz Capitulino | |
328 | 9f9daf9a | Luiz Capitulino | if (!fmt) {
|
329 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "QDict not specified");
|
330 | 9f9daf9a | Luiz Capitulino | } |
331 | 9f9daf9a | Luiz Capitulino | |
332 | 9f9daf9a | Luiz Capitulino | qerror_set_data(qerr, fmt, va); |
333 | 9f9daf9a | Luiz Capitulino | qerror_set_desc(qerr, fmt); |
334 | 9f9daf9a | Luiz Capitulino | |
335 | 9f9daf9a | Luiz Capitulino | return qerr;
|
336 | 9f9daf9a | Luiz Capitulino | } |
337 | 9f9daf9a | Luiz Capitulino | |
338 | a12eeaaa | Luiz Capitulino | static void parse_error(const QErrorStringTable *entry, int c) |
339 | 9f9daf9a | Luiz Capitulino | { |
340 | a12eeaaa | Luiz Capitulino | fprintf(stderr, "expected '%c' in '%s'", c, entry->desc);
|
341 | a12eeaaa | Luiz Capitulino | abort(); |
342 | 9f9daf9a | Luiz Capitulino | } |
343 | 9f9daf9a | Luiz Capitulino | |
344 | a12eeaaa | Luiz Capitulino | static const char *append_field(QDict *error, QString *outstr, |
345 | a12eeaaa | Luiz Capitulino | const QErrorStringTable *entry,
|
346 | 9f9daf9a | Luiz Capitulino | const char *start) |
347 | 9f9daf9a | Luiz Capitulino | { |
348 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
349 | 9f9daf9a | Luiz Capitulino | QDict *qdict; |
350 | 9f9daf9a | Luiz Capitulino | QString *key_qs; |
351 | 9f9daf9a | Luiz Capitulino | const char *end, *key; |
352 | 9f9daf9a | Luiz Capitulino | |
353 | 9f9daf9a | Luiz Capitulino | if (*start != '%') |
354 | a12eeaaa | Luiz Capitulino | parse_error(entry, '%');
|
355 | 9f9daf9a | Luiz Capitulino | start++; |
356 | 9f9daf9a | Luiz Capitulino | if (*start != '(') |
357 | a12eeaaa | Luiz Capitulino | parse_error(entry, '(');
|
358 | 9f9daf9a | Luiz Capitulino | start++; |
359 | 9f9daf9a | Luiz Capitulino | |
360 | 9f9daf9a | Luiz Capitulino | end = strchr(start, ')');
|
361 | 9f9daf9a | Luiz Capitulino | if (!end)
|
362 | a12eeaaa | Luiz Capitulino | parse_error(entry, ')');
|
363 | 9f9daf9a | Luiz Capitulino | |
364 | 9f9daf9a | Luiz Capitulino | key_qs = qstring_from_substr(start, 0, end - start - 1); |
365 | 9f9daf9a | Luiz Capitulino | key = qstring_get_str(key_qs); |
366 | 9f9daf9a | Luiz Capitulino | |
367 | a12eeaaa | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(error, "data"));
|
368 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qdict, key); |
369 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
370 | a12eeaaa | Luiz Capitulino | abort(); |
371 | 9f9daf9a | Luiz Capitulino | } |
372 | 9f9daf9a | Luiz Capitulino | |
373 | 9f9daf9a | Luiz Capitulino | switch (qobject_type(obj)) {
|
374 | 9f9daf9a | Luiz Capitulino | case QTYPE_QSTRING:
|
375 | 9f9daf9a | Luiz Capitulino | qstring_append(outstr, qdict_get_str(qdict, key)); |
376 | 9f9daf9a | Luiz Capitulino | break;
|
377 | 9f9daf9a | Luiz Capitulino | case QTYPE_QINT:
|
378 | 9f9daf9a | Luiz Capitulino | qstring_append_int(outstr, qdict_get_int(qdict, key)); |
379 | 9f9daf9a | Luiz Capitulino | break;
|
380 | 9f9daf9a | Luiz Capitulino | default:
|
381 | a12eeaaa | Luiz Capitulino | abort(); |
382 | 9f9daf9a | Luiz Capitulino | } |
383 | 9f9daf9a | Luiz Capitulino | |
384 | 9f9daf9a | Luiz Capitulino | QDECREF(key_qs); |
385 | 9f9daf9a | Luiz Capitulino | return ++end;
|
386 | 9f9daf9a | Luiz Capitulino | } |
387 | 9f9daf9a | Luiz Capitulino | |
388 | a12eeaaa | Luiz Capitulino | static QString *qerror_format_desc(QDict *error,
|
389 | a12eeaaa | Luiz Capitulino | const QErrorStringTable *entry)
|
390 | 9f9daf9a | Luiz Capitulino | { |
391 | 9f9daf9a | Luiz Capitulino | QString *qstring; |
392 | a12eeaaa | Luiz Capitulino | const char *p; |
393 | 9f9daf9a | Luiz Capitulino | |
394 | a12eeaaa | Luiz Capitulino | assert(entry != NULL);
|
395 | 9f9daf9a | Luiz Capitulino | |
396 | 9f9daf9a | Luiz Capitulino | qstring = qstring_new(); |
397 | 9f9daf9a | Luiz Capitulino | |
398 | a12eeaaa | Luiz Capitulino | for (p = entry->desc; *p != '\0';) { |
399 | 9f9daf9a | Luiz Capitulino | if (*p != '%') { |
400 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, *p++); |
401 | 9f9daf9a | Luiz Capitulino | } else if (*(p + 1) == '%') { |
402 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, '%');
|
403 | 9f9daf9a | Luiz Capitulino | p += 2;
|
404 | 9f9daf9a | Luiz Capitulino | } else {
|
405 | a12eeaaa | Luiz Capitulino | p = append_field(error, qstring, entry, p); |
406 | 9f9daf9a | Luiz Capitulino | } |
407 | 9f9daf9a | Luiz Capitulino | } |
408 | 9f9daf9a | Luiz Capitulino | |
409 | 77e595e7 | Markus Armbruster | return qstring;
|
410 | 77e595e7 | Markus Armbruster | } |
411 | 77e595e7 | Markus Armbruster | |
412 | 87c2f591 | Luiz Capitulino | QString *qerror_format(const char *fmt, QDict *error) |
413 | 87c2f591 | Luiz Capitulino | { |
414 | 87c2f591 | Luiz Capitulino | const QErrorStringTable *entry = NULL; |
415 | 87c2f591 | Luiz Capitulino | int i;
|
416 | 87c2f591 | Luiz Capitulino | |
417 | 87c2f591 | Luiz Capitulino | for (i = 0; qerror_table[i].error_fmt; i++) { |
418 | 87c2f591 | Luiz Capitulino | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
419 | 87c2f591 | Luiz Capitulino | entry = &qerror_table[i]; |
420 | 87c2f591 | Luiz Capitulino | break;
|
421 | 87c2f591 | Luiz Capitulino | } |
422 | 87c2f591 | Luiz Capitulino | } |
423 | 87c2f591 | Luiz Capitulino | |
424 | 87c2f591 | Luiz Capitulino | return qerror_format_desc(error, entry);
|
425 | 87c2f591 | Luiz Capitulino | } |
426 | 87c2f591 | Luiz Capitulino | |
427 | 77e595e7 | Markus Armbruster | /**
|
428 | a12eeaaa | Luiz Capitulino | * qerror_human(): Format QError data into human-readable string.
|
429 | a12eeaaa | Luiz Capitulino | */
|
430 | a12eeaaa | Luiz Capitulino | QString *qerror_human(const QError *qerror)
|
431 | a12eeaaa | Luiz Capitulino | { |
432 | a12eeaaa | Luiz Capitulino | return qerror_format_desc(qerror->error, qerror->entry);
|
433 | a12eeaaa | Luiz Capitulino | } |
434 | a12eeaaa | Luiz Capitulino | |
435 | a12eeaaa | Luiz Capitulino | /**
|
436 | 77e595e7 | Markus Armbruster | * qerror_print(): Print QError data
|
437 | 77e595e7 | Markus Armbruster | *
|
438 | 77e595e7 | Markus Armbruster | * This function will print the member 'desc' of the specified QError object,
|
439 | 1ecda02b | Markus Armbruster | * it uses error_report() for this, so that the output is routed to the right
|
440 | 77e595e7 | Markus Armbruster | * place (ie. stderr or Monitor's device).
|
441 | 77e595e7 | Markus Armbruster | */
|
442 | 827b0813 | Markus Armbruster | void qerror_print(QError *qerror)
|
443 | 77e595e7 | Markus Armbruster | { |
444 | 77e595e7 | Markus Armbruster | QString *qstring = qerror_human(qerror); |
445 | 827b0813 | Markus Armbruster | loc_push_restore(&qerror->loc); |
446 | 1ecda02b | Markus Armbruster | error_report("%s", qstring_get_str(qstring));
|
447 | 827b0813 | Markus Armbruster | loc_pop(&qerror->loc); |
448 | 9f9daf9a | Luiz Capitulino | QDECREF(qstring); |
449 | 9f9daf9a | Luiz Capitulino | } |
450 | 9f9daf9a | Luiz Capitulino | |
451 | 0167f772 | Markus Armbruster | void qerror_report_internal(const char *file, int linenr, const char *func, |
452 | 0167f772 | Markus Armbruster | const char *fmt, ...) |
453 | 0167f772 | Markus Armbruster | { |
454 | 0167f772 | Markus Armbruster | va_list va; |
455 | 0167f772 | Markus Armbruster | QError *qerror; |
456 | 0167f772 | Markus Armbruster | |
457 | 0167f772 | Markus Armbruster | va_start(va, fmt); |
458 | 0167f772 | Markus Armbruster | qerror = qerror_from_info(file, linenr, func, fmt, &va); |
459 | 0167f772 | Markus Armbruster | va_end(va); |
460 | 0167f772 | Markus Armbruster | |
461 | 0167f772 | Markus Armbruster | if (monitor_cur_is_qmp()) {
|
462 | 0167f772 | Markus Armbruster | monitor_set_error(cur_mon, qerror); |
463 | 0167f772 | Markus Armbruster | } else {
|
464 | 0167f772 | Markus Armbruster | qerror_print(qerror); |
465 | 0167f772 | Markus Armbruster | QDECREF(qerror); |
466 | 0167f772 | Markus Armbruster | } |
467 | 0167f772 | Markus Armbruster | } |
468 | 0167f772 | Markus Armbruster | |
469 | 9f9daf9a | Luiz Capitulino | /**
|
470 | 9f9daf9a | Luiz Capitulino | * qobject_to_qerror(): Convert a QObject into a QError
|
471 | 9f9daf9a | Luiz Capitulino | */
|
472 | 9f9daf9a | Luiz Capitulino | QError *qobject_to_qerror(const QObject *obj)
|
473 | 9f9daf9a | Luiz Capitulino | { |
474 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QERROR) {
|
475 | 9f9daf9a | Luiz Capitulino | return NULL; |
476 | 9f9daf9a | Luiz Capitulino | } |
477 | 9f9daf9a | Luiz Capitulino | |
478 | 9f9daf9a | Luiz Capitulino | return container_of(obj, QError, base);
|
479 | 9f9daf9a | Luiz Capitulino | } |
480 | 9f9daf9a | Luiz Capitulino | |
481 | 9f9daf9a | Luiz Capitulino | /**
|
482 | 9f9daf9a | Luiz Capitulino | * qerror_destroy_obj(): Free all memory allocated by a QError
|
483 | 9f9daf9a | Luiz Capitulino | */
|
484 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj) |
485 | 9f9daf9a | Luiz Capitulino | { |
486 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
487 | 9f9daf9a | Luiz Capitulino | |
488 | 9f9daf9a | Luiz Capitulino | assert(obj != NULL);
|
489 | 9f9daf9a | Luiz Capitulino | qerr = qobject_to_qerror(obj); |
490 | 9f9daf9a | Luiz Capitulino | |
491 | 9f9daf9a | Luiz Capitulino | QDECREF(qerr->error); |
492 | 9f9daf9a | Luiz Capitulino | qemu_free(qerr); |
493 | 9f9daf9a | Luiz Capitulino | } |