2 * jvmti_agent.c: JVMTI agent interface
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.
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.
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
19 * Copyright 2007 OProfile authors
22 * Copyright IBM Corporation 2007
24 #include <sys/types.h>
25 #include <sys/stat.h> /* for mkdir() */
36 #include <syscall.h> /* for gettid() */
39 #include "jvmti_agent.h"
40 #include "../util/jitdump.h"
42 #define JIT_LANG "java"
44 static char jit_path[PATH_MAX];
45 static void *marker_addr;
50 static const char pad_bytes[7];
52 static inline pid_t gettid(void)
54 return (pid_t)syscall(__NR_gettid);
57 static int get_e_machine(struct jitheader *hdr)
68 fd = open("/proc/self/exe", O_RDONLY);
72 sret = read(fd, id, sizeof(id));
73 if (sret != sizeof(id))
76 /* check ELF signature */
77 if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
80 sret = read(fd, &info, sizeof(info));
81 if (sret != sizeof(info))
86 m = 0; /* ELF EM_NONE */
95 #define NSEC_PER_SEC 1000000000
96 static int perf_clk_id = CLOCK_MONOTONIC;
98 static inline uint64_t
99 timespec_to_ns(const struct timespec *ts)
101 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
104 static inline uint64_t
105 perf_get_timestamp(void)
110 ret = clock_gettime(perf_clk_id, &ts);
114 return timespec_to_ns(&ts);
118 debug_cache_init(void)
127 localtime_r(&t, &tm);
129 base = getenv("JITDUMPDIR");
131 base = getenv("HOME");
135 strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
137 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
139 ret = mkdir(jit_path, 0755);
141 if (errno != EEXIST) {
142 warn("jvmti: cannot create jit cache dir %s", jit_path);
147 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
148 ret = mkdir(jit_path, 0755);
150 if (errno != EEXIST) {
151 warn("cannot create jit cache dir %s", jit_path);
156 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
158 p = mkdtemp(jit_path);
160 warn("cannot create jit cache dir %s", jit_path);
168 perf_open_marker_file(int fd)
172 pgsz = sysconf(_SC_PAGESIZE);
177 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
178 * The mmap is captured either live (perf record running when we mmap)
179 * or in deferred mode, via /proc/PID/maps
180 * the MMAP record is used as a marker of a jitdump file for more meta
181 * data info about the jitted code. Perf report/annotate detect this
182 * special filename and process the jitdump file.
184 * mapping must be PROT_EXEC to ensure it is captured by perf record
185 * even when not using -d option
187 marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
188 return (marker_addr == MAP_FAILED) ? -1 : 0;
192 perf_close_marker_file(void)
199 pgsz = sysconf(_SC_PAGESIZE);
203 munmap(marker_addr, pgsz);
206 void *jvmti_open(void)
209 char dump_path[PATH_MAX];
210 struct jitheader header;
215 * check if clockid is supported
217 if (!perf_get_timestamp())
218 warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
220 memset(&header, 0, sizeof(header));
227 snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
229 fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
234 * create perf.data maker for the jitdump file
236 if (perf_open_marker_file(fd)) {
237 warnx("jvmti: failed to create marker file");
241 fp = fdopen(fd, "w+");
243 warn("jvmti: cannot create %s", dump_path);
248 warnx("jvmti: jitdump in %s", dump_path);
250 if (get_e_machine(&header)) {
251 warn("get_e_machine failed\n");
255 header.magic = JITHEADER_MAGIC;
256 header.version = JITHEADER_VERSION;
257 header.total_size = sizeof(header);
258 header.pid = getpid();
260 /* calculate amount of padding '\0' */
261 pad_cnt = PADDING_8ALIGNED(header.total_size);
262 header.total_size += pad_cnt;
264 header.timestamp = perf_get_timestamp();
266 if (!fwrite(&header, sizeof(header), 1, fp)) {
267 warn("jvmti: cannot write dumpfile header");
271 /* write padding '\0' if necessary */
272 if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
273 warn("jvmti: cannot write dumpfile header padding");
284 jvmti_close(void *agent)
286 struct jr_code_close rec;
290 warnx("jvmti: incalid fd in close_agent");
294 rec.p.id = JIT_CODE_CLOSE;
295 rec.p.total_size = sizeof(rec);
297 rec.p.timestamp = perf_get_timestamp();
299 if (!fwrite(&rec, sizeof(rec), 1, fp))
306 perf_close_marker_file();
312 jvmti_write_code(void *agent, char const *sym,
313 uint64_t vma, void const *code, unsigned int const size)
315 static int code_generation = 1;
316 struct jr_code_load rec;
318 size_t padding_count;
322 /* don't care about 0 length function, no samples */
327 warnx("jvmti: invalid fd in write_native_code");
331 sym_len = strlen(sym) + 1;
333 rec.p.id = JIT_CODE_LOAD;
334 rec.p.total_size = sizeof(rec) + sym_len;
335 padding_count = PADDING_8ALIGNED(rec.p.total_size);
336 rec.p. total_size += padding_count;
337 rec.p.timestamp = perf_get_timestamp();
339 rec.code_size = size;
346 rec.p.total_size += size;
349 * If JVM is multi-threaded, nultiple concurrent calls to agent
350 * may be possible, so protect file writes
355 * get code index inside lock to avoid race condition
357 rec.code_index = code_generation++;
359 ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
360 fwrite_unlocked(sym, sym_len, 1, fp);
363 fwrite_unlocked(pad_bytes, padding_count, 1, fp);
366 fwrite_unlocked(code, size, 1, fp);
376 jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
377 jvmti_line_info_t *li, int nr_lines)
379 struct jr_code_debug_info rec;
380 size_t sret, len, size, flen;
381 size_t padding_count;
383 const char *fn = file;
394 warnx("jvmti: invalid fd in write_debug_info");
398 flen = strlen(file) + 1;
400 rec.p.id = JIT_CODE_DEBUG_INFO;
402 rec.p.timestamp = perf_get_timestamp();
403 rec.code_addr = (uint64_t)(uintptr_t)code;
404 rec.nr_entry = nr_lines;
407 * on disk source line info layout:
410 * int : column discriminator
411 * file[] : source file name
412 * padding : pad to multiple of 8 bytes
414 size += nr_lines * sizeof(struct debug_entry);
415 size += flen * nr_lines;
419 padding_count = PADDING_8ALIGNED(size);
421 rec.p.total_size = size + padding_count;
424 * If JVM is multi-threaded, nultiple concurrent calls to agent
425 * may be possible, so protect file writes
429 sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
433 for (i = 0; i < nr_lines; i++) {
435 addr = (uint64_t)li[i].pc;
437 sret = fwrite_unlocked(&addr, len, 1, fp);
441 len = sizeof(li[0].line_number);
442 sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
446 len = sizeof(li[0].discrim);
447 sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
451 sret = fwrite_unlocked(fn, flen, 1, fp);
456 sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);