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