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/log.h" 223514552eSAlex Bennée #include "qemu/range.h" 233514552eSAlex Bennée #include "qemu/error-report.h" 24bd6fee9fSMarkus Armbruster #include "qapi/error.h" 253514552eSAlex Bennée #include "qemu/cutils.h" 26c84ea00dSPaolo Bonzini #include "trace/control.h" 27d890d50dSDenis V. Lunev 28d890d50dSDenis V. Lunev static char *logfilename; 29d890d50dSDenis V. Lunev FILE *qemu_logfile; 30d890d50dSDenis V. Lunev int qemu_loglevel; 31d890d50dSDenis V. Lunev static int log_append = 0; 323514552eSAlex Bennée static GArray *debug_regions; 33d890d50dSDenis V. Lunev 34bdfb460eSRichard Henderson /* Return the number of characters emitted. */ 35bdfb460eSRichard Henderson int qemu_log(const char *fmt, ...) 36d890d50dSDenis V. Lunev { 37bdfb460eSRichard Henderson int ret = 0; 38d890d50dSDenis V. Lunev if (qemu_logfile) { 39bdfb460eSRichard Henderson va_list ap; 40bdfb460eSRichard Henderson va_start(ap, fmt); 41bdfb460eSRichard Henderson ret = vfprintf(qemu_logfile, fmt, ap); 42d890d50dSDenis V. Lunev va_end(ap); 43bdfb460eSRichard Henderson 44bdfb460eSRichard Henderson /* Don't pass back error results. */ 45bdfb460eSRichard Henderson if (ret < 0) { 46bdfb460eSRichard Henderson ret = 0; 47bdfb460eSRichard Henderson } 48bdfb460eSRichard Henderson } 49bdfb460eSRichard Henderson return ret; 50d890d50dSDenis V. Lunev } 51d890d50dSDenis V. Lunev 52f2937a33SPaolo Bonzini static bool log_uses_own_buffers; 53f2937a33SPaolo Bonzini 54d890d50dSDenis V. Lunev /* enable or disable low levels log */ 55f2937a33SPaolo Bonzini void qemu_set_log(int log_flags) 56d890d50dSDenis V. Lunev { 57d890d50dSDenis V. Lunev qemu_loglevel = log_flags; 58ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 59ed7f5f1dSPaolo Bonzini qemu_loglevel |= LOG_TRACE; 60ed7f5f1dSPaolo Bonzini #endif 61c586eac3SPaolo Bonzini if (!qemu_logfile && 62c586eac3SPaolo Bonzini (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { 63d890d50dSDenis V. Lunev if (logfilename) { 64d890d50dSDenis V. Lunev qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); 65d890d50dSDenis V. Lunev if (!qemu_logfile) { 66d890d50dSDenis V. Lunev perror(logfilename); 67d890d50dSDenis V. Lunev _exit(1); 68d890d50dSDenis V. Lunev } 6996c33a45SDimitris Aragiorgis /* In case we are a daemon redirect stderr to logfile */ 7096c33a45SDimitris Aragiorgis if (is_daemonized()) { 7196c33a45SDimitris Aragiorgis dup2(fileno(qemu_logfile), STDERR_FILENO); 7296c33a45SDimitris Aragiorgis fclose(qemu_logfile); 7396c33a45SDimitris Aragiorgis /* This will skip closing logfile in qemu_log_close() */ 7496c33a45SDimitris Aragiorgis qemu_logfile = stderr; 7596c33a45SDimitris Aragiorgis } 76d890d50dSDenis V. Lunev } else { 77d890d50dSDenis V. Lunev /* Default to stderr if no log file specified */ 78c586eac3SPaolo Bonzini assert(!is_daemonized()); 79d890d50dSDenis V. Lunev qemu_logfile = stderr; 80d890d50dSDenis V. Lunev } 81d890d50dSDenis V. Lunev /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ 82f2937a33SPaolo Bonzini if (log_uses_own_buffers) { 83d890d50dSDenis V. Lunev static char logfile_buf[4096]; 84d890d50dSDenis V. Lunev 85d890d50dSDenis V. Lunev setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); 86d890d50dSDenis V. Lunev } else { 87d890d50dSDenis V. Lunev #if defined(_WIN32) 88d890d50dSDenis V. Lunev /* Win32 doesn't support line-buffering, so use unbuffered output. */ 89d890d50dSDenis V. Lunev setvbuf(qemu_logfile, NULL, _IONBF, 0); 90d890d50dSDenis V. Lunev #else 91d890d50dSDenis V. Lunev setvbuf(qemu_logfile, NULL, _IOLBF, 0); 92d890d50dSDenis V. Lunev #endif 93d890d50dSDenis V. Lunev log_append = 1; 94d890d50dSDenis V. Lunev } 95d890d50dSDenis V. Lunev } 96c586eac3SPaolo Bonzini if (qemu_logfile && 97c586eac3SPaolo Bonzini (is_daemonized() ? logfilename == NULL : !qemu_loglevel)) { 98d890d50dSDenis V. Lunev qemu_log_close(); 99d890d50dSDenis V. Lunev } 100d890d50dSDenis V. Lunev } 101f2937a33SPaolo Bonzini 102f2937a33SPaolo Bonzini void qemu_log_needs_buffers(void) 103f2937a33SPaolo Bonzini { 104f2937a33SPaolo Bonzini log_uses_own_buffers = true; 105f2937a33SPaolo Bonzini } 106f2937a33SPaolo Bonzini 107f6880b7fSAlex Bennée /* 108f6880b7fSAlex Bennée * Allow the user to include %d in their logfile which will be 109f6880b7fSAlex Bennée * substituted with the current PID. This is useful for debugging many 110f6880b7fSAlex Bennée * nested linux-user tasks but will result in lots of logs. 111f6880b7fSAlex Bennée */ 112daa76aa4SMarkus Armbruster void qemu_set_log_filename(const char *filename, Error **errp) 113d890d50dSDenis V. Lunev { 114f6880b7fSAlex Bennée char *pidstr; 115d890d50dSDenis V. Lunev g_free(logfilename); 116*0f516ca4SRobert Foley logfilename = NULL; 117f6880b7fSAlex Bennée 118f6880b7fSAlex Bennée pidstr = strstr(filename, "%"); 119f6880b7fSAlex Bennée if (pidstr) { 120f6880b7fSAlex Bennée /* We only accept one %d, no other format strings */ 121f6880b7fSAlex Bennée if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { 122daa76aa4SMarkus Armbruster error_setg(errp, "Bad logfile format: %s", filename); 123daa76aa4SMarkus Armbruster return; 124f6880b7fSAlex Bennée } else { 125f6880b7fSAlex Bennée logfilename = g_strdup_printf(filename, getpid()); 126f6880b7fSAlex Bennée } 127f6880b7fSAlex Bennée } else { 128d890d50dSDenis V. Lunev logfilename = g_strdup(filename); 129f6880b7fSAlex Bennée } 130d890d50dSDenis V. Lunev qemu_log_close(); 131d890d50dSDenis V. Lunev qemu_set_log(qemu_loglevel); 132d890d50dSDenis V. Lunev } 133d890d50dSDenis V. Lunev 1343514552eSAlex Bennée /* Returns true if addr is in our debug filter or no filter defined 1353514552eSAlex Bennée */ 1363514552eSAlex Bennée bool qemu_log_in_addr_range(uint64_t addr) 1373514552eSAlex Bennée { 1383514552eSAlex Bennée if (debug_regions) { 1393514552eSAlex Bennée int i = 0; 1403514552eSAlex Bennée for (i = 0; i < debug_regions->len; i++) { 14158e19e6eSMarkus Armbruster Range *range = &g_array_index(debug_regions, Range, i); 142a0efbf16SMarkus Armbruster if (range_contains(range, addr)) { 1433514552eSAlex Bennée return true; 1443514552eSAlex Bennée } 1453514552eSAlex Bennée } 1463514552eSAlex Bennée return false; 1473514552eSAlex Bennée } else { 1483514552eSAlex Bennée return true; 1493514552eSAlex Bennée } 1503514552eSAlex Bennée } 1513514552eSAlex Bennée 1523514552eSAlex Bennée 153bd6fee9fSMarkus Armbruster void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) 1543514552eSAlex Bennée { 1553514552eSAlex Bennée gchar **ranges = g_strsplit(filter_spec, ",", 0); 156bd6fee9fSMarkus Armbruster int i; 1572ec62faeSMarkus Armbruster 1582ec62faeSMarkus Armbruster if (debug_regions) { 1592ec62faeSMarkus Armbruster g_array_unref(debug_regions); 1602ec62faeSMarkus Armbruster debug_regions = NULL; 1612ec62faeSMarkus Armbruster } 1622ec62faeSMarkus Armbruster 1633514552eSAlex Bennée debug_regions = g_array_sized_new(FALSE, FALSE, 1643514552eSAlex Bennée sizeof(Range), g_strv_length(ranges)); 165bd6fee9fSMarkus Armbruster for (i = 0; ranges[i]; i++) { 166bd6fee9fSMarkus Armbruster const char *r = ranges[i]; 167bd6fee9fSMarkus Armbruster const char *range_op, *r2, *e; 16858e19e6eSMarkus Armbruster uint64_t r1val, r2val, lob, upb; 169bd6fee9fSMarkus Armbruster struct Range range; 170bd6fee9fSMarkus Armbruster 171bd6fee9fSMarkus Armbruster range_op = strstr(r, "-"); 172bd6fee9fSMarkus Armbruster r2 = range_op ? range_op + 1 : NULL; 1733514552eSAlex Bennée if (!range_op) { 1743514552eSAlex Bennée range_op = strstr(r, "+"); 1753514552eSAlex Bennée r2 = range_op ? range_op + 1 : NULL; 1763514552eSAlex Bennée } 1773514552eSAlex Bennée if (!range_op) { 1783514552eSAlex Bennée range_op = strstr(r, ".."); 1793514552eSAlex Bennée r2 = range_op ? range_op + 2 : NULL; 1803514552eSAlex Bennée } 181bd6fee9fSMarkus Armbruster if (!range_op) { 182bd6fee9fSMarkus Armbruster error_setg(errp, "Bad range specifier"); 183bd6fee9fSMarkus Armbruster goto out; 184bd6fee9fSMarkus Armbruster } 1853514552eSAlex Bennée 186b30d1886SMarkus Armbruster if (qemu_strtou64(r, &e, 0, &r1val) 187bd6fee9fSMarkus Armbruster || e != range_op) { 188bd6fee9fSMarkus Armbruster error_setg(errp, "Invalid number to the left of %.*s", 189bd6fee9fSMarkus Armbruster (int)(r2 - range_op), range_op); 190bd6fee9fSMarkus Armbruster goto out; 191bd6fee9fSMarkus Armbruster } 192b30d1886SMarkus Armbruster if (qemu_strtou64(r2, NULL, 0, &r2val)) { 193bd6fee9fSMarkus Armbruster error_setg(errp, "Invalid number to the right of %.*s", 194bd6fee9fSMarkus Armbruster (int)(r2 - range_op), range_op); 195bd6fee9fSMarkus Armbruster goto out; 196bd6fee9fSMarkus Armbruster } 1973514552eSAlex Bennée 1983514552eSAlex Bennée switch (*range_op) { 1993514552eSAlex Bennée case '+': 20058e19e6eSMarkus Armbruster lob = r1val; 20158e19e6eSMarkus Armbruster upb = r1val + r2val - 1; 2023514552eSAlex Bennée break; 2033514552eSAlex Bennée case '-': 20458e19e6eSMarkus Armbruster upb = r1val; 20558e19e6eSMarkus Armbruster lob = r1val - (r2val - 1); 2063514552eSAlex Bennée break; 2073514552eSAlex Bennée case '.': 20858e19e6eSMarkus Armbruster lob = r1val; 20958e19e6eSMarkus Armbruster upb = r2val; 2103514552eSAlex Bennée break; 2113514552eSAlex Bennée default: 2123514552eSAlex Bennée g_assert_not_reached(); 2133514552eSAlex Bennée } 21458eeb83cSMarkus Armbruster if (lob > upb) { 21558e19e6eSMarkus Armbruster error_setg(errp, "Invalid range"); 21658e19e6eSMarkus Armbruster goto out; 21758e19e6eSMarkus Armbruster } 218a0efbf16SMarkus Armbruster range_set_bounds(&range, lob, upb); 2193514552eSAlex Bennée g_array_append_val(debug_regions, range); 2203514552eSAlex Bennée } 221bd6fee9fSMarkus Armbruster out: 2223514552eSAlex Bennée g_strfreev(ranges); 2233514552eSAlex Bennée } 2243514552eSAlex Bennée 22599affd1dSDenis V. Lunev /* fflush() the log file */ 22699affd1dSDenis V. Lunev void qemu_log_flush(void) 22799affd1dSDenis V. Lunev { 22899affd1dSDenis V. Lunev fflush(qemu_logfile); 22999affd1dSDenis V. Lunev } 23099affd1dSDenis V. Lunev 23199affd1dSDenis V. Lunev /* Close the log file */ 23299affd1dSDenis V. Lunev void qemu_log_close(void) 23399affd1dSDenis V. Lunev { 23499affd1dSDenis V. Lunev if (qemu_logfile) { 23599affd1dSDenis V. Lunev if (qemu_logfile != stderr) { 23699affd1dSDenis V. Lunev fclose(qemu_logfile); 23799affd1dSDenis V. Lunev } 23899affd1dSDenis V. Lunev qemu_logfile = NULL; 23999affd1dSDenis V. Lunev } 24099affd1dSDenis V. Lunev } 24199affd1dSDenis V. Lunev 242d890d50dSDenis V. Lunev const QEMULogItem qemu_log_items[] = { 243d890d50dSDenis V. Lunev { CPU_LOG_TB_OUT_ASM, "out_asm", 244d890d50dSDenis V. Lunev "show generated host assembly code for each compiled TB" }, 245d890d50dSDenis V. Lunev { CPU_LOG_TB_IN_ASM, "in_asm", 246d890d50dSDenis V. Lunev "show target assembly code for each compiled TB" }, 247d890d50dSDenis V. Lunev { CPU_LOG_TB_OP, "op", 248d890d50dSDenis V. Lunev "show micro ops for each compiled TB" }, 249d890d50dSDenis V. Lunev { CPU_LOG_TB_OP_OPT, "op_opt", 2505a18407fSRichard Henderson "show micro ops after optimization" }, 2515a18407fSRichard Henderson { CPU_LOG_TB_OP_IND, "op_ind", 2525a18407fSRichard Henderson "show micro ops before indirect lowering" }, 253d890d50dSDenis V. Lunev { CPU_LOG_INT, "int", 254d890d50dSDenis V. Lunev "show interrupts/exceptions in short format" }, 255d890d50dSDenis V. Lunev { CPU_LOG_EXEC, "exec", 256d890d50dSDenis V. Lunev "show trace before each executed TB (lots of logs)" }, 257d890d50dSDenis V. Lunev { CPU_LOG_TB_CPU, "cpu", 25854195736SAlex Bennée "show CPU registers before entering a TB (lots of logs)" }, 259ae765180SPeter Maydell { CPU_LOG_TB_FPU, "fpu", 260ae765180SPeter Maydell "include FPU registers in the 'cpu' logging" }, 261d890d50dSDenis V. Lunev { CPU_LOG_MMU, "mmu", 262d890d50dSDenis V. Lunev "log MMU-related activities" }, 263d890d50dSDenis V. Lunev { CPU_LOG_PCALL, "pcall", 264d890d50dSDenis V. Lunev "x86 only: show protected mode far calls/returns/exceptions" }, 265d890d50dSDenis V. Lunev { CPU_LOG_RESET, "cpu_reset", 266d890d50dSDenis V. Lunev "show CPU state before CPU resets" }, 267d890d50dSDenis V. Lunev { LOG_UNIMP, "unimp", 268d890d50dSDenis V. Lunev "log unimplemented functionality" }, 269d890d50dSDenis V. Lunev { LOG_GUEST_ERROR, "guest_errors", 270d890d50dSDenis V. Lunev "log when the guest OS does something invalid (eg accessing a\n" 271d890d50dSDenis V. Lunev "non-existent register)" }, 272d890d50dSDenis V. Lunev { CPU_LOG_PAGE, "page", 273d890d50dSDenis V. Lunev "dump pages at beginning of user mode emulation" }, 274d890d50dSDenis V. Lunev { CPU_LOG_TB_NOCHAIN, "nochain", 275d890d50dSDenis V. Lunev "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" 276d890d50dSDenis V. Lunev "complete traces" }, 277ca76a669SAlex Bennée #ifdef CONFIG_PLUGIN 278ca76a669SAlex Bennée { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"}, 279ca76a669SAlex Bennée #endif 280d890d50dSDenis V. Lunev { 0, NULL, NULL }, 281d890d50dSDenis V. Lunev }; 282d890d50dSDenis V. Lunev 283d890d50dSDenis V. Lunev /* takes a comma separated list of log masks. Return 0 if error. */ 284d890d50dSDenis V. Lunev int qemu_str_to_log_mask(const char *str) 285d890d50dSDenis V. Lunev { 286d890d50dSDenis V. Lunev const QEMULogItem *item; 28789d0a64fSDaniel P. Berrange int mask = 0; 28889d0a64fSDaniel P. Berrange char **parts = g_strsplit(str, ",", 0); 28989d0a64fSDaniel P. Berrange char **tmp; 290d890d50dSDenis V. Lunev 29189d0a64fSDaniel P. Berrange for (tmp = parts; tmp && *tmp; tmp++) { 29289d0a64fSDaniel P. Berrange if (g_str_equal(*tmp, "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 29789d0a64fSDaniel P. Berrange } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') { 29889d0a64fSDaniel P. Berrange trace_enable_events((*tmp) + 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++) { 30389d0a64fSDaniel P. Berrange if (g_str_equal(*tmp, item->name)) { 304d890d50dSDenis V. Lunev goto found; 305d890d50dSDenis V. Lunev } 306d890d50dSDenis V. Lunev } 30789d0a64fSDaniel P. Berrange goto error; 308d890d50dSDenis V. Lunev found: 309d890d50dSDenis V. Lunev mask |= item->mask; 310c84ea00dSPaolo Bonzini } 311d890d50dSDenis V. Lunev } 31289d0a64fSDaniel P. Berrange 31389d0a64fSDaniel P. Berrange g_strfreev(parts); 314d890d50dSDenis V. Lunev return mask; 31589d0a64fSDaniel P. Berrange 31689d0a64fSDaniel P. Berrange error: 31789d0a64fSDaniel P. Berrange g_strfreev(parts); 31889d0a64fSDaniel P. Berrange return 0; 319d890d50dSDenis V. Lunev } 320d890d50dSDenis V. Lunev 321d890d50dSDenis V. Lunev void qemu_print_log_usage(FILE *f) 322d890d50dSDenis V. Lunev { 323d890d50dSDenis V. Lunev const QEMULogItem *item; 324d890d50dSDenis V. Lunev fprintf(f, "Log items (comma separated):\n"); 325d890d50dSDenis V. Lunev for (item = qemu_log_items; item->mask != 0; item++) { 326c84ea00dSPaolo Bonzini fprintf(f, "%-15s %s\n", item->name, item->help); 327d890d50dSDenis V. Lunev } 328c84ea00dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG 329c84ea00dSPaolo Bonzini fprintf(f, "trace:PATTERN enable trace events\n"); 330c84ea00dSPaolo Bonzini fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); 331c84ea00dSPaolo Bonzini #endif 332d890d50dSDenis V. Lunev } 333