xref: /qemu/util/log.c (revision 2ec62fae)
1d890d50dSDenis V. Lunev /*
2d890d50dSDenis V. Lunev  * Logging support
3d890d50dSDenis V. Lunev  *
4d890d50dSDenis V. Lunev  *  Copyright (c) 2003 Fabrice Bellard
5d890d50dSDenis V. Lunev  *
6d890d50dSDenis V. Lunev  * This library is free software; you can redistribute it and/or
7d890d50dSDenis V. Lunev  * modify it under the terms of the GNU Lesser General Public
8d890d50dSDenis V. Lunev  * License as published by the Free Software Foundation; either
9d890d50dSDenis V. Lunev  * version 2 of the License, or (at your option) any later version.
10d890d50dSDenis V. Lunev  *
11d890d50dSDenis V. Lunev  * This library is distributed in the hope that it will be useful,
12d890d50dSDenis V. Lunev  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d890d50dSDenis V. Lunev  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14d890d50dSDenis V. Lunev  * Lesser General Public License for more details.
15d890d50dSDenis V. Lunev  *
16d890d50dSDenis V. Lunev  * You should have received a copy of the GNU Lesser General Public
17d890d50dSDenis V. Lunev  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18d890d50dSDenis V. Lunev  */
19d890d50dSDenis V. Lunev 
20d38ea87aSPeter Maydell #include "qemu/osdep.h"
21d890d50dSDenis V. Lunev #include "qemu-common.h"
22d890d50dSDenis V. Lunev #include "qemu/log.h"
233514552eSAlex Bennée #include "qemu/range.h"
243514552eSAlex Bennée #include "qemu/error-report.h"
253514552eSAlex Bennée #include "qemu/cutils.h"
26c84ea00dSPaolo Bonzini #include "trace/control.h"
27d890d50dSDenis V. Lunev 
28d890d50dSDenis V. Lunev static char *logfilename;
29d890d50dSDenis V. Lunev FILE *qemu_logfile;
30d890d50dSDenis V. Lunev int qemu_loglevel;
31d890d50dSDenis V. Lunev static int log_append = 0;
323514552eSAlex Bennée static GArray *debug_regions;
33d890d50dSDenis V. Lunev 
34d890d50dSDenis V. Lunev void qemu_log(const char *fmt, ...)
35d890d50dSDenis V. Lunev {
36d890d50dSDenis V. Lunev     va_list ap;
37d890d50dSDenis V. Lunev 
38d890d50dSDenis V. Lunev     va_start(ap, fmt);
39d890d50dSDenis V. Lunev     if (qemu_logfile) {
40d890d50dSDenis V. Lunev         vfprintf(qemu_logfile, fmt, ap);
41d890d50dSDenis V. Lunev     }
42d890d50dSDenis V. Lunev     va_end(ap);
43d890d50dSDenis V. Lunev }
44d890d50dSDenis V. Lunev 
45f2937a33SPaolo Bonzini static bool log_uses_own_buffers;
46f2937a33SPaolo Bonzini 
47d890d50dSDenis V. Lunev /* enable or disable low levels log */
48f2937a33SPaolo Bonzini void qemu_set_log(int log_flags)
49d890d50dSDenis V. Lunev {
50d890d50dSDenis V. Lunev     qemu_loglevel = log_flags;
51ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
52ed7f5f1dSPaolo Bonzini     qemu_loglevel |= LOG_TRACE;
53ed7f5f1dSPaolo Bonzini #endif
54c586eac3SPaolo Bonzini     if (!qemu_logfile &&
55c586eac3SPaolo Bonzini         (is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
56d890d50dSDenis V. Lunev         if (logfilename) {
57d890d50dSDenis V. Lunev             qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
58d890d50dSDenis V. Lunev             if (!qemu_logfile) {
59d890d50dSDenis V. Lunev                 perror(logfilename);
60d890d50dSDenis V. Lunev                 _exit(1);
61d890d50dSDenis V. Lunev             }
6296c33a45SDimitris Aragiorgis             /* In case we are a daemon redirect stderr to logfile */
6396c33a45SDimitris Aragiorgis             if (is_daemonized()) {
6496c33a45SDimitris Aragiorgis                 dup2(fileno(qemu_logfile), STDERR_FILENO);
6596c33a45SDimitris Aragiorgis                 fclose(qemu_logfile);
6696c33a45SDimitris Aragiorgis                 /* This will skip closing logfile in qemu_log_close() */
6796c33a45SDimitris Aragiorgis                 qemu_logfile = stderr;
6896c33a45SDimitris Aragiorgis             }
69d890d50dSDenis V. Lunev         } else {
70d890d50dSDenis V. Lunev             /* Default to stderr if no log file specified */
71c586eac3SPaolo Bonzini             assert(!is_daemonized());
72d890d50dSDenis V. Lunev             qemu_logfile = stderr;
73d890d50dSDenis V. Lunev         }
74d890d50dSDenis V. Lunev         /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
75f2937a33SPaolo Bonzini         if (log_uses_own_buffers) {
76d890d50dSDenis V. Lunev             static char logfile_buf[4096];
77d890d50dSDenis V. Lunev 
78d890d50dSDenis V. Lunev             setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
79d890d50dSDenis V. Lunev         } else {
80d890d50dSDenis V. Lunev #if defined(_WIN32)
81d890d50dSDenis V. Lunev             /* Win32 doesn't support line-buffering, so use unbuffered output. */
82d890d50dSDenis V. Lunev             setvbuf(qemu_logfile, NULL, _IONBF, 0);
83d890d50dSDenis V. Lunev #else
84d890d50dSDenis V. Lunev             setvbuf(qemu_logfile, NULL, _IOLBF, 0);
85d890d50dSDenis V. Lunev #endif
86d890d50dSDenis V. Lunev             log_append = 1;
87d890d50dSDenis V. Lunev         }
88d890d50dSDenis V. Lunev     }
89c586eac3SPaolo Bonzini     if (qemu_logfile &&
90c586eac3SPaolo Bonzini         (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
91d890d50dSDenis V. Lunev         qemu_log_close();
92d890d50dSDenis V. Lunev     }
93d890d50dSDenis V. Lunev }
94f2937a33SPaolo Bonzini 
95f2937a33SPaolo Bonzini void qemu_log_needs_buffers(void)
96f2937a33SPaolo Bonzini {
97f2937a33SPaolo Bonzini     log_uses_own_buffers = true;
98f2937a33SPaolo Bonzini }
99f2937a33SPaolo Bonzini 
100f6880b7fSAlex Bennée /*
101f6880b7fSAlex Bennée  * Allow the user to include %d in their logfile which will be
102f6880b7fSAlex Bennée  * substituted with the current PID. This is useful for debugging many
103f6880b7fSAlex Bennée  * nested linux-user tasks but will result in lots of logs.
104f6880b7fSAlex Bennée  */
105d890d50dSDenis V. Lunev void qemu_set_log_filename(const char *filename)
106d890d50dSDenis V. Lunev {
107f6880b7fSAlex Bennée     char *pidstr;
108d890d50dSDenis V. Lunev     g_free(logfilename);
109f6880b7fSAlex Bennée 
110f6880b7fSAlex Bennée     pidstr = strstr(filename, "%");
111f6880b7fSAlex Bennée     if (pidstr) {
112f6880b7fSAlex Bennée         /* We only accept one %d, no other format strings */
113f6880b7fSAlex Bennée         if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
114f6880b7fSAlex Bennée             error_report("Bad logfile format: %s", filename);
115f6880b7fSAlex Bennée             logfilename = NULL;
116f6880b7fSAlex Bennée         } else {
117f6880b7fSAlex Bennée             logfilename = g_strdup_printf(filename, getpid());
118f6880b7fSAlex Bennée         }
119f6880b7fSAlex Bennée     } else {
120d890d50dSDenis V. Lunev         logfilename = g_strdup(filename);
121f6880b7fSAlex Bennée     }
122d890d50dSDenis V. Lunev     qemu_log_close();
123d890d50dSDenis V. Lunev     qemu_set_log(qemu_loglevel);
124d890d50dSDenis V. Lunev }
125d890d50dSDenis V. Lunev 
1263514552eSAlex Bennée /* Returns true if addr is in our debug filter or no filter defined
1273514552eSAlex Bennée  */
1283514552eSAlex Bennée bool qemu_log_in_addr_range(uint64_t addr)
1293514552eSAlex Bennée {
1303514552eSAlex Bennée     if (debug_regions) {
1313514552eSAlex Bennée         int i = 0;
1323514552eSAlex Bennée         for (i = 0; i < debug_regions->len; i++) {
1333514552eSAlex Bennée             struct Range *range = &g_array_index(debug_regions, Range, i);
1343514552eSAlex Bennée             if (addr >= range->begin && addr <= range->end) {
1353514552eSAlex Bennée                 return true;
1363514552eSAlex Bennée             }
1373514552eSAlex Bennée         }
1383514552eSAlex Bennée         return false;
1393514552eSAlex Bennée     } else {
1403514552eSAlex Bennée         return true;
1413514552eSAlex Bennée     }
1423514552eSAlex Bennée }
1433514552eSAlex Bennée 
1443514552eSAlex Bennée 
1453514552eSAlex Bennée void qemu_set_dfilter_ranges(const char *filter_spec)
1463514552eSAlex Bennée {
1473514552eSAlex Bennée     gchar **ranges = g_strsplit(filter_spec, ",", 0);
148*2ec62faeSMarkus Armbruster 
149*2ec62faeSMarkus Armbruster     if (debug_regions) {
150*2ec62faeSMarkus Armbruster         g_array_unref(debug_regions);
151*2ec62faeSMarkus Armbruster         debug_regions = NULL;
152*2ec62faeSMarkus Armbruster     }
153*2ec62faeSMarkus Armbruster 
1543514552eSAlex Bennée     if (ranges) {
1553514552eSAlex Bennée         gchar **next = ranges;
1563514552eSAlex Bennée         gchar *r = *next++;
157*2ec62faeSMarkus Armbruster 
1583514552eSAlex Bennée         debug_regions = g_array_sized_new(FALSE, FALSE,
1593514552eSAlex Bennée                                           sizeof(Range), g_strv_length(ranges));
1603514552eSAlex Bennée         while (r) {
1613514552eSAlex Bennée             char *range_op = strstr(r, "-");
1623514552eSAlex Bennée             char *r2 = range_op ? range_op + 1 : NULL;
1633514552eSAlex Bennée             if (!range_op) {
1643514552eSAlex Bennée                 range_op = strstr(r, "+");
1653514552eSAlex Bennée                 r2 = range_op ? range_op + 1 : NULL;
1663514552eSAlex Bennée             }
1673514552eSAlex Bennée             if (!range_op) {
1683514552eSAlex Bennée                 range_op = strstr(r, "..");
1693514552eSAlex Bennée                 r2 = range_op ? range_op + 2 : NULL;
1703514552eSAlex Bennée             }
1713514552eSAlex Bennée             if (range_op) {
1723514552eSAlex Bennée                 const char *e = NULL;
1733514552eSAlex Bennée                 uint64_t r1val, r2val;
1743514552eSAlex Bennée 
1753514552eSAlex Bennée                 if ((qemu_strtoull(r, &e, 0, &r1val) == 0) &&
1763514552eSAlex Bennée                     (qemu_strtoull(r2, NULL, 0, &r2val) == 0) &&
1773514552eSAlex Bennée                     r2val > 0) {
1783514552eSAlex Bennée                     struct Range range;
1793514552eSAlex Bennée 
1803514552eSAlex Bennée                     g_assert(e == range_op);
1813514552eSAlex Bennée 
1823514552eSAlex Bennée                     switch (*range_op) {
1833514552eSAlex Bennée                     case '+':
1843514552eSAlex Bennée                     {
1853514552eSAlex Bennée                         range.begin = r1val;
1863514552eSAlex Bennée                         range.end = r1val + (r2val - 1);
1873514552eSAlex Bennée                         break;
1883514552eSAlex Bennée                     }
1893514552eSAlex Bennée                     case '-':
1903514552eSAlex Bennée                     {
1913514552eSAlex Bennée                         range.end = r1val;
1923514552eSAlex Bennée                         range.begin = r1val - (r2val - 1);
1933514552eSAlex Bennée                         break;
1943514552eSAlex Bennée                     }
1953514552eSAlex Bennée                     case '.':
1963514552eSAlex Bennée                         range.begin = r1val;
1973514552eSAlex Bennée                         range.end = r2val;
1983514552eSAlex Bennée                         break;
1993514552eSAlex Bennée                     default:
2003514552eSAlex Bennée                         g_assert_not_reached();
2013514552eSAlex Bennée                     }
2023514552eSAlex Bennée                     g_array_append_val(debug_regions, range);
2033514552eSAlex Bennée 
2043514552eSAlex Bennée                 } else {
2053514552eSAlex Bennée                     g_error("Failed to parse range in: %s", r);
2063514552eSAlex Bennée                 }
2073514552eSAlex Bennée             } else {
2083514552eSAlex Bennée                 g_error("Bad range specifier in: %s", r);
2093514552eSAlex Bennée             }
2103514552eSAlex Bennée             r = *next++;
2113514552eSAlex Bennée         }
2123514552eSAlex Bennée         g_strfreev(ranges);
2133514552eSAlex Bennée     }
2143514552eSAlex Bennée }
2153514552eSAlex Bennée 
21699affd1dSDenis V. Lunev /* fflush() the log file */
21799affd1dSDenis V. Lunev void qemu_log_flush(void)
21899affd1dSDenis V. Lunev {
21999affd1dSDenis V. Lunev     fflush(qemu_logfile);
22099affd1dSDenis V. Lunev }
22199affd1dSDenis V. Lunev 
22299affd1dSDenis V. Lunev /* Close the log file */
22399affd1dSDenis V. Lunev void qemu_log_close(void)
22499affd1dSDenis V. Lunev {
22599affd1dSDenis V. Lunev     if (qemu_logfile) {
22699affd1dSDenis V. Lunev         if (qemu_logfile != stderr) {
22799affd1dSDenis V. Lunev             fclose(qemu_logfile);
22899affd1dSDenis V. Lunev         }
22999affd1dSDenis V. Lunev         qemu_logfile = NULL;
23099affd1dSDenis V. Lunev     }
23199affd1dSDenis V. Lunev }
23299affd1dSDenis V. Lunev 
233d890d50dSDenis V. Lunev const QEMULogItem qemu_log_items[] = {
234d890d50dSDenis V. Lunev     { CPU_LOG_TB_OUT_ASM, "out_asm",
235d890d50dSDenis V. Lunev       "show generated host assembly code for each compiled TB" },
236d890d50dSDenis V. Lunev     { CPU_LOG_TB_IN_ASM, "in_asm",
237d890d50dSDenis V. Lunev       "show target assembly code for each compiled TB" },
238d890d50dSDenis V. Lunev     { CPU_LOG_TB_OP, "op",
239d890d50dSDenis V. Lunev       "show micro ops for each compiled TB" },
240d890d50dSDenis V. Lunev     { CPU_LOG_TB_OP_OPT, "op_opt",
241d890d50dSDenis V. Lunev       "show micro ops (x86 only: before eflags optimization) and\n"
242d890d50dSDenis V. Lunev       "after liveness analysis" },
243d890d50dSDenis V. Lunev     { CPU_LOG_INT, "int",
244d890d50dSDenis V. Lunev       "show interrupts/exceptions in short format" },
245d890d50dSDenis V. Lunev     { CPU_LOG_EXEC, "exec",
246d890d50dSDenis V. Lunev       "show trace before each executed TB (lots of logs)" },
247d890d50dSDenis V. Lunev     { CPU_LOG_TB_CPU, "cpu",
24854195736SAlex Bennée       "show CPU registers before entering a TB (lots of logs)" },
249d890d50dSDenis V. Lunev     { CPU_LOG_MMU, "mmu",
250d890d50dSDenis V. Lunev       "log MMU-related activities" },
251d890d50dSDenis V. Lunev     { CPU_LOG_PCALL, "pcall",
252d890d50dSDenis V. Lunev       "x86 only: show protected mode far calls/returns/exceptions" },
253d890d50dSDenis V. Lunev     { CPU_LOG_RESET, "cpu_reset",
254d890d50dSDenis V. Lunev       "show CPU state before CPU resets" },
255d890d50dSDenis V. Lunev     { LOG_UNIMP, "unimp",
256d890d50dSDenis V. Lunev       "log unimplemented functionality" },
257d890d50dSDenis V. Lunev     { LOG_GUEST_ERROR, "guest_errors",
258d890d50dSDenis V. Lunev       "log when the guest OS does something invalid (eg accessing a\n"
259d890d50dSDenis V. Lunev       "non-existent register)" },
260d890d50dSDenis V. Lunev     { CPU_LOG_PAGE, "page",
261d890d50dSDenis V. Lunev       "dump pages at beginning of user mode emulation" },
262d890d50dSDenis V. Lunev     { CPU_LOG_TB_NOCHAIN, "nochain",
263d890d50dSDenis V. Lunev       "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
264d890d50dSDenis V. Lunev       "complete traces" },
265d890d50dSDenis V. Lunev     { 0, NULL, NULL },
266d890d50dSDenis V. Lunev };
267d890d50dSDenis V. Lunev 
268d890d50dSDenis V. Lunev static int cmp1(const char *s1, int n, const char *s2)
269d890d50dSDenis V. Lunev {
270d890d50dSDenis V. Lunev     if (strlen(s2) != n) {
271d890d50dSDenis V. Lunev         return 0;
272d890d50dSDenis V. Lunev     }
273d890d50dSDenis V. Lunev     return memcmp(s1, s2, n) == 0;
274d890d50dSDenis V. Lunev }
275d890d50dSDenis V. Lunev 
276d890d50dSDenis V. Lunev /* takes a comma separated list of log masks. Return 0 if error. */
277d890d50dSDenis V. Lunev int qemu_str_to_log_mask(const char *str)
278d890d50dSDenis V. Lunev {
279d890d50dSDenis V. Lunev     const QEMULogItem *item;
280d890d50dSDenis V. Lunev     int mask;
281d890d50dSDenis V. Lunev     const char *p, *p1;
282d890d50dSDenis V. Lunev 
283d890d50dSDenis V. Lunev     p = str;
284d890d50dSDenis V. Lunev     mask = 0;
285d890d50dSDenis V. Lunev     for (;;) {
286d890d50dSDenis V. Lunev         p1 = strchr(p, ',');
287d890d50dSDenis V. Lunev         if (!p1) {
288d890d50dSDenis V. Lunev             p1 = p + strlen(p);
289d890d50dSDenis V. Lunev         }
290d890d50dSDenis V. Lunev         if (cmp1(p,p1-p,"all")) {
291d890d50dSDenis V. Lunev             for (item = qemu_log_items; item->mask != 0; item++) {
292d890d50dSDenis V. Lunev                 mask |= item->mask;
293d890d50dSDenis V. Lunev             }
294c84ea00dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
295c84ea00dSPaolo Bonzini         } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
296c84ea00dSPaolo Bonzini             trace_enable_events(p + 6);
297c84ea00dSPaolo Bonzini             mask |= LOG_TRACE;
298c84ea00dSPaolo Bonzini #endif
299d890d50dSDenis V. Lunev         } else {
300d890d50dSDenis V. Lunev             for (item = qemu_log_items; item->mask != 0; item++) {
301d890d50dSDenis V. Lunev                 if (cmp1(p, p1 - p, item->name)) {
302d890d50dSDenis V. Lunev                     goto found;
303d890d50dSDenis V. Lunev                 }
304d890d50dSDenis V. Lunev             }
305d890d50dSDenis V. Lunev             return 0;
306d890d50dSDenis V. Lunev         found:
307d890d50dSDenis V. Lunev             mask |= item->mask;
308c84ea00dSPaolo Bonzini         }
309d890d50dSDenis V. Lunev         if (*p1 != ',') {
310d890d50dSDenis V. Lunev             break;
311d890d50dSDenis V. Lunev         }
312d890d50dSDenis V. Lunev         p = p1 + 1;
313d890d50dSDenis V. Lunev     }
314d890d50dSDenis V. Lunev     return mask;
315d890d50dSDenis V. Lunev }
316d890d50dSDenis V. Lunev 
317d890d50dSDenis V. Lunev void qemu_print_log_usage(FILE *f)
318d890d50dSDenis V. Lunev {
319d890d50dSDenis V. Lunev     const QEMULogItem *item;
320d890d50dSDenis V. Lunev     fprintf(f, "Log items (comma separated):\n");
321d890d50dSDenis V. Lunev     for (item = qemu_log_items; item->mask != 0; item++) {
322c84ea00dSPaolo Bonzini         fprintf(f, "%-15s %s\n", item->name, item->help);
323d890d50dSDenis V. Lunev     }
324c84ea00dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
325c84ea00dSPaolo Bonzini     fprintf(f, "trace:PATTERN   enable trace events\n");
326c84ea00dSPaolo Bonzini     fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
327c84ea00dSPaolo Bonzini #endif
328d890d50dSDenis V. Lunev }
329