xref: /openbmc/qemu/trace/control.c (revision c5cc58b1)
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"
30243af022SPaolo Bonzini #include "trace/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;
42648b4823SJosh DuBois static bool init_trace_on_startup;
439f45a641SPaolo Bonzini static char *trace_opts_file;
44fe4db84dSDaniel P. Berrange 
45e9e0bb2aSDenis V. Lunev QemuOptsList qemu_trace_opts = {
46e9e0bb2aSDenis V. Lunev     .name = "trace",
47e9e0bb2aSDenis V. Lunev     .implied_opt_name = "enable",
48e9e0bb2aSDenis V. Lunev     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
49e9e0bb2aSDenis V. Lunev     .desc = {
50e9e0bb2aSDenis V. Lunev         {
51e9e0bb2aSDenis V. Lunev             .name = "enable",
52e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
53e9e0bb2aSDenis V. Lunev         },
54e9e0bb2aSDenis V. Lunev         {
55e9e0bb2aSDenis V. Lunev             .name = "events",
56e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
57e9e0bb2aSDenis V. Lunev         },{
58e9e0bb2aSDenis V. Lunev             .name = "file",
59e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
60e9e0bb2aSDenis V. Lunev         },
61e9e0bb2aSDenis V. Lunev         { /* end of list */ }
62e9e0bb2aSDenis V. Lunev     },
63e9e0bb2aSDenis V. Lunev };
64e9e0bb2aSDenis V. Lunev 
65e9e0bb2aSDenis V. Lunev 
66fe4db84dSDaniel P. Berrange void trace_event_register_group(TraceEvent **events)
67fe4db84dSDaniel P. Berrange {
68ca3fa0e8SDaniel P. Berrange     size_t i;
69ca3fa0e8SDaniel P. Berrange     for (i = 0; events[i] != NULL; i++) {
70ca3fa0e8SDaniel P. Berrange         events[i]->id = next_id++;
71d01c05c9SLluís Vilanova         if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
72d01c05c9SLluís Vilanova             continue;
73d01c05c9SLluís Vilanova         }
74d01c05c9SLluís Vilanova 
75d01c05c9SLluís Vilanova         if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
76ca3fa0e8SDaniel P. Berrange             events[i]->vcpu_id = next_vcpu_id++;
77d01c05c9SLluís Vilanova         } else {
7855d527a9SAlistair Francis             warn_report("too many vcpu trace events; dropping '%s'",
79d01c05c9SLluís Vilanova                         events[i]->name);
80ca3fa0e8SDaniel P. Berrange         }
81ca3fa0e8SDaniel P. Berrange     }
82fe4db84dSDaniel P. Berrange     event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
83fe4db84dSDaniel P. Berrange     event_groups[nevent_groups].events = events;
84fe4db84dSDaniel P. Berrange     nevent_groups++;
85fe4db84dSDaniel P. Berrange }
86fe4db84dSDaniel P. Berrange 
87fe4db84dSDaniel P. Berrange 
88b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name)
89b1bae816SLluís Vilanova {
90b1bae816SLluís Vilanova     assert(name != NULL);
91b1bae816SLluís Vilanova 
920d4e995cSDaniel P. Berrange     TraceEventIter iter;
930d4e995cSDaniel P. Berrange     TraceEvent *ev;
94117856c3SGerd Hoffmann     trace_event_iter_init_all(&iter);
950d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
96b1bae816SLluís Vilanova         if (strcmp(trace_event_get_name(ev), name) == 0) {
97b1bae816SLluís Vilanova             return ev;
98b1bae816SLluís Vilanova         }
99b1bae816SLluís Vilanova     }
100b1bae816SLluís Vilanova     return NULL;
101b1bae816SLluís Vilanova }
102b1bae816SLluís Vilanova 
103117856c3SGerd Hoffmann void trace_event_iter_init_all(TraceEventIter *iter)
1046a1b0f3aSDaniel P. Berrange {
1056a1b0f3aSDaniel P. Berrange     iter->event = 0;
106fe4db84dSDaniel P. Berrange     iter->group = 0;
107*c5cc58b1SGerd Hoffmann     iter->group_id = -1;
108117856c3SGerd Hoffmann     iter->pattern = NULL;
109117856c3SGerd Hoffmann }
110117856c3SGerd Hoffmann 
111117856c3SGerd Hoffmann void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
112117856c3SGerd Hoffmann {
113117856c3SGerd Hoffmann     trace_event_iter_init_all(iter);
1146a1b0f3aSDaniel P. Berrange     iter->pattern = pattern;
1156a1b0f3aSDaniel P. Berrange }
1166a1b0f3aSDaniel P. Berrange 
117*c5cc58b1SGerd Hoffmann void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id)
118*c5cc58b1SGerd Hoffmann {
119*c5cc58b1SGerd Hoffmann     trace_event_iter_init_all(iter);
120*c5cc58b1SGerd Hoffmann     iter->group_id = group_id;
121*c5cc58b1SGerd Hoffmann }
122*c5cc58b1SGerd Hoffmann 
1236a1b0f3aSDaniel P. Berrange TraceEvent *trace_event_iter_next(TraceEventIter *iter)
1246a1b0f3aSDaniel P. Berrange {
125fe4db84dSDaniel P. Berrange     while (iter->group < nevent_groups &&
126fe4db84dSDaniel P. Berrange            event_groups[iter->group].events[iter->event] != NULL) {
127fe4db84dSDaniel P. Berrange         TraceEvent *ev = event_groups[iter->group].events[iter->event];
128*c5cc58b1SGerd Hoffmann         size_t group = iter->group;
1296a1b0f3aSDaniel P. Berrange         iter->event++;
130fe4db84dSDaniel P. Berrange         if (event_groups[iter->group].events[iter->event] == NULL) {
131fe4db84dSDaniel P. Berrange             iter->event = 0;
132fe4db84dSDaniel P. Berrange             iter->group++;
133fe4db84dSDaniel P. Berrange         }
134*c5cc58b1SGerd Hoffmann         if (iter->pattern &&
135*c5cc58b1SGerd Hoffmann             !g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
136*c5cc58b1SGerd Hoffmann             continue;
1376a1b0f3aSDaniel P. Berrange         }
138*c5cc58b1SGerd Hoffmann         if (iter->group_id != -1 &&
139*c5cc58b1SGerd Hoffmann             iter->group_id != group) {
140*c5cc58b1SGerd Hoffmann             continue;
141*c5cc58b1SGerd Hoffmann         }
142*c5cc58b1SGerd Hoffmann         return ev;
1436a1b0f3aSDaniel P. Berrange     }
1446a1b0f3aSDaniel P. Berrange 
1456a1b0f3aSDaniel P. Berrange     return NULL;
1466a1b0f3aSDaniel P. Berrange }
1476a1b0f3aSDaniel P. Berrange 
1486745c8a0SDoug Evans void trace_list_events(FILE *f)
149e9527dd3SPaolo Bonzini {
1500d4e995cSDaniel P. Berrange     TraceEventIter iter;
1510d4e995cSDaniel P. Berrange     TraceEvent *ev;
152117856c3SGerd Hoffmann     trace_event_iter_init_all(&iter);
1530d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1546745c8a0SDoug Evans         fprintf(f, "%s\n", trace_event_get_name(ev));
155e9527dd3SPaolo Bonzini     }
1569f591a5dSPhilippe Mathieu-Daudé #ifdef CONFIG_TRACE_DTRACE
1576745c8a0SDoug Evans     fprintf(f, "This list of names of trace points may be incomplete "
1589f591a5dSPhilippe Mathieu-Daudé                "when using the DTrace/SystemTap backends.\n"
1599f591a5dSPhilippe Mathieu-Daudé                "Run 'qemu-trace-stap list %s' to print the full list.\n",
1609f591a5dSPhilippe Mathieu-Daudé             error_get_progname());
1619f591a5dSPhilippe Mathieu-Daudé #endif
162e9527dd3SPaolo Bonzini }
163e9527dd3SPaolo Bonzini 
164e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf)
16510578a25SPaolo Bonzini {
16610578a25SPaolo Bonzini     const bool enable = ('-' != line_buf[0]);
16710578a25SPaolo Bonzini     const char *line_ptr = enable ? line_buf : line_buf + 1;
1680d4e995cSDaniel P. Berrange     TraceEventIter iter;
1690d4e995cSDaniel P. Berrange     TraceEvent *ev;
1700d4e995cSDaniel P. Berrange     bool is_pattern = trace_event_is_pattern(line_ptr);
17110578a25SPaolo Bonzini 
172117856c3SGerd Hoffmann     trace_event_iter_init_pattern(&iter, line_ptr);
1730d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1740d4e995cSDaniel P. Berrange         if (!trace_event_get_state_static(ev)) {
1750d4e995cSDaniel P. Berrange             if (!is_pattern) {
1763dc6f869SAlistair Francis                 warn_report("trace event '%s' is not traceable",
17710578a25SPaolo Bonzini                             line_ptr);
1780d4e995cSDaniel P. Berrange                 return;
17910578a25SPaolo Bonzini             }
1800d4e995cSDaniel P. Berrange             continue;
1810d4e995cSDaniel P. Berrange         }
1820d4e995cSDaniel P. Berrange 
1830d4e995cSDaniel P. Berrange         /* start tracing */
1840d4e995cSDaniel P. Berrange         trace_event_set_state_dynamic(ev, enable);
1850d4e995cSDaniel P. Berrange         if (!is_pattern) {
1860d4e995cSDaniel P. Berrange             return;
1870d4e995cSDaniel P. Berrange         }
1880d4e995cSDaniel P. Berrange     }
1890d4e995cSDaniel P. Berrange 
1900d4e995cSDaniel P. Berrange     if (!is_pattern) {
1913dc6f869SAlistair Francis         warn_report("trace event '%s' does not exist",
1920d4e995cSDaniel P. Berrange                     line_ptr);
19310578a25SPaolo Bonzini     }
19410578a25SPaolo Bonzini }
19510578a25SPaolo Bonzini 
196e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf)
197e9527dd3SPaolo Bonzini {
198e9527dd3SPaolo Bonzini     if (is_help_option(line_buf)) {
1996745c8a0SDoug Evans         trace_list_events(stdout);
200947e4744SKevin Wolf         if (monitor_cur() == NULL) {
201e9527dd3SPaolo Bonzini             exit(0);
202acc6809dSDenis V. Lunev         }
203e9527dd3SPaolo Bonzini     } else {
204e9527dd3SPaolo Bonzini         do_trace_enable_events(line_buf);
205e9527dd3SPaolo Bonzini     }
206e9527dd3SPaolo Bonzini }
207e9527dd3SPaolo Bonzini 
208e9e0bb2aSDenis V. Lunev static void trace_init_events(const char *fname)
20923d15e86SLluís {
210a35d9be6SAlexey Kardashevskiy     Location loc;
211a35d9be6SAlexey Kardashevskiy     FILE *fp;
212a35d9be6SAlexey Kardashevskiy     char line_buf[1024];
213a35d9be6SAlexey Kardashevskiy     size_t line_idx = 0;
214a35d9be6SAlexey Kardashevskiy 
21523d15e86SLluís     if (fname == NULL) {
21623d15e86SLluís         return;
21723d15e86SLluís     }
21823d15e86SLluís 
219a35d9be6SAlexey Kardashevskiy     loc_push_none(&loc);
220a35d9be6SAlexey Kardashevskiy     loc_set_file(fname, 0);
221a35d9be6SAlexey Kardashevskiy     fp = fopen(fname, "r");
22223d15e86SLluís     if (!fp) {
223a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
22423d15e86SLluís         exit(1);
22523d15e86SLluís     }
22623d15e86SLluís     while (fgets(line_buf, sizeof(line_buf), fp)) {
227a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, ++line_idx);
22823d15e86SLluís         size_t len = strlen(line_buf);
22923d15e86SLluís         if (len > 1) {              /* skip empty lines */
23023d15e86SLluís             line_buf[len - 1] = '\0';
231794b1f96SAlexey Kardashevskiy             if ('#' == line_buf[0]) { /* skip commented lines */
232794b1f96SAlexey Kardashevskiy                 continue;
233794b1f96SAlexey Kardashevskiy             }
23410578a25SPaolo Bonzini             trace_enable_events(line_buf);
23523d15e86SLluís         }
23682432638SAlexey Kardashevskiy     }
23723d15e86SLluís     if (fclose(fp) != 0) {
238a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, 0);
239a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
24023d15e86SLluís         exit(1);
24123d15e86SLluís     }
242a35d9be6SAlexey Kardashevskiy     loc_pop(&loc);
24323d15e86SLluís }
2445b808275SLluís Vilanova 
24592eecfffSPaolo Bonzini void trace_init_file(void)
2465b808275SLluís Vilanova {
2475b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
2489f45a641SPaolo Bonzini     st_set_trace_file(trace_opts_file);
249648b4823SJosh DuBois     if (init_trace_on_startup) {
2501b7157beSJosh DuBois         st_set_trace_file_enabled(true);
251648b4823SJosh DuBois     }
252ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG
2533d88754eSAlex Bennée     /*
2543d88754eSAlex Bennée      * If both the simple and the log backends are enabled, "--trace file"
2553d88754eSAlex Bennée      * only applies to the simple backend; use "-D" for the log
2563d88754eSAlex Bennée      * backend. However we should only override -D if we actually have
2573d88754eSAlex Bennée      * something to override it with.
258ed7f5f1dSPaolo Bonzini      */
2599f45a641SPaolo Bonzini     if (trace_opts_file) {
2609f45a641SPaolo Bonzini         qemu_set_log_filename(trace_opts_file, &error_fatal);
2613d88754eSAlex Bennée     }
2625b808275SLluís Vilanova #else
2639f45a641SPaolo Bonzini     if (trace_opts_file) {
264db817b8cSYaowei Bai         fprintf(stderr, "error: --trace file=...: "
2655b808275SLluís Vilanova                 "option not supported by the selected tracing backends\n");
26641fc57e4SPaolo Bonzini         exit(1);
26741fc57e4SPaolo Bonzini     }
26841fc57e4SPaolo Bonzini #endif
26941fc57e4SPaolo Bonzini }
27041fc57e4SPaolo Bonzini 
27182e95ec8SLluís Vilanova void trace_fini_vcpu(CPUState *vcpu)
27282e95ec8SLluís Vilanova {
27382e95ec8SLluís Vilanova     TraceEventIter iter;
27482e95ec8SLluís Vilanova     TraceEvent *ev;
27582e95ec8SLluís Vilanova 
276a47e8715SLluís Vilanova     trace_guest_cpu_exit(vcpu);
277a47e8715SLluís Vilanova 
278117856c3SGerd Hoffmann     trace_event_iter_init_all(&iter);
27982e95ec8SLluís Vilanova     while ((ev = trace_event_iter_next(&iter)) != NULL) {
28082e95ec8SLluís Vilanova         if (trace_event_is_vcpu(ev) &&
28182e95ec8SLluís Vilanova             trace_event_get_state_static(ev) &&
28282e95ec8SLluís Vilanova             trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
28382e95ec8SLluís Vilanova             /* must disable to affect the global counter */
28482e95ec8SLluís Vilanova             trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
28582e95ec8SLluís Vilanova         }
28682e95ec8SLluís Vilanova     }
28782e95ec8SLluís Vilanova }
28882e95ec8SLluís Vilanova 
28941fc57e4SPaolo Bonzini bool trace_init_backends(void)
29041fc57e4SPaolo Bonzini {
29141fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE
29241fc57e4SPaolo Bonzini     if (!st_init()) {
29341fc57e4SPaolo Bonzini         fprintf(stderr, "failed to initialize simple tracing backend.\n");
2945b808275SLluís Vilanova         return false;
2955b808275SLluís Vilanova     }
2965b808275SLluís Vilanova #endif
2975b808275SLluís Vilanova 
2985b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
2995b808275SLluís Vilanova     if (!ftrace_init()) {
3005b808275SLluís Vilanova         fprintf(stderr, "failed to initialize ftrace backend.\n");
3015b808275SLluís Vilanova         return false;
3025b808275SLluís Vilanova     }
3035b808275SLluís Vilanova #endif
3045b808275SLluís Vilanova 
3050a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG
3060a852417SPaul Durrant     openlog(NULL, LOG_PID, LOG_DAEMON);
3070a852417SPaul Durrant #endif
3080a852417SPaul Durrant 
3095b808275SLluís Vilanova     return true;
3105b808275SLluís Vilanova }
311e9e0bb2aSDenis V. Lunev 
31292eecfffSPaolo Bonzini void trace_opt_parse(const char *optarg)
313e9e0bb2aSDenis V. Lunev {
314e9e0bb2aSDenis V. Lunev     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
315e9e0bb2aSDenis V. Lunev                                              optarg, true);
316e9e0bb2aSDenis V. Lunev     if (!opts) {
317e9e0bb2aSDenis V. Lunev         exit(1);
318e9e0bb2aSDenis V. Lunev     }
319e9e0bb2aSDenis V. Lunev     if (qemu_opt_get(opts, "enable")) {
320e9e0bb2aSDenis V. Lunev         trace_enable_events(qemu_opt_get(opts, "enable"));
321e9e0bb2aSDenis V. Lunev     }
322e9e0bb2aSDenis V. Lunev     trace_init_events(qemu_opt_get(opts, "events"));
323648b4823SJosh DuBois     init_trace_on_startup = true;
3249f45a641SPaolo Bonzini     g_free(trace_opts_file);
3259f45a641SPaolo Bonzini     trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
326e9e0bb2aSDenis V. Lunev     qemu_opts_del(opts);
327e9e0bb2aSDenis V. Lunev }
328b7d48952SDaniel P. Berrange 
329b7d48952SDaniel P. Berrange uint32_t trace_get_vcpu_event_count(void)
330b7d48952SDaniel P. Berrange {
331ca3fa0e8SDaniel P. Berrange     return next_vcpu_id;
332b7d48952SDaniel P. Berrange }
333