4 * Copyright IBM, Corp. 2011
5 * Copyright (C) 2011-2015 Red Hat, Inc.
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>,
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.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
23 const char *src
, *func
;
31 static void error_handle_fatal(Error
**errp
, Error
*err
)
33 if (errp
== &error_abort
) {
34 fprintf(stderr
, "Unexpected error in %s() at %s:%d:\n",
35 err
->func
, err
->src
, err
->line
);
36 error_report("%s", error_get_pretty(err
));
38 error_printf("%s", err
->hint
->str
);
42 if (errp
== &error_fatal
) {
43 error_report_err(err
);
48 static void error_setv(Error
**errp
,
49 const char *src
, int line
, const char *func
,
50 ErrorClass err_class
, const char *fmt
, va_list ap
,
54 int saved_errno
= errno
;
59 assert(*errp
== NULL
);
61 err
= g_malloc0(sizeof(*err
));
62 err
->msg
= g_strdup_vprintf(fmt
, ap
);
65 err
->msg
= g_strdup_printf("%s: %s", msg
, suffix
);
68 err
->err_class
= err_class
;
73 error_handle_fatal(errp
, err
);
79 void error_set_internal(Error
**errp
,
80 const char *src
, int line
, const char *func
,
81 ErrorClass err_class
, const char *fmt
, ...)
86 error_setv(errp
, src
, line
, func
, err_class
, fmt
, ap
, NULL
);
90 void error_setg_internal(Error
**errp
,
91 const char *src
, int line
, const char *func
,
97 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
, NULL
);
101 void error_setg_errno_internal(Error
**errp
,
102 const char *src
, int line
, const char *func
,
103 int os_errno
, const char *fmt
, ...)
106 int saved_errno
= errno
;
109 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
,
110 os_errno
!= 0 ? strerror(os_errno
) : NULL
);
116 void error_setg_file_open_internal(Error
**errp
,
117 const char *src
, int line
, const char *func
,
118 int os_errno
, const char *filename
)
120 error_setg_errno_internal(errp
, src
, line
, func
, os_errno
,
121 "Could not open '%s'", filename
);
124 void error_vprepend(Error
*const *errp
, const char *fmt
, va_list ap
)
132 newmsg
= g_string_new(NULL
);
133 g_string_vprintf(newmsg
, fmt
, ap
);
134 g_string_append(newmsg
, (*errp
)->msg
);
135 g_free((*errp
)->msg
);
136 (*errp
)->msg
= g_string_free(newmsg
, 0);
139 void error_prepend(Error
*const *errp
, const char *fmt
, ...)
144 error_vprepend(errp
, fmt
, ap
);
148 void error_append_hint(Error
*const *errp
, const char *fmt
, ...)
151 int saved_errno
= errno
;
158 assert(err
&& errp
!= &error_abort
&& errp
!= &error_fatal
);
161 err
->hint
= g_string_new(NULL
);
164 g_string_append_vprintf(err
->hint
, fmt
, ap
);
172 void error_setg_win32_internal(Error
**errp
,
173 const char *src
, int line
, const char *func
,
174 int win32_err
, const char *fmt
, ...)
183 if (win32_err
!= 0) {
184 suffix
= g_win32_error_message(win32_err
);
188 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
,
197 Error
*error_copy(const Error
*err
)
201 err_new
= g_malloc0(sizeof(*err
));
202 err_new
->msg
= g_strdup(err
->msg
);
203 err_new
->err_class
= err
->err_class
;
204 err_new
->src
= err
->src
;
205 err_new
->line
= err
->line
;
206 err_new
->func
= err
->func
;
208 err_new
->hint
= g_string_new(err
->hint
->str
);
214 ErrorClass
error_get_class(const Error
*err
)
216 return err
->err_class
;
219 const char *error_get_pretty(const Error
*err
)
224 void error_report_err(Error
*err
)
226 error_report("%s", error_get_pretty(err
));
228 error_printf("%s", err
->hint
->str
);
233 void warn_report_err(Error
*err
)
235 warn_report("%s", error_get_pretty(err
));
237 error_printf("%s", err
->hint
->str
);
242 void error_reportf_err(Error
*err
, const char *fmt
, ...)
247 error_vprepend(&err
, fmt
, ap
);
249 error_report_err(err
);
253 void warn_reportf_err(Error
*err
, const char *fmt
, ...)
258 error_vprepend(&err
, fmt
, ap
);
260 warn_report_err(err
);
263 void error_free(Error
*err
)
268 g_string_free(err
->hint
, true);
274 void error_free_or_abort(Error
**errp
)
276 assert(errp
&& *errp
);
281 void error_propagate(Error
**dst_errp
, Error
*local_err
)
286 error_handle_fatal(dst_errp
, local_err
);
287 if (dst_errp
&& !*dst_errp
) {
288 *dst_errp
= local_err
;
290 error_free(local_err
);
294 void error_propagate_prepend(Error
**dst_errp
, Error
*err
,
295 const char *fmt
, ...)
299 if (dst_errp
&& !*dst_errp
) {
301 error_vprepend(&err
, fmt
, ap
);
303 } /* else error is being ignored, don't bother with prepending */
304 error_propagate(dst_errp
, err
);