xref: /openbmc/qemu/trace/control.c (revision fe4db84d)
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"
135b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
145b808275SLluís Vilanova #include "trace/simple.h"
155b808275SLluís Vilanova #endif
165b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
175b808275SLluís Vilanova #include "trace/ftrace.h"
185b808275SLluís Vilanova #endif
19ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
20ed7f5f1dSPaolo Bonzini #include "qemu/log.h"
21ed7f5f1dSPaolo Bonzini #endif
220a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG
230a852417SPaul Durrant #include <syslog.h>
240a852417SPaul Durrant #endif
25daa76aa4SMarkus Armbruster #include "qapi/error.h"
26a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h"
27e9e0bb2aSDenis V. Lunev #include "qemu/config-file.h"
28acc6809dSDenis V. Lunev #include "monitor/monitor.h"
2923d15e86SLluís 
3043b48cfcSPaolo Bonzini int trace_events_enabled_count;
3143b48cfcSPaolo Bonzini 
32*fe4db84dSDaniel P. Berrange typedef struct TraceEventGroup {
33*fe4db84dSDaniel P. Berrange     TraceEvent **events;
34*fe4db84dSDaniel P. Berrange } TraceEventGroup;
35*fe4db84dSDaniel P. Berrange 
36*fe4db84dSDaniel P. Berrange static TraceEventGroup *event_groups;
37*fe4db84dSDaniel P. Berrange static size_t nevent_groups;
38*fe4db84dSDaniel P. Berrange 
39e9e0bb2aSDenis V. Lunev QemuOptsList qemu_trace_opts = {
40e9e0bb2aSDenis V. Lunev     .name = "trace",
41e9e0bb2aSDenis V. Lunev     .implied_opt_name = "enable",
42e9e0bb2aSDenis V. Lunev     .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
43e9e0bb2aSDenis V. Lunev     .desc = {
44e9e0bb2aSDenis V. Lunev         {
45e9e0bb2aSDenis V. Lunev             .name = "enable",
46e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
47e9e0bb2aSDenis V. Lunev         },
48e9e0bb2aSDenis V. Lunev         {
49e9e0bb2aSDenis V. Lunev             .name = "events",
50e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
51e9e0bb2aSDenis V. Lunev         },{
52e9e0bb2aSDenis V. Lunev             .name = "file",
53e9e0bb2aSDenis V. Lunev             .type = QEMU_OPT_STRING,
54e9e0bb2aSDenis V. Lunev         },
55e9e0bb2aSDenis V. Lunev         { /* end of list */ }
56e9e0bb2aSDenis V. Lunev     },
57e9e0bb2aSDenis V. Lunev };
58e9e0bb2aSDenis V. Lunev 
59e9e0bb2aSDenis V. Lunev 
60*fe4db84dSDaniel P. Berrange void trace_event_register_group(TraceEvent **events)
61*fe4db84dSDaniel P. Berrange {
62*fe4db84dSDaniel P. Berrange     event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
63*fe4db84dSDaniel P. Berrange     event_groups[nevent_groups].events = events;
64*fe4db84dSDaniel P. Berrange     nevent_groups++;
65*fe4db84dSDaniel P. Berrange }
66*fe4db84dSDaniel P. Berrange 
67*fe4db84dSDaniel P. Berrange 
68b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name)
69b1bae816SLluís Vilanova {
70b1bae816SLluís Vilanova     assert(name != NULL);
71b1bae816SLluís Vilanova 
720d4e995cSDaniel P. Berrange     TraceEventIter iter;
730d4e995cSDaniel P. Berrange     TraceEvent *ev;
740d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, NULL);
750d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
76b1bae816SLluís Vilanova         if (strcmp(trace_event_get_name(ev), name) == 0) {
77b1bae816SLluís Vilanova             return ev;
78b1bae816SLluís Vilanova         }
79b1bae816SLluís Vilanova     }
80b1bae816SLluís Vilanova     return NULL;
81b1bae816SLluís Vilanova }
82b1bae816SLluís Vilanova 
83b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev)
84b1bae816SLluís Vilanova {
85b1bae816SLluís Vilanova     while (*pat != '\0' && *ev != '\0') {
86b1bae816SLluís Vilanova         if (*pat == *ev) {
87b1bae816SLluís Vilanova             pat++;
88b1bae816SLluís Vilanova             ev++;
89b1bae816SLluís Vilanova         }
90b1bae816SLluís Vilanova         else if (*pat == '*') {
91b1bae816SLluís Vilanova             if (pattern_glob(pat, ev+1)) {
92b1bae816SLluís Vilanova                 return true;
93b1bae816SLluís Vilanova             } else if (pattern_glob(pat+1, ev)) {
94b1bae816SLluís Vilanova                 return true;
95b1bae816SLluís Vilanova             } else {
96b1bae816SLluís Vilanova                 return false;
97b1bae816SLluís Vilanova             }
98b1bae816SLluís Vilanova         } else {
99b1bae816SLluís Vilanova             return false;
100b1bae816SLluís Vilanova         }
101b1bae816SLluís Vilanova     }
102b1bae816SLluís Vilanova 
103b1bae816SLluís Vilanova     while (*pat == '*') {
104b1bae816SLluís Vilanova         pat++;
105b1bae816SLluís Vilanova     }
106b1bae816SLluís Vilanova 
107b1bae816SLluís Vilanova     if (*pat == '\0' && *ev == '\0') {
108b1bae816SLluís Vilanova         return true;
109b1bae816SLluís Vilanova     } else {
110b1bae816SLluís Vilanova         return false;
111b1bae816SLluís Vilanova     }
112b1bae816SLluís Vilanova }
113b1bae816SLluís Vilanova 
114b1bae816SLluís Vilanova 
1156a1b0f3aSDaniel P. Berrange void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
1166a1b0f3aSDaniel P. Berrange {
1176a1b0f3aSDaniel P. Berrange     iter->event = 0;
118*fe4db84dSDaniel P. Berrange     iter->group = 0;
1196a1b0f3aSDaniel P. Berrange     iter->pattern = pattern;
1206a1b0f3aSDaniel P. Berrange }
1216a1b0f3aSDaniel P. Berrange 
1226a1b0f3aSDaniel P. Berrange TraceEvent *trace_event_iter_next(TraceEventIter *iter)
1236a1b0f3aSDaniel P. Berrange {
124*fe4db84dSDaniel P. Berrange     while (iter->group < nevent_groups &&
125*fe4db84dSDaniel P. Berrange            event_groups[iter->group].events[iter->event] != NULL) {
126*fe4db84dSDaniel P. Berrange         TraceEvent *ev = event_groups[iter->group].events[iter->event];
1276a1b0f3aSDaniel P. Berrange         iter->event++;
128*fe4db84dSDaniel P. Berrange         if (event_groups[iter->group].events[iter->event] == NULL) {
129*fe4db84dSDaniel P. Berrange             iter->event = 0;
130*fe4db84dSDaniel P. Berrange             iter->group++;
131*fe4db84dSDaniel P. Berrange         }
1326a1b0f3aSDaniel P. Berrange         if (!iter->pattern ||
1336a1b0f3aSDaniel P. Berrange             pattern_glob(iter->pattern,
1346a1b0f3aSDaniel P. Berrange                          trace_event_get_name(ev))) {
1356a1b0f3aSDaniel P. Berrange             return ev;
1366a1b0f3aSDaniel P. Berrange         }
1376a1b0f3aSDaniel P. Berrange     }
1386a1b0f3aSDaniel P. Berrange 
1396a1b0f3aSDaniel P. Berrange     return NULL;
1406a1b0f3aSDaniel P. Berrange }
1416a1b0f3aSDaniel P. Berrange 
142e9527dd3SPaolo Bonzini void trace_list_events(void)
143e9527dd3SPaolo Bonzini {
1440d4e995cSDaniel P. Berrange     TraceEventIter iter;
1450d4e995cSDaniel P. Berrange     TraceEvent *ev;
1460d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, NULL);
1470d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1480d4e995cSDaniel P. Berrange         fprintf(stderr, "%s\n", trace_event_get_name(ev));
149e9527dd3SPaolo Bonzini     }
150e9527dd3SPaolo Bonzini }
151e9527dd3SPaolo Bonzini 
152e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf)
15310578a25SPaolo Bonzini {
15410578a25SPaolo Bonzini     const bool enable = ('-' != line_buf[0]);
15510578a25SPaolo Bonzini     const char *line_ptr = enable ? line_buf : line_buf + 1;
1560d4e995cSDaniel P. Berrange     TraceEventIter iter;
1570d4e995cSDaniel P. Berrange     TraceEvent *ev;
1580d4e995cSDaniel P. Berrange     bool is_pattern = trace_event_is_pattern(line_ptr);
15910578a25SPaolo Bonzini 
1600d4e995cSDaniel P. Berrange     trace_event_iter_init(&iter, line_ptr);
1610d4e995cSDaniel P. Berrange     while ((ev = trace_event_iter_next(&iter)) != NULL) {
1620d4e995cSDaniel P. Berrange         if (!trace_event_get_state_static(ev)) {
1630d4e995cSDaniel P. Berrange             if (!is_pattern) {
16410578a25SPaolo Bonzini                 error_report("WARNING: trace event '%s' is not traceable",
16510578a25SPaolo Bonzini                              line_ptr);
1660d4e995cSDaniel P. Berrange                 return;
16710578a25SPaolo Bonzini             }
1680d4e995cSDaniel P. Berrange             continue;
1690d4e995cSDaniel P. Berrange         }
1700d4e995cSDaniel P. Berrange 
1710d4e995cSDaniel P. Berrange         /* start tracing */
1720d4e995cSDaniel P. Berrange         trace_event_set_state_dynamic(ev, enable);
1730d4e995cSDaniel P. Berrange         if (!is_pattern) {
1740d4e995cSDaniel P. Berrange             return;
1750d4e995cSDaniel P. Berrange         }
1760d4e995cSDaniel P. Berrange     }
1770d4e995cSDaniel P. Berrange 
1780d4e995cSDaniel P. Berrange     if (!is_pattern) {
1790d4e995cSDaniel P. Berrange         error_report("WARNING: trace event '%s' does not exist",
1800d4e995cSDaniel P. Berrange                      line_ptr);
18110578a25SPaolo Bonzini     }
18210578a25SPaolo Bonzini }
18310578a25SPaolo Bonzini 
184e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf)
185e9527dd3SPaolo Bonzini {
186e9527dd3SPaolo Bonzini     if (is_help_option(line_buf)) {
187e9527dd3SPaolo Bonzini         trace_list_events();
188acc6809dSDenis V. Lunev         if (cur_mon == NULL) {
189e9527dd3SPaolo Bonzini             exit(0);
190acc6809dSDenis V. Lunev         }
191e9527dd3SPaolo Bonzini     } else {
192e9527dd3SPaolo Bonzini         do_trace_enable_events(line_buf);
193e9527dd3SPaolo Bonzini     }
194e9527dd3SPaolo Bonzini }
195e9527dd3SPaolo Bonzini 
196e9e0bb2aSDenis V. Lunev static void trace_init_events(const char *fname)
19723d15e86SLluís {
198a35d9be6SAlexey Kardashevskiy     Location loc;
199a35d9be6SAlexey Kardashevskiy     FILE *fp;
200a35d9be6SAlexey Kardashevskiy     char line_buf[1024];
201a35d9be6SAlexey Kardashevskiy     size_t line_idx = 0;
202a35d9be6SAlexey Kardashevskiy 
20323d15e86SLluís     if (fname == NULL) {
20423d15e86SLluís         return;
20523d15e86SLluís     }
20623d15e86SLluís 
207a35d9be6SAlexey Kardashevskiy     loc_push_none(&loc);
208a35d9be6SAlexey Kardashevskiy     loc_set_file(fname, 0);
209a35d9be6SAlexey Kardashevskiy     fp = fopen(fname, "r");
21023d15e86SLluís     if (!fp) {
211a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
21223d15e86SLluís         exit(1);
21323d15e86SLluís     }
21423d15e86SLluís     while (fgets(line_buf, sizeof(line_buf), fp)) {
215a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, ++line_idx);
21623d15e86SLluís         size_t len = strlen(line_buf);
21723d15e86SLluís         if (len > 1) {              /* skip empty lines */
21823d15e86SLluís             line_buf[len - 1] = '\0';
219794b1f96SAlexey Kardashevskiy             if ('#' == line_buf[0]) { /* skip commented lines */
220794b1f96SAlexey Kardashevskiy                 continue;
221794b1f96SAlexey Kardashevskiy             }
22210578a25SPaolo Bonzini             trace_enable_events(line_buf);
22323d15e86SLluís         }
22482432638SAlexey Kardashevskiy     }
22523d15e86SLluís     if (fclose(fp) != 0) {
226a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, 0);
227a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
22823d15e86SLluís         exit(1);
22923d15e86SLluís     }
230a35d9be6SAlexey Kardashevskiy     loc_pop(&loc);
23123d15e86SLluís }
2325b808275SLluís Vilanova 
23341fc57e4SPaolo Bonzini void trace_init_file(const char *file)
2345b808275SLluís Vilanova {
2355b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
23641fc57e4SPaolo Bonzini     st_set_trace_file(file);
237ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG
238ed7f5f1dSPaolo Bonzini     /* If both the simple and the log backends are enabled, "-trace file"
239ed7f5f1dSPaolo Bonzini      * only applies to the simple backend; use "-D" for the log backend.
240ed7f5f1dSPaolo Bonzini      */
241ed7f5f1dSPaolo Bonzini     if (file) {
242daa76aa4SMarkus Armbruster         qemu_set_log_filename(file, &error_fatal);
243ed7f5f1dSPaolo Bonzini     }
2445b808275SLluís Vilanova #else
2455b808275SLluís Vilanova     if (file) {
2465b808275SLluís Vilanova         fprintf(stderr, "error: -trace file=...: "
2475b808275SLluís Vilanova                 "option not supported by the selected tracing backends\n");
24841fc57e4SPaolo Bonzini         exit(1);
24941fc57e4SPaolo Bonzini     }
25041fc57e4SPaolo Bonzini #endif
25141fc57e4SPaolo Bonzini }
25241fc57e4SPaolo Bonzini 
25341fc57e4SPaolo Bonzini bool trace_init_backends(void)
25441fc57e4SPaolo Bonzini {
25541fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE
25641fc57e4SPaolo Bonzini     if (!st_init()) {
25741fc57e4SPaolo Bonzini         fprintf(stderr, "failed to initialize simple tracing backend.\n");
2585b808275SLluís Vilanova         return false;
2595b808275SLluís Vilanova     }
2605b808275SLluís Vilanova #endif
2615b808275SLluís Vilanova 
2625b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
2635b808275SLluís Vilanova     if (!ftrace_init()) {
2645b808275SLluís Vilanova         fprintf(stderr, "failed to initialize ftrace backend.\n");
2655b808275SLluís Vilanova         return false;
2665b808275SLluís Vilanova     }
2675b808275SLluís Vilanova #endif
2685b808275SLluís Vilanova 
2690a852417SPaul Durrant #ifdef CONFIG_TRACE_SYSLOG
2700a852417SPaul Durrant     openlog(NULL, LOG_PID, LOG_DAEMON);
2710a852417SPaul Durrant #endif
2720a852417SPaul Durrant 
2735b808275SLluís Vilanova     return true;
2745b808275SLluís Vilanova }
275e9e0bb2aSDenis V. Lunev 
276e9e0bb2aSDenis V. Lunev char *trace_opt_parse(const char *optarg)
277e9e0bb2aSDenis V. Lunev {
278e9e0bb2aSDenis V. Lunev     char *trace_file;
279e9e0bb2aSDenis V. Lunev     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
280e9e0bb2aSDenis V. Lunev                                              optarg, true);
281e9e0bb2aSDenis V. Lunev     if (!opts) {
282e9e0bb2aSDenis V. Lunev         exit(1);
283e9e0bb2aSDenis V. Lunev     }
284e9e0bb2aSDenis V. Lunev     if (qemu_opt_get(opts, "enable")) {
285e9e0bb2aSDenis V. Lunev         trace_enable_events(qemu_opt_get(opts, "enable"));
286e9e0bb2aSDenis V. Lunev     }
287e9e0bb2aSDenis V. Lunev     trace_init_events(qemu_opt_get(opts, "events"));
288e9e0bb2aSDenis V. Lunev     trace_file = g_strdup(qemu_opt_get(opts, "file"));
289e9e0bb2aSDenis V. Lunev     qemu_opts_del(opts);
290e9e0bb2aSDenis V. Lunev 
291e9e0bb2aSDenis V. Lunev     return trace_file;
292e9e0bb2aSDenis V. Lunev }
293