1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
14#include "monitor/monitor.h"
15
16
17
18
19
20
21void error_vprintf(const char *fmt, va_list ap)
22{
23 if (cur_mon) {
24 monitor_vprintf(cur_mon, fmt, ap);
25 } else {
26 vfprintf(stderr, fmt, ap);
27 }
28}
29
30
31
32
33
34void error_printf(const char *fmt, ...)
35{
36 va_list ap;
37
38 va_start(ap, fmt);
39 error_vprintf(fmt, ap);
40 va_end(ap);
41}
42
43void error_printf_unless_qmp(const char *fmt, ...)
44{
45 va_list ap;
46
47 if (!monitor_cur_is_qmp()) {
48 va_start(ap, fmt);
49 error_vprintf(fmt, ap);
50 va_end(ap);
51 }
52}
53
54static Location std_loc = {
55 .kind = LOC_NONE
56};
57static Location *cur_loc = &std_loc;
58
59
60
61
62
63
64Location *loc_push_restore(Location *loc)
65{
66 assert(!loc->prev);
67 loc->prev = cur_loc;
68 cur_loc = loc;
69 return loc;
70}
71
72
73
74
75
76
77
78Location *loc_push_none(Location *loc)
79{
80 loc->kind = LOC_NONE;
81 loc->prev = NULL;
82 return loc_push_restore(loc);
83}
84
85
86
87
88
89Location *loc_pop(Location *loc)
90{
91 assert(cur_loc == loc && loc->prev);
92 cur_loc = loc->prev;
93 loc->prev = NULL;
94 return loc;
95}
96
97
98
99
100Location *loc_save(Location *loc)
101{
102 *loc = *cur_loc;
103 loc->prev = NULL;
104 return loc;
105}
106
107
108
109
110void loc_restore(Location *loc)
111{
112 Location *prev = cur_loc->prev;
113 assert(!loc->prev);
114 *cur_loc = *loc;
115 cur_loc->prev = prev;
116}
117
118
119
120
121void loc_set_none(void)
122{
123 cur_loc->kind = LOC_NONE;
124}
125
126
127
128
129void loc_set_cmdline(char **argv, int idx, int cnt)
130{
131 cur_loc->kind = LOC_CMDLINE;
132 cur_loc->num = cnt;
133 cur_loc->ptr = argv + idx;
134}
135
136
137
138
139void loc_set_file(const char *fname, int lno)
140{
141 assert (fname || cur_loc->kind == LOC_FILE);
142 cur_loc->kind = LOC_FILE;
143 cur_loc->num = lno;
144 if (fname) {
145 cur_loc->ptr = fname;
146 }
147}
148
149static const char *progname;
150
151
152
153
154void error_set_progname(const char *argv0)
155{
156 const char *p = strrchr(argv0, '/');
157 progname = p ? p + 1 : argv0;
158}
159
160const char *error_get_progname(void)
161{
162 return progname;
163}
164
165
166
167
168void error_print_loc(void)
169{
170 const char *sep = "";
171 int i;
172 const char *const *argp;
173
174 if (!cur_mon && progname) {
175 fprintf(stderr, "%s:", progname);
176 sep = " ";
177 }
178 switch (cur_loc->kind) {
179 case LOC_CMDLINE:
180 argp = cur_loc->ptr;
181 for (i = 0; i < cur_loc->num; i++) {
182 error_printf("%s%s", sep, argp[i]);
183 sep = " ";
184 }
185 error_printf(": ");
186 break;
187 case LOC_FILE:
188 error_printf("%s:", (const char *)cur_loc->ptr);
189 if (cur_loc->num) {
190 error_printf("%d:", cur_loc->num);
191 }
192 error_printf(" ");
193 break;
194 default:
195 error_printf("%s", sep);
196 }
197}
198
199
200
201
202
203
204
205
206void error_report(const char *fmt, ...)
207{
208 va_list ap;
209
210 error_print_loc();
211 va_start(ap, fmt);
212 error_vprintf(fmt, ap);
213 va_end(ap);
214 error_printf("\n");
215}
216