log: Plug memory leak on multiple -dfilter
[qemu/ar7.git] / util / log.c
blob6f45e0a26c6c549d0bef8ce3df5e1196019e19e4
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 static bool log_uses_own_buffers;
47 /* enable or disable low levels log */
48 void qemu_set_log(int log_flags)
50 qemu_loglevel = log_flags;
51 #ifdef CONFIG_TRACE_LOG
52 qemu_loglevel |= LOG_TRACE;
53 #endif
54 if (!qemu_logfile &&
55 (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
56 if (logfilename) {
57 qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
58 if (!qemu_logfile) {
59 perror(logfilename);
60 _exit(1);
62 /* In case we are a daemon redirect stderr to logfile */
63 if (is_daemonized()) {
64 dup2(fileno(qemu_logfile), STDERR_FILENO);
65 fclose(qemu_logfile);
66 /* This will skip closing logfile in qemu_log_close() */
67 qemu_logfile = stderr;
69 } else {
70 /* Default to stderr if no log file specified */
71 assert(!is_daemonized());
72 qemu_logfile = stderr;
74 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
75 if (log_uses_own_buffers) {
76 static char logfile_buf[4096];
78 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
79 } else {
80 #if defined(_WIN32)
81 /* Win32 doesn't support line-buffering, so use unbuffered output. */
82 setvbuf(qemu_logfile, NULL, _IONBF, 0);
83 #else
84 setvbuf(qemu_logfile, NULL, _IOLBF, 0);
85 #endif
86 log_append = 1;
89 if (qemu_logfile &&
90 (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
91 qemu_log_close();
95 void qemu_log_needs_buffers(void)
97 log_uses_own_buffers = true;
101 * Allow the user to include %d in their logfile which will be
102 * substituted with the current PID. This is useful for debugging many
103 * nested linux-user tasks but will result in lots of logs.
105 void qemu_set_log_filename(const char *filename)
107 char *pidstr;
108 g_free(logfilename);
110 pidstr = strstr(filename, "%");
111 if (pidstr) {
112 /* We only accept one %d, no other format strings */
113 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
114 error_report("Bad logfile format: %s", filename);
115 logfilename = NULL;
116 } else {
117 logfilename = g_strdup_printf(filename, getpid());
119 } else {
120 logfilename = g_strdup(filename);
122 qemu_log_close();
123 qemu_set_log(qemu_loglevel);
126 /* Returns true if addr is in our debug filter or no filter defined
128 bool qemu_log_in_addr_range(uint64_t addr)
130 if (debug_regions) {
131 int i = 0;
132 for (i = 0; i < debug_regions->len; i++) {
133 struct Range *range = &g_array_index(debug_regions, Range, i);
134 if (addr >= range->begin && addr <= range->end) {
135 return true;
138 return false;
139 } else {
140 return true;
145 void qemu_set_dfilter_ranges(const char *filter_spec)
147 gchar **ranges = g_strsplit(filter_spec, ",", 0);
149 if (debug_regions) {
150 g_array_unref(debug_regions);
151 debug_regions = NULL;
154 if (ranges) {
155 gchar **next = ranges;
156 gchar *r = *next++;
158 debug_regions = g_array_sized_new(FALSE, FALSE,
159 sizeof(Range), g_strv_length(ranges));
160 while (r) {
161 char *range_op = strstr(r, "-");
162 char *r2 = range_op ? range_op + 1 : NULL;
163 if (!range_op) {
164 range_op = strstr(r, "+");
165 r2 = range_op ? range_op + 1 : NULL;
167 if (!range_op) {
168 range_op = strstr(r, "..");
169 r2 = range_op ? range_op + 2 : NULL;
171 if (range_op) {
172 const char *e = NULL;
173 uint64_t r1val, r2val;
175 if ((qemu_strtoull(r, &e, 0, &r1val) == 0) &&
176 (qemu_strtoull(r2, NULL, 0, &r2val) == 0) &&
177 r2val > 0) {
178 struct Range range;
180 g_assert(e == range_op);
182 switch (*range_op) {
183 case '+':
185 range.begin = r1val;
186 range.end = r1val + (r2val - 1);
187 break;
189 case '-':
191 range.end = r1val;
192 range.begin = r1val - (r2val - 1);
193 break;
195 case '.':
196 range.begin = r1val;
197 range.end = r2val;
198 break;
199 default:
200 g_assert_not_reached();
202 g_array_append_val(debug_regions, range);
204 } else {
205 g_error("Failed to parse range in: %s", r);
207 } else {
208 g_error("Bad range specifier in: %s", r);
210 r = *next++;
212 g_strfreev(ranges);
216 /* fflush() the log file */
217 void qemu_log_flush(void)
219 fflush(qemu_logfile);
222 /* Close the log file */
223 void qemu_log_close(void)
225 if (qemu_logfile) {
226 if (qemu_logfile != stderr) {
227 fclose(qemu_logfile);
229 qemu_logfile = NULL;
233 const QEMULogItem qemu_log_items[] = {
234 { CPU_LOG_TB_OUT_ASM, "out_asm",
235 "show generated host assembly code for each compiled TB" },
236 { CPU_LOG_TB_IN_ASM, "in_asm",
237 "show target assembly code for each compiled TB" },
238 { CPU_LOG_TB_OP, "op",
239 "show micro ops for each compiled TB" },
240 { CPU_LOG_TB_OP_OPT, "op_opt",
241 "show micro ops (x86 only: before eflags optimization) and\n"
242 "after liveness analysis" },
243 { CPU_LOG_INT, "int",
244 "show interrupts/exceptions in short format" },
245 { CPU_LOG_EXEC, "exec",
246 "show trace before each executed TB (lots of logs)" },
247 { CPU_LOG_TB_CPU, "cpu",
248 "show CPU registers before entering a TB (lots of logs)" },
249 { CPU_LOG_MMU, "mmu",
250 "log MMU-related activities" },
251 { CPU_LOG_PCALL, "pcall",
252 "x86 only: show protected mode far calls/returns/exceptions" },
253 { CPU_LOG_RESET, "cpu_reset",
254 "show CPU state before CPU resets" },
255 { LOG_UNIMP, "unimp",
256 "log unimplemented functionality" },
257 { LOG_GUEST_ERROR, "guest_errors",
258 "log when the guest OS does something invalid (eg accessing a\n"
259 "non-existent register)" },
260 { CPU_LOG_PAGE, "page",
261 "dump pages at beginning of user mode emulation" },
262 { CPU_LOG_TB_NOCHAIN, "nochain",
263 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
264 "complete traces" },
265 { 0, NULL, NULL },
268 static int cmp1(const char *s1, int n, const char *s2)
270 if (strlen(s2) != n) {
271 return 0;
273 return memcmp(s1, s2, n) == 0;
276 /* takes a comma separated list of log masks. Return 0 if error. */
277 int qemu_str_to_log_mask(const char *str)
279 const QEMULogItem *item;
280 int mask;
281 const char *p, *p1;
283 p = str;
284 mask = 0;
285 for (;;) {
286 p1 = strchr(p, ',');
287 if (!p1) {
288 p1 = p + strlen(p);
290 if (cmp1(p,p1-p,"all")) {
291 for (item = qemu_log_items; item->mask != 0; item++) {
292 mask |= item->mask;
294 #ifdef CONFIG_TRACE_LOG
295 } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
296 trace_enable_events(p + 6);
297 mask |= LOG_TRACE;
298 #endif
299 } else {
300 for (item = qemu_log_items; item->mask != 0; item++) {
301 if (cmp1(p, p1 - p, item->name)) {
302 goto found;
305 return 0;
306 found:
307 mask |= item->mask;
309 if (*p1 != ',') {
310 break;
312 p = p1 + 1;
314 return mask;
317 void qemu_print_log_usage(FILE *f)
319 const QEMULogItem *item;
320 fprintf(f, "Log items (comma separated):\n");
321 for (item = qemu_log_items; item->mask != 0; item++) {
322 fprintf(f, "%-15s %s\n", item->name, item->help);
324 #ifdef CONFIG_TRACE_LOG
325 fprintf(f, "trace:PATTERN enable trace events\n");
326 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
327 #endif