blob: bbeac4f66402444f3ab12562824d1c5412592cab [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
17#include "util.h"
18#include "event.h"
19#include "debug.h"
20#include "evlist.h"
21#include "symbol.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010022#include <elf.h>
23
Adrian Hunter2a28e232016-03-08 10:38:50 +020024#include "tsc.h"
Stephane Eranian9b07e272015-11-30 10:02:21 +010025#include "session.h"
26#include "jit.h"
27#include "jitdump.h"
28#include "genelf.h"
29#include "../builtin.h"
30
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030031#include <linux/ctype.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030032#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030033
Stephane Eranian9b07e272015-11-30 10:02:21 +010034struct jit_buf_desc {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010035 struct perf_data *output;
Stephane Eranian9b07e272015-11-30 10:02:21 +010036 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;
Ingo Molnaradba1632018-12-03 11:22:00 +010043 bool needs_bswap; /* handles cross-endianness */
Adrian Hunter2a28e232016-03-08 10:38:50 +020044 bool use_arch_timestamp;
Stephane Eranian9b07e272015-11-30 10:02:21 +010045 void *debug_data;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -070046 void *unwinding_data;
47 uint64_t unwinding_size;
48 uint64_t unwinding_mapped_size;
49 uint64_t eh_frame_hdr_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +010050 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
57struct 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[0];
62};
63
64struct jit_tool {
65 struct perf_tool tool;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010066 struct perf_data output;
67 struct perf_data input;
Stephane Eranian9b07e272015-11-30 10:02:21 +010068 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
74static int
75jit_emit_elf(char *filename,
76 const char *sym,
77 uint64_t code_addr,
78 const void *code,
Stephane Eranian598b7c62015-11-30 10:02:23 +010079 int csize,
80 void *debug,
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -070081 int nr_debug_entries,
82 void *unwinding,
83 uint32_t unwinding_header_size,
84 uint32_t unwinding_size)
Stephane Eranian9b07e272015-11-30 10:02:21 +010085{
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
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -070097 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
98 unwinding, unwinding_header_size, unwinding_size);
Stephane Eranian9b07e272015-11-30 10:02:21 +010099
100 close(fd);
101
102 if (ret)
103 unlink(filename);
104
105 return ret;
106}
107
108static void
109jit_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
118static int
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300119jit_validate_events(struct perf_session *session)
120{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200121 struct evsel *evsel;
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300122
123 /*
124 * check that all events use CLOCK_MONOTONIC
125 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300126 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200127 if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300128 return -1;
129 }
130 return 0;
131}
132
133static int
Stephane Eranian9b07e272015-11-30 10:02:21 +0100134jit_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
Adrian Hunter2a28e232016-03-08 10:38:50 +0200178 jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
179
Stephane Eranian9b07e272015-11-30 10:02:21 +0100180 if (verbose > 2)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200181 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 +0100182 header.version,
183 header.total_size,
184 (unsigned long long)header.timestamp,
185 header.pid,
Adrian Hunter2a28e232016-03-08 10:38:50 +0200186 header.elf_mach,
187 jd->use_arch_timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100188
Stefano Sanfilippo6760d772016-10-13 03:59:42 -0700189 if (header.version > JITHEADER_VERSION) {
Arnaldo Carvalho de Meloaa8cc2f2017-04-17 15:36:40 -0300190 pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
Stefano Sanfilippo6760d772016-10-13 03:59:42 -0700191 header.version);
192 goto error;
193 }
194
Stephane Eranian9b07e272015-11-30 10:02:21 +0100195 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
Adrian Hunter2a28e232016-03-08 10:38:50 +0200201 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
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300206 /*
207 * validate event is using the correct clockid
208 */
Adrian Hunter2a28e232016-03-08 10:38:50 +0200209 if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
Adrian Hunter4a018cc2016-03-07 16:44:41 -0300210 pr_err("error, jitted code must be sampled with perf record -k 1\n");
211 goto error;
212 }
213
Stephane Eranian9b07e272015-11-30 10:02:21 +0100214 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;
234error:
235 funlockfile(jd->in);
236 fclose(jd->in);
237 return retval;
238}
239
240static union jr_entry *
241jit_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) {
Stefano Sanfilippo7354ec72016-10-13 03:59:38 -0700286 pr_warning("next_entry: unknown record type %d, skipping\n", id);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100287 }
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;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700318 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;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100325 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:
Stefano Sanfilippo7354ec72016-10-13 03:59:38 -0700351 /* skip unknown record (we have read them) */
352 break;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100353 }
354 return jr;
355}
356
357static int
358jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
359{
360 ssize_t size;
361
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100362 size = perf_data__write(jd->output, event, event->header.size);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100363 if (size < 0)
364 return -1;
365
366 jd->bytes_written += size;
367 return 0;
368}
369
Adrian Hunter2a28e232016-03-08 10:38:50 +0200370static 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
381 if (!tc.time_mult)
382 return 0;
383
384 return tsc_to_perf_time(timestamp, &tc);
385}
386
Stephane Eranian9b07e272015-11-30 10:02:21 +0100387static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
388{
389 struct perf_sample sample;
390 union perf_event *event;
391 struct perf_tool *tool = jd->session->tool;
392 uint64_t code, addr;
393 uintptr_t uaddr;
394 char *filename;
395 struct stat st;
396 size_t size;
397 u16 idr_size;
398 const char *sym;
399 uint32_t count;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700400 int ret, csize, usize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100401 pid_t pid, tid;
402 struct {
403 u32 pid, tid;
404 u64 time;
405 } *id;
406
407 pid = jr->load.pid;
408 tid = jr->load.tid;
409 csize = jr->load.code_size;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700410 usize = jd->unwinding_mapped_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100411 addr = jr->load.code_addr;
412 sym = (void *)((unsigned long)jr + sizeof(jr->load));
413 code = (unsigned long)jr + jr->load.p.total_size - csize;
414 count = jr->load.code_index;
415 idr_size = jd->machine->id_hdr_size;
416
417 event = calloc(1, sizeof(*event) + idr_size);
418 if (!event)
419 return -1;
420
421 filename = event->mmap2.filename;
422 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%u.so",
423 jd->dir,
424 pid,
425 count);
426
427 size++; /* for \0 */
428
429 size = PERF_ALIGN(size, sizeof(u64));
430 uaddr = (uintptr_t)code;
Stefano Sanfilippo086f9f32016-10-13 03:59:41 -0700431 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
432 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100433
434 if (jd->debug_data && jd->nr_debug_entries) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300435 zfree(&jd->debug_data);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100436 jd->nr_debug_entries = 0;
437 }
438
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700439 if (jd->unwinding_data && jd->eh_frame_hdr_size) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300440 zfree(&jd->unwinding_data);
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700441 jd->eh_frame_hdr_size = 0;
442 jd->unwinding_mapped_size = 0;
443 jd->unwinding_size = 0;
444 }
445
Stephane Eranian9b07e272015-11-30 10:02:21 +0100446 if (ret) {
447 free(event);
448 return -1;
449 }
450 if (stat(filename, &st))
Colin Ian Kingf56ebf22016-04-19 00:07:18 +0100451 memset(&st, 0, sizeof(st));
Stephane Eranian9b07e272015-11-30 10:02:21 +0100452
453 event->mmap2.header.type = PERF_RECORD_MMAP2;
454 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
455 event->mmap2.header.size = (sizeof(event->mmap2) -
456 (sizeof(event->mmap2.filename) - size) + idr_size);
457
458 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
459 event->mmap2.start = addr;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700460 event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100461 event->mmap2.pid = pid;
462 event->mmap2.tid = tid;
463 event->mmap2.ino = st.st_ino;
464 event->mmap2.maj = major(st.st_dev);
465 event->mmap2.min = minor(st.st_dev);
466 event->mmap2.prot = st.st_mode;
467 event->mmap2.flags = MAP_SHARED;
468 event->mmap2.ino_generation = 1;
469
470 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
471 if (jd->sample_type & PERF_SAMPLE_TID) {
472 id->pid = pid;
473 id->tid = tid;
474 }
475 if (jd->sample_type & PERF_SAMPLE_TIME)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200476 id->time = convert_timestamp(jd, jr->load.p.timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100477
478 /*
479 * create pseudo sample to induce dso hit increment
480 * use first address as sample address
481 */
482 memset(&sample, 0, sizeof(sample));
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300483 sample.cpumode = PERF_RECORD_MISC_USER;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100484 sample.pid = pid;
485 sample.tid = tid;
486 sample.time = id->time;
487 sample.ip = addr;
488
489 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
490 if (ret)
491 return ret;
492
493 ret = jit_inject_event(jd, event);
494 /*
495 * mark dso as use to generate buildid in the header
496 */
497 if (!ret)
498 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
499
500 return ret;
501}
502
503static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
504{
505 struct perf_sample sample;
506 union perf_event *event;
507 struct perf_tool *tool = jd->session->tool;
508 char *filename;
509 size_t size;
510 struct stat st;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700511 int usize;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100512 u16 idr_size;
513 int ret;
514 pid_t pid, tid;
515 struct {
516 u32 pid, tid;
517 u64 time;
518 } *id;
519
520 pid = jr->move.pid;
521 tid = jr->move.tid;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700522 usize = jd->unwinding_mapped_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100523 idr_size = jd->machine->id_hdr_size;
524
525 /*
526 * +16 to account for sample_id_all (hack)
527 */
528 event = calloc(1, sizeof(*event) + 16);
529 if (!event)
530 return -1;
531
532 filename = event->mmap2.filename;
533 size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%"PRIu64,
534 jd->dir,
535 pid,
536 jr->move.code_index);
537
538 size++; /* for \0 */
539
540 if (stat(filename, &st))
Colin Ian Kingf56ebf22016-04-19 00:07:18 +0100541 memset(&st, 0, sizeof(st));
Stephane Eranian9b07e272015-11-30 10:02:21 +0100542
543 size = PERF_ALIGN(size, sizeof(u64));
544
545 event->mmap2.header.type = PERF_RECORD_MMAP2;
546 event->mmap2.header.misc = PERF_RECORD_MISC_USER;
547 event->mmap2.header.size = (sizeof(event->mmap2) -
548 (sizeof(event->mmap2.filename) - size) + idr_size);
549 event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
550 event->mmap2.start = jr->move.new_code_addr;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700551 event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
552 : jr->move.code_size;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100553 event->mmap2.pid = pid;
554 event->mmap2.tid = tid;
555 event->mmap2.ino = st.st_ino;
556 event->mmap2.maj = major(st.st_dev);
557 event->mmap2.min = minor(st.st_dev);
558 event->mmap2.prot = st.st_mode;
559 event->mmap2.flags = MAP_SHARED;
560 event->mmap2.ino_generation = 1;
561
562 id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
563 if (jd->sample_type & PERF_SAMPLE_TID) {
564 id->pid = pid;
565 id->tid = tid;
566 }
567 if (jd->sample_type & PERF_SAMPLE_TIME)
Adrian Hunter2a28e232016-03-08 10:38:50 +0200568 id->time = convert_timestamp(jd, jr->load.p.timestamp);
Stephane Eranian9b07e272015-11-30 10:02:21 +0100569
570 /*
571 * create pseudo sample to induce dso hit increment
572 * use first address as sample address
573 */
574 memset(&sample, 0, sizeof(sample));
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -0300575 sample.cpumode = PERF_RECORD_MISC_USER;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100576 sample.pid = pid;
577 sample.tid = tid;
578 sample.time = id->time;
579 sample.ip = jr->move.new_code_addr;
580
581 ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
582 if (ret)
583 return ret;
584
585 ret = jit_inject_event(jd, event);
586 if (!ret)
587 build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
588
589 return ret;
590}
591
592static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
593{
594 void *data;
595 size_t sz;
596
597 if (!(jd && jr))
598 return -1;
599
600 sz = jr->prefix.total_size - sizeof(jr->info);
601 data = malloc(sz);
602 if (!data)
603 return -1;
604
605 memcpy(data, &jr->info.entries, sz);
606
607 jd->debug_data = data;
608
609 /*
610 * we must use nr_entry instead of size here because
611 * we cannot distinguish actual entry from padding otherwise
612 */
613 jd->nr_debug_entries = jr->info.nr_entry;
614
615 return 0;
616}
617
618static int
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700619jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
620{
621 void *unwinding_data;
622 uint32_t unwinding_data_size;
623
624 if (!(jd && jr))
625 return -1;
626
627 unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
628 unwinding_data = malloc(unwinding_data_size);
629 if (!unwinding_data)
630 return -1;
631
632 memcpy(unwinding_data, &jr->unwinding.unwinding_data,
633 unwinding_data_size);
634
635 jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
636 jd->unwinding_size = jr->unwinding.unwinding_size;
637 jd->unwinding_mapped_size = jr->unwinding.mapped_size;
638 jd->unwinding_data = unwinding_data;
639
640 return 0;
641}
642
643static int
Stephane Eranian9b07e272015-11-30 10:02:21 +0100644jit_process_dump(struct jit_buf_desc *jd)
645{
646 union jr_entry *jr;
Arnaldo Carvalho de Meloef2c3e72016-10-13 17:12:35 -0300647 int ret = 0;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100648
649 while ((jr = jit_get_next_entry(jd))) {
650 switch(jr->prefix.id) {
651 case JIT_CODE_LOAD:
652 ret = jit_repipe_code_load(jd, jr);
653 break;
654 case JIT_CODE_MOVE:
655 ret = jit_repipe_code_move(jd, jr);
656 break;
657 case JIT_CODE_DEBUG_INFO:
658 ret = jit_repipe_debug_info(jd, jr);
659 break;
Stefano Sanfilippo0284fec2016-10-13 03:59:40 -0700660 case JIT_CODE_UNWINDING_INFO:
661 ret = jit_repipe_unwinding_info(jd, jr);
662 break;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100663 default:
664 ret = 0;
665 continue;
666 }
667 }
668 return ret;
669}
670
671static int
672jit_inject(struct jit_buf_desc *jd, char *path)
673{
674 int ret;
675
676 if (verbose > 0)
677 fprintf(stderr, "injecting: %s\n", path);
678
679 ret = jit_open(jd, path);
680 if (ret)
681 return -1;
682
683 ret = jit_process_dump(jd);
684
685 jit_close(jd);
686
687 if (verbose > 0)
688 fprintf(stderr, "injected: %s (%d)\n", path, ret);
689
690 return 0;
691}
692
693/*
694 * File must be with pattern .../jit-XXXX.dump
695 * where XXXX is the PID of the process which did the mmap()
696 * as captured in the RECORD_MMAP record
697 */
698static int
699jit_detect(char *mmap_name, pid_t pid)
700 {
701 char *p;
702 char *end = NULL;
703 pid_t pid2;
704
705 if (verbose > 2)
706 fprintf(stderr, "jit marker trying : %s\n", mmap_name);
707 /*
708 * get file name
709 */
710 p = strrchr(mmap_name, '/');
711 if (!p)
712 return -1;
713
714 /*
715 * match prefix
716 */
717 if (strncmp(p, "/jit-", 5))
718 return -1;
719
720 /*
721 * skip prefix
722 */
723 p += 5;
724
725 /*
726 * must be followed by a pid
727 */
728 if (!isdigit(*p))
729 return -1;
730
731 pid2 = (int)strtol(p, &end, 10);
732 if (!end)
733 return -1;
734
735 /*
736 * pid does not match mmap pid
737 * pid==0 in system-wide mode (synthesized)
738 */
739 if (pid && pid2 != pid)
740 return -1;
741 /*
742 * validate suffix
743 */
744 if (strcmp(end, ".dump"))
745 return -1;
746
747 if (verbose > 0)
748 fprintf(stderr, "jit marker found: %s\n", mmap_name);
749
750 return 0;
751}
752
753int
754jit_process(struct perf_session *session,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100755 struct perf_data *output,
Stephane Eranian9b07e272015-11-30 10:02:21 +0100756 struct machine *machine,
757 char *filename,
758 pid_t pid,
759 u64 *nbytes)
760{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200761 struct evsel *first;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100762 struct jit_buf_desc jd;
763 int ret;
764
765 /*
766 * first, detect marker mmap (i.e., the jitdump mmap)
767 */
768 if (jit_detect(filename, pid))
Adrian Hunter570735b2016-03-07 16:44:40 -0300769 return 0;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100770
771 memset(&jd, 0, sizeof(jd));
772
773 jd.session = session;
774 jd.output = output;
775 jd.machine = machine;
776
777 /*
778 * track sample_type to compute id_all layout
779 * perf sets the same sample type to all events as of now
780 */
781 first = perf_evlist__first(session->evlist);
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200782 jd.sample_type = first->core.attr.sample_type;
Stephane Eranian9b07e272015-11-30 10:02:21 +0100783
784 *nbytes = 0;
785
786 ret = jit_inject(&jd, filename);
Adrian Hunter570735b2016-03-07 16:44:40 -0300787 if (!ret) {
Stephane Eranian9b07e272015-11-30 10:02:21 +0100788 *nbytes = jd.bytes_written;
Adrian Hunter570735b2016-03-07 16:44:40 -0300789 ret = 1;
790 }
Stephane Eranian9b07e272015-11-30 10:02:21 +0100791
792 return ret;
793}