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