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