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