]>
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 | |
61f3c91a | 9 | * version 2.1 of the License, or (at your option) any later version. |
5726c27f BS |
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" |
6e8a355d | 28 | #include "qemu/lockable.h" |
7fc493f8 | 29 | #include "qemu/rcu.h" |
4e51069d RH |
30 | #ifdef CONFIG_LINUX |
31 | #include <sys/syscall.h> | |
32 | #endif | |
7fc493f8 RH |
33 | |
34 | ||
d5f55fff | 35 | typedef struct RCUCloseFILE { |
7fc493f8 RH |
36 | struct rcu_head rcu; |
37 | FILE *fd; | |
d5f55fff | 38 | } RCUCloseFILE; |
5726c27f | 39 | |
702979f7 RH |
40 | /* Mutex covering the other global_* variables. */ |
41 | static QemuMutex global_mutex; | |
42266464 | 42 | static char *global_filename; |
30f5a73a | 43 | static FILE *global_file; |
4e51069d | 44 | static __thread FILE *thread_file; |
702979f7 | 45 | |
eeacee4d | 46 | int qemu_loglevel; |
4e51069d RH |
47 | static bool log_append; |
48 | static bool log_per_thread; | |
3514552e | 49 | static GArray *debug_regions; |
5726c27f | 50 | |
7fc493f8 RH |
51 | /* Returns true if qemu_log() will really write somewhere. */ |
52 | bool qemu_log_enabled(void) | |
53 | { | |
4e51069d | 54 | return log_per_thread || qatomic_read(&global_file) != NULL; |
7fc493f8 RH |
55 | } |
56 | ||
57 | /* Returns true if qemu_log() will write somewhere other than stderr. */ | |
58 | bool qemu_log_separate(void) | |
59 | { | |
4e51069d RH |
60 | if (log_per_thread) { |
61 | return true; | |
62 | } else { | |
63 | FILE *logfile = qatomic_read(&global_file); | |
64 | return logfile && logfile != stderr; | |
65 | } | |
66 | } | |
67 | ||
68 | static int log_thread_id(void) | |
69 | { | |
70 | #ifdef CONFIG_GETTID | |
71 | return gettid(); | |
72 | #elif defined(SYS_gettid) | |
73 | return syscall(SYS_gettid); | |
74 | #else | |
75 | static int counter; | |
76 | return qatomic_fetch_inc(&counter); | |
77 | #endif | |
7fc493f8 RH |
78 | } |
79 | ||
c59fe6e5 RH |
80 | /* Lock/unlock output. */ |
81 | ||
c60f599b | 82 | FILE *qemu_log_trylock(void) |
c59fe6e5 | 83 | { |
30f5a73a | 84 | FILE *logfile; |
c60f599b | 85 | |
4e51069d RH |
86 | logfile = thread_file; |
87 | if (!logfile) { | |
88 | if (log_per_thread) { | |
89 | g_autofree char *filename | |
90 | = g_strdup_printf(global_filename, log_thread_id()); | |
91 | logfile = fopen(filename, "w"); | |
92 | if (!logfile) { | |
93 | return NULL; | |
94 | } | |
95 | thread_file = logfile; | |
96 | } else { | |
97 | rcu_read_lock(); | |
98 | /* | |
99 | * FIXME: typeof_strip_qual, as used by qatomic_rcu_read, | |
100 | * does not work with pointers to undefined structures, | |
101 | * such as we have with struct _IO_FILE and musl libc. | |
102 | * Since all we want is a read of a pointer, cast to void**, | |
103 | * which does work with typeof_strip_qual. | |
104 | */ | |
105 | logfile = qatomic_rcu_read((void **)&global_file); | |
106 | if (!logfile) { | |
107 | rcu_read_unlock(); | |
108 | return NULL; | |
109 | } | |
110 | } | |
c59fe6e5 | 111 | } |
4e51069d RH |
112 | |
113 | qemu_flockfile(logfile); | |
30f5a73a | 114 | return logfile; |
c59fe6e5 RH |
115 | } |
116 | ||
30f5a73a | 117 | void qemu_log_unlock(FILE *logfile) |
c59fe6e5 | 118 | { |
30f5a73a RH |
119 | if (logfile) { |
120 | fflush(logfile); | |
121 | qemu_funlockfile(logfile); | |
4e51069d RH |
122 | if (!log_per_thread) { |
123 | rcu_read_unlock(); | |
124 | } | |
c59fe6e5 | 125 | } |
c59fe6e5 RH |
126 | } |
127 | ||
3c06a417 | 128 | void qemu_log(const char *fmt, ...) |
eeacee4d | 129 | { |
095e9855 | 130 | FILE *f = qemu_log_trylock(); |
095e9855 | 131 | if (f) { |
bdfb460e | 132 | va_list ap; |
095e9855 | 133 | |
bdfb460e | 134 | va_start(ap, fmt); |
3c06a417 | 135 | vfprintf(f, fmt, ap); |
bdfb460e | 136 | va_end(ap); |
095e9855 | 137 | qemu_log_unlock(f); |
eeacee4d | 138 | } |
eeacee4d BS |
139 | } |
140 | ||
702979f7 | 141 | static void __attribute__((__constructor__)) startup(void) |
b8121fe7 | 142 | { |
702979f7 | 143 | qemu_mutex_init(&global_mutex); |
b8121fe7 RF |
144 | } |
145 | ||
d5f55fff | 146 | static void rcu_close_file(RCUCloseFILE *r) |
7606488c | 147 | { |
30f5a73a | 148 | fclose(r->fd); |
d5f55fff | 149 | g_free(r); |
7606488c RF |
150 | } |
151 | ||
4e51069d RH |
152 | /** |
153 | * valid_filename_template: | |
154 | * | |
155 | * Validate the filename template. Require %d if per_thread, allow it | |
156 | * otherwise; require no other % within the template. | |
157 | */ | |
158 | ||
159 | typedef enum { | |
160 | vft_error, | |
161 | vft_stderr, | |
162 | vft_strdup, | |
163 | vft_pid_printf, | |
164 | } ValidFilenameTemplateResult; | |
165 | ||
166 | static ValidFilenameTemplateResult | |
167 | valid_filename_template(const char *filename, bool per_thread, Error **errp) | |
168 | { | |
169 | if (filename) { | |
170 | char *pidstr = strstr(filename, "%"); | |
171 | ||
172 | if (pidstr) { | |
173 | /* We only accept one %d, no other format strings */ | |
174 | if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { | |
175 | error_setg(errp, "Bad logfile template: %s", filename); | |
176 | return 0; | |
177 | } | |
178 | return per_thread ? vft_strdup : vft_pid_printf; | |
179 | } | |
180 | } | |
181 | if (per_thread) { | |
182 | error_setg(errp, "Filename template with '%%d' required for 'tid'"); | |
183 | return vft_error; | |
184 | } | |
185 | return filename ? vft_strdup : vft_stderr; | |
186 | } | |
187 | ||
5726c27f | 188 | /* enable or disable low levels log */ |
144539d3 RH |
189 | static bool qemu_set_log_internal(const char *filename, bool changed_name, |
190 | int log_flags, Error **errp) | |
5726c27f | 191 | { |
144539d3 | 192 | bool need_to_open_file; |
beab3447 | 193 | bool daemonized; |
4e51069d | 194 | bool per_thread; |
30f5a73a | 195 | FILE *logfile; |
7606488c | 196 | |
702979f7 | 197 | QEMU_LOCK_GUARD(&global_mutex); |
8ae58d60 | 198 | logfile = global_file; |
144539d3 | 199 | |
4e51069d RH |
200 | per_thread = log_flags & LOG_PER_THREAD; |
201 | ||
144539d3 RH |
202 | if (changed_name) { |
203 | char *newname = NULL; | |
204 | ||
205 | /* | |
4e51069d RH |
206 | * Once threads start opening their own log files, we have no |
207 | * easy mechanism to tell them all to close and re-open. | |
208 | * There seems little cause to do so either -- this option | |
209 | * will most often be used at user-only startup. | |
144539d3 | 210 | */ |
4e51069d RH |
211 | if (log_per_thread) { |
212 | error_setg(errp, "Cannot change log filename after setting 'tid'"); | |
213 | return false; | |
214 | } | |
215 | ||
216 | switch (valid_filename_template(filename, per_thread, errp)) { | |
217 | case vft_error: | |
218 | return false; | |
219 | case vft_stderr: | |
220 | break; | |
221 | case vft_strdup: | |
222 | newname = g_strdup(filename); | |
223 | break; | |
224 | case vft_pid_printf: | |
225 | newname = g_strdup_printf(filename, getpid()); | |
226 | break; | |
144539d3 RH |
227 | } |
228 | ||
42266464 RH |
229 | g_free(global_filename); |
230 | global_filename = newname; | |
144539d3 | 231 | filename = newname; |
144539d3 | 232 | } else { |
42266464 | 233 | filename = global_filename; |
4e51069d RH |
234 | if (per_thread && |
235 | valid_filename_template(filename, true, errp) == vft_error) { | |
236 | return false; | |
237 | } | |
144539d3 RH |
238 | } |
239 | ||
4e51069d RH |
240 | /* Once the per-thread flag is set, it cannot be unset. */ |
241 | if (per_thread) { | |
242 | log_per_thread = true; | |
243 | } | |
244 | /* The flag itself is not relevant for need_to_open_file. */ | |
245 | log_flags &= ~LOG_PER_THREAD; | |
ed7f5f1d | 246 | #ifdef CONFIG_TRACE_LOG |
144539d3 | 247 | log_flags |= LOG_TRACE; |
ed7f5f1d | 248 | #endif |
144539d3 RH |
249 | qemu_loglevel = log_flags; |
250 | ||
045e8861 RF |
251 | /* |
252 | * In all cases we only log if qemu_loglevel is set. | |
253 | * Also: | |
4e51069d | 254 | * If per-thread, open the file for each thread in qemu_log_lock. |
045e8861 | 255 | * If not daemonized we will always log either to stderr |
42266464 RH |
256 | * or to a file (if there is a filename). |
257 | * If we are daemonized, we will only log if there is a filename. | |
045e8861 | 258 | */ |
beab3447 | 259 | daemonized = is_daemonized(); |
4e51069d | 260 | need_to_open_file = log_flags && !per_thread && (!daemonized || filename); |
144539d3 | 261 | |
92b24cb7 | 262 | if (logfile && (!need_to_open_file || changed_name)) { |
8ae58d60 | 263 | qatomic_rcu_set(&global_file, NULL); |
30f5a73a RH |
264 | if (logfile != stderr) { |
265 | RCUCloseFILE *r = g_new0(RCUCloseFILE, 1); | |
266 | r->fd = logfile; | |
267 | call_rcu(r, rcu_close_file, rcu); | |
268 | } | |
92b24cb7 | 269 | logfile = NULL; |
144539d3 | 270 | } |
92b24cb7 | 271 | |
144539d3 | 272 | if (!logfile && need_to_open_file) { |
144539d3 | 273 | if (filename) { |
30f5a73a RH |
274 | logfile = fopen(filename, log_append ? "a" : "w"); |
275 | if (!logfile) { | |
c5955f4f | 276 | error_setg_errno(errp, errno, "Error opening logfile %s", |
144539d3 | 277 | filename); |
c5955f4f | 278 | return false; |
989b697d | 279 | } |
96c33a45 | 280 | /* In case we are a daemon redirect stderr to logfile */ |
beab3447 | 281 | if (daemonized) { |
30f5a73a RH |
282 | dup2(fileno(logfile), STDERR_FILENO); |
283 | fclose(logfile); | |
d5f55fff | 284 | /* This will skip closing logfile in rcu_close_file. */ |
30f5a73a | 285 | logfile = stderr; |
96c33a45 | 286 | } |
989b697d PM |
287 | } else { |
288 | /* Default to stderr if no log file specified */ | |
beab3447 | 289 | assert(!daemonized); |
30f5a73a | 290 | logfile = stderr; |
5726c27f | 291 | } |
3437e545 | 292 | |
54ee5b3d | 293 | log_append = 1; |
144539d3 | 294 | |
8ae58d60 | 295 | qatomic_rcu_set(&global_file, logfile); |
5726c27f | 296 | } |
c5955f4f | 297 | return true; |
5726c27f | 298 | } |
f2937a33 | 299 | |
144539d3 | 300 | bool qemu_set_log(int log_flags, Error **errp) |
5726c27f | 301 | { |
144539d3 RH |
302 | return qemu_set_log_internal(NULL, false, log_flags, errp); |
303 | } | |
f6880b7f | 304 | |
144539d3 RH |
305 | bool qemu_set_log_filename(const char *filename, Error **errp) |
306 | { | |
307 | return qemu_set_log_internal(filename, true, qemu_loglevel, errp); | |
308 | } | |
e144a605 | 309 | |
144539d3 RH |
310 | bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp) |
311 | { | |
312 | return qemu_set_log_internal(name, true, flags, errp); | |
5726c27f BS |
313 | } |
314 | ||
3514552e AB |
315 | /* Returns true if addr is in our debug filter or no filter defined |
316 | */ | |
317 | bool qemu_log_in_addr_range(uint64_t addr) | |
318 | { | |
319 | if (debug_regions) { | |
320 | int i = 0; | |
321 | for (i = 0; i < debug_regions->len; i++) { | |
58e19e6e | 322 | Range *range = &g_array_index(debug_regions, Range, i); |
a0efbf16 | 323 | if (range_contains(range, addr)) { |
3514552e AB |
324 | return true; |
325 | } | |
326 | } | |
327 | return false; | |
328 | } else { | |
329 | return true; | |
330 | } | |
331 | } | |
332 | ||
333 | ||
bd6fee9f | 334 | void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) |
3514552e AB |
335 | { |
336 | gchar **ranges = g_strsplit(filter_spec, ",", 0); | |
bd6fee9f | 337 | int i; |
2ec62fae MA |
338 | |
339 | if (debug_regions) { | |
340 | g_array_unref(debug_regions); | |
341 | debug_regions = NULL; | |
342 | } | |
343 | ||
bd6fee9f MA |
344 | debug_regions = g_array_sized_new(FALSE, FALSE, |
345 | sizeof(Range), g_strv_length(ranges)); | |
346 | for (i = 0; ranges[i]; i++) { | |
347 | const char *r = ranges[i]; | |
348 | const char *range_op, *r2, *e; | |
58e19e6e | 349 | uint64_t r1val, r2val, lob, upb; |
bd6fee9f MA |
350 | struct Range range; |
351 | ||
352 | range_op = strstr(r, "-"); | |
353 | r2 = range_op ? range_op + 1 : NULL; | |
354 | if (!range_op) { | |
355 | range_op = strstr(r, "+"); | |
356 | r2 = range_op ? range_op + 1 : NULL; | |
357 | } | |
358 | if (!range_op) { | |
359 | range_op = strstr(r, ".."); | |
360 | r2 = range_op ? range_op + 2 : NULL; | |
361 | } | |
362 | if (!range_op) { | |
363 | error_setg(errp, "Bad range specifier"); | |
364 | goto out; | |
365 | } | |
366 | ||
b30d1886 | 367 | if (qemu_strtou64(r, &e, 0, &r1val) |
bd6fee9f MA |
368 | || e != range_op) { |
369 | error_setg(errp, "Invalid number to the left of %.*s", | |
370 | (int)(r2 - range_op), range_op); | |
371 | goto out; | |
372 | } | |
b30d1886 | 373 | if (qemu_strtou64(r2, NULL, 0, &r2val)) { |
bd6fee9f MA |
374 | error_setg(errp, "Invalid number to the right of %.*s", |
375 | (int)(r2 - range_op), range_op); | |
376 | goto out; | |
377 | } | |
bd6fee9f MA |
378 | |
379 | switch (*range_op) { | |
380 | case '+': | |
58e19e6e MA |
381 | lob = r1val; |
382 | upb = r1val + r2val - 1; | |
bd6fee9f MA |
383 | break; |
384 | case '-': | |
58e19e6e MA |
385 | upb = r1val; |
386 | lob = r1val - (r2val - 1); | |
bd6fee9f MA |
387 | break; |
388 | case '.': | |
58e19e6e MA |
389 | lob = r1val; |
390 | upb = r2val; | |
bd6fee9f MA |
391 | break; |
392 | default: | |
393 | g_assert_not_reached(); | |
3514552e | 394 | } |
58eeb83c | 395 | if (lob > upb) { |
58e19e6e MA |
396 | error_setg(errp, "Invalid range"); |
397 | goto out; | |
398 | } | |
a0efbf16 | 399 | range_set_bounds(&range, lob, upb); |
bd6fee9f | 400 | g_array_append_val(debug_regions, range); |
3514552e | 401 | } |
bd6fee9f MA |
402 | out: |
403 | g_strfreev(ranges); | |
3514552e AB |
404 | } |
405 | ||
38dad9e5 | 406 | const QEMULogItem qemu_log_items[] = { |
5726c27f BS |
407 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
408 | "show generated host assembly code for each compiled TB" }, | |
409 | { CPU_LOG_TB_IN_ASM, "in_asm", | |
410 | "show target assembly code for each compiled TB" }, | |
411 | { CPU_LOG_TB_OP, "op", | |
412 | "show micro ops for each compiled TB" }, | |
413 | { CPU_LOG_TB_OP_OPT, "op_opt", | |
5a18407f RH |
414 | "show micro ops after optimization" }, |
415 | { CPU_LOG_TB_OP_IND, "op_ind", | |
416 | "show micro ops before indirect lowering" }, | |
5726c27f BS |
417 | { CPU_LOG_INT, "int", |
418 | "show interrupts/exceptions in short format" }, | |
419 | { CPU_LOG_EXEC, "exec", | |
420 | "show trace before each executed TB (lots of logs)" }, | |
421 | { CPU_LOG_TB_CPU, "cpu", | |
54195736 | 422 | "show CPU registers before entering a TB (lots of logs)" }, |
ae765180 PM |
423 | { CPU_LOG_TB_FPU, "fpu", |
424 | "include FPU registers in the 'cpu' logging" }, | |
339aaf5b AP |
425 | { CPU_LOG_MMU, "mmu", |
426 | "log MMU-related activities" }, | |
5726c27f | 427 | { CPU_LOG_PCALL, "pcall", |
3437e545 | 428 | "x86 only: show protected mode far calls/returns/exceptions" }, |
5726c27f | 429 | { CPU_LOG_RESET, "cpu_reset", |
dbfe1b6a | 430 | "show CPU state before CPU resets" }, |
dafdf1ab BS |
431 | { LOG_UNIMP, "unimp", |
432 | "log unimplemented functionality" }, | |
e54eba19 PM |
433 | { LOG_GUEST_ERROR, "guest_errors", |
434 | "log when the guest OS does something invalid (eg accessing a\n" | |
435 | "non-existent register)" }, | |
13829020 PB |
436 | { CPU_LOG_PAGE, "page", |
437 | "dump pages at beginning of user mode emulation" }, | |
89a82cd4 RH |
438 | { CPU_LOG_TB_NOCHAIN, "nochain", |
439 | "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" | |
440 | "complete traces" }, | |
ca76a669 AB |
441 | #ifdef CONFIG_PLUGIN |
442 | { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"}, | |
443 | #endif | |
4b25a506 JK |
444 | { LOG_STRACE, "strace", |
445 | "log every user-mode syscall, its input, and its result" }, | |
4e51069d RH |
446 | { LOG_PER_THREAD, "tid", |
447 | "open a separate log file per thread; filename must contain '%d'" }, | |
5726c27f BS |
448 | { 0, NULL, NULL }, |
449 | }; | |
450 | ||
5726c27f | 451 | /* takes a comma separated list of log masks. Return 0 if error. */ |
4fde1eba | 452 | int qemu_str_to_log_mask(const char *str) |
5726c27f | 453 | { |
38dad9e5 | 454 | const QEMULogItem *item; |
89d0a64f DB |
455 | int mask = 0; |
456 | char **parts = g_strsplit(str, ",", 0); | |
457 | char **tmp; | |
5726c27f | 458 | |
89d0a64f DB |
459 | for (tmp = parts; tmp && *tmp; tmp++) { |
460 | if (g_str_equal(*tmp, "all")) { | |
38dad9e5 | 461 | for (item = qemu_log_items; item->mask != 0; item++) { |
5726c27f BS |
462 | mask |= item->mask; |
463 | } | |
c84ea00d | 464 | #ifdef CONFIG_TRACE_LOG |
89d0a64f DB |
465 | } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') { |
466 | trace_enable_events((*tmp) + 6); | |
c84ea00d PB |
467 | mask |= LOG_TRACE; |
468 | #endif | |
5726c27f | 469 | } else { |
38dad9e5 | 470 | for (item = qemu_log_items; item->mask != 0; item++) { |
89d0a64f | 471 | if (g_str_equal(*tmp, item->name)) { |
5726c27f BS |
472 | goto found; |
473 | } | |
474 | } | |
89d0a64f | 475 | goto error; |
c84ea00d PB |
476 | found: |
477 | mask |= item->mask; | |
5726c27f | 478 | } |
5726c27f | 479 | } |
89d0a64f DB |
480 | |
481 | g_strfreev(parts); | |
5726c27f | 482 | return mask; |
89d0a64f DB |
483 | |
484 | error: | |
485 | g_strfreev(parts); | |
486 | return 0; | |
5726c27f | 487 | } |
59a6fa6e PM |
488 | |
489 | void qemu_print_log_usage(FILE *f) | |
490 | { | |
38dad9e5 | 491 | const QEMULogItem *item; |
59a6fa6e | 492 | fprintf(f, "Log items (comma separated):\n"); |
38dad9e5 | 493 | for (item = qemu_log_items; item->mask != 0; item++) { |
c84ea00d | 494 | fprintf(f, "%-15s %s\n", item->name, item->help); |
59a6fa6e | 495 | } |
c84ea00d PB |
496 | #ifdef CONFIG_TRACE_LOG |
497 | fprintf(f, "trace:PATTERN enable trace events\n"); | |
498 | fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); | |
499 | #endif | |
59a6fa6e | 500 | } |