blob: 7cb01a802b9a1cbf713997c67b51de3ec6449f5b [file] [log] [blame]
Blue Swirl5726c272012-06-03 15:03:23 +00001/*
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-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/log.h"
Blue Swirl5726c272012-06-03 15:03:23 +000022
Markus Armbruster40a50b02013-01-22 11:08:03 +010023static char *logfilename;
Blue Swirleeacee42012-06-03 16:35:32 +000024FILE *qemu_logfile;
25int qemu_loglevel;
Blue Swirl5726c272012-06-03 15:03:23 +000026static int log_append = 0;
27
Blue Swirleeacee42012-06-03 16:35:32 +000028void qemu_log(const char *fmt, ...)
29{
30 va_list ap;
31
32 va_start(ap, fmt);
33 if (qemu_logfile) {
34 vfprintf(qemu_logfile, fmt, ap);
35 }
36 va_end(ap);
37}
38
39void qemu_log_mask(int mask, const char *fmt, ...)
40{
41 va_list ap;
42
43 va_start(ap, fmt);
44 if ((qemu_loglevel & mask) && qemu_logfile) {
45 vfprintf(qemu_logfile, fmt, ap);
46 }
47 va_end(ap);
48}
49
Blue Swirl5726c272012-06-03 15:03:23 +000050/* enable or disable low levels log */
Peter Maydell24537a02013-02-11 16:41:23 +000051void do_qemu_set_log(int log_flags, bool use_own_buffers)
Blue Swirl5726c272012-06-03 15:03:23 +000052{
Blue Swirleeacee42012-06-03 16:35:32 +000053 qemu_loglevel = log_flags;
54 if (qemu_loglevel && !qemu_logfile) {
Peter Maydell989b6972013-02-26 17:52:40 +000055 if (logfilename) {
56 qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
57 if (!qemu_logfile) {
58 perror(logfilename);
59 _exit(1);
60 }
61 } else {
62 /* Default to stderr if no log file specified */
63 qemu_logfile = stderr;
Blue Swirl5726c272012-06-03 15:03:23 +000064 }
Blue Swirl5726c272012-06-03 15:03:23 +000065 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
Blue Swirl3437e542012-07-07 14:40:18 +000066 if (use_own_buffers) {
Blue Swirl5726c272012-06-03 15:03:23 +000067 static char logfile_buf[4096];
Blue Swirl3437e542012-07-07 14:40:18 +000068
Blue Swirleeacee42012-06-03 16:35:32 +000069 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
Blue Swirl3437e542012-07-07 14:40:18 +000070 } else {
71#if defined(_WIN32)
72 /* Win32 doesn't support line-buffering, so use unbuffered output. */
73 setvbuf(qemu_logfile, NULL, _IONBF, 0);
Blue Swirl5726c272012-06-03 15:03:23 +000074#else
Blue Swirl3437e542012-07-07 14:40:18 +000075 setvbuf(qemu_logfile, NULL, _IOLBF, 0);
Blue Swirl5726c272012-06-03 15:03:23 +000076#endif
Blue Swirl3437e542012-07-07 14:40:18 +000077 log_append = 1;
78 }
Blue Swirl5726c272012-06-03 15:03:23 +000079 }
Blue Swirleeacee42012-06-03 16:35:32 +000080 if (!qemu_loglevel && qemu_logfile) {
Peter Maydell989b6972013-02-26 17:52:40 +000081 qemu_log_close();
Blue Swirl5726c272012-06-03 15:03:23 +000082 }
83}
84
Peter Maydell9a7e5422013-02-11 16:41:20 +000085void qemu_set_log_filename(const char *filename)
Blue Swirl5726c272012-06-03 15:03:23 +000086{
Markus Armbruster40a50b02013-01-22 11:08:03 +010087 g_free(logfilename);
Markus Armbruster636e0f22013-01-22 11:08:02 +010088 logfilename = g_strdup(filename);
Peter Maydell989b6972013-02-26 17:52:40 +000089 qemu_log_close();
Peter Maydell24537a02013-02-11 16:41:23 +000090 qemu_set_log(qemu_loglevel);
Blue Swirl5726c272012-06-03 15:03:23 +000091}
92
Peter Maydell38dad9e2013-02-11 16:41:25 +000093const QEMULogItem qemu_log_items[] = {
Blue Swirl5726c272012-06-03 15:03:23 +000094 { CPU_LOG_TB_OUT_ASM, "out_asm",
95 "show generated host assembly code for each compiled TB" },
96 { CPU_LOG_TB_IN_ASM, "in_asm",
97 "show target assembly code for each compiled TB" },
98 { CPU_LOG_TB_OP, "op",
99 "show micro ops for each compiled TB" },
100 { CPU_LOG_TB_OP_OPT, "op_opt",
Blue Swirl3437e542012-07-07 14:40:18 +0000101 "show micro ops (x86 only: before eflags optimization) and\n"
Blue Swirl5726c272012-06-03 15:03:23 +0000102 "after liveness analysis" },
103 { CPU_LOG_INT, "int",
104 "show interrupts/exceptions in short format" },
105 { CPU_LOG_EXEC, "exec",
106 "show trace before each executed TB (lots of logs)" },
107 { CPU_LOG_TB_CPU, "cpu",
108 "show CPU state before block translation" },
Antony Pavlov339aaf52014-12-13 19:48:18 +0300109 { CPU_LOG_MMU, "mmu",
110 "log MMU-related activities" },
Blue Swirl5726c272012-06-03 15:03:23 +0000111 { CPU_LOG_PCALL, "pcall",
Blue Swirl3437e542012-07-07 14:40:18 +0000112 "x86 only: show protected mode far calls/returns/exceptions" },
Blue Swirl5726c272012-06-03 15:03:23 +0000113 { CPU_LOG_RESET, "cpu_reset",
Thomas Huthdbfe1b62015-01-27 13:11:26 +0100114 "show CPU state before CPU resets" },
Blue Swirldafdf1a2012-06-03 17:04:28 +0000115 { LOG_UNIMP, "unimp",
116 "log unimplemented functionality" },
Peter Maydelle54eba12012-10-18 14:11:35 +0100117 { LOG_GUEST_ERROR, "guest_errors",
118 "log when the guest OS does something invalid (eg accessing a\n"
119 "non-existent register)" },
Richard Henderson89a82cd2015-09-16 15:33:53 -0700120 { CPU_LOG_TB_NOCHAIN, "nochain",
121 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
122 "complete traces" },
Blue Swirl5726c272012-06-03 15:03:23 +0000123 { 0, NULL, NULL },
124};
125
126static int cmp1(const char *s1, int n, const char *s2)
127{
128 if (strlen(s2) != n) {
129 return 0;
130 }
131 return memcmp(s1, s2, n) == 0;
132}
133
134/* takes a comma separated list of log masks. Return 0 if error. */
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000135int qemu_str_to_log_mask(const char *str)
Blue Swirl5726c272012-06-03 15:03:23 +0000136{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000137 const QEMULogItem *item;
Blue Swirl5726c272012-06-03 15:03:23 +0000138 int mask;
139 const char *p, *p1;
140
141 p = str;
142 mask = 0;
143 for (;;) {
144 p1 = strchr(p, ',');
145 if (!p1) {
146 p1 = p + strlen(p);
147 }
148 if (cmp1(p,p1-p,"all")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000149 for (item = qemu_log_items; item->mask != 0; item++) {
Blue Swirl5726c272012-06-03 15:03:23 +0000150 mask |= item->mask;
151 }
152 } else {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000153 for (item = qemu_log_items; item->mask != 0; item++) {
Blue Swirl5726c272012-06-03 15:03:23 +0000154 if (cmp1(p, p1 - p, item->name)) {
155 goto found;
156 }
157 }
158 return 0;
159 }
160 found:
161 mask |= item->mask;
162 if (*p1 != ',') {
163 break;
164 }
165 p = p1 + 1;
166 }
167 return mask;
168}
Peter Maydell59a6fa62013-02-11 16:41:21 +0000169
170void qemu_print_log_usage(FILE *f)
171{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000172 const QEMULogItem *item;
Peter Maydell59a6fa62013-02-11 16:41:21 +0000173 fprintf(f, "Log items (comma separated):\n");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000174 for (item = qemu_log_items; item->mask != 0; item++) {
Peter Maydell59a6fa62013-02-11 16:41:21 +0000175 fprintf(f, "%-10s %s\n", item->name, item->help);
176 }
177}