]>
Commit | Line | Data |
---|---|---|
5726c27f BS |
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 | ||
23 | #ifdef WIN32 | |
24 | static const char *logfilename = "qemu.log"; | |
25 | #else | |
26 | static const char *logfilename = "/tmp/qemu.log"; | |
27 | #endif | |
eeacee4d BS |
28 | FILE *qemu_logfile; |
29 | int qemu_loglevel; | |
5726c27f BS |
30 | static int log_append = 0; |
31 | ||
eeacee4d BS |
32 | void qemu_log(const char *fmt, ...) |
33 | { | |
34 | va_list ap; | |
35 | ||
36 | va_start(ap, fmt); | |
37 | if (qemu_logfile) { | |
38 | vfprintf(qemu_logfile, fmt, ap); | |
39 | } | |
40 | va_end(ap); | |
41 | } | |
42 | ||
43 | void qemu_log_mask(int mask, const char *fmt, ...) | |
44 | { | |
45 | va_list ap; | |
46 | ||
47 | va_start(ap, fmt); | |
48 | if ((qemu_loglevel & mask) && qemu_logfile) { | |
49 | vfprintf(qemu_logfile, fmt, ap); | |
50 | } | |
51 | va_end(ap); | |
52 | } | |
53 | ||
5726c27f | 54 | /* enable or disable low levels log */ |
3437e545 | 55 | void qemu_set_log(int log_flags, bool use_own_buffers) |
5726c27f | 56 | { |
eeacee4d BS |
57 | qemu_loglevel = log_flags; |
58 | if (qemu_loglevel && !qemu_logfile) { | |
59 | qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); | |
60 | if (!qemu_logfile) { | |
5726c27f BS |
61 | perror(logfilename); |
62 | _exit(1); | |
63 | } | |
5726c27f | 64 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ |
3437e545 | 65 | if (use_own_buffers) { |
5726c27f | 66 | static char logfile_buf[4096]; |
3437e545 | 67 | |
eeacee4d | 68 | setvbuf(qemu_logfile, logfile_buf, _IOLBF, sizeof(logfile_buf)); |
3437e545 BS |
69 | } else { |
70 | #if defined(_WIN32) | |
71 | /* Win32 doesn't support line-buffering, so use unbuffered output. */ | |
72 | setvbuf(qemu_logfile, NULL, _IONBF, 0); | |
5726c27f | 73 | #else |
3437e545 | 74 | setvbuf(qemu_logfile, NULL, _IOLBF, 0); |
5726c27f | 75 | #endif |
3437e545 BS |
76 | log_append = 1; |
77 | } | |
5726c27f | 78 | } |
eeacee4d BS |
79 | if (!qemu_loglevel && qemu_logfile) { |
80 | fclose(qemu_logfile); | |
81 | qemu_logfile = NULL; | |
5726c27f BS |
82 | } |
83 | } | |
84 | ||
85 | void cpu_set_log_filename(const char *filename) | |
86 | { | |
87 | logfilename = strdup(filename); | |
eeacee4d BS |
88 | if (qemu_logfile) { |
89 | fclose(qemu_logfile); | |
90 | qemu_logfile = NULL; | |
5726c27f | 91 | } |
eeacee4d | 92 | cpu_set_log(qemu_loglevel); |
5726c27f BS |
93 | } |
94 | ||
95 | const CPULogItem cpu_log_items[] = { | |
96 | { CPU_LOG_TB_OUT_ASM, "out_asm", | |
97 | "show generated host assembly code for each compiled TB" }, | |
98 | { CPU_LOG_TB_IN_ASM, "in_asm", | |
99 | "show target assembly code for each compiled TB" }, | |
100 | { CPU_LOG_TB_OP, "op", | |
101 | "show micro ops for each compiled TB" }, | |
102 | { CPU_LOG_TB_OP_OPT, "op_opt", | |
3437e545 | 103 | "show micro ops (x86 only: before eflags optimization) and\n" |
5726c27f BS |
104 | "after liveness analysis" }, |
105 | { CPU_LOG_INT, "int", | |
106 | "show interrupts/exceptions in short format" }, | |
107 | { CPU_LOG_EXEC, "exec", | |
108 | "show trace before each executed TB (lots of logs)" }, | |
109 | { CPU_LOG_TB_CPU, "cpu", | |
110 | "show CPU state before block translation" }, | |
5726c27f | 111 | { CPU_LOG_PCALL, "pcall", |
3437e545 | 112 | "x86 only: show protected mode far calls/returns/exceptions" }, |
5726c27f | 113 | { CPU_LOG_RESET, "cpu_reset", |
3437e545 | 114 | "x86 only: show CPU state before CPU resets" }, |
5726c27f BS |
115 | { CPU_LOG_IOPORT, "ioport", |
116 | "show all i/o ports accesses" }, | |
dafdf1ab BS |
117 | { LOG_UNIMP, "unimp", |
118 | "log unimplemented functionality" }, | |
5726c27f BS |
119 | { 0, NULL, NULL }, |
120 | }; | |
121 | ||
122 | static int cmp1(const char *s1, int n, const char *s2) | |
123 | { | |
124 | if (strlen(s2) != n) { | |
125 | return 0; | |
126 | } | |
127 | return memcmp(s1, s2, n) == 0; | |
128 | } | |
129 | ||
130 | /* takes a comma separated list of log masks. Return 0 if error. */ | |
131 | int cpu_str_to_log_mask(const char *str) | |
132 | { | |
133 | const CPULogItem *item; | |
134 | int mask; | |
135 | const char *p, *p1; | |
136 | ||
137 | p = str; | |
138 | mask = 0; | |
139 | for (;;) { | |
140 | p1 = strchr(p, ','); | |
141 | if (!p1) { | |
142 | p1 = p + strlen(p); | |
143 | } | |
144 | if (cmp1(p,p1-p,"all")) { | |
145 | for (item = cpu_log_items; item->mask != 0; item++) { | |
146 | mask |= item->mask; | |
147 | } | |
148 | } else { | |
149 | for (item = cpu_log_items; item->mask != 0; item++) { | |
150 | if (cmp1(p, p1 - p, item->name)) { | |
151 | goto found; | |
152 | } | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | found: | |
157 | mask |= item->mask; | |
158 | if (*p1 != ',') { | |
159 | break; | |
160 | } | |
161 | p = p1 + 1; | |
162 | } | |
163 | return mask; | |
164 | } |