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.1 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"
22 #include "qemu/range.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qemu/cutils.h"
26 #include "trace/control.h"
27 #include "qemu/thread.h"
28 #include "qemu/lockable.h"
30 static char *logfilename
;
31 static QemuMutex qemu_logfile_mutex
;
32 QemuLogFile
*qemu_logfile
;
34 static int log_append
= 0;
35 static GArray
*debug_regions
;
37 /* Return the number of characters emitted. */
38 int qemu_log(const char *fmt
, ...)
44 logfile
= qatomic_rcu_read(&qemu_logfile
);
48 ret
= vfprintf(logfile
->fd
, fmt
, ap
);
51 /* Don't pass back error results. */
60 static void __attribute__((__constructor__
)) qemu_logfile_init(void)
62 qemu_mutex_init(&qemu_logfile_mutex
);
65 static void qemu_logfile_free(QemuLogFile
*logfile
)
69 if (logfile
->fd
!= stderr
) {
75 /* enable or disable low levels log */
76 void qemu_set_log(int log_flags
)
78 bool need_to_open_file
= false;
81 qemu_loglevel
= log_flags
;
82 #ifdef CONFIG_TRACE_LOG
83 qemu_loglevel
|= LOG_TRACE
;
86 * In all cases we only log if qemu_loglevel is set.
88 * If not daemonized we will always log either to stderr
89 * or to a file (if there is a logfilename).
90 * If we are daemonized,
91 * we will only log if there is a logfilename.
93 if (qemu_loglevel
&& (!is_daemonized() || logfilename
)) {
94 need_to_open_file
= true;
96 QEMU_LOCK_GUARD(&qemu_logfile_mutex
);
97 if (qemu_logfile
&& !need_to_open_file
) {
98 logfile
= qemu_logfile
;
99 qatomic_rcu_set(&qemu_logfile
, NULL
);
100 call_rcu(logfile
, qemu_logfile_free
, rcu
);
101 } else if (!qemu_logfile
&& need_to_open_file
) {
102 logfile
= g_new0(QemuLogFile
, 1);
104 logfile
->fd
= fopen(logfilename
, log_append
? "a" : "w");
110 /* In case we are a daemon redirect stderr to logfile */
111 if (is_daemonized()) {
112 dup2(fileno(logfile
->fd
), STDERR_FILENO
);
114 /* This will skip closing logfile in qemu_log_close() */
115 logfile
->fd
= stderr
;
118 /* Default to stderr if no log file specified */
119 assert(!is_daemonized());
120 logfile
->fd
= stderr
;
124 /* Win32 doesn't support line-buffering, so use unbuffered output. */
125 setvbuf(logfile
->fd
, NULL
, _IONBF
, 0);
127 setvbuf(logfile
->fd
, NULL
, _IOLBF
, 0);
130 qatomic_rcu_set(&qemu_logfile
, logfile
);
135 * Allow the user to include %d in their logfile which will be
136 * substituted with the current PID. This is useful for debugging many
137 * nested linux-user tasks but will result in lots of logs.
139 * filename may be NULL. In that case, log output is sent to stderr
141 bool qemu_set_log_filename(const char *filename
, Error
**errp
)
147 char *pidstr
= strstr(filename
, "%");
149 /* We only accept one %d, no other format strings */
150 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
151 error_setg(errp
, "Bad logfile format: %s", filename
);
154 logfilename
= g_strdup_printf(filename
, getpid());
157 logfilename
= g_strdup(filename
);
162 qemu_set_log(qemu_loglevel
);
166 /* Returns true if addr is in our debug filter or no filter defined
168 bool qemu_log_in_addr_range(uint64_t addr
)
172 for (i
= 0; i
< debug_regions
->len
; i
++) {
173 Range
*range
= &g_array_index(debug_regions
, Range
, i
);
174 if (range_contains(range
, addr
)) {
185 void qemu_set_dfilter_ranges(const char *filter_spec
, Error
**errp
)
187 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
191 g_array_unref(debug_regions
);
192 debug_regions
= NULL
;
195 debug_regions
= g_array_sized_new(FALSE
, FALSE
,
196 sizeof(Range
), g_strv_length(ranges
));
197 for (i
= 0; ranges
[i
]; i
++) {
198 const char *r
= ranges
[i
];
199 const char *range_op
, *r2
, *e
;
200 uint64_t r1val
, r2val
, lob
, upb
;
203 range_op
= strstr(r
, "-");
204 r2
= range_op
? range_op
+ 1 : NULL
;
206 range_op
= strstr(r
, "+");
207 r2
= range_op
? range_op
+ 1 : NULL
;
210 range_op
= strstr(r
, "..");
211 r2
= range_op
? range_op
+ 2 : NULL
;
214 error_setg(errp
, "Bad range specifier");
218 if (qemu_strtou64(r
, &e
, 0, &r1val
)
220 error_setg(errp
, "Invalid number to the left of %.*s",
221 (int)(r2
- range_op
), range_op
);
224 if (qemu_strtou64(r2
, NULL
, 0, &r2val
)) {
225 error_setg(errp
, "Invalid number to the right of %.*s",
226 (int)(r2
- range_op
), range_op
);
233 upb
= r1val
+ r2val
- 1;
237 lob
= r1val
- (r2val
- 1);
244 g_assert_not_reached();
247 error_setg(errp
, "Invalid range");
250 range_set_bounds(&range
, lob
, upb
);
251 g_array_append_val(debug_regions
, range
);
257 /* fflush() the log file */
258 void qemu_log_flush(void)
260 QemuLogFile
*logfile
;
263 logfile
= qatomic_rcu_read(&qemu_logfile
);
270 /* Close the log file */
271 void qemu_log_close(void)
273 QemuLogFile
*logfile
;
275 qemu_mutex_lock(&qemu_logfile_mutex
);
276 logfile
= qemu_logfile
;
279 qatomic_rcu_set(&qemu_logfile
, NULL
);
280 call_rcu(logfile
, qemu_logfile_free
, rcu
);
282 qemu_mutex_unlock(&qemu_logfile_mutex
);
285 const QEMULogItem qemu_log_items
[] = {
286 { CPU_LOG_TB_OUT_ASM
, "out_asm",
287 "show generated host assembly code for each compiled TB" },
288 { CPU_LOG_TB_IN_ASM
, "in_asm",
289 "show target assembly code for each compiled TB" },
290 { CPU_LOG_TB_OP
, "op",
291 "show micro ops for each compiled TB" },
292 { CPU_LOG_TB_OP_OPT
, "op_opt",
293 "show micro ops after optimization" },
294 { CPU_LOG_TB_OP_IND
, "op_ind",
295 "show micro ops before indirect lowering" },
296 { CPU_LOG_INT
, "int",
297 "show interrupts/exceptions in short format" },
298 { CPU_LOG_EXEC
, "exec",
299 "show trace before each executed TB (lots of logs)" },
300 { CPU_LOG_TB_CPU
, "cpu",
301 "show CPU registers before entering a TB (lots of logs)" },
302 { CPU_LOG_TB_FPU
, "fpu",
303 "include FPU registers in the 'cpu' logging" },
304 { CPU_LOG_MMU
, "mmu",
305 "log MMU-related activities" },
306 { CPU_LOG_PCALL
, "pcall",
307 "x86 only: show protected mode far calls/returns/exceptions" },
308 { CPU_LOG_RESET
, "cpu_reset",
309 "show CPU state before CPU resets" },
310 { LOG_UNIMP
, "unimp",
311 "log unimplemented functionality" },
312 { LOG_GUEST_ERROR
, "guest_errors",
313 "log when the guest OS does something invalid (eg accessing a\n"
314 "non-existent register)" },
315 { CPU_LOG_PAGE
, "page",
316 "dump pages at beginning of user mode emulation" },
317 { CPU_LOG_TB_NOCHAIN
, "nochain",
318 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
321 { CPU_LOG_PLUGIN
, "plugin", "output from TCG plugins\n"},
323 { LOG_STRACE
, "strace",
324 "log every user-mode syscall, its input, and its result" },
328 /* takes a comma separated list of log masks. Return 0 if error. */
329 int qemu_str_to_log_mask(const char *str
)
331 const QEMULogItem
*item
;
333 char **parts
= g_strsplit(str
, ",", 0);
336 for (tmp
= parts
; tmp
&& *tmp
; tmp
++) {
337 if (g_str_equal(*tmp
, "all")) {
338 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
341 #ifdef CONFIG_TRACE_LOG
342 } else if (g_str_has_prefix(*tmp
, "trace:") && (*tmp
)[6] != '\0') {
343 trace_enable_events((*tmp
) + 6);
347 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
348 if (g_str_equal(*tmp
, item
->name
)) {
366 void qemu_print_log_usage(FILE *f
)
368 const QEMULogItem
*item
;
369 fprintf(f
, "Log items (comma separated):\n");
370 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
371 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
373 #ifdef CONFIG_TRACE_LOG
374 fprintf(f
, "trace:PATTERN enable trace events\n");
375 fprintf(f
, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");