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