]> git.proxmox.com Git - mirror_qemu.git/blame - trace/control.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / trace / control.c
CommitLineData
23d15e86
LV
1/*
2 * Interface for configuring and controlling the state of tracing events.
3 *
48151859 4 * Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
23d15e86 5 *
b1bae816
LV
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.
23d15e86
LV
8 */
9
d38ea87a 10#include "qemu/osdep.h"
23d15e86 11#include "trace/control.h"
f348b6d1 12#include "qemu/help_option.h"
922a01a0 13#include "qemu/option.h"
5b808275
LV
14#ifdef CONFIG_TRACE_SIMPLE
15#include "trace/simple.h"
16#endif
17#ifdef CONFIG_TRACE_FTRACE
18#include "trace/ftrace.h"
19#endif
ed7f5f1d
PB
20#ifdef CONFIG_TRACE_LOG
21#include "qemu/log.h"
22#endif
0a852417
PD
23#ifdef CONFIG_TRACE_SYSLOG
24#include <syslog.h>
25#endif
daa76aa4 26#include "qapi/error.h"
a35d9be6 27#include "qemu/error-report.h"
e9e0bb2a 28#include "qemu/config-file.h"
acc6809d 29#include "monitor/monitor.h"
0ab8ed18 30#include "trace-root.h"
23d15e86 31
43b48cfc
PB
32int trace_events_enabled_count;
33
fe4db84d
DB
34typedef struct TraceEventGroup {
35 TraceEvent **events;
36} TraceEventGroup;
37
38static TraceEventGroup *event_groups;
39static size_t nevent_groups;
ca3fa0e8
DB
40static uint32_t next_id;
41static uint32_t next_vcpu_id;
fe4db84d 42
e9e0bb2a
DL
43QemuOptsList qemu_trace_opts = {
44 .name = "trace",
45 .implied_opt_name = "enable",
46 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
47 .desc = {
48 {
49 .name = "enable",
50 .type = QEMU_OPT_STRING,
51 },
52 {
53 .name = "events",
54 .type = QEMU_OPT_STRING,
55 },{
56 .name = "file",
57 .type = QEMU_OPT_STRING,
58 },
59 { /* end of list */ }
60 },
61};
62
63
fe4db84d
DB
64void trace_event_register_group(TraceEvent **events)
65{
ca3fa0e8
DB
66 size_t i;
67 for (i = 0; events[i] != NULL; i++) {
68 events[i]->id = next_id++;
d01c05c9
LV
69 if (events[i]->vcpu_id == TRACE_VCPU_EVENT_NONE) {
70 continue;
71 }
72
73 if (likely(next_vcpu_id < CPU_TRACE_DSTATE_MAX_EVENTS)) {
ca3fa0e8 74 events[i]->vcpu_id = next_vcpu_id++;
d01c05c9 75 } else {
55d527a9
AF
76 warn_report("too many vcpu trace events; dropping '%s'",
77 events[i]->name);
ca3fa0e8
DB
78 }
79 }
fe4db84d
DB
80 event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
81 event_groups[nevent_groups].events = events;
82 nevent_groups++;
83}
84
85
b1bae816
LV
86TraceEvent *trace_event_name(const char *name)
87{
88 assert(name != NULL);
89
0d4e995c
DB
90 TraceEventIter iter;
91 TraceEvent *ev;
92 trace_event_iter_init(&iter, NULL);
93 while ((ev = trace_event_iter_next(&iter)) != NULL) {
b1bae816
LV
94 if (strcmp(trace_event_get_name(ev), name) == 0) {
95 return ev;
96 }
97 }
98 return NULL;
99}
100
6a1b0f3a
DB
101void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
102{
103 iter->event = 0;
fe4db84d 104 iter->group = 0;
6a1b0f3a
DB
105 iter->pattern = pattern;
106}
107
108TraceEvent *trace_event_iter_next(TraceEventIter *iter)
109{
fe4db84d
DB
110 while (iter->group < nevent_groups &&
111 event_groups[iter->group].events[iter->event] != NULL) {
112 TraceEvent *ev = event_groups[iter->group].events[iter->event];
6a1b0f3a 113 iter->event++;
fe4db84d
DB
114 if (event_groups[iter->group].events[iter->event] == NULL) {
115 iter->event = 0;
116 iter->group++;
117 }
6a1b0f3a 118 if (!iter->pattern ||
e66eae7a 119 g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
6a1b0f3a
DB
120 return ev;
121 }
122 }
123
124 return NULL;
125}
126
e9527dd3
PB
127void trace_list_events(void)
128{
0d4e995c
DB
129 TraceEventIter iter;
130 TraceEvent *ev;
131 trace_event_iter_init(&iter, NULL);
132 while ((ev = trace_event_iter_next(&iter)) != NULL) {
133 fprintf(stderr, "%s\n", trace_event_get_name(ev));
e9527dd3 134 }
9f591a5d
PMD
135#ifdef CONFIG_TRACE_DTRACE
136 fprintf(stderr, "This list of names of trace points may be incomplete "
137 "when using the DTrace/SystemTap backends.\n"
138 "Run 'qemu-trace-stap list %s' to print the full list.\n",
139 error_get_progname());
140#endif
e9527dd3
PB
141}
142
143static void do_trace_enable_events(const char *line_buf)
10578a25
PB
144{
145 const bool enable = ('-' != line_buf[0]);
146 const char *line_ptr = enable ? line_buf : line_buf + 1;
0d4e995c
DB
147 TraceEventIter iter;
148 TraceEvent *ev;
149 bool is_pattern = trace_event_is_pattern(line_ptr);
150
151 trace_event_iter_init(&iter, line_ptr);
152 while ((ev = trace_event_iter_next(&iter)) != NULL) {
153 if (!trace_event_get_state_static(ev)) {
154 if (!is_pattern) {
3dc6f869
AF
155 warn_report("trace event '%s' is not traceable",
156 line_ptr);
0d4e995c 157 return;
10578a25 158 }
0d4e995c 159 continue;
10578a25 160 }
0d4e995c
DB
161
162 /* start tracing */
163 trace_event_set_state_dynamic(ev, enable);
164 if (!is_pattern) {
165 return;
10578a25
PB
166 }
167 }
0d4e995c
DB
168
169 if (!is_pattern) {
3dc6f869
AF
170 warn_report("trace event '%s' does not exist",
171 line_ptr);
0d4e995c 172 }
10578a25
PB
173}
174
e9527dd3
PB
175void trace_enable_events(const char *line_buf)
176{
177 if (is_help_option(line_buf)) {
178 trace_list_events();
acc6809d
DL
179 if (cur_mon == NULL) {
180 exit(0);
181 }
e9527dd3
PB
182 } else {
183 do_trace_enable_events(line_buf);
184 }
185}
186
e9e0bb2a 187static void trace_init_events(const char *fname)
b1bae816 188{
a35d9be6
AK
189 Location loc;
190 FILE *fp;
191 char line_buf[1024];
192 size_t line_idx = 0;
193
23d15e86
LV
194 if (fname == NULL) {
195 return;
196 }
197
a35d9be6
AK
198 loc_push_none(&loc);
199 loc_set_file(fname, 0);
200 fp = fopen(fname, "r");
23d15e86 201 if (!fp) {
a35d9be6 202 error_report("%s", strerror(errno));
23d15e86
LV
203 exit(1);
204 }
23d15e86 205 while (fgets(line_buf, sizeof(line_buf), fp)) {
a35d9be6 206 loc_set_file(fname, ++line_idx);
23d15e86
LV
207 size_t len = strlen(line_buf);
208 if (len > 1) { /* skip empty lines */
209 line_buf[len - 1] = '\0';
794b1f96
AK
210 if ('#' == line_buf[0]) { /* skip commented lines */
211 continue;
212 }
10578a25 213 trace_enable_events(line_buf);
23d15e86
LV
214 }
215 }
216 if (fclose(fp) != 0) {
a35d9be6
AK
217 loc_set_file(fname, 0);
218 error_report("%s", strerror(errno));
23d15e86
LV
219 exit(1);
220 }
a35d9be6 221 loc_pop(&loc);
23d15e86 222}
5b808275 223
41fc57e4 224void trace_init_file(const char *file)
5b808275
LV
225{
226#ifdef CONFIG_TRACE_SIMPLE
41fc57e4 227 st_set_trace_file(file);
ed7f5f1d 228#elif defined CONFIG_TRACE_LOG
db817b8c 229 /* If both the simple and the log backends are enabled, "--trace file"
ed7f5f1d
PB
230 * only applies to the simple backend; use "-D" for the log backend.
231 */
232 if (file) {
daa76aa4 233 qemu_set_log_filename(file, &error_fatal);
ed7f5f1d 234 }
5b808275
LV
235#else
236 if (file) {
db817b8c 237 fprintf(stderr, "error: --trace file=...: "
5b808275 238 "option not supported by the selected tracing backends\n");
41fc57e4
PB
239 exit(1);
240 }
241#endif
242}
243
82e95ec8
LV
244void trace_fini_vcpu(CPUState *vcpu)
245{
246 TraceEventIter iter;
247 TraceEvent *ev;
248
a47e8715
LV
249 trace_guest_cpu_exit(vcpu);
250
82e95ec8
LV
251 trace_event_iter_init(&iter, NULL);
252 while ((ev = trace_event_iter_next(&iter)) != NULL) {
253 if (trace_event_is_vcpu(ev) &&
254 trace_event_get_state_static(ev) &&
255 trace_event_get_vcpu_state_dynamic(vcpu, ev)) {
256 /* must disable to affect the global counter */
257 trace_event_set_vcpu_state_dynamic(vcpu, ev, false);
258 }
259 }
260}
261
41fc57e4
PB
262bool trace_init_backends(void)
263{
264#ifdef CONFIG_TRACE_SIMPLE
265 if (!st_init()) {
266 fprintf(stderr, "failed to initialize simple tracing backend.\n");
5b808275
LV
267 return false;
268 }
269#endif
270
271#ifdef CONFIG_TRACE_FTRACE
272 if (!ftrace_init()) {
273 fprintf(stderr, "failed to initialize ftrace backend.\n");
274 return false;
275 }
276#endif
277
0a852417
PD
278#ifdef CONFIG_TRACE_SYSLOG
279 openlog(NULL, LOG_PID, LOG_DAEMON);
280#endif
281
5b808275
LV
282 return true;
283}
e9e0bb2a
DL
284
285char *trace_opt_parse(const char *optarg)
286{
287 char *trace_file;
288 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
289 optarg, true);
290 if (!opts) {
291 exit(1);
292 }
293 if (qemu_opt_get(opts, "enable")) {
294 trace_enable_events(qemu_opt_get(opts, "enable"));
295 }
296 trace_init_events(qemu_opt_get(opts, "events"));
297 trace_file = g_strdup(qemu_opt_get(opts, "file"));
298 qemu_opts_del(opts);
299
300 return trace_file;
301}
b7d48952
DB
302
303uint32_t trace_get_vcpu_event_count(void)
304{
ca3fa0e8 305 return next_vcpu_id;
b7d48952 306}