2 * Interface for configuring and controlling the state of tracing events.
4 * Copyright (C) 2011-2014 LluĂs Vilanova <vilanova@ac.upc.edu>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "trace/control.h"
12 #ifdef CONFIG_TRACE_SIMPLE
13 #include "trace/simple.h"
15 #ifdef CONFIG_TRACE_FTRACE
16 #include "trace/ftrace.h"
18 #ifdef CONFIG_TRACE_LOG
21 #include "qemu/error-report.h"
23 int trace_events_enabled_count
;
24 bool trace_events_dstate
[TRACE_EVENT_COUNT
];
26 TraceEvent
*trace_event_name(const char *name
)
31 for (i
= 0; i
< trace_event_count(); i
++) {
32 TraceEvent
*ev
= trace_event_id(i
);
33 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
40 static bool pattern_glob(const char *pat
, const char *ev
)
42 while (*pat
!= '\0' && *ev
!= '\0') {
47 else if (*pat
== '*') {
48 if (pattern_glob(pat
, ev
+1)) {
50 } else if (pattern_glob(pat
+1, ev
)) {
64 if (*pat
== '\0' && *ev
== '\0') {
71 TraceEvent
*trace_event_pattern(const char *pat
, TraceEvent
*ev
)
80 i
= trace_event_get_id(ev
);
84 while (i
< trace_event_count()) {
85 TraceEvent
*res
= trace_event_id(i
);
86 if (pattern_glob(pat
, trace_event_get_name(res
))) {
95 void trace_list_events(void)
98 for (i
= 0; i
< trace_event_count(); i
++) {
99 TraceEvent
*res
= trace_event_id(i
);
100 fprintf(stderr
, "%s\n", trace_event_get_name(res
));
104 static void do_trace_enable_events(const char *line_buf
)
106 const bool enable
= ('-' != line_buf
[0]);
107 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
109 if (trace_event_is_pattern(line_ptr
)) {
110 TraceEvent
*ev
= NULL
;
111 while ((ev
= trace_event_pattern(line_ptr
, ev
)) != NULL
) {
112 if (trace_event_get_state_static(ev
)) {
113 trace_event_set_state_dynamic(ev
, enable
);
117 TraceEvent
*ev
= trace_event_name(line_ptr
);
119 error_report("WARNING: trace event '%s' does not exist",
121 } else if (!trace_event_get_state_static(ev
)) {
122 error_report("WARNING: trace event '%s' is not traceable",
125 trace_event_set_state_dynamic(ev
, enable
);
130 void trace_enable_events(const char *line_buf
)
132 if (is_help_option(line_buf
)) {
136 do_trace_enable_events(line_buf
);
140 void trace_init_events(const char *fname
)
152 loc_set_file(fname
, 0);
153 fp
= fopen(fname
, "r");
155 error_report("%s", strerror(errno
));
158 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
159 loc_set_file(fname
, ++line_idx
);
160 size_t len
= strlen(line_buf
);
161 if (len
> 1) { /* skip empty lines */
162 line_buf
[len
- 1] = '\0';
163 if ('#' == line_buf
[0]) { /* skip commented lines */
166 trace_enable_events(line_buf
);
169 if (fclose(fp
) != 0) {
170 loc_set_file(fname
, 0);
171 error_report("%s", strerror(errno
));
177 void trace_init_file(const char *file
)
179 #ifdef CONFIG_TRACE_SIMPLE
180 st_set_trace_file(file
);
181 #elif defined CONFIG_TRACE_LOG
182 /* If both the simple and the log backends are enabled, "-trace file"
183 * only applies to the simple backend; use "-D" for the log backend.
186 qemu_set_log_filename(file
);
190 fprintf(stderr
, "error: -trace file=...: "
191 "option not supported by the selected tracing backends\n");
197 bool trace_init_backends(void)
199 #ifdef CONFIG_TRACE_SIMPLE
201 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
206 #ifdef CONFIG_TRACE_FTRACE
207 if (!ftrace_init()) {
208 fprintf(stderr
, "failed to initialize ftrace backend.\n");