1 // SPDX-License-Identifier: LGPL-2.1
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
21 #include <linux/string.h>
22 #include <linux/time64.h>
24 #include <netinet/in.h>
25 #include "event-parse.h"
26 #include "event-utils.h"
28 static const char *input_buf;
29 static unsigned long long input_buf_ptr;
30 static unsigned long long input_buf_siz;
32 static int is_flag_field;
33 static int is_symbolic_field;
35 static int show_warning = 1;
37 #define do_warning(fmt, ...) \
40 warning(fmt, ##__VA_ARGS__); \
43 #define do_warning_event(event, fmt, ...) \
49 warning("[%s:%s] " fmt, event->system, \
50 event->name, ##__VA_ARGS__); \
52 warning(fmt, ##__VA_ARGS__); \
55 static void init_input_buf(const char *buf, unsigned long long size)
62 const char *tep_get_input_buf(void)
67 unsigned long long tep_get_input_buf_ptr(void)
72 struct event_handler {
73 struct event_handler *next;
76 const char *event_name;
77 tep_event_handler_func func;
82 struct func_params *next;
83 enum tep_func_arg_type type;
86 struct tep_function_handler {
87 struct tep_function_handler *next;
88 enum tep_func_arg_type ret_type;
90 tep_func_handler func;
91 struct func_params *params;
95 static unsigned long long
96 process_defined_func(struct trace_seq *s, void *data, int size,
97 struct event_format *event, struct print_arg *arg);
99 static void free_func_handle(struct tep_function_handler *func);
102 * tep_buffer_init - init buffer for parsing
103 * @buf: buffer to parse
104 * @size: the size of the buffer
106 * For use with tep_read_token(), this initializes the internal
107 * buffer that tep_read_token() will parse.
109 void tep_buffer_init(const char *buf, unsigned long long size)
111 init_input_buf(buf, size);
114 void breakpoint(void)
120 struct print_arg *alloc_arg(void)
122 return calloc(1, sizeof(struct print_arg));
130 static int cmdline_cmp(const void *a, const void *b)
132 const struct cmdline *ca = a;
133 const struct cmdline *cb = b;
135 if (ca->pid < cb->pid)
137 if (ca->pid > cb->pid)
143 struct cmdline_list {
144 struct cmdline_list *next;
149 static int cmdline_init(struct tep_handle *pevent)
151 struct cmdline_list *cmdlist = pevent->cmdlist;
152 struct cmdline_list *item;
153 struct cmdline *cmdlines;
156 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
162 cmdlines[i].pid = cmdlist->pid;
163 cmdlines[i].comm = cmdlist->comm;
166 cmdlist = cmdlist->next;
170 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
172 pevent->cmdlines = cmdlines;
173 pevent->cmdlist = NULL;
178 static const char *find_cmdline(struct tep_handle *pevent, int pid)
180 const struct cmdline *comm;
186 if (!pevent->cmdlines && cmdline_init(pevent))
187 return "<not enough memory for cmdlines!>";
191 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
192 sizeof(*pevent->cmdlines), cmdline_cmp);
200 * tep_pid_is_registered - return if a pid has a cmdline registered
201 * @pevent: handle for the pevent
202 * @pid: The pid to check if it has a cmdline registered with.
204 * Returns 1 if the pid has a cmdline mapped to it
207 int tep_pid_is_registered(struct tep_handle *pevent, int pid)
209 const struct cmdline *comm;
215 if (!pevent->cmdlines && cmdline_init(pevent))
220 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
221 sizeof(*pevent->cmdlines), cmdline_cmp);
229 * If the command lines have been converted to an array, then
230 * we must add this pid. This is much slower than when cmdlines
231 * are added before the array is initialized.
233 static int add_new_comm(struct tep_handle *pevent, const char *comm, int pid)
235 struct cmdline *cmdlines = pevent->cmdlines;
236 const struct cmdline *cmdline;
242 /* avoid duplicates */
245 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
246 sizeof(*pevent->cmdlines), cmdline_cmp);
252 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
258 cmdlines[pevent->cmdline_count].comm = strdup(comm);
259 if (!cmdlines[pevent->cmdline_count].comm) {
265 cmdlines[pevent->cmdline_count].pid = pid;
267 if (cmdlines[pevent->cmdline_count].comm)
268 pevent->cmdline_count++;
270 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
271 pevent->cmdlines = cmdlines;
277 * tep_register_comm - register a pid / comm mapping
278 * @pevent: handle for the pevent
279 * @comm: the command line to register
280 * @pid: the pid to map the command line to
282 * This adds a mapping to search for command line names with
283 * a given pid. The comm is duplicated.
285 int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
287 struct cmdline_list *item;
289 if (pevent->cmdlines)
290 return add_new_comm(pevent, comm, pid);
292 item = malloc(sizeof(*item));
297 item->comm = strdup(comm);
299 item->comm = strdup("<...>");
305 item->next = pevent->cmdlist;
307 pevent->cmdlist = item;
308 pevent->cmdline_count++;
313 int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
315 pevent->trace_clock = strdup(trace_clock);
316 if (!pevent->trace_clock) {
324 unsigned long long addr;
330 struct func_list *next;
331 unsigned long long addr;
336 static int func_cmp(const void *a, const void *b)
338 const struct func_map *fa = a;
339 const struct func_map *fb = b;
341 if (fa->addr < fb->addr)
343 if (fa->addr > fb->addr)
350 * We are searching for a record in between, not an exact
353 static int func_bcmp(const void *a, const void *b)
355 const struct func_map *fa = a;
356 const struct func_map *fb = b;
358 if ((fa->addr == fb->addr) ||
360 (fa->addr > fb->addr &&
361 fa->addr < (fb+1)->addr))
364 if (fa->addr < fb->addr)
370 static int func_map_init(struct tep_handle *pevent)
372 struct func_list *funclist;
373 struct func_list *item;
374 struct func_map *func_map;
377 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
381 funclist = pevent->funclist;
385 func_map[i].func = funclist->func;
386 func_map[i].addr = funclist->addr;
387 func_map[i].mod = funclist->mod;
390 funclist = funclist->next;
394 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
397 * Add a special record at the end.
399 func_map[pevent->func_count].func = NULL;
400 func_map[pevent->func_count].addr = 0;
401 func_map[pevent->func_count].mod = NULL;
403 pevent->func_map = func_map;
404 pevent->funclist = NULL;
409 static struct func_map *
410 __find_func(struct tep_handle *pevent, unsigned long long addr)
412 struct func_map *func;
415 if (!pevent->func_map)
416 func_map_init(pevent);
420 func = bsearch(&key, pevent->func_map, pevent->func_count,
421 sizeof(*pevent->func_map), func_bcmp);
426 struct func_resolver {
427 tep_func_resolver_t *func;
433 * tep_set_function_resolver - set an alternative function resolver
434 * @pevent: handle for the pevent
435 * @resolver: function to be used
436 * @priv: resolver function private state.
438 * Some tools may have already a way to resolve kernel functions, allow them to
439 * keep using it instead of duplicating all the entries inside
442 int tep_set_function_resolver(struct tep_handle *pevent,
443 tep_func_resolver_t *func, void *priv)
445 struct func_resolver *resolver = malloc(sizeof(*resolver));
447 if (resolver == NULL)
450 resolver->func = func;
451 resolver->priv = priv;
453 free(pevent->func_resolver);
454 pevent->func_resolver = resolver;
460 * tep_reset_function_resolver - reset alternative function resolver
461 * @pevent: handle for the pevent
463 * Stop using whatever alternative resolver was set, use the default
466 void tep_reset_function_resolver(struct tep_handle *pevent)
468 free(pevent->func_resolver);
469 pevent->func_resolver = NULL;
472 static struct func_map *
473 find_func(struct tep_handle *pevent, unsigned long long addr)
475 struct func_map *map;
477 if (!pevent->func_resolver)
478 return __find_func(pevent, addr);
480 map = &pevent->func_resolver->map;
483 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
484 &map->addr, &map->mod);
485 if (map->func == NULL)
492 * tep_find_function - find a function by a given address
493 * @pevent: handle for the pevent
494 * @addr: the address to find the function with
496 * Returns a pointer to the function stored that has the given
497 * address. Note, the address does not have to be exact, it
498 * will select the function that would contain the address.
500 const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
502 struct func_map *map;
504 map = find_func(pevent, addr);
512 * tep_find_function_address - find a function address by a given address
513 * @pevent: handle for the pevent
514 * @addr: the address to find the function with
516 * Returns the address the function starts at. This can be used in
517 * conjunction with tep_find_function to print both the function
518 * name and the function offset.
521 tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
523 struct func_map *map;
525 map = find_func(pevent, addr);
533 * tep_register_function - register a function with a given address
534 * @pevent: handle for the pevent
535 * @function: the function name to register
536 * @addr: the address the function starts at
537 * @mod: the kernel module the function may be in (NULL for none)
539 * This registers a function name with an address and module.
540 * The @func passed in is duplicated.
542 int tep_register_function(struct tep_handle *pevent, char *func,
543 unsigned long long addr, char *mod)
545 struct func_list *item = malloc(sizeof(*item));
550 item->next = pevent->funclist;
551 item->func = strdup(func);
556 item->mod = strdup(mod);
563 pevent->funclist = item;
564 pevent->func_count++;
578 * tep_print_funcs - print out the stored functions
579 * @pevent: handle for the pevent
581 * This prints out the stored functions.
583 void tep_print_funcs(struct tep_handle *pevent)
587 if (!pevent->func_map)
588 func_map_init(pevent);
590 for (i = 0; i < (int)pevent->func_count; i++) {
592 pevent->func_map[i].addr,
593 pevent->func_map[i].func);
594 if (pevent->func_map[i].mod)
595 printf(" [%s]\n", pevent->func_map[i].mod);
602 unsigned long long addr;
607 struct printk_list *next;
608 unsigned long long addr;
612 static int printk_cmp(const void *a, const void *b)
614 const struct printk_map *pa = a;
615 const struct printk_map *pb = b;
617 if (pa->addr < pb->addr)
619 if (pa->addr > pb->addr)
625 static int printk_map_init(struct tep_handle *pevent)
627 struct printk_list *printklist;
628 struct printk_list *item;
629 struct printk_map *printk_map;
632 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
636 printklist = pevent->printklist;
640 printk_map[i].printk = printklist->printk;
641 printk_map[i].addr = printklist->addr;
644 printklist = printklist->next;
648 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
650 pevent->printk_map = printk_map;
651 pevent->printklist = NULL;
656 static struct printk_map *
657 find_printk(struct tep_handle *pevent, unsigned long long addr)
659 struct printk_map *printk;
660 struct printk_map key;
662 if (!pevent->printk_map && printk_map_init(pevent))
667 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
668 sizeof(*pevent->printk_map), printk_cmp);
674 * tep_register_print_string - register a string by its address
675 * @pevent: handle for the pevent
676 * @fmt: the string format to register
677 * @addr: the address the string was located at
679 * This registers a string by the address it was stored in the kernel.
680 * The @fmt passed in is duplicated.
682 int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
683 unsigned long long addr)
685 struct printk_list *item = malloc(sizeof(*item));
691 item->next = pevent->printklist;
694 /* Strip off quotes and '\n' from the end */
697 item->printk = strdup(fmt);
701 p = item->printk + strlen(item->printk) - 1;
706 if (strcmp(p, "\\n") == 0)
709 pevent->printklist = item;
710 pevent->printk_count++;
721 * tep_print_printk - print out the stored strings
722 * @pevent: handle for the pevent
724 * This prints the string formats that were stored.
726 void tep_print_printk(struct tep_handle *pevent)
730 if (!pevent->printk_map)
731 printk_map_init(pevent);
733 for (i = 0; i < (int)pevent->printk_count; i++) {
734 printf("%016llx %s\n",
735 pevent->printk_map[i].addr,
736 pevent->printk_map[i].printk);
740 static struct event_format *alloc_event(void)
742 return calloc(1, sizeof(struct event_format));
745 static int add_event(struct tep_handle *pevent, struct event_format *event)
748 struct event_format **events = realloc(pevent->events, sizeof(event) *
749 (pevent->nr_events + 1));
753 pevent->events = events;
755 for (i = 0; i < pevent->nr_events; i++) {
756 if (pevent->events[i]->id > event->id)
759 if (i < pevent->nr_events)
760 memmove(&pevent->events[i + 1],
762 sizeof(event) * (pevent->nr_events - i));
764 pevent->events[i] = event;
767 event->pevent = pevent;
772 static int event_item_type(enum event_type type)
775 case EVENT_ITEM ... EVENT_SQUOTE:
777 case EVENT_ERROR ... EVENT_DELIM:
783 static void free_flag_sym(struct print_flag_sym *fsym)
785 struct print_flag_sym *next;
796 static void free_arg(struct print_arg *arg)
798 struct print_arg *farg;
805 free(arg->atom.atom);
808 free(arg->field.name);
811 free_arg(arg->flags.field);
812 free(arg->flags.delim);
813 free_flag_sym(arg->flags.flags);
816 free_arg(arg->symbol.field);
817 free_flag_sym(arg->symbol.symbols);
821 free_arg(arg->hex.field);
822 free_arg(arg->hex.size);
824 case PRINT_INT_ARRAY:
825 free_arg(arg->int_array.field);
826 free_arg(arg->int_array.count);
827 free_arg(arg->int_array.el_size);
830 free(arg->typecast.type);
831 free_arg(arg->typecast.item);
835 free(arg->string.string);
838 free(arg->bitmask.bitmask);
840 case PRINT_DYNAMIC_ARRAY:
841 case PRINT_DYNAMIC_ARRAY_LEN:
842 free(arg->dynarray.index);
846 free_arg(arg->op.left);
847 free_arg(arg->op.right);
850 while (arg->func.args) {
851 farg = arg->func.args;
852 arg->func.args = farg->next;
865 static enum event_type get_type(int ch)
868 return EVENT_NEWLINE;
871 if (isalnum(ch) || ch == '_')
879 if (ch == '(' || ch == ')' || ch == ',')
885 static int __read_char(void)
887 if (input_buf_ptr >= input_buf_siz)
890 return input_buf[input_buf_ptr++];
893 static int __peek_char(void)
895 if (input_buf_ptr >= input_buf_siz)
898 return input_buf[input_buf_ptr];
902 * tep_peek_char - peek at the next character that will be read
904 * Returns the next character read, or -1 if end of buffer.
906 int tep_peek_char(void)
908 return __peek_char();
911 static int extend_token(char **tok, char *buf, int size)
913 char *newtok = realloc(*tok, size);
930 static enum event_type force_token(const char *str, char **tok);
932 static enum event_type __read_token(char **tok)
935 int ch, last_ch, quote_ch, next_ch;
938 enum event_type type;
948 if (type == EVENT_NONE)
956 if (asprintf(tok, "%c", ch) < 0)
964 next_ch = __peek_char();
965 if (next_ch == '>') {
966 buf[i++] = __read_char();
979 buf[i++] = __read_char();
991 default: /* what should we do instead? */
1001 buf[i++] = __read_char();
1006 /* don't keep quotes */
1012 if (i == (BUFSIZ - 1)) {
1016 if (extend_token(tok, buf, tok_size) < 0)
1023 /* the '\' '\' will cancel itself */
1024 if (ch == '\\' && last_ch == '\\')
1026 } while (ch != quote_ch || last_ch == '\\');
1027 /* remove the last quote */
1031 * For strings (double quotes) check the next token.
1032 * If it is another string, concatinate the two.
1034 if (type == EVENT_DQUOTE) {
1035 unsigned long long save_input_buf_ptr = input_buf_ptr;
1039 } while (isspace(ch));
1042 input_buf_ptr = save_input_buf_ptr;
1047 case EVENT_ERROR ... EVENT_SPACE:
1053 while (get_type(__peek_char()) == type) {
1054 if (i == (BUFSIZ - 1)) {
1058 if (extend_token(tok, buf, tok_size) < 0)
1068 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1071 if (type == EVENT_ITEM) {
1073 * Older versions of the kernel has a bug that
1074 * creates invalid symbols and will break the mac80211
1075 * parsing. This is a work around to that bug.
1077 * See Linux kernel commit:
1078 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1080 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1083 return force_token("\"%s\" ", tok);
1084 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1087 return force_token("\" sta:%pM\" ", tok);
1088 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1091 return force_token("\" vif:%p(%d)\" ", tok);
1098 static enum event_type force_token(const char *str, char **tok)
1100 const char *save_input_buf;
1101 unsigned long long save_input_buf_ptr;
1102 unsigned long long save_input_buf_siz;
1103 enum event_type type;
1105 /* save off the current input pointers */
1106 save_input_buf = input_buf;
1107 save_input_buf_ptr = input_buf_ptr;
1108 save_input_buf_siz = input_buf_siz;
1110 init_input_buf(str, strlen(str));
1112 type = __read_token(tok);
1114 /* reset back to original token */
1115 input_buf = save_input_buf;
1116 input_buf_ptr = save_input_buf_ptr;
1117 input_buf_siz = save_input_buf_siz;
1122 static void free_token(char *tok)
1128 static enum event_type read_token(char **tok)
1130 enum event_type type;
1133 type = __read_token(tok);
1134 if (type != EVENT_SPACE)
1146 * tep_read_token - access to utilites to use the pevent parser
1147 * @tok: The token to return
1149 * This will parse tokens from the string given by
1152 * Returns the token type.
1154 enum event_type tep_read_token(char **tok)
1156 return read_token(tok);
1160 * tep_free_token - free a token returned by tep_read_token
1161 * @token: the token to free
1163 void tep_free_token(char *token)
1169 static enum event_type read_token_item(char **tok)
1171 enum event_type type;
1174 type = __read_token(tok);
1175 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1186 static int test_type(enum event_type type, enum event_type expect)
1188 if (type != expect) {
1189 do_warning("Error: expected type %d but read %d",
1196 static int test_type_token(enum event_type type, const char *token,
1197 enum event_type expect, const char *expect_tok)
1199 if (type != expect) {
1200 do_warning("Error: expected type %d but read %d",
1205 if (strcmp(token, expect_tok) != 0) {
1206 do_warning("Error: expected '%s' but read '%s'",
1213 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1215 enum event_type type;
1218 type = read_token(tok);
1220 type = read_token_item(tok);
1221 return test_type(type, expect);
1224 static int read_expect_type(enum event_type expect, char **tok)
1226 return __read_expect_type(expect, tok, 1);
1229 static int __read_expected(enum event_type expect, const char *str,
1232 enum event_type type;
1237 type = read_token(&token);
1239 type = read_token_item(&token);
1241 ret = test_type_token(type, token, expect, str);
1248 static int read_expected(enum event_type expect, const char *str)
1250 return __read_expected(expect, str, 1);
1253 static int read_expected_item(enum event_type expect, const char *str)
1255 return __read_expected(expect, str, 0);
1258 static char *event_read_name(void)
1262 if (read_expected(EVENT_ITEM, "name") < 0)
1265 if (read_expected(EVENT_OP, ":") < 0)
1268 if (read_expect_type(EVENT_ITEM, &token) < 0)
1278 static int event_read_id(void)
1283 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1286 if (read_expected(EVENT_OP, ":") < 0)
1289 if (read_expect_type(EVENT_ITEM, &token) < 0)
1292 id = strtoul(token, NULL, 0);
1301 static int field_is_string(struct format_field *field)
1303 if ((field->flags & FIELD_IS_ARRAY) &&
1304 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1305 strstr(field->type, "s8")))
1311 static int field_is_dynamic(struct format_field *field)
1313 if (strncmp(field->type, "__data_loc", 10) == 0)
1319 static int field_is_long(struct format_field *field)
1321 /* includes long long */
1322 if (strstr(field->type, "long"))
1328 static unsigned int type_size(const char *name)
1330 /* This covers all FIELD_IS_STRING types. */
1348 for (i = 0; table[i].type; i++) {
1349 if (!strcmp(table[i].type, name))
1350 return table[i].size;
1356 static int event_read_fields(struct event_format *event, struct format_field **fields)
1358 struct format_field *field = NULL;
1359 enum event_type type;
1365 unsigned int size_dynamic = 0;
1367 type = read_token(&token);
1368 if (type == EVENT_NEWLINE) {
1375 if (test_type_token(type, token, EVENT_ITEM, "field"))
1379 type = read_token(&token);
1381 * The ftrace fields may still use the "special" name.
1384 if (event->flags & EVENT_FL_ISFTRACE &&
1385 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1387 type = read_token(&token);
1390 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1394 if (read_expect_type(EVENT_ITEM, &token) < 0)
1399 field = calloc(1, sizeof(*field));
1403 field->event = event;
1405 /* read the rest of the type */
1407 type = read_token(&token);
1408 if (type == EVENT_ITEM ||
1409 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1411 * Some of the ftrace fields are broken and have
1412 * an illegal "." in them.
1414 (event->flags & EVENT_FL_ISFTRACE &&
1415 type == EVENT_OP && strcmp(token, ".") == 0)) {
1417 if (strcmp(token, "*") == 0)
1418 field->flags |= FIELD_IS_POINTER;
1422 new_type = realloc(field->type,
1423 strlen(field->type) +
1424 strlen(last_token) + 2);
1429 field->type = new_type;
1430 strcat(field->type, " ");
1431 strcat(field->type, last_token);
1434 field->type = last_token;
1443 do_warning_event(event, "%s: no type found", __func__);
1446 field->name = field->alias = last_token;
1448 if (test_type(type, EVENT_OP))
1451 if (strcmp(token, "[") == 0) {
1452 enum event_type last_type = type;
1453 char *brackets = token;
1457 field->flags |= FIELD_IS_ARRAY;
1459 type = read_token(&token);
1461 if (type == EVENT_ITEM)
1462 field->arraylen = strtoul(token, NULL, 0);
1464 field->arraylen = 0;
1466 while (strcmp(token, "]") != 0) {
1467 if (last_type == EVENT_ITEM &&
1474 new_brackets = realloc(brackets,
1476 strlen(token) + len);
1477 if (!new_brackets) {
1481 brackets = new_brackets;
1483 strcat(brackets, " ");
1484 strcat(brackets, token);
1485 /* We only care about the last token */
1486 field->arraylen = strtoul(token, NULL, 0);
1488 type = read_token(&token);
1489 if (type == EVENT_NONE) {
1490 do_warning_event(event, "failed to find token");
1497 new_brackets = realloc(brackets, strlen(brackets) + 2);
1498 if (!new_brackets) {
1502 brackets = new_brackets;
1503 strcat(brackets, "]");
1505 /* add brackets to type */
1507 type = read_token(&token);
1509 * If the next token is not an OP, then it is of
1510 * the format: type [] item;
1512 if (type == EVENT_ITEM) {
1514 new_type = realloc(field->type,
1515 strlen(field->type) +
1516 strlen(field->name) +
1517 strlen(brackets) + 2);
1522 field->type = new_type;
1523 strcat(field->type, " ");
1524 strcat(field->type, field->name);
1525 size_dynamic = type_size(field->name);
1526 free_token(field->name);
1527 strcat(field->type, brackets);
1528 field->name = field->alias = token;
1529 type = read_token(&token);
1532 new_type = realloc(field->type,
1533 strlen(field->type) +
1534 strlen(brackets) + 1);
1539 field->type = new_type;
1540 strcat(field->type, brackets);
1545 if (field_is_string(field))
1546 field->flags |= FIELD_IS_STRING;
1547 if (field_is_dynamic(field))
1548 field->flags |= FIELD_IS_DYNAMIC;
1549 if (field_is_long(field))
1550 field->flags |= FIELD_IS_LONG;
1552 if (test_type_token(type, token, EVENT_OP, ";"))
1556 if (read_expected(EVENT_ITEM, "offset") < 0)
1559 if (read_expected(EVENT_OP, ":") < 0)
1562 if (read_expect_type(EVENT_ITEM, &token))
1564 field->offset = strtoul(token, NULL, 0);
1567 if (read_expected(EVENT_OP, ";") < 0)
1570 if (read_expected(EVENT_ITEM, "size") < 0)
1573 if (read_expected(EVENT_OP, ":") < 0)
1576 if (read_expect_type(EVENT_ITEM, &token))
1578 field->size = strtoul(token, NULL, 0);
1581 if (read_expected(EVENT_OP, ";") < 0)
1584 type = read_token(&token);
1585 if (type != EVENT_NEWLINE) {
1586 /* newer versions of the kernel have a "signed" type */
1587 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1592 if (read_expected(EVENT_OP, ":") < 0)
1595 if (read_expect_type(EVENT_ITEM, &token))
1598 if (strtoul(token, NULL, 0))
1599 field->flags |= FIELD_IS_SIGNED;
1602 if (read_expected(EVENT_OP, ";") < 0)
1605 if (read_expect_type(EVENT_NEWLINE, &token))
1611 if (field->flags & FIELD_IS_ARRAY) {
1612 if (field->arraylen)
1613 field->elementsize = field->size / field->arraylen;
1614 else if (field->flags & FIELD_IS_DYNAMIC)
1615 field->elementsize = size_dynamic;
1616 else if (field->flags & FIELD_IS_STRING)
1617 field->elementsize = 1;
1618 else if (field->flags & FIELD_IS_LONG)
1619 field->elementsize = event->pevent ?
1620 event->pevent->long_size :
1623 field->elementsize = field->size;
1626 fields = &field->next;
1643 static int event_read_format(struct event_format *event)
1648 if (read_expected_item(EVENT_ITEM, "format") < 0)
1651 if (read_expected(EVENT_OP, ":") < 0)
1654 if (read_expect_type(EVENT_NEWLINE, &token))
1658 ret = event_read_fields(event, &event->format.common_fields);
1661 event->format.nr_common = ret;
1663 ret = event_read_fields(event, &event->format.fields);
1666 event->format.nr_fields = ret;
1675 static enum event_type
1676 process_arg_token(struct event_format *event, struct print_arg *arg,
1677 char **tok, enum event_type type);
1679 static enum event_type
1680 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1682 enum event_type type;
1685 type = read_token(&token);
1688 return process_arg_token(event, arg, tok, type);
1691 static enum event_type
1692 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1695 * For __print_symbolic() and __print_flags, we need to completely
1696 * evaluate the first argument, which defines what to print next.
1698 static enum event_type
1699 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1701 enum event_type type;
1703 type = process_arg(event, arg, tok);
1705 while (type == EVENT_OP) {
1706 type = process_op(event, arg, tok);
1712 static enum event_type
1713 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1715 struct print_arg *arg, *left, *right;
1716 enum event_type type;
1721 right = alloc_arg();
1723 if (!arg || !left || !right) {
1724 do_warning_event(event, "%s: not enough memory!", __func__);
1725 /* arg will be freed at out_free */
1731 arg->type = PRINT_OP;
1732 arg->op.left = left;
1733 arg->op.right = right;
1736 type = process_arg(event, left, &token);
1739 if (type == EVENT_ERROR)
1742 /* Handle other operations in the arguments */
1743 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1744 type = process_op(event, left, &token);
1748 if (test_type_token(type, token, EVENT_OP, ":"))
1753 type = process_arg(event, right, &token);
1755 top->op.right = arg;
1761 /* Top may point to itself */
1762 top->op.right = NULL;
1768 static enum event_type
1769 process_array(struct event_format *event, struct print_arg *top, char **tok)
1771 struct print_arg *arg;
1772 enum event_type type;
1777 do_warning_event(event, "%s: not enough memory!", __func__);
1778 /* '*tok' is set to top->op.op. No need to free. */
1784 type = process_arg(event, arg, &token);
1785 if (test_type_token(type, token, EVENT_OP, "]"))
1788 top->op.right = arg;
1791 type = read_token_item(&token);
1802 static int get_op_prio(char *op)
1816 /* '>>' and '<<' are 8 */
1820 /* '==' and '!=' are 10 */
1830 do_warning("unknown op '%c'", op[0]);
1834 if (strcmp(op, "++") == 0 ||
1835 strcmp(op, "--") == 0) {
1837 } else if (strcmp(op, ">>") == 0 ||
1838 strcmp(op, "<<") == 0) {
1840 } else if (strcmp(op, ">=") == 0 ||
1841 strcmp(op, "<=") == 0) {
1843 } else if (strcmp(op, "==") == 0 ||
1844 strcmp(op, "!=") == 0) {
1846 } else if (strcmp(op, "&&") == 0) {
1848 } else if (strcmp(op, "||") == 0) {
1851 do_warning("unknown op '%s'", op);
1857 static int set_op_prio(struct print_arg *arg)
1860 /* single ops are the greatest */
1861 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1864 arg->op.prio = get_op_prio(arg->op.op);
1866 return arg->op.prio;
1869 /* Note, *tok does not get freed, but will most likely be saved */
1870 static enum event_type
1871 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1873 struct print_arg *left, *right = NULL;
1874 enum event_type type;
1877 /* the op is passed in via tok */
1880 if (arg->type == PRINT_OP && !arg->op.left) {
1881 /* handle single op */
1883 do_warning_event(event, "bad op token %s", token);
1893 do_warning_event(event, "bad op token %s", token);
1898 /* make an empty left */
1903 left->type = PRINT_NULL;
1904 arg->op.left = left;
1906 right = alloc_arg();
1910 arg->op.right = right;
1912 /* do not free the token, it belongs to an op */
1914 type = process_arg(event, right, tok);
1916 } else if (strcmp(token, "?") == 0) {
1922 /* copy the top arg to the left */
1925 arg->type = PRINT_OP;
1927 arg->op.left = left;
1930 /* it will set arg->op.right */
1931 type = process_cond(event, arg, tok);
1933 } else if (strcmp(token, ">>") == 0 ||
1934 strcmp(token, "<<") == 0 ||
1935 strcmp(token, "&") == 0 ||
1936 strcmp(token, "|") == 0 ||
1937 strcmp(token, "&&") == 0 ||
1938 strcmp(token, "||") == 0 ||
1939 strcmp(token, "-") == 0 ||
1940 strcmp(token, "+") == 0 ||
1941 strcmp(token, "*") == 0 ||
1942 strcmp(token, "^") == 0 ||
1943 strcmp(token, "/") == 0 ||
1944 strcmp(token, "%") == 0 ||
1945 strcmp(token, "<") == 0 ||
1946 strcmp(token, ">") == 0 ||
1947 strcmp(token, "<=") == 0 ||
1948 strcmp(token, ">=") == 0 ||
1949 strcmp(token, "==") == 0 ||
1950 strcmp(token, "!=") == 0) {
1956 /* copy the top arg to the left */
1959 arg->type = PRINT_OP;
1961 arg->op.left = left;
1962 arg->op.right = NULL;
1964 if (set_op_prio(arg) == -1) {
1965 event->flags |= EVENT_FL_FAILED;
1966 /* arg->op.op (= token) will be freed at out_free */
1971 type = read_token_item(&token);
1974 /* could just be a type pointer */
1975 if ((strcmp(arg->op.op, "*") == 0) &&
1976 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1979 if (left->type != PRINT_ATOM) {
1980 do_warning_event(event, "bad pointer type");
1983 new_atom = realloc(left->atom.atom,
1984 strlen(left->atom.atom) + 3);
1988 left->atom.atom = new_atom;
1989 strcat(left->atom.atom, " *");
1997 right = alloc_arg();
2001 type = process_arg_token(event, right, tok, type);
2002 if (type == EVENT_ERROR) {
2004 /* token was freed in process_arg_token() via *tok */
2009 if (right->type == PRINT_OP &&
2010 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2011 struct print_arg tmp;
2013 /* rotate ops according to the priority */
2014 arg->op.right = right->op.left;
2020 arg->op.left = right;
2022 arg->op.right = right;
2025 } else if (strcmp(token, "[") == 0) {
2033 arg->type = PRINT_OP;
2035 arg->op.left = left;
2039 /* it will set arg->op.right */
2040 type = process_array(event, arg, tok);
2043 do_warning_event(event, "unknown op '%s'", token);
2044 event->flags |= EVENT_FL_FAILED;
2045 /* the arg is now the left side */
2049 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2052 /* higher prios need to be closer to the root */
2053 prio = get_op_prio(*tok);
2055 if (prio > arg->op.prio)
2056 return process_op(event, arg, tok);
2058 return process_op(event, right, tok);
2064 do_warning_event(event, "%s: not enough memory!", __func__);
2071 static enum event_type
2072 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2075 enum event_type type;
2079 if (read_expected(EVENT_OP, "->") < 0)
2082 if (read_expect_type(EVENT_ITEM, &token) < 0)
2086 arg->type = PRINT_FIELD;
2087 arg->field.name = field;
2089 if (is_flag_field) {
2090 arg->field.field = tep_find_any_field(event, arg->field.name);
2091 arg->field.field->flags |= FIELD_IS_FLAG;
2093 } else if (is_symbolic_field) {
2094 arg->field.field = tep_find_any_field(event, arg->field.name);
2095 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2096 is_symbolic_field = 0;
2099 type = read_token(&token);
2111 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2112 struct print_arg **print_arg)
2114 struct print_arg *field;
2115 enum event_type type;
2119 field = alloc_arg();
2121 do_warning_event(event, "%s: not enough memory!", __func__);
2126 type = process_arg(event, field, &token);
2128 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2132 goto out_free_token;
2143 static char *arg_eval (struct print_arg *arg);
2145 static unsigned long long
2146 eval_type_str(unsigned long long val, const char *type, int pointer)
2156 if (type[len-1] != '*') {
2157 do_warning("pointer expected with non pointer type");
2163 do_warning("%s: not enough memory!", __func__);
2166 memcpy(ref, type, len);
2168 /* chop off the " *" */
2171 val = eval_type_str(val, ref, 0);
2176 /* check if this is a pointer */
2177 if (type[len - 1] == '*')
2180 /* Try to figure out the arg size*/
2181 if (strncmp(type, "struct", 6) == 0)
2185 if (strcmp(type, "u8") == 0)
2188 if (strcmp(type, "u16") == 0)
2189 return val & 0xffff;
2191 if (strcmp(type, "u32") == 0)
2192 return val & 0xffffffff;
2194 if (strcmp(type, "u64") == 0 ||
2195 strcmp(type, "s64"))
2198 if (strcmp(type, "s8") == 0)
2199 return (unsigned long long)(char)val & 0xff;
2201 if (strcmp(type, "s16") == 0)
2202 return (unsigned long long)(short)val & 0xffff;
2204 if (strcmp(type, "s32") == 0)
2205 return (unsigned long long)(int)val & 0xffffffff;
2207 if (strncmp(type, "unsigned ", 9) == 0) {
2212 if (strcmp(type, "char") == 0) {
2214 return (unsigned long long)(char)val & 0xff;
2219 if (strcmp(type, "short") == 0) {
2221 return (unsigned long long)(short)val & 0xffff;
2223 return val & 0xffff;
2226 if (strcmp(type, "int") == 0) {
2228 return (unsigned long long)(int)val & 0xffffffff;
2230 return val & 0xffffffff;
2237 * Try to figure out the type.
2239 static unsigned long long
2240 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2242 if (arg->type != PRINT_TYPE) {
2243 do_warning("expected type argument");
2247 return eval_type_str(val, arg->typecast.type, pointer);
2250 static int arg_num_eval(struct print_arg *arg, long long *val)
2252 long long left, right;
2255 switch (arg->type) {
2257 *val = strtoll(arg->atom.atom, NULL, 0);
2260 ret = arg_num_eval(arg->typecast.item, val);
2263 *val = eval_type(*val, arg, 0);
2266 switch (arg->op.op[0]) {
2268 ret = arg_num_eval(arg->op.left, &left);
2271 ret = arg_num_eval(arg->op.right, &right);
2275 *val = left || right;
2277 *val = left | right;
2280 ret = arg_num_eval(arg->op.left, &left);
2283 ret = arg_num_eval(arg->op.right, &right);
2287 *val = left && right;
2289 *val = left & right;
2292 ret = arg_num_eval(arg->op.left, &left);
2295 ret = arg_num_eval(arg->op.right, &right);
2298 switch (arg->op.op[1]) {
2300 *val = left < right;
2303 *val = left << right;
2306 *val = left <= right;
2309 do_warning("unknown op '%s'", arg->op.op);
2314 ret = arg_num_eval(arg->op.left, &left);
2317 ret = arg_num_eval(arg->op.right, &right);
2320 switch (arg->op.op[1]) {
2322 *val = left > right;
2325 *val = left >> right;
2328 *val = left >= right;
2331 do_warning("unknown op '%s'", arg->op.op);
2336 ret = arg_num_eval(arg->op.left, &left);
2339 ret = arg_num_eval(arg->op.right, &right);
2343 if (arg->op.op[1] != '=') {
2344 do_warning("unknown op '%s'", arg->op.op);
2347 *val = left == right;
2350 ret = arg_num_eval(arg->op.left, &left);
2353 ret = arg_num_eval(arg->op.right, &right);
2357 switch (arg->op.op[1]) {
2359 *val = left != right;
2362 do_warning("unknown op '%s'", arg->op.op);
2367 /* check for negative */
2368 if (arg->op.left->type == PRINT_NULL)
2371 ret = arg_num_eval(arg->op.left, &left);
2374 ret = arg_num_eval(arg->op.right, &right);
2377 *val = left - right;
2380 if (arg->op.left->type == PRINT_NULL)
2383 ret = arg_num_eval(arg->op.left, &left);
2386 ret = arg_num_eval(arg->op.right, &right);
2389 *val = left + right;
2392 ret = arg_num_eval(arg->op.right, &right);
2398 do_warning("unknown op '%s'", arg->op.op);
2404 case PRINT_FIELD ... PRINT_SYMBOL:
2409 do_warning("invalid eval type %d", arg->type);
2416 static char *arg_eval (struct print_arg *arg)
2419 static char buf[20];
2421 switch (arg->type) {
2423 return arg->atom.atom;
2425 return arg_eval(arg->typecast.item);
2427 if (!arg_num_eval(arg, &val))
2429 sprintf(buf, "%lld", val);
2433 case PRINT_FIELD ... PRINT_SYMBOL:
2438 do_warning("invalid eval type %d", arg->type);
2445 static enum event_type
2446 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2448 enum event_type type;
2449 struct print_arg *arg = NULL;
2450 struct print_flag_sym *field;
2456 type = read_token_item(&token);
2457 if (test_type_token(type, token, EVENT_OP, "{"))
2465 type = process_arg(event, arg, &token);
2467 if (type == EVENT_OP)
2468 type = process_op(event, arg, &token);
2470 if (type == EVENT_ERROR)
2473 if (test_type_token(type, token, EVENT_DELIM, ","))
2476 field = calloc(1, sizeof(*field));
2480 value = arg_eval(arg);
2482 goto out_free_field;
2483 field->value = strdup(value);
2484 if (field->value == NULL)
2485 goto out_free_field;
2493 type = process_arg(event, arg, &token);
2494 if (test_type_token(type, token, EVENT_OP, "}"))
2495 goto out_free_field;
2497 value = arg_eval(arg);
2499 goto out_free_field;
2500 field->str = strdup(value);
2501 if (field->str == NULL)
2502 goto out_free_field;
2507 list = &field->next;
2510 type = read_token_item(&token);
2511 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2517 free_flag_sym(field);
2526 static enum event_type
2527 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2529 struct print_arg *field;
2530 enum event_type type;
2533 memset(arg, 0, sizeof(*arg));
2534 arg->type = PRINT_FLAGS;
2536 field = alloc_arg();
2538 do_warning_event(event, "%s: not enough memory!", __func__);
2542 type = process_field_arg(event, field, &token);
2544 /* Handle operations in the first argument */
2545 while (type == EVENT_OP)
2546 type = process_op(event, field, &token);
2548 if (test_type_token(type, token, EVENT_DELIM, ","))
2549 goto out_free_field;
2552 arg->flags.field = field;
2554 type = read_token_item(&token);
2555 if (event_item_type(type)) {
2556 arg->flags.delim = token;
2557 type = read_token_item(&token);
2560 if (test_type_token(type, token, EVENT_DELIM, ","))
2563 type = process_fields(event, &arg->flags.flags, &token);
2564 if (test_type_token(type, token, EVENT_DELIM, ")"))
2568 type = read_token_item(tok);
2579 static enum event_type
2580 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2582 struct print_arg *field;
2583 enum event_type type;
2586 memset(arg, 0, sizeof(*arg));
2587 arg->type = PRINT_SYMBOL;
2589 field = alloc_arg();
2591 do_warning_event(event, "%s: not enough memory!", __func__);
2595 type = process_field_arg(event, field, &token);
2597 if (test_type_token(type, token, EVENT_DELIM, ","))
2598 goto out_free_field;
2600 arg->symbol.field = field;
2602 type = process_fields(event, &arg->symbol.symbols, &token);
2603 if (test_type_token(type, token, EVENT_DELIM, ")"))
2607 type = read_token_item(tok);
2618 static enum event_type
2619 process_hex_common(struct event_format *event, struct print_arg *arg,
2620 char **tok, enum print_arg_type type)
2622 memset(arg, 0, sizeof(*arg));
2625 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2628 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2631 return read_token_item(tok);
2634 free_arg(arg->hex.field);
2635 arg->hex.field = NULL;
2641 static enum event_type
2642 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2644 return process_hex_common(event, arg, tok, PRINT_HEX);
2647 static enum event_type
2648 process_hex_str(struct event_format *event, struct print_arg *arg,
2651 return process_hex_common(event, arg, tok, PRINT_HEX_STR);
2654 static enum event_type
2655 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2657 memset(arg, 0, sizeof(*arg));
2658 arg->type = PRINT_INT_ARRAY;
2660 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2663 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2666 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2669 return read_token_item(tok);
2672 free_arg(arg->int_array.count);
2673 arg->int_array.count = NULL;
2675 free_arg(arg->int_array.field);
2676 arg->int_array.field = NULL;
2682 static enum event_type
2683 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2685 struct format_field *field;
2686 enum event_type type;
2689 memset(arg, 0, sizeof(*arg));
2690 arg->type = PRINT_DYNAMIC_ARRAY;
2693 * The item within the parenthesis is another field that holds
2694 * the index into where the array starts.
2696 type = read_token(&token);
2698 if (type != EVENT_ITEM)
2701 /* Find the field */
2703 field = tep_find_field(event, token);
2707 arg->dynarray.field = field;
2708 arg->dynarray.index = 0;
2710 if (read_expected(EVENT_DELIM, ")") < 0)
2714 type = read_token_item(&token);
2716 if (type != EVENT_OP || strcmp(token, "[") != 0)
2722 do_warning_event(event, "%s: not enough memory!", __func__);
2727 type = process_arg(event, arg, &token);
2728 if (type == EVENT_ERROR)
2731 if (!test_type_token(type, token, EVENT_OP, "]"))
2735 type = read_token_item(tok);
2746 static enum event_type
2747 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2750 struct format_field *field;
2751 enum event_type type;
2754 if (read_expect_type(EVENT_ITEM, &token) < 0)
2757 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2759 /* Find the field */
2760 field = tep_find_field(event, token);
2764 arg->dynarray.field = field;
2765 arg->dynarray.index = 0;
2767 if (read_expected(EVENT_DELIM, ")") < 0)
2770 type = read_token(&token);
2782 static enum event_type
2783 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2785 struct print_arg *item_arg;
2786 enum event_type type;
2789 type = process_arg(event, arg, &token);
2791 if (type == EVENT_ERROR)
2794 if (type == EVENT_OP)
2795 type = process_op(event, arg, &token);
2797 if (type == EVENT_ERROR)
2800 if (test_type_token(type, token, EVENT_DELIM, ")"))
2804 type = read_token_item(&token);
2807 * If the next token is an item or another open paren, then
2808 * this was a typecast.
2810 if (event_item_type(type) ||
2811 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2813 /* make this a typecast and contine */
2815 /* prevous must be an atom */
2816 if (arg->type != PRINT_ATOM) {
2817 do_warning_event(event, "previous needed to be PRINT_ATOM");
2821 item_arg = alloc_arg();
2823 do_warning_event(event, "%s: not enough memory!",
2828 arg->type = PRINT_TYPE;
2829 arg->typecast.type = arg->atom.atom;
2830 arg->typecast.item = item_arg;
2831 type = process_arg_token(event, item_arg, &token, type);
2845 static enum event_type
2846 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2849 enum event_type type;
2852 if (read_expect_type(EVENT_ITEM, &token) < 0)
2855 arg->type = PRINT_STRING;
2856 arg->string.string = token;
2857 arg->string.offset = -1;
2859 if (read_expected(EVENT_DELIM, ")") < 0)
2862 type = read_token(&token);
2874 static enum event_type
2875 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2878 enum event_type type;
2881 if (read_expect_type(EVENT_ITEM, &token) < 0)
2884 arg->type = PRINT_BITMASK;
2885 arg->bitmask.bitmask = token;
2886 arg->bitmask.offset = -1;
2888 if (read_expected(EVENT_DELIM, ")") < 0)
2891 type = read_token(&token);
2903 static struct tep_function_handler *
2904 find_func_handler(struct tep_handle *pevent, char *func_name)
2906 struct tep_function_handler *func;
2911 for (func = pevent->func_handlers; func; func = func->next) {
2912 if (strcmp(func->name, func_name) == 0)
2919 static void remove_func_handler(struct tep_handle *pevent, char *func_name)
2921 struct tep_function_handler *func;
2922 struct tep_function_handler **next;
2924 next = &pevent->func_handlers;
2925 while ((func = *next)) {
2926 if (strcmp(func->name, func_name) == 0) {
2928 free_func_handle(func);
2935 static enum event_type
2936 process_func_handler(struct event_format *event, struct tep_function_handler *func,
2937 struct print_arg *arg, char **tok)
2939 struct print_arg **next_arg;
2940 struct print_arg *farg;
2941 enum event_type type;
2945 arg->type = PRINT_FUNC;
2946 arg->func.func = func;
2950 next_arg = &(arg->func.args);
2951 for (i = 0; i < func->nr_args; i++) {
2954 do_warning_event(event, "%s: not enough memory!",
2959 type = process_arg(event, farg, &token);
2960 if (i < (func->nr_args - 1)) {
2961 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2962 do_warning_event(event,
2963 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2964 func->name, func->nr_args,
2965 event->name, i + 1);
2969 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2970 do_warning_event(event,
2971 "Error: function '%s()' only expects %d arguments but event %s has more",
2972 func->name, func->nr_args, event->name);
2978 next_arg = &(farg->next);
2982 type = read_token(&token);
2993 static enum event_type
2994 process_function(struct event_format *event, struct print_arg *arg,
2995 char *token, char **tok)
2997 struct tep_function_handler *func;
2999 if (strcmp(token, "__print_flags") == 0) {
3002 return process_flags(event, arg, tok);
3004 if (strcmp(token, "__print_symbolic") == 0) {
3006 is_symbolic_field = 1;
3007 return process_symbols(event, arg, tok);
3009 if (strcmp(token, "__print_hex") == 0) {
3011 return process_hex(event, arg, tok);
3013 if (strcmp(token, "__print_hex_str") == 0) {
3015 return process_hex_str(event, arg, tok);
3017 if (strcmp(token, "__print_array") == 0) {
3019 return process_int_array(event, arg, tok);
3021 if (strcmp(token, "__get_str") == 0) {
3023 return process_str(event, arg, tok);
3025 if (strcmp(token, "__get_bitmask") == 0) {
3027 return process_bitmask(event, arg, tok);
3029 if (strcmp(token, "__get_dynamic_array") == 0) {
3031 return process_dynamic_array(event, arg, tok);
3033 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3035 return process_dynamic_array_len(event, arg, tok);
3038 func = find_func_handler(event->pevent, token);
3041 return process_func_handler(event, func, arg, tok);
3044 do_warning_event(event, "function %s not defined", token);
3049 static enum event_type
3050 process_arg_token(struct event_format *event, struct print_arg *arg,
3051 char **tok, enum event_type type)
3060 if (strcmp(token, "REC") == 0) {
3062 type = process_entry(event, arg, &token);
3066 /* test the next token */
3067 type = read_token_item(&token);
3070 * If the next token is a parenthesis, then this
3073 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3076 /* this will free atom. */
3077 type = process_function(event, arg, atom, &token);
3080 /* atoms can be more than one token long */
3081 while (type == EVENT_ITEM) {
3083 new_atom = realloc(atom,
3084 strlen(atom) + strlen(token) + 2);
3093 strcat(atom, token);
3095 type = read_token_item(&token);
3098 arg->type = PRINT_ATOM;
3099 arg->atom.atom = atom;
3104 arg->type = PRINT_ATOM;
3105 arg->atom.atom = token;
3106 type = read_token_item(&token);
3109 if (strcmp(token, "(") == 0) {
3111 type = process_paren(event, arg, &token);
3115 /* handle single ops */
3116 arg->type = PRINT_OP;
3118 arg->op.left = NULL;
3119 type = process_op(event, arg, &token);
3121 /* On error, the op is freed */
3122 if (type == EVENT_ERROR)
3125 /* return error type if errored */
3128 case EVENT_ERROR ... EVENT_NEWLINE:
3130 do_warning_event(event, "unexpected type %d", type);
3138 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3140 enum event_type type = EVENT_ERROR;
3141 struct print_arg *arg;
3146 if (type == EVENT_NEWLINE) {
3147 type = read_token_item(&token);
3153 do_warning_event(event, "%s: not enough memory!",
3158 type = process_arg(event, arg, &token);
3160 if (type == EVENT_ERROR) {
3169 if (type == EVENT_OP) {
3170 type = process_op(event, arg, &token);
3172 if (type == EVENT_ERROR) {
3181 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3188 } while (type != EVENT_NONE);
3190 if (type != EVENT_NONE && type != EVENT_ERROR)
3196 static int event_read_print(struct event_format *event)
3198 enum event_type type;
3202 if (read_expected_item(EVENT_ITEM, "print") < 0)
3205 if (read_expected(EVENT_ITEM, "fmt") < 0)
3208 if (read_expected(EVENT_OP, ":") < 0)
3211 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3215 event->print_fmt.format = token;
3216 event->print_fmt.args = NULL;
3218 /* ok to have no arg */
3219 type = read_token_item(&token);
3221 if (type == EVENT_NONE)
3224 /* Handle concatenation of print lines */
3225 if (type == EVENT_DQUOTE) {
3228 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3231 free_token(event->print_fmt.format);
3232 event->print_fmt.format = NULL;
3237 if (test_type_token(type, token, EVENT_DELIM, ","))
3242 ret = event_read_print_args(event, &event->print_fmt.args);
3254 * tep_find_common_field - return a common field by event
3255 * @event: handle for the event
3256 * @name: the name of the common field to return
3258 * Returns a common field from the event by the given @name.
3259 * This only searchs the common fields and not all field.
3261 struct format_field *
3262 tep_find_common_field(struct event_format *event, const char *name)
3264 struct format_field *format;
3266 for (format = event->format.common_fields;
3267 format; format = format->next) {
3268 if (strcmp(format->name, name) == 0)
3276 * tep_find_field - find a non-common field
3277 * @event: handle for the event
3278 * @name: the name of the non-common field
3280 * Returns a non-common field by the given @name.
3281 * This does not search common fields.
3283 struct format_field *
3284 tep_find_field(struct event_format *event, const char *name)
3286 struct format_field *format;
3288 for (format = event->format.fields;
3289 format; format = format->next) {
3290 if (strcmp(format->name, name) == 0)
3298 * tep_find_any_field - find any field by name
3299 * @event: handle for the event
3300 * @name: the name of the field
3302 * Returns a field by the given @name.
3303 * This searchs the common field names first, then
3304 * the non-common ones if a common one was not found.
3306 struct format_field *
3307 tep_find_any_field(struct event_format *event, const char *name)
3309 struct format_field *format;
3311 format = tep_find_common_field(event, name);
3314 return tep_find_field(event, name);
3318 * tep_read_number - read a number from data
3319 * @pevent: handle for the pevent
3320 * @ptr: the raw data
3321 * @size: the size of the data that holds the number
3323 * Returns the number (converted to host) from the
3326 unsigned long long tep_read_number(struct tep_handle *pevent,
3327 const void *ptr, int size)
3331 return *(unsigned char *)ptr;
3333 return data2host2(pevent, ptr);
3335 return data2host4(pevent, ptr);
3337 return data2host8(pevent, ptr);
3345 * tep_read_number_field - read a number from data
3346 * @field: a handle to the field
3347 * @data: the raw data to read
3348 * @value: the value to place the number in
3350 * Reads raw data according to a field offset and size,
3351 * and translates it into @value.
3353 * Returns 0 on success, -1 otherwise.
3355 int tep_read_number_field(struct format_field *field, const void *data,
3356 unsigned long long *value)
3360 switch (field->size) {
3365 *value = tep_read_number(field->event->pevent,
3366 data + field->offset, field->size);
3373 static int get_common_info(struct tep_handle *pevent,
3374 const char *type, int *offset, int *size)
3376 struct event_format *event;
3377 struct format_field *field;
3380 * All events should have the same common elements.
3381 * Pick any event to find where the type is;
3383 if (!pevent->events) {
3384 do_warning("no event_list!");
3388 event = pevent->events[0];
3389 field = tep_find_common_field(event, type);
3393 *offset = field->offset;
3394 *size = field->size;
3399 static int __parse_common(struct tep_handle *pevent, void *data,
3400 int *size, int *offset, const char *name)
3405 ret = get_common_info(pevent, name, offset, size);
3409 return tep_read_number(pevent, data + *offset, *size);
3412 static int trace_parse_common_type(struct tep_handle *pevent, void *data)
3414 return __parse_common(pevent, data,
3415 &pevent->type_size, &pevent->type_offset,
3419 static int parse_common_pid(struct tep_handle *pevent, void *data)
3421 return __parse_common(pevent, data,
3422 &pevent->pid_size, &pevent->pid_offset,
3426 static int parse_common_pc(struct tep_handle *pevent, void *data)
3428 return __parse_common(pevent, data,
3429 &pevent->pc_size, &pevent->pc_offset,
3430 "common_preempt_count");
3433 static int parse_common_flags(struct tep_handle *pevent, void *data)
3435 return __parse_common(pevent, data,
3436 &pevent->flags_size, &pevent->flags_offset,
3440 static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
3442 return __parse_common(pevent, data,
3443 &pevent->ld_size, &pevent->ld_offset,
3444 "common_lock_depth");
3447 static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
3449 return __parse_common(pevent, data,
3450 &pevent->ld_size, &pevent->ld_offset,
3451 "common_migrate_disable");
3454 static int events_id_cmp(const void *a, const void *b);
3457 * tep_find_event - find an event by given id
3458 * @pevent: a handle to the pevent
3459 * @id: the id of the event
3461 * Returns an event that has a given @id.
3463 struct event_format *tep_find_event(struct tep_handle *pevent, int id)
3465 struct event_format **eventptr;
3466 struct event_format key;
3467 struct event_format *pkey = &key;
3469 /* Check cache first */
3470 if (pevent->last_event && pevent->last_event->id == id)
3471 return pevent->last_event;
3475 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3476 sizeof(*pevent->events), events_id_cmp);
3479 pevent->last_event = *eventptr;
3487 * tep_find_event_by_name - find an event by given name
3488 * @pevent: a handle to the pevent
3489 * @sys: the system name to search for
3490 * @name: the name of the event to search for
3492 * This returns an event with a given @name and under the system
3493 * @sys. If @sys is NULL the first event with @name is returned.
3495 struct event_format *
3496 tep_find_event_by_name(struct tep_handle *pevent,
3497 const char *sys, const char *name)
3499 struct event_format *event;
3502 if (pevent->last_event &&
3503 strcmp(pevent->last_event->name, name) == 0 &&
3504 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3505 return pevent->last_event;
3507 for (i = 0; i < pevent->nr_events; i++) {
3508 event = pevent->events[i];
3509 if (strcmp(event->name, name) == 0) {
3512 if (strcmp(event->system, sys) == 0)
3516 if (i == pevent->nr_events)
3519 pevent->last_event = event;
3523 static unsigned long long
3524 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3526 struct tep_handle *pevent = event->pevent;
3527 unsigned long long val = 0;
3528 unsigned long long left, right;
3529 struct print_arg *typearg = NULL;
3530 struct print_arg *larg;
3531 unsigned long offset;
3532 unsigned int field_size;
3534 switch (arg->type) {
3539 return strtoull(arg->atom.atom, NULL, 0);
3541 if (!arg->field.field) {
3542 arg->field.field = tep_find_any_field(event, arg->field.name);
3543 if (!arg->field.field)
3544 goto out_warning_field;
3547 /* must be a number */
3548 val = tep_read_number(pevent, data + arg->field.field->offset,
3549 arg->field.field->size);
3553 case PRINT_INT_ARRAY:
3558 val = eval_num_arg(data, size, event, arg->typecast.item);
3559 return eval_type(val, arg, 0);
3567 val = process_defined_func(&s, data, size, event, arg);
3568 trace_seq_destroy(&s);
3572 if (strcmp(arg->op.op, "[") == 0) {
3574 * Arrays are special, since we don't want
3575 * to read the arg as is.
3577 right = eval_num_arg(data, size, event, arg->op.right);
3579 /* handle typecasts */
3580 larg = arg->op.left;
3581 while (larg->type == PRINT_TYPE) {
3584 larg = larg->typecast.item;
3587 /* Default to long size */
3588 field_size = pevent->long_size;
3590 switch (larg->type) {
3591 case PRINT_DYNAMIC_ARRAY:
3592 offset = tep_read_number(pevent,
3593 data + larg->dynarray.field->offset,
3594 larg->dynarray.field->size);
3595 if (larg->dynarray.field->elementsize)
3596 field_size = larg->dynarray.field->elementsize;
3598 * The actual length of the dynamic array is stored
3599 * in the top half of the field, and the offset
3600 * is in the bottom half of the 32 bit field.
3606 if (!larg->field.field) {
3608 tep_find_any_field(event, larg->field.name);
3609 if (!larg->field.field) {
3611 goto out_warning_field;
3614 field_size = larg->field.field->elementsize;
3615 offset = larg->field.field->offset +
3616 right * larg->field.field->elementsize;
3619 goto default_op; /* oops, all bets off */
3621 val = tep_read_number(pevent,
3622 data + offset, field_size);
3624 val = eval_type(val, typearg, 1);
3626 } else if (strcmp(arg->op.op, "?") == 0) {
3627 left = eval_num_arg(data, size, event, arg->op.left);
3628 arg = arg->op.right;
3630 val = eval_num_arg(data, size, event, arg->op.left);
3632 val = eval_num_arg(data, size, event, arg->op.right);
3636 left = eval_num_arg(data, size, event, arg->op.left);
3637 right = eval_num_arg(data, size, event, arg->op.right);
3638 switch (arg->op.op[0]) {
3640 switch (arg->op.op[1]) {
3645 val = left != right;
3648 goto out_warning_op;
3656 val = left || right;
3662 val = left && right;
3667 switch (arg->op.op[1]) {
3672 val = left << right;
3675 val = left <= right;
3678 goto out_warning_op;
3682 switch (arg->op.op[1]) {
3687 val = left >> right;
3690 val = left >= right;
3693 goto out_warning_op;
3697 if (arg->op.op[1] != '=')
3698 goto out_warning_op;
3700 val = left == right;
3718 goto out_warning_op;
3721 case PRINT_DYNAMIC_ARRAY_LEN:
3722 offset = tep_read_number(pevent,
3723 data + arg->dynarray.field->offset,
3724 arg->dynarray.field->size);
3726 * The total allocated length of the dynamic array is
3727 * stored in the top half of the field, and the offset
3728 * is in the bottom half of the 32 bit field.
3730 val = (unsigned long long)(offset >> 16);
3732 case PRINT_DYNAMIC_ARRAY:
3733 /* Without [], we pass the address to the dynamic data */
3734 offset = tep_read_number(pevent,
3735 data + arg->dynarray.field->offset,
3736 arg->dynarray.field->size);
3738 * The total allocated length of the dynamic array is
3739 * stored in the top half of the field, and the offset
3740 * is in the bottom half of the 32 bit field.
3743 val = (unsigned long long)((unsigned long)data + offset);
3745 default: /* not sure what to do there */
3751 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3755 do_warning_event(event, "%s: field %s not found",
3756 __func__, arg->field.name);
3762 unsigned long long value;
3765 static const struct flag flags[] = {
3766 { "HI_SOFTIRQ", 0 },
3767 { "TIMER_SOFTIRQ", 1 },
3768 { "NET_TX_SOFTIRQ", 2 },
3769 { "NET_RX_SOFTIRQ", 3 },
3770 { "BLOCK_SOFTIRQ", 4 },
3771 { "IRQ_POLL_SOFTIRQ", 5 },
3772 { "TASKLET_SOFTIRQ", 6 },
3773 { "SCHED_SOFTIRQ", 7 },
3774 { "HRTIMER_SOFTIRQ", 8 },
3775 { "RCU_SOFTIRQ", 9 },
3777 { "HRTIMER_NORESTART", 0 },
3778 { "HRTIMER_RESTART", 1 },
3781 static long long eval_flag(const char *flag)
3786 * Some flags in the format files do not get converted.
3787 * If the flag is not numeric, see if it is something that
3788 * we already know about.
3790 if (isdigit(flag[0]))
3791 return strtoull(flag, NULL, 0);
3793 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3794 if (strcmp(flags[i].name, flag) == 0)
3795 return flags[i].value;
3800 static void print_str_to_seq(struct trace_seq *s, const char *format,
3801 int len_arg, const char *str)
3804 trace_seq_printf(s, format, len_arg, str);
3806 trace_seq_printf(s, format, str);
3809 static void print_bitmask_to_seq(struct tep_handle *pevent,
3810 struct trace_seq *s, const char *format,
3811 int len_arg, const void *data, int size)
3813 int nr_bits = size * 8;
3814 int str_size = (nr_bits + 3) / 4;
3822 * The kernel likes to put in commas every 32 bits, we
3825 str_size += (nr_bits - 1) / 32;
3827 str = malloc(str_size + 1);
3829 do_warning("%s: not enough memory!", __func__);
3834 /* Start out with -2 for the two chars per byte */
3835 for (i = str_size - 2; i >= 0; i -= 2) {
3837 * data points to a bit mask of size bytes.
3838 * In the kernel, this is an array of long words, thus
3839 * endianess is very important.
3841 if (pevent->file_bigendian)
3842 index = size - (len + 1);
3846 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3847 memcpy(str + i, buf, 2);
3849 if (!(len & 3) && i > 0) {
3856 trace_seq_printf(s, format, len_arg, str);
3858 trace_seq_printf(s, format, str);
3863 static void print_str_arg(struct trace_seq *s, void *data, int size,
3864 struct event_format *event, const char *format,
3865 int len_arg, struct print_arg *arg)
3867 struct tep_handle *pevent = event->pevent;
3868 struct print_flag_sym *flag;
3869 struct format_field *field;
3870 struct printk_map *printk;
3871 long long val, fval;
3872 unsigned long long addr;
3878 switch (arg->type) {
3883 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3886 field = arg->field.field;
3888 field = tep_find_any_field(event, arg->field.name);
3890 str = arg->field.name;
3891 goto out_warning_field;
3893 arg->field.field = field;
3895 /* Zero sized fields, mean the rest of the data */
3896 len = field->size ? : size - field->offset;
3899 * Some events pass in pointers. If this is not an array
3900 * and the size is the same as long_size, assume that it
3903 if (!(field->flags & FIELD_IS_ARRAY) &&
3904 field->size == pevent->long_size) {
3906 /* Handle heterogeneous recording and processing
3910 * Traces recorded on 32-bit devices (32-bit
3911 * addressing) and processed on 64-bit devices:
3912 * In this case, only 32 bits should be read.
3915 * Traces recorded on 64 bit devices and processed
3916 * on 32-bit devices:
3917 * In this case, 64 bits must be read.
3919 addr = (pevent->long_size == 8) ?
3920 *(unsigned long long *)(data + field->offset) :
3921 (unsigned long long)*(unsigned int *)(data + field->offset);
3923 /* Check if it matches a print format */
3924 printk = find_printk(pevent, addr);
3926 trace_seq_puts(s, printk->printk);
3928 trace_seq_printf(s, "%llx", addr);
3931 str = malloc(len + 1);
3933 do_warning_event(event, "%s: not enough memory!",
3937 memcpy(str, data + field->offset, len);
3939 print_str_to_seq(s, format, len_arg, str);
3943 val = eval_num_arg(data, size, event, arg->flags.field);
3945 for (flag = arg->flags.flags; flag; flag = flag->next) {
3946 fval = eval_flag(flag->value);
3947 if (!val && fval < 0) {
3948 print_str_to_seq(s, format, len_arg, flag->str);
3951 if (fval > 0 && (val & fval) == fval) {
3952 if (print && arg->flags.delim)
3953 trace_seq_puts(s, arg->flags.delim);
3954 print_str_to_seq(s, format, len_arg, flag->str);
3960 if (print && arg->flags.delim)
3961 trace_seq_puts(s, arg->flags.delim);
3962 trace_seq_printf(s, "0x%llx", val);
3966 val = eval_num_arg(data, size, event, arg->symbol.field);
3967 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3968 fval = eval_flag(flag->value);
3970 print_str_to_seq(s, format, len_arg, flag->str);
3975 trace_seq_printf(s, "0x%llx", val);
3979 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3980 unsigned long offset;
3981 offset = tep_read_number(pevent,
3982 data + arg->hex.field->dynarray.field->offset,
3983 arg->hex.field->dynarray.field->size);
3984 hex = data + (offset & 0xffff);
3986 field = arg->hex.field->field.field;
3988 str = arg->hex.field->field.name;
3989 field = tep_find_any_field(event, str);
3991 goto out_warning_field;
3992 arg->hex.field->field.field = field;
3994 hex = data + field->offset;
3996 len = eval_num_arg(data, size, event, arg->hex.size);
3997 for (i = 0; i < len; i++) {
3998 if (i && arg->type == PRINT_HEX)
3999 trace_seq_putc(s, ' ');
4000 trace_seq_printf(s, "%02x", hex[i]);
4004 case PRINT_INT_ARRAY: {
4008 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
4009 unsigned long offset;
4010 struct format_field *field =
4011 arg->int_array.field->dynarray.field;
4012 offset = tep_read_number(pevent,
4013 data + field->offset,
4015 num = data + (offset & 0xffff);
4017 field = arg->int_array.field->field.field;
4019 str = arg->int_array.field->field.name;
4020 field = tep_find_any_field(event, str);
4022 goto out_warning_field;
4023 arg->int_array.field->field.field = field;
4025 num = data + field->offset;
4027 len = eval_num_arg(data, size, event, arg->int_array.count);
4028 el_size = eval_num_arg(data, size, event,
4029 arg->int_array.el_size);
4030 for (i = 0; i < len; i++) {
4032 trace_seq_putc(s, ' ');
4035 trace_seq_printf(s, "%u", *(uint8_t *)num);
4036 } else if (el_size == 2) {
4037 trace_seq_printf(s, "%u", *(uint16_t *)num);
4038 } else if (el_size == 4) {
4039 trace_seq_printf(s, "%u", *(uint32_t *)num);
4040 } else if (el_size == 8) {
4041 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4043 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4044 el_size, *(uint8_t *)num);
4054 case PRINT_STRING: {
4057 if (arg->string.offset == -1) {
4058 struct format_field *f;
4060 f = tep_find_any_field(event, arg->string.string);
4061 arg->string.offset = f->offset;
4063 str_offset = data2host4(pevent, data + arg->string.offset);
4064 str_offset &= 0xffff;
4065 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4069 print_str_to_seq(s, format, len_arg, arg->string.string);
4071 case PRINT_BITMASK: {
4075 if (arg->bitmask.offset == -1) {
4076 struct format_field *f;
4078 f = tep_find_any_field(event, arg->bitmask.bitmask);
4079 arg->bitmask.offset = f->offset;
4081 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4082 bitmask_size = bitmask_offset >> 16;
4083 bitmask_offset &= 0xffff;
4084 print_bitmask_to_seq(pevent, s, format, len_arg,
4085 data + bitmask_offset, bitmask_size);
4090 * The only op for string should be ? :
4092 if (arg->op.op[0] != '?')
4094 val = eval_num_arg(data, size, event, arg->op.left);
4096 print_str_arg(s, data, size, event,
4097 format, len_arg, arg->op.right->op.left);
4099 print_str_arg(s, data, size, event,
4100 format, len_arg, arg->op.right->op.right);
4103 process_defined_func(s, data, size, event, arg);
4113 do_warning_event(event, "%s: field %s not found",
4114 __func__, arg->field.name);
4117 static unsigned long long
4118 process_defined_func(struct trace_seq *s, void *data, int size,
4119 struct event_format *event, struct print_arg *arg)
4121 struct tep_function_handler *func_handle = arg->func.func;
4122 struct func_params *param;
4123 unsigned long long *args;
4124 unsigned long long ret;
4125 struct print_arg *farg;
4126 struct trace_seq str;
4128 struct save_str *next;
4130 } *strings = NULL, *string;
4133 if (!func_handle->nr_args) {
4134 ret = (*func_handle->func)(s, NULL);
4138 farg = arg->func.args;
4139 param = func_handle->params;
4142 args = malloc(sizeof(*args) * func_handle->nr_args);
4146 for (i = 0; i < func_handle->nr_args; i++) {
4147 switch (param->type) {
4148 case TEP_FUNC_ARG_INT:
4149 case TEP_FUNC_ARG_LONG:
4150 case TEP_FUNC_ARG_PTR:
4151 args[i] = eval_num_arg(data, size, event, farg);
4153 case TEP_FUNC_ARG_STRING:
4154 trace_seq_init(&str);
4155 print_str_arg(&str, data, size, event, "%s", -1, farg);
4156 trace_seq_terminate(&str);
4157 string = malloc(sizeof(*string));
4159 do_warning_event(event, "%s(%d): malloc str",
4160 __func__, __LINE__);
4163 string->next = strings;
4164 string->str = strdup(str.buffer);
4167 do_warning_event(event, "%s(%d): malloc str",
4168 __func__, __LINE__);
4171 args[i] = (uintptr_t)string->str;
4173 trace_seq_destroy(&str);
4177 * Something went totally wrong, this is not
4178 * an input error, something in this code broke.
4180 do_warning_event(event, "Unexpected end of arguments\n");
4184 param = param->next;
4187 ret = (*func_handle->func)(s, args);
4192 strings = string->next;
4198 /* TBD : handle return type here */
4202 static void free_args(struct print_arg *args)
4204 struct print_arg *next;
4214 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4216 struct tep_handle *pevent = event->pevent;
4217 struct format_field *field, *ip_field;
4218 struct print_arg *args, *arg, **next;
4219 unsigned long long ip, val;
4224 field = pevent->bprint_buf_field;
4225 ip_field = pevent->bprint_ip_field;
4228 field = tep_find_field(event, "buf");
4230 do_warning_event(event, "can't find buffer field for binary printk");
4233 ip_field = tep_find_field(event, "ip");
4235 do_warning_event(event, "can't find ip field for binary printk");
4238 pevent->bprint_buf_field = field;
4239 pevent->bprint_ip_field = ip_field;
4242 ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
4245 * The first arg is the IP pointer.
4249 do_warning_event(event, "%s(%d): not enough memory!",
4250 __func__, __LINE__);
4257 arg->type = PRINT_ATOM;
4259 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4262 /* skip the first "%ps: " */
4263 for (ptr = fmt + 5, bptr = data + field->offset;
4264 bptr < data + size && *ptr; ptr++) {
4289 if (isalnum(ptr[1])) {
4291 /* Check for special pointers */
4300 * Older kernels do not process
4301 * dereferenced pointers.
4302 * Only process if the pointer
4303 * value is a printable.
4305 if (isprint(*(char *)bptr))
4306 goto process_string;
4319 vsize = pevent->long_size;
4333 /* the pointers are always 4 bytes aligned */
4334 bptr = (void *)(((unsigned long)bptr + 3) &
4336 val = tep_read_number(pevent, bptr, vsize);
4340 do_warning_event(event, "%s(%d): not enough memory!",
4341 __func__, __LINE__);
4345 arg->type = PRINT_ATOM;
4346 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4353 * The '*' case means that an arg is used as the length.
4354 * We need to continue to figure out for what.
4364 do_warning_event(event, "%s(%d): not enough memory!",
4365 __func__, __LINE__);
4369 arg->type = PRINT_BSTRING;
4370 arg->string.string = strdup(bptr);
4371 if (!arg->string.string)
4373 bptr += strlen(bptr) + 1;
4390 get_bprint_format(void *data, int size __maybe_unused,
4391 struct event_format *event)
4393 struct tep_handle *pevent = event->pevent;
4394 unsigned long long addr;
4395 struct format_field *field;
4396 struct printk_map *printk;
4399 field = pevent->bprint_fmt_field;
4402 field = tep_find_field(event, "fmt");
4404 do_warning_event(event, "can't find format field for binary printk");
4407 pevent->bprint_fmt_field = field;
4410 addr = tep_read_number(pevent, data + field->offset, field->size);
4412 printk = find_printk(pevent, addr);
4414 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4419 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4425 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4426 struct event_format *event, struct print_arg *arg)
4429 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4431 if (arg->type == PRINT_FUNC) {
4432 process_defined_func(s, data, size, event, arg);
4436 if (arg->type != PRINT_FIELD) {
4437 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4443 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4444 if (!arg->field.field) {
4446 tep_find_any_field(event, arg->field.name);
4447 if (!arg->field.field) {
4448 do_warning_event(event, "%s: field %s not found",
4449 __func__, arg->field.name);
4453 if (arg->field.field->size != 6) {
4454 trace_seq_printf(s, "INVALIDMAC");
4457 buf = data + arg->field.field->offset;
4458 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4461 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4466 fmt = "%03d.%03d.%03d.%03d";
4468 fmt = "%d.%d.%d.%d";
4470 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4473 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4475 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4476 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4479 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4481 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4484 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4487 unsigned char zerolength[8];
4492 bool needcolon = false;
4494 struct in6_addr in6;
4496 memcpy(&in6, addr, sizeof(struct in6_addr));
4498 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4500 memset(zerolength, 0, sizeof(zerolength));
4507 /* find position of longest 0 run */
4508 for (i = 0; i < range; i++) {
4509 for (j = i; j < range; j++) {
4510 if (in6.s6_addr16[j] != 0)
4515 for (i = 0; i < range; i++) {
4516 if (zerolength[i] > longest) {
4517 longest = zerolength[i];
4521 if (longest == 1) /* don't compress a single 0 */
4525 for (i = 0; i < range; i++) {
4526 if (i == colonpos) {
4527 if (needcolon || i == 0)
4528 trace_seq_printf(s, ":");
4529 trace_seq_printf(s, ":");
4535 trace_seq_printf(s, ":");
4538 /* hex u16 without leading 0s */
4539 word = ntohs(in6.s6_addr16[i]);
4543 trace_seq_printf(s, "%x%02x", hi, lo);
4545 trace_seq_printf(s, "%x", lo);
4552 trace_seq_printf(s, ":");
4553 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4559 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4563 for (j = 0; j < 16; j += 2) {
4564 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4565 if (i == 'I' && j < 14)
4566 trace_seq_printf(s, ":");
4571 * %pi4 print an IPv4 address with leading zeros
4572 * %pI4 print an IPv4 address without leading zeros
4573 * %pi6 print an IPv6 address without colons
4574 * %pI6 print an IPv6 address with colons
4575 * %pI6c print an IPv6 address in compressed form with colons
4576 * %pISpc print an IP address based on sockaddr; p adds port.
4578 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4579 void *data, int size, struct event_format *event,
4580 struct print_arg *arg)
4584 if (arg->type == PRINT_FUNC) {
4585 process_defined_func(s, data, size, event, arg);
4589 if (arg->type != PRINT_FIELD) {
4590 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4594 if (!arg->field.field) {
4596 tep_find_any_field(event, arg->field.name);
4597 if (!arg->field.field) {
4598 do_warning("%s: field %s not found",
4599 __func__, arg->field.name);
4604 buf = data + arg->field.field->offset;
4606 if (arg->field.field->size != 4) {
4607 trace_seq_printf(s, "INVALIDIPv4");
4610 print_ip4_addr(s, i, buf);
4615 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4616 void *data, int size, struct event_format *event,
4617 struct print_arg *arg)
4624 if (i == 'I' && *ptr == 'c') {
4630 if (arg->type == PRINT_FUNC) {
4631 process_defined_func(s, data, size, event, arg);
4635 if (arg->type != PRINT_FIELD) {
4636 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4640 if (!arg->field.field) {
4642 tep_find_any_field(event, arg->field.name);
4643 if (!arg->field.field) {
4644 do_warning("%s: field %s not found",
4645 __func__, arg->field.name);
4650 buf = data + arg->field.field->offset;
4652 if (arg->field.field->size != 16) {
4653 trace_seq_printf(s, "INVALIDIPv6");
4658 print_ip6c_addr(s, buf);
4660 print_ip6_addr(s, i, buf);
4665 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4666 void *data, int size, struct event_format *event,
4667 struct print_arg *arg)
4669 char have_c = 0, have_p = 0;
4671 struct sockaddr_storage *sa;
4688 if (arg->type == PRINT_FUNC) {
4689 process_defined_func(s, data, size, event, arg);
4693 if (arg->type != PRINT_FIELD) {
4694 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4698 if (!arg->field.field) {
4700 tep_find_any_field(event, arg->field.name);
4701 if (!arg->field.field) {
4702 do_warning("%s: field %s not found",
4703 __func__, arg->field.name);
4708 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4710 if (sa->ss_family == AF_INET) {
4711 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4713 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4714 trace_seq_printf(s, "INVALIDIPv4");
4718 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4720 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4723 } else if (sa->ss_family == AF_INET6) {
4724 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4726 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4727 trace_seq_printf(s, "INVALIDIPv6");
4732 trace_seq_printf(s, "[");
4734 buf = (unsigned char *) &sa6->sin6_addr;
4736 print_ip6c_addr(s, buf);
4738 print_ip6_addr(s, i, buf);
4741 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4747 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4748 void *data, int size, struct event_format *event,
4749 struct print_arg *arg)
4751 char i = *ptr; /* 'i' or 'I' */
4764 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4767 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4770 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4779 static int is_printable_array(char *p, unsigned int len)
4783 for (i = 0; i < len && p[i]; i++)
4784 if (!isprint(p[i]) && !isspace(p[i]))
4789 void tep_print_field(struct trace_seq *s, void *data,
4790 struct format_field *field)
4792 unsigned long long val;
4793 unsigned int offset, len, i;
4794 struct tep_handle *pevent = field->event->pevent;
4796 if (field->flags & FIELD_IS_ARRAY) {
4797 offset = field->offset;
4799 if (field->flags & FIELD_IS_DYNAMIC) {
4800 val = tep_read_number(pevent, data + offset, len);
4805 if (field->flags & FIELD_IS_STRING &&
4806 is_printable_array(data + offset, len)) {
4807 trace_seq_printf(s, "%s", (char *)data + offset);
4809 trace_seq_puts(s, "ARRAY[");
4810 for (i = 0; i < len; i++) {
4812 trace_seq_puts(s, ", ");
4813 trace_seq_printf(s, "%02x",
4814 *((unsigned char *)data + offset + i));
4816 trace_seq_putc(s, ']');
4817 field->flags &= ~FIELD_IS_STRING;
4820 val = tep_read_number(pevent, data + field->offset,
4822 if (field->flags & FIELD_IS_POINTER) {
4823 trace_seq_printf(s, "0x%llx", val);
4824 } else if (field->flags & FIELD_IS_SIGNED) {
4825 switch (field->size) {
4828 * If field is long then print it in hex.
4829 * A long usually stores pointers.
4831 if (field->flags & FIELD_IS_LONG)
4832 trace_seq_printf(s, "0x%x", (int)val);
4834 trace_seq_printf(s, "%d", (int)val);
4837 trace_seq_printf(s, "%2d", (short)val);
4840 trace_seq_printf(s, "%1d", (char)val);
4843 trace_seq_printf(s, "%lld", val);
4846 if (field->flags & FIELD_IS_LONG)
4847 trace_seq_printf(s, "0x%llx", val);
4849 trace_seq_printf(s, "%llu", val);
4854 void tep_print_fields(struct trace_seq *s, void *data,
4855 int size __maybe_unused, struct event_format *event)
4857 struct format_field *field;
4859 field = event->format.fields;
4861 trace_seq_printf(s, " %s=", field->name);
4862 tep_print_field(s, data, field);
4863 field = field->next;
4867 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4869 struct tep_handle *pevent = event->pevent;
4870 struct print_fmt *print_fmt = &event->print_fmt;
4871 struct print_arg *arg = print_fmt->args;
4872 struct print_arg *args = NULL;
4873 const char *ptr = print_fmt->format;
4874 unsigned long long val;
4875 struct func_map *func;
4876 const char *saveptr;
4878 char *bprint_fmt = NULL;
4886 if (event->flags & EVENT_FL_FAILED) {
4887 trace_seq_printf(s, "[FAILED TO PARSE]");
4888 tep_print_fields(s, data, size, event);
4892 if (event->flags & EVENT_FL_ISBPRINT) {
4893 bprint_fmt = get_bprint_format(data, size, event);
4894 args = make_bprint_args(bprint_fmt, data, size, event);
4899 for (; *ptr; ptr++) {
4905 trace_seq_putc(s, '\n');
4908 trace_seq_putc(s, '\t');
4911 trace_seq_putc(s, '\r');
4914 trace_seq_putc(s, '\\');
4917 trace_seq_putc(s, *ptr);
4921 } else if (*ptr == '%') {
4929 trace_seq_putc(s, '%');
4932 /* FIXME: need to handle properly */
4944 /* The argument is the length. */
4946 do_warning_event(event, "no argument match");
4947 event->flags |= EVENT_FL_FAILED;
4950 len_arg = eval_num_arg(data, size, event, arg);
4961 if (pevent->long_size == 4)
4966 if (isalnum(ptr[1]))
4969 if (arg->type == PRINT_BSTRING) {
4970 trace_seq_puts(s, arg->string.string);
4974 if (*ptr == 'F' || *ptr == 'f' ||
4975 *ptr == 'S' || *ptr == 's') {
4977 } else if (*ptr == 'M' || *ptr == 'm') {
4978 print_mac_arg(s, *ptr, data, size, event, arg);
4981 } else if (*ptr == 'I' || *ptr == 'i') {
4984 n = print_ip_arg(s, ptr, data, size, event, arg);
4999 do_warning_event(event, "no argument match");
5000 event->flags |= EVENT_FL_FAILED;
5004 len = ((unsigned long)ptr + 1) -
5005 (unsigned long)saveptr;
5007 /* should never happen */
5009 do_warning_event(event, "bad format!");
5010 event->flags |= EVENT_FL_FAILED;
5014 memcpy(format, saveptr, len);
5017 val = eval_num_arg(data, size, event, arg);
5021 func = find_func(pevent, val);
5023 trace_seq_puts(s, func->func);
5024 if (show_func == 'F')
5031 if (pevent->long_size == 8 && ls == 1 &&
5032 sizeof(long) != 8) {
5035 /* make %l into %ll */
5036 if (ls == 1 && (p = strchr(format, 'l')))
5037 memmove(p+1, p, strlen(p)+1);
5038 else if (strcmp(format, "%p") == 0)
5039 strcpy(format, "0x%llx");
5045 trace_seq_printf(s, format, len_arg, (char)val);
5047 trace_seq_printf(s, format, (char)val);
5051 trace_seq_printf(s, format, len_arg, (short)val);
5053 trace_seq_printf(s, format, (short)val);
5057 trace_seq_printf(s, format, len_arg, (int)val);
5059 trace_seq_printf(s, format, (int)val);
5063 trace_seq_printf(s, format, len_arg, (long)val);
5065 trace_seq_printf(s, format, (long)val);
5069 trace_seq_printf(s, format, len_arg,
5072 trace_seq_printf(s, format, (long long)val);
5075 do_warning_event(event, "bad count (%d)", ls);
5076 event->flags |= EVENT_FL_FAILED;
5081 do_warning_event(event, "no matching argument");
5082 event->flags |= EVENT_FL_FAILED;
5086 len = ((unsigned long)ptr + 1) -
5087 (unsigned long)saveptr;
5089 /* should never happen */
5091 do_warning_event(event, "bad format!");
5092 event->flags |= EVENT_FL_FAILED;
5096 memcpy(format, saveptr, len);
5100 /* Use helper trace_seq */
5102 print_str_arg(&p, data, size, event,
5103 format, len_arg, arg);
5104 trace_seq_terminate(&p);
5105 trace_seq_puts(s, p.buffer);
5106 trace_seq_destroy(&p);
5110 trace_seq_printf(s, ">%c<", *ptr);
5114 trace_seq_putc(s, *ptr);
5117 if (event->flags & EVENT_FL_FAILED) {
5119 trace_seq_printf(s, "[FAILED TO PARSE]");
5129 * tep_data_lat_fmt - parse the data for the latency format
5130 * @pevent: a handle to the pevent
5131 * @s: the trace_seq to write to
5132 * @record: the record to read from
5134 * This parses out the Latency format (interrupts disabled,
5135 * need rescheduling, in hard/soft interrupt, preempt count
5136 * and lock depth) and places it into the trace_seq.
5138 void tep_data_lat_fmt(struct tep_handle *pevent,
5139 struct trace_seq *s, struct tep_record *record)
5141 static int check_lock_depth = 1;
5142 static int check_migrate_disable = 1;
5143 static int lock_depth_exists;
5144 static int migrate_disable_exists;
5145 unsigned int lat_flags;
5148 int migrate_disable;
5151 void *data = record->data;
5153 lat_flags = parse_common_flags(pevent, data);
5154 pc = parse_common_pc(pevent, data);
5155 /* lock_depth may not always exist */
5156 if (lock_depth_exists)
5157 lock_depth = parse_common_lock_depth(pevent, data);
5158 else if (check_lock_depth) {
5159 lock_depth = parse_common_lock_depth(pevent, data);
5161 check_lock_depth = 0;
5163 lock_depth_exists = 1;
5166 /* migrate_disable may not always exist */
5167 if (migrate_disable_exists)
5168 migrate_disable = parse_common_migrate_disable(pevent, data);
5169 else if (check_migrate_disable) {
5170 migrate_disable = parse_common_migrate_disable(pevent, data);
5171 if (migrate_disable < 0)
5172 check_migrate_disable = 0;
5174 migrate_disable_exists = 1;
5177 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5178 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5180 trace_seq_printf(s, "%c%c%c",
5181 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5182 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5184 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5186 (hardirq && softirq) ? 'H' :
5187 hardirq ? 'h' : softirq ? 's' : '.');
5190 trace_seq_printf(s, "%x", pc);
5192 trace_seq_putc(s, '.');
5194 if (migrate_disable_exists) {
5195 if (migrate_disable < 0)
5196 trace_seq_putc(s, '.');
5198 trace_seq_printf(s, "%d", migrate_disable);
5201 if (lock_depth_exists) {
5203 trace_seq_putc(s, '.');
5205 trace_seq_printf(s, "%d", lock_depth);
5208 trace_seq_terminate(s);
5212 * tep_data_type - parse out the given event type
5213 * @pevent: a handle to the pevent
5214 * @rec: the record to read from
5216 * This returns the event id from the @rec.
5218 int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
5220 return trace_parse_common_type(pevent, rec->data);
5224 * tep_data_event_from_type - find the event by a given type
5225 * @pevent: a handle to the pevent
5226 * @type: the type of the event.
5228 * This returns the event form a given @type;
5230 struct event_format *tep_data_event_from_type(struct tep_handle *pevent, int type)
5232 return tep_find_event(pevent, type);
5236 * tep_data_pid - parse the PID from record
5237 * @pevent: a handle to the pevent
5238 * @rec: the record to parse
5240 * This returns the PID from a record.
5242 int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
5244 return parse_common_pid(pevent, rec->data);
5248 * tep_data_preempt_count - parse the preempt count from the record
5249 * @pevent: a handle to the pevent
5250 * @rec: the record to parse
5252 * This returns the preempt count from a record.
5254 int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
5256 return parse_common_pc(pevent, rec->data);
5260 * tep_data_flags - parse the latency flags from the record
5261 * @pevent: a handle to the pevent
5262 * @rec: the record to parse
5264 * This returns the latency flags from a record.
5266 * Use trace_flag_type enum for the flags (see event-parse.h).
5268 int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
5270 return parse_common_flags(pevent, rec->data);
5274 * tep_data_comm_from_pid - return the command line from PID
5275 * @pevent: a handle to the pevent
5276 * @pid: the PID of the task to search for
5278 * This returns a pointer to the command line that has the given
5281 const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
5285 comm = find_cmdline(pevent, pid);
5289 static struct cmdline *
5290 pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct cmdline *next)
5292 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5295 cmdlist = cmdlist->next;
5297 cmdlist = pevent->cmdlist;
5299 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5300 cmdlist = cmdlist->next;
5302 return (struct cmdline *)cmdlist;
5306 * tep_data_pid_from_comm - return the pid from a given comm
5307 * @pevent: a handle to the pevent
5308 * @comm: the cmdline to find the pid from
5309 * @next: the cmdline structure to find the next comm
5311 * This returns the cmdline structure that holds a pid for a given
5312 * comm, or NULL if none found. As there may be more than one pid for
5313 * a given comm, the result of this call can be passed back into
5314 * a recurring call in the @next paramater, and then it will find the
5316 * Also, it does a linear seach, so it may be slow.
5318 struct cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
5319 struct cmdline *next)
5321 struct cmdline *cmdline;
5324 * If the cmdlines have not been converted yet, then use
5327 if (!pevent->cmdlines)
5328 return pid_from_cmdlist(pevent, comm, next);
5332 * The next pointer could have been still from
5333 * a previous call before cmdlines were created
5335 if (next < pevent->cmdlines ||
5336 next >= pevent->cmdlines + pevent->cmdline_count)
5343 cmdline = pevent->cmdlines;
5345 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5346 if (strcmp(cmdline->comm, comm) == 0)
5354 * tep_cmdline_pid - return the pid associated to a given cmdline
5355 * @cmdline: The cmdline structure to get the pid from
5357 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5360 int tep_cmdline_pid(struct tep_handle *pevent, struct cmdline *cmdline)
5362 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5368 * If cmdlines have not been created yet, or cmdline is
5369 * not part of the array, then treat it as a cmdlist instead.
5371 if (!pevent->cmdlines ||
5372 cmdline < pevent->cmdlines ||
5373 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5374 return cmdlist->pid;
5376 return cmdline->pid;
5380 * tep_event_info - parse the data into the print format
5381 * @s: the trace_seq to write to
5382 * @event: the handle to the event
5383 * @record: the record to read from
5385 * This parses the raw @data using the given @event information and
5386 * writes the print format into the trace_seq.
5388 void tep_event_info(struct trace_seq *s, struct event_format *event,
5389 struct tep_record *record)
5391 int print_pretty = 1;
5393 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5394 tep_print_fields(s, record->data, record->size, event);
5397 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5398 print_pretty = event->handler(s, record, event,
5402 pretty_print(s, record->data, record->size, event);
5405 trace_seq_terminate(s);
5408 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5410 if (!use_trace_clock)
5413 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5414 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5417 /* trace_clock is setting in tsc or counter mode */
5422 * tep_find_event_by_record - return the event from a given record
5423 * @pevent: a handle to the pevent
5424 * @record: The record to get the event from
5426 * Returns the associated event for a given record, or NULL if non is
5429 struct event_format *
5430 tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
5434 if (record->size < 0) {
5435 do_warning("ug! negative record size %d", record->size);
5439 type = trace_parse_common_type(pevent, record->data);
5441 return tep_find_event(pevent, type);
5445 * tep_print_event_task - Write the event task comm, pid and CPU
5446 * @pevent: a handle to the pevent
5447 * @s: the trace_seq to write to
5448 * @event: the handle to the record's event
5449 * @record: The record to get the event from
5451 * Writes the tasks comm, pid and CPU to @s.
5453 void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
5454 struct event_format *event,
5455 struct tep_record *record)
5457 void *data = record->data;
5461 pid = parse_common_pid(pevent, data);
5462 comm = find_cmdline(pevent, pid);
5464 if (pevent->latency_format) {
5465 trace_seq_printf(s, "%8.8s-%-5d %3d",
5466 comm, pid, record->cpu);
5468 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5472 * tep_print_event_time - Write the event timestamp
5473 * @pevent: a handle to the pevent
5474 * @s: the trace_seq to write to
5475 * @event: the handle to the record's event
5476 * @record: The record to get the event from
5477 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5479 * Writes the timestamp of the record into @s.
5481 void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
5482 struct event_format *event,
5483 struct tep_record *record,
5484 bool use_trace_clock)
5487 unsigned long usecs;
5488 unsigned long nsecs;
5490 bool use_usec_format;
5492 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5494 if (use_usec_format) {
5495 secs = record->ts / NSEC_PER_SEC;
5496 nsecs = record->ts - secs * NSEC_PER_SEC;
5499 if (pevent->latency_format) {
5500 tep_data_lat_fmt(pevent, s, record);
5503 if (use_usec_format) {
5504 if (pevent->flags & TEP_NSEC_OUTPUT) {
5508 usecs = (nsecs + 500) / NSEC_PER_USEC;
5509 /* To avoid usecs larger than 1 sec */
5510 if (usecs >= USEC_PER_SEC) {
5511 usecs -= USEC_PER_SEC;
5517 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5519 trace_seq_printf(s, " %12llu:", record->ts);
5523 * tep_print_event_data - Write the event data section
5524 * @pevent: a handle to the pevent
5525 * @s: the trace_seq to write to
5526 * @event: the handle to the record's event
5527 * @record: The record to get the event from
5529 * Writes the parsing of the record's data to @s.
5531 void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
5532 struct event_format *event,
5533 struct tep_record *record)
5535 static const char *spaces = " "; /* 20 spaces */
5538 trace_seq_printf(s, " %s: ", event->name);
5540 /* Space out the event names evenly. */
5541 len = strlen(event->name);
5543 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5545 tep_event_info(s, event, record);
5548 void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
5549 struct tep_record *record, bool use_trace_clock)
5551 struct event_format *event;
5553 event = tep_find_event_by_record(pevent, record);
5556 int type = trace_parse_common_type(pevent, record->data);
5558 do_warning("ug! no event found for type %d", type);
5559 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5560 for (i = 0; i < record->size; i++)
5561 trace_seq_printf(s, " %02x",
5562 ((unsigned char *)record->data)[i]);
5566 tep_print_event_task(pevent, s, event, record);
5567 tep_print_event_time(pevent, s, event, record, use_trace_clock);
5568 tep_print_event_data(pevent, s, event, record);
5571 static int events_id_cmp(const void *a, const void *b)
5573 struct event_format * const * ea = a;
5574 struct event_format * const * eb = b;
5576 if ((*ea)->id < (*eb)->id)
5579 if ((*ea)->id > (*eb)->id)
5585 static int events_name_cmp(const void *a, const void *b)
5587 struct event_format * const * ea = a;
5588 struct event_format * const * eb = b;
5591 res = strcmp((*ea)->name, (*eb)->name);
5595 res = strcmp((*ea)->system, (*eb)->system);
5599 return events_id_cmp(a, b);
5602 static int events_system_cmp(const void *a, const void *b)
5604 struct event_format * const * ea = a;
5605 struct event_format * const * eb = b;
5608 res = strcmp((*ea)->system, (*eb)->system);
5612 res = strcmp((*ea)->name, (*eb)->name);
5616 return events_id_cmp(a, b);
5619 struct event_format **tep_list_events(struct tep_handle *pevent, enum event_sort_type sort_type)
5621 struct event_format **events;
5622 int (*sort)(const void *a, const void *b);
5624 events = pevent->sort_events;
5626 if (events && pevent->last_type == sort_type)
5630 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5634 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5635 events[pevent->nr_events] = NULL;
5637 pevent->sort_events = events;
5639 /* the internal events are sorted by id */
5640 if (sort_type == EVENT_SORT_ID) {
5641 pevent->last_type = sort_type;
5646 switch (sort_type) {
5648 sort = events_id_cmp;
5650 case EVENT_SORT_NAME:
5651 sort = events_name_cmp;
5653 case EVENT_SORT_SYSTEM:
5654 sort = events_system_cmp;
5660 qsort(events, pevent->nr_events, sizeof(*events), sort);
5661 pevent->last_type = sort_type;
5666 static struct format_field **
5667 get_event_fields(const char *type, const char *name,
5668 int count, struct format_field *list)
5670 struct format_field **fields;
5671 struct format_field *field;
5674 fields = malloc(sizeof(*fields) * (count + 1));
5678 for (field = list; field; field = field->next) {
5679 fields[i++] = field;
5680 if (i == count + 1) {
5681 do_warning("event %s has more %s fields than specified",
5689 do_warning("event %s has less %s fields than specified",
5698 * tep_event_common_fields - return a list of common fields for an event
5699 * @event: the event to return the common fields of.
5701 * Returns an allocated array of fields. The last item in the array is NULL.
5702 * The array must be freed with free().
5704 struct format_field **tep_event_common_fields(struct event_format *event)
5706 return get_event_fields("common", event->name,
5707 event->format.nr_common,
5708 event->format.common_fields);
5712 * tep_event_fields - return a list of event specific fields for an event
5713 * @event: the event to return the fields of.
5715 * Returns an allocated array of fields. The last item in the array is NULL.
5716 * The array must be freed with free().
5718 struct format_field **tep_event_fields(struct event_format *event)
5720 return get_event_fields("event", event->name,
5721 event->format.nr_fields,
5722 event->format.fields);
5725 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5727 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5729 trace_seq_puts(s, ", ");
5730 print_fields(s, field->next);
5735 static void print_args(struct print_arg *args)
5737 int print_paren = 1;
5740 switch (args->type) {
5745 printf("%s", args->atom.atom);
5748 printf("REC->%s", args->field.name);
5751 printf("__print_flags(");
5752 print_args(args->flags.field);
5753 printf(", %s, ", args->flags.delim);
5755 print_fields(&s, args->flags.flags);
5756 trace_seq_do_printf(&s);
5757 trace_seq_destroy(&s);
5761 printf("__print_symbolic(");
5762 print_args(args->symbol.field);
5765 print_fields(&s, args->symbol.symbols);
5766 trace_seq_do_printf(&s);
5767 trace_seq_destroy(&s);
5771 printf("__print_hex(");
5772 print_args(args->hex.field);
5774 print_args(args->hex.size);
5778 printf("__print_hex_str(");
5779 print_args(args->hex.field);
5781 print_args(args->hex.size);
5784 case PRINT_INT_ARRAY:
5785 printf("__print_array(");
5786 print_args(args->int_array.field);
5788 print_args(args->int_array.count);
5790 print_args(args->int_array.el_size);
5795 printf("__get_str(%s)", args->string.string);
5798 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5801 printf("(%s)", args->typecast.type);
5802 print_args(args->typecast.item);
5805 if (strcmp(args->op.op, ":") == 0)
5809 print_args(args->op.left);
5810 printf(" %s ", args->op.op);
5811 print_args(args->op.right);
5816 /* we should warn... */
5821 print_args(args->next);
5825 static void parse_header_field(const char *field,
5826 int *offset, int *size, int mandatory)
5828 unsigned long long save_input_buf_ptr;
5829 unsigned long long save_input_buf_siz;
5833 save_input_buf_ptr = input_buf_ptr;
5834 save_input_buf_siz = input_buf_siz;
5836 if (read_expected(EVENT_ITEM, "field") < 0)
5838 if (read_expected(EVENT_OP, ":") < 0)
5842 if (read_expect_type(EVENT_ITEM, &token) < 0)
5847 * If this is not a mandatory field, then test it first.
5850 if (read_expected(EVENT_ITEM, field) < 0)
5853 if (read_expect_type(EVENT_ITEM, &token) < 0)
5855 if (strcmp(token, field) != 0)
5860 if (read_expected(EVENT_OP, ";") < 0)
5862 if (read_expected(EVENT_ITEM, "offset") < 0)
5864 if (read_expected(EVENT_OP, ":") < 0)
5866 if (read_expect_type(EVENT_ITEM, &token) < 0)
5868 *offset = atoi(token);
5870 if (read_expected(EVENT_OP, ";") < 0)
5872 if (read_expected(EVENT_ITEM, "size") < 0)
5874 if (read_expected(EVENT_OP, ":") < 0)
5876 if (read_expect_type(EVENT_ITEM, &token) < 0)
5878 *size = atoi(token);
5880 if (read_expected(EVENT_OP, ";") < 0)
5882 type = read_token(&token);
5883 if (type != EVENT_NEWLINE) {
5884 /* newer versions of the kernel have a "signed" type */
5885 if (type != EVENT_ITEM)
5888 if (strcmp(token, "signed") != 0)
5893 if (read_expected(EVENT_OP, ":") < 0)
5896 if (read_expect_type(EVENT_ITEM, &token))
5900 if (read_expected(EVENT_OP, ";") < 0)
5903 if (read_expect_type(EVENT_NEWLINE, &token))
5911 input_buf_ptr = save_input_buf_ptr;
5912 input_buf_siz = save_input_buf_siz;
5919 * tep_parse_header_page - parse the data stored in the header page
5920 * @pevent: the handle to the pevent
5921 * @buf: the buffer storing the header page format string
5922 * @size: the size of @buf
5923 * @long_size: the long size to use if there is no header
5925 * This parses the header page format for information on the
5926 * ring buffer used. The @buf should be copied from
5928 * /sys/kernel/debug/tracing/events/header_page
5930 int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
5937 * Old kernels did not have header page info.
5938 * Sorry but we just use what we find here in user space.
5940 pevent->header_page_ts_size = sizeof(long long);
5941 pevent->header_page_size_size = long_size;
5942 pevent->header_page_data_offset = sizeof(long long) + long_size;
5943 pevent->old_format = 1;
5946 init_input_buf(buf, size);
5948 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5949 &pevent->header_page_ts_size, 1);
5950 parse_header_field("commit", &pevent->header_page_size_offset,
5951 &pevent->header_page_size_size, 1);
5952 parse_header_field("overwrite", &pevent->header_page_overwrite,
5954 parse_header_field("data", &pevent->header_page_data_offset,
5955 &pevent->header_page_data_size, 1);
5960 static int event_matches(struct event_format *event,
5961 int id, const char *sys_name,
5962 const char *event_name)
5964 if (id >= 0 && id != event->id)
5967 if (event_name && (strcmp(event_name, event->name) != 0))
5970 if (sys_name && (strcmp(sys_name, event->system) != 0))
5976 static void free_handler(struct event_handler *handle)
5978 free((void *)handle->sys_name);
5979 free((void *)handle->event_name);
5983 static int find_event_handle(struct tep_handle *pevent, struct event_format *event)
5985 struct event_handler *handle, **next;
5987 for (next = &pevent->handlers; *next;
5988 next = &(*next)->next) {
5990 if (event_matches(event, handle->id,
5992 handle->event_name))
5999 pr_stat("overriding event (%d) %s:%s with new print handler",
6000 event->id, event->system, event->name);
6002 event->handler = handle->func;
6003 event->context = handle->context;
6005 *next = handle->next;
6006 free_handler(handle);
6012 * __tep_parse_format - parse the event format
6013 * @buf: the buffer storing the event format string
6014 * @size: the size of @buf
6015 * @sys: the system the event belongs to
6017 * This parses the event format and creates an event structure
6018 * to quickly parse raw data for a given event.
6020 * These files currently come from:
6022 * /sys/kernel/debug/tracing/events/.../.../format
6024 enum tep_errno __tep_parse_format(struct event_format **eventp,
6025 struct tep_handle *pevent, const char *buf,
6026 unsigned long size, const char *sys)
6028 struct event_format *event;
6031 init_input_buf(buf, size);
6033 *eventp = event = alloc_event();
6035 return TEP_ERRNO__MEM_ALLOC_FAILED;
6037 event->name = event_read_name();
6040 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6041 goto event_alloc_failed;
6044 if (strcmp(sys, "ftrace") == 0) {
6045 event->flags |= EVENT_FL_ISFTRACE;
6047 if (strcmp(event->name, "bprint") == 0)
6048 event->flags |= EVENT_FL_ISBPRINT;
6051 event->id = event_read_id();
6052 if (event->id < 0) {
6053 ret = TEP_ERRNO__READ_ID_FAILED;
6055 * This isn't an allocation error actually.
6056 * But as the ID is critical, just bail out.
6058 goto event_alloc_failed;
6061 event->system = strdup(sys);
6062 if (!event->system) {
6063 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6064 goto event_alloc_failed;
6067 /* Add pevent to event so that it can be referenced */
6068 event->pevent = pevent;
6070 ret = event_read_format(event);
6072 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6073 goto event_parse_failed;
6077 * If the event has an override, don't print warnings if the event
6078 * print format fails to parse.
6080 if (pevent && find_event_handle(pevent, event))
6083 ret = event_read_print(event);
6087 ret = TEP_ERRNO__READ_PRINT_FAILED;
6088 goto event_parse_failed;
6091 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6092 struct format_field *field;
6093 struct print_arg *arg, **list;
6095 /* old ftrace had no args */
6096 list = &event->print_fmt.args;
6097 for (field = event->format.fields; field; field = field->next) {
6100 event->flags |= EVENT_FL_FAILED;
6101 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6103 arg->type = PRINT_FIELD;
6104 arg->field.name = strdup(field->name);
6105 if (!arg->field.name) {
6106 event->flags |= EVENT_FL_FAILED;
6108 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6110 arg->field.field = field;
6120 event->flags |= EVENT_FL_FAILED;
6124 free(event->system);
6131 static enum tep_errno
6132 __parse_event(struct tep_handle *pevent,
6133 struct event_format **eventp,
6134 const char *buf, unsigned long size,
6137 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
6138 struct event_format *event = *eventp;
6143 if (pevent && add_event(pevent, event)) {
6144 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6145 goto event_add_failed;
6148 #define PRINT_ARGS 0
6149 if (PRINT_ARGS && event->print_fmt.args)
6150 print_args(event->print_fmt.args);
6155 tep_free_format(event);
6160 * tep_parse_format - parse the event format
6161 * @pevent: the handle to the pevent
6162 * @eventp: returned format
6163 * @buf: the buffer storing the event format string
6164 * @size: the size of @buf
6165 * @sys: the system the event belongs to
6167 * This parses the event format and creates an event structure
6168 * to quickly parse raw data for a given event.
6170 * These files currently come from:
6172 * /sys/kernel/debug/tracing/events/.../.../format
6174 enum tep_errno tep_parse_format(struct tep_handle *pevent,
6175 struct event_format **eventp,
6177 unsigned long size, const char *sys)
6179 return __parse_event(pevent, eventp, buf, size, sys);
6183 * tep_parse_event - parse the event format
6184 * @pevent: the handle to the pevent
6185 * @buf: the buffer storing the event format string
6186 * @size: the size of @buf
6187 * @sys: the system the event belongs to
6189 * This parses the event format and creates an event structure
6190 * to quickly parse raw data for a given event.
6192 * These files currently come from:
6194 * /sys/kernel/debug/tracing/events/.../.../format
6196 enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
6197 unsigned long size, const char *sys)
6199 struct event_format *event = NULL;
6200 return __parse_event(pevent, &event, buf, size, sys);
6204 #define _PE(code, str) str
6205 static const char * const tep_error_str[] = {
6210 int tep_strerror(struct tep_handle *pevent __maybe_unused,
6211 enum tep_errno errnum, char *buf, size_t buflen)
6217 str_error_r(errnum, buf, buflen);
6221 if (errnum <= __TEP_ERRNO__START ||
6222 errnum >= __TEP_ERRNO__END)
6225 idx = errnum - __TEP_ERRNO__START - 1;
6226 msg = tep_error_str[idx];
6227 snprintf(buf, buflen, "%s", msg);
6232 int get_field_val(struct trace_seq *s, struct format_field *field,
6233 const char *name, struct tep_record *record,
6234 unsigned long long *val, int err)
6238 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6242 if (tep_read_number_field(field, record->data, val)) {
6244 trace_seq_printf(s, " %s=INVALID", name);
6252 * tep_get_field_raw - return the raw pointer into the data field
6253 * @s: The seq to print to on error
6254 * @event: the event that the field is for
6255 * @name: The name of the field
6256 * @record: The record with the field name.
6257 * @len: place to store the field length.
6258 * @err: print default error if failed.
6260 * Returns a pointer into record->data of the field and places
6261 * the length of the field in @len.
6263 * On failure, it returns NULL.
6265 void *tep_get_field_raw(struct trace_seq *s, struct event_format *event,
6266 const char *name, struct tep_record *record,
6269 struct format_field *field;
6270 void *data = record->data;
6277 field = tep_find_field(event, name);
6281 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6285 /* Allow @len to be NULL */
6289 offset = field->offset;
6290 if (field->flags & FIELD_IS_DYNAMIC) {
6291 offset = tep_read_number(event->pevent,
6292 data + offset, field->size);
6293 *len = offset >> 16;
6298 return data + offset;
6302 * tep_get_field_val - find a field and return its value
6303 * @s: The seq to print to on error
6304 * @event: the event that the field is for
6305 * @name: The name of the field
6306 * @record: The record with the field name.
6307 * @val: place to store the value of the field.
6308 * @err: print default error if failed.
6310 * Returns 0 on success -1 on field not found.
6312 int tep_get_field_val(struct trace_seq *s, struct event_format *event,
6313 const char *name, struct tep_record *record,
6314 unsigned long long *val, int err)
6316 struct format_field *field;
6321 field = tep_find_field(event, name);
6323 return get_field_val(s, field, name, record, val, err);
6327 * tep_get_common_field_val - find a common field and return its value
6328 * @s: The seq to print to on error
6329 * @event: the event that the field is for
6330 * @name: The name of the field
6331 * @record: The record with the field name.
6332 * @val: place to store the value of the field.
6333 * @err: print default error if failed.
6335 * Returns 0 on success -1 on field not found.
6337 int tep_get_common_field_val(struct trace_seq *s, struct event_format *event,
6338 const char *name, struct tep_record *record,
6339 unsigned long long *val, int err)
6341 struct format_field *field;
6346 field = tep_find_common_field(event, name);
6348 return get_field_val(s, field, name, record, val, err);
6352 * tep_get_any_field_val - find a any field and return its value
6353 * @s: The seq to print to on error
6354 * @event: the event that the field is for
6355 * @name: The name of the field
6356 * @record: The record with the field name.
6357 * @val: place to store the value of the field.
6358 * @err: print default error if failed.
6360 * Returns 0 on success -1 on field not found.
6362 int tep_get_any_field_val(struct trace_seq *s, struct event_format *event,
6363 const char *name, struct tep_record *record,
6364 unsigned long long *val, int err)
6366 struct format_field *field;
6371 field = tep_find_any_field(event, name);
6373 return get_field_val(s, field, name, record, val, err);
6377 * tep_print_num_field - print a field and a format
6378 * @s: The seq to print to
6379 * @fmt: The printf format to print the field with.
6380 * @event: the event that the field is for
6381 * @name: The name of the field
6382 * @record: The record with the field name.
6383 * @err: print default error if failed.
6385 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6387 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6388 struct event_format *event, const char *name,
6389 struct tep_record *record, int err)
6391 struct format_field *field = tep_find_field(event, name);
6392 unsigned long long val;
6397 if (tep_read_number_field(field, record->data, &val))
6400 return trace_seq_printf(s, fmt, val);
6404 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6409 * tep_print_func_field - print a field and a format for function pointers
6410 * @s: The seq to print to
6411 * @fmt: The printf format to print the field with.
6412 * @event: the event that the field is for
6413 * @name: The name of the field
6414 * @record: The record with the field name.
6415 * @err: print default error if failed.
6417 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6419 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6420 struct event_format *event, const char *name,
6421 struct tep_record *record, int err)
6423 struct format_field *field = tep_find_field(event, name);
6424 struct tep_handle *pevent = event->pevent;
6425 unsigned long long val;
6426 struct func_map *func;
6432 if (tep_read_number_field(field, record->data, &val))
6435 func = find_func(pevent, val);
6438 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6440 sprintf(tmp, "0x%08llx", val);
6442 return trace_seq_printf(s, fmt, tmp);
6446 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6450 static void free_func_handle(struct tep_function_handler *func)
6452 struct func_params *params;
6456 while (func->params) {
6457 params = func->params;
6458 func->params = params->next;
6466 * tep_register_print_function - register a helper function
6467 * @pevent: the handle to the pevent
6468 * @func: the function to process the helper function
6469 * @ret_type: the return type of the helper function
6470 * @name: the name of the helper function
6471 * @parameters: A list of enum tep_func_arg_type
6473 * Some events may have helper functions in the print format arguments.
6474 * This allows a plugin to dynamically create a way to process one
6475 * of these functions.
6477 * The @parameters is a variable list of tep_func_arg_type enums that
6478 * must end with TEP_FUNC_ARG_VOID.
6480 int tep_register_print_function(struct tep_handle *pevent,
6481 tep_func_handler func,
6482 enum tep_func_arg_type ret_type,
6485 struct tep_function_handler *func_handle;
6486 struct func_params **next_param;
6487 struct func_params *param;
6488 enum tep_func_arg_type type;
6492 func_handle = find_func_handler(pevent, name);
6495 * This is most like caused by the users own
6496 * plugins updating the function. This overrides the
6499 pr_stat("override of function helper '%s'", name);
6500 remove_func_handler(pevent, name);
6503 func_handle = calloc(1, sizeof(*func_handle));
6505 do_warning("Failed to allocate function handler");
6506 return TEP_ERRNO__MEM_ALLOC_FAILED;
6509 func_handle->ret_type = ret_type;
6510 func_handle->name = strdup(name);
6511 func_handle->func = func;
6512 if (!func_handle->name) {
6513 do_warning("Failed to allocate function name");
6515 return TEP_ERRNO__MEM_ALLOC_FAILED;
6518 next_param = &(func_handle->params);
6521 type = va_arg(ap, enum tep_func_arg_type);
6522 if (type == TEP_FUNC_ARG_VOID)
6525 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6526 do_warning("Invalid argument type %d", type);
6527 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6531 param = malloc(sizeof(*param));
6533 do_warning("Failed to allocate function param");
6534 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6540 *next_param = param;
6541 next_param = &(param->next);
6543 func_handle->nr_args++;
6547 func_handle->next = pevent->func_handlers;
6548 pevent->func_handlers = func_handle;
6553 free_func_handle(func_handle);
6558 * tep_unregister_print_function - unregister a helper function
6559 * @pevent: the handle to the pevent
6560 * @func: the function to process the helper function
6561 * @name: the name of the helper function
6563 * This function removes existing print handler for function @name.
6565 * Returns 0 if the handler was removed successully, -1 otherwise.
6567 int tep_unregister_print_function(struct tep_handle *pevent,
6568 tep_func_handler func, char *name)
6570 struct tep_function_handler *func_handle;
6572 func_handle = find_func_handler(pevent, name);
6573 if (func_handle && func_handle->func == func) {
6574 remove_func_handler(pevent, name);
6580 static struct event_format *search_event(struct tep_handle *pevent, int id,
6581 const char *sys_name,
6582 const char *event_name)
6584 struct event_format *event;
6588 event = tep_find_event(pevent, id);
6591 if (event_name && (strcmp(event_name, event->name) != 0))
6593 if (sys_name && (strcmp(sys_name, event->system) != 0))
6596 event = tep_find_event_by_name(pevent, sys_name, event_name);
6604 * tep_register_event_handler - register a way to parse an event
6605 * @pevent: the handle to the pevent
6606 * @id: the id of the event to register
6607 * @sys_name: the system name the event belongs to
6608 * @event_name: the name of the event
6609 * @func: the function to call to parse the event information
6610 * @context: the data to be passed to @func
6612 * This function allows a developer to override the parsing of
6613 * a given event. If for some reason the default print format
6614 * is not sufficient, this function will register a function
6615 * for an event to be used to parse the data instead.
6617 * If @id is >= 0, then it is used to find the event.
6618 * else @sys_name and @event_name are used.
6620 int tep_register_event_handler(struct tep_handle *pevent, int id,
6621 const char *sys_name, const char *event_name,
6622 tep_event_handler_func func, void *context)
6624 struct event_format *event;
6625 struct event_handler *handle;
6627 event = search_event(pevent, id, sys_name, event_name);
6631 pr_stat("overriding event (%d) %s:%s with new print handler",
6632 event->id, event->system, event->name);
6634 event->handler = func;
6635 event->context = context;
6639 /* Save for later use. */
6640 handle = calloc(1, sizeof(*handle));
6642 do_warning("Failed to allocate event handler");
6643 return TEP_ERRNO__MEM_ALLOC_FAILED;
6648 handle->event_name = strdup(event_name);
6650 handle->sys_name = strdup(sys_name);
6652 if ((event_name && !handle->event_name) ||
6653 (sys_name && !handle->sys_name)) {
6654 do_warning("Failed to allocate event/sys name");
6655 free((void *)handle->event_name);
6656 free((void *)handle->sys_name);
6658 return TEP_ERRNO__MEM_ALLOC_FAILED;
6661 handle->func = func;
6662 handle->next = pevent->handlers;
6663 pevent->handlers = handle;
6664 handle->context = context;
6669 static int handle_matches(struct event_handler *handler, int id,
6670 const char *sys_name, const char *event_name,
6671 tep_event_handler_func func, void *context)
6673 if (id >= 0 && id != handler->id)
6676 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6679 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6682 if (func != handler->func || context != handler->context)
6689 * tep_unregister_event_handler - unregister an existing event handler
6690 * @pevent: the handle to the pevent
6691 * @id: the id of the event to unregister
6692 * @sys_name: the system name the handler belongs to
6693 * @event_name: the name of the event handler
6694 * @func: the function to call to parse the event information
6695 * @context: the data to be passed to @func
6697 * This function removes existing event handler (parser).
6699 * If @id is >= 0, then it is used to find the event.
6700 * else @sys_name and @event_name are used.
6702 * Returns 0 if handler was removed successfully, -1 if event was not found.
6704 int tep_unregister_event_handler(struct tep_handle *pevent, int id,
6705 const char *sys_name, const char *event_name,
6706 tep_event_handler_func func, void *context)
6708 struct event_format *event;
6709 struct event_handler *handle;
6710 struct event_handler **next;
6712 event = search_event(pevent, id, sys_name, event_name);
6716 if (event->handler == func && event->context == context) {
6717 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6718 event->id, event->system, event->name);
6720 event->handler = NULL;
6721 event->context = NULL;
6726 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6728 if (handle_matches(handle, id, sys_name, event_name,
6736 *next = handle->next;
6737 free_handler(handle);
6743 * tep_alloc - create a pevent handle
6745 struct tep_handle *tep_alloc(void)
6747 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
6750 pevent->ref_count = 1;
6755 void tep_ref(struct tep_handle *pevent)
6757 pevent->ref_count++;
6760 void tep_free_format_field(struct format_field *field)
6763 if (field->alias != field->name)
6769 static void free_format_fields(struct format_field *field)
6771 struct format_field *next;
6775 tep_free_format_field(field);
6780 static void free_formats(struct format *format)
6782 free_format_fields(format->common_fields);
6783 free_format_fields(format->fields);
6786 void tep_free_format(struct event_format *event)
6789 free(event->system);
6791 free_formats(&event->format);
6793 free(event->print_fmt.format);
6794 free_args(event->print_fmt.args);
6800 * tep_free - free a pevent handle
6801 * @pevent: the pevent handle to free
6803 void tep_free(struct tep_handle *pevent)
6805 struct cmdline_list *cmdlist, *cmdnext;
6806 struct func_list *funclist, *funcnext;
6807 struct printk_list *printklist, *printknext;
6808 struct tep_function_handler *func_handler;
6809 struct event_handler *handle;
6815 cmdlist = pevent->cmdlist;
6816 funclist = pevent->funclist;
6817 printklist = pevent->printklist;
6819 pevent->ref_count--;
6820 if (pevent->ref_count)
6823 if (pevent->cmdlines) {
6824 for (i = 0; i < pevent->cmdline_count; i++)
6825 free(pevent->cmdlines[i].comm);
6826 free(pevent->cmdlines);
6830 cmdnext = cmdlist->next;
6831 free(cmdlist->comm);
6836 if (pevent->func_map) {
6837 for (i = 0; i < (int)pevent->func_count; i++) {
6838 free(pevent->func_map[i].func);
6839 free(pevent->func_map[i].mod);
6841 free(pevent->func_map);
6845 funcnext = funclist->next;
6846 free(funclist->func);
6847 free(funclist->mod);
6849 funclist = funcnext;
6852 while (pevent->func_handlers) {
6853 func_handler = pevent->func_handlers;
6854 pevent->func_handlers = func_handler->next;
6855 free_func_handle(func_handler);
6858 if (pevent->printk_map) {
6859 for (i = 0; i < (int)pevent->printk_count; i++)
6860 free(pevent->printk_map[i].printk);
6861 free(pevent->printk_map);
6864 while (printklist) {
6865 printknext = printklist->next;
6866 free(printklist->printk);
6868 printklist = printknext;
6871 for (i = 0; i < pevent->nr_events; i++)
6872 tep_free_format(pevent->events[i]);
6874 while (pevent->handlers) {
6875 handle = pevent->handlers;
6876 pevent->handlers = handle->next;
6877 free_handler(handle);
6880 free(pevent->trace_clock);
6881 free(pevent->events);
6882 free(pevent->sort_events);
6883 free(pevent->func_resolver);
6888 void tep_unref(struct tep_handle *pevent)