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_all(&iter);
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_all(TraceEventIter *iter)
107 iter->pattern = NULL;
110 void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
112 trace_event_iter_init_all(iter);
113 iter->pattern = pattern;
116 TraceEvent *trace_event_iter_next(TraceEventIter *iter)
118 while (iter->group < nevent_groups &&
119 event_groups[iter->group].events[iter->event] != NULL) {
120 TraceEvent *ev = event_groups[iter->group].events[iter->event];
122 if (event_groups[iter->group].events[iter->event] == NULL) {
126 if (!iter->pattern ||
127 g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
135 void trace_list_events(FILE *f)
139 trace_event_iter_init_all(&iter);
140 while ((ev = trace_event_iter_next(&iter)) != NULL) {
141 fprintf(f, "%s\n", trace_event_get_name(ev));
143 #ifdef CONFIG_TRACE_DTRACE
144 fprintf(f, "This list of names of trace points may be incomplete "
145 "when using the DTrace/SystemTap backends.\n"
146 "Run 'qemu-trace-stap list %s' to print the full list.\n",
147 error_get_progname());
151 static void do_trace_enable_events(const char *line_buf)
153 const bool enable = ('-' != line_buf[0]);
154 const char *line_ptr = enable ? line_buf : line_buf + 1;
157 bool is_pattern = trace_event_is_pattern(line_ptr);
159 trace_event_iter_init_pattern(&iter, line_ptr);
160 while ((ev = trace_event_iter_next(&iter)) != NULL) {
161 if (!trace_event_get_state_static(ev)) {
163 warn_report("trace event '%s' is not traceable",
171 trace_event_set_state_dynamic(ev, enable);
178 warn_report("trace event '%s' does not exist",
183 void trace_enable_events(const char *line_buf)
185 if (is_help_option(line_buf)) {
186 trace_list_events(stdout);
187 if (monitor_cur() == NULL) {
191 do_trace_enable_events(line_buf);
195 static void trace_init_events(const char *fname)
207 loc_set_file(fname, 0);
208 fp = fopen(fname, "r");
210 error_report("%s", strerror(errno));
213 while (fgets(line_buf, sizeof(line_buf), fp)) {
214 loc_set_file(fname, ++line_idx);
215 size_t len = strlen(line_buf);
216 if (len > 1) { /* skip empty lines */
217 line_buf[len - 1] = '\0';
218 if ('#' == line_buf[0]) { /* skip commented lines */
221 trace_enable_events(line_buf);
224 if (fclose(fp) != 0) {
225 loc_set_file(fname, 0);
226 error_report("%s", strerror(errno));
232 void trace_init_file(void)
234 #ifdef CONFIG_TRACE_SIMPLE
235 st_set_trace_file(trace_opts_file);
236 if (init_trace_on_startup) {
237 st_set_trace_file_enabled(true);
239 #elif defined CONFIG_TRACE_LOG
241 * If both the simple and the log backends are enabled, "--trace file"
242 * only applies to the simple backend; use "-D" for the log
243 * backend. However we should only override -D if we actually have
244 * something to override it with.
246 if (trace_opts_file) {
247 qemu_set_log_filename(trace_opts_file, &error_fatal);
250 if (trace_opts_file) {
251 fprintf(stderr, "error: --trace file=...: "
252 "option not supported by the selected tracing backends\n");
258 void trace_fini_vcpu(CPUState *vcpu)
263 trace_guest_cpu_exit(vcpu);
265 trace_event_iter_init_all(&iter);
266 while ((ev = trace_event_iter_next(&iter)) != NULL) {
267 if (trace_event_is_vcpu(ev) &&
268 trace_event_get_state_static(ev) &&
269 trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
270 /* must disable to affect the global counter */
271 trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
276 bool trace_init_backends(void)
278 #ifdef CONFIG_TRACE_SIMPLE
280 fprintf(stderr, "failed to initialize simple tracing backend.\n");
285 #ifdef CONFIG_TRACE_FTRACE
286 if (!ftrace_init()) {
287 fprintf(stderr, "failed to initialize ftrace backend.\n");
292 #ifdef CONFIG_TRACE_SYSLOG
293 openlog(NULL, LOG_PID, LOG_DAEMON);
299 void trace_opt_parse(const char *optarg)
301 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
306 if (qemu_opt_get(opts, "enable")) {
307 trace_enable_events(qemu_opt_get(opts, "enable"));
309 trace_init_events(qemu_opt_get(opts, "events"));
310 init_trace_on_startup = true;
311 g_free(trace_opts_file);
312 trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
316 uint32_t trace_get_vcpu_event_count(void)