qemu with hax to log dma reads & writes
jcs.org/2018/11/12/vfio
1/*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
5 * Copyright (C) 2011-2015 Red Hat, Inc.
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
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 */
14
15/*
16 * Error reporting system loosely patterned after Glib's GError.
17 *
18 * Create an error:
19 * error_setg(&err, "situation normal, all fouled up");
20 *
21 * Create an error and add additional explanation:
22 * error_setg(&err, "invalid quark");
23 * error_append_hint(&err, "Valid quarks are up, down, strange, "
24 * "charm, top, bottom.\n");
25 *
26 * Do *not* contract this to
27 * error_setg(&err, "invalid quark\n"
28 * "Valid quarks are up, down, strange, charm, top, bottom.");
29 *
30 * Report an error to the current monitor if we have one, else stderr:
31 * error_report_err(err);
32 * This frees the error object.
33 *
34 * Likewise, but with additional text prepended:
35 * error_reportf_err(err, "Could not frobnicate '%s': ", name);
36 *
37 * Report an error somewhere else:
38 * const char *msg = error_get_pretty(err);
39 * do with msg what needs to be done...
40 * error_free(err);
41 * Note that this loses hints added with error_append_hint().
42 *
43 * Handle an error without reporting it (just for completeness):
44 * error_free(err);
45 *
46 * Assert that an expected error occurred, but clean it up without
47 * reporting it (primarily useful in testsuites):
48 * error_free_or_abort(&err);
49 *
50 * Pass an existing error to the caller:
51 * error_propagate(errp, err);
52 * where Error **errp is a parameter, by convention the last one.
53 *
54 * Pass an existing error to the caller with the message modified:
55 * error_propagate(errp, err);
56 * error_prepend(errp, "Could not frobnicate '%s': ", name);
57 *
58 * Create a new error and pass it to the caller:
59 * error_setg(errp, "situation normal, all fouled up");
60 *
61 * Call a function and receive an error from it:
62 * Error *err = NULL;
63 * foo(arg, &err);
64 * if (err) {
65 * handle the error...
66 * }
67 *
68 * Call a function ignoring errors:
69 * foo(arg, NULL);
70 *
71 * Call a function aborting on errors:
72 * foo(arg, &error_abort);
73 *
74 * Call a function treating errors as fatal:
75 * foo(arg, &error_fatal);
76 *
77 * Receive an error and pass it on to the caller:
78 * Error *err = NULL;
79 * foo(arg, &err);
80 * if (err) {
81 * handle the error...
82 * error_propagate(errp, err);
83 * }
84 * where Error **errp is a parameter, by convention the last one.
85 *
86 * Do *not* "optimize" this to
87 * foo(arg, errp);
88 * if (*errp) { // WRONG!
89 * handle the error...
90 * }
91 * because errp may be NULL!
92 *
93 * But when all you do with the error is pass it on, please use
94 * foo(arg, errp);
95 * for readability.
96 *
97 * Receive and accumulate multiple errors (first one wins):
98 * Error *err = NULL, *local_err = NULL;
99 * foo(arg, &err);
100 * bar(arg, &local_err);
101 * error_propagate(&err, local_err);
102 * if (err) {
103 * handle the error...
104 * }
105 *
106 * Do *not* "optimize" this to
107 * foo(arg, &err);
108 * bar(arg, &err); // WRONG!
109 * if (err) {
110 * handle the error...
111 * }
112 * because this may pass a non-null err to bar().
113 */
114
115#ifndef ERROR_H
116#define ERROR_H
117
118#include "qapi/qapi-types-common.h"
119
120/*
121 * Overall category of an error.
122 * Based on the qapi type QapiErrorClass, but reproduced here for nicer
123 * enum names.
124 */
125typedef enum ErrorClass {
126 ERROR_CLASS_GENERIC_ERROR = QAPI_ERROR_CLASS_GENERICERROR,
127 ERROR_CLASS_COMMAND_NOT_FOUND = QAPI_ERROR_CLASS_COMMANDNOTFOUND,
128 ERROR_CLASS_DEVICE_NOT_ACTIVE = QAPI_ERROR_CLASS_DEVICENOTACTIVE,
129 ERROR_CLASS_DEVICE_NOT_FOUND = QAPI_ERROR_CLASS_DEVICENOTFOUND,
130 ERROR_CLASS_KVM_MISSING_CAP = QAPI_ERROR_CLASS_KVMMISSINGCAP,
131} ErrorClass;
132
133/*
134 * Get @err's human-readable error message.
135 */
136const char *error_get_pretty(const Error *err);
137
138/*
139 * Get @err's error class.
140 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
141 * strongly discouraged.
142 */
143ErrorClass error_get_class(const Error *err);
144
145/*
146 * Create a new error object and assign it to *@errp.
147 * If @errp is NULL, the error is ignored. Don't bother creating one
148 * then.
149 * If @errp is &error_abort, print a suitable message and abort().
150 * If @errp is &error_fatal, print a suitable message and exit(1).
151 * If @errp is anything else, *@errp must be NULL.
152 * The new error's class is ERROR_CLASS_GENERIC_ERROR, and its
153 * human-readable error message is made from printf-style @fmt, ...
154 * The resulting message should be a single phrase, with no newline or
155 * trailing punctuation.
156 * Please don't error_setg(&error_fatal, ...), use error_report() and
157 * exit(), because that's more obvious.
158 * Likewise, don't error_setg(&error_abort, ...), use assert().
159 */
160#define error_setg(errp, fmt, ...) \
161 error_setg_internal((errp), __FILE__, __LINE__, __func__, \
162 (fmt), ## __VA_ARGS__)
163void error_setg_internal(Error **errp,
164 const char *src, int line, const char *func,
165 const char *fmt, ...)
166 GCC_FMT_ATTR(5, 6);
167
168/*
169 * Just like error_setg(), with @os_error info added to the message.
170 * If @os_error is non-zero, ": " + strerror(os_error) is appended to
171 * the human-readable error message.
172 *
173 * The value of errno (which usually can get clobbered by almost any
174 * function call) will be preserved.
175 */
176#define error_setg_errno(errp, os_error, fmt, ...) \
177 error_setg_errno_internal((errp), __FILE__, __LINE__, __func__, \
178 (os_error), (fmt), ## __VA_ARGS__)
179void error_setg_errno_internal(Error **errp,
180 const char *fname, int line, const char *func,
181 int os_error, const char *fmt, ...)
182 GCC_FMT_ATTR(6, 7);
183
184#ifdef _WIN32
185/*
186 * Just like error_setg(), with @win32_error info added to the message.
187 * If @win32_error is non-zero, ": " + g_win32_error_message(win32_err)
188 * is appended to the human-readable error message.
189 */
190#define error_setg_win32(errp, win32_err, fmt, ...) \
191 error_setg_win32_internal((errp), __FILE__, __LINE__, __func__, \
192 (win32_err), (fmt), ## __VA_ARGS__)
193void error_setg_win32_internal(Error **errp,
194 const char *src, int line, const char *func,
195 int win32_err, const char *fmt, ...)
196 GCC_FMT_ATTR(6, 7);
197#endif
198
199/*
200 * Propagate error object (if any) from @local_err to @dst_errp.
201 * If @local_err is NULL, do nothing (because there's nothing to
202 * propagate).
203 * Else, if @dst_errp is NULL, errors are being ignored. Free the
204 * error object.
205 * Else, if @dst_errp is &error_abort, print a suitable message and
206 * abort().
207 * Else, if @dst_errp is &error_fatal, print a suitable message and
208 * exit(1).
209 * Else, if @dst_errp already contains an error, ignore this one: free
210 * the error object.
211 * Else, move the error object from @local_err to *@dst_errp.
212 * On return, @local_err is invalid.
213 * Please don't error_propagate(&error_fatal, ...), use
214 * error_report_err() and exit(), because that's more obvious.
215 */
216void error_propagate(Error **dst_errp, Error *local_err);
217
218/*
219 * Prepend some text to @errp's human-readable error message.
220 * The text is made by formatting @fmt, @ap like vprintf().
221 */
222void error_vprepend(Error **errp, const char *fmt, va_list ap);
223
224/*
225 * Prepend some text to @errp's human-readable error message.
226 * The text is made by formatting @fmt, ... like printf().
227 */
228void error_prepend(Error **errp, const char *fmt, ...)
229 GCC_FMT_ATTR(2, 3);
230
231/*
232 * Append a printf-style human-readable explanation to an existing error.
233 * If the error is later reported to a human user with
234 * error_report_err() or warn_report_err(), the hints will be shown,
235 * too. If it's reported via QMP, the hints will be ignored.
236 * Intended use is adding helpful hints on the human user interface,
237 * e.g. a list of valid values. It's not for clarifying a confusing
238 * error message.
239 * @errp may be NULL, but not &error_fatal or &error_abort.
240 * Trivially the case if you call it only after error_setg() or
241 * error_propagate().
242 * May be called multiple times. The resulting hint should end with a
243 * newline.
244 */
245void error_append_hint(Error **errp, const char *fmt, ...)
246 GCC_FMT_ATTR(2, 3);
247
248/*
249 * Convenience function to report open() failure.
250 */
251#define error_setg_file_open(errp, os_errno, filename) \
252 error_setg_file_open_internal((errp), __FILE__, __LINE__, __func__, \
253 (os_errno), (filename))
254void error_setg_file_open_internal(Error **errp,
255 const char *src, int line, const char *func,
256 int os_errno, const char *filename);
257
258/*
259 * Return an exact copy of @err.
260 */
261Error *error_copy(const Error *err);
262
263/*
264 * Free @err.
265 * @err may be NULL.
266 */
267void error_free(Error *err);
268
269/*
270 * Convenience function to assert that *@errp is set, then silently free it.
271 */
272void error_free_or_abort(Error **errp);
273
274/*
275 * Convenience function to warn_report() and free @err.
276 * The report includes hints added with error_append_hint().
277 */
278void warn_report_err(Error *err);
279
280/*
281 * Convenience function to error_report() and free @err.
282 * The report includes hints added with error_append_hint().
283 */
284void error_report_err(Error *err);
285
286/*
287 * Convenience function to error_prepend(), warn_report() and free @err.
288 */
289void warn_reportf_err(Error *err, const char *fmt, ...)
290 GCC_FMT_ATTR(2, 3);
291
292/*
293 * Convenience function to error_prepend(), error_report() and free @err.
294 */
295void error_reportf_err(Error *err, const char *fmt, ...)
296 GCC_FMT_ATTR(2, 3);
297
298/*
299 * Just like error_setg(), except you get to specify the error class.
300 * Note: use of error classes other than ERROR_CLASS_GENERIC_ERROR is
301 * strongly discouraged.
302 */
303#define error_set(errp, err_class, fmt, ...) \
304 error_set_internal((errp), __FILE__, __LINE__, __func__, \
305 (err_class), (fmt), ## __VA_ARGS__)
306void error_set_internal(Error **errp,
307 const char *src, int line, const char *func,
308 ErrorClass err_class, const char *fmt, ...)
309 GCC_FMT_ATTR(6, 7);
310
311/*
312 * Special error destination to abort on error.
313 * See error_setg() and error_propagate() for details.
314 */
315extern Error *error_abort;
316
317/*
318 * Special error destination to exit(1) on error.
319 * See error_setg() and error_propagate() for details.
320 */
321extern Error *error_fatal;
322
323#endif