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