xref: /openbmc/qemu/trace/simple.c (revision 857a0e38)
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"
22b618c288SLluí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 */
31ef0bd3bbSLluís Vilanova #define HEADER_VERSION 3
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  */
4386946a2dSMichael Tokarev static CompatGMutex trace_lock;
4486946a2dSMichael Tokarev static CompatGCond trace_available_cond;
4586946a2dSMichael Tokarev static CompatGCond trace_empty_cond;
464a0e6714SStefan Hajnoczi 
47edb47ec4SLluís static bool trace_available;
48edb47ec4SLluís static bool trace_writeout_enabled;
49edb47ec4SLluís 
5062bab732SHarsh Prateek Bora enum {
5162bab732SHarsh Prateek Bora     TRACE_BUF_LEN = 4096 * 64,
5262bab732SHarsh Prateek Bora     TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
5362bab732SHarsh Prateek Bora };
5462bab732SHarsh Prateek Bora 
5562bab732SHarsh Prateek Bora uint8_t trace_buf[TRACE_BUF_LEN];
5630d94087SStefan Hajnoczi static volatile gint trace_idx;
5762bab732SHarsh Prateek Bora static unsigned int writeout_idx;
5830d94087SStefan Hajnoczi static volatile gint dropped_events;
5926896cbfSStefan Hajnoczi static uint32_t trace_pid;
60edb47ec4SLluís static FILE *trace_fp;
614552e410SStefan Weil static char *trace_file_name;
62edb47ec4SLluís 
6362bab732SHarsh Prateek Bora /* * Trace buffer entry */
6462bab732SHarsh Prateek Bora typedef struct {
6562bab732SHarsh Prateek Bora     uint64_t event; /*   TraceEventID */
6662bab732SHarsh Prateek Bora     uint64_t timestamp_ns;
6762bab732SHarsh Prateek Bora     uint32_t length;   /*    in bytes */
6826896cbfSStefan Hajnoczi     uint32_t pid;
69fb3a5085SMarkus Armbruster     uint64_t arguments[];
7062bab732SHarsh Prateek Bora } TraceRecord;
7162bab732SHarsh Prateek Bora 
7262bab732SHarsh Prateek Bora typedef struct {
7362bab732SHarsh Prateek Bora     uint64_t header_event_id; /* HEADER_EVENT_ID */
7462bab732SHarsh Prateek Bora     uint64_t header_magic;    /* HEADER_MAGIC    */
7562bab732SHarsh Prateek Bora     uint64_t header_version;  /* HEADER_VERSION  */
768ae601e8SHarsh Prateek Bora } TraceLogHeader;
7762bab732SHarsh Prateek Bora 
7862bab732SHarsh Prateek Bora 
7962bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
8062bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
8162bab732SHarsh Prateek Bora 
8262bab732SHarsh Prateek Bora static void clear_buffer_range(unsigned int idx, size_t len)
8362bab732SHarsh Prateek Bora {
8462bab732SHarsh Prateek Bora     uint32_t num = 0;
8562bab732SHarsh Prateek Bora     while (num < len) {
8662bab732SHarsh Prateek Bora         if (idx >= TRACE_BUF_LEN) {
8762bab732SHarsh Prateek Bora             idx = idx % TRACE_BUF_LEN;
8862bab732SHarsh Prateek Bora         }
8962bab732SHarsh Prateek Bora         trace_buf[idx++] = 0;
9062bab732SHarsh Prateek Bora         num++;
9162bab732SHarsh Prateek Bora     }
9262bab732SHarsh Prateek Bora }
93edb47ec4SLluís /**
94edb47ec4SLluís  * Read a trace record from the trace buffer
95edb47ec4SLluís  *
96edb47ec4SLluís  * @idx         Trace buffer index
97edb47ec4SLluís  * @record      Trace record to fill
98edb47ec4SLluís  *
99edb47ec4SLluís  * Returns false if the record is not valid.
100edb47ec4SLluís  */
10162bab732SHarsh Prateek Bora static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
102edb47ec4SLluís {
10362bab732SHarsh Prateek Bora     uint64_t event_flag = 0;
10462bab732SHarsh Prateek Bora     TraceRecord record;
10562bab732SHarsh Prateek Bora     /* read the event flag to see if its a valid record */
10662bab732SHarsh Prateek Bora     read_from_buffer(idx, &record, sizeof(event_flag));
10762bab732SHarsh Prateek Bora 
10862bab732SHarsh Prateek Bora     if (!(record.event & TRACE_RECORD_VALID)) {
109edb47ec4SLluís         return false;
110edb47ec4SLluís     }
111edb47ec4SLluís 
11262bab732SHarsh Prateek Bora     smp_rmb(); /* read memory barrier before accessing record */
11362bab732SHarsh Prateek Bora     /* read the record header to know record length */
11462bab732SHarsh Prateek Bora     read_from_buffer(idx, &record, sizeof(TraceRecord));
11562bab732SHarsh Prateek Bora     *recordptr = malloc(record.length); /* dont use g_malloc, can deadlock when traced */
11662bab732SHarsh Prateek Bora     /* make a copy of record to avoid being overwritten */
11762bab732SHarsh Prateek Bora     read_from_buffer(idx, *recordptr, record.length);
11862bab732SHarsh Prateek Bora     smp_rmb(); /* memory barrier before clearing valid flag */
11962bab732SHarsh Prateek Bora     (*recordptr)->event &= ~TRACE_RECORD_VALID;
12062bab732SHarsh Prateek Bora     /* clear the trace buffer range for consumed record otherwise any byte
12162bab732SHarsh Prateek Bora      * with its MSB set may be considered as a valid event id when the writer
12262bab732SHarsh Prateek Bora      * thread crosses this range of buffer again.
12362bab732SHarsh Prateek Bora      */
12462bab732SHarsh Prateek Bora     clear_buffer_range(idx, record.length);
125edb47ec4SLluís     return true;
126edb47ec4SLluís }
127edb47ec4SLluís 
128edb47ec4SLluís /**
129edb47ec4SLluís  * Kick writeout thread
130edb47ec4SLluís  *
131edb47ec4SLluís  * @wait        Whether to wait for writeout thread to complete
132edb47ec4SLluís  */
133edb47ec4SLluís static void flush_trace_file(bool wait)
134edb47ec4SLluís {
13586946a2dSMichael Tokarev     g_mutex_lock(&trace_lock);
136edb47ec4SLluís     trace_available = true;
13786946a2dSMichael Tokarev     g_cond_signal(&trace_available_cond);
138edb47ec4SLluís 
139edb47ec4SLluís     if (wait) {
14086946a2dSMichael Tokarev         g_cond_wait(&trace_empty_cond, &trace_lock);
141edb47ec4SLluís     }
142edb47ec4SLluís 
14386946a2dSMichael Tokarev     g_mutex_unlock(&trace_lock);
144edb47ec4SLluís }
145edb47ec4SLluís 
146edb47ec4SLluís static void wait_for_trace_records_available(void)
147edb47ec4SLluís {
14886946a2dSMichael Tokarev     g_mutex_lock(&trace_lock);
149edb47ec4SLluís     while (!(trace_available && trace_writeout_enabled)) {
15086946a2dSMichael Tokarev         g_cond_signal(&trace_empty_cond);
15186946a2dSMichael Tokarev         g_cond_wait(&trace_available_cond, &trace_lock);
152edb47ec4SLluís     }
153edb47ec4SLluís     trace_available = false;
15486946a2dSMichael Tokarev     g_mutex_unlock(&trace_lock);
155edb47ec4SLluís }
156edb47ec4SLluís 
15785aff158SStefan Hajnoczi static gpointer writeout_thread(gpointer opaque)
158edb47ec4SLluís {
15962bab732SHarsh Prateek Bora     TraceRecord *recordptr;
16062bab732SHarsh Prateek Bora     union {
16162bab732SHarsh Prateek Bora         TraceRecord rec;
16262bab732SHarsh Prateek Bora         uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
16362bab732SHarsh Prateek Bora     } dropped;
16462bab732SHarsh Prateek Bora     unsigned int idx = 0;
165fb3a5085SMarkus Armbruster     int dropped_count;
166edb47ec4SLluís     size_t unused __attribute__ ((unused));
167edb47ec4SLluís 
168edb47ec4SLluís     for (;;) {
169edb47ec4SLluís         wait_for_trace_records_available();
170edb47ec4SLluís 
171e722d705SMarkus Armbruster         if (g_atomic_int_get(&dropped_events)) {
17262bab732SHarsh Prateek Bora             dropped.rec.event = DROPPED_EVENT_ID,
17362bab732SHarsh Prateek Bora             dropped.rec.timestamp_ns = get_clock();
174fb3a5085SMarkus Armbruster             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;
18162bab732SHarsh Prateek Bora             unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
182edb47ec4SLluís         }
183edb47ec4SLluís 
18462bab732SHarsh Prateek Bora         while (get_trace_record(idx, &recordptr)) {
18562bab732SHarsh Prateek Bora             unused = fwrite(recordptr, recordptr->length, 1, trace_fp);
18662bab732SHarsh Prateek Bora             writeout_idx += recordptr->length;
18762bab732SHarsh Prateek Bora             free(recordptr); /* dont use g_free, can deadlock when traced */
188edb47ec4SLluís             idx = writeout_idx % TRACE_BUF_LEN;
189edb47ec4SLluís         }
190edb47ec4SLluís 
191edb47ec4SLluís         fflush(trace_fp);
192edb47ec4SLluís     }
193edb47ec4SLluís     return NULL;
194edb47ec4SLluís }
195edb47ec4SLluís 
19662bab732SHarsh Prateek Bora void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
197edb47ec4SLluís {
19862bab732SHarsh Prateek Bora     rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
199edb47ec4SLluís }
200edb47ec4SLluís 
20162bab732SHarsh Prateek Bora void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
20262bab732SHarsh Prateek Bora {
20362bab732SHarsh Prateek Bora     /* Write string length first */
20462bab732SHarsh Prateek Bora     rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
20562bab732SHarsh Prateek Bora     /* Write actual string now */
20662bab732SHarsh Prateek Bora     rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
20762bab732SHarsh Prateek Bora }
208edb47ec4SLluís 
20962bab732SHarsh Prateek Bora int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize)
21062bab732SHarsh Prateek Bora {
21162bab732SHarsh Prateek Bora     unsigned int idx, rec_off, old_idx, new_idx;
21262bab732SHarsh Prateek Bora     uint32_t rec_len = sizeof(TraceRecord) + datasize;
21360481e21SLluís Vilanova     uint64_t event_u64 = event;
21462bab732SHarsh Prateek Bora     uint64_t timestamp_ns = get_clock();
21562bab732SHarsh Prateek Bora 
216b6b2c962SMarkus Armbruster     do {
217e722d705SMarkus Armbruster         old_idx = g_atomic_int_get(&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 ! */
223fb3a5085SMarkus Armbruster             g_atomic_int_inc(&dropped_events);
22462bab732SHarsh Prateek Bora             return -ENOSPC;
22562bab732SHarsh Prateek Bora         }
226b6b2c962SMarkus Armbruster     } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
22762bab732SHarsh Prateek Bora 
22862bab732SHarsh Prateek Bora     idx = old_idx % TRACE_BUF_LEN;
22962bab732SHarsh Prateek Bora 
23062bab732SHarsh Prateek Bora     rec_off = idx;
23160481e21SLluís Vilanova     rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
23283d35d3eSHarsh Prateek Bora     rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
23383d35d3eSHarsh Prateek Bora     rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
23426896cbfSStefan Hajnoczi     rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid));
23562bab732SHarsh Prateek Bora 
23662bab732SHarsh Prateek Bora     rec->tbuf_idx = idx;
23762bab732SHarsh Prateek Bora     rec->rec_off  = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
23862bab732SHarsh Prateek Bora     return 0;
23962bab732SHarsh Prateek Bora }
24062bab732SHarsh Prateek Bora 
24162bab732SHarsh Prateek Bora static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
24262bab732SHarsh Prateek Bora {
24362bab732SHarsh Prateek Bora     uint8_t *data_ptr = dataptr;
24462bab732SHarsh Prateek Bora     uint32_t x = 0;
24562bab732SHarsh Prateek Bora     while (x < size) {
24662bab732SHarsh Prateek Bora         if (idx >= TRACE_BUF_LEN) {
24762bab732SHarsh Prateek Bora             idx = idx % TRACE_BUF_LEN;
24862bab732SHarsh Prateek Bora         }
24962bab732SHarsh Prateek Bora         data_ptr[x++] = trace_buf[idx++];
25062bab732SHarsh Prateek Bora     }
25162bab732SHarsh Prateek Bora }
25262bab732SHarsh Prateek Bora 
25362bab732SHarsh Prateek Bora static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
25462bab732SHarsh Prateek Bora {
25562bab732SHarsh Prateek Bora     uint8_t *data_ptr = dataptr;
25662bab732SHarsh Prateek Bora     uint32_t x = 0;
25762bab732SHarsh Prateek Bora     while (x < size) {
25862bab732SHarsh Prateek Bora         if (idx >= TRACE_BUF_LEN) {
25962bab732SHarsh Prateek Bora             idx = idx % TRACE_BUF_LEN;
26062bab732SHarsh Prateek Bora         }
26162bab732SHarsh Prateek Bora         trace_buf[idx++] = data_ptr[x++];
26262bab732SHarsh Prateek Bora     }
26362bab732SHarsh Prateek Bora     return idx; /* most callers wants to know where to write next */
26462bab732SHarsh Prateek Bora }
26562bab732SHarsh Prateek Bora 
26662bab732SHarsh Prateek Bora void trace_record_finish(TraceBufferRecord *rec)
26762bab732SHarsh Prateek Bora {
268db8894f2SHarsh Prateek Bora     TraceRecord record;
269db8894f2SHarsh Prateek Bora     read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
27062bab732SHarsh Prateek Bora     smp_wmb(); /* write barrier before marking as valid */
271db8894f2SHarsh Prateek Bora     record.event |= TRACE_RECORD_VALID;
272db8894f2SHarsh Prateek Bora     write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
27362bab732SHarsh Prateek Bora 
27430d94087SStefan Hajnoczi     if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
275e722d705SMarkus Armbruster         > TRACE_BUF_FLUSH_THRESHOLD) {
276edb47ec4SLluís         flush_trace_file(false);
277edb47ec4SLluís     }
278edb47ec4SLluís }
279edb47ec4SLluís 
280edb47ec4SLluís void st_set_trace_file_enabled(bool enable)
281edb47ec4SLluís {
282edb47ec4SLluís     if (enable == !!trace_fp) {
283edb47ec4SLluís         return; /* no change */
284edb47ec4SLluís     }
285edb47ec4SLluís 
286edb47ec4SLluís     /* Halt trace writeout */
287edb47ec4SLluís     flush_trace_file(true);
288edb47ec4SLluís     trace_writeout_enabled = false;
289edb47ec4SLluís     flush_trace_file(true);
290edb47ec4SLluís 
291edb47ec4SLluís     if (enable) {
2928ae601e8SHarsh Prateek Bora         static const TraceLogHeader header = {
29362bab732SHarsh Prateek Bora             .header_event_id = HEADER_EVENT_ID,
29462bab732SHarsh Prateek Bora             .header_magic = HEADER_MAGIC,
29562bab732SHarsh Prateek Bora             /* Older log readers will check for version at next location */
29662bab732SHarsh Prateek Bora             .header_version = HEADER_VERSION,
297edb47ec4SLluís         };
298edb47ec4SLluís 
2996c2a4074SStefan Hajnoczi         trace_fp = fopen(trace_file_name, "wb");
300edb47ec4SLluís         if (!trace_fp) {
301edb47ec4SLluís             return;
302edb47ec4SLluís         }
303edb47ec4SLluís 
304edb47ec4SLluís         if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
305edb47ec4SLluís             fclose(trace_fp);
306edb47ec4SLluís             trace_fp = NULL;
307edb47ec4SLluís             return;
308edb47ec4SLluís         }
309edb47ec4SLluís 
310edb47ec4SLluís         /* Resume trace writeout */
311edb47ec4SLluís         trace_writeout_enabled = true;
312edb47ec4SLluís         flush_trace_file(false);
313edb47ec4SLluís     } else {
314edb47ec4SLluís         fclose(trace_fp);
315edb47ec4SLluís         trace_fp = NULL;
316edb47ec4SLluís     }
317edb47ec4SLluís }
318edb47ec4SLluís 
319edb47ec4SLluís /**
320edb47ec4SLluís  * Set the name of a trace file
321edb47ec4SLluís  *
322edb47ec4SLluís  * @file        The trace file name or NULL for the default name-<pid> set at
323edb47ec4SLluís  *              config time
324edb47ec4SLluís  */
325edb47ec4SLluís bool st_set_trace_file(const char *file)
326edb47ec4SLluís {
327edb47ec4SLluís     st_set_trace_file_enabled(false);
328edb47ec4SLluís 
3294552e410SStefan Weil     g_free(trace_file_name);
330edb47ec4SLluís 
331edb47ec4SLluís     if (!file) {
332*857a0e38SStefan Weil         /* Type cast needed for Windows where getpid() returns an int. */
333*857a0e38SStefan Weil         trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
334edb47ec4SLluís     } else {
3354552e410SStefan Weil         trace_file_name = g_strdup_printf("%s", file);
336edb47ec4SLluís     }
337edb47ec4SLluís 
338edb47ec4SLluís     st_set_trace_file_enabled(true);
339edb47ec4SLluís     return true;
340edb47ec4SLluís }
341edb47ec4SLluís 
342edb47ec4SLluís void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
343edb47ec4SLluís {
344edb47ec4SLluís     stream_printf(stream, "Trace file \"%s\" %s.\n",
345edb47ec4SLluís                   trace_file_name, trace_fp ? "on" : "off");
346edb47ec4SLluís }
347edb47ec4SLluís 
348fc764105SLluís void st_flush_trace_buffer(void)
349fc764105SLluís {
350fc764105SLluís     flush_trace_file(true);
351fc764105SLluís }
352fc764105SLluís 
35385aff158SStefan Hajnoczi /* Helper function to create a thread with signals blocked.  Use glib's
35485aff158SStefan Hajnoczi  * portable threads since QEMU abstractions cannot be used due to reentrancy in
35585aff158SStefan Hajnoczi  * the tracer.  Also note the signal masking on POSIX hosts so that the thread
35685aff158SStefan Hajnoczi  * does not steal signals when the rest of the program wants them blocked.
35785aff158SStefan Hajnoczi  */
35885aff158SStefan Hajnoczi static GThread *trace_thread_create(GThreadFunc fn)
359edb47ec4SLluís {
36085aff158SStefan Hajnoczi     GThread *thread;
36185aff158SStefan Hajnoczi #ifndef _WIN32
362edb47ec4SLluís     sigset_t set, oldset;
363edb47ec4SLluís 
364edb47ec4SLluís     sigfillset(&set);
365edb47ec4SLluís     pthread_sigmask(SIG_SETMASK, &set, &oldset);
36685aff158SStefan Hajnoczi #endif
3674a0e6714SStefan Hajnoczi 
3684a0e6714SStefan Hajnoczi     thread = g_thread_new("trace-thread", fn, NULL);
3694a0e6714SStefan Hajnoczi 
37085aff158SStefan Hajnoczi #ifndef _WIN32
371edb47ec4SLluís     pthread_sigmask(SIG_SETMASK, &oldset, NULL);
37285aff158SStefan Hajnoczi #endif
373edb47ec4SLluís 
37485aff158SStefan Hajnoczi     return thread;
37585aff158SStefan Hajnoczi }
37685aff158SStefan Hajnoczi 
3775b808275SLluís Vilanova bool st_init(const char *file)
37885aff158SStefan Hajnoczi {
37985aff158SStefan Hajnoczi     GThread *thread;
38085aff158SStefan Hajnoczi 
38126896cbfSStefan Hajnoczi     trace_pid = getpid();
38226896cbfSStefan Hajnoczi 
38385aff158SStefan Hajnoczi     thread = trace_thread_create(writeout_thread);
38485aff158SStefan Hajnoczi     if (!thread) {
385e4858974SLluís         fprintf(stderr, "warning: unable to initialize simple trace backend\n");
38685aff158SStefan Hajnoczi         return false;
38785aff158SStefan Hajnoczi     }
38885aff158SStefan Hajnoczi 
389edb47ec4SLluís     atexit(st_flush_trace_buffer);
390edb47ec4SLluís     st_set_trace_file(file);
391edb47ec4SLluís     return true;
392edb47ec4SLluís }
393