xref: /linux/tools/perf/jvmti/jvmti_agent.c (revision 598b7c6919c7bbcc1243009721a01bc12275ff3e)
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>
38209045adSStephane Eranian 
39209045adSStephane Eranian #include "jvmti_agent.h"
40209045adSStephane Eranian #include "../util/jitdump.h"
41209045adSStephane Eranian 
42209045adSStephane Eranian #define JIT_LANG "java"
43209045adSStephane Eranian 
44209045adSStephane Eranian static char jit_path[PATH_MAX];
45209045adSStephane Eranian static void *marker_addr;
46209045adSStephane Eranian 
47209045adSStephane Eranian /*
48209045adSStephane Eranian  * padding buffer
49209045adSStephane Eranian  */
50209045adSStephane Eranian static const char pad_bytes[7];
51209045adSStephane Eranian 
52209045adSStephane Eranian static inline pid_t gettid(void)
53209045adSStephane Eranian {
54209045adSStephane Eranian 	return (pid_t)syscall(__NR_gettid);
55209045adSStephane Eranian }
56209045adSStephane Eranian 
57209045adSStephane Eranian static int get_e_machine(struct jitheader *hdr)
58209045adSStephane Eranian {
59209045adSStephane Eranian 	ssize_t sret;
60209045adSStephane Eranian 	char id[16];
61209045adSStephane Eranian 	int fd, ret = -1;
62209045adSStephane Eranian 	int m = -1;
63209045adSStephane Eranian 	struct {
64209045adSStephane Eranian 		uint16_t e_type;
65209045adSStephane Eranian 		uint16_t e_machine;
66209045adSStephane Eranian 	} info;
67209045adSStephane Eranian 
68209045adSStephane Eranian 	fd = open("/proc/self/exe", O_RDONLY);
69209045adSStephane Eranian 	if (fd == -1)
70209045adSStephane Eranian 		return -1;
71209045adSStephane Eranian 
72209045adSStephane Eranian 	sret = read(fd, id, sizeof(id));
73209045adSStephane Eranian 	if (sret != sizeof(id))
74209045adSStephane Eranian 		goto error;
75209045adSStephane Eranian 
76209045adSStephane Eranian 	/* check ELF signature */
77209045adSStephane Eranian 	if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
78209045adSStephane Eranian 		goto error;
79209045adSStephane Eranian 
80209045adSStephane Eranian 	sret = read(fd, &info, sizeof(info));
81209045adSStephane Eranian 	if (sret != sizeof(info))
82209045adSStephane Eranian 		goto error;
83209045adSStephane Eranian 
84209045adSStephane Eranian 	m = info.e_machine;
85209045adSStephane Eranian 	if (m < 0)
86209045adSStephane Eranian 		m = 0; /* ELF EM_NONE */
87209045adSStephane Eranian 
88209045adSStephane Eranian 	hdr->elf_mach = m;
89209045adSStephane Eranian 	ret = 0;
90209045adSStephane Eranian error:
91209045adSStephane Eranian 	close(fd);
92209045adSStephane Eranian 	return ret;
93209045adSStephane Eranian }
94209045adSStephane Eranian 
95209045adSStephane Eranian #define NSEC_PER_SEC	1000000000
96209045adSStephane Eranian static int perf_clk_id = CLOCK_MONOTONIC;
97209045adSStephane Eranian 
98209045adSStephane Eranian static inline uint64_t
99209045adSStephane Eranian timespec_to_ns(const struct timespec *ts)
100209045adSStephane Eranian {
101209045adSStephane Eranian         return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
102209045adSStephane Eranian }
103209045adSStephane Eranian 
104209045adSStephane Eranian static inline uint64_t
105209045adSStephane Eranian perf_get_timestamp(void)
106209045adSStephane Eranian {
107209045adSStephane Eranian 	struct timespec ts;
108209045adSStephane Eranian 	int ret;
109209045adSStephane Eranian 
110209045adSStephane Eranian 	ret = clock_gettime(perf_clk_id, &ts);
111209045adSStephane Eranian 	if (ret)
112209045adSStephane Eranian 		return 0;
113209045adSStephane Eranian 
114209045adSStephane Eranian 	return timespec_to_ns(&ts);
115209045adSStephane Eranian }
116209045adSStephane Eranian 
117209045adSStephane Eranian static int
118209045adSStephane Eranian debug_cache_init(void)
119209045adSStephane Eranian {
120209045adSStephane Eranian 	char str[32];
121209045adSStephane Eranian 	char *base, *p;
122209045adSStephane Eranian 	struct tm tm;
123209045adSStephane Eranian 	time_t t;
124209045adSStephane Eranian 	int ret;
125209045adSStephane Eranian 
126209045adSStephane Eranian 	time(&t);
127209045adSStephane Eranian 	localtime_r(&t, &tm);
128209045adSStephane Eranian 
129209045adSStephane Eranian 	base = getenv("JITDUMPDIR");
130209045adSStephane Eranian 	if (!base)
131209045adSStephane Eranian 		base = getenv("HOME");
132209045adSStephane Eranian 	if (!base)
133209045adSStephane Eranian 		base = ".";
134209045adSStephane Eranian 
135209045adSStephane Eranian 	strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
136209045adSStephane Eranian 
137209045adSStephane Eranian 	snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
138209045adSStephane Eranian 
139209045adSStephane Eranian 	ret = mkdir(jit_path, 0755);
140209045adSStephane Eranian 	if (ret == -1) {
141209045adSStephane Eranian 		if (errno != EEXIST) {
142209045adSStephane Eranian 			warn("jvmti: cannot create jit cache dir %s", jit_path);
143209045adSStephane Eranian 			return -1;
144209045adSStephane Eranian 		}
145209045adSStephane Eranian 	}
146209045adSStephane Eranian 
147209045adSStephane Eranian 	snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
148209045adSStephane Eranian 	ret = mkdir(jit_path, 0755);
149209045adSStephane Eranian 	if (ret == -1) {
150209045adSStephane Eranian 		if (errno != EEXIST) {
151209045adSStephane Eranian 			warn("cannot create jit cache dir %s", jit_path);
152209045adSStephane Eranian 			return -1;
153209045adSStephane Eranian 		}
154209045adSStephane Eranian 	}
155209045adSStephane Eranian 
156209045adSStephane Eranian 	snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
157209045adSStephane Eranian 
158209045adSStephane Eranian 	p = mkdtemp(jit_path);
159209045adSStephane Eranian 	if (p != jit_path) {
160209045adSStephane Eranian 		warn("cannot create jit cache dir %s", jit_path);
161209045adSStephane Eranian 		return -1;
162209045adSStephane Eranian 	}
163209045adSStephane Eranian 
164209045adSStephane Eranian 	return 0;
165209045adSStephane Eranian }
166209045adSStephane Eranian 
167209045adSStephane Eranian static int
168209045adSStephane Eranian perf_open_marker_file(int fd)
169209045adSStephane Eranian {
170209045adSStephane Eranian 	long pgsz;
171209045adSStephane Eranian 
172209045adSStephane Eranian 	pgsz = sysconf(_SC_PAGESIZE);
173209045adSStephane Eranian 	if (pgsz == -1)
174209045adSStephane Eranian 		return -1;
175209045adSStephane Eranian 
176209045adSStephane Eranian 	/*
177209045adSStephane Eranian 	 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
178209045adSStephane Eranian 	 * The mmap is captured either live (perf record running when we mmap)
179209045adSStephane Eranian 	 * or  in deferred mode, via /proc/PID/maps
180209045adSStephane Eranian 	 * the MMAP record is used as a marker of a jitdump file for more meta
181209045adSStephane Eranian 	 * data info about the jitted code. Perf report/annotate detect this
182209045adSStephane Eranian 	 * special filename and process the jitdump file.
183209045adSStephane Eranian 	 *
184209045adSStephane Eranian 	 * mapping must be PROT_EXEC to ensure it is captured by perf record
185209045adSStephane Eranian 	 * even when not using -d option
186209045adSStephane Eranian 	 */
187209045adSStephane Eranian 	marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
188209045adSStephane Eranian 	return (marker_addr == MAP_FAILED) ? -1 : 0;
189209045adSStephane Eranian }
190209045adSStephane Eranian 
191209045adSStephane Eranian static void
192209045adSStephane Eranian perf_close_marker_file(void)
193209045adSStephane Eranian {
194209045adSStephane Eranian 	long pgsz;
195209045adSStephane Eranian 
196209045adSStephane Eranian 	if (!marker_addr)
197209045adSStephane Eranian 		return;
198209045adSStephane Eranian 
199209045adSStephane Eranian 	pgsz = sysconf(_SC_PAGESIZE);
200209045adSStephane Eranian 	if (pgsz == -1)
201209045adSStephane Eranian 		return;
202209045adSStephane Eranian 
203209045adSStephane Eranian 	munmap(marker_addr, pgsz);
204209045adSStephane Eranian }
205209045adSStephane Eranian 
206209045adSStephane Eranian void *jvmti_open(void)
207209045adSStephane Eranian {
208209045adSStephane Eranian 	int pad_cnt;
209209045adSStephane Eranian 	char dump_path[PATH_MAX];
210209045adSStephane Eranian 	struct jitheader header;
211209045adSStephane Eranian 	int fd;
212209045adSStephane Eranian 	FILE *fp;
213209045adSStephane Eranian 
214209045adSStephane Eranian 	/*
215209045adSStephane Eranian 	 * check if clockid is supported
216209045adSStephane Eranian 	 */
217209045adSStephane Eranian 	if (!perf_get_timestamp())
218209045adSStephane Eranian 		warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
219209045adSStephane Eranian 
220209045adSStephane Eranian 	memset(&header, 0, sizeof(header));
221209045adSStephane Eranian 
222209045adSStephane Eranian 	debug_cache_init();
223209045adSStephane Eranian 
224209045adSStephane Eranian 	/*
225209045adSStephane Eranian 	 * jitdump file name
226209045adSStephane Eranian 	 */
227209045adSStephane Eranian 	snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
228209045adSStephane Eranian 
229209045adSStephane Eranian 	fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
230209045adSStephane Eranian 	if (fd == -1)
231209045adSStephane Eranian 		return NULL;
232209045adSStephane Eranian 
233209045adSStephane Eranian 	/*
234209045adSStephane Eranian 	 * create perf.data maker for the jitdump file
235209045adSStephane Eranian 	 */
236209045adSStephane Eranian 	if (perf_open_marker_file(fd)) {
237209045adSStephane Eranian 		warnx("jvmti: failed to create marker file");
238209045adSStephane Eranian 		return NULL;
239209045adSStephane Eranian 	}
240209045adSStephane Eranian 
241209045adSStephane Eranian 	fp = fdopen(fd, "w+");
242209045adSStephane Eranian 	if (!fp) {
243209045adSStephane Eranian 		warn("jvmti: cannot create %s", dump_path);
244209045adSStephane Eranian 		close(fd);
245209045adSStephane Eranian 		goto error;
246209045adSStephane Eranian 	}
247209045adSStephane Eranian 
248209045adSStephane Eranian 	warnx("jvmti: jitdump in %s", dump_path);
249209045adSStephane Eranian 
250209045adSStephane Eranian 	if (get_e_machine(&header)) {
251209045adSStephane Eranian 		warn("get_e_machine failed\n");
252209045adSStephane Eranian 		goto error;
253209045adSStephane Eranian 	}
254209045adSStephane Eranian 
255209045adSStephane Eranian 	header.magic      = JITHEADER_MAGIC;
256209045adSStephane Eranian 	header.version    = JITHEADER_VERSION;
257209045adSStephane Eranian 	header.total_size = sizeof(header);
258209045adSStephane Eranian 	header.pid        = getpid();
259209045adSStephane Eranian 
260209045adSStephane Eranian 	/* calculate amount of padding '\0' */
261209045adSStephane Eranian 	pad_cnt = PADDING_8ALIGNED(header.total_size);
262209045adSStephane Eranian 	header.total_size += pad_cnt;
263209045adSStephane Eranian 
264209045adSStephane Eranian 	header.timestamp = perf_get_timestamp();
265209045adSStephane Eranian 
266209045adSStephane Eranian 	if (!fwrite(&header, sizeof(header), 1, fp)) {
267209045adSStephane Eranian 		warn("jvmti: cannot write dumpfile header");
268209045adSStephane Eranian 		goto error;
269209045adSStephane Eranian 	}
270209045adSStephane Eranian 
271209045adSStephane Eranian 	/* write padding '\0' if necessary */
272209045adSStephane Eranian 	if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
273209045adSStephane Eranian 		warn("jvmti: cannot write dumpfile header padding");
274209045adSStephane Eranian 		goto error;
275209045adSStephane Eranian 	}
276209045adSStephane Eranian 
277209045adSStephane Eranian 	return fp;
278209045adSStephane Eranian error:
279209045adSStephane Eranian 	fclose(fp);
280209045adSStephane Eranian 	return NULL;
281209045adSStephane Eranian }
282209045adSStephane Eranian 
283209045adSStephane Eranian int
284209045adSStephane Eranian jvmti_close(void *agent)
285209045adSStephane Eranian {
286209045adSStephane Eranian 	struct jr_code_close rec;
287209045adSStephane Eranian 	FILE *fp = agent;
288209045adSStephane Eranian 
289209045adSStephane Eranian 	if (!fp) {
290209045adSStephane Eranian 		warnx("jvmti: incalid fd in close_agent");
291209045adSStephane Eranian 		return -1;
292209045adSStephane Eranian 	}
293209045adSStephane Eranian 
294209045adSStephane Eranian 	rec.p.id = JIT_CODE_CLOSE;
295209045adSStephane Eranian 	rec.p.total_size = sizeof(rec);
296209045adSStephane Eranian 
297209045adSStephane Eranian 	rec.p.timestamp = perf_get_timestamp();
298209045adSStephane Eranian 
299209045adSStephane Eranian 	if (!fwrite(&rec, sizeof(rec), 1, fp))
300209045adSStephane Eranian 		return -1;
301209045adSStephane Eranian 
302209045adSStephane Eranian 	fclose(fp);
303209045adSStephane Eranian 
304209045adSStephane Eranian 	fp = NULL;
305209045adSStephane Eranian 
306209045adSStephane Eranian 	perf_close_marker_file();
307209045adSStephane Eranian 
308209045adSStephane Eranian 	return 0;
309209045adSStephane Eranian }
310209045adSStephane Eranian 
311209045adSStephane Eranian int
312209045adSStephane Eranian jvmti_write_code(void *agent, char const *sym,
313209045adSStephane Eranian 	uint64_t vma, void const *code, unsigned int const size)
314209045adSStephane Eranian {
315209045adSStephane Eranian 	static int code_generation = 1;
316209045adSStephane Eranian 	struct jr_code_load rec;
317209045adSStephane Eranian 	size_t sym_len;
318209045adSStephane Eranian 	size_t padding_count;
319209045adSStephane Eranian 	FILE *fp = agent;
320209045adSStephane Eranian 	int ret = -1;
321209045adSStephane Eranian 
322209045adSStephane Eranian 	/* don't care about 0 length function, no samples */
323209045adSStephane Eranian 	if (size == 0)
324209045adSStephane Eranian 		return 0;
325209045adSStephane Eranian 
326209045adSStephane Eranian 	if (!fp) {
327209045adSStephane Eranian 		warnx("jvmti: invalid fd in write_native_code");
328209045adSStephane Eranian 		return -1;
329209045adSStephane Eranian 	}
330209045adSStephane Eranian 
331209045adSStephane Eranian 	sym_len = strlen(sym) + 1;
332209045adSStephane Eranian 
333209045adSStephane Eranian 	rec.p.id           = JIT_CODE_LOAD;
334209045adSStephane Eranian 	rec.p.total_size   = sizeof(rec) + sym_len;
335209045adSStephane Eranian 	padding_count      = PADDING_8ALIGNED(rec.p.total_size);
336209045adSStephane Eranian 	rec.p. total_size += padding_count;
337209045adSStephane Eranian 	rec.p.timestamp    = perf_get_timestamp();
338209045adSStephane Eranian 
339209045adSStephane Eranian 	rec.code_size  = size;
340209045adSStephane Eranian 	rec.vma        = vma;
341209045adSStephane Eranian 	rec.code_addr  = vma;
342209045adSStephane Eranian 	rec.pid	       = getpid();
343209045adSStephane Eranian 	rec.tid	       = gettid();
344209045adSStephane Eranian 
345209045adSStephane Eranian 	if (code)
346209045adSStephane Eranian 		rec.p.total_size += size;
347209045adSStephane Eranian 
348209045adSStephane Eranian 	/*
349209045adSStephane Eranian 	 * If JVM is multi-threaded, nultiple concurrent calls to agent
350209045adSStephane Eranian 	 * may be possible, so protect file writes
351209045adSStephane Eranian 	 */
352209045adSStephane Eranian 	flockfile(fp);
353209045adSStephane Eranian 
354209045adSStephane Eranian 	/*
355209045adSStephane Eranian 	 * get code index inside lock to avoid race condition
356209045adSStephane Eranian 	 */
357209045adSStephane Eranian 	rec.code_index = code_generation++;
358209045adSStephane Eranian 
359209045adSStephane Eranian 	ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
360209045adSStephane Eranian 	fwrite_unlocked(sym, sym_len, 1, fp);
361209045adSStephane Eranian 
362209045adSStephane Eranian 	if (padding_count)
363209045adSStephane Eranian 		fwrite_unlocked(pad_bytes, padding_count, 1, fp);
364209045adSStephane Eranian 
365209045adSStephane Eranian 	if (code)
366209045adSStephane Eranian 		fwrite_unlocked(code, size, 1, fp);
367209045adSStephane Eranian 
368209045adSStephane Eranian 	funlockfile(fp);
369209045adSStephane Eranian 
370209045adSStephane Eranian 	ret = 0;
371209045adSStephane Eranian 
372209045adSStephane Eranian 	return ret;
373209045adSStephane Eranian }
374209045adSStephane Eranian 
375209045adSStephane Eranian int
376209045adSStephane Eranian jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
377*598b7c69SStephane Eranian 		       jvmti_line_info_t *li, int nr_lines)
378209045adSStephane Eranian {
379209045adSStephane Eranian 	struct jr_code_debug_info rec;
380209045adSStephane Eranian 	size_t sret, len, size, flen;
381209045adSStephane Eranian 	size_t padding_count;
382*598b7c69SStephane Eranian 	uint64_t addr;
383*598b7c69SStephane Eranian 	const char *fn = file;
384209045adSStephane Eranian 	FILE *fp = agent;
385209045adSStephane Eranian 	int i;
386209045adSStephane Eranian 
387209045adSStephane Eranian 	/*
388209045adSStephane Eranian 	 * no entry to write
389209045adSStephane Eranian 	 */
390*598b7c69SStephane Eranian 	if (!nr_lines)
391209045adSStephane Eranian 		return 0;
392209045adSStephane Eranian 
393209045adSStephane Eranian 	if (!fp) {
394209045adSStephane Eranian 		warnx("jvmti: invalid fd in write_debug_info");
395209045adSStephane Eranian 		return -1;
396209045adSStephane Eranian 	}
397209045adSStephane Eranian 
398209045adSStephane Eranian 	flen = strlen(file) + 1;
399209045adSStephane Eranian 
400209045adSStephane Eranian 	rec.p.id        = JIT_CODE_DEBUG_INFO;
401209045adSStephane Eranian 	size            = sizeof(rec);
402209045adSStephane Eranian 	rec.p.timestamp = perf_get_timestamp();
403209045adSStephane Eranian 	rec.code_addr   = (uint64_t)(uintptr_t)code;
404*598b7c69SStephane Eranian 	rec.nr_entry    = nr_lines;
405209045adSStephane Eranian 
406209045adSStephane Eranian 	/*
407209045adSStephane Eranian 	 * on disk source line info layout:
408209045adSStephane Eranian 	 * uint64_t : addr
409209045adSStephane Eranian 	 * int      : line number
410*598b7c69SStephane Eranian 	 * int      : column discriminator
411209045adSStephane Eranian 	 * file[]   : source file name
412209045adSStephane Eranian 	 * padding  : pad to multiple of 8 bytes
413209045adSStephane Eranian 	 */
414*598b7c69SStephane Eranian 	size += nr_lines * sizeof(struct debug_entry);
415*598b7c69SStephane Eranian 	size += flen * nr_lines;
416209045adSStephane Eranian 	/*
417209045adSStephane Eranian 	 * pad to 8 bytes
418209045adSStephane Eranian 	 */
419209045adSStephane Eranian 	padding_count = PADDING_8ALIGNED(size);
420209045adSStephane Eranian 
421209045adSStephane Eranian 	rec.p.total_size = size + padding_count;
422209045adSStephane Eranian 
423209045adSStephane Eranian 	/*
424209045adSStephane Eranian 	 * If JVM is multi-threaded, nultiple concurrent calls to agent
425209045adSStephane Eranian 	 * may be possible, so protect file writes
426209045adSStephane Eranian 	 */
427209045adSStephane Eranian 	flockfile(fp);
428209045adSStephane Eranian 
429209045adSStephane Eranian 	sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
430209045adSStephane Eranian 	if (sret != 1)
431209045adSStephane Eranian 		goto error;
432209045adSStephane Eranian 
433*598b7c69SStephane Eranian 	for (i = 0; i < nr_lines; i++) {
434209045adSStephane Eranian 
435*598b7c69SStephane Eranian 		addr = (uint64_t)li[i].pc;
436209045adSStephane Eranian 		len  = sizeof(addr);
437209045adSStephane Eranian 		sret = fwrite_unlocked(&addr, len, 1, fp);
438209045adSStephane Eranian 		if (sret != 1)
439209045adSStephane Eranian 			goto error;
440209045adSStephane Eranian 
441*598b7c69SStephane Eranian 		len  = sizeof(li[0].line_number);
442209045adSStephane Eranian 		sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
443209045adSStephane Eranian 		if (sret != 1)
444209045adSStephane Eranian 			goto error;
445209045adSStephane Eranian 
446*598b7c69SStephane Eranian 		len  = sizeof(li[0].discrim);
447*598b7c69SStephane Eranian 		sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
448209045adSStephane Eranian 		if (sret != 1)
449209045adSStephane Eranian 			goto error;
450209045adSStephane Eranian 
451*598b7c69SStephane Eranian 		sret = fwrite_unlocked(fn, flen, 1, fp);
452*598b7c69SStephane Eranian 		if (sret != 1)
453*598b7c69SStephane Eranian 			goto error;
454209045adSStephane Eranian 	}
455209045adSStephane Eranian 	if (padding_count)
456209045adSStephane Eranian 		sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);
457209045adSStephane Eranian 		if (sret != 1)
458209045adSStephane Eranian 			goto error;
459209045adSStephane Eranian 
460209045adSStephane Eranian 	funlockfile(fp);
461209045adSStephane Eranian 	return 0;
462209045adSStephane Eranian error:
463209045adSStephane Eranian 	funlockfile(fp);
464209045adSStephane Eranian 	return -1;
465209045adSStephane Eranian }
466