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/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
;
42 static bool init_trace_on_startup
;
43 static char *trace_opts_file
;
45 QemuOptsList qemu_trace_opts
= {
47 .implied_opt_name
= "enable",
48 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
52 .type
= QEMU_OPT_STRING
,
56 .type
= QEMU_OPT_STRING
,
59 .type
= QEMU_OPT_STRING
,
66 void trace_event_register_group(TraceEvent
**events
)
69 for (i
= 0; events
[i
] != NULL
; i
++) {
70 events
[i
]->id
= next_id
++;
71 if (events
[i
]->vcpu_id
== TRACE_VCPU_EVENT_NONE
) {
75 if (likely(next_vcpu_id
< CPU_TRACE_DSTATE_MAX_EVENTS
)) {
76 events
[i
]->vcpu_id
= next_vcpu_id
++;
78 warn_report("too many vcpu trace events; dropping '%s'",
82 event_groups
= g_renew(TraceEventGroup
, event_groups
, nevent_groups
+ 1);
83 event_groups
[nevent_groups
].events
= events
;
88 TraceEvent
*trace_event_name(const char *name
)
94 trace_event_iter_init(&iter
, NULL
);
95 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
96 if (strcmp(trace_event_get_name(ev
), name
) == 0) {
103 void trace_event_iter_init(TraceEventIter
*iter
, const char *pattern
)
107 iter
->pattern
= pattern
;
110 TraceEvent
*trace_event_iter_next(TraceEventIter
*iter
)
112 while (iter
->group
< nevent_groups
&&
113 event_groups
[iter
->group
].events
[iter
->event
] != NULL
) {
114 TraceEvent
*ev
= event_groups
[iter
->group
].events
[iter
->event
];
116 if (event_groups
[iter
->group
].events
[iter
->event
] == NULL
) {
120 if (!iter
->pattern
||
121 g_pattern_match_simple(iter
->pattern
, trace_event_get_name(ev
))) {
129 void trace_list_events(FILE *f
)
133 trace_event_iter_init(&iter
, NULL
);
134 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
135 fprintf(f
, "%s\n", trace_event_get_name(ev
));
137 #ifdef CONFIG_TRACE_DTRACE
138 fprintf(f
, "This list of names of trace points may be incomplete "
139 "when using the DTrace/SystemTap backends.\n"
140 "Run 'qemu-trace-stap list %s' to print the full list.\n",
141 error_get_progname());
145 static void do_trace_enable_events(const char *line_buf
)
147 const bool enable
= ('-' != line_buf
[0]);
148 const char *line_ptr
= enable
? line_buf
: line_buf
+ 1;
151 bool is_pattern
= trace_event_is_pattern(line_ptr
);
153 trace_event_iter_init(&iter
, line_ptr
);
154 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
155 if (!trace_event_get_state_static(ev
)) {
157 warn_report("trace event '%s' is not traceable",
165 trace_event_set_state_dynamic(ev
, enable
);
172 warn_report("trace event '%s' does not exist",
177 void trace_enable_events(const char *line_buf
)
179 if (is_help_option(line_buf
)) {
180 trace_list_events(stdout
);
181 if (monitor_cur() == NULL
) {
185 do_trace_enable_events(line_buf
);
189 static void trace_init_events(const char *fname
)
201 loc_set_file(fname
, 0);
202 fp
= fopen(fname
, "r");
204 error_report("%s", strerror(errno
));
207 while (fgets(line_buf
, sizeof(line_buf
), fp
)) {
208 loc_set_file(fname
, ++line_idx
);
209 size_t len
= strlen(line_buf
);
210 if (len
> 1) { /* skip empty lines */
211 line_buf
[len
- 1] = '\0';
212 if ('#' == line_buf
[0]) { /* skip commented lines */
215 trace_enable_events(line_buf
);
218 if (fclose(fp
) != 0) {
219 loc_set_file(fname
, 0);
220 error_report("%s", strerror(errno
));
226 void trace_init_file(void)
228 #ifdef CONFIG_TRACE_SIMPLE
229 st_set_trace_file(trace_opts_file
);
230 if (init_trace_on_startup
) {
231 st_set_trace_file_enabled(true);
233 #elif defined CONFIG_TRACE_LOG
235 * If both the simple and the log backends are enabled, "--trace file"
236 * only applies to the simple backend; use "-D" for the log
237 * backend. However we should only override -D if we actually have
238 * something to override it with.
240 if (trace_opts_file
) {
241 qemu_set_log_filename(trace_opts_file
, &error_fatal
);
244 if (trace_opts_file
) {
245 fprintf(stderr
, "error: --trace file=...: "
246 "option not supported by the selected tracing backends\n");
252 void trace_fini_vcpu(CPUState
*vcpu
)
257 trace_guest_cpu_exit(vcpu
);
259 trace_event_iter_init(&iter
, NULL
);
260 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
261 if (trace_event_is_vcpu(ev
) &&
262 trace_event_get_state_static(ev
) &&
263 trace_event_get_vcpu_state_dynamic(vcpu
, ev
)) {
264 /* must disable to affect the global counter */
265 trace_event_set_vcpu_state_dynamic(vcpu
, ev
, false);
270 bool trace_init_backends(void)
272 #ifdef CONFIG_TRACE_SIMPLE
274 fprintf(stderr
, "failed to initialize simple tracing backend.\n");
279 #ifdef CONFIG_TRACE_FTRACE
280 if (!ftrace_init()) {
281 fprintf(stderr
, "failed to initialize ftrace backend.\n");
286 #ifdef CONFIG_TRACE_SYSLOG
287 openlog(NULL
, LOG_PID
, LOG_DAEMON
);
293 void trace_opt_parse(const char *optarg
)
295 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("trace"),
300 if (qemu_opt_get(opts
, "enable")) {
301 trace_enable_events(qemu_opt_get(opts
, "enable"));
303 trace_init_events(qemu_opt_get(opts
, "events"));
304 init_trace_on_startup
= true;
305 g_free(trace_opts_file
);
306 trace_opts_file
= g_strdup(qemu_opt_get(opts
, "file"));
310 uint32_t trace_get_vcpu_event_count(void)