jvmti_agent.c 9.3 KB

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