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 /* Lock/unlock output. */
39 FILE *qemu_log_trylock(void)
44 logfile
= qatomic_rcu_read(&qemu_logfile
);
46 qemu_flockfile(logfile
->fd
);
54 void qemu_log_unlock(FILE *fd
)
62 void qemu_log(const char *fmt
, ...)
64 FILE *f
= qemu_log_trylock();
75 static void __attribute__((__constructor__
)) qemu_logfile_init(void)
77 qemu_mutex_init(&qemu_logfile_mutex
);
80 static void qemu_logfile_free(QemuLogFile
*logfile
)
84 if (logfile
->fd
!= stderr
) {
90 /* enable or disable low levels log */
91 bool qemu_set_log(int log_flags
, Error
**errp
)
93 bool need_to_open_file
= false;
96 qemu_loglevel
= log_flags
;
97 #ifdef CONFIG_TRACE_LOG
98 qemu_loglevel
|= LOG_TRACE
;
101 * In all cases we only log if qemu_loglevel is set.
103 * If not daemonized we will always log either to stderr
104 * or to a file (if there is a logfilename).
105 * If we are daemonized,
106 * we will only log if there is a logfilename.
108 if (qemu_loglevel
&& (!is_daemonized() || logfilename
)) {
109 need_to_open_file
= true;
111 QEMU_LOCK_GUARD(&qemu_logfile_mutex
);
112 if (qemu_logfile
&& !need_to_open_file
) {
113 logfile
= qemu_logfile
;
114 qatomic_rcu_set(&qemu_logfile
, NULL
);
115 call_rcu(logfile
, qemu_logfile_free
, rcu
);
116 } else if (!qemu_logfile
&& need_to_open_file
) {
117 logfile
= g_new0(QemuLogFile
, 1);
119 logfile
->fd
= fopen(logfilename
, log_append
? "a" : "w");
121 error_setg_errno(errp
, errno
, "Error opening logfile %s",
125 /* In case we are a daemon redirect stderr to logfile */
126 if (is_daemonized()) {
127 dup2(fileno(logfile
->fd
), STDERR_FILENO
);
129 /* This will skip closing logfile in qemu_log_close() */
130 logfile
->fd
= stderr
;
133 /* Default to stderr if no log file specified */
134 assert(!is_daemonized());
135 logfile
->fd
= stderr
;
139 /* Win32 doesn't support line-buffering, so use unbuffered output. */
140 setvbuf(logfile
->fd
, NULL
, _IONBF
, 0);
142 setvbuf(logfile
->fd
, NULL
, _IOLBF
, 0);
145 qatomic_rcu_set(&qemu_logfile
, logfile
);
151 * Allow the user to include %d in their logfile which will be
152 * substituted with the current PID. This is useful for debugging many
153 * nested linux-user tasks but will result in lots of logs.
155 * filename may be NULL. In that case, log output is sent to stderr
157 bool qemu_set_log_filename(const char *filename
, Error
**errp
)
163 char *pidstr
= strstr(filename
, "%");
165 /* We only accept one %d, no other format strings */
166 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
167 error_setg(errp
, "Bad logfile format: %s", filename
);
170 logfilename
= g_strdup_printf(filename
, getpid());
173 logfilename
= g_strdup(filename
);
178 return qemu_set_log(qemu_loglevel
, errp
);
181 /* Returns true if addr is in our debug filter or no filter defined
183 bool qemu_log_in_addr_range(uint64_t addr
)
187 for (i
= 0; i
< debug_regions
->len
; i
++) {
188 Range
*range
= &g_array_index(debug_regions
, Range
, i
);
189 if (range_contains(range
, addr
)) {
200 void qemu_set_dfilter_ranges(const char *filter_spec
, Error
**errp
)
202 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
206 g_array_unref(debug_regions
);
207 debug_regions
= NULL
;
210 debug_regions
= g_array_sized_new(FALSE
, FALSE
,
211 sizeof(Range
), g_strv_length(ranges
));
212 for (i
= 0; ranges
[i
]; i
++) {
213 const char *r
= ranges
[i
];
214 const char *range_op
, *r2
, *e
;
215 uint64_t r1val
, r2val
, lob
, upb
;
218 range_op
= strstr(r
, "-");
219 r2
= range_op
? range_op
+ 1 : NULL
;
221 range_op
= strstr(r
, "+");
222 r2
= range_op
? range_op
+ 1 : NULL
;
225 range_op
= strstr(r
, "..");
226 r2
= range_op
? range_op
+ 2 : NULL
;
229 error_setg(errp
, "Bad range specifier");
233 if (qemu_strtou64(r
, &e
, 0, &r1val
)
235 error_setg(errp
, "Invalid number to the left of %.*s",
236 (int)(r2
- range_op
), range_op
);
239 if (qemu_strtou64(r2
, NULL
, 0, &r2val
)) {
240 error_setg(errp
, "Invalid number to the right of %.*s",
241 (int)(r2
- range_op
), range_op
);
248 upb
= r1val
+ r2val
- 1;
252 lob
= r1val
- (r2val
- 1);
259 g_assert_not_reached();
262 error_setg(errp
, "Invalid range");
265 range_set_bounds(&range
, lob
, upb
);
266 g_array_append_val(debug_regions
, range
);
272 /* fflush() the log file */
273 void qemu_log_flush(void)
275 QemuLogFile
*logfile
;
278 logfile
= qatomic_rcu_read(&qemu_logfile
);
285 /* Close the log file */
286 void qemu_log_close(void)
288 QemuLogFile
*logfile
;
290 qemu_mutex_lock(&qemu_logfile_mutex
);
291 logfile
= qemu_logfile
;
294 qatomic_rcu_set(&qemu_logfile
, NULL
);
295 call_rcu(logfile
, qemu_logfile_free
, rcu
);
297 qemu_mutex_unlock(&qemu_logfile_mutex
);
300 const QEMULogItem qemu_log_items
[] = {
301 { CPU_LOG_TB_OUT_ASM
, "out_asm",
302 "show generated host assembly code for each compiled TB" },
303 { CPU_LOG_TB_IN_ASM
, "in_asm",
304 "show target assembly code for each compiled TB" },
305 { CPU_LOG_TB_OP
, "op",
306 "show micro ops for each compiled TB" },
307 { CPU_LOG_TB_OP_OPT
, "op_opt",
308 "show micro ops after optimization" },
309 { CPU_LOG_TB_OP_IND
, "op_ind",
310 "show micro ops before indirect lowering" },
311 { CPU_LOG_INT
, "int",
312 "show interrupts/exceptions in short format" },
313 { CPU_LOG_EXEC
, "exec",
314 "show trace before each executed TB (lots of logs)" },
315 { CPU_LOG_TB_CPU
, "cpu",
316 "show CPU registers before entering a TB (lots of logs)" },
317 { CPU_LOG_TB_FPU
, "fpu",
318 "include FPU registers in the 'cpu' logging" },
319 { CPU_LOG_MMU
, "mmu",
320 "log MMU-related activities" },
321 { CPU_LOG_PCALL
, "pcall",
322 "x86 only: show protected mode far calls/returns/exceptions" },
323 { CPU_LOG_RESET
, "cpu_reset",
324 "show CPU state before CPU resets" },
325 { LOG_UNIMP
, "unimp",
326 "log unimplemented functionality" },
327 { LOG_GUEST_ERROR
, "guest_errors",
328 "log when the guest OS does something invalid (eg accessing a\n"
329 "non-existent register)" },
330 { CPU_LOG_PAGE
, "page",
331 "dump pages at beginning of user mode emulation" },
332 { CPU_LOG_TB_NOCHAIN
, "nochain",
333 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
336 { CPU_LOG_PLUGIN
, "plugin", "output from TCG plugins\n"},
338 { LOG_STRACE
, "strace",
339 "log every user-mode syscall, its input, and its result" },
343 /* takes a comma separated list of log masks. Return 0 if error. */
344 int qemu_str_to_log_mask(const char *str
)
346 const QEMULogItem
*item
;
348 char **parts
= g_strsplit(str
, ",", 0);
351 for (tmp
= parts
; tmp
&& *tmp
; tmp
++) {
352 if (g_str_equal(*tmp
, "all")) {
353 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
356 #ifdef CONFIG_TRACE_LOG
357 } else if (g_str_has_prefix(*tmp
, "trace:") && (*tmp
)[6] != '\0') {
358 trace_enable_events((*tmp
) + 6);
362 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
363 if (g_str_equal(*tmp
, item
->name
)) {
381 void qemu_print_log_usage(FILE *f
)
383 const QEMULogItem
*item
;
384 fprintf(f
, "Log items (comma separated):\n");
385 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
386 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
388 #ifdef CONFIG_TRACE_LOG
389 fprintf(f
, "trace:PATTERN enable trace events\n");
390 fprintf(f
, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");