scsi: mptconfig: fix an assert expression
[qemu/ar7.git] / trace / control.c
blob05d85accbd7c82b9bae1a638796704600d906857
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 TraceEventID i;
64 for (i = 0; i < trace_event_count(); i++) {
65 TraceEvent *ev = trace_event_id(i);
66 if (strcmp(trace_event_get_name(ev), name) == 0) {
67 return ev;
70 return NULL;
73 static bool pattern_glob(const char *pat, const char *ev)
75 while (*pat != '\0' && *ev != '\0') {
76 if (*pat == *ev) {
77 pat++;
78 ev++;
80 else if (*pat == '*') {
81 if (pattern_glob(pat, ev+1)) {
82 return true;
83 } else if (pattern_glob(pat+1, ev)) {
84 return true;
85 } else {
86 return false;
88 } else {
89 return false;
93 while (*pat == '*') {
94 pat++;
97 if (*pat == '\0' && *ev == '\0') {
98 return true;
99 } else {
100 return false;
104 TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
106 assert(pat != NULL);
108 TraceEventID i;
110 if (ev == NULL) {
111 i = -1;
112 } else {
113 i = trace_event_get_id(ev);
115 i++;
117 while (i < trace_event_count()) {
118 TraceEvent *res = trace_event_id(i);
119 if (pattern_glob(pat, trace_event_get_name(res))) {
120 return res;
122 i++;
125 return NULL;
128 void trace_list_events(void)
130 int i;
131 for (i = 0; i < trace_event_count(); i++) {
132 TraceEvent *res = trace_event_id(i);
133 fprintf(stderr, "%s\n", trace_event_get_name(res));
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;
142 if (trace_event_is_pattern(line_ptr)) {
143 TraceEvent *ev = NULL;
144 while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
145 if (trace_event_get_state_static(ev)) {
146 trace_event_set_state_dynamic_init(ev, enable);
149 } else {
150 TraceEvent *ev = trace_event_name(line_ptr);
151 if (ev == NULL) {
152 error_report("WARNING: trace event '%s' does not exist",
153 line_ptr);
154 } else if (!trace_event_get_state_static(ev)) {
155 error_report("WARNING: trace event '%s' is not traceable",
156 line_ptr);
157 } else {
158 trace_event_set_state_dynamic_init(ev, enable);
163 void trace_enable_events(const char *line_buf)
165 if (is_help_option(line_buf)) {
166 trace_list_events();
167 if (cur_mon == NULL) {
168 exit(0);
170 } else {
171 do_trace_enable_events(line_buf);
175 static void trace_init_events(const char *fname)
177 Location loc;
178 FILE *fp;
179 char line_buf[1024];
180 size_t line_idx = 0;
182 if (fname == NULL) {
183 return;
186 loc_push_none(&loc);
187 loc_set_file(fname, 0);
188 fp = fopen(fname, "r");
189 if (!fp) {
190 error_report("%s", strerror(errno));
191 exit(1);
193 while (fgets(line_buf, sizeof(line_buf), fp)) {
194 loc_set_file(fname, ++line_idx);
195 size_t len = strlen(line_buf);
196 if (len > 1) { /* skip empty lines */
197 line_buf[len - 1] = '\0';
198 if ('#' == line_buf[0]) { /* skip commented lines */
199 continue;
201 trace_enable_events(line_buf);
204 if (fclose(fp) != 0) {
205 loc_set_file(fname, 0);
206 error_report("%s", strerror(errno));
207 exit(1);
209 loc_pop(&loc);
212 void trace_init_file(const char *file)
214 #ifdef CONFIG_TRACE_SIMPLE
215 st_set_trace_file(file);
216 #elif defined CONFIG_TRACE_LOG
217 /* If both the simple and the log backends are enabled, "-trace file"
218 * only applies to the simple backend; use "-D" for the log backend.
220 if (file) {
221 qemu_set_log_filename(file, &error_fatal);
223 #else
224 if (file) {
225 fprintf(stderr, "error: -trace file=...: "
226 "option not supported by the selected tracing backends\n");
227 exit(1);
229 #endif
232 bool trace_init_backends(void)
234 #ifdef CONFIG_TRACE_SIMPLE
235 if (!st_init()) {
236 fprintf(stderr, "failed to initialize simple tracing backend.\n");
237 return false;
239 #endif
241 #ifdef CONFIG_TRACE_FTRACE
242 if (!ftrace_init()) {
243 fprintf(stderr, "failed to initialize ftrace backend.\n");
244 return false;
246 #endif
248 #ifdef CONFIG_TRACE_SYSLOG
249 openlog(NULL, LOG_PID, LOG_DAEMON);
250 #endif
252 return true;
255 char *trace_opt_parse(const char *optarg)
257 char *trace_file;
258 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
259 optarg, true);
260 if (!opts) {
261 exit(1);
263 if (qemu_opt_get(opts, "enable")) {
264 trace_enable_events(qemu_opt_get(opts, "enable"));
266 trace_init_events(qemu_opt_get(opts, "events"));
267 trace_file = g_strdup(qemu_opt_get(opts, "file"));
268 qemu_opts_del(opts);
270 return trace_file;
273 void trace_init_vcpu_events(void)
275 TraceEvent *ev = NULL;
276 while ((ev = trace_event_pattern("*", ev)) != NULL) {
277 if (trace_event_is_vcpu(ev) &&
278 trace_event_get_state_static(ev) &&
279 trace_event_get_state_dynamic(ev)) {
280 TraceEventID id = trace_event_get_id(ev);
281 /* check preconditions */
282 assert(trace_events_dstate[id] == 1);
283 /* disable early-init state ... */
284 trace_events_dstate[id] = 0;
285 trace_events_enabled_count--;
286 /* ... and properly re-enable */
287 trace_event_set_state_dynamic(ev, true);