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
)
63 void qemu_log(const char *fmt
, ...)
65 FILE *f
= qemu_log_trylock();
76 static void __attribute__((__constructor__
)) qemu_logfile_init(void)
78 qemu_mutex_init(&qemu_logfile_mutex
);
81 static void qemu_logfile_free(QemuLogFile
*logfile
)
85 if (logfile
->fd
!= stderr
) {
91 /* enable or disable low levels log */
92 bool qemu_set_log(int log_flags
, Error
**errp
)
94 bool need_to_open_file
= false;
97 qemu_loglevel
= log_flags
;
98 #ifdef CONFIG_TRACE_LOG
99 qemu_loglevel
|= LOG_TRACE
;
102 * In all cases we only log if qemu_loglevel is set.
104 * If not daemonized we will always log either to stderr
105 * or to a file (if there is a logfilename).
106 * If we are daemonized,
107 * we will only log if there is a logfilename.
109 if (qemu_loglevel
&& (!is_daemonized() || logfilename
)) {
110 need_to_open_file
= true;
112 QEMU_LOCK_GUARD(&qemu_logfile_mutex
);
113 if (qemu_logfile
&& !need_to_open_file
) {
114 logfile
= qemu_logfile
;
115 qatomic_rcu_set(&qemu_logfile
, NULL
);
116 call_rcu(logfile
, qemu_logfile_free
, rcu
);
117 } else if (!qemu_logfile
&& need_to_open_file
) {
118 logfile
= g_new0(QemuLogFile
, 1);
120 logfile
->fd
= fopen(logfilename
, log_append
? "a" : "w");
122 error_setg_errno(errp
, errno
, "Error opening logfile %s",
126 /* In case we are a daemon redirect stderr to logfile */
127 if (is_daemonized()) {
128 dup2(fileno(logfile
->fd
), STDERR_FILENO
);
130 /* This will skip closing logfile in qemu_log_close() */
131 logfile
->fd
= stderr
;
134 /* Default to stderr if no log file specified */
135 assert(!is_daemonized());
136 logfile
->fd
= stderr
;
140 /* Win32 doesn't support line-buffering, so use unbuffered output. */
141 setvbuf(logfile
->fd
, NULL
, _IONBF
, 0);
143 setvbuf(logfile
->fd
, NULL
, _IOLBF
, 0);
146 qatomic_rcu_set(&qemu_logfile
, logfile
);
152 * Allow the user to include %d in their logfile which will be
153 * substituted with the current PID. This is useful for debugging many
154 * nested linux-user tasks but will result in lots of logs.
156 * filename may be NULL. In that case, log output is sent to stderr
158 bool qemu_set_log_filename(const char *filename
, Error
**errp
)
164 char *pidstr
= strstr(filename
, "%");
166 /* We only accept one %d, no other format strings */
167 if (pidstr
[1] != 'd' || strchr(pidstr
+ 2, '%')) {
168 error_setg(errp
, "Bad logfile format: %s", filename
);
171 logfilename
= g_strdup_printf(filename
, getpid());
174 logfilename
= g_strdup(filename
);
179 return qemu_set_log(qemu_loglevel
, errp
);
182 /* Returns true if addr is in our debug filter or no filter defined
184 bool qemu_log_in_addr_range(uint64_t addr
)
188 for (i
= 0; i
< debug_regions
->len
; i
++) {
189 Range
*range
= &g_array_index(debug_regions
, Range
, i
);
190 if (range_contains(range
, addr
)) {
201 void qemu_set_dfilter_ranges(const char *filter_spec
, Error
**errp
)
203 gchar
**ranges
= g_strsplit(filter_spec
, ",", 0);
207 g_array_unref(debug_regions
);
208 debug_regions
= NULL
;
211 debug_regions
= g_array_sized_new(FALSE
, FALSE
,
212 sizeof(Range
), g_strv_length(ranges
));
213 for (i
= 0; ranges
[i
]; i
++) {
214 const char *r
= ranges
[i
];
215 const char *range_op
, *r2
, *e
;
216 uint64_t r1val
, r2val
, lob
, upb
;
219 range_op
= strstr(r
, "-");
220 r2
= range_op
? range_op
+ 1 : NULL
;
222 range_op
= strstr(r
, "+");
223 r2
= range_op
? range_op
+ 1 : NULL
;
226 range_op
= strstr(r
, "..");
227 r2
= range_op
? range_op
+ 2 : NULL
;
230 error_setg(errp
, "Bad range specifier");
234 if (qemu_strtou64(r
, &e
, 0, &r1val
)
236 error_setg(errp
, "Invalid number to the left of %.*s",
237 (int)(r2
- range_op
), range_op
);
240 if (qemu_strtou64(r2
, NULL
, 0, &r2val
)) {
241 error_setg(errp
, "Invalid number to the right of %.*s",
242 (int)(r2
- range_op
), range_op
);
249 upb
= r1val
+ r2val
- 1;
253 lob
= r1val
- (r2val
- 1);
260 g_assert_not_reached();
263 error_setg(errp
, "Invalid range");
266 range_set_bounds(&range
, lob
, upb
);
267 g_array_append_val(debug_regions
, range
);
273 /* Close the log file */
274 void qemu_log_close(void)
276 QemuLogFile
*logfile
;
278 qemu_mutex_lock(&qemu_logfile_mutex
);
279 logfile
= qemu_logfile
;
282 qatomic_rcu_set(&qemu_logfile
, NULL
);
283 call_rcu(logfile
, qemu_logfile_free
, rcu
);
285 qemu_mutex_unlock(&qemu_logfile_mutex
);
288 const QEMULogItem qemu_log_items
[] = {
289 { CPU_LOG_TB_OUT_ASM
, "out_asm",
290 "show generated host assembly code for each compiled TB" },
291 { CPU_LOG_TB_IN_ASM
, "in_asm",
292 "show target assembly code for each compiled TB" },
293 { CPU_LOG_TB_OP
, "op",
294 "show micro ops for each compiled TB" },
295 { CPU_LOG_TB_OP_OPT
, "op_opt",
296 "show micro ops after optimization" },
297 { CPU_LOG_TB_OP_IND
, "op_ind",
298 "show micro ops before indirect lowering" },
299 { CPU_LOG_INT
, "int",
300 "show interrupts/exceptions in short format" },
301 { CPU_LOG_EXEC
, "exec",
302 "show trace before each executed TB (lots of logs)" },
303 { CPU_LOG_TB_CPU
, "cpu",
304 "show CPU registers before entering a TB (lots of logs)" },
305 { CPU_LOG_TB_FPU
, "fpu",
306 "include FPU registers in the 'cpu' logging" },
307 { CPU_LOG_MMU
, "mmu",
308 "log MMU-related activities" },
309 { CPU_LOG_PCALL
, "pcall",
310 "x86 only: show protected mode far calls/returns/exceptions" },
311 { CPU_LOG_RESET
, "cpu_reset",
312 "show CPU state before CPU resets" },
313 { LOG_UNIMP
, "unimp",
314 "log unimplemented functionality" },
315 { LOG_GUEST_ERROR
, "guest_errors",
316 "log when the guest OS does something invalid (eg accessing a\n"
317 "non-existent register)" },
318 { CPU_LOG_PAGE
, "page",
319 "dump pages at beginning of user mode emulation" },
320 { CPU_LOG_TB_NOCHAIN
, "nochain",
321 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
324 { CPU_LOG_PLUGIN
, "plugin", "output from TCG plugins\n"},
326 { LOG_STRACE
, "strace",
327 "log every user-mode syscall, its input, and its result" },
331 /* takes a comma separated list of log masks. Return 0 if error. */
332 int qemu_str_to_log_mask(const char *str
)
334 const QEMULogItem
*item
;
336 char **parts
= g_strsplit(str
, ",", 0);
339 for (tmp
= parts
; tmp
&& *tmp
; tmp
++) {
340 if (g_str_equal(*tmp
, "all")) {
341 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
344 #ifdef CONFIG_TRACE_LOG
345 } else if (g_str_has_prefix(*tmp
, "trace:") && (*tmp
)[6] != '\0') {
346 trace_enable_events((*tmp
) + 6);
350 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
351 if (g_str_equal(*tmp
, item
->name
)) {
369 void qemu_print_log_usage(FILE *f
)
371 const QEMULogItem
*item
;
372 fprintf(f
, "Log items (comma separated):\n");
373 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
374 fprintf(f
, "%-15s %s\n", item
->name
, item
->help
);
376 #ifdef CONFIG_TRACE_LOG
377 fprintf(f
, "trace:PATTERN enable trace events\n");
378 fprintf(f
, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");