Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 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 |
Chetan Pant | 61f3c91 | 2020-10-23 12:44:24 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 21 | #include "qemu/log.h" |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 22 | #include "qemu/range.h" |
| 23 | #include "qemu/error-report.h" |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 24 | #include "qapi/error.h" |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 25 | #include "qemu/cutils.h" |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 26 | #include "trace/control.h" |
Robert Foley | b8121fe | 2019-11-18 16:15:25 -0500 | [diff] [blame] | 27 | #include "qemu/thread.h" |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 28 | #include "qemu/lockable.h" |
Richard Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 29 | #include "qemu/rcu.h" |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 30 | #ifdef CONFIG_LINUX |
| 31 | #include <sys/syscall.h> |
| 32 | #endif |
Richard Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 33 | |
| 34 | |
Richard Henderson | d5f55ff | 2022-04-17 11:30:17 -0700 | [diff] [blame] | 35 | typedef struct RCUCloseFILE { |
Richard Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 36 | struct rcu_head rcu; |
| 37 | FILE *fd; |
Richard Henderson | d5f55ff | 2022-04-17 11:30:17 -0700 | [diff] [blame] | 38 | } RCUCloseFILE; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 39 | |
Richard Henderson | 702979f | 2022-04-17 11:30:14 -0700 | [diff] [blame] | 40 | /* Mutex covering the other global_* variables. */ |
| 41 | static QemuMutex global_mutex; |
Richard Henderson | 4226646 | 2022-04-17 11:30:12 -0700 | [diff] [blame] | 42 | static char *global_filename; |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 43 | static FILE *global_file; |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 44 | static __thread FILE *thread_file; |
Greg Kurz | eff3de5 | 2022-10-21 12:57:34 +0200 | [diff] [blame] | 45 | static __thread Notifier qemu_log_thread_cleanup_notifier; |
Richard Henderson | 702979f | 2022-04-17 11:30:14 -0700 | [diff] [blame] | 46 | |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 47 | int qemu_loglevel; |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 48 | static bool log_per_thread; |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 49 | static GArray *debug_regions; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 50 | |
Richard Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 51 | /* Returns true if qemu_log() will really write somewhere. */ |
| 52 | bool qemu_log_enabled(void) |
| 53 | { |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 54 | return log_per_thread || qatomic_read(&global_file) != NULL; |
Richard Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* Returns true if qemu_log() will write somewhere other than stderr. */ |
| 58 | bool qemu_log_separate(void) |
| 59 | { |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 60 | if (log_per_thread) { |
| 61 | return true; |
| 62 | } else { |
| 63 | FILE *logfile = qatomic_read(&global_file); |
| 64 | return logfile && logfile != stderr; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static 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 Henderson | 7fc493f | 2022-04-17 11:30:05 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Greg Kurz | eff3de5 | 2022-10-21 12:57:34 +0200 | [diff] [blame] | 80 | static void qemu_log_thread_cleanup(Notifier *n, void *unused) |
| 81 | { |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 82 | if (thread_file != stderr) { |
| 83 | fclose(thread_file); |
| 84 | thread_file = NULL; |
| 85 | } |
Greg Kurz | eff3de5 | 2022-10-21 12:57:34 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 88 | /* Lock/unlock output. */ |
| 89 | |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 90 | static FILE *qemu_log_trylock_with_err(Error **errp) |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 91 | { |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 92 | FILE *logfile; |
Richard Henderson | c60f599 | 2022-04-17 11:29:47 -0700 | [diff] [blame] | 93 | |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 94 | 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 Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 101 | error_setg_errno(errp, errno, |
| 102 | "Error opening logfile %s for thread %d", |
| 103 | filename, log_thread_id()); |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | thread_file = logfile; |
Greg Kurz | eff3de5 | 2022-10-21 12:57:34 +0200 | [diff] [blame] | 107 | qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup; |
| 108 | qemu_thread_atexit_add(&qemu_log_thread_cleanup_notifier); |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 109 | } 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 Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 124 | } |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 125 | |
| 126 | qemu_flockfile(logfile); |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 127 | return logfile; |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 130 | FILE *qemu_log_trylock(void) |
| 131 | { |
| 132 | return qemu_log_trylock_with_err(NULL); |
| 133 | } |
| 134 | |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 135 | void qemu_log_unlock(FILE *logfile) |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 136 | { |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 137 | if (logfile) { |
| 138 | fflush(logfile); |
| 139 | qemu_funlockfile(logfile); |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 140 | if (!log_per_thread) { |
| 141 | rcu_read_unlock(); |
| 142 | } |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 143 | } |
Richard Henderson | c59fe6e | 2022-04-17 11:29:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Richard Henderson | 3c06a41 | 2022-04-17 11:29:57 -0700 | [diff] [blame] | 146 | void qemu_log(const char *fmt, ...) |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 147 | { |
Richard Henderson | 095e985 | 2022-04-17 11:29:56 -0700 | [diff] [blame] | 148 | FILE *f = qemu_log_trylock(); |
Richard Henderson | 095e985 | 2022-04-17 11:29:56 -0700 | [diff] [blame] | 149 | if (f) { |
Richard Henderson | bdfb460 | 2016-06-23 19:15:55 -0700 | [diff] [blame] | 150 | va_list ap; |
Richard Henderson | 095e985 | 2022-04-17 11:29:56 -0700 | [diff] [blame] | 151 | |
Richard Henderson | bdfb460 | 2016-06-23 19:15:55 -0700 | [diff] [blame] | 152 | va_start(ap, fmt); |
Richard Henderson | 3c06a41 | 2022-04-17 11:29:57 -0700 | [diff] [blame] | 153 | vfprintf(f, fmt, ap); |
Richard Henderson | bdfb460 | 2016-06-23 19:15:55 -0700 | [diff] [blame] | 154 | va_end(ap); |
Richard Henderson | 095e985 | 2022-04-17 11:29:56 -0700 | [diff] [blame] | 155 | qemu_log_unlock(f); |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 156 | } |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Richard Henderson | 702979f | 2022-04-17 11:30:14 -0700 | [diff] [blame] | 159 | static void __attribute__((__constructor__)) startup(void) |
Robert Foley | b8121fe | 2019-11-18 16:15:25 -0500 | [diff] [blame] | 160 | { |
Richard Henderson | 702979f | 2022-04-17 11:30:14 -0700 | [diff] [blame] | 161 | qemu_mutex_init(&global_mutex); |
Robert Foley | b8121fe | 2019-11-18 16:15:25 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Richard Henderson | d5f55ff | 2022-04-17 11:30:17 -0700 | [diff] [blame] | 164 | static void rcu_close_file(RCUCloseFILE *r) |
Robert Foley | 7606488 | 2019-11-18 16:15:27 -0500 | [diff] [blame] | 165 | { |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 166 | fclose(r->fd); |
Richard Henderson | d5f55ff | 2022-04-17 11:30:17 -0700 | [diff] [blame] | 167 | g_free(r); |
Robert Foley | 7606488 | 2019-11-18 16:15:27 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 170 | /** |
| 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 | |
| 177 | typedef enum { |
| 178 | vft_error, |
| 179 | vft_stderr, |
| 180 | vft_strdup, |
| 181 | vft_pid_printf, |
| 182 | } ValidFilenameTemplateResult; |
| 183 | |
| 184 | static ValidFilenameTemplateResult |
| 185 | valid_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 Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 206 | /* enable or disable low levels log */ |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 207 | static bool qemu_set_log_internal(const char *filename, bool changed_name, |
| 208 | int log_flags, Error **errp) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 209 | { |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 210 | bool need_to_open_file; |
Richard Henderson | beab344 | 2022-04-17 11:30:15 -0700 | [diff] [blame] | 211 | bool daemonized; |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 212 | bool per_thread; |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 213 | FILE *logfile; |
Robert Foley | 7606488 | 2019-11-18 16:15:27 -0500 | [diff] [blame] | 214 | |
Richard Henderson | 702979f | 2022-04-17 11:30:14 -0700 | [diff] [blame] | 215 | QEMU_LOCK_GUARD(&global_mutex); |
Richard Henderson | 8ae58d6 | 2022-04-17 11:30:13 -0700 | [diff] [blame] | 216 | logfile = global_file; |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 217 | |
Greg Kurz | 479b350 | 2022-11-04 13:00:58 +0100 | [diff] [blame] | 218 | /* The per-thread flag is immutable. */ |
| 219 | if (log_per_thread) { |
| 220 | log_flags |= LOG_PER_THREAD; |
Greg Kurz | 524fc73 | 2022-11-04 13:00:59 +0100 | [diff] [blame] | 221 | } else { |
| 222 | if (global_filename) { |
| 223 | log_flags &= ~LOG_PER_THREAD; |
| 224 | } |
Greg Kurz | 479b350 | 2022-11-04 13:00:58 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 227 | per_thread = log_flags & LOG_PER_THREAD; |
| 228 | |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 229 | if (changed_name) { |
| 230 | char *newname = NULL; |
| 231 | |
| 232 | /* |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 233 | * 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 Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 237 | */ |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 238 | if (log_per_thread) { |
| 239 | error_setg(errp, "Cannot change log filename after setting 'tid'"); |
| 240 | return false; |
| 241 | } |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 242 | |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 243 | 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 Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Richard Henderson | 4226646 | 2022-04-17 11:30:12 -0700 | [diff] [blame] | 256 | g_free(global_filename); |
| 257 | global_filename = newname; |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 258 | filename = newname; |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 259 | } else { |
Richard Henderson | 4226646 | 2022-04-17 11:30:12 -0700 | [diff] [blame] | 260 | filename = global_filename; |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 261 | if (per_thread && |
| 262 | valid_filename_template(filename, true, errp) == vft_error) { |
| 263 | return false; |
| 264 | } |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 267 | /* 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 Bonzini | ed7f5f1 | 2016-01-07 16:55:30 +0300 | [diff] [blame] | 273 | #ifdef CONFIG_TRACE_LOG |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 274 | log_flags |= LOG_TRACE; |
Paolo Bonzini | ed7f5f1 | 2016-01-07 16:55:30 +0300 | [diff] [blame] | 275 | #endif |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 276 | qemu_loglevel = log_flags; |
| 277 | |
Richard Henderson | beab344 | 2022-04-17 11:30:15 -0700 | [diff] [blame] | 278 | daemonized = is_daemonized(); |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 279 | 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 Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 293 | |
Paolo Bonzini | 59bde21 | 2022-11-08 15:00:31 +0100 | [diff] [blame] | 294 | if (logfile) { |
| 295 | fflush(logfile); |
| 296 | if (changed_name && logfile != stderr) { |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 297 | RCUCloseFILE *r = g_new0(RCUCloseFILE, 1); |
| 298 | r->fd = logfile; |
Paolo Bonzini | 59bde21 | 2022-11-08 15:00:31 +0100 | [diff] [blame] | 299 | qatomic_rcu_set(&global_file, NULL); |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 300 | call_rcu(r, rcu_close_file, rcu); |
Paolo Bonzini | 59bde21 | 2022-11-08 15:00:31 +0100 | [diff] [blame] | 301 | logfile = NULL; |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 302 | } |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 303 | } |
Richard Henderson | 92b24cb | 2022-04-17 11:30:16 -0700 | [diff] [blame] | 304 | |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 305 | if (log_per_thread && daemonized) { |
| 306 | logfile = thread_file; |
| 307 | } |
| 308 | |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 309 | if (!logfile && need_to_open_file) { |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 310 | if (filename) { |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 311 | 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 Maydell | 989b697 | 2013-02-26 17:52:40 +0000 | [diff] [blame] | 324 | } |
Dimitris Aragiorgis | 96c33a4 | 2016-02-18 13:38:38 +0200 | [diff] [blame] | 325 | /* In case we are a daemon redirect stderr to logfile */ |
Richard Henderson | beab344 | 2022-04-17 11:30:15 -0700 | [diff] [blame] | 326 | if (daemonized) { |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 327 | dup2(fileno(logfile), STDERR_FILENO); |
| 328 | fclose(logfile); |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 329 | /* |
| 330 | * This will skip closing logfile in rcu_close_file() |
| 331 | * or qemu_log_thread_cleanup(). |
| 332 | */ |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 333 | logfile = stderr; |
Dimitris Aragiorgis | 96c33a4 | 2016-02-18 13:38:38 +0200 | [diff] [blame] | 334 | } |
Peter Maydell | 989b697 | 2013-02-26 17:52:40 +0000 | [diff] [blame] | 335 | } else { |
| 336 | /* Default to stderr if no log file specified */ |
Richard Henderson | beab344 | 2022-04-17 11:30:15 -0700 | [diff] [blame] | 337 | assert(!daemonized); |
Richard Henderson | 30f5a73 | 2022-04-17 11:30:18 -0700 | [diff] [blame] | 338 | logfile = stderr; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 339 | } |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 340 | |
Greg Kurz | 9b063b7 | 2022-11-08 15:00:32 +0100 | [diff] [blame] | 341 | if (log_per_thread && daemonized) { |
| 342 | thread_file = logfile; |
| 343 | } else { |
| 344 | qatomic_rcu_set(&global_file, logfile); |
| 345 | } |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 346 | } |
Richard Henderson | c5955f4 | 2022-04-17 11:29:44 -0700 | [diff] [blame] | 347 | return true; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 348 | } |
Paolo Bonzini | f2937a3 | 2015-12-04 13:12:57 +0100 | [diff] [blame] | 349 | |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 350 | bool qemu_set_log(int log_flags, Error **errp) |
| 351 | { |
| 352 | return qemu_set_log_internal(NULL, false, log_flags, errp); |
| 353 | } |
| 354 | |
Richard Henderson | e2c7c6a | 2022-04-17 11:29:43 -0700 | [diff] [blame] | 355 | bool qemu_set_log_filename(const char *filename, Error **errp) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 356 | { |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 357 | return qemu_set_log_internal(filename, true, qemu_loglevel, errp); |
| 358 | } |
Alex Bennée | f6880b7 | 2016-03-15 14:30:23 +0000 | [diff] [blame] | 359 | |
Richard Henderson | 144539d | 2022-04-17 11:30:07 -0700 | [diff] [blame] | 360 | bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp) |
| 361 | { |
| 362 | return qemu_set_log_internal(name, true, flags, errp); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 365 | /* Returns true if addr is in our debug filter or no filter defined |
| 366 | */ |
| 367 | bool 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 Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 372 | Range *range = &g_array_index(debug_regions, Range, i); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 373 | if (range_contains(range, addr)) { |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 374 | return true; |
| 375 | } |
| 376 | } |
| 377 | return false; |
| 378 | } else { |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 384 | void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 385 | { |
| 386 | gchar **ranges = g_strsplit(filter_spec, ",", 0); |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 387 | int i; |
Markus Armbruster | 2ec62fa | 2016-06-15 19:27:14 +0200 | [diff] [blame] | 388 | |
| 389 | if (debug_regions) { |
| 390 | g_array_unref(debug_regions); |
| 391 | debug_regions = NULL; |
| 392 | } |
| 393 | |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 394 | 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 Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 399 | uint64_t r1val, r2val, lob, upb; |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 400 | struct Range range; |
Markus Armbruster | 2ec62fa | 2016-06-15 19:27:14 +0200 | [diff] [blame] | 401 | |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 402 | 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ée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 407 | } |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 408 | 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 Armbruster | b30d188 | 2017-02-21 21:13:50 +0100 | [diff] [blame] | 417 | if (qemu_strtou64(r, &e, 0, &r1val) |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 418 | || 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 Armbruster | b30d188 | 2017-02-21 21:13:50 +0100 | [diff] [blame] | 423 | if (qemu_strtou64(r2, NULL, 0, &r2val)) { |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 424 | error_setg(errp, "Invalid number to the right of %.*s", |
| 425 | (int)(r2 - range_op), range_op); |
| 426 | goto out; |
| 427 | } |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 428 | |
| 429 | switch (*range_op) { |
| 430 | case '+': |
Markus Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 431 | lob = r1val; |
| 432 | upb = r1val + r2val - 1; |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 433 | break; |
| 434 | case '-': |
Markus Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 435 | upb = r1val; |
| 436 | lob = r1val - (r2val - 1); |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 437 | break; |
| 438 | case '.': |
Markus Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 439 | lob = r1val; |
| 440 | upb = r2val; |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 441 | break; |
| 442 | default: |
| 443 | g_assert_not_reached(); |
| 444 | } |
Markus Armbruster | 58eeb83 | 2016-07-01 13:47:49 +0200 | [diff] [blame] | 445 | if (lob > upb) { |
Markus Armbruster | 58e19e6 | 2016-07-01 13:47:46 +0200 | [diff] [blame] | 446 | error_setg(errp, "Invalid range"); |
| 447 | goto out; |
| 448 | } |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 449 | range_set_bounds(&range, lob, upb); |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 450 | g_array_append_val(debug_regions, range); |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 451 | } |
Markus Armbruster | bd6fee9 | 2016-06-15 19:27:15 +0200 | [diff] [blame] | 452 | out: |
| 453 | g_strfreev(ranges); |
Alex Bennée | 3514552 | 2016-03-15 14:30:20 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 456 | const QEMULogItem qemu_log_items[] = { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 457 | { 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 Henderson | 5a18407 | 2016-06-23 20:34:33 -0700 | [diff] [blame] | 464 | "show micro ops after optimization" }, |
| 465 | { CPU_LOG_TB_OP_IND, "op_ind", |
| 466 | "show micro ops before indirect lowering" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 467 | { 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ée | 5419573 | 2016-03-15 14:30:17 +0000 | [diff] [blame] | 472 | "show CPU registers before entering a TB (lots of logs)" }, |
Peter Maydell | ae76518 | 2018-05-15 14:58:44 +0100 | [diff] [blame] | 473 | { CPU_LOG_TB_FPU, "fpu", |
| 474 | "include FPU registers in the 'cpu' logging" }, |
Antony Pavlov | 339aaf5 | 2014-12-13 19:48:18 +0300 | [diff] [blame] | 475 | { CPU_LOG_MMU, "mmu", |
| 476 | "log MMU-related activities" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 477 | { CPU_LOG_PCALL, "pcall", |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 478 | "x86 only: show protected mode far calls/returns/exceptions" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 479 | { CPU_LOG_RESET, "cpu_reset", |
Thomas Huth | dbfe1b6 | 2015-01-27 13:11:26 +0100 | [diff] [blame] | 480 | "show CPU state before CPU resets" }, |
Blue Swirl | dafdf1a | 2012-06-03 17:04:28 +0000 | [diff] [blame] | 481 | { LOG_UNIMP, "unimp", |
| 482 | "log unimplemented functionality" }, |
Peter Maydell | e54eba1 | 2012-10-18 14:11:35 +0100 | [diff] [blame] | 483 | { LOG_GUEST_ERROR, "guest_errors", |
| 484 | "log when the guest OS does something invalid (eg accessing a\n" |
| 485 | "non-existent register)" }, |
Paolo Bonzini | 1382902 | 2015-11-13 12:32:19 +0100 | [diff] [blame] | 486 | { CPU_LOG_PAGE, "page", |
| 487 | "dump pages at beginning of user mode emulation" }, |
Richard Henderson | 89a82cd | 2015-09-16 15:33:53 -0700 | [diff] [blame] | 488 | { CPU_LOG_TB_NOCHAIN, "nochain", |
| 489 | "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" |
| 490 | "complete traces" }, |
Alex Bennée | ca76a66 | 2019-10-11 16:34:05 +0100 | [diff] [blame] | 491 | #ifdef CONFIG_PLUGIN |
BALATON Zoltan | cb9291e | 2023-01-19 22:40:33 +0100 | [diff] [blame] | 492 | { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins"}, |
Alex Bennée | ca76a66 | 2019-10-11 16:34:05 +0100 | [diff] [blame] | 493 | #endif |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 494 | { LOG_STRACE, "strace", |
| 495 | "log every user-mode syscall, its input, and its result" }, |
Richard Henderson | 4e51069 | 2022-04-17 11:30:19 -0700 | [diff] [blame] | 496 | { LOG_PER_THREAD, "tid", |
| 497 | "open a separate log file per thread; filename must contain '%d'" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 498 | { 0, NULL, NULL }, |
| 499 | }; |
| 500 | |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 501 | /* takes a comma separated list of log masks. Return 0 if error. */ |
Peter Maydell | 4fde1eb | 2013-02-11 16:41:22 +0000 | [diff] [blame] | 502 | int qemu_str_to_log_mask(const char *str) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 503 | { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 504 | const QEMULogItem *item; |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 505 | int mask = 0; |
| 506 | char **parts = g_strsplit(str, ",", 0); |
| 507 | char **tmp; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 508 | |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 509 | for (tmp = parts; tmp && *tmp; tmp++) { |
| 510 | if (g_str_equal(*tmp, "all")) { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 511 | for (item = qemu_log_items; item->mask != 0; item++) { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 512 | mask |= item->mask; |
| 513 | } |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 514 | #ifdef CONFIG_TRACE_LOG |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 515 | } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') { |
| 516 | trace_enable_events((*tmp) + 6); |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 517 | mask |= LOG_TRACE; |
| 518 | #endif |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 519 | } else { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 520 | for (item = qemu_log_items; item->mask != 0; item++) { |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 521 | if (g_str_equal(*tmp, item->name)) { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 522 | goto found; |
| 523 | } |
| 524 | } |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 525 | goto error; |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 526 | found: |
| 527 | mask |= item->mask; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 528 | } |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 529 | } |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 530 | |
| 531 | g_strfreev(parts); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 532 | return mask; |
Daniel P. Berrange | 89d0a64 | 2016-09-06 19:25:43 +0100 | [diff] [blame] | 533 | |
| 534 | error: |
| 535 | g_strfreev(parts); |
| 536 | return 0; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 537 | } |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 538 | |
| 539 | void qemu_print_log_usage(FILE *f) |
| 540 | { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 541 | const QEMULogItem *item; |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 542 | fprintf(f, "Log items (comma separated):\n"); |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 543 | for (item = qemu_log_items; item->mask != 0; item++) { |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 544 | fprintf(f, "%-15s %s\n", item->name, item->help); |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 545 | } |
Paolo Bonzini | c84ea00 | 2016-01-07 16:55:32 +0300 | [diff] [blame] | 546 | #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 Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 550 | } |