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