Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include <stdlib.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <time.h> |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 15 | #ifndef _WIN32 |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 16 | #include <signal.h> |
| 17 | #include <pthread.h> |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 18 | #endif |
Blue Swirl | c57c846 | 2010-10-23 15:24:07 +0000 | [diff] [blame] | 19 | #include "qemu-timer.h" |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 20 | #include "trace.h" |
Lluís | e485897 | 2011-08-31 20:31:03 +0200 | [diff] [blame] | 21 | #include "trace/control.h" |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 22 | |
| 23 | /** Trace file header event ID */ |
| 24 | #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */ |
| 25 | |
| 26 | /** Trace file magic number */ |
| 27 | #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL |
| 28 | |
| 29 | /** Trace file version number, bump if format changes */ |
| 30 | #define HEADER_VERSION 0 |
| 31 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 32 | /** Records were dropped event ID */ |
| 33 | #define DROPPED_EVENT_ID (~(uint64_t)0 - 1) |
| 34 | |
| 35 | /** Trace record is valid */ |
| 36 | #define TRACE_RECORD_VALID ((uint64_t)1 << 63) |
| 37 | |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 38 | /** Trace buffer entry */ |
| 39 | typedef struct { |
| 40 | uint64_t event; |
| 41 | uint64_t timestamp_ns; |
| 42 | uint64_t x1; |
| 43 | uint64_t x2; |
| 44 | uint64_t x3; |
| 45 | uint64_t x4; |
| 46 | uint64_t x5; |
| 47 | uint64_t x6; |
| 48 | } TraceRecord; |
| 49 | |
| 50 | enum { |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 51 | TRACE_BUF_LEN = 4096, |
| 52 | TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4, |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Trace records are written out by a dedicated thread. The thread waits for |
| 57 | * records to become available, writes them out, and then waits again. |
| 58 | */ |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 59 | static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; |
| 60 | static GCond *trace_available_cond; |
| 61 | static GCond *trace_empty_cond; |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 62 | static bool trace_available; |
| 63 | static bool trace_writeout_enabled; |
| 64 | |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 65 | static TraceRecord trace_buf[TRACE_BUF_LEN]; |
| 66 | static unsigned int trace_idx; |
| 67 | static FILE *trace_fp; |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 68 | static char *trace_file_name = NULL; |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 69 | |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 70 | /** |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 71 | * Read a trace record from the trace buffer |
| 72 | * |
| 73 | * @idx Trace buffer index |
| 74 | * @record Trace record to fill |
| 75 | * |
| 76 | * Returns false if the record is not valid. |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 77 | */ |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 78 | static bool get_trace_record(unsigned int idx, TraceRecord *record) |
Prerna Saxena | 9410b56 | 2010-07-13 09:26:32 +0100 | [diff] [blame] | 79 | { |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 80 | if (!(trace_buf[idx].event & TRACE_RECORD_VALID)) { |
| 81 | return false; |
Prerna Saxena | 9410b56 | 2010-07-13 09:26:32 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 84 | __sync_synchronize(); /* read memory barrier before accessing record */ |
| 85 | |
| 86 | *record = trace_buf[idx]; |
| 87 | record->event &= ~TRACE_RECORD_VALID; |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 88 | return true; |
Prerna Saxena | 9410b56 | 2010-07-13 09:26:32 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Kick writeout thread |
| 93 | * |
| 94 | * @wait Whether to wait for writeout thread to complete |
| 95 | */ |
| 96 | static void flush_trace_file(bool wait) |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 97 | { |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 98 | g_static_mutex_lock(&trace_lock); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 99 | trace_available = true; |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 100 | g_cond_signal(trace_available_cond); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 101 | |
| 102 | if (wait) { |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 103 | g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 106 | g_static_mutex_unlock(&trace_lock); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void wait_for_trace_records_available(void) |
| 110 | { |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 111 | g_static_mutex_lock(&trace_lock); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 112 | while (!(trace_available && trace_writeout_enabled)) { |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 113 | g_cond_signal(trace_empty_cond); |
| 114 | g_cond_wait(trace_available_cond, |
| 115 | g_static_mutex_get_mutex(&trace_lock)); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 116 | } |
| 117 | trace_available = false; |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 118 | g_static_mutex_unlock(&trace_lock); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 121 | static gpointer writeout_thread(gpointer opaque) |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 122 | { |
| 123 | TraceRecord record; |
| 124 | unsigned int writeout_idx = 0; |
| 125 | unsigned int num_available, idx; |
Blue Swirl | 0caf448 | 2011-07-23 21:21:14 +0000 | [diff] [blame] | 126 | size_t unused __attribute__ ((unused)); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 127 | |
| 128 | for (;;) { |
| 129 | wait_for_trace_records_available(); |
| 130 | |
| 131 | num_available = trace_idx - writeout_idx; |
| 132 | if (num_available > TRACE_BUF_LEN) { |
| 133 | record = (TraceRecord){ |
| 134 | .event = DROPPED_EVENT_ID, |
| 135 | .x1 = num_available, |
| 136 | }; |
| 137 | unused = fwrite(&record, sizeof(record), 1, trace_fp); |
| 138 | writeout_idx += num_available; |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 139 | } |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 140 | |
| 141 | idx = writeout_idx % TRACE_BUF_LEN; |
| 142 | while (get_trace_record(idx, &record)) { |
| 143 | trace_buf[idx].event = 0; /* clear valid bit */ |
| 144 | unused = fwrite(&record, sizeof(record), 1, trace_fp); |
| 145 | idx = ++writeout_idx % TRACE_BUF_LEN; |
| 146 | } |
| 147 | |
| 148 | fflush(trace_fp); |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 149 | } |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 150 | return NULL; |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, |
| 154 | uint64_t x4, uint64_t x5, uint64_t x6) |
| 155 | { |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 156 | unsigned int idx; |
| 157 | uint64_t timestamp; |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 158 | |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 159 | if (!trace_list[event].state) { |
| 160 | return; |
| 161 | } |
| 162 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 163 | timestamp = get_clock(); |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 164 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 165 | idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN; |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 166 | trace_buf[idx] = (TraceRecord){ |
| 167 | .event = event, |
| 168 | .timestamp_ns = timestamp, |
| 169 | .x1 = x1, |
| 170 | .x2 = x2, |
| 171 | .x3 = x3, |
| 172 | .x4 = x4, |
| 173 | .x5 = x5, |
| 174 | .x6 = x6, |
| 175 | }; |
| 176 | __sync_synchronize(); /* write barrier before marking as valid */ |
| 177 | trace_buf[idx].event |= TRACE_RECORD_VALID; |
| 178 | |
| 179 | if ((idx + 1) % TRACE_BUF_FLUSH_THRESHOLD == 0) { |
| 180 | flush_trace_file(false); |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void trace0(TraceEventID event) |
| 185 | { |
| 186 | trace(event, 0, 0, 0, 0, 0, 0); |
| 187 | } |
| 188 | |
| 189 | void trace1(TraceEventID event, uint64_t x1) |
| 190 | { |
| 191 | trace(event, x1, 0, 0, 0, 0, 0); |
| 192 | } |
| 193 | |
| 194 | void trace2(TraceEventID event, uint64_t x1, uint64_t x2) |
| 195 | { |
| 196 | trace(event, x1, x2, 0, 0, 0, 0); |
| 197 | } |
| 198 | |
| 199 | void trace3(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3) |
| 200 | { |
| 201 | trace(event, x1, x2, x3, 0, 0, 0); |
| 202 | } |
| 203 | |
| 204 | void trace4(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4) |
| 205 | { |
| 206 | trace(event, x1, x2, x3, x4, 0, 0); |
| 207 | } |
| 208 | |
| 209 | void trace5(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5) |
| 210 | { |
| 211 | trace(event, x1, x2, x3, x4, x5, 0); |
| 212 | } |
| 213 | |
| 214 | void trace6(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6) |
| 215 | { |
| 216 | trace(event, x1, x2, x3, x4, x5, x6); |
| 217 | } |
| 218 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 219 | void st_set_trace_file_enabled(bool enable) |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 220 | { |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 221 | if (enable == !!trace_fp) { |
| 222 | return; /* no change */ |
| 223 | } |
| 224 | |
| 225 | /* Halt trace writeout */ |
| 226 | flush_trace_file(true); |
| 227 | trace_writeout_enabled = false; |
| 228 | flush_trace_file(true); |
| 229 | |
| 230 | if (enable) { |
| 231 | static const TraceRecord header = { |
| 232 | .event = HEADER_EVENT_ID, |
| 233 | .timestamp_ns = HEADER_MAGIC, |
| 234 | .x1 = HEADER_VERSION, |
| 235 | }; |
| 236 | |
Stefan Hajnoczi | 6c2a407 | 2011-09-05 18:31:21 +0100 | [diff] [blame] | 237 | trace_fp = fopen(trace_file_name, "wb"); |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 238 | if (!trace_fp) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (fwrite(&header, sizeof header, 1, trace_fp) != 1) { |
| 243 | fclose(trace_fp); |
| 244 | trace_fp = NULL; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | /* Resume trace writeout */ |
| 249 | trace_writeout_enabled = true; |
| 250 | flush_trace_file(false); |
| 251 | } else { |
| 252 | fclose(trace_fp); |
| 253 | trace_fp = NULL; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Set the name of a trace file |
| 259 | * |
| 260 | * @file The trace file name or NULL for the default name-<pid> set at |
| 261 | * config time |
| 262 | */ |
| 263 | bool st_set_trace_file(const char *file) |
| 264 | { |
| 265 | st_set_trace_file_enabled(false); |
| 266 | |
| 267 | free(trace_file_name); |
| 268 | |
| 269 | if (!file) { |
| 270 | if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) { |
| 271 | trace_file_name = NULL; |
| 272 | return false; |
| 273 | } |
| 274 | } else { |
| 275 | if (asprintf(&trace_file_name, "%s", file) < 0) { |
| 276 | trace_file_name = NULL; |
| 277 | return false; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | st_set_trace_file_enabled(true); |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) |
| 286 | { |
| 287 | stream_printf(stream, "Trace file \"%s\" %s.\n", |
| 288 | trace_file_name, trace_fp ? "on" : "off"); |
Stefan Hajnoczi | 26f7227 | 2010-05-22 19:24:51 +0100 | [diff] [blame] | 289 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 290 | |
| 291 | void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) |
| 292 | { |
| 293 | unsigned int i; |
| 294 | |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 295 | for (i = 0; i < TRACE_BUF_LEN; i++) { |
| 296 | TraceRecord record; |
| 297 | |
| 298 | if (!get_trace_record(i, &record)) { |
| 299 | continue; |
| 300 | } |
Blue Swirl | a12c668 | 2010-10-20 16:41:36 +0000 | [diff] [blame] | 301 | stream_printf(stream, "Event %" PRIu64 " : %" PRIx64 " %" PRIx64 |
| 302 | " %" PRIx64 " %" PRIx64 " %" PRIx64 " %" PRIx64 "\n", |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 303 | record.event, record.x1, record.x2, |
| 304 | record.x3, record.x4, record.x5, |
| 305 | record.x6); |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Lluís | fc76410 | 2011-08-31 20:31:18 +0200 | [diff] [blame] | 309 | void st_flush_trace_buffer(void) |
| 310 | { |
| 311 | flush_trace_file(true); |
| 312 | } |
| 313 | |
| 314 | void trace_print_events(FILE *stream, fprintf_function stream_printf) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 315 | { |
| 316 | unsigned int i; |
| 317 | |
| 318 | for (i = 0; i < NR_TRACE_EVENTS; i++) { |
| 319 | stream_printf(stream, "%s [Event ID %u] : state %u\n", |
| 320 | trace_list[i].tp_name, i, trace_list[i].state); |
| 321 | } |
| 322 | } |
| 323 | |
Lluís | fc76410 | 2011-08-31 20:31:18 +0200 | [diff] [blame] | 324 | bool trace_event_set_state(const char *name, bool state) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 325 | { |
| 326 | unsigned int i; |
Mark Wu | 454e202 | 2011-10-31 11:29:04 +0800 | [diff] [blame] | 327 | unsigned int len; |
| 328 | bool wildcard = false; |
| 329 | bool matched = false; |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 330 | |
Mark Wu | 454e202 | 2011-10-31 11:29:04 +0800 | [diff] [blame] | 331 | len = strlen(name); |
| 332 | if (len > 0 && name[len - 1] == '*') { |
| 333 | wildcard = true; |
| 334 | len -= 1; |
| 335 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 336 | for (i = 0; i < NR_TRACE_EVENTS; i++) { |
Mark Wu | 454e202 | 2011-10-31 11:29:04 +0800 | [diff] [blame] | 337 | if (wildcard) { |
| 338 | if (!strncmp(trace_list[i].tp_name, name, len)) { |
| 339 | trace_list[i].state = state; |
| 340 | matched = true; |
| 341 | } |
| 342 | continue; |
| 343 | } |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 344 | if (!strcmp(trace_list[i].tp_name, name)) { |
Lluís | fc76410 | 2011-08-31 20:31:18 +0200 | [diff] [blame] | 345 | trace_list[i].state = state; |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 346 | return true; |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 347 | } |
| 348 | } |
Mark Wu | 454e202 | 2011-10-31 11:29:04 +0800 | [diff] [blame] | 349 | return matched; |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 350 | } |
| 351 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 352 | /* Helper function to create a thread with signals blocked. Use glib's |
| 353 | * portable threads since QEMU abstractions cannot be used due to reentrancy in |
| 354 | * the tracer. Also note the signal masking on POSIX hosts so that the thread |
| 355 | * does not steal signals when the rest of the program wants them blocked. |
| 356 | */ |
| 357 | static GThread *trace_thread_create(GThreadFunc fn) |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 358 | { |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 359 | GThread *thread; |
| 360 | #ifndef _WIN32 |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 361 | sigset_t set, oldset; |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 362 | |
| 363 | sigfillset(&set); |
| 364 | pthread_sigmask(SIG_SETMASK, &set, &oldset); |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 365 | #endif |
Jun Koi | db3bf86 | 2012-03-08 14:20:37 +0800 | [diff] [blame] | 366 | thread = g_thread_create(fn, NULL, FALSE, NULL); |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 367 | #ifndef _WIN32 |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 368 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 369 | #endif |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 370 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 371 | return thread; |
| 372 | } |
| 373 | |
| 374 | bool trace_backend_init(const char *events, const char *file) |
| 375 | { |
| 376 | GThread *thread; |
| 377 | |
| 378 | if (!g_thread_supported()) { |
Alon Levy | 42ed372 | 2011-12-20 13:41:04 +0200 | [diff] [blame] | 379 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 380 | g_thread_init(NULL); |
Alon Levy | 42ed372 | 2011-12-20 13:41:04 +0200 | [diff] [blame] | 381 | #else |
| 382 | fprintf(stderr, "glib threading failed to initialize.\n"); |
| 383 | exit(1); |
| 384 | #endif |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 385 | } |
Stefan Hajnoczi | 0b5538c | 2011-02-26 18:38:39 +0000 | [diff] [blame] | 386 | |
Stefan Hajnoczi | 85aff15 | 2011-09-05 08:30:17 +0100 | [diff] [blame] | 387 | trace_available_cond = g_cond_new(); |
| 388 | trace_empty_cond = g_cond_new(); |
| 389 | |
| 390 | thread = trace_thread_create(writeout_thread); |
| 391 | if (!thread) { |
| 392 | fprintf(stderr, "warning: unable to initialize simple trace backend\n"); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | atexit(st_flush_trace_buffer); |
| 397 | trace_backend_init_events(events); |
| 398 | st_set_trace_file(file); |
Stefan Hajnoczi | 31d3c9b | 2011-03-13 20:14:30 +0000 | [diff] [blame] | 399 | return true; |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 400 | } |