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