xref: /freebsd/lib/libpmc/pmclog.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2007 Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by A. Joseph Koshy under
9  * sponsorship from the FreeBSD Foundation and Google, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/pmc.h>
38 #include <sys/pmclog.h>
39 
40 #include <assert.h>
41 #include <errno.h>
42 #include <pmc.h>
43 #include <pmclog.h>
44 #include <stddef.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 
51 #include <machine/pmc_mdep.h>
52 
53 #include "libpmcinternal.h"
54 
55 #define	PMCLOG_BUFFER_SIZE			512*1024
56 
57 /*
58  * API NOTES
59  *
60  * The pmclog(3) API is oriented towards parsing an event stream in
61  * "realtime", i.e., from an data source that may or may not preserve
62  * record boundaries -- for example when the data source is elsewhere
63  * on a network.  The API allows data to be fed into the parser zero
64  * or more bytes at a time.
65  *
66  * The state for a log file parser is maintained in a 'struct
67  * pmclog_parse_state'.  Parser invocations are done by calling
68  * 'pmclog_read()'; this function will inform the caller when a
69  * complete event is parsed.
70  *
71  * The parser first assembles a complete log file event in an internal
72  * work area (see "ps_saved" below).  Once a complete log file event
73  * is read, the parser then parses it and converts it to an event
74  * descriptor usable by the client.  We could possibly avoid this two
75  * step process by directly parsing the input log to set fields in the
76  * event record.  However the parser's state machine would get
77  * insanely complicated, and this code is unlikely to be used in
78  * performance critical paths.
79  */
80 
81 #define	PMCLOG_HEADER_FROM_SAVED_STATE(PS)				\
82 	(* ((uint32_t *) &(PS)->ps_saved))
83 
84 #define	PMCLOG_INITIALIZE_READER(LE,A)	LE = (uint32_t *) &(A)
85 #define	PMCLOG_SKIP32(LE)		(LE)++
86 #define	PMCLOG_READ32(LE,V) 		do {				\
87 		(V)  = *(LE)++;						\
88 	} while (0)
89 #define	PMCLOG_READ64(LE,V)		do {				\
90 		uint64_t _v;						\
91 		_v  = (uint64_t) *(LE)++;				\
92 		_v |= ((uint64_t) *(LE)++) << 32;			\
93 		(V) = _v;						\
94 	} while (0)
95 
96 #define	PMCLOG_READSTRING(LE,DST,LEN)	strlcpy((DST), (char *) (LE), (LEN))
97 
98 /*
99  * Assemble a log record from '*len' octets starting from address '*data'.
100  * Update 'data' and 'len' to reflect the number of bytes consumed.
101  *
102  * '*data' is potentially an unaligned address and '*len' octets may
103  * not be enough to complete a event record.
104  */
105 
106 static enum pmclog_parser_state
107 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
108 {
109 	int avail, copylen, recordsize, used;
110 	uint32_t h;
111 	const int HEADERSIZE = sizeof(uint32_t);
112 	char *src, *dst;
113 
114 	if ((avail = *len) <= 0)
115 		return (ps->ps_state = PL_STATE_ERROR);
116 
117 	src = *data;
118 	used = 0;
119 
120 	if (ps->ps_state == PL_STATE_NEW_RECORD)
121 		ps->ps_svcount = 0;
122 
123 	dst = (char *) &ps->ps_saved + ps->ps_svcount;
124 
125 	switch (ps->ps_state) {
126 	case PL_STATE_NEW_RECORD:
127 
128 		/*
129 		 * Transitions:
130 		 *
131 		 * Case A: avail < headersize
132 		 *	-> 'expecting header'
133 		 *
134 		 * Case B: avail >= headersize
135 		 *    B.1: avail < recordsize
136 		 *	   -> 'partial record'
137 		 *    B.2: avail >= recordsize
138 		 *         -> 'new record'
139 		 */
140 
141 		copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
142 		bcopy(src, dst, copylen);
143 		ps->ps_svcount = used = copylen;
144 
145 		if (copylen < HEADERSIZE) {
146 			ps->ps_state = PL_STATE_EXPECTING_HEADER;
147 			goto done;
148 		}
149 
150 		src += copylen;
151 		dst += copylen;
152 
153 		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
154 		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
155 
156 		if (recordsize <= 0)
157 			goto error;
158 
159 		if (recordsize <= avail) { /* full record available */
160 			bcopy(src, dst, recordsize - copylen);
161 			ps->ps_svcount = used = recordsize;
162 			goto done;
163 		}
164 
165 		/* header + a partial record is available */
166 		bcopy(src, dst, avail - copylen);
167 		ps->ps_svcount = used = avail;
168 		ps->ps_state = PL_STATE_PARTIAL_RECORD;
169 
170 		break;
171 
172 	case PL_STATE_EXPECTING_HEADER:
173 
174 		/*
175 		 * Transitions:
176 		 *
177 		 * Case C: avail+saved < headersize
178 		 * 	-> 'expecting header'
179 		 *
180 		 * Case D: avail+saved >= headersize
181 		 *    D.1: avail+saved < recordsize
182 		 *    	-> 'partial record'
183 		 *    D.2: avail+saved >= recordsize
184 		 *    	-> 'new record'
185 		 *    (see PARTIAL_RECORD handling below)
186 		 */
187 
188 		if (avail + ps->ps_svcount < HEADERSIZE) {
189 			bcopy(src, dst, avail);
190 			ps->ps_svcount += avail;
191 			used = avail;
192 			break;
193 		}
194 
195 		used = copylen = HEADERSIZE - ps->ps_svcount;
196 		bcopy(src, dst, copylen);
197 		src += copylen;
198 		dst += copylen;
199 		avail -= copylen;
200 		ps->ps_svcount += copylen;
201 
202 		/*FALLTHROUGH*/
203 
204 	case PL_STATE_PARTIAL_RECORD:
205 
206 		/*
207 		 * Transitions:
208 		 *
209 		 * Case E: avail+saved < recordsize
210 		 * 	-> 'partial record'
211 		 *
212 		 * Case F: avail+saved >= recordsize
213 		 * 	-> 'new record'
214 		 */
215 
216 		h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
217 		recordsize = PMCLOG_HEADER_TO_LENGTH(h);
218 
219 		if (recordsize <= 0)
220 			goto error;
221 
222 		if (avail + ps->ps_svcount < recordsize) {
223 			copylen = avail;
224 			ps->ps_state = PL_STATE_PARTIAL_RECORD;
225 		} else {
226 			copylen = recordsize - ps->ps_svcount;
227 			ps->ps_state = PL_STATE_NEW_RECORD;
228 		}
229 
230 		bcopy(src, dst, copylen);
231 		ps->ps_svcount += copylen;
232 		used += copylen;
233 		break;
234 
235 	default:
236 		goto error;
237 	}
238 
239  done:
240 	*data += used;
241 	*len  -= used;
242 	return ps->ps_state;
243 
244  error:
245 	ps->ps_state = PL_STATE_ERROR;
246 	return ps->ps_state;
247 }
248 
249 /*
250  * Get an event from the stream pointed to by '*data'.  '*len'
251  * indicates the number of bytes available to parse.  Arguments
252  * '*data' and '*len' are updated to indicate the number of bytes
253  * consumed.
254  */
255 
256 static int
257 pmclog_get_event(void *cookie, char **data, ssize_t *len,
258     struct pmclog_ev *ev)
259 {
260 	int evlen, pathlen;
261 	uint32_t h, *le, npc;
262 	enum pmclog_parser_state e;
263 	struct pmclog_parse_state *ps;
264 	struct pmclog_header *ph;
265 
266 	ps = (struct pmclog_parse_state *) cookie;
267 
268 	assert(ps->ps_state != PL_STATE_ERROR);
269 
270 	if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
271 		ev->pl_state = PMCLOG_ERROR;
272 		printf("state error\n");
273 		return -1;
274 	}
275 
276 	if (e != PL_STATE_NEW_RECORD) {
277 		ev->pl_state = PMCLOG_REQUIRE_DATA;
278 		return -1;
279 	}
280 
281 	PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
282 	ev->pl_data = le;
283 	ph = (struct pmclog_header *)(uintptr_t)le;
284 
285 	h = ph->pl_header;
286 	if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
287 		printf("bad magic\n");
288 		ps->ps_state = PL_STATE_ERROR;
289 		ev->pl_state = PMCLOG_ERROR;
290 		return -1;
291 	}
292 
293 	/* copy out the time stamp */
294 	ev->pl_ts.tv_sec = ph->pl_tsc;
295 	le += sizeof(*ph)/4;
296 
297 	evlen = PMCLOG_HEADER_TO_LENGTH(h);
298 
299 #define	PMCLOG_GET_PATHLEN(P,E,TYPE) do {				\
300 		(P) = (E) - offsetof(struct TYPE, pl_pathname);		\
301 		if ((P) > PATH_MAX || (P) < 0)				\
302 			goto error;					\
303 	} while (0)
304 
305 #define	PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do {				\
306 		(SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc))	\
307 			/ sizeof(uintfptr_t);				\
308 	} while (0);
309 
310 	switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
311 	case PMCLOG_TYPE_CALLCHAIN:
312 		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
313 		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_tid);
314 		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
315 		PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
316 		PMCLOG_GET_CALLCHAIN_SIZE(ev->pl_u.pl_cc.pl_npc,evlen);
317 		for (npc = 0; npc < ev->pl_u.pl_cc.pl_npc; npc++)
318 			PMCLOG_READADDR(le,ev->pl_u.pl_cc.pl_pc[npc]);
319 		for (;npc < PMC_CALLCHAIN_DEPTH_MAX; npc++)
320 			ev->pl_u.pl_cc.pl_pc[npc] = (uintfptr_t) 0;
321 		break;
322 	case PMCLOG_TYPE_CLOSELOG:
323 		ev->pl_state = PMCLOG_EOF;
324 		return (-1);
325 	case PMCLOG_TYPE_DROPNOTIFY:
326 		/* nothing to do */
327 		break;
328 	case PMCLOG_TYPE_INITIALIZE:
329 		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version);
330 		PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch);
331 		PMCLOG_READ64(le,ev->pl_u.pl_i.pl_tsc_freq);
332 		memcpy(&ev->pl_u.pl_i.pl_ts, le, sizeof(struct timespec));
333 		le += sizeof(struct timespec)/4;
334 		PMCLOG_READSTRING(le, ev->pl_u.pl_i.pl_cpuid, PMC_CPUID_LEN);
335 		memcpy(ev->pl_u.pl_i.pl_cpuid, le, PMC_CPUID_LEN);
336 		ps->ps_cpuid = strdup(ev->pl_u.pl_i.pl_cpuid);
337 		ps->ps_version = ev->pl_u.pl_i.pl_version;
338 		ps->ps_arch = ev->pl_u.pl_i.pl_arch;
339 		ps->ps_initialized = 1;
340 		break;
341 	case PMCLOG_TYPE_MAP_IN:
342 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
343 		PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
344 		PMCLOG_SKIP32(le);
345 		PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
346 		PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
347 		break;
348 	case PMCLOG_TYPE_MAP_OUT:
349 		PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
350 		PMCLOG_SKIP32(le);
351 		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
352 		PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
353 		break;
354 	case PMCLOG_TYPE_PMCALLOCATE:
355 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
356 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
357 		PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
358 		PMCLOG_SKIP32(le);
359 		PMCLOG_READ64(le,ev->pl_u.pl_a.pl_rate);
360 
361 		/*
362 		 * Could be either a PMC event code or a PMU event index;
363 		 * assume that their encodings don't overlap (i.e. no PMU event
364 		 * table is more than 0x1000 entries) to distinguish them here.
365 		 * Otherwise pmc_pmu_event_get_by_idx will go out of bounds if
366 		 * given a PMC event code when it knows about that CPU.
367 		 *
368 		 * XXX: Ideally we'd have user flags to give us that context.
369 		 */
370 		if (ev->pl_u.pl_a.pl_event < PMC_EVENT_FIRST)
371 			ev->pl_u.pl_a.pl_evname =
372 			    pmc_pmu_event_get_by_idx(ps->ps_cpuid,
373 				ev->pl_u.pl_a.pl_event);
374 		else if (ev->pl_u.pl_a.pl_event <= PMC_EVENT_LAST)
375 			ev->pl_u.pl_a.pl_evname =
376 			    _pmc_name_of_event(ev->pl_u.pl_a.pl_event,
377 				ps->ps_arch);
378 		else
379 			ev->pl_u.pl_a.pl_evname = NULL;
380 		if (ev->pl_u.pl_a.pl_evname == NULL) {
381 			printf("unknown event\n");
382 			goto error;
383 		}
384 		break;
385 	case PMCLOG_TYPE_PMCALLOCATEDYN:
386 		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_pmcid);
387 		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_event);
388 		PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_flags);
389 		PMCLOG_SKIP32(le);
390 		PMCLOG_READSTRING(le,ev->pl_u.pl_ad.pl_evname,PMC_NAME_MAX);
391 		break;
392 	case PMCLOG_TYPE_PMCATTACH:
393 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
394 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
395 		PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
396 		PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
397 		break;
398 	case PMCLOG_TYPE_PMCDETACH:
399 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
400 		PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
401 		break;
402 	case PMCLOG_TYPE_PROCCSW:
403 		PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
404 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
405 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
406 		PMCLOG_READ32(le,ev->pl_u.pl_c.pl_tid);
407 		break;
408 	case PMCLOG_TYPE_PROCEXEC:
409 		PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
410 		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
411 		PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
412 		PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_baseaddr);
413 		PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_dynaddr);
414 		PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
415 		break;
416 	case PMCLOG_TYPE_PROCEXIT:
417 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
418 		PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
419 		PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
420 		break;
421 	case PMCLOG_TYPE_PROCFORK:
422 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
423 		PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
424 		break;
425 	case PMCLOG_TYPE_SYSEXIT:
426 		PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
427 		break;
428 	case PMCLOG_TYPE_USERDATA:
429 		PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
430 		break;
431 	case PMCLOG_TYPE_THR_CREATE:
432 		PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_tid);
433 		PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_pid);
434 		PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_flags);
435 		PMCLOG_SKIP32(le);
436 		memcpy(ev->pl_u.pl_tc.pl_tdname, le, MAXCOMLEN+1);
437 		break;
438 	case PMCLOG_TYPE_THR_EXIT:
439 		PMCLOG_READ32(le,ev->pl_u.pl_te.pl_tid);
440 		break;
441 	case PMCLOG_TYPE_PROC_CREATE:
442 		PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_pid);
443 		PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_flags);
444 		memcpy(ev->pl_u.pl_pc.pl_pcomm, le, MAXCOMLEN+1);
445 		break;
446 	default:	/* unknown record type */
447 		ps->ps_state = PL_STATE_ERROR;
448 		ev->pl_state = PMCLOG_ERROR;
449 		return (-1);
450 	}
451 
452 	ev->pl_offset = (ps->ps_offset += evlen);
453 	ev->pl_count  = (ps->ps_count += 1);
454 	ev->pl_len = evlen;
455 	ev->pl_state = PMCLOG_OK;
456 	return 0;
457 
458  error:
459 	ev->pl_state = PMCLOG_ERROR;
460 	ps->ps_state = PL_STATE_ERROR;
461 	return -1;
462 }
463 
464 /*
465  * Extract and return the next event from the byte stream.
466  *
467  * Returns 0 and sets the event's state to PMCLOG_OK in case an event
468  * was successfully parsed.  Otherwise this function returns -1 and
469  * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
470  * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
471  * a parse error was encountered.
472  */
473 
474 int
475 pmclog_read(void *cookie, struct pmclog_ev *ev)
476 {
477 	int retval;
478 	ssize_t nread;
479 	struct pmclog_parse_state *ps;
480 
481 	ps = (struct pmclog_parse_state *) cookie;
482 
483 	if (ps->ps_state == PL_STATE_ERROR) {
484 		ev->pl_state = PMCLOG_ERROR;
485 		return -1;
486 	}
487 
488 	/*
489 	 * If there isn't enough data left for a new event try and get
490 	 * more data.
491 	 */
492 	if (ps->ps_len == 0) {
493 		ev->pl_state = PMCLOG_REQUIRE_DATA;
494 
495 		/*
496 		 * If we have a valid file descriptor to read from, attempt
497 		 * to read from that.  This read may return with an error,
498 		 * (which may be EAGAIN or other recoverable error), or
499 		 * can return EOF.
500 		 */
501 		if (ps->ps_fd != PMCLOG_FD_NONE) {
502 		refill:
503 			nread = read(ps->ps_fd, ps->ps_buffer,
504 			    PMCLOG_BUFFER_SIZE);
505 
506 			if (nread <= 0) {
507 				if (nread == 0)
508 					ev->pl_state = PMCLOG_EOF;
509 				else if (errno != EAGAIN) /* not restartable */
510 					ev->pl_state = PMCLOG_ERROR;
511 				return -1;
512 			}
513 
514 			ps->ps_len = nread;
515 			ps->ps_data = ps->ps_buffer;
516 		} else {
517 			return -1;
518 		}
519 	}
520 
521 	assert(ps->ps_len > 0);
522 
523 
524 	 /* Retrieve one event from the byte stream. */
525 	retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
526 	/*
527 	 * If we need more data and we have a configured fd, try read
528 	 * from it.
529 	 */
530 	if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
531 	    ps->ps_fd != -1) {
532 		assert(ps->ps_len == 0);
533 		goto refill;
534 	}
535 
536 	return retval;
537 }
538 
539 /*
540  * Feed data to a memory based parser.
541  *
542  * The memory area pointed to by 'data' needs to be valid till the
543  * next error return from pmclog_next_event().
544  */
545 
546 int
547 pmclog_feed(void *cookie, char *data, int len)
548 {
549 	struct pmclog_parse_state *ps;
550 
551 	ps = (struct pmclog_parse_state *) cookie;
552 
553 	if (len < 0 ||		/* invalid length */
554 	    ps->ps_buffer ||	/* called for a file parser */
555 	    ps->ps_len != 0)	/* unnecessary call */
556 		return -1;
557 
558 	ps->ps_data = data;
559 	ps->ps_len  = len;
560 
561 	return 0;
562 }
563 
564 /*
565  * Allocate and initialize parser state.
566  */
567 
568 void *
569 pmclog_open(int fd)
570 {
571 	struct pmclog_parse_state *ps;
572 
573 	if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
574 		return NULL;
575 
576 	ps->ps_state = PL_STATE_NEW_RECORD;
577 	ps->ps_arch = -1;
578 	ps->ps_initialized = 0;
579 	ps->ps_count = 0;
580 	ps->ps_offset = (off_t) 0;
581 	bzero(&ps->ps_saved, sizeof(ps->ps_saved));
582 	ps->ps_cpuid = NULL;
583 	ps->ps_svcount = 0;
584 	ps->ps_fd    = fd;
585 	ps->ps_data  = NULL;
586 	ps->ps_buffer = NULL;
587 	ps->ps_len   = 0;
588 
589 	/* allocate space for a work area */
590 	if (ps->ps_fd != PMCLOG_FD_NONE) {
591 		if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL) {
592 			free(ps);
593 			return NULL;
594 		}
595 	}
596 
597 	return ps;
598 }
599 
600 
601 /*
602  * Free up parser state.
603  */
604 
605 void
606 pmclog_close(void *cookie)
607 {
608 	struct pmclog_parse_state *ps;
609 
610 	ps = (struct pmclog_parse_state *) cookie;
611 
612 	if (ps->ps_buffer)
613 		free(ps->ps_buffer);
614 
615 	free(ps);
616 }
617