xref: /openbmc/qemu/trace/control.c (revision e9527dd3)
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
17a35d9be6SAlexey Kardashevskiy #include "qemu/error-report.h"
1823d15e86SLluís 
1943b48cfcSPaolo Bonzini int trace_events_enabled_count;
20585ec727SPaolo Bonzini bool trace_events_dstate[TRACE_EVENT_COUNT];
2143b48cfcSPaolo Bonzini 
22b1bae816SLluís Vilanova TraceEvent *trace_event_name(const char *name)
23b1bae816SLluís Vilanova {
24b1bae816SLluís Vilanova     assert(name != NULL);
25b1bae816SLluís Vilanova 
26b1bae816SLluís Vilanova     TraceEventID i;
27b1bae816SLluís Vilanova     for (i = 0; i < trace_event_count(); i++) {
28b1bae816SLluís Vilanova         TraceEvent *ev = trace_event_id(i);
29b1bae816SLluís Vilanova         if (strcmp(trace_event_get_name(ev), name) == 0) {
30b1bae816SLluís Vilanova             return ev;
31b1bae816SLluís Vilanova         }
32b1bae816SLluís Vilanova     }
33b1bae816SLluís Vilanova     return NULL;
34b1bae816SLluís Vilanova }
35b1bae816SLluís Vilanova 
36b1bae816SLluís Vilanova static bool pattern_glob(const char *pat, const char *ev)
37b1bae816SLluís Vilanova {
38b1bae816SLluís Vilanova     while (*pat != '\0' && *ev != '\0') {
39b1bae816SLluís Vilanova         if (*pat == *ev) {
40b1bae816SLluís Vilanova             pat++;
41b1bae816SLluís Vilanova             ev++;
42b1bae816SLluís Vilanova         }
43b1bae816SLluís Vilanova         else if (*pat == '*') {
44b1bae816SLluís Vilanova             if (pattern_glob(pat, ev+1)) {
45b1bae816SLluís Vilanova                 return true;
46b1bae816SLluís Vilanova             } else if (pattern_glob(pat+1, ev)) {
47b1bae816SLluís Vilanova                 return true;
48b1bae816SLluís Vilanova             } else {
49b1bae816SLluís Vilanova                 return false;
50b1bae816SLluís Vilanova             }
51b1bae816SLluís Vilanova         } else {
52b1bae816SLluís Vilanova             return false;
53b1bae816SLluís Vilanova         }
54b1bae816SLluís Vilanova     }
55b1bae816SLluís Vilanova 
56b1bae816SLluís Vilanova     while (*pat == '*') {
57b1bae816SLluís Vilanova         pat++;
58b1bae816SLluís Vilanova     }
59b1bae816SLluís Vilanova 
60b1bae816SLluís Vilanova     if (*pat == '\0' && *ev == '\0') {
61b1bae816SLluís Vilanova         return true;
62b1bae816SLluís Vilanova     } else {
63b1bae816SLluís Vilanova         return false;
64b1bae816SLluís Vilanova     }
65b1bae816SLluís Vilanova }
66b1bae816SLluís Vilanova 
67b1bae816SLluís Vilanova TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
68b1bae816SLluís Vilanova {
69b1bae816SLluís Vilanova     assert(pat != NULL);
70b1bae816SLluís Vilanova 
71b1bae816SLluís Vilanova     TraceEventID i;
72b1bae816SLluís Vilanova 
73b1bae816SLluís Vilanova     if (ev == NULL) {
74b1bae816SLluís Vilanova         i = -1;
75b1bae816SLluís Vilanova     } else {
76b1bae816SLluís Vilanova         i = trace_event_get_id(ev);
77b1bae816SLluís Vilanova     }
78b1bae816SLluís Vilanova     i++;
79b1bae816SLluís Vilanova 
80b1bae816SLluís Vilanova     while (i < trace_event_count()) {
81b1bae816SLluís Vilanova         TraceEvent *res = trace_event_id(i);
82b1bae816SLluís Vilanova         if (pattern_glob(pat, trace_event_get_name(res))) {
83b1bae816SLluís Vilanova             return res;
84b1bae816SLluís Vilanova         }
85b1bae816SLluís Vilanova         i++;
86b1bae816SLluís Vilanova     }
87b1bae816SLluís Vilanova 
88b1bae816SLluís Vilanova     return NULL;
89b1bae816SLluís Vilanova }
90b1bae816SLluís Vilanova 
91*e9527dd3SPaolo Bonzini void trace_list_events(void)
92*e9527dd3SPaolo Bonzini {
93*e9527dd3SPaolo Bonzini     int i;
94*e9527dd3SPaolo Bonzini     for (i = 0; i < trace_event_count(); i++) {
95*e9527dd3SPaolo Bonzini         TraceEvent *res = trace_event_id(i);
96*e9527dd3SPaolo Bonzini         fprintf(stderr, "%s\n", trace_event_get_name(res));
97*e9527dd3SPaolo Bonzini     }
98*e9527dd3SPaolo Bonzini }
99*e9527dd3SPaolo Bonzini 
100*e9527dd3SPaolo Bonzini static void do_trace_enable_events(const char *line_buf)
10110578a25SPaolo Bonzini {
10210578a25SPaolo Bonzini     const bool enable = ('-' != line_buf[0]);
10310578a25SPaolo Bonzini     const char *line_ptr = enable ? line_buf : line_buf + 1;
10410578a25SPaolo Bonzini 
10510578a25SPaolo Bonzini     if (trace_event_is_pattern(line_ptr)) {
10610578a25SPaolo Bonzini         TraceEvent *ev = NULL;
10710578a25SPaolo Bonzini         while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
10810578a25SPaolo Bonzini             if (trace_event_get_state_static(ev)) {
10910578a25SPaolo Bonzini                 trace_event_set_state_dynamic(ev, enable);
11010578a25SPaolo Bonzini             }
11110578a25SPaolo Bonzini         }
11210578a25SPaolo Bonzini     } else {
11310578a25SPaolo Bonzini         TraceEvent *ev = trace_event_name(line_ptr);
11410578a25SPaolo Bonzini         if (ev == NULL) {
11510578a25SPaolo Bonzini             error_report("WARNING: trace event '%s' does not exist",
11610578a25SPaolo Bonzini                          line_ptr);
11710578a25SPaolo Bonzini         } else if (!trace_event_get_state_static(ev)) {
11810578a25SPaolo Bonzini             error_report("WARNING: trace event '%s' is not traceable",
11910578a25SPaolo Bonzini                          line_ptr);
12010578a25SPaolo Bonzini         } else {
12110578a25SPaolo Bonzini             trace_event_set_state_dynamic(ev, enable);
12210578a25SPaolo Bonzini         }
12310578a25SPaolo Bonzini     }
12410578a25SPaolo Bonzini }
12510578a25SPaolo Bonzini 
126*e9527dd3SPaolo Bonzini void trace_enable_events(const char *line_buf)
127*e9527dd3SPaolo Bonzini {
128*e9527dd3SPaolo Bonzini     if (is_help_option(line_buf)) {
129*e9527dd3SPaolo Bonzini         trace_list_events();
130*e9527dd3SPaolo Bonzini         exit(0);
131*e9527dd3SPaolo Bonzini     } else {
132*e9527dd3SPaolo Bonzini         do_trace_enable_events(line_buf);
133*e9527dd3SPaolo Bonzini     }
134*e9527dd3SPaolo Bonzini }
135*e9527dd3SPaolo Bonzini 
13645bd0b41SPaolo Bonzini void trace_init_events(const char *fname)
13723d15e86SLluís {
138a35d9be6SAlexey Kardashevskiy     Location loc;
139a35d9be6SAlexey Kardashevskiy     FILE *fp;
140a35d9be6SAlexey Kardashevskiy     char line_buf[1024];
141a35d9be6SAlexey Kardashevskiy     size_t line_idx = 0;
142a35d9be6SAlexey Kardashevskiy 
14323d15e86SLluís     if (fname == NULL) {
14423d15e86SLluís         return;
14523d15e86SLluís     }
14623d15e86SLluís 
147a35d9be6SAlexey Kardashevskiy     loc_push_none(&loc);
148a35d9be6SAlexey Kardashevskiy     loc_set_file(fname, 0);
149a35d9be6SAlexey Kardashevskiy     fp = fopen(fname, "r");
15023d15e86SLluís     if (!fp) {
151a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
15223d15e86SLluís         exit(1);
15323d15e86SLluís     }
15423d15e86SLluís     while (fgets(line_buf, sizeof(line_buf), fp)) {
155a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, ++line_idx);
15623d15e86SLluís         size_t len = strlen(line_buf);
15723d15e86SLluís         if (len > 1) {              /* skip empty lines */
15823d15e86SLluís             line_buf[len - 1] = '\0';
159794b1f96SAlexey Kardashevskiy             if ('#' == line_buf[0]) { /* skip commented lines */
160794b1f96SAlexey Kardashevskiy                 continue;
161794b1f96SAlexey Kardashevskiy             }
16210578a25SPaolo Bonzini             trace_enable_events(line_buf);
16323d15e86SLluís         }
16482432638SAlexey Kardashevskiy     }
16523d15e86SLluís     if (fclose(fp) != 0) {
166a35d9be6SAlexey Kardashevskiy         loc_set_file(fname, 0);
167a35d9be6SAlexey Kardashevskiy         error_report("%s", strerror(errno));
16823d15e86SLluís         exit(1);
16923d15e86SLluís     }
170a35d9be6SAlexey Kardashevskiy     loc_pop(&loc);
17123d15e86SLluís }
1725b808275SLluís Vilanova 
17341fc57e4SPaolo Bonzini void trace_init_file(const char *file)
1745b808275SLluís Vilanova {
1755b808275SLluís Vilanova #ifdef CONFIG_TRACE_SIMPLE
17641fc57e4SPaolo Bonzini     st_set_trace_file(file);
1775b808275SLluís Vilanova #else
1785b808275SLluís Vilanova     if (file) {
1795b808275SLluís Vilanova         fprintf(stderr, "error: -trace file=...: "
1805b808275SLluís Vilanova                 "option not supported by the selected tracing backends\n");
18141fc57e4SPaolo Bonzini         exit(1);
18241fc57e4SPaolo Bonzini     }
18341fc57e4SPaolo Bonzini #endif
18441fc57e4SPaolo Bonzini }
18541fc57e4SPaolo Bonzini 
18641fc57e4SPaolo Bonzini bool trace_init_backends(void)
18741fc57e4SPaolo Bonzini {
18841fc57e4SPaolo Bonzini #ifdef CONFIG_TRACE_SIMPLE
18941fc57e4SPaolo Bonzini     if (!st_init()) {
19041fc57e4SPaolo Bonzini         fprintf(stderr, "failed to initialize simple tracing backend.\n");
1915b808275SLluís Vilanova         return false;
1925b808275SLluís Vilanova     }
1935b808275SLluís Vilanova #endif
1945b808275SLluís Vilanova 
1955b808275SLluís Vilanova #ifdef CONFIG_TRACE_FTRACE
1965b808275SLluís Vilanova     if (!ftrace_init()) {
1975b808275SLluís Vilanova         fprintf(stderr, "failed to initialize ftrace backend.\n");
1985b808275SLluís Vilanova         return false;
1995b808275SLluís Vilanova     }
2005b808275SLluís Vilanova #endif
2015b808275SLluís Vilanova 
2025b808275SLluís Vilanova     return true;
2035b808275SLluís Vilanova }
204