]>
Commit | Line | Data |
---|---|---|
5726c27f BS |
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 | ||
d38ea87a | 20 | #include "qemu/osdep.h" |
5726c27f | 21 | #include "qemu-common.h" |
1de7afc9 | 22 | #include "qemu/log.h" |
c84ea00d | 23 | #include "trace/control.h" |
5726c27f | 24 | |
40a50b0a | 25 | static char *logfilename; |
eeacee4d BS |
26 | FILE *qemu_logfile; |
27 | int qemu_loglevel; | |
5726c27f BS |
28 | static int log_append = 0; |
29 | ||
eeacee4d BS |
30 | void qemu_log(const char *fmt, ...) |
31 | { | |
32 | va_list ap; | |
33 | ||
34 | va_start(ap, fmt); | |
35 | if (qemu_logfile) { | |
36 | vfprintf(qemu_logfile, fmt, ap); | |
37 | } | |
38 | va_end(ap); | |
39 | } | |
40 | ||
41 | void qemu_log_mask(int mask, const char *fmt, ...) | |
42 | { | |
43 | va_list ap; | |
44 | ||
45 | va_start(ap, fmt); | |
46 | if ((qemu_loglevel & mask) && qemu_logfile) { | |
47 | vfprintf(qemu_logfile, fmt, ap); | |
48 | } | |
49 | va_end(ap); | |
50 | } | |
51 | ||
5726c27f | 52 | /* enable or disable low levels log */ |
24537a01 | 53 | void do_qemu_set_log(int log_flags, bool use_own_buffers) |
5726c27f | 54 | { |
eeacee4d | 55 | qemu_loglevel = log_flags; |
ed7f5f1d PB |
56 | #ifdef CONFIG_TRACE_LOG |
57 | qemu_loglevel |= LOG_TRACE; | |
58 | #endif | |
c586eac3 PB |
59 | if (!qemu_logfile && |
60 | (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { | |
989b697d PM |
61 | if (logfilename) { |
62 | qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); | |
63 | if (!qemu_logfile) { | |
64 | perror(logfilename); | |
65 | _exit(1); | |
66 | } | |
96c33a45 DA |
67 | /* In case we are a daemon redirect stderr to logfile */ |
68 | if (is_daemonized()) { | |
69 | dup2(fileno(qemu_logfile), STDERR_FILENO); | |
70 | fclose(qemu_logfile); | |
71 | /* This will skip closing logfile in qemu_log_close() */ | |
72 | qemu_logfile = stderr; | |
73 | } | |
989b697d PM |
74 | } else { |
75 | /* Default to stderr if no log file specified */ | |
c586eac3 | 76 | assert(!is_daemonized()); |
989b697d | 77 | qemu_logfile = stderr; |
5726c27f | 78 | } |
5726c27f | 79 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
3437e545 | 80 | if (use_own_buffers) { |
5726c27f | 81 | static char logfile_buf[4096]; |
3437e545 | 82 | |
eeacee4d | 83 | setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
3437e545 BS |
84 | } else { |
85 | #if defined(_WIN32) | |
86 | /* Win32 doesn't support line-buffering, so use unbuffered output. */ | |
87 | setvbuf(qemu_logfile, NULL, _IONBF, 0); | |
5726c27f | 88 | #else |
3437e545 | 89 | setvbuf(qemu_logfile, NULL, _IOLBF, 0); |
5726c27f | 90 | #endif |
3437e545 BS |
91 | log_append = 1; |
92 | } | |
5726c27f | 93 | } |
c586eac3 PB |
94 | if (qemu_logfile && |
95 | (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { | |
989b697d | 96 | qemu_log_close(); |
5726c27f BS |
97 | } |
98 | } | |
99 | ||
9a7e5424 | 100 | void qemu_set_log_filename(const char *filename) |
5726c27f | 101 | { |
40a50b0a | 102 | g_free(logfilename); |
636e0f27 | 103 | logfilename = g_strdup(filename); |
989b697d | 104 | qemu_log_close(); |
24537a01 | 105 | qemu_set_log(qemu_loglevel); |
5726c27f BS |
106 | } |
107 | ||
38dad9e5 | 108 | const QEMULogItem qemu_log_items[] = { |
5726c27f BS |
109 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
110 | "show generated host assembly code for each compiled TB" }, | |
111 | { CPU_LOG_TB_IN_ASM, "in_asm", | |
112 | "show target assembly code for each compiled TB" }, | |
113 | { CPU_LOG_TB_OP, "op", | |
114 | "show micro ops for each compiled TB" }, | |
115 | { CPU_LOG_TB_OP_OPT, "op_opt", | |
3437e545 | 116 | "show micro ops (x86 only: before eflags optimization) and\n" |
5726c27f BS |
117 | "after liveness analysis" }, |
118 | { CPU_LOG_INT, "int", | |
119 | "show interrupts/exceptions in short format" }, | |
120 | { CPU_LOG_EXEC, "exec", | |
121 | "show trace before each executed TB (lots of logs)" }, | |
122 | { CPU_LOG_TB_CPU, "cpu", | |
123 | "show CPU state before block translation" }, | |
339aaf5b AP |
124 | { CPU_LOG_MMU, "mmu", |
125 | "log MMU-related activities" }, | |
5726c27f | 126 | { CPU_LOG_PCALL, "pcall", |
3437e545 | 127 | "x86 only: show protected mode far calls/returns/exceptions" }, |
5726c27f | 128 | { CPU_LOG_RESET, "cpu_reset", |
dbfe1b6a | 129 | "show CPU state before CPU resets" }, |
dafdf1ab BS |
130 | { LOG_UNIMP, "unimp", |
131 | "log unimplemented functionality" }, | |
e54eba19 PM |
132 | { LOG_GUEST_ERROR, "guest_errors", |
133 | "log when the guest OS does something invalid (eg accessing a\n" | |
134 | "non-existent register)" }, | |
13829020 PB |
135 | { CPU_LOG_PAGE, "page", |
136 | "dump pages at beginning of user mode emulation" }, | |
89a82cd4 RH |
137 | { CPU_LOG_TB_NOCHAIN, "nochain", |
138 | "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" | |
139 | "complete traces" }, | |
5726c27f BS |
140 | { 0, NULL, NULL }, |
141 | }; | |
142 | ||
143 | static int cmp1(const char *s1, int n, const char *s2) | |
144 | { | |
145 | if (strlen(s2) != n) { | |
146 | return 0; | |
147 | } | |
148 | return memcmp(s1, s2, n) == 0; | |
149 | } | |
150 | ||
151 | /* takes a comma separated list of log masks. Return 0 if error. */ | |
4fde1eba | 152 | int qemu_str_to_log_mask(const char *str) |
5726c27f | 153 | { |
38dad9e5 | 154 | const QEMULogItem *item; |
5726c27f BS |
155 | int mask; |
156 | const char *p, *p1; | |
157 | ||
158 | p = str; | |
159 | mask = 0; | |
160 | for (;;) { | |
161 | p1 = strchr(p, ','); | |
162 | if (!p1) { | |
163 | p1 = p + strlen(p); | |
164 | } | |
165 | if (cmp1(p,p1-p,"all")) { | |
38dad9e5 | 166 | for (item = qemu_log_items; item->mask != 0; item++) { |
5726c27f BS |
167 | mask |= item->mask; |
168 | } | |
c84ea00d PB |
169 | #ifdef CONFIG_TRACE_LOG |
170 | } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) { | |
171 | trace_enable_events(p + 6); | |
172 | mask |= LOG_TRACE; | |
173 | #endif | |
5726c27f | 174 | } else { |
38dad9e5 | 175 | for (item = qemu_log_items; item->mask != 0; item++) { |
5726c27f BS |
176 | if (cmp1(p, p1 - p, item->name)) { |
177 | goto found; | |
178 | } | |
179 | } | |
180 | return 0; | |
c84ea00d PB |
181 | found: |
182 | mask |= item->mask; | |
5726c27f | 183 | } |
5726c27f BS |
184 | if (*p1 != ',') { |
185 | break; | |
186 | } | |
187 | p = p1 + 1; | |
188 | } | |
189 | return mask; | |
190 | } | |
59a6fa6e PM |
191 | |
192 | void qemu_print_log_usage(FILE *f) | |
193 | { | |
38dad9e5 | 194 | const QEMULogItem *item; |
59a6fa6e | 195 | fprintf(f, "Log items (comma separated):\n"); |
38dad9e5 | 196 | for (item = qemu_log_items; item->mask != 0; item++) { |
c84ea00d | 197 | fprintf(f, "%-15s %s\n", item->name, item->help); |
59a6fa6e | 198 | } |
c84ea00d PB |
199 | #ifdef CONFIG_TRACE_LOG |
200 | fprintf(f, "trace:PATTERN enable trace events\n"); | |
201 | fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); | |
202 | #endif | |
59a6fa6e | 203 | } |