jitdump.c 17 KB

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