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 11d38ea87aSPeter Maydell #include "qemu/osdep.h" 1285aff158SStefan Hajnoczi #ifndef _WIN32 13edb47ec4SLluís #include <pthread.h> 1485aff158SStefan Hajnoczi #endif 151de7afc9SPaolo Bonzini #include "qemu/timer.h" 16e4858974SLluís #include "trace/control.h" 17b618c288SLluís Vilanova #include "trace/simple.h" 182ab4b135SAlistair Francis #include "qemu/error-report.h" 19edb47ec4SLluís 20ef4c9fc8SDaniel P. Berrange /** Trace file header event ID, picked to avoid conflict with real event IDs */ 21ef4c9fc8SDaniel P. Berrange #define HEADER_EVENT_ID (~(uint64_t)0) 22edb47ec4SLluís 23edb47ec4SLluís /** Trace file magic number */ 24edb47ec4SLluís #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL 25edb47ec4SLluís 26edb47ec4SLluís /** Trace file version number, bump if format changes */ 277f1b588fSDaniel P. Berrange #define HEADER_VERSION 4 28edb47ec4SLluís 29edb47ec4SLluís /** Records were dropped event ID */ 30edb47ec4SLluís #define DROPPED_EVENT_ID (~(uint64_t)0 - 1) 31edb47ec4SLluís 32edb47ec4SLluís /** Trace record is valid */ 33edb47ec4SLluís #define TRACE_RECORD_VALID ((uint64_t)1 << 63) 34edb47ec4SLluís 35edb47ec4SLluís /* 36edb47ec4SLluís * Trace records are written out by a dedicated thread. The thread waits for 37edb47ec4SLluís * records to become available, writes them out, and then waits again. 38edb47ec4SLluís */ 39e7b3af81SDaniel P. Berrangé static GMutex trace_lock; 40e7b3af81SDaniel P. Berrangé static GCond trace_available_cond; 41e7b3af81SDaniel P. Berrangé static GCond trace_empty_cond; 424a0e6714SStefan Hajnoczi 43edb47ec4SLluís static bool trace_available; 44edb47ec4SLluís static bool trace_writeout_enabled; 45edb47ec4SLluís 4662bab732SHarsh Prateek Bora enum { 4762bab732SHarsh Prateek Bora TRACE_BUF_LEN = 4096 * 64, 4862bab732SHarsh Prateek Bora TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4, 4962bab732SHarsh Prateek Bora }; 5062bab732SHarsh Prateek Bora 5162bab732SHarsh Prateek Bora uint8_t trace_buf[TRACE_BUF_LEN]; 5230d94087SStefan Hajnoczi static volatile gint trace_idx; 5362bab732SHarsh Prateek Bora static unsigned int writeout_idx; 5430d94087SStefan Hajnoczi static volatile gint dropped_events; 5526896cbfSStefan Hajnoczi static uint32_t trace_pid; 56edb47ec4SLluís static FILE *trace_fp; 574552e410SStefan Weil static char *trace_file_name; 58edb47ec4SLluís 597f1b588fSDaniel P. Berrange #define TRACE_RECORD_TYPE_MAPPING 0 607f1b588fSDaniel P. Berrange #define TRACE_RECORD_TYPE_EVENT 1 617f1b588fSDaniel P. Berrange 6262bab732SHarsh Prateek Bora /* * Trace buffer entry */ 6362bab732SHarsh Prateek Bora typedef struct { 64ef4c9fc8SDaniel P. Berrange uint64_t event; /* event ID value */ 6562bab732SHarsh Prateek Bora uint64_t timestamp_ns; 6662bab732SHarsh Prateek Bora uint32_t length; /* in bytes */ 6726896cbfSStefan Hajnoczi uint32_t pid; 68fb3a5085SMarkus Armbruster uint64_t arguments[]; 6962bab732SHarsh Prateek Bora } TraceRecord; 7062bab732SHarsh Prateek Bora 7162bab732SHarsh Prateek Bora typedef struct { 7262bab732SHarsh Prateek Bora uint64_t header_event_id; /* HEADER_EVENT_ID */ 7362bab732SHarsh Prateek Bora uint64_t header_magic; /* HEADER_MAGIC */ 7462bab732SHarsh Prateek Bora uint64_t header_version; /* HEADER_VERSION */ 758ae601e8SHarsh Prateek Bora } TraceLogHeader; 7662bab732SHarsh Prateek Bora 7762bab732SHarsh Prateek Bora 7862bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size); 7962bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size); 8062bab732SHarsh Prateek Bora 8162bab732SHarsh Prateek Bora static void clear_buffer_range(unsigned int idx, size_t len) 8262bab732SHarsh Prateek Bora { 8362bab732SHarsh Prateek Bora uint32_t num = 0; 8462bab732SHarsh Prateek Bora while (num < len) { 8562bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 8662bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 8762bab732SHarsh Prateek Bora } 8862bab732SHarsh Prateek Bora trace_buf[idx++] = 0; 8962bab732SHarsh Prateek Bora num++; 9062bab732SHarsh Prateek Bora } 9162bab732SHarsh Prateek Bora } 92edb47ec4SLluís /** 93edb47ec4SLluís * Read a trace record from the trace buffer 94edb47ec4SLluís * 95edb47ec4SLluís * @idx Trace buffer index 96edb47ec4SLluís * @record Trace record to fill 97edb47ec4SLluís * 98edb47ec4SLluís * Returns false if the record is not valid. 99edb47ec4SLluís */ 10062bab732SHarsh Prateek Bora static bool get_trace_record(unsigned int idx, TraceRecord **recordptr) 101edb47ec4SLluís { 10262bab732SHarsh Prateek Bora uint64_t event_flag = 0; 10362bab732SHarsh Prateek Bora TraceRecord record; 10462bab732SHarsh Prateek Bora /* read the event flag to see if its a valid record */ 10562bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(event_flag)); 10662bab732SHarsh Prateek Bora 10762bab732SHarsh Prateek Bora if (!(record.event & TRACE_RECORD_VALID)) { 108edb47ec4SLluís return false; 109edb47ec4SLluís } 110edb47ec4SLluís 11162bab732SHarsh Prateek Bora smp_rmb(); /* read memory barrier before accessing record */ 11262bab732SHarsh Prateek Bora /* read the record header to know record length */ 11362bab732SHarsh Prateek Bora read_from_buffer(idx, &record, sizeof(TraceRecord)); 114cb8d4c8fSStefan Weil *recordptr = malloc(record.length); /* don't use g_malloc, can deadlock when traced */ 11562bab732SHarsh Prateek Bora /* make a copy of record to avoid being overwritten */ 11662bab732SHarsh Prateek Bora read_from_buffer(idx, *recordptr, record.length); 11762bab732SHarsh Prateek Bora smp_rmb(); /* memory barrier before clearing valid flag */ 11862bab732SHarsh Prateek Bora (*recordptr)->event &= ~TRACE_RECORD_VALID; 11962bab732SHarsh Prateek Bora /* clear the trace buffer range for consumed record otherwise any byte 12062bab732SHarsh Prateek Bora * with its MSB set may be considered as a valid event id when the writer 12162bab732SHarsh Prateek Bora * thread crosses this range of buffer again. 12262bab732SHarsh Prateek Bora */ 12362bab732SHarsh Prateek Bora clear_buffer_range(idx, record.length); 124edb47ec4SLluís return true; 125edb47ec4SLluís } 126edb47ec4SLluís 127edb47ec4SLluís /** 128edb47ec4SLluís * Kick writeout thread 129edb47ec4SLluís * 130edb47ec4SLluís * @wait Whether to wait for writeout thread to complete 131edb47ec4SLluís */ 132edb47ec4SLluís static void flush_trace_file(bool wait) 133edb47ec4SLluís { 13486946a2dSMichael Tokarev g_mutex_lock(&trace_lock); 135edb47ec4SLluís trace_available = true; 13686946a2dSMichael Tokarev g_cond_signal(&trace_available_cond); 137edb47ec4SLluís 138edb47ec4SLluís if (wait) { 13986946a2dSMichael Tokarev g_cond_wait(&trace_empty_cond, &trace_lock); 140edb47ec4SLluís } 141edb47ec4SLluís 14286946a2dSMichael Tokarev g_mutex_unlock(&trace_lock); 143edb47ec4SLluís } 144edb47ec4SLluís 145edb47ec4SLluís static void wait_for_trace_records_available(void) 146edb47ec4SLluís { 14786946a2dSMichael Tokarev g_mutex_lock(&trace_lock); 148edb47ec4SLluís while (!(trace_available && trace_writeout_enabled)) { 14986946a2dSMichael Tokarev g_cond_signal(&trace_empty_cond); 15086946a2dSMichael Tokarev g_cond_wait(&trace_available_cond, &trace_lock); 151edb47ec4SLluís } 152edb47ec4SLluís trace_available = false; 15386946a2dSMichael Tokarev g_mutex_unlock(&trace_lock); 154edb47ec4SLluís } 155edb47ec4SLluís 15685aff158SStefan Hajnoczi static gpointer writeout_thread(gpointer opaque) 157edb47ec4SLluís { 15862bab732SHarsh Prateek Bora TraceRecord *recordptr; 15962bab732SHarsh Prateek Bora union { 16062bab732SHarsh Prateek Bora TraceRecord rec; 16162bab732SHarsh Prateek Bora uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)]; 16262bab732SHarsh Prateek Bora } dropped; 16362bab732SHarsh Prateek Bora unsigned int idx = 0; 164fb3a5085SMarkus Armbruster int dropped_count; 165edb47ec4SLluís size_t unused __attribute__ ((unused)); 1667f1b588fSDaniel P. Berrange uint64_t type = TRACE_RECORD_TYPE_EVENT; 167edb47ec4SLluís 168edb47ec4SLluís for (;;) { 169edb47ec4SLluís wait_for_trace_records_available(); 170edb47ec4SLluís 171e722d705SMarkus Armbruster if (g_atomic_int_get(&dropped_events)) { 172*7ff59207SLarry Dewey dropped.rec.event = DROPPED_EVENT_ID; 17362bab732SHarsh Prateek Bora dropped.rec.timestamp_ns = get_clock(); 174*7ff59207SLarry Dewey dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t); 17526896cbfSStefan Hajnoczi dropped.rec.pid = trace_pid; 176b6b2c962SMarkus Armbruster do { 177e722d705SMarkus Armbruster dropped_count = g_atomic_int_get(&dropped_events); 178b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&dropped_events, 179b6b2c962SMarkus Armbruster dropped_count, 0)); 180fb3a5085SMarkus Armbruster dropped.rec.arguments[0] = dropped_count; 1817f1b588fSDaniel P. Berrange unused = fwrite(&type, sizeof(type), 1, trace_fp); 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)) { 1867f1b588fSDaniel P. Berrange unused = fwrite(&type, sizeof(type), 1, trace_fp); 18762bab732SHarsh Prateek Bora unused = fwrite(recordptr, recordptr->length, 1, trace_fp); 18862bab732SHarsh Prateek Bora writeout_idx += recordptr->length; 189cb8d4c8fSStefan Weil free(recordptr); /* don't use g_free, can deadlock when traced */ 190edb47ec4SLluís idx = writeout_idx % TRACE_BUF_LEN; 191edb47ec4SLluís } 192edb47ec4SLluís 193edb47ec4SLluís fflush(trace_fp); 194edb47ec4SLluís } 195edb47ec4SLluís return NULL; 196edb47ec4SLluís } 197edb47ec4SLluís 19862bab732SHarsh Prateek Bora void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val) 199edb47ec4SLluís { 20062bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t)); 201edb47ec4SLluís } 202edb47ec4SLluís 20362bab732SHarsh Prateek Bora void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen) 20462bab732SHarsh Prateek Bora { 20562bab732SHarsh Prateek Bora /* Write string length first */ 20662bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen)); 20762bab732SHarsh Prateek Bora /* Write actual string now */ 20862bab732SHarsh Prateek Bora rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen); 20962bab732SHarsh Prateek Bora } 210edb47ec4SLluís 211ef4c9fc8SDaniel P. Berrange int trace_record_start(TraceBufferRecord *rec, uint32_t event, size_t datasize) 21262bab732SHarsh Prateek Bora { 21362bab732SHarsh Prateek Bora unsigned int idx, rec_off, old_idx, new_idx; 21462bab732SHarsh Prateek Bora uint32_t rec_len = sizeof(TraceRecord) + datasize; 21560481e21SLluís Vilanova uint64_t event_u64 = event; 21662bab732SHarsh Prateek Bora uint64_t timestamp_ns = get_clock(); 21762bab732SHarsh Prateek Bora 218b6b2c962SMarkus Armbruster do { 219e722d705SMarkus Armbruster old_idx = g_atomic_int_get(&trace_idx); 22062bab732SHarsh Prateek Bora smp_rmb(); 22162bab732SHarsh Prateek Bora new_idx = old_idx + rec_len; 22262bab732SHarsh Prateek Bora 22362bab732SHarsh Prateek Bora if (new_idx - writeout_idx > TRACE_BUF_LEN) { 22462bab732SHarsh Prateek Bora /* Trace Buffer Full, Event dropped ! */ 225fb3a5085SMarkus Armbruster g_atomic_int_inc(&dropped_events); 22662bab732SHarsh Prateek Bora return -ENOSPC; 22762bab732SHarsh Prateek Bora } 228b6b2c962SMarkus Armbruster } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx)); 22962bab732SHarsh Prateek Bora 23062bab732SHarsh Prateek Bora idx = old_idx % TRACE_BUF_LEN; 23162bab732SHarsh Prateek Bora 23262bab732SHarsh Prateek Bora rec_off = idx; 23360481e21SLluís Vilanova rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64)); 23483d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, ×tamp_ns, sizeof(timestamp_ns)); 23583d35d3eSHarsh Prateek Bora rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len)); 23626896cbfSStefan Hajnoczi rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid)); 23762bab732SHarsh Prateek Bora 23862bab732SHarsh Prateek Bora rec->tbuf_idx = idx; 23962bab732SHarsh Prateek Bora rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN; 24062bab732SHarsh Prateek Bora return 0; 24162bab732SHarsh Prateek Bora } 24262bab732SHarsh Prateek Bora 24362bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size) 24462bab732SHarsh Prateek Bora { 24562bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 24662bab732SHarsh Prateek Bora uint32_t x = 0; 24762bab732SHarsh Prateek Bora while (x < size) { 24862bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 24962bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 25062bab732SHarsh Prateek Bora } 25162bab732SHarsh Prateek Bora data_ptr[x++] = trace_buf[idx++]; 25262bab732SHarsh Prateek Bora } 25362bab732SHarsh Prateek Bora } 25462bab732SHarsh Prateek Bora 25562bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size) 25662bab732SHarsh Prateek Bora { 25762bab732SHarsh Prateek Bora uint8_t *data_ptr = dataptr; 25862bab732SHarsh Prateek Bora uint32_t x = 0; 25962bab732SHarsh Prateek Bora while (x < size) { 26062bab732SHarsh Prateek Bora if (idx >= TRACE_BUF_LEN) { 26162bab732SHarsh Prateek Bora idx = idx % TRACE_BUF_LEN; 26262bab732SHarsh Prateek Bora } 26362bab732SHarsh Prateek Bora trace_buf[idx++] = data_ptr[x++]; 26462bab732SHarsh Prateek Bora } 26562bab732SHarsh Prateek Bora return idx; /* most callers wants to know where to write next */ 26662bab732SHarsh Prateek Bora } 26762bab732SHarsh Prateek Bora 26862bab732SHarsh Prateek Bora void trace_record_finish(TraceBufferRecord *rec) 26962bab732SHarsh Prateek Bora { 270db8894f2SHarsh Prateek Bora TraceRecord record; 271db8894f2SHarsh Prateek Bora read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 27262bab732SHarsh Prateek Bora smp_wmb(); /* write barrier before marking as valid */ 273db8894f2SHarsh Prateek Bora record.event |= TRACE_RECORD_VALID; 274db8894f2SHarsh Prateek Bora write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); 27562bab732SHarsh Prateek Bora 27630d94087SStefan Hajnoczi if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx) 277e722d705SMarkus Armbruster > TRACE_BUF_FLUSH_THRESHOLD) { 278edb47ec4SLluís flush_trace_file(false); 279edb47ec4SLluís } 280edb47ec4SLluís } 281edb47ec4SLluís 2827f1b588fSDaniel P. Berrange static int st_write_event_mapping(void) 2837f1b588fSDaniel P. Berrange { 2847f1b588fSDaniel P. Berrange uint64_t type = TRACE_RECORD_TYPE_MAPPING; 2857f1b588fSDaniel P. Berrange TraceEventIter iter; 2867f1b588fSDaniel P. Berrange TraceEvent *ev; 2877f1b588fSDaniel P. Berrange 2887f1b588fSDaniel P. Berrange trace_event_iter_init(&iter, NULL); 2897f1b588fSDaniel P. Berrange while ((ev = trace_event_iter_next(&iter)) != NULL) { 2907f1b588fSDaniel P. Berrange uint64_t id = trace_event_get_id(ev); 2917f1b588fSDaniel P. Berrange const char *name = trace_event_get_name(ev); 2927f1b588fSDaniel P. Berrange uint32_t len = strlen(name); 2937f1b588fSDaniel P. Berrange if (fwrite(&type, sizeof(type), 1, trace_fp) != 1 || 2947f1b588fSDaniel P. Berrange fwrite(&id, sizeof(id), 1, trace_fp) != 1 || 2957f1b588fSDaniel P. Berrange fwrite(&len, sizeof(len), 1, trace_fp) != 1 || 2967f1b588fSDaniel P. Berrange fwrite(name, len, 1, trace_fp) != 1) { 2977f1b588fSDaniel P. Berrange return -1; 2987f1b588fSDaniel P. Berrange } 2997f1b588fSDaniel P. Berrange } 3007f1b588fSDaniel P. Berrange 3017f1b588fSDaniel P. Berrange return 0; 3027f1b588fSDaniel P. Berrange } 3037f1b588fSDaniel P. Berrange 304edb47ec4SLluís void st_set_trace_file_enabled(bool enable) 305edb47ec4SLluís { 306edb47ec4SLluís if (enable == !!trace_fp) { 307edb47ec4SLluís return; /* no change */ 308edb47ec4SLluís } 309edb47ec4SLluís 310edb47ec4SLluís /* Halt trace writeout */ 311edb47ec4SLluís flush_trace_file(true); 312edb47ec4SLluís trace_writeout_enabled = false; 313edb47ec4SLluís flush_trace_file(true); 314edb47ec4SLluís 315edb47ec4SLluís if (enable) { 3168ae601e8SHarsh Prateek Bora static const TraceLogHeader header = { 31762bab732SHarsh Prateek Bora .header_event_id = HEADER_EVENT_ID, 31862bab732SHarsh Prateek Bora .header_magic = HEADER_MAGIC, 31962bab732SHarsh Prateek Bora /* Older log readers will check for version at next location */ 32062bab732SHarsh Prateek Bora .header_version = HEADER_VERSION, 321edb47ec4SLluís }; 322edb47ec4SLluís 3236c2a4074SStefan Hajnoczi trace_fp = fopen(trace_file_name, "wb"); 324edb47ec4SLluís if (!trace_fp) { 325edb47ec4SLluís return; 326edb47ec4SLluís } 327edb47ec4SLluís 3287f1b588fSDaniel P. Berrange if (fwrite(&header, sizeof header, 1, trace_fp) != 1 || 3297f1b588fSDaniel P. Berrange st_write_event_mapping() < 0) { 330edb47ec4SLluís fclose(trace_fp); 331edb47ec4SLluís trace_fp = NULL; 332edb47ec4SLluís return; 333edb47ec4SLluís } 334edb47ec4SLluís 335edb47ec4SLluís /* Resume trace writeout */ 336edb47ec4SLluís trace_writeout_enabled = true; 337edb47ec4SLluís flush_trace_file(false); 338edb47ec4SLluís } else { 339edb47ec4SLluís fclose(trace_fp); 340edb47ec4SLluís trace_fp = NULL; 341edb47ec4SLluís } 342edb47ec4SLluís } 343edb47ec4SLluís 344edb47ec4SLluís /** 345edb47ec4SLluís * Set the name of a trace file 346edb47ec4SLluís * 347edb47ec4SLluís * @file The trace file name or NULL for the default name-<pid> set at 348edb47ec4SLluís * config time 349edb47ec4SLluís */ 35041fc57e4SPaolo Bonzini void st_set_trace_file(const char *file) 351edb47ec4SLluís { 352edb47ec4SLluís st_set_trace_file_enabled(false); 353edb47ec4SLluís 3544552e410SStefan Weil g_free(trace_file_name); 355edb47ec4SLluís 356edb47ec4SLluís if (!file) { 357857a0e38SStefan Weil /* Type cast needed for Windows where getpid() returns an int. */ 358857a0e38SStefan Weil trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid()); 359edb47ec4SLluís } else { 3604552e410SStefan Weil trace_file_name = g_strdup_printf("%s", file); 361edb47ec4SLluís } 362edb47ec4SLluís 363edb47ec4SLluís st_set_trace_file_enabled(true); 364edb47ec4SLluís } 365edb47ec4SLluís 366edb47ec4SLluís void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) 367edb47ec4SLluís { 368edb47ec4SLluís stream_printf(stream, "Trace file \"%s\" %s.\n", 369edb47ec4SLluís trace_file_name, trace_fp ? "on" : "off"); 370edb47ec4SLluís } 371edb47ec4SLluís 372fc764105SLluís void st_flush_trace_buffer(void) 373fc764105SLluís { 374fc764105SLluís flush_trace_file(true); 375fc764105SLluís } 376fc764105SLluís 37785aff158SStefan Hajnoczi /* Helper function to create a thread with signals blocked. Use glib's 37885aff158SStefan Hajnoczi * portable threads since QEMU abstractions cannot be used due to reentrancy in 37985aff158SStefan Hajnoczi * the tracer. Also note the signal masking on POSIX hosts so that the thread 38085aff158SStefan Hajnoczi * does not steal signals when the rest of the program wants them blocked. 38185aff158SStefan Hajnoczi */ 38285aff158SStefan Hajnoczi static GThread *trace_thread_create(GThreadFunc fn) 383edb47ec4SLluís { 38485aff158SStefan Hajnoczi GThread *thread; 38585aff158SStefan Hajnoczi #ifndef _WIN32 386edb47ec4SLluís sigset_t set, oldset; 387edb47ec4SLluís 388edb47ec4SLluís sigfillset(&set); 389edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &set, &oldset); 39085aff158SStefan Hajnoczi #endif 3914a0e6714SStefan Hajnoczi 3924a0e6714SStefan Hajnoczi thread = g_thread_new("trace-thread", fn, NULL); 3934a0e6714SStefan Hajnoczi 39485aff158SStefan Hajnoczi #ifndef _WIN32 395edb47ec4SLluís pthread_sigmask(SIG_SETMASK, &oldset, NULL); 39685aff158SStefan Hajnoczi #endif 397edb47ec4SLluís 39885aff158SStefan Hajnoczi return thread; 39985aff158SStefan Hajnoczi } 40085aff158SStefan Hajnoczi 40141fc57e4SPaolo Bonzini bool st_init(void) 40285aff158SStefan Hajnoczi { 40385aff158SStefan Hajnoczi GThread *thread; 40485aff158SStefan Hajnoczi 40526896cbfSStefan Hajnoczi trace_pid = getpid(); 40626896cbfSStefan Hajnoczi 40785aff158SStefan Hajnoczi thread = trace_thread_create(writeout_thread); 40885aff158SStefan Hajnoczi if (!thread) { 4092ab4b135SAlistair Francis warn_report("unable to initialize simple trace backend"); 41085aff158SStefan Hajnoczi return false; 41185aff158SStefan Hajnoczi } 41285aff158SStefan Hajnoczi 413edb47ec4SLluís atexit(st_flush_trace_buffer); 414edb47ec4SLluís return true; 415edb47ec4SLluís } 416