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