jitdump.c 15 KB

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