simple.c 12 KB

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