xref: /openbmc/qemu/trace/control.c (revision 0a852417)
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 #ifdef CONFIG_TRACE_SIMPLE
14 #include "trace/simple.h"
15 #endif
16 #ifdef CONFIG_TRACE_FTRACE
17 #include "trace/ftrace.h"
18 #endif
19 #ifdef CONFIG_TRACE_LOG
20 #include "qemu/log.h"
21 #endif
22 #ifdef CONFIG_TRACE_SYSLOG
23 #include <syslog.h>
24 #endif
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/config-file.h"
28 #include "monitor/monitor.h"
29 
30 int trace_events_enabled_count;
31 /*
32  * Interpretation depends on wether the event has the 'vcpu' property:
33  * - false: Boolean value indicating whether the event is active.
34  * - true : Integral counting the number of vCPUs that have this event enabled.
35  */
36 uint16_t trace_events_dstate[TRACE_EVENT_COUNT];
37 /* Marks events for late vCPU state init */
38 static bool trace_events_dstate_init[TRACE_EVENT_COUNT];
39 
40 QemuOptsList qemu_trace_opts = {
41     .name = "trace",
42     .implied_opt_name = "enable",
43     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
44     .desc = {
45         {
46             .name = "enable",
47             .type = QEMU_OPT_STRING,
48         },
49         {
50             .name = "events",
51             .type = QEMU_OPT_STRING,
52         },{
53             .name = "file",
54             .type = QEMU_OPT_STRING,
55         },
56         { /* end of list */ }
57     },
58 };
59 
60 
61 TraceEvent *trace_event_name(const char *name)
62 {
63     assert(name != NULL);
64 
65     TraceEventID i;
66     for (i = 0; i < trace_event_count(); i++) {
67         TraceEvent *ev = trace_event_id(i);
68         if (strcmp(trace_event_get_name(ev), name) == 0) {
69             return ev;
70         }
71     }
72     return NULL;
73 }
74 
75 static bool pattern_glob(const char *pat, const char *ev)
76 {
77     while (*pat != '\0' && *ev != '\0') {
78         if (*pat == *ev) {
79             pat++;
80             ev++;
81         }
82         else if (*pat == '*') {
83             if (pattern_glob(pat, ev+1)) {
84                 return true;
85             } else if (pattern_glob(pat+1, ev)) {
86                 return true;
87             } else {
88                 return false;
89             }
90         } else {
91             return false;
92         }
93     }
94 
95     while (*pat == '*') {
96         pat++;
97     }
98 
99     if (*pat == '\0' && *ev == '\0') {
100         return true;
101     } else {
102         return false;
103     }
104 }
105 
106 TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
107 {
108     assert(pat != NULL);
109 
110     TraceEventID i;
111 
112     if (ev == NULL) {
113         i = -1;
114     } else {
115         i = trace_event_get_id(ev);
116     }
117     i++;
118 
119     while (i < trace_event_count()) {
120         TraceEvent *res = trace_event_id(i);
121         if (pattern_glob(pat, trace_event_get_name(res))) {
122             return res;
123         }
124         i++;
125     }
126 
127     return NULL;
128 }
129 
130 void trace_list_events(void)
131 {
132     int i;
133     for (i = 0; i < trace_event_count(); i++) {
134         TraceEvent *res = trace_event_id(i);
135         fprintf(stderr, "%s\n", trace_event_get_name(res));
136     }
137 }
138 
139 static void do_trace_enable_events(const char *line_buf)
140 {
141     const bool enable = ('-' != line_buf[0]);
142     const char *line_ptr = enable ? line_buf : line_buf + 1;
143 
144     if (trace_event_is_pattern(line_ptr)) {
145         TraceEvent *ev = NULL;
146         while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
147             if (trace_event_get_state_static(ev)) {
148                 /* start tracing */
149                 trace_event_set_state_dynamic(ev, enable);
150                 /* mark for late vCPU init */
151                 trace_events_dstate_init[ev->id] = true;
152             }
153         }
154     } else {
155         TraceEvent *ev = trace_event_name(line_ptr);
156         if (ev == NULL) {
157             error_report("WARNING: trace event '%s' does not exist",
158                          line_ptr);
159         } else if (!trace_event_get_state_static(ev)) {
160             error_report("WARNING: trace event '%s' is not traceable",
161                          line_ptr);
162         } else {
163             /* start tracing */
164             trace_event_set_state_dynamic(ev, enable);
165             /* mark for late vCPU init */
166             trace_events_dstate_init[ev->id] = true;
167         }
168     }
169 }
170 
171 void trace_enable_events(const char *line_buf)
172 {
173     if (is_help_option(line_buf)) {
174         trace_list_events();
175         if (cur_mon == NULL) {
176             exit(0);
177         }
178     } else {
179         do_trace_enable_events(line_buf);
180     }
181 }
182 
183 static void trace_init_events(const char *fname)
184 {
185     Location loc;
186     FILE *fp;
187     char line_buf[1024];
188     size_t line_idx = 0;
189 
190     if (fname == NULL) {
191         return;
192     }
193 
194     loc_push_none(&loc);
195     loc_set_file(fname, 0);
196     fp = fopen(fname, "r");
197     if (!fp) {
198         error_report("%s", strerror(errno));
199         exit(1);
200     }
201     while (fgets(line_buf, sizeof(line_buf), fp)) {
202         loc_set_file(fname, ++line_idx);
203         size_t len = strlen(line_buf);
204         if (len > 1) {              /* skip empty lines */
205             line_buf[len - 1] = '\0';
206             if ('#' == line_buf[0]) { /* skip commented lines */
207                 continue;
208             }
209             trace_enable_events(line_buf);
210         }
211     }
212     if (fclose(fp) != 0) {
213         loc_set_file(fname, 0);
214         error_report("%s", strerror(errno));
215         exit(1);
216     }
217     loc_pop(&loc);
218 }
219 
220 void trace_init_file(const char *file)
221 {
222 #ifdef CONFIG_TRACE_SIMPLE
223     st_set_trace_file(file);
224 #elif defined CONFIG_TRACE_LOG
225     /* If both the simple and the log backends are enabled, "-trace file"
226      * only applies to the simple backend; use "-D" for the log backend.
227      */
228     if (file) {
229         qemu_set_log_filename(file, &error_fatal);
230     }
231 #else
232     if (file) {
233         fprintf(stderr, "error: -trace file=...: "
234                 "option not supported by the selected tracing backends\n");
235         exit(1);
236     }
237 #endif
238 }
239 
240 bool trace_init_backends(void)
241 {
242 #ifdef CONFIG_TRACE_SIMPLE
243     if (!st_init()) {
244         fprintf(stderr, "failed to initialize simple tracing backend.\n");
245         return false;
246     }
247 #endif
248 
249 #ifdef CONFIG_TRACE_FTRACE
250     if (!ftrace_init()) {
251         fprintf(stderr, "failed to initialize ftrace backend.\n");
252         return false;
253     }
254 #endif
255 
256 #ifdef CONFIG_TRACE_SYSLOG
257     openlog(NULL, LOG_PID, LOG_DAEMON);
258 #endif
259 
260     return true;
261 }
262 
263 char *trace_opt_parse(const char *optarg)
264 {
265     char *trace_file;
266     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
267                                              optarg, true);
268     if (!opts) {
269         exit(1);
270     }
271     if (qemu_opt_get(opts, "enable")) {
272         trace_enable_events(qemu_opt_get(opts, "enable"));
273     }
274     trace_init_events(qemu_opt_get(opts, "events"));
275     trace_file = g_strdup(qemu_opt_get(opts, "file"));
276     qemu_opts_del(opts);
277 
278     return trace_file;
279 }
280 
281 void trace_init_vcpu_events(void)
282 {
283     TraceEvent *ev = NULL;
284     while ((ev = trace_event_pattern("*", ev)) != NULL) {
285         if (trace_event_is_vcpu(ev) &&
286             trace_event_get_state_static(ev) &&
287             trace_events_dstate_init[ev->id]) {
288             trace_event_set_state_dynamic(ev, true);
289         }
290     }
291 }
292