blob: 11ad030937273a925f774c6ae1170bb9271e5896 [file] [log] [blame]
Stefan Hajnoczi26f72272010-05-22 19:24:51 +01001/*
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>
Stefan Hajnoczi85aff152011-09-05 08:30:17 +010015#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000016#include <signal.h>
17#include <pthread.h>
Stefan Hajnoczi85aff152011-09-05 08:30:17 +010018#endif
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/timer.h"
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010020#include "trace.h"
Lluíse4858972011-08-31 20:31:03 +020021#include "trace/control.h"
Lluís Vilanovab618c282014-01-14 16:52:55 +010022#include "trace/simple.h"
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010023
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 */
Lluís Vilanovaef0bd3b2014-02-23 20:37:35 +010031#define HEADER_VERSION 3
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010032
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000033/** 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
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000039/*
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 */
Michael Tokarev86946a22014-05-08 12:30:46 +040043static CompatGMutex trace_lock;
44static CompatGCond trace_available_cond;
45static CompatGCond trace_empty_cond;
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +010046
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000047static bool trace_available;
48static bool trace_writeout_enabled;
49
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053050enum {
51 TRACE_BUF_LEN = 4096 * 64,
52 TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
53};
54
55uint8_t trace_buf[TRACE_BUF_LEN];
Stefan Hajnoczi30d94082013-02-12 14:34:04 +010056static volatile gint trace_idx;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053057static unsigned int writeout_idx;
Stefan Hajnoczi30d94082013-02-12 14:34:04 +010058static volatile gint dropped_events;
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +020059static uint32_t trace_pid;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010060static FILE *trace_fp;
Stefan Weil4552e412012-08-13 21:51:16 +020061static char *trace_file_name;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +010062
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053063/* * Trace buffer entry */
64typedef struct {
65 uint64_t event; /* TraceEventID */
66 uint64_t timestamp_ns;
67 uint32_t length; /* in bytes */
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +020068 uint32_t pid;
Markus Armbrusterfb3a5082013-01-25 16:43:37 +010069 uint64_t arguments[];
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053070} TraceRecord;
71
72typedef struct {
73 uint64_t header_event_id; /* HEADER_EVENT_ID */
74 uint64_t header_magic; /* HEADER_MAGIC */
75 uint64_t header_version; /* HEADER_VERSION */
Harsh Prateek Bora8ae601e2012-07-20 18:52:12 +053076} TraceLogHeader;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +053077
78
79static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
80static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
81
82static 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}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +010093/**
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +000094 * 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.
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100100 */
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530101static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
Prerna Saxena9410b562010-07-13 09:26:32 +0100102{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530103 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)) {
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000109 return false;
Prerna Saxena9410b562010-07-13 09:26:32 +0100110 }
111
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530112 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);
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100125 return true;
Prerna Saxena9410b562010-07-13 09:26:32 +0100126}
127
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000128/**
129 * Kick writeout thread
130 *
131 * @wait Whether to wait for writeout thread to complete
132 */
133static void flush_trace_file(bool wait)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100134{
Michael Tokarev86946a22014-05-08 12:30:46 +0400135 g_mutex_lock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000136 trace_available = true;
Michael Tokarev86946a22014-05-08 12:30:46 +0400137 g_cond_signal(&trace_available_cond);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000138
139 if (wait) {
Michael Tokarev86946a22014-05-08 12:30:46 +0400140 g_cond_wait(&trace_empty_cond, &trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000141 }
142
Michael Tokarev86946a22014-05-08 12:30:46 +0400143 g_mutex_unlock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000144}
145
146static void wait_for_trace_records_available(void)
147{
Michael Tokarev86946a22014-05-08 12:30:46 +0400148 g_mutex_lock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000149 while (!(trace_available && trace_writeout_enabled)) {
Michael Tokarev86946a22014-05-08 12:30:46 +0400150 g_cond_signal(&trace_empty_cond);
151 g_cond_wait(&trace_available_cond, &trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000152 }
153 trace_available = false;
Michael Tokarev86946a22014-05-08 12:30:46 +0400154 g_mutex_unlock(&trace_lock);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000155}
156
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100157static gpointer writeout_thread(gpointer opaque)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000158{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530159 TraceRecord *recordptr;
160 union {
161 TraceRecord rec;
162 uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
163 } dropped;
164 unsigned int idx = 0;
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100165 int dropped_count;
Blue Swirl0caf4482011-07-23 21:21:14 +0000166 size_t unused __attribute__ ((unused));
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000167
168 for (;;) {
169 wait_for_trace_records_available();
170
Markus Armbrustere722d702013-01-25 16:43:38 +0100171 if (g_atomic_int_get(&dropped_events)) {
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530172 dropped.rec.event = DROPPED_EVENT_ID,
173 dropped.rec.timestamp_ns = get_clock();
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100174 dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t),
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200175 dropped.rec.pid = trace_pid;
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100176 do {
Markus Armbrustere722d702013-01-25 16:43:38 +0100177 dropped_count = g_atomic_int_get(&dropped_events);
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100178 } while (!g_atomic_int_compare_and_exchange(&dropped_events,
179 dropped_count, 0));
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100180 dropped.rec.arguments[0] = dropped_count;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530181 unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100182 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000183
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530184 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;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000189 }
190
191 fflush(trace_fp);
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100192 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000193 return NULL;
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100194}
195
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530196void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100197{
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530198 rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
199}
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100200
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530201void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
202{
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);
207}
208
209int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize)
210{
211 unsigned int idx, rec_off, old_idx, new_idx;
212 uint32_t rec_len = sizeof(TraceRecord) + datasize;
Lluís Vilanova60481e22013-03-05 14:47:55 +0100213 uint64_t event_u64 = event;
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530214 uint64_t timestamp_ns = get_clock();
215
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100216 do {
Markus Armbrustere722d702013-01-25 16:43:38 +0100217 old_idx = g_atomic_int_get(&trace_idx);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530218 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 ! */
Markus Armbrusterfb3a5082013-01-25 16:43:37 +0100223 g_atomic_int_inc(&dropped_events);
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530224 return -ENOSPC;
225 }
Markus Armbrusterb6b2c962013-01-25 16:43:39 +0100226 } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
Prerna Saxena22890ab2010-06-24 17:04:53 +0530227
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530228 idx = old_idx % TRACE_BUF_LEN;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000229
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530230 rec_off = idx;
Lluís Vilanova60481e22013-03-05 14:47:55 +0100231 rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
Harsh Prateek Bora83d35d32012-07-20 18:52:13 +0530232 rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
233 rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200234 rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530235
236 rec->tbuf_idx = idx;
237 rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
238 return 0;
239}
240
241static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
242{
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 }
251}
252
253static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
254{
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 */
264}
265
266void trace_record_finish(TraceBufferRecord *rec)
267{
Harsh Prateek Boradb8894f2012-07-20 18:52:15 +0530268 TraceRecord record;
269 read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530270 smp_wmb(); /* write barrier before marking as valid */
Harsh Prateek Boradb8894f2012-07-20 18:52:15 +0530271 record.event |= TRACE_RECORD_VALID;
272 write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530273
Stefan Hajnoczi30d94082013-02-12 14:34:04 +0100274 if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
Markus Armbrustere722d702013-01-25 16:43:38 +0100275 > TRACE_BUF_FLUSH_THRESHOLD) {
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000276 flush_trace_file(false);
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100277 }
278}
279
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000280void st_set_trace_file_enabled(bool enable)
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100281{
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000282 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) {
Harsh Prateek Bora8ae601e2012-07-20 18:52:12 +0530292 static const TraceLogHeader header = {
Harsh Prateek Bora62bab732012-07-18 15:15:59 +0530293 .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,
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000297 };
298
Stefan Hajnoczi6c2a4072011-09-05 18:31:21 +0100299 trace_fp = fopen(trace_file_name, "wb");
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000300 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
319/**
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
324 */
325bool st_set_trace_file(const char *file)
326{
327 st_set_trace_file_enabled(false);
328
Stefan Weil4552e412012-08-13 21:51:16 +0200329 g_free(trace_file_name);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000330
331 if (!file) {
Stefan Weil4552e412012-08-13 21:51:16 +0200332 trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000333 } else {
Stefan Weil4552e412012-08-13 21:51:16 +0200334 trace_file_name = g_strdup_printf("%s", file);
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000335 }
336
337 st_set_trace_file_enabled(true);
338 return true;
339}
340
341void 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");
Stefan Hajnoczi26f72272010-05-22 19:24:51 +0100345}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530346
Lluísfc764102011-08-31 20:31:18 +0200347void st_flush_trace_buffer(void)
348{
349 flush_trace_file(true);
350}
351
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100352/* 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 */
357static GThread *trace_thread_create(GThreadFunc fn)
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000358{
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100359 GThread *thread;
360#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000361 sigset_t set, oldset;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000362
363 sigfillset(&set);
364 pthread_sigmask(SIG_SETMASK, &set, &oldset);
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100365#endif
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100366
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100367 thread = g_thread_new("trace-thread", fn, NULL);
Stefan Hajnoczi4a0e6712013-02-12 14:34:05 +0100368
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100369#ifndef _WIN32
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000370 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100371#endif
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000372
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100373 return thread;
374}
375
Lluís Vilanova5b808272014-05-27 15:02:14 +0200376bool st_init(const char *file)
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100377{
378 GThread *thread;
379
Stefan Hajnoczi26896cb2014-05-07 19:24:10 +0200380 trace_pid = getpid();
381
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100382 thread = trace_thread_create(writeout_thread);
383 if (!thread) {
384 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
385 return false;
386 }
387
388 atexit(st_flush_trace_buffer);
Stefan Hajnoczi85aff152011-09-05 08:30:17 +0100389 st_set_trace_file(file);
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +0000390 return true;
Prerna Saxena22890ab2010-06-24 17:04:53 +0530391}