1 #include <sys/sysmacros.h>
26 #include "../builtin.h"
29 struct perf_data_file *output;
30 struct perf_session *session;
31 struct machine *machine;
32 union jr_entry *entry;
37 bool needs_bswap; /* handles cross-endianess */
38 bool use_arch_timestamp;
41 uint64_t unwinding_size;
42 uint64_t unwinding_mapped_size;
43 uint64_t eh_frame_hdr_size;
44 size_t nr_debug_entries;
45 uint32_t code_load_count;
47 struct rb_root code_root;
51 struct debug_line_info {
54 /* The filename format is unspecified, absolute path, relative etc. */
55 char const filename[0];
59 struct perf_tool tool;
60 struct perf_data_file output;
61 struct perf_data_file input;
65 #define hmax(a, b) ((a) > (b) ? (a) : (b))
66 #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
69 jit_emit_elf(char *filename,
77 uint32_t unwinding_header_size,
78 uint32_t unwinding_size)
83 fprintf(stderr, "write ELF image %s\n", filename);
85 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
87 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(errno));
91 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
92 unwinding, unwinding_header_size, unwinding_size);
103 jit_close(struct jit_buf_desc *jd)
113 jit_validate_events(struct perf_session *session)
115 struct perf_evsel *evsel;
118 * check that all events use CLOCK_MONOTONIC
120 evlist__for_each_entry(session->evlist, evsel) {
121 if (evsel->attr.use_clockid == 0 || evsel->attr.clockid != CLOCK_MONOTONIC)
128 jit_open(struct jit_buf_desc *jd, const char *name)
130 struct jitheader header;
131 struct jr_prefix *prefix;
133 void *n, *buf = NULL;
134 int ret, retval = -1;
136 jd->in = fopen(name, "r");
140 bsz = hmax(sizeof(header), sizeof(*prefix));
147 * protect from writer modifying the file while we are reading it
151 ret = fread(buf, sizeof(header), 1, jd->in);
155 memcpy(&header, buf, sizeof(header));
157 if (header.magic != JITHEADER_MAGIC) {
158 if (header.magic != JITHEADER_MAGIC_SW)
160 jd->needs_bswap = true;
163 if (jd->needs_bswap) {
164 header.version = bswap_32(header.version);
165 header.total_size = bswap_32(header.total_size);
166 header.pid = bswap_32(header.pid);
167 header.elf_mach = bswap_32(header.elf_mach);
168 header.timestamp = bswap_64(header.timestamp);
169 header.flags = bswap_64(header.flags);
172 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
175 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
178 (unsigned long long)header.timestamp,
181 jd->use_arch_timestamp);
183 if (header.version > JITHEADER_VERSION) {
184 pr_err("wrong jitdump version %u, expected " STR(JITHEADER_VERSION),
189 if (header.flags & JITDUMP_FLAGS_RESERVED) {
190 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
191 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
195 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
196 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
201 * validate event is using the correct clockid
203 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
204 pr_err("error, jitted code must be sampled with perf record -k 1\n");
208 bs = header.total_size - sizeof(header);
211 n = realloc(buf, bs);
216 /* read extra we do not know about */
217 ret = fread(buf, bs - bsz, 1, jd->in);
222 * keep dirname for generating files and mmap records
224 strcpy(jd->dir, name);
234 static union jr_entry *
235 jit_get_next_entry(struct jit_buf_desc *jd)
237 struct jr_prefix *prefix;
246 if (jd->buf == NULL) {
247 size_t sz = getpagesize();
248 if (sz < sizeof(*prefix))
249 sz = sizeof(*prefix);
251 jd->buf = malloc(sz);
261 * file is still locked at this point
263 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
267 if (jd->needs_bswap) {
268 prefix->id = bswap_32(prefix->id);
269 prefix->total_size = bswap_32(prefix->total_size);
270 prefix->timestamp = bswap_64(prefix->timestamp);
273 size = prefix->total_size;
276 if (bs < sizeof(*prefix))
279 if (id >= JIT_CODE_MAX) {
280 pr_warning("next_entry: unknown record type %d, skipping\n", id);
282 if (bs > jd->bufsize) {
284 n = realloc(jd->buf, bs);
291 addr = ((void *)jd->buf) + sizeof(*prefix);
293 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
297 jr = (union jr_entry *)jd->buf;
300 case JIT_CODE_DEBUG_INFO:
301 if (jd->needs_bswap) {
303 jr->info.code_addr = bswap_64(jr->info.code_addr);
304 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
305 for (n = 0 ; n < jr->info.nr_entry; n++) {
306 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
307 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
308 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
312 case JIT_CODE_UNWINDING_INFO:
313 if (jd->needs_bswap) {
314 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
315 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
316 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
322 if (jd->needs_bswap) {
323 jr->load.pid = bswap_32(jr->load.pid);
324 jr->load.tid = bswap_32(jr->load.tid);
325 jr->load.vma = bswap_64(jr->load.vma);
326 jr->load.code_addr = bswap_64(jr->load.code_addr);
327 jr->load.code_size = bswap_64(jr->load.code_size);
328 jr->load.code_index= bswap_64(jr->load.code_index);
330 jd->code_load_count++;
333 if (jd->needs_bswap) {
334 jr->move.pid = bswap_32(jr->move.pid);
335 jr->move.tid = bswap_32(jr->move.tid);
336 jr->move.vma = bswap_64(jr->move.vma);
337 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
338 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
339 jr->move.code_size = bswap_64(jr->move.code_size);
340 jr->move.code_index = bswap_64(jr->move.code_index);
345 /* skip unknown record (we have read them) */
352 jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
356 size = perf_data_file__write(jd->output, event, event->header.size);
360 jd->bytes_written += size;
364 static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
366 struct perf_tsc_conversion tc;
368 if (!jd->use_arch_timestamp)
371 tc.time_shift = jd->session->time_conv.time_shift;
372 tc.time_mult = jd->session->time_conv.time_mult;
373 tc.time_zero = jd->session->time_conv.time_zero;
378 return tsc_to_perf_time(timestamp, &tc);
381 static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
383 struct perf_sample sample;
384 union perf_event *event;
385 struct perf_tool *tool = jd->session->tool;
394 int ret, csize, usize;
403 csize = jr->load.code_size;
404 usize = jd->unwinding_mapped_size;
405 addr = jr->load.code_addr;
406 sym = (void *)((unsigned long)jr + sizeof(jr->load));
407 code = (unsigned long)jr + jr->load.p.total_size - csize;
408 count = jr->load.code_index;
409 idr_size = jd->machine->id_hdr_size;
411 event = calloc(1, sizeof(*event) + idr_size);
415 filename = event->mmap2.filename;
416 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%u.so",
423 size = PERF_ALIGN(size, sizeof(u64));
424 uaddr = (uintptr_t)code;
425 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
426 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
428 if (jd->debug_data && jd->nr_debug_entries) {
429 free(jd->debug_data);
430 jd->debug_data = NULL;
431 jd->nr_debug_entries = 0;
434 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
435 free(jd->unwinding_data);
436 jd->unwinding_data = NULL;
437 jd->eh_frame_hdr_size = 0;
438 jd->unwinding_mapped_size = 0;
439 jd->unwinding_size = 0;
446 if (stat(filename, &st))
447 memset(&st, 0, sizeof(st));
449 event->mmap2.header.type = PERF_RECORD_MMAP2;
450 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
451 event->mmap2.header.size = (sizeof(event->mmap2) -
452 (sizeof(event->mmap2.filename) - size) + idr_size);
454 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
455 event->mmap2.start = addr;
456 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
457 event->mmap2.pid = pid;
458 event->mmap2.tid = tid;
459 event->mmap2.ino = st.st_ino;
460 event->mmap2.maj = major(st.st_dev);
461 event->mmap2.min = minor(st.st_dev);
462 event->mmap2.prot = st.st_mode;
463 event->mmap2.flags = MAP_SHARED;
464 event->mmap2.ino_generation = 1;
466 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
467 if (jd->sample_type & PERF_SAMPLE_TID) {
471 if (jd->sample_type & PERF_SAMPLE_TIME)
472 id->time = convert_timestamp(jd, jr->load.p.timestamp);
475 * create pseudo sample to induce dso hit increment
476 * use first address as sample address
478 memset(&sample, 0, sizeof(sample));
479 sample.cpumode = PERF_RECORD_MISC_USER;
482 sample.time = id->time;
485 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
489 ret = jit_inject_event(jd, event);
491 * mark dso as use to generate buildid in the header
494 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
499 static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
501 struct perf_sample sample;
502 union perf_event *event;
503 struct perf_tool *tool = jd->session->tool;
518 usize = jd->unwinding_mapped_size;
519 idr_size = jd->machine->id_hdr_size;
522 * +16 to account for sample_id_all (hack)
524 event = calloc(1, sizeof(*event) + 16);
528 filename = event->mmap2.filename;
529 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%"PRIu64,
532 jr->move.code_index);
536 if (stat(filename, &st))
537 memset(&st, 0, sizeof(st));
539 size = PERF_ALIGN(size, sizeof(u64));
541 event->mmap2.header.type = PERF_RECORD_MMAP2;
542 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
543 event->mmap2.header.size = (sizeof(event->mmap2) -
544 (sizeof(event->mmap2.filename) - size) + idr_size);
545 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
546 event->mmap2.start = jr->move.new_code_addr;
547 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
548 : jr->move.code_size;
549 event->mmap2.pid = pid;
550 event->mmap2.tid = tid;
551 event->mmap2.ino = st.st_ino;
552 event->mmap2.maj = major(st.st_dev);
553 event->mmap2.min = minor(st.st_dev);
554 event->mmap2.prot = st.st_mode;
555 event->mmap2.flags = MAP_SHARED;
556 event->mmap2.ino_generation = 1;
558 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
559 if (jd->sample_type & PERF_SAMPLE_TID) {
563 if (jd->sample_type & PERF_SAMPLE_TIME)
564 id->time = convert_timestamp(jd, jr->load.p.timestamp);
567 * create pseudo sample to induce dso hit increment
568 * use first address as sample address
570 memset(&sample, 0, sizeof(sample));
571 sample.cpumode = PERF_RECORD_MISC_USER;
574 sample.time = id->time;
575 sample.ip = jr->move.new_code_addr;
577 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
581 ret = jit_inject_event(jd, event);
583 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
588 static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
596 sz = jr->prefix.total_size - sizeof(jr->info);
601 memcpy(data, &jr->info.entries, sz);
603 jd->debug_data = data;
606 * we must use nr_entry instead of size here because
607 * we cannot distinguish actual entry from padding otherwise
609 jd->nr_debug_entries = jr->info.nr_entry;
615 jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
617 void *unwinding_data;
618 uint32_t unwinding_data_size;
623 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
624 unwinding_data = malloc(unwinding_data_size);
628 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
629 unwinding_data_size);
631 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
632 jd->unwinding_size = jr->unwinding.unwinding_size;
633 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
634 jd->unwinding_data = unwinding_data;
640 jit_process_dump(struct jit_buf_desc *jd)
645 while ((jr = jit_get_next_entry(jd))) {
646 switch(jr->prefix.id) {
648 ret = jit_repipe_code_load(jd, jr);
651 ret = jit_repipe_code_move(jd, jr);
653 case JIT_CODE_DEBUG_INFO:
654 ret = jit_repipe_debug_info(jd, jr);
656 case JIT_CODE_UNWINDING_INFO:
657 ret = jit_repipe_unwinding_info(jd, jr);
668 jit_inject(struct jit_buf_desc *jd, char *path)
673 fprintf(stderr, "injecting: %s\n", path);
675 ret = jit_open(jd, path);
679 ret = jit_process_dump(jd);
684 fprintf(stderr, "injected: %s (%d)\n", path, ret);
690 * File must be with pattern .../jit-XXXX.dump
691 * where XXXX is the PID of the process which did the mmap()
692 * as captured in the RECORD_MMAP record
695 jit_detect(char *mmap_name, pid_t pid)
702 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
706 p = strrchr(mmap_name, '/');
713 if (strncmp(p, "/jit-", 5))
722 * must be followed by a pid
727 pid2 = (int)strtol(p, &end, 10);
732 * pid does not match mmap pid
733 * pid==0 in system-wide mode (synthesized)
735 if (pid && pid2 != pid)
740 if (strcmp(end, ".dump"))
744 fprintf(stderr, "jit marker found: %s\n", mmap_name);
750 jit_process(struct perf_session *session,
751 struct perf_data_file *output,
752 struct machine *machine,
757 struct perf_evsel *first;
758 struct jit_buf_desc jd;
762 * first, detect marker mmap (i.e., the jitdump mmap)
764 if (jit_detect(filename, pid))
767 memset(&jd, 0, sizeof(jd));
769 jd.session = session;
771 jd.machine = machine;
774 * track sample_type to compute id_all layout
775 * perf sets the same sample type to all events as of now
777 first = perf_evlist__first(session->evlist);
778 jd.sample_type = first->attr.sample_type;
782 ret = jit_inject(&jd, filename);
784 *nbytes = jd.bytes_written;