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-common.h"
18 #include "qemu/error-report.h"
24 const char *src
, *func
;
32 static void error_handle_fatal(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_err(err
);
40 if (errp
== &error_fatal
) {
41 error_report_err(err
);
46 static void error_setv(Error
**errp
,
47 const char *src
, int line
, const char *func
,
48 ErrorClass err_class
, const char *fmt
, va_list ap
,
52 int saved_errno
= errno
;
57 assert(*errp
== NULL
);
59 err
= g_malloc0(sizeof(*err
));
60 err
->msg
= g_strdup_vprintf(fmt
, ap
);
63 err
->msg
= g_strdup_printf("%s: %s", msg
, suffix
);
66 err
->err_class
= err_class
;
71 error_handle_fatal(errp
, err
);
77 void error_set_internal(Error
**errp
,
78 const char *src
, int line
, const char *func
,
79 ErrorClass err_class
, const char *fmt
, ...)
84 error_setv(errp
, src
, line
, func
, err_class
, fmt
, ap
, NULL
);
88 void error_setg_internal(Error
**errp
,
89 const char *src
, int line
, const char *func
,
95 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
, NULL
);
99 void error_setg_errno_internal(Error
**errp
,
100 const char *src
, int line
, const char *func
,
101 int os_errno
, const char *fmt
, ...)
104 int saved_errno
= errno
;
111 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
, fmt
, ap
,
112 os_errno
!= 0 ? strerror(os_errno
) : NULL
);
118 void error_setg_file_open_internal(Error
**errp
,
119 const char *src
, int line
, const char *func
,
120 int os_errno
, const char *filename
)
122 error_setg_errno_internal(errp
, src
, line
, func
, os_errno
,
123 "Could not open '%s'", filename
);
126 void error_vprepend(Error
**errp
, const char *fmt
, va_list ap
)
134 newmsg
= g_string_new(NULL
);
135 g_string_vprintf(newmsg
, fmt
, ap
);
136 g_string_append(newmsg
, (*errp
)->msg
);
137 (*errp
)->msg
= g_string_free(newmsg
, 0);
140 void error_prepend(Error
**errp
, const char *fmt
, ...)
145 error_vprepend(errp
, fmt
, ap
);
149 void error_append_hint(Error
**errp
, const char *fmt
, ...)
152 int saved_errno
= errno
;
159 assert(err
&& errp
!= &error_abort
&& errp
!= &error_fatal
);
162 err
->hint
= g_string_new(NULL
);
165 g_string_append_vprintf(err
->hint
, fmt
, ap
);
173 void error_setg_win32_internal(Error
**errp
,
174 const char *src
, int line
, const char *func
,
175 int win32_err
, const char *fmt
, ...)
184 if (win32_err
!= 0) {
185 suffix
= g_win32_error_message(win32_err
);
189 error_setv(errp
, src
, line
, func
, ERROR_CLASS_GENERIC_ERROR
,
198 Error
*error_copy(const Error
*err
)
202 err_new
= g_malloc0(sizeof(*err
));
203 err_new
->msg
= g_strdup(err
->msg
);
204 err_new
->err_class
= err
->err_class
;
205 err_new
->src
= err
->src
;
206 err_new
->line
= err
->line
;
207 err_new
->func
= err
->func
;
209 err_new
->hint
= g_string_new(err
->hint
->str
);
215 ErrorClass
error_get_class(const Error
*err
)
217 return err
->err_class
;
220 const char *error_get_pretty(Error
*err
)
225 void error_report_err(Error
*err
)
227 error_report("%s", error_get_pretty(err
));
229 error_printf_unless_qmp("%s", err
->hint
->str
);
234 void error_reportf_err(Error
*err
, const char *fmt
, ...)
239 error_vprepend(&err
, fmt
, ap
);
241 error_report_err(err
);
244 void error_free(Error
*err
)
249 g_string_free(err
->hint
, true);
255 void error_free_or_abort(Error
**errp
)
257 assert(errp
&& *errp
);
262 void error_propagate(Error
**dst_errp
, Error
*local_err
)
267 error_handle_fatal(dst_errp
, local_err
);
268 if (dst_errp
&& !*dst_errp
) {
269 *dst_errp
= local_err
;
271 error_free(local_err
);