1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
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));
272 tep->cmdlines = cmdlines;
274 cmdlines[tep->cmdline_count].comm = strdup(comm);
275 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);
290 static int _tep_register_comm(struct tep_handle *tep,
291 const char *comm, int pid, bool override)
293 struct cmdline_list *item;
296 return add_new_comm(tep, comm, pid, override);
298 item = malloc(sizeof(*item));
303 item->comm = strdup(comm);
305 item->comm = strdup("<...>");
311 item->next = tep->cmdlist;
314 tep->cmdline_count++;
320 * tep_register_comm - register a pid / comm mapping
321 * @tep: a handle to the trace event parser context
322 * @comm: the command line to register
323 * @pid: the pid to map the command line to
325 * This adds a mapping to search for command line names with
326 * a given pid. The comm is duplicated. If a command with the same pid
327 * already exist, -1 is returned and errno is set to EEXIST
329 int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
331 return _tep_register_comm(tep, comm, pid, false);
335 * tep_override_comm - register a pid / comm mapping
336 * @tep: a handle to the trace event parser context
337 * @comm: the command line to register
338 * @pid: the pid to map the command line to
340 * This adds a mapping to search for command line names with
341 * a given pid. The comm is duplicated. If a command with the same pid
342 * already exist, the command string is udapted with the new one
344 int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
346 if (!tep->cmdlines && cmdline_init(tep)) {
350 return _tep_register_comm(tep, comm, pid, true);
353 int tep_register_trace_clock(struct tep_handle *tep, const char *trace_clock)
355 tep->trace_clock = strdup(trace_clock);
356 if (!tep->trace_clock) {
364 unsigned long long addr;
370 struct func_list *next;
371 unsigned long long addr;
376 static int func_cmp(const void *a, const void *b)
378 const struct func_map *fa = a;
379 const struct func_map *fb = b;
381 if (fa->addr < fb->addr)
383 if (fa->addr > fb->addr)
390 * We are searching for a record in between, not an exact
393 static int func_bcmp(const void *a, const void *b)
395 const struct func_map *fa = a;
396 const struct func_map *fb = b;
398 if ((fa->addr == fb->addr) ||
400 (fa->addr > fb->addr &&
401 fa->addr < (fb+1)->addr))
404 if (fa->addr < fb->addr)
410 static int func_map_init(struct tep_handle *tep)
412 struct func_list *funclist;
413 struct func_list *item;
414 struct func_map *func_map;
417 func_map = malloc(sizeof(*func_map) * (tep->func_count + 1));
421 funclist = tep->funclist;
425 func_map[i].func = funclist->func;
426 func_map[i].addr = funclist->addr;
427 func_map[i].mod = funclist->mod;
430 funclist = funclist->next;
434 qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp);
437 * Add a special record at the end.
439 func_map[tep->func_count].func = NULL;
440 func_map[tep->func_count].addr = 0;
441 func_map[tep->func_count].mod = NULL;
443 tep->func_map = func_map;
444 tep->funclist = NULL;
449 static struct func_map *
450 __find_func(struct tep_handle *tep, unsigned long long addr)
452 struct func_map *func;
460 func = bsearch(&key, tep->func_map, tep->func_count,
461 sizeof(*tep->func_map), func_bcmp);
466 struct func_resolver {
467 tep_func_resolver_t *func;
473 * tep_set_function_resolver - set an alternative function resolver
474 * @tep: a handle to the trace event parser context
475 * @resolver: function to be used
476 * @priv: resolver function private state.
478 * Some tools may have already a way to resolve kernel functions, allow them to
479 * keep using it instead of duplicating all the entries inside tep->funclist.
481 int tep_set_function_resolver(struct tep_handle *tep,
482 tep_func_resolver_t *func, void *priv)
484 struct func_resolver *resolver = malloc(sizeof(*resolver));
486 if (resolver == NULL)
489 resolver->func = func;
490 resolver->priv = priv;
492 free(tep->func_resolver);
493 tep->func_resolver = resolver;
499 * tep_reset_function_resolver - reset alternative function resolver
500 * @tep: a handle to the trace event parser context
502 * Stop using whatever alternative resolver was set, use the default
505 void tep_reset_function_resolver(struct tep_handle *tep)
507 free(tep->func_resolver);
508 tep->func_resolver = NULL;
511 static struct func_map *
512 find_func(struct tep_handle *tep, unsigned long long addr)
514 struct func_map *map;
516 if (!tep->func_resolver)
517 return __find_func(tep, addr);
519 map = &tep->func_resolver->map;
522 map->func = tep->func_resolver->func(tep->func_resolver->priv,
523 &map->addr, &map->mod);
524 if (map->func == NULL)
531 * tep_find_function - find a function by a given address
532 * @tep: a handle to the trace event parser context
533 * @addr: the address to find the function with
535 * Returns a pointer to the function stored that has the given
536 * address. Note, the address does not have to be exact, it
537 * will select the function that would contain the address.
539 const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
541 struct func_map *map;
543 map = find_func(tep, addr);
551 * tep_find_function_address - find a function address by a given address
552 * @tep: a handle to the trace event parser context
553 * @addr: the address to find the function with
555 * Returns the address the function starts at. This can be used in
556 * conjunction with tep_find_function to print both the function
557 * name and the function offset.
560 tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
562 struct func_map *map;
564 map = find_func(tep, addr);
572 * tep_register_function - register a function with a given address
573 * @tep: a handle to the trace event parser context
574 * @function: the function name to register
575 * @addr: the address the function starts at
576 * @mod: the kernel module the function may be in (NULL for none)
578 * This registers a function name with an address and module.
579 * The @func passed in is duplicated.
581 int tep_register_function(struct tep_handle *tep, char *func,
582 unsigned long long addr, char *mod)
584 struct func_list *item = malloc(sizeof(*item));
589 item->next = tep->funclist;
590 item->func = strdup(func);
595 item->mod = strdup(mod);
602 tep->funclist = item;
617 * tep_print_funcs - print out the stored functions
618 * @tep: a handle to the trace event parser context
620 * This prints out the stored functions.
622 void tep_print_funcs(struct tep_handle *tep)
629 for (i = 0; i < (int)tep->func_count; i++) {
631 tep->func_map[i].addr,
632 tep->func_map[i].func);
633 if (tep->func_map[i].mod)
634 printf(" [%s]\n", tep->func_map[i].mod);
641 unsigned long long addr;
646 struct printk_list *next;
647 unsigned long long addr;
651 static int printk_cmp(const void *a, const void *b)
653 const struct printk_map *pa = a;
654 const struct printk_map *pb = b;
656 if (pa->addr < pb->addr)
658 if (pa->addr > pb->addr)
664 static int printk_map_init(struct tep_handle *tep)
666 struct printk_list *printklist;
667 struct printk_list *item;
668 struct printk_map *printk_map;
671 printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1));
675 printklist = tep->printklist;
679 printk_map[i].printk = printklist->printk;
680 printk_map[i].addr = printklist->addr;
683 printklist = printklist->next;
687 qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp);
689 tep->printk_map = printk_map;
690 tep->printklist = NULL;
695 static struct printk_map *
696 find_printk(struct tep_handle *tep, unsigned long long addr)
698 struct printk_map *printk;
699 struct printk_map key;
701 if (!tep->printk_map && printk_map_init(tep))
706 printk = bsearch(&key, tep->printk_map, tep->printk_count,
707 sizeof(*tep->printk_map), printk_cmp);
713 * tep_register_print_string - register a string by its address
714 * @tep: a handle to the trace event parser context
715 * @fmt: the string format to register
716 * @addr: the address the string was located at
718 * This registers a string by the address it was stored in the kernel.
719 * The @fmt passed in is duplicated.
721 int tep_register_print_string(struct tep_handle *tep, const char *fmt,
722 unsigned long long addr)
724 struct printk_list *item = malloc(sizeof(*item));
730 item->next = tep->printklist;
733 /* Strip off quotes and '\n' from the end */
736 item->printk = strdup(fmt);
740 p = item->printk + strlen(item->printk) - 1;
745 if (strcmp(p, "\\n") == 0)
748 tep->printklist = item;
760 * tep_print_printk - print out the stored strings
761 * @tep: a handle to the trace event parser context
763 * This prints the string formats that were stored.
765 void tep_print_printk(struct tep_handle *tep)
769 if (!tep->printk_map)
770 printk_map_init(tep);
772 for (i = 0; i < (int)tep->printk_count; i++) {
773 printf("%016llx %s\n",
774 tep->printk_map[i].addr,
775 tep->printk_map[i].printk);
779 static struct tep_event *alloc_event(void)
781 return calloc(1, sizeof(struct tep_event));
784 static int add_event(struct tep_handle *tep, struct tep_event *event)
787 struct tep_event **events = realloc(tep->events, sizeof(event) *
788 (tep->nr_events + 1));
792 tep->events = events;
794 for (i = 0; i < tep->nr_events; i++) {
795 if (tep->events[i]->id > event->id)
798 if (i < tep->nr_events)
799 memmove(&tep->events[i + 1],
801 sizeof(event) * (tep->nr_events - i));
803 tep->events[i] = event;
811 static int event_item_type(enum tep_event_type type)
814 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
816 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
822 static void free_flag_sym(struct tep_print_flag_sym *fsym)
824 struct tep_print_flag_sym *next;
835 static void free_arg(struct tep_print_arg *arg)
837 struct tep_print_arg *farg;
844 free(arg->atom.atom);
846 case TEP_PRINT_FIELD:
847 free(arg->field.name);
849 case TEP_PRINT_FLAGS:
850 free_arg(arg->flags.field);
851 free(arg->flags.delim);
852 free_flag_sym(arg->flags.flags);
854 case TEP_PRINT_SYMBOL:
855 free_arg(arg->symbol.field);
856 free_flag_sym(arg->symbol.symbols);
859 case TEP_PRINT_HEX_STR:
860 free_arg(arg->hex.field);
861 free_arg(arg->hex.size);
863 case TEP_PRINT_INT_ARRAY:
864 free_arg(arg->int_array.field);
865 free_arg(arg->int_array.count);
866 free_arg(arg->int_array.el_size);
869 free(arg->typecast.type);
870 free_arg(arg->typecast.item);
872 case TEP_PRINT_STRING:
873 case TEP_PRINT_BSTRING:
874 free(arg->string.string);
876 case TEP_PRINT_BITMASK:
877 free(arg->bitmask.bitmask);
879 case TEP_PRINT_DYNAMIC_ARRAY:
880 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
881 free(arg->dynarray.index);
885 free_arg(arg->op.left);
886 free_arg(arg->op.right);
889 while (arg->func.args) {
890 farg = arg->func.args;
891 arg->func.args = farg->next;
904 static enum tep_event_type get_type(int ch)
907 return TEP_EVENT_NEWLINE;
909 return TEP_EVENT_SPACE;
910 if (isalnum(ch) || ch == '_')
911 return TEP_EVENT_ITEM;
913 return TEP_EVENT_SQUOTE;
915 return TEP_EVENT_DQUOTE;
917 return TEP_EVENT_NONE;
918 if (ch == '(' || ch == ')' || ch == ',')
919 return TEP_EVENT_DELIM;
924 static int __read_char(void)
926 if (input_buf_ptr >= input_buf_siz)
929 return input_buf[input_buf_ptr++];
932 static int __peek_char(void)
934 if (input_buf_ptr >= input_buf_siz)
937 return input_buf[input_buf_ptr];
941 * tep_peek_char - peek at the next character that will be read
943 * Returns the next character read, or -1 if end of buffer.
945 int tep_peek_char(void)
947 return __peek_char();
950 static int extend_token(char **tok, char *buf, int size)
952 char *newtok = realloc(*tok, size);
969 static enum tep_event_type force_token(const char *str, char **tok);
971 static enum tep_event_type __read_token(char **tok)
974 int ch, last_ch, quote_ch, next_ch;
977 enum tep_event_type type;
984 return TEP_EVENT_NONE;
987 if (type == TEP_EVENT_NONE)
993 case TEP_EVENT_NEWLINE:
994 case TEP_EVENT_DELIM:
995 if (asprintf(tok, "%c", ch) < 0)
996 return TEP_EVENT_ERROR;
1003 next_ch = __peek_char();
1004 if (next_ch == '>') {
1005 buf[i++] = __read_char();
1018 buf[i++] = __read_char();
1030 default: /* what should we do instead? */
1040 buf[i++] = __read_char();
1043 case TEP_EVENT_DQUOTE:
1044 case TEP_EVENT_SQUOTE:
1045 /* don't keep quotes */
1051 if (i == (BUFSIZ - 1)) {
1055 if (extend_token(tok, buf, tok_size) < 0)
1056 return TEP_EVENT_NONE;
1062 /* the '\' '\' will cancel itself */
1063 if (ch == '\\' && last_ch == '\\')
1065 } while (ch != quote_ch || last_ch == '\\');
1066 /* remove the last quote */
1070 * For strings (double quotes) check the next token.
1071 * If it is another string, concatinate the two.
1073 if (type == TEP_EVENT_DQUOTE) {
1074 unsigned long long save_input_buf_ptr = input_buf_ptr;
1078 } while (isspace(ch));
1081 input_buf_ptr = save_input_buf_ptr;
1086 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1087 case TEP_EVENT_ITEM:
1092 while (get_type(__peek_char()) == type) {
1093 if (i == (BUFSIZ - 1)) {
1097 if (extend_token(tok, buf, tok_size) < 0)
1098 return TEP_EVENT_NONE;
1107 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1108 return TEP_EVENT_NONE;
1110 if (type == TEP_EVENT_ITEM) {
1112 * Older versions of the kernel has a bug that
1113 * creates invalid symbols and will break the mac80211
1114 * parsing. This is a work around to that bug.
1116 * See Linux kernel commit:
1117 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1119 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1122 return force_token("\"%s\" ", tok);
1123 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1126 return force_token("\" sta:%pM\" ", tok);
1127 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1130 return force_token("\" vif:%p(%d)\" ", tok);
1137 static enum tep_event_type force_token(const char *str, char **tok)
1139 const char *save_input_buf;
1140 unsigned long long save_input_buf_ptr;
1141 unsigned long long save_input_buf_siz;
1142 enum tep_event_type type;
1144 /* save off the current input pointers */
1145 save_input_buf = input_buf;
1146 save_input_buf_ptr = input_buf_ptr;
1147 save_input_buf_siz = input_buf_siz;
1149 init_input_buf(str, strlen(str));
1151 type = __read_token(tok);
1153 /* reset back to original token */
1154 input_buf = save_input_buf;
1155 input_buf_ptr = save_input_buf_ptr;
1156 input_buf_siz = save_input_buf_siz;
1161 static void free_token(char *tok)
1167 static enum tep_event_type read_token(char **tok)
1169 enum tep_event_type type;
1172 type = __read_token(tok);
1173 if (type != TEP_EVENT_SPACE)
1181 return TEP_EVENT_NONE;
1185 * tep_read_token - access to utilities to use the tep parser
1186 * @tok: The token to return
1188 * This will parse tokens from the string given by
1191 * Returns the token type.
1193 enum tep_event_type tep_read_token(char **tok)
1195 return read_token(tok);
1199 * tep_free_token - free a token returned by tep_read_token
1200 * @token: the token to free
1202 void tep_free_token(char *token)
1208 static enum tep_event_type read_token_item(char **tok)
1210 enum tep_event_type type;
1213 type = __read_token(tok);
1214 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1222 return TEP_EVENT_NONE;
1225 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1227 if (type != expect) {
1228 do_warning("Error: expected type %d but read %d",
1235 static int test_type_token(enum tep_event_type type, const char *token,
1236 enum tep_event_type expect, const char *expect_tok)
1238 if (type != expect) {
1239 do_warning("Error: expected type %d but read %d",
1244 if (strcmp(token, expect_tok) != 0) {
1245 do_warning("Error: expected '%s' but read '%s'",
1252 static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
1254 enum tep_event_type type;
1257 type = read_token(tok);
1259 type = read_token_item(tok);
1260 return test_type(type, expect);
1263 static int read_expect_type(enum tep_event_type expect, char **tok)
1265 return __read_expect_type(expect, tok, 1);
1268 static int __read_expected(enum tep_event_type expect, const char *str,
1271 enum tep_event_type type;
1276 type = read_token(&token);
1278 type = read_token_item(&token);
1280 ret = test_type_token(type, token, expect, str);
1287 static int read_expected(enum tep_event_type expect, const char *str)
1289 return __read_expected(expect, str, 1);
1292 static int read_expected_item(enum tep_event_type expect, const char *str)
1294 return __read_expected(expect, str, 0);
1297 static char *event_read_name(void)
1301 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
1304 if (read_expected(TEP_EVENT_OP, ":") < 0)
1307 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1317 static int event_read_id(void)
1322 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
1325 if (read_expected(TEP_EVENT_OP, ":") < 0)
1328 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1331 id = strtoul(token, NULL, 0);
1340 static int field_is_string(struct tep_format_field *field)
1342 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1343 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1344 strstr(field->type, "s8")))
1350 static int field_is_dynamic(struct tep_format_field *field)
1352 if (strncmp(field->type, "__data_loc", 10) == 0)
1358 static int field_is_long(struct tep_format_field *field)
1360 /* includes long long */
1361 if (strstr(field->type, "long"))
1367 static unsigned int type_size(const char *name)
1369 /* This covers all TEP_FIELD_IS_STRING types. */
1387 for (i = 0; table[i].type; i++) {
1388 if (!strcmp(table[i].type, name))
1389 return table[i].size;
1395 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1397 struct tep_format_field *field = NULL;
1398 enum tep_event_type type;
1404 unsigned int size_dynamic = 0;
1406 type = read_token(&token);
1407 if (type == TEP_EVENT_NEWLINE) {
1414 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1418 type = read_token(&token);
1420 * The ftrace fields may still use the "special" name.
1423 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1424 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1426 type = read_token(&token);
1429 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1433 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1438 field = calloc(1, sizeof(*field));
1442 field->event = event;
1444 /* read the rest of the type */
1446 type = read_token(&token);
1447 if (type == TEP_EVENT_ITEM ||
1448 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1450 * Some of the ftrace fields are broken and have
1451 * an illegal "." in them.
1453 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1454 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1456 if (strcmp(token, "*") == 0)
1457 field->flags |= TEP_FIELD_IS_POINTER;
1461 new_type = realloc(field->type,
1462 strlen(field->type) +
1463 strlen(last_token) + 2);
1468 field->type = new_type;
1469 strcat(field->type, " ");
1470 strcat(field->type, last_token);
1473 field->type = last_token;
1482 do_warning_event(event, "%s: no type found", __func__);
1485 field->name = field->alias = last_token;
1487 if (test_type(type, TEP_EVENT_OP))
1490 if (strcmp(token, "[") == 0) {
1491 enum tep_event_type last_type = type;
1492 char *brackets = token;
1496 field->flags |= TEP_FIELD_IS_ARRAY;
1498 type = read_token(&token);
1500 if (type == TEP_EVENT_ITEM)
1501 field->arraylen = strtoul(token, NULL, 0);
1503 field->arraylen = 0;
1505 while (strcmp(token, "]") != 0) {
1506 if (last_type == TEP_EVENT_ITEM &&
1507 type == TEP_EVENT_ITEM)
1513 new_brackets = realloc(brackets,
1515 strlen(token) + len);
1516 if (!new_brackets) {
1520 brackets = new_brackets;
1522 strcat(brackets, " ");
1523 strcat(brackets, token);
1524 /* We only care about the last token */
1525 field->arraylen = strtoul(token, NULL, 0);
1527 type = read_token(&token);
1528 if (type == TEP_EVENT_NONE) {
1529 do_warning_event(event, "failed to find token");
1536 new_brackets = realloc(brackets, strlen(brackets) + 2);
1537 if (!new_brackets) {
1541 brackets = new_brackets;
1542 strcat(brackets, "]");
1544 /* add brackets to type */
1546 type = read_token(&token);
1548 * If the next token is not an OP, then it is of
1549 * the format: type [] item;
1551 if (type == TEP_EVENT_ITEM) {
1553 new_type = realloc(field->type,
1554 strlen(field->type) +
1555 strlen(field->name) +
1556 strlen(brackets) + 2);
1561 field->type = new_type;
1562 strcat(field->type, " ");
1563 strcat(field->type, field->name);
1564 size_dynamic = type_size(field->name);
1565 free_token(field->name);
1566 strcat(field->type, brackets);
1567 field->name = field->alias = token;
1568 type = read_token(&token);
1571 new_type = realloc(field->type,
1572 strlen(field->type) +
1573 strlen(brackets) + 1);
1578 field->type = new_type;
1579 strcat(field->type, brackets);
1584 if (field_is_string(field))
1585 field->flags |= TEP_FIELD_IS_STRING;
1586 if (field_is_dynamic(field))
1587 field->flags |= TEP_FIELD_IS_DYNAMIC;
1588 if (field_is_long(field))
1589 field->flags |= TEP_FIELD_IS_LONG;
1591 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1595 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1598 if (read_expected(TEP_EVENT_OP, ":") < 0)
1601 if (read_expect_type(TEP_EVENT_ITEM, &token))
1603 field->offset = strtoul(token, NULL, 0);
1606 if (read_expected(TEP_EVENT_OP, ";") < 0)
1609 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1612 if (read_expected(TEP_EVENT_OP, ":") < 0)
1615 if (read_expect_type(TEP_EVENT_ITEM, &token))
1617 field->size = strtoul(token, NULL, 0);
1620 if (read_expected(TEP_EVENT_OP, ";") < 0)
1623 type = read_token(&token);
1624 if (type != TEP_EVENT_NEWLINE) {
1625 /* newer versions of the kernel have a "signed" type */
1626 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1631 if (read_expected(TEP_EVENT_OP, ":") < 0)
1634 if (read_expect_type(TEP_EVENT_ITEM, &token))
1637 if (strtoul(token, NULL, 0))
1638 field->flags |= TEP_FIELD_IS_SIGNED;
1641 if (read_expected(TEP_EVENT_OP, ";") < 0)
1644 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1650 if (field->flags & TEP_FIELD_IS_ARRAY) {
1651 if (field->arraylen)
1652 field->elementsize = field->size / field->arraylen;
1653 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1654 field->elementsize = size_dynamic;
1655 else if (field->flags & TEP_FIELD_IS_STRING)
1656 field->elementsize = 1;
1657 else if (field->flags & TEP_FIELD_IS_LONG)
1658 field->elementsize = event->tep ?
1659 event->tep->long_size :
1662 field->elementsize = field->size;
1665 fields = &field->next;
1682 static int event_read_format(struct tep_event *event)
1687 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1690 if (read_expected(TEP_EVENT_OP, ":") < 0)
1693 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1697 ret = event_read_fields(event, &event->format.common_fields);
1700 event->format.nr_common = ret;
1702 ret = event_read_fields(event, &event->format.fields);
1705 event->format.nr_fields = ret;
1714 static enum tep_event_type
1715 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1716 char **tok, enum tep_event_type type);
1718 static enum tep_event_type
1719 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1721 enum tep_event_type type;
1724 type = read_token(&token);
1727 return process_arg_token(event, arg, tok, type);
1730 static enum tep_event_type
1731 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1734 * For __print_symbolic() and __print_flags, we need to completely
1735 * evaluate the first argument, which defines what to print next.
1737 static enum tep_event_type
1738 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1740 enum tep_event_type type;
1742 type = process_arg(event, arg, tok);
1744 while (type == TEP_EVENT_OP) {
1745 type = process_op(event, arg, tok);
1751 static enum tep_event_type
1752 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1754 struct tep_print_arg *arg, *left, *right;
1755 enum tep_event_type type;
1760 right = alloc_arg();
1762 if (!arg || !left || !right) {
1763 do_warning_event(event, "%s: not enough memory!", __func__);
1764 /* arg will be freed at out_free */
1770 arg->type = TEP_PRINT_OP;
1771 arg->op.left = left;
1772 arg->op.right = right;
1775 type = process_arg(event, left, &token);
1778 if (type == TEP_EVENT_ERROR)
1781 /* Handle other operations in the arguments */
1782 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1783 type = process_op(event, left, &token);
1787 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1792 type = process_arg(event, right, &token);
1794 top->op.right = arg;
1800 /* Top may point to itself */
1801 top->op.right = NULL;
1804 return TEP_EVENT_ERROR;
1807 static enum tep_event_type
1808 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1810 struct tep_print_arg *arg;
1811 enum tep_event_type type;
1816 do_warning_event(event, "%s: not enough memory!", __func__);
1817 /* '*tok' is set to top->op.op. No need to free. */
1819 return TEP_EVENT_ERROR;
1823 type = process_arg(event, arg, &token);
1824 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1827 top->op.right = arg;
1830 type = read_token_item(&token);
1838 return TEP_EVENT_ERROR;
1841 static int get_op_prio(char *op)
1855 /* '>>' and '<<' are 8 */
1859 /* '==' and '!=' are 10 */
1869 do_warning("unknown op '%c'", op[0]);
1873 if (strcmp(op, "++") == 0 ||
1874 strcmp(op, "--") == 0) {
1876 } else if (strcmp(op, ">>") == 0 ||
1877 strcmp(op, "<<") == 0) {
1879 } else if (strcmp(op, ">=") == 0 ||
1880 strcmp(op, "<=") == 0) {
1882 } else if (strcmp(op, "==") == 0 ||
1883 strcmp(op, "!=") == 0) {
1885 } else if (strcmp(op, "&&") == 0) {
1887 } else if (strcmp(op, "||") == 0) {
1890 do_warning("unknown op '%s'", op);
1896 static int set_op_prio(struct tep_print_arg *arg)
1899 /* single ops are the greatest */
1900 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1903 arg->op.prio = get_op_prio(arg->op.op);
1905 return arg->op.prio;
1908 /* Note, *tok does not get freed, but will most likely be saved */
1909 static enum tep_event_type
1910 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1912 struct tep_print_arg *left, *right = NULL;
1913 enum tep_event_type type;
1916 /* the op is passed in via tok */
1919 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1920 /* handle single op */
1922 do_warning_event(event, "bad op token %s", token);
1932 do_warning_event(event, "bad op token %s", token);
1937 /* make an empty left */
1942 left->type = TEP_PRINT_NULL;
1943 arg->op.left = left;
1945 right = alloc_arg();
1949 arg->op.right = right;
1951 /* do not free the token, it belongs to an op */
1953 type = process_arg(event, right, tok);
1955 } else if (strcmp(token, "?") == 0) {
1961 /* copy the top arg to the left */
1964 arg->type = TEP_PRINT_OP;
1966 arg->op.left = left;
1969 /* it will set arg->op.right */
1970 type = process_cond(event, arg, tok);
1972 } else if (strcmp(token, ">>") == 0 ||
1973 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) {
1995 /* copy the top arg to the left */
1998 arg->type = TEP_PRINT_OP;
2000 arg->op.left = left;
2001 arg->op.right = NULL;
2003 if (set_op_prio(arg) == -1) {
2004 event->flags |= TEP_EVENT_FL_FAILED;
2005 /* arg->op.op (= token) will be freed at out_free */
2010 type = read_token_item(&token);
2013 /* could just be a type pointer */
2014 if ((strcmp(arg->op.op, "*") == 0) &&
2015 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2018 if (left->type != TEP_PRINT_ATOM) {
2019 do_warning_event(event, "bad pointer type");
2022 new_atom = realloc(left->atom.atom,
2023 strlen(left->atom.atom) + 3);
2027 left->atom.atom = new_atom;
2028 strcat(left->atom.atom, " *");
2036 right = alloc_arg();
2040 type = process_arg_token(event, right, tok, type);
2041 if (type == TEP_EVENT_ERROR) {
2043 /* token was freed in process_arg_token() via *tok */
2048 if (right->type == TEP_PRINT_OP &&
2049 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2050 struct tep_print_arg tmp;
2052 /* rotate ops according to the priority */
2053 arg->op.right = right->op.left;
2059 arg->op.left = right;
2061 arg->op.right = right;
2064 } else if (strcmp(token, "[") == 0) {
2072 arg->type = TEP_PRINT_OP;
2074 arg->op.left = left;
2078 /* it will set arg->op.right */
2079 type = process_array(event, arg, tok);
2082 do_warning_event(event, "unknown op '%s'", token);
2083 event->flags |= TEP_EVENT_FL_FAILED;
2084 /* the arg is now the left side */
2088 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2091 /* higher prios need to be closer to the root */
2092 prio = get_op_prio(*tok);
2094 if (prio > arg->op.prio)
2095 return process_op(event, arg, tok);
2097 return process_op(event, right, tok);
2103 do_warning_event(event, "%s: not enough memory!", __func__);
2107 return TEP_EVENT_ERROR;
2110 static enum tep_event_type
2111 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2114 enum tep_event_type type;
2118 if (read_expected(TEP_EVENT_OP, "->") < 0)
2121 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2125 arg->type = TEP_PRINT_FIELD;
2126 arg->field.name = field;
2128 if (is_flag_field) {
2129 arg->field.field = tep_find_any_field(event, arg->field.name);
2130 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2132 } else if (is_symbolic_field) {
2133 arg->field.field = tep_find_any_field(event, arg->field.name);
2134 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2135 is_symbolic_field = 0;
2138 type = read_token(&token);
2147 return TEP_EVENT_ERROR;
2150 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2151 struct tep_print_arg **print_arg)
2153 struct tep_print_arg *field;
2154 enum tep_event_type type;
2158 field = alloc_arg();
2160 do_warning_event(event, "%s: not enough memory!", __func__);
2165 type = process_arg(event, field, &token);
2167 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2171 goto out_free_token;
2182 static char *arg_eval (struct tep_print_arg *arg);
2184 static unsigned long long
2185 eval_type_str(unsigned long long val, const char *type, int pointer)
2195 if (type[len-1] != '*') {
2196 do_warning("pointer expected with non pointer type");
2202 do_warning("%s: not enough memory!", __func__);
2205 memcpy(ref, type, len);
2207 /* chop off the " *" */
2210 val = eval_type_str(val, ref, 0);
2215 /* check if this is a pointer */
2216 if (type[len - 1] == '*')
2219 /* Try to figure out the arg size*/
2220 if (strncmp(type, "struct", 6) == 0)
2224 if (strcmp(type, "u8") == 0)
2227 if (strcmp(type, "u16") == 0)
2228 return val & 0xffff;
2230 if (strcmp(type, "u32") == 0)
2231 return val & 0xffffffff;
2233 if (strcmp(type, "u64") == 0 ||
2234 strcmp(type, "s64") == 0)
2237 if (strcmp(type, "s8") == 0)
2238 return (unsigned long long)(char)val & 0xff;
2240 if (strcmp(type, "s16") == 0)
2241 return (unsigned long long)(short)val & 0xffff;
2243 if (strcmp(type, "s32") == 0)
2244 return (unsigned long long)(int)val & 0xffffffff;
2246 if (strncmp(type, "unsigned ", 9) == 0) {
2251 if (strcmp(type, "char") == 0) {
2253 return (unsigned long long)(char)val & 0xff;
2258 if (strcmp(type, "short") == 0) {
2260 return (unsigned long long)(short)val & 0xffff;
2262 return val & 0xffff;
2265 if (strcmp(type, "int") == 0) {
2267 return (unsigned long long)(int)val & 0xffffffff;
2269 return val & 0xffffffff;
2276 * Try to figure out the type.
2278 static unsigned long long
2279 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2281 if (arg->type != TEP_PRINT_TYPE) {
2282 do_warning("expected type argument");
2286 return eval_type_str(val, arg->typecast.type, pointer);
2289 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2291 long long left, right;
2294 switch (arg->type) {
2295 case TEP_PRINT_ATOM:
2296 *val = strtoll(arg->atom.atom, NULL, 0);
2298 case TEP_PRINT_TYPE:
2299 ret = arg_num_eval(arg->typecast.item, val);
2302 *val = eval_type(*val, arg, 0);
2305 switch (arg->op.op[0]) {
2307 ret = arg_num_eval(arg->op.left, &left);
2310 ret = arg_num_eval(arg->op.right, &right);
2314 *val = left || right;
2316 *val = left | right;
2319 ret = arg_num_eval(arg->op.left, &left);
2322 ret = arg_num_eval(arg->op.right, &right);
2326 *val = left && right;
2328 *val = left & right;
2331 ret = arg_num_eval(arg->op.left, &left);
2334 ret = arg_num_eval(arg->op.right, &right);
2337 switch (arg->op.op[1]) {
2339 *val = left < right;
2342 *val = left << right;
2345 *val = left <= right;
2348 do_warning("unknown op '%s'", arg->op.op);
2353 ret = arg_num_eval(arg->op.left, &left);
2356 ret = arg_num_eval(arg->op.right, &right);
2359 switch (arg->op.op[1]) {
2361 *val = left > right;
2364 *val = left >> right;
2367 *val = left >= right;
2370 do_warning("unknown op '%s'", arg->op.op);
2375 ret = arg_num_eval(arg->op.left, &left);
2378 ret = arg_num_eval(arg->op.right, &right);
2382 if (arg->op.op[1] != '=') {
2383 do_warning("unknown op '%s'", arg->op.op);
2386 *val = left == right;
2389 ret = arg_num_eval(arg->op.left, &left);
2392 ret = arg_num_eval(arg->op.right, &right);
2396 switch (arg->op.op[1]) {
2398 *val = left != right;
2401 do_warning("unknown op '%s'", arg->op.op);
2406 /* check for negative */
2407 if (arg->op.left->type == TEP_PRINT_NULL)
2410 ret = arg_num_eval(arg->op.left, &left);
2413 ret = arg_num_eval(arg->op.right, &right);
2416 *val = left - right;
2419 if (arg->op.left->type == TEP_PRINT_NULL)
2422 ret = arg_num_eval(arg->op.left, &left);
2425 ret = arg_num_eval(arg->op.right, &right);
2428 *val = left + right;
2431 ret = arg_num_eval(arg->op.right, &right);
2437 do_warning("unknown op '%s'", arg->op.op);
2442 case TEP_PRINT_NULL:
2443 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2444 case TEP_PRINT_STRING:
2445 case TEP_PRINT_BSTRING:
2446 case TEP_PRINT_BITMASK:
2448 do_warning("invalid eval type %d", arg->type);
2455 static char *arg_eval (struct tep_print_arg *arg)
2458 static char buf[24];
2460 switch (arg->type) {
2461 case TEP_PRINT_ATOM:
2462 return arg->atom.atom;
2463 case TEP_PRINT_TYPE:
2464 return arg_eval(arg->typecast.item);
2466 if (!arg_num_eval(arg, &val))
2468 sprintf(buf, "%lld", val);
2471 case TEP_PRINT_NULL:
2472 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2473 case TEP_PRINT_STRING:
2474 case TEP_PRINT_BSTRING:
2475 case TEP_PRINT_BITMASK:
2477 do_warning("invalid eval type %d", arg->type);
2484 static enum tep_event_type
2485 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2487 enum tep_event_type type;
2488 struct tep_print_arg *arg = NULL;
2489 struct tep_print_flag_sym *field;
2495 type = read_token_item(&token);
2496 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2504 type = process_arg(event, arg, &token);
2506 if (type == TEP_EVENT_OP)
2507 type = process_op(event, arg, &token);
2509 if (type == TEP_EVENT_ERROR)
2512 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2515 field = calloc(1, sizeof(*field));
2519 value = arg_eval(arg);
2521 goto out_free_field;
2522 field->value = strdup(value);
2523 if (field->value == NULL)
2524 goto out_free_field;
2532 type = process_arg(event, arg, &token);
2533 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2534 goto out_free_field;
2536 value = arg_eval(arg);
2538 goto out_free_field;
2539 field->str = strdup(value);
2540 if (field->str == NULL)
2541 goto out_free_field;
2546 list = &field->next;
2549 type = read_token_item(&token);
2550 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2556 free_flag_sym(field);
2562 return TEP_EVENT_ERROR;
2565 static enum tep_event_type
2566 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2568 struct tep_print_arg *field;
2569 enum tep_event_type type;
2572 memset(arg, 0, sizeof(*arg));
2573 arg->type = TEP_PRINT_FLAGS;
2575 field = alloc_arg();
2577 do_warning_event(event, "%s: not enough memory!", __func__);
2581 type = process_field_arg(event, field, &token);
2583 /* Handle operations in the first argument */
2584 while (type == TEP_EVENT_OP)
2585 type = process_op(event, field, &token);
2587 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2588 goto out_free_field;
2591 arg->flags.field = field;
2593 type = read_token_item(&token);
2594 if (event_item_type(type)) {
2595 arg->flags.delim = token;
2596 type = read_token_item(&token);
2599 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2602 type = process_fields(event, &arg->flags.flags, &token);
2603 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2607 type = read_token_item(tok);
2615 return TEP_EVENT_ERROR;
2618 static enum tep_event_type
2619 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2621 struct tep_print_arg *field;
2622 enum tep_event_type type;
2625 memset(arg, 0, sizeof(*arg));
2626 arg->type = TEP_PRINT_SYMBOL;
2628 field = alloc_arg();
2630 do_warning_event(event, "%s: not enough memory!", __func__);
2634 type = process_field_arg(event, field, &token);
2636 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2637 goto out_free_field;
2639 arg->symbol.field = field;
2641 type = process_fields(event, &arg->symbol.symbols, &token);
2642 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2646 type = read_token_item(tok);
2654 return TEP_EVENT_ERROR;
2657 static enum tep_event_type
2658 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2659 char **tok, enum tep_print_arg_type type)
2661 memset(arg, 0, sizeof(*arg));
2664 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2667 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2670 return read_token_item(tok);
2673 free_arg(arg->hex.field);
2674 arg->hex.field = NULL;
2677 return TEP_EVENT_ERROR;
2680 static enum tep_event_type
2681 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2683 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2686 static enum tep_event_type
2687 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2690 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2693 static enum tep_event_type
2694 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2696 memset(arg, 0, sizeof(*arg));
2697 arg->type = TEP_PRINT_INT_ARRAY;
2699 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2702 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2705 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2708 return read_token_item(tok);
2711 free_arg(arg->int_array.count);
2712 arg->int_array.count = NULL;
2714 free_arg(arg->int_array.field);
2715 arg->int_array.field = NULL;
2718 return TEP_EVENT_ERROR;
2721 static enum tep_event_type
2722 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2724 struct tep_format_field *field;
2725 enum tep_event_type type;
2728 memset(arg, 0, sizeof(*arg));
2729 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2732 * The item within the parenthesis is another field that holds
2733 * the index into where the array starts.
2735 type = read_token(&token);
2737 if (type != TEP_EVENT_ITEM)
2740 /* Find the field */
2742 field = tep_find_field(event, token);
2746 arg->dynarray.field = field;
2747 arg->dynarray.index = 0;
2749 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2753 type = read_token_item(&token);
2755 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2761 do_warning_event(event, "%s: not enough memory!", __func__);
2763 return TEP_EVENT_ERROR;
2766 type = process_arg(event, arg, &token);
2767 if (type == TEP_EVENT_ERROR)
2770 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2774 type = read_token_item(tok);
2782 return TEP_EVENT_ERROR;
2785 static enum tep_event_type
2786 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2789 struct tep_format_field *field;
2790 enum tep_event_type type;
2793 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2796 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2798 /* Find the field */
2799 field = tep_find_field(event, token);
2803 arg->dynarray.field = field;
2804 arg->dynarray.index = 0;
2806 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2809 type = read_token(&token);
2818 return TEP_EVENT_ERROR;
2821 static enum tep_event_type
2822 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2824 struct tep_print_arg *item_arg;
2825 enum tep_event_type type;
2828 type = process_arg(event, arg, &token);
2830 if (type == TEP_EVENT_ERROR)
2833 if (type == TEP_EVENT_OP)
2834 type = process_op(event, arg, &token);
2836 if (type == TEP_EVENT_ERROR)
2839 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2843 type = read_token_item(&token);
2846 * If the next token is an item or another open paren, then
2847 * this was a typecast.
2849 if (event_item_type(type) ||
2850 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2852 /* make this a typecast and contine */
2854 /* prevous must be an atom */
2855 if (arg->type != TEP_PRINT_ATOM) {
2856 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2860 item_arg = alloc_arg();
2862 do_warning_event(event, "%s: not enough memory!",
2867 arg->type = TEP_PRINT_TYPE;
2868 arg->typecast.type = arg->atom.atom;
2869 arg->typecast.item = item_arg;
2870 type = process_arg_token(event, item_arg, &token, type);
2880 return TEP_EVENT_ERROR;
2884 static enum tep_event_type
2885 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2888 enum tep_event_type type;
2891 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2894 arg->type = TEP_PRINT_STRING;
2895 arg->string.string = token;
2896 arg->string.offset = -1;
2898 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2901 type = read_token(&token);
2910 return TEP_EVENT_ERROR;
2913 static enum tep_event_type
2914 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2917 enum tep_event_type type;
2920 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2923 arg->type = TEP_PRINT_BITMASK;
2924 arg->bitmask.bitmask = token;
2925 arg->bitmask.offset = -1;
2927 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2930 type = read_token(&token);
2939 return TEP_EVENT_ERROR;
2942 static struct tep_function_handler *
2943 find_func_handler(struct tep_handle *tep, char *func_name)
2945 struct tep_function_handler *func;
2950 for (func = tep->func_handlers; func; func = func->next) {
2951 if (strcmp(func->name, func_name) == 0)
2958 static void remove_func_handler(struct tep_handle *tep, char *func_name)
2960 struct tep_function_handler *func;
2961 struct tep_function_handler **next;
2963 next = &tep->func_handlers;
2964 while ((func = *next)) {
2965 if (strcmp(func->name, func_name) == 0) {
2967 free_func_handle(func);
2974 static enum tep_event_type
2975 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
2976 struct tep_print_arg *arg, char **tok)
2978 struct tep_print_arg **next_arg;
2979 struct tep_print_arg *farg;
2980 enum tep_event_type type;
2984 arg->type = TEP_PRINT_FUNC;
2985 arg->func.func = func;
2989 next_arg = &(arg->func.args);
2990 for (i = 0; i < func->nr_args; i++) {
2993 do_warning_event(event, "%s: not enough memory!",
2995 return TEP_EVENT_ERROR;
2998 type = process_arg(event, farg, &token);
2999 if (i < (func->nr_args - 1)) {
3000 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3001 do_warning_event(event,
3002 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3003 func->name, func->nr_args,
3004 event->name, i + 1);
3008 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3009 do_warning_event(event,
3010 "Error: function '%s()' only expects %d arguments but event %s has more",
3011 func->name, func->nr_args, event->name);
3017 next_arg = &(farg->next);
3021 type = read_token(&token);
3029 return TEP_EVENT_ERROR;
3032 static enum tep_event_type
3033 process_function(struct tep_event *event, struct tep_print_arg *arg,
3034 char *token, char **tok)
3036 struct tep_function_handler *func;
3038 if (strcmp(token, "__print_flags") == 0) {
3041 return process_flags(event, arg, tok);
3043 if (strcmp(token, "__print_symbolic") == 0) {
3045 is_symbolic_field = 1;
3046 return process_symbols(event, arg, tok);
3048 if (strcmp(token, "__print_hex") == 0) {
3050 return process_hex(event, arg, tok);
3052 if (strcmp(token, "__print_hex_str") == 0) {
3054 return process_hex_str(event, arg, tok);
3056 if (strcmp(token, "__print_array") == 0) {
3058 return process_int_array(event, arg, tok);
3060 if (strcmp(token, "__get_str") == 0) {
3062 return process_str(event, arg, tok);
3064 if (strcmp(token, "__get_bitmask") == 0) {
3066 return process_bitmask(event, arg, tok);
3068 if (strcmp(token, "__get_dynamic_array") == 0) {
3070 return process_dynamic_array(event, arg, tok);
3072 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3074 return process_dynamic_array_len(event, arg, tok);
3077 func = find_func_handler(event->tep, token);
3080 return process_func_handler(event, func, arg, tok);
3083 do_warning_event(event, "function %s not defined", token);
3085 return TEP_EVENT_ERROR;
3088 static enum tep_event_type
3089 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3090 char **tok, enum tep_event_type type)
3098 case TEP_EVENT_ITEM:
3099 if (strcmp(token, "REC") == 0) {
3101 type = process_entry(event, arg, &token);
3105 /* test the next token */
3106 type = read_token_item(&token);
3109 * If the next token is a parenthesis, then this
3112 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3115 /* this will free atom. */
3116 type = process_function(event, arg, atom, &token);
3119 /* atoms can be more than one token long */
3120 while (type == TEP_EVENT_ITEM) {
3122 new_atom = realloc(atom,
3123 strlen(atom) + strlen(token) + 2);
3128 return TEP_EVENT_ERROR;
3132 strcat(atom, token);
3134 type = read_token_item(&token);
3137 arg->type = TEP_PRINT_ATOM;
3138 arg->atom.atom = atom;
3141 case TEP_EVENT_DQUOTE:
3142 case TEP_EVENT_SQUOTE:
3143 arg->type = TEP_PRINT_ATOM;
3144 arg->atom.atom = token;
3145 type = read_token_item(&token);
3147 case TEP_EVENT_DELIM:
3148 if (strcmp(token, "(") == 0) {
3150 type = process_paren(event, arg, &token);
3154 /* handle single ops */
3155 arg->type = TEP_PRINT_OP;
3157 arg->op.left = NULL;
3158 type = process_op(event, arg, &token);
3160 /* On error, the op is freed */
3161 if (type == TEP_EVENT_ERROR)
3164 /* return error type if errored */
3167 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3169 do_warning_event(event, "unexpected type %d", type);
3170 return TEP_EVENT_ERROR;
3177 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3179 enum tep_event_type type = TEP_EVENT_ERROR;
3180 struct tep_print_arg *arg;
3185 if (type == TEP_EVENT_NEWLINE) {
3186 type = read_token_item(&token);
3192 do_warning_event(event, "%s: not enough memory!",
3197 type = process_arg(event, arg, &token);
3199 if (type == TEP_EVENT_ERROR) {
3208 if (type == TEP_EVENT_OP) {
3209 type = process_op(event, arg, &token);
3211 if (type == TEP_EVENT_ERROR) {
3220 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3227 } while (type != TEP_EVENT_NONE);
3229 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3235 static int event_read_print(struct tep_event *event)
3237 enum tep_event_type type;
3241 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3244 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3247 if (read_expected(TEP_EVENT_OP, ":") < 0)
3250 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3254 event->print_fmt.format = token;
3255 event->print_fmt.args = NULL;
3257 /* ok to have no arg */
3258 type = read_token_item(&token);
3260 if (type == TEP_EVENT_NONE)
3263 /* Handle concatenation of print lines */
3264 if (type == TEP_EVENT_DQUOTE) {
3267 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3270 free_token(event->print_fmt.format);
3271 event->print_fmt.format = NULL;
3276 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3281 ret = event_read_print_args(event, &event->print_fmt.args);
3293 * tep_find_common_field - return a common field by event
3294 * @event: handle for the event
3295 * @name: the name of the common field to return
3297 * Returns a common field from the event by the given @name.
3298 * This only searches the common fields and not all field.
3300 struct tep_format_field *
3301 tep_find_common_field(struct tep_event *event, const char *name)
3303 struct tep_format_field *format;
3305 for (format = event->format.common_fields;
3306 format; format = format->next) {
3307 if (strcmp(format->name, name) == 0)
3315 * tep_find_field - find a non-common field
3316 * @event: handle for the event
3317 * @name: the name of the non-common field
3319 * Returns a non-common field by the given @name.
3320 * This does not search common fields.
3322 struct tep_format_field *
3323 tep_find_field(struct tep_event *event, const char *name)
3325 struct tep_format_field *format;
3327 for (format = event->format.fields;
3328 format; format = format->next) {
3329 if (strcmp(format->name, name) == 0)
3337 * tep_find_any_field - find any field by name
3338 * @event: handle for the event
3339 * @name: the name of the field
3341 * Returns a field by the given @name.
3342 * This searches the common field names first, then
3343 * the non-common ones if a common one was not found.
3345 struct tep_format_field *
3346 tep_find_any_field(struct tep_event *event, const char *name)
3348 struct tep_format_field *format;
3350 format = tep_find_common_field(event, name);
3353 return tep_find_field(event, name);
3357 * tep_read_number - read a number from data
3358 * @tep: a handle to the trace event parser context
3359 * @ptr: the raw data
3360 * @size: the size of the data that holds the number
3362 * Returns the number (converted to host) from the
3365 unsigned long long tep_read_number(struct tep_handle *tep,
3366 const void *ptr, int size)
3368 unsigned long long val;
3372 return *(unsigned char *)ptr;
3374 return tep_data2host2(tep, *(unsigned short *)ptr);
3376 return tep_data2host4(tep, *(unsigned int *)ptr);
3378 memcpy(&val, (ptr), sizeof(unsigned long long));
3379 return tep_data2host8(tep, val);
3387 * tep_read_number_field - read a number from data
3388 * @field: a handle to the field
3389 * @data: the raw data to read
3390 * @value: the value to place the number in
3392 * Reads raw data according to a field offset and size,
3393 * and translates it into @value.
3395 * Returns 0 on success, -1 otherwise.
3397 int tep_read_number_field(struct tep_format_field *field, const void *data,
3398 unsigned long long *value)
3402 switch (field->size) {
3407 *value = tep_read_number(field->event->tep,
3408 data + field->offset, field->size);
3415 static int get_common_info(struct tep_handle *tep,
3416 const char *type, int *offset, int *size)
3418 struct tep_event *event;
3419 struct tep_format_field *field;
3422 * All events should have the same common elements.
3423 * Pick any event to find where the type is;
3426 do_warning("no event_list!");
3430 event = tep->events[0];
3431 field = tep_find_common_field(event, type);
3435 *offset = field->offset;
3436 *size = field->size;
3441 static int __parse_common(struct tep_handle *tep, void *data,
3442 int *size, int *offset, const char *name)
3447 ret = get_common_info(tep, name, offset, size);
3451 return tep_read_number(tep, data + *offset, *size);
3454 static int trace_parse_common_type(struct tep_handle *tep, void *data)
3456 return __parse_common(tep, data,
3457 &tep->type_size, &tep->type_offset,
3461 static int parse_common_pid(struct tep_handle *tep, void *data)
3463 return __parse_common(tep, data,
3464 &tep->pid_size, &tep->pid_offset,
3468 static int parse_common_pc(struct tep_handle *tep, void *data)
3470 return __parse_common(tep, data,
3471 &tep->pc_size, &tep->pc_offset,
3472 "common_preempt_count");
3475 static int parse_common_flags(struct tep_handle *tep, void *data)
3477 return __parse_common(tep, data,
3478 &tep->flags_size, &tep->flags_offset,
3482 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
3484 return __parse_common(tep, data,
3485 &tep->ld_size, &tep->ld_offset,
3486 "common_lock_depth");
3489 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
3491 return __parse_common(tep, data,
3492 &tep->ld_size, &tep->ld_offset,
3493 "common_migrate_disable");
3496 static int events_id_cmp(const void *a, const void *b);
3499 * tep_find_event - find an event by given id
3500 * @tep: a handle to the trace event parser context
3501 * @id: the id of the event
3503 * Returns an event that has a given @id.
3505 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
3507 struct tep_event **eventptr;
3508 struct tep_event key;
3509 struct tep_event *pkey = &key;
3511 /* Check cache first */
3512 if (tep->last_event && tep->last_event->id == id)
3513 return tep->last_event;
3517 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
3518 sizeof(*tep->events), events_id_cmp);
3521 tep->last_event = *eventptr;
3529 * tep_find_event_by_name - find an event by given name
3530 * @tep: a handle to the trace event parser context
3531 * @sys: the system name to search for
3532 * @name: the name of the event to search for
3534 * This returns an event with a given @name and under the system
3535 * @sys. If @sys is NULL the first event with @name is returned.
3538 tep_find_event_by_name(struct tep_handle *tep,
3539 const char *sys, const char *name)
3541 struct tep_event *event = NULL;
3544 if (tep->last_event &&
3545 strcmp(tep->last_event->name, name) == 0 &&
3546 (!sys || strcmp(tep->last_event->system, sys) == 0))
3547 return tep->last_event;
3549 for (i = 0; i < tep->nr_events; i++) {
3550 event = tep->events[i];
3551 if (strcmp(event->name, name) == 0) {
3554 if (strcmp(event->system, sys) == 0)
3558 if (i == tep->nr_events)
3561 tep->last_event = event;
3565 static unsigned long long
3566 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3568 struct tep_handle *tep = event->tep;
3569 unsigned long long val = 0;
3570 unsigned long long left, right;
3571 struct tep_print_arg *typearg = NULL;
3572 struct tep_print_arg *larg;
3573 unsigned long offset;
3574 unsigned int field_size;
3576 switch (arg->type) {
3577 case TEP_PRINT_NULL:
3580 case TEP_PRINT_ATOM:
3581 return strtoull(arg->atom.atom, NULL, 0);
3582 case TEP_PRINT_FIELD:
3583 if (!arg->field.field) {
3584 arg->field.field = tep_find_any_field(event, arg->field.name);
3585 if (!arg->field.field)
3586 goto out_warning_field;
3589 /* must be a number */
3590 val = tep_read_number(tep, data + arg->field.field->offset,
3591 arg->field.field->size);
3593 case TEP_PRINT_FLAGS:
3594 case TEP_PRINT_SYMBOL:
3595 case TEP_PRINT_INT_ARRAY:
3597 case TEP_PRINT_HEX_STR:
3599 case TEP_PRINT_TYPE:
3600 val = eval_num_arg(data, size, event, arg->typecast.item);
3601 return eval_type(val, arg, 0);
3602 case TEP_PRINT_STRING:
3603 case TEP_PRINT_BSTRING:
3604 case TEP_PRINT_BITMASK:
3606 case TEP_PRINT_FUNC: {
3609 val = process_defined_func(&s, data, size, event, arg);
3610 trace_seq_destroy(&s);
3614 if (strcmp(arg->op.op, "[") == 0) {
3616 * Arrays are special, since we don't want
3617 * to read the arg as is.
3619 right = eval_num_arg(data, size, event, arg->op.right);
3621 /* handle typecasts */
3622 larg = arg->op.left;
3623 while (larg->type == TEP_PRINT_TYPE) {
3626 larg = larg->typecast.item;
3629 /* Default to long size */
3630 field_size = tep->long_size;
3632 switch (larg->type) {
3633 case TEP_PRINT_DYNAMIC_ARRAY:
3634 offset = tep_read_number(tep,
3635 data + larg->dynarray.field->offset,
3636 larg->dynarray.field->size);
3637 if (larg->dynarray.field->elementsize)
3638 field_size = larg->dynarray.field->elementsize;
3640 * The actual length of the dynamic array is stored
3641 * in the top half of the field, and the offset
3642 * is in the bottom half of the 32 bit field.
3647 case TEP_PRINT_FIELD:
3648 if (!larg->field.field) {
3650 tep_find_any_field(event, larg->field.name);
3651 if (!larg->field.field) {
3653 goto out_warning_field;
3656 field_size = larg->field.field->elementsize;
3657 offset = larg->field.field->offset +
3658 right * larg->field.field->elementsize;
3661 goto default_op; /* oops, all bets off */
3663 val = tep_read_number(tep,
3664 data + offset, field_size);
3666 val = eval_type(val, typearg, 1);
3668 } else if (strcmp(arg->op.op, "?") == 0) {
3669 left = eval_num_arg(data, size, event, arg->op.left);
3670 arg = arg->op.right;
3672 val = eval_num_arg(data, size, event, arg->op.left);
3674 val = eval_num_arg(data, size, event, arg->op.right);
3678 left = eval_num_arg(data, size, event, arg->op.left);
3679 right = eval_num_arg(data, size, event, arg->op.right);
3680 switch (arg->op.op[0]) {
3682 switch (arg->op.op[1]) {
3687 val = left != right;
3690 goto out_warning_op;
3698 val = left || right;
3704 val = left && right;
3709 switch (arg->op.op[1]) {
3714 val = left << right;
3717 val = left <= right;
3720 goto out_warning_op;
3724 switch (arg->op.op[1]) {
3729 val = left >> right;
3732 val = left >= right;
3735 goto out_warning_op;
3739 if (arg->op.op[1] != '=')
3740 goto out_warning_op;
3742 val = left == right;
3760 goto out_warning_op;
3763 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3764 offset = tep_read_number(tep,
3765 data + arg->dynarray.field->offset,
3766 arg->dynarray.field->size);
3768 * The total allocated length of the dynamic array is
3769 * stored in the top half of the field, and the offset
3770 * is in the bottom half of the 32 bit field.
3772 val = (unsigned long long)(offset >> 16);
3774 case TEP_PRINT_DYNAMIC_ARRAY:
3775 /* Without [], we pass the address to the dynamic data */
3776 offset = tep_read_number(tep,
3777 data + arg->dynarray.field->offset,
3778 arg->dynarray.field->size);
3780 * The total allocated length of the dynamic array is
3781 * stored in the top half of the field, and the offset
3782 * is in the bottom half of the 32 bit field.
3785 val = (unsigned long long)((unsigned long)data + offset);
3787 default: /* not sure what to do there */
3793 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3797 do_warning_event(event, "%s: field %s not found",
3798 __func__, arg->field.name);
3804 unsigned long long value;
3807 static const struct flag flags[] = {
3808 { "HI_SOFTIRQ", 0 },
3809 { "TIMER_SOFTIRQ", 1 },
3810 { "NET_TX_SOFTIRQ", 2 },
3811 { "NET_RX_SOFTIRQ", 3 },
3812 { "BLOCK_SOFTIRQ", 4 },
3813 { "IRQ_POLL_SOFTIRQ", 5 },
3814 { "TASKLET_SOFTIRQ", 6 },
3815 { "SCHED_SOFTIRQ", 7 },
3816 { "HRTIMER_SOFTIRQ", 8 },
3817 { "RCU_SOFTIRQ", 9 },
3819 { "HRTIMER_NORESTART", 0 },
3820 { "HRTIMER_RESTART", 1 },
3823 static long long eval_flag(const char *flag)
3828 * Some flags in the format files do not get converted.
3829 * If the flag is not numeric, see if it is something that
3830 * we already know about.
3832 if (isdigit(flag[0]))
3833 return strtoull(flag, NULL, 0);
3835 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3836 if (strcmp(flags[i].name, flag) == 0)
3837 return flags[i].value;
3842 static void print_str_to_seq(struct trace_seq *s, const char *format,
3843 int len_arg, const char *str)
3846 trace_seq_printf(s, format, len_arg, str);
3848 trace_seq_printf(s, format, str);
3851 static void print_bitmask_to_seq(struct tep_handle *tep,
3852 struct trace_seq *s, const char *format,
3853 int len_arg, const void *data, int size)
3855 int nr_bits = size * 8;
3856 int str_size = (nr_bits + 3) / 4;
3864 * The kernel likes to put in commas every 32 bits, we
3867 str_size += (nr_bits - 1) / 32;
3869 str = malloc(str_size + 1);
3871 do_warning("%s: not enough memory!", __func__);
3876 /* Start out with -2 for the two chars per byte */
3877 for (i = str_size - 2; i >= 0; i -= 2) {
3879 * data points to a bit mask of size bytes.
3880 * In the kernel, this is an array of long words, thus
3881 * endianness is very important.
3883 if (tep->file_bigendian)
3884 index = size - (len + 1);
3888 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3889 memcpy(str + i, buf, 2);
3891 if (!(len & 3) && i > 0) {
3898 trace_seq_printf(s, format, len_arg, str);
3900 trace_seq_printf(s, format, str);
3905 static void print_str_arg(struct trace_seq *s, void *data, int size,
3906 struct tep_event *event, const char *format,
3907 int len_arg, struct tep_print_arg *arg)
3909 struct tep_handle *tep = event->tep;
3910 struct tep_print_flag_sym *flag;
3911 struct tep_format_field *field;
3912 struct printk_map *printk;
3913 long long val, fval;
3914 unsigned long long addr;
3920 switch (arg->type) {
3921 case TEP_PRINT_NULL:
3924 case TEP_PRINT_ATOM:
3925 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3927 case TEP_PRINT_FIELD:
3928 field = arg->field.field;
3930 field = tep_find_any_field(event, arg->field.name);
3932 str = arg->field.name;
3933 goto out_warning_field;
3935 arg->field.field = field;
3937 /* Zero sized fields, mean the rest of the data */
3938 len = field->size ? : size - field->offset;
3941 * Some events pass in pointers. If this is not an array
3942 * and the size is the same as long_size, assume that it
3945 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
3946 field->size == tep->long_size) {
3948 /* Handle heterogeneous recording and processing
3952 * Traces recorded on 32-bit devices (32-bit
3953 * addressing) and processed on 64-bit devices:
3954 * In this case, only 32 bits should be read.
3957 * Traces recorded on 64 bit devices and processed
3958 * on 32-bit devices:
3959 * In this case, 64 bits must be read.
3961 addr = (tep->long_size == 8) ?
3962 *(unsigned long long *)(data + field->offset) :
3963 (unsigned long long)*(unsigned int *)(data + field->offset);
3965 /* Check if it matches a print format */
3966 printk = find_printk(tep, addr);
3968 trace_seq_puts(s, printk->printk);
3970 trace_seq_printf(s, "%llx", addr);
3973 str = malloc(len + 1);
3975 do_warning_event(event, "%s: not enough memory!",
3979 memcpy(str, data + field->offset, len);
3981 print_str_to_seq(s, format, len_arg, str);
3984 case TEP_PRINT_FLAGS:
3985 val = eval_num_arg(data, size, event, arg->flags.field);
3987 for (flag = arg->flags.flags; flag; flag = flag->next) {
3988 fval = eval_flag(flag->value);
3989 if (!val && fval < 0) {
3990 print_str_to_seq(s, format, len_arg, flag->str);
3993 if (fval > 0 && (val & fval) == fval) {
3994 if (print && arg->flags.delim)
3995 trace_seq_puts(s, arg->flags.delim);
3996 print_str_to_seq(s, format, len_arg, flag->str);
4002 if (print && arg->flags.delim)
4003 trace_seq_puts(s, arg->flags.delim);
4004 trace_seq_printf(s, "0x%llx", val);
4007 case TEP_PRINT_SYMBOL:
4008 val = eval_num_arg(data, size, event, arg->symbol.field);
4009 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4010 fval = eval_flag(flag->value);
4012 print_str_to_seq(s, format, len_arg, flag->str);
4017 trace_seq_printf(s, "0x%llx", val);
4020 case TEP_PRINT_HEX_STR:
4021 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4022 unsigned long offset;
4023 offset = tep_read_number(tep,
4024 data + arg->hex.field->dynarray.field->offset,
4025 arg->hex.field->dynarray.field->size);
4026 hex = data + (offset & 0xffff);
4028 field = arg->hex.field->field.field;
4030 str = arg->hex.field->field.name;
4031 field = tep_find_any_field(event, str);
4033 goto out_warning_field;
4034 arg->hex.field->field.field = field;
4036 hex = data + field->offset;
4038 len = eval_num_arg(data, size, event, arg->hex.size);
4039 for (i = 0; i < len; i++) {
4040 if (i && arg->type == TEP_PRINT_HEX)
4041 trace_seq_putc(s, ' ');
4042 trace_seq_printf(s, "%02x", hex[i]);
4046 case TEP_PRINT_INT_ARRAY: {
4050 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4051 unsigned long offset;
4052 struct tep_format_field *field =
4053 arg->int_array.field->dynarray.field;
4054 offset = tep_read_number(tep,
4055 data + field->offset,
4057 num = data + (offset & 0xffff);
4059 field = arg->int_array.field->field.field;
4061 str = arg->int_array.field->field.name;
4062 field = tep_find_any_field(event, str);
4064 goto out_warning_field;
4065 arg->int_array.field->field.field = field;
4067 num = data + field->offset;
4069 len = eval_num_arg(data, size, event, arg->int_array.count);
4070 el_size = eval_num_arg(data, size, event,
4071 arg->int_array.el_size);
4072 for (i = 0; i < len; i++) {
4074 trace_seq_putc(s, ' ');
4077 trace_seq_printf(s, "%u", *(uint8_t *)num);
4078 } else if (el_size == 2) {
4079 trace_seq_printf(s, "%u", *(uint16_t *)num);
4080 } else if (el_size == 4) {
4081 trace_seq_printf(s, "%u", *(uint32_t *)num);
4082 } else if (el_size == 8) {
4083 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4085 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4086 el_size, *(uint8_t *)num);
4094 case TEP_PRINT_TYPE:
4096 case TEP_PRINT_STRING: {
4099 if (arg->string.offset == -1) {
4100 struct tep_format_field *f;
4102 f = tep_find_any_field(event, arg->string.string);
4103 arg->string.offset = f->offset;
4105 str_offset = tep_data2host4(tep, *(unsigned int *)(data + arg->string.offset));
4106 str_offset &= 0xffff;
4107 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4110 case TEP_PRINT_BSTRING:
4111 print_str_to_seq(s, format, len_arg, arg->string.string);
4113 case TEP_PRINT_BITMASK: {
4117 if (arg->bitmask.offset == -1) {
4118 struct tep_format_field *f;
4120 f = tep_find_any_field(event, arg->bitmask.bitmask);
4121 arg->bitmask.offset = f->offset;
4123 bitmask_offset = tep_data2host4(tep, *(unsigned int *)(data + arg->bitmask.offset));
4124 bitmask_size = bitmask_offset >> 16;
4125 bitmask_offset &= 0xffff;
4126 print_bitmask_to_seq(tep, s, format, len_arg,
4127 data + bitmask_offset, bitmask_size);
4132 * The only op for string should be ? :
4134 if (arg->op.op[0] != '?')
4136 val = eval_num_arg(data, size, event, arg->op.left);
4138 print_str_arg(s, data, size, event,
4139 format, len_arg, arg->op.right->op.left);
4141 print_str_arg(s, data, size, event,
4142 format, len_arg, arg->op.right->op.right);
4144 case TEP_PRINT_FUNC:
4145 process_defined_func(s, data, size, event, arg);
4155 do_warning_event(event, "%s: field %s not found",
4156 __func__, arg->field.name);
4159 static unsigned long long
4160 process_defined_func(struct trace_seq *s, void *data, int size,
4161 struct tep_event *event, struct tep_print_arg *arg)
4163 struct tep_function_handler *func_handle = arg->func.func;
4164 struct func_params *param;
4165 unsigned long long *args;
4166 unsigned long long ret;
4167 struct tep_print_arg *farg;
4168 struct trace_seq str;
4170 struct save_str *next;
4172 } *strings = NULL, *string;
4175 if (!func_handle->nr_args) {
4176 ret = (*func_handle->func)(s, NULL);
4180 farg = arg->func.args;
4181 param = func_handle->params;
4184 args = malloc(sizeof(*args) * func_handle->nr_args);
4188 for (i = 0; i < func_handle->nr_args; i++) {
4189 switch (param->type) {
4190 case TEP_FUNC_ARG_INT:
4191 case TEP_FUNC_ARG_LONG:
4192 case TEP_FUNC_ARG_PTR:
4193 args[i] = eval_num_arg(data, size, event, farg);
4195 case TEP_FUNC_ARG_STRING:
4196 trace_seq_init(&str);
4197 print_str_arg(&str, data, size, event, "%s", -1, farg);
4198 trace_seq_terminate(&str);
4199 string = malloc(sizeof(*string));
4201 do_warning_event(event, "%s(%d): malloc str",
4202 __func__, __LINE__);
4205 string->next = strings;
4206 string->str = strdup(str.buffer);
4209 do_warning_event(event, "%s(%d): malloc str",
4210 __func__, __LINE__);
4213 args[i] = (uintptr_t)string->str;
4215 trace_seq_destroy(&str);
4219 * Something went totally wrong, this is not
4220 * an input error, something in this code broke.
4222 do_warning_event(event, "Unexpected end of arguments\n");
4226 param = param->next;
4229 ret = (*func_handle->func)(s, args);
4234 strings = string->next;
4240 /* TBD : handle return type here */
4244 static void free_args(struct tep_print_arg *args)
4246 struct tep_print_arg *next;
4256 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4258 struct tep_handle *tep = event->tep;
4259 struct tep_format_field *field, *ip_field;
4260 struct tep_print_arg *args, *arg, **next;
4261 unsigned long long ip, val;
4266 field = tep->bprint_buf_field;
4267 ip_field = tep->bprint_ip_field;
4270 field = tep_find_field(event, "buf");
4272 do_warning_event(event, "can't find buffer field for binary printk");
4275 ip_field = tep_find_field(event, "ip");
4277 do_warning_event(event, "can't find ip field for binary printk");
4280 tep->bprint_buf_field = field;
4281 tep->bprint_ip_field = ip_field;
4284 ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
4287 * The first arg is the IP pointer.
4291 do_warning_event(event, "%s(%d): not enough memory!",
4292 __func__, __LINE__);
4299 arg->type = TEP_PRINT_ATOM;
4301 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4304 /* skip the first "%ps: " */
4305 for (ptr = fmt + 5, bptr = data + field->offset;
4306 bptr < data + size && *ptr; ptr++) {
4331 if (isalnum(ptr[1])) {
4333 /* Check for special pointers */
4343 * Older kernels do not process
4344 * dereferenced pointers.
4345 * Only process if the pointer
4346 * value is a printable.
4348 if (isprint(*(char *)bptr))
4349 goto process_string;
4362 vsize = tep->long_size;
4376 /* the pointers are always 4 bytes aligned */
4377 bptr = (void *)(((unsigned long)bptr + 3) &
4379 val = tep_read_number(tep, bptr, vsize);
4383 do_warning_event(event, "%s(%d): not enough memory!",
4384 __func__, __LINE__);
4388 arg->type = TEP_PRINT_ATOM;
4389 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4396 * The '*' case means that an arg is used as the length.
4397 * We need to continue to figure out for what.
4407 do_warning_event(event, "%s(%d): not enough memory!",
4408 __func__, __LINE__);
4412 arg->type = TEP_PRINT_BSTRING;
4413 arg->string.string = strdup(bptr);
4414 if (!arg->string.string)
4416 bptr += strlen(bptr) + 1;
4433 get_bprint_format(void *data, int size __maybe_unused,
4434 struct tep_event *event)
4436 struct tep_handle *tep = event->tep;
4437 unsigned long long addr;
4438 struct tep_format_field *field;
4439 struct printk_map *printk;
4442 field = tep->bprint_fmt_field;
4445 field = tep_find_field(event, "fmt");
4447 do_warning_event(event, "can't find format field for binary printk");
4450 tep->bprint_fmt_field = field;
4453 addr = tep_read_number(tep, data + field->offset, field->size);
4455 printk = find_printk(tep, addr);
4457 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4462 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4468 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4469 struct tep_event *event, struct tep_print_arg *arg)
4472 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4474 if (arg->type == TEP_PRINT_FUNC) {
4475 process_defined_func(s, data, size, event, arg);
4479 if (arg->type != TEP_PRINT_FIELD) {
4480 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4486 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4487 if (!arg->field.field) {
4489 tep_find_any_field(event, arg->field.name);
4490 if (!arg->field.field) {
4491 do_warning_event(event, "%s: field %s not found",
4492 __func__, arg->field.name);
4496 if (arg->field.field->size != 6) {
4497 trace_seq_printf(s, "INVALIDMAC");
4500 buf = data + arg->field.field->offset;
4501 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4504 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4509 fmt = "%03d.%03d.%03d.%03d";
4511 fmt = "%d.%d.%d.%d";
4513 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4516 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4518 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4519 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4522 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4524 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4527 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4530 unsigned char zerolength[8];
4535 bool needcolon = false;
4537 struct in6_addr in6;
4539 memcpy(&in6, addr, sizeof(struct in6_addr));
4541 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4543 memset(zerolength, 0, sizeof(zerolength));
4550 /* find position of longest 0 run */
4551 for (i = 0; i < range; i++) {
4552 for (j = i; j < range; j++) {
4553 if (in6.s6_addr16[j] != 0)
4558 for (i = 0; i < range; i++) {
4559 if (zerolength[i] > longest) {
4560 longest = zerolength[i];
4564 if (longest == 1) /* don't compress a single 0 */
4568 for (i = 0; i < range; i++) {
4569 if (i == colonpos) {
4570 if (needcolon || i == 0)
4571 trace_seq_printf(s, ":");
4572 trace_seq_printf(s, ":");
4578 trace_seq_printf(s, ":");
4581 /* hex u16 without leading 0s */
4582 word = ntohs(in6.s6_addr16[i]);
4586 trace_seq_printf(s, "%x%02x", hi, lo);
4588 trace_seq_printf(s, "%x", lo);
4595 trace_seq_printf(s, ":");
4596 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4602 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4606 for (j = 0; j < 16; j += 2) {
4607 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4608 if (i == 'I' && j < 14)
4609 trace_seq_printf(s, ":");
4614 * %pi4 print an IPv4 address with leading zeros
4615 * %pI4 print an IPv4 address without leading zeros
4616 * %pi6 print an IPv6 address without colons
4617 * %pI6 print an IPv6 address with colons
4618 * %pI6c print an IPv6 address in compressed form with colons
4619 * %pISpc print an IP address based on sockaddr; p adds port.
4621 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4622 void *data, int size, struct tep_event *event,
4623 struct tep_print_arg *arg)
4627 if (arg->type == TEP_PRINT_FUNC) {
4628 process_defined_func(s, data, size, event, arg);
4632 if (arg->type != TEP_PRINT_FIELD) {
4633 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4637 if (!arg->field.field) {
4639 tep_find_any_field(event, arg->field.name);
4640 if (!arg->field.field) {
4641 do_warning("%s: field %s not found",
4642 __func__, arg->field.name);
4647 buf = data + arg->field.field->offset;
4649 if (arg->field.field->size != 4) {
4650 trace_seq_printf(s, "INVALIDIPv4");
4653 print_ip4_addr(s, i, buf);
4658 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4659 void *data, int size, struct tep_event *event,
4660 struct tep_print_arg *arg)
4667 if (i == 'I' && *ptr == 'c') {
4673 if (arg->type == TEP_PRINT_FUNC) {
4674 process_defined_func(s, data, size, event, arg);
4678 if (arg->type != TEP_PRINT_FIELD) {
4679 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4683 if (!arg->field.field) {
4685 tep_find_any_field(event, arg->field.name);
4686 if (!arg->field.field) {
4687 do_warning("%s: field %s not found",
4688 __func__, arg->field.name);
4693 buf = data + arg->field.field->offset;
4695 if (arg->field.field->size != 16) {
4696 trace_seq_printf(s, "INVALIDIPv6");
4701 print_ip6c_addr(s, buf);
4703 print_ip6_addr(s, i, buf);
4708 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4709 void *data, int size, struct tep_event *event,
4710 struct tep_print_arg *arg)
4712 char have_c = 0, have_p = 0;
4714 struct sockaddr_storage *sa;
4731 if (arg->type == TEP_PRINT_FUNC) {
4732 process_defined_func(s, data, size, event, arg);
4736 if (arg->type != TEP_PRINT_FIELD) {
4737 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4741 if (!arg->field.field) {
4743 tep_find_any_field(event, arg->field.name);
4744 if (!arg->field.field) {
4745 do_warning("%s: field %s not found",
4746 __func__, arg->field.name);
4751 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4753 if (sa->ss_family == AF_INET) {
4754 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4756 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4757 trace_seq_printf(s, "INVALIDIPv4");
4761 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4763 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4766 } else if (sa->ss_family == AF_INET6) {
4767 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4769 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4770 trace_seq_printf(s, "INVALIDIPv6");
4775 trace_seq_printf(s, "[");
4777 buf = (unsigned char *) &sa6->sin6_addr;
4779 print_ip6c_addr(s, buf);
4781 print_ip6_addr(s, i, buf);
4784 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4790 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4791 void *data, int size, struct tep_event *event,
4792 struct tep_print_arg *arg)
4794 char i = *ptr; /* 'i' or 'I' */
4807 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4810 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4813 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4822 static int is_printable_array(char *p, unsigned int len)
4826 for (i = 0; i < len && p[i]; i++)
4827 if (!isprint(p[i]) && !isspace(p[i]))
4832 void tep_print_field(struct trace_seq *s, void *data,
4833 struct tep_format_field *field)
4835 unsigned long long val;
4836 unsigned int offset, len, i;
4837 struct tep_handle *tep = field->event->tep;
4839 if (field->flags & TEP_FIELD_IS_ARRAY) {
4840 offset = field->offset;
4842 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
4843 val = tep_read_number(tep, data + offset, len);
4848 if (field->flags & TEP_FIELD_IS_STRING &&
4849 is_printable_array(data + offset, len)) {
4850 trace_seq_printf(s, "%s", (char *)data + offset);
4852 trace_seq_puts(s, "ARRAY[");
4853 for (i = 0; i < len; i++) {
4855 trace_seq_puts(s, ", ");
4856 trace_seq_printf(s, "%02x",
4857 *((unsigned char *)data + offset + i));
4859 trace_seq_putc(s, ']');
4860 field->flags &= ~TEP_FIELD_IS_STRING;
4863 val = tep_read_number(tep, data + field->offset,
4865 if (field->flags & TEP_FIELD_IS_POINTER) {
4866 trace_seq_printf(s, "0x%llx", val);
4867 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
4868 switch (field->size) {
4871 * If field is long then print it in hex.
4872 * A long usually stores pointers.
4874 if (field->flags & TEP_FIELD_IS_LONG)
4875 trace_seq_printf(s, "0x%x", (int)val);
4877 trace_seq_printf(s, "%d", (int)val);
4880 trace_seq_printf(s, "%2d", (short)val);
4883 trace_seq_printf(s, "%1d", (char)val);
4886 trace_seq_printf(s, "%lld", val);
4889 if (field->flags & TEP_FIELD_IS_LONG)
4890 trace_seq_printf(s, "0x%llx", val);
4892 trace_seq_printf(s, "%llu", val);
4897 void tep_print_fields(struct trace_seq *s, void *data,
4898 int size __maybe_unused, struct tep_event *event)
4900 struct tep_format_field *field;
4902 field = event->format.fields;
4904 trace_seq_printf(s, " %s=", field->name);
4905 tep_print_field(s, data, field);
4906 field = field->next;
4910 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
4912 struct tep_handle *tep = event->tep;
4913 struct tep_print_fmt *print_fmt = &event->print_fmt;
4914 struct tep_print_arg *arg = print_fmt->args;
4915 struct tep_print_arg *args = NULL;
4916 const char *ptr = print_fmt->format;
4917 unsigned long long val;
4918 struct func_map *func;
4919 const char *saveptr;
4921 char *bprint_fmt = NULL;
4929 if (event->flags & TEP_EVENT_FL_FAILED) {
4930 trace_seq_printf(s, "[FAILED TO PARSE]");
4931 tep_print_fields(s, data, size, event);
4935 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
4936 bprint_fmt = get_bprint_format(data, size, event);
4937 args = make_bprint_args(bprint_fmt, data, size, event);
4942 for (; *ptr; ptr++) {
4948 trace_seq_putc(s, '\n');
4951 trace_seq_putc(s, '\t');
4954 trace_seq_putc(s, '\r');
4957 trace_seq_putc(s, '\\');
4960 trace_seq_putc(s, *ptr);
4964 } else if (*ptr == '%') {
4972 trace_seq_putc(s, '%');
4975 /* FIXME: need to handle properly */
4987 /* The argument is the length. */
4989 do_warning_event(event, "no argument match");
4990 event->flags |= TEP_EVENT_FL_FAILED;
4993 len_arg = eval_num_arg(data, size, event, arg);
5004 if (tep->long_size == 4)
5009 if (isalnum(ptr[1]))
5012 if (arg->type == TEP_PRINT_BSTRING) {
5013 trace_seq_puts(s, arg->string.string);
5018 if (*ptr == 'F' || *ptr == 'f' ||
5019 *ptr == 'S' || *ptr == 's') {
5021 } else if (*ptr == 'M' || *ptr == 'm') {
5022 print_mac_arg(s, *ptr, data, size, event, arg);
5025 } else if (*ptr == 'I' || *ptr == 'i') {
5028 n = print_ip_arg(s, ptr, data, size, event, arg);
5043 do_warning_event(event, "no argument match");
5044 event->flags |= TEP_EVENT_FL_FAILED;
5048 len = ((unsigned long)ptr + 1) -
5049 (unsigned long)saveptr;
5051 /* should never happen */
5053 do_warning_event(event, "bad format!");
5054 event->flags |= TEP_EVENT_FL_FAILED;
5058 memcpy(format, saveptr, len);
5061 val = eval_num_arg(data, size, event, arg);
5065 func = find_func(tep, val);
5067 trace_seq_puts(s, func->func);
5068 if (show_func == 'F')
5075 if (tep->long_size == 8 && ls == 1 &&
5076 sizeof(long) != 8) {
5079 /* make %l into %ll */
5080 if (ls == 1 && (p = strchr(format, 'l')))
5081 memmove(p+1, p, strlen(p)+1);
5082 else if (strcmp(format, "%p") == 0)
5083 strcpy(format, "0x%llx");
5089 trace_seq_printf(s, format, len_arg, (char)val);
5091 trace_seq_printf(s, format, (char)val);
5095 trace_seq_printf(s, format, len_arg, (short)val);
5097 trace_seq_printf(s, format, (short)val);
5101 trace_seq_printf(s, format, len_arg, (int)val);
5103 trace_seq_printf(s, format, (int)val);
5107 trace_seq_printf(s, format, len_arg, (long)val);
5109 trace_seq_printf(s, format, (long)val);
5113 trace_seq_printf(s, format, len_arg,
5116 trace_seq_printf(s, format, (long long)val);
5119 do_warning_event(event, "bad count (%d)", ls);
5120 event->flags |= TEP_EVENT_FL_FAILED;
5125 do_warning_event(event, "no matching argument");
5126 event->flags |= TEP_EVENT_FL_FAILED;
5130 len = ((unsigned long)ptr + 1) -
5131 (unsigned long)saveptr;
5133 /* should never happen */
5135 do_warning_event(event, "bad format!");
5136 event->flags |= TEP_EVENT_FL_FAILED;
5140 memcpy(format, saveptr, len);
5144 /* Use helper trace_seq */
5146 print_str_arg(&p, data, size, event,
5147 format, len_arg, arg);
5148 trace_seq_terminate(&p);
5149 trace_seq_puts(s, p.buffer);
5150 trace_seq_destroy(&p);
5154 trace_seq_printf(s, ">%c<", *ptr);
5158 trace_seq_putc(s, *ptr);
5161 if (event->flags & TEP_EVENT_FL_FAILED) {
5163 trace_seq_printf(s, "[FAILED TO PARSE]");
5173 * tep_data_latency_format - parse the data for the latency format
5174 * @tep: a handle to the trace event parser context
5175 * @s: the trace_seq to write to
5176 * @record: the record to read from
5178 * This parses out the Latency format (interrupts disabled,
5179 * need rescheduling, in hard/soft interrupt, preempt count
5180 * and lock depth) and places it into the trace_seq.
5182 void tep_data_latency_format(struct tep_handle *tep,
5183 struct trace_seq *s, struct tep_record *record)
5185 static int check_lock_depth = 1;
5186 static int check_migrate_disable = 1;
5187 static int lock_depth_exists;
5188 static int migrate_disable_exists;
5189 unsigned int lat_flags;
5192 int migrate_disable = 0;
5195 void *data = record->data;
5197 lat_flags = parse_common_flags(tep, data);
5198 pc = parse_common_pc(tep, data);
5199 /* lock_depth may not always exist */
5200 if (lock_depth_exists)
5201 lock_depth = parse_common_lock_depth(tep, data);
5202 else if (check_lock_depth) {
5203 lock_depth = parse_common_lock_depth(tep, data);
5205 check_lock_depth = 0;
5207 lock_depth_exists = 1;
5210 /* migrate_disable may not always exist */
5211 if (migrate_disable_exists)
5212 migrate_disable = parse_common_migrate_disable(tep, data);
5213 else if (check_migrate_disable) {
5214 migrate_disable = parse_common_migrate_disable(tep, data);
5215 if (migrate_disable < 0)
5216 check_migrate_disable = 0;
5218 migrate_disable_exists = 1;
5221 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5222 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5224 trace_seq_printf(s, "%c%c%c",
5225 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5226 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5228 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5230 (hardirq && softirq) ? 'H' :
5231 hardirq ? 'h' : softirq ? 's' : '.');
5234 trace_seq_printf(s, "%x", pc);
5236 trace_seq_putc(s, '.');
5238 if (migrate_disable_exists) {
5239 if (migrate_disable < 0)
5240 trace_seq_putc(s, '.');
5242 trace_seq_printf(s, "%d", migrate_disable);
5245 if (lock_depth_exists) {
5247 trace_seq_putc(s, '.');
5249 trace_seq_printf(s, "%d", lock_depth);
5252 trace_seq_terminate(s);
5256 * tep_data_type - parse out the given event type
5257 * @tep: a handle to the trace event parser context
5258 * @rec: the record to read from
5260 * This returns the event id from the @rec.
5262 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
5264 return trace_parse_common_type(tep, rec->data);
5268 * tep_data_pid - parse the PID from record
5269 * @tep: a handle to the trace event parser context
5270 * @rec: the record to parse
5272 * This returns the PID from a record.
5274 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
5276 return parse_common_pid(tep, rec->data);
5280 * tep_data_preempt_count - parse the preempt count from the record
5281 * @tep: a handle to the trace event parser context
5282 * @rec: the record to parse
5284 * This returns the preempt count from a record.
5286 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
5288 return parse_common_pc(tep, rec->data);
5292 * tep_data_flags - parse the latency flags from the record
5293 * @tep: a handle to the trace event parser context
5294 * @rec: the record to parse
5296 * This returns the latency flags from a record.
5298 * Use trace_flag_type enum for the flags (see event-parse.h).
5300 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
5302 return parse_common_flags(tep, rec->data);
5306 * tep_data_comm_from_pid - return the command line from PID
5307 * @tep: a handle to the trace event parser context
5308 * @pid: the PID of the task to search for
5310 * This returns a pointer to the command line that has the given
5313 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
5317 comm = find_cmdline(tep, pid);
5321 static struct tep_cmdline *
5322 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
5324 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5327 cmdlist = cmdlist->next;
5329 cmdlist = tep->cmdlist;
5331 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5332 cmdlist = cmdlist->next;
5334 return (struct tep_cmdline *)cmdlist;
5338 * tep_data_pid_from_comm - return the pid from a given comm
5339 * @tep: a handle to the trace event parser context
5340 * @comm: the cmdline to find the pid from
5341 * @next: the cmdline structure to find the next comm
5343 * This returns the cmdline structure that holds a pid for a given
5344 * comm, or NULL if none found. As there may be more than one pid for
5345 * a given comm, the result of this call can be passed back into
5346 * a recurring call in the @next parameter, and then it will find the
5348 * Also, it does a linear search, so it may be slow.
5350 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
5351 struct tep_cmdline *next)
5353 struct tep_cmdline *cmdline;
5356 * If the cmdlines have not been converted yet, then use
5360 return pid_from_cmdlist(tep, comm, next);
5364 * The next pointer could have been still from
5365 * a previous call before cmdlines were created
5367 if (next < tep->cmdlines ||
5368 next >= tep->cmdlines + tep->cmdline_count)
5375 cmdline = tep->cmdlines;
5377 while (cmdline < tep->cmdlines + tep->cmdline_count) {
5378 if (strcmp(cmdline->comm, comm) == 0)
5386 * tep_cmdline_pid - return the pid associated to a given cmdline
5387 * @tep: a handle to the trace event parser context
5388 * @cmdline: The cmdline structure to get the pid from
5390 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5393 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
5395 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5401 * If cmdlines have not been created yet, or cmdline is
5402 * not part of the array, then treat it as a cmdlist instead.
5404 if (!tep->cmdlines ||
5405 cmdline < tep->cmdlines ||
5406 cmdline >= tep->cmdlines + tep->cmdline_count)
5407 return cmdlist->pid;
5409 return cmdline->pid;
5413 * tep_event_info - parse the data into the print format
5414 * @s: the trace_seq to write to
5415 * @event: the handle to the event
5416 * @record: the record to read from
5418 * This parses the raw @data using the given @event information and
5419 * writes the print format into the trace_seq.
5421 void tep_event_info(struct trace_seq *s, struct tep_event *event,
5422 struct tep_record *record)
5424 int print_pretty = 1;
5426 if (event->tep->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
5427 tep_print_fields(s, record->data, record->size, event);
5430 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
5431 print_pretty = event->handler(s, record, event,
5435 pretty_print(s, record->data, record->size, event);
5438 trace_seq_terminate(s);
5441 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5443 if (!trace_clock || !use_trace_clock)
5446 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5447 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf")
5448 || !strncmp(trace_clock, "mono", 4))
5451 /* trace_clock is setting in tsc or counter mode */
5456 * tep_find_event_by_record - return the event from a given record
5457 * @tep: a handle to the trace event parser context
5458 * @record: The record to get the event from
5460 * Returns the associated event for a given record, or NULL if non is
5464 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
5468 if (record->size < 0) {
5469 do_warning("ug! negative record size %d", record->size);
5473 type = trace_parse_common_type(tep, record->data);
5475 return tep_find_event(tep, type);
5479 * tep_print_event_task - Write the event task comm, pid and CPU
5480 * @tep: a handle to the trace event parser context
5481 * @s: the trace_seq to write to
5482 * @event: the handle to the record's event
5483 * @record: The record to get the event from
5485 * Writes the tasks comm, pid and CPU to @s.
5487 void tep_print_event_task(struct tep_handle *tep, struct trace_seq *s,
5488 struct tep_event *event,
5489 struct tep_record *record)
5491 void *data = record->data;
5495 pid = parse_common_pid(tep, data);
5496 comm = find_cmdline(tep, pid);
5498 if (tep->latency_format)
5499 trace_seq_printf(s, "%8.8s-%-5d %3d", comm, pid, record->cpu);
5501 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5505 * tep_print_event_time - Write the event timestamp
5506 * @tep: a handle to the trace event parser context
5507 * @s: the trace_seq to write to
5508 * @event: the handle to the record's event
5509 * @record: The record to get the event from
5510 * @use_trace_clock: Set to parse according to the @tep->trace_clock
5512 * Writes the timestamp of the record into @s.
5514 void tep_print_event_time(struct tep_handle *tep, struct trace_seq *s,
5515 struct tep_event *event,
5516 struct tep_record *record,
5517 bool use_trace_clock)
5520 unsigned long usecs;
5521 unsigned long nsecs;
5523 bool use_usec_format;
5525 use_usec_format = is_timestamp_in_us(tep->trace_clock, use_trace_clock);
5526 if (use_usec_format) {
5527 secs = record->ts / NSEC_PER_SEC;
5528 nsecs = record->ts - secs * NSEC_PER_SEC;
5531 if (tep->latency_format) {
5532 tep_data_latency_format(tep, s, record);
5535 if (use_usec_format) {
5536 if (tep->flags & TEP_NSEC_OUTPUT) {
5540 usecs = (nsecs + 500) / NSEC_PER_USEC;
5541 /* To avoid usecs larger than 1 sec */
5542 if (usecs >= USEC_PER_SEC) {
5543 usecs -= USEC_PER_SEC;
5549 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5551 trace_seq_printf(s, " %12llu:", record->ts);
5555 * tep_print_event_data - Write the event data section
5556 * @tep: a handle to the trace event parser context
5557 * @s: the trace_seq to write to
5558 * @event: the handle to the record's event
5559 * @record: The record to get the event from
5561 * Writes the parsing of the record's data to @s.
5563 void tep_print_event_data(struct tep_handle *tep, struct trace_seq *s,
5564 struct tep_event *event,
5565 struct tep_record *record)
5567 static const char *spaces = " "; /* 20 spaces */
5570 trace_seq_printf(s, " %s: ", event->name);
5572 /* Space out the event names evenly. */
5573 len = strlen(event->name);
5575 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5577 tep_event_info(s, event, record);
5580 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
5581 struct tep_record *record, bool use_trace_clock)
5583 struct tep_event *event;
5585 event = tep_find_event_by_record(tep, record);
5588 int type = trace_parse_common_type(tep, record->data);
5590 do_warning("ug! no event found for type %d", type);
5591 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5592 for (i = 0; i < record->size; i++)
5593 trace_seq_printf(s, " %02x",
5594 ((unsigned char *)record->data)[i]);
5598 tep_print_event_task(tep, s, event, record);
5599 tep_print_event_time(tep, s, event, record, use_trace_clock);
5600 tep_print_event_data(tep, s, event, record);
5603 static int events_id_cmp(const void *a, const void *b)
5605 struct tep_event * const * ea = a;
5606 struct tep_event * const * eb = b;
5608 if ((*ea)->id < (*eb)->id)
5611 if ((*ea)->id > (*eb)->id)
5617 static int events_name_cmp(const void *a, const void *b)
5619 struct tep_event * const * ea = a;
5620 struct tep_event * const * eb = b;
5623 res = strcmp((*ea)->name, (*eb)->name);
5627 res = strcmp((*ea)->system, (*eb)->system);
5631 return events_id_cmp(a, b);
5634 static int events_system_cmp(const void *a, const void *b)
5636 struct tep_event * const * ea = a;
5637 struct tep_event * const * eb = b;
5640 res = strcmp((*ea)->system, (*eb)->system);
5644 res = strcmp((*ea)->name, (*eb)->name);
5648 return events_id_cmp(a, b);
5651 static struct tep_event **list_events_copy(struct tep_handle *tep)
5653 struct tep_event **events;
5658 events = malloc(sizeof(*events) * (tep->nr_events + 1));
5662 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
5663 events[tep->nr_events] = NULL;
5667 static void list_events_sort(struct tep_event **events, int nr_events,
5668 enum tep_event_sort_type sort_type)
5670 int (*sort)(const void *a, const void *b);
5672 switch (sort_type) {
5673 case TEP_EVENT_SORT_ID:
5674 sort = events_id_cmp;
5676 case TEP_EVENT_SORT_NAME:
5677 sort = events_name_cmp;
5679 case TEP_EVENT_SORT_SYSTEM:
5680 sort = events_system_cmp;
5687 qsort(events, nr_events, sizeof(*events), sort);
5691 * tep_list_events - Get events, sorted by given criteria.
5692 * @tep: a handle to the tep context
5693 * @sort_type: desired sort order of the events in the array
5695 * Returns an array of pointers to all events, sorted by the given
5696 * @sort_type criteria. The last element of the array is NULL. The returned
5697 * memory must not be freed, it is managed by the library.
5698 * The function is not thread safe.
5700 struct tep_event **tep_list_events(struct tep_handle *tep,
5701 enum tep_event_sort_type sort_type)
5703 struct tep_event **events;
5708 events = tep->sort_events;
5709 if (events && tep->last_type == sort_type)
5713 events = list_events_copy(tep);
5717 tep->sort_events = events;
5719 /* the internal events are sorted by id */
5720 if (sort_type == TEP_EVENT_SORT_ID) {
5721 tep->last_type = sort_type;
5726 list_events_sort(events, tep->nr_events, sort_type);
5727 tep->last_type = sort_type;
5734 * tep_list_events_copy - Thread safe version of tep_list_events()
5735 * @tep: a handle to the tep context
5736 * @sort_type: desired sort order of the events in the array
5738 * Returns an array of pointers to all events, sorted by the given
5739 * @sort_type criteria. The last element of the array is NULL. The returned
5740 * array is newly allocated inside the function and must be freed by the caller
5742 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
5743 enum tep_event_sort_type sort_type)
5745 struct tep_event **events;
5750 events = list_events_copy(tep);
5754 /* the internal events are sorted by id */
5755 if (sort_type == TEP_EVENT_SORT_ID)
5758 list_events_sort(events, tep->nr_events, sort_type);
5763 static struct tep_format_field **
5764 get_event_fields(const char *type, const char *name,
5765 int count, struct tep_format_field *list)
5767 struct tep_format_field **fields;
5768 struct tep_format_field *field;
5771 fields = malloc(sizeof(*fields) * (count + 1));
5775 for (field = list; field; field = field->next) {
5776 fields[i++] = field;
5777 if (i == count + 1) {
5778 do_warning("event %s has more %s fields than specified",
5786 do_warning("event %s has less %s fields than specified",
5795 * tep_event_common_fields - return a list of common fields for an event
5796 * @event: the event to return the common fields of.
5798 * Returns an allocated array of fields. The last item in the array is NULL.
5799 * The array must be freed with free().
5801 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
5803 return get_event_fields("common", event->name,
5804 event->format.nr_common,
5805 event->format.common_fields);
5809 * tep_event_fields - return a list of event specific fields for an event
5810 * @event: the event to return the fields of.
5812 * Returns an allocated array of fields. The last item in the array is NULL.
5813 * The array must be freed with free().
5815 struct tep_format_field **tep_event_fields(struct tep_event *event)
5817 return get_event_fields("event", event->name,
5818 event->format.nr_fields,
5819 event->format.fields);
5822 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
5824 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5826 trace_seq_puts(s, ", ");
5827 print_fields(s, field->next);
5832 static void print_args(struct tep_print_arg *args)
5834 int print_paren = 1;
5837 switch (args->type) {
5838 case TEP_PRINT_NULL:
5841 case TEP_PRINT_ATOM:
5842 printf("%s", args->atom.atom);
5844 case TEP_PRINT_FIELD:
5845 printf("REC->%s", args->field.name);
5847 case TEP_PRINT_FLAGS:
5848 printf("__print_flags(");
5849 print_args(args->flags.field);
5850 printf(", %s, ", args->flags.delim);
5852 print_fields(&s, args->flags.flags);
5853 trace_seq_do_printf(&s);
5854 trace_seq_destroy(&s);
5857 case TEP_PRINT_SYMBOL:
5858 printf("__print_symbolic(");
5859 print_args(args->symbol.field);
5862 print_fields(&s, args->symbol.symbols);
5863 trace_seq_do_printf(&s);
5864 trace_seq_destroy(&s);
5868 printf("__print_hex(");
5869 print_args(args->hex.field);
5871 print_args(args->hex.size);
5874 case TEP_PRINT_HEX_STR:
5875 printf("__print_hex_str(");
5876 print_args(args->hex.field);
5878 print_args(args->hex.size);
5881 case TEP_PRINT_INT_ARRAY:
5882 printf("__print_array(");
5883 print_args(args->int_array.field);
5885 print_args(args->int_array.count);
5887 print_args(args->int_array.el_size);
5890 case TEP_PRINT_STRING:
5891 case TEP_PRINT_BSTRING:
5892 printf("__get_str(%s)", args->string.string);
5894 case TEP_PRINT_BITMASK:
5895 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5897 case TEP_PRINT_TYPE:
5898 printf("(%s)", args->typecast.type);
5899 print_args(args->typecast.item);
5902 if (strcmp(args->op.op, ":") == 0)
5906 print_args(args->op.left);
5907 printf(" %s ", args->op.op);
5908 print_args(args->op.right);
5913 /* we should warn... */
5918 print_args(args->next);
5922 static void parse_header_field(const char *field,
5923 int *offset, int *size, int mandatory)
5925 unsigned long long save_input_buf_ptr;
5926 unsigned long long save_input_buf_siz;
5930 save_input_buf_ptr = input_buf_ptr;
5931 save_input_buf_siz = input_buf_siz;
5933 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
5935 if (read_expected(TEP_EVENT_OP, ":") < 0)
5939 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5944 * If this is not a mandatory field, then test it first.
5947 if (read_expected(TEP_EVENT_ITEM, field) < 0)
5950 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5952 if (strcmp(token, field) != 0)
5957 if (read_expected(TEP_EVENT_OP, ";") < 0)
5959 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
5961 if (read_expected(TEP_EVENT_OP, ":") < 0)
5963 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5965 *offset = atoi(token);
5967 if (read_expected(TEP_EVENT_OP, ";") < 0)
5969 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
5971 if (read_expected(TEP_EVENT_OP, ":") < 0)
5973 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
5975 *size = atoi(token);
5977 if (read_expected(TEP_EVENT_OP, ";") < 0)
5979 type = read_token(&token);
5980 if (type != TEP_EVENT_NEWLINE) {
5981 /* newer versions of the kernel have a "signed" type */
5982 if (type != TEP_EVENT_ITEM)
5985 if (strcmp(token, "signed") != 0)
5990 if (read_expected(TEP_EVENT_OP, ":") < 0)
5993 if (read_expect_type(TEP_EVENT_ITEM, &token))
5997 if (read_expected(TEP_EVENT_OP, ";") < 0)
6000 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
6008 input_buf_ptr = save_input_buf_ptr;
6009 input_buf_siz = save_input_buf_siz;
6016 * tep_parse_header_page - parse the data stored in the header page
6017 * @tep: a handle to the trace event parser context
6018 * @buf: the buffer storing the header page format string
6019 * @size: the size of @buf
6020 * @long_size: the long size to use if there is no header
6022 * This parses the header page format for information on the
6023 * ring buffer used. The @buf should be copied from
6025 * /sys/kernel/debug/tracing/events/header_page
6027 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
6034 * Old kernels did not have header page info.
6035 * Sorry but we just use what we find here in user space.
6037 tep->header_page_ts_size = sizeof(long long);
6038 tep->header_page_size_size = long_size;
6039 tep->header_page_data_offset = sizeof(long long) + long_size;
6040 tep->old_format = 1;
6043 init_input_buf(buf, size);
6045 parse_header_field("timestamp", &tep->header_page_ts_offset,
6046 &tep->header_page_ts_size, 1);
6047 parse_header_field("commit", &tep->header_page_size_offset,
6048 &tep->header_page_size_size, 1);
6049 parse_header_field("overwrite", &tep->header_page_overwrite,
6051 parse_header_field("data", &tep->header_page_data_offset,
6052 &tep->header_page_data_size, 1);
6057 static int event_matches(struct tep_event *event,
6058 int id, const char *sys_name,
6059 const char *event_name)
6061 if (id >= 0 && id != event->id)
6064 if (event_name && (strcmp(event_name, event->name) != 0))
6067 if (sys_name && (strcmp(sys_name, event->system) != 0))
6073 static void free_handler(struct event_handler *handle)
6075 free((void *)handle->sys_name);
6076 free((void *)handle->event_name);
6080 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
6082 struct event_handler *handle, **next;
6084 for (next = &tep->handlers; *next;
6085 next = &(*next)->next) {
6087 if (event_matches(event, handle->id,
6089 handle->event_name))
6096 pr_stat("overriding event (%d) %s:%s with new print handler",
6097 event->id, event->system, event->name);
6099 event->handler = handle->func;
6100 event->context = handle->context;
6102 *next = handle->next;
6103 free_handler(handle);
6109 * __tep_parse_format - parse the event format
6110 * @buf: the buffer storing the event format string
6111 * @size: the size of @buf
6112 * @sys: the system the event belongs to
6114 * This parses the event format and creates an event structure
6115 * to quickly parse raw data for a given event.
6117 * These files currently come from:
6119 * /sys/kernel/debug/tracing/events/.../.../format
6121 enum tep_errno __tep_parse_format(struct tep_event **eventp,
6122 struct tep_handle *tep, const char *buf,
6123 unsigned long size, const char *sys)
6125 struct tep_event *event;
6128 init_input_buf(buf, size);
6130 *eventp = event = alloc_event();
6132 return TEP_ERRNO__MEM_ALLOC_FAILED;
6134 event->name = event_read_name();
6137 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6138 goto event_alloc_failed;
6141 if (strcmp(sys, "ftrace") == 0) {
6142 event->flags |= TEP_EVENT_FL_ISFTRACE;
6144 if (strcmp(event->name, "bprint") == 0)
6145 event->flags |= TEP_EVENT_FL_ISBPRINT;
6148 event->id = event_read_id();
6149 if (event->id < 0) {
6150 ret = TEP_ERRNO__READ_ID_FAILED;
6152 * This isn't an allocation error actually.
6153 * But as the ID is critical, just bail out.
6155 goto event_alloc_failed;
6158 event->system = strdup(sys);
6159 if (!event->system) {
6160 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6161 goto event_alloc_failed;
6164 /* Add tep to event so that it can be referenced */
6167 ret = event_read_format(event);
6169 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6170 goto event_parse_failed;
6174 * If the event has an override, don't print warnings if the event
6175 * print format fails to parse.
6177 if (tep && find_event_handle(tep, event))
6180 ret = event_read_print(event);
6184 ret = TEP_ERRNO__READ_PRINT_FAILED;
6185 goto event_parse_failed;
6188 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6189 struct tep_format_field *field;
6190 struct tep_print_arg *arg, **list;
6192 /* old ftrace had no args */
6193 list = &event->print_fmt.args;
6194 for (field = event->format.fields; field; field = field->next) {
6197 event->flags |= TEP_EVENT_FL_FAILED;
6198 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6200 arg->type = TEP_PRINT_FIELD;
6201 arg->field.name = strdup(field->name);
6202 if (!arg->field.name) {
6203 event->flags |= TEP_EVENT_FL_FAILED;
6205 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6207 arg->field.field = field;
6217 event->flags |= TEP_EVENT_FL_FAILED;
6221 free(event->system);
6228 static enum tep_errno
6229 __parse_event(struct tep_handle *tep,
6230 struct tep_event **eventp,
6231 const char *buf, unsigned long size,
6234 int ret = __tep_parse_format(eventp, tep, buf, size, sys);
6235 struct tep_event *event = *eventp;
6240 if (tep && add_event(tep, event)) {
6241 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6242 goto event_add_failed;
6245 #define PRINT_ARGS 0
6246 if (PRINT_ARGS && event->print_fmt.args)
6247 print_args(event->print_fmt.args);
6252 tep_free_event(event);
6257 * tep_parse_format - parse the event format
6258 * @tep: a handle to the trace event parser context
6259 * @eventp: returned format
6260 * @buf: the buffer storing the event format string
6261 * @size: the size of @buf
6262 * @sys: the system the event belongs to
6264 * This parses the event format and creates an event structure
6265 * to quickly parse raw data for a given event.
6267 * These files currently come from:
6269 * /sys/kernel/debug/tracing/events/.../.../format
6271 enum tep_errno tep_parse_format(struct tep_handle *tep,
6272 struct tep_event **eventp,
6274 unsigned long size, const char *sys)
6276 return __parse_event(tep, eventp, buf, size, sys);
6280 * tep_parse_event - parse the event format
6281 * @tep: a handle to the trace event parser context
6282 * @buf: the buffer storing the event format string
6283 * @size: the size of @buf
6284 * @sys: the system the event belongs to
6286 * This parses the event format and creates an event structure
6287 * to quickly parse raw data for a given event.
6289 * These files currently come from:
6291 * /sys/kernel/debug/tracing/events/.../.../format
6293 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
6294 unsigned long size, const char *sys)
6296 struct tep_event *event = NULL;
6297 return __parse_event(tep, &event, buf, size, sys);
6300 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6301 const char *name, struct tep_record *record,
6302 unsigned long long *val, int err)
6306 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6310 if (tep_read_number_field(field, record->data, val)) {
6312 trace_seq_printf(s, " %s=INVALID", name);
6320 * tep_get_field_raw - return the raw pointer into the data field
6321 * @s: The seq to print to on error
6322 * @event: the event that the field is for
6323 * @name: The name of the field
6324 * @record: The record with the field name.
6325 * @len: place to store the field length.
6326 * @err: print default error if failed.
6328 * Returns a pointer into record->data of the field and places
6329 * the length of the field in @len.
6331 * On failure, it returns NULL.
6333 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6334 const char *name, struct tep_record *record,
6337 struct tep_format_field *field;
6338 void *data = record->data;
6345 field = tep_find_field(event, name);
6349 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6353 /* Allow @len to be NULL */
6357 offset = field->offset;
6358 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
6359 offset = tep_read_number(event->tep,
6360 data + offset, field->size);
6361 *len = offset >> 16;
6366 return data + offset;
6370 * tep_get_field_val - find a field and return its value
6371 * @s: The seq to print to on error
6372 * @event: the event that the field is for
6373 * @name: The name of the field
6374 * @record: The record with the field name.
6375 * @val: place to store the value of the field.
6376 * @err: print default error if failed.
6378 * Returns 0 on success -1 on field not found.
6380 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
6381 const char *name, struct tep_record *record,
6382 unsigned long long *val, int err)
6384 struct tep_format_field *field;
6389 field = tep_find_field(event, name);
6391 return get_field_val(s, field, name, record, val, err);
6395 * tep_get_common_field_val - find a common field and return its value
6396 * @s: The seq to print to on error
6397 * @event: the event that the field is for
6398 * @name: The name of the field
6399 * @record: The record with the field name.
6400 * @val: place to store the value of the field.
6401 * @err: print default error if failed.
6403 * Returns 0 on success -1 on field not found.
6405 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
6406 const char *name, struct tep_record *record,
6407 unsigned long long *val, int err)
6409 struct tep_format_field *field;
6414 field = tep_find_common_field(event, name);
6416 return get_field_val(s, field, name, record, val, err);
6420 * tep_get_any_field_val - find a any field and return its value
6421 * @s: The seq to print to on error
6422 * @event: the event that the field is for
6423 * @name: The name of the field
6424 * @record: The record with the field name.
6425 * @val: place to store the value of the field.
6426 * @err: print default error if failed.
6428 * Returns 0 on success -1 on field not found.
6430 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
6431 const char *name, struct tep_record *record,
6432 unsigned long long *val, int err)
6434 struct tep_format_field *field;
6439 field = tep_find_any_field(event, name);
6441 return get_field_val(s, field, name, record, val, err);
6445 * tep_print_num_field - print a field and a format
6446 * @s: The seq to print to
6447 * @fmt: The printf format to print the field with.
6448 * @event: the event that the field is for
6449 * @name: The name of the field
6450 * @record: The record with the field name.
6451 * @err: print default error if failed.
6453 * Returns positive value on success, negative in case of an error,
6454 * or 0 if buffer is full.
6456 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6457 struct tep_event *event, const char *name,
6458 struct tep_record *record, int err)
6460 struct tep_format_field *field = tep_find_field(event, name);
6461 unsigned long long val;
6466 if (tep_read_number_field(field, record->data, &val))
6469 return trace_seq_printf(s, fmt, val);
6473 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6478 * tep_print_func_field - print a field and a format for function pointers
6479 * @s: The seq to print to
6480 * @fmt: The printf format to print the field with.
6481 * @event: the event that the field is for
6482 * @name: The name of the field
6483 * @record: The record with the field name.
6484 * @err: print default error if failed.
6486 * Returns positive value on success, negative in case of an error,
6487 * or 0 if buffer is full.
6489 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6490 struct tep_event *event, const char *name,
6491 struct tep_record *record, int err)
6493 struct tep_format_field *field = tep_find_field(event, name);
6494 struct tep_handle *tep = event->tep;
6495 unsigned long long val;
6496 struct func_map *func;
6502 if (tep_read_number_field(field, record->data, &val))
6505 func = find_func(tep, val);
6508 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6510 sprintf(tmp, "0x%08llx", val);
6512 return trace_seq_printf(s, fmt, tmp);
6516 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6520 static void free_func_handle(struct tep_function_handler *func)
6522 struct func_params *params;
6526 while (func->params) {
6527 params = func->params;
6528 func->params = params->next;
6536 * tep_register_print_function - register a helper function
6537 * @tep: a handle to the trace event parser context
6538 * @func: the function to process the helper function
6539 * @ret_type: the return type of the helper function
6540 * @name: the name of the helper function
6541 * @parameters: A list of enum tep_func_arg_type
6543 * Some events may have helper functions in the print format arguments.
6544 * This allows a plugin to dynamically create a way to process one
6545 * of these functions.
6547 * The @parameters is a variable list of tep_func_arg_type enums that
6548 * must end with TEP_FUNC_ARG_VOID.
6550 int tep_register_print_function(struct tep_handle *tep,
6551 tep_func_handler func,
6552 enum tep_func_arg_type ret_type,
6555 struct tep_function_handler *func_handle;
6556 struct func_params **next_param;
6557 struct func_params *param;
6558 enum tep_func_arg_type type;
6562 func_handle = find_func_handler(tep, name);
6565 * This is most like caused by the users own
6566 * plugins updating the function. This overrides the
6569 pr_stat("override of function helper '%s'", name);
6570 remove_func_handler(tep, name);
6573 func_handle = calloc(1, sizeof(*func_handle));
6575 do_warning("Failed to allocate function handler");
6576 return TEP_ERRNO__MEM_ALLOC_FAILED;
6579 func_handle->ret_type = ret_type;
6580 func_handle->name = strdup(name);
6581 func_handle->func = func;
6582 if (!func_handle->name) {
6583 do_warning("Failed to allocate function name");
6585 return TEP_ERRNO__MEM_ALLOC_FAILED;
6588 next_param = &(func_handle->params);
6591 type = va_arg(ap, enum tep_func_arg_type);
6592 if (type == TEP_FUNC_ARG_VOID)
6595 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6596 do_warning("Invalid argument type %d", type);
6597 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6601 param = malloc(sizeof(*param));
6603 do_warning("Failed to allocate function param");
6604 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6610 *next_param = param;
6611 next_param = &(param->next);
6613 func_handle->nr_args++;
6617 func_handle->next = tep->func_handlers;
6618 tep->func_handlers = func_handle;
6623 free_func_handle(func_handle);
6628 * tep_unregister_print_function - unregister a helper function
6629 * @tep: a handle to the trace event parser context
6630 * @func: the function to process the helper function
6631 * @name: the name of the helper function
6633 * This function removes existing print handler for function @name.
6635 * Returns 0 if the handler was removed successully, -1 otherwise.
6637 int tep_unregister_print_function(struct tep_handle *tep,
6638 tep_func_handler func, char *name)
6640 struct tep_function_handler *func_handle;
6642 func_handle = find_func_handler(tep, name);
6643 if (func_handle && func_handle->func == func) {
6644 remove_func_handler(tep, name);
6650 static struct tep_event *search_event(struct tep_handle *tep, int id,
6651 const char *sys_name,
6652 const char *event_name)
6654 struct tep_event *event;
6658 event = tep_find_event(tep, id);
6661 if (event_name && (strcmp(event_name, event->name) != 0))
6663 if (sys_name && (strcmp(sys_name, event->system) != 0))
6666 event = tep_find_event_by_name(tep, sys_name, event_name);
6674 * tep_register_event_handler - register a way to parse an event
6675 * @tep: a handle to the trace event parser context
6676 * @id: the id of the event to register
6677 * @sys_name: the system name the event belongs to
6678 * @event_name: the name of the event
6679 * @func: the function to call to parse the event information
6680 * @context: the data to be passed to @func
6682 * This function allows a developer to override the parsing of
6683 * a given event. If for some reason the default print format
6684 * is not sufficient, this function will register a function
6685 * for an event to be used to parse the data instead.
6687 * If @id is >= 0, then it is used to find the event.
6688 * else @sys_name and @event_name are used.
6691 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
6692 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
6693 * negative TEP_ERRNO_... in case of an error
6696 int tep_register_event_handler(struct tep_handle *tep, int id,
6697 const char *sys_name, const char *event_name,
6698 tep_event_handler_func func, void *context)
6700 struct tep_event *event;
6701 struct event_handler *handle;
6703 event = search_event(tep, id, sys_name, event_name);
6707 pr_stat("overriding event (%d) %s:%s with new print handler",
6708 event->id, event->system, event->name);
6710 event->handler = func;
6711 event->context = context;
6712 return TEP_REGISTER_SUCCESS_OVERWRITE;
6715 /* Save for later use. */
6716 handle = calloc(1, sizeof(*handle));
6718 do_warning("Failed to allocate event handler");
6719 return TEP_ERRNO__MEM_ALLOC_FAILED;
6724 handle->event_name = strdup(event_name);
6726 handle->sys_name = strdup(sys_name);
6728 if ((event_name && !handle->event_name) ||
6729 (sys_name && !handle->sys_name)) {
6730 do_warning("Failed to allocate event/sys name");
6731 free((void *)handle->event_name);
6732 free((void *)handle->sys_name);
6734 return TEP_ERRNO__MEM_ALLOC_FAILED;
6737 handle->func = func;
6738 handle->next = tep->handlers;
6739 tep->handlers = handle;
6740 handle->context = context;
6742 return TEP_REGISTER_SUCCESS;
6745 static int handle_matches(struct event_handler *handler, int id,
6746 const char *sys_name, const char *event_name,
6747 tep_event_handler_func func, void *context)
6749 if (id >= 0 && id != handler->id)
6752 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6755 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6758 if (func != handler->func || context != handler->context)
6765 * tep_unregister_event_handler - unregister an existing event handler
6766 * @tep: a handle to the trace event parser context
6767 * @id: the id of the event to unregister
6768 * @sys_name: the system name the handler belongs to
6769 * @event_name: the name of the event handler
6770 * @func: the function to call to parse the event information
6771 * @context: the data to be passed to @func
6773 * This function removes existing event handler (parser).
6775 * If @id is >= 0, then it is used to find the event.
6776 * else @sys_name and @event_name are used.
6778 * Returns 0 if handler was removed successfully, -1 if event was not found.
6780 int tep_unregister_event_handler(struct tep_handle *tep, int id,
6781 const char *sys_name, const char *event_name,
6782 tep_event_handler_func func, void *context)
6784 struct tep_event *event;
6785 struct event_handler *handle;
6786 struct event_handler **next;
6788 event = search_event(tep, id, sys_name, event_name);
6792 if (event->handler == func && event->context == context) {
6793 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6794 event->id, event->system, event->name);
6796 event->handler = NULL;
6797 event->context = NULL;
6802 for (next = &tep->handlers; *next; next = &(*next)->next) {
6804 if (handle_matches(handle, id, sys_name, event_name,
6812 *next = handle->next;
6813 free_handler(handle);
6819 * tep_alloc - create a tep handle
6821 struct tep_handle *tep_alloc(void)
6823 struct tep_handle *tep = calloc(1, sizeof(*tep));
6827 tep->host_bigendian = tep_is_bigendian();
6833 void tep_ref(struct tep_handle *tep)
6838 int tep_get_ref(struct tep_handle *tep)
6841 return tep->ref_count;
6845 void tep_free_format_field(struct tep_format_field *field)
6848 if (field->alias != field->name)
6854 static void free_format_fields(struct tep_format_field *field)
6856 struct tep_format_field *next;
6860 tep_free_format_field(field);
6865 static void free_formats(struct tep_format *format)
6867 free_format_fields(format->common_fields);
6868 free_format_fields(format->fields);
6871 void tep_free_event(struct tep_event *event)
6874 free(event->system);
6876 free_formats(&event->format);
6878 free(event->print_fmt.format);
6879 free_args(event->print_fmt.args);
6885 * tep_free - free a tep handle
6886 * @tep: the tep handle to free
6888 void tep_free(struct tep_handle *tep)
6890 struct cmdline_list *cmdlist, *cmdnext;
6891 struct func_list *funclist, *funcnext;
6892 struct printk_list *printklist, *printknext;
6893 struct tep_function_handler *func_handler;
6894 struct event_handler *handle;
6900 cmdlist = tep->cmdlist;
6901 funclist = tep->funclist;
6902 printklist = tep->printklist;
6908 if (tep->cmdlines) {
6909 for (i = 0; i < tep->cmdline_count; i++)
6910 free(tep->cmdlines[i].comm);
6911 free(tep->cmdlines);
6915 cmdnext = cmdlist->next;
6916 free(cmdlist->comm);
6921 if (tep->func_map) {
6922 for (i = 0; i < (int)tep->func_count; i++) {
6923 free(tep->func_map[i].func);
6924 free(tep->func_map[i].mod);
6926 free(tep->func_map);
6930 funcnext = funclist->next;
6931 free(funclist->func);
6932 free(funclist->mod);
6934 funclist = funcnext;
6937 while (tep->func_handlers) {
6938 func_handler = tep->func_handlers;
6939 tep->func_handlers = func_handler->next;
6940 free_func_handle(func_handler);
6943 if (tep->printk_map) {
6944 for (i = 0; i < (int)tep->printk_count; i++)
6945 free(tep->printk_map[i].printk);
6946 free(tep->printk_map);
6949 while (printklist) {
6950 printknext = printklist->next;
6951 free(printklist->printk);
6953 printklist = printknext;
6956 for (i = 0; i < tep->nr_events; i++)
6957 tep_free_event(tep->events[i]);
6959 while (tep->handlers) {
6960 handle = tep->handlers;
6961 tep->handlers = handle->next;
6962 free_handler(handle);
6965 free(tep->trace_clock);
6967 free(tep->sort_events);
6968 free(tep->func_resolver);
6973 void tep_unref(struct tep_handle *tep)