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