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