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