4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
23 #include "qemu/range.h"
24 #include "qemu/error-report.h"
25 #include "qemu/cutils.h"
26 #include "trace/control.h"
28 static char *logfilename
;
31 static int log_append
= 0;
32 static GArray
*debug_regions
;
34 void qemu_log(const char *fmt
, ...)
40 vfprintf(qemu_logfile
, fmt
, ap
);
45 static bool log_uses_own_buffers
;
47 /* enable or disable low levels log */
48 void qemu_set_log(int log_flags
)
50 qemu_loglevel
= log_flags
;
51 #ifdef CONFIG_TRACE_LOG
52 qemu_loglevel
|= LOG_TRACE
;
55 (is_daemonized() ? logfilename
!= NULL
: qemu_loglevel
)) {
57 qemu_logfile
= fopen(logfilename
, log_append
? "a" : "w");
62 /* In case we are a daemon redirect stderr to logfile */
63 if (is_daemonized()) {
64 dup2(fileno(qemu_logfile
), STDERR_FILENO
);
66 /* This will skip closing logfile in qemu_log_close() */
67 qemu_logfile
= stderr
;
70 /* Default to stderr if no log file specified */
71 assert(!is_daemonized());
72 qemu_logfile
= stderr
;
74 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
75 if (log_uses_own_buffers
) {
76 static char logfile_buf
[4096];
78 setvbuf(qemu_logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
81 /* Win32 doesn't support line-buffering, so use unbuffered output. */
82 setvbuf(qemu_logfile
, NULL
, _IONBF
, 0);
84 setvbuf(qemu_logfile
, NULL
, _IOLBF
, 0);
90 (is_daemonized() ? logfilename
== NULL
: !qemu_loglevel
)) {
95 void qemu_log_needs_buffers(void)
97 log_uses_own_buffers
= true;
101 * Allow the user to include %d in their logfile which will be
102 * substituted with the current PID. This is useful for debugging many
103 * nested linux-user tasks but will result in lots of logs.
105 void qemu_set_log_filename(const char *filename
)
110 pidstr
= strstr(filename
, "%");
112 /* We only accept one %d, no other format strings */
113 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
114 error_report("Bad logfile format: %s", filename
);
117 logfilename
= g_strdup_printf(filename
, getpid());
120 logfilename
= g_strdup(filename
);
123 qemu_set_log(qemu_loglevel
);
126 /* Returns true if addr is in our debug filter or no filter defined
128 bool qemu_log_in_addr_range(uint64_t addr
)
132 for (i
= 0; i
< debug_regions
->len
; i
++) {
133 struct Range
*range
= &g_array_index(debug_regions
, Range
, i
);
134 if (addr
>= range
->begin
&& addr
<= range
->end
) {
145 void qemu_set_dfilter_ranges(const char *filter_spec
)
147 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
149 gchar
**next
= ranges
;
151 debug_regions
= g_array_sized_new(FALSE
, FALSE
,
152 sizeof(Range
), g_strv_length(ranges
));
154 char *range_op
= strstr(r
, "-");
155 char *r2
= range_op
? range_op
+ 1 : NULL
;
157 range_op
= strstr(r
, "+");
158 r2
= range_op
? range_op
+ 1 : NULL
;
161 range_op
= strstr(r
, "..");
162 r2
= range_op
? range_op
+ 2 : NULL
;
165 const char *e
= NULL
;
166 uint64_t r1val
, r2val
;
168 if ((qemu_strtoull(r
, &e
, 0, &r1val
) == 0) &&
169 (qemu_strtoull(r2
, NULL
, 0, &r2val
) == 0) &&
173 g_assert(e
== range_op
);
179 range
.end
= r1val
+ (r2val
- 1);
185 range
.begin
= r1val
- (r2val
- 1);
193 g_assert_not_reached();
195 g_array_append_val(debug_regions
, range
);
198 g_error("Failed to parse range in: %s", r
);
201 g_error("Bad range specifier in: %s", r
);
209 /* fflush() the log file */
210 void qemu_log_flush(void)
212 fflush(qemu_logfile
);
215 /* Close the log file */
216 void qemu_log_close(void)
219 if (qemu_logfile
!= stderr
) {
220 fclose(qemu_logfile
);
226 const QEMULogItem qemu_log_items
[] = {
227 { CPU_LOG_TB_OUT_ASM
, "out_asm",
228 "show generated host assembly code for each compiled TB" },
229 { CPU_LOG_TB_IN_ASM
, "in_asm",
230 "show target assembly code for each compiled TB" },
231 { CPU_LOG_TB_OP
, "op",
232 "show micro ops for each compiled TB" },
233 { CPU_LOG_TB_OP_OPT
, "op_opt",
234 "show micro ops (x86 only: before eflags optimization) and\n"
235 "after liveness analysis" },
236 { CPU_LOG_INT
, "int",
237 "show interrupts/exceptions in short format" },
238 { CPU_LOG_EXEC
, "exec",
239 "show trace before each executed TB (lots of logs)" },
240 { CPU_LOG_TB_CPU
, "cpu",
241 "show CPU registers before entering a TB (lots of logs)" },
242 { CPU_LOG_MMU
, "mmu",
243 "log MMU-related activities" },
244 { CPU_LOG_PCALL
, "pcall",
245 "x86 only: show protected mode far calls/returns/exceptions" },
246 { CPU_LOG_RESET
, "cpu_reset",
247 "show CPU state before CPU resets" },
248 { LOG_UNIMP
, "unimp",
249 "log unimplemented functionality" },
250 { LOG_GUEST_ERROR
, "guest_errors",
251 "log when the guest OS does something invalid (eg accessing a\n"
252 "non-existent register)" },
253 { CPU_LOG_PAGE
, "page",
254 "dump pages at beginning of user mode emulation" },
255 { CPU_LOG_TB_NOCHAIN
, "nochain",
256 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
261 static int cmp1(const char *s1
, int n
, const char *s2
)
263 if (strlen(s2
) != n
) {
266 return memcmp(s1
, s2
, n
) == 0;
269 /* takes a comma separated list of log masks. Return 0 if error. */
270 int qemu_str_to_log_mask(const char *str
)
272 const QEMULogItem
*item
;
283 if (cmp1(p
,p1
-p
,"all")) {
284 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
287 #ifdef CONFIG_TRACE_LOG
288 } else if (strncmp(p
, "trace:", 6) == 0 && p
+ 6 != p1
) {
289 trace_enable_events(p
+ 6);
293 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
294 if (cmp1(p
, p1
- p
, item
->name
)) {
310 void qemu_print_log_usage(FILE *f
)
312 const QEMULogItem
*item
;
313 fprintf(f
, "Log items (comma separated):\n");
314 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
315 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
317 #ifdef CONFIG_TRACE_LOG
318 fprintf(f
, "trace:PATTERN enable trace events\n");
319 fprintf(f
, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");