1d890d50dSDenis V. Lunev /* 2d890d50dSDenis V. Lunev * Logging support 3d890d50dSDenis V. Lunev * 4d890d50dSDenis V. Lunev * Copyright (c) 2003 Fabrice Bellard 5d890d50dSDenis V. Lunev * 6d890d50dSDenis V. Lunev * This library is free software; you can redistribute it and/or 7d890d50dSDenis V. Lunev * modify it under the terms of the GNU Lesser General Public 8d890d50dSDenis V. Lunev * License as published by the Free Software Foundation; either 9d890d50dSDenis V. Lunev * version 2 of the License, or (at your option) any later version. 10d890d50dSDenis V. Lunev * 11d890d50dSDenis V. Lunev * This library is distributed in the hope that it will be useful, 12d890d50dSDenis V. Lunev * but WITHOUT ANY WARRANTY; without even the implied warranty of 13d890d50dSDenis V. Lunev * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14d890d50dSDenis V. Lunev * Lesser General Public License for more details. 15d890d50dSDenis V. Lunev * 16d890d50dSDenis V. Lunev * You should have received a copy of the GNU Lesser General Public 17d890d50dSDenis V. Lunev * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18d890d50dSDenis V. Lunev */ 19d890d50dSDenis V. Lunev 20d38ea87aSPeter Maydell #include "qemu/osdep.h" 21d890d50dSDenis V. Lunev #include "qemu-common.h" 22d890d50dSDenis V. Lunev #include "qemu/log.h" 233514552eSAlex Bennée #include "qemu/range.h" 243514552eSAlex Bennée #include "qemu/error-report.h" 25bd6fee9fSMarkus Armbruster #include "qapi/error.h" 263514552eSAlex Bennée #include "qemu/cutils.h" 27c84ea00dSPaolo Bonzini #include "trace/control.h" 28d890d50dSDenis V. Lunev 29d890d50dSDenis V. Lunev static char *logfilename; 30d890d50dSDenis V. Lunev FILE *qemu_logfile; 31d890d50dSDenis V. Lunev int qemu_loglevel; 32d890d50dSDenis V. Lunev static int log_append = 0; 333514552eSAlex Bennée static GArray *debug_regions; 34d890d50dSDenis V. Lunev 35d890d50dSDenis V. Lunev void qemu_log(const char *fmt, ...) 36d890d50dSDenis V. Lunev { 37d890d50dSDenis V. Lunev va_list ap; 38d890d50dSDenis V. Lunev 39d890d50dSDenis V. Lunev va_start(ap, fmt); 40d890d50dSDenis V. Lunev if (qemu_logfile) { 41d890d50dSDenis V. Lunev vfprintf(qemu_logfile, fmt, ap); 42d890d50dSDenis V. Lunev } 43d890d50dSDenis V. Lunev va_end(ap); 44d890d50dSDenis V. Lunev } 45d890d50dSDenis V. Lunev 46f2937a33SPaolo Bonzini static bool log_uses_own_buffers; 47f2937a33SPaolo Bonzini 48d890d50dSDenis V. Lunev /* enable or disable low levels log */ 49f2937a33SPaolo Bonzini void qemu_set_log(int log_flags) 50d890d50dSDenis V. Lunev { 51d890d50dSDenis V. Lunev qemu_loglevel = log_flags; 52ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 53ed7f5f1dSPaolo Bonzini qemu_loglevel |= LOG_TRACE; 54ed7f5f1dSPaolo Bonzini #endif 55c586eac3SPaolo Bonzini if (!qemu_logfile && 56c586eac3SPaolo Bonzini (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { 57d890d50dSDenis V. Lunev if (logfilename) { 58d890d50dSDenis V. Lunev qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); 59d890d50dSDenis V. Lunev if (!qemu_logfile) { 60d890d50dSDenis V. Lunev perror(logfilename); 61d890d50dSDenis V. Lunev _exit(1); 62d890d50dSDenis V. Lunev } 6396c33a45SDimitris Aragiorgis /* In case we are a daemon redirect stderr to logfile */ 6496c33a45SDimitris Aragiorgis if (is_daemonized()) { 6596c33a45SDimitris Aragiorgis dup2(fileno(qemu_logfile), STDERR_FILENO); 6696c33a45SDimitris Aragiorgis fclose(qemu_logfile); 6796c33a45SDimitris Aragiorgis /* This will skip closing logfile in qemu_log_close() */ 6896c33a45SDimitris Aragiorgis qemu_logfile = stderr; 6996c33a45SDimitris Aragiorgis } 70d890d50dSDenis V. Lunev } else { 71d890d50dSDenis V. Lunev /* Default to stderr if no log file specified */ 72c586eac3SPaolo Bonzini assert(!is_daemonized()); 73d890d50dSDenis V. Lunev qemu_logfile = stderr; 74d890d50dSDenis V. Lunev } 75d890d50dSDenis V. Lunev /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ 76f2937a33SPaolo Bonzini if (log_uses_own_buffers) { 77d890d50dSDenis V. Lunev static char logfile_buf[4096]; 78d890d50dSDenis V. Lunev 79d890d50dSDenis V. Lunev setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); 80d890d50dSDenis V. Lunev } else { 81d890d50dSDenis V. Lunev #if defined(_WIN32) 82d890d50dSDenis V. Lunev /* Win32 doesn't support line-buffering, so use unbuffered output. */ 83d890d50dSDenis V. Lunev setvbuf(qemu_logfile, NULL, _IONBF, 0); 84d890d50dSDenis V. Lunev #else 85d890d50dSDenis V. Lunev setvbuf(qemu_logfile, NULL, _IOLBF, 0); 86d890d50dSDenis V. Lunev #endif 87d890d50dSDenis V. Lunev log_append = 1; 88d890d50dSDenis V. Lunev } 89d890d50dSDenis V. Lunev } 90c586eac3SPaolo Bonzini if (qemu_logfile && 91c586eac3SPaolo Bonzini (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { 92d890d50dSDenis V. Lunev qemu_log_close(); 93d890d50dSDenis V. Lunev } 94d890d50dSDenis V. Lunev } 95f2937a33SPaolo Bonzini 96f2937a33SPaolo Bonzini void qemu_log_needs_buffers(void) 97f2937a33SPaolo Bonzini { 98f2937a33SPaolo Bonzini log_uses_own_buffers = true; 99f2937a33SPaolo Bonzini } 100f2937a33SPaolo Bonzini 101f6880b7fSAlex Bennée /* 102f6880b7fSAlex Bennée * Allow the user to include %d in their logfile which will be 103f6880b7fSAlex Bennée * substituted with the current PID. This is useful for debugging many 104f6880b7fSAlex Bennée * nested linux-user tasks but will result in lots of logs. 105f6880b7fSAlex Bennée */ 106daa76aa4SMarkus Armbruster void qemu_set_log_filename(const char *filename, Error **errp) 107d890d50dSDenis V. Lunev { 108f6880b7fSAlex Bennée char *pidstr; 109d890d50dSDenis V. Lunev g_free(logfilename); 110f6880b7fSAlex Bennée 111f6880b7fSAlex Bennée pidstr = strstr(filename, "%"); 112f6880b7fSAlex Bennée if (pidstr) { 113f6880b7fSAlex Bennée /* We only accept one %d, no other format strings */ 114f6880b7fSAlex Bennée if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { 115daa76aa4SMarkus Armbruster error_setg(errp, "Bad logfile format: %s", filename); 116daa76aa4SMarkus Armbruster return; 117f6880b7fSAlex Bennée } else { 118f6880b7fSAlex Bennée logfilename = g_strdup_printf(filename, getpid()); 119f6880b7fSAlex Bennée } 120f6880b7fSAlex Bennée } else { 121d890d50dSDenis V. Lunev logfilename = g_strdup(filename); 122f6880b7fSAlex Bennée } 123d890d50dSDenis V. Lunev qemu_log_close(); 124d890d50dSDenis V. Lunev qemu_set_log(qemu_loglevel); 125d890d50dSDenis V. Lunev } 126d890d50dSDenis V. Lunev 1273514552eSAlex Bennée /* Returns true if addr is in our debug filter or no filter defined 1283514552eSAlex Bennée */ 1293514552eSAlex Bennée bool qemu_log_in_addr_range(uint64_t addr) 1303514552eSAlex Bennée { 1313514552eSAlex Bennée if (debug_regions) { 1323514552eSAlex Bennée int i = 0; 1333514552eSAlex Bennée for (i = 0; i < debug_regions->len; i++) { 13458e19e6eSMarkus Armbruster Range *range = &g_array_index(debug_regions, Range, i); 135a0efbf16SMarkus Armbruster if (range_contains(range, addr)) { 1363514552eSAlex Bennée return true; 1373514552eSAlex Bennée } 1383514552eSAlex Bennée } 1393514552eSAlex Bennée return false; 1403514552eSAlex Bennée } else { 1413514552eSAlex Bennée return true; 1423514552eSAlex Bennée } 1433514552eSAlex Bennée } 1443514552eSAlex Bennée 1453514552eSAlex Bennée 146bd6fee9fSMarkus Armbruster void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) 1473514552eSAlex Bennée { 1483514552eSAlex Bennée gchar **ranges = g_strsplit(filter_spec, ",", 0); 149bd6fee9fSMarkus Armbruster int i; 1502ec62faeSMarkus Armbruster 1512ec62faeSMarkus Armbruster if (debug_regions) { 1522ec62faeSMarkus Armbruster g_array_unref(debug_regions); 1532ec62faeSMarkus Armbruster debug_regions = NULL; 1542ec62faeSMarkus Armbruster } 1552ec62faeSMarkus Armbruster 1563514552eSAlex Bennée debug_regions = g_array_sized_new(FALSE, FALSE, 1573514552eSAlex Bennée sizeof(Range), g_strv_length(ranges)); 158bd6fee9fSMarkus Armbruster for (i = 0; ranges[i]; i++) { 159bd6fee9fSMarkus Armbruster const char *r = ranges[i]; 160bd6fee9fSMarkus Armbruster const char *range_op, *r2, *e; 16158e19e6eSMarkus Armbruster uint64_t r1val, r2val, lob, upb; 162bd6fee9fSMarkus Armbruster struct Range range; 163bd6fee9fSMarkus Armbruster 164bd6fee9fSMarkus Armbruster range_op = strstr(r, "-"); 165bd6fee9fSMarkus Armbruster r2 = range_op ? range_op + 1 : NULL; 1663514552eSAlex Bennée if (!range_op) { 1673514552eSAlex Bennée range_op = strstr(r, "+"); 1683514552eSAlex Bennée r2 = range_op ? range_op + 1 : NULL; 1693514552eSAlex Bennée } 1703514552eSAlex Bennée if (!range_op) { 1713514552eSAlex Bennée range_op = strstr(r, ".."); 1723514552eSAlex Bennée r2 = range_op ? range_op + 2 : NULL; 1733514552eSAlex Bennée } 174bd6fee9fSMarkus Armbruster if (!range_op) { 175bd6fee9fSMarkus Armbruster error_setg(errp, "Bad range specifier"); 176bd6fee9fSMarkus Armbruster goto out; 177bd6fee9fSMarkus Armbruster } 1783514552eSAlex Bennée 179bd6fee9fSMarkus Armbruster if (qemu_strtoull(r, &e, 0, &r1val) 180bd6fee9fSMarkus Armbruster || e != range_op) { 181bd6fee9fSMarkus Armbruster error_setg(errp, "Invalid number to the left of %.*s", 182bd6fee9fSMarkus Armbruster (int)(r2 - range_op), range_op); 183bd6fee9fSMarkus Armbruster goto out; 184bd6fee9fSMarkus Armbruster } 185bd6fee9fSMarkus Armbruster if (qemu_strtoull(r2, NULL, 0, &r2val)) { 186bd6fee9fSMarkus Armbruster error_setg(errp, "Invalid number to the right of %.*s", 187bd6fee9fSMarkus Armbruster (int)(r2 - range_op), range_op); 188bd6fee9fSMarkus Armbruster goto out; 189bd6fee9fSMarkus Armbruster } 1903514552eSAlex Bennée 1913514552eSAlex Bennée switch (*range_op) { 1923514552eSAlex Bennée case '+': 19358e19e6eSMarkus Armbruster lob = r1val; 19458e19e6eSMarkus Armbruster upb = r1val + r2val - 1; 1953514552eSAlex Bennée break; 1963514552eSAlex Bennée case '-': 19758e19e6eSMarkus Armbruster upb = r1val; 19858e19e6eSMarkus Armbruster lob = r1val - (r2val - 1); 1993514552eSAlex Bennée break; 2003514552eSAlex Bennée case '.': 20158e19e6eSMarkus Armbruster lob = r1val; 20258e19e6eSMarkus Armbruster upb = r2val; 2033514552eSAlex Bennée break; 2043514552eSAlex Bennée default: 2053514552eSAlex Bennée g_assert_not_reached(); 2063514552eSAlex Bennée } 207*58eeb83cSMarkus Armbruster if (lob > upb) { 20858e19e6eSMarkus Armbruster error_setg(errp, "Invalid range"); 20958e19e6eSMarkus Armbruster goto out; 21058e19e6eSMarkus Armbruster } 211a0efbf16SMarkus Armbruster range_set_bounds(&range, lob, upb); 2123514552eSAlex Bennée g_array_append_val(debug_regions, range); 2133514552eSAlex Bennée } 214bd6fee9fSMarkus Armbruster out: 2153514552eSAlex Bennée g_strfreev(ranges); 2163514552eSAlex Bennée } 2173514552eSAlex Bennée 21899affd1dSDenis V. Lunev /* fflush() the log file */ 21999affd1dSDenis V. Lunev void qemu_log_flush(void) 22099affd1dSDenis V. Lunev { 22199affd1dSDenis V. Lunev fflush(qemu_logfile); 22299affd1dSDenis V. Lunev } 22399affd1dSDenis V. Lunev 22499affd1dSDenis V. Lunev /* Close the log file */ 22599affd1dSDenis V. Lunev void qemu_log_close(void) 22699affd1dSDenis V. Lunev { 22799affd1dSDenis V. Lunev if (qemu_logfile) { 22899affd1dSDenis V. Lunev if (qemu_logfile != stderr) { 22999affd1dSDenis V. Lunev fclose(qemu_logfile); 23099affd1dSDenis V. Lunev } 23199affd1dSDenis V. Lunev qemu_logfile = NULL; 23299affd1dSDenis V. Lunev } 23399affd1dSDenis V. Lunev } 23499affd1dSDenis V. Lunev 235d890d50dSDenis V. Lunev const QEMULogItem qemu_log_items[] = { 236d890d50dSDenis V. Lunev { CPU_LOG_TB_OUT_ASM, "out_asm", 237d890d50dSDenis V. Lunev "show generated host assembly code for each compiled TB" }, 238d890d50dSDenis V. Lunev { CPU_LOG_TB_IN_ASM, "in_asm", 239d890d50dSDenis V. Lunev "show target assembly code for each compiled TB" }, 240d890d50dSDenis V. Lunev { CPU_LOG_TB_OP, "op", 241d890d50dSDenis V. Lunev "show micro ops for each compiled TB" }, 242d890d50dSDenis V. Lunev { CPU_LOG_TB_OP_OPT, "op_opt", 243d890d50dSDenis V. Lunev "show micro ops (x86 only: before eflags optimization) and\n" 244d890d50dSDenis V. Lunev "after liveness analysis" }, 245d890d50dSDenis V. Lunev { CPU_LOG_INT, "int", 246d890d50dSDenis V. Lunev "show interrupts/exceptions in short format" }, 247d890d50dSDenis V. Lunev { CPU_LOG_EXEC, "exec", 248d890d50dSDenis V. Lunev "show trace before each executed TB (lots of logs)" }, 249d890d50dSDenis V. Lunev { CPU_LOG_TB_CPU, "cpu", 25054195736SAlex Bennée "show CPU registers before entering a TB (lots of logs)" }, 251d890d50dSDenis V. Lunev { CPU_LOG_MMU, "mmu", 252d890d50dSDenis V. Lunev "log MMU-related activities" }, 253d890d50dSDenis V. Lunev { CPU_LOG_PCALL, "pcall", 254d890d50dSDenis V. Lunev "x86 only: show protected mode far calls/returns/exceptions" }, 255d890d50dSDenis V. Lunev { CPU_LOG_RESET, "cpu_reset", 256d890d50dSDenis V. Lunev "show CPU state before CPU resets" }, 257d890d50dSDenis V. Lunev { LOG_UNIMP, "unimp", 258d890d50dSDenis V. Lunev "log unimplemented functionality" }, 259d890d50dSDenis V. Lunev { LOG_GUEST_ERROR, "guest_errors", 260d890d50dSDenis V. Lunev "log when the guest OS does something invalid (eg accessing a\n" 261d890d50dSDenis V. Lunev "non-existent register)" }, 262d890d50dSDenis V. Lunev { CPU_LOG_PAGE, "page", 263d890d50dSDenis V. Lunev "dump pages at beginning of user mode emulation" }, 264d890d50dSDenis V. Lunev { CPU_LOG_TB_NOCHAIN, "nochain", 265d890d50dSDenis V. Lunev "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" 266d890d50dSDenis V. Lunev "complete traces" }, 267d890d50dSDenis V. Lunev { 0, NULL, NULL }, 268d890d50dSDenis V. Lunev }; 269d890d50dSDenis V. Lunev 270d890d50dSDenis V. Lunev static int cmp1(const char *s1, int n, const char *s2) 271d890d50dSDenis V. Lunev { 272d890d50dSDenis V. Lunev if (strlen(s2) != n) { 273d890d50dSDenis V. Lunev return 0; 274d890d50dSDenis V. Lunev } 275d890d50dSDenis V. Lunev return memcmp(s1, s2, n) == 0; 276d890d50dSDenis V. Lunev } 277d890d50dSDenis V. Lunev 278d890d50dSDenis V. Lunev /* takes a comma separated list of log masks. Return 0 if error. */ 279d890d50dSDenis V. Lunev int qemu_str_to_log_mask(const char *str) 280d890d50dSDenis V. Lunev { 281d890d50dSDenis V. Lunev const QEMULogItem *item; 282d890d50dSDenis V. Lunev int mask; 283d890d50dSDenis V. Lunev const char *p, *p1; 284d890d50dSDenis V. Lunev 285d890d50dSDenis V. Lunev p = str; 286d890d50dSDenis V. Lunev mask = 0; 287d890d50dSDenis V. Lunev for (;;) { 288d890d50dSDenis V. Lunev p1 = strchr(p, ','); 289d890d50dSDenis V. Lunev if (!p1) { 290d890d50dSDenis V. Lunev p1 = p + strlen(p); 291d890d50dSDenis V. Lunev } 292d890d50dSDenis V. Lunev if (cmp1(p,p1-p,"all")) { 293d890d50dSDenis V. Lunev for (item = qemu_log_items; item->mask != 0; item++) { 294d890d50dSDenis V. Lunev mask |= item->mask; 295d890d50dSDenis V. Lunev } 296c84ea00dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 297c84ea00dSPaolo Bonzini } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) { 298c84ea00dSPaolo Bonzini trace_enable_events(p + 6); 299c84ea00dSPaolo Bonzini mask |= LOG_TRACE; 300c84ea00dSPaolo Bonzini #endif 301d890d50dSDenis V. Lunev } else { 302d890d50dSDenis V. Lunev for (item = qemu_log_items; item->mask != 0; item++) { 303d890d50dSDenis V. Lunev if (cmp1(p, p1 - p, item->name)) { 304d890d50dSDenis V. Lunev goto found; 305d890d50dSDenis V. Lunev } 306d890d50dSDenis V. Lunev } 307d890d50dSDenis V. Lunev return 0; 308d890d50dSDenis V. Lunev found: 309d890d50dSDenis V. Lunev mask |= item->mask; 310c84ea00dSPaolo Bonzini } 311d890d50dSDenis V. Lunev if (*p1 != ',') { 312d890d50dSDenis V. Lunev break; 313d890d50dSDenis V. Lunev } 314d890d50dSDenis V. Lunev p = p1 + 1; 315d890d50dSDenis V. Lunev } 316d890d50dSDenis V. Lunev return mask; 317d890d50dSDenis V. Lunev } 318d890d50dSDenis V. Lunev 319d890d50dSDenis V. Lunev void qemu_print_log_usage(FILE *f) 320d890d50dSDenis V. Lunev { 321d890d50dSDenis V. Lunev const QEMULogItem *item; 322d890d50dSDenis V. Lunev fprintf(f, "Log items (comma separated):\n"); 323d890d50dSDenis V. Lunev for (item = qemu_log_items; item->mask != 0; item++) { 324c84ea00dSPaolo Bonzini fprintf(f, "%-15s %s\n", item->name, item->help); 325d890d50dSDenis V. Lunev } 326c84ea00dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 327c84ea00dSPaolo Bonzini fprintf(f, "trace:PATTERN enable trace events\n"); 328c84ea00dSPaolo Bonzini fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); 329c84ea00dSPaolo Bonzini #endif 330d890d50dSDenis V. Lunev } 331