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