2010-05-22 22:24:51 +04:00
|
|
|
/*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2011-09-05 11:30:17 +04:00
|
|
|
#ifndef _WIN32
|
2011-02-26 21:38:39 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
2011-09-05 11:30:17 +04:00
|
|
|
#endif
|
2010-10-23 19:24:07 +04:00
|
|
|
#include "qemu-timer.h"
|
2010-05-22 22:24:51 +04:00
|
|
|
#include "trace.h"
|
2011-08-31 22:31:03 +04:00
|
|
|
#include "trace/control.h"
|
2010-05-22 22:24:51 +04:00
|
|
|
|
|
|
|
/** Trace file header event ID */
|
|
|
|
#define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */
|
|
|
|
|
|
|
|
/** Trace file magic number */
|
|
|
|
#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
|
|
|
|
|
|
|
|
/** Trace file version number, bump if format changes */
|
|
|
|
#define HEADER_VERSION 0
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
/** 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)
|
|
|
|
|
2010-05-22 22:24:51 +04:00
|
|
|
/** Trace buffer entry */
|
|
|
|
typedef struct {
|
|
|
|
uint64_t event;
|
|
|
|
uint64_t timestamp_ns;
|
|
|
|
uint64_t x1;
|
|
|
|
uint64_t x2;
|
|
|
|
uint64_t x3;
|
|
|
|
uint64_t x4;
|
|
|
|
uint64_t x5;
|
|
|
|
uint64_t x6;
|
|
|
|
} TraceRecord;
|
|
|
|
|
|
|
|
enum {
|
2011-02-26 21:38:39 +03:00
|
|
|
TRACE_BUF_LEN = 4096,
|
|
|
|
TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
|
2010-05-22 22:24:51 +04:00
|
|
|
};
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
/*
|
|
|
|
* Trace records are written out by a dedicated thread. The thread waits for
|
|
|
|
* records to become available, writes them out, and then waits again.
|
|
|
|
*/
|
2011-09-05 11:30:17 +04:00
|
|
|
static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
|
|
|
|
static GCond *trace_available_cond;
|
|
|
|
static GCond *trace_empty_cond;
|
2011-02-26 21:38:39 +03:00
|
|
|
static bool trace_available;
|
|
|
|
static bool trace_writeout_enabled;
|
|
|
|
|
2010-05-22 22:24:51 +04:00
|
|
|
static TraceRecord trace_buf[TRACE_BUF_LEN];
|
|
|
|
static unsigned int trace_idx;
|
|
|
|
static FILE *trace_fp;
|
2010-07-13 12:26:33 +04:00
|
|
|
static char *trace_file_name = NULL;
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2010-07-13 12:26:33 +04:00
|
|
|
/**
|
2011-02-26 21:38:39 +03:00
|
|
|
* 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.
|
2010-07-13 12:26:33 +04:00
|
|
|
*/
|
2011-02-26 21:38:39 +03:00
|
|
|
static bool get_trace_record(unsigned int idx, TraceRecord *record)
|
2010-07-13 12:26:32 +04:00
|
|
|
{
|
2011-02-26 21:38:39 +03:00
|
|
|
if (!(trace_buf[idx].event & TRACE_RECORD_VALID)) {
|
|
|
|
return false;
|
2010-07-13 12:26:32 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
__sync_synchronize(); /* read memory barrier before accessing record */
|
|
|
|
|
|
|
|
*record = trace_buf[idx];
|
|
|
|
record->event &= ~TRACE_RECORD_VALID;
|
2010-07-13 12:26:33 +04:00
|
|
|
return true;
|
2010-07-13 12:26:32 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
/**
|
|
|
|
* Kick writeout thread
|
|
|
|
*
|
|
|
|
* @wait Whether to wait for writeout thread to complete
|
|
|
|
*/
|
|
|
|
static void flush_trace_file(bool wait)
|
2010-05-22 22:24:51 +04:00
|
|
|
{
|
2011-09-05 11:30:17 +04:00
|
|
|
g_static_mutex_lock(&trace_lock);
|
2011-02-26 21:38:39 +03:00
|
|
|
trace_available = true;
|
2011-09-05 11:30:17 +04:00
|
|
|
g_cond_signal(trace_available_cond);
|
2010-07-13 12:26:33 +04:00
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
if (wait) {
|
2011-09-05 11:30:17 +04:00
|
|
|
g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock));
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
2011-02-26 21:38:39 +03:00
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
g_static_mutex_unlock(&trace_lock);
|
2010-07-13 12:26:33 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
static void wait_for_trace_records_available(void)
|
2010-07-13 12:26:33 +04:00
|
|
|
{
|
2011-09-05 11:30:17 +04:00
|
|
|
g_static_mutex_lock(&trace_lock);
|
2011-02-26 21:38:39 +03:00
|
|
|
while (!(trace_available && trace_writeout_enabled)) {
|
2011-09-05 11:30:17 +04:00
|
|
|
g_cond_signal(trace_empty_cond);
|
|
|
|
g_cond_wait(trace_available_cond,
|
|
|
|
g_static_mutex_get_mutex(&trace_lock));
|
2010-07-13 12:26:33 +04:00
|
|
|
}
|
2011-02-26 21:38:39 +03:00
|
|
|
trace_available = false;
|
2011-09-05 11:30:17 +04:00
|
|
|
g_static_mutex_unlock(&trace_lock);
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
static gpointer writeout_thread(gpointer opaque)
|
2010-05-22 22:24:51 +04:00
|
|
|
{
|
2011-02-26 21:38:39 +03:00
|
|
|
TraceRecord record;
|
|
|
|
unsigned int writeout_idx = 0;
|
|
|
|
unsigned int num_available, idx;
|
2011-07-24 01:21:14 +04:00
|
|
|
size_t unused __attribute__ ((unused));
|
2011-02-26 21:38:39 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
wait_for_trace_records_available();
|
|
|
|
|
|
|
|
num_available = trace_idx - writeout_idx;
|
|
|
|
if (num_available > TRACE_BUF_LEN) {
|
|
|
|
record = (TraceRecord){
|
|
|
|
.event = DROPPED_EVENT_ID,
|
|
|
|
.x1 = num_available,
|
|
|
|
};
|
|
|
|
unused = fwrite(&record, sizeof(record), 1, trace_fp);
|
|
|
|
writeout_idx += num_available;
|
|
|
|
}
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
idx = writeout_idx % TRACE_BUF_LEN;
|
|
|
|
while (get_trace_record(idx, &record)) {
|
|
|
|
trace_buf[idx].event = 0; /* clear valid bit */
|
|
|
|
unused = fwrite(&record, sizeof(record), 1, trace_fp);
|
|
|
|
idx = ++writeout_idx % TRACE_BUF_LEN;
|
|
|
|
}
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
fflush(trace_fp);
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
2011-02-26 21:38:39 +03:00
|
|
|
return NULL;
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3,
|
|
|
|
uint64_t x4, uint64_t x5, uint64_t x6)
|
|
|
|
{
|
2011-02-26 21:38:39 +03:00
|
|
|
unsigned int idx;
|
|
|
|
uint64_t timestamp;
|
2010-05-22 22:24:51 +04:00
|
|
|
|
2010-06-24 15:34:53 +04:00
|
|
|
if (!trace_list[event].state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
timestamp = get_clock();
|
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN;
|
2011-02-26 21:38:39 +03:00
|
|
|
trace_buf[idx] = (TraceRecord){
|
|
|
|
.event = event,
|
|
|
|
.timestamp_ns = timestamp,
|
|
|
|
.x1 = x1,
|
|
|
|
.x2 = x2,
|
|
|
|
.x3 = x3,
|
|
|
|
.x4 = x4,
|
|
|
|
.x5 = x5,
|
|
|
|
.x6 = x6,
|
|
|
|
};
|
|
|
|
__sync_synchronize(); /* write barrier before marking as valid */
|
|
|
|
trace_buf[idx].event |= TRACE_RECORD_VALID;
|
|
|
|
|
|
|
|
if ((idx + 1) % TRACE_BUF_FLUSH_THRESHOLD == 0) {
|
|
|
|
flush_trace_file(false);
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace0(TraceEventID event)
|
|
|
|
{
|
|
|
|
trace(event, 0, 0, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace1(TraceEventID event, uint64_t x1)
|
|
|
|
{
|
|
|
|
trace(event, x1, 0, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace2(TraceEventID event, uint64_t x1, uint64_t x2)
|
|
|
|
{
|
|
|
|
trace(event, x1, x2, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace3(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3)
|
|
|
|
{
|
|
|
|
trace(event, x1, x2, x3, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace4(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4)
|
|
|
|
{
|
|
|
|
trace(event, x1, x2, x3, x4, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace5(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5)
|
|
|
|
{
|
|
|
|
trace(event, x1, x2, x3, x4, x5, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace6(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6)
|
|
|
|
{
|
|
|
|
trace(event, x1, x2, x3, x4, x5, x6);
|
|
|
|
}
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
void st_set_trace_file_enabled(bool enable)
|
|
|
|
{
|
|
|
|
if (enable == !!trace_fp) {
|
|
|
|
return; /* no change */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Halt trace writeout */
|
|
|
|
flush_trace_file(true);
|
|
|
|
trace_writeout_enabled = false;
|
|
|
|
flush_trace_file(true);
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
static const TraceRecord header = {
|
|
|
|
.event = HEADER_EVENT_ID,
|
|
|
|
.timestamp_ns = HEADER_MAGIC,
|
|
|
|
.x1 = HEADER_VERSION,
|
|
|
|
};
|
|
|
|
|
2011-09-05 21:31:21 +04:00
|
|
|
trace_fp = fopen(trace_file_name, "wb");
|
2011-02-26 21:38:39 +03:00
|
|
|
if (!trace_fp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
|
|
|
|
fclose(trace_fp);
|
|
|
|
trace_fp = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resume trace writeout */
|
|
|
|
trace_writeout_enabled = true;
|
|
|
|
flush_trace_file(false);
|
|
|
|
} else {
|
|
|
|
fclose(trace_fp);
|
|
|
|
trace_fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 22:24:51 +04:00
|
|
|
/**
|
2011-02-26 21:38:39 +03:00
|
|
|
* Set the name of a trace file
|
|
|
|
*
|
|
|
|
* @file The trace file name or NULL for the default name-<pid> set at
|
|
|
|
* config time
|
2010-05-22 22:24:51 +04:00
|
|
|
*/
|
2011-02-26 21:38:39 +03:00
|
|
|
bool st_set_trace_file(const char *file)
|
2010-05-22 22:24:51 +04:00
|
|
|
{
|
2011-02-26 21:38:39 +03:00
|
|
|
st_set_trace_file_enabled(false);
|
|
|
|
|
|
|
|
free(trace_file_name);
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) {
|
|
|
|
trace_file_name = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (asprintf(&trace_file_name, "%s", file) < 0) {
|
|
|
|
trace_file_name = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
st_set_trace_file_enabled(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
|
|
|
|
{
|
|
|
|
stream_printf(stream, "Trace file \"%s\" %s.\n",
|
|
|
|
trace_file_name, trace_fp ? "on" : "off");
|
2010-05-22 22:24:51 +04:00
|
|
|
}
|
2010-06-24 15:34:53 +04:00
|
|
|
|
|
|
|
void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
for (i = 0; i < TRACE_BUF_LEN; i++) {
|
|
|
|
TraceRecord record;
|
|
|
|
|
|
|
|
if (!get_trace_record(i, &record)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-20 20:41:36 +04:00
|
|
|
stream_printf(stream, "Event %" PRIu64 " : %" PRIx64 " %" PRIx64
|
|
|
|
" %" PRIx64 " %" PRIx64 " %" PRIx64 " %" PRIx64 "\n",
|
2011-02-26 21:38:39 +03:00
|
|
|
record.event, record.x1, record.x2,
|
|
|
|
record.x3, record.x4, record.x5,
|
|
|
|
record.x6);
|
2010-06-24 15:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:31:18 +04:00
|
|
|
void st_flush_trace_buffer(void)
|
|
|
|
{
|
|
|
|
flush_trace_file(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace_print_events(FILE *stream, fprintf_function stream_printf)
|
2010-06-24 15:34:53 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NR_TRACE_EVENTS; i++) {
|
|
|
|
stream_printf(stream, "%s [Event ID %u] : state %u\n",
|
|
|
|
trace_list[i].tp_name, i, trace_list[i].state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:31:18 +04:00
|
|
|
bool trace_event_set_state(const char *name, bool state)
|
2010-06-24 15:34:53 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2011-10-31 07:29:04 +04:00
|
|
|
unsigned int len;
|
|
|
|
bool wildcard = false;
|
|
|
|
bool matched = false;
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
if (len > 0 && name[len - 1] == '*') {
|
|
|
|
wildcard = true;
|
|
|
|
len -= 1;
|
|
|
|
}
|
2010-06-24 15:34:53 +04:00
|
|
|
for (i = 0; i < NR_TRACE_EVENTS; i++) {
|
2011-10-31 07:29:04 +04:00
|
|
|
if (wildcard) {
|
|
|
|
if (!strncmp(trace_list[i].tp_name, name, len)) {
|
|
|
|
trace_list[i].state = state;
|
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-26 21:38:39 +03:00
|
|
|
if (!strcmp(trace_list[i].tp_name, name)) {
|
2011-08-31 22:31:18 +04:00
|
|
|
trace_list[i].state = state;
|
2011-02-26 21:38:39 +03:00
|
|
|
return true;
|
2010-06-24 15:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-31 07:29:04 +04:00
|
|
|
return matched;
|
2011-02-26 21:38:39 +03:00
|
|
|
}
|
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
/* 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)
|
2010-06-24 15:34:53 +04:00
|
|
|
{
|
2011-09-05 11:30:17 +04:00
|
|
|
GThread *thread;
|
|
|
|
#ifndef _WIN32
|
2011-02-26 21:38:39 +03:00
|
|
|
sigset_t set, oldset;
|
2010-06-24 15:34:53 +04:00
|
|
|
|
2011-02-26 21:38:39 +03:00
|
|
|
sigfillset(&set);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &set, &oldset);
|
2011-09-05 11:30:17 +04:00
|
|
|
#endif
|
2012-03-08 10:20:37 +04:00
|
|
|
thread = g_thread_create(fn, NULL, FALSE, NULL);
|
2011-09-05 11:30:17 +04:00
|
|
|
#ifndef _WIN32
|
2011-02-26 21:38:39 +03:00
|
|
|
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
2011-09-05 11:30:17 +04:00
|
|
|
#endif
|
2011-02-26 21:38:39 +03:00
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool trace_backend_init(const char *events, const char *file)
|
|
|
|
{
|
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
if (!g_thread_supported()) {
|
2011-12-20 15:41:04 +04:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 31, 0)
|
2011-09-05 11:30:17 +04:00
|
|
|
g_thread_init(NULL);
|
2011-12-20 15:41:04 +04:00
|
|
|
#else
|
|
|
|
fprintf(stderr, "glib threading failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
#endif
|
2011-09-05 11:30:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
trace_available_cond = g_cond_new();
|
|
|
|
trace_empty_cond = g_cond_new();
|
|
|
|
|
|
|
|
thread = trace_thread_create(writeout_thread);
|
|
|
|
if (!thread) {
|
2011-08-31 22:31:03 +04:00
|
|
|
fprintf(stderr, "warning: unable to initialize simple trace backend\n");
|
2011-09-05 11:30:17 +04:00
|
|
|
return false;
|
2010-06-24 15:34:53 +04:00
|
|
|
}
|
2011-02-26 21:38:39 +03:00
|
|
|
|
2011-09-05 11:30:17 +04:00
|
|
|
atexit(st_flush_trace_buffer);
|
|
|
|
trace_backend_init_events(events);
|
|
|
|
st_set_trace_file(file);
|
2011-03-13 23:14:30 +03:00
|
|
|
return true;
|
2010-06-24 15:34:53 +04:00
|
|
|
}
|