4 * Copyright IBM, Corp. 2010
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
15 #include "qemu/timer.h"
16 #include "trace/control.h"
17 #include "trace/simple.h"
18 #include "qemu/error-report.h"
20 /** Trace file header event ID, picked to avoid conflict with real event IDs */
21 #define HEADER_EVENT_ID (~(uint64_t)0)
23 /** Trace file magic number */
24 #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
26 /** Trace file version number, bump if format changes */
27 #define HEADER_VERSION 4
29 /** Records were dropped event ID */
30 #define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
32 /** Trace record is valid */
33 #define TRACE_RECORD_VALID ((uint64_t)1 << 63)
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.
39 static CompatGMutex trace_lock
;
40 static CompatGCond trace_available_cond
;
41 static CompatGCond trace_empty_cond
;
43 static bool trace_available
;
44 static bool trace_writeout_enabled
;
47 TRACE_BUF_LEN
= 4096 * 64,
48 TRACE_BUF_FLUSH_THRESHOLD
= TRACE_BUF_LEN
/ 4,
51 uint8_t trace_buf
[TRACE_BUF_LEN
];
52 static volatile gint trace_idx
;
53 static unsigned int writeout_idx
;
54 static volatile gint dropped_events
;
55 static uint32_t trace_pid
;
56 static FILE *trace_fp
;
57 static char *trace_file_name
;
59 #define TRACE_RECORD_TYPE_MAPPING 0
60 #define TRACE_RECORD_TYPE_EVENT 1
62 /* * Trace buffer entry */
64 uint64_t event
; /* event ID value */
65 uint64_t timestamp_ns
;
66 uint32_t length
; /* in bytes */
72 uint64_t header_event_id
; /* HEADER_EVENT_ID */
73 uint64_t header_magic
; /* HEADER_MAGIC */
74 uint64_t header_version
; /* HEADER_VERSION */
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
);
81 static void clear_buffer_range(unsigned int idx
, size_t len
)
85 if (idx
>= TRACE_BUF_LEN
) {
86 idx
= idx
% TRACE_BUF_LEN
;
93 * Read a trace record from the trace buffer
95 * @idx Trace buffer index
96 * @record Trace record to fill
98 * Returns false if the record is not valid.
100 static bool get_trace_record(unsigned int idx
, TraceRecord
**recordptr
)
102 uint64_t event_flag
= 0;
104 /* read the event flag to see if its a valid record */
105 read_from_buffer(idx
, &record
, sizeof(event_flag
));
107 if (!(record
.event
& TRACE_RECORD_VALID
)) {
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
));
114 *recordptr
= malloc(record
.length
); /* don't use g_malloc, can deadlock when traced */
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.
123 clear_buffer_range(idx
, record
.length
);
128 * Kick writeout thread
130 * @wait Whether to wait for writeout thread to complete
132 static void flush_trace_file(bool wait
)
134 g_mutex_lock(&trace_lock
);
135 trace_available
= true;
136 g_cond_signal(&trace_available_cond
);
139 g_cond_wait(&trace_empty_cond
, &trace_lock
);
142 g_mutex_unlock(&trace_lock
);
145 static void wait_for_trace_records_available(void)
147 g_mutex_lock(&trace_lock
);
148 while (!(trace_available
&& trace_writeout_enabled
)) {
149 g_cond_signal(&trace_empty_cond
);
150 g_cond_wait(&trace_available_cond
, &trace_lock
);
152 trace_available
= false;
153 g_mutex_unlock(&trace_lock
);
156 static gpointer
writeout_thread(gpointer opaque
)
158 TraceRecord
*recordptr
;
161 uint8_t bytes
[sizeof(TraceRecord
) + sizeof(uint64_t)];
163 unsigned int idx
= 0;
165 size_t unused
__attribute__ ((unused
));
166 uint64_t type
= TRACE_RECORD_TYPE_EVENT
;
169 wait_for_trace_records_available();
171 if (g_atomic_int_get(&dropped_events
)) {
172 dropped
.rec
.event
= DROPPED_EVENT_ID
,
173 dropped
.rec
.timestamp_ns
= get_clock();
174 dropped
.rec
.length
= sizeof(TraceRecord
) + sizeof(uint64_t),
175 dropped
.rec
.pid
= trace_pid
;
177 dropped_count
= g_atomic_int_get(&dropped_events
);
178 } while (!g_atomic_int_compare_and_exchange(&dropped_events
,
180 dropped
.rec
.arguments
[0] = dropped_count
;
181 unused
= fwrite(&type
, sizeof(type
), 1, trace_fp
);
182 unused
= fwrite(&dropped
.rec
, dropped
.rec
.length
, 1, trace_fp
);
185 while (get_trace_record(idx
, &recordptr
)) {
186 unused
= fwrite(&type
, sizeof(type
), 1, trace_fp
);
187 unused
= fwrite(recordptr
, recordptr
->length
, 1, trace_fp
);
188 writeout_idx
+= recordptr
->length
;
189 free(recordptr
); /* don't use g_free, can deadlock when traced */
190 idx
= writeout_idx
% TRACE_BUF_LEN
;
198 void trace_record_write_u64(TraceBufferRecord
*rec
, uint64_t val
)
200 rec
->rec_off
= write_to_buffer(rec
->rec_off
, &val
, sizeof(uint64_t));
203 void trace_record_write_str(TraceBufferRecord
*rec
, const char *s
, uint32_t slen
)
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
);
211 int trace_record_start(TraceBufferRecord
*rec
, uint32_t event
, size_t datasize
)
213 unsigned int idx
, rec_off
, old_idx
, new_idx
;
214 uint32_t rec_len
= sizeof(TraceRecord
) + datasize
;
215 uint64_t event_u64
= event
;
216 uint64_t timestamp_ns
= get_clock();
219 old_idx
= g_atomic_int_get(&trace_idx
);
221 new_idx
= old_idx
+ rec_len
;
223 if (new_idx
- writeout_idx
> TRACE_BUF_LEN
) {
224 /* Trace Buffer Full, Event dropped ! */
225 g_atomic_int_inc(&dropped_events
);
228 } while (!g_atomic_int_compare_and_exchange(&trace_idx
, old_idx
, new_idx
));
230 idx
= old_idx
% TRACE_BUF_LEN
;
233 rec_off
= write_to_buffer(rec_off
, &event_u64
, sizeof(event_u64
));
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
));
236 rec_off
= write_to_buffer(rec_off
, &trace_pid
, sizeof(trace_pid
));
239 rec
->rec_off
= (idx
+ sizeof(TraceRecord
)) % TRACE_BUF_LEN
;
243 static void read_from_buffer(unsigned int idx
, void *dataptr
, size_t size
)
245 uint8_t *data_ptr
= dataptr
;
248 if (idx
>= TRACE_BUF_LEN
) {
249 idx
= idx
% TRACE_BUF_LEN
;
251 data_ptr
[x
++] = trace_buf
[idx
++];
255 static unsigned int write_to_buffer(unsigned int idx
, void *dataptr
, size_t size
)
257 uint8_t *data_ptr
= dataptr
;
260 if (idx
>= TRACE_BUF_LEN
) {
261 idx
= idx
% TRACE_BUF_LEN
;
263 trace_buf
[idx
++] = data_ptr
[x
++];
265 return idx
; /* most callers wants to know where to write next */
268 void trace_record_finish(TraceBufferRecord
*rec
)
271 read_from_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
272 smp_wmb(); /* write barrier before marking as valid */
273 record
.event
|= TRACE_RECORD_VALID
;
274 write_to_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
276 if (((unsigned int)g_atomic_int_get(&trace_idx
) - writeout_idx
)
277 > TRACE_BUF_FLUSH_THRESHOLD
) {
278 flush_trace_file(false);
282 static int st_write_event_mapping(void)
284 uint64_t type
= TRACE_RECORD_TYPE_MAPPING
;
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) {
304 void st_set_trace_file_enabled(bool enable
)
306 if (enable
== !!trace_fp
) {
307 return; /* no change */
310 /* Halt trace writeout */
311 flush_trace_file(true);
312 trace_writeout_enabled
= false;
313 flush_trace_file(true);
316 static const TraceLogHeader header
= {
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
,
323 trace_fp
= fopen(trace_file_name
, "wb");
328 if (fwrite(&header
, sizeof header
, 1, trace_fp
) != 1 ||
329 st_write_event_mapping() < 0) {
335 /* Resume trace writeout */
336 trace_writeout_enabled
= true;
337 flush_trace_file(false);
345 * Set the name of a trace file
347 * @file The trace file name or NULL for the default name-<pid> set at
350 void st_set_trace_file(const char *file
)
352 st_set_trace_file_enabled(false);
354 g_free(trace_file_name
);
357 /* Type cast needed for Windows where getpid() returns an int. */
358 trace_file_name
= g_strdup_printf(CONFIG_TRACE_FILE
, (pid_t
)getpid());
360 trace_file_name
= g_strdup_printf("%s", file
);
363 st_set_trace_file_enabled(true);
366 void st_print_trace_file_status(FILE *stream
, int (*stream_printf
)(FILE *stream
, const char *fmt
, ...))
368 stream_printf(stream
, "Trace file \"%s\" %s.\n",
369 trace_file_name
, trace_fp
? "on" : "off");
372 void st_flush_trace_buffer(void)
374 flush_trace_file(true);
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.
382 static GThread
*trace_thread_create(GThreadFunc fn
)
386 sigset_t set
, oldset
;
389 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
392 thread
= g_thread_new("trace-thread", fn
, NULL
);
395 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
405 trace_pid
= getpid();
407 thread
= trace_thread_create(writeout_thread
);
409 warn_report("unable to initialize simple trace backend");
413 atexit(st_flush_trace_buffer
);