]> git.proxmox.com Git - mirror_qemu.git/blame - include/qapi/error.h
Replace GCC_FMT_ATTR with G_GNUC_PRINTF
[mirror_qemu.git] / include / qapi / error.h
CommitLineData
d5ec4f27
LC
1/*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
edf6f3b3 5 * Copyright (C) 2011-2015 Red Hat, Inc.
d5ec4f27
LC
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
edf6f3b3 9 * Markus Armbruster <armbru@redhat.com>
d5ec4f27
LC
10 *
11 * This work is licensed under the terms of the GNU LGPL, version 2. See
12 * the COPYING.LIB file in the top-level directory.
13 */
edf6f3b3
MA
14
15/*
16 * Error reporting system loosely patterned after Glib's GError.
17 *
e3fe3988
MA
18 * = Rules =
19 *
20 * - Functions that use Error to report errors have an Error **errp
21 * parameter. It should be the last parameter, except for functions
22 * taking variable arguments.
23 *
24 * - You may pass NULL to not receive the error, &error_abort to abort
25 * on error, &error_fatal to exit(1) on error, or a pointer to a
26 * variable containing NULL to receive the error.
27 *
28 * - Separation of concerns: the function is responsible for detecting
29 * errors and failing cleanly; handling the error is its caller's
30 * job. Since the value of @errp is about handling the error, the
31 * function should not examine it.
32 *
ae7c80a7
VSO
33 * - The function may pass @errp to functions it calls to pass on
34 * their errors to its caller. If it dereferences @errp to check
35 * for errors, it must use ERRP_GUARD().
36 *
e3fe3988
MA
37 * - On success, the function should not touch *errp. On failure, it
38 * should set a new error, e.g. with error_setg(errp, ...), or
39 * propagate an existing one, e.g. with error_propagate(errp, ...).
40 *
41 * - Whenever practical, also return a value that indicates success /
42 * failure. This can make the error checking more concise, and can
43 * avoid useless error object creation and destruction. Note that
44 * we still have many functions returning void. We recommend
45 * • bool-valued functions return true on success / false on failure,
46 * • pointer-valued functions return non-null / null pointer, and
47 * • integer-valued functions return non-negative / negative.
48 *
9aac7d48
MA
49 * = Creating errors =
50 *
edf6f3b3 51 * Create an error:
ae7c80a7
VSO
52 * error_setg(errp, "situation normal, all fouled up");
53 * where @errp points to the location to receive the error.
edf6f3b3 54 *
f4d0064a 55 * Create an error and add additional explanation:
ae7c80a7
VSO
56 * error_setg(errp, "invalid quark");
57 * error_append_hint(errp, "Valid quarks are up, down, strange, "
f4d0064a 58 * "charm, top, bottom.\n");
ae7c80a7 59 * This may require use of ERRP_GUARD(); more on that below.
f4d0064a
MA
60 *
61 * Do *not* contract this to
ae7c80a7 62 * error_setg(errp, "invalid quark\n" // WRONG!
f4d0064a
MA
63 * "Valid quarks are up, down, strange, charm, top, bottom.");
64 *
9aac7d48
MA
65 * = Reporting and destroying errors =
66 *
10303f04 67 * Report an error to the current monitor if we have one, else stderr:
edf6f3b3
MA
68 * error_report_err(err);
69 * This frees the error object.
70 *
10303f04 71 * Likewise, but with additional text prepended:
8277d2aa
MA
72 * error_reportf_err(err, "Could not frobnicate '%s': ", name);
73 *
edf6f3b3
MA
74 * Report an error somewhere else:
75 * const char *msg = error_get_pretty(err);
76 * do with msg what needs to be done...
77 * error_free(err);
f4d0064a 78 * Note that this loses hints added with error_append_hint().
edf6f3b3 79 *
9aac7d48
MA
80 * Call a function ignoring errors:
81 * foo(arg, NULL);
82 * This is more concise than
83 * Error *err = NULL;
84 * foo(arg, &err);
85 * error_free(err); // don't do this
86 *
87 * Call a function aborting on errors:
88 * foo(arg, &error_abort);
89 * This is more concise and fails more nicely than
90 * Error *err = NULL;
91 * foo(arg, &err);
92 * assert(!err); // don't do this
93 *
94 * Call a function treating errors as fatal:
95 * foo(arg, &error_fatal);
96 * This is more concise than
97 * Error *err = NULL;
98 * foo(arg, &err);
99 * if (err) { // don't do this
100 * error_report_err(err);
101 * exit(1);
102 * }
103 *
edf6f3b3
MA
104 * Handle an error without reporting it (just for completeness):
105 * error_free(err);
106 *
a12a5a1a
EB
107 * Assert that an expected error occurred, but clean it up without
108 * reporting it (primarily useful in testsuites):
109 * error_free_or_abort(&err);
110 *
9aac7d48
MA
111 * = Passing errors around =
112 *
113 * Errors get passed to the caller through the conventional @errp
114 * parameter.
115 *
edf6f3b3
MA
116 * Create a new error and pass it to the caller:
117 * error_setg(errp, "situation normal, all fouled up");
118 *
e3fe3988
MA
119 * Call a function, receive an error from it, and pass it to the caller
120 * - when the function returns a value that indicates failure, say
121 * false:
122 * if (!foo(arg, errp)) {
edf6f3b3
MA
123 * handle the error...
124 * }
e3fe3988 125 * - when it does not, say because it is a void function:
ae7c80a7
VSO
126 * ERRP_GUARD();
127 * foo(arg, errp);
128 * if (*errp) {
129 * handle the error...
130 * }
131 * More on ERRP_GUARD() below.
132 *
133 * Code predating ERRP_GUARD() still exists, and looks like this:
edf6f3b3
MA
134 * Error *err = NULL;
135 * foo(arg, &err);
136 * if (err) {
137 * handle the error...
ae7c80a7 138 * error_propagate(errp, err); // deprecated
edf6f3b3 139 * }
ae7c80a7 140 * Avoid in new code. Do *not* "optimize" it to
edf6f3b3
MA
141 * foo(arg, errp);
142 * if (*errp) { // WRONG!
143 * handle the error...
144 * }
ae7c80a7 145 * because errp may be NULL without the ERRP_GUARD() guard.
edf6f3b3
MA
146 *
147 * But when all you do with the error is pass it on, please use
148 * foo(arg, errp);
149 * for readability.
8d780f43 150 *
e3fe3988
MA
151 * Receive an error, and handle it locally
152 * - when the function returns a value that indicates failure, say
153 * false:
154 * Error *err = NULL;
155 * if (!foo(arg, &err)) {
156 * handle the error...
157 * }
158 * - when it does not, say because it is a void function:
159 * Error *err = NULL;
160 * foo(arg, &err);
161 * if (err) {
162 * handle the error...
163 * }
164 *
ae7c80a7
VSO
165 * Pass an existing error to the caller:
166 * error_propagate(errp, err);
167 * This is rarely needed. When @err is a local variable, use of
168 * ERRP_GUARD() commonly results in more readable code.
169 *
170 * Pass an existing error to the caller with the message modified:
171 * error_propagate_prepend(errp, err,
172 * "Could not frobnicate '%s': ", name);
173 * This is more concise than
174 * error_propagate(errp, err); // don't do this
175 * error_prepend(errp, "Could not frobnicate '%s': ", name);
176 * and works even when @errp is &error_fatal.
177 *
8d780f43
MA
178 * Receive and accumulate multiple errors (first one wins):
179 * Error *err = NULL, *local_err = NULL;
180 * foo(arg, &err);
181 * bar(arg, &local_err);
182 * error_propagate(&err, local_err);
183 * if (err) {
184 * handle the error...
185 * }
186 *
187 * Do *not* "optimize" this to
47ff5ac8 188 * Error *err = NULL;
8d780f43
MA
189 * foo(arg, &err);
190 * bar(arg, &err); // WRONG!
191 * if (err) {
192 * handle the error...
193 * }
194 * because this may pass a non-null err to bar().
47ff5ac8
MA
195 *
196 * Likewise, do *not*
197 * Error *err = NULL;
198 * if (cond1) {
199 * error_setg(&err, ...);
200 * }
201 * if (cond2) {
202 * error_setg(&err, ...); // WRONG!
203 * }
204 * because this may pass a non-null err to error_setg().
ae7c80a7
VSO
205 *
206 * = Why, when and how to use ERRP_GUARD() =
207 *
208 * Without ERRP_GUARD(), use of the @errp parameter is restricted:
209 * - It must not be dereferenced, because it may be null.
210 * - It should not be passed to error_prepend() or
211 * error_append_hint(), because that doesn't work with &error_fatal.
212 * ERRP_GUARD() lifts these restrictions.
213 *
214 * To use ERRP_GUARD(), add it right at the beginning of the function.
215 * @errp can then be used without worrying about the argument being
216 * NULL or &error_fatal.
217 *
218 * Using it when it's not needed is safe, but please avoid cluttering
219 * the source with useless code.
220 *
221 * = Converting to ERRP_GUARD() =
222 *
223 * To convert a function to use ERRP_GUARD():
224 *
225 * 0. If the Error ** parameter is not named @errp, rename it to
226 * @errp.
227 *
228 * 1. Add an ERRP_GUARD() invocation, by convention right at the
229 * beginning of the function. This makes @errp safe to use.
230 *
231 * 2. Replace &err by errp, and err by *errp. Delete local variable
232 * @err.
233 *
234 * 3. Delete error_propagate(errp, *errp), replace
235 * error_propagate_prepend(errp, *errp, ...) by error_prepend(errp, ...)
236 *
237 * 4. Ensure @errp is valid at return: when you destroy *errp, set
d71a2432 238 * *errp = NULL.
ae7c80a7
VSO
239 *
240 * Example:
241 *
242 * bool fn(..., Error **errp)
243 * {
244 * Error *err = NULL;
245 *
246 * foo(arg, &err);
247 * if (err) {
248 * handle the error...
249 * error_propagate(errp, err);
250 * return false;
251 * }
252 * ...
253 * }
254 *
255 * becomes
256 *
257 * bool fn(..., Error **errp)
258 * {
259 * ERRP_GUARD();
260 *
261 * foo(arg, errp);
262 * if (*errp) {
263 * handle the error...
264 * return false;
265 * }
266 * ...
267 * }
8220f3ac
VSO
268 *
269 * For mass-conversion, use scripts/coccinelle/errp-guard.cocci.
edf6f3b3
MA
270 */
271
d5ec4f27
LC
272#ifndef ERROR_H
273#define ERROR_H
274
abb3d37d 275#include "qapi/qapi-types-error.h"
d5ec4f27 276
f22a28b8
EB
277/*
278 * Overall category of an error.
279 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
280 * enum names.
281 */
282typedef enum ErrorClass {
d20a580b
EB
283 ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
284 ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
d20a580b
EB
285 ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
286 ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
287 ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
f22a28b8
EB
288} ErrorClass;
289
edf6f3b3
MA
290/*
291 * Get @err's human-readable error message.
d5ec4f27 292 */
d59ce6f3 293const char *error_get_pretty(const Error *err);
d5ec4f27 294
edf6f3b3
MA
295/*
296 * Get @err's error class.
297 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
298 * strongly discouraged.
299 */
300ErrorClass error_get_class(const Error *err);
301
302/*
303 * Create a new error object and assign it to *@errp.
304 * If @errp is NULL, the error is ignored. Don't bother creating one
305 * then.
306 * If @errp is &error_abort, print a suitable message and abort().
a29a37b9 307 * If @errp is &error_fatal, print a suitable message and exit(1).
edf6f3b3
MA
308 * If @errp is anything else, *@errp must be NULL.
309 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
310 * human-readable error message is made from printf-style @fmt, ...
f4d0064a
MA
311 * The resulting message should be a single phrase, with no newline or
312 * trailing punctuation.
10303f04
MA
313 * Please don't error_setg(&error_fatal, ...), use error_report() and
314 * exit(), because that's more obvious.
315 * Likewise, don't error_setg(&error_abort, ...), use assert().
edf6f3b3 316 */
1e9b65bb
MA
317#define error_setg(errp, fmt, ...) \
318 error_setg_internal((errp), __FILE__, __LINE__, __func__, \
319 (fmt), ## __VA_ARGS__)
320void error_setg_internal(Error **errp,
321 const char *src, int line, const char *func,
322 const char *fmt, ...)
9edc6313 323 G_GNUC_PRINTF(5, 6);
edf6f3b3
MA
324
325/*
326 * Just like error_setg(), with @os_error info added to the message.
327 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
328 * the human-readable error message.
98cb89af
SS
329 *
330 * The value of errno (which usually can get clobbered by almost any
331 * function call) will be preserved.
680d16dc 332 */
1e9b65bb
MA
333#define error_setg_errno(errp, os_error, fmt, ...) \
334 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
335 (os_error), (fmt), ## __VA_ARGS__)
336void error_setg_errno_internal(Error **errp,
337 const char *fname, int line, const char *func,
338 int os_error, const char *fmt, ...)
9edc6313 339 G_GNUC_PRINTF(6, 7);
680d16dc 340
20840d4c 341#ifdef _WIN32
edf6f3b3
MA
342/*
343 * Just like error_setg(), with @win32_error info added to the message.
344 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
345 * is appended to the human-readable error message.
20840d4c 346 */
1e9b65bb
MA
347#define error_setg_win32(errp, win32_err, fmt, ...) \
348 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
349 (win32_err), (fmt), ## __VA_ARGS__)
350void error_setg_win32_internal(Error **errp,
351 const char *src, int line, const char *func,
352 int win32_err, const char *fmt, ...)
9edc6313 353 G_GNUC_PRINTF(6, 7);
20840d4c
TS
354#endif
355
edf6f3b3
MA
356/*
357 * Propagate error object (if any) from @local_err to @dst_errp.
358 * If @local_err is NULL, do nothing (because there's nothing to
359 * propagate).
360 * Else, if @dst_errp is NULL, errors are being ignored. Free the
361 * error object.
362 * Else, if @dst_errp is &error_abort, print a suitable message and
363 * abort().
a29a37b9
MA
364 * Else, if @dst_errp is &error_fatal, print a suitable message and
365 * exit(1).
edf6f3b3
MA
366 * Else, if @dst_errp already contains an error, ignore this one: free
367 * the error object.
368 * Else, move the error object from @local_err to *@dst_errp.
369 * On return, @local_err is invalid.
ae7c80a7 370 * Please use ERRP_GUARD() instead when possible.
10303f04
MA
371 * Please don't error_propagate(&error_fatal, ...), use
372 * error_report_err() and exit(), because that's more obvious.
75d789f8 373 */
edf6f3b3 374void error_propagate(Error **dst_errp, Error *local_err);
75d789f8 375
4b576648
MA
376
377/*
378 * Propagate error object (if any) with some text prepended.
379 * Behaves like
380 * error_prepend(&local_err, fmt, ...);
381 * error_propagate(dst_errp, local_err);
ae7c80a7 382 * Please use ERRP_GUARD() and error_prepend() instead when possible.
4b576648
MA
383 */
384void error_propagate_prepend(Error **dst_errp, Error *local_err,
192cf54a 385 const char *fmt, ...)
9edc6313 386 G_GNUC_PRINTF(3, 4);
4b576648 387
8277d2aa
MA
388/*
389 * Prepend some text to @errp's human-readable error message.
390 * The text is made by formatting @fmt, @ap like vprintf().
391 */
192cf54a 392void error_vprepend(Error *const *errp, const char *fmt, va_list ap)
9edc6313 393 G_GNUC_PRINTF(2, 0);
8277d2aa
MA
394
395/*
396 * Prepend some text to @errp's human-readable error message.
397 * The text is made by formatting @fmt, ... like printf().
398 */
49fbc723 399void error_prepend(Error *const *errp, const char *fmt, ...)
9edc6313 400 G_GNUC_PRINTF(2, 3);
8277d2aa
MA
401
402/*
50b7b000 403 * Append a printf-style human-readable explanation to an existing error.
508de478
MA
404 * If the error is later reported to a human user with
405 * error_report_err() or warn_report_err(), the hints will be shown,
406 * too. If it's reported via QMP, the hints will be ignored.
407 * Intended use is adding helpful hints on the human user interface,
408 * e.g. a list of valid values. It's not for clarifying a confusing
409 * error message.
f4d0064a
MA
410 * @errp may be NULL, but not &error_fatal or &error_abort.
411 * Trivially the case if you call it only after error_setg() or
412 * error_propagate().
413 * May be called multiple times. The resulting hint should end with a
414 * newline.
50b7b000 415 */
49fbc723 416void error_append_hint(Error *const *errp, const char *fmt, ...)
9edc6313 417 G_GNUC_PRINTF(2, 3);
50b7b000 418
edf6f3b3
MA
419/*
420 * Convenience function to report open() failure.
54028d75 421 */
1e9b65bb
MA
422#define error_setg_file_open(errp, os_errno, filename) \
423 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
424 (os_errno), (filename))
425void error_setg_file_open_internal(Error **errp,
426 const char *src, int line, const char *func,
427 int os_errno, const char *filename);
54028d75 428
ea25fbca 429/*
edf6f3b3 430 * Return an exact copy of @err.
79020cfc
LC
431 */
432Error *error_copy(const Error *err);
433
edf6f3b3
MA
434/*
435 * Free @err.
436 * @err may be NULL.
d5ec4f27 437 */
edf6f3b3 438void error_free(Error *err);
d5ec4f27 439
a12a5a1a
EB
440/*
441 * Convenience function to assert that *@errp is set, then silently free it.
442 */
443void error_free_or_abort(Error **errp);
444
e43ead1d
AF
445/*
446 * Convenience function to warn_report() and free @err.
508de478 447 * The report includes hints added with error_append_hint().
e43ead1d
AF
448 */
449void warn_report_err(Error *err);
450
edf6f3b3
MA
451/*
452 * Convenience function to error_report() and free @err.
508de478 453 * The report includes hints added with error_append_hint().
2ee2f1e4 454 */
f4d0064a 455void error_report_err(Error *err);
2ee2f1e4 456
e43ead1d
AF
457/*
458 * Convenience function to error_prepend(), warn_report() and free @err.
459 */
460void warn_reportf_err(Error *err, const char *fmt, ...)
9edc6313 461 G_GNUC_PRINTF(2, 3);
e43ead1d 462
8277d2aa
MA
463/*
464 * Convenience function to error_prepend(), error_report() and free @err.
465 */
466void error_reportf_err(Error *err, const char *fmt, ...)
9edc6313 467 G_GNUC_PRINTF(2, 3);
8277d2aa 468
edf6f3b3
MA
469/*
470 * Just like error_setg(), except you get to specify the error class.
471 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
472 * strongly discouraged.
d5ec4f27 473 */
1e9b65bb
MA
474#define error_set(errp, err_class, fmt, ...) \
475 error_set_internal((errp), __FILE__, __LINE__, __func__, \
476 (err_class), (fmt), ## __VA_ARGS__)
477void error_set_internal(Error **errp,
478 const char *src, int line, const char *func,
479 ErrorClass err_class, const char *fmt, ...)
9edc6313 480 G_GNUC_PRINTF(6, 7);
d5ec4f27 481
ae7c80a7
VSO
482/*
483 * Make @errp parameter easier to use regardless of argument value
484 *
485 * This macro is for use right at the beginning of a function that
486 * takes an Error **errp parameter to pass errors to its caller. The
487 * parameter must be named @errp.
488 *
489 * It must be used when the function dereferences @errp or passes
490 * @errp to error_prepend(), error_vprepend(), or error_append_hint().
491 * It is safe to use even when it's not needed, but please avoid
492 * cluttering the source with useless code.
493 *
494 * If @errp is NULL or &error_fatal, rewrite it to point to a local
495 * Error variable, which will be automatically propagated to the
496 * original @errp on function exit.
497 *
498 * Note: &error_abort is not rewritten, because that would move the
499 * abort from the place where the error is created to the place where
500 * it's propagated.
501 */
502#define ERRP_GUARD() \
503 g_auto(ErrorPropagator) _auto_errp_prop = {.errp = errp}; \
504 do { \
505 if (!errp || errp == &error_fatal) { \
506 errp = &_auto_errp_prop.local_err; \
507 } \
508 } while (0)
509
510typedef struct ErrorPropagator {
511 Error *local_err;
512 Error **errp;
513} ErrorPropagator;
514
515static inline void error_propagator_cleanup(ErrorPropagator *prop)
516{
517 error_propagate(prop->errp, prop->local_err);
518}
519
520G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ErrorPropagator, error_propagator_cleanup);
521
edf6f3b3 522/*
10303f04
MA
523 * Special error destination to abort on error.
524 * See error_setg() and error_propagate() for details.
5d24ee70 525 */
5d24ee70
PC
526extern Error *error_abort;
527
a29a37b9 528/*
10303f04
MA
529 * Special error destination to exit(1) on error.
530 * See error_setg() and error_propagate() for details.
a29a37b9
MA
531 */
532extern Error *error_fatal;
533
d5ec4f27 534#endif