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