root / qerror.c @ 528f7663
History | View | Annotate | Download (11.4 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 | 7bc84017 | Markus Armbruster | .error_fmt = QERR_DUPLICATE_ID, |
105 | 7bc84017 | Markus Armbruster | .desc = "Duplicate ID '%(id)' for %(object)",
|
106 | 7bc84017 | Markus Armbruster | }, |
107 | 7bc84017 | Markus Armbruster | { |
108 | c7c338c4 | Markus Armbruster | .error_fmt = QERR_FD_NOT_FOUND, |
109 | fc5469d8 | Markus Armbruster | .desc = "File descriptor named '%(name)' not found",
|
110 | c7c338c4 | Markus Armbruster | }, |
111 | c7c338c4 | Markus Armbruster | { |
112 | 41471a23 | Markus Armbruster | .error_fmt = QERR_FD_NOT_SUPPLIED, |
113 | 41471a23 | Markus Armbruster | .desc = "No file descriptor supplied via SCM_RIGHTS",
|
114 | 41471a23 | Markus Armbruster | }, |
115 | 41471a23 | Markus Armbruster | { |
116 | 17901e75 | Markus Armbruster | .error_fmt = QERR_INVALID_BLOCK_FORMAT, |
117 | fc5469d8 | Markus Armbruster | .desc = "Invalid block format '%(name)'",
|
118 | 17901e75 | Markus Armbruster | }, |
119 | 17901e75 | Markus Armbruster | { |
120 | 7a046f5f | Markus Armbruster | .error_fmt = QERR_INVALID_PARAMETER, |
121 | fc5469d8 | Markus Armbruster | .desc = "Invalid parameter '%(name)'",
|
122 | 7a046f5f | Markus Armbruster | }, |
123 | 7a046f5f | Markus Armbruster | { |
124 | e16a1812 | Markus Armbruster | .error_fmt = QERR_INVALID_PARAMETER_TYPE, |
125 | e16a1812 | Markus Armbruster | .desc = "Invalid parameter type, expected: %(expected)",
|
126 | 055f6122 | Luiz Capitulino | }, |
127 | 82a60711 | Luiz Capitulino | { |
128 | 985a3e52 | Markus Armbruster | .error_fmt = QERR_INVALID_PARAMETER_VALUE, |
129 | 985a3e52 | Markus Armbruster | .desc = "Parameter '%(name)' expects %(expected)",
|
130 | 985a3e52 | Markus Armbruster | }, |
131 | 985a3e52 | Markus Armbruster | { |
132 | e16a1812 | Markus Armbruster | .error_fmt = QERR_INVALID_PASSWORD, |
133 | fc5469d8 | Markus Armbruster | .desc = "Password incorrect",
|
134 | 4b9d4683 | Luiz Capitulino | }, |
135 | 4b9d4683 | Luiz Capitulino | { |
136 | e16a1812 | Markus Armbruster | .error_fmt = QERR_JSON_PARSING, |
137 | e16a1812 | Markus Armbruster | .desc = "Invalid JSON syntax",
|
138 | f6d855c5 | Luiz Capitulino | }, |
139 | f6d855c5 | Luiz Capitulino | { |
140 | 82a60711 | Luiz Capitulino | .error_fmt = QERR_KVM_MISSING_CAP, |
141 | 82a60711 | Luiz Capitulino | .desc = "Using KVM without %(capability), %(feature) unavailable",
|
142 | 82a60711 | Luiz Capitulino | }, |
143 | 4b9d4683 | Luiz Capitulino | { |
144 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_MISSING_PARAMETER, |
145 | fc5469d8 | Markus Armbruster | .desc = "Parameter '%(name)' is missing",
|
146 | fc5469d8 | Markus Armbruster | }, |
147 | fc5469d8 | Markus Armbruster | { |
148 | fab5767f | Markus Armbruster | .error_fmt = QERR_NO_BUS_FOR_DEVICE, |
149 | fab5767f | Markus Armbruster | .desc = "No '%(bus)' bus found for device '%(device)'",
|
150 | fab5767f | Markus Armbruster | }, |
151 | fab5767f | Markus Armbruster | { |
152 | fc5469d8 | Markus Armbruster | .error_fmt = QERR_OPEN_FILE_FAILED, |
153 | fc5469d8 | Markus Armbruster | .desc = "Could not open '%(filename)'",
|
154 | 4b9d4683 | Luiz Capitulino | }, |
155 | 4b9d4683 | Luiz Capitulino | { |
156 | c58a35f8 | Markus Armbruster | .error_fmt = QERR_PROPERTY_NOT_FOUND, |
157 | c58a35f8 | Markus Armbruster | .desc = "Property '%(device).%(property)' not found",
|
158 | c58a35f8 | Markus Armbruster | }, |
159 | c58a35f8 | Markus Armbruster | { |
160 | 06b4a703 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_BAD, |
161 | 06b4a703 | Markus Armbruster | .desc = "Property '%(device).%(property)' doesn't take value '%(value)'",
|
162 | 06b4a703 | Markus Armbruster | }, |
163 | 06b4a703 | Markus Armbruster | { |
164 | 9c5eff95 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_IN_USE, |
165 | 9c5eff95 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't take value '%(value)', it's in use",
|
166 | 9c5eff95 | Markus Armbruster | }, |
167 | 9c5eff95 | Markus Armbruster | { |
168 | 84745d68 | Markus Armbruster | .error_fmt = QERR_PROPERTY_VALUE_NOT_FOUND, |
169 | 84745d68 | Markus Armbruster | .desc = "Property '%(device).%(property)' can't find value '%(value)'",
|
170 | 84745d68 | Markus Armbruster | }, |
171 | 84745d68 | Markus Armbruster | { |
172 | 4b9d4683 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT, |
173 | abaf2f52 | Luiz Capitulino | .desc = "Expected '%(expected)' in QMP input",
|
174 | 4b9d4683 | Luiz Capitulino | }, |
175 | 4b9d4683 | Luiz Capitulino | { |
176 | 7dfb6123 | Luiz Capitulino | .error_fmt = QERR_QMP_BAD_INPUT_OBJECT_MEMBER, |
177 | 7dfb6123 | Luiz Capitulino | .desc = "QMP input object member '%(member)' expects '%(expected)'",
|
178 | 7dfb6123 | Luiz Capitulino | }, |
179 | 7dfb6123 | Luiz Capitulino | { |
180 | 60d76d7b | Luiz Capitulino | .error_fmt = QERR_QMP_EXTRA_MEMBER, |
181 | 60d76d7b | Luiz Capitulino | .desc = "QMP input object member '%(member)' is unexpected",
|
182 | 60d76d7b | Luiz Capitulino | }, |
183 | 60d76d7b | Luiz Capitulino | { |
184 | 7a84cb23 | Markus Armbruster | .error_fmt = QERR_SET_PASSWD_FAILED, |
185 | 7a84cb23 | Markus Armbruster | .desc = "Could not set password",
|
186 | 7a84cb23 | Markus Armbruster | }, |
187 | 7a84cb23 | Markus Armbruster | { |
188 | a488be27 | Markus Armbruster | .error_fmt = QERR_TOO_MANY_FILES, |
189 | a488be27 | Markus Armbruster | .desc = "Too many open files",
|
190 | a488be27 | Markus Armbruster | }, |
191 | a488be27 | Markus Armbruster | { |
192 | e16a1812 | Markus Armbruster | .error_fmt = QERR_UNDEFINED_ERROR, |
193 | e16a1812 | Markus Armbruster | .desc = "An undefined error has ocurred",
|
194 | 4b9d4683 | Luiz Capitulino | }, |
195 | a6906e31 | Markus Armbruster | { |
196 | a6906e31 | Markus Armbruster | .error_fmt = QERR_VNC_SERVER_FAILED, |
197 | a6906e31 | Markus Armbruster | .desc = "Could not start VNC server on %(target)",
|
198 | a6906e31 | Markus Armbruster | }, |
199 | 9f9daf9a | Luiz Capitulino | {} |
200 | 9f9daf9a | Luiz Capitulino | }; |
201 | 9f9daf9a | Luiz Capitulino | |
202 | 9f9daf9a | Luiz Capitulino | /**
|
203 | 9f9daf9a | Luiz Capitulino | * qerror_new(): Create a new QError
|
204 | 9f9daf9a | Luiz Capitulino | *
|
205 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
206 | 9f9daf9a | Luiz Capitulino | */
|
207 | 9f9daf9a | Luiz Capitulino | QError *qerror_new(void)
|
208 | 9f9daf9a | Luiz Capitulino | { |
209 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
210 | 9f9daf9a | Luiz Capitulino | |
211 | 9f9daf9a | Luiz Capitulino | qerr = qemu_mallocz(sizeof(*qerr));
|
212 | 9f9daf9a | Luiz Capitulino | QOBJECT_INIT(qerr, &qerror_type); |
213 | 9f9daf9a | Luiz Capitulino | |
214 | 9f9daf9a | Luiz Capitulino | return qerr;
|
215 | 9f9daf9a | Luiz Capitulino | } |
216 | 9f9daf9a | Luiz Capitulino | |
217 | 9f9daf9a | Luiz Capitulino | static void qerror_abort(const QError *qerr, const char *fmt, ...) |
218 | 9f9daf9a | Luiz Capitulino | { |
219 | 9f9daf9a | Luiz Capitulino | va_list ap; |
220 | 9f9daf9a | Luiz Capitulino | |
221 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func);
|
222 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "qerror: -> ");
|
223 | 9f9daf9a | Luiz Capitulino | |
224 | 9f9daf9a | Luiz Capitulino | va_start(ap, fmt); |
225 | 9f9daf9a | Luiz Capitulino | vfprintf(stderr, fmt, ap); |
226 | 9f9daf9a | Luiz Capitulino | va_end(ap); |
227 | 9f9daf9a | Luiz Capitulino | |
228 | 9f9daf9a | Luiz Capitulino | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr);
|
229 | 9f9daf9a | Luiz Capitulino | abort(); |
230 | 9f9daf9a | Luiz Capitulino | } |
231 | 9f9daf9a | Luiz Capitulino | |
232 | 9f9daf9a | Luiz Capitulino | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) |
233 | 9f9daf9a | Luiz Capitulino | { |
234 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
235 | 9f9daf9a | Luiz Capitulino | |
236 | 9f9daf9a | Luiz Capitulino | obj = qobject_from_jsonv(fmt, va); |
237 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
238 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "invalid format '%s'", fmt);
|
239 | 9f9daf9a | Luiz Capitulino | } |
240 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
241 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format is not a QDict '%s'", fmt);
|
242 | 9f9daf9a | Luiz Capitulino | } |
243 | 9f9daf9a | Luiz Capitulino | |
244 | 9f9daf9a | Luiz Capitulino | qerr->error = qobject_to_qdict(obj); |
245 | 9f9daf9a | Luiz Capitulino | |
246 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "class");
|
247 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
248 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'class' key in '%s'", fmt);
|
249 | 9f9daf9a | Luiz Capitulino | } |
250 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QSTRING) {
|
251 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'class' key value should be a QString");
|
252 | 9f9daf9a | Luiz Capitulino | } |
253 | 9f9daf9a | Luiz Capitulino | |
254 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qerr->error, "data");
|
255 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
256 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "missing 'data' key in '%s'", fmt);
|
257 | 9f9daf9a | Luiz Capitulino | } |
258 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QDICT) {
|
259 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "'data' key value should be a QDICT");
|
260 | 9f9daf9a | Luiz Capitulino | } |
261 | 9f9daf9a | Luiz Capitulino | } |
262 | 9f9daf9a | Luiz Capitulino | |
263 | 9f9daf9a | Luiz Capitulino | static void qerror_set_desc(QError *qerr, const char *fmt) |
264 | 9f9daf9a | Luiz Capitulino | { |
265 | 9f9daf9a | Luiz Capitulino | int i;
|
266 | 9f9daf9a | Luiz Capitulino | |
267 | 9f9daf9a | Luiz Capitulino | // FIXME: inefficient loop
|
268 | 9f9daf9a | Luiz Capitulino | |
269 | 9f9daf9a | Luiz Capitulino | for (i = 0; qerror_table[i].error_fmt; i++) { |
270 | 9f9daf9a | Luiz Capitulino | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
271 | 9f9daf9a | Luiz Capitulino | qerr->entry = &qerror_table[i]; |
272 | 9f9daf9a | Luiz Capitulino | return;
|
273 | 9f9daf9a | Luiz Capitulino | } |
274 | 9f9daf9a | Luiz Capitulino | } |
275 | 9f9daf9a | Luiz Capitulino | |
276 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "error format '%s' not found", fmt);
|
277 | 9f9daf9a | Luiz Capitulino | } |
278 | 9f9daf9a | Luiz Capitulino | |
279 | 9f9daf9a | Luiz Capitulino | /**
|
280 | 9f9daf9a | Luiz Capitulino | * qerror_from_info(): Create a new QError from error information
|
281 | 9f9daf9a | Luiz Capitulino | *
|
282 | 9f9daf9a | Luiz Capitulino | * The information consists of:
|
283 | 9f9daf9a | Luiz Capitulino | *
|
284 | 9f9daf9a | Luiz Capitulino | * - file the file name of where the error occurred
|
285 | 9f9daf9a | Luiz Capitulino | * - linenr the line number of where the error occurred
|
286 | 9f9daf9a | Luiz Capitulino | * - func the function name of where the error occurred
|
287 | 9f9daf9a | Luiz Capitulino | * - fmt JSON printf-like dictionary, there must exist keys 'class' and
|
288 | 9f9daf9a | Luiz Capitulino | * 'data'
|
289 | 9f9daf9a | Luiz Capitulino | * - va va_list of all arguments specified by fmt
|
290 | 9f9daf9a | Luiz Capitulino | *
|
291 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
292 | 9f9daf9a | Luiz Capitulino | */
|
293 | 9f9daf9a | Luiz Capitulino | QError *qerror_from_info(const char *file, int linenr, const char *func, |
294 | 9f9daf9a | Luiz Capitulino | const char *fmt, va_list *va) |
295 | 9f9daf9a | Luiz Capitulino | { |
296 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
297 | 9f9daf9a | Luiz Capitulino | |
298 | 9f9daf9a | Luiz Capitulino | qerr = qerror_new(); |
299 | 827b0813 | Markus Armbruster | loc_save(&qerr->loc); |
300 | 9f9daf9a | Luiz Capitulino | qerr->linenr = linenr; |
301 | 9f9daf9a | Luiz Capitulino | qerr->file = file; |
302 | 9f9daf9a | Luiz Capitulino | qerr->func = func; |
303 | 9f9daf9a | Luiz Capitulino | |
304 | 9f9daf9a | Luiz Capitulino | if (!fmt) {
|
305 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerr, "QDict not specified");
|
306 | 9f9daf9a | Luiz Capitulino | } |
307 | 9f9daf9a | Luiz Capitulino | |
308 | 9f9daf9a | Luiz Capitulino | qerror_set_data(qerr, fmt, va); |
309 | 9f9daf9a | Luiz Capitulino | qerror_set_desc(qerr, fmt); |
310 | 9f9daf9a | Luiz Capitulino | |
311 | 9f9daf9a | Luiz Capitulino | return qerr;
|
312 | 9f9daf9a | Luiz Capitulino | } |
313 | 9f9daf9a | Luiz Capitulino | |
314 | 9f9daf9a | Luiz Capitulino | static void parse_error(const QError *qerror, int c) |
315 | 9f9daf9a | Luiz Capitulino | { |
316 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc);
|
317 | 9f9daf9a | Luiz Capitulino | } |
318 | 9f9daf9a | Luiz Capitulino | |
319 | 9f9daf9a | Luiz Capitulino | static const char *append_field(QString *outstr, const QError *qerror, |
320 | 9f9daf9a | Luiz Capitulino | const char *start) |
321 | 9f9daf9a | Luiz Capitulino | { |
322 | 9f9daf9a | Luiz Capitulino | QObject *obj; |
323 | 9f9daf9a | Luiz Capitulino | QDict *qdict; |
324 | 9f9daf9a | Luiz Capitulino | QString *key_qs; |
325 | 9f9daf9a | Luiz Capitulino | const char *end, *key; |
326 | 9f9daf9a | Luiz Capitulino | |
327 | 9f9daf9a | Luiz Capitulino | if (*start != '%') |
328 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '%');
|
329 | 9f9daf9a | Luiz Capitulino | start++; |
330 | 9f9daf9a | Luiz Capitulino | if (*start != '(') |
331 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, '(');
|
332 | 9f9daf9a | Luiz Capitulino | start++; |
333 | 9f9daf9a | Luiz Capitulino | |
334 | 9f9daf9a | Luiz Capitulino | end = strchr(start, ')');
|
335 | 9f9daf9a | Luiz Capitulino | if (!end)
|
336 | 9f9daf9a | Luiz Capitulino | parse_error(qerror, ')');
|
337 | 9f9daf9a | Luiz Capitulino | |
338 | 9f9daf9a | Luiz Capitulino | key_qs = qstring_from_substr(start, 0, end - start - 1); |
339 | 9f9daf9a | Luiz Capitulino | key = qstring_get_str(key_qs); |
340 | 9f9daf9a | Luiz Capitulino | |
341 | 9f9daf9a | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(qerror->error, "data"));
|
342 | 9f9daf9a | Luiz Capitulino | obj = qdict_get(qdict, key); |
343 | 9f9daf9a | Luiz Capitulino | if (!obj) {
|
344 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "key '%s' not found in QDict", key);
|
345 | 9f9daf9a | Luiz Capitulino | } |
346 | 9f9daf9a | Luiz Capitulino | |
347 | 9f9daf9a | Luiz Capitulino | switch (qobject_type(obj)) {
|
348 | 9f9daf9a | Luiz Capitulino | case QTYPE_QSTRING:
|
349 | 9f9daf9a | Luiz Capitulino | qstring_append(outstr, qdict_get_str(qdict, key)); |
350 | 9f9daf9a | Luiz Capitulino | break;
|
351 | 9f9daf9a | Luiz Capitulino | case QTYPE_QINT:
|
352 | 9f9daf9a | Luiz Capitulino | qstring_append_int(outstr, qdict_get_int(qdict, key)); |
353 | 9f9daf9a | Luiz Capitulino | break;
|
354 | 9f9daf9a | Luiz Capitulino | default:
|
355 | 9f9daf9a | Luiz Capitulino | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj));
|
356 | 9f9daf9a | Luiz Capitulino | } |
357 | 9f9daf9a | Luiz Capitulino | |
358 | 9f9daf9a | Luiz Capitulino | QDECREF(key_qs); |
359 | 9f9daf9a | Luiz Capitulino | return ++end;
|
360 | 9f9daf9a | Luiz Capitulino | } |
361 | 9f9daf9a | Luiz Capitulino | |
362 | 9f9daf9a | Luiz Capitulino | /**
|
363 | 77e595e7 | Markus Armbruster | * qerror_human(): Format QError data into human-readable string.
|
364 | 9f9daf9a | Luiz Capitulino | *
|
365 | 77e595e7 | Markus Armbruster | * Formats according to member 'desc' of the specified QError object.
|
366 | 9f9daf9a | Luiz Capitulino | */
|
367 | 77e595e7 | Markus Armbruster | QString *qerror_human(const QError *qerror)
|
368 | 9f9daf9a | Luiz Capitulino | { |
369 | 9f9daf9a | Luiz Capitulino | const char *p; |
370 | 9f9daf9a | Luiz Capitulino | QString *qstring; |
371 | 9f9daf9a | Luiz Capitulino | |
372 | 9f9daf9a | Luiz Capitulino | assert(qerror->entry != NULL);
|
373 | 9f9daf9a | Luiz Capitulino | |
374 | 9f9daf9a | Luiz Capitulino | qstring = qstring_new(); |
375 | 9f9daf9a | Luiz Capitulino | |
376 | 9f9daf9a | Luiz Capitulino | for (p = qerror->entry->desc; *p != '\0';) { |
377 | 9f9daf9a | Luiz Capitulino | if (*p != '%') { |
378 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, *p++); |
379 | 9f9daf9a | Luiz Capitulino | } else if (*(p + 1) == '%') { |
380 | 9f9daf9a | Luiz Capitulino | qstring_append_chr(qstring, '%');
|
381 | 9f9daf9a | Luiz Capitulino | p += 2;
|
382 | 9f9daf9a | Luiz Capitulino | } else {
|
383 | 9f9daf9a | Luiz Capitulino | p = append_field(qstring, qerror, p); |
384 | 9f9daf9a | Luiz Capitulino | } |
385 | 9f9daf9a | Luiz Capitulino | } |
386 | 9f9daf9a | Luiz Capitulino | |
387 | 77e595e7 | Markus Armbruster | return qstring;
|
388 | 77e595e7 | Markus Armbruster | } |
389 | 77e595e7 | Markus Armbruster | |
390 | 77e595e7 | Markus Armbruster | /**
|
391 | 77e595e7 | Markus Armbruster | * qerror_print(): Print QError data
|
392 | 77e595e7 | Markus Armbruster | *
|
393 | 77e595e7 | Markus Armbruster | * This function will print the member 'desc' of the specified QError object,
|
394 | 1ecda02b | Markus Armbruster | * it uses error_report() for this, so that the output is routed to the right
|
395 | 77e595e7 | Markus Armbruster | * place (ie. stderr or Monitor's device).
|
396 | 77e595e7 | Markus Armbruster | */
|
397 | 827b0813 | Markus Armbruster | void qerror_print(QError *qerror)
|
398 | 77e595e7 | Markus Armbruster | { |
399 | 77e595e7 | Markus Armbruster | QString *qstring = qerror_human(qerror); |
400 | 827b0813 | Markus Armbruster | loc_push_restore(&qerror->loc); |
401 | 1ecda02b | Markus Armbruster | error_report("%s", qstring_get_str(qstring));
|
402 | 827b0813 | Markus Armbruster | loc_pop(&qerror->loc); |
403 | 9f9daf9a | Luiz Capitulino | QDECREF(qstring); |
404 | 9f9daf9a | Luiz Capitulino | } |
405 | 9f9daf9a | Luiz Capitulino | |
406 | 0167f772 | Markus Armbruster | void qerror_report_internal(const char *file, int linenr, const char *func, |
407 | 0167f772 | Markus Armbruster | const char *fmt, ...) |
408 | 0167f772 | Markus Armbruster | { |
409 | 0167f772 | Markus Armbruster | va_list va; |
410 | 0167f772 | Markus Armbruster | QError *qerror; |
411 | 0167f772 | Markus Armbruster | |
412 | 0167f772 | Markus Armbruster | va_start(va, fmt); |
413 | 0167f772 | Markus Armbruster | qerror = qerror_from_info(file, linenr, func, fmt, &va); |
414 | 0167f772 | Markus Armbruster | va_end(va); |
415 | 0167f772 | Markus Armbruster | |
416 | 0167f772 | Markus Armbruster | if (monitor_cur_is_qmp()) {
|
417 | 0167f772 | Markus Armbruster | monitor_set_error(cur_mon, qerror); |
418 | 0167f772 | Markus Armbruster | } else {
|
419 | 0167f772 | Markus Armbruster | qerror_print(qerror); |
420 | 0167f772 | Markus Armbruster | QDECREF(qerror); |
421 | 0167f772 | Markus Armbruster | } |
422 | 0167f772 | Markus Armbruster | } |
423 | 0167f772 | Markus Armbruster | |
424 | 9f9daf9a | Luiz Capitulino | /**
|
425 | 9f9daf9a | Luiz Capitulino | * qobject_to_qerror(): Convert a QObject into a QError
|
426 | 9f9daf9a | Luiz Capitulino | */
|
427 | 9f9daf9a | Luiz Capitulino | QError *qobject_to_qerror(const QObject *obj)
|
428 | 9f9daf9a | Luiz Capitulino | { |
429 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QERROR) {
|
430 | 9f9daf9a | Luiz Capitulino | return NULL; |
431 | 9f9daf9a | Luiz Capitulino | } |
432 | 9f9daf9a | Luiz Capitulino | |
433 | 9f9daf9a | Luiz Capitulino | return container_of(obj, QError, base);
|
434 | 9f9daf9a | Luiz Capitulino | } |
435 | 9f9daf9a | Luiz Capitulino | |
436 | 9f9daf9a | Luiz Capitulino | /**
|
437 | 9f9daf9a | Luiz Capitulino | * qerror_destroy_obj(): Free all memory allocated by a QError
|
438 | 9f9daf9a | Luiz Capitulino | */
|
439 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj) |
440 | 9f9daf9a | Luiz Capitulino | { |
441 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
442 | 9f9daf9a | Luiz Capitulino | |
443 | 9f9daf9a | Luiz Capitulino | assert(obj != NULL);
|
444 | 9f9daf9a | Luiz Capitulino | qerr = qobject_to_qerror(obj); |
445 | 9f9daf9a | Luiz Capitulino | |
446 | 9f9daf9a | Luiz Capitulino | QDECREF(qerr->error); |
447 | 9f9daf9a | Luiz Capitulino | qemu_free(qerr); |
448 | 9f9daf9a | Luiz Capitulino | } |