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, "name") < 0) 689 return NULL; 690 691 if (read_expected(EVENT_OP, ":") < 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, "ID") < 0) 710 return -1; 711 712 if (read_expected(EVENT_OP, ":") < 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, "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, ":") < 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, ";")) 896 goto fail; 897 free_token(token); 898 899 if (read_expected(EVENT_ITEM, "offset") < 0) 900 goto fail_expect; 901 902 if (read_expected(EVENT_OP, ":") < 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, ";") < 0) 911 goto fail_expect; 912 913 if (read_expected(EVENT_ITEM, "size") < 0) 914 goto fail_expect; 915 916 if (read_expected(EVENT_OP, ":") < 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, ";") < 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, "signed")) 931 goto fail; 932 933 free_token(token); 934 935 if (read_expected(EVENT_OP, ":") < 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, ";") < 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, "format") < 0) 974 return -1; 975 976 if (read_expected(EVENT_OP, ":") < 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, ":")) 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, "]")) 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, "->") < 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, "{")) 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, ",")) 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, "}")) 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, "(") < 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, ",")) 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, ",")) 1521 goto out_free; 1522 1523 type = process_fields(event, &arg->flags.flags, &token); 1524 if (test_type_token(type, token, EVENT_DELIM, ")")) 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, "(") < 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, ",")) 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, ")")) 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, ")")) { 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, "(") < 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, ")") < 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, "print") < 0) 1779 return -1; 1780 1781 if (read_expected(EVENT_ITEM, "fmt") < 0) 1782 return -1; 1783 1784 if (read_expected(EVENT_OP, ":") < 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, ",")) 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 const 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 "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 "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 "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 "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 "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 case '-': 2110 val = left - right; 2111 break; 2112 case '+': 2113 val = left + right; 2114 break; 2115 default: 2116 die("unknown op '%s'", arg->op.op); 2117 } 2118 break; 2119 default: /* not sure what to do there */ 2120 return 0; 2121 } 2122 return val; 2123 } 2124 2125 struct flag { 2126 const char *name; 2127 unsigned long long value; 2128 }; 2129 2130 static const struct flag flags[] = { 2131 { "HI_SOFTIRQ", 0 }, 2132 { "TIMER_SOFTIRQ", 1 }, 2133 { "NET_TX_SOFTIRQ", 2 }, 2134 { "NET_RX_SOFTIRQ", 3 }, 2135 { "BLOCK_SOFTIRQ", 4 }, 2136 { "BLOCK_IOPOLL_SOFTIRQ", 5 }, 2137 { "TASKLET_SOFTIRQ", 6 }, 2138 { "SCHED_SOFTIRQ", 7 }, 2139 { "HRTIMER_SOFTIRQ", 8 }, 2140 { "RCU_SOFTIRQ", 9 }, 2141 2142 { "HRTIMER_NORESTART", 0 }, 2143 { "HRTIMER_RESTART", 1 }, 2144 }; 2145 2146 static unsigned long long eval_flag(const char *flag) 2147 { 2148 int i; 2149 2150 /* 2151 * Some flags in the format files do not get converted. 2152 * If the flag is not numeric, see if it is something that 2153 * we already know about. 2154 */ 2155 if (isdigit(flag[0])) 2156 return strtoull(flag, NULL, 0); 2157 2158 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 2159 if (strcmp(flags[i].name, flag) == 0) 2160 return flags[i].value; 2161 2162 return 0; 2163 } 2164 2165 static void print_str_arg(void *data, int size, 2166 struct event *event, struct print_arg *arg) 2167 { 2168 struct print_flag_sym *flag; 2169 unsigned long long val, fval; 2170 char *str; 2171 int print; 2172 2173 switch (arg->type) { 2174 case PRINT_NULL: 2175 /* ?? */ 2176 return; 2177 case PRINT_ATOM: 2178 printf("%s", arg->atom.atom); 2179 return; 2180 case PRINT_FIELD: 2181 if (!arg->field.field) { 2182 arg->field.field = find_any_field(event, arg->field.name); 2183 if (!arg->field.field) 2184 die("field %s not found", arg->field.name); 2185 } 2186 str = malloc_or_die(arg->field.field->size + 1); 2187 memcpy(str, data + arg->field.field->offset, 2188 arg->field.field->size); 2189 str[arg->field.field->size] = 0; 2190 printf("%s", str); 2191 free(str); 2192 break; 2193 case PRINT_FLAGS: 2194 val = eval_num_arg(data, size, event, arg->flags.field); 2195 print = 0; 2196 for (flag = arg->flags.flags; flag; flag = flag->next) { 2197 fval = eval_flag(flag->value); 2198 if (!val && !fval) { 2199 printf("%s", flag->str); 2200 break; 2201 } 2202 if (fval && (val & fval) == fval) { 2203 if (print && arg->flags.delim) 2204 printf("%s", arg->flags.delim); 2205 printf("%s", flag->str); 2206 print = 1; 2207 val &= ~fval; 2208 } 2209 } 2210 break; 2211 case PRINT_SYMBOL: 2212 val = eval_num_arg(data, size, event, arg->symbol.field); 2213 for (flag = arg->symbol.symbols; flag; flag = flag->next) { 2214 fval = eval_flag(flag->value); 2215 if (val == fval) { 2216 printf("%s", flag->str); 2217 break; 2218 } 2219 } 2220 break; 2221 2222 case PRINT_TYPE: 2223 break; 2224 case PRINT_STRING: { 2225 int str_offset; 2226 2227 if (arg->string.offset == -1) { 2228 struct format_field *f; 2229 2230 f = find_any_field(event, arg->string.string); 2231 arg->string.offset = f->offset; 2232 } 2233 str_offset = *(int *)(data + arg->string.offset); 2234 str_offset &= 0xffff; 2235 printf("%s", ((char *)data) + str_offset); 2236 break; 2237 } 2238 case PRINT_OP: 2239 /* 2240 * The only op for string should be ? : 2241 */ 2242 if (arg->op.op[0] != '?') 2243 return; 2244 val = eval_num_arg(data, size, event, arg->op.left); 2245 if (val) 2246 print_str_arg(data, size, event, arg->op.right->op.left); 2247 else 2248 print_str_arg(data, size, event, arg->op.right->op.right); 2249 break; 2250 default: 2251 /* well... */ 2252 break; 2253 } 2254 } 2255 2256 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event) 2257 { 2258 static struct format_field *field, *ip_field; 2259 struct print_arg *args, *arg, **next; 2260 unsigned long long ip, val; 2261 char *ptr; 2262 void *bptr; 2263 2264 if (!field) { 2265 field = find_field(event, "buf"); 2266 if (!field) 2267 die("can't find buffer field for binary printk"); 2268 ip_field = find_field(event, "ip"); 2269 if (!ip_field) 2270 die("can't find ip field for binary printk"); 2271 } 2272 2273 ip = read_size(data + ip_field->offset, ip_field->size); 2274 2275 /* 2276 * The first arg is the IP pointer. 2277 */ 2278 args = malloc_or_die(sizeof(*args)); 2279 arg = args; 2280 arg->next = NULL; 2281 next = &arg->next; 2282 2283 arg->type = PRINT_ATOM; 2284 arg->atom.atom = malloc_or_die(32); 2285 sprintf(arg->atom.atom, "%lld", ip); 2286 2287 /* skip the first "%pf : " */ 2288 for (ptr = fmt + 6, bptr = data + field->offset; 2289 bptr < data + size && *ptr; ptr++) { 2290 int ls = 0; 2291 2292 if (*ptr == '%') { 2293 process_again: 2294 ptr++; 2295 switch (*ptr) { 2296 case '%': 2297 break; 2298 case 'l': 2299 ls++; 2300 goto process_again; 2301 case 'L': 2302 ls = 2; 2303 goto process_again; 2304 case '0' ... '9': 2305 goto process_again; 2306 case 'p': 2307 ls = 1; 2308 /* fall through */ 2309 case 'd': 2310 case 'u': 2311 case 'x': 2312 case 'i': 2313 /* the pointers are always 4 bytes aligned */ 2314 bptr = (void *)(((unsigned long)bptr + 3) & 2315 ~3); 2316 switch (ls) { 2317 case 0: 2318 case 1: 2319 ls = long_size; 2320 break; 2321 case 2: 2322 ls = 8; 2323 default: 2324 break; 2325 } 2326 val = read_size(bptr, ls); 2327 bptr += ls; 2328 arg = malloc_or_die(sizeof(*arg)); 2329 arg->next = NULL; 2330 arg->type = PRINT_ATOM; 2331 arg->atom.atom = malloc_or_die(32); 2332 sprintf(arg->atom.atom, "%lld", val); 2333 *next = arg; 2334 next = &arg->next; 2335 break; 2336 case 's': 2337 arg = malloc_or_die(sizeof(*arg)); 2338 arg->next = NULL; 2339 arg->type = PRINT_STRING; 2340 arg->string.string = strdup(bptr); 2341 bptr += strlen(bptr) + 1; 2342 *next = arg; 2343 next = &arg->next; 2344 default: 2345 break; 2346 } 2347 } 2348 } 2349 2350 return args; 2351 } 2352 2353 static void free_args(struct print_arg *args) 2354 { 2355 struct print_arg *next; 2356 2357 while (args) { 2358 next = args->next; 2359 2360 if (args->type == PRINT_ATOM) 2361 free(args->atom.atom); 2362 else 2363 free(args->string.string); 2364 free(args); 2365 args = next; 2366 } 2367 } 2368 2369 static char *get_bprint_format(void *data, int size __unused, struct event *event) 2370 { 2371 unsigned long long addr; 2372 static struct format_field *field; 2373 struct printk_map *printk; 2374 char *format; 2375 char *p; 2376 2377 if (!field) { 2378 field = find_field(event, "fmt"); 2379 if (!field) 2380 die("can't find format field for binary printk"); 2381 printf("field->offset = %d size=%d\n", field->offset, field->size); 2382 } 2383 2384 addr = read_size(data + field->offset, field->size); 2385 2386 printk = find_printk(addr); 2387 if (!printk) { 2388 format = malloc_or_die(45); 2389 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n", 2390 addr); 2391 return format; 2392 } 2393 2394 p = printk->printk; 2395 /* Remove any quotes. */ 2396 if (*p == '"') 2397 p++; 2398 format = malloc_or_die(strlen(p) + 10); 2399 sprintf(format, "%s : %s", "%pf", p); 2400 /* remove ending quotes and new line since we will add one too */ 2401 p = format + strlen(format) - 1; 2402 if (*p == '"') 2403 *p = 0; 2404 2405 p -= 2; 2406 if (strcmp(p, "\\n") == 0) 2407 *p = 0; 2408 2409 return format; 2410 } 2411 2412 static void pretty_print(void *data, int size, struct event *event) 2413 { 2414 struct print_fmt *print_fmt = &event->print_fmt; 2415 struct print_arg *arg = print_fmt->args; 2416 struct print_arg *args = NULL; 2417 const char *ptr = print_fmt->format; 2418 unsigned long long val; 2419 struct func_map *func; 2420 const char *saveptr; 2421 char *bprint_fmt = NULL; 2422 char format[32]; 2423 int show_func; 2424 int len; 2425 int ls; 2426 2427 if (event->flags & EVENT_FL_ISFUNC) 2428 ptr = " %pF <-- %pF"; 2429 2430 if (event->flags & EVENT_FL_ISBPRINT) { 2431 bprint_fmt = get_bprint_format(data, size, event); 2432 args = make_bprint_args(bprint_fmt, data, size, event); 2433 arg = args; 2434 ptr = bprint_fmt; 2435 } 2436 2437 for (; *ptr; ptr++) { 2438 ls = 0; 2439 if (*ptr == '\\') { 2440 ptr++; 2441 switch (*ptr) { 2442 case 'n': 2443 printf("\n"); 2444 break; 2445 case 't': 2446 printf("\t"); 2447 break; 2448 case 'r': 2449 printf("\r"); 2450 break; 2451 case '\\': 2452 printf("\\"); 2453 break; 2454 default: 2455 printf("%c", *ptr); 2456 break; 2457 } 2458 2459 } else if (*ptr == '%') { 2460 saveptr = ptr; 2461 show_func = 0; 2462 cont_process: 2463 ptr++; 2464 switch (*ptr) { 2465 case '%': 2466 printf("%%"); 2467 break; 2468 case 'l': 2469 ls++; 2470 goto cont_process; 2471 case 'L': 2472 ls = 2; 2473 goto cont_process; 2474 case 'z': 2475 case 'Z': 2476 case '0' ... '9': 2477 goto cont_process; 2478 case 'p': 2479 if (long_size == 4) 2480 ls = 1; 2481 else 2482 ls = 2; 2483 2484 if (*(ptr+1) == 'F' || 2485 *(ptr+1) == 'f') { 2486 ptr++; 2487 show_func = *ptr; 2488 } 2489 2490 /* fall through */ 2491 case 'd': 2492 case 'i': 2493 case 'x': 2494 case 'X': 2495 case 'u': 2496 if (!arg) 2497 die("no argument match"); 2498 2499 len = ((unsigned long)ptr + 1) - 2500 (unsigned long)saveptr; 2501 2502 /* should never happen */ 2503 if (len > 32) 2504 die("bad format!"); 2505 2506 memcpy(format, saveptr, len); 2507 format[len] = 0; 2508 2509 val = eval_num_arg(data, size, event, arg); 2510 arg = arg->next; 2511 2512 if (show_func) { 2513 func = find_func(val); 2514 if (func) { 2515 printf("%s", func->func); 2516 if (show_func == 'F') 2517 printf("+0x%llx", 2518 val - func->addr); 2519 break; 2520 } 2521 } 2522 switch (ls) { 2523 case 0: 2524 printf(format, (int)val); 2525 break; 2526 case 1: 2527 printf(format, (long)val); 2528 break; 2529 case 2: 2530 printf(format, (long long)val); 2531 break; 2532 default: 2533 die("bad count (%d)", ls); 2534 } 2535 break; 2536 case 's': 2537 if (!arg) 2538 die("no matching argument"); 2539 2540 print_str_arg(data, size, event, arg); 2541 arg = arg->next; 2542 break; 2543 default: 2544 printf(">%c<", *ptr); 2545 2546 } 2547 } else 2548 printf("%c", *ptr); 2549 } 2550 2551 if (args) { 2552 free_args(args); 2553 free(bprint_fmt); 2554 } 2555 } 2556 2557 static inline int log10_cpu(int nb) 2558 { 2559 if (nb / 100) 2560 return 3; 2561 if (nb / 10) 2562 return 2; 2563 return 1; 2564 } 2565 2566 static void print_lat_fmt(void *data, int size __unused) 2567 { 2568 unsigned int lat_flags; 2569 unsigned int pc; 2570 int lock_depth; 2571 int hardirq; 2572 int softirq; 2573 2574 lat_flags = parse_common_flags(data); 2575 pc = parse_common_pc(data); 2576 lock_depth = parse_common_lock_depth(data); 2577 2578 hardirq = lat_flags & TRACE_FLAG_HARDIRQ; 2579 softirq = lat_flags & TRACE_FLAG_SOFTIRQ; 2580 2581 printf("%c%c%c", 2582 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : 2583 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 2584 'X' : '.', 2585 (lat_flags & TRACE_FLAG_NEED_RESCHED) ? 2586 'N' : '.', 2587 (hardirq && softirq) ? 'H' : 2588 hardirq ? 'h' : softirq ? 's' : '.'); 2589 2590 if (pc) 2591 printf("%x", pc); 2592 else 2593 printf("."); 2594 2595 if (lock_depth < 0) 2596 printf("."); 2597 else 2598 printf("%d", lock_depth); 2599 } 2600 2601 /* taken from Linux, written by Frederic Weisbecker */ 2602 static void print_graph_cpu(int cpu) 2603 { 2604 int i; 2605 int log10_this = log10_cpu(cpu); 2606 int log10_all = log10_cpu(cpus); 2607 2608 2609 /* 2610 * Start with a space character - to make it stand out 2611 * to the right a bit when trace output is pasted into 2612 * email: 2613 */ 2614 printf(" "); 2615 2616 /* 2617 * Tricky - we space the CPU field according to the max 2618 * number of online CPUs. On a 2-cpu system it would take 2619 * a maximum of 1 digit - on a 128 cpu system it would 2620 * take up to 3 digits: 2621 */ 2622 for (i = 0; i < log10_all - log10_this; i++) 2623 printf(" "); 2624 2625 printf("%d) ", cpu); 2626 } 2627 2628 #define TRACE_GRAPH_PROCINFO_LENGTH 14 2629 #define TRACE_GRAPH_INDENT 2 2630 2631 static void print_graph_proc(int pid, const char *comm) 2632 { 2633 /* sign + log10(MAX_INT) + '\0' */ 2634 char pid_str[11]; 2635 int spaces = 0; 2636 int len; 2637 int i; 2638 2639 sprintf(pid_str, "%d", pid); 2640 2641 /* 1 stands for the "-" character */ 2642 len = strlen(comm) + strlen(pid_str) + 1; 2643 2644 if (len < TRACE_GRAPH_PROCINFO_LENGTH) 2645 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; 2646 2647 /* First spaces to align center */ 2648 for (i = 0; i < spaces / 2; i++) 2649 printf(" "); 2650 2651 printf("%s-%s", comm, pid_str); 2652 2653 /* Last spaces to align center */ 2654 for (i = 0; i < spaces - (spaces / 2); i++) 2655 printf(" "); 2656 } 2657 2658 static struct record * 2659 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func, 2660 struct record *next) 2661 { 2662 struct format_field *field; 2663 struct event *event; 2664 unsigned long val; 2665 int type; 2666 int pid; 2667 2668 type = trace_parse_common_type(next->data); 2669 event = trace_find_event(type); 2670 if (!event) 2671 return NULL; 2672 2673 if (!(event->flags & EVENT_FL_ISFUNCRET)) 2674 return NULL; 2675 2676 pid = parse_common_pid(next->data); 2677 field = find_field(event, "func"); 2678 if (!field) 2679 die("function return does not have field func"); 2680 2681 val = read_size(next->data + field->offset, field->size); 2682 2683 if (cur_pid != pid || cur_func != val) 2684 return NULL; 2685 2686 /* this is a leaf, now advance the iterator */ 2687 return trace_read_data(cpu); 2688 } 2689 2690 /* Signal a overhead of time execution to the output */ 2691 static void print_graph_overhead(unsigned long long duration) 2692 { 2693 /* Non nested entry or return */ 2694 if (duration == ~0ULL) 2695 return (void)printf(" "); 2696 2697 /* Duration exceeded 100 msecs */ 2698 if (duration > 100000ULL) 2699 return (void)printf("! "); 2700 2701 /* Duration exceeded 10 msecs */ 2702 if (duration > 10000ULL) 2703 return (void)printf("+ "); 2704 2705 printf(" "); 2706 } 2707 2708 static void print_graph_duration(unsigned long long duration) 2709 { 2710 unsigned long usecs = duration / 1000; 2711 unsigned long nsecs_rem = duration % 1000; 2712 /* log10(ULONG_MAX) + '\0' */ 2713 char msecs_str[21]; 2714 char nsecs_str[5]; 2715 int len; 2716 int i; 2717 2718 sprintf(msecs_str, "%lu", usecs); 2719 2720 /* Print msecs */ 2721 len = printf("%lu", usecs); 2722 2723 /* Print nsecs (we don't want to exceed 7 numbers) */ 2724 if (len < 7) { 2725 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem); 2726 len += printf(".%s", nsecs_str); 2727 } 2728 2729 printf(" us "); 2730 2731 /* Print remaining spaces to fit the row's width */ 2732 for (i = len; i < 7; i++) 2733 printf(" "); 2734 2735 printf("| "); 2736 } 2737 2738 static void 2739 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec) 2740 { 2741 unsigned long long rettime, calltime; 2742 unsigned long long duration, depth; 2743 unsigned long long val; 2744 struct format_field *field; 2745 struct func_map *func; 2746 struct event *ret_event; 2747 int type; 2748 int i; 2749 2750 type = trace_parse_common_type(ret_rec->data); 2751 ret_event = trace_find_event(type); 2752 2753 field = find_field(ret_event, "rettime"); 2754 if (!field) 2755 die("can't find rettime in return graph"); 2756 rettime = read_size(ret_rec->data + field->offset, field->size); 2757 2758 field = find_field(ret_event, "calltime"); 2759 if (!field) 2760 die("can't find rettime in return graph"); 2761 calltime = read_size(ret_rec->data + field->offset, field->size); 2762 2763 duration = rettime - calltime; 2764 2765 /* Overhead */ 2766 print_graph_overhead(duration); 2767 2768 /* Duration */ 2769 print_graph_duration(duration); 2770 2771 field = find_field(event, "depth"); 2772 if (!field) 2773 die("can't find depth in entry graph"); 2774 depth = read_size(data + field->offset, field->size); 2775 2776 /* Function */ 2777 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2778 printf(" "); 2779 2780 field = find_field(event, "func"); 2781 if (!field) 2782 die("can't find func in entry graph"); 2783 val = read_size(data + field->offset, field->size); 2784 func = find_func(val); 2785 2786 if (func) 2787 printf("%s();", func->func); 2788 else 2789 printf("%llx();", val); 2790 } 2791 2792 static void print_graph_nested(struct event *event, void *data) 2793 { 2794 struct format_field *field; 2795 unsigned long long depth; 2796 unsigned long long val; 2797 struct func_map *func; 2798 int i; 2799 2800 /* No overhead */ 2801 print_graph_overhead(-1); 2802 2803 /* No time */ 2804 printf(" | "); 2805 2806 field = find_field(event, "depth"); 2807 if (!field) 2808 die("can't find depth in entry graph"); 2809 depth = read_size(data + field->offset, field->size); 2810 2811 /* Function */ 2812 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2813 printf(" "); 2814 2815 field = find_field(event, "func"); 2816 if (!field) 2817 die("can't find func in entry graph"); 2818 val = read_size(data + field->offset, field->size); 2819 func = find_func(val); 2820 2821 if (func) 2822 printf("%s() {", func->func); 2823 else 2824 printf("%llx() {", val); 2825 } 2826 2827 static void 2828 pretty_print_func_ent(void *data, int size, struct event *event, 2829 int cpu, int pid, const char *comm, 2830 unsigned long secs, unsigned long usecs) 2831 { 2832 struct format_field *field; 2833 struct record *rec; 2834 void *copy_data; 2835 unsigned long val; 2836 2837 printf("%5lu.%06lu | ", secs, usecs); 2838 2839 print_graph_cpu(cpu); 2840 print_graph_proc(pid, comm); 2841 2842 printf(" | "); 2843 2844 if (latency_format) { 2845 print_lat_fmt(data, size); 2846 printf(" | "); 2847 } 2848 2849 field = find_field(event, "func"); 2850 if (!field) 2851 die("function entry does not have func field"); 2852 2853 val = read_size(data + field->offset, field->size); 2854 2855 /* 2856 * peek_data may unmap the data pointer. Copy it first. 2857 */ 2858 copy_data = malloc_or_die(size); 2859 memcpy(copy_data, data, size); 2860 data = copy_data; 2861 2862 rec = trace_peek_data(cpu); 2863 if (rec) { 2864 rec = get_return_for_leaf(cpu, pid, val, rec); 2865 if (rec) { 2866 print_graph_entry_leaf(event, data, rec); 2867 goto out_free; 2868 } 2869 } 2870 print_graph_nested(event, data); 2871 out_free: 2872 free(data); 2873 } 2874 2875 static void 2876 pretty_print_func_ret(void *data, int size __unused, struct event *event, 2877 int cpu, int pid, const char *comm, 2878 unsigned long secs, unsigned long usecs) 2879 { 2880 unsigned long long rettime, calltime; 2881 unsigned long long duration, depth; 2882 struct format_field *field; 2883 int i; 2884 2885 printf("%5lu.%06lu | ", secs, usecs); 2886 2887 print_graph_cpu(cpu); 2888 print_graph_proc(pid, comm); 2889 2890 printf(" | "); 2891 2892 if (latency_format) { 2893 print_lat_fmt(data, size); 2894 printf(" | "); 2895 } 2896 2897 field = find_field(event, "rettime"); 2898 if (!field) 2899 die("can't find rettime in return graph"); 2900 rettime = read_size(data + field->offset, field->size); 2901 2902 field = find_field(event, "calltime"); 2903 if (!field) 2904 die("can't find calltime in return graph"); 2905 calltime = read_size(data + field->offset, field->size); 2906 2907 duration = rettime - calltime; 2908 2909 /* Overhead */ 2910 print_graph_overhead(duration); 2911 2912 /* Duration */ 2913 print_graph_duration(duration); 2914 2915 field = find_field(event, "depth"); 2916 if (!field) 2917 die("can't find depth in entry graph"); 2918 depth = read_size(data + field->offset, field->size); 2919 2920 /* Function */ 2921 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++) 2922 printf(" "); 2923 2924 printf("}"); 2925 } 2926 2927 static void 2928 pretty_print_func_graph(void *data, int size, struct event *event, 2929 int cpu, int pid, const char *comm, 2930 unsigned long secs, unsigned long usecs) 2931 { 2932 if (event->flags & EVENT_FL_ISFUNCENT) 2933 pretty_print_func_ent(data, size, event, 2934 cpu, pid, comm, secs, usecs); 2935 else if (event->flags & EVENT_FL_ISFUNCRET) 2936 pretty_print_func_ret(data, size, event, 2937 cpu, pid, comm, secs, usecs); 2938 printf("\n"); 2939 } 2940 2941 void print_event(int cpu, void *data, int size, unsigned long long nsecs, 2942 char *comm) 2943 { 2944 struct event *event; 2945 unsigned long secs; 2946 unsigned long usecs; 2947 int type; 2948 int pid; 2949 2950 secs = nsecs / NSECS_PER_SEC; 2951 nsecs -= secs * NSECS_PER_SEC; 2952 usecs = nsecs / NSECS_PER_USEC; 2953 2954 type = trace_parse_common_type(data); 2955 2956 event = trace_find_event(type); 2957 if (!event) { 2958 warning("ug! no event found for type %d", type); 2959 return; 2960 } 2961 2962 pid = parse_common_pid(data); 2963 2964 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET)) 2965 return pretty_print_func_graph(data, size, event, cpu, 2966 pid, comm, secs, usecs); 2967 2968 if (latency_format) { 2969 printf("%8.8s-%-5d %3d", 2970 comm, pid, cpu); 2971 print_lat_fmt(data, size); 2972 } else 2973 printf("%16s-%-5d [%03d]", comm, pid, cpu); 2974 2975 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name); 2976 2977 if (event->flags & EVENT_FL_FAILED) { 2978 printf("EVENT '%s' FAILED TO PARSE\n", 2979 event->name); 2980 return; 2981 } 2982 2983 pretty_print(data, size, event); 2984 printf("\n"); 2985 } 2986 2987 static void print_fields(struct print_flag_sym *field) 2988 { 2989 printf("{ %s, %s }", field->value, field->str); 2990 if (field->next) { 2991 printf(", "); 2992 print_fields(field->next); 2993 } 2994 } 2995 2996 static void print_args(struct print_arg *args) 2997 { 2998 int print_paren = 1; 2999 3000 switch (args->type) { 3001 case PRINT_NULL: 3002 printf("null"); 3003 break; 3004 case PRINT_ATOM: 3005 printf("%s", args->atom.atom); 3006 break; 3007 case PRINT_FIELD: 3008 printf("REC->%s", args->field.name); 3009 break; 3010 case PRINT_FLAGS: 3011 printf("__print_flags("); 3012 print_args(args->flags.field); 3013 printf(", %s, ", args->flags.delim); 3014 print_fields(args->flags.flags); 3015 printf(")"); 3016 break; 3017 case PRINT_SYMBOL: 3018 printf("__print_symbolic("); 3019 print_args(args->symbol.field); 3020 printf(", "); 3021 print_fields(args->symbol.symbols); 3022 printf(")"); 3023 break; 3024 case PRINT_STRING: 3025 printf("__get_str(%s)", args->string.string); 3026 break; 3027 case PRINT_TYPE: 3028 printf("(%s)", args->typecast.type); 3029 print_args(args->typecast.item); 3030 break; 3031 case PRINT_OP: 3032 if (strcmp(args->op.op, ":") == 0) 3033 print_paren = 0; 3034 if (print_paren) 3035 printf("("); 3036 print_args(args->op.left); 3037 printf(" %s ", args->op.op); 3038 print_args(args->op.right); 3039 if (print_paren) 3040 printf(")"); 3041 break; 3042 default: 3043 /* we should warn... */ 3044 return; 3045 } 3046 if (args->next) { 3047 printf("\n"); 3048 print_args(args->next); 3049 } 3050 } 3051 3052 static void parse_header_field(const char *field, 3053 int *offset, int *size) 3054 { 3055 char *token; 3056 int type; 3057 3058 if (read_expected(EVENT_ITEM, "field") < 0) 3059 return; 3060 if (read_expected(EVENT_OP, ":") < 0) 3061 return; 3062 3063 /* type */ 3064 if (read_expect_type(EVENT_ITEM, &token) < 0) 3065 goto fail; 3066 free_token(token); 3067 3068 if (read_expected(EVENT_ITEM, field) < 0) 3069 return; 3070 if (read_expected(EVENT_OP, ";") < 0) 3071 return; 3072 if (read_expected(EVENT_ITEM, "offset") < 0) 3073 return; 3074 if (read_expected(EVENT_OP, ":") < 0) 3075 return; 3076 if (read_expect_type(EVENT_ITEM, &token) < 0) 3077 goto fail; 3078 *offset = atoi(token); 3079 free_token(token); 3080 if (read_expected(EVENT_OP, ";") < 0) 3081 return; 3082 if (read_expected(EVENT_ITEM, "size") < 0) 3083 return; 3084 if (read_expected(EVENT_OP, ":") < 0) 3085 return; 3086 if (read_expect_type(EVENT_ITEM, &token) < 0) 3087 goto fail; 3088 *size = atoi(token); 3089 free_token(token); 3090 if (read_expected(EVENT_OP, ";") < 0) 3091 return; 3092 type = read_token(&token); 3093 if (type != EVENT_NEWLINE) { 3094 /* newer versions of the kernel have a "signed" type */ 3095 if (type != EVENT_ITEM) 3096 goto fail; 3097 3098 if (strcmp(token, "signed") != 0) 3099 goto fail; 3100 3101 free_token(token); 3102 3103 if (read_expected(EVENT_OP, ":") < 0) 3104 return; 3105 3106 if (read_expect_type(EVENT_ITEM, &token)) 3107 goto fail; 3108 3109 free_token(token); 3110 if (read_expected(EVENT_OP, ";") < 0) 3111 return; 3112 3113 if (read_expect_type(EVENT_NEWLINE, &token)) 3114 goto fail; 3115 } 3116 fail: 3117 free_token(token); 3118 } 3119 3120 int parse_header_page(char *buf, unsigned long size) 3121 { 3122 init_input_buf(buf, size); 3123 3124 parse_header_field("timestamp", &header_page_ts_offset, 3125 &header_page_ts_size); 3126 parse_header_field("commit", &header_page_size_offset, 3127 &header_page_size_size); 3128 parse_header_field("data", &header_page_data_offset, 3129 &header_page_data_size); 3130 3131 return 0; 3132 } 3133 3134 int parse_ftrace_file(char *buf, unsigned long size) 3135 { 3136 struct format_field *field; 3137 struct print_arg *arg, **list; 3138 struct event *event; 3139 int ret; 3140 3141 init_input_buf(buf, size); 3142 3143 event = alloc_event(); 3144 if (!event) 3145 return -ENOMEM; 3146 3147 event->flags |= EVENT_FL_ISFTRACE; 3148 3149 event->name = event_read_name(); 3150 if (!event->name) 3151 die("failed to read ftrace event name"); 3152 3153 if (strcmp(event->name, "function") == 0) 3154 event->flags |= EVENT_FL_ISFUNC; 3155 3156 else if (strcmp(event->name, "funcgraph_entry") == 0) 3157 event->flags |= EVENT_FL_ISFUNCENT; 3158 3159 else if (strcmp(event->name, "funcgraph_exit") == 0) 3160 event->flags |= EVENT_FL_ISFUNCRET; 3161 3162 else if (strcmp(event->name, "bprint") == 0) 3163 event->flags |= EVENT_FL_ISBPRINT; 3164 3165 event->id = event_read_id(); 3166 if (event->id < 0) 3167 die("failed to read ftrace event id"); 3168 3169 add_event(event); 3170 3171 ret = event_read_format(event); 3172 if (ret < 0) 3173 die("failed to read ftrace event format"); 3174 3175 ret = event_read_print(event); 3176 if (ret < 0) 3177 die("failed to read ftrace event print fmt"); 3178 3179 /* New ftrace handles args */ 3180 if (ret > 0) 3181 return 0; 3182 /* 3183 * The arguments for ftrace files are parsed by the fields. 3184 * Set up the fields as their arguments. 3185 */ 3186 list = &event->print_fmt.args; 3187 for (field = event->format.fields; field; field = field->next) { 3188 arg = malloc_or_die(sizeof(*arg)); 3189 memset(arg, 0, sizeof(*arg)); 3190 *list = arg; 3191 list = &arg->next; 3192 arg->type = PRINT_FIELD; 3193 arg->field.name = field->name; 3194 arg->field.field = field; 3195 } 3196 return 0; 3197 } 3198 3199 int parse_event_file(char *buf, unsigned long size, char *sys) 3200 { 3201 struct event *event; 3202 int ret; 3203 3204 init_input_buf(buf, size); 3205 3206 event = alloc_event(); 3207 if (!event) 3208 return -ENOMEM; 3209 3210 event->name = event_read_name(); 3211 if (!event->name) 3212 die("failed to read event name"); 3213 3214 event->id = event_read_id(); 3215 if (event->id < 0) 3216 die("failed to read event id"); 3217 3218 ret = event_read_format(event); 3219 if (ret < 0) { 3220 warning("failed to read event format for %s", event->name); 3221 goto event_failed; 3222 } 3223 3224 ret = event_read_print(event); 3225 if (ret < 0) { 3226 warning("failed to read event print fmt for %s", event->name); 3227 goto event_failed; 3228 } 3229 3230 event->system = strdup(sys); 3231 3232 #define PRINT_ARGS 0 3233 if (PRINT_ARGS && event->print_fmt.args) 3234 print_args(event->print_fmt.args); 3235 3236 add_event(event); 3237 return 0; 3238 3239 event_failed: 3240 event->flags |= EVENT_FL_FAILED; 3241 /* still add it even if it failed */ 3242 add_event(event); 3243 return -1; 3244 } 3245 3246 void parse_set_info(int nr_cpus, int long_sz) 3247 { 3248 cpus = nr_cpus; 3249 long_size = long_sz; 3250 } 3251