jitdump.c 15 KB

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