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"
19 /** Trace file header event ID, picked to avoid conflict with real event IDs */
20 #define HEADER_EVENT_ID (~(uint64_t)0)
22 /** Trace file magic number */
23 #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
25 /** Trace file version number, bump if format changes */
26 #define HEADER_VERSION 4
28 /** Records were dropped event ID */
29 #define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
31 /** Trace record is valid */
32 #define TRACE_RECORD_VALID ((uint64_t)1 << 63)
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.
38 static CompatGMutex trace_lock
;
39 static CompatGCond trace_available_cond
;
40 static CompatGCond trace_empty_cond
;
42 static bool trace_available
;
43 static bool trace_writeout_enabled
;
46 TRACE_BUF_LEN
= 4096 * 64,
47 TRACE_BUF_FLUSH_THRESHOLD
= TRACE_BUF_LEN
/ 4,
50 uint8_t trace_buf
[TRACE_BUF_LEN
];
51 static volatile gint trace_idx
;
52 static unsigned int writeout_idx
;
53 static volatile gint dropped_events
;
54 static uint32_t trace_pid
;
55 static FILE *trace_fp
;
56 static char *trace_file_name
;
58 #define TRACE_RECORD_TYPE_MAPPING 0
59 #define TRACE_RECORD_TYPE_EVENT 1
61 /* * Trace buffer entry */
63 uint64_t event
; /* event ID value */
64 uint64_t timestamp_ns
;
65 uint32_t length
; /* in bytes */
71 uint64_t header_event_id
; /* HEADER_EVENT_ID */
72 uint64_t header_magic
; /* HEADER_MAGIC */
73 uint64_t header_version
; /* HEADER_VERSION */
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
);
80 static void clear_buffer_range(unsigned int idx
, size_t len
)
84 if (idx
>= TRACE_BUF_LEN
) {
85 idx
= idx
% TRACE_BUF_LEN
;
92 * Read a trace record from the trace buffer
94 * @idx Trace buffer index
95 * @record Trace record to fill
97 * Returns false if the record is not valid.
99 static bool get_trace_record(unsigned int idx
, TraceRecord
**recordptr
)
101 uint64_t event_flag
= 0;
103 /* read the event flag to see if its a valid record */
104 read_from_buffer(idx
, &record
, sizeof(event_flag
));
106 if (!(record
.event
& TRACE_RECORD_VALID
)) {
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
));
113 *recordptr
= malloc(record
.length
); /* don't use g_malloc, can deadlock when traced */
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.
122 clear_buffer_range(idx
, record
.length
);
127 * Kick writeout thread
129 * @wait Whether to wait for writeout thread to complete
131 static void flush_trace_file(bool wait
)
133 g_mutex_lock(&trace_lock
);
134 trace_available
= true;
135 g_cond_signal(&trace_available_cond
);
138 g_cond_wait(&trace_empty_cond
, &trace_lock
);
141 g_mutex_unlock(&trace_lock
);
144 static void wait_for_trace_records_available(void)
146 g_mutex_lock(&trace_lock
);
147 while (!(trace_available
&& trace_writeout_enabled
)) {
148 g_cond_signal(&trace_empty_cond
);
149 g_cond_wait(&trace_available_cond
, &trace_lock
);
151 trace_available
= false;
152 g_mutex_unlock(&trace_lock
);
155 static gpointer
writeout_thread(gpointer opaque
)
157 TraceRecord
*recordptr
;
160 uint8_t bytes
[sizeof(TraceRecord
) + sizeof(uint64_t)];
162 unsigned int idx
= 0;
164 size_t unused
__attribute__ ((unused
));
165 uint64_t type
= TRACE_RECORD_TYPE_EVENT
;
168 wait_for_trace_records_available();
170 if (g_atomic_int_get(&dropped_events
)) {
171 dropped
.rec
.event
= DROPPED_EVENT_ID
,
172 dropped
.rec
.timestamp_ns
= get_clock();
173 dropped
.rec
.length
= sizeof(TraceRecord
) + sizeof(uint64_t),
174 dropped
.rec
.pid
= trace_pid
;
176 dropped_count
= g_atomic_int_get(&dropped_events
);
177 } while (!g_atomic_int_compare_and_exchange(&dropped_events
,
179 dropped
.rec
.arguments
[0] = dropped_count
;
180 unused
= fwrite(&type
, sizeof(type
), 1, trace_fp
);
181 unused
= fwrite(&dropped
.rec
, dropped
.rec
.length
, 1, trace_fp
);
184 while (get_trace_record(idx
, &recordptr
)) {
185 unused
= fwrite(&type
, sizeof(type
), 1, trace_fp
);
186 unused
= fwrite(recordptr
, recordptr
->length
, 1, trace_fp
);
187 writeout_idx
+= recordptr
->length
;
188 free(recordptr
); /* don't use g_free, can deadlock when traced */
189 idx
= writeout_idx
% TRACE_BUF_LEN
;
197 void trace_record_write_u64(TraceBufferRecord
*rec
, uint64_t val
)
199 rec
->rec_off
= write_to_buffer(rec
->rec_off
, &val
, sizeof(uint64_t));
202 void trace_record_write_str(TraceBufferRecord
*rec
, const char *s
, uint32_t slen
)
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
);
210 int trace_record_start(TraceBufferRecord
*rec
, uint32_t event
, size_t datasize
)
212 unsigned int idx
, rec_off
, old_idx
, new_idx
;
213 uint32_t rec_len
= sizeof(TraceRecord
) + datasize
;
214 uint64_t event_u64
= event
;
215 uint64_t timestamp_ns
= get_clock();
218 old_idx
= g_atomic_int_get(&trace_idx
);
220 new_idx
= old_idx
+ rec_len
;
222 if (new_idx
- writeout_idx
> TRACE_BUF_LEN
) {
223 /* Trace Buffer Full, Event dropped ! */
224 g_atomic_int_inc(&dropped_events
);
227 } while (!g_atomic_int_compare_and_exchange(&trace_idx
, old_idx
, new_idx
));
229 idx
= old_idx
% TRACE_BUF_LEN
;
232 rec_off
= write_to_buffer(rec_off
, &event_u64
, sizeof(event_u64
));
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
));
235 rec_off
= write_to_buffer(rec_off
, &trace_pid
, sizeof(trace_pid
));
238 rec
->rec_off
= (idx
+ sizeof(TraceRecord
)) % TRACE_BUF_LEN
;
242 static void read_from_buffer(unsigned int idx
, void *dataptr
, size_t size
)
244 uint8_t *data_ptr
= dataptr
;
247 if (idx
>= TRACE_BUF_LEN
) {
248 idx
= idx
% TRACE_BUF_LEN
;
250 data_ptr
[x
++] = trace_buf
[idx
++];
254 static unsigned int write_to_buffer(unsigned int idx
, void *dataptr
, size_t size
)
256 uint8_t *data_ptr
= dataptr
;
259 if (idx
>= TRACE_BUF_LEN
) {
260 idx
= idx
% TRACE_BUF_LEN
;
262 trace_buf
[idx
++] = data_ptr
[x
++];
264 return idx
; /* most callers wants to know where to write next */
267 void trace_record_finish(TraceBufferRecord
*rec
)
270 read_from_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
271 smp_wmb(); /* write barrier before marking as valid */
272 record
.event
|= TRACE_RECORD_VALID
;
273 write_to_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
275 if (((unsigned int)g_atomic_int_get(&trace_idx
) - writeout_idx
)
276 > TRACE_BUF_FLUSH_THRESHOLD
) {
277 flush_trace_file(false);
281 static int st_write_event_mapping(void)
283 uint64_t type
= TRACE_RECORD_TYPE_MAPPING
;
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) {
303 void st_set_trace_file_enabled(bool enable
)
305 if (enable
== !!trace_fp
) {
306 return; /* no change */
309 /* Halt trace writeout */
310 flush_trace_file(true);
311 trace_writeout_enabled
= false;
312 flush_trace_file(true);
315 static const TraceLogHeader header
= {
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
,
322 trace_fp
= fopen(trace_file_name
, "wb");
327 if (fwrite(&header
, sizeof header
, 1, trace_fp
) != 1 ||
328 st_write_event_mapping() < 0) {
334 /* Resume trace writeout */
335 trace_writeout_enabled
= true;
336 flush_trace_file(false);
344 * Set the name of a trace file
346 * @file The trace file name or NULL for the default name-<pid> set at
349 void st_set_trace_file(const char *file
)
351 st_set_trace_file_enabled(false);
353 g_free(trace_file_name
);
356 /* Type cast needed for Windows where getpid() returns an int. */
357 trace_file_name
= g_strdup_printf(CONFIG_TRACE_FILE
, (pid_t
)getpid());
359 trace_file_name
= g_strdup_printf("%s", file
);
362 st_set_trace_file_enabled(true);
365 void st_print_trace_file_status(FILE *stream
, int (*stream_printf
)(FILE *stream
, const char *fmt
, ...))
367 stream_printf(stream
, "Trace file \"%s\" %s.\n",
368 trace_file_name
, trace_fp
? "on" : "off");
371 void st_flush_trace_buffer(void)
373 flush_trace_file(true);
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.
381 static GThread
*trace_thread_create(GThreadFunc fn
)
385 sigset_t set
, oldset
;
388 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
391 thread
= g_thread_new("trace-thread", fn
, NULL
);
394 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
404 trace_pid
= getpid();
406 thread
= trace_thread_create(writeout_thread
);
408 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
412 atexit(st_flush_trace_buffer
);