jitdump.c 15 KB

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