jvmti_agent.c 10 KB

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