net/filter-mirror:Add filter-mirror
[qemu/ar7.git] / util / log.c
blobb219081e91f02c569ad220b906a2a818f04c0510
1 /*
2 * Logging support
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"
21 #include "qemu-common.h"
22 #include "qemu/log.h"
23 #include "qemu/range.h"
24 #include "qemu/error-report.h"
25 #include "qemu/cutils.h"
26 #include "trace/control.h"
28 static char *logfilename;
29 FILE *qemu_logfile;
30 int qemu_loglevel;
31 static int log_append = 0;
32 static GArray *debug_regions;
34 void qemu_log(const char *fmt, ...)
36 va_list ap;
38 va_start(ap, fmt);
39 if (qemu_logfile) {
40 vfprintf(qemu_logfile, fmt, ap);
42 va_end(ap);
45 /* enable or disable low levels log */
46 void do_qemu_set_log(int log_flags, bool use_own_buffers)
48 qemu_loglevel = log_flags;
49 #ifdef CONFIG_TRACE_LOG
50 qemu_loglevel |= LOG_TRACE;
51 #endif
52 if (!qemu_logfile &&
53 (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
54 if (logfilename) {
55 qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
56 if (!qemu_logfile) {
57 perror(logfilename);
58 _exit(1);
60 /* In case we are a daemon redirect stderr to logfile */
61 if (is_daemonized()) {
62 dup2(fileno(qemu_logfile), STDERR_FILENO);
63 fclose(qemu_logfile);
64 /* This will skip closing logfile in qemu_log_close() */
65 qemu_logfile = stderr;
67 } else {
68 /* Default to stderr if no log file specified */
69 assert(!is_daemonized());
70 qemu_logfile = stderr;
72 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
73 if (use_own_buffers) {
74 static char logfile_buf[4096];
76 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
77 } else {
78 #if defined(_WIN32)
79 /* Win32 doesn't support line-buffering, so use unbuffered output. */
80 setvbuf(qemu_logfile, NULL, _IONBF, 0);
81 #else
82 setvbuf(qemu_logfile, NULL, _IOLBF, 0);
83 #endif
84 log_append = 1;
87 if (qemu_logfile &&
88 (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
89 qemu_log_close();
93 * Allow the user to include %d in their logfile which will be
94 * substituted with the current PID. This is useful for debugging many
95 * nested linux-user tasks but will result in lots of logs.
97 void qemu_set_log_filename(const char *filename)
99 char *pidstr;
100 g_free(logfilename);
102 pidstr = strstr(filename, "%");
103 if (pidstr) {
104 /* We only accept one %d, no other format strings */
105 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
106 error_report("Bad logfile format: %s", filename);
107 logfilename = NULL;
108 } else {
109 logfilename = g_strdup_printf(filename, getpid());
111 } else {
112 logfilename = g_strdup(filename);
114 qemu_log_close();
115 qemu_set_log(qemu_loglevel);
118 /* Returns true if addr is in our debug filter or no filter defined
120 bool qemu_log_in_addr_range(uint64_t addr)
122 if (debug_regions) {
123 int i = 0;
124 for (i = 0; i < debug_regions->len; i++) {
125 struct Range *range = &g_array_index(debug_regions, Range, i);
126 if (addr >= range->begin && addr <= range->end) {
127 return true;
130 return false;
131 } else {
132 return true;
137 void qemu_set_dfilter_ranges(const char *filter_spec)
139 gchar **ranges = g_strsplit(filter_spec, ",", 0);
140 if (ranges) {
141 gchar **next = ranges;
142 gchar *r = *next++;
143 debug_regions = g_array_sized_new(FALSE, FALSE,
144 sizeof(Range), g_strv_length(ranges));
145 while (r) {
146 char *range_op = strstr(r, "-");
147 char *r2 = range_op ? range_op + 1 : NULL;
148 if (!range_op) {
149 range_op = strstr(r, "+");
150 r2 = range_op ? range_op + 1 : NULL;
152 if (!range_op) {
153 range_op = strstr(r, "..");
154 r2 = range_op ? range_op + 2 : NULL;
156 if (range_op) {
157 const char *e = NULL;
158 uint64_t r1val, r2val;
160 if ((qemu_strtoull(r, &e, 0, &r1val) == 0) &&
161 (qemu_strtoull(r2, NULL, 0, &r2val) == 0) &&
162 r2val > 0) {
163 struct Range range;
165 g_assert(e == range_op);
167 switch (*range_op) {
168 case '+':
170 range.begin = r1val;
171 range.end = r1val + (r2val - 1);
172 break;
174 case '-':
176 range.end = r1val;
177 range.begin = r1val - (r2val - 1);
178 break;
180 case '.':
181 range.begin = r1val;
182 range.end = r2val;
183 break;
184 default:
185 g_assert_not_reached();
187 g_array_append_val(debug_regions, range);
189 } else {
190 g_error("Failed to parse range in: %s", r);
192 } else {
193 g_error("Bad range specifier in: %s", r);
195 r = *next++;
197 g_strfreev(ranges);
201 const QEMULogItem qemu_log_items[] = {
202 { CPU_LOG_TB_OUT_ASM, "out_asm",
203 "show generated host assembly code for each compiled TB" },
204 { CPU_LOG_TB_IN_ASM, "in_asm",
205 "show target assembly code for each compiled TB" },
206 { CPU_LOG_TB_OP, "op",
207 "show micro ops for each compiled TB" },
208 { CPU_LOG_TB_OP_OPT, "op_opt",
209 "show micro ops (x86 only: before eflags optimization) and\n"
210 "after liveness analysis" },
211 { CPU_LOG_INT, "int",
212 "show interrupts/exceptions in short format" },
213 { CPU_LOG_EXEC, "exec",
214 "show trace before each executed TB (lots of logs)" },
215 { CPU_LOG_TB_CPU, "cpu",
216 "show CPU registers before entering a TB (lots of logs)" },
217 { CPU_LOG_MMU, "mmu",
218 "log MMU-related activities" },
219 { CPU_LOG_PCALL, "pcall",
220 "x86 only: show protected mode far calls/returns/exceptions" },
221 { CPU_LOG_RESET, "cpu_reset",
222 "show CPU state before CPU resets" },
223 { LOG_UNIMP, "unimp",
224 "log unimplemented functionality" },
225 { LOG_GUEST_ERROR, "guest_errors",
226 "log when the guest OS does something invalid (eg accessing a\n"
227 "non-existent register)" },
228 { CPU_LOG_PAGE, "page",
229 "dump pages at beginning of user mode emulation" },
230 { CPU_LOG_TB_NOCHAIN, "nochain",
231 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
232 "complete traces" },
233 { 0, NULL, NULL },
236 static int cmp1(const char *s1, int n, const char *s2)
238 if (strlen(s2) != n) {
239 return 0;
241 return memcmp(s1, s2, n) == 0;
244 /* takes a comma separated list of log masks. Return 0 if error. */
245 int qemu_str_to_log_mask(const char *str)
247 const QEMULogItem *item;
248 int mask;
249 const char *p, *p1;
251 p = str;
252 mask = 0;
253 for (;;) {
254 p1 = strchr(p, ',');
255 if (!p1) {
256 p1 = p + strlen(p);
258 if (cmp1(p,p1-p,"all")) {
259 for (item = qemu_log_items; item->mask != 0; item++) {
260 mask |= item->mask;
262 #ifdef CONFIG_TRACE_LOG
263 } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
264 trace_enable_events(p + 6);
265 mask |= LOG_TRACE;
266 #endif
267 } else {
268 for (item = qemu_log_items; item->mask != 0; item++) {
269 if (cmp1(p, p1 - p, item->name)) {
270 goto found;
273 return 0;
274 found:
275 mask |= item->mask;
277 if (*p1 != ',') {
278 break;
280 p = p1 + 1;
282 return mask;
285 void qemu_print_log_usage(FILE *f)
287 const QEMULogItem *item;
288 fprintf(f, "Log items (comma separated):\n");
289 for (item = qemu_log_items; item->mask != 0; item++) {
290 fprintf(f, "%-15s %s\n", item->name, item->help);
292 #ifdef CONFIG_TRACE_LOG
293 fprintf(f, "trace:PATTERN enable trace events\n");
294 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
295 #endif