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: stable/11/lib/libpmc/pmclog.c 331722 2018-03-29 02:50:57Z eadler $");
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
50185363Sjkoshy#include "libpmcinternal.h"
51185363Sjkoshy
52147191Sjkoshy#define	PMCLOG_BUFFER_SIZE			4096
53147191Sjkoshy
54147191Sjkoshy/*
55147191Sjkoshy * API NOTES
56147191Sjkoshy *
57147191Sjkoshy * The pmclog(3) API is oriented towards parsing an event stream in
58147191Sjkoshy * "realtime", i.e., from an data source that may or may not preserve
59147191Sjkoshy * record boundaries -- for example when the data source is elsewhere
60147191Sjkoshy * on a network.  The API allows data to be fed into the parser zero
61147191Sjkoshy * or more bytes at a time.
62147191Sjkoshy *
63147191Sjkoshy * The state for a log file parser is maintained in a 'struct
64147191Sjkoshy * pmclog_parse_state'.  Parser invocations are done by calling
65147191Sjkoshy * 'pmclog_read()'; this function will inform the caller when a
66147191Sjkoshy * complete event is parsed.
67147191Sjkoshy *
68147191Sjkoshy * The parser first assembles a complete log file event in an internal
69147191Sjkoshy * work area (see "ps_saved" below).  Once a complete log file event
70147191Sjkoshy * is read, the parser then parses it and converts it to an event
71147191Sjkoshy * descriptor usable by the client.  We could possibly avoid this two
72147191Sjkoshy * step process by directly parsing the input log to set fields in the
73147191Sjkoshy * event record.  However the parser's state machine would get
74147191Sjkoshy * insanely complicated, and this code is unlikely to be used in
75147191Sjkoshy * performance critical paths.
76147191Sjkoshy */
77147191Sjkoshy
78147191Sjkoshyenum pmclog_parser_state {
79147191Sjkoshy	PL_STATE_NEW_RECORD,		/* in-between records */
80147191Sjkoshy	PL_STATE_EXPECTING_HEADER,	/* header being read */
81147191Sjkoshy	PL_STATE_PARTIAL_RECORD,	/* header present but not the record */
82147191Sjkoshy	PL_STATE_ERROR			/* parsing error encountered */
83147191Sjkoshy};
84147191Sjkoshy
85147191Sjkoshystruct pmclog_parse_state {
86147191Sjkoshy	enum pmclog_parser_state ps_state;
87147191Sjkoshy	enum pmc_cputype	ps_arch;	/* log file architecture */
88147191Sjkoshy	uint32_t		ps_version;	/* hwpmc version */
89147191Sjkoshy	int			ps_initialized;	/* whether initialized */
90147191Sjkoshy	int			ps_count;	/* count of records processed */
91147191Sjkoshy	off_t			ps_offset;	/* stream byte offset */
92147191Sjkoshy	union pmclog_entry	ps_saved;	/* saved partial log entry */
93147191Sjkoshy	int			ps_svcount;	/* #bytes saved */
94147191Sjkoshy	int			ps_fd;		/* active fd or -1 */
95147191Sjkoshy	char			*ps_buffer;	/* scratch buffer if fd != -1 */
96147191Sjkoshy	char			*ps_data;	/* current parse pointer */
97147191Sjkoshy	size_t			ps_len;		/* length of buffered data */
98147191Sjkoshy};
99147191Sjkoshy
100147191Sjkoshy#define	PMCLOG_HEADER_FROM_SAVED_STATE(PS)				\
101147191Sjkoshy	(* ((uint32_t *) &(PS)->ps_saved))
102147191Sjkoshy
103147191Sjkoshy#define	PMCLOG_INITIALIZE_READER(LE,A)	LE = (uint32_t *) &(A)
104147191Sjkoshy#define	PMCLOG_READ32(LE,V) 		do {				\
105147191Sjkoshy		(V)  = *(LE)++;						\
106147191Sjkoshy	} while (0)
107147191Sjkoshy#define	PMCLOG_READ64(LE,V)		do {				\
108147191Sjkoshy		uint64_t _v;						\
109147191Sjkoshy		_v  = (uint64_t) *(LE)++;				\
110147191Sjkoshy		_v |= ((uint64_t) *(LE)++) << 32;			\
111147191Sjkoshy		(V) = _v;						\
112147191Sjkoshy	} while (0)
113147191Sjkoshy
114147191Sjkoshy#define	PMCLOG_READSTRING(LE,DST,LEN)	strlcpy((DST), (char *) (LE), (LEN))
115147191Sjkoshy
116147191Sjkoshy/*
117147191Sjkoshy * Assemble a log record from '*len' octets starting from address '*data'.
118147191Sjkoshy * Update 'data' and 'len' to reflect the number of bytes consumed.
119147191Sjkoshy *
120147191Sjkoshy * '*data' is potentially an unaligned address and '*len' octets may
121147191Sjkoshy * not be enough to complete a event record.
122147191Sjkoshy */
123147191Sjkoshy
124147191Sjkoshystatic enum pmclog_parser_state
125147191Sjkoshypmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
126147191Sjkoshy{
127147191Sjkoshy	int avail, copylen, recordsize, used;
128147191Sjkoshy	uint32_t h;
129147191Sjkoshy	const int HEADERSIZE = sizeof(uint32_t);
130147191Sjkoshy	char *src, *dst;
131147191Sjkoshy
132147191Sjkoshy	if ((avail = *len) <= 0)
133147191Sjkoshy		return (ps->ps_state = PL_STATE_ERROR);
134147191Sjkoshy
135147191Sjkoshy	src = *data;
136147191Sjkoshy	h = used = 0;
137147191Sjkoshy
138147191Sjkoshy	if (ps->ps_state == PL_STATE_NEW_RECORD)
139147191Sjkoshy		ps->ps_svcount = 0;
140147191Sjkoshy
141147191Sjkoshy	dst = (char *) &ps->ps_saved + ps->ps_svcount;
142147191Sjkoshy
143147191Sjkoshy	switch (ps->ps_state) {
144147191Sjkoshy	case PL_STATE_NEW_RECORD:
145147191Sjkoshy
146147191Sjkoshy		/*
147147191Sjkoshy		 * Transitions:
148147191Sjkoshy		 *
149147191Sjkoshy		 * Case A: avail < headersize
150147191Sjkoshy		 *	-> 'expecting header'
151147191Sjkoshy		 *
152147191Sjkoshy		 * Case B: avail >= headersize
153147191Sjkoshy		 *    B.1: avail < recordsize
154147191Sjkoshy		 *	   -> 'partial record'
155147191Sjkoshy		 *    B.2: avail >= recordsize
156147191Sjkoshy		 *         -> 'new record'
157147191Sjkoshy		 */
158147191Sjkoshy
159147191Sjkoshy		copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
160147191Sjkoshy		bcopy(src, dst, copylen);
161147191Sjkoshy		ps->ps_svcount = used = copylen;
162147191Sjkoshy
163147191Sjkoshy		if (copylen < HEADERSIZE) {
164147191Sjkoshy			ps->ps_state = PL_STATE_EXPECTING_HEADER;
165147191Sjkoshy			goto done;
166147191Sjkoshy		}
167147191Sjkoshy
168147191Sjkoshy		src += copylen;
169147191Sjkoshy		dst += copylen;
170147191Sjkoshy
171147191Sjkoshy		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
172147191Sjkoshy		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
173147191Sjkoshy
174147191Sjkoshy		if (recordsize <= 0)
175147191Sjkoshy			goto error;
176147191Sjkoshy
177147191Sjkoshy		if (recordsize <= avail) { /* full record available */
178147191Sjkoshy			bcopy(src, dst, recordsize - copylen);
179147191Sjkoshy			ps->ps_svcount = used = recordsize;
180147191Sjkoshy			goto done;
181147191Sjkoshy		}
182147191Sjkoshy
183147191Sjkoshy		/* header + a partial record is available */
184147191Sjkoshy		bcopy(src, dst, avail - copylen);
185147191Sjkoshy		ps->ps_svcount = used = avail;
186147191Sjkoshy		ps->ps_state = PL_STATE_PARTIAL_RECORD;
187147191Sjkoshy
188147191Sjkoshy		break;
189147191Sjkoshy
190147191Sjkoshy	case PL_STATE_EXPECTING_HEADER:
191147191Sjkoshy
192147191Sjkoshy		/*
193147191Sjkoshy		 * Transitions:
194147191Sjkoshy		 *
195147191Sjkoshy		 * Case C: avail+saved < headersize
196147191Sjkoshy		 * 	-> 'expecting header'
197147191Sjkoshy		 *
198147191Sjkoshy		 * Case D: avail+saved >= headersize
199147191Sjkoshy		 *    D.1: avail+saved < recordsize
200147191Sjkoshy		 *    	-> 'partial record'
201147191Sjkoshy		 *    D.2: avail+saved >= recordsize
202147191Sjkoshy		 *    	-> 'new record'
203147191Sjkoshy		 *    (see PARTIAL_RECORD handling below)
204147191Sjkoshy		 */
205147191Sjkoshy
206147191Sjkoshy		if (avail + ps->ps_svcount < HEADERSIZE) {
207147191Sjkoshy			bcopy(src, dst, avail);
208147191Sjkoshy			ps->ps_svcount += avail;
209147191Sjkoshy			used = avail;
210147191Sjkoshy			break;
211147191Sjkoshy		}
212147191Sjkoshy
213147191Sjkoshy		used = copylen = HEADERSIZE - ps->ps_svcount;
214147191Sjkoshy		bcopy(src, dst, copylen);
215147191Sjkoshy		src += copylen;
216147191Sjkoshy		dst += copylen;
217147191Sjkoshy		avail -= copylen;
218147191Sjkoshy		ps->ps_svcount += copylen;
219147191Sjkoshy
220147191Sjkoshy		/*FALLTHROUGH*/
221147191Sjkoshy
222147191Sjkoshy	case PL_STATE_PARTIAL_RECORD:
223147191Sjkoshy
224147191Sjkoshy		/*
225147191Sjkoshy		 * Transitions:
226147191Sjkoshy		 *
227147191Sjkoshy		 * Case E: avail+saved < recordsize
228147191Sjkoshy		 * 	-> 'partial record'
229147191Sjkoshy		 *
230147191Sjkoshy		 * Case F: avail+saved >= recordsize
231147191Sjkoshy		 * 	-> 'new record'
232147191Sjkoshy		 */
233147191Sjkoshy
234147191Sjkoshy		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
235147191Sjkoshy		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
236147191Sjkoshy
237147191Sjkoshy		if (recordsize <= 0)
238147191Sjkoshy			goto error;
239147191Sjkoshy
240147191Sjkoshy		if (avail + ps->ps_svcount < recordsize) {
241147191Sjkoshy			copylen = avail;
242147191Sjkoshy			ps->ps_state = PL_STATE_PARTIAL_RECORD;
243147191Sjkoshy		} else {
244147191Sjkoshy			copylen = recordsize - ps->ps_svcount;
245147191Sjkoshy			ps->ps_state = PL_STATE_NEW_RECORD;
246147191Sjkoshy		}
247147191Sjkoshy
248147191Sjkoshy		bcopy(src, dst, copylen);
249147191Sjkoshy		ps->ps_svcount += copylen;
250147191Sjkoshy		used += copylen;
251147191Sjkoshy		break;
252147191Sjkoshy
253147191Sjkoshy	default:
254147191Sjkoshy		goto error;
255147191Sjkoshy	}
256147191Sjkoshy
257147191Sjkoshy done:
258147191Sjkoshy	*data += used;
259147191Sjkoshy	*len  -= used;
260147191Sjkoshy	return ps->ps_state;
261147191Sjkoshy
262147191Sjkoshy error:
263147191Sjkoshy	ps->ps_state = PL_STATE_ERROR;
264147191Sjkoshy	return ps->ps_state;
265147191Sjkoshy}
266147191Sjkoshy
267147191Sjkoshy/*
268147191Sjkoshy * Get an event from the stream pointed to by '*data'.  '*len'
269147191Sjkoshy * indicates the number of bytes available to parse.  Arguments
270147191Sjkoshy * '*data' and '*len' are updated to indicate the number of bytes
271147191Sjkoshy * consumed.
272147191Sjkoshy */
273147191Sjkoshy
274147191Sjkoshystatic int
275147191Sjkoshypmclog_get_event(void *cookie, char **data, ssize_t *len,
276147191Sjkoshy    struct pmclog_ev *ev)
277147191Sjkoshy{
278147191Sjkoshy	int evlen, pathlen;
279174215Sjkoshy	uint32_t h, *le, npc;
280147191Sjkoshy	enum pmclog_parser_state e;
281147191Sjkoshy	struct pmclog_parse_state *ps;
282147191Sjkoshy
283147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
284147191Sjkoshy
285147191Sjkoshy	assert(ps->ps_state != PL_STATE_ERROR);
286147191Sjkoshy
287147191Sjkoshy	if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
288147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
289147191Sjkoshy		return -1;
290147191Sjkoshy	}
291147191Sjkoshy
292147191Sjkoshy	if (e != PL_STATE_NEW_RECORD) {
293147191Sjkoshy		ev->pl_state = PMCLOG_REQUIRE_DATA;
294147191Sjkoshy		return -1;
295147191Sjkoshy	}
296147191Sjkoshy
297147191Sjkoshy	PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
298147191Sjkoshy
299147191Sjkoshy	PMCLOG_READ32(le,h);
300147191Sjkoshy
301147191Sjkoshy	if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
302147191Sjkoshy		ps->ps_state = PL_STATE_ERROR;
303147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
304147191Sjkoshy		return -1;
305147191Sjkoshy	}
306147191Sjkoshy
307147191Sjkoshy	/* copy out the time stamp */
308147191Sjkoshy	PMCLOG_READ32(le,ev->pl_ts.tv_sec);
309147191Sjkoshy	PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
310147191Sjkoshy
311147191Sjkoshy	evlen = PMCLOG_HEADER_TO_LENGTH(h);
312147191Sjkoshy
313147191Sjkoshy#define	PMCLOG_GET_PATHLEN(P,E,TYPE) do {				\
314147191Sjkoshy		(P) = (E) - offsetof(struct TYPE, pl_pathname);		\
315147191Sjkoshy		if ((P) > PATH_MAX || (P) < 0)				\
316147191Sjkoshy			goto error;					\
317147191Sjkoshy	} while (0)
318147191Sjkoshy
319174215Sjkoshy#define	PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do {				\
320174215Sjkoshy		(SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc))	\
321174215Sjkoshy			/ sizeof(uintfptr_t);				\
322174215Sjkoshy	} while (0);
323174215Sjkoshy
324147191Sjkoshy	switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
325174215Sjkoshy	case PMCLOG_TYPE_CALLCHAIN:
326174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
327174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
328174215Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
329174215Sjkoshy		PMCLOG_GET_CALLCHAIN_SIZE(ev->pl_u.pl_cc.pl_npc,evlen);
330174215Sjkoshy		for (npc = 0; npc < ev->pl_u.pl_cc.pl_npc; npc++)
331174215Sjkoshy			PMCLOG_READADDR(le,ev->pl_u.pl_cc.pl_pc[npc]);
332174215Sjkoshy		for (;npc < PMC_CALLCHAIN_DEPTH_MAX; npc++)
333174215Sjkoshy			ev->pl_u.pl_cc.pl_pc[npc] = (uintfptr_t) 0;
334174215Sjkoshy		break;
335147191Sjkoshy	case PMCLOG_TYPE_CLOSELOG:
336331319Skib		ev->pl_state = PMCLOG_EOF;
337331319Skib		return (-1);
338147191Sjkoshy	case PMCLOG_TYPE_DROPNOTIFY:
339147191Sjkoshy		/* nothing to do */
340147191Sjkoshy		break;
341147191Sjkoshy	case PMCLOG_TYPE_INITIALIZE:
342147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
343147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
344147191Sjkoshy		ps->ps_version = ev->pl_u.pl_i.pl_version;
345147191Sjkoshy		ps->ps_arch = ev->pl_u.pl_i.pl_arch;
346147191Sjkoshy		ps->ps_initialized = 1;
347147191Sjkoshy		break;
348157144Sjkoshy	case PMCLOG_TYPE_MAP_IN:
349157144Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
350157144Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
351157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
352157144Sjkoshy		PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
353147191Sjkoshy		break;
354157144Sjkoshy	case PMCLOG_TYPE_MAP_OUT:
355157144Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
356157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
357157144Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
358157144Sjkoshy		break;
359147191Sjkoshy	case PMCLOG_TYPE_PCSAMPLE:
360147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid);
361147191Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc);
362147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid);
363147708Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_s.pl_usermode);
364147191Sjkoshy		break;
365147191Sjkoshy	case PMCLOG_TYPE_PMCALLOCATE:
366147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
367147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
368147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
369147191Sjkoshy		if ((ev->pl_u.pl_a.pl_evname =
370185363Sjkoshy		    _pmc_name_of_event(ev->pl_u.pl_a.pl_event, ps->ps_arch))
371185363Sjkoshy		    == NULL)
372147191Sjkoshy			goto error;
373147191Sjkoshy		break;
374233628Sfabient	case PMCLOG_TYPE_PMCALLOCATEDYN:
375233628Sfabient		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_pmcid);
376233628Sfabient		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_event);
377233628Sfabient		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_flags);
378233628Sfabient		PMCLOG_READSTRING(le,ev->pl_u.pl_ad.pl_evname,PMC_NAME_MAX);
379233628Sfabient		break;
380147191Sjkoshy	case PMCLOG_TYPE_PMCATTACH:
381147191Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
382147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
383147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
384147191Sjkoshy		PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
385147191Sjkoshy		break;
386147191Sjkoshy	case PMCLOG_TYPE_PMCDETACH:
387147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
388147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
389147191Sjkoshy		break;
390147191Sjkoshy	case PMCLOG_TYPE_PROCCSW:
391147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
392147191Sjkoshy		PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
393147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
394147191Sjkoshy		break;
395147191Sjkoshy	case PMCLOG_TYPE_PROCEXEC:
396147191Sjkoshy		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
397147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
398147708Sjkoshy		PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
399147708Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
400147191Sjkoshy		PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
401147191Sjkoshy		break;
402147191Sjkoshy	case PMCLOG_TYPE_PROCEXIT:
403147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
404147191Sjkoshy		PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
405147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
406147191Sjkoshy		break;
407147191Sjkoshy	case PMCLOG_TYPE_PROCFORK:
408147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
409147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
410147191Sjkoshy		break;
411147191Sjkoshy	case PMCLOG_TYPE_SYSEXIT:
412147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
413147191Sjkoshy		break;
414147191Sjkoshy	case PMCLOG_TYPE_USERDATA:
415147191Sjkoshy		PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
416147191Sjkoshy		break;
417147191Sjkoshy	default:	/* unknown record type */
418147191Sjkoshy		ps->ps_state = PL_STATE_ERROR;
419147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
420174215Sjkoshy		return (-1);
421147191Sjkoshy	}
422147191Sjkoshy
423147191Sjkoshy	ev->pl_offset = (ps->ps_offset += evlen);
424147191Sjkoshy	ev->pl_count  = (ps->ps_count += 1);
425147191Sjkoshy	ev->pl_state = PMCLOG_OK;
426147191Sjkoshy	return 0;
427147191Sjkoshy
428147191Sjkoshy error:
429147191Sjkoshy	ev->pl_state = PMCLOG_ERROR;
430147191Sjkoshy	ps->ps_state = PL_STATE_ERROR;
431147191Sjkoshy	return -1;
432147191Sjkoshy}
433147191Sjkoshy
434147191Sjkoshy/*
435147191Sjkoshy * Extract and return the next event from the byte stream.
436147191Sjkoshy *
437147191Sjkoshy * Returns 0 and sets the event's state to PMCLOG_OK in case an event
438147191Sjkoshy * was successfully parsed.  Otherwise this function returns -1 and
439147191Sjkoshy * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
440147191Sjkoshy * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
441147191Sjkoshy * a parse error was encountered.
442147191Sjkoshy */
443147191Sjkoshy
444147191Sjkoshyint
445147191Sjkoshypmclog_read(void *cookie, struct pmclog_ev *ev)
446147191Sjkoshy{
447147864Sjkoshy	int retval;
448147191Sjkoshy	ssize_t nread;
449147191Sjkoshy	struct pmclog_parse_state *ps;
450147191Sjkoshy
451147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
452147191Sjkoshy
453147191Sjkoshy	if (ps->ps_state == PL_STATE_ERROR) {
454147191Sjkoshy		ev->pl_state = PMCLOG_ERROR;
455147191Sjkoshy		return -1;
456147191Sjkoshy	}
457147191Sjkoshy
458147191Sjkoshy	/*
459147191Sjkoshy	 * If there isn't enough data left for a new event try and get
460147191Sjkoshy	 * more data.
461147191Sjkoshy	 */
462147191Sjkoshy	if (ps->ps_len == 0) {
463147191Sjkoshy		ev->pl_state = PMCLOG_REQUIRE_DATA;
464147191Sjkoshy
465147191Sjkoshy		/*
466147191Sjkoshy		 * If we have a valid file descriptor to read from, attempt
467147191Sjkoshy		 * to read from that.  This read may return with an error,
468147191Sjkoshy		 * (which may be EAGAIN or other recoverable error), or
469147191Sjkoshy		 * can return EOF.
470147191Sjkoshy		 */
471147191Sjkoshy		if (ps->ps_fd != PMCLOG_FD_NONE) {
472147864Sjkoshy		refill:
473147191Sjkoshy			nread = read(ps->ps_fd, ps->ps_buffer,
474147191Sjkoshy			    PMCLOG_BUFFER_SIZE);
475147191Sjkoshy
476147191Sjkoshy			if (nread <= 0) {
477147708Sjkoshy				if (nread == 0)
478147708Sjkoshy					ev->pl_state = PMCLOG_EOF;
479147708Sjkoshy				else if (errno != EAGAIN) /* not restartable */
480147708Sjkoshy					ev->pl_state = PMCLOG_ERROR;
481147191Sjkoshy				return -1;
482147191Sjkoshy			}
483147191Sjkoshy
484147191Sjkoshy			ps->ps_len = nread;
485147191Sjkoshy			ps->ps_data = ps->ps_buffer;
486147191Sjkoshy		} else
487147191Sjkoshy			return -1;
488147191Sjkoshy	}
489147191Sjkoshy
490147191Sjkoshy	assert(ps->ps_len > 0);
491147191Sjkoshy
492147864Sjkoshy
493147864Sjkoshy	 /* Retrieve one event from the byte stream. */
494147864Sjkoshy	retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
495147864Sjkoshy
496147191Sjkoshy	/*
497147864Sjkoshy	 * If we need more data and we have a configured fd, try read
498147864Sjkoshy	 * from it.
499147191Sjkoshy	 */
500147864Sjkoshy	if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
501147864Sjkoshy	    ps->ps_fd != -1) {
502147864Sjkoshy		assert(ps->ps_len == 0);
503147864Sjkoshy		goto refill;
504147864Sjkoshy	}
505147864Sjkoshy
506147864Sjkoshy	return retval;
507147191Sjkoshy}
508147191Sjkoshy
509147191Sjkoshy/*
510147191Sjkoshy * Feed data to a memory based parser.
511147191Sjkoshy *
512147191Sjkoshy * The memory area pointed to by 'data' needs to be valid till the
513147191Sjkoshy * next error return from pmclog_next_event().
514147191Sjkoshy */
515147191Sjkoshy
516147191Sjkoshyint
517147191Sjkoshypmclog_feed(void *cookie, char *data, int len)
518147191Sjkoshy{
519147191Sjkoshy	struct pmclog_parse_state *ps;
520147191Sjkoshy
521147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
522147191Sjkoshy
523147191Sjkoshy	if (len < 0 ||		/* invalid length */
524147191Sjkoshy	    ps->ps_buffer ||	/* called for a file parser */
525147191Sjkoshy	    ps->ps_len != 0)	/* unnecessary call */
526147191Sjkoshy		return -1;
527147191Sjkoshy
528147191Sjkoshy	ps->ps_data = data;
529147191Sjkoshy	ps->ps_len  = len;
530147191Sjkoshy
531147191Sjkoshy	return 0;
532147191Sjkoshy}
533147191Sjkoshy
534147191Sjkoshy/*
535147191Sjkoshy * Allocate and initialize parser state.
536147191Sjkoshy */
537147191Sjkoshy
538147191Sjkoshyvoid *
539147191Sjkoshypmclog_open(int fd)
540147191Sjkoshy{
541147191Sjkoshy	struct pmclog_parse_state *ps;
542147191Sjkoshy
543147191Sjkoshy	if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
544147191Sjkoshy		return NULL;
545147191Sjkoshy
546147191Sjkoshy	ps->ps_state = PL_STATE_NEW_RECORD;
547147191Sjkoshy	ps->ps_arch = -1;
548147191Sjkoshy	ps->ps_initialized = 0;
549147191Sjkoshy	ps->ps_count = 0;
550147191Sjkoshy	ps->ps_offset = (off_t) 0;
551147191Sjkoshy	bzero(&ps->ps_saved, sizeof(ps->ps_saved));
552147191Sjkoshy	ps->ps_svcount = 0;
553147191Sjkoshy	ps->ps_fd    = fd;
554147191Sjkoshy	ps->ps_data  = NULL;
555147191Sjkoshy	ps->ps_buffer = NULL;
556147191Sjkoshy	ps->ps_len   = 0;
557147191Sjkoshy
558147191Sjkoshy	/* allocate space for a work area */
559147191Sjkoshy	if (ps->ps_fd != PMCLOG_FD_NONE) {
560208860Sfabient		if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL) {
561208860Sfabient			free(ps);
562147191Sjkoshy			return NULL;
563208860Sfabient		}
564147191Sjkoshy	}
565147191Sjkoshy
566147191Sjkoshy	return ps;
567147191Sjkoshy}
568147191Sjkoshy
569147191Sjkoshy
570147191Sjkoshy/*
571147191Sjkoshy * Free up parser state.
572147191Sjkoshy */
573147191Sjkoshy
574147191Sjkoshyvoid
575147191Sjkoshypmclog_close(void *cookie)
576147191Sjkoshy{
577147191Sjkoshy	struct pmclog_parse_state *ps;
578147191Sjkoshy
579147191Sjkoshy	ps = (struct pmclog_parse_state *) cookie;
580147191Sjkoshy
581147191Sjkoshy	if (ps->ps_buffer)
582147191Sjkoshy		free(ps->ps_buffer);
583147191Sjkoshy
584147191Sjkoshy	free(ps);
585147191Sjkoshy}
586