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 #include "qemu/rcu.h" 30 #ifdef CONFIG_LINUX 31 #include <sys/syscall.h> 32 #endif 33 34 35 typedef struct RCUCloseFILE { 36 struct rcu_head rcu; 37 FILE *fd; 38 } RCUCloseFILE; 39 40 /* Mutex covering the other global_* variables. */ 41 static QemuMutex global_mutex; 42 static char *global_filename; 43 static FILE *global_file; 44 static __thread FILE *thread_file; 45 static __thread Notifier qemu_log_thread_cleanup_notifier; 46 47 int qemu_loglevel; 48 static bool log_append; 49 static bool log_per_thread; 50 static GArray *debug_regions; 51 52 /* Returns true if qemu_log() will really write somewhere. */ 53 bool qemu_log_enabled(void) 54 { 55 return log_per_thread || qatomic_read(&global_file) != NULL; 56 } 57 58 /* Returns true if qemu_log() will write somewhere other than stderr. */ 59 bool qemu_log_separate(void) 60 { 61 if (log_per_thread) { 62 return true; 63 } else { 64 FILE *logfile = qatomic_read(&global_file); 65 return logfile && logfile != stderr; 66 } 67 } 68 69 static int log_thread_id(void) 70 { 71 #ifdef CONFIG_GETTID 72 return gettid(); 73 #elif defined(SYS_gettid) 74 return syscall(SYS_gettid); 75 #else 76 static int counter; 77 return qatomic_fetch_inc(&counter); 78 #endif 79 } 80 81 static void qemu_log_thread_cleanup(Notifier *n, void *unused) 82 { 83 fclose(thread_file); 84 thread_file = NULL; 85 } 86 87 /* Lock/unlock output. */ 88 89 FILE *qemu_log_trylock(void) 90 { 91 FILE *logfile; 92 93 logfile = thread_file; 94 if (!logfile) { 95 if (log_per_thread) { 96 g_autofree char *filename 97 = g_strdup_printf(global_filename, log_thread_id()); 98 logfile = fopen(filename, "w"); 99 if (!logfile) { 100 return NULL; 101 } 102 thread_file = logfile; 103 qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup; 104 qemu_thread_atexit_add(&qemu_log_thread_cleanup_notifier); 105 } else { 106 rcu_read_lock(); 107 /* 108 * FIXME: typeof_strip_qual, as used by qatomic_rcu_read, 109 * does not work with pointers to undefined structures, 110 * such as we have with struct _IO_FILE and musl libc. 111 * Since all we want is a read of a pointer, cast to void**, 112 * which does work with typeof_strip_qual. 113 */ 114 logfile = qatomic_rcu_read((void **)&global_file); 115 if (!logfile) { 116 rcu_read_unlock(); 117 return NULL; 118 } 119 } 120 } 121 122 qemu_flockfile(logfile); 123 return logfile; 124 } 125 126 void qemu_log_unlock(FILE *logfile) 127 { 128 if (logfile) { 129 fflush(logfile); 130 qemu_funlockfile(logfile); 131 if (!log_per_thread) { 132 rcu_read_unlock(); 133 } 134 } 135 } 136 137 void qemu_log(const char *fmt, ...) 138 { 139 FILE *f = qemu_log_trylock(); 140 if (f) { 141 va_list ap; 142 143 va_start(ap, fmt); 144 vfprintf(f, fmt, ap); 145 va_end(ap); 146 qemu_log_unlock(f); 147 } 148 } 149 150 static void __attribute__((__constructor__)) startup(void) 151 { 152 qemu_mutex_init(&global_mutex); 153 } 154 155 static void rcu_close_file(RCUCloseFILE *r) 156 { 157 fclose(r->fd); 158 g_free(r); 159 } 160 161 /** 162 * valid_filename_template: 163 * 164 * Validate the filename template. Require %d if per_thread, allow it 165 * otherwise; require no other % within the template. 166 */ 167 168 typedef enum { 169 vft_error, 170 vft_stderr, 171 vft_strdup, 172 vft_pid_printf, 173 } ValidFilenameTemplateResult; 174 175 static ValidFilenameTemplateResult 176 valid_filename_template(const char *filename, bool per_thread, Error **errp) 177 { 178 if (filename) { 179 char *pidstr = strstr(filename, "%"); 180 181 if (pidstr) { 182 /* We only accept one %d, no other format strings */ 183 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { 184 error_setg(errp, "Bad logfile template: %s", filename); 185 return 0; 186 } 187 return per_thread ? vft_strdup : vft_pid_printf; 188 } 189 } 190 if (per_thread) { 191 error_setg(errp, "Filename template with '%%d' required for 'tid'"); 192 return vft_error; 193 } 194 return filename ? vft_strdup : vft_stderr; 195 } 196 197 /* enable or disable low levels log */ 198 static bool qemu_set_log_internal(const char *filename, bool changed_name, 199 int log_flags, Error **errp) 200 { 201 bool need_to_open_file; 202 bool daemonized; 203 bool per_thread; 204 FILE *logfile; 205 206 QEMU_LOCK_GUARD(&global_mutex); 207 logfile = global_file; 208 209 per_thread = log_flags & LOG_PER_THREAD; 210 211 if (changed_name) { 212 char *newname = NULL; 213 214 /* 215 * Once threads start opening their own log files, we have no 216 * easy mechanism to tell them all to close and re-open. 217 * There seems little cause to do so either -- this option 218 * will most often be used at user-only startup. 219 */ 220 if (log_per_thread) { 221 error_setg(errp, "Cannot change log filename after setting 'tid'"); 222 return false; 223 } 224 225 switch (valid_filename_template(filename, per_thread, errp)) { 226 case vft_error: 227 return false; 228 case vft_stderr: 229 break; 230 case vft_strdup: 231 newname = g_strdup(filename); 232 break; 233 case vft_pid_printf: 234 newname = g_strdup_printf(filename, getpid()); 235 break; 236 } 237 238 g_free(global_filename); 239 global_filename = newname; 240 filename = newname; 241 } else { 242 filename = global_filename; 243 if (per_thread && 244 valid_filename_template(filename, true, errp) == vft_error) { 245 return false; 246 } 247 } 248 249 /* Once the per-thread flag is set, it cannot be unset. */ 250 if (per_thread) { 251 log_per_thread = true; 252 } 253 /* The flag itself is not relevant for need_to_open_file. */ 254 log_flags &= ~LOG_PER_THREAD; 255 #ifdef CONFIG_TRACE_LOG 256 log_flags |= LOG_TRACE; 257 #endif 258 qemu_loglevel = log_flags; 259 260 /* 261 * In all cases we only log if qemu_loglevel is set. 262 * Also: 263 * If per-thread, open the file for each thread in qemu_log_lock. 264 * If not daemonized we will always log either to stderr 265 * or to a file (if there is a filename). 266 * If we are daemonized, we will only log if there is a filename. 267 */ 268 daemonized = is_daemonized(); 269 need_to_open_file = log_flags && !per_thread && (!daemonized || filename); 270 271 if (logfile && (!need_to_open_file || changed_name)) { 272 qatomic_rcu_set(&global_file, NULL); 273 if (logfile != stderr) { 274 RCUCloseFILE *r = g_new0(RCUCloseFILE, 1); 275 r->fd = logfile; 276 call_rcu(r, rcu_close_file, rcu); 277 } 278 logfile = NULL; 279 } 280 281 if (!logfile && need_to_open_file) { 282 if (filename) { 283 logfile = fopen(filename, log_append ? "a" : "w"); 284 if (!logfile) { 285 error_setg_errno(errp, errno, "Error opening logfile %s", 286 filename); 287 return false; 288 } 289 /* In case we are a daemon redirect stderr to logfile */ 290 if (daemonized) { 291 dup2(fileno(logfile), STDERR_FILENO); 292 fclose(logfile); 293 /* This will skip closing logfile in rcu_close_file. */ 294 logfile = stderr; 295 } 296 } else { 297 /* Default to stderr if no log file specified */ 298 assert(!daemonized); 299 logfile = stderr; 300 } 301 302 log_append = 1; 303 304 qatomic_rcu_set(&global_file, logfile); 305 } 306 return true; 307 } 308 309 bool qemu_set_log(int log_flags, Error **errp) 310 { 311 return qemu_set_log_internal(NULL, false, log_flags, errp); 312 } 313 314 bool qemu_set_log_filename(const char *filename, Error **errp) 315 { 316 return qemu_set_log_internal(filename, true, qemu_loglevel, errp); 317 } 318 319 bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp) 320 { 321 return qemu_set_log_internal(name, true, flags, errp); 322 } 323 324 /* Returns true if addr is in our debug filter or no filter defined 325 */ 326 bool qemu_log_in_addr_range(uint64_t addr) 327 { 328 if (debug_regions) { 329 int i = 0; 330 for (i = 0; i < debug_regions->len; i++) { 331 Range *range = &g_array_index(debug_regions, Range, i); 332 if (range_contains(range, addr)) { 333 return true; 334 } 335 } 336 return false; 337 } else { 338 return true; 339 } 340 } 341 342 343 void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) 344 { 345 gchar **ranges = g_strsplit(filter_spec, ",", 0); 346 int i; 347 348 if (debug_regions) { 349 g_array_unref(debug_regions); 350 debug_regions = NULL; 351 } 352 353 debug_regions = g_array_sized_new(FALSE, FALSE, 354 sizeof(Range), g_strv_length(ranges)); 355 for (i = 0; ranges[i]; i++) { 356 const char *r = ranges[i]; 357 const char *range_op, *r2, *e; 358 uint64_t r1val, r2val, lob, upb; 359 struct Range range; 360 361 range_op = strstr(r, "-"); 362 r2 = range_op ? range_op + 1 : NULL; 363 if (!range_op) { 364 range_op = strstr(r, "+"); 365 r2 = range_op ? range_op + 1 : NULL; 366 } 367 if (!range_op) { 368 range_op = strstr(r, ".."); 369 r2 = range_op ? range_op + 2 : NULL; 370 } 371 if (!range_op) { 372 error_setg(errp, "Bad range specifier"); 373 goto out; 374 } 375 376 if (qemu_strtou64(r, &e, 0, &r1val) 377 || e != range_op) { 378 error_setg(errp, "Invalid number to the left of %.*s", 379 (int)(r2 - range_op), range_op); 380 goto out; 381 } 382 if (qemu_strtou64(r2, NULL, 0, &r2val)) { 383 error_setg(errp, "Invalid number to the right of %.*s", 384 (int)(r2 - range_op), range_op); 385 goto out; 386 } 387 388 switch (*range_op) { 389 case '+': 390 lob = r1val; 391 upb = r1val + r2val - 1; 392 break; 393 case '-': 394 upb = r1val; 395 lob = r1val - (r2val - 1); 396 break; 397 case '.': 398 lob = r1val; 399 upb = r2val; 400 break; 401 default: 402 g_assert_not_reached(); 403 } 404 if (lob > upb) { 405 error_setg(errp, "Invalid range"); 406 goto out; 407 } 408 range_set_bounds(&range, lob, upb); 409 g_array_append_val(debug_regions, range); 410 } 411 out: 412 g_strfreev(ranges); 413 } 414 415 const QEMULogItem qemu_log_items[] = { 416 { CPU_LOG_TB_OUT_ASM, "out_asm", 417 "show generated host assembly code for each compiled TB" }, 418 { CPU_LOG_TB_IN_ASM, "in_asm", 419 "show target assembly code for each compiled TB" }, 420 { CPU_LOG_TB_OP, "op", 421 "show micro ops for each compiled TB" }, 422 { CPU_LOG_TB_OP_OPT, "op_opt", 423 "show micro ops after optimization" }, 424 { CPU_LOG_TB_OP_IND, "op_ind", 425 "show micro ops before indirect lowering" }, 426 { CPU_LOG_INT, "int", 427 "show interrupts/exceptions in short format" }, 428 { CPU_LOG_EXEC, "exec", 429 "show trace before each executed TB (lots of logs)" }, 430 { CPU_LOG_TB_CPU, "cpu", 431 "show CPU registers before entering a TB (lots of logs)" }, 432 { CPU_LOG_TB_FPU, "fpu", 433 "include FPU registers in the 'cpu' logging" }, 434 { CPU_LOG_MMU, "mmu", 435 "log MMU-related activities" }, 436 { CPU_LOG_PCALL, "pcall", 437 "x86 only: show protected mode far calls/returns/exceptions" }, 438 { CPU_LOG_RESET, "cpu_reset", 439 "show CPU state before CPU resets" }, 440 { LOG_UNIMP, "unimp", 441 "log unimplemented functionality" }, 442 { LOG_GUEST_ERROR, "guest_errors", 443 "log when the guest OS does something invalid (eg accessing a\n" 444 "non-existent register)" }, 445 { CPU_LOG_PAGE, "page", 446 "dump pages at beginning of user mode emulation" }, 447 { CPU_LOG_TB_NOCHAIN, "nochain", 448 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" 449 "complete traces" }, 450 #ifdef CONFIG_PLUGIN 451 { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"}, 452 #endif 453 { LOG_STRACE, "strace", 454 "log every user-mode syscall, its input, and its result" }, 455 { LOG_PER_THREAD, "tid", 456 "open a separate log file per thread; filename must contain '%d'" }, 457 { 0, NULL, NULL }, 458 }; 459 460 /* takes a comma separated list of log masks. Return 0 if error. */ 461 int qemu_str_to_log_mask(const char *str) 462 { 463 const QEMULogItem *item; 464 int mask = 0; 465 char **parts = g_strsplit(str, ",", 0); 466 char **tmp; 467 468 for (tmp = parts; tmp && *tmp; tmp++) { 469 if (g_str_equal(*tmp, "all")) { 470 for (item = qemu_log_items; item->mask != 0; item++) { 471 mask |= item->mask; 472 } 473 #ifdef CONFIG_TRACE_LOG 474 } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') { 475 trace_enable_events((*tmp) + 6); 476 mask |= LOG_TRACE; 477 #endif 478 } else { 479 for (item = qemu_log_items; item->mask != 0; item++) { 480 if (g_str_equal(*tmp, item->name)) { 481 goto found; 482 } 483 } 484 goto error; 485 found: 486 mask |= item->mask; 487 } 488 } 489 490 g_strfreev(parts); 491 return mask; 492 493 error: 494 g_strfreev(parts); 495 return 0; 496 } 497 498 void qemu_print_log_usage(FILE *f) 499 { 500 const QEMULogItem *item; 501 fprintf(f, "Log items (comma separated):\n"); 502 for (item = qemu_log_items; item->mask != 0; item++) { 503 fprintf(f, "%-15s %s\n", item->name, item->help); 504 } 505 #ifdef CONFIG_TRACE_LOG 506 fprintf(f, "trace:PATTERN enable trace events\n"); 507 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); 508 #endif 509 } 510