simple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #ifndef _WIN32
  15. #include <signal.h>
  16. #include <pthread.h>
  17. #endif
  18. #include "qemu/timer.h"
  19. #include "trace.h"
  20. #include "trace/control.h"
  21. #include "trace/simple.h"
  22. /** Trace file header event ID */
  23. #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */
  24. /** Trace file magic number */
  25. #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
  26. /** Trace file version number, bump if format changes */
  27. #define HEADER_VERSION 3
  28. /** Records were dropped event ID */
  29. #define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
  30. /** Trace record is valid */
  31. #define TRACE_RECORD_VALID ((uint64_t)1 << 63)
  32. /*
  33. * Trace records are written out by a dedicated thread. The thread waits for
  34. * records to become available, writes them out, and then waits again.
  35. */
  36. static CompatGMutex trace_lock;
  37. static CompatGCond trace_available_cond;
  38. static CompatGCond trace_empty_cond;
  39. static bool trace_available;
  40. static bool trace_writeout_enabled;
  41. enum {
  42. TRACE_BUF_LEN = 4096 * 64,
  43. TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
  44. };
  45. uint8_t trace_buf[TRACE_BUF_LEN];
  46. static volatile gint trace_idx;
  47. static unsigned int writeout_idx;
  48. static volatile gint dropped_events;
  49. static uint32_t trace_pid;
  50. static FILE *trace_fp;
  51. static char *trace_file_name;
  52. /* * Trace buffer entry */
  53. typedef struct {
  54. uint64_t event; /* TraceEventID */
  55. uint64_t timestamp_ns;
  56. uint32_t length; /* in bytes */
  57. uint32_t pid;
  58. uint64_t arguments[];
  59. } TraceRecord;
  60. typedef struct {
  61. uint64_t header_event_id; /* HEADER_EVENT_ID */
  62. uint64_t header_magic; /* HEADER_MAGIC */
  63. uint64_t header_version; /* HEADER_VERSION */
  64. } TraceLogHeader;
  65. static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
  66. static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
  67. static void clear_buffer_range(unsigned int idx, size_t len)
  68. {
  69. uint32_t num = 0;
  70. while (num < len) {
  71. if (idx >= TRACE_BUF_LEN) {
  72. idx = idx % TRACE_BUF_LEN;
  73. }
  74. trace_buf[idx++] = 0;
  75. num++;
  76. }
  77. }
  78. /**
  79. * Read a trace record from the trace buffer
  80. *
  81. * @idx Trace buffer index
  82. * @record Trace record to fill
  83. *
  84. * Returns false if the record is not valid.
  85. */
  86. static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
  87. {
  88. uint64_t event_flag = 0;
  89. TraceRecord record;
  90. /* read the event flag to see if its a valid record */
  91. read_from_buffer(idx, &record, sizeof(event_flag));
  92. if (!(record.event & TRACE_RECORD_VALID)) {
  93. return false;
  94. }
  95. smp_rmb(); /* read memory barrier before accessing record */
  96. /* read the record header to know record length */
  97. read_from_buffer(idx, &record, sizeof(TraceRecord));
  98. *recordptr = malloc(record.length); /* dont use g_malloc, can deadlock when traced */
  99. /* make a copy of record to avoid being overwritten */
  100. read_from_buffer(idx, *recordptr, record.length);
  101. smp_rmb(); /* memory barrier before clearing valid flag */
  102. (*recordptr)->event &= ~TRACE_RECORD_VALID;
  103. /* clear the trace buffer range for consumed record otherwise any byte
  104. * with its MSB set may be considered as a valid event id when the writer
  105. * thread crosses this range of buffer again.
  106. */
  107. clear_buffer_range(idx, record.length);
  108. return true;
  109. }
  110. /**
  111. * Kick writeout thread
  112. *
  113. * @wait Whether to wait for writeout thread to complete
  114. */
  115. static void flush_trace_file(bool wait)
  116. {
  117. g_mutex_lock(&trace_lock);
  118. trace_available = true;
  119. g_cond_signal(&trace_available_cond);
  120. if (wait) {
  121. g_cond_wait(&trace_empty_cond, &trace_lock);
  122. }
  123. g_mutex_unlock(&trace_lock);
  124. }
  125. static void wait_for_trace_records_available(void)
  126. {
  127. g_mutex_lock(&trace_lock);
  128. while (!(trace_available && trace_writeout_enabled)) {
  129. g_cond_signal(&trace_empty_cond);
  130. g_cond_wait(&trace_available_cond, &trace_lock);
  131. }
  132. trace_available = false;
  133. g_mutex_unlock(&trace_lock);
  134. }
  135. static gpointer writeout_thread(gpointer opaque)
  136. {
  137. TraceRecord *recordptr;
  138. union {
  139. TraceRecord rec;
  140. uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
  141. } dropped;
  142. unsigned int idx = 0;
  143. int dropped_count;
  144. size_t unused __attribute__ ((unused));
  145. for (;;) {
  146. wait_for_trace_records_available();
  147. if (g_atomic_int_get(&dropped_events)) {
  148. dropped.rec.event = DROPPED_EVENT_ID,
  149. dropped.rec.timestamp_ns = get_clock();
  150. dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t),
  151. dropped.rec.pid = trace_pid;
  152. do {
  153. dropped_count = g_atomic_int_get(&dropped_events);
  154. } while (!g_atomic_int_compare_and_exchange(&dropped_events,
  155. dropped_count, 0));
  156. dropped.rec.arguments[0] = dropped_count;
  157. unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
  158. }
  159. while (get_trace_record(idx, &recordptr)) {
  160. unused = fwrite(recordptr, recordptr->length, 1, trace_fp);
  161. writeout_idx += recordptr->length;
  162. free(recordptr); /* dont use g_free, can deadlock when traced */
  163. idx = writeout_idx % TRACE_BUF_LEN;
  164. }
  165. fflush(trace_fp);
  166. }
  167. return NULL;
  168. }
  169. void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
  170. {
  171. rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
  172. }
  173. void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
  174. {
  175. /* Write string length first */
  176. rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
  177. /* Write actual string now */
  178. rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
  179. }
  180. int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize)
  181. {
  182. unsigned int idx, rec_off, old_idx, new_idx;
  183. uint32_t rec_len = sizeof(TraceRecord) + datasize;
  184. uint64_t event_u64 = event;
  185. uint64_t timestamp_ns = get_clock();
  186. do {
  187. old_idx = g_atomic_int_get(&trace_idx);
  188. smp_rmb();
  189. new_idx = old_idx + rec_len;
  190. if (new_idx - writeout_idx > TRACE_BUF_LEN) {
  191. /* Trace Buffer Full, Event dropped ! */
  192. g_atomic_int_inc(&dropped_events);
  193. return -ENOSPC;
  194. }
  195. } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
  196. idx = old_idx % TRACE_BUF_LEN;
  197. rec_off = idx;
  198. rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
  199. rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
  200. rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
  201. rec_off = write_to_buffer(rec_off, &trace_pid, sizeof(trace_pid));
  202. rec->tbuf_idx = idx;
  203. rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
  204. return 0;
  205. }
  206. static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
  207. {
  208. uint8_t *data_ptr = dataptr;
  209. uint32_t x = 0;
  210. while (x < size) {
  211. if (idx >= TRACE_BUF_LEN) {
  212. idx = idx % TRACE_BUF_LEN;
  213. }
  214. data_ptr[x++] = trace_buf[idx++];
  215. }
  216. }
  217. static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
  218. {
  219. uint8_t *data_ptr = dataptr;
  220. uint32_t x = 0;
  221. while (x < size) {
  222. if (idx >= TRACE_BUF_LEN) {
  223. idx = idx % TRACE_BUF_LEN;
  224. }
  225. trace_buf[idx++] = data_ptr[x++];
  226. }
  227. return idx; /* most callers wants to know where to write next */
  228. }
  229. void trace_record_finish(TraceBufferRecord *rec)
  230. {
  231. TraceRecord record;
  232. read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
  233. smp_wmb(); /* write barrier before marking as valid */
  234. record.event |= TRACE_RECORD_VALID;
  235. write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
  236. if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
  237. > TRACE_BUF_FLUSH_THRESHOLD) {
  238. flush_trace_file(false);
  239. }
  240. }
  241. void st_set_trace_file_enabled(bool enable)
  242. {
  243. if (enable == !!trace_fp) {
  244. return; /* no change */
  245. }
  246. /* Halt trace writeout */
  247. flush_trace_file(true);
  248. trace_writeout_enabled = false;
  249. flush_trace_file(true);
  250. if (enable) {
  251. static const TraceLogHeader header = {
  252. .header_event_id = HEADER_EVENT_ID,
  253. .header_magic = HEADER_MAGIC,
  254. /* Older log readers will check for version at next location */
  255. .header_version = HEADER_VERSION,
  256. };
  257. trace_fp = fopen(trace_file_name, "wb");
  258. if (!trace_fp) {
  259. return;
  260. }
  261. if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
  262. fclose(trace_fp);
  263. trace_fp = NULL;
  264. return;
  265. }
  266. /* Resume trace writeout */
  267. trace_writeout_enabled = true;
  268. flush_trace_file(false);
  269. } else {
  270. fclose(trace_fp);
  271. trace_fp = NULL;
  272. }
  273. }
  274. /**
  275. * Set the name of a trace file
  276. *
  277. * @file The trace file name or NULL for the default name-<pid> set at
  278. * config time
  279. */
  280. bool st_set_trace_file(const char *file)
  281. {
  282. st_set_trace_file_enabled(false);
  283. g_free(trace_file_name);
  284. if (!file) {
  285. trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
  286. } else {
  287. trace_file_name = g_strdup_printf("%s", file);
  288. }
  289. st_set_trace_file_enabled(true);
  290. return true;
  291. }
  292. void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
  293. {
  294. stream_printf(stream, "Trace file \"%s\" %s.\n",
  295. trace_file_name, trace_fp ? "on" : "off");
  296. }
  297. void st_flush_trace_buffer(void)
  298. {
  299. flush_trace_file(true);
  300. }
  301. /* Helper function to create a thread with signals blocked. Use glib's
  302. * portable threads since QEMU abstractions cannot be used due to reentrancy in
  303. * the tracer. Also note the signal masking on POSIX hosts so that the thread
  304. * does not steal signals when the rest of the program wants them blocked.
  305. */
  306. static GThread *trace_thread_create(GThreadFunc fn)
  307. {
  308. GThread *thread;
  309. #ifndef _WIN32
  310. sigset_t set, oldset;
  311. sigfillset(&set);
  312. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  313. #endif
  314. thread = g_thread_new("trace-thread", fn, NULL);
  315. #ifndef _WIN32
  316. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  317. #endif
  318. return thread;
  319. }
  320. bool st_init(const char *file)
  321. {
  322. GThread *thread;
  323. trace_pid = getpid();
  324. thread = trace_thread_create(writeout_thread);
  325. if (!thread) {
  326. fprintf(stderr, "warning: unable to initialize simple trace backend\n");
  327. return false;
  328. }
  329. atexit(st_flush_trace_buffer);
  330. st_set_trace_file(file);
  331. return true;
  332. }