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 #include "qemu/option.h"
14 #ifdef CONFIG_TRACE_SIMPLE
15 #include "trace/simple.h"
17 #ifdef CONFIG_TRACE_FTRACE
18 #include "trace/ftrace.h"
20 #ifdef CONFIG_TRACE_LOG
23 #ifdef CONFIG_TRACE_SYSLOG
26 #include "qapi/error.h"
27 #include "qemu/error-report.h"
28 #include "qemu/config-file.h"
29 #include "monitor/monitor.h"
30 #include "trace-root.h"
32 int trace_events_enabled_count
;
34 typedef struct TraceEventGroup
{
38 static TraceEventGroup
*event_groups
;
39 static size_t nevent_groups
;
40 static uint32_t next_id
;
41 static uint32_t next_vcpu_id
;
43 QemuOptsList qemu_trace_opts
= {
45 .implied_opt_name
= "enable",
46 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
50 .type
= QEMU_OPT_STRING
,
54 .type
= QEMU_OPT_STRING
,
57 .type
= QEMU_OPT_STRING
,
64 void trace_event_register_group(TraceEvent
**events
)
67 for (i
= 0; events
[i
] != NULL
; i
++) {
68 events
[i
]->id
= next_id
++;
69 if (events
[i
]->vcpu_id
== TRACE_VCPU_EVENT_NONE
) {
73 if (likely(next_vcpu_id
< CPU_TRACE_DSTATE_MAX_EVENTS
)) {
74 events
[i
]->vcpu_id
= next_vcpu_id
++;
76 warn_report("too many vcpu trace events; dropping '%s'",
80 event_groups
= g_renew(TraceEventGroup
, event_groups
, nevent_groups
+ 1);
81 event_groups
[nevent_groups
].events
= events
;
86 TraceEvent
*trace_event_name(const char *name
)
92 trace_event_iter_init(&iter
, NULL
);
93 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
94 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
101 static bool pattern_glob(const char *pat
, const char *ev
)
103 while (*pat
!= '\0' && *ev
!= '\0') {
108 else if (*pat
== '*') {
109 if (pattern_glob(pat
, ev
+1)) {
111 } else if (pattern_glob(pat
+1, ev
)) {
121 while (*pat
== '*') {
125 if (*pat
== '\0' && *ev
== '\0') {
133 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
137 iter
->pattern
= pattern
;
140 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
142 while (iter
->group
< nevent_groups
&&
143 event_groups
[iter
->group
].events
[iter
->event
] != NULL
) {
144 TraceEvent
*ev
= event_groups
[iter
->group
].events
[iter
->event
];
146 if (event_groups
[iter
->group
].events
[iter
->event
] == NULL
) {
150 if (!iter
->pattern
||
151 pattern_glob(iter
->pattern
,
152 trace_event_get_name(ev
))) {
160 void trace_list_events(void)
164 trace_event_iter_init(&iter
, NULL
);
165 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
166 fprintf(stderr
, "%s\n", trace_event_get_name(ev
));
170 static void do_trace_enable_events(const char *line_buf
)
172 const bool enable
= ('-' != line_buf
[0]);
173 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
176 bool is_pattern
= trace_event_is_pattern(line_ptr
);
178 trace_event_iter_init(&iter
, line_ptr
);
179 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
180 if (!trace_event_get_state_static(ev
)) {
182 warn_report("trace event '%s' is not traceable",
190 trace_event_set_state_dynamic(ev
, enable
);
197 warn_report("trace event '%s' does not exist",
202 void trace_enable_events(const char *line_buf
)
204 if (is_help_option(line_buf
)) {
206 if (cur_mon
== NULL
) {
210 do_trace_enable_events(line_buf
);
214 static void trace_init_events(const char *fname
)
226 loc_set_file(fname
, 0);
227 fp
= fopen(fname
, "r");
229 error_report("%s", strerror(errno
));
232 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
233 loc_set_file(fname
, ++line_idx
);
234 size_t len
= strlen(line_buf
);
235 if (len
> 1) { /* skip empty lines */
236 line_buf
[len
- 1] = '\0';
237 if ('#' == line_buf
[0]) { /* skip commented lines */
240 trace_enable_events(line_buf
);
243 if (fclose(fp
) != 0) {
244 loc_set_file(fname
, 0);
245 error_report("%s", strerror(errno
));
251 void trace_init_file(const char *file
)
253 #ifdef CONFIG_TRACE_SIMPLE
254 st_set_trace_file(file
);
255 #elif defined CONFIG_TRACE_LOG
256 /* If both the simple and the log backends are enabled, "--trace file"
257 * only applies to the simple backend; use "-D" for the log backend.
260 qemu_set_log_filename(file
, &error_fatal
);
264 fprintf(stderr
, "error: --trace file=...: "
265 "option not supported by the selected tracing backends\n");
271 void trace_fini_vcpu(CPUState
*vcpu
)
276 trace_guest_cpu_exit(vcpu
);
278 trace_event_iter_init(&iter
, NULL
);
279 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
280 if (trace_event_is_vcpu(ev
) &&
281 trace_event_get_state_static(ev
) &&
282 trace_event_get_vcpu_state_dynamic(vcpu
, ev
)) {
283 /* must disable to affect the global counter */
284 trace_event_set_vcpu_state_dynamic(vcpu
, ev
, false);
289 bool trace_init_backends(void)
291 #ifdef CONFIG_TRACE_SIMPLE
293 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
298 #ifdef CONFIG_TRACE_FTRACE
299 if (!ftrace_init()) {
300 fprintf(stderr
, "failed to initialize ftrace backend.\n");
305 #ifdef CONFIG_TRACE_SYSLOG
306 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
312 char *trace_opt_parse(const char *optarg
)
315 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
320 if (qemu_opt_get(opts
, "enable")) {
321 trace_enable_events(qemu_opt_get(opts
, "enable"));
323 trace_init_events(qemu_opt_get(opts
, "events"));
324 trace_file
= g_strdup(qemu_opt_get(opts
, "file"));
330 uint32_t trace_get_vcpu_event_count(void)