2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2007 Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/pmclog.h>
51 #include <machine/pmc_mdep.h>
53 #include "libpmcinternal.h"
55 #define PMCLOG_BUFFER_SIZE 4096
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.
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.
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.
81 #define PMCLOG_HEADER_FROM_SAVED_STATE(PS) \
82 (* ((uint32_t *) &(PS)->ps_saved))
84 #define PMCLOG_INITIALIZE_READER(LE,A) LE = (uint32_t *) &(A)
85 #define PMCLOG_READ32(LE,V) do { \
88 #define PMCLOG_READ64(LE,V) do { \
90 _v = (uint64_t) *(LE)++; \
91 _v |= ((uint64_t) *(LE)++) << 32; \
95 #define PMCLOG_READSTRING(LE,DST,LEN) strlcpy((DST), (char *) (LE), (LEN))
98 * Assemble a log record from '*len' octets starting from address '*data'.
99 * Update 'data' and 'len' to reflect the number of bytes consumed.
101 * '*data' is potentially an unaligned address and '*len' octets may
102 * not be enough to complete a event record.
105 static enum pmclog_parser_state
106 pmclog_get_record(struct pmclog_parse_state *ps, char **data, ssize_t *len)
108 int avail, copylen, recordsize, used;
110 const int HEADERSIZE = sizeof(uint32_t);
113 if ((avail = *len) <= 0)
114 return (ps->ps_state = PL_STATE_ERROR);
119 if (ps->ps_state == PL_STATE_NEW_RECORD)
122 dst = (char *) &ps->ps_saved + ps->ps_svcount;
124 switch (ps->ps_state) {
125 case PL_STATE_NEW_RECORD:
130 * Case A: avail < headersize
131 * -> 'expecting header'
133 * Case B: avail >= headersize
134 * B.1: avail < recordsize
135 * -> 'partial record'
136 * B.2: avail >= recordsize
140 copylen = avail < HEADERSIZE ? avail : HEADERSIZE;
141 bcopy(src, dst, copylen);
142 ps->ps_svcount = used = copylen;
144 if (copylen < HEADERSIZE) {
145 ps->ps_state = PL_STATE_EXPECTING_HEADER;
152 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
153 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
158 if (recordsize <= avail) { /* full record available */
159 bcopy(src, dst, recordsize - copylen);
160 ps->ps_svcount = used = recordsize;
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;
171 case PL_STATE_EXPECTING_HEADER:
176 * Case C: avail+saved < headersize
177 * -> 'expecting header'
179 * Case D: avail+saved >= headersize
180 * D.1: avail+saved < recordsize
181 * -> 'partial record'
182 * D.2: avail+saved >= recordsize
184 * (see PARTIAL_RECORD handling below)
187 if (avail + ps->ps_svcount < HEADERSIZE) {
188 bcopy(src, dst, avail);
189 ps->ps_svcount += avail;
194 used = copylen = HEADERSIZE - ps->ps_svcount;
195 bcopy(src, dst, copylen);
199 ps->ps_svcount += copylen;
203 case PL_STATE_PARTIAL_RECORD:
208 * Case E: avail+saved < recordsize
209 * -> 'partial record'
211 * Case F: avail+saved >= recordsize
215 h = PMCLOG_HEADER_FROM_SAVED_STATE(ps);
216 recordsize = PMCLOG_HEADER_TO_LENGTH(h);
221 if (avail + ps->ps_svcount < recordsize) {
223 ps->ps_state = PL_STATE_PARTIAL_RECORD;
225 copylen = recordsize - ps->ps_svcount;
226 ps->ps_state = PL_STATE_NEW_RECORD;
229 bcopy(src, dst, copylen);
230 ps->ps_svcount += copylen;
244 ps->ps_state = PL_STATE_ERROR;
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
256 pmclog_get_event(void *cookie, char **data, ssize_t *len,
257 struct pmclog_ev *ev)
260 uint32_t h, *le, npc, noop;
261 enum pmclog_parser_state e;
262 struct pmclog_parse_state *ps;
264 ps = (struct pmclog_parse_state *) cookie;
266 assert(ps->ps_state != PL_STATE_ERROR);
268 if ((e = pmclog_get_record(ps,data,len)) == PL_STATE_ERROR) {
269 ev->pl_state = PMCLOG_ERROR;
270 printf("state error\n");
274 if (e != PL_STATE_NEW_RECORD) {
275 ev->pl_state = PMCLOG_REQUIRE_DATA;
279 PMCLOG_INITIALIZE_READER(le, ps->ps_saved);
283 if (!PMCLOG_HEADER_CHECK_MAGIC(h)) {
284 printf("bad magic\n");
285 ps->ps_state = PL_STATE_ERROR;
286 ev->pl_state = PMCLOG_ERROR;
290 /* copy out the time stamp */
291 PMCLOG_READ32(le,ev->pl_ts.tv_sec);
292 PMCLOG_READ32(le,ev->pl_ts.tv_nsec);
294 evlen = PMCLOG_HEADER_TO_LENGTH(h);
296 #define PMCLOG_GET_PATHLEN(P,E,TYPE) do { \
297 (P) = (E) - offsetof(struct TYPE, pl_pathname); \
298 if ((P) > PATH_MAX || (P) < 0) \
302 #define PMCLOG_GET_CALLCHAIN_SIZE(SZ,E) do { \
303 (SZ) = ((E) - offsetof(struct pmclog_callchain, pl_pc)) \
304 / sizeof(uintfptr_t); \
307 switch (ev->pl_type = PMCLOG_HEADER_TO_TYPE(h)) {
308 case PMCLOG_TYPE_CALLCHAIN:
309 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pid);
310 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_tid);
311 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_pmcid);
312 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags);
313 PMCLOG_READ32(le,ev->pl_u.pl_cc.pl_cpuflags2);
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;
320 case PMCLOG_TYPE_CLOSELOG:
321 ev->pl_state = PMCLOG_EOF;
323 case PMCLOG_TYPE_DROPNOTIFY:
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_READSTRING(le, ev->pl_u.pl_i.pl_cpuid, PMC_CPUID_LEN);
330 memcpy(ev->pl_u.pl_i.pl_cpuid, le, PMC_CPUID_LEN);
331 ps->ps_cpuid = strdup(ev->pl_u.pl_i.pl_cpuid);
332 ps->ps_version = ev->pl_u.pl_i.pl_version;
333 ps->ps_arch = ev->pl_u.pl_i.pl_arch;
334 ps->ps_initialized = 1;
336 case PMCLOG_TYPE_MAP_IN:
337 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in);
338 PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid);
339 PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start);
340 PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen);
342 case PMCLOG_TYPE_MAP_OUT:
343 PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid);
344 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start);
345 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end);
347 case PMCLOG_TYPE_PMCALLOCATE:
348 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid);
349 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event);
350 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags);
351 PMCLOG_READ64(le,ev->pl_u.pl_a.pl_rate);
352 ev->pl_u.pl_a.pl_evname = pmc_pmu_event_get_by_idx(ps->ps_cpuid, ev->pl_u.pl_a.pl_event);
353 if (ev->pl_u.pl_a.pl_evname != NULL)
355 else if ((ev->pl_u.pl_a.pl_evname =
356 _pmc_name_of_event(ev->pl_u.pl_a.pl_event, ps->ps_arch))
358 printf("unknown event\n");
362 case PMCLOG_TYPE_PMCALLOCATEDYN:
363 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_pmcid);
364 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_event);
365 PMCLOG_READ32(le,ev->pl_u.pl_ad.pl_flags);
366 PMCLOG_READSTRING(le,ev->pl_u.pl_ad.pl_evname,PMC_NAME_MAX);
368 case PMCLOG_TYPE_PMCATTACH:
369 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach);
370 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid);
371 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid);
372 PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen);
374 case PMCLOG_TYPE_PMCDETACH:
375 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid);
376 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid);
378 case PMCLOG_TYPE_PROCCSW:
379 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid);
380 PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value);
381 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid);
382 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_tid);
384 case PMCLOG_TYPE_PROCEXEC:
385 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec);
386 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid);
387 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid);
388 PMCLOG_READ32(le,noop);
389 PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr);
390 PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen);
392 case PMCLOG_TYPE_PROCEXIT:
393 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid);
394 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid);
395 PMCLOG_READ32(le,noop);
396 PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value);
398 case PMCLOG_TYPE_PROCFORK:
399 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
400 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
402 case PMCLOG_TYPE_SYSEXIT:
403 PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
405 case PMCLOG_TYPE_USERDATA:
406 PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata);
408 case PMCLOG_TYPE_THR_CREATE:
409 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_tid);
410 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_pid);
411 PMCLOG_READ32(le,ev->pl_u.pl_tc.pl_flags);
412 memcpy(ev->pl_u.pl_tc.pl_tdname, le, MAXCOMLEN+1);
414 case PMCLOG_TYPE_THR_EXIT:
415 PMCLOG_READ32(le,ev->pl_u.pl_te.pl_tid);
417 case PMCLOG_TYPE_PROC_CREATE:
418 PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_pid);
419 PMCLOG_READ32(le,ev->pl_u.pl_pc.pl_flags);
420 PMCLOG_READ32(le,noop);
421 memcpy(ev->pl_u.pl_pc.pl_pcomm, le, MAXCOMLEN+1);
423 default: /* unknown record type */
424 ps->ps_state = PL_STATE_ERROR;
425 ev->pl_state = PMCLOG_ERROR;
429 ev->pl_offset = (ps->ps_offset += evlen);
430 ev->pl_count = (ps->ps_count += 1);
432 ev->pl_state = PMCLOG_OK;
436 ev->pl_state = PMCLOG_ERROR;
437 ps->ps_state = PL_STATE_ERROR;
442 * Extract and return the next event from the byte stream.
444 * Returns 0 and sets the event's state to PMCLOG_OK in case an event
445 * was successfully parsed. Otherwise this function returns -1 and
446 * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data
447 * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if
448 * a parse error was encountered.
452 pmclog_read(void *cookie, struct pmclog_ev *ev)
456 struct pmclog_parse_state *ps;
458 ps = (struct pmclog_parse_state *) cookie;
460 if (ps->ps_state == PL_STATE_ERROR) {
461 ev->pl_state = PMCLOG_ERROR;
466 * If there isn't enough data left for a new event try and get
469 if (ps->ps_len == 0) {
470 ev->pl_state = PMCLOG_REQUIRE_DATA;
473 * If we have a valid file descriptor to read from, attempt
474 * to read from that. This read may return with an error,
475 * (which may be EAGAIN or other recoverable error), or
478 if (ps->ps_fd != PMCLOG_FD_NONE) {
480 nread = read(ps->ps_fd, ps->ps_buffer,
485 ev->pl_state = PMCLOG_EOF;
486 else if (errno != EAGAIN) /* not restartable */
487 ev->pl_state = PMCLOG_ERROR;
492 ps->ps_data = ps->ps_buffer;
498 assert(ps->ps_len > 0);
501 /* Retrieve one event from the byte stream. */
502 retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev);
504 * If we need more data and we have a configured fd, try read
507 if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA &&
509 assert(ps->ps_len == 0);
517 * Feed data to a memory based parser.
519 * The memory area pointed to by 'data' needs to be valid till the
520 * next error return from pmclog_next_event().
524 pmclog_feed(void *cookie, char *data, int len)
526 struct pmclog_parse_state *ps;
528 ps = (struct pmclog_parse_state *) cookie;
530 if (len < 0 || /* invalid length */
531 ps->ps_buffer || /* called for a file parser */
532 ps->ps_len != 0) /* unnecessary call */
542 * Allocate and initialize parser state.
548 struct pmclog_parse_state *ps;
550 if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL)
553 ps->ps_state = PL_STATE_NEW_RECORD;
555 ps->ps_initialized = 0;
557 ps->ps_offset = (off_t) 0;
558 bzero(&ps->ps_saved, sizeof(ps->ps_saved));
563 ps->ps_buffer = NULL;
566 /* allocate space for a work area */
567 if (ps->ps_fd != PMCLOG_FD_NONE) {
568 if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL) {
579 * Free up parser state.
583 pmclog_close(void *cookie)
585 struct pmclog_parse_state *ps;
587 ps = (struct pmclog_parse_state *) cookie;