Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * jvmti_agent.c: JVMTI agent interface
0003  *
0004  * Adapted from the Oprofile code in opagent.c:
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0018  *
0019  * Copyright 2007 OProfile authors
0020  * Jens Wilke
0021  * Daniel Hansel
0022  * Copyright IBM Corporation 2007
0023  */
0024 #include <sys/types.h>
0025 #include <sys/stat.h> /* for mkdir() */
0026 #include <stdio.h>
0027 #include <errno.h>
0028 #include <string.h>
0029 #include <stdlib.h>
0030 #include <stdint.h>
0031 #include <limits.h>
0032 #include <fcntl.h>
0033 #include <unistd.h>
0034 #include <time.h>
0035 #include <sys/mman.h>
0036 #include <syscall.h> /* for gettid() */
0037 #include <err.h>
0038 #include <linux/kernel.h>
0039 
0040 #include "jvmti_agent.h"
0041 #include "../util/jitdump.h"
0042 
0043 #define JIT_LANG "java"
0044 
0045 static char jit_path[PATH_MAX];
0046 static void *marker_addr;
0047 
0048 #ifndef HAVE_GETTID
0049 static inline pid_t gettid(void)
0050 {
0051     return (pid_t)syscall(__NR_gettid);
0052 }
0053 #endif
0054 
0055 static int get_e_machine(struct jitheader *hdr)
0056 {
0057     ssize_t sret;
0058     char id[16];
0059     int fd, ret = -1;
0060     struct {
0061         uint16_t e_type;
0062         uint16_t e_machine;
0063     } info;
0064 
0065     fd = open("/proc/self/exe", O_RDONLY);
0066     if (fd == -1)
0067         return -1;
0068 
0069     sret = read(fd, id, sizeof(id));
0070     if (sret != sizeof(id))
0071         goto error;
0072 
0073     /* check ELF signature */
0074     if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
0075         goto error;
0076 
0077     sret = read(fd, &info, sizeof(info));
0078     if (sret != sizeof(info))
0079         goto error;
0080 
0081     hdr->elf_mach = info.e_machine;
0082     ret = 0;
0083 error:
0084     close(fd);
0085     return ret;
0086 }
0087 
0088 static int use_arch_timestamp;
0089 
0090 static inline uint64_t
0091 get_arch_timestamp(void)
0092 {
0093 #if defined(__i386__) || defined(__x86_64__)
0094     unsigned int low, high;
0095 
0096     asm volatile("rdtsc" : "=a" (low), "=d" (high));
0097 
0098     return low | ((uint64_t)high) << 32;
0099 #else
0100     return 0;
0101 #endif
0102 }
0103 
0104 #define NSEC_PER_SEC    1000000000
0105 static int perf_clk_id = CLOCK_MONOTONIC;
0106 
0107 static inline uint64_t
0108 timespec_to_ns(const struct timespec *ts)
0109 {
0110         return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
0111 }
0112 
0113 static inline uint64_t
0114 perf_get_timestamp(void)
0115 {
0116     struct timespec ts;
0117     int ret;
0118 
0119     if (use_arch_timestamp)
0120         return get_arch_timestamp();
0121 
0122     ret = clock_gettime(perf_clk_id, &ts);
0123     if (ret)
0124         return 0;
0125 
0126     return timespec_to_ns(&ts);
0127 }
0128 
0129 static int
0130 create_jit_cache_dir(void)
0131 {
0132     char str[32];
0133     char *base, *p;
0134     struct tm tm;
0135     time_t t;
0136     int ret;
0137 
0138     time(&t);
0139     localtime_r(&t, &tm);
0140 
0141     base = getenv("JITDUMPDIR");
0142     if (!base)
0143         base = getenv("HOME");
0144     if (!base)
0145         base = ".";
0146 
0147     strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
0148 
0149     ret = snprintf(jit_path, PATH_MAX, "%s/.debug/", base);
0150     if (ret >= PATH_MAX) {
0151         warnx("jvmti: cannot generate jit cache dir because %s/.debug/"
0152             " is too long, please check the cwd, JITDUMPDIR, and"
0153             " HOME variables", base);
0154         return -1;
0155     }
0156     ret = mkdir(jit_path, 0755);
0157     if (ret == -1) {
0158         if (errno != EEXIST) {
0159             warn("jvmti: cannot create jit cache dir %s", jit_path);
0160             return -1;
0161         }
0162     }
0163 
0164     ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit", base);
0165     if (ret >= PATH_MAX) {
0166         warnx("jvmti: cannot generate jit cache dir because"
0167             " %s/.debug/jit is too long, please check the cwd,"
0168             " JITDUMPDIR, and HOME variables", base);
0169         return -1;
0170     }
0171     ret = mkdir(jit_path, 0755);
0172     if (ret == -1) {
0173         if (errno != EEXIST) {
0174             warn("jvmti: cannot create jit cache dir %s", jit_path);
0175             return -1;
0176         }
0177     }
0178 
0179     ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit/%s.XXXXXXXX", base, str);
0180     if (ret >= PATH_MAX) {
0181         warnx("jvmti: cannot generate jit cache dir because"
0182             " %s/.debug/jit/%s.XXXXXXXX is too long, please check"
0183             " the cwd, JITDUMPDIR, and HOME variables",
0184             base, str);
0185         return -1;
0186     }
0187     p = mkdtemp(jit_path);
0188     if (p != jit_path) {
0189         warn("jvmti: cannot create jit cache dir %s", jit_path);
0190         return -1;
0191     }
0192 
0193     return 0;
0194 }
0195 
0196 static int
0197 perf_open_marker_file(int fd)
0198 {
0199     long pgsz;
0200 
0201     pgsz = sysconf(_SC_PAGESIZE);
0202     if (pgsz == -1)
0203         return -1;
0204 
0205     /*
0206      * we mmap the jitdump to create an MMAP RECORD in perf.data file.
0207      * The mmap is captured either live (perf record running when we mmap)
0208      * or  in deferred mode, via /proc/PID/maps
0209      * the MMAP record is used as a marker of a jitdump file for more meta
0210      * data info about the jitted code. Perf report/annotate detect this
0211      * special filename and process the jitdump file.
0212      *
0213      * mapping must be PROT_EXEC to ensure it is captured by perf record
0214      * even when not using -d option
0215      */
0216     marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
0217     return (marker_addr == MAP_FAILED) ? -1 : 0;
0218 }
0219 
0220 static void
0221 perf_close_marker_file(void)
0222 {
0223     long pgsz;
0224 
0225     if (!marker_addr)
0226         return;
0227 
0228     pgsz = sysconf(_SC_PAGESIZE);
0229     if (pgsz == -1)
0230         return;
0231 
0232     munmap(marker_addr, pgsz);
0233 }
0234 
0235 static void
0236 init_arch_timestamp(void)
0237 {
0238     char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
0239 
0240     if (!str || !*str || !strcmp(str, "0"))
0241         return;
0242 
0243     use_arch_timestamp = 1;
0244 }
0245 
0246 void *jvmti_open(void)
0247 {
0248     char dump_path[PATH_MAX];
0249     struct jitheader header;
0250     int fd, ret;
0251     FILE *fp;
0252 
0253     init_arch_timestamp();
0254 
0255     /*
0256      * check if clockid is supported
0257      */
0258     if (!perf_get_timestamp()) {
0259         if (use_arch_timestamp)
0260             warnx("jvmti: arch timestamp not supported");
0261         else
0262             warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
0263     }
0264 
0265     memset(&header, 0, sizeof(header));
0266 
0267     /*
0268      * jitdump file dir
0269      */
0270     if (create_jit_cache_dir() < 0)
0271         return NULL;
0272 
0273     /*
0274      * jitdump file name
0275      */
0276     ret = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
0277     if (ret >= PATH_MAX) {
0278         warnx("jvmti: cannot generate jitdump file full path because"
0279             " %s/jit-%i.dump is too long, please check the cwd,"
0280             " JITDUMPDIR, and HOME variables", jit_path, getpid());
0281         return NULL;
0282     }
0283 
0284     fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
0285     if (fd == -1)
0286         return NULL;
0287 
0288     /*
0289      * create perf.data maker for the jitdump file
0290      */
0291     if (perf_open_marker_file(fd)) {
0292         warnx("jvmti: failed to create marker file");
0293         return NULL;
0294     }
0295 
0296     fp = fdopen(fd, "w+");
0297     if (!fp) {
0298         warn("jvmti: cannot create %s", dump_path);
0299         close(fd);
0300         goto error;
0301     }
0302 
0303     warnx("jvmti: jitdump in %s", dump_path);
0304 
0305     if (get_e_machine(&header)) {
0306         warn("get_e_machine failed\n");
0307         goto error;
0308     }
0309 
0310     header.magic      = JITHEADER_MAGIC;
0311     header.version    = JITHEADER_VERSION;
0312     header.total_size = sizeof(header);
0313     header.pid        = getpid();
0314 
0315     header.timestamp = perf_get_timestamp();
0316 
0317     if (use_arch_timestamp)
0318         header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
0319 
0320     if (!fwrite(&header, sizeof(header), 1, fp)) {
0321         warn("jvmti: cannot write dumpfile header");
0322         goto error;
0323     }
0324     return fp;
0325 error:
0326     fclose(fp);
0327     return NULL;
0328 }
0329 
0330 int
0331 jvmti_close(void *agent)
0332 {
0333     struct jr_code_close rec;
0334     FILE *fp = agent;
0335 
0336     if (!fp) {
0337         warnx("jvmti: invalid fd in close_agent");
0338         return -1;
0339     }
0340 
0341     rec.p.id = JIT_CODE_CLOSE;
0342     rec.p.total_size = sizeof(rec);
0343 
0344     rec.p.timestamp = perf_get_timestamp();
0345 
0346     if (!fwrite(&rec, sizeof(rec), 1, fp))
0347         return -1;
0348 
0349     fclose(fp);
0350 
0351     fp = NULL;
0352 
0353     perf_close_marker_file();
0354 
0355     return 0;
0356 }
0357 
0358 int
0359 jvmti_write_code(void *agent, char const *sym,
0360     uint64_t vma, void const *code, unsigned int const size)
0361 {
0362     static int code_generation = 1;
0363     struct jr_code_load rec;
0364     size_t sym_len;
0365     FILE *fp = agent;
0366     int ret = -1;
0367 
0368     /* don't care about 0 length function, no samples */
0369     if (size == 0)
0370         return 0;
0371 
0372     if (!fp) {
0373         warnx("jvmti: invalid fd in write_native_code");
0374         return -1;
0375     }
0376 
0377     sym_len = strlen(sym) + 1;
0378 
0379     rec.p.id           = JIT_CODE_LOAD;
0380     rec.p.total_size   = sizeof(rec) + sym_len;
0381     rec.p.timestamp    = perf_get_timestamp();
0382 
0383     rec.code_size  = size;
0384     rec.vma        = vma;
0385     rec.code_addr  = vma;
0386     rec.pid        = getpid();
0387     rec.tid        = gettid();
0388 
0389     if (code)
0390         rec.p.total_size += size;
0391 
0392     /*
0393      * If JVM is multi-threaded, multiple concurrent calls to agent
0394      * may be possible, so protect file writes
0395      */
0396     flockfile(fp);
0397 
0398     /*
0399      * get code index inside lock to avoid race condition
0400      */
0401     rec.code_index = code_generation++;
0402 
0403     ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
0404     fwrite_unlocked(sym, sym_len, 1, fp);
0405 
0406     if (code)
0407         fwrite_unlocked(code, size, 1, fp);
0408 
0409     funlockfile(fp);
0410 
0411     ret = 0;
0412 
0413     return ret;
0414 }
0415 
0416 int
0417 jvmti_write_debug_info(void *agent, uint64_t code,
0418     int nr_lines, jvmti_line_info_t *li,
0419     const char * const * file_names)
0420 {
0421     struct jr_code_debug_info rec;
0422     size_t sret, len, size, flen = 0;
0423     uint64_t addr;
0424     FILE *fp = agent;
0425     int i;
0426 
0427     /*
0428      * no entry to write
0429      */
0430     if (!nr_lines)
0431         return 0;
0432 
0433     if (!fp) {
0434         warnx("jvmti: invalid fd in write_debug_info");
0435         return -1;
0436     }
0437 
0438     for (i = 0; i < nr_lines; ++i) {
0439         flen += strlen(file_names[i]) + 1;
0440     }
0441 
0442     rec.p.id        = JIT_CODE_DEBUG_INFO;
0443     size            = sizeof(rec);
0444     rec.p.timestamp = perf_get_timestamp();
0445     rec.code_addr   = (uint64_t)(uintptr_t)code;
0446     rec.nr_entry    = nr_lines;
0447 
0448     /*
0449      * on disk source line info layout:
0450      * uint64_t : addr
0451      * int      : line number
0452      * int      : column discriminator
0453      * file[]   : source file name
0454      */
0455     size += nr_lines * sizeof(struct debug_entry);
0456     size += flen;
0457     rec.p.total_size = size;
0458 
0459     /*
0460      * If JVM is multi-threaded, multiple concurrent calls to agent
0461      * may be possible, so protect file writes
0462      */
0463     flockfile(fp);
0464 
0465     sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
0466     if (sret != 1)
0467         goto error;
0468 
0469     for (i = 0; i < nr_lines; i++) {
0470 
0471         addr = (uint64_t)li[i].pc;
0472         len  = sizeof(addr);
0473         sret = fwrite_unlocked(&addr, len, 1, fp);
0474         if (sret != 1)
0475             goto error;
0476 
0477         len  = sizeof(li[0].line_number);
0478         sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
0479         if (sret != 1)
0480             goto error;
0481 
0482         len  = sizeof(li[0].discrim);
0483         sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
0484         if (sret != 1)
0485             goto error;
0486 
0487         sret = fwrite_unlocked(file_names[i], strlen(file_names[i]) + 1, 1, fp);
0488         if (sret != 1)
0489             goto error;
0490     }
0491     funlockfile(fp);
0492     return 0;
0493 error:
0494     funlockfile(fp);
0495     return -1;
0496 }