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 |
| 9 | * version 2 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-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 21 | #include "qemu/log.h" |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 22 | |
Markus Armbruster | 40a50b0 | 2013-01-22 11:08:03 +0100 | [diff] [blame] | 23 | static char *logfilename; |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 24 | FILE *qemu_logfile; |
| 25 | int qemu_loglevel; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 26 | static int log_append = 0; |
| 27 | |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 28 | void qemu_log(const char *fmt, ...) |
| 29 | { |
| 30 | va_list ap; |
| 31 | |
| 32 | va_start(ap, fmt); |
| 33 | if (qemu_logfile) { |
| 34 | vfprintf(qemu_logfile, fmt, ap); |
| 35 | } |
| 36 | va_end(ap); |
| 37 | } |
| 38 | |
| 39 | void qemu_log_mask(int mask, const char *fmt, ...) |
| 40 | { |
| 41 | va_list ap; |
| 42 | |
| 43 | va_start(ap, fmt); |
| 44 | if ((qemu_loglevel & mask) && qemu_logfile) { |
| 45 | vfprintf(qemu_logfile, fmt, ap); |
| 46 | } |
| 47 | va_end(ap); |
| 48 | } |
| 49 | |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 50 | /* enable or disable low levels log */ |
Peter Maydell | 24537a0 | 2013-02-11 16:41:23 +0000 | [diff] [blame] | 51 | void do_qemu_set_log(int log_flags, bool use_own_buffers) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 52 | { |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 53 | qemu_loglevel = log_flags; |
| 54 | if (qemu_loglevel && !qemu_logfile) { |
Peter Maydell | 989b697 | 2013-02-26 17:52:40 +0000 | [diff] [blame] | 55 | if (logfilename) { |
| 56 | qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); |
| 57 | if (!qemu_logfile) { |
| 58 | perror(logfilename); |
| 59 | _exit(1); |
| 60 | } |
| 61 | } else { |
| 62 | /* Default to stderr if no log file specified */ |
| 63 | qemu_logfile = stderr; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 64 | } |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 65 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 66 | if (use_own_buffers) { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 67 | static char logfile_buf[4096]; |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 68 | |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 69 | setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 70 | } else { |
| 71 | #if defined(_WIN32) |
| 72 | /* Win32 doesn't support line-buffering, so use unbuffered output. */ |
| 73 | setvbuf(qemu_logfile, NULL, _IONBF, 0); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 74 | #else |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 75 | setvbuf(qemu_logfile, NULL, _IOLBF, 0); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 76 | #endif |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 77 | log_append = 1; |
| 78 | } |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 79 | } |
Blue Swirl | eeacee4 | 2012-06-03 16:35:32 +0000 | [diff] [blame] | 80 | if (!qemu_loglevel && qemu_logfile) { |
Peter Maydell | 989b697 | 2013-02-26 17:52:40 +0000 | [diff] [blame] | 81 | qemu_log_close(); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Peter Maydell | 9a7e542 | 2013-02-11 16:41:20 +0000 | [diff] [blame] | 85 | void qemu_set_log_filename(const char *filename) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 86 | { |
Markus Armbruster | 40a50b0 | 2013-01-22 11:08:03 +0100 | [diff] [blame] | 87 | g_free(logfilename); |
Markus Armbruster | 636e0f2 | 2013-01-22 11:08:02 +0100 | [diff] [blame] | 88 | logfilename = g_strdup(filename); |
Peter Maydell | 989b697 | 2013-02-26 17:52:40 +0000 | [diff] [blame] | 89 | qemu_log_close(); |
Peter Maydell | 24537a0 | 2013-02-11 16:41:23 +0000 | [diff] [blame] | 90 | qemu_set_log(qemu_loglevel); |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 93 | const QEMULogItem qemu_log_items[] = { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 94 | { CPU_LOG_TB_OUT_ASM, "out_asm", |
| 95 | "show generated host assembly code for each compiled TB" }, |
| 96 | { CPU_LOG_TB_IN_ASM, "in_asm", |
| 97 | "show target assembly code for each compiled TB" }, |
| 98 | { CPU_LOG_TB_OP, "op", |
| 99 | "show micro ops for each compiled TB" }, |
| 100 | { CPU_LOG_TB_OP_OPT, "op_opt", |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 101 | "show micro ops (x86 only: before eflags optimization) and\n" |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 102 | "after liveness analysis" }, |
| 103 | { CPU_LOG_INT, "int", |
| 104 | "show interrupts/exceptions in short format" }, |
| 105 | { CPU_LOG_EXEC, "exec", |
| 106 | "show trace before each executed TB (lots of logs)" }, |
| 107 | { CPU_LOG_TB_CPU, "cpu", |
| 108 | "show CPU state before block translation" }, |
Antony Pavlov | 339aaf5 | 2014-12-13 19:48:18 +0300 | [diff] [blame] | 109 | { CPU_LOG_MMU, "mmu", |
| 110 | "log MMU-related activities" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 111 | { CPU_LOG_PCALL, "pcall", |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 112 | "x86 only: show protected mode far calls/returns/exceptions" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 113 | { CPU_LOG_RESET, "cpu_reset", |
Blue Swirl | 3437e54 | 2012-07-07 14:40:18 +0000 | [diff] [blame] | 114 | "x86 only: show CPU state before CPU resets" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 115 | { CPU_LOG_IOPORT, "ioport", |
| 116 | "show all i/o ports accesses" }, |
Blue Swirl | dafdf1a | 2012-06-03 17:04:28 +0000 | [diff] [blame] | 117 | { LOG_UNIMP, "unimp", |
| 118 | "log unimplemented functionality" }, |
Peter Maydell | e54eba1 | 2012-10-18 14:11:35 +0100 | [diff] [blame] | 119 | { LOG_GUEST_ERROR, "guest_errors", |
| 120 | "log when the guest OS does something invalid (eg accessing a\n" |
| 121 | "non-existent register)" }, |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 122 | { 0, NULL, NULL }, |
| 123 | }; |
| 124 | |
| 125 | static int cmp1(const char *s1, int n, const char *s2) |
| 126 | { |
| 127 | if (strlen(s2) != n) { |
| 128 | return 0; |
| 129 | } |
| 130 | return memcmp(s1, s2, n) == 0; |
| 131 | } |
| 132 | |
| 133 | /* takes a comma separated list of log masks. Return 0 if error. */ |
Peter Maydell | 4fde1eb | 2013-02-11 16:41:22 +0000 | [diff] [blame] | 134 | int qemu_str_to_log_mask(const char *str) |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 135 | { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 136 | const QEMULogItem *item; |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 137 | int mask; |
| 138 | const char *p, *p1; |
| 139 | |
| 140 | p = str; |
| 141 | mask = 0; |
| 142 | for (;;) { |
| 143 | p1 = strchr(p, ','); |
| 144 | if (!p1) { |
| 145 | p1 = p + strlen(p); |
| 146 | } |
| 147 | if (cmp1(p,p1-p,"all")) { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 148 | for (item = qemu_log_items; item->mask != 0; item++) { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 149 | mask |= item->mask; |
| 150 | } |
| 151 | } else { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 152 | for (item = qemu_log_items; item->mask != 0; item++) { |
Blue Swirl | 5726c27 | 2012-06-03 15:03:23 +0000 | [diff] [blame] | 153 | if (cmp1(p, p1 - p, item->name)) { |
| 154 | goto found; |
| 155 | } |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | found: |
| 160 | mask |= item->mask; |
| 161 | if (*p1 != ',') { |
| 162 | break; |
| 163 | } |
| 164 | p = p1 + 1; |
| 165 | } |
| 166 | return mask; |
| 167 | } |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 168 | |
| 169 | void qemu_print_log_usage(FILE *f) |
| 170 | { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 171 | const QEMULogItem *item; |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 172 | fprintf(f, "Log items (comma separated):\n"); |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 173 | for (item = qemu_log_items; item->mask != 0; item++) { |
Peter Maydell | 59a6fa6 | 2013-02-11 16:41:21 +0000 | [diff] [blame] | 174 | fprintf(f, "%-10s %s\n", item->name, item->help); |
| 175 | } |
| 176 | } |