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"
17
#ifdef CONFIG_TRACE_FTRACE
18
#include "trace/ftrace.h"
20
#ifdef CONFIG_TRACE_LOG
23
#ifdef CONFIG_TRACE_SYSLOG
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 {
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 = {
47
.implied_opt_name = "enable",
48
.head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
52
.type = QEMU_OPT_STRING,
56
.type = QEMU_OPT_STRING,
59
.type = QEMU_OPT_STRING,
66
void trace_event_register_group(TraceEvent **events)
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;
76
#ifdef CONFIG_TRACE_SIMPLE
77
st_init_group(nevent_groups - 1);
82
TraceEvent *trace_event_name(const char *name)
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) {
97
void trace_event_iter_init_all(TraceEventIter *iter)
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;
124
if (event_groups[iter->group].events[iter->event] == NULL) {
129
!g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
132
if (iter->group_id != -1 &&
133
iter->group_id != group) {
142
void trace_list_events(FILE *f)
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",
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;
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)) {
170
warn_report("trace event '%s' is not traceable",
178
trace_event_set_state_dynamic(ev, enable);
185
warn_report("trace event '%s' does not exist",
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) {
198
do_trace_enable_events(line_buf);
202
static void trace_init_events(const char *fname)
214
loc_set_file(fname, 0);
215
fp = fopen(fname, "r");
217
error_report("%s", strerror(errno));
220
while (fgets(line_buf, sizeof(line_buf), fp)) {
221
loc_set_file(fname, ++line_idx);
222
size_t len = strlen(line_buf);
224
line_buf[len - 1] = '\0';
225
if ('#' == line_buf[0]) {
228
trace_enable_events(line_buf);
231
if (fclose(fp) != 0) {
232
loc_set_file(fname, 0);
233
error_report("%s", strerror(errno));
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
253
if (trace_opts_file) {
254
qemu_set_log_filename(trace_opts_file, &error_fatal);
257
if (trace_opts_file) {
258
fprintf(stderr, "error: --trace file=...: "
259
"option not supported by the selected tracing backends\n");
265
bool trace_init_backends(void)
267
#ifdef CONFIG_TRACE_SIMPLE
269
fprintf(stderr, "failed to initialize simple tracing backend.\n");
274
#ifdef CONFIG_TRACE_FTRACE
275
if (!ftrace_init()) {
276
fprintf(stderr, "failed to initialize ftrace backend.\n");
281
#ifdef CONFIG_TRACE_SYSLOG
282
openlog(NULL, LOG_PID, LOG_DAEMON);
288
void trace_opt_parse(const char *optstr)
290
QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
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"));
305
uint32_t trace_get_vcpu_event_count(void)