jitdump.c 19.5 KB
Newer Older
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 7 8 9 10 11 12 13 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <byteswap.h>
#include <sys/stat.h>
#include <sys/mman.h>
15
#include <linux/stringify.h>
16

17
#include "build-id.h"
18 19 20
#include "event.h"
#include "debug.h"
#include "evlist.h"
21
#include "namespaces.h"
22 23 24
#include "symbol.h"
#include <elf.h>

25
#include "tsc.h"
26 27 28 29
#include "session.h"
#include "jit.h"
#include "jitdump.h"
#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 38
	struct perf_session *session;
	struct machine *machine;
39
	struct nsinfo  *nsi;
40 41 42 43 44
	union jr_entry   *entry;
	void             *buf;
	uint64_t	 sample_type;
	size_t           bufsize;
	FILE             *in;
45
	bool		 needs_bswap; /* handles cross-endianness */
46
	bool		 use_arch_timestamp;
47
	void		 *debug_data;
48 49 50 51
	void		 *unwinding_data;
	uint64_t	 unwinding_size;
	uint64_t	 unwinding_mapped_size;
	uint64_t         eh_frame_hdr_size;
52 53 54 55 56 57 58 59 60 61 62
	size_t		 nr_debug_entries;
	uint32_t         code_load_count;
	u64		 bytes_written;
	struct rb_root   code_root;
	char		 dir[PATH_MAX];
};

struct debug_line_info {
	unsigned long vma;
	unsigned int lineno;
	/* The filename format is unspecified, absolute path, relative etc. */
63
	char const filename[];
64 65 66 67
};

struct jit_tool {
	struct perf_tool tool;
68 69
	struct perf_data	output;
	struct perf_data	input;
70 71 72 73 74 75 76
	u64 bytes_written;
};

#define hmax(a, b) ((a) > (b) ? (a) : (b))
#define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))

static int
77 78
jit_emit_elf(struct jit_buf_desc *jd,
	     char *filename,
79 80 81
	     const char *sym,
	     uint64_t code_addr,
	     const void *code,
82 83
	     int csize,
	     void *debug,
84 85 86 87
	     int nr_debug_entries,
	     void *unwinding,
	     uint32_t unwinding_header_size,
	     uint32_t unwinding_size)
88
{
89 90
	int ret, fd, saved_errno;
	struct nscookie nsc;
91 92 93 94

	if (verbose > 0)
		fprintf(stderr, "write ELF image %s\n", filename);

95
	nsinfo__mountns_enter(jd->nsi, &nsc);
96
	fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
97 98
	saved_errno = errno;
	nsinfo__mountns_exit(&nsc);
99
	if (fd == -1) {
100
		pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(saved_errno));
101 102 103
		return -1;
	}

104 105
	ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
			    unwinding, unwinding_header_size, unwinding_size);
106 107 108

        close(fd);

109 110 111 112 113
	if (ret) {
		nsinfo__mountns_enter(jd->nsi, &nsc);
		unlink(filename);
		nsinfo__mountns_exit(&nsc);
	}
114 115 116 117 118 119 120 121 122 123 124 125 126 127

	return ret;
}

static void
jit_close(struct jit_buf_desc *jd)
{
	if (!(jd && jd->in))
		return;
	funlockfile(jd->in);
	fclose(jd->in);
	jd->in = NULL;
}

128 129 130
static int
jit_validate_events(struct perf_session *session)
{
131
	struct evsel *evsel;
132 133 134 135

	/*
	 * check that all events use CLOCK_MONOTONIC
	 */
136
	evlist__for_each_entry(session->evlist, evsel) {
137
		if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
138 139 140 141 142
			return -1;
	}
	return 0;
}

143 144 145 146
static int
jit_open(struct jit_buf_desc *jd, const char *name)
{
	struct jitheader header;
147
	struct nscookie nsc;
148 149 150 151 152
	struct jr_prefix *prefix;
	ssize_t bs, bsz = 0;
	void *n, *buf = NULL;
	int ret, retval = -1;

153
	nsinfo__mountns_enter(jd->nsi, &nsc);
154
	jd->in = fopen(name, "r");
155
	nsinfo__mountns_exit(&nsc);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	if (!jd->in)
		return -1;

	bsz = hmax(sizeof(header), sizeof(*prefix));

	buf = malloc(bsz);
	if (!buf)
		goto error;

	/*
	 * protect from writer modifying the file while we are reading it
	 */
	flockfile(jd->in);

	ret = fread(buf, sizeof(header), 1, jd->in);
	if (ret != 1)
		goto error;

	memcpy(&header, buf, sizeof(header));

	if (header.magic != JITHEADER_MAGIC) {
		if (header.magic != JITHEADER_MAGIC_SW)
			goto error;
		jd->needs_bswap = true;
	}

	if (jd->needs_bswap) {
		header.version    = bswap_32(header.version);
		header.total_size = bswap_32(header.total_size);
		header.pid	  = bswap_32(header.pid);
		header.elf_mach   = bswap_32(header.elf_mach);
		header.timestamp  = bswap_64(header.timestamp);
		header.flags      = bswap_64(header.flags);
	}

191 192
	jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;

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 196 197 198
			header.version,
			header.total_size,
			(unsigned long long)header.timestamp,
			header.pid,
199 200
			header.elf_mach,
			jd->use_arch_timestamp);
201

202
	if (header.version > JITHEADER_VERSION) {
203
		pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
204 205 206 207
			header.version);
		goto error;
	}

208 209 210 211 212 213
	if (header.flags & JITDUMP_FLAGS_RESERVED) {
		pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
		       (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
		goto error;
	}

214 215 216 217 218
	if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
		pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
		goto error;
	}

219 220 221
	/*
	 * validate event is using the correct clockid
	 */
222
	if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
223 224 225 226
		pr_err("error, jitted code must be sampled with perf record -k 1\n");
		goto error;
	}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	bs = header.total_size - sizeof(header);

	if (bs > bsz) {
		n = realloc(buf, bs);
		if (!n)
			goto error;
		bsz = bs;
		buf = n;
		/* read extra we do not know about */
		ret = fread(buf, bs - bsz, 1, jd->in);
		if (ret != 1)
			goto error;
	}
	/*
	 * keep dirname for generating files and mmap records
	 */
	strcpy(jd->dir, name);
	dirname(jd->dir);

	return 0;
error:
	funlockfile(jd->in);
	fclose(jd->in);
	return retval;
}

static union jr_entry *
jit_get_next_entry(struct jit_buf_desc *jd)
{
	struct jr_prefix *prefix;
	union jr_entry *jr;
	void *addr;
	size_t bs, size;
	int id, ret;

	if (!(jd && jd->in))
		return NULL;

	if (jd->buf == NULL) {
		size_t sz = getpagesize();
		if (sz < sizeof(*prefix))
			sz = sizeof(*prefix);

		jd->buf = malloc(sz);
		if (jd->buf == NULL)
			return NULL;

		jd->bufsize = sz;
	}

	prefix = jd->buf;

	/*
	 * file is still locked at this point
	 */
	ret = fread(prefix, sizeof(*prefix), 1, jd->in);
	if (ret  != 1)
		return NULL;

	if (jd->needs_bswap) {
		prefix->id   	   = bswap_32(prefix->id);
		prefix->total_size = bswap_32(prefix->total_size);
		prefix->timestamp  = bswap_64(prefix->timestamp);
	}
	id   = prefix->id;
	size = prefix->total_size;

	bs = (size_t)size;
	if (bs < sizeof(*prefix))
		return NULL;

	if (id >= JIT_CODE_MAX) {
299
		pr_warning("next_entry: unknown record type %d, skipping\n", id);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	}
	if (bs > jd->bufsize) {
		void *n;
		n = realloc(jd->buf, bs);
		if (!n)
			return NULL;
		jd->buf = n;
		jd->bufsize = bs;
	}

	addr = ((void *)jd->buf) + sizeof(*prefix);

	ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
	if (ret != 1)
		return NULL;

	jr = (union jr_entry *)jd->buf;

	switch(id) {
	case JIT_CODE_DEBUG_INFO:
		if (jd->needs_bswap) {
			uint64_t n;
			jr->info.code_addr = bswap_64(jr->info.code_addr);
			jr->info.nr_entry  = bswap_64(jr->info.nr_entry);
			for (n = 0 ; n < jr->info.nr_entry; n++) {
				jr->info.entries[n].addr    = bswap_64(jr->info.entries[n].addr);
				jr->info.entries[n].lineno  = bswap_32(jr->info.entries[n].lineno);
				jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
			}
		}
		break;
331 332 333 334 335 336 337
	case JIT_CODE_UNWINDING_INFO:
		if (jd->needs_bswap) {
			jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
			jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
			jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
		}
		break;
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	case JIT_CODE_CLOSE:
		break;
	case JIT_CODE_LOAD:
		if (jd->needs_bswap) {
			jr->load.pid       = bswap_32(jr->load.pid);
			jr->load.tid       = bswap_32(jr->load.tid);
			jr->load.vma       = bswap_64(jr->load.vma);
			jr->load.code_addr = bswap_64(jr->load.code_addr);
			jr->load.code_size = bswap_64(jr->load.code_size);
			jr->load.code_index= bswap_64(jr->load.code_index);
		}
		jd->code_load_count++;
		break;
	case JIT_CODE_MOVE:
		if (jd->needs_bswap) {
			jr->move.pid           = bswap_32(jr->move.pid);
			jr->move.tid           = bswap_32(jr->move.tid);
			jr->move.vma           = bswap_64(jr->move.vma);
			jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
			jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
			jr->move.code_size     = bswap_64(jr->move.code_size);
			jr->move.code_index    = bswap_64(jr->move.code_index);
		}
		break;
	case JIT_CODE_MAX:
	default:
364 365
		/* skip unknown record (we have read them) */
		break;
366 367 368 369 370 371 372 373 374
	}
	return jr;
}

static int
jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
{
	ssize_t size;

375
	size = perf_data__write(jd->output, event, event->header.size);
376 377 378 379 380 381 382
	if (size < 0)
		return -1;

	jd->bytes_written += size;
	return 0;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396
static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr)
{
	if (jd->nsi && jd->nsi->in_pidns)
		return jd->nsi->tgid;
	return jr->load.pid;
}

static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr)
{
	if (jd->nsi && jd->nsi->in_pidns)
		return jd->nsi->pid;
	return jr->load.tid;
}

397 398 399 400 401 402 403
static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
{
	struct perf_tsc_conversion tc;

	if (!jd->use_arch_timestamp)
		return timestamp;

404 405 406 407 408 409 410 411 412
	tc.time_shift	       = jd->session->time_conv.time_shift;
	tc.time_mult	       = jd->session->time_conv.time_mult;
	tc.time_zero	       = jd->session->time_conv.time_zero;
	tc.time_cycles	       = jd->session->time_conv.time_cycles;
	tc.time_mask	       = jd->session->time_conv.time_mask;
	tc.cap_user_time_zero  = jd->session->time_conv.cap_user_time_zero;
	tc.cap_user_time_short = jd->session->time_conv.cap_user_time_short;

	if (!tc.cap_user_time_zero)
413 414 415 416 417
		return 0;

	return tsc_to_perf_time(timestamp, &tc);
}

418 419 420 421 422 423 424 425 426 427 428 429
static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
{
	struct perf_sample sample;
	union perf_event *event;
	struct perf_tool *tool = jd->session->tool;
	uint64_t code, addr;
	uintptr_t uaddr;
	char *filename;
	struct stat st;
	size_t size;
	u16 idr_size;
	const char *sym;
430
	uint64_t count;
431
	int ret, csize, usize;
432
	pid_t nspid, pid, tid;
433 434 435 436 437
	struct {
		u32 pid, tid;
		u64 time;
	} *id;

438 439 440
	nspid = jr->load.pid;
	pid   = jr_entry_pid(jd, jr);
	tid   = jr_entry_tid(jd, jr);
441
	csize = jr->load.code_size;
442
	usize = jd->unwinding_mapped_size;
443 444 445 446 447 448 449 450 451 452 453
	addr  = jr->load.code_addr;
	sym   = (void *)((unsigned long)jr + sizeof(jr->load));
	code  = (unsigned long)jr + jr->load.p.total_size - csize;
	count = jr->load.code_index;
	idr_size = jd->machine->id_hdr_size;

	event = calloc(1, sizeof(*event) + idr_size);
	if (!event)
		return -1;

	filename = event->mmap2.filename;
454
	size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
455
			jd->dir,
456
			nspid,
457 458 459 460 461 462
			count);

	size++; /* for \0 */

	size = PERF_ALIGN(size, sizeof(u64));
	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 469 470
		jd->nr_debug_entries = 0;
	}

471
	if (jd->unwinding_data && jd->eh_frame_hdr_size) {
472
		zfree(&jd->unwinding_data);
473 474 475 476 477
		jd->eh_frame_hdr_size = 0;
		jd->unwinding_mapped_size = 0;
		jd->unwinding_size = 0;
	}

478 479 480 481
	if (ret) {
		free(event);
		return -1;
	}
482
	if (nsinfo__stat(filename, &st, jd->nsi))
483
		memset(&st, 0, sizeof(st));
484 485 486 487 488 489 490 491

	event->mmap2.header.type = PERF_RECORD_MMAP2;
	event->mmap2.header.misc = PERF_RECORD_MISC_USER;
	event->mmap2.header.size = (sizeof(event->mmap2) -
			(sizeof(event->mmap2.filename) - size) + idr_size);

	event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
	event->mmap2.start = addr;
492
	event->mmap2.len   = usize ? ALIGN_8(csize) + usize : csize;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	event->mmap2.pid   = pid;
	event->mmap2.tid   = tid;
	event->mmap2.ino   = st.st_ino;
	event->mmap2.maj   = major(st.st_dev);
	event->mmap2.min   = minor(st.st_dev);
	event->mmap2.prot  = st.st_mode;
	event->mmap2.flags = MAP_SHARED;
	event->mmap2.ino_generation = 1;

	id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
	if (jd->sample_type & PERF_SAMPLE_TID) {
		id->pid  = pid;
		id->tid  = tid;
	}
	if (jd->sample_type & PERF_SAMPLE_TIME)
508
		id->time = convert_timestamp(jd, jr->load.p.timestamp);
509 510 511 512 513 514

	/*
	 * create pseudo sample to induce dso hit increment
	 * use first address as sample address
	 */
	memset(&sample, 0, sizeof(sample));
515
	sample.cpumode = PERF_RECORD_MISC_USER;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	sample.pid  = pid;
	sample.tid  = tid;
	sample.time = id->time;
	sample.ip   = addr;

	ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
	if (ret)
		return ret;

	ret = jit_inject_event(jd, event);
	/*
	 * mark dso as use to generate buildid in the header
	 */
	if (!ret)
		build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);

	return ret;
}

static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
{
	struct perf_sample sample;
	union perf_event *event;
	struct perf_tool *tool = jd->session->tool;
	char *filename;
	size_t size;
	struct stat st;
543
	int usize;
544 545
	u16 idr_size;
	int ret;
546
	pid_t nspid, pid, tid;
547 548 549 550 551
	struct {
		u32 pid, tid;
		u64 time;
	} *id;

552 553 554
	nspid = jr->load.pid;
	pid   = jr_entry_pid(jd, jr);
	tid   = jr_entry_tid(jd, jr);
555
	usize = jd->unwinding_mapped_size;
556 557 558 559 560 561 562 563 564 565
	idr_size = jd->machine->id_hdr_size;

	/*
	 * +16 to account for sample_id_all (hack)
	 */
	event = calloc(1, sizeof(*event) + 16);
	if (!event)
		return -1;

	filename = event->mmap2.filename;
566
	size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
567
	         jd->dir,
568
		 nspid,
569 570 571 572
		 jr->move.code_index);

	size++; /* for \0 */

573
	if (nsinfo__stat(filename, &st, jd->nsi))
574
		memset(&st, 0, sizeof(st));
575 576 577 578 579 580 581 582 583

	size = PERF_ALIGN(size, sizeof(u64));

	event->mmap2.header.type = PERF_RECORD_MMAP2;
	event->mmap2.header.misc = PERF_RECORD_MISC_USER;
	event->mmap2.header.size = (sizeof(event->mmap2) -
			(sizeof(event->mmap2.filename) - size) + idr_size);
	event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
	event->mmap2.start = jr->move.new_code_addr;
584 585
	event->mmap2.len   = usize ? ALIGN_8(jr->move.code_size) + usize
				   : jr->move.code_size;
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	event->mmap2.pid   = pid;
	event->mmap2.tid   = tid;
	event->mmap2.ino   = st.st_ino;
	event->mmap2.maj   = major(st.st_dev);
	event->mmap2.min   = minor(st.st_dev);
	event->mmap2.prot  = st.st_mode;
	event->mmap2.flags = MAP_SHARED;
	event->mmap2.ino_generation = 1;

	id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
	if (jd->sample_type & PERF_SAMPLE_TID) {
		id->pid  = pid;
		id->tid  = tid;
	}
	if (jd->sample_type & PERF_SAMPLE_TIME)
601
		id->time = convert_timestamp(jd, jr->load.p.timestamp);
602 603 604 605 606 607

	/*
	 * create pseudo sample to induce dso hit increment
	 * use first address as sample address
	 */
	memset(&sample, 0, sizeof(sample));
608
	sample.cpumode = PERF_RECORD_MISC_USER;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	sample.pid  = pid;
	sample.tid  = tid;
	sample.time = id->time;
	sample.ip   = jr->move.new_code_addr;

	ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
	if (ret)
		return ret;

	ret = jit_inject_event(jd, event);
	if (!ret)
		build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);

	return ret;
}

static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
{
	void *data;
	size_t sz;

	if (!(jd && jr))
		return -1;

	sz  = jr->prefix.total_size - sizeof(jr->info);
	data = malloc(sz);
	if (!data)
		return -1;

	memcpy(data, &jr->info.entries, sz);

	jd->debug_data       = data;

	/*
	 * we must use nr_entry instead of size here because
	 * we cannot distinguish actual entry from padding otherwise
	 */
	jd->nr_debug_entries = jr->info.nr_entry;

	return 0;
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
static int
jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
{
	void *unwinding_data;
	uint32_t unwinding_data_size;

	if (!(jd && jr))
		return -1;

	unwinding_data_size  = jr->prefix.total_size - sizeof(jr->unwinding);
	unwinding_data = malloc(unwinding_data_size);
	if (!unwinding_data)
		return -1;

	memcpy(unwinding_data, &jr->unwinding.unwinding_data,
	       unwinding_data_size);

	jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
	jd->unwinding_size = jr->unwinding.unwinding_size;
	jd->unwinding_mapped_size = jr->unwinding.mapped_size;
	jd->unwinding_data = unwinding_data;

	return 0;
}

676 677 678 679
static int
jit_process_dump(struct jit_buf_desc *jd)
{
	union jr_entry *jr;
680
	int ret = 0;
681 682 683 684 685 686 687 688 689 690 691 692

	while ((jr = jit_get_next_entry(jd))) {
		switch(jr->prefix.id) {
		case JIT_CODE_LOAD:
			ret = jit_repipe_code_load(jd, jr);
			break;
		case JIT_CODE_MOVE:
			ret = jit_repipe_code_move(jd, jr);
			break;
		case JIT_CODE_DEBUG_INFO:
			ret = jit_repipe_debug_info(jd, jr);
			break;
693 694 695
		case JIT_CODE_UNWINDING_INFO:
			ret = jit_repipe_unwinding_info(jd, jr);
			break;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		default:
			ret = 0;
			continue;
		}
	}
	return ret;
}

static int
jit_inject(struct jit_buf_desc *jd, char *path)
{
	int ret;

	if (verbose > 0)
		fprintf(stderr, "injecting: %s\n", path);

	ret = jit_open(jd, path);
	if (ret)
		return -1;

	ret = jit_process_dump(jd);

	jit_close(jd);

	if (verbose > 0)
		fprintf(stderr, "injected: %s (%d)\n", path, ret);

	return 0;
}

/*
 * File must be with pattern .../jit-XXXX.dump
 * where XXXX is the PID of the process which did the mmap()
 * as captured in the RECORD_MMAP record
 */
static int
732
jit_detect(char *mmap_name, pid_t pid, struct nsinfo *nsi)
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
 {
	char *p;
	char *end = NULL;
	pid_t pid2;

	if (verbose > 2)
		fprintf(stderr, "jit marker trying : %s\n", mmap_name);
	/*
	 * get file name
	 */
	p = strrchr(mmap_name, '/');
	if (!p)
		return -1;

	/*
	 * match prefix
	 */
	if (strncmp(p, "/jit-", 5))
		return -1;

	/*
	 * skip prefix
	 */
	p += 5;

	/*
	 * must be followed by a pid
	 */
	if (!isdigit(*p))
		return -1;

	pid2 = (int)strtol(p, &end, 10);
	if (!end)
		return -1;

	/*
	 * pid does not match mmap pid
	 * pid==0 in system-wide mode (synthesized)
	 */
772
	if (pid && pid2 != nsi->nstgid)
773 774 775 776 777 778 779 780 781 782 783 784 785
		return -1;
	/*
	 * validate suffix
	 */
	if (strcmp(end, ".dump"))
		return -1;

	if (verbose > 0)
		fprintf(stderr, "jit marker found: %s\n", mmap_name);

	return 0;
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
static void jit_add_pid(struct machine *machine, pid_t pid)
{
	struct thread *thread = machine__findnew_thread(machine, pid, pid);

	if (!thread) {
		pr_err("%s: thread %d not found or created\n", __func__, pid);
		return;
	}

	thread->priv = (void *)1;
}

static bool jit_has_pid(struct machine *machine, pid_t pid)
{
	struct thread *thread = machine__find_thread(machine, pid, pid);

	if (!thread)
		return 0;

	return (bool)thread->priv;
}

808 809
int
jit_process(struct perf_session *session,
810
	    struct perf_data *output,
811 812 813
	    struct machine *machine,
	    char *filename,
	    pid_t pid,
814
	    pid_t tid,
815 816
	    u64 *nbytes)
{
817 818
	struct thread *thread;
	struct nsinfo *nsi;
819
	struct evsel *first;
820 821 822
	struct jit_buf_desc jd;
	int ret;

823 824 825 826 827 828 829 830 831
	thread = machine__findnew_thread(machine, pid, tid);
	if (thread == NULL) {
		pr_err("problem processing JIT mmap event, skipping it.\n");
		return 0;
	}

	nsi = nsinfo__get(thread->nsinfo);
	thread__put(thread);

832 833 834
	/*
	 * first, detect marker mmap (i.e., the jitdump mmap)
	 */
835 836 837
	if (jit_detect(filename, pid, nsi)) {
		nsinfo__put(nsi);

838 839 840 841
		// Strip //anon* mmaps if we processed a jitdump for this pid
		if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
			return 1;

842
		return 0;
843
	}
844 845 846 847 848 849

	memset(&jd, 0, sizeof(jd));

	jd.session = session;
	jd.output  = output;
	jd.machine = machine;
850
	jd.nsi = nsi;
851 852 853 854 855

	/*
	 * track sample_type to compute id_all layout
	 * perf sets the same sample type to all events as of now
	 */
856
	first = evlist__first(session->evlist);
857
	jd.sample_type = first->core.attr.sample_type;
858 859 860 861

	*nbytes = 0;

	ret = jit_inject(&jd, filename);
862
	if (!ret) {
863
		jit_add_pid(machine, pid);
864
		*nbytes = jd.bytes_written;
865 866
		ret = 1;
	}
867

868 869
	nsinfo__put(jd.nsi);

870 871
	return ret;
}