]>
Commit | Line | Data |
---|---|---|
26f7227b SH |
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> | |
85aff158 | 15 | #ifndef _WIN32 |
0b5538c3 SH |
16 | #include <signal.h> |
17 | #include <pthread.h> | |
85aff158 | 18 | #endif |
1de7afc9 | 19 | #include "qemu/timer.h" |
26f7227b | 20 | #include "trace.h" |
e4858974 | 21 | #include "trace/control.h" |
26f7227b SH |
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 */ | |
62bab732 | 30 | #define HEADER_VERSION 2 |
26f7227b | 31 | |
0b5538c3 SH |
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 | ||
0b5538c3 SH |
38 | /* |
39 | * Trace records are written out by a dedicated thread. The thread waits for | |
40 | * records to become available, writes them out, and then waits again. | |
41 | */ | |
85aff158 | 42 | static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; |
4a0e6714 SH |
43 | |
44 | /* g_cond_new() was deprecated in glib 2.31 but we still need to support it */ | |
45 | #if GLIB_CHECK_VERSION(2, 31, 0) | |
46 | static GCond the_trace_available_cond; | |
47 | static GCond the_trace_empty_cond; | |
48 | static GCond *trace_available_cond = &the_trace_available_cond; | |
49 | static GCond *trace_empty_cond = &the_trace_empty_cond; | |
50 | #else | |
85aff158 SH |
51 | static GCond *trace_available_cond; |
52 | static GCond *trace_empty_cond; | |
4a0e6714 SH |
53 | #endif |
54 | ||
0b5538c3 SH |
55 | static bool trace_available; |
56 | static bool trace_writeout_enabled; | |
57 | ||
62bab732 HPB |
58 | enum { |
59 | TRACE_BUF_LEN = 4096 * 64, | |
60 | TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4, | |
61 | }; | |
62 | ||
63 | uint8_t trace_buf[TRACE_BUF_LEN]; | |
30d94087 | 64 | static volatile gint trace_idx; |
62bab732 | 65 | static unsigned int writeout_idx; |
30d94087 | 66 | static volatile gint dropped_events; |
26f7227b | 67 | static FILE *trace_fp; |
4552e410 | 68 | static char *trace_file_name; |
26f7227b | 69 | |
62bab732 HPB |
70 | /* * Trace buffer entry */ |
71 | typedef struct { | |
72 | uint64_t event; /* TraceEventID */ | |
73 | uint64_t timestamp_ns; | |
74 | uint32_t length; /* in bytes */ | |
75 | uint32_t reserved; /* unused */ | |
fb3a5085 | 76 | uint64_t arguments[]; |
62bab732 HPB |
77 | } TraceRecord; |
78 | ||
79 | typedef struct { | |
80 | uint64_t header_event_id; /* HEADER_EVENT_ID */ | |
81 | uint64_t header_magic; /* HEADER_MAGIC */ | |
82 | uint64_t header_version; /* HEADER_VERSION */ | |
8ae601e8 | 83 | } TraceLogHeader; |
62bab732 HPB |
84 | |
85 | ||
86 | static void read_from_buffer(unsigned int idx, void *dataptr, size_t size); | |
87 | static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size); | |
88 | ||
89 | static void clear_buffer_range(unsigned int idx, size_t len) | |
90 | { | |
91 | uint32_t num = 0; | |
92 | while (num < len) { | |
93 | if (idx >= TRACE_BUF_LEN) { | |
94 | idx = idx % TRACE_BUF_LEN; | |
95 | } | |
96 | trace_buf[idx++] = 0; | |
97 | num++; | |
98 | } | |
99 | } | |
c5ceb523 | 100 | /** |
0b5538c3 SH |
101 | * Read a trace record from the trace buffer |
102 | * | |
103 | * @idx Trace buffer index | |
104 | * @record Trace record to fill | |
105 | * | |
106 | * Returns false if the record is not valid. | |
c5ceb523 | 107 | */ |
62bab732 | 108 | static bool get_trace_record(unsigned int idx, TraceRecord **recordptr) |
9410b56c | 109 | { |
62bab732 HPB |
110 | uint64_t event_flag = 0; |
111 | TraceRecord record; | |
112 | /* read the event flag to see if its a valid record */ | |
113 | read_from_buffer(idx, &record, sizeof(event_flag)); | |
114 | ||
115 | if (!(record.event & TRACE_RECORD_VALID)) { | |
0b5538c3 | 116 | return false; |
9410b56c PS |
117 | } |
118 | ||
62bab732 HPB |
119 | smp_rmb(); /* read memory barrier before accessing record */ |
120 | /* read the record header to know record length */ | |
121 | read_from_buffer(idx, &record, sizeof(TraceRecord)); | |
122 | *recordptr = malloc(record.length); /* dont use g_malloc, can deadlock when traced */ | |
123 | /* make a copy of record to avoid being overwritten */ | |
124 | read_from_buffer(idx, *recordptr, record.length); | |
125 | smp_rmb(); /* memory barrier before clearing valid flag */ | |
126 | (*recordptr)->event &= ~TRACE_RECORD_VALID; | |
127 | /* clear the trace buffer range for consumed record otherwise any byte | |
128 | * with its MSB set may be considered as a valid event id when the writer | |
129 | * thread crosses this range of buffer again. | |
130 | */ | |
131 | clear_buffer_range(idx, record.length); | |
c5ceb523 | 132 | return true; |
9410b56c PS |
133 | } |
134 | ||
0b5538c3 SH |
135 | /** |
136 | * Kick writeout thread | |
137 | * | |
138 | * @wait Whether to wait for writeout thread to complete | |
139 | */ | |
140 | static void flush_trace_file(bool wait) | |
26f7227b | 141 | { |
85aff158 | 142 | g_static_mutex_lock(&trace_lock); |
0b5538c3 | 143 | trace_available = true; |
85aff158 | 144 | g_cond_signal(trace_available_cond); |
c5ceb523 | 145 | |
0b5538c3 | 146 | if (wait) { |
85aff158 | 147 | g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); |
26f7227b | 148 | } |
0b5538c3 | 149 | |
85aff158 | 150 | g_static_mutex_unlock(&trace_lock); |
c5ceb523 SH |
151 | } |
152 | ||
0b5538c3 | 153 | static void wait_for_trace_records_available(void) |
c5ceb523 | 154 | { |
85aff158 | 155 | g_static_mutex_lock(&trace_lock); |
0b5538c3 | 156 | while (!(trace_available && trace_writeout_enabled)) { |
85aff158 SH |
157 | g_cond_signal(trace_empty_cond); |
158 | g_cond_wait(trace_available_cond, | |
159 | g_static_mutex_get_mutex(&trace_lock)); | |
c5ceb523 | 160 | } |
0b5538c3 | 161 | trace_available = false; |
85aff158 | 162 | g_static_mutex_unlock(&trace_lock); |
26f7227b SH |
163 | } |
164 | ||
85aff158 | 165 | static gpointer writeout_thread(gpointer opaque) |
26f7227b | 166 | { |
62bab732 HPB |
167 | TraceRecord *recordptr; |
168 | union { | |
169 | TraceRecord rec; | |
170 | uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)]; | |
171 | } dropped; | |
172 | unsigned int idx = 0; | |
fb3a5085 | 173 | int dropped_count; |
0caf448b | 174 | size_t unused __attribute__ ((unused)); |
0b5538c3 SH |
175 | |
176 | for (;;) { | |
177 | wait_for_trace_records_available(); | |
178 | ||
e722d705 | 179 | if (g_atomic_int_get(&dropped_events)) { |
62bab732 HPB |
180 | dropped.rec.event = DROPPED_EVENT_ID, |
181 | dropped.rec.timestamp_ns = get_clock(); | |
fb3a5085 | 182 | dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t), |
62bab732 | 183 | dropped.rec.reserved = 0; |
b6b2c962 | 184 | do { |
e722d705 | 185 | dropped_count = g_atomic_int_get(&dropped_events); |
b6b2c962 MA |
186 | } while (!g_atomic_int_compare_and_exchange(&dropped_events, |
187 | dropped_count, 0)); | |
fb3a5085 | 188 | dropped.rec.arguments[0] = dropped_count; |
62bab732 | 189 | unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp); |
0b5538c3 | 190 | } |
26f7227b | 191 | |
62bab732 HPB |
192 | while (get_trace_record(idx, &recordptr)) { |
193 | unused = fwrite(recordptr, recordptr->length, 1, trace_fp); | |
194 | writeout_idx += recordptr->length; | |
195 | free(recordptr); /* dont use g_free, can deadlock when traced */ | |
196 | idx = writeout_idx % TRACE_BUF_LEN; | |
0b5538c3 | 197 | } |
26f7227b | 198 | |
0b5538c3 | 199 | fflush(trace_fp); |
26f7227b | 200 | } |
0b5538c3 | 201 | return NULL; |
26f7227b SH |
202 | } |
203 | ||
62bab732 | 204 | void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val) |
26f7227b | 205 | { |
62bab732 | 206 | rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t)); |
26f7227b SH |
207 | } |
208 | ||
62bab732 | 209 | void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen) |
26f7227b | 210 | { |
62bab732 HPB |
211 | /* Write string length first */ |
212 | rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen)); | |
213 | /* Write actual string now */ | |
214 | rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen); | |
26f7227b SH |
215 | } |
216 | ||
62bab732 | 217 | int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize) |
26f7227b | 218 | { |
62bab732 HPB |
219 | unsigned int idx, rec_off, old_idx, new_idx; |
220 | uint32_t rec_len = sizeof(TraceRecord) + datasize; | |
221 | uint64_t timestamp_ns = get_clock(); | |
222 | ||
b6b2c962 | 223 | do { |
e722d705 | 224 | old_idx = g_atomic_int_get(&trace_idx); |
62bab732 HPB |
225 | smp_rmb(); |
226 | new_idx = old_idx + rec_len; | |
227 | ||
228 | if (new_idx - writeout_idx > TRACE_BUF_LEN) { | |
229 | /* Trace Buffer Full, Event dropped ! */ | |
fb3a5085 | 230 | g_atomic_int_inc(&dropped_events); |
62bab732 HPB |
231 | return -ENOSPC; |
232 | } | |
b6b2c962 | 233 | } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx)); |
26f7227b | 234 | |
62bab732 | 235 | idx = old_idx % TRACE_BUF_LEN; |
62bab732 HPB |
236 | |
237 | rec_off = idx; | |
83d35d3e HPB |
238 | rec_off = write_to_buffer(rec_off, &event, sizeof(event)); |
239 | rec_off = write_to_buffer(rec_off, ×tamp_ns, sizeof(timestamp_ns)); | |
240 | rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len)); | |
62bab732 HPB |
241 | |
242 | rec->tbuf_idx = idx; | |
243 | rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN; | |
244 | return 0; | |
26f7227b SH |
245 | } |
246 | ||
62bab732 | 247 | static void read_from_buffer(unsigned int idx, void *dataptr, size_t size) |
26f7227b | 248 | { |
62bab732 HPB |
249 | uint8_t *data_ptr = dataptr; |
250 | uint32_t x = 0; | |
251 | while (x < size) { | |
252 | if (idx >= TRACE_BUF_LEN) { | |
253 | idx = idx % TRACE_BUF_LEN; | |
254 | } | |
255 | data_ptr[x++] = trace_buf[idx++]; | |
256 | } | |
26f7227b SH |
257 | } |
258 | ||
62bab732 | 259 | static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size) |
26f7227b | 260 | { |
62bab732 HPB |
261 | uint8_t *data_ptr = dataptr; |
262 | uint32_t x = 0; | |
263 | while (x < size) { | |
264 | if (idx >= TRACE_BUF_LEN) { | |
265 | idx = idx % TRACE_BUF_LEN; | |
266 | } | |
267 | trace_buf[idx++] = data_ptr[x++]; | |
268 | } | |
269 | return idx; /* most callers wants to know where to write next */ | |
26f7227b SH |
270 | } |
271 | ||
62bab732 | 272 | void trace_record_finish(TraceBufferRecord *rec) |
26f7227b | 273 | { |
db8894f2 HPB |
274 | TraceRecord record; |
275 | read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); | |
62bab732 | 276 | smp_wmb(); /* write barrier before marking as valid */ |
db8894f2 HPB |
277 | record.event |= TRACE_RECORD_VALID; |
278 | write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord)); | |
62bab732 | 279 | |
30d94087 | 280 | if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx) |
e722d705 | 281 | > TRACE_BUF_FLUSH_THRESHOLD) { |
62bab732 HPB |
282 | flush_trace_file(false); |
283 | } | |
26f7227b SH |
284 | } |
285 | ||
0b5538c3 SH |
286 | void st_set_trace_file_enabled(bool enable) |
287 | { | |
288 | if (enable == !!trace_fp) { | |
289 | return; /* no change */ | |
290 | } | |
291 | ||
292 | /* Halt trace writeout */ | |
293 | flush_trace_file(true); | |
294 | trace_writeout_enabled = false; | |
295 | flush_trace_file(true); | |
296 | ||
297 | if (enable) { | |
8ae601e8 | 298 | static const TraceLogHeader header = { |
62bab732 HPB |
299 | .header_event_id = HEADER_EVENT_ID, |
300 | .header_magic = HEADER_MAGIC, | |
301 | /* Older log readers will check for version at next location */ | |
302 | .header_version = HEADER_VERSION, | |
0b5538c3 SH |
303 | }; |
304 | ||
6c2a4074 | 305 | trace_fp = fopen(trace_file_name, "wb"); |
0b5538c3 SH |
306 | if (!trace_fp) { |
307 | return; | |
308 | } | |
309 | ||
310 | if (fwrite(&header, sizeof header, 1, trace_fp) != 1) { | |
311 | fclose(trace_fp); | |
312 | trace_fp = NULL; | |
313 | return; | |
314 | } | |
315 | ||
316 | /* Resume trace writeout */ | |
317 | trace_writeout_enabled = true; | |
318 | flush_trace_file(false); | |
319 | } else { | |
320 | fclose(trace_fp); | |
321 | trace_fp = NULL; | |
322 | } | |
323 | } | |
324 | ||
26f7227b | 325 | /** |
0b5538c3 SH |
326 | * Set the name of a trace file |
327 | * | |
328 | * @file The trace file name or NULL for the default name-<pid> set at | |
329 | * config time | |
26f7227b | 330 | */ |
0b5538c3 | 331 | bool st_set_trace_file(const char *file) |
26f7227b | 332 | { |
0b5538c3 SH |
333 | st_set_trace_file_enabled(false); |
334 | ||
4552e410 | 335 | g_free(trace_file_name); |
0b5538c3 SH |
336 | |
337 | if (!file) { | |
4552e410 | 338 | trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid()); |
0b5538c3 | 339 | } else { |
4552e410 | 340 | trace_file_name = g_strdup_printf("%s", file); |
0b5538c3 SH |
341 | } |
342 | ||
343 | st_set_trace_file_enabled(true); | |
344 | return true; | |
345 | } | |
346 | ||
347 | void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) | |
348 | { | |
349 | stream_printf(stream, "Trace file \"%s\" %s.\n", | |
350 | trace_file_name, trace_fp ? "on" : "off"); | |
26f7227b | 351 | } |
22890ab5 | 352 | |
fc764105 LV |
353 | void st_flush_trace_buffer(void) |
354 | { | |
355 | flush_trace_file(true); | |
356 | } | |
357 | ||
358 | void trace_print_events(FILE *stream, fprintf_function stream_printf) | |
22890ab5 PS |
359 | { |
360 | unsigned int i; | |
361 | ||
362 | for (i = 0; i < NR_TRACE_EVENTS; i++) { | |
363 | stream_printf(stream, "%s [Event ID %u] : state %u\n", | |
364 | trace_list[i].tp_name, i, trace_list[i].state); | |
365 | } | |
366 | } | |
367 | ||
fc764105 | 368 | bool trace_event_set_state(const char *name, bool state) |
22890ab5 PS |
369 | { |
370 | unsigned int i; | |
454e202d MW |
371 | unsigned int len; |
372 | bool wildcard = false; | |
373 | bool matched = false; | |
374 | ||
375 | len = strlen(name); | |
376 | if (len > 0 && name[len - 1] == '*') { | |
377 | wildcard = true; | |
378 | len -= 1; | |
379 | } | |
22890ab5 | 380 | for (i = 0; i < NR_TRACE_EVENTS; i++) { |
454e202d MW |
381 | if (wildcard) { |
382 | if (!strncmp(trace_list[i].tp_name, name, len)) { | |
383 | trace_list[i].state = state; | |
384 | matched = true; | |
385 | } | |
386 | continue; | |
387 | } | |
0b5538c3 | 388 | if (!strcmp(trace_list[i].tp_name, name)) { |
fc764105 | 389 | trace_list[i].state = state; |
0b5538c3 | 390 | return true; |
22890ab5 PS |
391 | } |
392 | } | |
454e202d | 393 | return matched; |
0b5538c3 SH |
394 | } |
395 | ||
85aff158 SH |
396 | /* Helper function to create a thread with signals blocked. Use glib's |
397 | * portable threads since QEMU abstractions cannot be used due to reentrancy in | |
398 | * the tracer. Also note the signal masking on POSIX hosts so that the thread | |
399 | * does not steal signals when the rest of the program wants them blocked. | |
400 | */ | |
401 | static GThread *trace_thread_create(GThreadFunc fn) | |
22890ab5 | 402 | { |
85aff158 SH |
403 | GThread *thread; |
404 | #ifndef _WIN32 | |
0b5538c3 | 405 | sigset_t set, oldset; |
22890ab5 | 406 | |
0b5538c3 SH |
407 | sigfillset(&set); |
408 | pthread_sigmask(SIG_SETMASK, &set, &oldset); | |
85aff158 | 409 | #endif |
4a0e6714 SH |
410 | |
411 | #if GLIB_CHECK_VERSION(2, 31, 0) | |
412 | thread = g_thread_new("trace-thread", fn, NULL); | |
413 | #else | |
db3bf869 | 414 | thread = g_thread_create(fn, NULL, FALSE, NULL); |
4a0e6714 SH |
415 | #endif |
416 | ||
85aff158 | 417 | #ifndef _WIN32 |
0b5538c3 | 418 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
85aff158 | 419 | #endif |
0b5538c3 | 420 | |
85aff158 SH |
421 | return thread; |
422 | } | |
423 | ||
424 | bool trace_backend_init(const char *events, const char *file) | |
425 | { | |
426 | GThread *thread; | |
427 | ||
428 | if (!g_thread_supported()) { | |
42ed3727 | 429 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
85aff158 | 430 | g_thread_init(NULL); |
42ed3727 AL |
431 | #else |
432 | fprintf(stderr, "glib threading failed to initialize.\n"); | |
433 | exit(1); | |
434 | #endif | |
85aff158 SH |
435 | } |
436 | ||
4a0e6714 | 437 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
85aff158 SH |
438 | trace_available_cond = g_cond_new(); |
439 | trace_empty_cond = g_cond_new(); | |
4a0e6714 | 440 | #endif |
85aff158 SH |
441 | |
442 | thread = trace_thread_create(writeout_thread); | |
443 | if (!thread) { | |
e4858974 | 444 | fprintf(stderr, "warning: unable to initialize simple trace backend\n"); |
85aff158 | 445 | return false; |
22890ab5 | 446 | } |
0b5538c3 | 447 | |
85aff158 SH |
448 | atexit(st_flush_trace_buffer); |
449 | trace_backend_init_events(events); | |
450 | st_set_trace_file(file); | |
31d3c9b8 | 451 | return true; |
22890ab5 | 452 | } |