pmclog.c revision 174215
1147191Sjkoshy/*-
2174215Sjkoshy * Copyright (c) 2005-2007 Joseph Koshy
3174215Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
4147191Sjkoshy * All rights reserved.
5147191Sjkoshy *
6174215Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
7174215Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
8174215Sjkoshy *
9147191Sjkoshy * Redistribution and use in source and binary forms, with or without
10147191Sjkoshy * modification, are permitted provided that the following conditions
11147191Sjkoshy * are met:
12147191Sjkoshy * 1. Redistributions of source code must retain the above copyright
13147191Sjkoshy *    notice, this list of conditions and the following disclaimer.
14147191Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
15147191Sjkoshy *    notice, this list of conditions and the following disclaimer in the
16147191Sjkoshy *    documentation and/or other materials provided with the distribution.
17147191Sjkoshy *
18147191Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19147191Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20147191Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21147191Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22147191Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23147191Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24147191Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25147191Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26147191Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27147191Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28147191Sjkoshy * SUCH DAMAGE.
29147191Sjkoshy */
30147191Sjkoshy
31147191Sjkoshy#include <sys/cdefs.h>
32147191Sjkoshy__FBSDID("$FreeBSD: head/lib/libpmc/pmclog.c 174215 2007-12-03 11:15:46Z jkoshy $");
33147191Sjkoshy
34147191Sjkoshy#include <sys/param.h>
35147191Sjkoshy#include <sys/pmc.h>
36147191Sjkoshy#include <sys/pmclog.h>
37147191Sjkoshy
38147191Sjkoshy#include <assert.h>
39147191Sjkoshy#include <errno.h>
40147191Sjkoshy#include <pmc.h>
41147191Sjkoshy#include <pmclog.h>
42147191Sjkoshy#include <stddef.h>
43147191Sjkoshy#include <stdlib.h>
44147191Sjkoshy#include <string.h>
45147191Sjkoshy#include <strings.h>
46147191Sjkoshy#include <unistd.h>
47147191Sjkoshy
48147191Sjkoshy#include <machine/pmc_mdep.h>
49147191Sjkoshy
50147191Sjkoshy#define	PMCLOG_BUFFER_SIZE			4096
51147191Sjkoshy
52147191Sjkoshy/*
53147191Sjkoshy * API NOTES
54147191Sjkoshy *
55147191Sjkoshy * The pmclog(3) API is oriented towards parsing an event stream in
56147191Sjkoshy * "realtime", i.e., from an data source that may or may not preserve
57147191Sjkoshy * record boundaries -- for example when the data source is elsewhere
58147191Sjkoshy * on a network.  The API allows data to be fed into the parser zero
59147191Sjkoshy * or more bytes at a time.
60147191Sjkoshy *
61147191Sjkoshy * The state for a log file parser is maintained in a 'struct
62147191Sjkoshy * pmclog_parse_state'.  Parser invocations are done by calling
63147191Sjkoshy * 'pmclog_read()'; this function will inform the caller when a
64147191Sjkoshy * complete event is parsed.
65147191Sjkoshy *
66147191Sjkoshy * The parser first assembles a complete log file event in an internal
67147191Sjkoshy * work area (see "ps_saved" below).  Once a complete log file event
68147191Sjkoshy * is read, the parser then parses it and converts it to an event
69147191Sjkoshy * descriptor usable by the client.  We could possibly avoid this two
70147191Sjkoshy * step process by directly parsing the input log to set fields in the
71147191Sjkoshy * event record.  However the parser's state machine would get
72147191Sjkoshy * insanely complicated, and this code is unlikely to be used in
73147191Sjkoshy * performance critical paths.
74147191Sjkoshy */
75147191Sjkoshy
76147191Sjkoshyenum pmclog_parser_state {
77147191Sjkoshy	PL_STATE_NEW_RECORD,		/* in-between records */
78147191Sjkoshy	PL_STATE_EXPECTING_HEADER,	/* header being read */
79147191Sjkoshy	PL_STATE_PARTIAL_RECORD,	/* header present but not the record */
80147191Sjkoshy	PL_STATE_ERROR			/* parsing error encountered */
81147191Sjkoshy};
82147191Sjkoshy
83147191Sjkoshystruct pmclog_parse_state {
84147191Sjkoshy	enum pmclog_parser_state ps_state;
85147191Sjkoshy	enum pmc_cputype	ps_arch;	/* log file architecture */
86147191Sjkoshy	uint32_t		ps_version;	/* hwpmc version */
87147191Sjkoshy	int			ps_initialized;	/* whether initialized */
88147191Sjkoshy	int			ps_count;	/* count of records processed */
89147191Sjkoshy	off_t			ps_offset;	/* stream byte offset */
90147191Sjkoshy	union pmclog_entry	ps_saved;	/* saved partial log entry */
91147191Sjkoshy	int			ps_svcount;	/* #bytes saved */
92147191Sjkoshy	int			ps_fd;		/* active fd or -1 */
93147191Sjkoshy	char			*ps_buffer;	/* scratch buffer if fd != -1 */
94147191Sjkoshy	char			*ps_data;	/* current parse pointer */
95147191Sjkoshy	size_t			ps_len;		/* length of buffered data */
96147191Sjkoshy};
97147191Sjkoshy
98147191Sjkoshy#define	PMCLOG_HEADER_FROM_SAVED_STATE(PS)				\
99147191Sjkoshy	(* ((uint32_t *) &(PS)->ps_saved))
100147191Sjkoshy
101147191Sjkoshy#define	PMCLOG_INITIALIZE_READER(LE,A)	LE = (uint32_t *) &(A)
102147191Sjkoshy#define	PMCLOG_READ32(LE,V) 		do {				\
103147191Sjkoshy		(V)  = *(LE)++;						\
104147191Sjkoshy	} while (0)
105147191Sjkoshy#define	PMCLOG_READ64(LE,V)		do {				\
106147191Sjkoshy		uint64_t _v;						\
107147191Sjkoshy		_v  = (uint64_t) *(LE)++;				\
108147191Sjkoshy		_v |= ((uint64_t) *(LE)++) << 32;			\
109147191Sjkoshy		(V) = _v;						\
110147191Sjkoshy	} while (0)
111147191Sjkoshy
112147191Sjkoshy#define	PMCLOG_READSTRING(LE,DST,LEN)	strlcpy((DST), (char *) (LE), (LEN))
113147191Sjkoshy
114147191Sjkoshy/*
115147191Sjkoshy * Assemble a log record from '*len' octets starting from address '*data'.
116147191Sjkoshy * Update 'data' and 'len' to reflect the number of bytes consumed.
117147191Sjkoshy *
118147191Sjkoshy * '*data' is potentially an unaligned address and '*len' octets may
119147191Sjkoshy * not be enough to complete a event record.
120147191Sjkoshy */
121147191Sjkoshy
122147191Sjkoshystatic enum pmclog_parser_state
123147191Sjkoshypmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
124147191Sjkoshy{
125147191Sjkoshy	int avail, copylen, recordsize, used;
126147191Sjkoshy	uint32_t h;
127147191Sjkoshy	const int HEADERSIZE = sizeof(uint32_t);
128147191Sjkoshy	char *src, *dst;
129147191Sjkoshy
130147191Sjkoshy	if ((avail = *len) <= 0)
131147191Sjkoshy		return (ps->ps_state = PL_STATE_ERROR);
132147191Sjkoshy
133147191Sjkoshy	src = *data;
134147191Sjkoshy	h = used = 0;
135147191Sjkoshy
136147191Sjkoshy	if (ps->ps_state == PL_STATE_NEW_RECORD)
137147191Sjkoshy		ps->ps_svcount = 0;
138147191Sjkoshy
139147191Sjkoshy	dst = (char *) &ps->ps_saved + ps->ps_svcount;
140147191Sjkoshy
141147191Sjkoshy	switch (ps->ps_state) {
142147191Sjkoshy	case PL_STATE_NEW_RECORD:
143147191Sjkoshy
144147191Sjkoshy		/*
145147191Sjkoshy		 * Transitions:
146147191Sjkoshy		 *
147147191Sjkoshy		 * Case A: avail < headersize
148147191Sjkoshy		 *	-> 'expecting header'
149147191Sjkoshy		 *
150147191Sjkoshy		 * Case B: avail >= headersize
151147191Sjkoshy		 *    B.1: avail < recordsize
152147191Sjkoshy		 *	   -> 'partial record'
153147191Sjkoshy		 *    B.2: avail >= recordsize
154147191Sjkoshy		 *         -> 'new record'
155147191Sjkoshy		 */
156147191Sjkoshy
157147191Sjkoshy		copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
158147191Sjkoshy		bcopy(src, dst, copylen);
159147191Sjkoshy		ps->ps_svcount = used = copylen;
160147191Sjkoshy
161147191Sjkoshy		if (copylen < HEADERSIZE) {
162147191Sjkoshy			ps->ps_state = PL_STATE_EXPECTING_HEADER;
163147191Sjkoshy			goto done;
164147191Sjkoshy		}
165147191Sjkoshy
166147191Sjkoshy		src += copylen;
167147191Sjkoshy		dst += copylen;
168147191Sjkoshy
169147191Sjkoshy		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
170147191Sjkoshy		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
171147191Sjkoshy
172147191Sjkoshy		if (recordsize <= 0)
173147191Sjkoshy			goto error;
174147191Sjkoshy
175147191Sjkoshy		if (recordsize <= avail) { /* full record available */
176147191Sjkoshy			bcopy(src, dst, recordsize - copylen);
177147191Sjkoshy			ps->ps_svcount = used = recordsize;
178147191Sjkoshy			goto done;
179147191Sjkoshy		}
180147191Sjkoshy
181147191Sjkoshy		/* header + a partial record is available */
182147191Sjkoshy		bcopy(src, dst, avail - copylen);
183147191Sjkoshy		ps->ps_svcount = used = avail;
184147191Sjkoshy		ps->ps_state = PL_STATE_PARTIAL_RECORD;
185147191Sjkoshy
186147191Sjkoshy		break;
187147191Sjkoshy
188147191Sjkoshy	case PL_STATE_EXPECTING_HEADER:
189147191Sjkoshy
190147191Sjkoshy		/*
191147191Sjkoshy		 * Transitions:
192147191Sjkoshy		 *
193147191Sjkoshy		 * Case C: avail+saved < headersize
194147191Sjkoshy		 * 	-> 'expecting header'
195147191Sjkoshy		 *
196147191Sjkoshy		 * Case D: avail+saved >= headersize
197147191Sjkoshy		 *    D.1: avail+saved < recordsize
198147191Sjkoshy		 *    	-> 'partial record'
199147191Sjkoshy		 *    D.2: avail+saved >= recordsize
200147191Sjkoshy		 *    	-> 'new record'
201147191Sjkoshy		 *    (see PARTIAL_RECORD handling below)
202147191Sjkoshy		 */
203147191Sjkoshy
204147191Sjkoshy		if (avail + ps->ps_svcount < HEADERSIZE) {
205147191Sjkoshy			bcopy(src, dst, avail);
206147191Sjkoshy			ps->ps_svcount += avail;
207147191Sjkoshy			used = avail;
208147191Sjkoshy			break;
209147191Sjkoshy		}
210147191Sjkoshy
211147191Sjkoshy		used = copylen = HEADERSIZE - ps->ps_svcount;
212147191Sjkoshy		bcopy(src, dst, copylen);
213147191Sjkoshy		src += copylen;
214147191Sjkoshy		dst += copylen;
215147191Sjkoshy		avail -= copylen;
216147191Sjkoshy		ps->ps_svcount += copylen;
217147191Sjkoshy
218147191Sjkoshy		/*FALLTHROUGH*/
219147191Sjkoshy
220147191Sjkoshy	case PL_STATE_PARTIAL_RECORD:
221147191Sjkoshy
222147191Sjkoshy		/*
223147191Sjkoshy		 * Transitions:
224147191Sjkoshy		 *
225147191Sjkoshy		 * Case E: avail+saved < recordsize
226147191Sjkoshy		 * 	-> 'partial record'
227147191Sjkoshy		 *
228147191Sjkoshy		 * Case F: avail+saved >= recordsize
229147191Sjkoshy		 * 	-> 'new record'
230147191Sjkoshy		 */
231147191Sjkoshy
232147191Sjkoshy		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
233147191Sjkoshy		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
234147191Sjkoshy
235147191Sjkoshy		if (recordsize <= 0)
236147191Sjkoshy			goto error;
237147191Sjkoshy
238147191Sjkoshy		if (avail + ps->ps_svcount < recordsize) {
239147191Sjkoshy			copylen = avail;
240147191Sjkoshy			ps->ps_state = PL_STATE_PARTIAL_RECORD;
241147191Sjkoshy		} else {
242147191Sjkoshy			copylen = recordsize - ps->ps_svcount;
243147191Sjkoshy			ps->ps_state = PL_STATE_NEW_RECORD;
244147191Sjkoshy		}
245147191Sjkoshy
246147191Sjkoshy		bcopy(src, dst, copylen);
247147191Sjkoshy		ps->ps_svcount += copylen;
248147191Sjkoshy		used += copylen;
249147191Sjkoshy		break;
250147191Sjkoshy
251147191Sjkoshy	default:
252147191Sjkoshy		goto error;
253147191Sjkoshy	}
254147191Sjkoshy
255147191Sjkoshy done:
256147191Sjkoshy	*data += used;
257147191Sjkoshy	*len  -= used;
258147191Sjkoshy	return ps->ps_state;
259147191Sjkoshy
260147191Sjkoshy error:
261147191Sjkoshy	ps->ps_state = PL_STATE_ERROR;
262147191Sjkoshy	return ps->ps_state;
263147191Sjkoshy}
264147191Sjkoshy
265147191Sjkoshy/*
266147191Sjkoshy * Get an event from the stream pointed to by '*data'.  '*len'
267147191Sjkoshy * indicates the number of bytes available to parse.  Arguments
268147191Sjkoshy * '*data' and '*len' are updated to indicate the number of bytes
269147191Sjkoshy * consumed.
270147191Sjkoshy */
271147191Sjkoshy
272147191Sjkoshystatic int
273147191Sjkoshypmclog_get_event(void *cookie, char **data, ssize_t *len,
274147191Sjkoshy    struct pmclog_ev *ev)
275147191Sjkoshy{
276147191Sjkoshy	int evlen, pathlen;
277174215Sjkoshy	uint32_t h, *le, npc;
278147191Sjkoshy	enum pmclog_parser_state e;
279147191Sjkoshy	struct pmclog_parse_state *ps;
280147191Sjkoshy
281147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
282147191Sjkoshy
283147191Sjkoshy	assert(ps->ps_state != PL_STATE_ERROR);
284147191Sjkoshy
285147191Sjkoshy	if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
286147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
287147191Sjkoshy		return -1;
288147191Sjkoshy	}
289147191Sjkoshy
290147191Sjkoshy	if (e != PL_STATE_NEW_RECORD) {
291147191Sjkoshy		ev->pl_state = PMCLOG_REQUIRE_DATA;
292147191Sjkoshy		return -1;
293147191Sjkoshy	}
294147191Sjkoshy
295147191Sjkoshy	PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
296147191Sjkoshy
297147191Sjkoshy	PMCLOG_READ32(le,h);
298147191Sjkoshy
299147191Sjkoshy	if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
300147191Sjkoshy		ps->ps_state = PL_STATE_ERROR;
301147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
302147191Sjkoshy		return -1;
303147191Sjkoshy	}
304147191Sjkoshy
305147191Sjkoshy	/* copy out the time stamp */
306147191Sjkoshy	PMCLOG_READ32(le,ev->pl_ts.tv_sec);
307147191Sjkoshy	PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
308147191Sjkoshy
309147191Sjkoshy	evlen = PMCLOG_HEADER_TO_LENGTH(h);
310147191Sjkoshy
311147191Sjkoshy#define	PMCLOG_GET_PATHLEN(P,E,TYPE) do {				\
312147191Sjkoshy		(P) = (E) - offsetof(struct TYPE, pl_pathname);		\
313147191Sjkoshy		if ((P) > PATH_MAX || (P) < 0)				\
314147191Sjkoshy			goto error;					\
315147191Sjkoshy	} while (0)
316147191Sjkoshy
317174215Sjkoshy#define	PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do {				\
318174215Sjkoshy		(SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc))	\
319174215Sjkoshy			/ sizeof(uintfptr_t);				\
320174215Sjkoshy	} while (0);
321174215Sjkoshy
322147191Sjkoshy	switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
323174215Sjkoshy	case PMCLOG_TYPE_CALLCHAIN:
324174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
325174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
326174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
327174215Sjkoshy		PMCLOG_GET_CALLCHAIN_SIZE(ev->pl_u.pl_cc.pl_npc,evlen);
328174215Sjkoshy		for (npc = 0; npc < ev->pl_u.pl_cc.pl_npc; npc++)
329174215Sjkoshy			PMCLOG_READADDR(le,ev->pl_u.pl_cc.pl_pc[npc]);
330174215Sjkoshy		for (;npc < PMC_CALLCHAIN_DEPTH_MAX; npc++)
331174215Sjkoshy			ev->pl_u.pl_cc.pl_pc[npc] = (uintfptr_t) 0;
332174215Sjkoshy		break;
333147191Sjkoshy	case PMCLOG_TYPE_CLOSELOG:
334147191Sjkoshy	case PMCLOG_TYPE_DROPNOTIFY:
335147191Sjkoshy		/* nothing to do */
336147191Sjkoshy		break;
337147191Sjkoshy	case PMCLOG_TYPE_INITIALIZE:
338147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
339147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
340147191Sjkoshy		ps->ps_version = ev->pl_u.pl_i.pl_version;
341147191Sjkoshy		ps->ps_arch = ev->pl_u.pl_i.pl_arch;
342147191Sjkoshy		ps->ps_initialized = 1;
343147191Sjkoshy		break;
344157144Sjkoshy	case PMCLOG_TYPE_MAP_IN:
345157144Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
346157144Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
347157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
348157144Sjkoshy		PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
349147191Sjkoshy		break;
350157144Sjkoshy	case PMCLOG_TYPE_MAP_OUT:
351157144Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
352157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
353157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
354157144Sjkoshy		break;
355147191Sjkoshy	case PMCLOG_TYPE_PCSAMPLE:
356147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid);
357147191Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc);
358147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid);
359147708Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_usermode);
360147191Sjkoshy		break;
361147191Sjkoshy	case PMCLOG_TYPE_PMCALLOCATE:
362147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
363147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
364147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
365147191Sjkoshy		if ((ev->pl_u.pl_a.pl_evname =
366147191Sjkoshy		    pmc_name_of_event(ev->pl_u.pl_a.pl_event)) == NULL)
367147191Sjkoshy			goto error;
368147191Sjkoshy		break;
369147191Sjkoshy	case PMCLOG_TYPE_PMCATTACH:
370147191Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
371147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
372147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
373147191Sjkoshy		PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
374147191Sjkoshy		break;
375147191Sjkoshy	case PMCLOG_TYPE_PMCDETACH:
376147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
377147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
378147191Sjkoshy		break;
379147191Sjkoshy	case PMCLOG_TYPE_PROCCSW:
380147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
381147191Sjkoshy		PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
382147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
383147191Sjkoshy		break;
384147191Sjkoshy	case PMCLOG_TYPE_PROCEXEC:
385147191Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
386147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
387147708Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
388147708Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
389147191Sjkoshy		PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
390147191Sjkoshy		break;
391147191Sjkoshy	case PMCLOG_TYPE_PROCEXIT:
392147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
393147191Sjkoshy		PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
394147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
395147191Sjkoshy		break;
396147191Sjkoshy	case PMCLOG_TYPE_PROCFORK:
397147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
398147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
399147191Sjkoshy		break;
400147191Sjkoshy	case PMCLOG_TYPE_SYSEXIT:
401147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
402147191Sjkoshy		break;
403147191Sjkoshy	case PMCLOG_TYPE_USERDATA:
404147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
405147191Sjkoshy		break;
406147191Sjkoshy	default:	/* unknown record type */
407147191Sjkoshy		ps->ps_state = PL_STATE_ERROR;
408147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
409174215Sjkoshy		return (-1);
410147191Sjkoshy	}
411147191Sjkoshy
412147191Sjkoshy	ev->pl_offset = (ps->ps_offset += evlen);
413147191Sjkoshy	ev->pl_count  = (ps->ps_count += 1);
414147191Sjkoshy	ev->pl_state = PMCLOG_OK;
415147191Sjkoshy	return 0;
416147191Sjkoshy
417147191Sjkoshy error:
418147191Sjkoshy	ev->pl_state = PMCLOG_ERROR;
419147191Sjkoshy	ps->ps_state = PL_STATE_ERROR;
420147191Sjkoshy	return -1;
421147191Sjkoshy}
422147191Sjkoshy
423147191Sjkoshy/*
424147191Sjkoshy * Extract and return the next event from the byte stream.
425147191Sjkoshy *
426147191Sjkoshy * Returns 0 and sets the event's state to PMCLOG_OK in case an event
427147191Sjkoshy * was successfully parsed.  Otherwise this function returns -1 and
428147191Sjkoshy * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
429147191Sjkoshy * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
430147191Sjkoshy * a parse error was encountered.
431147191Sjkoshy */
432147191Sjkoshy
433147191Sjkoshyint
434147191Sjkoshypmclog_read(void *cookie, struct pmclog_ev *ev)
435147191Sjkoshy{
436147864Sjkoshy	int retval;
437147191Sjkoshy	ssize_t nread;
438147191Sjkoshy	struct pmclog_parse_state *ps;
439147191Sjkoshy
440147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
441147191Sjkoshy
442147191Sjkoshy	if (ps->ps_state == PL_STATE_ERROR) {
443147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
444147191Sjkoshy		return -1;
445147191Sjkoshy	}
446147191Sjkoshy
447147191Sjkoshy	/*
448147191Sjkoshy	 * If there isn't enough data left for a new event try and get
449147191Sjkoshy	 * more data.
450147191Sjkoshy	 */
451147191Sjkoshy	if (ps->ps_len == 0) {
452147191Sjkoshy		ev->pl_state = PMCLOG_REQUIRE_DATA;
453147191Sjkoshy
454147191Sjkoshy		/*
455147191Sjkoshy		 * If we have a valid file descriptor to read from, attempt
456147191Sjkoshy		 * to read from that.  This read may return with an error,
457147191Sjkoshy		 * (which may be EAGAIN or other recoverable error), or
458147191Sjkoshy		 * can return EOF.
459147191Sjkoshy		 */
460147191Sjkoshy		if (ps->ps_fd != PMCLOG_FD_NONE) {
461147864Sjkoshy		refill:
462147191Sjkoshy			nread = read(ps->ps_fd, ps->ps_buffer,
463147191Sjkoshy			    PMCLOG_BUFFER_SIZE);
464147191Sjkoshy
465147191Sjkoshy			if (nread <= 0) {
466147708Sjkoshy				if (nread == 0)
467147708Sjkoshy					ev->pl_state = PMCLOG_EOF;
468147708Sjkoshy				else if (errno != EAGAIN) /* not restartable */
469147708Sjkoshy					ev->pl_state = PMCLOG_ERROR;
470147191Sjkoshy				return -1;
471147191Sjkoshy			}
472147191Sjkoshy
473147191Sjkoshy			ps->ps_len = nread;
474147191Sjkoshy			ps->ps_data = ps->ps_buffer;
475147191Sjkoshy		} else
476147191Sjkoshy			return -1;
477147191Sjkoshy	}
478147191Sjkoshy
479147191Sjkoshy	assert(ps->ps_len > 0);
480147191Sjkoshy
481147864Sjkoshy
482147864Sjkoshy	 /* Retrieve one event from the byte stream. */
483147864Sjkoshy	retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
484147864Sjkoshy
485147191Sjkoshy	/*
486147864Sjkoshy	 * If we need more data and we have a configured fd, try read
487147864Sjkoshy	 * from it.
488147191Sjkoshy	 */
489147864Sjkoshy	if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
490147864Sjkoshy	    ps->ps_fd != -1) {
491147864Sjkoshy		assert(ps->ps_len == 0);
492147864Sjkoshy		goto refill;
493147864Sjkoshy	}
494147864Sjkoshy
495147864Sjkoshy	return retval;
496147191Sjkoshy}
497147191Sjkoshy
498147191Sjkoshy/*
499147191Sjkoshy * Feed data to a memory based parser.
500147191Sjkoshy *
501147191Sjkoshy * The memory area pointed to by 'data' needs to be valid till the
502147191Sjkoshy * next error return from pmclog_next_event().
503147191Sjkoshy */
504147191Sjkoshy
505147191Sjkoshyint
506147191Sjkoshypmclog_feed(void *cookie, char *data, int len)
507147191Sjkoshy{
508147191Sjkoshy	struct pmclog_parse_state *ps;
509147191Sjkoshy
510147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
511147191Sjkoshy
512147191Sjkoshy	if (len < 0 ||		/* invalid length */
513147191Sjkoshy	    ps->ps_buffer ||	/* called for a file parser */
514147191Sjkoshy	    ps->ps_len != 0)	/* unnecessary call */
515147191Sjkoshy		return -1;
516147191Sjkoshy
517147191Sjkoshy	ps->ps_data = data;
518147191Sjkoshy	ps->ps_len  = len;
519147191Sjkoshy
520147191Sjkoshy	return 0;
521147191Sjkoshy}
522147191Sjkoshy
523147191Sjkoshy/*
524147191Sjkoshy * Allocate and initialize parser state.
525147191Sjkoshy */
526147191Sjkoshy
527147191Sjkoshyvoid *
528147191Sjkoshypmclog_open(int fd)
529147191Sjkoshy{
530147191Sjkoshy	struct pmclog_parse_state *ps;
531147191Sjkoshy
532147191Sjkoshy	if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
533147191Sjkoshy		return NULL;
534147191Sjkoshy
535147191Sjkoshy	ps->ps_state = PL_STATE_NEW_RECORD;
536147191Sjkoshy	ps->ps_arch = -1;
537147191Sjkoshy	ps->ps_initialized = 0;
538147191Sjkoshy	ps->ps_count = 0;
539147191Sjkoshy	ps->ps_offset = (off_t) 0;
540147191Sjkoshy	bzero(&ps->ps_saved, sizeof(ps->ps_saved));
541147191Sjkoshy	ps->ps_svcount = 0;
542147191Sjkoshy	ps->ps_fd    = fd;
543147191Sjkoshy	ps->ps_data  = NULL;
544147191Sjkoshy	ps->ps_buffer = NULL;
545147191Sjkoshy	ps->ps_len   = 0;
546147191Sjkoshy
547147191Sjkoshy	/* allocate space for a work area */
548147191Sjkoshy	if (ps->ps_fd != PMCLOG_FD_NONE) {
549147191Sjkoshy		if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL)
550147191Sjkoshy			return NULL;
551147191Sjkoshy	}
552147191Sjkoshy
553147191Sjkoshy	return ps;
554147191Sjkoshy}
555147191Sjkoshy
556147191Sjkoshy
557147191Sjkoshy/*
558147191Sjkoshy * Free up parser state.
559147191Sjkoshy */
560147191Sjkoshy
561147191Sjkoshyvoid
562147191Sjkoshypmclog_close(void *cookie)
563147191Sjkoshy{
564147191Sjkoshy	struct pmclog_parse_state *ps;
565147191Sjkoshy
566147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
567147191Sjkoshy
568147191Sjkoshy	if (ps->ps_buffer)
569147191Sjkoshy		free(ps->ps_buffer);
570147191Sjkoshy
571147191Sjkoshy	free(ps);
572147191Sjkoshy}
573