xref: /openbmc/qemu/trace/control.c (revision a68694cd)
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 
43 QemuOptsList qemu_trace_opts = {
44     .name = "trace",
45     .implied_opt_name = "enable",
46     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
47     .desc = {
48         {
49             .name = "enable",
50             .type = QEMU_OPT_STRING,
51         },
52         {
53             .name = "events",
54             .type = QEMU_OPT_STRING,
55         },{
56             .name = "file",
57             .type = QEMU_OPT_STRING,
58         },
59         { /* end of list */ }
60     },
61 };
62 
63 
64 void trace_event_register_group(TraceEvent **events)
65 {
66     size_t i;
67     for (i = 0; events[i] != NULL; i++) {
68         events[i]->id = next_id++;
69         if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
70             continue;
71         }
72 
73         if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
74             events[i]->vcpu_id = next_vcpu_id++;
75         } else {
76             warn_report("too many vcpu trace events; dropping '%s'",
77                         events[i]->name);
78         }
79     }
80     event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
81     event_groups[nevent_groups].events = events;
82     nevent_groups++;
83 }
84 
85 
86 TraceEvent *trace_event_name(const char *name)
87 {
88     assert(name != NULL);
89 
90     TraceEventIter iter;
91     TraceEvent *ev;
92     trace_event_iter_init(&iter, NULL);
93     while ((ev = trace_event_iter_next(&iter)) != NULL) {
94         if (strcmp(trace_event_get_name(ev), name) == 0) {
95             return ev;
96         }
97     }
98     return NULL;
99 }
100 
101 void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
102 {
103     iter->event = 0;
104     iter->group = 0;
105     iter->pattern = pattern;
106 }
107 
108 TraceEvent *trace_event_iter_next(TraceEventIter *iter)
109 {
110     while (iter->group < nevent_groups &&
111            event_groups[iter->group].events[iter->event] != NULL) {
112         TraceEvent *ev = event_groups[iter->group].events[iter->event];
113         iter->event++;
114         if (event_groups[iter->group].events[iter->event] == NULL) {
115             iter->event = 0;
116             iter->group++;
117         }
118         if (!iter->pattern ||
119             g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
120             return ev;
121         }
122     }
123 
124     return NULL;
125 }
126 
127 void trace_list_events(void)
128 {
129     TraceEventIter iter;
130     TraceEvent *ev;
131     trace_event_iter_init(&iter, NULL);
132     while ((ev = trace_event_iter_next(&iter)) != NULL) {
133         fprintf(stderr, "%s\n", trace_event_get_name(ev));
134     }
135 #ifdef CONFIG_TRACE_DTRACE
136     fprintf(stderr, "This list of names of trace points may be incomplete "
137                     "when using the DTrace/SystemTap backends.\n"
138                     "Run 'qemu-trace-stap list %s' to print the full list.\n",
139             error_get_progname());
140 #endif
141 }
142 
143 static void do_trace_enable_events(const char *line_buf)
144 {
145     const bool enable = ('-' != line_buf[0]);
146     const char *line_ptr = enable ? line_buf : line_buf + 1;
147     TraceEventIter iter;
148     TraceEvent *ev;
149     bool is_pattern = trace_event_is_pattern(line_ptr);
150 
151     trace_event_iter_init(&iter, line_ptr);
152     while ((ev = trace_event_iter_next(&iter)) != NULL) {
153         if (!trace_event_get_state_static(ev)) {
154             if (!is_pattern) {
155                 warn_report("trace event '%s' is not traceable",
156                             line_ptr);
157                 return;
158             }
159             continue;
160         }
161 
162         /* start tracing */
163         trace_event_set_state_dynamic(ev, enable);
164         if (!is_pattern) {
165             return;
166         }
167     }
168 
169     if (!is_pattern) {
170         warn_report("trace event '%s' does not exist",
171                     line_ptr);
172     }
173 }
174 
175 void trace_enable_events(const char *line_buf)
176 {
177     if (is_help_option(line_buf)) {
178         trace_list_events();
179         if (cur_mon == NULL) {
180             exit(0);
181         }
182     } else {
183         do_trace_enable_events(line_buf);
184     }
185 }
186 
187 static void trace_init_events(const char *fname)
188 {
189     Location loc;
190     FILE *fp;
191     char line_buf[1024];
192     size_t line_idx = 0;
193 
194     if (fname == NULL) {
195         return;
196     }
197 
198     loc_push_none(&loc);
199     loc_set_file(fname, 0);
200     fp = fopen(fname, "r");
201     if (!fp) {
202         error_report("%s", strerror(errno));
203         exit(1);
204     }
205     while (fgets(line_buf, sizeof(line_buf), fp)) {
206         loc_set_file(fname, ++line_idx);
207         size_t len = strlen(line_buf);
208         if (len > 1) {              /* skip empty lines */
209             line_buf[len - 1] = '\0';
210             if ('#' == line_buf[0]) { /* skip commented lines */
211                 continue;
212             }
213             trace_enable_events(line_buf);
214         }
215     }
216     if (fclose(fp) != 0) {
217         loc_set_file(fname, 0);
218         error_report("%s", strerror(errno));
219         exit(1);
220     }
221     loc_pop(&loc);
222 }
223 
224 void trace_init_file(const char *file)
225 {
226 #ifdef CONFIG_TRACE_SIMPLE
227     st_set_trace_file(file);
228     st_set_trace_file_enabled(true);
229 #elif defined CONFIG_TRACE_LOG
230     /*
231      * If both the simple and the log backends are enabled, "--trace file"
232      * only applies to the simple backend; use "-D" for the log
233      * backend. However we should only override -D if we actually have
234      * something to override it with.
235      */
236     if (file) {
237         qemu_set_log_filename(file, &error_fatal);
238     }
239 #else
240     if (file) {
241         fprintf(stderr, "error: --trace file=...: "
242                 "option not supported by the selected tracing backends\n");
243         exit(1);
244     }
245 #endif
246 }
247 
248 void trace_fini_vcpu(CPUState *vcpu)
249 {
250     TraceEventIter iter;
251     TraceEvent *ev;
252 
253     trace_guest_cpu_exit(vcpu);
254 
255     trace_event_iter_init(&iter, NULL);
256     while ((ev = trace_event_iter_next(&iter)) != NULL) {
257         if (trace_event_is_vcpu(ev) &&
258             trace_event_get_state_static(ev) &&
259             trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
260             /* must disable to affect the global counter */
261             trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
262         }
263     }
264 }
265 
266 bool trace_init_backends(void)
267 {
268 #ifdef CONFIG_TRACE_SIMPLE
269     if (!st_init()) {
270         fprintf(stderr, "failed to initialize simple tracing backend.\n");
271         return false;
272     }
273 #endif
274 
275 #ifdef CONFIG_TRACE_FTRACE
276     if (!ftrace_init()) {
277         fprintf(stderr, "failed to initialize ftrace backend.\n");
278         return false;
279     }
280 #endif
281 
282 #ifdef CONFIG_TRACE_SYSLOG
283     openlog(NULL, LOG_PID, LOG_DAEMON);
284 #endif
285 
286     return true;
287 }
288 
289 char *trace_opt_parse(const char *optarg)
290 {
291     char *trace_file;
292     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
293                                              optarg, true);
294     if (!opts) {
295         exit(1);
296     }
297     if (qemu_opt_get(opts, "enable")) {
298         trace_enable_events(qemu_opt_get(opts, "enable"));
299     }
300     trace_init_events(qemu_opt_get(opts, "events"));
301     trace_file = g_strdup(qemu_opt_get(opts, "file"));
302     qemu_opts_del(opts);
303 
304     return trace_file;
305 }
306 
307 uint32_t trace_get_vcpu_event_count(void)
308 {
309     return next_vcpu_id;
310 }
311