jvmti_agent.c 10 KB

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