]> git.ipfire.org Git - thirdparty/qemu.git/blame - util/log.c
log: Fix qemu_set_dfilter_ranges() error reporting
[thirdparty/qemu.git] / util / log.c
CommitLineData
5726c27f
BS
1/*
2 * Logging support
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
d38ea87a 20#include "qemu/osdep.h"
5726c27f 21#include "qemu-common.h"
1de7afc9 22#include "qemu/log.h"
3514552e
AB
23#include "qemu/range.h"
24#include "qemu/error-report.h"
bd6fee9f 25#include "qapi/error.h"
3514552e 26#include "qemu/cutils.h"
c84ea00d 27#include "trace/control.h"
5726c27f 28
40a50b0a 29static char *logfilename;
eeacee4d
BS
30FILE *qemu_logfile;
31int qemu_loglevel;
5726c27f 32static int log_append = 0;
3514552e 33static GArray *debug_regions;
5726c27f 34
eeacee4d
BS
35void qemu_log(const char *fmt, ...)
36{
37 va_list ap;
38
39 va_start(ap, fmt);
40 if (qemu_logfile) {
41 vfprintf(qemu_logfile, fmt, ap);
42 }
43 va_end(ap);
44}
45
f2937a33
PB
46static bool log_uses_own_buffers;
47
5726c27f 48/* enable or disable low levels log */
f2937a33 49void qemu_set_log(int log_flags)
5726c27f 50{
eeacee4d 51 qemu_loglevel = log_flags;
ed7f5f1d
PB
52#ifdef CONFIG_TRACE_LOG
53 qemu_loglevel |= LOG_TRACE;
54#endif
c586eac3
PB
55 if (!qemu_logfile &&
56 (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
989b697d
PM
57 if (logfilename) {
58 qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
59 if (!qemu_logfile) {
60 perror(logfilename);
61 _exit(1);
62 }
96c33a45
DA
63 /* In case we are a daemon redirect stderr to logfile */
64 if (is_daemonized()) {
65 dup2(fileno(qemu_logfile), STDERR_FILENO);
66 fclose(qemu_logfile);
67 /* This will skip closing logfile in qemu_log_close() */
68 qemu_logfile = stderr;
69 }
989b697d
PM
70 } else {
71 /* Default to stderr if no log file specified */
c586eac3 72 assert(!is_daemonized());
989b697d 73 qemu_logfile = stderr;
5726c27f 74 }
5726c27f 75 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
f2937a33 76 if (log_uses_own_buffers) {
5726c27f 77 static char logfile_buf[4096];
3437e545 78
eeacee4d 79 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
3437e545
BS
80 } else {
81#if defined(_WIN32)
82 /* Win32 doesn't support line-buffering, so use unbuffered output. */
83 setvbuf(qemu_logfile, NULL, _IONBF, 0);
5726c27f 84#else
3437e545 85 setvbuf(qemu_logfile, NULL, _IOLBF, 0);
5726c27f 86#endif
3437e545
BS
87 log_append = 1;
88 }
5726c27f 89 }
c586eac3
PB
90 if (qemu_logfile &&
91 (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
989b697d 92 qemu_log_close();
5726c27f
BS
93 }
94}
f2937a33
PB
95
96void qemu_log_needs_buffers(void)
97{
98 log_uses_own_buffers = true;
99}
100
f6880b7f
AB
101/*
102 * Allow the user to include %d in their logfile which will be
103 * substituted with the current PID. This is useful for debugging many
104 * nested linux-user tasks but will result in lots of logs.
105 */
9a7e5424 106void qemu_set_log_filename(const char *filename)
5726c27f 107{
f6880b7f 108 char *pidstr;
40a50b0a 109 g_free(logfilename);
f6880b7f
AB
110
111 pidstr = strstr(filename, "%");
112 if (pidstr) {
113 /* We only accept one %d, no other format strings */
114 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
115 error_report("Bad logfile format: %s", filename);
116 logfilename = NULL;
117 } else {
118 logfilename = g_strdup_printf(filename, getpid());
119 }
120 } else {
121 logfilename = g_strdup(filename);
122 }
989b697d 123 qemu_log_close();
24537a01 124 qemu_set_log(qemu_loglevel);
5726c27f
BS
125}
126
3514552e
AB
127/* Returns true if addr is in our debug filter or no filter defined
128 */
129bool qemu_log_in_addr_range(uint64_t addr)
130{
131 if (debug_regions) {
132 int i = 0;
133 for (i = 0; i < debug_regions->len; i++) {
134 struct Range *range = &g_array_index(debug_regions, Range, i);
135 if (addr >= range->begin && addr <= range->end) {
136 return true;
137 }
138 }
139 return false;
140 } else {
141 return true;
142 }
143}
144
145
bd6fee9f 146void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
3514552e
AB
147{
148 gchar **ranges = g_strsplit(filter_spec, ",", 0);
bd6fee9f 149 int i;
2ec62fae
MA
150
151 if (debug_regions) {
152 g_array_unref(debug_regions);
153 debug_regions = NULL;
154 }
155
bd6fee9f
MA
156 debug_regions = g_array_sized_new(FALSE, FALSE,
157 sizeof(Range), g_strv_length(ranges));
158 for (i = 0; ranges[i]; i++) {
159 const char *r = ranges[i];
160 const char *range_op, *r2, *e;
161 uint64_t r1val, r2val;
162 struct Range range;
163
164 range_op = strstr(r, "-");
165 r2 = range_op ? range_op + 1 : NULL;
166 if (!range_op) {
167 range_op = strstr(r, "+");
168 r2 = range_op ? range_op + 1 : NULL;
169 }
170 if (!range_op) {
171 range_op = strstr(r, "..");
172 r2 = range_op ? range_op + 2 : NULL;
173 }
174 if (!range_op) {
175 error_setg(errp, "Bad range specifier");
176 goto out;
177 }
178
179 if (qemu_strtoull(r, &e, 0, &r1val)
180 || e != range_op) {
181 error_setg(errp, "Invalid number to the left of %.*s",
182 (int)(r2 - range_op), range_op);
183 goto out;
184 }
185 if (qemu_strtoull(r2, NULL, 0, &r2val)) {
186 error_setg(errp, "Invalid number to the right of %.*s",
187 (int)(r2 - range_op), range_op);
188 goto out;
189 }
190 if (r2val == 0) {
191 error_setg(errp, "Invalid range");
192 goto out;
193 }
194
195 switch (*range_op) {
196 case '+':
197 range.begin = r1val;
198 range.end = r1val + (r2val - 1);
199 break;
200 case '-':
201 range.end = r1val;
202 range.begin = r1val - (r2val - 1);
203 break;
204 case '.':
205 range.begin = r1val;
206 range.end = r2val;
207 break;
208 default:
209 g_assert_not_reached();
3514552e 210 }
bd6fee9f 211 g_array_append_val(debug_regions, range);
3514552e 212 }
bd6fee9f
MA
213out:
214 g_strfreev(ranges);
3514552e
AB
215}
216
99affd1d
DL
217/* fflush() the log file */
218void qemu_log_flush(void)
219{
220 fflush(qemu_logfile);
221}
222
223/* Close the log file */
224void qemu_log_close(void)
225{
226 if (qemu_logfile) {
227 if (qemu_logfile != stderr) {
228 fclose(qemu_logfile);
229 }
230 qemu_logfile = NULL;
231 }
232}
233
38dad9e5 234const QEMULogItem qemu_log_items[] = {
5726c27f
BS
235 { CPU_LOG_TB_OUT_ASM, "out_asm",
236 "show generated host assembly code for each compiled TB" },
237 { CPU_LOG_TB_IN_ASM, "in_asm",
238 "show target assembly code for each compiled TB" },
239 { CPU_LOG_TB_OP, "op",
240 "show micro ops for each compiled TB" },
241 { CPU_LOG_TB_OP_OPT, "op_opt",
3437e545 242 "show micro ops (x86 only: before eflags optimization) and\n"
5726c27f
BS
243 "after liveness analysis" },
244 { CPU_LOG_INT, "int",
245 "show interrupts/exceptions in short format" },
246 { CPU_LOG_EXEC, "exec",
247 "show trace before each executed TB (lots of logs)" },
248 { CPU_LOG_TB_CPU, "cpu",
54195736 249 "show CPU registers before entering a TB (lots of logs)" },
339aaf5b
AP
250 { CPU_LOG_MMU, "mmu",
251 "log MMU-related activities" },
5726c27f 252 { CPU_LOG_PCALL, "pcall",
3437e545 253 "x86 only: show protected mode far calls/returns/exceptions" },
5726c27f 254 { CPU_LOG_RESET, "cpu_reset",
dbfe1b6a 255 "show CPU state before CPU resets" },
dafdf1ab
BS
256 { LOG_UNIMP, "unimp",
257 "log unimplemented functionality" },
e54eba19
PM
258 { LOG_GUEST_ERROR, "guest_errors",
259 "log when the guest OS does something invalid (eg accessing a\n"
260 "non-existent register)" },
13829020
PB
261 { CPU_LOG_PAGE, "page",
262 "dump pages at beginning of user mode emulation" },
89a82cd4
RH
263 { CPU_LOG_TB_NOCHAIN, "nochain",
264 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
265 "complete traces" },
5726c27f
BS
266 { 0, NULL, NULL },
267};
268
269static int cmp1(const char *s1, int n, const char *s2)
270{
271 if (strlen(s2) != n) {
272 return 0;
273 }
274 return memcmp(s1, s2, n) == 0;
275}
276
277/* takes a comma separated list of log masks. Return 0 if error. */
4fde1eba 278int qemu_str_to_log_mask(const char *str)
5726c27f 279{
38dad9e5 280 const QEMULogItem *item;
5726c27f
BS
281 int mask;
282 const char *p, *p1;
283
284 p = str;
285 mask = 0;
286 for (;;) {
287 p1 = strchr(p, ',');
288 if (!p1) {
289 p1 = p + strlen(p);
290 }
291 if (cmp1(p,p1-p,"all")) {
38dad9e5 292 for (item = qemu_log_items; item->mask != 0; item++) {
5726c27f
BS
293 mask |= item->mask;
294 }
c84ea00d
PB
295#ifdef CONFIG_TRACE_LOG
296 } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
297 trace_enable_events(p + 6);
298 mask |= LOG_TRACE;
299#endif
5726c27f 300 } else {
38dad9e5 301 for (item = qemu_log_items; item->mask != 0; item++) {
5726c27f
BS
302 if (cmp1(p, p1 - p, item->name)) {
303 goto found;
304 }
305 }
306 return 0;
c84ea00d
PB
307 found:
308 mask |= item->mask;
5726c27f 309 }
5726c27f
BS
310 if (*p1 != ',') {
311 break;
312 }
313 p = p1 + 1;
314 }
315 return mask;
316}
59a6fa6e
PM
317
318void qemu_print_log_usage(FILE *f)
319{
38dad9e5 320 const QEMULogItem *item;
59a6fa6e 321 fprintf(f, "Log items (comma separated):\n");
38dad9e5 322 for (item = qemu_log_items; item->mask != 0; item++) {
c84ea00d 323 fprintf(f, "%-15s %s\n", item->name, item->help);
59a6fa6e 324 }
c84ea00d
PB
325#ifdef CONFIG_TRACE_LOG
326 fprintf(f, "trace:PATTERN enable trace events\n");
327 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
328#endif
59a6fa6e 329}