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() */
38 #include <linux/kernel.h>
40 #include "jvmti_agent.h"
41 #include "../util/jitdump.h"
43 #define JIT_LANG "java"
45 static char jit_path
[PATH_MAX
];
46 static void *marker_addr
;
48 static inline pid_t
gettid(void)
50 return (pid_t
)syscall(__NR_gettid
);
53 static int get_e_machine(struct jitheader
*hdr
)
63 fd
= open("/proc/self/exe", O_RDONLY
);
67 sret
= read(fd
, id
, sizeof(id
));
68 if (sret
!= sizeof(id
))
71 /* check ELF signature */
72 if (id
[0] != 0x7f || id
[1] != 'E' || id
[2] != 'L' || id
[3] != 'F')
75 sret
= read(fd
, &info
, sizeof(info
));
76 if (sret
!= sizeof(info
))
79 hdr
->elf_mach
= info
.e_machine
;
86 static int use_arch_timestamp
;
88 static inline uint64_t
89 get_arch_timestamp(void)
91 #if defined(__i386__) || defined(__x86_64__)
92 unsigned int low
, high
;
94 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
96 return low
| ((uint64_t)high
) << 32;
102 #define NSEC_PER_SEC 1000000000
103 static int perf_clk_id
= CLOCK_MONOTONIC
;
105 static inline uint64_t
106 timespec_to_ns(const struct timespec
*ts
)
108 return ((uint64_t) ts
->tv_sec
* NSEC_PER_SEC
) + ts
->tv_nsec
;
111 static inline uint64_t
112 perf_get_timestamp(void)
117 if (use_arch_timestamp
)
118 return get_arch_timestamp();
120 ret
= clock_gettime(perf_clk_id
, &ts
);
124 return timespec_to_ns(&ts
);
128 debug_cache_init(void)
137 localtime_r(&t
, &tm
);
139 base
= getenv("JITDUMPDIR");
141 base
= getenv("HOME");
145 strftime(str
, sizeof(str
), JIT_LANG
"-jit-%Y%m%d", &tm
);
147 snprintf(jit_path
, PATH_MAX
- 1, "%s/.debug/", base
);
149 ret
= mkdir(jit_path
, 0755);
151 if (errno
!= EEXIST
) {
152 warn("jvmti: cannot create jit cache dir %s", jit_path
);
157 snprintf(jit_path
, PATH_MAX
- 1, "%s/.debug/jit", base
);
158 ret
= mkdir(jit_path
, 0755);
160 if (errno
!= EEXIST
) {
161 warn("cannot create jit cache dir %s", jit_path
);
166 snprintf(jit_path
, PATH_MAX
- 1, "%s/.debug/jit/%s.XXXXXXXX", base
, str
);
168 p
= mkdtemp(jit_path
);
170 warn("cannot create jit cache dir %s", jit_path
);
178 perf_open_marker_file(int fd
)
182 pgsz
= sysconf(_SC_PAGESIZE
);
187 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
188 * The mmap is captured either live (perf record running when we mmap)
189 * or in deferred mode, via /proc/PID/maps
190 * the MMAP record is used as a marker of a jitdump file for more meta
191 * data info about the jitted code. Perf report/annotate detect this
192 * special filename and process the jitdump file.
194 * mapping must be PROT_EXEC to ensure it is captured by perf record
195 * even when not using -d option
197 marker_addr
= mmap(NULL
, pgsz
, PROT_READ
|PROT_EXEC
, MAP_PRIVATE
, fd
, 0);
198 return (marker_addr
== MAP_FAILED
) ? -1 : 0;
202 perf_close_marker_file(void)
209 pgsz
= sysconf(_SC_PAGESIZE
);
213 munmap(marker_addr
, pgsz
);
217 init_arch_timestamp(void)
219 char *str
= getenv("JITDUMP_USE_ARCH_TIMESTAMP");
221 if (!str
|| !*str
|| !strcmp(str
, "0"))
224 use_arch_timestamp
= 1;
227 void *jvmti_open(void)
229 char dump_path
[PATH_MAX
];
230 struct jitheader header
;
234 init_arch_timestamp();
237 * check if clockid is supported
239 if (!perf_get_timestamp()) {
240 if (use_arch_timestamp
)
241 warnx("jvmti: arch timestamp not supported");
243 warnx("jvmti: kernel does not support %d clock id", perf_clk_id
);
246 memset(&header
, 0, sizeof(header
));
253 scnprintf(dump_path
, PATH_MAX
, "%s/jit-%i.dump", jit_path
, getpid());
255 fd
= open(dump_path
, O_CREAT
|O_TRUNC
|O_RDWR
, 0666);
260 * create perf.data maker for the jitdump file
262 if (perf_open_marker_file(fd
)) {
263 warnx("jvmti: failed to create marker file");
267 fp
= fdopen(fd
, "w+");
269 warn("jvmti: cannot create %s", dump_path
);
274 warnx("jvmti: jitdump in %s", dump_path
);
276 if (get_e_machine(&header
)) {
277 warn("get_e_machine failed\n");
281 header
.magic
= JITHEADER_MAGIC
;
282 header
.version
= JITHEADER_VERSION
;
283 header
.total_size
= sizeof(header
);
284 header
.pid
= getpid();
286 header
.timestamp
= perf_get_timestamp();
288 if (use_arch_timestamp
)
289 header
.flags
|= JITDUMP_FLAGS_ARCH_TIMESTAMP
;
291 if (!fwrite(&header
, sizeof(header
), 1, fp
)) {
292 warn("jvmti: cannot write dumpfile header");
302 jvmti_close(void *agent
)
304 struct jr_code_close rec
;
308 warnx("jvmti: invalid fd in close_agent");
312 rec
.p
.id
= JIT_CODE_CLOSE
;
313 rec
.p
.total_size
= sizeof(rec
);
315 rec
.p
.timestamp
= perf_get_timestamp();
317 if (!fwrite(&rec
, sizeof(rec
), 1, fp
))
324 perf_close_marker_file();
330 jvmti_write_code(void *agent
, char const *sym
,
331 uint64_t vma
, void const *code
, unsigned int const size
)
333 static int code_generation
= 1;
334 struct jr_code_load rec
;
339 /* don't care about 0 length function, no samples */
344 warnx("jvmti: invalid fd in write_native_code");
348 sym_len
= strlen(sym
) + 1;
350 rec
.p
.id
= JIT_CODE_LOAD
;
351 rec
.p
.total_size
= sizeof(rec
) + sym_len
;
352 rec
.p
.timestamp
= perf_get_timestamp();
354 rec
.code_size
= size
;
361 rec
.p
.total_size
+= size
;
364 * If JVM is multi-threaded, nultiple concurrent calls to agent
365 * may be possible, so protect file writes
370 * get code index inside lock to avoid race condition
372 rec
.code_index
= code_generation
++;
374 ret
= fwrite_unlocked(&rec
, sizeof(rec
), 1, fp
);
375 fwrite_unlocked(sym
, sym_len
, 1, fp
);
378 fwrite_unlocked(code
, size
, 1, fp
);
388 jvmti_write_debug_info(void *agent
, uint64_t code
,
389 int nr_lines
, jvmti_line_info_t
*li
,
390 const char * const * file_names
)
392 struct jr_code_debug_info rec
;
393 size_t sret
, len
, size
, flen
= 0;
405 warnx("jvmti: invalid fd in write_debug_info");
409 for (i
= 0; i
< nr_lines
; ++i
) {
410 flen
+= strlen(file_names
[i
]) + 1;
413 rec
.p
.id
= JIT_CODE_DEBUG_INFO
;
415 rec
.p
.timestamp
= perf_get_timestamp();
416 rec
.code_addr
= (uint64_t)(uintptr_t)code
;
417 rec
.nr_entry
= nr_lines
;
420 * on disk source line info layout:
423 * int : column discriminator
424 * file[] : source file name
426 size
+= nr_lines
* sizeof(struct debug_entry
);
428 rec
.p
.total_size
= size
;
431 * If JVM is multi-threaded, nultiple concurrent calls to agent
432 * may be possible, so protect file writes
436 sret
= fwrite_unlocked(&rec
, sizeof(rec
), 1, fp
);
440 for (i
= 0; i
< nr_lines
; i
++) {
442 addr
= (uint64_t)li
[i
].pc
;
444 sret
= fwrite_unlocked(&addr
, len
, 1, fp
);
448 len
= sizeof(li
[0].line_number
);
449 sret
= fwrite_unlocked(&li
[i
].line_number
, len
, 1, fp
);
453 len
= sizeof(li
[0].discrim
);
454 sret
= fwrite_unlocked(&li
[i
].discrim
, len
, 1, fp
);
458 sret
= fwrite_unlocked(file_names
[i
], strlen(file_names
[i
]) + 1, 1, fp
);