2 * Linux perf perf-<pid>.map and jit-<pid>.dump integration.
4 * The jitdump spec can be found at [1].
6 * [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/tools/perf/Documentation/jitdump-specification.txt
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "qemu/osdep.h"
13 #include "exec/exec-all.h"
14 #include "qemu/timer.h"
17 #include "debuginfo.h"
20 static FILE *safe_fopen_w(const char *path
)
26 /* Delete the old file, if any. */
29 /* Avoid symlink attacks by using O_CREAT | O_EXCL. */
30 fd
= open(path
, O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
35 /* Convert fd to FILE*. */
49 void perf_enable_perfmap(void)
53 snprintf(map_file
, sizeof(map_file
), "/tmp/perf-%d.map", getpid());
54 perfmap
= safe_fopen_w(map_file
);
55 if (perfmap
== NULL
) {
56 warn_report("Could not open %s: %s, proceeding without perfmap",
57 map_file
, strerror(errno
));
61 /* Get PC and size of code JITed for guest instruction #INSN. */
62 static void get_host_pc_size(uintptr_t *host_pc
, uint16_t *host_size
,
63 const void *start
, size_t insn
)
65 uint16_t start_off
= insn
? tcg_ctx
->gen_insn_end_off
[insn
- 1] : 0;
68 *host_pc
= (uintptr_t)start
+ start_off
;
71 *host_size
= tcg_ctx
->gen_insn_end_off
[insn
] - start_off
;
75 static const char *pretty_symbol(const struct debuginfo_query
*q
, size_t *len
)
77 static __thread
char buf
[64];
81 tmp
= snprintf(buf
, sizeof(buf
), "guest-0x%"PRIx64
, q
->address
);
83 *len
= MIN(tmp
+ 1, sizeof(buf
));
90 *len
= strlen(q
->symbol
) + 1;
95 tmp
= snprintf(buf
, sizeof(buf
), "%s+0x%"PRIx64
, q
->symbol
, q
->offset
);
97 *len
= MIN(tmp
+ 1, sizeof(buf
));
102 static void write_perfmap_entry(const void *start
, size_t insn
,
103 const struct debuginfo_query
*q
)
108 get_host_pc_size(&host_pc
, &host_size
, start
, insn
);
109 fprintf(perfmap
, "%"PRIxPTR
" %"PRIx16
" %s\n",
110 host_pc
, host_size
, pretty_symbol(q
, NULL
));
113 static FILE *jitdump
;
115 #define JITHEADER_MAGIC 0x4A695444
116 #define JITHEADER_VERSION 1
129 enum jit_record_type
{
131 JIT_CODE_DEBUG_INFO
= 2,
140 struct jr_code_load
{
158 struct jr_code_debug_info
{
163 struct debug_entry entries
[];
166 static uint32_t get_e_machine(void)
168 Elf64_Ehdr elf_header
;
172 QEMU_BUILD_BUG_ON(offsetof(Elf32_Ehdr
, e_machine
) !=
173 offsetof(Elf64_Ehdr
, e_machine
));
175 exe
= fopen("/proc/self/exe", "r");
180 n
= fread(&elf_header
, sizeof(elf_header
), 1, exe
);
186 return elf_header
.e_machine
;
189 void perf_enable_jitdump(void)
191 struct jitheader header
;
192 char jitdump_file
[32];
196 warn_report("CLOCK_MONOTONIC is not available, proceeding without jitdump");
200 snprintf(jitdump_file
, sizeof(jitdump_file
), "jit-%d.dump", getpid());
201 jitdump
= safe_fopen_w(jitdump_file
);
202 if (jitdump
== NULL
) {
203 warn_report("Could not open %s: %s, proceeding without jitdump",
204 jitdump_file
, strerror(errno
));
209 * `perf inject` will see that the mapped file name in the corresponding
210 * PERF_RECORD_MMAP or PERF_RECORD_MMAP2 event is of the form jit-%d.dump
211 * and will process it as a jitdump file.
213 perf_marker
= mmap(NULL
, qemu_real_host_page_size(), PROT_READ
| PROT_EXEC
,
214 MAP_PRIVATE
, fileno(jitdump
), 0);
215 if (perf_marker
== MAP_FAILED
) {
216 warn_report("Could not map %s: %s, proceeding without jitdump",
217 jitdump_file
, strerror(errno
));
223 header
.magic
= JITHEADER_MAGIC
;
224 header
.version
= JITHEADER_VERSION
;
225 header
.total_size
= sizeof(header
);
226 header
.elf_mach
= get_e_machine();
228 header
.pid
= getpid();
229 header
.timestamp
= get_clock();
231 fwrite(&header
, sizeof(header
), 1, jitdump
);
234 void perf_report_prologue(const void *start
, size_t size
)
237 fprintf(perfmap
, "%"PRIxPTR
" %zx tcg-prologue-buffer\n",
238 (uintptr_t)start
, size
);
242 /* Write a JIT_CODE_DEBUG_INFO jitdump entry. */
243 static void write_jr_code_debug_info(const void *start
,
244 const struct debuginfo_query
*q
,
247 struct jr_code_debug_info rec
;
248 struct debug_entry ent
;
252 /* Write the header. */
253 rec
.p
.id
= JIT_CODE_DEBUG_INFO
;
254 rec
.p
.total_size
= sizeof(rec
) + sizeof(ent
) + 1;
255 rec
.p
.timestamp
= get_clock();
256 rec
.code_addr
= (uintptr_t)start
;
258 for (insn
= 0; insn
< icount
; insn
++) {
260 rec
.p
.total_size
+= sizeof(ent
) + strlen(q
[insn
].file
) + 1;
264 fwrite(&rec
, sizeof(rec
), 1, jitdump
);
266 /* Write the main debug entries. */
267 for (insn
= 0; insn
< icount
; insn
++) {
269 get_host_pc_size(&host_pc
, NULL
, start
, insn
);
271 ent
.lineno
= q
[insn
].line
;
273 fwrite(&ent
, sizeof(ent
), 1, jitdump
);
274 fwrite(q
[insn
].file
, strlen(q
[insn
].file
) + 1, 1, jitdump
);
278 /* Write the trailing debug_entry. */
279 ent
.addr
= (uintptr_t)start
+ tcg_ctx
->gen_insn_end_off
[icount
- 1];
282 fwrite(&ent
, sizeof(ent
), 1, jitdump
);
283 fwrite("", 1, 1, jitdump
);
286 /* Write a JIT_CODE_LOAD jitdump entry. */
287 static void write_jr_code_load(const void *start
, uint16_t host_size
,
288 const struct debuginfo_query
*q
)
290 static uint64_t code_index
;
291 struct jr_code_load rec
;
295 symbol
= pretty_symbol(q
, &symbol_size
);
296 rec
.p
.id
= JIT_CODE_LOAD
;
297 rec
.p
.total_size
= sizeof(rec
) + symbol_size
+ host_size
;
298 rec
.p
.timestamp
= get_clock();
300 rec
.tid
= qemu_get_thread_id();
301 rec
.vma
= (uintptr_t)start
;
302 rec
.code_addr
= (uintptr_t)start
;
303 rec
.code_size
= host_size
;
304 rec
.code_index
= code_index
++;
305 fwrite(&rec
, sizeof(rec
), 1, jitdump
);
306 fwrite(symbol
, symbol_size
, 1, jitdump
);
307 fwrite(start
, host_size
, 1, jitdump
);
310 void perf_report_code(uint64_t guest_pc
, TranslationBlock
*tb
,
313 struct debuginfo_query
*q
;
316 if (!perfmap
&& !jitdump
) {
320 q
= g_try_malloc0_n(tb
->icount
, sizeof(*q
));
327 /* Query debuginfo for each guest instruction. */
328 for (insn
= 0; insn
< tb
->icount
; insn
++) {
329 /* FIXME: This replicates the restore_state_to_opc() logic. */
330 q
[insn
].address
= tcg_ctx
->gen_insn_data
[insn
][0];
331 if (tb_cflags(tb
) & CF_PCREL
) {
332 q
[insn
].address
|= (guest_pc
& TARGET_PAGE_MASK
);
334 #if defined(TARGET_I386)
335 q
[insn
].address
-= tb
->cs_base
;
338 q
[insn
].flags
= DEBUGINFO_SYMBOL
| (jitdump
? DEBUGINFO_LINE
: 0);
340 debuginfo_query(q
, tb
->icount
);
342 /* Emit perfmap entries if needed. */
345 for (insn
= 0; insn
< tb
->icount
; insn
++) {
346 write_perfmap_entry(start
, insn
, &q
[insn
]);
348 funlockfile(perfmap
);
351 /* Emit jitdump entries if needed. */
354 write_jr_code_debug_info(start
, q
, tb
->icount
);
355 write_jr_code_load(start
, tcg_ctx
->gen_insn_end_off
[tb
->icount
- 1],
357 funlockfile(jitdump
);