2011-08-31 22:31:31 +04:00
|
|
|
/*
|
|
|
|
* Interface for configuring and controlling the state of tracing events.
|
|
|
|
*
|
2016-07-11 13:53:41 +03:00
|
|
|
* Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
|
2011-08-31 22:31:31 +04:00
|
|
|
*
|
2013-03-05 17:47:38 +04:00
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
2011-08-31 22:31:31 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2011-08-31 22:31:31 +04:00
|
|
|
#include "trace/control.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/help_option.h"
|
2018-02-01 14:18:46 +03:00
|
|
|
#include "qemu/option.h"
|
2014-05-27 17:02:14 +04:00
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
|
|
|
#include "trace/simple.h"
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TRACE_FTRACE
|
|
|
|
#include "trace/ftrace.h"
|
|
|
|
#endif
|
2016-01-07 16:55:30 +03:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
#include "qemu/log.h"
|
|
|
|
#endif
|
2016-08-04 16:44:14 +03:00
|
|
|
#ifdef CONFIG_TRACE_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
2016-06-15 20:27:16 +03:00
|
|
|
#include "qapi/error.h"
|
2014-06-02 10:34:10 +04:00
|
|
|
#include "qemu/error-report.h"
|
2016-06-17 17:44:10 +03:00
|
|
|
#include "qemu/config-file.h"
|
2016-03-16 14:36:51 +03:00
|
|
|
#include "monitor/monitor.h"
|
2020-02-04 14:20:10 +03:00
|
|
|
#include "trace/trace-root.h"
|
2011-08-31 22:31:31 +04:00
|
|
|
|
2015-10-28 09:06:26 +03:00
|
|
|
int trace_events_enabled_count;
|
|
|
|
|
2016-10-04 16:35:52 +03:00
|
|
|
typedef struct TraceEventGroup {
|
|
|
|
TraceEvent **events;
|
|
|
|
} TraceEventGroup;
|
|
|
|
|
|
|
|
static TraceEventGroup *event_groups;
|
|
|
|
static size_t nevent_groups;
|
2016-10-04 16:35:54 +03:00
|
|
|
static uint32_t next_id;
|
|
|
|
static uint32_t next_vcpu_id;
|
2020-08-16 20:46:10 +03:00
|
|
|
static bool init_trace_on_startup;
|
2021-02-09 17:57:58 +03:00
|
|
|
static char *trace_opts_file;
|
2016-10-04 16:35:52 +03:00
|
|
|
|
2016-06-17 17:44:10 +03:00
|
|
|
QemuOptsList qemu_trace_opts = {
|
|
|
|
.name = "trace",
|
|
|
|
.implied_opt_name = "enable",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "enable",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "events",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
},{
|
|
|
|
.name = "file",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-04 16:35:52 +03:00
|
|
|
void trace_event_register_group(TraceEvent **events)
|
|
|
|
{
|
2016-10-04 16:35:54 +03:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; events[i] != NULL; i++) {
|
|
|
|
events[i]->id = next_id++;
|
|
|
|
}
|
2016-10-04 16:35:52 +03:00
|
|
|
event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
|
|
|
|
event_groups[nevent_groups].events = events;
|
|
|
|
nevent_groups++;
|
2021-06-01 16:24:06 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
|
|
|
st_init_group(nevent_groups - 1);
|
|
|
|
#endif
|
2016-10-04 16:35:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-05 17:47:38 +04:00
|
|
|
TraceEvent *trace_event_name(const char *name)
|
|
|
|
{
|
|
|
|
assert(name != NULL);
|
|
|
|
|
2016-10-04 16:35:43 +03:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
2021-06-01 16:24:03 +03:00
|
|
|
trace_event_iter_init_all(&iter);
|
2016-10-04 16:35:43 +03:00
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
2013-03-05 17:47:38 +04:00
|
|
|
if (strcmp(trace_event_get_name(ev), name) == 0) {
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-01 16:24:03 +03:00
|
|
|
void trace_event_iter_init_all(TraceEventIter *iter)
|
2016-10-04 16:35:42 +03:00
|
|
|
{
|
|
|
|
iter->event = 0;
|
2016-10-04 16:35:52 +03:00
|
|
|
iter->group = 0;
|
2021-06-01 16:24:04 +03:00
|
|
|
iter->group_id = -1;
|
2021-06-01 16:24:03 +03:00
|
|
|
iter->pattern = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
|
|
|
|
{
|
|
|
|
trace_event_iter_init_all(iter);
|
2016-10-04 16:35:42 +03:00
|
|
|
iter->pattern = pattern;
|
|
|
|
}
|
|
|
|
|
2021-06-01 16:24:04 +03:00
|
|
|
void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id)
|
|
|
|
{
|
|
|
|
trace_event_iter_init_all(iter);
|
|
|
|
iter->group_id = group_id;
|
|
|
|
}
|
|
|
|
|
2016-10-04 16:35:42 +03:00
|
|
|
TraceEvent *trace_event_iter_next(TraceEventIter *iter)
|
|
|
|
{
|
2016-10-04 16:35:52 +03:00
|
|
|
while (iter->group < nevent_groups &&
|
|
|
|
event_groups[iter->group].events[iter->event] != NULL) {
|
|
|
|
TraceEvent *ev = event_groups[iter->group].events[iter->event];
|
2021-06-01 16:24:04 +03:00
|
|
|
size_t group = iter->group;
|
2016-10-04 16:35:42 +03:00
|
|
|
iter->event++;
|
2016-10-04 16:35:52 +03:00
|
|
|
if (event_groups[iter->group].events[iter->event] == NULL) {
|
|
|
|
iter->event = 0;
|
|
|
|
iter->group++;
|
|
|
|
}
|
2021-06-01 16:24:04 +03:00
|
|
|
if (iter->pattern &&
|
|
|
|
!g_pattern_match_simple(iter->pattern, trace_event_get_name(ev))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (iter->group_id != -1 &&
|
|
|
|
iter->group_id != group) {
|
|
|
|
continue;
|
2016-10-04 16:35:42 +03:00
|
|
|
}
|
2021-06-01 16:24:04 +03:00
|
|
|
return ev;
|
2016-10-04 16:35:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:52:45 +03:00
|
|
|
void trace_list_events(FILE *f)
|
2016-01-07 16:55:27 +03:00
|
|
|
{
|
2016-10-04 16:35:43 +03:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
2021-06-01 16:24:03 +03:00
|
|
|
trace_event_iter_init_all(&iter);
|
2016-10-04 16:35:43 +03:00
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
2020-11-26 00:52:45 +03:00
|
|
|
fprintf(f, "%s\n", trace_event_get_name(ev));
|
2016-01-07 16:55:27 +03:00
|
|
|
}
|
2019-08-23 17:22:03 +03:00
|
|
|
#ifdef CONFIG_TRACE_DTRACE
|
2020-11-26 00:52:45 +03:00
|
|
|
fprintf(f, "This list of names of trace points may be incomplete "
|
|
|
|
"when using the DTrace/SystemTap backends.\n"
|
|
|
|
"Run 'qemu-trace-stap list %s' to print the full list.\n",
|
2022-02-21 13:11:47 +03:00
|
|
|
g_get_prgname());
|
2019-08-23 17:22:03 +03:00
|
|
|
#endif
|
2016-01-07 16:55:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_trace_enable_events(const char *line_buf)
|
2016-01-07 16:55:26 +03:00
|
|
|
{
|
|
|
|
const bool enable = ('-' != line_buf[0]);
|
|
|
|
const char *line_ptr = enable ? line_buf : line_buf + 1;
|
2016-10-04 16:35:43 +03:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
|
|
|
bool is_pattern = trace_event_is_pattern(line_ptr);
|
|
|
|
|
2021-06-01 16:24:03 +03:00
|
|
|
trace_event_iter_init_pattern(&iter, line_ptr);
|
2016-10-04 16:35:43 +03:00
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
|
|
|
if (!trace_event_get_state_static(ev)) {
|
|
|
|
if (!is_pattern) {
|
2017-07-12 16:57:41 +03:00
|
|
|
warn_report("trace event '%s' is not traceable",
|
|
|
|
line_ptr);
|
2016-10-04 16:35:43 +03:00
|
|
|
return;
|
2016-01-07 16:55:26 +03:00
|
|
|
}
|
2016-10-04 16:35:43 +03:00
|
|
|
continue;
|
2016-01-07 16:55:26 +03:00
|
|
|
}
|
2016-10-04 16:35:43 +03:00
|
|
|
|
|
|
|
/* start tracing */
|
|
|
|
trace_event_set_state_dynamic(ev, enable);
|
|
|
|
if (!is_pattern) {
|
|
|
|
return;
|
2016-01-07 16:55:26 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-04 16:35:43 +03:00
|
|
|
|
|
|
|
if (!is_pattern) {
|
2017-07-12 16:57:41 +03:00
|
|
|
warn_report("trace event '%s' does not exist",
|
|
|
|
line_ptr);
|
2016-10-04 16:35:43 +03:00
|
|
|
}
|
2016-01-07 16:55:26 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 16:55:27 +03:00
|
|
|
void trace_enable_events(const char *line_buf)
|
|
|
|
{
|
|
|
|
if (is_help_option(line_buf)) {
|
2020-11-26 00:52:45 +03:00
|
|
|
trace_list_events(stdout);
|
2020-10-05 18:58:44 +03:00
|
|
|
if (monitor_cur() == NULL) {
|
2016-03-16 14:36:51 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
2016-01-07 16:55:27 +03:00
|
|
|
} else {
|
|
|
|
do_trace_enable_events(line_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 17:44:10 +03:00
|
|
|
static void trace_init_events(const char *fname)
|
2013-03-05 17:47:38 +04:00
|
|
|
{
|
2014-06-02 10:34:10 +04:00
|
|
|
Location loc;
|
|
|
|
FILE *fp;
|
|
|
|
char line_buf[1024];
|
|
|
|
size_t line_idx = 0;
|
|
|
|
|
2011-08-31 22:31:31 +04:00
|
|
|
if (fname == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-02 10:34:10 +04:00
|
|
|
loc_push_none(&loc);
|
|
|
|
loc_set_file(fname, 0);
|
|
|
|
fp = fopen(fname, "r");
|
2011-08-31 22:31:31 +04:00
|
|
|
if (!fp) {
|
2014-06-02 10:34:10 +04:00
|
|
|
error_report("%s", strerror(errno));
|
2011-08-31 22:31:31 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (fgets(line_buf, sizeof(line_buf), fp)) {
|
2014-06-02 10:34:10 +04:00
|
|
|
loc_set_file(fname, ++line_idx);
|
2011-08-31 22:31:31 +04:00
|
|
|
size_t len = strlen(line_buf);
|
|
|
|
if (len > 1) { /* skip empty lines */
|
|
|
|
line_buf[len - 1] = '\0';
|
2012-06-14 08:41:40 +04:00
|
|
|
if ('#' == line_buf[0]) { /* skip commented lines */
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-07 16:55:26 +03:00
|
|
|
trace_enable_events(line_buf);
|
2011-08-31 22:31:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fclose(fp) != 0) {
|
2014-06-02 10:34:10 +04:00
|
|
|
loc_set_file(fname, 0);
|
|
|
|
error_report("%s", strerror(errno));
|
2011-08-31 22:31:31 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-06-02 10:34:10 +04:00
|
|
|
loc_pop(&loc);
|
2011-08-31 22:31:31 +04:00
|
|
|
}
|
2014-05-27 17:02:14 +04:00
|
|
|
|
2020-11-02 14:58:41 +03:00
|
|
|
void trace_init_file(void)
|
2014-05-27 17:02:14 +04:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
2021-02-09 17:57:58 +03:00
|
|
|
st_set_trace_file(trace_opts_file);
|
2020-08-16 20:46:10 +03:00
|
|
|
if (init_trace_on_startup) {
|
|
|
|
st_set_trace_file_enabled(true);
|
|
|
|
}
|
2016-01-07 16:55:30 +03:00
|
|
|
#elif defined CONFIG_TRACE_LOG
|
2020-02-25 15:47:00 +03:00
|
|
|
/*
|
|
|
|
* If both the simple and the log backends are enabled, "--trace file"
|
|
|
|
* only applies to the simple backend; use "-D" for the log
|
|
|
|
* backend. However we should only override -D if we actually have
|
|
|
|
* something to override it with.
|
2016-01-07 16:55:30 +03:00
|
|
|
*/
|
2021-02-09 17:57:58 +03:00
|
|
|
if (trace_opts_file) {
|
|
|
|
qemu_set_log_filename(trace_opts_file, &error_fatal);
|
2020-02-25 15:47:00 +03:00
|
|
|
}
|
2014-05-27 17:02:14 +04:00
|
|
|
#else
|
2021-02-09 17:57:58 +03:00
|
|
|
if (trace_opts_file) {
|
2018-07-04 06:17:27 +03:00
|
|
|
fprintf(stderr, "error: --trace file=...: "
|
2014-05-27 17:02:14 +04:00
|
|
|
"option not supported by the selected tracing backends\n");
|
2016-01-07 16:55:24 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool trace_init_backends(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
|
|
|
if (!st_init()) {
|
|
|
|
fprintf(stderr, "failed to initialize simple tracing backend.\n");
|
2014-05-27 17:02:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TRACE_FTRACE
|
|
|
|
if (!ftrace_init()) {
|
|
|
|
fprintf(stderr, "failed to initialize ftrace backend.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-04 16:44:14 +03:00
|
|
|
#ifdef CONFIG_TRACE_SYSLOG
|
|
|
|
openlog(NULL, LOG_PID, LOG_DAEMON);
|
|
|
|
#endif
|
|
|
|
|
2014-05-27 17:02:14 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-17 17:44:10 +03:00
|
|
|
|
2023-10-04 15:00:19 +03:00
|
|
|
void trace_opt_parse(const char *optstr)
|
2016-06-17 17:44:10 +03:00
|
|
|
{
|
|
|
|
QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
|
2023-10-04 15:00:19 +03:00
|
|
|
optstr, true);
|
2016-06-17 17:44:10 +03:00
|
|
|
if (!opts) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (qemu_opt_get(opts, "enable")) {
|
|
|
|
trace_enable_events(qemu_opt_get(opts, "enable"));
|
|
|
|
}
|
|
|
|
trace_init_events(qemu_opt_get(opts, "events"));
|
2020-08-16 20:46:10 +03:00
|
|
|
init_trace_on_startup = true;
|
2021-02-09 17:57:58 +03:00
|
|
|
g_free(trace_opts_file);
|
|
|
|
trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
|
2016-06-17 17:44:10 +03:00
|
|
|
qemu_opts_del(opts);
|
|
|
|
}
|
2016-10-04 16:35:53 +03:00
|
|
|
|
|
|
|
uint32_t trace_get_vcpu_event_count(void)
|
|
|
|
{
|
2016-10-04 16:35:54 +03:00
|
|
|
return next_vcpu_id;
|
2016-10-04 16:35:53 +03:00
|
|
|
}
|