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 #include "qemu/help_option.h"
13 #ifdef CONFIG_TRACE_SIMPLE
14 #include "trace/simple.h"
16 #ifdef CONFIG_TRACE_FTRACE
17 #include "trace/ftrace.h"
19 #ifdef CONFIG_TRACE_LOG
22 #include "qemu/error-report.h"
24 int trace_events_enabled_count
;
25 bool trace_events_dstate
[TRACE_EVENT_COUNT
];
27 TraceEvent
*trace_event_name(const char *name
)
32 for (i
= 0; i
< trace_event_count(); i
++) {
33 TraceEvent
*ev
= trace_event_id(i
);
34 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
41 static bool pattern_glob(const char *pat
, const char *ev
)
43 while (*pat
!= '\0' && *ev
!= '\0') {
48 else if (*pat
== '*') {
49 if (pattern_glob(pat
, ev
+1)) {
51 } else if (pattern_glob(pat
+1, ev
)) {
65 if (*pat
== '\0' && *ev
== '\0') {
72 TraceEvent
*trace_event_pattern(const char *pat
, TraceEvent
*ev
)
81 i
= trace_event_get_id(ev
);
85 while (i
< trace_event_count()) {
86 TraceEvent
*res
= trace_event_id(i
);
87 if (pattern_glob(pat
, trace_event_get_name(res
))) {
96 void trace_list_events(void)
99 for (i
= 0; i
< trace_event_count(); i
++) {
100 TraceEvent
*res
= trace_event_id(i
);
101 fprintf(stderr
, "%s\n", trace_event_get_name(res
));
105 static void do_trace_enable_events(const char *line_buf
)
107 const bool enable
= ('-' != line_buf
[0]);
108 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
110 if (trace_event_is_pattern(line_ptr
)) {
111 TraceEvent
*ev
= NULL
;
112 while ((ev
= trace_event_pattern(line_ptr
, ev
)) != NULL
) {
113 if (trace_event_get_state_static(ev
)) {
114 trace_event_set_state_dynamic(ev
, enable
);
118 TraceEvent
*ev
= trace_event_name(line_ptr
);
120 error_report("WARNING: trace event '%s' does not exist",
122 } else if (!trace_event_get_state_static(ev
)) {
123 error_report("WARNING: trace event '%s' is not traceable",
126 trace_event_set_state_dynamic(ev
, enable
);
131 void trace_enable_events(const char *line_buf
)
133 if (is_help_option(line_buf
)) {
137 do_trace_enable_events(line_buf
);
141 void trace_init_events(const char *fname
)
153 loc_set_file(fname
, 0);
154 fp
= fopen(fname
, "r");
156 error_report("%s", strerror(errno
));
159 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
160 loc_set_file(fname
, ++line_idx
);
161 size_t len
= strlen(line_buf
);
162 if (len
> 1) { /* skip empty lines */
163 line_buf
[len
- 1] = '\0';
164 if ('#' == line_buf
[0]) { /* skip commented lines */
167 trace_enable_events(line_buf
);
170 if (fclose(fp
) != 0) {
171 loc_set_file(fname
, 0);
172 error_report("%s", strerror(errno
));
178 void trace_init_file(const char *file
)
180 #ifdef CONFIG_TRACE_SIMPLE
181 st_set_trace_file(file
);
182 #elif defined CONFIG_TRACE_LOG
183 /* If both the simple and the log backends are enabled, "-trace file"
184 * only applies to the simple backend; use "-D" for the log backend.
187 qemu_set_log_filename(file
);
191 fprintf(stderr
, "error: -trace file=...: "
192 "option not supported by the selected tracing backends\n");
198 bool trace_init_backends(void)
200 #ifdef CONFIG_TRACE_SIMPLE
202 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
207 #ifdef CONFIG_TRACE_FTRACE
208 if (!ftrace_init()) {
209 fprintf(stderr
, "failed to initialize ftrace backend.\n");