1
2
3
4
5
6
7
8
9
10
11#include "qemu/osdep.h"
12#ifndef _WIN32
13#include <pthread.h>
14#endif
15#include "qemu/timer.h"
16#include "trace/control.h"
17#include "trace/simple.h"
18
19
20#define HEADER_EVENT_ID (~(uint64_t)0)
21
22
23#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
24
25
26#define HEADER_VERSION 4
27
28
29#define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
30
31
32#define TRACE_RECORD_VALID ((uint64_t)1 << 63)
33
34
35
36
37
38static CompatGMutex trace_lock;
39static CompatGCond trace_available_cond;
40static CompatGCond trace_empty_cond;
41
42static bool trace_available;
43static bool trace_writeout_enabled;
44
45enum {
46 TRACE_BUF_LEN = 4096 * 64,
47 TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
48};
49
50uint8_t trace_buf[TRACE_BUF_LEN];
51static volatile gint trace_idx;
52static unsigned int writeout_idx;
53static volatile gint dropped_events;
54static uint32_t trace_pid;
55static FILE *trace_fp;
56static char *trace_file_name;
57
58#define TRACE_RECORD_TYPE_MAPPING 0
59#define TRACE_RECORD_TYPE_EVENT 1
60
61
62typedef struct {
63 uint64_t event;
64 uint64_t timestamp_ns;
65 uint32_t length;
66 uint32_t pid;
67 uint64_t arguments[];
68} TraceRecord;
69
70typedef struct {
71 uint64_t header_event_id;
72 uint64_t header_magic;
73 uint64_t header_version;
74} TraceLogHeader;
75
76
77static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
78static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
79
80static void clear_buffer_range(unsigned int idx, size_t len)
81{
82 uint32_t num = 0;
83 while (num < len) {
84 if (idx >= TRACE_BUF_LEN) {
85 idx = idx % TRACE_BUF_LEN;
86 }
87 trace_buf[idx++] = 0;
88 num++;
89 }
90}
91
92
93
94
95
96
97
98
99static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
100{
101 uint64_t event_flag = 0;
102 TraceRecord record;
103
104 read_from_buffer(idx, &record, sizeof(event_flag));
105
106 if (!(record.event & TRACE_RECORD_VALID)) {
107 return false;
108 }
109
110 smp_rmb();
111
112 read_from_buffer(idx, &record, sizeof(TraceRecord));
113 *recordptr = malloc(record.length);
114
115 read_from_buffer(idx, *recordptr, record.length);
116 smp_rmb();
117 (*recordptr)->event &= ~TRACE_RECORD_VALID;
118
119
120
121
122 clear_buffer_range(idx, record.length);
123 return true;
124}
125
126
127
128
129
130
131static void flush_trace_file(bool wait)
132{
133 g_mutex_lock(&trace_lock);
134 trace_available = true;
135 g_cond_signal(&trace_available_cond);
136
137 if (wait) {
138 g_cond_wait(&trace_empty_cond, &trace_lock);
139 }
140
141 g_mutex_unlock(&trace_lock);
142}
143
144static void wait_for_trace_records_available(void)
145{
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);
150 }
151 trace_available = false;
152 g_mutex_unlock(&trace_lock);
153}
154
155static gpointer writeout_thread(gpointer opaque)
156{
157 TraceRecord *recordptr;
158 union {
159 TraceRecord rec;
160 uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
161 } dropped;
162 unsigned int idx = 0;
163 int dropped_count;
164 size_t unused __attribute__ ((unused));
165 uint64_t type = TRACE_RECORD_TYPE_EVENT;
166
167 for (;;) {
168 wait_for_trace_records_available();
169
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;
175 do {
176 dropped_count = g_atomic_int_get(&dropped_events);
177 } while (!g_atomic_int_compare_and_exchange(&dropped_events,
178 dropped_count, 0));
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);
182 }
183
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);
189 idx = writeout_idx % TRACE_BUF_LEN;
190 }
191
192 fflush(trace_fp);
193 }
194 return NULL;
195}
196
197void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
198{
199 rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
200}
201
202void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
203{
204
205 rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
206
207 rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
208}
209
210int trace_record_start(TraceBufferRecord *rec, uint32_t event, size_t datasize)
211{
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();
216
217 do {
218 old_idx = g_atomic_int_get(&trace_idx);
219 smp_rmb();
220 new_idx = old_idx + rec_len;
221
222 if (new_idx - writeout_idx > TRACE_BUF_LEN) {
223
224 g_atomic_int_inc(&dropped_events);
225 return -ENOSPC;
226 }
227 } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
228
229 idx = old_idx % TRACE_BUF_LEN;
230
231 rec_off = idx;
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));
236
237 rec->tbuf_idx = idx;
238 rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
239 return 0;
240}
241
242static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
243{
244 uint8_t *data_ptr = dataptr;
245 uint32_t x = 0;
246 while (x < size) {
247 if (idx >= TRACE_BUF_LEN) {
248 idx = idx % TRACE_BUF_LEN;
249 }
250 data_ptr[x++] = trace_buf[idx++];
251 }
252}
253
254static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
255{
256 uint8_t *data_ptr = dataptr;
257 uint32_t x = 0;
258 while (x < size) {
259 if (idx >= TRACE_BUF_LEN) {
260 idx = idx % TRACE_BUF_LEN;
261 }
262 trace_buf[idx++] = data_ptr[x++];
263 }
264 return idx;
265}
266
267void trace_record_finish(TraceBufferRecord *rec)
268{
269 TraceRecord record;
270 read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
271 smp_wmb();
272 record.event |= TRACE_RECORD_VALID;
273 write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
274
275 if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
276 > TRACE_BUF_FLUSH_THRESHOLD) {
277 flush_trace_file(false);
278 }
279}
280
281static int st_write_event_mapping(void)
282{
283 uint64_t type = TRACE_RECORD_TYPE_MAPPING;
284 TraceEventIter iter;
285 TraceEvent *ev;
286
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) {
296 return -1;
297 }
298 }
299
300 return 0;
301}
302
303void st_set_trace_file_enabled(bool enable)
304{
305 if (enable == !!trace_fp) {
306 return;
307 }
308
309
310 flush_trace_file(true);
311 trace_writeout_enabled = false;
312 flush_trace_file(true);
313
314 if (enable) {
315 static const TraceLogHeader header = {
316 .header_event_id = HEADER_EVENT_ID,
317 .header_magic = HEADER_MAGIC,
318
319 .header_version = HEADER_VERSION,
320 };
321
322 trace_fp = fopen(trace_file_name, "wb");
323 if (!trace_fp) {
324 return;
325 }
326
327 if (fwrite(&header, sizeof header, 1, trace_fp) != 1 ||
328 st_write_event_mapping() < 0) {
329 fclose(trace_fp);
330 trace_fp = NULL;
331 return;
332 }
333
334
335 trace_writeout_enabled = true;
336 flush_trace_file(false);
337 } else {
338 fclose(trace_fp);
339 trace_fp = NULL;
340 }
341}
342
343
344
345
346
347
348
349void st_set_trace_file(const char *file)
350{
351 st_set_trace_file_enabled(false);
352
353 g_free(trace_file_name);
354
355 if (!file) {
356
357 trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, (pid_t)getpid());
358 } else {
359 trace_file_name = g_strdup_printf("%s", file);
360 }
361
362 st_set_trace_file_enabled(true);
363}
364
365void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
366{
367 stream_printf(stream, "Trace file \"%s\" %s.\n",
368 trace_file_name, trace_fp ? "on" : "off");
369}
370
371void st_flush_trace_buffer(void)
372{
373 flush_trace_file(true);
374}
375
376
377
378
379
380
381static GThread *trace_thread_create(GThreadFunc fn)
382{
383 GThread *thread;
384#ifndef _WIN32
385 sigset_t set, oldset;
386
387 sigfillset(&set);
388 pthread_sigmask(SIG_SETMASK, &set, &oldset);
389#endif
390
391 thread = g_thread_new("trace-thread", fn, NULL);
392
393#ifndef _WIN32
394 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
395#endif
396
397 return thread;
398}
399
400bool st_init(void)
401{
402 GThread *thread;
403
404 trace_pid = getpid();
405
406 thread = trace_thread_create(writeout_thread);
407 if (!thread) {
408 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
409 return false;
410 }
411
412 atexit(st_flush_trace_buffer);
413 return true;
414}
415