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