1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
21 #include <linux/time64.h>
23 #include <netinet/in.h>
24 #include "event-parse.h"
26 #include "event-parse-local.h"
27 #include "event-utils.h"
28 #include "trace-seq.h"
30 static const char *input_buf;
31 static unsigned long long input_buf_ptr;
32 static unsigned long long input_buf_siz;
34 static int is_flag_field;
35 static int is_symbolic_field;
37 static int show_warning = 1;
39 #define do_warning(fmt, ...) \
42 warning(fmt, ##__VA_ARGS__); \
45 #define do_warning_event(event, fmt, ...) \
51 warning("[%s:%s] " fmt, event->system, \
52 event->name, ##__VA_ARGS__); \
54 warning(fmt, ##__VA_ARGS__); \
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_long(struct tep_format_field *field)
1372 /* includes long long */
1373 if (strstr(field->type, "long"))
1379 static unsigned int type_size(const char *name)
1381 /* This covers all TEP_FIELD_IS_STRING types. */
1399 for (i = 0; table[i].type; i++) {
1400 if (!strcmp(table[i].type, name))
1401 return table[i].size;
1407 static int append(char **buf, const char *delim, const char *str)
1411 new_buf = realloc(*buf, strlen(*buf) + strlen(delim) + strlen(str) + 1);
1414 strcat(new_buf, delim);
1415 strcat(new_buf, str);
1420 static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
1422 struct tep_format_field *field = NULL;
1423 enum tep_event_type type;
1431 unsigned int size_dynamic = 0;
1433 type = read_token(&token);
1434 if (type == TEP_EVENT_NEWLINE) {
1441 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
1445 type = read_token(&token);
1447 * The ftrace fields may still use the "special" name.
1450 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
1451 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
1453 type = read_token(&token);
1456 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
1460 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
1465 field = calloc(1, sizeof(*field));
1469 field->event = event;
1471 /* read the rest of the type */
1473 type = read_token(&token);
1474 if (type == TEP_EVENT_ITEM ||
1475 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
1477 * Some of the ftrace fields are broken and have
1478 * an illegal "." in them.
1480 (event->flags & TEP_EVENT_FL_ISFTRACE &&
1481 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
1483 if (strcmp(token, "*") == 0)
1484 field->flags |= TEP_FIELD_IS_POINTER;
1487 ret = append(&field->type, delim, last_token);
1492 field->type = last_token;
1498 /* Handle __attribute__((user)) */
1499 if ((type == TEP_EVENT_DELIM) &&
1500 strcmp("__attribute__", last_token) == 0 &&
1505 ret = append(&field->type, " ", last_token);
1506 ret |= append(&field->type, "", "(");
1511 while ((type = read_token(&token)) != TEP_EVENT_NONE) {
1512 if (type == TEP_EVENT_DELIM) {
1513 if (token[0] == '(')
1515 else if (token[0] == ')')
1519 ret = append(&field->type, "", token);
1522 ret = append(&field->type, delim, token);
1536 do_warning_event(event, "%s: no type found", __func__);
1539 field->name = field->alias = last_token;
1541 if (test_type(type, TEP_EVENT_OP))
1544 if (strcmp(token, "[") == 0) {
1545 enum tep_event_type last_type = type;
1546 char *brackets = token;
1548 field->flags |= TEP_FIELD_IS_ARRAY;
1550 type = read_token(&token);
1552 if (type == TEP_EVENT_ITEM)
1553 field->arraylen = strtoul(token, NULL, 0);
1555 field->arraylen = 0;
1557 while (strcmp(token, "]") != 0) {
1560 if (last_type == TEP_EVENT_ITEM &&
1561 type == TEP_EVENT_ITEM)
1568 ret = append(&brackets, delim, token);
1573 /* We only care about the last token */
1574 field->arraylen = strtoul(token, NULL, 0);
1576 type = read_token(&token);
1577 if (type == TEP_EVENT_NONE) {
1579 do_warning_event(event, "failed to find token");
1586 ret = append(&brackets, "", "]");
1592 /* add brackets to type */
1594 type = read_token(&token);
1596 * If the next token is not an OP, then it is of
1597 * the format: type [] item;
1599 if (type == TEP_EVENT_ITEM) {
1600 ret = append(&field->type, " ", field->name);
1605 ret = append(&field->type, "", brackets);
1607 size_dynamic = type_size(field->name);
1608 free_token(field->name);
1609 field->name = field->alias = token;
1610 type = read_token(&token);
1612 ret = append(&field->type, "", brackets);
1621 if (field_is_string(field))
1622 field->flags |= TEP_FIELD_IS_STRING;
1623 if (field_is_dynamic(field))
1624 field->flags |= TEP_FIELD_IS_DYNAMIC;
1625 if (field_is_long(field))
1626 field->flags |= TEP_FIELD_IS_LONG;
1628 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
1632 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
1635 if (read_expected(TEP_EVENT_OP, ":") < 0)
1638 if (read_expect_type(TEP_EVENT_ITEM, &token))
1640 field->offset = strtoul(token, NULL, 0);
1643 if (read_expected(TEP_EVENT_OP, ";") < 0)
1646 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
1649 if (read_expected(TEP_EVENT_OP, ":") < 0)
1652 if (read_expect_type(TEP_EVENT_ITEM, &token))
1654 field->size = strtoul(token, NULL, 0);
1657 if (read_expected(TEP_EVENT_OP, ";") < 0)
1660 type = read_token(&token);
1661 if (type != TEP_EVENT_NEWLINE) {
1662 /* newer versions of the kernel have a "signed" type */
1663 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
1668 if (read_expected(TEP_EVENT_OP, ":") < 0)
1671 if (read_expect_type(TEP_EVENT_ITEM, &token))
1674 if (strtoul(token, NULL, 0))
1675 field->flags |= TEP_FIELD_IS_SIGNED;
1678 if (read_expected(TEP_EVENT_OP, ";") < 0)
1681 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1687 if (field->flags & TEP_FIELD_IS_ARRAY) {
1688 if (field->arraylen)
1689 field->elementsize = field->size / field->arraylen;
1690 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
1691 field->elementsize = size_dynamic;
1692 else if (field->flags & TEP_FIELD_IS_STRING)
1693 field->elementsize = 1;
1694 else if (field->flags & TEP_FIELD_IS_LONG)
1695 field->elementsize = event->tep ?
1696 event->tep->long_size :
1699 field->elementsize = field->size;
1702 fields = &field->next;
1719 static int event_read_format(struct tep_event *event)
1724 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
1727 if (read_expected(TEP_EVENT_OP, ":") < 0)
1730 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
1734 ret = event_read_fields(event, &event->format.common_fields);
1737 event->format.nr_common = ret;
1739 ret = event_read_fields(event, &event->format.fields);
1742 event->format.nr_fields = ret;
1751 static enum tep_event_type
1752 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
1753 char **tok, enum tep_event_type type);
1755 static enum tep_event_type
1756 process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1758 enum tep_event_type type;
1761 type = read_token(&token);
1764 return process_arg_token(event, arg, tok, type);
1767 static enum tep_event_type
1768 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
1771 * For __print_symbolic() and __print_flags, we need to completely
1772 * evaluate the first argument, which defines what to print next.
1774 static enum tep_event_type
1775 process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1777 enum tep_event_type type;
1779 type = process_arg(event, arg, tok);
1781 while (type == TEP_EVENT_OP) {
1782 type = process_op(event, arg, tok);
1788 static enum tep_event_type
1789 process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
1791 struct tep_print_arg *arg, *left, *right;
1792 enum tep_event_type type;
1797 right = alloc_arg();
1799 if (!arg || !left || !right) {
1800 do_warning_event(event, "%s: not enough memory!", __func__);
1801 /* arg will be freed at out_free */
1807 arg->type = TEP_PRINT_OP;
1808 arg->op.left = left;
1809 arg->op.right = right;
1812 type = process_arg(event, left, &token);
1815 if (type == TEP_EVENT_ERROR)
1818 /* Handle other operations in the arguments */
1819 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
1820 type = process_op(event, left, &token);
1824 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
1829 type = process_arg(event, right, &token);
1831 top->op.right = arg;
1837 /* Top may point to itself */
1838 top->op.right = NULL;
1841 return TEP_EVENT_ERROR;
1844 static enum tep_event_type
1845 process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
1847 struct tep_print_arg *arg;
1848 enum tep_event_type type;
1853 do_warning_event(event, "%s: not enough memory!", __func__);
1854 /* '*tok' is set to top->op.op. No need to free. */
1856 return TEP_EVENT_ERROR;
1860 type = process_arg(event, arg, &token);
1861 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
1864 top->op.right = arg;
1867 type = read_token_item(&token);
1875 return TEP_EVENT_ERROR;
1878 static int get_op_prio(char *op)
1892 /* '>>' and '<<' are 8 */
1896 /* '==' and '!=' are 10 */
1906 do_warning("unknown op '%c'", op[0]);
1910 if (strcmp(op, "++") == 0 ||
1911 strcmp(op, "--") == 0) {
1913 } else if (strcmp(op, ">>") == 0 ||
1914 strcmp(op, "<<") == 0) {
1916 } else if (strcmp(op, ">=") == 0 ||
1917 strcmp(op, "<=") == 0) {
1919 } else if (strcmp(op, "==") == 0 ||
1920 strcmp(op, "!=") == 0) {
1922 } else if (strcmp(op, "&&") == 0) {
1924 } else if (strcmp(op, "||") == 0) {
1927 do_warning("unknown op '%s'", op);
1933 static int set_op_prio(struct tep_print_arg *arg)
1936 /* single ops are the greatest */
1937 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
1940 arg->op.prio = get_op_prio(arg->op.op);
1942 return arg->op.prio;
1945 /* Note, *tok does not get freed, but will most likely be saved */
1946 static enum tep_event_type
1947 process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
1949 struct tep_print_arg *left, *right = NULL;
1950 enum tep_event_type type;
1953 /* the op is passed in via tok */
1956 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
1957 /* handle single op */
1959 do_warning_event(event, "bad op token %s", token);
1969 do_warning_event(event, "bad op token %s", token);
1974 /* make an empty left */
1979 left->type = TEP_PRINT_NULL;
1980 arg->op.left = left;
1982 right = alloc_arg();
1986 arg->op.right = right;
1988 /* do not free the token, it belongs to an op */
1990 type = process_arg(event, right, tok);
1992 } else if (strcmp(token, "?") == 0) {
1998 /* copy the top arg to the left */
2001 arg->type = TEP_PRINT_OP;
2003 arg->op.left = left;
2006 /* it will set arg->op.right */
2007 type = process_cond(event, arg, tok);
2009 } else if (strcmp(token, ">>") == 0 ||
2010 strcmp(token, "<<") == 0 ||
2011 strcmp(token, "&") == 0 ||
2012 strcmp(token, "|") == 0 ||
2013 strcmp(token, "&&") == 0 ||
2014 strcmp(token, "||") == 0 ||
2015 strcmp(token, "-") == 0 ||
2016 strcmp(token, "+") == 0 ||
2017 strcmp(token, "*") == 0 ||
2018 strcmp(token, "^") == 0 ||
2019 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) {
2032 /* copy the top arg to the left */
2035 arg->type = TEP_PRINT_OP;
2037 arg->op.left = left;
2038 arg->op.right = NULL;
2040 if (set_op_prio(arg) == -1) {
2041 event->flags |= TEP_EVENT_FL_FAILED;
2042 /* arg->op.op (= token) will be freed at out_free */
2047 type = read_token_item(&token);
2050 /* could just be a type pointer */
2051 if ((strcmp(arg->op.op, "*") == 0) &&
2052 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
2055 if (left->type != TEP_PRINT_ATOM) {
2056 do_warning_event(event, "bad pointer type");
2059 ret = append(&left->atom.atom, " ", "*");
2070 right = alloc_arg();
2074 type = process_arg_token(event, right, tok, type);
2075 if (type == TEP_EVENT_ERROR) {
2077 /* token was freed in process_arg_token() via *tok */
2082 if (right->type == TEP_PRINT_OP &&
2083 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2084 struct tep_print_arg tmp;
2086 /* rotate ops according to the priority */
2087 arg->op.right = right->op.left;
2093 arg->op.left = right;
2095 arg->op.right = right;
2098 } else if (strcmp(token, "[") == 0) {
2106 arg->type = TEP_PRINT_OP;
2108 arg->op.left = left;
2112 /* it will set arg->op.right */
2113 type = process_array(event, arg, tok);
2116 do_warning_event(event, "unknown op '%s'", token);
2117 event->flags |= TEP_EVENT_FL_FAILED;
2118 /* the arg is now the left side */
2122 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
2125 /* higher prios need to be closer to the root */
2126 prio = get_op_prio(*tok);
2128 if (prio > arg->op.prio)
2129 return process_op(event, arg, tok);
2131 return process_op(event, right, tok);
2137 do_warning_event(event, "%s: not enough memory!", __func__);
2141 return TEP_EVENT_ERROR;
2144 static enum tep_event_type
2145 process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2148 enum tep_event_type type;
2152 if (read_expected(TEP_EVENT_OP, "->") < 0)
2155 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2159 arg->type = TEP_PRINT_FIELD;
2160 arg->field.name = field;
2162 if (is_flag_field) {
2163 arg->field.field = tep_find_any_field(event, arg->field.name);
2164 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
2166 } else if (is_symbolic_field) {
2167 arg->field.field = tep_find_any_field(event, arg->field.name);
2168 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
2169 is_symbolic_field = 0;
2172 type = read_token(&token);
2181 return TEP_EVENT_ERROR;
2184 static int alloc_and_process_delim(struct tep_event *event, char *next_token,
2185 struct tep_print_arg **print_arg)
2187 struct tep_print_arg *field;
2188 enum tep_event_type type;
2192 field = alloc_arg();
2194 do_warning_event(event, "%s: not enough memory!", __func__);
2199 type = process_arg(event, field, &token);
2201 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
2205 goto out_free_token;
2216 static char *arg_eval (struct tep_print_arg *arg);
2218 static unsigned long long
2219 eval_type_str(unsigned long long val, const char *type, int pointer)
2229 if (type[len-1] != '*') {
2230 do_warning("pointer expected with non pointer type");
2236 do_warning("%s: not enough memory!", __func__);
2239 memcpy(ref, type, len);
2241 /* chop off the " *" */
2244 val = eval_type_str(val, ref, 0);
2249 /* check if this is a pointer */
2250 if (type[len - 1] == '*')
2253 /* Try to figure out the arg size*/
2254 if (strncmp(type, "struct", 6) == 0)
2258 if (strcmp(type, "u8") == 0)
2261 if (strcmp(type, "u16") == 0)
2262 return val & 0xffff;
2264 if (strcmp(type, "u32") == 0)
2265 return val & 0xffffffff;
2267 if (strcmp(type, "u64") == 0 ||
2268 strcmp(type, "s64") == 0)
2271 if (strcmp(type, "s8") == 0)
2272 return (unsigned long long)(char)val & 0xff;
2274 if (strcmp(type, "s16") == 0)
2275 return (unsigned long long)(short)val & 0xffff;
2277 if (strcmp(type, "s32") == 0)
2278 return (unsigned long long)(int)val & 0xffffffff;
2280 if (strncmp(type, "unsigned ", 9) == 0) {
2285 if (strcmp(type, "char") == 0) {
2287 return (unsigned long long)(char)val & 0xff;
2292 if (strcmp(type, "short") == 0) {
2294 return (unsigned long long)(short)val & 0xffff;
2296 return val & 0xffff;
2299 if (strcmp(type, "int") == 0) {
2301 return (unsigned long long)(int)val & 0xffffffff;
2303 return val & 0xffffffff;
2310 * Try to figure out the type.
2312 static unsigned long long
2313 eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
2315 if (arg->type != TEP_PRINT_TYPE) {
2316 do_warning("expected type argument");
2320 return eval_type_str(val, arg->typecast.type, pointer);
2323 static int arg_num_eval(struct tep_print_arg *arg, long long *val)
2325 long long left, right;
2328 switch (arg->type) {
2329 case TEP_PRINT_ATOM:
2330 *val = strtoll(arg->atom.atom, NULL, 0);
2332 case TEP_PRINT_TYPE:
2333 ret = arg_num_eval(arg->typecast.item, val);
2336 *val = eval_type(*val, arg, 0);
2339 switch (arg->op.op[0]) {
2341 ret = arg_num_eval(arg->op.left, &left);
2344 ret = arg_num_eval(arg->op.right, &right);
2348 *val = left || right;
2350 *val = left | right;
2353 ret = arg_num_eval(arg->op.left, &left);
2356 ret = arg_num_eval(arg->op.right, &right);
2360 *val = left && right;
2362 *val = left & right;
2365 ret = arg_num_eval(arg->op.left, &left);
2368 ret = arg_num_eval(arg->op.right, &right);
2371 switch (arg->op.op[1]) {
2373 *val = left < right;
2376 *val = left << right;
2379 *val = left <= right;
2382 do_warning("unknown op '%s'", arg->op.op);
2387 ret = arg_num_eval(arg->op.left, &left);
2390 ret = arg_num_eval(arg->op.right, &right);
2393 switch (arg->op.op[1]) {
2395 *val = left > right;
2398 *val = left >> right;
2401 *val = left >= right;
2404 do_warning("unknown op '%s'", arg->op.op);
2409 ret = arg_num_eval(arg->op.left, &left);
2412 ret = arg_num_eval(arg->op.right, &right);
2416 if (arg->op.op[1] != '=') {
2417 do_warning("unknown op '%s'", arg->op.op);
2420 *val = left == right;
2423 ret = arg_num_eval(arg->op.left, &left);
2426 ret = arg_num_eval(arg->op.right, &right);
2430 switch (arg->op.op[1]) {
2432 *val = left != right;
2435 do_warning("unknown op '%s'", arg->op.op);
2440 /* check for negative */
2441 if (arg->op.left->type == TEP_PRINT_NULL)
2444 ret = arg_num_eval(arg->op.left, &left);
2447 ret = arg_num_eval(arg->op.right, &right);
2450 *val = left - right;
2453 if (arg->op.left->type == TEP_PRINT_NULL)
2456 ret = arg_num_eval(arg->op.left, &left);
2459 ret = arg_num_eval(arg->op.right, &right);
2462 *val = left + right;
2465 ret = arg_num_eval(arg->op.right, &right);
2471 do_warning("unknown op '%s'", arg->op.op);
2476 case TEP_PRINT_NULL:
2477 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2478 case TEP_PRINT_STRING:
2479 case TEP_PRINT_BSTRING:
2480 case TEP_PRINT_BITMASK:
2482 do_warning("invalid eval type %d", arg->type);
2489 static char *arg_eval (struct tep_print_arg *arg)
2492 static char buf[24];
2494 switch (arg->type) {
2495 case TEP_PRINT_ATOM:
2496 return arg->atom.atom;
2497 case TEP_PRINT_TYPE:
2498 return arg_eval(arg->typecast.item);
2500 if (!arg_num_eval(arg, &val))
2502 sprintf(buf, "%lld", val);
2505 case TEP_PRINT_NULL:
2506 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2507 case TEP_PRINT_STRING:
2508 case TEP_PRINT_BSTRING:
2509 case TEP_PRINT_BITMASK:
2511 do_warning("invalid eval type %d", arg->type);
2518 static enum tep_event_type
2519 process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
2521 enum tep_event_type type;
2522 struct tep_print_arg *arg = NULL;
2523 struct tep_print_flag_sym *field;
2529 type = read_token_item(&token);
2530 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
2538 type = process_arg(event, arg, &token);
2540 if (type == TEP_EVENT_OP)
2541 type = process_op(event, arg, &token);
2543 if (type == TEP_EVENT_ERROR)
2546 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2549 field = calloc(1, sizeof(*field));
2553 value = arg_eval(arg);
2555 goto out_free_field;
2556 field->value = strdup(value);
2557 if (field->value == NULL)
2558 goto out_free_field;
2566 type = process_arg(event, arg, &token);
2567 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
2568 goto out_free_field;
2570 value = arg_eval(arg);
2572 goto out_free_field;
2573 field->str = strdup(value);
2574 if (field->str == NULL)
2575 goto out_free_field;
2580 list = &field->next;
2583 type = read_token_item(&token);
2584 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
2590 free_flag_sym(field);
2596 return TEP_EVENT_ERROR;
2599 static enum tep_event_type
2600 process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2602 struct tep_print_arg *field;
2603 enum tep_event_type type;
2606 memset(arg, 0, sizeof(*arg));
2607 arg->type = TEP_PRINT_FLAGS;
2609 field = alloc_arg();
2611 do_warning_event(event, "%s: not enough memory!", __func__);
2615 type = process_field_arg(event, field, &token);
2617 /* Handle operations in the first argument */
2618 while (type == TEP_EVENT_OP)
2619 type = process_op(event, field, &token);
2621 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2622 goto out_free_field;
2625 arg->flags.field = field;
2627 type = read_token_item(&token);
2628 if (event_item_type(type)) {
2629 arg->flags.delim = token;
2630 type = read_token_item(&token);
2633 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2636 type = process_fields(event, &arg->flags.flags, &token);
2637 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2641 type = read_token_item(tok);
2649 return TEP_EVENT_ERROR;
2652 static enum tep_event_type
2653 process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2655 struct tep_print_arg *field;
2656 enum tep_event_type type;
2659 memset(arg, 0, sizeof(*arg));
2660 arg->type = TEP_PRINT_SYMBOL;
2662 field = alloc_arg();
2664 do_warning_event(event, "%s: not enough memory!", __func__);
2668 type = process_field_arg(event, field, &token);
2670 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
2671 goto out_free_field;
2673 arg->symbol.field = field;
2675 type = process_fields(event, &arg->symbol.symbols, &token);
2676 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2680 type = read_token_item(tok);
2688 return TEP_EVENT_ERROR;
2691 static enum tep_event_type
2692 process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
2693 char **tok, enum tep_print_arg_type type)
2695 memset(arg, 0, sizeof(*arg));
2698 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2701 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2704 return read_token_item(tok);
2707 free_arg(arg->hex.field);
2708 arg->hex.field = NULL;
2711 return TEP_EVENT_ERROR;
2714 static enum tep_event_type
2715 process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2717 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
2720 static enum tep_event_type
2721 process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
2724 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
2727 static enum tep_event_type
2728 process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2730 memset(arg, 0, sizeof(*arg));
2731 arg->type = TEP_PRINT_INT_ARRAY;
2733 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2736 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2739 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2742 return read_token_item(tok);
2745 free_arg(arg->int_array.count);
2746 arg->int_array.count = NULL;
2748 free_arg(arg->int_array.field);
2749 arg->int_array.field = NULL;
2752 return TEP_EVENT_ERROR;
2755 static enum tep_event_type
2756 process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2758 struct tep_format_field *field;
2759 enum tep_event_type type;
2762 memset(arg, 0, sizeof(*arg));
2763 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
2766 * The item within the parenthesis is another field that holds
2767 * the index into where the array starts.
2769 type = read_token(&token);
2771 if (type != TEP_EVENT_ITEM)
2774 /* Find the field */
2776 field = tep_find_field(event, token);
2780 arg->dynarray.field = field;
2781 arg->dynarray.index = 0;
2783 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2787 type = read_token_item(&token);
2789 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
2795 do_warning_event(event, "%s: not enough memory!", __func__);
2797 return TEP_EVENT_ERROR;
2800 type = process_arg(event, arg, &token);
2801 if (type == TEP_EVENT_ERROR)
2804 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
2808 type = read_token_item(tok);
2816 return TEP_EVENT_ERROR;
2819 static enum tep_event_type
2820 process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
2823 struct tep_format_field *field;
2824 enum tep_event_type type;
2827 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2830 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
2832 /* Find the field */
2833 field = tep_find_field(event, token);
2837 arg->dynarray.field = field;
2838 arg->dynarray.index = 0;
2840 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2844 type = read_token(&token);
2853 return TEP_EVENT_ERROR;
2856 static enum tep_event_type
2857 process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
2859 struct tep_print_arg *item_arg;
2860 enum tep_event_type type;
2863 type = process_arg(event, arg, &token);
2865 if (type == TEP_EVENT_ERROR)
2868 if (type == TEP_EVENT_OP)
2869 type = process_op(event, arg, &token);
2871 if (type == TEP_EVENT_ERROR)
2874 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
2878 type = read_token_item(&token);
2881 * If the next token is an item or another open paren, then
2882 * this was a typecast.
2884 if (event_item_type(type) ||
2885 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
2887 /* make this a typecast and contine */
2889 /* prevous must be an atom */
2890 if (arg->type != TEP_PRINT_ATOM) {
2891 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
2895 item_arg = alloc_arg();
2897 do_warning_event(event, "%s: not enough memory!",
2902 arg->type = TEP_PRINT_TYPE;
2903 arg->typecast.type = arg->atom.atom;
2904 arg->typecast.item = item_arg;
2905 type = process_arg_token(event, item_arg, &token, type);
2915 return TEP_EVENT_ERROR;
2919 static enum tep_event_type
2920 process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2923 enum tep_event_type type;
2926 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2929 arg->type = TEP_PRINT_STRING;
2930 arg->string.string = token;
2931 arg->string.offset = -1;
2933 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2936 type = read_token(&token);
2945 return TEP_EVENT_ERROR;
2948 static enum tep_event_type
2949 process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
2952 enum tep_event_type type;
2955 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
2958 arg->type = TEP_PRINT_BITMASK;
2959 arg->bitmask.bitmask = token;
2960 arg->bitmask.offset = -1;
2962 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
2965 type = read_token(&token);
2974 return TEP_EVENT_ERROR;
2977 static struct tep_function_handler *
2978 find_func_handler(struct tep_handle *tep, char *func_name)
2980 struct tep_function_handler *func;
2985 for (func = tep->func_handlers; func; func = func->next) {
2986 if (strcmp(func->name, func_name) == 0)
2993 static void remove_func_handler(struct tep_handle *tep, char *func_name)
2995 struct tep_function_handler *func;
2996 struct tep_function_handler **next;
2998 next = &tep->func_handlers;
2999 while ((func = *next)) {
3000 if (strcmp(func->name, func_name) == 0) {
3002 free_func_handle(func);
3009 static enum tep_event_type
3010 process_func_handler(struct tep_event *event, struct tep_function_handler *func,
3011 struct tep_print_arg *arg, char **tok)
3013 struct tep_print_arg **next_arg;
3014 struct tep_print_arg *farg;
3015 enum tep_event_type type;
3019 arg->type = TEP_PRINT_FUNC;
3020 arg->func.func = func;
3024 next_arg = &(arg->func.args);
3025 for (i = 0; i < func->nr_args; i++) {
3028 do_warning_event(event, "%s: not enough memory!",
3030 return TEP_EVENT_ERROR;
3033 type = process_arg(event, farg, &token);
3034 if (i < (func->nr_args - 1)) {
3035 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
3036 do_warning_event(event,
3037 "Error: function '%s()' expects %d arguments but event %s only uses %d",
3038 func->name, func->nr_args,
3039 event->name, i + 1);
3043 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
3044 do_warning_event(event,
3045 "Error: function '%s()' only expects %d arguments but event %s has more",
3046 func->name, func->nr_args, event->name);
3052 next_arg = &(farg->next);
3056 type = read_token(&token);
3064 return TEP_EVENT_ERROR;
3067 static enum tep_event_type
3068 process_builtin_expect(struct tep_event *event, struct tep_print_arg *arg, char **tok)
3070 enum tep_event_type type;
3073 /* Handle __builtin_expect( cond, #) */
3074 type = process_arg(event, arg, &token);
3076 if (type != TEP_EVENT_DELIM || token[0] != ',')
3081 /* We don't care what the second parameter is of the __builtin_expect() */
3082 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
3085 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
3089 type = read_token_item(tok);
3095 return TEP_EVENT_ERROR;
3098 static enum tep_event_type
3099 process_function(struct tep_event *event, struct tep_print_arg *arg,
3100 char *token, char **tok)
3102 struct tep_function_handler *func;
3104 if (strcmp(token, "__print_flags") == 0) {
3107 return process_flags(event, arg, tok);
3109 if (strcmp(token, "__print_symbolic") == 0) {
3111 is_symbolic_field = 1;
3112 return process_symbols(event, arg, tok);
3114 if (strcmp(token, "__print_hex") == 0) {
3116 return process_hex(event, arg, tok);
3118 if (strcmp(token, "__print_hex_str") == 0) {
3120 return process_hex_str(event, arg, tok);
3122 if (strcmp(token, "__print_array") == 0) {
3124 return process_int_array(event, arg, tok);
3126 if (strcmp(token, "__get_str") == 0) {
3128 return process_str(event, arg, tok);
3130 if (strcmp(token, "__get_bitmask") == 0) {
3132 return process_bitmask(event, arg, tok);
3134 if (strcmp(token, "__get_dynamic_array") == 0) {
3136 return process_dynamic_array(event, arg, tok);
3138 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3140 return process_dynamic_array_len(event, arg, tok);
3142 if (strcmp(token, "__builtin_expect") == 0) {
3144 return process_builtin_expect(event, arg, tok);
3147 func = find_func_handler(event->tep, token);
3150 return process_func_handler(event, func, arg, tok);
3153 do_warning_event(event, "function %s not defined", token);
3155 return TEP_EVENT_ERROR;
3158 static enum tep_event_type
3159 process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
3160 char **tok, enum tep_event_type type)
3168 case TEP_EVENT_ITEM:
3169 if (strcmp(token, "REC") == 0) {
3171 type = process_entry(event, arg, &token);
3175 /* test the next token */
3176 type = read_token_item(&token);
3179 * If the next token is a parenthesis, then this
3182 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
3185 /* this will free atom. */
3186 type = process_function(event, arg, atom, &token);
3189 /* atoms can be more than one token long */
3190 while (type == TEP_EVENT_ITEM) {
3193 ret = append(&atom, " ", token);
3198 return TEP_EVENT_ERROR;
3201 type = read_token_item(&token);
3204 arg->type = TEP_PRINT_ATOM;
3205 arg->atom.atom = atom;
3208 case TEP_EVENT_DQUOTE:
3209 case TEP_EVENT_SQUOTE:
3210 arg->type = TEP_PRINT_ATOM;
3211 arg->atom.atom = token;
3212 type = read_token_item(&token);
3214 case TEP_EVENT_DELIM:
3215 if (strcmp(token, "(") == 0) {
3217 type = process_paren(event, arg, &token);
3221 /* handle single ops */
3222 arg->type = TEP_PRINT_OP;
3224 arg->op.left = NULL;
3225 type = process_op(event, arg, &token);
3227 /* On error, the op is freed */
3228 if (type == TEP_EVENT_ERROR)
3231 /* return error type if errored */
3234 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
3236 do_warning_event(event, "unexpected type %d", type);
3237 return TEP_EVENT_ERROR;
3244 static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
3246 enum tep_event_type type = TEP_EVENT_ERROR;
3247 struct tep_print_arg *arg;
3252 if (type == TEP_EVENT_NEWLINE) {
3253 type = read_token_item(&token);
3259 do_warning_event(event, "%s: not enough memory!",
3264 type = process_arg(event, arg, &token);
3266 if (type == TEP_EVENT_ERROR) {
3275 if (type == TEP_EVENT_OP) {
3276 type = process_op(event, arg, &token);
3278 if (type == TEP_EVENT_ERROR) {
3287 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
3294 } while (type != TEP_EVENT_NONE);
3296 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
3302 static int event_read_print(struct tep_event *event)
3304 enum tep_event_type type;
3308 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
3311 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
3314 if (read_expected(TEP_EVENT_OP, ":") < 0)
3317 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
3321 event->print_fmt.format = token;
3322 event->print_fmt.args = NULL;
3324 /* ok to have no arg */
3325 type = read_token_item(&token);
3327 if (type == TEP_EVENT_NONE)
3330 /* Handle concatenation of print lines */
3331 if (type == TEP_EVENT_DQUOTE) {
3334 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3337 free_token(event->print_fmt.format);
3338 event->print_fmt.format = NULL;
3343 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
3348 ret = event_read_print_args(event, &event->print_fmt.args);
3360 * tep_find_common_field - return a common field by event
3361 * @event: handle for the event
3362 * @name: the name of the common field to return
3364 * Returns a common field from the event by the given @name.
3365 * This only searches the common fields and not all field.
3367 struct tep_format_field *
3368 tep_find_common_field(struct tep_event *event, const char *name)
3370 struct tep_format_field *format;
3372 for (format = event->format.common_fields;
3373 format; format = format->next) {
3374 if (strcmp(format->name, name) == 0)
3382 * tep_find_field - find a non-common field
3383 * @event: handle for the event
3384 * @name: the name of the non-common field
3386 * Returns a non-common field by the given @name.
3387 * This does not search common fields.
3389 struct tep_format_field *
3390 tep_find_field(struct tep_event *event, const char *name)
3392 struct tep_format_field *format;
3394 for (format = event->format.fields;
3395 format; format = format->next) {
3396 if (strcmp(format->name, name) == 0)
3404 * tep_find_any_field - find any field by name
3405 * @event: handle for the event
3406 * @name: the name of the field
3408 * Returns a field by the given @name.
3409 * This searches the common field names first, then
3410 * the non-common ones if a common one was not found.
3412 struct tep_format_field *
3413 tep_find_any_field(struct tep_event *event, const char *name)
3415 struct tep_format_field *format;
3417 format = tep_find_common_field(event, name);
3420 return tep_find_field(event, name);
3424 * tep_read_number - read a number from data
3425 * @tep: a handle to the trace event parser context
3426 * @ptr: the raw data
3427 * @size: the size of the data that holds the number
3429 * Returns the number (converted to host) from the
3432 unsigned long long tep_read_number(struct tep_handle *tep,
3433 const void *ptr, int size)
3435 unsigned long long val;
3439 return *(unsigned char *)ptr;
3441 return data2host2(tep, *(unsigned short *)ptr);
3443 return data2host4(tep, *(unsigned int *)ptr);
3445 memcpy(&val, (ptr), sizeof(unsigned long long));
3446 return data2host8(tep, val);
3454 * tep_read_number_field - read a number from data
3455 * @field: a handle to the field
3456 * @data: the raw data to read
3457 * @value: the value to place the number in
3459 * Reads raw data according to a field offset and size,
3460 * and translates it into @value.
3462 * Returns 0 on success, -1 otherwise.
3464 int tep_read_number_field(struct tep_format_field *field, const void *data,
3465 unsigned long long *value)
3469 switch (field->size) {
3474 *value = tep_read_number(field->event->tep,
3475 data + field->offset, field->size);
3482 static int get_common_info(struct tep_handle *tep,
3483 const char *type, int *offset, int *size)
3485 struct tep_event *event;
3486 struct tep_format_field *field;
3489 * All events should have the same common elements.
3490 * Pick any event to find where the type is;
3493 do_warning("no event_list!");
3497 event = tep->events[0];
3498 field = tep_find_common_field(event, type);
3502 *offset = field->offset;
3503 *size = field->size;
3508 static int __parse_common(struct tep_handle *tep, void *data,
3509 int *size, int *offset, const char *name)
3514 ret = get_common_info(tep, name, offset, size);
3518 return tep_read_number(tep, data + *offset, *size);
3521 static int trace_parse_common_type(struct tep_handle *tep, void *data)
3523 return __parse_common(tep, data,
3524 &tep->type_size, &tep->type_offset,
3528 static int parse_common_pid(struct tep_handle *tep, void *data)
3530 return __parse_common(tep, data,
3531 &tep->pid_size, &tep->pid_offset,
3535 static int parse_common_pc(struct tep_handle *tep, void *data)
3537 return __parse_common(tep, data,
3538 &tep->pc_size, &tep->pc_offset,
3539 "common_preempt_count");
3542 static int parse_common_flags(struct tep_handle *tep, void *data)
3544 return __parse_common(tep, data,
3545 &tep->flags_size, &tep->flags_offset,
3549 static int parse_common_lock_depth(struct tep_handle *tep, void *data)
3551 return __parse_common(tep, data,
3552 &tep->ld_size, &tep->ld_offset,
3553 "common_lock_depth");
3556 static int parse_common_migrate_disable(struct tep_handle *tep, void *data)
3558 return __parse_common(tep, data,
3559 &tep->ld_size, &tep->ld_offset,
3560 "common_migrate_disable");
3563 static int events_id_cmp(const void *a, const void *b);
3566 * tep_find_event - find an event by given id
3567 * @tep: a handle to the trace event parser context
3568 * @id: the id of the event
3570 * Returns an event that has a given @id.
3572 struct tep_event *tep_find_event(struct tep_handle *tep, int id)
3574 struct tep_event **eventptr;
3575 struct tep_event key;
3576 struct tep_event *pkey = &key;
3578 /* Check cache first */
3579 if (tep->last_event && tep->last_event->id == id)
3580 return tep->last_event;
3584 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
3585 sizeof(*tep->events), events_id_cmp);
3588 tep->last_event = *eventptr;
3596 * tep_find_event_by_name - find an event by given name
3597 * @tep: a handle to the trace event parser context
3598 * @sys: the system name to search for
3599 * @name: the name of the event to search for
3601 * This returns an event with a given @name and under the system
3602 * @sys. If @sys is NULL the first event with @name is returned.
3605 tep_find_event_by_name(struct tep_handle *tep,
3606 const char *sys, const char *name)
3608 struct tep_event *event = NULL;
3611 if (tep->last_event &&
3612 strcmp(tep->last_event->name, name) == 0 &&
3613 (!sys || strcmp(tep->last_event->system, sys) == 0))
3614 return tep->last_event;
3616 for (i = 0; i < tep->nr_events; i++) {
3617 event = tep->events[i];
3618 if (strcmp(event->name, name) == 0) {
3621 if (strcmp(event->system, sys) == 0)
3625 if (i == tep->nr_events)
3628 tep->last_event = event;
3632 static unsigned long long
3633 eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
3635 struct tep_handle *tep = event->tep;
3636 unsigned long long val = 0;
3637 unsigned long long left, right;
3638 struct tep_print_arg *typearg = NULL;
3639 struct tep_print_arg *larg;
3640 unsigned long offset;
3641 unsigned int field_size;
3643 switch (arg->type) {
3644 case TEP_PRINT_NULL:
3647 case TEP_PRINT_ATOM:
3648 return strtoull(arg->atom.atom, NULL, 0);
3649 case TEP_PRINT_FIELD:
3650 if (!arg->field.field) {
3651 arg->field.field = tep_find_any_field(event, arg->field.name);
3652 if (!arg->field.field)
3653 goto out_warning_field;
3656 /* must be a number */
3657 val = tep_read_number(tep, data + arg->field.field->offset,
3658 arg->field.field->size);
3660 case TEP_PRINT_FLAGS:
3661 case TEP_PRINT_SYMBOL:
3662 case TEP_PRINT_INT_ARRAY:
3664 case TEP_PRINT_HEX_STR:
3666 case TEP_PRINT_TYPE:
3667 val = eval_num_arg(data, size, event, arg->typecast.item);
3668 return eval_type(val, arg, 0);
3669 case TEP_PRINT_STRING:
3670 case TEP_PRINT_BSTRING:
3671 case TEP_PRINT_BITMASK:
3673 case TEP_PRINT_FUNC: {
3676 val = process_defined_func(&s, data, size, event, arg);
3677 trace_seq_destroy(&s);
3681 if (strcmp(arg->op.op, "[") == 0) {
3683 * Arrays are special, since we don't want
3684 * to read the arg as is.
3686 right = eval_num_arg(data, size, event, arg->op.right);
3688 /* handle typecasts */
3689 larg = arg->op.left;
3690 while (larg->type == TEP_PRINT_TYPE) {
3693 larg = larg->typecast.item;
3696 /* Default to long size */
3697 field_size = tep->long_size;
3699 switch (larg->type) {
3700 case TEP_PRINT_DYNAMIC_ARRAY:
3701 offset = tep_read_number(tep,
3702 data + larg->dynarray.field->offset,
3703 larg->dynarray.field->size);
3704 if (larg->dynarray.field->elementsize)
3705 field_size = larg->dynarray.field->elementsize;
3707 * The actual length of the dynamic array is stored
3708 * in the top half of the field, and the offset
3709 * is in the bottom half of the 32 bit field.
3714 case TEP_PRINT_FIELD:
3715 if (!larg->field.field) {
3717 tep_find_any_field(event, larg->field.name);
3718 if (!larg->field.field) {
3720 goto out_warning_field;
3723 field_size = larg->field.field->elementsize;
3724 offset = larg->field.field->offset +
3725 right * larg->field.field->elementsize;
3728 goto default_op; /* oops, all bets off */
3730 val = tep_read_number(tep,
3731 data + offset, field_size);
3733 val = eval_type(val, typearg, 1);
3735 } else if (strcmp(arg->op.op, "?") == 0) {
3736 left = eval_num_arg(data, size, event, arg->op.left);
3737 arg = arg->op.right;
3739 val = eval_num_arg(data, size, event, arg->op.left);
3741 val = eval_num_arg(data, size, event, arg->op.right);
3745 left = eval_num_arg(data, size, event, arg->op.left);
3746 right = eval_num_arg(data, size, event, arg->op.right);
3747 switch (arg->op.op[0]) {
3749 switch (arg->op.op[1]) {
3754 val = left != right;
3757 goto out_warning_op;
3765 val = left || right;
3771 val = left && right;
3776 switch (arg->op.op[1]) {
3781 val = left << right;
3784 val = left <= right;
3787 goto out_warning_op;
3791 switch (arg->op.op[1]) {
3796 val = left >> right;
3799 val = left >= right;
3802 goto out_warning_op;
3806 if (arg->op.op[1] != '=')
3807 goto out_warning_op;
3809 val = left == right;
3827 goto out_warning_op;
3830 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
3831 offset = tep_read_number(tep,
3832 data + arg->dynarray.field->offset,
3833 arg->dynarray.field->size);
3835 * The total allocated length of the dynamic array is
3836 * stored in the top half of the field, and the offset
3837 * is in the bottom half of the 32 bit field.
3839 val = (unsigned long long)(offset >> 16);
3841 case TEP_PRINT_DYNAMIC_ARRAY:
3842 /* Without [], we pass the address to the dynamic data */
3843 offset = tep_read_number(tep,
3844 data + arg->dynarray.field->offset,
3845 arg->dynarray.field->size);
3847 * The total allocated length of the dynamic array is
3848 * stored in the top half of the field, and the offset
3849 * is in the bottom half of the 32 bit field.
3852 val = (unsigned long long)((unsigned long)data + offset);
3854 default: /* not sure what to do there */
3860 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3864 do_warning_event(event, "%s: field %s not found",
3865 __func__, arg->field.name);
3871 unsigned long long value;
3874 static const struct flag flags[] = {
3875 { "HI_SOFTIRQ", 0 },
3876 { "TIMER_SOFTIRQ", 1 },
3877 { "NET_TX_SOFTIRQ", 2 },
3878 { "NET_RX_SOFTIRQ", 3 },
3879 { "BLOCK_SOFTIRQ", 4 },
3880 { "IRQ_POLL_SOFTIRQ", 5 },
3881 { "TASKLET_SOFTIRQ", 6 },
3882 { "SCHED_SOFTIRQ", 7 },
3883 { "HRTIMER_SOFTIRQ", 8 },
3884 { "RCU_SOFTIRQ", 9 },
3886 { "HRTIMER_NORESTART", 0 },
3887 { "HRTIMER_RESTART", 1 },
3890 static long long eval_flag(const char *flag)
3895 * Some flags in the format files do not get converted.
3896 * If the flag is not numeric, see if it is something that
3897 * we already know about.
3899 if (isdigit(flag[0]))
3900 return strtoull(flag, NULL, 0);
3902 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3903 if (strcmp(flags[i].name, flag) == 0)
3904 return flags[i].value;
3909 static void print_str_to_seq(struct trace_seq *s, const char *format,
3910 int len_arg, const char *str)
3913 trace_seq_printf(s, format, len_arg, str);
3915 trace_seq_printf(s, format, str);
3918 static void print_bitmask_to_seq(struct tep_handle *tep,
3919 struct trace_seq *s, const char *format,
3920 int len_arg, const void *data, int size)
3922 int nr_bits = size * 8;
3923 int str_size = (nr_bits + 3) / 4;
3931 * The kernel likes to put in commas every 32 bits, we
3934 str_size += (nr_bits - 1) / 32;
3936 str = malloc(str_size + 1);
3938 do_warning("%s: not enough memory!", __func__);
3943 /* Start out with -2 for the two chars per byte */
3944 for (i = str_size - 2; i >= 0; i -= 2) {
3946 * data points to a bit mask of size bytes.
3947 * In the kernel, this is an array of long words, thus
3948 * endianness is very important.
3950 if (tep->file_bigendian)
3951 index = size - (len + 1);
3955 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3956 memcpy(str + i, buf, 2);
3958 if (!(len & 3) && i > 0) {
3965 trace_seq_printf(s, format, len_arg, str);
3967 trace_seq_printf(s, format, str);
3972 static void print_str_arg(struct trace_seq *s, void *data, int size,
3973 struct tep_event *event, const char *format,
3974 int len_arg, struct tep_print_arg *arg)
3976 struct tep_handle *tep = event->tep;
3977 struct tep_print_flag_sym *flag;
3978 struct tep_format_field *field;
3979 struct printk_map *printk;
3980 long long val, fval;
3981 unsigned long long addr;
3987 switch (arg->type) {
3988 case TEP_PRINT_NULL:
3991 case TEP_PRINT_ATOM:
3992 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3994 case TEP_PRINT_FIELD:
3995 field = arg->field.field;
3997 field = tep_find_any_field(event, arg->field.name);
3999 str = arg->field.name;
4000 goto out_warning_field;
4002 arg->field.field = field;
4004 /* Zero sized fields, mean the rest of the data */
4005 len = field->size ? : size - field->offset;
4008 * Some events pass in pointers. If this is not an array
4009 * and the size is the same as long_size, assume that it
4012 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
4013 field->size == tep->long_size) {
4015 /* Handle heterogeneous recording and processing
4019 * Traces recorded on 32-bit devices (32-bit
4020 * addressing) and processed on 64-bit devices:
4021 * In this case, only 32 bits should be read.
4024 * Traces recorded on 64 bit devices and processed
4025 * on 32-bit devices:
4026 * In this case, 64 bits must be read.
4028 addr = (tep->long_size == 8) ?
4029 *(unsigned long long *)(data + field->offset) :
4030 (unsigned long long)*(unsigned int *)(data + field->offset);
4032 /* Check if it matches a print format */
4033 printk = find_printk(tep, addr);
4035 trace_seq_puts(s, printk->printk);
4037 trace_seq_printf(s, "%llx", addr);
4040 str = malloc(len + 1);
4042 do_warning_event(event, "%s: not enough memory!",
4046 memcpy(str, data + field->offset, len);
4048 print_str_to_seq(s, format, len_arg, str);
4051 case TEP_PRINT_FLAGS:
4052 val = eval_num_arg(data, size, event, arg->flags.field);
4054 for (flag = arg->flags.flags; flag; flag = flag->next) {
4055 fval = eval_flag(flag->value);
4056 if (!val && fval < 0) {
4057 print_str_to_seq(s, format, len_arg, flag->str);
4060 if (fval > 0 && (val & fval) == fval) {
4061 if (print && arg->flags.delim)
4062 trace_seq_puts(s, arg->flags.delim);
4063 print_str_to_seq(s, format, len_arg, flag->str);
4069 if (print && arg->flags.delim)
4070 trace_seq_puts(s, arg->flags.delim);
4071 trace_seq_printf(s, "0x%llx", val);
4074 case TEP_PRINT_SYMBOL:
4075 val = eval_num_arg(data, size, event, arg->symbol.field);
4076 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4077 fval = eval_flag(flag->value);
4079 print_str_to_seq(s, format, len_arg, flag->str);
4084 trace_seq_printf(s, "0x%llx", val);
4087 case TEP_PRINT_HEX_STR:
4088 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4089 unsigned long offset;
4090 offset = tep_read_number(tep,
4091 data + arg->hex.field->dynarray.field->offset,
4092 arg->hex.field->dynarray.field->size);
4093 hex = data + (offset & 0xffff);
4095 field = arg->hex.field->field.field;
4097 str = arg->hex.field->field.name;
4098 field = tep_find_any_field(event, str);
4100 goto out_warning_field;
4101 arg->hex.field->field.field = field;
4103 hex = data + field->offset;
4105 len = eval_num_arg(data, size, event, arg->hex.size);
4106 for (i = 0; i < len; i++) {
4107 if (i && arg->type == TEP_PRINT_HEX)
4108 trace_seq_putc(s, ' ');
4109 trace_seq_printf(s, "%02x", hex[i]);
4113 case TEP_PRINT_INT_ARRAY: {
4117 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
4118 unsigned long offset;
4119 struct tep_format_field *field =
4120 arg->int_array.field->dynarray.field;
4121 offset = tep_read_number(tep,
4122 data + field->offset,
4124 num = data + (offset & 0xffff);
4126 field = arg->int_array.field->field.field;
4128 str = arg->int_array.field->field.name;
4129 field = tep_find_any_field(event, str);
4131 goto out_warning_field;
4132 arg->int_array.field->field.field = field;
4134 num = data + field->offset;
4136 len = eval_num_arg(data, size, event, arg->int_array.count);
4137 el_size = eval_num_arg(data, size, event,
4138 arg->int_array.el_size);
4139 for (i = 0; i < len; i++) {
4141 trace_seq_putc(s, ' ');
4144 trace_seq_printf(s, "%u", *(uint8_t *)num);
4145 } else if (el_size == 2) {
4146 trace_seq_printf(s, "%u", *(uint16_t *)num);
4147 } else if (el_size == 4) {
4148 trace_seq_printf(s, "%u", *(uint32_t *)num);
4149 } else if (el_size == 8) {
4150 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4152 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4153 el_size, *(uint8_t *)num);
4161 case TEP_PRINT_TYPE:
4163 case TEP_PRINT_STRING: {
4166 if (arg->string.offset == -1) {
4167 struct tep_format_field *f;
4169 f = tep_find_any_field(event, arg->string.string);
4170 arg->string.offset = f->offset;
4172 str_offset = data2host4(tep, *(unsigned int *)(data + arg->string.offset));
4173 str_offset &= 0xffff;
4174 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4177 case TEP_PRINT_BSTRING:
4178 print_str_to_seq(s, format, len_arg, arg->string.string);
4180 case TEP_PRINT_BITMASK: {
4184 if (arg->bitmask.offset == -1) {
4185 struct tep_format_field *f;
4187 f = tep_find_any_field(event, arg->bitmask.bitmask);
4188 arg->bitmask.offset = f->offset;
4190 bitmask_offset = data2host4(tep, *(unsigned int *)(data + arg->bitmask.offset));
4191 bitmask_size = bitmask_offset >> 16;
4192 bitmask_offset &= 0xffff;
4193 print_bitmask_to_seq(tep, s, format, len_arg,
4194 data + bitmask_offset, bitmask_size);
4199 * The only op for string should be ? :
4201 if (arg->op.op[0] != '?')
4203 val = eval_num_arg(data, size, event, arg->op.left);
4205 print_str_arg(s, data, size, event,
4206 format, len_arg, arg->op.right->op.left);
4208 print_str_arg(s, data, size, event,
4209 format, len_arg, arg->op.right->op.right);
4211 case TEP_PRINT_FUNC:
4212 process_defined_func(s, data, size, event, arg);
4222 do_warning_event(event, "%s: field %s not found",
4223 __func__, arg->field.name);
4226 static unsigned long long
4227 process_defined_func(struct trace_seq *s, void *data, int size,
4228 struct tep_event *event, struct tep_print_arg *arg)
4230 struct tep_function_handler *func_handle = arg->func.func;
4231 struct func_params *param;
4232 unsigned long long *args;
4233 unsigned long long ret;
4234 struct tep_print_arg *farg;
4235 struct trace_seq str;
4237 struct save_str *next;
4239 } *strings = NULL, *string;
4242 if (!func_handle->nr_args) {
4243 ret = (*func_handle->func)(s, NULL);
4247 farg = arg->func.args;
4248 param = func_handle->params;
4251 args = malloc(sizeof(*args) * func_handle->nr_args);
4255 for (i = 0; i < func_handle->nr_args; i++) {
4256 switch (param->type) {
4257 case TEP_FUNC_ARG_INT:
4258 case TEP_FUNC_ARG_LONG:
4259 case TEP_FUNC_ARG_PTR:
4260 args[i] = eval_num_arg(data, size, event, farg);
4262 case TEP_FUNC_ARG_STRING:
4263 trace_seq_init(&str);
4264 print_str_arg(&str, data, size, event, "%s", -1, farg);
4265 trace_seq_terminate(&str);
4266 string = malloc(sizeof(*string));
4268 do_warning_event(event, "%s(%d): malloc str",
4269 __func__, __LINE__);
4272 string->next = strings;
4273 string->str = strdup(str.buffer);
4276 do_warning_event(event, "%s(%d): malloc str",
4277 __func__, __LINE__);
4280 args[i] = (uintptr_t)string->str;
4282 trace_seq_destroy(&str);
4286 * Something went totally wrong, this is not
4287 * an input error, something in this code broke.
4289 do_warning_event(event, "Unexpected end of arguments\n");
4293 param = param->next;
4296 ret = (*func_handle->func)(s, args);
4301 strings = string->next;
4307 /* TBD : handle return type here */
4311 static void free_args(struct tep_print_arg *args)
4313 struct tep_print_arg *next;
4323 static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
4325 struct tep_handle *tep = event->tep;
4326 struct tep_format_field *field, *ip_field;
4327 struct tep_print_arg *args, *arg, **next;
4328 unsigned long long ip, val;
4333 field = tep->bprint_buf_field;
4334 ip_field = tep->bprint_ip_field;
4337 field = tep_find_field(event, "buf");
4339 do_warning_event(event, "can't find buffer field for binary printk");
4342 ip_field = tep_find_field(event, "ip");
4344 do_warning_event(event, "can't find ip field for binary printk");
4347 tep->bprint_buf_field = field;
4348 tep->bprint_ip_field = ip_field;
4351 ip = tep_read_number(tep, data + ip_field->offset, ip_field->size);
4354 * The first arg is the IP pointer.
4358 do_warning_event(event, "%s(%d): not enough memory!",
4359 __func__, __LINE__);
4366 arg->type = TEP_PRINT_ATOM;
4368 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4371 /* skip the first "%ps: " */
4372 for (ptr = fmt + 5, bptr = data + field->offset;
4373 bptr < data + size && *ptr; ptr++) {
4398 if (isalnum(ptr[1])) {
4400 /* Check for special pointers */
4409 * Pre-5.5 kernels use %pf and
4410 * %pF for printing symbols
4411 * while kernels since 5.5 use
4412 * %pfw for fwnodes. So check
4413 * %p[fF] isn't followed by 'w'.
4420 * Older kernels do not process
4421 * dereferenced pointers.
4422 * Only process if the pointer
4423 * value is a printable.
4425 if (isprint(*(char *)bptr))
4426 goto process_string;
4441 vsize = tep->long_size;
4455 /* the pointers are always 4 bytes aligned */
4456 bptr = (void *)(((unsigned long)bptr + 3) &
4458 val = tep_read_number(tep, bptr, vsize);
4462 do_warning_event(event, "%s(%d): not enough memory!",
4463 __func__, __LINE__);
4467 arg->type = TEP_PRINT_ATOM;
4468 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4475 * The '*' case means that an arg is used as the length.
4476 * We need to continue to figure out for what.
4486 do_warning_event(event, "%s(%d): not enough memory!",
4487 __func__, __LINE__);
4491 arg->type = TEP_PRINT_BSTRING;
4492 arg->string.string = strdup(bptr);
4493 if (!arg->string.string)
4495 bptr += strlen(bptr) + 1;
4512 get_bprint_format(void *data, int size __maybe_unused,
4513 struct tep_event *event)
4515 struct tep_handle *tep = event->tep;
4516 unsigned long long addr;
4517 struct tep_format_field *field;
4518 struct printk_map *printk;
4521 field = tep->bprint_fmt_field;
4524 field = tep_find_field(event, "fmt");
4526 do_warning_event(event, "can't find format field for binary printk");
4529 tep->bprint_fmt_field = field;
4532 addr = tep_read_number(tep, data + field->offset, field->size);
4534 printk = find_printk(tep, addr);
4536 if (asprintf(&format, "%%ps: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4541 if (asprintf(&format, "%s: %s", "%ps", printk->printk) < 0)
4547 static int print_mac_arg(struct trace_seq *s, const char *format,
4548 void *data, int size, struct tep_event *event,
4549 struct tep_print_arg *arg)
4551 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4552 bool reverse = false;
4556 if (arg->type == TEP_PRINT_FUNC) {
4557 process_defined_func(s, data, size, event, arg);
4561 if (arg->type != TEP_PRINT_FIELD) {
4562 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4567 if (format[0] == 'm') {
4568 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4569 } else if (format[0] == 'M' && format[1] == 'F') {
4570 fmt = "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x";
4573 if (format[1] == 'R') {
4578 if (!arg->field.field) {
4580 tep_find_any_field(event, arg->field.name);
4581 if (!arg->field.field) {
4582 do_warning_event(event, "%s: field %s not found",
4583 __func__, arg->field.name);
4587 if (arg->field.field->size != 6) {
4588 trace_seq_printf(s, "INVALIDMAC");
4592 buf = data + arg->field.field->offset;
4594 trace_seq_printf(s, fmt, buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
4596 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4601 static int parse_ip4_print_args(struct tep_handle *tep,
4602 const char *ptr, bool *reverse)
4611 if (tep->file_bigendian)
4633 static void print_ip4_addr(struct trace_seq *s, char i, bool reverse, unsigned char *buf)
4638 fmt = "%03d.%03d.%03d.%03d";
4640 fmt = "%d.%d.%d.%d";
4643 trace_seq_printf(s, fmt, buf[3], buf[2], buf[1], buf[0]);
4645 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4649 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4651 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4652 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4655 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4657 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4660 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4663 unsigned char zerolength[8];
4668 bool needcolon = false;
4670 struct in6_addr in6;
4672 memcpy(&in6, addr, sizeof(struct in6_addr));
4674 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4676 memset(zerolength, 0, sizeof(zerolength));
4683 /* find position of longest 0 run */
4684 for (i = 0; i < range; i++) {
4685 for (j = i; j < range; j++) {
4686 if (in6.s6_addr16[j] != 0)
4691 for (i = 0; i < range; i++) {
4692 if (zerolength[i] > longest) {
4693 longest = zerolength[i];
4697 if (longest == 1) /* don't compress a single 0 */
4701 for (i = 0; i < range; i++) {
4702 if (i == colonpos) {
4703 if (needcolon || i == 0)
4704 trace_seq_printf(s, ":");
4705 trace_seq_printf(s, ":");
4711 trace_seq_printf(s, ":");
4714 /* hex u16 without leading 0s */
4715 word = ntohs(in6.s6_addr16[i]);
4719 trace_seq_printf(s, "%x%02x", hi, lo);
4721 trace_seq_printf(s, "%x", lo);
4728 trace_seq_printf(s, ":");
4729 print_ip4_addr(s, 'I', false, &in6.s6_addr[12]);
4735 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4739 for (j = 0; j < 16; j += 2) {
4740 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4741 if (i == 'I' && j < 14)
4742 trace_seq_printf(s, ":");
4747 * %pi4 print an IPv4 address with leading zeros
4748 * %pI4 print an IPv4 address without leading zeros
4749 * %pi6 print an IPv6 address without colons
4750 * %pI6 print an IPv6 address with colons
4751 * %pI6c print an IPv6 address in compressed form with colons
4752 * %pISpc print an IP address based on sockaddr; p adds port.
4754 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4755 void *data, int size, struct tep_event *event,
4756 struct tep_print_arg *arg)
4758 bool reverse = false;
4762 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
4764 if (arg->type == TEP_PRINT_FUNC) {
4765 process_defined_func(s, data, size, event, arg);
4769 if (arg->type != TEP_PRINT_FIELD) {
4770 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4774 if (!arg->field.field) {
4776 tep_find_any_field(event, arg->field.name);
4777 if (!arg->field.field) {
4778 do_warning("%s: field %s not found",
4779 __func__, arg->field.name);
4784 buf = data + arg->field.field->offset;
4786 if (arg->field.field->size != 4) {
4787 trace_seq_printf(s, "INVALIDIPv4");
4791 print_ip4_addr(s, i, reverse, buf);
4796 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4797 void *data, int size, struct tep_event *event,
4798 struct tep_print_arg *arg)
4805 if (i == 'I' && *ptr == 'c') {
4811 if (arg->type == TEP_PRINT_FUNC) {
4812 process_defined_func(s, data, size, event, arg);
4816 if (arg->type != TEP_PRINT_FIELD) {
4817 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4821 if (!arg->field.field) {
4823 tep_find_any_field(event, arg->field.name);
4824 if (!arg->field.field) {
4825 do_warning("%s: field %s not found",
4826 __func__, arg->field.name);
4831 buf = data + arg->field.field->offset;
4833 if (arg->field.field->size != 16) {
4834 trace_seq_printf(s, "INVALIDIPv6");
4839 print_ip6c_addr(s, buf);
4841 print_ip6_addr(s, i, buf);
4846 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4847 void *data, int size, struct tep_event *event,
4848 struct tep_print_arg *arg)
4850 char have_c = 0, have_p = 0;
4852 struct sockaddr_storage *sa;
4853 bool reverse = false;
4870 ret = parse_ip4_print_args(event->tep, ptr, &reverse);
4874 if (arg->type == TEP_PRINT_FUNC) {
4875 process_defined_func(s, data, size, event, arg);
4879 if (arg->type != TEP_PRINT_FIELD) {
4880 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4884 if (!arg->field.field) {
4886 tep_find_any_field(event, arg->field.name);
4887 if (!arg->field.field) {
4888 do_warning("%s: field %s not found",
4889 __func__, arg->field.name);
4894 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4896 if (sa->ss_family == AF_INET) {
4897 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4899 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4900 trace_seq_printf(s, "INVALIDIPv4");
4904 print_ip4_addr(s, i, reverse, (unsigned char *) &sa4->sin_addr);
4906 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4909 } else if (sa->ss_family == AF_INET6) {
4910 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4912 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4913 trace_seq_printf(s, "INVALIDIPv6");
4918 trace_seq_printf(s, "[");
4920 buf = (unsigned char *) &sa6->sin6_addr;
4922 print_ip6c_addr(s, buf);
4924 print_ip6_addr(s, i, buf);
4927 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4933 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4934 void *data, int size, struct tep_event *event,
4935 struct tep_print_arg *arg)
4937 char i = *ptr; /* 'i' or 'I' */
4945 rc += print_ipv4_arg(s, ptr + 1, i, data, size, event, arg);
4948 rc += print_ipv6_arg(s, ptr + 1, i, data, size, event, arg);
4951 rc += print_ipsa_arg(s, ptr + 1, i, data, size, event, arg);
4960 static const int guid_index[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
4961 static const int uuid_index[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4963 static int print_uuid_arg(struct trace_seq *s, const char *ptr,
4964 void *data, int size, struct tep_event *event,
4965 struct tep_print_arg *arg)
4967 const int *index = uuid_index;
4968 char *format = "%02x";
4973 switch (*(ptr + 1)) {
4989 if (arg->type == TEP_PRINT_FUNC) {
4990 process_defined_func(s, data, size, event, arg);
4994 if (arg->type != TEP_PRINT_FIELD) {
4995 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4999 if (!arg->field.field) {
5001 tep_find_any_field(event, arg->field.name);
5002 if (!arg->field.field) {
5003 do_warning("%s: field %s not found",
5004 __func__, arg->field.name);
5009 if (arg->field.field->size != 16) {
5010 trace_seq_printf(s, "INVALIDUUID");
5014 buf = data + arg->field.field->offset;
5016 for (i = 0; i < 16; i++) {
5017 trace_seq_printf(s, format, buf[index[i]] & 0xff);
5023 trace_seq_printf(s, "-");
5031 static int print_raw_buff_arg(struct trace_seq *s, const char *ptr,
5032 void *data, int size, struct tep_event *event,
5033 struct tep_print_arg *arg, int print_len)
5035 int plen = print_len;
5040 unsigned long offset;
5043 switch (*(ptr + 1)) {
5058 if (arg->type == TEP_PRINT_FUNC) {
5059 process_defined_func(s, data, size, event, arg);
5063 if (arg->type != TEP_PRINT_DYNAMIC_ARRAY) {
5064 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
5068 offset = tep_read_number(event->tep,
5069 data + arg->dynarray.field->offset,
5070 arg->dynarray.field->size);
5071 arr_len = (unsigned long long)(offset >> 16);
5072 buf = data + (offset & 0xffff);
5080 trace_seq_printf(s, "%02x", buf[0] & 0xff);
5081 for (i = 1; i < plen; i++)
5082 trace_seq_printf(s, "%s%02x", delim, buf[i] & 0xff);
5087 static int is_printable_array(char *p, unsigned int len)
5091 for (i = 0; i < len && p[i]; i++)
5092 if (!isprint(p[i]) && !isspace(p[i]))
5097 void tep_print_field(struct trace_seq *s, void *data,
5098 struct tep_format_field *field)
5100 unsigned long long val;
5101 unsigned int offset, len, i;
5102 struct tep_handle *tep = field->event->tep;
5104 if (field->flags & TEP_FIELD_IS_ARRAY) {
5105 offset = field->offset;
5107 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
5108 val = tep_read_number(tep, data + offset, len);
5113 if (field->flags & TEP_FIELD_IS_STRING &&
5114 is_printable_array(data + offset, len)) {
5115 trace_seq_printf(s, "%s", (char *)data + offset);
5117 trace_seq_puts(s, "ARRAY[");
5118 for (i = 0; i < len; i++) {
5120 trace_seq_puts(s, ", ");
5121 trace_seq_printf(s, "%02x",
5122 *((unsigned char *)data + offset + i));
5124 trace_seq_putc(s, ']');
5125 field->flags &= ~TEP_FIELD_IS_STRING;
5128 val = tep_read_number(tep, data + field->offset,
5130 if (field->flags & TEP_FIELD_IS_POINTER) {
5131 trace_seq_printf(s, "0x%llx", val);
5132 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
5133 switch (field->size) {
5136 * If field is long then print it in hex.
5137 * A long usually stores pointers.
5139 if (field->flags & TEP_FIELD_IS_LONG)
5140 trace_seq_printf(s, "0x%x", (int)val);
5142 trace_seq_printf(s, "%d", (int)val);
5145 trace_seq_printf(s, "%2d", (short)val);
5148 trace_seq_printf(s, "%1d", (char)val);
5151 trace_seq_printf(s, "%lld", val);
5154 if (field->flags & TEP_FIELD_IS_LONG)
5155 trace_seq_printf(s, "0x%llx", val);
5157 trace_seq_printf(s, "%llu", val);
5162 void tep_print_fields(struct trace_seq *s, void *data,
5163 int size __maybe_unused, struct tep_event *event)
5165 struct tep_format_field *field;
5167 field = event->format.fields;
5169 trace_seq_printf(s, " %s=", field->name);
5170 tep_print_field(s, data, field);
5171 field = field->next;
5175 static int print_function(struct trace_seq *s, const char *format,
5176 void *data, int size, struct tep_event *event,
5177 struct tep_print_arg *arg)
5179 struct func_map *func;
5180 unsigned long long val;
5182 val = eval_num_arg(data, size, event, arg);
5183 func = find_func(event->tep, val);
5185 trace_seq_puts(s, func->func);
5186 if (*format == 'F' || *format == 'S')
5187 trace_seq_printf(s, "+0x%llx", val - func->addr);
5189 if (event->tep->long_size == 4)
5190 trace_seq_printf(s, "0x%lx", (long)val);
5192 trace_seq_printf(s, "0x%llx", (long long)val);
5198 static int print_arg_pointer(struct trace_seq *s, const char *format, int plen,
5199 void *data, int size,
5200 struct tep_event *event, struct tep_print_arg *arg)
5202 unsigned long long val;
5205 if (arg->type == TEP_PRINT_BSTRING) {
5206 trace_seq_puts(s, arg->string.string);
5210 if (*format == 'p') {
5222 ret += print_function(s, format, data, size, event, arg);
5226 ret += print_mac_arg(s, format, data, size, event, arg);
5230 ret += print_ip_arg(s, format, data, size, event, arg);
5233 ret += print_uuid_arg(s, format, data, size, event, arg);
5236 ret += print_raw_buff_arg(s, format, data, size, event, arg, plen);
5240 val = eval_num_arg(data, size, event, arg);
5241 trace_seq_printf(s, "%p", (void *)(intptr_t)val);
5249 static int print_arg_number(struct trace_seq *s, const char *format, int plen,
5250 void *data, int size, int ls,
5251 struct tep_event *event, struct tep_print_arg *arg)
5253 unsigned long long val;
5255 val = eval_num_arg(data, size, event, arg);
5260 trace_seq_printf(s, format, plen, (char)val);
5262 trace_seq_printf(s, format, (char)val);
5266 trace_seq_printf(s, format, plen, (short)val);
5268 trace_seq_printf(s, format, (short)val);
5272 trace_seq_printf(s, format, plen, (int)val);
5274 trace_seq_printf(s, format, (int)val);
5278 trace_seq_printf(s, format, plen, (long)val);
5280 trace_seq_printf(s, format, (long)val);
5284 trace_seq_printf(s, format, plen, (long long)val);
5286 trace_seq_printf(s, format, (long long)val);
5289 do_warning_event(event, "bad count (%d)", ls);
5290 event->flags |= TEP_EVENT_FL_FAILED;
5296 static void print_arg_string(struct trace_seq *s, const char *format, int plen,
5297 void *data, int size,
5298 struct tep_event *event, struct tep_print_arg *arg)
5302 /* Use helper trace_seq */
5304 print_str_arg(&p, data, size, event,
5306 trace_seq_terminate(&p);
5307 trace_seq_puts(s, p.buffer);
5308 trace_seq_destroy(&p);
5311 static int parse_arg_format_pointer(const char *format)
5327 switch (format[1]) {
5339 switch (format[1]) {
5343 switch (format[index]) {
5358 switch (format[index]) {
5367 if (format[1] == '4') {
5374 if (format[index] == 'c')
5382 switch (format[1]) {
5393 switch (format[1]) {
5409 static void free_parse_args(struct tep_print_parse *arg)
5411 struct tep_print_parse *del;
5421 static int parse_arg_add(struct tep_print_parse **parse, char *format,
5422 enum tep_print_parse_type type,
5423 struct tep_print_arg *arg,
5424 struct tep_print_arg *len_as_arg,
5427 struct tep_print_parse *parg = NULL;
5429 parg = calloc(1, sizeof(*parg));
5432 parg->format = strdup(format);
5437 parg->len_as_arg = len_as_arg;
5449 static int parse_arg_format(struct tep_print_parse **parse,
5450 struct tep_event *event,
5451 const char *format, struct tep_print_arg **arg)
5453 struct tep_print_arg *len_arg = NULL;
5454 char print_format[32];
5455 const char *start = format;
5463 for (; *format; format++) {
5466 /* FIXME: need to handle properly */
5484 /* The argument is the length. */
5486 do_warning_event(event, "no argument match");
5487 event->flags |= TEP_EVENT_FL_FAILED;
5491 do_warning_event(event, "argument already matched");
5492 event->flags |= TEP_EVENT_FL_FAILED;
5496 *arg = (*arg)->next;
5500 do_warning_event(event, "no argument match");
5501 event->flags |= TEP_EVENT_FL_FAILED;
5504 res = parse_arg_format_pointer(format + 1);
5509 len = ((unsigned long)format + 1) -
5510 (unsigned long)start;
5511 /* should never happen */
5513 do_warning_event(event, "bad format!");
5514 event->flags |= TEP_EVENT_FL_FAILED;
5517 memcpy(print_format, start, len);
5518 print_format[len] = 0;
5520 parse_arg_add(parse, print_format,
5521 PRINT_FMT_ARG_POINTER, *arg, len_arg, ls);
5522 *arg = (*arg)->next;
5532 do_warning_event(event, "no argument match");
5533 event->flags |= TEP_EVENT_FL_FAILED;
5537 len = ((unsigned long)format + 1) -
5538 (unsigned long)start;
5540 /* should never happen */
5542 do_warning_event(event, "bad format!");
5543 event->flags |= TEP_EVENT_FL_FAILED;
5546 memcpy(print_format, start, len);
5547 print_format[len] = 0;
5549 if (event->tep->long_size == 8 && ls == 1 &&
5550 sizeof(long) != 8) {
5553 /* make %l into %ll */
5554 if (ls == 1 && (p = strchr(print_format, 'l')))
5555 memmove(p+1, p, strlen(p)+1);
5558 if (ls < -2 || ls > 2) {
5559 do_warning_event(event, "bad count (%d)", ls);
5560 event->flags |= TEP_EVENT_FL_FAILED;
5562 parse_arg_add(parse, print_format,
5563 PRINT_FMT_ARG_DIGIT, *arg, len_arg, ls);
5564 *arg = (*arg)->next;
5569 do_warning_event(event, "no matching argument");
5570 event->flags |= TEP_EVENT_FL_FAILED;
5574 len = ((unsigned long)format + 1) -
5575 (unsigned long)start;
5577 /* should never happen */
5579 do_warning_event(event, "bad format!");
5580 event->flags |= TEP_EVENT_FL_FAILED;
5584 memcpy(print_format, start, len);
5585 print_format[len] = 0;
5587 parse_arg_add(parse, print_format,
5588 PRINT_FMT_ARG_STRING, *arg, len_arg, 0);
5589 *arg = (*arg)->next;
5593 snprintf(print_format, 32, ">%c<", *format);
5594 parse_arg_add(parse, print_format,
5595 PRINT_FMT_STRING, NULL, NULL, 0);
5607 static int parse_arg_string(struct tep_print_parse **parse, const char *format)
5613 for (; *format; format++) {
5614 if (*format == '\\') {
5619 trace_seq_putc(&s, '\n');
5622 trace_seq_putc(&s, '\t');
5625 trace_seq_putc(&s, '\r');
5628 trace_seq_putc(&s, '\\');
5631 trace_seq_putc(&s, *format);
5634 } else if (*format == '%') {
5635 if (*(format + 1) == '%') {
5636 trace_seq_putc(&s, '%');
5642 trace_seq_putc(&s, *format);
5646 trace_seq_terminate(&s);
5647 parse_arg_add(parse, s.buffer, PRINT_FMT_STRING, NULL, NULL, 0);
5648 trace_seq_destroy(&s);
5653 static struct tep_print_parse *
5654 parse_args(struct tep_event *event, const char *format, struct tep_print_arg *arg)
5656 struct tep_print_parse *parse_ret = NULL;
5657 struct tep_print_parse **parse = NULL;
5661 len = strlen(format);
5665 if (*format == '%' && *(format + 1) != '%')
5666 ret = parse_arg_format(parse, event, format, &arg);
5668 ret = parse_arg_string(parse, format);
5670 parse = &((*parse)->next);
5681 static void print_event_cache(struct tep_print_parse *parse, struct trace_seq *s,
5682 void *data, int size, struct tep_event *event)
5687 if (parse->len_as_arg)
5688 len_arg = eval_num_arg(data, size, event, parse->len_as_arg);
5689 switch (parse->type) {
5690 case PRINT_FMT_ARG_DIGIT:
5691 print_arg_number(s, parse->format,
5692 parse->len_as_arg ? len_arg : -1, data,
5693 size, parse->ls, event, parse->arg);
5695 case PRINT_FMT_ARG_POINTER:
5696 print_arg_pointer(s, parse->format,
5697 parse->len_as_arg ? len_arg : 1,
5698 data, size, event, parse->arg);
5700 case PRINT_FMT_ARG_STRING:
5701 print_arg_string(s, parse->format,
5702 parse->len_as_arg ? len_arg : -1,
5703 data, size, event, parse->arg);
5705 case PRINT_FMT_STRING:
5707 trace_seq_printf(s, "%s", parse->format);
5710 parse = parse->next;
5714 static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
5716 struct tep_print_parse *parse = event->print_fmt.print_cache;
5717 struct tep_print_arg *args = NULL;
5718 char *bprint_fmt = NULL;
5720 if (event->flags & TEP_EVENT_FL_FAILED) {
5721 trace_seq_printf(s, "[FAILED TO PARSE]");
5722 tep_print_fields(s, data, size, event);
5726 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
5727 bprint_fmt = get_bprint_format(data, size, event);
5728 args = make_bprint_args(bprint_fmt, data, size, event);
5729 parse = parse_args(event, bprint_fmt, args);
5732 print_event_cache(parse, s, data, size, event);
5734 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
5735 free_parse_args(parse);
5742 * This parses out the Latency format (interrupts disabled,
5743 * need rescheduling, in hard/soft interrupt, preempt count
5744 * and lock depth) and places it into the trace_seq.
5746 static void data_latency_format(struct tep_handle *tep, struct trace_seq *s,
5747 char *format, struct tep_record *record)
5749 static int check_lock_depth = 1;
5750 static int check_migrate_disable = 1;
5751 static int lock_depth_exists;
5752 static int migrate_disable_exists;
5753 unsigned int lat_flags;
5754 struct trace_seq sq;
5757 int migrate_disable = 0;
5760 void *data = record->data;
5762 trace_seq_init(&sq);
5763 lat_flags = parse_common_flags(tep, data);
5764 pc = parse_common_pc(tep, data);
5765 /* lock_depth may not always exist */
5766 if (lock_depth_exists)
5767 lock_depth = parse_common_lock_depth(tep, data);
5768 else if (check_lock_depth) {
5769 lock_depth = parse_common_lock_depth(tep, data);
5771 check_lock_depth = 0;
5773 lock_depth_exists = 1;
5776 /* migrate_disable may not always exist */
5777 if (migrate_disable_exists)
5778 migrate_disable = parse_common_migrate_disable(tep, data);
5779 else if (check_migrate_disable) {
5780 migrate_disable = parse_common_migrate_disable(tep, data);
5781 if (migrate_disable < 0)
5782 check_migrate_disable = 0;
5784 migrate_disable_exists = 1;
5787 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5788 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5790 trace_seq_printf(&sq, "%c%c%c",
5791 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5792 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5794 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5796 (hardirq && softirq) ? 'H' :
5797 hardirq ? 'h' : softirq ? 's' : '.');
5800 trace_seq_printf(&sq, "%x", pc);
5802 trace_seq_printf(&sq, ".");
5804 if (migrate_disable_exists) {
5805 if (migrate_disable < 0)
5806 trace_seq_printf(&sq, ".");
5808 trace_seq_printf(&sq, "%d", migrate_disable);
5811 if (lock_depth_exists) {
5813 trace_seq_printf(&sq, ".");
5815 trace_seq_printf(&sq, "%d", lock_depth);
5818 if (sq.state == TRACE_SEQ__MEM_ALLOC_FAILED) {
5819 s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
5823 trace_seq_terminate(&sq);
5824 trace_seq_puts(s, sq.buffer);
5825 trace_seq_destroy(&sq);
5826 trace_seq_terminate(s);
5830 * tep_data_type - parse out the given event type
5831 * @tep: a handle to the trace event parser context
5832 * @rec: the record to read from
5834 * This returns the event id from the @rec.
5836 int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
5838 return trace_parse_common_type(tep, rec->data);
5842 * tep_data_pid - parse the PID from record
5843 * @tep: a handle to the trace event parser context
5844 * @rec: the record to parse
5846 * This returns the PID from a record.
5848 int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
5850 return parse_common_pid(tep, rec->data);
5854 * tep_data_preempt_count - parse the preempt count from the record
5855 * @tep: a handle to the trace event parser context
5856 * @rec: the record to parse
5858 * This returns the preempt count from a record.
5860 int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
5862 return parse_common_pc(tep, rec->data);
5866 * tep_data_flags - parse the latency flags from the record
5867 * @tep: a handle to the trace event parser context
5868 * @rec: the record to parse
5870 * This returns the latency flags from a record.
5872 * Use trace_flag_type enum for the flags (see event-parse.h).
5874 int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
5876 return parse_common_flags(tep, rec->data);
5880 * tep_data_comm_from_pid - return the command line from PID
5881 * @tep: a handle to the trace event parser context
5882 * @pid: the PID of the task to search for
5884 * This returns a pointer to the command line that has the given
5887 const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
5891 comm = find_cmdline(tep, pid);
5895 static struct tep_cmdline *
5896 pid_from_cmdlist(struct tep_handle *tep, const char *comm, struct tep_cmdline *next)
5898 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5901 cmdlist = cmdlist->next;
5903 cmdlist = tep->cmdlist;
5905 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5906 cmdlist = cmdlist->next;
5908 return (struct tep_cmdline *)cmdlist;
5912 * tep_data_pid_from_comm - return the pid from a given comm
5913 * @tep: a handle to the trace event parser context
5914 * @comm: the cmdline to find the pid from
5915 * @next: the cmdline structure to find the next comm
5917 * This returns the cmdline structure that holds a pid for a given
5918 * comm, or NULL if none found. As there may be more than one pid for
5919 * a given comm, the result of this call can be passed back into
5920 * a recurring call in the @next parameter, and then it will find the
5922 * Also, it does a linear search, so it may be slow.
5924 struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
5925 struct tep_cmdline *next)
5927 struct tep_cmdline *cmdline;
5930 * If the cmdlines have not been converted yet, then use
5934 return pid_from_cmdlist(tep, comm, next);
5938 * The next pointer could have been still from
5939 * a previous call before cmdlines were created
5941 if (next < tep->cmdlines ||
5942 next >= tep->cmdlines + tep->cmdline_count)
5949 cmdline = tep->cmdlines;
5951 while (cmdline < tep->cmdlines + tep->cmdline_count) {
5952 if (strcmp(cmdline->comm, comm) == 0)
5960 * tep_cmdline_pid - return the pid associated to a given cmdline
5961 * @tep: a handle to the trace event parser context
5962 * @cmdline: The cmdline structure to get the pid from
5964 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5967 int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
5969 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5975 * If cmdlines have not been created yet, or cmdline is
5976 * not part of the array, then treat it as a cmdlist instead.
5978 if (!tep->cmdlines ||
5979 cmdline < tep->cmdlines ||
5980 cmdline >= tep->cmdlines + tep->cmdline_count)
5981 return cmdlist->pid;
5983 return cmdline->pid;
5987 * This parses the raw @data using the given @event information and
5988 * writes the print format into the trace_seq.
5990 static void print_event_info(struct trace_seq *s, char *format, bool raw,
5991 struct tep_event *event, struct tep_record *record)
5993 int print_pretty = 1;
5995 if (raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
5996 tep_print_fields(s, record->data, record->size, event);
5999 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
6000 print_pretty = event->handler(s, record, event,
6004 pretty_print(s, record->data, record->size, event);
6007 trace_seq_terminate(s);
6011 * tep_find_event_by_record - return the event from a given record
6012 * @tep: a handle to the trace event parser context
6013 * @record: The record to get the event from
6015 * Returns the associated event for a given record, or NULL if non is
6019 tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
6023 if (record->size < 0) {
6024 do_warning("ug! negative record size %d", record->size);
6028 type = trace_parse_common_type(tep, record->data);
6030 return tep_find_event(tep, type);
6034 * Writes the timestamp of the record into @s. Time divisor and precision can be
6035 * specified as part of printf @format string. Example:
6036 * "%3.1000d" - divide the time by 1000 and print the first 3 digits
6037 * before the dot. Thus, the timestamp "123456000" will be printed as
6040 static void print_event_time(struct tep_handle *tep, struct trace_seq *s,
6041 char *format, struct tep_event *event,
6042 struct tep_record *record)
6044 unsigned long long time;
6050 if (isdigit(*(format + 1)))
6051 prec = atoi(format + 1);
6052 divstr = strchr(format, '.');
6053 if (divstr && isdigit(*(divstr + 1)))
6054 div = atoi(divstr + 1);
6064 if (p10 > 1 && p10 < time)
6065 trace_seq_printf(s, "%5llu.%0*llu", time / p10, prec, time % p10);
6067 trace_seq_printf(s, "%12llu", time);
6070 struct print_event_type {
6079 static void print_string(struct tep_handle *tep, struct trace_seq *s,
6080 struct tep_record *record, struct tep_event *event,
6081 const char *arg, struct print_event_type *type)
6086 if (strncmp(arg, TEP_PRINT_LATENCY, strlen(TEP_PRINT_LATENCY)) == 0) {
6087 data_latency_format(tep, s, type->format, record);
6088 } else if (strncmp(arg, TEP_PRINT_COMM, strlen(TEP_PRINT_COMM)) == 0) {
6089 pid = parse_common_pid(tep, record->data);
6090 comm = find_cmdline(tep, pid);
6091 trace_seq_printf(s, type->format, comm);
6092 } else if (strncmp(arg, TEP_PRINT_INFO_RAW, strlen(TEP_PRINT_INFO_RAW)) == 0) {
6093 print_event_info(s, type->format, true, event, record);
6094 } else if (strncmp(arg, TEP_PRINT_INFO, strlen(TEP_PRINT_INFO)) == 0) {
6095 print_event_info(s, type->format, false, event, record);
6096 } else if (strncmp(arg, TEP_PRINT_NAME, strlen(TEP_PRINT_NAME)) == 0) {
6097 trace_seq_printf(s, type->format, event->name);
6099 trace_seq_printf(s, "[UNKNOWN TEP TYPE %s]", arg);
6104 static void print_int(struct tep_handle *tep, struct trace_seq *s,
6105 struct tep_record *record, struct tep_event *event,
6106 int arg, struct print_event_type *type)
6112 param = record->cpu;
6115 param = parse_common_pid(tep, record->data);
6117 case TEP_PRINT_TIME:
6118 return print_event_time(tep, s, type->format, event, record);
6122 trace_seq_printf(s, type->format, param);
6125 static int tep_print_event_param_type(char *format,
6126 struct print_event_type *type)
6128 char *str = format + 1;
6131 type->type = EVENT_TYPE_UNKNOWN;
6140 type->type = EVENT_TYPE_INT;
6143 type->type = EVENT_TYPE_STRING;
6148 if (type->type != EVENT_TYPE_UNKNOWN)
6151 memset(type->format, 0, 32);
6152 memcpy(type->format, format, i < 32 ? i : 31);
6157 * tep_print_event - Write various event information
6158 * @tep: a handle to the trace event parser context
6159 * @s: the trace_seq to write to
6160 * @record: The record to get the event from
6161 * @format: a printf format string. Supported event fileds:
6162 * TEP_PRINT_PID, "%d" - event PID
6163 * TEP_PRINT_CPU, "%d" - event CPU
6164 * TEP_PRINT_COMM, "%s" - event command string
6165 * TEP_PRINT_NAME, "%s" - event name
6166 * TEP_PRINT_LATENCY, "%s" - event latency
6167 * TEP_PRINT_TIME, %d - event time stamp. A divisor and precision
6168 * can be specified as part of this format string:
6169 * "%precision.divisord". Example:
6170 * "%3.1000d" - divide the time by 1000 and print the first
6171 * 3 digits before the dot. Thus, the time stamp
6172 * "123456000" will be printed as "123.456"
6173 * TEP_PRINT_INFO, "%s" - event information. If any width is specified in
6174 * the format string, the event information will be printed
6176 * Writes the specified event information into @s.
6178 void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
6179 struct tep_record *record, const char *fmt, ...)
6181 struct print_event_type type;
6182 char *format = strdup(fmt);
6183 char *current = format;
6187 struct tep_event *event;
6192 event = tep_find_event_by_record(tep, record);
6193 va_start(args, fmt);
6195 current = strchr(str, '%');
6197 trace_seq_puts(s, str);
6200 memset(&type, 0, sizeof(type));
6201 offset = tep_print_event_param_type(current, &type);
6203 trace_seq_puts(s, str);
6205 switch (type.type) {
6206 case EVENT_TYPE_STRING:
6207 print_string(tep, s, record, event,
6208 va_arg(args, char*), &type);
6210 case EVENT_TYPE_INT:
6211 print_int(tep, s, record, event,
6212 va_arg(args, int), &type);
6214 case EVENT_TYPE_UNKNOWN:
6216 trace_seq_printf(s, "[UNKNOWN TYPE]");
6226 static int events_id_cmp(const void *a, const void *b)
6228 struct tep_event * const * ea = a;
6229 struct tep_event * const * eb = b;
6231 if ((*ea)->id < (*eb)->id)
6234 if ((*ea)->id > (*eb)->id)
6240 static int events_name_cmp(const void *a, const void *b)
6242 struct tep_event * const * ea = a;
6243 struct tep_event * const * eb = b;
6246 res = strcmp((*ea)->name, (*eb)->name);
6250 res = strcmp((*ea)->system, (*eb)->system);
6254 return events_id_cmp(a, b);
6257 static int events_system_cmp(const void *a, const void *b)
6259 struct tep_event * const * ea = a;
6260 struct tep_event * const * eb = b;
6263 res = strcmp((*ea)->system, (*eb)->system);
6267 res = strcmp((*ea)->name, (*eb)->name);
6271 return events_id_cmp(a, b);
6274 static struct tep_event **list_events_copy(struct tep_handle *tep)
6276 struct tep_event **events;
6281 events = malloc(sizeof(*events) * (tep->nr_events + 1));
6285 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
6286 events[tep->nr_events] = NULL;
6290 static void list_events_sort(struct tep_event **events, int nr_events,
6291 enum tep_event_sort_type sort_type)
6293 int (*sort)(const void *a, const void *b);
6295 switch (sort_type) {
6296 case TEP_EVENT_SORT_ID:
6297 sort = events_id_cmp;
6299 case TEP_EVENT_SORT_NAME:
6300 sort = events_name_cmp;
6302 case TEP_EVENT_SORT_SYSTEM:
6303 sort = events_system_cmp;
6310 qsort(events, nr_events, sizeof(*events), sort);
6314 * tep_list_events - Get events, sorted by given criteria.
6315 * @tep: a handle to the tep context
6316 * @sort_type: desired sort order of the events in the array
6318 * Returns an array of pointers to all events, sorted by the given
6319 * @sort_type criteria. The last element of the array is NULL. The returned
6320 * memory must not be freed, it is managed by the library.
6321 * The function is not thread safe.
6323 struct tep_event **tep_list_events(struct tep_handle *tep,
6324 enum tep_event_sort_type sort_type)
6326 struct tep_event **events;
6331 events = tep->sort_events;
6332 if (events && tep->last_type == sort_type)
6336 events = list_events_copy(tep);
6340 tep->sort_events = events;
6342 /* the internal events are sorted by id */
6343 if (sort_type == TEP_EVENT_SORT_ID) {
6344 tep->last_type = sort_type;
6349 list_events_sort(events, tep->nr_events, sort_type);
6350 tep->last_type = sort_type;
6357 * tep_list_events_copy - Thread safe version of tep_list_events()
6358 * @tep: a handle to the tep context
6359 * @sort_type: desired sort order of the events in the array
6361 * Returns an array of pointers to all events, sorted by the given
6362 * @sort_type criteria. The last element of the array is NULL. The returned
6363 * array is newly allocated inside the function and must be freed by the caller
6365 struct tep_event **tep_list_events_copy(struct tep_handle *tep,
6366 enum tep_event_sort_type sort_type)
6368 struct tep_event **events;
6373 events = list_events_copy(tep);
6377 /* the internal events are sorted by id */
6378 if (sort_type == TEP_EVENT_SORT_ID)
6381 list_events_sort(events, tep->nr_events, sort_type);
6386 static struct tep_format_field **
6387 get_event_fields(const char *type, const char *name,
6388 int count, struct tep_format_field *list)
6390 struct tep_format_field **fields;
6391 struct tep_format_field *field;
6394 fields = malloc(sizeof(*fields) * (count + 1));
6398 for (field = list; field; field = field->next) {
6399 fields[i++] = field;
6400 if (i == count + 1) {
6401 do_warning("event %s has more %s fields than specified",
6409 do_warning("event %s has less %s fields than specified",
6418 * tep_event_common_fields - return a list of common fields for an event
6419 * @event: the event to return the common fields of.
6421 * Returns an allocated array of fields. The last item in the array is NULL.
6422 * The array must be freed with free().
6424 struct tep_format_field **tep_event_common_fields(struct tep_event *event)
6426 return get_event_fields("common", event->name,
6427 event->format.nr_common,
6428 event->format.common_fields);
6432 * tep_event_fields - return a list of event specific fields for an event
6433 * @event: the event to return the fields of.
6435 * Returns an allocated array of fields. The last item in the array is NULL.
6436 * The array must be freed with free().
6438 struct tep_format_field **tep_event_fields(struct tep_event *event)
6440 return get_event_fields("event", event->name,
6441 event->format.nr_fields,
6442 event->format.fields);
6445 static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
6447 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
6449 trace_seq_puts(s, ", ");
6450 print_fields(s, field->next);
6455 static void print_args(struct tep_print_arg *args)
6457 int print_paren = 1;
6460 switch (args->type) {
6461 case TEP_PRINT_NULL:
6464 case TEP_PRINT_ATOM:
6465 printf("%s", args->atom.atom);
6467 case TEP_PRINT_FIELD:
6468 printf("REC->%s", args->field.name);
6470 case TEP_PRINT_FLAGS:
6471 printf("__print_flags(");
6472 print_args(args->flags.field);
6473 printf(", %s, ", args->flags.delim);
6475 print_fields(&s, args->flags.flags);
6476 trace_seq_do_printf(&s);
6477 trace_seq_destroy(&s);
6480 case TEP_PRINT_SYMBOL:
6481 printf("__print_symbolic(");
6482 print_args(args->symbol.field);
6485 print_fields(&s, args->symbol.symbols);
6486 trace_seq_do_printf(&s);
6487 trace_seq_destroy(&s);
6491 printf("__print_hex(");
6492 print_args(args->hex.field);
6494 print_args(args->hex.size);
6497 case TEP_PRINT_HEX_STR:
6498 printf("__print_hex_str(");
6499 print_args(args->hex.field);
6501 print_args(args->hex.size);
6504 case TEP_PRINT_INT_ARRAY:
6505 printf("__print_array(");
6506 print_args(args->int_array.field);
6508 print_args(args->int_array.count);
6510 print_args(args->int_array.el_size);
6513 case TEP_PRINT_STRING:
6514 case TEP_PRINT_BSTRING:
6515 printf("__get_str(%s)", args->string.string);
6517 case TEP_PRINT_BITMASK:
6518 printf("__get_bitmask(%s)", args->bitmask.bitmask);
6520 case TEP_PRINT_TYPE:
6521 printf("(%s)", args->typecast.type);
6522 print_args(args->typecast.item);
6525 if (strcmp(args->op.op, ":") == 0)
6529 print_args(args->op.left);
6530 printf(" %s ", args->op.op);
6531 print_args(args->op.right);
6536 /* we should warn... */
6541 print_args(args->next);
6545 static void parse_header_field(const char *field,
6546 int *offset, int *size, int mandatory)
6548 unsigned long long save_input_buf_ptr;
6549 unsigned long long save_input_buf_siz;
6553 save_input_buf_ptr = input_buf_ptr;
6554 save_input_buf_siz = input_buf_siz;
6556 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
6558 if (read_expected(TEP_EVENT_OP, ":") < 0)
6562 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6567 * If this is not a mandatory field, then test it first.
6570 if (read_expected(TEP_EVENT_ITEM, field) < 0)
6573 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6575 if (strcmp(token, field) != 0)
6580 if (read_expected(TEP_EVENT_OP, ";") < 0)
6582 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
6584 if (read_expected(TEP_EVENT_OP, ":") < 0)
6586 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6588 *offset = atoi(token);
6590 if (read_expected(TEP_EVENT_OP, ";") < 0)
6592 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
6594 if (read_expected(TEP_EVENT_OP, ":") < 0)
6596 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
6598 *size = atoi(token);
6600 if (read_expected(TEP_EVENT_OP, ";") < 0)
6602 type = read_token(&token);
6603 if (type != TEP_EVENT_NEWLINE) {
6604 /* newer versions of the kernel have a "signed" type */
6605 if (type != TEP_EVENT_ITEM)
6608 if (strcmp(token, "signed") != 0)
6613 if (read_expected(TEP_EVENT_OP, ":") < 0)
6616 if (read_expect_type(TEP_EVENT_ITEM, &token))
6620 if (read_expected(TEP_EVENT_OP, ";") < 0)
6623 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
6631 input_buf_ptr = save_input_buf_ptr;
6632 input_buf_siz = save_input_buf_siz;
6639 * tep_parse_header_page - parse the data stored in the header page
6640 * @tep: a handle to the trace event parser context
6641 * @buf: the buffer storing the header page format string
6642 * @size: the size of @buf
6643 * @long_size: the long size to use if there is no header
6645 * This parses the header page format for information on the
6646 * ring buffer used. The @buf should be copied from
6648 * /sys/kernel/debug/tracing/events/header_page
6650 int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
6657 * Old kernels did not have header page info.
6658 * Sorry but we just use what we find here in user space.
6660 tep->header_page_ts_size = sizeof(long long);
6661 tep->header_page_size_size = long_size;
6662 tep->header_page_data_offset = sizeof(long long) + long_size;
6663 tep->old_format = 1;
6666 init_input_buf(buf, size);
6668 parse_header_field("timestamp", &tep->header_page_ts_offset,
6669 &tep->header_page_ts_size, 1);
6670 parse_header_field("commit", &tep->header_page_size_offset,
6671 &tep->header_page_size_size, 1);
6672 parse_header_field("overwrite", &tep->header_page_overwrite,
6674 parse_header_field("data", &tep->header_page_data_offset,
6675 &tep->header_page_data_size, 1);
6680 static int event_matches(struct tep_event *event,
6681 int id, const char *sys_name,
6682 const char *event_name)
6684 if (id >= 0 && id != event->id)
6687 if (event_name && (strcmp(event_name, event->name) != 0))
6690 if (sys_name && (strcmp(sys_name, event->system) != 0))
6696 static void free_handler(struct event_handler *handle)
6698 free((void *)handle->sys_name);
6699 free((void *)handle->event_name);
6703 static int find_event_handle(struct tep_handle *tep, struct tep_event *event)
6705 struct event_handler *handle, **next;
6707 for (next = &tep->handlers; *next;
6708 next = &(*next)->next) {
6710 if (event_matches(event, handle->id,
6712 handle->event_name))
6719 pr_stat("overriding event (%d) %s:%s with new print handler",
6720 event->id, event->system, event->name);
6722 event->handler = handle->func;
6723 event->context = handle->context;
6725 *next = handle->next;
6726 free_handler(handle);
6732 * parse_format - parse the event format
6733 * @buf: the buffer storing the event format string
6734 * @size: the size of @buf
6735 * @sys: the system the event belongs to
6737 * This parses the event format and creates an event structure
6738 * to quickly parse raw data for a given event.
6740 * These files currently come from:
6742 * /sys/kernel/debug/tracing/events/.../.../format
6744 static enum tep_errno parse_format(struct tep_event **eventp,
6745 struct tep_handle *tep, const char *buf,
6746 unsigned long size, const char *sys)
6748 struct tep_event *event;
6751 init_input_buf(buf, size);
6753 *eventp = event = alloc_event();
6755 return TEP_ERRNO__MEM_ALLOC_FAILED;
6757 event->name = event_read_name();
6760 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6761 goto event_alloc_failed;
6764 if (strcmp(sys, "ftrace") == 0) {
6765 event->flags |= TEP_EVENT_FL_ISFTRACE;
6767 if (strcmp(event->name, "bprint") == 0)
6768 event->flags |= TEP_EVENT_FL_ISBPRINT;
6771 event->id = event_read_id();
6772 if (event->id < 0) {
6773 ret = TEP_ERRNO__READ_ID_FAILED;
6775 * This isn't an allocation error actually.
6776 * But as the ID is critical, just bail out.
6778 goto event_alloc_failed;
6781 event->system = strdup(sys);
6782 if (!event->system) {
6783 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6784 goto event_alloc_failed;
6787 /* Add tep to event so that it can be referenced */
6790 ret = event_read_format(event);
6792 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6793 goto event_parse_failed;
6797 * If the event has an override, don't print warnings if the event
6798 * print format fails to parse.
6800 if (tep && find_event_handle(tep, event))
6803 ret = event_read_print(event);
6807 ret = TEP_ERRNO__READ_PRINT_FAILED;
6808 goto event_parse_failed;
6811 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
6812 struct tep_format_field *field;
6813 struct tep_print_arg *arg, **list;
6815 /* old ftrace had no args */
6816 list = &event->print_fmt.args;
6817 for (field = event->format.fields; field; field = field->next) {
6820 event->flags |= TEP_EVENT_FL_FAILED;
6821 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6823 arg->type = TEP_PRINT_FIELD;
6824 arg->field.name = strdup(field->name);
6825 if (!arg->field.name) {
6826 event->flags |= TEP_EVENT_FL_FAILED;
6828 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6830 arg->field.field = field;
6836 if (!(event->flags & TEP_EVENT_FL_ISBPRINT))
6837 event->print_fmt.print_cache = parse_args(event,
6838 event->print_fmt.format,
6839 event->print_fmt.args);
6844 event->flags |= TEP_EVENT_FL_FAILED;
6848 free(event->system);
6855 static enum tep_errno
6856 __parse_event(struct tep_handle *tep,
6857 struct tep_event **eventp,
6858 const char *buf, unsigned long size,
6861 int ret = parse_format(eventp, tep, buf, size, sys);
6862 struct tep_event *event = *eventp;
6867 if (tep && add_event(tep, event)) {
6868 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6869 goto event_add_failed;
6872 #define PRINT_ARGS 0
6873 if (PRINT_ARGS && event->print_fmt.args)
6874 print_args(event->print_fmt.args);
6879 free_tep_event(event);
6884 * tep_parse_format - parse the event format
6885 * @tep: a handle to the trace event parser context
6886 * @eventp: returned format
6887 * @buf: the buffer storing the event format string
6888 * @size: the size of @buf
6889 * @sys: the system the event belongs to
6891 * This parses the event format and creates an event structure
6892 * to quickly parse raw data for a given event.
6894 * These files currently come from:
6896 * /sys/kernel/debug/tracing/events/.../.../format
6898 enum tep_errno tep_parse_format(struct tep_handle *tep,
6899 struct tep_event **eventp,
6901 unsigned long size, const char *sys)
6903 return __parse_event(tep, eventp, buf, size, sys);
6907 * tep_parse_event - parse the event format
6908 * @tep: a handle to the trace event parser context
6909 * @buf: the buffer storing the event format string
6910 * @size: the size of @buf
6911 * @sys: the system the event belongs to
6913 * This parses the event format and creates an event structure
6914 * to quickly parse raw data for a given event.
6916 * These files currently come from:
6918 * /sys/kernel/debug/tracing/events/.../.../format
6920 enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
6921 unsigned long size, const char *sys)
6923 struct tep_event *event = NULL;
6924 return __parse_event(tep, &event, buf, size, sys);
6927 int get_field_val(struct trace_seq *s, struct tep_format_field *field,
6928 const char *name, struct tep_record *record,
6929 unsigned long long *val, int err)
6933 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6937 if (tep_read_number_field(field, record->data, val)) {
6939 trace_seq_printf(s, " %s=INVALID", name);
6947 * tep_get_field_raw - return the raw pointer into the data field
6948 * @s: The seq to print to on error
6949 * @event: the event that the field is for
6950 * @name: The name of the field
6951 * @record: The record with the field name.
6952 * @len: place to store the field length.
6953 * @err: print default error if failed.
6955 * Returns a pointer into record->data of the field and places
6956 * the length of the field in @len.
6958 * On failure, it returns NULL.
6960 void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
6961 const char *name, struct tep_record *record,
6964 struct tep_format_field *field;
6965 void *data = record->data;
6972 field = tep_find_field(event, name);
6976 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6980 /* Allow @len to be NULL */
6984 offset = field->offset;
6985 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
6986 offset = tep_read_number(event->tep,
6987 data + offset, field->size);
6988 *len = offset >> 16;
6993 return data + offset;
6997 * tep_get_field_val - find a field and return its value
6998 * @s: The seq to print to on error
6999 * @event: the event that the field is for
7000 * @name: The name of the field
7001 * @record: The record with the field name.
7002 * @val: place to store the value of the field.
7003 * @err: print default error if failed.
7005 * Returns 0 on success -1 on field not found.
7007 int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
7008 const char *name, struct tep_record *record,
7009 unsigned long long *val, int err)
7011 struct tep_format_field *field;
7016 field = tep_find_field(event, name);
7018 return get_field_val(s, field, name, record, val, err);
7022 * tep_get_common_field_val - find a common field and return its value
7023 * @s: The seq to print to on error
7024 * @event: the event that the field is for
7025 * @name: The name of the field
7026 * @record: The record with the field name.
7027 * @val: place to store the value of the field.
7028 * @err: print default error if failed.
7030 * Returns 0 on success -1 on field not found.
7032 int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
7033 const char *name, struct tep_record *record,
7034 unsigned long long *val, int err)
7036 struct tep_format_field *field;
7041 field = tep_find_common_field(event, name);
7043 return get_field_val(s, field, name, record, val, err);
7047 * tep_get_any_field_val - find a any field and return its value
7048 * @s: The seq to print to on error
7049 * @event: the event that the field is for
7050 * @name: The name of the field
7051 * @record: The record with the field name.
7052 * @val: place to store the value of the field.
7053 * @err: print default error if failed.
7055 * Returns 0 on success -1 on field not found.
7057 int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
7058 const char *name, struct tep_record *record,
7059 unsigned long long *val, int err)
7061 struct tep_format_field *field;
7066 field = tep_find_any_field(event, name);
7068 return get_field_val(s, field, name, record, val, err);
7072 * tep_print_num_field - print a field and a format
7073 * @s: The seq to print to
7074 * @fmt: The printf format to print the field with.
7075 * @event: the event that the field is for
7076 * @name: The name of the field
7077 * @record: The record with the field name.
7078 * @err: print default error if failed.
7080 * Returns positive value on success, negative in case of an error,
7081 * or 0 if buffer is full.
7083 int tep_print_num_field(struct trace_seq *s, const char *fmt,
7084 struct tep_event *event, const char *name,
7085 struct tep_record *record, int err)
7087 struct tep_format_field *field = tep_find_field(event, name);
7088 unsigned long long val;
7093 if (tep_read_number_field(field, record->data, &val))
7096 return trace_seq_printf(s, fmt, val);
7100 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
7105 * tep_print_func_field - print a field and a format for function pointers
7106 * @s: The seq to print to
7107 * @fmt: The printf format to print the field with.
7108 * @event: the event that the field is for
7109 * @name: The name of the field
7110 * @record: The record with the field name.
7111 * @err: print default error if failed.
7113 * Returns positive value on success, negative in case of an error,
7114 * or 0 if buffer is full.
7116 int tep_print_func_field(struct trace_seq *s, const char *fmt,
7117 struct tep_event *event, const char *name,
7118 struct tep_record *record, int err)
7120 struct tep_format_field *field = tep_find_field(event, name);
7121 struct tep_handle *tep = event->tep;
7122 unsigned long long val;
7123 struct func_map *func;
7129 if (tep_read_number_field(field, record->data, &val))
7132 func = find_func(tep, val);
7135 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
7137 sprintf(tmp, "0x%08llx", val);
7139 return trace_seq_printf(s, fmt, tmp);
7143 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
7147 static void free_func_handle(struct tep_function_handler *func)
7149 struct func_params *params;
7153 while (func->params) {
7154 params = func->params;
7155 func->params = params->next;
7163 * tep_register_print_function - register a helper function
7164 * @tep: a handle to the trace event parser context
7165 * @func: the function to process the helper function
7166 * @ret_type: the return type of the helper function
7167 * @name: the name of the helper function
7168 * @parameters: A list of enum tep_func_arg_type
7170 * Some events may have helper functions in the print format arguments.
7171 * This allows a plugin to dynamically create a way to process one
7172 * of these functions.
7174 * The @parameters is a variable list of tep_func_arg_type enums that
7175 * must end with TEP_FUNC_ARG_VOID.
7177 int tep_register_print_function(struct tep_handle *tep,
7178 tep_func_handler func,
7179 enum tep_func_arg_type ret_type,
7182 struct tep_function_handler *func_handle;
7183 struct func_params **next_param;
7184 struct func_params *param;
7185 enum tep_func_arg_type type;
7189 func_handle = find_func_handler(tep, name);
7192 * This is most like caused by the users own
7193 * plugins updating the function. This overrides the
7196 pr_stat("override of function helper '%s'", name);
7197 remove_func_handler(tep, name);
7200 func_handle = calloc(1, sizeof(*func_handle));
7202 do_warning("Failed to allocate function handler");
7203 return TEP_ERRNO__MEM_ALLOC_FAILED;
7206 func_handle->ret_type = ret_type;
7207 func_handle->name = strdup(name);
7208 func_handle->func = func;
7209 if (!func_handle->name) {
7210 do_warning("Failed to allocate function name");
7212 return TEP_ERRNO__MEM_ALLOC_FAILED;
7215 next_param = &(func_handle->params);
7218 type = va_arg(ap, enum tep_func_arg_type);
7219 if (type == TEP_FUNC_ARG_VOID)
7222 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
7223 do_warning("Invalid argument type %d", type);
7224 ret = TEP_ERRNO__INVALID_ARG_TYPE;
7228 param = malloc(sizeof(*param));
7230 do_warning("Failed to allocate function param");
7231 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
7237 *next_param = param;
7238 next_param = &(param->next);
7240 func_handle->nr_args++;
7244 func_handle->next = tep->func_handlers;
7245 tep->func_handlers = func_handle;
7250 free_func_handle(func_handle);
7255 * tep_unregister_print_function - unregister a helper function
7256 * @tep: a handle to the trace event parser context
7257 * @func: the function to process the helper function
7258 * @name: the name of the helper function
7260 * This function removes existing print handler for function @name.
7262 * Returns 0 if the handler was removed successully, -1 otherwise.
7264 int tep_unregister_print_function(struct tep_handle *tep,
7265 tep_func_handler func, char *name)
7267 struct tep_function_handler *func_handle;
7269 func_handle = find_func_handler(tep, name);
7270 if (func_handle && func_handle->func == func) {
7271 remove_func_handler(tep, name);
7277 static struct tep_event *search_event(struct tep_handle *tep, int id,
7278 const char *sys_name,
7279 const char *event_name)
7281 struct tep_event *event;
7285 event = tep_find_event(tep, id);
7288 if (event_name && (strcmp(event_name, event->name) != 0))
7290 if (sys_name && (strcmp(sys_name, event->system) != 0))
7293 event = tep_find_event_by_name(tep, sys_name, event_name);
7301 * tep_register_event_handler - register a way to parse an event
7302 * @tep: a handle to the trace event parser context
7303 * @id: the id of the event to register
7304 * @sys_name: the system name the event belongs to
7305 * @event_name: the name of the event
7306 * @func: the function to call to parse the event information
7307 * @context: the data to be passed to @func
7309 * This function allows a developer to override the parsing of
7310 * a given event. If for some reason the default print format
7311 * is not sufficient, this function will register a function
7312 * for an event to be used to parse the data instead.
7314 * If @id is >= 0, then it is used to find the event.
7315 * else @sys_name and @event_name are used.
7318 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
7319 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
7320 * negative TEP_ERRNO_... in case of an error
7323 int tep_register_event_handler(struct tep_handle *tep, int id,
7324 const char *sys_name, const char *event_name,
7325 tep_event_handler_func func, void *context)
7327 struct tep_event *event;
7328 struct event_handler *handle;
7330 event = search_event(tep, id, sys_name, event_name);
7334 pr_stat("overriding event (%d) %s:%s with new print handler",
7335 event->id, event->system, event->name);
7337 event->handler = func;
7338 event->context = context;
7339 return TEP_REGISTER_SUCCESS_OVERWRITE;
7342 /* Save for later use. */
7343 handle = calloc(1, sizeof(*handle));
7345 do_warning("Failed to allocate event handler");
7346 return TEP_ERRNO__MEM_ALLOC_FAILED;
7351 handle->event_name = strdup(event_name);
7353 handle->sys_name = strdup(sys_name);
7355 if ((event_name && !handle->event_name) ||
7356 (sys_name && !handle->sys_name)) {
7357 do_warning("Failed to allocate event/sys name");
7358 free((void *)handle->event_name);
7359 free((void *)handle->sys_name);
7361 return TEP_ERRNO__MEM_ALLOC_FAILED;
7364 handle->func = func;
7365 handle->next = tep->handlers;
7366 tep->handlers = handle;
7367 handle->context = context;
7369 return TEP_REGISTER_SUCCESS;
7372 static int handle_matches(struct event_handler *handler, int id,
7373 const char *sys_name, const char *event_name,
7374 tep_event_handler_func func, void *context)
7376 if (id >= 0 && id != handler->id)
7379 if (event_name && (strcmp(event_name, handler->event_name) != 0))
7382 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
7385 if (func != handler->func || context != handler->context)
7392 * tep_unregister_event_handler - unregister an existing event handler
7393 * @tep: a handle to the trace event parser context
7394 * @id: the id of the event to unregister
7395 * @sys_name: the system name the handler belongs to
7396 * @event_name: the name of the event handler
7397 * @func: the function to call to parse the event information
7398 * @context: the data to be passed to @func
7400 * This function removes existing event handler (parser).
7402 * If @id is >= 0, then it is used to find the event.
7403 * else @sys_name and @event_name are used.
7405 * Returns 0 if handler was removed successfully, -1 if event was not found.
7407 int tep_unregister_event_handler(struct tep_handle *tep, int id,
7408 const char *sys_name, const char *event_name,
7409 tep_event_handler_func func, void *context)
7411 struct tep_event *event;
7412 struct event_handler *handle;
7413 struct event_handler **next;
7415 event = search_event(tep, id, sys_name, event_name);
7419 if (event->handler == func && event->context == context) {
7420 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
7421 event->id, event->system, event->name);
7423 event->handler = NULL;
7424 event->context = NULL;
7429 for (next = &tep->handlers; *next; next = &(*next)->next) {
7431 if (handle_matches(handle, id, sys_name, event_name,
7439 *next = handle->next;
7440 free_handler(handle);
7446 * tep_alloc - create a tep handle
7448 struct tep_handle *tep_alloc(void)
7450 struct tep_handle *tep = calloc(1, sizeof(*tep));
7454 tep->host_bigendian = tep_is_bigendian();
7460 void tep_ref(struct tep_handle *tep)
7465 int tep_get_ref(struct tep_handle *tep)
7468 return tep->ref_count;
7472 __hidden void free_tep_format_field(struct tep_format_field *field)
7475 if (field->alias != field->name)
7481 static void free_format_fields(struct tep_format_field *field)
7483 struct tep_format_field *next;
7487 free_tep_format_field(field);
7492 static void free_formats(struct tep_format *format)
7494 free_format_fields(format->common_fields);
7495 free_format_fields(format->fields);
7498 __hidden void free_tep_event(struct tep_event *event)
7501 free(event->system);
7503 free_formats(&event->format);
7505 free(event->print_fmt.format);
7506 free_args(event->print_fmt.args);
7507 free_parse_args(event->print_fmt.print_cache);
7512 * tep_free - free a tep handle
7513 * @tep: the tep handle to free
7515 void tep_free(struct tep_handle *tep)
7517 struct cmdline_list *cmdlist, *cmdnext;
7518 struct func_list *funclist, *funcnext;
7519 struct printk_list *printklist, *printknext;
7520 struct tep_function_handler *func_handler;
7521 struct event_handler *handle;
7527 cmdlist = tep->cmdlist;
7528 funclist = tep->funclist;
7529 printklist = tep->printklist;
7535 if (tep->cmdlines) {
7536 for (i = 0; i < tep->cmdline_count; i++)
7537 free(tep->cmdlines[i].comm);
7538 free(tep->cmdlines);
7542 cmdnext = cmdlist->next;
7543 free(cmdlist->comm);
7548 if (tep->func_map) {
7549 for (i = 0; i < (int)tep->func_count; i++) {
7550 free(tep->func_map[i].func);
7551 free(tep->func_map[i].mod);
7553 free(tep->func_map);
7557 funcnext = funclist->next;
7558 free(funclist->func);
7559 free(funclist->mod);
7561 funclist = funcnext;
7564 while (tep->func_handlers) {
7565 func_handler = tep->func_handlers;
7566 tep->func_handlers = func_handler->next;
7567 free_func_handle(func_handler);
7570 if (tep->printk_map) {
7571 for (i = 0; i < (int)tep->printk_count; i++)
7572 free(tep->printk_map[i].printk);
7573 free(tep->printk_map);
7576 while (printklist) {
7577 printknext = printklist->next;
7578 free(printklist->printk);
7580 printklist = printknext;
7583 for (i = 0; i < tep->nr_events; i++)
7584 free_tep_event(tep->events[i]);
7586 while (tep->handlers) {
7587 handle = tep->handlers;
7588 tep->handlers = handle->next;
7589 free_handler(handle);
7593 free(tep->sort_events);
7594 free(tep->func_resolver);
7595 free_tep_plugin_paths(tep);
7600 void tep_unref(struct tep_handle *tep)