]> git.proxmox.com Git - qemu.git/blame - qemu-error.c
qdev: Convert qbus_find() to QError
[qemu.git] / qemu-error.c
CommitLineData
ba0fe87a
MA
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
b4a51f7f
MA
13#include <stdio.h>
14#include "monitor.h"
15#include "sysemu.h"
16
ba0fe87a
MA
17/*
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)
b4a51f7f 23{
6e4f984c 24 if (cur_mon) {
ba0fe87a 25 monitor_vprintf(cur_mon, fmt, ap);
6e4f984c 26 } else {
ba0fe87a 27 vfprintf(stderr, fmt, ap);
b4a51f7f 28 }
ba0fe87a
MA
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
827b0813
MA
44static Location std_loc = {
45 .kind = LOC_NONE
46};
47static Location *cur_loc = &std_loc;
48
49/*
50 * Push location saved in LOC onto the location stack, return it.
51 * The top of that stack is the current location.
52 * Needs a matching loc_pop().
53 */
54Location *loc_push_restore(Location *loc)
55{
56 assert(!loc->prev);
57 loc->prev = cur_loc;
58 cur_loc = loc;
59 return loc;
60}
61
62/*
63 * Initialize *LOC to "nowhere", push it onto the location stack.
64 * The top of that stack is the current location.
65 * Needs a matching loc_pop().
66 * Return LOC.
67 */
68Location *loc_push_none(Location *loc)
69{
70 loc->kind = LOC_NONE;
71 loc->prev = NULL;
72 return loc_push_restore(loc);
73}
74
75/*
76 * Pop the location stack.
77 * LOC must be the current location, i.e. the top of the stack.
78 */
79Location *loc_pop(Location *loc)
80{
81 assert(cur_loc == loc && loc->prev);
82 cur_loc = loc->prev;
83 loc->prev = NULL;
84 return loc;
85}
86
87/*
88 * Save the current location in LOC, return LOC.
89 */
90Location *loc_save(Location *loc)
91{
92 *loc = *cur_loc;
93 loc->prev = NULL;
94 return loc;
95}
96
97/*
98 * Change the current location to the one saved in LOC.
99 */
100void loc_restore(Location *loc)
101{
102 Location *prev = cur_loc->prev;
103 assert(!loc->prev);
104 *cur_loc = *loc;
105 cur_loc->prev = prev;
106}
107
108/*
109 * Change the current location to "nowhere in particular".
110 */
111void loc_set_none(void)
112{
113 cur_loc->kind = LOC_NONE;
114}
115
0f0bc3f1
MA
116/*
117 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
118 */
119void loc_set_cmdline(char **argv, int idx, int cnt)
120{
121 cur_loc->kind = LOC_CMDLINE;
122 cur_loc->num = cnt;
123 cur_loc->ptr = argv + idx;
124}
125
cf5a65aa
MA
126/*
127 * Change the current location to file FNAME, line LNO.
128 */
129void loc_set_file(const char *fname, int lno)
130{
131 assert (fname || cur_loc->kind == LOC_FILE);
132 cur_loc->kind = LOC_FILE;
133 cur_loc->num = lno;
134 if (fname) {
135 cur_loc->ptr = fname;
136 }
137}
138
65abca0a
MA
139static const char *progname;
140
141/*
142 * Set the program name for error_print_loc().
143 */
144void error_set_progname(const char *argv0)
145{
146 const char *p = strrchr(argv0, '/');
147 progname = p ? p + 1 : argv0;
148}
149
827b0813
MA
150/*
151 * Print current location to current monitor if we have one, else to stderr.
152 */
153void error_print_loc(void)
154{
65abca0a 155 const char *sep = "";
0f0bc3f1
MA
156 int i;
157 const char *const *argp;
65abca0a
MA
158
159 if (!cur_mon) {
160 fprintf(stderr, "%s:", progname);
161 sep = " ";
162 }
827b0813 163 switch (cur_loc->kind) {
0f0bc3f1
MA
164 case LOC_CMDLINE:
165 argp = cur_loc->ptr;
166 for (i = 0; i < cur_loc->num; i++) {
167 error_printf("%s%s", sep, argp[i]);
168 sep = " ";
169 }
170 error_printf(": ");
171 break;
cf5a65aa
MA
172 case LOC_FILE:
173 error_printf("%s:", (const char *)cur_loc->ptr);
174 if (cur_loc->num) {
175 error_printf("%d:", cur_loc->num);
176 }
177 error_printf(" ");
178 break;
65abca0a
MA
179 default:
180 error_printf(sep);
827b0813
MA
181 }
182}
183
1ecda02b
MA
184/*
185 * Print an error message to current monitor if we have one, else to stderr.
827b0813 186 * Prepend the current location and append a newline.
ab5b027e 187 * It's wrong to call this in a QMP monitor. Use qerror_report() there.
1ecda02b
MA
188 */
189void error_report(const char *fmt, ...)
ba0fe87a
MA
190{
191 va_list ap;
192
827b0813 193 error_print_loc();
ba0fe87a
MA
194 va_start(ap, fmt);
195 error_vprintf(fmt, ap);
196 va_end(ap);
1ecda02b 197 error_printf("\n");
b4a51f7f
MA
198}
199
ab5b027e
MA
200void qerror_report_internal(const char *file, int linenr, const char *func,
201 const char *fmt, ...)
b4a51f7f
MA
202{
203 va_list va;
204 QError *qerror;
205
b4a51f7f
MA
206 va_start(va, fmt);
207 qerror = qerror_from_info(file, linenr, func, fmt, &va);
208 va_end(va);
209
cde0fc75 210 if (monitor_cur_is_qmp()) {
6e4f984c
MA
211 monitor_set_error(cur_mon, qerror);
212 } else {
b4a51f7f
MA
213 qerror_print(qerror);
214 QDECREF(qerror);
b4a51f7f
MA
215 }
216}