blob: 53b4f6c58eb9ede6a311c02af05335fdba005d07 [file] [log] [blame]
Blue Swirl5726c272012-06-03 15:03:23 +00001/*
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
Chetan Pant61f3c912020-10-23 12:44:24 +00009 * version 2.1 of the License, or (at your option) any later version.
Blue Swirl5726c272012-06-03 15:03:23 +000010 *
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
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/log.h"
Alex Bennée35145522016-03-15 14:30:20 +000022#include "qemu/range.h"
23#include "qemu/error-report.h"
Markus Armbrusterbd6fee92016-06-15 19:27:15 +020024#include "qapi/error.h"
Alex Bennée35145522016-03-15 14:30:20 +000025#include "qemu/cutils.h"
Paolo Bonzinic84ea002016-01-07 16:55:32 +030026#include "trace/control.h"
Robert Foleyb8121fe2019-11-18 16:15:25 -050027#include "qemu/thread.h"
Daniel Brodsky6e8a3552020-04-03 21:21:08 -070028#include "qemu/lockable.h"
Richard Henderson7fc493f2022-04-17 11:30:05 -070029#include "qemu/rcu.h"
Richard Henderson4e510692022-04-17 11:30:19 -070030#ifdef CONFIG_LINUX
31#include <sys/syscall.h>
32#endif
Richard Henderson7fc493f2022-04-17 11:30:05 -070033
34
Richard Hendersond5f55ff2022-04-17 11:30:17 -070035typedef struct RCUCloseFILE {
Richard Henderson7fc493f2022-04-17 11:30:05 -070036 struct rcu_head rcu;
37 FILE *fd;
Richard Hendersond5f55ff2022-04-17 11:30:17 -070038} RCUCloseFILE;
Blue Swirl5726c272012-06-03 15:03:23 +000039
Richard Henderson702979f2022-04-17 11:30:14 -070040/* Mutex covering the other global_* variables. */
41static QemuMutex global_mutex;
Richard Henderson42266462022-04-17 11:30:12 -070042static char *global_filename;
Richard Henderson30f5a732022-04-17 11:30:18 -070043static FILE *global_file;
Richard Henderson4e510692022-04-17 11:30:19 -070044static __thread FILE *thread_file;
Greg Kurzeff3de52022-10-21 12:57:34 +020045static __thread Notifier qemu_log_thread_cleanup_notifier;
Richard Henderson702979f2022-04-17 11:30:14 -070046
Blue Swirleeacee42012-06-03 16:35:32 +000047int qemu_loglevel;
Richard Henderson4e510692022-04-17 11:30:19 -070048static bool log_per_thread;
Alex Bennée35145522016-03-15 14:30:20 +000049static GArray *debug_regions;
Blue Swirl5726c272012-06-03 15:03:23 +000050
Richard Henderson7fc493f2022-04-17 11:30:05 -070051/* Returns true if qemu_log() will really write somewhere. */
52bool qemu_log_enabled(void)
53{
Richard Henderson4e510692022-04-17 11:30:19 -070054 return log_per_thread || qatomic_read(&global_file) != NULL;
Richard Henderson7fc493f2022-04-17 11:30:05 -070055}
56
57/* Returns true if qemu_log() will write somewhere other than stderr. */
58bool qemu_log_separate(void)
59{
Richard Henderson4e510692022-04-17 11:30:19 -070060 if (log_per_thread) {
61 return true;
62 } else {
63 FILE *logfile = qatomic_read(&global_file);
64 return logfile && logfile != stderr;
65 }
66}
67
68static int log_thread_id(void)
69{
70#ifdef CONFIG_GETTID
71 return gettid();
72#elif defined(SYS_gettid)
73 return syscall(SYS_gettid);
74#else
75 static int counter;
76 return qatomic_fetch_inc(&counter);
77#endif
Richard Henderson7fc493f2022-04-17 11:30:05 -070078}
79
Greg Kurzeff3de52022-10-21 12:57:34 +020080static void qemu_log_thread_cleanup(Notifier *n, void *unused)
81{
Greg Kurz9b063b72022-11-08 15:00:32 +010082 if (thread_file != stderr) {
83 fclose(thread_file);
84 thread_file = NULL;
85 }
Greg Kurzeff3de52022-10-21 12:57:34 +020086}
87
Richard Hendersonc59fe6e2022-04-17 11:29:46 -070088/* Lock/unlock output. */
89
Greg Kurz9b063b72022-11-08 15:00:32 +010090static FILE *qemu_log_trylock_with_err(Error **errp)
Richard Hendersonc59fe6e2022-04-17 11:29:46 -070091{
Richard Henderson30f5a732022-04-17 11:30:18 -070092 FILE *logfile;
Richard Hendersonc60f5992022-04-17 11:29:47 -070093
Richard Henderson4e510692022-04-17 11:30:19 -070094 logfile = thread_file;
95 if (!logfile) {
96 if (log_per_thread) {
97 g_autofree char *filename
98 = g_strdup_printf(global_filename, log_thread_id());
99 logfile = fopen(filename, "w");
100 if (!logfile) {
Greg Kurz9b063b72022-11-08 15:00:32 +0100101 error_setg_errno(errp, errno,
102 "Error opening logfile %s for thread %d",
103 filename, log_thread_id());
Richard Henderson4e510692022-04-17 11:30:19 -0700104 return NULL;
105 }
106 thread_file = logfile;
Greg Kurzeff3de52022-10-21 12:57:34 +0200107 qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup;
108 qemu_thread_atexit_add(&qemu_log_thread_cleanup_notifier);
Richard Henderson4e510692022-04-17 11:30:19 -0700109 } else {
110 rcu_read_lock();
111 /*
112 * FIXME: typeof_strip_qual, as used by qatomic_rcu_read,
113 * does not work with pointers to undefined structures,
114 * such as we have with struct _IO_FILE and musl libc.
115 * Since all we want is a read of a pointer, cast to void**,
116 * which does work with typeof_strip_qual.
117 */
118 logfile = qatomic_rcu_read((void **)&global_file);
119 if (!logfile) {
120 rcu_read_unlock();
121 return NULL;
122 }
123 }
Richard Hendersonc59fe6e2022-04-17 11:29:46 -0700124 }
Richard Henderson4e510692022-04-17 11:30:19 -0700125
126 qemu_flockfile(logfile);
Richard Henderson30f5a732022-04-17 11:30:18 -0700127 return logfile;
Richard Hendersonc59fe6e2022-04-17 11:29:46 -0700128}
129
Greg Kurz9b063b72022-11-08 15:00:32 +0100130FILE *qemu_log_trylock(void)
131{
132 return qemu_log_trylock_with_err(NULL);
133}
134
Richard Henderson30f5a732022-04-17 11:30:18 -0700135void qemu_log_unlock(FILE *logfile)
Richard Hendersonc59fe6e2022-04-17 11:29:46 -0700136{
Richard Henderson30f5a732022-04-17 11:30:18 -0700137 if (logfile) {
138 fflush(logfile);
139 qemu_funlockfile(logfile);
Richard Henderson4e510692022-04-17 11:30:19 -0700140 if (!log_per_thread) {
141 rcu_read_unlock();
142 }
Richard Hendersonc59fe6e2022-04-17 11:29:46 -0700143 }
Richard Hendersonc59fe6e2022-04-17 11:29:46 -0700144}
145
Richard Henderson3c06a412022-04-17 11:29:57 -0700146void qemu_log(const char *fmt, ...)
Blue Swirleeacee42012-06-03 16:35:32 +0000147{
Richard Henderson095e9852022-04-17 11:29:56 -0700148 FILE *f = qemu_log_trylock();
Richard Henderson095e9852022-04-17 11:29:56 -0700149 if (f) {
Richard Hendersonbdfb4602016-06-23 19:15:55 -0700150 va_list ap;
Richard Henderson095e9852022-04-17 11:29:56 -0700151
Richard Hendersonbdfb4602016-06-23 19:15:55 -0700152 va_start(ap, fmt);
Richard Henderson3c06a412022-04-17 11:29:57 -0700153 vfprintf(f, fmt, ap);
Richard Hendersonbdfb4602016-06-23 19:15:55 -0700154 va_end(ap);
Richard Henderson095e9852022-04-17 11:29:56 -0700155 qemu_log_unlock(f);
Blue Swirleeacee42012-06-03 16:35:32 +0000156 }
Blue Swirleeacee42012-06-03 16:35:32 +0000157}
158
Richard Henderson702979f2022-04-17 11:30:14 -0700159static void __attribute__((__constructor__)) startup(void)
Robert Foleyb8121fe2019-11-18 16:15:25 -0500160{
Richard Henderson702979f2022-04-17 11:30:14 -0700161 qemu_mutex_init(&global_mutex);
Robert Foleyb8121fe2019-11-18 16:15:25 -0500162}
163
Richard Hendersond5f55ff2022-04-17 11:30:17 -0700164static void rcu_close_file(RCUCloseFILE *r)
Robert Foley76064882019-11-18 16:15:27 -0500165{
Richard Henderson30f5a732022-04-17 11:30:18 -0700166 fclose(r->fd);
Richard Hendersond5f55ff2022-04-17 11:30:17 -0700167 g_free(r);
Robert Foley76064882019-11-18 16:15:27 -0500168}
169
Richard Henderson4e510692022-04-17 11:30:19 -0700170/**
171 * valid_filename_template:
172 *
173 * Validate the filename template. Require %d if per_thread, allow it
174 * otherwise; require no other % within the template.
175 */
176
177typedef enum {
178 vft_error,
179 vft_stderr,
180 vft_strdup,
181 vft_pid_printf,
182} ValidFilenameTemplateResult;
183
184static ValidFilenameTemplateResult
185valid_filename_template(const char *filename, bool per_thread, Error **errp)
186{
187 if (filename) {
188 char *pidstr = strstr(filename, "%");
189
190 if (pidstr) {
191 /* We only accept one %d, no other format strings */
192 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
193 error_setg(errp, "Bad logfile template: %s", filename);
194 return 0;
195 }
196 return per_thread ? vft_strdup : vft_pid_printf;
197 }
198 }
199 if (per_thread) {
200 error_setg(errp, "Filename template with '%%d' required for 'tid'");
201 return vft_error;
202 }
203 return filename ? vft_strdup : vft_stderr;
204}
205
Blue Swirl5726c272012-06-03 15:03:23 +0000206/* enable or disable low levels log */
Richard Henderson144539d2022-04-17 11:30:07 -0700207static bool qemu_set_log_internal(const char *filename, bool changed_name,
208 int log_flags, Error **errp)
Blue Swirl5726c272012-06-03 15:03:23 +0000209{
Richard Henderson144539d2022-04-17 11:30:07 -0700210 bool need_to_open_file;
Richard Hendersonbeab3442022-04-17 11:30:15 -0700211 bool daemonized;
Richard Henderson4e510692022-04-17 11:30:19 -0700212 bool per_thread;
Richard Henderson30f5a732022-04-17 11:30:18 -0700213 FILE *logfile;
Robert Foley76064882019-11-18 16:15:27 -0500214
Richard Henderson702979f2022-04-17 11:30:14 -0700215 QEMU_LOCK_GUARD(&global_mutex);
Richard Henderson8ae58d62022-04-17 11:30:13 -0700216 logfile = global_file;
Richard Henderson144539d2022-04-17 11:30:07 -0700217
Greg Kurz479b3502022-11-04 13:00:58 +0100218 /* The per-thread flag is immutable. */
219 if (log_per_thread) {
220 log_flags |= LOG_PER_THREAD;
Greg Kurz524fc732022-11-04 13:00:59 +0100221 } else {
222 if (global_filename) {
223 log_flags &= ~LOG_PER_THREAD;
224 }
Greg Kurz479b3502022-11-04 13:00:58 +0100225 }
226
Richard Henderson4e510692022-04-17 11:30:19 -0700227 per_thread = log_flags & LOG_PER_THREAD;
228
Richard Henderson144539d2022-04-17 11:30:07 -0700229 if (changed_name) {
230 char *newname = NULL;
231
232 /*
Richard Henderson4e510692022-04-17 11:30:19 -0700233 * Once threads start opening their own log files, we have no
234 * easy mechanism to tell them all to close and re-open.
235 * There seems little cause to do so either -- this option
236 * will most often be used at user-only startup.
Richard Henderson144539d2022-04-17 11:30:07 -0700237 */
Richard Henderson4e510692022-04-17 11:30:19 -0700238 if (log_per_thread) {
239 error_setg(errp, "Cannot change log filename after setting 'tid'");
240 return false;
241 }
Richard Henderson144539d2022-04-17 11:30:07 -0700242
Richard Henderson4e510692022-04-17 11:30:19 -0700243 switch (valid_filename_template(filename, per_thread, errp)) {
244 case vft_error:
245 return false;
246 case vft_stderr:
247 break;
248 case vft_strdup:
249 newname = g_strdup(filename);
250 break;
251 case vft_pid_printf:
252 newname = g_strdup_printf(filename, getpid());
253 break;
Richard Henderson144539d2022-04-17 11:30:07 -0700254 }
255
Richard Henderson42266462022-04-17 11:30:12 -0700256 g_free(global_filename);
257 global_filename = newname;
Richard Henderson144539d2022-04-17 11:30:07 -0700258 filename = newname;
Richard Henderson144539d2022-04-17 11:30:07 -0700259 } else {
Richard Henderson42266462022-04-17 11:30:12 -0700260 filename = global_filename;
Richard Henderson4e510692022-04-17 11:30:19 -0700261 if (per_thread &&
262 valid_filename_template(filename, true, errp) == vft_error) {
263 return false;
264 }
Richard Henderson144539d2022-04-17 11:30:07 -0700265 }
266
Richard Henderson4e510692022-04-17 11:30:19 -0700267 /* Once the per-thread flag is set, it cannot be unset. */
268 if (per_thread) {
269 log_per_thread = true;
270 }
271 /* The flag itself is not relevant for need_to_open_file. */
272 log_flags &= ~LOG_PER_THREAD;
Paolo Bonzinied7f5f12016-01-07 16:55:30 +0300273#ifdef CONFIG_TRACE_LOG
Richard Henderson144539d2022-04-17 11:30:07 -0700274 log_flags |= LOG_TRACE;
Paolo Bonzinied7f5f12016-01-07 16:55:30 +0300275#endif
Richard Henderson144539d2022-04-17 11:30:07 -0700276 qemu_loglevel = log_flags;
277
Richard Hendersonbeab3442022-04-17 11:30:15 -0700278 daemonized = is_daemonized();
Greg Kurz9b063b72022-11-08 15:00:32 +0100279 need_to_open_file = false;
280 if (!daemonized) {
281 /*
282 * If not daemonized we only log if qemu_loglevel is set, either to
283 * stderr or to a file (if there is a filename).
284 * If per-thread, open the file for each thread in qemu_log_trylock().
285 */
286 need_to_open_file = qemu_loglevel && !log_per_thread;
287 } else {
288 /*
289 * If we are daemonized, we will only log if there is a filename.
290 */
291 need_to_open_file = filename != NULL;
292 }
Richard Henderson144539d2022-04-17 11:30:07 -0700293
Paolo Bonzini59bde212022-11-08 15:00:31 +0100294 if (logfile) {
295 fflush(logfile);
296 if (changed_name && logfile != stderr) {
Richard Henderson30f5a732022-04-17 11:30:18 -0700297 RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
298 r->fd = logfile;
Paolo Bonzini59bde212022-11-08 15:00:31 +0100299 qatomic_rcu_set(&global_file, NULL);
Richard Henderson30f5a732022-04-17 11:30:18 -0700300 call_rcu(r, rcu_close_file, rcu);
Paolo Bonzini59bde212022-11-08 15:00:31 +0100301 logfile = NULL;
Richard Henderson30f5a732022-04-17 11:30:18 -0700302 }
Richard Henderson144539d2022-04-17 11:30:07 -0700303 }
Richard Henderson92b24cb2022-04-17 11:30:16 -0700304
Greg Kurz9b063b72022-11-08 15:00:32 +0100305 if (log_per_thread && daemonized) {
306 logfile = thread_file;
307 }
308
Richard Henderson144539d2022-04-17 11:30:07 -0700309 if (!logfile && need_to_open_file) {
Richard Henderson144539d2022-04-17 11:30:07 -0700310 if (filename) {
Greg Kurz9b063b72022-11-08 15:00:32 +0100311 if (log_per_thread) {
312 logfile = qemu_log_trylock_with_err(errp);
313 if (!logfile) {
314 return false;
315 }
316 qemu_log_unlock(logfile);
317 } else {
318 logfile = fopen(filename, "w");
319 if (!logfile) {
320 error_setg_errno(errp, errno, "Error opening logfile %s",
321 filename);
322 return false;
323 }
Peter Maydell989b6972013-02-26 17:52:40 +0000324 }
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200325 /* In case we are a daemon redirect stderr to logfile */
Richard Hendersonbeab3442022-04-17 11:30:15 -0700326 if (daemonized) {
Richard Henderson30f5a732022-04-17 11:30:18 -0700327 dup2(fileno(logfile), STDERR_FILENO);
328 fclose(logfile);
Greg Kurz9b063b72022-11-08 15:00:32 +0100329 /*
330 * This will skip closing logfile in rcu_close_file()
331 * or qemu_log_thread_cleanup().
332 */
Richard Henderson30f5a732022-04-17 11:30:18 -0700333 logfile = stderr;
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200334 }
Peter Maydell989b6972013-02-26 17:52:40 +0000335 } else {
336 /* Default to stderr if no log file specified */
Richard Hendersonbeab3442022-04-17 11:30:15 -0700337 assert(!daemonized);
Richard Henderson30f5a732022-04-17 11:30:18 -0700338 logfile = stderr;
Blue Swirl5726c272012-06-03 15:03:23 +0000339 }
Blue Swirl3437e542012-07-07 14:40:18 +0000340
Greg Kurz9b063b72022-11-08 15:00:32 +0100341 if (log_per_thread && daemonized) {
342 thread_file = logfile;
343 } else {
344 qatomic_rcu_set(&global_file, logfile);
345 }
Blue Swirl5726c272012-06-03 15:03:23 +0000346 }
Richard Hendersonc5955f42022-04-17 11:29:44 -0700347 return true;
Blue Swirl5726c272012-06-03 15:03:23 +0000348}
Paolo Bonzinif2937a32015-12-04 13:12:57 +0100349
Richard Henderson144539d2022-04-17 11:30:07 -0700350bool qemu_set_log(int log_flags, Error **errp)
351{
352 return qemu_set_log_internal(NULL, false, log_flags, errp);
353}
354
Richard Hendersone2c7c6a2022-04-17 11:29:43 -0700355bool qemu_set_log_filename(const char *filename, Error **errp)
Blue Swirl5726c272012-06-03 15:03:23 +0000356{
Richard Henderson144539d2022-04-17 11:30:07 -0700357 return qemu_set_log_internal(filename, true, qemu_loglevel, errp);
358}
Alex Bennéef6880b72016-03-15 14:30:23 +0000359
Richard Henderson144539d2022-04-17 11:30:07 -0700360bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp)
361{
362 return qemu_set_log_internal(name, true, flags, errp);
Blue Swirl5726c272012-06-03 15:03:23 +0000363}
364
Alex Bennée35145522016-03-15 14:30:20 +0000365/* Returns true if addr is in our debug filter or no filter defined
366 */
367bool qemu_log_in_addr_range(uint64_t addr)
368{
369 if (debug_regions) {
370 int i = 0;
371 for (i = 0; i < debug_regions->len; i++) {
Markus Armbruster58e19e62016-07-01 13:47:46 +0200372 Range *range = &g_array_index(debug_regions, Range, i);
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200373 if (range_contains(range, addr)) {
Alex Bennée35145522016-03-15 14:30:20 +0000374 return true;
375 }
376 }
377 return false;
378 } else {
379 return true;
380 }
381}
382
383
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200384void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
Alex Bennée35145522016-03-15 14:30:20 +0000385{
386 gchar **ranges = g_strsplit(filter_spec, ",", 0);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200387 int i;
Markus Armbruster2ec62fa2016-06-15 19:27:14 +0200388
389 if (debug_regions) {
390 g_array_unref(debug_regions);
391 debug_regions = NULL;
392 }
393
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200394 debug_regions = g_array_sized_new(FALSE, FALSE,
395 sizeof(Range), g_strv_length(ranges));
396 for (i = 0; ranges[i]; i++) {
397 const char *r = ranges[i];
398 const char *range_op, *r2, *e;
Markus Armbruster58e19e62016-07-01 13:47:46 +0200399 uint64_t r1val, r2val, lob, upb;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200400 struct Range range;
Markus Armbruster2ec62fa2016-06-15 19:27:14 +0200401
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200402 range_op = strstr(r, "-");
403 r2 = range_op ? range_op + 1 : NULL;
404 if (!range_op) {
405 range_op = strstr(r, "+");
406 r2 = range_op ? range_op + 1 : NULL;
Alex Bennée35145522016-03-15 14:30:20 +0000407 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200408 if (!range_op) {
409 range_op = strstr(r, "..");
410 r2 = range_op ? range_op + 2 : NULL;
411 }
412 if (!range_op) {
413 error_setg(errp, "Bad range specifier");
414 goto out;
415 }
416
Markus Armbrusterb30d1882017-02-21 21:13:50 +0100417 if (qemu_strtou64(r, &e, 0, &r1val)
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200418 || e != range_op) {
419 error_setg(errp, "Invalid number to the left of %.*s",
420 (int)(r2 - range_op), range_op);
421 goto out;
422 }
Markus Armbrusterb30d1882017-02-21 21:13:50 +0100423 if (qemu_strtou64(r2, NULL, 0, &r2val)) {
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200424 error_setg(errp, "Invalid number to the right of %.*s",
425 (int)(r2 - range_op), range_op);
426 goto out;
427 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200428
429 switch (*range_op) {
430 case '+':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200431 lob = r1val;
432 upb = r1val + r2val - 1;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200433 break;
434 case '-':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200435 upb = r1val;
436 lob = r1val - (r2val - 1);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200437 break;
438 case '.':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200439 lob = r1val;
440 upb = r2val;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200441 break;
442 default:
443 g_assert_not_reached();
444 }
Markus Armbruster58eeb832016-07-01 13:47:49 +0200445 if (lob > upb) {
Markus Armbruster58e19e62016-07-01 13:47:46 +0200446 error_setg(errp, "Invalid range");
447 goto out;
448 }
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200449 range_set_bounds(&range, lob, upb);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200450 g_array_append_val(debug_regions, range);
Alex Bennée35145522016-03-15 14:30:20 +0000451 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200452out:
453 g_strfreev(ranges);
Alex Bennée35145522016-03-15 14:30:20 +0000454}
455
Peter Maydell38dad9e2013-02-11 16:41:25 +0000456const QEMULogItem qemu_log_items[] = {
Blue Swirl5726c272012-06-03 15:03:23 +0000457 { CPU_LOG_TB_OUT_ASM, "out_asm",
458 "show generated host assembly code for each compiled TB" },
459 { CPU_LOG_TB_IN_ASM, "in_asm",
460 "show target assembly code for each compiled TB" },
461 { CPU_LOG_TB_OP, "op",
462 "show micro ops for each compiled TB" },
463 { CPU_LOG_TB_OP_OPT, "op_opt",
Richard Henderson5a184072016-06-23 20:34:33 -0700464 "show micro ops after optimization" },
465 { CPU_LOG_TB_OP_IND, "op_ind",
466 "show micro ops before indirect lowering" },
Blue Swirl5726c272012-06-03 15:03:23 +0000467 { CPU_LOG_INT, "int",
468 "show interrupts/exceptions in short format" },
469 { CPU_LOG_EXEC, "exec",
470 "show trace before each executed TB (lots of logs)" },
471 { CPU_LOG_TB_CPU, "cpu",
Alex Bennée54195732016-03-15 14:30:17 +0000472 "show CPU registers before entering a TB (lots of logs)" },
Peter Maydellae765182018-05-15 14:58:44 +0100473 { CPU_LOG_TB_FPU, "fpu",
474 "include FPU registers in the 'cpu' logging" },
Antony Pavlov339aaf52014-12-13 19:48:18 +0300475 { CPU_LOG_MMU, "mmu",
476 "log MMU-related activities" },
Blue Swirl5726c272012-06-03 15:03:23 +0000477 { CPU_LOG_PCALL, "pcall",
Blue Swirl3437e542012-07-07 14:40:18 +0000478 "x86 only: show protected mode far calls/returns/exceptions" },
Blue Swirl5726c272012-06-03 15:03:23 +0000479 { CPU_LOG_RESET, "cpu_reset",
Thomas Huthdbfe1b62015-01-27 13:11:26 +0100480 "show CPU state before CPU resets" },
Blue Swirldafdf1a2012-06-03 17:04:28 +0000481 { LOG_UNIMP, "unimp",
482 "log unimplemented functionality" },
Peter Maydelle54eba12012-10-18 14:11:35 +0100483 { LOG_GUEST_ERROR, "guest_errors",
484 "log when the guest OS does something invalid (eg accessing a\n"
485 "non-existent register)" },
Paolo Bonzini13829022015-11-13 12:32:19 +0100486 { CPU_LOG_PAGE, "page",
487 "dump pages at beginning of user mode emulation" },
Richard Henderson89a82cd2015-09-16 15:33:53 -0700488 { CPU_LOG_TB_NOCHAIN, "nochain",
489 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
490 "complete traces" },
Alex Bennéeca76a662019-10-11 16:34:05 +0100491#ifdef CONFIG_PLUGIN
BALATON Zoltancb9291e2023-01-19 22:40:33 +0100492 { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins"},
Alex Bennéeca76a662019-10-11 16:34:05 +0100493#endif
Josh Kunz4b25a502020-02-03 18:54:14 -0800494 { LOG_STRACE, "strace",
495 "log every user-mode syscall, its input, and its result" },
Richard Henderson4e510692022-04-17 11:30:19 -0700496 { LOG_PER_THREAD, "tid",
497 "open a separate log file per thread; filename must contain '%d'" },
Blue Swirl5726c272012-06-03 15:03:23 +0000498 { 0, NULL, NULL },
499};
500
Blue Swirl5726c272012-06-03 15:03:23 +0000501/* takes a comma separated list of log masks. Return 0 if error. */
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000502int qemu_str_to_log_mask(const char *str)
Blue Swirl5726c272012-06-03 15:03:23 +0000503{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000504 const QEMULogItem *item;
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100505 int mask = 0;
506 char **parts = g_strsplit(str, ",", 0);
507 char **tmp;
Blue Swirl5726c272012-06-03 15:03:23 +0000508
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100509 for (tmp = parts; tmp && *tmp; tmp++) {
510 if (g_str_equal(*tmp, "all")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000511 for (item = qemu_log_items; item->mask != 0; item++) {
Blue Swirl5726c272012-06-03 15:03:23 +0000512 mask |= item->mask;
513 }
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300514#ifdef CONFIG_TRACE_LOG
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100515 } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
516 trace_enable_events((*tmp) + 6);
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300517 mask |= LOG_TRACE;
518#endif
Blue Swirl5726c272012-06-03 15:03:23 +0000519 } else {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000520 for (item = qemu_log_items; item->mask != 0; item++) {
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100521 if (g_str_equal(*tmp, item->name)) {
Blue Swirl5726c272012-06-03 15:03:23 +0000522 goto found;
523 }
524 }
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100525 goto error;
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300526 found:
527 mask |= item->mask;
Blue Swirl5726c272012-06-03 15:03:23 +0000528 }
Blue Swirl5726c272012-06-03 15:03:23 +0000529 }
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100530
531 g_strfreev(parts);
Blue Swirl5726c272012-06-03 15:03:23 +0000532 return mask;
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100533
534 error:
535 g_strfreev(parts);
536 return 0;
Blue Swirl5726c272012-06-03 15:03:23 +0000537}
Peter Maydell59a6fa62013-02-11 16:41:21 +0000538
539void qemu_print_log_usage(FILE *f)
540{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000541 const QEMULogItem *item;
Peter Maydell59a6fa62013-02-11 16:41:21 +0000542 fprintf(f, "Log items (comma separated):\n");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000543 for (item = qemu_log_items; item->mask != 0; item++) {
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300544 fprintf(f, "%-15s %s\n", item->name, item->help);
Peter Maydell59a6fa62013-02-11 16:41:21 +0000545 }
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300546#ifdef CONFIG_TRACE_LOG
547 fprintf(f, "trace:PATTERN enable trace events\n");
548 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
549#endif
Peter Maydell59a6fa62013-02-11 16:41:21 +0000550}