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
;
32 static void error_handle(Error
**errp
, Error
*err
)
34 if (errp
== &error_abort
) {
35 fprintf(stderr
, "Unexpected error in %s() at %s:%d:\n",
36 err
->func
, err
->src
, err
->line
);
37 error_report("%s", error_get_pretty(err
));
39 error_printf("%s", err
->hint
->str
);
43 if (errp
== &error_fatal
) {
44 error_report_err(err
);
47 if (errp
== &error_warn
) {
49 } else if (errp
&& !*errp
) {
57 static void error_setv(Error
**errp
,
58 const char *src
, int line
, const char *func
,
59 ErrorClass err_class
, const char *fmt
, va_list ap
,
63 int saved_errno
= errno
;
68 assert(*errp
== NULL
);
70 err
= g_malloc0(sizeof(*err
));
71 err
->msg
= g_strdup_vprintf(fmt
, ap
);
74 err
->msg
= g_strdup_printf("%s: %s", msg
, suffix
);
77 err
->err_class
= err_class
;
82 error_handle(errp
, err
);
87 void error_set_internal(Error
**errp
,
88 const char *src
, int line
, const char *func
,
89 ErrorClass err_class
, const char *fmt
, ...)
94 error_setv(errp
, src
, line
, func
, err_class
, fmt
, ap
, NULL
);
98 void error_setg_internal(Error
**errp
,
99 const char *src
, int line
, const char *func
,
100 const char *fmt
, ...)
105 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
, NULL
);
109 void error_setg_errno_internal(Error
**errp
,
110 const char *src
, int line
, const char *func
,
111 int os_errno
, const char *fmt
, ...)
114 int saved_errno
= errno
;
117 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
,
118 os_errno
!= 0 ? strerror(os_errno
) : NULL
);
124 void error_setg_file_open_internal(Error
**errp
,
125 const char *src
, int line
, const char *func
,
126 int os_errno
, const char *filename
)
128 error_setg_errno_internal(errp
, src
, line
, func
, os_errno
,
129 "Could not open '%s'", filename
);
132 void error_vprepend(Error
*const *errp
, const char *fmt
, va_list ap
)
140 newmsg
= g_string_new(NULL
);
141 g_string_vprintf(newmsg
, fmt
, ap
);
142 g_string_append(newmsg
, (*errp
)->msg
);
143 g_free((*errp
)->msg
);
144 (*errp
)->msg
= g_string_free(newmsg
, 0);
147 void error_prepend(Error
*const *errp
, const char *fmt
, ...)
152 error_vprepend(errp
, fmt
, ap
);
156 void error_append_hint(Error
*const *errp
, const char *fmt
, ...)
159 int saved_errno
= errno
;
166 assert(err
&& errp
!= &error_abort
&& errp
!= &error_fatal
);
169 err
->hint
= g_string_new(NULL
);
172 g_string_append_vprintf(err
->hint
, fmt
, ap
);
180 void error_setg_win32_internal(Error
**errp
,
181 const char *src
, int line
, const char *func
,
182 int win32_err
, const char *fmt
, ...)
191 if (win32_err
!= 0) {
192 suffix
= g_win32_error_message(win32_err
);
196 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
,
205 Error
*error_copy(const Error
*err
)
209 err_new
= g_malloc0(sizeof(*err
));
210 err_new
->msg
= g_strdup(err
->msg
);
211 err_new
->err_class
= err
->err_class
;
212 err_new
->src
= err
->src
;
213 err_new
->line
= err
->line
;
214 err_new
->func
= err
->func
;
216 err_new
->hint
= g_string_new(err
->hint
->str
);
222 ErrorClass
error_get_class(const Error
*err
)
224 return err
->err_class
;
227 const char *error_get_pretty(const Error
*err
)
232 void error_report_err(Error
*err
)
234 error_report("%s", error_get_pretty(err
));
236 error_printf("%s", err
->hint
->str
);
241 void warn_report_err(Error
*err
)
243 warn_report("%s", error_get_pretty(err
));
245 error_printf("%s", err
->hint
->str
);
250 void error_reportf_err(Error
*err
, const char *fmt
, ...)
255 error_vprepend(&err
, fmt
, ap
);
257 error_report_err(err
);
261 void warn_reportf_err(Error
*err
, const char *fmt
, ...)
266 error_vprepend(&err
, fmt
, ap
);
268 warn_report_err(err
);
271 void error_free(Error
*err
)
276 g_string_free(err
->hint
, true);
282 void error_free_or_abort(Error
**errp
)
284 assert(errp
&& *errp
);
289 void error_propagate(Error
**dst_errp
, Error
*local_err
)
294 error_handle(dst_errp
, local_err
);
297 void error_propagate_prepend(Error
**dst_errp
, Error
*err
,
298 const char *fmt
, ...)
302 if (dst_errp
&& !*dst_errp
) {
304 error_vprepend(&err
, fmt
, ap
);
306 } /* else error is being ignored, don't bother with prepending */
307 error_propagate(dst_errp
, err
);