log.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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/>.
 */

#include "qemu-common.h"
21
#include "qemu/log.h"
P
Paolo Bonzini 已提交
22
#include "trace/control.h"
23

24
static char *logfilename;
B
Blue Swirl 已提交
25 26
FILE *qemu_logfile;
int qemu_loglevel;
27 28
static int log_append = 0;

B
Blue Swirl 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
void qemu_log(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    if (qemu_logfile) {
        vfprintf(qemu_logfile, fmt, ap);
    }
    va_end(ap);
}

void qemu_log_mask(int mask, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    if ((qemu_loglevel & mask) && qemu_logfile) {
        vfprintf(qemu_logfile, fmt, ap);
    }
    va_end(ap);
}

51
/* enable or disable low levels log */
52
void do_qemu_set_log(int log_flags, bool use_own_buffers)
53
{
B
Blue Swirl 已提交
54
    qemu_loglevel = log_flags;
55 56 57
#ifdef CONFIG_TRACE_LOG
    qemu_loglevel |= LOG_TRACE;
#endif
B
Blue Swirl 已提交
58
    if (qemu_loglevel && !qemu_logfile) {
59 60 61 62 63 64 65 66 67
        if (logfilename) {
            qemu_logfile = fopen(logfilename, log_append ? "a" : "w");
            if (!qemu_logfile) {
                perror(logfilename);
                _exit(1);
            }
        } else {
            /* Default to stderr if no log file specified */
            qemu_logfile = stderr;
68 69
        }
        /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
B
Blue Swirl 已提交
70
        if (use_own_buffers) {
71
            static char logfile_buf[4096];
B
Blue Swirl 已提交
72

B
Blue Swirl 已提交
73
            setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
B
Blue Swirl 已提交
74 75 76 77
        } else {
#if defined(_WIN32)
            /* Win32 doesn't support line-buffering, so use unbuffered output. */
            setvbuf(qemu_logfile, NULL, _IONBF, 0);
78
#else
B
Blue Swirl 已提交
79
            setvbuf(qemu_logfile, NULL, _IOLBF, 0);
80
#endif
B
Blue Swirl 已提交
81 82
            log_append = 1;
        }
83
    }
B
Blue Swirl 已提交
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 99 100 101 102 103 104
    { 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",
B
Blue Swirl 已提交
105
      "show micro ops (x86 only: before eflags optimization) and\n"
106 107 108 109 110 111 112
      "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",
      "show CPU state before block translation" },
113 114
    { CPU_LOG_MMU, "mmu",
      "log MMU-related activities" },
115
    { CPU_LOG_PCALL, "pcall",
B
Blue Swirl 已提交
116
      "x86 only: show protected mode far calls/returns/exceptions" },
117
    { CPU_LOG_RESET, "cpu_reset",
118
      "show CPU state before CPU resets" },
119 120
    { LOG_UNIMP, "unimp",
      "log unimplemented functionality" },
121 122 123
    { LOG_GUEST_ERROR, "guest_errors",
      "log when the guest OS does something invalid (eg accessing a\n"
      "non-existent register)" },
P
Paolo Bonzini 已提交
124 125
    { CPU_LOG_PAGE, "page",
      "dump pages at beginning of user mode emulation" },
126 127 128
    { CPU_LOG_TB_NOCHAIN, "nochain",
      "do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
      "complete traces" },
129 130 131 132 133 134 135 136 137 138 139 140
    { 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. */
141
int qemu_str_to_log_mask(const char *str)
142
{
143
    const QEMULogItem *item;
144 145 146 147 148 149 150 151 152 153 154
    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")) {
155
            for (item = qemu_log_items; item->mask != 0; item++) {
156 157
                mask |= item->mask;
            }
P
Paolo Bonzini 已提交
158 159 160 161 162
#ifdef CONFIG_TRACE_LOG
        } else if (strncmp(p, "trace:", 6) == 0 && p + 6 != p1) {
            trace_enable_events(p + 6);
            mask |= LOG_TRACE;
#endif
163
        } else {
164
            for (item = qemu_log_items; item->mask != 0; item++) {
165 166 167 168 169
                if (cmp1(p, p1 - p, item->name)) {
                    goto found;
                }
            }
            return 0;
P
Paolo Bonzini 已提交
170 171
        found:
            mask |= item->mask;
172 173 174 175 176 177 178 179
        }
        if (*p1 != ',') {
            break;
        }
        p = p1 + 1;
    }
    return mask;
}
180 181 182

void qemu_print_log_usage(FILE *f)
{
183
    const QEMULogItem *item;
184
    fprintf(f, "Log items (comma separated):\n");
185
    for (item = qemu_log_items; item->mask != 0; item++) {
P
Paolo Bonzini 已提交
186
        fprintf(f, "%-15s %s\n", item->name, item->help);
187
    }
P
Paolo Bonzini 已提交
188 189 190 191
#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
192
}