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