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 191de7afc9SPaolo Bonzini #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; 56fb3a5085SMarkus Armbruster static int dropped_events; 57edb47ec4SLluís static FILE *trace_fp; 584552e410SStefan Weil static char *trace_file_name; 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 */ 66fb3a5085SMarkus Armbruster uint64_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; 163fb3a5085SMarkus Armbruster int dropped_count; 164edb47ec4SLluís size_t unused __attribute__ ((unused)); 165edb47ec4SLluís 166edb47ec4SLluís for (;;) { 167edb47ec4SLluís wait_for_trace_records_available(); 168edb47ec4SLluís 169e722d705SMarkus Armbruster if (g_atomic_int_get(&dropped_events)) { 17062bab732SHarsh Prateek Bora dropped.rec.event = DROPPED_EVENT_ID, 17162bab732SHarsh Prateek Bora dropped.rec.timestamp_ns = get_clock(); 172fb3a5085SMarkus Armbruster dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t), 17362bab732SHarsh Prateek Bora dropped.rec.reserved = 0; 174*b6b2c962SMarkus Armbruster do { 175e722d705SMarkus Armbruster dropped_count = g_atomic_int_get(&dropped_events); 176*b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&dropped_events, 177*b6b2c962SMarkus Armbruster dropped_count, 0)); 178fb3a5085SMarkus Armbruster dropped.rec.arguments[0] = dropped_count; 17962bab732SHarsh Prateek Bora unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp); 180edb47ec4SLluís } 181edb47ec4SLluís 18262bab732SHarsh Prateek Bora while (get_trace_record(idx, &recordptr)) { 18362bab732SHarsh Prateek Bora unused = fwrite(recordptr, recordptr->length, 1, trace_fp); 18462bab732SHarsh Prateek Bora writeout_idx += recordptr->length; 18562bab732SHarsh Prateek Bora free(recordptr); /* dont use g_free, can deadlock when traced */ 186edb47ec4SLluís idx = writeout_idx % TRACE_BUF_LEN; 187edb47ec4SLluís } 188edb47ec4SLluís 189edb47ec4SLluís fflush(trace_fp); 190edb47ec4SLluís } 191edb47ec4SLluís return NULL; 192edb47ec4SLluís } 193edb47ec4SLluís 19462bab732SHarsh Prateek Bora void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val) 195edb47ec4SLluís { 19662bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t)); 197edb47ec4SLluís } 198edb47ec4SLluís 19962bab732SHarsh Prateek Bora void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen) 20062bab732SHarsh Prateek Bora { 20162bab732SHarsh Prateek Bora /* Write string length first */ 20262bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen)); 20362bab732SHarsh Prateek Bora /* Write actual string now */ 20462bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen); 20562bab732SHarsh Prateek Bora } 206edb47ec4SLluís 20762bab732SHarsh Prateek Bora int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize) 20862bab732SHarsh Prateek Bora { 20962bab732SHarsh Prateek Bora unsigned int idx, rec_off, old_idx, new_idx; 21062bab732SHarsh Prateek Bora uint32_t rec_len = sizeof(TraceRecord) + datasize; 21162bab732SHarsh Prateek Bora uint64_t timestamp_ns = get_clock(); 21262bab732SHarsh Prateek Bora 213*b6b2c962SMarkus Armbruster do { 214e722d705SMarkus Armbruster old_idx = g_atomic_int_get(&trace_idx); 21562bab732SHarsh Prateek Bora smp_rmb(); 21662bab732SHarsh Prateek Bora new_idx = old_idx + rec_len; 21762bab732SHarsh Prateek Bora 21862bab732SHarsh Prateek Bora if (new_idx - writeout_idx > TRACE_BUF_LEN) { 21962bab732SHarsh Prateek Bora /* Trace Buffer Full, Event dropped ! */ 220fb3a5085SMarkus Armbruster g_atomic_int_inc(&dropped_events); 22162bab732SHarsh Prateek Bora return -ENOSPC; 22262bab732SHarsh Prateek Bora } 223*b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx)); 22462bab732SHarsh Prateek Bora 22562bab732SHarsh Prateek Bora idx = old_idx % TRACE_BUF_LEN; 22662bab732SHarsh Prateek Bora 22762bab732SHarsh Prateek Bora rec_off = idx; 22883d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &event, sizeof(event)); 22983d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, ×tamp_ns, sizeof(timestamp_ns)); 23083d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len)); 23162bab732SHarsh Prateek Bora 23262bab732SHarsh Prateek Bora rec->tbuf_idx = idx; 23362bab732SHarsh Prateek Bora rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN; 23462bab732SHarsh Prateek Bora return 0; 23562bab732SHarsh Prateek Bora } 23662bab732SHarsh Prateek Bora 23762bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size) 23862bab732SHarsh Prateek Bora { 23962bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 24062bab732SHarsh Prateek Bora uint32_t x = 0; 24162bab732SHarsh Prateek Bora while (x < size) { 24262bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 24362bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 24462bab732SHarsh Prateek Bora } 24562bab732SHarsh Prateek Bora data_ptr[x++] = trace_buf[idx++]; 24662bab732SHarsh Prateek Bora } 24762bab732SHarsh Prateek Bora } 24862bab732SHarsh Prateek Bora 24962bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size) 25062bab732SHarsh Prateek Bora { 25162bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 25262bab732SHarsh Prateek Bora uint32_t x = 0; 25362bab732SHarsh Prateek Bora while (x < size) { 25462bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 25562bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 25662bab732SHarsh Prateek Bora } 25762bab732SHarsh Prateek Bora trace_buf[idx++] = data_ptr[x++]; 25862bab732SHarsh Prateek Bora } 25962bab732SHarsh Prateek Bora return idx; /* most callers wants to know where to write next */ 26062bab732SHarsh Prateek Bora } 26162bab732SHarsh Prateek Bora 26262bab732SHarsh Prateek Bora void trace_record_finish(TraceBufferRecord *rec) 26362bab732SHarsh Prateek Bora { 264db8894f2SHarsh Prateek Bora TraceRecord record; 265db8894f2SHarsh Prateek Bora read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 26662bab732SHarsh Prateek Bora smp_wmb(); /* write barrier before marking as valid */ 267db8894f2SHarsh Prateek Bora record.event |= TRACE_RECORD_VALID; 268db8894f2SHarsh Prateek Bora write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 26962bab732SHarsh Prateek Bora 270e722d705SMarkus Armbruster if ((g_atomic_int_get(&trace_idx) - writeout_idx) 271e722d705SMarkus Armbruster > TRACE_BUF_FLUSH_THRESHOLD) { 272edb47ec4SLluís flush_trace_file(false); 273edb47ec4SLluís } 274edb47ec4SLluís } 275edb47ec4SLluís 276edb47ec4SLluís void st_set_trace_file_enabled(bool enable) 277edb47ec4SLluís { 278edb47ec4SLluís if (enable == !!trace_fp) { 279edb47ec4SLluís return; /* no change */ 280edb47ec4SLluís } 281edb47ec4SLluís 282edb47ec4SLluís /* Halt trace writeout */ 283edb47ec4SLluís flush_trace_file(true); 284edb47ec4SLluís trace_writeout_enabled = false; 285edb47ec4SLluís flush_trace_file(true); 286edb47ec4SLluís 287edb47ec4SLluís if (enable) { 2888ae601e8SHarsh Prateek Bora static const TraceLogHeader header = { 28962bab732SHarsh Prateek Bora .header_event_id = HEADER_EVENT_ID, 29062bab732SHarsh Prateek Bora .header_magic = HEADER_MAGIC, 29162bab732SHarsh Prateek Bora /* Older log readers will check for version at next location */ 29262bab732SHarsh Prateek Bora .header_version = HEADER_VERSION, 293edb47ec4SLluís }; 294edb47ec4SLluís 2956c2a4074SStefan Hajnoczi trace_fp = fopen(trace_file_name, "wb"); 296edb47ec4SLluís if (!trace_fp) { 297edb47ec4SLluís return; 298edb47ec4SLluís } 299edb47ec4SLluís 300edb47ec4SLluís if (fwrite(&header, sizeof header, 1, trace_fp) != 1) { 301edb47ec4SLluís fclose(trace_fp); 302edb47ec4SLluís trace_fp = NULL; 303edb47ec4SLluís return; 304edb47ec4SLluís } 305edb47ec4SLluís 306edb47ec4SLluís /* Resume trace writeout */ 307edb47ec4SLluís trace_writeout_enabled = true; 308edb47ec4SLluís flush_trace_file(false); 309edb47ec4SLluís } else { 310edb47ec4SLluís fclose(trace_fp); 311edb47ec4SLluís trace_fp = NULL; 312edb47ec4SLluís } 313edb47ec4SLluís } 314edb47ec4SLluís 315edb47ec4SLluís /** 316edb47ec4SLluís * Set the name of a trace file 317edb47ec4SLluís * 318edb47ec4SLluís * @file The trace file name or NULL for the default name-<pid> set at 319edb47ec4SLluís * config time 320edb47ec4SLluís */ 321edb47ec4SLluís bool st_set_trace_file(const char *file) 322edb47ec4SLluís { 323edb47ec4SLluís st_set_trace_file_enabled(false); 324edb47ec4SLluís 3254552e410SStefan Weil g_free(trace_file_name); 326edb47ec4SLluís 327edb47ec4SLluís if (!file) { 3284552e410SStefan Weil trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid()); 329edb47ec4SLluís } else { 3304552e410SStefan Weil trace_file_name = g_strdup_printf("%s", file); 331edb47ec4SLluís } 332edb47ec4SLluís 333edb47ec4SLluís st_set_trace_file_enabled(true); 334edb47ec4SLluís return true; 335edb47ec4SLluís } 336edb47ec4SLluís 337edb47ec4SLluís void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) 338edb47ec4SLluís { 339edb47ec4SLluís stream_printf(stream, "Trace file \"%s\" %s.\n", 340edb47ec4SLluís trace_file_name, trace_fp ? "on" : "off"); 341edb47ec4SLluís } 342edb47ec4SLluís 343fc764105SLluís void st_flush_trace_buffer(void) 344fc764105SLluís { 345fc764105SLluís flush_trace_file(true); 346fc764105SLluís } 347fc764105SLluís 348fc764105SLluís void trace_print_events(FILE *stream, fprintf_function stream_printf) 349edb47ec4SLluís { 350edb47ec4SLluís unsigned int i; 351edb47ec4SLluís 352edb47ec4SLluís for (i = 0; i < NR_TRACE_EVENTS; i++) { 353edb47ec4SLluís stream_printf(stream, "%s [Event ID %u] : state %u\n", 354edb47ec4SLluís trace_list[i].tp_name, i, trace_list[i].state); 355edb47ec4SLluís } 356edb47ec4SLluís } 357edb47ec4SLluís 358fc764105SLluís bool trace_event_set_state(const char *name, bool state) 359edb47ec4SLluís { 360edb47ec4SLluís unsigned int i; 361454e202dSMark Wu unsigned int len; 362454e202dSMark Wu bool wildcard = false; 363454e202dSMark Wu bool matched = false; 364edb47ec4SLluís 365454e202dSMark Wu len = strlen(name); 366454e202dSMark Wu if (len > 0 && name[len - 1] == '*') { 367454e202dSMark Wu wildcard = true; 368454e202dSMark Wu len -= 1; 369454e202dSMark Wu } 370edb47ec4SLluís for (i = 0; i < NR_TRACE_EVENTS; i++) { 371454e202dSMark Wu if (wildcard) { 372454e202dSMark Wu if (!strncmp(trace_list[i].tp_name, name, len)) { 373454e202dSMark Wu trace_list[i].state = state; 374454e202dSMark Wu matched = true; 375454e202dSMark Wu } 376454e202dSMark Wu continue; 377454e202dSMark Wu } 378edb47ec4SLluís if (!strcmp(trace_list[i].tp_name, name)) { 379fc764105SLluís trace_list[i].state = state; 380edb47ec4SLluís return true; 381edb47ec4SLluís } 382edb47ec4SLluís } 383454e202dSMark Wu return matched; 384edb47ec4SLluís } 385edb47ec4SLluís 38685aff158SStefan Hajnoczi /* Helper function to create a thread with signals blocked. Use glib's 38785aff158SStefan Hajnoczi * portable threads since QEMU abstractions cannot be used due to reentrancy in 38885aff158SStefan Hajnoczi * the tracer. Also note the signal masking on POSIX hosts so that the thread 38985aff158SStefan Hajnoczi * does not steal signals when the rest of the program wants them blocked. 39085aff158SStefan Hajnoczi */ 39185aff158SStefan Hajnoczi static GThread *trace_thread_create(GThreadFunc fn) 392edb47ec4SLluís { 39385aff158SStefan Hajnoczi GThread *thread; 39485aff158SStefan Hajnoczi #ifndef _WIN32 395edb47ec4SLluís sigset_t set, oldset; 396edb47ec4SLluís 397edb47ec4SLluís sigfillset(&set); 398edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &set, &oldset); 39985aff158SStefan Hajnoczi #endif 400db3bf869SJun Koi thread = g_thread_create(fn, NULL, FALSE, NULL); 40185aff158SStefan Hajnoczi #ifndef _WIN32 402edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &oldset, NULL); 40385aff158SStefan Hajnoczi #endif 404edb47ec4SLluís 40585aff158SStefan Hajnoczi return thread; 40685aff158SStefan Hajnoczi } 40785aff158SStefan Hajnoczi 40885aff158SStefan Hajnoczi bool trace_backend_init(const char *events, const char *file) 40985aff158SStefan Hajnoczi { 41085aff158SStefan Hajnoczi GThread *thread; 41185aff158SStefan Hajnoczi 41285aff158SStefan Hajnoczi if (!g_thread_supported()) { 41342ed3727SAlon Levy #if !GLIB_CHECK_VERSION(2, 31, 0) 41485aff158SStefan Hajnoczi g_thread_init(NULL); 41542ed3727SAlon Levy #else 41642ed3727SAlon Levy fprintf(stderr, "glib threading failed to initialize.\n"); 41742ed3727SAlon Levy exit(1); 41842ed3727SAlon Levy #endif 41985aff158SStefan Hajnoczi } 42085aff158SStefan Hajnoczi 42185aff158SStefan Hajnoczi trace_available_cond = g_cond_new(); 42285aff158SStefan Hajnoczi trace_empty_cond = g_cond_new(); 42385aff158SStefan Hajnoczi 42485aff158SStefan Hajnoczi thread = trace_thread_create(writeout_thread); 42585aff158SStefan Hajnoczi if (!thread) { 426e4858974SLluís fprintf(stderr, "warning: unable to initialize simple trace backend\n"); 42785aff158SStefan Hajnoczi return false; 42885aff158SStefan Hajnoczi } 42985aff158SStefan Hajnoczi 430edb47ec4SLluís atexit(st_flush_trace_buffer); 43123d15e86SLluís trace_backend_init_events(events); 432edb47ec4SLluís st_set_trace_file(file); 433edb47ec4SLluís return true; 434edb47ec4SLluís } 435