1 // SPDX-License-Identifier: LGPL-2.1
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.
21 #include <linux/time64.h>
23 #include <netinet/in.h>
24 #include "event-parse.h"
26 #include "event-parse-local.h"
27 #include "event-utils.h"
28 #include "trace-seq.h"
30 static const char *input_buf;
31 static unsigned long long input_buf_ptr;
32 static unsigned long long input_buf_siz;
34 static int is_flag_field;
35 static int is_symbolic_field;
37 static int show_warning = 1;
39 #define do_warning(fmt, ...) \
42 warning(fmt, ##__VA_ARGS__); \
45 #define do_warning_event(event, fmt, ...) \
51 warning("[%s:%s] " fmt, event->system, \
52 event->name, ##__VA_ARGS__); \
54 warning(fmt, ##__VA_ARGS__); \
57 static void init_input_buf(const char *buf, unsigned long long size)
64 const char *tep_get_input_buf(void)
69 unsigned long long tep_get_input_buf_ptr(void)
74 struct event_handler {
75 struct event_handler *next;
78 const char *event_name;
79 tep_event_handler_func func;
84 struct func_params *next;
85 enum tep_func_arg_type type;
88 struct tep_function_handler {
89 struct tep_function_handler *next;
90 enum tep_func_arg_type ret_type;
92 tep_func_handler func;
93 struct func_params *params;
97 static unsigned long long
98 process_defined_func(struct trace_seq *s, void *data, int size,
99 struct tep_event *event, struct tep_print_arg *arg);
101 static void free_func_handle(struct tep_function_handler *func);
104 * tep_buffer_init - init buffer for parsing
105 * @buf: buffer to parse
106 * @size: the size of the buffer
108 * For use with tep_read_token(), this initializes the internal
109 * buffer that tep_read_token() will parse.
111 void tep_buffer_init(const char *buf, unsigned long long size)
113 init_input_buf(buf, size);
116 void breakpoint(void)
122 struct tep_print_arg *alloc_arg(void)
124 return calloc(1, sizeof(struct tep_print_arg));
132 static int cmdline_cmp(const void *a, const void *b)
134 const struct tep_cmdline *ca = a;
135 const struct tep_cmdline *cb = b;
137 if (ca->pid < cb->pid)
139 if (ca->pid > cb->pid)
145 struct cmdline_list {
146 struct cmdline_list *next;
151 static int cmdline_init(struct tep_handle *pevent)
153 struct cmdline_list *cmdlist = pevent->cmdlist;
154 struct cmdline_list *item;
155 struct tep_cmdline *cmdlines;
158 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
164 cmdlines[i].pid = cmdlist->pid;
165 cmdlines[i].comm = cmdlist->comm;
168 cmdlist = cmdlist->next;
172 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
174 pevent->cmdlines = cmdlines;
175 pevent->cmdlist = NULL;
180 static const char *find_cmdline(struct tep_handle *pevent, int pid)
182 const struct tep_cmdline *comm;
183 struct tep_cmdline key;
188 if (!pevent->cmdlines && cmdline_init(pevent))
189 return "<not enough memory for cmdlines!>";
193 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
194 sizeof(*pevent->cmdlines), cmdline_cmp);
202 * tep_pid_is_registered - return if a pid has a cmdline registered
203 * @pevent: handle for the pevent
204 * @pid: The pid to check if it has a cmdline registered with.
206 * Returns 1 if the pid has a cmdline mapped to it
209 int tep_pid_is_registered(struct tep_handle *pevent, int pid)
211 const struct tep_cmdline *comm;
212 struct tep_cmdline key;
217 if (!pevent->cmdlines && cmdline_init(pevent))
222 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
223 sizeof(*pevent->cmdlines), cmdline_cmp);
231 * If the command lines have been converted to an array, then
232 * we must add this pid. This is much slower than when cmdlines
233 * are added before the array is initialized.
235 static int add_new_comm(struct tep_handle *pevent,
236 const char *comm, int pid, bool override)
238 struct tep_cmdline *cmdlines = pevent->cmdlines;
239 struct tep_cmdline *cmdline;
240 struct tep_cmdline key;
246 /* avoid duplicates */
249 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
250 sizeof(*pevent->cmdlines), cmdline_cmp);
256 new_comm = strdup(comm);
262 cmdline->comm = new_comm;
267 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
273 cmdlines[pevent->cmdline_count].comm = strdup(comm);
274 if (!cmdlines[pevent->cmdline_count].comm) {
280 cmdlines[pevent->cmdline_count].pid = pid;
282 if (cmdlines[pevent->cmdline_count].comm)
283 pevent->cmdline_count++;
285 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
286 pevent->cmdlines = cmdlines;
291 static int _tep_register_comm(struct tep_handle *pevent,
292 const char *comm, int pid, bool override)
294 struct cmdline_list *item;
296 if (pevent->cmdlines)
297 return add_new_comm(pevent, comm, pid, override);
299 item = malloc(sizeof(*item));
304 item->comm = strdup(comm);
306 item->comm = strdup("<...>");
312 item->next = pevent->cmdlist;
314 pevent->cmdlist = item;
315 pevent->cmdline_count++;
321 * tep_register_comm - register a pid / comm mapping
322 * @pevent: handle for the pevent
323 * @comm: the command line to register
324 * @pid: the pid to map the command line to
326 * This adds a mapping to search for command line names with
327 * a given pid. The comm is duplicated. If a command with the same pid
328 * already exist, -1 is returned and errno is set to EEXIST
330 int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
332 return _tep_register_comm(pevent, comm, pid, false);
336 * tep_override_comm - register a pid / comm mapping
337 * @pevent: handle for the pevent
338 * @comm: the command line to register
339 * @pid: the pid to map the command line to
341 * This adds a mapping to search for command line names with
342 * a given pid. The comm is duplicated. If a command with the same pid
343 * already exist, the command string is udapted with the new one
345 int tep_override_comm(struct tep_handle *pevent, const char *comm, int pid)
347 if (!pevent->cmdlines && cmdline_init(pevent)) {
351 return _tep_register_comm(pevent, comm, pid, true);
354 int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
356 pevent->trace_clock = strdup(trace_clock);
357 if (!pevent->trace_clock) {
365 unsigned long long addr;
371 struct func_list *next;
372 unsigned long long addr;
377 static int func_cmp(const void *a, const void *b)
379 const struct func_map *fa = a;
380 const struct func_map *fb = b;
382 if (fa->addr < fb->addr)
384 if (fa->addr > fb->addr)
391 * We are searching for a record in between, not an exact
394 static int func_bcmp(const void *a, const void *b)
396 const struct func_map *fa = a;
397 const struct func_map *fb = b;
399 if ((fa->addr == fb->addr) ||
401 (fa->addr > fb->addr &&
402 fa->addr < (fb+1)->addr))
405 if (fa->addr < fb->addr)
411 static int func_map_init(struct tep_handle *pevent)
413 struct func_list *funclist;
414 struct func_list *item;
415 struct func_map *func_map;
418 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
422 funclist = pevent->funclist;
426 func_map[i].func = funclist->func;
427 func_map[i].addr = funclist->addr;
428 func_map[i].mod = funclist->mod;
431 funclist = funclist->next;
435 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
438 * Add a special record at the end.
440 func_map[pevent->func_count].func = NULL;
441 func_map[pevent->func_count].addr = 0;
442 func_map[pevent->func_count].mod = NULL;
444 pevent->func_map = func_map;
445 pevent->funclist = NULL;
450 static struct func_map *
451 __find_func(struct tep_handle *pevent, unsigned long long addr)
453 struct func_map *func;
456 if (!pevent->func_map)
457 func_map_init(pevent);
461 func = bsearch(&key, pevent->func_map, pevent->func_count,
462 sizeof(*pevent->func_map), func_bcmp);
467 struct func_resolver {
468 tep_func_resolver_t *func;
474 * tep_set_function_resolver - set an alternative function resolver
475 * @pevent: handle for the pevent
476 * @resolver: function to be used
477 * @priv: resolver function private state.
479 * Some tools may have already a way to resolve kernel functions, allow them to
480 * keep using it instead of duplicating all the entries inside
483 int tep_set_function_resolver(struct tep_handle *pevent,
484 tep_func_resolver_t *func, void *priv)
486 struct func_resolver *resolver = malloc(sizeof(*resolver));
488 if (resolver == NULL)
491 resolver->func = func;
492 resolver->priv = priv;
494 free(pevent->func_resolver);
495 pevent->func_resolver = resolver;
501 * tep_reset_function_resolver - reset alternative function resolver
502 * @pevent: handle for the pevent
504 * Stop using whatever alternative resolver was set, use the default
507 void tep_reset_function_resolver(struct tep_handle *pevent)
509 free(pevent->func_resolver);
510 pevent->func_resolver = NULL;
513 static struct func_map *
514 find_func(struct tep_handle *pevent, unsigned long long addr)
516 struct func_map *map;
518 if (!pevent->func_resolver)
519 return __find_func(pevent, addr);
521 map = &pevent->func_resolver->map;
524 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
525 &map->addr, &map->mod);
526 if (map->func == NULL)
533 * tep_find_function - find a function by a given address
534 * @pevent: handle for the pevent
535 * @addr: the address to find the function with
537 * Returns a pointer to the function stored that has the given
538 * address. Note, the address does not have to be exact, it
539 * will select the function that would contain the address.
541 const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
543 struct func_map *map;
545 map = find_func(pevent, addr);
553 * tep_find_function_address - find a function address by a given address
554 * @pevent: handle for the pevent
555 * @addr: the address to find the function with
557 * Returns the address the function starts at. This can be used in
558 * conjunction with tep_find_function to print both the function
559 * name and the function offset.
562 tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
564 struct func_map *map;
566 map = find_func(pevent, addr);
574 * tep_register_function - register a function with a given address
575 * @pevent: handle for the pevent
576 * @function: the function name to register
577 * @addr: the address the function starts at
578 * @mod: the kernel module the function may be in (NULL for none)
580 * This registers a function name with an address and module.
581 * The @func passed in is duplicated.
583 int tep_register_function(struct tep_handle *pevent, char *func,
584 unsigned long long addr, char *mod)
586 struct func_list *item = malloc(sizeof(*item));
591 item->next = pevent->funclist;
592 item->func = strdup(func);
597 item->mod = strdup(mod);
604 pevent->funclist = item;
605 pevent->func_count++;
619 * tep_print_funcs - print out the stored functions
620 * @pevent: handle for the pevent
622 * This prints out the stored functions.
624 void tep_print_funcs(struct tep_handle *pevent)
628 if (!pevent->func_map)
629 func_map_init(pevent);
631 for (i = 0; i < (int)pevent->func_count; i++) {
633 pevent->func_map[i].addr,
634 pevent->func_map[i].func);
635 if (pevent->func_map[i].mod)
636 printf(" [%s]\n", pevent->func_map[i].mod);
643 unsigned long long addr;
648 struct printk_list *next;
649 unsigned long long addr;
653 static int printk_cmp(const void *a, const void *b)
655 const struct printk_map *pa = a;
656 const struct printk_map *pb = b;
658 if (pa->addr < pb->addr)
660 if (pa->addr > pb->addr)
666 static int printk_map_init(struct tep_handle *pevent)
668 struct printk_list *printklist;
669 struct printk_list *item;
670 struct printk_map *printk_map;
673 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
677 printklist = pevent->printklist;
681 printk_map[i].printk = printklist->printk;
682 printk_map[i].addr = printklist->addr;
685 printklist = printklist->next;
689 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
691 pevent->printk_map = printk_map;
692 pevent->printklist = NULL;
697 static struct printk_map *
698 find_printk(struct tep_handle *pevent, unsigned long long addr)
700 struct printk_map *printk;
701 struct printk_map key;
703 if (!pevent->printk_map && printk_map_init(pevent))
708 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
709 sizeof(*pevent->printk_map), printk_cmp);
715 * tep_register_print_string - register a string by its address
716 * @pevent: handle for the pevent
717 * @fmt: the string format to register
718 * @addr: the address the string was located at
720 * This registers a string by the address it was stored in the kernel.
721 * The @fmt passed in is duplicated.
723 int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
724 unsigned long long addr)
726 struct printk_list *item = malloc(sizeof(*item));
732 item->next = pevent->printklist;
735 /* Strip off quotes and '\n' from the end */
738 item->printk = strdup(fmt);
742 p = item->printk + strlen(item->printk) - 1;
747 if (strcmp(p, "\\n") == 0)
750 pevent->printklist = item;
751 pevent->printk_count++;
762 * tep_print_printk - print out the stored strings
763 * @pevent: handle for the pevent
765 * This prints the string formats that were stored.
767 void tep_print_printk(struct tep_handle *pevent)
771 if (!pevent->printk_map)
772 printk_map_init(pevent);
774 for (i = 0; i < (int)pevent->printk_count; i++) {
775 printf("%016llx %s\n",
776 pevent->printk_map[i].addr,
777 pevent->printk_map[i].printk);
781 static struct tep_event *alloc_event(void)
783 return calloc(1, sizeof(struct tep_event));
786 static int add_event(struct tep_handle *pevent, struct tep_event *event)
789 struct tep_event **events = realloc(pevent->events, sizeof(event) *
790 (pevent->nr_events + 1));
794 pevent->events = events;
796 for (i = 0; i < pevent->nr_events; i++) {
797 if (pevent->events[i]->id > event->id)
800 if (i < pevent->nr_events)
801 memmove(&pevent->events[i + 1],
803 sizeof(event) * (pevent->nr_events - i));
805 pevent->events[i] = event;
808 event->pevent = pevent;
813 static int event_item_type(enum tep_event_type type)
816 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
818 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
824 static void free_flag_sym(struct tep_print_flag_sym *fsym)
826 struct tep_print_flag_sym *next;
837 static void free_arg(struct tep_print_arg *arg)
839 struct tep_print_arg *farg;
846 free(arg->atom.atom);
848 case TEP_PRINT_FIELD:
849 free(arg->field.name);
851 case TEP_PRINT_FLAGS:
852 free_arg(arg->flags.field);
853 free(arg->flags.delim);
854 free_flag_sym(arg->flags.flags);
856 case TEP_PRINT_SYMBOL:
857 free_arg(arg->symbol.field);
858 free_flag_sym(arg->symbol.symbols);
861 case TEP_PRINT_HEX_STR:
862 free_arg(arg->hex.field);
863 free_arg(arg->hex.size);
865 case TEP_PRINT_INT_ARRAY:
866 free_arg(arg->int_array.field);
867 free_arg(arg->int_array.count);
868 free_arg(arg->int_array.el_size);
871 free(arg->typecast.type);
872 free_arg(arg->typecast.item);
874 case TEP_PRINT_STRING:
875 case TEP_PRINT_BSTRING:
876 free(arg->string.string);
878 case TEP_PRINT_BITMASK:
879 free(arg->bitmask.bitmask);
881 case TEP_PRINT_DYNAMIC_ARRAY:
882 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
883 free(arg->dynarray.index);
887 free_arg(arg->op.left);
888 free_arg(arg->op.right);
891 while (arg->func.args) {
892 farg = arg->func.args;
893 arg->func.args = farg->next;
906 static enum tep_event_type get_type(int ch)
909 return TEP_EVENT_NEWLINE;
911 return TEP_EVENT_SPACE;
912 if (isalnum(ch) || ch == '_')
913 return TEP_EVENT_ITEM;
915 return TEP_EVENT_SQUOTE;
917 return TEP_EVENT_DQUOTE;
919 return TEP_EVENT_NONE;
920 if (ch == '(' || ch == ')' || ch == ',')
921 return TEP_EVENT_DELIM;
926 static int __read_char(void)
928 if (input_buf_ptr >= input_buf_siz)
931 return input_buf[input_buf_ptr++];
934 static int __peek_char(void)
936 if (input_buf_ptr >= input_buf_siz)
939 return input_buf[input_buf_ptr];
943 * tep_peek_char - peek at the next character that will be read
945 * Returns the next character read, or -1 if end of buffer.
947 int tep_peek_char(void)
949 return __peek_char();
952 static int extend_token(char **tok, char *buf, int size)
954 char *newtok = realloc(*tok, size);
971 static enum tep_event_type force_token(const char *str, char **tok);
973 static enum tep_event_type __read_token(char **tok)
976 int ch, last_ch, quote_ch, next_ch;
979 enum tep_event_type type;
986 return TEP_EVENT_NONE;
989 if (type == TEP_EVENT_NONE)
995 case TEP_EVENT_NEWLINE:
996 case TEP_EVENT_DELIM:
997 if (asprintf(tok, "%c", ch) < 0)
998 return TEP_EVENT_ERROR;
1005 next_ch = __peek_char();
1006 if (next_ch == '>') {
1007 buf[i++] = __read_char();
1020 buf[i++] = __read_char();
1032 default: /* what should we do instead? */
1042 buf[i++] = __read_char();
1045 case TEP_EVENT_DQUOTE:
1046 case TEP_EVENT_SQUOTE:
1047 /* don't keep quotes */
1053 if (i == (BUFSIZ - 1)) {
1057 if (extend_token(tok, buf, tok_size) < 0)
1058 return TEP_EVENT_NONE;
1064 /* the '\' '\' will cancel itself */
1065 if (ch == '\\' && last_ch == '\\')
1067 } while (ch != quote_ch || last_ch == '\\');
1068 /* remove the last quote */
1072 * For strings (double quotes) check the next token.
1073 * If it is another string, concatinate the two.
1075 if (type == TEP_EVENT_DQUOTE) {
1076 unsigned long long save_input_buf_ptr = input_buf_ptr;
1080 } while (isspace(ch));
1083 input_buf_ptr = save_input_buf_ptr;
1088 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1089 case TEP_EVENT_ITEM:
1094 while (get_type(__peek_char()) == type) {
1095 if (i == (BUFSIZ - 1)) {
1099 if (extend_token(tok, buf, tok_size) < 0)
1100 return TEP_EVENT_NONE;
1109 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1110 return TEP_EVENT_NONE;
1112 if (type == TEP_EVENT_ITEM) {
1114 * Older versions of the kernel has a bug that
1115 * creates invalid symbols and will break the mac80211
1116 * parsing. This is a work around to that bug.
1118 * See Linux kernel commit:
1119 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1121 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1124 return force_token("\"%s\" ", tok);
1125 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1128 return force_token("\" sta:%pM\" ", tok);
1129 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1132 return force_token("\" vif:%p(%d)\" ", tok);
1139 static enum tep_event_type force_token(const char *str, char **tok)
1141 const char *save_input_buf;
1142 unsigned long long save_input_buf_ptr;
1143 unsigned long long save_input_buf_siz;
1144 enum tep_event_type type;
1146 /* save off the current input pointers */
1147 save_input_buf = input_buf;
1148 save_input_buf_ptr = input_buf_ptr;
1149 save_input_buf_siz = input_buf_siz;
1151 init_input_buf(str, strlen(str));
1153 type = __read_token(tok);
1155 /* reset back to original token */
1156 input_buf = save_input_buf;
1157 input_buf_ptr = save_input_buf_ptr;
1158 input_buf_siz = save_input_buf_siz;
1163 static void free_token(char *tok)
1169 static enum tep_event_type read_token(char **tok)
1171 enum tep_event_type type;
1174 type = __read_token(tok);
1175 if (type != TEP_EVENT_SPACE)
1183 return TEP_EVENT_NONE;
1187 * tep_read_token - access to utilities to use the pevent parser
1188 * @tok: The token to return
1190 * This will parse tokens from the string given by
1193 * Returns the token type.
1195 enum tep_event_type tep_read_token(char **tok)
1197 return read_token(tok);
1201 * tep_free_token - free a token returned by tep_read_token
1202 * @token: the token to free
1204 void tep_free_token(char *token)
1210 static enum tep_event_type read_token_item(char **tok)
1212 enum tep_event_type type;
1215 type = __read_token(tok);
1216 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1224 return TEP_EVENT_NONE;
1227 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1229 if (type != expect) {
1230 do_warning("Error: expected type %d but read %d",
1237 static int test_type_token(enum tep_event_type type, const char *token,
1238 enum tep_event_type expect, const char *expect_tok)
1240 if (type != expect) {
1241 do_warning("Error: expected type %d but read %d",
1246 if (strcmp(token, expect_tok) != 0) {
1247 do_warning("Error: expected '%s' but read '%s'",
1254 static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
1256 enum tep_event_type type;
1259 type = read_token(tok);
1261 type = read_token_item(tok);
1262 return test_type(type, expect);
1265 static int read_expect_type(enum tep_event_type expect, char **tok)
1267 return __read_expect_type(expect, tok, 1);
1270 static int __read_expected(enum tep_event_type expect, const char *str,
1273 enum tep_event_type type;
1278 type = read_token(&token);
1280 type = read_token_item(&token);
1282 ret = test_type_token(type, token, expect, str);
1289 static int read_expected(enum tep_event_type expect, const char *str)
1291 return __read_expected(expect, str, 1);
1294 static int read_expected_item(enum tep_event_type expect, const char *str)
1296 return __read_expected(expect, str, 0);
1299 static char *event_read_name(void)
1303 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
1306 if (read_expected(TEP_EVENT_OP, ":") < 0)
1309 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1319 static int event_read_id(void)
1324 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
1327 if (read_expected(TEP_EVENT_OP, ":") < 0)
1330 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1333 id = strtoul(token, NULL, 0);
1342 static int field_is_string(struct tep_format_field *field)
1344 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1345 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1346 strstr(field->type, "s8")))
1352 static int field_is_dynamic(struct tep_format_field *field)
1354 if (strncmp(field->type, "__data_loc", 10) == 0)
1360 static int field_is_long(struct tep_format_field *field)
1362 /* includes long long */
1363 if (strstr(field->type, "long"))
1369 static unsigned int type_size(const char *name)
1371 /* This covers all TEP_FIELD_IS_STRING types. */
1389 for (i = 0; table[i].type; i++) {
1390 if (!strcmp(table[i].type, name))
1391 return table[i].size;
1397 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1399 struct tep_format_field *field = NULL;
1400 enum tep_event_type type;
1406 unsigned int size_dynamic = 0;
1408 type = read_token(&token);
1409 if (type == TEP_EVENT_NEWLINE) {
1416 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1420 type = read_token(&token);
1422 * The ftrace fields may still use the "special" name.
1425 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1426 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1428 type = read_token(&token);
1431 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1435 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1440 field = calloc(1, sizeof(*field));
1444 field->event = event;
1446 /* read the rest of the type */
1448 type = read_token(&token);
1449 if (type == TEP_EVENT_ITEM ||
1450 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1452 * Some of the ftrace fields are broken and have
1453 * an illegal "." in them.
1455 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1456 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1458 if (strcmp(token, "*") == 0)
1459 field->flags |= TEP_FIELD_IS_POINTER;
1463 new_type = realloc(field->type,
1464 strlen(field->type) +
1465 strlen(last_token) + 2);
1470 field->type = new_type;
1471 strcat(field->type, " ");
1472 strcat(field->type, last_token);
1475 field->type = last_token;
1484 do_warning_event(event, "%s: no type found", __func__);
1487 field->name = field->alias = last_token;
1489 if (test_type(type, TEP_EVENT_OP))
1492 if (strcmp(token, "[") == 0) {
1493 enum tep_event_type last_type = type;
1494 char *brackets = token;
1498 field->flags |= TEP_FIELD_IS_ARRAY;
1500 type = read_token(&token);
1502 if (type == TEP_EVENT_ITEM)
1503 field->arraylen = strtoul(token, NULL, 0);
1505 field->arraylen = 0;
1507 while (strcmp(token, "]") != 0) {
1508 if (last_type == TEP_EVENT_ITEM &&
1509 type == TEP_EVENT_ITEM)
1515 new_brackets = realloc(brackets,
1517 strlen(token) + len);
1518 if (!new_brackets) {
1522 brackets = new_brackets;
1524 strcat(brackets, " ");
1525 strcat(brackets, token);
1526 /* We only care about the last token */
1527 field->arraylen = strtoul(token, NULL, 0);
1529 type = read_token(&token);
1530 if (type == TEP_EVENT_NONE) {
1531 do_warning_event(event, "failed to find token");
1538 new_brackets = realloc(brackets, strlen(brackets) + 2);
1539 if (!new_brackets) {
1543 brackets = new_brackets;
1544 strcat(brackets, "]");
1546 /* add brackets to type */
1548 type = read_token(&token);
1550 * If the next token is not an OP, then it is of
1551 * the format: type [] item;
1553 if (type == TEP_EVENT_ITEM) {
1555 new_type = realloc(field->type,
1556 strlen(field->type) +
1557 strlen(field->name) +
1558 strlen(brackets) + 2);
1563 field->type = new_type;
1564 strcat(field->type, " ");
1565 strcat(field->type, field->name);
1566 size_dynamic = type_size(field->name);
1567 free_token(field->name);
1568 strcat(field->type, brackets);
1569 field->name = field->alias = token;
1570 type = read_token(&token);
1573 new_type = realloc(field->type,
1574 strlen(field->type) +
1575 strlen(brackets) + 1);
1580 field->type = new_type;
1581 strcat(field->type, brackets);
1586 if (field_is_string(field))
1587 field->flags |= TEP_FIELD_IS_STRING;
1588 if (field_is_dynamic(field))
1589 field->flags |= TEP_FIELD_IS_DYNAMIC;
1590 if (field_is_long(field))
1591 field->flags |= TEP_FIELD_IS_LONG;
1593 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1597 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1600 if (read_expected(TEP_EVENT_OP, ":") < 0)
1603 if (read_expect_type(TEP_EVENT_ITEM, &token))
1605 field->offset = strtoul(token, NULL, 0);
1608 if (read_expected(TEP_EVENT_OP, ";") < 0)
1611 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1614 if (read_expected(TEP_EVENT_OP, ":") < 0)
1617 if (read_expect_type(TEP_EVENT_ITEM, &token))
1619 field->size = strtoul(token, NULL, 0);
1622 if (read_expected(TEP_EVENT_OP, ";") < 0)
1625 type = read_token(&token);
1626 if (type != TEP_EVENT_NEWLINE) {
1627 /* newer versions of the kernel have a "signed" type */
1628 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1633 if (read_expected(TEP_EVENT_OP, ":") < 0)
1636 if (read_expect_type(TEP_EVENT_ITEM, &token))
1639 if (strtoul(token, NULL, 0))
1640 field->flags |= TEP_FIELD_IS_SIGNED;
1643 if (read_expected(TEP_EVENT_OP, ";") < 0)
1646 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1652 if (field->flags & TEP_FIELD_IS_ARRAY) {
1653 if (field->arraylen)
1654 field->elementsize = field->size / field->arraylen;
1655 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1656 field->elementsize = size_dynamic;
1657 else if (field->flags & TEP_FIELD_IS_STRING)
1658 field->elementsize = 1;
1659 else if (field->flags & TEP_FIELD_IS_LONG)
1660 field->elementsize = event->pevent ?
1661 event->pevent->long_size :
1664 field->elementsize = field->size;
1667 fields = &field->next;
1684 static int event_read_format(struct tep_event *event)
1689 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1692 if (read_expected(TEP_EVENT_OP, ":") < 0)
1695 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1699 ret = event_read_fields(event, &event->format.common_fields);
1702 event->format.nr_common = ret;
1704 ret = event_read_fields(event, &event->format.fields);
1707 event->format.nr_fields = ret;
1716 static enum tep_event_type
1717 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1718 char **tok, enum tep_event_type type);
1720 static enum tep_event_type
1721 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1723 enum tep_event_type type;
1726 type = read_token(&token);
1729 return process_arg_token(event, arg, tok, type);
1732 static enum tep_event_type
1733 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1736 * For __print_symbolic() and __print_flags, we need to completely
1737 * evaluate the first argument, which defines what to print next.
1739 static enum tep_event_type
1740 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1742 enum tep_event_type type;
1744 type = process_arg(event, arg, tok);
1746 while (type == TEP_EVENT_OP) {
1747 type = process_op(event, arg, tok);
1753 static enum tep_event_type
1754 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1756 struct tep_print_arg *arg, *left, *right;
1757 enum tep_event_type type;
1762 right = alloc_arg();
1764 if (!arg || !left || !right) {
1765 do_warning_event(event, "%s: not enough memory!", __func__);
1766 /* arg will be freed at out_free */
1772 arg->type = TEP_PRINT_OP;
1773 arg->op.left = left;
1774 arg->op.right = right;
1777 type = process_arg(event, left, &token);
1780 if (type == TEP_EVENT_ERROR)
1783 /* Handle other operations in the arguments */
1784 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1785 type = process_op(event, left, &token);
1789 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1794 type = process_arg(event, right, &token);
1796 top->op.right = arg;
1802 /* Top may point to itself */
1803 top->op.right = NULL;
1806 return TEP_EVENT_ERROR;
1809 static enum tep_event_type
1810 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1812 struct tep_print_arg *arg;
1813 enum tep_event_type type;
1818 do_warning_event(event, "%s: not enough memory!", __func__);
1819 /* '*tok' is set to top->op.op. No need to free. */
1821 return TEP_EVENT_ERROR;
1825 type = process_arg(event, arg, &token);
1826 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1829 top->op.right = arg;
1832 type = read_token_item(&token);
1840 return TEP_EVENT_ERROR;
1843 static int get_op_prio(char *op)
1857 /* '>>' and '<<' are 8 */
1861 /* '==' and '!=' are 10 */
1871 do_warning("unknown op '%c'", op[0]);
1875 if (strcmp(op, "++") == 0 ||
1876 strcmp(op, "--") == 0) {
1878 } else if (strcmp(op, ">>") == 0 ||
1879 strcmp(op, "<<") == 0) {
1881 } else if (strcmp(op, ">=") == 0 ||
1882 strcmp(op, "<=") == 0) {
1884 } else if (strcmp(op, "==") == 0 ||
1885 strcmp(op, "!=") == 0) {
1887 } else if (strcmp(op, "&&") == 0) {
1889 } else if (strcmp(op, "||") == 0) {
1892 do_warning("unknown op '%s'", op);
1898 static int set_op_prio(struct tep_print_arg *arg)
1901 /* single ops are the greatest */
1902 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1905 arg->op.prio = get_op_prio(arg->op.op);
1907 return arg->op.prio;
1910 /* Note, *tok does not get freed, but will most likely be saved */
1911 static enum tep_event_type
1912 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1914 struct tep_print_arg *left, *right = NULL;
1915 enum tep_event_type type;
1918 /* the op is passed in via tok */
1921 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1922 /* handle single op */
1924 do_warning_event(event, "bad op token %s", token);
1934 do_warning_event(event, "bad op token %s", token);
1939 /* make an empty left */
1944 left->type = TEP_PRINT_NULL;
1945 arg->op.left = left;
1947 right = alloc_arg();
1951 arg->op.right = right;
1953 /* do not free the token, it belongs to an op */
1955 type = process_arg(event, right, tok);
1957 } else if (strcmp(token, "?") == 0) {
1963 /* copy the top arg to the left */
1966 arg->type = TEP_PRINT_OP;
1968 arg->op.left = left;
1971 /* it will set arg->op.right */
1972 type = process_cond(event, arg, tok);
1974 } else if (strcmp(token, ">>") == 0 ||
1975 strcmp(token, "<<") == 0 ||
1976 strcmp(token, "&") == 0 ||
1977 strcmp(token, "|") == 0 ||
1978 strcmp(token, "&&") == 0 ||
1979 strcmp(token, "||") == 0 ||
1980 strcmp(token, "-") == 0 ||
1981 strcmp(token, "+") == 0 ||
1982 strcmp(token, "*") == 0 ||
1983 strcmp(token, "^") == 0 ||
1984 strcmp(token, "/") == 0 ||
1985 strcmp(token, "%") == 0 ||
1986 strcmp(token, "<") == 0 ||
1987 strcmp(token, ">") == 0 ||
1988 strcmp(token, "<=") == 0 ||
1989 strcmp(token, ">=") == 0 ||
1990 strcmp(token, "==") == 0 ||
1991 strcmp(token, "!=") == 0) {
1997 /* copy the top arg to the left */
2000 arg->type = TEP_PRINT_OP;
2002 arg->op.left = left;
2003 arg->op.right = NULL;
2005 if (set_op_prio(arg) == -1) {
2006 event->flags |= TEP_EVENT_FL_FAILED;
2007 /* arg->op.op (= token) will be freed at out_free */
2012 type = read_token_item(&token);
2015 /* could just be a type pointer */
2016 if ((strcmp(arg->op.op, "*") == 0) &&
2017 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2020 if (left->type != TEP_PRINT_ATOM) {
2021 do_warning_event(event, "bad pointer type");
2024 new_atom = realloc(left->atom.atom,
2025 strlen(left->atom.atom) + 3);
2029 left->atom.atom = new_atom;
2030 strcat(left->atom.atom, " *");
2038 right = alloc_arg();
2042 type = process_arg_token(event, right, tok, type);
2043 if (type == TEP_EVENT_ERROR) {
2045 /* token was freed in process_arg_token() via *tok */
2050 if (right->type == TEP_PRINT_OP &&
2051 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2052 struct tep_print_arg tmp;
2054 /* rotate ops according to the priority */
2055 arg->op.right = right->op.left;
2061 arg->op.left = right;
2063 arg->op.right = right;
2066 } else if (strcmp(token, "[") == 0) {
2074 arg->type = TEP_PRINT_OP;
2076 arg->op.left = left;
2080 /* it will set arg->op.right */
2081 type = process_array(event, arg, tok);
2084 do_warning_event(event, "unknown op '%s'", token);
2085 event->flags |= TEP_EVENT_FL_FAILED;
2086 /* the arg is now the left side */
2090 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2093 /* higher prios need to be closer to the root */
2094 prio = get_op_prio(*tok);
2096 if (prio > arg->op.prio)
2097 return process_op(event, arg, tok);
2099 return process_op(event, right, tok);
2105 do_warning_event(event, "%s: not enough memory!", __func__);
2109 return TEP_EVENT_ERROR;
2112 static enum tep_event_type
2113 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2116 enum tep_event_type type;
2120 if (read_expected(TEP_EVENT_OP, "->") < 0)
2123 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2127 arg->type = TEP_PRINT_FIELD;
2128 arg->field.name = field;
2130 if (is_flag_field) {
2131 arg->field.field = tep_find_any_field(event, arg->field.name);
2132 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2134 } else if (is_symbolic_field) {
2135 arg->field.field = tep_find_any_field(event, arg->field.name);
2136 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2137 is_symbolic_field = 0;
2140 type = read_token(&token);
2149 return TEP_EVENT_ERROR;
2152 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2153 struct tep_print_arg **print_arg)
2155 struct tep_print_arg *field;
2156 enum tep_event_type type;
2160 field = alloc_arg();
2162 do_warning_event(event, "%s: not enough memory!", __func__);
2167 type = process_arg(event, field, &token);
2169 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2173 goto out_free_token;
2184 static char *arg_eval (struct tep_print_arg *arg);
2186 static unsigned long long
2187 eval_type_str(unsigned long long val, const char *type, int pointer)
2197 if (type[len-1] != '*') {
2198 do_warning("pointer expected with non pointer type");
2204 do_warning("%s: not enough memory!", __func__);
2207 memcpy(ref, type, len);
2209 /* chop off the " *" */
2212 val = eval_type_str(val, ref, 0);
2217 /* check if this is a pointer */
2218 if (type[len - 1] == '*')
2221 /* Try to figure out the arg size*/
2222 if (strncmp(type, "struct", 6) == 0)
2226 if (strcmp(type, "u8") == 0)
2229 if (strcmp(type, "u16") == 0)
2230 return val & 0xffff;
2232 if (strcmp(type, "u32") == 0)
2233 return val & 0xffffffff;
2235 if (strcmp(type, "u64") == 0 ||
2236 strcmp(type, "s64"))
2239 if (strcmp(type, "s8") == 0)
2240 return (unsigned long long)(char)val & 0xff;
2242 if (strcmp(type, "s16") == 0)
2243 return (unsigned long long)(short)val & 0xffff;
2245 if (strcmp(type, "s32") == 0)
2246 return (unsigned long long)(int)val & 0xffffffff;
2248 if (strncmp(type, "unsigned ", 9) == 0) {
2253 if (strcmp(type, "char") == 0) {
2255 return (unsigned long long)(char)val & 0xff;
2260 if (strcmp(type, "short") == 0) {
2262 return (unsigned long long)(short)val & 0xffff;
2264 return val & 0xffff;
2267 if (strcmp(type, "int") == 0) {
2269 return (unsigned long long)(int)val & 0xffffffff;
2271 return val & 0xffffffff;
2278 * Try to figure out the type.
2280 static unsigned long long
2281 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2283 if (arg->type != TEP_PRINT_TYPE) {
2284 do_warning("expected type argument");
2288 return eval_type_str(val, arg->typecast.type, pointer);
2291 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2293 long long left, right;
2296 switch (arg->type) {
2297 case TEP_PRINT_ATOM:
2298 *val = strtoll(arg->atom.atom, NULL, 0);
2300 case TEP_PRINT_TYPE:
2301 ret = arg_num_eval(arg->typecast.item, val);
2304 *val = eval_type(*val, arg, 0);
2307 switch (arg->op.op[0]) {
2309 ret = arg_num_eval(arg->op.left, &left);
2312 ret = arg_num_eval(arg->op.right, &right);
2316 *val = left || right;
2318 *val = left | right;
2321 ret = arg_num_eval(arg->op.left, &left);
2324 ret = arg_num_eval(arg->op.right, &right);
2328 *val = left && right;
2330 *val = left & right;
2333 ret = arg_num_eval(arg->op.left, &left);
2336 ret = arg_num_eval(arg->op.right, &right);
2339 switch (arg->op.op[1]) {
2341 *val = left < right;
2344 *val = left << right;
2347 *val = left <= right;
2350 do_warning("unknown op '%s'", arg->op.op);
2355 ret = arg_num_eval(arg->op.left, &left);
2358 ret = arg_num_eval(arg->op.right, &right);
2361 switch (arg->op.op[1]) {
2363 *val = left > right;
2366 *val = left >> right;
2369 *val = left >= right;
2372 do_warning("unknown op '%s'", arg->op.op);
2377 ret = arg_num_eval(arg->op.left, &left);
2380 ret = arg_num_eval(arg->op.right, &right);
2384 if (arg->op.op[1] != '=') {
2385 do_warning("unknown op '%s'", arg->op.op);
2388 *val = left == right;
2391 ret = arg_num_eval(arg->op.left, &left);
2394 ret = arg_num_eval(arg->op.right, &right);
2398 switch (arg->op.op[1]) {
2400 *val = left != right;
2403 do_warning("unknown op '%s'", arg->op.op);
2408 /* check for negative */
2409 if (arg->op.left->type == TEP_PRINT_NULL)
2412 ret = arg_num_eval(arg->op.left, &left);
2415 ret = arg_num_eval(arg->op.right, &right);
2418 *val = left - right;
2421 if (arg->op.left->type == TEP_PRINT_NULL)
2424 ret = arg_num_eval(arg->op.left, &left);
2427 ret = arg_num_eval(arg->op.right, &right);
2430 *val = left + right;
2433 ret = arg_num_eval(arg->op.right, &right);
2439 do_warning("unknown op '%s'", arg->op.op);
2444 case TEP_PRINT_NULL:
2445 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2446 case TEP_PRINT_STRING:
2447 case TEP_PRINT_BSTRING:
2448 case TEP_PRINT_BITMASK:
2450 do_warning("invalid eval type %d", arg->type);
2457 static char *arg_eval (struct tep_print_arg *arg)
2460 static char buf[24];
2462 switch (arg->type) {
2463 case TEP_PRINT_ATOM:
2464 return arg->atom.atom;
2465 case TEP_PRINT_TYPE:
2466 return arg_eval(arg->typecast.item);
2468 if (!arg_num_eval(arg, &val))
2470 sprintf(buf, "%lld", val);
2473 case TEP_PRINT_NULL:
2474 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2475 case TEP_PRINT_STRING:
2476 case TEP_PRINT_BSTRING:
2477 case TEP_PRINT_BITMASK:
2479 do_warning("invalid eval type %d", arg->type);
2486 static enum tep_event_type
2487 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2489 enum tep_event_type type;
2490 struct tep_print_arg *arg = NULL;
2491 struct tep_print_flag_sym *field;
2497 type = read_token_item(&token);
2498 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2506 type = process_arg(event, arg, &token);
2508 if (type == TEP_EVENT_OP)
2509 type = process_op(event, arg, &token);
2511 if (type == TEP_EVENT_ERROR)
2514 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2517 field = calloc(1, sizeof(*field));
2521 value = arg_eval(arg);
2523 goto out_free_field;
2524 field->value = strdup(value);
2525 if (field->value == NULL)
2526 goto out_free_field;
2534 type = process_arg(event, arg, &token);
2535 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2536 goto out_free_field;
2538 value = arg_eval(arg);
2540 goto out_free_field;
2541 field->str = strdup(value);
2542 if (field->str == NULL)
2543 goto out_free_field;
2548 list = &field->next;
2551 type = read_token_item(&token);
2552 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2558 free_flag_sym(field);
2564 return TEP_EVENT_ERROR;
2567 static enum tep_event_type
2568 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2570 struct tep_print_arg *field;
2571 enum tep_event_type type;
2574 memset(arg, 0, sizeof(*arg));
2575 arg->type = TEP_PRINT_FLAGS;
2577 field = alloc_arg();
2579 do_warning_event(event, "%s: not enough memory!", __func__);
2583 type = process_field_arg(event, field, &token);
2585 /* Handle operations in the first argument */
2586 while (type == TEP_EVENT_OP)
2587 type = process_op(event, field, &token);
2589 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2590 goto out_free_field;
2593 arg->flags.field = field;
2595 type = read_token_item(&token);
2596 if (event_item_type(type)) {
2597 arg->flags.delim = token;
2598 type = read_token_item(&token);
2601 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2604 type = process_fields(event, &arg->flags.flags, &token);
2605 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2609 type = read_token_item(tok);
2617 return TEP_EVENT_ERROR;
2620 static enum tep_event_type
2621 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2623 struct tep_print_arg *field;
2624 enum tep_event_type type;
2627 memset(arg, 0, sizeof(*arg));
2628 arg->type = TEP_PRINT_SYMBOL;
2630 field = alloc_arg();
2632 do_warning_event(event, "%s: not enough memory!", __func__);
2636 type = process_field_arg(event, field, &token);
2638 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2639 goto out_free_field;
2641 arg->symbol.field = field;
2643 type = process_fields(event, &arg->symbol.symbols, &token);
2644 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2648 type = read_token_item(tok);
2656 return TEP_EVENT_ERROR;
2659 static enum tep_event_type
2660 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2661 char **tok, enum tep_print_arg_type type)
2663 memset(arg, 0, sizeof(*arg));
2666 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2669 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2672 return read_token_item(tok);
2675 free_arg(arg->hex.field);
2676 arg->hex.field = NULL;
2679 return TEP_EVENT_ERROR;
2682 static enum tep_event_type
2683 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2685 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2688 static enum tep_event_type
2689 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2692 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2695 static enum tep_event_type
2696 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2698 memset(arg, 0, sizeof(*arg));
2699 arg->type = TEP_PRINT_INT_ARRAY;
2701 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2704 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2707 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2710 return read_token_item(tok);
2713 free_arg(arg->int_array.count);
2714 arg->int_array.count = NULL;
2716 free_arg(arg->int_array.field);
2717 arg->int_array.field = NULL;
2720 return TEP_EVENT_ERROR;
2723 static enum tep_event_type
2724 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2726 struct tep_format_field *field;
2727 enum tep_event_type type;
2730 memset(arg, 0, sizeof(*arg));
2731 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2734 * The item within the parenthesis is another field that holds
2735 * the index into where the array starts.
2737 type = read_token(&token);
2739 if (type != TEP_EVENT_ITEM)
2742 /* Find the field */
2744 field = tep_find_field(event, token);
2748 arg->dynarray.field = field;
2749 arg->dynarray.index = 0;
2751 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2755 type = read_token_item(&token);
2757 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2763 do_warning_event(event, "%s: not enough memory!", __func__);
2765 return TEP_EVENT_ERROR;
2768 type = process_arg(event, arg, &token);
2769 if (type == TEP_EVENT_ERROR)
2772 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2776 type = read_token_item(tok);
2784 return TEP_EVENT_ERROR;
2787 static enum tep_event_type
2788 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2791 struct tep_format_field *field;
2792 enum tep_event_type type;
2795 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2798 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2800 /* Find the field */
2801 field = tep_find_field(event, token);
2805 arg->dynarray.field = field;
2806 arg->dynarray.index = 0;
2808 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2811 type = read_token(&token);
2820 return TEP_EVENT_ERROR;
2823 static enum tep_event_type
2824 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2826 struct tep_print_arg *item_arg;
2827 enum tep_event_type type;
2830 type = process_arg(event, arg, &token);
2832 if (type == TEP_EVENT_ERROR)
2835 if (type == TEP_EVENT_OP)
2836 type = process_op(event, arg, &token);
2838 if (type == TEP_EVENT_ERROR)
2841 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2845 type = read_token_item(&token);
2848 * If the next token is an item or another open paren, then
2849 * this was a typecast.
2851 if (event_item_type(type) ||
2852 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2854 /* make this a typecast and contine */
2856 /* prevous must be an atom */
2857 if (arg->type != TEP_PRINT_ATOM) {
2858 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2862 item_arg = alloc_arg();
2864 do_warning_event(event, "%s: not enough memory!",
2869 arg->type = TEP_PRINT_TYPE;
2870 arg->typecast.type = arg->atom.atom;
2871 arg->typecast.item = item_arg;
2872 type = process_arg_token(event, item_arg, &token, type);
2882 return TEP_EVENT_ERROR;
2886 static enum tep_event_type
2887 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2890 enum tep_event_type type;
2893 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2896 arg->type = TEP_PRINT_STRING;
2897 arg->string.string = token;
2898 arg->string.offset = -1;
2900 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2903 type = read_token(&token);
2912 return TEP_EVENT_ERROR;
2915 static enum tep_event_type
2916 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2919 enum tep_event_type type;
2922 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2925 arg->type = TEP_PRINT_BITMASK;
2926 arg->bitmask.bitmask = token;
2927 arg->bitmask.offset = -1;
2929 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2932 type = read_token(&token);
2941 return TEP_EVENT_ERROR;
2944 static struct tep_function_handler *
2945 find_func_handler(struct tep_handle *pevent, char *func_name)
2947 struct tep_function_handler *func;
2952 for (func = pevent->func_handlers; func; func = func->next) {
2953 if (strcmp(func->name, func_name) == 0)
2960 static void remove_func_handler(struct tep_handle *pevent, char *func_name)
2962 struct tep_function_handler *func;
2963 struct tep_function_handler **next;
2965 next = &pevent->func_handlers;
2966 while ((func = *next)) {
2967 if (strcmp(func->name, func_name) == 0) {
2969 free_func_handle(func);
2976 static enum tep_event_type
2977 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
2978 struct tep_print_arg *arg, char **tok)
2980 struct tep_print_arg **next_arg;
2981 struct tep_print_arg *farg;
2982 enum tep_event_type type;
2986 arg->type = TEP_PRINT_FUNC;
2987 arg->func.func = func;
2991 next_arg = &(arg->func.args);
2992 for (i = 0; i < func->nr_args; i++) {
2995 do_warning_event(event, "%s: not enough memory!",
2997 return TEP_EVENT_ERROR;
3000 type = process_arg(event, farg, &token);
3001 if (i < (func->nr_args - 1)) {
3002 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3003 do_warning_event(event,
3004 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3005 func->name, func->nr_args,
3006 event->name, i + 1);
3010 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3011 do_warning_event(event,
3012 "Error: function '%s()' only expects %d arguments but event %s has more",
3013 func->name, func->nr_args, event->name);
3019 next_arg = &(farg->next);
3023 type = read_token(&token);
3031 return TEP_EVENT_ERROR;
3034 static enum tep_event_type
3035 process_function(struct tep_event *event, struct tep_print_arg *arg,
3036 char *token, char **tok)
3038 struct tep_function_handler *func;
3040 if (strcmp(token, "__print_flags") == 0) {
3043 return process_flags(event, arg, tok);
3045 if (strcmp(token, "__print_symbolic") == 0) {
3047 is_symbolic_field = 1;
3048 return process_symbols(event, arg, tok);
3050 if (strcmp(token, "__print_hex") == 0) {
3052 return process_hex(event, arg, tok);
3054 if (strcmp(token, "__print_hex_str") == 0) {
3056 return process_hex_str(event, arg, tok);
3058 if (strcmp(token, "__print_array") == 0) {
3060 return process_int_array(event, arg, tok);
3062 if (strcmp(token, "__get_str") == 0) {
3064 return process_str(event, arg, tok);
3066 if (strcmp(token, "__get_bitmask") == 0) {
3068 return process_bitmask(event, arg, tok);
3070 if (strcmp(token, "__get_dynamic_array") == 0) {
3072 return process_dynamic_array(event, arg, tok);
3074 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3076 return process_dynamic_array_len(event, arg, tok);
3079 func = find_func_handler(event->pevent, token);
3082 return process_func_handler(event, func, arg, tok);
3085 do_warning_event(event, "function %s not defined", token);
3087 return TEP_EVENT_ERROR;
3090 static enum tep_event_type
3091 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3092 char **tok, enum tep_event_type type)
3100 case TEP_EVENT_ITEM:
3101 if (strcmp(token, "REC") == 0) {
3103 type = process_entry(event, arg, &token);
3107 /* test the next token */
3108 type = read_token_item(&token);
3111 * If the next token is a parenthesis, then this
3114 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3117 /* this will free atom. */
3118 type = process_function(event, arg, atom, &token);
3121 /* atoms can be more than one token long */
3122 while (type == TEP_EVENT_ITEM) {
3124 new_atom = realloc(atom,
3125 strlen(atom) + strlen(token) + 2);
3130 return TEP_EVENT_ERROR;
3134 strcat(atom, token);
3136 type = read_token_item(&token);
3139 arg->type = TEP_PRINT_ATOM;
3140 arg->atom.atom = atom;
3143 case TEP_EVENT_DQUOTE:
3144 case TEP_EVENT_SQUOTE:
3145 arg->type = TEP_PRINT_ATOM;
3146 arg->atom.atom = token;
3147 type = read_token_item(&token);
3149 case TEP_EVENT_DELIM:
3150 if (strcmp(token, "(") == 0) {
3152 type = process_paren(event, arg, &token);
3156 /* handle single ops */
3157 arg->type = TEP_PRINT_OP;
3159 arg->op.left = NULL;
3160 type = process_op(event, arg, &token);
3162 /* On error, the op is freed */
3163 if (type == TEP_EVENT_ERROR)
3166 /* return error type if errored */
3169 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3171 do_warning_event(event, "unexpected type %d", type);
3172 return TEP_EVENT_ERROR;
3179 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3181 enum tep_event_type type = TEP_EVENT_ERROR;
3182 struct tep_print_arg *arg;
3187 if (type == TEP_EVENT_NEWLINE) {
3188 type = read_token_item(&token);
3194 do_warning_event(event, "%s: not enough memory!",
3199 type = process_arg(event, arg, &token);
3201 if (type == TEP_EVENT_ERROR) {
3210 if (type == TEP_EVENT_OP) {
3211 type = process_op(event, arg, &token);
3213 if (type == TEP_EVENT_ERROR) {
3222 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3229 } while (type != TEP_EVENT_NONE);
3231 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3237 static int event_read_print(struct tep_event *event)
3239 enum tep_event_type type;
3243 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3246 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3249 if (read_expected(TEP_EVENT_OP, ":") < 0)
3252 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3256 event->print_fmt.format = token;
3257 event->print_fmt.args = NULL;
3259 /* ok to have no arg */
3260 type = read_token_item(&token);
3262 if (type == TEP_EVENT_NONE)
3265 /* Handle concatenation of print lines */
3266 if (type == TEP_EVENT_DQUOTE) {
3269 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3272 free_token(event->print_fmt.format);
3273 event->print_fmt.format = NULL;
3278 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3283 ret = event_read_print_args(event, &event->print_fmt.args);
3295 * tep_find_common_field - return a common field by event
3296 * @event: handle for the event
3297 * @name: the name of the common field to return
3299 * Returns a common field from the event by the given @name.
3300 * This only searches the common fields and not all field.
3302 struct tep_format_field *
3303 tep_find_common_field(struct tep_event *event, const char *name)
3305 struct tep_format_field *format;
3307 for (format = event->format.common_fields;
3308 format; format = format->next) {
3309 if (strcmp(format->name, name) == 0)
3317 * tep_find_field - find a non-common field
3318 * @event: handle for the event
3319 * @name: the name of the non-common field
3321 * Returns a non-common field by the given @name.
3322 * This does not search common fields.
3324 struct tep_format_field *
3325 tep_find_field(struct tep_event *event, const char *name)
3327 struct tep_format_field *format;
3329 for (format = event->format.fields;
3330 format; format = format->next) {
3331 if (strcmp(format->name, name) == 0)
3339 * tep_find_any_field - find any field by name
3340 * @event: handle for the event
3341 * @name: the name of the field
3343 * Returns a field by the given @name.
3344 * This searches the common field names first, then
3345 * the non-common ones if a common one was not found.
3347 struct tep_format_field *
3348 tep_find_any_field(struct tep_event *event, const char *name)
3350 struct tep_format_field *format;
3352 format = tep_find_common_field(event, name);
3355 return tep_find_field(event, name);
3359 * tep_read_number - read a number from data
3360 * @pevent: handle for the pevent
3361 * @ptr: the raw data
3362 * @size: the size of the data that holds the number
3364 * Returns the number (converted to host) from the
3367 unsigned long long tep_read_number(struct tep_handle *pevent,
3368 const void *ptr, int size)
3370 unsigned long long val;
3374 return *(unsigned char *)ptr;
3376 return tep_data2host2(pevent, *(unsigned short *)ptr);
3378 return tep_data2host4(pevent, *(unsigned int *)ptr);
3380 memcpy(&val, (ptr), sizeof(unsigned long long));
3381 return tep_data2host8(pevent, val);
3389 * tep_read_number_field - read a number from data
3390 * @field: a handle to the field
3391 * @data: the raw data to read
3392 * @value: the value to place the number in
3394 * Reads raw data according to a field offset and size,
3395 * and translates it into @value.
3397 * Returns 0 on success, -1 otherwise.
3399 int tep_read_number_field(struct tep_format_field *field, const void *data,
3400 unsigned long long *value)
3404 switch (field->size) {
3409 *value = tep_read_number(field->event->pevent,
3410 data + field->offset, field->size);
3417 static int get_common_info(struct tep_handle *pevent,
3418 const char *type, int *offset, int *size)
3420 struct tep_event *event;
3421 struct tep_format_field *field;
3424 * All events should have the same common elements.
3425 * Pick any event to find where the type is;
3427 if (!pevent->events) {
3428 do_warning("no event_list!");
3432 event = pevent->events[0];
3433 field = tep_find_common_field(event, type);
3437 *offset = field->offset;
3438 *size = field->size;
3443 static int __parse_common(struct tep_handle *pevent, void *data,
3444 int *size, int *offset, const char *name)
3449 ret = get_common_info(pevent, name, offset, size);
3453 return tep_read_number(pevent, data + *offset, *size);
3456 static int trace_parse_common_type(struct tep_handle *pevent, void *data)
3458 return __parse_common(pevent, data,
3459 &pevent->type_size, &pevent->type_offset,
3463 static int parse_common_pid(struct tep_handle *pevent, void *data)
3465 return __parse_common(pevent, data,
3466 &pevent->pid_size, &pevent->pid_offset,
3470 static int parse_common_pc(struct tep_handle *pevent, void *data)
3472 return __parse_common(pevent, data,
3473 &pevent->pc_size, &pevent->pc_offset,
3474 "common_preempt_count");
3477 static int parse_common_flags(struct tep_handle *pevent, void *data)
3479 return __parse_common(pevent, data,
3480 &pevent->flags_size, &pevent->flags_offset,
3484 static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
3486 return __parse_common(pevent, data,
3487 &pevent->ld_size, &pevent->ld_offset,
3488 "common_lock_depth");
3491 static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
3493 return __parse_common(pevent, data,
3494 &pevent->ld_size, &pevent->ld_offset,
3495 "common_migrate_disable");
3498 static int events_id_cmp(const void *a, const void *b);
3501 * tep_find_event - find an event by given id
3502 * @pevent: a handle to the pevent
3503 * @id: the id of the event
3505 * Returns an event that has a given @id.
3507 struct tep_event *tep_find_event(struct tep_handle *pevent, int id)
3509 struct tep_event **eventptr;
3510 struct tep_event key;
3511 struct tep_event *pkey = &key;
3513 /* Check cache first */
3514 if (pevent->last_event && pevent->last_event->id == id)
3515 return pevent->last_event;
3519 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3520 sizeof(*pevent->events), events_id_cmp);
3523 pevent->last_event = *eventptr;
3531 * tep_find_event_by_name - find an event by given name
3532 * @pevent: a handle to the pevent
3533 * @sys: the system name to search for
3534 * @name: the name of the event to search for
3536 * This returns an event with a given @name and under the system
3537 * @sys. If @sys is NULL the first event with @name is returned.
3540 tep_find_event_by_name(struct tep_handle *pevent,
3541 const char *sys, const char *name)
3543 struct tep_event *event = NULL;
3546 if (pevent->last_event &&
3547 strcmp(pevent->last_event->name, name) == 0 &&
3548 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3549 return pevent->last_event;
3551 for (i = 0; i < pevent->nr_events; i++) {
3552 event = pevent->events[i];
3553 if (strcmp(event->name, name) == 0) {
3556 if (strcmp(event->system, sys) == 0)
3560 if (i == pevent->nr_events)
3563 pevent->last_event = event;
3567 static unsigned long long
3568 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3570 struct tep_handle *pevent = event->pevent;
3571 unsigned long long val = 0;
3572 unsigned long long left, right;
3573 struct tep_print_arg *typearg = NULL;
3574 struct tep_print_arg *larg;
3575 unsigned long offset;
3576 unsigned int field_size;
3578 switch (arg->type) {
3579 case TEP_PRINT_NULL:
3582 case TEP_PRINT_ATOM:
3583 return strtoull(arg->atom.atom, NULL, 0);
3584 case TEP_PRINT_FIELD:
3585 if (!arg->field.field) {
3586 arg->field.field = tep_find_any_field(event, arg->field.name);
3587 if (!arg->field.field)
3588 goto out_warning_field;
3591 /* must be a number */
3592 val = tep_read_number(pevent, data + arg->field.field->offset,
3593 arg->field.field->size);
3595 case TEP_PRINT_FLAGS:
3596 case TEP_PRINT_SYMBOL:
3597 case TEP_PRINT_INT_ARRAY:
3599 case TEP_PRINT_HEX_STR:
3601 case TEP_PRINT_TYPE:
3602 val = eval_num_arg(data, size, event, arg->typecast.item);
3603 return eval_type(val, arg, 0);
3604 case TEP_PRINT_STRING:
3605 case TEP_PRINT_BSTRING:
3606 case TEP_PRINT_BITMASK:
3608 case TEP_PRINT_FUNC: {
3611 val = process_defined_func(&s, data, size, event, arg);
3612 trace_seq_destroy(&s);
3616 if (strcmp(arg->op.op, "[") == 0) {
3618 * Arrays are special, since we don't want
3619 * to read the arg as is.
3621 right = eval_num_arg(data, size, event, arg->op.right);
3623 /* handle typecasts */
3624 larg = arg->op.left;
3625 while (larg->type == TEP_PRINT_TYPE) {
3628 larg = larg->typecast.item;
3631 /* Default to long size */
3632 field_size = pevent->long_size;
3634 switch (larg->type) {
3635 case TEP_PRINT_DYNAMIC_ARRAY:
3636 offset = tep_read_number(pevent,
3637 data + larg->dynarray.field->offset,
3638 larg->dynarray.field->size);
3639 if (larg->dynarray.field->elementsize)
3640 field_size = larg->dynarray.field->elementsize;
3642 * The actual length of the dynamic array is stored
3643 * in the top half of the field, and the offset
3644 * is in the bottom half of the 32 bit field.
3649 case TEP_PRINT_FIELD:
3650 if (!larg->field.field) {
3652 tep_find_any_field(event, larg->field.name);
3653 if (!larg->field.field) {
3655 goto out_warning_field;
3658 field_size = larg->field.field->elementsize;
3659 offset = larg->field.field->offset +
3660 right * larg->field.field->elementsize;
3663 goto default_op; /* oops, all bets off */
3665 val = tep_read_number(pevent,
3666 data + offset, field_size);
3668 val = eval_type(val, typearg, 1);
3670 } else if (strcmp(arg->op.op, "?") == 0) {
3671 left = eval_num_arg(data, size, event, arg->op.left);
3672 arg = arg->op.right;
3674 val = eval_num_arg(data, size, event, arg->op.left);
3676 val = eval_num_arg(data, size, event, arg->op.right);
3680 left = eval_num_arg(data, size, event, arg->op.left);
3681 right = eval_num_arg(data, size, event, arg->op.right);
3682 switch (arg->op.op[0]) {
3684 switch (arg->op.op[1]) {
3689 val = left != right;
3692 goto out_warning_op;
3700 val = left || right;
3706 val = left && right;
3711 switch (arg->op.op[1]) {
3716 val = left << right;
3719 val = left <= right;
3722 goto out_warning_op;
3726 switch (arg->op.op[1]) {
3731 val = left >> right;
3734 val = left >= right;
3737 goto out_warning_op;
3741 if (arg->op.op[1] != '=')
3742 goto out_warning_op;
3744 val = left == right;
3762 goto out_warning_op;
3765 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3766 offset = tep_read_number(pevent,
3767 data + arg->dynarray.field->offset,
3768 arg->dynarray.field->size);
3770 * The total allocated length of the dynamic array is
3771 * stored in the top half of the field, and the offset
3772 * is in the bottom half of the 32 bit field.
3774 val = (unsigned long long)(offset >> 16);
3776 case TEP_PRINT_DYNAMIC_ARRAY:
3777 /* Without [], we pass the address to the dynamic data */
3778 offset = tep_read_number(pevent,
3779 data + arg->dynarray.field->offset,
3780 arg->dynarray.field->size);
3782 * The total allocated length of the dynamic array is
3783 * stored in the top half of the field, and the offset
3784 * is in the bottom half of the 32 bit field.
3787 val = (unsigned long long)((unsigned long)data + offset);
3789 default: /* not sure what to do there */
3795 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3799 do_warning_event(event, "%s: field %s not found",
3800 __func__, arg->field.name);
3806 unsigned long long value;
3809 static const struct flag flags[] = {
3810 { "HI_SOFTIRQ", 0 },
3811 { "TIMER_SOFTIRQ", 1 },
3812 { "NET_TX_SOFTIRQ", 2 },
3813 { "NET_RX_SOFTIRQ", 3 },
3814 { "BLOCK_SOFTIRQ", 4 },
3815 { "IRQ_POLL_SOFTIRQ", 5 },
3816 { "TASKLET_SOFTIRQ", 6 },
3817 { "SCHED_SOFTIRQ", 7 },
3818 { "HRTIMER_SOFTIRQ", 8 },
3819 { "RCU_SOFTIRQ", 9 },
3821 { "HRTIMER_NORESTART", 0 },
3822 { "HRTIMER_RESTART", 1 },
3825 static long long eval_flag(const char *flag)
3830 * Some flags in the format files do not get converted.
3831 * If the flag is not numeric, see if it is something that
3832 * we already know about.
3834 if (isdigit(flag[0]))
3835 return strtoull(flag, NULL, 0);
3837 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3838 if (strcmp(flags[i].name, flag) == 0)
3839 return flags[i].value;
3844 static void print_str_to_seq(struct trace_seq *s, const char *format,
3845 int len_arg, const char *str)
3848 trace_seq_printf(s, format, len_arg, str);
3850 trace_seq_printf(s, format, str);
3853 static void print_bitmask_to_seq(struct tep_handle *pevent,
3854 struct trace_seq *s, const char *format,
3855 int len_arg, const void *data, int size)
3857 int nr_bits = size * 8;
3858 int str_size = (nr_bits + 3) / 4;
3866 * The kernel likes to put in commas every 32 bits, we
3869 str_size += (nr_bits - 1) / 32;
3871 str = malloc(str_size + 1);
3873 do_warning("%s: not enough memory!", __func__);
3878 /* Start out with -2 for the two chars per byte */
3879 for (i = str_size - 2; i >= 0; i -= 2) {
3881 * data points to a bit mask of size bytes.
3882 * In the kernel, this is an array of long words, thus
3883 * endianness is very important.
3885 if (pevent->file_bigendian)
3886 index = size - (len + 1);
3890 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3891 memcpy(str + i, buf, 2);
3893 if (!(len & 3) && i > 0) {
3900 trace_seq_printf(s, format, len_arg, str);
3902 trace_seq_printf(s, format, str);
3907 static void print_str_arg(struct trace_seq *s, void *data, int size,
3908 struct tep_event *event, const char *format,
3909 int len_arg, struct tep_print_arg *arg)
3911 struct tep_handle *pevent = event->pevent;
3912 struct tep_print_flag_sym *flag;
3913 struct tep_format_field *field;
3914 struct printk_map *printk;
3915 long long val, fval;
3916 unsigned long long addr;
3922 switch (arg->type) {
3923 case TEP_PRINT_NULL:
3926 case TEP_PRINT_ATOM:
3927 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3929 case TEP_PRINT_FIELD:
3930 field = arg->field.field;
3932 field = tep_find_any_field(event, arg->field.name);
3934 str = arg->field.name;
3935 goto out_warning_field;
3937 arg->field.field = field;
3939 /* Zero sized fields, mean the rest of the data */
3940 len = field->size ? : size - field->offset;
3943 * Some events pass in pointers. If this is not an array
3944 * and the size is the same as long_size, assume that it
3947 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
3948 field->size == pevent->long_size) {
3950 /* Handle heterogeneous recording and processing
3954 * Traces recorded on 32-bit devices (32-bit
3955 * addressing) and processed on 64-bit devices:
3956 * In this case, only 32 bits should be read.
3959 * Traces recorded on 64 bit devices and processed
3960 * on 32-bit devices:
3961 * In this case, 64 bits must be read.
3963 addr = (pevent->long_size == 8) ?
3964 *(unsigned long long *)(data + field->offset) :
3965 (unsigned long long)*(unsigned int *)(data + field->offset);
3967 /* Check if it matches a print format */
3968 printk = find_printk(pevent, addr);
3970 trace_seq_puts(s, printk->printk);
3972 trace_seq_printf(s, "%llx", addr);
3975 str = malloc(len + 1);
3977 do_warning_event(event, "%s: not enough memory!",
3981 memcpy(str, data + field->offset, len);
3983 print_str_to_seq(s, format, len_arg, str);
3986 case TEP_PRINT_FLAGS:
3987 val = eval_num_arg(data, size, event, arg->flags.field);
3989 for (flag = arg->flags.flags; flag; flag = flag->next) {
3990 fval = eval_flag(flag->value);
3991 if (!val && fval < 0) {
3992 print_str_to_seq(s, format, len_arg, flag->str);
3995 if (fval > 0 && (val & fval) == fval) {
3996 if (print && arg->flags.delim)
3997 trace_seq_puts(s, arg->flags.delim);
3998 print_str_to_seq(s, format, len_arg, flag->str);
4004 if (print && arg->flags.delim)
4005 trace_seq_puts(s, arg->flags.delim);
4006 trace_seq_printf(s, "0x%llx", val);
4009 case TEP_PRINT_SYMBOL:
4010 val = eval_num_arg(data, size, event, arg->symbol.field);
4011 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4012 fval = eval_flag(flag->value);
4014 print_str_to_seq(s, format, len_arg, flag->str);
4019 trace_seq_printf(s, "0x%llx", val);
4022 case TEP_PRINT_HEX_STR:
4023 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4024 unsigned long offset;
4025 offset = tep_read_number(pevent,
4026 data + arg->hex.field->dynarray.field->offset,
4027 arg->hex.field->dynarray.field->size);
4028 hex = data + (offset & 0xffff);
4030 field = arg->hex.field->field.field;
4032 str = arg->hex.field->field.name;
4033 field = tep_find_any_field(event, str);
4035 goto out_warning_field;
4036 arg->hex.field->field.field = field;
4038 hex = data + field->offset;
4040 len = eval_num_arg(data, size, event, arg->hex.size);
4041 for (i = 0; i < len; i++) {
4042 if (i && arg->type == TEP_PRINT_HEX)
4043 trace_seq_putc(s, ' ');
4044 trace_seq_printf(s, "%02x", hex[i]);
4048 case TEP_PRINT_INT_ARRAY: {
4052 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4053 unsigned long offset;
4054 struct tep_format_field *field =
4055 arg->int_array.field->dynarray.field;
4056 offset = tep_read_number(pevent,
4057 data + field->offset,
4059 num = data + (offset & 0xffff);
4061 field = arg->int_array.field->field.field;
4063 str = arg->int_array.field->field.name;
4064 field = tep_find_any_field(event, str);
4066 goto out_warning_field;
4067 arg->int_array.field->field.field = field;
4069 num = data + field->offset;
4071 len = eval_num_arg(data, size, event, arg->int_array.count);
4072 el_size = eval_num_arg(data, size, event,
4073 arg->int_array.el_size);
4074 for (i = 0; i < len; i++) {
4076 trace_seq_putc(s, ' ');
4079 trace_seq_printf(s, "%u", *(uint8_t *)num);
4080 } else if (el_size == 2) {
4081 trace_seq_printf(s, "%u", *(uint16_t *)num);
4082 } else if (el_size == 4) {
4083 trace_seq_printf(s, "%u", *(uint32_t *)num);
4084 } else if (el_size == 8) {
4085 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4087 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4088 el_size, *(uint8_t *)num);
4096 case TEP_PRINT_TYPE:
4098 case TEP_PRINT_STRING: {
4101 if (arg->string.offset == -1) {
4102 struct tep_format_field *f;
4104 f = tep_find_any_field(event, arg->string.string);
4105 arg->string.offset = f->offset;
4107 str_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->string.offset));
4108 str_offset &= 0xffff;
4109 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4112 case TEP_PRINT_BSTRING:
4113 print_str_to_seq(s, format, len_arg, arg->string.string);
4115 case TEP_PRINT_BITMASK: {
4119 if (arg->bitmask.offset == -1) {
4120 struct tep_format_field *f;
4122 f = tep_find_any_field(event, arg->bitmask.bitmask);
4123 arg->bitmask.offset = f->offset;
4125 bitmask_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->bitmask.offset));
4126 bitmask_size = bitmask_offset >> 16;
4127 bitmask_offset &= 0xffff;
4128 print_bitmask_to_seq(pevent, s, format, len_arg,
4129 data + bitmask_offset, bitmask_size);
4134 * The only op for string should be ? :
4136 if (arg->op.op[0] != '?')
4138 val = eval_num_arg(data, size, event, arg->op.left);
4140 print_str_arg(s, data, size, event,
4141 format, len_arg, arg->op.right->op.left);
4143 print_str_arg(s, data, size, event,
4144 format, len_arg, arg->op.right->op.right);
4146 case TEP_PRINT_FUNC:
4147 process_defined_func(s, data, size, event, arg);
4157 do_warning_event(event, "%s: field %s not found",
4158 __func__, arg->field.name);
4161 static unsigned long long
4162 process_defined_func(struct trace_seq *s, void *data, int size,
4163 struct tep_event *event, struct tep_print_arg *arg)
4165 struct tep_function_handler *func_handle = arg->func.func;
4166 struct func_params *param;
4167 unsigned long long *args;
4168 unsigned long long ret;
4169 struct tep_print_arg *farg;
4170 struct trace_seq str;
4172 struct save_str *next;
4174 } *strings = NULL, *string;
4177 if (!func_handle->nr_args) {
4178 ret = (*func_handle->func)(s, NULL);
4182 farg = arg->func.args;
4183 param = func_handle->params;
4186 args = malloc(sizeof(*args) * func_handle->nr_args);
4190 for (i = 0; i < func_handle->nr_args; i++) {
4191 switch (param->type) {
4192 case TEP_FUNC_ARG_INT:
4193 case TEP_FUNC_ARG_LONG:
4194 case TEP_FUNC_ARG_PTR:
4195 args[i] = eval_num_arg(data, size, event, farg);
4197 case TEP_FUNC_ARG_STRING:
4198 trace_seq_init(&str);
4199 print_str_arg(&str, data, size, event, "%s", -1, farg);
4200 trace_seq_terminate(&str);
4201 string = malloc(sizeof(*string));
4203 do_warning_event(event, "%s(%d): malloc str",
4204 __func__, __LINE__);
4207 string->next = strings;
4208 string->str = strdup(str.buffer);
4211 do_warning_event(event, "%s(%d): malloc str",
4212 __func__, __LINE__);
4215 args[i] = (uintptr_t)string->str;
4217 trace_seq_destroy(&str);
4221 * Something went totally wrong, this is not
4222 * an input error, something in this code broke.
4224 do_warning_event(event, "Unexpected end of arguments\n");
4228 param = param->next;
4231 ret = (*func_handle->func)(s, args);
4236 strings = string->next;
4242 /* TBD : handle return type here */
4246 static void free_args(struct tep_print_arg *args)
4248 struct tep_print_arg *next;
4258 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4260 struct tep_handle *pevent = event->pevent;
4261 struct tep_format_field *field, *ip_field;
4262 struct tep_print_arg *args, *arg, **next;
4263 unsigned long long ip, val;
4268 field = pevent->bprint_buf_field;
4269 ip_field = pevent->bprint_ip_field;
4272 field = tep_find_field(event, "buf");
4274 do_warning_event(event, "can't find buffer field for binary printk");
4277 ip_field = tep_find_field(event, "ip");
4279 do_warning_event(event, "can't find ip field for binary printk");
4282 pevent->bprint_buf_field = field;
4283 pevent->bprint_ip_field = ip_field;
4286 ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
4289 * The first arg is the IP pointer.
4293 do_warning_event(event, "%s(%d): not enough memory!",
4294 __func__, __LINE__);
4301 arg->type = TEP_PRINT_ATOM;
4303 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4306 /* skip the first "%ps: " */
4307 for (ptr = fmt + 5, bptr = data + field->offset;
4308 bptr < data + size && *ptr; ptr++) {
4333 if (isalnum(ptr[1])) {
4335 /* Check for special pointers */
4344 * Older kernels do not process
4345 * dereferenced pointers.
4346 * Only process if the pointer
4347 * value is a printable.
4349 if (isprint(*(char *)bptr))
4350 goto process_string;
4363 vsize = pevent->long_size;
4377 /* the pointers are always 4 bytes aligned */
4378 bptr = (void *)(((unsigned long)bptr + 3) &
4380 val = tep_read_number(pevent, bptr, vsize);
4384 do_warning_event(event, "%s(%d): not enough memory!",
4385 __func__, __LINE__);
4389 arg->type = TEP_PRINT_ATOM;
4390 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4397 * The '*' case means that an arg is used as the length.
4398 * We need to continue to figure out for what.
4408 do_warning_event(event, "%s(%d): not enough memory!",
4409 __func__, __LINE__);
4413 arg->type = TEP_PRINT_BSTRING;
4414 arg->string.string = strdup(bptr);
4415 if (!arg->string.string)
4417 bptr += strlen(bptr) + 1;
4434 get_bprint_format(void *data, int size __maybe_unused,
4435 struct tep_event *event)
4437 struct tep_handle *pevent = event->pevent;
4438 unsigned long long addr;
4439 struct tep_format_field *field;
4440 struct printk_map *printk;
4443 field = pevent->bprint_fmt_field;
4446 field = tep_find_field(event, "fmt");
4448 do_warning_event(event, "can't find format field for binary printk");
4451 pevent->bprint_fmt_field = field;
4454 addr = tep_read_number(pevent, data + field->offset, field->size);
4456 printk = find_printk(pevent, addr);
4458 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4463 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4469 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4470 struct tep_event *event, struct tep_print_arg *arg)
4473 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4475 if (arg->type == TEP_PRINT_FUNC) {
4476 process_defined_func(s, data, size, event, arg);
4480 if (arg->type != TEP_PRINT_FIELD) {
4481 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4487 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4488 if (!arg->field.field) {
4490 tep_find_any_field(event, arg->field.name);
4491 if (!arg->field.field) {
4492 do_warning_event(event, "%s: field %s not found",
4493 __func__, arg->field.name);
4497 if (arg->field.field->size != 6) {
4498 trace_seq_printf(s, "INVALIDMAC");
4501 buf = data + arg->field.field->offset;
4502 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4505 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4510 fmt = "%03d.%03d.%03d.%03d";
4512 fmt = "%d.%d.%d.%d";
4514 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4517 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4519 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4520 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4523 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4525 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4528 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4531 unsigned char zerolength[8];
4536 bool needcolon = false;
4538 struct in6_addr in6;
4540 memcpy(&in6, addr, sizeof(struct in6_addr));
4542 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4544 memset(zerolength, 0, sizeof(zerolength));
4551 /* find position of longest 0 run */
4552 for (i = 0; i < range; i++) {
4553 for (j = i; j < range; j++) {
4554 if (in6.s6_addr16[j] != 0)
4559 for (i = 0; i < range; i++) {
4560 if (zerolength[i] > longest) {
4561 longest = zerolength[i];
4565 if (longest == 1) /* don't compress a single 0 */
4569 for (i = 0; i < range; i++) {
4570 if (i == colonpos) {
4571 if (needcolon || i == 0)
4572 trace_seq_printf(s, ":");
4573 trace_seq_printf(s, ":");
4579 trace_seq_printf(s, ":");
4582 /* hex u16 without leading 0s */
4583 word = ntohs(in6.s6_addr16[i]);
4587 trace_seq_printf(s, "%x%02x", hi, lo);
4589 trace_seq_printf(s, "%x", lo);
4596 trace_seq_printf(s, ":");
4597 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4603 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4607 for (j = 0; j < 16; j += 2) {
4608 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4609 if (i == 'I' && j < 14)
4610 trace_seq_printf(s, ":");
4615 * %pi4 print an IPv4 address with leading zeros
4616 * %pI4 print an IPv4 address without leading zeros
4617 * %pi6 print an IPv6 address without colons
4618 * %pI6 print an IPv6 address with colons
4619 * %pI6c print an IPv6 address in compressed form with colons
4620 * %pISpc print an IP address based on sockaddr; p adds port.
4622 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4623 void *data, int size, struct tep_event *event,
4624 struct tep_print_arg *arg)
4628 if (arg->type == TEP_PRINT_FUNC) {
4629 process_defined_func(s, data, size, event, arg);
4633 if (arg->type != TEP_PRINT_FIELD) {
4634 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4638 if (!arg->field.field) {
4640 tep_find_any_field(event, arg->field.name);
4641 if (!arg->field.field) {
4642 do_warning("%s: field %s not found",
4643 __func__, arg->field.name);
4648 buf = data + arg->field.field->offset;
4650 if (arg->field.field->size != 4) {
4651 trace_seq_printf(s, "INVALIDIPv4");
4654 print_ip4_addr(s, i, buf);
4659 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4660 void *data, int size, struct tep_event *event,
4661 struct tep_print_arg *arg)
4668 if (i == 'I' && *ptr == 'c') {
4674 if (arg->type == TEP_PRINT_FUNC) {
4675 process_defined_func(s, data, size, event, arg);
4679 if (arg->type != TEP_PRINT_FIELD) {
4680 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4684 if (!arg->field.field) {
4686 tep_find_any_field(event, arg->field.name);
4687 if (!arg->field.field) {
4688 do_warning("%s: field %s not found",
4689 __func__, arg->field.name);
4694 buf = data + arg->field.field->offset;
4696 if (arg->field.field->size != 16) {
4697 trace_seq_printf(s, "INVALIDIPv6");
4702 print_ip6c_addr(s, buf);
4704 print_ip6_addr(s, i, buf);
4709 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4710 void *data, int size, struct tep_event *event,
4711 struct tep_print_arg *arg)
4713 char have_c = 0, have_p = 0;
4715 struct sockaddr_storage *sa;
4732 if (arg->type == TEP_PRINT_FUNC) {
4733 process_defined_func(s, data, size, event, arg);
4737 if (arg->type != TEP_PRINT_FIELD) {
4738 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4742 if (!arg->field.field) {
4744 tep_find_any_field(event, arg->field.name);
4745 if (!arg->field.field) {
4746 do_warning("%s: field %s not found",
4747 __func__, arg->field.name);
4752 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4754 if (sa->ss_family == AF_INET) {
4755 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4757 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4758 trace_seq_printf(s, "INVALIDIPv4");
4762 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4764 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4767 } else if (sa->ss_family == AF_INET6) {
4768 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4770 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4771 trace_seq_printf(s, "INVALIDIPv6");
4776 trace_seq_printf(s, "[");
4778 buf = (unsigned char *) &sa6->sin6_addr;
4780 print_ip6c_addr(s, buf);
4782 print_ip6_addr(s, i, buf);
4785 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4791 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4792 void *data, int size, struct tep_event *event,
4793 struct tep_print_arg *arg)
4795 char i = *ptr; /* 'i' or 'I' */
4808 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4811 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4814 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4823 static int is_printable_array(char *p, unsigned int len)
4827 for (i = 0; i < len && p[i]; i++)
4828 if (!isprint(p[i]) && !isspace(p[i]))
4833 void tep_print_field(struct trace_seq *s, void *data,
4834 struct tep_format_field *field)
4836 unsigned long long val;
4837 unsigned int offset, len, i;
4838 struct tep_handle *pevent = field->event->pevent;
4840 if (field->flags & TEP_FIELD_IS_ARRAY) {
4841 offset = field->offset;
4843 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
4844 val = tep_read_number(pevent, data + offset, len);
4849 if (field->flags & TEP_FIELD_IS_STRING &&
4850 is_printable_array(data + offset, len)) {
4851 trace_seq_printf(s, "%s", (char *)data + offset);
4853 trace_seq_puts(s, "ARRAY[");
4854 for (i = 0; i < len; i++) {
4856 trace_seq_puts(s, ", ");
4857 trace_seq_printf(s, "%02x",
4858 *((unsigned char *)data + offset + i));
4860 trace_seq_putc(s, ']');
4861 field->flags &= ~TEP_FIELD_IS_STRING;
4864 val = tep_read_number(pevent, data + field->offset,
4866 if (field->flags & TEP_FIELD_IS_POINTER) {
4867 trace_seq_printf(s, "0x%llx", val);
4868 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
4869 switch (field->size) {
4872 * If field is long then print it in hex.
4873 * A long usually stores pointers.
4875 if (field->flags & TEP_FIELD_IS_LONG)
4876 trace_seq_printf(s, "0x%x", (int)val);
4878 trace_seq_printf(s, "%d", (int)val);
4881 trace_seq_printf(s, "%2d", (short)val);
4884 trace_seq_printf(s, "%1d", (char)val);
4887 trace_seq_printf(s, "%lld", val);
4890 if (field->flags & TEP_FIELD_IS_LONG)
4891 trace_seq_printf(s, "0x%llx", val);
4893 trace_seq_printf(s, "%llu", val);
4898 void tep_print_fields(struct trace_seq *s, void *data,
4899 int size __maybe_unused, struct tep_event *event)
4901 struct tep_format_field *field;
4903 field = event->format.fields;
4905 trace_seq_printf(s, " %s=", field->name);
4906 tep_print_field(s, data, field);
4907 field = field->next;
4911 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
4913 struct tep_handle *pevent = event->pevent;
4914 struct tep_print_fmt *print_fmt = &event->print_fmt;
4915 struct tep_print_arg *arg = print_fmt->args;
4916 struct tep_print_arg *args = NULL;
4917 const char *ptr = print_fmt->format;
4918 unsigned long long val;
4919 struct func_map *func;
4920 const char *saveptr;
4922 char *bprint_fmt = NULL;
4930 if (event->flags & TEP_EVENT_FL_FAILED) {
4931 trace_seq_printf(s, "[FAILED TO PARSE]");
4932 tep_print_fields(s, data, size, event);
4936 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
4937 bprint_fmt = get_bprint_format(data, size, event);
4938 args = make_bprint_args(bprint_fmt, data, size, event);
4943 for (; *ptr; ptr++) {
4949 trace_seq_putc(s, '\n');
4952 trace_seq_putc(s, '\t');
4955 trace_seq_putc(s, '\r');
4958 trace_seq_putc(s, '\\');
4961 trace_seq_putc(s, *ptr);
4965 } else if (*ptr == '%') {
4973 trace_seq_putc(s, '%');
4976 /* FIXME: need to handle properly */
4988 /* The argument is the length. */
4990 do_warning_event(event, "no argument match");
4991 event->flags |= TEP_EVENT_FL_FAILED;
4994 len_arg = eval_num_arg(data, size, event, arg);
5005 if (pevent->long_size == 4)
5010 if (isalnum(ptr[1]))
5013 if (arg->type == TEP_PRINT_BSTRING) {
5014 trace_seq_puts(s, arg->string.string);
5019 if (*ptr == 'F' || *ptr == 'f' ||
5020 *ptr == 'S' || *ptr == 's') {
5022 } else if (*ptr == 'M' || *ptr == 'm') {
5023 print_mac_arg(s, *ptr, data, size, event, arg);
5026 } else if (*ptr == 'I' || *ptr == 'i') {
5029 n = print_ip_arg(s, ptr, data, size, event, arg);
5044 do_warning_event(event, "no argument match");
5045 event->flags |= TEP_EVENT_FL_FAILED;
5049 len = ((unsigned long)ptr + 1) -
5050 (unsigned long)saveptr;
5052 /* should never happen */
5054 do_warning_event(event, "bad format!");
5055 event->flags |= TEP_EVENT_FL_FAILED;
5059 memcpy(format, saveptr, len);
5062 val = eval_num_arg(data, size, event, arg);
5066 func = find_func(pevent, val);
5068 trace_seq_puts(s, func->func);
5069 if (show_func == 'F')
5076 if (pevent->long_size == 8 && ls == 1 &&
5077 sizeof(long) != 8) {
5080 /* make %l into %ll */
5081 if (ls == 1 && (p = strchr(format, 'l')))
5082 memmove(p+1, p, strlen(p)+1);
5083 else if (strcmp(format, "%p") == 0)
5084 strcpy(format, "0x%llx");
5090 trace_seq_printf(s, format, len_arg, (char)val);
5092 trace_seq_printf(s, format, (char)val);
5096 trace_seq_printf(s, format, len_arg, (short)val);
5098 trace_seq_printf(s, format, (short)val);
5102 trace_seq_printf(s, format, len_arg, (int)val);
5104 trace_seq_printf(s, format, (int)val);
5108 trace_seq_printf(s, format, len_arg, (long)val);
5110 trace_seq_printf(s, format, (long)val);
5114 trace_seq_printf(s, format, len_arg,
5117 trace_seq_printf(s, format, (long long)val);
5120 do_warning_event(event, "bad count (%d)", ls);
5121 event->flags |= TEP_EVENT_FL_FAILED;
5126 do_warning_event(event, "no matching argument");
5127 event->flags |= TEP_EVENT_FL_FAILED;
5131 len = ((unsigned long)ptr + 1) -
5132 (unsigned long)saveptr;
5134 /* should never happen */
5136 do_warning_event(event, "bad format!");
5137 event->flags |= TEP_EVENT_FL_FAILED;
5141 memcpy(format, saveptr, len);
5145 /* Use helper trace_seq */
5147 print_str_arg(&p, data, size, event,
5148 format, len_arg, arg);
5149 trace_seq_terminate(&p);
5150 trace_seq_puts(s, p.buffer);
5151 trace_seq_destroy(&p);
5155 trace_seq_printf(s, ">%c<", *ptr);
5159 trace_seq_putc(s, *ptr);
5162 if (event->flags & TEP_EVENT_FL_FAILED) {
5164 trace_seq_printf(s, "[FAILED TO PARSE]");
5174 * tep_data_lat_fmt - parse the data for the latency format
5175 * @pevent: a handle to the pevent
5176 * @s: the trace_seq to write to
5177 * @record: the record to read from
5179 * This parses out the Latency format (interrupts disabled,
5180 * need rescheduling, in hard/soft interrupt, preempt count
5181 * and lock depth) and places it into the trace_seq.
5183 void tep_data_lat_fmt(struct tep_handle *pevent,
5184 struct trace_seq *s, struct tep_record *record)
5186 static int check_lock_depth = 1;
5187 static int check_migrate_disable = 1;
5188 static int lock_depth_exists;
5189 static int migrate_disable_exists;
5190 unsigned int lat_flags;
5193 int migrate_disable = 0;
5196 void *data = record->data;
5198 lat_flags = parse_common_flags(pevent, data);
5199 pc = parse_common_pc(pevent, data);
5200 /* lock_depth may not always exist */
5201 if (lock_depth_exists)
5202 lock_depth = parse_common_lock_depth(pevent, data);
5203 else if (check_lock_depth) {
5204 lock_depth = parse_common_lock_depth(pevent, data);
5206 check_lock_depth = 0;
5208 lock_depth_exists = 1;
5211 /* migrate_disable may not always exist */
5212 if (migrate_disable_exists)
5213 migrate_disable = parse_common_migrate_disable(pevent, data);
5214 else if (check_migrate_disable) {
5215 migrate_disable = parse_common_migrate_disable(pevent, data);
5216 if (migrate_disable < 0)
5217 check_migrate_disable = 0;
5219 migrate_disable_exists = 1;
5222 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5223 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5225 trace_seq_printf(s, "%c%c%c",
5226 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5227 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5229 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5231 (hardirq && softirq) ? 'H' :
5232 hardirq ? 'h' : softirq ? 's' : '.');
5235 trace_seq_printf(s, "%x", pc);
5237 trace_seq_putc(s, '.');
5239 if (migrate_disable_exists) {
5240 if (migrate_disable < 0)
5241 trace_seq_putc(s, '.');
5243 trace_seq_printf(s, "%d", migrate_disable);
5246 if (lock_depth_exists) {
5248 trace_seq_putc(s, '.');
5250 trace_seq_printf(s, "%d", lock_depth);
5253 trace_seq_terminate(s);
5257 * tep_data_type - parse out the given event type
5258 * @pevent: a handle to the pevent
5259 * @rec: the record to read from
5261 * This returns the event id from the @rec.
5263 int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
5265 return trace_parse_common_type(pevent, rec->data);
5269 * tep_data_pid - parse the PID from record
5270 * @pevent: a handle to the pevent
5271 * @rec: the record to parse
5273 * This returns the PID from a record.
5275 int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
5277 return parse_common_pid(pevent, rec->data);
5281 * tep_data_preempt_count - parse the preempt count from the record
5282 * @pevent: a handle to the pevent
5283 * @rec: the record to parse
5285 * This returns the preempt count from a record.
5287 int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
5289 return parse_common_pc(pevent, rec->data);
5293 * tep_data_flags - parse the latency flags from the record
5294 * @pevent: a handle to the pevent
5295 * @rec: the record to parse
5297 * This returns the latency flags from a record.
5299 * Use trace_flag_type enum for the flags (see event-parse.h).
5301 int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
5303 return parse_common_flags(pevent, rec->data);
5307 * tep_data_comm_from_pid - return the command line from PID
5308 * @pevent: a handle to the pevent
5309 * @pid: the PID of the task to search for
5311 * This returns a pointer to the command line that has the given
5314 const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
5318 comm = find_cmdline(pevent, pid);
5322 static struct tep_cmdline *
5323 pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct tep_cmdline *next)
5325 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5328 cmdlist = cmdlist->next;
5330 cmdlist = pevent->cmdlist;
5332 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5333 cmdlist = cmdlist->next;
5335 return (struct tep_cmdline *)cmdlist;
5339 * tep_data_pid_from_comm - return the pid from a given comm
5340 * @pevent: a handle to the pevent
5341 * @comm: the cmdline to find the pid from
5342 * @next: the cmdline structure to find the next comm
5344 * This returns the cmdline structure that holds a pid for a given
5345 * comm, or NULL if none found. As there may be more than one pid for
5346 * a given comm, the result of this call can be passed back into
5347 * a recurring call in the @next parameter, and then it will find the
5349 * Also, it does a linear search, so it may be slow.
5351 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
5352 struct tep_cmdline *next)
5354 struct tep_cmdline *cmdline;
5357 * If the cmdlines have not been converted yet, then use
5360 if (!pevent->cmdlines)
5361 return pid_from_cmdlist(pevent, comm, next);
5365 * The next pointer could have been still from
5366 * a previous call before cmdlines were created
5368 if (next < pevent->cmdlines ||
5369 next >= pevent->cmdlines + pevent->cmdline_count)
5376 cmdline = pevent->cmdlines;
5378 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5379 if (strcmp(cmdline->comm, comm) == 0)
5387 * tep_cmdline_pid - return the pid associated to a given cmdline
5388 * @cmdline: The cmdline structure to get the pid from
5390 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5393 int tep_cmdline_pid(struct tep_handle *pevent, struct tep_cmdline *cmdline)
5395 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5401 * If cmdlines have not been created yet, or cmdline is
5402 * not part of the array, then treat it as a cmdlist instead.
5404 if (!pevent->cmdlines ||
5405 cmdline < pevent->cmdlines ||
5406 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5407 return cmdlist->pid;
5409 return cmdline->pid;
5413 * tep_event_info - parse the data into the print format
5414 * @s: the trace_seq to write to
5415 * @event: the handle to the event
5416 * @record: the record to read from
5418 * This parses the raw @data using the given @event information and
5419 * writes the print format into the trace_seq.
5421 void tep_event_info(struct trace_seq *s, struct tep_event *event,
5422 struct tep_record *record)
5424 int print_pretty = 1;
5426 if (event->pevent->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
5427 tep_print_fields(s, record->data, record->size, event);
5430 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
5431 print_pretty = event->handler(s, record, event,
5435 pretty_print(s, record->data, record->size, event);
5438 trace_seq_terminate(s);
5441 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5443 if (!trace_clock || !use_trace_clock)
5446 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5447 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5450 /* trace_clock is setting in tsc or counter mode */
5455 * tep_find_event_by_record - return the event from a given record
5456 * @pevent: a handle to the pevent
5457 * @record: The record to get the event from
5459 * Returns the associated event for a given record, or NULL if non is
5463 tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
5467 if (record->size < 0) {
5468 do_warning("ug! negative record size %d", record->size);
5472 type = trace_parse_common_type(pevent, record->data);
5474 return tep_find_event(pevent, type);
5478 * tep_print_event_task - Write the event task comm, pid and CPU
5479 * @pevent: a handle to the pevent
5480 * @s: the trace_seq to write to
5481 * @event: the handle to the record's event
5482 * @record: The record to get the event from
5484 * Writes the tasks comm, pid and CPU to @s.
5486 void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
5487 struct tep_event *event,
5488 struct tep_record *record)
5490 void *data = record->data;
5494 pid = parse_common_pid(pevent, data);
5495 comm = find_cmdline(pevent, pid);
5497 if (pevent->latency_format) {
5498 trace_seq_printf(s, "%8.8s-%-5d %3d",
5499 comm, pid, record->cpu);
5501 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5505 * tep_print_event_time - Write the event timestamp
5506 * @pevent: a handle to the pevent
5507 * @s: the trace_seq to write to
5508 * @event: the handle to the record's event
5509 * @record: The record to get the event from
5510 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5512 * Writes the timestamp of the record into @s.
5514 void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
5515 struct tep_event *event,
5516 struct tep_record *record,
5517 bool use_trace_clock)
5520 unsigned long usecs;
5521 unsigned long nsecs;
5523 bool use_usec_format;
5525 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5527 if (use_usec_format) {
5528 secs = record->ts / NSEC_PER_SEC;
5529 nsecs = record->ts - secs * NSEC_PER_SEC;
5532 if (pevent->latency_format) {
5533 tep_data_lat_fmt(pevent, s, record);
5536 if (use_usec_format) {
5537 if (pevent->flags & TEP_NSEC_OUTPUT) {
5541 usecs = (nsecs + 500) / NSEC_PER_USEC;
5542 /* To avoid usecs larger than 1 sec */
5543 if (usecs >= USEC_PER_SEC) {
5544 usecs -= USEC_PER_SEC;
5550 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5552 trace_seq_printf(s, " %12llu:", record->ts);
5556 * tep_print_event_data - Write the event data section
5557 * @pevent: a handle to the pevent
5558 * @s: the trace_seq to write to
5559 * @event: the handle to the record's event
5560 * @record: The record to get the event from
5562 * Writes the parsing of the record's data to @s.
5564 void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
5565 struct tep_event *event,
5566 struct tep_record *record)
5568 static const char *spaces = " "; /* 20 spaces */
5571 trace_seq_printf(s, " %s: ", event->name);
5573 /* Space out the event names evenly. */
5574 len = strlen(event->name);
5576 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5578 tep_event_info(s, event, record);
5581 void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
5582 struct tep_record *record, bool use_trace_clock)
5584 struct tep_event *event;
5586 event = tep_find_event_by_record(pevent, record);
5589 int type = trace_parse_common_type(pevent, record->data);
5591 do_warning("ug! no event found for type %d", type);
5592 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5593 for (i = 0; i < record->size; i++)
5594 trace_seq_printf(s, " %02x",
5595 ((unsigned char *)record->data)[i]);
5599 tep_print_event_task(pevent, s, event, record);
5600 tep_print_event_time(pevent, s, event, record, use_trace_clock);
5601 tep_print_event_data(pevent, s, event, record);
5604 static int events_id_cmp(const void *a, const void *b)
5606 struct tep_event * const * ea = a;
5607 struct tep_event * const * eb = b;
5609 if ((*ea)->id < (*eb)->id)
5612 if ((*ea)->id > (*eb)->id)
5618 static int events_name_cmp(const void *a, const void *b)
5620 struct tep_event * const * ea = a;
5621 struct tep_event * const * eb = b;
5624 res = strcmp((*ea)->name, (*eb)->name);
5628 res = strcmp((*ea)->system, (*eb)->system);
5632 return events_id_cmp(a, b);
5635 static int events_system_cmp(const void *a, const void *b)
5637 struct tep_event * const * ea = a;
5638 struct tep_event * const * eb = b;
5641 res = strcmp((*ea)->system, (*eb)->system);
5645 res = strcmp((*ea)->name, (*eb)->name);
5649 return events_id_cmp(a, b);
5652 struct tep_event **tep_list_events(struct tep_handle *pevent, enum tep_event_sort_type sort_type)
5654 struct tep_event **events;
5655 int (*sort)(const void *a, const void *b);
5657 events = pevent->sort_events;
5659 if (events && pevent->last_type == sort_type)
5663 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5667 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5668 events[pevent->nr_events] = NULL;
5670 pevent->sort_events = events;
5672 /* the internal events are sorted by id */
5673 if (sort_type == TEP_EVENT_SORT_ID) {
5674 pevent->last_type = sort_type;
5679 switch (sort_type) {
5680 case TEP_EVENT_SORT_ID:
5681 sort = events_id_cmp;
5683 case TEP_EVENT_SORT_NAME:
5684 sort = events_name_cmp;
5686 case TEP_EVENT_SORT_SYSTEM:
5687 sort = events_system_cmp;
5693 qsort(events, pevent->nr_events, sizeof(*events), sort);
5694 pevent->last_type = sort_type;
5699 static struct tep_format_field **
5700 get_event_fields(const char *type, const char *name,
5701 int count, struct tep_format_field *list)
5703 struct tep_format_field **fields;
5704 struct tep_format_field *field;
5707 fields = malloc(sizeof(*fields) * (count + 1));
5711 for (field = list; field; field = field->next) {
5712 fields[i++] = field;
5713 if (i == count + 1) {
5714 do_warning("event %s has more %s fields than specified",
5722 do_warning("event %s has less %s fields than specified",
5731 * tep_event_common_fields - return a list of common fields for an event
5732 * @event: the event to return the common fields of.
5734 * Returns an allocated array of fields. The last item in the array is NULL.
5735 * The array must be freed with free().
5737 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
5739 return get_event_fields("common", event->name,
5740 event->format.nr_common,
5741 event->format.common_fields);
5745 * tep_event_fields - return a list of event specific fields for an event
5746 * @event: the event to return the fields of.
5748 * Returns an allocated array of fields. The last item in the array is NULL.
5749 * The array must be freed with free().
5751 struct tep_format_field **tep_event_fields(struct tep_event *event)
5753 return get_event_fields("event", event->name,
5754 event->format.nr_fields,
5755 event->format.fields);
5758 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
5760 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5762 trace_seq_puts(s, ", ");
5763 print_fields(s, field->next);
5768 static void print_args(struct tep_print_arg *args)
5770 int print_paren = 1;
5773 switch (args->type) {
5774 case TEP_PRINT_NULL:
5777 case TEP_PRINT_ATOM:
5778 printf("%s", args->atom.atom);
5780 case TEP_PRINT_FIELD:
5781 printf("REC->%s", args->field.name);
5783 case TEP_PRINT_FLAGS:
5784 printf("__print_flags(");
5785 print_args(args->flags.field);
5786 printf(", %s, ", args->flags.delim);
5788 print_fields(&s, args->flags.flags);
5789 trace_seq_do_printf(&s);
5790 trace_seq_destroy(&s);
5793 case TEP_PRINT_SYMBOL:
5794 printf("__print_symbolic(");
5795 print_args(args->symbol.field);
5798 print_fields(&s, args->symbol.symbols);
5799 trace_seq_do_printf(&s);
5800 trace_seq_destroy(&s);
5804 printf("__print_hex(");
5805 print_args(args->hex.field);
5807 print_args(args->hex.size);
5810 case TEP_PRINT_HEX_STR:
5811 printf("__print_hex_str(");
5812 print_args(args->hex.field);
5814 print_args(args->hex.size);
5817 case TEP_PRINT_INT_ARRAY:
5818 printf("__print_array(");
5819 print_args(args->int_array.field);
5821 print_args(args->int_array.count);
5823 print_args(args->int_array.el_size);
5826 case TEP_PRINT_STRING:
5827 case TEP_PRINT_BSTRING:
5828 printf("__get_str(%s)", args->string.string);
5830 case TEP_PRINT_BITMASK:
5831 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5833 case TEP_PRINT_TYPE:
5834 printf("(%s)", args->typecast.type);
5835 print_args(args->typecast.item);
5838 if (strcmp(args->op.op, ":") == 0)
5842 print_args(args->op.left);
5843 printf(" %s ", args->op.op);
5844 print_args(args->op.right);
5849 /* we should warn... */
5854 print_args(args->next);
5858 static void parse_header_field(const char *field,
5859 int *offset, int *size, int mandatory)
5861 unsigned long long save_input_buf_ptr;
5862 unsigned long long save_input_buf_siz;
5866 save_input_buf_ptr = input_buf_ptr;
5867 save_input_buf_siz = input_buf_siz;
5869 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
5871 if (read_expected(TEP_EVENT_OP, ":") < 0)
5875 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5880 * If this is not a mandatory field, then test it first.
5883 if (read_expected(TEP_EVENT_ITEM, field) < 0)
5886 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5888 if (strcmp(token, field) != 0)
5893 if (read_expected(TEP_EVENT_OP, ";") < 0)
5895 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
5897 if (read_expected(TEP_EVENT_OP, ":") < 0)
5899 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5901 *offset = atoi(token);
5903 if (read_expected(TEP_EVENT_OP, ";") < 0)
5905 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
5907 if (read_expected(TEP_EVENT_OP, ":") < 0)
5909 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5911 *size = atoi(token);
5913 if (read_expected(TEP_EVENT_OP, ";") < 0)
5915 type = read_token(&token);
5916 if (type != TEP_EVENT_NEWLINE) {
5917 /* newer versions of the kernel have a "signed" type */
5918 if (type != TEP_EVENT_ITEM)
5921 if (strcmp(token, "signed") != 0)
5926 if (read_expected(TEP_EVENT_OP, ":") < 0)
5929 if (read_expect_type(TEP_EVENT_ITEM, &token))
5933 if (read_expected(TEP_EVENT_OP, ";") < 0)
5936 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
5944 input_buf_ptr = save_input_buf_ptr;
5945 input_buf_siz = save_input_buf_siz;
5952 * tep_parse_header_page - parse the data stored in the header page
5953 * @pevent: the handle to the pevent
5954 * @buf: the buffer storing the header page format string
5955 * @size: the size of @buf
5956 * @long_size: the long size to use if there is no header
5958 * This parses the header page format for information on the
5959 * ring buffer used. The @buf should be copied from
5961 * /sys/kernel/debug/tracing/events/header_page
5963 int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
5970 * Old kernels did not have header page info.
5971 * Sorry but we just use what we find here in user space.
5973 pevent->header_page_ts_size = sizeof(long long);
5974 pevent->header_page_size_size = long_size;
5975 pevent->header_page_data_offset = sizeof(long long) + long_size;
5976 pevent->old_format = 1;
5979 init_input_buf(buf, size);
5981 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5982 &pevent->header_page_ts_size, 1);
5983 parse_header_field("commit", &pevent->header_page_size_offset,
5984 &pevent->header_page_size_size, 1);
5985 parse_header_field("overwrite", &pevent->header_page_overwrite,
5987 parse_header_field("data", &pevent->header_page_data_offset,
5988 &pevent->header_page_data_size, 1);
5993 static int event_matches(struct tep_event *event,
5994 int id, const char *sys_name,
5995 const char *event_name)
5997 if (id >= 0 && id != event->id)
6000 if (event_name && (strcmp(event_name, event->name) != 0))
6003 if (sys_name && (strcmp(sys_name, event->system) != 0))
6009 static void free_handler(struct event_handler *handle)
6011 free((void *)handle->sys_name);
6012 free((void *)handle->event_name);
6016 static int find_event_handle(struct tep_handle *pevent, struct tep_event *event)
6018 struct event_handler *handle, **next;
6020 for (next = &pevent->handlers; *next;
6021 next = &(*next)->next) {
6023 if (event_matches(event, handle->id,
6025 handle->event_name))
6032 pr_stat("overriding event (%d) %s:%s with new print handler",
6033 event->id, event->system, event->name);
6035 event->handler = handle->func;
6036 event->context = handle->context;
6038 *next = handle->next;
6039 free_handler(handle);
6045 * __tep_parse_format - parse the event format
6046 * @buf: the buffer storing the event format string
6047 * @size: the size of @buf
6048 * @sys: the system the event belongs to
6050 * This parses the event format and creates an event structure
6051 * to quickly parse raw data for a given event.
6053 * These files currently come from:
6055 * /sys/kernel/debug/tracing/events/.../.../format
6057 enum tep_errno __tep_parse_format(struct tep_event **eventp,
6058 struct tep_handle *pevent, const char *buf,
6059 unsigned long size, const char *sys)
6061 struct tep_event *event;
6064 init_input_buf(buf, size);
6066 *eventp = event = alloc_event();
6068 return TEP_ERRNO__MEM_ALLOC_FAILED;
6070 event->name = event_read_name();
6073 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6074 goto event_alloc_failed;
6077 if (strcmp(sys, "ftrace") == 0) {
6078 event->flags |= TEP_EVENT_FL_ISFTRACE;
6080 if (strcmp(event->name, "bprint") == 0)
6081 event->flags |= TEP_EVENT_FL_ISBPRINT;
6084 event->id = event_read_id();
6085 if (event->id < 0) {
6086 ret = TEP_ERRNO__READ_ID_FAILED;
6088 * This isn't an allocation error actually.
6089 * But as the ID is critical, just bail out.
6091 goto event_alloc_failed;
6094 event->system = strdup(sys);
6095 if (!event->system) {
6096 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6097 goto event_alloc_failed;
6100 /* Add pevent to event so that it can be referenced */
6101 event->pevent = pevent;
6103 ret = event_read_format(event);
6105 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6106 goto event_parse_failed;
6110 * If the event has an override, don't print warnings if the event
6111 * print format fails to parse.
6113 if (pevent && find_event_handle(pevent, event))
6116 ret = event_read_print(event);
6120 ret = TEP_ERRNO__READ_PRINT_FAILED;
6121 goto event_parse_failed;
6124 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6125 struct tep_format_field *field;
6126 struct tep_print_arg *arg, **list;
6128 /* old ftrace had no args */
6129 list = &event->print_fmt.args;
6130 for (field = event->format.fields; field; field = field->next) {
6133 event->flags |= TEP_EVENT_FL_FAILED;
6134 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6136 arg->type = TEP_PRINT_FIELD;
6137 arg->field.name = strdup(field->name);
6138 if (!arg->field.name) {
6139 event->flags |= TEP_EVENT_FL_FAILED;
6141 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6143 arg->field.field = field;
6153 event->flags |= TEP_EVENT_FL_FAILED;
6157 free(event->system);
6164 static enum tep_errno
6165 __parse_event(struct tep_handle *pevent,
6166 struct tep_event **eventp,
6167 const char *buf, unsigned long size,
6170 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
6171 struct tep_event *event = *eventp;
6176 if (pevent && add_event(pevent, event)) {
6177 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6178 goto event_add_failed;
6181 #define PRINT_ARGS 0
6182 if (PRINT_ARGS && event->print_fmt.args)
6183 print_args(event->print_fmt.args);
6188 tep_free_event(event);
6193 * tep_parse_format - parse the event format
6194 * @pevent: the handle to the pevent
6195 * @eventp: returned format
6196 * @buf: the buffer storing the event format string
6197 * @size: the size of @buf
6198 * @sys: the system the event belongs to
6200 * This parses the event format and creates an event structure
6201 * to quickly parse raw data for a given event.
6203 * These files currently come from:
6205 * /sys/kernel/debug/tracing/events/.../.../format
6207 enum tep_errno tep_parse_format(struct tep_handle *pevent,
6208 struct tep_event **eventp,
6210 unsigned long size, const char *sys)
6212 return __parse_event(pevent, eventp, buf, size, sys);
6216 * tep_parse_event - parse the event format
6217 * @pevent: the handle to the pevent
6218 * @buf: the buffer storing the event format string
6219 * @size: the size of @buf
6220 * @sys: the system the event belongs to
6222 * This parses the event format and creates an event structure
6223 * to quickly parse raw data for a given event.
6225 * These files currently come from:
6227 * /sys/kernel/debug/tracing/events/.../.../format
6229 enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
6230 unsigned long size, const char *sys)
6232 struct tep_event *event = NULL;
6233 return __parse_event(pevent, &event, buf, size, sys);
6236 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6237 const char *name, struct tep_record *record,
6238 unsigned long long *val, int err)
6242 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6246 if (tep_read_number_field(field, record->data, val)) {
6248 trace_seq_printf(s, " %s=INVALID", name);
6256 * tep_get_field_raw - return the raw pointer into the data field
6257 * @s: The seq to print to on error
6258 * @event: the event that the field is for
6259 * @name: The name of the field
6260 * @record: The record with the field name.
6261 * @len: place to store the field length.
6262 * @err: print default error if failed.
6264 * Returns a pointer into record->data of the field and places
6265 * the length of the field in @len.
6267 * On failure, it returns NULL.
6269 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6270 const char *name, struct tep_record *record,
6273 struct tep_format_field *field;
6274 void *data = record->data;
6281 field = tep_find_field(event, name);
6285 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6289 /* Allow @len to be NULL */
6293 offset = field->offset;
6294 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
6295 offset = tep_read_number(event->pevent,
6296 data + offset, field->size);
6297 *len = offset >> 16;
6302 return data + offset;
6306 * tep_get_field_val - find a field and return its value
6307 * @s: The seq to print to on error
6308 * @event: the event that the field is for
6309 * @name: The name of the field
6310 * @record: The record with the field name.
6311 * @val: place to store the value of the field.
6312 * @err: print default error if failed.
6314 * Returns 0 on success -1 on field not found.
6316 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
6317 const char *name, struct tep_record *record,
6318 unsigned long long *val, int err)
6320 struct tep_format_field *field;
6325 field = tep_find_field(event, name);
6327 return get_field_val(s, field, name, record, val, err);
6331 * tep_get_common_field_val - find a common field and return its value
6332 * @s: The seq to print to on error
6333 * @event: the event that the field is for
6334 * @name: The name of the field
6335 * @record: The record with the field name.
6336 * @val: place to store the value of the field.
6337 * @err: print default error if failed.
6339 * Returns 0 on success -1 on field not found.
6341 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
6342 const char *name, struct tep_record *record,
6343 unsigned long long *val, int err)
6345 struct tep_format_field *field;
6350 field = tep_find_common_field(event, name);
6352 return get_field_val(s, field, name, record, val, err);
6356 * tep_get_any_field_val - find a any field and return its value
6357 * @s: The seq to print to on error
6358 * @event: the event that the field is for
6359 * @name: The name of the field
6360 * @record: The record with the field name.
6361 * @val: place to store the value of the field.
6362 * @err: print default error if failed.
6364 * Returns 0 on success -1 on field not found.
6366 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
6367 const char *name, struct tep_record *record,
6368 unsigned long long *val, int err)
6370 struct tep_format_field *field;
6375 field = tep_find_any_field(event, name);
6377 return get_field_val(s, field, name, record, val, err);
6381 * tep_print_num_field - print a field and a format
6382 * @s: The seq to print to
6383 * @fmt: The printf format to print the field with.
6384 * @event: the event that the field is for
6385 * @name: The name of the field
6386 * @record: The record with the field name.
6387 * @err: print default error if failed.
6389 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6391 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6392 struct tep_event *event, const char *name,
6393 struct tep_record *record, int err)
6395 struct tep_format_field *field = tep_find_field(event, name);
6396 unsigned long long val;
6401 if (tep_read_number_field(field, record->data, &val))
6404 return trace_seq_printf(s, fmt, val);
6408 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6413 * tep_print_func_field - print a field and a format for function pointers
6414 * @s: The seq to print to
6415 * @fmt: The printf format to print the field with.
6416 * @event: the event that the field is for
6417 * @name: The name of the field
6418 * @record: The record with the field name.
6419 * @err: print default error if failed.
6421 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6423 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6424 struct tep_event *event, const char *name,
6425 struct tep_record *record, int err)
6427 struct tep_format_field *field = tep_find_field(event, name);
6428 struct tep_handle *pevent = event->pevent;
6429 unsigned long long val;
6430 struct func_map *func;
6436 if (tep_read_number_field(field, record->data, &val))
6439 func = find_func(pevent, val);
6442 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6444 sprintf(tmp, "0x%08llx", val);
6446 return trace_seq_printf(s, fmt, tmp);
6450 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6454 static void free_func_handle(struct tep_function_handler *func)
6456 struct func_params *params;
6460 while (func->params) {
6461 params = func->params;
6462 func->params = params->next;
6470 * tep_register_print_function - register a helper function
6471 * @pevent: the handle to the pevent
6472 * @func: the function to process the helper function
6473 * @ret_type: the return type of the helper function
6474 * @name: the name of the helper function
6475 * @parameters: A list of enum tep_func_arg_type
6477 * Some events may have helper functions in the print format arguments.
6478 * This allows a plugin to dynamically create a way to process one
6479 * of these functions.
6481 * The @parameters is a variable list of tep_func_arg_type enums that
6482 * must end with TEP_FUNC_ARG_VOID.
6484 int tep_register_print_function(struct tep_handle *pevent,
6485 tep_func_handler func,
6486 enum tep_func_arg_type ret_type,
6489 struct tep_function_handler *func_handle;
6490 struct func_params **next_param;
6491 struct func_params *param;
6492 enum tep_func_arg_type type;
6496 func_handle = find_func_handler(pevent, name);
6499 * This is most like caused by the users own
6500 * plugins updating the function. This overrides the
6503 pr_stat("override of function helper '%s'", name);
6504 remove_func_handler(pevent, name);
6507 func_handle = calloc(1, sizeof(*func_handle));
6509 do_warning("Failed to allocate function handler");
6510 return TEP_ERRNO__MEM_ALLOC_FAILED;
6513 func_handle->ret_type = ret_type;
6514 func_handle->name = strdup(name);
6515 func_handle->func = func;
6516 if (!func_handle->name) {
6517 do_warning("Failed to allocate function name");
6519 return TEP_ERRNO__MEM_ALLOC_FAILED;
6522 next_param = &(func_handle->params);
6525 type = va_arg(ap, enum tep_func_arg_type);
6526 if (type == TEP_FUNC_ARG_VOID)
6529 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6530 do_warning("Invalid argument type %d", type);
6531 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6535 param = malloc(sizeof(*param));
6537 do_warning("Failed to allocate function param");
6538 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6544 *next_param = param;
6545 next_param = &(param->next);
6547 func_handle->nr_args++;
6551 func_handle->next = pevent->func_handlers;
6552 pevent->func_handlers = func_handle;
6557 free_func_handle(func_handle);
6562 * tep_unregister_print_function - unregister a helper function
6563 * @pevent: the handle to the pevent
6564 * @func: the function to process the helper function
6565 * @name: the name of the helper function
6567 * This function removes existing print handler for function @name.
6569 * Returns 0 if the handler was removed successully, -1 otherwise.
6571 int tep_unregister_print_function(struct tep_handle *pevent,
6572 tep_func_handler func, char *name)
6574 struct tep_function_handler *func_handle;
6576 func_handle = find_func_handler(pevent, name);
6577 if (func_handle && func_handle->func == func) {
6578 remove_func_handler(pevent, name);
6584 static struct tep_event *search_event(struct tep_handle *pevent, int id,
6585 const char *sys_name,
6586 const char *event_name)
6588 struct tep_event *event;
6592 event = tep_find_event(pevent, id);
6595 if (event_name && (strcmp(event_name, event->name) != 0))
6597 if (sys_name && (strcmp(sys_name, event->system) != 0))
6600 event = tep_find_event_by_name(pevent, sys_name, event_name);
6608 * tep_register_event_handler - register a way to parse an event
6609 * @pevent: the handle to the pevent
6610 * @id: the id of the event to register
6611 * @sys_name: the system name the event belongs to
6612 * @event_name: the name of the event
6613 * @func: the function to call to parse the event information
6614 * @context: the data to be passed to @func
6616 * This function allows a developer to override the parsing of
6617 * a given event. If for some reason the default print format
6618 * is not sufficient, this function will register a function
6619 * for an event to be used to parse the data instead.
6621 * If @id is >= 0, then it is used to find the event.
6622 * else @sys_name and @event_name are used.
6625 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
6626 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
6627 * negative TEP_ERRNO_... in case of an error
6630 int tep_register_event_handler(struct tep_handle *pevent, int id,
6631 const char *sys_name, const char *event_name,
6632 tep_event_handler_func func, void *context)
6634 struct tep_event *event;
6635 struct event_handler *handle;
6637 event = search_event(pevent, id, sys_name, event_name);
6641 pr_stat("overriding event (%d) %s:%s with new print handler",
6642 event->id, event->system, event->name);
6644 event->handler = func;
6645 event->context = context;
6646 return TEP_REGISTER_SUCCESS_OVERWRITE;
6649 /* Save for later use. */
6650 handle = calloc(1, sizeof(*handle));
6652 do_warning("Failed to allocate event handler");
6653 return TEP_ERRNO__MEM_ALLOC_FAILED;
6658 handle->event_name = strdup(event_name);
6660 handle->sys_name = strdup(sys_name);
6662 if ((event_name && !handle->event_name) ||
6663 (sys_name && !handle->sys_name)) {
6664 do_warning("Failed to allocate event/sys name");
6665 free((void *)handle->event_name);
6666 free((void *)handle->sys_name);
6668 return TEP_ERRNO__MEM_ALLOC_FAILED;
6671 handle->func = func;
6672 handle->next = pevent->handlers;
6673 pevent->handlers = handle;
6674 handle->context = context;
6676 return TEP_REGISTER_SUCCESS;
6679 static int handle_matches(struct event_handler *handler, int id,
6680 const char *sys_name, const char *event_name,
6681 tep_event_handler_func func, void *context)
6683 if (id >= 0 && id != handler->id)
6686 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6689 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6692 if (func != handler->func || context != handler->context)
6699 * tep_unregister_event_handler - unregister an existing event handler
6700 * @pevent: the handle to the pevent
6701 * @id: the id of the event to unregister
6702 * @sys_name: the system name the handler belongs to
6703 * @event_name: the name of the event handler
6704 * @func: the function to call to parse the event information
6705 * @context: the data to be passed to @func
6707 * This function removes existing event handler (parser).
6709 * If @id is >= 0, then it is used to find the event.
6710 * else @sys_name and @event_name are used.
6712 * Returns 0 if handler was removed successfully, -1 if event was not found.
6714 int tep_unregister_event_handler(struct tep_handle *pevent, int id,
6715 const char *sys_name, const char *event_name,
6716 tep_event_handler_func func, void *context)
6718 struct tep_event *event;
6719 struct event_handler *handle;
6720 struct event_handler **next;
6722 event = search_event(pevent, id, sys_name, event_name);
6726 if (event->handler == func && event->context == context) {
6727 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6728 event->id, event->system, event->name);
6730 event->handler = NULL;
6731 event->context = NULL;
6736 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6738 if (handle_matches(handle, id, sys_name, event_name,
6746 *next = handle->next;
6747 free_handler(handle);
6753 * tep_alloc - create a pevent handle
6755 struct tep_handle *tep_alloc(void)
6757 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
6760 pevent->ref_count = 1;
6761 pevent->host_bigendian = tep_host_bigendian();
6767 void tep_ref(struct tep_handle *pevent)
6769 pevent->ref_count++;
6772 int tep_get_ref(struct tep_handle *tep)
6775 return tep->ref_count;
6779 void tep_free_format_field(struct tep_format_field *field)
6782 if (field->alias != field->name)
6788 static void free_format_fields(struct tep_format_field *field)
6790 struct tep_format_field *next;
6794 tep_free_format_field(field);
6799 static void free_formats(struct tep_format *format)
6801 free_format_fields(format->common_fields);
6802 free_format_fields(format->fields);
6805 void tep_free_event(struct tep_event *event)
6808 free(event->system);
6810 free_formats(&event->format);
6812 free(event->print_fmt.format);
6813 free_args(event->print_fmt.args);
6819 * tep_free - free a pevent handle
6820 * @pevent: the pevent handle to free
6822 void tep_free(struct tep_handle *pevent)
6824 struct cmdline_list *cmdlist, *cmdnext;
6825 struct func_list *funclist, *funcnext;
6826 struct printk_list *printklist, *printknext;
6827 struct tep_function_handler *func_handler;
6828 struct event_handler *handle;
6834 cmdlist = pevent->cmdlist;
6835 funclist = pevent->funclist;
6836 printklist = pevent->printklist;
6838 pevent->ref_count--;
6839 if (pevent->ref_count)
6842 if (pevent->cmdlines) {
6843 for (i = 0; i < pevent->cmdline_count; i++)
6844 free(pevent->cmdlines[i].comm);
6845 free(pevent->cmdlines);
6849 cmdnext = cmdlist->next;
6850 free(cmdlist->comm);
6855 if (pevent->func_map) {
6856 for (i = 0; i < (int)pevent->func_count; i++) {
6857 free(pevent->func_map[i].func);
6858 free(pevent->func_map[i].mod);
6860 free(pevent->func_map);
6864 funcnext = funclist->next;
6865 free(funclist->func);
6866 free(funclist->mod);
6868 funclist = funcnext;
6871 while (pevent->func_handlers) {
6872 func_handler = pevent->func_handlers;
6873 pevent->func_handlers = func_handler->next;
6874 free_func_handle(func_handler);
6877 if (pevent->printk_map) {
6878 for (i = 0; i < (int)pevent->printk_count; i++)
6879 free(pevent->printk_map[i].printk);
6880 free(pevent->printk_map);
6883 while (printklist) {
6884 printknext = printklist->next;
6885 free(printklist->printk);
6887 printklist = printknext;
6890 for (i = 0; i < pevent->nr_events; i++)
6891 tep_free_event(pevent->events[i]);
6893 while (pevent->handlers) {
6894 handle = pevent->handlers;
6895 pevent->handlers = handle->next;
6896 free_handler(handle);
6899 free(pevent->trace_clock);
6900 free(pevent->events);
6901 free(pevent->sort_events);
6902 free(pevent->func_resolver);
6907 void tep_unref(struct tep_handle *pevent)