1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/sysmacros.h>
3 #include <sys/types.h>
4 #include <errno.h>
5 #include <libgen.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include <byteswap.h>
13 #include <sys/stat.h>
14 #include <sys/mman.h>
15 #include <linux/stringify.h>
16
17 #include "event.h"
18 #include "debug.h"
19 #include "dso.h"
20 #include "evlist.h"
21 #include "namespaces.h"
22 #include "symbol.h"
23 #include <elf.h>
24
25 #include "tsc.h"
26 #include "session.h"
27 #include "jit.h"
28 #include "jitdump.h"
29 #include "genelf.h"
30 #include "thread.h"
31
32 #include <linux/ctype.h>
33 #include <linux/zalloc.h>
34
35 struct jit_buf_desc {
36 struct perf_data *output;
37 struct perf_session *session;
38 struct machine *machine;
39 struct nsinfo *nsi;
40 union jr_entry *entry;
41 void *buf;
42 uint64_t sample_type;
43 size_t bufsize;
44 FILE *in;
45 bool needs_bswap; /* handles cross-endianness */
46 bool use_arch_timestamp;
47 void *debug_data;
48 void *unwinding_data;
49 uint64_t unwinding_size;
50 uint64_t unwinding_mapped_size;
51 uint64_t eh_frame_hdr_size;
52 size_t nr_debug_entries;
53 uint32_t code_load_count;
54 u64 bytes_written;
55 struct rb_root code_root;
56 char dir[PATH_MAX];
57 };
58
59 struct jit_tool {
60 struct perf_tool tool;
61 struct perf_data output;
62 struct perf_data input;
63 u64 bytes_written;
64 };
65
66 #define hmax(a, b) ((a) > (b) ? (a) : (b))
67 #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
68
69 static int
jit_emit_elf(struct jit_buf_desc * jd,char * filename,const char * sym,uint64_t code_addr,const void * code,int csize,void * debug,int nr_debug_entries,void * unwinding,uint32_t unwinding_header_size,uint32_t unwinding_size)70 jit_emit_elf(struct jit_buf_desc *jd,
71 char *filename,
72 const char *sym,
73 uint64_t code_addr,
74 const void *code,
75 int csize,
76 void *debug,
77 int nr_debug_entries,
78 void *unwinding,
79 uint32_t unwinding_header_size,
80 uint32_t unwinding_size)
81 {
82 int ret, fd, saved_errno;
83 struct nscookie nsc;
84
85 if (verbose > 0)
86 fprintf(stderr, "write ELF image %s\n", filename);
87
88 nsinfo__mountns_enter(jd->nsi, &nsc);
89 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
90 saved_errno = errno;
91 nsinfo__mountns_exit(&nsc);
92 if (fd == -1) {
93 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(saved_errno));
94 return -1;
95 }
96
97 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
98 unwinding, unwinding_header_size, unwinding_size);
99
100 close(fd);
101
102 if (ret) {
103 nsinfo__mountns_enter(jd->nsi, &nsc);
104 unlink(filename);
105 nsinfo__mountns_exit(&nsc);
106 }
107
108 return ret;
109 }
110
111 static void
jit_close(struct jit_buf_desc * jd)112 jit_close(struct jit_buf_desc *jd)
113 {
114 if (!(jd && jd->in))
115 return;
116 funlockfile(jd->in);
117 fclose(jd->in);
118 jd->in = NULL;
119 }
120
121 static int
jit_validate_events(struct perf_session * session)122 jit_validate_events(struct perf_session *session)
123 {
124 struct evsel *evsel;
125
126 /*
127 * check that all events use CLOCK_MONOTONIC
128 */
129 evlist__for_each_entry(session->evlist, evsel) {
130 if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
131 return -1;
132 }
133 return 0;
134 }
135
136 static int
jit_open(struct jit_buf_desc * jd,const char * name)137 jit_open(struct jit_buf_desc *jd, const char *name)
138 {
139 struct jitheader header;
140 struct nscookie nsc;
141 struct jr_prefix *prefix;
142 ssize_t bs, bsz = 0;
143 void *n, *buf = NULL;
144 int ret, retval = -1;
145
146 nsinfo__mountns_enter(jd->nsi, &nsc);
147 jd->in = fopen(name, "r");
148 nsinfo__mountns_exit(&nsc);
149 if (!jd->in)
150 return -1;
151
152 bsz = hmax(sizeof(header), sizeof(*prefix));
153
154 buf = malloc(bsz);
155 if (!buf)
156 goto error;
157
158 /*
159 * protect from writer modifying the file while we are reading it
160 */
161 flockfile(jd->in);
162
163 ret = fread(buf, sizeof(header), 1, jd->in);
164 if (ret != 1)
165 goto error;
166
167 memcpy(&header, buf, sizeof(header));
168
169 if (header.magic != JITHEADER_MAGIC) {
170 if (header.magic != JITHEADER_MAGIC_SW)
171 goto error;
172 jd->needs_bswap = true;
173 }
174
175 if (jd->needs_bswap) {
176 header.version = bswap_32(header.version);
177 header.total_size = bswap_32(header.total_size);
178 header.pid = bswap_32(header.pid);
179 header.elf_mach = bswap_32(header.elf_mach);
180 header.timestamp = bswap_64(header.timestamp);
181 header.flags = bswap_64(header.flags);
182 }
183
184 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
185
186 if (verbose > 2)
187 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
188 header.version,
189 header.total_size,
190 (unsigned long long)header.timestamp,
191 header.pid,
192 header.elf_mach,
193 jd->use_arch_timestamp);
194
195 if (header.version > JITHEADER_VERSION) {
196 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
197 header.version);
198 goto error;
199 }
200
201 if (header.flags & JITDUMP_FLAGS_RESERVED) {
202 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
203 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
204 goto error;
205 }
206
207 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
208 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
209 goto error;
210 }
211
212 /*
213 * validate event is using the correct clockid
214 */
215 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
216 pr_err("error, jitted code must be sampled with perf record -k 1\n");
217 goto error;
218 }
219
220 bs = header.total_size - sizeof(header);
221
222 if (bs > bsz) {
223 n = realloc(buf, bs);
224 if (!n)
225 goto error;
226 bsz = bs;
227 buf = n;
228 /* read extra we do not know about */
229 ret = fread(buf, bs - bsz, 1, jd->in);
230 if (ret != 1)
231 goto error;
232 }
233 /*
234 * keep dirname for generating files and mmap records
235 */
236 strcpy(jd->dir, name);
237 dirname(jd->dir);
238 free(buf);
239
240 return 0;
241 error:
242 free(buf);
243 funlockfile(jd->in);
244 fclose(jd->in);
245 return retval;
246 }
247
248 static union jr_entry *
jit_get_next_entry(struct jit_buf_desc * jd)249 jit_get_next_entry(struct jit_buf_desc *jd)
250 {
251 struct jr_prefix *prefix;
252 union jr_entry *jr;
253 void *addr;
254 size_t bs, size;
255 int id, ret;
256
257 if (!(jd && jd->in))
258 return NULL;
259
260 if (jd->buf == NULL) {
261 size_t sz = getpagesize();
262 if (sz < sizeof(*prefix))
263 sz = sizeof(*prefix);
264
265 jd->buf = malloc(sz);
266 if (jd->buf == NULL)
267 return NULL;
268
269 jd->bufsize = sz;
270 }
271
272 prefix = jd->buf;
273
274 /*
275 * file is still locked at this point
276 */
277 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
278 if (ret != 1)
279 return NULL;
280
281 if (jd->needs_bswap) {
282 prefix->id = bswap_32(prefix->id);
283 prefix->total_size = bswap_32(prefix->total_size);
284 prefix->timestamp = bswap_64(prefix->timestamp);
285 }
286 id = prefix->id;
287 size = prefix->total_size;
288
289 bs = (size_t)size;
290 if (bs < sizeof(*prefix))
291 return NULL;
292
293 if (id >= JIT_CODE_MAX) {
294 pr_warning("next_entry: unknown record type %d, skipping\n", id);
295 }
296 if (bs > jd->bufsize) {
297 void *n;
298 n = realloc(jd->buf, bs);
299 if (!n)
300 return NULL;
301 jd->buf = n;
302 jd->bufsize = bs;
303 }
304
305 addr = ((void *)jd->buf) + sizeof(*prefix);
306
307 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
308 if (ret != 1)
309 return NULL;
310
311 jr = (union jr_entry *)jd->buf;
312
313 switch(id) {
314 case JIT_CODE_DEBUG_INFO:
315 if (jd->needs_bswap) {
316 uint64_t n;
317 jr->info.code_addr = bswap_64(jr->info.code_addr);
318 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
319 for (n = 0 ; n < jr->info.nr_entry; n++) {
320 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
321 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
322 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
323 }
324 }
325 break;
326 case JIT_CODE_UNWINDING_INFO:
327 if (jd->needs_bswap) {
328 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
329 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
330 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
331 }
332 break;
333 case JIT_CODE_CLOSE:
334 break;
335 case JIT_CODE_LOAD:
336 if (jd->needs_bswap) {
337 jr->load.pid = bswap_32(jr->load.pid);
338 jr->load.tid = bswap_32(jr->load.tid);
339 jr->load.vma = bswap_64(jr->load.vma);
340 jr->load.code_addr = bswap_64(jr->load.code_addr);
341 jr->load.code_size = bswap_64(jr->load.code_size);
342 jr->load.code_index= bswap_64(jr->load.code_index);
343 }
344 jd->code_load_count++;
345 break;
346 case JIT_CODE_MOVE:
347 if (jd->needs_bswap) {
348 jr->move.pid = bswap_32(jr->move.pid);
349 jr->move.tid = bswap_32(jr->move.tid);
350 jr->move.vma = bswap_64(jr->move.vma);
351 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
352 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
353 jr->move.code_size = bswap_64(jr->move.code_size);
354 jr->move.code_index = bswap_64(jr->move.code_index);
355 }
356 break;
357 case JIT_CODE_MAX:
358 default:
359 /* skip unknown record (we have read them) */
360 break;
361 }
362 return jr;
363 }
364
365 static int
jit_inject_event(struct jit_buf_desc * jd,union perf_event * event)366 jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
367 {
368 ssize_t size;
369
370 size = perf_data__write(jd->output, event, event->header.size);
371 if (size < 0)
372 return -1;
373
374 jd->bytes_written += size;
375 return 0;
376 }
377
jr_entry_pid(struct jit_buf_desc * jd,union jr_entry * jr)378 static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr)
379 {
380 if (jd->nsi && nsinfo__in_pidns(jd->nsi))
381 return nsinfo__tgid(jd->nsi);
382 return jr->load.pid;
383 }
384
jr_entry_tid(struct jit_buf_desc * jd,union jr_entry * jr)385 static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr)
386 {
387 if (jd->nsi && nsinfo__in_pidns(jd->nsi))
388 return nsinfo__pid(jd->nsi);
389 return jr->load.tid;
390 }
391
convert_timestamp(struct jit_buf_desc * jd,uint64_t timestamp)392 static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
393 {
394 struct perf_tsc_conversion tc = { .time_shift = 0, };
395 struct perf_record_time_conv *time_conv = &jd->session->time_conv;
396
397 if (!jd->use_arch_timestamp)
398 return timestamp;
399
400 tc.time_shift = time_conv->time_shift;
401 tc.time_mult = time_conv->time_mult;
402 tc.time_zero = time_conv->time_zero;
403
404 /*
405 * The event TIME_CONV was extended for the fields from "time_cycles"
406 * when supported cap_user_time_short, for backward compatibility,
407 * checks the event size and assigns these extended fields if these
408 * fields are contained in the event.
409 */
410 if (event_contains(*time_conv, time_cycles)) {
411 tc.time_cycles = time_conv->time_cycles;
412 tc.time_mask = time_conv->time_mask;
413 tc.cap_user_time_zero = time_conv->cap_user_time_zero;
414 tc.cap_user_time_short = time_conv->cap_user_time_short;
415
416 if (!tc.cap_user_time_zero)
417 return 0;
418 }
419
420 return tsc_to_perf_time(timestamp, &tc);
421 }
422
jit_repipe_code_load(struct jit_buf_desc * jd,union jr_entry * jr)423 static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
424 {
425 struct perf_sample sample;
426 union perf_event *event;
427 const struct perf_tool *tool = jd->session->tool;
428 uint64_t code, addr;
429 uintptr_t uaddr;
430 char *filename;
431 struct stat st;
432 size_t size;
433 u16 idr_size;
434 const char *sym;
435 uint64_t count;
436 int ret, csize, usize;
437 pid_t nspid, pid, tid;
438 struct {
439 u32 pid, tid;
440 u64 time;
441 } *id;
442
443 nspid = jr->load.pid;
444 pid = jr_entry_pid(jd, jr);
445 tid = jr_entry_tid(jd, jr);
446 csize = jr->load.code_size;
447 usize = jd->unwinding_mapped_size;
448 addr = jr->load.code_addr;
449 sym = (void *)((unsigned long)jr + sizeof(jr->load));
450 code = (unsigned long)jr + jr->load.p.total_size - csize;
451 count = jr->load.code_index;
452 idr_size = jd->machine->id_hdr_size;
453
454 event = calloc(1, sizeof(*event) + idr_size);
455 if (!event)
456 return -1;
457
458 filename = event->mmap2.filename;
459 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
460 jd->dir,
461 nspid,
462 count);
463
464 size++; /* for \0 */
465
466 size = PERF_ALIGN(size, sizeof(u64));
467 uaddr = (uintptr_t)code;
468 ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
469 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
470
471 if (jd->debug_data && jd->nr_debug_entries) {
472 zfree(&jd->debug_data);
473 jd->nr_debug_entries = 0;
474 }
475
476 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
477 zfree(&jd->unwinding_data);
478 jd->eh_frame_hdr_size = 0;
479 jd->unwinding_mapped_size = 0;
480 jd->unwinding_size = 0;
481 }
482
483 if (ret) {
484 free(event);
485 return -1;
486 }
487 if (nsinfo__stat(filename, &st, jd->nsi))
488 memset(&st, 0, sizeof(st));
489
490 event->mmap2.header.type = PERF_RECORD_MMAP2;
491 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
492 event->mmap2.header.size = (sizeof(event->mmap2) -
493 (sizeof(event->mmap2.filename) - size) + idr_size);
494
495 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
496 event->mmap2.start = addr;
497 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
498 event->mmap2.pid = pid;
499 event->mmap2.tid = tid;
500 event->mmap2.ino = st.st_ino;
501 event->mmap2.maj = major(st.st_dev);
502 event->mmap2.min = minor(st.st_dev);
503 event->mmap2.prot = st.st_mode;
504 event->mmap2.flags = MAP_SHARED;
505 event->mmap2.ino_generation = 1;
506
507 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
508 if (jd->sample_type & PERF_SAMPLE_TID) {
509 id->pid = pid;
510 id->tid = tid;
511 }
512 if (jd->sample_type & PERF_SAMPLE_TIME)
513 id->time = convert_timestamp(jd, jr->load.p.timestamp);
514
515 /*
516 * create pseudo sample to induce dso hit increment
517 * use first address as sample address
518 */
519 perf_sample__init(&sample, /*all=*/true);
520 sample.cpumode = PERF_RECORD_MISC_USER;
521 sample.pid = pid;
522 sample.tid = tid;
523 sample.time = id->time;
524 sample.ip = addr;
525
526 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
527 if (ret)
528 goto out;
529
530 ret = jit_inject_event(jd, event);
531 /*
532 * mark dso as use to generate buildid in the header
533 */
534 if (!ret) {
535 struct dso_id dso_id = {
536 {
537 .maj = event->mmap2.maj,
538 .min = event->mmap2.min,
539 .ino = event->mmap2.ino,
540 .ino_generation = event->mmap2.ino_generation,
541 },
542 .mmap2_valid = true,
543 .mmap2_ino_generation_valid = true,
544 };
545 struct dso *dso = machine__findnew_dso_id(jd->machine, filename, &dso_id);
546
547 if (dso)
548 dso__set_hit(dso);
549 }
550 out:
551 perf_sample__exit(&sample);
552 free(event);
553 return ret;
554 }
555
jit_repipe_code_move(struct jit_buf_desc * jd,union jr_entry * jr)556 static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
557 {
558 struct perf_sample sample;
559 union perf_event *event;
560 const struct perf_tool *tool = jd->session->tool;
561 char *filename;
562 size_t size;
563 struct stat st;
564 int usize;
565 u16 idr_size;
566 int ret;
567 pid_t nspid, pid, tid;
568 struct {
569 u32 pid, tid;
570 u64 time;
571 } *id;
572
573 nspid = jr->load.pid;
574 pid = jr_entry_pid(jd, jr);
575 tid = jr_entry_tid(jd, jr);
576 usize = jd->unwinding_mapped_size;
577 idr_size = jd->machine->id_hdr_size;
578
579 /*
580 * +16 to account for sample_id_all (hack)
581 */
582 event = calloc(1, sizeof(*event) + 16);
583 if (!event)
584 return -1;
585
586 filename = event->mmap2.filename;
587 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
588 jd->dir,
589 nspid,
590 jr->move.code_index);
591
592 size++; /* for \0 */
593
594 if (nsinfo__stat(filename, &st, jd->nsi))
595 memset(&st, 0, sizeof(st));
596
597 size = PERF_ALIGN(size, sizeof(u64));
598
599 event->mmap2.header.type = PERF_RECORD_MMAP2;
600 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
601 event->mmap2.header.size = (sizeof(event->mmap2) -
602 (sizeof(event->mmap2.filename) - size) + idr_size);
603 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
604 event->mmap2.start = jr->move.new_code_addr;
605 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
606 : jr->move.code_size;
607 event->mmap2.pid = pid;
608 event->mmap2.tid = tid;
609 event->mmap2.ino = st.st_ino;
610 event->mmap2.maj = major(st.st_dev);
611 event->mmap2.min = minor(st.st_dev);
612 event->mmap2.prot = st.st_mode;
613 event->mmap2.flags = MAP_SHARED;
614 event->mmap2.ino_generation = 1;
615
616 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
617 if (jd->sample_type & PERF_SAMPLE_TID) {
618 id->pid = pid;
619 id->tid = tid;
620 }
621 if (jd->sample_type & PERF_SAMPLE_TIME)
622 id->time = convert_timestamp(jd, jr->load.p.timestamp);
623
624 /*
625 * create pseudo sample to induce dso hit increment
626 * use first address as sample address
627 */
628 perf_sample__init(&sample, /*all=*/true);
629 sample.cpumode = PERF_RECORD_MISC_USER;
630 sample.pid = pid;
631 sample.tid = tid;
632 sample.time = id->time;
633 sample.ip = jr->move.new_code_addr;
634
635 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
636 if (ret)
637 goto out;
638
639 ret = jit_inject_event(jd, event);
640 if (!ret)
641 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
642 out:
643 perf_sample__exit(&sample);
644 return ret;
645 }
646
jit_repipe_debug_info(struct jit_buf_desc * jd,union jr_entry * jr)647 static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
648 {
649 void *data;
650 size_t sz;
651
652 if (!(jd && jr))
653 return -1;
654
655 sz = jr->prefix.total_size - sizeof(jr->info);
656 data = malloc(sz);
657 if (!data)
658 return -1;
659
660 memcpy(data, &jr->info.entries, sz);
661
662 jd->debug_data = data;
663
664 /*
665 * we must use nr_entry instead of size here because
666 * we cannot distinguish actual entry from padding otherwise
667 */
668 jd->nr_debug_entries = jr->info.nr_entry;
669
670 return 0;
671 }
672
673 static int
jit_repipe_unwinding_info(struct jit_buf_desc * jd,union jr_entry * jr)674 jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
675 {
676 void *unwinding_data;
677 uint32_t unwinding_data_size;
678
679 if (!(jd && jr))
680 return -1;
681
682 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
683 unwinding_data = malloc(unwinding_data_size);
684 if (!unwinding_data)
685 return -1;
686
687 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
688 unwinding_data_size);
689
690 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
691 jd->unwinding_size = jr->unwinding.unwinding_size;
692 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
693 free(jd->unwinding_data);
694 jd->unwinding_data = unwinding_data;
695
696 return 0;
697 }
698
699 static int
jit_process_dump(struct jit_buf_desc * jd)700 jit_process_dump(struct jit_buf_desc *jd)
701 {
702 union jr_entry *jr;
703 int ret = 0;
704
705 while ((jr = jit_get_next_entry(jd))) {
706 switch(jr->prefix.id) {
707 case JIT_CODE_LOAD:
708 ret = jit_repipe_code_load(jd, jr);
709 break;
710 case JIT_CODE_MOVE:
711 ret = jit_repipe_code_move(jd, jr);
712 break;
713 case JIT_CODE_DEBUG_INFO:
714 ret = jit_repipe_debug_info(jd, jr);
715 break;
716 case JIT_CODE_UNWINDING_INFO:
717 ret = jit_repipe_unwinding_info(jd, jr);
718 break;
719 default:
720 ret = 0;
721 continue;
722 }
723 }
724 return ret;
725 }
726
727 static int
jit_inject(struct jit_buf_desc * jd,const char * path)728 jit_inject(struct jit_buf_desc *jd, const char *path)
729 {
730 int ret;
731
732 if (verbose > 0)
733 fprintf(stderr, "injecting: %s\n", path);
734
735 ret = jit_open(jd, path);
736 if (ret)
737 return -1;
738
739 ret = jit_process_dump(jd);
740
741 jit_close(jd);
742
743 if (verbose > 0)
744 fprintf(stderr, "injected: %s (%d)\n", path, ret);
745
746 return 0;
747 }
748
749 /*
750 * File must be with pattern .../jit-XXXX.dump
751 * where XXXX is the PID of the process which did the mmap()
752 * as captured in the RECORD_MMAP record
753 */
754 static int
jit_detect(const char * mmap_name,pid_t pid,struct nsinfo * nsi,bool * in_pidns)755 jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi, bool *in_pidns)
756 {
757 char *p;
758 char *end = NULL;
759 pid_t pid2;
760
761 if (verbose > 2)
762 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
763 /*
764 * get file name
765 */
766 p = strrchr(mmap_name, '/');
767 if (!p)
768 return -1;
769
770 /*
771 * match prefix
772 */
773 if (strncmp(p, "/jit-", 5))
774 return -1;
775
776 /*
777 * skip prefix
778 */
779 p += 5;
780
781 /*
782 * must be followed by a pid
783 */
784 if (!isdigit(*p))
785 return -1;
786
787 pid2 = (int)strtol(p, &end, 10);
788 if (!end)
789 return -1;
790
791 *in_pidns = pid == nsinfo__nstgid(nsi);
792 /*
793 * pid does not match mmap pid
794 * pid==0 in system-wide mode (synthesized)
795 *
796 * If the pid in the file name is equal to the nstgid, then
797 * the agent ran inside a container and perf outside the
798 * container, so record it for further use in jit_inject().
799 */
800 if (pid && !(pid2 == pid || *in_pidns))
801 return -1;
802 /*
803 * validate suffix
804 */
805 if (strcmp(end, ".dump"))
806 return -1;
807
808 if (verbose > 0)
809 fprintf(stderr, "jit marker found: %s\n", mmap_name);
810
811 return 0;
812 }
813
jit_add_pid(struct machine * machine,pid_t pid)814 static void jit_add_pid(struct machine *machine, pid_t pid)
815 {
816 struct thread *thread = machine__findnew_thread(machine, pid, pid);
817
818 if (!thread) {
819 pr_err("%s: thread %d not found or created\n", __func__, pid);
820 return;
821 }
822
823 thread__set_priv(thread, (void *)true);
824 thread__put(thread);
825 }
826
jit_has_pid(struct machine * machine,pid_t pid)827 static bool jit_has_pid(struct machine *machine, pid_t pid)
828 {
829 struct thread *thread = machine__find_thread(machine, pid, pid);
830 void *priv;
831
832 if (!thread)
833 return false;
834
835 priv = thread__priv(thread);
836 thread__put(thread);
837 return (bool)priv;
838 }
839
840 int
jit_process(struct perf_session * session,struct perf_data * output,struct machine * machine,const char * filename,pid_t pid,pid_t tid,u64 * nbytes)841 jit_process(struct perf_session *session,
842 struct perf_data *output,
843 struct machine *machine,
844 const char *filename,
845 pid_t pid,
846 pid_t tid,
847 u64 *nbytes)
848 {
849 struct thread *thread;
850 struct nsinfo *nsi;
851 struct evsel *first;
852 struct jit_buf_desc jd;
853 bool in_pidns = false;
854 int ret;
855
856 thread = machine__findnew_thread(machine, pid, tid);
857 if (thread == NULL) {
858 pr_err("problem processing JIT mmap event, skipping it.\n");
859 return 0;
860 }
861
862 nsi = nsinfo__get(thread__nsinfo(thread));
863 thread__put(thread);
864
865 /*
866 * first, detect marker mmap (i.e., the jitdump mmap)
867 */
868 if (jit_detect(filename, pid, nsi, &in_pidns)) {
869 nsinfo__put(nsi);
870
871 /*
872 * Strip //anon*, [anon:* and /memfd:* mmaps if we processed a jitdump for this pid
873 */
874 if (jit_has_pid(machine, pid) &&
875 ((strncmp(filename, "//anon", 6) == 0) ||
876 (strncmp(filename, "[anon:", 6) == 0) ||
877 (strncmp(filename, "/memfd:", 7) == 0)))
878 return 1;
879
880 return 0;
881 }
882
883 memset(&jd, 0, sizeof(jd));
884
885 jd.session = session;
886 jd.output = output;
887 jd.machine = machine;
888 jd.nsi = nsi;
889
890 if (in_pidns)
891 nsinfo__set_in_pidns(nsi);
892
893 /*
894 * track sample_type to compute id_all layout
895 * perf sets the same sample type to all events as of now
896 */
897 first = evlist__first(session->evlist);
898 jd.sample_type = first->core.attr.sample_type;
899
900 *nbytes = 0;
901
902 ret = jit_inject(&jd, filename);
903 if (!ret) {
904 jit_add_pid(machine, pid);
905 *nbytes = jd.bytes_written;
906 ret = 1;
907 }
908
909 nsinfo__put(jd.nsi);
910 free(jd.buf);
911
912 return ret;
913 }
914