simple.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 0
  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. /** Trace buffer entry */
  32. typedef struct {
  33. uint64_t event;
  34. uint64_t timestamp_ns;
  35. uint64_t x1;
  36. uint64_t x2;
  37. uint64_t x3;
  38. uint64_t x4;
  39. uint64_t x5;
  40. uint64_t x6;
  41. } TraceRecord;
  42. enum {
  43. TRACE_BUF_LEN = 4096,
  44. TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
  45. };
  46. /*
  47. * Trace records are written out by a dedicated thread. The thread waits for
  48. * records to become available, writes them out, and then waits again.
  49. */
  50. static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
  51. static GCond *trace_available_cond;
  52. static GCond *trace_empty_cond;
  53. static bool trace_available;
  54. static bool trace_writeout_enabled;
  55. static TraceRecord trace_buf[TRACE_BUF_LEN];
  56. static unsigned int trace_idx;
  57. static FILE *trace_fp;
  58. static char *trace_file_name = NULL;
  59. /**
  60. * Read a trace record from the trace buffer
  61. *
  62. * @idx Trace buffer index
  63. * @record Trace record to fill
  64. *
  65. * Returns false if the record is not valid.
  66. */
  67. static bool get_trace_record(unsigned int idx, TraceRecord *record)
  68. {
  69. if (!(trace_buf[idx].event & TRACE_RECORD_VALID)) {
  70. return false;
  71. }
  72. __sync_synchronize(); /* read memory barrier before accessing record */
  73. *record = trace_buf[idx];
  74. record->event &= ~TRACE_RECORD_VALID;
  75. return true;
  76. }
  77. /**
  78. * Kick writeout thread
  79. *
  80. * @wait Whether to wait for writeout thread to complete
  81. */
  82. static void flush_trace_file(bool wait)
  83. {
  84. g_static_mutex_lock(&trace_lock);
  85. trace_available = true;
  86. g_cond_signal(trace_available_cond);
  87. if (wait) {
  88. g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock));
  89. }
  90. g_static_mutex_unlock(&trace_lock);
  91. }
  92. static void wait_for_trace_records_available(void)
  93. {
  94. g_static_mutex_lock(&trace_lock);
  95. while (!(trace_available && trace_writeout_enabled)) {
  96. g_cond_signal(trace_empty_cond);
  97. g_cond_wait(trace_available_cond,
  98. g_static_mutex_get_mutex(&trace_lock));
  99. }
  100. trace_available = false;
  101. g_static_mutex_unlock(&trace_lock);
  102. }
  103. static gpointer writeout_thread(gpointer opaque)
  104. {
  105. TraceRecord record;
  106. unsigned int writeout_idx = 0;
  107. unsigned int num_available, idx;
  108. size_t unused __attribute__ ((unused));
  109. for (;;) {
  110. wait_for_trace_records_available();
  111. num_available = trace_idx - writeout_idx;
  112. if (num_available > TRACE_BUF_LEN) {
  113. record = (TraceRecord){
  114. .event = DROPPED_EVENT_ID,
  115. .x1 = num_available,
  116. };
  117. unused = fwrite(&record, sizeof(record), 1, trace_fp);
  118. writeout_idx += num_available;
  119. }
  120. idx = writeout_idx % TRACE_BUF_LEN;
  121. while (get_trace_record(idx, &record)) {
  122. trace_buf[idx].event = 0; /* clear valid bit */
  123. unused = fwrite(&record, sizeof(record), 1, trace_fp);
  124. idx = ++writeout_idx % TRACE_BUF_LEN;
  125. }
  126. fflush(trace_fp);
  127. }
  128. return NULL;
  129. }
  130. static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3,
  131. uint64_t x4, uint64_t x5, uint64_t x6)
  132. {
  133. unsigned int idx;
  134. uint64_t timestamp;
  135. if (!trace_list[event].state) {
  136. return;
  137. }
  138. timestamp = get_clock();
  139. #if GLIB_CHECK_VERSION(2, 30, 0)
  140. idx = g_atomic_int_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN;
  141. #else
  142. idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN;
  143. #endif
  144. trace_buf[idx] = (TraceRecord){
  145. .event = event,
  146. .timestamp_ns = timestamp,
  147. .x1 = x1,
  148. .x2 = x2,
  149. .x3 = x3,
  150. .x4 = x4,
  151. .x5 = x5,
  152. .x6 = x6,
  153. };
  154. __sync_synchronize(); /* write barrier before marking as valid */
  155. trace_buf[idx].event |= TRACE_RECORD_VALID;
  156. if ((idx + 1) % TRACE_BUF_FLUSH_THRESHOLD == 0) {
  157. flush_trace_file(false);
  158. }
  159. }
  160. void trace0(TraceEventID event)
  161. {
  162. trace(event, 0, 0, 0, 0, 0, 0);
  163. }
  164. void trace1(TraceEventID event, uint64_t x1)
  165. {
  166. trace(event, x1, 0, 0, 0, 0, 0);
  167. }
  168. void trace2(TraceEventID event, uint64_t x1, uint64_t x2)
  169. {
  170. trace(event, x1, x2, 0, 0, 0, 0);
  171. }
  172. void trace3(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3)
  173. {
  174. trace(event, x1, x2, x3, 0, 0, 0);
  175. }
  176. void trace4(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4)
  177. {
  178. trace(event, x1, x2, x3, x4, 0, 0);
  179. }
  180. void trace5(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5)
  181. {
  182. trace(event, x1, x2, x3, x4, x5, 0);
  183. }
  184. void trace6(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6)
  185. {
  186. trace(event, x1, x2, x3, x4, x5, x6);
  187. }
  188. void st_set_trace_file_enabled(bool enable)
  189. {
  190. if (enable == !!trace_fp) {
  191. return; /* no change */
  192. }
  193. /* Halt trace writeout */
  194. flush_trace_file(true);
  195. trace_writeout_enabled = false;
  196. flush_trace_file(true);
  197. if (enable) {
  198. static const TraceRecord header = {
  199. .event = HEADER_EVENT_ID,
  200. .timestamp_ns = HEADER_MAGIC,
  201. .x1 = HEADER_VERSION,
  202. };
  203. trace_fp = fopen(trace_file_name, "wb");
  204. if (!trace_fp) {
  205. return;
  206. }
  207. if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
  208. fclose(trace_fp);
  209. trace_fp = NULL;
  210. return;
  211. }
  212. /* Resume trace writeout */
  213. trace_writeout_enabled = true;
  214. flush_trace_file(false);
  215. } else {
  216. fclose(trace_fp);
  217. trace_fp = NULL;
  218. }
  219. }
  220. /**
  221. * Set the name of a trace file
  222. *
  223. * @file The trace file name or NULL for the default name-<pid> set at
  224. * config time
  225. */
  226. bool st_set_trace_file(const char *file)
  227. {
  228. st_set_trace_file_enabled(false);
  229. free(trace_file_name);
  230. if (!file) {
  231. if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) {
  232. trace_file_name = NULL;
  233. return false;
  234. }
  235. } else {
  236. if (asprintf(&trace_file_name, "%s", file) < 0) {
  237. trace_file_name = NULL;
  238. return false;
  239. }
  240. }
  241. st_set_trace_file_enabled(true);
  242. return true;
  243. }
  244. void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
  245. {
  246. stream_printf(stream, "Trace file \"%s\" %s.\n",
  247. trace_file_name, trace_fp ? "on" : "off");
  248. }
  249. void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
  250. {
  251. unsigned int i;
  252. for (i = 0; i < TRACE_BUF_LEN; i++) {
  253. TraceRecord record;
  254. if (!get_trace_record(i, &record)) {
  255. continue;
  256. }
  257. stream_printf(stream, "Event %" PRIu64 " : %" PRIx64 " %" PRIx64
  258. " %" PRIx64 " %" PRIx64 " %" PRIx64 " %" PRIx64 "\n",
  259. record.event, record.x1, record.x2,
  260. record.x3, record.x4, record.x5,
  261. record.x6);
  262. }
  263. }
  264. void st_flush_trace_buffer(void)
  265. {
  266. flush_trace_file(true);
  267. }
  268. void trace_print_events(FILE *stream, fprintf_function stream_printf)
  269. {
  270. unsigned int i;
  271. for (i = 0; i < NR_TRACE_EVENTS; i++) {
  272. stream_printf(stream, "%s [Event ID %u] : state %u\n",
  273. trace_list[i].tp_name, i, trace_list[i].state);
  274. }
  275. }
  276. bool trace_event_set_state(const char *name, bool state)
  277. {
  278. unsigned int i;
  279. unsigned int len;
  280. bool wildcard = false;
  281. bool matched = false;
  282. len = strlen(name);
  283. if (len > 0 && name[len - 1] == '*') {
  284. wildcard = true;
  285. len -= 1;
  286. }
  287. for (i = 0; i < NR_TRACE_EVENTS; i++) {
  288. if (wildcard) {
  289. if (!strncmp(trace_list[i].tp_name, name, len)) {
  290. trace_list[i].state = state;
  291. matched = true;
  292. }
  293. continue;
  294. }
  295. if (!strcmp(trace_list[i].tp_name, name)) {
  296. trace_list[i].state = state;
  297. return true;
  298. }
  299. }
  300. return matched;
  301. }
  302. /* Helper function to create a thread with signals blocked. Use glib's
  303. * portable threads since QEMU abstractions cannot be used due to reentrancy in
  304. * the tracer. Also note the signal masking on POSIX hosts so that the thread
  305. * does not steal signals when the rest of the program wants them blocked.
  306. */
  307. static GThread *trace_thread_create(GThreadFunc fn)
  308. {
  309. GThread *thread;
  310. #ifndef _WIN32
  311. sigset_t set, oldset;
  312. sigfillset(&set);
  313. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  314. #endif
  315. thread = g_thread_create(fn, NULL, FALSE, NULL);
  316. #ifndef _WIN32
  317. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  318. #endif
  319. return thread;
  320. }
  321. bool trace_backend_init(const char *events, const char *file)
  322. {
  323. GThread *thread;
  324. if (!g_thread_supported()) {
  325. #if !GLIB_CHECK_VERSION(2, 31, 0)
  326. g_thread_init(NULL);
  327. #else
  328. fprintf(stderr, "glib threading failed to initialize.\n");
  329. exit(1);
  330. #endif
  331. }
  332. trace_available_cond = g_cond_new();
  333. trace_empty_cond = g_cond_new();
  334. thread = trace_thread_create(writeout_thread);
  335. if (!thread) {
  336. fprintf(stderr, "warning: unable to initialize simple trace backend\n");
  337. return false;
  338. }
  339. atexit(st_flush_trace_buffer);
  340. trace_backend_init_events(events);
  341. st_set_trace_file(file);
  342. return true;
  343. }