Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / trace / control.c
blobef107829ac00ebfb90c01b2677a27aae33c7e841
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 #include "qemu/option.h"
14 #ifdef CONFIG_TRACE_SIMPLE
15 #include "trace/simple.h"
16 #endif
17 #ifdef CONFIG_TRACE_FTRACE
18 #include "trace/ftrace.h"
19 #endif
20 #ifdef CONFIG_TRACE_LOG
21 #include "qemu/log.h"
22 #endif
23 #ifdef CONFIG_TRACE_SYSLOG
24 #include <syslog.h>
25 #endif
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 {
35 TraceEvent **events;
36 } 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 = {
46 .name = "trace",
47 .implied_opt_name = "enable",
48 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
49 .desc = {
51 .name = "enable",
52 .type = QEMU_OPT_STRING,
55 .name = "events",
56 .type = QEMU_OPT_STRING,
57 },{
58 .name = "file",
59 .type = QEMU_OPT_STRING,
61 { /* end of list */ }
66 void trace_event_register_group(TraceEvent **events)
68 size_t i;
69 for (i = 0; events[i] != NULL; i++) {
70 events[i]->id = next_id++;
72 event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
73 event_groups[nevent_groups].events = events;
74 nevent_groups++;
76 #ifdef CONFIG_TRACE_SIMPLE
77 st_init_group(nevent_groups - 1);
78 #endif
82 TraceEvent *trace_event_name(const char *name)
84 assert(name != NULL);
86 TraceEventIter iter;
87 TraceEvent *ev;
88 trace_event_iter_init_all(&iter);
89 while ((ev = trace_event_iter_next(&iter)) != NULL) {
90 if (strcmp(trace_event_get_name(ev), name) == 0) {
91 return ev;
94 return NULL;
97 void trace_event_iter_init_all(TraceEventIter *iter)
99 iter->event = 0;
100 iter->group = 0;
101 iter->group_id = -1;
102 iter->pattern = NULL;
105 void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
107 trace_event_iter_init_all(iter);
108 iter->pattern = pattern;
111 void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id)
113 trace_event_iter_init_all(iter);
114 iter->group_id = group_id;
117 TraceEvent *trace_event_iter_next(TraceEventIter *iter)
119 while (iter->group < nevent_groups &&
120 event_groups[iter->group].events[iter->event] != NULL) {
121 TraceEvent *ev = event_groups[iter->group].events[iter->event];
122 size_t group = iter->group;
123 iter->event++;
124 if (event_groups[iter->group].events[iter->event] == NULL) {
125 iter->event = 0;
126 iter->group++;
128 if (iter->pattern &&
129 !g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
130 continue;
132 if (iter->group_id != -1 &&
133 iter->group_id != group) {
134 continue;
136 return ev;
139 return NULL;
142 void trace_list_events(FILE *f)
144 TraceEventIter iter;
145 TraceEvent *ev;
146 trace_event_iter_init_all(&iter);
147 while ((ev = trace_event_iter_next(&iter)) != NULL) {
148 fprintf(f, "%s\n", trace_event_get_name(ev));
150 #ifdef CONFIG_TRACE_DTRACE
151 fprintf(f, "This list of names of trace points may be incomplete "
152 "when using the DTrace/SystemTap backends.\n"
153 "Run 'qemu-trace-stap list %s' to print the full list.\n",
154 g_get_prgname());
155 #endif
158 static void do_trace_enable_events(const char *line_buf)
160 const bool enable = ('-' != line_buf[0]);
161 const char *line_ptr = enable ? line_buf : line_buf + 1;
162 TraceEventIter iter;
163 TraceEvent *ev;
164 bool is_pattern = trace_event_is_pattern(line_ptr);
166 trace_event_iter_init_pattern(&iter, line_ptr);
167 while ((ev = trace_event_iter_next(&iter)) != NULL) {
168 if (!trace_event_get_state_static(ev)) {
169 if (!is_pattern) {
170 warn_report("trace event '%s' is not traceable",
171 line_ptr);
172 return;
174 continue;
177 /* start tracing */
178 trace_event_set_state_dynamic(ev, enable);
179 if (!is_pattern) {
180 return;
184 if (!is_pattern) {
185 warn_report("trace event '%s' does not exist",
186 line_ptr);
190 void trace_enable_events(const char *line_buf)
192 if (is_help_option(line_buf)) {
193 trace_list_events(stdout);
194 if (monitor_cur() == NULL) {
195 exit(0);
197 } else {
198 do_trace_enable_events(line_buf);
202 static void trace_init_events(const char *fname)
204 Location loc;
205 FILE *fp;
206 char line_buf[1024];
207 size_t line_idx = 0;
209 if (fname == NULL) {
210 return;
213 loc_push_none(&loc);
214 loc_set_file(fname, 0);
215 fp = fopen(fname, "r");
216 if (!fp) {
217 error_report("%s", strerror(errno));
218 exit(1);
220 while (fgets(line_buf, sizeof(line_buf), fp)) {
221 loc_set_file(fname, ++line_idx);
222 size_t len = strlen(line_buf);
223 if (len > 1) { /* skip empty lines */
224 line_buf[len - 1] = '\0';
225 if ('#' == line_buf[0]) { /* skip commented lines */
226 continue;
228 trace_enable_events(line_buf);
231 if (fclose(fp) != 0) {
232 loc_set_file(fname, 0);
233 error_report("%s", strerror(errno));
234 exit(1);
236 loc_pop(&loc);
239 void trace_init_file(void)
241 #ifdef CONFIG_TRACE_SIMPLE
242 st_set_trace_file(trace_opts_file);
243 if (init_trace_on_startup) {
244 st_set_trace_file_enabled(true);
246 #elif defined CONFIG_TRACE_LOG
248 * If both the simple and the log backends are enabled, "--trace file"
249 * only applies to the simple backend; use "-D" for the log
250 * backend. However we should only override -D if we actually have
251 * something to override it with.
253 if (trace_opts_file) {
254 qemu_set_log_filename(trace_opts_file, &error_fatal);
256 #else
257 if (trace_opts_file) {
258 fprintf(stderr, "error: --trace file=...: "
259 "option not supported by the selected tracing backends\n");
260 exit(1);
262 #endif
265 bool trace_init_backends(void)
267 #ifdef CONFIG_TRACE_SIMPLE
268 if (!st_init()) {
269 fprintf(stderr, "failed to initialize simple tracing backend.\n");
270 return false;
272 #endif
274 #ifdef CONFIG_TRACE_FTRACE
275 if (!ftrace_init()) {
276 fprintf(stderr, "failed to initialize ftrace backend.\n");
277 return false;
279 #endif
281 #ifdef CONFIG_TRACE_SYSLOG
282 openlog(NULL, LOG_PID, LOG_DAEMON);
283 #endif
285 return true;
288 void trace_opt_parse(const char *optstr)
290 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
291 optstr, true);
292 if (!opts) {
293 exit(1);
295 if (qemu_opt_get(opts, "enable")) {
296 trace_enable_events(qemu_opt_get(opts, "enable"));
298 trace_init_events(qemu_opt_get(opts, "events"));
299 init_trace_on_startup = true;
300 g_free(trace_opts_file);
301 trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
302 qemu_opts_del(opts);
305 uint32_t trace_get_vcpu_event_count(void)
307 return next_vcpu_id;