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 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 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 case PMCLOG_TYPE_DROPNOTIFY: 337 /* nothing to do */ 338 break; 339 case PMCLOG_TYPE_INITIALIZE: 340 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_version); 341 PMCLOG_READ32(le,ev->pl_u.pl_i.pl_arch); 342 ps->ps_version = ev->pl_u.pl_i.pl_version; 343 ps->ps_arch = ev->pl_u.pl_i.pl_arch; 344 ps->ps_initialized = 1; 345 break; 346 case PMCLOG_TYPE_MAP_IN: 347 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_map_in); 348 PMCLOG_READ32(le,ev->pl_u.pl_mi.pl_pid); 349 PMCLOG_READADDR(le,ev->pl_u.pl_mi.pl_start); 350 PMCLOG_READSTRING(le, ev->pl_u.pl_mi.pl_pathname, pathlen); 351 break; 352 case PMCLOG_TYPE_MAP_OUT: 353 PMCLOG_READ32(le,ev->pl_u.pl_mo.pl_pid); 354 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_start); 355 PMCLOG_READADDR(le,ev->pl_u.pl_mo.pl_end); 356 break; 357 case PMCLOG_TYPE_PCSAMPLE: 358 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pid); 359 PMCLOG_READADDR(le,ev->pl_u.pl_s.pl_pc); 360 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_pmcid); 361 PMCLOG_READ32(le,ev->pl_u.pl_s.pl_usermode); 362 break; 363 case PMCLOG_TYPE_PMCALLOCATE: 364 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_pmcid); 365 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_event); 366 PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags); 367 if ((ev->pl_u.pl_a.pl_evname = 368 _pmc_name_of_event(ev->pl_u.pl_a.pl_event, ps->ps_arch)) 369 == NULL) 370 goto error; 371 break; 372 case PMCLOG_TYPE_PMCATTACH: 373 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_pmcattach); 374 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pmcid); 375 PMCLOG_READ32(le,ev->pl_u.pl_t.pl_pid); 376 PMCLOG_READSTRING(le,ev->pl_u.pl_t.pl_pathname,pathlen); 377 break; 378 case PMCLOG_TYPE_PMCDETACH: 379 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pmcid); 380 PMCLOG_READ32(le,ev->pl_u.pl_d.pl_pid); 381 break; 382 case PMCLOG_TYPE_PROCCSW: 383 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pmcid); 384 PMCLOG_READ64(le,ev->pl_u.pl_c.pl_value); 385 PMCLOG_READ32(le,ev->pl_u.pl_c.pl_pid); 386 break; 387 case PMCLOG_TYPE_PROCEXEC: 388 PMCLOG_GET_PATHLEN(pathlen,evlen,pmclog_procexec); 389 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pid); 390 PMCLOG_READADDR(le,ev->pl_u.pl_x.pl_entryaddr); 391 PMCLOG_READ32(le,ev->pl_u.pl_x.pl_pmcid); 392 PMCLOG_READSTRING(le,ev->pl_u.pl_x.pl_pathname,pathlen); 393 break; 394 case PMCLOG_TYPE_PROCEXIT: 395 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pmcid); 396 PMCLOG_READ64(le,ev->pl_u.pl_e.pl_value); 397 PMCLOG_READ32(le,ev->pl_u.pl_e.pl_pid); 398 break; 399 case PMCLOG_TYPE_PROCFORK: 400 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid); 401 PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid); 402 break; 403 case PMCLOG_TYPE_SYSEXIT: 404 PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid); 405 break; 406 case PMCLOG_TYPE_USERDATA: 407 PMCLOG_READ32(le,ev->pl_u.pl_u.pl_userdata); 408 break; 409 default: /* unknown record type */ 410 ps->ps_state = PL_STATE_ERROR; 411 ev->pl_state = PMCLOG_ERROR; 412 return (-1); 413 } 414 415 ev->pl_offset = (ps->ps_offset += evlen); 416 ev->pl_count = (ps->ps_count += 1); 417 ev->pl_state = PMCLOG_OK; 418 return 0; 419 420 error: 421 ev->pl_state = PMCLOG_ERROR; 422 ps->ps_state = PL_STATE_ERROR; 423 return -1; 424 } 425 426 /* 427 * Extract and return the next event from the byte stream. 428 * 429 * Returns 0 and sets the event's state to PMCLOG_OK in case an event 430 * was successfully parsed. Otherwise this function returns -1 and 431 * sets the event's state to one of PMCLOG_REQUIRE_DATA (if more data 432 * is needed) or PMCLOG_EOF (if an EOF was seen) or PMCLOG_ERROR if 433 * a parse error was encountered. 434 */ 435 436 int 437 pmclog_read(void *cookie, struct pmclog_ev *ev) 438 { 439 int retval; 440 ssize_t nread; 441 struct pmclog_parse_state *ps; 442 443 ps = (struct pmclog_parse_state *) cookie; 444 445 if (ps->ps_state == PL_STATE_ERROR) { 446 ev->pl_state = PMCLOG_ERROR; 447 return -1; 448 } 449 450 /* 451 * If there isn't enough data left for a new event try and get 452 * more data. 453 */ 454 if (ps->ps_len == 0) { 455 ev->pl_state = PMCLOG_REQUIRE_DATA; 456 457 /* 458 * If we have a valid file descriptor to read from, attempt 459 * to read from that. This read may return with an error, 460 * (which may be EAGAIN or other recoverable error), or 461 * can return EOF. 462 */ 463 if (ps->ps_fd != PMCLOG_FD_NONE) { 464 refill: 465 nread = read(ps->ps_fd, ps->ps_buffer, 466 PMCLOG_BUFFER_SIZE); 467 468 if (nread <= 0) { 469 if (nread == 0) 470 ev->pl_state = PMCLOG_EOF; 471 else if (errno != EAGAIN) /* not restartable */ 472 ev->pl_state = PMCLOG_ERROR; 473 return -1; 474 } 475 476 ps->ps_len = nread; 477 ps->ps_data = ps->ps_buffer; 478 } else 479 return -1; 480 } 481 482 assert(ps->ps_len > 0); 483 484 485 /* Retrieve one event from the byte stream. */ 486 retval = pmclog_get_event(ps, &ps->ps_data, &ps->ps_len, ev); 487 488 /* 489 * If we need more data and we have a configured fd, try read 490 * from it. 491 */ 492 if (retval < 0 && ev->pl_state == PMCLOG_REQUIRE_DATA && 493 ps->ps_fd != -1) { 494 assert(ps->ps_len == 0); 495 goto refill; 496 } 497 498 return retval; 499 } 500 501 /* 502 * Feed data to a memory based parser. 503 * 504 * The memory area pointed to by 'data' needs to be valid till the 505 * next error return from pmclog_next_event(). 506 */ 507 508 int 509 pmclog_feed(void *cookie, char *data, int len) 510 { 511 struct pmclog_parse_state *ps; 512 513 ps = (struct pmclog_parse_state *) cookie; 514 515 if (len < 0 || /* invalid length */ 516 ps->ps_buffer || /* called for a file parser */ 517 ps->ps_len != 0) /* unnecessary call */ 518 return -1; 519 520 ps->ps_data = data; 521 ps->ps_len = len; 522 523 return 0; 524 } 525 526 /* 527 * Allocate and initialize parser state. 528 */ 529 530 void * 531 pmclog_open(int fd) 532 { 533 struct pmclog_parse_state *ps; 534 535 if ((ps = (struct pmclog_parse_state *) malloc(sizeof(*ps))) == NULL) 536 return NULL; 537 538 ps->ps_state = PL_STATE_NEW_RECORD; 539 ps->ps_arch = -1; 540 ps->ps_initialized = 0; 541 ps->ps_count = 0; 542 ps->ps_offset = (off_t) 0; 543 bzero(&ps->ps_saved, sizeof(ps->ps_saved)); 544 ps->ps_svcount = 0; 545 ps->ps_fd = fd; 546 ps->ps_data = NULL; 547 ps->ps_buffer = NULL; 548 ps->ps_len = 0; 549 550 /* allocate space for a work area */ 551 if (ps->ps_fd != PMCLOG_FD_NONE) { 552 if ((ps->ps_buffer = malloc(PMCLOG_BUFFER_SIZE)) == NULL) 553 return NULL; 554 } 555 556 return ps; 557 } 558 559 560 /* 561 * Free up parser state. 562 */ 563 564 void 565 pmclog_close(void *cookie) 566 { 567 struct pmclog_parse_state *ps; 568 569 ps = (struct pmclog_parse_state *) cookie; 570 571 if (ps->ps_buffer) 572 free(ps->ps_buffer); 573 574 free(ps); 575 } 576