Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Error Objects |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 5 | * Copyright (C) 2011-2015 Red Hat, Inc. |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 9 | * Markus Armbruster <armbru@redhat.com>, |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 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 | */ |
Stefan Weil | e4ea5e2 | 2011-06-13 23:01:53 +0200 | [diff] [blame] | 14 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 16 | #include "qapi/error.h" |
Cole Robinson | 073a341 | 2014-03-21 19:42:28 -0400 | [diff] [blame] | 17 | #include "qemu/error-report.h" |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 18 | |
| 19 | struct Error |
| 20 | { |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 21 | char *msg; |
Luiz Capitulino | 13f59ae | 2012-07-27 14:09:29 -0300 | [diff] [blame] | 22 | ErrorClass err_class; |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 23 | const char *src, *func; |
| 24 | int line; |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 25 | GString *hint; |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 26 | }; |
| 27 | |
Peter Crosthwaite | 5d24ee7 | 2014-01-01 18:46:59 -0800 | [diff] [blame] | 28 | Error *error_abort; |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 29 | Error *error_fatal; |
Peter Crosthwaite | 5d24ee7 | 2014-01-01 18:46:59 -0800 | [diff] [blame] | 30 | |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 31 | static void error_handle_fatal(Error **errp, Error *err) |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 32 | { |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 33 | if (errp == &error_abort) { |
| 34 | fprintf(stderr, "Unexpected error in %s() at %s:%d:\n", |
| 35 | err->func, err->src, err->line); |
Vladimir Sementsov-Ogievskiy | b922c05 | 2019-04-15 17:25:19 +0300 | [diff] [blame] | 36 | error_report("%s", error_get_pretty(err)); |
| 37 | if (err->hint) { |
| 38 | error_printf("%s", err->hint->str); |
| 39 | } |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 40 | abort(); |
| 41 | } |
| 42 | if (errp == &error_fatal) { |
| 43 | error_report_err(err); |
| 44 | exit(1); |
| 45 | } |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void error_setv(Error **errp, |
| 49 | const char *src, int line, const char *func, |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 50 | ErrorClass err_class, const char *fmt, va_list ap, |
| 51 | const char *suffix) |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 52 | { |
| 53 | Error *err; |
Max Reitz | b276d24 | 2013-11-07 20:10:29 +0100 | [diff] [blame] | 54 | int saved_errno = errno; |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 55 | |
| 56 | if (errp == NULL) { |
| 57 | return; |
| 58 | } |
Paolo Bonzini | d195325 | 2012-07-17 16:17:04 +0200 | [diff] [blame] | 59 | assert(*errp == NULL); |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 60 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 61 | err = g_malloc0(sizeof(*err)); |
Luiz Capitulino | df1e608 | 2012-07-27 17:51:03 -0300 | [diff] [blame] | 62 | err->msg = g_strdup_vprintf(fmt, ap); |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 63 | if (suffix) { |
| 64 | char *msg = err->msg; |
| 65 | err->msg = g_strdup_printf("%s: %s", msg, suffix); |
| 66 | g_free(msg); |
| 67 | } |
Luiz Capitulino | 13f59ae | 2012-07-27 14:09:29 -0300 | [diff] [blame] | 68 | err->err_class = err_class; |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 69 | err->src = src; |
| 70 | err->line = line; |
| 71 | err->func = func; |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 72 | |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 73 | error_handle_fatal(errp, err); |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 74 | *errp = err; |
Max Reitz | b276d24 | 2013-11-07 20:10:29 +0100 | [diff] [blame] | 75 | |
| 76 | errno = saved_errno; |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 79 | void error_set_internal(Error **errp, |
| 80 | const char *src, int line, const char *func, |
| 81 | ErrorClass err_class, const char *fmt, ...) |
Markus Armbruster | 5523750 | 2015-06-19 13:59:47 +0200 | [diff] [blame] | 82 | { |
| 83 | va_list ap; |
| 84 | |
| 85 | va_start(ap, fmt); |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 86 | error_setv(errp, src, line, func, err_class, fmt, ap, NULL); |
Markus Armbruster | 5523750 | 2015-06-19 13:59:47 +0200 | [diff] [blame] | 87 | va_end(ap); |
| 88 | } |
| 89 | |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 90 | void error_setg_internal(Error **errp, |
| 91 | const char *src, int line, const char *func, |
| 92 | const char *fmt, ...) |
Markus Armbruster | a9499dd | 2015-06-19 15:36:16 +0200 | [diff] [blame] | 93 | { |
| 94 | va_list ap; |
| 95 | |
| 96 | va_start(ap, fmt); |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 97 | error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap, NULL); |
Markus Armbruster | a9499dd | 2015-06-19 15:36:16 +0200 | [diff] [blame] | 98 | va_end(ap); |
| 99 | } |
| 100 | |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 101 | void error_setg_errno_internal(Error **errp, |
| 102 | const char *src, int line, const char *func, |
| 103 | int os_errno, const char *fmt, ...) |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 104 | { |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 105 | va_list ap; |
Max Reitz | b276d24 | 2013-11-07 20:10:29 +0100 | [diff] [blame] | 106 | int saved_errno = errno; |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 107 | |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 108 | va_start(ap, fmt); |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 109 | error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, fmt, ap, |
| 110 | os_errno != 0 ? strerror(os_errno) : NULL); |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 111 | va_end(ap); |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 112 | |
Max Reitz | b276d24 | 2013-11-07 20:10:29 +0100 | [diff] [blame] | 113 | errno = saved_errno; |
Paolo Bonzini | 680d16d | 2012-10-02 09:00:45 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 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) |
Luiz Capitulino | 54028d7 | 2013-06-07 14:24:49 -0400 | [diff] [blame] | 119 | { |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 120 | error_setg_errno_internal(errp, src, line, func, os_errno, |
| 121 | "Could not open '%s'", filename); |
Luiz Capitulino | 54028d7 | 2013-06-07 14:24:49 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Vladimir Sementsov-Ogievskiy | 49fbc72 | 2019-12-05 20:46:17 +0300 | [diff] [blame] | 124 | void error_vprepend(Error *const *errp, const char *fmt, va_list ap) |
Markus Armbruster | 8277d2a | 2015-12-18 16:35:12 +0100 | [diff] [blame] | 125 | { |
| 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 Reitz | 536eeea | 2017-04-13 18:09:52 +0200 | [diff] [blame] | 135 | g_free((*errp)->msg); |
Markus Armbruster | 8277d2a | 2015-12-18 16:35:12 +0100 | [diff] [blame] | 136 | (*errp)->msg = g_string_free(newmsg, 0); |
| 137 | } |
| 138 | |
Vladimir Sementsov-Ogievskiy | 49fbc72 | 2019-12-05 20:46:17 +0300 | [diff] [blame] | 139 | void error_prepend(Error *const *errp, const char *fmt, ...) |
Markus Armbruster | 8277d2a | 2015-12-18 16:35:12 +0100 | [diff] [blame] | 140 | { |
| 141 | va_list ap; |
| 142 | |
| 143 | va_start(ap, fmt); |
| 144 | error_vprepend(errp, fmt, ap); |
| 145 | va_end(ap); |
| 146 | } |
| 147 | |
Vladimir Sementsov-Ogievskiy | 49fbc72 | 2019-12-05 20:46:17 +0300 | [diff] [blame] | 148 | void error_append_hint(Error *const *errp, const char *fmt, ...) |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 149 | { |
| 150 | va_list ap; |
| 151 | int saved_errno = errno; |
| 152 | Error *err; |
| 153 | |
| 154 | if (!errp) { |
| 155 | return; |
| 156 | } |
| 157 | err = *errp; |
Markus Armbruster | f4d0064 | 2015-12-18 16:35:08 +0100 | [diff] [blame] | 158 | assert(err && errp != &error_abort && errp != &error_fatal); |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 159 | |
| 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 Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 170 | #ifdef _WIN32 |
| 171 | |
Markus Armbruster | 1e9b65b | 2015-06-19 19:21:59 +0200 | [diff] [blame] | 172 | void error_setg_win32_internal(Error **errp, |
| 173 | const char *src, int line, const char *func, |
| 174 | int win32_err, const char *fmt, ...) |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 175 | { |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 176 | va_list ap; |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 177 | char *suffix = NULL; |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 178 | |
| 179 | if (errp == NULL) { |
| 180 | return; |
| 181 | } |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 182 | |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 183 | if (win32_err != 0) { |
| 184 | suffix = g_win32_error_message(win32_err); |
| 185 | } |
| 186 | |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 187 | va_start(ap, fmt); |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 188 | error_setv(errp, src, line, func, ERROR_CLASS_GENERIC_ERROR, |
| 189 | fmt, ap, suffix); |
Markus Armbruster | 5523750 | 2015-06-19 13:59:47 +0200 | [diff] [blame] | 190 | va_end(ap); |
| 191 | |
Daniel P. Berrange | 20e2dec | 2016-03-09 17:28:24 +0000 | [diff] [blame] | 192 | g_free(suffix); |
Tomoki Sekiyama | 20840d4 | 2013-08-07 11:40:11 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | #endif |
| 196 | |
Luiz Capitulino | 79020cf | 2011-12-05 16:04:05 -0200 | [diff] [blame] | 197 | Error *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 Capitulino | 13f59ae | 2012-07-27 14:09:29 -0300 | [diff] [blame] | 203 | err_new->err_class = err->err_class; |
Eric Blake | 88e2ce2 | 2015-09-10 10:34:50 -0600 | [diff] [blame] | 204 | err_new->src = err->src; |
| 205 | err_new->line = err->line; |
| 206 | err_new->func = err->func; |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 207 | if (err->hint) { |
| 208 | err_new->hint = g_string_new(err->hint->str); |
| 209 | } |
Luiz Capitulino | 79020cf | 2011-12-05 16:04:05 -0200 | [diff] [blame] | 210 | |
| 211 | return err_new; |
| 212 | } |
| 213 | |
Luiz Capitulino | ea25fbc | 2012-08-01 16:29:38 -0300 | [diff] [blame] | 214 | ErrorClass error_get_class(const Error *err) |
| 215 | { |
| 216 | return err->err_class; |
| 217 | } |
| 218 | |
Daniel P. Berrange | d59ce6f | 2016-04-27 11:05:00 +0100 | [diff] [blame] | 219 | const char *error_get_pretty(const Error *err) |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 220 | { |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 221 | return err->msg; |
| 222 | } |
| 223 | |
Markus Armbruster | 2ee2f1e | 2015-02-06 15:27:19 +0100 | [diff] [blame] | 224 | void error_report_err(Error *err) |
| 225 | { |
| 226 | error_report("%s", error_get_pretty(err)); |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 227 | if (err->hint) { |
Markus Armbruster | 4c8b7c8 | 2019-04-16 17:38:50 +0200 | [diff] [blame] | 228 | error_printf("%s", err->hint->str); |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 229 | } |
Markus Armbruster | 2ee2f1e | 2015-02-06 15:27:19 +0100 | [diff] [blame] | 230 | error_free(err); |
| 231 | } |
| 232 | |
Alistair Francis | e43ead1 | 2017-07-12 06:57:52 -0700 | [diff] [blame] | 233 | void warn_report_err(Error *err) |
| 234 | { |
| 235 | warn_report("%s", error_get_pretty(err)); |
| 236 | if (err->hint) { |
Markus Armbruster | 4c8b7c8 | 2019-04-16 17:38:50 +0200 | [diff] [blame] | 237 | error_printf("%s", err->hint->str); |
Alistair Francis | e43ead1 | 2017-07-12 06:57:52 -0700 | [diff] [blame] | 238 | } |
| 239 | error_free(err); |
| 240 | } |
| 241 | |
Markus Armbruster | 8277d2a | 2015-12-18 16:35:12 +0100 | [diff] [blame] | 242 | void 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 Francis | e43ead1 | 2017-07-12 06:57:52 -0700 | [diff] [blame] | 252 | |
| 253 | void 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 Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 263 | void error_free(Error *err) |
| 264 | { |
| 265 | if (err) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 266 | g_free(err->msg); |
Eric Blake | 50b7b00 | 2015-09-10 10:19:16 -0600 | [diff] [blame] | 267 | if (err->hint) { |
| 268 | g_string_free(err->hint, true); |
| 269 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 270 | g_free(err); |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 274 | void error_free_or_abort(Error **errp) |
| 275 | { |
| 276 | assert(errp && *errp); |
| 277 | error_free(*errp); |
| 278 | *errp = NULL; |
| 279 | } |
| 280 | |
Markus Armbruster | 64dfefe | 2014-05-02 13:26:32 +0200 | [diff] [blame] | 281 | void error_propagate(Error **dst_errp, Error *local_err) |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 282 | { |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 283 | if (!local_err) { |
| 284 | return; |
| 285 | } |
| 286 | error_handle_fatal(dst_errp, local_err); |
| 287 | if (dst_errp && !*dst_errp) { |
Markus Armbruster | 64dfefe | 2014-05-02 13:26:32 +0200 | [diff] [blame] | 288 | *dst_errp = local_err; |
Markus Armbruster | a29a37b | 2015-09-11 16:51:42 +0200 | [diff] [blame] | 289 | } else { |
Luiz Capitulino | d5ec4f2 | 2011-06-01 12:14:49 -0500 | [diff] [blame] | 290 | error_free(local_err); |
| 291 | } |
| 292 | } |
Markus Armbruster | 4b57664 | 2018-10-17 10:26:25 +0200 | [diff] [blame] | 293 | |
| 294 | void 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 | } |