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.1 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/log.h" 22 #include "qemu/range.h" 23 #include "qemu/error-report.h" 24 #include "qapi/error.h" 25 #include "qemu/cutils.h" 26 #include "trace/control.h" 27 #include "qemu/thread.h" 28 #include "qemu/lockable.h" 29 30 static char *logfilename; 31 static QemuMutex qemu_logfile_mutex; 32 QemuLogFile *qemu_logfile; 33 int qemu_loglevel; 34 static int log_append = 0; 35 static GArray *debug_regions; 36 37 /* Lock/unlock output. */ 38 39 FILE *qemu_log_trylock(void) 40 { 41 QemuLogFile *logfile; 42 43 rcu_read_lock(); 44 logfile = qatomic_rcu_read(&qemu_logfile); 45 if (logfile) { 46 qemu_flockfile(logfile->fd); 47 return logfile->fd; 48 } else { 49 rcu_read_unlock(); 50 return NULL; 51 } 52 } 53 54 void qemu_log_unlock(FILE *fd) 55 { 56 if (fd) { 57 qemu_funlockfile(fd); 58 rcu_read_unlock(); 59 } 60 } 61 62 /* Return the number of characters emitted. */ 63 int qemu_log(const char *fmt, ...) 64 { 65 int ret = 0; 66 QemuLogFile *logfile; 67 68 rcu_read_lock(); 69 logfile = qatomic_rcu_read(&qemu_logfile); 70 if (logfile) { 71 va_list ap; 72 va_start(ap, fmt); 73 ret = vfprintf(logfile->fd, fmt, ap); 74 va_end(ap); 75 76 /* Don't pass back error results. */ 77 if (ret < 0) { 78 ret = 0; 79 } 80 } 81 rcu_read_unlock(); 82 return ret; 83 } 84 85 static void __attribute__((__constructor__)) qemu_logfile_init(void) 86 { 87 qemu_mutex_init(&qemu_logfile_mutex); 88 } 89 90 static void qemu_logfile_free(QemuLogFile *logfile) 91 { 92 g_assert(logfile); 93 94 if (logfile->fd != stderr) { 95 fclose(logfile->fd); 96 } 97 g_free(logfile); 98 } 99 100 /* enable or disable low levels log */ 101 bool qemu_set_log(int log_flags, Error **errp) 102 { 103 bool need_to_open_file = false; 104 QemuLogFile *logfile; 105 106 qemu_loglevel = log_flags; 107 #ifdef CONFIG_TRACE_LOG 108 qemu_loglevel |= LOG_TRACE; 109 #endif 110 /* 111 * In all cases we only log if qemu_loglevel is set. 112 * Also: 113 * If not daemonized we will always log either to stderr 114 * or to a file (if there is a logfilename). 115 * If we are daemonized, 116 * we will only log if there is a logfilename. 117 */ 118 if (qemu_loglevel && (!is_daemonized() || logfilename)) { 119 need_to_open_file = true; 120 } 121 QEMU_LOCK_GUARD(&qemu_logfile_mutex); 122 if (qemu_logfile && !need_to_open_file) { 123 logfile = qemu_logfile; 124 qatomic_rcu_set(&qemu_logfile, NULL); 125 call_rcu(logfile, qemu_logfile_free, rcu); 126 } else if (!qemu_logfile && need_to_open_file) { 127 logfile = g_new0(QemuLogFile, 1); 128 if (logfilename) { 129 logfile->fd = fopen(logfilename, log_append ? "a" : "w"); 130 if (!logfile->fd) { 131 error_setg_errno(errp, errno, "Error opening logfile %s", 132 logfilename); 133 return false; 134 } 135 /* In case we are a daemon redirect stderr to logfile */ 136 if (is_daemonized()) { 137 dup2(fileno(logfile->fd), STDERR_FILENO); 138 fclose(logfile->fd); 139 /* This will skip closing logfile in qemu_log_close() */ 140 logfile->fd = stderr; 141 } 142 } else { 143 /* Default to stderr if no log file specified */ 144 assert(!is_daemonized()); 145 logfile->fd = stderr; 146 } 147 148 #if defined(_WIN32) 149 /* Win32 doesn't support line-buffering, so use unbuffered output. */ 150 setvbuf(logfile->fd, NULL, _IONBF, 0); 151 #else 152 setvbuf(logfile->fd, NULL, _IOLBF, 0); 153 #endif 154 log_append = 1; 155 qatomic_rcu_set(&qemu_logfile, logfile); 156 } 157 return true; 158 } 159 160 /* 161 * Allow the user to include %d in their logfile which will be 162 * substituted with the current PID. This is useful for debugging many 163 * nested linux-user tasks but will result in lots of logs. 164 * 165 * filename may be NULL. In that case, log output is sent to stderr 166 */ 167 bool qemu_set_log_filename(const char *filename, Error **errp) 168 { 169 g_free(logfilename); 170 logfilename = NULL; 171 172 if (filename) { 173 char *pidstr = strstr(filename, "%"); 174 if (pidstr) { 175 /* We only accept one %d, no other format strings */ 176 if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { 177 error_setg(errp, "Bad logfile format: %s", filename); 178 return false; 179 } else { 180 logfilename = g_strdup_printf(filename, getpid()); 181 } 182 } else { 183 logfilename = g_strdup(filename); 184 } 185 } 186 187 qemu_log_close(); 188 return qemu_set_log(qemu_loglevel, errp); 189 } 190 191 /* Returns true if addr is in our debug filter or no filter defined 192 */ 193 bool qemu_log_in_addr_range(uint64_t addr) 194 { 195 if (debug_regions) { 196 int i = 0; 197 for (i = 0; i < debug_regions->len; i++) { 198 Range *range = &g_array_index(debug_regions, Range, i); 199 if (range_contains(range, addr)) { 200 return true; 201 } 202 } 203 return false; 204 } else { 205 return true; 206 } 207 } 208 209 210 void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp) 211 { 212 gchar **ranges = g_strsplit(filter_spec, ",", 0); 213 int i; 214 215 if (debug_regions) { 216 g_array_unref(debug_regions); 217 debug_regions = NULL; 218 } 219 220 debug_regions = g_array_sized_new(FALSE, FALSE, 221 sizeof(Range), g_strv_length(ranges)); 222 for (i = 0; ranges[i]; i++) { 223 const char *r = ranges[i]; 224 const char *range_op, *r2, *e; 225 uint64_t r1val, r2val, lob, upb; 226 struct Range range; 227 228 range_op = strstr(r, "-"); 229 r2 = range_op ? range_op + 1 : NULL; 230 if (!range_op) { 231 range_op = strstr(r, "+"); 232 r2 = range_op ? range_op + 1 : NULL; 233 } 234 if (!range_op) { 235 range_op = strstr(r, ".."); 236 r2 = range_op ? range_op + 2 : NULL; 237 } 238 if (!range_op) { 239 error_setg(errp, "Bad range specifier"); 240 goto out; 241 } 242 243 if (qemu_strtou64(r, &e, 0, &r1val) 244 || e != range_op) { 245 error_setg(errp, "Invalid number to the left of %.*s", 246 (int)(r2 - range_op), range_op); 247 goto out; 248 } 249 if (qemu_strtou64(r2, NULL, 0, &r2val)) { 250 error_setg(errp, "Invalid number to the right of %.*s", 251 (int)(r2 - range_op), range_op); 252 goto out; 253 } 254 255 switch (*range_op) { 256 case '+': 257 lob = r1val; 258 upb = r1val + r2val - 1; 259 break; 260 case '-': 261 upb = r1val; 262 lob = r1val - (r2val - 1); 263 break; 264 case '.': 265 lob = r1val; 266 upb = r2val; 267 break; 268 default: 269 g_assert_not_reached(); 270 } 271 if (lob > upb) { 272 error_setg(errp, "Invalid range"); 273 goto out; 274 } 275 range_set_bounds(&range, lob, upb); 276 g_array_append_val(debug_regions, range); 277 } 278 out: 279 g_strfreev(ranges); 280 } 281 282 /* fflush() the log file */ 283 void qemu_log_flush(void) 284 { 285 QemuLogFile *logfile; 286 287 rcu_read_lock(); 288 logfile = qatomic_rcu_read(&qemu_logfile); 289 if (logfile) { 290 fflush(logfile->fd); 291 } 292 rcu_read_unlock(); 293 } 294 295 /* Close the log file */ 296 void qemu_log_close(void) 297 { 298 QemuLogFile *logfile; 299 300 qemu_mutex_lock(&qemu_logfile_mutex); 301 logfile = qemu_logfile; 302 303 if (logfile) { 304 qatomic_rcu_set(&qemu_logfile, NULL); 305 call_rcu(logfile, qemu_logfile_free, rcu); 306 } 307 qemu_mutex_unlock(&qemu_logfile_mutex); 308 } 309 310 const QEMULogItem qemu_log_items[] = { 311 { CPU_LOG_TB_OUT_ASM, "out_asm", 312 "show generated host assembly code for each compiled TB" }, 313 { CPU_LOG_TB_IN_ASM, "in_asm", 314 "show target assembly code for each compiled TB" }, 315 { CPU_LOG_TB_OP, "op", 316 "show micro ops for each compiled TB" }, 317 { CPU_LOG_TB_OP_OPT, "op_opt", 318 "show micro ops after optimization" }, 319 { CPU_LOG_TB_OP_IND, "op_ind", 320 "show micro ops before indirect lowering" }, 321 { CPU_LOG_INT, "int", 322 "show interrupts/exceptions in short format" }, 323 { CPU_LOG_EXEC, "exec", 324 "show trace before each executed TB (lots of logs)" }, 325 { CPU_LOG_TB_CPU, "cpu", 326 "show CPU registers before entering a TB (lots of logs)" }, 327 { CPU_LOG_TB_FPU, "fpu", 328 "include FPU registers in the 'cpu' logging" }, 329 { CPU_LOG_MMU, "mmu", 330 "log MMU-related activities" }, 331 { CPU_LOG_PCALL, "pcall", 332 "x86 only: show protected mode far calls/returns/exceptions" }, 333 { CPU_LOG_RESET, "cpu_reset", 334 "show CPU state before CPU resets" }, 335 { LOG_UNIMP, "unimp", 336 "log unimplemented functionality" }, 337 { LOG_GUEST_ERROR, "guest_errors", 338 "log when the guest OS does something invalid (eg accessing a\n" 339 "non-existent register)" }, 340 { CPU_LOG_PAGE, "page", 341 "dump pages at beginning of user mode emulation" }, 342 { CPU_LOG_TB_NOCHAIN, "nochain", 343 "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n" 344 "complete traces" }, 345 #ifdef CONFIG_PLUGIN 346 { CPU_LOG_PLUGIN, "plugin", "output from TCG plugins\n"}, 347 #endif 348 { LOG_STRACE, "strace", 349 "log every user-mode syscall, its input, and its result" }, 350 { 0, NULL, NULL }, 351 }; 352 353 /* takes a comma separated list of log masks. Return 0 if error. */ 354 int qemu_str_to_log_mask(const char *str) 355 { 356 const QEMULogItem *item; 357 int mask = 0; 358 char **parts = g_strsplit(str, ",", 0); 359 char **tmp; 360 361 for (tmp = parts; tmp && *tmp; tmp++) { 362 if (g_str_equal(*tmp, "all")) { 363 for (item = qemu_log_items; item->mask != 0; item++) { 364 mask |= item->mask; 365 } 366 #ifdef CONFIG_TRACE_LOG 367 } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') { 368 trace_enable_events((*tmp) + 6); 369 mask |= LOG_TRACE; 370 #endif 371 } else { 372 for (item = qemu_log_items; item->mask != 0; item++) { 373 if (g_str_equal(*tmp, item->name)) { 374 goto found; 375 } 376 } 377 goto error; 378 found: 379 mask |= item->mask; 380 } 381 } 382 383 g_strfreev(parts); 384 return mask; 385 386 error: 387 g_strfreev(parts); 388 return 0; 389 } 390 391 void qemu_print_log_usage(FILE *f) 392 { 393 const QEMULogItem *item; 394 fprintf(f, "Log items (comma separated):\n"); 395 for (item = qemu_log_items; item->mask != 0; item++) { 396 fprintf(f, "%-15s %s\n", item->name, item->help); 397 } 398 #ifdef CONFIG_TRACE_LOG 399 fprintf(f, "trace:PATTERN enable trace events\n"); 400 fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n"); 401 #endif 402 } 403