| // SPDX-License-Identifier: LGPL-2.1 |
| /* |
| * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| * |
| * |
| * The parts for function graph printing was taken and modified from the |
| * Linux Kernel that were written by |
| * - Copyright (C) 2009 Frederic Weisbecker, |
| * Frederic Weisbecker gave his permission to relicense the code to |
| * the Lesser General Public License. |
| */ |
| #include <inttypes.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdarg.h> |
| #include <ctype.h> |
| #include <errno.h> |
| #include <stdint.h> |
| #include <limits.h> |
| #include <linux/time64.h> |
| |
| #include <netinet/in.h> |
| #include "event-parse.h" |
| |
| #include "event-parse-local.h" |
| #include "event-utils.h" |
| #include "trace-seq.h" |
| |
| static const char *input_buf; |
| static unsigned long long input_buf_ptr; |
| static unsigned long long input_buf_siz; |
| |
| static int is_flag_field; |
| static int is_symbolic_field; |
| |
| static int show_warning = 1; |
| |
| #define do_warning(fmt, ...) \ |
| do { \ |
| if (show_warning) \ |
| warning(fmt, ##__VA_ARGS__); \ |
| } while (0) |
| |
| #define do_warning_event(event, fmt, ...) \ |
| do { \ |
| if (!show_warning) \ |
| continue; \ |
| \ |
| if (event) \ |
| warning("[%s:%s] " fmt, event->system, \ |
| event->name, ##__VA_ARGS__); \ |
| else \ |
| warning(fmt, ##__VA_ARGS__); \ |
| } while (0) |
| |
| /** |
| * init_input_buf - init buffer for parsing |
| * @buf: buffer to parse |
| * @size: the size of the buffer |
| * |
| * Initializes the internal buffer that tep_read_token() will parse. |
| */ |
| __hidden void init_input_buf(const char *buf, unsigned long long size) |
| { |
| input_buf = buf; |
| input_buf_siz = size; |
| input_buf_ptr = 0; |
| } |
| |
| __hidden const char *get_input_buf(void) |
| { |
| return input_buf; |
| } |
| |
| __hidden unsigned long long get_input_buf_ptr(void) |
| { |
| return input_buf_ptr; |
| } |
| |
| struct event_handler { |
| struct event_handler *next; |
| int id; |
| const char *sys_name; |
| const char *event_name; |
| tep_event_handler_func func; |
| void *context; |
| }; |
| |
| struct func_params { |
| struct func_params *next; |
| enum tep_func_arg_type type; |
| }; |
| |
| struct tep_function_handler { |
| struct tep_function_handler *next; |
| enum tep_func_arg_type ret_type; |
| char *name; |
| tep_func_handler func; |
| struct func_params *params; |
| int nr_args; |
| }; |
| |
| static unsigned long long |
| process_defined_func(struct trace_seq *s, void *data, int size, |
| struct tep_event *event, struct tep_print_arg *arg); |
| |
| static void free_func_handle(struct tep_function_handler *func); |
| |
| void breakpoint(void) |
| { |
| static int x; |
| x++; |
| } |
| |
| static struct tep_print_arg *alloc_arg(void) |
| { |
| return calloc(1, sizeof(struct tep_print_arg)); |
| } |
| |
| struct tep_cmdline { |
| char *comm; |
| int pid; |
| }; |
| |
| static int cmdline_cmp(const void *a, const void *b) |
| { |
| const struct tep_cmdline *ca = a; |
| const struct tep_cmdline *cb = b; |
| |
| if (ca->pid < cb->pid) |
| return -1; |
| if (ca->pid > cb->pid) |
| return 1; |
| |
| return 0; |
| } |
| |
| /* Looking for where to place the key */ |
| static int cmdline_slot_cmp(const void *a, const void *b) |
| { |
| const struct tep_cmdline *ca = a; |
| const struct tep_cmdline *cb = b; |
| const struct tep_cmdline *cb1 = cb + 1; |
| |
| if (ca->pid < cb->pid) |
| return -1; |
| |
| if (ca->pid > cb->pid) { |
| if (ca->pid <= cb1->pid) |
| return 0; |
| return 1; |
| } |
| |
| return 0; |
| } |
| |
| struct cmdline_list { |
| struct cmdline_list *next; |
| char *comm; |
| int pid; |
| }; |
| |
| static int cmdline_init(struct tep_handle *tep) |
| { |
| struct cmdline_list *cmdlist = tep->cmdlist; |
| struct cmdline_list *item; |
| struct tep_cmdline *cmdlines; |
| int i; |
| |
| cmdlines = malloc(sizeof(*cmdlines) * tep->cmdline_count); |
| if (!cmdlines) |
| return -1; |
| |
| i = 0; |
| while (cmdlist) { |
| cmdlines[i].pid = cmdlist->pid; |
| cmdlines[i].comm = cmdlist->comm; |
| i++; |
| item = cmdlist; |
| cmdlist = cmdlist->next; |
| free(item); |
| } |
| |
| qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp); |
| |
| tep->cmdlines = cmdlines; |
| tep->cmdlist = NULL; |
| |
| return 0; |
| } |
| |
| static const char *find_cmdline(struct tep_handle *tep, int pid) |
| { |
| const struct tep_cmdline *comm; |
| struct tep_cmdline key; |
| |
| if (!pid) |
| return "<idle>"; |
| |
| if (!tep->cmdlines && cmdline_init(tep)) |
| return "<not enough memory for cmdlines!>"; |
| |
| key.pid = pid; |
| |
| comm = bsearch(&key, tep->cmdlines, tep->cmdline_count, |
| sizeof(*tep->cmdlines), cmdline_cmp); |
| |
| if (comm) |
| return comm->comm; |
| return "<...>"; |
| } |
| |
| /** |
| * tep_is_pid_registered - return if a pid has a cmdline registered |
| * @tep: a handle to the trace event parser context |
| * @pid: The pid to check if it has a cmdline registered with. |
| * |
| * Returns true if the pid has a cmdline mapped to it |
| * false otherwise. |
| */ |
| bool tep_is_pid_registered(struct tep_handle *tep, int pid) |
| { |
| const struct tep_cmdline *comm; |
| struct tep_cmdline key; |
| |
| if (!pid) |
| return true; |
| |
| if (!tep->cmdlines && cmdline_init(tep)) |
| return false; |
| |
| key.pid = pid; |
| |
| comm = bsearch(&key, tep->cmdlines, tep->cmdline_count, |
| sizeof(*tep->cmdlines), cmdline_cmp); |
| |
| if (comm) |
| return true; |
| return false; |
| } |
| |
| /* |
| * If the command lines have been converted to an array, then |
| * we must add this pid. This is much slower than when cmdlines |
| * are added before the array is initialized. |
| */ |
| static int add_new_comm(struct tep_handle *tep, |
| const char *comm, int pid, bool override) |
| { |
| struct tep_cmdline *cmdlines = tep->cmdlines; |
| struct tep_cmdline *cmdline; |
| struct tep_cmdline key; |
| char *new_comm; |
| int cnt; |
| |
| if (!pid) |
| return 0; |
| |
| /* avoid duplicates */ |
| key.pid = pid; |
| |
| cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count, |
| sizeof(*tep->cmdlines), cmdline_cmp); |
| if (cmdline) { |
| if (!override) { |
| errno = EEXIST; |
| return -1; |
| } |
| new_comm = strdup(comm); |
| if (!new_comm) { |
| errno = ENOMEM; |
| return -1; |
| } |
| free(cmdline->comm); |
| cmdline->comm = new_comm; |
| |
| return 0; |
| } |
| |
| cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1)); |
| if (!cmdlines) { |
| errno = ENOMEM; |
| return -1; |
| } |
| tep->cmdlines = cmdlines; |
| |
| key.comm = strdup(comm); |
| if (!key.comm) { |
| errno = ENOMEM; |
| return -1; |
| } |
| |
| if (!tep->cmdline_count) { |
| /* no entries yet */ |
| tep->cmdlines[0] = key; |
| tep->cmdline_count++; |
| return 0; |
| } |
| |
| /* Now find where we want to store the new cmdline */ |
| cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count - 1, |
| sizeof(*tep->cmdlines), cmdline_slot_cmp); |
| |
| cnt = tep->cmdline_count; |
| if (cmdline) { |
| /* cmdline points to the one before the spot we want */ |
| cmdline++; |
| cnt -= cmdline - tep->cmdlines; |
| |
| } else { |
| /* The new entry is either before or after the list */ |
| if (key.pid > tep->cmdlines[tep->cmdline_count - 1].pid) { |
| tep->cmdlines[tep->cmdline_count++] = key; |
| return 0; |
| } |
| cmdline = &tep->cmdlines[0]; |
| } |
| memmove(cmdline + 1, cmdline, (cnt * sizeof(*cmdline))); |
| *cmdline = key; |
| |
| tep->cmdline_count++; |
| |
| return 0; |
| } |
| |
| static int _tep_register_comm(struct tep_handle *tep, |
| const char *comm, int pid, bool override) |
| { |
| struct cmdline_list *item; |
| |
| if (tep->cmdlines) |
| return add_new_comm(tep, comm, pid, override); |
| |
| item = malloc(sizeof(*item)); |
| if (!item) |
| return -1; |
| |
| if (comm) |
| item->comm = strdup(comm); |
| else |
| item->comm = strdup("<...>"); |
| if (!item->comm) { |
| free(item); |
| return -1; |
| } |
| item->pid = pid; |
| item->next = tep->cmdlist; |
| |
| tep->cmdlist = item; |
| tep->cmdline_count++; |
| |
| return 0; |
| } |
| |
| /** |
| * tep_register_comm - register a pid / comm mapping |
| * @tep: a handle to the trace event parser context |
| * @comm: the command line to register |
| * @pid: the pid to map the command line to |
| * |
| * This adds a mapping to search for command line names with |
| * a given pid. The comm is duplicated. If a command with the same pid |
| * already exist, -1 is returned and errno is set to EEXIST |
| */ |
| int tep_register_comm(struct tep_handle *tep, const char *comm, int pid) |
| { |
| return _tep_register_comm(tep, comm, pid, false); |
| } |
| |
| /** |
| * tep_override_comm - register a pid / comm mapping |
| * @tep: a handle to the trace event parser context |
| * @comm: the command line to register |
| * @pid: the pid to map the command line to |
| * |
| * This adds a mapping to search for command line names with |
| * a given pid. The comm is duplicated. If a command with the same pid |
| * already exist, the command string is udapted with the new one |
| */ |
| int tep_override_comm(struct tep_handle *tep, const char *comm, int pid) |
| { |
| if (!tep->cmdlines && cmdline_init(tep)) { |
| errno = ENOMEM; |
| return -1; |
| } |
| return _tep_register_comm(tep, comm, pid, true); |
| } |
| |
| struct func_map { |
| unsigned long long addr; |
| char *func; |
| char *mod; |
| }; |
| |
| struct func_list { |
| struct func_list *next; |
| unsigned long long addr; |
| char *func; |
| char *mod; |
| }; |
| |
| static int func_cmp(const void *a, const void *b) |
| { |
| const struct func_map *fa = a; |
| const struct func_map *fb = b; |
| |
| if (fa->addr < fb->addr) |
| return -1; |
| if (fa->addr > fb->addr) |
| return 1; |
| |
| return 0; |
| } |
| |
| /* |
| * We are searching for a record in between, not an exact |
| * match. |
| */ |
| static int func_bcmp(const void *a, const void *b) |
| { |
| const struct func_map *fa = a; |
| const struct func_map *fb = b; |
| |
| if ((fa->addr == fb->addr) || |
| |
| (fa->addr > fb->addr && |
| fa->addr < (fb+1)->addr)) |
| return 0; |
| |
| if (fa->addr < fb->addr) |
| return -1; |
| |
| return 1; |
| } |
| |
| static int func_map_init(struct tep_handle *tep) |
| { |
| struct func_list *funclist; |
| struct func_list *item; |
| struct func_map *func_map; |
| int i; |
| |
| func_map = malloc(sizeof(*func_map) * (tep->func_count + 1)); |
| if (!func_map) |
| return -1; |
| |
| funclist = tep->funclist; |
| |
| i = 0; |
| while (funclist) { |
| func_map[i].func = funclist->func; |
| func_map[i].addr = funclist->addr; |
| func_map[i].mod = funclist->mod; |
| i++; |
| item = funclist; |
| funclist = funclist->next; |
| free(item); |
| } |
| |
| qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp); |
| |
| /* |
| * Add a special record at the end. |
| */ |
| func_map[tep->func_count].func = NULL; |
| func_map[tep->func_count].addr = 0; |
| func_map[tep->func_count].mod = NULL; |
| |
| tep->func_map = func_map; |
| tep->funclist = NULL; |
| |
| return 0; |
| } |
| |
| static struct func_map * |
| __find_func(struct tep_handle *tep, unsigned long long addr) |
| { |
| struct func_map *func; |
| struct func_map key; |
| |
| if (!tep->func_map) |
| func_map_init(tep); |
| |
| key.addr = addr; |
| |
| func = bsearch(&key, tep->func_map, tep->func_count, |
| sizeof(*tep->func_map), func_bcmp); |
| |
| return func; |
| } |
| |
| struct func_resolver { |
| tep_func_resolver_t *func; |
| void *priv; |
| struct func_map map; |
| }; |
| |
| /** |
| * tep_set_function_resolver - set an alternative function resolver |
| * @tep: a handle to the trace event parser context |
| * @resolver: function to be used |
| * @priv: resolver function private state. |
| * |
| * Some tools may have already a way to resolve kernel functions, allow them to |
| * keep using it instead of duplicating all the entries inside tep->funclist. |
| */ |
| int tep_set_function_resolver(struct tep_handle *tep, |
| tep_func_resolver_t *func, void *priv) |
| { |
| struct func_resolver *resolver = malloc(sizeof(*resolver)); |
| |
| if (resolver == NULL) |
| return -1; |
| |
| resolver->func = func; |
| resolver->priv = priv; |
| |
| free(tep->func_resolver); |
| tep->func_resolver = resolver; |
| |
| return 0; |
| } |
| |
| /** |
| * tep_reset_function_resolver - reset alternative function resolver |
| * @tep: a handle to the trace event parser context |
| * |
| * Stop using whatever alternative resolver was set, use the default |
| * one instead. |
| */ |
| void tep_reset_function_resolver(struct tep_handle *tep) |
| { |
| free(tep->func_resolver); |
| tep->func_resolver = NULL; |
| } |
| |
| static struct func_map * |
| find_func(struct tep_handle *tep, unsigned long long addr) |
| { |
| struct func_map *map; |
| |
| if (!tep->func_resolver) |
| return __find_func(tep, addr); |
| |
| map = &tep->func_resolver->map; |
| map->mod = NULL; |
| map->addr = addr; |
| map->func = tep->func_resolver->func(tep->func_resolver->priv, |
| &map->addr, &map->mod); |
| if (map->func == NULL) |
| return NULL; |
| |
| return map; |
| } |
| |
| /** |
| * tep_find_function - find a function by a given address |
| * @tep: a handle to the trace event parser context |
| * @addr: the address to find the function with |
| * |
| * Returns a pointer to the function stored that has the given |
| * address. Note, the address does not have to be exact, it |
| * will select the function that would contain the address. |
| */ |
| const char *tep_find_function(struct tep_handle *tep, unsigned long long addr) |
| { |
| struct func_map *map; |
| |
| map = find_func(tep, addr); |
| if (!map) |
| return NULL; |
| |
| return map->func; |
| } |
| |
| /** |
| * tep_find_function_address - find a function address by a given address |
| * @tep: a handle to the trace event parser context |
| * @addr: the address to find the function with |
| * |
| * Returns the address the function starts at. This can be used in |
| * conjunction with tep_find_function to print both the function |
| * name and the function offset. |
| */ |
| unsigned long long |
| tep_find_function_address(struct tep_handle *tep, unsigned long long addr) |
| { |
| struct func_map *map; |
| |
| map = find_func(tep, addr); |
| if (!map) |
| return 0; |
| |
| return map->addr; |
| } |
| |
| /** |
| * tep_register_function - register a function with a given address |
| * @tep: a handle to the trace event parser context |
| * @function: the function name to register |
| * @addr: the address the function starts at |
| * @mod: the kernel module the function may be in (NULL for none) |
| * |
| * This registers a function name with an address and module. |
| * The @func passed in is duplicated. |
| */ |
| int tep_register_function(struct tep_handle *tep, char *func, |
| unsigned long long addr, char *mod) |
| { |
| struct func_list *item = malloc(sizeof(*item)); |
| |
| if (!item) |
| return -1; |
| |
| item->next = tep->funclist; |
| item->func = strdup(func); |
| if (!item->func) |
| goto out_free; |
| |
| if (mod) { |
| item->mod = strdup(mod); |
| if (!item->mod) |
| goto out_free_func; |
| } else |
| item->mod = NULL; |
| item->addr = addr; |
| |
| tep->funclist = item; |
| tep->func_count++; |
| |
| return 0; |
| |
| out_free_func: |
| free(item->func); |
| item->func = NULL; |
| out_free: |
| free(item); |
| errno = ENOMEM; |
| return -1; |
| } |
| |
| /** |
| * tep_print_funcs - print out the stored functions |
| * @tep: a handle to the trace event parser context |
| * |
| * This prints out the stored functions. |
| */ |
| void tep_print_funcs(struct tep_handle *tep) |
| { |
| int i; |
| |
| if (!tep->func_map) |
| func_map_init(tep); |
| |
| for (i = 0; i < (int)tep->func_count; i++) { |
| printf("%016llx %s", |
| tep->func_map[i].addr, |
| tep->func_map[i].func); |
| if (tep->func_map[i].mod) |
| printf(" [%s]\n", tep->func_map[i].mod); |
| else |
| printf("\n"); |
| } |
| } |
| |
| struct printk_map { |
| unsigned long long addr; |
| char *printk; |
| }; |
| |
| struct printk_list { |
| struct printk_list *next; |
| unsigned long long addr; |
| char *printk; |
| }; |
| |
| static int printk_cmp(const void *a, const void *b) |
| { |
| const struct printk_map *pa = a; |
| const struct printk_map *pb = b; |
| |
| if (pa->addr < pb->addr) |
| return -1; |
| if (pa->addr > pb->addr) |
| return 1; |
| |
| return 0; |
| } |
| |
| static int printk_map_init(struct tep_handle *tep) |
| { |
| struct printk_list *printklist; |
| struct printk_list *item; |
| struct printk_map *printk_map; |
| int i; |
| |
| printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1)); |
| if (!printk_map) |
| return -1; |
| |
| printklist = tep->printklist; |
| |
| i = 0; |
| while (printklist) { |
| printk_map[i].printk = printklist->printk; |
| printk_map[i].addr = printklist->addr; |
| i++; |
| item = printklist; |
| printklist = printklist->next; |
| free(item); |
| } |
| |
| qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp); |
| |
| tep->printk_map = printk_map; |
| tep->printklist = NULL; |
| |
| return 0; |
| } |
| |
| static struct printk_map * |
| find_printk(struct tep_handle *tep, unsigned long long addr) |
| { |
| struct printk_map *printk; |
| struct printk_map key; |
| |
| if (!tep->printk_map && printk_map_init(tep)) |
| return NULL; |
| |
| key.addr = addr; |
| |
| printk = bsearch(&key, tep->printk_map, tep->printk_count, |
| sizeof(*tep->printk_map), printk_cmp); |
| |
| return printk; |
| } |
| |
| /** |
| * tep_register_print_string - register a string by its address |
| * @tep: a handle to the trace event parser context |
| * @fmt: the string format to register |
| * @addr: the address the string was located at |
| * |
| * This registers a string by the address it was stored in the kernel. |
| * The @fmt passed in is duplicated. |
| */ |
| int tep_register_print_string(struct tep_handle *tep, const char *fmt, |
| unsigned long long addr) |
| { |
| struct printk_list *item = malloc(sizeof(*item)); |
| char *p; |
| |
| if (!item) |
| return -1; |
| |
| item->next = tep->printklist; |
| item->addr = addr; |
| |
| /* Strip off quotes and '\n' from the end */ |
| if (fmt[0] == '"') |
| fmt++; |
| item->printk = strdup(fmt); |
| if (!item->printk) |
| goto out_free; |
| |
| p = item->printk + strlen(item->printk) - 1; |
| if (*p == '"') |
| *p = 0; |
| |
| p -= 2; |
| if (strcmp(p, "\\n") == 0) |
| *p = 0; |
| |
| tep->printklist = item; |
| tep->printk_count++; |
| |
| return 0; |
| |
| out_free: |
| free(item); |
| errno = ENOMEM; |
| return -1; |
| } |
| |
| /** |
| * tep_print_printk - print out the stored strings |
| * @tep: a handle to the trace event parser context |
| * |
| * This prints the string formats that were stored. |
| */ |
| void tep_print_printk(struct tep_handle *tep) |
| { |
| int i; |
| |
| if (!tep->printk_map) |
| printk_map_init(tep); |
| |
| for (i = 0; i < (int)tep->printk_count; i++) { |
| printf("%016llx %s\n", |
| tep->printk_map[i].addr, |
| tep->printk_map[i].printk); |
| } |
| } |
| |
| static struct tep_event *alloc_event(void) |
| { |
| return calloc(1, sizeof(struct tep_event)); |
| } |
| |
| static int add_event(struct tep_handle *tep, struct tep_event *event) |
| { |
| int i; |
| struct tep_event **events = realloc(tep->events, sizeof(event) * |
| (tep->nr_events + 1)); |
| if (!events) |
| return -1; |
| |
| tep->events = events; |
| |
| for (i = 0; i < tep->nr_events; i++) { |
| if (tep->events[i]->id > event->id) |
| break; |
| } |
| if (i < tep->nr_events) |
| memmove(&tep->events[i + 1], |
| &tep->events[i], |
| sizeof(event) * (tep->nr_events - i)); |
| |
| tep->events[i] = event; |
| tep->nr_events++; |
| |
| event->tep = tep; |
| |
| return 0; |
| } |
| |
| static int event_item_type(enum tep_event_type type) |
| { |
| switch (type) { |
| case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE: |
| return 1; |
| case TEP_EVENT_ERROR ... TEP_EVENT_DELIM: |
| default: |
| return 0; |
| } |
| } |
| |
| static void free_flag_sym(struct tep_print_flag_sym *fsym) |
| { |
| struct tep_print_flag_sym *next; |
| |
| while (fsym) { |
| next = fsym->next; |
| free(fsym->value); |
| free(fsym->str); |
| free(fsym); |
| fsym = next; |
| } |
| } |
| |
| static void free_arg(struct tep_print_arg *arg) |
| { |
| struct tep_print_arg *farg; |
| |
| if (!arg) |
| return; |
| |
| switch (arg->type) { |
| case TEP_PRINT_ATOM: |
| free(arg->atom.atom); |
| break; |
| case TEP_PRINT_FIELD: |
| free(arg->field.name); |
| break; |
| case TEP_PRINT_FLAGS: |
| free_arg(arg->flags.field); |
| free(arg->flags.delim); |
| free_flag_sym(arg->flags.flags); |
| break; |
| case TEP_PRINT_SYMBOL: |
| free_arg(arg->symbol.field); |
| free_flag_sym(arg->symbol.symbols); |
| break; |
| case TEP_PRINT_HEX: |
| case TEP_PRINT_HEX_STR: |
| free_arg(arg->hex.field); |
| free_arg(arg->hex.size); |
| break; |
| case TEP_PRINT_INT_ARRAY: |
| free_arg(arg->int_array.field); |
| free_arg(arg->int_array.count); |
| free_arg(arg->int_array.el_size); |
| break; |
| case TEP_PRINT_TYPE: |
| free(arg->typecast.type); |
| free_arg(arg->typecast.item); |
| break; |
| case TEP_PRINT_STRING: |
| case TEP_PRINT_BSTRING: |
| free(arg->string.string); |
| break; |
| case TEP_PRINT_BITMASK: |
| free(arg->bitmask.bitmask); |
| break; |
| case TEP_PRINT_DYNAMIC_ARRAY: |
| case TEP_PRINT_DYNAMIC_ARRAY_LEN: |
| free(arg->dynarray.index); |
| break; |
| case TEP_PRINT_OP: |
| free(arg->op.op); |
| free_arg(arg->op.left); |
| free_arg(arg->op.right); |
| break; |
| case TEP_PRINT_FUNC: |
| while (arg->func.args) { |
| farg = arg->func.args; |
| arg->func.args = farg->next; |
| free_arg(farg); |
| } |
| break; |
| |
| case TEP_PRINT_NULL: |
| default: |
| break; |
| } |
| |
| free(arg); |
| } |
| |
| static enum tep_event_type get_type(int ch) |
| { |
| if (ch == '\n') |
| return TEP_EVENT_NEWLINE; |
| if (isspace(ch)) |
| return TEP_EVENT_SPACE; |
| if (isalnum(ch) || ch == '_') |
| return TEP_EVENT_ITEM; |
| if (ch == '\'') |
| return TEP_EVENT_SQUOTE; |
| if (ch == '"') |
| return TEP_EVENT_DQUOTE; |
| if (!isprint(ch)) |
| return TEP_EVENT_NONE; |
| if (ch == '(' || ch == ')' || ch == ',') |
| return TEP_EVENT_DELIM; |
| |
| return TEP_EVENT_OP; |
| } |
| |
| static int __read_char(void) |
| { |
| if (input_buf_ptr >= input_buf_siz) |
| return -1; |
| |
| return input_buf[input_buf_ptr++]; |
| } |
| |
| /** |
| * peek_char - peek at the next character that will be read |
| * |
| * Returns the next character read, or -1 if end of buffer. |
| */ |
| __hidden int peek_char(void) |
| { |
| if (input_buf_ptr >= input_buf_siz) |
| return -1; |
| |
| return input_buf[input_buf_ptr]; |
| } |
| |
| static int extend_token(char **tok, char *buf, int size) |
| { |
| char *newtok = realloc(*tok, size); |
| |
| if (!newtok) { |
| free(*tok); |
| *tok = NULL; |
| return -1; |
| } |
| |
| if (!*tok) |
| strcpy(newtok, buf); |
| else |
| strcat(newtok, buf); |
| *tok = newtok; |
| |
| return 0; |
| } |
| |
| static enum tep_event_type force_token(const char *str, char **tok); |
| |
| static enum tep_event_type __read_token(char **tok) |
| { |
| char buf[BUFSIZ]; |
| int ch, last_ch, quote_ch, next_ch; |
| int i = 0; |
| int tok_size = 0; |
| enum tep_event_type type; |
| |
| *tok = NULL; |
| |
| |
| ch = __read_char(); |
| if (ch < 0) |
| return TEP_EVENT_NONE; |
| |
| type = get_type(ch); |
| if (type == TEP_EVENT_NONE) |
| return type; |
| |
| buf[i++] = ch; |
| |
| switch (type) { |
| case TEP_EVENT_NEWLINE: |
| case TEP_EVENT_DELIM: |
| if (asprintf(tok, "%c", ch) < 0) |
| return TEP_EVENT_ERROR; |
| |
| return type; |
| |
| case TEP_EVENT_OP: |
| switch (ch) { |
| case '-': |
| next_ch = peek_char(); |
| if (next_ch == '>') { |
| buf[i++] = __read_char(); |
| break; |
| } |
| /* fall through */ |
| case '+': |
| case '|': |
| case '&': |
| case '>': |
| case '<': |
| last_ch = ch; |
| ch = peek_char(); |
| if (ch != last_ch) |
| goto test_equal; |
| buf[i++] = __read_char(); |
| switch (last_ch) { |
| case '>': |
| case '<': |
| goto test_equal; |
| default: |
| break; |
| } |
| break; |
| case '!': |
| case '=': |
| goto test_equal; |
| default: /* what should we do instead? */ |
| break; |
| } |
| buf[i] = 0; |
| *tok = strdup(buf); |
| return type; |
| |
| test_equal: |
| ch = peek_char(); |
| if (ch == '=') |
| buf[i++] = __read_char(); |
| goto out; |
| |
| case TEP_EVENT_DQUOTE: |
| case TEP_EVENT_SQUOTE: |
| /* don't keep quotes */ |
| i--; |
| quote_ch = ch; |
| last_ch = 0; |
| concat: |
| do { |
| if (i == (BUFSIZ - 1)) { |
| buf[i] = 0; |
| tok_size += BUFSIZ; |
| |
| if (extend_token(tok, buf, tok_size) < 0) |
| return TEP_EVENT_NONE; |
| i = 0; |
| } |
| last_ch = ch; |
| ch = __read_char(); |
| buf[i++] = ch; |
| /* the '\' '\' will cancel itself */ |
| if (ch == '\\' && last_ch == '\\') |
| last_ch = 0; |
| } while (ch != quote_ch || last_ch == '\\'); |
| /* remove the last quote */ |
| i--; |
| |
| /* |
| * For strings (double quotes) check the next token. |
| * If it is another string, concatinate the two. |
| */ |
| if (type == TEP_EVENT_DQUOTE) { |
| unsigned long long save_input_buf_ptr = input_buf_ptr; |
| |
| do { |
| ch = __read_char(); |
| } while (isspace(ch)); |
| if (ch == '"') |
| goto concat; |
| input_buf_ptr = save_input_buf_ptr; |
| } |
| |
| goto out; |
| |
| case TEP_EVENT_ERROR ... TEP_EVENT_SPACE: |
| case TEP_EVENT_ITEM: |
| default: |
| break; |
| } |
| |
| while (get_type(peek_char()) == type) { |
| if (i == (BUFSIZ - 1)) { |
| buf[i] = 0; |
| tok_size += BUFSIZ; |
| |
| if (extend_token(tok, buf, tok_size) < 0) |
| return TEP_EVENT_NONE; |
| i = 0; |
| } |
| ch = __read_char(); |
| buf[i++] = ch; |
| } |
| |
| out: |
| buf[i] = 0; |
| if (extend_token(tok, buf, tok_size + i + 1) < 0) |
| return TEP_EVENT_NONE; |
| |
| if (type == TEP_EVENT_ITEM) { |
| /* |
| * Older versions of the kernel has a bug that |
| * creates invalid symbols and will break the mac80211 |
| * parsing. This is a work around to that bug. |
| * |
| * See Linux kernel commit: |
| * 811cb50baf63461ce0bdb234927046131fc7fa8b |
| */ |
| if (strcmp(*tok, "LOCAL_PR_FMT") == 0) { |
| free(*tok); |
| *tok = NULL; |
| return force_token("\"%s\" ", tok); |
| } else if (strcmp(*tok, "STA_PR_FMT") == 0) { |
| free(*tok); |
| *tok = NULL; |
| return force_token("\" sta:%pM\" ", tok); |
| } else if (strcmp(*tok, "VIF_PR_FMT") == 0) { |
| free(*tok); |
| *tok = NULL; |
| return force_token("\" vif:%p(%d)\" ", tok); |
| } |
| } |
| |
| return type; |
| } |
| |
| static enum tep_event_type force_token(const char *str, char **tok) |
| { |
| const char *save_input_buf; |
| unsigned long long save_input_buf_ptr; |
| unsigned long long save_input_buf_siz; |
| enum tep_event_type type; |
| |
| /* save off the current input pointers */ |
| save_input_buf = input_buf; |
| save_input_buf_ptr = input_buf_ptr; |
| save_input_buf_siz = input_buf_siz; |
| |
| init_input_buf(str, strlen(str)); |
| |
| type = __read_token(tok); |
| |
| /* reset back to original token */ |
| input_buf = save_input_buf; |
| input_buf_ptr = save_input_buf_ptr; |
| input_buf_siz = save_input_buf_siz; |
| |
| return type; |
| } |
| |
| /** |
| * free_token - free a token returned by tep_read_token |
| * @token: the token to free |
| */ |
| __hidden void free_token(char *tok) |
| { |
| if (tok) |
| free(tok); |
| } |
| |
| /** |
| * read_token - access to utilities to use the tep parser |
| * @tok: The token to return |
| * |
| * This will parse tokens from the string given by |
| * tep_init_data(). |
| * |
| * Returns the token type. |
| */ |
| __hidden enum tep_event_type read_token(char **tok) |
| { |
| enum tep_event_type type; |
| |
| for (;;) { |
| type = __read_token(tok); |
| if (type != TEP_EVENT_SPACE) |
| return type; |
| |
| free_token(*tok); |
| } |
| |
| /* not reached */ |
| *tok = NULL; |
| return TEP_EVENT_NONE; |
| } |
| |
| /* no newline */ |
| static enum tep_event_type read_token_item(char **tok) |
| { |
| enum tep_event_type type; |
| |
| for (;;) { |
| type = __read_token(tok); |
| if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE) |
| return type; |
| free_token(*tok); |
| *tok = NULL; |
| } |
| |
| /* not reached */ |
| *tok = NULL; |
| return TEP_EVENT_NONE; |
| } |
| |
| static int test_type(enum tep_event_type type, enum tep_event_type expect) |
| { |
| if (type != expect) { |
| do_warning("Error: expected type %d but read %d", |
| expect, type); |
| return -1; |
| } |
| return 0; |
| } |
| |
| static int test_type_token(enum tep_event_type type, const char *token, |
| enum tep_event_type expect, const char *expect_tok) |
| { |
| if (type != expect) { |
| do_warning("Error: expected type %d but read %d", |
| expect, type); |
| return -1; |
| } |
| |
| if (strcmp(token, expect_tok) != 0) { |
| do_warning("Error: expected '%s' but read '%s'", |
| expect_tok, token); |
| return -1; |
| } |
| return 0; |
| } |
| |
| static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok) |
| { |
| enum tep_event_type type; |
| |
| if (newline_ok) |
| type = read_token(tok); |
| else |
| type = read_token_item(tok); |
| return test_type(type, expect); |
| } |
| |
| static int read_expect_type(enum tep_event_type expect, char **tok) |
| { |
| return __read_expect_type(expect, tok, 1); |
| } |
| |
| static int __read_expected(enum tep_event_type expect, const char *str, |
| int newline_ok) |
| { |
| enum tep_event_type type; |
| char *token; |
| int ret; |
| |
| if (newline_ok) |
| type = read_token(&token); |
| else |
| type = read_token_item(&token); |
| |
| ret = test_type_token(type, token, expect, str); |
| |
| free_token(token); |
| |
| return ret; |
| } |
| |
| static int read_expected(enum tep_event_type expect, const char *str) |
| { |
| return __read_expected(expect, str, 1); |
| } |
| |
| static int read_expected_item(enum tep_event_type expect, const char *str) |
| { |
| return __read_expected(expect, str, 0); |
| } |
| |
| static char *event_read_name(void) |
| { |
| char *token; |
| |
| if (read_expected(TEP_EVENT_ITEM, "name") < 0) |
| return NULL; |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| return NULL; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto fail; |
| |
| return token; |
| |
| fail: |
| free_token(token); |
| return NULL; |
| } |
| |
| static int event_read_id(void) |
| { |
| char *token; |
| int id; |
| |
| if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0) |
| return -1; |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| return -1; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto fail; |
| |
| id = strtoul(token, NULL, 0); |
| free_token(token); |
| return id; |
| |
| fail: |
| free_token(token); |
| return -1; |
| } |
| |
| static int field_is_string(struct tep_format_field *field) |
| { |
| if ((field->flags & TEP_FIELD_IS_ARRAY) && |
| (strstr(field->type, "char") || strstr(field->type, "u8") || |
| strstr(field->type, "s8"))) |
| return 1; |
| |
| return 0; |
| } |
| |
| static int field_is_dynamic(struct tep_format_field *field) |
| { |
| if (strncmp(field->type, "__data_loc", 10) == 0) |
| return 1; |
| |
| return 0; |
| } |
| |
| static int field_is_long(struct tep_format_field *field) |
| { |
| /* includes long long */ |
| if (strstr(field->type, "long")) |
| return 1; |
| |
| return 0; |
| } |
| |
| static unsigned int type_size(const char *name) |
| { |
| /* This covers all TEP_FIELD_IS_STRING types. */ |
| static struct { |
| const char *type; |
| unsigned int size; |
| } table[] = { |
| { "u8", 1 }, |
| { "u16", 2 }, |
| { "u32", 4 }, |
| { "u64", 8 }, |
| { "s8", 1 }, |
| { "s16", 2 }, |
| { "s32", 4 }, |
| { "s64", 8 }, |
| { "char", 1 }, |
| { }, |
| }; |
| int i; |
| |
| for (i = 0; table[i].type; i++) { |
| if (!strcmp(table[i].type, name)) |
| return table[i].size; |
| } |
| |
| return 0; |
| } |
| |
| static int append(char **buf, const char *delim, const char *str) |
| { |
| char *new_buf; |
| |
| new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1); |
| if (!new_buf) |
| return -1; |
| strcat(new_buf, delim); |
| strcat(new_buf, str); |
| *buf = new_buf; |
| return 0; |
| } |
| |
| static int event_read_fields(struct tep_event *event, struct tep_format_field **fields) |
| { |
| struct tep_format_field *field = NULL; |
| enum tep_event_type type; |
| char *token; |
| char *last_token; |
| char *delim = " "; |
| int count = 0; |
| int ret; |
| |
| do { |
| unsigned int size_dynamic = 0; |
| |
| type = read_token(&token); |
| if (type == TEP_EVENT_NEWLINE) { |
| free_token(token); |
| return count; |
| } |
| |
| count++; |
| |
| if (test_type_token(type, token, TEP_EVENT_ITEM, "field")) |
| goto fail; |
| free_token(token); |
| |
| type = read_token(&token); |
| /* |
| * The ftrace fields may still use the "special" name. |
| * Just ignore it. |
| */ |
| if (event->flags & TEP_EVENT_FL_ISFTRACE && |
| type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) { |
| free_token(token); |
| type = read_token(&token); |
| } |
| |
| if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0) |
| goto fail; |
| |
| free_token(token); |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto fail; |
| |
| last_token = token; |
| |
| field = calloc(1, sizeof(*field)); |
| if (!field) |
| goto fail; |
| |
| field->event = event; |
| |
| /* read the rest of the type */ |
| for (;;) { |
| type = read_token(&token); |
| if (type == TEP_EVENT_ITEM || |
| (type == TEP_EVENT_OP && strcmp(token, "*") == 0) || |
| /* |
| * Some of the ftrace fields are broken and have |
| * an illegal "." in them. |
| */ |
| (event->flags & TEP_EVENT_FL_ISFTRACE && |
| type == TEP_EVENT_OP && strcmp(token, ".") == 0)) { |
| |
| if (strcmp(token, "*") == 0) |
| field->flags |= TEP_FIELD_IS_POINTER; |
| |
| if (field->type) { |
| ret = append(&field->type, delim, last_token); |
| free(last_token); |
| if (ret < 0) |
| goto fail; |
| } else |
| field->type = last_token; |
| last_token = token; |
| delim = " "; |
| continue; |
| } |
| |
| /* Handle __attribute__((user)) */ |
| if ((type == TEP_EVENT_DELIM) && |
| strcmp("__attribute__", last_token) == 0 && |
| token[0] == '(') { |
| int depth = 1; |
| int ret; |
| |
| ret = append(&field->type, " ", last_token); |
| ret |= append(&field->type, "", "("); |
| if (ret < 0) |
| goto fail; |
| |
| delim = " "; |
| while ((type = read_token(&token)) != TEP_EVENT_NONE) { |
| if (type == TEP_EVENT_DELIM) { |
| if (token[0] == '(') |
| depth++; |
| else if (token[0] == ')') |
| depth--; |
| if (!depth) |
| break; |
| ret = append(&field->type, "", token); |
| delim = ""; |
| } else { |
| ret = append(&field->type, delim, token); |
| delim = " "; |
| } |
| if (ret < 0) |
| goto fail; |
| free(last_token); |
| last_token = token; |
| } |
| continue; |
| } |
| break; |
| } |
| |
| if (!field->type) { |
| do_warning_event(event, "%s: no type found", __func__); |
| goto fail; |
| } |
| field->name = field->alias = last_token; |
| |
| if (test_type(type, TEP_EVENT_OP)) |
| goto fail; |
| |
| if (strcmp(token, "[") == 0) { |
| enum tep_event_type last_type = type; |
| char *brackets = token; |
| |
| field->flags |= TEP_FIELD_IS_ARRAY; |
| |
| type = read_token(&token); |
| |
| if (type == TEP_EVENT_ITEM) |
| field->arraylen = strtoul(token, NULL, 0); |
| else |
| field->arraylen = 0; |
| |
| while (strcmp(token, "]") != 0) { |
| const char *delim; |
| |
| if (last_type == TEP_EVENT_ITEM && |
| type == TEP_EVENT_ITEM) |
| delim = " "; |
| else |
| delim = ""; |
| |
| last_type = type; |
| |
| ret = append(&brackets, delim, token); |
| if (ret < 0) { |
| free(brackets); |
| goto fail; |
| } |
| /* We only care about the last token */ |
| field->arraylen = strtoul(token, NULL, 0); |
| free_token(token); |
| type = read_token(&token); |
| if (type == TEP_EVENT_NONE) { |
| free(brackets); |
| do_warning_event(event, "failed to find token"); |
| goto fail; |
| } |
| } |
| |
| free_token(token); |
| |
| ret = append(&brackets, "", "]"); |
| if (ret < 0) { |
| free(brackets); |
| goto fail; |
| } |
| |
| /* add brackets to type */ |
| |
| type = read_token(&token); |
| /* |
| * If the next token is not an OP, then it is of |
| * the format: type [] item; |
| */ |
| if (type == TEP_EVENT_ITEM) { |
| ret = append(&field->type, " ", field->name); |
| if (ret < 0) { |
| free(brackets); |
| goto fail; |
| } |
| ret = append(&field->type, "", brackets); |
| |
| size_dynamic = type_size(field->name); |
| free_token(field->name); |
| field->name = field->alias = token; |
| type = read_token(&token); |
| } else { |
| ret = append(&field->type, "", brackets); |
| if (ret < 0) { |
| free(brackets); |
| goto fail; |
| } |
| } |
| free(brackets); |
| } |
| |
| if (field_is_string(field)) |
| field->flags |= TEP_FIELD_IS_STRING; |
| if (field_is_dynamic(field)) |
| field->flags |= TEP_FIELD_IS_DYNAMIC; |
| if (field_is_long(field)) |
| field->flags |= TEP_FIELD_IS_LONG; |
| |
| if (test_type_token(type, token, TEP_EVENT_OP, ";")) |
| goto fail; |
| free_token(token); |
| |
| if (read_expected(TEP_EVENT_ITEM, "offset") < 0) |
| goto fail_expect; |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| goto fail_expect; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token)) |
| goto fail; |
| field->offset = strtoul(token, NULL, 0); |
| free_token(token); |
| |
| if (read_expected(TEP_EVENT_OP, ";") < 0) |
| goto fail_expect; |
| |
| if (read_expected(TEP_EVENT_ITEM, "size") < 0) |
| goto fail_expect; |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| goto fail_expect; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token)) |
| goto fail; |
| field->size = strtoul(token, NULL, 0); |
| free_token(token); |
| |
| if (read_expected(TEP_EVENT_OP, ";") < 0) |
| goto fail_expect; |
| |
| type = read_token(&token); |
| if (type != TEP_EVENT_NEWLINE) { |
| /* newer versions of the kernel have a "signed" type */ |
| if (test_type_token(type, token, TEP_EVENT_ITEM, "signed")) |
| goto fail; |
| |
| free_token(token); |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| goto fail_expect; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token)) |
| goto fail; |
| |
| if (strtoul(token, NULL, 0)) |
| field->flags |= TEP_FIELD_IS_SIGNED; |
| |
| free_token(token); |
| if (read_expected(TEP_EVENT_OP, ";") < 0) |
| goto fail_expect; |
| |
| if (read_expect_type(TEP_EVENT_NEWLINE, &token)) |
| goto fail; |
| } |
| |
| free_token(token); |
| |
| if (field->flags & TEP_FIELD_IS_ARRAY) { |
| if (field->arraylen) |
| field->elementsize = field->size / field->arraylen; |
| else if (field->flags & TEP_FIELD_IS_DYNAMIC) |
| field->elementsize = size_dynamic; |
| else if (field->flags & TEP_FIELD_IS_STRING) |
| field->elementsize = 1; |
| else if (field->flags & TEP_FIELD_IS_LONG) |
| field->elementsize = event->tep ? |
| event->tep->long_size : |
| sizeof(long); |
| } else |
| field->elementsize = field->size; |
| |
| *fields = field; |
| fields = &field->next; |
| |
| } while (1); |
| |
| return 0; |
| |
| fail: |
| free_token(token); |
| fail_expect: |
| if (field) { |
| free(field->type); |
| free(field->name); |
| free(field); |
| } |
| return -1; |
| } |
| |
| static int event_read_format(struct tep_event *event) |
| { |
| char *token; |
| int ret; |
| |
| if (read_expected_item(TEP_EVENT_ITEM, "format") < 0) |
| return -1; |
| |
| if (read_expected(TEP_EVENT_OP, ":") < 0) |
| return -1; |
| |
| if (read_expect_type(TEP_EVENT_NEWLINE, &token)) |
| goto fail; |
| free_token(token); |
| |
| ret = event_read_fields(event, &event->format.common_fields); |
| if (ret < 0) |
| return ret; |
| event->format.nr_common = ret; |
| |
| ret = event_read_fields(event, &event->format.fields); |
| if (ret < 0) |
| return ret; |
| event->format.nr_fields = ret; |
| |
| return 0; |
| |
| fail: |
| free_token(token); |
| return -1; |
| } |
| |
| static enum tep_event_type |
| process_arg_token(struct tep_event *event, struct tep_print_arg *arg, |
| char **tok, enum tep_event_type type); |
| |
| static enum tep_event_type |
| process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| enum tep_event_type type; |
| char *token; |
| |
| type = read_token(&token); |
| *tok = token; |
| |
| return process_arg_token(event, arg, tok, type); |
| } |
| |
| static enum tep_event_type |
| process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok); |
| |
| /* |
| * For __print_symbolic() and __print_flags, we need to completely |
| * evaluate the first argument, which defines what to print next. |
| */ |
| static enum tep_event_type |
| process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| enum tep_event_type type; |
| |
| type = process_arg(event, arg, tok); |
| |
| while (type == TEP_EVENT_OP) { |
| type = process_op(event, arg, tok); |
| } |
| |
| return type; |
| } |
| |
| static enum tep_event_type |
| process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok) |
| { |
| struct tep_print_arg *arg, *left, *right; |
| enum tep_event_type type; |
| char *token = NULL; |
| |
| arg = alloc_arg(); |
| left = alloc_arg(); |
| right = alloc_arg(); |
| |
| if (!arg || !left || !right) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| /* arg will be freed at out_free */ |
| free_arg(left); |
| free_arg(right); |
| goto out_free; |
| } |
| |
| arg->type = TEP_PRINT_OP; |
| arg->op.left = left; |
| arg->op.right = right; |
| |
| *tok = NULL; |
| type = process_arg(event, left, &token); |
| |
| again: |
| if (type == TEP_EVENT_ERROR) |
| goto out_free; |
| |
| /* Handle other operations in the arguments */ |
| if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) { |
| type = process_op(event, left, &token); |
| goto again; |
| } |
| |
| if (test_type_token(type, token, TEP_EVENT_OP, ":")) |
| goto out_free; |
| |
| arg->op.op = token; |
| |
| type = process_arg(event, right, &token); |
| |
| top->op.right = arg; |
| |
| *tok = token; |
| return type; |
| |
| out_free: |
| /* Top may point to itself */ |
| top->op.right = NULL; |
| free_token(token); |
| free_arg(arg); |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_array(struct tep_event *event, struct tep_print_arg *top, char **tok) |
| { |
| struct tep_print_arg *arg; |
| enum tep_event_type type; |
| char *token = NULL; |
| |
| arg = alloc_arg(); |
| if (!arg) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| /* '*tok' is set to top->op.op. No need to free. */ |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| *tok = NULL; |
| type = process_arg(event, arg, &token); |
| if (test_type_token(type, token, TEP_EVENT_OP, "]")) |
| goto out_free; |
| |
| top->op.right = arg; |
| |
| free_token(token); |
| type = read_token_item(&token); |
| *tok = token; |
| |
| return type; |
| |
| out_free: |
| free_token(token); |
| free_arg(arg); |
| return TEP_EVENT_ERROR; |
| } |
| |
| static int get_op_prio(char *op) |
| { |
| if (!op[1]) { |
| switch (op[0]) { |
| case '~': |
| case '!': |
| return 4; |
| case '*': |
| case '/': |
| case '%': |
| return 6; |
| case '+': |
| case '-': |
| return 7; |
| /* '>>' and '<<' are 8 */ |
| case '<': |
| case '>': |
| return 9; |
| /* '==' and '!=' are 10 */ |
| case '&': |
| return 11; |
| case '^': |
| return 12; |
| case '|': |
| return 13; |
| case '?': |
| return 16; |
| default: |
| do_warning("unknown op '%c'", op[0]); |
| return -1; |
| } |
| } else { |
| if (strcmp(op, "++") == 0 || |
| strcmp(op, "--") == 0) { |
| return 3; |
| } else if (strcmp(op, ">>") == 0 || |
| strcmp(op, "<<") == 0) { |
| return 8; |
| } else if (strcmp(op, ">=") == 0 || |
| strcmp(op, "<=") == 0) { |
| return 9; |
| } else if (strcmp(op, "==") == 0 || |
| strcmp(op, "!=") == 0) { |
| return 10; |
| } else if (strcmp(op, "&&") == 0) { |
| return 14; |
| } else if (strcmp(op, "||") == 0) { |
| return 15; |
| } else { |
| do_warning("unknown op '%s'", op); |
| return -1; |
| } |
| } |
| } |
| |
| static int set_op_prio(struct tep_print_arg *arg) |
| { |
| |
| /* single ops are the greatest */ |
| if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL) |
| arg->op.prio = 0; |
| else |
| arg->op.prio = get_op_prio(arg->op.op); |
| |
| return arg->op.prio; |
| } |
| |
| /* Note, *tok does not get freed, but will most likely be saved */ |
| static enum tep_event_type |
| process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_print_arg *left, *right = NULL; |
| enum tep_event_type type; |
| char *token; |
| |
| /* the op is passed in via tok */ |
| token = *tok; |
| |
| if (arg->type == TEP_PRINT_OP && !arg->op.left) { |
| /* handle single op */ |
| if (token[1]) { |
| do_warning_event(event, "bad op token %s", token); |
| goto out_free; |
| } |
| switch (token[0]) { |
| case '~': |
| case '!': |
| case '+': |
| case '-': |
| break; |
| default: |
| do_warning_event(event, "bad op token %s", token); |
| goto out_free; |
| |
| } |
| |
| /* make an empty left */ |
| left = alloc_arg(); |
| if (!left) |
| goto out_warn_free; |
| |
| left->type = TEP_PRINT_NULL; |
| arg->op.left = left; |
| |
| right = alloc_arg(); |
| if (!right) |
| goto out_warn_free; |
| |
| arg->op.right = right; |
| |
| /* do not free the token, it belongs to an op */ |
| *tok = NULL; |
| type = process_arg(event, right, tok); |
| |
| } else if (strcmp(token, "?") == 0) { |
| |
| left = alloc_arg(); |
| if (!left) |
| goto out_warn_free; |
| |
| /* copy the top arg to the left */ |
| *left = *arg; |
| |
| arg->type = TEP_PRINT_OP; |
| arg->op.op = token; |
| arg->op.left = left; |
| arg->op.prio = 0; |
| |
| /* it will set arg->op.right */ |
| type = process_cond(event, arg, tok); |
| |
| } else if (strcmp(token, ">>") == 0 || |
| strcmp(token, "<<") == 0 || |
| strcmp(token, "&") == 0 || |
| strcmp(token, "|") == 0 || |
| strcmp(token, "&&") == 0 || |
| strcmp(token, "||") == 0 || |
| strcmp(token, "-") == 0 || |
| strcmp(token, "+") == 0 || |
| strcmp(token, "*") == 0 || |
| strcmp(token, "^") == 0 || |
| strcmp(token, "/") == 0 || |
| strcmp(token, "%") == 0 || |
| strcmp(token, "<") == 0 || |
| strcmp(token, ">") == 0 || |
| strcmp(token, "<=") == 0 || |
| strcmp(token, ">=") == 0 || |
| strcmp(token, "==") == 0 || |
| strcmp(token, "!=") == 0) { |
| |
| left = alloc_arg(); |
| if (!left) |
| goto out_warn_free; |
| |
| /* copy the top arg to the left */ |
| *left = *arg; |
| |
| arg->type = TEP_PRINT_OP; |
| arg->op.op = token; |
| arg->op.left = left; |
| arg->op.right = NULL; |
| |
| if (set_op_prio(arg) == -1) { |
| event->flags |= TEP_EVENT_FL_FAILED; |
| /* arg->op.op (= token) will be freed at out_free */ |
| arg->op.op = NULL; |
| goto out_free; |
| } |
| |
| type = read_token_item(&token); |
| *tok = token; |
| |
| /* could just be a type pointer */ |
| if ((strcmp(arg->op.op, "*") == 0) && |
| type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) { |
| int ret; |
| |
| if (left->type != TEP_PRINT_ATOM) { |
| do_warning_event(event, "bad pointer type"); |
| goto out_free; |
| } |
| ret = append(&left->atom.atom, " ", "*"); |
| if (ret < 0) |
| goto out_warn_free; |
| |
| free(arg->op.op); |
| *arg = *left; |
| free(left); |
| |
| return type; |
| } |
| |
| right = alloc_arg(); |
| if (!right) |
| goto out_warn_free; |
| |
| type = process_arg_token(event, right, tok, type); |
| if (type == TEP_EVENT_ERROR) { |
| free_arg(right); |
| /* token was freed in process_arg_token() via *tok */ |
| token = NULL; |
| goto out_free; |
| } |
| |
| if (right->type == TEP_PRINT_OP && |
| get_op_prio(arg->op.op) < get_op_prio(right->op.op)) { |
| struct tep_print_arg tmp; |
| |
| /* rotate ops according to the priority */ |
| arg->op.right = right->op.left; |
| |
| tmp = *arg; |
| *arg = *right; |
| *right = tmp; |
| |
| arg->op.left = right; |
| } else { |
| arg->op.right = right; |
| } |
| |
| } else if (strcmp(token, "[") == 0) { |
| |
| left = alloc_arg(); |
| if (!left) |
| goto out_warn_free; |
| |
| *left = *arg; |
| |
| arg->type = TEP_PRINT_OP; |
| arg->op.op = token; |
| arg->op.left = left; |
| |
| arg->op.prio = 0; |
| |
| /* it will set arg->op.right */ |
| type = process_array(event, arg, tok); |
| |
| } else { |
| do_warning_event(event, "unknown op '%s'", token); |
| event->flags |= TEP_EVENT_FL_FAILED; |
| /* the arg is now the left side */ |
| goto out_free; |
| } |
| |
| if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) { |
| int prio; |
| |
| /* higher prios need to be closer to the root */ |
| prio = get_op_prio(*tok); |
| |
| if (prio > arg->op.prio) |
| return process_op(event, arg, tok); |
| |
| return process_op(event, right, tok); |
| } |
| |
| return type; |
| |
| out_warn_free: |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg, |
| char **tok) |
| { |
| enum tep_event_type type; |
| char *field; |
| char *token; |
| |
| if (read_expected(TEP_EVENT_OP, "->") < 0) |
| goto out_err; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto out_free; |
| field = token; |
| |
| arg->type = TEP_PRINT_FIELD; |
| arg->field.name = field; |
| |
| if (is_flag_field) { |
| arg->field.field = tep_find_any_field(event, arg->field.name); |
| arg->field.field->flags |= TEP_FIELD_IS_FLAG; |
| is_flag_field = 0; |
| } else if (is_symbolic_field) { |
| arg->field.field = tep_find_any_field(event, arg->field.name); |
| arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC; |
| is_symbolic_field = 0; |
| } |
| |
| type = read_token(&token); |
| *tok = token; |
| |
| return type; |
| |
| out_free: |
| free_token(token); |
| out_err: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static int alloc_and_process_delim(struct tep_event *event, char *next_token, |
| struct tep_print_arg **print_arg) |
| { |
| struct tep_print_arg *field; |
| enum tep_event_type type; |
| char *token; |
| int ret = 0; |
| |
| field = alloc_arg(); |
| if (!field) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| errno = ENOMEM; |
| return -1; |
| } |
| |
| type = process_arg(event, field, &token); |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) { |
| errno = EINVAL; |
| ret = -1; |
| free_arg(field); |
| goto out_free_token; |
| } |
| |
| *print_arg = field; |
| |
| out_free_token: |
| free_token(token); |
| |
| return ret; |
| } |
| |
| static char *arg_eval (struct tep_print_arg *arg); |
| |
| static unsigned long long |
| eval_type_str(unsigned long long val, const char *type, int pointer) |
| { |
| int sign = 0; |
| char *ref; |
| int len; |
| |
| len = strlen(type); |
| |
| if (pointer) { |
| |
| if (type[len-1] != '*') { |
| do_warning("pointer expected with non pointer type"); |
| return val; |
| } |
| |
| ref = malloc(len); |
| if (!ref) { |
| do_warning("%s: not enough memory!", __func__); |
| return val; |
| } |
| memcpy(ref, type, len); |
| |
| /* chop off the " *" */ |
| ref[len - 2] = 0; |
| |
| val = eval_type_str(val, ref, 0); |
| free(ref); |
| return val; |
| } |
| |
| /* check if this is a pointer */ |
| if (type[len - 1] == '*') |
| return val; |
| |
| /* Try to figure out the arg size*/ |
| if (strncmp(type, "struct", 6) == 0) |
| /* all bets off */ |
| return val; |
| |
| if (strcmp(type, "u8") == 0) |
| return val & 0xff; |
| |
| if (strcmp(type, "u16") == 0) |
| return val & 0xffff; |
| |
| if (strcmp(type, "u32") == 0) |
| return val & 0xffffffff; |
| |
| if (strcmp(type, "u64") == 0 || |
| strcmp(type, "s64") == 0) |
| return val; |
| |
| if (strcmp(type, "s8") == 0) |
| return (unsigned long long)(char)val & 0xff; |
| |
| if (strcmp(type, "s16") == 0) |
| return (unsigned long long)(short)val & 0xffff; |
| |
| if (strcmp(type, "s32") == 0) |
| return (unsigned long long)(int)val & 0xffffffff; |
| |
| if (strncmp(type, "unsigned ", 9) == 0) { |
| sign = 0; |
| type += 9; |
| } |
| |
| if (strcmp(type, "char") == 0) { |
| if (sign) |
| return (unsigned long long)(char)val & 0xff; |
| else |
| return val & 0xff; |
| } |
| |
| if (strcmp(type, "short") == 0) { |
| if (sign) |
| return (unsigned long long)(short)val & 0xffff; |
| else |
| return val & 0xffff; |
| } |
| |
| if (strcmp(type, "int") == 0) { |
| if (sign) |
| return (unsigned long long)(int)val & 0xffffffff; |
| else |
| return val & 0xffffffff; |
| } |
| |
| return val; |
| } |
| |
| /* |
| * Try to figure out the type. |
| */ |
| static unsigned long long |
| eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer) |
| { |
| if (arg->type != TEP_PRINT_TYPE) { |
| do_warning("expected type argument"); |
| return 0; |
| } |
| |
| return eval_type_str(val, arg->typecast.type, pointer); |
| } |
| |
| static int arg_num_eval(struct tep_print_arg *arg, long long *val) |
| { |
| long long left, right; |
| int ret = 1; |
| |
| switch (arg->type) { |
| case TEP_PRINT_ATOM: |
| *val = strtoll(arg->atom.atom, NULL, 0); |
| break; |
| case TEP_PRINT_TYPE: |
| ret = arg_num_eval(arg->typecast.item, val); |
| if (!ret) |
| break; |
| *val = eval_type(*val, arg, 0); |
| break; |
| case TEP_PRINT_OP: |
| switch (arg->op.op[0]) { |
| case '|': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| if (arg->op.op[1]) |
| *val = left || right; |
| else |
| *val = left | right; |
| break; |
| case '&': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| if (arg->op.op[1]) |
| *val = left && right; |
| else |
| *val = left & right; |
| break; |
| case '<': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| switch (arg->op.op[1]) { |
| case 0: |
| *val = left < right; |
| break; |
| case '<': |
| *val = left << right; |
| break; |
| case '=': |
| *val = left <= right; |
| break; |
| default: |
| do_warning("unknown op '%s'", arg->op.op); |
| ret = 0; |
| } |
| break; |
| case '>': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| switch (arg->op.op[1]) { |
| case 0: |
| *val = left > right; |
| break; |
| case '>': |
| *val = left >> right; |
| break; |
| case '=': |
| *val = left >= right; |
| break; |
| default: |
| do_warning("unknown op '%s'", arg->op.op); |
| ret = 0; |
| } |
| break; |
| case '=': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| |
| if (arg->op.op[1] != '=') { |
| do_warning("unknown op '%s'", arg->op.op); |
| ret = 0; |
| } else |
| *val = left == right; |
| break; |
| case '!': |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| |
| switch (arg->op.op[1]) { |
| case '=': |
| *val = left != right; |
| break; |
| default: |
| do_warning("unknown op '%s'", arg->op.op); |
| ret = 0; |
| } |
| break; |
| case '-': |
| /* check for negative */ |
| if (arg->op.left->type == TEP_PRINT_NULL) |
| left = 0; |
| else |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| *val = left - right; |
| break; |
| case '+': |
| if (arg->op.left->type == TEP_PRINT_NULL) |
| left = 0; |
| else |
| ret = arg_num_eval(arg->op.left, &left); |
| if (!ret) |
| break; |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| *val = left + right; |
| break; |
| case '~': |
| ret = arg_num_eval(arg->op.right, &right); |
| if (!ret) |
| break; |
| *val = ~right; |
| break; |
| default: |
| do_warning("unknown op '%s'", arg->op.op); |
| ret = 0; |
| } |
| break; |
| |
| case TEP_PRINT_NULL: |
| case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL: |
| case TEP_PRINT_STRING: |
| case TEP_PRINT_BSTRING: |
| case TEP_PRINT_BITMASK: |
| default: |
| do_warning("invalid eval type %d", arg->type); |
| ret = 0; |
| |
| } |
| return ret; |
| } |
| |
| static char *arg_eval (struct tep_print_arg *arg) |
| { |
| long long val; |
| static char buf[24]; |
| |
| switch (arg->type) { |
| case TEP_PRINT_ATOM: |
| return arg->atom.atom; |
| case TEP_PRINT_TYPE: |
| return arg_eval(arg->typecast.item); |
| case TEP_PRINT_OP: |
| if (!arg_num_eval(arg, &val)) |
| break; |
| sprintf(buf, "%lld", val); |
| return buf; |
| |
| case TEP_PRINT_NULL: |
| case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL: |
| case TEP_PRINT_STRING: |
| case TEP_PRINT_BSTRING: |
| case TEP_PRINT_BITMASK: |
| default: |
| do_warning("invalid eval type %d", arg->type); |
| break; |
| } |
| |
| return NULL; |
| } |
| |
| static enum tep_event_type |
| process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok) |
| { |
| enum tep_event_type type; |
| struct tep_print_arg *arg = NULL; |
| struct tep_print_flag_sym *field; |
| char *token = *tok; |
| char *value; |
| |
| do { |
| free_token(token); |
| type = read_token_item(&token); |
| if (test_type_token(type, token, TEP_EVENT_OP, "{")) |
| break; |
| |
| arg = alloc_arg(); |
| if (!arg) |
| goto out_free; |
| |
| free_token(token); |
| type = process_arg(event, arg, &token); |
| |
| if (type == TEP_EVENT_OP) |
| type = process_op(event, arg, &token); |
| |
| if (type == TEP_EVENT_ERROR) |
| goto out_free; |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ",")) |
| goto out_free; |
| |
| field = calloc(1, sizeof(*field)); |
| if (!field) |
| goto out_free; |
| |
| value = arg_eval(arg); |
| if (value == NULL) |
| goto out_free_field; |
| field->value = strdup(value); |
| if (field->value == NULL) |
| goto out_free_field; |
| |
| free_arg(arg); |
| arg = alloc_arg(); |
| if (!arg) |
| goto out_free; |
| |
| free_token(token); |
| type = process_arg(event, arg, &token); |
| if (test_type_token(type, token, TEP_EVENT_OP, "}")) |
| goto out_free_field; |
| |
| value = arg_eval(arg); |
| if (value == NULL) |
| goto out_free_field; |
| field->str = strdup(value); |
| if (field->str == NULL) |
| goto out_free_field; |
| free_arg(arg); |
| arg = NULL; |
| |
| *list = field; |
| list = &field->next; |
| |
| free_token(token); |
| type = read_token_item(&token); |
| } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0); |
| |
| *tok = token; |
| return type; |
| |
| out_free_field: |
| free_flag_sym(field); |
| out_free: |
| free_arg(arg); |
| free_token(token); |
| *tok = NULL; |
| |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_print_arg *field; |
| enum tep_event_type type; |
| char *token = NULL; |
| |
| memset(arg, 0, sizeof(*arg)); |
| arg->type = TEP_PRINT_FLAGS; |
| |
| field = alloc_arg(); |
| if (!field) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| goto out_free; |
| } |
| |
| type = process_field_arg(event, field, &token); |
| |
| /* Handle operations in the first argument */ |
| while (type == TEP_EVENT_OP) |
| type = process_op(event, field, &token); |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ",")) |
| goto out_free_field; |
| free_token(token); |
| |
| arg->flags.field = field; |
| |
| type = read_token_item(&token); |
| if (event_item_type(type)) { |
| arg->flags.delim = token; |
| type = read_token_item(&token); |
| } |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ",")) |
| goto out_free; |
| |
| type = process_fields(event, &arg->flags.flags, &token); |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ")")) |
| goto out_free; |
| |
| free_token(token); |
| type = read_token_item(tok); |
| return type; |
| |
| out_free_field: |
| free_arg(field); |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_print_arg *field; |
| enum tep_event_type type; |
| char *token = NULL; |
| |
| memset(arg, 0, sizeof(*arg)); |
| arg->type = TEP_PRINT_SYMBOL; |
| |
| field = alloc_arg(); |
| if (!field) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| goto out_free; |
| } |
| |
| type = process_field_arg(event, field, &token); |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ",")) |
| goto out_free_field; |
| |
| arg->symbol.field = field; |
| |
| type = process_fields(event, &arg->symbol.symbols, &token); |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ")")) |
| goto out_free; |
| |
| free_token(token); |
| type = read_token_item(tok); |
| return type; |
| |
| out_free_field: |
| free_arg(field); |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_hex_common(struct tep_event *event, struct tep_print_arg *arg, |
| char **tok, enum tep_print_arg_type type) |
| { |
| memset(arg, 0, sizeof(*arg)); |
| arg->type = type; |
| |
| if (alloc_and_process_delim(event, ",", &arg->hex.field)) |
| goto out; |
| |
| if (alloc_and_process_delim(event, ")", &arg->hex.size)) |
| goto free_field; |
| |
| return read_token_item(tok); |
| |
| free_field: |
| free_arg(arg->hex.field); |
| arg->hex.field = NULL; |
| out: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| return process_hex_common(event, arg, tok, TEP_PRINT_HEX); |
| } |
| |
| static enum tep_event_type |
| process_hex_str(struct tep_event *event, struct tep_print_arg *arg, |
| char **tok) |
| { |
| return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR); |
| } |
| |
| static enum tep_event_type |
| process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| memset(arg, 0, sizeof(*arg)); |
| arg->type = TEP_PRINT_INT_ARRAY; |
| |
| if (alloc_and_process_delim(event, ",", &arg->int_array.field)) |
| goto out; |
| |
| if (alloc_and_process_delim(event, ",", &arg->int_array.count)) |
| goto free_field; |
| |
| if (alloc_and_process_delim(event, ")", &arg->int_array.el_size)) |
| goto free_size; |
| |
| return read_token_item(tok); |
| |
| free_size: |
| free_arg(arg->int_array.count); |
| arg->int_array.count = NULL; |
| free_field: |
| free_arg(arg->int_array.field); |
| arg->int_array.field = NULL; |
| out: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_format_field *field; |
| enum tep_event_type type; |
| char *token; |
| |
| memset(arg, 0, sizeof(*arg)); |
| arg->type = TEP_PRINT_DYNAMIC_ARRAY; |
| |
| /* |
| * The item within the parenthesis is another field that holds |
| * the index into where the array starts. |
| */ |
| type = read_token(&token); |
| *tok = token; |
| if (type != TEP_EVENT_ITEM) |
| goto out_free; |
| |
| /* Find the field */ |
| |
| field = tep_find_field(event, token); |
| if (!field) |
| goto out_free; |
| |
| arg->dynarray.field = field; |
| arg->dynarray.index = 0; |
| |
| if (read_expected(TEP_EVENT_DELIM, ")") < 0) |
| goto out_free; |
| |
| free_token(token); |
| type = read_token_item(&token); |
| *tok = token; |
| if (type != TEP_EVENT_OP || strcmp(token, "[") != 0) |
| return type; |
| |
| free_token(token); |
| arg = alloc_arg(); |
| if (!arg) { |
| do_warning_event(event, "%s: not enough memory!", __func__); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| type = process_arg(event, arg, &token); |
| if (type == TEP_EVENT_ERROR) |
| goto out_free_arg; |
| |
| if (!test_type_token(type, token, TEP_EVENT_OP, "]")) |
| goto out_free_arg; |
| |
| free_token(token); |
| type = read_token_item(tok); |
| return type; |
| |
| out_free_arg: |
| free_arg(arg); |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg, |
| char **tok) |
| { |
| struct tep_format_field *field; |
| enum tep_event_type type; |
| char *token; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto out_free; |
| |
| arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN; |
| |
| /* Find the field */ |
| field = tep_find_field(event, token); |
| if (!field) |
| goto out_free; |
| |
| arg->dynarray.field = field; |
| arg->dynarray.index = 0; |
| |
| if (read_expected(TEP_EVENT_DELIM, ")") < 0) |
| goto out_err; |
| |
| free_token(token); |
| type = read_token(&token); |
| *tok = token; |
| |
| return type; |
| |
| out_free: |
| free_token(token); |
| out_err: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_print_arg *item_arg; |
| enum tep_event_type type; |
| char *token; |
| |
| type = process_arg(event, arg, &token); |
| |
| if (type == TEP_EVENT_ERROR) |
| goto out_free; |
| |
| if (type == TEP_EVENT_OP) |
| type = process_op(event, arg, &token); |
| |
| if (type == TEP_EVENT_ERROR) |
| goto out_free; |
| |
| if (test_type_token(type, token, TEP_EVENT_DELIM, ")")) |
| goto out_free; |
| |
| free_token(token); |
| type = read_token_item(&token); |
| |
| /* |
| * If the next token is an item or another open paren, then |
| * this was a typecast. |
| */ |
| if (event_item_type(type) || |
| (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) { |
| |
| /* make this a typecast and contine */ |
| |
| /* prevous must be an atom */ |
| if (arg->type != TEP_PRINT_ATOM) { |
| do_warning_event(event, "previous needed to be TEP_PRINT_ATOM"); |
| goto out_free; |
| } |
| |
| item_arg = alloc_arg(); |
| if (!item_arg) { |
| do_warning_event(event, "%s: not enough memory!", |
| __func__); |
| goto out_free; |
| } |
| |
| arg->type = TEP_PRINT_TYPE; |
| arg->typecast.type = arg->atom.atom; |
| arg->typecast.item = item_arg; |
| type = process_arg_token(event, item_arg, &token, type); |
| |
| } |
| |
| *tok = token; |
| return type; |
| |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| |
| static enum tep_event_type |
| process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg, |
| char **tok) |
| { |
| enum tep_event_type type; |
| char *token; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto out_free; |
| |
| arg->type = TEP_PRINT_STRING; |
| arg->string.string = token; |
| arg->string.offset = -1; |
| |
| if (read_expected(TEP_EVENT_DELIM, ")") < 0) |
| goto out_err; |
| |
| type = read_token(&token); |
| *tok = token; |
| |
| return type; |
| |
| out_free: |
| free_token(token); |
| out_err: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg, |
| char **tok) |
| { |
| enum tep_event_type type; |
| char *token; |
| |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto out_free; |
| |
| arg->type = TEP_PRINT_BITMASK; |
| arg->bitmask.bitmask = token; |
| arg->bitmask.offset = -1; |
| |
| if (read_expected(TEP_EVENT_DELIM, ")") < 0) |
| goto out_err; |
| |
| type = read_token(&token); |
| *tok = token; |
| |
| return type; |
| |
| out_free: |
| free_token(token); |
| out_err: |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static struct tep_function_handler * |
| find_func_handler(struct tep_handle *tep, char *func_name) |
| { |
| struct tep_function_handler *func; |
| |
| if (!tep) |
| return NULL; |
| |
| for (func = tep->func_handlers; func; func = func->next) { |
| if (strcmp(func->name, func_name) == 0) |
| break; |
| } |
| |
| return func; |
| } |
| |
| static void remove_func_handler(struct tep_handle *tep, char *func_name) |
| { |
| struct tep_function_handler *func; |
| struct tep_function_handler **next; |
| |
| next = &tep->func_handlers; |
| while ((func = *next)) { |
| if (strcmp(func->name, func_name) == 0) { |
| *next = func->next; |
| free_func_handle(func); |
| break; |
| } |
| next = &func->next; |
| } |
| } |
| |
| static enum tep_event_type |
| process_func_handler(struct tep_event *event, struct tep_function_handler *func, |
| struct tep_print_arg *arg, char **tok) |
| { |
| struct tep_print_arg **next_arg; |
| struct tep_print_arg *farg; |
| enum tep_event_type type; |
| char *token; |
| int i; |
| |
| arg->type = TEP_PRINT_FUNC; |
| arg->func.func = func; |
| |
| *tok = NULL; |
| |
| next_arg = &(arg->func.args); |
| for (i = 0; i < func->nr_args; i++) { |
| farg = alloc_arg(); |
| if (!farg) { |
| do_warning_event(event, "%s: not enough memory!", |
| __func__); |
| return TEP_EVENT_ERROR; |
| } |
| |
| type = process_arg(event, farg, &token); |
| if (i < (func->nr_args - 1)) { |
| if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) { |
| do_warning_event(event, |
| "Error: function '%s()' expects %d arguments but event %s only uses %d", |
| func->name, func->nr_args, |
| event->name, i + 1); |
| goto err; |
| } |
| } else { |
| if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) { |
| do_warning_event(event, |
| "Error: function '%s()' only expects %d arguments but event %s has more", |
| func->name, func->nr_args, event->name); |
| goto err; |
| } |
| } |
| |
| *next_arg = farg; |
| next_arg = &(farg->next); |
| free_token(token); |
| } |
| |
| type = read_token(&token); |
| *tok = token; |
| |
| return type; |
| |
| err: |
| free_arg(farg); |
| free_token(token); |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_builtin_expect(struct tep_event *event, struct tep_print_arg *arg, char **tok) |
| { |
| enum tep_event_type type; |
| char *token = NULL; |
| |
| /* Handle __builtin_expect( cond, #) */ |
| type = process_arg(event, arg, &token); |
| |
| if (type != TEP_EVENT_DELIM || token[0] != ',') |
| goto out_free; |
| |
| free_token(token); |
| |
| /* We don't care what the second parameter is of the __builtin_expect() */ |
| if (read_expect_type(TEP_EVENT_ITEM, &token) < 0) |
| goto out_free; |
| |
| if (read_expected(TEP_EVENT_DELIM, ")") < 0) |
| goto out_free; |
| |
| free_token(token); |
| type = read_token_item(tok); |
| return type; |
| |
| out_free: |
| free_token(token); |
| *tok = NULL; |
| return TEP_EVENT_ERROR; |
| } |
| |
| static enum tep_event_type |
| process_function(struct tep_event *event, struct tep_print_arg *arg, |
| char *token, char **tok) |
| { |
| struct tep_function_handler *func; |
|