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