xref: /openbmc/qemu/trace/control.c (revision 9f591a5d)
123d15e86SLluís /*
223d15e86SLluís  * Interface for configuring and controlling the state of tracing events.
323d15e86SLluís  *
448151859SLluís Vilanova  * Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
523d15e86SLluís  *
6b1bae816SLluís Vilanova  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7b1bae816SLluís Vilanova  * See the COPYING file in the top-level directory.
823d15e86SLluís  */
923d15e86SLluís 
10d38ea87aSPeter Maydell #include "qemu/osdep.h"
1123d15e86SLluís #include "trace/control.h"
12f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
13922a01a0SMarkus Armbruster #include "qemu/option.h"
145b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
155b808275SLluís Vilanova #include "trace/simple.h"
165b808275SLluís Vilanova #endif
175b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
185b808275SLluís Vilanova #include "trace/ftrace.h"
195b808275SLluís Vilanova #endif
20ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
21ed7f5f1dSPaolo Bonzini #include "qemu/log.h"
22ed7f5f1dSPaolo Bonzini #endif
230a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG
240a852417SPaul Durrant #include <syslog.h>
250a852417SPaul Durrant #endif
26daa76aa4SMarkus Armbruster #include "qapi/error.h"
27a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h"
28e9e0bb2aSDenis V. Lunev #include "qemu/config-file.h"
29acc6809dSDenis V. Lunev #include "monitor/monitor.h"
300ab8ed18SDaniel P. Berrange #include "trace-root.h"
3123d15e86SLluís 
3243b48cfcSPaolo Bonzini int trace_events_enabled_count;
3343b48cfcSPaolo Bonzini 
34fe4db84dSDaniel P. Berrange typedef struct TraceEventGroup {
35fe4db84dSDaniel P. Berrange     TraceEvent **events;
36fe4db84dSDaniel P. Berrange } TraceEventGroup;
37fe4db84dSDaniel P. Berrange 
38fe4db84dSDaniel P. Berrange static TraceEventGroup *event_groups;
39fe4db84dSDaniel P. Berrange static size_t nevent_groups;
40ca3fa0e8SDaniel P. Berrange static uint32_t next_id;
41ca3fa0e8SDaniel P. Berrange static uint32_t next_vcpu_id;
42fe4db84dSDaniel P. Berrange 
43e9e0bb2aSDenis V. Lunev QemuOptsList qemu_trace_opts = {
44e9e0bb2aSDenis V. Lunev     .name = "trace",
45e9e0bb2aSDenis V. Lunev     .implied_opt_name = "enable",
46e9e0bb2aSDenis V. Lunev     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
47e9e0bb2aSDenis V. Lunev     .desc = {
48e9e0bb2aSDenis V. Lunev         {
49e9e0bb2aSDenis V. Lunev             .name = "enable",
50e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
51e9e0bb2aSDenis V. Lunev         },
52e9e0bb2aSDenis V. Lunev         {
53e9e0bb2aSDenis V. Lunev             .name = "events",
54e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
55e9e0bb2aSDenis V. Lunev         },{
56e9e0bb2aSDenis V. Lunev             .name = "file",
57e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
58e9e0bb2aSDenis V. Lunev         },
59e9e0bb2aSDenis V. Lunev         { /* end of list */ }
60e9e0bb2aSDenis V. Lunev     },
61e9e0bb2aSDenis V. Lunev };
62e9e0bb2aSDenis V. Lunev 
63e9e0bb2aSDenis V. Lunev 
64fe4db84dSDaniel P. Berrange void trace_event_register_group(TraceEvent **events)
65fe4db84dSDaniel P. Berrange {
66ca3fa0e8SDaniel P. Berrange     size_t i;
67ca3fa0e8SDaniel P. Berrange     for (i = 0; events[i] != NULL; i++) {
68ca3fa0e8SDaniel P. Berrange         events[i]->id = next_id++;
69d01c05c9SLluís Vilanova         if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
70d01c05c9SLluís Vilanova             continue;
71d01c05c9SLluís Vilanova         }
72d01c05c9SLluís Vilanova 
73d01c05c9SLluís Vilanova         if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
74ca3fa0e8SDaniel P. Berrange             events[i]->vcpu_id = next_vcpu_id++;
75d01c05c9SLluís Vilanova         } else {
7655d527a9SAlistair Francis             warn_report("too many vcpu trace events; dropping '%s'",
77d01c05c9SLluís Vilanova                         events[i]->name);
78ca3fa0e8SDaniel P. Berrange         }
79ca3fa0e8SDaniel P. Berrange     }
80fe4db84dSDaniel P. Berrange     event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
81fe4db84dSDaniel P. Berrange     event_groups[nevent_groups].events = events;
82fe4db84dSDaniel P. Berrange     nevent_groups++;
83fe4db84dSDaniel P. Berrange }
84fe4db84dSDaniel P. Berrange 
85fe4db84dSDaniel P. Berrange 
86b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name)
87b1bae816SLluís Vilanova {
88b1bae816SLluís Vilanova     assert(name != NULL);
89b1bae816SLluís Vilanova 
900d4e995cSDaniel P. Berrange     TraceEventIter iter;
910d4e995cSDaniel P. Berrange     TraceEvent *ev;
920d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, NULL);
930d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
94b1bae816SLluís Vilanova         if (strcmp(trace_event_get_name(ev), name) == 0) {
95b1bae816SLluís Vilanova             return ev;
96b1bae816SLluís Vilanova         }
97b1bae816SLluís Vilanova     }
98b1bae816SLluís Vilanova     return NULL;
99b1bae816SLluís Vilanova }
100b1bae816SLluís Vilanova 
101b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev)
102b1bae816SLluís Vilanova {
103b1bae816SLluís Vilanova     while (*pat != '\0' && *ev != '\0') {
104b1bae816SLluís Vilanova         if (*pat == *ev) {
105b1bae816SLluís Vilanova             pat++;
106b1bae816SLluís Vilanova             ev++;
107b1bae816SLluís Vilanova         }
108b1bae816SLluís Vilanova         else if (*pat == '*') {
109b1bae816SLluís Vilanova             if (pattern_glob(pat, ev+1)) {
110b1bae816SLluís Vilanova                 return true;
111b1bae816SLluís Vilanova             } else if (pattern_glob(pat+1, ev)) {
112b1bae816SLluís Vilanova                 return true;
113b1bae816SLluís Vilanova             } else {
114b1bae816SLluís Vilanova                 return false;
115b1bae816SLluís Vilanova             }
116b1bae816SLluís Vilanova         } else {
117b1bae816SLluís Vilanova             return false;
118b1bae816SLluís Vilanova         }
119b1bae816SLluís Vilanova     }
120b1bae816SLluís Vilanova 
121b1bae816SLluís Vilanova     while (*pat == '*') {
122b1bae816SLluís Vilanova         pat++;
123b1bae816SLluís Vilanova     }
124b1bae816SLluís Vilanova 
125b1bae816SLluís Vilanova     if (*pat == '\0' && *ev == '\0') {
126b1bae816SLluís Vilanova         return true;
127b1bae816SLluís Vilanova     } else {
128b1bae816SLluís Vilanova         return false;
129b1bae816SLluís Vilanova     }
130b1bae816SLluís Vilanova }
131b1bae816SLluís Vilanova 
132b1bae816SLluís Vilanova 
1336a1b0f3aSDaniel P. Berrange void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
1346a1b0f3aSDaniel P. Berrange {
1356a1b0f3aSDaniel P. Berrange     iter->event = 0;
136fe4db84dSDaniel P. Berrange     iter->group = 0;
1376a1b0f3aSDaniel P. Berrange     iter->pattern = pattern;
1386a1b0f3aSDaniel P. Berrange }
1396a1b0f3aSDaniel P. Berrange 
1406a1b0f3aSDaniel P. Berrange TraceEvent *trace_event_iter_next(TraceEventIter *iter)
1416a1b0f3aSDaniel P. Berrange {
142fe4db84dSDaniel P. Berrange     while (iter->group < nevent_groups &&
143fe4db84dSDaniel P. Berrange            event_groups[iter->group].events[iter->event] != NULL) {
144fe4db84dSDaniel P. Berrange         TraceEvent *ev = event_groups[iter->group].events[iter->event];
1456a1b0f3aSDaniel P. Berrange         iter->event++;
146fe4db84dSDaniel P. Berrange         if (event_groups[iter->group].events[iter->event] == NULL) {
147fe4db84dSDaniel P. Berrange             iter->event = 0;
148fe4db84dSDaniel P. Berrange             iter->group++;
149fe4db84dSDaniel P. Berrange         }
1506a1b0f3aSDaniel P. Berrange         if (!iter->pattern ||
1516a1b0f3aSDaniel P. Berrange             pattern_glob(iter->pattern,
1526a1b0f3aSDaniel P. Berrange                          trace_event_get_name(ev))) {
1536a1b0f3aSDaniel P. Berrange             return ev;
1546a1b0f3aSDaniel P. Berrange         }
1556a1b0f3aSDaniel P. Berrange     }
1566a1b0f3aSDaniel P. Berrange 
1576a1b0f3aSDaniel P. Berrange     return NULL;
1586a1b0f3aSDaniel P. Berrange }
1596a1b0f3aSDaniel P. Berrange 
160e9527dd3SPaolo Bonzini void trace_list_events(void)
161e9527dd3SPaolo Bonzini {
1620d4e995cSDaniel P. Berrange     TraceEventIter iter;
1630d4e995cSDaniel P. Berrange     TraceEvent *ev;
1640d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, NULL);
1650d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1660d4e995cSDaniel P. Berrange         fprintf(stderr, "%s\n", trace_event_get_name(ev));
167e9527dd3SPaolo Bonzini     }
168*9f591a5dSPhilippe Mathieu-Daudé #ifdef CONFIG_TRACE_DTRACE
169*9f591a5dSPhilippe Mathieu-Daudé     fprintf(stderr, "This list of names of trace points may be incomplete "
170*9f591a5dSPhilippe Mathieu-Daudé                     "when using the DTrace/SystemTap backends.\n"
171*9f591a5dSPhilippe Mathieu-Daudé                     "Run 'qemu-trace-stap list %s' to print the full list.\n",
172*9f591a5dSPhilippe Mathieu-Daudé             error_get_progname());
173*9f591a5dSPhilippe Mathieu-Daudé #endif
174e9527dd3SPaolo Bonzini }
175e9527dd3SPaolo Bonzini 
176e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf)
17710578a25SPaolo Bonzini {
17810578a25SPaolo Bonzini     const bool enable = ('-' != line_buf[0]);
17910578a25SPaolo Bonzini     const char *line_ptr = enable ? line_buf : line_buf + 1;
1800d4e995cSDaniel P. Berrange     TraceEventIter iter;
1810d4e995cSDaniel P. Berrange     TraceEvent *ev;
1820d4e995cSDaniel P. Berrange     bool is_pattern = trace_event_is_pattern(line_ptr);
18310578a25SPaolo Bonzini 
1840d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, line_ptr);
1850d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1860d4e995cSDaniel P. Berrange         if (!trace_event_get_state_static(ev)) {
1870d4e995cSDaniel P. Berrange             if (!is_pattern) {
1883dc6f869SAlistair Francis                 warn_report("trace event '%s' is not traceable",
18910578a25SPaolo Bonzini                             line_ptr);
1900d4e995cSDaniel P. Berrange                 return;
19110578a25SPaolo Bonzini             }
1920d4e995cSDaniel P. Berrange             continue;
1930d4e995cSDaniel P. Berrange         }
1940d4e995cSDaniel P. Berrange 
1950d4e995cSDaniel P. Berrange         /* start tracing */
1960d4e995cSDaniel P. Berrange         trace_event_set_state_dynamic(ev, enable);
1970d4e995cSDaniel P. Berrange         if (!is_pattern) {
1980d4e995cSDaniel P. Berrange             return;
1990d4e995cSDaniel P. Berrange         }
2000d4e995cSDaniel P. Berrange     }
2010d4e995cSDaniel P. Berrange 
2020d4e995cSDaniel P. Berrange     if (!is_pattern) {
2033dc6f869SAlistair Francis         warn_report("trace event '%s' does not exist",
2040d4e995cSDaniel P. Berrange                     line_ptr);
20510578a25SPaolo Bonzini     }
20610578a25SPaolo Bonzini }
20710578a25SPaolo Bonzini 
208e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf)
209e9527dd3SPaolo Bonzini {
210e9527dd3SPaolo Bonzini     if (is_help_option(line_buf)) {
211e9527dd3SPaolo Bonzini         trace_list_events();
212acc6809dSDenis V. Lunev         if (cur_mon == NULL) {
213e9527dd3SPaolo Bonzini             exit(0);
214acc6809dSDenis V. Lunev         }
215e9527dd3SPaolo Bonzini     } else {
216e9527dd3SPaolo Bonzini         do_trace_enable_events(line_buf);
217e9527dd3SPaolo Bonzini     }
218e9527dd3SPaolo Bonzini }
219e9527dd3SPaolo Bonzini 
220e9e0bb2aSDenis V. Lunev static void trace_init_events(const char *fname)
22123d15e86SLluís {
222a35d9be6SAlexey Kardashevskiy     Location loc;
223a35d9be6SAlexey Kardashevskiy     FILE *fp;
224a35d9be6SAlexey Kardashevskiy     char line_buf[1024];
225a35d9be6SAlexey Kardashevskiy     size_t line_idx = 0;
226a35d9be6SAlexey Kardashevskiy 
22723d15e86SLluís     if (fname == NULL) {
22823d15e86SLluís         return;
22923d15e86SLluís     }
23023d15e86SLluís 
231a35d9be6SAlexey Kardashevskiy     loc_push_none(&loc);
232a35d9be6SAlexey Kardashevskiy     loc_set_file(fname, 0);
233a35d9be6SAlexey Kardashevskiy     fp = fopen(fname, "r");
23423d15e86SLluís     if (!fp) {
235a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
23623d15e86SLluís         exit(1);
23723d15e86SLluís     }
23823d15e86SLluís     while (fgets(line_buf, sizeof(line_buf), fp)) {
239a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, ++line_idx);
24023d15e86SLluís         size_t len = strlen(line_buf);
24123d15e86SLluís         if (len > 1) {              /* skip empty lines */
24223d15e86SLluís             line_buf[len - 1] = '\0';
243794b1f96SAlexey Kardashevskiy             if ('#' == line_buf[0]) { /* skip commented lines */
244794b1f96SAlexey Kardashevskiy                 continue;
245794b1f96SAlexey Kardashevskiy             }
24610578a25SPaolo Bonzini             trace_enable_events(line_buf);
24723d15e86SLluís         }
24882432638SAlexey Kardashevskiy     }
24923d15e86SLluís     if (fclose(fp) != 0) {
250a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, 0);
251a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
25223d15e86SLluís         exit(1);
25323d15e86SLluís     }
254a35d9be6SAlexey Kardashevskiy     loc_pop(&loc);
25523d15e86SLluís }
2565b808275SLluís Vilanova 
25741fc57e4SPaolo Bonzini void trace_init_file(const char *file)
2585b808275SLluís Vilanova {
2595b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
26041fc57e4SPaolo Bonzini     st_set_trace_file(file);
261ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG
262db817b8cSYaowei Bai     /* If both the simple and the log backends are enabled, "--trace file"
263ed7f5f1dSPaolo Bonzini      * only applies to the simple backend; use "-D" for the log backend.
264ed7f5f1dSPaolo Bonzini      */
265ed7f5f1dSPaolo Bonzini     if (file) {
266daa76aa4SMarkus Armbruster         qemu_set_log_filename(file, &error_fatal);
267ed7f5f1dSPaolo Bonzini     }
2685b808275SLluís Vilanova #else
2695b808275SLluís Vilanova     if (file) {
270db817b8cSYaowei Bai         fprintf(stderr, "error: --trace file=...: "
2715b808275SLluís Vilanova                 "option not supported by the selected tracing backends\n");
27241fc57e4SPaolo Bonzini         exit(1);
27341fc57e4SPaolo Bonzini     }
27441fc57e4SPaolo Bonzini #endif
27541fc57e4SPaolo Bonzini }
27641fc57e4SPaolo Bonzini 
27782e95ec8SLluís Vilanova void trace_fini_vcpu(CPUState *vcpu)
27882e95ec8SLluís Vilanova {
27982e95ec8SLluís Vilanova     TraceEventIter iter;
28082e95ec8SLluís Vilanova     TraceEvent *ev;
28182e95ec8SLluís Vilanova 
282a47e8715SLluís Vilanova     trace_guest_cpu_exit(vcpu);
283a47e8715SLluís Vilanova 
28482e95ec8SLluís Vilanova     trace_event_iter_init(&iter, NULL);
28582e95ec8SLluís Vilanova     while ((ev = trace_event_iter_next(&iter)) != NULL) {
28682e95ec8SLluís Vilanova         if (trace_event_is_vcpu(ev) &&
28782e95ec8SLluís Vilanova             trace_event_get_state_static(ev) &&
28882e95ec8SLluís Vilanova             trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
28982e95ec8SLluís Vilanova             /* must disable to affect the global counter */
29082e95ec8SLluís Vilanova             trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
29182e95ec8SLluís Vilanova         }
29282e95ec8SLluís Vilanova     }
29382e95ec8SLluís Vilanova }
29482e95ec8SLluís Vilanova 
29541fc57e4SPaolo Bonzini bool trace_init_backends(void)
29641fc57e4SPaolo Bonzini {
29741fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE
29841fc57e4SPaolo Bonzini     if (!st_init()) {
29941fc57e4SPaolo Bonzini         fprintf(stderr, "failed to initialize simple tracing backend.\n");
3005b808275SLluís Vilanova         return false;
3015b808275SLluís Vilanova     }
3025b808275SLluís Vilanova #endif
3035b808275SLluís Vilanova 
3045b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
3055b808275SLluís Vilanova     if (!ftrace_init()) {
3065b808275SLluís Vilanova         fprintf(stderr, "failed to initialize ftrace backend.\n");
3075b808275SLluís Vilanova         return false;
3085b808275SLluís Vilanova     }
3095b808275SLluís Vilanova #endif
3105b808275SLluís Vilanova 
3110a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG
3120a852417SPaul Durrant     openlog(NULL, LOG_PID, LOG_DAEMON);
3130a852417SPaul Durrant #endif
3140a852417SPaul Durrant 
3155b808275SLluís Vilanova     return true;
3165b808275SLluís Vilanova }
317e9e0bb2aSDenis V. Lunev 
318e9e0bb2aSDenis V. Lunev char *trace_opt_parse(const char *optarg)
319e9e0bb2aSDenis V. Lunev {
320e9e0bb2aSDenis V. Lunev     char *trace_file;
321e9e0bb2aSDenis V. Lunev     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
322e9e0bb2aSDenis V. Lunev                                              optarg, true);
323e9e0bb2aSDenis V. Lunev     if (!opts) {
324e9e0bb2aSDenis V. Lunev         exit(1);
325e9e0bb2aSDenis V. Lunev     }
326e9e0bb2aSDenis V. Lunev     if (qemu_opt_get(opts, "enable")) {
327e9e0bb2aSDenis V. Lunev         trace_enable_events(qemu_opt_get(opts, "enable"));
328e9e0bb2aSDenis V. Lunev     }
329e9e0bb2aSDenis V. Lunev     trace_init_events(qemu_opt_get(opts, "events"));
330e9e0bb2aSDenis V. Lunev     trace_file = g_strdup(qemu_opt_get(opts, "file"));
331e9e0bb2aSDenis V. Lunev     qemu_opts_del(opts);
332e9e0bb2aSDenis V. Lunev 
333e9e0bb2aSDenis V. Lunev     return trace_file;
334e9e0bb2aSDenis V. Lunev }
335b7d48952SDaniel P. Berrange 
336b7d48952SDaniel P. Berrange uint32_t trace_get_vcpu_event_count(void)
337b7d48952SDaniel P. Berrange {
338ca3fa0e8SDaniel P. Berrange     return next_vcpu_id;
339b7d48952SDaniel P. Berrange }
340