1209045adSStephane Eranian /* 2209045adSStephane Eranian * jvmti_agent.c: JVMTI agent interface 3209045adSStephane Eranian * 4209045adSStephane Eranian * Adapted from the Oprofile code in opagent.c: 5209045adSStephane Eranian * This library is free software; you can redistribute it and/or 6209045adSStephane Eranian * modify it under the terms of the GNU Lesser General Public 7209045adSStephane Eranian * License as published by the Free Software Foundation; either 8209045adSStephane Eranian * version 2.1 of the License, or (at your option) any later version. 9209045adSStephane Eranian * 10209045adSStephane Eranian * This library is distributed in the hope that it will be useful, 11209045adSStephane Eranian * but WITHOUT ANY WARRANTY; without even the implied warranty of 12209045adSStephane Eranian * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13209045adSStephane Eranian * Lesser General Public License for more details. 14209045adSStephane Eranian * 15209045adSStephane Eranian * You should have received a copy of the GNU Lesser General Public 16209045adSStephane Eranian * License along with this library; if not, write to the Free Software 17209045adSStephane Eranian * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18209045adSStephane Eranian * 19209045adSStephane Eranian * Copyright 2007 OProfile authors 20209045adSStephane Eranian * Jens Wilke 21209045adSStephane Eranian * Daniel Hansel 22209045adSStephane Eranian * Copyright IBM Corporation 2007 23209045adSStephane Eranian */ 24209045adSStephane Eranian #include <sys/types.h> 25209045adSStephane Eranian #include <sys/stat.h> /* for mkdir() */ 26209045adSStephane Eranian #include <stdio.h> 27209045adSStephane Eranian #include <errno.h> 28209045adSStephane Eranian #include <string.h> 29209045adSStephane Eranian #include <stdlib.h> 30209045adSStephane Eranian #include <stdint.h> 31209045adSStephane Eranian #include <limits.h> 32209045adSStephane Eranian #include <fcntl.h> 33209045adSStephane Eranian #include <unistd.h> 34209045adSStephane Eranian #include <time.h> 35209045adSStephane Eranian #include <sys/mman.h> 36209045adSStephane Eranian #include <syscall.h> /* for gettid() */ 37209045adSStephane Eranian #include <err.h> 38a09603f8SJiri Olsa #include <linux/kernel.h> 39209045adSStephane Eranian 40209045adSStephane Eranian #include "jvmti_agent.h" 41209045adSStephane Eranian #include "../util/jitdump.h" 42209045adSStephane Eranian 43209045adSStephane Eranian #define JIT_LANG "java" 44209045adSStephane Eranian 45209045adSStephane Eranian static char jit_path[PATH_MAX]; 46209045adSStephane Eranian static void *marker_addr; 47209045adSStephane Eranian 48209045adSStephane Eranian static inline pid_t gettid(void) 49209045adSStephane Eranian { 50209045adSStephane Eranian return (pid_t)syscall(__NR_gettid); 51209045adSStephane Eranian } 52209045adSStephane Eranian 53209045adSStephane Eranian static int get_e_machine(struct jitheader *hdr) 54209045adSStephane Eranian { 55209045adSStephane Eranian ssize_t sret; 56209045adSStephane Eranian char id[16]; 57209045adSStephane Eranian int fd, ret = -1; 58209045adSStephane Eranian struct { 59209045adSStephane Eranian uint16_t e_type; 60209045adSStephane Eranian uint16_t e_machine; 61209045adSStephane Eranian } info; 62209045adSStephane Eranian 63209045adSStephane Eranian fd = open("/proc/self/exe", O_RDONLY); 64209045adSStephane Eranian if (fd == -1) 65209045adSStephane Eranian return -1; 66209045adSStephane Eranian 67209045adSStephane Eranian sret = read(fd, id, sizeof(id)); 68209045adSStephane Eranian if (sret != sizeof(id)) 69209045adSStephane Eranian goto error; 70209045adSStephane Eranian 71209045adSStephane Eranian /* check ELF signature */ 72209045adSStephane Eranian if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F') 73209045adSStephane Eranian goto error; 74209045adSStephane Eranian 75209045adSStephane Eranian sret = read(fd, &info, sizeof(info)); 76209045adSStephane Eranian if (sret != sizeof(info)) 77209045adSStephane Eranian goto error; 78209045adSStephane Eranian 799fcfcdf3SDan Carpenter hdr->elf_mach = info.e_machine; 80209045adSStephane Eranian ret = 0; 81209045adSStephane Eranian error: 82209045adSStephane Eranian close(fd); 83209045adSStephane Eranian return ret; 84209045adSStephane Eranian } 85209045adSStephane Eranian 862a28e230SAdrian Hunter static int use_arch_timestamp; 872a28e230SAdrian Hunter 882a28e230SAdrian Hunter static inline uint64_t 892a28e230SAdrian Hunter get_arch_timestamp(void) 902a28e230SAdrian Hunter { 912a28e230SAdrian Hunter #if defined(__i386__) || defined(__x86_64__) 922a28e230SAdrian Hunter unsigned int low, high; 932a28e230SAdrian Hunter 942a28e230SAdrian Hunter asm volatile("rdtsc" : "=a" (low), "=d" (high)); 952a28e230SAdrian Hunter 962a28e230SAdrian Hunter return low | ((uint64_t)high) << 32; 972a28e230SAdrian Hunter #else 982a28e230SAdrian Hunter return 0; 992a28e230SAdrian Hunter #endif 1002a28e230SAdrian Hunter } 1012a28e230SAdrian Hunter 102209045adSStephane Eranian #define NSEC_PER_SEC 1000000000 103209045adSStephane Eranian static int perf_clk_id = CLOCK_MONOTONIC; 104209045adSStephane Eranian 105209045adSStephane Eranian static inline uint64_t 106209045adSStephane Eranian timespec_to_ns(const struct timespec *ts) 107209045adSStephane Eranian { 108209045adSStephane Eranian return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 109209045adSStephane Eranian } 110209045adSStephane Eranian 111209045adSStephane Eranian static inline uint64_t 112209045adSStephane Eranian perf_get_timestamp(void) 113209045adSStephane Eranian { 114209045adSStephane Eranian struct timespec ts; 115209045adSStephane Eranian int ret; 116209045adSStephane Eranian 1172a28e230SAdrian Hunter if (use_arch_timestamp) 1182a28e230SAdrian Hunter return get_arch_timestamp(); 1192a28e230SAdrian Hunter 120209045adSStephane Eranian ret = clock_gettime(perf_clk_id, &ts); 121209045adSStephane Eranian if (ret) 122209045adSStephane Eranian return 0; 123209045adSStephane Eranian 124209045adSStephane Eranian return timespec_to_ns(&ts); 125209045adSStephane Eranian } 126209045adSStephane Eranian 127209045adSStephane Eranian static int 128*6ac22262SGustavo Romero create_jit_cache_dir(void) 129209045adSStephane Eranian { 130209045adSStephane Eranian char str[32]; 131209045adSStephane Eranian char *base, *p; 132209045adSStephane Eranian struct tm tm; 133209045adSStephane Eranian time_t t; 134209045adSStephane Eranian int ret; 135209045adSStephane Eranian 136209045adSStephane Eranian time(&t); 137209045adSStephane Eranian localtime_r(&t, &tm); 138209045adSStephane Eranian 139209045adSStephane Eranian base = getenv("JITDUMPDIR"); 140209045adSStephane Eranian if (!base) 141209045adSStephane Eranian base = getenv("HOME"); 142209045adSStephane Eranian if (!base) 143209045adSStephane Eranian base = "."; 144209045adSStephane Eranian 145209045adSStephane Eranian strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm); 146209045adSStephane Eranian 147*6ac22262SGustavo Romero ret = snprintf(jit_path, PATH_MAX, "%s/.debug/", base); 148*6ac22262SGustavo Romero if (ret >= PATH_MAX) { 149*6ac22262SGustavo Romero warnx("jvmti: cannot generate jit cache dir because %s/.debug/" 150*6ac22262SGustavo Romero " is too long, please check the cwd, JITDUMPDIR, and" 151*6ac22262SGustavo Romero " HOME variables", base); 152*6ac22262SGustavo Romero return -1; 153*6ac22262SGustavo Romero } 154209045adSStephane Eranian ret = mkdir(jit_path, 0755); 155209045adSStephane Eranian if (ret == -1) { 156209045adSStephane Eranian if (errno != EEXIST) { 157209045adSStephane Eranian warn("jvmti: cannot create jit cache dir %s", jit_path); 158209045adSStephane Eranian return -1; 159209045adSStephane Eranian } 160209045adSStephane Eranian } 161209045adSStephane Eranian 162*6ac22262SGustavo Romero ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit", base); 163*6ac22262SGustavo Romero if (ret >= PATH_MAX) { 164*6ac22262SGustavo Romero warnx("jvmti: cannot generate jit cache dir because" 165*6ac22262SGustavo Romero " %s/.debug/jit is too long, please check the cwd," 166*6ac22262SGustavo Romero " JITDUMPDIR, and HOME variables", base); 167*6ac22262SGustavo Romero return -1; 168*6ac22262SGustavo Romero } 169209045adSStephane Eranian ret = mkdir(jit_path, 0755); 170209045adSStephane Eranian if (ret == -1) { 171209045adSStephane Eranian if (errno != EEXIST) { 172*6ac22262SGustavo Romero warn("jvmti: cannot create jit cache dir %s", jit_path); 173209045adSStephane Eranian return -1; 174209045adSStephane Eranian } 175209045adSStephane Eranian } 176209045adSStephane Eranian 177*6ac22262SGustavo Romero ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit/%s.XXXXXXXX", base, str); 178*6ac22262SGustavo Romero if (ret >= PATH_MAX) { 179*6ac22262SGustavo Romero warnx("jvmti: cannot generate jit cache dir because" 180*6ac22262SGustavo Romero " %s/.debug/jit/%s.XXXXXXXX is too long, please check" 181*6ac22262SGustavo Romero " the cwd, JITDUMPDIR, and HOME variables", 182*6ac22262SGustavo Romero base, str); 183*6ac22262SGustavo Romero return -1; 184*6ac22262SGustavo Romero } 185209045adSStephane Eranian p = mkdtemp(jit_path); 186209045adSStephane Eranian if (p != jit_path) { 187*6ac22262SGustavo Romero warn("jvmti: cannot create jit cache dir %s", jit_path); 188209045adSStephane Eranian return -1; 189209045adSStephane Eranian } 190209045adSStephane Eranian 191209045adSStephane Eranian return 0; 192209045adSStephane Eranian } 193209045adSStephane Eranian 194209045adSStephane Eranian static int 195209045adSStephane Eranian perf_open_marker_file(int fd) 196209045adSStephane Eranian { 197209045adSStephane Eranian long pgsz; 198209045adSStephane Eranian 199209045adSStephane Eranian pgsz = sysconf(_SC_PAGESIZE); 200209045adSStephane Eranian if (pgsz == -1) 201209045adSStephane Eranian return -1; 202209045adSStephane Eranian 203209045adSStephane Eranian /* 204209045adSStephane Eranian * we mmap the jitdump to create an MMAP RECORD in perf.data file. 205209045adSStephane Eranian * The mmap is captured either live (perf record running when we mmap) 206209045adSStephane Eranian * or in deferred mode, via /proc/PID/maps 207209045adSStephane Eranian * the MMAP record is used as a marker of a jitdump file for more meta 208209045adSStephane Eranian * data info about the jitted code. Perf report/annotate detect this 209209045adSStephane Eranian * special filename and process the jitdump file. 210209045adSStephane Eranian * 211209045adSStephane Eranian * mapping must be PROT_EXEC to ensure it is captured by perf record 212209045adSStephane Eranian * even when not using -d option 213209045adSStephane Eranian */ 214209045adSStephane Eranian marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0); 215209045adSStephane Eranian return (marker_addr == MAP_FAILED) ? -1 : 0; 216209045adSStephane Eranian } 217209045adSStephane Eranian 218209045adSStephane Eranian static void 219209045adSStephane Eranian perf_close_marker_file(void) 220209045adSStephane Eranian { 221209045adSStephane Eranian long pgsz; 222209045adSStephane Eranian 223209045adSStephane Eranian if (!marker_addr) 224209045adSStephane Eranian return; 225209045adSStephane Eranian 226209045adSStephane Eranian pgsz = sysconf(_SC_PAGESIZE); 227209045adSStephane Eranian if (pgsz == -1) 228209045adSStephane Eranian return; 229209045adSStephane Eranian 230209045adSStephane Eranian munmap(marker_addr, pgsz); 231209045adSStephane Eranian } 232209045adSStephane Eranian 2332a28e230SAdrian Hunter static void 2342a28e230SAdrian Hunter init_arch_timestamp(void) 2352a28e230SAdrian Hunter { 2362a28e230SAdrian Hunter char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP"); 2372a28e230SAdrian Hunter 2382a28e230SAdrian Hunter if (!str || !*str || !strcmp(str, "0")) 2392a28e230SAdrian Hunter return; 2402a28e230SAdrian Hunter 2412a28e230SAdrian Hunter use_arch_timestamp = 1; 2422a28e230SAdrian Hunter } 2432a28e230SAdrian Hunter 244209045adSStephane Eranian void *jvmti_open(void) 245209045adSStephane Eranian { 246209045adSStephane Eranian char dump_path[PATH_MAX]; 247209045adSStephane Eranian struct jitheader header; 248*6ac22262SGustavo Romero int fd, ret; 249209045adSStephane Eranian FILE *fp; 250209045adSStephane Eranian 2512a28e230SAdrian Hunter init_arch_timestamp(); 2522a28e230SAdrian Hunter 253209045adSStephane Eranian /* 254209045adSStephane Eranian * check if clockid is supported 255209045adSStephane Eranian */ 2562a28e230SAdrian Hunter if (!perf_get_timestamp()) { 2572a28e230SAdrian Hunter if (use_arch_timestamp) 2582a28e230SAdrian Hunter warnx("jvmti: arch timestamp not supported"); 2592a28e230SAdrian Hunter else 260209045adSStephane Eranian warnx("jvmti: kernel does not support %d clock id", perf_clk_id); 2612a28e230SAdrian Hunter } 262209045adSStephane Eranian 263209045adSStephane Eranian memset(&header, 0, sizeof(header)); 264209045adSStephane Eranian 265*6ac22262SGustavo Romero /* 266*6ac22262SGustavo Romero * jitdump file dir 267*6ac22262SGustavo Romero */ 268*6ac22262SGustavo Romero if (create_jit_cache_dir() < 0) 269*6ac22262SGustavo Romero return NULL; 270209045adSStephane Eranian 271209045adSStephane Eranian /* 272209045adSStephane Eranian * jitdump file name 273209045adSStephane Eranian */ 274*6ac22262SGustavo Romero ret = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid()); 275*6ac22262SGustavo Romero if (ret >= PATH_MAX) { 276*6ac22262SGustavo Romero warnx("jvmti: cannot generate jitdump file full path because" 277*6ac22262SGustavo Romero " %s/jit-%i.dump is too long, please check the cwd," 278*6ac22262SGustavo Romero " JITDUMPDIR, and HOME variables", jit_path, getpid()); 279*6ac22262SGustavo Romero return NULL; 280*6ac22262SGustavo Romero } 281209045adSStephane Eranian 282209045adSStephane Eranian fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666); 283209045adSStephane Eranian if (fd == -1) 284209045adSStephane Eranian return NULL; 285209045adSStephane Eranian 286209045adSStephane Eranian /* 287209045adSStephane Eranian * create perf.data maker for the jitdump file 288209045adSStephane Eranian */ 289209045adSStephane Eranian if (perf_open_marker_file(fd)) { 290209045adSStephane Eranian warnx("jvmti: failed to create marker file"); 291209045adSStephane Eranian return NULL; 292209045adSStephane Eranian } 293209045adSStephane Eranian 294209045adSStephane Eranian fp = fdopen(fd, "w+"); 295209045adSStephane Eranian if (!fp) { 296209045adSStephane Eranian warn("jvmti: cannot create %s", dump_path); 297209045adSStephane Eranian close(fd); 298209045adSStephane Eranian goto error; 299209045adSStephane Eranian } 300209045adSStephane Eranian 301209045adSStephane Eranian warnx("jvmti: jitdump in %s", dump_path); 302209045adSStephane Eranian 303209045adSStephane Eranian if (get_e_machine(&header)) { 304209045adSStephane Eranian warn("get_e_machine failed\n"); 305209045adSStephane Eranian goto error; 306209045adSStephane Eranian } 307209045adSStephane Eranian 308209045adSStephane Eranian header.magic = JITHEADER_MAGIC; 309209045adSStephane Eranian header.version = JITHEADER_VERSION; 310209045adSStephane Eranian header.total_size = sizeof(header); 311209045adSStephane Eranian header.pid = getpid(); 312209045adSStephane Eranian 313209045adSStephane Eranian header.timestamp = perf_get_timestamp(); 314209045adSStephane Eranian 3152a28e230SAdrian Hunter if (use_arch_timestamp) 3162a28e230SAdrian Hunter header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP; 3172a28e230SAdrian Hunter 318209045adSStephane Eranian if (!fwrite(&header, sizeof(header), 1, fp)) { 319209045adSStephane Eranian warn("jvmti: cannot write dumpfile header"); 320209045adSStephane Eranian goto error; 321209045adSStephane Eranian } 322209045adSStephane Eranian return fp; 323209045adSStephane Eranian error: 324209045adSStephane Eranian fclose(fp); 325209045adSStephane Eranian return NULL; 326209045adSStephane Eranian } 327209045adSStephane Eranian 328209045adSStephane Eranian int 329209045adSStephane Eranian jvmti_close(void *agent) 330209045adSStephane Eranian { 331209045adSStephane Eranian struct jr_code_close rec; 332209045adSStephane Eranian FILE *fp = agent; 333209045adSStephane Eranian 334209045adSStephane Eranian if (!fp) { 33519f0edb9SColin Ian King warnx("jvmti: invalid fd in close_agent"); 336209045adSStephane Eranian return -1; 337209045adSStephane Eranian } 338209045adSStephane Eranian 339209045adSStephane Eranian rec.p.id = JIT_CODE_CLOSE; 340209045adSStephane Eranian rec.p.total_size = sizeof(rec); 341209045adSStephane Eranian 342209045adSStephane Eranian rec.p.timestamp = perf_get_timestamp(); 343209045adSStephane Eranian 344209045adSStephane Eranian if (!fwrite(&rec, sizeof(rec), 1, fp)) 345209045adSStephane Eranian return -1; 346209045adSStephane Eranian 347209045adSStephane Eranian fclose(fp); 348209045adSStephane Eranian 349209045adSStephane Eranian fp = NULL; 350209045adSStephane Eranian 351209045adSStephane Eranian perf_close_marker_file(); 352209045adSStephane Eranian 353209045adSStephane Eranian return 0; 354209045adSStephane Eranian } 355209045adSStephane Eranian 356209045adSStephane Eranian int 357209045adSStephane Eranian jvmti_write_code(void *agent, char const *sym, 358209045adSStephane Eranian uint64_t vma, void const *code, unsigned int const size) 359209045adSStephane Eranian { 360209045adSStephane Eranian static int code_generation = 1; 361209045adSStephane Eranian struct jr_code_load rec; 362209045adSStephane Eranian size_t sym_len; 363209045adSStephane Eranian FILE *fp = agent; 364209045adSStephane Eranian int ret = -1; 365209045adSStephane Eranian 366209045adSStephane Eranian /* don't care about 0 length function, no samples */ 367209045adSStephane Eranian if (size == 0) 368209045adSStephane Eranian return 0; 369209045adSStephane Eranian 370209045adSStephane Eranian if (!fp) { 371209045adSStephane Eranian warnx("jvmti: invalid fd in write_native_code"); 372209045adSStephane Eranian return -1; 373209045adSStephane Eranian } 374209045adSStephane Eranian 375209045adSStephane Eranian sym_len = strlen(sym) + 1; 376209045adSStephane Eranian 377209045adSStephane Eranian rec.p.id = JIT_CODE_LOAD; 378209045adSStephane Eranian rec.p.total_size = sizeof(rec) + sym_len; 379209045adSStephane Eranian rec.p.timestamp = perf_get_timestamp(); 380209045adSStephane Eranian 381209045adSStephane Eranian rec.code_size = size; 382209045adSStephane Eranian rec.vma = vma; 383209045adSStephane Eranian rec.code_addr = vma; 384209045adSStephane Eranian rec.pid = getpid(); 385209045adSStephane Eranian rec.tid = gettid(); 386209045adSStephane Eranian 387209045adSStephane Eranian if (code) 388209045adSStephane Eranian rec.p.total_size += size; 389209045adSStephane Eranian 390209045adSStephane Eranian /* 391209045adSStephane Eranian * If JVM is multi-threaded, nultiple concurrent calls to agent 392209045adSStephane Eranian * may be possible, so protect file writes 393209045adSStephane Eranian */ 394209045adSStephane Eranian flockfile(fp); 395209045adSStephane Eranian 396209045adSStephane Eranian /* 397209045adSStephane Eranian * get code index inside lock to avoid race condition 398209045adSStephane Eranian */ 399209045adSStephane Eranian rec.code_index = code_generation++; 400209045adSStephane Eranian 401209045adSStephane Eranian ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp); 402209045adSStephane Eranian fwrite_unlocked(sym, sym_len, 1, fp); 403209045adSStephane Eranian 404209045adSStephane Eranian if (code) 405209045adSStephane Eranian fwrite_unlocked(code, size, 1, fp); 406209045adSStephane Eranian 407209045adSStephane Eranian funlockfile(fp); 408209045adSStephane Eranian 409209045adSStephane Eranian ret = 0; 410209045adSStephane Eranian 411209045adSStephane Eranian return ret; 412209045adSStephane Eranian } 413209045adSStephane Eranian 414209045adSStephane Eranian int 415ca58d7e6SBen Gainey jvmti_write_debug_info(void *agent, uint64_t code, 416ca58d7e6SBen Gainey int nr_lines, jvmti_line_info_t *li, 417ca58d7e6SBen Gainey const char * const * file_names) 418209045adSStephane Eranian { 419209045adSStephane Eranian struct jr_code_debug_info rec; 420ca58d7e6SBen Gainey size_t sret, len, size, flen = 0; 421598b7c69SStephane Eranian uint64_t addr; 422209045adSStephane Eranian FILE *fp = agent; 423209045adSStephane Eranian int i; 424209045adSStephane Eranian 425209045adSStephane Eranian /* 426209045adSStephane Eranian * no entry to write 427209045adSStephane Eranian */ 428598b7c69SStephane Eranian if (!nr_lines) 429209045adSStephane Eranian return 0; 430209045adSStephane Eranian 431209045adSStephane Eranian if (!fp) { 432209045adSStephane Eranian warnx("jvmti: invalid fd in write_debug_info"); 433209045adSStephane Eranian return -1; 434209045adSStephane Eranian } 435209045adSStephane Eranian 436ca58d7e6SBen Gainey for (i = 0; i < nr_lines; ++i) { 437ca58d7e6SBen Gainey flen += strlen(file_names[i]) + 1; 438ca58d7e6SBen Gainey } 439209045adSStephane Eranian 440209045adSStephane Eranian rec.p.id = JIT_CODE_DEBUG_INFO; 441209045adSStephane Eranian size = sizeof(rec); 442209045adSStephane Eranian rec.p.timestamp = perf_get_timestamp(); 443209045adSStephane Eranian rec.code_addr = (uint64_t)(uintptr_t)code; 444598b7c69SStephane Eranian rec.nr_entry = nr_lines; 445209045adSStephane Eranian 446209045adSStephane Eranian /* 447209045adSStephane Eranian * on disk source line info layout: 448209045adSStephane Eranian * uint64_t : addr 449209045adSStephane Eranian * int : line number 450598b7c69SStephane Eranian * int : column discriminator 451209045adSStephane Eranian * file[] : source file name 452209045adSStephane Eranian */ 453598b7c69SStephane Eranian size += nr_lines * sizeof(struct debug_entry); 454ca58d7e6SBen Gainey size += flen; 45513b9012aSStephane Eranian rec.p.total_size = size; 456209045adSStephane Eranian 457209045adSStephane Eranian /* 458209045adSStephane Eranian * If JVM is multi-threaded, nultiple concurrent calls to agent 459209045adSStephane Eranian * may be possible, so protect file writes 460209045adSStephane Eranian */ 461209045adSStephane Eranian flockfile(fp); 462209045adSStephane Eranian 463209045adSStephane Eranian sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp); 464209045adSStephane Eranian if (sret != 1) 465209045adSStephane Eranian goto error; 466209045adSStephane Eranian 467598b7c69SStephane Eranian for (i = 0; i < nr_lines; i++) { 468209045adSStephane Eranian 469598b7c69SStephane Eranian addr = (uint64_t)li[i].pc; 470209045adSStephane Eranian len = sizeof(addr); 471209045adSStephane Eranian sret = fwrite_unlocked(&addr, len, 1, fp); 472209045adSStephane Eranian if (sret != 1) 473209045adSStephane Eranian goto error; 474209045adSStephane Eranian 475598b7c69SStephane Eranian len = sizeof(li[0].line_number); 476209045adSStephane Eranian sret = fwrite_unlocked(&li[i].line_number, len, 1, fp); 477209045adSStephane Eranian if (sret != 1) 478209045adSStephane Eranian goto error; 479209045adSStephane Eranian 480598b7c69SStephane Eranian len = sizeof(li[0].discrim); 481598b7c69SStephane Eranian sret = fwrite_unlocked(&li[i].discrim, len, 1, fp); 482209045adSStephane Eranian if (sret != 1) 483209045adSStephane Eranian goto error; 484209045adSStephane Eranian 485ca58d7e6SBen Gainey sret = fwrite_unlocked(file_names[i], strlen(file_names[i]) + 1, 1, fp); 486598b7c69SStephane Eranian if (sret != 1) 487598b7c69SStephane Eranian goto error; 488209045adSStephane Eranian } 489209045adSStephane Eranian funlockfile(fp); 490209045adSStephane Eranian return 0; 491209045adSStephane Eranian error: 492209045adSStephane Eranian funlockfile(fp); 493209045adSStephane Eranian return -1; 494209045adSStephane Eranian } 495