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