2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include <linux/string.h>
37 #include <netinet/in.h>
38 #include "event-parse.h"
39 #include "event-utils.h"
41 static const char *input_buf;
42 static unsigned long long input_buf_ptr;
43 static unsigned long long input_buf_siz;
45 static int is_flag_field;
46 static int is_symbolic_field;
48 static int show_warning = 1;
50 #define do_warning(fmt, ...) \
53 warning(fmt, ##__VA_ARGS__); \
56 #define do_warning_event(event, fmt, ...) \
62 warning("[%s:%s] " fmt, event->system, \
63 event->name, ##__VA_ARGS__); \
65 warning(fmt, ##__VA_ARGS__); \
68 static void init_input_buf(const char *buf, unsigned long long size)
75 const char *pevent_get_input_buf(void)
80 unsigned long long pevent_get_input_buf_ptr(void)
85 struct event_handler {
86 struct event_handler *next;
89 const char *event_name;
90 pevent_event_handler_func func;
94 struct pevent_func_params {
95 struct pevent_func_params *next;
96 enum pevent_func_arg_type type;
99 struct pevent_function_handler {
100 struct pevent_function_handler *next;
101 enum pevent_func_arg_type ret_type;
103 pevent_func_handler func;
104 struct pevent_func_params *params;
108 static unsigned long long
109 process_defined_func(struct trace_seq *s, void *data, int size,
110 struct event_format *event, struct print_arg *arg);
112 static void free_func_handle(struct pevent_function_handler *func);
115 * pevent_buffer_init - init buffer for parsing
116 * @buf: buffer to parse
117 * @size: the size of the buffer
119 * For use with pevent_read_token(), this initializes the internal
120 * buffer that pevent_read_token() will parse.
122 void pevent_buffer_init(const char *buf, unsigned long long size)
124 init_input_buf(buf, size);
127 void breakpoint(void)
133 struct print_arg *alloc_arg(void)
135 return calloc(1, sizeof(struct print_arg));
143 static int cmdline_cmp(const void *a, const void *b)
145 const struct cmdline *ca = a;
146 const struct cmdline *cb = b;
148 if (ca->pid < cb->pid)
150 if (ca->pid > cb->pid)
156 struct cmdline_list {
157 struct cmdline_list *next;
162 static int cmdline_init(struct pevent *pevent)
164 struct cmdline_list *cmdlist = pevent->cmdlist;
165 struct cmdline_list *item;
166 struct cmdline *cmdlines;
169 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
175 cmdlines[i].pid = cmdlist->pid;
176 cmdlines[i].comm = cmdlist->comm;
179 cmdlist = cmdlist->next;
183 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
185 pevent->cmdlines = cmdlines;
186 pevent->cmdlist = NULL;
191 static const char *find_cmdline(struct pevent *pevent, int pid)
193 const struct cmdline *comm;
199 if (!pevent->cmdlines && cmdline_init(pevent))
200 return "<not enough memory for cmdlines!>";
204 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
205 sizeof(*pevent->cmdlines), cmdline_cmp);
213 * pevent_pid_is_registered - return if a pid has a cmdline registered
214 * @pevent: handle for the pevent
215 * @pid: The pid to check if it has a cmdline registered with.
217 * Returns 1 if the pid has a cmdline mapped to it
220 int pevent_pid_is_registered(struct pevent *pevent, int pid)
222 const struct cmdline *comm;
228 if (!pevent->cmdlines && cmdline_init(pevent))
233 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
234 sizeof(*pevent->cmdlines), cmdline_cmp);
242 * If the command lines have been converted to an array, then
243 * we must add this pid. This is much slower than when cmdlines
244 * are added before the array is initialized.
246 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
248 struct cmdline *cmdlines = pevent->cmdlines;
249 const struct cmdline *cmdline;
255 /* avoid duplicates */
258 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
259 sizeof(*pevent->cmdlines), cmdline_cmp);
265 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
270 pevent->cmdlines = cmdlines;
272 cmdlines[pevent->cmdline_count].comm = strdup(comm);
273 if (!cmdlines[pevent->cmdline_count].comm) {
278 cmdlines[pevent->cmdline_count].pid = pid;
280 if (cmdlines[pevent->cmdline_count].comm)
281 pevent->cmdline_count++;
283 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
289 * pevent_register_comm - register a pid / comm mapping
290 * @pevent: handle for the pevent
291 * @comm: the command line to register
292 * @pid: the pid to map the command line to
294 * This adds a mapping to search for command line names with
295 * a given pid. The comm is duplicated.
297 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
299 struct cmdline_list *item;
301 if (pevent->cmdlines)
302 return add_new_comm(pevent, comm, pid);
304 item = malloc(sizeof(*item));
309 item->comm = strdup(comm);
311 item->comm = strdup("<...>");
317 item->next = pevent->cmdlist;
319 pevent->cmdlist = item;
320 pevent->cmdline_count++;
325 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
327 pevent->trace_clock = strdup(trace_clock);
328 if (!pevent->trace_clock) {
336 unsigned long long addr;
342 struct func_list *next;
343 unsigned long long addr;
348 static int func_cmp(const void *a, const void *b)
350 const struct func_map *fa = a;
351 const struct func_map *fb = b;
353 if (fa->addr < fb->addr)
355 if (fa->addr > fb->addr)
362 * We are searching for a record in between, not an exact
365 static int func_bcmp(const void *a, const void *b)
367 const struct func_map *fa = a;
368 const struct func_map *fb = b;
370 if ((fa->addr == fb->addr) ||
372 (fa->addr > fb->addr &&
373 fa->addr < (fb+1)->addr))
376 if (fa->addr < fb->addr)
382 static int func_map_init(struct pevent *pevent)
384 struct func_list *funclist;
385 struct func_list *item;
386 struct func_map *func_map;
389 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
393 funclist = pevent->funclist;
397 func_map[i].func = funclist->func;
398 func_map[i].addr = funclist->addr;
399 func_map[i].mod = funclist->mod;
402 funclist = funclist->next;
406 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
409 * Add a special record at the end.
411 func_map[pevent->func_count].func = NULL;
412 func_map[pevent->func_count].addr = 0;
413 func_map[pevent->func_count].mod = NULL;
415 pevent->func_map = func_map;
416 pevent->funclist = NULL;
421 static struct func_map *
422 __find_func(struct pevent *pevent, unsigned long long addr)
424 struct func_map *func;
427 if (!pevent->func_map)
428 func_map_init(pevent);
432 func = bsearch(&key, pevent->func_map, pevent->func_count,
433 sizeof(*pevent->func_map), func_bcmp);
438 struct func_resolver {
439 pevent_func_resolver_t *func;
445 * pevent_set_function_resolver - set an alternative function resolver
446 * @pevent: handle for the pevent
447 * @resolver: function to be used
448 * @priv: resolver function private state.
450 * Some tools may have already a way to resolve kernel functions, allow them to
451 * keep using it instead of duplicating all the entries inside
454 int pevent_set_function_resolver(struct pevent *pevent,
455 pevent_func_resolver_t *func, void *priv)
457 struct func_resolver *resolver = malloc(sizeof(*resolver));
459 if (resolver == NULL)
462 resolver->func = func;
463 resolver->priv = priv;
465 free(pevent->func_resolver);
466 pevent->func_resolver = resolver;
472 * pevent_reset_function_resolver - reset alternative function resolver
473 * @pevent: handle for the pevent
475 * Stop using whatever alternative resolver was set, use the default
478 void pevent_reset_function_resolver(struct pevent *pevent)
480 free(pevent->func_resolver);
481 pevent->func_resolver = NULL;
484 static struct func_map *
485 find_func(struct pevent *pevent, unsigned long long addr)
487 struct func_map *map;
489 if (!pevent->func_resolver)
490 return __find_func(pevent, addr);
492 map = &pevent->func_resolver->map;
495 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
496 &map->addr, &map->mod);
497 if (map->func == NULL)
504 * pevent_find_function - find a function by a given address
505 * @pevent: handle for the pevent
506 * @addr: the address to find the function with
508 * Returns a pointer to the function stored that has the given
509 * address. Note, the address does not have to be exact, it
510 * will select the function that would contain the address.
512 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
514 struct func_map *map;
516 map = find_func(pevent, addr);
524 * pevent_find_function_address - find a function address by a given address
525 * @pevent: handle for the pevent
526 * @addr: the address to find the function with
528 * Returns the address the function starts at. This can be used in
529 * conjunction with pevent_find_function to print both the function
530 * name and the function offset.
533 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
535 struct func_map *map;
537 map = find_func(pevent, addr);
545 * pevent_register_function - register a function with a given address
546 * @pevent: handle for the pevent
547 * @function: the function name to register
548 * @addr: the address the function starts at
549 * @mod: the kernel module the function may be in (NULL for none)
551 * This registers a function name with an address and module.
552 * The @func passed in is duplicated.
554 int pevent_register_function(struct pevent *pevent, char *func,
555 unsigned long long addr, char *mod)
557 struct func_list *item = malloc(sizeof(*item));
562 item->next = pevent->funclist;
563 item->func = strdup(func);
568 item->mod = strdup(mod);
575 pevent->funclist = item;
576 pevent->func_count++;
590 * pevent_print_funcs - print out the stored functions
591 * @pevent: handle for the pevent
593 * This prints out the stored functions.
595 void pevent_print_funcs(struct pevent *pevent)
599 if (!pevent->func_map)
600 func_map_init(pevent);
602 for (i = 0; i < (int)pevent->func_count; i++) {
604 pevent->func_map[i].addr,
605 pevent->func_map[i].func);
606 if (pevent->func_map[i].mod)
607 printf(" [%s]\n", pevent->func_map[i].mod);
614 unsigned long long addr;
619 struct printk_list *next;
620 unsigned long long addr;
624 static int printk_cmp(const void *a, const void *b)
626 const struct printk_map *pa = a;
627 const struct printk_map *pb = b;
629 if (pa->addr < pb->addr)
631 if (pa->addr > pb->addr)
637 static int printk_map_init(struct pevent *pevent)
639 struct printk_list *printklist;
640 struct printk_list *item;
641 struct printk_map *printk_map;
644 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
648 printklist = pevent->printklist;
652 printk_map[i].printk = printklist->printk;
653 printk_map[i].addr = printklist->addr;
656 printklist = printklist->next;
660 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
662 pevent->printk_map = printk_map;
663 pevent->printklist = NULL;
668 static struct printk_map *
669 find_printk(struct pevent *pevent, unsigned long long addr)
671 struct printk_map *printk;
672 struct printk_map key;
674 if (!pevent->printk_map && printk_map_init(pevent))
679 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
680 sizeof(*pevent->printk_map), printk_cmp);
686 * pevent_register_print_string - register a string by its address
687 * @pevent: handle for the pevent
688 * @fmt: the string format to register
689 * @addr: the address the string was located at
691 * This registers a string by the address it was stored in the kernel.
692 * The @fmt passed in is duplicated.
694 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
695 unsigned long long addr)
697 struct printk_list *item = malloc(sizeof(*item));
703 item->next = pevent->printklist;
706 /* Strip off quotes and '\n' from the end */
709 item->printk = strdup(fmt);
713 p = item->printk + strlen(item->printk) - 1;
718 if (strcmp(p, "\\n") == 0)
721 pevent->printklist = item;
722 pevent->printk_count++;
733 * pevent_print_printk - print out the stored strings
734 * @pevent: handle for the pevent
736 * This prints the string formats that were stored.
738 void pevent_print_printk(struct pevent *pevent)
742 if (!pevent->printk_map)
743 printk_map_init(pevent);
745 for (i = 0; i < (int)pevent->printk_count; i++) {
746 printf("%016llx %s\n",
747 pevent->printk_map[i].addr,
748 pevent->printk_map[i].printk);
752 static struct event_format *alloc_event(void)
754 return calloc(1, sizeof(struct event_format));
757 static int add_event(struct pevent *pevent, struct event_format *event)
760 struct event_format **events = realloc(pevent->events, sizeof(event) *
761 (pevent->nr_events + 1));
765 pevent->events = events;
767 for (i = 0; i < pevent->nr_events; i++) {
768 if (pevent->events[i]->id > event->id)
771 if (i < pevent->nr_events)
772 memmove(&pevent->events[i + 1],
774 sizeof(event) * (pevent->nr_events - i));
776 pevent->events[i] = event;
779 event->pevent = pevent;
784 static int event_item_type(enum event_type type)
787 case EVENT_ITEM ... EVENT_SQUOTE:
789 case EVENT_ERROR ... EVENT_DELIM:
795 static void free_flag_sym(struct print_flag_sym *fsym)
797 struct print_flag_sym *next;
808 static void free_arg(struct print_arg *arg)
810 struct print_arg *farg;
817 free(arg->atom.atom);
820 free(arg->field.name);
823 free_arg(arg->flags.field);
824 free(arg->flags.delim);
825 free_flag_sym(arg->flags.flags);
828 free_arg(arg->symbol.field);
829 free_flag_sym(arg->symbol.symbols);
832 free_arg(arg->hex.field);
833 free_arg(arg->hex.size);
835 case PRINT_INT_ARRAY:
836 free_arg(arg->int_array.field);
837 free_arg(arg->int_array.count);
838 free_arg(arg->int_array.el_size);
841 free(arg->typecast.type);
842 free_arg(arg->typecast.item);
846 free(arg->string.string);
849 free(arg->bitmask.bitmask);
851 case PRINT_DYNAMIC_ARRAY:
852 case PRINT_DYNAMIC_ARRAY_LEN:
853 free(arg->dynarray.index);
857 free_arg(arg->op.left);
858 free_arg(arg->op.right);
861 while (arg->func.args) {
862 farg = arg->func.args;
863 arg->func.args = farg->next;
876 static enum event_type get_type(int ch)
879 return EVENT_NEWLINE;
882 if (isalnum(ch) || ch == '_')
890 if (ch == '(' || ch == ')' || ch == ',')
896 static int __read_char(void)
898 if (input_buf_ptr >= input_buf_siz)
901 return input_buf[input_buf_ptr++];
904 static int __peek_char(void)
906 if (input_buf_ptr >= input_buf_siz)
909 return input_buf[input_buf_ptr];
913 * pevent_peek_char - peek at the next character that will be read
915 * Returns the next character read, or -1 if end of buffer.
917 int pevent_peek_char(void)
919 return __peek_char();
922 static int extend_token(char **tok, char *buf, int size)
924 char *newtok = realloc(*tok, size);
941 static enum event_type force_token(const char *str, char **tok);
943 static enum event_type __read_token(char **tok)
946 int ch, last_ch, quote_ch, next_ch;
949 enum event_type type;
959 if (type == EVENT_NONE)
967 if (asprintf(tok, "%c", ch) < 0)
975 next_ch = __peek_char();
976 if (next_ch == '>') {
977 buf[i++] = __read_char();
990 buf[i++] = __read_char();
1002 default: /* what should we do instead? */
1012 buf[i++] = __read_char();
1017 /* don't keep quotes */
1023 if (i == (BUFSIZ - 1)) {
1027 if (extend_token(tok, buf, tok_size) < 0)
1034 /* the '\' '\' will cancel itself */
1035 if (ch == '\\' && last_ch == '\\')
1037 } while (ch != quote_ch || last_ch == '\\');
1038 /* remove the last quote */
1042 * For strings (double quotes) check the next token.
1043 * If it is another string, concatinate the two.
1045 if (type == EVENT_DQUOTE) {
1046 unsigned long long save_input_buf_ptr = input_buf_ptr;
1050 } while (isspace(ch));
1053 input_buf_ptr = save_input_buf_ptr;
1058 case EVENT_ERROR ... EVENT_SPACE:
1064 while (get_type(__peek_char()) == type) {
1065 if (i == (BUFSIZ - 1)) {
1069 if (extend_token(tok, buf, tok_size) < 0)
1079 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1082 if (type == EVENT_ITEM) {
1084 * Older versions of the kernel has a bug that
1085 * creates invalid symbols and will break the mac80211
1086 * parsing. This is a work around to that bug.
1088 * See Linux kernel commit:
1089 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1091 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1094 return force_token("\"\%s\" ", tok);
1095 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1098 return force_token("\" sta:%pM\" ", tok);
1099 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1102 return force_token("\" vif:%p(%d)\" ", tok);
1109 static enum event_type force_token(const char *str, char **tok)
1111 const char *save_input_buf;
1112 unsigned long long save_input_buf_ptr;
1113 unsigned long long save_input_buf_siz;
1114 enum event_type type;
1116 /* save off the current input pointers */
1117 save_input_buf = input_buf;
1118 save_input_buf_ptr = input_buf_ptr;
1119 save_input_buf_siz = input_buf_siz;
1121 init_input_buf(str, strlen(str));
1123 type = __read_token(tok);
1125 /* reset back to original token */
1126 input_buf = save_input_buf;
1127 input_buf_ptr = save_input_buf_ptr;
1128 input_buf_siz = save_input_buf_siz;
1133 static void free_token(char *tok)
1139 static enum event_type read_token(char **tok)
1141 enum event_type type;
1144 type = __read_token(tok);
1145 if (type != EVENT_SPACE)
1157 * pevent_read_token - access to utilites to use the pevent parser
1158 * @tok: The token to return
1160 * This will parse tokens from the string given by
1161 * pevent_init_data().
1163 * Returns the token type.
1165 enum event_type pevent_read_token(char **tok)
1167 return read_token(tok);
1171 * pevent_free_token - free a token returned by pevent_read_token
1172 * @token: the token to free
1174 void pevent_free_token(char *token)
1180 static enum event_type read_token_item(char **tok)
1182 enum event_type type;
1185 type = __read_token(tok);
1186 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1197 static int test_type(enum event_type type, enum event_type expect)
1199 if (type != expect) {
1200 do_warning("Error: expected type %d but read %d",
1207 static int test_type_token(enum event_type type, const char *token,
1208 enum event_type expect, const char *expect_tok)
1210 if (type != expect) {
1211 do_warning("Error: expected type %d but read %d",
1216 if (strcmp(token, expect_tok) != 0) {
1217 do_warning("Error: expected '%s' but read '%s'",
1224 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1226 enum event_type type;
1229 type = read_token(tok);
1231 type = read_token_item(tok);
1232 return test_type(type, expect);
1235 static int read_expect_type(enum event_type expect, char **tok)
1237 return __read_expect_type(expect, tok, 1);
1240 static int __read_expected(enum event_type expect, const char *str,
1243 enum event_type type;
1248 type = read_token(&token);
1250 type = read_token_item(&token);
1252 ret = test_type_token(type, token, expect, str);
1259 static int read_expected(enum event_type expect, const char *str)
1261 return __read_expected(expect, str, 1);
1264 static int read_expected_item(enum event_type expect, const char *str)
1266 return __read_expected(expect, str, 0);
1269 static char *event_read_name(void)
1273 if (read_expected(EVENT_ITEM, "name") < 0)
1276 if (read_expected(EVENT_OP, ":") < 0)
1279 if (read_expect_type(EVENT_ITEM, &token) < 0)
1289 static int event_read_id(void)
1294 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1297 if (read_expected(EVENT_OP, ":") < 0)
1300 if (read_expect_type(EVENT_ITEM, &token) < 0)
1303 id = strtoul(token, NULL, 0);
1312 static int field_is_string(struct format_field *field)
1314 if ((field->flags & FIELD_IS_ARRAY) &&
1315 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1316 strstr(field->type, "s8")))
1322 static int field_is_dynamic(struct format_field *field)
1324 if (strncmp(field->type, "__data_loc", 10) == 0)
1330 static int field_is_long(struct format_field *field)
1332 /* includes long long */
1333 if (strstr(field->type, "long"))
1339 static unsigned int type_size(const char *name)
1341 /* This covers all FIELD_IS_STRING types. */
1359 for (i = 0; table[i].type; i++) {
1360 if (!strcmp(table[i].type, name))
1361 return table[i].size;
1367 static int event_read_fields(struct event_format *event, struct format_field **fields)
1369 struct format_field *field = NULL;
1370 enum event_type type;
1376 unsigned int size_dynamic = 0;
1378 type = read_token(&token);
1379 if (type == EVENT_NEWLINE) {
1386 if (test_type_token(type, token, EVENT_ITEM, "field"))
1390 type = read_token(&token);
1392 * The ftrace fields may still use the "special" name.
1395 if (event->flags & EVENT_FL_ISFTRACE &&
1396 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1398 type = read_token(&token);
1401 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1405 if (read_expect_type(EVENT_ITEM, &token) < 0)
1410 field = calloc(1, sizeof(*field));
1414 field->event = event;
1416 /* read the rest of the type */
1418 type = read_token(&token);
1419 if (type == EVENT_ITEM ||
1420 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1422 * Some of the ftrace fields are broken and have
1423 * an illegal "." in them.
1425 (event->flags & EVENT_FL_ISFTRACE &&
1426 type == EVENT_OP && strcmp(token, ".") == 0)) {
1428 if (strcmp(token, "*") == 0)
1429 field->flags |= FIELD_IS_POINTER;
1433 new_type = realloc(field->type,
1434 strlen(field->type) +
1435 strlen(last_token) + 2);
1440 field->type = new_type;
1441 strcat(field->type, " ");
1442 strcat(field->type, last_token);
1445 field->type = last_token;
1454 do_warning_event(event, "%s: no type found", __func__);
1457 field->name = field->alias = last_token;
1459 if (test_type(type, EVENT_OP))
1462 if (strcmp(token, "[") == 0) {
1463 enum event_type last_type = type;
1464 char *brackets = token;
1468 field->flags |= FIELD_IS_ARRAY;
1470 type = read_token(&token);
1472 if (type == EVENT_ITEM)
1473 field->arraylen = strtoul(token, NULL, 0);
1475 field->arraylen = 0;
1477 while (strcmp(token, "]") != 0) {
1478 if (last_type == EVENT_ITEM &&
1485 new_brackets = realloc(brackets,
1487 strlen(token) + len);
1488 if (!new_brackets) {
1492 brackets = new_brackets;
1494 strcat(brackets, " ");
1495 strcat(brackets, token);
1496 /* We only care about the last token */
1497 field->arraylen = strtoul(token, NULL, 0);
1499 type = read_token(&token);
1500 if (type == EVENT_NONE) {
1501 do_warning_event(event, "failed to find token");
1508 new_brackets = realloc(brackets, strlen(brackets) + 2);
1509 if (!new_brackets) {
1513 brackets = new_brackets;
1514 strcat(brackets, "]");
1516 /* add brackets to type */
1518 type = read_token(&token);
1520 * If the next token is not an OP, then it is of
1521 * the format: type [] item;
1523 if (type == EVENT_ITEM) {
1525 new_type = realloc(field->type,
1526 strlen(field->type) +
1527 strlen(field->name) +
1528 strlen(brackets) + 2);
1533 field->type = new_type;
1534 strcat(field->type, " ");
1535 strcat(field->type, field->name);
1536 size_dynamic = type_size(field->name);
1537 free_token(field->name);
1538 strcat(field->type, brackets);
1539 field->name = field->alias = token;
1540 type = read_token(&token);
1543 new_type = realloc(field->type,
1544 strlen(field->type) +
1545 strlen(brackets) + 1);
1550 field->type = new_type;
1551 strcat(field->type, brackets);
1556 if (field_is_string(field))
1557 field->flags |= FIELD_IS_STRING;
1558 if (field_is_dynamic(field))
1559 field->flags |= FIELD_IS_DYNAMIC;
1560 if (field_is_long(field))
1561 field->flags |= FIELD_IS_LONG;
1563 if (test_type_token(type, token, EVENT_OP, ";"))
1567 if (read_expected(EVENT_ITEM, "offset") < 0)
1570 if (read_expected(EVENT_OP, ":") < 0)
1573 if (read_expect_type(EVENT_ITEM, &token))
1575 field->offset = strtoul(token, NULL, 0);
1578 if (read_expected(EVENT_OP, ";") < 0)
1581 if (read_expected(EVENT_ITEM, "size") < 0)
1584 if (read_expected(EVENT_OP, ":") < 0)
1587 if (read_expect_type(EVENT_ITEM, &token))
1589 field->size = strtoul(token, NULL, 0);
1592 if (read_expected(EVENT_OP, ";") < 0)
1595 type = read_token(&token);
1596 if (type != EVENT_NEWLINE) {
1597 /* newer versions of the kernel have a "signed" type */
1598 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1603 if (read_expected(EVENT_OP, ":") < 0)
1606 if (read_expect_type(EVENT_ITEM, &token))
1609 if (strtoul(token, NULL, 0))
1610 field->flags |= FIELD_IS_SIGNED;
1613 if (read_expected(EVENT_OP, ";") < 0)
1616 if (read_expect_type(EVENT_NEWLINE, &token))
1622 if (field->flags & FIELD_IS_ARRAY) {
1623 if (field->arraylen)
1624 field->elementsize = field->size / field->arraylen;
1625 else if (field->flags & FIELD_IS_DYNAMIC)
1626 field->elementsize = size_dynamic;
1627 else if (field->flags & FIELD_IS_STRING)
1628 field->elementsize = 1;
1629 else if (field->flags & FIELD_IS_LONG)
1630 field->elementsize = event->pevent ?
1631 event->pevent->long_size :
1634 field->elementsize = field->size;
1637 fields = &field->next;
1654 static int event_read_format(struct event_format *event)
1659 if (read_expected_item(EVENT_ITEM, "format") < 0)
1662 if (read_expected(EVENT_OP, ":") < 0)
1665 if (read_expect_type(EVENT_NEWLINE, &token))
1669 ret = event_read_fields(event, &event->format.common_fields);
1672 event->format.nr_common = ret;
1674 ret = event_read_fields(event, &event->format.fields);
1677 event->format.nr_fields = ret;
1686 static enum event_type
1687 process_arg_token(struct event_format *event, struct print_arg *arg,
1688 char **tok, enum event_type type);
1690 static enum event_type
1691 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1693 enum event_type type;
1696 type = read_token(&token);
1699 return process_arg_token(event, arg, tok, type);
1702 static enum event_type
1703 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1706 * For __print_symbolic() and __print_flags, we need to completely
1707 * evaluate the first argument, which defines what to print next.
1709 static enum event_type
1710 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1712 enum event_type type;
1714 type = process_arg(event, arg, tok);
1716 while (type == EVENT_OP) {
1717 type = process_op(event, arg, tok);
1723 static enum event_type
1724 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1726 struct print_arg *arg, *left, *right;
1727 enum event_type type;
1732 right = alloc_arg();
1734 if (!arg || !left || !right) {
1735 do_warning_event(event, "%s: not enough memory!", __func__);
1736 /* arg will be freed at out_free */
1742 arg->type = PRINT_OP;
1743 arg->op.left = left;
1744 arg->op.right = right;
1747 type = process_arg(event, left, &token);
1750 if (type == EVENT_ERROR)
1753 /* Handle other operations in the arguments */
1754 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1755 type = process_op(event, left, &token);
1759 if (test_type_token(type, token, EVENT_OP, ":"))
1764 type = process_arg(event, right, &token);
1766 top->op.right = arg;
1772 /* Top may point to itself */
1773 top->op.right = NULL;
1779 static enum event_type
1780 process_array(struct event_format *event, struct print_arg *top, char **tok)
1782 struct print_arg *arg;
1783 enum event_type type;
1788 do_warning_event(event, "%s: not enough memory!", __func__);
1789 /* '*tok' is set to top->op.op. No need to free. */
1795 type = process_arg(event, arg, &token);
1796 if (test_type_token(type, token, EVENT_OP, "]"))
1799 top->op.right = arg;
1802 type = read_token_item(&token);
1813 static int get_op_prio(char *op)
1827 /* '>>' and '<<' are 8 */
1831 /* '==' and '!=' are 10 */
1841 do_warning("unknown op '%c'", op[0]);
1845 if (strcmp(op, "++") == 0 ||
1846 strcmp(op, "--") == 0) {
1848 } else if (strcmp(op, ">>") == 0 ||
1849 strcmp(op, "<<") == 0) {
1851 } else if (strcmp(op, ">=") == 0 ||
1852 strcmp(op, "<=") == 0) {
1854 } else if (strcmp(op, "==") == 0 ||
1855 strcmp(op, "!=") == 0) {
1857 } else if (strcmp(op, "&&") == 0) {
1859 } else if (strcmp(op, "||") == 0) {
1862 do_warning("unknown op '%s'", op);
1868 static int set_op_prio(struct print_arg *arg)
1871 /* single ops are the greatest */
1872 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1875 arg->op.prio = get_op_prio(arg->op.op);
1877 return arg->op.prio;
1880 /* Note, *tok does not get freed, but will most likely be saved */
1881 static enum event_type
1882 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1884 struct print_arg *left, *right = NULL;
1885 enum event_type type;
1888 /* the op is passed in via tok */
1891 if (arg->type == PRINT_OP && !arg->op.left) {
1892 /* handle single op */
1894 do_warning_event(event, "bad op token %s", token);
1904 do_warning_event(event, "bad op token %s", token);
1909 /* make an empty left */
1914 left->type = PRINT_NULL;
1915 arg->op.left = left;
1917 right = alloc_arg();
1921 arg->op.right = right;
1923 /* do not free the token, it belongs to an op */
1925 type = process_arg(event, right, tok);
1927 } else if (strcmp(token, "?") == 0) {
1933 /* copy the top arg to the left */
1936 arg->type = PRINT_OP;
1938 arg->op.left = left;
1941 /* it will set arg->op.right */
1942 type = process_cond(event, arg, tok);
1944 } else if (strcmp(token, ">>") == 0 ||
1945 strcmp(token, "<<") == 0 ||
1946 strcmp(token, "&") == 0 ||
1947 strcmp(token, "|") == 0 ||
1948 strcmp(token, "&&") == 0 ||
1949 strcmp(token, "||") == 0 ||
1950 strcmp(token, "-") == 0 ||
1951 strcmp(token, "+") == 0 ||
1952 strcmp(token, "*") == 0 ||
1953 strcmp(token, "^") == 0 ||
1954 strcmp(token, "/") == 0 ||
1955 strcmp(token, "%") == 0 ||
1956 strcmp(token, "<") == 0 ||
1957 strcmp(token, ">") == 0 ||
1958 strcmp(token, "<=") == 0 ||
1959 strcmp(token, ">=") == 0 ||
1960 strcmp(token, "==") == 0 ||
1961 strcmp(token, "!=") == 0) {
1967 /* copy the top arg to the left */
1970 arg->type = PRINT_OP;
1972 arg->op.left = left;
1973 arg->op.right = NULL;
1975 if (set_op_prio(arg) == -1) {
1976 event->flags |= EVENT_FL_FAILED;
1977 /* arg->op.op (= token) will be freed at out_free */
1982 type = read_token_item(&token);
1985 /* could just be a type pointer */
1986 if ((strcmp(arg->op.op, "*") == 0) &&
1987 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1990 if (left->type != PRINT_ATOM) {
1991 do_warning_event(event, "bad pointer type");
1994 new_atom = realloc(left->atom.atom,
1995 strlen(left->atom.atom) + 3);
1999 left->atom.atom = new_atom;
2000 strcat(left->atom.atom, " *");
2008 right = alloc_arg();
2012 type = process_arg_token(event, right, tok, type);
2013 if (type == EVENT_ERROR) {
2015 /* token was freed in process_arg_token() via *tok */
2020 if (right->type == PRINT_OP &&
2021 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2022 struct print_arg tmp;
2024 /* rotate ops according to the priority */
2025 arg->op.right = right->op.left;
2031 arg->op.left = right;
2033 arg->op.right = right;
2036 } else if (strcmp(token, "[") == 0) {
2044 arg->type = PRINT_OP;
2046 arg->op.left = left;
2050 /* it will set arg->op.right */
2051 type = process_array(event, arg, tok);
2054 do_warning_event(event, "unknown op '%s'", token);
2055 event->flags |= EVENT_FL_FAILED;
2056 /* the arg is now the left side */
2060 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2063 /* higher prios need to be closer to the root */
2064 prio = get_op_prio(*tok);
2066 if (prio > arg->op.prio)
2067 return process_op(event, arg, tok);
2069 return process_op(event, right, tok);
2075 do_warning_event(event, "%s: not enough memory!", __func__);
2082 static enum event_type
2083 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2086 enum event_type type;
2090 if (read_expected(EVENT_OP, "->") < 0)
2093 if (read_expect_type(EVENT_ITEM, &token) < 0)
2097 arg->type = PRINT_FIELD;
2098 arg->field.name = field;
2100 if (is_flag_field) {
2101 arg->field.field = pevent_find_any_field(event, arg->field.name);
2102 arg->field.field->flags |= FIELD_IS_FLAG;
2104 } else if (is_symbolic_field) {
2105 arg->field.field = pevent_find_any_field(event, arg->field.name);
2106 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2107 is_symbolic_field = 0;
2110 type = read_token(&token);
2122 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2123 struct print_arg **print_arg)
2125 struct print_arg *field;
2126 enum event_type type;
2130 field = alloc_arg();
2132 do_warning_event(event, "%s: not enough memory!", __func__);
2137 type = process_arg(event, field, &token);
2139 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2143 goto out_free_token;
2154 static char *arg_eval (struct print_arg *arg);
2156 static unsigned long long
2157 eval_type_str(unsigned long long val, const char *type, int pointer)
2167 if (type[len-1] != '*') {
2168 do_warning("pointer expected with non pointer type");
2174 do_warning("%s: not enough memory!", __func__);
2177 memcpy(ref, type, len);
2179 /* chop off the " *" */
2182 val = eval_type_str(val, ref, 0);
2187 /* check if this is a pointer */
2188 if (type[len - 1] == '*')
2191 /* Try to figure out the arg size*/
2192 if (strncmp(type, "struct", 6) == 0)
2196 if (strcmp(type, "u8") == 0)
2199 if (strcmp(type, "u16") == 0)
2200 return val & 0xffff;
2202 if (strcmp(type, "u32") == 0)
2203 return val & 0xffffffff;
2205 if (strcmp(type, "u64") == 0 ||
2206 strcmp(type, "s64") == 0)
2209 if (strcmp(type, "s8") == 0)
2210 return (unsigned long long)(char)val & 0xff;
2212 if (strcmp(type, "s16") == 0)
2213 return (unsigned long long)(short)val & 0xffff;
2215 if (strcmp(type, "s32") == 0)
2216 return (unsigned long long)(int)val & 0xffffffff;
2218 if (strncmp(type, "unsigned ", 9) == 0) {
2223 if (strcmp(type, "char") == 0) {
2225 return (unsigned long long)(char)val & 0xff;
2230 if (strcmp(type, "short") == 0) {
2232 return (unsigned long long)(short)val & 0xffff;
2234 return val & 0xffff;
2237 if (strcmp(type, "int") == 0) {
2239 return (unsigned long long)(int)val & 0xffffffff;
2241 return val & 0xffffffff;
2248 * Try to figure out the type.
2250 static unsigned long long
2251 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2253 if (arg->type != PRINT_TYPE) {
2254 do_warning("expected type argument");
2258 return eval_type_str(val, arg->typecast.type, pointer);
2261 static int arg_num_eval(struct print_arg *arg, long long *val)
2263 long long left, right;
2266 switch (arg->type) {
2268 *val = strtoll(arg->atom.atom, NULL, 0);
2271 ret = arg_num_eval(arg->typecast.item, val);
2274 *val = eval_type(*val, arg, 0);
2277 switch (arg->op.op[0]) {
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);
2298 *val = left && right;
2300 *val = left & right;
2303 ret = arg_num_eval(arg->op.left, &left);
2306 ret = arg_num_eval(arg->op.right, &right);
2309 switch (arg->op.op[1]) {
2311 *val = left < right;
2314 *val = left << right;
2317 *val = left <= right;
2320 do_warning("unknown op '%s'", arg->op.op);
2325 ret = arg_num_eval(arg->op.left, &left);
2328 ret = arg_num_eval(arg->op.right, &right);
2331 switch (arg->op.op[1]) {
2333 *val = left > right;
2336 *val = left >> right;
2339 *val = left >= right;
2342 do_warning("unknown op '%s'", arg->op.op);
2347 ret = arg_num_eval(arg->op.left, &left);
2350 ret = arg_num_eval(arg->op.right, &right);
2354 if (arg->op.op[1] != '=') {
2355 do_warning("unknown op '%s'", arg->op.op);
2358 *val = left == right;
2361 ret = arg_num_eval(arg->op.left, &left);
2364 ret = arg_num_eval(arg->op.right, &right);
2368 switch (arg->op.op[1]) {
2370 *val = left != right;
2373 do_warning("unknown op '%s'", arg->op.op);
2378 /* check for negative */
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 if (arg->op.left->type == PRINT_NULL)
2394 ret = arg_num_eval(arg->op.left, &left);
2397 ret = arg_num_eval(arg->op.right, &right);
2400 *val = left + right;
2403 ret = arg_num_eval(arg->op.right, &right);
2409 do_warning("unknown op '%s'", arg->op.op);
2415 case PRINT_FIELD ... PRINT_SYMBOL:
2420 do_warning("invalid eval type %d", arg->type);
2427 static char *arg_eval (struct print_arg *arg)
2430 static char buf[24];
2432 switch (arg->type) {
2434 return arg->atom.atom;
2436 return arg_eval(arg->typecast.item);
2438 if (!arg_num_eval(arg, &val))
2440 sprintf(buf, "%lld", val);
2444 case PRINT_FIELD ... PRINT_SYMBOL:
2449 do_warning("invalid eval type %d", arg->type);
2456 static enum event_type
2457 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2459 enum event_type type;
2460 struct print_arg *arg = NULL;
2461 struct print_flag_sym *field;
2467 type = read_token_item(&token);
2468 if (test_type_token(type, token, EVENT_OP, "{"))
2476 type = process_arg(event, arg, &token);
2478 if (type == EVENT_OP)
2479 type = process_op(event, arg, &token);
2481 if (type == EVENT_ERROR)
2484 if (test_type_token(type, token, EVENT_DELIM, ","))
2487 field = calloc(1, sizeof(*field));
2491 value = arg_eval(arg);
2493 goto out_free_field;
2494 field->value = strdup(value);
2495 if (field->value == NULL)
2496 goto out_free_field;
2504 type = process_arg(event, arg, &token);
2505 if (test_type_token(type, token, EVENT_OP, "}"))
2506 goto out_free_field;
2508 value = arg_eval(arg);
2510 goto out_free_field;
2511 field->str = strdup(value);
2512 if (field->str == NULL)
2513 goto out_free_field;
2518 list = &field->next;
2521 type = read_token_item(&token);
2522 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2528 free_flag_sym(field);
2537 static enum event_type
2538 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2540 struct print_arg *field;
2541 enum event_type type;
2544 memset(arg, 0, sizeof(*arg));
2545 arg->type = PRINT_FLAGS;
2547 field = alloc_arg();
2549 do_warning_event(event, "%s: not enough memory!", __func__);
2553 type = process_field_arg(event, field, &token);
2555 /* Handle operations in the first argument */
2556 while (type == EVENT_OP)
2557 type = process_op(event, field, &token);
2559 if (test_type_token(type, token, EVENT_DELIM, ","))
2560 goto out_free_field;
2563 arg->flags.field = field;
2565 type = read_token_item(&token);
2566 if (event_item_type(type)) {
2567 arg->flags.delim = token;
2568 type = read_token_item(&token);
2571 if (test_type_token(type, token, EVENT_DELIM, ","))
2574 type = process_fields(event, &arg->flags.flags, &token);
2575 if (test_type_token(type, token, EVENT_DELIM, ")"))
2579 type = read_token_item(tok);
2590 static enum event_type
2591 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2593 struct print_arg *field;
2594 enum event_type type;
2597 memset(arg, 0, sizeof(*arg));
2598 arg->type = PRINT_SYMBOL;
2600 field = alloc_arg();
2602 do_warning_event(event, "%s: not enough memory!", __func__);
2606 type = process_field_arg(event, field, &token);
2608 if (test_type_token(type, token, EVENT_DELIM, ","))
2609 goto out_free_field;
2611 arg->symbol.field = field;
2613 type = process_fields(event, &arg->symbol.symbols, &token);
2614 if (test_type_token(type, token, EVENT_DELIM, ")"))
2618 type = read_token_item(tok);
2629 static enum event_type
2630 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2632 memset(arg, 0, sizeof(*arg));
2633 arg->type = PRINT_HEX;
2635 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2638 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2641 return read_token_item(tok);
2644 free_arg(arg->hex.field);
2645 arg->hex.field = NULL;
2651 static enum event_type
2652 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2654 memset(arg, 0, sizeof(*arg));
2655 arg->type = PRINT_INT_ARRAY;
2657 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2660 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2663 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2666 return read_token_item(tok);
2669 free_arg(arg->int_array.count);
2670 arg->int_array.count = NULL;
2672 free_arg(arg->int_array.field);
2673 arg->int_array.field = NULL;
2679 static enum event_type
2680 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2682 struct format_field *field;
2683 enum event_type type;
2686 memset(arg, 0, sizeof(*arg));
2687 arg->type = PRINT_DYNAMIC_ARRAY;
2690 * The item within the parenthesis is another field that holds
2691 * the index into where the array starts.
2693 type = read_token(&token);
2695 if (type != EVENT_ITEM)
2698 /* Find the field */
2700 field = pevent_find_field(event, token);
2704 arg->dynarray.field = field;
2705 arg->dynarray.index = 0;
2707 if (read_expected(EVENT_DELIM, ")") < 0)
2711 type = read_token_item(&token);
2713 if (type != EVENT_OP || strcmp(token, "[") != 0)
2719 do_warning_event(event, "%s: not enough memory!", __func__);
2724 type = process_arg(event, arg, &token);
2725 if (type == EVENT_ERROR)
2728 if (!test_type_token(type, token, EVENT_OP, "]"))
2732 type = read_token_item(tok);
2743 static enum event_type
2744 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2747 struct format_field *field;
2748 enum event_type type;
2751 if (read_expect_type(EVENT_ITEM, &token) < 0)
2754 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2756 /* Find the field */
2757 field = pevent_find_field(event, token);
2761 arg->dynarray.field = field;
2762 arg->dynarray.index = 0;
2764 if (read_expected(EVENT_DELIM, ")") < 0)
2768 type = read_token(&token);
2780 static enum event_type
2781 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2783 struct print_arg *item_arg;
2784 enum event_type type;
2787 type = process_arg(event, arg, &token);
2789 if (type == EVENT_ERROR)
2792 if (type == EVENT_OP)
2793 type = process_op(event, arg, &token);
2795 if (type == EVENT_ERROR)
2798 if (test_type_token(type, token, EVENT_DELIM, ")"))
2802 type = read_token_item(&token);
2805 * If the next token is an item or another open paren, then
2806 * this was a typecast.
2808 if (event_item_type(type) ||
2809 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2811 /* make this a typecast and contine */
2813 /* prevous must be an atom */
2814 if (arg->type != PRINT_ATOM) {
2815 do_warning_event(event, "previous needed to be PRINT_ATOM");
2819 item_arg = alloc_arg();
2821 do_warning_event(event, "%s: not enough memory!",
2826 arg->type = PRINT_TYPE;
2827 arg->typecast.type = arg->atom.atom;
2828 arg->typecast.item = item_arg;
2829 type = process_arg_token(event, item_arg, &token, type);
2843 static enum event_type
2844 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2847 enum event_type type;
2850 if (read_expect_type(EVENT_ITEM, &token) < 0)
2853 arg->type = PRINT_STRING;
2854 arg->string.string = token;
2855 arg->string.offset = -1;
2857 if (read_expected(EVENT_DELIM, ")") < 0)
2860 type = read_token(&token);
2872 static enum event_type
2873 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2876 enum event_type type;
2879 if (read_expect_type(EVENT_ITEM, &token) < 0)
2882 arg->type = PRINT_BITMASK;
2883 arg->bitmask.bitmask = token;
2884 arg->bitmask.offset = -1;
2886 if (read_expected(EVENT_DELIM, ")") < 0)
2889 type = read_token(&token);
2901 static struct pevent_function_handler *
2902 find_func_handler(struct pevent *pevent, char *func_name)
2904 struct pevent_function_handler *func;
2909 for (func = pevent->func_handlers; func; func = func->next) {
2910 if (strcmp(func->name, func_name) == 0)
2917 static void remove_func_handler(struct pevent *pevent, char *func_name)
2919 struct pevent_function_handler *func;
2920 struct pevent_function_handler **next;
2922 next = &pevent->func_handlers;
2923 while ((func = *next)) {
2924 if (strcmp(func->name, func_name) == 0) {
2926 free_func_handle(func);
2933 static enum event_type
2934 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2935 struct print_arg *arg, char **tok)
2937 struct print_arg **next_arg;
2938 struct print_arg *farg;
2939 enum event_type type;
2943 arg->type = PRINT_FUNC;
2944 arg->func.func = func;
2948 next_arg = &(arg->func.args);
2949 for (i = 0; i < func->nr_args; i++) {
2952 do_warning_event(event, "%s: not enough memory!",
2957 type = process_arg(event, farg, &token);
2958 if (i < (func->nr_args - 1)) {
2959 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2960 do_warning_event(event,
2961 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2962 func->name, func->nr_args,
2963 event->name, i + 1);
2967 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2968 do_warning_event(event,
2969 "Error: function '%s()' only expects %d arguments but event %s has more",
2970 func->name, func->nr_args, event->name);
2976 next_arg = &(farg->next);
2980 type = read_token(&token);
2991 static enum event_type
2992 process_function(struct event_format *event, struct print_arg *arg,
2993 char *token, char **tok)
2995 struct pevent_function_handler *func;
2997 if (strcmp(token, "__print_flags") == 0) {
3000 return process_flags(event, arg, tok);
3002 if (strcmp(token, "__print_symbolic") == 0) {
3004 is_symbolic_field = 1;
3005 return process_symbols(event, arg, tok);
3007 if (strcmp(token, "__print_hex") == 0) {
3009 return process_hex(event, arg, tok);
3011 if (strcmp(token, "__print_array") == 0) {
3013 return process_int_array(event, arg, tok);
3015 if (strcmp(token, "__get_str") == 0) {
3017 return process_str(event, arg, tok);
3019 if (strcmp(token, "__get_bitmask") == 0) {
3021 return process_bitmask(event, arg, tok);
3023 if (strcmp(token, "__get_dynamic_array") == 0) {
3025 return process_dynamic_array(event, arg, tok);
3027 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3029 return process_dynamic_array_len(event, arg, tok);
3032 func = find_func_handler(event->pevent, token);
3035 return process_func_handler(event, func, arg, tok);
3038 do_warning_event(event, "function %s not defined", token);
3043 static enum event_type
3044 process_arg_token(struct event_format *event, struct print_arg *arg,
3045 char **tok, enum event_type type)
3054 if (strcmp(token, "REC") == 0) {
3056 type = process_entry(event, arg, &token);
3060 /* test the next token */
3061 type = read_token_item(&token);
3064 * If the next token is a parenthesis, then this
3067 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3070 /* this will free atom. */
3071 type = process_function(event, arg, atom, &token);
3074 /* atoms can be more than one token long */
3075 while (type == EVENT_ITEM) {
3077 new_atom = realloc(atom,
3078 strlen(atom) + strlen(token) + 2);
3087 strcat(atom, token);
3089 type = read_token_item(&token);
3092 arg->type = PRINT_ATOM;
3093 arg->atom.atom = atom;
3098 arg->type = PRINT_ATOM;
3099 arg->atom.atom = token;
3100 type = read_token_item(&token);
3103 if (strcmp(token, "(") == 0) {
3105 type = process_paren(event, arg, &token);
3109 /* handle single ops */
3110 arg->type = PRINT_OP;
3112 arg->op.left = NULL;
3113 type = process_op(event, arg, &token);
3115 /* On error, the op is freed */
3116 if (type == EVENT_ERROR)
3119 /* return error type if errored */
3122 case EVENT_ERROR ... EVENT_NEWLINE:
3124 do_warning_event(event, "unexpected type %d", type);
3132 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3134 enum event_type type = EVENT_ERROR;
3135 struct print_arg *arg;
3140 if (type == EVENT_NEWLINE) {
3141 type = read_token_item(&token);
3147 do_warning_event(event, "%s: not enough memory!",
3152 type = process_arg(event, arg, &token);
3154 if (type == EVENT_ERROR) {
3163 if (type == EVENT_OP) {
3164 type = process_op(event, arg, &token);
3166 if (type == EVENT_ERROR) {
3175 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3182 } while (type != EVENT_NONE);
3184 if (type != EVENT_NONE && type != EVENT_ERROR)
3190 static int event_read_print(struct event_format *event)
3192 enum event_type type;
3196 if (read_expected_item(EVENT_ITEM, "print") < 0)
3199 if (read_expected(EVENT_ITEM, "fmt") < 0)
3202 if (read_expected(EVENT_OP, ":") < 0)
3205 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3209 event->print_fmt.format = token;
3210 event->print_fmt.args = NULL;
3212 /* ok to have no arg */
3213 type = read_token_item(&token);
3215 if (type == EVENT_NONE)
3218 /* Handle concatenation of print lines */
3219 if (type == EVENT_DQUOTE) {
3222 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3225 free_token(event->print_fmt.format);
3226 event->print_fmt.format = NULL;
3231 if (test_type_token(type, token, EVENT_DELIM, ","))
3236 ret = event_read_print_args(event, &event->print_fmt.args);
3248 * pevent_find_common_field - return a common field by event
3249 * @event: handle for the event
3250 * @name: the name of the common field to return
3252 * Returns a common field from the event by the given @name.
3253 * This only searchs the common fields and not all field.
3255 struct format_field *
3256 pevent_find_common_field(struct event_format *event, const char *name)
3258 struct format_field *format;
3260 for (format = event->format.common_fields;
3261 format; format = format->next) {
3262 if (strcmp(format->name, name) == 0)
3270 * pevent_find_field - find a non-common field
3271 * @event: handle for the event
3272 * @name: the name of the non-common field
3274 * Returns a non-common field by the given @name.
3275 * This does not search common fields.
3277 struct format_field *
3278 pevent_find_field(struct event_format *event, const char *name)
3280 struct format_field *format;
3282 for (format = event->format.fields;
3283 format; format = format->next) {
3284 if (strcmp(format->name, name) == 0)
3292 * pevent_find_any_field - find any field by name
3293 * @event: handle for the event
3294 * @name: the name of the field
3296 * Returns a field by the given @name.
3297 * This searchs the common field names first, then
3298 * the non-common ones if a common one was not found.
3300 struct format_field *
3301 pevent_find_any_field(struct event_format *event, const char *name)
3303 struct format_field *format;
3305 format = pevent_find_common_field(event, name);
3308 return pevent_find_field(event, name);
3312 * pevent_read_number - read a number from data
3313 * @pevent: handle for the pevent
3314 * @ptr: the raw data
3315 * @size: the size of the data that holds the number
3317 * Returns the number (converted to host) from the
3320 unsigned long long pevent_read_number(struct pevent *pevent,
3321 const void *ptr, int size)
3325 return *(unsigned char *)ptr;
3327 return data2host2(pevent, ptr);
3329 return data2host4(pevent, ptr);
3331 return data2host8(pevent, ptr);
3339 * pevent_read_number_field - read a number from data
3340 * @field: a handle to the field
3341 * @data: the raw data to read
3342 * @value: the value to place the number in
3344 * Reads raw data according to a field offset and size,
3345 * and translates it into @value.
3347 * Returns 0 on success, -1 otherwise.
3349 int pevent_read_number_field(struct format_field *field, const void *data,
3350 unsigned long long *value)
3354 switch (field->size) {
3359 *value = pevent_read_number(field->event->pevent,
3360 data + field->offset, field->size);
3367 static int get_common_info(struct pevent *pevent,
3368 const char *type, int *offset, int *size)
3370 struct event_format *event;
3371 struct format_field *field;
3374 * All events should have the same common elements.
3375 * Pick any event to find where the type is;
3377 if (!pevent->events) {
3378 do_warning("no event_list!");
3382 event = pevent->events[0];
3383 field = pevent_find_common_field(event, type);
3387 *offset = field->offset;
3388 *size = field->size;
3393 static int __parse_common(struct pevent *pevent, void *data,
3394 int *size, int *offset, const char *name)
3399 ret = get_common_info(pevent, name, offset, size);
3403 return pevent_read_number(pevent, data + *offset, *size);
3406 static int trace_parse_common_type(struct pevent *pevent, void *data)
3408 return __parse_common(pevent, data,
3409 &pevent->type_size, &pevent->type_offset,
3413 static int parse_common_pid(struct pevent *pevent, void *data)
3415 return __parse_common(pevent, data,
3416 &pevent->pid_size, &pevent->pid_offset,
3420 static int parse_common_pc(struct pevent *pevent, void *data)
3422 return __parse_common(pevent, data,
3423 &pevent->pc_size, &pevent->pc_offset,
3424 "common_preempt_count");
3427 static int parse_common_flags(struct pevent *pevent, void *data)
3429 return __parse_common(pevent, data,
3430 &pevent->flags_size, &pevent->flags_offset,
3434 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3436 return __parse_common(pevent, data,
3437 &pevent->ld_size, &pevent->ld_offset,
3438 "common_lock_depth");
3441 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3443 return __parse_common(pevent, data,
3444 &pevent->ld_size, &pevent->ld_offset,
3445 "common_migrate_disable");
3448 static int events_id_cmp(const void *a, const void *b);
3451 * pevent_find_event - find an event by given id
3452 * @pevent: a handle to the pevent
3453 * @id: the id of the event
3455 * Returns an event that has a given @id.
3457 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3459 struct event_format **eventptr;
3460 struct event_format key;
3461 struct event_format *pkey = &key;
3463 /* Check cache first */
3464 if (pevent->last_event && pevent->last_event->id == id)
3465 return pevent->last_event;
3469 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3470 sizeof(*pevent->events), events_id_cmp);
3473 pevent->last_event = *eventptr;
3481 * pevent_find_event_by_name - find an event by given name
3482 * @pevent: a handle to the pevent
3483 * @sys: the system name to search for
3484 * @name: the name of the event to search for
3486 * This returns an event with a given @name and under the system
3487 * @sys. If @sys is NULL the first event with @name is returned.
3489 struct event_format *
3490 pevent_find_event_by_name(struct pevent *pevent,
3491 const char *sys, const char *name)
3493 struct event_format *event;
3496 if (pevent->last_event &&
3497 strcmp(pevent->last_event->name, name) == 0 &&
3498 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3499 return pevent->last_event;
3501 for (i = 0; i < pevent->nr_events; i++) {
3502 event = pevent->events[i];
3503 if (strcmp(event->name, name) == 0) {
3506 if (strcmp(event->system, sys) == 0)
3510 if (i == pevent->nr_events)
3513 pevent->last_event = event;
3517 static unsigned long long
3518 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3520 struct pevent *pevent = event->pevent;
3521 unsigned long long val = 0;
3522 unsigned long long left, right;
3523 struct print_arg *typearg = NULL;
3524 struct print_arg *larg;
3525 unsigned long offset;
3526 unsigned int field_size;
3528 switch (arg->type) {
3533 return strtoull(arg->atom.atom, NULL, 0);
3535 if (!arg->field.field) {
3536 arg->field.field = pevent_find_any_field(event, arg->field.name);
3537 if (!arg->field.field)
3538 goto out_warning_field;
3541 /* must be a number */
3542 val = pevent_read_number(pevent, data + arg->field.field->offset,
3543 arg->field.field->size);
3547 case PRINT_INT_ARRAY:
3551 val = eval_num_arg(data, size, event, arg->typecast.item);
3552 return eval_type(val, arg, 0);
3560 val = process_defined_func(&s, data, size, event, arg);
3561 trace_seq_destroy(&s);
3565 if (strcmp(arg->op.op, "[") == 0) {
3567 * Arrays are special, since we don't want
3568 * to read the arg as is.
3570 right = eval_num_arg(data, size, event, arg->op.right);
3572 /* handle typecasts */
3573 larg = arg->op.left;
3574 while (larg->type == PRINT_TYPE) {
3577 larg = larg->typecast.item;
3580 /* Default to long size */
3581 field_size = pevent->long_size;
3583 switch (larg->type) {
3584 case PRINT_DYNAMIC_ARRAY:
3585 offset = pevent_read_number(pevent,
3586 data + larg->dynarray.field->offset,
3587 larg->dynarray.field->size);
3588 if (larg->dynarray.field->elementsize)
3589 field_size = larg->dynarray.field->elementsize;
3591 * The actual length of the dynamic array is stored
3592 * in the top half of the field, and the offset
3593 * is in the bottom half of the 32 bit field.
3599 if (!larg->field.field) {
3601 pevent_find_any_field(event, larg->field.name);
3602 if (!larg->field.field) {
3604 goto out_warning_field;
3607 field_size = larg->field.field->elementsize;
3608 offset = larg->field.field->offset +
3609 right * larg->field.field->elementsize;
3612 goto default_op; /* oops, all bets off */
3614 val = pevent_read_number(pevent,
3615 data + offset, field_size);
3617 val = eval_type(val, typearg, 1);
3619 } else if (strcmp(arg->op.op, "?") == 0) {
3620 left = eval_num_arg(data, size, event, arg->op.left);
3621 arg = arg->op.right;
3623 val = eval_num_arg(data, size, event, arg->op.left);
3625 val = eval_num_arg(data, size, event, arg->op.right);
3629 left = eval_num_arg(data, size, event, arg->op.left);
3630 right = eval_num_arg(data, size, event, arg->op.right);
3631 switch (arg->op.op[0]) {
3633 switch (arg->op.op[1]) {
3638 val = left != right;
3641 goto out_warning_op;
3649 val = left || right;
3655 val = left && right;
3660 switch (arg->op.op[1]) {
3665 val = left << right;
3668 val = left <= right;
3671 goto out_warning_op;
3675 switch (arg->op.op[1]) {
3680 val = left >> right;
3683 val = left >= right;
3686 goto out_warning_op;
3690 if (arg->op.op[1] != '=')
3691 goto out_warning_op;
3693 val = left == right;
3711 goto out_warning_op;
3714 case PRINT_DYNAMIC_ARRAY_LEN:
3715 offset = pevent_read_number(pevent,
3716 data + arg->dynarray.field->offset,
3717 arg->dynarray.field->size);
3719 * The total allocated length of the dynamic array is
3720 * stored in the top half of the field, and the offset
3721 * is in the bottom half of the 32 bit field.
3723 val = (unsigned long long)(offset >> 16);
3725 case PRINT_DYNAMIC_ARRAY:
3726 /* Without [], we pass the address to the dynamic data */
3727 offset = pevent_read_number(pevent,
3728 data + arg->dynarray.field->offset,
3729 arg->dynarray.field->size);
3731 * The total allocated length of the dynamic array is
3732 * stored in the top half of the field, and the offset
3733 * is in the bottom half of the 32 bit field.
3736 val = (unsigned long long)((unsigned long)data + offset);
3738 default: /* not sure what to do there */
3744 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3748 do_warning_event(event, "%s: field %s not found",
3749 __func__, arg->field.name);
3755 unsigned long long value;
3758 static const struct flag flags[] = {
3759 { "HI_SOFTIRQ", 0 },
3760 { "TIMER_SOFTIRQ", 1 },
3761 { "NET_TX_SOFTIRQ", 2 },
3762 { "NET_RX_SOFTIRQ", 3 },
3763 { "BLOCK_SOFTIRQ", 4 },
3764 { "IRQ_POLL_SOFTIRQ", 5 },
3765 { "TASKLET_SOFTIRQ", 6 },
3766 { "SCHED_SOFTIRQ", 7 },
3767 { "HRTIMER_SOFTIRQ", 8 },
3768 { "RCU_SOFTIRQ", 9 },
3770 { "HRTIMER_NORESTART", 0 },
3771 { "HRTIMER_RESTART", 1 },
3774 static long long eval_flag(const char *flag)
3779 * Some flags in the format files do not get converted.
3780 * If the flag is not numeric, see if it is something that
3781 * we already know about.
3783 if (isdigit(flag[0]))
3784 return strtoull(flag, NULL, 0);
3786 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3787 if (strcmp(flags[i].name, flag) == 0)
3788 return flags[i].value;
3793 static void print_str_to_seq(struct trace_seq *s, const char *format,
3794 int len_arg, const char *str)
3797 trace_seq_printf(s, format, len_arg, str);
3799 trace_seq_printf(s, format, str);
3802 static void print_bitmask_to_seq(struct pevent *pevent,
3803 struct trace_seq *s, const char *format,
3804 int len_arg, const void *data, int size)
3806 int nr_bits = size * 8;
3807 int str_size = (nr_bits + 3) / 4;
3815 * The kernel likes to put in commas every 32 bits, we
3818 str_size += (nr_bits - 1) / 32;
3820 str = malloc(str_size + 1);
3822 do_warning("%s: not enough memory!", __func__);
3827 /* Start out with -2 for the two chars per byte */
3828 for (i = str_size - 2; i >= 0; i -= 2) {
3830 * data points to a bit mask of size bytes.
3831 * In the kernel, this is an array of long words, thus
3832 * endianess is very important.
3834 if (pevent->file_bigendian)
3835 index = size - (len + 1);
3839 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3840 memcpy(str + i, buf, 2);
3842 if (!(len & 3) && i > 0) {
3849 trace_seq_printf(s, format, len_arg, str);
3851 trace_seq_printf(s, format, str);
3856 static void print_str_arg(struct trace_seq *s, void *data, int size,
3857 struct event_format *event, const char *format,
3858 int len_arg, struct print_arg *arg)
3860 struct pevent *pevent = event->pevent;
3861 struct print_flag_sym *flag;
3862 struct format_field *field;
3863 struct printk_map *printk;
3864 long long val, fval;
3865 unsigned long long addr;
3871 switch (arg->type) {
3876 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3879 field = arg->field.field;
3881 field = pevent_find_any_field(event, arg->field.name);
3883 str = arg->field.name;
3884 goto out_warning_field;
3886 arg->field.field = field;
3888 /* Zero sized fields, mean the rest of the data */
3889 len = field->size ? : size - field->offset;
3892 * Some events pass in pointers. If this is not an array
3893 * and the size is the same as long_size, assume that it
3896 if (!(field->flags & FIELD_IS_ARRAY) &&
3897 field->size == pevent->long_size) {
3899 /* Handle heterogeneous recording and processing
3903 * Traces recorded on 32-bit devices (32-bit
3904 * addressing) and processed on 64-bit devices:
3905 * In this case, only 32 bits should be read.
3908 * Traces recorded on 64 bit devices and processed
3909 * on 32-bit devices:
3910 * In this case, 64 bits must be read.
3912 addr = (pevent->long_size == 8) ?
3913 *(unsigned long long *)(data + field->offset) :
3914 (unsigned long long)*(unsigned int *)(data + field->offset);
3916 /* Check if it matches a print format */
3917 printk = find_printk(pevent, addr);
3919 trace_seq_puts(s, printk->printk);
3921 trace_seq_printf(s, "%llx", addr);
3924 str = malloc(len + 1);
3926 do_warning_event(event, "%s: not enough memory!",
3930 memcpy(str, data + field->offset, len);
3932 print_str_to_seq(s, format, len_arg, str);
3936 val = eval_num_arg(data, size, event, arg->flags.field);
3938 for (flag = arg->flags.flags; flag; flag = flag->next) {
3939 fval = eval_flag(flag->value);
3940 if (!val && fval < 0) {
3941 print_str_to_seq(s, format, len_arg, flag->str);
3944 if (fval > 0 && (val & fval) == fval) {
3945 if (print && arg->flags.delim)
3946 trace_seq_puts(s, arg->flags.delim);
3947 print_str_to_seq(s, format, len_arg, flag->str);
3954 val = eval_num_arg(data, size, event, arg->symbol.field);
3955 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3956 fval = eval_flag(flag->value);
3958 print_str_to_seq(s, format, len_arg, flag->str);
3964 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3965 unsigned long offset;
3966 offset = pevent_read_number(pevent,
3967 data + arg->hex.field->dynarray.field->offset,
3968 arg->hex.field->dynarray.field->size);
3969 hex = data + (offset & 0xffff);
3971 field = arg->hex.field->field.field;
3973 str = arg->hex.field->field.name;
3974 field = pevent_find_any_field(event, str);
3976 goto out_warning_field;
3977 arg->hex.field->field.field = field;
3979 hex = data + field->offset;
3981 len = eval_num_arg(data, size, event, arg->hex.size);
3982 for (i = 0; i < len; i++) {
3984 trace_seq_putc(s, ' ');
3985 trace_seq_printf(s, "%02x", hex[i]);
3989 case PRINT_INT_ARRAY: {
3993 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3994 unsigned long offset;
3995 struct format_field *field =
3996 arg->int_array.field->dynarray.field;
3997 offset = pevent_read_number(pevent,
3998 data + field->offset,
4000 num = data + (offset & 0xffff);
4002 field = arg->int_array.field->field.field;
4004 str = arg->int_array.field->field.name;
4005 field = pevent_find_any_field(event, str);
4007 goto out_warning_field;
4008 arg->int_array.field->field.field = field;
4010 num = data + field->offset;
4012 len = eval_num_arg(data, size, event, arg->int_array.count);
4013 el_size = eval_num_arg(data, size, event,
4014 arg->int_array.el_size);
4015 for (i = 0; i < len; i++) {
4017 trace_seq_putc(s, ' ');
4020 trace_seq_printf(s, "%u", *(uint8_t *)num);
4021 } else if (el_size == 2) {
4022 trace_seq_printf(s, "%u", *(uint16_t *)num);
4023 } else if (el_size == 4) {
4024 trace_seq_printf(s, "%u", *(uint32_t *)num);
4025 } else if (el_size == 8) {
4026 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4028 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4029 el_size, *(uint8_t *)num);
4039 case PRINT_STRING: {
4042 if (arg->string.offset == -1) {
4043 struct format_field *f;
4045 f = pevent_find_any_field(event, arg->string.string);
4046 arg->string.offset = f->offset;
4048 str_offset = data2host4(pevent, data + arg->string.offset);
4049 str_offset &= 0xffff;
4050 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4054 print_str_to_seq(s, format, len_arg, arg->string.string);
4056 case PRINT_BITMASK: {
4060 if (arg->bitmask.offset == -1) {
4061 struct format_field *f;
4063 f = pevent_find_any_field(event, arg->bitmask.bitmask);
4064 arg->bitmask.offset = f->offset;
4066 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4067 bitmask_size = bitmask_offset >> 16;
4068 bitmask_offset &= 0xffff;
4069 print_bitmask_to_seq(pevent, s, format, len_arg,
4070 data + bitmask_offset, bitmask_size);
4075 * The only op for string should be ? :
4077 if (arg->op.op[0] != '?')
4079 val = eval_num_arg(data, size, event, arg->op.left);
4081 print_str_arg(s, data, size, event,
4082 format, len_arg, arg->op.right->op.left);
4084 print_str_arg(s, data, size, event,
4085 format, len_arg, arg->op.right->op.right);
4088 process_defined_func(s, data, size, event, arg);
4098 do_warning_event(event, "%s: field %s not found",
4099 __func__, arg->field.name);
4102 static unsigned long long
4103 process_defined_func(struct trace_seq *s, void *data, int size,
4104 struct event_format *event, struct print_arg *arg)
4106 struct pevent_function_handler *func_handle = arg->func.func;
4107 struct pevent_func_params *param;
4108 unsigned long long *args;
4109 unsigned long long ret;
4110 struct print_arg *farg;
4111 struct trace_seq str;
4113 struct save_str *next;
4115 } *strings = NULL, *string;
4118 if (!func_handle->nr_args) {
4119 ret = (*func_handle->func)(s, NULL);
4123 farg = arg->func.args;
4124 param = func_handle->params;
4127 args = malloc(sizeof(*args) * func_handle->nr_args);
4131 for (i = 0; i < func_handle->nr_args; i++) {
4132 switch (param->type) {
4133 case PEVENT_FUNC_ARG_INT:
4134 case PEVENT_FUNC_ARG_LONG:
4135 case PEVENT_FUNC_ARG_PTR:
4136 args[i] = eval_num_arg(data, size, event, farg);
4138 case PEVENT_FUNC_ARG_STRING:
4139 trace_seq_init(&str);
4140 print_str_arg(&str, data, size, event, "%s", -1, farg);
4141 trace_seq_terminate(&str);
4142 string = malloc(sizeof(*string));
4144 do_warning_event(event, "%s(%d): malloc str",
4145 __func__, __LINE__);
4148 string->next = strings;
4149 string->str = strdup(str.buffer);
4152 do_warning_event(event, "%s(%d): malloc str",
4153 __func__, __LINE__);
4156 args[i] = (uintptr_t)string->str;
4158 trace_seq_destroy(&str);
4162 * Something went totally wrong, this is not
4163 * an input error, something in this code broke.
4165 do_warning_event(event, "Unexpected end of arguments\n");
4169 param = param->next;
4172 ret = (*func_handle->func)(s, args);
4177 strings = string->next;
4183 /* TBD : handle return type here */
4187 static void free_args(struct print_arg *args)
4189 struct print_arg *next;
4199 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4201 struct pevent *pevent = event->pevent;
4202 struct format_field *field, *ip_field;
4203 struct print_arg *args, *arg, **next;
4204 unsigned long long ip, val;
4209 field = pevent->bprint_buf_field;
4210 ip_field = pevent->bprint_ip_field;
4213 field = pevent_find_field(event, "buf");
4215 do_warning_event(event, "can't find buffer field for binary printk");
4218 ip_field = pevent_find_field(event, "ip");
4220 do_warning_event(event, "can't find ip field for binary printk");
4223 pevent->bprint_buf_field = field;
4224 pevent->bprint_ip_field = ip_field;
4227 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4230 * The first arg is the IP pointer.
4234 do_warning_event(event, "%s(%d): not enough memory!",
4235 __func__, __LINE__);
4242 arg->type = PRINT_ATOM;
4244 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4247 /* skip the first "%ps: " */
4248 for (ptr = fmt + 5, bptr = data + field->offset;
4249 bptr < data + size && *ptr; ptr++) {
4284 vsize = pevent->long_size;
4298 /* the pointers are always 4 bytes aligned */
4299 bptr = (void *)(((unsigned long)bptr + 3) &
4301 val = pevent_read_number(pevent, bptr, vsize);
4305 do_warning_event(event, "%s(%d): not enough memory!",
4306 __func__, __LINE__);
4310 arg->type = PRINT_ATOM;
4311 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4318 * The '*' case means that an arg is used as the length.
4319 * We need to continue to figure out for what.
4328 do_warning_event(event, "%s(%d): not enough memory!",
4329 __func__, __LINE__);
4333 arg->type = PRINT_BSTRING;
4334 arg->string.string = strdup(bptr);
4335 if (!arg->string.string)
4337 bptr += strlen(bptr) + 1;
4354 get_bprint_format(void *data, int size __maybe_unused,
4355 struct event_format *event)
4357 struct pevent *pevent = event->pevent;
4358 unsigned long long addr;
4359 struct format_field *field;
4360 struct printk_map *printk;
4363 field = pevent->bprint_fmt_field;
4366 field = pevent_find_field(event, "fmt");
4368 do_warning_event(event, "can't find format field for binary printk");
4371 pevent->bprint_fmt_field = field;
4374 addr = pevent_read_number(pevent, data + field->offset, field->size);
4376 printk = find_printk(pevent, addr);
4378 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4383 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4389 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4390 struct event_format *event, struct print_arg *arg)
4393 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4395 if (arg->type == PRINT_FUNC) {
4396 process_defined_func(s, data, size, event, arg);
4400 if (arg->type != PRINT_FIELD) {
4401 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4407 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4408 if (!arg->field.field) {
4410 pevent_find_any_field(event, arg->field.name);
4411 if (!arg->field.field) {
4412 do_warning_event(event, "%s: field %s not found",
4413 __func__, arg->field.name);
4417 if (arg->field.field->size != 6) {
4418 trace_seq_printf(s, "INVALIDMAC");
4421 buf = data + arg->field.field->offset;
4422 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4425 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4430 fmt = "%03d.%03d.%03d.%03d";
4432 fmt = "%d.%d.%d.%d";
4434 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4437 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4439 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4440 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4443 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4445 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4448 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4451 unsigned char zerolength[8];
4456 bool needcolon = false;
4458 struct in6_addr in6;
4460 memcpy(&in6, addr, sizeof(struct in6_addr));
4462 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4464 memset(zerolength, 0, sizeof(zerolength));
4471 /* find position of longest 0 run */
4472 for (i = 0; i < range; i++) {
4473 for (j = i; j < range; j++) {
4474 if (in6.s6_addr16[j] != 0)
4479 for (i = 0; i < range; i++) {
4480 if (zerolength[i] > longest) {
4481 longest = zerolength[i];
4485 if (longest == 1) /* don't compress a single 0 */
4489 for (i = 0; i < range; i++) {
4490 if (i == colonpos) {
4491 if (needcolon || i == 0)
4492 trace_seq_printf(s, ":");
4493 trace_seq_printf(s, ":");
4499 trace_seq_printf(s, ":");
4502 /* hex u16 without leading 0s */
4503 word = ntohs(in6.s6_addr16[i]);
4507 trace_seq_printf(s, "%x%02x", hi, lo);
4509 trace_seq_printf(s, "%x", lo);
4516 trace_seq_printf(s, ":");
4517 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4523 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4527 for (j = 0; j < 16; j += 2) {
4528 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4529 if (i == 'I' && j < 14)
4530 trace_seq_printf(s, ":");
4535 * %pi4 print an IPv4 address with leading zeros
4536 * %pI4 print an IPv4 address without leading zeros
4537 * %pi6 print an IPv6 address without colons
4538 * %pI6 print an IPv6 address with colons
4539 * %pI6c print an IPv6 address in compressed form with colons
4540 * %pISpc print an IP address based on sockaddr; p adds port.
4542 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4543 void *data, int size, struct event_format *event,
4544 struct print_arg *arg)
4548 if (arg->type == PRINT_FUNC) {
4549 process_defined_func(s, data, size, event, arg);
4553 if (arg->type != PRINT_FIELD) {
4554 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4558 if (!arg->field.field) {
4560 pevent_find_any_field(event, arg->field.name);
4561 if (!arg->field.field) {
4562 do_warning("%s: field %s not found",
4563 __func__, arg->field.name);
4568 buf = data + arg->field.field->offset;
4570 if (arg->field.field->size != 4) {
4571 trace_seq_printf(s, "INVALIDIPv4");
4574 print_ip4_addr(s, i, buf);
4579 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4580 void *data, int size, struct event_format *event,
4581 struct print_arg *arg)
4588 if (i == 'I' && *ptr == 'c') {
4594 if (arg->type == PRINT_FUNC) {
4595 process_defined_func(s, data, size, event, arg);
4599 if (arg->type != PRINT_FIELD) {
4600 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4604 if (!arg->field.field) {
4606 pevent_find_any_field(event, arg->field.name);
4607 if (!arg->field.field) {
4608 do_warning("%s: field %s not found",
4609 __func__, arg->field.name);
4614 buf = data + arg->field.field->offset;
4616 if (arg->field.field->size != 16) {
4617 trace_seq_printf(s, "INVALIDIPv6");
4622 print_ip6c_addr(s, buf);
4624 print_ip6_addr(s, i, buf);
4629 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4630 void *data, int size, struct event_format *event,
4631 struct print_arg *arg)
4633 char have_c = 0, have_p = 0;
4635 struct sockaddr_storage *sa;
4652 if (arg->type == PRINT_FUNC) {
4653 process_defined_func(s, data, size, event, arg);
4657 if (arg->type != PRINT_FIELD) {
4658 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4662 if (!arg->field.field) {
4664 pevent_find_any_field(event, arg->field.name);
4665 if (!arg->field.field) {
4666 do_warning("%s: field %s not found",
4667 __func__, arg->field.name);
4672 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4674 if (sa->ss_family == AF_INET) {
4675 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4677 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4678 trace_seq_printf(s, "INVALIDIPv4");
4682 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4684 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4687 } else if (sa->ss_family == AF_INET6) {
4688 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4690 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4691 trace_seq_printf(s, "INVALIDIPv6");
4696 trace_seq_printf(s, "[");
4698 buf = (unsigned char *) &sa6->sin6_addr;
4700 print_ip6c_addr(s, buf);
4702 print_ip6_addr(s, i, buf);
4705 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4711 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4712 void *data, int size, struct event_format *event,
4713 struct print_arg *arg)
4715 char i = *ptr; /* 'i' or 'I' */
4728 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4731 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4734 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4743 static int is_printable_array(char *p, unsigned int len)
4747 for (i = 0; i < len && p[i]; i++)
4748 if (!isprint(p[i]) && !isspace(p[i]))
4753 void pevent_print_field(struct trace_seq *s, void *data,
4754 struct format_field *field)
4756 unsigned long long val;
4757 unsigned int offset, len, i;
4758 struct pevent *pevent = field->event->pevent;
4760 if (field->flags & FIELD_IS_ARRAY) {
4761 offset = field->offset;
4763 if (field->flags & FIELD_IS_DYNAMIC) {
4764 val = pevent_read_number(pevent, data + offset, len);
4769 if (field->flags & FIELD_IS_STRING &&
4770 is_printable_array(data + offset, len)) {
4771 trace_seq_printf(s, "%s", (char *)data + offset);
4773 trace_seq_puts(s, "ARRAY[");
4774 for (i = 0; i < len; i++) {
4776 trace_seq_puts(s, ", ");
4777 trace_seq_printf(s, "%02x",
4778 *((unsigned char *)data + offset + i));
4780 trace_seq_putc(s, ']');
4781 field->flags &= ~FIELD_IS_STRING;
4784 val = pevent_read_number(pevent, data + field->offset,
4786 if (field->flags & FIELD_IS_POINTER) {
4787 trace_seq_printf(s, "0x%llx", val);
4788 } else if (field->flags & FIELD_IS_SIGNED) {
4789 switch (field->size) {
4792 * If field is long then print it in hex.
4793 * A long usually stores pointers.
4795 if (field->flags & FIELD_IS_LONG)
4796 trace_seq_printf(s, "0x%x", (int)val);
4798 trace_seq_printf(s, "%d", (int)val);
4801 trace_seq_printf(s, "%2d", (short)val);
4804 trace_seq_printf(s, "%1d", (char)val);
4807 trace_seq_printf(s, "%lld", val);
4810 if (field->flags & FIELD_IS_LONG)
4811 trace_seq_printf(s, "0x%llx", val);
4813 trace_seq_printf(s, "%llu", val);
4818 void pevent_print_fields(struct trace_seq *s, void *data,
4819 int size __maybe_unused, struct event_format *event)
4821 struct format_field *field;
4823 field = event->format.fields;
4825 trace_seq_printf(s, " %s=", field->name);
4826 pevent_print_field(s, data, field);
4827 field = field->next;
4831 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4833 struct pevent *pevent = event->pevent;
4834 struct print_fmt *print_fmt = &event->print_fmt;
4835 struct print_arg *arg = print_fmt->args;
4836 struct print_arg *args = NULL;
4837 const char *ptr = print_fmt->format;
4838 unsigned long long val;
4839 struct func_map *func;
4840 const char *saveptr;
4842 char *bprint_fmt = NULL;
4850 if (event->flags & EVENT_FL_FAILED) {
4851 trace_seq_printf(s, "[FAILED TO PARSE]");
4852 pevent_print_fields(s, data, size, event);
4856 if (event->flags & EVENT_FL_ISBPRINT) {
4857 bprint_fmt = get_bprint_format(data, size, event);
4858 args = make_bprint_args(bprint_fmt, data, size, event);
4863 for (; *ptr; ptr++) {
4869 trace_seq_putc(s, '\n');
4872 trace_seq_putc(s, '\t');
4875 trace_seq_putc(s, '\r');
4878 trace_seq_putc(s, '\\');
4881 trace_seq_putc(s, *ptr);
4885 } else if (*ptr == '%') {
4893 trace_seq_putc(s, '%');
4896 /* FIXME: need to handle properly */
4908 /* The argument is the length. */
4910 do_warning_event(event, "no argument match");
4911 event->flags |= EVENT_FL_FAILED;
4914 len_arg = eval_num_arg(data, size, event, arg);
4925 if (pevent->long_size == 4)
4930 if (isalnum(ptr[1]))
4933 if (*ptr == 'F' || *ptr == 'f' ||
4934 *ptr == 'S' || *ptr == 's') {
4936 } else if (*ptr == 'M' || *ptr == 'm') {
4937 print_mac_arg(s, *ptr, data, size, event, arg);
4940 } else if (*ptr == 'I' || *ptr == 'i') {
4943 n = print_ip_arg(s, ptr, data, size, event, arg);
4958 do_warning_event(event, "no argument match");
4959 event->flags |= EVENT_FL_FAILED;
4963 len = ((unsigned long)ptr + 1) -
4964 (unsigned long)saveptr;
4966 /* should never happen */
4968 do_warning_event(event, "bad format!");
4969 event->flags |= EVENT_FL_FAILED;
4973 memcpy(format, saveptr, len);
4976 val = eval_num_arg(data, size, event, arg);
4980 func = find_func(pevent, val);
4982 trace_seq_puts(s, func->func);
4983 if (show_func == 'F')
4990 if (pevent->long_size == 8 && ls == 1 &&
4991 sizeof(long) != 8) {
4994 /* make %l into %ll */
4995 if (ls == 1 && (p = strchr(format, 'l')))
4996 memmove(p+1, p, strlen(p)+1);
4997 else if (strcmp(format, "%p") == 0)
4998 strcpy(format, "0x%llx");
5004 trace_seq_printf(s, format, len_arg, (char)val);
5006 trace_seq_printf(s, format, (char)val);
5010 trace_seq_printf(s, format, len_arg, (short)val);
5012 trace_seq_printf(s, format, (short)val);
5016 trace_seq_printf(s, format, len_arg, (int)val);
5018 trace_seq_printf(s, format, (int)val);
5022 trace_seq_printf(s, format, len_arg, (long)val);
5024 trace_seq_printf(s, format, (long)val);
5028 trace_seq_printf(s, format, len_arg,
5031 trace_seq_printf(s, format, (long long)val);
5034 do_warning_event(event, "bad count (%d)", ls);
5035 event->flags |= EVENT_FL_FAILED;
5040 do_warning_event(event, "no matching argument");
5041 event->flags |= EVENT_FL_FAILED;
5045 len = ((unsigned long)ptr + 1) -
5046 (unsigned long)saveptr;
5048 /* should never happen */
5050 do_warning_event(event, "bad format!");
5051 event->flags |= EVENT_FL_FAILED;
5055 memcpy(format, saveptr, len);
5059 /* Use helper trace_seq */
5061 print_str_arg(&p, data, size, event,
5062 format, len_arg, arg);
5063 trace_seq_terminate(&p);
5064 trace_seq_puts(s, p.buffer);
5065 trace_seq_destroy(&p);
5069 trace_seq_printf(s, ">%c<", *ptr);
5073 trace_seq_putc(s, *ptr);
5076 if (event->flags & EVENT_FL_FAILED) {
5078 trace_seq_printf(s, "[FAILED TO PARSE]");
5088 * pevent_data_lat_fmt - parse the data for the latency format
5089 * @pevent: a handle to the pevent
5090 * @s: the trace_seq to write to
5091 * @record: the record to read from
5093 * This parses out the Latency format (interrupts disabled,
5094 * need rescheduling, in hard/soft interrupt, preempt count
5095 * and lock depth) and places it into the trace_seq.
5097 void pevent_data_lat_fmt(struct pevent *pevent,
5098 struct trace_seq *s, struct pevent_record *record)
5100 static int check_lock_depth = 1;
5101 static int check_migrate_disable = 1;
5102 static int lock_depth_exists;
5103 static int migrate_disable_exists;
5104 unsigned int lat_flags;
5107 int migrate_disable;
5110 void *data = record->data;
5112 lat_flags = parse_common_flags(pevent, data);
5113 pc = parse_common_pc(pevent, data);
5114 /* lock_depth may not always exist */
5115 if (lock_depth_exists)
5116 lock_depth = parse_common_lock_depth(pevent, data);
5117 else if (check_lock_depth) {
5118 lock_depth = parse_common_lock_depth(pevent, data);
5120 check_lock_depth = 0;
5122 lock_depth_exists = 1;
5125 /* migrate_disable may not always exist */
5126 if (migrate_disable_exists)
5127 migrate_disable = parse_common_migrate_disable(pevent, data);
5128 else if (check_migrate_disable) {
5129 migrate_disable = parse_common_migrate_disable(pevent, data);
5130 if (migrate_disable < 0)
5131 check_migrate_disable = 0;
5133 migrate_disable_exists = 1;
5136 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5137 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5139 trace_seq_printf(s, "%c%c%c",
5140 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5141 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5143 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5145 (hardirq && softirq) ? 'H' :
5146 hardirq ? 'h' : softirq ? 's' : '.');
5149 trace_seq_printf(s, "%x", pc);
5151 trace_seq_putc(s, '.');
5153 if (migrate_disable_exists) {
5154 if (migrate_disable < 0)
5155 trace_seq_putc(s, '.');
5157 trace_seq_printf(s, "%d", migrate_disable);
5160 if (lock_depth_exists) {
5162 trace_seq_putc(s, '.');
5164 trace_seq_printf(s, "%d", lock_depth);
5167 trace_seq_terminate(s);
5171 * pevent_data_type - parse out the given event type
5172 * @pevent: a handle to the pevent
5173 * @rec: the record to read from
5175 * This returns the event id from the @rec.
5177 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5179 return trace_parse_common_type(pevent, rec->data);
5183 * pevent_data_event_from_type - find the event by a given type
5184 * @pevent: a handle to the pevent
5185 * @type: the type of the event.
5187 * This returns the event form a given @type;
5189 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5191 return pevent_find_event(pevent, type);
5195 * pevent_data_pid - parse the PID from raw data
5196 * @pevent: a handle to the pevent
5197 * @rec: the record to parse
5199 * This returns the PID from a raw data.
5201 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5203 return parse_common_pid(pevent, rec->data);
5207 * pevent_data_comm_from_pid - return the command line from PID
5208 * @pevent: a handle to the pevent
5209 * @pid: the PID of the task to search for
5211 * This returns a pointer to the command line that has the given
5214 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5218 comm = find_cmdline(pevent, pid);
5222 static struct cmdline *
5223 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5225 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5228 cmdlist = cmdlist->next;
5230 cmdlist = pevent->cmdlist;
5232 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5233 cmdlist = cmdlist->next;
5235 return (struct cmdline *)cmdlist;
5239 * pevent_data_pid_from_comm - return the pid from a given comm
5240 * @pevent: a handle to the pevent
5241 * @comm: the cmdline to find the pid from
5242 * @next: the cmdline structure to find the next comm
5244 * This returns the cmdline structure that holds a pid for a given
5245 * comm, or NULL if none found. As there may be more than one pid for
5246 * a given comm, the result of this call can be passed back into
5247 * a recurring call in the @next paramater, and then it will find the
5249 * Also, it does a linear seach, so it may be slow.
5251 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5252 struct cmdline *next)
5254 struct cmdline *cmdline;
5257 * If the cmdlines have not been converted yet, then use
5260 if (!pevent->cmdlines)
5261 return pid_from_cmdlist(pevent, comm, next);
5265 * The next pointer could have been still from
5266 * a previous call before cmdlines were created
5268 if (next < pevent->cmdlines ||
5269 next >= pevent->cmdlines + pevent->cmdline_count)
5276 cmdline = pevent->cmdlines;
5278 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5279 if (strcmp(cmdline->comm, comm) == 0)
5287 * pevent_cmdline_pid - return the pid associated to a given cmdline
5288 * @cmdline: The cmdline structure to get the pid from
5290 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5293 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5295 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5301 * If cmdlines have not been created yet, or cmdline is
5302 * not part of the array, then treat it as a cmdlist instead.
5304 if (!pevent->cmdlines ||
5305 cmdline < pevent->cmdlines ||
5306 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5307 return cmdlist->pid;
5309 return cmdline->pid;
5313 * pevent_data_comm_from_pid - parse the data into the print format
5314 * @s: the trace_seq to write to
5315 * @event: the handle to the event
5316 * @record: the record to read from
5318 * This parses the raw @data using the given @event information and
5319 * writes the print format into the trace_seq.
5321 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5322 struct pevent_record *record)
5324 int print_pretty = 1;
5326 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5327 pevent_print_fields(s, record->data, record->size, event);
5330 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5331 print_pretty = event->handler(s, record, event,
5335 pretty_print(s, record->data, record->size, event);
5338 trace_seq_terminate(s);
5341 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5343 if (!use_trace_clock)
5346 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5347 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5350 /* trace_clock is setting in tsc or counter mode */
5355 * pevent_find_event_by_record - return the event from a given record
5356 * @pevent: a handle to the pevent
5357 * @record: The record to get the event from
5359 * Returns the associated event for a given record, or NULL if non is
5362 struct event_format *
5363 pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
5367 if (record->size < 0) {
5368 do_warning("ug! negative record size %d", record->size);
5372 type = trace_parse_common_type(pevent, record->data);
5374 return pevent_find_event(pevent, type);
5378 * pevent_print_event_task - Write the event task comm, pid and CPU
5379 * @pevent: a handle to the pevent
5380 * @s: the trace_seq to write to
5381 * @event: the handle to the record's event
5382 * @record: The record to get the event from
5384 * Writes the tasks comm, pid and CPU to @s.
5386 void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
5387 struct event_format *event,
5388 struct pevent_record *record)
5390 void *data = record->data;
5394 pid = parse_common_pid(pevent, data);
5395 comm = find_cmdline(pevent, pid);
5397 if (pevent->latency_format) {
5398 trace_seq_printf(s, "%8.8s-%-5d %3d",
5399 comm, pid, record->cpu);
5401 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5405 * pevent_print_event_time - Write the event timestamp
5406 * @pevent: a handle to the pevent
5407 * @s: the trace_seq to write to
5408 * @event: the handle to the record's event
5409 * @record: The record to get the event from
5410 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5412 * Writes the timestamp of the record into @s.
5414 void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
5415 struct event_format *event,
5416 struct pevent_record *record,
5417 bool use_trace_clock)
5420 unsigned long usecs;
5421 unsigned long nsecs;
5423 bool use_usec_format;
5425 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5427 if (use_usec_format) {
5428 secs = record->ts / NSECS_PER_SEC;
5429 nsecs = record->ts - secs * NSECS_PER_SEC;
5432 if (pevent->latency_format) {
5433 pevent_data_lat_fmt(pevent, s, record);
5436 if (use_usec_format) {
5437 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5441 usecs = (nsecs + 500) / NSECS_PER_USEC;
5442 /* To avoid usecs larger than 1 sec */
5443 if (usecs >= 1000000) {
5450 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5452 trace_seq_printf(s, " %12llu:", record->ts);
5456 * pevent_print_event_data - Write the event data section
5457 * @pevent: a handle to the pevent
5458 * @s: the trace_seq to write to
5459 * @event: the handle to the record's event
5460 * @record: The record to get the event from
5462 * Writes the parsing of the record's data to @s.
5464 void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
5465 struct event_format *event,
5466 struct pevent_record *record)
5468 static const char *spaces = " "; /* 20 spaces */
5471 trace_seq_printf(s, " %s: ", event->name);
5473 /* Space out the event names evenly. */
5474 len = strlen(event->name);
5476 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5478 pevent_event_info(s, event, record);
5481 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5482 struct pevent_record *record, bool use_trace_clock)
5484 struct event_format *event;
5486 event = pevent_find_event_by_record(pevent, record);
5488 do_warning("ug! no event found for type %d",
5489 trace_parse_common_type(pevent, record->data));
5493 pevent_print_event_task(pevent, s, event, record);
5494 pevent_print_event_time(pevent, s, event, record, use_trace_clock);
5495 pevent_print_event_data(pevent, s, event, record);
5498 static int events_id_cmp(const void *a, const void *b)
5500 struct event_format * const * ea = a;
5501 struct event_format * const * eb = b;
5503 if ((*ea)->id < (*eb)->id)
5506 if ((*ea)->id > (*eb)->id)
5512 static int events_name_cmp(const void *a, const void *b)
5514 struct event_format * const * ea = a;
5515 struct event_format * const * eb = b;
5518 res = strcmp((*ea)->name, (*eb)->name);
5522 res = strcmp((*ea)->system, (*eb)->system);
5526 return events_id_cmp(a, b);
5529 static int events_system_cmp(const void *a, const void *b)
5531 struct event_format * const * ea = a;
5532 struct event_format * const * eb = b;
5535 res = strcmp((*ea)->system, (*eb)->system);
5539 res = strcmp((*ea)->name, (*eb)->name);
5543 return events_id_cmp(a, b);
5546 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5548 struct event_format **events;
5549 int (*sort)(const void *a, const void *b);
5551 events = pevent->sort_events;
5553 if (events && pevent->last_type == sort_type)
5557 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5561 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5562 events[pevent->nr_events] = NULL;
5564 pevent->sort_events = events;
5566 /* the internal events are sorted by id */
5567 if (sort_type == EVENT_SORT_ID) {
5568 pevent->last_type = sort_type;
5573 switch (sort_type) {
5575 sort = events_id_cmp;
5577 case EVENT_SORT_NAME:
5578 sort = events_name_cmp;
5580 case EVENT_SORT_SYSTEM:
5581 sort = events_system_cmp;
5587 qsort(events, pevent->nr_events, sizeof(*events), sort);
5588 pevent->last_type = sort_type;
5593 static struct format_field **
5594 get_event_fields(const char *type, const char *name,
5595 int count, struct format_field *list)
5597 struct format_field **fields;
5598 struct format_field *field;
5601 fields = malloc(sizeof(*fields) * (count + 1));
5605 for (field = list; field; field = field->next) {
5606 fields[i++] = field;
5607 if (i == count + 1) {
5608 do_warning("event %s has more %s fields than specified",
5616 do_warning("event %s has less %s fields than specified",
5625 * pevent_event_common_fields - return a list of common fields for an event
5626 * @event: the event to return the common fields of.
5628 * Returns an allocated array of fields. The last item in the array is NULL.
5629 * The array must be freed with free().
5631 struct format_field **pevent_event_common_fields(struct event_format *event)
5633 return get_event_fields("common", event->name,
5634 event->format.nr_common,
5635 event->format.common_fields);
5639 * pevent_event_fields - return a list of event specific fields for an event
5640 * @event: the event to return the fields of.
5642 * Returns an allocated array of fields. The last item in the array is NULL.
5643 * The array must be freed with free().
5645 struct format_field **pevent_event_fields(struct event_format *event)
5647 return get_event_fields("event", event->name,
5648 event->format.nr_fields,
5649 event->format.fields);
5652 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5654 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5656 trace_seq_puts(s, ", ");
5657 print_fields(s, field->next);
5662 static void print_args(struct print_arg *args)
5664 int print_paren = 1;
5667 switch (args->type) {
5672 printf("%s", args->atom.atom);
5675 printf("REC->%s", args->field.name);
5678 printf("__print_flags(");
5679 print_args(args->flags.field);
5680 printf(", %s, ", args->flags.delim);
5682 print_fields(&s, args->flags.flags);
5683 trace_seq_do_printf(&s);
5684 trace_seq_destroy(&s);
5688 printf("__print_symbolic(");
5689 print_args(args->symbol.field);
5692 print_fields(&s, args->symbol.symbols);
5693 trace_seq_do_printf(&s);
5694 trace_seq_destroy(&s);
5698 printf("__print_hex(");
5699 print_args(args->hex.field);
5701 print_args(args->hex.size);
5704 case PRINT_INT_ARRAY:
5705 printf("__print_array(");
5706 print_args(args->int_array.field);
5708 print_args(args->int_array.count);
5710 print_args(args->int_array.el_size);
5715 printf("__get_str(%s)", args->string.string);
5718 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5721 printf("(%s)", args->typecast.type);
5722 print_args(args->typecast.item);
5725 if (strcmp(args->op.op, ":") == 0)
5729 print_args(args->op.left);
5730 printf(" %s ", args->op.op);
5731 print_args(args->op.right);
5736 /* we should warn... */
5741 print_args(args->next);
5745 static void parse_header_field(const char *field,
5746 int *offset, int *size, int mandatory)
5748 unsigned long long save_input_buf_ptr;
5749 unsigned long long save_input_buf_siz;
5753 save_input_buf_ptr = input_buf_ptr;
5754 save_input_buf_siz = input_buf_siz;
5756 if (read_expected(EVENT_ITEM, "field") < 0)
5758 if (read_expected(EVENT_OP, ":") < 0)
5762 if (read_expect_type(EVENT_ITEM, &token) < 0)
5767 * If this is not a mandatory field, then test it first.
5770 if (read_expected(EVENT_ITEM, field) < 0)
5773 if (read_expect_type(EVENT_ITEM, &token) < 0)
5775 if (strcmp(token, field) != 0)
5780 if (read_expected(EVENT_OP, ";") < 0)
5782 if (read_expected(EVENT_ITEM, "offset") < 0)
5784 if (read_expected(EVENT_OP, ":") < 0)
5786 if (read_expect_type(EVENT_ITEM, &token) < 0)
5788 *offset = atoi(token);
5790 if (read_expected(EVENT_OP, ";") < 0)
5792 if (read_expected(EVENT_ITEM, "size") < 0)
5794 if (read_expected(EVENT_OP, ":") < 0)
5796 if (read_expect_type(EVENT_ITEM, &token) < 0)
5798 *size = atoi(token);
5800 if (read_expected(EVENT_OP, ";") < 0)
5802 type = read_token(&token);
5803 if (type != EVENT_NEWLINE) {
5804 /* newer versions of the kernel have a "signed" type */
5805 if (type != EVENT_ITEM)
5808 if (strcmp(token, "signed") != 0)
5813 if (read_expected(EVENT_OP, ":") < 0)
5816 if (read_expect_type(EVENT_ITEM, &token))
5820 if (read_expected(EVENT_OP, ";") < 0)
5823 if (read_expect_type(EVENT_NEWLINE, &token))
5831 input_buf_ptr = save_input_buf_ptr;
5832 input_buf_siz = save_input_buf_siz;
5839 * pevent_parse_header_page - parse the data stored in the header page
5840 * @pevent: the handle to the pevent
5841 * @buf: the buffer storing the header page format string
5842 * @size: the size of @buf
5843 * @long_size: the long size to use if there is no header
5845 * This parses the header page format for information on the
5846 * ring buffer used. The @buf should be copied from
5848 * /sys/kernel/debug/tracing/events/header_page
5850 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5857 * Old kernels did not have header page info.
5858 * Sorry but we just use what we find here in user space.
5860 pevent->header_page_ts_size = sizeof(long long);
5861 pevent->header_page_size_size = long_size;
5862 pevent->header_page_data_offset = sizeof(long long) + long_size;
5863 pevent->old_format = 1;
5866 init_input_buf(buf, size);
5868 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5869 &pevent->header_page_ts_size, 1);
5870 parse_header_field("commit", &pevent->header_page_size_offset,
5871 &pevent->header_page_size_size, 1);
5872 parse_header_field("overwrite", &pevent->header_page_overwrite,
5874 parse_header_field("data", &pevent->header_page_data_offset,
5875 &pevent->header_page_data_size, 1);
5880 static int event_matches(struct event_format *event,
5881 int id, const char *sys_name,
5882 const char *event_name)
5884 if (id >= 0 && id != event->id)
5887 if (event_name && (strcmp(event_name, event->name) != 0))
5890 if (sys_name && (strcmp(sys_name, event->system) != 0))
5896 static void free_handler(struct event_handler *handle)
5898 free((void *)handle->sys_name);
5899 free((void *)handle->event_name);
5903 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5905 struct event_handler *handle, **next;
5907 for (next = &pevent->handlers; *next;
5908 next = &(*next)->next) {
5910 if (event_matches(event, handle->id,
5912 handle->event_name))
5919 pr_stat("overriding event (%d) %s:%s with new print handler",
5920 event->id, event->system, event->name);
5922 event->handler = handle->func;
5923 event->context = handle->context;
5925 *next = handle->next;
5926 free_handler(handle);
5932 * __pevent_parse_format - parse the event format
5933 * @buf: the buffer storing the event format string
5934 * @size: the size of @buf
5935 * @sys: the system the event belongs to
5937 * This parses the event format and creates an event structure
5938 * to quickly parse raw data for a given event.
5940 * These files currently come from:
5942 * /sys/kernel/debug/tracing/events/.../.../format
5944 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5945 struct pevent *pevent, const char *buf,
5946 unsigned long size, const char *sys)
5948 struct event_format *event;
5951 init_input_buf(buf, size);
5953 *eventp = event = alloc_event();
5955 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5957 event->name = event_read_name();
5960 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5961 goto event_alloc_failed;
5964 if (strcmp(sys, "ftrace") == 0) {
5965 event->flags |= EVENT_FL_ISFTRACE;
5967 if (strcmp(event->name, "bprint") == 0)
5968 event->flags |= EVENT_FL_ISBPRINT;
5971 event->id = event_read_id();
5972 if (event->id < 0) {
5973 ret = PEVENT_ERRNO__READ_ID_FAILED;
5975 * This isn't an allocation error actually.
5976 * But as the ID is critical, just bail out.
5978 goto event_alloc_failed;
5981 event->system = strdup(sys);
5982 if (!event->system) {
5983 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5984 goto event_alloc_failed;
5987 /* Add pevent to event so that it can be referenced */
5988 event->pevent = pevent;
5990 ret = event_read_format(event);
5992 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5993 goto event_parse_failed;
5997 * If the event has an override, don't print warnings if the event
5998 * print format fails to parse.
6000 if (pevent && find_event_handle(pevent, event))
6003 ret = event_read_print(event);
6007 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
6008 goto event_parse_failed;
6011 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6012 struct format_field *field;
6013 struct print_arg *arg, **list;
6015 /* old ftrace had no args */
6016 list = &event->print_fmt.args;
6017 for (field = event->format.fields; field; field = field->next) {
6020 event->flags |= EVENT_FL_FAILED;
6021 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6023 arg->type = PRINT_FIELD;
6024 arg->field.name = strdup(field->name);
6025 if (!arg->field.name) {
6026 event->flags |= EVENT_FL_FAILED;
6028 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6030 arg->field.field = field;
6040 event->flags |= EVENT_FL_FAILED;
6044 free(event->system);
6051 static enum pevent_errno
6052 __pevent_parse_event(struct pevent *pevent,
6053 struct event_format **eventp,
6054 const char *buf, unsigned long size,
6057 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
6058 struct event_format *event = *eventp;
6063 if (pevent && add_event(pevent, event)) {
6064 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6065 goto event_add_failed;
6068 #define PRINT_ARGS 0
6069 if (PRINT_ARGS && event->print_fmt.args)
6070 print_args(event->print_fmt.args);
6075 pevent_free_format(event);
6080 * pevent_parse_format - parse the event format
6081 * @pevent: the handle to the pevent
6082 * @eventp: returned format
6083 * @buf: the buffer storing the event format string
6084 * @size: the size of @buf
6085 * @sys: the system the event belongs to
6087 * This parses the event format and creates an event structure
6088 * to quickly parse raw data for a given event.
6090 * These files currently come from:
6092 * /sys/kernel/debug/tracing/events/.../.../format
6094 enum pevent_errno pevent_parse_format(struct pevent *pevent,
6095 struct event_format **eventp,
6097 unsigned long size, const char *sys)
6099 return __pevent_parse_event(pevent, eventp, buf, size, sys);
6103 * pevent_parse_event - parse the event format
6104 * @pevent: the handle to the pevent
6105 * @buf: the buffer storing the event format string
6106 * @size: the size of @buf
6107 * @sys: the system the event belongs to
6109 * This parses the event format and creates an event structure
6110 * to quickly parse raw data for a given event.
6112 * These files currently come from:
6114 * /sys/kernel/debug/tracing/events/.../.../format
6116 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
6117 unsigned long size, const char *sys)
6119 struct event_format *event = NULL;
6120 return __pevent_parse_event(pevent, &event, buf, size, sys);
6124 #define _PE(code, str) str
6125 static const char * const pevent_error_str[] = {
6130 int pevent_strerror(struct pevent *pevent __maybe_unused,
6131 enum pevent_errno errnum, char *buf, size_t buflen)
6137 str_error_r(errnum, buf, buflen);
6141 if (errnum <= __PEVENT_ERRNO__START ||
6142 errnum >= __PEVENT_ERRNO__END)
6145 idx = errnum - __PEVENT_ERRNO__START - 1;
6146 msg = pevent_error_str[idx];
6147 snprintf(buf, buflen, "%s", msg);
6152 int get_field_val(struct trace_seq *s, struct format_field *field,
6153 const char *name, struct pevent_record *record,
6154 unsigned long long *val, int err)
6158 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6162 if (pevent_read_number_field(field, record->data, val)) {
6164 trace_seq_printf(s, " %s=INVALID", name);
6172 * pevent_get_field_raw - return the raw pointer into the data field
6173 * @s: The seq to print to on error
6174 * @event: the event that the field is for
6175 * @name: The name of the field
6176 * @record: The record with the field name.
6177 * @len: place to store the field length.
6178 * @err: print default error if failed.
6180 * Returns a pointer into record->data of the field and places
6181 * the length of the field in @len.
6183 * On failure, it returns NULL.
6185 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6186 const char *name, struct pevent_record *record,
6189 struct format_field *field;
6190 void *data = record->data;
6197 field = pevent_find_field(event, name);
6201 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6205 /* Allow @len to be NULL */
6209 offset = field->offset;
6210 if (field->flags & FIELD_IS_DYNAMIC) {
6211 offset = pevent_read_number(event->pevent,
6212 data + offset, field->size);
6213 *len = offset >> 16;
6218 return data + offset;
6222 * pevent_get_field_val - find a field and return its value
6223 * @s: The seq to print to on error
6224 * @event: the event that the field is for
6225 * @name: The name of the field
6226 * @record: The record with the field name.
6227 * @val: place to store the value of the field.
6228 * @err: print default error if failed.
6230 * Returns 0 on success -1 on field not found.
6232 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6233 const char *name, struct pevent_record *record,
6234 unsigned long long *val, int err)
6236 struct format_field *field;
6241 field = pevent_find_field(event, name);
6243 return get_field_val(s, field, name, record, val, err);
6247 * pevent_get_common_field_val - find a common field and return its value
6248 * @s: The seq to print to on error
6249 * @event: the event that the field is for
6250 * @name: The name of the field
6251 * @record: The record with the field name.
6252 * @val: place to store the value of the field.
6253 * @err: print default error if failed.
6255 * Returns 0 on success -1 on field not found.
6257 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6258 const char *name, struct pevent_record *record,
6259 unsigned long long *val, int err)
6261 struct format_field *field;
6266 field = pevent_find_common_field(event, name);
6268 return get_field_val(s, field, name, record, val, err);
6272 * pevent_get_any_field_val - find a any field and return its value
6273 * @s: The seq to print to on error
6274 * @event: the event that the field is for
6275 * @name: The name of the field
6276 * @record: The record with the field name.
6277 * @val: place to store the value of the field.
6278 * @err: print default error if failed.
6280 * Returns 0 on success -1 on field not found.
6282 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6283 const char *name, struct pevent_record *record,
6284 unsigned long long *val, int err)
6286 struct format_field *field;
6291 field = pevent_find_any_field(event, name);
6293 return get_field_val(s, field, name, record, val, err);
6297 * pevent_print_num_field - print a field and a format
6298 * @s: The seq to print to
6299 * @fmt: The printf format to print the field with.
6300 * @event: the event that the field is for
6301 * @name: The name of the field
6302 * @record: The record with the field name.
6303 * @err: print default error if failed.
6305 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6307 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6308 struct event_format *event, const char *name,
6309 struct pevent_record *record, int err)
6311 struct format_field *field = pevent_find_field(event, name);
6312 unsigned long long val;
6317 if (pevent_read_number_field(field, record->data, &val))
6320 return trace_seq_printf(s, fmt, val);
6324 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6329 * pevent_print_func_field - print a field and a format for function pointers
6330 * @s: The seq to print to
6331 * @fmt: The printf format to print the field with.
6332 * @event: the event that the field is for
6333 * @name: The name of the field
6334 * @record: The record with the field name.
6335 * @err: print default error if failed.
6337 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6339 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6340 struct event_format *event, const char *name,
6341 struct pevent_record *record, int err)
6343 struct format_field *field = pevent_find_field(event, name);
6344 struct pevent *pevent = event->pevent;
6345 unsigned long long val;
6346 struct func_map *func;
6352 if (pevent_read_number_field(field, record->data, &val))
6355 func = find_func(pevent, val);
6358 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6360 sprintf(tmp, "0x%08llx", val);
6362 return trace_seq_printf(s, fmt, tmp);
6366 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6370 static void free_func_handle(struct pevent_function_handler *func)
6372 struct pevent_func_params *params;
6376 while (func->params) {
6377 params = func->params;
6378 func->params = params->next;
6386 * pevent_register_print_function - register a helper function
6387 * @pevent: the handle to the pevent
6388 * @func: the function to process the helper function
6389 * @ret_type: the return type of the helper function
6390 * @name: the name of the helper function
6391 * @parameters: A list of enum pevent_func_arg_type
6393 * Some events may have helper functions in the print format arguments.
6394 * This allows a plugin to dynamically create a way to process one
6395 * of these functions.
6397 * The @parameters is a variable list of pevent_func_arg_type enums that
6398 * must end with PEVENT_FUNC_ARG_VOID.
6400 int pevent_register_print_function(struct pevent *pevent,
6401 pevent_func_handler func,
6402 enum pevent_func_arg_type ret_type,
6405 struct pevent_function_handler *func_handle;
6406 struct pevent_func_params **next_param;
6407 struct pevent_func_params *param;
6408 enum pevent_func_arg_type type;
6412 func_handle = find_func_handler(pevent, name);
6415 * This is most like caused by the users own
6416 * plugins updating the function. This overrides the
6419 pr_stat("override of function helper '%s'", name);
6420 remove_func_handler(pevent, name);
6423 func_handle = calloc(1, sizeof(*func_handle));
6425 do_warning("Failed to allocate function handler");
6426 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6429 func_handle->ret_type = ret_type;
6430 func_handle->name = strdup(name);
6431 func_handle->func = func;
6432 if (!func_handle->name) {
6433 do_warning("Failed to allocate function name");
6435 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6438 next_param = &(func_handle->params);
6441 type = va_arg(ap, enum pevent_func_arg_type);
6442 if (type == PEVENT_FUNC_ARG_VOID)
6445 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6446 do_warning("Invalid argument type %d", type);
6447 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6451 param = malloc(sizeof(*param));
6453 do_warning("Failed to allocate function param");
6454 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6460 *next_param = param;
6461 next_param = &(param->next);
6463 func_handle->nr_args++;
6467 func_handle->next = pevent->func_handlers;
6468 pevent->func_handlers = func_handle;
6473 free_func_handle(func_handle);
6478 * pevent_unregister_print_function - unregister a helper function
6479 * @pevent: the handle to the pevent
6480 * @func: the function to process the helper function
6481 * @name: the name of the helper function
6483 * This function removes existing print handler for function @name.
6485 * Returns 0 if the handler was removed successully, -1 otherwise.
6487 int pevent_unregister_print_function(struct pevent *pevent,
6488 pevent_func_handler func, char *name)
6490 struct pevent_function_handler *func_handle;
6492 func_handle = find_func_handler(pevent, name);
6493 if (func_handle && func_handle->func == func) {
6494 remove_func_handler(pevent, name);
6500 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6501 const char *sys_name,
6502 const char *event_name)
6504 struct event_format *event;
6508 event = pevent_find_event(pevent, id);
6511 if (event_name && (strcmp(event_name, event->name) != 0))
6513 if (sys_name && (strcmp(sys_name, event->system) != 0))
6516 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6524 * pevent_register_event_handler - register a way to parse an event
6525 * @pevent: the handle to the pevent
6526 * @id: the id of the event to register
6527 * @sys_name: the system name the event belongs to
6528 * @event_name: the name of the event
6529 * @func: the function to call to parse the event information
6530 * @context: the data to be passed to @func
6532 * This function allows a developer to override the parsing of
6533 * a given event. If for some reason the default print format
6534 * is not sufficient, this function will register a function
6535 * for an event to be used to parse the data instead.
6537 * If @id is >= 0, then it is used to find the event.
6538 * else @sys_name and @event_name are used.
6540 int pevent_register_event_handler(struct pevent *pevent, int id,
6541 const char *sys_name, const char *event_name,
6542 pevent_event_handler_func func, void *context)
6544 struct event_format *event;
6545 struct event_handler *handle;
6547 event = pevent_search_event(pevent, id, sys_name, event_name);
6551 pr_stat("overriding event (%d) %s:%s with new print handler",
6552 event->id, event->system, event->name);
6554 event->handler = func;
6555 event->context = context;
6559 /* Save for later use. */
6560 handle = calloc(1, sizeof(*handle));
6562 do_warning("Failed to allocate event handler");
6563 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6568 handle->event_name = strdup(event_name);
6570 handle->sys_name = strdup(sys_name);
6572 if ((event_name && !handle->event_name) ||
6573 (sys_name && !handle->sys_name)) {
6574 do_warning("Failed to allocate event/sys name");
6575 free((void *)handle->event_name);
6576 free((void *)handle->sys_name);
6578 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6581 handle->func = func;
6582 handle->next = pevent->handlers;
6583 pevent->handlers = handle;
6584 handle->context = context;
6589 static int handle_matches(struct event_handler *handler, int id,
6590 const char *sys_name, const char *event_name,
6591 pevent_event_handler_func func, void *context)
6593 if (id >= 0 && id != handler->id)
6596 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6599 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6602 if (func != handler->func || context != handler->context)
6609 * pevent_unregister_event_handler - unregister an existing event handler
6610 * @pevent: the handle to the pevent
6611 * @id: the id of the event to unregister
6612 * @sys_name: the system name the handler belongs to
6613 * @event_name: the name of the event handler
6614 * @func: the function to call to parse the event information
6615 * @context: the data to be passed to @func
6617 * This function removes existing event handler (parser).
6619 * If @id is >= 0, then it is used to find the event.
6620 * else @sys_name and @event_name are used.
6622 * Returns 0 if handler was removed successfully, -1 if event was not found.
6624 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6625 const char *sys_name, const char *event_name,
6626 pevent_event_handler_func func, void *context)
6628 struct event_format *event;
6629 struct event_handler *handle;
6630 struct event_handler **next;
6632 event = pevent_search_event(pevent, id, sys_name, event_name);
6636 if (event->handler == func && event->context == context) {
6637 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6638 event->id, event->system, event->name);
6640 event->handler = NULL;
6641 event->context = NULL;
6646 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6648 if (handle_matches(handle, id, sys_name, event_name,
6656 *next = handle->next;
6657 free_handler(handle);
6663 * pevent_alloc - create a pevent handle
6665 struct pevent *pevent_alloc(void)
6667 struct pevent *pevent = calloc(1, sizeof(*pevent));
6670 pevent->ref_count = 1;
6675 void pevent_ref(struct pevent *pevent)
6677 pevent->ref_count++;
6680 void pevent_free_format_field(struct format_field *field)
6683 if (field->alias != field->name)
6689 static void free_format_fields(struct format_field *field)
6691 struct format_field *next;
6695 pevent_free_format_field(field);
6700 static void free_formats(struct format *format)
6702 free_format_fields(format->common_fields);
6703 free_format_fields(format->fields);
6706 void pevent_free_format(struct event_format *event)
6709 free(event->system);
6711 free_formats(&event->format);
6713 free(event->print_fmt.format);
6714 free_args(event->print_fmt.args);
6720 * pevent_free - free a pevent handle
6721 * @pevent: the pevent handle to free
6723 void pevent_free(struct pevent *pevent)
6725 struct cmdline_list *cmdlist, *cmdnext;
6726 struct func_list *funclist, *funcnext;
6727 struct printk_list *printklist, *printknext;
6728 struct pevent_function_handler *func_handler;
6729 struct event_handler *handle;
6735 cmdlist = pevent->cmdlist;
6736 funclist = pevent->funclist;
6737 printklist = pevent->printklist;
6739 pevent->ref_count--;
6740 if (pevent->ref_count)
6743 if (pevent->cmdlines) {
6744 for (i = 0; i < pevent->cmdline_count; i++)
6745 free(pevent->cmdlines[i].comm);
6746 free(pevent->cmdlines);
6750 cmdnext = cmdlist->next;
6751 free(cmdlist->comm);
6756 if (pevent->func_map) {
6757 for (i = 0; i < (int)pevent->func_count; i++) {
6758 free(pevent->func_map[i].func);
6759 free(pevent->func_map[i].mod);
6761 free(pevent->func_map);
6765 funcnext = funclist->next;
6766 free(funclist->func);
6767 free(funclist->mod);
6769 funclist = funcnext;
6772 while (pevent->func_handlers) {
6773 func_handler = pevent->func_handlers;
6774 pevent->func_handlers = func_handler->next;
6775 free_func_handle(func_handler);
6778 if (pevent->printk_map) {
6779 for (i = 0; i < (int)pevent->printk_count; i++)
6780 free(pevent->printk_map[i].printk);
6781 free(pevent->printk_map);
6784 while (printklist) {
6785 printknext = printklist->next;
6786 free(printklist->printk);
6788 printklist = printknext;
6791 for (i = 0; i < pevent->nr_events; i++)
6792 pevent_free_format(pevent->events[i]);
6794 while (pevent->handlers) {
6795 handle = pevent->handlers;
6796 pevent->handlers = handle->next;
6797 free_handler(handle);
6800 free(pevent->trace_clock);
6801 free(pevent->events);
6802 free(pevent->sort_events);
6803 free(pevent->func_resolver);
6808 void pevent_unref(struct pevent *pevent)
6810 pevent_free(pevent);