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"
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"
29 static char *logfilename;
30 static QemuMutex qemu_logfile_mutex;
31 QemuLogFile *qemu_logfile;
33 static int log_append = 0;
34 static GArray *debug_regions;
36 /* Return the number of characters emitted. */
37 int qemu_log(const char *fmt, ...)
43 logfile = atomic_rcu_read(&qemu_logfile);
47 ret = vfprintf(logfile->fd, fmt, ap);
50 /* Don't pass back error results. */
59 static void __attribute__((__constructor__)) qemu_logfile_init(void)
61 qemu_mutex_init(&qemu_logfile_mutex);
64 static void qemu_logfile_free(QemuLogFile *logfile)
68 if (logfile->fd != stderr) {
74 static bool log_uses_own_buffers;
76 /* enable or disable low levels log */
77 void qemu_set_log(int log_flags)
79 bool need_to_open_file = false;
82 qemu_loglevel = log_flags;
83 #ifdef CONFIG_TRACE_LOG
84 qemu_loglevel |= LOG_TRACE;
87 * In all cases we only log if qemu_loglevel is set.
89 * If not daemonized we will always log either to stderr
90 * or to a file (if there is a logfilename).
91 * If we are daemonized,
92 * we will only log if there is a logfilename.
94 if (qemu_loglevel && (!is_daemonized() || logfilename)) {
95 need_to_open_file = true;
97 qemu_mutex_lock(&qemu_logfile_mutex);
98 if (qemu_logfile && !need_to_open_file) {
99 logfile = qemu_logfile;
100 atomic_rcu_set(&qemu_logfile, NULL);
101 call_rcu(logfile, qemu_logfile_free, rcu);
102 } else if (!qemu_logfile && need_to_open_file) {
103 logfile = g_new0(QemuLogFile, 1);
105 logfile->fd = fopen(logfilename, log_append ? "a" : "w");
111 /* In case we are a daemon redirect stderr to logfile */
112 if (is_daemonized()) {
113 dup2(fileno(logfile->fd), STDERR_FILENO);
115 /* This will skip closing logfile in qemu_log_close() */
116 logfile->fd = stderr;
119 /* Default to stderr if no log file specified */
120 assert(!is_daemonized());
121 logfile->fd = stderr;
123 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
124 if (log_uses_own_buffers) {
125 static char logfile_buf[4096];
127 setvbuf(logfile->fd, logfile_buf, _IOLBF, sizeof(logfile_buf));
130 /* Win32 doesn't support line-buffering, so use unbuffered output. */
131 setvbuf(logfile->fd, NULL, _IONBF, 0);
133 setvbuf(logfile->fd, NULL, _IOLBF, 0);
137 atomic_rcu_set(&qemu_logfile, logfile);
139 qemu_mutex_unlock(&qemu_logfile_mutex);
142 void qemu_log_needs_buffers(void)
144 log_uses_own_buffers = true;
148 * Allow the user to include %d in their logfile which will be
149 * substituted with the current PID. This is useful for debugging many
150 * nested linux-user tasks but will result in lots of logs.
152 void qemu_set_log_filename(const char *filename, Error **errp)
158 pidstr = strstr(filename, "%");
160 /* We only accept one %d, no other format strings */
161 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
162 error_setg(errp, "Bad logfile format: %s", filename);
165 logfilename = g_strdup_printf(filename, getpid());
168 logfilename = g_strdup(filename);
171 qemu_set_log(qemu_loglevel);
174 /* Returns true if addr is in our debug filter or no filter defined
176 bool qemu_log_in_addr_range(uint64_t addr)
180 for (i = 0; i < debug_regions->len; i++) {
181 Range *range = &g_array_index(debug_regions, Range, i);
182 if (range_contains(range, addr)) {
193 void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
195 gchar **ranges = g_strsplit(filter_spec, ",", 0);
199 g_array_unref(debug_regions);
200 debug_regions = NULL;
203 debug_regions = g_array_sized_new(FALSE, FALSE,
204 sizeof(Range), g_strv_length(ranges));
205 for (i = 0; ranges[i]; i++) {
206 const char *r = ranges[i];
207 const char *range_op, *r2, *e;
208 uint64_t r1val, r2val, lob, upb;
211 range_op = strstr(r, "-");
212 r2 = range_op ? range_op + 1 : NULL;
214 range_op = strstr(r, "+");
215 r2 = range_op ? range_op + 1 : NULL;
218 range_op = strstr(r, "..");
219 r2 = range_op ? range_op + 2 : NULL;
222 error_setg(errp, "Bad range specifier");
226 if (qemu_strtou64(r, &e, 0, &r1val)
228 error_setg(errp, "Invalid number to the left of %.*s",
229 (int)(r2 - range_op), range_op);
232 if (qemu_strtou64(r2, NULL, 0, &r2val)) {
233 error_setg(errp, "Invalid number to the right of %.*s",
234 (int)(r2 - range_op), range_op);
241 upb = r1val + r2val - 1;
245 lob = r1val - (r2val - 1);
252 g_assert_not_reached();
255 error_setg(errp, "Invalid range");
258 range_set_bounds(&range, lob, upb);
259 g_array_append_val(debug_regions, range);
265 /* fflush() the log file */
266 void qemu_log_flush(void)
268 QemuLogFile *logfile;
271 logfile = atomic_rcu_read(&qemu_logfile);
278 /* Close the log file */
279 void qemu_log_close(void)
281 QemuLogFile *logfile;
283 qemu_mutex_lock(&qemu_logfile_mutex);
284 logfile = qemu_logfile;
287 atomic_rcu_set(&qemu_logfile, NULL);
288 call_rcu(logfile, qemu_logfile_free, rcu);
290 qemu_mutex_unlock(&qemu_logfile_mutex);
293 const QEMULogItem qemu_log_items[] = {
294 { CPU_LOG_TB_OUT_ASM, "out_asm",
295 "show generated host assembly code for each compiled TB" },
296 { CPU_LOG_TB_IN_ASM, "in_asm",
297 "show target assembly code for each compiled TB" },
298 { CPU_LOG_TB_OP, "op",
299 "show micro ops for each compiled TB" },
300 { CPU_LOG_TB_OP_OPT, "op_opt",
301 "show micro ops after optimization" },
302 { CPU_LOG_TB_OP_IND, "op_ind",
303 "show micro ops before indirect lowering" },
304 { CPU_LOG_INT, "int",
305 "show interrupts/exceptions in short format" },
306 { CPU_LOG_EXEC, "exec",
307 "show trace before each executed TB (lots of logs)" },
308 { CPU_LOG_TB_CPU, "cpu",
309 "show CPU registers before entering a TB (lots of logs)" },
310 { CPU_LOG_TB_FPU, "fpu",
311 "include FPU registers in the 'cpu' logging" },
312 { CPU_LOG_MMU, "mmu",
313 "log MMU-related activities" },
314 { CPU_LOG_PCALL, "pcall",
315 "x86 only: show protected mode far calls/returns/exceptions" },
316 { CPU_LOG_RESET, "cpu_reset",
317 "show CPU state before CPU resets" },
318 { LOG_UNIMP, "unimp",
319 "log unimplemented functionality" },
320 { LOG_GUEST_ERROR, "guest_errors",
321 "log when the guest OS does something invalid (eg accessing a\n"
322 "non-existent register)" },
323 { CPU_LOG_PAGE, "page",
324 "dump pages at beginning of user mode emulation" },
325 { CPU_LOG_TB_NOCHAIN, "nochain",
326 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
329 { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"},
334 /* takes a comma separated list of log masks. Return 0 if error. */
335 int qemu_str_to_log_mask(const char *str)
337 const QEMULogItem *item;
339 char **parts = g_strsplit(str, ",", 0);
342 for (tmp = parts; tmp && *tmp; tmp++) {
343 if (g_str_equal(*tmp, "all")) {
344 for (item = qemu_log_items; item->mask != 0; item++) {
347 #ifdef CONFIG_TRACE_LOG
348 } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
349 trace_enable_events((*tmp) + 6);
353 for (item = qemu_log_items; item->mask != 0; item++) {
354 if (g_str_equal(*tmp, item->name)) {
372 void qemu_print_log_usage(FILE *f)
374 const QEMULogItem *item;
375 fprintf(f, "Log items (comma separated):\n");
376 for (item = qemu_log_items; item->mask != 0; item++) {
377 fprintf(f, "%-15s %s\n", item->name, item->help);
379 #ifdef CONFIG_TRACE_LOG
380 fprintf(f, "trace:PATTERN enable trace events\n");
381 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");