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)
67 fd = open("/proc/self/exe", O_RDONLY);
71 sret = read(fd, id, sizeof(id));
72 if (sret != sizeof(id))
75 /* check ELF signature */
76 if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
79 sret = read(fd, &info, sizeof(info));
80 if (sret != sizeof(info))
83 hdr->elf_mach = info.e_machine;
90 static int use_arch_timestamp;
92 static inline uint64_t
93 get_arch_timestamp(void)
95 #if defined(__i386__) || defined(__x86_64__)
96 unsigned int low, high;
98 asm volatile("rdtsc" : "=a" (low), "=d" (high));
100 return low | ((uint64_t)high) << 32;
106 #define NSEC_PER_SEC 1000000000
107 static int perf_clk_id = CLOCK_MONOTONIC;
109 static inline uint64_t
110 timespec_to_ns(const struct timespec *ts)
112 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
115 static inline uint64_t
116 perf_get_timestamp(void)
121 if (use_arch_timestamp)
122 return get_arch_timestamp();
124 ret = clock_gettime(perf_clk_id, &ts);
128 return timespec_to_ns(&ts);
132 debug_cache_init(void)
141 localtime_r(&t, &tm);
143 base = getenv("JITDUMPDIR");
145 base = getenv("HOME");
149 strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
151 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
153 ret = mkdir(jit_path, 0755);
155 if (errno != EEXIST) {
156 warn("jvmti: cannot create jit cache dir %s", jit_path);
161 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
162 ret = mkdir(jit_path, 0755);
164 if (errno != EEXIST) {
165 warn("cannot create jit cache dir %s", jit_path);
170 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
172 p = mkdtemp(jit_path);
174 warn("cannot create jit cache dir %s", jit_path);
182 perf_open_marker_file(int fd)
186 pgsz = sysconf(_SC_PAGESIZE);
191 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
192 * The mmap is captured either live (perf record running when we mmap)
193 * or in deferred mode, via /proc/PID/maps
194 * the MMAP record is used as a marker of a jitdump file for more meta
195 * data info about the jitted code. Perf report/annotate detect this
196 * special filename and process the jitdump file.
198 * mapping must be PROT_EXEC to ensure it is captured by perf record
199 * even when not using -d option
201 marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
202 return (marker_addr == MAP_FAILED) ? -1 : 0;
206 perf_close_marker_file(void)
213 pgsz = sysconf(_SC_PAGESIZE);
217 munmap(marker_addr, pgsz);
221 init_arch_timestamp(void)
223 char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
225 if (!str || !*str || !strcmp(str, "0"))
228 use_arch_timestamp = 1;
231 void *jvmti_open(void)
234 char dump_path[PATH_MAX];
235 struct jitheader header;
239 init_arch_timestamp();
242 * check if clockid is supported
244 if (!perf_get_timestamp()) {
245 if (use_arch_timestamp)
246 warnx("jvmti: arch timestamp not supported");
248 warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
251 memset(&header, 0, sizeof(header));
258 snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
260 fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
265 * create perf.data maker for the jitdump file
267 if (perf_open_marker_file(fd)) {
268 warnx("jvmti: failed to create marker file");
272 fp = fdopen(fd, "w+");
274 warn("jvmti: cannot create %s", dump_path);
279 warnx("jvmti: jitdump in %s", dump_path);
281 if (get_e_machine(&header)) {
282 warn("get_e_machine failed\n");
286 header.magic = JITHEADER_MAGIC;
287 header.version = JITHEADER_VERSION;
288 header.total_size = sizeof(header);
289 header.pid = getpid();
291 /* calculate amount of padding '\0' */
292 pad_cnt = PADDING_8ALIGNED(header.total_size);
293 header.total_size += pad_cnt;
295 header.timestamp = perf_get_timestamp();
297 if (use_arch_timestamp)
298 header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
300 if (!fwrite(&header, sizeof(header), 1, fp)) {
301 warn("jvmti: cannot write dumpfile header");
305 /* write padding '\0' if necessary */
306 if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
307 warn("jvmti: cannot write dumpfile header padding");
318 jvmti_close(void *agent)
320 struct jr_code_close rec;
324 warnx("jvmti: incalid fd in close_agent");
328 rec.p.id = JIT_CODE_CLOSE;
329 rec.p.total_size = sizeof(rec);
331 rec.p.timestamp = perf_get_timestamp();
333 if (!fwrite(&rec, sizeof(rec), 1, fp))
340 perf_close_marker_file();
346 jvmti_write_code(void *agent, char const *sym,
347 uint64_t vma, void const *code, unsigned int const size)
349 static int code_generation = 1;
350 struct jr_code_load rec;
352 size_t padding_count;
356 /* don't care about 0 length function, no samples */
361 warnx("jvmti: invalid fd in write_native_code");
365 sym_len = strlen(sym) + 1;
367 rec.p.id = JIT_CODE_LOAD;
368 rec.p.total_size = sizeof(rec) + sym_len;
369 padding_count = PADDING_8ALIGNED(rec.p.total_size);
370 rec.p. total_size += padding_count;
371 rec.p.timestamp = perf_get_timestamp();
373 rec.code_size = size;
380 rec.p.total_size += size;
383 * If JVM is multi-threaded, nultiple concurrent calls to agent
384 * may be possible, so protect file writes
389 * get code index inside lock to avoid race condition
391 rec.code_index = code_generation++;
393 ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
394 fwrite_unlocked(sym, sym_len, 1, fp);
397 fwrite_unlocked(pad_bytes, padding_count, 1, fp);
400 fwrite_unlocked(code, size, 1, fp);
410 jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
411 jvmti_line_info_t *li, int nr_lines)
413 struct jr_code_debug_info rec;
414 size_t sret, len, size, flen;
415 size_t padding_count;
417 const char *fn = file;
428 warnx("jvmti: invalid fd in write_debug_info");
432 flen = strlen(file) + 1;
434 rec.p.id = JIT_CODE_DEBUG_INFO;
436 rec.p.timestamp = perf_get_timestamp();
437 rec.code_addr = (uint64_t)(uintptr_t)code;
438 rec.nr_entry = nr_lines;
441 * on disk source line info layout:
444 * int : column discriminator
445 * file[] : source file name
446 * padding : pad to multiple of 8 bytes
448 size += nr_lines * sizeof(struct debug_entry);
449 size += flen * nr_lines;
453 padding_count = PADDING_8ALIGNED(size);
455 rec.p.total_size = size + padding_count;
458 * If JVM is multi-threaded, nultiple concurrent calls to agent
459 * may be possible, so protect file writes
463 sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
467 for (i = 0; i < nr_lines; i++) {
469 addr = (uint64_t)li[i].pc;
471 sret = fwrite_unlocked(&addr, len, 1, fp);
475 len = sizeof(li[0].line_number);
476 sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
480 len = sizeof(li[0].discrim);
481 sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
485 sret = fwrite_unlocked(fn, flen, 1, fp);
490 sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);