2011-09-01 02:31:31 +08:00
|
|
|
/*
|
|
|
|
* Interface for configuring and controlling the state of tracing events.
|
|
|
|
*
|
2016-07-11 18:53:41 +08:00
|
|
|
* Copyright (C) 2011-2016 Lluís Vilanova <vilanova@ac.upc.edu>
|
2011-09-01 02:31:31 +08:00
|
|
|
*
|
2013-03-05 21:47:38 +08: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-09-01 02:31:31 +08:00
|
|
|
*/
|
|
|
|
|
2016-01-30 01:50:05 +08:00
|
|
|
#include "qemu/osdep.h"
|
2011-09-01 02:31:31 +08:00
|
|
|
#include "trace/control.h"
|
2016-03-21 01:16:19 +08:00
|
|
|
#include "qemu/help_option.h"
|
2014-05-27 21:02:14 +08:00
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
|
|
|
#include "trace/simple.h"
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_TRACE_FTRACE
|
|
|
|
#include "trace/ftrace.h"
|
|
|
|
#endif
|
2016-01-07 21:55:30 +08:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
#include "qemu/log.h"
|
|
|
|
#endif
|
2016-08-04 21:44:14 +08:00
|
|
|
#ifdef CONFIG_TRACE_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
2016-06-16 01:27:16 +08:00
|
|
|
#include "qapi/error.h"
|
2014-06-02 14:34:10 +08:00
|
|
|
#include "qemu/error-report.h"
|
2016-06-17 22:44:10 +08:00
|
|
|
#include "qemu/config-file.h"
|
2016-03-16 19:36:51 +08:00
|
|
|
#include "monitor/monitor.h"
|
2011-09-01 02:31:31 +08:00
|
|
|
|
2015-10-28 14:06:26 +08:00
|
|
|
int trace_events_enabled_count;
|
|
|
|
|
2016-06-17 22:44:10 +08: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 */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-05 21:47:38 +08:00
|
|
|
TraceEvent *trace_event_name(const char *name)
|
|
|
|
{
|
|
|
|
assert(name != NULL);
|
|
|
|
|
2016-10-04 21:35:43 +08:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
|
|
|
trace_event_iter_init(&iter, NULL);
|
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
2013-03-05 21:47:38 +08:00
|
|
|
if (strcmp(trace_event_get_name(ev), name) == 0) {
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pattern_glob(const char *pat, const char *ev)
|
|
|
|
{
|
|
|
|
while (*pat != '\0' && *ev != '\0') {
|
|
|
|
if (*pat == *ev) {
|
|
|
|
pat++;
|
|
|
|
ev++;
|
|
|
|
}
|
|
|
|
else if (*pat == '*') {
|
|
|
|
if (pattern_glob(pat, ev+1)) {
|
|
|
|
return true;
|
|
|
|
} else if (pattern_glob(pat+1, ev)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*pat == '*') {
|
|
|
|
pat++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pat == '\0' && *ev == '\0') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-04 21:35:42 +08:00
|
|
|
void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
|
|
|
|
{
|
|
|
|
iter->event = 0;
|
|
|
|
iter->pattern = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraceEvent *trace_event_iter_next(TraceEventIter *iter)
|
|
|
|
{
|
2016-10-04 21:35:49 +08:00
|
|
|
while (trace_events[iter->event] != NULL) {
|
2016-10-04 21:35:48 +08:00
|
|
|
TraceEvent *ev = trace_events[iter->event];
|
2016-10-04 21:35:42 +08:00
|
|
|
iter->event++;
|
|
|
|
if (!iter->pattern ||
|
|
|
|
pattern_glob(iter->pattern,
|
|
|
|
trace_event_get_name(ev))) {
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:55:27 +08:00
|
|
|
void trace_list_events(void)
|
|
|
|
{
|
2016-10-04 21:35:43 +08:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
|
|
|
trace_event_iter_init(&iter, NULL);
|
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
|
|
|
fprintf(stderr, "%s\n", trace_event_get_name(ev));
|
2016-01-07 21:55:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_trace_enable_events(const char *line_buf)
|
2016-01-07 21:55:26 +08:00
|
|
|
{
|
|
|
|
const bool enable = ('-' != line_buf[0]);
|
|
|
|
const char *line_ptr = enable ? line_buf : line_buf + 1;
|
2016-10-04 21:35:43 +08:00
|
|
|
TraceEventIter iter;
|
|
|
|
TraceEvent *ev;
|
|
|
|
bool is_pattern = trace_event_is_pattern(line_ptr);
|
|
|
|
|
|
|
|
trace_event_iter_init(&iter, line_ptr);
|
|
|
|
while ((ev = trace_event_iter_next(&iter)) != NULL) {
|
|
|
|
if (!trace_event_get_state_static(ev)) {
|
|
|
|
if (!is_pattern) {
|
|
|
|
error_report("WARNING: trace event '%s' is not traceable",
|
|
|
|
line_ptr);
|
|
|
|
return;
|
2016-01-07 21:55:26 +08:00
|
|
|
}
|
2016-10-04 21:35:43 +08:00
|
|
|
continue;
|
2016-01-07 21:55:26 +08:00
|
|
|
}
|
2016-10-04 21:35:43 +08:00
|
|
|
|
|
|
|
/* start tracing */
|
|
|
|
trace_event_set_state_dynamic(ev, enable);
|
|
|
|
if (!is_pattern) {
|
|
|
|
return;
|
2016-01-07 21:55:26 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-04 21:35:43 +08:00
|
|
|
|
|
|
|
if (!is_pattern) {
|
|
|
|
error_report("WARNING: trace event '%s' does not exist",
|
|
|
|
line_ptr);
|
|
|
|
}
|
2016-01-07 21:55:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:55:27 +08:00
|
|
|
void trace_enable_events(const char *line_buf)
|
|
|
|
{
|
|
|
|
if (is_help_option(line_buf)) {
|
|
|
|
trace_list_events();
|
2016-03-16 19:36:51 +08:00
|
|
|
if (cur_mon == NULL) {
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-01-07 21:55:27 +08:00
|
|
|
} else {
|
|
|
|
do_trace_enable_events(line_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:44:10 +08:00
|
|
|
static void trace_init_events(const char *fname)
|
2013-03-05 21:47:38 +08:00
|
|
|
{
|
2014-06-02 14:34:10 +08:00
|
|
|
Location loc;
|
|
|
|
FILE *fp;
|
|
|
|
char line_buf[1024];
|
|
|
|
size_t line_idx = 0;
|
|
|
|
|
2011-09-01 02:31:31 +08:00
|
|
|
if (fname == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-02 14:34:10 +08:00
|
|
|
loc_push_none(&loc);
|
|
|
|
loc_set_file(fname, 0);
|
|
|
|
fp = fopen(fname, "r");
|
2011-09-01 02:31:31 +08:00
|
|
|
if (!fp) {
|
2014-06-02 14:34:10 +08:00
|
|
|
error_report("%s", strerror(errno));
|
2011-09-01 02:31:31 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (fgets(line_buf, sizeof(line_buf), fp)) {
|
2014-06-02 14:34:10 +08:00
|
|
|
loc_set_file(fname, ++line_idx);
|
2011-09-01 02:31:31 +08:00
|
|
|
size_t len = strlen(line_buf);
|
|
|
|
if (len > 1) { /* skip empty lines */
|
|
|
|
line_buf[len - 1] = '\0';
|
2012-06-14 12:41:40 +08:00
|
|
|
if ('#' == line_buf[0]) { /* skip commented lines */
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-07 21:55:26 +08:00
|
|
|
trace_enable_events(line_buf);
|
2011-09-01 02:31:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fclose(fp) != 0) {
|
2014-06-02 14:34:10 +08:00
|
|
|
loc_set_file(fname, 0);
|
|
|
|
error_report("%s", strerror(errno));
|
2011-09-01 02:31:31 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-06-02 14:34:10 +08:00
|
|
|
loc_pop(&loc);
|
2011-09-01 02:31:31 +08:00
|
|
|
}
|
2014-05-27 21:02:14 +08:00
|
|
|
|
2016-01-07 21:55:24 +08:00
|
|
|
void trace_init_file(const char *file)
|
2014-05-27 21:02:14 +08:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_TRACE_SIMPLE
|
2016-01-07 21:55:24 +08:00
|
|
|
st_set_trace_file(file);
|
2016-01-07 21:55:30 +08:00
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
if (file) {
|
2016-06-16 01:27:16 +08:00
|
|
|
qemu_set_log_filename(file, &error_fatal);
|
2016-01-07 21:55:30 +08:00
|
|
|
}
|
2014-05-27 21:02:14 +08:00
|
|
|
#else
|
|
|
|
if (file) {
|
|
|
|
fprintf(stderr, "error: -trace file=...: "
|
|
|
|
"option not supported by the selected tracing backends\n");
|
2016-01-07 21:55:24 +08: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 21:02:14 +08: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 21:44:14 +08:00
|
|
|
#ifdef CONFIG_TRACE_SYSLOG
|
|
|
|
openlog(NULL, LOG_PID, LOG_DAEMON);
|
|
|
|
#endif
|
|
|
|
|
2014-05-27 21:02:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-17 22:44:10 +08:00
|
|
|
|
|
|
|
char *trace_opt_parse(const char *optarg)
|
|
|
|
{
|
|
|
|
char *trace_file;
|
|
|
|
QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
|
|
|
|
optarg, 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"));
|
|
|
|
trace_file = g_strdup(qemu_opt_get(opts, "file"));
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
|
|
|
|
return trace_file;
|
|
|
|
}
|