1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/sysmacros.h>
15 #include <linux/stringify.h>
21 #include "namespaces.h"
32 #include <linux/ctype.h>
33 #include <linux/zalloc.h>
36 struct perf_data *output;
37 struct perf_session *session;
38 struct machine *machine;
40 union jr_entry *entry;
45 bool needs_bswap; /* handles cross-endianness */
46 bool use_arch_timestamp;
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;
55 struct rb_root code_root;
59 struct debug_line_info {
62 /* The filename format is unspecified, absolute path, relative etc. */
63 char const filename[];
67 struct perf_tool tool;
68 struct perf_data output;
69 struct perf_data input;
73 #define hmax(a, b) ((a) > (b) ? (a) : (b))
74 #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
77 jit_emit_elf(struct jit_buf_desc *jd,
86 uint32_t unwinding_header_size,
87 uint32_t unwinding_size)
89 int ret, fd, saved_errno;
93 fprintf(stderr, "write ELF image %s\n", filename);
95 nsinfo__mountns_enter(jd->nsi, &nsc);
96 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
98 nsinfo__mountns_exit(&nsc);
100 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(saved_errno));
104 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
105 unwinding, unwinding_header_size, unwinding_size);
110 nsinfo__mountns_enter(jd->nsi, &nsc);
112 nsinfo__mountns_exit(&nsc);
119 jit_close(struct jit_buf_desc *jd)
129 jit_validate_events(struct perf_session *session)
134 * check that all events use CLOCK_MONOTONIC
136 evlist__for_each_entry(session->evlist, evsel) {
137 if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
144 jit_open(struct jit_buf_desc *jd, const char *name)
146 struct jitheader header;
148 struct jr_prefix *prefix;
150 void *n, *buf = NULL;
151 int ret, retval = -1;
153 nsinfo__mountns_enter(jd->nsi, &nsc);
154 jd->in = fopen(name, "r");
155 nsinfo__mountns_exit(&nsc);
159 bsz = hmax(sizeof(header), sizeof(*prefix));
166 * protect from writer modifying the file while we are reading it
170 ret = fread(buf, sizeof(header), 1, jd->in);
174 memcpy(&header, buf, sizeof(header));
176 if (header.magic != JITHEADER_MAGIC) {
177 if (header.magic != JITHEADER_MAGIC_SW)
179 jd->needs_bswap = true;
182 if (jd->needs_bswap) {
183 header.version = bswap_32(header.version);
184 header.total_size = bswap_32(header.total_size);
185 header.pid = bswap_32(header.pid);
186 header.elf_mach = bswap_32(header.elf_mach);
187 header.timestamp = bswap_64(header.timestamp);
188 header.flags = bswap_64(header.flags);
191 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
194 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
197 (unsigned long long)header.timestamp,
200 jd->use_arch_timestamp);
202 if (header.version > JITHEADER_VERSION) {
203 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
208 if (header.flags & JITDUMP_FLAGS_RESERVED) {
209 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
210 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
214 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
215 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
220 * validate event is using the correct clockid
222 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
223 pr_err("error, jitted code must be sampled with perf record -k 1\n");
227 bs = header.total_size - sizeof(header);
230 n = realloc(buf, bs);
235 /* read extra we do not know about */
236 ret = fread(buf, bs - bsz, 1, jd->in);
241 * keep dirname for generating files and mmap records
243 strcpy(jd->dir, name);
253 static union jr_entry *
254 jit_get_next_entry(struct jit_buf_desc *jd)
256 struct jr_prefix *prefix;
265 if (jd->buf == NULL) {
266 size_t sz = getpagesize();
267 if (sz < sizeof(*prefix))
268 sz = sizeof(*prefix);
270 jd->buf = malloc(sz);
280 * file is still locked at this point
282 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
286 if (jd->needs_bswap) {
287 prefix->id = bswap_32(prefix->id);
288 prefix->total_size = bswap_32(prefix->total_size);
289 prefix->timestamp = bswap_64(prefix->timestamp);
292 size = prefix->total_size;
295 if (bs < sizeof(*prefix))
298 if (id >= JIT_CODE_MAX) {
299 pr_warning("next_entry: unknown record type %d, skipping\n", id);
301 if (bs > jd->bufsize) {
303 n = realloc(jd->buf, bs);
310 addr = ((void *)jd->buf) + sizeof(*prefix);
312 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
316 jr = (union jr_entry *)jd->buf;
319 case JIT_CODE_DEBUG_INFO:
320 if (jd->needs_bswap) {
322 jr->info.code_addr = bswap_64(jr->info.code_addr);
323 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
324 for (n = 0 ; n < jr->info.nr_entry; n++) {
325 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
326 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
327 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
331 case JIT_CODE_UNWINDING_INFO:
332 if (jd->needs_bswap) {
333 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
334 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
335 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
341 if (jd->needs_bswap) {
342 jr->load.pid = bswap_32(jr->load.pid);
343 jr->load.tid = bswap_32(jr->load.tid);
344 jr->load.vma = bswap_64(jr->load.vma);
345 jr->load.code_addr = bswap_64(jr->load.code_addr);
346 jr->load.code_size = bswap_64(jr->load.code_size);
347 jr->load.code_index= bswap_64(jr->load.code_index);
349 jd->code_load_count++;
352 if (jd->needs_bswap) {
353 jr->move.pid = bswap_32(jr->move.pid);
354 jr->move.tid = bswap_32(jr->move.tid);
355 jr->move.vma = bswap_64(jr->move.vma);
356 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
357 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
358 jr->move.code_size = bswap_64(jr->move.code_size);
359 jr->move.code_index = bswap_64(jr->move.code_index);
364 /* skip unknown record (we have read them) */
371 jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
375 size = perf_data__write(jd->output, event, event->header.size);
379 jd->bytes_written += size;
383 static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr)
385 if (jd->nsi && nsinfo__in_pidns(jd->nsi))
386 return nsinfo__tgid(jd->nsi);
390 static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr)
392 if (jd->nsi && nsinfo__in_pidns(jd->nsi))
393 return nsinfo__pid(jd->nsi);
397 static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
399 struct perf_tsc_conversion tc = { .time_shift = 0, };
400 struct perf_record_time_conv *time_conv = &jd->session->time_conv;
402 if (!jd->use_arch_timestamp)
405 tc.time_shift = time_conv->time_shift;
406 tc.time_mult = time_conv->time_mult;
407 tc.time_zero = time_conv->time_zero;
410 * The event TIME_CONV was extended for the fields from "time_cycles"
411 * when supported cap_user_time_short, for backward compatibility,
412 * checks the event size and assigns these extended fields if these
413 * fields are contained in the event.
415 if (event_contains(*time_conv, time_cycles)) {
416 tc.time_cycles = time_conv->time_cycles;
417 tc.time_mask = time_conv->time_mask;
418 tc.cap_user_time_zero = time_conv->cap_user_time_zero;
419 tc.cap_user_time_short = time_conv->cap_user_time_short;
421 if (!tc.cap_user_time_zero)
425 return tsc_to_perf_time(timestamp, &tc);
428 static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
430 struct perf_sample sample;
431 union perf_event *event;
432 struct perf_tool *tool = jd->session->tool;
441 int ret, csize, usize;
442 pid_t nspid, pid, tid;
448 nspid = jr->load.pid;
449 pid = jr_entry_pid(jd, jr);
450 tid = jr_entry_tid(jd, jr);
451 csize = jr->load.code_size;
452 usize = jd->unwinding_mapped_size;
453 addr = jr->load.code_addr;
454 sym = (void *)((unsigned long)jr + sizeof(jr->load));
455 code = (unsigned long)jr + jr->load.p.total_size - csize;
456 count = jr->load.code_index;
457 idr_size = jd->machine->id_hdr_size;
459 event = calloc(1, sizeof(*event) + idr_size);
463 filename = event->mmap2.filename;
464 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
471 size = PERF_ALIGN(size, sizeof(u64));
472 uaddr = (uintptr_t)code;
473 ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
474 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
476 if (jd->debug_data && jd->nr_debug_entries) {
477 zfree(&jd->debug_data);
478 jd->nr_debug_entries = 0;
481 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
482 zfree(&jd->unwinding_data);
483 jd->eh_frame_hdr_size = 0;
484 jd->unwinding_mapped_size = 0;
485 jd->unwinding_size = 0;
492 if (nsinfo__stat(filename, &st, jd->nsi))
493 memset(&st, 0, sizeof(st));
495 event->mmap2.header.type = PERF_RECORD_MMAP2;
496 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
497 event->mmap2.header.size = (sizeof(event->mmap2) -
498 (sizeof(event->mmap2.filename) - size) + idr_size);
500 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
501 event->mmap2.start = addr;
502 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
503 event->mmap2.pid = pid;
504 event->mmap2.tid = tid;
505 event->mmap2.ino = st.st_ino;
506 event->mmap2.maj = major(st.st_dev);
507 event->mmap2.min = minor(st.st_dev);
508 event->mmap2.prot = st.st_mode;
509 event->mmap2.flags = MAP_SHARED;
510 event->mmap2.ino_generation = 1;
512 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
513 if (jd->sample_type & PERF_SAMPLE_TID) {
517 if (jd->sample_type & PERF_SAMPLE_TIME)
518 id->time = convert_timestamp(jd, jr->load.p.timestamp);
521 * create pseudo sample to induce dso hit increment
522 * use first address as sample address
524 memset(&sample, 0, sizeof(sample));
525 sample.cpumode = PERF_RECORD_MISC_USER;
528 sample.time = id->time;
531 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
535 ret = jit_inject_event(jd, event);
537 * mark dso as use to generate buildid in the header
540 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
545 static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
547 struct perf_sample sample;
548 union perf_event *event;
549 struct perf_tool *tool = jd->session->tool;
556 pid_t nspid, pid, tid;
562 nspid = jr->load.pid;
563 pid = jr_entry_pid(jd, jr);
564 tid = jr_entry_tid(jd, jr);
565 usize = jd->unwinding_mapped_size;
566 idr_size = jd->machine->id_hdr_size;
569 * +16 to account for sample_id_all (hack)
571 event = calloc(1, sizeof(*event) + 16);
575 filename = event->mmap2.filename;
576 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
579 jr->move.code_index);
583 if (nsinfo__stat(filename, &st, jd->nsi))
584 memset(&st, 0, sizeof(st));
586 size = PERF_ALIGN(size, sizeof(u64));
588 event->mmap2.header.type = PERF_RECORD_MMAP2;
589 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
590 event->mmap2.header.size = (sizeof(event->mmap2) -
591 (sizeof(event->mmap2.filename) - size) + idr_size);
592 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
593 event->mmap2.start = jr->move.new_code_addr;
594 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
595 : jr->move.code_size;
596 event->mmap2.pid = pid;
597 event->mmap2.tid = tid;
598 event->mmap2.ino = st.st_ino;
599 event->mmap2.maj = major(st.st_dev);
600 event->mmap2.min = minor(st.st_dev);
601 event->mmap2.prot = st.st_mode;
602 event->mmap2.flags = MAP_SHARED;
603 event->mmap2.ino_generation = 1;
605 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
606 if (jd->sample_type & PERF_SAMPLE_TID) {
610 if (jd->sample_type & PERF_SAMPLE_TIME)
611 id->time = convert_timestamp(jd, jr->load.p.timestamp);
614 * create pseudo sample to induce dso hit increment
615 * use first address as sample address
617 memset(&sample, 0, sizeof(sample));
618 sample.cpumode = PERF_RECORD_MISC_USER;
621 sample.time = id->time;
622 sample.ip = jr->move.new_code_addr;
624 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
628 ret = jit_inject_event(jd, event);
630 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
635 static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
643 sz = jr->prefix.total_size - sizeof(jr->info);
648 memcpy(data, &jr->info.entries, sz);
650 jd->debug_data = data;
653 * we must use nr_entry instead of size here because
654 * we cannot distinguish actual entry from padding otherwise
656 jd->nr_debug_entries = jr->info.nr_entry;
662 jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
664 void *unwinding_data;
665 uint32_t unwinding_data_size;
670 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
671 unwinding_data = malloc(unwinding_data_size);
675 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
676 unwinding_data_size);
678 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
679 jd->unwinding_size = jr->unwinding.unwinding_size;
680 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
681 jd->unwinding_data = unwinding_data;
687 jit_process_dump(struct jit_buf_desc *jd)
692 while ((jr = jit_get_next_entry(jd))) {
693 switch(jr->prefix.id) {
695 ret = jit_repipe_code_load(jd, jr);
698 ret = jit_repipe_code_move(jd, jr);
700 case JIT_CODE_DEBUG_INFO:
701 ret = jit_repipe_debug_info(jd, jr);
703 case JIT_CODE_UNWINDING_INFO:
704 ret = jit_repipe_unwinding_info(jd, jr);
715 jit_inject(struct jit_buf_desc *jd, char *path)
720 fprintf(stderr, "injecting: %s\n", path);
722 ret = jit_open(jd, path);
726 ret = jit_process_dump(jd);
731 fprintf(stderr, "injected: %s (%d)\n", path, ret);
737 * File must be with pattern .../jit-XXXX.dump
738 * where XXXX is the PID of the process which did the mmap()
739 * as captured in the RECORD_MMAP record
742 jit_detect(char *mmap_name, pid_t pid, struct nsinfo *nsi)
749 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
753 p = strrchr(mmap_name, '/');
760 if (strncmp(p, "/jit-", 5))
769 * must be followed by a pid
774 pid2 = (int)strtol(p, &end, 10);
779 * pid does not match mmap pid
780 * pid==0 in system-wide mode (synthesized)
782 if (pid && pid2 != nsinfo__nstgid(nsi))
787 if (strcmp(end, ".dump"))
791 fprintf(stderr, "jit marker found: %s\n", mmap_name);
796 static void jit_add_pid(struct machine *machine, pid_t pid)
798 struct thread *thread = machine__findnew_thread(machine, pid, pid);
801 pr_err("%s: thread %d not found or created\n", __func__, pid);
805 thread->priv = (void *)1;
808 static bool jit_has_pid(struct machine *machine, pid_t pid)
810 struct thread *thread = machine__find_thread(machine, pid, pid);
815 return (bool)thread->priv;
819 jit_process(struct perf_session *session,
820 struct perf_data *output,
821 struct machine *machine,
827 struct thread *thread;
830 struct jit_buf_desc jd;
833 thread = machine__findnew_thread(machine, pid, tid);
834 if (thread == NULL) {
835 pr_err("problem processing JIT mmap event, skipping it.\n");
839 nsi = nsinfo__get(thread->nsinfo);
843 * first, detect marker mmap (i.e., the jitdump mmap)
845 if (jit_detect(filename, pid, nsi)) {
848 // Strip //anon* mmaps if we processed a jitdump for this pid
849 if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
855 memset(&jd, 0, sizeof(jd));
857 jd.session = session;
859 jd.machine = machine;
863 * track sample_type to compute id_all layout
864 * perf sets the same sample type to all events as of now
866 first = evlist__first(session->evlist);
867 jd.sample_type = first->core.attr.sample_type;
871 ret = jit_inject(&jd, filename);
873 jit_add_pid(machine, pid);
874 *nbytes = jd.bytes_written;