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