blob: b82fb87316daed1fe855579fbcc919c5b807511d [file] [log] [blame]
Lluís23d15e82011-08-31 20:31:31 +02001/*
2 * Interface for configuring and controlling the state of tracing events.
3 *
Lluís Vilanova48151852016-07-11 12:53:41 +02004 * Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
Lluís23d15e82011-08-31 20:31:31 +02005 *
Lluís Vilanovab1bae812013-03-05 14:47:38 +01006 * 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.
Lluís23d15e82011-08-31 20:31:31 +02008 */
9
Peter Maydelld38ea872016-01-29 17:50:05 +000010#include "qemu/osdep.h"
Lluís23d15e82011-08-31 20:31:31 +020011#include "trace/control.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020012#include "qemu/help_option.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010013#include "qemu/option.h"
Lluís Vilanova5b808272014-05-27 15:02:14 +020014#ifdef CONFIG_TRACE_SIMPLE
15#include "trace/simple.h"
16#endif
17#ifdef CONFIG_TRACE_FTRACE
18#include "trace/ftrace.h"
19#endif
Paolo Bonzinied7f5f12016-01-07 16:55:30 +030020#ifdef CONFIG_TRACE_LOG
21#include "qemu/log.h"
22#endif
Paul Durrant0a852412016-08-04 14:44:14 +010023#ifdef CONFIG_TRACE_SYSLOG
24#include <syslog.h>
25#endif
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +020026#include "qapi/error.h"
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +100027#include "qemu/error-report.h"
Denis V. Luneve9e0bb22016-06-17 17:44:10 +030028#include "qemu/config-file.h"
Denis V. Lunevacc68092016-03-16 14:36:51 +030029#include "monitor/monitor.h"
Paolo Bonzini243af022020-02-04 12:20:10 +010030#include "trace/trace-root.h"
Lluís23d15e82011-08-31 20:31:31 +020031
Paolo Bonzini43b48cf2015-10-28 07:06:26 +010032int trace_events_enabled_count;
33
Daniel P. Berrangefe4db842016-10-04 14:35:52 +010034typedef struct TraceEventGroup {
35 TraceEvent **events;
36} TraceEventGroup;
37
38static TraceEventGroup *event_groups;
39static size_t nevent_groups;
Daniel P. Berrangeca3fa0e2016-10-04 14:35:54 +010040static uint32_t next_id;
41static uint32_t next_vcpu_id;
Josh DuBois648b4822020-08-16 12:46:10 -050042static bool init_trace_on_startup;
Daniel P. Berrangefe4db842016-10-04 14:35:52 +010043
Denis V. Luneve9e0bb22016-06-17 17:44:10 +030044QemuOptsList qemu_trace_opts = {
45 .name = "trace",
46 .implied_opt_name = "enable",
47 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
48 .desc = {
49 {
50 .name = "enable",
51 .type = QEMU_OPT_STRING,
52 },
53 {
54 .name = "events",
55 .type = QEMU_OPT_STRING,
56 },{
57 .name = "file",
58 .type = QEMU_OPT_STRING,
59 },
60 { /* end of list */ }
61 },
62};
63
64
Daniel P. Berrangefe4db842016-10-04 14:35:52 +010065void trace_event_register_group(TraceEvent **events)
66{
Daniel P. Berrangeca3fa0e2016-10-04 14:35:54 +010067 size_t i;
68 for (i = 0; events[i] != NULL; i++) {
69 events[i]->id = next_id++;
Lluís Vilanovad01c05c2017-07-04 10:34:19 +020070 if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
71 continue;
72 }
73
74 if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
Daniel P. Berrangeca3fa0e2016-10-04 14:35:54 +010075 events[i]->vcpu_id = next_vcpu_id++;
Lluís Vilanovad01c05c2017-07-04 10:34:19 +020076 } else {
Alistair Francis55d527a2017-09-11 12:52:46 -070077 warn_report("too many vcpu trace events; dropping '%s'",
78 events[i]->name);
Daniel P. Berrangeca3fa0e2016-10-04 14:35:54 +010079 }
80 }
Daniel P. Berrangefe4db842016-10-04 14:35:52 +010081 event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
82 event_groups[nevent_groups].events = events;
83 nevent_groups++;
84}
85
86
Lluís Vilanovab1bae812013-03-05 14:47:38 +010087TraceEvent *trace_event_name(const char *name)
88{
89 assert(name != NULL);
90
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +010091 TraceEventIter iter;
92 TraceEvent *ev;
93 trace_event_iter_init(&iter, NULL);
94 while ((ev = trace_event_iter_next(&iter)) != NULL) {
Lluís Vilanovab1bae812013-03-05 14:47:38 +010095 if (strcmp(trace_event_get_name(ev), name) == 0) {
96 return ev;
97 }
98 }
99 return NULL;
100}
101
Daniel P. Berrange6a1b0f32016-10-04 14:35:42 +0100102void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
103{
104 iter->event = 0;
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100105 iter->group = 0;
Daniel P. Berrange6a1b0f32016-10-04 14:35:42 +0100106 iter->pattern = pattern;
107}
108
109TraceEvent *trace_event_iter_next(TraceEventIter *iter)
110{
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100111 while (iter->group < nevent_groups &&
112 event_groups[iter->group].events[iter->event] != NULL) {
113 TraceEvent *ev = event_groups[iter->group].events[iter->event];
Daniel P. Berrange6a1b0f32016-10-04 14:35:42 +0100114 iter->event++;
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100115 if (event_groups[iter->group].events[iter->event] == NULL) {
116 iter->event = 0;
117 iter->group++;
118 }
Daniel P. Berrange6a1b0f32016-10-04 14:35:42 +0100119 if (!iter->pattern ||
Alex Bennéee66eae72019-12-05 12:25:17 +0000120 g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
Daniel P. Berrange6a1b0f32016-10-04 14:35:42 +0100121 return ev;
122 }
123 }
124
125 return NULL;
126}
127
Paolo Bonzinie9527dd2016-01-07 16:55:27 +0300128void trace_list_events(void)
129{
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100130 TraceEventIter iter;
131 TraceEvent *ev;
132 trace_event_iter_init(&iter, NULL);
133 while ((ev = trace_event_iter_next(&iter)) != NULL) {
134 fprintf(stderr, "%s\n", trace_event_get_name(ev));
Paolo Bonzinie9527dd2016-01-07 16:55:27 +0300135 }
Philippe Mathieu-Daudé9f591a52019-08-23 16:22:03 +0200136#ifdef CONFIG_TRACE_DTRACE
137 fprintf(stderr, "This list of names of trace points may be incomplete "
138 "when using the DTrace/SystemTap backends.\n"
139 "Run 'qemu-trace-stap list %s' to print the full list.\n",
140 error_get_progname());
141#endif
Paolo Bonzinie9527dd2016-01-07 16:55:27 +0300142}
143
144static void do_trace_enable_events(const char *line_buf)
Paolo Bonzini10578a22016-01-07 16:55:26 +0300145{
146 const bool enable = ('-' != line_buf[0]);
147 const char *line_ptr = enable ? line_buf : line_buf + 1;
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100148 TraceEventIter iter;
149 TraceEvent *ev;
150 bool is_pattern = trace_event_is_pattern(line_ptr);
Paolo Bonzini10578a22016-01-07 16:55:26 +0300151
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100152 trace_event_iter_init(&iter, line_ptr);
153 while ((ev = trace_event_iter_next(&iter)) != NULL) {
154 if (!trace_event_get_state_static(ev)) {
155 if (!is_pattern) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700156 warn_report("trace event '%s' is not traceable",
157 line_ptr);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100158 return;
Paolo Bonzini10578a22016-01-07 16:55:26 +0300159 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100160 continue;
Paolo Bonzini10578a22016-01-07 16:55:26 +0300161 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100162
163 /* start tracing */
164 trace_event_set_state_dynamic(ev, enable);
165 if (!is_pattern) {
166 return;
Paolo Bonzini10578a22016-01-07 16:55:26 +0300167 }
168 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100169
170 if (!is_pattern) {
Alistair Francis3dc6f862017-07-12 06:57:41 -0700171 warn_report("trace event '%s' does not exist",
172 line_ptr);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +0100173 }
Paolo Bonzini10578a22016-01-07 16:55:26 +0300174}
175
Paolo Bonzinie9527dd2016-01-07 16:55:27 +0300176void trace_enable_events(const char *line_buf)
177{
178 if (is_help_option(line_buf)) {
179 trace_list_events();
Kevin Wolf947e4742020-10-05 17:58:44 +0200180 if (monitor_cur() == NULL) {
Denis V. Lunevacc68092016-03-16 14:36:51 +0300181 exit(0);
182 }
Paolo Bonzinie9527dd2016-01-07 16:55:27 +0300183 } else {
184 do_trace_enable_events(line_buf);
185 }
186}
187
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300188static void trace_init_events(const char *fname)
Lluís23d15e82011-08-31 20:31:31 +0200189{
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000190 Location loc;
191 FILE *fp;
192 char line_buf[1024];
193 size_t line_idx = 0;
194
Lluís23d15e82011-08-31 20:31:31 +0200195 if (fname == NULL) {
196 return;
197 }
198
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000199 loc_push_none(&loc);
200 loc_set_file(fname, 0);
201 fp = fopen(fname, "r");
Lluís23d15e82011-08-31 20:31:31 +0200202 if (!fp) {
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000203 error_report("%s", strerror(errno));
Lluís23d15e82011-08-31 20:31:31 +0200204 exit(1);
205 }
Lluís23d15e82011-08-31 20:31:31 +0200206 while (fgets(line_buf, sizeof(line_buf), fp)) {
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000207 loc_set_file(fname, ++line_idx);
Lluís23d15e82011-08-31 20:31:31 +0200208 size_t len = strlen(line_buf);
209 if (len > 1) { /* skip empty lines */
210 line_buf[len - 1] = '\0';
Alexey Kardashevskiy794b1f92012-06-14 14:41:40 +1000211 if ('#' == line_buf[0]) { /* skip commented lines */
212 continue;
213 }
Paolo Bonzini10578a22016-01-07 16:55:26 +0300214 trace_enable_events(line_buf);
Lluís23d15e82011-08-31 20:31:31 +0200215 }
216 }
217 if (fclose(fp) != 0) {
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000218 loc_set_file(fname, 0);
219 error_report("%s", strerror(errno));
Lluís23d15e82011-08-31 20:31:31 +0200220 exit(1);
221 }
Alexey Kardashevskiya35d9be2014-06-02 16:34:10 +1000222 loc_pop(&loc);
Lluís23d15e82011-08-31 20:31:31 +0200223}
Lluís Vilanova5b808272014-05-27 15:02:14 +0200224
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500225void trace_init_file(void)
Lluís Vilanova5b808272014-05-27 15:02:14 +0200226{
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500227 QemuOpts *opts = qemu_find_opts_singleton("trace");
228 const char *file = qemu_opt_get(opts, "file");
Lluís Vilanova5b808272014-05-27 15:02:14 +0200229#ifdef CONFIG_TRACE_SIMPLE
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300230 st_set_trace_file(file);
Josh DuBois648b4822020-08-16 12:46:10 -0500231 if (init_trace_on_startup) {
232 st_set_trace_file_enabled(true);
233 }
Paolo Bonzinied7f5f12016-01-07 16:55:30 +0300234#elif defined CONFIG_TRACE_LOG
Alex Bennée3d887542020-02-25 12:47:00 +0000235 /*
236 * If both the simple and the log backends are enabled, "--trace file"
237 * only applies to the simple backend; use "-D" for the log
238 * backend. However we should only override -D if we actually have
239 * something to override it with.
Paolo Bonzinied7f5f12016-01-07 16:55:30 +0300240 */
Alex Bennée3d887542020-02-25 12:47:00 +0000241 if (file) {
242 qemu_set_log_filename(file, &error_fatal);
243 }
Lluís Vilanova5b808272014-05-27 15:02:14 +0200244#else
245 if (file) {
Yaowei Baidb817b82018-07-04 11:17:27 +0800246 fprintf(stderr, "error: --trace file=...: "
Lluís Vilanova5b808272014-05-27 15:02:14 +0200247 "option not supported by the selected tracing backends\n");
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300248 exit(1);
249 }
250#endif
251}
252
Lluís Vilanova82e95ec2016-12-26 22:24:40 +0100253void trace_fini_vcpu(CPUState *vcpu)
254{
255 TraceEventIter iter;
256 TraceEvent *ev;
257
Lluís Vilanovaa47e8712016-12-26 22:24:46 +0100258 trace_guest_cpu_exit(vcpu);
259
Lluís Vilanova82e95ec2016-12-26 22:24:40 +0100260 trace_event_iter_init(&iter, NULL);
261 while ((ev = trace_event_iter_next(&iter)) != NULL) {
262 if (trace_event_is_vcpu(ev) &&
263 trace_event_get_state_static(ev) &&
264 trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
265 /* must disable to affect the global counter */
266 trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
267 }
268 }
269}
270
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300271bool trace_init_backends(void)
272{
273#ifdef CONFIG_TRACE_SIMPLE
274 if (!st_init()) {
275 fprintf(stderr, "failed to initialize simple tracing backend.\n");
Lluís Vilanova5b808272014-05-27 15:02:14 +0200276 return false;
277 }
278#endif
279
280#ifdef CONFIG_TRACE_FTRACE
281 if (!ftrace_init()) {
282 fprintf(stderr, "failed to initialize ftrace backend.\n");
283 return false;
284 }
285#endif
286
Paul Durrant0a852412016-08-04 14:44:14 +0100287#ifdef CONFIG_TRACE_SYSLOG
288 openlog(NULL, LOG_PID, LOG_DAEMON);
289#endif
290
Lluís Vilanova5b808272014-05-27 15:02:14 +0200291 return true;
292}
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300293
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500294void trace_opt_parse(const char *optarg)
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300295{
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300296 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
297 optarg, true);
298 if (!opts) {
299 exit(1);
300 }
301 if (qemu_opt_get(opts, "enable")) {
302 trace_enable_events(qemu_opt_get(opts, "enable"));
303 }
304 trace_init_events(qemu_opt_get(opts, "events"));
Josh DuBois648b4822020-08-16 12:46:10 -0500305 init_trace_on_startup = true;
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300306 qemu_opts_del(opts);
Denis V. Luneve9e0bb22016-06-17 17:44:10 +0300307}
Daniel P. Berrangeb7d48952016-10-04 14:35:53 +0100308
309uint32_t trace_get_vcpu_event_count(void)
310{
Daniel P. Berrangeca3fa0e2016-10-04 14:35:54 +0100311 return next_vcpu_id;
Daniel P. Berrangeb7d48952016-10-04 14:35:53 +0100312}