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