at v3.7-rc1 5612 lines 120 kB view raw
1/* 2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> 3 * 4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; 8 * version 2.1 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 Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 * 21 * The parts for function graph printing was taken and modified from the 22 * Linux Kernel that were written by 23 * - Copyright (C) 2009 Frederic Weisbecker, 24 * Frederic Weisbecker gave his permission to relicense the code to 25 * the Lesser General Public License. 26 */ 27#include <stdio.h> 28#include <stdlib.h> 29#include <string.h> 30#include <stdarg.h> 31#include <ctype.h> 32#include <errno.h> 33#include <stdint.h> 34#include <limits.h> 35 36#include "event-parse.h" 37#include "event-utils.h" 38 39static const char *input_buf; 40static unsigned long long input_buf_ptr; 41static unsigned long long input_buf_siz; 42 43static int is_flag_field; 44static int is_symbolic_field; 45 46static int show_warning = 1; 47 48#define do_warning(fmt, ...) \ 49 do { \ 50 if (show_warning) \ 51 warning(fmt, ##__VA_ARGS__); \ 52 } while (0) 53 54static void init_input_buf(const char *buf, unsigned long long size) 55{ 56 input_buf = buf; 57 input_buf_siz = size; 58 input_buf_ptr = 0; 59} 60 61const char *pevent_get_input_buf(void) 62{ 63 return input_buf; 64} 65 66unsigned long long pevent_get_input_buf_ptr(void) 67{ 68 return input_buf_ptr; 69} 70 71struct event_handler { 72 struct event_handler *next; 73 int id; 74 const char *sys_name; 75 const char *event_name; 76 pevent_event_handler_func func; 77 void *context; 78}; 79 80struct pevent_func_params { 81 struct pevent_func_params *next; 82 enum pevent_func_arg_type type; 83}; 84 85struct pevent_function_handler { 86 struct pevent_function_handler *next; 87 enum pevent_func_arg_type ret_type; 88 char *name; 89 pevent_func_handler func; 90 struct pevent_func_params *params; 91 int nr_args; 92}; 93 94static unsigned long long 95process_defined_func(struct trace_seq *s, void *data, int size, 96 struct event_format *event, struct print_arg *arg); 97 98static void free_func_handle(struct pevent_function_handler *func); 99 100/** 101 * pevent_buffer_init - init buffer for parsing 102 * @buf: buffer to parse 103 * @size: the size of the buffer 104 * 105 * For use with pevent_read_token(), this initializes the internal 106 * buffer that pevent_read_token() will parse. 107 */ 108void pevent_buffer_init(const char *buf, unsigned long long size) 109{ 110 init_input_buf(buf, size); 111} 112 113void breakpoint(void) 114{ 115 static int x; 116 x++; 117} 118 119struct print_arg *alloc_arg(void) 120{ 121 return calloc(1, sizeof(struct print_arg)); 122} 123 124struct cmdline { 125 char *comm; 126 int pid; 127}; 128 129static int cmdline_cmp(const void *a, const void *b) 130{ 131 const struct cmdline *ca = a; 132 const struct cmdline *cb = b; 133 134 if (ca->pid < cb->pid) 135 return -1; 136 if (ca->pid > cb->pid) 137 return 1; 138 139 return 0; 140} 141 142struct cmdline_list { 143 struct cmdline_list *next; 144 char *comm; 145 int pid; 146}; 147 148static int cmdline_init(struct pevent *pevent) 149{ 150 struct cmdline_list *cmdlist = pevent->cmdlist; 151 struct cmdline_list *item; 152 struct cmdline *cmdlines; 153 int i; 154 155 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count); 156 if (!cmdlines) 157 return -1; 158 159 i = 0; 160 while (cmdlist) { 161 cmdlines[i].pid = cmdlist->pid; 162 cmdlines[i].comm = cmdlist->comm; 163 i++; 164 item = cmdlist; 165 cmdlist = cmdlist->next; 166 free(item); 167 } 168 169 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); 170 171 pevent->cmdlines = cmdlines; 172 pevent->cmdlist = NULL; 173 174 return 0; 175} 176 177static char *find_cmdline(struct pevent *pevent, int pid) 178{ 179 const struct cmdline *comm; 180 struct cmdline key; 181 182 if (!pid) 183 return "<idle>"; 184 185 if (!pevent->cmdlines && cmdline_init(pevent)) 186 return "<not enough memory for cmdlines!>"; 187 188 key.pid = pid; 189 190 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, 191 sizeof(*pevent->cmdlines), cmdline_cmp); 192 193 if (comm) 194 return comm->comm; 195 return "<...>"; 196} 197 198/** 199 * pevent_pid_is_registered - return if a pid has a cmdline registered 200 * @pevent: handle for the pevent 201 * @pid: The pid to check if it has a cmdline registered with. 202 * 203 * Returns 1 if the pid has a cmdline mapped to it 204 * 0 otherwise. 205 */ 206int pevent_pid_is_registered(struct pevent *pevent, int pid) 207{ 208 const struct cmdline *comm; 209 struct cmdline key; 210 211 if (!pid) 212 return 1; 213 214 if (!pevent->cmdlines && cmdline_init(pevent)) 215 return 0; 216 217 key.pid = pid; 218 219 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, 220 sizeof(*pevent->cmdlines), cmdline_cmp); 221 222 if (comm) 223 return 1; 224 return 0; 225} 226 227/* 228 * If the command lines have been converted to an array, then 229 * we must add this pid. This is much slower than when cmdlines 230 * are added before the array is initialized. 231 */ 232static int add_new_comm(struct pevent *pevent, const char *comm, int pid) 233{ 234 struct cmdline *cmdlines = pevent->cmdlines; 235 const struct cmdline *cmdline; 236 struct cmdline key; 237 238 if (!pid) 239 return 0; 240 241 /* avoid duplicates */ 242 key.pid = pid; 243 244 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, 245 sizeof(*pevent->cmdlines), cmdline_cmp); 246 if (cmdline) { 247 errno = EEXIST; 248 return -1; 249 } 250 251 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); 252 if (!cmdlines) { 253 errno = ENOMEM; 254 return -1; 255 } 256 257 cmdlines[pevent->cmdline_count].comm = strdup(comm); 258 if (!cmdlines[pevent->cmdline_count].comm) { 259 free(cmdlines); 260 errno = ENOMEM; 261 return -1; 262 } 263 264 cmdlines[pevent->cmdline_count].pid = pid; 265 266 if (cmdlines[pevent->cmdline_count].comm) 267 pevent->cmdline_count++; 268 269 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); 270 pevent->cmdlines = cmdlines; 271 272 return 0; 273} 274 275/** 276 * pevent_register_comm - register a pid / comm mapping 277 * @pevent: handle for the pevent 278 * @comm: the command line to register 279 * @pid: the pid to map the command line to 280 * 281 * This adds a mapping to search for command line names with 282 * a given pid. The comm is duplicated. 283 */ 284int pevent_register_comm(struct pevent *pevent, const char *comm, int pid) 285{ 286 struct cmdline_list *item; 287 288 if (pevent->cmdlines) 289 return add_new_comm(pevent, comm, pid); 290 291 item = malloc(sizeof(*item)); 292 if (!item) 293 return -1; 294 295 item->comm = strdup(comm); 296 if (!item->comm) { 297 free(item); 298 return -1; 299 } 300 item->pid = pid; 301 item->next = pevent->cmdlist; 302 303 pevent->cmdlist = item; 304 pevent->cmdline_count++; 305 306 return 0; 307} 308 309struct func_map { 310 unsigned long long addr; 311 char *func; 312 char *mod; 313}; 314 315struct func_list { 316 struct func_list *next; 317 unsigned long long addr; 318 char *func; 319 char *mod; 320}; 321 322static int func_cmp(const void *a, const void *b) 323{ 324 const struct func_map *fa = a; 325 const struct func_map *fb = b; 326 327 if (fa->addr < fb->addr) 328 return -1; 329 if (fa->addr > fb->addr) 330 return 1; 331 332 return 0; 333} 334 335/* 336 * We are searching for a record in between, not an exact 337 * match. 338 */ 339static int func_bcmp(const void *a, const void *b) 340{ 341 const struct func_map *fa = a; 342 const struct func_map *fb = b; 343 344 if ((fa->addr == fb->addr) || 345 346 (fa->addr > fb->addr && 347 fa->addr < (fb+1)->addr)) 348 return 0; 349 350 if (fa->addr < fb->addr) 351 return -1; 352 353 return 1; 354} 355 356static int func_map_init(struct pevent *pevent) 357{ 358 struct func_list *funclist; 359 struct func_list *item; 360 struct func_map *func_map; 361 int i; 362 363 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1)); 364 if (!func_map) 365 return -1; 366 367 funclist = pevent->funclist; 368 369 i = 0; 370 while (funclist) { 371 func_map[i].func = funclist->func; 372 func_map[i].addr = funclist->addr; 373 func_map[i].mod = funclist->mod; 374 i++; 375 item = funclist; 376 funclist = funclist->next; 377 free(item); 378 } 379 380 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp); 381 382 /* 383 * Add a special record at the end. 384 */ 385 func_map[pevent->func_count].func = NULL; 386 func_map[pevent->func_count].addr = 0; 387 func_map[pevent->func_count].mod = NULL; 388 389 pevent->func_map = func_map; 390 pevent->funclist = NULL; 391 392 return 0; 393} 394 395static struct func_map * 396find_func(struct pevent *pevent, unsigned long long addr) 397{ 398 struct func_map *func; 399 struct func_map key; 400 401 if (!pevent->func_map) 402 func_map_init(pevent); 403 404 key.addr = addr; 405 406 func = bsearch(&key, pevent->func_map, pevent->func_count, 407 sizeof(*pevent->func_map), func_bcmp); 408 409 return func; 410} 411 412/** 413 * pevent_find_function - find a function by a given address 414 * @pevent: handle for the pevent 415 * @addr: the address to find the function with 416 * 417 * Returns a pointer to the function stored that has the given 418 * address. Note, the address does not have to be exact, it 419 * will select the function that would contain the address. 420 */ 421const char *pevent_find_function(struct pevent *pevent, unsigned long long addr) 422{ 423 struct func_map *map; 424 425 map = find_func(pevent, addr); 426 if (!map) 427 return NULL; 428 429 return map->func; 430} 431 432/** 433 * pevent_find_function_address - find a function address by a given address 434 * @pevent: handle for the pevent 435 * @addr: the address to find the function with 436 * 437 * Returns the address the function starts at. This can be used in 438 * conjunction with pevent_find_function to print both the function 439 * name and the function offset. 440 */ 441unsigned long long 442pevent_find_function_address(struct pevent *pevent, unsigned long long addr) 443{ 444 struct func_map *map; 445 446 map = find_func(pevent, addr); 447 if (!map) 448 return 0; 449 450 return map->addr; 451} 452 453/** 454 * pevent_register_function - register a function with a given address 455 * @pevent: handle for the pevent 456 * @function: the function name to register 457 * @addr: the address the function starts at 458 * @mod: the kernel module the function may be in (NULL for none) 459 * 460 * This registers a function name with an address and module. 461 * The @func passed in is duplicated. 462 */ 463int pevent_register_function(struct pevent *pevent, char *func, 464 unsigned long long addr, char *mod) 465{ 466 struct func_list *item = malloc(sizeof(*item)); 467 468 if (!item) 469 return -1; 470 471 item->next = pevent->funclist; 472 item->func = strdup(func); 473 if (!item->func) 474 goto out_free; 475 476 if (mod) { 477 item->mod = strdup(mod); 478 if (!item->mod) 479 goto out_free_func; 480 } else 481 item->mod = NULL; 482 item->addr = addr; 483 484 pevent->funclist = item; 485 pevent->func_count++; 486 487 return 0; 488 489out_free_func: 490 free(item->func); 491 item->func = NULL; 492out_free: 493 free(item); 494 errno = ENOMEM; 495 return -1; 496} 497 498/** 499 * pevent_print_funcs - print out the stored functions 500 * @pevent: handle for the pevent 501 * 502 * This prints out the stored functions. 503 */ 504void pevent_print_funcs(struct pevent *pevent) 505{ 506 int i; 507 508 if (!pevent->func_map) 509 func_map_init(pevent); 510 511 for (i = 0; i < (int)pevent->func_count; i++) { 512 printf("%016llx %s", 513 pevent->func_map[i].addr, 514 pevent->func_map[i].func); 515 if (pevent->func_map[i].mod) 516 printf(" [%s]\n", pevent->func_map[i].mod); 517 else 518 printf("\n"); 519 } 520} 521 522struct printk_map { 523 unsigned long long addr; 524 char *printk; 525}; 526 527struct printk_list { 528 struct printk_list *next; 529 unsigned long long addr; 530 char *printk; 531}; 532 533static int printk_cmp(const void *a, const void *b) 534{ 535 const struct printk_map *pa = a; 536 const struct printk_map *pb = b; 537 538 if (pa->addr < pb->addr) 539 return -1; 540 if (pa->addr > pb->addr) 541 return 1; 542 543 return 0; 544} 545 546static int printk_map_init(struct pevent *pevent) 547{ 548 struct printk_list *printklist; 549 struct printk_list *item; 550 struct printk_map *printk_map; 551 int i; 552 553 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1)); 554 if (!printk_map) 555 return -1; 556 557 printklist = pevent->printklist; 558 559 i = 0; 560 while (printklist) { 561 printk_map[i].printk = printklist->printk; 562 printk_map[i].addr = printklist->addr; 563 i++; 564 item = printklist; 565 printklist = printklist->next; 566 free(item); 567 } 568 569 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp); 570 571 pevent->printk_map = printk_map; 572 pevent->printklist = NULL; 573 574 return 0; 575} 576 577static struct printk_map * 578find_printk(struct pevent *pevent, unsigned long long addr) 579{ 580 struct printk_map *printk; 581 struct printk_map key; 582 583 if (!pevent->printk_map && printk_map_init(pevent)) 584 return NULL; 585 586 key.addr = addr; 587 588 printk = bsearch(&key, pevent->printk_map, pevent->printk_count, 589 sizeof(*pevent->printk_map), printk_cmp); 590 591 return printk; 592} 593 594/** 595 * pevent_register_print_string - register a string by its address 596 * @pevent: handle for the pevent 597 * @fmt: the string format to register 598 * @addr: the address the string was located at 599 * 600 * This registers a string by the address it was stored in the kernel. 601 * The @fmt passed in is duplicated. 602 */ 603int pevent_register_print_string(struct pevent *pevent, char *fmt, 604 unsigned long long addr) 605{ 606 struct printk_list *item = malloc(sizeof(*item)); 607 608 if (!item) 609 return -1; 610 611 item->next = pevent->printklist; 612 item->addr = addr; 613 614 item->printk = strdup(fmt); 615 if (!item->printk) 616 goto out_free; 617 618 pevent->printklist = item; 619 pevent->printk_count++; 620 621 return 0; 622 623out_free: 624 free(item); 625 errno = ENOMEM; 626 return -1; 627} 628 629/** 630 * pevent_print_printk - print out the stored strings 631 * @pevent: handle for the pevent 632 * 633 * This prints the string formats that were stored. 634 */ 635void pevent_print_printk(struct pevent *pevent) 636{ 637 int i; 638 639 if (!pevent->printk_map) 640 printk_map_init(pevent); 641 642 for (i = 0; i < (int)pevent->printk_count; i++) { 643 printf("%016llx %s\n", 644 pevent->printk_map[i].addr, 645 pevent->printk_map[i].printk); 646 } 647} 648 649static struct event_format *alloc_event(void) 650{ 651 return calloc(1, sizeof(struct event_format)); 652} 653 654static int add_event(struct pevent *pevent, struct event_format *event) 655{ 656 int i; 657 struct event_format **events = realloc(pevent->events, sizeof(event) * 658 (pevent->nr_events + 1)); 659 if (!events) 660 return -1; 661 662 pevent->events = events; 663 664 for (i = 0; i < pevent->nr_events; i++) { 665 if (pevent->events[i]->id > event->id) 666 break; 667 } 668 if (i < pevent->nr_events) 669 memmove(&pevent->events[i + 1], 670 &pevent->events[i], 671 sizeof(event) * (pevent->nr_events - i)); 672 673 pevent->events[i] = event; 674 pevent->nr_events++; 675 676 event->pevent = pevent; 677 678 return 0; 679} 680 681static int event_item_type(enum event_type type) 682{ 683 switch (type) { 684 case EVENT_ITEM ... EVENT_SQUOTE: 685 return 1; 686 case EVENT_ERROR ... EVENT_DELIM: 687 default: 688 return 0; 689 } 690} 691 692static void free_flag_sym(struct print_flag_sym *fsym) 693{ 694 struct print_flag_sym *next; 695 696 while (fsym) { 697 next = fsym->next; 698 free(fsym->value); 699 free(fsym->str); 700 free(fsym); 701 fsym = next; 702 } 703} 704 705static void free_arg(struct print_arg *arg) 706{ 707 struct print_arg *farg; 708 709 if (!arg) 710 return; 711 712 switch (arg->type) { 713 case PRINT_ATOM: 714 free(arg->atom.atom); 715 break; 716 case PRINT_FIELD: 717 free(arg->field.name); 718 break; 719 case PRINT_FLAGS: 720 free_arg(arg->flags.field); 721 free(arg->flags.delim); 722 free_flag_sym(arg->flags.flags); 723 break; 724 case PRINT_SYMBOL: 725 free_arg(arg->symbol.field); 726 free_flag_sym(arg->symbol.symbols); 727 break; 728 case PRINT_HEX: 729 free_arg(arg->hex.field); 730 free_arg(arg->hex.size); 731 break; 732 case PRINT_TYPE: 733 free(arg->typecast.type); 734 free_arg(arg->typecast.item); 735 break; 736 case PRINT_STRING: 737 case PRINT_BSTRING: 738 free(arg->string.string); 739 break; 740 case PRINT_DYNAMIC_ARRAY: 741 free(arg->dynarray.index); 742 break; 743 case PRINT_OP: 744 free(arg->op.op); 745 free_arg(arg->op.left); 746 free_arg(arg->op.right); 747 break; 748 case PRINT_FUNC: 749 while (arg->func.args) { 750 farg = arg->func.args; 751 arg->func.args = farg->next; 752 free_arg(farg); 753 } 754 break; 755 756 case PRINT_NULL: 757 default: 758 break; 759 } 760 761 free(arg); 762} 763 764static enum event_type get_type(int ch) 765{ 766 if (ch == '\n') 767 return EVENT_NEWLINE; 768 if (isspace(ch)) 769 return EVENT_SPACE; 770 if (isalnum(ch) || ch == '_') 771 return EVENT_ITEM; 772 if (ch == '\'') 773 return EVENT_SQUOTE; 774 if (ch == '"') 775 return EVENT_DQUOTE; 776 if (!isprint(ch)) 777 return EVENT_NONE; 778 if (ch == '(' || ch == ')' || ch == ',') 779 return EVENT_DELIM; 780 781 return EVENT_OP; 782} 783 784static int __read_char(void) 785{ 786 if (input_buf_ptr >= input_buf_siz) 787 return -1; 788 789 return input_buf[input_buf_ptr++]; 790} 791 792static int __peek_char(void) 793{ 794 if (input_buf_ptr >= input_buf_siz) 795 return -1; 796 797 return input_buf[input_buf_ptr]; 798} 799 800/** 801 * pevent_peek_char - peek at the next character that will be read 802 * 803 * Returns the next character read, or -1 if end of buffer. 804 */ 805int pevent_peek_char(void) 806{ 807 return __peek_char(); 808} 809 810static int extend_token(char **tok, char *buf, int size) 811{ 812 char *newtok = realloc(*tok, size); 813 814 if (!newtok) { 815 free(*tok); 816 *tok = NULL; 817 return -1; 818 } 819 820 if (!*tok) 821 strcpy(newtok, buf); 822 else 823 strcat(newtok, buf); 824 *tok = newtok; 825 826 return 0; 827} 828 829static enum event_type force_token(const char *str, char **tok); 830 831static enum event_type __read_token(char **tok) 832{ 833 char buf[BUFSIZ]; 834 int ch, last_ch, quote_ch, next_ch; 835 int i = 0; 836 int tok_size = 0; 837 enum event_type type; 838 839 *tok = NULL; 840 841 842 ch = __read_char(); 843 if (ch < 0) 844 return EVENT_NONE; 845 846 type = get_type(ch); 847 if (type == EVENT_NONE) 848 return type; 849 850 buf[i++] = ch; 851 852 switch (type) { 853 case EVENT_NEWLINE: 854 case EVENT_DELIM: 855 if (asprintf(tok, "%c", ch) < 0) 856 return EVENT_ERROR; 857 858 return type; 859 860 case EVENT_OP: 861 switch (ch) { 862 case '-': 863 next_ch = __peek_char(); 864 if (next_ch == '>') { 865 buf[i++] = __read_char(); 866 break; 867 } 868 /* fall through */ 869 case '+': 870 case '|': 871 case '&': 872 case '>': 873 case '<': 874 last_ch = ch; 875 ch = __peek_char(); 876 if (ch != last_ch) 877 goto test_equal; 878 buf[i++] = __read_char(); 879 switch (last_ch) { 880 case '>': 881 case '<': 882 goto test_equal; 883 default: 884 break; 885 } 886 break; 887 case '!': 888 case '=': 889 goto test_equal; 890 default: /* what should we do instead? */ 891 break; 892 } 893 buf[i] = 0; 894 *tok = strdup(buf); 895 return type; 896 897 test_equal: 898 ch = __peek_char(); 899 if (ch == '=') 900 buf[i++] = __read_char(); 901 goto out; 902 903 case EVENT_DQUOTE: 904 case EVENT_SQUOTE: 905 /* don't keep quotes */ 906 i--; 907 quote_ch = ch; 908 last_ch = 0; 909 concat: 910 do { 911 if (i == (BUFSIZ - 1)) { 912 buf[i] = 0; 913 tok_size += BUFSIZ; 914 915 if (extend_token(tok, buf, tok_size) < 0) 916 return EVENT_NONE; 917 i = 0; 918 } 919 last_ch = ch; 920 ch = __read_char(); 921 buf[i++] = ch; 922 /* the '\' '\' will cancel itself */ 923 if (ch == '\\' && last_ch == '\\') 924 last_ch = 0; 925 } while (ch != quote_ch || last_ch == '\\'); 926 /* remove the last quote */ 927 i--; 928 929 /* 930 * For strings (double quotes) check the next token. 931 * If it is another string, concatinate the two. 932 */ 933 if (type == EVENT_DQUOTE) { 934 unsigned long long save_input_buf_ptr = input_buf_ptr; 935 936 do { 937 ch = __read_char(); 938 } while (isspace(ch)); 939 if (ch == '"') 940 goto concat; 941 input_buf_ptr = save_input_buf_ptr; 942 } 943 944 goto out; 945 946 case EVENT_ERROR ... EVENT_SPACE: 947 case EVENT_ITEM: 948 default: 949 break; 950 } 951 952 while (get_type(__peek_char()) == type) { 953 if (i == (BUFSIZ - 1)) { 954 buf[i] = 0; 955 tok_size += BUFSIZ; 956 957 if (extend_token(tok, buf, tok_size) < 0) 958 return EVENT_NONE; 959 i = 0; 960 } 961 ch = __read_char(); 962 buf[i++] = ch; 963 } 964 965 out: 966 buf[i] = 0; 967 if (extend_token(tok, buf, tok_size + i + 1) < 0) 968 return EVENT_NONE; 969 970 if (type == EVENT_ITEM) { 971 /* 972 * Older versions of the kernel has a bug that 973 * creates invalid symbols and will break the mac80211 974 * parsing. This is a work around to that bug. 975 * 976 * See Linux kernel commit: 977 * 811cb50baf63461ce0bdb234927046131fc7fa8b 978 */ 979 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) { 980 free(*tok); 981 *tok = NULL; 982 return force_token("\"\%s\" ", tok); 983 } else if (strcmp(*tok, "STA_PR_FMT") == 0) { 984 free(*tok); 985 *tok = NULL; 986 return force_token("\" sta:%pM\" ", tok); 987 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) { 988 free(*tok); 989 *tok = NULL; 990 return force_token("\" vif:%p(%d)\" ", tok); 991 } 992 } 993 994 return type; 995} 996 997static enum event_type force_token(const char *str, char **tok) 998{ 999 const char *save_input_buf; 1000 unsigned long long save_input_buf_ptr; 1001 unsigned long long save_input_buf_siz; 1002 enum event_type type; 1003 1004 /* save off the current input pointers */ 1005 save_input_buf = input_buf; 1006 save_input_buf_ptr = input_buf_ptr; 1007 save_input_buf_siz = input_buf_siz; 1008 1009 init_input_buf(str, strlen(str)); 1010 1011 type = __read_token(tok); 1012 1013 /* reset back to original token */ 1014 input_buf = save_input_buf; 1015 input_buf_ptr = save_input_buf_ptr; 1016 input_buf_siz = save_input_buf_siz; 1017 1018 return type; 1019} 1020 1021static void free_token(char *tok) 1022{ 1023 if (tok) 1024 free(tok); 1025} 1026 1027static enum event_type read_token(char **tok) 1028{ 1029 enum event_type type; 1030 1031 for (;;) { 1032 type = __read_token(tok); 1033 if (type != EVENT_SPACE) 1034 return type; 1035 1036 free_token(*tok); 1037 } 1038 1039 /* not reached */ 1040 *tok = NULL; 1041 return EVENT_NONE; 1042} 1043 1044/** 1045 * pevent_read_token - access to utilites to use the pevent parser 1046 * @tok: The token to return 1047 * 1048 * This will parse tokens from the string given by 1049 * pevent_init_data(). 1050 * 1051 * Returns the token type. 1052 */ 1053enum event_type pevent_read_token(char **tok) 1054{ 1055 return read_token(tok); 1056} 1057 1058/** 1059 * pevent_free_token - free a token returned by pevent_read_token 1060 * @token: the token to free 1061 */ 1062void pevent_free_token(char *token) 1063{ 1064 free_token(token); 1065} 1066 1067/* no newline */ 1068static enum event_type read_token_item(char **tok) 1069{ 1070 enum event_type type; 1071 1072 for (;;) { 1073 type = __read_token(tok); 1074 if (type != EVENT_SPACE && type != EVENT_NEWLINE) 1075 return type; 1076 free_token(*tok); 1077 *tok = NULL; 1078 } 1079 1080 /* not reached */ 1081 *tok = NULL; 1082 return EVENT_NONE; 1083} 1084 1085static int test_type(enum event_type type, enum event_type expect) 1086{ 1087 if (type != expect) { 1088 do_warning("Error: expected type %d but read %d", 1089 expect, type); 1090 return -1; 1091 } 1092 return 0; 1093} 1094 1095static int test_type_token(enum event_type type, const char *token, 1096 enum event_type expect, const char *expect_tok) 1097{ 1098 if (type != expect) { 1099 do_warning("Error: expected type %d but read %d", 1100 expect, type); 1101 return -1; 1102 } 1103 1104 if (strcmp(token, expect_tok) != 0) { 1105 do_warning("Error: expected '%s' but read '%s'", 1106 expect_tok, token); 1107 return -1; 1108 } 1109 return 0; 1110} 1111 1112static int __read_expect_type(enum event_type expect, char **tok, int newline_ok) 1113{ 1114 enum event_type type; 1115 1116 if (newline_ok) 1117 type = read_token(tok); 1118 else 1119 type = read_token_item(tok); 1120 return test_type(type, expect); 1121} 1122 1123static int read_expect_type(enum event_type expect, char **tok) 1124{ 1125 return __read_expect_type(expect, tok, 1); 1126} 1127 1128static int __read_expected(enum event_type expect, const char *str, 1129 int newline_ok) 1130{ 1131 enum event_type type; 1132 char *token; 1133 int ret; 1134 1135 if (newline_ok) 1136 type = read_token(&token); 1137 else 1138 type = read_token_item(&token); 1139 1140 ret = test_type_token(type, token, expect, str); 1141 1142 free_token(token); 1143 1144 return ret; 1145} 1146 1147static int read_expected(enum event_type expect, const char *str) 1148{ 1149 return __read_expected(expect, str, 1); 1150} 1151 1152static int read_expected_item(enum event_type expect, const char *str) 1153{ 1154 return __read_expected(expect, str, 0); 1155} 1156 1157static char *event_read_name(void) 1158{ 1159 char *token; 1160 1161 if (read_expected(EVENT_ITEM, "name") < 0) 1162 return NULL; 1163 1164 if (read_expected(EVENT_OP, ":") < 0) 1165 return NULL; 1166 1167 if (read_expect_type(EVENT_ITEM, &token) < 0) 1168 goto fail; 1169 1170 return token; 1171 1172 fail: 1173 free_token(token); 1174 return NULL; 1175} 1176 1177static int event_read_id(void) 1178{ 1179 char *token; 1180 int id; 1181 1182 if (read_expected_item(EVENT_ITEM, "ID") < 0) 1183 return -1; 1184 1185 if (read_expected(EVENT_OP, ":") < 0) 1186 return -1; 1187 1188 if (read_expect_type(EVENT_ITEM, &token) < 0) 1189 goto fail; 1190 1191 id = strtoul(token, NULL, 0); 1192 free_token(token); 1193 return id; 1194 1195 fail: 1196 free_token(token); 1197 return -1; 1198} 1199 1200static int field_is_string(struct format_field *field) 1201{ 1202 if ((field->flags & FIELD_IS_ARRAY) && 1203 (strstr(field->type, "char") || strstr(field->type, "u8") || 1204 strstr(field->type, "s8"))) 1205 return 1; 1206 1207 return 0; 1208} 1209 1210static int field_is_dynamic(struct format_field *field) 1211{ 1212 if (strncmp(field->type, "__data_loc", 10) == 0) 1213 return 1; 1214 1215 return 0; 1216} 1217 1218static int field_is_long(struct format_field *field) 1219{ 1220 /* includes long long */ 1221 if (strstr(field->type, "long")) 1222 return 1; 1223 1224 return 0; 1225} 1226 1227static int event_read_fields(struct event_format *event, struct format_field **fields) 1228{ 1229 struct format_field *field = NULL; 1230 enum event_type type; 1231 char *token; 1232 char *last_token; 1233 int count = 0; 1234 1235 do { 1236 type = read_token(&token); 1237 if (type == EVENT_NEWLINE) { 1238 free_token(token); 1239 return count; 1240 } 1241 1242 count++; 1243 1244 if (test_type_token(type, token, EVENT_ITEM, "field")) 1245 goto fail; 1246 free_token(token); 1247 1248 type = read_token(&token); 1249 /* 1250 * The ftrace fields may still use the "special" name. 1251 * Just ignore it. 1252 */ 1253 if (event->flags & EVENT_FL_ISFTRACE && 1254 type == EVENT_ITEM && strcmp(token, "special") == 0) { 1255 free_token(token); 1256 type = read_token(&token); 1257 } 1258 1259 if (test_type_token(type, token, EVENT_OP, ":") < 0) 1260 goto fail; 1261 1262 free_token(token); 1263 if (read_expect_type(EVENT_ITEM, &token) < 0) 1264 goto fail; 1265 1266 last_token = token; 1267 1268 field = calloc(1, sizeof(*field)); 1269 if (!field) 1270 goto fail; 1271 1272 field->event = event; 1273 1274 /* read the rest of the type */ 1275 for (;;) { 1276 type = read_token(&token); 1277 if (type == EVENT_ITEM || 1278 (type == EVENT_OP && strcmp(token, "*") == 0) || 1279 /* 1280 * Some of the ftrace fields are broken and have 1281 * an illegal "." in them. 1282 */ 1283 (event->flags & EVENT_FL_ISFTRACE && 1284 type == EVENT_OP && strcmp(token, ".") == 0)) { 1285 1286 if (strcmp(token, "*") == 0) 1287 field->flags |= FIELD_IS_POINTER; 1288 1289 if (field->type) { 1290 char *new_type; 1291 new_type = realloc(field->type, 1292 strlen(field->type) + 1293 strlen(last_token) + 2); 1294 if (!new_type) { 1295 free(last_token); 1296 goto fail; 1297 } 1298 field->type = new_type; 1299 strcat(field->type, " "); 1300 strcat(field->type, last_token); 1301 free(last_token); 1302 } else 1303 field->type = last_token; 1304 last_token = token; 1305 continue; 1306 } 1307 1308 break; 1309 } 1310 1311 if (!field->type) { 1312 do_warning("%s: no type found", __func__); 1313 goto fail; 1314 } 1315 field->name = last_token; 1316 1317 if (test_type(type, EVENT_OP)) 1318 goto fail; 1319 1320 if (strcmp(token, "[") == 0) { 1321 enum event_type last_type = type; 1322 char *brackets = token; 1323 char *new_brackets; 1324 int len; 1325 1326 field->flags |= FIELD_IS_ARRAY; 1327 1328 type = read_token(&token); 1329 1330 if (type == EVENT_ITEM) 1331 field->arraylen = strtoul(token, NULL, 0); 1332 else 1333 field->arraylen = 0; 1334 1335 while (strcmp(token, "]") != 0) { 1336 if (last_type == EVENT_ITEM && 1337 type == EVENT_ITEM) 1338 len = 2; 1339 else 1340 len = 1; 1341 last_type = type; 1342 1343 new_brackets = realloc(brackets, 1344 strlen(brackets) + 1345 strlen(token) + len); 1346 if (!new_brackets) { 1347 free(brackets); 1348 goto fail; 1349 } 1350 brackets = new_brackets; 1351 if (len == 2) 1352 strcat(brackets, " "); 1353 strcat(brackets, token); 1354 /* We only care about the last token */ 1355 field->arraylen = strtoul(token, NULL, 0); 1356 free_token(token); 1357 type = read_token(&token); 1358 if (type == EVENT_NONE) { 1359 do_warning("failed to find token"); 1360 goto fail; 1361 } 1362 } 1363 1364 free_token(token); 1365 1366 new_brackets = realloc(brackets, strlen(brackets) + 2); 1367 if (!new_brackets) { 1368 free(brackets); 1369 goto fail; 1370 } 1371 brackets = new_brackets; 1372 strcat(brackets, "]"); 1373 1374 /* add brackets to type */ 1375 1376 type = read_token(&token); 1377 /* 1378 * If the next token is not an OP, then it is of 1379 * the format: type [] item; 1380 */ 1381 if (type == EVENT_ITEM) { 1382 char *new_type; 1383 new_type = realloc(field->type, 1384 strlen(field->type) + 1385 strlen(field->name) + 1386 strlen(brackets) + 2); 1387 if (!new_type) { 1388 free(brackets); 1389 goto fail; 1390 } 1391 field->type = new_type; 1392 strcat(field->type, " "); 1393 strcat(field->type, field->name); 1394 free_token(field->name); 1395 strcat(field->type, brackets); 1396 field->name = token; 1397 type = read_token(&token); 1398 } else { 1399 char *new_type; 1400 new_type = realloc(field->type, 1401 strlen(field->type) + 1402 strlen(brackets) + 1); 1403 if (!new_type) { 1404 free(brackets); 1405 goto fail; 1406 } 1407 field->type = new_type; 1408 strcat(field->type, brackets); 1409 } 1410 free(brackets); 1411 } 1412 1413 if (field_is_string(field)) 1414 field->flags |= FIELD_IS_STRING; 1415 if (field_is_dynamic(field)) 1416 field->flags |= FIELD_IS_DYNAMIC; 1417 if (field_is_long(field)) 1418 field->flags |= FIELD_IS_LONG; 1419 1420 if (test_type_token(type, token, EVENT_OP, ";")) 1421 goto fail; 1422 free_token(token); 1423 1424 if (read_expected(EVENT_ITEM, "offset") < 0) 1425 goto fail_expect; 1426 1427 if (read_expected(EVENT_OP, ":") < 0) 1428 goto fail_expect; 1429 1430 if (read_expect_type(EVENT_ITEM, &token)) 1431 goto fail; 1432 field->offset = strtoul(token, NULL, 0); 1433 free_token(token); 1434 1435 if (read_expected(EVENT_OP, ";") < 0) 1436 goto fail_expect; 1437 1438 if (read_expected(EVENT_ITEM, "size") < 0) 1439 goto fail_expect; 1440 1441 if (read_expected(EVENT_OP, ":") < 0) 1442 goto fail_expect; 1443 1444 if (read_expect_type(EVENT_ITEM, &token)) 1445 goto fail; 1446 field->size = strtoul(token, NULL, 0); 1447 free_token(token); 1448 1449 if (read_expected(EVENT_OP, ";") < 0) 1450 goto fail_expect; 1451 1452 type = read_token(&token); 1453 if (type != EVENT_NEWLINE) { 1454 /* newer versions of the kernel have a "signed" type */ 1455 if (test_type_token(type, token, EVENT_ITEM, "signed")) 1456 goto fail; 1457 1458 free_token(token); 1459 1460 if (read_expected(EVENT_OP, ":") < 0) 1461 goto fail_expect; 1462 1463 if (read_expect_type(EVENT_ITEM, &token)) 1464 goto fail; 1465 1466 /* add signed type */ 1467 1468 free_token(token); 1469 if (read_expected(EVENT_OP, ";") < 0) 1470 goto fail_expect; 1471 1472 if (read_expect_type(EVENT_NEWLINE, &token)) 1473 goto fail; 1474 } 1475 1476 free_token(token); 1477 1478 if (field->flags & FIELD_IS_ARRAY) { 1479 if (field->arraylen) 1480 field->elementsize = field->size / field->arraylen; 1481 else if (field->flags & FIELD_IS_STRING) 1482 field->elementsize = 1; 1483 else 1484 field->elementsize = event->pevent->long_size; 1485 } else 1486 field->elementsize = field->size; 1487 1488 *fields = field; 1489 fields = &field->next; 1490 1491 } while (1); 1492 1493 return 0; 1494 1495fail: 1496 free_token(token); 1497fail_expect: 1498 if (field) { 1499 free(field->type); 1500 free(field->name); 1501 free(field); 1502 } 1503 return -1; 1504} 1505 1506static int event_read_format(struct event_format *event) 1507{ 1508 char *token; 1509 int ret; 1510 1511 if (read_expected_item(EVENT_ITEM, "format") < 0) 1512 return -1; 1513 1514 if (read_expected(EVENT_OP, ":") < 0) 1515 return -1; 1516 1517 if (read_expect_type(EVENT_NEWLINE, &token)) 1518 goto fail; 1519 free_token(token); 1520 1521 ret = event_read_fields(event, &event->format.common_fields); 1522 if (ret < 0) 1523 return ret; 1524 event->format.nr_common = ret; 1525 1526 ret = event_read_fields(event, &event->format.fields); 1527 if (ret < 0) 1528 return ret; 1529 event->format.nr_fields = ret; 1530 1531 return 0; 1532 1533 fail: 1534 free_token(token); 1535 return -1; 1536} 1537 1538static enum event_type 1539process_arg_token(struct event_format *event, struct print_arg *arg, 1540 char **tok, enum event_type type); 1541 1542static enum event_type 1543process_arg(struct event_format *event, struct print_arg *arg, char **tok) 1544{ 1545 enum event_type type; 1546 char *token; 1547 1548 type = read_token(&token); 1549 *tok = token; 1550 1551 return process_arg_token(event, arg, tok, type); 1552} 1553 1554static enum event_type 1555process_op(struct event_format *event, struct print_arg *arg, char **tok); 1556 1557static enum event_type 1558process_cond(struct event_format *event, struct print_arg *top, char **tok) 1559{ 1560 struct print_arg *arg, *left, *right; 1561 enum event_type type; 1562 char *token = NULL; 1563 1564 arg = alloc_arg(); 1565 left = alloc_arg(); 1566 right = alloc_arg(); 1567 1568 if (!arg || !left || !right) { 1569 do_warning("%s: not enough memory!", __func__); 1570 /* arg will be freed at out_free */ 1571 free_arg(left); 1572 free_arg(right); 1573 goto out_free; 1574 } 1575 1576 arg->type = PRINT_OP; 1577 arg->op.left = left; 1578 arg->op.right = right; 1579 1580 *tok = NULL; 1581 type = process_arg(event, left, &token); 1582 1583 again: 1584 /* Handle other operations in the arguments */ 1585 if (type == EVENT_OP && strcmp(token, ":") != 0) { 1586 type = process_op(event, left, &token); 1587 goto again; 1588 } 1589 1590 if (test_type_token(type, token, EVENT_OP, ":")) 1591 goto out_free; 1592 1593 arg->op.op = token; 1594 1595 type = process_arg(event, right, &token); 1596 1597 top->op.right = arg; 1598 1599 *tok = token; 1600 return type; 1601 1602out_free: 1603 /* Top may point to itself */ 1604 top->op.right = NULL; 1605 free_token(token); 1606 free_arg(arg); 1607 return EVENT_ERROR; 1608} 1609 1610static enum event_type 1611process_array(struct event_format *event, struct print_arg *top, char **tok) 1612{ 1613 struct print_arg *arg; 1614 enum event_type type; 1615 char *token = NULL; 1616 1617 arg = alloc_arg(); 1618 if (!arg) { 1619 do_warning("%s: not enough memory!", __func__); 1620 /* '*tok' is set to top->op.op. No need to free. */ 1621 *tok = NULL; 1622 return EVENT_ERROR; 1623 } 1624 1625 *tok = NULL; 1626 type = process_arg(event, arg, &token); 1627 if (test_type_token(type, token, EVENT_OP, "]")) 1628 goto out_free; 1629 1630 top->op.right = arg; 1631 1632 free_token(token); 1633 type = read_token_item(&token); 1634 *tok = token; 1635 1636 return type; 1637 1638out_free: 1639 free_token(token); 1640 free_arg(arg); 1641 return EVENT_ERROR; 1642} 1643 1644static int get_op_prio(char *op) 1645{ 1646 if (!op[1]) { 1647 switch (op[0]) { 1648 case '~': 1649 case '!': 1650 return 4; 1651 case '*': 1652 case '/': 1653 case '%': 1654 return 6; 1655 case '+': 1656 case '-': 1657 return 7; 1658 /* '>>' and '<<' are 8 */ 1659 case '<': 1660 case '>': 1661 return 9; 1662 /* '==' and '!=' are 10 */ 1663 case '&': 1664 return 11; 1665 case '^': 1666 return 12; 1667 case '|': 1668 return 13; 1669 case '?': 1670 return 16; 1671 default: 1672 do_warning("unknown op '%c'", op[0]); 1673 return -1; 1674 } 1675 } else { 1676 if (strcmp(op, "++") == 0 || 1677 strcmp(op, "--") == 0) { 1678 return 3; 1679 } else if (strcmp(op, ">>") == 0 || 1680 strcmp(op, "<<") == 0) { 1681 return 8; 1682 } else if (strcmp(op, ">=") == 0 || 1683 strcmp(op, "<=") == 0) { 1684 return 9; 1685 } else if (strcmp(op, "==") == 0 || 1686 strcmp(op, "!=") == 0) { 1687 return 10; 1688 } else if (strcmp(op, "&&") == 0) { 1689 return 14; 1690 } else if (strcmp(op, "||") == 0) { 1691 return 15; 1692 } else { 1693 do_warning("unknown op '%s'", op); 1694 return -1; 1695 } 1696 } 1697} 1698 1699static int set_op_prio(struct print_arg *arg) 1700{ 1701 1702 /* single ops are the greatest */ 1703 if (!arg->op.left || arg->op.left->type == PRINT_NULL) 1704 arg->op.prio = 0; 1705 else 1706 arg->op.prio = get_op_prio(arg->op.op); 1707 1708 return arg->op.prio; 1709} 1710 1711/* Note, *tok does not get freed, but will most likely be saved */ 1712static enum event_type 1713process_op(struct event_format *event, struct print_arg *arg, char **tok) 1714{ 1715 struct print_arg *left, *right = NULL; 1716 enum event_type type; 1717 char *token; 1718 1719 /* the op is passed in via tok */ 1720 token = *tok; 1721 1722 if (arg->type == PRINT_OP && !arg->op.left) { 1723 /* handle single op */ 1724 if (token[1]) { 1725 do_warning("bad op token %s", token); 1726 goto out_free; 1727 } 1728 switch (token[0]) { 1729 case '~': 1730 case '!': 1731 case '+': 1732 case '-': 1733 break; 1734 default: 1735 do_warning("bad op token %s", token); 1736 goto out_free; 1737 1738 } 1739 1740 /* make an empty left */ 1741 left = alloc_arg(); 1742 if (!left) 1743 goto out_warn_free; 1744 1745 left->type = PRINT_NULL; 1746 arg->op.left = left; 1747 1748 right = alloc_arg(); 1749 if (!right) 1750 goto out_warn_free; 1751 1752 arg->op.right = right; 1753 1754 /* do not free the token, it belongs to an op */ 1755 *tok = NULL; 1756 type = process_arg(event, right, tok); 1757 1758 } else if (strcmp(token, "?") == 0) { 1759 1760 left = alloc_arg(); 1761 if (!left) 1762 goto out_warn_free; 1763 1764 /* copy the top arg to the left */ 1765 *left = *arg; 1766 1767 arg->type = PRINT_OP; 1768 arg->op.op = token; 1769 arg->op.left = left; 1770 arg->op.prio = 0; 1771 1772 /* it will set arg->op.right */ 1773 type = process_cond(event, arg, tok); 1774 1775 } else if (strcmp(token, ">>") == 0 || 1776 strcmp(token, "<<") == 0 || 1777 strcmp(token, "&") == 0 || 1778 strcmp(token, "|") == 0 || 1779 strcmp(token, "&&") == 0 || 1780 strcmp(token, "||") == 0 || 1781 strcmp(token, "-") == 0 || 1782 strcmp(token, "+") == 0 || 1783 strcmp(token, "*") == 0 || 1784 strcmp(token, "^") == 0 || 1785 strcmp(token, "/") == 0 || 1786 strcmp(token, "<") == 0 || 1787 strcmp(token, ">") == 0 || 1788 strcmp(token, "==") == 0 || 1789 strcmp(token, "!=") == 0) { 1790 1791 left = alloc_arg(); 1792 if (!left) 1793 goto out_warn_free; 1794 1795 /* copy the top arg to the left */ 1796 *left = *arg; 1797 1798 arg->type = PRINT_OP; 1799 arg->op.op = token; 1800 arg->op.left = left; 1801 arg->op.right = NULL; 1802 1803 if (set_op_prio(arg) == -1) { 1804 event->flags |= EVENT_FL_FAILED; 1805 /* arg->op.op (= token) will be freed at out_free */ 1806 arg->op.op = NULL; 1807 goto out_free; 1808 } 1809 1810 type = read_token_item(&token); 1811 *tok = token; 1812 1813 /* could just be a type pointer */ 1814 if ((strcmp(arg->op.op, "*") == 0) && 1815 type == EVENT_DELIM && (strcmp(token, ")") == 0)) { 1816 char *new_atom; 1817 1818 if (left->type != PRINT_ATOM) { 1819 do_warning("bad pointer type"); 1820 goto out_free; 1821 } 1822 new_atom = realloc(left->atom.atom, 1823 strlen(left->atom.atom) + 3); 1824 if (!new_atom) 1825 goto out_warn_free; 1826 1827 left->atom.atom = new_atom; 1828 strcat(left->atom.atom, " *"); 1829 free(arg->op.op); 1830 *arg = *left; 1831 free(left); 1832 1833 return type; 1834 } 1835 1836 right = alloc_arg(); 1837 if (!right) 1838 goto out_warn_free; 1839 1840 type = process_arg_token(event, right, tok, type); 1841 arg->op.right = right; 1842 1843 } else if (strcmp(token, "[") == 0) { 1844 1845 left = alloc_arg(); 1846 if (!left) 1847 goto out_warn_free; 1848 1849 *left = *arg; 1850 1851 arg->type = PRINT_OP; 1852 arg->op.op = token; 1853 arg->op.left = left; 1854 1855 arg->op.prio = 0; 1856 1857 /* it will set arg->op.right */ 1858 type = process_array(event, arg, tok); 1859 1860 } else { 1861 do_warning("unknown op '%s'", token); 1862 event->flags |= EVENT_FL_FAILED; 1863 /* the arg is now the left side */ 1864 goto out_free; 1865 } 1866 1867 if (type == EVENT_OP && strcmp(*tok, ":") != 0) { 1868 int prio; 1869 1870 /* higher prios need to be closer to the root */ 1871 prio = get_op_prio(*tok); 1872 1873 if (prio > arg->op.prio) 1874 return process_op(event, arg, tok); 1875 1876 return process_op(event, right, tok); 1877 } 1878 1879 return type; 1880 1881out_warn_free: 1882 do_warning("%s: not enough memory!", __func__); 1883out_free: 1884 free_token(token); 1885 *tok = NULL; 1886 return EVENT_ERROR; 1887} 1888 1889static enum event_type 1890process_entry(struct event_format *event __maybe_unused, struct print_arg *arg, 1891 char **tok) 1892{ 1893 enum event_type type; 1894 char *field; 1895 char *token; 1896 1897 if (read_expected(EVENT_OP, "->") < 0) 1898 goto out_err; 1899 1900 if (read_expect_type(EVENT_ITEM, &token) < 0) 1901 goto out_free; 1902 field = token; 1903 1904 arg->type = PRINT_FIELD; 1905 arg->field.name = field; 1906 1907 if (is_flag_field) { 1908 arg->field.field = pevent_find_any_field(event, arg->field.name); 1909 arg->field.field->flags |= FIELD_IS_FLAG; 1910 is_flag_field = 0; 1911 } else if (is_symbolic_field) { 1912 arg->field.field = pevent_find_any_field(event, arg->field.name); 1913 arg->field.field->flags |= FIELD_IS_SYMBOLIC; 1914 is_symbolic_field = 0; 1915 } 1916 1917 type = read_token(&token); 1918 *tok = token; 1919 1920 return type; 1921 1922 out_free: 1923 free_token(token); 1924 out_err: 1925 *tok = NULL; 1926 return EVENT_ERROR; 1927} 1928 1929static char *arg_eval (struct print_arg *arg); 1930 1931static unsigned long long 1932eval_type_str(unsigned long long val, const char *type, int pointer) 1933{ 1934 int sign = 0; 1935 char *ref; 1936 int len; 1937 1938 len = strlen(type); 1939 1940 if (pointer) { 1941 1942 if (type[len-1] != '*') { 1943 do_warning("pointer expected with non pointer type"); 1944 return val; 1945 } 1946 1947 ref = malloc(len); 1948 if (!ref) { 1949 do_warning("%s: not enough memory!", __func__); 1950 return val; 1951 } 1952 memcpy(ref, type, len); 1953 1954 /* chop off the " *" */ 1955 ref[len - 2] = 0; 1956 1957 val = eval_type_str(val, ref, 0); 1958 free(ref); 1959 return val; 1960 } 1961 1962 /* check if this is a pointer */ 1963 if (type[len - 1] == '*') 1964 return val; 1965 1966 /* Try to figure out the arg size*/ 1967 if (strncmp(type, "struct", 6) == 0) 1968 /* all bets off */ 1969 return val; 1970 1971 if (strcmp(type, "u8") == 0) 1972 return val & 0xff; 1973 1974 if (strcmp(type, "u16") == 0) 1975 return val & 0xffff; 1976 1977 if (strcmp(type, "u32") == 0) 1978 return val & 0xffffffff; 1979 1980 if (strcmp(type, "u64") == 0 || 1981 strcmp(type, "s64")) 1982 return val; 1983 1984 if (strcmp(type, "s8") == 0) 1985 return (unsigned long long)(char)val & 0xff; 1986 1987 if (strcmp(type, "s16") == 0) 1988 return (unsigned long long)(short)val & 0xffff; 1989 1990 if (strcmp(type, "s32") == 0) 1991 return (unsigned long long)(int)val & 0xffffffff; 1992 1993 if (strncmp(type, "unsigned ", 9) == 0) { 1994 sign = 0; 1995 type += 9; 1996 } 1997 1998 if (strcmp(type, "char") == 0) { 1999 if (sign) 2000 return (unsigned long long)(char)val & 0xff; 2001 else 2002 return val & 0xff; 2003 } 2004 2005 if (strcmp(type, "short") == 0) { 2006 if (sign) 2007 return (unsigned long long)(short)val & 0xffff; 2008 else 2009 return val & 0xffff; 2010 } 2011 2012 if (strcmp(type, "int") == 0) { 2013 if (sign) 2014 return (unsigned long long)(int)val & 0xffffffff; 2015 else 2016 return val & 0xffffffff; 2017 } 2018 2019 return val; 2020} 2021 2022/* 2023 * Try to figure out the type. 2024 */ 2025static unsigned long long 2026eval_type(unsigned long long val, struct print_arg *arg, int pointer) 2027{ 2028 if (arg->type != PRINT_TYPE) { 2029 do_warning("expected type argument"); 2030 return 0; 2031 } 2032 2033 return eval_type_str(val, arg->typecast.type, pointer); 2034} 2035 2036static int arg_num_eval(struct print_arg *arg, long long *val) 2037{ 2038 long long left, right; 2039 int ret = 1; 2040 2041 switch (arg->type) { 2042 case PRINT_ATOM: 2043 *val = strtoll(arg->atom.atom, NULL, 0); 2044 break; 2045 case PRINT_TYPE: 2046 ret = arg_num_eval(arg->typecast.item, val); 2047 if (!ret) 2048 break; 2049 *val = eval_type(*val, arg, 0); 2050 break; 2051 case PRINT_OP: 2052 switch (arg->op.op[0]) { 2053 case '|': 2054 ret = arg_num_eval(arg->op.left, &left); 2055 if (!ret) 2056 break; 2057 ret = arg_num_eval(arg->op.right, &right); 2058 if (!ret) 2059 break; 2060 if (arg->op.op[1]) 2061 *val = left || right; 2062 else 2063 *val = left | right; 2064 break; 2065 case '&': 2066 ret = arg_num_eval(arg->op.left, &left); 2067 if (!ret) 2068 break; 2069 ret = arg_num_eval(arg->op.right, &right); 2070 if (!ret) 2071 break; 2072 if (arg->op.op[1]) 2073 *val = left && right; 2074 else 2075 *val = left & right; 2076 break; 2077 case '<': 2078 ret = arg_num_eval(arg->op.left, &left); 2079 if (!ret) 2080 break; 2081 ret = arg_num_eval(arg->op.right, &right); 2082 if (!ret) 2083 break; 2084 switch (arg->op.op[1]) { 2085 case 0: 2086 *val = left < right; 2087 break; 2088 case '<': 2089 *val = left << right; 2090 break; 2091 case '=': 2092 *val = left <= right; 2093 break; 2094 default: 2095 do_warning("unknown op '%s'", arg->op.op); 2096 ret = 0; 2097 } 2098 break; 2099 case '>': 2100 ret = arg_num_eval(arg->op.left, &left); 2101 if (!ret) 2102 break; 2103 ret = arg_num_eval(arg->op.right, &right); 2104 if (!ret) 2105 break; 2106 switch (arg->op.op[1]) { 2107 case 0: 2108 *val = left > right; 2109 break; 2110 case '>': 2111 *val = left >> right; 2112 break; 2113 case '=': 2114 *val = left >= right; 2115 break; 2116 default: 2117 do_warning("unknown op '%s'", arg->op.op); 2118 ret = 0; 2119 } 2120 break; 2121 case '=': 2122 ret = arg_num_eval(arg->op.left, &left); 2123 if (!ret) 2124 break; 2125 ret = arg_num_eval(arg->op.right, &right); 2126 if (!ret) 2127 break; 2128 2129 if (arg->op.op[1] != '=') { 2130 do_warning("unknown op '%s'", arg->op.op); 2131 ret = 0; 2132 } else 2133 *val = left == right; 2134 break; 2135 case '!': 2136 ret = arg_num_eval(arg->op.left, &left); 2137 if (!ret) 2138 break; 2139 ret = arg_num_eval(arg->op.right, &right); 2140 if (!ret) 2141 break; 2142 2143 switch (arg->op.op[1]) { 2144 case '=': 2145 *val = left != right; 2146 break; 2147 default: 2148 do_warning("unknown op '%s'", arg->op.op); 2149 ret = 0; 2150 } 2151 break; 2152 case '-': 2153 /* check for negative */ 2154 if (arg->op.left->type == PRINT_NULL) 2155 left = 0; 2156 else 2157 ret = arg_num_eval(arg->op.left, &left); 2158 if (!ret) 2159 break; 2160 ret = arg_num_eval(arg->op.right, &right); 2161 if (!ret) 2162 break; 2163 *val = left - right; 2164 break; 2165 case '+': 2166 if (arg->op.left->type == PRINT_NULL) 2167 left = 0; 2168 else 2169 ret = arg_num_eval(arg->op.left, &left); 2170 if (!ret) 2171 break; 2172 ret = arg_num_eval(arg->op.right, &right); 2173 if (!ret) 2174 break; 2175 *val = left + right; 2176 break; 2177 default: 2178 do_warning("unknown op '%s'", arg->op.op); 2179 ret = 0; 2180 } 2181 break; 2182 2183 case PRINT_NULL: 2184 case PRINT_FIELD ... PRINT_SYMBOL: 2185 case PRINT_STRING: 2186 case PRINT_BSTRING: 2187 default: 2188 do_warning("invalid eval type %d", arg->type); 2189 ret = 0; 2190 2191 } 2192 return ret; 2193} 2194 2195static char *arg_eval (struct print_arg *arg) 2196{ 2197 long long val; 2198 static char buf[20]; 2199 2200 switch (arg->type) { 2201 case PRINT_ATOM: 2202 return arg->atom.atom; 2203 case PRINT_TYPE: 2204 return arg_eval(arg->typecast.item); 2205 case PRINT_OP: 2206 if (!arg_num_eval(arg, &val)) 2207 break; 2208 sprintf(buf, "%lld", val); 2209 return buf; 2210 2211 case PRINT_NULL: 2212 case PRINT_FIELD ... PRINT_SYMBOL: 2213 case PRINT_STRING: 2214 case PRINT_BSTRING: 2215 default: 2216 do_warning("invalid eval type %d", arg->type); 2217 break; 2218 } 2219 2220 return NULL; 2221} 2222 2223static enum event_type 2224process_fields(struct event_format *event, struct print_flag_sym **list, char **tok) 2225{ 2226 enum event_type type; 2227 struct print_arg *arg = NULL; 2228 struct print_flag_sym *field; 2229 char *token = *tok; 2230 char *value; 2231 2232 do { 2233 free_token(token); 2234 type = read_token_item(&token); 2235 if (test_type_token(type, token, EVENT_OP, "{")) 2236 break; 2237 2238 arg = alloc_arg(); 2239 if (!arg) 2240 goto out_free; 2241 2242 free_token(token); 2243 type = process_arg(event, arg, &token); 2244 2245 if (type == EVENT_OP) 2246 type = process_op(event, arg, &token); 2247 2248 if (type == EVENT_ERROR) 2249 goto out_free; 2250 2251 if (test_type_token(type, token, EVENT_DELIM, ",")) 2252 goto out_free; 2253 2254 field = calloc(1, sizeof(*field)); 2255 if (!field) 2256 goto out_free; 2257 2258 value = arg_eval(arg); 2259 if (value == NULL) 2260 goto out_free_field; 2261 field->value = strdup(value); 2262 if (field->value == NULL) 2263 goto out_free_field; 2264 2265 free_arg(arg); 2266 arg = alloc_arg(); 2267 if (!arg) 2268 goto out_free; 2269 2270 free_token(token); 2271 type = process_arg(event, arg, &token); 2272 if (test_type_token(type, token, EVENT_OP, "}")) 2273 goto out_free_field; 2274 2275 value = arg_eval(arg); 2276 if (value == NULL) 2277 goto out_free_field; 2278 field->str = strdup(value); 2279 if (field->str == NULL) 2280 goto out_free_field; 2281 free_arg(arg); 2282 arg = NULL; 2283 2284 *list = field; 2285 list = &field->next; 2286 2287 free_token(token); 2288 type = read_token_item(&token); 2289 } while (type == EVENT_DELIM && strcmp(token, ",") == 0); 2290 2291 *tok = token; 2292 return type; 2293 2294out_free_field: 2295 free_flag_sym(field); 2296out_free: 2297 free_arg(arg); 2298 free_token(token); 2299 *tok = NULL; 2300 2301 return EVENT_ERROR; 2302} 2303 2304static enum event_type 2305process_flags(struct event_format *event, struct print_arg *arg, char **tok) 2306{ 2307 struct print_arg *field; 2308 enum event_type type; 2309 char *token; 2310 2311 memset(arg, 0, sizeof(*arg)); 2312 arg->type = PRINT_FLAGS; 2313 2314 field = alloc_arg(); 2315 if (!field) { 2316 do_warning("%s: not enough memory!", __func__); 2317 goto out_free; 2318 } 2319 2320 type = process_arg(event, field, &token); 2321 2322 /* Handle operations in the first argument */ 2323 while (type == EVENT_OP) 2324 type = process_op(event, field, &token); 2325 2326 if (test_type_token(type, token, EVENT_DELIM, ",")) 2327 goto out_free_field; 2328 free_token(token); 2329 2330 arg->flags.field = field; 2331 2332 type = read_token_item(&token); 2333 if (event_item_type(type)) { 2334 arg->flags.delim = token; 2335 type = read_token_item(&token); 2336 } 2337 2338 if (test_type_token(type, token, EVENT_DELIM, ",")) 2339 goto out_free; 2340 2341 type = process_fields(event, &arg->flags.flags, &token); 2342 if (test_type_token(type, token, EVENT_DELIM, ")")) 2343 goto out_free; 2344 2345 free_token(token); 2346 type = read_token_item(tok); 2347 return type; 2348 2349out_free_field: 2350 free_arg(field); 2351out_free: 2352 free_token(token); 2353 *tok = NULL; 2354 return EVENT_ERROR; 2355} 2356 2357static enum event_type 2358process_symbols(struct event_format *event, struct print_arg *arg, char **tok) 2359{ 2360 struct print_arg *field; 2361 enum event_type type; 2362 char *token; 2363 2364 memset(arg, 0, sizeof(*arg)); 2365 arg->type = PRINT_SYMBOL; 2366 2367 field = alloc_arg(); 2368 if (!field) { 2369 do_warning("%s: not enough memory!", __func__); 2370 goto out_free; 2371 } 2372 2373 type = process_arg(event, field, &token); 2374 if (test_type_token(type, token, EVENT_DELIM, ",")) 2375 goto out_free_field; 2376 2377 arg->symbol.field = field; 2378 2379 type = process_fields(event, &arg->symbol.symbols, &token); 2380 if (test_type_token(type, token, EVENT_DELIM, ")")) 2381 goto out_free; 2382 2383 free_token(token); 2384 type = read_token_item(tok); 2385 return type; 2386 2387out_free_field: 2388 free_arg(field); 2389out_free: 2390 free_token(token); 2391 *tok = NULL; 2392 return EVENT_ERROR; 2393} 2394 2395static enum event_type 2396process_hex(struct event_format *event, struct print_arg *arg, char **tok) 2397{ 2398 struct print_arg *field; 2399 enum event_type type; 2400 char *token; 2401 2402 memset(arg, 0, sizeof(*arg)); 2403 arg->type = PRINT_HEX; 2404 2405 field = alloc_arg(); 2406 if (!field) { 2407 do_warning("%s: not enough memory!", __func__); 2408 goto out_free; 2409 } 2410 2411 type = process_arg(event, field, &token); 2412 2413 if (test_type_token(type, token, EVENT_DELIM, ",")) 2414 goto out_free; 2415 2416 arg->hex.field = field; 2417 2418 free_token(token); 2419 2420 field = alloc_arg(); 2421 if (!field) { 2422 do_warning("%s: not enough memory!", __func__); 2423 *tok = NULL; 2424 return EVENT_ERROR; 2425 } 2426 2427 type = process_arg(event, field, &token); 2428 2429 if (test_type_token(type, token, EVENT_DELIM, ")")) 2430 goto out_free; 2431 2432 arg->hex.size = field; 2433 2434 free_token(token); 2435 type = read_token_item(tok); 2436 return type; 2437 2438 out_free: 2439 free_arg(field); 2440 free_token(token); 2441 *tok = NULL; 2442 return EVENT_ERROR; 2443} 2444 2445static enum event_type 2446process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok) 2447{ 2448 struct format_field *field; 2449 enum event_type type; 2450 char *token; 2451 2452 memset(arg, 0, sizeof(*arg)); 2453 arg->type = PRINT_DYNAMIC_ARRAY; 2454 2455 /* 2456 * The item within the parenthesis is another field that holds 2457 * the index into where the array starts. 2458 */ 2459 type = read_token(&token); 2460 *tok = token; 2461 if (type != EVENT_ITEM) 2462 goto out_free; 2463 2464 /* Find the field */ 2465 2466 field = pevent_find_field(event, token); 2467 if (!field) 2468 goto out_free; 2469 2470 arg->dynarray.field = field; 2471 arg->dynarray.index = 0; 2472 2473 if (read_expected(EVENT_DELIM, ")") < 0) 2474 goto out_free; 2475 2476 free_token(token); 2477 type = read_token_item(&token); 2478 *tok = token; 2479 if (type != EVENT_OP || strcmp(token, "[") != 0) 2480 return type; 2481 2482 free_token(token); 2483 arg = alloc_arg(); 2484 if (!field) { 2485 do_warning("%s: not enough memory!", __func__); 2486 *tok = NULL; 2487 return EVENT_ERROR; 2488 } 2489 2490 type = process_arg(event, arg, &token); 2491 if (type == EVENT_ERROR) 2492 goto out_free_arg; 2493 2494 if (!test_type_token(type, token, EVENT_OP, "]")) 2495 goto out_free_arg; 2496 2497 free_token(token); 2498 type = read_token_item(tok); 2499 return type; 2500 2501 out_free_arg: 2502 free_arg(arg); 2503 out_free: 2504 free_token(token); 2505 *tok = NULL; 2506 return EVENT_ERROR; 2507} 2508 2509static enum event_type 2510process_paren(struct event_format *event, struct print_arg *arg, char **tok) 2511{ 2512 struct print_arg *item_arg; 2513 enum event_type type; 2514 char *token; 2515 2516 type = process_arg(event, arg, &token); 2517 2518 if (type == EVENT_ERROR) 2519 goto out_free; 2520 2521 if (type == EVENT_OP) 2522 type = process_op(event, arg, &token); 2523 2524 if (type == EVENT_ERROR) 2525 goto out_free; 2526 2527 if (test_type_token(type, token, EVENT_DELIM, ")")) 2528 goto out_free; 2529 2530 free_token(token); 2531 type = read_token_item(&token); 2532 2533 /* 2534 * If the next token is an item or another open paren, then 2535 * this was a typecast. 2536 */ 2537 if (event_item_type(type) || 2538 (type == EVENT_DELIM && strcmp(token, "(") == 0)) { 2539 2540 /* make this a typecast and contine */ 2541 2542 /* prevous must be an atom */ 2543 if (arg->type != PRINT_ATOM) { 2544 do_warning("previous needed to be PRINT_ATOM"); 2545 goto out_free; 2546 } 2547 2548 item_arg = alloc_arg(); 2549 if (!item_arg) { 2550 do_warning("%s: not enough memory!", __func__); 2551 goto out_free; 2552 } 2553 2554 arg->type = PRINT_TYPE; 2555 arg->typecast.type = arg->atom.atom; 2556 arg->typecast.item = item_arg; 2557 type = process_arg_token(event, item_arg, &token, type); 2558 2559 } 2560 2561 *tok = token; 2562 return type; 2563 2564 out_free: 2565 free_token(token); 2566 *tok = NULL; 2567 return EVENT_ERROR; 2568} 2569 2570 2571static enum event_type 2572process_str(struct event_format *event __maybe_unused, struct print_arg *arg, 2573 char **tok) 2574{ 2575 enum event_type type; 2576 char *token; 2577 2578 if (read_expect_type(EVENT_ITEM, &token) < 0) 2579 goto out_free; 2580 2581 arg->type = PRINT_STRING; 2582 arg->string.string = token; 2583 arg->string.offset = -1; 2584 2585 if (read_expected(EVENT_DELIM, ")") < 0) 2586 goto out_err; 2587 2588 type = read_token(&token); 2589 *tok = token; 2590 2591 return type; 2592 2593 out_free: 2594 free_token(token); 2595 out_err: 2596 *tok = NULL; 2597 return EVENT_ERROR; 2598} 2599 2600static struct pevent_function_handler * 2601find_func_handler(struct pevent *pevent, char *func_name) 2602{ 2603 struct pevent_function_handler *func; 2604 2605 for (func = pevent->func_handlers; func; func = func->next) { 2606 if (strcmp(func->name, func_name) == 0) 2607 break; 2608 } 2609 2610 return func; 2611} 2612 2613static void remove_func_handler(struct pevent *pevent, char *func_name) 2614{ 2615 struct pevent_function_handler *func; 2616 struct pevent_function_handler **next; 2617 2618 next = &pevent->func_handlers; 2619 while ((func = *next)) { 2620 if (strcmp(func->name, func_name) == 0) { 2621 *next = func->next; 2622 free_func_handle(func); 2623 break; 2624 } 2625 next = &func->next; 2626 } 2627} 2628 2629static enum event_type 2630process_func_handler(struct event_format *event, struct pevent_function_handler *func, 2631 struct print_arg *arg, char **tok) 2632{ 2633 struct print_arg **next_arg; 2634 struct print_arg *farg; 2635 enum event_type type; 2636 char *token; 2637 char *test; 2638 int i; 2639 2640 arg->type = PRINT_FUNC; 2641 arg->func.func = func; 2642 2643 *tok = NULL; 2644 2645 next_arg = &(arg->func.args); 2646 for (i = 0; i < func->nr_args; i++) { 2647 farg = alloc_arg(); 2648 if (!farg) { 2649 do_warning("%s: not enough memory!", __func__); 2650 return EVENT_ERROR; 2651 } 2652 2653 type = process_arg(event, farg, &token); 2654 if (i < (func->nr_args - 1)) 2655 test = ","; 2656 else 2657 test = ")"; 2658 2659 if (test_type_token(type, token, EVENT_DELIM, test)) { 2660 free_arg(farg); 2661 free_token(token); 2662 return EVENT_ERROR; 2663 } 2664 2665 *next_arg = farg; 2666 next_arg = &(farg->next); 2667 free_token(token); 2668 } 2669 2670 type = read_token(&token); 2671 *tok = token; 2672 2673 return type; 2674} 2675 2676static enum event_type 2677process_function(struct event_format *event, struct print_arg *arg, 2678 char *token, char **tok) 2679{ 2680 struct pevent_function_handler *func; 2681 2682 if (strcmp(token, "__print_flags") == 0) { 2683 free_token(token); 2684 is_flag_field = 1; 2685 return process_flags(event, arg, tok); 2686 } 2687 if (strcmp(token, "__print_symbolic") == 0) { 2688 free_token(token); 2689 is_symbolic_field = 1; 2690 return process_symbols(event, arg, tok); 2691 } 2692 if (strcmp(token, "__print_hex") == 0) { 2693 free_token(token); 2694 return process_hex(event, arg, tok); 2695 } 2696 if (strcmp(token, "__get_str") == 0) { 2697 free_token(token); 2698 return process_str(event, arg, tok); 2699 } 2700 if (strcmp(token, "__get_dynamic_array") == 0) { 2701 free_token(token); 2702 return process_dynamic_array(event, arg, tok); 2703 } 2704 2705 func = find_func_handler(event->pevent, token); 2706 if (func) { 2707 free_token(token); 2708 return process_func_handler(event, func, arg, tok); 2709 } 2710 2711 do_warning("function %s not defined", token); 2712 free_token(token); 2713 return EVENT_ERROR; 2714} 2715 2716static enum event_type 2717process_arg_token(struct event_format *event, struct print_arg *arg, 2718 char **tok, enum event_type type) 2719{ 2720 char *token; 2721 char *atom; 2722 2723 token = *tok; 2724 2725 switch (type) { 2726 case EVENT_ITEM: 2727 if (strcmp(token, "REC") == 0) { 2728 free_token(token); 2729 type = process_entry(event, arg, &token); 2730 break; 2731 } 2732 atom = token; 2733 /* test the next token */ 2734 type = read_token_item(&token); 2735 2736 /* 2737 * If the next token is a parenthesis, then this 2738 * is a function. 2739 */ 2740 if (type == EVENT_DELIM && strcmp(token, "(") == 0) { 2741 free_token(token); 2742 token = NULL; 2743 /* this will free atom. */ 2744 type = process_function(event, arg, atom, &token); 2745 break; 2746 } 2747 /* atoms can be more than one token long */ 2748 while (type == EVENT_ITEM) { 2749 char *new_atom; 2750 new_atom = realloc(atom, 2751 strlen(atom) + strlen(token) + 2); 2752 if (!new_atom) { 2753 free(atom); 2754 *tok = NULL; 2755 free_token(token); 2756 return EVENT_ERROR; 2757 } 2758 atom = new_atom; 2759 strcat(atom, " "); 2760 strcat(atom, token); 2761 free_token(token); 2762 type = read_token_item(&token); 2763 } 2764 2765 arg->type = PRINT_ATOM; 2766 arg->atom.atom = atom; 2767 break; 2768 2769 case EVENT_DQUOTE: 2770 case EVENT_SQUOTE: 2771 arg->type = PRINT_ATOM; 2772 arg->atom.atom = token; 2773 type = read_token_item(&token); 2774 break; 2775 case EVENT_DELIM: 2776 if (strcmp(token, "(") == 0) { 2777 free_token(token); 2778 type = process_paren(event, arg, &token); 2779 break; 2780 } 2781 case EVENT_OP: 2782 /* handle single ops */ 2783 arg->type = PRINT_OP; 2784 arg->op.op = token; 2785 arg->op.left = NULL; 2786 type = process_op(event, arg, &token); 2787 2788 /* On error, the op is freed */ 2789 if (type == EVENT_ERROR) 2790 arg->op.op = NULL; 2791 2792 /* return error type if errored */ 2793 break; 2794 2795 case EVENT_ERROR ... EVENT_NEWLINE: 2796 default: 2797 do_warning("unexpected type %d", type); 2798 return EVENT_ERROR; 2799 } 2800 *tok = token; 2801 2802 return type; 2803} 2804 2805static int event_read_print_args(struct event_format *event, struct print_arg **list) 2806{ 2807 enum event_type type = EVENT_ERROR; 2808 struct print_arg *arg; 2809 char *token; 2810 int args = 0; 2811 2812 do { 2813 if (type == EVENT_NEWLINE) { 2814 type = read_token_item(&token); 2815 continue; 2816 } 2817 2818 arg = alloc_arg(); 2819 if (!arg) { 2820 do_warning("%s: not enough memory!", __func__); 2821 return -1; 2822 } 2823 2824 type = process_arg(event, arg, &token); 2825 2826 if (type == EVENT_ERROR) { 2827 free_token(token); 2828 free_arg(arg); 2829 return -1; 2830 } 2831 2832 *list = arg; 2833 args++; 2834 2835 if (type == EVENT_OP) { 2836 type = process_op(event, arg, &token); 2837 free_token(token); 2838 if (type == EVENT_ERROR) { 2839 *list = NULL; 2840 free_arg(arg); 2841 return -1; 2842 } 2843 list = &arg->next; 2844 continue; 2845 } 2846 2847 if (type == EVENT_DELIM && strcmp(token, ",") == 0) { 2848 free_token(token); 2849 *list = arg; 2850 list = &arg->next; 2851 continue; 2852 } 2853 break; 2854 } while (type != EVENT_NONE); 2855 2856 if (type != EVENT_NONE && type != EVENT_ERROR) 2857 free_token(token); 2858 2859 return args; 2860} 2861 2862static int event_read_print(struct event_format *event) 2863{ 2864 enum event_type type; 2865 char *token; 2866 int ret; 2867 2868 if (read_expected_item(EVENT_ITEM, "print") < 0) 2869 return -1; 2870 2871 if (read_expected(EVENT_ITEM, "fmt") < 0) 2872 return -1; 2873 2874 if (read_expected(EVENT_OP, ":") < 0) 2875 return -1; 2876 2877 if (read_expect_type(EVENT_DQUOTE, &token) < 0) 2878 goto fail; 2879 2880 concat: 2881 event->print_fmt.format = token; 2882 event->print_fmt.args = NULL; 2883 2884 /* ok to have no arg */ 2885 type = read_token_item(&token); 2886 2887 if (type == EVENT_NONE) 2888 return 0; 2889 2890 /* Handle concatenation of print lines */ 2891 if (type == EVENT_DQUOTE) { 2892 char *cat; 2893 2894 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0) 2895 goto fail; 2896 free_token(token); 2897 free_token(event->print_fmt.format); 2898 event->print_fmt.format = NULL; 2899 token = cat; 2900 goto concat; 2901 } 2902 2903 if (test_type_token(type, token, EVENT_DELIM, ",")) 2904 goto fail; 2905 2906 free_token(token); 2907 2908 ret = event_read_print_args(event, &event->print_fmt.args); 2909 if (ret < 0) 2910 return -1; 2911 2912 return ret; 2913 2914 fail: 2915 free_token(token); 2916 return -1; 2917} 2918 2919/** 2920 * pevent_find_common_field - return a common field by event 2921 * @event: handle for the event 2922 * @name: the name of the common field to return 2923 * 2924 * Returns a common field from the event by the given @name. 2925 * This only searchs the common fields and not all field. 2926 */ 2927struct format_field * 2928pevent_find_common_field(struct event_format *event, const char *name) 2929{ 2930 struct format_field *format; 2931 2932 for (format = event->format.common_fields; 2933 format; format = format->next) { 2934 if (strcmp(format->name, name) == 0) 2935 break; 2936 } 2937 2938 return format; 2939} 2940 2941/** 2942 * pevent_find_field - find a non-common field 2943 * @event: handle for the event 2944 * @name: the name of the non-common field 2945 * 2946 * Returns a non-common field by the given @name. 2947 * This does not search common fields. 2948 */ 2949struct format_field * 2950pevent_find_field(struct event_format *event, const char *name) 2951{ 2952 struct format_field *format; 2953 2954 for (format = event->format.fields; 2955 format; format = format->next) { 2956 if (strcmp(format->name, name) == 0) 2957 break; 2958 } 2959 2960 return format; 2961} 2962 2963/** 2964 * pevent_find_any_field - find any field by name 2965 * @event: handle for the event 2966 * @name: the name of the field 2967 * 2968 * Returns a field by the given @name. 2969 * This searchs the common field names first, then 2970 * the non-common ones if a common one was not found. 2971 */ 2972struct format_field * 2973pevent_find_any_field(struct event_format *event, const char *name) 2974{ 2975 struct format_field *format; 2976 2977 format = pevent_find_common_field(event, name); 2978 if (format) 2979 return format; 2980 return pevent_find_field(event, name); 2981} 2982 2983/** 2984 * pevent_read_number - read a number from data 2985 * @pevent: handle for the pevent 2986 * @ptr: the raw data 2987 * @size: the size of the data that holds the number 2988 * 2989 * Returns the number (converted to host) from the 2990 * raw data. 2991 */ 2992unsigned long long pevent_read_number(struct pevent *pevent, 2993 const void *ptr, int size) 2994{ 2995 switch (size) { 2996 case 1: 2997 return *(unsigned char *)ptr; 2998 case 2: 2999 return data2host2(pevent, ptr); 3000 case 4: 3001 return data2host4(pevent, ptr); 3002 case 8: 3003 return data2host8(pevent, ptr); 3004 default: 3005 /* BUG! */ 3006 return 0; 3007 } 3008} 3009 3010/** 3011 * pevent_read_number_field - read a number from data 3012 * @field: a handle to the field 3013 * @data: the raw data to read 3014 * @value: the value to place the number in 3015 * 3016 * Reads raw data according to a field offset and size, 3017 * and translates it into @value. 3018 * 3019 * Returns 0 on success, -1 otherwise. 3020 */ 3021int pevent_read_number_field(struct format_field *field, const void *data, 3022 unsigned long long *value) 3023{ 3024 if (!field) 3025 return -1; 3026 switch (field->size) { 3027 case 1: 3028 case 2: 3029 case 4: 3030 case 8: 3031 *value = pevent_read_number(field->event->pevent, 3032 data + field->offset, field->size); 3033 return 0; 3034 default: 3035 return -1; 3036 } 3037} 3038 3039static int get_common_info(struct pevent *pevent, 3040 const char *type, int *offset, int *size) 3041{ 3042 struct event_format *event; 3043 struct format_field *field; 3044 3045 /* 3046 * All events should have the same common elements. 3047 * Pick any event to find where the type is; 3048 */ 3049 if (!pevent->events) { 3050 do_warning("no event_list!"); 3051 return -1; 3052 } 3053 3054 event = pevent->events[0]; 3055 field = pevent_find_common_field(event, type); 3056 if (!field) 3057 return -1; 3058 3059 *offset = field->offset; 3060 *size = field->size; 3061 3062 return 0; 3063} 3064 3065static int __parse_common(struct pevent *pevent, void *data, 3066 int *size, int *offset, const char *name) 3067{ 3068 int ret; 3069 3070 if (!*size) { 3071 ret = get_common_info(pevent, name, offset, size); 3072 if (ret < 0) 3073 return ret; 3074 } 3075 return pevent_read_number(pevent, data + *offset, *size); 3076} 3077 3078static int trace_parse_common_type(struct pevent *pevent, void *data) 3079{ 3080 return __parse_common(pevent, data, 3081 &pevent->type_size, &pevent->type_offset, 3082 "common_type"); 3083} 3084 3085static int parse_common_pid(struct pevent *pevent, void *data) 3086{ 3087 return __parse_common(pevent, data, 3088 &pevent->pid_size, &pevent->pid_offset, 3089 "common_pid"); 3090} 3091 3092static int parse_common_pc(struct pevent *pevent, void *data) 3093{ 3094 return __parse_common(pevent, data, 3095 &pevent->pc_size, &pevent->pc_offset, 3096 "common_preempt_count"); 3097} 3098 3099static int parse_common_flags(struct pevent *pevent, void *data) 3100{ 3101 return __parse_common(pevent, data, 3102 &pevent->flags_size, &pevent->flags_offset, 3103 "common_flags"); 3104} 3105 3106static int parse_common_lock_depth(struct pevent *pevent, void *data) 3107{ 3108 return __parse_common(pevent, data, 3109 &pevent->ld_size, &pevent->ld_offset, 3110 "common_lock_depth"); 3111} 3112 3113static int parse_common_migrate_disable(struct pevent *pevent, void *data) 3114{ 3115 return __parse_common(pevent, data, 3116 &pevent->ld_size, &pevent->ld_offset, 3117 "common_migrate_disable"); 3118} 3119 3120static int events_id_cmp(const void *a, const void *b); 3121 3122/** 3123 * pevent_find_event - find an event by given id 3124 * @pevent: a handle to the pevent 3125 * @id: the id of the event 3126 * 3127 * Returns an event that has a given @id. 3128 */ 3129struct event_format *pevent_find_event(struct pevent *pevent, int id) 3130{ 3131 struct event_format **eventptr; 3132 struct event_format key; 3133 struct event_format *pkey = &key; 3134 3135 /* Check cache first */ 3136 if (pevent->last_event && pevent->last_event->id == id) 3137 return pevent->last_event; 3138 3139 key.id = id; 3140 3141 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events, 3142 sizeof(*pevent->events), events_id_cmp); 3143 3144 if (eventptr) { 3145 pevent->last_event = *eventptr; 3146 return *eventptr; 3147 } 3148 3149 return NULL; 3150} 3151 3152/** 3153 * pevent_find_event_by_name - find an event by given name 3154 * @pevent: a handle to the pevent 3155 * @sys: the system name to search for 3156 * @name: the name of the event to search for 3157 * 3158 * This returns an event with a given @name and under the system 3159 * @sys. If @sys is NULL the first event with @name is returned. 3160 */ 3161struct event_format * 3162pevent_find_event_by_name(struct pevent *pevent, 3163 const char *sys, const char *name) 3164{ 3165 struct event_format *event; 3166 int i; 3167 3168 if (pevent->last_event && 3169 strcmp(pevent->last_event->name, name) == 0 && 3170 (!sys || strcmp(pevent->last_event->system, sys) == 0)) 3171 return pevent->last_event; 3172 3173 for (i = 0; i < pevent->nr_events; i++) { 3174 event = pevent->events[i]; 3175 if (strcmp(event->name, name) == 0) { 3176 if (!sys) 3177 break; 3178 if (strcmp(event->system, sys) == 0) 3179 break; 3180 } 3181 } 3182 if (i == pevent->nr_events) 3183 event = NULL; 3184 3185 pevent->last_event = event; 3186 return event; 3187} 3188 3189static unsigned long long 3190eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg) 3191{ 3192 struct pevent *pevent = event->pevent; 3193 unsigned long long val = 0; 3194 unsigned long long left, right; 3195 struct print_arg *typearg = NULL; 3196 struct print_arg *larg; 3197 unsigned long offset; 3198 unsigned int field_size; 3199 3200 switch (arg->type) { 3201 case PRINT_NULL: 3202 /* ?? */ 3203 return 0; 3204 case PRINT_ATOM: 3205 return strtoull(arg->atom.atom, NULL, 0); 3206 case PRINT_FIELD: 3207 if (!arg->field.field) { 3208 arg->field.field = pevent_find_any_field(event, arg->field.name); 3209 if (!arg->field.field) 3210 goto out_warning_field; 3211 3212 } 3213 /* must be a number */ 3214 val = pevent_read_number(pevent, data + arg->field.field->offset, 3215 arg->field.field->size); 3216 break; 3217 case PRINT_FLAGS: 3218 case PRINT_SYMBOL: 3219 case PRINT_HEX: 3220 break; 3221 case PRINT_TYPE: 3222 val = eval_num_arg(data, size, event, arg->typecast.item); 3223 return eval_type(val, arg, 0); 3224 case PRINT_STRING: 3225 case PRINT_BSTRING: 3226 return 0; 3227 case PRINT_FUNC: { 3228 struct trace_seq s; 3229 trace_seq_init(&s); 3230 val = process_defined_func(&s, data, size, event, arg); 3231 trace_seq_destroy(&s); 3232 return val; 3233 } 3234 case PRINT_OP: 3235 if (strcmp(arg->op.op, "[") == 0) { 3236 /* 3237 * Arrays are special, since we don't want 3238 * to read the arg as is. 3239 */ 3240 right = eval_num_arg(data, size, event, arg->op.right); 3241 3242 /* handle typecasts */ 3243 larg = arg->op.left; 3244 while (larg->type == PRINT_TYPE) { 3245 if (!typearg) 3246 typearg = larg; 3247 larg = larg->typecast.item; 3248 } 3249 3250 /* Default to long size */ 3251 field_size = pevent->long_size; 3252 3253 switch (larg->type) { 3254 case PRINT_DYNAMIC_ARRAY: 3255 offset = pevent_read_number(pevent, 3256 data + larg->dynarray.field->offset, 3257 larg->dynarray.field->size); 3258 if (larg->dynarray.field->elementsize) 3259 field_size = larg->dynarray.field->elementsize; 3260 /* 3261 * The actual length of the dynamic array is stored 3262 * in the top half of the field, and the offset 3263 * is in the bottom half of the 32 bit field. 3264 */ 3265 offset &= 0xffff; 3266 offset += right; 3267 break; 3268 case PRINT_FIELD: 3269 if (!larg->field.field) { 3270 larg->field.field = 3271 pevent_find_any_field(event, larg->field.name); 3272 if (!larg->field.field) { 3273 arg = larg; 3274 goto out_warning_field; 3275 } 3276 } 3277 field_size = larg->field.field->elementsize; 3278 offset = larg->field.field->offset + 3279 right * larg->field.field->elementsize; 3280 break; 3281 default: 3282 goto default_op; /* oops, all bets off */ 3283 } 3284 val = pevent_read_number(pevent, 3285 data + offset, field_size); 3286 if (typearg) 3287 val = eval_type(val, typearg, 1); 3288 break; 3289 } else if (strcmp(arg->op.op, "?") == 0) { 3290 left = eval_num_arg(data, size, event, arg->op.left); 3291 arg = arg->op.right; 3292 if (left) 3293 val = eval_num_arg(data, size, event, arg->op.left); 3294 else 3295 val = eval_num_arg(data, size, event, arg->op.right); 3296 break; 3297 } 3298 default_op: 3299 left = eval_num_arg(data, size, event, arg->op.left); 3300 right = eval_num_arg(data, size, event, arg->op.right); 3301 switch (arg->op.op[0]) { 3302 case '!': 3303 switch (arg->op.op[1]) { 3304 case 0: 3305 val = !right; 3306 break; 3307 case '=': 3308 val = left != right; 3309 break; 3310 default: 3311 goto out_warning_op; 3312 } 3313 break; 3314 case '~': 3315 val = ~right; 3316 break; 3317 case '|': 3318 if (arg->op.op[1]) 3319 val = left || right; 3320 else 3321 val = left | right; 3322 break; 3323 case '&': 3324 if (arg->op.op[1]) 3325 val = left && right; 3326 else 3327 val = left & right; 3328 break; 3329 case '<': 3330 switch (arg->op.op[1]) { 3331 case 0: 3332 val = left < right; 3333 break; 3334 case '<': 3335 val = left << right; 3336 break; 3337 case '=': 3338 val = left <= right; 3339 break; 3340 default: 3341 goto out_warning_op; 3342 } 3343 break; 3344 case '>': 3345 switch (arg->op.op[1]) { 3346 case 0: 3347 val = left > right; 3348 break; 3349 case '>': 3350 val = left >> right; 3351 break; 3352 case '=': 3353 val = left >= right; 3354 break; 3355 default: 3356 goto out_warning_op; 3357 } 3358 break; 3359 case '=': 3360 if (arg->op.op[1] != '=') 3361 goto out_warning_op; 3362 3363 val = left == right; 3364 break; 3365 case '-': 3366 val = left - right; 3367 break; 3368 case '+': 3369 val = left + right; 3370 break; 3371 case '/': 3372 val = left / right; 3373 break; 3374 case '*': 3375 val = left * right; 3376 break; 3377 default: 3378 goto out_warning_op; 3379 } 3380 break; 3381 default: /* not sure what to do there */ 3382 return 0; 3383 } 3384 return val; 3385 3386out_warning_op: 3387 do_warning("%s: unknown op '%s'", __func__, arg->op.op); 3388 return 0; 3389 3390out_warning_field: 3391 do_warning("%s: field %s not found", __func__, arg->field.name); 3392 return 0; 3393} 3394 3395struct flag { 3396 const char *name; 3397 unsigned long long value; 3398}; 3399 3400static const struct flag flags[] = { 3401 { "HI_SOFTIRQ", 0 }, 3402 { "TIMER_SOFTIRQ", 1 }, 3403 { "NET_TX_SOFTIRQ", 2 }, 3404 { "NET_RX_SOFTIRQ", 3 }, 3405 { "BLOCK_SOFTIRQ", 4 }, 3406 { "BLOCK_IOPOLL_SOFTIRQ", 5 }, 3407 { "TASKLET_SOFTIRQ", 6 }, 3408 { "SCHED_SOFTIRQ", 7 }, 3409 { "HRTIMER_SOFTIRQ", 8 }, 3410 { "RCU_SOFTIRQ", 9 }, 3411 3412 { "HRTIMER_NORESTART", 0 }, 3413 { "HRTIMER_RESTART", 1 }, 3414}; 3415 3416static unsigned long long eval_flag(const char *flag) 3417{ 3418 int i; 3419 3420 /* 3421 * Some flags in the format files do not get converted. 3422 * If the flag is not numeric, see if it is something that 3423 * we already know about. 3424 */ 3425 if (isdigit(flag[0])) 3426 return strtoull(flag, NULL, 0); 3427 3428 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) 3429 if (strcmp(flags[i].name, flag) == 0) 3430 return flags[i].value; 3431 3432 return 0; 3433} 3434 3435static void print_str_to_seq(struct trace_seq *s, const char *format, 3436 int len_arg, const char *str) 3437{ 3438 if (len_arg >= 0) 3439 trace_seq_printf(s, format, len_arg, str); 3440 else 3441 trace_seq_printf(s, format, str); 3442} 3443 3444static void print_str_arg(struct trace_seq *s, void *data, int size, 3445 struct event_format *event, const char *format, 3446 int len_arg, struct print_arg *arg) 3447{ 3448 struct pevent *pevent = event->pevent; 3449 struct print_flag_sym *flag; 3450 struct format_field *field; 3451 unsigned long long val, fval; 3452 unsigned long addr; 3453 char *str; 3454 unsigned char *hex; 3455 int print; 3456 int i, len; 3457 3458 switch (arg->type) { 3459 case PRINT_NULL: 3460 /* ?? */ 3461 return; 3462 case PRINT_ATOM: 3463 print_str_to_seq(s, format, len_arg, arg->atom.atom); 3464 return; 3465 case PRINT_FIELD: 3466 field = arg->field.field; 3467 if (!field) { 3468 field = pevent_find_any_field(event, arg->field.name); 3469 if (!field) { 3470 str = arg->field.name; 3471 goto out_warning_field; 3472 } 3473 arg->field.field = field; 3474 } 3475 /* Zero sized fields, mean the rest of the data */ 3476 len = field->size ? : size - field->offset; 3477 3478 /* 3479 * Some events pass in pointers. If this is not an array 3480 * and the size is the same as long_size, assume that it 3481 * is a pointer. 3482 */ 3483 if (!(field->flags & FIELD_IS_ARRAY) && 3484 field->size == pevent->long_size) { 3485 addr = *(unsigned long *)(data + field->offset); 3486 trace_seq_printf(s, "%lx", addr); 3487 break; 3488 } 3489 str = malloc(len + 1); 3490 if (!str) { 3491 do_warning("%s: not enough memory!", __func__); 3492 return; 3493 } 3494 memcpy(str, data + field->offset, len); 3495 str[len] = 0; 3496 print_str_to_seq(s, format, len_arg, str); 3497 free(str); 3498 break; 3499 case PRINT_FLAGS: 3500 val = eval_num_arg(data, size, event, arg->flags.field); 3501 print = 0; 3502 for (flag = arg->flags.flags; flag; flag = flag->next) { 3503 fval = eval_flag(flag->value); 3504 if (!val && !fval) { 3505 print_str_to_seq(s, format, len_arg, flag->str); 3506 break; 3507 } 3508 if (fval && (val & fval) == fval) { 3509 if (print && arg->flags.delim) 3510 trace_seq_puts(s, arg->flags.delim); 3511 print_str_to_seq(s, format, len_arg, flag->str); 3512 print = 1; 3513 val &= ~fval; 3514 } 3515 } 3516 break; 3517 case PRINT_SYMBOL: 3518 val = eval_num_arg(data, size, event, arg->symbol.field); 3519 for (flag = arg->symbol.symbols; flag; flag = flag->next) { 3520 fval = eval_flag(flag->value); 3521 if (val == fval) { 3522 print_str_to_seq(s, format, len_arg, flag->str); 3523 break; 3524 } 3525 } 3526 break; 3527 case PRINT_HEX: 3528 field = arg->hex.field->field.field; 3529 if (!field) { 3530 str = arg->hex.field->field.name; 3531 field = pevent_find_any_field(event, str); 3532 if (!field) 3533 goto out_warning_field; 3534 arg->hex.field->field.field = field; 3535 } 3536 hex = data + field->offset; 3537 len = eval_num_arg(data, size, event, arg->hex.size); 3538 for (i = 0; i < len; i++) { 3539 if (i) 3540 trace_seq_putc(s, ' '); 3541 trace_seq_printf(s, "%02x", hex[i]); 3542 } 3543 break; 3544 3545 case PRINT_TYPE: 3546 break; 3547 case PRINT_STRING: { 3548 int str_offset; 3549 3550 if (arg->string.offset == -1) { 3551 struct format_field *f; 3552 3553 f = pevent_find_any_field(event, arg->string.string); 3554 arg->string.offset = f->offset; 3555 } 3556 str_offset = data2host4(pevent, data + arg->string.offset); 3557 str_offset &= 0xffff; 3558 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset); 3559 break; 3560 } 3561 case PRINT_BSTRING: 3562 print_str_to_seq(s, format, len_arg, arg->string.string); 3563 break; 3564 case PRINT_OP: 3565 /* 3566 * The only op for string should be ? : 3567 */ 3568 if (arg->op.op[0] != '?') 3569 return; 3570 val = eval_num_arg(data, size, event, arg->op.left); 3571 if (val) 3572 print_str_arg(s, data, size, event, 3573 format, len_arg, arg->op.right->op.left); 3574 else 3575 print_str_arg(s, data, size, event, 3576 format, len_arg, arg->op.right->op.right); 3577 break; 3578 case PRINT_FUNC: 3579 process_defined_func(s, data, size, event, arg); 3580 break; 3581 default: 3582 /* well... */ 3583 break; 3584 } 3585 3586 return; 3587 3588out_warning_field: 3589 do_warning("%s: field %s not found", __func__, arg->field.name); 3590} 3591 3592static unsigned long long 3593process_defined_func(struct trace_seq *s, void *data, int size, 3594 struct event_format *event, struct print_arg *arg) 3595{ 3596 struct pevent_function_handler *func_handle = arg->func.func; 3597 struct pevent_func_params *param; 3598 unsigned long long *args; 3599 unsigned long long ret; 3600 struct print_arg *farg; 3601 struct trace_seq str; 3602 struct save_str { 3603 struct save_str *next; 3604 char *str; 3605 } *strings = NULL, *string; 3606 int i; 3607 3608 if (!func_handle->nr_args) { 3609 ret = (*func_handle->func)(s, NULL); 3610 goto out; 3611 } 3612 3613 farg = arg->func.args; 3614 param = func_handle->params; 3615 3616 ret = ULLONG_MAX; 3617 args = malloc(sizeof(*args) * func_handle->nr_args); 3618 if (!args) 3619 goto out; 3620 3621 for (i = 0; i < func_handle->nr_args; i++) { 3622 switch (param->type) { 3623 case PEVENT_FUNC_ARG_INT: 3624 case PEVENT_FUNC_ARG_LONG: 3625 case PEVENT_FUNC_ARG_PTR: 3626 args[i] = eval_num_arg(data, size, event, farg); 3627 break; 3628 case PEVENT_FUNC_ARG_STRING: 3629 trace_seq_init(&str); 3630 print_str_arg(&str, data, size, event, "%s", -1, farg); 3631 trace_seq_terminate(&str); 3632 string = malloc(sizeof(*string)); 3633 if (!string) { 3634 do_warning("%s(%d): malloc str", __func__, __LINE__); 3635 goto out_free; 3636 } 3637 string->next = strings; 3638 string->str = strdup(str.buffer); 3639 if (!string->str) { 3640 free(string); 3641 do_warning("%s(%d): malloc str", __func__, __LINE__); 3642 goto out_free; 3643 } 3644 args[i] = (uintptr_t)string->str; 3645 strings = string; 3646 trace_seq_destroy(&str); 3647 break; 3648 default: 3649 /* 3650 * Something went totally wrong, this is not 3651 * an input error, something in this code broke. 3652 */ 3653 do_warning("Unexpected end of arguments\n"); 3654 goto out_free; 3655 } 3656 farg = farg->next; 3657 param = param->next; 3658 } 3659 3660 ret = (*func_handle->func)(s, args); 3661out_free: 3662 free(args); 3663 while (strings) { 3664 string = strings; 3665 strings = string->next; 3666 free(string->str); 3667 free(string); 3668 } 3669 3670 out: 3671 /* TBD : handle return type here */ 3672 return ret; 3673} 3674 3675static void free_args(struct print_arg *args) 3676{ 3677 struct print_arg *next; 3678 3679 while (args) { 3680 next = args->next; 3681 3682 free_arg(args); 3683 args = next; 3684 } 3685} 3686 3687static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event) 3688{ 3689 struct pevent *pevent = event->pevent; 3690 struct format_field *field, *ip_field; 3691 struct print_arg *args, *arg, **next; 3692 unsigned long long ip, val; 3693 char *ptr; 3694 void *bptr; 3695 int vsize; 3696 3697 field = pevent->bprint_buf_field; 3698 ip_field = pevent->bprint_ip_field; 3699 3700 if (!field) { 3701 field = pevent_find_field(event, "buf"); 3702 if (!field) { 3703 do_warning("can't find buffer field for binary printk"); 3704 return NULL; 3705 } 3706 ip_field = pevent_find_field(event, "ip"); 3707 if (!ip_field) { 3708 do_warning("can't find ip field for binary printk"); 3709 return NULL; 3710 } 3711 pevent->bprint_buf_field = field; 3712 pevent->bprint_ip_field = ip_field; 3713 } 3714 3715 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size); 3716 3717 /* 3718 * The first arg is the IP pointer. 3719 */ 3720 args = alloc_arg(); 3721 if (!args) { 3722 do_warning("%s(%d): not enough memory!", __func__, __LINE__); 3723 return NULL; 3724 } 3725 arg = args; 3726 arg->next = NULL; 3727 next = &arg->next; 3728 3729 arg->type = PRINT_ATOM; 3730 3731 if (asprintf(&arg->atom.atom, "%lld", ip) < 0) 3732 goto out_free; 3733 3734 /* skip the first "%pf : " */ 3735 for (ptr = fmt + 6, bptr = data + field->offset; 3736 bptr < data + size && *ptr; ptr++) { 3737 int ls = 0; 3738 3739 if (*ptr == '%') { 3740 process_again: 3741 ptr++; 3742 switch (*ptr) { 3743 case '%': 3744 break; 3745 case 'l': 3746 ls++; 3747 goto process_again; 3748 case 'L': 3749 ls = 2; 3750 goto process_again; 3751 case '0' ... '9': 3752 goto process_again; 3753 case '.': 3754 goto process_again; 3755 case 'p': 3756 ls = 1; 3757 /* fall through */ 3758 case 'd': 3759 case 'u': 3760 case 'x': 3761 case 'i': 3762 switch (ls) { 3763 case 0: 3764 vsize = 4; 3765 break; 3766 case 1: 3767 vsize = pevent->long_size; 3768 break; 3769 case 2: 3770 vsize = 8; 3771 break; 3772 default: 3773 vsize = ls; /* ? */ 3774 break; 3775 } 3776 /* fall through */ 3777 case '*': 3778 if (*ptr == '*') 3779 vsize = 4; 3780 3781 /* the pointers are always 4 bytes aligned */ 3782 bptr = (void *)(((unsigned long)bptr + 3) & 3783 ~3); 3784 val = pevent_read_number(pevent, bptr, vsize); 3785 bptr += vsize; 3786 arg = alloc_arg(); 3787 if (!arg) { 3788 do_warning("%s(%d): not enough memory!", 3789 __func__, __LINE__); 3790 goto out_free; 3791 } 3792 arg->next = NULL; 3793 arg->type = PRINT_ATOM; 3794 if (asprintf(&arg->atom.atom, "%lld", val) < 0) { 3795 free(arg); 3796 goto out_free; 3797 } 3798 *next = arg; 3799 next = &arg->next; 3800 /* 3801 * The '*' case means that an arg is used as the length. 3802 * We need to continue to figure out for what. 3803 */ 3804 if (*ptr == '*') 3805 goto process_again; 3806 3807 break; 3808 case 's': 3809 arg = alloc_arg(); 3810 if (!arg) { 3811 do_warning("%s(%d): not enough memory!", 3812 __func__, __LINE__); 3813 goto out_free; 3814 } 3815 arg->next = NULL; 3816 arg->type = PRINT_BSTRING; 3817 arg->string.string = strdup(bptr); 3818 if (!arg->string.string) 3819 goto out_free; 3820 bptr += strlen(bptr) + 1; 3821 *next = arg; 3822 next = &arg->next; 3823 default: 3824 break; 3825 } 3826 } 3827 } 3828 3829 return args; 3830 3831out_free: 3832 free_args(args); 3833 return NULL; 3834} 3835 3836static char * 3837get_bprint_format(void *data, int size __maybe_unused, 3838 struct event_format *event) 3839{ 3840 struct pevent *pevent = event->pevent; 3841 unsigned long long addr; 3842 struct format_field *field; 3843 struct printk_map *printk; 3844 char *format; 3845 char *p; 3846 3847 field = pevent->bprint_fmt_field; 3848 3849 if (!field) { 3850 field = pevent_find_field(event, "fmt"); 3851 if (!field) { 3852 do_warning("can't find format field for binary printk"); 3853 return NULL; 3854 } 3855 pevent->bprint_fmt_field = field; 3856 } 3857 3858 addr = pevent_read_number(pevent, data + field->offset, field->size); 3859 3860 printk = find_printk(pevent, addr); 3861 if (!printk) { 3862 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0) 3863 return NULL; 3864 return format; 3865 } 3866 3867 p = printk->printk; 3868 /* Remove any quotes. */ 3869 if (*p == '"') 3870 p++; 3871 if (asprintf(&format, "%s : %s", "%pf", p) < 0) 3872 return NULL; 3873 /* remove ending quotes and new line since we will add one too */ 3874 p = format + strlen(format) - 1; 3875 if (*p == '"') 3876 *p = 0; 3877 3878 p -= 2; 3879 if (strcmp(p, "\\n") == 0) 3880 *p = 0; 3881 3882 return format; 3883} 3884 3885static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size, 3886 struct event_format *event, struct print_arg *arg) 3887{ 3888 unsigned char *buf; 3889 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"; 3890 3891 if (arg->type == PRINT_FUNC) { 3892 process_defined_func(s, data, size, event, arg); 3893 return; 3894 } 3895 3896 if (arg->type != PRINT_FIELD) { 3897 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", 3898 arg->type); 3899 return; 3900 } 3901 3902 if (mac == 'm') 3903 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x"; 3904 if (!arg->field.field) { 3905 arg->field.field = 3906 pevent_find_any_field(event, arg->field.name); 3907 if (!arg->field.field) { 3908 do_warning("%s: field %s not found", 3909 __func__, arg->field.name); 3910 return; 3911 } 3912 } 3913 if (arg->field.field->size != 6) { 3914 trace_seq_printf(s, "INVALIDMAC"); 3915 return; 3916 } 3917 buf = data + arg->field.field->offset; 3918 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); 3919} 3920 3921static int is_printable_array(char *p, unsigned int len) 3922{ 3923 unsigned int i; 3924 3925 for (i = 0; i < len && p[i]; i++) 3926 if (!isprint(p[i])) 3927 return 0; 3928 return 1; 3929} 3930 3931static void print_event_fields(struct trace_seq *s, void *data, int size, 3932 struct event_format *event) 3933{ 3934 struct format_field *field; 3935 unsigned long long val; 3936 unsigned int offset, len, i; 3937 3938 field = event->format.fields; 3939 while (field) { 3940 trace_seq_printf(s, " %s=", field->name); 3941 if (field->flags & FIELD_IS_ARRAY) { 3942 offset = field->offset; 3943 len = field->size; 3944 if (field->flags & FIELD_IS_DYNAMIC) { 3945 val = pevent_read_number(event->pevent, data + offset, len); 3946 offset = val; 3947 len = offset >> 16; 3948 offset &= 0xffff; 3949 } 3950 if (field->flags & FIELD_IS_STRING && 3951 is_printable_array(data + offset, len)) { 3952 trace_seq_printf(s, "%s", (char *)data + offset); 3953 } else { 3954 trace_seq_puts(s, "ARRAY["); 3955 for (i = 0; i < len; i++) { 3956 if (i) 3957 trace_seq_puts(s, ", "); 3958 trace_seq_printf(s, "%02x", 3959 *((unsigned char *)data + offset + i)); 3960 } 3961 trace_seq_putc(s, ']'); 3962 field->flags &= ~FIELD_IS_STRING; 3963 } 3964 } else { 3965 val = pevent_read_number(event->pevent, data + field->offset, 3966 field->size); 3967 if (field->flags & FIELD_IS_POINTER) { 3968 trace_seq_printf(s, "0x%llx", val); 3969 } else if (field->flags & FIELD_IS_SIGNED) { 3970 switch (field->size) { 3971 case 4: 3972 /* 3973 * If field is long then print it in hex. 3974 * A long usually stores pointers. 3975 */ 3976 if (field->flags & FIELD_IS_LONG) 3977 trace_seq_printf(s, "0x%x", (int)val); 3978 else 3979 trace_seq_printf(s, "%d", (int)val); 3980 break; 3981 case 2: 3982 trace_seq_printf(s, "%2d", (short)val); 3983 break; 3984 case 1: 3985 trace_seq_printf(s, "%1d", (char)val); 3986 break; 3987 default: 3988 trace_seq_printf(s, "%lld", val); 3989 } 3990 } else { 3991 if (field->flags & FIELD_IS_LONG) 3992 trace_seq_printf(s, "0x%llx", val); 3993 else 3994 trace_seq_printf(s, "%llu", val); 3995 } 3996 } 3997 field = field->next; 3998 } 3999} 4000 4001static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event) 4002{ 4003 struct pevent *pevent = event->pevent; 4004 struct print_fmt *print_fmt = &event->print_fmt; 4005 struct print_arg *arg = print_fmt->args; 4006 struct print_arg *args = NULL; 4007 const char *ptr = print_fmt->format; 4008 unsigned long long val; 4009 struct func_map *func; 4010 const char *saveptr; 4011 char *bprint_fmt = NULL; 4012 char format[32]; 4013 int show_func; 4014 int len_as_arg; 4015 int len_arg; 4016 int len; 4017 int ls; 4018 4019 if (event->flags & EVENT_FL_FAILED) { 4020 trace_seq_printf(s, "[FAILED TO PARSE]"); 4021 print_event_fields(s, data, size, event); 4022 return; 4023 } 4024 4025 if (event->flags & EVENT_FL_ISBPRINT) { 4026 bprint_fmt = get_bprint_format(data, size, event); 4027 args = make_bprint_args(bprint_fmt, data, size, event); 4028 arg = args; 4029 ptr = bprint_fmt; 4030 } 4031 4032 for (; *ptr; ptr++) { 4033 ls = 0; 4034 if (*ptr == '\\') { 4035 ptr++; 4036 switch (*ptr) { 4037 case 'n': 4038 trace_seq_putc(s, '\n'); 4039 break; 4040 case 't': 4041 trace_seq_putc(s, '\t'); 4042 break; 4043 case 'r': 4044 trace_seq_putc(s, '\r'); 4045 break; 4046 case '\\': 4047 trace_seq_putc(s, '\\'); 4048 break; 4049 default: 4050 trace_seq_putc(s, *ptr); 4051 break; 4052 } 4053 4054 } else if (*ptr == '%') { 4055 saveptr = ptr; 4056 show_func = 0; 4057 len_as_arg = 0; 4058 cont_process: 4059 ptr++; 4060 switch (*ptr) { 4061 case '%': 4062 trace_seq_putc(s, '%'); 4063 break; 4064 case '#': 4065 /* FIXME: need to handle properly */ 4066 goto cont_process; 4067 case 'h': 4068 ls--; 4069 goto cont_process; 4070 case 'l': 4071 ls++; 4072 goto cont_process; 4073 case 'L': 4074 ls = 2; 4075 goto cont_process; 4076 case '*': 4077 /* The argument is the length. */ 4078 if (!arg) { 4079 do_warning("no argument match"); 4080 event->flags |= EVENT_FL_FAILED; 4081 goto out_failed; 4082 } 4083 len_arg = eval_num_arg(data, size, event, arg); 4084 len_as_arg = 1; 4085 arg = arg->next; 4086 goto cont_process; 4087 case '.': 4088 case 'z': 4089 case 'Z': 4090 case '0' ... '9': 4091 goto cont_process; 4092 case 'p': 4093 if (pevent->long_size == 4) 4094 ls = 1; 4095 else 4096 ls = 2; 4097 4098 if (*(ptr+1) == 'F' || 4099 *(ptr+1) == 'f') { 4100 ptr++; 4101 show_func = *ptr; 4102 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') { 4103 print_mac_arg(s, *(ptr+1), data, size, event, arg); 4104 ptr++; 4105 arg = arg->next; 4106 break; 4107 } 4108 4109 /* fall through */ 4110 case 'd': 4111 case 'i': 4112 case 'x': 4113 case 'X': 4114 case 'u': 4115 if (!arg) { 4116 do_warning("no argument match"); 4117 event->flags |= EVENT_FL_FAILED; 4118 goto out_failed; 4119 } 4120 4121 len = ((unsigned long)ptr + 1) - 4122 (unsigned long)saveptr; 4123 4124 /* should never happen */ 4125 if (len > 31) { 4126 do_warning("bad format!"); 4127 event->flags |= EVENT_FL_FAILED; 4128 len = 31; 4129 } 4130 4131 memcpy(format, saveptr, len); 4132 format[len] = 0; 4133 4134 val = eval_num_arg(data, size, event, arg); 4135 arg = arg->next; 4136 4137 if (show_func) { 4138 func = find_func(pevent, val); 4139 if (func) { 4140 trace_seq_puts(s, func->func); 4141 if (show_func == 'F') 4142 trace_seq_printf(s, 4143 "+0x%llx", 4144 val - func->addr); 4145 break; 4146 } 4147 } 4148 if (pevent->long_size == 8 && ls && 4149 sizeof(long) != 8) { 4150 char *p; 4151 4152 ls = 2; 4153 /* make %l into %ll */ 4154 p = strchr(format, 'l'); 4155 if (p) 4156 memmove(p+1, p, strlen(p)+1); 4157 else if (strcmp(format, "%p") == 0) 4158 strcpy(format, "0x%llx"); 4159 } 4160 switch (ls) { 4161 case -2: 4162 if (len_as_arg) 4163 trace_seq_printf(s, format, len_arg, (char)val); 4164 else 4165 trace_seq_printf(s, format, (char)val); 4166 break; 4167 case -1: 4168 if (len_as_arg) 4169 trace_seq_printf(s, format, len_arg, (short)val); 4170 else 4171 trace_seq_printf(s, format, (short)val); 4172 break; 4173 case 0: 4174 if (len_as_arg) 4175 trace_seq_printf(s, format, len_arg, (int)val); 4176 else 4177 trace_seq_printf(s, format, (int)val); 4178 break; 4179 case 1: 4180 if (len_as_arg) 4181 trace_seq_printf(s, format, len_arg, (long)val); 4182 else 4183 trace_seq_printf(s, format, (long)val); 4184 break; 4185 case 2: 4186 if (len_as_arg) 4187 trace_seq_printf(s, format, len_arg, 4188 (long long)val); 4189 else 4190 trace_seq_printf(s, format, (long long)val); 4191 break; 4192 default: 4193 do_warning("bad count (%d)", ls); 4194 event->flags |= EVENT_FL_FAILED; 4195 } 4196 break; 4197 case 's': 4198 if (!arg) { 4199 do_warning("no matching argument"); 4200 event->flags |= EVENT_FL_FAILED; 4201 goto out_failed; 4202 } 4203 4204 len = ((unsigned long)ptr + 1) - 4205 (unsigned long)saveptr; 4206 4207 /* should never happen */ 4208 if (len > 31) { 4209 do_warning("bad format!"); 4210 event->flags |= EVENT_FL_FAILED; 4211 len = 31; 4212 } 4213 4214 memcpy(format, saveptr, len); 4215 format[len] = 0; 4216 if (!len_as_arg) 4217 len_arg = -1; 4218 print_str_arg(s, data, size, event, 4219 format, len_arg, arg); 4220 arg = arg->next; 4221 break; 4222 default: 4223 trace_seq_printf(s, ">%c<", *ptr); 4224 4225 } 4226 } else 4227 trace_seq_putc(s, *ptr); 4228 } 4229 4230 if (event->flags & EVENT_FL_FAILED) { 4231out_failed: 4232 trace_seq_printf(s, "[FAILED TO PARSE]"); 4233 } 4234 4235 if (args) { 4236 free_args(args); 4237 free(bprint_fmt); 4238 } 4239} 4240 4241/** 4242 * pevent_data_lat_fmt - parse the data for the latency format 4243 * @pevent: a handle to the pevent 4244 * @s: the trace_seq to write to 4245 * @record: the record to read from 4246 * 4247 * This parses out the Latency format (interrupts disabled, 4248 * need rescheduling, in hard/soft interrupt, preempt count 4249 * and lock depth) and places it into the trace_seq. 4250 */ 4251void pevent_data_lat_fmt(struct pevent *pevent, 4252 struct trace_seq *s, struct pevent_record *record) 4253{ 4254 static int check_lock_depth = 1; 4255 static int check_migrate_disable = 1; 4256 static int lock_depth_exists; 4257 static int migrate_disable_exists; 4258 unsigned int lat_flags; 4259 unsigned int pc; 4260 int lock_depth; 4261 int migrate_disable; 4262 int hardirq; 4263 int softirq; 4264 void *data = record->data; 4265 4266 lat_flags = parse_common_flags(pevent, data); 4267 pc = parse_common_pc(pevent, data); 4268 /* lock_depth may not always exist */ 4269 if (lock_depth_exists) 4270 lock_depth = parse_common_lock_depth(pevent, data); 4271 else if (check_lock_depth) { 4272 lock_depth = parse_common_lock_depth(pevent, data); 4273 if (lock_depth < 0) 4274 check_lock_depth = 0; 4275 else 4276 lock_depth_exists = 1; 4277 } 4278 4279 /* migrate_disable may not always exist */ 4280 if (migrate_disable_exists) 4281 migrate_disable = parse_common_migrate_disable(pevent, data); 4282 else if (check_migrate_disable) { 4283 migrate_disable = parse_common_migrate_disable(pevent, data); 4284 if (migrate_disable < 0) 4285 check_migrate_disable = 0; 4286 else 4287 migrate_disable_exists = 1; 4288 } 4289 4290 hardirq = lat_flags & TRACE_FLAG_HARDIRQ; 4291 softirq = lat_flags & TRACE_FLAG_SOFTIRQ; 4292 4293 trace_seq_printf(s, "%c%c%c", 4294 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : 4295 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 4296 'X' : '.', 4297 (lat_flags & TRACE_FLAG_NEED_RESCHED) ? 4298 'N' : '.', 4299 (hardirq && softirq) ? 'H' : 4300 hardirq ? 'h' : softirq ? 's' : '.'); 4301 4302 if (pc) 4303 trace_seq_printf(s, "%x", pc); 4304 else 4305 trace_seq_putc(s, '.'); 4306 4307 if (migrate_disable_exists) { 4308 if (migrate_disable < 0) 4309 trace_seq_putc(s, '.'); 4310 else 4311 trace_seq_printf(s, "%d", migrate_disable); 4312 } 4313 4314 if (lock_depth_exists) { 4315 if (lock_depth < 0) 4316 trace_seq_putc(s, '.'); 4317 else 4318 trace_seq_printf(s, "%d", lock_depth); 4319 } 4320 4321 trace_seq_terminate(s); 4322} 4323 4324/** 4325 * pevent_data_type - parse out the given event type 4326 * @pevent: a handle to the pevent 4327 * @rec: the record to read from 4328 * 4329 * This returns the event id from the @rec. 4330 */ 4331int pevent_data_type(struct pevent *pevent, struct pevent_record *rec) 4332{ 4333 return trace_parse_common_type(pevent, rec->data); 4334} 4335 4336/** 4337 * pevent_data_event_from_type - find the event by a given type 4338 * @pevent: a handle to the pevent 4339 * @type: the type of the event. 4340 * 4341 * This returns the event form a given @type; 4342 */ 4343struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type) 4344{ 4345 return pevent_find_event(pevent, type); 4346} 4347 4348/** 4349 * pevent_data_pid - parse the PID from raw data 4350 * @pevent: a handle to the pevent 4351 * @rec: the record to parse 4352 * 4353 * This returns the PID from a raw data. 4354 */ 4355int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec) 4356{ 4357 return parse_common_pid(pevent, rec->data); 4358} 4359 4360/** 4361 * pevent_data_comm_from_pid - return the command line from PID 4362 * @pevent: a handle to the pevent 4363 * @pid: the PID of the task to search for 4364 * 4365 * This returns a pointer to the command line that has the given 4366 * @pid. 4367 */ 4368const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid) 4369{ 4370 const char *comm; 4371 4372 comm = find_cmdline(pevent, pid); 4373 return comm; 4374} 4375 4376/** 4377 * pevent_data_comm_from_pid - parse the data into the print format 4378 * @s: the trace_seq to write to 4379 * @event: the handle to the event 4380 * @record: the record to read from 4381 * 4382 * This parses the raw @data using the given @event information and 4383 * writes the print format into the trace_seq. 4384 */ 4385void pevent_event_info(struct trace_seq *s, struct event_format *event, 4386 struct pevent_record *record) 4387{ 4388 int print_pretty = 1; 4389 4390 if (event->pevent->print_raw) 4391 print_event_fields(s, record->data, record->size, event); 4392 else { 4393 4394 if (event->handler) 4395 print_pretty = event->handler(s, record, event, 4396 event->context); 4397 4398 if (print_pretty) 4399 pretty_print(s, record->data, record->size, event); 4400 } 4401 4402 trace_seq_terminate(s); 4403} 4404 4405void pevent_print_event(struct pevent *pevent, struct trace_seq *s, 4406 struct pevent_record *record) 4407{ 4408 static char *spaces = " "; /* 20 spaces */ 4409 struct event_format *event; 4410 unsigned long secs; 4411 unsigned long usecs; 4412 unsigned long nsecs; 4413 const char *comm; 4414 void *data = record->data; 4415 int type; 4416 int pid; 4417 int len; 4418 int p; 4419 4420 secs = record->ts / NSECS_PER_SEC; 4421 nsecs = record->ts - secs * NSECS_PER_SEC; 4422 4423 if (record->size < 0) { 4424 do_warning("ug! negative record size %d", record->size); 4425 return; 4426 } 4427 4428 type = trace_parse_common_type(pevent, data); 4429 4430 event = pevent_find_event(pevent, type); 4431 if (!event) { 4432 do_warning("ug! no event found for type %d", type); 4433 return; 4434 } 4435 4436 pid = parse_common_pid(pevent, data); 4437 comm = find_cmdline(pevent, pid); 4438 4439 if (pevent->latency_format) { 4440 trace_seq_printf(s, "%8.8s-%-5d %3d", 4441 comm, pid, record->cpu); 4442 pevent_data_lat_fmt(pevent, s, record); 4443 } else 4444 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu); 4445 4446 if (pevent->flags & PEVENT_NSEC_OUTPUT) { 4447 usecs = nsecs; 4448 p = 9; 4449 } else { 4450 usecs = (nsecs + 500) / NSECS_PER_USEC; 4451 p = 6; 4452 } 4453 4454 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name); 4455 4456 /* Space out the event names evenly. */ 4457 len = strlen(event->name); 4458 if (len < 20) 4459 trace_seq_printf(s, "%.*s", 20 - len, spaces); 4460 4461 pevent_event_info(s, event, record); 4462} 4463 4464static int events_id_cmp(const void *a, const void *b) 4465{ 4466 struct event_format * const * ea = a; 4467 struct event_format * const * eb = b; 4468 4469 if ((*ea)->id < (*eb)->id) 4470 return -1; 4471 4472 if ((*ea)->id > (*eb)->id) 4473 return 1; 4474 4475 return 0; 4476} 4477 4478static int events_name_cmp(const void *a, const void *b) 4479{ 4480 struct event_format * const * ea = a; 4481 struct event_format * const * eb = b; 4482 int res; 4483 4484 res = strcmp((*ea)->name, (*eb)->name); 4485 if (res) 4486 return res; 4487 4488 res = strcmp((*ea)->system, (*eb)->system); 4489 if (res) 4490 return res; 4491 4492 return events_id_cmp(a, b); 4493} 4494 4495static int events_system_cmp(const void *a, const void *b) 4496{ 4497 struct event_format * const * ea = a; 4498 struct event_format * const * eb = b; 4499 int res; 4500 4501 res = strcmp((*ea)->system, (*eb)->system); 4502 if (res) 4503 return res; 4504 4505 res = strcmp((*ea)->name, (*eb)->name); 4506 if (res) 4507 return res; 4508 4509 return events_id_cmp(a, b); 4510} 4511 4512struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type) 4513{ 4514 struct event_format **events; 4515 int (*sort)(const void *a, const void *b); 4516 4517 events = pevent->sort_events; 4518 4519 if (events && pevent->last_type == sort_type) 4520 return events; 4521 4522 if (!events) { 4523 events = malloc(sizeof(*events) * (pevent->nr_events + 1)); 4524 if (!events) 4525 return NULL; 4526 4527 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events); 4528 events[pevent->nr_events] = NULL; 4529 4530 pevent->sort_events = events; 4531 4532 /* the internal events are sorted by id */ 4533 if (sort_type == EVENT_SORT_ID) { 4534 pevent->last_type = sort_type; 4535 return events; 4536 } 4537 } 4538 4539 switch (sort_type) { 4540 case EVENT_SORT_ID: 4541 sort = events_id_cmp; 4542 break; 4543 case EVENT_SORT_NAME: 4544 sort = events_name_cmp; 4545 break; 4546 case EVENT_SORT_SYSTEM: 4547 sort = events_system_cmp; 4548 break; 4549 default: 4550 return events; 4551 } 4552 4553 qsort(events, pevent->nr_events, sizeof(*events), sort); 4554 pevent->last_type = sort_type; 4555 4556 return events; 4557} 4558 4559static struct format_field ** 4560get_event_fields(const char *type, const char *name, 4561 int count, struct format_field *list) 4562{ 4563 struct format_field **fields; 4564 struct format_field *field; 4565 int i = 0; 4566 4567 fields = malloc(sizeof(*fields) * (count + 1)); 4568 if (!fields) 4569 return NULL; 4570 4571 for (field = list; field; field = field->next) { 4572 fields[i++] = field; 4573 if (i == count + 1) { 4574 do_warning("event %s has more %s fields than specified", 4575 name, type); 4576 i--; 4577 break; 4578 } 4579 } 4580 4581 if (i != count) 4582 do_warning("event %s has less %s fields than specified", 4583 name, type); 4584 4585 fields[i] = NULL; 4586 4587 return fields; 4588} 4589 4590/** 4591 * pevent_event_common_fields - return a list of common fields for an event 4592 * @event: the event to return the common fields of. 4593 * 4594 * Returns an allocated array of fields. The last item in the array is NULL. 4595 * The array must be freed with free(). 4596 */ 4597struct format_field **pevent_event_common_fields(struct event_format *event) 4598{ 4599 return get_event_fields("common", event->name, 4600 event->format.nr_common, 4601 event->format.common_fields); 4602} 4603 4604/** 4605 * pevent_event_fields - return a list of event specific fields for an event 4606 * @event: the event to return the fields of. 4607 * 4608 * Returns an allocated array of fields. The last item in the array is NULL. 4609 * The array must be freed with free(). 4610 */ 4611struct format_field **pevent_event_fields(struct event_format *event) 4612{ 4613 return get_event_fields("event", event->name, 4614 event->format.nr_fields, 4615 event->format.fields); 4616} 4617 4618static void print_fields(struct trace_seq *s, struct print_flag_sym *field) 4619{ 4620 trace_seq_printf(s, "{ %s, %s }", field->value, field->str); 4621 if (field->next) { 4622 trace_seq_puts(s, ", "); 4623 print_fields(s, field->next); 4624 } 4625} 4626 4627/* for debugging */ 4628static void print_args(struct print_arg *args) 4629{ 4630 int print_paren = 1; 4631 struct trace_seq s; 4632 4633 switch (args->type) { 4634 case PRINT_NULL: 4635 printf("null"); 4636 break; 4637 case PRINT_ATOM: 4638 printf("%s", args->atom.atom); 4639 break; 4640 case PRINT_FIELD: 4641 printf("REC->%s", args->field.name); 4642 break; 4643 case PRINT_FLAGS: 4644 printf("__print_flags("); 4645 print_args(args->flags.field); 4646 printf(", %s, ", args->flags.delim); 4647 trace_seq_init(&s); 4648 print_fields(&s, args->flags.flags); 4649 trace_seq_do_printf(&s); 4650 trace_seq_destroy(&s); 4651 printf(")"); 4652 break; 4653 case PRINT_SYMBOL: 4654 printf("__print_symbolic("); 4655 print_args(args->symbol.field); 4656 printf(", "); 4657 trace_seq_init(&s); 4658 print_fields(&s, args->symbol.symbols); 4659 trace_seq_do_printf(&s); 4660 trace_seq_destroy(&s); 4661 printf(")"); 4662 break; 4663 case PRINT_HEX: 4664 printf("__print_hex("); 4665 print_args(args->hex.field); 4666 printf(", "); 4667 print_args(args->hex.size); 4668 printf(")"); 4669 break; 4670 case PRINT_STRING: 4671 case PRINT_BSTRING: 4672 printf("__get_str(%s)", args->string.string); 4673 break; 4674 case PRINT_TYPE: 4675 printf("(%s)", args->typecast.type); 4676 print_args(args->typecast.item); 4677 break; 4678 case PRINT_OP: 4679 if (strcmp(args->op.op, ":") == 0) 4680 print_paren = 0; 4681 if (print_paren) 4682 printf("("); 4683 print_args(args->op.left); 4684 printf(" %s ", args->op.op); 4685 print_args(args->op.right); 4686 if (print_paren) 4687 printf(")"); 4688 break; 4689 default: 4690 /* we should warn... */ 4691 return; 4692 } 4693 if (args->next) { 4694 printf("\n"); 4695 print_args(args->next); 4696 } 4697} 4698 4699static void parse_header_field(const char *field, 4700 int *offset, int *size, int mandatory) 4701{ 4702 unsigned long long save_input_buf_ptr; 4703 unsigned long long save_input_buf_siz; 4704 char *token; 4705 int type; 4706 4707 save_input_buf_ptr = input_buf_ptr; 4708 save_input_buf_siz = input_buf_siz; 4709 4710 if (read_expected(EVENT_ITEM, "field") < 0) 4711 return; 4712 if (read_expected(EVENT_OP, ":") < 0) 4713 return; 4714 4715 /* type */ 4716 if (read_expect_type(EVENT_ITEM, &token) < 0) 4717 goto fail; 4718 free_token(token); 4719 4720 /* 4721 * If this is not a mandatory field, then test it first. 4722 */ 4723 if (mandatory) { 4724 if (read_expected(EVENT_ITEM, field) < 0) 4725 return; 4726 } else { 4727 if (read_expect_type(EVENT_ITEM, &token) < 0) 4728 goto fail; 4729 if (strcmp(token, field) != 0) 4730 goto discard; 4731 free_token(token); 4732 } 4733 4734 if (read_expected(EVENT_OP, ";") < 0) 4735 return; 4736 if (read_expected(EVENT_ITEM, "offset") < 0) 4737 return; 4738 if (read_expected(EVENT_OP, ":") < 0) 4739 return; 4740 if (read_expect_type(EVENT_ITEM, &token) < 0) 4741 goto fail; 4742 *offset = atoi(token); 4743 free_token(token); 4744 if (read_expected(EVENT_OP, ";") < 0) 4745 return; 4746 if (read_expected(EVENT_ITEM, "size") < 0) 4747 return; 4748 if (read_expected(EVENT_OP, ":") < 0) 4749 return; 4750 if (read_expect_type(EVENT_ITEM, &token) < 0) 4751 goto fail; 4752 *size = atoi(token); 4753 free_token(token); 4754 if (read_expected(EVENT_OP, ";") < 0) 4755 return; 4756 type = read_token(&token); 4757 if (type != EVENT_NEWLINE) { 4758 /* newer versions of the kernel have a "signed" type */ 4759 if (type != EVENT_ITEM) 4760 goto fail; 4761 4762 if (strcmp(token, "signed") != 0) 4763 goto fail; 4764 4765 free_token(token); 4766 4767 if (read_expected(EVENT_OP, ":") < 0) 4768 return; 4769 4770 if (read_expect_type(EVENT_ITEM, &token)) 4771 goto fail; 4772 4773 free_token(token); 4774 if (read_expected(EVENT_OP, ";") < 0) 4775 return; 4776 4777 if (read_expect_type(EVENT_NEWLINE, &token)) 4778 goto fail; 4779 } 4780 fail: 4781 free_token(token); 4782 return; 4783 4784 discard: 4785 input_buf_ptr = save_input_buf_ptr; 4786 input_buf_siz = save_input_buf_siz; 4787 *offset = 0; 4788 *size = 0; 4789 free_token(token); 4790} 4791 4792/** 4793 * pevent_parse_header_page - parse the data stored in the header page 4794 * @pevent: the handle to the pevent 4795 * @buf: the buffer storing the header page format string 4796 * @size: the size of @buf 4797 * @long_size: the long size to use if there is no header 4798 * 4799 * This parses the header page format for information on the 4800 * ring buffer used. The @buf should be copied from 4801 * 4802 * /sys/kernel/debug/tracing/events/header_page 4803 */ 4804int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size, 4805 int long_size) 4806{ 4807 int ignore; 4808 4809 if (!size) { 4810 /* 4811 * Old kernels did not have header page info. 4812 * Sorry but we just use what we find here in user space. 4813 */ 4814 pevent->header_page_ts_size = sizeof(long long); 4815 pevent->header_page_size_size = long_size; 4816 pevent->header_page_data_offset = sizeof(long long) + long_size; 4817 pevent->old_format = 1; 4818 return -1; 4819 } 4820 init_input_buf(buf, size); 4821 4822 parse_header_field("timestamp", &pevent->header_page_ts_offset, 4823 &pevent->header_page_ts_size, 1); 4824 parse_header_field("commit", &pevent->header_page_size_offset, 4825 &pevent->header_page_size_size, 1); 4826 parse_header_field("overwrite", &pevent->header_page_overwrite, 4827 &ignore, 0); 4828 parse_header_field("data", &pevent->header_page_data_offset, 4829 &pevent->header_page_data_size, 1); 4830 4831 return 0; 4832} 4833 4834static int event_matches(struct event_format *event, 4835 int id, const char *sys_name, 4836 const char *event_name) 4837{ 4838 if (id >= 0 && id != event->id) 4839 return 0; 4840 4841 if (event_name && (strcmp(event_name, event->name) != 0)) 4842 return 0; 4843 4844 if (sys_name && (strcmp(sys_name, event->system) != 0)) 4845 return 0; 4846 4847 return 1; 4848} 4849 4850static void free_handler(struct event_handler *handle) 4851{ 4852 free((void *)handle->sys_name); 4853 free((void *)handle->event_name); 4854 free(handle); 4855} 4856 4857static int find_event_handle(struct pevent *pevent, struct event_format *event) 4858{ 4859 struct event_handler *handle, **next; 4860 4861 for (next = &pevent->handlers; *next; 4862 next = &(*next)->next) { 4863 handle = *next; 4864 if (event_matches(event, handle->id, 4865 handle->sys_name, 4866 handle->event_name)) 4867 break; 4868 } 4869 4870 if (!(*next)) 4871 return 0; 4872 4873 pr_stat("overriding event (%d) %s:%s with new print handler", 4874 event->id, event->system, event->name); 4875 4876 event->handler = handle->func; 4877 event->context = handle->context; 4878 4879 *next = handle->next; 4880 free_handler(handle); 4881 4882 return 1; 4883} 4884 4885/** 4886 * __pevent_parse_format - parse the event format 4887 * @buf: the buffer storing the event format string 4888 * @size: the size of @buf 4889 * @sys: the system the event belongs to 4890 * 4891 * This parses the event format and creates an event structure 4892 * to quickly parse raw data for a given event. 4893 * 4894 * These files currently come from: 4895 * 4896 * /sys/kernel/debug/tracing/events/.../.../format 4897 */ 4898enum pevent_errno __pevent_parse_format(struct event_format **eventp, 4899 struct pevent *pevent, const char *buf, 4900 unsigned long size, const char *sys) 4901{ 4902 struct event_format *event; 4903 int ret; 4904 4905 init_input_buf(buf, size); 4906 4907 *eventp = event = alloc_event(); 4908 if (!event) 4909 return PEVENT_ERRNO__MEM_ALLOC_FAILED; 4910 4911 event->name = event_read_name(); 4912 if (!event->name) { 4913 /* Bad event? */ 4914 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; 4915 goto event_alloc_failed; 4916 } 4917 4918 if (strcmp(sys, "ftrace") == 0) { 4919 event->flags |= EVENT_FL_ISFTRACE; 4920 4921 if (strcmp(event->name, "bprint") == 0) 4922 event->flags |= EVENT_FL_ISBPRINT; 4923 } 4924 4925 event->id = event_read_id(); 4926 if (event->id < 0) { 4927 ret = PEVENT_ERRNO__READ_ID_FAILED; 4928 /* 4929 * This isn't an allocation error actually. 4930 * But as the ID is critical, just bail out. 4931 */ 4932 goto event_alloc_failed; 4933 } 4934 4935 event->system = strdup(sys); 4936 if (!event->system) { 4937 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; 4938 goto event_alloc_failed; 4939 } 4940 4941 ret = event_read_format(event); 4942 if (ret < 0) { 4943 ret = PEVENT_ERRNO__READ_FORMAT_FAILED; 4944 goto event_parse_failed; 4945 } 4946 4947 /* 4948 * If the event has an override, don't print warnings if the event 4949 * print format fails to parse. 4950 */ 4951 if (pevent && find_event_handle(pevent, event)) 4952 show_warning = 0; 4953 4954 ret = event_read_print(event); 4955 show_warning = 1; 4956 4957 if (ret < 0) { 4958 ret = PEVENT_ERRNO__READ_PRINT_FAILED; 4959 goto event_parse_failed; 4960 } 4961 4962 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) { 4963 struct format_field *field; 4964 struct print_arg *arg, **list; 4965 4966 /* old ftrace had no args */ 4967 list = &event->print_fmt.args; 4968 for (field = event->format.fields; field; field = field->next) { 4969 arg = alloc_arg(); 4970 if (!arg) { 4971 event->flags |= EVENT_FL_FAILED; 4972 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED; 4973 } 4974 arg->type = PRINT_FIELD; 4975 arg->field.name = strdup(field->name); 4976 if (!arg->field.name) { 4977 event->flags |= EVENT_FL_FAILED; 4978 free_arg(arg); 4979 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED; 4980 } 4981 arg->field.field = field; 4982 *list = arg; 4983 list = &arg->next; 4984 } 4985 return 0; 4986 } 4987 4988 return 0; 4989 4990 event_parse_failed: 4991 event->flags |= EVENT_FL_FAILED; 4992 return ret; 4993 4994 event_alloc_failed: 4995 free(event->system); 4996 free(event->name); 4997 free(event); 4998 *eventp = NULL; 4999 return ret; 5000} 5001 5002/** 5003 * pevent_parse_format - parse the event format 5004 * @buf: the buffer storing the event format string 5005 * @size: the size of @buf 5006 * @sys: the system the event belongs to 5007 * 5008 * This parses the event format and creates an event structure 5009 * to quickly parse raw data for a given event. 5010 * 5011 * These files currently come from: 5012 * 5013 * /sys/kernel/debug/tracing/events/.../.../format 5014 */ 5015enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf, 5016 unsigned long size, const char *sys) 5017{ 5018 return __pevent_parse_format(eventp, NULL, buf, size, sys); 5019} 5020 5021/** 5022 * pevent_parse_event - parse the event format 5023 * @pevent: the handle to the pevent 5024 * @buf: the buffer storing the event format string 5025 * @size: the size of @buf 5026 * @sys: the system the event belongs to 5027 * 5028 * This parses the event format and creates an event structure 5029 * to quickly parse raw data for a given event. 5030 * 5031 * These files currently come from: 5032 * 5033 * /sys/kernel/debug/tracing/events/.../.../format 5034 */ 5035enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf, 5036 unsigned long size, const char *sys) 5037{ 5038 struct event_format *event = NULL; 5039 int ret = __pevent_parse_format(&event, pevent, buf, size, sys); 5040 5041 if (event == NULL) 5042 return ret; 5043 5044 /* Add pevent to event so that it can be referenced */ 5045 event->pevent = pevent; 5046 5047 if (add_event(pevent, event)) { 5048 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; 5049 goto event_add_failed; 5050 } 5051 5052#define PRINT_ARGS 0 5053 if (PRINT_ARGS && event->print_fmt.args) 5054 print_args(event->print_fmt.args); 5055 5056 return 0; 5057 5058event_add_failed: 5059 pevent_free_format(event); 5060 return ret; 5061} 5062 5063#undef _PE 5064#define _PE(code, str) str 5065static const char * const pevent_error_str[] = { 5066 PEVENT_ERRORS 5067}; 5068#undef _PE 5069 5070int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum, 5071 char *buf, size_t buflen) 5072{ 5073 int idx; 5074 const char *msg; 5075 5076 if (errnum >= 0) { 5077 msg = strerror_r(errnum, buf, buflen); 5078 if (msg != buf) { 5079 size_t len = strlen(msg); 5080 memcpy(buf, msg, min(buflen - 1, len)); 5081 *(buf + min(buflen - 1, len)) = '\0'; 5082 } 5083 return 0; 5084 } 5085 5086 if (errnum <= __PEVENT_ERRNO__START || 5087 errnum >= __PEVENT_ERRNO__END) 5088 return -1; 5089 5090 idx = errnum - __PEVENT_ERRNO__START - 1; 5091 msg = pevent_error_str[idx]; 5092 5093 switch (errnum) { 5094 case PEVENT_ERRNO__MEM_ALLOC_FAILED: 5095 case PEVENT_ERRNO__PARSE_EVENT_FAILED: 5096 case PEVENT_ERRNO__READ_ID_FAILED: 5097 case PEVENT_ERRNO__READ_FORMAT_FAILED: 5098 case PEVENT_ERRNO__READ_PRINT_FAILED: 5099 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED: 5100 snprintf(buf, buflen, "%s", msg); 5101 break; 5102 5103 default: 5104 /* cannot reach here */ 5105 break; 5106 } 5107 5108 return 0; 5109} 5110 5111int get_field_val(struct trace_seq *s, struct format_field *field, 5112 const char *name, struct pevent_record *record, 5113 unsigned long long *val, int err) 5114{ 5115 if (!field) { 5116 if (err) 5117 trace_seq_printf(s, "<CANT FIND FIELD %s>", name); 5118 return -1; 5119 } 5120 5121 if (pevent_read_number_field(field, record->data, val)) { 5122 if (err) 5123 trace_seq_printf(s, " %s=INVALID", name); 5124 return -1; 5125 } 5126 5127 return 0; 5128} 5129 5130/** 5131 * pevent_get_field_raw - return the raw pointer into the data field 5132 * @s: The seq to print to on error 5133 * @event: the event that the field is for 5134 * @name: The name of the field 5135 * @record: The record with the field name. 5136 * @len: place to store the field length. 5137 * @err: print default error if failed. 5138 * 5139 * Returns a pointer into record->data of the field and places 5140 * the length of the field in @len. 5141 * 5142 * On failure, it returns NULL. 5143 */ 5144void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event, 5145 const char *name, struct pevent_record *record, 5146 int *len, int err) 5147{ 5148 struct format_field *field; 5149 void *data = record->data; 5150 unsigned offset; 5151 int dummy; 5152 5153 if (!event) 5154 return NULL; 5155 5156 field = pevent_find_field(event, name); 5157 5158 if (!field) { 5159 if (err) 5160 trace_seq_printf(s, "<CANT FIND FIELD %s>", name); 5161 return NULL; 5162 } 5163 5164 /* Allow @len to be NULL */ 5165 if (!len) 5166 len = &dummy; 5167 5168 offset = field->offset; 5169 if (field->flags & FIELD_IS_DYNAMIC) { 5170 offset = pevent_read_number(event->pevent, 5171 data + offset, field->size); 5172 *len = offset >> 16; 5173 offset &= 0xffff; 5174 } else 5175 *len = field->size; 5176 5177 return data + offset; 5178} 5179 5180/** 5181 * pevent_get_field_val - find a field and return its value 5182 * @s: The seq to print to on error 5183 * @event: the event that the field is for 5184 * @name: The name of the field 5185 * @record: The record with the field name. 5186 * @val: place to store the value of the field. 5187 * @err: print default error if failed. 5188 * 5189 * Returns 0 on success -1 on field not found. 5190 */ 5191int pevent_get_field_val(struct trace_seq *s, struct event_format *event, 5192 const char *name, struct pevent_record *record, 5193 unsigned long long *val, int err) 5194{ 5195 struct format_field *field; 5196 5197 if (!event) 5198 return -1; 5199 5200 field = pevent_find_field(event, name); 5201 5202 return get_field_val(s, field, name, record, val, err); 5203} 5204 5205/** 5206 * pevent_get_common_field_val - find a common field and return its value 5207 * @s: The seq to print to on error 5208 * @event: the event that the field is for 5209 * @name: The name of the field 5210 * @record: The record with the field name. 5211 * @val: place to store the value of the field. 5212 * @err: print default error if failed. 5213 * 5214 * Returns 0 on success -1 on field not found. 5215 */ 5216int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event, 5217 const char *name, struct pevent_record *record, 5218 unsigned long long *val, int err) 5219{ 5220 struct format_field *field; 5221 5222 if (!event) 5223 return -1; 5224 5225 field = pevent_find_common_field(event, name); 5226 5227 return get_field_val(s, field, name, record, val, err); 5228} 5229 5230/** 5231 * pevent_get_any_field_val - find a any field and return its value 5232 * @s: The seq to print to on error 5233 * @event: the event that the field is for 5234 * @name: The name of the field 5235 * @record: The record with the field name. 5236 * @val: place to store the value of the field. 5237 * @err: print default error if failed. 5238 * 5239 * Returns 0 on success -1 on field not found. 5240 */ 5241int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event, 5242 const char *name, struct pevent_record *record, 5243 unsigned long long *val, int err) 5244{ 5245 struct format_field *field; 5246 5247 if (!event) 5248 return -1; 5249 5250 field = pevent_find_any_field(event, name); 5251 5252 return get_field_val(s, field, name, record, val, err); 5253} 5254 5255/** 5256 * pevent_print_num_field - print a field and a format 5257 * @s: The seq to print to 5258 * @fmt: The printf format to print the field with. 5259 * @event: the event that the field is for 5260 * @name: The name of the field 5261 * @record: The record with the field name. 5262 * @err: print default error if failed. 5263 * 5264 * Returns: 0 on success, -1 field not found, or 1 if buffer is full. 5265 */ 5266int pevent_print_num_field(struct trace_seq *s, const char *fmt, 5267 struct event_format *event, const char *name, 5268 struct pevent_record *record, int err) 5269{ 5270 struct format_field *field = pevent_find_field(event, name); 5271 unsigned long long val; 5272 5273 if (!field) 5274 goto failed; 5275 5276 if (pevent_read_number_field(field, record->data, &val)) 5277 goto failed; 5278 5279 return trace_seq_printf(s, fmt, val); 5280 5281 failed: 5282 if (err) 5283 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name); 5284 return -1; 5285} 5286 5287static void free_func_handle(struct pevent_function_handler *func) 5288{ 5289 struct pevent_func_params *params; 5290 5291 free(func->name); 5292 5293 while (func->params) { 5294 params = func->params; 5295 func->params = params->next; 5296 free(params); 5297 } 5298 5299 free(func); 5300} 5301 5302/** 5303 * pevent_register_print_function - register a helper function 5304 * @pevent: the handle to the pevent 5305 * @func: the function to process the helper function 5306 * @ret_type: the return type of the helper function 5307 * @name: the name of the helper function 5308 * @parameters: A list of enum pevent_func_arg_type 5309 * 5310 * Some events may have helper functions in the print format arguments. 5311 * This allows a plugin to dynamically create a way to process one 5312 * of these functions. 5313 * 5314 * The @parameters is a variable list of pevent_func_arg_type enums that 5315 * must end with PEVENT_FUNC_ARG_VOID. 5316 */ 5317int pevent_register_print_function(struct pevent *pevent, 5318 pevent_func_handler func, 5319 enum pevent_func_arg_type ret_type, 5320 char *name, ...) 5321{ 5322 struct pevent_function_handler *func_handle; 5323 struct pevent_func_params **next_param; 5324 struct pevent_func_params *param; 5325 enum pevent_func_arg_type type; 5326 va_list ap; 5327 int ret; 5328 5329 func_handle = find_func_handler(pevent, name); 5330 if (func_handle) { 5331 /* 5332 * This is most like caused by the users own 5333 * plugins updating the function. This overrides the 5334 * system defaults. 5335 */ 5336 pr_stat("override of function helper '%s'", name); 5337 remove_func_handler(pevent, name); 5338 } 5339 5340 func_handle = calloc(1, sizeof(*func_handle)); 5341 if (!func_handle) { 5342 do_warning("Failed to allocate function handler"); 5343 return PEVENT_ERRNO__MEM_ALLOC_FAILED; 5344 } 5345 5346 func_handle->ret_type = ret_type; 5347 func_handle->name = strdup(name); 5348 func_handle->func = func; 5349 if (!func_handle->name) { 5350 do_warning("Failed to allocate function name"); 5351 free(func_handle); 5352 return PEVENT_ERRNO__MEM_ALLOC_FAILED; 5353 } 5354 5355 next_param = &(func_handle->params); 5356 va_start(ap, name); 5357 for (;;) { 5358 type = va_arg(ap, enum pevent_func_arg_type); 5359 if (type == PEVENT_FUNC_ARG_VOID) 5360 break; 5361 5362 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) { 5363 do_warning("Invalid argument type %d", type); 5364 ret = PEVENT_ERRNO__INVALID_ARG_TYPE; 5365 goto out_free; 5366 } 5367 5368 param = malloc(sizeof(*param)); 5369 if (!param) { 5370 do_warning("Failed to allocate function param"); 5371 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; 5372 goto out_free; 5373 } 5374 param->type = type; 5375 param->next = NULL; 5376 5377 *next_param = param; 5378 next_param = &(param->next); 5379 5380 func_handle->nr_args++; 5381 } 5382 va_end(ap); 5383 5384 func_handle->next = pevent->func_handlers; 5385 pevent->func_handlers = func_handle; 5386 5387 return 0; 5388 out_free: 5389 va_end(ap); 5390 free_func_handle(func_handle); 5391 return ret; 5392} 5393 5394/** 5395 * pevent_register_event_handler - register a way to parse an event 5396 * @pevent: the handle to the pevent 5397 * @id: the id of the event to register 5398 * @sys_name: the system name the event belongs to 5399 * @event_name: the name of the event 5400 * @func: the function to call to parse the event information 5401 * @context: the data to be passed to @func 5402 * 5403 * This function allows a developer to override the parsing of 5404 * a given event. If for some reason the default print format 5405 * is not sufficient, this function will register a function 5406 * for an event to be used to parse the data instead. 5407 * 5408 * If @id is >= 0, then it is used to find the event. 5409 * else @sys_name and @event_name are used. 5410 */ 5411int pevent_register_event_handler(struct pevent *pevent, 5412 int id, char *sys_name, char *event_name, 5413 pevent_event_handler_func func, 5414 void *context) 5415{ 5416 struct event_format *event; 5417 struct event_handler *handle; 5418 5419 if (id >= 0) { 5420 /* search by id */ 5421 event = pevent_find_event(pevent, id); 5422 if (!event) 5423 goto not_found; 5424 if (event_name && (strcmp(event_name, event->name) != 0)) 5425 goto not_found; 5426 if (sys_name && (strcmp(sys_name, event->system) != 0)) 5427 goto not_found; 5428 } else { 5429 event = pevent_find_event_by_name(pevent, sys_name, event_name); 5430 if (!event) 5431 goto not_found; 5432 } 5433 5434 pr_stat("overriding event (%d) %s:%s with new print handler", 5435 event->id, event->system, event->name); 5436 5437 event->handler = func; 5438 event->context = context; 5439 return 0; 5440 5441 not_found: 5442 /* Save for later use. */ 5443 handle = calloc(1, sizeof(*handle)); 5444 if (!handle) { 5445 do_warning("Failed to allocate event handler"); 5446 return PEVENT_ERRNO__MEM_ALLOC_FAILED; 5447 } 5448 5449 handle->id = id; 5450 if (event_name) 5451 handle->event_name = strdup(event_name); 5452 if (sys_name) 5453 handle->sys_name = strdup(sys_name); 5454 5455 if ((event_name && !handle->event_name) || 5456 (sys_name && !handle->sys_name)) { 5457 do_warning("Failed to allocate event/sys name"); 5458 free((void *)handle->event_name); 5459 free((void *)handle->sys_name); 5460 free(handle); 5461 return PEVENT_ERRNO__MEM_ALLOC_FAILED; 5462 } 5463 5464 handle->func = func; 5465 handle->next = pevent->handlers; 5466 pevent->handlers = handle; 5467 handle->context = context; 5468 5469 return -1; 5470} 5471 5472/** 5473 * pevent_alloc - create a pevent handle 5474 */ 5475struct pevent *pevent_alloc(void) 5476{ 5477 struct pevent *pevent = calloc(1, sizeof(*pevent)); 5478 5479 if (pevent) 5480 pevent->ref_count = 1; 5481 5482 return pevent; 5483} 5484 5485void pevent_ref(struct pevent *pevent) 5486{ 5487 pevent->ref_count++; 5488} 5489 5490static void free_format_fields(struct format_field *field) 5491{ 5492 struct format_field *next; 5493 5494 while (field) { 5495 next = field->next; 5496 free(field->type); 5497 free(field->name); 5498 free(field); 5499 field = next; 5500 } 5501} 5502 5503static void free_formats(struct format *format) 5504{ 5505 free_format_fields(format->common_fields); 5506 free_format_fields(format->fields); 5507} 5508 5509void pevent_free_format(struct event_format *event) 5510{ 5511 free(event->name); 5512 free(event->system); 5513 5514 free_formats(&event->format); 5515 5516 free(event->print_fmt.format); 5517 free_args(event->print_fmt.args); 5518 5519 free(event); 5520} 5521 5522/** 5523 * pevent_free - free a pevent handle 5524 * @pevent: the pevent handle to free 5525 */ 5526void pevent_free(struct pevent *pevent) 5527{ 5528 struct cmdline_list *cmdlist, *cmdnext; 5529 struct func_list *funclist, *funcnext; 5530 struct printk_list *printklist, *printknext; 5531 struct pevent_function_handler *func_handler; 5532 struct event_handler *handle; 5533 int i; 5534 5535 if (!pevent) 5536 return; 5537 5538 cmdlist = pevent->cmdlist; 5539 funclist = pevent->funclist; 5540 printklist = pevent->printklist; 5541 5542 pevent->ref_count--; 5543 if (pevent->ref_count) 5544 return; 5545 5546 if (pevent->cmdlines) { 5547 for (i = 0; i < pevent->cmdline_count; i++) 5548 free(pevent->cmdlines[i].comm); 5549 free(pevent->cmdlines); 5550 } 5551 5552 while (cmdlist) { 5553 cmdnext = cmdlist->next; 5554 free(cmdlist->comm); 5555 free(cmdlist); 5556 cmdlist = cmdnext; 5557 } 5558 5559 if (pevent->func_map) { 5560 for (i = 0; i < pevent->func_count; i++) { 5561 free(pevent->func_map[i].func); 5562 free(pevent->func_map[i].mod); 5563 } 5564 free(pevent->func_map); 5565 } 5566 5567 while (funclist) { 5568 funcnext = funclist->next; 5569 free(funclist->func); 5570 free(funclist->mod); 5571 free(funclist); 5572 funclist = funcnext; 5573 } 5574 5575 while (pevent->func_handlers) { 5576 func_handler = pevent->func_handlers; 5577 pevent->func_handlers = func_handler->next; 5578 free_func_handle(func_handler); 5579 } 5580 5581 if (pevent->printk_map) { 5582 for (i = 0; i < pevent->printk_count; i++) 5583 free(pevent->printk_map[i].printk); 5584 free(pevent->printk_map); 5585 } 5586 5587 while (printklist) { 5588 printknext = printklist->next; 5589 free(printklist->printk); 5590 free(printklist); 5591 printklist = printknext; 5592 } 5593 5594 for (i = 0; i < pevent->nr_events; i++) 5595 pevent_free_format(pevent->events[i]); 5596 5597 while (pevent->handlers) { 5598 handle = pevent->handlers; 5599 pevent->handlers = handle->next; 5600 free_handler(handle); 5601 } 5602 5603 free(pevent->events); 5604 free(pevent->sort_events); 5605 5606 free(pevent); 5607} 5608 5609void pevent_unref(struct pevent *pevent) 5610{ 5611 pevent_free(pevent); 5612}