perf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Linux perf perf-<pid>.map and jit-<pid>.dump integration.
  3. *
  4. * The jitdump spec can be found at [1].
  5. *
  6. * [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/tools/perf/Documentation/jitdump-specification.txt
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. */
  10. #include "qemu/osdep.h"
  11. #include "elf.h"
  12. #include "exec/target_page.h"
  13. #include "exec/translation-block.h"
  14. #include "qemu/timer.h"
  15. #include "tcg/debuginfo.h"
  16. #include "tcg/perf.h"
  17. #include "tcg/tcg.h"
  18. static FILE *safe_fopen_w(const char *path)
  19. {
  20. int saved_errno;
  21. FILE *f;
  22. int fd;
  23. /* Delete the old file, if any. */
  24. unlink(path);
  25. /* Avoid symlink attacks by using O_CREAT | O_EXCL. */
  26. fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  27. if (fd == -1) {
  28. return NULL;
  29. }
  30. /* Convert fd to FILE*. */
  31. f = fdopen(fd, "w");
  32. if (f == NULL) {
  33. saved_errno = errno;
  34. close(fd);
  35. errno = saved_errno;
  36. return NULL;
  37. }
  38. return f;
  39. }
  40. static FILE *perfmap;
  41. void perf_enable_perfmap(void)
  42. {
  43. char map_file[32];
  44. snprintf(map_file, sizeof(map_file), "/tmp/perf-%d.map", getpid());
  45. perfmap = safe_fopen_w(map_file);
  46. if (perfmap == NULL) {
  47. warn_report("Could not open %s: %s, proceeding without perfmap",
  48. map_file, strerror(errno));
  49. }
  50. }
  51. /* Get PC and size of code JITed for guest instruction #INSN. */
  52. static void get_host_pc_size(uintptr_t *host_pc, uint16_t *host_size,
  53. const void *start, size_t insn)
  54. {
  55. uint16_t start_off = insn ? tcg_ctx->gen_insn_end_off[insn - 1] : 0;
  56. if (host_pc) {
  57. *host_pc = (uintptr_t)start + start_off;
  58. }
  59. if (host_size) {
  60. *host_size = tcg_ctx->gen_insn_end_off[insn] - start_off;
  61. }
  62. }
  63. static const char *pretty_symbol(const struct debuginfo_query *q, size_t *len)
  64. {
  65. static __thread char buf[64];
  66. int tmp;
  67. if (!q->symbol) {
  68. tmp = snprintf(buf, sizeof(buf), "guest-0x%"PRIx64, q->address);
  69. if (len) {
  70. *len = MIN(tmp + 1, sizeof(buf));
  71. }
  72. return buf;
  73. }
  74. if (!q->offset) {
  75. if (len) {
  76. *len = strlen(q->symbol) + 1;
  77. }
  78. return q->symbol;
  79. }
  80. tmp = snprintf(buf, sizeof(buf), "%s+0x%"PRIx64, q->symbol, q->offset);
  81. if (len) {
  82. *len = MIN(tmp + 1, sizeof(buf));
  83. }
  84. return buf;
  85. }
  86. static void write_perfmap_entry(const void *start, size_t insn,
  87. const struct debuginfo_query *q)
  88. {
  89. uint16_t host_size;
  90. uintptr_t host_pc;
  91. get_host_pc_size(&host_pc, &host_size, start, insn);
  92. fprintf(perfmap, "%"PRIxPTR" %"PRIx16" %s\n",
  93. host_pc, host_size, pretty_symbol(q, NULL));
  94. }
  95. static FILE *jitdump;
  96. static size_t perf_marker_size;
  97. static void *perf_marker = MAP_FAILED;
  98. #define JITHEADER_MAGIC 0x4A695444
  99. #define JITHEADER_VERSION 1
  100. struct jitheader {
  101. uint32_t magic;
  102. uint32_t version;
  103. uint32_t total_size;
  104. uint32_t elf_mach;
  105. uint32_t pad1;
  106. uint32_t pid;
  107. uint64_t timestamp;
  108. uint64_t flags;
  109. };
  110. enum jit_record_type {
  111. JIT_CODE_LOAD = 0,
  112. JIT_CODE_DEBUG_INFO = 2,
  113. };
  114. struct jr_prefix {
  115. uint32_t id;
  116. uint32_t total_size;
  117. uint64_t timestamp;
  118. };
  119. struct jr_code_load {
  120. struct jr_prefix p;
  121. uint32_t pid;
  122. uint32_t tid;
  123. uint64_t vma;
  124. uint64_t code_addr;
  125. uint64_t code_size;
  126. uint64_t code_index;
  127. };
  128. struct debug_entry {
  129. uint64_t addr;
  130. int lineno;
  131. int discrim;
  132. const char name[];
  133. };
  134. struct jr_code_debug_info {
  135. struct jr_prefix p;
  136. uint64_t code_addr;
  137. uint64_t nr_entry;
  138. struct debug_entry entries[];
  139. };
  140. static uint32_t get_e_machine(void)
  141. {
  142. Elf64_Ehdr elf_header;
  143. FILE *exe;
  144. size_t n;
  145. QEMU_BUILD_BUG_ON(offsetof(Elf32_Ehdr, e_machine) !=
  146. offsetof(Elf64_Ehdr, e_machine));
  147. exe = fopen("/proc/self/exe", "r");
  148. if (exe == NULL) {
  149. return EM_NONE;
  150. }
  151. n = fread(&elf_header, sizeof(elf_header), 1, exe);
  152. fclose(exe);
  153. if (n != 1) {
  154. return EM_NONE;
  155. }
  156. return elf_header.e_machine;
  157. }
  158. void perf_enable_jitdump(void)
  159. {
  160. struct jitheader header;
  161. char jitdump_file[32];
  162. if (!use_rt_clock) {
  163. warn_report("CLOCK_MONOTONIC is not available, proceeding without jitdump");
  164. return;
  165. }
  166. snprintf(jitdump_file, sizeof(jitdump_file), "jit-%d.dump", getpid());
  167. jitdump = safe_fopen_w(jitdump_file);
  168. if (jitdump == NULL) {
  169. warn_report("Could not open %s: %s, proceeding without jitdump",
  170. jitdump_file, strerror(errno));
  171. return;
  172. }
  173. /*
  174. * `perf inject` will see that the mapped file name in the corresponding
  175. * PERF_RECORD_MMAP or PERF_RECORD_MMAP2 event is of the form jit-%d.dump
  176. * and will process it as a jitdump file.
  177. */
  178. perf_marker_size = qemu_real_host_page_size();
  179. perf_marker = mmap(NULL, perf_marker_size, PROT_READ | PROT_EXEC,
  180. MAP_PRIVATE, fileno(jitdump), 0);
  181. if (perf_marker == MAP_FAILED) {
  182. warn_report("Could not map %s: %s, proceeding without jitdump",
  183. jitdump_file, strerror(errno));
  184. fclose(jitdump);
  185. jitdump = NULL;
  186. return;
  187. }
  188. header.magic = JITHEADER_MAGIC;
  189. header.version = JITHEADER_VERSION;
  190. header.total_size = sizeof(header);
  191. header.elf_mach = get_e_machine();
  192. header.pad1 = 0;
  193. header.pid = getpid();
  194. header.timestamp = get_clock();
  195. header.flags = 0;
  196. fwrite(&header, sizeof(header), 1, jitdump);
  197. }
  198. void perf_report_prologue(const void *start, size_t size)
  199. {
  200. if (perfmap) {
  201. fprintf(perfmap, "%"PRIxPTR" %zx tcg-prologue-buffer\n",
  202. (uintptr_t)start, size);
  203. }
  204. }
  205. /* Write a JIT_CODE_DEBUG_INFO jitdump entry. */
  206. static void write_jr_code_debug_info(const void *start,
  207. const struct debuginfo_query *q,
  208. size_t icount)
  209. {
  210. struct jr_code_debug_info rec;
  211. struct debug_entry ent;
  212. uintptr_t host_pc;
  213. int insn;
  214. /* Write the header. */
  215. rec.p.id = JIT_CODE_DEBUG_INFO;
  216. rec.p.total_size = sizeof(rec) + sizeof(ent) + 1;
  217. rec.p.timestamp = get_clock();
  218. rec.code_addr = (uintptr_t)start;
  219. rec.nr_entry = 1;
  220. for (insn = 0; insn < icount; insn++) {
  221. if (q[insn].file) {
  222. rec.p.total_size += sizeof(ent) + strlen(q[insn].file) + 1;
  223. rec.nr_entry++;
  224. }
  225. }
  226. fwrite(&rec, sizeof(rec), 1, jitdump);
  227. /* Write the main debug entries. */
  228. for (insn = 0; insn < icount; insn++) {
  229. if (q[insn].file) {
  230. get_host_pc_size(&host_pc, NULL, start, insn);
  231. ent.addr = host_pc;
  232. ent.lineno = q[insn].line;
  233. ent.discrim = 0;
  234. fwrite(&ent, sizeof(ent), 1, jitdump);
  235. fwrite(q[insn].file, strlen(q[insn].file) + 1, 1, jitdump);
  236. }
  237. }
  238. /* Write the trailing debug_entry. */
  239. ent.addr = (uintptr_t)start + tcg_ctx->gen_insn_end_off[icount - 1];
  240. ent.lineno = 0;
  241. ent.discrim = 0;
  242. fwrite(&ent, sizeof(ent), 1, jitdump);
  243. fwrite("", 1, 1, jitdump);
  244. }
  245. /* Write a JIT_CODE_LOAD jitdump entry. */
  246. static void write_jr_code_load(const void *start, uint16_t host_size,
  247. const struct debuginfo_query *q)
  248. {
  249. static uint64_t code_index;
  250. struct jr_code_load rec;
  251. const char *symbol;
  252. size_t symbol_size;
  253. symbol = pretty_symbol(q, &symbol_size);
  254. rec.p.id = JIT_CODE_LOAD;
  255. rec.p.total_size = sizeof(rec) + symbol_size + host_size;
  256. rec.p.timestamp = get_clock();
  257. rec.pid = getpid();
  258. rec.tid = qemu_get_thread_id();
  259. rec.vma = (uintptr_t)start;
  260. rec.code_addr = (uintptr_t)start;
  261. rec.code_size = host_size;
  262. rec.code_index = code_index++;
  263. fwrite(&rec, sizeof(rec), 1, jitdump);
  264. fwrite(symbol, symbol_size, 1, jitdump);
  265. fwrite(start, host_size, 1, jitdump);
  266. }
  267. void perf_report_code(uint64_t guest_pc, TranslationBlock *tb,
  268. const void *start)
  269. {
  270. struct debuginfo_query *q;
  271. size_t insn, start_words;
  272. uint64_t *gen_insn_data;
  273. if (!perfmap && !jitdump) {
  274. return;
  275. }
  276. q = g_try_malloc0_n(tb->icount, sizeof(*q));
  277. if (!q) {
  278. return;
  279. }
  280. debuginfo_lock();
  281. /* Query debuginfo for each guest instruction. */
  282. gen_insn_data = tcg_ctx->gen_insn_data;
  283. start_words = tcg_ctx->insn_start_words;
  284. for (insn = 0; insn < tb->icount; insn++) {
  285. /* FIXME: This replicates the restore_state_to_opc() logic. */
  286. q[insn].address = gen_insn_data[insn * start_words + 0];
  287. if (tb_cflags(tb) & CF_PCREL) {
  288. q[insn].address |= (guest_pc & qemu_target_page_mask());
  289. }
  290. q[insn].flags = DEBUGINFO_SYMBOL | (jitdump ? DEBUGINFO_LINE : 0);
  291. }
  292. debuginfo_query(q, tb->icount);
  293. /* Emit perfmap entries if needed. */
  294. if (perfmap) {
  295. flockfile(perfmap);
  296. for (insn = 0; insn < tb->icount; insn++) {
  297. write_perfmap_entry(start, insn, &q[insn]);
  298. }
  299. funlockfile(perfmap);
  300. }
  301. /* Emit jitdump entries if needed. */
  302. if (jitdump) {
  303. flockfile(jitdump);
  304. write_jr_code_debug_info(start, q, tb->icount);
  305. write_jr_code_load(start, tcg_ctx->gen_insn_end_off[tb->icount - 1],
  306. q);
  307. funlockfile(jitdump);
  308. }
  309. debuginfo_unlock();
  310. g_free(q);
  311. }
  312. void perf_exit(void)
  313. {
  314. if (perfmap) {
  315. fclose(perfmap);
  316. perfmap = NULL;
  317. }
  318. if (perf_marker != MAP_FAILED) {
  319. munmap(perf_marker, perf_marker_size);
  320. perf_marker = MAP_FAILED;
  321. }
  322. if (jitdump) {
  323. fclose(jitdump);
  324. jitdump = NULL;
  325. }
  326. }