jvmti_agent.c 9.1 KB

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