]>
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; | |
94 | trace_event_iter_init(&iter, NULL); | |
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 | ||
6a1b0f3a DB |
103 | void trace_event_iter_init(TraceEventIter *iter, const char *pattern) |
104 | { | |
105 | iter->event = 0; | |
fe4db84d | 106 | iter->group = 0; |
6a1b0f3a DB |
107 | iter->pattern = pattern; |
108 | } | |
109 | ||
110 | TraceEvent *trace_event_iter_next(TraceEventIter *iter) | |
111 | { | |
fe4db84d DB |
112 | while (iter->group < nevent_groups && |
113 | event_groups[iter->group].events[iter->event] != NULL) { | |
114 | TraceEvent *ev = event_groups[iter->group].events[iter->event]; | |
6a1b0f3a | 115 | iter->event++; |
fe4db84d DB |
116 | if (event_groups[iter->group].events[iter->event] == NULL) { |
117 | iter->event = 0; | |
118 | iter->group++; | |
119 | } | |
6a1b0f3a | 120 | if (!iter->pattern || |
e66eae7a | 121 | g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) { |
6a1b0f3a DB |
122 | return ev; |
123 | } | |
124 | } | |
125 | ||
126 | return NULL; | |
127 | } | |
128 | ||
6745c8a0 | 129 | void trace_list_events(FILE *f) |
e9527dd3 | 130 | { |
0d4e995c DB |
131 | TraceEventIter iter; |
132 | TraceEvent *ev; | |
133 | trace_event_iter_init(&iter, NULL); | |
134 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
6745c8a0 | 135 | fprintf(f, "%s\n", trace_event_get_name(ev)); |
e9527dd3 | 136 | } |
9f591a5d | 137 | #ifdef CONFIG_TRACE_DTRACE |
6745c8a0 DE |
138 | fprintf(f, "This list of names of trace points may be incomplete " |
139 | "when using the DTrace/SystemTap backends.\n" | |
140 | "Run 'qemu-trace-stap list %s' to print the full list.\n", | |
9f591a5d PMD |
141 | error_get_progname()); |
142 | #endif | |
e9527dd3 PB |
143 | } |
144 | ||
145 | static void do_trace_enable_events(const char *line_buf) | |
10578a25 PB |
146 | { |
147 | const bool enable = ('-' != line_buf[0]); | |
148 | const char *line_ptr = enable ? line_buf : line_buf + 1; | |
0d4e995c DB |
149 | TraceEventIter iter; |
150 | TraceEvent *ev; | |
151 | bool is_pattern = trace_event_is_pattern(line_ptr); | |
152 | ||
153 | trace_event_iter_init(&iter, line_ptr); | |
154 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
155 | if (!trace_event_get_state_static(ev)) { | |
156 | if (!is_pattern) { | |
3dc6f869 AF |
157 | warn_report("trace event '%s' is not traceable", |
158 | line_ptr); | |
0d4e995c | 159 | return; |
10578a25 | 160 | } |
0d4e995c | 161 | continue; |
10578a25 | 162 | } |
0d4e995c DB |
163 | |
164 | /* start tracing */ | |
165 | trace_event_set_state_dynamic(ev, enable); | |
166 | if (!is_pattern) { | |
167 | return; | |
10578a25 PB |
168 | } |
169 | } | |
0d4e995c DB |
170 | |
171 | if (!is_pattern) { | |
3dc6f869 AF |
172 | warn_report("trace event '%s' does not exist", |
173 | line_ptr); | |
0d4e995c | 174 | } |
10578a25 PB |
175 | } |
176 | ||
e9527dd3 PB |
177 | void trace_enable_events(const char *line_buf) |
178 | { | |
179 | if (is_help_option(line_buf)) { | |
6745c8a0 | 180 | trace_list_events(stdout); |
947e4744 | 181 | if (monitor_cur() == NULL) { |
acc6809d DL |
182 | exit(0); |
183 | } | |
e9527dd3 PB |
184 | } else { |
185 | do_trace_enable_events(line_buf); | |
186 | } | |
187 | } | |
188 | ||
e9e0bb2a | 189 | static void trace_init_events(const char *fname) |
b1bae816 | 190 | { |
a35d9be6 AK |
191 | Location loc; |
192 | FILE *fp; | |
193 | char line_buf[1024]; | |
194 | size_t line_idx = 0; | |
195 | ||
23d15e86 LV |
196 | if (fname == NULL) { |
197 | return; | |
198 | } | |
199 | ||
a35d9be6 AK |
200 | loc_push_none(&loc); |
201 | loc_set_file(fname, 0); | |
202 | fp = fopen(fname, "r"); | |
23d15e86 | 203 | if (!fp) { |
a35d9be6 | 204 | error_report("%s", strerror(errno)); |
23d15e86 LV |
205 | exit(1); |
206 | } | |
23d15e86 | 207 | while (fgets(line_buf, sizeof(line_buf), fp)) { |
a35d9be6 | 208 | loc_set_file(fname, ++line_idx); |
23d15e86 LV |
209 | size_t len = strlen(line_buf); |
210 | if (len > 1) { /* skip empty lines */ | |
211 | line_buf[len - 1] = '\0'; | |
794b1f96 AK |
212 | if ('#' == line_buf[0]) { /* skip commented lines */ |
213 | continue; | |
214 | } | |
10578a25 | 215 | trace_enable_events(line_buf); |
23d15e86 LV |
216 | } |
217 | } | |
218 | if (fclose(fp) != 0) { | |
a35d9be6 AK |
219 | loc_set_file(fname, 0); |
220 | error_report("%s", strerror(errno)); | |
23d15e86 LV |
221 | exit(1); |
222 | } | |
a35d9be6 | 223 | loc_pop(&loc); |
23d15e86 | 224 | } |
5b808275 | 225 | |
92eecfff | 226 | void trace_init_file(void) |
5b808275 LV |
227 | { |
228 | #ifdef CONFIG_TRACE_SIMPLE | |
9f45a641 | 229 | st_set_trace_file(trace_opts_file); |
648b4823 JD |
230 | if (init_trace_on_startup) { |
231 | st_set_trace_file_enabled(true); | |
232 | } | |
ed7f5f1d | 233 | #elif defined CONFIG_TRACE_LOG |
3d88754e AB |
234 | /* |
235 | * If both the simple and the log backends are enabled, "--trace file" | |
236 | * only applies to the simple backend; use "-D" for the log | |
237 | * backend. However we should only override -D if we actually have | |
238 | * something to override it with. | |
ed7f5f1d | 239 | */ |
9f45a641 PB |
240 | if (trace_opts_file) { |
241 | qemu_set_log_filename(trace_opts_file, &error_fatal); | |
3d88754e | 242 | } |
5b808275 | 243 | #else |
9f45a641 | 244 | if (trace_opts_file) { |
db817b8c | 245 | fprintf(stderr, "error: --trace file=...: " |
5b808275 | 246 | "option not supported by the selected tracing backends\n"); |
41fc57e4 PB |
247 | exit(1); |
248 | } | |
249 | #endif | |
250 | } | |
251 | ||
82e95ec8 LV |
252 | void trace_fini_vcpu(CPUState *vcpu) |
253 | { | |
254 | TraceEventIter iter; | |
255 | TraceEvent *ev; | |
256 | ||
a47e8715 LV |
257 | trace_guest_cpu_exit(vcpu); |
258 | ||
82e95ec8 LV |
259 | trace_event_iter_init(&iter, NULL); |
260 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
261 | if (trace_event_is_vcpu(ev) && | |
262 | trace_event_get_state_static(ev) && | |
263 | trace_event_get_vcpu_state_dynamic(vcpu, ev)) { | |
264 | /* must disable to affect the global counter */ | |
265 | trace_event_set_vcpu_state_dynamic(vcpu, ev, false); | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
41fc57e4 PB |
270 | bool trace_init_backends(void) |
271 | { | |
272 | #ifdef CONFIG_TRACE_SIMPLE | |
273 | if (!st_init()) { | |
274 | fprintf(stderr, "failed to initialize simple tracing backend.\n"); | |
5b808275 LV |
275 | return false; |
276 | } | |
277 | #endif | |
278 | ||
279 | #ifdef CONFIG_TRACE_FTRACE | |
280 | if (!ftrace_init()) { | |
281 | fprintf(stderr, "failed to initialize ftrace backend.\n"); | |
282 | return false; | |
283 | } | |
284 | #endif | |
285 | ||
0a852417 PD |
286 | #ifdef CONFIG_TRACE_SYSLOG |
287 | openlog(NULL, LOG_PID, LOG_DAEMON); | |
288 | #endif | |
289 | ||
5b808275 LV |
290 | return true; |
291 | } | |
e9e0bb2a | 292 | |
92eecfff | 293 | void trace_opt_parse(const char *optarg) |
e9e0bb2a | 294 | { |
e9e0bb2a DL |
295 | QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), |
296 | optarg, true); | |
297 | if (!opts) { | |
298 | exit(1); | |
299 | } | |
300 | if (qemu_opt_get(opts, "enable")) { | |
301 | trace_enable_events(qemu_opt_get(opts, "enable")); | |
302 | } | |
303 | trace_init_events(qemu_opt_get(opts, "events")); | |
648b4823 | 304 | init_trace_on_startup = true; |
9f45a641 PB |
305 | g_free(trace_opts_file); |
306 | trace_opts_file = g_strdup(qemu_opt_get(opts, "file")); | |
e9e0bb2a | 307 | qemu_opts_del(opts); |
e9e0bb2a | 308 | } |
b7d48952 DB |
309 | |
310 | uint32_t trace_get_vcpu_event_count(void) | |
311 | { | |
ca3fa0e8 | 312 | return next_vcpu_id; |
b7d48952 | 313 | } |