15584e2dbSIlya Leoshkevich /*
25584e2dbSIlya Leoshkevich * Linux perf perf-<pid>.map and jit-<pid>.dump integration.
35584e2dbSIlya Leoshkevich *
45584e2dbSIlya Leoshkevich * The jitdump spec can be found at [1].
55584e2dbSIlya Leoshkevich *
65584e2dbSIlya Leoshkevich * [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/tools/perf/Documentation/jitdump-specification.txt
75584e2dbSIlya Leoshkevich *
85584e2dbSIlya Leoshkevich * SPDX-License-Identifier: GPL-2.0-or-later
95584e2dbSIlya Leoshkevich */
105584e2dbSIlya Leoshkevich
115584e2dbSIlya Leoshkevich #include "qemu/osdep.h"
125584e2dbSIlya Leoshkevich #include "elf.h"
138a6a9ab6SIlya Leoshkevich #include "exec/target_page.h"
14327b75a4SIlya Leoshkevich #include "exec/translation-block.h"
155584e2dbSIlya Leoshkevich #include "qemu/timer.h"
16327b75a4SIlya Leoshkevich #include "tcg/debuginfo.h"
17327b75a4SIlya Leoshkevich #include "tcg/perf.h"
185584e2dbSIlya Leoshkevich #include "tcg/tcg.h"
195584e2dbSIlya Leoshkevich
safe_fopen_w(const char * path)205584e2dbSIlya Leoshkevich static FILE *safe_fopen_w(const char *path)
215584e2dbSIlya Leoshkevich {
225584e2dbSIlya Leoshkevich int saved_errno;
235584e2dbSIlya Leoshkevich FILE *f;
245584e2dbSIlya Leoshkevich int fd;
255584e2dbSIlya Leoshkevich
265584e2dbSIlya Leoshkevich /* Delete the old file, if any. */
275584e2dbSIlya Leoshkevich unlink(path);
285584e2dbSIlya Leoshkevich
295584e2dbSIlya Leoshkevich /* Avoid symlink attacks by using O_CREAT | O_EXCL. */
305584e2dbSIlya Leoshkevich fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
315584e2dbSIlya Leoshkevich if (fd == -1) {
325584e2dbSIlya Leoshkevich return NULL;
335584e2dbSIlya Leoshkevich }
345584e2dbSIlya Leoshkevich
355584e2dbSIlya Leoshkevich /* Convert fd to FILE*. */
365584e2dbSIlya Leoshkevich f = fdopen(fd, "w");
375584e2dbSIlya Leoshkevich if (f == NULL) {
385584e2dbSIlya Leoshkevich saved_errno = errno;
395584e2dbSIlya Leoshkevich close(fd);
405584e2dbSIlya Leoshkevich errno = saved_errno;
415584e2dbSIlya Leoshkevich return NULL;
425584e2dbSIlya Leoshkevich }
435584e2dbSIlya Leoshkevich
445584e2dbSIlya Leoshkevich return f;
455584e2dbSIlya Leoshkevich }
465584e2dbSIlya Leoshkevich
475584e2dbSIlya Leoshkevich static FILE *perfmap;
485584e2dbSIlya Leoshkevich
perf_enable_perfmap(void)495584e2dbSIlya Leoshkevich void perf_enable_perfmap(void)
505584e2dbSIlya Leoshkevich {
515584e2dbSIlya Leoshkevich char map_file[32];
525584e2dbSIlya Leoshkevich
535584e2dbSIlya Leoshkevich snprintf(map_file, sizeof(map_file), "/tmp/perf-%d.map", getpid());
545584e2dbSIlya Leoshkevich perfmap = safe_fopen_w(map_file);
555584e2dbSIlya Leoshkevich if (perfmap == NULL) {
565584e2dbSIlya Leoshkevich warn_report("Could not open %s: %s, proceeding without perfmap",
575584e2dbSIlya Leoshkevich map_file, strerror(errno));
585584e2dbSIlya Leoshkevich }
595584e2dbSIlya Leoshkevich }
605584e2dbSIlya Leoshkevich
615584e2dbSIlya Leoshkevich /* Get PC and size of code JITed for guest instruction #INSN. */
get_host_pc_size(uintptr_t * host_pc,uint16_t * host_size,const void * start,size_t insn)625584e2dbSIlya Leoshkevich static void get_host_pc_size(uintptr_t *host_pc, uint16_t *host_size,
635584e2dbSIlya Leoshkevich const void *start, size_t insn)
645584e2dbSIlya Leoshkevich {
655584e2dbSIlya Leoshkevich uint16_t start_off = insn ? tcg_ctx->gen_insn_end_off[insn - 1] : 0;
665584e2dbSIlya Leoshkevich
675584e2dbSIlya Leoshkevich if (host_pc) {
685584e2dbSIlya Leoshkevich *host_pc = (uintptr_t)start + start_off;
695584e2dbSIlya Leoshkevich }
705584e2dbSIlya Leoshkevich if (host_size) {
715584e2dbSIlya Leoshkevich *host_size = tcg_ctx->gen_insn_end_off[insn] - start_off;
725584e2dbSIlya Leoshkevich }
735584e2dbSIlya Leoshkevich }
745584e2dbSIlya Leoshkevich
pretty_symbol(const struct debuginfo_query * q,size_t * len)755584e2dbSIlya Leoshkevich static const char *pretty_symbol(const struct debuginfo_query *q, size_t *len)
765584e2dbSIlya Leoshkevich {
775584e2dbSIlya Leoshkevich static __thread char buf[64];
785584e2dbSIlya Leoshkevich int tmp;
795584e2dbSIlya Leoshkevich
805584e2dbSIlya Leoshkevich if (!q->symbol) {
815584e2dbSIlya Leoshkevich tmp = snprintf(buf, sizeof(buf), "guest-0x%"PRIx64, q->address);
825584e2dbSIlya Leoshkevich if (len) {
835584e2dbSIlya Leoshkevich *len = MIN(tmp + 1, sizeof(buf));
845584e2dbSIlya Leoshkevich }
855584e2dbSIlya Leoshkevich return buf;
865584e2dbSIlya Leoshkevich }
875584e2dbSIlya Leoshkevich
885584e2dbSIlya Leoshkevich if (!q->offset) {
895584e2dbSIlya Leoshkevich if (len) {
905584e2dbSIlya Leoshkevich *len = strlen(q->symbol) + 1;
915584e2dbSIlya Leoshkevich }
925584e2dbSIlya Leoshkevich return q->symbol;
935584e2dbSIlya Leoshkevich }
945584e2dbSIlya Leoshkevich
955584e2dbSIlya Leoshkevich tmp = snprintf(buf, sizeof(buf), "%s+0x%"PRIx64, q->symbol, q->offset);
965584e2dbSIlya Leoshkevich if (len) {
975584e2dbSIlya Leoshkevich *len = MIN(tmp + 1, sizeof(buf));
985584e2dbSIlya Leoshkevich }
995584e2dbSIlya Leoshkevich return buf;
1005584e2dbSIlya Leoshkevich }
1015584e2dbSIlya Leoshkevich
write_perfmap_entry(const void * start,size_t insn,const struct debuginfo_query * q)1025584e2dbSIlya Leoshkevich static void write_perfmap_entry(const void *start, size_t insn,
1035584e2dbSIlya Leoshkevich const struct debuginfo_query *q)
1045584e2dbSIlya Leoshkevich {
1055584e2dbSIlya Leoshkevich uint16_t host_size;
1065584e2dbSIlya Leoshkevich uintptr_t host_pc;
1075584e2dbSIlya Leoshkevich
1085584e2dbSIlya Leoshkevich get_host_pc_size(&host_pc, &host_size, start, insn);
1095584e2dbSIlya Leoshkevich fprintf(perfmap, "%"PRIxPTR" %"PRIx16" %s\n",
1105584e2dbSIlya Leoshkevich host_pc, host_size, pretty_symbol(q, NULL));
1115584e2dbSIlya Leoshkevich }
1125584e2dbSIlya Leoshkevich
1135584e2dbSIlya Leoshkevich static FILE *jitdump;
114e7cd7a39SIlya Leoshkevich static size_t perf_marker_size;
115e7cd7a39SIlya Leoshkevich static void *perf_marker = MAP_FAILED;
1165584e2dbSIlya Leoshkevich
1175584e2dbSIlya Leoshkevich #define JITHEADER_MAGIC 0x4A695444
1185584e2dbSIlya Leoshkevich #define JITHEADER_VERSION 1
1195584e2dbSIlya Leoshkevich
1205584e2dbSIlya Leoshkevich struct jitheader {
1215584e2dbSIlya Leoshkevich uint32_t magic;
1225584e2dbSIlya Leoshkevich uint32_t version;
1235584e2dbSIlya Leoshkevich uint32_t total_size;
1245584e2dbSIlya Leoshkevich uint32_t elf_mach;
1255584e2dbSIlya Leoshkevich uint32_t pad1;
1265584e2dbSIlya Leoshkevich uint32_t pid;
1275584e2dbSIlya Leoshkevich uint64_t timestamp;
1285584e2dbSIlya Leoshkevich uint64_t flags;
1295584e2dbSIlya Leoshkevich };
1305584e2dbSIlya Leoshkevich
1315584e2dbSIlya Leoshkevich enum jit_record_type {
1325584e2dbSIlya Leoshkevich JIT_CODE_LOAD = 0,
1335584e2dbSIlya Leoshkevich JIT_CODE_DEBUG_INFO = 2,
1345584e2dbSIlya Leoshkevich };
1355584e2dbSIlya Leoshkevich
1365584e2dbSIlya Leoshkevich struct jr_prefix {
1375584e2dbSIlya Leoshkevich uint32_t id;
1385584e2dbSIlya Leoshkevich uint32_t total_size;
1395584e2dbSIlya Leoshkevich uint64_t timestamp;
1405584e2dbSIlya Leoshkevich };
1415584e2dbSIlya Leoshkevich
1425584e2dbSIlya Leoshkevich struct jr_code_load {
1435584e2dbSIlya Leoshkevich struct jr_prefix p;
1445584e2dbSIlya Leoshkevich
1455584e2dbSIlya Leoshkevich uint32_t pid;
1465584e2dbSIlya Leoshkevich uint32_t tid;
1475584e2dbSIlya Leoshkevich uint64_t vma;
1485584e2dbSIlya Leoshkevich uint64_t code_addr;
1495584e2dbSIlya Leoshkevich uint64_t code_size;
1505584e2dbSIlya Leoshkevich uint64_t code_index;
1515584e2dbSIlya Leoshkevich };
1525584e2dbSIlya Leoshkevich
1535584e2dbSIlya Leoshkevich struct debug_entry {
1545584e2dbSIlya Leoshkevich uint64_t addr;
1555584e2dbSIlya Leoshkevich int lineno;
1565584e2dbSIlya Leoshkevich int discrim;
1575584e2dbSIlya Leoshkevich const char name[];
1585584e2dbSIlya Leoshkevich };
1595584e2dbSIlya Leoshkevich
1605584e2dbSIlya Leoshkevich struct jr_code_debug_info {
1615584e2dbSIlya Leoshkevich struct jr_prefix p;
1625584e2dbSIlya Leoshkevich
1635584e2dbSIlya Leoshkevich uint64_t code_addr;
1645584e2dbSIlya Leoshkevich uint64_t nr_entry;
1655584e2dbSIlya Leoshkevich struct debug_entry entries[];
1665584e2dbSIlya Leoshkevich };
1675584e2dbSIlya Leoshkevich
get_e_machine(void)1685584e2dbSIlya Leoshkevich static uint32_t get_e_machine(void)
1695584e2dbSIlya Leoshkevich {
1705584e2dbSIlya Leoshkevich Elf64_Ehdr elf_header;
1715584e2dbSIlya Leoshkevich FILE *exe;
1725584e2dbSIlya Leoshkevich size_t n;
1735584e2dbSIlya Leoshkevich
1745584e2dbSIlya Leoshkevich QEMU_BUILD_BUG_ON(offsetof(Elf32_Ehdr, e_machine) !=
1755584e2dbSIlya Leoshkevich offsetof(Elf64_Ehdr, e_machine));
1765584e2dbSIlya Leoshkevich
1775584e2dbSIlya Leoshkevich exe = fopen("/proc/self/exe", "r");
1785584e2dbSIlya Leoshkevich if (exe == NULL) {
1795584e2dbSIlya Leoshkevich return EM_NONE;
1805584e2dbSIlya Leoshkevich }
1815584e2dbSIlya Leoshkevich
1825584e2dbSIlya Leoshkevich n = fread(&elf_header, sizeof(elf_header), 1, exe);
1835584e2dbSIlya Leoshkevich fclose(exe);
1845584e2dbSIlya Leoshkevich if (n != 1) {
1855584e2dbSIlya Leoshkevich return EM_NONE;
1865584e2dbSIlya Leoshkevich }
1875584e2dbSIlya Leoshkevich
1885584e2dbSIlya Leoshkevich return elf_header.e_machine;
1895584e2dbSIlya Leoshkevich }
1905584e2dbSIlya Leoshkevich
perf_enable_jitdump(void)1915584e2dbSIlya Leoshkevich void perf_enable_jitdump(void)
1925584e2dbSIlya Leoshkevich {
1935584e2dbSIlya Leoshkevich struct jitheader header;
1945584e2dbSIlya Leoshkevich char jitdump_file[32];
1955584e2dbSIlya Leoshkevich
1965584e2dbSIlya Leoshkevich if (!use_rt_clock) {
1975584e2dbSIlya Leoshkevich warn_report("CLOCK_MONOTONIC is not available, proceeding without jitdump");
1985584e2dbSIlya Leoshkevich return;
1995584e2dbSIlya Leoshkevich }
2005584e2dbSIlya Leoshkevich
2015584e2dbSIlya Leoshkevich snprintf(jitdump_file, sizeof(jitdump_file), "jit-%d.dump", getpid());
2025584e2dbSIlya Leoshkevich jitdump = safe_fopen_w(jitdump_file);
2035584e2dbSIlya Leoshkevich if (jitdump == NULL) {
2045584e2dbSIlya Leoshkevich warn_report("Could not open %s: %s, proceeding without jitdump",
2055584e2dbSIlya Leoshkevich jitdump_file, strerror(errno));
2065584e2dbSIlya Leoshkevich return;
2075584e2dbSIlya Leoshkevich }
2085584e2dbSIlya Leoshkevich
2095584e2dbSIlya Leoshkevich /*
2105584e2dbSIlya Leoshkevich * `perf inject` will see that the mapped file name in the corresponding
2115584e2dbSIlya Leoshkevich * PERF_RECORD_MMAP or PERF_RECORD_MMAP2 event is of the form jit-%d.dump
2125584e2dbSIlya Leoshkevich * and will process it as a jitdump file.
2135584e2dbSIlya Leoshkevich */
214e7cd7a39SIlya Leoshkevich perf_marker_size = qemu_real_host_page_size();
215e7cd7a39SIlya Leoshkevich perf_marker = mmap(NULL, perf_marker_size, PROT_READ | PROT_EXEC,
2165584e2dbSIlya Leoshkevich MAP_PRIVATE, fileno(jitdump), 0);
2175584e2dbSIlya Leoshkevich if (perf_marker == MAP_FAILED) {
2185584e2dbSIlya Leoshkevich warn_report("Could not map %s: %s, proceeding without jitdump",
2195584e2dbSIlya Leoshkevich jitdump_file, strerror(errno));
2205584e2dbSIlya Leoshkevich fclose(jitdump);
2215584e2dbSIlya Leoshkevich jitdump = NULL;
2225584e2dbSIlya Leoshkevich return;
2235584e2dbSIlya Leoshkevich }
2245584e2dbSIlya Leoshkevich
2255584e2dbSIlya Leoshkevich header.magic = JITHEADER_MAGIC;
2265584e2dbSIlya Leoshkevich header.version = JITHEADER_VERSION;
2275584e2dbSIlya Leoshkevich header.total_size = sizeof(header);
2285584e2dbSIlya Leoshkevich header.elf_mach = get_e_machine();
2295584e2dbSIlya Leoshkevich header.pad1 = 0;
2305584e2dbSIlya Leoshkevich header.pid = getpid();
2315584e2dbSIlya Leoshkevich header.timestamp = get_clock();
2325584e2dbSIlya Leoshkevich header.flags = 0;
2335584e2dbSIlya Leoshkevich fwrite(&header, sizeof(header), 1, jitdump);
2345584e2dbSIlya Leoshkevich }
2355584e2dbSIlya Leoshkevich
perf_report_prologue(const void * start,size_t size)2365584e2dbSIlya Leoshkevich void perf_report_prologue(const void *start, size_t size)
2375584e2dbSIlya Leoshkevich {
2385584e2dbSIlya Leoshkevich if (perfmap) {
2395584e2dbSIlya Leoshkevich fprintf(perfmap, "%"PRIxPTR" %zx tcg-prologue-buffer\n",
2405584e2dbSIlya Leoshkevich (uintptr_t)start, size);
2415584e2dbSIlya Leoshkevich }
2425584e2dbSIlya Leoshkevich }
2435584e2dbSIlya Leoshkevich
2445584e2dbSIlya Leoshkevich /* Write a JIT_CODE_DEBUG_INFO jitdump entry. */
write_jr_code_debug_info(const void * start,const struct debuginfo_query * q,size_t icount)2455584e2dbSIlya Leoshkevich static void write_jr_code_debug_info(const void *start,
2465584e2dbSIlya Leoshkevich const struct debuginfo_query *q,
2475584e2dbSIlya Leoshkevich size_t icount)
2485584e2dbSIlya Leoshkevich {
2495584e2dbSIlya Leoshkevich struct jr_code_debug_info rec;
2505584e2dbSIlya Leoshkevich struct debug_entry ent;
2515584e2dbSIlya Leoshkevich uintptr_t host_pc;
2525584e2dbSIlya Leoshkevich int insn;
2535584e2dbSIlya Leoshkevich
2545584e2dbSIlya Leoshkevich /* Write the header. */
2555584e2dbSIlya Leoshkevich rec.p.id = JIT_CODE_DEBUG_INFO;
2565584e2dbSIlya Leoshkevich rec.p.total_size = sizeof(rec) + sizeof(ent) + 1;
2575584e2dbSIlya Leoshkevich rec.p.timestamp = get_clock();
2585584e2dbSIlya Leoshkevich rec.code_addr = (uintptr_t)start;
2595584e2dbSIlya Leoshkevich rec.nr_entry = 1;
2605584e2dbSIlya Leoshkevich for (insn = 0; insn < icount; insn++) {
2615584e2dbSIlya Leoshkevich if (q[insn].file) {
2625584e2dbSIlya Leoshkevich rec.p.total_size += sizeof(ent) + strlen(q[insn].file) + 1;
2635584e2dbSIlya Leoshkevich rec.nr_entry++;
2645584e2dbSIlya Leoshkevich }
2655584e2dbSIlya Leoshkevich }
2665584e2dbSIlya Leoshkevich fwrite(&rec, sizeof(rec), 1, jitdump);
2675584e2dbSIlya Leoshkevich
2685584e2dbSIlya Leoshkevich /* Write the main debug entries. */
2695584e2dbSIlya Leoshkevich for (insn = 0; insn < icount; insn++) {
2705584e2dbSIlya Leoshkevich if (q[insn].file) {
2715584e2dbSIlya Leoshkevich get_host_pc_size(&host_pc, NULL, start, insn);
2725584e2dbSIlya Leoshkevich ent.addr = host_pc;
2735584e2dbSIlya Leoshkevich ent.lineno = q[insn].line;
2745584e2dbSIlya Leoshkevich ent.discrim = 0;
2755584e2dbSIlya Leoshkevich fwrite(&ent, sizeof(ent), 1, jitdump);
2765584e2dbSIlya Leoshkevich fwrite(q[insn].file, strlen(q[insn].file) + 1, 1, jitdump);
2775584e2dbSIlya Leoshkevich }
2785584e2dbSIlya Leoshkevich }
2795584e2dbSIlya Leoshkevich
2805584e2dbSIlya Leoshkevich /* Write the trailing debug_entry. */
2815584e2dbSIlya Leoshkevich ent.addr = (uintptr_t)start + tcg_ctx->gen_insn_end_off[icount - 1];
2825584e2dbSIlya Leoshkevich ent.lineno = 0;
2835584e2dbSIlya Leoshkevich ent.discrim = 0;
2845584e2dbSIlya Leoshkevich fwrite(&ent, sizeof(ent), 1, jitdump);
2855584e2dbSIlya Leoshkevich fwrite("", 1, 1, jitdump);
2865584e2dbSIlya Leoshkevich }
2875584e2dbSIlya Leoshkevich
2885584e2dbSIlya Leoshkevich /* Write a JIT_CODE_LOAD jitdump entry. */
write_jr_code_load(const void * start,uint16_t host_size,const struct debuginfo_query * q)2895584e2dbSIlya Leoshkevich static void write_jr_code_load(const void *start, uint16_t host_size,
2905584e2dbSIlya Leoshkevich const struct debuginfo_query *q)
2915584e2dbSIlya Leoshkevich {
2925584e2dbSIlya Leoshkevich static uint64_t code_index;
2935584e2dbSIlya Leoshkevich struct jr_code_load rec;
2945584e2dbSIlya Leoshkevich const char *symbol;
2955584e2dbSIlya Leoshkevich size_t symbol_size;
2965584e2dbSIlya Leoshkevich
2975584e2dbSIlya Leoshkevich symbol = pretty_symbol(q, &symbol_size);
2985584e2dbSIlya Leoshkevich rec.p.id = JIT_CODE_LOAD;
2995584e2dbSIlya Leoshkevich rec.p.total_size = sizeof(rec) + symbol_size + host_size;
3005584e2dbSIlya Leoshkevich rec.p.timestamp = get_clock();
3015584e2dbSIlya Leoshkevich rec.pid = getpid();
3025584e2dbSIlya Leoshkevich rec.tid = qemu_get_thread_id();
3035584e2dbSIlya Leoshkevich rec.vma = (uintptr_t)start;
3045584e2dbSIlya Leoshkevich rec.code_addr = (uintptr_t)start;
3055584e2dbSIlya Leoshkevich rec.code_size = host_size;
3065584e2dbSIlya Leoshkevich rec.code_index = code_index++;
3075584e2dbSIlya Leoshkevich fwrite(&rec, sizeof(rec), 1, jitdump);
3085584e2dbSIlya Leoshkevich fwrite(symbol, symbol_size, 1, jitdump);
3095584e2dbSIlya Leoshkevich fwrite(start, host_size, 1, jitdump);
3105584e2dbSIlya Leoshkevich }
3115584e2dbSIlya Leoshkevich
perf_report_code(uint64_t guest_pc,TranslationBlock * tb,const void * start)3125584e2dbSIlya Leoshkevich void perf_report_code(uint64_t guest_pc, TranslationBlock *tb,
3135584e2dbSIlya Leoshkevich const void *start)
3145584e2dbSIlya Leoshkevich {
3155584e2dbSIlya Leoshkevich struct debuginfo_query *q;
316e1d8fabcSRichard Henderson size_t insn;
317747bd69dSRichard Henderson uint64_t *gen_insn_data;
3185584e2dbSIlya Leoshkevich
3195584e2dbSIlya Leoshkevich if (!perfmap && !jitdump) {
3205584e2dbSIlya Leoshkevich return;
3215584e2dbSIlya Leoshkevich }
3225584e2dbSIlya Leoshkevich
3235584e2dbSIlya Leoshkevich q = g_try_malloc0_n(tb->icount, sizeof(*q));
3245584e2dbSIlya Leoshkevich if (!q) {
3255584e2dbSIlya Leoshkevich return;
3265584e2dbSIlya Leoshkevich }
3275584e2dbSIlya Leoshkevich
3285584e2dbSIlya Leoshkevich debuginfo_lock();
3295584e2dbSIlya Leoshkevich
3305584e2dbSIlya Leoshkevich /* Query debuginfo for each guest instruction. */
331747bd69dSRichard Henderson gen_insn_data = tcg_ctx->gen_insn_data;
332747bd69dSRichard Henderson
3335584e2dbSIlya Leoshkevich for (insn = 0; insn < tb->icount; insn++) {
3345584e2dbSIlya Leoshkevich /* FIXME: This replicates the restore_state_to_opc() logic. */
335e1d8fabcSRichard Henderson q[insn].address = gen_insn_data[insn * INSN_START_WORDS + 0];
3364be79026SAnton Johansson if (tb_cflags(tb) & CF_PCREL) {
337*11efde54SRichard Henderson q[insn].address |= guest_pc & TARGET_PAGE_MASK;
3385584e2dbSIlya Leoshkevich }
3395584e2dbSIlya Leoshkevich q[insn].flags = DEBUGINFO_SYMBOL | (jitdump ? DEBUGINFO_LINE : 0);
3405584e2dbSIlya Leoshkevich }
3415584e2dbSIlya Leoshkevich debuginfo_query(q, tb->icount);
3425584e2dbSIlya Leoshkevich
3435584e2dbSIlya Leoshkevich /* Emit perfmap entries if needed. */
3445584e2dbSIlya Leoshkevich if (perfmap) {
3455584e2dbSIlya Leoshkevich flockfile(perfmap);
3465584e2dbSIlya Leoshkevich for (insn = 0; insn < tb->icount; insn++) {
3475584e2dbSIlya Leoshkevich write_perfmap_entry(start, insn, &q[insn]);
3485584e2dbSIlya Leoshkevich }
3495584e2dbSIlya Leoshkevich funlockfile(perfmap);
3505584e2dbSIlya Leoshkevich }
3515584e2dbSIlya Leoshkevich
3525584e2dbSIlya Leoshkevich /* Emit jitdump entries if needed. */
3535584e2dbSIlya Leoshkevich if (jitdump) {
3545584e2dbSIlya Leoshkevich flockfile(jitdump);
3555584e2dbSIlya Leoshkevich write_jr_code_debug_info(start, q, tb->icount);
3565584e2dbSIlya Leoshkevich write_jr_code_load(start, tcg_ctx->gen_insn_end_off[tb->icount - 1],
3575584e2dbSIlya Leoshkevich q);
3585584e2dbSIlya Leoshkevich funlockfile(jitdump);
3595584e2dbSIlya Leoshkevich }
3605584e2dbSIlya Leoshkevich
3615584e2dbSIlya Leoshkevich debuginfo_unlock();
3625584e2dbSIlya Leoshkevich g_free(q);
3635584e2dbSIlya Leoshkevich }
3645584e2dbSIlya Leoshkevich
perf_exit(void)3655584e2dbSIlya Leoshkevich void perf_exit(void)
3665584e2dbSIlya Leoshkevich {
3675584e2dbSIlya Leoshkevich if (perfmap) {
3685584e2dbSIlya Leoshkevich fclose(perfmap);
3695584e2dbSIlya Leoshkevich perfmap = NULL;
3705584e2dbSIlya Leoshkevich }
3715584e2dbSIlya Leoshkevich
372e7cd7a39SIlya Leoshkevich if (perf_marker != MAP_FAILED) {
373e7cd7a39SIlya Leoshkevich munmap(perf_marker, perf_marker_size);
374e7cd7a39SIlya Leoshkevich perf_marker = MAP_FAILED;
375e7cd7a39SIlya Leoshkevich }
376e7cd7a39SIlya Leoshkevich
3775584e2dbSIlya Leoshkevich if (jitdump) {
3785584e2dbSIlya Leoshkevich fclose(jitdump);
3795584e2dbSIlya Leoshkevich jitdump = NULL;
3805584e2dbSIlya Leoshkevich }
3815584e2dbSIlya Leoshkevich }
382