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" 22*b618c288SLluís Vilanova #include "trace/simple.h" 23edb47ec4SLluís 24edb47ec4SLluís /** Trace file header event ID */ 25edb47ec4SLluís #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */ 26edb47ec4SLluís 27edb47ec4SLluís /** Trace file magic number */ 28edb47ec4SLluís #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL 29edb47ec4SLluís 30edb47ec4SLluís /** Trace file version number, bump if format changes */ 3162bab732SHarsh Prateek Bora #define HEADER_VERSION 2 32edb47ec4SLluís 33edb47ec4SLluís /** Records were dropped event ID */ 34edb47ec4SLluís #define DROPPED_EVENT_ID (~(uint64_t)0 - 1) 35edb47ec4SLluís 36edb47ec4SLluís /** Trace record is valid */ 37edb47ec4SLluís #define TRACE_RECORD_VALID ((uint64_t)1 << 63) 38edb47ec4SLluís 39edb47ec4SLluís /* 40edb47ec4SLluís * Trace records are written out by a dedicated thread. The thread waits for 41edb47ec4SLluís * records to become available, writes them out, and then waits again. 42edb47ec4SLluís */ 4385aff158SStefan Hajnoczi static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; 444a0e6714SStefan Hajnoczi 454a0e6714SStefan Hajnoczi /* g_cond_new() was deprecated in glib 2.31 but we still need to support it */ 464a0e6714SStefan Hajnoczi #if GLIB_CHECK_VERSION(2, 31, 0) 474a0e6714SStefan Hajnoczi static GCond the_trace_available_cond; 484a0e6714SStefan Hajnoczi static GCond the_trace_empty_cond; 494a0e6714SStefan Hajnoczi static GCond *trace_available_cond = &the_trace_available_cond; 504a0e6714SStefan Hajnoczi static GCond *trace_empty_cond = &the_trace_empty_cond; 514a0e6714SStefan Hajnoczi #else 5285aff158SStefan Hajnoczi static GCond *trace_available_cond; 5385aff158SStefan Hajnoczi static GCond *trace_empty_cond; 544a0e6714SStefan Hajnoczi #endif 554a0e6714SStefan Hajnoczi 56edb47ec4SLluís static bool trace_available; 57edb47ec4SLluís static bool trace_writeout_enabled; 58edb47ec4SLluís 5962bab732SHarsh Prateek Bora enum { 6062bab732SHarsh Prateek Bora TRACE_BUF_LEN = 4096 * 64, 6162bab732SHarsh Prateek Bora TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4, 6262bab732SHarsh Prateek Bora }; 6362bab732SHarsh Prateek Bora 6462bab732SHarsh Prateek Bora uint8_t trace_buf[TRACE_BUF_LEN]; 6530d94087SStefan Hajnoczi static volatile gint trace_idx; 6662bab732SHarsh Prateek Bora static unsigned int writeout_idx; 6730d94087SStefan Hajnoczi static volatile gint dropped_events; 68edb47ec4SLluís static FILE *trace_fp; 694552e410SStefan Weil static char *trace_file_name; 70edb47ec4SLluís 7162bab732SHarsh Prateek Bora /* * Trace buffer entry */ 7262bab732SHarsh Prateek Bora typedef struct { 7362bab732SHarsh Prateek Bora uint64_t event; /* TraceEventID */ 7462bab732SHarsh Prateek Bora uint64_t timestamp_ns; 7562bab732SHarsh Prateek Bora uint32_t length; /* in bytes */ 7662bab732SHarsh Prateek Bora uint32_t reserved; /* unused */ 77fb3a5085SMarkus Armbruster uint64_t arguments[]; 7862bab732SHarsh Prateek Bora } TraceRecord; 7962bab732SHarsh Prateek Bora 8062bab732SHarsh Prateek Bora typedef struct { 8162bab732SHarsh Prateek Bora uint64_t header_event_id; /* HEADER_EVENT_ID */ 8262bab732SHarsh Prateek Bora uint64_t header_magic; /* HEADER_MAGIC */ 8362bab732SHarsh Prateek Bora uint64_t header_version; /* HEADER_VERSION */ 848ae601e8SHarsh Prateek Bora } TraceLogHeader; 8562bab732SHarsh Prateek Bora 8662bab732SHarsh Prateek Bora 8762bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size); 8862bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size); 8962bab732SHarsh Prateek Bora 9062bab732SHarsh Prateek Bora static void clear_buffer_range(unsigned int idx, size_t len) 9162bab732SHarsh Prateek Bora { 9262bab732SHarsh Prateek Bora uint32_t num = 0; 9362bab732SHarsh Prateek Bora while (num < len) { 9462bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 9562bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 9662bab732SHarsh Prateek Bora } 9762bab732SHarsh Prateek Bora trace_buf[idx++] = 0; 9862bab732SHarsh Prateek Bora num++; 9962bab732SHarsh Prateek Bora } 10062bab732SHarsh Prateek Bora } 101edb47ec4SLluís /** 102edb47ec4SLluís * Read a trace record from the trace buffer 103edb47ec4SLluís * 104edb47ec4SLluís * @idx Trace buffer index 105edb47ec4SLluís * @record Trace record to fill 106edb47ec4SLluís * 107edb47ec4SLluís * Returns false if the record is not valid. 108edb47ec4SLluís */ 10962bab732SHarsh Prateek Bora static bool get_trace_record(unsigned int idx, TraceRecord **recordptr) 110edb47ec4SLluís { 11162bab732SHarsh Prateek Bora uint64_t event_flag = 0; 11262bab732SHarsh Prateek Bora TraceRecord record; 11362bab732SHarsh Prateek Bora /* read the event flag to see if its a valid record */ 11462bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(event_flag)); 11562bab732SHarsh Prateek Bora 11662bab732SHarsh Prateek Bora if (!(record.event & TRACE_RECORD_VALID)) { 117edb47ec4SLluís return false; 118edb47ec4SLluís } 119edb47ec4SLluís 12062bab732SHarsh Prateek Bora smp_rmb(); /* read memory barrier before accessing record */ 12162bab732SHarsh Prateek Bora /* read the record header to know record length */ 12262bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(TraceRecord)); 12362bab732SHarsh Prateek Bora *recordptr = malloc(record.length); /* dont use g_malloc, can deadlock when traced */ 12462bab732SHarsh Prateek Bora /* make a copy of record to avoid being overwritten */ 12562bab732SHarsh Prateek Bora read_from_buffer(idx, *recordptr, record.length); 12662bab732SHarsh Prateek Bora smp_rmb(); /* memory barrier before clearing valid flag */ 12762bab732SHarsh Prateek Bora (*recordptr)->event &= ~TRACE_RECORD_VALID; 12862bab732SHarsh Prateek Bora /* clear the trace buffer range for consumed record otherwise any byte 12962bab732SHarsh Prateek Bora * with its MSB set may be considered as a valid event id when the writer 13062bab732SHarsh Prateek Bora * thread crosses this range of buffer again. 13162bab732SHarsh Prateek Bora */ 13262bab732SHarsh Prateek Bora clear_buffer_range(idx, record.length); 133edb47ec4SLluís return true; 134edb47ec4SLluís } 135edb47ec4SLluís 136edb47ec4SLluís /** 137edb47ec4SLluís * Kick writeout thread 138edb47ec4SLluís * 139edb47ec4SLluís * @wait Whether to wait for writeout thread to complete 140edb47ec4SLluís */ 141edb47ec4SLluís static void flush_trace_file(bool wait) 142edb47ec4SLluís { 14385aff158SStefan Hajnoczi g_static_mutex_lock(&trace_lock); 144edb47ec4SLluís trace_available = true; 14585aff158SStefan Hajnoczi g_cond_signal(trace_available_cond); 146edb47ec4SLluís 147edb47ec4SLluís if (wait) { 14885aff158SStefan Hajnoczi g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); 149edb47ec4SLluís } 150edb47ec4SLluís 15185aff158SStefan Hajnoczi g_static_mutex_unlock(&trace_lock); 152edb47ec4SLluís } 153edb47ec4SLluís 154edb47ec4SLluís static void wait_for_trace_records_available(void) 155edb47ec4SLluís { 15685aff158SStefan Hajnoczi g_static_mutex_lock(&trace_lock); 157edb47ec4SLluís while (!(trace_available && trace_writeout_enabled)) { 15885aff158SStefan Hajnoczi g_cond_signal(trace_empty_cond); 15985aff158SStefan Hajnoczi g_cond_wait(trace_available_cond, 16085aff158SStefan Hajnoczi g_static_mutex_get_mutex(&trace_lock)); 161edb47ec4SLluís } 162edb47ec4SLluís trace_available = false; 16385aff158SStefan Hajnoczi g_static_mutex_unlock(&trace_lock); 164edb47ec4SLluís } 165edb47ec4SLluís 16685aff158SStefan Hajnoczi static gpointer writeout_thread(gpointer opaque) 167edb47ec4SLluís { 16862bab732SHarsh Prateek Bora TraceRecord *recordptr; 16962bab732SHarsh Prateek Bora union { 17062bab732SHarsh Prateek Bora TraceRecord rec; 17162bab732SHarsh Prateek Bora uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)]; 17262bab732SHarsh Prateek Bora } dropped; 17362bab732SHarsh Prateek Bora unsigned int idx = 0; 174fb3a5085SMarkus Armbruster int dropped_count; 175edb47ec4SLluís size_t unused __attribute__ ((unused)); 176edb47ec4SLluís 177edb47ec4SLluís for (;;) { 178edb47ec4SLluís wait_for_trace_records_available(); 179edb47ec4SLluís 180e722d705SMarkus Armbruster if (g_atomic_int_get(&dropped_events)) { 18162bab732SHarsh Prateek Bora dropped.rec.event = DROPPED_EVENT_ID, 18262bab732SHarsh Prateek Bora dropped.rec.timestamp_ns = get_clock(); 183fb3a5085SMarkus Armbruster dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t), 18462bab732SHarsh Prateek Bora dropped.rec.reserved = 0; 185b6b2c962SMarkus Armbruster do { 186e722d705SMarkus Armbruster dropped_count = g_atomic_int_get(&dropped_events); 187b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&dropped_events, 188b6b2c962SMarkus Armbruster dropped_count, 0)); 189fb3a5085SMarkus Armbruster dropped.rec.arguments[0] = dropped_count; 19062bab732SHarsh Prateek Bora unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp); 191edb47ec4SLluís } 192edb47ec4SLluís 19362bab732SHarsh Prateek Bora while (get_trace_record(idx, &recordptr)) { 19462bab732SHarsh Prateek Bora unused = fwrite(recordptr, recordptr->length, 1, trace_fp); 19562bab732SHarsh Prateek Bora writeout_idx += recordptr->length; 19662bab732SHarsh Prateek Bora free(recordptr); /* dont use g_free, can deadlock when traced */ 197edb47ec4SLluís idx = writeout_idx % TRACE_BUF_LEN; 198edb47ec4SLluís } 199edb47ec4SLluís 200edb47ec4SLluís fflush(trace_fp); 201edb47ec4SLluís } 202edb47ec4SLluís return NULL; 203edb47ec4SLluís } 204edb47ec4SLluís 20562bab732SHarsh Prateek Bora void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val) 206edb47ec4SLluís { 20762bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t)); 208edb47ec4SLluís } 209edb47ec4SLluís 21062bab732SHarsh Prateek Bora void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen) 21162bab732SHarsh Prateek Bora { 21262bab732SHarsh Prateek Bora /* Write string length first */ 21362bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen)); 21462bab732SHarsh Prateek Bora /* Write actual string now */ 21562bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen); 21662bab732SHarsh Prateek Bora } 217edb47ec4SLluís 21862bab732SHarsh Prateek Bora int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize) 21962bab732SHarsh Prateek Bora { 22062bab732SHarsh Prateek Bora unsigned int idx, rec_off, old_idx, new_idx; 22162bab732SHarsh Prateek Bora uint32_t rec_len = sizeof(TraceRecord) + datasize; 22260481e21SLluís Vilanova uint64_t event_u64 = event; 22362bab732SHarsh Prateek Bora uint64_t timestamp_ns = get_clock(); 22462bab732SHarsh Prateek Bora 225b6b2c962SMarkus Armbruster do { 226e722d705SMarkus Armbruster old_idx = g_atomic_int_get(&trace_idx); 22762bab732SHarsh Prateek Bora smp_rmb(); 22862bab732SHarsh Prateek Bora new_idx = old_idx + rec_len; 22962bab732SHarsh Prateek Bora 23062bab732SHarsh Prateek Bora if (new_idx - writeout_idx > TRACE_BUF_LEN) { 23162bab732SHarsh Prateek Bora /* Trace Buffer Full, Event dropped ! */ 232fb3a5085SMarkus Armbruster g_atomic_int_inc(&dropped_events); 23362bab732SHarsh Prateek Bora return -ENOSPC; 23462bab732SHarsh Prateek Bora } 235b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx)); 23662bab732SHarsh Prateek Bora 23762bab732SHarsh Prateek Bora idx = old_idx % TRACE_BUF_LEN; 23862bab732SHarsh Prateek Bora 23962bab732SHarsh Prateek Bora rec_off = idx; 24060481e21SLluís Vilanova rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64)); 24183d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, ×tamp_ns, sizeof(timestamp_ns)); 24283d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len)); 24362bab732SHarsh Prateek Bora 24462bab732SHarsh Prateek Bora rec->tbuf_idx = idx; 24562bab732SHarsh Prateek Bora rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN; 24662bab732SHarsh Prateek Bora return 0; 24762bab732SHarsh Prateek Bora } 24862bab732SHarsh Prateek Bora 24962bab732SHarsh Prateek Bora static void read_from_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 data_ptr[x++] = trace_buf[idx++]; 25862bab732SHarsh Prateek Bora } 25962bab732SHarsh Prateek Bora } 26062bab732SHarsh Prateek Bora 26162bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size) 26262bab732SHarsh Prateek Bora { 26362bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 26462bab732SHarsh Prateek Bora uint32_t x = 0; 26562bab732SHarsh Prateek Bora while (x < size) { 26662bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 26762bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 26862bab732SHarsh Prateek Bora } 26962bab732SHarsh Prateek Bora trace_buf[idx++] = data_ptr[x++]; 27062bab732SHarsh Prateek Bora } 27162bab732SHarsh Prateek Bora return idx; /* most callers wants to know where to write next */ 27262bab732SHarsh Prateek Bora } 27362bab732SHarsh Prateek Bora 27462bab732SHarsh Prateek Bora void trace_record_finish(TraceBufferRecord *rec) 27562bab732SHarsh Prateek Bora { 276db8894f2SHarsh Prateek Bora TraceRecord record; 277db8894f2SHarsh Prateek Bora read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 27862bab732SHarsh Prateek Bora smp_wmb(); /* write barrier before marking as valid */ 279db8894f2SHarsh Prateek Bora record.event |= TRACE_RECORD_VALID; 280db8894f2SHarsh Prateek Bora write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 28162bab732SHarsh Prateek Bora 28230d94087SStefan Hajnoczi if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx) 283e722d705SMarkus Armbruster > TRACE_BUF_FLUSH_THRESHOLD) { 284edb47ec4SLluís flush_trace_file(false); 285edb47ec4SLluís } 286edb47ec4SLluís } 287edb47ec4SLluís 288edb47ec4SLluís void st_set_trace_file_enabled(bool enable) 289edb47ec4SLluís { 290edb47ec4SLluís if (enable == !!trace_fp) { 291edb47ec4SLluís return; /* no change */ 292edb47ec4SLluís } 293edb47ec4SLluís 294edb47ec4SLluís /* Halt trace writeout */ 295edb47ec4SLluís flush_trace_file(true); 296edb47ec4SLluís trace_writeout_enabled = false; 297edb47ec4SLluís flush_trace_file(true); 298edb47ec4SLluís 299edb47ec4SLluís if (enable) { 3008ae601e8SHarsh Prateek Bora static const TraceLogHeader header = { 30162bab732SHarsh Prateek Bora .header_event_id = HEADER_EVENT_ID, 30262bab732SHarsh Prateek Bora .header_magic = HEADER_MAGIC, 30362bab732SHarsh Prateek Bora /* Older log readers will check for version at next location */ 30462bab732SHarsh Prateek Bora .header_version = HEADER_VERSION, 305edb47ec4SLluís }; 306edb47ec4SLluís 3076c2a4074SStefan Hajnoczi trace_fp = fopen(trace_file_name, "wb"); 308edb47ec4SLluís if (!trace_fp) { 309edb47ec4SLluís return; 310edb47ec4SLluís } 311edb47ec4SLluís 312edb47ec4SLluís if (fwrite(&header, sizeof header, 1, trace_fp) != 1) { 313edb47ec4SLluís fclose(trace_fp); 314edb47ec4SLluís trace_fp = NULL; 315edb47ec4SLluís return; 316edb47ec4SLluís } 317edb47ec4SLluís 318edb47ec4SLluís /* Resume trace writeout */ 319edb47ec4SLluís trace_writeout_enabled = true; 320edb47ec4SLluís flush_trace_file(false); 321edb47ec4SLluís } else { 322edb47ec4SLluís fclose(trace_fp); 323edb47ec4SLluís trace_fp = NULL; 324edb47ec4SLluís } 325edb47ec4SLluís } 326edb47ec4SLluís 327edb47ec4SLluís /** 328edb47ec4SLluís * Set the name of a trace file 329edb47ec4SLluís * 330edb47ec4SLluís * @file The trace file name or NULL for the default name-<pid> set at 331edb47ec4SLluís * config time 332edb47ec4SLluís */ 333edb47ec4SLluís bool st_set_trace_file(const char *file) 334edb47ec4SLluís { 335edb47ec4SLluís st_set_trace_file_enabled(false); 336edb47ec4SLluís 3374552e410SStefan Weil g_free(trace_file_name); 338edb47ec4SLluís 339edb47ec4SLluís if (!file) { 3404552e410SStefan Weil trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid()); 341edb47ec4SLluís } else { 3424552e410SStefan Weil trace_file_name = g_strdup_printf("%s", file); 343edb47ec4SLluís } 344edb47ec4SLluís 345edb47ec4SLluís st_set_trace_file_enabled(true); 346edb47ec4SLluís return true; 347edb47ec4SLluís } 348edb47ec4SLluís 349edb47ec4SLluís void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) 350edb47ec4SLluís { 351edb47ec4SLluís stream_printf(stream, "Trace file \"%s\" %s.\n", 352edb47ec4SLluís trace_file_name, trace_fp ? "on" : "off"); 353edb47ec4SLluís } 354edb47ec4SLluís 355fc764105SLluís void st_flush_trace_buffer(void) 356fc764105SLluís { 357fc764105SLluís flush_trace_file(true); 358fc764105SLluís } 359fc764105SLluís 360fc764105SLluís void trace_print_events(FILE *stream, fprintf_function stream_printf) 361edb47ec4SLluís { 362edb47ec4SLluís unsigned int i; 363edb47ec4SLluís 36460481e21SLluís Vilanova for (i = 0; i < trace_event_count(); i++) { 36560481e21SLluís Vilanova TraceEvent *ev = trace_event_id(i); 366edb47ec4SLluís stream_printf(stream, "%s [Event ID %u] : state %u\n", 36760481e21SLluís Vilanova trace_event_get_name(ev), i, trace_event_get_state_dynamic(ev)); 368edb47ec4SLluís } 369edb47ec4SLluís } 370edb47ec4SLluís 37160481e21SLluís Vilanova void trace_event_set_state_dynamic_backend(TraceEvent *ev, bool state) 372edb47ec4SLluís { 37360481e21SLluís Vilanova ev->dstate = state; 374edb47ec4SLluís } 375edb47ec4SLluís 37685aff158SStefan Hajnoczi /* Helper function to create a thread with signals blocked. Use glib's 37785aff158SStefan Hajnoczi * portable threads since QEMU abstractions cannot be used due to reentrancy in 37885aff158SStefan Hajnoczi * the tracer. Also note the signal masking on POSIX hosts so that the thread 37985aff158SStefan Hajnoczi * does not steal signals when the rest of the program wants them blocked. 38085aff158SStefan Hajnoczi */ 38185aff158SStefan Hajnoczi static GThread *trace_thread_create(GThreadFunc fn) 382edb47ec4SLluís { 38385aff158SStefan Hajnoczi GThread *thread; 38485aff158SStefan Hajnoczi #ifndef _WIN32 385edb47ec4SLluís sigset_t set, oldset; 386edb47ec4SLluís 387edb47ec4SLluís sigfillset(&set); 388edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &set, &oldset); 38985aff158SStefan Hajnoczi #endif 3904a0e6714SStefan Hajnoczi 3914a0e6714SStefan Hajnoczi #if GLIB_CHECK_VERSION(2, 31, 0) 3924a0e6714SStefan Hajnoczi thread = g_thread_new("trace-thread", fn, NULL); 3934a0e6714SStefan Hajnoczi #else 394db3bf869SJun Koi thread = g_thread_create(fn, NULL, FALSE, NULL); 3954a0e6714SStefan Hajnoczi #endif 3964a0e6714SStefan Hajnoczi 39785aff158SStefan Hajnoczi #ifndef _WIN32 398edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &oldset, NULL); 39985aff158SStefan Hajnoczi #endif 400edb47ec4SLluís 40185aff158SStefan Hajnoczi return thread; 40285aff158SStefan Hajnoczi } 40385aff158SStefan Hajnoczi 40485aff158SStefan Hajnoczi bool trace_backend_init(const char *events, const char *file) 40585aff158SStefan Hajnoczi { 40685aff158SStefan Hajnoczi GThread *thread; 40785aff158SStefan Hajnoczi 40885aff158SStefan Hajnoczi if (!g_thread_supported()) { 40942ed3727SAlon Levy #if !GLIB_CHECK_VERSION(2, 31, 0) 41085aff158SStefan Hajnoczi g_thread_init(NULL); 41142ed3727SAlon Levy #else 41242ed3727SAlon Levy fprintf(stderr, "glib threading failed to initialize.\n"); 41342ed3727SAlon Levy exit(1); 41442ed3727SAlon Levy #endif 41585aff158SStefan Hajnoczi } 41685aff158SStefan Hajnoczi 4174a0e6714SStefan Hajnoczi #if !GLIB_CHECK_VERSION(2, 31, 0) 41885aff158SStefan Hajnoczi trace_available_cond = g_cond_new(); 41985aff158SStefan Hajnoczi trace_empty_cond = g_cond_new(); 4204a0e6714SStefan Hajnoczi #endif 42185aff158SStefan Hajnoczi 42285aff158SStefan Hajnoczi thread = trace_thread_create(writeout_thread); 42385aff158SStefan Hajnoczi if (!thread) { 424e4858974SLluís fprintf(stderr, "warning: unable to initialize simple trace backend\n"); 42585aff158SStefan Hajnoczi return false; 42685aff158SStefan Hajnoczi } 42785aff158SStefan Hajnoczi 428edb47ec4SLluís atexit(st_flush_trace_buffer); 42923d15e86SLluís trace_backend_init_events(events); 430edb47ec4SLluís st_set_trace_file(file); 431edb47ec4SLluís return true; 432edb47ec4SLluís } 433