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