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"
31 #include <sys/syscall.h>
35 typedef struct RCUCloseFILE
{
40 /* Mutex covering the other global_* variables. */
41 static QemuMutex global_mutex
;
42 static char *global_filename
;
43 static FILE *global_file
;
44 static __thread
FILE *thread_file
;
47 static bool log_append
;
48 static bool log_per_thread
;
49 static GArray
*debug_regions
;
51 /* Returns true if qemu_log() will really write somewhere. */
52 bool qemu_log_enabled(void)
54 return log_per_thread
|| qatomic_read(&global_file
) != NULL
;
57 /* Returns true if qemu_log() will write somewhere other than stderr. */
58 bool qemu_log_separate(void)
63 FILE *logfile
= qatomic_read(&global_file
);
64 return logfile
&& logfile
!= stderr
;
68 static int log_thread_id(void)
72 #elif defined(SYS_gettid)
73 return syscall(SYS_gettid
);
76 return qatomic_fetch_inc(&counter
);
80 /* Lock/unlock output. */
82 FILE *qemu_log_trylock(void)
86 logfile
= thread_file
;
89 g_autofree
char *filename
90 = g_strdup_printf(global_filename
, log_thread_id());
91 logfile
= fopen(filename
, "w");
95 thread_file
= logfile
;
99 * FIXME: typeof_strip_qual, as used by qatomic_rcu_read,
100 * does not work with pointers to undefined structures,
101 * such as we have with struct _IO_FILE and musl libc.
102 * Since all we want is a read of a pointer, cast to void**,
103 * which does work with typeof_strip_qual.
105 logfile
= qatomic_rcu_read((void **)&global_file
);
113 qemu_flockfile(logfile
);
117 void qemu_log_unlock(FILE *logfile
)
121 qemu_funlockfile(logfile
);
122 if (!log_per_thread
) {
128 void qemu_log(const char *fmt
, ...)
130 FILE *f
= qemu_log_trylock();
135 vfprintf(f
, fmt
, ap
);
141 static void __attribute__((__constructor__
)) startup(void)
143 qemu_mutex_init(&global_mutex
);
146 static void rcu_close_file(RCUCloseFILE
*r
)
153 * valid_filename_template:
155 * Validate the filename template. Require %d if per_thread, allow it
156 * otherwise; require no other % within the template.
164 } ValidFilenameTemplateResult
;
166 static ValidFilenameTemplateResult
167 valid_filename_template(const char *filename
, bool per_thread
, Error
**errp
)
170 char *pidstr
= strstr(filename
, "%");
173 /* We only accept one %d, no other format strings */
174 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
175 error_setg(errp
, "Bad logfile template: %s", filename
);
178 return per_thread
? vft_strdup
: vft_pid_printf
;
182 error_setg(errp
, "Filename template with '%%d' required for 'tid'");
185 return filename
? vft_strdup
: vft_stderr
;
188 /* enable or disable low levels log */
189 static bool qemu_set_log_internal(const char *filename
, bool changed_name
,
190 int log_flags
, Error
**errp
)
192 bool need_to_open_file
;
197 QEMU_LOCK_GUARD(&global_mutex
);
198 logfile
= global_file
;
200 per_thread
= log_flags
& LOG_PER_THREAD
;
203 char *newname
= NULL
;
206 * Once threads start opening their own log files, we have no
207 * easy mechanism to tell them all to close and re-open.
208 * There seems little cause to do so either -- this option
209 * will most often be used at user-only startup.
211 if (log_per_thread
) {
212 error_setg(errp
, "Cannot change log filename after setting 'tid'");
216 switch (valid_filename_template(filename
, per_thread
, errp
)) {
222 newname
= g_strdup(filename
);
225 newname
= g_strdup_printf(filename
, getpid());
229 g_free(global_filename
);
230 global_filename
= newname
;
233 filename
= global_filename
;
235 valid_filename_template(filename
, true, errp
) == vft_error
) {
240 /* Once the per-thread flag is set, it cannot be unset. */
242 log_per_thread
= true;
244 /* The flag itself is not relevant for need_to_open_file. */
245 log_flags
&= ~LOG_PER_THREAD
;
246 #ifdef CONFIG_TRACE_LOG
247 log_flags
|= LOG_TRACE
;
249 qemu_loglevel
= log_flags
;
252 * In all cases we only log if qemu_loglevel is set.
254 * If per-thread, open the file for each thread in qemu_log_lock.
255 * If not daemonized we will always log either to stderr
256 * or to a file (if there is a filename).
257 * If we are daemonized, we will only log if there is a filename.
259 daemonized
= is_daemonized();
260 need_to_open_file
= log_flags
&& !per_thread
&& (!daemonized
|| filename
);
262 if (logfile
&& (!need_to_open_file
|| changed_name
)) {
263 qatomic_rcu_set(&global_file
, NULL
);
264 if (logfile
!= stderr
) {
265 RCUCloseFILE
*r
= g_new0(RCUCloseFILE
, 1);
267 call_rcu(r
, rcu_close_file
, rcu
);
272 if (!logfile
&& need_to_open_file
) {
274 logfile
= fopen(filename
, log_append
? "a" : "w");
276 error_setg_errno(errp
, errno
, "Error opening logfile %s",
280 /* In case we are a daemon redirect stderr to logfile */
282 dup2(fileno(logfile
), STDERR_FILENO
);
284 /* This will skip closing logfile in rcu_close_file. */
288 /* Default to stderr if no log file specified */
295 qatomic_rcu_set(&global_file
, logfile
);
300 bool qemu_set_log(int log_flags
, Error
**errp
)
302 return qemu_set_log_internal(NULL
, false, log_flags
, errp
);
305 bool qemu_set_log_filename(const char *filename
, Error
**errp
)
307 return qemu_set_log_internal(filename
, true, qemu_loglevel
, errp
);
310 bool qemu_set_log_filename_flags(const char *name
, int flags
, Error
**errp
)
312 return qemu_set_log_internal(name
, true, flags
, errp
);
315 /* Returns true if addr is in our debug filter or no filter defined
317 bool qemu_log_in_addr_range(uint64_t addr
)
321 for (i
= 0; i
< debug_regions
->len
; i
++) {
322 Range
*range
= &g_array_index(debug_regions
, Range
, i
);
323 if (range_contains(range
, addr
)) {
334 void qemu_set_dfilter_ranges(const char *filter_spec
, Error
**errp
)
336 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
340 g_array_unref(debug_regions
);
341 debug_regions
= NULL
;
344 debug_regions
= g_array_sized_new(FALSE
, FALSE
,
345 sizeof(Range
), g_strv_length(ranges
));
346 for (i
= 0; ranges
[i
]; i
++) {
347 const char *r
= ranges
[i
];
348 const char *range_op
, *r2
, *e
;
349 uint64_t r1val
, r2val
, lob
, upb
;
352 range_op
= strstr(r
, "-");
353 r2
= range_op
? range_op
+ 1 : NULL
;
355 range_op
= strstr(r
, "+");
356 r2
= range_op
? range_op
+ 1 : NULL
;
359 range_op
= strstr(r
, "..");
360 r2
= range_op
? range_op
+ 2 : NULL
;
363 error_setg(errp
, "Bad range specifier");
367 if (qemu_strtou64(r
, &e
, 0, &r1val
)
369 error_setg(errp
, "Invalid number to the left of %.*s",
370 (int)(r2
- range_op
), range_op
);
373 if (qemu_strtou64(r2
, NULL
, 0, &r2val
)) {
374 error_setg(errp
, "Invalid number to the right of %.*s",
375 (int)(r2
- range_op
), range_op
);
382 upb
= r1val
+ r2val
- 1;
386 lob
= r1val
- (r2val
- 1);
393 g_assert_not_reached();
396 error_setg(errp
, "Invalid range");
399 range_set_bounds(&range
, lob
, upb
);
400 g_array_append_val(debug_regions
, range
);
406 const QEMULogItem qemu_log_items
[] = {
407 { CPU_LOG_TB_OUT_ASM
, "out_asm",
408 "show generated host assembly code for each compiled TB" },
409 { CPU_LOG_TB_IN_ASM
, "in_asm",
410 "show target assembly code for each compiled TB" },
411 { CPU_LOG_TB_OP
, "op",
412 "show micro ops for each compiled TB" },
413 { CPU_LOG_TB_OP_OPT
, "op_opt",
414 "show micro ops after optimization" },
415 { CPU_LOG_TB_OP_IND
, "op_ind",
416 "show micro ops before indirect lowering" },
417 { CPU_LOG_INT
, "int",
418 "show interrupts/exceptions in short format" },
419 { CPU_LOG_EXEC
, "exec",
420 "show trace before each executed TB (lots of logs)" },
421 { CPU_LOG_TB_CPU
, "cpu",
422 "show CPU registers before entering a TB (lots of logs)" },
423 { CPU_LOG_TB_FPU
, "fpu",
424 "include FPU registers in the 'cpu' logging" },
425 { CPU_LOG_MMU
, "mmu",
426 "log MMU-related activities" },
427 { CPU_LOG_PCALL
, "pcall",
428 "x86 only: show protected mode far calls/returns/exceptions" },
429 { CPU_LOG_RESET
, "cpu_reset",
430 "show CPU state before CPU resets" },
431 { LOG_UNIMP
, "unimp",
432 "log unimplemented functionality" },
433 { LOG_GUEST_ERROR
, "guest_errors",
434 "log when the guest OS does something invalid (eg accessing a\n"
435 "non-existent register)" },
436 { CPU_LOG_PAGE
, "page",
437 "dump pages at beginning of user mode emulation" },
438 { CPU_LOG_TB_NOCHAIN
, "nochain",
439 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
442 { CPU_LOG_PLUGIN
, "plugin", "output from TCG plugins\n"},
444 { LOG_STRACE
, "strace",
445 "log every user-mode syscall, its input, and its result" },
446 { LOG_PER_THREAD
, "tid",
447 "open a separate log file per thread; filename must contain '%d'" },
451 /* takes a comma separated list of log masks. Return 0 if error. */
452 int qemu_str_to_log_mask(const char *str
)
454 const QEMULogItem
*item
;
456 char **parts
= g_strsplit(str
, ",", 0);
459 for (tmp
= parts
; tmp
&& *tmp
; tmp
++) {
460 if (g_str_equal(*tmp
, "all")) {
461 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
464 #ifdef CONFIG_TRACE_LOG
465 } else if (g_str_has_prefix(*tmp
, "trace:") && (*tmp
)[6] != '\0') {
466 trace_enable_events((*tmp
) + 6);
470 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
471 if (g_str_equal(*tmp
, item
->name
)) {
489 void qemu_print_log_usage(FILE *f
)
491 const QEMULogItem
*item
;
492 fprintf(f
, "Log items (comma separated):\n");
493 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
494 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
496 #ifdef CONFIG_TRACE_LOG
497 fprintf(f
, "trace:PATTERN enable trace events\n");
498 fprintf(f
, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");