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