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
;
38 static uint32_t next_id
;
39 static uint32_t next_vcpu_id
;
41 QemuOptsList qemu_trace_opts
= {
43 .implied_opt_name
= "enable",
44 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
48 .type
= QEMU_OPT_STRING
,
52 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_STRING
,
62 void trace_event_register_group(TraceEvent
**events
)
65 for (i
= 0; events
[i
] != NULL
; i
++) {
66 events
[i
]->id
= next_id
++;
67 if (events
[i
]->vcpu_id
!= TRACE_VCPU_EVENT_NONE
) {
68 events
[i
]->vcpu_id
= next_vcpu_id
++;
71 event_groups
= g_renew(TraceEventGroup
, event_groups
, nevent_groups
+ 1);
72 event_groups
[nevent_groups
].events
= events
;
77 TraceEvent
*trace_event_name(const char *name
)
83 trace_event_iter_init(&iter
, NULL
);
84 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
85 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
92 static bool pattern_glob(const char *pat
, const char *ev
)
94 while (*pat
!= '\0' && *ev
!= '\0') {
99 else if (*pat
== '*') {
100 if (pattern_glob(pat
, ev
+1)) {
102 } else if (pattern_glob(pat
+1, ev
)) {
112 while (*pat
== '*') {
116 if (*pat
== '\0' && *ev
== '\0') {
124 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
128 iter
->pattern
= pattern
;
131 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
133 while (iter
->group
< nevent_groups
&&
134 event_groups
[iter
->group
].events
[iter
->event
] != NULL
) {
135 TraceEvent
*ev
= event_groups
[iter
->group
].events
[iter
->event
];
137 if (event_groups
[iter
->group
].events
[iter
->event
] == NULL
) {
141 if (!iter
->pattern
||
142 pattern_glob(iter
->pattern
,
143 trace_event_get_name(ev
))) {
151 void trace_list_events(void)
155 trace_event_iter_init(&iter
, NULL
);
156 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
157 fprintf(stderr
, "%s\n", trace_event_get_name(ev
));
161 static void do_trace_enable_events(const char *line_buf
)
163 const bool enable
= ('-' != line_buf
[0]);
164 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
167 bool is_pattern
= trace_event_is_pattern(line_ptr
);
169 trace_event_iter_init(&iter
, line_ptr
);
170 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
171 if (!trace_event_get_state_static(ev
)) {
173 error_report("WARNING: trace event '%s' is not traceable",
181 trace_event_set_state_dynamic(ev
, enable
);
188 error_report("WARNING: trace event '%s' does not exist",
193 void trace_enable_events(const char *line_buf
)
195 if (is_help_option(line_buf
)) {
197 if (cur_mon
== NULL
) {
201 do_trace_enable_events(line_buf
);
205 static void trace_init_events(const char *fname
)
217 loc_set_file(fname
, 0);
218 fp
= fopen(fname
, "r");
220 error_report("%s", strerror(errno
));
223 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
224 loc_set_file(fname
, ++line_idx
);
225 size_t len
= strlen(line_buf
);
226 if (len
> 1) { /* skip empty lines */
227 line_buf
[len
- 1] = '\0';
228 if ('#' == line_buf
[0]) { /* skip commented lines */
231 trace_enable_events(line_buf
);
234 if (fclose(fp
) != 0) {
235 loc_set_file(fname
, 0);
236 error_report("%s", strerror(errno
));
242 void trace_init_file(const char *file
)
244 #ifdef CONFIG_TRACE_SIMPLE
245 st_set_trace_file(file
);
246 #elif defined CONFIG_TRACE_LOG
247 /* If both the simple and the log backends are enabled, "-trace file"
248 * only applies to the simple backend; use "-D" for the log backend.
251 qemu_set_log_filename(file
, &error_fatal
);
255 fprintf(stderr
, "error: -trace file=...: "
256 "option not supported by the selected tracing backends\n");
262 bool trace_init_backends(void)
264 #ifdef CONFIG_TRACE_SIMPLE
266 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
271 #ifdef CONFIG_TRACE_FTRACE
272 if (!ftrace_init()) {
273 fprintf(stderr
, "failed to initialize ftrace backend.\n");
278 #ifdef CONFIG_TRACE_SYSLOG
279 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
285 char *trace_opt_parse(const char *optarg
)
288 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
293 if (qemu_opt_get(opts
, "enable")) {
294 trace_enable_events(qemu_opt_get(opts
, "enable"));
296 trace_init_events(qemu_opt_get(opts
, "events"));
297 trace_file
= g_strdup(qemu_opt_get(opts
, "file"));
303 uint32_t trace_get_vcpu_event_count(void)