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 "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "trace/control.h"
29 static char *logfilename
;
32 static int log_append
= 0;
33 static GArray
*debug_regions
;
35 void qemu_log(const char *fmt
, ...)
41 vfprintf(qemu_logfile
, fmt
, ap
);
46 static bool log_uses_own_buffers
;
48 /* enable or disable low levels log */
49 void qemu_set_log(int log_flags
)
51 qemu_loglevel
= log_flags
;
52 #ifdef CONFIG_TRACE_LOG
53 qemu_loglevel
|= LOG_TRACE
;
56 (is_daemonized() ? logfilename
!= NULL
: qemu_loglevel
)) {
58 qemu_logfile
= fopen(logfilename
, log_append
? "a" : "w");
63 /* In case we are a daemon redirect stderr to logfile */
64 if (is_daemonized()) {
65 dup2(fileno(qemu_logfile
), STDERR_FILENO
);
67 /* This will skip closing logfile in qemu_log_close() */
68 qemu_logfile
= stderr
;
71 /* Default to stderr if no log file specified */
72 assert(!is_daemonized());
73 qemu_logfile
= stderr
;
75 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
76 if (log_uses_own_buffers
) {
77 static char logfile_buf
[4096];
79 setvbuf(qemu_logfile
, logfile_buf
, _IOLBF
, sizeof(logfile_buf
));
82 /* Win32 doesn't support line-buffering, so use unbuffered output. */
83 setvbuf(qemu_logfile
, NULL
, _IONBF
, 0);
85 setvbuf(qemu_logfile
, NULL
, _IOLBF
, 0);
91 (is_daemonized() ? logfilename
== NULL
: !qemu_loglevel
)) {
96 void qemu_log_needs_buffers(void)
98 log_uses_own_buffers
= true;
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.
106 void qemu_set_log_filename(const char *filename
, Error
**errp
)
111 pidstr
= strstr(filename
, "%");
113 /* We only accept one %d, no other format strings */
114 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
115 error_setg(errp
, "Bad logfile format: %s", filename
);
118 logfilename
= g_strdup_printf(filename
, getpid());
121 logfilename
= g_strdup(filename
);
124 qemu_set_log(qemu_loglevel
);
127 /* Returns true if addr is in our debug filter or no filter defined
129 bool qemu_log_in_addr_range(uint64_t addr
)
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
) {
146 void qemu_set_dfilter_ranges(const char *filter_spec
, Error
**errp
)
148 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
152 g_array_unref(debug_regions
);
153 debug_regions
= NULL
;
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
;
164 range_op
= strstr(r
, "-");
165 r2
= range_op
? range_op
+ 1 : NULL
;
167 range_op
= strstr(r
, "+");
168 r2
= range_op
? range_op
+ 1 : NULL
;
171 range_op
= strstr(r
, "..");
172 r2
= range_op
? range_op
+ 2 : NULL
;
175 error_setg(errp
, "Bad range specifier");
179 if (qemu_strtoull(r
, &e
, 0, &r1val
)
181 error_setg(errp
, "Invalid number to the left of %.*s",
182 (int)(r2
- range_op
), range_op
);
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
);
191 error_setg(errp
, "Invalid range");
198 range
.end
= r1val
+ (r2val
- 1);
202 range
.begin
= r1val
- (r2val
- 1);
209 g_assert_not_reached();
211 g_array_append_val(debug_regions
, range
);
217 /* fflush() the log file */
218 void qemu_log_flush(void)
220 fflush(qemu_logfile
);
223 /* Close the log file */
224 void qemu_log_close(void)
227 if (qemu_logfile
!= stderr
) {
228 fclose(qemu_logfile
);
234 const QEMULogItem qemu_log_items
[] = {
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",
242 "show micro ops (x86 only: before eflags optimization) and\n"
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",
249 "show CPU registers before entering a TB (lots of logs)" },
250 { CPU_LOG_MMU
, "mmu",
251 "log MMU-related activities" },
252 { CPU_LOG_PCALL
, "pcall",
253 "x86 only: show protected mode far calls/returns/exceptions" },
254 { CPU_LOG_RESET
, "cpu_reset",
255 "show CPU state before CPU resets" },
256 { LOG_UNIMP
, "unimp",
257 "log unimplemented functionality" },
258 { LOG_GUEST_ERROR
, "guest_errors",
259 "log when the guest OS does something invalid (eg accessing a\n"
260 "non-existent register)" },
261 { CPU_LOG_PAGE
, "page",
262 "dump pages at beginning of user mode emulation" },
263 { CPU_LOG_TB_NOCHAIN
, "nochain",
264 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
269 static int cmp1(const char *s1
, int n
, const char *s2
)
271 if (strlen(s2
) != n
) {
274 return memcmp(s1
, s2
, n
) == 0;
277 /* takes a comma separated list of log masks. Return 0 if error. */
278 int qemu_str_to_log_mask(const char *str
)
280 const QEMULogItem
*item
;
291 if (cmp1(p
,p1
-p
,"all")) {
292 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
295 #ifdef CONFIG_TRACE_LOG
296 } else if (strncmp(p
, "trace:", 6) == 0 && p
+ 6 != p1
) {
297 trace_enable_events(p
+ 6);
301 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
302 if (cmp1(p
, p1
- p
, item
->name
)) {
318 void qemu_print_log_usage(FILE *f
)
320 const QEMULogItem
*item
;
321 fprintf(f
, "Log items (comma separated):\n");
322 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
323 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
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");