2 * Interface for configuring and controlling the state of tracing events.
4 * Copyright (C) 2011-2016 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 #ifdef CONFIG_TRACE_SYSLOG
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/config-file.h"
28 #include "monitor/monitor.h"
30 int trace_events_enabled_count
;
32 QemuOptsList qemu_trace_opts
= {
34 .implied_opt_name
= "enable",
35 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
39 .type
= QEMU_OPT_STRING
,
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_STRING
,
53 TraceEvent
*trace_event_name(const char *name
)
59 trace_event_iter_init(&iter
, NULL
);
60 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
61 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
68 static bool pattern_glob(const char *pat
, const char *ev
)
70 while (*pat
!= '\0' && *ev
!= '\0') {
75 else if (*pat
== '*') {
76 if (pattern_glob(pat
, ev
+1)) {
78 } else if (pattern_glob(pat
+1, ev
)) {
92 if (*pat
== '\0' && *ev
== '\0') {
100 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
103 iter
->pattern
= pattern
;
106 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
108 while (trace_events
[iter
->event
] != NULL
) {
109 TraceEvent
*ev
= trace_events
[iter
->event
];
111 if (!iter
->pattern
||
112 pattern_glob(iter
->pattern
,
113 trace_event_get_name(ev
))) {
121 void trace_list_events(void)
125 trace_event_iter_init(&iter
, NULL
);
126 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
127 fprintf(stderr
, "%s\n", trace_event_get_name(ev
));
131 static void do_trace_enable_events(const char *line_buf
)
133 const bool enable
= ('-' != line_buf
[0]);
134 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
137 bool is_pattern
= trace_event_is_pattern(line_ptr
);
139 trace_event_iter_init(&iter
, line_ptr
);
140 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
141 if (!trace_event_get_state_static(ev
)) {
143 error_report("WARNING: trace event '%s' is not traceable",
151 trace_event_set_state_dynamic(ev
, enable
);
158 error_report("WARNING: trace event '%s' does not exist",
163 void trace_enable_events(const char *line_buf
)
165 if (is_help_option(line_buf
)) {
167 if (cur_mon
== NULL
) {
171 do_trace_enable_events(line_buf
);
175 static void trace_init_events(const char *fname
)
187 loc_set_file(fname
, 0);
188 fp
= fopen(fname
, "r");
190 error_report("%s", strerror(errno
));
193 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
194 loc_set_file(fname
, ++line_idx
);
195 size_t len
= strlen(line_buf
);
196 if (len
> 1) { /* skip empty lines */
197 line_buf
[len
- 1] = '\0';
198 if ('#' == line_buf
[0]) { /* skip commented lines */
201 trace_enable_events(line_buf
);
204 if (fclose(fp
) != 0) {
205 loc_set_file(fname
, 0);
206 error_report("%s", strerror(errno
));
212 void trace_init_file(const char *file
)
214 #ifdef CONFIG_TRACE_SIMPLE
215 st_set_trace_file(file
);
216 #elif defined CONFIG_TRACE_LOG
217 /* If both the simple and the log backends are enabled, "-trace file"
218 * only applies to the simple backend; use "-D" for the log backend.
221 qemu_set_log_filename(file
, &error_fatal
);
225 fprintf(stderr
, "error: -trace file=...: "
226 "option not supported by the selected tracing backends\n");
232 bool trace_init_backends(void)
234 #ifdef CONFIG_TRACE_SIMPLE
236 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
241 #ifdef CONFIG_TRACE_FTRACE
242 if (!ftrace_init()) {
243 fprintf(stderr
, "failed to initialize ftrace backend.\n");
248 #ifdef CONFIG_TRACE_SYSLOG
249 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
255 char *trace_opt_parse(const char *optarg
)
258 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
263 if (qemu_opt_get(opts
, "enable")) {
264 trace_enable_events(qemu_opt_get(opts
, "enable"));
266 trace_init_events(qemu_opt_get(opts
, "events"));
267 trace_file
= g_strdup(qemu_opt_get(opts
, "file"));