OSDN Git Service

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