blob: fc7106ec496d7e4d28a65f6582e372ae8d1e4331 [file] [log] [blame]
Stefan Hajnoczi26f72272010-05-22 19:24:51 +01001/*
2 * Simple trace backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
8 *
9 */
10
Peter Maydelld38ea872016-01-29 17:50:05 +000011#include "qemu/osdep.h"
Stefan Hajnoczi85aff152011-09-05 08:30:17 +010012#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000013#include <pthread.h>
Stefan Hajnoczi85aff152011-09-05 08:30:17 +010014#endif
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010015#include "qemu/timer.h"
Lluíse4858972011-08-31 20:31:03 +020016#include "trace/control.h"
Lluís Vilanovab618c282014-01-14 16:52:55 +010017#include "trace/simple.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070018#include "qemu/error-report.h"
Markus Armbrusterba4912c2019-04-17 21:17:50 +020019#include "qemu/qemu-print.h"
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010020
Daniel P. Berrangeef4c9fc2016-10-04 14:35:49 +010021/** Trace file header event ID, picked to avoid conflict with real event IDs */
22#define HEADER_EVENT_ID (~(uint64_t)0)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010023
24/** Trace file magic number */
25#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
26
27/** Trace file version number, bump if format changes */
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010028#define HEADER_VERSION 4
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010029
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000030/** Records were dropped event ID */
31#define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
32
33/** Trace record is valid */
34#define TRACE_RECORD_VALID ((uint64_t)1 << 63)
35
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000036/*
37 * Trace records are written out by a dedicated thread. The thread waits for
38 * records to become available, writes them out, and then waits again.
39 */
Daniel P. Berrangée7b3af82018-05-04 15:34:46 +010040static GMutex trace_lock;
41static GCond trace_available_cond;
42static GCond trace_empty_cond;
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +010043
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000044static bool trace_available;
45static bool trace_writeout_enabled;
46
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053047enum {
48 TRACE_BUF_LEN = 4096 * 64,
49 TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
50};
51
52uint8_t trace_buf[TRACE_BUF_LEN];
Stefan Hajnoczi30d94082013-02-12 14:34:04 +010053static volatile gint trace_idx;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053054static unsigned int writeout_idx;
Stefan Hajnoczi30d94082013-02-12 14:34:04 +010055static volatile gint dropped_events;
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +020056static uint32_t trace_pid;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010057static FILE *trace_fp;
Stefan Weil4552e412012-08-13 21:51:16 +020058static char *trace_file_name;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010059
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +010060#define TRACE_RECORD_TYPE_MAPPING 0
61#define TRACE_RECORD_TYPE_EVENT 1
62
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053063/* * Trace buffer entry */
64typedef struct {
Daniel P. Berrangeef4c9fc2016-10-04 14:35:49 +010065 uint64_t event; /* event ID value */
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053066 uint64_t timestamp_ns;
67 uint32_t length; /* in bytes */
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +020068 uint32_t pid;
Markus Armbrusterfb3a5082013-01-25 16:43:37 +010069 uint64_t arguments[];
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053070} TraceRecord;
71
72typedef struct {
73 uint64_t header_event_id; /* HEADER_EVENT_ID */
74 uint64_t header_magic; /* HEADER_MAGIC */
75 uint64_t header_version; /* HEADER_VERSION */
Harsh Prateek Bora8ae601e2012-07-20 18:52:12 +053076} TraceLogHeader;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053077
78
79static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
80static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
81
82static void clear_buffer_range(unsigned int idx, size_t len)
83{
84 uint32_t num = 0;
85 while (num < len) {
86 if (idx >= TRACE_BUF_LEN) {
87 idx = idx % TRACE_BUF_LEN;
88 }
89 trace_buf[idx++] = 0;
90 num++;
91 }
92}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +010093/**
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000094 * Read a trace record from the trace buffer
95 *
96 * @idx Trace buffer index
97 * @record Trace record to fill
98 *
99 * Returns false if the record is not valid.
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100100 */
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530101static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
Prerna Saxena9410b562010-07-13 09:26:32 +0100102{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530103 uint64_t event_flag = 0;
104 TraceRecord record;
105 /* read the event flag to see if its a valid record */
106 read_from_buffer(idx, &record, sizeof(event_flag));
107
108 if (!(record.event & TRACE_RECORD_VALID)) {
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000109 return false;
Prerna Saxena9410b562010-07-13 09:26:32 +0100110 }
111
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530112 smp_rmb(); /* read memory barrier before accessing record */
113 /* read the record header to know record length */
114 read_from_buffer(idx, &record, sizeof(TraceRecord));
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100115 *recordptr = malloc(record.length); /* don't use g_malloc, can deadlock when traced */
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530116 /* make a copy of record to avoid being overwritten */
117 read_from_buffer(idx, *recordptr, record.length);
118 smp_rmb(); /* memory barrier before clearing valid flag */
119 (*recordptr)->event &= ~TRACE_RECORD_VALID;
120 /* clear the trace buffer range for consumed record otherwise any byte
121 * with its MSB set may be considered as a valid event id when the writer
122 * thread crosses this range of buffer again.
123 */
124 clear_buffer_range(idx, record.length);
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100125 return true;
Prerna Saxena9410b562010-07-13 09:26:32 +0100126}
127
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000128/**
129 * Kick writeout thread
130 *
131 * @wait Whether to wait for writeout thread to complete
132 */
133static void flush_trace_file(bool wait)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100134{
Michael Tokarev86946a22014-05-08 12:30:46 +0400135 g_mutex_lock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000136 trace_available = true;
Michael Tokarev86946a22014-05-08 12:30:46 +0400137 g_cond_signal(&trace_available_cond);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000138
139 if (wait) {
Michael Tokarev86946a22014-05-08 12:30:46 +0400140 g_cond_wait(&trace_empty_cond, &trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000141 }
142
Michael Tokarev86946a22014-05-08 12:30:46 +0400143 g_mutex_unlock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000144}
145
146static void wait_for_trace_records_available(void)
147{
Michael Tokarev86946a22014-05-08 12:30:46 +0400148 g_mutex_lock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000149 while (!(trace_available && trace_writeout_enabled)) {
Michael Tokarev86946a22014-05-08 12:30:46 +0400150 g_cond_signal(&trace_empty_cond);
151 g_cond_wait(&trace_available_cond, &trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000152 }
153 trace_available = false;
Michael Tokarev86946a22014-05-08 12:30:46 +0400154 g_mutex_unlock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000155}
156
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100157static gpointer writeout_thread(gpointer opaque)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000158{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530159 TraceRecord *recordptr;
160 union {
161 TraceRecord rec;
162 uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
163 } dropped;
164 unsigned int idx = 0;
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100165 int dropped_count;
Blue Swirl0caf4482011-07-23 21:21:14 +0000166 size_t unused __attribute__ ((unused));
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100167 uint64_t type = TRACE_RECORD_TYPE_EVENT;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000168
169 for (;;) {
170 wait_for_trace_records_available();
171
Markus Armbrustere722d702013-01-25 16:43:38 +0100172 if (g_atomic_int_get(&dropped_events)) {
Larry Dewey7ff59202018-11-27 12:08:49 -0700173 dropped.rec.event = DROPPED_EVENT_ID;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530174 dropped.rec.timestamp_ns = get_clock();
Larry Dewey7ff59202018-11-27 12:08:49 -0700175 dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t);
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200176 dropped.rec.pid = trace_pid;
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100177 do {
Markus Armbrustere722d702013-01-25 16:43:38 +0100178 dropped_count = g_atomic_int_get(&dropped_events);
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100179 } while (!g_atomic_int_compare_and_exchange(&dropped_events,
180 dropped_count, 0));
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100181 dropped.rec.arguments[0] = dropped_count;
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100182 unused = fwrite(&type, sizeof(type), 1, trace_fp);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530183 unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100184 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000185
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530186 while (get_trace_record(idx, &recordptr)) {
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100187 unused = fwrite(&type, sizeof(type), 1, trace_fp);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530188 unused = fwrite(recordptr, recordptr->length, 1, trace_fp);
189 writeout_idx += recordptr->length;
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100190 free(recordptr); /* don't use g_free, can deadlock when traced */
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530191 idx = writeout_idx % TRACE_BUF_LEN;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000192 }
193
194 fflush(trace_fp);
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100195 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000196 return NULL;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100197}
198
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530199void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100200{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530201 rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
202}
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100203
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530204void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
205{
206 /* Write string length first */
207 rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
208 /* Write actual string now */
209 rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
210}
211
Daniel P. Berrangeef4c9fc2016-10-04 14:35:49 +0100212int trace_record_start(TraceBufferRecord *rec, uint32_t event, size_t datasize)
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530213{
214 unsigned int idx, rec_off, old_idx, new_idx;
215 uint32_t rec_len = sizeof(TraceRecord) + datasize;
Lluís Vilanova60481e22013-03-05 14:47:55 +0100216 uint64_t event_u64 = event;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530217 uint64_t timestamp_ns = get_clock();
218
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100219 do {
Markus Armbrustere722d702013-01-25 16:43:38 +0100220 old_idx = g_atomic_int_get(&trace_idx);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530221 smp_rmb();
222 new_idx = old_idx + rec_len;
223
224 if (new_idx - writeout_idx > TRACE_BUF_LEN) {
225 /* Trace Buffer Full, Event dropped ! */
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100226 g_atomic_int_inc(&dropped_events);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530227 return -ENOSPC;
228 }
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100229 } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
Prerna Saxena22890ab2010-06-24 17:04:53 +0530230
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530231 idx = old_idx % TRACE_BUF_LEN;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000232
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530233 rec_off = idx;
Lluís Vilanova60481e22013-03-05 14:47:55 +0100234 rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
Harsh Prateek Bora83d35d32012-07-20 18:52:13 +0530235 rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
236 rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200237 rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530238
239 rec->tbuf_idx = idx;
240 rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
241 return 0;
242}
243
244static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
245{
246 uint8_t *data_ptr = dataptr;
247 uint32_t x = 0;
248 while (x < size) {
249 if (idx >= TRACE_BUF_LEN) {
250 idx = idx % TRACE_BUF_LEN;
251 }
252 data_ptr[x++] = trace_buf[idx++];
253 }
254}
255
256static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
257{
258 uint8_t *data_ptr = dataptr;
259 uint32_t x = 0;
260 while (x < size) {
261 if (idx >= TRACE_BUF_LEN) {
262 idx = idx % TRACE_BUF_LEN;
263 }
264 trace_buf[idx++] = data_ptr[x++];
265 }
266 return idx; /* most callers wants to know where to write next */
267}
268
269void trace_record_finish(TraceBufferRecord *rec)
270{
Harsh Prateek Boradb8894f2012-07-20 18:52:15 +0530271 TraceRecord record;
272 read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530273 smp_wmb(); /* write barrier before marking as valid */
Harsh Prateek Boradb8894f2012-07-20 18:52:15 +0530274 record.event |= TRACE_RECORD_VALID;
275 write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530276
Stefan Hajnoczi30d94082013-02-12 14:34:04 +0100277 if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
Markus Armbrustere722d702013-01-25 16:43:38 +0100278 > TRACE_BUF_FLUSH_THRESHOLD) {
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000279 flush_trace_file(false);
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100280 }
281}
282
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100283static int st_write_event_mapping(void)
284{
285 uint64_t type = TRACE_RECORD_TYPE_MAPPING;
286 TraceEventIter iter;
287 TraceEvent *ev;
288
289 trace_event_iter_init(&iter, NULL);
290 while ((ev = trace_event_iter_next(&iter)) != NULL) {
291 uint64_t id = trace_event_get_id(ev);
292 const char *name = trace_event_get_name(ev);
293 uint32_t len = strlen(name);
294 if (fwrite(&type, sizeof(type), 1, trace_fp) != 1 ||
295 fwrite(&id, sizeof(id), 1, trace_fp) != 1 ||
296 fwrite(&len, sizeof(len), 1, trace_fp) != 1 ||
297 fwrite(name, len, 1, trace_fp) != 1) {
298 return -1;
299 }
300 }
301
302 return 0;
303}
304
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000305void st_set_trace_file_enabled(bool enable)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100306{
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000307 if (enable == !!trace_fp) {
308 return; /* no change */
309 }
310
311 /* Halt trace writeout */
312 flush_trace_file(true);
313 trace_writeout_enabled = false;
314 flush_trace_file(true);
315
316 if (enable) {
Harsh Prateek Bora8ae601e2012-07-20 18:52:12 +0530317 static const TraceLogHeader header = {
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530318 .header_event_id = HEADER_EVENT_ID,
319 .header_magic = HEADER_MAGIC,
320 /* Older log readers will check for version at next location */
321 .header_version = HEADER_VERSION,
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000322 };
323
Stefan Hajnoczi6c2a4072011-09-05 18:31:21 +0100324 trace_fp = fopen(trace_file_name, "wb");
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000325 if (!trace_fp) {
326 return;
327 }
328
Daniel P. Berrange7f1b5882016-10-04 14:35:50 +0100329 if (fwrite(&header, sizeof header, 1, trace_fp) != 1 ||
330 st_write_event_mapping() < 0) {
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000331 fclose(trace_fp);
332 trace_fp = NULL;
333 return;
334 }
335
336 /* Resume trace writeout */
337 trace_writeout_enabled = true;
338 flush_trace_file(false);
339 } else {
340 fclose(trace_fp);
341 trace_fp = NULL;
342 }
343}
344
345/**
346 * Set the name of a trace file
347 *
348 * @file The trace file name or NULL for the default name-<pid> set at
349 * config time
350 */
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300351void st_set_trace_file(const char *file)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000352{
353 st_set_trace_file_enabled(false);
354
Stefan Weil4552e412012-08-13 21:51:16 +0200355 g_free(trace_file_name);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000356
357 if (!file) {
Stefan Weil857a0e32015-03-11 22:08:56 +0100358 /* Type cast needed for Windows where getpid() returns an int. */
359 trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000360 } else {
Stefan Weil4552e412012-08-13 21:51:16 +0200361 trace_file_name = g_strdup_printf("%s", file);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000362 }
363
364 st_set_trace_file_enabled(true);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000365}
366
Markus Armbrusterba4912c2019-04-17 21:17:50 +0200367void st_print_trace_file_status(void)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000368{
Markus Armbrusterba4912c2019-04-17 21:17:50 +0200369 qemu_printf("Trace file \"%s\" %s.\n",
370 trace_file_name, trace_fp ? "on" : "off");
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100371}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530372
Lluísfc764102011-08-31 20:31:18 +0200373void st_flush_trace_buffer(void)
374{
375 flush_trace_file(true);
376}
377
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100378/* Helper function to create a thread with signals blocked. Use glib's
379 * portable threads since QEMU abstractions cannot be used due to reentrancy in
380 * the tracer. Also note the signal masking on POSIX hosts so that the thread
381 * does not steal signals when the rest of the program wants them blocked.
382 */
383static GThread *trace_thread_create(GThreadFunc fn)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000384{
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100385 GThread *thread;
386#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000387 sigset_t set, oldset;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000388
389 sigfillset(&set);
390 pthread_sigmask(SIG_SETMASK, &set, &oldset);
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100391#endif
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100392
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100393 thread = g_thread_new("trace-thread", fn, NULL);
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100394
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100395#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000396 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100397#endif
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000398
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100399 return thread;
400}
401
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300402bool st_init(void)
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100403{
404 GThread *thread;
405
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200406 trace_pid = getpid();
407
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100408 thread = trace_thread_create(writeout_thread);
409 if (!thread) {
Alistair Francis2ab4b132017-09-11 12:52:50 -0700410 warn_report("unable to initialize simple trace backend");
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100411 return false;
412 }
413
414 atexit(st_flush_trace_buffer);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +0000415 return true;
Prerna Saxena22890ab2010-06-24 17:04:53 +0530416}