qemu-log.c 4.82 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"
22

23
static char *logfilename;
Blue Swirl's avatar
Blue Swirl committed
24 25
FILE *qemu_logfile;
int qemu_loglevel;
26 27
static int log_append = 0;

Blue Swirl's avatar
Blue Swirl committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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);
}

50
/* enable or disable low levels log */
51
void do_qemu_set_log(int log_flags, bool use_own_buffers)
52
{
Blue Swirl's avatar
Blue Swirl committed
53 54
    qemu_loglevel = log_flags;
    if (qemu_loglevel && !qemu_logfile) {
55 56 57 58 59 60 61 62 63
        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;
64 65
        }
        /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
66
        if (use_own_buffers) {
67
            static char logfile_buf[4096];
68

Blue Swirl's avatar
Blue Swirl committed
69
            setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
70 71 72 73
        } else {
#if defined(_WIN32)
            /* Win32 doesn't support line-buffering, so use unbuffered output. */
            setvbuf(qemu_logfile, NULL, _IONBF, 0);
74
#else
75
            setvbuf(qemu_logfile, NULL, _IOLBF, 0);
76
#endif
77 78
            log_append = 1;
        }
79
    }
Blue Swirl's avatar
Blue Swirl committed
80
    if (!qemu_loglevel && qemu_logfile) {
81
        qemu_log_close();
82 83 84
    }
}

85
void qemu_set_log_filename(const char *filename)
86
{
87
    g_free(logfilename);
88
    logfilename = g_strdup(filename);
89
    qemu_log_close();
90
    qemu_set_log(qemu_loglevel);
91 92
}

93
const QEMULogItem qemu_log_items[] = {
94 95 96 97 98 99 100
    { 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",
101
      "show micro ops (x86 only: before eflags optimization) and\n"
102 103 104 105 106 107 108 109
      "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" },
    { CPU_LOG_PCALL, "pcall",
110
      "x86 only: show protected mode far calls/returns/exceptions" },
111
    { CPU_LOG_RESET, "cpu_reset",
112
      "x86 only: show CPU state before CPU resets" },
113 114
    { CPU_LOG_IOPORT, "ioport",
      "show all i/o ports accesses" },
115 116
    { LOG_UNIMP, "unimp",
      "log unimplemented functionality" },
117 118 119
    { LOG_GUEST_ERROR, "guest_errors",
      "log when the guest OS does something invalid (eg accessing a\n"
      "non-existent register)" },
120 121 122 123 124 125 126 127 128 129 130 131
    { 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. */
132
int qemu_str_to_log_mask(const char *str)
133
{
134
    const QEMULogItem *item;
135 136 137 138 139 140 141 142 143 144 145
    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")) {
146
            for (item = qemu_log_items; item->mask != 0; item++) {
147 148 149
                mask |= item->mask;
            }
        } else {
150
            for (item = qemu_log_items; item->mask != 0; item++) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
                if (cmp1(p, p1 - p, item->name)) {
                    goto found;
                }
            }
            return 0;
        }
    found:
        mask |= item->mask;
        if (*p1 != ',') {
            break;
        }
        p = p1 + 1;
    }
    return mask;
}
166 167 168

void qemu_print_log_usage(FILE *f)
{
169
    const QEMULogItem *item;
170
    fprintf(f, "Log items (comma separated):\n");
171
    for (item = qemu_log_items; item->mask != 0; item++) {
172 173 174
        fprintf(f, "%-10s %s\n", item->name, item->help);
    }
}