blob: 1ef35664af3d925d302d798a53639aaa491b4aae [file] [log] [blame]
Markus Armbrusterba0fe872010-02-18 17:14:17 +01001/*
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 Maydellaafd7582016-01-29 17:49:55 +000013#include "qemu/osdep.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010014#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010015#include "qemu/error-report.h"
Markus Armbrusterb4a51f72010-02-17 10:55:46 +010016
Markus Armbrusterba0fe872010-02-18 17:14:17 +010017/*
18 * Print to current monitor if we have one, else to stderr.
19 * TODO should return int, so callers can calculate width, but that
20 * requires surgery to monitor_vprintf(). Left for another day.
21 */
22void error_vprintf(const char *fmt, va_list ap)
23{
Cole Robinson4ad417b2014-03-21 19:42:24 -040024 if (cur_mon && !monitor_cur_is_qmp()) {
Markus Armbrusterba0fe872010-02-18 17:14:17 +010025 monitor_vprintf(cur_mon, fmt, ap);
26 } else {
27 vfprintf(stderr, fmt, ap);
28 }
29}
30
31/*
32 * Print to current monitor if we have one, else to stderr.
33 * TODO just like error_vprintf()
34 */
35void error_printf(const char *fmt, ...)
36{
37 va_list ap;
38
39 va_start(ap, fmt);
40 error_vprintf(fmt, ap);
41 va_end(ap);
42}
43
Markus Armbrusteraa924ae2010-02-19 10:30:05 +010044void error_printf_unless_qmp(const char *fmt, ...)
45{
46 va_list ap;
47
48 if (!monitor_cur_is_qmp()) {
49 va_start(ap, fmt);
50 error_vprintf(fmt, ap);
51 va_end(ap);
52 }
53}
54
Markus Armbruster827b0812010-02-18 19:46:49 +010055static Location std_loc = {
56 .kind = LOC_NONE
57};
58static Location *cur_loc = &std_loc;
59
60/*
61 * Push location saved in LOC onto the location stack, return it.
62 * The top of that stack is the current location.
63 * Needs a matching loc_pop().
64 */
65Location *loc_push_restore(Location *loc)
66{
67 assert(!loc->prev);
68 loc->prev = cur_loc;
69 cur_loc = loc;
70 return loc;
71}
72
73/*
74 * Initialize *LOC to "nowhere", push it onto the location stack.
75 * The top of that stack is the current location.
76 * Needs a matching loc_pop().
77 * Return LOC.
78 */
79Location *loc_push_none(Location *loc)
80{
81 loc->kind = LOC_NONE;
82 loc->prev = NULL;
83 return loc_push_restore(loc);
84}
85
86/*
87 * Pop the location stack.
88 * LOC must be the current location, i.e. the top of the stack.
89 */
90Location *loc_pop(Location *loc)
91{
92 assert(cur_loc == loc && loc->prev);
93 cur_loc = loc->prev;
94 loc->prev = NULL;
95 return loc;
96}
97
98/*
99 * Save the current location in LOC, return LOC.
100 */
101Location *loc_save(Location *loc)
102{
103 *loc = *cur_loc;
104 loc->prev = NULL;
105 return loc;
106}
107
108/*
109 * Change the current location to the one saved in LOC.
110 */
111void loc_restore(Location *loc)
112{
113 Location *prev = cur_loc->prev;
114 assert(!loc->prev);
115 *cur_loc = *loc;
116 cur_loc->prev = prev;
117}
118
119/*
120 * Change the current location to "nowhere in particular".
121 */
122void loc_set_none(void)
123{
124 cur_loc->kind = LOC_NONE;
125}
126
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100127/*
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +0100128 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
129 */
130void loc_set_cmdline(char **argv, int idx, int cnt)
131{
132 cur_loc->kind = LOC_CMDLINE;
133 cur_loc->num = cnt;
134 cur_loc->ptr = argv + idx;
135}
136
137/*
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100138 * Change the current location to file FNAME, line LNO.
139 */
140void loc_set_file(const char *fname, int lno)
141{
142 assert (fname || cur_loc->kind == LOC_FILE);
143 cur_loc->kind = LOC_FILE;
144 cur_loc->num = lno;
145 if (fname) {
146 cur_loc->ptr = fname;
147 }
148}
149
Markus Armbruster65abca02010-02-24 14:37:14 +0100150static const char *progname;
151
152/*
153 * Set the program name for error_print_loc().
154 */
155void error_set_progname(const char *argv0)
156{
157 const char *p = strrchr(argv0, '/');
158 progname = p ? p + 1 : argv0;
159}
160
michael@ozlabs.org7636a472011-12-13 12:22:57 +1100161const char *error_get_progname(void)
162{
163 return progname;
164}
165
Markus Armbruster827b0812010-02-18 19:46:49 +0100166/*
167 * Print current location to current monitor if we have one, else to stderr.
168 */
Cole Robinsond876f602014-03-21 19:42:22 -0400169static void error_print_loc(void)
Markus Armbruster827b0812010-02-18 19:46:49 +0100170{
Markus Armbruster65abca02010-02-24 14:37:14 +0100171 const char *sep = "";
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +0100172 int i;
173 const char *const *argp;
Markus Armbruster65abca02010-02-24 14:37:14 +0100174
Markus Armbruster6627f642010-03-22 10:29:03 +0100175 if (!cur_mon && progname) {
Markus Armbruster65abca02010-02-24 14:37:14 +0100176 fprintf(stderr, "%s:", progname);
177 sep = " ";
178 }
Markus Armbruster827b0812010-02-18 19:46:49 +0100179 switch (cur_loc->kind) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +0100180 case LOC_CMDLINE:
181 argp = cur_loc->ptr;
182 for (i = 0; i < cur_loc->num; i++) {
183 error_printf("%s%s", sep, argp[i]);
184 sep = " ";
185 }
186 error_printf(": ");
187 break;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100188 case LOC_FILE:
189 error_printf("%s:", (const char *)cur_loc->ptr);
190 if (cur_loc->num) {
191 error_printf("%d:", cur_loc->num);
192 }
193 error_printf(" ");
194 break;
Markus Armbruster65abca02010-02-24 14:37:14 +0100195 default:
Edgar E. Iglesiasbb334b12010-03-23 16:13:03 +0100196 error_printf("%s", sep);
Markus Armbruster827b0812010-02-18 19:46:49 +0100197 }
198}
199
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400200bool enable_timestamp_msg;
Markus Armbruster1ecda022010-02-18 17:25:24 +0100201/*
202 * Print an error message to current monitor if we have one, else to stderr.
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100203 * Format arguments like vsprintf(). The resulting message should be
204 * a single phrase, with no newline or trailing punctuation.
Markus Armbruster827b0812010-02-18 19:46:49 +0100205 * Prepend the current location and append a newline.
Markus Armbruster485febc2015-03-13 17:25:50 +0100206 * It's wrong to call this in a QMP monitor. Use error_setg() there.
Markus Armbruster1ecda022010-02-18 17:25:24 +0100207 */
Corey Minyard5748e4c2014-10-08 07:11:54 -0500208void error_vreport(const char *fmt, va_list ap)
Markus Armbrusterb4a51f72010-02-17 10:55:46 +0100209{
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400210 GTimeVal tv;
211 gchar *timestr;
212
Stefan Hajnoczi615cf662015-08-10 14:15:41 +0100213 if (enable_timestamp_msg && !cur_mon) {
Seiji Aguchi5e2ac512013-07-03 23:02:46 -0400214 g_get_current_time(&tv);
215 timestr = g_time_val_to_iso8601(&tv);
216 error_printf("%s ", timestr);
217 g_free(timestr);
218 }
Markus Armbrusterb4a51f72010-02-17 10:55:46 +0100219
Markus Armbruster827b0812010-02-18 19:46:49 +0100220 error_print_loc();
Markus Armbrusterba0fe872010-02-18 17:14:17 +0100221 error_vprintf(fmt, ap);
Markus Armbruster1ecda022010-02-18 17:25:24 +0100222 error_printf("\n");
Markus Armbrusterb4a51f72010-02-17 10:55:46 +0100223}
Corey Minyard5748e4c2014-10-08 07:11:54 -0500224
225/*
226 * Print an error message to current monitor if we have one, else to stderr.
Markus Armbrusterf4d00642015-12-18 16:35:08 +0100227 * Format arguments like sprintf(). The resulting message should be a
228 * single phrase, with no newline or trailing punctuation.
Corey Minyard5748e4c2014-10-08 07:11:54 -0500229 * Prepend the current location and append a newline.
Markus Armbruster485febc2015-03-13 17:25:50 +0100230 * It's wrong to call this in a QMP monitor. Use error_setg() there.
Corey Minyard5748e4c2014-10-08 07:11:54 -0500231 */
232void error_report(const char *fmt, ...)
233{
234 va_list ap;
235
236 va_start(ap, fmt);
237 error_vreport(fmt, ap);
238 va_end(ap);
239}