jvmti_agent.c 10.0 KB

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