1 #include <sys/sysmacros.h>
12 #include <linux/stringify.h>
27 #include "../builtin.h"
30 struct perf_data_file *output;
31 struct perf_session *session;
32 struct machine *machine;
33 union jr_entry *entry;
38 bool needs_bswap; /* handles cross-endianess */
39 bool use_arch_timestamp;
42 uint64_t unwinding_size;
43 uint64_t unwinding_mapped_size;
44 uint64_t eh_frame_hdr_size;
45 size_t nr_debug_entries;
46 uint32_t code_load_count;
48 struct rb_root code_root;
52 struct debug_line_info {
55 /* The filename format is unspecified, absolute path, relative etc. */
56 char const filename[0];
60 struct perf_tool tool;
61 struct perf_data_file output;
62 struct perf_data_file input;
66 #define hmax(a, b) ((a) > (b) ? (a) : (b))
67 #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
70 jit_emit_elf(char *filename,
78 uint32_t unwinding_header_size,
79 uint32_t unwinding_size)
84 fprintf(stderr, "write ELF image %s\n", filename);
86 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
88 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(errno));
92 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
93 unwinding, unwinding_header_size, unwinding_size);
104 jit_close(struct jit_buf_desc *jd)
114 jit_validate_events(struct perf_session *session)
116 struct perf_evsel *evsel;
119 * check that all events use CLOCK_MONOTONIC
121 evlist__for_each_entry(session->evlist, evsel) {
122 if (evsel->attr.use_clockid == 0 || evsel->attr.clockid != CLOCK_MONOTONIC)
129 jit_open(struct jit_buf_desc *jd, const char *name)
131 struct jitheader header;
132 struct jr_prefix *prefix;
134 void *n, *buf = NULL;
135 int ret, retval = -1;
137 jd->in = fopen(name, "r");
141 bsz = hmax(sizeof(header), sizeof(*prefix));
148 * protect from writer modifying the file while we are reading it
152 ret = fread(buf, sizeof(header), 1, jd->in);
156 memcpy(&header, buf, sizeof(header));
158 if (header.magic != JITHEADER_MAGIC) {
159 if (header.magic != JITHEADER_MAGIC_SW)
161 jd->needs_bswap = true;
164 if (jd->needs_bswap) {
165 header.version = bswap_32(header.version);
166 header.total_size = bswap_32(header.total_size);
167 header.pid = bswap_32(header.pid);
168 header.elf_mach = bswap_32(header.elf_mach);
169 header.timestamp = bswap_64(header.timestamp);
170 header.flags = bswap_64(header.flags);
173 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
176 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
179 (unsigned long long)header.timestamp,
182 jd->use_arch_timestamp);
184 if (header.version > JITHEADER_VERSION) {
185 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
190 if (header.flags & JITDUMP_FLAGS_RESERVED) {
191 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
192 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
196 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
197 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
202 * validate event is using the correct clockid
204 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
205 pr_err("error, jitted code must be sampled with perf record -k 1\n");
209 bs = header.total_size - sizeof(header);
212 n = realloc(buf, bs);
217 /* read extra we do not know about */
218 ret = fread(buf, bs - bsz, 1, jd->in);
223 * keep dirname for generating files and mmap records
225 strcpy(jd->dir, name);
235 static union jr_entry *
236 jit_get_next_entry(struct jit_buf_desc *jd)
238 struct jr_prefix *prefix;
247 if (jd->buf == NULL) {
248 size_t sz = getpagesize();
249 if (sz < sizeof(*prefix))
250 sz = sizeof(*prefix);
252 jd->buf = malloc(sz);
262 * file is still locked at this point
264 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
268 if (jd->needs_bswap) {
269 prefix->id = bswap_32(prefix->id);
270 prefix->total_size = bswap_32(prefix->total_size);
271 prefix->timestamp = bswap_64(prefix->timestamp);
274 size = prefix->total_size;
277 if (bs < sizeof(*prefix))
280 if (id >= JIT_CODE_MAX) {
281 pr_warning("next_entry: unknown record type %d, skipping\n", id);
283 if (bs > jd->bufsize) {
285 n = realloc(jd->buf, bs);
292 addr = ((void *)jd->buf) + sizeof(*prefix);
294 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
298 jr = (union jr_entry *)jd->buf;
301 case JIT_CODE_DEBUG_INFO:
302 if (jd->needs_bswap) {
304 jr->info.code_addr = bswap_64(jr->info.code_addr);
305 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
306 for (n = 0 ; n < jr->info.nr_entry; n++) {
307 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
308 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
309 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
313 case JIT_CODE_UNWINDING_INFO:
314 if (jd->needs_bswap) {
315 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
316 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
317 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
323 if (jd->needs_bswap) {
324 jr->load.pid = bswap_32(jr->load.pid);
325 jr->load.tid = bswap_32(jr->load.tid);
326 jr->load.vma = bswap_64(jr->load.vma);
327 jr->load.code_addr = bswap_64(jr->load.code_addr);
328 jr->load.code_size = bswap_64(jr->load.code_size);
329 jr->load.code_index= bswap_64(jr->load.code_index);
331 jd->code_load_count++;
334 if (jd->needs_bswap) {
335 jr->move.pid = bswap_32(jr->move.pid);
336 jr->move.tid = bswap_32(jr->move.tid);
337 jr->move.vma = bswap_64(jr->move.vma);
338 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
339 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
340 jr->move.code_size = bswap_64(jr->move.code_size);
341 jr->move.code_index = bswap_64(jr->move.code_index);
346 /* skip unknown record (we have read them) */
353 jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
357 size = perf_data_file__write(jd->output, event, event->header.size);
361 jd->bytes_written += size;
365 static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
367 struct perf_tsc_conversion tc;
369 if (!jd->use_arch_timestamp)
372 tc.time_shift = jd->session->time_conv.time_shift;
373 tc.time_mult = jd->session->time_conv.time_mult;
374 tc.time_zero = jd->session->time_conv.time_zero;
379 return tsc_to_perf_time(timestamp, &tc);
382 static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
384 struct perf_sample sample;
385 union perf_event *event;
386 struct perf_tool *tool = jd->session->tool;
395 int ret, csize, usize;
404 csize = jr->load.code_size;
405 usize = jd->unwinding_mapped_size;
406 addr = jr->load.code_addr;
407 sym = (void *)((unsigned long)jr + sizeof(jr->load));
408 code = (unsigned long)jr + jr->load.p.total_size - csize;
409 count = jr->load.code_index;
410 idr_size = jd->machine->id_hdr_size;
412 event = calloc(1, sizeof(*event) + idr_size);
416 filename = event->mmap2.filename;
417 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%u.so",
424 size = PERF_ALIGN(size, sizeof(u64));
425 uaddr = (uintptr_t)code;
426 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
427 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
429 if (jd->debug_data && jd->nr_debug_entries) {
430 free(jd->debug_data);
431 jd->debug_data = NULL;
432 jd->nr_debug_entries = 0;
435 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
436 free(jd->unwinding_data);
437 jd->unwinding_data = NULL;
438 jd->eh_frame_hdr_size = 0;
439 jd->unwinding_mapped_size = 0;
440 jd->unwinding_size = 0;
447 if (stat(filename, &st))
448 memset(&st, 0, sizeof(st));
450 event->mmap2.header.type = PERF_RECORD_MMAP2;
451 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
452 event->mmap2.header.size = (sizeof(event->mmap2) -
453 (sizeof(event->mmap2.filename) - size) + idr_size);
455 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
456 event->mmap2.start = addr;
457 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
458 event->mmap2.pid = pid;
459 event->mmap2.tid = tid;
460 event->mmap2.ino = st.st_ino;
461 event->mmap2.maj = major(st.st_dev);
462 event->mmap2.min = minor(st.st_dev);
463 event->mmap2.prot = st.st_mode;
464 event->mmap2.flags = MAP_SHARED;
465 event->mmap2.ino_generation = 1;
467 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
468 if (jd->sample_type & PERF_SAMPLE_TID) {
472 if (jd->sample_type & PERF_SAMPLE_TIME)
473 id->time = convert_timestamp(jd, jr->load.p.timestamp);
476 * create pseudo sample to induce dso hit increment
477 * use first address as sample address
479 memset(&sample, 0, sizeof(sample));
480 sample.cpumode = PERF_RECORD_MISC_USER;
483 sample.time = id->time;
486 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
490 ret = jit_inject_event(jd, event);
492 * mark dso as use to generate buildid in the header
495 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
500 static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
502 struct perf_sample sample;
503 union perf_event *event;
504 struct perf_tool *tool = jd->session->tool;
519 usize = jd->unwinding_mapped_size;
520 idr_size = jd->machine->id_hdr_size;
523 * +16 to account for sample_id_all (hack)
525 event = calloc(1, sizeof(*event) + 16);
529 filename = event->mmap2.filename;
530 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%"PRIu64,
533 jr->move.code_index);
537 if (stat(filename, &st))
538 memset(&st, 0, sizeof(st));
540 size = PERF_ALIGN(size, sizeof(u64));
542 event->mmap2.header.type = PERF_RECORD_MMAP2;
543 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
544 event->mmap2.header.size = (sizeof(event->mmap2) -
545 (sizeof(event->mmap2.filename) - size) + idr_size);
546 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
547 event->mmap2.start = jr->move.new_code_addr;
548 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
549 : jr->move.code_size;
550 event->mmap2.pid = pid;
551 event->mmap2.tid = tid;
552 event->mmap2.ino = st.st_ino;
553 event->mmap2.maj = major(st.st_dev);
554 event->mmap2.min = minor(st.st_dev);
555 event->mmap2.prot = st.st_mode;
556 event->mmap2.flags = MAP_SHARED;
557 event->mmap2.ino_generation = 1;
559 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
560 if (jd->sample_type & PERF_SAMPLE_TID) {
564 if (jd->sample_type & PERF_SAMPLE_TIME)
565 id->time = convert_timestamp(jd, jr->load.p.timestamp);
568 * create pseudo sample to induce dso hit increment
569 * use first address as sample address
571 memset(&sample, 0, sizeof(sample));
572 sample.cpumode = PERF_RECORD_MISC_USER;
575 sample.time = id->time;
576 sample.ip = jr->move.new_code_addr;
578 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
582 ret = jit_inject_event(jd, event);
584 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
589 static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
597 sz = jr->prefix.total_size - sizeof(jr->info);
602 memcpy(data, &jr->info.entries, sz);
604 jd->debug_data = data;
607 * we must use nr_entry instead of size here because
608 * we cannot distinguish actual entry from padding otherwise
610 jd->nr_debug_entries = jr->info.nr_entry;
616 jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
618 void *unwinding_data;
619 uint32_t unwinding_data_size;
624 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
625 unwinding_data = malloc(unwinding_data_size);
629 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
630 unwinding_data_size);
632 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
633 jd->unwinding_size = jr->unwinding.unwinding_size;
634 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
635 jd->unwinding_data = unwinding_data;
641 jit_process_dump(struct jit_buf_desc *jd)
646 while ((jr = jit_get_next_entry(jd))) {
647 switch(jr->prefix.id) {
649 ret = jit_repipe_code_load(jd, jr);
652 ret = jit_repipe_code_move(jd, jr);
654 case JIT_CODE_DEBUG_INFO:
655 ret = jit_repipe_debug_info(jd, jr);
657 case JIT_CODE_UNWINDING_INFO:
658 ret = jit_repipe_unwinding_info(jd, jr);
669 jit_inject(struct jit_buf_desc *jd, char *path)
674 fprintf(stderr, "injecting: %s\n", path);
676 ret = jit_open(jd, path);
680 ret = jit_process_dump(jd);
685 fprintf(stderr, "injected: %s (%d)\n", path, ret);
691 * File must be with pattern .../jit-XXXX.dump
692 * where XXXX is the PID of the process which did the mmap()
693 * as captured in the RECORD_MMAP record
696 jit_detect(char *mmap_name, pid_t pid)
703 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
707 p = strrchr(mmap_name, '/');
714 if (strncmp(p, "/jit-", 5))
723 * must be followed by a pid
728 pid2 = (int)strtol(p, &end, 10);
733 * pid does not match mmap pid
734 * pid==0 in system-wide mode (synthesized)
736 if (pid && pid2 != pid)
741 if (strcmp(end, ".dump"))
745 fprintf(stderr, "jit marker found: %s\n", mmap_name);
751 jit_process(struct perf_session *session,
752 struct perf_data_file *output,
753 struct machine *machine,
758 struct perf_evsel *first;
759 struct jit_buf_desc jd;
763 * first, detect marker mmap (i.e., the jitdump mmap)
765 if (jit_detect(filename, pid))
768 memset(&jd, 0, sizeof(jd));
770 jd.session = session;
772 jd.machine = machine;
775 * track sample_type to compute id_all layout
776 * perf sets the same sample type to all events as of now
778 first = perf_evlist__first(session->evlist);
779 jd.sample_type = first->attr.sample_type;
783 ret = jit_inject(&jd, filename);
785 *nbytes = jd.bytes_written;