xref: /linux/tools/perf/jvmti/jvmti_agent.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
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 
484541a8bbSArnaldo Carvalho de Melo #ifndef HAVE_GETTID
gettid(void)49209045adSStephane Eranian static inline pid_t gettid(void)
50209045adSStephane Eranian {
51209045adSStephane Eranian 	return (pid_t)syscall(__NR_gettid);
52209045adSStephane Eranian }
534541a8bbSArnaldo Carvalho de Melo #endif
54209045adSStephane Eranian 
get_e_machine(struct jitheader * hdr)55209045adSStephane Eranian static int get_e_machine(struct jitheader *hdr)
56209045adSStephane Eranian {
57209045adSStephane Eranian 	ssize_t sret;
58209045adSStephane Eranian 	char id[16];
59209045adSStephane Eranian 	int fd, ret = -1;
60209045adSStephane Eranian 	struct {
61209045adSStephane Eranian 		uint16_t e_type;
62209045adSStephane Eranian 		uint16_t e_machine;
63209045adSStephane Eranian 	} info;
64209045adSStephane Eranian 
65209045adSStephane Eranian 	fd = open("/proc/self/exe", O_RDONLY);
66209045adSStephane Eranian 	if (fd == -1)
67209045adSStephane Eranian 		return -1;
68209045adSStephane Eranian 
69209045adSStephane Eranian 	sret = read(fd, id, sizeof(id));
70209045adSStephane Eranian 	if (sret != sizeof(id))
71209045adSStephane Eranian 		goto error;
72209045adSStephane Eranian 
73209045adSStephane Eranian 	/* check ELF signature */
74209045adSStephane Eranian 	if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
75209045adSStephane Eranian 		goto error;
76209045adSStephane Eranian 
77209045adSStephane Eranian 	sret = read(fd, &info, sizeof(info));
78209045adSStephane Eranian 	if (sret != sizeof(info))
79209045adSStephane Eranian 		goto error;
80209045adSStephane Eranian 
819fcfcdf3SDan Carpenter 	hdr->elf_mach = info.e_machine;
82209045adSStephane Eranian 	ret = 0;
83209045adSStephane Eranian error:
84209045adSStephane Eranian 	close(fd);
85209045adSStephane Eranian 	return ret;
86209045adSStephane Eranian }
87209045adSStephane Eranian 
882a28e230SAdrian Hunter static int use_arch_timestamp;
892a28e230SAdrian Hunter 
902a28e230SAdrian Hunter static inline uint64_t
get_arch_timestamp(void)912a28e230SAdrian Hunter get_arch_timestamp(void)
922a28e230SAdrian Hunter {
932a28e230SAdrian Hunter #if defined(__i386__) || defined(__x86_64__)
942a28e230SAdrian Hunter 	unsigned int low, high;
952a28e230SAdrian Hunter 
962a28e230SAdrian Hunter 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
972a28e230SAdrian Hunter 
982a28e230SAdrian Hunter 	return low | ((uint64_t)high) << 32;
992a28e230SAdrian Hunter #else
1002a28e230SAdrian Hunter 	return 0;
1012a28e230SAdrian Hunter #endif
1022a28e230SAdrian Hunter }
1032a28e230SAdrian Hunter 
104209045adSStephane Eranian #define NSEC_PER_SEC	1000000000
105209045adSStephane Eranian static int perf_clk_id = CLOCK_MONOTONIC;
106209045adSStephane Eranian 
107209045adSStephane Eranian static inline uint64_t
timespec_to_ns(const struct timespec * ts)108209045adSStephane Eranian timespec_to_ns(const struct timespec *ts)
109209045adSStephane Eranian {
110209045adSStephane Eranian         return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
111209045adSStephane Eranian }
112209045adSStephane Eranian 
113209045adSStephane Eranian static inline uint64_t
perf_get_timestamp(void)114209045adSStephane Eranian perf_get_timestamp(void)
115209045adSStephane Eranian {
116209045adSStephane Eranian 	struct timespec ts;
117209045adSStephane Eranian 	int ret;
118209045adSStephane Eranian 
1192a28e230SAdrian Hunter 	if (use_arch_timestamp)
1202a28e230SAdrian Hunter 		return get_arch_timestamp();
1212a28e230SAdrian Hunter 
122209045adSStephane Eranian 	ret = clock_gettime(perf_clk_id, &ts);
123209045adSStephane Eranian 	if (ret)
124209045adSStephane Eranian 		return 0;
125209045adSStephane Eranian 
126209045adSStephane Eranian 	return timespec_to_ns(&ts);
127209045adSStephane Eranian }
128209045adSStephane Eranian 
129209045adSStephane Eranian static int
create_jit_cache_dir(void)1306ac22262SGustavo Romero create_jit_cache_dir(void)
131209045adSStephane Eranian {
132209045adSStephane Eranian 	char str[32];
133209045adSStephane Eranian 	char *base, *p;
134209045adSStephane Eranian 	struct tm tm;
135209045adSStephane Eranian 	time_t t;
136209045adSStephane Eranian 	int ret;
137209045adSStephane Eranian 
138209045adSStephane Eranian 	time(&t);
139209045adSStephane Eranian 	localtime_r(&t, &tm);
140209045adSStephane Eranian 
141209045adSStephane Eranian 	base = getenv("JITDUMPDIR");
142209045adSStephane Eranian 	if (!base)
143209045adSStephane Eranian 		base = getenv("HOME");
144209045adSStephane Eranian 	if (!base)
145209045adSStephane Eranian 		base = ".";
146209045adSStephane Eranian 
147209045adSStephane Eranian 	strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
148209045adSStephane Eranian 
1496ac22262SGustavo Romero 	ret = snprintf(jit_path, PATH_MAX, "%s/.debug/", base);
1506ac22262SGustavo Romero 	if (ret >= PATH_MAX) {
1516ac22262SGustavo Romero 		warnx("jvmti: cannot generate jit cache dir because %s/.debug/"
1526ac22262SGustavo Romero 			" is too long, please check the cwd, JITDUMPDIR, and"
1536ac22262SGustavo Romero 			" HOME variables", base);
1546ac22262SGustavo Romero 		return -1;
1556ac22262SGustavo Romero 	}
156209045adSStephane Eranian 	ret = mkdir(jit_path, 0755);
157209045adSStephane Eranian 	if (ret == -1) {
158209045adSStephane Eranian 		if (errno != EEXIST) {
159209045adSStephane Eranian 			warn("jvmti: cannot create jit cache dir %s", jit_path);
160209045adSStephane Eranian 			return -1;
161209045adSStephane Eranian 		}
162209045adSStephane Eranian 	}
163209045adSStephane Eranian 
1646ac22262SGustavo Romero 	ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit", base);
1656ac22262SGustavo Romero 	if (ret >= PATH_MAX) {
1666ac22262SGustavo Romero 		warnx("jvmti: cannot generate jit cache dir because"
1676ac22262SGustavo Romero 			" %s/.debug/jit is too long, please check the cwd,"
1686ac22262SGustavo Romero 			" JITDUMPDIR, and HOME variables", base);
1696ac22262SGustavo Romero 		return -1;
1706ac22262SGustavo Romero 	}
171209045adSStephane Eranian 	ret = mkdir(jit_path, 0755);
172209045adSStephane Eranian 	if (ret == -1) {
173209045adSStephane Eranian 		if (errno != EEXIST) {
1746ac22262SGustavo Romero 			warn("jvmti: cannot create jit cache dir %s", jit_path);
175209045adSStephane Eranian 			return -1;
176209045adSStephane Eranian 		}
177209045adSStephane Eranian 	}
178209045adSStephane Eranian 
1796ac22262SGustavo Romero 	ret = snprintf(jit_path, PATH_MAX, "%s/.debug/jit/%s.XXXXXXXX", base, str);
1806ac22262SGustavo Romero 	if (ret >= PATH_MAX) {
1816ac22262SGustavo Romero 		warnx("jvmti: cannot generate jit cache dir because"
1826ac22262SGustavo Romero 			" %s/.debug/jit/%s.XXXXXXXX is too long, please check"
1836ac22262SGustavo Romero 			" the cwd, JITDUMPDIR, and HOME variables",
1846ac22262SGustavo Romero 			base, str);
1856ac22262SGustavo Romero 		return -1;
1866ac22262SGustavo Romero 	}
187209045adSStephane Eranian 	p = mkdtemp(jit_path);
188209045adSStephane Eranian 	if (p != jit_path) {
1896ac22262SGustavo Romero 		warn("jvmti: cannot create jit cache dir %s", jit_path);
190209045adSStephane Eranian 		return -1;
191209045adSStephane Eranian 	}
192209045adSStephane Eranian 
193209045adSStephane Eranian 	return 0;
194209045adSStephane Eranian }
195209045adSStephane Eranian 
196209045adSStephane Eranian static int
perf_open_marker_file(int fd)197209045adSStephane Eranian perf_open_marker_file(int fd)
198209045adSStephane Eranian {
199209045adSStephane Eranian 	long pgsz;
200209045adSStephane Eranian 
201209045adSStephane Eranian 	pgsz = sysconf(_SC_PAGESIZE);
202209045adSStephane Eranian 	if (pgsz == -1)
203209045adSStephane Eranian 		return -1;
204209045adSStephane Eranian 
205209045adSStephane Eranian 	/*
206209045adSStephane Eranian 	 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
207209045adSStephane Eranian 	 * The mmap is captured either live (perf record running when we mmap)
208209045adSStephane Eranian 	 * or  in deferred mode, via /proc/PID/maps
209209045adSStephane Eranian 	 * the MMAP record is used as a marker of a jitdump file for more meta
210209045adSStephane Eranian 	 * data info about the jitted code. Perf report/annotate detect this
211209045adSStephane Eranian 	 * special filename and process the jitdump file.
212209045adSStephane Eranian 	 *
213209045adSStephane Eranian 	 * mapping must be PROT_EXEC to ensure it is captured by perf record
214209045adSStephane Eranian 	 * even when not using -d option
215209045adSStephane Eranian 	 */
216209045adSStephane Eranian 	marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
217209045adSStephane Eranian 	return (marker_addr == MAP_FAILED) ? -1 : 0;
218209045adSStephane Eranian }
219209045adSStephane Eranian 
220209045adSStephane Eranian static void
perf_close_marker_file(void)221209045adSStephane Eranian perf_close_marker_file(void)
222209045adSStephane Eranian {
223209045adSStephane Eranian 	long pgsz;
224209045adSStephane Eranian 
225209045adSStephane Eranian 	if (!marker_addr)
226209045adSStephane Eranian 		return;
227209045adSStephane Eranian 
228209045adSStephane Eranian 	pgsz = sysconf(_SC_PAGESIZE);
229209045adSStephane Eranian 	if (pgsz == -1)
230209045adSStephane Eranian 		return;
231209045adSStephane Eranian 
232209045adSStephane Eranian 	munmap(marker_addr, pgsz);
233209045adSStephane Eranian }
234209045adSStephane Eranian 
2352a28e230SAdrian Hunter static void
init_arch_timestamp(void)2362a28e230SAdrian Hunter init_arch_timestamp(void)
2372a28e230SAdrian Hunter {
2382a28e230SAdrian Hunter 	char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
2392a28e230SAdrian Hunter 
2402a28e230SAdrian Hunter 	if (!str || !*str || !strcmp(str, "0"))
2412a28e230SAdrian Hunter 		return;
2422a28e230SAdrian Hunter 
2432a28e230SAdrian Hunter 	use_arch_timestamp = 1;
2442a28e230SAdrian Hunter }
2452a28e230SAdrian Hunter 
jvmti_open(void)246209045adSStephane Eranian void *jvmti_open(void)
247209045adSStephane Eranian {
248209045adSStephane Eranian 	char dump_path[PATH_MAX];
249209045adSStephane Eranian 	struct jitheader header;
2506ac22262SGustavo Romero 	int fd, ret;
251209045adSStephane Eranian 	FILE *fp;
252209045adSStephane Eranian 
2532a28e230SAdrian Hunter 	init_arch_timestamp();
2542a28e230SAdrian Hunter 
255209045adSStephane Eranian 	/*
256209045adSStephane Eranian 	 * check if clockid is supported
257209045adSStephane Eranian 	 */
2582a28e230SAdrian Hunter 	if (!perf_get_timestamp()) {
2592a28e230SAdrian Hunter 		if (use_arch_timestamp)
2602a28e230SAdrian Hunter 			warnx("jvmti: arch timestamp not supported");
2612a28e230SAdrian Hunter 		else
262209045adSStephane Eranian 			warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
2632a28e230SAdrian Hunter 	}
264209045adSStephane Eranian 
265209045adSStephane Eranian 	memset(&header, 0, sizeof(header));
266209045adSStephane Eranian 
2676ac22262SGustavo Romero 	/*
2686ac22262SGustavo Romero 	 * jitdump file dir
2696ac22262SGustavo Romero 	 */
2706ac22262SGustavo Romero 	if (create_jit_cache_dir() < 0)
2716ac22262SGustavo Romero 		return NULL;
272209045adSStephane Eranian 
273209045adSStephane Eranian 	/*
274209045adSStephane Eranian 	 * jitdump file name
275209045adSStephane Eranian 	 */
2766ac22262SGustavo Romero 	ret = snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
2776ac22262SGustavo Romero 	if (ret >= PATH_MAX) {
2786ac22262SGustavo Romero 		warnx("jvmti: cannot generate jitdump file full path because"
2796ac22262SGustavo Romero 			" %s/jit-%i.dump is too long, please check the cwd,"
2806ac22262SGustavo Romero 			" JITDUMPDIR, and HOME variables", jit_path, getpid());
2816ac22262SGustavo Romero 		return NULL;
2826ac22262SGustavo Romero 	}
283209045adSStephane Eranian 
284209045adSStephane Eranian 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
285209045adSStephane Eranian 	if (fd == -1)
286209045adSStephane Eranian 		return NULL;
287209045adSStephane Eranian 
288209045adSStephane Eranian 	/*
289209045adSStephane Eranian 	 * create perf.data maker for the jitdump file
290209045adSStephane Eranian 	 */
291209045adSStephane Eranian 	if (perf_open_marker_file(fd)) {
292209045adSStephane Eranian 		warnx("jvmti: failed to create marker file");
293209045adSStephane Eranian 		return NULL;
294209045adSStephane Eranian 	}
295209045adSStephane Eranian 
296209045adSStephane Eranian 	fp = fdopen(fd, "w+");
297209045adSStephane Eranian 	if (!fp) {
298209045adSStephane Eranian 		warn("jvmti: cannot create %s", dump_path);
299209045adSStephane Eranian 		close(fd);
300209045adSStephane Eranian 		goto error;
301209045adSStephane Eranian 	}
302209045adSStephane Eranian 
303209045adSStephane Eranian 	warnx("jvmti: jitdump in %s", dump_path);
304209045adSStephane Eranian 
305209045adSStephane Eranian 	if (get_e_machine(&header)) {
306209045adSStephane Eranian 		warn("get_e_machine failed\n");
307209045adSStephane Eranian 		goto error;
308209045adSStephane Eranian 	}
309209045adSStephane Eranian 
310209045adSStephane Eranian 	header.magic      = JITHEADER_MAGIC;
311209045adSStephane Eranian 	header.version    = JITHEADER_VERSION;
312209045adSStephane Eranian 	header.total_size = sizeof(header);
313209045adSStephane Eranian 	header.pid        = getpid();
314209045adSStephane Eranian 
315209045adSStephane Eranian 	header.timestamp = perf_get_timestamp();
316209045adSStephane Eranian 
3172a28e230SAdrian Hunter 	if (use_arch_timestamp)
3182a28e230SAdrian Hunter 		header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
3192a28e230SAdrian Hunter 
320209045adSStephane Eranian 	if (!fwrite(&header, sizeof(header), 1, fp)) {
321209045adSStephane Eranian 		warn("jvmti: cannot write dumpfile header");
322209045adSStephane Eranian 		goto error;
323209045adSStephane Eranian 	}
324209045adSStephane Eranian 	return fp;
325209045adSStephane Eranian error:
326209045adSStephane Eranian 	fclose(fp);
327209045adSStephane Eranian 	return NULL;
328209045adSStephane Eranian }
329209045adSStephane Eranian 
330209045adSStephane Eranian int
jvmti_close(void * agent)331209045adSStephane Eranian jvmti_close(void *agent)
332209045adSStephane Eranian {
333209045adSStephane Eranian 	struct jr_code_close rec;
334209045adSStephane Eranian 	FILE *fp = agent;
335209045adSStephane Eranian 
336209045adSStephane Eranian 	if (!fp) {
33719f0edb9SColin Ian King 		warnx("jvmti: invalid fd in close_agent");
338209045adSStephane Eranian 		return -1;
339209045adSStephane Eranian 	}
340209045adSStephane Eranian 
341209045adSStephane Eranian 	rec.p.id = JIT_CODE_CLOSE;
342209045adSStephane Eranian 	rec.p.total_size = sizeof(rec);
343209045adSStephane Eranian 
344209045adSStephane Eranian 	rec.p.timestamp = perf_get_timestamp();
345209045adSStephane Eranian 
346209045adSStephane Eranian 	if (!fwrite(&rec, sizeof(rec), 1, fp))
347209045adSStephane Eranian 		return -1;
348209045adSStephane Eranian 
349209045adSStephane Eranian 	fclose(fp);
350209045adSStephane Eranian 
351209045adSStephane Eranian 	fp = NULL;
352209045adSStephane Eranian 
353209045adSStephane Eranian 	perf_close_marker_file();
354209045adSStephane Eranian 
355209045adSStephane Eranian 	return 0;
356209045adSStephane Eranian }
357209045adSStephane Eranian 
358209045adSStephane Eranian int
jvmti_write_code(void * agent,char const * sym,uint64_t vma,void const * code,unsigned int const size)359209045adSStephane Eranian jvmti_write_code(void *agent, char const *sym,
360209045adSStephane Eranian 	uint64_t vma, void const *code, unsigned int const size)
361209045adSStephane Eranian {
362209045adSStephane Eranian 	static int code_generation = 1;
363209045adSStephane Eranian 	struct jr_code_load rec;
364209045adSStephane Eranian 	size_t sym_len;
365209045adSStephane Eranian 	FILE *fp = agent;
366209045adSStephane Eranian 	int ret = -1;
367209045adSStephane Eranian 
368209045adSStephane Eranian 	/* don't care about 0 length function, no samples */
369209045adSStephane Eranian 	if (size == 0)
370209045adSStephane Eranian 		return 0;
371209045adSStephane Eranian 
372209045adSStephane Eranian 	if (!fp) {
373209045adSStephane Eranian 		warnx("jvmti: invalid fd in write_native_code");
374209045adSStephane Eranian 		return -1;
375209045adSStephane Eranian 	}
376209045adSStephane Eranian 
377209045adSStephane Eranian 	sym_len = strlen(sym) + 1;
378209045adSStephane Eranian 
379209045adSStephane Eranian 	rec.p.id           = JIT_CODE_LOAD;
380209045adSStephane Eranian 	rec.p.total_size   = sizeof(rec) + sym_len;
381209045adSStephane Eranian 	rec.p.timestamp    = perf_get_timestamp();
382209045adSStephane Eranian 
383209045adSStephane Eranian 	rec.code_size  = size;
384209045adSStephane Eranian 	rec.vma        = vma;
385209045adSStephane Eranian 	rec.code_addr  = vma;
386209045adSStephane Eranian 	rec.pid	       = getpid();
387209045adSStephane Eranian 	rec.tid	       = gettid();
388209045adSStephane Eranian 
389209045adSStephane Eranian 	if (code)
390209045adSStephane Eranian 		rec.p.total_size += size;
391209045adSStephane Eranian 
392209045adSStephane Eranian 	/*
393*4d39c89fSIngo Molnar 	 * If JVM is multi-threaded, multiple concurrent calls to agent
394209045adSStephane Eranian 	 * may be possible, so protect file writes
395209045adSStephane Eranian 	 */
396209045adSStephane Eranian 	flockfile(fp);
397209045adSStephane Eranian 
398209045adSStephane Eranian 	/*
399209045adSStephane Eranian 	 * get code index inside lock to avoid race condition
400209045adSStephane Eranian 	 */
401209045adSStephane Eranian 	rec.code_index = code_generation++;
402209045adSStephane Eranian 
403209045adSStephane Eranian 	ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
404209045adSStephane Eranian 	fwrite_unlocked(sym, sym_len, 1, fp);
405209045adSStephane Eranian 
406209045adSStephane Eranian 	if (code)
407209045adSStephane Eranian 		fwrite_unlocked(code, size, 1, fp);
408209045adSStephane Eranian 
409209045adSStephane Eranian 	funlockfile(fp);
410209045adSStephane Eranian 
411209045adSStephane Eranian 	ret = 0;
412209045adSStephane Eranian 
413209045adSStephane Eranian 	return ret;
414209045adSStephane Eranian }
415209045adSStephane Eranian 
416209045adSStephane Eranian int
jvmti_write_debug_info(void * agent,uint64_t code,int nr_lines,jvmti_line_info_t * li,const char * const * file_names)417ca58d7e6SBen Gainey jvmti_write_debug_info(void *agent, uint64_t code,
418ca58d7e6SBen Gainey     int nr_lines, jvmti_line_info_t *li,
419ca58d7e6SBen Gainey     const char * const * file_names)
420209045adSStephane Eranian {
421209045adSStephane Eranian 	struct jr_code_debug_info rec;
422ca58d7e6SBen Gainey 	size_t sret, len, size, flen = 0;
423598b7c69SStephane Eranian 	uint64_t addr;
424209045adSStephane Eranian 	FILE *fp = agent;
425209045adSStephane Eranian 	int i;
426209045adSStephane Eranian 
427209045adSStephane Eranian 	/*
428209045adSStephane Eranian 	 * no entry to write
429209045adSStephane Eranian 	 */
430598b7c69SStephane Eranian 	if (!nr_lines)
431209045adSStephane Eranian 		return 0;
432209045adSStephane Eranian 
433209045adSStephane Eranian 	if (!fp) {
434209045adSStephane Eranian 		warnx("jvmti: invalid fd in write_debug_info");
435209045adSStephane Eranian 		return -1;
436209045adSStephane Eranian 	}
437209045adSStephane Eranian 
438ca58d7e6SBen Gainey 	for (i = 0; i < nr_lines; ++i) {
439ca58d7e6SBen Gainey 	    flen += strlen(file_names[i]) + 1;
440ca58d7e6SBen Gainey 	}
441209045adSStephane Eranian 
442209045adSStephane Eranian 	rec.p.id        = JIT_CODE_DEBUG_INFO;
443209045adSStephane Eranian 	size            = sizeof(rec);
444209045adSStephane Eranian 	rec.p.timestamp = perf_get_timestamp();
445209045adSStephane Eranian 	rec.code_addr   = (uint64_t)(uintptr_t)code;
446598b7c69SStephane Eranian 	rec.nr_entry    = nr_lines;
447209045adSStephane Eranian 
448209045adSStephane Eranian 	/*
449209045adSStephane Eranian 	 * on disk source line info layout:
450209045adSStephane Eranian 	 * uint64_t : addr
451209045adSStephane Eranian 	 * int      : line number
452598b7c69SStephane Eranian 	 * int      : column discriminator
453209045adSStephane Eranian 	 * file[]   : source file name
454209045adSStephane Eranian 	 */
455598b7c69SStephane Eranian 	size += nr_lines * sizeof(struct debug_entry);
456ca58d7e6SBen Gainey 	size += flen;
45713b9012aSStephane Eranian 	rec.p.total_size = size;
458209045adSStephane Eranian 
459209045adSStephane Eranian 	/*
460*4d39c89fSIngo Molnar 	 * If JVM is multi-threaded, multiple concurrent calls to agent
461209045adSStephane Eranian 	 * may be possible, so protect file writes
462209045adSStephane Eranian 	 */
463209045adSStephane Eranian 	flockfile(fp);
464209045adSStephane Eranian 
465209045adSStephane Eranian 	sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
466209045adSStephane Eranian 	if (sret != 1)
467209045adSStephane Eranian 		goto error;
468209045adSStephane Eranian 
469598b7c69SStephane Eranian 	for (i = 0; i < nr_lines; i++) {
470209045adSStephane Eranian 
471598b7c69SStephane Eranian 		addr = (uint64_t)li[i].pc;
472209045adSStephane Eranian 		len  = sizeof(addr);
473209045adSStephane Eranian 		sret = fwrite_unlocked(&addr, len, 1, fp);
474209045adSStephane Eranian 		if (sret != 1)
475209045adSStephane Eranian 			goto error;
476209045adSStephane Eranian 
477598b7c69SStephane Eranian 		len  = sizeof(li[0].line_number);
478209045adSStephane Eranian 		sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
479209045adSStephane Eranian 		if (sret != 1)
480209045adSStephane Eranian 			goto error;
481209045adSStephane Eranian 
482598b7c69SStephane Eranian 		len  = sizeof(li[0].discrim);
483598b7c69SStephane Eranian 		sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
484209045adSStephane Eranian 		if (sret != 1)
485209045adSStephane Eranian 			goto error;
486209045adSStephane Eranian 
487ca58d7e6SBen Gainey 		sret = fwrite_unlocked(file_names[i], strlen(file_names[i]) + 1, 1, fp);
488598b7c69SStephane Eranian 		if (sret != 1)
489598b7c69SStephane Eranian 			goto error;
490209045adSStephane Eranian 	}
491209045adSStephane Eranian 	funlockfile(fp);
492209045adSStephane Eranian 	return 0;
493209045adSStephane Eranian error:
494209045adSStephane Eranian 	funlockfile(fp);
495209045adSStephane Eranian 	return -1;
496209045adSStephane Eranian }
497