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