1 // SPDX-License-Identifier: LGPL-2.1
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
21 #include <linux/time64.h>
23 #include <netinet/in.h>
24 #include "event-parse.h"
26 #include "event-parse-local.h"
27 #include "event-utils.h"
28 #include "trace-seq.h"
30 static const char *input_buf;
31 static unsigned long long input_buf_ptr;
32 static unsigned long long input_buf_siz;
34 static int is_flag_field;
35 static int is_symbolic_field;
37 static int show_warning = 1;
39 #define do_warning(fmt, ...) \
42 warning(fmt, ##__VA_ARGS__); \
45 #define do_warning_event(event, fmt, ...) \
51 warning("[%s:%s] " fmt, event->system, \
52 event->name, ##__VA_ARGS__); \
54 warning(fmt, ##__VA_ARGS__); \
57 static void init_input_buf(const char *buf, unsigned long long size)
64 const char *tep_get_input_buf(void)
69 unsigned long long tep_get_input_buf_ptr(void)
74 struct event_handler {
75 struct event_handler *next;
78 const char *event_name;
79 tep_event_handler_func func;
84 struct func_params *next;
85 enum tep_func_arg_type type;
88 struct tep_function_handler {
89 struct tep_function_handler *next;
90 enum tep_func_arg_type ret_type;
92 tep_func_handler func;
93 struct func_params *params;
97 static unsigned long long
98 process_defined_func(struct trace_seq *s, void *data, int size,
99 struct tep_event *event, struct tep_print_arg *arg);
101 static void free_func_handle(struct tep_function_handler *func);
104 * tep_buffer_init - init buffer for parsing
105 * @buf: buffer to parse
106 * @size: the size of the buffer
108 * For use with tep_read_token(), this initializes the internal
109 * buffer that tep_read_token() will parse.
111 void tep_buffer_init(const char *buf, unsigned long long size)
113 init_input_buf(buf, size);
116 void breakpoint(void)
122 struct tep_print_arg *alloc_arg(void)
124 return calloc(1, sizeof(struct tep_print_arg));
132 static int cmdline_cmp(const void *a, const void *b)
134 const struct tep_cmdline *ca = a;
135 const struct tep_cmdline *cb = b;
137 if (ca->pid < cb->pid)
139 if (ca->pid > cb->pid)
145 struct cmdline_list {
146 struct cmdline_list *next;
151 static int cmdline_init(struct tep_handle *tep)
153 struct cmdline_list *cmdlist = tep->cmdlist;
154 struct cmdline_list *item;
155 struct tep_cmdline *cmdlines;
158 cmdlines = malloc(sizeof(*cmdlines) * tep->cmdline_count);
164 cmdlines[i].pid = cmdlist->pid;
165 cmdlines[i].comm = cmdlist->comm;
168 cmdlist = cmdlist->next;
172 qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
174 tep->cmdlines = cmdlines;
180 static const char *find_cmdline(struct tep_handle *tep, int pid)
182 const struct tep_cmdline *comm;
183 struct tep_cmdline key;
188 if (!tep->cmdlines && cmdline_init(tep))
189 return "<not enough memory for cmdlines!>";
193 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
194 sizeof(*tep->cmdlines), cmdline_cmp);
202 * tep_is_pid_registered - return if a pid has a cmdline registered
203 * @tep: a handle to the trace event parser context
204 * @pid: The pid to check if it has a cmdline registered with.
206 * Returns true if the pid has a cmdline mapped to it
209 bool tep_is_pid_registered(struct tep_handle *tep, int pid)
211 const struct tep_cmdline *comm;
212 struct tep_cmdline key;
217 if (!tep->cmdlines && cmdline_init(tep))
222 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
223 sizeof(*tep->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 *tep,
236 const char *comm, int pid, bool override)
238 struct tep_cmdline *cmdlines = tep->cmdlines;
239 struct tep_cmdline *cmdline;
240 struct tep_cmdline key;
246 /* avoid duplicates */
249 cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count,
250 sizeof(*tep->cmdlines), cmdline_cmp);
256 new_comm = strdup(comm);
262 cmdline->comm = new_comm;
267 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1));
273 cmdlines[tep->cmdline_count].comm = strdup(comm);
274 if (!cmdlines[tep->cmdline_count].comm) {
280 cmdlines[tep->cmdline_count].pid = pid;
282 if (cmdlines[tep->cmdline_count].comm)
283 tep->cmdline_count++;
285 qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
286 tep->cmdlines = cmdlines;
291 static int _tep_register_comm(struct tep_handle *tep,
292 const char *comm, int pid, bool override)
294 struct cmdline_list *item;
297 return add_new_comm(tep, comm, pid, override);
299 item = malloc(sizeof(*item));
304 item->comm = strdup(comm);
306 item->comm = strdup("<...>");
312 item->next = tep->cmdlist;
315 tep->cmdline_count++;
321 * tep_register_comm - register a pid / comm mapping
322 * @tep: a handle to the trace event parser context
323 * @comm: the command line to register
324 * @pid: the pid to map the command line to
326 * This adds a mapping to search for command line names with
327 * a given pid. The comm is duplicated. If a command with the same pid
328 * already exist, -1 is returned and errno is set to EEXIST
330 int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
332 return _tep_register_comm(tep, comm, pid, false);
336 * tep_override_comm - register a pid / comm mapping
337 * @tep: a handle to the trace event parser context
338 * @comm: the command line to register
339 * @pid: the pid to map the command line to
341 * This adds a mapping to search for command line names with
342 * a given pid. The comm is duplicated. If a command with the same pid
343 * already exist, the command string is udapted with the new one
345 int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
347 if (!tep->cmdlines && cmdline_init(tep)) {
351 return _tep_register_comm(tep, comm, pid, true);
354 int tep_register_trace_clock(struct tep_handle *tep, const char *trace_clock)
356 tep->trace_clock = strdup(trace_clock);
357 if (!tep->trace_clock) {
365 unsigned long long addr;
371 struct func_list *next;
372 unsigned long long addr;
377 static int func_cmp(const void *a, const void *b)
379 const struct func_map *fa = a;
380 const struct func_map *fb = b;
382 if (fa->addr < fb->addr)
384 if (fa->addr > fb->addr)
391 * We are searching for a record in between, not an exact
394 static int func_bcmp(const void *a, const void *b)
396 const struct func_map *fa = a;
397 const struct func_map *fb = b;
399 if ((fa->addr == fb->addr) ||
401 (fa->addr > fb->addr &&
402 fa->addr < (fb+1)->addr))
405 if (fa->addr < fb->addr)
411 static int func_map_init(struct tep_handle *tep)
413 struct func_list *funclist;
414 struct func_list *item;
415 struct func_map *func_map;
418 func_map = malloc(sizeof(*func_map) * (tep->func_count + 1));
422 funclist = tep->funclist;
426 func_map[i].func = funclist->func;
427 func_map[i].addr = funclist->addr;
428 func_map[i].mod = funclist->mod;
431 funclist = funclist->next;
435 qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp);
438 * Add a special record at the end.
440 func_map[tep->func_count].func = NULL;
441 func_map[tep->func_count].addr = 0;
442 func_map[tep->func_count].mod = NULL;
444 tep->func_map = func_map;
445 tep->funclist = NULL;
450 static struct func_map *
451 __find_func(struct tep_handle *tep, unsigned long long addr)
453 struct func_map *func;
461 func = bsearch(&key, tep->func_map, tep->func_count,
462 sizeof(*tep->func_map), func_bcmp);
467 struct func_resolver {
468 tep_func_resolver_t *func;
474 * tep_set_function_resolver - set an alternative function resolver
475 * @tep: a handle to the trace event parser context
476 * @resolver: function to be used
477 * @priv: resolver function private state.
479 * Some tools may have already a way to resolve kernel functions, allow them to
480 * keep using it instead of duplicating all the entries inside tep->funclist.
482 int tep_set_function_resolver(struct tep_handle *tep,
483 tep_func_resolver_t *func, void *priv)
485 struct func_resolver *resolver = malloc(sizeof(*resolver));
487 if (resolver == NULL)
490 resolver->func = func;
491 resolver->priv = priv;
493 free(tep->func_resolver);
494 tep->func_resolver = resolver;
500 * tep_reset_function_resolver - reset alternative function resolver
501 * @tep: a handle to the trace event parser context
503 * Stop using whatever alternative resolver was set, use the default
506 void tep_reset_function_resolver(struct tep_handle *tep)
508 free(tep->func_resolver);
509 tep->func_resolver = NULL;
512 static struct func_map *
513 find_func(struct tep_handle *tep, unsigned long long addr)
515 struct func_map *map;
517 if (!tep->func_resolver)
518 return __find_func(tep, addr);
520 map = &tep->func_resolver->map;
523 map->func = tep->func_resolver->func(tep->func_resolver->priv,
524 &map->addr, &map->mod);
525 if (map->func == NULL)
532 * tep_find_function - find a function by a given address
533 * @tep: a handle to the trace event parser context
534 * @addr: the address to find the function with
536 * Returns a pointer to the function stored that has the given
537 * address. Note, the address does not have to be exact, it
538 * will select the function that would contain the address.
540 const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
542 struct func_map *map;
544 map = find_func(tep, addr);
552 * tep_find_function_address - find a function address by a given address
553 * @tep: a handle to the trace event parser context
554 * @addr: the address to find the function with
556 * Returns the address the function starts at. This can be used in
557 * conjunction with tep_find_function to print both the function
558 * name and the function offset.
561 tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
563 struct func_map *map;
565 map = find_func(tep, addr);
573 * tep_register_function - register a function with a given address
574 * @tep: a handle to the trace event parser context
575 * @function: the function name to register
576 * @addr: the address the function starts at
577 * @mod: the kernel module the function may be in (NULL for none)
579 * This registers a function name with an address and module.
580 * The @func passed in is duplicated.
582 int tep_register_function(struct tep_handle *tep, char *func,
583 unsigned long long addr, char *mod)
585 struct func_list *item = malloc(sizeof(*item));
590 item->next = tep->funclist;
591 item->func = strdup(func);
596 item->mod = strdup(mod);
603 tep->funclist = item;
618 * tep_print_funcs - print out the stored functions
619 * @tep: a handle to the trace event parser context
621 * This prints out the stored functions.
623 void tep_print_funcs(struct tep_handle *tep)
630 for (i = 0; i < (int)tep->func_count; i++) {
632 tep->func_map[i].addr,
633 tep->func_map[i].func);
634 if (tep->func_map[i].mod)
635 printf(" [%s]\n", tep->func_map[i].mod);
642 unsigned long long addr;
647 struct printk_list *next;
648 unsigned long long addr;
652 static int printk_cmp(const void *a, const void *b)
654 const struct printk_map *pa = a;
655 const struct printk_map *pb = b;
657 if (pa->addr < pb->addr)
659 if (pa->addr > pb->addr)
665 static int printk_map_init(struct tep_handle *tep)
667 struct printk_list *printklist;
668 struct printk_list *item;
669 struct printk_map *printk_map;
672 printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1));
676 printklist = tep->printklist;
680 printk_map[i].printk = printklist->printk;
681 printk_map[i].addr = printklist->addr;
684 printklist = printklist->next;
688 qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp);
690 tep->printk_map = printk_map;
691 tep->printklist = NULL;
696 static struct printk_map *
697 find_printk(struct tep_handle *tep, unsigned long long addr)
699 struct printk_map *printk;
700 struct printk_map key;
702 if (!tep->printk_map && printk_map_init(tep))
707 printk = bsearch(&key, tep->printk_map, tep->printk_count,
708 sizeof(*tep->printk_map), printk_cmp);
714 * tep_register_print_string - register a string by its address
715 * @tep: a handle to the trace event parser context
716 * @fmt: the string format to register
717 * @addr: the address the string was located at
719 * This registers a string by the address it was stored in the kernel.
720 * The @fmt passed in is duplicated.
722 int tep_register_print_string(struct tep_handle *tep, const char *fmt,
723 unsigned long long addr)
725 struct printk_list *item = malloc(sizeof(*item));
731 item->next = tep->printklist;
734 /* Strip off quotes and '\n' from the end */
737 item->printk = strdup(fmt);
741 p = item->printk + strlen(item->printk) - 1;
746 if (strcmp(p, "\\n") == 0)
749 tep->printklist = item;
761 * tep_print_printk - print out the stored strings
762 * @tep: a handle to the trace event parser context
764 * This prints the string formats that were stored.
766 void tep_print_printk(struct tep_handle *tep)
770 if (!tep->printk_map)
771 printk_map_init(tep);
773 for (i = 0; i < (int)tep->printk_count; i++) {
774 printf("%016llx %s\n",
775 tep->printk_map[i].addr,
776 tep->printk_map[i].printk);
780 static struct tep_event *alloc_event(void)
782 return calloc(1, sizeof(struct tep_event));
785 static int add_event(struct tep_handle *tep, struct tep_event *event)
788 struct tep_event **events = realloc(tep->events, sizeof(event) *
789 (tep->nr_events + 1));
793 tep->events = events;
795 for (i = 0; i < tep->nr_events; i++) {
796 if (tep->events[i]->id > event->id)
799 if (i < tep->nr_events)
800 memmove(&tep->events[i + 1],
802 sizeof(event) * (tep->nr_events - i));
804 tep->events[i] = event;
812 static int event_item_type(enum tep_event_type type)
815 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
817 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
823 static void free_flag_sym(struct tep_print_flag_sym *fsym)
825 struct tep_print_flag_sym *next;
836 static void free_arg(struct tep_print_arg *arg)
838 struct tep_print_arg *farg;
845 free(arg->atom.atom);
847 case TEP_PRINT_FIELD:
848 free(arg->field.name);
850 case TEP_PRINT_FLAGS:
851 free_arg(arg->flags.field);
852 free(arg->flags.delim);
853 free_flag_sym(arg->flags.flags);
855 case TEP_PRINT_SYMBOL:
856 free_arg(arg->symbol.field);
857 free_flag_sym(arg->symbol.symbols);
860 case TEP_PRINT_HEX_STR:
861 free_arg(arg->hex.field);
862 free_arg(arg->hex.size);
864 case TEP_PRINT_INT_ARRAY:
865 free_arg(arg->int_array.field);
866 free_arg(arg->int_array.count);
867 free_arg(arg->int_array.el_size);
870 free(arg->typecast.type);
871 free_arg(arg->typecast.item);
873 case TEP_PRINT_STRING:
874 case TEP_PRINT_BSTRING:
875 free(arg->string.string);
877 case TEP_PRINT_BITMASK:
878 free(arg->bitmask.bitmask);
880 case TEP_PRINT_DYNAMIC_ARRAY:
881 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
882 free(arg->dynarray.index);
886 free_arg(arg->op.left);
887 free_arg(arg->op.right);
890 while (arg->func.args) {
891 farg = arg->func.args;
892 arg->func.args = farg->next;
905 static enum tep_event_type get_type(int ch)
908 return TEP_EVENT_NEWLINE;
910 return TEP_EVENT_SPACE;
911 if (isalnum(ch) || ch == '_')
912 return TEP_EVENT_ITEM;
914 return TEP_EVENT_SQUOTE;
916 return TEP_EVENT_DQUOTE;
918 return TEP_EVENT_NONE;
919 if (ch == '(' || ch == ')' || ch == ',')
920 return TEP_EVENT_DELIM;
925 static int __read_char(void)
927 if (input_buf_ptr >= input_buf_siz)
930 return input_buf[input_buf_ptr++];
933 static int __peek_char(void)
935 if (input_buf_ptr >= input_buf_siz)
938 return input_buf[input_buf_ptr];
942 * tep_peek_char - peek at the next character that will be read
944 * Returns the next character read, or -1 if end of buffer.
946 int tep_peek_char(void)
948 return __peek_char();
951 static int extend_token(char **tok, char *buf, int size)
953 char *newtok = realloc(*tok, size);
970 static enum tep_event_type force_token(const char *str, char **tok);
972 static enum tep_event_type __read_token(char **tok)
975 int ch, last_ch, quote_ch, next_ch;
978 enum tep_event_type type;
985 return TEP_EVENT_NONE;
988 if (type == TEP_EVENT_NONE)
994 case TEP_EVENT_NEWLINE:
995 case TEP_EVENT_DELIM:
996 if (asprintf(tok, "%c", ch) < 0)
997 return TEP_EVENT_ERROR;
1004 next_ch = __peek_char();
1005 if (next_ch == '>') {
1006 buf[i++] = __read_char();
1019 buf[i++] = __read_char();
1031 default: /* what should we do instead? */
1041 buf[i++] = __read_char();
1044 case TEP_EVENT_DQUOTE:
1045 case TEP_EVENT_SQUOTE:
1046 /* don't keep quotes */
1052 if (i == (BUFSIZ - 1)) {
1056 if (extend_token(tok, buf, tok_size) < 0)
1057 return TEP_EVENT_NONE;
1063 /* the '\' '\' will cancel itself */
1064 if (ch == '\\' && last_ch == '\\')
1066 } while (ch != quote_ch || last_ch == '\\');
1067 /* remove the last quote */
1071 * For strings (double quotes) check the next token.
1072 * If it is another string, concatinate the two.
1074 if (type == TEP_EVENT_DQUOTE) {
1075 unsigned long long save_input_buf_ptr = input_buf_ptr;
1079 } while (isspace(ch));
1082 input_buf_ptr = save_input_buf_ptr;
1087 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1088 case TEP_EVENT_ITEM:
1093 while (get_type(__peek_char()) == type) {
1094 if (i == (BUFSIZ - 1)) {
1098 if (extend_token(tok, buf, tok_size) < 0)
1099 return TEP_EVENT_NONE;
1108 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1109 return TEP_EVENT_NONE;
1111 if (type == TEP_EVENT_ITEM) {
1113 * Older versions of the kernel has a bug that
1114 * creates invalid symbols and will break the mac80211
1115 * parsing. This is a work around to that bug.
1117 * See Linux kernel commit:
1118 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1120 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1123 return force_token("\"%s\" ", tok);
1124 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1127 return force_token("\" sta:%pM\" ", tok);
1128 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1131 return force_token("\" vif:%p(%d)\" ", tok);
1138 static enum tep_event_type force_token(const char *str, char **tok)
1140 const char *save_input_buf;
1141 unsigned long long save_input_buf_ptr;
1142 unsigned long long save_input_buf_siz;
1143 enum tep_event_type type;
1145 /* save off the current input pointers */
1146 save_input_buf = input_buf;
1147 save_input_buf_ptr = input_buf_ptr;
1148 save_input_buf_siz = input_buf_siz;
1150 init_input_buf(str, strlen(str));
1152 type = __read_token(tok);
1154 /* reset back to original token */
1155 input_buf = save_input_buf;
1156 input_buf_ptr = save_input_buf_ptr;
1157 input_buf_siz = save_input_buf_siz;
1162 static void free_token(char *tok)
1168 static enum tep_event_type read_token(char **tok)
1170 enum tep_event_type type;
1173 type = __read_token(tok);
1174 if (type != TEP_EVENT_SPACE)
1182 return TEP_EVENT_NONE;
1186 * tep_read_token - access to utilities to use the tep parser
1187 * @tok: The token to return
1189 * This will parse tokens from the string given by
1192 * Returns the token type.
1194 enum tep_event_type tep_read_token(char **tok)
1196 return read_token(tok);
1200 * tep_free_token - free a token returned by tep_read_token
1201 * @token: the token to free
1203 void tep_free_token(char *token)
1209 static enum tep_event_type read_token_item(char **tok)
1211 enum tep_event_type type;
1214 type = __read_token(tok);
1215 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1223 return TEP_EVENT_NONE;
1226 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1228 if (type != expect) {
1229 do_warning("Error: expected type %d but read %d",
1236 static int test_type_token(enum tep_event_type type, const char *token,
1237 enum tep_event_type expect, const char *expect_tok)
1239 if (type != expect) {
1240 do_warning("Error: expected type %d but read %d",
1245 if (strcmp(token, expect_tok) != 0) {
1246 do_warning("Error: expected '%s' but read '%s'",
1253 static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
1255 enum tep_event_type type;
1258 type = read_token(tok);
1260 type = read_token_item(tok);
1261 return test_type(type, expect);
1264 static int read_expect_type(enum tep_event_type expect, char **tok)
1266 return __read_expect_type(expect, tok, 1);
1269 static int __read_expected(enum tep_event_type expect, const char *str,
1272 enum tep_event_type type;
1277 type = read_token(&token);
1279 type = read_token_item(&token);
1281 ret = test_type_token(type, token, expect, str);
1288 static int read_expected(enum tep_event_type expect, const char *str)
1290 return __read_expected(expect, str, 1);
1293 static int read_expected_item(enum tep_event_type expect, const char *str)
1295 return __read_expected(expect, str, 0);
1298 static char *event_read_name(void)
1302 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
1305 if (read_expected(TEP_EVENT_OP, ":") < 0)
1308 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1318 static int event_read_id(void)
1323 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
1326 if (read_expected(TEP_EVENT_OP, ":") < 0)
1329 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1332 id = strtoul(token, NULL, 0);
1341 static int field_is_string(struct tep_format_field *field)
1343 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1344 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1345 strstr(field->type, "s8")))
1351 static int field_is_dynamic(struct tep_format_field *field)
1353 if (strncmp(field->type, "__data_loc", 10) == 0)
1359 static int field_is_long(struct tep_format_field *field)
1361 /* includes long long */
1362 if (strstr(field->type, "long"))
1368 static unsigned int type_size(const char *name)
1370 /* This covers all TEP_FIELD_IS_STRING types. */
1388 for (i = 0; table[i].type; i++) {
1389 if (!strcmp(table[i].type, name))
1390 return table[i].size;
1396 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1398 struct tep_format_field *field = NULL;
1399 enum tep_event_type type;
1405 unsigned int size_dynamic = 0;
1407 type = read_token(&token);
1408 if (type == TEP_EVENT_NEWLINE) {
1415 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1419 type = read_token(&token);
1421 * The ftrace fields may still use the "special" name.
1424 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1425 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1427 type = read_token(&token);
1430 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1434 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1439 field = calloc(1, sizeof(*field));
1443 field->event = event;
1445 /* read the rest of the type */
1447 type = read_token(&token);
1448 if (type == TEP_EVENT_ITEM ||
1449 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1451 * Some of the ftrace fields are broken and have
1452 * an illegal "." in them.
1454 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1455 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1457 if (strcmp(token, "*") == 0)
1458 field->flags |= TEP_FIELD_IS_POINTER;
1462 new_type = realloc(field->type,
1463 strlen(field->type) +
1464 strlen(last_token) + 2);
1469 field->type = new_type;
1470 strcat(field->type, " ");
1471 strcat(field->type, last_token);
1474 field->type = last_token;
1483 do_warning_event(event, "%s: no type found", __func__);
1486 field->name = field->alias = last_token;
1488 if (test_type(type, TEP_EVENT_OP))
1491 if (strcmp(token, "[") == 0) {
1492 enum tep_event_type last_type = type;
1493 char *brackets = token;
1497 field->flags |= TEP_FIELD_IS_ARRAY;
1499 type = read_token(&token);
1501 if (type == TEP_EVENT_ITEM)
1502 field->arraylen = strtoul(token, NULL, 0);
1504 field->arraylen = 0;
1506 while (strcmp(token, "]") != 0) {
1507 if (last_type == TEP_EVENT_ITEM &&
1508 type == TEP_EVENT_ITEM)
1514 new_brackets = realloc(brackets,
1516 strlen(token) + len);
1517 if (!new_brackets) {
1521 brackets = new_brackets;
1523 strcat(brackets, " ");
1524 strcat(brackets, token);
1525 /* We only care about the last token */
1526 field->arraylen = strtoul(token, NULL, 0);
1528 type = read_token(&token);
1529 if (type == TEP_EVENT_NONE) {
1530 do_warning_event(event, "failed to find token");
1537 new_brackets = realloc(brackets, strlen(brackets) + 2);
1538 if (!new_brackets) {
1542 brackets = new_brackets;
1543 strcat(brackets, "]");
1545 /* add brackets to type */
1547 type = read_token(&token);
1549 * If the next token is not an OP, then it is of
1550 * the format: type [] item;
1552 if (type == TEP_EVENT_ITEM) {
1554 new_type = realloc(field->type,
1555 strlen(field->type) +
1556 strlen(field->name) +
1557 strlen(brackets) + 2);
1562 field->type = new_type;
1563 strcat(field->type, " ");
1564 strcat(field->type, field->name);
1565 size_dynamic = type_size(field->name);
1566 free_token(field->name);
1567 strcat(field->type, brackets);
1568 field->name = field->alias = token;
1569 type = read_token(&token);
1572 new_type = realloc(field->type,
1573 strlen(field->type) +
1574 strlen(brackets) + 1);
1579 field->type = new_type;
1580 strcat(field->type, brackets);
1585 if (field_is_string(field))
1586 field->flags |= TEP_FIELD_IS_STRING;
1587 if (field_is_dynamic(field))
1588 field->flags |= TEP_FIELD_IS_DYNAMIC;
1589 if (field_is_long(field))
1590 field->flags |= TEP_FIELD_IS_LONG;
1592 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1596 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1599 if (read_expected(TEP_EVENT_OP, ":") < 0)
1602 if (read_expect_type(TEP_EVENT_ITEM, &token))
1604 field->offset = strtoul(token, NULL, 0);
1607 if (read_expected(TEP_EVENT_OP, ";") < 0)
1610 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1613 if (read_expected(TEP_EVENT_OP, ":") < 0)
1616 if (read_expect_type(TEP_EVENT_ITEM, &token))
1618 field->size = strtoul(token, NULL, 0);
1621 if (read_expected(TEP_EVENT_OP, ";") < 0)
1624 type = read_token(&token);
1625 if (type != TEP_EVENT_NEWLINE) {
1626 /* newer versions of the kernel have a "signed" type */
1627 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1632 if (read_expected(TEP_EVENT_OP, ":") < 0)
1635 if (read_expect_type(TEP_EVENT_ITEM, &token))
1638 if (strtoul(token, NULL, 0))
1639 field->flags |= TEP_FIELD_IS_SIGNED;
1642 if (read_expected(TEP_EVENT_OP, ";") < 0)
1645 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1651 if (field->flags & TEP_FIELD_IS_ARRAY) {
1652 if (field->arraylen)
1653 field->elementsize = field->size / field->arraylen;
1654 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1655 field->elementsize = size_dynamic;
1656 else if (field->flags & TEP_FIELD_IS_STRING)
1657 field->elementsize = 1;
1658 else if (field->flags & TEP_FIELD_IS_LONG)
1659 field->elementsize = event->tep ?
1660 event->tep->long_size :
1663 field->elementsize = field->size;
1666 fields = &field->next;
1683 static int event_read_format(struct tep_event *event)
1688 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1691 if (read_expected(TEP_EVENT_OP, ":") < 0)
1694 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1698 ret = event_read_fields(event, &event->format.common_fields);
1701 event->format.nr_common = ret;
1703 ret = event_read_fields(event, &event->format.fields);
1706 event->format.nr_fields = ret;
1715 static enum tep_event_type
1716 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1717 char **tok, enum tep_event_type type);
1719 static enum tep_event_type
1720 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1722 enum tep_event_type type;
1725 type = read_token(&token);
1728 return process_arg_token(event, arg, tok, type);
1731 static enum tep_event_type
1732 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1735 * For __print_symbolic() and __print_flags, we need to completely
1736 * evaluate the first argument, which defines what to print next.
1738 static enum tep_event_type
1739 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1741 enum tep_event_type type;
1743 type = process_arg(event, arg, tok);
1745 while (type == TEP_EVENT_OP) {
1746 type = process_op(event, arg, tok);
1752 static enum tep_event_type
1753 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1755 struct tep_print_arg *arg, *left, *right;
1756 enum tep_event_type type;
1761 right = alloc_arg();
1763 if (!arg || !left || !right) {
1764 do_warning_event(event, "%s: not enough memory!", __func__);
1765 /* arg will be freed at out_free */
1771 arg->type = TEP_PRINT_OP;
1772 arg->op.left = left;
1773 arg->op.right = right;
1776 type = process_arg(event, left, &token);
1779 if (type == TEP_EVENT_ERROR)
1782 /* Handle other operations in the arguments */
1783 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1784 type = process_op(event, left, &token);
1788 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1793 type = process_arg(event, right, &token);
1795 top->op.right = arg;
1801 /* Top may point to itself */
1802 top->op.right = NULL;
1805 return TEP_EVENT_ERROR;
1808 static enum tep_event_type
1809 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1811 struct tep_print_arg *arg;
1812 enum tep_event_type type;
1817 do_warning_event(event, "%s: not enough memory!", __func__);
1818 /* '*tok' is set to top->op.op. No need to free. */
1820 return TEP_EVENT_ERROR;
1824 type = process_arg(event, arg, &token);
1825 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1828 top->op.right = arg;
1831 type = read_token_item(&token);
1839 return TEP_EVENT_ERROR;
1842 static int get_op_prio(char *op)
1856 /* '>>' and '<<' are 8 */
1860 /* '==' and '!=' are 10 */
1870 do_warning("unknown op '%c'", op[0]);
1874 if (strcmp(op, "++") == 0 ||
1875 strcmp(op, "--") == 0) {
1877 } else if (strcmp(op, ">>") == 0 ||
1878 strcmp(op, "<<") == 0) {
1880 } else if (strcmp(op, ">=") == 0 ||
1881 strcmp(op, "<=") == 0) {
1883 } else if (strcmp(op, "==") == 0 ||
1884 strcmp(op, "!=") == 0) {
1886 } else if (strcmp(op, "&&") == 0) {
1888 } else if (strcmp(op, "||") == 0) {
1891 do_warning("unknown op '%s'", op);
1897 static int set_op_prio(struct tep_print_arg *arg)
1900 /* single ops are the greatest */
1901 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1904 arg->op.prio = get_op_prio(arg->op.op);
1906 return arg->op.prio;
1909 /* Note, *tok does not get freed, but will most likely be saved */
1910 static enum tep_event_type
1911 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1913 struct tep_print_arg *left, *right = NULL;
1914 enum tep_event_type type;
1917 /* the op is passed in via tok */
1920 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1921 /* handle single op */
1923 do_warning_event(event, "bad op token %s", token);
1933 do_warning_event(event, "bad op token %s", token);
1938 /* make an empty left */
1943 left->type = TEP_PRINT_NULL;
1944 arg->op.left = left;
1946 right = alloc_arg();
1950 arg->op.right = right;
1952 /* do not free the token, it belongs to an op */
1954 type = process_arg(event, right, tok);
1956 } else if (strcmp(token, "?") == 0) {
1962 /* copy the top arg to the left */
1965 arg->type = TEP_PRINT_OP;
1967 arg->op.left = left;
1970 /* it will set arg->op.right */
1971 type = process_cond(event, arg, tok);
1973 } else if (strcmp(token, ">>") == 0 ||
1974 strcmp(token, "<<") == 0 ||
1975 strcmp(token, "&") == 0 ||
1976 strcmp(token, "|") == 0 ||
1977 strcmp(token, "&&") == 0 ||
1978 strcmp(token, "||") == 0 ||
1979 strcmp(token, "-") == 0 ||
1980 strcmp(token, "+") == 0 ||
1981 strcmp(token, "*") == 0 ||
1982 strcmp(token, "^") == 0 ||
1983 strcmp(token, "/") == 0 ||
1984 strcmp(token, "%") == 0 ||
1985 strcmp(token, "<") == 0 ||
1986 strcmp(token, ">") == 0 ||
1987 strcmp(token, "<=") == 0 ||
1988 strcmp(token, ">=") == 0 ||
1989 strcmp(token, "==") == 0 ||
1990 strcmp(token, "!=") == 0) {
1996 /* copy the top arg to the left */
1999 arg->type = TEP_PRINT_OP;
2001 arg->op.left = left;
2002 arg->op.right = NULL;
2004 if (set_op_prio(arg) == -1) {
2005 event->flags |= TEP_EVENT_FL_FAILED;
2006 /* arg->op.op (= token) will be freed at out_free */
2011 type = read_token_item(&token);
2014 /* could just be a type pointer */
2015 if ((strcmp(arg->op.op, "*") == 0) &&
2016 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2019 if (left->type != TEP_PRINT_ATOM) {
2020 do_warning_event(event, "bad pointer type");
2023 new_atom = realloc(left->atom.atom,
2024 strlen(left->atom.atom) + 3);
2028 left->atom.atom = new_atom;
2029 strcat(left->atom.atom, " *");
2037 right = alloc_arg();
2041 type = process_arg_token(event, right, tok, type);
2042 if (type == TEP_EVENT_ERROR) {
2044 /* token was freed in process_arg_token() via *tok */
2049 if (right->type == TEP_PRINT_OP &&
2050 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2051 struct tep_print_arg tmp;
2053 /* rotate ops according to the priority */
2054 arg->op.right = right->op.left;
2060 arg->op.left = right;
2062 arg->op.right = right;
2065 } else if (strcmp(token, "[") == 0) {
2073 arg->type = TEP_PRINT_OP;
2075 arg->op.left = left;
2079 /* it will set arg->op.right */
2080 type = process_array(event, arg, tok);
2083 do_warning_event(event, "unknown op '%s'", token);
2084 event->flags |= TEP_EVENT_FL_FAILED;
2085 /* the arg is now the left side */
2089 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2092 /* higher prios need to be closer to the root */
2093 prio = get_op_prio(*tok);
2095 if (prio > arg->op.prio)
2096 return process_op(event, arg, tok);
2098 return process_op(event, right, tok);
2104 do_warning_event(event, "%s: not enough memory!", __func__);
2108 return TEP_EVENT_ERROR;
2111 static enum tep_event_type
2112 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2115 enum tep_event_type type;
2119 if (read_expected(TEP_EVENT_OP, "->") < 0)
2122 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2126 arg->type = TEP_PRINT_FIELD;
2127 arg->field.name = field;
2129 if (is_flag_field) {
2130 arg->field.field = tep_find_any_field(event, arg->field.name);
2131 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2133 } else if (is_symbolic_field) {
2134 arg->field.field = tep_find_any_field(event, arg->field.name);
2135 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2136 is_symbolic_field = 0;
2139 type = read_token(&token);
2148 return TEP_EVENT_ERROR;
2151 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2152 struct tep_print_arg **print_arg)
2154 struct tep_print_arg *field;
2155 enum tep_event_type type;
2159 field = alloc_arg();
2161 do_warning_event(event, "%s: not enough memory!", __func__);
2166 type = process_arg(event, field, &token);
2168 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2172 goto out_free_token;
2183 static char *arg_eval (struct tep_print_arg *arg);
2185 static unsigned long long
2186 eval_type_str(unsigned long long val, const char *type, int pointer)
2196 if (type[len-1] != '*') {
2197 do_warning("pointer expected with non pointer type");
2203 do_warning("%s: not enough memory!", __func__);
2206 memcpy(ref, type, len);
2208 /* chop off the " *" */
2211 val = eval_type_str(val, ref, 0);
2216 /* check if this is a pointer */
2217 if (type[len - 1] == '*')
2220 /* Try to figure out the arg size*/
2221 if (strncmp(type, "struct", 6) == 0)
2225 if (strcmp(type, "u8") == 0)
2228 if (strcmp(type, "u16") == 0)
2229 return val & 0xffff;
2231 if (strcmp(type, "u32") == 0)
2232 return val & 0xffffffff;
2234 if (strcmp(type, "u64") == 0 ||
2235 strcmp(type, "s64") == 0)
2238 if (strcmp(type, "s8") == 0)
2239 return (unsigned long long)(char)val & 0xff;
2241 if (strcmp(type, "s16") == 0)
2242 return (unsigned long long)(short)val & 0xffff;
2244 if (strcmp(type, "s32") == 0)
2245 return (unsigned long long)(int)val & 0xffffffff;
2247 if (strncmp(type, "unsigned ", 9) == 0) {
2252 if (strcmp(type, "char") == 0) {
2254 return (unsigned long long)(char)val & 0xff;
2259 if (strcmp(type, "short") == 0) {
2261 return (unsigned long long)(short)val & 0xffff;
2263 return val & 0xffff;
2266 if (strcmp(type, "int") == 0) {
2268 return (unsigned long long)(int)val & 0xffffffff;
2270 return val & 0xffffffff;
2277 * Try to figure out the type.
2279 static unsigned long long
2280 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2282 if (arg->type != TEP_PRINT_TYPE) {
2283 do_warning("expected type argument");
2287 return eval_type_str(val, arg->typecast.type, pointer);
2290 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2292 long long left, right;
2295 switch (arg->type) {
2296 case TEP_PRINT_ATOM:
2297 *val = strtoll(arg->atom.atom, NULL, 0);
2299 case TEP_PRINT_TYPE:
2300 ret = arg_num_eval(arg->typecast.item, val);
2303 *val = eval_type(*val, arg, 0);
2306 switch (arg->op.op[0]) {
2308 ret = arg_num_eval(arg->op.left, &left);
2311 ret = arg_num_eval(arg->op.right, &right);
2315 *val = left || right;
2317 *val = left | right;
2320 ret = arg_num_eval(arg->op.left, &left);
2323 ret = arg_num_eval(arg->op.right, &right);
2327 *val = left && right;
2329 *val = left & right;
2332 ret = arg_num_eval(arg->op.left, &left);
2335 ret = arg_num_eval(arg->op.right, &right);
2338 switch (arg->op.op[1]) {
2340 *val = left < right;
2343 *val = left << right;
2346 *val = left <= right;
2349 do_warning("unknown op '%s'", arg->op.op);
2354 ret = arg_num_eval(arg->op.left, &left);
2357 ret = arg_num_eval(arg->op.right, &right);
2360 switch (arg->op.op[1]) {
2362 *val = left > right;
2365 *val = left >> right;
2368 *val = left >= right;
2371 do_warning("unknown op '%s'", arg->op.op);
2376 ret = arg_num_eval(arg->op.left, &left);
2379 ret = arg_num_eval(arg->op.right, &right);
2383 if (arg->op.op[1] != '=') {
2384 do_warning("unknown op '%s'", arg->op.op);
2387 *val = left == right;
2390 ret = arg_num_eval(arg->op.left, &left);
2393 ret = arg_num_eval(arg->op.right, &right);
2397 switch (arg->op.op[1]) {
2399 *val = left != right;
2402 do_warning("unknown op '%s'", arg->op.op);
2407 /* check for negative */
2408 if (arg->op.left->type == TEP_PRINT_NULL)
2411 ret = arg_num_eval(arg->op.left, &left);
2414 ret = arg_num_eval(arg->op.right, &right);
2417 *val = left - right;
2420 if (arg->op.left->type == TEP_PRINT_NULL)
2423 ret = arg_num_eval(arg->op.left, &left);
2426 ret = arg_num_eval(arg->op.right, &right);
2429 *val = left + right;
2432 ret = arg_num_eval(arg->op.right, &right);
2438 do_warning("unknown op '%s'", arg->op.op);
2443 case TEP_PRINT_NULL:
2444 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2445 case TEP_PRINT_STRING:
2446 case TEP_PRINT_BSTRING:
2447 case TEP_PRINT_BITMASK:
2449 do_warning("invalid eval type %d", arg->type);
2456 static char *arg_eval (struct tep_print_arg *arg)
2459 static char buf[24];
2461 switch (arg->type) {
2462 case TEP_PRINT_ATOM:
2463 return arg->atom.atom;
2464 case TEP_PRINT_TYPE:
2465 return arg_eval(arg->typecast.item);
2467 if (!arg_num_eval(arg, &val))
2469 sprintf(buf, "%lld", val);
2472 case TEP_PRINT_NULL:
2473 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2474 case TEP_PRINT_STRING:
2475 case TEP_PRINT_BSTRING:
2476 case TEP_PRINT_BITMASK:
2478 do_warning("invalid eval type %d", arg->type);
2485 static enum tep_event_type
2486 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2488 enum tep_event_type type;
2489 struct tep_print_arg *arg = NULL;
2490 struct tep_print_flag_sym *field;
2496 type = read_token_item(&token);
2497 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2505 type = process_arg(event, arg, &token);
2507 if (type == TEP_EVENT_OP)
2508 type = process_op(event, arg, &token);
2510 if (type == TEP_EVENT_ERROR)
2513 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2516 field = calloc(1, sizeof(*field));
2520 value = arg_eval(arg);
2522 goto out_free_field;
2523 field->value = strdup(value);
2524 if (field->value == NULL)
2525 goto out_free_field;
2533 type = process_arg(event, arg, &token);
2534 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2535 goto out_free_field;
2537 value = arg_eval(arg);
2539 goto out_free_field;
2540 field->str = strdup(value);
2541 if (field->str == NULL)
2542 goto out_free_field;
2547 list = &field->next;
2550 type = read_token_item(&token);
2551 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2557 free_flag_sym(field);
2563 return TEP_EVENT_ERROR;
2566 static enum tep_event_type
2567 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2569 struct tep_print_arg *field;
2570 enum tep_event_type type;
2573 memset(arg, 0, sizeof(*arg));
2574 arg->type = TEP_PRINT_FLAGS;
2576 field = alloc_arg();
2578 do_warning_event(event, "%s: not enough memory!", __func__);
2582 type = process_field_arg(event, field, &token);
2584 /* Handle operations in the first argument */
2585 while (type == TEP_EVENT_OP)
2586 type = process_op(event, field, &token);
2588 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2589 goto out_free_field;
2592 arg->flags.field = field;
2594 type = read_token_item(&token);
2595 if (event_item_type(type)) {
2596 arg->flags.delim = token;
2597 type = read_token_item(&token);
2600 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2603 type = process_fields(event, &arg->flags.flags, &token);
2604 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2608 type = read_token_item(tok);
2616 return TEP_EVENT_ERROR;
2619 static enum tep_event_type
2620 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2622 struct tep_print_arg *field;
2623 enum tep_event_type type;
2626 memset(arg, 0, sizeof(*arg));
2627 arg->type = TEP_PRINT_SYMBOL;
2629 field = alloc_arg();
2631 do_warning_event(event, "%s: not enough memory!", __func__);
2635 type = process_field_arg(event, field, &token);
2637 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2638 goto out_free_field;
2640 arg->symbol.field = field;
2642 type = process_fields(event, &arg->symbol.symbols, &token);
2643 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2647 type = read_token_item(tok);
2655 return TEP_EVENT_ERROR;
2658 static enum tep_event_type
2659 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2660 char **tok, enum tep_print_arg_type type)
2662 memset(arg, 0, sizeof(*arg));
2665 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2668 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2671 return read_token_item(tok);
2674 free_arg(arg->hex.field);
2675 arg->hex.field = NULL;
2678 return TEP_EVENT_ERROR;
2681 static enum tep_event_type
2682 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2684 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2687 static enum tep_event_type
2688 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2691 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2694 static enum tep_event_type
2695 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2697 memset(arg, 0, sizeof(*arg));
2698 arg->type = TEP_PRINT_INT_ARRAY;
2700 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2703 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2706 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2709 return read_token_item(tok);
2712 free_arg(arg->int_array.count);
2713 arg->int_array.count = NULL;
2715 free_arg(arg->int_array.field);
2716 arg->int_array.field = NULL;
2719 return TEP_EVENT_ERROR;
2722 static enum tep_event_type
2723 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2725 struct tep_format_field *field;
2726 enum tep_event_type type;
2729 memset(arg, 0, sizeof(*arg));
2730 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2733 * The item within the parenthesis is another field that holds
2734 * the index into where the array starts.
2736 type = read_token(&token);
2738 if (type != TEP_EVENT_ITEM)
2741 /* Find the field */
2743 field = tep_find_field(event, token);
2747 arg->dynarray.field = field;
2748 arg->dynarray.index = 0;
2750 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2754 type = read_token_item(&token);
2756 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2762 do_warning_event(event, "%s: not enough memory!", __func__);
2764 return TEP_EVENT_ERROR;
2767 type = process_arg(event, arg, &token);
2768 if (type == TEP_EVENT_ERROR)
2771 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2775 type = read_token_item(tok);
2783 return TEP_EVENT_ERROR;
2786 static enum tep_event_type
2787 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2790 struct tep_format_field *field;
2791 enum tep_event_type type;
2794 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2797 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2799 /* Find the field */
2800 field = tep_find_field(event, token);
2804 arg->dynarray.field = field;
2805 arg->dynarray.index = 0;
2807 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2810 type = read_token(&token);
2819 return TEP_EVENT_ERROR;
2822 static enum tep_event_type
2823 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2825 struct tep_print_arg *item_arg;
2826 enum tep_event_type type;
2829 type = process_arg(event, arg, &token);
2831 if (type == TEP_EVENT_ERROR)
2834 if (type == TEP_EVENT_OP)
2835 type = process_op(event, arg, &token);
2837 if (type == TEP_EVENT_ERROR)
2840 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2844 type = read_token_item(&token);
2847 * If the next token is an item or another open paren, then
2848 * this was a typecast.
2850 if (event_item_type(type) ||
2851 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2853 /* make this a typecast and contine */
2855 /* prevous must be an atom */
2856 if (arg->type != TEP_PRINT_ATOM) {
2857 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2861 item_arg = alloc_arg();
2863 do_warning_event(event, "%s: not enough memory!",
2868 arg->type = TEP_PRINT_TYPE;
2869 arg->typecast.type = arg->atom.atom;
2870 arg->typecast.item = item_arg;
2871 type = process_arg_token(event, item_arg, &token, type);
2881 return TEP_EVENT_ERROR;
2885 static enum tep_event_type
2886 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2889 enum tep_event_type type;
2892 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2895 arg->type = TEP_PRINT_STRING;
2896 arg->string.string = token;
2897 arg->string.offset = -1;
2899 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2902 type = read_token(&token);
2911 return TEP_EVENT_ERROR;
2914 static enum tep_event_type
2915 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2918 enum tep_event_type type;
2921 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2924 arg->type = TEP_PRINT_BITMASK;
2925 arg->bitmask.bitmask = token;
2926 arg->bitmask.offset = -1;
2928 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2931 type = read_token(&token);
2940 return TEP_EVENT_ERROR;
2943 static struct tep_function_handler *
2944 find_func_handler(struct tep_handle *tep, char *func_name)
2946 struct tep_function_handler *func;
2951 for (func = tep->func_handlers; func; func = func->next) {
2952 if (strcmp(func->name, func_name) == 0)
2959 static void remove_func_handler(struct tep_handle *tep, char *func_name)
2961 struct tep_function_handler *func;
2962 struct tep_function_handler **next;
2964 next = &tep->func_handlers;
2965 while ((func = *next)) {
2966 if (strcmp(func->name, func_name) == 0) {
2968 free_func_handle(func);
2975 static enum tep_event_type
2976 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
2977 struct tep_print_arg *arg, char **tok)
2979 struct tep_print_arg **next_arg;
2980 struct tep_print_arg *farg;
2981 enum tep_event_type type;
2985 arg->type = TEP_PRINT_FUNC;
2986 arg->func.func = func;
2990 next_arg = &(arg->func.args);
2991 for (i = 0; i < func->nr_args; i++) {
2994 do_warning_event(event, "%s: not enough memory!",
2996 return TEP_EVENT_ERROR;
2999 type = process_arg(event, farg, &token);
3000 if (i < (func->nr_args - 1)) {
3001 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3002 do_warning_event(event,
3003 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3004 func->name, func->nr_args,
3005 event->name, i + 1);
3009 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3010 do_warning_event(event,
3011 "Error: function '%s()' only expects %d arguments but event %s has more",
3012 func->name, func->nr_args, event->name);
3018 next_arg = &(farg->next);
3022 type = read_token(&token);
3030 return TEP_EVENT_ERROR;
3033 static enum tep_event_type
3034 process_function(struct tep_event *event, struct tep_print_arg *arg,
3035 char *token, char **tok)
3037 struct tep_function_handler *func;
3039 if (strcmp(token, "__print_flags") == 0) {
3042 return process_flags(event, arg, tok);
3044 if (strcmp(token, "__print_symbolic") == 0) {
3046 is_symbolic_field = 1;
3047 return process_symbols(event, arg, tok);
3049 if (strcmp(token, "__print_hex") == 0) {
3051 return process_hex(event, arg, tok);
3053 if (strcmp(token, "__print_hex_str") == 0) {
3055 return process_hex_str(event, arg, tok);
3057 if (strcmp(token, "__print_array") == 0) {
3059 return process_int_array(event, arg, tok);
3061 if (strcmp(token, "__get_str") == 0) {
3063 return process_str(event, arg, tok);
3065 if (strcmp(token, "__get_bitmask") == 0) {
3067 return process_bitmask(event, arg, tok);
3069 if (strcmp(token, "__get_dynamic_array") == 0) {
3071 return process_dynamic_array(event, arg, tok);
3073 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3075 return process_dynamic_array_len(event, arg, tok);
3078 func = find_func_handler(event->tep, token);
3081 return process_func_handler(event, func, arg, tok);
3084 do_warning_event(event, "function %s not defined", token);
3086 return TEP_EVENT_ERROR;
3089 static enum tep_event_type
3090 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3091 char **tok, enum tep_event_type type)
3099 case TEP_EVENT_ITEM:
3100 if (strcmp(token, "REC") == 0) {
3102 type = process_entry(event, arg, &token);
3106 /* test the next token */
3107 type = read_token_item(&token);
3110 * If the next token is a parenthesis, then this
3113 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3116 /* this will free atom. */
3117 type = process_function(event, arg, atom, &token);
3120 /* atoms can be more than one token long */
3121 while (type == TEP_EVENT_ITEM) {
3123 new_atom = realloc(atom,
3124 strlen(atom) + strlen(token) + 2);
3129 return TEP_EVENT_ERROR;
3133 strcat(atom, token);
3135 type = read_token_item(&token);
3138 arg->type = TEP_PRINT_ATOM;
3139 arg->atom.atom = atom;
3142 case TEP_EVENT_DQUOTE:
3143 case TEP_EVENT_SQUOTE:
3144 arg->type = TEP_PRINT_ATOM;
3145 arg->atom.atom = token;
3146 type = read_token_item(&token);
3148 case TEP_EVENT_DELIM:
3149 if (strcmp(token, "(") == 0) {
3151 type = process_paren(event, arg, &token);
3155 /* handle single ops */
3156 arg->type = TEP_PRINT_OP;
3158 arg->op.left = NULL;
3159 type = process_op(event, arg, &token);
3161 /* On error, the op is freed */
3162 if (type == TEP_EVENT_ERROR)
3165 /* return error type if errored */
3168 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3170 do_warning_event(event, "unexpected type %d", type);
3171 return TEP_EVENT_ERROR;
3178 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3180 enum tep_event_type type = TEP_EVENT_ERROR;
3181 struct tep_print_arg *arg;
3186 if (type == TEP_EVENT_NEWLINE) {
3187 type = read_token_item(&token);
3193 do_warning_event(event, "%s: not enough memory!",
3198 type = process_arg(event, arg, &token);
3200 if (type == TEP_EVENT_ERROR) {
3209 if (type == TEP_EVENT_OP) {
3210 type = process_op(event, arg, &token);
3212 if (type == TEP_EVENT_ERROR) {
3221 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3228 } while (type != TEP_EVENT_NONE);
3230 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3236 static int event_read_print(struct tep_event *event)
3238 enum tep_event_type type;
3242 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3245 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3248 if (read_expected(TEP_EVENT_OP, ":") < 0)
3251 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3255 event->print_fmt.format = token;
3256 event->print_fmt.args = NULL;
3258 /* ok to have no arg */
3259 type = read_token_item(&token);
3261 if (type == TEP_EVENT_NONE)
3264 /* Handle concatenation of print lines */
3265 if (type == TEP_EVENT_DQUOTE) {
3268 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3271 free_token(event->print_fmt.format);
3272 event->print_fmt.format = NULL;
3277 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3282 ret = event_read_print_args(event, &event->print_fmt.args);
3294 * tep_find_common_field - return a common field by event
3295 * @event: handle for the event
3296 * @name: the name of the common field to return
3298 * Returns a common field from the event by the given @name.
3299 * This only searches the common fields and not all field.
3301 struct tep_format_field *
3302 tep_find_common_field(struct tep_event *event, const char *name)
3304 struct tep_format_field *format;
3306 for (format = event->format.common_fields;
3307 format; format = format->next) {
3308 if (strcmp(format->name, name) == 0)
3316 * tep_find_field - find a non-common field
3317 * @event: handle for the event
3318 * @name: the name of the non-common field
3320 * Returns a non-common field by the given @name.
3321 * This does not search common fields.
3323 struct tep_format_field *
3324 tep_find_field(struct tep_event *event, const char *name)
3326 struct tep_format_field *format;
3328 for (format = event->format.fields;
3329 format; format = format->next) {
3330 if (strcmp(format->name, name) == 0)
3338 * tep_find_any_field - find any field by name
3339 * @event: handle for the event
3340 * @name: the name of the field
3342 * Returns a field by the given @name.
3343 * This searches the common field names first, then
3344 * the non-common ones if a common one was not found.
3346 struct tep_format_field *
3347 tep_find_any_field(struct tep_event *event, const char *name)
3349 struct tep_format_field *format;
3351 format = tep_find_common_field(event, name);
3354 return tep_find_field(event, name);
3358 * tep_read_number - read a number from data
3359 * @tep: a handle to the trace event parser context
3360 * @ptr: the raw data
3361 * @size: the size of the data that holds the number
3363 * Returns the number (converted to host) from the
3366 unsigned long long tep_read_number(struct tep_handle *tep,
3367 const void *ptr, int size)
3369 unsigned long long val;
3373 return *(unsigned char *)ptr;
3375 return tep_data2host2(tep, *(unsigned short *)ptr);
3377 return tep_data2host4(tep, *(unsigned int *)ptr);
3379 memcpy(&val, (ptr), sizeof(unsigned long long));
3380 return tep_data2host8(tep, val);
3388 * tep_read_number_field - read a number from data
3389 * @field: a handle to the field
3390 * @data: the raw data to read
3391 * @value: the value to place the number in
3393 * Reads raw data according to a field offset and size,
3394 * and translates it into @value.
3396 * Returns 0 on success, -1 otherwise.
3398 int tep_read_number_field(struct tep_format_field *field, const void *data,
3399 unsigned long long *value)
3403 switch (field->size) {
3408 *value = tep_read_number(field->event->tep,
3409 data + field->offset, field->size);
3416 static int get_common_info(struct tep_handle *tep,
3417 const char *type, int *offset, int *size)
3419 struct tep_event *event;
3420 struct tep_format_field *field;
3423 * All events should have the same common elements.
3424 * Pick any event to find where the type is;
3427 do_warning("no event_list!");
3431 event = tep->events[0];
3432 field = tep_find_common_field(event, type);
3436 *offset = field->offset;
3437 *size = field->size;
3442 static int __parse_common(struct tep_handle *tep, void *data,
3443 int *size, int *offset, const char *name)
3448 ret = get_common_info(tep, name, offset, size);
3452 return tep_read_number(tep, data + *offset, *size);
3455 static int trace_parse_common_type(struct tep_handle *tep, void *data)
3457 return __parse_common(tep, data,
3458 &tep->type_size, &tep->type_offset,
3462 static int parse_common_pid(struct tep_handle *tep, void *data)
3464 return __parse_common(tep, data,
3465 &tep->pid_size, &tep->pid_offset,
3469 static int parse_common_pc(struct tep_handle *tep, void *data)
3471 return __parse_common(tep, data,
3472 &tep->pc_size, &tep->pc_offset,
3473 "common_preempt_count");
3476 static int parse_common_flags(struct tep_handle *tep, void *data)
3478 return __parse_common(tep, data,
3479 &tep->flags_size, &tep->flags_offset,
3483 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
3485 return __parse_common(tep, data,
3486 &tep->ld_size, &tep->ld_offset,
3487 "common_lock_depth");
3490 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
3492 return __parse_common(tep, data,
3493 &tep->ld_size, &tep->ld_offset,
3494 "common_migrate_disable");
3497 static int events_id_cmp(const void *a, const void *b);
3500 * tep_find_event - find an event by given id
3501 * @tep: a handle to the trace event parser context
3502 * @id: the id of the event
3504 * Returns an event that has a given @id.
3506 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
3508 struct tep_event **eventptr;
3509 struct tep_event key;
3510 struct tep_event *pkey = &key;
3512 /* Check cache first */
3513 if (tep->last_event && tep->last_event->id == id)
3514 return tep->last_event;
3518 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
3519 sizeof(*tep->events), events_id_cmp);
3522 tep->last_event = *eventptr;
3530 * tep_find_event_by_name - find an event by given name
3531 * @tep: a handle to the trace event parser context
3532 * @sys: the system name to search for
3533 * @name: the name of the event to search for
3535 * This returns an event with a given @name and under the system
3536 * @sys. If @sys is NULL the first event with @name is returned.
3539 tep_find_event_by_name(struct tep_handle *tep,
3540 const char *sys, const char *name)
3542 struct tep_event *event = NULL;
3545 if (tep->last_event &&
3546 strcmp(tep->last_event->name, name) == 0 &&
3547 (!sys || strcmp(tep->last_event->system, sys) == 0))
3548 return tep->last_event;
3550 for (i = 0; i < tep->nr_events; i++) {
3551 event = tep->events[i];
3552 if (strcmp(event->name, name) == 0) {
3555 if (strcmp(event->system, sys) == 0)
3559 if (i == tep->nr_events)
3562 tep->last_event = event;
3566 static unsigned long long
3567 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3569 struct tep_handle *tep = event->tep;
3570 unsigned long long val = 0;
3571 unsigned long long left, right;
3572 struct tep_print_arg *typearg = NULL;
3573 struct tep_print_arg *larg;
3574 unsigned long offset;
3575 unsigned int field_size;
3577 switch (arg->type) {
3578 case TEP_PRINT_NULL:
3581 case TEP_PRINT_ATOM:
3582 return strtoull(arg->atom.atom, NULL, 0);
3583 case TEP_PRINT_FIELD:
3584 if (!arg->field.field) {
3585 arg->field.field = tep_find_any_field(event, arg->field.name);
3586 if (!arg->field.field)
3587 goto out_warning_field;
3590 /* must be a number */
3591 val = tep_read_number(tep, data + arg->field.field->offset,
3592 arg->field.field->size);
3594 case TEP_PRINT_FLAGS:
3595 case TEP_PRINT_SYMBOL:
3596 case TEP_PRINT_INT_ARRAY:
3598 case TEP_PRINT_HEX_STR:
3600 case TEP_PRINT_TYPE:
3601 val = eval_num_arg(data, size, event, arg->typecast.item);
3602 return eval_type(val, arg, 0);
3603 case TEP_PRINT_STRING:
3604 case TEP_PRINT_BSTRING:
3605 case TEP_PRINT_BITMASK:
3607 case TEP_PRINT_FUNC: {
3610 val = process_defined_func(&s, data, size, event, arg);
3611 trace_seq_destroy(&s);
3615 if (strcmp(arg->op.op, "[") == 0) {
3617 * Arrays are special, since we don't want
3618 * to read the arg as is.
3620 right = eval_num_arg(data, size, event, arg->op.right);
3622 /* handle typecasts */
3623 larg = arg->op.left;
3624 while (larg->type == TEP_PRINT_TYPE) {
3627 larg = larg->typecast.item;
3630 /* Default to long size */
3631 field_size = tep->long_size;
3633 switch (larg->type) {
3634 case TEP_PRINT_DYNAMIC_ARRAY:
3635 offset = tep_read_number(tep,
3636 data + larg->dynarray.field->offset,
3637 larg->dynarray.field->size);
3638 if (larg->dynarray.field->elementsize)
3639 field_size = larg->dynarray.field->elementsize;
3641 * The actual length of the dynamic array is stored
3642 * in the top half of the field, and the offset
3643 * is in the bottom half of the 32 bit field.
3648 case TEP_PRINT_FIELD:
3649 if (!larg->field.field) {
3651 tep_find_any_field(event, larg->field.name);
3652 if (!larg->field.field) {
3654 goto out_warning_field;
3657 field_size = larg->field.field->elementsize;
3658 offset = larg->field.field->offset +
3659 right * larg->field.field->elementsize;
3662 goto default_op; /* oops, all bets off */
3664 val = tep_read_number(tep,
3665 data + offset, field_size);
3667 val = eval_type(val, typearg, 1);
3669 } else if (strcmp(arg->op.op, "?") == 0) {
3670 left = eval_num_arg(data, size, event, arg->op.left);
3671 arg = arg->op.right;
3673 val = eval_num_arg(data, size, event, arg->op.left);
3675 val = eval_num_arg(data, size, event, arg->op.right);
3679 left = eval_num_arg(data, size, event, arg->op.left);
3680 right = eval_num_arg(data, size, event, arg->op.right);
3681 switch (arg->op.op[0]) {
3683 switch (arg->op.op[1]) {
3688 val = left != right;
3691 goto out_warning_op;
3699 val = left || right;
3705 val = left && right;
3710 switch (arg->op.op[1]) {
3715 val = left << right;
3718 val = left <= right;
3721 goto out_warning_op;
3725 switch (arg->op.op[1]) {
3730 val = left >> right;
3733 val = left >= right;
3736 goto out_warning_op;
3740 if (arg->op.op[1] != '=')
3741 goto out_warning_op;
3743 val = left == right;
3761 goto out_warning_op;
3764 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3765 offset = tep_read_number(tep,
3766 data + arg->dynarray.field->offset,
3767 arg->dynarray.field->size);
3769 * The total allocated length of the dynamic array is
3770 * stored in the top half of the field, and the offset
3771 * is in the bottom half of the 32 bit field.
3773 val = (unsigned long long)(offset >> 16);
3775 case TEP_PRINT_DYNAMIC_ARRAY:
3776 /* Without [], we pass the address to the dynamic data */
3777 offset = tep_read_number(tep,
3778 data + arg->dynarray.field->offset,
3779 arg->dynarray.field->size);
3781 * The total allocated length of the dynamic array is
3782 * stored in the top half of the field, and the offset
3783 * is in the bottom half of the 32 bit field.
3786 val = (unsigned long long)((unsigned long)data + offset);
3788 default: /* not sure what to do there */
3794 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3798 do_warning_event(event, "%s: field %s not found",
3799 __func__, arg->field.name);
3805 unsigned long long value;
3808 static const struct flag flags[] = {
3809 { "HI_SOFTIRQ", 0 },
3810 { "TIMER_SOFTIRQ", 1 },
3811 { "NET_TX_SOFTIRQ", 2 },
3812 { "NET_RX_SOFTIRQ", 3 },
3813 { "BLOCK_SOFTIRQ", 4 },
3814 { "IRQ_POLL_SOFTIRQ", 5 },
3815 { "TASKLET_SOFTIRQ", 6 },
3816 { "SCHED_SOFTIRQ", 7 },
3817 { "HRTIMER_SOFTIRQ", 8 },
3818 { "RCU_SOFTIRQ", 9 },
3820 { "HRTIMER_NORESTART", 0 },
3821 { "HRTIMER_RESTART", 1 },
3824 static long long eval_flag(const char *flag)
3829 * Some flags in the format files do not get converted.
3830 * If the flag is not numeric, see if it is something that
3831 * we already know about.
3833 if (isdigit(flag[0]))
3834 return strtoull(flag, NULL, 0);
3836 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3837 if (strcmp(flags[i].name, flag) == 0)
3838 return flags[i].value;
3843 static void print_str_to_seq(struct trace_seq *s, const char *format,
3844 int len_arg, const char *str)
3847 trace_seq_printf(s, format, len_arg, str);
3849 trace_seq_printf(s, format, str);
3852 static void print_bitmask_to_seq(struct tep_handle *tep,
3853 struct trace_seq *s, const char *format,
3854 int len_arg, const void *data, int size)
3856 int nr_bits = size * 8;
3857 int str_size = (nr_bits + 3) / 4;
3865 * The kernel likes to put in commas every 32 bits, we
3868 str_size += (nr_bits - 1) / 32;
3870 str = malloc(str_size + 1);
3872 do_warning("%s: not enough memory!", __func__);
3877 /* Start out with -2 for the two chars per byte */
3878 for (i = str_size - 2; i >= 0; i -= 2) {
3880 * data points to a bit mask of size bytes.
3881 * In the kernel, this is an array of long words, thus
3882 * endianness is very important.
3884 if (tep->file_bigendian)
3885 index = size - (len + 1);
3889 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3890 memcpy(str + i, buf, 2);
3892 if (!(len & 3) && i > 0) {
3899 trace_seq_printf(s, format, len_arg, str);
3901 trace_seq_printf(s, format, str);
3906 static void print_str_arg(struct trace_seq *s, void *data, int size,
3907 struct tep_event *event, const char *format,
3908 int len_arg, struct tep_print_arg *arg)
3910 struct tep_handle *tep = event->tep;
3911 struct tep_print_flag_sym *flag;
3912 struct tep_format_field *field;
3913 struct printk_map *printk;
3914 long long val, fval;
3915 unsigned long long addr;
3921 switch (arg->type) {
3922 case TEP_PRINT_NULL:
3925 case TEP_PRINT_ATOM:
3926 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3928 case TEP_PRINT_FIELD:
3929 field = arg->field.field;
3931 field = tep_find_any_field(event, arg->field.name);
3933 str = arg->field.name;
3934 goto out_warning_field;
3936 arg->field.field = field;
3938 /* Zero sized fields, mean the rest of the data */
3939 len = field->size ? : size - field->offset;
3942 * Some events pass in pointers. If this is not an array
3943 * and the size is the same as long_size, assume that it
3946 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
3947 field->size == tep->long_size) {
3949 /* Handle heterogeneous recording and processing
3953 * Traces recorded on 32-bit devices (32-bit
3954 * addressing) and processed on 64-bit devices:
3955 * In this case, only 32 bits should be read.
3958 * Traces recorded on 64 bit devices and processed
3959 * on 32-bit devices:
3960 * In this case, 64 bits must be read.
3962 addr = (tep->long_size == 8) ?
3963 *(unsigned long long *)(data + field->offset) :
3964 (unsigned long long)*(unsigned int *)(data + field->offset);
3966 /* Check if it matches a print format */
3967 printk = find_printk(tep, addr);
3969 trace_seq_puts(s, printk->printk);
3971 trace_seq_printf(s, "%llx", addr);
3974 str = malloc(len + 1);
3976 do_warning_event(event, "%s: not enough memory!",
3980 memcpy(str, data + field->offset, len);
3982 print_str_to_seq(s, format, len_arg, str);
3985 case TEP_PRINT_FLAGS:
3986 val = eval_num_arg(data, size, event, arg->flags.field);
3988 for (flag = arg->flags.flags; flag; flag = flag->next) {
3989 fval = eval_flag(flag->value);
3990 if (!val && fval < 0) {
3991 print_str_to_seq(s, format, len_arg, flag->str);
3994 if (fval > 0 && (val & fval) == fval) {
3995 if (print && arg->flags.delim)
3996 trace_seq_puts(s, arg->flags.delim);
3997 print_str_to_seq(s, format, len_arg, flag->str);
4003 if (print && arg->flags.delim)
4004 trace_seq_puts(s, arg->flags.delim);
4005 trace_seq_printf(s, "0x%llx", val);
4008 case TEP_PRINT_SYMBOL:
4009 val = eval_num_arg(data, size, event, arg->symbol.field);
4010 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4011 fval = eval_flag(flag->value);
4013 print_str_to_seq(s, format, len_arg, flag->str);
4018 trace_seq_printf(s, "0x%llx", val);
4021 case TEP_PRINT_HEX_STR:
4022 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4023 unsigned long offset;
4024 offset = tep_read_number(tep,
4025 data + arg->hex.field->dynarray.field->offset,
4026 arg->hex.field->dynarray.field->size);
4027 hex = data + (offset & 0xffff);
4029 field = arg->hex.field->field.field;
4031 str = arg->hex.field->field.name;
4032 field = tep_find_any_field(event, str);
4034 goto out_warning_field;
4035 arg->hex.field->field.field = field;
4037 hex = data + field->offset;
4039 len = eval_num_arg(data, size, event, arg->hex.size);
4040 for (i = 0; i < len; i++) {
4041 if (i && arg->type == TEP_PRINT_HEX)
4042 trace_seq_putc(s, ' ');
4043 trace_seq_printf(s, "%02x", hex[i]);
4047 case TEP_PRINT_INT_ARRAY: {
4051 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4052 unsigned long offset;
4053 struct tep_format_field *field =
4054 arg->int_array.field->dynarray.field;
4055 offset = tep_read_number(tep,
4056 data + field->offset,
4058 num = data + (offset & 0xffff);
4060 field = arg->int_array.field->field.field;
4062 str = arg->int_array.field->field.name;
4063 field = tep_find_any_field(event, str);
4065 goto out_warning_field;
4066 arg->int_array.field->field.field = field;
4068 num = data + field->offset;
4070 len = eval_num_arg(data, size, event, arg->int_array.count);
4071 el_size = eval_num_arg(data, size, event,
4072 arg->int_array.el_size);
4073 for (i = 0; i < len; i++) {
4075 trace_seq_putc(s, ' ');
4078 trace_seq_printf(s, "%u", *(uint8_t *)num);
4079 } else if (el_size == 2) {
4080 trace_seq_printf(s, "%u", *(uint16_t *)num);
4081 } else if (el_size == 4) {
4082 trace_seq_printf(s, "%u", *(uint32_t *)num);
4083 } else if (el_size == 8) {
4084 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4086 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4087 el_size, *(uint8_t *)num);
4095 case TEP_PRINT_TYPE:
4097 case TEP_PRINT_STRING: {
4100 if (arg->string.offset == -1) {
4101 struct tep_format_field *f;
4103 f = tep_find_any_field(event, arg->string.string);
4104 arg->string.offset = f->offset;
4106 str_offset = tep_data2host4(tep, *(unsigned int *)(data + arg->string.offset));
4107 str_offset &= 0xffff;
4108 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4111 case TEP_PRINT_BSTRING:
4112 print_str_to_seq(s, format, len_arg, arg->string.string);
4114 case TEP_PRINT_BITMASK: {
4118 if (arg->bitmask.offset == -1) {
4119 struct tep_format_field *f;
4121 f = tep_find_any_field(event, arg->bitmask.bitmask);
4122 arg->bitmask.offset = f->offset;
4124 bitmask_offset = tep_data2host4(tep, *(unsigned int *)(data + arg->bitmask.offset));
4125 bitmask_size = bitmask_offset >> 16;
4126 bitmask_offset &= 0xffff;
4127 print_bitmask_to_seq(tep, s, format, len_arg,
4128 data + bitmask_offset, bitmask_size);
4133 * The only op for string should be ? :
4135 if (arg->op.op[0] != '?')
4137 val = eval_num_arg(data, size, event, arg->op.left);
4139 print_str_arg(s, data, size, event,
4140 format, len_arg, arg->op.right->op.left);
4142 print_str_arg(s, data, size, event,
4143 format, len_arg, arg->op.right->op.right);
4145 case TEP_PRINT_FUNC:
4146 process_defined_func(s, data, size, event, arg);
4156 do_warning_event(event, "%s: field %s not found",
4157 __func__, arg->field.name);
4160 static unsigned long long
4161 process_defined_func(struct trace_seq *s, void *data, int size,
4162 struct tep_event *event, struct tep_print_arg *arg)
4164 struct tep_function_handler *func_handle = arg->func.func;
4165 struct func_params *param;
4166 unsigned long long *args;
4167 unsigned long long ret;
4168 struct tep_print_arg *farg;
4169 struct trace_seq str;
4171 struct save_str *next;
4173 } *strings = NULL, *string;
4176 if (!func_handle->nr_args) {
4177 ret = (*func_handle->func)(s, NULL);
4181 farg = arg->func.args;
4182 param = func_handle->params;
4185 args = malloc(sizeof(*args) * func_handle->nr_args);
4189 for (i = 0; i < func_handle->nr_args; i++) {
4190 switch (param->type) {
4191 case TEP_FUNC_ARG_INT:
4192 case TEP_FUNC_ARG_LONG:
4193 case TEP_FUNC_ARG_PTR:
4194 args[i] = eval_num_arg(data, size, event, farg);
4196 case TEP_FUNC_ARG_STRING:
4197 trace_seq_init(&str);
4198 print_str_arg(&str, data, size, event, "%s", -1, farg);
4199 trace_seq_terminate(&str);
4200 string = malloc(sizeof(*string));
4202 do_warning_event(event, "%s(%d): malloc str",
4203 __func__, __LINE__);
4206 string->next = strings;
4207 string->str = strdup(str.buffer);
4210 do_warning_event(event, "%s(%d): malloc str",
4211 __func__, __LINE__);
4214 args[i] = (uintptr_t)string->str;
4216 trace_seq_destroy(&str);
4220 * Something went totally wrong, this is not
4221 * an input error, something in this code broke.
4223 do_warning_event(event, "Unexpected end of arguments\n");
4227 param = param->next;
4230 ret = (*func_handle->func)(s, args);
4235 strings = string->next;
4241 /* TBD : handle return type here */
4245 static void free_args(struct tep_print_arg *args)
4247 struct tep_print_arg *next;
4257 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4259 struct tep_handle *tep = event->tep;
4260 struct tep_format_field *field, *ip_field;
4261 struct tep_print_arg *args, *arg, **next;
4262 unsigned long long ip, val;
4267 field = tep->bprint_buf_field;
4268 ip_field = tep->bprint_ip_field;
4271 field = tep_find_field(event, "buf");
4273 do_warning_event(event, "can't find buffer field for binary printk");
4276 ip_field = tep_find_field(event, "ip");
4278 do_warning_event(event, "can't find ip field for binary printk");
4281 tep->bprint_buf_field = field;
4282 tep->bprint_ip_field = ip_field;
4285 ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
4288 * The first arg is the IP pointer.
4292 do_warning_event(event, "%s(%d): not enough memory!",
4293 __func__, __LINE__);
4300 arg->type = TEP_PRINT_ATOM;
4302 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4305 /* skip the first "%ps: " */
4306 for (ptr = fmt + 5, bptr = data + field->offset;
4307 bptr < data + size && *ptr; ptr++) {
4332 if (isalnum(ptr[1])) {
4334 /* Check for special pointers */
4344 * Older kernels do not process
4345 * dereferenced pointers.
4346 * Only process if the pointer
4347 * value is a printable.
4349 if (isprint(*(char *)bptr))
4350 goto process_string;
4363 vsize = tep->long_size;
4377 /* the pointers are always 4 bytes aligned */
4378 bptr = (void *)(((unsigned long)bptr + 3) &
4380 val = tep_read_number(tep, bptr, vsize);
4384 do_warning_event(event, "%s(%d): not enough memory!",
4385 __func__, __LINE__);
4389 arg->type = TEP_PRINT_ATOM;
4390 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4397 * The '*' case means that an arg is used as the length.
4398 * We need to continue to figure out for what.
4408 do_warning_event(event, "%s(%d): not enough memory!",
4409 __func__, __LINE__);
4413 arg->type = TEP_PRINT_BSTRING;
4414 arg->string.string = strdup(bptr);
4415 if (!arg->string.string)
4417 bptr += strlen(bptr) + 1;
4434 get_bprint_format(void *data, int size __maybe_unused,
4435 struct tep_event *event)
4437 struct tep_handle *tep = event->tep;
4438 unsigned long long addr;
4439 struct tep_format_field *field;
4440 struct printk_map *printk;
4443 field = tep->bprint_fmt_field;
4446 field = tep_find_field(event, "fmt");
4448 do_warning_event(event, "can't find format field for binary printk");
4451 tep->bprint_fmt_field = field;
4454 addr = tep_read_number(tep, data + field->offset, field->size);
4456 printk = find_printk(tep, addr);
4458 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4463 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4469 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4470 struct tep_event *event, struct tep_print_arg *arg)
4473 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4475 if (arg->type == TEP_PRINT_FUNC) {
4476 process_defined_func(s, data, size, event, arg);
4480 if (arg->type != TEP_PRINT_FIELD) {
4481 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4487 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4488 if (!arg->field.field) {
4490 tep_find_any_field(event, arg->field.name);
4491 if (!arg->field.field) {
4492 do_warning_event(event, "%s: field %s not found",
4493 __func__, arg->field.name);
4497 if (arg->field.field->size != 6) {
4498 trace_seq_printf(s, "INVALIDMAC");
4501 buf = data + arg->field.field->offset;
4502 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4505 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4510 fmt = "%03d.%03d.%03d.%03d";
4512 fmt = "%d.%d.%d.%d";
4514 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4517 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4519 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4520 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4523 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4525 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4528 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4531 unsigned char zerolength[8];
4536 bool needcolon = false;
4538 struct in6_addr in6;
4540 memcpy(&in6, addr, sizeof(struct in6_addr));
4542 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4544 memset(zerolength, 0, sizeof(zerolength));
4551 /* find position of longest 0 run */
4552 for (i = 0; i < range; i++) {
4553 for (j = i; j < range; j++) {
4554 if (in6.s6_addr16[j] != 0)
4559 for (i = 0; i < range; i++) {
4560 if (zerolength[i] > longest) {
4561 longest = zerolength[i];
4565 if (longest == 1) /* don't compress a single 0 */
4569 for (i = 0; i < range; i++) {
4570 if (i == colonpos) {
4571 if (needcolon || i == 0)
4572 trace_seq_printf(s, ":");
4573 trace_seq_printf(s, ":");
4579 trace_seq_printf(s, ":");
4582 /* hex u16 without leading 0s */
4583 word = ntohs(in6.s6_addr16[i]);
4587 trace_seq_printf(s, "%x%02x", hi, lo);
4589 trace_seq_printf(s, "%x", lo);
4596 trace_seq_printf(s, ":");
4597 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4603 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4607 for (j = 0; j < 16; j += 2) {
4608 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4609 if (i == 'I' && j < 14)
4610 trace_seq_printf(s, ":");
4615 * %pi4 print an IPv4 address with leading zeros
4616 * %pI4 print an IPv4 address without leading zeros
4617 * %pi6 print an IPv6 address without colons
4618 * %pI6 print an IPv6 address with colons
4619 * %pI6c print an IPv6 address in compressed form with colons
4620 * %pISpc print an IP address based on sockaddr; p adds port.
4622 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4623 void *data, int size, struct tep_event *event,
4624 struct tep_print_arg *arg)
4628 if (arg->type == TEP_PRINT_FUNC) {
4629 process_defined_func(s, data, size, event, arg);
4633 if (arg->type != TEP_PRINT_FIELD) {
4634 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4638 if (!arg->field.field) {
4640 tep_find_any_field(event, arg->field.name);
4641 if (!arg->field.field) {
4642 do_warning("%s: field %s not found",
4643 __func__, arg->field.name);
4648 buf = data + arg->field.field->offset;
4650 if (arg->field.field->size != 4) {
4651 trace_seq_printf(s, "INVALIDIPv4");
4654 print_ip4_addr(s, i, buf);
4659 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4660 void *data, int size, struct tep_event *event,
4661 struct tep_print_arg *arg)
4668 if (i == 'I' && *ptr == 'c') {
4674 if (arg->type == TEP_PRINT_FUNC) {
4675 process_defined_func(s, data, size, event, arg);
4679 if (arg->type != TEP_PRINT_FIELD) {
4680 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4684 if (!arg->field.field) {
4686 tep_find_any_field(event, arg->field.name);
4687 if (!arg->field.field) {
4688 do_warning("%s: field %s not found",
4689 __func__, arg->field.name);
4694 buf = data + arg->field.field->offset;
4696 if (arg->field.field->size != 16) {
4697 trace_seq_printf(s, "INVALIDIPv6");
4702 print_ip6c_addr(s, buf);
4704 print_ip6_addr(s, i, buf);
4709 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4710 void *data, int size, struct tep_event *event,
4711 struct tep_print_arg *arg)
4713 char have_c = 0, have_p = 0;
4715 struct sockaddr_storage *sa;
4732 if (arg->type == TEP_PRINT_FUNC) {
4733 process_defined_func(s, data, size, event, arg);
4737 if (arg->type != TEP_PRINT_FIELD) {
4738 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4742 if (!arg->field.field) {
4744 tep_find_any_field(event, arg->field.name);
4745 if (!arg->field.field) {
4746 do_warning("%s: field %s not found",
4747 __func__, arg->field.name);
4752 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4754 if (sa->ss_family == AF_INET) {
4755 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4757 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4758 trace_seq_printf(s, "INVALIDIPv4");
4762 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4764 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4767 } else if (sa->ss_family == AF_INET6) {
4768 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4770 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4771 trace_seq_printf(s, "INVALIDIPv6");
4776 trace_seq_printf(s, "[");
4778 buf = (unsigned char *) &sa6->sin6_addr;
4780 print_ip6c_addr(s, buf);
4782 print_ip6_addr(s, i, buf);
4785 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4791 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4792 void *data, int size, struct tep_event *event,
4793 struct tep_print_arg *arg)
4795 char i = *ptr; /* 'i' or 'I' */
4808 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4811 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4814 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4823 static int is_printable_array(char *p, unsigned int len)
4827 for (i = 0; i < len && p[i]; i++)
4828 if (!isprint(p[i]) && !isspace(p[i]))
4833 void tep_print_field(struct trace_seq *s, void *data,
4834 struct tep_format_field *field)
4836 unsigned long long val;
4837 unsigned int offset, len, i;
4838 struct tep_handle *tep = field->event->tep;
4840 if (field->flags & TEP_FIELD_IS_ARRAY) {
4841 offset = field->offset;
4843 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
4844 val = tep_read_number(tep, data + offset, len);
4849 if (field->flags & TEP_FIELD_IS_STRING &&
4850 is_printable_array(data + offset, len)) {
4851 trace_seq_printf(s, "%s", (char *)data + offset);
4853 trace_seq_puts(s, "ARRAY[");
4854 for (i = 0; i < len; i++) {
4856 trace_seq_puts(s, ", ");
4857 trace_seq_printf(s, "%02x",
4858 *((unsigned char *)data + offset + i));
4860 trace_seq_putc(s, ']');
4861 field->flags &= ~TEP_FIELD_IS_STRING;
4864 val = tep_read_number(tep, data + field->offset,
4866 if (field->flags & TEP_FIELD_IS_POINTER) {
4867 trace_seq_printf(s, "0x%llx", val);
4868 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
4869 switch (field->size) {
4872 * If field is long then print it in hex.
4873 * A long usually stores pointers.
4875 if (field->flags & TEP_FIELD_IS_LONG)
4876 trace_seq_printf(s, "0x%x", (int)val);
4878 trace_seq_printf(s, "%d", (int)val);
4881 trace_seq_printf(s, "%2d", (short)val);
4884 trace_seq_printf(s, "%1d", (char)val);
4887 trace_seq_printf(s, "%lld", val);
4890 if (field->flags & TEP_FIELD_IS_LONG)
4891 trace_seq_printf(s, "0x%llx", val);
4893 trace_seq_printf(s, "%llu", val);
4898 void tep_print_fields(struct trace_seq *s, void *data,
4899 int size __maybe_unused, struct tep_event *event)
4901 struct tep_format_field *field;
4903 field = event->format.fields;
4905 trace_seq_printf(s, " %s=", field->name);
4906 tep_print_field(s, data, field);
4907 field = field->next;
4911 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
4913 struct tep_handle *tep = event->tep;
4914 struct tep_print_fmt *print_fmt = &event->print_fmt;
4915 struct tep_print_arg *arg = print_fmt->args;
4916 struct tep_print_arg *args = NULL;
4917 const char *ptr = print_fmt->format;
4918 unsigned long long val;
4919 struct func_map *func;
4920 const char *saveptr;
4922 char *bprint_fmt = NULL;
4930 if (event->flags & TEP_EVENT_FL_FAILED) {
4931 trace_seq_printf(s, "[FAILED TO PARSE]");
4932 tep_print_fields(s, data, size, event);
4936 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
4937 bprint_fmt = get_bprint_format(data, size, event);
4938 args = make_bprint_args(bprint_fmt, data, size, event);
4943 for (; *ptr; ptr++) {
4949 trace_seq_putc(s, '\n');
4952 trace_seq_putc(s, '\t');
4955 trace_seq_putc(s, '\r');
4958 trace_seq_putc(s, '\\');
4961 trace_seq_putc(s, *ptr);
4965 } else if (*ptr == '%') {
4973 trace_seq_putc(s, '%');
4976 /* FIXME: need to handle properly */
4988 /* The argument is the length. */
4990 do_warning_event(event, "no argument match");
4991 event->flags |= TEP_EVENT_FL_FAILED;
4994 len_arg = eval_num_arg(data, size, event, arg);
5005 if (tep->long_size == 4)
5010 if (isalnum(ptr[1]))
5013 if (arg->type == TEP_PRINT_BSTRING) {
5014 trace_seq_puts(s, arg->string.string);
5019 if (*ptr == 'F' || *ptr == 'f' ||
5020 *ptr == 'S' || *ptr == 's') {
5022 } else if (*ptr == 'M' || *ptr == 'm') {
5023 print_mac_arg(s, *ptr, data, size, event, arg);
5026 } else if (*ptr == 'I' || *ptr == 'i') {
5029 n = print_ip_arg(s, ptr, data, size, event, arg);
5044 do_warning_event(event, "no argument match");
5045 event->flags |= TEP_EVENT_FL_FAILED;
5049 len = ((unsigned long)ptr + 1) -
5050 (unsigned long)saveptr;
5052 /* should never happen */
5054 do_warning_event(event, "bad format!");
5055 event->flags |= TEP_EVENT_FL_FAILED;
5059 memcpy(format, saveptr, len);
5062 val = eval_num_arg(data, size, event, arg);
5066 func = find_func(tep, val);
5068 trace_seq_puts(s, func->func);
5069 if (show_func == 'F')
5076 if (tep->long_size == 8 && ls == 1 &&
5077 sizeof(long) != 8) {
5080 /* make %l into %ll */
5081 if (ls == 1 && (p = strchr(format, 'l')))
5082 memmove(p+1, p, strlen(p)+1);
5083 else if (strcmp(format, "%p") == 0)
5084 strcpy(format, "0x%llx");
5090 trace_seq_printf(s, format, len_arg, (char)val);
5092 trace_seq_printf(s, format, (char)val);
5096 trace_seq_printf(s, format, len_arg, (short)val);
5098 trace_seq_printf(s, format, (short)val);
5102 trace_seq_printf(s, format, len_arg, (int)val);
5104 trace_seq_printf(s, format, (int)val);
5108 trace_seq_printf(s, format, len_arg, (long)val);
5110 trace_seq_printf(s, format, (long)val);
5114 trace_seq_printf(s, format, len_arg,
5117 trace_seq_printf(s, format, (long long)val);
5120 do_warning_event(event, "bad count (%d)", ls);
5121 event->flags |= TEP_EVENT_FL_FAILED;
5126 do_warning_event(event, "no matching argument");
5127 event->flags |= TEP_EVENT_FL_FAILED;
5131 len = ((unsigned long)ptr + 1) -
5132 (unsigned long)saveptr;
5134 /* should never happen */
5136 do_warning_event(event, "bad format!");
5137 event->flags |= TEP_EVENT_FL_FAILED;
5141 memcpy(format, saveptr, len);
5145 /* Use helper trace_seq */
5147 print_str_arg(&p, data, size, event,
5148 format, len_arg, arg);
5149 trace_seq_terminate(&p);
5150 trace_seq_puts(s, p.buffer);
5151 trace_seq_destroy(&p);
5155 trace_seq_printf(s, ">%c<", *ptr);
5159 trace_seq_putc(s, *ptr);
5162 if (event->flags & TEP_EVENT_FL_FAILED) {
5164 trace_seq_printf(s, "[FAILED TO PARSE]");
5174 * tep_data_latency_format - parse the data for the latency format
5175 * @tep: a handle to the trace event parser context
5176 * @s: the trace_seq to write to
5177 * @record: the record to read from
5179 * This parses out the Latency format (interrupts disabled,
5180 * need rescheduling, in hard/soft interrupt, preempt count
5181 * and lock depth) and places it into the trace_seq.
5183 void tep_data_latency_format(struct tep_handle *tep,
5184 struct trace_seq *s, struct tep_record *record)
5186 static int check_lock_depth = 1;
5187 static int check_migrate_disable = 1;
5188 static int lock_depth_exists;
5189 static int migrate_disable_exists;
5190 unsigned int lat_flags;
5193 int migrate_disable = 0;
5196 void *data = record->data;
5198 lat_flags = parse_common_flags(tep, data);
5199 pc = parse_common_pc(tep, data);
5200 /* lock_depth may not always exist */
5201 if (lock_depth_exists)
5202 lock_depth = parse_common_lock_depth(tep, data);
5203 else if (check_lock_depth) {
5204 lock_depth = parse_common_lock_depth(tep, data);
5206 check_lock_depth = 0;
5208 lock_depth_exists = 1;
5211 /* migrate_disable may not always exist */
5212 if (migrate_disable_exists)
5213 migrate_disable = parse_common_migrate_disable(tep, data);
5214 else if (check_migrate_disable) {
5215 migrate_disable = parse_common_migrate_disable(tep, data);
5216 if (migrate_disable < 0)
5217 check_migrate_disable = 0;
5219 migrate_disable_exists = 1;
5222 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5223 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5225 trace_seq_printf(s, "%c%c%c",
5226 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5227 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5229 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5231 (hardirq && softirq) ? 'H' :
5232 hardirq ? 'h' : softirq ? 's' : '.');
5235 trace_seq_printf(s, "%x", pc);
5237 trace_seq_putc(s, '.');
5239 if (migrate_disable_exists) {
5240 if (migrate_disable < 0)
5241 trace_seq_putc(s, '.');
5243 trace_seq_printf(s, "%d", migrate_disable);
5246 if (lock_depth_exists) {
5248 trace_seq_putc(s, '.');
5250 trace_seq_printf(s, "%d", lock_depth);
5253 trace_seq_terminate(s);
5257 * tep_data_type - parse out the given event type
5258 * @tep: a handle to the trace event parser context
5259 * @rec: the record to read from
5261 * This returns the event id from the @rec.
5263 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
5265 return trace_parse_common_type(tep, rec->data);
5269 * tep_data_pid - parse the PID from record
5270 * @tep: a handle to the trace event parser context
5271 * @rec: the record to parse
5273 * This returns the PID from a record.
5275 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
5277 return parse_common_pid(tep, rec->data);
5281 * tep_data_preempt_count - parse the preempt count from the record
5282 * @tep: a handle to the trace event parser context
5283 * @rec: the record to parse
5285 * This returns the preempt count from a record.
5287 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
5289 return parse_common_pc(tep, rec->data);
5293 * tep_data_flags - parse the latency flags from the record
5294 * @tep: a handle to the trace event parser context
5295 * @rec: the record to parse
5297 * This returns the latency flags from a record.
5299 * Use trace_flag_type enum for the flags (see event-parse.h).
5301 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
5303 return parse_common_flags(tep, rec->data);
5307 * tep_data_comm_from_pid - return the command line from PID
5308 * @tep: a handle to the trace event parser context
5309 * @pid: the PID of the task to search for
5311 * This returns a pointer to the command line that has the given
5314 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
5318 comm = find_cmdline(tep, pid);
5322 static struct tep_cmdline *
5323 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
5325 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5328 cmdlist = cmdlist->next;
5330 cmdlist = tep->cmdlist;
5332 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5333 cmdlist = cmdlist->next;
5335 return (struct tep_cmdline *)cmdlist;
5339 * tep_data_pid_from_comm - return the pid from a given comm
5340 * @tep: a handle to the trace event parser context
5341 * @comm: the cmdline to find the pid from
5342 * @next: the cmdline structure to find the next comm
5344 * This returns the cmdline structure that holds a pid for a given
5345 * comm, or NULL if none found. As there may be more than one pid for
5346 * a given comm, the result of this call can be passed back into
5347 * a recurring call in the @next parameter, and then it will find the
5349 * Also, it does a linear search, so it may be slow.
5351 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
5352 struct tep_cmdline *next)
5354 struct tep_cmdline *cmdline;
5357 * If the cmdlines have not been converted yet, then use
5361 return pid_from_cmdlist(tep, comm, next);
5365 * The next pointer could have been still from
5366 * a previous call before cmdlines were created
5368 if (next < tep->cmdlines ||
5369 next >= tep->cmdlines + tep->cmdline_count)
5376 cmdline = tep->cmdlines;
5378 while (cmdline < tep->cmdlines + tep->cmdline_count) {
5379 if (strcmp(cmdline->comm, comm) == 0)
5387 * tep_cmdline_pid - return the pid associated to a given cmdline
5388 * @tep: a handle to the trace event parser context
5389 * @cmdline: The cmdline structure to get the pid from
5391 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5394 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
5396 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5402 * If cmdlines have not been created yet, or cmdline is
5403 * not part of the array, then treat it as a cmdlist instead.
5405 if (!tep->cmdlines ||
5406 cmdline < tep->cmdlines ||
5407 cmdline >= tep->cmdlines + tep->cmdline_count)
5408 return cmdlist->pid;
5410 return cmdline->pid;
5414 * tep_event_info - parse the data into the print format
5415 * @s: the trace_seq to write to
5416 * @event: the handle to the event
5417 * @record: the record to read from
5419 * This parses the raw @data using the given @event information and
5420 * writes the print format into the trace_seq.
5422 void tep_event_info(struct trace_seq *s, struct tep_event *event,
5423 struct tep_record *record)
5425 int print_pretty = 1;
5427 if (event->tep->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
5428 tep_print_fields(s, record->data, record->size, event);
5431 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
5432 print_pretty = event->handler(s, record, event,
5436 pretty_print(s, record->data, record->size, event);
5439 trace_seq_terminate(s);
5442 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5444 if (!trace_clock || !use_trace_clock)
5447 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5448 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf")
5449 || !strncmp(trace_clock, "mono", 4))
5452 /* trace_clock is setting in tsc or counter mode */
5457 * tep_find_event_by_record - return the event from a given record
5458 * @tep: a handle to the trace event parser context
5459 * @record: The record to get the event from
5461 * Returns the associated event for a given record, or NULL if non is
5465 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
5469 if (record->size < 0) {
5470 do_warning("ug! negative record size %d", record->size);
5474 type = trace_parse_common_type(tep, record->data);
5476 return tep_find_event(tep, type);
5480 * tep_print_event_task - Write the event task comm, pid and CPU
5481 * @tep: a handle to the trace event parser context
5482 * @s: the trace_seq to write to
5483 * @event: the handle to the record's event
5484 * @record: The record to get the event from
5486 * Writes the tasks comm, pid and CPU to @s.
5488 void tep_print_event_task(struct tep_handle *tep, struct trace_seq *s,
5489 struct tep_event *event,
5490 struct tep_record *record)
5492 void *data = record->data;
5496 pid = parse_common_pid(tep, data);
5497 comm = find_cmdline(tep, pid);
5499 if (tep->latency_format)
5500 trace_seq_printf(s, "%8.8s-%-5d %3d", comm, pid, record->cpu);
5502 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5506 * tep_print_event_time - Write the event timestamp
5507 * @tep: a handle to the trace event parser context
5508 * @s: the trace_seq to write to
5509 * @event: the handle to the record's event
5510 * @record: The record to get the event from
5511 * @use_trace_clock: Set to parse according to the @tep->trace_clock
5513 * Writes the timestamp of the record into @s.
5515 void tep_print_event_time(struct tep_handle *tep, struct trace_seq *s,
5516 struct tep_event *event,
5517 struct tep_record *record,
5518 bool use_trace_clock)
5521 unsigned long usecs;
5522 unsigned long nsecs;
5524 bool use_usec_format;
5526 use_usec_format = is_timestamp_in_us(tep->trace_clock, use_trace_clock);
5527 if (use_usec_format) {
5528 secs = record->ts / NSEC_PER_SEC;
5529 nsecs = record->ts - secs * NSEC_PER_SEC;
5532 if (tep->latency_format) {
5533 tep_data_latency_format(tep, s, record);
5536 if (use_usec_format) {
5537 if (tep->flags & TEP_NSEC_OUTPUT) {
5541 usecs = (nsecs + 500) / NSEC_PER_USEC;
5542 /* To avoid usecs larger than 1 sec */
5543 if (usecs >= USEC_PER_SEC) {
5544 usecs -= USEC_PER_SEC;
5550 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5552 trace_seq_printf(s, " %12llu:", record->ts);
5556 * tep_print_event_data - Write the event data section
5557 * @tep: a handle to the trace event parser context
5558 * @s: the trace_seq to write to
5559 * @event: the handle to the record's event
5560 * @record: The record to get the event from
5562 * Writes the parsing of the record's data to @s.
5564 void tep_print_event_data(struct tep_handle *tep, struct trace_seq *s,
5565 struct tep_event *event,
5566 struct tep_record *record)
5568 static const char *spaces = " "; /* 20 spaces */
5571 trace_seq_printf(s, " %s: ", event->name);
5573 /* Space out the event names evenly. */
5574 len = strlen(event->name);
5576 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5578 tep_event_info(s, event, record);
5581 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
5582 struct tep_record *record, bool use_trace_clock)
5584 struct tep_event *event;
5586 event = tep_find_event_by_record(tep, record);
5589 int type = trace_parse_common_type(tep, record->data);
5591 do_warning("ug! no event found for type %d", type);
5592 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5593 for (i = 0; i < record->size; i++)
5594 trace_seq_printf(s, " %02x",
5595 ((unsigned char *)record->data)[i]);
5599 tep_print_event_task(tep, s, event, record);
5600 tep_print_event_time(tep, s, event, record, use_trace_clock);
5601 tep_print_event_data(tep, s, event, record);
5604 static int events_id_cmp(const void *a, const void *b)
5606 struct tep_event * const * ea = a;
5607 struct tep_event * const * eb = b;
5609 if ((*ea)->id < (*eb)->id)
5612 if ((*ea)->id > (*eb)->id)
5618 static int events_name_cmp(const void *a, const void *b)
5620 struct tep_event * const * ea = a;
5621 struct tep_event * const * eb = b;
5624 res = strcmp((*ea)->name, (*eb)->name);
5628 res = strcmp((*ea)->system, (*eb)->system);
5632 return events_id_cmp(a, b);
5635 static int events_system_cmp(const void *a, const void *b)
5637 struct tep_event * const * ea = a;
5638 struct tep_event * const * eb = b;
5641 res = strcmp((*ea)->system, (*eb)->system);
5645 res = strcmp((*ea)->name, (*eb)->name);
5649 return events_id_cmp(a, b);
5652 static struct tep_event **list_events_copy(struct tep_handle *tep)
5654 struct tep_event **events;
5659 events = malloc(sizeof(*events) * (tep->nr_events + 1));
5663 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
5664 events[tep->nr_events] = NULL;
5668 static void list_events_sort(struct tep_event **events, int nr_events,
5669 enum tep_event_sort_type sort_type)
5671 int (*sort)(const void *a, const void *b);
5673 switch (sort_type) {
5674 case TEP_EVENT_SORT_ID:
5675 sort = events_id_cmp;
5677 case TEP_EVENT_SORT_NAME:
5678 sort = events_name_cmp;
5680 case TEP_EVENT_SORT_SYSTEM:
5681 sort = events_system_cmp;
5688 qsort(events, nr_events, sizeof(*events), sort);
5692 * tep_list_events - Get events, sorted by given criteria.
5693 * @tep: a handle to the tep context
5694 * @sort_type: desired sort order of the events in the array
5696 * Returns an array of pointers to all events, sorted by the given
5697 * @sort_type criteria. The last element of the array is NULL. The returned
5698 * memory must not be freed, it is managed by the library.
5699 * The function is not thread safe.
5701 struct tep_event **tep_list_events(struct tep_handle *tep,
5702 enum tep_event_sort_type sort_type)
5704 struct tep_event **events;
5709 events = tep->sort_events;
5710 if (events && tep->last_type == sort_type)
5714 events = list_events_copy(tep);
5718 tep->sort_events = events;
5720 /* the internal events are sorted by id */
5721 if (sort_type == TEP_EVENT_SORT_ID) {
5722 tep->last_type = sort_type;
5727 list_events_sort(events, tep->nr_events, sort_type);
5728 tep->last_type = sort_type;
5735 * tep_list_events_copy - Thread safe version of tep_list_events()
5736 * @tep: a handle to the tep context
5737 * @sort_type: desired sort order of the events in the array
5739 * Returns an array of pointers to all events, sorted by the given
5740 * @sort_type criteria. The last element of the array is NULL. The returned
5741 * array is newly allocated inside the function and must be freed by the caller
5743 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
5744 enum tep_event_sort_type sort_type)
5746 struct tep_event **events;
5751 events = list_events_copy(tep);
5755 /* the internal events are sorted by id */
5756 if (sort_type == TEP_EVENT_SORT_ID)
5759 list_events_sort(events, tep->nr_events, sort_type);
5764 static struct tep_format_field **
5765 get_event_fields(const char *type, const char *name,
5766 int count, struct tep_format_field *list)
5768 struct tep_format_field **fields;
5769 struct tep_format_field *field;
5772 fields = malloc(sizeof(*fields) * (count + 1));
5776 for (field = list; field; field = field->next) {
5777 fields[i++] = field;
5778 if (i == count + 1) {
5779 do_warning("event %s has more %s fields than specified",
5787 do_warning("event %s has less %s fields than specified",
5796 * tep_event_common_fields - return a list of common fields for an event
5797 * @event: the event to return the common fields of.
5799 * Returns an allocated array of fields. The last item in the array is NULL.
5800 * The array must be freed with free().
5802 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
5804 return get_event_fields("common", event->name,
5805 event->format.nr_common,
5806 event->format.common_fields);
5810 * tep_event_fields - return a list of event specific fields for an event
5811 * @event: the event to return the fields of.
5813 * Returns an allocated array of fields. The last item in the array is NULL.
5814 * The array must be freed with free().
5816 struct tep_format_field **tep_event_fields(struct tep_event *event)
5818 return get_event_fields("event", event->name,
5819 event->format.nr_fields,
5820 event->format.fields);
5823 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
5825 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5827 trace_seq_puts(s, ", ");
5828 print_fields(s, field->next);
5833 static void print_args(struct tep_print_arg *args)
5835 int print_paren = 1;
5838 switch (args->type) {
5839 case TEP_PRINT_NULL:
5842 case TEP_PRINT_ATOM:
5843 printf("%s", args->atom.atom);
5845 case TEP_PRINT_FIELD:
5846 printf("REC->%s", args->field.name);
5848 case TEP_PRINT_FLAGS:
5849 printf("__print_flags(");
5850 print_args(args->flags.field);
5851 printf(", %s, ", args->flags.delim);
5853 print_fields(&s, args->flags.flags);
5854 trace_seq_do_printf(&s);
5855 trace_seq_destroy(&s);
5858 case TEP_PRINT_SYMBOL:
5859 printf("__print_symbolic(");
5860 print_args(args->symbol.field);
5863 print_fields(&s, args->symbol.symbols);
5864 trace_seq_do_printf(&s);
5865 trace_seq_destroy(&s);
5869 printf("__print_hex(");
5870 print_args(args->hex.field);
5872 print_args(args->hex.size);
5875 case TEP_PRINT_HEX_STR:
5876 printf("__print_hex_str(");
5877 print_args(args->hex.field);
5879 print_args(args->hex.size);
5882 case TEP_PRINT_INT_ARRAY:
5883 printf("__print_array(");
5884 print_args(args->int_array.field);
5886 print_args(args->int_array.count);
5888 print_args(args->int_array.el_size);
5891 case TEP_PRINT_STRING:
5892 case TEP_PRINT_BSTRING:
5893 printf("__get_str(%s)", args->string.string);
5895 case TEP_PRINT_BITMASK:
5896 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5898 case TEP_PRINT_TYPE:
5899 printf("(%s)", args->typecast.type);
5900 print_args(args->typecast.item);
5903 if (strcmp(args->op.op, ":") == 0)
5907 print_args(args->op.left);
5908 printf(" %s ", args->op.op);
5909 print_args(args->op.right);
5914 /* we should warn... */
5919 print_args(args->next);
5923 static void parse_header_field(const char *field,
5924 int *offset, int *size, int mandatory)
5926 unsigned long long save_input_buf_ptr;
5927 unsigned long long save_input_buf_siz;
5931 save_input_buf_ptr = input_buf_ptr;
5932 save_input_buf_siz = input_buf_siz;
5934 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
5936 if (read_expected(TEP_EVENT_OP, ":") < 0)
5940 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5945 * If this is not a mandatory field, then test it first.
5948 if (read_expected(TEP_EVENT_ITEM, field) < 0)
5951 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5953 if (strcmp(token, field) != 0)
5958 if (read_expected(TEP_EVENT_OP, ";") < 0)
5960 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
5962 if (read_expected(TEP_EVENT_OP, ":") < 0)
5964 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5966 *offset = atoi(token);
5968 if (read_expected(TEP_EVENT_OP, ";") < 0)
5970 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
5972 if (read_expected(TEP_EVENT_OP, ":") < 0)
5974 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5976 *size = atoi(token);
5978 if (read_expected(TEP_EVENT_OP, ";") < 0)
5980 type = read_token(&token);
5981 if (type != TEP_EVENT_NEWLINE) {
5982 /* newer versions of the kernel have a "signed" type */
5983 if (type != TEP_EVENT_ITEM)
5986 if (strcmp(token, "signed") != 0)
5991 if (read_expected(TEP_EVENT_OP, ":") < 0)
5994 if (read_expect_type(TEP_EVENT_ITEM, &token))
5998 if (read_expected(TEP_EVENT_OP, ";") < 0)
6001 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
6009 input_buf_ptr = save_input_buf_ptr;
6010 input_buf_siz = save_input_buf_siz;
6017 * tep_parse_header_page - parse the data stored in the header page
6018 * @tep: a handle to the trace event parser context
6019 * @buf: the buffer storing the header page format string
6020 * @size: the size of @buf
6021 * @long_size: the long size to use if there is no header
6023 * This parses the header page format for information on the
6024 * ring buffer used. The @buf should be copied from
6026 * /sys/kernel/debug/tracing/events/header_page
6028 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
6035 * Old kernels did not have header page info.
6036 * Sorry but we just use what we find here in user space.
6038 tep->header_page_ts_size = sizeof(long long);
6039 tep->header_page_size_size = long_size;
6040 tep->header_page_data_offset = sizeof(long long) + long_size;
6041 tep->old_format = 1;
6044 init_input_buf(buf, size);
6046 parse_header_field("timestamp", &tep->header_page_ts_offset,
6047 &tep->header_page_ts_size, 1);
6048 parse_header_field("commit", &tep->header_page_size_offset,
6049 &tep->header_page_size_size, 1);
6050 parse_header_field("overwrite", &tep->header_page_overwrite,
6052 parse_header_field("data", &tep->header_page_data_offset,
6053 &tep->header_page_data_size, 1);
6058 static int event_matches(struct tep_event *event,
6059 int id, const char *sys_name,
6060 const char *event_name)
6062 if (id >= 0 && id != event->id)
6065 if (event_name && (strcmp(event_name, event->name) != 0))
6068 if (sys_name && (strcmp(sys_name, event->system) != 0))
6074 static void free_handler(struct event_handler *handle)
6076 free((void *)handle->sys_name);
6077 free((void *)handle->event_name);
6081 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
6083 struct event_handler *handle, **next;
6085 for (next = &tep->handlers; *next;
6086 next = &(*next)->next) {
6088 if (event_matches(event, handle->id,
6090 handle->event_name))
6097 pr_stat("overriding event (%d) %s:%s with new print handler",
6098 event->id, event->system, event->name);
6100 event->handler = handle->func;
6101 event->context = handle->context;
6103 *next = handle->next;
6104 free_handler(handle);
6110 * __tep_parse_format - parse the event format
6111 * @buf: the buffer storing the event format string
6112 * @size: the size of @buf
6113 * @sys: the system the event belongs to
6115 * This parses the event format and creates an event structure
6116 * to quickly parse raw data for a given event.
6118 * These files currently come from:
6120 * /sys/kernel/debug/tracing/events/.../.../format
6122 enum tep_errno __tep_parse_format(struct tep_event **eventp,
6123 struct tep_handle *tep, const char *buf,
6124 unsigned long size, const char *sys)
6126 struct tep_event *event;
6129 init_input_buf(buf, size);
6131 *eventp = event = alloc_event();
6133 return TEP_ERRNO__MEM_ALLOC_FAILED;
6135 event->name = event_read_name();
6138 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6139 goto event_alloc_failed;
6142 if (strcmp(sys, "ftrace") == 0) {
6143 event->flags |= TEP_EVENT_FL_ISFTRACE;
6145 if (strcmp(event->name, "bprint") == 0)
6146 event->flags |= TEP_EVENT_FL_ISBPRINT;
6149 event->id = event_read_id();
6150 if (event->id < 0) {
6151 ret = TEP_ERRNO__READ_ID_FAILED;
6153 * This isn't an allocation error actually.
6154 * But as the ID is critical, just bail out.
6156 goto event_alloc_failed;
6159 event->system = strdup(sys);
6160 if (!event->system) {
6161 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6162 goto event_alloc_failed;
6165 /* Add tep to event so that it can be referenced */
6168 ret = event_read_format(event);
6170 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6171 goto event_parse_failed;
6175 * If the event has an override, don't print warnings if the event
6176 * print format fails to parse.
6178 if (tep && find_event_handle(tep, event))
6181 ret = event_read_print(event);
6185 ret = TEP_ERRNO__READ_PRINT_FAILED;
6186 goto event_parse_failed;
6189 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6190 struct tep_format_field *field;
6191 struct tep_print_arg *arg, **list;
6193 /* old ftrace had no args */
6194 list = &event->print_fmt.args;
6195 for (field = event->format.fields; field; field = field->next) {
6198 event->flags |= TEP_EVENT_FL_FAILED;
6199 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6201 arg->type = TEP_PRINT_FIELD;
6202 arg->field.name = strdup(field->name);
6203 if (!arg->field.name) {
6204 event->flags |= TEP_EVENT_FL_FAILED;
6206 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6208 arg->field.field = field;
6218 event->flags |= TEP_EVENT_FL_FAILED;
6222 free(event->system);
6229 static enum tep_errno
6230 __parse_event(struct tep_handle *tep,
6231 struct tep_event **eventp,
6232 const char *buf, unsigned long size,
6235 int ret = __tep_parse_format(eventp, tep, buf, size, sys);
6236 struct tep_event *event = *eventp;
6241 if (tep && add_event(tep, event)) {
6242 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6243 goto event_add_failed;
6246 #define PRINT_ARGS 0
6247 if (PRINT_ARGS && event->print_fmt.args)
6248 print_args(event->print_fmt.args);
6253 tep_free_event(event);
6258 * tep_parse_format - parse the event format
6259 * @tep: a handle to the trace event parser context
6260 * @eventp: returned format
6261 * @buf: the buffer storing the event format string
6262 * @size: the size of @buf
6263 * @sys: the system the event belongs to
6265 * This parses the event format and creates an event structure
6266 * to quickly parse raw data for a given event.
6268 * These files currently come from:
6270 * /sys/kernel/debug/tracing/events/.../.../format
6272 enum tep_errno tep_parse_format(struct tep_handle *tep,
6273 struct tep_event **eventp,
6275 unsigned long size, const char *sys)
6277 return __parse_event(tep, eventp, buf, size, sys);
6281 * tep_parse_event - parse the event format
6282 * @tep: a handle to the trace event parser context
6283 * @buf: the buffer storing the event format string
6284 * @size: the size of @buf
6285 * @sys: the system the event belongs to
6287 * This parses the event format and creates an event structure
6288 * to quickly parse raw data for a given event.
6290 * These files currently come from:
6292 * /sys/kernel/debug/tracing/events/.../.../format
6294 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
6295 unsigned long size, const char *sys)
6297 struct tep_event *event = NULL;
6298 return __parse_event(tep, &event, buf, size, sys);
6301 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6302 const char *name, struct tep_record *record,
6303 unsigned long long *val, int err)
6307 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6311 if (tep_read_number_field(field, record->data, val)) {
6313 trace_seq_printf(s, " %s=INVALID", name);
6321 * tep_get_field_raw - return the raw pointer into the data field
6322 * @s: The seq to print to on error
6323 * @event: the event that the field is for
6324 * @name: The name of the field
6325 * @record: The record with the field name.
6326 * @len: place to store the field length.
6327 * @err: print default error if failed.
6329 * Returns a pointer into record->data of the field and places
6330 * the length of the field in @len.
6332 * On failure, it returns NULL.
6334 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6335 const char *name, struct tep_record *record,
6338 struct tep_format_field *field;
6339 void *data = record->data;
6346 field = tep_find_field(event, name);
6350 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6354 /* Allow @len to be NULL */
6358 offset = field->offset;
6359 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
6360 offset = tep_read_number(event->tep,
6361 data + offset, field->size);
6362 *len = offset >> 16;
6367 return data + offset;
6371 * tep_get_field_val - find a field and return its value
6372 * @s: The seq to print to on error
6373 * @event: the event that the field is for
6374 * @name: The name of the field
6375 * @record: The record with the field name.
6376 * @val: place to store the value of the field.
6377 * @err: print default error if failed.
6379 * Returns 0 on success -1 on field not found.
6381 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
6382 const char *name, struct tep_record *record,
6383 unsigned long long *val, int err)
6385 struct tep_format_field *field;
6390 field = tep_find_field(event, name);
6392 return get_field_val(s, field, name, record, val, err);
6396 * tep_get_common_field_val - find a common field and return its value
6397 * @s: The seq to print to on error
6398 * @event: the event that the field is for
6399 * @name: The name of the field
6400 * @record: The record with the field name.
6401 * @val: place to store the value of the field.
6402 * @err: print default error if failed.
6404 * Returns 0 on success -1 on field not found.
6406 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
6407 const char *name, struct tep_record *record,
6408 unsigned long long *val, int err)
6410 struct tep_format_field *field;
6415 field = tep_find_common_field(event, name);
6417 return get_field_val(s, field, name, record, val, err);
6421 * tep_get_any_field_val - find a any field and return its value
6422 * @s: The seq to print to on error
6423 * @event: the event that the field is for
6424 * @name: The name of the field
6425 * @record: The record with the field name.
6426 * @val: place to store the value of the field.
6427 * @err: print default error if failed.
6429 * Returns 0 on success -1 on field not found.
6431 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
6432 const char *name, struct tep_record *record,
6433 unsigned long long *val, int err)
6435 struct tep_format_field *field;
6440 field = tep_find_any_field(event, name);
6442 return get_field_val(s, field, name, record, val, err);
6446 * tep_print_num_field - print a field and a format
6447 * @s: The seq to print to
6448 * @fmt: The printf format to print the field with.
6449 * @event: the event that the field is for
6450 * @name: The name of the field
6451 * @record: The record with the field name.
6452 * @err: print default error if failed.
6454 * Returns positive value on success, negative in case of an error,
6455 * or 0 if buffer is full.
6457 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6458 struct tep_event *event, const char *name,
6459 struct tep_record *record, int err)
6461 struct tep_format_field *field = tep_find_field(event, name);
6462 unsigned long long val;
6467 if (tep_read_number_field(field, record->data, &val))
6470 return trace_seq_printf(s, fmt, val);
6474 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6479 * tep_print_func_field - print a field and a format for function pointers
6480 * @s: The seq to print to
6481 * @fmt: The printf format to print the field with.
6482 * @event: the event that the field is for
6483 * @name: The name of the field
6484 * @record: The record with the field name.
6485 * @err: print default error if failed.
6487 * Returns positive value on success, negative in case of an error,
6488 * or 0 if buffer is full.
6490 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6491 struct tep_event *event, const char *name,
6492 struct tep_record *record, int err)
6494 struct tep_format_field *field = tep_find_field(event, name);
6495 struct tep_handle *tep = event->tep;
6496 unsigned long long val;
6497 struct func_map *func;
6503 if (tep_read_number_field(field, record->data, &val))
6506 func = find_func(tep, val);
6509 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6511 sprintf(tmp, "0x%08llx", val);
6513 return trace_seq_printf(s, fmt, tmp);
6517 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6521 static void free_func_handle(struct tep_function_handler *func)
6523 struct func_params *params;
6527 while (func->params) {
6528 params = func->params;
6529 func->params = params->next;
6537 * tep_register_print_function - register a helper function
6538 * @tep: a handle to the trace event parser context
6539 * @func: the function to process the helper function
6540 * @ret_type: the return type of the helper function
6541 * @name: the name of the helper function
6542 * @parameters: A list of enum tep_func_arg_type
6544 * Some events may have helper functions in the print format arguments.
6545 * This allows a plugin to dynamically create a way to process one
6546 * of these functions.
6548 * The @parameters is a variable list of tep_func_arg_type enums that
6549 * must end with TEP_FUNC_ARG_VOID.
6551 int tep_register_print_function(struct tep_handle *tep,
6552 tep_func_handler func,
6553 enum tep_func_arg_type ret_type,
6556 struct tep_function_handler *func_handle;
6557 struct func_params **next_param;
6558 struct func_params *param;
6559 enum tep_func_arg_type type;
6563 func_handle = find_func_handler(tep, name);
6566 * This is most like caused by the users own
6567 * plugins updating the function. This overrides the
6570 pr_stat("override of function helper '%s'", name);
6571 remove_func_handler(tep, name);
6574 func_handle = calloc(1, sizeof(*func_handle));
6576 do_warning("Failed to allocate function handler");
6577 return TEP_ERRNO__MEM_ALLOC_FAILED;
6580 func_handle->ret_type = ret_type;
6581 func_handle->name = strdup(name);
6582 func_handle->func = func;
6583 if (!func_handle->name) {
6584 do_warning("Failed to allocate function name");
6586 return TEP_ERRNO__MEM_ALLOC_FAILED;
6589 next_param = &(func_handle->params);
6592 type = va_arg(ap, enum tep_func_arg_type);
6593 if (type == TEP_FUNC_ARG_VOID)
6596 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6597 do_warning("Invalid argument type %d", type);
6598 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6602 param = malloc(sizeof(*param));
6604 do_warning("Failed to allocate function param");
6605 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6611 *next_param = param;
6612 next_param = &(param->next);
6614 func_handle->nr_args++;
6618 func_handle->next = tep->func_handlers;
6619 tep->func_handlers = func_handle;
6624 free_func_handle(func_handle);
6629 * tep_unregister_print_function - unregister a helper function
6630 * @tep: a handle to the trace event parser context
6631 * @func: the function to process the helper function
6632 * @name: the name of the helper function
6634 * This function removes existing print handler for function @name.
6636 * Returns 0 if the handler was removed successully, -1 otherwise.
6638 int tep_unregister_print_function(struct tep_handle *tep,
6639 tep_func_handler func, char *name)
6641 struct tep_function_handler *func_handle;
6643 func_handle = find_func_handler(tep, name);
6644 if (func_handle && func_handle->func == func) {
6645 remove_func_handler(tep, name);
6651 static struct tep_event *search_event(struct tep_handle *tep, int id,
6652 const char *sys_name,
6653 const char *event_name)
6655 struct tep_event *event;
6659 event = tep_find_event(tep, id);
6662 if (event_name && (strcmp(event_name, event->name) != 0))
6664 if (sys_name && (strcmp(sys_name, event->system) != 0))
6667 event = tep_find_event_by_name(tep, sys_name, event_name);
6675 * tep_register_event_handler - register a way to parse an event
6676 * @tep: a handle to the trace event parser context
6677 * @id: the id of the event to register
6678 * @sys_name: the system name the event belongs to
6679 * @event_name: the name of the event
6680 * @func: the function to call to parse the event information
6681 * @context: the data to be passed to @func
6683 * This function allows a developer to override the parsing of
6684 * a given event. If for some reason the default print format
6685 * is not sufficient, this function will register a function
6686 * for an event to be used to parse the data instead.
6688 * If @id is >= 0, then it is used to find the event.
6689 * else @sys_name and @event_name are used.
6692 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
6693 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
6694 * negative TEP_ERRNO_... in case of an error
6697 int tep_register_event_handler(struct tep_handle *tep, int id,
6698 const char *sys_name, const char *event_name,
6699 tep_event_handler_func func, void *context)
6701 struct tep_event *event;
6702 struct event_handler *handle;
6704 event = search_event(tep, id, sys_name, event_name);
6708 pr_stat("overriding event (%d) %s:%s with new print handler",
6709 event->id, event->system, event->name);
6711 event->handler = func;
6712 event->context = context;
6713 return TEP_REGISTER_SUCCESS_OVERWRITE;
6716 /* Save for later use. */
6717 handle = calloc(1, sizeof(*handle));
6719 do_warning("Failed to allocate event handler");
6720 return TEP_ERRNO__MEM_ALLOC_FAILED;
6725 handle->event_name = strdup(event_name);
6727 handle->sys_name = strdup(sys_name);
6729 if ((event_name && !handle->event_name) ||
6730 (sys_name && !handle->sys_name)) {
6731 do_warning("Failed to allocate event/sys name");
6732 free((void *)handle->event_name);
6733 free((void *)handle->sys_name);
6735 return TEP_ERRNO__MEM_ALLOC_FAILED;
6738 handle->func = func;
6739 handle->next = tep->handlers;
6740 tep->handlers = handle;
6741 handle->context = context;
6743 return TEP_REGISTER_SUCCESS;
6746 static int handle_matches(struct event_handler *handler, int id,
6747 const char *sys_name, const char *event_name,
6748 tep_event_handler_func func, void *context)
6750 if (id >= 0 && id != handler->id)
6753 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6756 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6759 if (func != handler->func || context != handler->context)
6766 * tep_unregister_event_handler - unregister an existing event handler
6767 * @tep: a handle to the trace event parser context
6768 * @id: the id of the event to unregister
6769 * @sys_name: the system name the handler belongs to
6770 * @event_name: the name of the event handler
6771 * @func: the function to call to parse the event information
6772 * @context: the data to be passed to @func
6774 * This function removes existing event handler (parser).
6776 * If @id is >= 0, then it is used to find the event.
6777 * else @sys_name and @event_name are used.
6779 * Returns 0 if handler was removed successfully, -1 if event was not found.
6781 int tep_unregister_event_handler(struct tep_handle *tep, int id,
6782 const char *sys_name, const char *event_name,
6783 tep_event_handler_func func, void *context)
6785 struct tep_event *event;
6786 struct event_handler *handle;
6787 struct event_handler **next;
6789 event = search_event(tep, id, sys_name, event_name);
6793 if (event->handler == func && event->context == context) {
6794 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6795 event->id, event->system, event->name);
6797 event->handler = NULL;
6798 event->context = NULL;
6803 for (next = &tep->handlers; *next; next = &(*next)->next) {
6805 if (handle_matches(handle, id, sys_name, event_name,
6813 *next = handle->next;
6814 free_handler(handle);
6820 * tep_alloc - create a tep handle
6822 struct tep_handle *tep_alloc(void)
6824 struct tep_handle *tep = calloc(1, sizeof(*tep));
6828 tep->host_bigendian = tep_is_bigendian();
6834 void tep_ref(struct tep_handle *tep)
6839 int tep_get_ref(struct tep_handle *tep)
6842 return tep->ref_count;
6846 void tep_free_format_field(struct tep_format_field *field)
6849 if (field->alias != field->name)
6855 static void free_format_fields(struct tep_format_field *field)
6857 struct tep_format_field *next;
6861 tep_free_format_field(field);
6866 static void free_formats(struct tep_format *format)
6868 free_format_fields(format->common_fields);
6869 free_format_fields(format->fields);
6872 void tep_free_event(struct tep_event *event)
6875 free(event->system);
6877 free_formats(&event->format);
6879 free(event->print_fmt.format);
6880 free_args(event->print_fmt.args);
6886 * tep_free - free a tep handle
6887 * @tep: the tep handle to free
6889 void tep_free(struct tep_handle *tep)
6891 struct cmdline_list *cmdlist, *cmdnext;
6892 struct func_list *funclist, *funcnext;
6893 struct printk_list *printklist, *printknext;
6894 struct tep_function_handler *func_handler;
6895 struct event_handler *handle;
6901 cmdlist = tep->cmdlist;
6902 funclist = tep->funclist;
6903 printklist = tep->printklist;
6909 if (tep->cmdlines) {
6910 for (i = 0; i < tep->cmdline_count; i++)
6911 free(tep->cmdlines[i].comm);
6912 free(tep->cmdlines);
6916 cmdnext = cmdlist->next;
6917 free(cmdlist->comm);
6922 if (tep->func_map) {
6923 for (i = 0; i < (int)tep->func_count; i++) {
6924 free(tep->func_map[i].func);
6925 free(tep->func_map[i].mod);
6927 free(tep->func_map);
6931 funcnext = funclist->next;
6932 free(funclist->func);
6933 free(funclist->mod);
6935 funclist = funcnext;
6938 while (tep->func_handlers) {
6939 func_handler = tep->func_handlers;
6940 tep->func_handlers = func_handler->next;
6941 free_func_handle(func_handler);
6944 if (tep->printk_map) {
6945 for (i = 0; i < (int)tep->printk_count; i++)
6946 free(tep->printk_map[i].printk);
6947 free(tep->printk_map);
6950 while (printklist) {
6951 printknext = printklist->next;
6952 free(printklist->printk);
6954 printklist = printknext;
6957 for (i = 0; i < tep->nr_events; i++)
6958 tep_free_event(tep->events[i]);
6960 while (tep->handlers) {
6961 handle = tep->handlers;
6962 tep->handlers = handle->next;
6963 free_handler(handle);
6966 free(tep->trace_clock);
6968 free(tep->sort_events);
6969 free(tep->func_resolver);
6974 void tep_unref(struct tep_handle *tep)