jitdump.c 21 KB

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