2012-06-03 15:03:23 +00:00
|
|
|
/*
|
|
|
|
* Logging support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:50:05 +00:00
|
|
|
#include "qemu/osdep.h"
|
2012-06-03 15:03:23 +00:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/log.h"
|
2016-03-15 14:30:20 +00:00
|
|
|
#include "qemu/range.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/cutils.h"
|
2016-01-07 13:55:32 +00:00
|
|
|
#include "trace/control.h"
|
2012-06-03 15:03:23 +00:00
|
|
|
|
2013-01-22 10:08:03 +00:00
|
|
|
static char *logfilename;
|
2012-06-03 16:35:32 +00:00
|
|
|
FILE *qemu_logfile;
|
|
|
|
int qemu_loglevel;
|
2012-06-03 15:03:23 +00:00
|
|
|
static int log_append = 0;
|
2016-03-15 14:30:20 +00:00
|
|
|
static GArray *debug_regions;
|
2012-06-03 15:03:23 +00:00
|
|
|
|
2012-06-03 16:35:32 +00:00
|
|
|
void qemu_log(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (qemu_logfile) {
|
|
|
|
vfprintf(qemu_logfile, fmt, ap);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-12-04 12:12:57 +00:00
|
|
|
static bool log_uses_own_buffers;
|
|
|
|
|
2012-06-03 15:03:23 +00:00
|
|
|
/* enable or disable low levels log */
|
2015-12-04 12:12:57 +00:00
|
|
|
void qemu_set_log(int log_flags)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2012-06-03 16:35:32 +00:00
|
|
|
qemu_loglevel = log_flags;
|
2016-01-07 13:55:30 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
qemu_loglevel |= LOG_TRACE;
|
|
|
|
#endif
|
2016-02-29 11:18:40 +00:00
|
|
|
if (!qemu_logfile &&
|
|
|
|
(is_daemonized() ? logfilename != NULL : qemu_loglevel)) {
|
2013-02-26 17:52:40 +00:00
|
|
|
if (logfilename) {
|
|
|
|
qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
|
|
|
|
if (!qemu_logfile) {
|
|
|
|
perror(logfilename);
|
|
|
|
_exit(1);
|
|
|
|
}
|
2016-02-18 11:38:38 +00:00
|
|
|
/* In case we are a daemon redirect stderr to logfile */
|
|
|
|
if (is_daemonized()) {
|
|
|
|
dup2(fileno(qemu_logfile), STDERR_FILENO);
|
|
|
|
fclose(qemu_logfile);
|
|
|
|
/* This will skip closing logfile in qemu_log_close() */
|
|
|
|
qemu_logfile = stderr;
|
|
|
|
}
|
2013-02-26 17:52:40 +00:00
|
|
|
} else {
|
|
|
|
/* Default to stderr if no log file specified */
|
2016-02-29 11:18:40 +00:00
|
|
|
assert(!is_daemonized());
|
2013-02-26 17:52:40 +00:00
|
|
|
qemu_logfile = stderr;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
/* must avoid mmap() usage of glibc by setting a buffer "by hand" */
|
2015-12-04 12:12:57 +00:00
|
|
|
if (log_uses_own_buffers) {
|
2012-06-03 15:03:23 +00:00
|
|
|
static char logfile_buf[4096];
|
2012-07-07 14:40:18 +00:00
|
|
|
|
2012-06-03 16:35:32 +00:00
|
|
|
setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
|
2012-07-07 14:40:18 +00:00
|
|
|
} else {
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* Win32 doesn't support line-buffering, so use unbuffered output. */
|
|
|
|
setvbuf(qemu_logfile, NULL, _IONBF, 0);
|
2012-06-03 15:03:23 +00:00
|
|
|
#else
|
2012-07-07 14:40:18 +00:00
|
|
|
setvbuf(qemu_logfile, NULL, _IOLBF, 0);
|
2012-06-03 15:03:23 +00:00
|
|
|
#endif
|
2012-07-07 14:40:18 +00:00
|
|
|
log_append = 1;
|
|
|
|
}
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
2016-02-29 11:18:40 +00:00
|
|
|
if (qemu_logfile &&
|
|
|
|
(is_daemonized() ? logfilename == NULL : !qemu_loglevel)) {
|
2013-02-26 17:52:40 +00:00
|
|
|
qemu_log_close();
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 12:12:57 +00:00
|
|
|
|
|
|
|
void qemu_log_needs_buffers(void)
|
|
|
|
{
|
|
|
|
log_uses_own_buffers = true;
|
|
|
|
}
|
|
|
|
|
2016-03-15 14:30:23 +00:00
|
|
|
/*
|
|
|
|
* Allow the user to include %d in their logfile which will be
|
|
|
|
* substituted with the current PID. This is useful for debugging many
|
|
|
|
* nested linux-user tasks but will result in lots of logs.
|
|
|
|
*/
|
2013-02-11 16:41:20 +00:00
|
|
|
void qemu_set_log_filename(const char *filename)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2016-03-15 14:30:23 +00:00
|
|
|
char *pidstr;
|
2013-01-22 10:08:03 +00:00
|
|
|
g_free(logfilename);
|
2016-03-15 14:30:23 +00:00
|
|
|
|
|
|
|
pidstr = strstr(filename, "%");
|
|
|
|
if (pidstr) {
|
|
|
|
/* We only accept one %d, no other format strings */
|
|
|
|
if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
|
|
|
|
error_report("Bad logfile format: %s", filename);
|
|
|
|
logfilename = NULL;
|
|
|
|
} else {
|
|
|
|
logfilename = g_strdup_printf(filename, getpid());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logfilename = g_strdup(filename);
|
|
|
|
}
|
2013-02-26 17:52:40 +00:00
|
|
|
qemu_log_close();
|
2013-02-11 16:41:23 +00:00
|
|
|
qemu_set_log(qemu_loglevel);
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 14:30:20 +00:00
|
|
|
/* Returns true if addr is in our debug filter or no filter defined
|
|
|
|
*/
|
|
|
|
bool qemu_log_in_addr_range(uint64_t addr)
|
|
|
|
{
|
|
|
|
if (debug_regions) {
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < debug_regions->len; i++) {
|
|
|
|
struct Range *range = &g_array_index(debug_regions, Range, i);
|
|
|
|
if (addr >= range->begin && addr <= range->end) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qemu_set_dfilter_ranges(const char *filter_spec)
|
|
|
|
{
|
|
|
|
gchar **ranges = g_strsplit(filter_spec, ",", 0);
|
2016-06-15 17:27:14 +00:00
|
|
|
|
|
|
|
if (debug_regions) {
|
|
|
|
g_array_unref(debug_regions);
|
|
|
|
debug_regions = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-15 14:30:20 +00:00
|
|
|
if (ranges) {
|
|
|
|
gchar **next = ranges;
|
|
|
|
gchar *r = *next++;
|
2016-06-15 17:27:14 +00:00
|
|
|
|
2016-03-15 14:30:20 +00:00
|
|
|
debug_regions = g_array_sized_new(FALSE, FALSE,
|
|
|
|
sizeof(Range), g_strv_length(ranges));
|
|
|
|
while (r) {
|
|
|
|
char *range_op = strstr(r, "-");
|
|
|
|
char *r2 = range_op ? range_op + 1 : NULL;
|
|
|
|
if (!range_op) {
|
|
|
|
range_op = strstr(r, "+");
|
|
|
|
r2 = range_op ? range_op + 1 : NULL;
|
|
|
|
}
|
|
|
|
if (!range_op) {
|
|
|
|
range_op = strstr(r, "..");
|
|
|
|
r2 = range_op ? range_op + 2 : NULL;
|
|
|
|
}
|
|
|
|
if (range_op) {
|
|
|
|
const char *e = NULL;
|
|
|
|
uint64_t r1val, r2val;
|
|
|
|
|
|
|
|
if ((qemu_strtoull(r, &e, 0, &r1val) == 0) &&
|
|
|
|
(qemu_strtoull(r2, NULL, 0, &r2val) == 0) &&
|
|
|
|
r2val > 0) {
|
|
|
|
struct Range range;
|
|
|
|
|
|
|
|
g_assert(e == range_op);
|
|
|
|
|
|
|
|
switch (*range_op) {
|
|
|
|
case '+':
|
|
|
|
{
|
|
|
|
range.begin = r1val;
|
|
|
|
range.end = r1val + (r2val - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '-':
|
|
|
|
{
|
|
|
|
range.end = r1val;
|
|
|
|
range.begin = r1val - (r2val - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '.':
|
|
|
|
range.begin = r1val;
|
|
|
|
range.end = r2val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
g_array_append_val(debug_regions, range);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
g_error("Failed to parse range in: %s", r);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_error("Bad range specifier in: %s", r);
|
|
|
|
}
|
|
|
|
r = *next++;
|
|
|
|
}
|
|
|
|
g_strfreev(ranges);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 11:36:52 +00:00
|
|
|
/* fflush() the log file */
|
|
|
|
void qemu_log_flush(void)
|
|
|
|
{
|
|
|
|
fflush(qemu_logfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the log file */
|
|
|
|
void qemu_log_close(void)
|
|
|
|
{
|
|
|
|
if (qemu_logfile) {
|
|
|
|
if (qemu_logfile != stderr) {
|
|
|
|
fclose(qemu_logfile);
|
|
|
|
}
|
|
|
|
qemu_logfile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem qemu_log_items[] = {
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_TB_OUT_ASM, "out_asm",
|
|
|
|
"show generated host assembly code for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_IN_ASM, "in_asm",
|
|
|
|
"show target assembly code for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_OP, "op",
|
|
|
|
"show micro ops for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_OP_OPT, "op_opt",
|
2012-07-07 14:40:18 +00:00
|
|
|
"show micro ops (x86 only: before eflags optimization) and\n"
|
2012-06-03 15:03:23 +00:00
|
|
|
"after liveness analysis" },
|
|
|
|
{ CPU_LOG_INT, "int",
|
|
|
|
"show interrupts/exceptions in short format" },
|
|
|
|
{ CPU_LOG_EXEC, "exec",
|
|
|
|
"show trace before each executed TB (lots of logs)" },
|
|
|
|
{ CPU_LOG_TB_CPU, "cpu",
|
2016-03-15 14:30:17 +00:00
|
|
|
"show CPU registers before entering a TB (lots of logs)" },
|
2014-12-13 16:48:18 +00:00
|
|
|
{ CPU_LOG_MMU, "mmu",
|
|
|
|
"log MMU-related activities" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_PCALL, "pcall",
|
2012-07-07 14:40:18 +00:00
|
|
|
"x86 only: show protected mode far calls/returns/exceptions" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_RESET, "cpu_reset",
|
2015-01-27 12:11:26 +00:00
|
|
|
"show CPU state before CPU resets" },
|
2012-06-03 17:04:28 +00:00
|
|
|
{ LOG_UNIMP, "unimp",
|
|
|
|
"log unimplemented functionality" },
|
2012-10-18 13:11:35 +00:00
|
|
|
{ LOG_GUEST_ERROR, "guest_errors",
|
|
|
|
"log when the guest OS does something invalid (eg accessing a\n"
|
|
|
|
"non-existent register)" },
|
2015-11-13 11:32:19 +00:00
|
|
|
{ CPU_LOG_PAGE, "page",
|
|
|
|
"dump pages at beginning of user mode emulation" },
|
2015-09-16 22:33:53 +00:00
|
|
|
{ CPU_LOG_TB_NOCHAIN, "nochain",
|
|
|
|
"do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
|
|
|
|
"complete traces" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ 0, NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmp1(const char *s1, int n, const char *s2)
|
|
|
|
{
|
|
|
|
if (strlen(s2) != n) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return memcmp(s1, s2, n) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* takes a comma separated list of log masks. Return 0 if error. */
|
2013-02-11 16:41:22 +00:00
|
|
|
int qemu_str_to_log_mask(const char *str)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem *item;
|
2012-06-03 15:03:23 +00:00
|
|
|
int mask;
|
|
|
|
const char *p, *p1;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
mask = 0;
|
|
|
|
for (;;) {
|
|
|
|
p1 = strchr(p, ',');
|
|
|
|
if (!p1) {
|
|
|
|
p1 = p + strlen(p);
|
|
|
|
}
|
|
|
|
if (cmp1(p,p1-p,"all")) {
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2012-06-03 15:03:23 +00:00
|
|
|
mask |= item->mask;
|
|
|
|
}
|
2016-01-07 13:55:32 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
} else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
|
|
|
|
trace_enable_events(p + 6);
|
|
|
|
mask |= LOG_TRACE;
|
|
|
|
#endif
|
2012-06-03 15:03:23 +00:00
|
|
|
} else {
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2012-06-03 15:03:23 +00:00
|
|
|
if (cmp1(p, p1 - p, item->name)) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2016-01-07 13:55:32 +00:00
|
|
|
found:
|
|
|
|
mask |= item->mask;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
if (*p1 != ',') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = p1 + 1;
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
2013-02-11 16:41:21 +00:00
|
|
|
|
|
|
|
void qemu_print_log_usage(FILE *f)
|
|
|
|
{
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem *item;
|
2013-02-11 16:41:21 +00:00
|
|
|
fprintf(f, "Log items (comma separated):\n");
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2016-01-07 13:55:32 +00:00
|
|
|
fprintf(f, "%-15s %s\n", item->name, item->help);
|
2013-02-11 16:41:21 +00:00
|
|
|
}
|
2016-01-07 13:55:32 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
fprintf(f, "trace:PATTERN enable trace events\n");
|
|
|
|
fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
|
|
|
|
#endif
|
2013-02-11 16:41:21 +00:00
|
|
|
}
|