1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/sysmacros.h>
15 #include <linux/stringify.h>
31 #include <linux/ctype.h>
32 #include <linux/zalloc.h>
35 struct perf_data *output;
36 struct perf_session *session;
37 struct machine *machine;
38 union jr_entry *entry;
43 bool needs_bswap; /* handles cross-endianness */
44 bool use_arch_timestamp;
47 uint64_t unwinding_size;
48 uint64_t unwinding_mapped_size;
49 uint64_t eh_frame_hdr_size;
50 size_t nr_debug_entries;
51 uint32_t code_load_count;
53 struct rb_root code_root;
57 struct debug_line_info {
60 /* The filename format is unspecified, absolute path, relative etc. */
61 char const filename[];
65 struct perf_tool tool;
66 struct perf_data output;
67 struct perf_data input;
71 #define hmax(a, b) ((a) > (b) ? (a) : (b))
72 #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
75 jit_emit_elf(char *filename,
83 uint32_t unwinding_header_size,
84 uint32_t unwinding_size)
89 fprintf(stderr, "write ELF image %s\n", filename);
91 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
93 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(errno));
97 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
98 unwinding, unwinding_header_size, unwinding_size);
109 jit_close(struct jit_buf_desc *jd)
119 jit_validate_events(struct perf_session *session)
124 * check that all events use CLOCK_MONOTONIC
126 evlist__for_each_entry(session->evlist, evsel) {
127 if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
134 jit_open(struct jit_buf_desc *jd, const char *name)
136 struct jitheader header;
137 struct jr_prefix *prefix;
139 void *n, *buf = NULL;
140 int ret, retval = -1;
142 jd->in = fopen(name, "r");
146 bsz = hmax(sizeof(header), sizeof(*prefix));
153 * protect from writer modifying the file while we are reading it
157 ret = fread(buf, sizeof(header), 1, jd->in);
161 memcpy(&header, buf, sizeof(header));
163 if (header.magic != JITHEADER_MAGIC) {
164 if (header.magic != JITHEADER_MAGIC_SW)
166 jd->needs_bswap = true;
169 if (jd->needs_bswap) {
170 header.version = bswap_32(header.version);
171 header.total_size = bswap_32(header.total_size);
172 header.pid = bswap_32(header.pid);
173 header.elf_mach = bswap_32(header.elf_mach);
174 header.timestamp = bswap_64(header.timestamp);
175 header.flags = bswap_64(header.flags);
178 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
181 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
184 (unsigned long long)header.timestamp,
187 jd->use_arch_timestamp);
189 if (header.version > JITHEADER_VERSION) {
190 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
195 if (header.flags & JITDUMP_FLAGS_RESERVED) {
196 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
197 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
201 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
202 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
207 * validate event is using the correct clockid
209 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
210 pr_err("error, jitted code must be sampled with perf record -k 1\n");
214 bs = header.total_size - sizeof(header);
217 n = realloc(buf, bs);
222 /* read extra we do not know about */
223 ret = fread(buf, bs - bsz, 1, jd->in);
228 * keep dirname for generating files and mmap records
230 strcpy(jd->dir, name);
240 static union jr_entry *
241 jit_get_next_entry(struct jit_buf_desc *jd)
243 struct jr_prefix *prefix;
252 if (jd->buf == NULL) {
253 size_t sz = getpagesize();
254 if (sz < sizeof(*prefix))
255 sz = sizeof(*prefix);
257 jd->buf = malloc(sz);
267 * file is still locked at this point
269 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
273 if (jd->needs_bswap) {
274 prefix->id = bswap_32(prefix->id);
275 prefix->total_size = bswap_32(prefix->total_size);
276 prefix->timestamp = bswap_64(prefix->timestamp);
279 size = prefix->total_size;
282 if (bs < sizeof(*prefix))
285 if (id >= JIT_CODE_MAX) {
286 pr_warning("next_entry: unknown record type %d, skipping\n", id);
288 if (bs > jd->bufsize) {
290 n = realloc(jd->buf, bs);
297 addr = ((void *)jd->buf) + sizeof(*prefix);
299 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
303 jr = (union jr_entry *)jd->buf;
306 case JIT_CODE_DEBUG_INFO:
307 if (jd->needs_bswap) {
309 jr->info.code_addr = bswap_64(jr->info.code_addr);
310 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
311 for (n = 0 ; n < jr->info.nr_entry; n++) {
312 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
313 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
314 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
318 case JIT_CODE_UNWINDING_INFO:
319 if (jd->needs_bswap) {
320 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
321 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
322 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
328 if (jd->needs_bswap) {
329 jr->load.pid = bswap_32(jr->load.pid);
330 jr->load.tid = bswap_32(jr->load.tid);
331 jr->load.vma = bswap_64(jr->load.vma);
332 jr->load.code_addr = bswap_64(jr->load.code_addr);
333 jr->load.code_size = bswap_64(jr->load.code_size);
334 jr->load.code_index= bswap_64(jr->load.code_index);
336 jd->code_load_count++;
339 if (jd->needs_bswap) {
340 jr->move.pid = bswap_32(jr->move.pid);
341 jr->move.tid = bswap_32(jr->move.tid);
342 jr->move.vma = bswap_64(jr->move.vma);
343 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
344 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
345 jr->move.code_size = bswap_64(jr->move.code_size);
346 jr->move.code_index = bswap_64(jr->move.code_index);
351 /* skip unknown record (we have read them) */
358 jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
362 size = perf_data__write(jd->output, event, event->header.size);
366 jd->bytes_written += size;
370 static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
372 struct perf_tsc_conversion tc = { .time_shift = 0, };
373 struct perf_record_time_conv *time_conv = &jd->session->time_conv;
375 if (!jd->use_arch_timestamp)
378 tc.time_shift = time_conv->time_shift;
379 tc.time_mult = time_conv->time_mult;
380 tc.time_zero = time_conv->time_zero;
383 * The event TIME_CONV was extended for the fields from "time_cycles"
384 * when supported cap_user_time_short, for backward compatibility,
385 * checks the event size and assigns these extended fields if these
386 * fields are contained in the event.
388 if (event_contains(*time_conv, time_cycles)) {
389 tc.time_cycles = time_conv->time_cycles;
390 tc.time_mask = time_conv->time_mask;
391 tc.cap_user_time_zero = time_conv->cap_user_time_zero;
392 tc.cap_user_time_short = time_conv->cap_user_time_short;
394 if (!tc.cap_user_time_zero)
398 return tsc_to_perf_time(timestamp, &tc);
401 static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
403 struct perf_sample sample;
404 union perf_event *event;
405 struct perf_tool *tool = jd->session->tool;
414 int ret, csize, usize;
423 csize = jr->load.code_size;
424 usize = jd->unwinding_mapped_size;
425 addr = jr->load.code_addr;
426 sym = (void *)((unsigned long)jr + sizeof(jr->load));
427 code = (unsigned long)jr + jr->load.p.total_size - csize;
428 count = jr->load.code_index;
429 idr_size = jd->machine->id_hdr_size;
431 event = calloc(1, sizeof(*event) + idr_size);
435 filename = event->mmap2.filename;
436 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
443 size = PERF_ALIGN(size, sizeof(u64));
444 uaddr = (uintptr_t)code;
445 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
446 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
448 if (jd->debug_data && jd->nr_debug_entries) {
449 zfree(&jd->debug_data);
450 jd->nr_debug_entries = 0;
453 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
454 zfree(&jd->unwinding_data);
455 jd->eh_frame_hdr_size = 0;
456 jd->unwinding_mapped_size = 0;
457 jd->unwinding_size = 0;
464 if (stat(filename, &st))
465 memset(&st, 0, sizeof(st));
467 event->mmap2.header.type = PERF_RECORD_MMAP2;
468 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
469 event->mmap2.header.size = (sizeof(event->mmap2) -
470 (sizeof(event->mmap2.filename) - size) + idr_size);
472 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
473 event->mmap2.start = addr;
474 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
475 event->mmap2.pid = pid;
476 event->mmap2.tid = tid;
477 event->mmap2.ino = st.st_ino;
478 event->mmap2.maj = major(st.st_dev);
479 event->mmap2.min = minor(st.st_dev);
480 event->mmap2.prot = st.st_mode;
481 event->mmap2.flags = MAP_SHARED;
482 event->mmap2.ino_generation = 1;
484 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
485 if (jd->sample_type & PERF_SAMPLE_TID) {
489 if (jd->sample_type & PERF_SAMPLE_TIME)
490 id->time = convert_timestamp(jd, jr->load.p.timestamp);
493 * create pseudo sample to induce dso hit increment
494 * use first address as sample address
496 memset(&sample, 0, sizeof(sample));
497 sample.cpumode = PERF_RECORD_MISC_USER;
500 sample.time = id->time;
503 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
507 ret = jit_inject_event(jd, event);
509 * mark dso as use to generate buildid in the header
512 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
517 static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
519 struct perf_sample sample;
520 union perf_event *event;
521 struct perf_tool *tool = jd->session->tool;
536 usize = jd->unwinding_mapped_size;
537 idr_size = jd->machine->id_hdr_size;
540 * +16 to account for sample_id_all (hack)
542 event = calloc(1, sizeof(*event) + 16);
546 filename = event->mmap2.filename;
547 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
550 jr->move.code_index);
554 if (stat(filename, &st))
555 memset(&st, 0, sizeof(st));
557 size = PERF_ALIGN(size, sizeof(u64));
559 event->mmap2.header.type = PERF_RECORD_MMAP2;
560 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
561 event->mmap2.header.size = (sizeof(event->mmap2) -
562 (sizeof(event->mmap2.filename) - size) + idr_size);
563 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
564 event->mmap2.start = jr->move.new_code_addr;
565 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
566 : jr->move.code_size;
567 event->mmap2.pid = pid;
568 event->mmap2.tid = tid;
569 event->mmap2.ino = st.st_ino;
570 event->mmap2.maj = major(st.st_dev);
571 event->mmap2.min = minor(st.st_dev);
572 event->mmap2.prot = st.st_mode;
573 event->mmap2.flags = MAP_SHARED;
574 event->mmap2.ino_generation = 1;
576 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
577 if (jd->sample_type & PERF_SAMPLE_TID) {
581 if (jd->sample_type & PERF_SAMPLE_TIME)
582 id->time = convert_timestamp(jd, jr->load.p.timestamp);
585 * create pseudo sample to induce dso hit increment
586 * use first address as sample address
588 memset(&sample, 0, sizeof(sample));
589 sample.cpumode = PERF_RECORD_MISC_USER;
592 sample.time = id->time;
593 sample.ip = jr->move.new_code_addr;
595 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
599 ret = jit_inject_event(jd, event);
601 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
606 static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
614 sz = jr->prefix.total_size - sizeof(jr->info);
619 memcpy(data, &jr->info.entries, sz);
621 jd->debug_data = data;
624 * we must use nr_entry instead of size here because
625 * we cannot distinguish actual entry from padding otherwise
627 jd->nr_debug_entries = jr->info.nr_entry;
633 jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
635 void *unwinding_data;
636 uint32_t unwinding_data_size;
641 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
642 unwinding_data = malloc(unwinding_data_size);
646 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
647 unwinding_data_size);
649 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
650 jd->unwinding_size = jr->unwinding.unwinding_size;
651 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
652 jd->unwinding_data = unwinding_data;
658 jit_process_dump(struct jit_buf_desc *jd)
663 while ((jr = jit_get_next_entry(jd))) {
664 switch(jr->prefix.id) {
666 ret = jit_repipe_code_load(jd, jr);
669 ret = jit_repipe_code_move(jd, jr);
671 case JIT_CODE_DEBUG_INFO:
672 ret = jit_repipe_debug_info(jd, jr);
674 case JIT_CODE_UNWINDING_INFO:
675 ret = jit_repipe_unwinding_info(jd, jr);
686 jit_inject(struct jit_buf_desc *jd, char *path)
691 fprintf(stderr, "injecting: %s\n", path);
693 ret = jit_open(jd, path);
697 ret = jit_process_dump(jd);
702 fprintf(stderr, "injected: %s (%d)\n", path, ret);
708 * File must be with pattern .../jit-XXXX.dump
709 * where XXXX is the PID of the process which did the mmap()
710 * as captured in the RECORD_MMAP record
713 jit_detect(char *mmap_name, pid_t pid)
720 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
724 p = strrchr(mmap_name, '/');
731 if (strncmp(p, "/jit-", 5))
740 * must be followed by a pid
745 pid2 = (int)strtol(p, &end, 10);
750 * pid does not match mmap pid
751 * pid==0 in system-wide mode (synthesized)
753 if (pid && pid2 != pid)
758 if (strcmp(end, ".dump"))
762 fprintf(stderr, "jit marker found: %s\n", mmap_name);
767 static void jit_add_pid(struct machine *machine, pid_t pid)
769 struct thread *thread = machine__findnew_thread(machine, pid, pid);
772 pr_err("%s: thread %d not found or created\n", __func__, pid);
776 thread->priv = (void *)1;
779 static bool jit_has_pid(struct machine *machine, pid_t pid)
781 struct thread *thread = machine__find_thread(machine, pid, pid);
786 return (bool)thread->priv;
790 jit_process(struct perf_session *session,
791 struct perf_data *output,
792 struct machine *machine,
798 struct jit_buf_desc jd;
802 * first, detect marker mmap (i.e., the jitdump mmap)
804 if (jit_detect(filename, pid)) {
805 // Strip //anon* mmaps if we processed a jitdump for this pid
806 if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
812 memset(&jd, 0, sizeof(jd));
814 jd.session = session;
816 jd.machine = machine;
819 * track sample_type to compute id_all layout
820 * perf sets the same sample type to all events as of now
822 first = evlist__first(session->evlist);
823 jd.sample_type = first->core.attr.sample_type;
827 ret = jit_inject(&jd, filename);
829 jit_add_pid(machine, pid);
830 *nbytes = jd.bytes_written;