simple.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 2
  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. #if GLIB_CHECK_VERSION(2, 32, 0)
  37. static GMutex trace_lock;
  38. #define lock_trace_lock() g_mutex_lock(&trace_lock)
  39. #define unlock_trace_lock() g_mutex_unlock(&trace_lock)
  40. #define get_trace_lock_mutex() (&trace_lock)
  41. #else
  42. static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
  43. #define lock_trace_lock() g_static_mutex_lock(&trace_lock)
  44. #define unlock_trace_lock() g_static_mutex_unlock(&trace_lock)
  45. #define get_trace_lock_mutex() g_static_mutex_get_mutex(&trace_lock)
  46. #endif
  47. /* g_cond_new() was deprecated in glib 2.31 but we still need to support it */
  48. #if GLIB_CHECK_VERSION(2, 31, 0)
  49. static GCond the_trace_available_cond;
  50. static GCond the_trace_empty_cond;
  51. static GCond *trace_available_cond = &the_trace_available_cond;
  52. static GCond *trace_empty_cond = &the_trace_empty_cond;
  53. #else
  54. static GCond *trace_available_cond;
  55. static GCond *trace_empty_cond;
  56. #endif
  57. static bool trace_available;
  58. static bool trace_writeout_enabled;
  59. enum {
  60. TRACE_BUF_LEN = 4096 * 64,
  61. TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
  62. };
  63. uint8_t trace_buf[TRACE_BUF_LEN];
  64. static volatile gint trace_idx;
  65. static unsigned int writeout_idx;
  66. static volatile gint dropped_events;
  67. static FILE *trace_fp;
  68. static char *trace_file_name;
  69. /* * Trace buffer entry */
  70. typedef struct {
  71. uint64_t event; /* TraceEventID */
  72. uint64_t timestamp_ns;
  73. uint32_t length; /* in bytes */
  74. uint32_t reserved; /* unused */
  75. uint64_t arguments[];
  76. } TraceRecord;
  77. typedef struct {
  78. uint64_t header_event_id; /* HEADER_EVENT_ID */
  79. uint64_t header_magic; /* HEADER_MAGIC */
  80. uint64_t header_version; /* HEADER_VERSION */
  81. } TraceLogHeader;
  82. static void read_from_buffer(unsigned int idx, void *dataptr, size_t size);
  83. static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size);
  84. static void clear_buffer_range(unsigned int idx, size_t len)
  85. {
  86. uint32_t num = 0;
  87. while (num < len) {
  88. if (idx >= TRACE_BUF_LEN) {
  89. idx = idx % TRACE_BUF_LEN;
  90. }
  91. trace_buf[idx++] = 0;
  92. num++;
  93. }
  94. }
  95. /**
  96. * Read a trace record from the trace buffer
  97. *
  98. * @idx Trace buffer index
  99. * @record Trace record to fill
  100. *
  101. * Returns false if the record is not valid.
  102. */
  103. static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
  104. {
  105. uint64_t event_flag = 0;
  106. TraceRecord record;
  107. /* read the event flag to see if its a valid record */
  108. read_from_buffer(idx, &record, sizeof(event_flag));
  109. if (!(record.event & TRACE_RECORD_VALID)) {
  110. return false;
  111. }
  112. 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);
  125. return true;
  126. }
  127. /**
  128. * Kick writeout thread
  129. *
  130. * @wait Whether to wait for writeout thread to complete
  131. */
  132. static void flush_trace_file(bool wait)
  133. {
  134. lock_trace_lock();
  135. trace_available = true;
  136. g_cond_signal(trace_available_cond);
  137. if (wait) {
  138. g_cond_wait(trace_empty_cond, get_trace_lock_mutex());
  139. }
  140. unlock_trace_lock();
  141. }
  142. static void wait_for_trace_records_available(void)
  143. {
  144. lock_trace_lock();
  145. while (!(trace_available && trace_writeout_enabled)) {
  146. g_cond_signal(trace_empty_cond);
  147. g_cond_wait(trace_available_cond, get_trace_lock_mutex());
  148. }
  149. trace_available = false;
  150. unlock_trace_lock();
  151. }
  152. static gpointer writeout_thread(gpointer opaque)
  153. {
  154. TraceRecord *recordptr;
  155. union {
  156. TraceRecord rec;
  157. uint8_t bytes[sizeof(TraceRecord) + sizeof(uint64_t)];
  158. } dropped;
  159. unsigned int idx = 0;
  160. int dropped_count;
  161. size_t unused __attribute__ ((unused));
  162. for (;;) {
  163. wait_for_trace_records_available();
  164. if (g_atomic_int_get(&dropped_events)) {
  165. dropped.rec.event = DROPPED_EVENT_ID,
  166. dropped.rec.timestamp_ns = get_clock();
  167. dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t),
  168. dropped.rec.reserved = 0;
  169. do {
  170. dropped_count = g_atomic_int_get(&dropped_events);
  171. } while (!g_atomic_int_compare_and_exchange(&dropped_events,
  172. dropped_count, 0));
  173. dropped.rec.arguments[0] = dropped_count;
  174. unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
  175. }
  176. while (get_trace_record(idx, &recordptr)) {
  177. unused = fwrite(recordptr, recordptr->length, 1, trace_fp);
  178. writeout_idx += recordptr->length;
  179. free(recordptr); /* dont use g_free, can deadlock when traced */
  180. idx = writeout_idx % TRACE_BUF_LEN;
  181. }
  182. fflush(trace_fp);
  183. }
  184. return NULL;
  185. }
  186. void trace_record_write_u64(TraceBufferRecord *rec, uint64_t val)
  187. {
  188. rec->rec_off = write_to_buffer(rec->rec_off, &val, sizeof(uint64_t));
  189. }
  190. void trace_record_write_str(TraceBufferRecord *rec, const char *s, uint32_t slen)
  191. {
  192. /* Write string length first */
  193. rec->rec_off = write_to_buffer(rec->rec_off, &slen, sizeof(slen));
  194. /* Write actual string now */
  195. rec->rec_off = write_to_buffer(rec->rec_off, (void*)s, slen);
  196. }
  197. int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasize)
  198. {
  199. unsigned int idx, rec_off, old_idx, new_idx;
  200. uint32_t rec_len = sizeof(TraceRecord) + datasize;
  201. uint64_t event_u64 = event;
  202. uint64_t timestamp_ns = get_clock();
  203. do {
  204. old_idx = g_atomic_int_get(&trace_idx);
  205. smp_rmb();
  206. new_idx = old_idx + rec_len;
  207. if (new_idx - writeout_idx > TRACE_BUF_LEN) {
  208. /* Trace Buffer Full, Event dropped ! */
  209. g_atomic_int_inc(&dropped_events);
  210. return -ENOSPC;
  211. }
  212. } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
  213. idx = old_idx % TRACE_BUF_LEN;
  214. rec_off = idx;
  215. rec_off = write_to_buffer(rec_off, &event_u64, sizeof(event_u64));
  216. rec_off = write_to_buffer(rec_off, &timestamp_ns, sizeof(timestamp_ns));
  217. rec_off = write_to_buffer(rec_off, &rec_len, sizeof(rec_len));
  218. rec->tbuf_idx = idx;
  219. rec->rec_off = (idx + sizeof(TraceRecord)) % TRACE_BUF_LEN;
  220. return 0;
  221. }
  222. static void read_from_buffer(unsigned int idx, void *dataptr, size_t size)
  223. {
  224. uint8_t *data_ptr = dataptr;
  225. uint32_t x = 0;
  226. while (x < size) {
  227. if (idx >= TRACE_BUF_LEN) {
  228. idx = idx % TRACE_BUF_LEN;
  229. }
  230. data_ptr[x++] = trace_buf[idx++];
  231. }
  232. }
  233. static unsigned int write_to_buffer(unsigned int idx, void *dataptr, size_t size)
  234. {
  235. uint8_t *data_ptr = dataptr;
  236. uint32_t x = 0;
  237. while (x < size) {
  238. if (idx >= TRACE_BUF_LEN) {
  239. idx = idx % TRACE_BUF_LEN;
  240. }
  241. trace_buf[idx++] = data_ptr[x++];
  242. }
  243. return idx; /* most callers wants to know where to write next */
  244. }
  245. void trace_record_finish(TraceBufferRecord *rec)
  246. {
  247. TraceRecord record;
  248. read_from_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
  249. smp_wmb(); /* write barrier before marking as valid */
  250. record.event |= TRACE_RECORD_VALID;
  251. write_to_buffer(rec->tbuf_idx, &record, sizeof(TraceRecord));
  252. if (((unsigned int)g_atomic_int_get(&trace_idx) - writeout_idx)
  253. > TRACE_BUF_FLUSH_THRESHOLD) {
  254. flush_trace_file(false);
  255. }
  256. }
  257. void st_set_trace_file_enabled(bool enable)
  258. {
  259. if (enable == !!trace_fp) {
  260. return; /* no change */
  261. }
  262. /* Halt trace writeout */
  263. flush_trace_file(true);
  264. trace_writeout_enabled = false;
  265. flush_trace_file(true);
  266. if (enable) {
  267. static const TraceLogHeader header = {
  268. .header_event_id = HEADER_EVENT_ID,
  269. .header_magic = HEADER_MAGIC,
  270. /* Older log readers will check for version at next location */
  271. .header_version = HEADER_VERSION,
  272. };
  273. trace_fp = fopen(trace_file_name, "wb");
  274. if (!trace_fp) {
  275. return;
  276. }
  277. if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
  278. fclose(trace_fp);
  279. trace_fp = NULL;
  280. return;
  281. }
  282. /* Resume trace writeout */
  283. trace_writeout_enabled = true;
  284. flush_trace_file(false);
  285. } else {
  286. fclose(trace_fp);
  287. trace_fp = NULL;
  288. }
  289. }
  290. /**
  291. * Set the name of a trace file
  292. *
  293. * @file The trace file name or NULL for the default name-<pid> set at
  294. * config time
  295. */
  296. bool st_set_trace_file(const char *file)
  297. {
  298. st_set_trace_file_enabled(false);
  299. g_free(trace_file_name);
  300. if (!file) {
  301. trace_file_name = g_strdup_printf(CONFIG_TRACE_FILE, getpid());
  302. } else {
  303. trace_file_name = g_strdup_printf("%s", file);
  304. }
  305. st_set_trace_file_enabled(true);
  306. return true;
  307. }
  308. void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
  309. {
  310. stream_printf(stream, "Trace file \"%s\" %s.\n",
  311. trace_file_name, trace_fp ? "on" : "off");
  312. }
  313. void st_flush_trace_buffer(void)
  314. {
  315. flush_trace_file(true);
  316. }
  317. void trace_print_events(FILE *stream, fprintf_function stream_printf)
  318. {
  319. unsigned int i;
  320. for (i = 0; i < trace_event_count(); i++) {
  321. TraceEvent *ev = trace_event_id(i);
  322. stream_printf(stream, "%s [Event ID %u] : state %u\n",
  323. trace_event_get_name(ev), i, trace_event_get_state_dynamic(ev));
  324. }
  325. }
  326. void trace_event_set_state_dynamic_backend(TraceEvent *ev, bool state)
  327. {
  328. ev->dstate = state;
  329. }
  330. /* Helper function to create a thread with signals blocked. Use glib's
  331. * portable threads since QEMU abstractions cannot be used due to reentrancy in
  332. * the tracer. Also note the signal masking on POSIX hosts so that the thread
  333. * does not steal signals when the rest of the program wants them blocked.
  334. */
  335. static GThread *trace_thread_create(GThreadFunc fn)
  336. {
  337. GThread *thread;
  338. #ifndef _WIN32
  339. sigset_t set, oldset;
  340. sigfillset(&set);
  341. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  342. #endif
  343. #if GLIB_CHECK_VERSION(2, 31, 0)
  344. thread = g_thread_new("trace-thread", fn, NULL);
  345. #else
  346. thread = g_thread_create(fn, NULL, FALSE, NULL);
  347. #endif
  348. #ifndef _WIN32
  349. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  350. #endif
  351. return thread;
  352. }
  353. bool trace_backend_init(const char *events, const char *file)
  354. {
  355. GThread *thread;
  356. #if !GLIB_CHECK_VERSION(2, 31, 0)
  357. trace_available_cond = g_cond_new();
  358. trace_empty_cond = g_cond_new();
  359. #endif
  360. thread = trace_thread_create(writeout_thread);
  361. if (!thread) {
  362. fprintf(stderr, "warning: unable to initialize simple trace backend\n");
  363. return false;
  364. }
  365. atexit(st_flush_trace_buffer);
  366. trace_backend_init_events(events);
  367. st_set_trace_file(file);
  368. return true;
  369. }