]>
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" |
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" |
23d15e86 | 29 | |
43b48cfc PB |
30 | int trace_events_enabled_count; |
31 | ||
e9e0bb2a DL |
32 | QemuOptsList qemu_trace_opts = { |
33 | .name = "trace", | |
34 | .implied_opt_name = "enable", | |
35 | .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), | |
36 | .desc = { | |
37 | { | |
38 | .name = "enable", | |
39 | .type = QEMU_OPT_STRING, | |
40 | }, | |
41 | { | |
42 | .name = "events", | |
43 | .type = QEMU_OPT_STRING, | |
44 | },{ | |
45 | .name = "file", | |
46 | .type = QEMU_OPT_STRING, | |
47 | }, | |
48 | { /* end of list */ } | |
49 | }, | |
50 | }; | |
51 | ||
52 | ||
b1bae816 LV |
53 | TraceEvent *trace_event_name(const char *name) |
54 | { | |
55 | assert(name != NULL); | |
56 | ||
0d4e995c DB |
57 | TraceEventIter iter; |
58 | TraceEvent *ev; | |
59 | trace_event_iter_init(&iter, NULL); | |
60 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
b1bae816 LV |
61 | if (strcmp(trace_event_get_name(ev), name) == 0) { |
62 | return ev; | |
63 | } | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | static bool pattern_glob(const char *pat, const char *ev) | |
69 | { | |
70 | while (*pat != '\0' && *ev != '\0') { | |
71 | if (*pat == *ev) { | |
72 | pat++; | |
73 | ev++; | |
74 | } | |
75 | else if (*pat == '*') { | |
76 | if (pattern_glob(pat, ev+1)) { | |
77 | return true; | |
78 | } else if (pattern_glob(pat+1, ev)) { | |
79 | return true; | |
80 | } else { | |
81 | return false; | |
82 | } | |
83 | } else { | |
84 | return false; | |
85 | } | |
86 | } | |
87 | ||
88 | while (*pat == '*') { | |
89 | pat++; | |
90 | } | |
91 | ||
92 | if (*pat == '\0' && *ev == '\0') { | |
93 | return true; | |
94 | } else { | |
95 | return false; | |
96 | } | |
97 | } | |
98 | ||
b1bae816 | 99 | |
6a1b0f3a DB |
100 | void trace_event_iter_init(TraceEventIter *iter, const char *pattern) |
101 | { | |
102 | iter->event = 0; | |
103 | iter->pattern = pattern; | |
104 | } | |
105 | ||
106 | TraceEvent *trace_event_iter_next(TraceEventIter *iter) | |
107 | { | |
ef4c9fc8 | 108 | while (trace_events[iter->event] != NULL) { |
79218be4 | 109 | TraceEvent *ev = trace_events[iter->event]; |
6a1b0f3a DB |
110 | iter->event++; |
111 | if (!iter->pattern || | |
112 | pattern_glob(iter->pattern, | |
113 | trace_event_get_name(ev))) { | |
114 | return ev; | |
115 | } | |
116 | } | |
117 | ||
118 | return NULL; | |
119 | } | |
120 | ||
e9527dd3 PB |
121 | void trace_list_events(void) |
122 | { | |
0d4e995c DB |
123 | TraceEventIter iter; |
124 | TraceEvent *ev; | |
125 | trace_event_iter_init(&iter, NULL); | |
126 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
127 | fprintf(stderr, "%s\n", trace_event_get_name(ev)); | |
e9527dd3 PB |
128 | } |
129 | } | |
130 | ||
131 | static void do_trace_enable_events(const char *line_buf) | |
10578a25 PB |
132 | { |
133 | const bool enable = ('-' != line_buf[0]); | |
134 | const char *line_ptr = enable ? line_buf : line_buf + 1; | |
0d4e995c DB |
135 | TraceEventIter iter; |
136 | TraceEvent *ev; | |
137 | bool is_pattern = trace_event_is_pattern(line_ptr); | |
138 | ||
139 | trace_event_iter_init(&iter, line_ptr); | |
140 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
141 | if (!trace_event_get_state_static(ev)) { | |
142 | if (!is_pattern) { | |
143 | error_report("WARNING: trace event '%s' is not traceable", | |
144 | line_ptr); | |
145 | return; | |
10578a25 | 146 | } |
0d4e995c | 147 | continue; |
10578a25 | 148 | } |
0d4e995c DB |
149 | |
150 | /* start tracing */ | |
151 | trace_event_set_state_dynamic(ev, enable); | |
152 | if (!is_pattern) { | |
153 | return; | |
10578a25 PB |
154 | } |
155 | } | |
0d4e995c DB |
156 | |
157 | if (!is_pattern) { | |
158 | error_report("WARNING: trace event '%s' does not exist", | |
159 | line_ptr); | |
160 | } | |
10578a25 PB |
161 | } |
162 | ||
e9527dd3 PB |
163 | void trace_enable_events(const char *line_buf) |
164 | { | |
165 | if (is_help_option(line_buf)) { | |
166 | trace_list_events(); | |
acc6809d DL |
167 | if (cur_mon == NULL) { |
168 | exit(0); | |
169 | } | |
e9527dd3 PB |
170 | } else { |
171 | do_trace_enable_events(line_buf); | |
172 | } | |
173 | } | |
174 | ||
e9e0bb2a | 175 | static void trace_init_events(const char *fname) |
b1bae816 | 176 | { |
a35d9be6 AK |
177 | Location loc; |
178 | FILE *fp; | |
179 | char line_buf[1024]; | |
180 | size_t line_idx = 0; | |
181 | ||
23d15e86 LV |
182 | if (fname == NULL) { |
183 | return; | |
184 | } | |
185 | ||
a35d9be6 AK |
186 | loc_push_none(&loc); |
187 | loc_set_file(fname, 0); | |
188 | fp = fopen(fname, "r"); | |
23d15e86 | 189 | if (!fp) { |
a35d9be6 | 190 | error_report("%s", strerror(errno)); |
23d15e86 LV |
191 | exit(1); |
192 | } | |
23d15e86 | 193 | while (fgets(line_buf, sizeof(line_buf), fp)) { |
a35d9be6 | 194 | loc_set_file(fname, ++line_idx); |
23d15e86 LV |
195 | size_t len = strlen(line_buf); |
196 | if (len > 1) { /* skip empty lines */ | |
197 | line_buf[len - 1] = '\0'; | |
794b1f96 AK |
198 | if ('#' == line_buf[0]) { /* skip commented lines */ |
199 | continue; | |
200 | } | |
10578a25 | 201 | trace_enable_events(line_buf); |
23d15e86 LV |
202 | } |
203 | } | |
204 | if (fclose(fp) != 0) { | |
a35d9be6 AK |
205 | loc_set_file(fname, 0); |
206 | error_report("%s", strerror(errno)); | |
23d15e86 LV |
207 | exit(1); |
208 | } | |
a35d9be6 | 209 | loc_pop(&loc); |
23d15e86 | 210 | } |
5b808275 | 211 | |
41fc57e4 | 212 | void trace_init_file(const char *file) |
5b808275 LV |
213 | { |
214 | #ifdef CONFIG_TRACE_SIMPLE | |
41fc57e4 | 215 | st_set_trace_file(file); |
ed7f5f1d PB |
216 | #elif defined CONFIG_TRACE_LOG |
217 | /* If both the simple and the log backends are enabled, "-trace file" | |
218 | * only applies to the simple backend; use "-D" for the log backend. | |
219 | */ | |
220 | if (file) { | |
daa76aa4 | 221 | qemu_set_log_filename(file, &error_fatal); |
ed7f5f1d | 222 | } |
5b808275 LV |
223 | #else |
224 | if (file) { | |
225 | fprintf(stderr, "error: -trace file=...: " | |
226 | "option not supported by the selected tracing backends\n"); | |
41fc57e4 PB |
227 | exit(1); |
228 | } | |
229 | #endif | |
230 | } | |
231 | ||
232 | bool trace_init_backends(void) | |
233 | { | |
234 | #ifdef CONFIG_TRACE_SIMPLE | |
235 | if (!st_init()) { | |
236 | fprintf(stderr, "failed to initialize simple tracing backend.\n"); | |
5b808275 LV |
237 | return false; |
238 | } | |
239 | #endif | |
240 | ||
241 | #ifdef CONFIG_TRACE_FTRACE | |
242 | if (!ftrace_init()) { | |
243 | fprintf(stderr, "failed to initialize ftrace backend.\n"); | |
244 | return false; | |
245 | } | |
246 | #endif | |
247 | ||
0a852417 PD |
248 | #ifdef CONFIG_TRACE_SYSLOG |
249 | openlog(NULL, LOG_PID, LOG_DAEMON); | |
250 | #endif | |
251 | ||
5b808275 LV |
252 | return true; |
253 | } | |
e9e0bb2a DL |
254 | |
255 | char *trace_opt_parse(const char *optarg) | |
256 | { | |
257 | char *trace_file; | |
258 | QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"), | |
259 | optarg, true); | |
260 | if (!opts) { | |
261 | exit(1); | |
262 | } | |
263 | if (qemu_opt_get(opts, "enable")) { | |
264 | trace_enable_events(qemu_opt_get(opts, "enable")); | |
265 | } | |
266 | trace_init_events(qemu_opt_get(opts, "events")); | |
267 | trace_file = g_strdup(qemu_opt_get(opts, "file")); | |
268 | qemu_opts_del(opts); | |
269 | ||
270 | return trace_file; | |
271 | } |