blob: 6219819855c28e9cd69e1343b201e87a8f539deb [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);
Fiona Ebnerf05142d2023-10-04 14:44:46 +0200301 }
302 if (changed_name) {
Paolo Bonzini59bde212022-11-08 15:00:31 +0100303 logfile = NULL;
Richard Henderson30f5a732022-04-17 11:30:18 -0700304 }
Richard Henderson144539d2022-04-17 11:30:07 -0700305 }
Richard Henderson92b24cb2022-04-17 11:30:16 -0700306
Greg Kurz9b063b72022-11-08 15:00:32 +0100307 if (log_per_thread && daemonized) {
308 logfile = thread_file;
309 }
310
Richard Henderson144539d2022-04-17 11:30:07 -0700311 if (!logfile && need_to_open_file) {
Richard Henderson144539d2022-04-17 11:30:07 -0700312 if (filename) {
Greg Kurz9b063b72022-11-08 15:00:32 +0100313 if (log_per_thread) {
314 logfile = qemu_log_trylock_with_err(errp);
315 if (!logfile) {
316 return false;
317 }
318 qemu_log_unlock(logfile);
319 } else {
320 logfile = fopen(filename, "w");
321 if (!logfile) {
322 error_setg_errno(errp, errno, "Error opening logfile %s",
323 filename);
324 return false;
325 }
Peter Maydell989b6972013-02-26 17:52:40 +0000326 }
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200327 /* In case we are a daemon redirect stderr to logfile */
Richard Hendersonbeab3442022-04-17 11:30:15 -0700328 if (daemonized) {
Richard Henderson30f5a732022-04-17 11:30:18 -0700329 dup2(fileno(logfile), STDERR_FILENO);
330 fclose(logfile);
Greg Kurz9b063b72022-11-08 15:00:32 +0100331 /*
332 * This will skip closing logfile in rcu_close_file()
333 * or qemu_log_thread_cleanup().
334 */
Richard Henderson30f5a732022-04-17 11:30:18 -0700335 logfile = stderr;
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200336 }
Peter Maydell989b6972013-02-26 17:52:40 +0000337 } else {
338 /* Default to stderr if no log file specified */
Richard Hendersonbeab3442022-04-17 11:30:15 -0700339 assert(!daemonized);
Richard Henderson30f5a732022-04-17 11:30:18 -0700340 logfile = stderr;
Blue Swirl5726c272012-06-03 15:03:23 +0000341 }
Blue Swirl3437e542012-07-07 14:40:18 +0000342
Greg Kurz9b063b72022-11-08 15:00:32 +0100343 if (log_per_thread && daemonized) {
344 thread_file = logfile;
345 } else {
346 qatomic_rcu_set(&global_file, logfile);
347 }
Blue Swirl5726c272012-06-03 15:03:23 +0000348 }
Richard Hendersonc5955f42022-04-17 11:29:44 -0700349 return true;
Blue Swirl5726c272012-06-03 15:03:23 +0000350}
Paolo Bonzinif2937a32015-12-04 13:12:57 +0100351
Richard Henderson144539d2022-04-17 11:30:07 -0700352bool qemu_set_log(int log_flags, Error **errp)
353{
354 return qemu_set_log_internal(NULL, false, log_flags, errp);
355}
356
Richard Hendersone2c7c6a2022-04-17 11:29:43 -0700357bool qemu_set_log_filename(const char *filename, Error **errp)
Blue Swirl5726c272012-06-03 15:03:23 +0000358{
Richard Henderson144539d2022-04-17 11:30:07 -0700359 return qemu_set_log_internal(filename, true, qemu_loglevel, errp);
360}
Alex Bennéef6880b72016-03-15 14:30:23 +0000361
Richard Henderson144539d2022-04-17 11:30:07 -0700362bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp)
363{
364 return qemu_set_log_internal(name, true, flags, errp);
Blue Swirl5726c272012-06-03 15:03:23 +0000365}
366
Alex Bennée35145522016-03-15 14:30:20 +0000367/* Returns true if addr is in our debug filter or no filter defined
368 */
369bool qemu_log_in_addr_range(uint64_t addr)
370{
371 if (debug_regions) {
372 int i = 0;
373 for (i = 0; i < debug_regions->len; i++) {
Markus Armbruster58e19e62016-07-01 13:47:46 +0200374 Range *range = &g_array_index(debug_regions, Range, i);
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200375 if (range_contains(range, addr)) {
Alex Bennée35145522016-03-15 14:30:20 +0000376 return true;
377 }
378 }
379 return false;
380 } else {
381 return true;
382 }
383}
384
385
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200386void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
Alex Bennée35145522016-03-15 14:30:20 +0000387{
388 gchar **ranges = g_strsplit(filter_spec, ",", 0);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200389 int i;
Markus Armbruster2ec62fa2016-06-15 19:27:14 +0200390
391 if (debug_regions) {
392 g_array_unref(debug_regions);
393 debug_regions = NULL;
394 }
395
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200396 debug_regions = g_array_sized_new(FALSE, FALSE,
397 sizeof(Range), g_strv_length(ranges));
398 for (i = 0; ranges[i]; i++) {
399 const char *r = ranges[i];
400 const char *range_op, *r2, *e;
Markus Armbruster58e19e62016-07-01 13:47:46 +0200401 uint64_t r1val, r2val, lob, upb;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200402 struct Range range;
Markus Armbruster2ec62fa2016-06-15 19:27:14 +0200403
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200404 range_op = strstr(r, "-");
405 r2 = range_op ? range_op + 1 : NULL;
406 if (!range_op) {
407 range_op = strstr(r, "+");
408 r2 = range_op ? range_op + 1 : NULL;
Alex Bennée35145522016-03-15 14:30:20 +0000409 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200410 if (!range_op) {
411 range_op = strstr(r, "..");
412 r2 = range_op ? range_op + 2 : NULL;
413 }
414 if (!range_op) {
415 error_setg(errp, "Bad range specifier");
416 goto out;
417 }
418
Markus Armbrusterb30d1882017-02-21 21:13:50 +0100419 if (qemu_strtou64(r, &e, 0, &r1val)
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200420 || e != range_op) {
421 error_setg(errp, "Invalid number to the left of %.*s",
422 (int)(r2 - range_op), range_op);
423 goto out;
424 }
Markus Armbrusterb30d1882017-02-21 21:13:50 +0100425 if (qemu_strtou64(r2, NULL, 0, &r2val)) {
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200426 error_setg(errp, "Invalid number to the right of %.*s",
427 (int)(r2 - range_op), range_op);
428 goto out;
429 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200430
431 switch (*range_op) {
432 case '+':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200433 lob = r1val;
434 upb = r1val + r2val - 1;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200435 break;
436 case '-':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200437 upb = r1val;
438 lob = r1val - (r2val - 1);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200439 break;
440 case '.':
Markus Armbruster58e19e62016-07-01 13:47:46 +0200441 lob = r1val;
442 upb = r2val;
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200443 break;
444 default:
445 g_assert_not_reached();
446 }
Markus Armbruster58eeb832016-07-01 13:47:49 +0200447 if (lob > upb) {
Markus Armbruster58e19e62016-07-01 13:47:46 +0200448 error_setg(errp, "Invalid range");
449 goto out;
450 }
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200451 range_set_bounds(&range, lob, upb);
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200452 g_array_append_val(debug_regions, range);
Alex Bennée35145522016-03-15 14:30:20 +0000453 }
Markus Armbrusterbd6fee92016-06-15 19:27:15 +0200454out:
455 g_strfreev(ranges);
Alex Bennée35145522016-03-15 14:30:20 +0000456}
457
Peter Maydell38dad9e2013-02-11 16:41:25 +0000458const QEMULogItem qemu_log_items[] = {
Blue Swirl5726c272012-06-03 15:03:23 +0000459 { CPU_LOG_TB_OUT_ASM, "out_asm",
460 "show generated host assembly code for each compiled TB" },
461 { CPU_LOG_TB_IN_ASM, "in_asm",
462 "show target assembly code for each compiled TB" },
463 { CPU_LOG_TB_OP, "op",
464 "show micro ops for each compiled TB" },
465 { CPU_LOG_TB_OP_OPT, "op_opt",
Richard Henderson5a184072016-06-23 20:34:33 -0700466 "show micro ops after optimization" },
467 { CPU_LOG_TB_OP_IND, "op_ind",
468 "show micro ops before indirect lowering" },
Richard Hendersonb384c732024-03-15 10:33:49 -1000469#ifdef CONFIG_PLUGIN
470 { LOG_TB_OP_PLUGIN, "op_plugin",
471 "show micro ops before plugin injection" },
472#endif
Blue Swirl5726c272012-06-03 15:03:23 +0000473 { CPU_LOG_INT, "int",
474 "show interrupts/exceptions in short format" },
475 { CPU_LOG_EXEC, "exec",
476 "show trace before each executed TB (lots of logs)" },
477 { CPU_LOG_TB_CPU, "cpu",
Alex Bennée54195732016-03-15 14:30:17 +0000478 "show CPU registers before entering a TB (lots of logs)" },
Peter Maydellae765182018-05-15 14:58:44 +0100479 { CPU_LOG_TB_FPU, "fpu",
480 "include FPU registers in the 'cpu' logging" },
Antony Pavlov339aaf52014-12-13 19:48:18 +0300481 { CPU_LOG_MMU, "mmu",
482 "log MMU-related activities" },
Blue Swirl5726c272012-06-03 15:03:23 +0000483 { CPU_LOG_PCALL, "pcall",
Blue Swirl3437e542012-07-07 14:40:18 +0000484 "x86 only: show protected mode far calls/returns/exceptions" },
Blue Swirl5726c272012-06-03 15:03:23 +0000485 { CPU_LOG_RESET, "cpu_reset",
Thomas Huthdbfe1b62015-01-27 13:11:26 +0100486 "show CPU state before CPU resets" },
Blue Swirldafdf1a2012-06-03 17:04:28 +0000487 { LOG_UNIMP, "unimp",
488 "log unimplemented functionality" },
Peter Maydelle54eba12012-10-18 14:11:35 +0100489 { LOG_GUEST_ERROR, "guest_errors",
490 "log when the guest OS does something invalid (eg accessing a\n"
491 "non-existent register)" },
Paolo Bonzini13829022015-11-13 12:32:19 +0100492 { CPU_LOG_PAGE, "page",
493 "dump pages at beginning of user mode emulation" },
Richard Henderson89a82cd2015-09-16 15:33:53 -0700494 { CPU_LOG_TB_NOCHAIN, "nochain",
495 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
496 "complete traces" },
Alex Bennéeca76a662019-10-11 16:34:05 +0100497#ifdef CONFIG_PLUGIN
BALATON Zoltancb9291e2023-01-19 22:40:33 +0100498 { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins"},
Alex Bennéeca76a662019-10-11 16:34:05 +0100499#endif
Josh Kunz4b25a502020-02-03 18:54:14 -0800500 { LOG_STRACE, "strace",
501 "log every user-mode syscall, its input, and its result" },
Richard Henderson4e510692022-04-17 11:30:19 -0700502 { LOG_PER_THREAD, "tid",
503 "open a separate log file per thread; filename must contain '%d'" },
Ivan Klokovb84694d2023-04-10 15:44:50 +0300504 { CPU_LOG_TB_VPU, "vpu",
505 "include VPU registers in the 'cpu' logging" },
Blue Swirl5726c272012-06-03 15:03:23 +0000506 { 0, NULL, NULL },
507};
508
Blue Swirl5726c272012-06-03 15:03:23 +0000509/* takes a comma separated list of log masks. Return 0 if error. */
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000510int qemu_str_to_log_mask(const char *str)
Blue Swirl5726c272012-06-03 15:03:23 +0000511{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000512 const QEMULogItem *item;
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100513 int mask = 0;
514 char **parts = g_strsplit(str, ",", 0);
515 char **tmp;
Blue Swirl5726c272012-06-03 15:03:23 +0000516
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100517 for (tmp = parts; tmp && *tmp; tmp++) {
518 if (g_str_equal(*tmp, "all")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000519 for (item = qemu_log_items; item->mask != 0; item++) {
Blue Swirl5726c272012-06-03 15:03:23 +0000520 mask |= item->mask;
521 }
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300522#ifdef CONFIG_TRACE_LOG
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100523 } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
524 trace_enable_events((*tmp) + 6);
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300525 mask |= LOG_TRACE;
526#endif
Blue Swirl5726c272012-06-03 15:03:23 +0000527 } else {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000528 for (item = qemu_log_items; item->mask != 0; item++) {
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100529 if (g_str_equal(*tmp, item->name)) {
Blue Swirl5726c272012-06-03 15:03:23 +0000530 goto found;
531 }
532 }
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100533 goto error;
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300534 found:
535 mask |= item->mask;
Blue Swirl5726c272012-06-03 15:03:23 +0000536 }
Blue Swirl5726c272012-06-03 15:03:23 +0000537 }
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100538
539 g_strfreev(parts);
Blue Swirl5726c272012-06-03 15:03:23 +0000540 return mask;
Daniel P. Berrange89d0a642016-09-06 19:25:43 +0100541
542 error:
543 g_strfreev(parts);
544 return 0;
Blue Swirl5726c272012-06-03 15:03:23 +0000545}
Peter Maydell59a6fa62013-02-11 16:41:21 +0000546
547void qemu_print_log_usage(FILE *f)
548{
Peter Maydell38dad9e2013-02-11 16:41:25 +0000549 const QEMULogItem *item;
Peter Maydell59a6fa62013-02-11 16:41:21 +0000550 fprintf(f, "Log items (comma separated):\n");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000551 for (item = qemu_log_items; item->mask != 0; item++) {
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300552 fprintf(f, "%-15s %s\n", item->name, item->help);
Peter Maydell59a6fa62013-02-11 16:41:21 +0000553 }
Paolo Bonzinic84ea002016-01-07 16:55:32 +0300554#ifdef CONFIG_TRACE_LOG
555 fprintf(f, "trace:PATTERN enable trace events\n");
556 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
557#endif
Peter Maydell59a6fa62013-02-11 16:41:21 +0000558}