Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/trace/control.c
Line
Count
Source
1
/*
2
 * Interface for configuring and controlling the state of tracing events.
3
 *
4
 * Copyright (C) 2011-2016 LluĂ­s Vilanova <vilanova@ac.upc.edu>
5
 *
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
 */
9
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
31
int trace_events_enabled_count;
32
33
typedef struct TraceEventGroup {
34
    TraceEvent **events;
35
} TraceEventGroup;
36
37
static TraceEventGroup *event_groups;
38
static size_t nevent_groups;
39
static uint32_t next_id;
40
static uint32_t next_vcpu_id;
41
#ifdef CONFIG_TRACE_SIMPLE
42
static bool init_trace_on_startup;
43
#endif
44
static char *trace_opts_file;
45
46
QemuOptsList qemu_trace_opts = {
47
    .name = "trace",
48
    .implied_opt_name = "enable",
49
    .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
50
    .desc = {
51
        {
52
            .name = "enable",
53
            .type = QEMU_OPT_STRING,
54
        },
55
        {
56
            .name = "events",
57
            .type = QEMU_OPT_STRING,
58
        },{
59
            .name = "file",
60
            .type = QEMU_OPT_STRING,
61
        },
62
        { /* end of list */ }
63
    },
64
};
65
66
67
void trace_event_register_group(TraceEvent **events)
68
5.67k
{
69
5.67k
    size_t i;
70
345k
    for (i = 0; events[i] != NULL; i++) {
71
339k
        events[i]->id = next_id++;
72
339k
    }
73
5.67k
    event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
74
5.67k
    event_groups[nevent_groups].events = events;
75
5.67k
    nevent_groups++;
76
77
#ifdef CONFIG_TRACE_SIMPLE
78
    st_init_group(nevent_groups - 1);
79
#endif
80
5.67k
}
81
82
83
TraceEvent *trace_event_name(const char *name)
84
0
{
85
0
    assert(name != NULL);
86
87
0
    TraceEventIter iter;
88
0
    TraceEvent *ev;
89
0
    trace_event_iter_init_all(&iter);
90
0
    while ((ev = trace_event_iter_next(&iter)) != NULL) {
91
0
        if (strcmp(trace_event_get_name(ev), name) == 0) {
92
0
            return ev;
93
0
        }
94
0
    }
95
0
    return NULL;
96
0
}
97
98
void trace_event_iter_init_all(TraceEventIter *iter)
99
0
{
100
0
    iter->event = 0;
101
0
    iter->group = 0;
102
0
    iter->group_id = -1;
103
0
    iter->pattern = NULL;
104
0
}
105
106
void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
107
0
{
108
0
    trace_event_iter_init_all(iter);
109
0
    iter->pattern = pattern;
110
0
}
111
112
void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id)
113
0
{
114
0
    trace_event_iter_init_all(iter);
115
0
    iter->group_id = group_id;
116
0
}
117
118
TraceEvent *trace_event_iter_next(TraceEventIter *iter)
119
0
{
120
0
    while (iter->group < nevent_groups &&
121
0
           event_groups[iter->group].events[iter->event] != NULL) {
122
0
        TraceEvent *ev = event_groups[iter->group].events[iter->event];
123
0
        size_t group = iter->group;
124
0
        iter->event++;
125
0
        if (event_groups[iter->group].events[iter->event] == NULL) {
126
0
            iter->event = 0;
127
0
            iter->group++;
128
0
        }
129
0
        if (iter->pattern &&
130
0
            !g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
131
0
            continue;
132
0
        }
133
0
        if (iter->group_id != -1 &&
134
0
            iter->group_id != group) {
135
0
            continue;
136
0
        }
137
0
        return ev;
138
0
    }
139
140
0
    return NULL;
141
0
}
142
143
void trace_list_events(FILE *f)
144
0
{
145
0
    TraceEventIter iter;
146
0
    TraceEvent *ev;
147
0
    trace_event_iter_init_all(&iter);
148
0
    while ((ev = trace_event_iter_next(&iter)) != NULL) {
149
0
        fprintf(f, "%s\n", trace_event_get_name(ev));
150
0
    }
151
#ifdef CONFIG_TRACE_DTRACE
152
    fprintf(f, "This list of names of trace points may be incomplete "
153
               "when using the DTrace/SystemTap backends.\n"
154
               "Run 'qemu-trace-stap list %s' to print the full list.\n",
155
            g_get_prgname());
156
#endif
157
0
}
158
159
static void do_trace_enable_events(const char *line_buf)
160
0
{
161
0
    const bool enable = ('-' != line_buf[0]);
162
0
    const char *line_ptr = enable ? line_buf : line_buf + 1;
163
0
    TraceEventIter iter;
164
0
    TraceEvent *ev;
165
0
    bool is_pattern = trace_event_is_pattern(line_ptr);
166
167
0
    trace_event_iter_init_pattern(&iter, line_ptr);
168
0
    while ((ev = trace_event_iter_next(&iter)) != NULL) {
169
0
        if (!trace_event_get_state_static(ev)) {
170
0
            if (!is_pattern) {
171
0
                warn_report("trace event '%s' is not traceable",
172
0
                            line_ptr);
173
0
                return;
174
0
            }
175
0
            continue;
176
0
        }
177
178
        /* start tracing */
179
0
        trace_event_set_state_dynamic(ev, enable);
180
0
        if (!is_pattern) {
181
0
            return;
182
0
        }
183
0
    }
184
185
0
    if (!is_pattern) {
186
0
        warn_report("trace event '%s' does not exist",
187
0
                    line_ptr);
188
0
    }
189
0
}
190
191
void trace_enable_events(const char *line_buf)
192
0
{
193
0
    if (is_help_option(line_buf)) {
194
0
        trace_list_events(stdout);
195
0
        if (monitor_cur() == NULL) {
196
0
            exit(0);
197
0
        }
198
0
    } else {
199
0
        do_trace_enable_events(line_buf);
200
0
    }
201
0
}
202
203
static void trace_init_events(const char *fname)
204
0
{
205
0
    Location loc;
206
0
    FILE *fp;
207
0
    char line_buf[1024];
208
0
    size_t line_idx = 0;
209
210
0
    if (fname == NULL) {
211
0
        return;
212
0
    }
213
214
0
    loc_push_none(&loc);
215
0
    loc_set_file(fname, 0);
216
0
    fp = fopen(fname, "r");
217
0
    if (!fp) {
218
0
        error_report("%s", strerror(errno));
219
0
        exit(1);
220
0
    }
221
0
    while (fgets(line_buf, sizeof(line_buf), fp)) {
222
0
        loc_set_file(fname, ++line_idx);
223
0
        size_t len = strlen(line_buf);
224
0
        if (len > 1) {              /* skip empty lines */
225
0
            line_buf[len - 1] = '\0';
226
0
            if ('#' == line_buf[0]) { /* skip commented lines */
227
0
                continue;
228
0
            }
229
0
            trace_enable_events(line_buf);
230
0
        }
231
0
    }
232
0
    if (fclose(fp) != 0) {
233
0
        loc_set_file(fname, 0);
234
0
        error_report("%s", strerror(errno));
235
0
        exit(1);
236
0
    }
237
0
    loc_pop(&loc);
238
0
}
239
240
void trace_init_file(void)
241
61
{
242
#ifdef CONFIG_TRACE_SIMPLE
243
    st_set_trace_file(trace_opts_file);
244
    if (init_trace_on_startup) {
245
        st_set_trace_file_enabled(true);
246
    }
247
#elif defined CONFIG_TRACE_LOG
248
    /*
249
     * If both the simple and the log backends are enabled, "--trace file"
250
     * only applies to the simple backend; use "-D" for the log
251
     * backend. However we should only override -D if we actually have
252
     * something to override it with.
253
     */
254
61
    if (trace_opts_file) {
255
0
        qemu_set_log_filename(trace_opts_file, &error_fatal);
256
0
    }
257
#else
258
    if (trace_opts_file) {
259
        fprintf(stderr, "error: --trace file=...: "
260
                "option not supported by the selected tracing backends\n");
261
        exit(1);
262
    }
263
#endif
264
61
}
265
266
bool trace_init_backends(void)
267
61
{
268
#ifdef CONFIG_TRACE_SIMPLE
269
    if (!st_init()) {
270
        fprintf(stderr, "failed to initialize simple tracing backend.\n");
271
        return false;
272
    }
273
#endif
274
275
#ifdef CONFIG_TRACE_FTRACE
276
    if (!ftrace_init()) {
277
        fprintf(stderr, "failed to initialize ftrace backend.\n");
278
        return false;
279
    }
280
#endif
281
282
#ifdef CONFIG_TRACE_SYSLOG
283
    openlog(NULL, LOG_PID, LOG_DAEMON);
284
#endif
285
286
61
    return true;
287
61
}
288
289
void trace_opt_parse(const char *optstr)
290
0
{
291
0
    QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
292
0
                                             optstr, true);
293
0
    if (!opts) {
294
0
        exit(1);
295
0
    }
296
0
    if (qemu_opt_get(opts, "enable")) {
297
0
        trace_enable_events(qemu_opt_get(opts, "enable"));
298
0
    }
299
0
    trace_init_events(qemu_opt_get(opts, "events"));
300
#ifdef CONFIG_TRACE_SIMPLE
301
    init_trace_on_startup = true;
302
#endif
303
0
    g_free(trace_opts_file);
304
0
    trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
305
0
    qemu_opts_del(opts);
306
0
}
307
308
uint32_t trace_get_vcpu_event_count(void)
309
0
{
310
0
    return next_vcpu_id;
311
0
}