jvmti_agent.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. debug_cache_init(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. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
  123. ret = mkdir(jit_path, 0755);
  124. if (ret == -1) {
  125. if (errno != EEXIST) {
  126. warn("jvmti: cannot create jit cache dir %s", jit_path);
  127. return -1;
  128. }
  129. }
  130. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
  131. ret = mkdir(jit_path, 0755);
  132. if (ret == -1) {
  133. if (errno != EEXIST) {
  134. warn("cannot create jit cache dir %s", jit_path);
  135. return -1;
  136. }
  137. }
  138. snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
  139. p = mkdtemp(jit_path);
  140. if (p != jit_path) {
  141. warn("cannot create jit cache dir %s", jit_path);
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. static int
  147. perf_open_marker_file(int fd)
  148. {
  149. long pgsz;
  150. pgsz = sysconf(_SC_PAGESIZE);
  151. if (pgsz == -1)
  152. return -1;
  153. /*
  154. * we mmap the jitdump to create an MMAP RECORD in perf.data file.
  155. * The mmap is captured either live (perf record running when we mmap)
  156. * or in deferred mode, via /proc/PID/maps
  157. * the MMAP record is used as a marker of a jitdump file for more meta
  158. * data info about the jitted code. Perf report/annotate detect this
  159. * special filename and process the jitdump file.
  160. *
  161. * mapping must be PROT_EXEC to ensure it is captured by perf record
  162. * even when not using -d option
  163. */
  164. marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
  165. return (marker_addr == MAP_FAILED) ? -1 : 0;
  166. }
  167. static void
  168. perf_close_marker_file(void)
  169. {
  170. long pgsz;
  171. if (!marker_addr)
  172. return;
  173. pgsz = sysconf(_SC_PAGESIZE);
  174. if (pgsz == -1)
  175. return;
  176. munmap(marker_addr, pgsz);
  177. }
  178. static void
  179. init_arch_timestamp(void)
  180. {
  181. char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
  182. if (!str || !*str || !strcmp(str, "0"))
  183. return;
  184. use_arch_timestamp = 1;
  185. }
  186. void *jvmti_open(void)
  187. {
  188. char dump_path[PATH_MAX];
  189. struct jitheader header;
  190. int fd;
  191. FILE *fp;
  192. init_arch_timestamp();
  193. /*
  194. * check if clockid is supported
  195. */
  196. if (!perf_get_timestamp()) {
  197. if (use_arch_timestamp)
  198. warnx("jvmti: arch timestamp not supported");
  199. else
  200. warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
  201. }
  202. memset(&header, 0, sizeof(header));
  203. debug_cache_init();
  204. /*
  205. * jitdump file name
  206. */
  207. scnprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
  208. fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
  209. if (fd == -1)
  210. return NULL;
  211. /*
  212. * create perf.data maker for the jitdump file
  213. */
  214. if (perf_open_marker_file(fd)) {
  215. warnx("jvmti: failed to create marker file");
  216. return NULL;
  217. }
  218. fp = fdopen(fd, "w+");
  219. if (!fp) {
  220. warn("jvmti: cannot create %s", dump_path);
  221. close(fd);
  222. goto error;
  223. }
  224. warnx("jvmti: jitdump in %s", dump_path);
  225. if (get_e_machine(&header)) {
  226. warn("get_e_machine failed\n");
  227. goto error;
  228. }
  229. header.magic = JITHEADER_MAGIC;
  230. header.version = JITHEADER_VERSION;
  231. header.total_size = sizeof(header);
  232. header.pid = getpid();
  233. header.timestamp = perf_get_timestamp();
  234. if (use_arch_timestamp)
  235. header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
  236. if (!fwrite(&header, sizeof(header), 1, fp)) {
  237. warn("jvmti: cannot write dumpfile header");
  238. goto error;
  239. }
  240. return fp;
  241. error:
  242. fclose(fp);
  243. return NULL;
  244. }
  245. int
  246. jvmti_close(void *agent)
  247. {
  248. struct jr_code_close rec;
  249. FILE *fp = agent;
  250. if (!fp) {
  251. warnx("jvmti: invalid fd in close_agent");
  252. return -1;
  253. }
  254. rec.p.id = JIT_CODE_CLOSE;
  255. rec.p.total_size = sizeof(rec);
  256. rec.p.timestamp = perf_get_timestamp();
  257. if (!fwrite(&rec, sizeof(rec), 1, fp))
  258. return -1;
  259. fclose(fp);
  260. fp = NULL;
  261. perf_close_marker_file();
  262. return 0;
  263. }
  264. int
  265. jvmti_write_code(void *agent, char const *sym,
  266. uint64_t vma, void const *code, unsigned int const size)
  267. {
  268. static int code_generation = 1;
  269. struct jr_code_load rec;
  270. size_t sym_len;
  271. FILE *fp = agent;
  272. int ret = -1;
  273. /* don't care about 0 length function, no samples */
  274. if (size == 0)
  275. return 0;
  276. if (!fp) {
  277. warnx("jvmti: invalid fd in write_native_code");
  278. return -1;
  279. }
  280. sym_len = strlen(sym) + 1;
  281. rec.p.id = JIT_CODE_LOAD;
  282. rec.p.total_size = sizeof(rec) + sym_len;
  283. rec.p.timestamp = perf_get_timestamp();
  284. rec.code_size = size;
  285. rec.vma = vma;
  286. rec.code_addr = vma;
  287. rec.pid = getpid();
  288. rec.tid = gettid();
  289. if (code)
  290. rec.p.total_size += size;
  291. /*
  292. * If JVM is multi-threaded, nultiple concurrent calls to agent
  293. * may be possible, so protect file writes
  294. */
  295. flockfile(fp);
  296. /*
  297. * get code index inside lock to avoid race condition
  298. */
  299. rec.code_index = code_generation++;
  300. ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
  301. fwrite_unlocked(sym, sym_len, 1, fp);
  302. if (code)
  303. fwrite_unlocked(code, size, 1, fp);
  304. funlockfile(fp);
  305. ret = 0;
  306. return ret;
  307. }
  308. int
  309. jvmti_write_debug_info(void *agent, uint64_t code,
  310. int nr_lines, jvmti_line_info_t *li,
  311. const char * const * file_names)
  312. {
  313. struct jr_code_debug_info rec;
  314. size_t sret, len, size, flen = 0;
  315. uint64_t addr;
  316. FILE *fp = agent;
  317. int i;
  318. /*
  319. * no entry to write
  320. */
  321. if (!nr_lines)
  322. return 0;
  323. if (!fp) {
  324. warnx("jvmti: invalid fd in write_debug_info");
  325. return -1;
  326. }
  327. for (i = 0; i < nr_lines; ++i) {
  328. flen += strlen(file_names[i]) + 1;
  329. }
  330. rec.p.id = JIT_CODE_DEBUG_INFO;
  331. size = sizeof(rec);
  332. rec.p.timestamp = perf_get_timestamp();
  333. rec.code_addr = (uint64_t)(uintptr_t)code;
  334. rec.nr_entry = nr_lines;
  335. /*
  336. * on disk source line info layout:
  337. * uint64_t : addr
  338. * int : line number
  339. * int : column discriminator
  340. * file[] : source file name
  341. */
  342. size += nr_lines * sizeof(struct debug_entry);
  343. size += flen;
  344. rec.p.total_size = size;
  345. /*
  346. * If JVM is multi-threaded, nultiple concurrent calls to agent
  347. * may be possible, so protect file writes
  348. */
  349. flockfile(fp);
  350. sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
  351. if (sret != 1)
  352. goto error;
  353. for (i = 0; i < nr_lines; i++) {
  354. addr = (uint64_t)li[i].pc;
  355. len = sizeof(addr);
  356. sret = fwrite_unlocked(&addr, len, 1, fp);
  357. if (sret != 1)
  358. goto error;
  359. len = sizeof(li[0].line_number);
  360. sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
  361. if (sret != 1)
  362. goto error;
  363. len = sizeof(li[0].discrim);
  364. sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
  365. if (sret != 1)
  366. goto error;
  367. sret = fwrite_unlocked(file_names[i], strlen(file_names[i]) + 1, 1, fp);
  368. if (sret != 1)
  369. goto error;
  370. }
  371. funlockfile(fp);
  372. return 0;
  373. error:
  374. funlockfile(fp);
  375. return -1;
  376. }