Markus Armbruster | ba0fe87 | 2010-02-18 17:14:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Error reporting |
| 3 | * |
| 4 | * Copyright (C) 2010 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Markus Armbruster <armbru@redhat.com>, |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 14 | #include "monitor/monitor.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 15 | #include "qemu/error-report.h" |
Markus Armbruster | b4a51f7 | 2010-02-17 10:55:46 +0100 | [diff] [blame] | 16 | |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 17 | /* |
| 18 | * @report_type is the type of message: error, warning or |
| 19 | * informational. |
| 20 | */ |
| 21 | typedef enum { |
| 22 | REPORT_TYPE_ERROR, |
| 23 | REPORT_TYPE_WARNING, |
| 24 | REPORT_TYPE_INFO, |
| 25 | } report_type; |
| 26 | |
Markus Armbruster | ba0fe87 | 2010-02-18 17:14:17 +0100 | [diff] [blame] | 27 | void error_printf(const char *fmt, ...) |
| 28 | { |
| 29 | va_list ap; |
| 30 | |
| 31 | va_start(ap, fmt); |
| 32 | error_vprintf(fmt, ap); |
| 33 | va_end(ap); |
| 34 | } |
| 35 | |
Markus Armbruster | aa924ae | 2010-02-19 10:30:05 +0100 | [diff] [blame] | 36 | void error_printf_unless_qmp(const char *fmt, ...) |
| 37 | { |
| 38 | va_list ap; |
| 39 | |
Paolo Bonzini | 397d30e | 2016-10-24 18:31:02 +0200 | [diff] [blame] | 40 | va_start(ap, fmt); |
| 41 | error_vprintf_unless_qmp(fmt, ap); |
| 42 | va_end(ap); |
Markus Armbruster | aa924ae | 2010-02-19 10:30:05 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 45 | static Location std_loc = { |
| 46 | .kind = LOC_NONE |
| 47 | }; |
| 48 | static Location *cur_loc = &std_loc; |
| 49 | |
| 50 | /* |
| 51 | * Push location saved in LOC onto the location stack, return it. |
| 52 | * The top of that stack is the current location. |
| 53 | * Needs a matching loc_pop(). |
| 54 | */ |
| 55 | Location *loc_push_restore(Location *loc) |
| 56 | { |
| 57 | assert(!loc->prev); |
| 58 | loc->prev = cur_loc; |
| 59 | cur_loc = loc; |
| 60 | return loc; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Initialize *LOC to "nowhere", push it onto the location stack. |
| 65 | * The top of that stack is the current location. |
| 66 | * Needs a matching loc_pop(). |
| 67 | * Return LOC. |
| 68 | */ |
| 69 | Location *loc_push_none(Location *loc) |
| 70 | { |
| 71 | loc->kind = LOC_NONE; |
| 72 | loc->prev = NULL; |
| 73 | return loc_push_restore(loc); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Pop the location stack. |
| 78 | * LOC must be the current location, i.e. the top of the stack. |
| 79 | */ |
| 80 | Location *loc_pop(Location *loc) |
| 81 | { |
| 82 | assert(cur_loc == loc && loc->prev); |
| 83 | cur_loc = loc->prev; |
| 84 | loc->prev = NULL; |
| 85 | return loc; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Save the current location in LOC, return LOC. |
| 90 | */ |
| 91 | Location *loc_save(Location *loc) |
| 92 | { |
| 93 | *loc = *cur_loc; |
| 94 | loc->prev = NULL; |
| 95 | return loc; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Change the current location to the one saved in LOC. |
| 100 | */ |
| 101 | void loc_restore(Location *loc) |
| 102 | { |
| 103 | Location *prev = cur_loc->prev; |
| 104 | assert(!loc->prev); |
| 105 | *cur_loc = *loc; |
| 106 | cur_loc->prev = prev; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Change the current location to "nowhere in particular". |
| 111 | */ |
| 112 | void loc_set_none(void) |
| 113 | { |
| 114 | cur_loc->kind = LOC_NONE; |
| 115 | } |
| 116 | |
Markus Armbruster | cf5a65a | 2010-02-18 19:48:33 +0100 | [diff] [blame] | 117 | /* |
Markus Armbruster | 0f0bc3f | 2010-02-18 20:13:51 +0100 | [diff] [blame] | 118 | * Change the current location to argument ARGV[IDX..IDX+CNT-1]. |
| 119 | */ |
| 120 | void loc_set_cmdline(char **argv, int idx, int cnt) |
| 121 | { |
| 122 | cur_loc->kind = LOC_CMDLINE; |
| 123 | cur_loc->num = cnt; |
| 124 | cur_loc->ptr = argv + idx; |
| 125 | } |
| 126 | |
| 127 | /* |
Markus Armbruster | cf5a65a | 2010-02-18 19:48:33 +0100 | [diff] [blame] | 128 | * Change the current location to file FNAME, line LNO. |
| 129 | */ |
| 130 | void loc_set_file(const char *fname, int lno) |
| 131 | { |
| 132 | assert (fname || cur_loc->kind == LOC_FILE); |
| 133 | cur_loc->kind = LOC_FILE; |
| 134 | cur_loc->num = lno; |
| 135 | if (fname) { |
| 136 | cur_loc->ptr = fname; |
| 137 | } |
| 138 | } |
| 139 | |
Markus Armbruster | 65abca0 | 2010-02-24 14:37:14 +0100 | [diff] [blame] | 140 | static const char *progname; |
| 141 | |
| 142 | /* |
| 143 | * Set the program name for error_print_loc(). |
| 144 | */ |
| 145 | void error_set_progname(const char *argv0) |
| 146 | { |
| 147 | const char *p = strrchr(argv0, '/'); |
| 148 | progname = p ? p + 1 : argv0; |
| 149 | } |
| 150 | |
michael@ozlabs.org | 7636a47 | 2011-12-13 12:22:57 +1100 | [diff] [blame] | 151 | const char *error_get_progname(void) |
| 152 | { |
| 153 | return progname; |
| 154 | } |
| 155 | |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 156 | /* |
| 157 | * Print current location to current monitor if we have one, else to stderr. |
| 158 | */ |
Alistair Francis | beeb175 | 2017-07-12 06:57:31 -0700 | [diff] [blame] | 159 | static void print_loc(void) |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 160 | { |
Markus Armbruster | 65abca0 | 2010-02-24 14:37:14 +0100 | [diff] [blame] | 161 | const char *sep = ""; |
Markus Armbruster | 0f0bc3f | 2010-02-18 20:13:51 +0100 | [diff] [blame] | 162 | int i; |
| 163 | const char *const *argp; |
Markus Armbruster | 65abca0 | 2010-02-24 14:37:14 +0100 | [diff] [blame] | 164 | |
Markus Armbruster | 6627f64 | 2010-03-22 10:29:03 +0100 | [diff] [blame] | 165 | if (!cur_mon && progname) { |
Markus Armbruster | 65abca0 | 2010-02-24 14:37:14 +0100 | [diff] [blame] | 166 | fprintf(stderr, "%s:", progname); |
| 167 | sep = " "; |
| 168 | } |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 169 | switch (cur_loc->kind) { |
Markus Armbruster | 0f0bc3f | 2010-02-18 20:13:51 +0100 | [diff] [blame] | 170 | case LOC_CMDLINE: |
| 171 | argp = cur_loc->ptr; |
| 172 | for (i = 0; i < cur_loc->num; i++) { |
| 173 | error_printf("%s%s", sep, argp[i]); |
| 174 | sep = " "; |
| 175 | } |
| 176 | error_printf(": "); |
| 177 | break; |
Markus Armbruster | cf5a65a | 2010-02-18 19:48:33 +0100 | [diff] [blame] | 178 | case LOC_FILE: |
| 179 | error_printf("%s:", (const char *)cur_loc->ptr); |
| 180 | if (cur_loc->num) { |
| 181 | error_printf("%d:", cur_loc->num); |
| 182 | } |
| 183 | error_printf(" "); |
| 184 | break; |
Markus Armbruster | 65abca0 | 2010-02-24 14:37:14 +0100 | [diff] [blame] | 185 | default: |
Edgar E. Iglesias | bb334b1 | 2010-03-23 16:13:03 +0100 | [diff] [blame] | 186 | error_printf("%s", sep); |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Seiji Aguchi | 5e2ac51 | 2013-07-03 23:02:46 -0400 | [diff] [blame] | 190 | bool enable_timestamp_msg; |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 191 | /* |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 192 | * Print a message to current monitor if we have one, else to stderr. |
| 193 | * @report_type is the type of message: error, warning or informational. |
Markus Armbruster | f4d0064 | 2015-12-18 16:35:08 +0100 | [diff] [blame] | 194 | * Format arguments like vsprintf(). The resulting message should be |
| 195 | * a single phrase, with no newline or trailing punctuation. |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 196 | * Prepend the current location and append a newline. |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 197 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 198 | */ |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 199 | static void vreport(report_type type, const char *fmt, va_list ap) |
Markus Armbruster | b4a51f7 | 2010-02-17 10:55:46 +0100 | [diff] [blame] | 200 | { |
Seiji Aguchi | 5e2ac51 | 2013-07-03 23:02:46 -0400 | [diff] [blame] | 201 | GTimeVal tv; |
| 202 | gchar *timestr; |
| 203 | |
Markus Armbruster | 70f17a1 | 2017-07-19 09:33:34 +0200 | [diff] [blame] | 204 | if (enable_timestamp_msg && !cur_mon) { |
| 205 | g_get_current_time(&tv); |
| 206 | timestr = g_time_val_to_iso8601(&tv); |
| 207 | error_printf("%s ", timestr); |
| 208 | g_free(timestr); |
| 209 | } |
| 210 | |
| 211 | print_loc(); |
| 212 | |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 213 | switch (type) { |
| 214 | case REPORT_TYPE_ERROR: |
| 215 | break; |
| 216 | case REPORT_TYPE_WARNING: |
| 217 | error_printf("warning: "); |
| 218 | break; |
| 219 | case REPORT_TYPE_INFO: |
| 220 | error_printf("info: "); |
| 221 | break; |
| 222 | } |
| 223 | |
Markus Armbruster | ba0fe87 | 2010-02-18 17:14:17 +0100 | [diff] [blame] | 224 | error_vprintf(fmt, ap); |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 225 | error_printf("\n"); |
Markus Armbruster | b4a51f7 | 2010-02-17 10:55:46 +0100 | [diff] [blame] | 226 | } |
Corey Minyard | 5748e4c | 2014-10-08 07:11:54 -0500 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Print an error message to current monitor if we have one, else to stderr. |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 230 | * Format arguments like vsprintf(). The resulting message should be |
| 231 | * a single phrase, with no newline or trailing punctuation. |
| 232 | * Prepend the current location and append a newline. |
| 233 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
| 234 | */ |
| 235 | void error_vreport(const char *fmt, va_list ap) |
| 236 | { |
| 237 | vreport(REPORT_TYPE_ERROR, fmt, ap); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Print a warning message to current monitor if we have one, else to stderr. |
| 242 | * Format arguments like vsprintf(). The resulting message should be |
| 243 | * a single phrase, with no newline or trailing punctuation. |
| 244 | * Prepend the current location and append a newline. |
| 245 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
| 246 | */ |
| 247 | void warn_vreport(const char *fmt, va_list ap) |
| 248 | { |
| 249 | vreport(REPORT_TYPE_WARNING, fmt, ap); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Print an information message to current monitor if we have one, else to |
| 254 | * stderr. |
| 255 | * Format arguments like vsprintf(). The resulting message should be |
| 256 | * a single phrase, with no newline or trailing punctuation. |
| 257 | * Prepend the current location and append a newline. |
| 258 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
| 259 | */ |
| 260 | void info_vreport(const char *fmt, va_list ap) |
| 261 | { |
| 262 | vreport(REPORT_TYPE_INFO, fmt, ap); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Print an error message to current monitor if we have one, else to stderr. |
| 267 | * Format arguments like sprintf(). The resulting message should be |
| 268 | * a single phrase, with no newline or trailing punctuation. |
Corey Minyard | 5748e4c | 2014-10-08 07:11:54 -0500 | [diff] [blame] | 269 | * Prepend the current location and append a newline. |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 270 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
Corey Minyard | 5748e4c | 2014-10-08 07:11:54 -0500 | [diff] [blame] | 271 | */ |
| 272 | void error_report(const char *fmt, ...) |
| 273 | { |
| 274 | va_list ap; |
| 275 | |
| 276 | va_start(ap, fmt); |
Alistair Francis | 97f4030 | 2017-07-12 06:57:36 -0700 | [diff] [blame] | 277 | vreport(REPORT_TYPE_ERROR, fmt, ap); |
| 278 | va_end(ap); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Print a warning message to current monitor if we have one, else to stderr. |
| 283 | * Format arguments like sprintf(). The resulting message should be a |
| 284 | * single phrase, with no newline or trailing punctuation. |
| 285 | * Prepend the current location and append a newline. |
| 286 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
| 287 | */ |
| 288 | void warn_report(const char *fmt, ...) |
| 289 | { |
| 290 | va_list ap; |
| 291 | |
| 292 | va_start(ap, fmt); |
| 293 | vreport(REPORT_TYPE_WARNING, fmt, ap); |
| 294 | va_end(ap); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Print an information message to current monitor if we have one, else to |
| 299 | * stderr. |
| 300 | * Format arguments like sprintf(). The resulting message should be a |
| 301 | * single phrase, with no newline or trailing punctuation. |
| 302 | * Prepend the current location and append a newline. |
| 303 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
| 304 | */ |
| 305 | void info_report(const char *fmt, ...) |
| 306 | { |
| 307 | va_list ap; |
| 308 | |
| 309 | va_start(ap, fmt); |
| 310 | vreport(REPORT_TYPE_INFO, fmt, ap); |
Corey Minyard | 5748e4c | 2014-10-08 07:11:54 -0500 | [diff] [blame] | 311 | va_end(ap); |
| 312 | } |