blob: b6c89d141208cf9d7d683eeb6c309e960cd3722f [file] [log] [blame]
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -05001/*
2 * QEMU Error Objects
3 *
4 * Copyright IBM, Corp. 2011
Markus Armbrustera29a37b2015-09-11 16:51:42 +02005 * Copyright (C) 2011-2015 Red Hat, Inc.
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -05006 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
Markus Armbrustera29a37b2015-09-11 16:51:42 +02009 * Markus Armbruster <armbru@redhat.com>,
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050010 *
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 */
Stefan Weile4ea5e22011-06-13 23:01:53 +020014
Peter Maydellaafd7582016-01-29 17:49:55 +000015#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Cole Robinson073a3412014-03-21 19:42:28 -040017#include "qemu/error-report.h"
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050018
19struct Error
20{
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050021 char *msg;
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030022 ErrorClass err_class;
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020023 const char *src, *func;
24 int line;
Eric Blake50b7b002015-09-10 10:19:16 -060025 GString *hint;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050026};
27
Peter Crosthwaite5d24ee72014-01-01 18:46:59 -080028Error *error_abort;
Markus Armbrustera29a37b2015-09-11 16:51:42 +020029Error *error_fatal;
Peter Crosthwaite5d24ee72014-01-01 18:46:59 -080030
Markus Armbrustera29a37b2015-09-11 16:51:42 +020031static void error_handle_fatal(Error **errp, Error *err)
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020032{
Markus Armbrustera29a37b2015-09-11 16:51:42 +020033 if (errp == &error_abort) {
34 fprintf(stderr, "Unexpected error in %s() at %s:%d:\n",
35 err->func, err->src, err->line);
Vladimir Sementsov-Ogievskiyb922c052019-04-15 17:25:19 +030036 error_report("%s", error_get_pretty(err));
37 if (err->hint) {
38 error_printf("%s", err->hint->str);
39 }
Markus Armbrustera29a37b2015-09-11 16:51:42 +020040 abort();
41 }
42 if (errp == &error_fatal) {
43 error_report_err(err);
44 exit(1);
45 }
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020046}
47
48static void error_setv(Error **errp,
49 const char *src, int line, const char *func,
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +000050 ErrorClass err_class, const char *fmt, va_list ap,
51 const char *suffix)
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050052{
53 Error *err;
Max Reitzb276d242013-11-07 20:10:29 +010054 int saved_errno = errno;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050055
56 if (errp == NULL) {
57 return;
58 }
Paolo Bonzinid1953252012-07-17 16:17:04 +020059 assert(*errp == NULL);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050060
Anthony Liguori7267c092011-08-20 22:09:37 -050061 err = g_malloc0(sizeof(*err));
Luiz Capitulinodf1e6082012-07-27 17:51:03 -030062 err->msg = g_strdup_vprintf(fmt, ap);
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +000063 if (suffix) {
64 char *msg = err->msg;
65 err->msg = g_strdup_printf("%s: %s", msg, suffix);
66 g_free(msg);
67 }
Luiz Capitulino13f59ae2012-07-27 14:09:29 -030068 err->err_class = err_class;
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020069 err->src = src;
70 err->line = line;
71 err->func = func;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050072
Markus Armbrustera29a37b2015-09-11 16:51:42 +020073 error_handle_fatal(errp, err);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050074 *errp = err;
Max Reitzb276d242013-11-07 20:10:29 +010075
76 errno = saved_errno;
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -050077}
78
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020079void error_set_internal(Error **errp,
80 const char *src, int line, const char *func,
81 ErrorClass err_class, const char *fmt, ...)
Markus Armbruster55237502015-06-19 13:59:47 +020082{
83 va_list ap;
84
85 va_start(ap, fmt);
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +000086 error_setv(errp, src, line, func, err_class, fmt, ap, NULL);
Markus Armbruster55237502015-06-19 13:59:47 +020087 va_end(ap);
88}
89
Markus Armbruster1e9b65b2015-06-19 19:21:59 +020090void error_setg_internal(Error **errp,
91 const char *src, int line, const char *func,
92 const char *fmt, ...)
Markus Armbrustera9499dd2015-06-19 15:36:16 +020093{
94 va_list ap;
95
96 va_start(ap, fmt);
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +000097 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap, NULL);
Markus Armbrustera9499dd2015-06-19 15:36:16 +020098 va_end(ap);
99}
100
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200101void error_setg_errno_internal(Error **errp,
102 const char *src, int line, const char *func,
103 int os_errno, const char *fmt, ...)
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200104{
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200105 va_list ap;
Max Reitzb276d242013-11-07 20:10:29 +0100106 int saved_errno = errno;
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200107
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200108 va_start(ap, fmt);
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +0000109 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap,
110 os_errno != 0 ? strerror(os_errno) : NULL);
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200111 va_end(ap);
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200112
Max Reitzb276d242013-11-07 20:10:29 +0100113 errno = saved_errno;
Paolo Bonzini680d16d2012-10-02 09:00:45 +0200114}
115
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200116void error_setg_file_open_internal(Error **errp,
117 const char *src, int line, const char *func,
118 int os_errno, const char *filename)
Luiz Capitulino54028d72013-06-07 14:24:49 -0400119{
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200120 error_setg_errno_internal(errp, src, line, func, os_errno,
121 "Could not open '%s'", filename);
Luiz Capitulino54028d72013-06-07 14:24:49 -0400122}
123
Vladimir Sementsov-Ogievskiy49fbc722019-12-05 20:46:17 +0300124void error_vprepend(Error *const *errp, const char *fmt, va_list ap)
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100125{
126 GString *newmsg;
127
128 if (!errp) {
129 return;
130 }
131
132 newmsg = g_string_new(NULL);
133 g_string_vprintf(newmsg, fmt, ap);
134 g_string_append(newmsg, (*errp)->msg);
Max Reitz536eeea2017-04-13 18:09:52 +0200135 g_free((*errp)->msg);
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100136 (*errp)->msg = g_string_free(newmsg, 0);
137}
138
Vladimir Sementsov-Ogievskiy49fbc722019-12-05 20:46:17 +0300139void error_prepend(Error *const *errp, const char *fmt, ...)
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100140{
141 va_list ap;
142
143 va_start(ap, fmt);
144 error_vprepend(errp, fmt, ap);
145 va_end(ap);
146}
147
Vladimir Sementsov-Ogievskiy49fbc722019-12-05 20:46:17 +0300148void error_append_hint(Error *const *errp, const char *fmt, ...)
Eric Blake50b7b002015-09-10 10:19:16 -0600149{
150 va_list ap;
151 int saved_errno = errno;
152 Error *err;
153
154 if (!errp) {
155 return;
156 }
157 err = *errp;
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100158 assert(err && errp != &error_abort && errp != &error_fatal);
Eric Blake50b7b002015-09-10 10:19:16 -0600159
160 if (!err->hint) {
161 err->hint = g_string_new(NULL);
162 }
163 va_start(ap, fmt);
164 g_string_append_vprintf(err->hint, fmt, ap);
165 va_end(ap);
166
167 errno = saved_errno;
168}
169
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400170#ifdef _WIN32
171
Markus Armbruster1e9b65b2015-06-19 19:21:59 +0200172void error_setg_win32_internal(Error **errp,
173 const char *src, int line, const char *func,
174 int win32_err, const char *fmt, ...)
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400175{
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400176 va_list ap;
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +0000177 char *suffix = NULL;
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400178
179 if (errp == NULL) {
180 return;
181 }
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400182
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +0000183 if (win32_err != 0) {
184 suffix = g_win32_error_message(win32_err);
185 }
186
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400187 va_start(ap, fmt);
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +0000188 error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR,
189 fmt, ap, suffix);
Markus Armbruster55237502015-06-19 13:59:47 +0200190 va_end(ap);
191
Daniel P. Berrange20e2dec2016-03-09 17:28:24 +0000192 g_free(suffix);
Tomoki Sekiyama20840d42013-08-07 11:40:11 -0400193}
194
195#endif
196
Luiz Capitulino79020cf2011-12-05 16:04:05 -0200197Error *error_copy(const Error *err)
198{
199 Error *err_new;
200
201 err_new = g_malloc0(sizeof(*err));
202 err_new->msg = g_strdup(err->msg);
Luiz Capitulino13f59ae2012-07-27 14:09:29 -0300203 err_new->err_class = err->err_class;
Eric Blake88e2ce22015-09-10 10:34:50 -0600204 err_new->src = err->src;
205 err_new->line = err->line;
206 err_new->func = err->func;
Eric Blake50b7b002015-09-10 10:19:16 -0600207 if (err->hint) {
208 err_new->hint = g_string_new(err->hint->str);
209 }
Luiz Capitulino79020cf2011-12-05 16:04:05 -0200210
211 return err_new;
212}
213
Luiz Capitulinoea25fbc2012-08-01 16:29:38 -0300214ErrorClass error_get_class(const Error *err)
215{
216 return err->err_class;
217}
218
Daniel P. Berranged59ce6f2016-04-27 11:05:00 +0100219const char *error_get_pretty(const Error *err)
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500220{
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500221 return err->msg;
222}
223
Markus Armbruster2ee2f1e2015-02-06 15:27:19 +0100224void error_report_err(Error *err)
225{
226 error_report("%s", error_get_pretty(err));
Eric Blake50b7b002015-09-10 10:19:16 -0600227 if (err->hint) {
Markus Armbruster4c8b7c82019-04-16 17:38:50 +0200228 error_printf("%s", err->hint->str);
Eric Blake50b7b002015-09-10 10:19:16 -0600229 }
Markus Armbruster2ee2f1e2015-02-06 15:27:19 +0100230 error_free(err);
231}
232
Alistair Francise43ead12017-07-12 06:57:52 -0700233void warn_report_err(Error *err)
234{
235 warn_report("%s", error_get_pretty(err));
236 if (err->hint) {
Markus Armbruster4c8b7c82019-04-16 17:38:50 +0200237 error_printf("%s", err->hint->str);
Alistair Francise43ead12017-07-12 06:57:52 -0700238 }
239 error_free(err);
240}
241
Markus Armbruster8277d2a2015-12-18 16:35:12 +0100242void error_reportf_err(Error *err, const char *fmt, ...)
243{
244 va_list ap;
245
246 va_start(ap, fmt);
247 error_vprepend(&err, fmt, ap);
248 va_end(ap);
249 error_report_err(err);
250}
251
Alistair Francise43ead12017-07-12 06:57:52 -0700252
253void warn_reportf_err(Error *err, const char *fmt, ...)
254{
255 va_list ap;
256
257 va_start(ap, fmt);
258 error_vprepend(&err, fmt, ap);
259 va_end(ap);
260 warn_report_err(err);
261}
262
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500263void error_free(Error *err)
264{
265 if (err) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500266 g_free(err->msg);
Eric Blake50b7b002015-09-10 10:19:16 -0600267 if (err->hint) {
268 g_string_free(err->hint, true);
269 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500270 g_free(err);
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500271 }
272}
273
Eric Blakea12a5a12015-11-05 23:35:31 -0700274void error_free_or_abort(Error **errp)
275{
276 assert(errp && *errp);
277 error_free(*errp);
278 *errp = NULL;
279}
280
Markus Armbruster64dfefe2014-05-02 13:26:32 +0200281void error_propagate(Error **dst_errp, Error *local_err)
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500282{
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200283 if (!local_err) {
284 return;
285 }
286 error_handle_fatal(dst_errp, local_err);
287 if (dst_errp && !*dst_errp) {
Markus Armbruster64dfefe2014-05-02 13:26:32 +0200288 *dst_errp = local_err;
Markus Armbrustera29a37b2015-09-11 16:51:42 +0200289 } else {
Luiz Capitulinod5ec4f22011-06-01 12:14:49 -0500290 error_free(local_err);
291 }
292}
Markus Armbruster4b576642018-10-17 10:26:25 +0200293
294void error_propagate_prepend(Error **dst_errp, Error *err,
295 const char *fmt, ...)
296{
297 va_list ap;
298
299 if (dst_errp && !*dst_errp) {
300 va_start(ap, fmt);
301 error_vprepend(&err, fmt, ap);
302 va_end(ap);
303 } /* else error is being ignored, don't bother with prepending */
304 error_propagate(dst_errp, err);
305}