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