xref: /qemu/util/log.c (revision 69b205bb)
1 /*
2  * Logging support
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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 "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "trace/control.h"
28 
29 static char *logfilename;
30 FILE *qemu_logfile;
31 int qemu_loglevel;
32 static int log_append = 0;
33 static GArray *debug_regions;
34 
35 void qemu_log(const char *fmt, ...)
36 {
37     va_list ap;
38 
39     va_start(ap, fmt);
40     if (qemu_logfile) {
41         vfprintf(qemu_logfile, fmt, ap);
42     }
43     va_end(ap);
44 }
45 
46 static bool log_uses_own_buffers;
47 
48 /* enable or disable low levels log */
49 void qemu_set_log(int log_flags)
50 {
51     qemu_loglevel = log_flags;
52 #ifdef CONFIG_TRACE_LOG
53     qemu_loglevel |= LOG_TRACE;
54 #endif
55     if (!qemu_logfile &&
56         (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
57         if (logfilename) {
58             qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
59             if (!qemu_logfile) {
60                 perror(logfilename);
61                 _exit(1);
62             }
63             /* In case we are a daemon redirect stderr to logfile */
64             if (is_daemonized()) {
65                 dup2(fileno(qemu_logfile), STDERR_FILENO);
66                 fclose(qemu_logfile);
67                 /* This will skip closing logfile in qemu_log_close() */
68                 qemu_logfile = stderr;
69             }
70         } else {
71             /* Default to stderr if no log file specified */
72             assert(!is_daemonized());
73             qemu_logfile = stderr;
74         }
75         /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
76         if (log_uses_own_buffers) {
77             static char logfile_buf[4096];
78 
79             setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
80         } else {
81 #if defined(_WIN32)
82             /* Win32 doesn't support line-buffering, so use unbuffered output. */
83             setvbuf(qemu_logfile, NULL, _IONBF, 0);
84 #else
85             setvbuf(qemu_logfile, NULL, _IOLBF, 0);
86 #endif
87             log_append = 1;
88         }
89     }
90     if (qemu_logfile &&
91         (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
92         qemu_log_close();
93     }
94 }
95 
96 void qemu_log_needs_buffers(void)
97 {
98     log_uses_own_buffers = true;
99 }
100 
101 /*
102  * Allow the user to include %d in their logfile which will be
103  * substituted with the current PID. This is useful for debugging many
104  * nested linux-user tasks but will result in lots of logs.
105  */
106 void qemu_set_log_filename(const char *filename, Error **errp)
107 {
108     char *pidstr;
109     g_free(logfilename);
110 
111     pidstr = strstr(filename, "%");
112     if (pidstr) {
113         /* We only accept one %d, no other format strings */
114         if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
115             error_setg(errp, "Bad logfile format: %s", filename);
116             return;
117         } else {
118             logfilename = g_strdup_printf(filename, getpid());
119         }
120     } else {
121         logfilename = g_strdup(filename);
122     }
123     qemu_log_close();
124     qemu_set_log(qemu_loglevel);
125 }
126 
127 /* Returns true if addr is in our debug filter or no filter defined
128  */
129 bool qemu_log_in_addr_range(uint64_t addr)
130 {
131     if (debug_regions) {
132         int i = 0;
133         for (i = 0; i < debug_regions->len; i++) {
134             Range *range = &g_array_index(debug_regions, Range, i);
135             if (range_contains(range, addr)) {
136                 return true;
137             }
138         }
139         return false;
140     } else {
141         return true;
142     }
143 }
144 
145 
146 void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
147 {
148     gchar **ranges = g_strsplit(filter_spec, ",", 0);
149     int i;
150 
151     if (debug_regions) {
152         g_array_unref(debug_regions);
153         debug_regions = NULL;
154     }
155 
156     debug_regions = g_array_sized_new(FALSE, FALSE,
157                                       sizeof(Range), g_strv_length(ranges));
158     for (i = 0; ranges[i]; i++) {
159         const char *r = ranges[i];
160         const char *range_op, *r2, *e;
161         uint64_t r1val, r2val, lob, upb;
162         struct Range range;
163 
164         range_op = strstr(r, "-");
165         r2 = range_op ? range_op + 1 : NULL;
166         if (!range_op) {
167             range_op = strstr(r, "+");
168             r2 = range_op ? range_op + 1 : NULL;
169         }
170         if (!range_op) {
171             range_op = strstr(r, "..");
172             r2 = range_op ? range_op + 2 : NULL;
173         }
174         if (!range_op) {
175             error_setg(errp, "Bad range specifier");
176             goto out;
177         }
178 
179         if (qemu_strtoull(r, &e, 0, &r1val)
180             || e != range_op) {
181             error_setg(errp, "Invalid number to the left of %.*s",
182                        (int)(r2 - range_op), range_op);
183             goto out;
184         }
185         if (qemu_strtoull(r2, NULL, 0, &r2val)) {
186             error_setg(errp, "Invalid number to the right of %.*s",
187                        (int)(r2 - range_op), range_op);
188             goto out;
189         }
190 
191         switch (*range_op) {
192         case '+':
193             lob = r1val;
194             upb = r1val + r2val - 1;
195             break;
196         case '-':
197             upb = r1val;
198             lob = r1val - (r2val - 1);
199             break;
200         case '.':
201             lob = r1val;
202             upb = r2val;
203             break;
204         default:
205             g_assert_not_reached();
206         }
207         if (lob > upb) {
208             error_setg(errp, "Invalid range");
209             goto out;
210         }
211         range_set_bounds(&range, lob, upb);
212         g_array_append_val(debug_regions, range);
213     }
214 out:
215     g_strfreev(ranges);
216 }
217 
218 /* fflush() the log file */
219 void qemu_log_flush(void)
220 {
221     fflush(qemu_logfile);
222 }
223 
224 /* Close the log file */
225 void qemu_log_close(void)
226 {
227     if (qemu_logfile) {
228         if (qemu_logfile != stderr) {
229             fclose(qemu_logfile);
230         }
231         qemu_logfile = NULL;
232     }
233 }
234 
235 const QEMULogItem qemu_log_items[] = {
236     { CPU_LOG_TB_OUT_ASM, "out_asm",
237       "show generated host assembly code for each compiled TB" },
238     { CPU_LOG_TB_IN_ASM, "in_asm",
239       "show target assembly code for each compiled TB" },
240     { CPU_LOG_TB_OP, "op",
241       "show micro ops for each compiled TB" },
242     { CPU_LOG_TB_OP_OPT, "op_opt",
243       "show micro ops (x86 only: before eflags optimization) and\n"
244       "after liveness analysis" },
245     { CPU_LOG_INT, "int",
246       "show interrupts/exceptions in short format" },
247     { CPU_LOG_EXEC, "exec",
248       "show trace before each executed TB (lots of logs)" },
249     { CPU_LOG_TB_CPU, "cpu",
250       "show CPU registers before entering a TB (lots of logs)" },
251     { CPU_LOG_MMU, "mmu",
252       "log MMU-related activities" },
253     { CPU_LOG_PCALL, "pcall",
254       "x86 only: show protected mode far calls/returns/exceptions" },
255     { CPU_LOG_RESET, "cpu_reset",
256       "show CPU state before CPU resets" },
257     { LOG_UNIMP, "unimp",
258       "log unimplemented functionality" },
259     { LOG_GUEST_ERROR, "guest_errors",
260       "log when the guest OS does something invalid (eg accessing a\n"
261       "non-existent register)" },
262     { CPU_LOG_PAGE, "page",
263       "dump pages at beginning of user mode emulation" },
264     { CPU_LOG_TB_NOCHAIN, "nochain",
265       "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
266       "complete traces" },
267     { 0, NULL, NULL },
268 };
269 
270 static int cmp1(const char *s1, int n, const char *s2)
271 {
272     if (strlen(s2) != n) {
273         return 0;
274     }
275     return memcmp(s1, s2, n) == 0;
276 }
277 
278 /* takes a comma separated list of log masks. Return 0 if error. */
279 int qemu_str_to_log_mask(const char *str)
280 {
281     const QEMULogItem *item;
282     int mask;
283     const char *p, *p1;
284 
285     p = str;
286     mask = 0;
287     for (;;) {
288         p1 = strchr(p, ',');
289         if (!p1) {
290             p1 = p + strlen(p);
291         }
292         if (cmp1(p,p1-p,"all")) {
293             for (item = qemu_log_items; item->mask != 0; item++) {
294                 mask |= item->mask;
295             }
296 #ifdef CONFIG_TRACE_LOG
297         } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
298             trace_enable_events(p + 6);
299             mask |= LOG_TRACE;
300 #endif
301         } else {
302             for (item = qemu_log_items; item->mask != 0; item++) {
303                 if (cmp1(p, p1 - p, item->name)) {
304                     goto found;
305                 }
306             }
307             return 0;
308         found:
309             mask |= item->mask;
310         }
311         if (*p1 != ',') {
312             break;
313         }
314         p = p1 + 1;
315     }
316     return mask;
317 }
318 
319 void qemu_print_log_usage(FILE *f)
320 {
321     const QEMULogItem *item;
322     fprintf(f, "Log items (comma separated):\n");
323     for (item = qemu_log_items; item->mask != 0; item++) {
324         fprintf(f, "%-15s %s\n", item->name, item->help);
325     }
326 #ifdef CONFIG_TRACE_LOG
327     fprintf(f, "trace:PATTERN   enable trace events\n");
328     fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
329 #endif
330 }
331