jitdump.c 17 KB

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