1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
21 #include <linux/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));
257 pevent->cmdlines = cmdlines;
259 cmdlines[pevent->cmdline_count].comm = strdup(comm);
260 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);
276 * tep_register_comm - register a pid / comm mapping
277 * @pevent: handle for the pevent
278 * @comm: the command line to register
279 * @pid: the pid to map the command line to
281 * This adds a mapping to search for command line names with
282 * a given pid. The comm is duplicated.
284 int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
286 struct cmdline_list *item;
288 if (pevent->cmdlines)
289 return add_new_comm(pevent, comm, pid);
291 item = malloc(sizeof(*item));
296 item->comm = strdup(comm);
298 item->comm = strdup("<...>");
304 item->next = pevent->cmdlist;
306 pevent->cmdlist = item;
307 pevent->cmdline_count++;
312 int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
314 pevent->trace_clock = strdup(trace_clock);
315 if (!pevent->trace_clock) {
323 unsigned long long addr;
329 struct func_list *next;
330 unsigned long long addr;
335 static int func_cmp(const void *a, const void *b)
337 const struct func_map *fa = a;
338 const struct func_map *fb = b;
340 if (fa->addr < fb->addr)
342 if (fa->addr > fb->addr)
349 * We are searching for a record in between, not an exact
352 static int func_bcmp(const void *a, const void *b)
354 const struct func_map *fa = a;
355 const struct func_map *fb = b;
357 if ((fa->addr == fb->addr) ||
359 (fa->addr > fb->addr &&
360 fa->addr < (fb+1)->addr))
363 if (fa->addr < fb->addr)
369 static int func_map_init(struct tep_handle *pevent)
371 struct func_list *funclist;
372 struct func_list *item;
373 struct func_map *func_map;
376 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
380 funclist = pevent->funclist;
384 func_map[i].func = funclist->func;
385 func_map[i].addr = funclist->addr;
386 func_map[i].mod = funclist->mod;
389 funclist = funclist->next;
393 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
396 * Add a special record at the end.
398 func_map[pevent->func_count].func = NULL;
399 func_map[pevent->func_count].addr = 0;
400 func_map[pevent->func_count].mod = NULL;
402 pevent->func_map = func_map;
403 pevent->funclist = NULL;
408 static struct func_map *
409 __find_func(struct tep_handle *pevent, unsigned long long addr)
411 struct func_map *func;
414 if (!pevent->func_map)
415 func_map_init(pevent);
419 func = bsearch(&key, pevent->func_map, pevent->func_count,
420 sizeof(*pevent->func_map), func_bcmp);
425 struct func_resolver {
426 tep_func_resolver_t *func;
432 * tep_set_function_resolver - set an alternative function resolver
433 * @pevent: handle for the pevent
434 * @resolver: function to be used
435 * @priv: resolver function private state.
437 * Some tools may have already a way to resolve kernel functions, allow them to
438 * keep using it instead of duplicating all the entries inside
441 int tep_set_function_resolver(struct tep_handle *pevent,
442 tep_func_resolver_t *func, void *priv)
444 struct func_resolver *resolver = malloc(sizeof(*resolver));
446 if (resolver == NULL)
449 resolver->func = func;
450 resolver->priv = priv;
452 free(pevent->func_resolver);
453 pevent->func_resolver = resolver;
459 * tep_reset_function_resolver - reset alternative function resolver
460 * @pevent: handle for the pevent
462 * Stop using whatever alternative resolver was set, use the default
465 void tep_reset_function_resolver(struct tep_handle *pevent)
467 free(pevent->func_resolver);
468 pevent->func_resolver = NULL;
471 static struct func_map *
472 find_func(struct tep_handle *pevent, unsigned long long addr)
474 struct func_map *map;
476 if (!pevent->func_resolver)
477 return __find_func(pevent, addr);
479 map = &pevent->func_resolver->map;
482 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
483 &map->addr, &map->mod);
484 if (map->func == NULL)
491 * tep_find_function - find a function by a given address
492 * @pevent: handle for the pevent
493 * @addr: the address to find the function with
495 * Returns a pointer to the function stored that has the given
496 * address. Note, the address does not have to be exact, it
497 * will select the function that would contain the address.
499 const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
501 struct func_map *map;
503 map = find_func(pevent, addr);
511 * tep_find_function_address - find a function address by a given address
512 * @pevent: handle for the pevent
513 * @addr: the address to find the function with
515 * Returns the address the function starts at. This can be used in
516 * conjunction with tep_find_function to print both the function
517 * name and the function offset.
520 tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
522 struct func_map *map;
524 map = find_func(pevent, addr);
532 * tep_register_function - register a function with a given address
533 * @pevent: handle for the pevent
534 * @function: the function name to register
535 * @addr: the address the function starts at
536 * @mod: the kernel module the function may be in (NULL for none)
538 * This registers a function name with an address and module.
539 * The @func passed in is duplicated.
541 int tep_register_function(struct tep_handle *pevent, char *func,
542 unsigned long long addr, char *mod)
544 struct func_list *item = malloc(sizeof(*item));
549 item->next = pevent->funclist;
550 item->func = strdup(func);
555 item->mod = strdup(mod);
562 pevent->funclist = item;
563 pevent->func_count++;
577 * tep_print_funcs - print out the stored functions
578 * @pevent: handle for the pevent
580 * This prints out the stored functions.
582 void tep_print_funcs(struct tep_handle *pevent)
586 if (!pevent->func_map)
587 func_map_init(pevent);
589 for (i = 0; i < (int)pevent->func_count; i++) {
591 pevent->func_map[i].addr,
592 pevent->func_map[i].func);
593 if (pevent->func_map[i].mod)
594 printf(" [%s]\n", pevent->func_map[i].mod);
601 unsigned long long addr;
606 struct printk_list *next;
607 unsigned long long addr;
611 static int printk_cmp(const void *a, const void *b)
613 const struct printk_map *pa = a;
614 const struct printk_map *pb = b;
616 if (pa->addr < pb->addr)
618 if (pa->addr > pb->addr)
624 static int printk_map_init(struct tep_handle *pevent)
626 struct printk_list *printklist;
627 struct printk_list *item;
628 struct printk_map *printk_map;
631 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
635 printklist = pevent->printklist;
639 printk_map[i].printk = printklist->printk;
640 printk_map[i].addr = printklist->addr;
643 printklist = printklist->next;
647 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
649 pevent->printk_map = printk_map;
650 pevent->printklist = NULL;
655 static struct printk_map *
656 find_printk(struct tep_handle *pevent, unsigned long long addr)
658 struct printk_map *printk;
659 struct printk_map key;
661 if (!pevent->printk_map && printk_map_init(pevent))
666 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
667 sizeof(*pevent->printk_map), printk_cmp);
673 * tep_register_print_string - register a string by its address
674 * @pevent: handle for the pevent
675 * @fmt: the string format to register
676 * @addr: the address the string was located at
678 * This registers a string by the address it was stored in the kernel.
679 * The @fmt passed in is duplicated.
681 int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
682 unsigned long long addr)
684 struct printk_list *item = malloc(sizeof(*item));
690 item->next = pevent->printklist;
693 /* Strip off quotes and '\n' from the end */
696 item->printk = strdup(fmt);
700 p = item->printk + strlen(item->printk) - 1;
705 if (strcmp(p, "\\n") == 0)
708 pevent->printklist = item;
709 pevent->printk_count++;
720 * tep_print_printk - print out the stored strings
721 * @pevent: handle for the pevent
723 * This prints the string formats that were stored.
725 void tep_print_printk(struct tep_handle *pevent)
729 if (!pevent->printk_map)
730 printk_map_init(pevent);
732 for (i = 0; i < (int)pevent->printk_count; i++) {
733 printf("%016llx %s\n",
734 pevent->printk_map[i].addr,
735 pevent->printk_map[i].printk);
739 static struct event_format *alloc_event(void)
741 return calloc(1, sizeof(struct event_format));
744 static int add_event(struct tep_handle *pevent, struct event_format *event)
747 struct event_format **events = realloc(pevent->events, sizeof(event) *
748 (pevent->nr_events + 1));
752 pevent->events = events;
754 for (i = 0; i < pevent->nr_events; i++) {
755 if (pevent->events[i]->id > event->id)
758 if (i < pevent->nr_events)
759 memmove(&pevent->events[i + 1],
761 sizeof(event) * (pevent->nr_events - i));
763 pevent->events[i] = event;
766 event->pevent = pevent;
771 static int event_item_type(enum event_type type)
774 case EVENT_ITEM ... EVENT_SQUOTE:
776 case EVENT_ERROR ... EVENT_DELIM:
782 static void free_flag_sym(struct print_flag_sym *fsym)
784 struct print_flag_sym *next;
795 static void free_arg(struct print_arg *arg)
797 struct print_arg *farg;
804 free(arg->atom.atom);
807 free(arg->field.name);
810 free_arg(arg->flags.field);
811 free(arg->flags.delim);
812 free_flag_sym(arg->flags.flags);
815 free_arg(arg->symbol.field);
816 free_flag_sym(arg->symbol.symbols);
820 free_arg(arg->hex.field);
821 free_arg(arg->hex.size);
823 case PRINT_INT_ARRAY:
824 free_arg(arg->int_array.field);
825 free_arg(arg->int_array.count);
826 free_arg(arg->int_array.el_size);
829 free(arg->typecast.type);
830 free_arg(arg->typecast.item);
834 free(arg->string.string);
837 free(arg->bitmask.bitmask);
839 case PRINT_DYNAMIC_ARRAY:
840 case PRINT_DYNAMIC_ARRAY_LEN:
841 free(arg->dynarray.index);
845 free_arg(arg->op.left);
846 free_arg(arg->op.right);
849 while (arg->func.args) {
850 farg = arg->func.args;
851 arg->func.args = farg->next;
864 static enum event_type get_type(int ch)
867 return EVENT_NEWLINE;
870 if (isalnum(ch) || ch == '_')
878 if (ch == '(' || ch == ')' || ch == ',')
884 static int __read_char(void)
886 if (input_buf_ptr >= input_buf_siz)
889 return input_buf[input_buf_ptr++];
892 static int __peek_char(void)
894 if (input_buf_ptr >= input_buf_siz)
897 return input_buf[input_buf_ptr];
901 * tep_peek_char - peek at the next character that will be read
903 * Returns the next character read, or -1 if end of buffer.
905 int tep_peek_char(void)
907 return __peek_char();
910 static int extend_token(char **tok, char *buf, int size)
912 char *newtok = realloc(*tok, size);
929 static enum event_type force_token(const char *str, char **tok);
931 static enum event_type __read_token(char **tok)
934 int ch, last_ch, quote_ch, next_ch;
937 enum event_type type;
947 if (type == EVENT_NONE)
955 if (asprintf(tok, "%c", ch) < 0)
963 next_ch = __peek_char();
964 if (next_ch == '>') {
965 buf[i++] = __read_char();
978 buf[i++] = __read_char();
990 default: /* what should we do instead? */
1000 buf[i++] = __read_char();
1005 /* don't keep quotes */
1011 if (i == (BUFSIZ - 1)) {
1015 if (extend_token(tok, buf, tok_size) < 0)
1022 /* the '\' '\' will cancel itself */
1023 if (ch == '\\' && last_ch == '\\')
1025 } while (ch != quote_ch || last_ch == '\\');
1026 /* remove the last quote */
1030 * For strings (double quotes) check the next token.
1031 * If it is another string, concatinate the two.
1033 if (type == EVENT_DQUOTE) {
1034 unsigned long long save_input_buf_ptr = input_buf_ptr;
1038 } while (isspace(ch));
1041 input_buf_ptr = save_input_buf_ptr;
1046 case EVENT_ERROR ... EVENT_SPACE:
1052 while (get_type(__peek_char()) == type) {
1053 if (i == (BUFSIZ - 1)) {
1057 if (extend_token(tok, buf, tok_size) < 0)
1067 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1070 if (type == EVENT_ITEM) {
1072 * Older versions of the kernel has a bug that
1073 * creates invalid symbols and will break the mac80211
1074 * parsing. This is a work around to that bug.
1076 * See Linux kernel commit:
1077 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1079 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1082 return force_token("\"%s\" ", tok);
1083 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1086 return force_token("\" sta:%pM\" ", tok);
1087 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1090 return force_token("\" vif:%p(%d)\" ", tok);
1097 static enum event_type force_token(const char *str, char **tok)
1099 const char *save_input_buf;
1100 unsigned long long save_input_buf_ptr;
1101 unsigned long long save_input_buf_siz;
1102 enum event_type type;
1104 /* save off the current input pointers */
1105 save_input_buf = input_buf;
1106 save_input_buf_ptr = input_buf_ptr;
1107 save_input_buf_siz = input_buf_siz;
1109 init_input_buf(str, strlen(str));
1111 type = __read_token(tok);
1113 /* reset back to original token */
1114 input_buf = save_input_buf;
1115 input_buf_ptr = save_input_buf_ptr;
1116 input_buf_siz = save_input_buf_siz;
1121 static void free_token(char *tok)
1127 static enum event_type read_token(char **tok)
1129 enum event_type type;
1132 type = __read_token(tok);
1133 if (type != EVENT_SPACE)
1145 * tep_read_token - access to utilites to use the pevent parser
1146 * @tok: The token to return
1148 * This will parse tokens from the string given by
1151 * Returns the token type.
1153 enum event_type tep_read_token(char **tok)
1155 return read_token(tok);
1159 * tep_free_token - free a token returned by tep_read_token
1160 * @token: the token to free
1162 void tep_free_token(char *token)
1168 static enum event_type read_token_item(char **tok)
1170 enum event_type type;
1173 type = __read_token(tok);
1174 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1185 static int test_type(enum event_type type, enum event_type expect)
1187 if (type != expect) {
1188 do_warning("Error: expected type %d but read %d",
1195 static int test_type_token(enum event_type type, const char *token,
1196 enum event_type expect, const char *expect_tok)
1198 if (type != expect) {
1199 do_warning("Error: expected type %d but read %d",
1204 if (strcmp(token, expect_tok) != 0) {
1205 do_warning("Error: expected '%s' but read '%s'",
1212 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1214 enum event_type type;
1217 type = read_token(tok);
1219 type = read_token_item(tok);
1220 return test_type(type, expect);
1223 static int read_expect_type(enum event_type expect, char **tok)
1225 return __read_expect_type(expect, tok, 1);
1228 static int __read_expected(enum event_type expect, const char *str,
1231 enum event_type type;
1236 type = read_token(&token);
1238 type = read_token_item(&token);
1240 ret = test_type_token(type, token, expect, str);
1247 static int read_expected(enum event_type expect, const char *str)
1249 return __read_expected(expect, str, 1);
1252 static int read_expected_item(enum event_type expect, const char *str)
1254 return __read_expected(expect, str, 0);
1257 static char *event_read_name(void)
1261 if (read_expected(EVENT_ITEM, "name") < 0)
1264 if (read_expected(EVENT_OP, ":") < 0)
1267 if (read_expect_type(EVENT_ITEM, &token) < 0)
1277 static int event_read_id(void)
1282 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1285 if (read_expected(EVENT_OP, ":") < 0)
1288 if (read_expect_type(EVENT_ITEM, &token) < 0)
1291 id = strtoul(token, NULL, 0);
1300 static int field_is_string(struct format_field *field)
1302 if ((field->flags & FIELD_IS_ARRAY) &&
1303 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1304 strstr(field->type, "s8")))
1310 static int field_is_dynamic(struct format_field *field)
1312 if (strncmp(field->type, "__data_loc", 10) == 0)
1318 static int field_is_long(struct format_field *field)
1320 /* includes long long */
1321 if (strstr(field->type, "long"))
1327 static unsigned int type_size(const char *name)
1329 /* This covers all FIELD_IS_STRING types. */
1347 for (i = 0; table[i].type; i++) {
1348 if (!strcmp(table[i].type, name))
1349 return table[i].size;
1355 static int event_read_fields(struct event_format *event, struct format_field **fields)
1357 struct format_field *field = NULL;
1358 enum event_type type;
1364 unsigned int size_dynamic = 0;
1366 type = read_token(&token);
1367 if (type == EVENT_NEWLINE) {
1374 if (test_type_token(type, token, EVENT_ITEM, "field"))
1378 type = read_token(&token);
1380 * The ftrace fields may still use the "special" name.
1383 if (event->flags & EVENT_FL_ISFTRACE &&
1384 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1386 type = read_token(&token);
1389 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1393 if (read_expect_type(EVENT_ITEM, &token) < 0)
1398 field = calloc(1, sizeof(*field));
1402 field->event = event;
1404 /* read the rest of the type */
1406 type = read_token(&token);
1407 if (type == EVENT_ITEM ||
1408 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1410 * Some of the ftrace fields are broken and have
1411 * an illegal "." in them.
1413 (event->flags & EVENT_FL_ISFTRACE &&
1414 type == EVENT_OP && strcmp(token, ".") == 0)) {
1416 if (strcmp(token, "*") == 0)
1417 field->flags |= FIELD_IS_POINTER;
1421 new_type = realloc(field->type,
1422 strlen(field->type) +
1423 strlen(last_token) + 2);
1428 field->type = new_type;
1429 strcat(field->type, " ");
1430 strcat(field->type, last_token);
1433 field->type = last_token;
1442 do_warning_event(event, "%s: no type found", __func__);
1445 field->name = field->alias = last_token;
1447 if (test_type(type, EVENT_OP))
1450 if (strcmp(token, "[") == 0) {
1451 enum event_type last_type = type;
1452 char *brackets = token;
1456 field->flags |= FIELD_IS_ARRAY;
1458 type = read_token(&token);
1460 if (type == EVENT_ITEM)
1461 field->arraylen = strtoul(token, NULL, 0);
1463 field->arraylen = 0;
1465 while (strcmp(token, "]") != 0) {
1466 if (last_type == EVENT_ITEM &&
1473 new_brackets = realloc(brackets,
1475 strlen(token) + len);
1476 if (!new_brackets) {
1480 brackets = new_brackets;
1482 strcat(brackets, " ");
1483 strcat(brackets, token);
1484 /* We only care about the last token */
1485 field->arraylen = strtoul(token, NULL, 0);
1487 type = read_token(&token);
1488 if (type == EVENT_NONE) {
1489 do_warning_event(event, "failed to find token");
1496 new_brackets = realloc(brackets, strlen(brackets) + 2);
1497 if (!new_brackets) {
1501 brackets = new_brackets;
1502 strcat(brackets, "]");
1504 /* add brackets to type */
1506 type = read_token(&token);
1508 * If the next token is not an OP, then it is of
1509 * the format: type [] item;
1511 if (type == EVENT_ITEM) {
1513 new_type = realloc(field->type,
1514 strlen(field->type) +
1515 strlen(field->name) +
1516 strlen(brackets) + 2);
1521 field->type = new_type;
1522 strcat(field->type, " ");
1523 strcat(field->type, field->name);
1524 size_dynamic = type_size(field->name);
1525 free_token(field->name);
1526 strcat(field->type, brackets);
1527 field->name = field->alias = token;
1528 type = read_token(&token);
1531 new_type = realloc(field->type,
1532 strlen(field->type) +
1533 strlen(brackets) + 1);
1538 field->type = new_type;
1539 strcat(field->type, brackets);
1544 if (field_is_string(field))
1545 field->flags |= FIELD_IS_STRING;
1546 if (field_is_dynamic(field))
1547 field->flags |= FIELD_IS_DYNAMIC;
1548 if (field_is_long(field))
1549 field->flags |= FIELD_IS_LONG;
1551 if (test_type_token(type, token, EVENT_OP, ";"))
1555 if (read_expected(EVENT_ITEM, "offset") < 0)
1558 if (read_expected(EVENT_OP, ":") < 0)
1561 if (read_expect_type(EVENT_ITEM, &token))
1563 field->offset = strtoul(token, NULL, 0);
1566 if (read_expected(EVENT_OP, ";") < 0)
1569 if (read_expected(EVENT_ITEM, "size") < 0)
1572 if (read_expected(EVENT_OP, ":") < 0)
1575 if (read_expect_type(EVENT_ITEM, &token))
1577 field->size = strtoul(token, NULL, 0);
1580 if (read_expected(EVENT_OP, ";") < 0)
1583 type = read_token(&token);
1584 if (type != EVENT_NEWLINE) {
1585 /* newer versions of the kernel have a "signed" type */
1586 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1591 if (read_expected(EVENT_OP, ":") < 0)
1594 if (read_expect_type(EVENT_ITEM, &token))
1597 if (strtoul(token, NULL, 0))
1598 field->flags |= FIELD_IS_SIGNED;
1601 if (read_expected(EVENT_OP, ";") < 0)
1604 if (read_expect_type(EVENT_NEWLINE, &token))
1610 if (field->flags & FIELD_IS_ARRAY) {
1611 if (field->arraylen)
1612 field->elementsize = field->size / field->arraylen;
1613 else if (field->flags & FIELD_IS_DYNAMIC)
1614 field->elementsize = size_dynamic;
1615 else if (field->flags & FIELD_IS_STRING)
1616 field->elementsize = 1;
1617 else if (field->flags & FIELD_IS_LONG)
1618 field->elementsize = event->pevent ?
1619 event->pevent->long_size :
1622 field->elementsize = field->size;
1625 fields = &field->next;
1642 static int event_read_format(struct event_format *event)
1647 if (read_expected_item(EVENT_ITEM, "format") < 0)
1650 if (read_expected(EVENT_OP, ":") < 0)
1653 if (read_expect_type(EVENT_NEWLINE, &token))
1657 ret = event_read_fields(event, &event->format.common_fields);
1660 event->format.nr_common = ret;
1662 ret = event_read_fields(event, &event->format.fields);
1665 event->format.nr_fields = ret;
1674 static enum event_type
1675 process_arg_token(struct event_format *event, struct print_arg *arg,
1676 char **tok, enum event_type type);
1678 static enum event_type
1679 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1681 enum event_type type;
1684 type = read_token(&token);
1687 return process_arg_token(event, arg, tok, type);
1690 static enum event_type
1691 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1694 * For __print_symbolic() and __print_flags, we need to completely
1695 * evaluate the first argument, which defines what to print next.
1697 static enum event_type
1698 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1700 enum event_type type;
1702 type = process_arg(event, arg, tok);
1704 while (type == EVENT_OP) {
1705 type = process_op(event, arg, tok);
1711 static enum event_type
1712 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1714 struct print_arg *arg, *left, *right;
1715 enum event_type type;
1720 right = alloc_arg();
1722 if (!arg || !left || !right) {
1723 do_warning_event(event, "%s: not enough memory!", __func__);
1724 /* arg will be freed at out_free */
1730 arg->type = PRINT_OP;
1731 arg->op.left = left;
1732 arg->op.right = right;
1735 type = process_arg(event, left, &token);
1738 if (type == EVENT_ERROR)
1741 /* Handle other operations in the arguments */
1742 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1743 type = process_op(event, left, &token);
1747 if (test_type_token(type, token, EVENT_OP, ":"))
1752 type = process_arg(event, right, &token);
1754 top->op.right = arg;
1760 /* Top may point to itself */
1761 top->op.right = NULL;
1767 static enum event_type
1768 process_array(struct event_format *event, struct print_arg *top, char **tok)
1770 struct print_arg *arg;
1771 enum event_type type;
1776 do_warning_event(event, "%s: not enough memory!", __func__);
1777 /* '*tok' is set to top->op.op. No need to free. */
1783 type = process_arg(event, arg, &token);
1784 if (test_type_token(type, token, EVENT_OP, "]"))
1787 top->op.right = arg;
1790 type = read_token_item(&token);
1801 static int get_op_prio(char *op)
1815 /* '>>' and '<<' are 8 */
1819 /* '==' and '!=' are 10 */
1829 do_warning("unknown op '%c'", op[0]);
1833 if (strcmp(op, "++") == 0 ||
1834 strcmp(op, "--") == 0) {
1836 } else if (strcmp(op, ">>") == 0 ||
1837 strcmp(op, "<<") == 0) {
1839 } else if (strcmp(op, ">=") == 0 ||
1840 strcmp(op, "<=") == 0) {
1842 } else if (strcmp(op, "==") == 0 ||
1843 strcmp(op, "!=") == 0) {
1845 } else if (strcmp(op, "&&") == 0) {
1847 } else if (strcmp(op, "||") == 0) {
1850 do_warning("unknown op '%s'", op);
1856 static int set_op_prio(struct print_arg *arg)
1859 /* single ops are the greatest */
1860 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1863 arg->op.prio = get_op_prio(arg->op.op);
1865 return arg->op.prio;
1868 /* Note, *tok does not get freed, but will most likely be saved */
1869 static enum event_type
1870 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1872 struct print_arg *left, *right = NULL;
1873 enum event_type type;
1876 /* the op is passed in via tok */
1879 if (arg->type == PRINT_OP && !arg->op.left) {
1880 /* handle single op */
1882 do_warning_event(event, "bad op token %s", token);
1892 do_warning_event(event, "bad op token %s", token);
1897 /* make an empty left */
1902 left->type = PRINT_NULL;
1903 arg->op.left = left;
1905 right = alloc_arg();
1909 arg->op.right = right;
1911 /* do not free the token, it belongs to an op */
1913 type = process_arg(event, right, tok);
1915 } else if (strcmp(token, "?") == 0) {
1921 /* copy the top arg to the left */
1924 arg->type = PRINT_OP;
1926 arg->op.left = left;
1929 /* it will set arg->op.right */
1930 type = process_cond(event, arg, tok);
1932 } else if (strcmp(token, ">>") == 0 ||
1933 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) {
1955 /* copy the top arg to the left */
1958 arg->type = PRINT_OP;
1960 arg->op.left = left;
1961 arg->op.right = NULL;
1963 if (set_op_prio(arg) == -1) {
1964 event->flags |= EVENT_FL_FAILED;
1965 /* arg->op.op (= token) will be freed at out_free */
1970 type = read_token_item(&token);
1973 /* could just be a type pointer */
1974 if ((strcmp(arg->op.op, "*") == 0) &&
1975 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1978 if (left->type != PRINT_ATOM) {
1979 do_warning_event(event, "bad pointer type");
1982 new_atom = realloc(left->atom.atom,
1983 strlen(left->atom.atom) + 3);
1987 left->atom.atom = new_atom;
1988 strcat(left->atom.atom, " *");
1996 right = alloc_arg();
2000 type = process_arg_token(event, right, tok, type);
2001 if (type == EVENT_ERROR) {
2003 /* token was freed in process_arg_token() via *tok */
2008 if (right->type == PRINT_OP &&
2009 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2010 struct print_arg tmp;
2012 /* rotate ops according to the priority */
2013 arg->op.right = right->op.left;
2019 arg->op.left = right;
2021 arg->op.right = right;
2024 } else if (strcmp(token, "[") == 0) {
2032 arg->type = PRINT_OP;
2034 arg->op.left = left;
2038 /* it will set arg->op.right */
2039 type = process_array(event, arg, tok);
2042 do_warning_event(event, "unknown op '%s'", token);
2043 event->flags |= EVENT_FL_FAILED;
2044 /* the arg is now the left side */
2048 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2051 /* higher prios need to be closer to the root */
2052 prio = get_op_prio(*tok);
2054 if (prio > arg->op.prio)
2055 return process_op(event, arg, tok);
2057 return process_op(event, right, tok);
2063 do_warning_event(event, "%s: not enough memory!", __func__);
2070 static enum event_type
2071 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2074 enum event_type type;
2078 if (read_expected(EVENT_OP, "->") < 0)
2081 if (read_expect_type(EVENT_ITEM, &token) < 0)
2085 arg->type = PRINT_FIELD;
2086 arg->field.name = field;
2088 if (is_flag_field) {
2089 arg->field.field = tep_find_any_field(event, arg->field.name);
2090 arg->field.field->flags |= FIELD_IS_FLAG;
2092 } else if (is_symbolic_field) {
2093 arg->field.field = tep_find_any_field(event, arg->field.name);
2094 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2095 is_symbolic_field = 0;
2098 type = read_token(&token);
2110 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2111 struct print_arg **print_arg)
2113 struct print_arg *field;
2114 enum event_type type;
2118 field = alloc_arg();
2120 do_warning_event(event, "%s: not enough memory!", __func__);
2125 type = process_arg(event, field, &token);
2127 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2131 goto out_free_token;
2142 static char *arg_eval (struct print_arg *arg);
2144 static unsigned long long
2145 eval_type_str(unsigned long long val, const char *type, int pointer)
2155 if (type[len-1] != '*') {
2156 do_warning("pointer expected with non pointer type");
2162 do_warning("%s: not enough memory!", __func__);
2165 memcpy(ref, type, len);
2167 /* chop off the " *" */
2170 val = eval_type_str(val, ref, 0);
2175 /* check if this is a pointer */
2176 if (type[len - 1] == '*')
2179 /* Try to figure out the arg size*/
2180 if (strncmp(type, "struct", 6) == 0)
2184 if (strcmp(type, "u8") == 0)
2187 if (strcmp(type, "u16") == 0)
2188 return val & 0xffff;
2190 if (strcmp(type, "u32") == 0)
2191 return val & 0xffffffff;
2193 if (strcmp(type, "u64") == 0 ||
2194 strcmp(type, "s64") == 0)
2197 if (strcmp(type, "s8") == 0)
2198 return (unsigned long long)(char)val & 0xff;
2200 if (strcmp(type, "s16") == 0)
2201 return (unsigned long long)(short)val & 0xffff;
2203 if (strcmp(type, "s32") == 0)
2204 return (unsigned long long)(int)val & 0xffffffff;
2206 if (strncmp(type, "unsigned ", 9) == 0) {
2211 if (strcmp(type, "char") == 0) {
2213 return (unsigned long long)(char)val & 0xff;
2218 if (strcmp(type, "short") == 0) {
2220 return (unsigned long long)(short)val & 0xffff;
2222 return val & 0xffff;
2225 if (strcmp(type, "int") == 0) {
2227 return (unsigned long long)(int)val & 0xffffffff;
2229 return val & 0xffffffff;
2236 * Try to figure out the type.
2238 static unsigned long long
2239 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2241 if (arg->type != PRINT_TYPE) {
2242 do_warning("expected type argument");
2246 return eval_type_str(val, arg->typecast.type, pointer);
2249 static int arg_num_eval(struct print_arg *arg, long long *val)
2251 long long left, right;
2254 switch (arg->type) {
2256 *val = strtoll(arg->atom.atom, NULL, 0);
2259 ret = arg_num_eval(arg->typecast.item, val);
2262 *val = eval_type(*val, arg, 0);
2265 switch (arg->op.op[0]) {
2267 ret = arg_num_eval(arg->op.left, &left);
2270 ret = arg_num_eval(arg->op.right, &right);
2274 *val = left || right;
2276 *val = left | right;
2279 ret = arg_num_eval(arg->op.left, &left);
2282 ret = arg_num_eval(arg->op.right, &right);
2286 *val = left && right;
2288 *val = left & right;
2291 ret = arg_num_eval(arg->op.left, &left);
2294 ret = arg_num_eval(arg->op.right, &right);
2297 switch (arg->op.op[1]) {
2299 *val = left < right;
2302 *val = left << right;
2305 *val = left <= right;
2308 do_warning("unknown op '%s'", arg->op.op);
2313 ret = arg_num_eval(arg->op.left, &left);
2316 ret = arg_num_eval(arg->op.right, &right);
2319 switch (arg->op.op[1]) {
2321 *val = left > right;
2324 *val = left >> right;
2327 *val = left >= right;
2330 do_warning("unknown op '%s'", arg->op.op);
2335 ret = arg_num_eval(arg->op.left, &left);
2338 ret = arg_num_eval(arg->op.right, &right);
2342 if (arg->op.op[1] != '=') {
2343 do_warning("unknown op '%s'", arg->op.op);
2346 *val = left == right;
2349 ret = arg_num_eval(arg->op.left, &left);
2352 ret = arg_num_eval(arg->op.right, &right);
2356 switch (arg->op.op[1]) {
2358 *val = left != right;
2361 do_warning("unknown op '%s'", arg->op.op);
2366 /* check for negative */
2367 if (arg->op.left->type == PRINT_NULL)
2370 ret = arg_num_eval(arg->op.left, &left);
2373 ret = arg_num_eval(arg->op.right, &right);
2376 *val = left - right;
2379 if (arg->op.left->type == PRINT_NULL)
2382 ret = arg_num_eval(arg->op.left, &left);
2385 ret = arg_num_eval(arg->op.right, &right);
2388 *val = left + right;
2391 ret = arg_num_eval(arg->op.right, &right);
2397 do_warning("unknown op '%s'", arg->op.op);
2403 case PRINT_FIELD ... PRINT_SYMBOL:
2408 do_warning("invalid eval type %d", arg->type);
2415 static char *arg_eval (struct print_arg *arg)
2418 static char buf[24];
2420 switch (arg->type) {
2422 return arg->atom.atom;
2424 return arg_eval(arg->typecast.item);
2426 if (!arg_num_eval(arg, &val))
2428 sprintf(buf, "%lld", val);
2432 case PRINT_FIELD ... PRINT_SYMBOL:
2437 do_warning("invalid eval type %d", arg->type);
2444 static enum event_type
2445 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2447 enum event_type type;
2448 struct print_arg *arg = NULL;
2449 struct print_flag_sym *field;
2455 type = read_token_item(&token);
2456 if (test_type_token(type, token, EVENT_OP, "{"))
2464 type = process_arg(event, arg, &token);
2466 if (type == EVENT_OP)
2467 type = process_op(event, arg, &token);
2469 if (type == EVENT_ERROR)
2472 if (test_type_token(type, token, EVENT_DELIM, ","))
2475 field = calloc(1, sizeof(*field));
2479 value = arg_eval(arg);
2481 goto out_free_field;
2482 field->value = strdup(value);
2483 if (field->value == NULL)
2484 goto out_free_field;
2492 type = process_arg(event, arg, &token);
2493 if (test_type_token(type, token, EVENT_OP, "}"))
2494 goto out_free_field;
2496 value = arg_eval(arg);
2498 goto out_free_field;
2499 field->str = strdup(value);
2500 if (field->str == NULL)
2501 goto out_free_field;
2506 list = &field->next;
2509 type = read_token_item(&token);
2510 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2516 free_flag_sym(field);
2525 static enum event_type
2526 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2528 struct print_arg *field;
2529 enum event_type type;
2532 memset(arg, 0, sizeof(*arg));
2533 arg->type = PRINT_FLAGS;
2535 field = alloc_arg();
2537 do_warning_event(event, "%s: not enough memory!", __func__);
2541 type = process_field_arg(event, field, &token);
2543 /* Handle operations in the first argument */
2544 while (type == EVENT_OP)
2545 type = process_op(event, field, &token);
2547 if (test_type_token(type, token, EVENT_DELIM, ","))
2548 goto out_free_field;
2551 arg->flags.field = field;
2553 type = read_token_item(&token);
2554 if (event_item_type(type)) {
2555 arg->flags.delim = token;
2556 type = read_token_item(&token);
2559 if (test_type_token(type, token, EVENT_DELIM, ","))
2562 type = process_fields(event, &arg->flags.flags, &token);
2563 if (test_type_token(type, token, EVENT_DELIM, ")"))
2567 type = read_token_item(tok);
2578 static enum event_type
2579 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2581 struct print_arg *field;
2582 enum event_type type;
2585 memset(arg, 0, sizeof(*arg));
2586 arg->type = PRINT_SYMBOL;
2588 field = alloc_arg();
2590 do_warning_event(event, "%s: not enough memory!", __func__);
2594 type = process_field_arg(event, field, &token);
2596 if (test_type_token(type, token, EVENT_DELIM, ","))
2597 goto out_free_field;
2599 arg->symbol.field = field;
2601 type = process_fields(event, &arg->symbol.symbols, &token);
2602 if (test_type_token(type, token, EVENT_DELIM, ")"))
2606 type = read_token_item(tok);
2617 static enum event_type
2618 process_hex_common(struct event_format *event, struct print_arg *arg,
2619 char **tok, enum print_arg_type type)
2621 memset(arg, 0, sizeof(*arg));
2624 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2627 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2630 return read_token_item(tok);
2633 free_arg(arg->hex.field);
2634 arg->hex.field = NULL;
2640 static enum event_type
2641 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2643 return process_hex_common(event, arg, tok, PRINT_HEX);
2646 static enum event_type
2647 process_hex_str(struct event_format *event, struct print_arg *arg,
2650 return process_hex_common(event, arg, tok, PRINT_HEX_STR);
2653 static enum event_type
2654 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2656 memset(arg, 0, sizeof(*arg));
2657 arg->type = PRINT_INT_ARRAY;
2659 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2662 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2665 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2668 return read_token_item(tok);
2671 free_arg(arg->int_array.count);
2672 arg->int_array.count = NULL;
2674 free_arg(arg->int_array.field);
2675 arg->int_array.field = NULL;
2681 static enum event_type
2682 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2684 struct format_field *field;
2685 enum event_type type;
2688 memset(arg, 0, sizeof(*arg));
2689 arg->type = PRINT_DYNAMIC_ARRAY;
2692 * The item within the parenthesis is another field that holds
2693 * the index into where the array starts.
2695 type = read_token(&token);
2697 if (type != EVENT_ITEM)
2700 /* Find the field */
2702 field = tep_find_field(event, token);
2706 arg->dynarray.field = field;
2707 arg->dynarray.index = 0;
2709 if (read_expected(EVENT_DELIM, ")") < 0)
2713 type = read_token_item(&token);
2715 if (type != EVENT_OP || strcmp(token, "[") != 0)
2721 do_warning_event(event, "%s: not enough memory!", __func__);
2726 type = process_arg(event, arg, &token);
2727 if (type == EVENT_ERROR)
2730 if (!test_type_token(type, token, EVENT_OP, "]"))
2734 type = read_token_item(tok);
2745 static enum event_type
2746 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2749 struct format_field *field;
2750 enum event_type type;
2753 if (read_expect_type(EVENT_ITEM, &token) < 0)
2756 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2758 /* Find the field */
2759 field = tep_find_field(event, token);
2763 arg->dynarray.field = field;
2764 arg->dynarray.index = 0;
2766 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);
4975 if (*ptr == 'F' || *ptr == 'f' ||
4976 *ptr == 'S' || *ptr == 's') {
4978 } else if (*ptr == 'M' || *ptr == 'm') {
4979 print_mac_arg(s, *ptr, data, size, event, arg);
4982 } else if (*ptr == 'I' || *ptr == 'i') {
4985 n = print_ip_arg(s, ptr, data, size, event, arg);
5000 do_warning_event(event, "no argument match");
5001 event->flags |= EVENT_FL_FAILED;
5005 len = ((unsigned long)ptr + 1) -
5006 (unsigned long)saveptr;
5008 /* should never happen */
5010 do_warning_event(event, "bad format!");
5011 event->flags |= EVENT_FL_FAILED;
5015 memcpy(format, saveptr, len);
5018 val = eval_num_arg(data, size, event, arg);
5022 func = find_func(pevent, val);
5024 trace_seq_puts(s, func->func);
5025 if (show_func == 'F')
5032 if (pevent->long_size == 8 && ls == 1 &&
5033 sizeof(long) != 8) {
5036 /* make %l into %ll */
5037 if (ls == 1 && (p = strchr(format, 'l')))
5038 memmove(p+1, p, strlen(p)+1);
5039 else if (strcmp(format, "%p") == 0)
5040 strcpy(format, "0x%llx");
5046 trace_seq_printf(s, format, len_arg, (char)val);
5048 trace_seq_printf(s, format, (char)val);
5052 trace_seq_printf(s, format, len_arg, (short)val);
5054 trace_seq_printf(s, format, (short)val);
5058 trace_seq_printf(s, format, len_arg, (int)val);
5060 trace_seq_printf(s, format, (int)val);
5064 trace_seq_printf(s, format, len_arg, (long)val);
5066 trace_seq_printf(s, format, (long)val);
5070 trace_seq_printf(s, format, len_arg,
5073 trace_seq_printf(s, format, (long long)val);
5076 do_warning_event(event, "bad count (%d)", ls);
5077 event->flags |= EVENT_FL_FAILED;
5082 do_warning_event(event, "no matching argument");
5083 event->flags |= EVENT_FL_FAILED;
5087 len = ((unsigned long)ptr + 1) -
5088 (unsigned long)saveptr;
5090 /* should never happen */
5092 do_warning_event(event, "bad format!");
5093 event->flags |= EVENT_FL_FAILED;
5097 memcpy(format, saveptr, len);
5101 /* Use helper trace_seq */
5103 print_str_arg(&p, data, size, event,
5104 format, len_arg, arg);
5105 trace_seq_terminate(&p);
5106 trace_seq_puts(s, p.buffer);
5107 trace_seq_destroy(&p);
5111 trace_seq_printf(s, ">%c<", *ptr);
5115 trace_seq_putc(s, *ptr);
5118 if (event->flags & EVENT_FL_FAILED) {
5120 trace_seq_printf(s, "[FAILED TO PARSE]");
5130 * tep_data_lat_fmt - parse the data for the latency format
5131 * @pevent: a handle to the pevent
5132 * @s: the trace_seq to write to
5133 * @record: the record to read from
5135 * This parses out the Latency format (interrupts disabled,
5136 * need rescheduling, in hard/soft interrupt, preempt count
5137 * and lock depth) and places it into the trace_seq.
5139 void tep_data_lat_fmt(struct tep_handle *pevent,
5140 struct trace_seq *s, struct tep_record *record)
5142 static int check_lock_depth = 1;
5143 static int check_migrate_disable = 1;
5144 static int lock_depth_exists;
5145 static int migrate_disable_exists;
5146 unsigned int lat_flags;
5149 int migrate_disable;
5152 void *data = record->data;
5154 lat_flags = parse_common_flags(pevent, data);
5155 pc = parse_common_pc(pevent, data);
5156 /* lock_depth may not always exist */
5157 if (lock_depth_exists)
5158 lock_depth = parse_common_lock_depth(pevent, data);
5159 else if (check_lock_depth) {
5160 lock_depth = parse_common_lock_depth(pevent, data);
5162 check_lock_depth = 0;
5164 lock_depth_exists = 1;
5167 /* migrate_disable may not always exist */
5168 if (migrate_disable_exists)
5169 migrate_disable = parse_common_migrate_disable(pevent, data);
5170 else if (check_migrate_disable) {
5171 migrate_disable = parse_common_migrate_disable(pevent, data);
5172 if (migrate_disable < 0)
5173 check_migrate_disable = 0;
5175 migrate_disable_exists = 1;
5178 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5179 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5181 trace_seq_printf(s, "%c%c%c",
5182 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5183 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5185 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5187 (hardirq && softirq) ? 'H' :
5188 hardirq ? 'h' : softirq ? 's' : '.');
5191 trace_seq_printf(s, "%x", pc);
5193 trace_seq_putc(s, '.');
5195 if (migrate_disable_exists) {
5196 if (migrate_disable < 0)
5197 trace_seq_putc(s, '.');
5199 trace_seq_printf(s, "%d", migrate_disable);
5202 if (lock_depth_exists) {
5204 trace_seq_putc(s, '.');
5206 trace_seq_printf(s, "%d", lock_depth);
5209 trace_seq_terminate(s);
5213 * tep_data_type - parse out the given event type
5214 * @pevent: a handle to the pevent
5215 * @rec: the record to read from
5217 * This returns the event id from the @rec.
5219 int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
5221 return trace_parse_common_type(pevent, rec->data);
5225 * tep_data_event_from_type - find the event by a given type
5226 * @pevent: a handle to the pevent
5227 * @type: the type of the event.
5229 * This returns the event form a given @type;
5231 struct event_format *tep_data_event_from_type(struct tep_handle *pevent, int type)
5233 return tep_find_event(pevent, type);
5237 * tep_data_pid - parse the PID from record
5238 * @pevent: a handle to the pevent
5239 * @rec: the record to parse
5241 * This returns the PID from a record.
5243 int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
5245 return parse_common_pid(pevent, rec->data);
5249 * tep_data_preempt_count - parse the preempt count from the record
5250 * @pevent: a handle to the pevent
5251 * @rec: the record to parse
5253 * This returns the preempt count from a record.
5255 int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
5257 return parse_common_pc(pevent, rec->data);
5261 * tep_data_flags - parse the latency flags from the record
5262 * @pevent: a handle to the pevent
5263 * @rec: the record to parse
5265 * This returns the latency flags from a record.
5267 * Use trace_flag_type enum for the flags (see event-parse.h).
5269 int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
5271 return parse_common_flags(pevent, rec->data);
5275 * tep_data_comm_from_pid - return the command line from PID
5276 * @pevent: a handle to the pevent
5277 * @pid: the PID of the task to search for
5279 * This returns a pointer to the command line that has the given
5282 const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
5286 comm = find_cmdline(pevent, pid);
5290 static struct cmdline *
5291 pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct cmdline *next)
5293 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5296 cmdlist = cmdlist->next;
5298 cmdlist = pevent->cmdlist;
5300 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5301 cmdlist = cmdlist->next;
5303 return (struct cmdline *)cmdlist;
5307 * tep_data_pid_from_comm - return the pid from a given comm
5308 * @pevent: a handle to the pevent
5309 * @comm: the cmdline to find the pid from
5310 * @next: the cmdline structure to find the next comm
5312 * This returns the cmdline structure that holds a pid for a given
5313 * comm, or NULL if none found. As there may be more than one pid for
5314 * a given comm, the result of this call can be passed back into
5315 * a recurring call in the @next paramater, and then it will find the
5317 * Also, it does a linear seach, so it may be slow.
5319 struct cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
5320 struct cmdline *next)
5322 struct cmdline *cmdline;
5325 * If the cmdlines have not been converted yet, then use
5328 if (!pevent->cmdlines)
5329 return pid_from_cmdlist(pevent, comm, next);
5333 * The next pointer could have been still from
5334 * a previous call before cmdlines were created
5336 if (next < pevent->cmdlines ||
5337 next >= pevent->cmdlines + pevent->cmdline_count)
5344 cmdline = pevent->cmdlines;
5346 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5347 if (strcmp(cmdline->comm, comm) == 0)
5355 * tep_cmdline_pid - return the pid associated to a given cmdline
5356 * @cmdline: The cmdline structure to get the pid from
5358 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5361 int tep_cmdline_pid(struct tep_handle *pevent, struct cmdline *cmdline)
5363 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5369 * If cmdlines have not been created yet, or cmdline is
5370 * not part of the array, then treat it as a cmdlist instead.
5372 if (!pevent->cmdlines ||
5373 cmdline < pevent->cmdlines ||
5374 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5375 return cmdlist->pid;
5377 return cmdline->pid;
5381 * tep_event_info - parse the data into the print format
5382 * @s: the trace_seq to write to
5383 * @event: the handle to the event
5384 * @record: the record to read from
5386 * This parses the raw @data using the given @event information and
5387 * writes the print format into the trace_seq.
5389 void tep_event_info(struct trace_seq *s, struct event_format *event,
5390 struct tep_record *record)
5392 int print_pretty = 1;
5394 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5395 tep_print_fields(s, record->data, record->size, event);
5398 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5399 print_pretty = event->handler(s, record, event,
5403 pretty_print(s, record->data, record->size, event);
5406 trace_seq_terminate(s);
5409 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5411 if (!use_trace_clock)
5414 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5415 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5418 /* trace_clock is setting in tsc or counter mode */
5423 * tep_find_event_by_record - return the event from a given record
5424 * @pevent: a handle to the pevent
5425 * @record: The record to get the event from
5427 * Returns the associated event for a given record, or NULL if non is
5430 struct event_format *
5431 tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
5435 if (record->size < 0) {
5436 do_warning("ug! negative record size %d", record->size);
5440 type = trace_parse_common_type(pevent, record->data);
5442 return tep_find_event(pevent, type);
5446 * tep_print_event_task - Write the event task comm, pid and CPU
5447 * @pevent: a handle to the pevent
5448 * @s: the trace_seq to write to
5449 * @event: the handle to the record's event
5450 * @record: The record to get the event from
5452 * Writes the tasks comm, pid and CPU to @s.
5454 void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
5455 struct event_format *event,
5456 struct tep_record *record)
5458 void *data = record->data;
5462 pid = parse_common_pid(pevent, data);
5463 comm = find_cmdline(pevent, pid);
5465 if (pevent->latency_format) {
5466 trace_seq_printf(s, "%8.8s-%-5d %3d",
5467 comm, pid, record->cpu);
5469 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5473 * tep_print_event_time - Write the event timestamp
5474 * @pevent: a handle to the pevent
5475 * @s: the trace_seq to write to
5476 * @event: the handle to the record's event
5477 * @record: The record to get the event from
5478 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5480 * Writes the timestamp of the record into @s.
5482 void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
5483 struct event_format *event,
5484 struct tep_record *record,
5485 bool use_trace_clock)
5488 unsigned long usecs;
5489 unsigned long nsecs;
5491 bool use_usec_format;
5493 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5495 if (use_usec_format) {
5496 secs = record->ts / NSEC_PER_SEC;
5497 nsecs = record->ts - secs * NSEC_PER_SEC;
5500 if (pevent->latency_format) {
5501 tep_data_lat_fmt(pevent, s, record);
5504 if (use_usec_format) {
5505 if (pevent->flags & TEP_NSEC_OUTPUT) {
5509 usecs = (nsecs + 500) / NSEC_PER_USEC;
5510 /* To avoid usecs larger than 1 sec */
5511 if (usecs >= USEC_PER_SEC) {
5512 usecs -= USEC_PER_SEC;
5518 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5520 trace_seq_printf(s, " %12llu:", record->ts);
5524 * tep_print_event_data - Write the event data section
5525 * @pevent: a handle to the pevent
5526 * @s: the trace_seq to write to
5527 * @event: the handle to the record's event
5528 * @record: The record to get the event from
5530 * Writes the parsing of the record's data to @s.
5532 void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
5533 struct event_format *event,
5534 struct tep_record *record)
5536 static const char *spaces = " "; /* 20 spaces */
5539 trace_seq_printf(s, " %s: ", event->name);
5541 /* Space out the event names evenly. */
5542 len = strlen(event->name);
5544 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5546 tep_event_info(s, event, record);
5549 void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
5550 struct tep_record *record, bool use_trace_clock)
5552 struct event_format *event;
5554 event = tep_find_event_by_record(pevent, record);
5557 int type = trace_parse_common_type(pevent, record->data);
5559 do_warning("ug! no event found for type %d", type);
5560 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5561 for (i = 0; i < record->size; i++)
5562 trace_seq_printf(s, " %02x",
5563 ((unsigned char *)record->data)[i]);
5567 tep_print_event_task(pevent, s, event, record);
5568 tep_print_event_time(pevent, s, event, record, use_trace_clock);
5569 tep_print_event_data(pevent, s, event, record);
5572 static int events_id_cmp(const void *a, const void *b)
5574 struct event_format * const * ea = a;
5575 struct event_format * const * eb = b;
5577 if ((*ea)->id < (*eb)->id)
5580 if ((*ea)->id > (*eb)->id)
5586 static int events_name_cmp(const void *a, const void *b)
5588 struct event_format * const * ea = a;
5589 struct event_format * const * eb = b;
5592 res = strcmp((*ea)->name, (*eb)->name);
5596 res = strcmp((*ea)->system, (*eb)->system);
5600 return events_id_cmp(a, b);
5603 static int events_system_cmp(const void *a, const void *b)
5605 struct event_format * const * ea = a;
5606 struct event_format * const * eb = b;
5609 res = strcmp((*ea)->system, (*eb)->system);
5613 res = strcmp((*ea)->name, (*eb)->name);
5617 return events_id_cmp(a, b);
5620 struct event_format **tep_list_events(struct tep_handle *pevent, enum event_sort_type sort_type)
5622 struct event_format **events;
5623 int (*sort)(const void *a, const void *b);
5625 events = pevent->sort_events;
5627 if (events && pevent->last_type == sort_type)
5631 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5635 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5636 events[pevent->nr_events] = NULL;
5638 pevent->sort_events = events;
5640 /* the internal events are sorted by id */
5641 if (sort_type == EVENT_SORT_ID) {
5642 pevent->last_type = sort_type;
5647 switch (sort_type) {
5649 sort = events_id_cmp;
5651 case EVENT_SORT_NAME:
5652 sort = events_name_cmp;
5654 case EVENT_SORT_SYSTEM:
5655 sort = events_system_cmp;
5661 qsort(events, pevent->nr_events, sizeof(*events), sort);
5662 pevent->last_type = sort_type;
5667 static struct format_field **
5668 get_event_fields(const char *type, const char *name,
5669 int count, struct format_field *list)
5671 struct format_field **fields;
5672 struct format_field *field;
5675 fields = malloc(sizeof(*fields) * (count + 1));
5679 for (field = list; field; field = field->next) {
5680 fields[i++] = field;
5681 if (i == count + 1) {
5682 do_warning("event %s has more %s fields than specified",
5690 do_warning("event %s has less %s fields than specified",
5699 * tep_event_common_fields - return a list of common fields for an event
5700 * @event: the event to return the common fields of.
5702 * Returns an allocated array of fields. The last item in the array is NULL.
5703 * The array must be freed with free().
5705 struct format_field **tep_event_common_fields(struct event_format *event)
5707 return get_event_fields("common", event->name,
5708 event->format.nr_common,
5709 event->format.common_fields);
5713 * tep_event_fields - return a list of event specific fields for an event
5714 * @event: the event to return the fields of.
5716 * Returns an allocated array of fields. The last item in the array is NULL.
5717 * The array must be freed with free().
5719 struct format_field **tep_event_fields(struct event_format *event)
5721 return get_event_fields("event", event->name,
5722 event->format.nr_fields,
5723 event->format.fields);
5726 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5728 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5730 trace_seq_puts(s, ", ");
5731 print_fields(s, field->next);
5736 static void print_args(struct print_arg *args)
5738 int print_paren = 1;
5741 switch (args->type) {
5746 printf("%s", args->atom.atom);
5749 printf("REC->%s", args->field.name);
5752 printf("__print_flags(");
5753 print_args(args->flags.field);
5754 printf(", %s, ", args->flags.delim);
5756 print_fields(&s, args->flags.flags);
5757 trace_seq_do_printf(&s);
5758 trace_seq_destroy(&s);
5762 printf("__print_symbolic(");
5763 print_args(args->symbol.field);
5766 print_fields(&s, args->symbol.symbols);
5767 trace_seq_do_printf(&s);
5768 trace_seq_destroy(&s);
5772 printf("__print_hex(");
5773 print_args(args->hex.field);
5775 print_args(args->hex.size);
5779 printf("__print_hex_str(");
5780 print_args(args->hex.field);
5782 print_args(args->hex.size);
5785 case PRINT_INT_ARRAY:
5786 printf("__print_array(");
5787 print_args(args->int_array.field);
5789 print_args(args->int_array.count);
5791 print_args(args->int_array.el_size);
5796 printf("__get_str(%s)", args->string.string);
5799 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5802 printf("(%s)", args->typecast.type);
5803 print_args(args->typecast.item);
5806 if (strcmp(args->op.op, ":") == 0)
5810 print_args(args->op.left);
5811 printf(" %s ", args->op.op);
5812 print_args(args->op.right);
5817 /* we should warn... */
5822 print_args(args->next);
5826 static void parse_header_field(const char *field,
5827 int *offset, int *size, int mandatory)
5829 unsigned long long save_input_buf_ptr;
5830 unsigned long long save_input_buf_siz;
5834 save_input_buf_ptr = input_buf_ptr;
5835 save_input_buf_siz = input_buf_siz;
5837 if (read_expected(EVENT_ITEM, "field") < 0)
5839 if (read_expected(EVENT_OP, ":") < 0)
5843 if (read_expect_type(EVENT_ITEM, &token) < 0)
5848 * If this is not a mandatory field, then test it first.
5851 if (read_expected(EVENT_ITEM, field) < 0)
5854 if (read_expect_type(EVENT_ITEM, &token) < 0)
5856 if (strcmp(token, field) != 0)
5861 if (read_expected(EVENT_OP, ";") < 0)
5863 if (read_expected(EVENT_ITEM, "offset") < 0)
5865 if (read_expected(EVENT_OP, ":") < 0)
5867 if (read_expect_type(EVENT_ITEM, &token) < 0)
5869 *offset = atoi(token);
5871 if (read_expected(EVENT_OP, ";") < 0)
5873 if (read_expected(EVENT_ITEM, "size") < 0)
5875 if (read_expected(EVENT_OP, ":") < 0)
5877 if (read_expect_type(EVENT_ITEM, &token) < 0)
5879 *size = atoi(token);
5881 if (read_expected(EVENT_OP, ";") < 0)
5883 type = read_token(&token);
5884 if (type != EVENT_NEWLINE) {
5885 /* newer versions of the kernel have a "signed" type */
5886 if (type != EVENT_ITEM)
5889 if (strcmp(token, "signed") != 0)
5894 if (read_expected(EVENT_OP, ":") < 0)
5897 if (read_expect_type(EVENT_ITEM, &token))
5901 if (read_expected(EVENT_OP, ";") < 0)
5904 if (read_expect_type(EVENT_NEWLINE, &token))
5912 input_buf_ptr = save_input_buf_ptr;
5913 input_buf_siz = save_input_buf_siz;
5920 * tep_parse_header_page - parse the data stored in the header page
5921 * @pevent: the handle to the pevent
5922 * @buf: the buffer storing the header page format string
5923 * @size: the size of @buf
5924 * @long_size: the long size to use if there is no header
5926 * This parses the header page format for information on the
5927 * ring buffer used. The @buf should be copied from
5929 * /sys/kernel/debug/tracing/events/header_page
5931 int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
5938 * Old kernels did not have header page info.
5939 * Sorry but we just use what we find here in user space.
5941 pevent->header_page_ts_size = sizeof(long long);
5942 pevent->header_page_size_size = long_size;
5943 pevent->header_page_data_offset = sizeof(long long) + long_size;
5944 pevent->old_format = 1;
5947 init_input_buf(buf, size);
5949 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5950 &pevent->header_page_ts_size, 1);
5951 parse_header_field("commit", &pevent->header_page_size_offset,
5952 &pevent->header_page_size_size, 1);
5953 parse_header_field("overwrite", &pevent->header_page_overwrite,
5955 parse_header_field("data", &pevent->header_page_data_offset,
5956 &pevent->header_page_data_size, 1);
5961 static int event_matches(struct event_format *event,
5962 int id, const char *sys_name,
5963 const char *event_name)
5965 if (id >= 0 && id != event->id)
5968 if (event_name && (strcmp(event_name, event->name) != 0))
5971 if (sys_name && (strcmp(sys_name, event->system) != 0))
5977 static void free_handler(struct event_handler *handle)
5979 free((void *)handle->sys_name);
5980 free((void *)handle->event_name);
5984 static int find_event_handle(struct tep_handle *pevent, struct event_format *event)
5986 struct event_handler *handle, **next;
5988 for (next = &pevent->handlers; *next;
5989 next = &(*next)->next) {
5991 if (event_matches(event, handle->id,
5993 handle->event_name))
6000 pr_stat("overriding event (%d) %s:%s with new print handler",
6001 event->id, event->system, event->name);
6003 event->handler = handle->func;
6004 event->context = handle->context;
6006 *next = handle->next;
6007 free_handler(handle);
6013 * __tep_parse_format - parse the event format
6014 * @buf: the buffer storing the event format string
6015 * @size: the size of @buf
6016 * @sys: the system the event belongs to
6018 * This parses the event format and creates an event structure
6019 * to quickly parse raw data for a given event.
6021 * These files currently come from:
6023 * /sys/kernel/debug/tracing/events/.../.../format
6025 enum tep_errno __tep_parse_format(struct event_format **eventp,
6026 struct tep_handle *pevent, const char *buf,
6027 unsigned long size, const char *sys)
6029 struct event_format *event;
6032 init_input_buf(buf, size);
6034 *eventp = event = alloc_event();
6036 return TEP_ERRNO__MEM_ALLOC_FAILED;
6038 event->name = event_read_name();
6041 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6042 goto event_alloc_failed;
6045 if (strcmp(sys, "ftrace") == 0) {
6046 event->flags |= EVENT_FL_ISFTRACE;
6048 if (strcmp(event->name, "bprint") == 0)
6049 event->flags |= EVENT_FL_ISBPRINT;
6052 event->id = event_read_id();
6053 if (event->id < 0) {
6054 ret = TEP_ERRNO__READ_ID_FAILED;
6056 * This isn't an allocation error actually.
6057 * But as the ID is critical, just bail out.
6059 goto event_alloc_failed;
6062 event->system = strdup(sys);
6063 if (!event->system) {
6064 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6065 goto event_alloc_failed;
6068 /* Add pevent to event so that it can be referenced */
6069 event->pevent = pevent;
6071 ret = event_read_format(event);
6073 ret = TEP_ERRNO__READ_FORMAT_FAILED;
6074 goto event_parse_failed;
6078 * If the event has an override, don't print warnings if the event
6079 * print format fails to parse.
6081 if (pevent && find_event_handle(pevent, event))
6084 ret = event_read_print(event);
6088 ret = TEP_ERRNO__READ_PRINT_FAILED;
6089 goto event_parse_failed;
6092 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6093 struct format_field *field;
6094 struct print_arg *arg, **list;
6096 /* old ftrace had no args */
6097 list = &event->print_fmt.args;
6098 for (field = event->format.fields; field; field = field->next) {
6101 event->flags |= EVENT_FL_FAILED;
6102 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6104 arg->type = PRINT_FIELD;
6105 arg->field.name = strdup(field->name);
6106 if (!arg->field.name) {
6107 event->flags |= EVENT_FL_FAILED;
6109 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
6111 arg->field.field = field;
6121 event->flags |= EVENT_FL_FAILED;
6125 free(event->system);
6132 static enum tep_errno
6133 __parse_event(struct tep_handle *pevent,
6134 struct event_format **eventp,
6135 const char *buf, unsigned long size,
6138 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
6139 struct event_format *event = *eventp;
6144 if (pevent && add_event(pevent, event)) {
6145 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6146 goto event_add_failed;
6149 #define PRINT_ARGS 0
6150 if (PRINT_ARGS && event->print_fmt.args)
6151 print_args(event->print_fmt.args);
6156 tep_free_format(event);
6161 * tep_parse_format - parse the event format
6162 * @pevent: the handle to the pevent
6163 * @eventp: returned format
6164 * @buf: the buffer storing the event format string
6165 * @size: the size of @buf
6166 * @sys: the system the event belongs to
6168 * This parses the event format and creates an event structure
6169 * to quickly parse raw data for a given event.
6171 * These files currently come from:
6173 * /sys/kernel/debug/tracing/events/.../.../format
6175 enum tep_errno tep_parse_format(struct tep_handle *pevent,
6176 struct event_format **eventp,
6178 unsigned long size, const char *sys)
6180 return __parse_event(pevent, eventp, buf, size, sys);
6184 * tep_parse_event - parse the event format
6185 * @pevent: the handle to the pevent
6186 * @buf: the buffer storing the event format string
6187 * @size: the size of @buf
6188 * @sys: the system the event belongs to
6190 * This parses the event format and creates an event structure
6191 * to quickly parse raw data for a given event.
6193 * These files currently come from:
6195 * /sys/kernel/debug/tracing/events/.../.../format
6197 enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
6198 unsigned long size, const char *sys)
6200 struct event_format *event = NULL;
6201 return __parse_event(pevent, &event, buf, size, sys);
6205 #define _PE(code, str) str
6206 static const char * const tep_error_str[] = {
6211 int tep_strerror(struct tep_handle *pevent __maybe_unused,
6212 enum tep_errno errnum, char *buf, size_t buflen)
6218 str_error_r(errnum, buf, buflen);
6222 if (errnum <= __TEP_ERRNO__START ||
6223 errnum >= __TEP_ERRNO__END)
6226 idx = errnum - __TEP_ERRNO__START - 1;
6227 msg = tep_error_str[idx];
6228 snprintf(buf, buflen, "%s", msg);
6233 int get_field_val(struct trace_seq *s, struct format_field *field,
6234 const char *name, struct tep_record *record,
6235 unsigned long long *val, int err)
6239 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6243 if (tep_read_number_field(field, record->data, val)) {
6245 trace_seq_printf(s, " %s=INVALID", name);
6253 * tep_get_field_raw - return the raw pointer into the data field
6254 * @s: The seq to print to on error
6255 * @event: the event that the field is for
6256 * @name: The name of the field
6257 * @record: The record with the field name.
6258 * @len: place to store the field length.
6259 * @err: print default error if failed.
6261 * Returns a pointer into record->data of the field and places
6262 * the length of the field in @len.
6264 * On failure, it returns NULL.
6266 void *tep_get_field_raw(struct trace_seq *s, struct event_format *event,
6267 const char *name, struct tep_record *record,
6270 struct format_field *field;
6271 void *data = record->data;
6278 field = tep_find_field(event, name);
6282 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6286 /* Allow @len to be NULL */
6290 offset = field->offset;
6291 if (field->flags & FIELD_IS_DYNAMIC) {
6292 offset = tep_read_number(event->pevent,
6293 data + offset, field->size);
6294 *len = offset >> 16;
6299 return data + offset;
6303 * tep_get_field_val - find a field and return its value
6304 * @s: The seq to print to on error
6305 * @event: the event that the field is for
6306 * @name: The name of the field
6307 * @record: The record with the field name.
6308 * @val: place to store the value of the field.
6309 * @err: print default error if failed.
6311 * Returns 0 on success -1 on field not found.
6313 int tep_get_field_val(struct trace_seq *s, struct event_format *event,
6314 const char *name, struct tep_record *record,
6315 unsigned long long *val, int err)
6317 struct format_field *field;
6322 field = tep_find_field(event, name);
6324 return get_field_val(s, field, name, record, val, err);
6328 * tep_get_common_field_val - find a common field and return its value
6329 * @s: The seq to print to on error
6330 * @event: the event that the field is for
6331 * @name: The name of the field
6332 * @record: The record with the field name.
6333 * @val: place to store the value of the field.
6334 * @err: print default error if failed.
6336 * Returns 0 on success -1 on field not found.
6338 int tep_get_common_field_val(struct trace_seq *s, struct event_format *event,
6339 const char *name, struct tep_record *record,
6340 unsigned long long *val, int err)
6342 struct format_field *field;
6347 field = tep_find_common_field(event, name);
6349 return get_field_val(s, field, name, record, val, err);
6353 * tep_get_any_field_val - find a any field and return its value
6354 * @s: The seq to print to on error
6355 * @event: the event that the field is for
6356 * @name: The name of the field
6357 * @record: The record with the field name.
6358 * @val: place to store the value of the field.
6359 * @err: print default error if failed.
6361 * Returns 0 on success -1 on field not found.
6363 int tep_get_any_field_val(struct trace_seq *s, struct event_format *event,
6364 const char *name, struct tep_record *record,
6365 unsigned long long *val, int err)
6367 struct format_field *field;
6372 field = tep_find_any_field(event, name);
6374 return get_field_val(s, field, name, record, val, err);
6378 * tep_print_num_field - print a field and a format
6379 * @s: The seq to print to
6380 * @fmt: The printf format to print the field with.
6381 * @event: the event that the field is for
6382 * @name: The name of the field
6383 * @record: The record with the field name.
6384 * @err: print default error if failed.
6386 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6388 int tep_print_num_field(struct trace_seq *s, const char *fmt,
6389 struct event_format *event, const char *name,
6390 struct tep_record *record, int err)
6392 struct format_field *field = tep_find_field(event, name);
6393 unsigned long long val;
6398 if (tep_read_number_field(field, record->data, &val))
6401 return trace_seq_printf(s, fmt, val);
6405 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6410 * tep_print_func_field - print a field and a format for function pointers
6411 * @s: The seq to print to
6412 * @fmt: The printf format to print the field with.
6413 * @event: the event that the field is for
6414 * @name: The name of the field
6415 * @record: The record with the field name.
6416 * @err: print default error if failed.
6418 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6420 int tep_print_func_field(struct trace_seq *s, const char *fmt,
6421 struct event_format *event, const char *name,
6422 struct tep_record *record, int err)
6424 struct format_field *field = tep_find_field(event, name);
6425 struct tep_handle *pevent = event->pevent;
6426 unsigned long long val;
6427 struct func_map *func;
6433 if (tep_read_number_field(field, record->data, &val))
6436 func = find_func(pevent, val);
6439 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6441 sprintf(tmp, "0x%08llx", val);
6443 return trace_seq_printf(s, fmt, tmp);
6447 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6451 static void free_func_handle(struct tep_function_handler *func)
6453 struct func_params *params;
6457 while (func->params) {
6458 params = func->params;
6459 func->params = params->next;
6467 * tep_register_print_function - register a helper function
6468 * @pevent: the handle to the pevent
6469 * @func: the function to process the helper function
6470 * @ret_type: the return type of the helper function
6471 * @name: the name of the helper function
6472 * @parameters: A list of enum tep_func_arg_type
6474 * Some events may have helper functions in the print format arguments.
6475 * This allows a plugin to dynamically create a way to process one
6476 * of these functions.
6478 * The @parameters is a variable list of tep_func_arg_type enums that
6479 * must end with TEP_FUNC_ARG_VOID.
6481 int tep_register_print_function(struct tep_handle *pevent,
6482 tep_func_handler func,
6483 enum tep_func_arg_type ret_type,
6486 struct tep_function_handler *func_handle;
6487 struct func_params **next_param;
6488 struct func_params *param;
6489 enum tep_func_arg_type type;
6493 func_handle = find_func_handler(pevent, name);
6496 * This is most like caused by the users own
6497 * plugins updating the function. This overrides the
6500 pr_stat("override of function helper '%s'", name);
6501 remove_func_handler(pevent, name);
6504 func_handle = calloc(1, sizeof(*func_handle));
6506 do_warning("Failed to allocate function handler");
6507 return TEP_ERRNO__MEM_ALLOC_FAILED;
6510 func_handle->ret_type = ret_type;
6511 func_handle->name = strdup(name);
6512 func_handle->func = func;
6513 if (!func_handle->name) {
6514 do_warning("Failed to allocate function name");
6516 return TEP_ERRNO__MEM_ALLOC_FAILED;
6519 next_param = &(func_handle->params);
6522 type = va_arg(ap, enum tep_func_arg_type);
6523 if (type == TEP_FUNC_ARG_VOID)
6526 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
6527 do_warning("Invalid argument type %d", type);
6528 ret = TEP_ERRNO__INVALID_ARG_TYPE;
6532 param = malloc(sizeof(*param));
6534 do_warning("Failed to allocate function param");
6535 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
6541 *next_param = param;
6542 next_param = &(param->next);
6544 func_handle->nr_args++;
6548 func_handle->next = pevent->func_handlers;
6549 pevent->func_handlers = func_handle;
6554 free_func_handle(func_handle);
6559 * tep_unregister_print_function - unregister a helper function
6560 * @pevent: the handle to the pevent
6561 * @func: the function to process the helper function
6562 * @name: the name of the helper function
6564 * This function removes existing print handler for function @name.
6566 * Returns 0 if the handler was removed successully, -1 otherwise.
6568 int tep_unregister_print_function(struct tep_handle *pevent,
6569 tep_func_handler func, char *name)
6571 struct tep_function_handler *func_handle;
6573 func_handle = find_func_handler(pevent, name);
6574 if (func_handle && func_handle->func == func) {
6575 remove_func_handler(pevent, name);
6581 static struct event_format *search_event(struct tep_handle *pevent, int id,
6582 const char *sys_name,
6583 const char *event_name)
6585 struct event_format *event;
6589 event = tep_find_event(pevent, id);
6592 if (event_name && (strcmp(event_name, event->name) != 0))
6594 if (sys_name && (strcmp(sys_name, event->system) != 0))
6597 event = tep_find_event_by_name(pevent, sys_name, event_name);
6605 * tep_register_event_handler - register a way to parse an event
6606 * @pevent: the handle to the pevent
6607 * @id: the id of the event to register
6608 * @sys_name: the system name the event belongs to
6609 * @event_name: the name of the event
6610 * @func: the function to call to parse the event information
6611 * @context: the data to be passed to @func
6613 * This function allows a developer to override the parsing of
6614 * a given event. If for some reason the default print format
6615 * is not sufficient, this function will register a function
6616 * for an event to be used to parse the data instead.
6618 * If @id is >= 0, then it is used to find the event.
6619 * else @sys_name and @event_name are used.
6621 int tep_register_event_handler(struct tep_handle *pevent, int id,
6622 const char *sys_name, const char *event_name,
6623 tep_event_handler_func func, void *context)
6625 struct event_format *event;
6626 struct event_handler *handle;
6628 event = search_event(pevent, id, sys_name, event_name);
6632 pr_stat("overriding event (%d) %s:%s with new print handler",
6633 event->id, event->system, event->name);
6635 event->handler = func;
6636 event->context = context;
6640 /* Save for later use. */
6641 handle = calloc(1, sizeof(*handle));
6643 do_warning("Failed to allocate event handler");
6644 return TEP_ERRNO__MEM_ALLOC_FAILED;
6649 handle->event_name = strdup(event_name);
6651 handle->sys_name = strdup(sys_name);
6653 if ((event_name && !handle->event_name) ||
6654 (sys_name && !handle->sys_name)) {
6655 do_warning("Failed to allocate event/sys name");
6656 free((void *)handle->event_name);
6657 free((void *)handle->sys_name);
6659 return TEP_ERRNO__MEM_ALLOC_FAILED;
6662 handle->func = func;
6663 handle->next = pevent->handlers;
6664 pevent->handlers = handle;
6665 handle->context = context;
6670 static int handle_matches(struct event_handler *handler, int id,
6671 const char *sys_name, const char *event_name,
6672 tep_event_handler_func func, void *context)
6674 if (id >= 0 && id != handler->id)
6677 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6680 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6683 if (func != handler->func || context != handler->context)
6690 * tep_unregister_event_handler - unregister an existing event handler
6691 * @pevent: the handle to the pevent
6692 * @id: the id of the event to unregister
6693 * @sys_name: the system name the handler belongs to
6694 * @event_name: the name of the event handler
6695 * @func: the function to call to parse the event information
6696 * @context: the data to be passed to @func
6698 * This function removes existing event handler (parser).
6700 * If @id is >= 0, then it is used to find the event.
6701 * else @sys_name and @event_name are used.
6703 * Returns 0 if handler was removed successfully, -1 if event was not found.
6705 int tep_unregister_event_handler(struct tep_handle *pevent, int id,
6706 const char *sys_name, const char *event_name,
6707 tep_event_handler_func func, void *context)
6709 struct event_format *event;
6710 struct event_handler *handle;
6711 struct event_handler **next;
6713 event = search_event(pevent, id, sys_name, event_name);
6717 if (event->handler == func && event->context == context) {
6718 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6719 event->id, event->system, event->name);
6721 event->handler = NULL;
6722 event->context = NULL;
6727 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6729 if (handle_matches(handle, id, sys_name, event_name,
6737 *next = handle->next;
6738 free_handler(handle);
6744 * tep_alloc - create a pevent handle
6746 struct tep_handle *tep_alloc(void)
6748 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
6751 pevent->ref_count = 1;
6756 void tep_ref(struct tep_handle *pevent)
6758 pevent->ref_count++;
6761 void tep_free_format_field(struct format_field *field)
6764 if (field->alias != field->name)
6770 static void free_format_fields(struct format_field *field)
6772 struct format_field *next;
6776 tep_free_format_field(field);
6781 static void free_formats(struct format *format)
6783 free_format_fields(format->common_fields);
6784 free_format_fields(format->fields);
6787 void tep_free_format(struct event_format *event)
6790 free(event->system);
6792 free_formats(&event->format);
6794 free(event->print_fmt.format);
6795 free_args(event->print_fmt.args);
6801 * tep_free - free a pevent handle
6802 * @pevent: the pevent handle to free
6804 void tep_free(struct tep_handle *pevent)
6806 struct cmdline_list *cmdlist, *cmdnext;
6807 struct func_list *funclist, *funcnext;
6808 struct printk_list *printklist, *printknext;
6809 struct tep_function_handler *func_handler;
6810 struct event_handler *handle;
6816 cmdlist = pevent->cmdlist;
6817 funclist = pevent->funclist;
6818 printklist = pevent->printklist;
6820 pevent->ref_count--;
6821 if (pevent->ref_count)
6824 if (pevent->cmdlines) {
6825 for (i = 0; i < pevent->cmdline_count; i++)
6826 free(pevent->cmdlines[i].comm);
6827 free(pevent->cmdlines);
6831 cmdnext = cmdlist->next;
6832 free(cmdlist->comm);
6837 if (pevent->func_map) {
6838 for (i = 0; i < (int)pevent->func_count; i++) {
6839 free(pevent->func_map[i].func);
6840 free(pevent->func_map[i].mod);
6842 free(pevent->func_map);
6846 funcnext = funclist->next;
6847 free(funclist->func);
6848 free(funclist->mod);
6850 funclist = funcnext;
6853 while (pevent->func_handlers) {
6854 func_handler = pevent->func_handlers;
6855 pevent->func_handlers = func_handler->next;
6856 free_func_handle(func_handler);
6859 if (pevent->printk_map) {
6860 for (i = 0; i < (int)pevent->printk_count; i++)
6861 free(pevent->printk_map[i].printk);
6862 free(pevent->printk_map);
6865 while (printklist) {
6866 printknext = printklist->next;
6867 free(printklist->printk);
6869 printklist = printknext;
6872 for (i = 0; i < pevent->nr_events; i++)
6873 tep_free_format(pevent->events[i]);
6875 while (pevent->handlers) {
6876 handle = pevent->handlers;
6877 pevent->handlers = handle->next;
6878 free_handler(handle);
6881 free(pevent->trace_clock);
6882 free(pevent->events);
6883 free(pevent->sort_events);
6884 free(pevent->func_resolver);
6889 void tep_unref(struct tep_handle *pevent)