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;
47 static inline pid_t gettid(void)
49 return (pid_t)syscall(__NR_gettid);
52 static int get_e_machine(struct jitheader *hdr)
62 fd = open("/proc/self/exe", O_RDONLY);
66 sret = read(fd, id, sizeof(id));
67 if (sret != sizeof(id))
70 /* check ELF signature */
71 if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
74 sret = read(fd, &info, sizeof(info));
75 if (sret != sizeof(info))
78 hdr->elf_mach = info.e_machine;
85 static int use_arch_timestamp;
87 static inline uint64_t
88 get_arch_timestamp(void)
90 #if defined(__i386__) || defined(__x86_64__)
91 unsigned int low, high;
93 asm volatile("rdtsc" : "=a" (low), "=d" (high));
95 return low | ((uint64_t)high) << 32;
101 #define NSEC_PER_SEC 1000000000
102 static int perf_clk_id = CLOCK_MONOTONIC;
104 static inline uint64_t
105 timespec_to_ns(const struct timespec *ts)
107 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
110 static inline uint64_t
111 perf_get_timestamp(void)
116 if (use_arch_timestamp)
117 return get_arch_timestamp();
119 ret = clock_gettime(perf_clk_id, &ts);
123 return timespec_to_ns(&ts);
127 debug_cache_init(void)
136 localtime_r(&t, &tm);
138 base = getenv("JITDUMPDIR");
140 base = getenv("HOME");
144 strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
146 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
148 ret = mkdir(jit_path, 0755);
150 if (errno != EEXIST) {
151 warn("jvmti: cannot create jit cache dir %s", jit_path);
156 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
157 ret = mkdir(jit_path, 0755);
159 if (errno != EEXIST) {
160 warn("cannot create jit cache dir %s", jit_path);
165 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
167 p = mkdtemp(jit_path);
169 warn("cannot create jit cache dir %s", jit_path);
177 perf_open_marker_file(int fd)
181 pgsz = sysconf(_SC_PAGESIZE);
186 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
187 * The mmap is captured either live (perf record running when we mmap)
188 * or in deferred mode, via /proc/PID/maps
189 * the MMAP record is used as a marker of a jitdump file for more meta
190 * data info about the jitted code. Perf report/annotate detect this
191 * special filename and process the jitdump file.
193 * mapping must be PROT_EXEC to ensure it is captured by perf record
194 * even when not using -d option
196 marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
197 return (marker_addr == MAP_FAILED) ? -1 : 0;
201 perf_close_marker_file(void)
208 pgsz = sysconf(_SC_PAGESIZE);
212 munmap(marker_addr, pgsz);
216 init_arch_timestamp(void)
218 char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
220 if (!str || !*str || !strcmp(str, "0"))
223 use_arch_timestamp = 1;
226 void *jvmti_open(void)
228 char dump_path[PATH_MAX];
229 struct jitheader header;
233 init_arch_timestamp();
236 * check if clockid is supported
238 if (!perf_get_timestamp()) {
239 if (use_arch_timestamp)
240 warnx("jvmti: arch timestamp not supported");
242 warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
245 memset(&header, 0, sizeof(header));
252 snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
254 fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
259 * create perf.data maker for the jitdump file
261 if (perf_open_marker_file(fd)) {
262 warnx("jvmti: failed to create marker file");
266 fp = fdopen(fd, "w+");
268 warn("jvmti: cannot create %s", dump_path);
273 warnx("jvmti: jitdump in %s", dump_path);
275 if (get_e_machine(&header)) {
276 warn("get_e_machine failed\n");
280 header.magic = JITHEADER_MAGIC;
281 header.version = JITHEADER_VERSION;
282 header.total_size = sizeof(header);
283 header.pid = getpid();
285 header.timestamp = perf_get_timestamp();
287 if (use_arch_timestamp)
288 header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
290 if (!fwrite(&header, sizeof(header), 1, fp)) {
291 warn("jvmti: cannot write dumpfile header");
301 jvmti_close(void *agent)
303 struct jr_code_close rec;
307 warnx("jvmti: invalid fd in close_agent");
311 rec.p.id = JIT_CODE_CLOSE;
312 rec.p.total_size = sizeof(rec);
314 rec.p.timestamp = perf_get_timestamp();
316 if (!fwrite(&rec, sizeof(rec), 1, fp))
323 perf_close_marker_file();
329 jvmti_write_code(void *agent, char const *sym,
330 uint64_t vma, void const *code, unsigned int const size)
332 static int code_generation = 1;
333 struct jr_code_load rec;
338 /* don't care about 0 length function, no samples */
343 warnx("jvmti: invalid fd in write_native_code");
347 sym_len = strlen(sym) + 1;
349 rec.p.id = JIT_CODE_LOAD;
350 rec.p.total_size = sizeof(rec) + sym_len;
351 rec.p.timestamp = perf_get_timestamp();
353 rec.code_size = size;
360 rec.p.total_size += size;
363 * If JVM is multi-threaded, nultiple concurrent calls to agent
364 * may be possible, so protect file writes
369 * get code index inside lock to avoid race condition
371 rec.code_index = code_generation++;
373 ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
374 fwrite_unlocked(sym, sym_len, 1, fp);
377 fwrite_unlocked(code, size, 1, fp);
387 jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
388 jvmti_line_info_t *li, int nr_lines)
390 struct jr_code_debug_info rec;
391 size_t sret, len, size, flen;
393 const char *fn = file;
404 warnx("jvmti: invalid fd in write_debug_info");
408 flen = strlen(file) + 1;
410 rec.p.id = JIT_CODE_DEBUG_INFO;
412 rec.p.timestamp = perf_get_timestamp();
413 rec.code_addr = (uint64_t)(uintptr_t)code;
414 rec.nr_entry = nr_lines;
417 * on disk source line info layout:
420 * int : column discriminator
421 * file[] : source file name
423 size += nr_lines * sizeof(struct debug_entry);
424 size += flen * nr_lines;
425 rec.p.total_size = size;
428 * If JVM is multi-threaded, nultiple concurrent calls to agent
429 * may be possible, so protect file writes
433 sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
437 for (i = 0; i < nr_lines; i++) {
439 addr = (uint64_t)li[i].pc;
441 sret = fwrite_unlocked(&addr, len, 1, fp);
445 len = sizeof(li[0].line_number);
446 sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
450 len = sizeof(li[0].discrim);
451 sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
455 sret = fwrite_unlocked(fn, flen, 1, fp);