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 typedef struct TraceEventGroup
{
36 static TraceEventGroup
*event_groups
;
37 static size_t nevent_groups
;
39 QemuOptsList qemu_trace_opts
= {
41 .implied_opt_name
= "enable",
42 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
46 .type
= QEMU_OPT_STRING
,
50 .type
= QEMU_OPT_STRING
,
53 .type
= QEMU_OPT_STRING
,
60 void trace_event_register_group(TraceEvent
**events
)
62 event_groups
= g_renew(TraceEventGroup
, event_groups
, nevent_groups
+ 1);
63 event_groups
[nevent_groups
].events
= events
;
68 TraceEvent
*trace_event_name(const char *name
)
74 trace_event_iter_init(&iter
, NULL
);
75 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
76 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
83 static bool pattern_glob(const char *pat
, const char *ev
)
85 while (*pat
!= '\0' && *ev
!= '\0') {
90 else if (*pat
== '*') {
91 if (pattern_glob(pat
, ev
+1)) {
93 } else if (pattern_glob(pat
+1, ev
)) {
103 while (*pat
== '*') {
107 if (*pat
== '\0' && *ev
== '\0') {
115 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
119 iter
->pattern
= pattern
;
122 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
124 while (iter
->group
< nevent_groups
&&
125 event_groups
[iter
->group
].events
[iter
->event
] != NULL
) {
126 TraceEvent
*ev
= event_groups
[iter
->group
].events
[iter
->event
];
128 if (event_groups
[iter
->group
].events
[iter
->event
] == NULL
) {
132 if (!iter
->pattern
||
133 pattern_glob(iter
->pattern
,
134 trace_event_get_name(ev
))) {
142 void trace_list_events(void)
146 trace_event_iter_init(&iter
, NULL
);
147 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
148 fprintf(stderr
, "%s\n", trace_event_get_name(ev
));
152 static void do_trace_enable_events(const char *line_buf
)
154 const bool enable
= ('-' != line_buf
[0]);
155 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
158 bool is_pattern
= trace_event_is_pattern(line_ptr
);
160 trace_event_iter_init(&iter
, line_ptr
);
161 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
162 if (!trace_event_get_state_static(ev
)) {
164 error_report("WARNING: trace event '%s' is not traceable",
172 trace_event_set_state_dynamic(ev
, enable
);
179 error_report("WARNING: trace event '%s' does not exist",
184 void trace_enable_events(const char *line_buf
)
186 if (is_help_option(line_buf
)) {
188 if (cur_mon
== NULL
) {
192 do_trace_enable_events(line_buf
);
196 static void trace_init_events(const char *fname
)
208 loc_set_file(fname
, 0);
209 fp
= fopen(fname
, "r");
211 error_report("%s", strerror(errno
));
214 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
215 loc_set_file(fname
, ++line_idx
);
216 size_t len
= strlen(line_buf
);
217 if (len
> 1) { /* skip empty lines */
218 line_buf
[len
- 1] = '\0';
219 if ('#' == line_buf
[0]) { /* skip commented lines */
222 trace_enable_events(line_buf
);
225 if (fclose(fp
) != 0) {
226 loc_set_file(fname
, 0);
227 error_report("%s", strerror(errno
));
233 void trace_init_file(const char *file
)
235 #ifdef CONFIG_TRACE_SIMPLE
236 st_set_trace_file(file
);
237 #elif defined CONFIG_TRACE_LOG
238 /* If both the simple and the log backends are enabled, "-trace file"
239 * only applies to the simple backend; use "-D" for the log backend.
242 qemu_set_log_filename(file
, &error_fatal
);
246 fprintf(stderr
, "error: -trace file=...: "
247 "option not supported by the selected tracing backends\n");
253 bool trace_init_backends(void)
255 #ifdef CONFIG_TRACE_SIMPLE
257 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
262 #ifdef CONFIG_TRACE_FTRACE
263 if (!ftrace_init()) {
264 fprintf(stderr
, "failed to initialize ftrace backend.\n");
269 #ifdef CONFIG_TRACE_SYSLOG
270 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
276 char *trace_opt_parse(const char *optarg
)
279 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
284 if (qemu_opt_get(opts
, "enable")) {
285 trace_enable_events(qemu_opt_get(opts
, "enable"));
287 trace_init_events(qemu_opt_get(opts
, "events"));
288 trace_file
= g_strdup(qemu_opt_get(opts
, "file"));