Import QEMU upstream snapshot d2e570c

Upstream: https://gitlab.com/qemu-project/qemu.git

Upstream-Commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
This commit is contained in:
2026-08-31 02:15:30 +02:00
commit cf256aa081
11315 changed files with 3598369 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2011-2016 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef TRACE__CONTROL_INTERNAL_H
#define TRACE__CONTROL_INTERNAL_H
extern int trace_events_enabled_count;
static inline bool trace_event_is_pattern(const char *str)
{
assert(str != NULL);
return strchr(str, '*') != NULL;
}
static inline uint32_t trace_event_get_id(TraceEvent *ev)
{
assert(ev != NULL);
return ev->id;
}
static inline const char * trace_event_get_name(TraceEvent *ev)
{
assert(ev != NULL);
return ev->name;
}
static inline bool trace_event_get_state_static(TraceEvent *ev)
{
assert(ev != NULL);
return ev->sstate;
}
/* it's on fast path, avoid consistency checks (asserts) */
#define trace_event_get_state_dynamic_by_id(id) \
(unlikely(trace_events_enabled_count) && _ ## id ## _DSTATE)
static inline bool trace_event_get_state_dynamic(TraceEvent *ev)
{
return unlikely(trace_events_enabled_count) && *ev->dstate;
}
void trace_event_register_group(TraceEvent **events);
#endif /* TRACE__CONTROL_INTERNAL_H */
+54
View File
@@ -0,0 +1,54 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2014-2017 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "trace/control.h"
void trace_event_set_state_dynamic_init(TraceEvent *ev, bool state)
{
bool state_pre;
assert(trace_event_get_state_static(ev));
/*
* We ignore the "vcpu" property here, since no vCPUs have been created
* yet. Then dstate can only be 1 or 0.
*/
state_pre = *ev->dstate;
if (state_pre != state) {
if (state) {
trace_events_enabled_count++;
*ev->dstate = 1;
} else {
trace_events_enabled_count--;
*ev->dstate = 0;
}
}
}
void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
{
assert(trace_event_get_state_static(ev));
/*
* There is no longer a "vcpu" property, dstate can only be 1 or
* 0. With it, we haven't instantiated any vCPU yet, so we will
* set a global state instead, and trace_init_vcpu will reconcile
* it afterwards.
*/
bool state_pre = *ev->dstate;
if (state_pre != state) {
if (state) {
trace_events_enabled_count++;
*ev->dstate = 1;
} else {
trace_events_enabled_count--;
*ev->dstate = 0;
}
}
}
+311
View File
@@ -0,0 +1,311 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2011-2016 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "trace/control.h"
#include "qemu/help_option.h"
#include "qemu/option.h"
#ifdef CONFIG_TRACE_SIMPLE
#include "trace/simple.h"
#endif
#ifdef CONFIG_TRACE_FTRACE
#include "trace/ftrace.h"
#endif
#ifdef CONFIG_TRACE_LOG
#include "qemu/log.h"
#endif
#ifdef CONFIG_TRACE_SYSLOG
#include <syslog.h>
#endif
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/config-file.h"
#include "monitor/monitor.h"
int trace_events_enabled_count;
typedef struct TraceEventGroup {
TraceEvent **events;
} TraceEventGroup;
static TraceEventGroup *event_groups;
static size_t nevent_groups;
static uint32_t next_id;
static uint32_t next_vcpu_id;
#ifdef CONFIG_TRACE_SIMPLE
static bool init_trace_on_startup;
#endif
static char *trace_opts_file;
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 */ }
},
};
void trace_event_register_group(TraceEvent **events)
{
size_t i;
for (i = 0; events[i] != NULL; i++) {
events[i]->id = next_id++;
}
event_groups = g_renew(TraceEventGroup, event_groups, nevent_groups + 1);
event_groups[nevent_groups].events = events;
nevent_groups++;
#ifdef CONFIG_TRACE_SIMPLE
st_init_group(nevent_groups - 1);
#endif
}
TraceEvent *trace_event_name(const char *name)
{
assert(name != NULL);
TraceEventIter iter;
TraceEvent *ev;
trace_event_iter_init_all(&iter);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (strcmp(trace_event_get_name(ev), name) == 0) {
return ev;
}
}
return NULL;
}
void trace_event_iter_init_all(TraceEventIter *iter)
{
iter->event = 0;
iter->group = 0;
iter->group_id = -1;
iter->pattern = NULL;
}
void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern)
{
trace_event_iter_init_all(iter);
iter->pattern = pattern;
}
void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id)
{
trace_event_iter_init_all(iter);
iter->group_id = group_id;
}
TraceEvent *trace_event_iter_next(TraceEventIter *iter)
{
while (iter->group < nevent_groups &&
event_groups[iter->group].events[iter->event] != NULL) {
TraceEvent *ev = event_groups[iter->group].events[iter->event];
size_t group = iter->group;
iter->event++;
if (event_groups[iter->group].events[iter->event] == NULL) {
iter->event = 0;
iter->group++;
}
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;
}
return ev;
}
return NULL;
}
void trace_list_events(FILE *f)
{
TraceEventIter iter;
TraceEvent *ev;
trace_event_iter_init_all(&iter);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
fprintf(f, "%s\n", trace_event_get_name(ev));
}
#ifdef CONFIG_TRACE_DTRACE
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",
g_get_prgname());
#endif
}
static void do_trace_enable_events(const char *line_buf)
{
const bool enable = ('-' != line_buf[0]);
const char *line_ptr = enable ? line_buf : line_buf + 1;
TraceEventIter iter;
TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(line_ptr);
trace_event_iter_init_pattern(&iter, line_ptr);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (!trace_event_get_state_static(ev)) {
if (!is_pattern) {
warn_report("trace event '%s' is not traceable",
line_ptr);
return;
}
continue;
}
/* start tracing */
trace_event_set_state_dynamic(ev, enable);
if (!is_pattern) {
return;
}
}
if (!is_pattern) {
warn_report("trace event '%s' does not exist",
line_ptr);
}
}
void trace_enable_events(const char *line_buf)
{
if (is_help_option(line_buf)) {
trace_list_events(stdout);
if (monitor_cur() == NULL) {
exit(0);
}
} else {
do_trace_enable_events(line_buf);
}
}
static void trace_init_events(const char *fname)
{
Location loc;
FILE *fp;
char line_buf[1024];
size_t line_idx = 0;
if (fname == NULL) {
return;
}
loc_push_none(&loc);
loc_set_file(fname, 0);
fp = fopen(fname, "r");
if (!fp) {
error_report("%s", strerror(errno));
exit(1);
}
while (fgets(line_buf, sizeof(line_buf), fp)) {
loc_set_file(fname, ++line_idx);
size_t len = strlen(line_buf);
if (len > 1) { /* skip empty lines */
line_buf[len - 1] = '\0';
if ('#' == line_buf[0]) { /* skip commented lines */
continue;
}
trace_enable_events(line_buf);
}
}
if (fclose(fp) != 0) {
loc_set_file(fname, 0);
error_report("%s", strerror(errno));
exit(1);
}
loc_pop(&loc);
}
void trace_init_file(void)
{
#ifdef CONFIG_TRACE_SIMPLE
st_set_trace_file(trace_opts_file);
if (init_trace_on_startup) {
st_set_trace_file_enabled(true);
}
#elif defined CONFIG_TRACE_LOG
/*
* 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.
*/
if (trace_opts_file) {
qemu_set_log_filename(trace_opts_file, &error_fatal);
}
#else
if (trace_opts_file) {
fprintf(stderr, "error: --trace file=...: "
"option not supported by the selected tracing backends\n");
exit(1);
}
#endif
}
bool trace_init_backends(void)
{
#ifdef CONFIG_TRACE_SIMPLE
if (!st_init()) {
fprintf(stderr, "failed to initialize simple tracing backend.\n");
return false;
}
#endif
#ifdef CONFIG_TRACE_FTRACE
if (!ftrace_init()) {
fprintf(stderr, "failed to initialize ftrace backend.\n");
return false;
}
#endif
#ifdef CONFIG_TRACE_SYSLOG
openlog(NULL, LOG_PID, LOG_DAEMON);
#endif
return true;
}
void trace_opt_parse(const char *optstr)
{
QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
optstr, true);
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"));
#ifdef CONFIG_TRACE_SIMPLE
init_trace_on_startup = true;
#endif
g_free(trace_opts_file);
trace_opts_file = g_strdup(qemu_opt_get(opts, "file"));
qemu_opts_del(opts);
}
uint32_t trace_get_vcpu_event_count(void)
{
return next_vcpu_id;
}
+216
View File
@@ -0,0 +1,216 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2011-2016 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef TRACE__CONTROL_H
#define TRACE__CONTROL_H
#include "event-internal.h"
typedef struct TraceEventIter {
/* iter state */
size_t event;
size_t group;
/* filter conditions */
size_t group_id;
const char *pattern;
} TraceEventIter;
/**
* trace_event_iter_init_all:
* @iter: the event iterator struct
*
* Initialize the event iterator struct @iter,
* for all events.
*/
void trace_event_iter_init_all(TraceEventIter *iter);
/**
* trace_event_iter_init_pattern:
* @iter: the event iterator struct
* @pattern: pattern to filter events on name
*
* Initialize the event iterator struct @iter,
* using @pattern to filter out events
* with non-matching names.
*/
void trace_event_iter_init_pattern(TraceEventIter *iter, const char *pattern);
/**
* trace_event_iter_init_group:
* @iter: the event iterator struct
* @group_id: group_id to filter events by group.
*
* Initialize the event iterator struct @iter,
* using @group_id to filter for events in the group.
*/
void trace_event_iter_init_group(TraceEventIter *iter, size_t group_id);
/**
* trace_event_iter_next:
* @iter: the event iterator struct
*
* Get the next event, if any. When this returns NULL,
* the iterator should no longer be used.
*
* Returns: the next event, or NULL if no more events exist
*/
TraceEvent *trace_event_iter_next(TraceEventIter *iter);
/**
* trace_event_name:
* @id: Event name.
*
* Search an event by its name.
*
* Returns: pointer to #TraceEvent or NULL if not found.
*/
TraceEvent *trace_event_name(const char *name);
/**
* trace_event_is_pattern:
*
* Whether the given string is an event name pattern.
*/
static bool trace_event_is_pattern(const char *str);
/**
* trace_event_get_id:
*
* Get the identifier of an event.
*/
static uint32_t trace_event_get_id(TraceEvent *ev);
/**
* trace_event_get_name:
*
* Get the name of an event.
*/
static const char * trace_event_get_name(TraceEvent *ev);
/**
* trace_event_get_state:
* @id: Event identifier name.
*
* Get the tracing state of an event, both static and the QEMU dynamic state.
*
* If the event has the disabled property, the check will have no performance
* impact.
*/
#define trace_event_get_state(id) \
((id ##_ENABLED) && trace_event_get_state_dynamic_by_id(id))
/**
* trace_event_get_state_backends:
* @id: Event identifier name.
*
* Get the tracing state of an event, both static and dynamic state from all
* compiled-in backends.
*
* If the event has the disabled property, the check will have no performance
* impact.
*
* Returns: true if at least one backend has the event enabled and the event
* does not have the disabled property.
*/
#define trace_event_get_state_backends(id) \
((id ##_ENABLED) && id ##_BACKEND_DSTATE())
/**
* trace_event_get_state_static:
* @id: Event identifier.
*
* Get the static tracing state of an event.
*
* Use the define 'TRACE_${EVENT_NAME}_ENABLED' for compile-time checks (it will
* be set to 1 or 0 according to the presence of the disabled property).
*/
static bool trace_event_get_state_static(TraceEvent *ev);
/**
* trace_event_get_state_dynamic:
*
* Get the dynamic tracing state of an event.
*
* If the event has the 'vcpu' property, gets the OR'ed state of all vCPUs.
*/
static bool trace_event_get_state_dynamic(TraceEvent *ev);
/**
* trace_event_set_state_dynamic:
*
* Set the dynamic tracing state of an event.
*
* If the event has the 'vcpu' property, sets the state on all vCPUs.
*
* Pre-condition: trace_event_get_state_static(ev) == true
*/
void trace_event_set_state_dynamic(TraceEvent *ev, bool state);
/**
* trace_init_backends:
*
* Initialize the tracing backend.
*
* Returns: Whether the backends could be successfully initialized.
*/
bool trace_init_backends(void);
/**
* trace_init_file:
*
* Record the name of the output file for the tracing backend.
* Exits if no selected backend does not support specifying the
* output file, and a file was specified with "-trace file=...".
*/
void trace_init_file(void);
/**
* trace_list_events:
* @f: Where to send output.
*
* List all available events.
*/
void trace_list_events(FILE *f);
/**
* trace_enable_events:
* @line_buf: A string with a glob pattern of events to be enabled or,
* if the string starts with '-', disabled.
*
* Enable or disable matching events.
*/
void trace_enable_events(const char *line_buf);
/**
* Definition of QEMU options describing trace subsystem configuration
*/
extern QemuOptsList qemu_trace_opts;
/**
* trace_opt_parse:
* @optstr: A string argument of --trace command line argument
*
* Initialize tracing subsystem.
*/
void trace_opt_parse(const char *optstr);
/**
* trace_get_vcpu_event_count:
*
* Return the number of known vcpu-specific events
*/
uint32_t trace_get_vcpu_event_count(void);
#include "control-internal.h"
#endif /* TRACE__CONTROL_H */
+42
View File
@@ -0,0 +1,42 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2012-2016 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef TRACE__EVENT_INTERNAL_H
#define TRACE__EVENT_INTERNAL_H
/*
* Special value for TraceEvent.vcpu_id field to indicate
* that the event is not VCPU specific
*/
#define TRACE_VCPU_EVENT_NONE ((uint32_t)-1)
/**
* TraceEvent:
* @id: Unique event identifier.
* @name: Event name.
* @sstate: Static tracing state.
* @dstate: Dynamic tracing state
*
* Interpretation of @dstate depends on whether the event has the 'vcpu'
* property:
* - false: Boolean value indicating whether the event is active.
* - true : Integral counting the number of vCPUs that have this event enabled.
*
* Opaque generic description of a tracing event.
*/
typedef struct TraceEvent {
uint32_t id;
const char * name;
const bool sstate;
uint16_t *dstate;
} TraceEvent;
void trace_event_set_state_dynamic_init(TraceEvent *ev, bool state);
#endif /* TRACE__EVENT_INTERNAL_H */
+110
View File
@@ -0,0 +1,110 @@
/*
* Ftrace trace backend
*
* Copyright (C) 2013 Hitachi, Ltd.
* Created by Eiichi Tsukata <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include "trace/control.h"
#include "trace/ftrace.h"
int trace_marker_fd;
static int find_mount(char *mount_point, const char *fstype)
{
char type[100];
FILE *fp;
int ret = 0;
fp = fopen("/proc/mounts", "r");
if (fp == NULL) {
return 0;
}
while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
mount_point, type) == 2) {
if (strcmp(type, fstype) == 0) {
ret = 1;
break;
}
}
fclose(fp);
return ret;
}
void ftrace_write(const char *fmt, ...)
{
char ftrace_buf[MAX_TRACE_STRLEN];
int unused __attribute__ ((unused));
int trlen;
va_list ap;
va_start(ap, fmt);
trlen = vsnprintf(ftrace_buf, MAX_TRACE_STRLEN, fmt, ap);
va_end(ap);
trlen = MIN(trlen, MAX_TRACE_STRLEN - 1);
unused = write(trace_marker_fd, ftrace_buf, trlen);
}
bool ftrace_init(void)
{
char mount_point[PATH_MAX];
char path[PATH_MAX];
int tracefs_found;
int trace_fd = -1;
const char *subdir = "";
tracefs_found = find_mount(mount_point, "tracefs");
if (!tracefs_found) {
tracefs_found = find_mount(mount_point, "debugfs");
subdir = "/tracing";
}
if (tracefs_found) {
if (snprintf(path, PATH_MAX, "%s%s/tracing_on", mount_point, subdir)
>= sizeof(path)) {
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
return false;
}
trace_fd = open(path, O_WRONLY);
if (trace_fd < 0) {
if (errno == EACCES) {
trace_marker_fd = open("/dev/null", O_WRONLY);
if (trace_marker_fd != -1) {
return true;
}
}
perror("Could not open ftrace 'tracing_on' file");
return false;
} else {
if (write(trace_fd, "1", 1) < 0) {
perror("Could not write to 'tracing_on' file");
close(trace_fd);
return false;
}
close(trace_fd);
}
if (snprintf(path, PATH_MAX, "%s%s/trace_marker", mount_point, subdir)
>= sizeof(path)) {
fprintf(stderr, "Using tracefs mountpoint would exceed PATH_MAX\n");
return false;
}
trace_marker_fd = open(path, O_WRONLY);
if (trace_marker_fd < 0) {
perror("Could not open ftrace 'trace_marker' file");
return false;
}
} else {
fprintf(stderr, "tracefs is not mounted\n");
return false;
}
return true;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef TRACE_FTRACE_H
#define TRACE_FTRACE_H
#define MAX_TRACE_STRLEN 512
#define _STR(x) #x
#define STR(x) _STR(x)
extern int trace_marker_fd;
bool ftrace_init(void);
G_GNUC_PRINTF(1, 2) void ftrace_write(const char *fmt, ...);
#endif /* TRACE_FTRACE_H */
+99
View File
@@ -0,0 +1,99 @@
system_ss.add(files('control-target.c', 'trace-hmp-cmds.c'))
trace_rs_targets = []
trace_events_files = []
foreach item : [ '.' ] + trace_events_subdirs + qapi_trace_events
if item in qapi_trace_events
trace_events_file = item
group_name = fs.name(item).underscorify()
else
trace_events_file = meson.project_source_root() / item / 'trace-events'
group_name = item == '.' ? 'root' : item.underscorify()
endif
trace_events_files += [ trace_events_file ]
group = '--group=' + group_name
fmt = '@0@-' + group_name + '.@1@'
trace_h = custom_target(fmt.format('trace', 'h'),
output: fmt.format('trace', 'h'),
input: trace_events_file,
command: [ tracetool, group, '--format=h', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
genh += trace_h
trace_c = custom_target(fmt.format('trace', 'c'),
output: fmt.format('trace', 'c'),
input: trace_events_file,
command: [ tracetool, group, '--format=c', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
trace_rs = custom_target(fmt.format('trace', 'rs'),
output: fmt.format('trace', 'rs'),
input: trace_events_file,
command: [ tracetool, group, '--format=rs', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
if 'ust' in get_option('trace_backends')
trace_ust_h = custom_target(fmt.format('trace-ust', 'h'),
output: fmt.format('trace-ust', 'h'),
input: trace_events_file,
command: [ tracetool, group, '--format=ust-events-h', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
trace_ss.add(trace_ust_h, lttng)
genh += trace_ust_h
endif
trace_ss.add(trace_h, trace_c)
trace_rs_targets += trace_rs
if 'dtrace' in get_option('trace_backends')
trace_dtrace = custom_target(fmt.format('trace-dtrace', 'dtrace'),
output: fmt.format('trace-dtrace', 'dtrace'),
input: trace_events_file,
command: [ tracetool, group, '--format=d', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
trace_dtrace_h = custom_target(fmt.format('trace-dtrace', 'h'),
output: fmt.format('trace-dtrace', 'h'),
input: trace_dtrace,
command: [ dtrace, '-DSTAP_SDT_V2', '-o', '@OUTPUT@', '-h', '-s', '@INPUT@' ])
trace_ss.add(trace_dtrace_h)
if host_machine.system() != 'darwin'
trace_dtrace_o = custom_target(fmt.format('trace-dtrace', 'o'),
output: fmt.format('trace-dtrace', 'o'),
input: trace_dtrace,
command: [ dtrace, '-DSTAP_SDT_V2', '-o', '@OUTPUT@', '-G', '-s', '@INPUT@' ])
trace_ss.add(trace_dtrace_o)
endif
genh += trace_dtrace_h
endif
endforeach
cat = [ python, '-c', 'import fileinput; [print(line, end="") for line in fileinput.input()]', '@INPUT@' ]
trace_events_all = custom_target('trace-events-all',
output: 'trace-events-all',
input: trace_events_files,
command: cat,
capture: true,
install: get_option('trace_backends') != [ 'nop' ],
install_dir: qemu_datadir)
if 'ust' in get_option('trace_backends')
trace_ust_all_h = custom_target('trace-ust-all.h',
output: 'trace-ust-all.h',
input: trace_events_files,
command: [ tracetool, '--group=all', '--format=ust-events-h', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
trace_ust_all_c = custom_target('trace-ust-all.c',
output: 'trace-ust-all.c',
input: trace_events_files,
command: [ tracetool, '--group=all', '--format=ust-events-c', '@INPUT@', '@OUTPUT@' ],
depend_files: tracetool_depends)
trace_ss.add(trace_ust_all_h, trace_ust_all_c)
genh += trace_ust_all_h
endif
if 'simple' in get_option('trace_backends')
trace_ss.add(files('simple.c'))
endif
if 'ftrace' in get_option('trace_backends')
trace_ss.add(files('ftrace.c'))
endif
trace_ss.add(files('control.c'))
if have_system or have_tools or have_ga
trace_ss.add(files('qmp.c'))
endif
+108
View File
@@ -0,0 +1,108 @@
/*
* QMP commands for tracing events.
*
* Copyright (C) 2014-2016 Lluís Vilanova <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-trace.h"
#include "control.h"
static bool check_events(bool ignore_unavailable, bool is_pattern,
const char *name, Error **errp)
{
if (!is_pattern) {
TraceEvent *ev = trace_event_name(name);
/* error for non-existing event */
if (ev == NULL) {
error_setg(errp, "unknown event \"%s\"", name);
return false;
}
/* error for unavailable event */
if (!ignore_unavailable && !trace_event_get_state_static(ev)) {
error_setg(errp, "event \"%s\" is disabled", name);
return false;
}
return true;
} else {
/* error for unavailable events */
TraceEventIter iter;
TraceEvent *ev;
trace_event_iter_init_pattern(&iter, name);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (!ignore_unavailable && !trace_event_get_state_static(ev)) {
error_setg(errp, "event \"%s\" is disabled", trace_event_get_name(ev));
return false;
}
}
return true;
}
}
TraceEventInfoList *qmp_trace_event_get_state(const char *name,
Error **errp)
{
TraceEventInfoList *events = NULL;
TraceEventIter iter;
TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(name);
/* Check events */
if (!check_events(true, is_pattern, name, errp)) {
return NULL;
}
/* Get states (all errors checked above) */
trace_event_iter_init_pattern(&iter, name);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
TraceEventInfo *value;
value = g_new(TraceEventInfo, 1);
value->name = g_strdup(trace_event_get_name(ev));
if (!trace_event_get_state_static(ev)) {
value->state = TRACE_EVENT_STATE_UNAVAILABLE;
} else {
if (trace_event_get_state_dynamic(ev)) {
value->state = TRACE_EVENT_STATE_ENABLED;
} else {
value->state = TRACE_EVENT_STATE_DISABLED;
}
}
QAPI_LIST_PREPEND(events, value);
}
return events;
}
void qmp_trace_event_set_state(const char *name, bool enable,
bool has_ignore_unavailable, bool ignore_unavailable,
Error **errp)
{
TraceEventIter iter;
TraceEvent *ev;
bool is_pattern = trace_event_is_pattern(name);
/* Check events */
if (!check_events(has_ignore_unavailable && ignore_unavailable,
is_pattern, name, errp)) {
return;
}
/* Apply changes (all errors checked above) */
trace_event_iter_init_pattern(&iter, name);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
if (!trace_event_get_state_static(ev)) {
continue;
}
trace_event_set_state_dynamic(ev, enable);
}
}
+436
View File
@@ -0,0 +1,436 @@
/*
* Simple trace backend
*
* Copyright IBM, Corp. 2010
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#ifndef _WIN32
#include <pthread.h>
#endif
#include "qemu/timer.h"
#include "trace/control.h"
#include "trace/simple.h"
#include "qemu/error-report.h"
#include "qemu/qemu-print.h"
/** Trace file header event ID, picked to avoid conflict with real event IDs */
#define HEADER_EVENT_ID (~(uint64_t)0)
/** Trace file magic number */
#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
/** Trace file version number, bump if format changes */
#define HEADER_VERSION 4
/** Records were dropped event ID */
#define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
/** Trace record is valid */
#define TRACE_RECORD_VALID ((uint64_t)1 << 63)
/*
* Trace records are written out by a dedicated thread. The thread waits for
* records to become available, writes them out, and then waits again.
*/
static GMutex trace_lock;
static GCond trace_available_cond;
static GCond trace_empty_cond;
static bool trace_available;
static bool trace_writeout_enabled;
enum {
TRACE_BUF_LEN = 4096 * 64,
TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
};
uint8_t trace_buf[TRACE_BUF_LEN];
static volatile gint trace_idx;
static unsigned int writeout_idx;
static volatile gint dropped_events;
static uint32_t trace_pid;
static FILE *trace_fp;
static char *trace_file_name;
#define TRACE_RECORD_TYPE_MAPPING 0
#define TRACE_RECORD_TYPE_EVENT 1
/* * Trace buffer entry */
typedef struct {
uint64_t event; /* event ID value */
uint64_t timestamp_ns;
uint32_t length; /* in bytes */
uint32_t pid;
uint64_t arguments[];
} TraceRecord;
typedef struct {
uint64_t header_event_id; /* HEADER_EVENT_ID */
uint64_t header_magic; /* HEADER_MAGIC */
uint64_t header_version; /* HEADER_VERSION */
} TraceLogHeader;
static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
static void clear_buffer_range(unsigned int idx, size_t len)
{
uint32_t num = 0;
while (num < len) {
if (idx >= TRACE_BUF_LEN) {
idx = idx % TRACE_BUF_LEN;
}
trace_buf[idx++] = 0;
num++;
}
}
/**
* Read a trace record from the trace buffer
*
* @idx Trace buffer index
* @record Trace record to fill
*
* Returns false if the record is not valid.
*/
static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
{
uint64_t event_flag = 0;
TraceRecord record;
/* read the event flag to see if its a valid record */
read_from_buffer(idx, &record, sizeof(event_flag));
if (!(record.event & TRACE_RECORD_VALID)) {
return false;
}
smp_rmb(); /* read memory barrier before accessing record */
/* read the record header to know record length */
read_from_buffer(idx, &record, sizeof(TraceRecord));
*recordptr = malloc(record.length); /* don't use g_malloc, can deadlock when traced */
/* make a copy of record to avoid being overwritten */
read_from_buffer(idx, *recordptr, record.length);
smp_rmb(); /* memory barrier before clearing valid flag */
(*recordptr)->event &= ~TRACE_RECORD_VALID;
/* clear the trace buffer range for consumed record otherwise any byte
* with its MSB set may be considered as a valid event id when the writer
* thread crosses this range of buffer again.
*/
clear_buffer_range(idx, record.length);
return true;
}
/**
* Kick writeout thread
*
* @wait Whether to wait for writeout thread to complete
*/
static void flush_trace_file(bool wait)
{
g_mutex_lock(&trace_lock);
trace_available = true;
g_cond_signal(&trace_available_cond);
if (wait) {
g_cond_wait(&trace_empty_cond, &trace_lock);
}
g_mutex_unlock(&trace_lock);
}
static void wait_for_trace_records_available(void)
{
g_mutex_lock(&trace_lock);
while (!(trace_available && trace_writeout_enabled)) {
g_cond_signal(&trace_empty_cond);
g_cond_wait(&trace_available_cond, &trace_lock);
}
trace_available = false;
g_mutex_unlock(&trace_lock);
}
static gpointer writeout_thread(gpointer opaque)
{
TraceRecord *recordptr;
union {
TraceRecord rec;
uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
} dropped;
unsigned int idx = 0;
int dropped_count;
size_t unused __attribute__ ((unused));
uint64_t type = TRACE_RECORD_TYPE_EVENT;
for (;;) {
wait_for_trace_records_available();
if (g_atomic_int_get(&dropped_events)) {
dropped.rec.event = DROPPED_EVENT_ID;
dropped.rec.timestamp_ns = get_clock();
dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t);
dropped.rec.pid = trace_pid;
do {
dropped_count = g_atomic_int_get(&dropped_events);
} while (!g_atomic_int_compare_and_exchange(&dropped_events,
dropped_count, 0));
dropped.rec.arguments[0] = dropped_count;
unused = fwrite(&type, sizeof(type), 1, trace_fp);
unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
}
while (get_trace_record(idx, &recordptr)) {
unused = fwrite(&type, sizeof(type), 1, trace_fp);
unused = fwrite(recordptr, recordptr->length, 1, trace_fp);
writeout_idx += recordptr->length;
free(recordptr); /* don't use g_free, can deadlock when traced */
idx = writeout_idx % TRACE_BUF_LEN;
}
fflush(trace_fp);
}
return NULL;
}
void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
{
rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
}
void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
{
/* Write string length first */
rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
/* Write actual string now */
rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
}
int trace_record_start(TraceBufferRecord *rec, uint32_t event, size_t datasize)
{
unsigned int idx, rec_off, old_idx, new_idx;
uint32_t rec_len = sizeof(TraceRecord) + datasize;
uint64_t event_u64 = event;
uint64_t timestamp_ns = get_clock();
do {
old_idx = g_atomic_int_get(&trace_idx);
smp_rmb();
new_idx = old_idx + rec_len;
if (new_idx - writeout_idx > TRACE_BUF_LEN) {
/* Trace Buffer Full, Event dropped ! */
g_atomic_int_inc(&dropped_events);
return -ENOSPC;
}
} while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
idx = old_idx % TRACE_BUF_LEN;
rec_off = idx;
rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid));
rec->tbuf_idx = idx;
rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
return 0;
}
static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
{
uint8_t *data_ptr = dataptr;
uint32_t x = 0;
while (x < size) {
if (idx >= TRACE_BUF_LEN) {
idx = idx % TRACE_BUF_LEN;
}
data_ptr[x++] = trace_buf[idx++];
}
}
static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
{
uint8_t *data_ptr = dataptr;
uint32_t x = 0;
while (x < size) {
if (idx >= TRACE_BUF_LEN) {
idx = idx % TRACE_BUF_LEN;
}
trace_buf[idx++] = data_ptr[x++];
}
return idx; /* most callers wants to know where to write next */
}
void trace_record_finish(TraceBufferRecord *rec)
{
TraceRecord record;
read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
smp_wmb(); /* write barrier before marking as valid */
record.event |= TRACE_RECORD_VALID;
write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
> TRACE_BUF_FLUSH_THRESHOLD) {
flush_trace_file(false);
}
}
static int st_write_event_mapping(TraceEventIter *iter)
{
uint64_t type = TRACE_RECORD_TYPE_MAPPING;
TraceEvent *ev;
while ((ev = trace_event_iter_next(iter)) != NULL) {
uint64_t id = trace_event_get_id(ev);
const char *name = trace_event_get_name(ev);
uint32_t len = strlen(name);
if (fwrite(&type, sizeof(type), 1, trace_fp) != 1 ||
fwrite(&id, sizeof(id), 1, trace_fp) != 1 ||
fwrite(&len, sizeof(len), 1, trace_fp) != 1 ||
fwrite(name, len, 1, trace_fp) != 1) {
return -1;
}
}
return 0;
}
/**
* Enable / disable tracing, return whether it was enabled.
*
* @enable: enable if %true, else disable.
*/
bool st_set_trace_file_enabled(bool enable)
{
TraceEventIter iter;
bool was_enabled = trace_fp;
if (enable == !!trace_fp) {
return was_enabled; /* no change */
}
/* Halt trace writeout */
flush_trace_file(true);
trace_writeout_enabled = false;
flush_trace_file(true);
if (enable) {
static const TraceLogHeader header = {
.header_event_id = HEADER_EVENT_ID,
.header_magic = HEADER_MAGIC,
/* Older log readers will check for version at next location */
.header_version = HEADER_VERSION,
};
trace_fp = fopen(trace_file_name, "wb");
if (!trace_fp) {
return was_enabled;
}
trace_event_iter_init_all(&iter);
if (fwrite(&header, sizeof header, 1, trace_fp) != 1 ||
st_write_event_mapping(&iter) < 0) {
fclose(trace_fp);
trace_fp = NULL;
return was_enabled;
}
/* Resume trace writeout */
trace_writeout_enabled = true;
flush_trace_file(false);
} else {
fclose(trace_fp);
trace_fp = NULL;
}
return was_enabled;
}
/**
* Set the name of a trace file
*
* @file The trace file name or NULL for the default name-<pid> set at
* config time
*/
void st_set_trace_file(const char *file)
{
bool saved_enable = st_set_trace_file_enabled(false);
g_free(trace_file_name);
if (!file) {
/* Type cast needed for Windows where getpid() returns an int. */
trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE "-" FMT_pid, (pid_t)getpid());
} else {
trace_file_name = g_strdup(file);
}
st_set_trace_file_enabled(saved_enable);
}
void st_print_trace_file_status(void)
{
qemu_printf("Trace file \"%s\" %s.\n",
trace_file_name, trace_fp ? "on" : "off");
}
void st_flush_trace_buffer(void)
{
flush_trace_file(true);
}
/* Helper function to create a thread with signals blocked. Use glib's
* portable threads since QEMU abstractions cannot be used due to reentrancy in
* the tracer. Also note the signal masking on POSIX hosts so that the thread
* does not steal signals when the rest of the program wants them blocked.
*/
static GThread *trace_thread_create(GThreadFunc fn)
{
GThread *thread;
#ifndef _WIN32
sigset_t set, oldset;
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, &oldset);
#endif
thread = g_thread_new("trace-thread", fn, NULL);
#ifndef _WIN32
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
#endif
return thread;
}
bool st_init(void)
{
GThread *thread;
trace_pid = getpid();
thread = trace_thread_create(writeout_thread);
if (!thread) {
warn_report("unable to initialize simple trace backend");
return false;
}
atexit(st_flush_trace_buffer);
return true;
}
void st_init_group(size_t group)
{
TraceEventIter iter;
if (!trace_writeout_enabled) {
return;
}
trace_event_iter_init_group(&iter, group);
st_write_event_mapping(&iter);
}
+52
View File
@@ -0,0 +1,52 @@
/*
* Simple trace backend
*
* Copyright IBM, Corp. 2010
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
*/
#ifndef TRACE_SIMPLE_H
#define TRACE_SIMPLE_H
void st_print_trace_file_status(void);
bool st_set_trace_file_enabled(bool enable);
void st_set_trace_file(const char *file);
bool st_init(void);
void st_init_group(size_t group);
void st_flush_trace_buffer(void);
typedef struct {
unsigned int tbuf_idx;
unsigned int rec_off;
} TraceBufferRecord;
/* Note for hackers: Make sure MAX_TRACE_LEN < sizeof(uint32_t) */
#define MAX_TRACE_STRLEN 512
/**
* Initialize a trace record and claim space for it in the buffer
*
* @arglen number of bytes required for arguments
*/
int trace_record_start(TraceBufferRecord *rec, uint32_t id, size_t arglen);
/**
* Append a 64-bit argument to a trace record
*/
void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val);
/**
* Append a string argument to a trace record
*/
void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen);
/**
* Mark a trace record completed
*
* Don't append any more arguments to the trace record after calling this.
*/
void trace_record_finish(TraceBufferRecord *rec);
#endif /* TRACE_SIMPLE_H */
+137
View File
@@ -0,0 +1,137 @@
/*
* HMP commands related to tracing
*
* Copyright (c) 2003-2004 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "monitor/hmp.h"
#include "monitor/hmp-completion.h"
#include "monitor/monitor.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-trace.h"
#include "qobject/qdict.h"
#include "trace/control.h"
#ifdef CONFIG_TRACE_SIMPLE
#include "trace/simple.h"
#endif
void hmp_trace_event(Monitor *mon, const QDict *qdict)
{
const char *tp_name = qdict_get_str(qdict, "name");
bool new_state = qdict_get_bool(qdict, "option");
Error *local_err = NULL;
qmp_trace_event_set_state(tp_name, new_state,
true, true, &local_err);
if (local_err) {
error_report_err(local_err);
}
}
#ifdef CONFIG_TRACE_SIMPLE
void hmp_trace_file(Monitor *mon, const QDict *qdict)
{
const char *op = qdict_get_try_str(qdict, "op");
const char *arg = qdict_get_try_str(qdict, "arg");
if (!op) {
st_print_trace_file_status();
} else if (!strcmp(op, "on")) {
st_set_trace_file_enabled(true);
} else if (!strcmp(op, "off")) {
st_set_trace_file_enabled(false);
} else if (!strcmp(op, "flush")) {
st_flush_trace_buffer();
} else if (!strcmp(op, "set")) {
if (arg) {
st_set_trace_file(arg);
}
} else {
monitor_printf(mon, "unexpected argument \"%s\"\n", op);
hmp_help_cmd(mon, "trace-file");
}
}
#endif
void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
{
const char *name = qdict_get_try_str(qdict, "name");
TraceEventInfoList *events;
TraceEventInfoList *elem;
Error *local_err = NULL;
if (name == NULL) {
name = "*";
}
events = qmp_trace_event_get_state(name, &local_err);
if (local_err) {
error_report_err(local_err);
return;
}
for (elem = events; elem != NULL; elem = elem->next) {
monitor_printf(mon, "%s : state %u\n",
elem->value->name,
elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
}
qapi_free_TraceEventInfoList(events);
}
void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
{
size_t len;
len = strlen(str);
readline_set_completion_index(rs, len);
if (nb_args == 2) {
TraceEventIter iter;
TraceEvent *ev;
char *pattern = g_strdup_printf("%s*", str);
trace_event_iter_init_pattern(&iter, pattern);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
readline_add_completion(rs, trace_event_get_name(ev));
}
g_free(pattern);
}
}
void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
{
size_t len;
len = strlen(str);
readline_set_completion_index(rs, len);
if (nb_args == 2) {
TraceEventIter iter;
TraceEvent *ev;
char *pattern = g_strdup_printf("%s*", str);
trace_event_iter_init_pattern(&iter, pattern);
while ((ev = trace_event_iter_next(&iter)) != NULL) {
readline_add_completion(rs, trace_event_get_name(ev));
}
g_free(pattern);
} else if (nb_args == 3) {
readline_add_completion_of(rs, str, "on");
readline_add_completion_of(rs, str, "off");
}
}