trace: remove some now unused functions
[qemu/kevin.git] / trace / control.c
bloba423f32eb09f1d599b11697e2e61eca394a8adf2
1 /*
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.
8 */
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"
15 #endif
16 #ifdef CONFIG_TRACE_FTRACE
17 #include "trace/ftrace.h"
18 #endif
19 #ifdef CONFIG_TRACE_LOG
20 #include "qemu/log.h"
21 #endif
22 #ifdef CONFIG_TRACE_SYSLOG
23 #include <syslog.h>
24 #endif
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/config-file.h"
28 #include "monitor/monitor.h"
30 int trace_events_enabled_count;
32 * Interpretation depends on wether the event has the 'vcpu' property:
33 * - false: Boolean value indicating whether the event is active.
34 * - true : Integral counting the number of vCPUs that have this event enabled.
36 uint16_t trace_events_dstate[TRACE_EVENT_COUNT];
38 QemuOptsList qemu_trace_opts = {
39 .name = "trace",
40 .implied_opt_name = "enable",
41 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
42 .desc = {
44 .name = "enable",
45 .type = QEMU_OPT_STRING,
48 .name = "events",
49 .type = QEMU_OPT_STRING,
50 },{
51 .name = "file",
52 .type = QEMU_OPT_STRING,
54 { /* end of list */ }
59 TraceEvent *trace_event_name(const char *name)
61 assert(name != NULL);
63 TraceEventIter iter;
64 TraceEvent *ev;
65 trace_event_iter_init(&iter, NULL);
66 while ((ev = trace_event_iter_next(&iter)) != NULL) {
67 if (strcmp(trace_event_get_name(ev), name) == 0) {
68 return ev;
71 return NULL;
74 static bool pattern_glob(const char *pat, const char *ev)
76 while (*pat != '\0' && *ev != '\0') {
77 if (*pat == *ev) {
78 pat++;
79 ev++;
81 else if (*pat == '*') {
82 if (pattern_glob(pat, ev+1)) {
83 return true;
84 } else if (pattern_glob(pat+1, ev)) {
85 return true;
86 } else {
87 return false;
89 } else {
90 return false;
94 while (*pat == '*') {
95 pat++;
98 if (*pat == '\0' && *ev == '\0') {
99 return true;
100 } else {
101 return false;
106 void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
108 iter->event = 0;
109 iter->pattern = pattern;
112 TraceEvent *trace_event_iter_next(TraceEventIter *iter)
114 while (iter->event < TRACE_EVENT_COUNT) {
115 TraceEvent *ev = &(trace_events[iter->event]);
116 iter->event++;
117 if (!iter->pattern ||
118 pattern_glob(iter->pattern,
119 trace_event_get_name(ev))) {
120 return ev;
124 return NULL;
127 void trace_list_events(void)
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));
137 static void do_trace_enable_events(const char *line_buf)
139 const bool enable = ('-' != line_buf[0]);
140 const char *line_ptr = enable ? line_buf : line_buf + 1;
141 TraceEventIter iter;
142 TraceEvent *ev;
143 bool is_pattern = trace_event_is_pattern(line_ptr);
145 trace_event_iter_init(&iter, line_ptr);
146 while ((ev = trace_event_iter_next(&iter)) != NULL) {
147 if (!trace_event_get_state_static(ev)) {
148 if (!is_pattern) {
149 error_report("WARNING: trace event '%s' is not traceable",
150 line_ptr);
151 return;
153 continue;
156 /* start tracing */
157 trace_event_set_state_dynamic(ev, enable);
158 if (!is_pattern) {
159 return;
163 if (!is_pattern) {
164 error_report("WARNING: trace event '%s' does not exist",
165 line_ptr);
169 void trace_enable_events(const char *line_buf)
171 if (is_help_option(line_buf)) {
172 trace_list_events();
173 if (cur_mon == NULL) {
174 exit(0);
176 } else {
177 do_trace_enable_events(line_buf);
181 static void trace_init_events(const char *fname)
183 Location loc;
184 FILE *fp;
185 char line_buf[1024];
186 size_t line_idx = 0;
188 if (fname == NULL) {
189 return;
192 loc_push_none(&loc);
193 loc_set_file(fname, 0);
194 fp = fopen(fname, "r");
195 if (!fp) {
196 error_report("%s", strerror(errno));
197 exit(1);
199 while (fgets(line_buf, sizeof(line_buf), fp)) {
200 loc_set_file(fname, ++line_idx);
201 size_t len = strlen(line_buf);
202 if (len > 1) { /* skip empty lines */
203 line_buf[len - 1] = '\0';
204 if ('#' == line_buf[0]) { /* skip commented lines */
205 continue;
207 trace_enable_events(line_buf);
210 if (fclose(fp) != 0) {
211 loc_set_file(fname, 0);
212 error_report("%s", strerror(errno));
213 exit(1);
215 loc_pop(&loc);
218 void trace_init_file(const char *file)
220 #ifdef CONFIG_TRACE_SIMPLE
221 st_set_trace_file(file);
222 #elif defined CONFIG_TRACE_LOG
223 /* If both the simple and the log backends are enabled, "-trace file"
224 * only applies to the simple backend; use "-D" for the log backend.
226 if (file) {
227 qemu_set_log_filename(file, &error_fatal);
229 #else
230 if (file) {
231 fprintf(stderr, "error: -trace file=...: "
232 "option not supported by the selected tracing backends\n");
233 exit(1);
235 #endif
238 bool trace_init_backends(void)
240 #ifdef CONFIG_TRACE_SIMPLE
241 if (!st_init()) {
242 fprintf(stderr, "failed to initialize simple tracing backend.\n");
243 return false;
245 #endif
247 #ifdef CONFIG_TRACE_FTRACE
248 if (!ftrace_init()) {
249 fprintf(stderr, "failed to initialize ftrace backend.\n");
250 return false;
252 #endif
254 #ifdef CONFIG_TRACE_SYSLOG
255 openlog(NULL, LOG_PID, LOG_DAEMON);
256 #endif
258 return true;
261 char *trace_opt_parse(const char *optarg)
263 char *trace_file;
264 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
265 optarg, true);
266 if (!opts) {
267 exit(1);
269 if (qemu_opt_get(opts, "enable")) {
270 trace_enable_events(qemu_opt_get(opts, "enable"));
272 trace_init_events(qemu_opt_get(opts, "events"));
273 trace_file = g_strdup(qemu_opt_get(opts, "file"));
274 qemu_opts_del(opts);
276 return trace_file;