Skip to content

Commit

Permalink
trace: Provide a detailed event control interface
Browse files Browse the repository at this point in the history
This interface decouples event obtaining from interaction.

Events can be obtained through three different methods:

* identifier
* name
* simple wildcard pattern

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
  • Loading branch information
Lluís Vilanova authored and stefanhaRH committed Mar 28, 2013
1 parent 45be2f5 commit b1bae81
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 58 deletions.
44 changes: 18 additions & 26 deletions docs/tracing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,37 @@ respectively. This ensures portability between 32- and 64-bit platforms.

== Generic interface and monitor commands ==

You can programmatically query and control the dynamic state of trace events
through a backend-agnostic interface:
You can programmatically query and control the state of trace events through a
backend-agnostic interface provided by the header "trace/control.h".

* trace_print_events
Note that some of the backends do not provide an implementation for some parts
of this interface, in which case QEMU will just print a warning (please refer to
header "trace/control.h" to see which routines are backend-dependent).

* trace_event_set_state
Enables or disables trace events at runtime inside QEMU.
The function returns "true" if the state of the event has been successfully
changed, or "false" otherwise:

#include "trace/control.h"

trace_event_set_state("virtio_irq", true); /* enable */
[...]
trace_event_set_state("virtio_irq", false); /* disable */

Note that some of the backends do not provide an implementation for this
interface, in which case QEMU will just print a warning.

This functionality is also provided through monitor commands:
The state of events can also be queried and modified through monitor commands:

* info trace-events
View available trace events and their state. State 1 means enabled, state 0
means disabled.

* trace-event NAME on|off
Enable/disable a given trace event or a group of events having common prefix
through wildcard.
Enable/disable a given trace event or a group of events (using wildcards).

The "-trace events=<file>" command line argument can be used to enable the
events listed in <file> from the very beginning of the program. This file must
contain one event name per line.

A basic wildcard matching is supported in both the monitor command "trace
-event" and the events list file. That means you can enable/disable the events
having a common prefix in a batch. For example, virtio-blk trace events could
be enabled using:
trace-event virtio_blk_* on

If a line in the "-trace events=<file>" file begins with a '-', the trace event
will be disabled instead of enabled. This is useful when a wildcard was used
to enable an entire family of events but one noisy event needs to be disabled.

Wildcard matching is supported in both the monitor command "trace-event" and the
events list file. That means you can enable/disable the events having a common
prefix in a batch. For example, virtio-blk trace events could be enabled using
the following monitor command:

trace-event virtio_blk_* on

== Trace backends ==

The "tracetool" script automates tedious trace event code generation and also
Expand Down Expand Up @@ -263,3 +251,7 @@ guard such computations and avoid its compilation when the event is disabled:
}
return ptr;
}

You can check both if the event has been disabled and is dynamically enabled at
the same time using the 'trace_event_get_state' routine (see header
"trace/control.h" for more information).
67 changes: 67 additions & 0 deletions trace/control-internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2011-2012 Lluís Vilanova <vilanova@ac.upc.edu>
*
* 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

#include <string.h>


extern TraceEvent trace_events[];


static inline TraceEvent *trace_event_id(TraceEventID id)
{
assert(id < trace_event_count());
return &trace_events[id];
}

static inline TraceEventID trace_event_count(void)
{
return TRACE_EVENT_COUNT;
}

static inline bool trace_event_is_pattern(const char *str)
{
assert(str != NULL);
return strchr(str, '*') != NULL;
}

static inline TraceEventID 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;
}

static inline bool trace_event_get_state_dynamic(TraceEvent *ev)
{
assert(ev != NULL);
return ev->dstate;
}

static inline void trace_event_set_state_dynamic(TraceEvent *ev, bool state)
{
assert(ev != NULL);
assert(trace_event_get_state_static(ev));
return trace_event_set_state_dynamic_backend(ev, state);
}

#endif /* TRACE__CONTROL_INTERNAL_H */
106 changes: 93 additions & 13 deletions trace/control.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,86 @@
/*
* Interface for configuring and controlling the state of tracing events.
*
* Copyright (C) 2011 Lluís Vilanova <vilanova@ac.upc.edu>
* Copyright (C) 2011-2012 Lluís Vilanova <vilanova@ac.upc.edu>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
* 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 "trace/control.h"


void trace_backend_init_events(const char *fname)
TraceEvent *trace_event_name(const char *name)
{
assert(name != NULL);

TraceEventID i;
for (i = 0; i < trace_event_count(); i++) {
TraceEvent *ev = trace_event_id(i);
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;
}
}

TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
{
int ret;
assert(pat != NULL);

TraceEventID i;

if (ev == NULL) {
i = -1;
} else {
i = trace_event_get_id(ev);
}
i++;

while (i < trace_event_count()) {
TraceEvent *res = trace_event_id(i);
if (pattern_glob(pat, trace_event_get_name(res))) {
return res;
}
i++;
}

return NULL;
}

void trace_backend_init_events(const char *fname)
{
if (fname == NULL) {
return;
}
Expand All @@ -32,15 +99,28 @@ void trace_backend_init_events(const char *fname)
if ('#' == line_buf[0]) { /* skip commented lines */
continue;
}
if ('-' == line_buf[0]) {
ret = trace_event_set_state(line_buf+1, false);
const bool enable = ('-' != line_buf[0]);
char *line_ptr = enable ? line_buf : line_buf + 1;
if (trace_event_is_pattern(line_ptr)) {
TraceEvent *ev = NULL;
while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
if (trace_event_get_state_static(ev)) {
trace_event_set_state_dynamic(ev, enable);
}
}
} else {
ret = trace_event_set_state(line_buf, true);
}
if (!ret) {
fprintf(stderr,
"error: trace event '%s' does not exist\n", line_buf);
exit(1);
TraceEvent *ev = trace_event_name(line_ptr);
if (ev == NULL) {
fprintf(stderr,
"error: trace event '%s' does not exist\n", line_ptr);
exit(1);
}
if (!trace_event_get_state_static(ev)) {
fprintf(stderr,
"error: trace event '%s' is not traceable\n", line_ptr);
exit(1);
}
trace_event_set_state_dynamic(ev, enable);
}
}
}
Expand Down
Loading

0 comments on commit b1bae81

Please sign in to comment.