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"
29 #include "trace-root.h"
31 int trace_events_enabled_count
;
33 typedef struct TraceEventGroup
{
37 static TraceEventGroup
*event_groups
;
38 static size_t nevent_groups
;
39 static uint32_t next_id
;
40 static uint32_t next_vcpu_id
;
42 QemuOptsList qemu_trace_opts
= {
44 .implied_opt_name
= "enable",
45 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
49 .type
= QEMU_OPT_STRING
,
53 .type
= QEMU_OPT_STRING
,
56 .type
= QEMU_OPT_STRING
,
63 void trace_event_register_group(TraceEvent
**events
)
66 for (i
= 0; events
[i
] != NULL
; i
++) {
67 events
[i
]->id
= next_id
++;
68 if (events
[i
]->vcpu_id
== TRACE_VCPU_EVENT_NONE
) {
72 if (likely(next_vcpu_id
< CPU_TRACE_DSTATE_MAX_EVENTS
)) {
73 events
[i
]->vcpu_id
= next_vcpu_id
++;
75 warn_report("too many vcpu trace events; dropping '%s'",
79 event_groups
= g_renew(TraceEventGroup
, event_groups
, nevent_groups
+ 1);
80 event_groups
[nevent_groups
].events
= events
;
85 TraceEvent
*trace_event_name(const char *name
)
91 trace_event_iter_init(&iter
, NULL
);
92 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
93 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
100 static bool pattern_glob(const char *pat
, const char *ev
)
102 while (*pat
!= '\0' && *ev
!= '\0') {
107 else if (*pat
== '*') {
108 if (pattern_glob(pat
, ev
+1)) {
110 } else if (pattern_glob(pat
+1, ev
)) {
120 while (*pat
== '*') {
124 if (*pat
== '\0' && *ev
== '\0') {
132 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
136 iter
->pattern
= pattern
;
139 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
141 while (iter
->group
< nevent_groups
&&
142 event_groups
[iter
->group
].events
[iter
->event
] != NULL
) {
143 TraceEvent
*ev
= event_groups
[iter
->group
].events
[iter
->event
];
145 if (event_groups
[iter
->group
].events
[iter
->event
] == NULL
) {
149 if (!iter
->pattern
||
150 pattern_glob(iter
->pattern
,
151 trace_event_get_name(ev
))) {
159 void trace_list_events(void)
163 trace_event_iter_init(&iter
, NULL
);
164 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
165 fprintf(stderr
, "%s\n", trace_event_get_name(ev
));
169 static void do_trace_enable_events(const char *line_buf
)
171 const bool enable
= ('-' != line_buf
[0]);
172 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
175 bool is_pattern
= trace_event_is_pattern(line_ptr
);
177 trace_event_iter_init(&iter
, line_ptr
);
178 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
179 if (!trace_event_get_state_static(ev
)) {
181 warn_report("trace event '%s' is not traceable",
189 trace_event_set_state_dynamic(ev
, enable
);
196 warn_report("trace event '%s' does not exist",
201 void trace_enable_events(const char *line_buf
)
203 if (is_help_option(line_buf
)) {
205 if (cur_mon
== NULL
) {
209 do_trace_enable_events(line_buf
);
213 static void trace_init_events(const char *fname
)
225 loc_set_file(fname
, 0);
226 fp
= fopen(fname
, "r");
228 error_report("%s", strerror(errno
));
231 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
232 loc_set_file(fname
, ++line_idx
);
233 size_t len
= strlen(line_buf
);
234 if (len
> 1) { /* skip empty lines */
235 line_buf
[len
- 1] = '\0';
236 if ('#' == line_buf
[0]) { /* skip commented lines */
239 trace_enable_events(line_buf
);
242 if (fclose(fp
) != 0) {
243 loc_set_file(fname
, 0);
244 error_report("%s", strerror(errno
));
250 void trace_init_file(const char *file
)
252 #ifdef CONFIG_TRACE_SIMPLE
253 st_set_trace_file(file
);
254 #elif defined CONFIG_TRACE_LOG
255 /* If both the simple and the log backends are enabled, "-trace file"
256 * only applies to the simple backend; use "-D" for the log backend.
259 qemu_set_log_filename(file
, &error_fatal
);
263 fprintf(stderr
, "error: -trace file=...: "
264 "option not supported by the selected tracing backends\n");
270 void trace_fini_vcpu(CPUState
*vcpu
)
275 trace_guest_cpu_exit(vcpu
);
277 trace_event_iter_init(&iter
, NULL
);
278 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
279 if (trace_event_is_vcpu(ev
) &&
280 trace_event_get_state_static(ev
) &&
281 trace_event_get_vcpu_state_dynamic(vcpu
, ev
)) {
282 /* must disable to affect the global counter */
283 trace_event_set_vcpu_state_dynamic(vcpu
, ev
, false);
288 bool trace_init_backends(void)
290 #ifdef CONFIG_TRACE_SIMPLE
292 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
297 #ifdef CONFIG_TRACE_FTRACE
298 if (!ftrace_init()) {
299 fprintf(stderr
, "failed to initialize ftrace backend.\n");
304 #ifdef CONFIG_TRACE_SYSLOG
305 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
311 char *trace_opt_parse(const char *optarg
)
314 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
319 if (qemu_opt_get(opts
, "enable")) {
320 trace_enable_events(qemu_opt_get(opts
, "enable"));
322 trace_init_events(qemu_opt_get(opts
, "events"));
323 trace_file
= g_strdup(qemu_opt_get(opts
, "file"));
329 uint32_t trace_get_vcpu_event_count(void)