]> git.proxmox.com Git - mirror_qemu.git/blame - util/log.c
qemu-log: new option -dfilter to limit output
[mirror_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"
25#include "qemu/cutils.h"
c84ea00d 26#include "trace/control.h"
5726c27f 27
40a50b0a 28static char *logfilename;
eeacee4d
BS
29FILE *qemu_logfile;
30int qemu_loglevel;
5726c27f 31static int log_append = 0;
3514552e 32static GArray *debug_regions;
5726c27f 33
eeacee4d
BS
34void qemu_log(const char *fmt, ...)
35{
36 va_list ap;
37
38 va_start(ap, fmt);
39 if (qemu_logfile) {
40 vfprintf(qemu_logfile, fmt, ap);
41 }
42 va_end(ap);
43}
44
5726c27f 45/* enable or disable low levels log */
24537a01 46void do_qemu_set_log(int log_flags, bool use_own_buffers)
5726c27f 47{
eeacee4d 48 qemu_loglevel = log_flags;
ed7f5f1d
PB
49#ifdef CONFIG_TRACE_LOG
50 qemu_loglevel |= LOG_TRACE;
51#endif
c586eac3
PB
52 if (!qemu_logfile &&
53 (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
989b697d
PM
54 if (logfilename) {
55 qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
56 if (!qemu_logfile) {
57 perror(logfilename);
58 _exit(1);
59 }
96c33a45
DA
60 /* In case we are a daemon redirect stderr to logfile */
61 if (is_daemonized()) {
62 dup2(fileno(qemu_logfile), STDERR_FILENO);
63 fclose(qemu_logfile);
64 /* This will skip closing logfile in qemu_log_close() */
65 qemu_logfile = stderr;
66 }
989b697d
PM
67 } else {
68 /* Default to stderr if no log file specified */
c586eac3 69 assert(!is_daemonized());
989b697d 70 qemu_logfile = stderr;
5726c27f 71 }
5726c27f 72 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3437e545 73 if (use_own_buffers) {
5726c27f 74 static char logfile_buf[4096];
3437e545 75
eeacee4d 76 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
3437e545
BS
77 } else {
78#if defined(_WIN32)
79 /* Win32 doesn't support line-buffering, so use unbuffered output. */
80 setvbuf(qemu_logfile, NULL, _IONBF, 0);
5726c27f 81#else
3437e545 82 setvbuf(qemu_logfile, NULL, _IOLBF, 0);
5726c27f 83#endif
3437e545
BS
84 log_append = 1;
85 }
5726c27f 86 }
c586eac3
PB
87 if (qemu_logfile &&
88 (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
989b697d 89 qemu_log_close();
5726c27f
BS
90 }
91}
92
9a7e5424 93void qemu_set_log_filename(const char *filename)
5726c27f 94{
40a50b0a 95 g_free(logfilename);
636e0f27 96 logfilename = g_strdup(filename);
989b697d 97 qemu_log_close();
24537a01 98 qemu_set_log(qemu_loglevel);
5726c27f
BS
99}
100
3514552e
AB
101/* Returns true if addr is in our debug filter or no filter defined
102 */
103bool qemu_log_in_addr_range(uint64_t addr)
104{
105 if (debug_regions) {
106 int i = 0;
107 for (i = 0; i < debug_regions->len; i++) {
108 struct Range *range = &g_array_index(debug_regions, Range, i);
109 if (addr >= range->begin && addr <= range->end) {
110 return true;
111 }
112 }
113 return false;
114 } else {
115 return true;
116 }
117}
118
119
120void qemu_set_dfilter_ranges(const char *filter_spec)
121{
122 gchar **ranges = g_strsplit(filter_spec, ",", 0);
123 if (ranges) {
124 gchar **next = ranges;
125 gchar *r = *next++;
126 debug_regions = g_array_sized_new(FALSE, FALSE,
127 sizeof(Range), g_strv_length(ranges));
128 while (r) {
129 char *range_op = strstr(r, "-");
130 char *r2 = range_op ? range_op + 1 : NULL;
131 if (!range_op) {
132 range_op = strstr(r, "+");
133 r2 = range_op ? range_op + 1 : NULL;
134 }
135 if (!range_op) {
136 range_op = strstr(r, "..");
137 r2 = range_op ? range_op + 2 : NULL;
138 }
139 if (range_op) {
140 const char *e = NULL;
141 uint64_t r1val, r2val;
142
143 if ((qemu_strtoull(r, &e, 0, &r1val) == 0) &&
144 (qemu_strtoull(r2, NULL, 0, &r2val) == 0) &&
145 r2val > 0) {
146 struct Range range;
147
148 g_assert(e == range_op);
149
150 switch (*range_op) {
151 case '+':
152 {
153 range.begin = r1val;
154 range.end = r1val + (r2val - 1);
155 break;
156 }
157 case '-':
158 {
159 range.end = r1val;
160 range.begin = r1val - (r2val - 1);
161 break;
162 }
163 case '.':
164 range.begin = r1val;
165 range.end = r2val;
166 break;
167 default:
168 g_assert_not_reached();
169 }
170 g_array_append_val(debug_regions, range);
171
172 } else {
173 g_error("Failed to parse range in: %s", r);
174 }
175 } else {
176 g_error("Bad range specifier in: %s", r);
177 }
178 r = *next++;
179 }
180 g_strfreev(ranges);
181 }
182}
183
38dad9e5 184const QEMULogItem qemu_log_items[] = {
5726c27f
BS
185 { CPU_LOG_TB_OUT_ASM, "out_asm",
186 "show generated host assembly code for each compiled TB" },
187 { CPU_LOG_TB_IN_ASM, "in_asm",
188 "show target assembly code for each compiled TB" },
189 { CPU_LOG_TB_OP, "op",
190 "show micro ops for each compiled TB" },
191 { CPU_LOG_TB_OP_OPT, "op_opt",
3437e545 192 "show micro ops (x86 only: before eflags optimization) and\n"
5726c27f
BS
193 "after liveness analysis" },
194 { CPU_LOG_INT, "int",
195 "show interrupts/exceptions in short format" },
196 { CPU_LOG_EXEC, "exec",
197 "show trace before each executed TB (lots of logs)" },
198 { CPU_LOG_TB_CPU, "cpu",
54195736 199 "show CPU registers before entering a TB (lots of logs)" },
339aaf5b
AP
200 { CPU_LOG_MMU, "mmu",
201 "log MMU-related activities" },
5726c27f 202 { CPU_LOG_PCALL, "pcall",
3437e545 203 "x86 only: show protected mode far calls/returns/exceptions" },
5726c27f 204 { CPU_LOG_RESET, "cpu_reset",
dbfe1b6a 205 "show CPU state before CPU resets" },
dafdf1ab
BS
206 { LOG_UNIMP, "unimp",
207 "log unimplemented functionality" },
e54eba19
PM
208 { LOG_GUEST_ERROR, "guest_errors",
209 "log when the guest OS does something invalid (eg accessing a\n"
210 "non-existent register)" },
13829020
PB
211 { CPU_LOG_PAGE, "page",
212 "dump pages at beginning of user mode emulation" },
89a82cd4
RH
213 { CPU_LOG_TB_NOCHAIN, "nochain",
214 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
215 "complete traces" },
5726c27f
BS
216 { 0, NULL, NULL },
217};
218
219static int cmp1(const char *s1, int n, const char *s2)
220{
221 if (strlen(s2) != n) {
222 return 0;
223 }
224 return memcmp(s1, s2, n) == 0;
225}
226
227/* takes a comma separated list of log masks. Return 0 if error. */
4fde1eba 228int qemu_str_to_log_mask(const char *str)
5726c27f 229{
38dad9e5 230 const QEMULogItem *item;
5726c27f
BS
231 int mask;
232 const char *p, *p1;
233
234 p = str;
235 mask = 0;
236 for (;;) {
237 p1 = strchr(p, ',');
238 if (!p1) {
239 p1 = p + strlen(p);
240 }
241 if (cmp1(p,p1-p,"all")) {
38dad9e5 242 for (item = qemu_log_items; item->mask != 0; item++) {
5726c27f
BS
243 mask |= item->mask;
244 }
c84ea00d
PB
245#ifdef CONFIG_TRACE_LOG
246 } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
247 trace_enable_events(p + 6);
248 mask |= LOG_TRACE;
249#endif
5726c27f 250 } else {
38dad9e5 251 for (item = qemu_log_items; item->mask != 0; item++) {
5726c27f
BS
252 if (cmp1(p, p1 - p, item->name)) {
253 goto found;
254 }
255 }
256 return 0;
c84ea00d
PB
257 found:
258 mask |= item->mask;
5726c27f 259 }
5726c27f
BS
260 if (*p1 != ',') {
261 break;
262 }
263 p = p1 + 1;
264 }
265 return mask;
266}
59a6fa6e
PM
267
268void qemu_print_log_usage(FILE *f)
269{
38dad9e5 270 const QEMULogItem *item;
59a6fa6e 271 fprintf(f, "Log items (comma separated):\n");
38dad9e5 272 for (item = qemu_log_items; item->mask != 0; item++) {
c84ea00d 273 fprintf(f, "%-15s %s\n", item->name, item->help);
59a6fa6e 274 }
c84ea00d
PB
275#ifdef CONFIG_TRACE_LOG
276 fprintf(f, "trace:PATTERN enable trace events\n");
277 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
278#endif
59a6fa6e 279}