1 // SPDX-License-Identifier: LGPL-2.1
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
21 #include <linux/time64.h>
23 #include <netinet/in.h>
24 #include "event-parse.h"
26 #include "event-parse-local.h"
27 #include "event-utils.h"
28 #include "trace-seq.h"
30 static const char *input_buf;
31 static unsigned long long input_buf_ptr;
32 static unsigned long long input_buf_siz;
34 static int is_flag_field;
35 static int is_symbolic_field;
37 static int show_warning = 1;
39 #define do_warning(fmt, ...) \
42 warning(fmt, ##__VA_ARGS__); \
45 #define do_warning_event(event, fmt, ...) \
51 warning("[%s:%s] " fmt, event->system, \
52 event->name, ##__VA_ARGS__); \
54 warning(fmt, ##__VA_ARGS__); \
58 * init_input_buf - init buffer for parsing
59 * @buf: buffer to parse
60 * @size: the size of the buffer
62 * Initializes the internal buffer that tep_read_token() will parse.
64 __hidden void init_input_buf(const char *buf, unsigned long long size)
71 __hidden const char *get_input_buf(void)
76 __hidden unsigned long long get_input_buf_ptr(void)
81 struct event_handler {
82 struct event_handler *next;
85 const char *event_name;
86 tep_event_handler_func func;
91 struct func_params *next;
92 enum tep_func_arg_type type;
95 struct tep_function_handler {
96 struct tep_function_handler *next;
97 enum tep_func_arg_type ret_type;
99 tep_func_handler func;
100 struct func_params *params;
104 static unsigned long long
105 process_defined_func(struct trace_seq *s, void *data, int size,
106 struct tep_event *event, struct tep_print_arg *arg);
108 static void free_func_handle(struct tep_function_handler *func);
110 void breakpoint(void)
116 static struct tep_print_arg *alloc_arg(void)
118 return calloc(1, sizeof(struct tep_print_arg));
126 static int cmdline_cmp(const void *a, const void *b)
128 const struct tep_cmdline *ca = a;
129 const struct tep_cmdline *cb = b;
131 if (ca->pid < cb->pid)
133 if (ca->pid > cb->pid)
139 /* Looking for where to place the key */
140 static int cmdline_slot_cmp(const void *a, const void *b)
142 const struct tep_cmdline *ca = a;
143 const struct tep_cmdline *cb = b;
144 const struct tep_cmdline *cb1 = cb + 1;
146 if (ca->pid < cb->pid)
149 if (ca->pid > cb->pid) {
150 if (ca->pid <= cb1->pid)
158 struct cmdline_list {
159 struct cmdline_list *next;
164 static int cmdline_init(struct tep_handle *tep)
166 struct cmdline_list *cmdlist = tep->cmdlist;
167 struct cmdline_list *item;
168 struct tep_cmdline *cmdlines;
171 cmdlines = malloc(sizeof(*cmdlines) * tep->cmdline_count);
177 cmdlines[i].pid = cmdlist->pid;
178 cmdlines[i].comm = cmdlist->comm;
181 cmdlist = cmdlist->next;
185 qsort(cmdlines, tep->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
187 tep->cmdlines = cmdlines;
193 static const char *find_cmdline(struct tep_handle *tep, int pid)
195 const struct tep_cmdline *comm;
196 struct tep_cmdline key;
201 if (!tep->cmdlines && cmdline_init(tep))
202 return "<not enough memory for cmdlines!>";
206 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
207 sizeof(*tep->cmdlines), cmdline_cmp);
215 * tep_is_pid_registered - return if a pid has a cmdline registered
216 * @tep: a handle to the trace event parser context
217 * @pid: The pid to check if it has a cmdline registered with.
219 * Returns true if the pid has a cmdline mapped to it
222 bool tep_is_pid_registered(struct tep_handle *tep, int pid)
224 const struct tep_cmdline *comm;
225 struct tep_cmdline key;
230 if (!tep->cmdlines && cmdline_init(tep))
235 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
236 sizeof(*tep->cmdlines), cmdline_cmp);
244 * If the command lines have been converted to an array, then
245 * we must add this pid. This is much slower than when cmdlines
246 * are added before the array is initialized.
248 static int add_new_comm(struct tep_handle *tep,
249 const char *comm, int pid, bool override)
251 struct tep_cmdline *cmdlines = tep->cmdlines;
252 struct tep_cmdline *cmdline;
253 struct tep_cmdline key;
260 /* avoid duplicates */
263 cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count,
264 sizeof(*tep->cmdlines), cmdline_cmp);
270 new_comm = strdup(comm);
276 cmdline->comm = new_comm;
281 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1));
286 tep->cmdlines = cmdlines;
288 key.comm = strdup(comm);
294 if (!tep->cmdline_count) {
296 tep->cmdlines[0] = key;
297 tep->cmdline_count++;
301 /* Now find where we want to store the new cmdline */
302 cmdline = bsearch(&key, tep->cmdlines, tep->cmdline_count - 1,
303 sizeof(*tep->cmdlines), cmdline_slot_cmp);
305 cnt = tep->cmdline_count;
307 /* cmdline points to the one before the spot we want */
309 cnt -= cmdline - tep->cmdlines;
312 /* The new entry is either before or after the list */
313 if (key.pid > tep->cmdlines[tep->cmdline_count - 1].pid) {
314 tep->cmdlines[tep->cmdline_count++] = key;
317 cmdline = &tep->cmdlines[0];
319 memmove(cmdline + 1, cmdline, (cnt * sizeof(*cmdline)));
322 tep->cmdline_count++;
327 static int _tep_register_comm(struct tep_handle *tep,
328 const char *comm, int pid, bool override)
330 struct cmdline_list *item;
333 return add_new_comm(tep, comm, pid, override);
335 item = malloc(sizeof(*item));
340 item->comm = strdup(comm);
342 item->comm = strdup("<...>");
348 item->next = tep->cmdlist;
351 tep->cmdline_count++;
357 * tep_register_comm - register a pid / comm mapping
358 * @tep: a handle to the trace event parser context
359 * @comm: the command line to register
360 * @pid: the pid to map the command line to
362 * This adds a mapping to search for command line names with
363 * a given pid. The comm is duplicated. If a command with the same pid
364 * already exist, -1 is returned and errno is set to EEXIST
366 int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
368 return _tep_register_comm(tep, comm, pid, false);
372 * tep_override_comm - register a pid / comm mapping
373 * @tep: a handle to the trace event parser context
374 * @comm: the command line to register
375 * @pid: the pid to map the command line to
377 * This adds a mapping to search for command line names with
378 * a given pid. The comm is duplicated. If a command with the same pid
379 * already exist, the command string is udapted with the new one
381 int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
383 if (!tep->cmdlines && cmdline_init(tep)) {
387 return _tep_register_comm(tep, comm, pid, true);
391 unsigned long long addr;
397 struct func_list *next;
398 unsigned long long addr;
403 static int func_cmp(const void *a, const void *b)
405 const struct func_map *fa = a;
406 const struct func_map *fb = b;
408 if (fa->addr < fb->addr)
410 if (fa->addr > fb->addr)
417 * We are searching for a record in between, not an exact
420 static int func_bcmp(const void *a, const void *b)
422 const struct func_map *fa = a;
423 const struct func_map *fb = b;
425 if ((fa->addr == fb->addr) ||
427 (fa->addr > fb->addr &&
428 fa->addr < (fb+1)->addr))
431 if (fa->addr < fb->addr)
437 static int func_map_init(struct tep_handle *tep)
439 struct func_list *funclist;
440 struct func_list *item;
441 struct func_map *func_map;
444 func_map = malloc(sizeof(*func_map) * (tep->func_count + 1));
448 funclist = tep->funclist;
452 func_map[i].func = funclist->func;
453 func_map[i].addr = funclist->addr;
454 func_map[i].mod = funclist->mod;
457 funclist = funclist->next;
461 qsort(func_map, tep->func_count, sizeof(*func_map), func_cmp);
464 * Add a special record at the end.
466 func_map[tep->func_count].func = NULL;
467 func_map[tep->func_count].addr = 0;
468 func_map[tep->func_count].mod = NULL;
470 tep->func_map = func_map;
471 tep->funclist = NULL;
476 static struct func_map *
477 __find_func(struct tep_handle *tep, unsigned long long addr)
479 struct func_map *func;
487 func = bsearch(&key, tep->func_map, tep->func_count,
488 sizeof(*tep->func_map), func_bcmp);
493 struct func_resolver {
494 tep_func_resolver_t *func;
500 * tep_set_function_resolver - set an alternative function resolver
501 * @tep: a handle to the trace event parser context
502 * @resolver: function to be used
503 * @priv: resolver function private state.
505 * Some tools may have already a way to resolve kernel functions, allow them to
506 * keep using it instead of duplicating all the entries inside tep->funclist.
508 int tep_set_function_resolver(struct tep_handle *tep,
509 tep_func_resolver_t *func, void *priv)
511 struct func_resolver *resolver = malloc(sizeof(*resolver));
513 if (resolver == NULL)
516 resolver->func = func;
517 resolver->priv = priv;
519 free(tep->func_resolver);
520 tep->func_resolver = resolver;
526 * tep_reset_function_resolver - reset alternative function resolver
527 * @tep: a handle to the trace event parser context
529 * Stop using whatever alternative resolver was set, use the default
532 void tep_reset_function_resolver(struct tep_handle *tep)
534 free(tep->func_resolver);
535 tep->func_resolver = NULL;
538 static struct func_map *
539 find_func(struct tep_handle *tep, unsigned long long addr)
541 struct func_map *map;
543 if (!tep->func_resolver)
544 return __find_func(tep, addr);
546 map = &tep->func_resolver->map;
549 map->func = tep->func_resolver->func(tep->func_resolver->priv,
550 &map->addr, &map->mod);
551 if (map->func == NULL)
558 * tep_find_function - find a function by a given address
559 * @tep: a handle to the trace event parser context
560 * @addr: the address to find the function with
562 * Returns a pointer to the function stored that has the given
563 * address. Note, the address does not have to be exact, it
564 * will select the function that would contain the address.
566 const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
568 struct func_map *map;
570 map = find_func(tep, addr);
578 * tep_find_function_address - find a function address by a given address
579 * @tep: a handle to the trace event parser context
580 * @addr: the address to find the function with
582 * Returns the address the function starts at. This can be used in
583 * conjunction with tep_find_function to print both the function
584 * name and the function offset.
587 tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
589 struct func_map *map;
591 map = find_func(tep, addr);
599 * tep_register_function - register a function with a given address
600 * @tep: a handle to the trace event parser context
601 * @function: the function name to register
602 * @addr: the address the function starts at
603 * @mod: the kernel module the function may be in (NULL for none)
605 * This registers a function name with an address and module.
606 * The @func passed in is duplicated.
608 int tep_register_function(struct tep_handle *tep, char *func,
609 unsigned long long addr, char *mod)
611 struct func_list *item = malloc(sizeof(*item));
616 item->next = tep->funclist;
617 item->func = strdup(func);
622 item->mod = strdup(mod);
629 tep->funclist = item;
644 * tep_print_funcs - print out the stored functions
645 * @tep: a handle to the trace event parser context
647 * This prints out the stored functions.
649 void tep_print_funcs(struct tep_handle *tep)
656 for (i = 0; i < (int)tep->func_count; i++) {
658 tep->func_map[i].addr,
659 tep->func_map[i].func);
660 if (tep->func_map[i].mod)
661 printf(" [%s]\n", tep->func_map[i].mod);
668 unsigned long long addr;
673 struct printk_list *next;
674 unsigned long long addr;
678 static int printk_cmp(const void *a, const void *b)
680 const struct printk_map *pa = a;
681 const struct printk_map *pb = b;
683 if (pa->addr < pb->addr)
685 if (pa->addr > pb->addr)
691 static int printk_map_init(struct tep_handle *tep)
693 struct printk_list *printklist;
694 struct printk_list *item;
695 struct printk_map *printk_map;
698 printk_map = malloc(sizeof(*printk_map) * (tep->printk_count + 1));
702 printklist = tep->printklist;
706 printk_map[i].printk = printklist->printk;
707 printk_map[i].addr = printklist->addr;
710 printklist = printklist->next;
714 qsort(printk_map, tep->printk_count, sizeof(*printk_map), printk_cmp);
716 tep->printk_map = printk_map;
717 tep->printklist = NULL;
722 static struct printk_map *
723 find_printk(struct tep_handle *tep, unsigned long long addr)
725 struct printk_map *printk;
726 struct printk_map key;
728 if (!tep->printk_map && printk_map_init(tep))
733 printk = bsearch(&key, tep->printk_map, tep->printk_count,
734 sizeof(*tep->printk_map), printk_cmp);
740 * tep_register_print_string - register a string by its address
741 * @tep: a handle to the trace event parser context
742 * @fmt: the string format to register
743 * @addr: the address the string was located at
745 * This registers a string by the address it was stored in the kernel.
746 * The @fmt passed in is duplicated.
748 int tep_register_print_string(struct tep_handle *tep, const char *fmt,
749 unsigned long long addr)
751 struct printk_list *item = malloc(sizeof(*item));
757 item->next = tep->printklist;
760 /* Strip off quotes and '\n' from the end */
763 item->printk = strdup(fmt);
767 p = item->printk + strlen(item->printk) - 1;
772 if (strcmp(p, "\\n") == 0)
775 tep->printklist = item;
787 * tep_print_printk - print out the stored strings
788 * @tep: a handle to the trace event parser context
790 * This prints the string formats that were stored.
792 void tep_print_printk(struct tep_handle *tep)
796 if (!tep->printk_map)
797 printk_map_init(tep);
799 for (i = 0; i < (int)tep->printk_count; i++) {
800 printf("%016llx %s\n",
801 tep->printk_map[i].addr,
802 tep->printk_map[i].printk);
806 static struct tep_event *alloc_event(void)
808 return calloc(1, sizeof(struct tep_event));
811 static int add_event(struct tep_handle *tep, struct tep_event *event)
814 struct tep_event **events = realloc(tep->events, sizeof(event) *
815 (tep->nr_events + 1));
819 tep->events = events;
821 for (i = 0; i < tep->nr_events; i++) {
822 if (tep->events[i]->id > event->id)
825 if (i < tep->nr_events)
826 memmove(&tep->events[i + 1],
828 sizeof(event) * (tep->nr_events - i));
830 tep->events[i] = event;
838 static int event_item_type(enum tep_event_type type)
841 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
843 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
849 static void free_flag_sym(struct tep_print_flag_sym *fsym)
851 struct tep_print_flag_sym *next;
862 static void free_arg(struct tep_print_arg *arg)
864 struct tep_print_arg *farg;
871 free(arg->atom.atom);
873 case TEP_PRINT_FIELD:
874 free(arg->field.name);
876 case TEP_PRINT_FLAGS:
877 free_arg(arg->flags.field);
878 free(arg->flags.delim);
879 free_flag_sym(arg->flags.flags);
881 case TEP_PRINT_SYMBOL:
882 free_arg(arg->symbol.field);
883 free_flag_sym(arg->symbol.symbols);
886 case TEP_PRINT_HEX_STR:
887 free_arg(arg->hex.field);
888 free_arg(arg->hex.size);
890 case TEP_PRINT_INT_ARRAY:
891 free_arg(arg->int_array.field);
892 free_arg(arg->int_array.count);
893 free_arg(arg->int_array.el_size);
896 free(arg->typecast.type);
897 free_arg(arg->typecast.item);
899 case TEP_PRINT_STRING:
900 case TEP_PRINT_BSTRING:
901 free(arg->string.string);
903 case TEP_PRINT_BITMASK:
904 free(arg->bitmask.bitmask);
906 case TEP_PRINT_DYNAMIC_ARRAY:
907 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
908 free(arg->dynarray.index);
912 free_arg(arg->op.left);
913 free_arg(arg->op.right);
916 while (arg->func.args) {
917 farg = arg->func.args;
918 arg->func.args = farg->next;
931 static enum tep_event_type get_type(int ch)
934 return TEP_EVENT_NEWLINE;
936 return TEP_EVENT_SPACE;
937 if (isalnum(ch) || ch == '_')
938 return TEP_EVENT_ITEM;
940 return TEP_EVENT_SQUOTE;
942 return TEP_EVENT_DQUOTE;
944 return TEP_EVENT_NONE;
945 if (ch == '(' || ch == ')' || ch == ',')
946 return TEP_EVENT_DELIM;
951 static int __read_char(void)
953 if (input_buf_ptr >= input_buf_siz)
956 return input_buf[input_buf_ptr++];
960 * peek_char - peek at the next character that will be read
962 * Returns the next character read, or -1 if end of buffer.
964 __hidden int peek_char(void)
966 if (input_buf_ptr >= input_buf_siz)
969 return input_buf[input_buf_ptr];
972 static int extend_token(char **tok, char *buf, int size)
974 char *newtok = realloc(*tok, size);
991 static enum tep_event_type force_token(const char *str, char **tok);
993 static enum tep_event_type __read_token(char **tok)
996 int ch, last_ch, quote_ch, next_ch;
999 enum tep_event_type type;
1006 return TEP_EVENT_NONE;
1008 type = get_type(ch);
1009 if (type == TEP_EVENT_NONE)
1015 case TEP_EVENT_NEWLINE:
1016 case TEP_EVENT_DELIM:
1017 if (asprintf(tok, "%c", ch) < 0)
1018 return TEP_EVENT_ERROR;
1025 next_ch = peek_char();
1026 if (next_ch == '>') {
1027 buf[i++] = __read_char();
1040 buf[i++] = __read_char();
1052 default: /* what should we do instead? */
1062 buf[i++] = __read_char();
1065 case TEP_EVENT_DQUOTE:
1066 case TEP_EVENT_SQUOTE:
1067 /* don't keep quotes */
1073 if (i == (BUFSIZ - 1)) {
1077 if (extend_token(tok, buf, tok_size) < 0)
1078 return TEP_EVENT_NONE;
1084 /* the '\' '\' will cancel itself */
1085 if (ch == '\\' && last_ch == '\\')
1087 } while (ch != quote_ch || last_ch == '\\');
1088 /* remove the last quote */
1092 * For strings (double quotes) check the next token.
1093 * If it is another string, concatinate the two.
1095 if (type == TEP_EVENT_DQUOTE) {
1096 unsigned long long save_input_buf_ptr = input_buf_ptr;
1100 } while (isspace(ch));
1103 input_buf_ptr = save_input_buf_ptr;
1108 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1109 case TEP_EVENT_ITEM:
1114 while (get_type(peek_char()) == type) {
1115 if (i == (BUFSIZ - 1)) {
1119 if (extend_token(tok, buf, tok_size) < 0)
1120 return TEP_EVENT_NONE;
1129 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1130 return TEP_EVENT_NONE;
1132 if (type == TEP_EVENT_ITEM) {
1134 * Older versions of the kernel has a bug that
1135 * creates invalid symbols and will break the mac80211
1136 * parsing. This is a work around to that bug.
1138 * See Linux kernel commit:
1139 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1141 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1144 return force_token("\"%s\" ", tok);
1145 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1148 return force_token("\" sta:%pM\" ", tok);
1149 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1152 return force_token("\" vif:%p(%d)\" ", tok);
1159 static enum tep_event_type force_token(const char *str, char **tok)
1161 const char *save_input_buf;
1162 unsigned long long save_input_buf_ptr;
1163 unsigned long long save_input_buf_siz;
1164 enum tep_event_type type;
1166 /* save off the current input pointers */
1167 save_input_buf = input_buf;
1168 save_input_buf_ptr = input_buf_ptr;
1169 save_input_buf_siz = input_buf_siz;
1171 init_input_buf(str, strlen(str));
1173 type = __read_token(tok);
1175 /* reset back to original token */
1176 input_buf = save_input_buf;
1177 input_buf_ptr = save_input_buf_ptr;
1178 input_buf_siz = save_input_buf_siz;
1184 * free_token - free a token returned by tep_read_token
1185 * @token: the token to free
1187 __hidden void free_token(char *tok)
1194 * read_token - access to utilities to use the tep parser
1195 * @tok: The token to return
1197 * This will parse tokens from the string given by
1200 * Returns the token type.
1202 __hidden enum tep_event_type read_token(char **tok)
1204 enum tep_event_type type;
1207 type = __read_token(tok);
1208 if (type != TEP_EVENT_SPACE)
1216 return TEP_EVENT_NONE;
1220 static enum tep_event_type read_token_item(char **tok)
1222 enum tep_event_type type;
1225 type = __read_token(tok);
1226 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
1234 return TEP_EVENT_NONE;
1237 static int test_type(enum tep_event_type type, enum tep_event_type expect)
1239 if (type != expect) {
1240 do_warning("Error: expected type %d but read %d",
1247 static int test_type_token(enum tep_event_type type, const char *token,
1248 enum tep_event_type expect, const char *expect_tok)
1250 if (type != expect) {
1251 do_warning("Error: expected type %d but read %d",
1256 if (strcmp(token, expect_tok) != 0) {
1257 do_warning("Error: expected '%s' but read '%s'",
1264 static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
1266 enum tep_event_type type;
1269 type = read_token(tok);
1271 type = read_token_item(tok);
1272 return test_type(type, expect);
1275 static int read_expect_type(enum tep_event_type expect, char **tok)
1277 return __read_expect_type(expect, tok, 1);
1280 static int __read_expected(enum tep_event_type expect, const char *str,
1283 enum tep_event_type type;
1288 type = read_token(&token);
1290 type = read_token_item(&token);
1292 ret = test_type_token(type, token, expect, str);
1299 static int read_expected(enum tep_event_type expect, const char *str)
1301 return __read_expected(expect, str, 1);
1304 static int read_expected_item(enum tep_event_type expect, const char *str)
1306 return __read_expected(expect, str, 0);
1309 static char *event_read_name(void)
1313 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
1316 if (read_expected(TEP_EVENT_OP, ":") < 0)
1319 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1329 static int event_read_id(void)
1334 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
1337 if (read_expected(TEP_EVENT_OP, ":") < 0)
1340 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1343 id = strtoul(token, NULL, 0);
1352 static int field_is_string(struct tep_format_field *field)
1354 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
1355 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1356 strstr(field->type, "s8")))
1362 static int field_is_dynamic(struct tep_format_field *field)
1364 if (strncmp(field->type, "__data_loc", 10) == 0)
1370 static int field_is_relative_dynamic(struct tep_format_field *field)
1372 if (strncmp(field->type, "__rel_loc", 9) == 0)
1378 static int field_is_long(struct tep_format_field *field)
1380 /* includes long long */
1381 if (strstr(field->type, "long"))
1387 static unsigned int type_size(const char *name)
1389 /* This covers all TEP_FIELD_IS_STRING types. */
1407 for (i = 0; table[i].type; i++) {
1408 if (!strcmp(table[i].type, name))
1409 return table[i].size;
1415 static int append(char **buf, const char *delim, const char *str)
1419 new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1);
1422 strcat(new_buf, delim);
1423 strcat(new_buf, str);
1428 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1430 struct tep_format_field *field = NULL;
1431 enum tep_event_type type;
1439 unsigned int size_dynamic = 0;
1441 type = read_token(&token);
1442 if (type == TEP_EVENT_NEWLINE) {
1449 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1453 type = read_token(&token);
1455 * The ftrace fields may still use the "special" name.
1458 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1459 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1461 type = read_token(&token);
1464 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1468 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1473 field = calloc(1, sizeof(*field));
1477 field->event = event;
1479 /* read the rest of the type */
1481 type = read_token(&token);
1482 if (type == TEP_EVENT_ITEM ||
1483 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1485 * Some of the ftrace fields are broken and have
1486 * an illegal "." in them.
1488 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1489 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1491 if (strcmp(token, "*") == 0)
1492 field->flags |= TEP_FIELD_IS_POINTER;
1495 ret = append(&field->type, delim, last_token);
1500 field->type = last_token;
1506 /* Handle __attribute__((user)) */
1507 if ((type == TEP_EVENT_DELIM) &&
1508 strcmp("__attribute__", last_token) == 0 &&
1513 ret = append(&field->type, " ", last_token);
1514 ret |= append(&field->type, "", "(");
1519 while ((type = read_token(&token)) != TEP_EVENT_NONE) {
1520 if (type == TEP_EVENT_DELIM) {
1521 if (token[0] == '(')
1523 else if (token[0] == ')')
1527 ret = append(&field->type, "", token);
1530 ret = append(&field->type, delim, token);
1544 do_warning_event(event, "%s: no type found", __func__);
1547 field->name = field->alias = last_token;
1549 if (test_type(type, TEP_EVENT_OP))
1552 if (strcmp(token, "[") == 0) {
1553 enum tep_event_type last_type = type;
1554 char *brackets = token;
1556 field->flags |= TEP_FIELD_IS_ARRAY;
1558 type = read_token(&token);
1560 if (type == TEP_EVENT_ITEM)
1561 field->arraylen = strtoul(token, NULL, 0);
1563 field->arraylen = 0;
1565 while (strcmp(token, "]") != 0) {
1568 if (last_type == TEP_EVENT_ITEM &&
1569 type == TEP_EVENT_ITEM)
1576 ret = append(&brackets, delim, token);
1581 /* We only care about the last token */
1582 field->arraylen = strtoul(token, NULL, 0);
1584 type = read_token(&token);
1585 if (type == TEP_EVENT_NONE) {
1587 do_warning_event(event, "failed to find token");
1594 ret = append(&brackets, "", "]");
1600 /* add brackets to type */
1602 type = read_token(&token);
1604 * If the next token is not an OP, then it is of
1605 * the format: type [] item;
1607 if (type == TEP_EVENT_ITEM) {
1608 ret = append(&field->type, " ", field->name);
1613 ret = append(&field->type, "", brackets);
1615 size_dynamic = type_size(field->name);
1616 free_token(field->name);
1617 field->name = field->alias = token;
1618 type = read_token(&token);
1620 ret = append(&field->type, "", brackets);
1629 if (field_is_string(field))
1630 field->flags |= TEP_FIELD_IS_STRING;
1631 if (field_is_dynamic(field))
1632 field->flags |= TEP_FIELD_IS_DYNAMIC;
1633 if (field_is_relative_dynamic(field))
1634 field->flags |= TEP_FIELD_IS_DYNAMIC | TEP_FIELD_IS_RELATIVE;
1635 if (field_is_long(field))
1636 field->flags |= TEP_FIELD_IS_LONG;
1638 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1642 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1645 if (read_expected(TEP_EVENT_OP, ":") < 0)
1648 if (read_expect_type(TEP_EVENT_ITEM, &token))
1650 field->offset = strtoul(token, NULL, 0);
1653 if (read_expected(TEP_EVENT_OP, ";") < 0)
1656 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1659 if (read_expected(TEP_EVENT_OP, ":") < 0)
1662 if (read_expect_type(TEP_EVENT_ITEM, &token))
1664 field->size = strtoul(token, NULL, 0);
1667 if (read_expected(TEP_EVENT_OP, ";") < 0)
1670 type = read_token(&token);
1671 if (type != TEP_EVENT_NEWLINE) {
1672 /* newer versions of the kernel have a "signed" type */
1673 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1678 if (read_expected(TEP_EVENT_OP, ":") < 0)
1681 if (read_expect_type(TEP_EVENT_ITEM, &token))
1684 if (strtoul(token, NULL, 0))
1685 field->flags |= TEP_FIELD_IS_SIGNED;
1688 if (read_expected(TEP_EVENT_OP, ";") < 0)
1691 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1697 if (field->flags & TEP_FIELD_IS_ARRAY) {
1698 if (field->arraylen)
1699 field->elementsize = field->size / field->arraylen;
1700 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1701 field->elementsize = size_dynamic;
1702 else if (field->flags & TEP_FIELD_IS_STRING)
1703 field->elementsize = 1;
1704 else if (field->flags & TEP_FIELD_IS_LONG)
1705 field->elementsize = event->tep ?
1706 event->tep->long_size :
1709 field->elementsize = field->size;
1712 fields = &field->next;
1729 static int event_read_format(struct tep_event *event)
1734 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1737 if (read_expected(TEP_EVENT_OP, ":") < 0)
1740 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1744 ret = event_read_fields(event, &event->format.common_fields);
1747 event->format.nr_common = ret;
1749 ret = event_read_fields(event, &event->format.fields);
1752 event->format.nr_fields = ret;
1761 static enum tep_event_type
1762 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1763 char **tok, enum tep_event_type type);
1765 static enum tep_event_type
1766 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1768 enum tep_event_type type;
1771 type = read_token(&token);
1774 return process_arg_token(event, arg, tok, type);
1777 static enum tep_event_type
1778 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1781 * For __print_symbolic() and __print_flags, we need to completely
1782 * evaluate the first argument, which defines what to print next.
1784 static enum tep_event_type
1785 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1787 enum tep_event_type type;
1789 type = process_arg(event, arg, tok);
1791 while (type == TEP_EVENT_OP) {
1792 type = process_op(event, arg, tok);
1798 static enum tep_event_type
1799 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1801 struct tep_print_arg *arg, *left, *right;
1802 enum tep_event_type type;
1807 right = alloc_arg();
1809 if (!arg || !left || !right) {
1810 do_warning_event(event, "%s: not enough memory!", __func__);
1811 /* arg will be freed at out_free */
1817 arg->type = TEP_PRINT_OP;
1818 arg->op.left = left;
1819 arg->op.right = right;
1822 type = process_arg(event, left, &token);
1825 if (type == TEP_EVENT_ERROR)
1828 /* Handle other operations in the arguments */
1829 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1830 type = process_op(event, left, &token);
1834 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1839 type = process_arg(event, right, &token);
1841 top->op.right = arg;
1847 /* Top may point to itself */
1848 top->op.right = NULL;
1851 return TEP_EVENT_ERROR;
1854 static enum tep_event_type
1855 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1857 struct tep_print_arg *arg;
1858 enum tep_event_type type;
1863 do_warning_event(event, "%s: not enough memory!", __func__);
1864 /* '*tok' is set to top->op.op. No need to free. */
1866 return TEP_EVENT_ERROR;
1870 type = process_arg(event, arg, &token);
1871 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1874 top->op.right = arg;
1877 type = read_token_item(&token);
1885 return TEP_EVENT_ERROR;
1888 static int get_op_prio(char *op)
1902 /* '>>' and '<<' are 8 */
1906 /* '==' and '!=' are 10 */
1916 do_warning("unknown op '%c'", op[0]);
1920 if (strcmp(op, "++") == 0 ||
1921 strcmp(op, "--") == 0) {
1923 } else if (strcmp(op, ">>") == 0 ||
1924 strcmp(op, "<<") == 0) {
1926 } else if (strcmp(op, ">=") == 0 ||
1927 strcmp(op, "<=") == 0) {
1929 } else if (strcmp(op, "==") == 0 ||
1930 strcmp(op, "!=") == 0) {
1932 } else if (strcmp(op, "&&") == 0) {
1934 } else if (strcmp(op, "||") == 0) {
1937 do_warning("unknown op '%s'", op);
1943 static int set_op_prio(struct tep_print_arg *arg)
1946 /* single ops are the greatest */
1947 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1950 arg->op.prio = get_op_prio(arg->op.op);
1952 return arg->op.prio;
1955 /* Note, *tok does not get freed, but will most likely be saved */
1956 static enum tep_event_type
1957 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1959 struct tep_print_arg *left, *right = NULL;
1960 enum tep_event_type type;
1963 /* the op is passed in via tok */
1966 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1967 /* handle single op */
1969 do_warning_event(event, "bad op token %s", token);
1979 do_warning_event(event, "bad op token %s", token);
1984 /* make an empty left */
1989 left->type = TEP_PRINT_NULL;
1990 arg->op.left = left;
1992 right = alloc_arg();
1996 arg->op.right = right;
1998 /* do not free the token, it belongs to an op */
2000 type = process_arg(event, right, tok);
2002 } else if (strcmp(token, "?") == 0) {
2008 /* copy the top arg to the left */
2011 arg->type = TEP_PRINT_OP;
2013 arg->op.left = left;
2016 /* it will set arg->op.right */
2017 type = process_cond(event, arg, tok);
2019 } else if (strcmp(token, ">>") == 0 ||
2020 strcmp(token, "<<") == 0 ||
2021 strcmp(token, "&") == 0 ||
2022 strcmp(token, "|") == 0 ||
2023 strcmp(token, "&&") == 0 ||
2024 strcmp(token, "||") == 0 ||
2025 strcmp(token, "-") == 0 ||
2026 strcmp(token, "+") == 0 ||
2027 strcmp(token, "*") == 0 ||
2028 strcmp(token, "^") == 0 ||
2029 strcmp(token, "/") == 0 ||
2030 strcmp(token, "%") == 0 ||
2031 strcmp(token, "<") == 0 ||
2032 strcmp(token, ">") == 0 ||
2033 strcmp(token, "<=") == 0 ||
2034 strcmp(token, ">=") == 0 ||
2035 strcmp(token, "==") == 0 ||
2036 strcmp(token, "!=") == 0) {
2042 /* copy the top arg to the left */
2045 arg->type = TEP_PRINT_OP;
2047 arg->op.left = left;
2048 arg->op.right = NULL;
2050 if (set_op_prio(arg) == -1) {
2051 event->flags |= TEP_EVENT_FL_FAILED;
2052 /* arg->op.op (= token) will be freed at out_free */
2057 type = read_token_item(&token);
2060 /* could just be a type pointer */
2061 if ((strcmp(arg->op.op, "*") == 0) &&
2062 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2065 if (left->type != TEP_PRINT_ATOM) {
2066 do_warning_event(event, "bad pointer type");
2069 ret = append(&left->atom.atom, " ", "*");
2080 right = alloc_arg();
2084 type = process_arg_token(event, right, tok, type);
2085 if (type == TEP_EVENT_ERROR) {
2087 /* token was freed in process_arg_token() via *tok */
2092 if (right->type == TEP_PRINT_OP &&
2093 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2094 struct tep_print_arg tmp;
2096 /* rotate ops according to the priority */
2097 arg->op.right = right->op.left;
2103 arg->op.left = right;
2105 arg->op.right = right;
2108 } else if (strcmp(token, "[") == 0) {
2116 arg->type = TEP_PRINT_OP;
2118 arg->op.left = left;
2122 /* it will set arg->op.right */
2123 type = process_array(event, arg, tok);
2126 do_warning_event(event, "unknown op '%s'", token);
2127 event->flags |= TEP_EVENT_FL_FAILED;
2128 /* the arg is now the left side */
2132 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2135 /* higher prios need to be closer to the root */
2136 prio = get_op_prio(*tok);
2138 if (prio > arg->op.prio)
2139 return process_op(event, arg, tok);
2141 return process_op(event, right, tok);
2147 do_warning_event(event, "%s: not enough memory!", __func__);
2151 return TEP_EVENT_ERROR;
2154 static enum tep_event_type
2155 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2158 enum tep_event_type type;
2162 if (read_expected(TEP_EVENT_OP, "->") < 0)
2165 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2169 arg->type = TEP_PRINT_FIELD;
2170 arg->field.name = field;
2172 if (is_flag_field) {
2173 arg->field.field = tep_find_any_field(event, arg->field.name);
2174 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2176 } else if (is_symbolic_field) {
2177 arg->field.field = tep_find_any_field(event, arg->field.name);
2178 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2179 is_symbolic_field = 0;
2182 type = read_token(&token);
2191 return TEP_EVENT_ERROR;
2194 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2195 struct tep_print_arg **print_arg)
2197 struct tep_print_arg *field;
2198 enum tep_event_type type;
2202 field = alloc_arg();
2204 do_warning_event(event, "%s: not enough memory!", __func__);
2209 type = process_arg(event, field, &token);
2211 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2215 goto out_free_token;
2226 static char *arg_eval (struct tep_print_arg *arg);
2228 static unsigned long long
2229 eval_type_str(unsigned long long val, const char *type, int pointer)
2239 if (type[len-1] != '*') {
2240 do_warning("pointer expected with non pointer type");
2246 do_warning("%s: not enough memory!", __func__);
2249 memcpy(ref, type, len);
2251 /* chop off the " *" */
2254 val = eval_type_str(val, ref, 0);
2259 /* check if this is a pointer */
2260 if (type[len - 1] == '*')
2263 /* Try to figure out the arg size*/
2264 if (strncmp(type, "struct", 6) == 0)
2268 if (strcmp(type, "u8") == 0)
2271 if (strcmp(type, "u16") == 0)
2272 return val & 0xffff;
2274 if (strcmp(type, "u32") == 0)
2275 return val & 0xffffffff;
2277 if (strcmp(type, "u64") == 0 ||
2278 strcmp(type, "s64") == 0)
2281 if (strcmp(type, "s8") == 0)
2282 return (unsigned long long)(char)val & 0xff;
2284 if (strcmp(type, "s16") == 0)
2285 return (unsigned long long)(short)val & 0xffff;
2287 if (strcmp(type, "s32") == 0)
2288 return (unsigned long long)(int)val & 0xffffffff;
2290 if (strncmp(type, "unsigned ", 9) == 0) {
2295 if (strcmp(type, "char") == 0) {
2297 return (unsigned long long)(char)val & 0xff;
2302 if (strcmp(type, "short") == 0) {
2304 return (unsigned long long)(short)val & 0xffff;
2306 return val & 0xffff;
2309 if (strcmp(type, "int") == 0) {
2311 return (unsigned long long)(int)val & 0xffffffff;
2313 return val & 0xffffffff;
2320 * Try to figure out the type.
2322 static unsigned long long
2323 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2325 if (arg->type != TEP_PRINT_TYPE) {
2326 do_warning("expected type argument");
2330 return eval_type_str(val, arg->typecast.type, pointer);
2333 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2335 long long left, right;
2338 switch (arg->type) {
2339 case TEP_PRINT_ATOM:
2340 *val = strtoll(arg->atom.atom, NULL, 0);
2342 case TEP_PRINT_TYPE:
2343 ret = arg_num_eval(arg->typecast.item, val);
2346 *val = eval_type(*val, arg, 0);
2349 switch (arg->op.op[0]) {
2351 ret = arg_num_eval(arg->op.left, &left);
2354 ret = arg_num_eval(arg->op.right, &right);
2358 *val = left || right;
2360 *val = left | right;
2363 ret = arg_num_eval(arg->op.left, &left);
2366 ret = arg_num_eval(arg->op.right, &right);
2370 *val = left && right;
2372 *val = left & right;
2375 ret = arg_num_eval(arg->op.left, &left);
2378 ret = arg_num_eval(arg->op.right, &right);
2381 switch (arg->op.op[1]) {
2383 *val = left < right;
2386 *val = left << right;
2389 *val = left <= right;
2392 do_warning("unknown op '%s'", arg->op.op);
2397 ret = arg_num_eval(arg->op.left, &left);
2400 ret = arg_num_eval(arg->op.right, &right);
2403 switch (arg->op.op[1]) {
2405 *val = left > right;
2408 *val = left >> right;
2411 *val = left >= right;
2414 do_warning("unknown op '%s'", arg->op.op);
2419 ret = arg_num_eval(arg->op.left, &left);
2422 ret = arg_num_eval(arg->op.right, &right);
2426 if (arg->op.op[1] != '=') {
2427 do_warning("unknown op '%s'", arg->op.op);
2430 *val = left == right;
2433 ret = arg_num_eval(arg->op.left, &left);
2436 ret = arg_num_eval(arg->op.right, &right);
2440 switch (arg->op.op[1]) {
2442 *val = left != right;
2445 do_warning("unknown op '%s'", arg->op.op);
2450 /* check for negative */
2451 if (arg->op.left->type == TEP_PRINT_NULL)
2454 ret = arg_num_eval(arg->op.left, &left);
2457 ret = arg_num_eval(arg->op.right, &right);
2460 *val = left - right;
2463 if (arg->op.left->type == TEP_PRINT_NULL)
2466 ret = arg_num_eval(arg->op.left, &left);
2469 ret = arg_num_eval(arg->op.right, &right);
2472 *val = left + right;
2475 ret = arg_num_eval(arg->op.right, &right);
2481 do_warning("unknown op '%s'", arg->op.op);
2486 case TEP_PRINT_NULL:
2487 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2488 case TEP_PRINT_STRING:
2489 case TEP_PRINT_BSTRING:
2490 case TEP_PRINT_BITMASK:
2492 do_warning("invalid eval type %d", arg->type);
2499 static char *arg_eval (struct tep_print_arg *arg)
2502 static char buf[24];
2504 switch (arg->type) {
2505 case TEP_PRINT_ATOM:
2506 return arg->atom.atom;
2507 case TEP_PRINT_TYPE:
2508 return arg_eval(arg->typecast.item);
2510 if (!arg_num_eval(arg, &val))
2512 sprintf(buf, "%lld", val);
2515 case TEP_PRINT_NULL:
2516 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2517 case TEP_PRINT_STRING:
2518 case TEP_PRINT_BSTRING:
2519 case TEP_PRINT_BITMASK:
2521 do_warning("invalid eval type %d", arg->type);
2528 static enum tep_event_type
2529 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2531 enum tep_event_type type;
2532 struct tep_print_arg *arg = NULL;
2533 struct tep_print_flag_sym *field;
2539 type = read_token_item(&token);
2540 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2548 type = process_arg(event, arg, &token);
2550 if (type == TEP_EVENT_OP)
2551 type = process_op(event, arg, &token);
2553 if (type == TEP_EVENT_ERROR)
2556 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2559 field = calloc(1, sizeof(*field));
2563 value = arg_eval(arg);
2565 goto out_free_field;
2566 field->value = strdup(value);
2567 if (field->value == NULL)
2568 goto out_free_field;
2576 type = process_arg(event, arg, &token);
2577 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2578 goto out_free_field;
2580 value = arg_eval(arg);
2582 goto out_free_field;
2583 field->str = strdup(value);
2584 if (field->str == NULL)
2585 goto out_free_field;
2590 list = &field->next;
2593 type = read_token_item(&token);
2594 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2600 free_flag_sym(field);
2606 return TEP_EVENT_ERROR;
2609 static enum tep_event_type
2610 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2612 struct tep_print_arg *field;
2613 enum tep_event_type type;
2616 memset(arg, 0, sizeof(*arg));
2617 arg->type = TEP_PRINT_FLAGS;
2619 field = alloc_arg();
2621 do_warning_event(event, "%s: not enough memory!", __func__);
2625 type = process_field_arg(event, field, &token);
2627 /* Handle operations in the first argument */
2628 while (type == TEP_EVENT_OP)
2629 type = process_op(event, field, &token);
2631 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2632 goto out_free_field;
2635 arg->flags.field = field;
2637 type = read_token_item(&token);
2638 if (event_item_type(type)) {
2639 arg->flags.delim = token;
2640 type = read_token_item(&token);
2643 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2646 type = process_fields(event, &arg->flags.flags, &token);
2647 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2651 type = read_token_item(tok);
2659 return TEP_EVENT_ERROR;
2662 static enum tep_event_type
2663 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2665 struct tep_print_arg *field;
2666 enum tep_event_type type;
2669 memset(arg, 0, sizeof(*arg));
2670 arg->type = TEP_PRINT_SYMBOL;
2672 field = alloc_arg();
2674 do_warning_event(event, "%s: not enough memory!", __func__);
2678 type = process_field_arg(event, field, &token);
2680 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2681 goto out_free_field;
2683 arg->symbol.field = field;
2685 type = process_fields(event, &arg->symbol.symbols, &token);
2686 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2690 type = read_token_item(tok);
2698 return TEP_EVENT_ERROR;
2701 static enum tep_event_type
2702 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2703 char **tok, enum tep_print_arg_type type)
2705 memset(arg, 0, sizeof(*arg));
2708 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2711 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2714 return read_token_item(tok);
2717 free_arg(arg->hex.field);
2718 arg->hex.field = NULL;
2721 return TEP_EVENT_ERROR;
2724 static enum tep_event_type
2725 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2727 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2730 static enum tep_event_type
2731 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2734 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2737 static enum tep_event_type
2738 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2740 memset(arg, 0, sizeof(*arg));
2741 arg->type = TEP_PRINT_INT_ARRAY;
2743 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2746 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2749 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2752 return read_token_item(tok);
2755 free_arg(arg->int_array.count);
2756 arg->int_array.count = NULL;
2758 free_arg(arg->int_array.field);
2759 arg->int_array.field = NULL;
2762 return TEP_EVENT_ERROR;
2765 static enum tep_event_type
2766 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2768 struct tep_format_field *field;
2769 enum tep_event_type type;
2772 memset(arg, 0, sizeof(*arg));
2773 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2776 * The item within the parenthesis is another field that holds
2777 * the index into where the array starts.
2779 type = read_token(&token);
2781 if (type != TEP_EVENT_ITEM)
2784 /* Find the field */
2786 field = tep_find_field(event, token);
2790 arg->dynarray.field = field;
2791 arg->dynarray.index = 0;
2793 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2797 type = read_token_item(&token);
2799 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2805 do_warning_event(event, "%s: not enough memory!", __func__);
2807 return TEP_EVENT_ERROR;
2810 type = process_arg(event, arg, &token);
2811 if (type == TEP_EVENT_ERROR)
2814 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2818 type = read_token_item(tok);
2826 return TEP_EVENT_ERROR;
2829 static enum tep_event_type
2830 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2833 struct tep_format_field *field;
2834 enum tep_event_type type;
2837 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2840 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2842 /* Find the field */
2843 field = tep_find_field(event, token);
2847 arg->dynarray.field = field;
2848 arg->dynarray.index = 0;
2850 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2854 type = read_token(&token);
2863 return TEP_EVENT_ERROR;
2866 static enum tep_event_type
2867 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2869 struct tep_print_arg *item_arg;
2870 enum tep_event_type type;
2873 type = process_arg(event, arg, &token);
2875 if (type == TEP_EVENT_ERROR)
2878 if (type == TEP_EVENT_OP)
2879 type = process_op(event, arg, &token);
2881 if (type == TEP_EVENT_ERROR)
2884 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2888 type = read_token_item(&token);
2891 * If the next token is an item or another open paren, then
2892 * this was a typecast.
2894 if (event_item_type(type) ||
2895 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2897 /* make this a typecast and contine */
2899 /* prevous must be an atom */
2900 if (arg->type != TEP_PRINT_ATOM) {
2901 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2905 item_arg = alloc_arg();
2907 do_warning_event(event, "%s: not enough memory!",
2912 arg->type = TEP_PRINT_TYPE;
2913 arg->typecast.type = arg->atom.atom;
2914 arg->typecast.item = item_arg;
2915 type = process_arg_token(event, item_arg, &token, type);
2925 return TEP_EVENT_ERROR;
2929 static enum tep_event_type
2930 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2933 enum tep_event_type type;
2936 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2939 arg->type = TEP_PRINT_STRING;
2940 arg->string.string = token;
2941 arg->string.field = NULL;
2943 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2946 type = read_token(&token);
2955 return TEP_EVENT_ERROR;
2958 static enum tep_event_type
2959 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2962 enum tep_event_type type;
2965 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2968 arg->type = TEP_PRINT_BITMASK;
2969 arg->bitmask.bitmask = token;
2970 arg->bitmask.field = NULL;
2972 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2975 type = read_token(&token);
2984 return TEP_EVENT_ERROR;
2987 static struct tep_function_handler *
2988 find_func_handler(struct tep_handle *tep, char *func_name)
2990 struct tep_function_handler *func;
2995 for (func = tep->func_handlers; func; func = func->next) {
2996 if (strcmp(func->name, func_name) == 0)
3003 static void remove_func_handler(struct tep_handle *tep, char *func_name)
3005 struct tep_function_handler *func;
3006 struct tep_function_handler **next;
3008 next = &tep->func_handlers;
3009 while ((func = *next)) {
3010 if (strcmp(func->name, func_name) == 0) {
3012 free_func_handle(func);
3019 static enum tep_event_type
3020 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
3021 struct tep_print_arg *arg, char **tok)
3023 struct tep_print_arg **next_arg;
3024 struct tep_print_arg *farg;
3025 enum tep_event_type type;
3029 arg->type = TEP_PRINT_FUNC;
3030 arg->func.func = func;
3034 next_arg = &(arg->func.args);
3035 for (i = 0; i < func->nr_args; i++) {
3038 do_warning_event(event, "%s: not enough memory!",
3040 return TEP_EVENT_ERROR;
3043 type = process_arg(event, farg, &token);
3044 if (i < (func->nr_args - 1)) {
3045 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3046 do_warning_event(event,
3047 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3048 func->name, func->nr_args,
3049 event->name, i + 1);
3053 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3054 do_warning_event(event,
3055 "Error: function '%s()' only expects %d arguments but event %s has more",
3056 func->name, func->nr_args, event->name);
3062 next_arg = &(farg->next);
3066 type = read_token(&token);
3074 return TEP_EVENT_ERROR;
3077 static enum tep_event_type
3078 process_builtin_expect(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3080 enum tep_event_type type;
3083 /* Handle __builtin_expect( cond, #) */
3084 type = process_arg(event, arg, &token);
3086 if (type != TEP_EVENT_DELIM || token[0] != ',')
3091 /* We don't care what the second parameter is of the __builtin_expect() */
3092 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
3095 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
3099 type = read_token_item(tok);
3105 return TEP_EVENT_ERROR;
3108 static enum tep_event_type
3109 process_function(struct tep_event *event, struct tep_print_arg *arg,
3110 char *token, char **tok)
3112 struct tep_function_handler *func;
3114 if (strcmp(token, "__print_flags") == 0) {
3117 return process_flags(event, arg, tok);
3119 if (strcmp(token, "__print_symbolic") == 0) {
3121 is_symbolic_field = 1;
3122 return process_symbols(event, arg, tok);
3124 if (strcmp(token, "__print_hex") == 0) {
3126 return process_hex(event, arg, tok);
3128 if (strcmp(token, "__print_hex_str") == 0) {
3130 return process_hex_str(event, arg, tok);
3132 if (strcmp(token, "__print_array") == 0) {
3134 return process_int_array(event, arg, tok);
3136 if (strcmp(token, "__get_str") == 0 ||
3137 strcmp(token, "__get_rel_str") == 0) {
3139 return process_str(event, arg, tok);
3141 if (strcmp(token, "__get_bitmask") == 0 ||
3142 strcmp(token, "__get_rel_bitmask") == 0) {
3144 return process_bitmask(event, arg, tok);
3146 if (strcmp(token, "__get_dynamic_array") == 0 ||
3147 strcmp(token, "__get_rel_dynamic_array") == 0) {
3149 return process_dynamic_array(event, arg, tok);
3151 if (strcmp(token, "__get_dynamic_array_len") == 0 ||
3152 strcmp(token, "__get_rel_dynamic_array_len") == 0) {
3154 return process_dynamic_array_len(event, arg, tok);
3156 if (strcmp(token, "__builtin_expect") == 0) {
3158 return process_builtin_expect(event, arg, tok);
3161 func = find_func_handler(event->tep, token);
3164 return process_func_handler(event, func, arg, tok);
3167 do_warning_event(event, "function %s not defined", token);
3169 return TEP_EVENT_ERROR;
3172 static enum tep_event_type
3173 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3174 char **tok, enum tep_event_type type)
3182 case TEP_EVENT_ITEM:
3183 if (strcmp(token, "REC") == 0) {
3185 type = process_entry(event, arg, &token);
3189 /* test the next token */
3190 type = read_token_item(&token);
3193 * If the next token is a parenthesis, then this
3196 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3199 /* this will free atom. */
3200 type = process_function(event, arg, atom, &token);
3203 /* atoms can be more than one token long */
3204 while (type == TEP_EVENT_ITEM) {
3207 ret = append(&atom, " ", token);
3212 return TEP_EVENT_ERROR;
3215 type = read_token_item(&token);
3218 arg->type = TEP_PRINT_ATOM;
3219 arg->atom.atom = atom;
3222 case TEP_EVENT_DQUOTE:
3223 case TEP_EVENT_SQUOTE:
3224 arg->type = TEP_PRINT_ATOM;
3225 arg->atom.atom = token;
3226 type = read_token_item(&token);
3228 case TEP_EVENT_DELIM:
3229 if (strcmp(token, "(") == 0) {
3231 type = process_paren(event, arg, &token);
3235 /* handle single ops */
3236 arg->type = TEP_PRINT_OP;
3238 arg->op.left = NULL;
3239 type = process_op(event, arg, &token);
3241 /* On error, the op is freed */
3242 if (type == TEP_EVENT_ERROR)
3245 /* return error type if errored */
3248 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3250 do_warning_event(event, "unexpected type %d", type);
3251 return TEP_EVENT_ERROR;
3258 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3260 enum tep_event_type type = TEP_EVENT_ERROR;
3261 struct tep_print_arg *arg;
3266 if (type == TEP_EVENT_NEWLINE) {
3267 type = read_token_item(&token);
3273 do_warning_event(event, "%s: not enough memory!",
3278 type = process_arg(event, arg, &token);
3280 if (type == TEP_EVENT_ERROR) {
3289 if (type == TEP_EVENT_OP) {
3290 type = process_op(event, arg, &token);
3292 if (type == TEP_EVENT_ERROR) {
3301 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3308 } while (type != TEP_EVENT_NONE);
3310 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3316 static int event_read_print(struct tep_event *event)
3318 enum tep_event_type type;
3322 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3325 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3328 if (read_expected(TEP_EVENT_OP, ":") < 0)
3331 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3335 event->print_fmt.format = token;
3336 event->print_fmt.args = NULL;
3338 /* ok to have no arg */
3339 type = read_token_item(&token);
3341 if (type == TEP_EVENT_NONE)
3344 /* Handle concatenation of print lines */
3345 if (type == TEP_EVENT_DQUOTE) {
3348 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3351 free_token(event->print_fmt.format);
3352 event->print_fmt.format = NULL;
3357 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3362 ret = event_read_print_args(event, &event->print_fmt.args);
3374 * tep_find_common_field - return a common field by event
3375 * @event: handle for the event
3376 * @name: the name of the common field to return
3378 * Returns a common field from the event by the given @name.
3379 * This only searches the common fields and not all field.
3381 struct tep_format_field *
3382 tep_find_common_field(struct tep_event *event, const char *name)
3384 struct tep_format_field *format;
3386 for (format = event->format.common_fields;
3387 format; format = format->next) {
3388 if (strcmp(format->name, name) == 0)
3396 * tep_find_field - find a non-common field
3397 * @event: handle for the event
3398 * @name: the name of the non-common field
3400 * Returns a non-common field by the given @name.
3401 * This does not search common fields.
3403 struct tep_format_field *
3404 tep_find_field(struct tep_event *event, const char *name)
3406 struct tep_format_field *format;
3408 for (format = event->format.fields;
3409 format; format = format->next) {
3410 if (strcmp(format->name, name) == 0)
3418 * tep_find_any_field - find any field by name
3419 * @event: handle for the event
3420 * @name: the name of the field
3422 * Returns a field by the given @name.
3423 * This searches the common field names first, then
3424 * the non-common ones if a common one was not found.
3426 struct tep_format_field *
3427 tep_find_any_field(struct tep_event *event, const char *name)
3429 struct tep_format_field *format;
3431 format = tep_find_common_field(event, name);
3434 return tep_find_field(event, name);
3438 * tep_read_number - read a number from data
3439 * @tep: a handle to the trace event parser context
3440 * @ptr: the raw data
3441 * @size: the size of the data that holds the number
3443 * Returns the number (converted to host) from the
3446 unsigned long long tep_read_number(struct tep_handle *tep,
3447 const void *ptr, int size)
3449 unsigned long long val;
3453 return *(unsigned char *)ptr;
3455 return data2host2(tep, *(unsigned short *)ptr);
3457 return data2host4(tep, *(unsigned int *)ptr);
3459 memcpy(&val, (ptr), sizeof(unsigned long long));
3460 return data2host8(tep, val);
3468 * tep_read_number_field - read a number from data
3469 * @field: a handle to the field
3470 * @data: the raw data to read
3471 * @value: the value to place the number in
3473 * Reads raw data according to a field offset and size,
3474 * and translates it into @value.
3476 * Returns 0 on success, -1 otherwise.
3478 int tep_read_number_field(struct tep_format_field *field, const void *data,
3479 unsigned long long *value)
3483 switch (field->size) {
3488 *value = tep_read_number(field->event->tep,
3489 data + field->offset, field->size);
3496 static int get_common_info(struct tep_handle *tep,
3497 const char *type, int *offset, int *size)
3499 struct tep_event *event;
3500 struct tep_format_field *field;
3503 * All events should have the same common elements.
3504 * Pick any event to find where the type is;
3507 do_warning("no event_list!");
3511 event = tep->events[0];
3512 field = tep_find_common_field(event, type);
3516 *offset = field->offset;
3517 *size = field->size;
3522 static int __parse_common(struct tep_handle *tep, void *data,
3523 int *size, int *offset, const char *name)
3528 ret = get_common_info(tep, name, offset, size);
3532 return tep_read_number(tep, data + *offset, *size);
3535 static int trace_parse_common_type(struct tep_handle *tep, void *data)
3537 return __parse_common(tep, data,
3538 &tep->type_size, &tep->type_offset,
3542 static int parse_common_pid(struct tep_handle *tep, void *data)
3544 return __parse_common(tep, data,
3545 &tep->pid_size, &tep->pid_offset,
3549 static int parse_common_pc(struct tep_handle *tep, void *data)
3551 return __parse_common(tep, data,
3552 &tep->pc_size, &tep->pc_offset,
3553 "common_preempt_count");
3556 static int parse_common_flags(struct tep_handle *tep, void *data)
3558 return __parse_common(tep, data,
3559 &tep->flags_size, &tep->flags_offset,
3563 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
3565 return __parse_common(tep, data,
3566 &tep->ld_size, &tep->ld_offset,
3567 "common_lock_depth");
3570 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
3572 return __parse_common(tep, data,
3573 &tep->ld_size, &tep->ld_offset,
3574 "common_migrate_disable");
3577 static int events_id_cmp(const void *a, const void *b);
3580 * tep_find_event - find an event by given id
3581 * @tep: a handle to the trace event parser context
3582 * @id: the id of the event
3584 * Returns an event that has a given @id.
3586 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
3588 struct tep_event **eventptr;
3589 struct tep_event key;
3590 struct tep_event *pkey = &key;
3592 /* Check cache first */
3593 if (tep->last_event && tep->last_event->id == id)
3594 return tep->last_event;
3598 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
3599 sizeof(*tep->events), events_id_cmp);
3602 tep->last_event = *eventptr;
3610 * tep_find_event_by_name - find an event by given name
3611 * @tep: a handle to the trace event parser context
3612 * @sys: the system name to search for
3613 * @name: the name of the event to search for
3615 * This returns an event with a given @name and under the system
3616 * @sys. If @sys is NULL the first event with @name is returned.
3619 tep_find_event_by_name(struct tep_handle *tep,
3620 const char *sys, const char *name)
3622 struct tep_event *event = NULL;
3625 if (tep->last_event &&
3626 strcmp(tep->last_event->name, name) == 0 &&
3627 (!sys || strcmp(tep->last_event->system, sys) == 0))
3628 return tep->last_event;
3630 for (i = 0; i < tep->nr_events; i++) {
3631 event = tep->events[i];
3632 if (strcmp(event->name, name) == 0) {
3635 if (strcmp(event->system, sys) == 0)
3639 if (i == tep->nr_events)
3642 tep->last_event = event;
3646 static unsigned long long
3647 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3649 struct tep_handle *tep = event->tep;
3650 unsigned long long val = 0;
3651 unsigned long long left, right;
3652 struct tep_print_arg *typearg = NULL;
3653 struct tep_print_arg *larg;
3654 unsigned long offset;
3655 unsigned int field_size;
3657 switch (arg->type) {
3658 case TEP_PRINT_NULL:
3661 case TEP_PRINT_ATOM:
3662 return strtoull(arg->atom.atom, NULL, 0);
3663 case TEP_PRINT_FIELD:
3664 if (!arg->field.field) {
3665 arg->field.field = tep_find_any_field(event, arg->field.name);
3666 if (!arg->field.field)
3667 goto out_warning_field;
3670 /* must be a number */
3671 val = tep_read_number(tep, data + arg->field.field->offset,
3672 arg->field.field->size);
3674 case TEP_PRINT_FLAGS:
3675 case TEP_PRINT_SYMBOL:
3676 case TEP_PRINT_INT_ARRAY:
3678 case TEP_PRINT_HEX_STR:
3680 case TEP_PRINT_TYPE:
3681 val = eval_num_arg(data, size, event, arg->typecast.item);
3682 return eval_type(val, arg, 0);
3683 case TEP_PRINT_STRING:
3684 case TEP_PRINT_BSTRING:
3685 case TEP_PRINT_BITMASK:
3687 case TEP_PRINT_FUNC: {
3690 val = process_defined_func(&s, data, size, event, arg);
3691 trace_seq_destroy(&s);
3695 if (strcmp(arg->op.op, "[") == 0) {
3697 * Arrays are special, since we don't want
3698 * to read the arg as is.
3700 right = eval_num_arg(data, size, event, arg->op.right);
3702 /* handle typecasts */
3703 larg = arg->op.left;
3704 while (larg->type == TEP_PRINT_TYPE) {
3707 larg = larg->typecast.item;
3710 /* Default to long size */
3711 field_size = tep->long_size;
3713 switch (larg->type) {
3714 case TEP_PRINT_DYNAMIC_ARRAY:
3715 offset = tep_read_number(tep,
3716 data + larg->dynarray.field->offset,
3717 larg->dynarray.field->size);
3718 if (larg->dynarray.field->elementsize)
3719 field_size = larg->dynarray.field->elementsize;
3721 * The actual length of the dynamic array is stored
3722 * in the top half of the field, and the offset
3723 * is in the bottom half of the 32 bit field.
3728 case TEP_PRINT_FIELD:
3729 if (!larg->field.field) {
3731 tep_find_any_field(event, larg->field.name);
3732 if (!larg->field.field) {
3734 goto out_warning_field;
3737 field_size = larg->field.field->elementsize;
3738 offset = larg->field.field->offset +
3739 right * larg->field.field->elementsize;
3742 goto default_op; /* oops, all bets off */
3744 val = tep_read_number(tep,
3745 data + offset, field_size);
3747 val = eval_type(val, typearg, 1);
3749 } else if (strcmp(arg->op.op, "?") == 0) {
3750 left = eval_num_arg(data, size, event, arg->op.left);
3751 arg = arg->op.right;
3753 val = eval_num_arg(data, size, event, arg->op.left);
3755 val = eval_num_arg(data, size, event, arg->op.right);
3759 left = eval_num_arg(data, size, event, arg->op.left);
3760 right = eval_num_arg(data, size, event, arg->op.right);
3761 switch (arg->op.op[0]) {
3763 switch (arg->op.op[1]) {
3768 val = left != right;
3771 goto out_warning_op;
3779 val = left || right;
3785 val = left && right;
3790 switch (arg->op.op[1]) {
3795 val = left << right;
3798 val = left <= right;
3801 goto out_warning_op;
3805 switch (arg->op.op[1]) {
3810 val = left >> right;
3813 val = left >= right;
3816 goto out_warning_op;
3820 if (arg->op.op[1] != '=')
3821 goto out_warning_op;
3823 val = left == right;
3841 goto out_warning_op;
3844 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3845 offset = tep_read_number(tep,
3846 data + arg->dynarray.field->offset,
3847 arg->dynarray.field->size);
3849 * The total allocated length of the dynamic array is
3850 * stored in the top half of the field, and the offset
3851 * is in the bottom half of the 32 bit field.
3853 val = (unsigned long long)(offset >> 16);
3855 case TEP_PRINT_DYNAMIC_ARRAY:
3856 /* Without [], we pass the address to the dynamic data */
3857 offset = tep_read_number(tep,
3858 data + arg->dynarray.field->offset,
3859 arg->dynarray.field->size);
3861 * The total allocated length of the dynamic array is
3862 * stored in the top half of the field, and the offset
3863 * is in the bottom half of the 32 bit field.
3866 val = (unsigned long long)((unsigned long)data + offset);
3868 default: /* not sure what to do there */
3874 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3878 do_warning_event(event, "%s: field %s not found",
3879 __func__, arg->field.name);
3885 unsigned long long value;
3888 static const struct flag flags[] = {
3889 { "HI_SOFTIRQ", 0 },
3890 { "TIMER_SOFTIRQ", 1 },
3891 { "NET_TX_SOFTIRQ", 2 },
3892 { "NET_RX_SOFTIRQ", 3 },
3893 { "BLOCK_SOFTIRQ", 4 },
3894 { "IRQ_POLL_SOFTIRQ", 5 },
3895 { "TASKLET_SOFTIRQ", 6 },
3896 { "SCHED_SOFTIRQ", 7 },
3897 { "HRTIMER_SOFTIRQ", 8 },
3898 { "RCU_SOFTIRQ", 9 },
3900 { "HRTIMER_NORESTART", 0 },
3901 { "HRTIMER_RESTART", 1 },
3904 static long long eval_flag(const char *flag)
3909 * Some flags in the format files do not get converted.
3910 * If the flag is not numeric, see if it is something that
3911 * we already know about.
3913 if (isdigit(flag[0]))
3914 return strtoull(flag, NULL, 0);
3916 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3917 if (strcmp(flags[i].name, flag) == 0)
3918 return flags[i].value;
3923 static void print_str_to_seq(struct trace_seq *s, const char *format,
3924 int len_arg, const char *str)
3927 trace_seq_printf(s, format, len_arg, str);
3929 trace_seq_printf(s, format, str);
3932 static void print_bitmask_to_seq(struct tep_handle *tep,
3933 struct trace_seq *s, const char *format,
3934 int len_arg, const void *data, int size)
3936 int nr_bits = size * 8;
3937 int str_size = (nr_bits + 3) / 4;
3945 * The kernel likes to put in commas every 32 bits, we
3948 str_size += (nr_bits - 1) / 32;
3950 str = malloc(str_size + 1);
3952 do_warning("%s: not enough memory!", __func__);
3957 /* Start out with -2 for the two chars per byte */
3958 for (i = str_size - 2; i >= 0; i -= 2) {
3960 * data points to a bit mask of size bytes.
3961 * In the kernel, this is an array of long words, thus
3962 * endianness is very important.
3964 if (tep->file_bigendian)
3965 index = size - (len + 1);
3969 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3970 memcpy(str + i, buf, 2);
3972 if (!(len & 3) && i > 0) {
3979 trace_seq_printf(s, format, len_arg, str);
3981 trace_seq_printf(s, format, str);
3986 static void print_str_arg(struct trace_seq *s, void *data, int size,
3987 struct tep_event *event, const char *format,
3988 int len_arg, struct tep_print_arg *arg)
3990 struct tep_handle *tep = event->tep;
3991 struct tep_print_flag_sym *flag;
3992 struct tep_format_field *field;
3993 struct printk_map *printk;
3994 long long val, fval;
3995 unsigned long long addr;
4001 switch (arg->type) {
4002 case TEP_PRINT_NULL:
4005 case TEP_PRINT_ATOM:
4006 print_str_to_seq(s, format, len_arg, arg->atom.atom);
4008 case TEP_PRINT_FIELD:
4009 field = arg->field.field;
4011 field = tep_find_any_field(event, arg->field.name);
4013 str = arg->field.name;
4014 goto out_warning_field;
4016 arg->field.field = field;
4018 /* Zero sized fields, mean the rest of the data */
4019 len = field->size ? : size - field->offset;
4022 * Some events pass in pointers. If this is not an array
4023 * and the size is the same as long_size, assume that it
4026 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
4027 field->size == tep->long_size) {
4029 /* Handle heterogeneous recording and processing
4033 * Traces recorded on 32-bit devices (32-bit
4034 * addressing) and processed on 64-bit devices:
4035 * In this case, only 32 bits should be read.
4038 * Traces recorded on 64 bit devices and processed
4039 * on 32-bit devices:
4040 * In this case, 64 bits must be read.
4042 addr = (tep->long_size == 8) ?
4043 *(unsigned long long *)(data + field->offset) :
4044 (unsigned long long)*(unsigned int *)(data + field->offset);
4046 /* Check if it matches a print format */
4047 printk = find_printk(tep, addr);
4049 trace_seq_puts(s, printk->printk);
4051 trace_seq_printf(s, "%llx", addr);
4054 str = malloc(len + 1);
4056 do_warning_event(event, "%s: not enough memory!",
4060 memcpy(str, data + field->offset, len);
4062 print_str_to_seq(s, format, len_arg, str);
4065 case TEP_PRINT_FLAGS:
4066 val = eval_num_arg(data, size, event, arg->flags.field);
4068 for (flag = arg->flags.flags; flag; flag = flag->next) {
4069 fval = eval_flag(flag->value);
4070 if (!val && fval < 0) {
4071 print_str_to_seq(s, format, len_arg, flag->str);
4074 if (fval > 0 && (val & fval) == fval) {
4075 if (print && arg->flags.delim)
4076 trace_seq_puts(s, arg->flags.delim);
4077 print_str_to_seq(s, format, len_arg, flag->str);
4083 if (print && arg->flags.delim)
4084 trace_seq_puts(s, arg->flags.delim);
4085 trace_seq_printf(s, "0x%llx", val);
4088 case TEP_PRINT_SYMBOL:
4089 val = eval_num_arg(data, size, event, arg->symbol.field);
4090 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4091 fval = eval_flag(flag->value);
4093 print_str_to_seq(s, format, len_arg, flag->str);
4098 trace_seq_printf(s, "0x%llx", val);
4101 case TEP_PRINT_HEX_STR:
4102 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4103 unsigned long offset;
4104 offset = tep_read_number(tep,
4105 data + arg->hex.field->dynarray.field->offset,
4106 arg->hex.field->dynarray.field->size);
4107 hex = data + (offset & 0xffff);
4109 field = arg->hex.field->field.field;
4111 str = arg->hex.field->field.name;
4112 field = tep_find_any_field(event, str);
4114 goto out_warning_field;
4115 arg->hex.field->field.field = field;
4117 hex = data + field->offset;
4119 len = eval_num_arg(data, size, event, arg->hex.size);
4120 for (i = 0; i < len; i++) {
4121 if (i && arg->type == TEP_PRINT_HEX)
4122 trace_seq_putc(s, ' ');
4123 trace_seq_printf(s, "%02x", hex[i]);
4127 case TEP_PRINT_INT_ARRAY: {
4131 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4132 unsigned long offset;
4133 struct tep_format_field *field =
4134 arg->int_array.field->dynarray.field;
4135 offset = tep_read_number(tep,
4136 data + field->offset,
4138 num = data + (offset & 0xffff);
4140 field = arg->int_array.field->field.field;
4142 str = arg->int_array.field->field.name;
4143 field = tep_find_any_field(event, str);
4145 goto out_warning_field;
4146 arg->int_array.field->field.field = field;
4148 num = data + field->offset;
4150 len = eval_num_arg(data, size, event, arg->int_array.count);
4151 el_size = eval_num_arg(data, size, event,
4152 arg->int_array.el_size);
4153 for (i = 0; i < len; i++) {
4155 trace_seq_putc(s, ' ');
4158 trace_seq_printf(s, "%u", *(uint8_t *)num);
4159 } else if (el_size == 2) {
4160 trace_seq_printf(s, "%u", *(uint16_t *)num);
4161 } else if (el_size == 4) {
4162 trace_seq_printf(s, "%u", *(uint32_t *)num);
4163 } else if (el_size == 8) {
4164 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4166 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4167 el_size, *(uint8_t *)num);
4175 case TEP_PRINT_TYPE:
4177 case TEP_PRINT_STRING: {
4180 if (!arg->string.field)
4181 arg->string.field = tep_find_any_field(event, arg->string.string);
4182 if (!arg->string.field)
4185 str_offset = data2host4(tep,
4186 *(unsigned int *)(data + arg->string.field->offset));
4187 str_offset &= 0xffff;
4188 if (arg->string.field->flags & TEP_FIELD_IS_RELATIVE)
4189 str_offset += arg->string.field->offset + arg->string.field->size;
4190 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4193 case TEP_PRINT_BSTRING:
4194 print_str_to_seq(s, format, len_arg, arg->string.string);
4196 case TEP_PRINT_BITMASK: {
4200 if (!arg->bitmask.field)
4201 arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
4202 if (!arg->bitmask.field)
4204 bitmask_offset = data2host4(tep,
4205 *(unsigned int *)(data + arg->bitmask.field->offset));
4206 bitmask_size = bitmask_offset >> 16;
4207 bitmask_offset &= 0xffff;
4208 if (arg->bitmask.field->flags & TEP_FIELD_IS_RELATIVE)
4209 bitmask_offset += arg->bitmask.field->offset + arg->bitmask.field->size;
4210 print_bitmask_to_seq(tep, s, format, len_arg,
4211 data + bitmask_offset, bitmask_size);
4216 * The only op for string should be ? :
4218 if (arg->op.op[0] != '?')
4220 val = eval_num_arg(data, size, event, arg->op.left);
4222 print_str_arg(s, data, size, event,
4223 format, len_arg, arg->op.right->op.left);
4225 print_str_arg(s, data, size, event,
4226 format, len_arg, arg->op.right->op.right);
4228 case TEP_PRINT_FUNC:
4229 process_defined_func(s, data, size, event, arg);
4239 do_warning_event(event, "%s: field %s not found",
4240 __func__, arg->field.name);
4243 static unsigned long long
4244 process_defined_func(struct trace_seq *s, void *data, int size,
4245 struct tep_event *event, struct tep_print_arg *arg)
4247 struct tep_function_handler *func_handle = arg->func.func;
4248 struct func_params *param;
4249 unsigned long long *args;
4250 unsigned long long ret;
4251 struct tep_print_arg *farg;
4252 struct trace_seq str;
4254 struct save_str *next;
4256 } *strings = NULL, *string;
4259 if (!func_handle->nr_args) {
4260 ret = (*func_handle->func)(s, NULL);
4264 farg = arg->func.args;
4265 param = func_handle->params;
4268 args = malloc(sizeof(*args) * func_handle->nr_args);
4272 for (i = 0; i < func_handle->nr_args; i++) {
4273 switch (param->type) {
4274 case TEP_FUNC_ARG_INT:
4275 case TEP_FUNC_ARG_LONG:
4276 case TEP_FUNC_ARG_PTR:
4277 args[i] = eval_num_arg(data, size, event, farg);
4279 case TEP_FUNC_ARG_STRING:
4280 trace_seq_init(&str);
4281 print_str_arg(&str, data, size, event, "%s", -1, farg);
4282 trace_seq_terminate(&str);
4283 string = malloc(sizeof(*string));
4285 do_warning_event(event, "%s(%d): malloc str",
4286 __func__, __LINE__);
4289 string->next = strings;
4290 string->str = strdup(str.buffer);
4293 do_warning_event(event, "%s(%d): malloc str",
4294 __func__, __LINE__);
4297 args[i] = (uintptr_t)string->str;
4299 trace_seq_destroy(&str);
4303 * Something went totally wrong, this is not
4304 * an input error, something in this code broke.
4306 do_warning_event(event, "Unexpected end of arguments\n");
4310 param = param->next;
4313 ret = (*func_handle->func)(s, args);
4318 strings = string->next;
4324 /* TBD : handle return type here */
4328 static void free_args(struct tep_print_arg *args)
4330 struct tep_print_arg *next;
4340 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4342 struct tep_handle *tep = event->tep;
4343 struct tep_format_field *field, *ip_field;
4344 struct tep_print_arg *args, *arg, **next;
4345 unsigned long long ip, val;
4350 field = tep->bprint_buf_field;
4351 ip_field = tep->bprint_ip_field;
4354 field = tep_find_field(event, "buf");
4356 do_warning_event(event, "can't find buffer field for binary printk");
4359 ip_field = tep_find_field(event, "ip");
4361 do_warning_event(event, "can't find ip field for binary printk");
4364 tep->bprint_buf_field = field;
4365 tep->bprint_ip_field = ip_field;
4368 ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
4371 * The first arg is the IP pointer.
4375 do_warning_event(event, "%s(%d): not enough memory!",
4376 __func__, __LINE__);
4383 arg->type = TEP_PRINT_ATOM;
4385 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4388 /* skip the first "%ps: " */
4389 for (ptr = fmt + 5, bptr = data + field->offset;
4390 bptr < data + size && *ptr; ptr++) {
4415 if (isalnum(ptr[1])) {
4417 /* Check for special pointers */
4426 * Pre-5.5 kernels use %pf and
4427 * %pF for printing symbols
4428 * while kernels since 5.5 use
4429 * %pfw for fwnodes. So check
4430 * %p[fF] isn't followed by 'w'.
4437 * Older kernels do not process
4438 * dereferenced pointers.
4439 * Only process if the pointer
4440 * value is a printable.
4442 if (isprint(*(char *)bptr))
4443 goto process_string;
4458 vsize = tep->long_size;
4472 /* the pointers are always 4 bytes aligned */
4473 bptr = (void *)(((unsigned long)bptr + 3) &
4475 val = tep_read_number(tep, bptr, vsize);
4479 do_warning_event(event, "%s(%d): not enough memory!",
4480 __func__, __LINE__);
4484 arg->type = TEP_PRINT_ATOM;
4485 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4492 * The '*' case means that an arg is used as the length.
4493 * We need to continue to figure out for what.
4503 do_warning_event(event, "%s(%d): not enough memory!",
4504 __func__, __LINE__);
4508 arg->type = TEP_PRINT_BSTRING;
4509 arg->string.string = strdup(bptr);
4510 if (!arg->string.string)
4512 bptr += strlen(bptr) + 1;
4529 get_bprint_format(void *data, int size __maybe_unused,
4530 struct tep_event *event)
4532 struct tep_handle *tep = event->tep;
4533 unsigned long long addr;
4534 struct tep_format_field *field;
4535 struct printk_map *printk;
4538 field = tep->bprint_fmt_field;
4541 field = tep_find_field(event, "fmt");
4543 do_warning_event(event, "can't find format field for binary printk");
4546 tep->bprint_fmt_field = field;
4549 addr = tep_read_number(tep, data + field->offset, field->size);
4551 printk = find_printk(tep, addr);
4553 if (asprintf(&format, "%%ps: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4558 if (asprintf(&format, "%s: %s", "%ps", printk->printk) < 0)
4564 static int print_mac_arg(struct trace_seq *s, const char *format,
4565 void *data, int size, struct tep_event *event,
4566 struct tep_print_arg *arg)
4568 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4569 bool reverse = false;
4573 if (arg->type == TEP_PRINT_FUNC) {
4574 process_defined_func(s, data, size, event, arg);
4578 if (arg->type != TEP_PRINT_FIELD) {
4579 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4584 if (format[0] == 'm') {
4585 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4586 } else if (format[0] == 'M' && format[1] == 'F') {
4587 fmt = "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x";
4590 if (format[1] == 'R') {
4595 if (!arg->field.field) {
4597 tep_find_any_field(event, arg->field.name);
4598 if (!arg->field.field) {
4599 do_warning_event(event, "%s: field %s not found",
4600 __func__, arg->field.name);
4604 if (arg->field.field->size != 6) {
4605 trace_seq_printf(s, "INVALIDMAC");
4609 buf = data + arg->field.field->offset;
4611 trace_seq_printf(s, fmt, buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
4613 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4618 static int parse_ip4_print_args(struct tep_handle *tep,
4619 const char *ptr, bool *reverse)
4628 if (tep->file_bigendian)
4650 static void print_ip4_addr(struct trace_seq *s, char i, bool reverse, unsigned char *buf)
4655 fmt = "%03d.%03d.%03d.%03d";
4657 fmt = "%d.%d.%d.%d";
4660 trace_seq_printf(s, fmt, buf[3], buf[2], buf[1], buf[0]);
4662 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4666 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4668 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4669 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4672 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4674 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4677 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4680 unsigned char zerolength[8];
4685 bool needcolon = false;
4687 struct in6_addr in6;
4689 memcpy(&in6, addr, sizeof(struct in6_addr));
4691 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4693 memset(zerolength, 0, sizeof(zerolength));
4700 /* find position of longest 0 run */
4701 for (i = 0; i < range; i++) {
4702 for (j = i; j < range; j++) {
4703 if (in6.s6_addr16[j] != 0)
4708 for (i = 0; i < range; i++) {
4709 if (zerolength[i] > longest) {
4710 longest = zerolength[i];
4714 if (longest == 1) /* don't compress a single 0 */
4718 for (i = 0; i < range; i++) {
4719 if (i == colonpos) {
4720 if (needcolon || i == 0)
4721 trace_seq_printf(s, ":");
4722 trace_seq_printf(s, ":");
4728 trace_seq_printf(s, ":");
4731 /* hex u16 without leading 0s */
4732 word = ntohs(in6.s6_addr16[i]);
4736 trace_seq_printf(s, "%x%02x", hi, lo);
4738 trace_seq_printf(s, "%x", lo);
4745 trace_seq_printf(s, ":");
4746 print_ip4_addr(s, 'I', false, &in6.s6_addr[12]);
4752 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4756 for (j = 0; j < 16; j += 2) {
4757 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4758 if (i == 'I' && j < 14)
4759 trace_seq_printf(s, ":");
4764 * %pi4 print an IPv4 address with leading zeros
4765 * %pI4 print an IPv4 address without leading zeros
4766 * %pi6 print an IPv6 address without colons
4767 * %pI6 print an IPv6 address with colons
4768 * %pI6c print an IPv6 address in compressed form with colons
4769 * %pISpc print an IP address based on sockaddr; p adds port.
4771 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4772 void *data, int size, struct tep_event *event,
4773 struct tep_print_arg *arg)
4775 bool reverse = false;
4779 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
4781 if (arg->type == TEP_PRINT_FUNC) {
4782 process_defined_func(s, data, size, event, arg);
4786 if (arg->type != TEP_PRINT_FIELD) {
4787 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4791 if (!arg->field.field) {
4793 tep_find_any_field(event, arg->field.name);
4794 if (!arg->field.field) {
4795 do_warning("%s: field %s not found",
4796 __func__, arg->field.name);
4801 buf = data + arg->field.field->offset;
4803 if (arg->field.field->size != 4) {
4804 trace_seq_printf(s, "INVALIDIPv4");
4808 print_ip4_addr(s, i, reverse, buf);
4813 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4814 void *data, int size, struct tep_event *event,
4815 struct tep_print_arg *arg)
4822 if (i == 'I' && *ptr == 'c') {
4828 if (arg->type == TEP_PRINT_FUNC) {
4829 process_defined_func(s, data, size, event, arg);
4833 if (arg->type != TEP_PRINT_FIELD) {
4834 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4838 if (!arg->field.field) {
4840 tep_find_any_field(event, arg->field.name);
4841 if (!arg->field.field) {
4842 do_warning("%s: field %s not found",
4843 __func__, arg->field.name);
4848 buf = data + arg->field.field->offset;
4850 if (arg->field.field->size != 16) {
4851 trace_seq_printf(s, "INVALIDIPv6");
4856 print_ip6c_addr(s, buf);
4858 print_ip6_addr(s, i, buf);
4863 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4864 void *data, int size, struct tep_event *event,
4865 struct tep_print_arg *arg)
4867 char have_c = 0, have_p = 0;
4869 struct sockaddr_storage *sa;
4870 bool reverse = false;
4887 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
4891 if (arg->type == TEP_PRINT_FUNC) {
4892 process_defined_func(s, data, size, event, arg);
4896 if (arg->type != TEP_PRINT_FIELD) {
4897 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4901 if (!arg->field.field) {
4903 tep_find_any_field(event, arg->field.name);
4904 if (!arg->field.field) {
4905 do_warning("%s: field %s not found",
4906 __func__, arg->field.name);
4911 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4913 if (sa->ss_family == AF_INET) {
4914 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4916 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4917 trace_seq_printf(s, "INVALIDIPv4");
4921 print_ip4_addr(s, i, reverse, (unsigned char *) &sa4->sin_addr);
4923 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4926 } else if (sa->ss_family == AF_INET6) {
4927 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4929 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4930 trace_seq_printf(s, "INVALIDIPv6");
4935 trace_seq_printf(s, "[");
4937 buf = (unsigned char *) &sa6->sin6_addr;
4939 print_ip6c_addr(s, buf);
4941 print_ip6_addr(s, i, buf);
4944 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4950 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4951 void *data, int size, struct tep_event *event,
4952 struct tep_print_arg *arg)
4954 char i = *ptr; /* 'i' or 'I' */
4962 rc += print_ipv4_arg(s, ptr + 1, i, data, size, event, arg);
4965 rc += print_ipv6_arg(s, ptr + 1, i, data, size, event, arg);
4968 rc += print_ipsa_arg(s, ptr + 1, i, data, size, event, arg);
4977 static const int guid_index[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
4978 static const int uuid_index[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4980 static int print_uuid_arg(struct trace_seq *s, const char *ptr,
4981 void *data, int size, struct tep_event *event,
4982 struct tep_print_arg *arg)
4984 const int *index = uuid_index;
4985 char *format = "%02x";
4990 switch (*(ptr + 1)) {
5006 if (arg->type == TEP_PRINT_FUNC) {
5007 process_defined_func(s, data, size, event, arg);
5011 if (arg->type != TEP_PRINT_FIELD) {
5012 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5016 if (!arg->field.field) {
5018 tep_find_any_field(event, arg->field.name);
5019 if (!arg->field.field) {
5020 do_warning("%s: field %s not found",
5021 __func__, arg->field.name);
5026 if (arg->field.field->size != 16) {
5027 trace_seq_printf(s, "INVALIDUUID");
5031 buf = data + arg->field.field->offset;
5033 for (i = 0; i < 16; i++) {
5034 trace_seq_printf(s, format, buf[index[i]] & 0xff);
5040 trace_seq_printf(s, "-");
5048 static int print_raw_buff_arg(struct trace_seq *s, const char *ptr,
5049 void *data, int size, struct tep_event *event,
5050 struct tep_print_arg *arg, int print_len)
5052 int plen = print_len;
5057 unsigned long offset;
5060 switch (*(ptr + 1)) {
5075 if (arg->type == TEP_PRINT_FUNC) {
5076 process_defined_func(s, data, size, event, arg);
5080 if (arg->type != TEP_PRINT_DYNAMIC_ARRAY) {
5081 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5085 offset = tep_read_number(event->tep,
5086 data + arg->dynarray.field->offset,
5087 arg->dynarray.field->size);
5088 arr_len = (unsigned long long)(offset >> 16);
5089 buf = data + (offset & 0xffff);
5097 trace_seq_printf(s, "%02x", buf[0] & 0xff);
5098 for (i = 1; i < plen; i++)
5099 trace_seq_printf(s, "%s%02x", delim, buf[i] & 0xff);
5104 static int is_printable_array(char *p, unsigned int len)
5108 for (i = 0; i < len && p[i]; i++)
5109 if (!isprint(p[i]) && !isspace(p[i]))
5114 void tep_print_field(struct trace_seq *s, void *data,
5115 struct tep_format_field *field)
5117 unsigned long long val;
5118 unsigned int offset, len, i;
5119 struct tep_handle *tep = field->event->tep;
5121 if (field->flags & TEP_FIELD_IS_ARRAY) {
5122 offset = field->offset;
5124 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
5125 val = tep_read_number(tep, data + offset, len);
5129 if (field->flags & TEP_FIELD_IS_RELATIVE)
5130 offset += field->offset + field->size;
5132 if (field->flags & TEP_FIELD_IS_STRING &&
5133 is_printable_array(data + offset, len)) {
5134 trace_seq_printf(s, "%s", (char *)data + offset);
5136 trace_seq_puts(s, "ARRAY[");
5137 for (i = 0; i < len; i++) {
5139 trace_seq_puts(s, ", ");
5140 trace_seq_printf(s, "%02x",
5141 *((unsigned char *)data + offset + i));
5143 trace_seq_putc(s, ']');
5144 field->flags &= ~TEP_FIELD_IS_STRING;
5147 val = tep_read_number(tep, data + field->offset,
5149 if (field->flags & TEP_FIELD_IS_POINTER) {
5150 trace_seq_printf(s, "0x%llx", val);
5151 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
5152 switch (field->size) {
5155 * If field is long then print it in hex.
5156 * A long usually stores pointers.
5158 if (field->flags & TEP_FIELD_IS_LONG)
5159 trace_seq_printf(s, "0x%x", (int)val);
5161 trace_seq_printf(s, "%d", (int)val);
5164 trace_seq_printf(s, "%2d", (short)val);
5167 trace_seq_printf(s, "%1d", (char)val);
5170 trace_seq_printf(s, "%lld", val);
5173 if (field->flags & TEP_FIELD_IS_LONG)
5174 trace_seq_printf(s, "0x%llx", val);
5176 trace_seq_printf(s, "%llu", val);
5181 void tep_print_fields(struct trace_seq *s, void *data,
5182 int size __maybe_unused, struct tep_event *event)
5184 struct tep_format_field *field;
5186 field = event->format.fields;
5188 trace_seq_printf(s, " %s=", field->name);
5189 tep_print_field(s, data, field);
5190 field = field->next;
5194 static int print_function(struct trace_seq *s, const char *format,
5195 void *data, int size, struct tep_event *event,
5196 struct tep_print_arg *arg)
5198 struct func_map *func;
5199 unsigned long long val;
5201 val = eval_num_arg(data, size, event, arg);
5202 func = find_func(event->tep, val);
5204 trace_seq_puts(s, func->func);
5205 if (*format == 'F' || *format == 'S')
5206 trace_seq_printf(s, "+0x%llx", val - func->addr);
5208 if (event->tep->long_size == 4)
5209 trace_seq_printf(s, "0x%lx", (long)val);
5211 trace_seq_printf(s, "0x%llx", (long long)val);
5217 static int print_arg_pointer(struct trace_seq *s, const char *format, int plen,
5218 void *data, int size,
5219 struct tep_event *event, struct tep_print_arg *arg)
5221 unsigned long long val;
5224 if (arg->type == TEP_PRINT_BSTRING) {
5225 trace_seq_puts(s, arg->string.string);
5229 if (*format == 'p') {
5241 ret += print_function(s, format, data, size, event, arg);
5245 ret += print_mac_arg(s, format, data, size, event, arg);
5249 ret += print_ip_arg(s, format, data, size, event, arg);
5252 ret += print_uuid_arg(s, format, data, size, event, arg);
5255 ret += print_raw_buff_arg(s, format, data, size, event, arg, plen);
5259 val = eval_num_arg(data, size, event, arg);
5260 trace_seq_printf(s, "%p", (void *)(intptr_t)val);
5268 static int print_arg_number(struct trace_seq *s, const char *format, int plen,
5269 void *data, int size, int ls,
5270 struct tep_event *event, struct tep_print_arg *arg)
5272 unsigned long long val;
5274 val = eval_num_arg(data, size, event, arg);
5279 trace_seq_printf(s, format, plen, (char)val);
5281 trace_seq_printf(s, format, (char)val);
5285 trace_seq_printf(s, format, plen, (short)val);
5287 trace_seq_printf(s, format, (short)val);
5291 trace_seq_printf(s, format, plen, (int)val);
5293 trace_seq_printf(s, format, (int)val);
5297 trace_seq_printf(s, format, plen, (long)val);
5299 trace_seq_printf(s, format, (long)val);
5303 trace_seq_printf(s, format, plen, (long long)val);
5305 trace_seq_printf(s, format, (long long)val);
5308 do_warning_event(event, "bad count (%d)", ls);
5309 event->flags |= TEP_EVENT_FL_FAILED;
5315 static void print_arg_string(struct trace_seq *s, const char *format, int plen,
5316 void *data, int size,
5317 struct tep_event *event, struct tep_print_arg *arg)
5321 /* Use helper trace_seq */
5323 print_str_arg(&p, data, size, event,
5325 trace_seq_terminate(&p);
5326 trace_seq_puts(s, p.buffer);
5327 trace_seq_destroy(&p);
5330 static int parse_arg_format_pointer(const char *format)
5346 switch (format[1]) {
5358 switch (format[1]) {
5362 switch (format[index]) {
5377 switch (format[index]) {
5386 if (format[1] == '4') {
5393 if (format[index] == 'c')
5401 switch (format[1]) {
5412 switch (format[1]) {
5428 static void free_parse_args(struct tep_print_parse *arg)
5430 struct tep_print_parse *del;
5440 static int parse_arg_add(struct tep_print_parse **parse, char *format,
5441 enum tep_print_parse_type type,
5442 struct tep_print_arg *arg,
5443 struct tep_print_arg *len_as_arg,
5446 struct tep_print_parse *parg = NULL;
5448 parg = calloc(1, sizeof(*parg));
5451 parg->format = strdup(format);
5456 parg->len_as_arg = len_as_arg;
5468 static int parse_arg_format(struct tep_print_parse **parse,
5469 struct tep_event *event,
5470 const char *format, struct tep_print_arg **arg)
5472 struct tep_print_arg *len_arg = NULL;
5473 char print_format[32];
5474 const char *start = format;
5482 for (; *format; format++) {
5485 /* FIXME: need to handle properly */
5503 /* The argument is the length. */
5505 do_warning_event(event, "no argument match");
5506 event->flags |= TEP_EVENT_FL_FAILED;
5510 do_warning_event(event, "argument already matched");
5511 event->flags |= TEP_EVENT_FL_FAILED;
5515 *arg = (*arg)->next;
5519 do_warning_event(event, "no argument match");
5520 event->flags |= TEP_EVENT_FL_FAILED;
5523 res = parse_arg_format_pointer(format + 1);
5528 len = ((unsigned long)format + 1) -
5529 (unsigned long)start;
5530 /* should never happen */
5532 do_warning_event(event, "bad format!");
5533 event->flags |= TEP_EVENT_FL_FAILED;
5536 memcpy(print_format, start, len);
5537 print_format[len] = 0;
5539 parse_arg_add(parse, print_format,
5540 PRINT_FMT_ARG_POINTER, *arg, len_arg, ls);
5541 *arg = (*arg)->next;
5551 do_warning_event(event, "no argument match");
5552 event->flags |= TEP_EVENT_FL_FAILED;
5556 len = ((unsigned long)format + 1) -
5557 (unsigned long)start;
5559 /* should never happen */
5561 do_warning_event(event, "bad format!");
5562 event->flags |= TEP_EVENT_FL_FAILED;
5565 memcpy(print_format, start, len);
5566 print_format[len] = 0;
5568 if (event->tep->long_size == 8 && ls == 1 &&
5569 sizeof(long) != 8) {
5572 /* make %l into %ll */
5573 if (ls == 1 && (p = strchr(print_format, 'l')))
5574 memmove(p+1, p, strlen(p)+1);
5577 if (ls < -2 || ls > 2) {
5578 do_warning_event(event, "bad count (%d)", ls);
5579 event->flags |= TEP_EVENT_FL_FAILED;
5581 parse_arg_add(parse, print_format,
5582 PRINT_FMT_ARG_DIGIT, *arg, len_arg, ls);
5583 *arg = (*arg)->next;
5588 do_warning_event(event, "no matching argument");
5589 event->flags |= TEP_EVENT_FL_FAILED;
5593 len = ((unsigned long)format + 1) -
5594 (unsigned long)start;
5596 /* should never happen */
5598 do_warning_event(event, "bad format!");
5599 event->flags |= TEP_EVENT_FL_FAILED;
5603 memcpy(print_format, start, len);
5604 print_format[len] = 0;
5606 parse_arg_add(parse, print_format,
5607 PRINT_FMT_ARG_STRING, *arg, len_arg, 0);
5608 *arg = (*arg)->next;
5612 snprintf(print_format, 32, ">%c<", *format);
5613 parse_arg_add(parse, print_format,
5614 PRINT_FMT_STRING, NULL, NULL, 0);
5626 static int parse_arg_string(struct tep_print_parse **parse, const char *format)
5632 for (; *format; format++) {
5633 if (*format == '\\') {
5638 trace_seq_putc(&s, '\n');
5641 trace_seq_putc(&s, '\t');
5644 trace_seq_putc(&s, '\r');
5647 trace_seq_putc(&s, '\\');
5650 trace_seq_putc(&s, *format);
5653 } else if (*format == '%') {
5654 if (*(format + 1) == '%') {
5655 trace_seq_putc(&s, '%');
5661 trace_seq_putc(&s, *format);
5665 trace_seq_terminate(&s);
5666 parse_arg_add(parse, s.buffer, PRINT_FMT_STRING, NULL, NULL, 0);
5667 trace_seq_destroy(&s);
5672 static struct tep_print_parse *
5673 parse_args(struct tep_event *event, const char *format, struct tep_print_arg *arg)
5675 struct tep_print_parse *parse_ret = NULL;
5676 struct tep_print_parse **parse = NULL;
5680 len = strlen(format);
5684 if (*format == '%' && *(format + 1) != '%')
5685 ret = parse_arg_format(parse, event, format, &arg);
5687 ret = parse_arg_string(parse, format);
5689 parse = &((*parse)->next);
5700 static void print_event_cache(struct tep_print_parse *parse, struct trace_seq *s,
5701 void *data, int size, struct tep_event *event)
5706 if (parse->len_as_arg)
5707 len_arg = eval_num_arg(data, size, event, parse->len_as_arg);
5708 switch (parse->type) {
5709 case PRINT_FMT_ARG_DIGIT:
5710 print_arg_number(s, parse->format,
5711 parse->len_as_arg ? len_arg : -1, data,
5712 size, parse->ls, event, parse->arg);
5714 case PRINT_FMT_ARG_POINTER:
5715 print_arg_pointer(s, parse->format,
5716 parse->len_as_arg ? len_arg : 1,
5717 data, size, event, parse->arg);
5719 case PRINT_FMT_ARG_STRING:
5720 print_arg_string(s, parse->format,
5721 parse->len_as_arg ? len_arg : -1,
5722 data, size, event, parse->arg);
5724 case PRINT_FMT_STRING:
5726 trace_seq_printf(s, "%s", parse->format);
5729 parse = parse->next;
5733 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
5735 struct tep_print_parse *parse = event->print_fmt.print_cache;
5736 struct tep_print_arg *args = NULL;
5737 char *bprint_fmt = NULL;
5739 if (event->flags & TEP_EVENT_FL_FAILED) {
5740 trace_seq_printf(s, "[FAILED TO PARSE]");
5741 tep_print_fields(s, data, size, event);
5745 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
5746 bprint_fmt = get_bprint_format(data, size, event);
5747 args = make_bprint_args(bprint_fmt, data, size, event);
5748 parse = parse_args(event, bprint_fmt, args);
5751 print_event_cache(parse, s, data, size, event);
5753 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
5754 free_parse_args(parse);
5761 * This parses out the Latency format (interrupts disabled,
5762 * need rescheduling, in hard/soft interrupt, preempt count
5763 * and lock depth) and places it into the trace_seq.
5765 static void data_latency_format(struct tep_handle *tep, struct trace_seq *s,
5766 char *format, struct tep_record *record)
5768 static int check_lock_depth = 1;
5769 static int check_migrate_disable = 1;
5770 static int lock_depth_exists;
5771 static int migrate_disable_exists;
5772 unsigned int lat_flags;
5773 struct trace_seq sq;
5776 int migrate_disable = 0;
5779 void *data = record->data;
5781 trace_seq_init(&sq);
5782 lat_flags = parse_common_flags(tep, data);
5783 pc = parse_common_pc(tep, data);
5784 /* lock_depth may not always exist */
5785 if (lock_depth_exists)
5786 lock_depth = parse_common_lock_depth(tep, data);
5787 else if (check_lock_depth) {
5788 lock_depth = parse_common_lock_depth(tep, data);
5790 check_lock_depth = 0;
5792 lock_depth_exists = 1;
5795 /* migrate_disable may not always exist */
5796 if (migrate_disable_exists)
5797 migrate_disable = parse_common_migrate_disable(tep, data);
5798 else if (check_migrate_disable) {
5799 migrate_disable = parse_common_migrate_disable(tep, data);
5800 if (migrate_disable < 0)
5801 check_migrate_disable = 0;
5803 migrate_disable_exists = 1;
5806 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5807 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5809 trace_seq_printf(&sq, "%c%c%c",
5810 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5811 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5813 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5815 (hardirq && softirq) ? 'H' :
5816 hardirq ? 'h' : softirq ? 's' : '.');
5819 trace_seq_printf(&sq, "%x", pc);
5821 trace_seq_printf(&sq, ".");
5823 if (migrate_disable_exists) {
5824 if (migrate_disable < 0)
5825 trace_seq_printf(&sq, ".");
5827 trace_seq_printf(&sq, "%d", migrate_disable);
5830 if (lock_depth_exists) {
5832 trace_seq_printf(&sq, ".");
5834 trace_seq_printf(&sq, "%d", lock_depth);
5837 if (sq.state == TRACE_SEQ__MEM_ALLOC_FAILED) {
5838 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
5842 trace_seq_terminate(&sq);
5843 trace_seq_puts(s, sq.buffer);
5844 trace_seq_destroy(&sq);
5845 trace_seq_terminate(s);
5849 * tep_data_type - parse out the given event type
5850 * @tep: a handle to the trace event parser context
5851 * @rec: the record to read from
5853 * This returns the event id from the @rec.
5855 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
5857 return trace_parse_common_type(tep, rec->data);
5861 * tep_data_pid - parse the PID from record
5862 * @tep: a handle to the trace event parser context
5863 * @rec: the record to parse
5865 * This returns the PID from a record.
5867 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
5869 return parse_common_pid(tep, rec->data);
5873 * tep_data_preempt_count - parse the preempt count from the record
5874 * @tep: a handle to the trace event parser context
5875 * @rec: the record to parse
5877 * This returns the preempt count from a record.
5879 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
5881 return parse_common_pc(tep, rec->data);
5885 * tep_data_flags - parse the latency flags from the record
5886 * @tep: a handle to the trace event parser context
5887 * @rec: the record to parse
5889 * This returns the latency flags from a record.
5891 * Use trace_flag_type enum for the flags (see event-parse.h).
5893 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
5895 return parse_common_flags(tep, rec->data);
5899 * tep_data_comm_from_pid - return the command line from PID
5900 * @tep: a handle to the trace event parser context
5901 * @pid: the PID of the task to search for
5903 * This returns a pointer to the command line that has the given
5906 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
5910 comm = find_cmdline(tep, pid);
5914 static struct tep_cmdline *
5915 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
5917 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5920 cmdlist = cmdlist->next;
5922 cmdlist = tep->cmdlist;
5924 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5925 cmdlist = cmdlist->next;
5927 return (struct tep_cmdline *)cmdlist;
5931 * tep_data_pid_from_comm - return the pid from a given comm
5932 * @tep: a handle to the trace event parser context
5933 * @comm: the cmdline to find the pid from
5934 * @next: the cmdline structure to find the next comm
5936 * This returns the cmdline structure that holds a pid for a given
5937 * comm, or NULL if none found. As there may be more than one pid for
5938 * a given comm, the result of this call can be passed back into
5939 * a recurring call in the @next parameter, and then it will find the
5941 * Also, it does a linear search, so it may be slow.
5943 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
5944 struct tep_cmdline *next)
5946 struct tep_cmdline *cmdline;
5949 * If the cmdlines have not been converted yet, then use
5953 return pid_from_cmdlist(tep, comm, next);
5957 * The next pointer could have been still from
5958 * a previous call before cmdlines were created
5960 if (next < tep->cmdlines ||
5961 next >= tep->cmdlines + tep->cmdline_count)
5968 cmdline = tep->cmdlines;
5970 while (cmdline < tep->cmdlines + tep->cmdline_count) {
5971 if (strcmp(cmdline->comm, comm) == 0)
5979 * tep_cmdline_pid - return the pid associated to a given cmdline
5980 * @tep: a handle to the trace event parser context
5981 * @cmdline: The cmdline structure to get the pid from
5983 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5986 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
5988 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5994 * If cmdlines have not been created yet, or cmdline is
5995 * not part of the array, then treat it as a cmdlist instead.
5997 if (!tep->cmdlines ||
5998 cmdline < tep->cmdlines ||
5999 cmdline >= tep->cmdlines + tep->cmdline_count)
6000 return cmdlist->pid;
6002 return cmdline->pid;
6006 * This parses the raw @data using the given @event information and
6007 * writes the print format into the trace_seq.
6009 static void print_event_info(struct trace_seq *s, char *format, bool raw,
6010 struct tep_event *event, struct tep_record *record)
6012 int print_pretty = 1;
6014 if (raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
6015 tep_print_fields(s, record->data, record->size, event);
6018 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
6019 print_pretty = event->handler(s, record, event,
6023 pretty_print(s, record->data, record->size, event);
6026 trace_seq_terminate(s);
6030 * tep_find_event_by_record - return the event from a given record
6031 * @tep: a handle to the trace event parser context
6032 * @record: The record to get the event from
6034 * Returns the associated event for a given record, or NULL if non is
6038 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
6042 if (record->size < 0) {
6043 do_warning("ug! negative record size %d", record->size);
6047 type = trace_parse_common_type(tep, record->data);
6049 return tep_find_event(tep, type);
6053 * Writes the timestamp of the record into @s. Time divisor and precision can be
6054 * specified as part of printf @format string. Example:
6055 * "%3.1000d" - divide the time by 1000 and print the first 3 digits
6056 * before the dot. Thus, the timestamp "123456000" will be printed as
6059 static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
6060 char *format, struct tep_event *event,
6061 struct tep_record *record)
6063 unsigned long long time;
6069 if (isdigit(*(format + 1)))
6070 prec = atoi(format + 1);
6071 divstr = strchr(format, '.');
6072 if (divstr && isdigit(*(divstr + 1)))
6073 div = atoi(divstr + 1);
6083 if (p10 > 1 && p10 < time)
6084 trace_seq_printf(s, "%5llu.%0*llu", time / p10, prec, time % p10);
6086 trace_seq_printf(s, "%12llu", time);
6089 struct print_event_type {
6098 static void print_string(struct tep_handle *tep, struct trace_seq *s,
6099 struct tep_record *record, struct tep_event *event,
6100 const char *arg, struct print_event_type *type)
6105 if (strncmp(arg, TEP_PRINT_LATENCY, strlen(TEP_PRINT_LATENCY)) == 0) {
6106 data_latency_format(tep, s, type->format, record);
6107 } else if (strncmp(arg, TEP_PRINT_COMM, strlen(TEP_PRINT_COMM)) == 0) {
6108 pid = parse_common_pid(tep, record->data);
6109 comm = find_cmdline(tep, pid);
6110 trace_seq_printf(s, type->format, comm);
6111 } else if (strncmp(arg, TEP_PRINT_INFO_RAW, strlen(TEP_PRINT_INFO_RAW)) == 0) {
6112 print_event_info(s, type->format, true, event, record);
6113 } else if (strncmp(arg, TEP_PRINT_INFO, strlen(TEP_PRINT_INFO)) == 0) {
6114 print_event_info(s, type->format, false, event, record);
6115 } else if (strncmp(arg, TEP_PRINT_NAME, strlen(TEP_PRINT_NAME)) == 0) {
6116 trace_seq_printf(s, type->format, event->name);
6118 trace_seq_printf(s, "[UNKNOWN TEP TYPE %s]", arg);
6123 static void print_int(struct tep_handle *tep, struct trace_seq *s,
6124 struct tep_record *record, struct tep_event *event,
6125 int arg, struct print_event_type *type)
6131 param = record->cpu;
6134 param = parse_common_pid(tep, record->data);
6136 case TEP_PRINT_TIME:
6137 return print_event_time(tep, s, type->format, event, record);
6141 trace_seq_printf(s, type->format, param);
6144 static int tep_print_event_param_type(char *format,
6145 struct print_event_type *type)
6147 char *str = format + 1;
6150 type->type = EVENT_TYPE_UNKNOWN;
6159 type->type = EVENT_TYPE_INT;
6162 type->type = EVENT_TYPE_STRING;
6167 if (type->type != EVENT_TYPE_UNKNOWN)
6170 memset(type->format, 0, 32);
6171 memcpy(type->format, format, i < 32 ? i : 31);
6176 * tep_print_event - Write various event information
6177 * @tep: a handle to the trace event parser context
6178 * @s: the trace_seq to write to
6179 * @record: The record to get the event from
6180 * @format: a printf format string. Supported event fileds:
6181 * TEP_PRINT_PID, "%d" - event PID
6182 * TEP_PRINT_CPU, "%d" - event CPU
6183 * TEP_PRINT_COMM, "%s" - event command string
6184 * TEP_PRINT_NAME, "%s" - event name
6185 * TEP_PRINT_LATENCY, "%s" - event latency
6186 * TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
6187 * can be specified as part of this format string:
6188 * "%precision.divisord". Example:
6189 * "%3.1000d" - divide the time by 1000 and print the first
6190 * 3 digits before the dot. Thus, the time stamp
6191 * "123456000" will be printed as "123.456"
6192 * TEP_PRINT_INFO, "%s" - event information. If any width is specified in
6193 * the format string, the event information will be printed
6195 * Writes the specified event information into @s.
6197 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
6198 struct tep_record *record, const char *fmt, ...)
6200 struct print_event_type type;
6201 char *format = strdup(fmt);
6202 char *current = format;
6206 struct tep_event *event;
6211 event = tep_find_event_by_record(tep, record);
6212 va_start(args, fmt);
6214 current = strchr(str, '%');
6216 trace_seq_puts(s, str);
6219 memset(&type, 0, sizeof(type));
6220 offset = tep_print_event_param_type(current, &type);
6222 trace_seq_puts(s, str);
6224 switch (type.type) {
6225 case EVENT_TYPE_STRING:
6226 print_string(tep, s, record, event,
6227 va_arg(args, char*), &type);
6229 case EVENT_TYPE_INT:
6230 print_int(tep, s, record, event,
6231 va_arg(args, int), &type);
6233 case EVENT_TYPE_UNKNOWN:
6235 trace_seq_printf(s, "[UNKNOWN TYPE]");
6245 static int events_id_cmp(const void *a, const void *b)
6247 struct tep_event * const * ea = a;
6248 struct tep_event * const * eb = b;
6250 if ((*ea)->id < (*eb)->id)
6253 if ((*ea)->id > (*eb)->id)
6259 static int events_name_cmp(const void *a, const void *b)
6261 struct tep_event * const * ea = a;
6262 struct tep_event * const * eb = b;
6265 res = strcmp((*ea)->name, (*eb)->name);
6269 res = strcmp((*ea)->system, (*eb)->system);
6273 return events_id_cmp(a, b);
6276 static int events_system_cmp(const void *a, const void *b)
6278 struct tep_event * const * ea = a;
6279 struct tep_event * const * eb = b;
6282 res = strcmp((*ea)->system, (*eb)->system);
6286 res = strcmp((*ea)->name, (*eb)->name);
6290 return events_id_cmp(a, b);
6293 static struct tep_event **list_events_copy(struct tep_handle *tep)
6295 struct tep_event **events;
6300 events = malloc(sizeof(*events) * (tep->nr_events + 1));
6304 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
6305 events[tep->nr_events] = NULL;
6309 static void list_events_sort(struct tep_event **events, int nr_events,
6310 enum tep_event_sort_type sort_type)
6312 int (*sort)(const void *a, const void *b);
6314 switch (sort_type) {
6315 case TEP_EVENT_SORT_ID:
6316 sort = events_id_cmp;
6318 case TEP_EVENT_SORT_NAME:
6319 sort = events_name_cmp;
6321 case TEP_EVENT_SORT_SYSTEM:
6322 sort = events_system_cmp;
6329 qsort(events, nr_events, sizeof(*events), sort);
6333 * tep_list_events - Get events, sorted by given criteria.
6334 * @tep: a handle to the tep context
6335 * @sort_type: desired sort order of the events in the array
6337 * Returns an array of pointers to all events, sorted by the given
6338 * @sort_type criteria. The last element of the array is NULL. The returned
6339 * memory must not be freed, it is managed by the library.
6340 * The function is not thread safe.
6342 struct tep_event **tep_list_events(struct tep_handle *tep,
6343 enum tep_event_sort_type sort_type)
6345 struct tep_event **events;
6350 events = tep->sort_events;
6351 if (events && tep->last_type == sort_type)
6355 events = list_events_copy(tep);
6359 tep->sort_events = events;
6361 /* the internal events are sorted by id */
6362 if (sort_type == TEP_EVENT_SORT_ID) {
6363 tep->last_type = sort_type;
6368 list_events_sort(events, tep->nr_events, sort_type);
6369 tep->last_type = sort_type;
6376 * tep_list_events_copy - Thread safe version of tep_list_events()
6377 * @tep: a handle to the tep context
6378 * @sort_type: desired sort order of the events in the array
6380 * Returns an array of pointers to all events, sorted by the given
6381 * @sort_type criteria. The last element of the array is NULL. The returned
6382 * array is newly allocated inside the function and must be freed by the caller
6384 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
6385 enum tep_event_sort_type sort_type)
6387 struct tep_event **events;
6392 events = list_events_copy(tep);
6396 /* the internal events are sorted by id */
6397 if (sort_type == TEP_EVENT_SORT_ID)
6400 list_events_sort(events, tep->nr_events, sort_type);
6405 static struct tep_format_field **
6406 get_event_fields(const char *type, const char *name,
6407 int count, struct tep_format_field *list)
6409 struct tep_format_field **fields;
6410 struct tep_format_field *field;
6413 fields = malloc(sizeof(*fields) * (count + 1));
6417 for (field = list; field; field = field->next) {
6418 fields[i++] = field;
6419 if (i == count + 1) {
6420 do_warning("event %s has more %s fields than specified",
6428 do_warning("event %s has less %s fields than specified",
6437 * tep_event_common_fields - return a list of common fields for an event
6438 * @event: the event to return the common fields of.
6440 * Returns an allocated array of fields. The last item in the array is NULL.
6441 * The array must be freed with free().
6443 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
6445 return get_event_fields("common", event->name,
6446 event->format.nr_common,
6447 event->format.common_fields);
6451 * tep_event_fields - return a list of event specific fields for an event
6452 * @event: the event to return the fields of.
6454 * Returns an allocated array of fields. The last item in the array is NULL.
6455 * The array must be freed with free().
6457 struct tep_format_field **tep_event_fields(struct tep_event *event)
6459 return get_event_fields("event", event->name,
6460 event->format.nr_fields,
6461 event->format.fields);
6464 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
6466 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
6468 trace_seq_puts(s, ", ");
6469 print_fields(s, field->next);
6474 static void print_args(struct tep_print_arg *args)
6476 int print_paren = 1;
6479 switch (args->type) {
6480 case TEP_PRINT_NULL:
6483 case TEP_PRINT_ATOM:
6484 printf("%s", args->atom.atom);
6486 case TEP_PRINT_FIELD:
6487 printf("REC->%s", args->field.name);
6489 case TEP_PRINT_FLAGS:
6490 printf("__print_flags(");
6491 print_args(args->flags.field);
6492 printf(", %s, ", args->flags.delim);
6494 print_fields(&s, args->flags.flags);
6495 trace_seq_do_printf(&s);
6496 trace_seq_destroy(&s);
6499 case TEP_PRINT_SYMBOL:
6500 printf("__print_symbolic(");
6501 print_args(args->symbol.field);
6504 print_fields(&s, args->symbol.symbols);
6505 trace_seq_do_printf(&s);
6506 trace_seq_destroy(&s);
6510 printf("__print_hex(");
6511 print_args(args->hex.field);
6513 print_args(args->hex.size);
6516 case TEP_PRINT_HEX_STR:
6517 printf("__print_hex_str(");
6518 print_args(args->hex.field);
6520 print_args(args->hex.size);
6523 case TEP_PRINT_INT_ARRAY:
6524 printf("__print_array(");
6525 print_args(args->int_array.field);
6527 print_args(args->int_array.count);
6529 print_args(args->int_array.el_size);
6532 case TEP_PRINT_STRING:
6533 case TEP_PRINT_BSTRING:
6534 printf("__get_str(%s)", args->string.string);
6536 case TEP_PRINT_BITMASK:
6537 printf("__get_bitmask(%s)", args->bitmask.bitmask);
6539 case TEP_PRINT_TYPE:
6540 printf("(%s)", args->typecast.type);
6541 print_args(args->typecast.item);
6544 if (strcmp(args->op.op, ":") == 0)
6548 print_args(args->op.left);
6549 printf(" %s ", args->op.op);
6550 print_args(args->op.right);
6555 /* we should warn... */
6560 print_args(args->next);
6564 static void parse_header_field(const char *field,
6565 int *offset, int *size, int mandatory)
6567 unsigned long long save_input_buf_ptr;
6568 unsigned long long save_input_buf_siz;
6572 save_input_buf_ptr = input_buf_ptr;
6573 save_input_buf_siz = input_buf_siz;
6575 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
6577 if (read_expected(TEP_EVENT_OP, ":") < 0)
6581 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6586 * If this is not a mandatory field, then test it first.
6589 if (read_expected(TEP_EVENT_ITEM, field) < 0)
6592 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6594 if (strcmp(token, field) != 0)
6599 if (read_expected(TEP_EVENT_OP, ";") < 0)
6601 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
6603 if (read_expected(TEP_EVENT_OP, ":") < 0)
6605 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6607 *offset = atoi(token);
6609 if (read_expected(TEP_EVENT_OP, ";") < 0)
6611 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
6613 if (read_expected(TEP_EVENT_OP, ":") < 0)
6615 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6617 *size = atoi(token);
6619 if (read_expected(TEP_EVENT_OP, ";") < 0)
6621 type = read_token(&token);
6622 if (type != TEP_EVENT_NEWLINE) {
6623 /* newer versions of the kernel have a "signed" type */
6624 if (type != TEP_EVENT_ITEM)
6627 if (strcmp(token, "signed") != 0)
6632 if (read_expected(TEP_EVENT_OP, ":") < 0)
6635 if (read_expect_type(TEP_EVENT_ITEM, &token))
6639 if (read_expected(TEP_EVENT_OP, ";") < 0)
6642 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
6650 input_buf_ptr = save_input_buf_ptr;
6651 input_buf_siz = save_input_buf_siz;
6658 * tep_parse_header_page - parse the data stored in the header page
6659 * @tep: a handle to the trace event parser context
6660 * @buf: the buffer storing the header page format string
6661 * @size: the size of @buf
6662 * @long_size: the long size to use if there is no header
6664 * This parses the header page format for information on the
6665 * ring buffer used. The @buf should be copied from
6667 * /sys/kernel/debug/tracing/events/header_page
6669 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
6676 * Old kernels did not have header page info.
6677 * Sorry but we just use what we find here in user space.
6679 tep->header_page_ts_size = sizeof(long long);
6680 tep->header_page_size_size = long_size;
6681 tep->header_page_data_offset = sizeof(long long) + long_size;
6682 tep->old_format = 1;
6685 init_input_buf(buf, size);
6687 parse_header_field("timestamp", &tep->header_page_ts_offset,
6688 &tep->header_page_ts_size, 1);
6689 parse_header_field("commit", &tep->header_page_size_offset,
6690 &tep->header_page_size_size, 1);
6691 parse_header_field("overwrite", &tep->header_page_overwrite,
6693 parse_header_field("data", &tep->header_page_data_offset,
6694 &tep->header_page_data_size, 1);
6699 static int event_matches(struct tep_event *event,
6700 int id, const char *sys_name,
6701 const char *event_name)
6703 if (id >= 0 && id != event->id)
6706 if (event_name && (strcmp(event_name, event->name) != 0))
6709 if (sys_name && (strcmp(sys_name, event->system) != 0))
6715 static void free_handler(struct event_handler *handle)
6717 free((void *)handle->sys_name);
6718 free((void *)handle->event_name);
6722 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
6724 struct event_handler *handle, **next;
6726 for (next = &tep->handlers; *next;
6727 next = &(*next)->next) {
6729 if (event_matches(event, handle->id,
6731 handle->event_name))
6738 pr_stat("overriding event (%d) %s:%s with new print handler",
6739 event->id, event->system, event->name);
6741 event->handler = handle->func;
6742 event->context = handle->context;
6744 *next = handle->next;
6745 free_handler(handle);
6751 * parse_format - parse the event format
6752 * @buf: the buffer storing the event format string
6753 * @size: the size of @buf
6754 * @sys: the system the event belongs to
6756 * This parses the event format and creates an event structure
6757 * to quickly parse raw data for a given event.
6759 * These files currently come from:
6761 * /sys/kernel/debug/tracing/events/.../.../format
6763 static enum tep_errno parse_format(struct tep_event **eventp,
6764 struct tep_handle *tep, const char *buf,
6765 unsigned long size, const char *sys)
6767 struct tep_event *event;
6770 init_input_buf(buf, size);
6772 *eventp = event = alloc_event();
6774 return TEP_ERRNO__MEM_ALLOC_FAILED;
6776 event->name = event_read_name();
6779 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6780 goto event_alloc_failed;
6783 if (strcmp(sys, "ftrace") == 0) {
6784 event->flags |= TEP_EVENT_FL_ISFTRACE;
6786 if (strcmp(event->name, "bprint") == 0)
6787 event->flags |= TEP_EVENT_FL_ISBPRINT;
6790 event->id = event_read_id();
6791 if (event->id < 0) {
6792 ret = TEP_ERRNO__READ_ID_FAILED;
6794 * This isn't an allocation error actually.
6795 * But as the ID is critical, just bail out.
6797 goto event_alloc_failed;
6800 event->system = strdup(sys);
6801 if (!event->system) {
6802 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6803 goto event_alloc_failed;
6806 /* Add tep to event so that it can be referenced */
6809 ret = event_read_format(event);
6811 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6812 goto event_parse_failed;
6816 * If the event has an override, don't print warnings if the event
6817 * print format fails to parse.
6819 if (tep && find_event_handle(tep, event))
6822 ret = event_read_print(event);
6826 ret = TEP_ERRNO__READ_PRINT_FAILED;
6827 goto event_parse_failed;
6830 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6831 struct tep_format_field *field;
6832 struct tep_print_arg *arg, **list;
6834 /* old ftrace had no args */
6835 list = &event->print_fmt.args;
6836 for (field = event->format.fields; field; field = field->next) {
6839 event->flags |= TEP_EVENT_FL_FAILED;
6840 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6842 arg->type = TEP_PRINT_FIELD;
6843 arg->field.name = strdup(field->name);
6844 if (!arg->field.name) {
6845 event->flags |= TEP_EVENT_FL_FAILED;
6847 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6849 arg->field.field = field;
6855 if (!(event->flags & TEP_EVENT_FL_ISBPRINT))
6856 event->print_fmt.print_cache = parse_args(event,
6857 event->print_fmt.format,
6858 event->print_fmt.args);
6863 event->flags |= TEP_EVENT_FL_FAILED;
6867 free(event->system);
6874 static enum tep_errno
6875 __parse_event(struct tep_handle *tep,
6876 struct tep_event **eventp,
6877 const char *buf, unsigned long size,
6880 int ret = parse_format(eventp, tep, buf, size, sys);
6881 struct tep_event *event = *eventp;
6886 if (tep && add_event(tep, event)) {
6887 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6888 goto event_add_failed;
6891 #define PRINT_ARGS 0
6892 if (PRINT_ARGS && event->print_fmt.args)
6893 print_args(event->print_fmt.args);
6898 free_tep_event(event);
6903 * tep_parse_format - parse the event format
6904 * @tep: a handle to the trace event parser context
6905 * @eventp: returned format
6906 * @buf: the buffer storing the event format string
6907 * @size: the size of @buf
6908 * @sys: the system the event belongs to
6910 * This parses the event format and creates an event structure
6911 * to quickly parse raw data for a given event.
6913 * These files currently come from:
6915 * /sys/kernel/debug/tracing/events/.../.../format
6917 enum tep_errno tep_parse_format(struct tep_handle *tep,
6918 struct tep_event **eventp,
6920 unsigned long size, const char *sys)
6922 return __parse_event(tep, eventp, buf, size, sys);
6926 * tep_parse_event - parse the event format
6927 * @tep: a handle to the trace event parser context
6928 * @buf: the buffer storing the event format string
6929 * @size: the size of @buf
6930 * @sys: the system the event belongs to
6932 * This parses the event format and creates an event structure
6933 * to quickly parse raw data for a given event.
6935 * These files currently come from:
6937 * /sys/kernel/debug/tracing/events/.../.../format
6939 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
6940 unsigned long size, const char *sys)
6942 struct tep_event *event = NULL;
6943 return __parse_event(tep, &event, buf, size, sys);
6946 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6947 const char *name, struct tep_record *record,
6948 unsigned long long *val, int err)
6952 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6956 if (tep_read_number_field(field, record->data, val)) {
6958 trace_seq_printf(s, " %s=INVALID", name);
6966 * tep_get_field_raw - return the raw pointer into the data field
6967 * @s: The seq to print to on error
6968 * @event: the event that the field is for
6969 * @name: The name of the field
6970 * @record: The record with the field name.
6971 * @len: place to store the field length.
6972 * @err: print default error if failed.
6974 * Returns a pointer into record->data of the field and places
6975 * the length of the field in @len.
6977 * On failure, it returns NULL.
6979 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6980 const char *name, struct tep_record *record,
6983 struct tep_format_field *field;
6984 void *data = record->data;
6991 field = tep_find_field(event, name);
6995 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6999 /* Allow @len to be NULL */
7003 offset = field->offset;
7004 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
7005 offset = tep_read_number(event->tep,
7006 data + offset, field->size);
7007 *len = offset >> 16;
7009 if (field->flags & TEP_FIELD_IS_RELATIVE)
7010 offset += field->offset + field->size;
7014 return data + offset;
7018 * tep_get_field_val - find a field and return its value
7019 * @s: The seq to print to on error
7020 * @event: the event that the field is for
7021 * @name: The name of the field
7022 * @record: The record with the field name.
7023 * @val: place to store the value of the field.
7024 * @err: print default error if failed.
7026 * Returns 0 on success -1 on field not found.
7028 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
7029 const char *name, struct tep_record *record,
7030 unsigned long long *val, int err)
7032 struct tep_format_field *field;
7037 field = tep_find_field(event, name);
7039 return get_field_val(s, field, name, record, val, err);
7043 * tep_get_common_field_val - find a common field and return its value
7044 * @s: The seq to print to on error
7045 * @event: the event that the field is for
7046 * @name: The name of the field
7047 * @record: The record with the field name.
7048 * @val: place to store the value of the field.
7049 * @err: print default error if failed.
7051 * Returns 0 on success -1 on field not found.
7053 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
7054 const char *name, struct tep_record *record,
7055 unsigned long long *val, int err)
7057 struct tep_format_field *field;
7062 field = tep_find_common_field(event, name);
7064 return get_field_val(s, field, name, record, val, err);
7068 * tep_get_any_field_val - find a any field and return its value
7069 * @s: The seq to print to on error
7070 * @event: the event that the field is for
7071 * @name: The name of the field
7072 * @record: The record with the field name.
7073 * @val: place to store the value of the field.
7074 * @err: print default error if failed.
7076 * Returns 0 on success -1 on field not found.
7078 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
7079 const char *name, struct tep_record *record,
7080 unsigned long long *val, int err)
7082 struct tep_format_field *field;
7087 field = tep_find_any_field(event, name);
7089 return get_field_val(s, field, name, record, val, err);
7093 * tep_print_num_field - print a field and a format
7094 * @s: The seq to print to
7095 * @fmt: The printf format to print the field with.
7096 * @event: the event that the field is for
7097 * @name: The name of the field
7098 * @record: The record with the field name.
7099 * @err: print default error if failed.
7101 * Returns positive value on success, negative in case of an error,
7102 * or 0 if buffer is full.
7104 int tep_print_num_field(struct trace_seq *s, const char *fmt,
7105 struct tep_event *event, const char *name,
7106 struct tep_record *record, int err)
7108 struct tep_format_field *field = tep_find_field(event, name);
7109 unsigned long long val;
7114 if (tep_read_number_field(field, record->data, &val))
7117 return trace_seq_printf(s, fmt, val);
7121 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
7126 * tep_print_func_field - print a field and a format for function pointers
7127 * @s: The seq to print to
7128 * @fmt: The printf format to print the field with.
7129 * @event: the event that the field is for
7130 * @name: The name of the field
7131 * @record: The record with the field name.
7132 * @err: print default error if failed.
7134 * Returns positive value on success, negative in case of an error,
7135 * or 0 if buffer is full.
7137 int tep_print_func_field(struct trace_seq *s, const char *fmt,
7138 struct tep_event *event, const char *name,
7139 struct tep_record *record, int err)
7141 struct tep_format_field *field = tep_find_field(event, name);
7142 struct tep_handle *tep = event->tep;
7143 unsigned long long val;
7144 struct func_map *func;
7150 if (tep_read_number_field(field, record->data, &val))
7153 func = find_func(tep, val);
7156 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
7158 sprintf(tmp, "0x%08llx", val);
7160 return trace_seq_printf(s, fmt, tmp);
7164 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
7168 static void free_func_handle(struct tep_function_handler *func)
7170 struct func_params *params;
7174 while (func->params) {
7175 params = func->params;
7176 func->params = params->next;
7184 * tep_register_print_function - register a helper function
7185 * @tep: a handle to the trace event parser context
7186 * @func: the function to process the helper function
7187 * @ret_type: the return type of the helper function
7188 * @name: the name of the helper function
7189 * @parameters: A list of enum tep_func_arg_type
7191 * Some events may have helper functions in the print format arguments.
7192 * This allows a plugin to dynamically create a way to process one
7193 * of these functions.
7195 * The @parameters is a variable list of tep_func_arg_type enums that
7196 * must end with TEP_FUNC_ARG_VOID.
7198 int tep_register_print_function(struct tep_handle *tep,
7199 tep_func_handler func,
7200 enum tep_func_arg_type ret_type,
7203 struct tep_function_handler *func_handle;
7204 struct func_params **next_param;
7205 struct func_params *param;
7206 enum tep_func_arg_type type;
7210 func_handle = find_func_handler(tep, name);
7213 * This is most like caused by the users own
7214 * plugins updating the function. This overrides the
7217 pr_stat("override of function helper '%s'", name);
7218 remove_func_handler(tep, name);
7221 func_handle = calloc(1, sizeof(*func_handle));
7223 do_warning("Failed to allocate function handler");
7224 return TEP_ERRNO__MEM_ALLOC_FAILED;
7227 func_handle->ret_type = ret_type;
7228 func_handle->name = strdup(name);
7229 func_handle->func = func;
7230 if (!func_handle->name) {
7231 do_warning("Failed to allocate function name");
7233 return TEP_ERRNO__MEM_ALLOC_FAILED;
7236 next_param = &(func_handle->params);
7239 type = va_arg(ap, enum tep_func_arg_type);
7240 if (type == TEP_FUNC_ARG_VOID)
7243 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
7244 do_warning("Invalid argument type %d", type);
7245 ret = TEP_ERRNO__INVALID_ARG_TYPE;
7249 param = malloc(sizeof(*param));
7251 do_warning("Failed to allocate function param");
7252 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7258 *next_param = param;
7259 next_param = &(param->next);
7261 func_handle->nr_args++;
7265 func_handle->next = tep->func_handlers;
7266 tep->func_handlers = func_handle;
7271 free_func_handle(func_handle);
7276 * tep_unregister_print_function - unregister a helper function
7277 * @tep: a handle to the trace event parser context
7278 * @func: the function to process the helper function
7279 * @name: the name of the helper function
7281 * This function removes existing print handler for function @name.
7283 * Returns 0 if the handler was removed successully, -1 otherwise.
7285 int tep_unregister_print_function(struct tep_handle *tep,
7286 tep_func_handler func, char *name)
7288 struct tep_function_handler *func_handle;
7290 func_handle = find_func_handler(tep, name);
7291 if (func_handle && func_handle->func == func) {
7292 remove_func_handler(tep, name);
7298 static struct tep_event *search_event(struct tep_handle *tep, int id,
7299 const char *sys_name,
7300 const char *event_name)
7302 struct tep_event *event;
7306 event = tep_find_event(tep, id);
7309 if (event_name && (strcmp(event_name, event->name) != 0))
7311 if (sys_name && (strcmp(sys_name, event->system) != 0))
7314 event = tep_find_event_by_name(tep, sys_name, event_name);
7322 * tep_register_event_handler - register a way to parse an event
7323 * @tep: a handle to the trace event parser context
7324 * @id: the id of the event to register
7325 * @sys_name: the system name the event belongs to
7326 * @event_name: the name of the event
7327 * @func: the function to call to parse the event information
7328 * @context: the data to be passed to @func
7330 * This function allows a developer to override the parsing of
7331 * a given event. If for some reason the default print format
7332 * is not sufficient, this function will register a function
7333 * for an event to be used to parse the data instead.
7335 * If @id is >= 0, then it is used to find the event.
7336 * else @sys_name and @event_name are used.
7339 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
7340 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
7341 * negative TEP_ERRNO_... in case of an error
7344 int tep_register_event_handler(struct tep_handle *tep, int id,
7345 const char *sys_name, const char *event_name,
7346 tep_event_handler_func func, void *context)
7348 struct tep_event *event;
7349 struct event_handler *handle;
7351 event = search_event(tep, id, sys_name, event_name);
7355 pr_stat("overriding event (%d) %s:%s with new print handler",
7356 event->id, event->system, event->name);
7358 event->handler = func;
7359 event->context = context;
7360 return TEP_REGISTER_SUCCESS_OVERWRITE;
7363 /* Save for later use. */
7364 handle = calloc(1, sizeof(*handle));
7366 do_warning("Failed to allocate event handler");
7367 return TEP_ERRNO__MEM_ALLOC_FAILED;
7372 handle->event_name = strdup(event_name);
7374 handle->sys_name = strdup(sys_name);
7376 if ((event_name && !handle->event_name) ||
7377 (sys_name && !handle->sys_name)) {
7378 do_warning("Failed to allocate event/sys name");
7379 free((void *)handle->event_name);
7380 free((void *)handle->sys_name);
7382 return TEP_ERRNO__MEM_ALLOC_FAILED;
7385 handle->func = func;
7386 handle->next = tep->handlers;
7387 tep->handlers = handle;
7388 handle->context = context;
7390 return TEP_REGISTER_SUCCESS;
7393 static int handle_matches(struct event_handler *handler, int id,
7394 const char *sys_name, const char *event_name,
7395 tep_event_handler_func func, void *context)
7397 if (id >= 0 && id != handler->id)
7400 if (event_name && (strcmp(event_name, handler->event_name) != 0))
7403 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
7406 if (func != handler->func || context != handler->context)
7413 * tep_unregister_event_handler - unregister an existing event handler
7414 * @tep: a handle to the trace event parser context
7415 * @id: the id of the event to unregister
7416 * @sys_name: the system name the handler belongs to
7417 * @event_name: the name of the event handler
7418 * @func: the function to call to parse the event information
7419 * @context: the data to be passed to @func
7421 * This function removes existing event handler (parser).
7423 * If @id is >= 0, then it is used to find the event.
7424 * else @sys_name and @event_name are used.
7426 * Returns 0 if handler was removed successfully, -1 if event was not found.
7428 int tep_unregister_event_handler(struct tep_handle *tep, int id,
7429 const char *sys_name, const char *event_name,
7430 tep_event_handler_func func, void *context)
7432 struct tep_event *event;
7433 struct event_handler *handle;
7434 struct event_handler **next;
7436 event = search_event(tep, id, sys_name, event_name);
7440 if (event->handler == func && event->context == context) {
7441 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
7442 event->id, event->system, event->name);
7444 event->handler = NULL;
7445 event->context = NULL;
7450 for (next = &tep->handlers; *next; next = &(*next)->next) {
7452 if (handle_matches(handle, id, sys_name, event_name,
7460 *next = handle->next;
7461 free_handler(handle);
7467 * tep_alloc - create a tep handle
7469 struct tep_handle *tep_alloc(void)
7471 struct tep_handle *tep = calloc(1, sizeof(*tep));
7475 tep->host_bigendian = tep_is_bigendian();
7481 void tep_ref(struct tep_handle *tep)
7486 int tep_get_ref(struct tep_handle *tep)
7489 return tep->ref_count;
7493 __hidden void free_tep_format_field(struct tep_format_field *field)
7496 if (field->alias != field->name)
7502 static void free_format_fields(struct tep_format_field *field)
7504 struct tep_format_field *next;
7508 free_tep_format_field(field);
7513 static void free_formats(struct tep_format *format)
7515 free_format_fields(format->common_fields);
7516 free_format_fields(format->fields);
7519 __hidden void free_tep_event(struct tep_event *event)
7522 free(event->system);
7524 free_formats(&event->format);
7526 free(event->print_fmt.format);
7527 free_args(event->print_fmt.args);
7528 free_parse_args(event->print_fmt.print_cache);
7533 * tep_free - free a tep handle
7534 * @tep: the tep handle to free
7536 void tep_free(struct tep_handle *tep)
7538 struct cmdline_list *cmdlist, *cmdnext;
7539 struct func_list *funclist, *funcnext;
7540 struct printk_list *printklist, *printknext;
7541 struct tep_function_handler *func_handler;
7542 struct event_handler *handle;
7548 cmdlist = tep->cmdlist;
7549 funclist = tep->funclist;
7550 printklist = tep->printklist;
7556 if (tep->cmdlines) {
7557 for (i = 0; i < tep->cmdline_count; i++)
7558 free(tep->cmdlines[i].comm);
7559 free(tep->cmdlines);
7563 cmdnext = cmdlist->next;
7564 free(cmdlist->comm);
7569 if (tep->func_map) {
7570 for (i = 0; i < (int)tep->func_count; i++) {
7571 free(tep->func_map[i].func);
7572 free(tep->func_map[i].mod);
7574 free(tep->func_map);
7578 funcnext = funclist->next;
7579 free(funclist->func);
7580 free(funclist->mod);
7582 funclist = funcnext;
7585 while (tep->func_handlers) {
7586 func_handler = tep->func_handlers;
7587 tep->func_handlers = func_handler->next;
7588 free_func_handle(func_handler);
7591 if (tep->printk_map) {
7592 for (i = 0; i < (int)tep->printk_count; i++)
7593 free(tep->printk_map[i].printk);
7594 free(tep->printk_map);
7597 while (printklist) {
7598 printknext = printklist->next;
7599 free(printklist->printk);
7601 printklist = printknext;
7604 for (i = 0; i < tep->nr_events; i++)
7605 free_tep_event(tep->events[i]);
7607 while (tep->handlers) {
7608 handle = tep->handlers;
7609 tep->handlers = handle->next;
7610 free_handler(handle);
7614 free(tep->sort_events);
7615 free(tep->func_resolver);
7616 free_tep_plugin_paths(tep);
7621 void tep_unref(struct tep_handle *tep)