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/osdep.h" 21 #include "qemu-common.h" 22 #include "qemu/log.h" 23 #include "trace/control.h" 24 25 static char *logfilename; 26 FILE *qemu_logfile; 27 int qemu_loglevel; 28 static int log_append = 0; 29 30 void qemu_log(const char *fmt, ...) 31 { 32 va_list ap; 33 34 va_start(ap, fmt); 35 if (qemu_logfile) { 36 vfprintf(qemu_logfile, fmt, ap); 37 } 38 va_end(ap); 39 } 40 41 void qemu_log_mask(int mask, const char *fmt, ...) 42 { 43 va_list ap; 44 45 va_start(ap, fmt); 46 if ((qemu_loglevel & mask) && qemu_logfile) { 47 vfprintf(qemu_logfile, fmt, ap); 48 } 49 va_end(ap); 50 } 51 52 /* enable or disable low levels log */ 53 void do_qemu_set_log(int log_flags, bool use_own_buffers) 54 { 55 qemu_loglevel = log_flags; 56 #ifdef CONFIG_TRACE_LOG 57 qemu_loglevel |= LOG_TRACE; 58 #endif 59 if (qemu_loglevel && !qemu_logfile) { 60 if (logfilename) { 61 qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); 62 if (!qemu_logfile) { 63 perror(logfilename); 64 _exit(1); 65 } 66 } else { 67 /* Default to stderr if no log file specified */ 68 qemu_logfile = stderr; 69 } 70 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ 71 if (use_own_buffers) { 72 static char logfile_buf[4096]; 73 74 setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); 75 } else { 76 #if defined(_WIN32) 77 /* Win32 doesn't support line-buffering, so use unbuffered output. */ 78 setvbuf(qemu_logfile, NULL, _IONBF, 0); 79 #else 80 setvbuf(qemu_logfile, NULL, _IOLBF, 0); 81 #endif 82 log_append = 1; 83 } 84 } 85 if (!qemu_loglevel && qemu_logfile) { 86 qemu_log_close(); 87 } 88 } 89 90 void qemu_set_log_filename(const char *filename) 91 { 92 g_free(logfilename); 93 logfilename = g_strdup(filename); 94 qemu_log_close(); 95 qemu_set_log(qemu_loglevel); 96 } 97 98 const QEMULogItem qemu_log_items[] = { 99 { CPU_LOG_TB_OUT_ASM, "out_asm", 100 "show generated host assembly code for each compiled TB" }, 101 { CPU_LOG_TB_IN_ASM, "in_asm", 102 "show target assembly code for each compiled TB" }, 103 { CPU_LOG_TB_OP, "op", 104 "show micro ops for each compiled TB" }, 105 { CPU_LOG_TB_OP_OPT, "op_opt", 106 "show micro ops (x86 only: before eflags optimization) and\n" 107 "after liveness analysis" }, 108 { CPU_LOG_INT, "int", 109 "show interrupts/exceptions in short format" }, 110 { CPU_LOG_EXEC, "exec", 111 "show trace before each executed TB (lots of logs)" }, 112 { CPU_LOG_TB_CPU, "cpu", 113 "show CPU state before block translation" }, 114 { CPU_LOG_MMU, "mmu", 115 "log MMU-related activities" }, 116 { CPU_LOG_PCALL, "pcall", 117 "x86 only: show protected mode far calls/returns/exceptions" }, 118 { CPU_LOG_RESET, "cpu_reset", 119 "show CPU state before CPU resets" }, 120 { LOG_UNIMP, "unimp", 121 "log unimplemented functionality" }, 122 { LOG_GUEST_ERROR, "guest_errors", 123 "log when the guest OS does something invalid (eg accessing a\n" 124 "non-existent register)" }, 125 { CPU_LOG_PAGE, "page", 126 "dump pages at beginning of user mode emulation" }, 127 { CPU_LOG_TB_NOCHAIN, "nochain", 128 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" 129 "complete traces" }, 130 { 0, NULL, NULL }, 131 }; 132 133 static int cmp1(const char *s1, int n, const char *s2) 134 { 135 if (strlen(s2) != n) { 136 return 0; 137 } 138 return memcmp(s1, s2, n) == 0; 139 } 140 141 /* takes a comma separated list of log masks. Return 0 if error. */ 142 int qemu_str_to_log_mask(const char *str) 143 { 144 const QEMULogItem *item; 145 int mask; 146 const char *p, *p1; 147 148 p = str; 149 mask = 0; 150 for (;;) { 151 p1 = strchr(p, ','); 152 if (!p1) { 153 p1 = p + strlen(p); 154 } 155 if (cmp1(p,p1-p,"all")) { 156 for (item = qemu_log_items; item->mask != 0; item++) { 157 mask |= item->mask; 158 } 159 #ifdef CONFIG_TRACE_LOG 160 } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) { 161 trace_enable_events(p + 6); 162 mask |= LOG_TRACE; 163 #endif 164 } else { 165 for (item = qemu_log_items; item->mask != 0; item++) { 166 if (cmp1(p, p1 - p, item->name)) { 167 goto found; 168 } 169 } 170 return 0; 171 found: 172 mask |= item->mask; 173 } 174 if (*p1 != ',') { 175 break; 176 } 177 p = p1 + 1; 178 } 179 return mask; 180 } 181 182 void qemu_print_log_usage(FILE *f) 183 { 184 const QEMULogItem *item; 185 fprintf(f, "Log items (comma separated):\n"); 186 for (item = qemu_log_items; item->mask != 0; item++) { 187 fprintf(f, "%-15s %s\n", item->name, item->help); 188 } 189 #ifdef CONFIG_TRACE_LOG 190 fprintf(f, "trace:PATTERN enable trace events\n"); 191 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); 192 #endif 193 } 194