jitdump.c 19 KB

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