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 static char *input_buf; 44 static unsigned long long input_buf_ptr; 45 static unsigned long long input_buf_siz; 46 47 static int cpus; 48 static int long_size; 49 50 static void init_input_buf(char *buf, unsigned long long size) 51 { 52 input_buf = buf; 53 input_buf_siz = size; 54 input_buf_ptr = 0; 55 } 56 57 struct cmdline { 58 char *comm; 59 int pid; 60 }; 61 62 static struct cmdline *cmdlines; 63 static int cmdline_count; 64 65 static int cmdline_cmp(const void *a, const void *b) 66 { 67 const struct cmdline *ca = a; 68 const struct cmdline *cb = b; 69 70 if (ca->pid < cb->pid) 71 return -1; 72 if (ca->pid > cb->pid) 73 return 1; 74 75 return 0; 76 } 77 78 void parse_cmdlines(char *file, int size __unused) 79 { 80 struct cmdline_list { 81 struct cmdline_list *next; 82 char *comm; 83 int pid; 84 } *list = NULL, *item; 85 char *line; 86 char *next = NULL; 87 int i; 88 89 line = strtok_r(file, "\n", &next); 90 while (line) { 91 item = malloc_or_die(sizeof(*item)); 92 sscanf(line, "%d %as", &item->pid, 93 (float *)(void *)&item->comm); /* workaround gcc warning */ 94 item->next = list; 95 list = item; 96 line = strtok_r(NULL, "\n", &next); 97 cmdline_count++; 98 } 99 100 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count); 101 102 i = 0; 103 while (list) { 104 cmdlines[i].pid = list->pid; 105 cmdlines[i].comm = list->comm; 106 i++; 107 item = list; 108 list = list->next; 109 free(item); 110 } 111 112 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp); 113 } 114 115 static struct func_map { 116 unsigned long long addr; 117 char *func; 118 char *mod; 119 } *func_list; 120 static unsigned int func_count; 121 122 static int func_cmp(const void *a, const void *b) 123 { 124 const struct func_map *fa = a; 125 const struct func_map *fb = b; 126 127 if (fa->addr < fb->addr) 128 return -1; 129 if (fa->addr > fb->addr) 130 return 1; 131 132 return 0; 133 } 134 135 void parse_proc_kallsyms(char *file, unsigned int size __unused) 136 { 137 struct func_list { 138 struct func_list *next; 139 unsigned long long addr; 140 char *func; 141 char *mod; 142 } *list = NULL, *item; 143 char *line; 144 char *next = NULL; 145 char *addr_str; 146 char ch; 147 int ret; 148 int i; 149 150 line = strtok_r(file, "\n", &next); 151 while (line) { 152 item = malloc_or_die(sizeof(*item)); 153 item->mod = NULL; 154 ret = sscanf(line, "%as %c %as\t[%as", 155 (float *)(void *)&addr_str, /* workaround gcc warning */ 156 &ch, 157 (float *)(void *)&item->func, 158 (float *)(void *)&item->mod); 159 item->addr = strtoull(addr_str, NULL, 16); 160 free(addr_str); 161 162 /* truncate the extra ']' */ 163 if (item->mod) 164 item->mod[strlen(item->mod) - 1] = 0; 165 166 167 item->next = list; 168 list = item; 169 line = strtok_r(NULL, "\n", &next); 170 func_count++; 171 } 172 173 func_list = malloc_or_die(sizeof(*func_list) * func_count + 1); 174 175 i = 0; 176 while (list) { 177 func_list[i].func = list->func; 178 func_list[i].addr = list->addr; 179 func_list[i].mod = list->mod; 180 i++; 181 item = list; 182 list = list->next; 183 free(item); 184 } 185 186 qsort(func_list, func_count, sizeof(*func_list), func_cmp); 187 188 /* 189 * Add a special record at the end. 190 */ 191 func_list[func_count].func = NULL; 192 func_list[func_count].addr = 0; 193 func_list[func_count].mod = NULL; 194 } 195 196 /* 197 * We are searching for a record in between, not an exact 198 * match. 199 */ 200 static int func_bcmp(const void *a, const void *b) 201 { 202 const struct func_map *fa = a; 203 const struct func_map *fb = b; 204 205 if ((fa->addr == fb->addr) || 206 207 (fa->addr > fb->addr && 208 fa->addr < (fb+1)->addr)) 209 return 0; 210 211 if (fa->addr < fb->addr) 212 return -1; 213 214 return 1; 215 } 216 217 static struct func_map *find_func(unsigned long long addr) 218 { 219 struct func_map *func; 220 struct func_map key; 221 222 key.addr = addr; 223 224 func = bsearch(&key, func_list, func_count, sizeof(*func_list), 225 func_bcmp); 226 227 return func; 228 } 229 230 void print_funcs(void) 231 { 232 int i; 233 234 for (i = 0; i < (int)func_count; i++) { 235 printf("%016llx %s", 236 func_list[i].addr, 237 func_list[i].func); 238 if (func_list[i].mod) 239 printf(" [%s]\n", func_list[i].mod); 240 else 241 printf("\n"); 242 } 243 } 244 245 static struct printk_map { 246 unsigned long long addr; 247 char *printk; 248 } *printk_list; 249 static unsigned int printk_count; 250 251 static int printk_cmp(const void *a, const void *b) 252 { 253 const struct func_map *fa = a; 254 const struct func_map *fb = b; 255 256 if (fa->addr < fb->addr) 257 return -1; 258 if (fa->addr > fb->addr) 259 return 1; 260 261 return 0; 262 } 263 264 static struct printk_map *find_printk(unsigned long long addr) 265 { 266 struct printk_map *printk; 267 struct printk_map key; 268 269 key.addr = addr; 270 271 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list), 272 printk_cmp); 273 274 return printk; 275 } 276 277 void parse_ftrace_printk(char *file, unsigned int size __unused) 278 { 279 struct printk_list { 280 struct printk_list *next; 281 unsigned long long addr; 282 char *printk; 283 } *list = NULL, *item; 284 char *line; 285 char *next = NULL; 286 char *addr_str; 287 int ret; 288 int i; 289 290 line = strtok_r(file, "\n", &next); 291 while (line) { 292 item = malloc_or_die(sizeof(*item)); 293 ret = sscanf(line, "%as : %as", 294 (float *)(void *)&addr_str, /* workaround gcc warning */ 295 (float *)(void *)&item->printk); 296 item->addr = strtoull(addr_str, NULL, 16); 297 free(addr_str); 298 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 0; 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 int trace_parse_common_type(void *data) 1934 { 1935 static int type_offset; 1936 static int type_size; 1937 int ret; 1938 1939 if (!type_size) { 1940 ret = get_common_info("common_type", 1941 &type_offset, 1942 &type_size); 1943 if (ret < 0) 1944 return ret; 1945 } 1946 return read_size(data + type_offset, type_size); 1947 } 1948 1949 static int parse_common_pid(void *data) 1950 { 1951 static int pid_offset; 1952 static int pid_size; 1953 int ret; 1954 1955 if (!pid_size) { 1956 ret = get_common_info("common_pid", 1957 &pid_offset, 1958 &pid_size); 1959 if (ret < 0) 1960 return ret; 1961 } 1962 1963 return read_size(data + pid_offset, pid_size); 1964 } 1965 1966 struct event *trace_find_event(int id) 1967 { 1968 struct event *event; 1969 1970 for (event = event_list; event; event = event->next) { 1971 if (event->id == id) 1972 break; 1973 } 1974 return event; 1975 } 1976 1977 static unsigned long long eval_num_arg(void *data, int size, 1978 struct event *event, struct print_arg *arg) 1979 { 1980 unsigned long long val = 0; 1981 unsigned long long left, right; 1982 struct print_arg *larg; 1983 1984 switch (arg->type) { 1985 case PRINT_NULL: 1986 /* ?? */ 1987 return 0; 1988 case PRINT_ATOM: 1989 return strtoull(arg->atom.atom, NULL, 0); 1990 case PRINT_FIELD: 1991 if (!arg->field.field) { 1992 arg->field.field = find_any_field(event, arg->field.name); 1993 if (!arg->field.field) 1994 die("field %s not found", arg->field.name); 1995 } 1996 /* must be a number */ 1997 val = read_size(data + arg->field.field->offset, 1998 arg->field.field->size); 1999 break; 2000 case PRINT_FLAGS: 2001 case PRINT_SYMBOL: 2002 break; 2003 case PRINT_TYPE: 2004 return eval_num_arg(data, size, event, arg->typecast.item); 2005 case PRINT_STRING: 2006 return 0; 2007 break; 2008 case PRINT_OP: 2009 if (strcmp(arg->op.op, "[") == 0) { 2010 /* 2011 * Arrays are special, since we don't want 2012 * to read the arg as is. 2013 */ 2014 if (arg->op.left->type != PRINT_FIELD) 2015 goto default_op; /* oops, all bets off */ 2016 larg = arg->op.left; 2017 if (!larg->field.field) { 2018 larg->field.field = 2019 find_any_field(event, larg->field.name); 2020 if (!larg->field.field) 2021 die("field %s not found", larg->field.name); 2022 } 2023 right = eval_num_arg(data, size, event, arg->op.right); 2024 val = read_size(data + larg->field.field->offset + 2025 right * long_size, long_size); 2026 break; 2027 } 2028 default_op: 2029 left = eval_num_arg(data, size, event, arg->op.left); 2030 right = eval_num_arg(data, size, event, arg->op.right); 2031 switch (arg->op.op[0]) { 2032 case '|': 2033 if (arg->op.op[1]) 2034 val = left || right; 2035 else 2036 val = left | right; 2037 break; 2038 case '&': 2039 if (arg->op.op[1]) 2040 val = left && right; 2041 else 2042 val = left & right; 2043 break; 2044 case '<': 2045 switch (arg->op.op[1]) { 2046 case 0: 2047 val = left < right; 2048 break; 2049 case '<': 2050 val = left << right; 2051 break; 2052 case '=': 2053 val = left <= right; 2054 break; 2055 default: 2056 die("unknown op '%s'", arg->op.op); 2057 } 2058 break; 2059 case '>': 2060 switch (arg->op.op[1]) { 2061 case 0: 2062 val = left > right; 2063 break; 2064 case '>': 2065 val = left >> right; 2066 break; 2067 case '=': 2068 val = left >= right; 2069 break; 2070 default: 2071 die("unknown op '%s'", arg->op.op); 2072 } 2073 break; 2074 case '=': 2075 if (arg->op.op[1] != '=') 2076 die("unknown op '%s'", arg->op.op); 2077 val = left == right; 2078 break; 2079 default: 2080 die("unknown op '%s'", arg->op.op); 2081 } 2082 break; 2083 default: /* not sure what to do there */ 2084 return 0; 2085 } 2086 return val; 2087 } 2088 2089 struct flag { 2090 const char *name; 2091 unsigned long long value; 2092 }; 2093 2094 static const struct flag flags[] = { 2095 { "HI_SOFTIRQ", 0 }, 2096 { "TIMER_SOFTIRQ", 1 }, 2097 { "NET_TX_SOFTIRQ", 2 }, 2098 { "NET_RX_SOFTIRQ", 3 }, 2099 { "BLOCK_SOFTIRQ", 4 }, 2100 { "BLOCK_IOPOLL_SOFTIRQ", 5 }, 2101 { "TASKLET_SOFTIRQ", 6 }, 2102 { "SCHED_SOFTIRQ", 7 }, 2103 { "HRTIMER_SOFTIRQ", 8 }, 2104 { "RCU_SOFTIRQ", 9 }, 2105 2106 { "HRTIMER_NORESTART", 0 }, 2107 { "HRTIMER_RESTART", 1 }, 2108 }; 2109 2110 static unsigned long long eval_flag(const char *flag) 2111 { 2112 int i; 2113 2114 /* 2115 * Some flags in the format files do not get converted. 2116 * If the flag is not numeric, see if it is something that 2117 * we already know about. 2118 */ 2119 if (isdigit(flag[0])) 2120 return strtoull(flag, NULL, 0); 2121 2122 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 2123 if (strcmp(flags[i].name, flag) == 0) 2124 return flags[i].value; 2125 2126 return 0; 2127 } 2128 2129 static void print_str_arg(void *data, int size, 2130 struct event *event, struct print_arg *arg) 2131 { 2132 struct print_flag_sym *flag; 2133 unsigned long long val, fval; 2134 char *str; 2135 int print; 2136 2137 switch (arg->type) { 2138 case PRINT_NULL: 2139 /* ?? */ 2140 return; 2141 case PRINT_ATOM: 2142 printf("%s", arg->atom.atom); 2143 return; 2144 case PRINT_FIELD: 2145 if (!arg->field.field) { 2146 arg->field.field = find_any_field(event, arg->field.name); 2147 if (!arg->field.field) 2148 die("field %s not found", arg->field.name); 2149 } 2150 str = malloc_or_die(arg->field.field->size + 1); 2151 memcpy(str, data + arg->field.field->offset, 2152 arg->field.field->size); 2153 str[arg->field.field->size] = 0; 2154 printf("%s", str); 2155 free(str); 2156 break; 2157 case PRINT_FLAGS: 2158 val = eval_num_arg(data, size, event, arg->flags.field); 2159 print = 0; 2160 for (flag = arg->flags.flags; flag; flag = flag->next) { 2161 fval = eval_flag(flag->value); 2162 if (!val && !fval) { 2163 printf("%s", flag->str); 2164 break; 2165 } 2166 if (fval && (val & fval) == fval) { 2167 if (print && arg->flags.delim) 2168 printf("%s", arg->flags.delim); 2169 printf("%s", flag->str); 2170 print = 1; 2171 val &= ~fval; 2172 } 2173 } 2174 break; 2175 case PRINT_SYMBOL: 2176 val = eval_num_arg(data, size, event, arg->symbol.field); 2177 for (flag = arg->symbol.symbols; flag; flag = flag->next) { 2178 fval = eval_flag(flag->value); 2179 if (val == fval) { 2180 printf("%s", flag->str); 2181 break; 2182 } 2183 } 2184 break; 2185 2186 case PRINT_TYPE: 2187 break; 2188 case PRINT_STRING: { 2189 int str_offset; 2190 2191 if (arg->string.offset == -1) { 2192 struct format_field *f; 2193 2194 f = find_any_field(event, arg->string.string); 2195 arg->string.offset = f->offset; 2196 } 2197 str_offset = *(int *)(data + arg->string.offset); 2198 str_offset &= 0xffff; 2199 printf("%s", ((char *)data) + str_offset); 2200 break; 2201 } 2202 case PRINT_OP: 2203 /* 2204 * The only op for string should be ? : 2205 */ 2206 if (arg->op.op[0] != '?') 2207 return; 2208 val = eval_num_arg(data, size, event, arg->op.left); 2209 if (val) 2210 print_str_arg(data, size, event, arg->op.right->op.left); 2211 else 2212 print_str_arg(data, size, event, arg->op.right->op.right); 2213 break; 2214 default: 2215 /* well... */ 2216 break; 2217 } 2218 } 2219 2220 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event) 2221 { 2222 static struct format_field *field, *ip_field; 2223 struct print_arg *args, *arg, **next; 2224 unsigned long long ip, val; 2225 char *ptr; 2226 void *bptr; 2227 2228 if (!field) { 2229 field = find_field(event, "buf"); 2230 if (!field) 2231 die("can't find buffer field for binary printk"); 2232 ip_field = find_field(event, "ip"); 2233 if (!ip_field) 2234 die("can't find ip field for binary printk"); 2235 } 2236 2237 ip = read_size(data + ip_field->offset, ip_field->size); 2238 2239 /* 2240 * The first arg is the IP pointer. 2241 */ 2242 args = malloc_or_die(sizeof(*args)); 2243 arg = args; 2244 arg->next = NULL; 2245 next = &arg->next; 2246 2247 arg->type = PRINT_ATOM; 2248 arg->atom.atom = malloc_or_die(32); 2249 sprintf(arg->atom.atom, "%lld", ip); 2250 2251 /* skip the first "%pf : " */ 2252 for (ptr = fmt + 6, bptr = data + field->offset; 2253 bptr < data + size && *ptr; ptr++) { 2254 int ls = 0; 2255 2256 if (*ptr == '%') { 2257 process_again: 2258 ptr++; 2259 switch (*ptr) { 2260 case '%': 2261 break; 2262 case 'l': 2263 ls++; 2264 goto process_again; 2265 case 'L': 2266 ls = 2; 2267 goto process_again; 2268 case '0' ... '9': 2269 goto process_again; 2270 case 'p': 2271 ls = 1; 2272 /* fall through */ 2273 case 'd': 2274 case 'u': 2275 case 'x': 2276 case 'i': 2277 bptr = (void *)(((unsigned long)bptr + (long_size - 1)) & 2278 ~(long_size - 1)); 2279 switch (ls) { 2280 case 0: 2281 case 1: 2282 ls = long_size; 2283 break; 2284 case 2: 2285 ls = 8; 2286 default: 2287 break; 2288 } 2289 val = read_size(bptr, ls); 2290 bptr += ls; 2291 arg = malloc_or_die(sizeof(*arg)); 2292 arg->next = NULL; 2293 arg->type = PRINT_ATOM; 2294 arg->atom.atom = malloc_or_die(32); 2295 sprintf(arg->atom.atom, "%lld", val); 2296 *next = arg; 2297 next = &arg->next; 2298 break; 2299 case 's': 2300 arg = malloc_or_die(sizeof(*arg)); 2301 arg->next = NULL; 2302 arg->type = PRINT_STRING; 2303 arg->string.string = strdup(bptr); 2304 bptr += strlen(bptr) + 1; 2305 *next = arg; 2306 next = &arg->next; 2307 default: 2308 break; 2309 } 2310 } 2311 } 2312 2313 return args; 2314 } 2315 2316 static void free_args(struct print_arg *args) 2317 { 2318 struct print_arg *next; 2319 2320 while (args) { 2321 next = args->next; 2322 2323 if (args->type == PRINT_ATOM) 2324 free(args->atom.atom); 2325 else 2326 free(args->string.string); 2327 free(args); 2328 args = next; 2329 } 2330 } 2331 2332 static char *get_bprint_format(void *data, int size __unused, struct event *event) 2333 { 2334 unsigned long long addr; 2335 static struct format_field *field; 2336 struct printk_map *printk; 2337 char *format; 2338 char *p; 2339 2340 if (!field) { 2341 field = find_field(event, "fmt"); 2342 if (!field) 2343 die("can't find format field for binary printk"); 2344 printf("field->offset = %d size=%d\n", field->offset, field->size); 2345 } 2346 2347 addr = read_size(data + field->offset, field->size); 2348 2349 printk = find_printk(addr); 2350 if (!printk) { 2351 format = malloc_or_die(45); 2352 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n", 2353 addr); 2354 return format; 2355 } 2356 2357 p = printk->printk; 2358 /* Remove any quotes. */ 2359 if (*p == '"') 2360 p++; 2361 format = malloc_or_die(strlen(p) + 10); 2362 sprintf(format, "%s : %s", "%pf", p); 2363 /* remove ending quotes and new line since we will add one too */ 2364 p = format + strlen(format) - 1; 2365 if (*p == '"') 2366 *p = 0; 2367 2368 p -= 2; 2369 if (strcmp(p, "\\n") == 0) 2370 *p = 0; 2371 2372 return format; 2373 } 2374 2375 static void pretty_print(void *data, int size, struct event *event) 2376 { 2377 struct print_fmt *print_fmt = &event->print_fmt; 2378 struct print_arg *arg = print_fmt->args; 2379 struct print_arg *args = NULL; 2380 const char *ptr = print_fmt->format; 2381 unsigned long long val; 2382 struct func_map *func; 2383 const char *saveptr; 2384 char *bprint_fmt = NULL; 2385 char format[32]; 2386 int show_func; 2387 int len; 2388 int ls; 2389 2390 if (event->flags & EVENT_FL_ISFUNC) 2391 ptr = " %pF <-- %pF"; 2392 2393 if (event->flags & EVENT_FL_ISBPRINT) { 2394 bprint_fmt = get_bprint_format(data, size, event); 2395 args = make_bprint_args(bprint_fmt, data, size, event); 2396 arg = args; 2397 ptr = bprint_fmt; 2398 } 2399 2400 for (; *ptr; ptr++) { 2401 ls = 0; 2402 if (*ptr == '\\') { 2403 ptr++; 2404 switch (*ptr) { 2405 case 'n': 2406 printf("\n"); 2407 break; 2408 case 't': 2409 printf("\t"); 2410 break; 2411 case 'r': 2412 printf("\r"); 2413 break; 2414 case '\\': 2415 printf("\\"); 2416 break; 2417 default: 2418 printf("%c", *ptr); 2419 break; 2420 } 2421 2422 } else if (*ptr == '%') { 2423 saveptr = ptr; 2424 show_func = 0; 2425 cont_process: 2426 ptr++; 2427 switch (*ptr) { 2428 case '%': 2429 printf("%%"); 2430 break; 2431 case 'l': 2432 ls++; 2433 goto cont_process; 2434 case 'L': 2435 ls = 2; 2436 goto cont_process; 2437 case 'z': 2438 case 'Z': 2439 case '0' ... '9': 2440 goto cont_process; 2441 case 'p': 2442 if (long_size == 4) 2443 ls = 1; 2444 else 2445 ls = 2; 2446 2447 if (*(ptr+1) == 'F' || 2448 *(ptr+1) == 'f') { 2449 ptr++; 2450 show_func = *ptr; 2451 } 2452 2453 /* fall through */ 2454 case 'd': 2455 case 'i': 2456 case 'x': 2457 case 'X': 2458 case 'u': 2459 if (!arg) 2460 die("no argument match"); 2461 2462 len = ((unsigned long)ptr + 1) - 2463 (unsigned long)saveptr; 2464 2465 /* should never happen */ 2466 if (len > 32) 2467 die("bad format!"); 2468 2469 memcpy(format, saveptr, len); 2470 format[len] = 0; 2471 2472 val = eval_num_arg(data, size, event, arg); 2473 arg = arg->next; 2474 2475 if (show_func) { 2476 func = find_func(val); 2477 if (func) { 2478 printf("%s", func->func); 2479 if (show_func == 'F') 2480 printf("+0x%llx", 2481 val - func->addr); 2482 break; 2483 } 2484 } 2485 switch (ls) { 2486 case 0: 2487 printf(format, (int)val); 2488 break; 2489 case 1: 2490 printf(format, (long)val); 2491 break; 2492 case 2: 2493 printf(format, (long long)val); 2494 break; 2495 default: 2496 die("bad count (%d)", ls); 2497 } 2498 break; 2499 case 's': 2500 if (!arg) 2501 die("no matching argument"); 2502 2503 print_str_arg(data, size, event, arg); 2504 arg = arg->next; 2505 break; 2506 default: 2507 printf(">%c<", *ptr); 2508 2509 } 2510 } else 2511 printf("%c", *ptr); 2512 } 2513 2514 if (args) { 2515 free_args(args); 2516 free(bprint_fmt); 2517 } 2518 } 2519 2520 static inline int log10_cpu(int nb) 2521 { 2522 if (nb / 100) 2523 return 3; 2524 if (nb / 10) 2525 return 2; 2526 return 1; 2527 } 2528 2529 /* taken from Linux, written by Frederic Weisbecker */ 2530 static void print_graph_cpu(int cpu) 2531 { 2532 int i; 2533 int log10_this = log10_cpu(cpu); 2534 int log10_all = log10_cpu(cpus); 2535 2536 2537 /* 2538 * Start with a space character - to make it stand out 2539 * to the right a bit when trace output is pasted into 2540 * email: 2541 */ 2542 printf(" "); 2543 2544 /* 2545 * Tricky - we space the CPU field according to the max 2546 * number of online CPUs. On a 2-cpu system it would take 2547 * a maximum of 1 digit - on a 128 cpu system it would 2548 * take up to 3 digits: 2549 */ 2550 for (i = 0; i < log10_all - log10_this; i++) 2551 printf(" "); 2552 2553 printf("%d) ", cpu); 2554 } 2555 2556 #define TRACE_GRAPH_PROCINFO_LENGTH 14 2557 #define TRACE_GRAPH_INDENT 2 2558 2559 static void print_graph_proc(int pid, const char *comm) 2560 { 2561 /* sign + log10(MAX_INT) + '\0' */ 2562 char pid_str[11]; 2563 int spaces = 0; 2564 int len; 2565 int i; 2566 2567 sprintf(pid_str, "%d", pid); 2568 2569 /* 1 stands for the "-" character */ 2570 len = strlen(comm) + strlen(pid_str) + 1; 2571 2572 if (len < TRACE_GRAPH_PROCINFO_LENGTH) 2573 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; 2574 2575 /* First spaces to align center */ 2576 for (i = 0; i < spaces / 2; i++) 2577 printf(" "); 2578 2579 printf("%s-%s", comm, pid_str); 2580 2581 /* Last spaces to align center */ 2582 for (i = 0; i < spaces - (spaces / 2); i++) 2583 printf(" "); 2584 } 2585 2586 static struct record * 2587 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func, 2588 struct record *next) 2589 { 2590 struct format_field *field; 2591 struct event *event; 2592 unsigned long val; 2593 int type; 2594 int pid; 2595 2596 type = trace_parse_common_type(next->data); 2597 event = trace_find_event(type); 2598 if (!event) 2599 return NULL; 2600 2601 if (!(event->flags & EVENT_FL_ISFUNCRET)) 2602 return NULL; 2603 2604 pid = parse_common_pid(next->data); 2605 field = find_field(event, "func"); 2606 if (!field) 2607 die("function return does not have field func"); 2608 2609 val = read_size(next->data + field->offset, field->size); 2610 2611 if (cur_pid != pid || cur_func != val) 2612 return NULL; 2613 2614 /* this is a leaf, now advance the iterator */ 2615 return trace_read_data(cpu); 2616 } 2617 2618 /* Signal a overhead of time execution to the output */ 2619 static void print_graph_overhead(unsigned long long duration) 2620 { 2621 /* Non nested entry or return */ 2622 if (duration == ~0ULL) 2623 return (void)printf(" "); 2624 2625 /* Duration exceeded 100 msecs */ 2626 if (duration > 100000ULL) 2627 return (void)printf("! "); 2628 2629 /* Duration exceeded 10 msecs */ 2630 if (duration > 10000ULL) 2631 return (void)printf("+ "); 2632 2633 printf(" "); 2634 } 2635 2636 static void print_graph_duration(unsigned long long duration) 2637 { 2638 unsigned long usecs = duration / 1000; 2639 unsigned long nsecs_rem = duration % 1000; 2640 /* log10(ULONG_MAX) + '\0' */ 2641 char msecs_str[21]; 2642 char nsecs_str[5]; 2643 int len; 2644 int i; 2645 2646 sprintf(msecs_str, "%lu", usecs); 2647 2648 /* Print msecs */ 2649 len = printf("%lu", usecs); 2650 2651 /* Print nsecs (we don't want to exceed 7 numbers) */ 2652 if (len < 7) { 2653 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); 2654 len += printf(".%s", nsecs_str); 2655 } 2656 2657 printf(" us "); 2658 2659 /* Print remaining spaces to fit the row's width */ 2660 for (i = len; i < 7; i++) 2661 printf(" "); 2662 2663 printf("| "); 2664 } 2665 2666 static void 2667 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec) 2668 { 2669 unsigned long long rettime, calltime; 2670 unsigned long long duration, depth; 2671 unsigned long long val; 2672 struct format_field *field; 2673 struct func_map *func; 2674 struct event *ret_event; 2675 int type; 2676 int i; 2677 2678 type = trace_parse_common_type(ret_rec->data); 2679 ret_event = trace_find_event(type); 2680 2681 field = find_field(ret_event, "rettime"); 2682 if (!field) 2683 die("can't find rettime in return graph"); 2684 rettime = read_size(ret_rec->data + field->offset, field->size); 2685 2686 field = find_field(ret_event, "calltime"); 2687 if (!field) 2688 die("can't find rettime in return graph"); 2689 calltime = read_size(ret_rec->data + field->offset, field->size); 2690 2691 duration = rettime - calltime; 2692 2693 /* Overhead */ 2694 print_graph_overhead(duration); 2695 2696 /* Duration */ 2697 print_graph_duration(duration); 2698 2699 field = find_field(event, "depth"); 2700 if (!field) 2701 die("can't find depth in entry graph"); 2702 depth = read_size(data + field->offset, field->size); 2703 2704 /* Function */ 2705 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2706 printf(" "); 2707 2708 field = find_field(event, "func"); 2709 if (!field) 2710 die("can't find func in entry graph"); 2711 val = read_size(data + field->offset, field->size); 2712 func = find_func(val); 2713 2714 if (func) 2715 printf("%s();", func->func); 2716 else 2717 printf("%llx();", val); 2718 } 2719 2720 static void print_graph_nested(struct event *event, void *data) 2721 { 2722 struct format_field *field; 2723 unsigned long long depth; 2724 unsigned long long val; 2725 struct func_map *func; 2726 int i; 2727 2728 /* No overhead */ 2729 print_graph_overhead(-1); 2730 2731 /* No time */ 2732 printf(" | "); 2733 2734 field = find_field(event, "depth"); 2735 if (!field) 2736 die("can't find depth in entry graph"); 2737 depth = read_size(data + field->offset, field->size); 2738 2739 /* Function */ 2740 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2741 printf(" "); 2742 2743 field = find_field(event, "func"); 2744 if (!field) 2745 die("can't find func in entry graph"); 2746 val = read_size(data + field->offset, field->size); 2747 func = find_func(val); 2748 2749 if (func) 2750 printf("%s() {", func->func); 2751 else 2752 printf("%llx() {", val); 2753 } 2754 2755 static void 2756 pretty_print_func_ent(void *data, int size, struct event *event, 2757 int cpu, int pid, const char *comm, 2758 unsigned long secs, unsigned long usecs) 2759 { 2760 struct format_field *field; 2761 struct record *rec; 2762 void *copy_data; 2763 unsigned long val; 2764 2765 printf("%5lu.%06lu | ", secs, usecs); 2766 2767 print_graph_cpu(cpu); 2768 print_graph_proc(pid, comm); 2769 2770 printf(" | "); 2771 2772 field = find_field(event, "func"); 2773 if (!field) 2774 die("function entry does not have func field"); 2775 2776 val = read_size(data + field->offset, field->size); 2777 2778 /* 2779 * peek_data may unmap the data pointer. Copy it first. 2780 */ 2781 copy_data = malloc_or_die(size); 2782 memcpy(copy_data, data, size); 2783 data = copy_data; 2784 2785 rec = trace_peek_data(cpu); 2786 if (rec) { 2787 rec = get_return_for_leaf(cpu, pid, val, rec); 2788 if (rec) { 2789 print_graph_entry_leaf(event, data, rec); 2790 goto out_free; 2791 } 2792 } 2793 print_graph_nested(event, data); 2794 out_free: 2795 free(data); 2796 } 2797 2798 static void 2799 pretty_print_func_ret(void *data, int size __unused, struct event *event, 2800 int cpu, int pid, const char *comm, 2801 unsigned long secs, unsigned long usecs) 2802 { 2803 unsigned long long rettime, calltime; 2804 unsigned long long duration, depth; 2805 struct format_field *field; 2806 int i; 2807 2808 printf("%5lu.%06lu | ", secs, usecs); 2809 2810 print_graph_cpu(cpu); 2811 print_graph_proc(pid, comm); 2812 2813 printf(" | "); 2814 2815 field = find_field(event, "rettime"); 2816 if (!field) 2817 die("can't find rettime in return graph"); 2818 rettime = read_size(data + field->offset, field->size); 2819 2820 field = find_field(event, "calltime"); 2821 if (!field) 2822 die("can't find calltime in return graph"); 2823 calltime = read_size(data + field->offset, field->size); 2824 2825 duration = rettime - calltime; 2826 2827 /* Overhead */ 2828 print_graph_overhead(duration); 2829 2830 /* Duration */ 2831 print_graph_duration(duration); 2832 2833 field = find_field(event, "depth"); 2834 if (!field) 2835 die("can't find depth in entry graph"); 2836 depth = read_size(data + field->offset, field->size); 2837 2838 /* Function */ 2839 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2840 printf(" "); 2841 2842 printf("}"); 2843 } 2844 2845 static void 2846 pretty_print_func_graph(void *data, int size, struct event *event, 2847 int cpu, int pid, const char *comm, 2848 unsigned long secs, unsigned long usecs) 2849 { 2850 if (event->flags & EVENT_FL_ISFUNCENT) 2851 pretty_print_func_ent(data, size, event, 2852 cpu, pid, comm, secs, usecs); 2853 else if (event->flags & EVENT_FL_ISFUNCRET) 2854 pretty_print_func_ret(data, size, event, 2855 cpu, pid, comm, secs, usecs); 2856 printf("\n"); 2857 } 2858 2859 void print_event(int cpu, void *data, int size, unsigned long long nsecs, 2860 char *comm) 2861 { 2862 struct event *event; 2863 unsigned long secs; 2864 unsigned long usecs; 2865 int type; 2866 int pid; 2867 2868 secs = nsecs / NSECS_PER_SEC; 2869 nsecs -= secs * NSECS_PER_SEC; 2870 usecs = nsecs / NSECS_PER_USEC; 2871 2872 type = trace_parse_common_type(data); 2873 2874 event = trace_find_event(type); 2875 if (!event) { 2876 warning("ug! no event found for type %d", type); 2877 return; 2878 } 2879 2880 pid = parse_common_pid(data); 2881 2882 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET)) 2883 return pretty_print_func_graph(data, size, event, cpu, 2884 pid, comm, secs, usecs); 2885 2886 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ", 2887 comm, pid, cpu, 2888 secs, nsecs, event->name); 2889 2890 if (event->flags & EVENT_FL_FAILED) { 2891 printf("EVENT '%s' FAILED TO PARSE\n", 2892 event->name); 2893 return; 2894 } 2895 2896 pretty_print(data, size, event); 2897 printf("\n"); 2898 } 2899 2900 static void print_fields(struct print_flag_sym *field) 2901 { 2902 printf("{ %s, %s }", field->value, field->str); 2903 if (field->next) { 2904 printf(", "); 2905 print_fields(field->next); 2906 } 2907 } 2908 2909 static void print_args(struct print_arg *args) 2910 { 2911 int print_paren = 1; 2912 2913 switch (args->type) { 2914 case PRINT_NULL: 2915 printf("null"); 2916 break; 2917 case PRINT_ATOM: 2918 printf("%s", args->atom.atom); 2919 break; 2920 case PRINT_FIELD: 2921 printf("REC->%s", args->field.name); 2922 break; 2923 case PRINT_FLAGS: 2924 printf("__print_flags("); 2925 print_args(args->flags.field); 2926 printf(", %s, ", args->flags.delim); 2927 print_fields(args->flags.flags); 2928 printf(")"); 2929 break; 2930 case PRINT_SYMBOL: 2931 printf("__print_symbolic("); 2932 print_args(args->symbol.field); 2933 printf(", "); 2934 print_fields(args->symbol.symbols); 2935 printf(")"); 2936 break; 2937 case PRINT_STRING: 2938 printf("__get_str(%s)", args->string.string); 2939 break; 2940 case PRINT_TYPE: 2941 printf("(%s)", args->typecast.type); 2942 print_args(args->typecast.item); 2943 break; 2944 case PRINT_OP: 2945 if (strcmp(args->op.op, ":") == 0) 2946 print_paren = 0; 2947 if (print_paren) 2948 printf("("); 2949 print_args(args->op.left); 2950 printf(" %s ", args->op.op); 2951 print_args(args->op.right); 2952 if (print_paren) 2953 printf(")"); 2954 break; 2955 default: 2956 /* we should warn... */ 2957 return; 2958 } 2959 if (args->next) { 2960 printf("\n"); 2961 print_args(args->next); 2962 } 2963 } 2964 2965 static void parse_header_field(char *field, 2966 int *offset, int *size) 2967 { 2968 char *token; 2969 int type; 2970 2971 if (read_expected(EVENT_ITEM, (char *)"field") < 0) 2972 return; 2973 if (read_expected(EVENT_OP, (char *)":") < 0) 2974 return; 2975 2976 /* type */ 2977 if (read_expect_type(EVENT_ITEM, &token) < 0) 2978 goto fail; 2979 free_token(token); 2980 2981 if (read_expected(EVENT_ITEM, field) < 0) 2982 return; 2983 if (read_expected(EVENT_OP, (char *)";") < 0) 2984 return; 2985 if (read_expected(EVENT_ITEM, (char *)"offset") < 0) 2986 return; 2987 if (read_expected(EVENT_OP, (char *)":") < 0) 2988 return; 2989 if (read_expect_type(EVENT_ITEM, &token) < 0) 2990 goto fail; 2991 *offset = atoi(token); 2992 free_token(token); 2993 if (read_expected(EVENT_OP, (char *)";") < 0) 2994 return; 2995 if (read_expected(EVENT_ITEM, (char *)"size") < 0) 2996 return; 2997 if (read_expected(EVENT_OP, (char *)":") < 0) 2998 return; 2999 if (read_expect_type(EVENT_ITEM, &token) < 0) 3000 goto fail; 3001 *size = atoi(token); 3002 free_token(token); 3003 if (read_expected(EVENT_OP, (char *)";") < 0) 3004 return; 3005 type = read_token(&token); 3006 if (type != EVENT_NEWLINE) { 3007 /* newer versions of the kernel have a "signed" type */ 3008 if (type != EVENT_ITEM) 3009 goto fail; 3010 3011 if (strcmp(token, (char *)"signed") != 0) 3012 goto fail; 3013 3014 free_token(token); 3015 3016 if (read_expected(EVENT_OP, (char *)":") < 0) 3017 return; 3018 3019 if (read_expect_type(EVENT_ITEM, &token)) 3020 goto fail; 3021 3022 free_token(token); 3023 if (read_expected(EVENT_OP, (char *)";") < 0) 3024 return; 3025 3026 if (read_expect_type(EVENT_NEWLINE, &token)) 3027 goto fail; 3028 } 3029 fail: 3030 free_token(token); 3031 } 3032 3033 int parse_header_page(char *buf, unsigned long size) 3034 { 3035 init_input_buf(buf, size); 3036 3037 parse_header_field((char *)"timestamp", &header_page_ts_offset, 3038 &header_page_ts_size); 3039 parse_header_field((char *)"commit", &header_page_size_offset, 3040 &header_page_size_size); 3041 parse_header_field((char *)"data", &header_page_data_offset, 3042 &header_page_data_size); 3043 3044 return 0; 3045 } 3046 3047 int parse_ftrace_file(char *buf, unsigned long size) 3048 { 3049 struct format_field *field; 3050 struct print_arg *arg, **list; 3051 struct event *event; 3052 int ret; 3053 3054 init_input_buf(buf, size); 3055 3056 event = alloc_event(); 3057 if (!event) 3058 return -ENOMEM; 3059 3060 event->flags |= EVENT_FL_ISFTRACE; 3061 3062 event->name = event_read_name(); 3063 if (!event->name) 3064 die("failed to read ftrace event name"); 3065 3066 if (strcmp(event->name, "function") == 0) 3067 event->flags |= EVENT_FL_ISFUNC; 3068 3069 else if (strcmp(event->name, "funcgraph_entry") == 0) 3070 event->flags |= EVENT_FL_ISFUNCENT; 3071 3072 else if (strcmp(event->name, "funcgraph_exit") == 0) 3073 event->flags |= EVENT_FL_ISFUNCRET; 3074 3075 else if (strcmp(event->name, "bprint") == 0) 3076 event->flags |= EVENT_FL_ISBPRINT; 3077 3078 event->id = event_read_id(); 3079 if (event->id < 0) 3080 die("failed to read ftrace event id"); 3081 3082 add_event(event); 3083 3084 ret = event_read_format(event); 3085 if (ret < 0) 3086 die("failed to read ftrace event format"); 3087 3088 ret = event_read_print(event); 3089 if (ret < 0) 3090 die("failed to read ftrace event print fmt"); 3091 3092 /* 3093 * The arguments for ftrace files are parsed by the fields. 3094 * Set up the fields as their arguments. 3095 */ 3096 list = &event->print_fmt.args; 3097 for (field = event->format.fields; field; field = field->next) { 3098 arg = malloc_or_die(sizeof(*arg)); 3099 memset(arg, 0, sizeof(*arg)); 3100 *list = arg; 3101 list = &arg->next; 3102 arg->type = PRINT_FIELD; 3103 arg->field.name = field->name; 3104 arg->field.field = field; 3105 } 3106 return 0; 3107 } 3108 3109 int parse_event_file(char *buf, unsigned long size, char *sys) 3110 { 3111 struct event *event; 3112 int ret; 3113 3114 init_input_buf(buf, size); 3115 3116 event = alloc_event(); 3117 if (!event) 3118 return -ENOMEM; 3119 3120 event->name = event_read_name(); 3121 if (!event->name) 3122 die("failed to read event name"); 3123 3124 event->id = event_read_id(); 3125 if (event->id < 0) 3126 die("failed to read event id"); 3127 3128 ret = event_read_format(event); 3129 if (ret < 0) { 3130 warning("failed to read event format for %s", event->name); 3131 goto event_failed; 3132 } 3133 3134 ret = event_read_print(event); 3135 if (ret < 0) { 3136 warning("failed to read event print fmt for %s", event->name); 3137 goto event_failed; 3138 } 3139 3140 event->system = strdup(sys); 3141 3142 #define PRINT_ARGS 0 3143 if (PRINT_ARGS && event->print_fmt.args) 3144 print_args(event->print_fmt.args); 3145 3146 add_event(event); 3147 return 0; 3148 3149 event_failed: 3150 event->flags |= EVENT_FL_FAILED; 3151 /* still add it even if it failed */ 3152 add_event(event); 3153 return -1; 3154 } 3155 3156 void parse_set_info(int nr_cpus, int long_sz) 3157 { 3158 cpus = nr_cpus; 3159 long_size = long_sz; 3160 } 3161