1edb47ec4SLluís /* 2edb47ec4SLluís * Simple trace backend 3edb47ec4SLluís * 4edb47ec4SLluís * Copyright IBM, Corp. 2010 5edb47ec4SLluís * 6edb47ec4SLluís * This work is licensed under the terms of the GNU GPL, version 2. See 7edb47ec4SLluís * the COPYING file in the top-level directory. 8edb47ec4SLluís * 9edb47ec4SLluís */ 10edb47ec4SLluís 11edb47ec4SLluís #include <stdlib.h> 12edb47ec4SLluís #include <stdint.h> 13edb47ec4SLluís #include <stdio.h> 14edb47ec4SLluís #include <time.h> 1585aff158SStefan Hajnoczi #ifndef _WIN32 16edb47ec4SLluís #include <signal.h> 17edb47ec4SLluís #include <pthread.h> 1885aff158SStefan Hajnoczi #endif 19edb47ec4SLluís #include "qemu-timer.h" 20edb47ec4SLluís #include "trace.h" 21e4858974SLluís #include "trace/control.h" 22edb47ec4SLluís 23edb47ec4SLluís /** Trace file header event ID */ 24edb47ec4SLluís #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */ 25edb47ec4SLluís 26edb47ec4SLluís /** Trace file magic number */ 27edb47ec4SLluís #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL 28edb47ec4SLluís 29edb47ec4SLluís /** Trace file version number, bump if format changes */ 3062bab732SHarsh Prateek Bora #define HEADER_VERSION 2 31edb47ec4SLluís 32edb47ec4SLluís /** Records were dropped event ID */ 33edb47ec4SLluís #define DROPPED_EVENT_ID (~(uint64_t)0 - 1) 34edb47ec4SLluís 35edb47ec4SLluís /** Trace record is valid */ 36edb47ec4SLluís #define TRACE_RECORD_VALID ((uint64_t)1 << 63) 37edb47ec4SLluís 38edb47ec4SLluís /* 39edb47ec4SLluís * Trace records are written out by a dedicated thread. The thread waits for 40edb47ec4SLluís * records to become available, writes them out, and then waits again. 41edb47ec4SLluís */ 4285aff158SStefan Hajnoczi static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; 4385aff158SStefan Hajnoczi static GCond *trace_available_cond; 4485aff158SStefan Hajnoczi static GCond *trace_empty_cond; 45edb47ec4SLluís static bool trace_available; 46edb47ec4SLluís static bool trace_writeout_enabled; 47edb47ec4SLluís 4862bab732SHarsh Prateek Bora enum { 4962bab732SHarsh Prateek Bora TRACE_BUF_LEN = 4096 * 64, 5062bab732SHarsh Prateek Bora TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4, 5162bab732SHarsh Prateek Bora }; 5262bab732SHarsh Prateek Bora 5362bab732SHarsh Prateek Bora uint8_t trace_buf[TRACE_BUF_LEN]; 54edb47ec4SLluís static unsigned int trace_idx; 5562bab732SHarsh Prateek Bora static unsigned int writeout_idx; 5662bab732SHarsh Prateek Bora static uint64_t dropped_events; 57edb47ec4SLluís static FILE *trace_fp; 58edb47ec4SLluís static char *trace_file_name = NULL; 59edb47ec4SLluís 6062bab732SHarsh Prateek Bora /* * Trace buffer entry */ 6162bab732SHarsh Prateek Bora typedef struct { 6262bab732SHarsh Prateek Bora uint64_t event; /* TraceEventID */ 6362bab732SHarsh Prateek Bora uint64_t timestamp_ns; 6462bab732SHarsh Prateek Bora uint32_t length; /* in bytes */ 6562bab732SHarsh Prateek Bora uint32_t reserved; /* unused */ 6662bab732SHarsh Prateek Bora uint8_t arguments[]; 6762bab732SHarsh Prateek Bora } TraceRecord; 6862bab732SHarsh Prateek Bora 6962bab732SHarsh Prateek Bora typedef struct { 7062bab732SHarsh Prateek Bora uint64_t header_event_id; /* HEADER_EVENT_ID */ 7162bab732SHarsh Prateek Bora uint64_t header_magic; /* HEADER_MAGIC */ 7262bab732SHarsh Prateek Bora uint64_t header_version; /* HEADER_VERSION */ 738ae601e8SHarsh Prateek Bora } TraceLogHeader; 7462bab732SHarsh Prateek Bora 7562bab732SHarsh Prateek Bora 7662bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size); 7762bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size); 7862bab732SHarsh Prateek Bora 7962bab732SHarsh Prateek Bora static void clear_buffer_range(unsigned int idx, size_t len) 8062bab732SHarsh Prateek Bora { 8162bab732SHarsh Prateek Bora uint32_t num = 0; 8262bab732SHarsh Prateek Bora while (num < len) { 8362bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 8462bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 8562bab732SHarsh Prateek Bora } 8662bab732SHarsh Prateek Bora trace_buf[idx++] = 0; 8762bab732SHarsh Prateek Bora num++; 8862bab732SHarsh Prateek Bora } 8962bab732SHarsh Prateek Bora } 90edb47ec4SLluís /** 91edb47ec4SLluís * Read a trace record from the trace buffer 92edb47ec4SLluís * 93edb47ec4SLluís * @idx Trace buffer index 94edb47ec4SLluís * @record Trace record to fill 95edb47ec4SLluís * 96edb47ec4SLluís * Returns false if the record is not valid. 97edb47ec4SLluís */ 9862bab732SHarsh Prateek Bora static bool get_trace_record(unsigned int idx, TraceRecord **recordptr) 99edb47ec4SLluís { 10062bab732SHarsh Prateek Bora uint64_t event_flag = 0; 10162bab732SHarsh Prateek Bora TraceRecord record; 10262bab732SHarsh Prateek Bora /* read the event flag to see if its a valid record */ 10362bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(event_flag)); 10462bab732SHarsh Prateek Bora 10562bab732SHarsh Prateek Bora if (!(record.event & TRACE_RECORD_VALID)) { 106edb47ec4SLluís return false; 107edb47ec4SLluís } 108edb47ec4SLluís 10962bab732SHarsh Prateek Bora smp_rmb(); /* read memory barrier before accessing record */ 11062bab732SHarsh Prateek Bora /* read the record header to know record length */ 11162bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(TraceRecord)); 11262bab732SHarsh Prateek Bora *recordptr = malloc(record.length); /* dont use g_malloc, can deadlock when traced */ 11362bab732SHarsh Prateek Bora /* make a copy of record to avoid being overwritten */ 11462bab732SHarsh Prateek Bora read_from_buffer(idx, *recordptr, record.length); 11562bab732SHarsh Prateek Bora smp_rmb(); /* memory barrier before clearing valid flag */ 11662bab732SHarsh Prateek Bora (*recordptr)->event &= ~TRACE_RECORD_VALID; 11762bab732SHarsh Prateek Bora /* clear the trace buffer range for consumed record otherwise any byte 11862bab732SHarsh Prateek Bora * with its MSB set may be considered as a valid event id when the writer 11962bab732SHarsh Prateek Bora * thread crosses this range of buffer again. 12062bab732SHarsh Prateek Bora */ 12162bab732SHarsh Prateek Bora clear_buffer_range(idx, record.length); 122edb47ec4SLluís return true; 123edb47ec4SLluís } 124edb47ec4SLluís 125edb47ec4SLluís /** 126edb47ec4SLluís * Kick writeout thread 127edb47ec4SLluís * 128edb47ec4SLluís * @wait Whether to wait for writeout thread to complete 129edb47ec4SLluís */ 130edb47ec4SLluís static void flush_trace_file(bool wait) 131edb47ec4SLluís { 13285aff158SStefan Hajnoczi g_static_mutex_lock(&trace_lock); 133edb47ec4SLluís trace_available = true; 13485aff158SStefan Hajnoczi g_cond_signal(trace_available_cond); 135edb47ec4SLluís 136edb47ec4SLluís if (wait) { 13785aff158SStefan Hajnoczi g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); 138edb47ec4SLluís } 139edb47ec4SLluís 14085aff158SStefan Hajnoczi g_static_mutex_unlock(&trace_lock); 141edb47ec4SLluís } 142edb47ec4SLluís 143edb47ec4SLluís static void wait_for_trace_records_available(void) 144edb47ec4SLluís { 14585aff158SStefan Hajnoczi g_static_mutex_lock(&trace_lock); 146edb47ec4SLluís while (!(trace_available && trace_writeout_enabled)) { 14785aff158SStefan Hajnoczi g_cond_signal(trace_empty_cond); 14885aff158SStefan Hajnoczi g_cond_wait(trace_available_cond, 14985aff158SStefan Hajnoczi g_static_mutex_get_mutex(&trace_lock)); 150edb47ec4SLluís } 151edb47ec4SLluís trace_available = false; 15285aff158SStefan Hajnoczi g_static_mutex_unlock(&trace_lock); 153edb47ec4SLluís } 154edb47ec4SLluís 15585aff158SStefan Hajnoczi static gpointer writeout_thread(gpointer opaque) 156edb47ec4SLluís { 15762bab732SHarsh Prateek Bora TraceRecord *recordptr; 15862bab732SHarsh Prateek Bora union { 15962bab732SHarsh Prateek Bora TraceRecord rec; 16062bab732SHarsh Prateek Bora uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)]; 16162bab732SHarsh Prateek Bora } dropped; 16262bab732SHarsh Prateek Bora unsigned int idx = 0; 16362bab732SHarsh Prateek Bora uint64_t dropped_count; 164edb47ec4SLluís size_t unused __attribute__ ((unused)); 165edb47ec4SLluís 166edb47ec4SLluís for (;;) { 167edb47ec4SLluís wait_for_trace_records_available(); 168edb47ec4SLluís 16962bab732SHarsh Prateek Bora if (dropped_events) { 17062bab732SHarsh Prateek Bora dropped.rec.event = DROPPED_EVENT_ID, 17162bab732SHarsh Prateek Bora dropped.rec.timestamp_ns = get_clock(); 17262bab732SHarsh Prateek Bora dropped.rec.length = sizeof(TraceRecord) + sizeof(dropped_events), 17362bab732SHarsh Prateek Bora dropped.rec.reserved = 0; 17462bab732SHarsh Prateek Bora while (1) { 17562bab732SHarsh Prateek Bora dropped_count = dropped_events; 17662bab732SHarsh Prateek Bora if (g_atomic_int_compare_and_exchange((gint *)&dropped_events, 17762bab732SHarsh Prateek Bora dropped_count, 0)) { 17862bab732SHarsh Prateek Bora break; 17962bab732SHarsh Prateek Bora } 18062bab732SHarsh Prateek Bora } 18162bab732SHarsh Prateek Bora memcpy(dropped.rec.arguments, &dropped_count, sizeof(uint64_t)); 18262bab732SHarsh Prateek Bora unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp); 183edb47ec4SLluís } 184edb47ec4SLluís 18562bab732SHarsh Prateek Bora while (get_trace_record(idx, &recordptr)) { 18662bab732SHarsh Prateek Bora unused = fwrite(recordptr, recordptr->length, 1, trace_fp); 18762bab732SHarsh Prateek Bora writeout_idx += recordptr->length; 18862bab732SHarsh Prateek Bora free(recordptr); /* dont use g_free, can deadlock when traced */ 189edb47ec4SLluís idx = writeout_idx % TRACE_BUF_LEN; 190edb47ec4SLluís } 191edb47ec4SLluís 192edb47ec4SLluís fflush(trace_fp); 193edb47ec4SLluís } 194edb47ec4SLluís return NULL; 195edb47ec4SLluís } 196edb47ec4SLluís 19762bab732SHarsh Prateek Bora void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val) 198edb47ec4SLluís { 19962bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t)); 200edb47ec4SLluís } 201edb47ec4SLluís 20262bab732SHarsh Prateek Bora void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen) 20362bab732SHarsh Prateek Bora { 20462bab732SHarsh Prateek Bora /* Write string length first */ 20562bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen)); 20662bab732SHarsh Prateek Bora /* Write actual string now */ 20762bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen); 20862bab732SHarsh Prateek Bora } 209edb47ec4SLluís 21062bab732SHarsh Prateek Bora int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize) 21162bab732SHarsh Prateek Bora { 21262bab732SHarsh Prateek Bora unsigned int idx, rec_off, old_idx, new_idx; 21362bab732SHarsh Prateek Bora uint32_t rec_len = sizeof(TraceRecord) + datasize; 21462bab732SHarsh Prateek Bora uint64_t timestamp_ns = get_clock(); 21562bab732SHarsh Prateek Bora 21662bab732SHarsh Prateek Bora while (1) { 21762bab732SHarsh Prateek Bora old_idx = trace_idx; 21862bab732SHarsh Prateek Bora smp_rmb(); 21962bab732SHarsh Prateek Bora new_idx = old_idx + rec_len; 22062bab732SHarsh Prateek Bora 22162bab732SHarsh Prateek Bora if (new_idx - writeout_idx > TRACE_BUF_LEN) { 22262bab732SHarsh Prateek Bora /* Trace Buffer Full, Event dropped ! */ 22362bab732SHarsh Prateek Bora g_atomic_int_inc((gint *)&dropped_events); 22462bab732SHarsh Prateek Bora return -ENOSPC; 22562bab732SHarsh Prateek Bora } 22662bab732SHarsh Prateek Bora 22762bab732SHarsh Prateek Bora if (g_atomic_int_compare_and_exchange((gint *)&trace_idx, 22862bab732SHarsh Prateek Bora old_idx, new_idx)) { 22962bab732SHarsh Prateek Bora break; 23062bab732SHarsh Prateek Bora } 23162bab732SHarsh Prateek Bora } 23262bab732SHarsh Prateek Bora 23362bab732SHarsh Prateek Bora idx = old_idx % TRACE_BUF_LEN; 23462bab732SHarsh Prateek Bora 23562bab732SHarsh Prateek Bora rec_off = idx; 23683d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &event, sizeof(event)); 23783d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, ×tamp_ns, sizeof(timestamp_ns)); 23883d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len)); 23962bab732SHarsh Prateek Bora 24062bab732SHarsh Prateek Bora rec->tbuf_idx = idx; 24162bab732SHarsh Prateek Bora rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN; 24262bab732SHarsh Prateek Bora return 0; 24362bab732SHarsh Prateek Bora } 24462bab732SHarsh Prateek Bora 24562bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size) 24662bab732SHarsh Prateek Bora { 24762bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 24862bab732SHarsh Prateek Bora uint32_t x = 0; 24962bab732SHarsh Prateek Bora while (x < size) { 25062bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 25162bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 25262bab732SHarsh Prateek Bora } 25362bab732SHarsh Prateek Bora data_ptr[x++] = trace_buf[idx++]; 25462bab732SHarsh Prateek Bora } 25562bab732SHarsh Prateek Bora } 25662bab732SHarsh Prateek Bora 25762bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size) 25862bab732SHarsh Prateek Bora { 25962bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 26062bab732SHarsh Prateek Bora uint32_t x = 0; 26162bab732SHarsh Prateek Bora while (x < size) { 26262bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 26362bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 26462bab732SHarsh Prateek Bora } 26562bab732SHarsh Prateek Bora trace_buf[idx++] = data_ptr[x++]; 26662bab732SHarsh Prateek Bora } 26762bab732SHarsh Prateek Bora return idx; /* most callers wants to know where to write next */ 26862bab732SHarsh Prateek Bora } 26962bab732SHarsh Prateek Bora 27062bab732SHarsh Prateek Bora void trace_record_finish(TraceBufferRecord *rec) 27162bab732SHarsh Prateek Bora { 272*db8894f2SHarsh Prateek Bora TraceRecord record; 273*db8894f2SHarsh Prateek Bora read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 27462bab732SHarsh Prateek Bora smp_wmb(); /* write barrier before marking as valid */ 275*db8894f2SHarsh Prateek Bora record.event |= TRACE_RECORD_VALID; 276*db8894f2SHarsh Prateek Bora write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 27762bab732SHarsh Prateek Bora 27862bab732SHarsh Prateek Bora if ((trace_idx - writeout_idx) > TRACE_BUF_FLUSH_THRESHOLD) { 279edb47ec4SLluís flush_trace_file(false); 280edb47ec4SLluís } 281edb47ec4SLluís } 282edb47ec4SLluís 283edb47ec4SLluís void st_set_trace_file_enabled(bool enable) 284edb47ec4SLluís { 285edb47ec4SLluís if (enable == !!trace_fp) { 286edb47ec4SLluís return; /* no change */ 287edb47ec4SLluís } 288edb47ec4SLluís 289edb47ec4SLluís /* Halt trace writeout */ 290edb47ec4SLluís flush_trace_file(true); 291edb47ec4SLluís trace_writeout_enabled = false; 292edb47ec4SLluís flush_trace_file(true); 293edb47ec4SLluís 294edb47ec4SLluís if (enable) { 2958ae601e8SHarsh Prateek Bora static const TraceLogHeader header = { 29662bab732SHarsh Prateek Bora .header_event_id = HEADER_EVENT_ID, 29762bab732SHarsh Prateek Bora .header_magic = HEADER_MAGIC, 29862bab732SHarsh Prateek Bora /* Older log readers will check for version at next location */ 29962bab732SHarsh Prateek Bora .header_version = HEADER_VERSION, 300edb47ec4SLluís }; 301edb47ec4SLluís 3026c2a4074SStefan Hajnoczi trace_fp = fopen(trace_file_name, "wb"); 303edb47ec4SLluís if (!trace_fp) { 304edb47ec4SLluís return; 305edb47ec4SLluís } 306edb47ec4SLluís 307edb47ec4SLluís if (fwrite(&header, sizeof header, 1, trace_fp) != 1) { 308edb47ec4SLluís fclose(trace_fp); 309edb47ec4SLluís trace_fp = NULL; 310edb47ec4SLluís return; 311edb47ec4SLluís } 312edb47ec4SLluís 313edb47ec4SLluís /* Resume trace writeout */ 314edb47ec4SLluís trace_writeout_enabled = true; 315edb47ec4SLluís flush_trace_file(false); 316edb47ec4SLluís } else { 317edb47ec4SLluís fclose(trace_fp); 318edb47ec4SLluís trace_fp = NULL; 319edb47ec4SLluís } 320edb47ec4SLluís } 321edb47ec4SLluís 322edb47ec4SLluís /** 323edb47ec4SLluís * Set the name of a trace file 324edb47ec4SLluís * 325edb47ec4SLluís * @file The trace file name or NULL for the default name-<pid> set at 326edb47ec4SLluís * config time 327edb47ec4SLluís */ 328edb47ec4SLluís bool st_set_trace_file(const char *file) 329edb47ec4SLluís { 330edb47ec4SLluís st_set_trace_file_enabled(false); 331edb47ec4SLluís 332edb47ec4SLluís free(trace_file_name); 333edb47ec4SLluís 334edb47ec4SLluís if (!file) { 335edb47ec4SLluís if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) { 336edb47ec4SLluís trace_file_name = NULL; 337edb47ec4SLluís return false; 338edb47ec4SLluís } 339edb47ec4SLluís } else { 340edb47ec4SLluís if (asprintf(&trace_file_name, "%s", file) < 0) { 341edb47ec4SLluís trace_file_name = NULL; 342edb47ec4SLluís return false; 343edb47ec4SLluís } 344edb47ec4SLluís } 345edb47ec4SLluís 346edb47ec4SLluís st_set_trace_file_enabled(true); 347edb47ec4SLluís return true; 348edb47ec4SLluís } 349edb47ec4SLluís 350edb47ec4SLluís void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) 351edb47ec4SLluís { 352edb47ec4SLluís stream_printf(stream, "Trace file \"%s\" %s.\n", 353edb47ec4SLluís trace_file_name, trace_fp ? "on" : "off"); 354edb47ec4SLluís } 355edb47ec4SLluís 356fc764105SLluís void st_flush_trace_buffer(void) 357fc764105SLluís { 358fc764105SLluís flush_trace_file(true); 359fc764105SLluís } 360fc764105SLluís 361fc764105SLluís void trace_print_events(FILE *stream, fprintf_function stream_printf) 362edb47ec4SLluís { 363edb47ec4SLluís unsigned int i; 364edb47ec4SLluís 365edb47ec4SLluís for (i = 0; i < NR_TRACE_EVENTS; i++) { 366edb47ec4SLluís stream_printf(stream, "%s [Event ID %u] : state %u\n", 367edb47ec4SLluís trace_list[i].tp_name, i, trace_list[i].state); 368edb47ec4SLluís } 369edb47ec4SLluís } 370edb47ec4SLluís 371fc764105SLluís bool trace_event_set_state(const char *name, bool state) 372edb47ec4SLluís { 373edb47ec4SLluís unsigned int i; 374454e202dSMark Wu unsigned int len; 375454e202dSMark Wu bool wildcard = false; 376454e202dSMark Wu bool matched = false; 377edb47ec4SLluís 378454e202dSMark Wu len = strlen(name); 379454e202dSMark Wu if (len > 0 && name[len - 1] == '*') { 380454e202dSMark Wu wildcard = true; 381454e202dSMark Wu len -= 1; 382454e202dSMark Wu } 383edb47ec4SLluís for (i = 0; i < NR_TRACE_EVENTS; i++) { 384454e202dSMark Wu if (wildcard) { 385454e202dSMark Wu if (!strncmp(trace_list[i].tp_name, name, len)) { 386454e202dSMark Wu trace_list[i].state = state; 387454e202dSMark Wu matched = true; 388454e202dSMark Wu } 389454e202dSMark Wu continue; 390454e202dSMark Wu } 391edb47ec4SLluís if (!strcmp(trace_list[i].tp_name, name)) { 392fc764105SLluís trace_list[i].state = state; 393edb47ec4SLluís return true; 394edb47ec4SLluís } 395edb47ec4SLluís } 396454e202dSMark Wu return matched; 397edb47ec4SLluís } 398edb47ec4SLluís 39985aff158SStefan Hajnoczi /* Helper function to create a thread with signals blocked. Use glib's 40085aff158SStefan Hajnoczi * portable threads since QEMU abstractions cannot be used due to reentrancy in 40185aff158SStefan Hajnoczi * the tracer. Also note the signal masking on POSIX hosts so that the thread 40285aff158SStefan Hajnoczi * does not steal signals when the rest of the program wants them blocked. 40385aff158SStefan Hajnoczi */ 40485aff158SStefan Hajnoczi static GThread *trace_thread_create(GThreadFunc fn) 405edb47ec4SLluís { 40685aff158SStefan Hajnoczi GThread *thread; 40785aff158SStefan Hajnoczi #ifndef _WIN32 408edb47ec4SLluís sigset_t set, oldset; 409edb47ec4SLluís 410edb47ec4SLluís sigfillset(&set); 411edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &set, &oldset); 41285aff158SStefan Hajnoczi #endif 413db3bf869SJun Koi thread = g_thread_create(fn, NULL, FALSE, NULL); 41485aff158SStefan Hajnoczi #ifndef _WIN32 415edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &oldset, NULL); 41685aff158SStefan Hajnoczi #endif 417edb47ec4SLluís 41885aff158SStefan Hajnoczi return thread; 41985aff158SStefan Hajnoczi } 42085aff158SStefan Hajnoczi 42185aff158SStefan Hajnoczi bool trace_backend_init(const char *events, const char *file) 42285aff158SStefan Hajnoczi { 42385aff158SStefan Hajnoczi GThread *thread; 42485aff158SStefan Hajnoczi 42585aff158SStefan Hajnoczi if (!g_thread_supported()) { 42642ed3727SAlon Levy #if !GLIB_CHECK_VERSION(2, 31, 0) 42785aff158SStefan Hajnoczi g_thread_init(NULL); 42842ed3727SAlon Levy #else 42942ed3727SAlon Levy fprintf(stderr, "glib threading failed to initialize.\n"); 43042ed3727SAlon Levy exit(1); 43142ed3727SAlon Levy #endif 43285aff158SStefan Hajnoczi } 43385aff158SStefan Hajnoczi 43485aff158SStefan Hajnoczi trace_available_cond = g_cond_new(); 43585aff158SStefan Hajnoczi trace_empty_cond = g_cond_new(); 43685aff158SStefan Hajnoczi 43785aff158SStefan Hajnoczi thread = trace_thread_create(writeout_thread); 43885aff158SStefan Hajnoczi if (!thread) { 439e4858974SLluís fprintf(stderr, "warning: unable to initialize simple trace backend\n"); 44085aff158SStefan Hajnoczi return false; 44185aff158SStefan Hajnoczi } 44285aff158SStefan Hajnoczi 443edb47ec4SLluís atexit(st_flush_trace_buffer); 44423d15e86SLluís trace_backend_init_events(events); 445edb47ec4SLluís st_set_trace_file(file); 446edb47ec4SLluís return true; 447edb47ec4SLluís } 448