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