1 /* 2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com> 3 * 4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 of the License (not later!) 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * 19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 * 21 * The parts for function graph printing was taken and modified from the 22 * Linux Kernel that were written by Frederic Weisbecker. 23 */ 24 #define _GNU_SOURCE 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <ctype.h> 29 #include <errno.h> 30 31 #undef _GNU_SOURCE 32 #include "../perf.h" 33 #include "util.h" 34 #include "trace-event.h" 35 36 int header_page_ts_offset; 37 int header_page_ts_size; 38 int header_page_size_offset; 39 int header_page_size_size; 40 int header_page_data_offset; 41 int header_page_data_size; 42 43 int latency_format; 44 45 static char *input_buf; 46 static unsigned long long input_buf_ptr; 47 static unsigned long long input_buf_siz; 48 49 static int cpus; 50 static int long_size; 51 52 static void init_input_buf(char *buf, unsigned long long size) 53 { 54 input_buf = buf; 55 input_buf_siz = size; 56 input_buf_ptr = 0; 57 } 58 59 struct cmdline { 60 char *comm; 61 int pid; 62 }; 63 64 static struct cmdline *cmdlines; 65 static int cmdline_count; 66 67 static int cmdline_cmp(const void *a, const void *b) 68 { 69 const struct cmdline *ca = a; 70 const struct cmdline *cb = b; 71 72 if (ca->pid < cb->pid) 73 return -1; 74 if (ca->pid > cb->pid) 75 return 1; 76 77 return 0; 78 } 79 80 void parse_cmdlines(char *file, int size __unused) 81 { 82 struct cmdline_list { 83 struct cmdline_list *next; 84 char *comm; 85 int pid; 86 } *list = NULL, *item; 87 char *line; 88 char *next = NULL; 89 int i; 90 91 line = strtok_r(file, "\n", &next); 92 while (line) { 93 item = malloc_or_die(sizeof(*item)); 94 sscanf(line, "%d %as", &item->pid, 95 (float *)(void *)&item->comm); /* workaround gcc warning */ 96 item->next = list; 97 list = item; 98 line = strtok_r(NULL, "\n", &next); 99 cmdline_count++; 100 } 101 102 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count); 103 104 i = 0; 105 while (list) { 106 cmdlines[i].pid = list->pid; 107 cmdlines[i].comm = list->comm; 108 i++; 109 item = list; 110 list = list->next; 111 free(item); 112 } 113 114 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp); 115 } 116 117 static struct func_map { 118 unsigned long long addr; 119 char *func; 120 char *mod; 121 } *func_list; 122 static unsigned int func_count; 123 124 static int func_cmp(const void *a, const void *b) 125 { 126 const struct func_map *fa = a; 127 const struct func_map *fb = b; 128 129 if (fa->addr < fb->addr) 130 return -1; 131 if (fa->addr > fb->addr) 132 return 1; 133 134 return 0; 135 } 136 137 void parse_proc_kallsyms(char *file, unsigned int size __unused) 138 { 139 struct func_list { 140 struct func_list *next; 141 unsigned long long addr; 142 char *func; 143 char *mod; 144 } *list = NULL, *item; 145 char *line; 146 char *next = NULL; 147 char *addr_str; 148 char ch; 149 int ret; 150 int i; 151 152 line = strtok_r(file, "\n", &next); 153 while (line) { 154 item = malloc_or_die(sizeof(*item)); 155 item->mod = NULL; 156 ret = sscanf(line, "%as %c %as\t[%as", 157 (float *)(void *)&addr_str, /* workaround gcc warning */ 158 &ch, 159 (float *)(void *)&item->func, 160 (float *)(void *)&item->mod); 161 item->addr = strtoull(addr_str, NULL, 16); 162 free(addr_str); 163 164 /* truncate the extra ']' */ 165 if (item->mod) 166 item->mod[strlen(item->mod) - 1] = 0; 167 168 169 item->next = list; 170 list = item; 171 line = strtok_r(NULL, "\n", &next); 172 func_count++; 173 } 174 175 func_list = malloc_or_die(sizeof(*func_list) * func_count + 1); 176 177 i = 0; 178 while (list) { 179 func_list[i].func = list->func; 180 func_list[i].addr = list->addr; 181 func_list[i].mod = list->mod; 182 i++; 183 item = list; 184 list = list->next; 185 free(item); 186 } 187 188 qsort(func_list, func_count, sizeof(*func_list), func_cmp); 189 190 /* 191 * Add a special record at the end. 192 */ 193 func_list[func_count].func = NULL; 194 func_list[func_count].addr = 0; 195 func_list[func_count].mod = NULL; 196 } 197 198 /* 199 * We are searching for a record in between, not an exact 200 * match. 201 */ 202 static int func_bcmp(const void *a, const void *b) 203 { 204 const struct func_map *fa = a; 205 const struct func_map *fb = b; 206 207 if ((fa->addr == fb->addr) || 208 209 (fa->addr > fb->addr && 210 fa->addr < (fb+1)->addr)) 211 return 0; 212 213 if (fa->addr < fb->addr) 214 return -1; 215 216 return 1; 217 } 218 219 static struct func_map *find_func(unsigned long long addr) 220 { 221 struct func_map *func; 222 struct func_map key; 223 224 key.addr = addr; 225 226 func = bsearch(&key, func_list, func_count, sizeof(*func_list), 227 func_bcmp); 228 229 return func; 230 } 231 232 void print_funcs(void) 233 { 234 int i; 235 236 for (i = 0; i < (int)func_count; i++) { 237 printf("%016llx %s", 238 func_list[i].addr, 239 func_list[i].func); 240 if (func_list[i].mod) 241 printf(" [%s]\n", func_list[i].mod); 242 else 243 printf("\n"); 244 } 245 } 246 247 static struct printk_map { 248 unsigned long long addr; 249 char *printk; 250 } *printk_list; 251 static unsigned int printk_count; 252 253 static int printk_cmp(const void *a, const void *b) 254 { 255 const struct func_map *fa = a; 256 const struct func_map *fb = b; 257 258 if (fa->addr < fb->addr) 259 return -1; 260 if (fa->addr > fb->addr) 261 return 1; 262 263 return 0; 264 } 265 266 static struct printk_map *find_printk(unsigned long long addr) 267 { 268 struct printk_map *printk; 269 struct printk_map key; 270 271 key.addr = addr; 272 273 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list), 274 printk_cmp); 275 276 return printk; 277 } 278 279 void parse_ftrace_printk(char *file, unsigned int size __unused) 280 { 281 struct printk_list { 282 struct printk_list *next; 283 unsigned long long addr; 284 char *printk; 285 } *list = NULL, *item; 286 char *line; 287 char *next = NULL; 288 char *addr_str; 289 char *fmt; 290 int i; 291 292 line = strtok_r(file, "\n", &next); 293 while (line) { 294 item = malloc_or_die(sizeof(*item)); 295 addr_str = strtok_r(line, ":", &fmt); 296 item->addr = strtoull(addr_str, NULL, 16); 297 /* fmt still has a space, skip it */ 298 item->printk = strdup(fmt+1); 299 item->next = list; 300 list = item; 301 line = strtok_r(NULL, "\n", &next); 302 printk_count++; 303 } 304 305 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1); 306 307 i = 0; 308 while (list) { 309 printk_list[i].printk = list->printk; 310 printk_list[i].addr = list->addr; 311 i++; 312 item = list; 313 list = list->next; 314 free(item); 315 } 316 317 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp); 318 } 319 320 void print_printk(void) 321 { 322 int i; 323 324 for (i = 0; i < (int)printk_count; i++) { 325 printf("%016llx %s\n", 326 printk_list[i].addr, 327 printk_list[i].printk); 328 } 329 } 330 331 static struct event *alloc_event(void) 332 { 333 struct event *event; 334 335 event = malloc_or_die(sizeof(*event)); 336 memset(event, 0, sizeof(*event)); 337 338 return event; 339 } 340 341 enum event_type { 342 EVENT_ERROR, 343 EVENT_NONE, 344 EVENT_SPACE, 345 EVENT_NEWLINE, 346 EVENT_OP, 347 EVENT_DELIM, 348 EVENT_ITEM, 349 EVENT_DQUOTE, 350 EVENT_SQUOTE, 351 }; 352 353 static struct event *event_list; 354 355 static void add_event(struct event *event) 356 { 357 event->next = event_list; 358 event_list = event; 359 } 360 361 static int event_item_type(enum event_type type) 362 { 363 switch (type) { 364 case EVENT_ITEM ... EVENT_SQUOTE: 365 return 1; 366 case EVENT_ERROR ... EVENT_DELIM: 367 default: 368 return 0; 369 } 370 } 371 372 static void free_arg(struct print_arg *arg) 373 { 374 if (!arg) 375 return; 376 377 switch (arg->type) { 378 case PRINT_ATOM: 379 if (arg->atom.atom) 380 free(arg->atom.atom); 381 break; 382 case PRINT_NULL: 383 case PRINT_FIELD ... PRINT_OP: 384 default: 385 /* todo */ 386 break; 387 } 388 389 free(arg); 390 } 391 392 static enum event_type get_type(int ch) 393 { 394 if (ch == '\n') 395 return EVENT_NEWLINE; 396 if (isspace(ch)) 397 return EVENT_SPACE; 398 if (isalnum(ch) || ch == '_') 399 return EVENT_ITEM; 400 if (ch == '\'') 401 return EVENT_SQUOTE; 402 if (ch == '"') 403 return EVENT_DQUOTE; 404 if (!isprint(ch)) 405 return EVENT_NONE; 406 if (ch == '(' || ch == ')' || ch == ',') 407 return EVENT_DELIM; 408 409 return EVENT_OP; 410 } 411 412 static int __read_char(void) 413 { 414 if (input_buf_ptr >= input_buf_siz) 415 return -1; 416 417 return input_buf[input_buf_ptr++]; 418 } 419 420 static int __peek_char(void) 421 { 422 if (input_buf_ptr >= input_buf_siz) 423 return -1; 424 425 return input_buf[input_buf_ptr]; 426 } 427 428 static enum event_type __read_token(char **tok) 429 { 430 char buf[BUFSIZ]; 431 int ch, last_ch, quote_ch, next_ch; 432 int i = 0; 433 int tok_size = 0; 434 enum event_type type; 435 436 *tok = NULL; 437 438 439 ch = __read_char(); 440 if (ch < 0) 441 return EVENT_NONE; 442 443 type = get_type(ch); 444 if (type == EVENT_NONE) 445 return type; 446 447 buf[i++] = ch; 448 449 switch (type) { 450 case EVENT_NEWLINE: 451 case EVENT_DELIM: 452 *tok = malloc_or_die(2); 453 (*tok)[0] = ch; 454 (*tok)[1] = 0; 455 return type; 456 457 case EVENT_OP: 458 switch (ch) { 459 case '-': 460 next_ch = __peek_char(); 461 if (next_ch == '>') { 462 buf[i++] = __read_char(); 463 break; 464 } 465 /* fall through */ 466 case '+': 467 case '|': 468 case '&': 469 case '>': 470 case '<': 471 last_ch = ch; 472 ch = __peek_char(); 473 if (ch != last_ch) 474 goto test_equal; 475 buf[i++] = __read_char(); 476 switch (last_ch) { 477 case '>': 478 case '<': 479 goto test_equal; 480 default: 481 break; 482 } 483 break; 484 case '!': 485 case '=': 486 goto test_equal; 487 default: /* what should we do instead? */ 488 break; 489 } 490 buf[i] = 0; 491 *tok = strdup(buf); 492 return type; 493 494 test_equal: 495 ch = __peek_char(); 496 if (ch == '=') 497 buf[i++] = __read_char(); 498 break; 499 500 case EVENT_DQUOTE: 501 case EVENT_SQUOTE: 502 /* don't keep quotes */ 503 i--; 504 quote_ch = ch; 505 last_ch = 0; 506 do { 507 if (i == (BUFSIZ - 1)) { 508 buf[i] = 0; 509 if (*tok) { 510 *tok = realloc(*tok, tok_size + BUFSIZ); 511 if (!*tok) 512 return EVENT_NONE; 513 strcat(*tok, buf); 514 } else 515 *tok = strdup(buf); 516 517 if (!*tok) 518 return EVENT_NONE; 519 tok_size += BUFSIZ; 520 i = 0; 521 } 522 last_ch = ch; 523 ch = __read_char(); 524 buf[i++] = ch; 525 /* the '\' '\' will cancel itself */ 526 if (ch == '\\' && last_ch == '\\') 527 last_ch = 0; 528 } while (ch != quote_ch || last_ch == '\\'); 529 /* remove the last quote */ 530 i--; 531 goto out; 532 533 case EVENT_ERROR ... EVENT_SPACE: 534 case EVENT_ITEM: 535 default: 536 break; 537 } 538 539 while (get_type(__peek_char()) == type) { 540 if (i == (BUFSIZ - 1)) { 541 buf[i] = 0; 542 if (*tok) { 543 *tok = realloc(*tok, tok_size + BUFSIZ); 544 if (!*tok) 545 return EVENT_NONE; 546 strcat(*tok, buf); 547 } else 548 *tok = strdup(buf); 549 550 if (!*tok) 551 return EVENT_NONE; 552 tok_size += BUFSIZ; 553 i = 0; 554 } 555 ch = __read_char(); 556 buf[i++] = ch; 557 } 558 559 out: 560 buf[i] = 0; 561 if (*tok) { 562 *tok = realloc(*tok, tok_size + i); 563 if (!*tok) 564 return EVENT_NONE; 565 strcat(*tok, buf); 566 } else 567 *tok = strdup(buf); 568 if (!*tok) 569 return EVENT_NONE; 570 571 return type; 572 } 573 574 static void free_token(char *tok) 575 { 576 if (tok) 577 free(tok); 578 } 579 580 static enum event_type read_token(char **tok) 581 { 582 enum event_type type; 583 584 for (;;) { 585 type = __read_token(tok); 586 if (type != EVENT_SPACE) 587 return type; 588 589 free_token(*tok); 590 } 591 592 /* not reached */ 593 return EVENT_NONE; 594 } 595 596 /* no newline */ 597 static enum event_type read_token_item(char **tok) 598 { 599 enum event_type type; 600 601 for (;;) { 602 type = __read_token(tok); 603 if (type != EVENT_SPACE && type != EVENT_NEWLINE) 604 return type; 605 606 free_token(*tok); 607 } 608 609 /* not reached */ 610 return EVENT_NONE; 611 } 612 613 static int test_type(enum event_type type, enum event_type expect) 614 { 615 if (type != expect) { 616 warning("Error: expected type %d but read %d", 617 expect, type); 618 return -1; 619 } 620 return 0; 621 } 622 623 static int test_type_token(enum event_type type, char *token, 624 enum event_type expect, const char *expect_tok) 625 { 626 if (type != expect) { 627 warning("Error: expected type %d but read %d", 628 expect, type); 629 return -1; 630 } 631 632 if (strcmp(token, expect_tok) != 0) { 633 warning("Error: expected '%s' but read '%s'", 634 expect_tok, token); 635 return -1; 636 } 637 return 0; 638 } 639 640 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok) 641 { 642 enum event_type type; 643 644 if (newline_ok) 645 type = read_token(tok); 646 else 647 type = read_token_item(tok); 648 return test_type(type, expect); 649 } 650 651 static int read_expect_type(enum event_type expect, char **tok) 652 { 653 return __read_expect_type(expect, tok, 1); 654 } 655 656 static int __read_expected(enum event_type expect, const char *str, int newline_ok) 657 { 658 enum event_type type; 659 char *token; 660 int ret; 661 662 if (newline_ok) 663 type = read_token(&token); 664 else 665 type = read_token_item(&token); 666 667 ret = test_type_token(type, token, expect, str); 668 669 free_token(token); 670 671 return ret; 672 } 673 674 static int read_expected(enum event_type expect, const char *str) 675 { 676 return __read_expected(expect, str, 1); 677 } 678 679 static int read_expected_item(enum event_type expect, const char *str) 680 { 681 return __read_expected(expect, str, 0); 682 } 683 684 static char *event_read_name(void) 685 { 686 char *token; 687 688 if (read_expected(EVENT_ITEM, (char *)"name") < 0) 689 return NULL; 690 691 if (read_expected(EVENT_OP, (char *)":") < 0) 692 return NULL; 693 694 if (read_expect_type(EVENT_ITEM, &token) < 0) 695 goto fail; 696 697 return token; 698 699 fail: 700 free_token(token); 701 return NULL; 702 } 703 704 static int event_read_id(void) 705 { 706 char *token; 707 int id; 708 709 if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0) 710 return -1; 711 712 if (read_expected(EVENT_OP, (char *)":") < 0) 713 return -1; 714 715 if (read_expect_type(EVENT_ITEM, &token) < 0) 716 goto fail; 717 718 id = strtoul(token, NULL, 0); 719 free_token(token); 720 return id; 721 722 fail: 723 free_token(token); 724 return -1; 725 } 726 727 static int field_is_string(struct format_field *field) 728 { 729 if ((field->flags & FIELD_IS_ARRAY) && 730 (!strstr(field->type, "char") || !strstr(field->type, "u8") || 731 !strstr(field->type, "s8"))) 732 return 1; 733 734 return 0; 735 } 736 737 static int field_is_dynamic(struct format_field *field) 738 { 739 if (!strcmp(field->type, "__data_loc")) 740 return 1; 741 742 return 0; 743 } 744 745 static int event_read_fields(struct event *event, struct format_field **fields) 746 { 747 struct format_field *field = NULL; 748 enum event_type type; 749 char *token; 750 char *last_token; 751 int count = 0; 752 753 do { 754 type = read_token(&token); 755 if (type == EVENT_NEWLINE) { 756 free_token(token); 757 return count; 758 } 759 760 count++; 761 762 if (test_type_token(type, token, EVENT_ITEM, (char *)"field")) 763 goto fail; 764 free_token(token); 765 766 type = read_token(&token); 767 /* 768 * The ftrace fields may still use the "special" name. 769 * Just ignore it. 770 */ 771 if (event->flags & EVENT_FL_ISFTRACE && 772 type == EVENT_ITEM && strcmp(token, "special") == 0) { 773 free_token(token); 774 type = read_token(&token); 775 } 776 777 if (test_type_token(type, token, EVENT_OP, (char *)":") < 0) 778 return -1; 779 780 if (read_expect_type(EVENT_ITEM, &token) < 0) 781 goto fail; 782 783 last_token = token; 784 785 field = malloc_or_die(sizeof(*field)); 786 memset(field, 0, sizeof(*field)); 787 788 /* read the rest of the type */ 789 for (;;) { 790 type = read_token(&token); 791 if (type == EVENT_ITEM || 792 (type == EVENT_OP && strcmp(token, "*") == 0) || 793 /* 794 * Some of the ftrace fields are broken and have 795 * an illegal "." in them. 796 */ 797 (event->flags & EVENT_FL_ISFTRACE && 798 type == EVENT_OP && strcmp(token, ".") == 0)) { 799 800 if (strcmp(token, "*") == 0) 801 field->flags |= FIELD_IS_POINTER; 802 803 if (field->type) { 804 field->type = realloc(field->type, 805 strlen(field->type) + 806 strlen(last_token) + 2); 807 strcat(field->type, " "); 808 strcat(field->type, last_token); 809 } else 810 field->type = last_token; 811 last_token = token; 812 continue; 813 } 814 815 break; 816 } 817 818 if (!field->type) { 819 die("no type found"); 820 goto fail; 821 } 822 field->name = last_token; 823 824 if (test_type(type, EVENT_OP)) 825 goto fail; 826 827 if (strcmp(token, "[") == 0) { 828 enum event_type last_type = type; 829 char *brackets = token; 830 int len; 831 832 field->flags |= FIELD_IS_ARRAY; 833 834 type = read_token(&token); 835 while (strcmp(token, "]") != 0) { 836 if (last_type == EVENT_ITEM && 837 type == EVENT_ITEM) 838 len = 2; 839 else 840 len = 1; 841 last_type = type; 842 843 brackets = realloc(brackets, 844 strlen(brackets) + 845 strlen(token) + len); 846 if (len == 2) 847 strcat(brackets, " "); 848 strcat(brackets, token); 849 free_token(token); 850 type = read_token(&token); 851 if (type == EVENT_NONE) { 852 die("failed to find token"); 853 goto fail; 854 } 855 } 856 857 free_token(token); 858 859 brackets = realloc(brackets, strlen(brackets) + 2); 860 strcat(brackets, "]"); 861 862 /* add brackets to type */ 863 864 type = read_token(&token); 865 /* 866 * If the next token is not an OP, then it is of 867 * the format: type [] item; 868 */ 869 if (type == EVENT_ITEM) { 870 field->type = realloc(field->type, 871 strlen(field->type) + 872 strlen(field->name) + 873 strlen(brackets) + 2); 874 strcat(field->type, " "); 875 strcat(field->type, field->name); 876 free_token(field->name); 877 strcat(field->type, brackets); 878 field->name = token; 879 type = read_token(&token); 880 } else { 881 field->type = realloc(field->type, 882 strlen(field->type) + 883 strlen(brackets) + 1); 884 strcat(field->type, brackets); 885 } 886 free(brackets); 887 } 888 889 if (field_is_string(field)) { 890 field->flags |= FIELD_IS_STRING; 891 if (field_is_dynamic(field)) 892 field->flags |= FIELD_IS_DYNAMIC; 893 } 894 895 if (test_type_token(type, token, EVENT_OP, (char *)";")) 896 goto fail; 897 free_token(token); 898 899 if (read_expected(EVENT_ITEM, (char *)"offset") < 0) 900 goto fail_expect; 901 902 if (read_expected(EVENT_OP, (char *)":") < 0) 903 goto fail_expect; 904 905 if (read_expect_type(EVENT_ITEM, &token)) 906 goto fail; 907 field->offset = strtoul(token, NULL, 0); 908 free_token(token); 909 910 if (read_expected(EVENT_OP, (char *)";") < 0) 911 goto fail_expect; 912 913 if (read_expected(EVENT_ITEM, (char *)"size") < 0) 914 goto fail_expect; 915 916 if (read_expected(EVENT_OP, (char *)":") < 0) 917 goto fail_expect; 918 919 if (read_expect_type(EVENT_ITEM, &token)) 920 goto fail; 921 field->size = strtoul(token, NULL, 0); 922 free_token(token); 923 924 if (read_expected(EVENT_OP, (char *)";") < 0) 925 goto fail_expect; 926 927 type = read_token(&token); 928 if (type != EVENT_NEWLINE) { 929 /* newer versions of the kernel have a "signed" type */ 930 if (test_type_token(type, token, EVENT_ITEM, (char *)"signed")) 931 goto fail; 932 933 free_token(token); 934 935 if (read_expected(EVENT_OP, (char *)":") < 0) 936 goto fail_expect; 937 938 if (read_expect_type(EVENT_ITEM, &token)) 939 goto fail; 940 941 /* add signed type */ 942 943 free_token(token); 944 if (read_expected(EVENT_OP, (char *)";") < 0) 945 goto fail_expect; 946 947 if (read_expect_type(EVENT_NEWLINE, &token)) 948 goto fail; 949 } 950 951 free_token(token); 952 953 *fields = field; 954 fields = &field->next; 955 956 } while (1); 957 958 return 0; 959 960 fail: 961 free_token(token); 962 fail_expect: 963 if (field) 964 free(field); 965 return -1; 966 } 967 968 static int event_read_format(struct event *event) 969 { 970 char *token; 971 int ret; 972 973 if (read_expected_item(EVENT_ITEM, (char *)"format") < 0) 974 return -1; 975 976 if (read_expected(EVENT_OP, (char *)":") < 0) 977 return -1; 978 979 if (read_expect_type(EVENT_NEWLINE, &token)) 980 goto fail; 981 free_token(token); 982 983 ret = event_read_fields(event, &event->format.common_fields); 984 if (ret < 0) 985 return ret; 986 event->format.nr_common = ret; 987 988 ret = event_read_fields(event, &event->format.fields); 989 if (ret < 0) 990 return ret; 991 event->format.nr_fields = ret; 992 993 return 0; 994 995 fail: 996 free_token(token); 997 return -1; 998 } 999 1000 enum event_type 1001 process_arg_token(struct event *event, struct print_arg *arg, 1002 char **tok, enum event_type type); 1003 1004 static enum event_type 1005 process_arg(struct event *event, struct print_arg *arg, char **tok) 1006 { 1007 enum event_type type; 1008 char *token; 1009 1010 type = read_token(&token); 1011 *tok = token; 1012 1013 return process_arg_token(event, arg, tok, type); 1014 } 1015 1016 static enum event_type 1017 process_cond(struct event *event, struct print_arg *top, char **tok) 1018 { 1019 struct print_arg *arg, *left, *right; 1020 enum event_type type; 1021 char *token = NULL; 1022 1023 arg = malloc_or_die(sizeof(*arg)); 1024 memset(arg, 0, sizeof(*arg)); 1025 1026 left = malloc_or_die(sizeof(*left)); 1027 1028 right = malloc_or_die(sizeof(*right)); 1029 1030 arg->type = PRINT_OP; 1031 arg->op.left = left; 1032 arg->op.right = right; 1033 1034 *tok = NULL; 1035 type = process_arg(event, left, &token); 1036 if (test_type_token(type, token, EVENT_OP, (char *)":")) 1037 goto out_free; 1038 1039 arg->op.op = token; 1040 1041 type = process_arg(event, right, &token); 1042 1043 top->op.right = arg; 1044 1045 *tok = token; 1046 return type; 1047 1048 out_free: 1049 free_token(*tok); 1050 free(right); 1051 free(left); 1052 free_arg(arg); 1053 return EVENT_ERROR; 1054 } 1055 1056 static enum event_type 1057 process_array(struct event *event, struct print_arg *top, char **tok) 1058 { 1059 struct print_arg *arg; 1060 enum event_type type; 1061 char *token = NULL; 1062 1063 arg = malloc_or_die(sizeof(*arg)); 1064 memset(arg, 0, sizeof(*arg)); 1065 1066 *tok = NULL; 1067 type = process_arg(event, arg, &token); 1068 if (test_type_token(type, token, EVENT_OP, (char *)"]")) 1069 goto out_free; 1070 1071 top->op.right = arg; 1072 1073 free_token(token); 1074 type = read_token_item(&token); 1075 *tok = token; 1076 1077 return type; 1078 1079 out_free: 1080 free_token(*tok); 1081 free_arg(arg); 1082 return EVENT_ERROR; 1083 } 1084 1085 static int get_op_prio(char *op) 1086 { 1087 if (!op[1]) { 1088 switch (op[0]) { 1089 case '*': 1090 case '/': 1091 case '%': 1092 return 6; 1093 case '+': 1094 case '-': 1095 return 7; 1096 /* '>>' and '<<' are 8 */ 1097 case '<': 1098 case '>': 1099 return 9; 1100 /* '==' and '!=' are 10 */ 1101 case '&': 1102 return 11; 1103 case '^': 1104 return 12; 1105 case '|': 1106 return 13; 1107 case '?': 1108 return 16; 1109 default: 1110 die("unknown op '%c'", op[0]); 1111 return -1; 1112 } 1113 } else { 1114 if (strcmp(op, "++") == 0 || 1115 strcmp(op, "--") == 0) { 1116 return 3; 1117 } else if (strcmp(op, ">>") == 0 || 1118 strcmp(op, "<<") == 0) { 1119 return 8; 1120 } else if (strcmp(op, ">=") == 0 || 1121 strcmp(op, "<=") == 0) { 1122 return 9; 1123 } else if (strcmp(op, "==") == 0 || 1124 strcmp(op, "!=") == 0) { 1125 return 10; 1126 } else if (strcmp(op, "&&") == 0) { 1127 return 14; 1128 } else if (strcmp(op, "||") == 0) { 1129 return 15; 1130 } else { 1131 die("unknown op '%s'", op); 1132 return -1; 1133 } 1134 } 1135 } 1136 1137 static void set_op_prio(struct print_arg *arg) 1138 { 1139 1140 /* single ops are the greatest */ 1141 if (!arg->op.left || arg->op.left->type == PRINT_NULL) { 1142 arg->op.prio = 0; 1143 return; 1144 } 1145 1146 arg->op.prio = get_op_prio(arg->op.op); 1147 } 1148 1149 static enum event_type 1150 process_op(struct event *event, struct print_arg *arg, char **tok) 1151 { 1152 struct print_arg *left, *right = NULL; 1153 enum event_type type; 1154 char *token; 1155 1156 /* the op is passed in via tok */ 1157 token = *tok; 1158 1159 if (arg->type == PRINT_OP && !arg->op.left) { 1160 /* handle single op */ 1161 if (token[1]) { 1162 die("bad op token %s", token); 1163 return EVENT_ERROR; 1164 } 1165 switch (token[0]) { 1166 case '!': 1167 case '+': 1168 case '-': 1169 break; 1170 default: 1171 die("bad op token %s", token); 1172 return EVENT_ERROR; 1173 } 1174 1175 /* make an empty left */ 1176 left = malloc_or_die(sizeof(*left)); 1177 left->type = PRINT_NULL; 1178 arg->op.left = left; 1179 1180 right = malloc_or_die(sizeof(*right)); 1181 arg->op.right = right; 1182 1183 type = process_arg(event, right, tok); 1184 1185 } else if (strcmp(token, "?") == 0) { 1186 1187 left = malloc_or_die(sizeof(*left)); 1188 /* copy the top arg to the left */ 1189 *left = *arg; 1190 1191 arg->type = PRINT_OP; 1192 arg->op.op = token; 1193 arg->op.left = left; 1194 arg->op.prio = 0; 1195 1196 type = process_cond(event, arg, tok); 1197 1198 } else if (strcmp(token, ">>") == 0 || 1199 strcmp(token, "<<") == 0 || 1200 strcmp(token, "&") == 0 || 1201 strcmp(token, "|") == 0 || 1202 strcmp(token, "&&") == 0 || 1203 strcmp(token, "||") == 0 || 1204 strcmp(token, "-") == 0 || 1205 strcmp(token, "+") == 0 || 1206 strcmp(token, "*") == 0 || 1207 strcmp(token, "^") == 0 || 1208 strcmp(token, "/") == 0 || 1209 strcmp(token, "<") == 0 || 1210 strcmp(token, ">") == 0 || 1211 strcmp(token, "==") == 0 || 1212 strcmp(token, "!=") == 0) { 1213 1214 left = malloc_or_die(sizeof(*left)); 1215 1216 /* copy the top arg to the left */ 1217 *left = *arg; 1218 1219 arg->type = PRINT_OP; 1220 arg->op.op = token; 1221 arg->op.left = left; 1222 1223 set_op_prio(arg); 1224 1225 right = malloc_or_die(sizeof(*right)); 1226 1227 type = read_token_item(&token); 1228 *tok = token; 1229 1230 /* could just be a type pointer */ 1231 if ((strcmp(arg->op.op, "*") == 0) && 1232 type == EVENT_DELIM && (strcmp(token, ")") == 0)) { 1233 if (left->type != PRINT_ATOM) 1234 die("bad pointer type"); 1235 left->atom.atom = realloc(left->atom.atom, 1236 sizeof(left->atom.atom) + 3); 1237 strcat(left->atom.atom, " *"); 1238 *arg = *left; 1239 free(arg); 1240 1241 return type; 1242 } 1243 1244 type = process_arg_token(event, right, tok, type); 1245 1246 arg->op.right = right; 1247 1248 } else if (strcmp(token, "[") == 0) { 1249 1250 left = malloc_or_die(sizeof(*left)); 1251 *left = *arg; 1252 1253 arg->type = PRINT_OP; 1254 arg->op.op = token; 1255 arg->op.left = left; 1256 1257 arg->op.prio = 0; 1258 type = process_array(event, arg, tok); 1259 1260 } else { 1261 warning("unknown op '%s'", token); 1262 event->flags |= EVENT_FL_FAILED; 1263 /* the arg is now the left side */ 1264 return EVENT_NONE; 1265 } 1266 1267 if (type == EVENT_OP) { 1268 int prio; 1269 1270 /* higher prios need to be closer to the root */ 1271 prio = get_op_prio(*tok); 1272 1273 if (prio > arg->op.prio) 1274 return process_op(event, arg, tok); 1275 1276 return process_op(event, right, tok); 1277 } 1278 1279 return type; 1280 } 1281 1282 static enum event_type 1283 process_entry(struct event *event __unused, struct print_arg *arg, 1284 char **tok) 1285 { 1286 enum event_type type; 1287 char *field; 1288 char *token; 1289 1290 if (read_expected(EVENT_OP, (char *)"->") < 0) 1291 return EVENT_ERROR; 1292 1293 if (read_expect_type(EVENT_ITEM, &token) < 0) 1294 goto fail; 1295 field = token; 1296 1297 arg->type = PRINT_FIELD; 1298 arg->field.name = field; 1299 1300 type = read_token(&token); 1301 *tok = token; 1302 1303 return type; 1304 1305 fail: 1306 free_token(token); 1307 return EVENT_ERROR; 1308 } 1309 1310 static char *arg_eval (struct print_arg *arg); 1311 1312 static long long arg_num_eval(struct print_arg *arg) 1313 { 1314 long long left, right; 1315 long long val = 0; 1316 1317 switch (arg->type) { 1318 case PRINT_ATOM: 1319 val = strtoll(arg->atom.atom, NULL, 0); 1320 break; 1321 case PRINT_TYPE: 1322 val = arg_num_eval(arg->typecast.item); 1323 break; 1324 case PRINT_OP: 1325 switch (arg->op.op[0]) { 1326 case '|': 1327 left = arg_num_eval(arg->op.left); 1328 right = arg_num_eval(arg->op.right); 1329 if (arg->op.op[1]) 1330 val = left || right; 1331 else 1332 val = left | right; 1333 break; 1334 case '&': 1335 left = arg_num_eval(arg->op.left); 1336 right = arg_num_eval(arg->op.right); 1337 if (arg->op.op[1]) 1338 val = left && right; 1339 else 1340 val = left & right; 1341 break; 1342 case '<': 1343 left = arg_num_eval(arg->op.left); 1344 right = arg_num_eval(arg->op.right); 1345 switch (arg->op.op[1]) { 1346 case 0: 1347 val = left < right; 1348 break; 1349 case '<': 1350 val = left << right; 1351 break; 1352 case '=': 1353 val = left <= right; 1354 break; 1355 default: 1356 die("unknown op '%s'", arg->op.op); 1357 } 1358 break; 1359 case '>': 1360 left = arg_num_eval(arg->op.left); 1361 right = arg_num_eval(arg->op.right); 1362 switch (arg->op.op[1]) { 1363 case 0: 1364 val = left > right; 1365 break; 1366 case '>': 1367 val = left >> right; 1368 break; 1369 case '=': 1370 val = left >= right; 1371 break; 1372 default: 1373 die("unknown op '%s'", arg->op.op); 1374 } 1375 break; 1376 case '=': 1377 left = arg_num_eval(arg->op.left); 1378 right = arg_num_eval(arg->op.right); 1379 1380 if (arg->op.op[1] != '=') 1381 die("unknown op '%s'", arg->op.op); 1382 1383 val = left == right; 1384 break; 1385 case '!': 1386 left = arg_num_eval(arg->op.left); 1387 right = arg_num_eval(arg->op.right); 1388 1389 switch (arg->op.op[1]) { 1390 case '=': 1391 val = left != right; 1392 break; 1393 default: 1394 die("unknown op '%s'", arg->op.op); 1395 } 1396 break; 1397 default: 1398 die("unknown op '%s'", arg->op.op); 1399 } 1400 break; 1401 1402 case PRINT_NULL: 1403 case PRINT_FIELD ... PRINT_SYMBOL: 1404 case PRINT_STRING: 1405 default: 1406 die("invalid eval type %d", arg->type); 1407 1408 } 1409 return val; 1410 } 1411 1412 static char *arg_eval (struct print_arg *arg) 1413 { 1414 long long val; 1415 static char buf[20]; 1416 1417 switch (arg->type) { 1418 case PRINT_ATOM: 1419 return arg->atom.atom; 1420 case PRINT_TYPE: 1421 return arg_eval(arg->typecast.item); 1422 case PRINT_OP: 1423 val = arg_num_eval(arg); 1424 sprintf(buf, "%lld", val); 1425 return buf; 1426 1427 case PRINT_NULL: 1428 case PRINT_FIELD ... PRINT_SYMBOL: 1429 case PRINT_STRING: 1430 default: 1431 die("invalid eval type %d", arg->type); 1432 break; 1433 } 1434 1435 return NULL; 1436 } 1437 1438 static enum event_type 1439 process_fields(struct event *event, struct print_flag_sym **list, char **tok) 1440 { 1441 enum event_type type; 1442 struct print_arg *arg = NULL; 1443 struct print_flag_sym *field; 1444 char *token = NULL; 1445 char *value; 1446 1447 do { 1448 free_token(token); 1449 type = read_token_item(&token); 1450 if (test_type_token(type, token, EVENT_OP, (char *)"{")) 1451 break; 1452 1453 arg = malloc_or_die(sizeof(*arg)); 1454 1455 free_token(token); 1456 type = process_arg(event, arg, &token); 1457 if (test_type_token(type, token, EVENT_DELIM, (char *)",")) 1458 goto out_free; 1459 1460 field = malloc_or_die(sizeof(*field)); 1461 memset(field, 0, sizeof(field)); 1462 1463 value = arg_eval(arg); 1464 field->value = strdup(value); 1465 1466 free_token(token); 1467 type = process_arg(event, arg, &token); 1468 if (test_type_token(type, token, EVENT_OP, (char *)"}")) 1469 goto out_free; 1470 1471 value = arg_eval(arg); 1472 field->str = strdup(value); 1473 free_arg(arg); 1474 arg = NULL; 1475 1476 *list = field; 1477 list = &field->next; 1478 1479 free_token(token); 1480 type = read_token_item(&token); 1481 } while (type == EVENT_DELIM && strcmp(token, ",") == 0); 1482 1483 *tok = token; 1484 return type; 1485 1486 out_free: 1487 free_arg(arg); 1488 free_token(token); 1489 1490 return EVENT_ERROR; 1491 } 1492 1493 static enum event_type 1494 process_flags(struct event *event, struct print_arg *arg, char **tok) 1495 { 1496 struct print_arg *field; 1497 enum event_type type; 1498 char *token; 1499 1500 memset(arg, 0, sizeof(*arg)); 1501 arg->type = PRINT_FLAGS; 1502 1503 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0) 1504 return EVENT_ERROR; 1505 1506 field = malloc_or_die(sizeof(*field)); 1507 1508 type = process_arg(event, field, &token); 1509 if (test_type_token(type, token, EVENT_DELIM, (char *)",")) 1510 goto out_free; 1511 1512 arg->flags.field = field; 1513 1514 type = read_token_item(&token); 1515 if (event_item_type(type)) { 1516 arg->flags.delim = token; 1517 type = read_token_item(&token); 1518 } 1519 1520 if (test_type_token(type, token, EVENT_DELIM, (char *)",")) 1521 goto out_free; 1522 1523 type = process_fields(event, &arg->flags.flags, &token); 1524 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) 1525 goto out_free; 1526 1527 free_token(token); 1528 type = read_token_item(tok); 1529 return type; 1530 1531 out_free: 1532 free_token(token); 1533 return EVENT_ERROR; 1534 } 1535 1536 static enum event_type 1537 process_symbols(struct event *event, struct print_arg *arg, char **tok) 1538 { 1539 struct print_arg *field; 1540 enum event_type type; 1541 char *token; 1542 1543 memset(arg, 0, sizeof(*arg)); 1544 arg->type = PRINT_SYMBOL; 1545 1546 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0) 1547 return EVENT_ERROR; 1548 1549 field = malloc_or_die(sizeof(*field)); 1550 1551 type = process_arg(event, field, &token); 1552 if (test_type_token(type, token, EVENT_DELIM, (char *)",")) 1553 goto out_free; 1554 1555 arg->symbol.field = field; 1556 1557 type = process_fields(event, &arg->symbol.symbols, &token); 1558 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) 1559 goto out_free; 1560 1561 free_token(token); 1562 type = read_token_item(tok); 1563 return type; 1564 1565 out_free: 1566 free_token(token); 1567 return EVENT_ERROR; 1568 } 1569 1570 static enum event_type 1571 process_paren(struct event *event, struct print_arg *arg, char **tok) 1572 { 1573 struct print_arg *item_arg; 1574 enum event_type type; 1575 char *token; 1576 1577 type = process_arg(event, arg, &token); 1578 1579 if (type == EVENT_ERROR) 1580 return EVENT_ERROR; 1581 1582 if (type == EVENT_OP) 1583 type = process_op(event, arg, &token); 1584 1585 if (type == EVENT_ERROR) 1586 return EVENT_ERROR; 1587 1588 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) { 1589 free_token(token); 1590 return EVENT_ERROR; 1591 } 1592 1593 free_token(token); 1594 type = read_token_item(&token); 1595 1596 /* 1597 * If the next token is an item or another open paren, then 1598 * this was a typecast. 1599 */ 1600 if (event_item_type(type) || 1601 (type == EVENT_DELIM && strcmp(token, "(") == 0)) { 1602 1603 /* make this a typecast and contine */ 1604 1605 /* prevous must be an atom */ 1606 if (arg->type != PRINT_ATOM) 1607 die("previous needed to be PRINT_ATOM"); 1608 1609 item_arg = malloc_or_die(sizeof(*item_arg)); 1610 1611 arg->type = PRINT_TYPE; 1612 arg->typecast.type = arg->atom.atom; 1613 arg->typecast.item = item_arg; 1614 type = process_arg_token(event, item_arg, &token, type); 1615 1616 } 1617 1618 *tok = token; 1619 return type; 1620 } 1621 1622 1623 static enum event_type 1624 process_str(struct event *event __unused, struct print_arg *arg, char **tok) 1625 { 1626 enum event_type type; 1627 char *token; 1628 1629 if (read_expected(EVENT_DELIM, (char *)"(") < 0) 1630 return EVENT_ERROR; 1631 1632 if (read_expect_type(EVENT_ITEM, &token) < 0) 1633 goto fail; 1634 1635 arg->type = PRINT_STRING; 1636 arg->string.string = token; 1637 arg->string.offset = -1; 1638 1639 if (read_expected(EVENT_DELIM, (char *)")") < 0) 1640 return EVENT_ERROR; 1641 1642 type = read_token(&token); 1643 *tok = token; 1644 1645 return type; 1646 fail: 1647 free_token(token); 1648 return EVENT_ERROR; 1649 } 1650 1651 enum event_type 1652 process_arg_token(struct event *event, struct print_arg *arg, 1653 char **tok, enum event_type type) 1654 { 1655 char *token; 1656 char *atom; 1657 1658 token = *tok; 1659 1660 switch (type) { 1661 case EVENT_ITEM: 1662 if (strcmp(token, "REC") == 0) { 1663 free_token(token); 1664 type = process_entry(event, arg, &token); 1665 } else if (strcmp(token, "__print_flags") == 0) { 1666 free_token(token); 1667 type = process_flags(event, arg, &token); 1668 } else if (strcmp(token, "__print_symbolic") == 0) { 1669 free_token(token); 1670 type = process_symbols(event, arg, &token); 1671 } else if (strcmp(token, "__get_str") == 0) { 1672 free_token(token); 1673 type = process_str(event, arg, &token); 1674 } else { 1675 atom = token; 1676 /* test the next token */ 1677 type = read_token_item(&token); 1678 1679 /* atoms can be more than one token long */ 1680 while (type == EVENT_ITEM) { 1681 atom = realloc(atom, strlen(atom) + strlen(token) + 2); 1682 strcat(atom, " "); 1683 strcat(atom, token); 1684 free_token(token); 1685 type = read_token_item(&token); 1686 } 1687 1688 /* todo, test for function */ 1689 1690 arg->type = PRINT_ATOM; 1691 arg->atom.atom = atom; 1692 } 1693 break; 1694 case EVENT_DQUOTE: 1695 case EVENT_SQUOTE: 1696 arg->type = PRINT_ATOM; 1697 arg->atom.atom = token; 1698 type = read_token_item(&token); 1699 break; 1700 case EVENT_DELIM: 1701 if (strcmp(token, "(") == 0) { 1702 free_token(token); 1703 type = process_paren(event, arg, &token); 1704 break; 1705 } 1706 case EVENT_OP: 1707 /* handle single ops */ 1708 arg->type = PRINT_OP; 1709 arg->op.op = token; 1710 arg->op.left = NULL; 1711 type = process_op(event, arg, &token); 1712 1713 break; 1714 1715 case EVENT_ERROR ... EVENT_NEWLINE: 1716 default: 1717 die("unexpected type %d", type); 1718 } 1719 *tok = token; 1720 1721 return type; 1722 } 1723 1724 static int event_read_print_args(struct event *event, struct print_arg **list) 1725 { 1726 enum event_type type = EVENT_ERROR; 1727 struct print_arg *arg; 1728 char *token; 1729 int args = 0; 1730 1731 do { 1732 if (type == EVENT_NEWLINE) { 1733 free_token(token); 1734 type = read_token_item(&token); 1735 continue; 1736 } 1737 1738 arg = malloc_or_die(sizeof(*arg)); 1739 memset(arg, 0, sizeof(*arg)); 1740 1741 type = process_arg(event, arg, &token); 1742 1743 if (type == EVENT_ERROR) { 1744 free_arg(arg); 1745 return -1; 1746 } 1747 1748 *list = arg; 1749 args++; 1750 1751 if (type == EVENT_OP) { 1752 type = process_op(event, arg, &token); 1753 list = &arg->next; 1754 continue; 1755 } 1756 1757 if (type == EVENT_DELIM && strcmp(token, ",") == 0) { 1758 free_token(token); 1759 *list = arg; 1760 list = &arg->next; 1761 continue; 1762 } 1763 break; 1764 } while (type != EVENT_NONE); 1765 1766 if (type != EVENT_NONE) 1767 free_token(token); 1768 1769 return args; 1770 } 1771 1772 static int event_read_print(struct event *event) 1773 { 1774 enum event_type type; 1775 char *token; 1776 int ret; 1777 1778 if (read_expected_item(EVENT_ITEM, (char *)"print") < 0) 1779 return -1; 1780 1781 if (read_expected(EVENT_ITEM, (char *)"fmt") < 0) 1782 return -1; 1783 1784 if (read_expected(EVENT_OP, (char *)":") < 0) 1785 return -1; 1786 1787 if (read_expect_type(EVENT_DQUOTE, &token) < 0) 1788 goto fail; 1789 1790 concat: 1791 event->print_fmt.format = token; 1792 event->print_fmt.args = NULL; 1793 1794 /* ok to have no arg */ 1795 type = read_token_item(&token); 1796 1797 if (type == EVENT_NONE) 1798 return 0; 1799 1800 /* Handle concatination of print lines */ 1801 if (type == EVENT_DQUOTE) { 1802 char *cat; 1803 1804 cat = malloc_or_die(strlen(event->print_fmt.format) + 1805 strlen(token) + 1); 1806 strcpy(cat, event->print_fmt.format); 1807 strcat(cat, token); 1808 free_token(token); 1809 free_token(event->print_fmt.format); 1810 event->print_fmt.format = NULL; 1811 token = cat; 1812 goto concat; 1813 } 1814 1815 if (test_type_token(type, token, EVENT_DELIM, (char *)",")) 1816 goto fail; 1817 1818 free_token(token); 1819 1820 ret = event_read_print_args(event, &event->print_fmt.args); 1821 if (ret < 0) 1822 return -1; 1823 1824 return ret; 1825 1826 fail: 1827 free_token(token); 1828 return -1; 1829 } 1830 1831 static struct format_field * 1832 find_common_field(struct event *event, const char *name) 1833 { 1834 struct format_field *format; 1835 1836 for (format = event->format.common_fields; 1837 format; format = format->next) { 1838 if (strcmp(format->name, name) == 0) 1839 break; 1840 } 1841 1842 return format; 1843 } 1844 1845 static struct format_field * 1846 find_field(struct event *event, const char *name) 1847 { 1848 struct format_field *format; 1849 1850 for (format = event->format.fields; 1851 format; format = format->next) { 1852 if (strcmp(format->name, name) == 0) 1853 break; 1854 } 1855 1856 return format; 1857 } 1858 1859 static struct format_field * 1860 find_any_field(struct event *event, const char *name) 1861 { 1862 struct format_field *format; 1863 1864 format = find_common_field(event, name); 1865 if (format) 1866 return format; 1867 return find_field(event, name); 1868 } 1869 1870 static unsigned long long read_size(void *ptr, int size) 1871 { 1872 switch (size) { 1873 case 1: 1874 return *(unsigned char *)ptr; 1875 case 2: 1876 return data2host2(ptr); 1877 case 4: 1878 return data2host4(ptr); 1879 case 8: 1880 return data2host8(ptr); 1881 default: 1882 /* BUG! */ 1883 return 0; 1884 } 1885 } 1886 1887 unsigned long long 1888 raw_field_value(struct event *event, const char *name, void *data) 1889 { 1890 struct format_field *field; 1891 1892 field = find_any_field(event, name); 1893 if (!field) 1894 return 0ULL; 1895 1896 return read_size(data + field->offset, field->size); 1897 } 1898 1899 void *raw_field_ptr(struct event *event, const char *name, void *data) 1900 { 1901 struct format_field *field; 1902 1903 field = find_any_field(event, name); 1904 if (!field) 1905 return NULL; 1906 1907 return data + field->offset; 1908 } 1909 1910 static int get_common_info(const char *type, int *offset, int *size) 1911 { 1912 struct event *event; 1913 struct format_field *field; 1914 1915 /* 1916 * All events should have the same common elements. 1917 * Pick any event to find where the type is; 1918 */ 1919 if (!event_list) 1920 die("no event_list!"); 1921 1922 event = event_list; 1923 field = find_common_field(event, type); 1924 if (!field) 1925 die("field '%s' not found", type); 1926 1927 *offset = field->offset; 1928 *size = field->size; 1929 1930 return 0; 1931 } 1932 1933 static int __parse_common(void *data, int *size, int *offset, 1934 char *name) 1935 { 1936 int ret; 1937 1938 if (!*size) { 1939 ret = get_common_info(name, offset, size); 1940 if (ret < 0) 1941 return ret; 1942 } 1943 return read_size(data + *offset, *size); 1944 } 1945 1946 int trace_parse_common_type(void *data) 1947 { 1948 static int type_offset; 1949 static int type_size; 1950 1951 return __parse_common(data, &type_size, &type_offset, 1952 (char *)"common_type"); 1953 } 1954 1955 static int parse_common_pid(void *data) 1956 { 1957 static int pid_offset; 1958 static int pid_size; 1959 1960 return __parse_common(data, &pid_size, &pid_offset, 1961 (char *)"common_pid"); 1962 } 1963 1964 static int parse_common_pc(void *data) 1965 { 1966 static int pc_offset; 1967 static int pc_size; 1968 1969 return __parse_common(data, &pc_size, &pc_offset, 1970 (char *)"common_preempt_count"); 1971 } 1972 1973 static int parse_common_flags(void *data) 1974 { 1975 static int flags_offset; 1976 static int flags_size; 1977 1978 return __parse_common(data, &flags_size, &flags_offset, 1979 (char *)"common_flags"); 1980 } 1981 1982 static int parse_common_lock_depth(void *data) 1983 { 1984 static int ld_offset; 1985 static int ld_size; 1986 int ret; 1987 1988 ret = __parse_common(data, &ld_size, &ld_offset, 1989 (char *)"common_lock_depth"); 1990 if (ret < 0) 1991 return -1; 1992 1993 return ret; 1994 } 1995 1996 struct event *trace_find_event(int id) 1997 { 1998 struct event *event; 1999 2000 for (event = event_list; event; event = event->next) { 2001 if (event->id == id) 2002 break; 2003 } 2004 return event; 2005 } 2006 2007 static unsigned long long eval_num_arg(void *data, int size, 2008 struct event *event, struct print_arg *arg) 2009 { 2010 unsigned long long val = 0; 2011 unsigned long long left, right; 2012 struct print_arg *larg; 2013 2014 switch (arg->type) { 2015 case PRINT_NULL: 2016 /* ?? */ 2017 return 0; 2018 case PRINT_ATOM: 2019 return strtoull(arg->atom.atom, NULL, 0); 2020 case PRINT_FIELD: 2021 if (!arg->field.field) { 2022 arg->field.field = find_any_field(event, arg->field.name); 2023 if (!arg->field.field) 2024 die("field %s not found", arg->field.name); 2025 } 2026 /* must be a number */ 2027 val = read_size(data + arg->field.field->offset, 2028 arg->field.field->size); 2029 break; 2030 case PRINT_FLAGS: 2031 case PRINT_SYMBOL: 2032 break; 2033 case PRINT_TYPE: 2034 return eval_num_arg(data, size, event, arg->typecast.item); 2035 case PRINT_STRING: 2036 return 0; 2037 break; 2038 case PRINT_OP: 2039 if (strcmp(arg->op.op, "[") == 0) { 2040 /* 2041 * Arrays are special, since we don't want 2042 * to read the arg as is. 2043 */ 2044 if (arg->op.left->type != PRINT_FIELD) 2045 goto default_op; /* oops, all bets off */ 2046 larg = arg->op.left; 2047 if (!larg->field.field) { 2048 larg->field.field = 2049 find_any_field(event, larg->field.name); 2050 if (!larg->field.field) 2051 die("field %s not found", larg->field.name); 2052 } 2053 right = eval_num_arg(data, size, event, arg->op.right); 2054 val = read_size(data + larg->field.field->offset + 2055 right * long_size, long_size); 2056 break; 2057 } 2058 default_op: 2059 left = eval_num_arg(data, size, event, arg->op.left); 2060 right = eval_num_arg(data, size, event, arg->op.right); 2061 switch (arg->op.op[0]) { 2062 case '|': 2063 if (arg->op.op[1]) 2064 val = left || right; 2065 else 2066 val = left | right; 2067 break; 2068 case '&': 2069 if (arg->op.op[1]) 2070 val = left && right; 2071 else 2072 val = left & right; 2073 break; 2074 case '<': 2075 switch (arg->op.op[1]) { 2076 case 0: 2077 val = left < right; 2078 break; 2079 case '<': 2080 val = left << right; 2081 break; 2082 case '=': 2083 val = left <= right; 2084 break; 2085 default: 2086 die("unknown op '%s'", arg->op.op); 2087 } 2088 break; 2089 case '>': 2090 switch (arg->op.op[1]) { 2091 case 0: 2092 val = left > right; 2093 break; 2094 case '>': 2095 val = left >> right; 2096 break; 2097 case '=': 2098 val = left >= right; 2099 break; 2100 default: 2101 die("unknown op '%s'", arg->op.op); 2102 } 2103 break; 2104 case '=': 2105 if (arg->op.op[1] != '=') 2106 die("unknown op '%s'", arg->op.op); 2107 val = left == right; 2108 break; 2109 default: 2110 die("unknown op '%s'", arg->op.op); 2111 } 2112 break; 2113 default: /* not sure what to do there */ 2114 return 0; 2115 } 2116 return val; 2117 } 2118 2119 struct flag { 2120 const char *name; 2121 unsigned long long value; 2122 }; 2123 2124 static const struct flag flags[] = { 2125 { "HI_SOFTIRQ", 0 }, 2126 { "TIMER_SOFTIRQ", 1 }, 2127 { "NET_TX_SOFTIRQ", 2 }, 2128 { "NET_RX_SOFTIRQ", 3 }, 2129 { "BLOCK_SOFTIRQ", 4 }, 2130 { "BLOCK_IOPOLL_SOFTIRQ", 5 }, 2131 { "TASKLET_SOFTIRQ", 6 }, 2132 { "SCHED_SOFTIRQ", 7 }, 2133 { "HRTIMER_SOFTIRQ", 8 }, 2134 { "RCU_SOFTIRQ", 9 }, 2135 2136 { "HRTIMER_NORESTART", 0 }, 2137 { "HRTIMER_RESTART", 1 }, 2138 }; 2139 2140 static unsigned long long eval_flag(const char *flag) 2141 { 2142 int i; 2143 2144 /* 2145 * Some flags in the format files do not get converted. 2146 * If the flag is not numeric, see if it is something that 2147 * we already know about. 2148 */ 2149 if (isdigit(flag[0])) 2150 return strtoull(flag, NULL, 0); 2151 2152 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 2153 if (strcmp(flags[i].name, flag) == 0) 2154 return flags[i].value; 2155 2156 return 0; 2157 } 2158 2159 static void print_str_arg(void *data, int size, 2160 struct event *event, struct print_arg *arg) 2161 { 2162 struct print_flag_sym *flag; 2163 unsigned long long val, fval; 2164 char *str; 2165 int print; 2166 2167 switch (arg->type) { 2168 case PRINT_NULL: 2169 /* ?? */ 2170 return; 2171 case PRINT_ATOM: 2172 printf("%s", arg->atom.atom); 2173 return; 2174 case PRINT_FIELD: 2175 if (!arg->field.field) { 2176 arg->field.field = find_any_field(event, arg->field.name); 2177 if (!arg->field.field) 2178 die("field %s not found", arg->field.name); 2179 } 2180 str = malloc_or_die(arg->field.field->size + 1); 2181 memcpy(str, data + arg->field.field->offset, 2182 arg->field.field->size); 2183 str[arg->field.field->size] = 0; 2184 printf("%s", str); 2185 free(str); 2186 break; 2187 case PRINT_FLAGS: 2188 val = eval_num_arg(data, size, event, arg->flags.field); 2189 print = 0; 2190 for (flag = arg->flags.flags; flag; flag = flag->next) { 2191 fval = eval_flag(flag->value); 2192 if (!val && !fval) { 2193 printf("%s", flag->str); 2194 break; 2195 } 2196 if (fval && (val & fval) == fval) { 2197 if (print && arg->flags.delim) 2198 printf("%s", arg->flags.delim); 2199 printf("%s", flag->str); 2200 print = 1; 2201 val &= ~fval; 2202 } 2203 } 2204 break; 2205 case PRINT_SYMBOL: 2206 val = eval_num_arg(data, size, event, arg->symbol.field); 2207 for (flag = arg->symbol.symbols; flag; flag = flag->next) { 2208 fval = eval_flag(flag->value); 2209 if (val == fval) { 2210 printf("%s", flag->str); 2211 break; 2212 } 2213 } 2214 break; 2215 2216 case PRINT_TYPE: 2217 break; 2218 case PRINT_STRING: { 2219 int str_offset; 2220 2221 if (arg->string.offset == -1) { 2222 struct format_field *f; 2223 2224 f = find_any_field(event, arg->string.string); 2225 arg->string.offset = f->offset; 2226 } 2227 str_offset = *(int *)(data + arg->string.offset); 2228 str_offset &= 0xffff; 2229 printf("%s", ((char *)data) + str_offset); 2230 break; 2231 } 2232 case PRINT_OP: 2233 /* 2234 * The only op for string should be ? : 2235 */ 2236 if (arg->op.op[0] != '?') 2237 return; 2238 val = eval_num_arg(data, size, event, arg->op.left); 2239 if (val) 2240 print_str_arg(data, size, event, arg->op.right->op.left); 2241 else 2242 print_str_arg(data, size, event, arg->op.right->op.right); 2243 break; 2244 default: 2245 /* well... */ 2246 break; 2247 } 2248 } 2249 2250 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event) 2251 { 2252 static struct format_field *field, *ip_field; 2253 struct print_arg *args, *arg, **next; 2254 unsigned long long ip, val; 2255 char *ptr; 2256 void *bptr; 2257 2258 if (!field) { 2259 field = find_field(event, "buf"); 2260 if (!field) 2261 die("can't find buffer field for binary printk"); 2262 ip_field = find_field(event, "ip"); 2263 if (!ip_field) 2264 die("can't find ip field for binary printk"); 2265 } 2266 2267 ip = read_size(data + ip_field->offset, ip_field->size); 2268 2269 /* 2270 * The first arg is the IP pointer. 2271 */ 2272 args = malloc_or_die(sizeof(*args)); 2273 arg = args; 2274 arg->next = NULL; 2275 next = &arg->next; 2276 2277 arg->type = PRINT_ATOM; 2278 arg->atom.atom = malloc_or_die(32); 2279 sprintf(arg->atom.atom, "%lld", ip); 2280 2281 /* skip the first "%pf : " */ 2282 for (ptr = fmt + 6, bptr = data + field->offset; 2283 bptr < data + size && *ptr; ptr++) { 2284 int ls = 0; 2285 2286 if (*ptr == '%') { 2287 process_again: 2288 ptr++; 2289 switch (*ptr) { 2290 case '%': 2291 break; 2292 case 'l': 2293 ls++; 2294 goto process_again; 2295 case 'L': 2296 ls = 2; 2297 goto process_again; 2298 case '0' ... '9': 2299 goto process_again; 2300 case 'p': 2301 ls = 1; 2302 /* fall through */ 2303 case 'd': 2304 case 'u': 2305 case 'x': 2306 case 'i': 2307 /* the pointers are always 4 bytes aligned */ 2308 bptr = (void *)(((unsigned long)bptr + 3) & 2309 ~3); 2310 switch (ls) { 2311 case 0: 2312 case 1: 2313 ls = long_size; 2314 break; 2315 case 2: 2316 ls = 8; 2317 default: 2318 break; 2319 } 2320 val = read_size(bptr, ls); 2321 bptr += ls; 2322 arg = malloc_or_die(sizeof(*arg)); 2323 arg->next = NULL; 2324 arg->type = PRINT_ATOM; 2325 arg->atom.atom = malloc_or_die(32); 2326 sprintf(arg->atom.atom, "%lld", val); 2327 *next = arg; 2328 next = &arg->next; 2329 break; 2330 case 's': 2331 arg = malloc_or_die(sizeof(*arg)); 2332 arg->next = NULL; 2333 arg->type = PRINT_STRING; 2334 arg->string.string = strdup(bptr); 2335 bptr += strlen(bptr) + 1; 2336 *next = arg; 2337 next = &arg->next; 2338 default: 2339 break; 2340 } 2341 } 2342 } 2343 2344 return args; 2345 } 2346 2347 static void free_args(struct print_arg *args) 2348 { 2349 struct print_arg *next; 2350 2351 while (args) { 2352 next = args->next; 2353 2354 if (args->type == PRINT_ATOM) 2355 free(args->atom.atom); 2356 else 2357 free(args->string.string); 2358 free(args); 2359 args = next; 2360 } 2361 } 2362 2363 static char *get_bprint_format(void *data, int size __unused, struct event *event) 2364 { 2365 unsigned long long addr; 2366 static struct format_field *field; 2367 struct printk_map *printk; 2368 char *format; 2369 char *p; 2370 2371 if (!field) { 2372 field = find_field(event, "fmt"); 2373 if (!field) 2374 die("can't find format field for binary printk"); 2375 printf("field->offset = %d size=%d\n", field->offset, field->size); 2376 } 2377 2378 addr = read_size(data + field->offset, field->size); 2379 2380 printk = find_printk(addr); 2381 if (!printk) { 2382 format = malloc_or_die(45); 2383 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n", 2384 addr); 2385 return format; 2386 } 2387 2388 p = printk->printk; 2389 /* Remove any quotes. */ 2390 if (*p == '"') 2391 p++; 2392 format = malloc_or_die(strlen(p) + 10); 2393 sprintf(format, "%s : %s", "%pf", p); 2394 /* remove ending quotes and new line since we will add one too */ 2395 p = format + strlen(format) - 1; 2396 if (*p == '"') 2397 *p = 0; 2398 2399 p -= 2; 2400 if (strcmp(p, "\\n") == 0) 2401 *p = 0; 2402 2403 return format; 2404 } 2405 2406 static void pretty_print(void *data, int size, struct event *event) 2407 { 2408 struct print_fmt *print_fmt = &event->print_fmt; 2409 struct print_arg *arg = print_fmt->args; 2410 struct print_arg *args = NULL; 2411 const char *ptr = print_fmt->format; 2412 unsigned long long val; 2413 struct func_map *func; 2414 const char *saveptr; 2415 char *bprint_fmt = NULL; 2416 char format[32]; 2417 int show_func; 2418 int len; 2419 int ls; 2420 2421 if (event->flags & EVENT_FL_ISFUNC) 2422 ptr = " %pF <-- %pF"; 2423 2424 if (event->flags & EVENT_FL_ISBPRINT) { 2425 bprint_fmt = get_bprint_format(data, size, event); 2426 args = make_bprint_args(bprint_fmt, data, size, event); 2427 arg = args; 2428 ptr = bprint_fmt; 2429 } 2430 2431 for (; *ptr; ptr++) { 2432 ls = 0; 2433 if (*ptr == '\\') { 2434 ptr++; 2435 switch (*ptr) { 2436 case 'n': 2437 printf("\n"); 2438 break; 2439 case 't': 2440 printf("\t"); 2441 break; 2442 case 'r': 2443 printf("\r"); 2444 break; 2445 case '\\': 2446 printf("\\"); 2447 break; 2448 default: 2449 printf("%c", *ptr); 2450 break; 2451 } 2452 2453 } else if (*ptr == '%') { 2454 saveptr = ptr; 2455 show_func = 0; 2456 cont_process: 2457 ptr++; 2458 switch (*ptr) { 2459 case '%': 2460 printf("%%"); 2461 break; 2462 case 'l': 2463 ls++; 2464 goto cont_process; 2465 case 'L': 2466 ls = 2; 2467 goto cont_process; 2468 case 'z': 2469 case 'Z': 2470 case '0' ... '9': 2471 goto cont_process; 2472 case 'p': 2473 if (long_size == 4) 2474 ls = 1; 2475 else 2476 ls = 2; 2477 2478 if (*(ptr+1) == 'F' || 2479 *(ptr+1) == 'f') { 2480 ptr++; 2481 show_func = *ptr; 2482 } 2483 2484 /* fall through */ 2485 case 'd': 2486 case 'i': 2487 case 'x': 2488 case 'X': 2489 case 'u': 2490 if (!arg) 2491 die("no argument match"); 2492 2493 len = ((unsigned long)ptr + 1) - 2494 (unsigned long)saveptr; 2495 2496 /* should never happen */ 2497 if (len > 32) 2498 die("bad format!"); 2499 2500 memcpy(format, saveptr, len); 2501 format[len] = 0; 2502 2503 val = eval_num_arg(data, size, event, arg); 2504 arg = arg->next; 2505 2506 if (show_func) { 2507 func = find_func(val); 2508 if (func) { 2509 printf("%s", func->func); 2510 if (show_func == 'F') 2511 printf("+0x%llx", 2512 val - func->addr); 2513 break; 2514 } 2515 } 2516 switch (ls) { 2517 case 0: 2518 printf(format, (int)val); 2519 break; 2520 case 1: 2521 printf(format, (long)val); 2522 break; 2523 case 2: 2524 printf(format, (long long)val); 2525 break; 2526 default: 2527 die("bad count (%d)", ls); 2528 } 2529 break; 2530 case 's': 2531 if (!arg) 2532 die("no matching argument"); 2533 2534 print_str_arg(data, size, event, arg); 2535 arg = arg->next; 2536 break; 2537 default: 2538 printf(">%c<", *ptr); 2539 2540 } 2541 } else 2542 printf("%c", *ptr); 2543 } 2544 2545 if (args) { 2546 free_args(args); 2547 free(bprint_fmt); 2548 } 2549 } 2550 2551 static inline int log10_cpu(int nb) 2552 { 2553 if (nb / 100) 2554 return 3; 2555 if (nb / 10) 2556 return 2; 2557 return 1; 2558 } 2559 2560 static void print_lat_fmt(void *data, int size __unused) 2561 { 2562 unsigned int lat_flags; 2563 unsigned int pc; 2564 int lock_depth; 2565 int hardirq; 2566 int softirq; 2567 2568 lat_flags = parse_common_flags(data); 2569 pc = parse_common_pc(data); 2570 lock_depth = parse_common_lock_depth(data); 2571 2572 hardirq = lat_flags & TRACE_FLAG_HARDIRQ; 2573 softirq = lat_flags & TRACE_FLAG_SOFTIRQ; 2574 2575 printf("%c%c%c", 2576 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : 2577 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 2578 'X' : '.', 2579 (lat_flags & TRACE_FLAG_NEED_RESCHED) ? 2580 'N' : '.', 2581 (hardirq && softirq) ? 'H' : 2582 hardirq ? 'h' : softirq ? 's' : '.'); 2583 2584 if (pc) 2585 printf("%x", pc); 2586 else 2587 printf("."); 2588 2589 if (lock_depth < 0) 2590 printf("."); 2591 else 2592 printf("%d", lock_depth); 2593 } 2594 2595 /* taken from Linux, written by Frederic Weisbecker */ 2596 static void print_graph_cpu(int cpu) 2597 { 2598 int i; 2599 int log10_this = log10_cpu(cpu); 2600 int log10_all = log10_cpu(cpus); 2601 2602 2603 /* 2604 * Start with a space character - to make it stand out 2605 * to the right a bit when trace output is pasted into 2606 * email: 2607 */ 2608 printf(" "); 2609 2610 /* 2611 * Tricky - we space the CPU field according to the max 2612 * number of online CPUs. On a 2-cpu system it would take 2613 * a maximum of 1 digit - on a 128 cpu system it would 2614 * take up to 3 digits: 2615 */ 2616 for (i = 0; i < log10_all - log10_this; i++) 2617 printf(" "); 2618 2619 printf("%d) ", cpu); 2620 } 2621 2622 #define TRACE_GRAPH_PROCINFO_LENGTH 14 2623 #define TRACE_GRAPH_INDENT 2 2624 2625 static void print_graph_proc(int pid, const char *comm) 2626 { 2627 /* sign + log10(MAX_INT) + '\0' */ 2628 char pid_str[11]; 2629 int spaces = 0; 2630 int len; 2631 int i; 2632 2633 sprintf(pid_str, "%d", pid); 2634 2635 /* 1 stands for the "-" character */ 2636 len = strlen(comm) + strlen(pid_str) + 1; 2637 2638 if (len < TRACE_GRAPH_PROCINFO_LENGTH) 2639 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; 2640 2641 /* First spaces to align center */ 2642 for (i = 0; i < spaces / 2; i++) 2643 printf(" "); 2644 2645 printf("%s-%s", comm, pid_str); 2646 2647 /* Last spaces to align center */ 2648 for (i = 0; i < spaces - (spaces / 2); i++) 2649 printf(" "); 2650 } 2651 2652 static struct record * 2653 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func, 2654 struct record *next) 2655 { 2656 struct format_field *field; 2657 struct event *event; 2658 unsigned long val; 2659 int type; 2660 int pid; 2661 2662 type = trace_parse_common_type(next->data); 2663 event = trace_find_event(type); 2664 if (!event) 2665 return NULL; 2666 2667 if (!(event->flags & EVENT_FL_ISFUNCRET)) 2668 return NULL; 2669 2670 pid = parse_common_pid(next->data); 2671 field = find_field(event, "func"); 2672 if (!field) 2673 die("function return does not have field func"); 2674 2675 val = read_size(next->data + field->offset, field->size); 2676 2677 if (cur_pid != pid || cur_func != val) 2678 return NULL; 2679 2680 /* this is a leaf, now advance the iterator */ 2681 return trace_read_data(cpu); 2682 } 2683 2684 /* Signal a overhead of time execution to the output */ 2685 static void print_graph_overhead(unsigned long long duration) 2686 { 2687 /* Non nested entry or return */ 2688 if (duration == ~0ULL) 2689 return (void)printf(" "); 2690 2691 /* Duration exceeded 100 msecs */ 2692 if (duration > 100000ULL) 2693 return (void)printf("! "); 2694 2695 /* Duration exceeded 10 msecs */ 2696 if (duration > 10000ULL) 2697 return (void)printf("+ "); 2698 2699 printf(" "); 2700 } 2701 2702 static void print_graph_duration(unsigned long long duration) 2703 { 2704 unsigned long usecs = duration / 1000; 2705 unsigned long nsecs_rem = duration % 1000; 2706 /* log10(ULONG_MAX) + '\0' */ 2707 char msecs_str[21]; 2708 char nsecs_str[5]; 2709 int len; 2710 int i; 2711 2712 sprintf(msecs_str, "%lu", usecs); 2713 2714 /* Print msecs */ 2715 len = printf("%lu", usecs); 2716 2717 /* Print nsecs (we don't want to exceed 7 numbers) */ 2718 if (len < 7) { 2719 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); 2720 len += printf(".%s", nsecs_str); 2721 } 2722 2723 printf(" us "); 2724 2725 /* Print remaining spaces to fit the row's width */ 2726 for (i = len; i < 7; i++) 2727 printf(" "); 2728 2729 printf("| "); 2730 } 2731 2732 static void 2733 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec) 2734 { 2735 unsigned long long rettime, calltime; 2736 unsigned long long duration, depth; 2737 unsigned long long val; 2738 struct format_field *field; 2739 struct func_map *func; 2740 struct event *ret_event; 2741 int type; 2742 int i; 2743 2744 type = trace_parse_common_type(ret_rec->data); 2745 ret_event = trace_find_event(type); 2746 2747 field = find_field(ret_event, "rettime"); 2748 if (!field) 2749 die("can't find rettime in return graph"); 2750 rettime = read_size(ret_rec->data + field->offset, field->size); 2751 2752 field = find_field(ret_event, "calltime"); 2753 if (!field) 2754 die("can't find rettime in return graph"); 2755 calltime = read_size(ret_rec->data + field->offset, field->size); 2756 2757 duration = rettime - calltime; 2758 2759 /* Overhead */ 2760 print_graph_overhead(duration); 2761 2762 /* Duration */ 2763 print_graph_duration(duration); 2764 2765 field = find_field(event, "depth"); 2766 if (!field) 2767 die("can't find depth in entry graph"); 2768 depth = read_size(data + field->offset, field->size); 2769 2770 /* Function */ 2771 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2772 printf(" "); 2773 2774 field = find_field(event, "func"); 2775 if (!field) 2776 die("can't find func in entry graph"); 2777 val = read_size(data + field->offset, field->size); 2778 func = find_func(val); 2779 2780 if (func) 2781 printf("%s();", func->func); 2782 else 2783 printf("%llx();", val); 2784 } 2785 2786 static void print_graph_nested(struct event *event, void *data) 2787 { 2788 struct format_field *field; 2789 unsigned long long depth; 2790 unsigned long long val; 2791 struct func_map *func; 2792 int i; 2793 2794 /* No overhead */ 2795 print_graph_overhead(-1); 2796 2797 /* No time */ 2798 printf(" | "); 2799 2800 field = find_field(event, "depth"); 2801 if (!field) 2802 die("can't find depth in entry graph"); 2803 depth = read_size(data + field->offset, field->size); 2804 2805 /* Function */ 2806 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2807 printf(" "); 2808 2809 field = find_field(event, "func"); 2810 if (!field) 2811 die("can't find func in entry graph"); 2812 val = read_size(data + field->offset, field->size); 2813 func = find_func(val); 2814 2815 if (func) 2816 printf("%s() {", func->func); 2817 else 2818 printf("%llx() {", val); 2819 } 2820 2821 static void 2822 pretty_print_func_ent(void *data, int size, struct event *event, 2823 int cpu, int pid, const char *comm, 2824 unsigned long secs, unsigned long usecs) 2825 { 2826 struct format_field *field; 2827 struct record *rec; 2828 void *copy_data; 2829 unsigned long val; 2830 2831 printf("%5lu.%06lu | ", secs, usecs); 2832 2833 print_graph_cpu(cpu); 2834 print_graph_proc(pid, comm); 2835 2836 printf(" | "); 2837 2838 if (latency_format) { 2839 print_lat_fmt(data, size); 2840 printf(" | "); 2841 } 2842 2843 field = find_field(event, "func"); 2844 if (!field) 2845 die("function entry does not have func field"); 2846 2847 val = read_size(data + field->offset, field->size); 2848 2849 /* 2850 * peek_data may unmap the data pointer. Copy it first. 2851 */ 2852 copy_data = malloc_or_die(size); 2853 memcpy(copy_data, data, size); 2854 data = copy_data; 2855 2856 rec = trace_peek_data(cpu); 2857 if (rec) { 2858 rec = get_return_for_leaf(cpu, pid, val, rec); 2859 if (rec) { 2860 print_graph_entry_leaf(event, data, rec); 2861 goto out_free; 2862 } 2863 } 2864 print_graph_nested(event, data); 2865 out_free: 2866 free(data); 2867 } 2868 2869 static void 2870 pretty_print_func_ret(void *data, int size __unused, struct event *event, 2871 int cpu, int pid, const char *comm, 2872 unsigned long secs, unsigned long usecs) 2873 { 2874 unsigned long long rettime, calltime; 2875 unsigned long long duration, depth; 2876 struct format_field *field; 2877 int i; 2878 2879 printf("%5lu.%06lu | ", secs, usecs); 2880 2881 print_graph_cpu(cpu); 2882 print_graph_proc(pid, comm); 2883 2884 printf(" | "); 2885 2886 if (latency_format) { 2887 print_lat_fmt(data, size); 2888 printf(" | "); 2889 } 2890 2891 field = find_field(event, "rettime"); 2892 if (!field) 2893 die("can't find rettime in return graph"); 2894 rettime = read_size(data + field->offset, field->size); 2895 2896 field = find_field(event, "calltime"); 2897 if (!field) 2898 die("can't find calltime in return graph"); 2899 calltime = read_size(data + field->offset, field->size); 2900 2901 duration = rettime - calltime; 2902 2903 /* Overhead */ 2904 print_graph_overhead(duration); 2905 2906 /* Duration */ 2907 print_graph_duration(duration); 2908 2909 field = find_field(event, "depth"); 2910 if (!field) 2911 die("can't find depth in entry graph"); 2912 depth = read_size(data + field->offset, field->size); 2913 2914 /* Function */ 2915 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2916 printf(" "); 2917 2918 printf("}"); 2919 } 2920 2921 static void 2922 pretty_print_func_graph(void *data, int size, struct event *event, 2923 int cpu, int pid, const char *comm, 2924 unsigned long secs, unsigned long usecs) 2925 { 2926 if (event->flags & EVENT_FL_ISFUNCENT) 2927 pretty_print_func_ent(data, size, event, 2928 cpu, pid, comm, secs, usecs); 2929 else if (event->flags & EVENT_FL_ISFUNCRET) 2930 pretty_print_func_ret(data, size, event, 2931 cpu, pid, comm, secs, usecs); 2932 printf("\n"); 2933 } 2934 2935 void print_event(int cpu, void *data, int size, unsigned long long nsecs, 2936 char *comm) 2937 { 2938 struct event *event; 2939 unsigned long secs; 2940 unsigned long usecs; 2941 int type; 2942 int pid; 2943 2944 secs = nsecs / NSECS_PER_SEC; 2945 nsecs -= secs * NSECS_PER_SEC; 2946 usecs = nsecs / NSECS_PER_USEC; 2947 2948 type = trace_parse_common_type(data); 2949 2950 event = trace_find_event(type); 2951 if (!event) { 2952 warning("ug! no event found for type %d", type); 2953 return; 2954 } 2955 2956 pid = parse_common_pid(data); 2957 2958 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET)) 2959 return pretty_print_func_graph(data, size, event, cpu, 2960 pid, comm, secs, usecs); 2961 2962 if (latency_format) { 2963 printf("%8.8s-%-5d %3d", 2964 comm, pid, cpu); 2965 print_lat_fmt(data, size); 2966 } else 2967 printf("%16s-%-5d [%03d]", comm, pid, cpu); 2968 2969 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name); 2970 2971 if (event->flags & EVENT_FL_FAILED) { 2972 printf("EVENT '%s' FAILED TO PARSE\n", 2973 event->name); 2974 return; 2975 } 2976 2977 pretty_print(data, size, event); 2978 printf("\n"); 2979 } 2980 2981 static void print_fields(struct print_flag_sym *field) 2982 { 2983 printf("{ %s, %s }", field->value, field->str); 2984 if (field->next) { 2985 printf(", "); 2986 print_fields(field->next); 2987 } 2988 } 2989 2990 static void print_args(struct print_arg *args) 2991 { 2992 int print_paren = 1; 2993 2994 switch (args->type) { 2995 case PRINT_NULL: 2996 printf("null"); 2997 break; 2998 case PRINT_ATOM: 2999 printf("%s", args->atom.atom); 3000 break; 3001 case PRINT_FIELD: 3002 printf("REC->%s", args->field.name); 3003 break; 3004 case PRINT_FLAGS: 3005 printf("__print_flags("); 3006 print_args(args->flags.field); 3007 printf(", %s, ", args->flags.delim); 3008 print_fields(args->flags.flags); 3009 printf(")"); 3010 break; 3011 case PRINT_SYMBOL: 3012 printf("__print_symbolic("); 3013 print_args(args->symbol.field); 3014 printf(", "); 3015 print_fields(args->symbol.symbols); 3016 printf(")"); 3017 break; 3018 case PRINT_STRING: 3019 printf("__get_str(%s)", args->string.string); 3020 break; 3021 case PRINT_TYPE: 3022 printf("(%s)", args->typecast.type); 3023 print_args(args->typecast.item); 3024 break; 3025 case PRINT_OP: 3026 if (strcmp(args->op.op, ":") == 0) 3027 print_paren = 0; 3028 if (print_paren) 3029 printf("("); 3030 print_args(args->op.left); 3031 printf(" %s ", args->op.op); 3032 print_args(args->op.right); 3033 if (print_paren) 3034 printf(")"); 3035 break; 3036 default: 3037 /* we should warn... */ 3038 return; 3039 } 3040 if (args->next) { 3041 printf("\n"); 3042 print_args(args->next); 3043 } 3044 } 3045 3046 static void parse_header_field(char *field, 3047 int *offset, int *size) 3048 { 3049 char *token; 3050 int type; 3051 3052 if (read_expected(EVENT_ITEM, (char *)"field") < 0) 3053 return; 3054 if (read_expected(EVENT_OP, (char *)":") < 0) 3055 return; 3056 3057 /* type */ 3058 if (read_expect_type(EVENT_ITEM, &token) < 0) 3059 goto fail; 3060 free_token(token); 3061 3062 if (read_expected(EVENT_ITEM, field) < 0) 3063 return; 3064 if (read_expected(EVENT_OP, (char *)";") < 0) 3065 return; 3066 if (read_expected(EVENT_ITEM, (char *)"offset") < 0) 3067 return; 3068 if (read_expected(EVENT_OP, (char *)":") < 0) 3069 return; 3070 if (read_expect_type(EVENT_ITEM, &token) < 0) 3071 goto fail; 3072 *offset = atoi(token); 3073 free_token(token); 3074 if (read_expected(EVENT_OP, (char *)";") < 0) 3075 return; 3076 if (read_expected(EVENT_ITEM, (char *)"size") < 0) 3077 return; 3078 if (read_expected(EVENT_OP, (char *)":") < 0) 3079 return; 3080 if (read_expect_type(EVENT_ITEM, &token) < 0) 3081 goto fail; 3082 *size = atoi(token); 3083 free_token(token); 3084 if (read_expected(EVENT_OP, (char *)";") < 0) 3085 return; 3086 type = read_token(&token); 3087 if (type != EVENT_NEWLINE) { 3088 /* newer versions of the kernel have a "signed" type */ 3089 if (type != EVENT_ITEM) 3090 goto fail; 3091 3092 if (strcmp(token, (char *)"signed") != 0) 3093 goto fail; 3094 3095 free_token(token); 3096 3097 if (read_expected(EVENT_OP, (char *)":") < 0) 3098 return; 3099 3100 if (read_expect_type(EVENT_ITEM, &token)) 3101 goto fail; 3102 3103 free_token(token); 3104 if (read_expected(EVENT_OP, (char *)";") < 0) 3105 return; 3106 3107 if (read_expect_type(EVENT_NEWLINE, &token)) 3108 goto fail; 3109 } 3110 fail: 3111 free_token(token); 3112 } 3113 3114 int parse_header_page(char *buf, unsigned long size) 3115 { 3116 init_input_buf(buf, size); 3117 3118 parse_header_field((char *)"timestamp", &header_page_ts_offset, 3119 &header_page_ts_size); 3120 parse_header_field((char *)"commit", &header_page_size_offset, 3121 &header_page_size_size); 3122 parse_header_field((char *)"data", &header_page_data_offset, 3123 &header_page_data_size); 3124 3125 return 0; 3126 } 3127 3128 int parse_ftrace_file(char *buf, unsigned long size) 3129 { 3130 struct format_field *field; 3131 struct print_arg *arg, **list; 3132 struct event *event; 3133 int ret; 3134 3135 init_input_buf(buf, size); 3136 3137 event = alloc_event(); 3138 if (!event) 3139 return -ENOMEM; 3140 3141 event->flags |= EVENT_FL_ISFTRACE; 3142 3143 event->name = event_read_name(); 3144 if (!event->name) 3145 die("failed to read ftrace event name"); 3146 3147 if (strcmp(event->name, "function") == 0) 3148 event->flags |= EVENT_FL_ISFUNC; 3149 3150 else if (strcmp(event->name, "funcgraph_entry") == 0) 3151 event->flags |= EVENT_FL_ISFUNCENT; 3152 3153 else if (strcmp(event->name, "funcgraph_exit") == 0) 3154 event->flags |= EVENT_FL_ISFUNCRET; 3155 3156 else if (strcmp(event->name, "bprint") == 0) 3157 event->flags |= EVENT_FL_ISBPRINT; 3158 3159 event->id = event_read_id(); 3160 if (event->id < 0) 3161 die("failed to read ftrace event id"); 3162 3163 add_event(event); 3164 3165 ret = event_read_format(event); 3166 if (ret < 0) 3167 die("failed to read ftrace event format"); 3168 3169 ret = event_read_print(event); 3170 if (ret < 0) 3171 die("failed to read ftrace event print fmt"); 3172 3173 /* New ftrace handles args */ 3174 if (ret > 0) 3175 return 0; 3176 /* 3177 * The arguments for ftrace files are parsed by the fields. 3178 * Set up the fields as their arguments. 3179 */ 3180 list = &event->print_fmt.args; 3181 for (field = event->format.fields; field; field = field->next) { 3182 arg = malloc_or_die(sizeof(*arg)); 3183 memset(arg, 0, sizeof(*arg)); 3184 *list = arg; 3185 list = &arg->next; 3186 arg->type = PRINT_FIELD; 3187 arg->field.name = field->name; 3188 arg->field.field = field; 3189 } 3190 return 0; 3191 } 3192 3193 int parse_event_file(char *buf, unsigned long size, char *sys) 3194 { 3195 struct event *event; 3196 int ret; 3197 3198 init_input_buf(buf, size); 3199 3200 event = alloc_event(); 3201 if (!event) 3202 return -ENOMEM; 3203 3204 event->name = event_read_name(); 3205 if (!event->name) 3206 die("failed to read event name"); 3207 3208 event->id = event_read_id(); 3209 if (event->id < 0) 3210 die("failed to read event id"); 3211 3212 ret = event_read_format(event); 3213 if (ret < 0) { 3214 warning("failed to read event format for %s", event->name); 3215 goto event_failed; 3216 } 3217 3218 ret = event_read_print(event); 3219 if (ret < 0) { 3220 warning("failed to read event print fmt for %s", event->name); 3221 goto event_failed; 3222 } 3223 3224 event->system = strdup(sys); 3225 3226 #define PRINT_ARGS 0 3227 if (PRINT_ARGS && event->print_fmt.args) 3228 print_args(event->print_fmt.args); 3229 3230 add_event(event); 3231 return 0; 3232 3233 event_failed: 3234 event->flags |= EVENT_FL_FAILED; 3235 /* still add it even if it failed */ 3236 add_event(event); 3237 return -1; 3238 } 3239 3240 void parse_set_info(int nr_cpus, int long_sz) 3241 { 3242 cpus = nr_cpus; 3243 long_size = long_sz; 3244 } 3245