xref: /openbmc/qemu/accel/tcg/perf.c (revision c3bef3b4)
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 
11 #include "qemu/osdep.h"
12 #include "elf.h"
13 #include "exec/exec-all.h"
14 #include "qemu/timer.h"
15 #include "tcg/tcg.h"
16 
17 #include "debuginfo.h"
18 #include "perf.h"
19 
20 static FILE *safe_fopen_w(const char *path)
21 {
22     int saved_errno;
23     FILE *f;
24     int fd;
25 
26     /* Delete the old file, if any. */
27     unlink(path);
28 
29     /* Avoid symlink attacks by using O_CREAT | O_EXCL. */
30     fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
31     if (fd == -1) {
32         return NULL;
33     }
34 
35     /* Convert fd to FILE*. */
36     f = fdopen(fd, "w");
37     if (f == NULL) {
38         saved_errno = errno;
39         close(fd);
40         errno = saved_errno;
41         return NULL;
42     }
43 
44     return f;
45 }
46 
47 static FILE *perfmap;
48 
49 void perf_enable_perfmap(void)
50 {
51     char map_file[32];
52 
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));
58     }
59 }
60 
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)
64 {
65     uint16_t start_off = insn ? tcg_ctx->gen_insn_end_off[insn - 1] : 0;
66 
67     if (host_pc) {
68         *host_pc = (uintptr_t)start + start_off;
69     }
70     if (host_size) {
71         *host_size = tcg_ctx->gen_insn_end_off[insn] - start_off;
72     }
73 }
74 
75 static const char *pretty_symbol(const struct debuginfo_query *q, size_t *len)
76 {
77     static __thread char buf[64];
78     int tmp;
79 
80     if (!q->symbol) {
81         tmp = snprintf(buf, sizeof(buf), "guest-0x%"PRIx64, q->address);
82         if (len) {
83             *len = MIN(tmp + 1, sizeof(buf));
84         }
85         return buf;
86     }
87 
88     if (!q->offset) {
89         if (len) {
90             *len = strlen(q->symbol) + 1;
91         }
92         return q->symbol;
93     }
94 
95     tmp = snprintf(buf, sizeof(buf), "%s+0x%"PRIx64, q->symbol, q->offset);
96     if (len) {
97         *len = MIN(tmp + 1, sizeof(buf));
98     }
99     return buf;
100 }
101 
102 static void write_perfmap_entry(const void *start, size_t insn,
103                                 const struct debuginfo_query *q)
104 {
105     uint16_t host_size;
106     uintptr_t host_pc;
107 
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));
111 }
112 
113 static FILE *jitdump;
114 
115 #define JITHEADER_MAGIC 0x4A695444
116 #define JITHEADER_VERSION 1
117 
118 struct jitheader {
119     uint32_t magic;
120     uint32_t version;
121     uint32_t total_size;
122     uint32_t elf_mach;
123     uint32_t pad1;
124     uint32_t pid;
125     uint64_t timestamp;
126     uint64_t flags;
127 };
128 
129 enum jit_record_type {
130     JIT_CODE_LOAD = 0,
131     JIT_CODE_DEBUG_INFO = 2,
132 };
133 
134 struct jr_prefix {
135     uint32_t id;
136     uint32_t total_size;
137     uint64_t timestamp;
138 };
139 
140 struct jr_code_load {
141     struct jr_prefix p;
142 
143     uint32_t pid;
144     uint32_t tid;
145     uint64_t vma;
146     uint64_t code_addr;
147     uint64_t code_size;
148     uint64_t code_index;
149 };
150 
151 struct debug_entry {
152     uint64_t addr;
153     int lineno;
154     int discrim;
155     const char name[];
156 };
157 
158 struct jr_code_debug_info {
159     struct jr_prefix p;
160 
161     uint64_t code_addr;
162     uint64_t nr_entry;
163     struct debug_entry entries[];
164 };
165 
166 static uint32_t get_e_machine(void)
167 {
168     Elf64_Ehdr elf_header;
169     FILE *exe;
170     size_t n;
171 
172     QEMU_BUILD_BUG_ON(offsetof(Elf32_Ehdr, e_machine) !=
173                       offsetof(Elf64_Ehdr, e_machine));
174 
175     exe = fopen("/proc/self/exe", "r");
176     if (exe == NULL) {
177         return EM_NONE;
178     }
179 
180     n = fread(&elf_header, sizeof(elf_header), 1, exe);
181     fclose(exe);
182     if (n != 1) {
183         return EM_NONE;
184     }
185 
186     return elf_header.e_machine;
187 }
188 
189 void perf_enable_jitdump(void)
190 {
191     struct jitheader header;
192     char jitdump_file[32];
193     void *perf_marker;
194 
195     if (!use_rt_clock) {
196         warn_report("CLOCK_MONOTONIC is not available, proceeding without jitdump");
197         return;
198     }
199 
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));
205         return;
206     }
207 
208     /*
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.
212      */
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));
218         fclose(jitdump);
219         jitdump = NULL;
220         return;
221     }
222 
223     header.magic = JITHEADER_MAGIC;
224     header.version = JITHEADER_VERSION;
225     header.total_size = sizeof(header);
226     header.elf_mach = get_e_machine();
227     header.pad1 = 0;
228     header.pid = getpid();
229     header.timestamp = get_clock();
230     header.flags = 0;
231     fwrite(&header, sizeof(header), 1, jitdump);
232 }
233 
234 void perf_report_prologue(const void *start, size_t size)
235 {
236     if (perfmap) {
237         fprintf(perfmap, "%"PRIxPTR" %zx tcg-prologue-buffer\n",
238                 (uintptr_t)start, size);
239     }
240 }
241 
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,
245                                      size_t icount)
246 {
247     struct jr_code_debug_info rec;
248     struct debug_entry ent;
249     uintptr_t host_pc;
250     int insn;
251 
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;
257     rec.nr_entry = 1;
258     for (insn = 0; insn < icount; insn++) {
259         if (q[insn].file) {
260             rec.p.total_size += sizeof(ent) + strlen(q[insn].file) + 1;
261             rec.nr_entry++;
262         }
263     }
264     fwrite(&rec, sizeof(rec), 1, jitdump);
265 
266     /* Write the main debug entries. */
267     for (insn = 0; insn < icount; insn++) {
268         if (q[insn].file) {
269             get_host_pc_size(&host_pc, NULL, start, insn);
270             ent.addr = host_pc;
271             ent.lineno = q[insn].line;
272             ent.discrim = 0;
273             fwrite(&ent, sizeof(ent), 1, jitdump);
274             fwrite(q[insn].file, strlen(q[insn].file) + 1, 1, jitdump);
275         }
276     }
277 
278     /* Write the trailing debug_entry. */
279     ent.addr = (uintptr_t)start + tcg_ctx->gen_insn_end_off[icount - 1];
280     ent.lineno = 0;
281     ent.discrim = 0;
282     fwrite(&ent, sizeof(ent), 1, jitdump);
283     fwrite("", 1, 1, jitdump);
284 }
285 
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)
289 {
290     static uint64_t code_index;
291     struct jr_code_load rec;
292     const char *symbol;
293     size_t symbol_size;
294 
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();
299     rec.pid = getpid();
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);
308 }
309 
310 void perf_report_code(uint64_t guest_pc, TranslationBlock *tb,
311                       const void *start)
312 {
313     struct debuginfo_query *q;
314     size_t insn;
315 
316     if (!perfmap && !jitdump) {
317         return;
318     }
319 
320     q = g_try_malloc0_n(tb->icount, sizeof(*q));
321     if (!q) {
322         return;
323     }
324 
325     debuginfo_lock();
326 
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 (TARGET_TB_PCREL) {
332             q[insn].address |= (guest_pc & TARGET_PAGE_MASK);
333         } else {
334 #if defined(TARGET_I386)
335             q[insn].address -= tb->cs_base;
336 #endif
337         }
338         q[insn].flags = DEBUGINFO_SYMBOL | (jitdump ? DEBUGINFO_LINE : 0);
339     }
340     debuginfo_query(q, tb->icount);
341 
342     /* Emit perfmap entries if needed. */
343     if (perfmap) {
344         flockfile(perfmap);
345         for (insn = 0; insn < tb->icount; insn++) {
346             write_perfmap_entry(start, insn, &q[insn]);
347         }
348         funlockfile(perfmap);
349     }
350 
351     /* Emit jitdump entries if needed. */
352     if (jitdump) {
353         flockfile(jitdump);
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],
356                            q);
357         funlockfile(jitdump);
358     }
359 
360     debuginfo_unlock();
361     g_free(q);
362 }
363 
364 void perf_exit(void)
365 {
366     if (perfmap) {
367         fclose(perfmap);
368         perfmap = NULL;
369     }
370 
371     if (jitdump) {
372         fclose(jitdump);
373         jitdump = NULL;
374     }
375 }
376