#include <signal.h>
#include <pthread.h>
#endif
-#include "qemu-timer.h"
+#include "qemu/timer.h"
#include "trace.h"
#include "trace/control.h"
+#include "trace/simple.h"
/** Trace file header event ID */
#define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */
#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
/** Trace file version number, bump if format changes */
-#define HEADER_VERSION 2
+#define HEADER_VERSION 3
/** Records were dropped event ID */
#define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
* 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 GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
-static GCond *trace_available_cond;
-static GCond *trace_empty_cond;
+static CompatGMutex trace_lock;
+static CompatGCond trace_available_cond;
+static CompatGCond trace_empty_cond;
+
static bool trace_available;
static bool trace_writeout_enabled;
};
uint8_t trace_buf[TRACE_BUF_LEN];
-static unsigned int trace_idx;
+static volatile gint trace_idx;
static unsigned int writeout_idx;
-static uint64_t dropped_events;
+static volatile gint dropped_events;
+static uint32_t trace_pid;
static FILE *trace_fp;
-static char *trace_file_name = NULL;
+static char *trace_file_name;
/* * Trace buffer entry */
typedef struct {
uint64_t event; /* TraceEventID */
uint64_t timestamp_ns;
uint32_t length; /* in bytes */
- uint32_t reserved; /* unused */
- uint8_t arguments[];
+ 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 */
-} TraceRecordHeader;
+} TraceLogHeader;
static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
*/
static void flush_trace_file(bool wait)
{
- g_static_mutex_lock(&trace_lock);
+ g_mutex_lock(&trace_lock);
trace_available = true;
- g_cond_signal(trace_available_cond);
+ g_cond_signal(&trace_available_cond);
if (wait) {
- g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock));
+ g_cond_wait(&trace_empty_cond, &trace_lock);
}
- g_static_mutex_unlock(&trace_lock);
+ g_mutex_unlock(&trace_lock);
}
static void wait_for_trace_records_available(void)
{
- g_static_mutex_lock(&trace_lock);
+ g_mutex_lock(&trace_lock);
while (!(trace_available && trace_writeout_enabled)) {
- g_cond_signal(trace_empty_cond);
- g_cond_wait(trace_available_cond,
- g_static_mutex_get_mutex(&trace_lock));
+ g_cond_signal(&trace_empty_cond);
+ g_cond_wait(&trace_available_cond, &trace_lock);
}
trace_available = false;
- g_static_mutex_unlock(&trace_lock);
+ g_mutex_unlock(&trace_lock);
}
static gpointer writeout_thread(gpointer opaque)
uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
} dropped;
unsigned int idx = 0;
- uint64_t dropped_count;
+ int dropped_count;
size_t unused __attribute__ ((unused));
for (;;) {
wait_for_trace_records_available();
- if (dropped_events) {
+ 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(dropped_events),
- dropped.rec.reserved = 0;
- while (1) {
- dropped_count = dropped_events;
- if (g_atomic_int_compare_and_exchange((gint *)&dropped_events,
- dropped_count, 0)) {
- break;
- }
- }
- memcpy(dropped.rec.arguments, &dropped_count, sizeof(uint64_t));
+ 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(&dropped.rec, dropped.rec.length, 1, trace_fp);
}
{
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();
- while (1) {
- old_idx = trace_idx;
+ 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((gint *)&dropped_events);
+ g_atomic_int_inc(&dropped_events);
return -ENOSPC;
}
-
- if (g_atomic_int_compare_and_exchange((gint *)&trace_idx,
- old_idx, new_idx)) {
- break;
- }
- }
+ } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
idx = old_idx % TRACE_BUF_LEN;
- /* To check later if threshold crossed */
- rec->next_tbuf_idx = new_idx % TRACE_BUF_LEN;
rec_off = idx;
- rec_off = write_to_buffer(rec_off, (uint8_t*)&event, sizeof(event));
- rec_off = write_to_buffer(rec_off, (uint8_t*)×tamp_ns, sizeof(timestamp_ns));
- rec_off = write_to_buffer(rec_off, (uint8_t*)&rec_len, sizeof(rec_len));
+ rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
+ rec_off = write_to_buffer(rec_off, ×tamp_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;
void trace_record_finish(TraceBufferRecord *rec)
{
- uint8_t temp_rec[sizeof(TraceRecord)];
- TraceRecord *record = (TraceRecord *) temp_rec;
- read_from_buffer(rec->tbuf_idx, temp_rec, sizeof(TraceRecord));
+ 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, temp_rec, sizeof(TraceRecord));
+ record.event |= TRACE_RECORD_VALID;
+ write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
- if ((trace_idx - writeout_idx) > TRACE_BUF_FLUSH_THRESHOLD) {
+ if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
+ > TRACE_BUF_FLUSH_THRESHOLD) {
flush_trace_file(false);
}
}
flush_trace_file(true);
if (enable) {
- static const TraceRecordHeader header = {
+ static const TraceLogHeader header = {
.header_event_id = HEADER_EVENT_ID,
.header_magic = HEADER_MAGIC,
/* Older log readers will check for version at next location */
{
st_set_trace_file_enabled(false);
- free(trace_file_name);
+ g_free(trace_file_name);
if (!file) {
- if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) {
- trace_file_name = NULL;
- return false;
- }
+ trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
} else {
- if (asprintf(&trace_file_name, "%s", file) < 0) {
- trace_file_name = NULL;
- return false;
- }
+ trace_file_name = g_strdup_printf("%s", file);
}
st_set_trace_file_enabled(true);
flush_trace_file(true);
}
-void trace_print_events(FILE *stream, fprintf_function stream_printf)
-{
- 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);
- }
-}
-
-bool trace_event_set_state(const char *name, bool state)
-{
- unsigned int i;
- unsigned int len;
- bool wildcard = false;
- bool matched = false;
-
- len = strlen(name);
- if (len > 0 && name[len - 1] == '*') {
- wildcard = true;
- len -= 1;
- }
- for (i = 0; i < NR_TRACE_EVENTS; i++) {
- if (wildcard) {
- if (!strncmp(trace_list[i].tp_name, name, len)) {
- trace_list[i].state = state;
- matched = true;
- }
- continue;
- }
- if (!strcmp(trace_list[i].tp_name, name)) {
- trace_list[i].state = state;
- return true;
- }
- }
- return matched;
-}
-
/* 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
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, &oldset);
#endif
- thread = g_thread_create(fn, NULL, FALSE, NULL);
+
+ thread = g_thread_new("trace-thread", fn, NULL);
+
#ifndef _WIN32
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
#endif
return thread;
}
-bool trace_backend_init(const char *events, const char *file)
+bool st_init(const char *file)
{
GThread *thread;
- if (!g_thread_supported()) {
-#if !GLIB_CHECK_VERSION(2, 31, 0)
- g_thread_init(NULL);
-#else
- fprintf(stderr, "glib threading failed to initialize.\n");
- exit(1);
-#endif
- }
-
- trace_available_cond = g_cond_new();
- trace_empty_cond = g_cond_new();
+ trace_pid = getpid();
thread = trace_thread_create(writeout_thread);
if (!thread) {
}
atexit(st_flush_trace_buffer);
- trace_backend_init_events(events);
st_set_trace_file(file);
return true;
}