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