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 cmdline *ca = a;
135 const struct 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 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 cmdline *comm;
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 cmdline *comm;
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, const char *comm, int pid)
237 struct cmdline *cmdlines = pevent->cmdlines;
238 const struct cmdline *cmdline;
244 /* avoid duplicates */
247 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
248 sizeof(*pevent->cmdlines), cmdline_cmp);
254 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
260 cmdlines[pevent->cmdline_count].comm = strdup(comm);
261 if (!cmdlines[pevent->cmdline_count].comm) {
267 cmdlines[pevent->cmdline_count].pid = pid;
269 if (cmdlines[pevent->cmdline_count].comm)
270 pevent->cmdline_count++;
272 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
273 pevent->cmdlines = cmdlines;
279 * tep_register_comm - register a pid / comm mapping
280 * @pevent: handle for the pevent
281 * @comm: the command line to register
282 * @pid: the pid to map the command line to
284 * This adds a mapping to search for command line names with
285 * a given pid. The comm is duplicated.
287 int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
289 struct cmdline_list *item;
291 if (pevent->cmdlines)
292 return add_new_comm(pevent, comm, pid);
294 item = malloc(sizeof(*item));
299 item->comm = strdup(comm);
301 item->comm = strdup("<...>");
307 item->next = pevent->cmdlist;
309 pevent->cmdlist = item;
310 pevent->cmdline_count++;
315 int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
317 pevent->trace_clock = strdup(trace_clock);
318 if (!pevent->trace_clock) {
326 unsigned long long addr;
332 struct func_list *next;
333 unsigned long long addr;
338 static int func_cmp(const void *a, const void *b)
340 const struct func_map *fa = a;
341 const struct func_map *fb = b;
343 if (fa->addr < fb->addr)
345 if (fa->addr > fb->addr)
352 * We are searching for a record in between, not an exact
355 static int func_bcmp(const void *a, const void *b)
357 const struct func_map *fa = a;
358 const struct func_map *fb = b;
360 if ((fa->addr == fb->addr) ||
362 (fa->addr > fb->addr &&
363 fa->addr < (fb+1)->addr))
366 if (fa->addr < fb->addr)
372 static int func_map_init(struct tep_handle *pevent)
374 struct func_list *funclist;
375 struct func_list *item;
376 struct func_map *func_map;
379 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
383 funclist = pevent->funclist;
387 func_map[i].func = funclist->func;
388 func_map[i].addr = funclist->addr;
389 func_map[i].mod = funclist->mod;
392 funclist = funclist->next;
396 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
399 * Add a special record at the end.
401 func_map[pevent->func_count].func = NULL;
402 func_map[pevent->func_count].addr = 0;
403 func_map[pevent->func_count].mod = NULL;
405 pevent->func_map = func_map;
406 pevent->funclist = NULL;
411 static struct func_map *
412 __find_func(struct tep_handle *pevent, unsigned long long addr)
414 struct func_map *func;
417 if (!pevent->func_map)
418 func_map_init(pevent);
422 func = bsearch(&key, pevent->func_map, pevent->func_count,
423 sizeof(*pevent->func_map), func_bcmp);
428 struct func_resolver {
429 tep_func_resolver_t *func;
435 * tep_set_function_resolver - set an alternative function resolver
436 * @pevent: handle for the pevent
437 * @resolver: function to be used
438 * @priv: resolver function private state.
440 * Some tools may have already a way to resolve kernel functions, allow them to
441 * keep using it instead of duplicating all the entries inside
444 int tep_set_function_resolver(struct tep_handle *pevent,
445 tep_func_resolver_t *func, void *priv)
447 struct func_resolver *resolver = malloc(sizeof(*resolver));
449 if (resolver == NULL)
452 resolver->func = func;
453 resolver->priv = priv;
455 free(pevent->func_resolver);
456 pevent->func_resolver = resolver;
462 * tep_reset_function_resolver - reset alternative function resolver
463 * @pevent: handle for the pevent
465 * Stop using whatever alternative resolver was set, use the default
468 void tep_reset_function_resolver(struct tep_handle *pevent)
470 free(pevent->func_resolver);
471 pevent->func_resolver = NULL;
474 static struct func_map *
475 find_func(struct tep_handle *pevent, unsigned long long addr)
477 struct func_map *map;
479 if (!pevent->func_resolver)
480 return __find_func(pevent, addr);
482 map = &pevent->func_resolver->map;
485 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
486 &map->addr, &map->mod);
487 if (map->func == NULL)
494 * tep_find_function - find a function by a given address
495 * @pevent: handle for the pevent
496 * @addr: the address to find the function with
498 * Returns a pointer to the function stored that has the given
499 * address. Note, the address does not have to be exact, it
500 * will select the function that would contain the address.
502 const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
504 struct func_map *map;
506 map = find_func(pevent, addr);
514 * tep_find_function_address - find a function address by a given address
515 * @pevent: handle for the pevent
516 * @addr: the address to find the function with
518 * Returns the address the function starts at. This can be used in
519 * conjunction with tep_find_function to print both the function
520 * name and the function offset.
523 tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
525 struct func_map *map;
527 map = find_func(pevent, addr);
535 * tep_register_function - register a function with a given address
536 * @pevent: handle for the pevent
537 * @function: the function name to register
538 * @addr: the address the function starts at
539 * @mod: the kernel module the function may be in (NULL for none)
541 * This registers a function name with an address and module.
542 * The @func passed in is duplicated.
544 int tep_register_function(struct tep_handle *pevent, char *func,
545 unsigned long long addr, char *mod)
547 struct func_list *item = malloc(sizeof(*item));
552 item->next = pevent->funclist;
553 item->func = strdup(func);
558 item->mod = strdup(mod);
565 pevent->funclist = item;
566 pevent->func_count++;
580 * tep_print_funcs - print out the stored functions
581 * @pevent: handle for the pevent
583 * This prints out the stored functions.
585 void tep_print_funcs(struct tep_handle *pevent)
589 if (!pevent->func_map)
590 func_map_init(pevent);
592 for (i = 0; i < (int)pevent->func_count; i++) {
594 pevent->func_map[i].addr,
595 pevent->func_map[i].func);
596 if (pevent->func_map[i].mod)
597 printf(" [%s]\n", pevent->func_map[i].mod);
604 unsigned long long addr;
609 struct printk_list *next;
610 unsigned long long addr;
614 static int printk_cmp(const void *a, const void *b)
616 const struct printk_map *pa = a;
617 const struct printk_map *pb = b;
619 if (pa->addr < pb->addr)
621 if (pa->addr > pb->addr)
627 static int printk_map_init(struct tep_handle *pevent)
629 struct printk_list *printklist;
630 struct printk_list *item;
631 struct printk_map *printk_map;
634 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
638 printklist = pevent->printklist;
642 printk_map[i].printk = printklist->printk;
643 printk_map[i].addr = printklist->addr;
646 printklist = printklist->next;
650 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
652 pevent->printk_map = printk_map;
653 pevent->printklist = NULL;
658 static struct printk_map *
659 find_printk(struct tep_handle *pevent, unsigned long long addr)
661 struct printk_map *printk;
662 struct printk_map key;
664 if (!pevent->printk_map && printk_map_init(pevent))
669 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
670 sizeof(*pevent->printk_map), printk_cmp);
676 * tep_register_print_string - register a string by its address
677 * @pevent: handle for the pevent
678 * @fmt: the string format to register
679 * @addr: the address the string was located at
681 * This registers a string by the address it was stored in the kernel.
682 * The @fmt passed in is duplicated.
684 int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
685 unsigned long long addr)
687 struct printk_list *item = malloc(sizeof(*item));
693 item->next = pevent->printklist;
696 /* Strip off quotes and '\n' from the end */
699 item->printk = strdup(fmt);
703 p = item->printk + strlen(item->printk) - 1;
708 if (strcmp(p, "\\n") == 0)
711 pevent->printklist = item;
712 pevent->printk_count++;
723 * tep_print_printk - print out the stored strings
724 * @pevent: handle for the pevent
726 * This prints the string formats that were stored.
728 void tep_print_printk(struct tep_handle *pevent)
732 if (!pevent->printk_map)
733 printk_map_init(pevent);
735 for (i = 0; i < (int)pevent->printk_count; i++) {
736 printf("%016llx %s\n",
737 pevent->printk_map[i].addr,
738 pevent->printk_map[i].printk);
742 static struct tep_event *alloc_event(void)
744 return calloc(1, sizeof(struct tep_event));
747 static int add_event(struct tep_handle *pevent, struct tep_event *event)
750 struct tep_event **events = realloc(pevent->events, sizeof(event) *
751 (pevent->nr_events + 1));
755 pevent->events = events;
757 for (i = 0; i < pevent->nr_events; i++) {
758 if (pevent->events[i]->id > event->id)
761 if (i < pevent->nr_events)
762 memmove(&pevent->events[i + 1],
764 sizeof(event) * (pevent->nr_events - i));
766 pevent->events[i] = event;
769 event->pevent = pevent;
774 static int event_item_type(enum tep_event_type type)
777 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
779 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
785 static void free_flag_sym(struct tep_print_flag_sym *fsym)
787 struct tep_print_flag_sym *next;
798 static void free_arg(struct tep_print_arg *arg)
800 struct tep_print_arg *farg;
807 free(arg->atom.atom);
809 case TEP_PRINT_FIELD:
810 free(arg->field.name);
812 case TEP_PRINT_FLAGS:
813 free_arg(arg->flags.field);
814 free(arg->flags.delim);
815 free_flag_sym(arg->flags.flags);
817 case TEP_PRINT_SYMBOL:
818 free_arg(arg->symbol.field);
819 free_flag_sym(arg->symbol.symbols);
822 case TEP_PRINT_HEX_STR:
823 free_arg(arg->hex.field);
824 free_arg(arg->hex.size);
826 case TEP_PRINT_INT_ARRAY:
827 free_arg(arg->int_array.field);
828 free_arg(arg->int_array.count);
829 free_arg(arg->int_array.el_size);
832 free(arg->typecast.type);
833 free_arg(arg->typecast.item);
835 case TEP_PRINT_STRING:
836 case TEP_PRINT_BSTRING:
837 free(arg->string.string);
839 case TEP_PRINT_BITMASK:
840 free(arg->bitmask.bitmask);
842 case TEP_PRINT_DYNAMIC_ARRAY:
843 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
844 free(arg->dynarray.index);
848 free_arg(arg->op.left);
849 free_arg(arg->op.right);
852 while (arg->func.args) {
853 farg = arg->func.args;
854 arg->func.args = farg->next;
867 static enum tep_event_type get_type(int ch)
870 return TEP_EVENT_NEWLINE;
872 return TEP_EVENT_SPACE;
873 if (isalnum(ch) || ch == '_')
874 return TEP_EVENT_ITEM;
876 return TEP_EVENT_SQUOTE;
878 return TEP_EVENT_DQUOTE;
880 return TEP_EVENT_NONE;
881 if (ch == '(' || ch == ')' || ch == ',')
882 return TEP_EVENT_DELIM;
887 static int __read_char(void)
889 if (input_buf_ptr >= input_buf_siz)
892 return input_buf[input_buf_ptr++];
895 static int __peek_char(void)
897 if (input_buf_ptr >= input_buf_siz)
900 return input_buf[input_buf_ptr];
904 * tep_peek_char - peek at the next character that will be read
906 * Returns the next character read, or -1 if end of buffer.
908 int tep_peek_char(void)
910 return __peek_char();
913 static int extend_token(char **tok, char *buf, int size)
915 char *newtok = realloc(*tok, size);
932 static enum tep_event_type force_token(const char *str, char **tok);
934 static enum tep_event_type __read_token(char **tok)
937 int ch, last_ch, quote_ch, next_ch;
940 enum tep_event_type type;
947 return TEP_EVENT_NONE;
950 if (type == TEP_EVENT_NONE)
956 case TEP_EVENT_NEWLINE:
957 case TEP_EVENT_DELIM:
958 if (asprintf(tok, "%c", ch) < 0)
959 return TEP_EVENT_ERROR;
966 next_ch = __peek_char();
967 if (next_ch == '>') {
968 buf[i++] = __read_char();
981 buf[i++] = __read_char();
993 default: /* what should we do instead? */
1003 buf[i++] = __read_char();
1006 case TEP_EVENT_DQUOTE:
1007 case TEP_EVENT_SQUOTE:
1008 /* don't keep quotes */
1014 if (i == (BUFSIZ - 1)) {
1018 if (extend_token(tok, buf, tok_size) < 0)
1019 return TEP_EVENT_NONE;
1025 /* the '\' '\' will cancel itself */
1026 if (ch == '\\' && last_ch == '\\')
1028 } while (ch != quote_ch || last_ch == '\\');
1029 /* remove the last quote */
1033 * For strings (double quotes) check the next token.
1034 * If it is another string, concatinate the two.
1036 if (type == TEP_EVENT_DQUOTE) {
1037 unsigned long long save_input_buf_ptr = input_buf_ptr;
1041 } while (isspace(ch));
1044 input_buf_ptr = save_input_buf_ptr;
1049 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1050 case TEP_EVENT_ITEM:
1055 while (get_type(__peek_char()) == type) {
1056 if (i == (BUFSIZ - 1)) {
1060 if (extend_token(tok, buf, tok_size) < 0)
1061 return TEP_EVENT_NONE;
1070 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1071 return TEP_EVENT_NONE;
1073 if (type == TEP_EVENT_ITEM) {
1075 * Older versions of the kernel has a bug that
1076 * creates invalid symbols and will break the mac80211
1077 * parsing. This is a work around to that bug.
1079 * See Linux kernel commit:
1080 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1082 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1085 return force_token("\"%s\" ", tok);
1086 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1089 return force_token("\" sta:%pM\" ", tok);
1090 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1093 return force_token("\" vif:%p(%d)\" ", tok);
1100 static enum tep_event_type force_token(const char *str, char **tok)
1102 const char *save_input_buf;
1103 unsigned long long save_input_buf_ptr;
1104 unsigned long long save_input_buf_siz;
1105 enum tep_event_type type;
1107 /* save off the current input pointers */
1108 save_input_buf = input_buf;
1109 save_input_buf_ptr = input_buf_ptr;
1110 save_input_buf_siz = input_buf_siz;
1112 init_input_buf(str, strlen(str));
1114 type = __read_token(tok);
1116 /* reset back to original token */
1117 input_buf = save_input_buf;
1118 input_buf_ptr = save_input_buf_ptr;
1119 input_buf_siz = save_input_buf_siz;
1124 static void free_token(char *tok)
1130 static enum tep_event_type read_token(char **tok)
1132 enum tep_event_type type;
1135 type = __read_token(tok);
1136 if (type != TEP_EVENT_SPACE)
1144 return TEP_EVENT_NONE;
1148 * tep_read_token - access to utilities to use the pevent parser
1149 * @tok: The token to return
1151 * This will parse tokens from the string given by
1154 * Returns the token type.
1156 enum tep_event_type tep_read_token(char **tok)
1158 return read_token(tok);
1162 * tep_free_token - free a token returned by tep_read_token
1163 * @token: the token to free
1165 void tep_free_token(char *token)
1171 static enum tep_event_type read_token_item(char **tok)
1173 enum tep_event_type type;
1176 type = __read_token(tok);
1177 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1185 return TEP_EVENT_NONE;
1188 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1190 if (type != expect) {
1191 do_warning("Error: expected type %d but read %d",
1198 static int test_type_token(enum tep_event_type type, const char *token,
1199 enum tep_event_type expect, const char *expect_tok)
1201 if (type != expect) {
1202 do_warning("Error: expected type %d but read %d",
1207 if (strcmp(token, expect_tok) != 0) {
1208 do_warning("Error: expected '%s' but read '%s'",
1215 static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
1217 enum tep_event_type type;
1220 type = read_token(tok);
1222 type = read_token_item(tok);
1223 return test_type(type, expect);
1226 static int read_expect_type(enum tep_event_type expect, char **tok)
1228 return __read_expect_type(expect, tok, 1);
1231 static int __read_expected(enum tep_event_type expect, const char *str,
1234 enum tep_event_type type;
1239 type = read_token(&token);
1241 type = read_token_item(&token);
1243 ret = test_type_token(type, token, expect, str);
1250 static int read_expected(enum tep_event_type expect, const char *str)
1252 return __read_expected(expect, str, 1);
1255 static int read_expected_item(enum tep_event_type expect, const char *str)
1257 return __read_expected(expect, str, 0);
1260 static char *event_read_name(void)
1264 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
1267 if (read_expected(TEP_EVENT_OP, ":") < 0)
1270 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1280 static int event_read_id(void)
1285 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
1288 if (read_expected(TEP_EVENT_OP, ":") < 0)
1291 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1294 id = strtoul(token, NULL, 0);
1303 static int field_is_string(struct tep_format_field *field)
1305 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1306 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1307 strstr(field->type, "s8")))
1313 static int field_is_dynamic(struct tep_format_field *field)
1315 if (strncmp(field->type, "__data_loc", 10) == 0)
1321 static int field_is_long(struct tep_format_field *field)
1323 /* includes long long */
1324 if (strstr(field->type, "long"))
1330 static unsigned int type_size(const char *name)
1332 /* This covers all TEP_FIELD_IS_STRING types. */
1350 for (i = 0; table[i].type; i++) {
1351 if (!strcmp(table[i].type, name))
1352 return table[i].size;
1358 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1360 struct tep_format_field *field = NULL;
1361 enum tep_event_type type;
1367 unsigned int size_dynamic = 0;
1369 type = read_token(&token);
1370 if (type == TEP_EVENT_NEWLINE) {
1377 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1381 type = read_token(&token);
1383 * The ftrace fields may still use the "special" name.
1386 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1387 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1389 type = read_token(&token);
1392 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1396 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1401 field = calloc(1, sizeof(*field));
1405 field->event = event;
1407 /* read the rest of the type */
1409 type = read_token(&token);
1410 if (type == TEP_EVENT_ITEM ||
1411 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1413 * Some of the ftrace fields are broken and have
1414 * an illegal "." in them.
1416 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1417 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1419 if (strcmp(token, "*") == 0)
1420 field->flags |= TEP_FIELD_IS_POINTER;
1424 new_type = realloc(field->type,
1425 strlen(field->type) +
1426 strlen(last_token) + 2);
1431 field->type = new_type;
1432 strcat(field->type, " ");
1433 strcat(field->type, last_token);
1436 field->type = last_token;
1445 do_warning_event(event, "%s: no type found", __func__);
1448 field->name = field->alias = last_token;
1450 if (test_type(type, TEP_EVENT_OP))
1453 if (strcmp(token, "[") == 0) {
1454 enum tep_event_type last_type = type;
1455 char *brackets = token;
1459 field->flags |= TEP_FIELD_IS_ARRAY;
1461 type = read_token(&token);
1463 if (type == TEP_EVENT_ITEM)
1464 field->arraylen = strtoul(token, NULL, 0);
1466 field->arraylen = 0;
1468 while (strcmp(token, "]") != 0) {
1469 if (last_type == TEP_EVENT_ITEM &&
1470 type == TEP_EVENT_ITEM)
1476 new_brackets = realloc(brackets,
1478 strlen(token) + len);
1479 if (!new_brackets) {
1483 brackets = new_brackets;
1485 strcat(brackets, " ");
1486 strcat(brackets, token);
1487 /* We only care about the last token */
1488 field->arraylen = strtoul(token, NULL, 0);
1490 type = read_token(&token);
1491 if (type == TEP_EVENT_NONE) {
1492 do_warning_event(event, "failed to find token");
1499 new_brackets = realloc(brackets, strlen(brackets) + 2);
1500 if (!new_brackets) {
1504 brackets = new_brackets;
1505 strcat(brackets, "]");
1507 /* add brackets to type */
1509 type = read_token(&token);
1511 * If the next token is not an OP, then it is of
1512 * the format: type [] item;
1514 if (type == TEP_EVENT_ITEM) {
1516 new_type = realloc(field->type,
1517 strlen(field->type) +
1518 strlen(field->name) +
1519 strlen(brackets) + 2);
1524 field->type = new_type;
1525 strcat(field->type, " ");
1526 strcat(field->type, field->name);
1527 size_dynamic = type_size(field->name);
1528 free_token(field->name);
1529 strcat(field->type, brackets);
1530 field->name = field->alias = token;
1531 type = read_token(&token);
1534 new_type = realloc(field->type,
1535 strlen(field->type) +
1536 strlen(brackets) + 1);
1541 field->type = new_type;
1542 strcat(field->type, brackets);
1547 if (field_is_string(field))
1548 field->flags |= TEP_FIELD_IS_STRING;
1549 if (field_is_dynamic(field))
1550 field->flags |= TEP_FIELD_IS_DYNAMIC;
1551 if (field_is_long(field))
1552 field->flags |= TEP_FIELD_IS_LONG;
1554 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1558 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1561 if (read_expected(TEP_EVENT_OP, ":") < 0)
1564 if (read_expect_type(TEP_EVENT_ITEM, &token))
1566 field->offset = strtoul(token, NULL, 0);
1569 if (read_expected(TEP_EVENT_OP, ";") < 0)
1572 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1575 if (read_expected(TEP_EVENT_OP, ":") < 0)
1578 if (read_expect_type(TEP_EVENT_ITEM, &token))
1580 field->size = strtoul(token, NULL, 0);
1583 if (read_expected(TEP_EVENT_OP, ";") < 0)
1586 type = read_token(&token);
1587 if (type != TEP_EVENT_NEWLINE) {
1588 /* newer versions of the kernel have a "signed" type */
1589 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1594 if (read_expected(TEP_EVENT_OP, ":") < 0)
1597 if (read_expect_type(TEP_EVENT_ITEM, &token))
1600 if (strtoul(token, NULL, 0))
1601 field->flags |= TEP_FIELD_IS_SIGNED;
1604 if (read_expected(TEP_EVENT_OP, ";") < 0)
1607 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1613 if (field->flags & TEP_FIELD_IS_ARRAY) {
1614 if (field->arraylen)
1615 field->elementsize = field->size / field->arraylen;
1616 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1617 field->elementsize = size_dynamic;
1618 else if (field->flags & TEP_FIELD_IS_STRING)
1619 field->elementsize = 1;
1620 else if (field->flags & TEP_FIELD_IS_LONG)
1621 field->elementsize = event->pevent ?
1622 event->pevent->long_size :
1625 field->elementsize = field->size;
1628 fields = &field->next;
1645 static int event_read_format(struct tep_event *event)
1650 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1653 if (read_expected(TEP_EVENT_OP, ":") < 0)
1656 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1660 ret = event_read_fields(event, &event->format.common_fields);
1663 event->format.nr_common = ret;
1665 ret = event_read_fields(event, &event->format.fields);
1668 event->format.nr_fields = ret;
1677 static enum tep_event_type
1678 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1679 char **tok, enum tep_event_type type);
1681 static enum tep_event_type
1682 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1684 enum tep_event_type type;
1687 type = read_token(&token);
1690 return process_arg_token(event, arg, tok, type);
1693 static enum tep_event_type
1694 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1697 * For __print_symbolic() and __print_flags, we need to completely
1698 * evaluate the first argument, which defines what to print next.
1700 static enum tep_event_type
1701 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1703 enum tep_event_type type;
1705 type = process_arg(event, arg, tok);
1707 while (type == TEP_EVENT_OP) {
1708 type = process_op(event, arg, tok);
1714 static enum tep_event_type
1715 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1717 struct tep_print_arg *arg, *left, *right;
1718 enum tep_event_type type;
1723 right = alloc_arg();
1725 if (!arg || !left || !right) {
1726 do_warning_event(event, "%s: not enough memory!", __func__);
1727 /* arg will be freed at out_free */
1733 arg->type = TEP_PRINT_OP;
1734 arg->op.left = left;
1735 arg->op.right = right;
1738 type = process_arg(event, left, &token);
1741 if (type == TEP_EVENT_ERROR)
1744 /* Handle other operations in the arguments */
1745 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1746 type = process_op(event, left, &token);
1750 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1755 type = process_arg(event, right, &token);
1757 top->op.right = arg;
1763 /* Top may point to itself */
1764 top->op.right = NULL;
1767 return TEP_EVENT_ERROR;
1770 static enum tep_event_type
1771 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1773 struct tep_print_arg *arg;
1774 enum tep_event_type type;
1779 do_warning_event(event, "%s: not enough memory!", __func__);
1780 /* '*tok' is set to top->op.op. No need to free. */
1782 return TEP_EVENT_ERROR;
1786 type = process_arg(event, arg, &token);
1787 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1790 top->op.right = arg;
1793 type = read_token_item(&token);
1801 return TEP_EVENT_ERROR;
1804 static int get_op_prio(char *op)
1818 /* '>>' and '<<' are 8 */
1822 /* '==' and '!=' are 10 */
1832 do_warning("unknown op '%c'", op[0]);
1836 if (strcmp(op, "++") == 0 ||
1837 strcmp(op, "--") == 0) {
1839 } else if (strcmp(op, ">>") == 0 ||
1840 strcmp(op, "<<") == 0) {
1842 } else if (strcmp(op, ">=") == 0 ||
1843 strcmp(op, "<=") == 0) {
1845 } else if (strcmp(op, "==") == 0 ||
1846 strcmp(op, "!=") == 0) {
1848 } else if (strcmp(op, "&&") == 0) {
1850 } else if (strcmp(op, "||") == 0) {
1853 do_warning("unknown op '%s'", op);
1859 static int set_op_prio(struct tep_print_arg *arg)
1862 /* single ops are the greatest */
1863 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1866 arg->op.prio = get_op_prio(arg->op.op);
1868 return arg->op.prio;
1871 /* Note, *tok does not get freed, but will most likely be saved */
1872 static enum tep_event_type
1873 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1875 struct tep_print_arg *left, *right = NULL;
1876 enum tep_event_type type;
1879 /* the op is passed in via tok */
1882 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1883 /* handle single op */
1885 do_warning_event(event, "bad op token %s", token);
1895 do_warning_event(event, "bad op token %s", token);
1900 /* make an empty left */
1905 left->type = TEP_PRINT_NULL;
1906 arg->op.left = left;
1908 right = alloc_arg();
1912 arg->op.right = right;
1914 /* do not free the token, it belongs to an op */
1916 type = process_arg(event, right, tok);
1918 } else if (strcmp(token, "?") == 0) {
1924 /* copy the top arg to the left */
1927 arg->type = TEP_PRINT_OP;
1929 arg->op.left = left;
1932 /* it will set arg->op.right */
1933 type = process_cond(event, arg, tok);
1935 } else if (strcmp(token, ">>") == 0 ||
1936 strcmp(token, "<<") == 0 ||
1937 strcmp(token, "&") == 0 ||
1938 strcmp(token, "|") == 0 ||
1939 strcmp(token, "&&") == 0 ||
1940 strcmp(token, "||") == 0 ||
1941 strcmp(token, "-") == 0 ||
1942 strcmp(token, "+") == 0 ||
1943 strcmp(token, "*") == 0 ||
1944 strcmp(token, "^") == 0 ||
1945 strcmp(token, "/") == 0 ||
1946 strcmp(token, "%") == 0 ||
1947 strcmp(token, "<") == 0 ||
1948 strcmp(token, ">") == 0 ||
1949 strcmp(token, "<=") == 0 ||
1950 strcmp(token, ">=") == 0 ||
1951 strcmp(token, "==") == 0 ||
1952 strcmp(token, "!=") == 0) {
1958 /* copy the top arg to the left */
1961 arg->type = TEP_PRINT_OP;
1963 arg->op.left = left;
1964 arg->op.right = NULL;
1966 if (set_op_prio(arg) == -1) {
1967 event->flags |= TEP_EVENT_FL_FAILED;
1968 /* arg->op.op (= token) will be freed at out_free */
1973 type = read_token_item(&token);
1976 /* could just be a type pointer */
1977 if ((strcmp(arg->op.op, "*") == 0) &&
1978 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
1981 if (left->type != TEP_PRINT_ATOM) {
1982 do_warning_event(event, "bad pointer type");
1985 new_atom = realloc(left->atom.atom,
1986 strlen(left->atom.atom) + 3);
1990 left->atom.atom = new_atom;
1991 strcat(left->atom.atom, " *");
1999 right = alloc_arg();
2003 type = process_arg_token(event, right, tok, type);
2004 if (type == TEP_EVENT_ERROR) {
2006 /* token was freed in process_arg_token() via *tok */
2011 if (right->type == TEP_PRINT_OP &&
2012 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2013 struct tep_print_arg tmp;
2015 /* rotate ops according to the priority */
2016 arg->op.right = right->op.left;
2022 arg->op.left = right;
2024 arg->op.right = right;
2027 } else if (strcmp(token, "[") == 0) {
2035 arg->type = TEP_PRINT_OP;
2037 arg->op.left = left;
2041 /* it will set arg->op.right */
2042 type = process_array(event, arg, tok);
2045 do_warning_event(event, "unknown op '%s'", token);
2046 event->flags |= TEP_EVENT_FL_FAILED;
2047 /* the arg is now the left side */
2051 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2054 /* higher prios need to be closer to the root */
2055 prio = get_op_prio(*tok);
2057 if (prio > arg->op.prio)
2058 return process_op(event, arg, tok);
2060 return process_op(event, right, tok);
2066 do_warning_event(event, "%s: not enough memory!", __func__);
2070 return TEP_EVENT_ERROR;
2073 static enum tep_event_type
2074 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2077 enum tep_event_type type;
2081 if (read_expected(TEP_EVENT_OP, "->") < 0)
2084 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2088 arg->type = TEP_PRINT_FIELD;
2089 arg->field.name = field;
2091 if (is_flag_field) {
2092 arg->field.field = tep_find_any_field(event, arg->field.name);
2093 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2095 } else if (is_symbolic_field) {
2096 arg->field.field = tep_find_any_field(event, arg->field.name);
2097 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2098 is_symbolic_field = 0;
2101 type = read_token(&token);
2110 return TEP_EVENT_ERROR;
2113 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2114 struct tep_print_arg **print_arg)
2116 struct tep_print_arg *field;
2117 enum tep_event_type type;
2121 field = alloc_arg();
2123 do_warning_event(event, "%s: not enough memory!", __func__);
2128 type = process_arg(event, field, &token);
2130 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2134 goto out_free_token;
2145 static char *arg_eval (struct tep_print_arg *arg);
2147 static unsigned long long
2148 eval_type_str(unsigned long long val, const char *type, int pointer)
2158 if (type[len-1] != '*') {
2159 do_warning("pointer expected with non pointer type");
2165 do_warning("%s: not enough memory!", __func__);
2168 memcpy(ref, type, len);
2170 /* chop off the " *" */
2173 val = eval_type_str(val, ref, 0);
2178 /* check if this is a pointer */
2179 if (type[len - 1] == '*')
2182 /* Try to figure out the arg size*/
2183 if (strncmp(type, "struct", 6) == 0)
2187 if (strcmp(type, "u8") == 0)
2190 if (strcmp(type, "u16") == 0)
2191 return val & 0xffff;
2193 if (strcmp(type, "u32") == 0)
2194 return val & 0xffffffff;
2196 if (strcmp(type, "u64") == 0 ||
2197 strcmp(type, "s64"))
2200 if (strcmp(type, "s8") == 0)
2201 return (unsigned long long)(char)val & 0xff;
2203 if (strcmp(type, "s16") == 0)
2204 return (unsigned long long)(short)val & 0xffff;
2206 if (strcmp(type, "s32") == 0)
2207 return (unsigned long long)(int)val & 0xffffffff;
2209 if (strncmp(type, "unsigned ", 9) == 0) {
2214 if (strcmp(type, "char") == 0) {
2216 return (unsigned long long)(char)val & 0xff;
2221 if (strcmp(type, "short") == 0) {
2223 return (unsigned long long)(short)val & 0xffff;
2225 return val & 0xffff;
2228 if (strcmp(type, "int") == 0) {
2230 return (unsigned long long)(int)val & 0xffffffff;
2232 return val & 0xffffffff;
2239 * Try to figure out the type.
2241 static unsigned long long
2242 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2244 if (arg->type != TEP_PRINT_TYPE) {
2245 do_warning("expected type argument");
2249 return eval_type_str(val, arg->typecast.type, pointer);
2252 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2254 long long left, right;
2257 switch (arg->type) {
2258 case TEP_PRINT_ATOM:
2259 *val = strtoll(arg->atom.atom, NULL, 0);
2261 case TEP_PRINT_TYPE:
2262 ret = arg_num_eval(arg->typecast.item, val);
2265 *val = eval_type(*val, arg, 0);
2268 switch (arg->op.op[0]) {
2270 ret = arg_num_eval(arg->op.left, &left);
2273 ret = arg_num_eval(arg->op.right, &right);
2277 *val = left || right;
2279 *val = left | right;
2282 ret = arg_num_eval(arg->op.left, &left);
2285 ret = arg_num_eval(arg->op.right, &right);
2289 *val = left && right;
2291 *val = left & right;
2294 ret = arg_num_eval(arg->op.left, &left);
2297 ret = arg_num_eval(arg->op.right, &right);
2300 switch (arg->op.op[1]) {
2302 *val = left < right;
2305 *val = left << right;
2308 *val = left <= right;
2311 do_warning("unknown op '%s'", arg->op.op);
2316 ret = arg_num_eval(arg->op.left, &left);
2319 ret = arg_num_eval(arg->op.right, &right);
2322 switch (arg->op.op[1]) {
2324 *val = left > right;
2327 *val = left >> right;
2330 *val = left >= right;
2333 do_warning("unknown op '%s'", arg->op.op);
2338 ret = arg_num_eval(arg->op.left, &left);
2341 ret = arg_num_eval(arg->op.right, &right);
2345 if (arg->op.op[1] != '=') {
2346 do_warning("unknown op '%s'", arg->op.op);
2349 *val = left == right;
2352 ret = arg_num_eval(arg->op.left, &left);
2355 ret = arg_num_eval(arg->op.right, &right);
2359 switch (arg->op.op[1]) {
2361 *val = left != right;
2364 do_warning("unknown op '%s'", arg->op.op);
2369 /* check for negative */
2370 if (arg->op.left->type == TEP_PRINT_NULL)
2373 ret = arg_num_eval(arg->op.left, &left);
2376 ret = arg_num_eval(arg->op.right, &right);
2379 *val = left - right;
2382 if (arg->op.left->type == TEP_PRINT_NULL)
2385 ret = arg_num_eval(arg->op.left, &left);
2388 ret = arg_num_eval(arg->op.right, &right);
2391 *val = left + right;
2394 ret = arg_num_eval(arg->op.right, &right);
2400 do_warning("unknown op '%s'", arg->op.op);
2405 case TEP_PRINT_NULL:
2406 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2407 case TEP_PRINT_STRING:
2408 case TEP_PRINT_BSTRING:
2409 case TEP_PRINT_BITMASK:
2411 do_warning("invalid eval type %d", arg->type);
2418 static char *arg_eval (struct tep_print_arg *arg)
2421 static char buf[20];
2423 switch (arg->type) {
2424 case TEP_PRINT_ATOM:
2425 return arg->atom.atom;
2426 case TEP_PRINT_TYPE:
2427 return arg_eval(arg->typecast.item);
2429 if (!arg_num_eval(arg, &val))
2431 sprintf(buf, "%lld", val);
2434 case TEP_PRINT_NULL:
2435 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2436 case TEP_PRINT_STRING:
2437 case TEP_PRINT_BSTRING:
2438 case TEP_PRINT_BITMASK:
2440 do_warning("invalid eval type %d", arg->type);
2447 static enum tep_event_type
2448 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2450 enum tep_event_type type;
2451 struct tep_print_arg *arg = NULL;
2452 struct tep_print_flag_sym *field;
2458 type = read_token_item(&token);
2459 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2467 type = process_arg(event, arg, &token);
2469 if (type == TEP_EVENT_OP)
2470 type = process_op(event, arg, &token);
2472 if (type == TEP_EVENT_ERROR)
2475 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2478 field = calloc(1, sizeof(*field));
2482 value = arg_eval(arg);
2484 goto out_free_field;
2485 field->value = strdup(value);
2486 if (field->value == NULL)
2487 goto out_free_field;
2495 type = process_arg(event, arg, &token);
2496 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2497 goto out_free_field;
2499 value = arg_eval(arg);
2501 goto out_free_field;
2502 field->str = strdup(value);
2503 if (field->str == NULL)
2504 goto out_free_field;
2509 list = &field->next;
2512 type = read_token_item(&token);
2513 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2519 free_flag_sym(field);
2525 return TEP_EVENT_ERROR;
2528 static enum tep_event_type
2529 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2531 struct tep_print_arg *field;
2532 enum tep_event_type type;
2535 memset(arg, 0, sizeof(*arg));
2536 arg->type = TEP_PRINT_FLAGS;
2538 field = alloc_arg();
2540 do_warning_event(event, "%s: not enough memory!", __func__);
2544 type = process_field_arg(event, field, &token);
2546 /* Handle operations in the first argument */
2547 while (type == TEP_EVENT_OP)
2548 type = process_op(event, field, &token);
2550 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2551 goto out_free_field;
2554 arg->flags.field = field;
2556 type = read_token_item(&token);
2557 if (event_item_type(type)) {
2558 arg->flags.delim = token;
2559 type = read_token_item(&token);
2562 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2565 type = process_fields(event, &arg->flags.flags, &token);
2566 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2570 type = read_token_item(tok);
2578 return TEP_EVENT_ERROR;
2581 static enum tep_event_type
2582 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2584 struct tep_print_arg *field;
2585 enum tep_event_type type;
2588 memset(arg, 0, sizeof(*arg));
2589 arg->type = TEP_PRINT_SYMBOL;
2591 field = alloc_arg();
2593 do_warning_event(event, "%s: not enough memory!", __func__);
2597 type = process_field_arg(event, field, &token);
2599 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2600 goto out_free_field;
2602 arg->symbol.field = field;
2604 type = process_fields(event, &arg->symbol.symbols, &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_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2622 char **tok, enum tep_print_arg_type type)
2624 memset(arg, 0, sizeof(*arg));
2627 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2630 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2633 return read_token_item(tok);
2636 free_arg(arg->hex.field);
2637 arg->hex.field = NULL;
2640 return TEP_EVENT_ERROR;
2643 static enum tep_event_type
2644 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2646 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2649 static enum tep_event_type
2650 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2653 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2656 static enum tep_event_type
2657 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2659 memset(arg, 0, sizeof(*arg));
2660 arg->type = TEP_PRINT_INT_ARRAY;
2662 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2665 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2668 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2671 return read_token_item(tok);
2674 free_arg(arg->int_array.count);
2675 arg->int_array.count = NULL;
2677 free_arg(arg->int_array.field);
2678 arg->int_array.field = NULL;
2681 return TEP_EVENT_ERROR;
2684 static enum tep_event_type
2685 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2687 struct tep_format_field *field;
2688 enum tep_event_type type;
2691 memset(arg, 0, sizeof(*arg));
2692 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2695 * The item within the parenthesis is another field that holds
2696 * the index into where the array starts.
2698 type = read_token(&token);
2700 if (type != TEP_EVENT_ITEM)
2703 /* Find the field */
2705 field = tep_find_field(event, token);
2709 arg->dynarray.field = field;
2710 arg->dynarray.index = 0;
2712 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2716 type = read_token_item(&token);
2718 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2724 do_warning_event(event, "%s: not enough memory!", __func__);
2726 return TEP_EVENT_ERROR;
2729 type = process_arg(event, arg, &token);
2730 if (type == TEP_EVENT_ERROR)
2733 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2737 type = read_token_item(tok);
2745 return TEP_EVENT_ERROR;
2748 static enum tep_event_type
2749 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2752 struct tep_format_field *field;
2753 enum tep_event_type type;
2756 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2759 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2761 /* Find the field */
2762 field = tep_find_field(event, token);
2766 arg->dynarray.field = field;
2767 arg->dynarray.index = 0;
2769 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2772 type = read_token(&token);
2781 return TEP_EVENT_ERROR;
2784 static enum tep_event_type
2785 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2787 struct tep_print_arg *item_arg;
2788 enum tep_event_type type;
2791 type = process_arg(event, arg, &token);
2793 if (type == TEP_EVENT_ERROR)
2796 if (type == TEP_EVENT_OP)
2797 type = process_op(event, arg, &token);
2799 if (type == TEP_EVENT_ERROR)
2802 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2806 type = read_token_item(&token);
2809 * If the next token is an item or another open paren, then
2810 * this was a typecast.
2812 if (event_item_type(type) ||
2813 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2815 /* make this a typecast and contine */
2817 /* prevous must be an atom */
2818 if (arg->type != TEP_PRINT_ATOM) {
2819 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2823 item_arg = alloc_arg();
2825 do_warning_event(event, "%s: not enough memory!",
2830 arg->type = TEP_PRINT_TYPE;
2831 arg->typecast.type = arg->atom.atom;
2832 arg->typecast.item = item_arg;
2833 type = process_arg_token(event, item_arg, &token, type);
2843 return TEP_EVENT_ERROR;
2847 static enum tep_event_type
2848 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2851 enum tep_event_type type;
2854 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2857 arg->type = TEP_PRINT_STRING;
2858 arg->string.string = token;
2859 arg->string.offset = -1;
2861 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2864 type = read_token(&token);
2873 return TEP_EVENT_ERROR;
2876 static enum tep_event_type
2877 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2880 enum tep_event_type type;
2883 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2886 arg->type = TEP_PRINT_BITMASK;
2887 arg->bitmask.bitmask = token;
2888 arg->bitmask.offset = -1;
2890 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2893 type = read_token(&token);
2902 return TEP_EVENT_ERROR;
2905 static struct tep_function_handler *
2906 find_func_handler(struct tep_handle *pevent, char *func_name)
2908 struct tep_function_handler *func;
2913 for (func = pevent->func_handlers; func; func = func->next) {
2914 if (strcmp(func->name, func_name) == 0)
2921 static void remove_func_handler(struct tep_handle *pevent, char *func_name)
2923 struct tep_function_handler *func;
2924 struct tep_function_handler **next;
2926 next = &pevent->func_handlers;
2927 while ((func = *next)) {
2928 if (strcmp(func->name, func_name) == 0) {
2930 free_func_handle(func);
2937 static enum tep_event_type
2938 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
2939 struct tep_print_arg *arg, char **tok)
2941 struct tep_print_arg **next_arg;
2942 struct tep_print_arg *farg;
2943 enum tep_event_type type;
2947 arg->type = TEP_PRINT_FUNC;
2948 arg->func.func = func;
2952 next_arg = &(arg->func.args);
2953 for (i = 0; i < func->nr_args; i++) {
2956 do_warning_event(event, "%s: not enough memory!",
2958 return TEP_EVENT_ERROR;
2961 type = process_arg(event, farg, &token);
2962 if (i < (func->nr_args - 1)) {
2963 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
2964 do_warning_event(event,
2965 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2966 func->name, func->nr_args,
2967 event->name, i + 1);
2971 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
2972 do_warning_event(event,
2973 "Error: function '%s()' only expects %d arguments but event %s has more",
2974 func->name, func->nr_args, event->name);
2980 next_arg = &(farg->next);
2984 type = read_token(&token);
2992 return TEP_EVENT_ERROR;
2995 static enum tep_event_type
2996 process_function(struct tep_event *event, struct tep_print_arg *arg,
2997 char *token, char **tok)
2999 struct tep_function_handler *func;
3001 if (strcmp(token, "__print_flags") == 0) {
3004 return process_flags(event, arg, tok);
3006 if (strcmp(token, "__print_symbolic") == 0) {
3008 is_symbolic_field = 1;
3009 return process_symbols(event, arg, tok);
3011 if (strcmp(token, "__print_hex") == 0) {
3013 return process_hex(event, arg, tok);
3015 if (strcmp(token, "__print_hex_str") == 0) {
3017 return process_hex_str(event, arg, tok);
3019 if (strcmp(token, "__print_array") == 0) {
3021 return process_int_array(event, arg, tok);
3023 if (strcmp(token, "__get_str") == 0) {
3025 return process_str(event, arg, tok);
3027 if (strcmp(token, "__get_bitmask") == 0) {
3029 return process_bitmask(event, arg, tok);
3031 if (strcmp(token, "__get_dynamic_array") == 0) {
3033 return process_dynamic_array(event, arg, tok);
3035 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3037 return process_dynamic_array_len(event, arg, tok);
3040 func = find_func_handler(event->pevent, token);
3043 return process_func_handler(event, func, arg, tok);
3046 do_warning_event(event, "function %s not defined", token);
3048 return TEP_EVENT_ERROR;
3051 static enum tep_event_type
3052 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3053 char **tok, enum tep_event_type type)
3061 case TEP_EVENT_ITEM:
3062 if (strcmp(token, "REC") == 0) {
3064 type = process_entry(event, arg, &token);
3068 /* test the next token */
3069 type = read_token_item(&token);
3072 * If the next token is a parenthesis, then this
3075 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3078 /* this will free atom. */
3079 type = process_function(event, arg, atom, &token);
3082 /* atoms can be more than one token long */
3083 while (type == TEP_EVENT_ITEM) {
3085 new_atom = realloc(atom,
3086 strlen(atom) + strlen(token) + 2);
3091 return TEP_EVENT_ERROR;
3095 strcat(atom, token);
3097 type = read_token_item(&token);
3100 arg->type = TEP_PRINT_ATOM;
3101 arg->atom.atom = atom;
3104 case TEP_EVENT_DQUOTE:
3105 case TEP_EVENT_SQUOTE:
3106 arg->type = TEP_PRINT_ATOM;
3107 arg->atom.atom = token;
3108 type = read_token_item(&token);
3110 case TEP_EVENT_DELIM:
3111 if (strcmp(token, "(") == 0) {
3113 type = process_paren(event, arg, &token);
3117 /* handle single ops */
3118 arg->type = TEP_PRINT_OP;
3120 arg->op.left = NULL;
3121 type = process_op(event, arg, &token);
3123 /* On error, the op is freed */
3124 if (type == TEP_EVENT_ERROR)
3127 /* return error type if errored */
3130 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3132 do_warning_event(event, "unexpected type %d", type);
3133 return TEP_EVENT_ERROR;
3140 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3142 enum tep_event_type type = TEP_EVENT_ERROR;
3143 struct tep_print_arg *arg;
3148 if (type == TEP_EVENT_NEWLINE) {
3149 type = read_token_item(&token);
3155 do_warning_event(event, "%s: not enough memory!",
3160 type = process_arg(event, arg, &token);
3162 if (type == TEP_EVENT_ERROR) {
3171 if (type == TEP_EVENT_OP) {
3172 type = process_op(event, arg, &token);
3174 if (type == TEP_EVENT_ERROR) {
3183 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3190 } while (type != TEP_EVENT_NONE);
3192 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3198 static int event_read_print(struct tep_event *event)
3200 enum tep_event_type type;
3204 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3207 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3210 if (read_expected(TEP_EVENT_OP, ":") < 0)
3213 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3217 event->print_fmt.format = token;
3218 event->print_fmt.args = NULL;
3220 /* ok to have no arg */
3221 type = read_token_item(&token);
3223 if (type == TEP_EVENT_NONE)
3226 /* Handle concatenation of print lines */
3227 if (type == TEP_EVENT_DQUOTE) {
3230 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3233 free_token(event->print_fmt.format);
3234 event->print_fmt.format = NULL;
3239 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3244 ret = event_read_print_args(event, &event->print_fmt.args);
3256 * tep_find_common_field - return a common field by event
3257 * @event: handle for the event
3258 * @name: the name of the common field to return
3260 * Returns a common field from the event by the given @name.
3261 * This only searches the common fields and not all field.
3263 struct tep_format_field *
3264 tep_find_common_field(struct tep_event *event, const char *name)
3266 struct tep_format_field *format;
3268 for (format = event->format.common_fields;
3269 format; format = format->next) {
3270 if (strcmp(format->name, name) == 0)
3278 * tep_find_field - find a non-common field
3279 * @event: handle for the event
3280 * @name: the name of the non-common field
3282 * Returns a non-common field by the given @name.
3283 * This does not search common fields.
3285 struct tep_format_field *
3286 tep_find_field(struct tep_event *event, const char *name)
3288 struct tep_format_field *format;
3290 for (format = event->format.fields;
3291 format; format = format->next) {
3292 if (strcmp(format->name, name) == 0)
3300 * tep_find_any_field - find any field by name
3301 * @event: handle for the event
3302 * @name: the name of the field
3304 * Returns a field by the given @name.
3305 * This searches the common field names first, then
3306 * the non-common ones if a common one was not found.
3308 struct tep_format_field *
3309 tep_find_any_field(struct tep_event *event, const char *name)
3311 struct tep_format_field *format;
3313 format = tep_find_common_field(event, name);
3316 return tep_find_field(event, name);
3320 * tep_read_number - read a number from data
3321 * @pevent: handle for the pevent
3322 * @ptr: the raw data
3323 * @size: the size of the data that holds the number
3325 * Returns the number (converted to host) from the
3328 unsigned long long tep_read_number(struct tep_handle *pevent,
3329 const void *ptr, int size)
3331 unsigned long long val;
3335 return *(unsigned char *)ptr;
3337 return tep_data2host2(pevent, *(unsigned short *)ptr);
3339 return tep_data2host4(pevent, *(unsigned int *)ptr);
3341 memcpy(&val, (ptr), sizeof(unsigned long long));
3342 return tep_data2host8(pevent, val);
3350 * tep_read_number_field - read a number from data
3351 * @field: a handle to the field
3352 * @data: the raw data to read
3353 * @value: the value to place the number in
3355 * Reads raw data according to a field offset and size,
3356 * and translates it into @value.
3358 * Returns 0 on success, -1 otherwise.
3360 int tep_read_number_field(struct tep_format_field *field, const void *data,
3361 unsigned long long *value)
3365 switch (field->size) {
3370 *value = tep_read_number(field->event->pevent,
3371 data + field->offset, field->size);
3378 static int get_common_info(struct tep_handle *pevent,
3379 const char *type, int *offset, int *size)
3381 struct tep_event *event;
3382 struct tep_format_field *field;
3385 * All events should have the same common elements.
3386 * Pick any event to find where the type is;
3388 if (!pevent->events) {
3389 do_warning("no event_list!");
3393 event = pevent->events[0];
3394 field = tep_find_common_field(event, type);
3398 *offset = field->offset;
3399 *size = field->size;
3404 static int __parse_common(struct tep_handle *pevent, void *data,
3405 int *size, int *offset, const char *name)
3410 ret = get_common_info(pevent, name, offset, size);
3414 return tep_read_number(pevent, data + *offset, *size);
3417 static int trace_parse_common_type(struct tep_handle *pevent, void *data)
3419 return __parse_common(pevent, data,
3420 &pevent->type_size, &pevent->type_offset,
3424 static int parse_common_pid(struct tep_handle *pevent, void *data)
3426 return __parse_common(pevent, data,
3427 &pevent->pid_size, &pevent->pid_offset,
3431 static int parse_common_pc(struct tep_handle *pevent, void *data)
3433 return __parse_common(pevent, data,
3434 &pevent->pc_size, &pevent->pc_offset,
3435 "common_preempt_count");
3438 static int parse_common_flags(struct tep_handle *pevent, void *data)
3440 return __parse_common(pevent, data,
3441 &pevent->flags_size, &pevent->flags_offset,
3445 static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
3447 return __parse_common(pevent, data,
3448 &pevent->ld_size, &pevent->ld_offset,
3449 "common_lock_depth");
3452 static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
3454 return __parse_common(pevent, data,
3455 &pevent->ld_size, &pevent->ld_offset,
3456 "common_migrate_disable");
3459 static int events_id_cmp(const void *a, const void *b);
3462 * tep_find_event - find an event by given id
3463 * @pevent: a handle to the pevent
3464 * @id: the id of the event
3466 * Returns an event that has a given @id.
3468 struct tep_event *tep_find_event(struct tep_handle *pevent, int id)
3470 struct tep_event **eventptr;
3471 struct tep_event key;
3472 struct tep_event *pkey = &key;
3474 /* Check cache first */
3475 if (pevent->last_event && pevent->last_event->id == id)
3476 return pevent->last_event;
3480 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3481 sizeof(*pevent->events), events_id_cmp);
3484 pevent->last_event = *eventptr;
3492 * tep_find_event_by_name - find an event by given name
3493 * @pevent: a handle to the pevent
3494 * @sys: the system name to search for
3495 * @name: the name of the event to search for
3497 * This returns an event with a given @name and under the system
3498 * @sys. If @sys is NULL the first event with @name is returned.
3501 tep_find_event_by_name(struct tep_handle *pevent,
3502 const char *sys, const char *name)
3504 struct tep_event *event = NULL;
3507 if (pevent->last_event &&
3508 strcmp(pevent->last_event->name, name) == 0 &&
3509 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3510 return pevent->last_event;
3512 for (i = 0; i < pevent->nr_events; i++) {
3513 event = pevent->events[i];
3514 if (strcmp(event->name, name) == 0) {
3517 if (strcmp(event->system, sys) == 0)
3521 if (i == pevent->nr_events)
3524 pevent->last_event = event;
3528 static unsigned long long
3529 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3531 struct tep_handle *pevent = event->pevent;
3532 unsigned long long val = 0;
3533 unsigned long long left, right;
3534 struct tep_print_arg *typearg = NULL;
3535 struct tep_print_arg *larg;
3536 unsigned long offset;
3537 unsigned int field_size;
3539 switch (arg->type) {
3540 case TEP_PRINT_NULL:
3543 case TEP_PRINT_ATOM:
3544 return strtoull(arg->atom.atom, NULL, 0);
3545 case TEP_PRINT_FIELD:
3546 if (!arg->field.field) {
3547 arg->field.field = tep_find_any_field(event, arg->field.name);
3548 if (!arg->field.field)
3549 goto out_warning_field;
3552 /* must be a number */
3553 val = tep_read_number(pevent, data + arg->field.field->offset,
3554 arg->field.field->size);
3556 case TEP_PRINT_FLAGS:
3557 case TEP_PRINT_SYMBOL:
3558 case TEP_PRINT_INT_ARRAY:
3560 case TEP_PRINT_HEX_STR:
3562 case TEP_PRINT_TYPE:
3563 val = eval_num_arg(data, size, event, arg->typecast.item);
3564 return eval_type(val, arg, 0);
3565 case TEP_PRINT_STRING:
3566 case TEP_PRINT_BSTRING:
3567 case TEP_PRINT_BITMASK:
3569 case TEP_PRINT_FUNC: {
3572 val = process_defined_func(&s, data, size, event, arg);
3573 trace_seq_destroy(&s);
3577 if (strcmp(arg->op.op, "[") == 0) {
3579 * Arrays are special, since we don't want
3580 * to read the arg as is.
3582 right = eval_num_arg(data, size, event, arg->op.right);
3584 /* handle typecasts */
3585 larg = arg->op.left;
3586 while (larg->type == TEP_PRINT_TYPE) {
3589 larg = larg->typecast.item;
3592 /* Default to long size */
3593 field_size = pevent->long_size;
3595 switch (larg->type) {
3596 case TEP_PRINT_DYNAMIC_ARRAY:
3597 offset = tep_read_number(pevent,
3598 data + larg->dynarray.field->offset,
3599 larg->dynarray.field->size);
3600 if (larg->dynarray.field->elementsize)
3601 field_size = larg->dynarray.field->elementsize;
3603 * The actual length of the dynamic array is stored
3604 * in the top half of the field, and the offset
3605 * is in the bottom half of the 32 bit field.
3610 case TEP_PRINT_FIELD:
3611 if (!larg->field.field) {
3613 tep_find_any_field(event, larg->field.name);
3614 if (!larg->field.field) {
3616 goto out_warning_field;
3619 field_size = larg->field.field->elementsize;
3620 offset = larg->field.field->offset +
3621 right * larg->field.field->elementsize;
3624 goto default_op; /* oops, all bets off */
3626 val = tep_read_number(pevent,
3627 data + offset, field_size);
3629 val = eval_type(val, typearg, 1);
3631 } else if (strcmp(arg->op.op, "?") == 0) {
3632 left = eval_num_arg(data, size, event, arg->op.left);
3633 arg = arg->op.right;
3635 val = eval_num_arg(data, size, event, arg->op.left);
3637 val = eval_num_arg(data, size, event, arg->op.right);
3641 left = eval_num_arg(data, size, event, arg->op.left);
3642 right = eval_num_arg(data, size, event, arg->op.right);
3643 switch (arg->op.op[0]) {
3645 switch (arg->op.op[1]) {
3650 val = left != right;
3653 goto out_warning_op;
3661 val = left || right;
3667 val = left && right;
3672 switch (arg->op.op[1]) {
3677 val = left << right;
3680 val = left <= right;
3683 goto out_warning_op;
3687 switch (arg->op.op[1]) {
3692 val = left >> right;
3695 val = left >= right;
3698 goto out_warning_op;
3702 if (arg->op.op[1] != '=')
3703 goto out_warning_op;
3705 val = left == right;
3723 goto out_warning_op;
3726 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3727 offset = tep_read_number(pevent,
3728 data + arg->dynarray.field->offset,
3729 arg->dynarray.field->size);
3731 * The total allocated length of the dynamic array is
3732 * stored in the top half of the field, and the offset
3733 * is in the bottom half of the 32 bit field.
3735 val = (unsigned long long)(offset >> 16);
3737 case TEP_PRINT_DYNAMIC_ARRAY:
3738 /* Without [], we pass the address to the dynamic data */
3739 offset = tep_read_number(pevent,
3740 data + arg->dynarray.field->offset,
3741 arg->dynarray.field->size);
3743 * The total allocated length of the dynamic array is
3744 * stored in the top half of the field, and the offset
3745 * is in the bottom half of the 32 bit field.
3748 val = (unsigned long long)((unsigned long)data + offset);
3750 default: /* not sure what to do there */
3756 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3760 do_warning_event(event, "%s: field %s not found",
3761 __func__, arg->field.name);
3767 unsigned long long value;
3770 static const struct flag flags[] = {
3771 { "HI_SOFTIRQ", 0 },
3772 { "TIMER_SOFTIRQ", 1 },
3773 { "NET_TX_SOFTIRQ", 2 },
3774 { "NET_RX_SOFTIRQ", 3 },
3775 { "BLOCK_SOFTIRQ", 4 },
3776 { "IRQ_POLL_SOFTIRQ", 5 },
3777 { "TASKLET_SOFTIRQ", 6 },
3778 { "SCHED_SOFTIRQ", 7 },
3779 { "HRTIMER_SOFTIRQ", 8 },
3780 { "RCU_SOFTIRQ", 9 },
3782 { "HRTIMER_NORESTART", 0 },
3783 { "HRTIMER_RESTART", 1 },
3786 static long long eval_flag(const char *flag)
3791 * Some flags in the format files do not get converted.
3792 * If the flag is not numeric, see if it is something that
3793 * we already know about.
3795 if (isdigit(flag[0]))
3796 return strtoull(flag, NULL, 0);
3798 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3799 if (strcmp(flags[i].name, flag) == 0)
3800 return flags[i].value;
3805 static void print_str_to_seq(struct trace_seq *s, const char *format,
3806 int len_arg, const char *str)
3809 trace_seq_printf(s, format, len_arg, str);
3811 trace_seq_printf(s, format, str);
3814 static void print_bitmask_to_seq(struct tep_handle *pevent,
3815 struct trace_seq *s, const char *format,
3816 int len_arg, const void *data, int size)
3818 int nr_bits = size * 8;
3819 int str_size = (nr_bits + 3) / 4;
3827 * The kernel likes to put in commas every 32 bits, we
3830 str_size += (nr_bits - 1) / 32;
3832 str = malloc(str_size + 1);
3834 do_warning("%s: not enough memory!", __func__);
3839 /* Start out with -2 for the two chars per byte */
3840 for (i = str_size - 2; i >= 0; i -= 2) {
3842 * data points to a bit mask of size bytes.
3843 * In the kernel, this is an array of long words, thus
3844 * endianness is very important.
3846 if (pevent->file_bigendian)
3847 index = size - (len + 1);
3851 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3852 memcpy(str + i, buf, 2);
3854 if (!(len & 3) && i > 0) {
3861 trace_seq_printf(s, format, len_arg, str);
3863 trace_seq_printf(s, format, str);
3868 static void print_str_arg(struct trace_seq *s, void *data, int size,
3869 struct tep_event *event, const char *format,
3870 int len_arg, struct tep_print_arg *arg)
3872 struct tep_handle *pevent = event->pevent;
3873 struct tep_print_flag_sym *flag;
3874 struct tep_format_field *field;
3875 struct printk_map *printk;
3876 long long val, fval;
3877 unsigned long long addr;
3883 switch (arg->type) {
3884 case TEP_PRINT_NULL:
3887 case TEP_PRINT_ATOM:
3888 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3890 case TEP_PRINT_FIELD:
3891 field = arg->field.field;
3893 field = tep_find_any_field(event, arg->field.name);
3895 str = arg->field.name;
3896 goto out_warning_field;
3898 arg->field.field = field;
3900 /* Zero sized fields, mean the rest of the data */
3901 len = field->size ? : size - field->offset;
3904 * Some events pass in pointers. If this is not an array
3905 * and the size is the same as long_size, assume that it
3908 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
3909 field->size == pevent->long_size) {
3911 /* Handle heterogeneous recording and processing
3915 * Traces recorded on 32-bit devices (32-bit
3916 * addressing) and processed on 64-bit devices:
3917 * In this case, only 32 bits should be read.
3920 * Traces recorded on 64 bit devices and processed
3921 * on 32-bit devices:
3922 * In this case, 64 bits must be read.
3924 addr = (pevent->long_size == 8) ?
3925 *(unsigned long long *)(data + field->offset) :
3926 (unsigned long long)*(unsigned int *)(data + field->offset);
3928 /* Check if it matches a print format */
3929 printk = find_printk(pevent, addr);
3931 trace_seq_puts(s, printk->printk);
3933 trace_seq_printf(s, "%llx", addr);
3936 str = malloc(len + 1);
3938 do_warning_event(event, "%s: not enough memory!",
3942 memcpy(str, data + field->offset, len);
3944 print_str_to_seq(s, format, len_arg, str);
3947 case TEP_PRINT_FLAGS:
3948 val = eval_num_arg(data, size, event, arg->flags.field);
3950 for (flag = arg->flags.flags; flag; flag = flag->next) {
3951 fval = eval_flag(flag->value);
3952 if (!val && fval < 0) {
3953 print_str_to_seq(s, format, len_arg, flag->str);
3956 if (fval > 0 && (val & fval) == fval) {
3957 if (print && arg->flags.delim)
3958 trace_seq_puts(s, arg->flags.delim);
3959 print_str_to_seq(s, format, len_arg, flag->str);
3965 if (print && arg->flags.delim)
3966 trace_seq_puts(s, arg->flags.delim);
3967 trace_seq_printf(s, "0x%llx", val);
3970 case TEP_PRINT_SYMBOL:
3971 val = eval_num_arg(data, size, event, arg->symbol.field);
3972 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3973 fval = eval_flag(flag->value);
3975 print_str_to_seq(s, format, len_arg, flag->str);
3980 trace_seq_printf(s, "0x%llx", val);
3983 case TEP_PRINT_HEX_STR:
3984 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
3985 unsigned long offset;
3986 offset = tep_read_number(pevent,
3987 data + arg->hex.field->dynarray.field->offset,
3988 arg->hex.field->dynarray.field->size);
3989 hex = data + (offset & 0xffff);
3991 field = arg->hex.field->field.field;
3993 str = arg->hex.field->field.name;
3994 field = tep_find_any_field(event, str);
3996 goto out_warning_field;
3997 arg->hex.field->field.field = field;
3999 hex = data + field->offset;
4001 len = eval_num_arg(data, size, event, arg->hex.size);
4002 for (i = 0; i < len; i++) {
4003 if (i && arg->type == TEP_PRINT_HEX)
4004 trace_seq_putc(s, ' ');
4005 trace_seq_printf(s, "%02x", hex[i]);
4009 case TEP_PRINT_INT_ARRAY: {
4013 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4014 unsigned long offset;
4015 struct tep_format_field *field =
4016 arg->int_array.field->dynarray.field;
4017 offset = tep_read_number(pevent,
4018 data + field->offset,
4020 num = data + (offset & 0xffff);
4022 field = arg->int_array.field->field.field;
4024 str = arg->int_array.field->field.name;
4025 field = tep_find_any_field(event, str);
4027 goto out_warning_field;
4028 arg->int_array.field->field.field = field;
4030 num = data + field->offset;
4032 len = eval_num_arg(data, size, event, arg->int_array.count);
4033 el_size = eval_num_arg(data, size, event,
4034 arg->int_array.el_size);
4035 for (i = 0; i < len; i++) {
4037 trace_seq_putc(s, ' ');
4040 trace_seq_printf(s, "%u", *(uint8_t *)num);
4041 } else if (el_size == 2) {
4042 trace_seq_printf(s, "%u", *(uint16_t *)num);
4043 } else if (el_size == 4) {
4044 trace_seq_printf(s, "%u", *(uint32_t *)num);
4045 } else if (el_size == 8) {
4046 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4048 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4049 el_size, *(uint8_t *)num);
4057 case TEP_PRINT_TYPE:
4059 case TEP_PRINT_STRING: {
4062 if (arg->string.offset == -1) {
4063 struct tep_format_field *f;
4065 f = tep_find_any_field(event, arg->string.string);
4066 arg->string.offset = f->offset;
4068 str_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->string.offset));
4069 str_offset &= 0xffff;
4070 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4073 case TEP_PRINT_BSTRING:
4074 print_str_to_seq(s, format, len_arg, arg->string.string);
4076 case TEP_PRINT_BITMASK: {
4080 if (arg->bitmask.offset == -1) {
4081 struct tep_format_field *f;
4083 f = tep_find_any_field(event, arg->bitmask.bitmask);
4084 arg->bitmask.offset = f->offset;
4086 bitmask_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->bitmask.offset));
4087 bitmask_size = bitmask_offset >> 16;
4088 bitmask_offset &= 0xffff;
4089 print_bitmask_to_seq(pevent, s, format, len_arg,
4090 data + bitmask_offset, bitmask_size);
4095 * The only op for string should be ? :
4097 if (arg->op.op[0] != '?')
4099 val = eval_num_arg(data, size, event, arg->op.left);
4101 print_str_arg(s, data, size, event,
4102 format, len_arg, arg->op.right->op.left);
4104 print_str_arg(s, data, size, event,
4105 format, len_arg, arg->op.right->op.right);
4107 case TEP_PRINT_FUNC:
4108 process_defined_func(s, data, size, event, arg);
4118 do_warning_event(event, "%s: field %s not found",
4119 __func__, arg->field.name);
4122 static unsigned long long
4123 process_defined_func(struct trace_seq *s, void *data, int size,
4124 struct tep_event *event, struct tep_print_arg *arg)
4126 struct tep_function_handler *func_handle = arg->func.func;
4127 struct func_params *param;
4128 unsigned long long *args;
4129 unsigned long long ret;
4130 struct tep_print_arg *farg;
4131 struct trace_seq str;
4133 struct save_str *next;
4135 } *strings = NULL, *string;
4138 if (!func_handle->nr_args) {
4139 ret = (*func_handle->func)(s, NULL);
4143 farg = arg->func.args;
4144 param = func_handle->params;
4147 args = malloc(sizeof(*args) * func_handle->nr_args);
4151 for (i = 0; i < func_handle->nr_args; i++) {
4152 switch (param->type) {
4153 case TEP_FUNC_ARG_INT:
4154 case TEP_FUNC_ARG_LONG:
4155 case TEP_FUNC_ARG_PTR:
4156 args[i] = eval_num_arg(data, size, event, farg);
4158 case TEP_FUNC_ARG_STRING:
4159 trace_seq_init(&str);
4160 print_str_arg(&str, data, size, event, "%s", -1, farg);
4161 trace_seq_terminate(&str);
4162 string = malloc(sizeof(*string));
4164 do_warning_event(event, "%s(%d): malloc str",
4165 __func__, __LINE__);
4168 string->next = strings;
4169 string->str = strdup(str.buffer);
4172 do_warning_event(event, "%s(%d): malloc str",
4173 __func__, __LINE__);
4176 args[i] = (uintptr_t)string->str;
4178 trace_seq_destroy(&str);
4182 * Something went totally wrong, this is not
4183 * an input error, something in this code broke.
4185 do_warning_event(event, "Unexpected end of arguments\n");
4189 param = param->next;
4192 ret = (*func_handle->func)(s, args);
4197 strings = string->next;
4203 /* TBD : handle return type here */
4207 static void free_args(struct tep_print_arg *args)
4209 struct tep_print_arg *next;
4219 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4221 struct tep_handle *pevent = event->pevent;
4222 struct tep_format_field *field, *ip_field;
4223 struct tep_print_arg *args, *arg, **next;
4224 unsigned long long ip, val;
4229 field = pevent->bprint_buf_field;
4230 ip_field = pevent->bprint_ip_field;
4233 field = tep_find_field(event, "buf");
4235 do_warning_event(event, "can't find buffer field for binary printk");
4238 ip_field = tep_find_field(event, "ip");
4240 do_warning_event(event, "can't find ip field for binary printk");
4243 pevent->bprint_buf_field = field;
4244 pevent->bprint_ip_field = ip_field;
4247 ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
4250 * The first arg is the IP pointer.
4254 do_warning_event(event, "%s(%d): not enough memory!",
4255 __func__, __LINE__);
4262 arg->type = TEP_PRINT_ATOM;
4264 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4267 /* skip the first "%ps: " */
4268 for (ptr = fmt + 5, bptr = data + field->offset;
4269 bptr < data + size && *ptr; ptr++) {
4294 if (isalnum(ptr[1])) {
4296 /* Check for special pointers */
4305 * Older kernels do not process
4306 * dereferenced pointers.
4307 * Only process if the pointer
4308 * value is a printable.
4310 if (isprint(*(char *)bptr))
4311 goto process_string;
4324 vsize = pevent->long_size;
4338 /* the pointers are always 4 bytes aligned */
4339 bptr = (void *)(((unsigned long)bptr + 3) &
4341 val = tep_read_number(pevent, bptr, vsize);
4345 do_warning_event(event, "%s(%d): not enough memory!",
4346 __func__, __LINE__);
4350 arg->type = TEP_PRINT_ATOM;
4351 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4358 * The '*' case means that an arg is used as the length.
4359 * We need to continue to figure out for what.
4369 do_warning_event(event, "%s(%d): not enough memory!",
4370 __func__, __LINE__);
4374 arg->type = TEP_PRINT_BSTRING;
4375 arg->string.string = strdup(bptr);
4376 if (!arg->string.string)
4378 bptr += strlen(bptr) + 1;
4395 get_bprint_format(void *data, int size __maybe_unused,
4396 struct tep_event *event)
4398 struct tep_handle *pevent = event->pevent;
4399 unsigned long long addr;
4400 struct tep_format_field *field;
4401 struct printk_map *printk;
4404 field = pevent->bprint_fmt_field;
4407 field = tep_find_field(event, "fmt");
4409 do_warning_event(event, "can't find format field for binary printk");
4412 pevent->bprint_fmt_field = field;
4415 addr = tep_read_number(pevent, data + field->offset, field->size);
4417 printk = find_printk(pevent, addr);
4419 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4424 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4430 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4431 struct tep_event *event, struct tep_print_arg *arg)
4434 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4436 if (arg->type == TEP_PRINT_FUNC) {
4437 process_defined_func(s, data, size, event, arg);
4441 if (arg->type != TEP_PRINT_FIELD) {
4442 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4448 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4449 if (!arg->field.field) {
4451 tep_find_any_field(event, arg->field.name);
4452 if (!arg->field.field) {
4453 do_warning_event(event, "%s: field %s not found",
4454 __func__, arg->field.name);
4458 if (arg->field.field->size != 6) {
4459 trace_seq_printf(s, "INVALIDMAC");
4462 buf = data + arg->field.field->offset;
4463 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4466 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4471 fmt = "%03d.%03d.%03d.%03d";
4473 fmt = "%d.%d.%d.%d";
4475 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4478 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4480 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4481 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4484 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4486 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4489 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4492 unsigned char zerolength[8];
4497 bool needcolon = false;
4499 struct in6_addr in6;
4501 memcpy(&in6, addr, sizeof(struct in6_addr));
4503 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4505 memset(zerolength, 0, sizeof(zerolength));
4512 /* find position of longest 0 run */
4513 for (i = 0; i < range; i++) {
4514 for (j = i; j < range; j++) {
4515 if (in6.s6_addr16[j] != 0)
4520 for (i = 0; i < range; i++) {
4521 if (zerolength[i] > longest) {
4522 longest = zerolength[i];
4526 if (longest == 1) /* don't compress a single 0 */
4530 for (i = 0; i < range; i++) {
4531 if (i == colonpos) {
4532 if (needcolon || i == 0)
4533 trace_seq_printf(s, ":");
4534 trace_seq_printf(s, ":");
4540 trace_seq_printf(s, ":");
4543 /* hex u16 without leading 0s */
4544 word = ntohs(in6.s6_addr16[i]);
4548 trace_seq_printf(s, "%x%02x", hi, lo);
4550 trace_seq_printf(s, "%x", lo);
4557 trace_seq_printf(s, ":");
4558 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4564 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4568 for (j = 0; j < 16; j += 2) {
4569 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4570 if (i == 'I' && j < 14)
4571 trace_seq_printf(s, ":");
4576 * %pi4 print an IPv4 address with leading zeros
4577 * %pI4 print an IPv4 address without leading zeros
4578 * %pi6 print an IPv6 address without colons
4579 * %pI6 print an IPv6 address with colons
4580 * %pI6c print an IPv6 address in compressed form with colons
4581 * %pISpc print an IP address based on sockaddr; p adds port.
4583 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4584 void *data, int size, struct tep_event *event,
4585 struct tep_print_arg *arg)
4589 if (arg->type == TEP_PRINT_FUNC) {
4590 process_defined_func(s, data, size, event, arg);
4594 if (arg->type != TEP_PRINT_FIELD) {
4595 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4599 if (!arg->field.field) {
4601 tep_find_any_field(event, arg->field.name);
4602 if (!arg->field.field) {
4603 do_warning("%s: field %s not found",
4604 __func__, arg->field.name);
4609 buf = data + arg->field.field->offset;
4611 if (arg->field.field->size != 4) {
4612 trace_seq_printf(s, "INVALIDIPv4");
4615 print_ip4_addr(s, i, buf);
4620 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4621 void *data, int size, struct tep_event *event,
4622 struct tep_print_arg *arg)
4629 if (i == 'I' && *ptr == 'c') {
4635 if (arg->type == TEP_PRINT_FUNC) {
4636 process_defined_func(s, data, size, event, arg);
4640 if (arg->type != TEP_PRINT_FIELD) {
4641 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4645 if (!arg->field.field) {
4647 tep_find_any_field(event, arg->field.name);
4648 if (!arg->field.field) {
4649 do_warning("%s: field %s not found",
4650 __func__, arg->field.name);
4655 buf = data + arg->field.field->offset;
4657 if (arg->field.field->size != 16) {
4658 trace_seq_printf(s, "INVALIDIPv6");
4663 print_ip6c_addr(s, buf);
4665 print_ip6_addr(s, i, buf);
4670 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4671 void *data, int size, struct tep_event *event,
4672 struct tep_print_arg *arg)
4674 char have_c = 0, have_p = 0;
4676 struct sockaddr_storage *sa;
4693 if (arg->type == TEP_PRINT_FUNC) {
4694 process_defined_func(s, data, size, event, arg);
4698 if (arg->type != TEP_PRINT_FIELD) {
4699 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4703 if (!arg->field.field) {
4705 tep_find_any_field(event, arg->field.name);
4706 if (!arg->field.field) {
4707 do_warning("%s: field %s not found",
4708 __func__, arg->field.name);
4713 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4715 if (sa->ss_family == AF_INET) {
4716 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4718 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4719 trace_seq_printf(s, "INVALIDIPv4");
4723 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4725 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4728 } else if (sa->ss_family == AF_INET6) {
4729 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4731 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4732 trace_seq_printf(s, "INVALIDIPv6");
4737 trace_seq_printf(s, "[");
4739 buf = (unsigned char *) &sa6->sin6_addr;
4741 print_ip6c_addr(s, buf);
4743 print_ip6_addr(s, i, buf);
4746 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4752 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4753 void *data, int size, struct tep_event *event,
4754 struct tep_print_arg *arg)
4756 char i = *ptr; /* 'i' or 'I' */
4769 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4772 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4775 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4784 static int is_printable_array(char *p, unsigned int len)
4788 for (i = 0; i < len && p[i]; i++)
4789 if (!isprint(p[i]) && !isspace(p[i]))
4794 void tep_print_field(struct trace_seq *s, void *data,
4795 struct tep_format_field *field)
4797 unsigned long long val;
4798 unsigned int offset, len, i;
4799 struct tep_handle *pevent = field->event->pevent;
4801 if (field->flags & TEP_FIELD_IS_ARRAY) {
4802 offset = field->offset;
4804 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
4805 val = tep_read_number(pevent, data + offset, len);
4810 if (field->flags & TEP_FIELD_IS_STRING &&
4811 is_printable_array(data + offset, len)) {
4812 trace_seq_printf(s, "%s", (char *)data + offset);
4814 trace_seq_puts(s, "ARRAY[");
4815 for (i = 0; i < len; i++) {
4817 trace_seq_puts(s, ", ");
4818 trace_seq_printf(s, "%02x",
4819 *((unsigned char *)data + offset + i));
4821 trace_seq_putc(s, ']');
4822 field->flags &= ~TEP_FIELD_IS_STRING;
4825 val = tep_read_number(pevent, data + field->offset,
4827 if (field->flags & TEP_FIELD_IS_POINTER) {
4828 trace_seq_printf(s, "0x%llx", val);
4829 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
4830 switch (field->size) {
4833 * If field is long then print it in hex.
4834 * A long usually stores pointers.
4836 if (field->flags & TEP_FIELD_IS_LONG)
4837 trace_seq_printf(s, "0x%x", (int)val);
4839 trace_seq_printf(s, "%d", (int)val);
4842 trace_seq_printf(s, "%2d", (short)val);
4845 trace_seq_printf(s, "%1d", (char)val);
4848 trace_seq_printf(s, "%lld", val);
4851 if (field->flags & TEP_FIELD_IS_LONG)
4852 trace_seq_printf(s, "0x%llx", val);
4854 trace_seq_printf(s, "%llu", val);
4859 void tep_print_fields(struct trace_seq *s, void *data,
4860 int size __maybe_unused, struct tep_event *event)
4862 struct tep_format_field *field;
4864 field = event->format.fields;
4866 trace_seq_printf(s, " %s=", field->name);
4867 tep_print_field(s, data, field);
4868 field = field->next;
4872 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
4874 struct tep_handle *pevent = event->pevent;
4875 struct tep_print_fmt *print_fmt = &event->print_fmt;
4876 struct tep_print_arg *arg = print_fmt->args;
4877 struct tep_print_arg *args = NULL;
4878 const char *ptr = print_fmt->format;
4879 unsigned long long val;
4880 struct func_map *func;
4881 const char *saveptr;
4883 char *bprint_fmt = NULL;
4891 if (event->flags & TEP_EVENT_FL_FAILED) {
4892 trace_seq_printf(s, "[FAILED TO PARSE]");
4893 tep_print_fields(s, data, size, event);
4897 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
4898 bprint_fmt = get_bprint_format(data, size, event);
4899 args = make_bprint_args(bprint_fmt, data, size, event);
4904 for (; *ptr; ptr++) {
4910 trace_seq_putc(s, '\n');
4913 trace_seq_putc(s, '\t');
4916 trace_seq_putc(s, '\r');
4919 trace_seq_putc(s, '\\');
4922 trace_seq_putc(s, *ptr);
4926 } else if (*ptr == '%') {
4934 trace_seq_putc(s, '%');
4937 /* FIXME: need to handle properly */
4949 /* The argument is the length. */
4951 do_warning_event(event, "no argument match");
4952 event->flags |= TEP_EVENT_FL_FAILED;
4955 len_arg = eval_num_arg(data, size, event, arg);
4966 if (pevent->long_size == 4)
4971 if (isalnum(ptr[1]))
4974 if (arg->type == TEP_PRINT_BSTRING) {
4975 trace_seq_puts(s, arg->string.string);
4980 if (*ptr == 'F' || *ptr == 'f' ||
4981 *ptr == 'S' || *ptr == 's') {
4983 } else if (*ptr == 'M' || *ptr == 'm') {
4984 print_mac_arg(s, *ptr, data, size, event, arg);
4987 } else if (*ptr == 'I' || *ptr == 'i') {
4990 n = print_ip_arg(s, ptr, data, size, event, arg);
5005 do_warning_event(event, "no argument match");
5006 event->flags |= TEP_EVENT_FL_FAILED;
5010 len = ((unsigned long)ptr + 1) -
5011 (unsigned long)saveptr;
5013 /* should never happen */
5015 do_warning_event(event, "bad format!");
5016 event->flags |= TEP_EVENT_FL_FAILED;
5020 memcpy(format, saveptr, len);
5023 val = eval_num_arg(data, size, event, arg);
5027 func = find_func(pevent, val);
5029 trace_seq_puts(s, func->func);
5030 if (show_func == 'F')
5037 if (pevent->long_size == 8 && ls == 1 &&
5038 sizeof(long) != 8) {
5041 /* make %l into %ll */
5042 if (ls == 1 && (p = strchr(format, 'l')))
5043 memmove(p+1, p, strlen(p)+1);
5044 else if (strcmp(format, "%p") == 0)
5045 strcpy(format, "0x%llx");
5051 trace_seq_printf(s, format, len_arg, (char)val);
5053 trace_seq_printf(s, format, (char)val);
5057 trace_seq_printf(s, format, len_arg, (short)val);
5059 trace_seq_printf(s, format, (short)val);
5063 trace_seq_printf(s, format, len_arg, (int)val);
5065 trace_seq_printf(s, format, (int)val);
5069 trace_seq_printf(s, format, len_arg, (long)val);
5071 trace_seq_printf(s, format, (long)val);
5075 trace_seq_printf(s, format, len_arg,
5078 trace_seq_printf(s, format, (long long)val);
5081 do_warning_event(event, "bad count (%d)", ls);
5082 event->flags |= TEP_EVENT_FL_FAILED;
5087 do_warning_event(event, "no matching argument");
5088 event->flags |= TEP_EVENT_FL_FAILED;
5092 len = ((unsigned long)ptr + 1) -
5093 (unsigned long)saveptr;
5095 /* should never happen */
5097 do_warning_event(event, "bad format!");
5098 event->flags |= TEP_EVENT_FL_FAILED;
5102 memcpy(format, saveptr, len);
5106 /* Use helper trace_seq */
5108 print_str_arg(&p, data, size, event,
5109 format, len_arg, arg);
5110 trace_seq_terminate(&p);
5111 trace_seq_puts(s, p.buffer);
5112 trace_seq_destroy(&p);
5116 trace_seq_printf(s, ">%c<", *ptr);
5120 trace_seq_putc(s, *ptr);
5123 if (event->flags & TEP_EVENT_FL_FAILED) {
5125 trace_seq_printf(s, "[FAILED TO PARSE]");
5135 * tep_data_lat_fmt - parse the data for the latency format
5136 * @pevent: a handle to the pevent
5137 * @s: the trace_seq to write to
5138 * @record: the record to read from
5140 * This parses out the Latency format (interrupts disabled,
5141 * need rescheduling, in hard/soft interrupt, preempt count
5142 * and lock depth) and places it into the trace_seq.
5144 void tep_data_lat_fmt(struct tep_handle *pevent,
5145 struct trace_seq *s, struct tep_record *record)
5147 static int check_lock_depth = 1;
5148 static int check_migrate_disable = 1;
5149 static int lock_depth_exists;
5150 static int migrate_disable_exists;
5151 unsigned int lat_flags;
5154 int migrate_disable = 0;
5157 void *data = record->data;
5159 lat_flags = parse_common_flags(pevent, data);
5160 pc = parse_common_pc(pevent, data);
5161 /* lock_depth may not always exist */
5162 if (lock_depth_exists)
5163 lock_depth = parse_common_lock_depth(pevent, data);
5164 else if (check_lock_depth) {
5165 lock_depth = parse_common_lock_depth(pevent, data);
5167 check_lock_depth = 0;
5169 lock_depth_exists = 1;
5172 /* migrate_disable may not always exist */
5173 if (migrate_disable_exists)
5174 migrate_disable = parse_common_migrate_disable(pevent, data);
5175 else if (check_migrate_disable) {
5176 migrate_disable = parse_common_migrate_disable(pevent, data);
5177 if (migrate_disable < 0)
5178 check_migrate_disable = 0;
5180 migrate_disable_exists = 1;
5183 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5184 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5186 trace_seq_printf(s, "%c%c%c",
5187 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5188 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5190 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5192 (hardirq && softirq) ? 'H' :
5193 hardirq ? 'h' : softirq ? 's' : '.');
5196 trace_seq_printf(s, "%x", pc);
5198 trace_seq_putc(s, '.');
5200 if (migrate_disable_exists) {
5201 if (migrate_disable < 0)
5202 trace_seq_putc(s, '.');
5204 trace_seq_printf(s, "%d", migrate_disable);
5207 if (lock_depth_exists) {
5209 trace_seq_putc(s, '.');
5211 trace_seq_printf(s, "%d", lock_depth);
5214 trace_seq_terminate(s);
5218 * tep_data_type - parse out the given event type
5219 * @pevent: a handle to the pevent
5220 * @rec: the record to read from
5222 * This returns the event id from the @rec.
5224 int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
5226 return trace_parse_common_type(pevent, rec->data);
5230 * tep_data_event_from_type - find the event by a given type
5231 * @pevent: a handle to the pevent
5232 * @type: the type of the event.
5234 * This returns the event form a given @type;
5236 struct tep_event *tep_data_event_from_type(struct tep_handle *pevent, int type)
5238 return tep_find_event(pevent, type);
5242 * tep_data_pid - parse the PID from record
5243 * @pevent: a handle to the pevent
5244 * @rec: the record to parse
5246 * This returns the PID from a record.
5248 int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
5250 return parse_common_pid(pevent, rec->data);
5254 * tep_data_preempt_count - parse the preempt count from the record
5255 * @pevent: a handle to the pevent
5256 * @rec: the record to parse
5258 * This returns the preempt count from a record.
5260 int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
5262 return parse_common_pc(pevent, rec->data);
5266 * tep_data_flags - parse the latency flags from the record
5267 * @pevent: a handle to the pevent
5268 * @rec: the record to parse
5270 * This returns the latency flags from a record.
5272 * Use trace_flag_type enum for the flags (see event-parse.h).
5274 int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
5276 return parse_common_flags(pevent, rec->data);
5280 * tep_data_comm_from_pid - return the command line from PID
5281 * @pevent: a handle to the pevent
5282 * @pid: the PID of the task to search for
5284 * This returns a pointer to the command line that has the given
5287 const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
5291 comm = find_cmdline(pevent, pid);
5295 static struct cmdline *
5296 pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct cmdline *next)
5298 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5301 cmdlist = cmdlist->next;
5303 cmdlist = pevent->cmdlist;
5305 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5306 cmdlist = cmdlist->next;
5308 return (struct cmdline *)cmdlist;
5312 * tep_data_pid_from_comm - return the pid from a given comm
5313 * @pevent: a handle to the pevent
5314 * @comm: the cmdline to find the pid from
5315 * @next: the cmdline structure to find the next comm
5317 * This returns the cmdline structure that holds a pid for a given
5318 * comm, or NULL if none found. As there may be more than one pid for
5319 * a given comm, the result of this call can be passed back into
5320 * a recurring call in the @next parameter, and then it will find the
5322 * Also, it does a linear search, so it may be slow.
5324 struct cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
5325 struct cmdline *next)
5327 struct cmdline *cmdline;
5330 * If the cmdlines have not been converted yet, then use
5333 if (!pevent->cmdlines)
5334 return pid_from_cmdlist(pevent, comm, next);
5338 * The next pointer could have been still from
5339 * a previous call before cmdlines were created
5341 if (next < pevent->cmdlines ||
5342 next >= pevent->cmdlines + pevent->cmdline_count)
5349 cmdline = pevent->cmdlines;
5351 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5352 if (strcmp(cmdline->comm, comm) == 0)
5360 * tep_cmdline_pid - return the pid associated to a given cmdline
5361 * @cmdline: The cmdline structure to get the pid from
5363 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5366 int tep_cmdline_pid(struct tep_handle *pevent, struct cmdline *cmdline)
5368 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5374 * If cmdlines have not been created yet, or cmdline is
5375 * not part of the array, then treat it as a cmdlist instead.
5377 if (!pevent->cmdlines ||
5378 cmdline < pevent->cmdlines ||
5379 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5380 return cmdlist->pid;
5382 return cmdline->pid;
5386 * tep_event_info - parse the data into the print format
5387 * @s: the trace_seq to write to
5388 * @event: the handle to the event
5389 * @record: the record to read from
5391 * This parses the raw @data using the given @event information and
5392 * writes the print format into the trace_seq.
5394 void tep_event_info(struct trace_seq *s, struct tep_event *event,
5395 struct tep_record *record)
5397 int print_pretty = 1;
5399 if (event->pevent->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
5400 tep_print_fields(s, record->data, record->size, event);
5403 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
5404 print_pretty = event->handler(s, record, event,
5408 pretty_print(s, record->data, record->size, event);
5411 trace_seq_terminate(s);
5414 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5416 if (!trace_clock || !use_trace_clock)
5419 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5420 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5423 /* trace_clock is setting in tsc or counter mode */
5428 * tep_find_event_by_record - return the event from a given record
5429 * @pevent: a handle to the pevent
5430 * @record: The record to get the event from
5432 * Returns the associated event for a given record, or NULL if non is
5436 tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
5440 if (record->size < 0) {
5441 do_warning("ug! negative record size %d", record->size);
5445 type = trace_parse_common_type(pevent, record->data);
5447 return tep_find_event(pevent, type);
5451 * tep_print_event_task - Write the event task comm, pid and CPU
5452 * @pevent: a handle to the pevent
5453 * @s: the trace_seq to write to
5454 * @event: the handle to the record's event
5455 * @record: The record to get the event from
5457 * Writes the tasks comm, pid and CPU to @s.
5459 void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
5460 struct tep_event *event,
5461 struct tep_record *record)
5463 void *data = record->data;
5467 pid = parse_common_pid(pevent, data);
5468 comm = find_cmdline(pevent, pid);
5470 if (pevent->latency_format) {
5471 trace_seq_printf(s, "%8.8s-%-5d %3d",
5472 comm, pid, record->cpu);
5474 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5478 * tep_print_event_time - Write the event timestamp
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
5483 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5485 * Writes the timestamp of the record into @s.
5487 void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
5488 struct tep_event *event,
5489 struct tep_record *record,
5490 bool use_trace_clock)
5493 unsigned long usecs;
5494 unsigned long nsecs;
5496 bool use_usec_format;
5498 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5500 if (use_usec_format) {
5501 secs = record->ts / NSEC_PER_SEC;
5502 nsecs = record->ts - secs * NSEC_PER_SEC;
5505 if (pevent->latency_format) {
5506 tep_data_lat_fmt(pevent, s, record);
5509 if (use_usec_format) {
5510 if (pevent->flags & TEP_NSEC_OUTPUT) {
5514 usecs = (nsecs + 500) / NSEC_PER_USEC;
5515 /* To avoid usecs larger than 1 sec */
5516 if (usecs >= USEC_PER_SEC) {
5517 usecs -= USEC_PER_SEC;
5523 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5525 trace_seq_printf(s, " %12llu:", record->ts);
5529 * tep_print_event_data - Write the event data section
5530 * @pevent: a handle to the pevent
5531 * @s: the trace_seq to write to
5532 * @event: the handle to the record's event
5533 * @record: The record to get the event from
5535 * Writes the parsing of the record's data to @s.
5537 void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
5538 struct tep_event *event,
5539 struct tep_record *record)
5541 static const char *spaces = " "; /* 20 spaces */
5544 trace_seq_printf(s, " %s: ", event->name);
5546 /* Space out the event names evenly. */
5547 len = strlen(event->name);
5549 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5551 tep_event_info(s, event, record);
5554 void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
5555 struct tep_record *record, bool use_trace_clock)
5557 struct tep_event *event;
5559 event = tep_find_event_by_record(pevent, record);
5562 int type = trace_parse_common_type(pevent, record->data);
5564 do_warning("ug! no event found for type %d", type);
5565 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5566 for (i = 0; i < record->size; i++)
5567 trace_seq_printf(s, " %02x",
5568 ((unsigned char *)record->data)[i]);
5572 tep_print_event_task(pevent, s, event, record);
5573 tep_print_event_time(pevent, s, event, record, use_trace_clock);
5574 tep_print_event_data(pevent, s, event, record);
5577 static int events_id_cmp(const void *a, const void *b)
5579 struct tep_event * const * ea = a;
5580 struct tep_event * const * eb = b;
5582 if ((*ea)->id < (*eb)->id)
5585 if ((*ea)->id > (*eb)->id)
5591 static int events_name_cmp(const void *a, const void *b)
5593 struct tep_event * const * ea = a;
5594 struct tep_event * const * eb = b;
5597 res = strcmp((*ea)->name, (*eb)->name);
5601 res = strcmp((*ea)->system, (*eb)->system);
5605 return events_id_cmp(a, b);
5608 static int events_system_cmp(const void *a, const void *b)
5610 struct tep_event * const * ea = a;
5611 struct tep_event * const * eb = b;
5614 res = strcmp((*ea)->system, (*eb)->system);
5618 res = strcmp((*ea)->name, (*eb)->name);
5622 return events_id_cmp(a, b);
5625 struct tep_event **tep_list_events(struct tep_handle *pevent, enum tep_event_sort_type sort_type)
5627 struct tep_event **events;
5628 int (*sort)(const void *a, const void *b);
5630 events = pevent->sort_events;
5632 if (events && pevent->last_type == sort_type)
5636 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5640 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5641 events[pevent->nr_events] = NULL;
5643 pevent->sort_events = events;
5645 /* the internal events are sorted by id */
5646 if (sort_type == TEP_EVENT_SORT_ID) {
5647 pevent->last_type = sort_type;
5652 switch (sort_type) {
5653 case TEP_EVENT_SORT_ID:
5654 sort = events_id_cmp;
5656 case TEP_EVENT_SORT_NAME:
5657 sort = events_name_cmp;
5659 case TEP_EVENT_SORT_SYSTEM:
5660 sort = events_system_cmp;
5666 qsort(events, pevent->nr_events, sizeof(*events), sort);
5667 pevent->last_type = sort_type;
5672 static struct tep_format_field **
5673 get_event_fields(const char *type, const char *name,
5674 int count, struct tep_format_field *list)
5676 struct tep_format_field **fields;
5677 struct tep_format_field *field;
5680 fields = malloc(sizeof(*fields) * (count + 1));
5684 for (field = list; field; field = field->next) {
5685 fields[i++] = field;
5686 if (i == count + 1) {
5687 do_warning("event %s has more %s fields than specified",
5695 do_warning("event %s has less %s fields than specified",
5704 * tep_event_common_fields - return a list of common fields for an event
5705 * @event: the event to return the common fields of.
5707 * Returns an allocated array of fields. The last item in the array is NULL.
5708 * The array must be freed with free().
5710 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
5712 return get_event_fields("common", event->name,
5713 event->format.nr_common,
5714 event->format.common_fields);
5718 * tep_event_fields - return a list of event specific fields for an event
5719 * @event: the event to return the fields of.
5721 * Returns an allocated array of fields. The last item in the array is NULL.
5722 * The array must be freed with free().
5724 struct tep_format_field **tep_event_fields(struct tep_event *event)
5726 return get_event_fields("event", event->name,
5727 event->format.nr_fields,
5728 event->format.fields);
5731 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
5733 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5735 trace_seq_puts(s, ", ");
5736 print_fields(s, field->next);
5741 static void print_args(struct tep_print_arg *args)
5743 int print_paren = 1;
5746 switch (args->type) {
5747 case TEP_PRINT_NULL:
5750 case TEP_PRINT_ATOM:
5751 printf("%s", args->atom.atom);
5753 case TEP_PRINT_FIELD:
5754 printf("REC->%s", args->field.name);
5756 case TEP_PRINT_FLAGS:
5757 printf("__print_flags(");
5758 print_args(args->flags.field);
5759 printf(", %s, ", args->flags.delim);
5761 print_fields(&s, args->flags.flags);
5762 trace_seq_do_printf(&s);
5763 trace_seq_destroy(&s);
5766 case TEP_PRINT_SYMBOL:
5767 printf("__print_symbolic(");
5768 print_args(args->symbol.field);
5771 print_fields(&s, args->symbol.symbols);
5772 trace_seq_do_printf(&s);
5773 trace_seq_destroy(&s);
5777 printf("__print_hex(");
5778 print_args(args->hex.field);
5780 print_args(args->hex.size);
5783 case TEP_PRINT_HEX_STR:
5784 printf("__print_hex_str(");
5785 print_args(args->hex.field);
5787 print_args(args->hex.size);
5790 case TEP_PRINT_INT_ARRAY:
5791 printf("__print_array(");
5792 print_args(args->int_array.field);
5794 print_args(args->int_array.count);
5796 print_args(args->int_array.el_size);
5799 case TEP_PRINT_STRING:
5800 case TEP_PRINT_BSTRING:
5801 printf("__get_str(%s)", args->string.string);
5803 case TEP_PRINT_BITMASK:
5804 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5806 case TEP_PRINT_TYPE:
5807 printf("(%s)", args->typecast.type);
5808 print_args(args->typecast.item);
5811 if (strcmp(args->op.op, ":") == 0)
5815 print_args(args->op.left);
5816 printf(" %s ", args->op.op);
5817 print_args(args->op.right);
5822 /* we should warn... */
5827 print_args(args->next);
5831 static void parse_header_field(const char *field,
5832 int *offset, int *size, int mandatory)
5834 unsigned long long save_input_buf_ptr;
5835 unsigned long long save_input_buf_siz;
5839 save_input_buf_ptr = input_buf_ptr;
5840 save_input_buf_siz = input_buf_siz;
5842 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
5844 if (read_expected(TEP_EVENT_OP, ":") < 0)
5848 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5853 * If this is not a mandatory field, then test it first.
5856 if (read_expected(TEP_EVENT_ITEM, field) < 0)
5859 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5861 if (strcmp(token, field) != 0)
5866 if (read_expected(TEP_EVENT_OP, ";") < 0)
5868 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
5870 if (read_expected(TEP_EVENT_OP, ":") < 0)
5872 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5874 *offset = atoi(token);
5876 if (read_expected(TEP_EVENT_OP, ";") < 0)
5878 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
5880 if (read_expected(TEP_EVENT_OP, ":") < 0)
5882 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5884 *size = atoi(token);
5886 if (read_expected(TEP_EVENT_OP, ";") < 0)
5888 type = read_token(&token);
5889 if (type != TEP_EVENT_NEWLINE) {
5890 /* newer versions of the kernel have a "signed" type */
5891 if (type != TEP_EVENT_ITEM)
5894 if (strcmp(token, "signed") != 0)
5899 if (read_expected(TEP_EVENT_OP, ":") < 0)
5902 if (read_expect_type(TEP_EVENT_ITEM, &token))
5906 if (read_expected(TEP_EVENT_OP, ";") < 0)
5909 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
5917 input_buf_ptr = save_input_buf_ptr;
5918 input_buf_siz = save_input_buf_siz;
5925 * tep_parse_header_page - parse the data stored in the header page
5926 * @pevent: the handle to the pevent
5927 * @buf: the buffer storing the header page format string
5928 * @size: the size of @buf
5929 * @long_size: the long size to use if there is no header
5931 * This parses the header page format for information on the
5932 * ring buffer used. The @buf should be copied from
5934 * /sys/kernel/debug/tracing/events/header_page
5936 int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
5943 * Old kernels did not have header page info.
5944 * Sorry but we just use what we find here in user space.
5946 pevent->header_page_ts_size = sizeof(long long);
5947 pevent->header_page_size_size = long_size;
5948 pevent->header_page_data_offset = sizeof(long long) + long_size;
5949 pevent->old_format = 1;
5952 init_input_buf(buf, size);
5954 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5955 &pevent->header_page_ts_size, 1);
5956 parse_header_field("commit", &pevent->header_page_size_offset,
5957 &pevent->header_page_size_size, 1);
5958 parse_header_field("overwrite", &pevent->header_page_overwrite,
5960 parse_header_field("data", &pevent->header_page_data_offset,
5961 &pevent->header_page_data_size, 1);
5966 static int event_matches(struct tep_event *event,
5967 int id, const char *sys_name,
5968 const char *event_name)
5970 if (id >= 0 && id != event->id)
5973 if (event_name && (strcmp(event_name, event->name) != 0))
5976 if (sys_name && (strcmp(sys_name, event->system) != 0))
5982 static void free_handler(struct event_handler *handle)
5984 free((void *)handle->sys_name);
5985 free((void *)handle->event_name);
5989 static int find_event_handle(struct tep_handle *pevent, struct tep_event *event)
5991 struct event_handler *handle, **next;
5993 for (next = &pevent->handlers; *next;
5994 next = &(*next)->next) {
5996 if (event_matches(event, handle->id,
5998 handle->event_name))
6005 pr_stat("overriding event (%d) %s:%s with new print handler",
6006 event->id, event->system, event->name);
6008 event->handler = handle->func;
6009 event->context = handle->context;
6011 *next = handle->next;
6012 free_handler(handle);
6018 * __tep_parse_format - parse the event format
6019 * @buf: the buffer storing the event format string
6020 * @size: the size of @buf
6021 * @sys: the system the event belongs to
6023 * This parses the event format and creates an event structure
6024 * to quickly parse raw data for a given event.
6026 * These files currently come from:
6028 * /sys/kernel/debug/tracing/events/.../.../format
6030 enum tep_errno __tep_parse_format(struct tep_event **eventp,
6031 struct tep_handle *pevent, const char *buf,
6032 unsigned long size, const char *sys)
6034 struct tep_event *event;
6037 init_input_buf(buf, size);
6039 *eventp = event = alloc_event();
6041 return TEP_ERRNO__MEM_ALLOC_FAILED;
6043 event->name = event_read_name();
6046 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6047 goto event_alloc_failed;
6050 if (strcmp(sys, "ftrace") == 0) {
6051 event->flags |= TEP_EVENT_FL_ISFTRACE;
6053 if (strcmp(event->name, "bprint") == 0)
6054 event->flags |= TEP_EVENT_FL_ISBPRINT;
6057 event->id = event_read_id();
6058 if (event->id < 0) {
6059 ret = TEP_ERRNO__READ_ID_FAILED;
6061 * This isn't an allocation error actually.
6062 * But as the ID is critical, just bail out.
6064 goto event_alloc_failed;
6067 event->system = strdup(sys);
6068 if (!event->system) {
6069 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6070 goto event_alloc_failed;
6073 /* Add pevent to event so that it can be referenced */
6074 event->pevent = pevent;
6076 ret = event_read_format(event);
6078 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6079 goto event_parse_failed;
6083 * If the event has an override, don't print warnings if the event
6084 * print format fails to parse.
6086 if (pevent && find_event_handle(pevent, event))
6089 ret = event_read_print(event);
6093 ret = TEP_ERRNO__READ_PRINT_FAILED;
6094 goto event_parse_failed;
6097 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6098 struct tep_format_field *field;
6099 struct tep_print_arg *arg, **list;
6101 /* old ftrace had no args */
6102 list = &event->print_fmt.args;
6103 for (field = event->format.fields; field; field = field->next) {
6106 event->flags |= TEP_EVENT_FL_FAILED;
6107 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6109 arg->type = TEP_PRINT_FIELD;
6110 arg->field.name = strdup(field->name);
6111 if (!arg->field.name) {
6112 event->flags |= TEP_EVENT_FL_FAILED;
6114 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6116 arg->field.field = field;
6126 event->flags |= TEP_EVENT_FL_FAILED;
6130 free(event->system);
6137 static enum tep_errno
6138 __parse_event(struct tep_handle *pevent,
6139 struct tep_event **eventp,
6140 const char *buf, unsigned long size,
6143 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
6144 struct tep_event *event = *eventp;
6149 if (pevent && add_event(pevent, event)) {
6150 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6151 goto event_add_failed;
6154 #define PRINT_ARGS 0
6155 if (PRINT_ARGS && event->print_fmt.args)
6156 print_args(event->print_fmt.args);
6161 tep_free_event(event);
6166 * tep_parse_format - parse the event format
6167 * @pevent: the handle to the pevent
6168 * @eventp: returned format
6169 * @buf: the buffer storing the event format string
6170 * @size: the size of @buf
6171 * @sys: the system the event belongs to
6173 * This parses the event format and creates an event structure
6174 * to quickly parse raw data for a given event.
6176 * These files currently come from:
6178 * /sys/kernel/debug/tracing/events/.../.../format
6180 enum tep_errno tep_parse_format(struct tep_handle *pevent,
6181 struct tep_event **eventp,
6183 unsigned long size, const char *sys)
6185 return __parse_event(pevent, eventp, buf, size, sys);
6189 * tep_parse_event - parse the event format
6190 * @pevent: the handle to the pevent
6191 * @buf: the buffer storing the event format string
6192 * @size: the size of @buf
6193 * @sys: the system the event belongs to
6195 * This parses the event format and creates an event structure
6196 * to quickly parse raw data for a given event.
6198 * These files currently come from:
6200 * /sys/kernel/debug/tracing/events/.../.../format
6202 enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
6203 unsigned long size, const char *sys)
6205 struct tep_event *event = NULL;
6206 return __parse_event(pevent, &event, buf, size, sys);
6209 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6210 const char *name, struct tep_record *record,
6211 unsigned long long *val, int err)
6215 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6219 if (tep_read_number_field(field, record->data, val)) {
6221 trace_seq_printf(s, " %s=INVALID", name);
6229 * tep_get_field_raw - return the raw pointer into the data field
6230 * @s: The seq to print to on error
6231 * @event: the event that the field is for
6232 * @name: The name of the field
6233 * @record: The record with the field name.
6234 * @len: place to store the field length.
6235 * @err: print default error if failed.
6237 * Returns a pointer into record->data of the field and places
6238 * the length of the field in @len.
6240 * On failure, it returns NULL.
6242 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6243 const char *name, struct tep_record *record,
6246 struct tep_format_field *field;
6247 void *data = record->data;
6254 field = tep_find_field(event, name);
6258 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6262 /* Allow @len to be NULL */
6266 offset = field->offset;
6267 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
6268 offset = tep_read_number(event->pevent,
6269 data + offset, field->size);
6270 *len = offset >> 16;
6275 return data + offset;
6279 * tep_get_field_val - find a field and return its value
6280 * @s: The seq to print to on error
6281 * @event: the event that the field is for
6282 * @name: The name of the field
6283 * @record: The record with the field name.
6284 * @val: place to store the value of the field.
6285 * @err: print default error if failed.
6287 * Returns 0 on success -1 on field not found.
6289 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
6290 const char *name, struct tep_record *record,
6291 unsigned long long *val, int err)
6293 struct tep_format_field *field;
6298 field = tep_find_field(event, name);
6300 return get_field_val(s, field, name, record, val, err);
6304 * tep_get_common_field_val - find a common field and return its value
6305 * @s: The seq to print to on error
6306 * @event: the event that the field is for
6307 * @name: The name of the field
6308 * @record: The record with the field name.
6309 * @val: place to store the value of the field.
6310 * @err: print default error if failed.
6312 * Returns 0 on success -1 on field not found.
6314 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
6315 const char *name, struct tep_record *record,
6316 unsigned long long *val, int err)
6318 struct tep_format_field *field;
6323 field = tep_find_common_field(event, name);
6325 return get_field_val(s, field, name, record, val, err);
6329 * tep_get_any_field_val - find a any field and return its value
6330 * @s: The seq to print to on error
6331 * @event: the event that the field is for
6332 * @name: The name of the field
6333 * @record: The record with the field name.
6334 * @val: place to store the value of the field.
6335 * @err: print default error if failed.
6337 * Returns 0 on success -1 on field not found.
6339 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
6340 const char *name, struct tep_record *record,
6341 unsigned long long *val, int err)
6343 struct tep_format_field *field;
6348 field = tep_find_any_field(event, name);
6350 return get_field_val(s, field, name, record, val, err);
6354 * tep_print_num_field - print a field and a format
6355 * @s: The seq to print to
6356 * @fmt: The printf format to print the field with.
6357 * @event: the event that the field is for
6358 * @name: The name of the field
6359 * @record: The record with the field name.
6360 * @err: print default error if failed.
6362 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6364 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6365 struct tep_event *event, const char *name,
6366 struct tep_record *record, int err)
6368 struct tep_format_field *field = tep_find_field(event, name);
6369 unsigned long long val;
6374 if (tep_read_number_field(field, record->data, &val))
6377 return trace_seq_printf(s, fmt, val);
6381 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6386 * tep_print_func_field - print a field and a format for function pointers
6387 * @s: The seq to print to
6388 * @fmt: The printf format to print the field with.
6389 * @event: the event that the field is for
6390 * @name: The name of the field
6391 * @record: The record with the field name.
6392 * @err: print default error if failed.
6394 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6396 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6397 struct tep_event *event, const char *name,
6398 struct tep_record *record, int err)
6400 struct tep_format_field *field = tep_find_field(event, name);
6401 struct tep_handle *pevent = event->pevent;
6402 unsigned long long val;
6403 struct func_map *func;
6409 if (tep_read_number_field(field, record->data, &val))
6412 func = find_func(pevent, val);
6415 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6417 sprintf(tmp, "0x%08llx", val);
6419 return trace_seq_printf(s, fmt, tmp);
6423 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6427 static void free_func_handle(struct tep_function_handler *func)
6429 struct func_params *params;
6433 while (func->params) {
6434 params = func->params;
6435 func->params = params->next;
6443 * tep_register_print_function - register a helper function
6444 * @pevent: the handle to the pevent
6445 * @func: the function to process the helper function
6446 * @ret_type: the return type of the helper function
6447 * @name: the name of the helper function
6448 * @parameters: A list of enum tep_func_arg_type
6450 * Some events may have helper functions in the print format arguments.
6451 * This allows a plugin to dynamically create a way to process one
6452 * of these functions.
6454 * The @parameters is a variable list of tep_func_arg_type enums that
6455 * must end with TEP_FUNC_ARG_VOID.
6457 int tep_register_print_function(struct tep_handle *pevent,
6458 tep_func_handler func,
6459 enum tep_func_arg_type ret_type,
6462 struct tep_function_handler *func_handle;
6463 struct func_params **next_param;
6464 struct func_params *param;
6465 enum tep_func_arg_type type;
6469 func_handle = find_func_handler(pevent, name);
6472 * This is most like caused by the users own
6473 * plugins updating the function. This overrides the
6476 pr_stat("override of function helper '%s'", name);
6477 remove_func_handler(pevent, name);
6480 func_handle = calloc(1, sizeof(*func_handle));
6482 do_warning("Failed to allocate function handler");
6483 return TEP_ERRNO__MEM_ALLOC_FAILED;
6486 func_handle->ret_type = ret_type;
6487 func_handle->name = strdup(name);
6488 func_handle->func = func;
6489 if (!func_handle->name) {
6490 do_warning("Failed to allocate function name");
6492 return TEP_ERRNO__MEM_ALLOC_FAILED;
6495 next_param = &(func_handle->params);
6498 type = va_arg(ap, enum tep_func_arg_type);
6499 if (type == TEP_FUNC_ARG_VOID)
6502 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6503 do_warning("Invalid argument type %d", type);
6504 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6508 param = malloc(sizeof(*param));
6510 do_warning("Failed to allocate function param");
6511 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6517 *next_param = param;
6518 next_param = &(param->next);
6520 func_handle->nr_args++;
6524 func_handle->next = pevent->func_handlers;
6525 pevent->func_handlers = func_handle;
6530 free_func_handle(func_handle);
6535 * tep_unregister_print_function - unregister a helper function
6536 * @pevent: the handle to the pevent
6537 * @func: the function to process the helper function
6538 * @name: the name of the helper function
6540 * This function removes existing print handler for function @name.
6542 * Returns 0 if the handler was removed successully, -1 otherwise.
6544 int tep_unregister_print_function(struct tep_handle *pevent,
6545 tep_func_handler func, char *name)
6547 struct tep_function_handler *func_handle;
6549 func_handle = find_func_handler(pevent, name);
6550 if (func_handle && func_handle->func == func) {
6551 remove_func_handler(pevent, name);
6557 static struct tep_event *search_event(struct tep_handle *pevent, int id,
6558 const char *sys_name,
6559 const char *event_name)
6561 struct tep_event *event;
6565 event = tep_find_event(pevent, id);
6568 if (event_name && (strcmp(event_name, event->name) != 0))
6570 if (sys_name && (strcmp(sys_name, event->system) != 0))
6573 event = tep_find_event_by_name(pevent, sys_name, event_name);
6581 * tep_register_event_handler - register a way to parse an event
6582 * @pevent: the handle to the pevent
6583 * @id: the id of the event to register
6584 * @sys_name: the system name the event belongs to
6585 * @event_name: the name of the event
6586 * @func: the function to call to parse the event information
6587 * @context: the data to be passed to @func
6589 * This function allows a developer to override the parsing of
6590 * a given event. If for some reason the default print format
6591 * is not sufficient, this function will register a function
6592 * for an event to be used to parse the data instead.
6594 * If @id is >= 0, then it is used to find the event.
6595 * else @sys_name and @event_name are used.
6597 int tep_register_event_handler(struct tep_handle *pevent, int id,
6598 const char *sys_name, const char *event_name,
6599 tep_event_handler_func func, void *context)
6601 struct tep_event *event;
6602 struct event_handler *handle;
6604 event = search_event(pevent, id, sys_name, event_name);
6608 pr_stat("overriding event (%d) %s:%s with new print handler",
6609 event->id, event->system, event->name);
6611 event->handler = func;
6612 event->context = context;
6616 /* Save for later use. */
6617 handle = calloc(1, sizeof(*handle));
6619 do_warning("Failed to allocate event handler");
6620 return TEP_ERRNO__MEM_ALLOC_FAILED;
6625 handle->event_name = strdup(event_name);
6627 handle->sys_name = strdup(sys_name);
6629 if ((event_name && !handle->event_name) ||
6630 (sys_name && !handle->sys_name)) {
6631 do_warning("Failed to allocate event/sys name");
6632 free((void *)handle->event_name);
6633 free((void *)handle->sys_name);
6635 return TEP_ERRNO__MEM_ALLOC_FAILED;
6638 handle->func = func;
6639 handle->next = pevent->handlers;
6640 pevent->handlers = handle;
6641 handle->context = context;
6646 static int handle_matches(struct event_handler *handler, int id,
6647 const char *sys_name, const char *event_name,
6648 tep_event_handler_func func, void *context)
6650 if (id >= 0 && id != handler->id)
6653 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6656 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6659 if (func != handler->func || context != handler->context)
6666 * tep_unregister_event_handler - unregister an existing event handler
6667 * @pevent: the handle to the pevent
6668 * @id: the id of the event to unregister
6669 * @sys_name: the system name the handler belongs to
6670 * @event_name: the name of the event handler
6671 * @func: the function to call to parse the event information
6672 * @context: the data to be passed to @func
6674 * This function removes existing event handler (parser).
6676 * If @id is >= 0, then it is used to find the event.
6677 * else @sys_name and @event_name are used.
6679 * Returns 0 if handler was removed successfully, -1 if event was not found.
6681 int tep_unregister_event_handler(struct tep_handle *pevent, int id,
6682 const char *sys_name, const char *event_name,
6683 tep_event_handler_func func, void *context)
6685 struct tep_event *event;
6686 struct event_handler *handle;
6687 struct event_handler **next;
6689 event = search_event(pevent, id, sys_name, event_name);
6693 if (event->handler == func && event->context == context) {
6694 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6695 event->id, event->system, event->name);
6697 event->handler = NULL;
6698 event->context = NULL;
6703 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6705 if (handle_matches(handle, id, sys_name, event_name,
6713 *next = handle->next;
6714 free_handler(handle);
6720 * tep_alloc - create a pevent handle
6722 struct tep_handle *tep_alloc(void)
6724 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
6727 pevent->ref_count = 1;
6732 void tep_ref(struct tep_handle *pevent)
6734 pevent->ref_count++;
6737 int tep_get_ref(struct tep_handle *tep)
6740 return tep->ref_count;
6744 void tep_free_format_field(struct tep_format_field *field)
6747 if (field->alias != field->name)
6753 static void free_format_fields(struct tep_format_field *field)
6755 struct tep_format_field *next;
6759 tep_free_format_field(field);
6764 static void free_formats(struct tep_format *format)
6766 free_format_fields(format->common_fields);
6767 free_format_fields(format->fields);
6770 void tep_free_event(struct tep_event *event)
6773 free(event->system);
6775 free_formats(&event->format);
6777 free(event->print_fmt.format);
6778 free_args(event->print_fmt.args);
6784 * tep_free - free a pevent handle
6785 * @pevent: the pevent handle to free
6787 void tep_free(struct tep_handle *pevent)
6789 struct cmdline_list *cmdlist, *cmdnext;
6790 struct func_list *funclist, *funcnext;
6791 struct printk_list *printklist, *printknext;
6792 struct tep_function_handler *func_handler;
6793 struct event_handler *handle;
6799 cmdlist = pevent->cmdlist;
6800 funclist = pevent->funclist;
6801 printklist = pevent->printklist;
6803 pevent->ref_count--;
6804 if (pevent->ref_count)
6807 if (pevent->cmdlines) {
6808 for (i = 0; i < pevent->cmdline_count; i++)
6809 free(pevent->cmdlines[i].comm);
6810 free(pevent->cmdlines);
6814 cmdnext = cmdlist->next;
6815 free(cmdlist->comm);
6820 if (pevent->func_map) {
6821 for (i = 0; i < (int)pevent->func_count; i++) {
6822 free(pevent->func_map[i].func);
6823 free(pevent->func_map[i].mod);
6825 free(pevent->func_map);
6829 funcnext = funclist->next;
6830 free(funclist->func);
6831 free(funclist->mod);
6833 funclist = funcnext;
6836 while (pevent->func_handlers) {
6837 func_handler = pevent->func_handlers;
6838 pevent->func_handlers = func_handler->next;
6839 free_func_handle(func_handler);
6842 if (pevent->printk_map) {
6843 for (i = 0; i < (int)pevent->printk_count; i++)
6844 free(pevent->printk_map[i].printk);
6845 free(pevent->printk_map);
6848 while (printklist) {
6849 printknext = printklist->next;
6850 free(printklist->printk);
6852 printklist = printknext;
6855 for (i = 0; i < pevent->nr_events; i++)
6856 tep_free_event(pevent->events[i]);
6858 while (pevent->handlers) {
6859 handle = pevent->handlers;
6860 pevent->handlers = handle->next;
6861 free_handler(handle);
6864 free(pevent->trace_clock);
6865 free(pevent->events);
6866 free(pevent->sort_events);
6867 free(pevent->func_resolver);
6872 void tep_unref(struct tep_handle *pevent)