jvmti_agent.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * jvmti_agent.c: JVMTI agent interface
  3. *
  4. * Adapted from the Oprofile code in opagent.c:
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Copyright 2007 OProfile authors
  20. * Jens Wilke
  21. * Daniel Hansel
  22. * Copyright IBM Corporation 2007
  23. */
  24. #include <sys/types.h>
  25. #include <sys/stat.h> /* for mkdir() */
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <limits.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <time.h>
  35. #include <sys/mman.h>
  36. #include <syscall.h> /* for gettid() */
  37. #include <err.h>
  38. #include "jvmti_agent.h"
  39. #include "../util/jitdump.h"
  40. #define JIT_LANG "java"
  41. static char jit_path[PATH_MAX];
  42. static void *marker_addr;
  43. /*
  44. * padding buffer
  45. */
  46. static const char pad_bytes[7];
  47. static inline pid_t gettid(void)
  48. {
  49. return (pid_t)syscall(__NR_gettid);
  50. }
  51. static int get_e_machine(struct jitheader *hdr)
  52. {
  53. ssize_t sret;
  54. char id[16];
  55. int fd, ret = -1;
  56. struct {
  57. uint16_t e_type;
  58. uint16_t e_machine;
  59. } info;
  60. fd = open("/proc/self/exe", O_RDONLY);
  61. if (fd == -1)
  62. return -1;
  63. sret = read(fd, id, sizeof(id));
  64. if (sret != sizeof(id))
  65. goto error;
  66. /* check ELF signature */
  67. if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
  68. goto error;
  69. sret = read(fd, &info, sizeof(info));
  70. if (sret != sizeof(info))
  71. goto error;
  72. hdr->elf_mach = info.e_machine;
  73. ret = 0;
  74. error:
  75. close(fd);
  76. return ret;
  77. }
  78. static int use_arch_timestamp;
  79. static inline uint64_t
  80. get_arch_timestamp(void)
  81. {
  82. #if defined(__i386__) || defined(__x86_64__)
  83. unsigned int low, high;
  84. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  85. return low | ((uint64_t)high) << 32;
  86. #else
  87. return 0;
  88. #endif
  89. }
  90. #define NSEC_PER_SEC 1000000000
  91. static int perf_clk_id = CLOCK_MONOTONIC;
  92. static inline uint64_t
  93. timespec_to_ns(const struct timespec *ts)
  94. {
  95. return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  96. }
  97. static inline uint64_t
  98. perf_get_timestamp(void)
  99. {
  100. struct timespec ts;
  101. int ret;
  102. if (use_arch_timestamp)
  103. return get_arch_timestamp();
  104. ret = clock_gettime(perf_clk_id, &ts);
  105. if (ret)
  106. return 0;
  107. return timespec_to_ns(&ts);
  108. }
  109. static int
  110. debug_cache_init(void)
  111. {
  112. char str[32];
  113. char *base, *p;
  114. struct tm tm;
  115. time_t t;
  116. int ret;
  117. time(&t);
  118. localtime_r(&t, &tm);
  119. base = getenv("JITDUMPDIR");
  120. if (!base)
  121. base = getenv("HOME");
  122. if (!base)
  123. base = ".";
  124. strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
  125. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
  126. ret = mkdir(jit_path, 0755);
  127. if (ret == -1) {
  128. if (errno != EEXIST) {
  129. warn("jvmti: cannot create jit cache dir %s", jit_path);
  130. return -1;
  131. }
  132. }
  133. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
  134. ret = mkdir(jit_path, 0755);
  135. if (ret == -1) {
  136. if (errno != EEXIST) {
  137. warn("cannot create jit cache dir %s", jit_path);
  138. return -1;
  139. }
  140. }
  141. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
  142. p = mkdtemp(jit_path);
  143. if (p != jit_path) {
  144. warn("cannot create jit cache dir %s", jit_path);
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. static int
  150. perf_open_marker_file(int fd)
  151. {
  152. long pgsz;
  153. pgsz = sysconf(_SC_PAGESIZE);
  154. if (pgsz == -1)
  155. return -1;
  156. /*
  157. * we mmap the jitdump to create an MMAP RECORD in perf.data file.
  158. * The mmap is captured either live (perf record running when we mmap)
  159. * or in deferred mode, via /proc/PID/maps
  160. * the MMAP record is used as a marker of a jitdump file for more meta
  161. * data info about the jitted code. Perf report/annotate detect this
  162. * special filename and process the jitdump file.
  163. *
  164. * mapping must be PROT_EXEC to ensure it is captured by perf record
  165. * even when not using -d option
  166. */
  167. marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
  168. return (marker_addr == MAP_FAILED) ? -1 : 0;
  169. }
  170. static void
  171. perf_close_marker_file(void)
  172. {
  173. long pgsz;
  174. if (!marker_addr)
  175. return;
  176. pgsz = sysconf(_SC_PAGESIZE);
  177. if (pgsz == -1)
  178. return;
  179. munmap(marker_addr, pgsz);
  180. }
  181. static void
  182. init_arch_timestamp(void)
  183. {
  184. char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
  185. if (!str || !*str || !strcmp(str, "0"))
  186. return;
  187. use_arch_timestamp = 1;
  188. }
  189. void *jvmti_open(void)
  190. {
  191. int pad_cnt;
  192. char dump_path[PATH_MAX];
  193. struct jitheader header;
  194. int fd;
  195. FILE *fp;
  196. init_arch_timestamp();
  197. /*
  198. * check if clockid is supported
  199. */
  200. if (!perf_get_timestamp()) {
  201. if (use_arch_timestamp)
  202. warnx("jvmti: arch timestamp not supported");
  203. else
  204. warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
  205. }
  206. memset(&header, 0, sizeof(header));
  207. debug_cache_init();
  208. /*
  209. * jitdump file name
  210. */
  211. snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
  212. fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
  213. if (fd == -1)
  214. return NULL;
  215. /*
  216. * create perf.data maker for the jitdump file
  217. */
  218. if (perf_open_marker_file(fd)) {
  219. warnx("jvmti: failed to create marker file");
  220. return NULL;
  221. }
  222. fp = fdopen(fd, "w+");
  223. if (!fp) {
  224. warn("jvmti: cannot create %s", dump_path);
  225. close(fd);
  226. goto error;
  227. }
  228. warnx("jvmti: jitdump in %s", dump_path);
  229. if (get_e_machine(&header)) {
  230. warn("get_e_machine failed\n");
  231. goto error;
  232. }
  233. header.magic = JITHEADER_MAGIC;
  234. header.version = JITHEADER_VERSION;
  235. header.total_size = sizeof(header);
  236. header.pid = getpid();
  237. /* calculate amount of padding '\0' */
  238. pad_cnt = PADDING_8ALIGNED(header.total_size);
  239. header.total_size += pad_cnt;
  240. header.timestamp = perf_get_timestamp();
  241. if (use_arch_timestamp)
  242. header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
  243. if (!fwrite(&header, sizeof(header), 1, fp)) {
  244. warn("jvmti: cannot write dumpfile header");
  245. goto error;
  246. }
  247. /* write padding '\0' if necessary */
  248. if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
  249. warn("jvmti: cannot write dumpfile header padding");
  250. goto error;
  251. }
  252. return fp;
  253. error:
  254. fclose(fp);
  255. return NULL;
  256. }
  257. int
  258. jvmti_close(void *agent)
  259. {
  260. struct jr_code_close rec;
  261. FILE *fp = agent;
  262. if (!fp) {
  263. warnx("jvmti: incalid fd in close_agent");
  264. return -1;
  265. }
  266. rec.p.id = JIT_CODE_CLOSE;
  267. rec.p.total_size = sizeof(rec);
  268. rec.p.timestamp = perf_get_timestamp();
  269. if (!fwrite(&rec, sizeof(rec), 1, fp))
  270. return -1;
  271. fclose(fp);
  272. fp = NULL;
  273. perf_close_marker_file();
  274. return 0;
  275. }
  276. int
  277. jvmti_write_code(void *agent, char const *sym,
  278. uint64_t vma, void const *code, unsigned int const size)
  279. {
  280. static int code_generation = 1;
  281. struct jr_code_load rec;
  282. size_t sym_len;
  283. size_t padding_count;
  284. FILE *fp = agent;
  285. int ret = -1;
  286. /* don't care about 0 length function, no samples */
  287. if (size == 0)
  288. return 0;
  289. if (!fp) {
  290. warnx("jvmti: invalid fd in write_native_code");
  291. return -1;
  292. }
  293. sym_len = strlen(sym) + 1;
  294. rec.p.id = JIT_CODE_LOAD;
  295. rec.p.total_size = sizeof(rec) + sym_len;
  296. padding_count = PADDING_8ALIGNED(rec.p.total_size);
  297. rec.p. total_size += padding_count;
  298. rec.p.timestamp = perf_get_timestamp();
  299. rec.code_size = size;
  300. rec.vma = vma;
  301. rec.code_addr = vma;
  302. rec.pid = getpid();
  303. rec.tid = gettid();
  304. if (code)
  305. rec.p.total_size += size;
  306. /*
  307. * If JVM is multi-threaded, nultiple concurrent calls to agent
  308. * may be possible, so protect file writes
  309. */
  310. flockfile(fp);
  311. /*
  312. * get code index inside lock to avoid race condition
  313. */
  314. rec.code_index = code_generation++;
  315. ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
  316. fwrite_unlocked(sym, sym_len, 1, fp);
  317. if (padding_count)
  318. fwrite_unlocked(pad_bytes, padding_count, 1, fp);
  319. if (code)
  320. fwrite_unlocked(code, size, 1, fp);
  321. funlockfile(fp);
  322. ret = 0;
  323. return ret;
  324. }
  325. int
  326. jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
  327. jvmti_line_info_t *li, int nr_lines)
  328. {
  329. struct jr_code_debug_info rec;
  330. size_t sret, len, size, flen;
  331. size_t padding_count;
  332. uint64_t addr;
  333. const char *fn = file;
  334. FILE *fp = agent;
  335. int i;
  336. /*
  337. * no entry to write
  338. */
  339. if (!nr_lines)
  340. return 0;
  341. if (!fp) {
  342. warnx("jvmti: invalid fd in write_debug_info");
  343. return -1;
  344. }
  345. flen = strlen(file) + 1;
  346. rec.p.id = JIT_CODE_DEBUG_INFO;
  347. size = sizeof(rec);
  348. rec.p.timestamp = perf_get_timestamp();
  349. rec.code_addr = (uint64_t)(uintptr_t)code;
  350. rec.nr_entry = nr_lines;
  351. /*
  352. * on disk source line info layout:
  353. * uint64_t : addr
  354. * int : line number
  355. * int : column discriminator
  356. * file[] : source file name
  357. * padding : pad to multiple of 8 bytes
  358. */
  359. size += nr_lines * sizeof(struct debug_entry);
  360. size += flen * nr_lines;
  361. /*
  362. * pad to 8 bytes
  363. */
  364. padding_count = PADDING_8ALIGNED(size);
  365. rec.p.total_size = size + padding_count;
  366. /*
  367. * If JVM is multi-threaded, nultiple concurrent calls to agent
  368. * may be possible, so protect file writes
  369. */
  370. flockfile(fp);
  371. sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
  372. if (sret != 1)
  373. goto error;
  374. for (i = 0; i < nr_lines; i++) {
  375. addr = (uint64_t)li[i].pc;
  376. len = sizeof(addr);
  377. sret = fwrite_unlocked(&addr, len, 1, fp);
  378. if (sret != 1)
  379. goto error;
  380. len = sizeof(li[0].line_number);
  381. sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
  382. if (sret != 1)
  383. goto error;
  384. len = sizeof(li[0].discrim);
  385. sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
  386. if (sret != 1)
  387. goto error;
  388. sret = fwrite_unlocked(fn, flen, 1, fp);
  389. if (sret != 1)
  390. goto error;
  391. }
  392. if (padding_count) {
  393. sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);
  394. if (sret != 1)
  395. goto error;
  396. }
  397. funlockfile(fp);
  398. return 0;
  399. error:
  400. funlockfile(fp);
  401. return -1;
  402. }