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