xref: /openbmc/qemu/trace/control.c (revision ed7f5f1d)
123d15e86SLluís /*
223d15e86SLluís  * Interface for configuring and controlling the state of tracing events.
323d15e86SLluís  *
45b808275SLluís Vilanova  * Copyright (C) 2011-2014 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 
1023d15e86SLluís #include "trace/control.h"
115b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
125b808275SLluís Vilanova #include "trace/simple.h"
135b808275SLluís Vilanova #endif
145b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
155b808275SLluís Vilanova #include "trace/ftrace.h"
165b808275SLluís Vilanova #endif
17*ed7f5f1dSPaolo Bonzini #ifdef CONFIG_TRACE_LOG
18*ed7f5f1dSPaolo Bonzini #include "qemu/log.h"
19*ed7f5f1dSPaolo Bonzini #endif
20a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h"
2123d15e86SLluís 
2243b48cfcSPaolo Bonzini int trace_events_enabled_count;
23585ec727SPaolo Bonzini bool trace_events_dstate[TRACE_EVENT_COUNT];
2443b48cfcSPaolo Bonzini 
25b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name)
26b1bae816SLluís Vilanova {
27b1bae816SLluís Vilanova     assert(name != NULL);
28b1bae816SLluís Vilanova 
29b1bae816SLluís Vilanova     TraceEventID i;
30b1bae816SLluís Vilanova     for (i = 0; i < trace_event_count(); i++) {
31b1bae816SLluís Vilanova         TraceEvent *ev = trace_event_id(i);
32b1bae816SLluís Vilanova         if (strcmp(trace_event_get_name(ev), name) == 0) {
33b1bae816SLluís Vilanova             return ev;
34b1bae816SLluís Vilanova         }
35b1bae816SLluís Vilanova     }
36b1bae816SLluís Vilanova     return NULL;
37b1bae816SLluís Vilanova }
38b1bae816SLluís Vilanova 
39b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev)
40b1bae816SLluís Vilanova {
41b1bae816SLluís Vilanova     while (*pat != '\0' && *ev != '\0') {
42b1bae816SLluís Vilanova         if (*pat == *ev) {
43b1bae816SLluís Vilanova             pat++;
44b1bae816SLluís Vilanova             ev++;
45b1bae816SLluís Vilanova         }
46b1bae816SLluís Vilanova         else if (*pat == '*') {
47b1bae816SLluís Vilanova             if (pattern_glob(pat, ev+1)) {
48b1bae816SLluís Vilanova                 return true;
49b1bae816SLluís Vilanova             } else if (pattern_glob(pat+1, ev)) {
50b1bae816SLluís Vilanova                 return true;
51b1bae816SLluís Vilanova             } else {
52b1bae816SLluís Vilanova                 return false;
53b1bae816SLluís Vilanova             }
54b1bae816SLluís Vilanova         } else {
55b1bae816SLluís Vilanova             return false;
56b1bae816SLluís Vilanova         }
57b1bae816SLluís Vilanova     }
58b1bae816SLluís Vilanova 
59b1bae816SLluís Vilanova     while (*pat == '*') {
60b1bae816SLluís Vilanova         pat++;
61b1bae816SLluís Vilanova     }
62b1bae816SLluís Vilanova 
63b1bae816SLluís Vilanova     if (*pat == '\0' && *ev == '\0') {
64b1bae816SLluís Vilanova         return true;
65b1bae816SLluís Vilanova     } else {
66b1bae816SLluís Vilanova         return false;
67b1bae816SLluís Vilanova     }
68b1bae816SLluís Vilanova }
69b1bae816SLluís Vilanova 
70b1bae816SLluís Vilanova TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
71b1bae816SLluís Vilanova {
72b1bae816SLluís Vilanova     assert(pat != NULL);
73b1bae816SLluís Vilanova 
74b1bae816SLluís Vilanova     TraceEventID i;
75b1bae816SLluís Vilanova 
76b1bae816SLluís Vilanova     if (ev == NULL) {
77b1bae816SLluís Vilanova         i = -1;
78b1bae816SLluís Vilanova     } else {
79b1bae816SLluís Vilanova         i = trace_event_get_id(ev);
80b1bae816SLluís Vilanova     }
81b1bae816SLluís Vilanova     i++;
82b1bae816SLluís Vilanova 
83b1bae816SLluís Vilanova     while (i < trace_event_count()) {
84b1bae816SLluís Vilanova         TraceEvent *res = trace_event_id(i);
85b1bae816SLluís Vilanova         if (pattern_glob(pat, trace_event_get_name(res))) {
86b1bae816SLluís Vilanova             return res;
87b1bae816SLluís Vilanova         }
88b1bae816SLluís Vilanova         i++;
89b1bae816SLluís Vilanova     }
90b1bae816SLluís Vilanova 
91b1bae816SLluís Vilanova     return NULL;
92b1bae816SLluís Vilanova }
93b1bae816SLluís Vilanova 
94e9527dd3SPaolo Bonzini void trace_list_events(void)
95e9527dd3SPaolo Bonzini {
96e9527dd3SPaolo Bonzini     int i;
97e9527dd3SPaolo Bonzini     for (i = 0; i < trace_event_count(); i++) {
98e9527dd3SPaolo Bonzini         TraceEvent *res = trace_event_id(i);
99e9527dd3SPaolo Bonzini         fprintf(stderr, "%s\n", trace_event_get_name(res));
100e9527dd3SPaolo Bonzini     }
101e9527dd3SPaolo Bonzini }
102e9527dd3SPaolo Bonzini 
103e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf)
10410578a25SPaolo Bonzini {
10510578a25SPaolo Bonzini     const bool enable = ('-' != line_buf[0]);
10610578a25SPaolo Bonzini     const char *line_ptr = enable ? line_buf : line_buf + 1;
10710578a25SPaolo Bonzini 
10810578a25SPaolo Bonzini     if (trace_event_is_pattern(line_ptr)) {
10910578a25SPaolo Bonzini         TraceEvent *ev = NULL;
11010578a25SPaolo Bonzini         while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
11110578a25SPaolo Bonzini             if (trace_event_get_state_static(ev)) {
11210578a25SPaolo Bonzini                 trace_event_set_state_dynamic(ev, enable);
11310578a25SPaolo Bonzini             }
11410578a25SPaolo Bonzini         }
11510578a25SPaolo Bonzini     } else {
11610578a25SPaolo Bonzini         TraceEvent *ev = trace_event_name(line_ptr);
11710578a25SPaolo Bonzini         if (ev == NULL) {
11810578a25SPaolo Bonzini             error_report("WARNING: trace event '%s' does not exist",
11910578a25SPaolo Bonzini                          line_ptr);
12010578a25SPaolo Bonzini         } else if (!trace_event_get_state_static(ev)) {
12110578a25SPaolo Bonzini             error_report("WARNING: trace event '%s' is not traceable",
12210578a25SPaolo Bonzini                          line_ptr);
12310578a25SPaolo Bonzini         } else {
12410578a25SPaolo Bonzini             trace_event_set_state_dynamic(ev, enable);
12510578a25SPaolo Bonzini         }
12610578a25SPaolo Bonzini     }
12710578a25SPaolo Bonzini }
12810578a25SPaolo Bonzini 
129e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf)
130e9527dd3SPaolo Bonzini {
131e9527dd3SPaolo Bonzini     if (is_help_option(line_buf)) {
132e9527dd3SPaolo Bonzini         trace_list_events();
133e9527dd3SPaolo Bonzini         exit(0);
134e9527dd3SPaolo Bonzini     } else {
135e9527dd3SPaolo Bonzini         do_trace_enable_events(line_buf);
136e9527dd3SPaolo Bonzini     }
137e9527dd3SPaolo Bonzini }
138e9527dd3SPaolo Bonzini 
13945bd0b41SPaolo Bonzini void trace_init_events(const char *fname)
14023d15e86SLluís {
141a35d9be6SAlexey Kardashevskiy     Location loc;
142a35d9be6SAlexey Kardashevskiy     FILE *fp;
143a35d9be6SAlexey Kardashevskiy     char line_buf[1024];
144a35d9be6SAlexey Kardashevskiy     size_t line_idx = 0;
145a35d9be6SAlexey Kardashevskiy 
14623d15e86SLluís     if (fname == NULL) {
14723d15e86SLluís         return;
14823d15e86SLluís     }
14923d15e86SLluís 
150a35d9be6SAlexey Kardashevskiy     loc_push_none(&loc);
151a35d9be6SAlexey Kardashevskiy     loc_set_file(fname, 0);
152a35d9be6SAlexey Kardashevskiy     fp = fopen(fname, "r");
15323d15e86SLluís     if (!fp) {
154a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
15523d15e86SLluís         exit(1);
15623d15e86SLluís     }
15723d15e86SLluís     while (fgets(line_buf, sizeof(line_buf), fp)) {
158a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, ++line_idx);
15923d15e86SLluís         size_t len = strlen(line_buf);
16023d15e86SLluís         if (len > 1) {              /* skip empty lines */
16123d15e86SLluís             line_buf[len - 1] = '\0';
162794b1f96SAlexey Kardashevskiy             if ('#' == line_buf[0]) { /* skip commented lines */
163794b1f96SAlexey Kardashevskiy                 continue;
164794b1f96SAlexey Kardashevskiy             }
16510578a25SPaolo Bonzini             trace_enable_events(line_buf);
16623d15e86SLluís         }
16782432638SAlexey Kardashevskiy     }
16823d15e86SLluís     if (fclose(fp) != 0) {
169a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, 0);
170a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
17123d15e86SLluís         exit(1);
17223d15e86SLluís     }
173a35d9be6SAlexey Kardashevskiy     loc_pop(&loc);
17423d15e86SLluís }
1755b808275SLluís Vilanova 
17641fc57e4SPaolo Bonzini void trace_init_file(const char *file)
1775b808275SLluís Vilanova {
1785b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
17941fc57e4SPaolo Bonzini     st_set_trace_file(file);
180*ed7f5f1dSPaolo Bonzini #elif defined CONFIG_TRACE_LOG
181*ed7f5f1dSPaolo Bonzini     /* If both the simple and the log backends are enabled, "-trace file"
182*ed7f5f1dSPaolo Bonzini      * only applies to the simple backend; use "-D" for the log backend.
183*ed7f5f1dSPaolo Bonzini      */
184*ed7f5f1dSPaolo Bonzini     if (file) {
185*ed7f5f1dSPaolo Bonzini         qemu_set_log_filename(file);
186*ed7f5f1dSPaolo Bonzini     }
1875b808275SLluís Vilanova #else
1885b808275SLluís Vilanova     if (file) {
1895b808275SLluís Vilanova         fprintf(stderr, "error: -trace file=...: "
1905b808275SLluís Vilanova                 "option not supported by the selected tracing backends\n");
19141fc57e4SPaolo Bonzini         exit(1);
19241fc57e4SPaolo Bonzini     }
19341fc57e4SPaolo Bonzini #endif
19441fc57e4SPaolo Bonzini }
19541fc57e4SPaolo Bonzini 
19641fc57e4SPaolo Bonzini bool trace_init_backends(void)
19741fc57e4SPaolo Bonzini {
19841fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE
19941fc57e4SPaolo Bonzini     if (!st_init()) {
20041fc57e4SPaolo Bonzini         fprintf(stderr, "failed to initialize simple tracing backend.\n");
2015b808275SLluís Vilanova         return false;
2025b808275SLluís Vilanova     }
2035b808275SLluís Vilanova #endif
2045b808275SLluís Vilanova 
2055b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
2065b808275SLluís Vilanova     if (!ftrace_init()) {
2075b808275SLluís Vilanova         fprintf(stderr, "failed to initialize ftrace backend.\n");
2085b808275SLluís Vilanova         return false;
2095b808275SLluís Vilanova     }
2105b808275SLluís Vilanova #endif
2115b808275SLluís Vilanova 
2125b808275SLluís Vilanova     return true;
2135b808275SLluís Vilanova }
214