blob: b80f29bfc7bb2b1070e85dc01ad0c7b4d386e79a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo49a7f012016-08-13 01:12:10 -03002#include <sys/sysmacros.h>
Stephane Eranian9b07e272015-11-30 10:02:21 +01003#include <sys/types.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03004#include <errno.h>
Arnaldo Carvalho de Melo68c01882019-01-22 11:14:55 -02005#include <libgen.h>
Stephane Eranian9b07e272015-11-30 10:02:21 +01006#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>
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -030015#include <linux/stringify.h>
Stephane Eranian9b07e272015-11-30 10:02:21 +010016
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030017#include "build-id.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010018#include "util.h"
19#include "event.h"
20#include "debug.h"
21#include "evlist.h"
22#include "symbol.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010023#include <elf.h>
24
Adrian Hunter2a28e232016-03-08 10:38:50 +020025#include "tsc.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010026#include "session.h"
27#include "jit.h"
28#include "jitdump.h"
29#include "genelf.h"
30#include "../builtin.h"
31
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030032#include <linux/ctype.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030033#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030034
Stephane Eranian9b07e272015-11-30 10:02:21 +010035struct jit_buf_desc {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010036 struct perf_data *output;
Stephane Eranian9b07e272015-11-30 10:02:21 +010037 struct perf_session *session;
38 struct machine *machine;
39 union jr_entry *entry;
40 void *buf;
41 uint64_t sample_type;
42 size_t bufsize;
43 FILE *in;
Ingo Molnaradba1632018-12-03 11:22:00 +010044 bool needs_bswap; /* handles cross-endianness */
Adrian Hunter2a28e232016-03-08 10:38:50 +020045 bool use_arch_timestamp;
Stephane Eranian9b07e272015-11-30 10:02:21 +010046 void *debug_data;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -070047 void *unwinding_data;
48 uint64_t unwinding_size;
49 uint64_t unwinding_mapped_size;
50 uint64_t eh_frame_hdr_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +010051 size_t nr_debug_entries;
52 uint32_t code_load_count;
53 u64 bytes_written;
54 struct rb_root code_root;
55 char dir[PATH_MAX];
56};
57
58struct debug_line_info {
59 unsigned long vma;
60 unsigned int lineno;
61 /* The filename format is unspecified, absolute path, relative etc. */
62 char const filename[0];
63};
64
65struct jit_tool {
66 struct perf_tool tool;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010067 struct perf_data output;
68 struct perf_data input;
Stephane Eranian9b07e272015-11-30 10:02:21 +010069 u64 bytes_written;
70};
71
72#define hmax(a, b) ((a) > (b) ? (a) : (b))
73#define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
74
75static int
76jit_emit_elf(char *filename,
77 const char *sym,
78 uint64_t code_addr,
79 const void *code,
Stephane Eranian598b7c62015-11-30 10:02:23 +010080 int csize,
81 void *debug,
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -070082 int nr_debug_entries,
83 void *unwinding,
84 uint32_t unwinding_header_size,
85 uint32_t unwinding_size)
Stephane Eranian9b07e272015-11-30 10:02:21 +010086{
87 int ret, fd;
88
89 if (verbose > 0)
90 fprintf(stderr, "write ELF image %s\n", filename);
91
92 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
93 if (fd == -1) {
94 pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(errno));
95 return -1;
96 }
97
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -070098 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
99 unwinding, unwinding_header_size, unwinding_size);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100100
101 close(fd);
102
103 if (ret)
104 unlink(filename);
105
106 return ret;
107}
108
109static void
110jit_close(struct jit_buf_desc *jd)
111{
112 if (!(jd && jd->in))
113 return;
114 funlockfile(jd->in);
115 fclose(jd->in);
116 jd->in = NULL;
117}
118
119static int
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300120jit_validate_events(struct perf_session *session)
121{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200122 struct evsel *evsel;
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300123
124 /*
125 * check that all events use CLOCK_MONOTONIC
126 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300127 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200128 if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300129 return -1;
130 }
131 return 0;
132}
133
134static int
Stephane Eranian9b07e272015-11-30 10:02:21 +0100135jit_open(struct jit_buf_desc *jd, const char *name)
136{
137 struct jitheader header;
138 struct jr_prefix *prefix;
139 ssize_t bs, bsz = 0;
140 void *n, *buf = NULL;
141 int ret, retval = -1;
142
143 jd->in = fopen(name, "r");
144 if (!jd->in)
145 return -1;
146
147 bsz = hmax(sizeof(header), sizeof(*prefix));
148
149 buf = malloc(bsz);
150 if (!buf)
151 goto error;
152
153 /*
154 * protect from writer modifying the file while we are reading it
155 */
156 flockfile(jd->in);
157
158 ret = fread(buf, sizeof(header), 1, jd->in);
159 if (ret != 1)
160 goto error;
161
162 memcpy(&header, buf, sizeof(header));
163
164 if (header.magic != JITHEADER_MAGIC) {
165 if (header.magic != JITHEADER_MAGIC_SW)
166 goto error;
167 jd->needs_bswap = true;
168 }
169
170 if (jd->needs_bswap) {
171 header.version = bswap_32(header.version);
172 header.total_size = bswap_32(header.total_size);
173 header.pid = bswap_32(header.pid);
174 header.elf_mach = bswap_32(header.elf_mach);
175 header.timestamp = bswap_64(header.timestamp);
176 header.flags = bswap_64(header.flags);
177 }
178
Adrian Hunter2a28e232016-03-08 10:38:50 +0200179 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
180
Stephane Eranian9b07e272015-11-30 10:02:21 +0100181 if (verbose > 2)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200182 pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
Stephane Eranian9b07e272015-11-30 10:02:21 +0100183 header.version,
184 header.total_size,
185 (unsigned long long)header.timestamp,
186 header.pid,
Adrian Hunter2a28e232016-03-08 10:38:50 +0200187 header.elf_mach,
188 jd->use_arch_timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100189
Stefano Sanfilippo6760d772016-10-13 03:59:42 -0700190 if (header.version > JITHEADER_VERSION) {
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -0300191 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
Stefano Sanfilippo6760d772016-10-13 03:59:42 -0700192 header.version);
193 goto error;
194 }
195
Stephane Eranian9b07e272015-11-30 10:02:21 +0100196 if (header.flags & JITDUMP_FLAGS_RESERVED) {
197 pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
198 (unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
199 goto error;
200 }
201
Adrian Hunter2a28e232016-03-08 10:38:50 +0200202 if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
203 pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
204 goto error;
205 }
206
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300207 /*
208 * validate event is using the correct clockid
209 */
Adrian Hunter2a28e232016-03-08 10:38:50 +0200210 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300211 pr_err("error, jitted code must be sampled with perf record -k 1\n");
212 goto error;
213 }
214
Stephane Eranian9b07e272015-11-30 10:02:21 +0100215 bs = header.total_size - sizeof(header);
216
217 if (bs > bsz) {
218 n = realloc(buf, bs);
219 if (!n)
220 goto error;
221 bsz = bs;
222 buf = n;
223 /* read extra we do not know about */
224 ret = fread(buf, bs - bsz, 1, jd->in);
225 if (ret != 1)
226 goto error;
227 }
228 /*
229 * keep dirname for generating files and mmap records
230 */
231 strcpy(jd->dir, name);
232 dirname(jd->dir);
233
234 return 0;
235error:
236 funlockfile(jd->in);
237 fclose(jd->in);
238 return retval;
239}
240
241static union jr_entry *
242jit_get_next_entry(struct jit_buf_desc *jd)
243{
244 struct jr_prefix *prefix;
245 union jr_entry *jr;
246 void *addr;
247 size_t bs, size;
248 int id, ret;
249
250 if (!(jd && jd->in))
251 return NULL;
252
253 if (jd->buf == NULL) {
254 size_t sz = getpagesize();
255 if (sz < sizeof(*prefix))
256 sz = sizeof(*prefix);
257
258 jd->buf = malloc(sz);
259 if (jd->buf == NULL)
260 return NULL;
261
262 jd->bufsize = sz;
263 }
264
265 prefix = jd->buf;
266
267 /*
268 * file is still locked at this point
269 */
270 ret = fread(prefix, sizeof(*prefix), 1, jd->in);
271 if (ret != 1)
272 return NULL;
273
274 if (jd->needs_bswap) {
275 prefix->id = bswap_32(prefix->id);
276 prefix->total_size = bswap_32(prefix->total_size);
277 prefix->timestamp = bswap_64(prefix->timestamp);
278 }
279 id = prefix->id;
280 size = prefix->total_size;
281
282 bs = (size_t)size;
283 if (bs < sizeof(*prefix))
284 return NULL;
285
286 if (id >= JIT_CODE_MAX) {
Stefano Sanfilippo7354ec72016-10-13 03:59:38 -0700287 pr_warning("next_entry: unknown record type %d, skipping\n", id);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100288 }
289 if (bs > jd->bufsize) {
290 void *n;
291 n = realloc(jd->buf, bs);
292 if (!n)
293 return NULL;
294 jd->buf = n;
295 jd->bufsize = bs;
296 }
297
298 addr = ((void *)jd->buf) + sizeof(*prefix);
299
300 ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
301 if (ret != 1)
302 return NULL;
303
304 jr = (union jr_entry *)jd->buf;
305
306 switch(id) {
307 case JIT_CODE_DEBUG_INFO:
308 if (jd->needs_bswap) {
309 uint64_t n;
310 jr->info.code_addr = bswap_64(jr->info.code_addr);
311 jr->info.nr_entry = bswap_64(jr->info.nr_entry);
312 for (n = 0 ; n < jr->info.nr_entry; n++) {
313 jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
314 jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
315 jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
316 }
317 }
318 break;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700319 case JIT_CODE_UNWINDING_INFO:
320 if (jd->needs_bswap) {
321 jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
322 jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
323 jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
324 }
325 break;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100326 case JIT_CODE_CLOSE:
327 break;
328 case JIT_CODE_LOAD:
329 if (jd->needs_bswap) {
330 jr->load.pid = bswap_32(jr->load.pid);
331 jr->load.tid = bswap_32(jr->load.tid);
332 jr->load.vma = bswap_64(jr->load.vma);
333 jr->load.code_addr = bswap_64(jr->load.code_addr);
334 jr->load.code_size = bswap_64(jr->load.code_size);
335 jr->load.code_index= bswap_64(jr->load.code_index);
336 }
337 jd->code_load_count++;
338 break;
339 case JIT_CODE_MOVE:
340 if (jd->needs_bswap) {
341 jr->move.pid = bswap_32(jr->move.pid);
342 jr->move.tid = bswap_32(jr->move.tid);
343 jr->move.vma = bswap_64(jr->move.vma);
344 jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
345 jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
346 jr->move.code_size = bswap_64(jr->move.code_size);
347 jr->move.code_index = bswap_64(jr->move.code_index);
348 }
349 break;
350 case JIT_CODE_MAX:
351 default:
Stefano Sanfilippo7354ec72016-10-13 03:59:38 -0700352 /* skip unknown record (we have read them) */
353 break;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100354 }
355 return jr;
356}
357
358static int
359jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
360{
361 ssize_t size;
362
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100363 size = perf_data__write(jd->output, event, event->header.size);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100364 if (size < 0)
365 return -1;
366
367 jd->bytes_written += size;
368 return 0;
369}
370
Adrian Hunter2a28e232016-03-08 10:38:50 +0200371static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
372{
373 struct perf_tsc_conversion tc;
374
375 if (!jd->use_arch_timestamp)
376 return timestamp;
377
378 tc.time_shift = jd->session->time_conv.time_shift;
379 tc.time_mult = jd->session->time_conv.time_mult;
380 tc.time_zero = jd->session->time_conv.time_zero;
381
382 if (!tc.time_mult)
383 return 0;
384
385 return tsc_to_perf_time(timestamp, &tc);
386}
387
Stephane Eranian9b07e272015-11-30 10:02:21 +0100388static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
389{
390 struct perf_sample sample;
391 union perf_event *event;
392 struct perf_tool *tool = jd->session->tool;
393 uint64_t code, addr;
394 uintptr_t uaddr;
395 char *filename;
396 struct stat st;
397 size_t size;
398 u16 idr_size;
399 const char *sym;
400 uint32_t count;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700401 int ret, csize, usize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100402 pid_t pid, tid;
403 struct {
404 u32 pid, tid;
405 u64 time;
406 } *id;
407
408 pid = jr->load.pid;
409 tid = jr->load.tid;
410 csize = jr->load.code_size;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700411 usize = jd->unwinding_mapped_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100412 addr = jr->load.code_addr;
413 sym = (void *)((unsigned long)jr + sizeof(jr->load));
414 code = (unsigned long)jr + jr->load.p.total_size - csize;
415 count = jr->load.code_index;
416 idr_size = jd->machine->id_hdr_size;
417
418 event = calloc(1, sizeof(*event) + idr_size);
419 if (!event)
420 return -1;
421
422 filename = event->mmap2.filename;
423 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%u.so",
424 jd->dir,
425 pid,
426 count);
427
428 size++; /* for \0 */
429
430 size = PERF_ALIGN(size, sizeof(u64));
431 uaddr = (uintptr_t)code;
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -0700432 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
433 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100434
435 if (jd->debug_data && jd->nr_debug_entries) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300436 zfree(&jd->debug_data);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100437 jd->nr_debug_entries = 0;
438 }
439
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700440 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300441 zfree(&jd->unwinding_data);
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700442 jd->eh_frame_hdr_size = 0;
443 jd->unwinding_mapped_size = 0;
444 jd->unwinding_size = 0;
445 }
446
Stephane Eranian9b07e272015-11-30 10:02:21 +0100447 if (ret) {
448 free(event);
449 return -1;
450 }
451 if (stat(filename, &st))
Colin Ian Kingf56ebf22016-04-19 00:07:18 +0100452 memset(&st, 0, sizeof(st));
Stephane Eranian9b07e272015-11-30 10:02:21 +0100453
454 event->mmap2.header.type = PERF_RECORD_MMAP2;
455 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
456 event->mmap2.header.size = (sizeof(event->mmap2) -
457 (sizeof(event->mmap2.filename) - size) + idr_size);
458
459 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
460 event->mmap2.start = addr;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700461 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100462 event->mmap2.pid = pid;
463 event->mmap2.tid = tid;
464 event->mmap2.ino = st.st_ino;
465 event->mmap2.maj = major(st.st_dev);
466 event->mmap2.min = minor(st.st_dev);
467 event->mmap2.prot = st.st_mode;
468 event->mmap2.flags = MAP_SHARED;
469 event->mmap2.ino_generation = 1;
470
471 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
472 if (jd->sample_type & PERF_SAMPLE_TID) {
473 id->pid = pid;
474 id->tid = tid;
475 }
476 if (jd->sample_type & PERF_SAMPLE_TIME)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200477 id->time = convert_timestamp(jd, jr->load.p.timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100478
479 /*
480 * create pseudo sample to induce dso hit increment
481 * use first address as sample address
482 */
483 memset(&sample, 0, sizeof(sample));
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300484 sample.cpumode = PERF_RECORD_MISC_USER;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100485 sample.pid = pid;
486 sample.tid = tid;
487 sample.time = id->time;
488 sample.ip = addr;
489
490 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
491 if (ret)
492 return ret;
493
494 ret = jit_inject_event(jd, event);
495 /*
496 * mark dso as use to generate buildid in the header
497 */
498 if (!ret)
499 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
500
501 return ret;
502}
503
504static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
505{
506 struct perf_sample sample;
507 union perf_event *event;
508 struct perf_tool *tool = jd->session->tool;
509 char *filename;
510 size_t size;
511 struct stat st;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700512 int usize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100513 u16 idr_size;
514 int ret;
515 pid_t pid, tid;
516 struct {
517 u32 pid, tid;
518 u64 time;
519 } *id;
520
521 pid = jr->move.pid;
522 tid = jr->move.tid;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700523 usize = jd->unwinding_mapped_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100524 idr_size = jd->machine->id_hdr_size;
525
526 /*
527 * +16 to account for sample_id_all (hack)
528 */
529 event = calloc(1, sizeof(*event) + 16);
530 if (!event)
531 return -1;
532
533 filename = event->mmap2.filename;
534 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%"PRIu64,
535 jd->dir,
536 pid,
537 jr->move.code_index);
538
539 size++; /* for \0 */
540
541 if (stat(filename, &st))
Colin Ian Kingf56ebf22016-04-19 00:07:18 +0100542 memset(&st, 0, sizeof(st));
Stephane Eranian9b07e272015-11-30 10:02:21 +0100543
544 size = PERF_ALIGN(size, sizeof(u64));
545
546 event->mmap2.header.type = PERF_RECORD_MMAP2;
547 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
548 event->mmap2.header.size = (sizeof(event->mmap2) -
549 (sizeof(event->mmap2.filename) - size) + idr_size);
550 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
551 event->mmap2.start = jr->move.new_code_addr;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700552 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
553 : jr->move.code_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100554 event->mmap2.pid = pid;
555 event->mmap2.tid = tid;
556 event->mmap2.ino = st.st_ino;
557 event->mmap2.maj = major(st.st_dev);
558 event->mmap2.min = minor(st.st_dev);
559 event->mmap2.prot = st.st_mode;
560 event->mmap2.flags = MAP_SHARED;
561 event->mmap2.ino_generation = 1;
562
563 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
564 if (jd->sample_type & PERF_SAMPLE_TID) {
565 id->pid = pid;
566 id->tid = tid;
567 }
568 if (jd->sample_type & PERF_SAMPLE_TIME)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200569 id->time = convert_timestamp(jd, jr->load.p.timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100570
571 /*
572 * create pseudo sample to induce dso hit increment
573 * use first address as sample address
574 */
575 memset(&sample, 0, sizeof(sample));
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300576 sample.cpumode = PERF_RECORD_MISC_USER;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100577 sample.pid = pid;
578 sample.tid = tid;
579 sample.time = id->time;
580 sample.ip = jr->move.new_code_addr;
581
582 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
583 if (ret)
584 return ret;
585
586 ret = jit_inject_event(jd, event);
587 if (!ret)
588 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
589
590 return ret;
591}
592
593static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
594{
595 void *data;
596 size_t sz;
597
598 if (!(jd && jr))
599 return -1;
600
601 sz = jr->prefix.total_size - sizeof(jr->info);
602 data = malloc(sz);
603 if (!data)
604 return -1;
605
606 memcpy(data, &jr->info.entries, sz);
607
608 jd->debug_data = data;
609
610 /*
611 * we must use nr_entry instead of size here because
612 * we cannot distinguish actual entry from padding otherwise
613 */
614 jd->nr_debug_entries = jr->info.nr_entry;
615
616 return 0;
617}
618
619static int
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700620jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
621{
622 void *unwinding_data;
623 uint32_t unwinding_data_size;
624
625 if (!(jd && jr))
626 return -1;
627
628 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
629 unwinding_data = malloc(unwinding_data_size);
630 if (!unwinding_data)
631 return -1;
632
633 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
634 unwinding_data_size);
635
636 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
637 jd->unwinding_size = jr->unwinding.unwinding_size;
638 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
639 jd->unwinding_data = unwinding_data;
640
641 return 0;
642}
643
644static int
Stephane Eranian9b07e272015-11-30 10:02:21 +0100645jit_process_dump(struct jit_buf_desc *jd)
646{
647 union jr_entry *jr;
Arnaldo Carvalho de Meloef2c3e72016-10-13 17:12:35 -0300648 int ret = 0;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100649
650 while ((jr = jit_get_next_entry(jd))) {
651 switch(jr->prefix.id) {
652 case JIT_CODE_LOAD:
653 ret = jit_repipe_code_load(jd, jr);
654 break;
655 case JIT_CODE_MOVE:
656 ret = jit_repipe_code_move(jd, jr);
657 break;
658 case JIT_CODE_DEBUG_INFO:
659 ret = jit_repipe_debug_info(jd, jr);
660 break;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700661 case JIT_CODE_UNWINDING_INFO:
662 ret = jit_repipe_unwinding_info(jd, jr);
663 break;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100664 default:
665 ret = 0;
666 continue;
667 }
668 }
669 return ret;
670}
671
672static int
673jit_inject(struct jit_buf_desc *jd, char *path)
674{
675 int ret;
676
677 if (verbose > 0)
678 fprintf(stderr, "injecting: %s\n", path);
679
680 ret = jit_open(jd, path);
681 if (ret)
682 return -1;
683
684 ret = jit_process_dump(jd);
685
686 jit_close(jd);
687
688 if (verbose > 0)
689 fprintf(stderr, "injected: %s (%d)\n", path, ret);
690
691 return 0;
692}
693
694/*
695 * File must be with pattern .../jit-XXXX.dump
696 * where XXXX is the PID of the process which did the mmap()
697 * as captured in the RECORD_MMAP record
698 */
699static int
700jit_detect(char *mmap_name, pid_t pid)
701 {
702 char *p;
703 char *end = NULL;
704 pid_t pid2;
705
706 if (verbose > 2)
707 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
708 /*
709 * get file name
710 */
711 p = strrchr(mmap_name, '/');
712 if (!p)
713 return -1;
714
715 /*
716 * match prefix
717 */
718 if (strncmp(p, "/jit-", 5))
719 return -1;
720
721 /*
722 * skip prefix
723 */
724 p += 5;
725
726 /*
727 * must be followed by a pid
728 */
729 if (!isdigit(*p))
730 return -1;
731
732 pid2 = (int)strtol(p, &end, 10);
733 if (!end)
734 return -1;
735
736 /*
737 * pid does not match mmap pid
738 * pid==0 in system-wide mode (synthesized)
739 */
740 if (pid && pid2 != pid)
741 return -1;
742 /*
743 * validate suffix
744 */
745 if (strcmp(end, ".dump"))
746 return -1;
747
748 if (verbose > 0)
749 fprintf(stderr, "jit marker found: %s\n", mmap_name);
750
751 return 0;
752}
753
754int
755jit_process(struct perf_session *session,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100756 struct perf_data *output,
Stephane Eranian9b07e272015-11-30 10:02:21 +0100757 struct machine *machine,
758 char *filename,
759 pid_t pid,
760 u64 *nbytes)
761{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200762 struct evsel *first;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100763 struct jit_buf_desc jd;
764 int ret;
765
766 /*
767 * first, detect marker mmap (i.e., the jitdump mmap)
768 */
769 if (jit_detect(filename, pid))
Adrian Hunter570735b2016-03-07 16:44:40 -0300770 return 0;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100771
772 memset(&jd, 0, sizeof(jd));
773
774 jd.session = session;
775 jd.output = output;
776 jd.machine = machine;
777
778 /*
779 * track sample_type to compute id_all layout
780 * perf sets the same sample type to all events as of now
781 */
782 first = perf_evlist__first(session->evlist);
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200783 jd.sample_type = first->core.attr.sample_type;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100784
785 *nbytes = 0;
786
787 ret = jit_inject(&jd, filename);
Adrian Hunter570735b2016-03-07 16:44:40 -0300788 if (!ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100789 *nbytes = jd.bytes_written;
Adrian Hunter570735b2016-03-07 16:44:40 -0300790 ret = 1;
791 }
Stephane Eranian9b07e272015-11-30 10:02:21 +0100792
793 return ret;
794}