]>
Commit | Line | Data |
---|---|---|
23d15e86 LV |
1 | /* |
2 | * Interface for configuring and controlling the state of tracing events. | |
3 | * | |
48151859 | 4 | * Copyright (C) 2011-2016 Lluís Vilanova <[email protected]> |
23d15e86 | 5 | * |
b1bae816 LV |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
7 | * See the COPYING file in the top-level directory. | |
23d15e86 LV |
8 | */ |
9 | ||
d38ea87a | 10 | #include "qemu/osdep.h" |
23d15e86 | 11 | #include "trace/control.h" |
f348b6d1 | 12 | #include "qemu/help_option.h" |
5b808275 LV |
13 | #ifdef CONFIG_TRACE_SIMPLE |
14 | #include "trace/simple.h" | |
15 | #endif | |
16 | #ifdef CONFIG_TRACE_FTRACE | |
17 | #include "trace/ftrace.h" | |
18 | #endif | |
ed7f5f1d PB |
19 | #ifdef CONFIG_TRACE_LOG |
20 | #include "qemu/log.h" | |
21 | #endif | |
0a852417 PD |
22 | #ifdef CONFIG_TRACE_SYSLOG |
23 | #include <syslog.h> | |
24 | #endif | |
daa76aa4 | 25 | #include "qapi/error.h" |
a35d9be6 | 26 | #include "qemu/error-report.h" |
e9e0bb2a | 27 | #include "qemu/config-file.h" |
acc6809d | 28 | #include "monitor/monitor.h" |
0ab8ed18 | 29 | #include "trace-root.h" |
23d15e86 | 30 | |
43b48cfc PB |
31 | int trace_events_enabled_count; |
32 | ||
fe4db84d DB |
33 | typedef struct TraceEventGroup { |
34 | TraceEvent **events; | |
35 | } TraceEventGroup; | |
36 | ||
37 | static TraceEventGroup *event_groups; | |
38 | static size_t nevent_groups; | |
ca3fa0e8 DB |
39 | static uint32_t next_id; |
40 | static uint32_t next_vcpu_id; | |
fe4db84d | 41 | |
e9e0bb2a DL |
42 | QemuOptsList qemu_trace_opts = { |
43 | .name = "trace", | |
44 | .implied_opt_name = "enable", | |
45 | .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), | |
46 | .desc = { | |
47 | { | |
48 | .name = "enable", | |
49 | .type = QEMU_OPT_STRING, | |
50 | }, | |
51 | { | |
52 | .name = "events", | |
53 | .type = QEMU_OPT_STRING, | |
54 | },{ | |
55 | .name = "file", | |
56 | .type = QEMU_OPT_STRING, | |
57 | }, | |
58 | { /* end of list */ } | |
59 | }, | |
60 | }; | |
61 | ||
62 | ||
fe4db84d DB |
63 | void trace_event_register_group(TraceEvent **events) |
64 | { | |
ca3fa0e8 DB |
65 | size_t i; |
66 | for (i = 0; events[i] != NULL; i++) { | |
67 | events[i]->id = next_id++; | |
68 | if (events[i]->vcpu_id != TRACE_VCPU_EVENT_NONE) { | |
69 | events[i]->vcpu_id = next_vcpu_id++; | |
70 | } | |
71 | } | |
fe4db84d DB |
72 | event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1); |
73 | event_groups[nevent_groups].events = events; | |
74 | nevent_groups++; | |
75 | } | |
76 | ||
77 | ||
b1bae816 LV |
78 | TraceEvent *trace_event_name(const char *name) |
79 | { | |
80 | assert(name != NULL); | |
81 | ||
0d4e995c DB |
82 | TraceEventIter iter; |
83 | TraceEvent *ev; | |
84 | trace_event_iter_init(&iter, NULL); | |
85 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
b1bae816 LV |
86 | if (strcmp(trace_event_get_name(ev), name) == 0) { |
87 | return ev; | |
88 | } | |
89 | } | |
90 | return NULL; | |
91 | } | |
92 | ||
93 | static bool pattern_glob(const char *pat, const char *ev) | |
94 | { | |
95 | while (*pat != '\0' && *ev != '\0') { | |
96 | if (*pat == *ev) { | |
97 | pat++; | |
98 | ev++; | |
99 | } | |
100 | else if (*pat == '*') { | |
101 | if (pattern_glob(pat, ev+1)) { | |
102 | return true; | |
103 | } else if (pattern_glob(pat+1, ev)) { | |
104 | return true; | |
105 | } else { | |
106 | return false; | |
107 | } | |
108 | } else { | |
109 | return false; | |
110 | } | |
111 | } | |
112 | ||
113 | while (*pat == '*') { | |
114 | pat++; | |
115 | } | |
116 | ||
117 | if (*pat == '\0' && *ev == '\0') { | |
118 | return true; | |
119 | } else { | |
120 | return false; | |
121 | } | |
122 | } | |
123 | ||
b1bae816 | 124 | |
6a1b0f3a DB |
125 | void trace_event_iter_init(TraceEventIter *iter, const char *pattern) |
126 | { | |
127 | iter->event = 0; | |
fe4db84d | 128 | iter->group = 0; |
6a1b0f3a DB |
129 | iter->pattern = pattern; |
130 | } | |
131 | ||
132 | TraceEvent *trace_event_iter_next(TraceEventIter *iter) | |
133 | { | |
fe4db84d DB |
134 | while (iter->group < nevent_groups && |
135 | event_groups[iter->group].events[iter->event] != NULL) { | |
136 | TraceEvent *ev = event_groups[iter->group].events[iter->event]; | |
6a1b0f3a | 137 | iter->event++; |
fe4db84d DB |
138 | if (event_groups[iter->group].events[iter->event] == NULL) { |
139 | iter->event = 0; | |
140 | iter->group++; | |
141 | } | |
6a1b0f3a DB |
142 | if (!iter->pattern || |
143 | pattern_glob(iter->pattern, | |
144 | trace_event_get_name(ev))) { | |
145 | return ev; | |
146 | } | |
147 | } | |
148 | ||
149 | return NULL; | |
150 | } | |
151 | ||
e9527dd3 PB |
152 | void trace_list_events(void) |
153 | { | |
0d4e995c DB |
154 | TraceEventIter iter; |
155 | TraceEvent *ev; | |
156 | trace_event_iter_init(&iter, NULL); | |
157 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
158 | fprintf(stderr, "%s\n", trace_event_get_name(ev)); | |
e9527dd3 PB |
159 | } |
160 | } | |
161 | ||
162 | static void do_trace_enable_events(const char *line_buf) | |
10578a25 PB |
163 | { |
164 | const bool enable = ('-' != line_buf[0]); | |
165 | const char *line_ptr = enable ? line_buf : line_buf + 1; | |
0d4e995c DB |
166 | TraceEventIter iter; |
167 | TraceEvent *ev; | |
168 | bool is_pattern = trace_event_is_pattern(line_ptr); | |
169 | ||
170 | trace_event_iter_init(&iter, line_ptr); | |
171 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
172 | if (!trace_event_get_state_static(ev)) { | |
173 | if (!is_pattern) { | |
174 | error_report("WARNING: trace event '%s' is not traceable", | |
175 | line_ptr); | |
176 | return; | |
10578a25 | 177 | } |
0d4e995c | 178 | continue; |
10578a25 | 179 | } |
0d4e995c DB |
180 | |
181 | /* start tracing */ | |
182 | trace_event_set_state_dynamic(ev, enable); | |
183 | if (!is_pattern) { | |
184 | return; | |
10578a25 PB |
185 | } |
186 | } | |
0d4e995c DB |
187 | |
188 | if (!is_pattern) { | |
189 | error_report("WARNING: trace event '%s' does not exist", | |
190 | line_ptr); | |
191 | } | |
10578a25 PB |
192 | } |
193 | ||
e9527dd3 PB |
194 | void trace_enable_events(const char *line_buf) |
195 | { | |
196 | if (is_help_option(line_buf)) { | |
197 | trace_list_events(); | |
acc6809d DL |
198 | if (cur_mon == NULL) { |
199 | exit(0); | |
200 | } | |
e9527dd3 PB |
201 | } else { |
202 | do_trace_enable_events(line_buf); | |
203 | } | |
204 | } | |
205 | ||
e9e0bb2a | 206 | static void trace_init_events(const char *fname) |
b1bae816 | 207 | { |
a35d9be6 AK |
208 | Location loc; |
209 | FILE *fp; | |
210 | char line_buf[1024]; | |
211 | size_t line_idx = 0; | |
212 | ||
23d15e86 LV |
213 | if (fname == NULL) { |
214 | return; | |
215 | } | |
216 | ||
a35d9be6 AK |
217 | loc_push_none(&loc); |
218 | loc_set_file(fname, 0); | |
219 | fp = fopen(fname, "r"); | |
23d15e86 | 220 | if (!fp) { |
a35d9be6 | 221 | error_report("%s", strerror(errno)); |
23d15e86 LV |
222 | exit(1); |
223 | } | |
23d15e86 | 224 | while (fgets(line_buf, sizeof(line_buf), fp)) { |
a35d9be6 | 225 | loc_set_file(fname, ++line_idx); |
23d15e86 LV |
226 | size_t len = strlen(line_buf); |
227 | if (len > 1) { /* skip empty lines */ | |
228 | line_buf[len - 1] = '\0'; | |
794b1f96 AK |
229 | if ('#' == line_buf[0]) { /* skip commented lines */ |
230 | continue; | |
231 | } | |
10578a25 | 232 | trace_enable_events(line_buf); |
23d15e86 LV |
233 | } |
234 | } | |
235 | if (fclose(fp) != 0) { | |
a35d9be6 AK |
236 | loc_set_file(fname, 0); |
237 | error_report("%s", strerror(errno)); | |
23d15e86 LV |
238 | exit(1); |
239 | } | |
a35d9be6 | 240 | loc_pop(&loc); |
23d15e86 | 241 | } |
5b808275 | 242 | |
41fc57e4 | 243 | void trace_init_file(const char *file) |
5b808275 LV |
244 | { |
245 | #ifdef CONFIG_TRACE_SIMPLE | |
41fc57e4 | 246 | st_set_trace_file(file); |
ed7f5f1d PB |
247 | #elif defined CONFIG_TRACE_LOG |
248 | /* If both the simple and the log backends are enabled, "-trace file" | |
249 | * only applies to the simple backend; use "-D" for the log backend. | |
250 | */ | |
251 | if (file) { | |
daa76aa4 | 252 | qemu_set_log_filename(file, &error_fatal); |
ed7f5f1d | 253 | } |
5b808275 LV |
254 | #else |
255 | if (file) { | |
256 | fprintf(stderr, "error: -trace file=...: " | |
257 | "option not supported by the selected tracing backends\n"); | |
41fc57e4 PB |
258 | exit(1); |
259 | } | |
260 | #endif | |
261 | } | |
262 | ||
82e95ec8 LV |
263 | void trace_fini_vcpu(CPUState *vcpu) |
264 | { | |
265 | TraceEventIter iter; | |
266 | TraceEvent *ev; | |
267 | ||
a47e8715 LV |
268 | trace_guest_cpu_exit(vcpu); |
269 | ||
82e95ec8 LV |
270 | trace_event_iter_init(&iter, NULL); |
271 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
272 | if (trace_event_is_vcpu(ev) && | |
273 | trace_event_get_state_static(ev) && | |
274 | trace_event_get_vcpu_state_dynamic(vcpu, ev)) { | |
275 | /* must disable to affect the global counter */ | |
276 | trace_event_set_vcpu_state_dynamic(vcpu, ev, false); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
41fc57e4 PB |
281 | bool trace_init_backends(void) |
282 | { | |
283 | #ifdef CONFIG_TRACE_SIMPLE | |
284 | if (!st_init()) { | |
285 | fprintf(stderr, "failed to initialize simple tracing backend.\n"); | |
5b808275 LV |
286 | return false; |
287 | } | |
288 | #endif | |
289 | ||
290 | #ifdef CONFIG_TRACE_FTRACE | |
291 | if (!ftrace_init()) { | |
292 | fprintf(stderr, "failed to initialize ftrace backend.\n"); | |
293 | return false; | |
294 | } | |
295 | #endif | |
296 | ||
0a852417 PD |
297 | #ifdef CONFIG_TRACE_SYSLOG |
298 | openlog(NULL, LOG_PID, LOG_DAEMON); | |
299 | #endif | |
300 | ||
5b808275 LV |
301 | return true; |
302 | } | |
e9e0bb2a DL |
303 | |
304 | char *trace_opt_parse(const char *optarg) | |
305 | { | |
306 | char *trace_file; | |
307 | QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), | |
308 | optarg, true); | |
309 | if (!opts) { | |
310 | exit(1); | |
311 | } | |
312 | if (qemu_opt_get(opts, "enable")) { | |
313 | trace_enable_events(qemu_opt_get(opts, "enable")); | |
314 | } | |
315 | trace_init_events(qemu_opt_get(opts, "events")); | |
316 | trace_file = g_strdup(qemu_opt_get(opts, "file")); | |
317 | qemu_opts_del(opts); | |
318 | ||
319 | return trace_file; | |
320 | } | |
b7d48952 DB |
321 | |
322 | uint32_t trace_get_vcpu_event_count(void) | |
323 | { | |
ca3fa0e8 | 324 | return next_vcpu_id; |
b7d48952 | 325 | } |