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 <netinet/ip6.h>
36 #include "event-parse.h"
37 #include "event-utils.h"
39 static const char *input_buf;
40 static unsigned long long input_buf_ptr;
41 static unsigned long long input_buf_siz;
43 static int is_flag_field;
44 static int is_symbolic_field;
46 static int show_warning = 1;
48 #define do_warning(fmt, ...) \
51 warning(fmt, ##__VA_ARGS__); \
54 #define do_warning_event(event, fmt, ...) \
60 warning("[%s:%s] " fmt, event->system, \
61 event->name, ##__VA_ARGS__); \
63 warning(fmt, ##__VA_ARGS__); \
66 static void init_input_buf(const char *buf, unsigned long long size)
73 const char *pevent_get_input_buf(void)
78 unsigned long long pevent_get_input_buf_ptr(void)
83 struct event_handler {
84 struct event_handler *next;
87 const char *event_name;
88 pevent_event_handler_func func;
92 struct pevent_func_params {
93 struct pevent_func_params *next;
94 enum pevent_func_arg_type type;
97 struct pevent_function_handler {
98 struct pevent_function_handler *next;
99 enum pevent_func_arg_type ret_type;
101 pevent_func_handler func;
102 struct pevent_func_params *params;
106 static unsigned long long
107 process_defined_func(struct trace_seq *s, void *data, int size,
108 struct event_format *event, struct print_arg *arg);
110 static void free_func_handle(struct pevent_function_handler *func);
113 * pevent_buffer_init - init buffer for parsing
114 * @buf: buffer to parse
115 * @size: the size of the buffer
117 * For use with pevent_read_token(), this initializes the internal
118 * buffer that pevent_read_token() will parse.
120 void pevent_buffer_init(const char *buf, unsigned long long size)
122 init_input_buf(buf, size);
125 void breakpoint(void)
131 struct print_arg *alloc_arg(void)
133 return calloc(1, sizeof(struct print_arg));
141 static int cmdline_cmp(const void *a, const void *b)
143 const struct cmdline *ca = a;
144 const struct cmdline *cb = b;
146 if (ca->pid < cb->pid)
148 if (ca->pid > cb->pid)
154 struct cmdline_list {
155 struct cmdline_list *next;
160 static int cmdline_init(struct pevent *pevent)
162 struct cmdline_list *cmdlist = pevent->cmdlist;
163 struct cmdline_list *item;
164 struct cmdline *cmdlines;
167 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
173 cmdlines[i].pid = cmdlist->pid;
174 cmdlines[i].comm = cmdlist->comm;
177 cmdlist = cmdlist->next;
181 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
183 pevent->cmdlines = cmdlines;
184 pevent->cmdlist = NULL;
189 static const char *find_cmdline(struct pevent *pevent, int pid)
191 const struct cmdline *comm;
197 if (!pevent->cmdlines && cmdline_init(pevent))
198 return "<not enough memory for cmdlines!>";
202 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
203 sizeof(*pevent->cmdlines), cmdline_cmp);
211 * pevent_pid_is_registered - return if a pid has a cmdline registered
212 * @pevent: handle for the pevent
213 * @pid: The pid to check if it has a cmdline registered with.
215 * Returns 1 if the pid has a cmdline mapped to it
218 int pevent_pid_is_registered(struct pevent *pevent, int pid)
220 const struct cmdline *comm;
226 if (!pevent->cmdlines && cmdline_init(pevent))
231 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
232 sizeof(*pevent->cmdlines), cmdline_cmp);
240 * If the command lines have been converted to an array, then
241 * we must add this pid. This is much slower than when cmdlines
242 * are added before the array is initialized.
244 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
246 struct cmdline *cmdlines = pevent->cmdlines;
247 const struct cmdline *cmdline;
253 /* avoid duplicates */
256 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
257 sizeof(*pevent->cmdlines), cmdline_cmp);
263 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
269 cmdlines[pevent->cmdline_count].comm = strdup(comm);
270 if (!cmdlines[pevent->cmdline_count].comm) {
276 cmdlines[pevent->cmdline_count].pid = pid;
278 if (cmdlines[pevent->cmdline_count].comm)
279 pevent->cmdline_count++;
281 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
282 pevent->cmdlines = cmdlines;
288 * pevent_register_comm - register a pid / comm mapping
289 * @pevent: handle for the pevent
290 * @comm: the command line to register
291 * @pid: the pid to map the command line to
293 * This adds a mapping to search for command line names with
294 * a given pid. The comm is duplicated.
296 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
298 struct cmdline_list *item;
300 if (pevent->cmdlines)
301 return add_new_comm(pevent, comm, pid);
303 item = malloc(sizeof(*item));
308 item->comm = strdup(comm);
310 item->comm = strdup("<...>");
316 item->next = pevent->cmdlist;
318 pevent->cmdlist = item;
319 pevent->cmdline_count++;
324 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
326 pevent->trace_clock = strdup(trace_clock);
327 if (!pevent->trace_clock) {
335 unsigned long long addr;
341 struct func_list *next;
342 unsigned long long addr;
347 static int func_cmp(const void *a, const void *b)
349 const struct func_map *fa = a;
350 const struct func_map *fb = b;
352 if (fa->addr < fb->addr)
354 if (fa->addr > fb->addr)
361 * We are searching for a record in between, not an exact
364 static int func_bcmp(const void *a, const void *b)
366 const struct func_map *fa = a;
367 const struct func_map *fb = b;
369 if ((fa->addr == fb->addr) ||
371 (fa->addr > fb->addr &&
372 fa->addr < (fb+1)->addr))
375 if (fa->addr < fb->addr)
381 static int func_map_init(struct pevent *pevent)
383 struct func_list *funclist;
384 struct func_list *item;
385 struct func_map *func_map;
388 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
392 funclist = pevent->funclist;
396 func_map[i].func = funclist->func;
397 func_map[i].addr = funclist->addr;
398 func_map[i].mod = funclist->mod;
401 funclist = funclist->next;
405 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
408 * Add a special record at the end.
410 func_map[pevent->func_count].func = NULL;
411 func_map[pevent->func_count].addr = 0;
412 func_map[pevent->func_count].mod = NULL;
414 pevent->func_map = func_map;
415 pevent->funclist = NULL;
420 static struct func_map *
421 __find_func(struct pevent *pevent, unsigned long long addr)
423 struct func_map *func;
426 if (!pevent->func_map)
427 func_map_init(pevent);
431 func = bsearch(&key, pevent->func_map, pevent->func_count,
432 sizeof(*pevent->func_map), func_bcmp);
437 struct func_resolver {
438 pevent_func_resolver_t *func;
444 * pevent_set_function_resolver - set an alternative function resolver
445 * @pevent: handle for the pevent
446 * @resolver: function to be used
447 * @priv: resolver function private state.
449 * Some tools may have already a way to resolve kernel functions, allow them to
450 * keep using it instead of duplicating all the entries inside
453 int pevent_set_function_resolver(struct pevent *pevent,
454 pevent_func_resolver_t *func, void *priv)
456 struct func_resolver *resolver = malloc(sizeof(*resolver));
458 if (resolver == NULL)
461 resolver->func = func;
462 resolver->priv = priv;
464 free(pevent->func_resolver);
465 pevent->func_resolver = resolver;
471 * pevent_reset_function_resolver - reset alternative function resolver
472 * @pevent: handle for the pevent
474 * Stop using whatever alternative resolver was set, use the default
477 void pevent_reset_function_resolver(struct pevent *pevent)
479 free(pevent->func_resolver);
480 pevent->func_resolver = NULL;
483 static struct func_map *
484 find_func(struct pevent *pevent, unsigned long long addr)
486 struct func_map *map;
488 if (!pevent->func_resolver)
489 return __find_func(pevent, addr);
491 map = &pevent->func_resolver->map;
494 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
495 &map->addr, &map->mod);
496 if (map->func == NULL)
503 * pevent_find_function - find a function by a given address
504 * @pevent: handle for the pevent
505 * @addr: the address to find the function with
507 * Returns a pointer to the function stored that has the given
508 * address. Note, the address does not have to be exact, it
509 * will select the function that would contain the address.
511 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
513 struct func_map *map;
515 map = find_func(pevent, addr);
523 * pevent_find_function_address - find a function address by a given address
524 * @pevent: handle for the pevent
525 * @addr: the address to find the function with
527 * Returns the address the function starts at. This can be used in
528 * conjunction with pevent_find_function to print both the function
529 * name and the function offset.
532 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
534 struct func_map *map;
536 map = find_func(pevent, addr);
544 * pevent_register_function - register a function with a given address
545 * @pevent: handle for the pevent
546 * @function: the function name to register
547 * @addr: the address the function starts at
548 * @mod: the kernel module the function may be in (NULL for none)
550 * This registers a function name with an address and module.
551 * The @func passed in is duplicated.
553 int pevent_register_function(struct pevent *pevent, char *func,
554 unsigned long long addr, char *mod)
556 struct func_list *item = malloc(sizeof(*item));
561 item->next = pevent->funclist;
562 item->func = strdup(func);
567 item->mod = strdup(mod);
574 pevent->funclist = item;
575 pevent->func_count++;
589 * pevent_print_funcs - print out the stored functions
590 * @pevent: handle for the pevent
592 * This prints out the stored functions.
594 void pevent_print_funcs(struct pevent *pevent)
598 if (!pevent->func_map)
599 func_map_init(pevent);
601 for (i = 0; i < (int)pevent->func_count; i++) {
603 pevent->func_map[i].addr,
604 pevent->func_map[i].func);
605 if (pevent->func_map[i].mod)
606 printf(" [%s]\n", pevent->func_map[i].mod);
613 unsigned long long addr;
618 struct printk_list *next;
619 unsigned long long addr;
623 static int printk_cmp(const void *a, const void *b)
625 const struct printk_map *pa = a;
626 const struct printk_map *pb = b;
628 if (pa->addr < pb->addr)
630 if (pa->addr > pb->addr)
636 static int printk_map_init(struct pevent *pevent)
638 struct printk_list *printklist;
639 struct printk_list *item;
640 struct printk_map *printk_map;
643 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
647 printklist = pevent->printklist;
651 printk_map[i].printk = printklist->printk;
652 printk_map[i].addr = printklist->addr;
655 printklist = printklist->next;
659 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
661 pevent->printk_map = printk_map;
662 pevent->printklist = NULL;
667 static struct printk_map *
668 find_printk(struct pevent *pevent, unsigned long long addr)
670 struct printk_map *printk;
671 struct printk_map key;
673 if (!pevent->printk_map && printk_map_init(pevent))
678 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
679 sizeof(*pevent->printk_map), printk_cmp);
685 * pevent_register_print_string - register a string by its address
686 * @pevent: handle for the pevent
687 * @fmt: the string format to register
688 * @addr: the address the string was located at
690 * This registers a string by the address it was stored in the kernel.
691 * The @fmt passed in is duplicated.
693 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
694 unsigned long long addr)
696 struct printk_list *item = malloc(sizeof(*item));
702 item->next = pevent->printklist;
705 /* Strip off quotes and '\n' from the end */
708 item->printk = strdup(fmt);
712 p = item->printk + strlen(item->printk) - 1;
717 if (strcmp(p, "\\n") == 0)
720 pevent->printklist = item;
721 pevent->printk_count++;
732 * pevent_print_printk - print out the stored strings
733 * @pevent: handle for the pevent
735 * This prints the string formats that were stored.
737 void pevent_print_printk(struct pevent *pevent)
741 if (!pevent->printk_map)
742 printk_map_init(pevent);
744 for (i = 0; i < (int)pevent->printk_count; i++) {
745 printf("%016llx %s\n",
746 pevent->printk_map[i].addr,
747 pevent->printk_map[i].printk);
751 static struct event_format *alloc_event(void)
753 return calloc(1, sizeof(struct event_format));
756 static int add_event(struct pevent *pevent, struct event_format *event)
759 struct event_format **events = realloc(pevent->events, sizeof(event) *
760 (pevent->nr_events + 1));
764 pevent->events = events;
766 for (i = 0; i < pevent->nr_events; i++) {
767 if (pevent->events[i]->id > event->id)
770 if (i < pevent->nr_events)
771 memmove(&pevent->events[i + 1],
773 sizeof(event) * (pevent->nr_events - i));
775 pevent->events[i] = event;
778 event->pevent = pevent;
783 static int event_item_type(enum event_type type)
786 case EVENT_ITEM ... EVENT_SQUOTE:
788 case EVENT_ERROR ... EVENT_DELIM:
794 static void free_flag_sym(struct print_flag_sym *fsym)
796 struct print_flag_sym *next;
807 static void free_arg(struct print_arg *arg)
809 struct print_arg *farg;
816 free(arg->atom.atom);
819 free(arg->field.name);
822 free_arg(arg->flags.field);
823 free(arg->flags.delim);
824 free_flag_sym(arg->flags.flags);
827 free_arg(arg->symbol.field);
828 free_flag_sym(arg->symbol.symbols);
831 free_arg(arg->hex.field);
832 free_arg(arg->hex.size);
834 case PRINT_INT_ARRAY:
835 free_arg(arg->int_array.field);
836 free_arg(arg->int_array.count);
837 free_arg(arg->int_array.el_size);
840 free(arg->typecast.type);
841 free_arg(arg->typecast.item);
845 free(arg->string.string);
848 free(arg->bitmask.bitmask);
850 case PRINT_DYNAMIC_ARRAY:
851 free(arg->dynarray.index);
855 free_arg(arg->op.left);
856 free_arg(arg->op.right);
859 while (arg->func.args) {
860 farg = arg->func.args;
861 arg->func.args = farg->next;
874 static enum event_type get_type(int ch)
877 return EVENT_NEWLINE;
880 if (isalnum(ch) || ch == '_')
888 if (ch == '(' || ch == ')' || ch == ',')
894 static int __read_char(void)
896 if (input_buf_ptr >= input_buf_siz)
899 return input_buf[input_buf_ptr++];
902 static int __peek_char(void)
904 if (input_buf_ptr >= input_buf_siz)
907 return input_buf[input_buf_ptr];
911 * pevent_peek_char - peek at the next character that will be read
913 * Returns the next character read, or -1 if end of buffer.
915 int pevent_peek_char(void)
917 return __peek_char();
920 static int extend_token(char **tok, char *buf, int size)
922 char *newtok = realloc(*tok, size);
939 static enum event_type force_token(const char *str, char **tok);
941 static enum event_type __read_token(char **tok)
944 int ch, last_ch, quote_ch, next_ch;
947 enum event_type type;
957 if (type == EVENT_NONE)
965 if (asprintf(tok, "%c", ch) < 0)
973 next_ch = __peek_char();
974 if (next_ch == '>') {
975 buf[i++] = __read_char();
988 buf[i++] = __read_char();
1000 default: /* what should we do instead? */
1010 buf[i++] = __read_char();
1015 /* don't keep quotes */
1021 if (i == (BUFSIZ - 1)) {
1025 if (extend_token(tok, buf, tok_size) < 0)
1032 /* the '\' '\' will cancel itself */
1033 if (ch == '\\' && last_ch == '\\')
1035 } while (ch != quote_ch || last_ch == '\\');
1036 /* remove the last quote */
1040 * For strings (double quotes) check the next token.
1041 * If it is another string, concatinate the two.
1043 if (type == EVENT_DQUOTE) {
1044 unsigned long long save_input_buf_ptr = input_buf_ptr;
1048 } while (isspace(ch));
1051 input_buf_ptr = save_input_buf_ptr;
1056 case EVENT_ERROR ... EVENT_SPACE:
1062 while (get_type(__peek_char()) == type) {
1063 if (i == (BUFSIZ - 1)) {
1067 if (extend_token(tok, buf, tok_size) < 0)
1077 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1080 if (type == EVENT_ITEM) {
1082 * Older versions of the kernel has a bug that
1083 * creates invalid symbols and will break the mac80211
1084 * parsing. This is a work around to that bug.
1086 * See Linux kernel commit:
1087 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1089 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1092 return force_token("\"\%s\" ", tok);
1093 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1096 return force_token("\" sta:%pM\" ", tok);
1097 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1100 return force_token("\" vif:%p(%d)\" ", tok);
1107 static enum event_type force_token(const char *str, char **tok)
1109 const char *save_input_buf;
1110 unsigned long long save_input_buf_ptr;
1111 unsigned long long save_input_buf_siz;
1112 enum event_type type;
1114 /* save off the current input pointers */
1115 save_input_buf = input_buf;
1116 save_input_buf_ptr = input_buf_ptr;
1117 save_input_buf_siz = input_buf_siz;
1119 init_input_buf(str, strlen(str));
1121 type = __read_token(tok);
1123 /* reset back to original token */
1124 input_buf = save_input_buf;
1125 input_buf_ptr = save_input_buf_ptr;
1126 input_buf_siz = save_input_buf_siz;
1131 static void free_token(char *tok)
1137 static enum event_type read_token(char **tok)
1139 enum event_type type;
1142 type = __read_token(tok);
1143 if (type != EVENT_SPACE)
1155 * pevent_read_token - access to utilites to use the pevent parser
1156 * @tok: The token to return
1158 * This will parse tokens from the string given by
1159 * pevent_init_data().
1161 * Returns the token type.
1163 enum event_type pevent_read_token(char **tok)
1165 return read_token(tok);
1169 * pevent_free_token - free a token returned by pevent_read_token
1170 * @token: the token to free
1172 void pevent_free_token(char *token)
1178 static enum event_type read_token_item(char **tok)
1180 enum event_type type;
1183 type = __read_token(tok);
1184 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1195 static int test_type(enum event_type type, enum event_type expect)
1197 if (type != expect) {
1198 do_warning("Error: expected type %d but read %d",
1205 static int test_type_token(enum event_type type, const char *token,
1206 enum event_type expect, const char *expect_tok)
1208 if (type != expect) {
1209 do_warning("Error: expected type %d but read %d",
1214 if (strcmp(token, expect_tok) != 0) {
1215 do_warning("Error: expected '%s' but read '%s'",
1222 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1224 enum event_type type;
1227 type = read_token(tok);
1229 type = read_token_item(tok);
1230 return test_type(type, expect);
1233 static int read_expect_type(enum event_type expect, char **tok)
1235 return __read_expect_type(expect, tok, 1);
1238 static int __read_expected(enum event_type expect, const char *str,
1241 enum event_type type;
1246 type = read_token(&token);
1248 type = read_token_item(&token);
1250 ret = test_type_token(type, token, expect, str);
1257 static int read_expected(enum event_type expect, const char *str)
1259 return __read_expected(expect, str, 1);
1262 static int read_expected_item(enum event_type expect, const char *str)
1264 return __read_expected(expect, str, 0);
1267 static char *event_read_name(void)
1271 if (read_expected(EVENT_ITEM, "name") < 0)
1274 if (read_expected(EVENT_OP, ":") < 0)
1277 if (read_expect_type(EVENT_ITEM, &token) < 0)
1287 static int event_read_id(void)
1292 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1295 if (read_expected(EVENT_OP, ":") < 0)
1298 if (read_expect_type(EVENT_ITEM, &token) < 0)
1301 id = strtoul(token, NULL, 0);
1310 static int field_is_string(struct format_field *field)
1312 if ((field->flags & FIELD_IS_ARRAY) &&
1313 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1314 strstr(field->type, "s8")))
1320 static int field_is_dynamic(struct format_field *field)
1322 if (strncmp(field->type, "__data_loc", 10) == 0)
1328 static int field_is_long(struct format_field *field)
1330 /* includes long long */
1331 if (strstr(field->type, "long"))
1337 static unsigned int type_size(const char *name)
1339 /* This covers all FIELD_IS_STRING types. */
1357 for (i = 0; table[i].type; i++) {
1358 if (!strcmp(table[i].type, name))
1359 return table[i].size;
1365 static int event_read_fields(struct event_format *event, struct format_field **fields)
1367 struct format_field *field = NULL;
1368 enum event_type type;
1374 unsigned int size_dynamic = 0;
1376 type = read_token(&token);
1377 if (type == EVENT_NEWLINE) {
1384 if (test_type_token(type, token, EVENT_ITEM, "field"))
1388 type = read_token(&token);
1390 * The ftrace fields may still use the "special" name.
1393 if (event->flags & EVENT_FL_ISFTRACE &&
1394 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1396 type = read_token(&token);
1399 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1403 if (read_expect_type(EVENT_ITEM, &token) < 0)
1408 field = calloc(1, sizeof(*field));
1412 field->event = event;
1414 /* read the rest of the type */
1416 type = read_token(&token);
1417 if (type == EVENT_ITEM ||
1418 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1420 * Some of the ftrace fields are broken and have
1421 * an illegal "." in them.
1423 (event->flags & EVENT_FL_ISFTRACE &&
1424 type == EVENT_OP && strcmp(token, ".") == 0)) {
1426 if (strcmp(token, "*") == 0)
1427 field->flags |= FIELD_IS_POINTER;
1431 new_type = realloc(field->type,
1432 strlen(field->type) +
1433 strlen(last_token) + 2);
1438 field->type = new_type;
1439 strcat(field->type, " ");
1440 strcat(field->type, last_token);
1443 field->type = last_token;
1452 do_warning_event(event, "%s: no type found", __func__);
1455 field->name = field->alias = last_token;
1457 if (test_type(type, EVENT_OP))
1460 if (strcmp(token, "[") == 0) {
1461 enum event_type last_type = type;
1462 char *brackets = token;
1466 field->flags |= FIELD_IS_ARRAY;
1468 type = read_token(&token);
1470 if (type == EVENT_ITEM)
1471 field->arraylen = strtoul(token, NULL, 0);
1473 field->arraylen = 0;
1475 while (strcmp(token, "]") != 0) {
1476 if (last_type == EVENT_ITEM &&
1483 new_brackets = realloc(brackets,
1485 strlen(token) + len);
1486 if (!new_brackets) {
1490 brackets = new_brackets;
1492 strcat(brackets, " ");
1493 strcat(brackets, token);
1494 /* We only care about the last token */
1495 field->arraylen = strtoul(token, NULL, 0);
1497 type = read_token(&token);
1498 if (type == EVENT_NONE) {
1499 do_warning_event(event, "failed to find token");
1506 new_brackets = realloc(brackets, strlen(brackets) + 2);
1507 if (!new_brackets) {
1511 brackets = new_brackets;
1512 strcat(brackets, "]");
1514 /* add brackets to type */
1516 type = read_token(&token);
1518 * If the next token is not an OP, then it is of
1519 * the format: type [] item;
1521 if (type == EVENT_ITEM) {
1523 new_type = realloc(field->type,
1524 strlen(field->type) +
1525 strlen(field->name) +
1526 strlen(brackets) + 2);
1531 field->type = new_type;
1532 strcat(field->type, " ");
1533 strcat(field->type, field->name);
1534 size_dynamic = type_size(field->name);
1535 free_token(field->name);
1536 strcat(field->type, brackets);
1537 field->name = field->alias = token;
1538 type = read_token(&token);
1541 new_type = realloc(field->type,
1542 strlen(field->type) +
1543 strlen(brackets) + 1);
1548 field->type = new_type;
1549 strcat(field->type, brackets);
1554 if (field_is_string(field))
1555 field->flags |= FIELD_IS_STRING;
1556 if (field_is_dynamic(field))
1557 field->flags |= FIELD_IS_DYNAMIC;
1558 if (field_is_long(field))
1559 field->flags |= FIELD_IS_LONG;
1561 if (test_type_token(type, token, EVENT_OP, ";"))
1565 if (read_expected(EVENT_ITEM, "offset") < 0)
1568 if (read_expected(EVENT_OP, ":") < 0)
1571 if (read_expect_type(EVENT_ITEM, &token))
1573 field->offset = strtoul(token, NULL, 0);
1576 if (read_expected(EVENT_OP, ";") < 0)
1579 if (read_expected(EVENT_ITEM, "size") < 0)
1582 if (read_expected(EVENT_OP, ":") < 0)
1585 if (read_expect_type(EVENT_ITEM, &token))
1587 field->size = strtoul(token, NULL, 0);
1590 if (read_expected(EVENT_OP, ";") < 0)
1593 type = read_token(&token);
1594 if (type != EVENT_NEWLINE) {
1595 /* newer versions of the kernel have a "signed" type */
1596 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1601 if (read_expected(EVENT_OP, ":") < 0)
1604 if (read_expect_type(EVENT_ITEM, &token))
1607 if (strtoul(token, NULL, 0))
1608 field->flags |= FIELD_IS_SIGNED;
1611 if (read_expected(EVENT_OP, ";") < 0)
1614 if (read_expect_type(EVENT_NEWLINE, &token))
1620 if (field->flags & FIELD_IS_ARRAY) {
1621 if (field->arraylen)
1622 field->elementsize = field->size / field->arraylen;
1623 else if (field->flags & FIELD_IS_DYNAMIC)
1624 field->elementsize = size_dynamic;
1625 else if (field->flags & FIELD_IS_STRING)
1626 field->elementsize = 1;
1627 else if (field->flags & FIELD_IS_LONG)
1628 field->elementsize = event->pevent ?
1629 event->pevent->long_size :
1632 field->elementsize = field->size;
1635 fields = &field->next;
1652 static int event_read_format(struct event_format *event)
1657 if (read_expected_item(EVENT_ITEM, "format") < 0)
1660 if (read_expected(EVENT_OP, ":") < 0)
1663 if (read_expect_type(EVENT_NEWLINE, &token))
1667 ret = event_read_fields(event, &event->format.common_fields);
1670 event->format.nr_common = ret;
1672 ret = event_read_fields(event, &event->format.fields);
1675 event->format.nr_fields = ret;
1684 static enum event_type
1685 process_arg_token(struct event_format *event, struct print_arg *arg,
1686 char **tok, enum event_type type);
1688 static enum event_type
1689 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1691 enum event_type type;
1694 type = read_token(&token);
1697 return process_arg_token(event, arg, tok, type);
1700 static enum event_type
1701 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1704 * For __print_symbolic() and __print_flags, we need to completely
1705 * evaluate the first argument, which defines what to print next.
1707 static enum event_type
1708 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1710 enum event_type type;
1712 type = process_arg(event, arg, tok);
1714 while (type == EVENT_OP) {
1715 type = process_op(event, arg, tok);
1721 static enum event_type
1722 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1724 struct print_arg *arg, *left, *right;
1725 enum event_type type;
1730 right = alloc_arg();
1732 if (!arg || !left || !right) {
1733 do_warning_event(event, "%s: not enough memory!", __func__);
1734 /* arg will be freed at out_free */
1740 arg->type = PRINT_OP;
1741 arg->op.left = left;
1742 arg->op.right = right;
1745 type = process_arg(event, left, &token);
1748 if (type == EVENT_ERROR)
1751 /* Handle other operations in the arguments */
1752 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1753 type = process_op(event, left, &token);
1757 if (test_type_token(type, token, EVENT_OP, ":"))
1762 type = process_arg(event, right, &token);
1764 top->op.right = arg;
1770 /* Top may point to itself */
1771 top->op.right = NULL;
1777 static enum event_type
1778 process_array(struct event_format *event, struct print_arg *top, char **tok)
1780 struct print_arg *arg;
1781 enum event_type type;
1786 do_warning_event(event, "%s: not enough memory!", __func__);
1787 /* '*tok' is set to top->op.op. No need to free. */
1793 type = process_arg(event, arg, &token);
1794 if (test_type_token(type, token, EVENT_OP, "]"))
1797 top->op.right = arg;
1800 type = read_token_item(&token);
1811 static int get_op_prio(char *op)
1825 /* '>>' and '<<' are 8 */
1829 /* '==' and '!=' are 10 */
1839 do_warning("unknown op '%c'", op[0]);
1843 if (strcmp(op, "++") == 0 ||
1844 strcmp(op, "--") == 0) {
1846 } else if (strcmp(op, ">>") == 0 ||
1847 strcmp(op, "<<") == 0) {
1849 } else if (strcmp(op, ">=") == 0 ||
1850 strcmp(op, "<=") == 0) {
1852 } else if (strcmp(op, "==") == 0 ||
1853 strcmp(op, "!=") == 0) {
1855 } else if (strcmp(op, "&&") == 0) {
1857 } else if (strcmp(op, "||") == 0) {
1860 do_warning("unknown op '%s'", op);
1866 static int set_op_prio(struct print_arg *arg)
1869 /* single ops are the greatest */
1870 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1873 arg->op.prio = get_op_prio(arg->op.op);
1875 return arg->op.prio;
1878 /* Note, *tok does not get freed, but will most likely be saved */
1879 static enum event_type
1880 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1882 struct print_arg *left, *right = NULL;
1883 enum event_type type;
1886 /* the op is passed in via tok */
1889 if (arg->type == PRINT_OP && !arg->op.left) {
1890 /* handle single op */
1892 do_warning_event(event, "bad op token %s", token);
1902 do_warning_event(event, "bad op token %s", token);
1907 /* make an empty left */
1912 left->type = PRINT_NULL;
1913 arg->op.left = left;
1915 right = alloc_arg();
1919 arg->op.right = right;
1921 /* do not free the token, it belongs to an op */
1923 type = process_arg(event, right, tok);
1925 } else if (strcmp(token, "?") == 0) {
1931 /* copy the top arg to the left */
1934 arg->type = PRINT_OP;
1936 arg->op.left = left;
1939 /* it will set arg->op.right */
1940 type = process_cond(event, arg, tok);
1942 } else if (strcmp(token, ">>") == 0 ||
1943 strcmp(token, "<<") == 0 ||
1944 strcmp(token, "&") == 0 ||
1945 strcmp(token, "|") == 0 ||
1946 strcmp(token, "&&") == 0 ||
1947 strcmp(token, "||") == 0 ||
1948 strcmp(token, "-") == 0 ||
1949 strcmp(token, "+") == 0 ||
1950 strcmp(token, "*") == 0 ||
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) {
1964 /* copy the top arg to the left */
1967 arg->type = PRINT_OP;
1969 arg->op.left = left;
1970 arg->op.right = NULL;
1972 if (set_op_prio(arg) == -1) {
1973 event->flags |= EVENT_FL_FAILED;
1974 /* arg->op.op (= token) will be freed at out_free */
1979 type = read_token_item(&token);
1982 /* could just be a type pointer */
1983 if ((strcmp(arg->op.op, "*") == 0) &&
1984 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1987 if (left->type != PRINT_ATOM) {
1988 do_warning_event(event, "bad pointer type");
1991 new_atom = realloc(left->atom.atom,
1992 strlen(left->atom.atom) + 3);
1996 left->atom.atom = new_atom;
1997 strcat(left->atom.atom, " *");
2005 right = alloc_arg();
2009 type = process_arg_token(event, right, tok, type);
2010 if (type == EVENT_ERROR) {
2012 /* token was freed in process_arg_token() via *tok */
2017 if (right->type == PRINT_OP &&
2018 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2019 struct print_arg tmp;
2021 /* rotate ops according to the priority */
2022 arg->op.right = right->op.left;
2028 arg->op.left = right;
2030 arg->op.right = right;
2033 } else if (strcmp(token, "[") == 0) {
2041 arg->type = PRINT_OP;
2043 arg->op.left = left;
2047 /* it will set arg->op.right */
2048 type = process_array(event, arg, tok);
2051 do_warning_event(event, "unknown op '%s'", token);
2052 event->flags |= EVENT_FL_FAILED;
2053 /* the arg is now the left side */
2057 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2060 /* higher prios need to be closer to the root */
2061 prio = get_op_prio(*tok);
2063 if (prio > arg->op.prio)
2064 return process_op(event, arg, tok);
2066 return process_op(event, right, tok);
2072 do_warning_event(event, "%s: not enough memory!", __func__);
2079 static enum event_type
2080 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2083 enum event_type type;
2087 if (read_expected(EVENT_OP, "->") < 0)
2090 if (read_expect_type(EVENT_ITEM, &token) < 0)
2094 arg->type = PRINT_FIELD;
2095 arg->field.name = field;
2097 if (is_flag_field) {
2098 arg->field.field = pevent_find_any_field(event, arg->field.name);
2099 arg->field.field->flags |= FIELD_IS_FLAG;
2101 } else if (is_symbolic_field) {
2102 arg->field.field = pevent_find_any_field(event, arg->field.name);
2103 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2104 is_symbolic_field = 0;
2107 type = read_token(&token);
2119 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2120 struct print_arg **print_arg)
2122 struct print_arg *field;
2123 enum event_type type;
2127 field = alloc_arg();
2129 do_warning_event(event, "%s: not enough memory!", __func__);
2134 type = process_arg(event, field, &token);
2136 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2140 goto out_free_token;
2151 static char *arg_eval (struct print_arg *arg);
2153 static unsigned long long
2154 eval_type_str(unsigned long long val, const char *type, int pointer)
2164 if (type[len-1] != '*') {
2165 do_warning("pointer expected with non pointer type");
2171 do_warning("%s: not enough memory!", __func__);
2174 memcpy(ref, type, len);
2176 /* chop off the " *" */
2179 val = eval_type_str(val, ref, 0);
2184 /* check if this is a pointer */
2185 if (type[len - 1] == '*')
2188 /* Try to figure out the arg size*/
2189 if (strncmp(type, "struct", 6) == 0)
2193 if (strcmp(type, "u8") == 0)
2196 if (strcmp(type, "u16") == 0)
2197 return val & 0xffff;
2199 if (strcmp(type, "u32") == 0)
2200 return val & 0xffffffff;
2202 if (strcmp(type, "u64") == 0 ||
2203 strcmp(type, "s64"))
2206 if (strcmp(type, "s8") == 0)
2207 return (unsigned long long)(char)val & 0xff;
2209 if (strcmp(type, "s16") == 0)
2210 return (unsigned long long)(short)val & 0xffff;
2212 if (strcmp(type, "s32") == 0)
2213 return (unsigned long long)(int)val & 0xffffffff;
2215 if (strncmp(type, "unsigned ", 9) == 0) {
2220 if (strcmp(type, "char") == 0) {
2222 return (unsigned long long)(char)val & 0xff;
2227 if (strcmp(type, "short") == 0) {
2229 return (unsigned long long)(short)val & 0xffff;
2231 return val & 0xffff;
2234 if (strcmp(type, "int") == 0) {
2236 return (unsigned long long)(int)val & 0xffffffff;
2238 return val & 0xffffffff;
2245 * Try to figure out the type.
2247 static unsigned long long
2248 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2250 if (arg->type != PRINT_TYPE) {
2251 do_warning("expected type argument");
2255 return eval_type_str(val, arg->typecast.type, pointer);
2258 static int arg_num_eval(struct print_arg *arg, long long *val)
2260 long long left, right;
2263 switch (arg->type) {
2265 *val = strtoll(arg->atom.atom, NULL, 0);
2268 ret = arg_num_eval(arg->typecast.item, val);
2271 *val = eval_type(*val, arg, 0);
2274 switch (arg->op.op[0]) {
2276 ret = arg_num_eval(arg->op.left, &left);
2279 ret = arg_num_eval(arg->op.right, &right);
2283 *val = left || right;
2285 *val = left | right;
2288 ret = arg_num_eval(arg->op.left, &left);
2291 ret = arg_num_eval(arg->op.right, &right);
2295 *val = left && right;
2297 *val = left & right;
2300 ret = arg_num_eval(arg->op.left, &left);
2303 ret = arg_num_eval(arg->op.right, &right);
2306 switch (arg->op.op[1]) {
2308 *val = left < right;
2311 *val = left << right;
2314 *val = left <= right;
2317 do_warning("unknown op '%s'", arg->op.op);
2322 ret = arg_num_eval(arg->op.left, &left);
2325 ret = arg_num_eval(arg->op.right, &right);
2328 switch (arg->op.op[1]) {
2330 *val = left > right;
2333 *val = left >> right;
2336 *val = left >= right;
2339 do_warning("unknown op '%s'", arg->op.op);
2344 ret = arg_num_eval(arg->op.left, &left);
2347 ret = arg_num_eval(arg->op.right, &right);
2351 if (arg->op.op[1] != '=') {
2352 do_warning("unknown op '%s'", arg->op.op);
2355 *val = left == right;
2358 ret = arg_num_eval(arg->op.left, &left);
2361 ret = arg_num_eval(arg->op.right, &right);
2365 switch (arg->op.op[1]) {
2367 *val = left != right;
2370 do_warning("unknown op '%s'", arg->op.op);
2375 /* check for negative */
2376 if (arg->op.left->type == PRINT_NULL)
2379 ret = arg_num_eval(arg->op.left, &left);
2382 ret = arg_num_eval(arg->op.right, &right);
2385 *val = left - right;
2388 if (arg->op.left->type == PRINT_NULL)
2391 ret = arg_num_eval(arg->op.left, &left);
2394 ret = arg_num_eval(arg->op.right, &right);
2397 *val = left + right;
2400 do_warning("unknown op '%s'", arg->op.op);
2406 case PRINT_FIELD ... PRINT_SYMBOL:
2411 do_warning("invalid eval type %d", arg->type);
2418 static char *arg_eval (struct print_arg *arg)
2421 static char buf[20];
2423 switch (arg->type) {
2425 return arg->atom.atom;
2427 return arg_eval(arg->typecast.item);
2429 if (!arg_num_eval(arg, &val))
2431 sprintf(buf, "%lld", val);
2435 case PRINT_FIELD ... PRINT_SYMBOL:
2440 do_warning("invalid eval type %d", arg->type);
2447 static enum event_type
2448 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2450 enum event_type type;
2451 struct print_arg *arg = NULL;
2452 struct print_flag_sym *field;
2458 type = read_token_item(&token);
2459 if (test_type_token(type, token, EVENT_OP, "{"))
2467 type = process_arg(event, arg, &token);
2469 if (type == EVENT_OP)
2470 type = process_op(event, arg, &token);
2472 if (type == EVENT_ERROR)
2475 if (test_type_token(type, token, EVENT_DELIM, ","))
2478 field = calloc(1, sizeof(*field));
2482 value = arg_eval(arg);
2484 goto out_free_field;
2485 field->value = strdup(value);
2486 if (field->value == NULL)
2487 goto out_free_field;
2495 type = process_arg(event, arg, &token);
2496 if (test_type_token(type, token, EVENT_OP, "}"))
2497 goto out_free_field;
2499 value = arg_eval(arg);
2501 goto out_free_field;
2502 field->str = strdup(value);
2503 if (field->str == NULL)
2504 goto out_free_field;
2509 list = &field->next;
2512 type = read_token_item(&token);
2513 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2519 free_flag_sym(field);
2528 static enum event_type
2529 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2531 struct print_arg *field;
2532 enum event_type type;
2535 memset(arg, 0, sizeof(*arg));
2536 arg->type = PRINT_FLAGS;
2538 field = alloc_arg();
2540 do_warning_event(event, "%s: not enough memory!", __func__);
2544 type = process_field_arg(event, field, &token);
2546 /* Handle operations in the first argument */
2547 while (type == EVENT_OP)
2548 type = process_op(event, field, &token);
2550 if (test_type_token(type, token, EVENT_DELIM, ","))
2551 goto out_free_field;
2554 arg->flags.field = field;
2556 type = read_token_item(&token);
2557 if (event_item_type(type)) {
2558 arg->flags.delim = token;
2559 type = read_token_item(&token);
2562 if (test_type_token(type, token, EVENT_DELIM, ","))
2565 type = process_fields(event, &arg->flags.flags, &token);
2566 if (test_type_token(type, token, EVENT_DELIM, ")"))
2570 type = read_token_item(tok);
2581 static enum event_type
2582 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2584 struct print_arg *field;
2585 enum event_type type;
2588 memset(arg, 0, sizeof(*arg));
2589 arg->type = PRINT_SYMBOL;
2591 field = alloc_arg();
2593 do_warning_event(event, "%s: not enough memory!", __func__);
2597 type = process_field_arg(event, field, &token);
2599 if (test_type_token(type, token, EVENT_DELIM, ","))
2600 goto out_free_field;
2602 arg->symbol.field = field;
2604 type = process_fields(event, &arg->symbol.symbols, &token);
2605 if (test_type_token(type, token, EVENT_DELIM, ")"))
2609 type = read_token_item(tok);
2620 static enum event_type
2621 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2623 memset(arg, 0, sizeof(*arg));
2624 arg->type = PRINT_HEX;
2626 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2629 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2632 return read_token_item(tok);
2635 free_arg(arg->hex.field);
2641 static enum event_type
2642 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2644 memset(arg, 0, sizeof(*arg));
2645 arg->type = PRINT_INT_ARRAY;
2647 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2650 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2653 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2656 return read_token_item(tok);
2659 free_arg(arg->int_array.count);
2661 free_arg(arg->int_array.field);
2667 static enum event_type
2668 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2670 struct format_field *field;
2671 enum event_type type;
2674 memset(arg, 0, sizeof(*arg));
2675 arg->type = PRINT_DYNAMIC_ARRAY;
2678 * The item within the parenthesis is another field that holds
2679 * the index into where the array starts.
2681 type = read_token(&token);
2683 if (type != EVENT_ITEM)
2686 /* Find the field */
2688 field = pevent_find_field(event, token);
2692 arg->dynarray.field = field;
2693 arg->dynarray.index = 0;
2695 if (read_expected(EVENT_DELIM, ")") < 0)
2699 type = read_token_item(&token);
2701 if (type != EVENT_OP || strcmp(token, "[") != 0)
2707 do_warning_event(event, "%s: not enough memory!", __func__);
2712 type = process_arg(event, arg, &token);
2713 if (type == EVENT_ERROR)
2716 if (!test_type_token(type, token, EVENT_OP, "]"))
2720 type = read_token_item(tok);
2731 static enum event_type
2732 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2734 struct print_arg *item_arg;
2735 enum event_type type;
2738 type = process_arg(event, arg, &token);
2740 if (type == EVENT_ERROR)
2743 if (type == EVENT_OP)
2744 type = process_op(event, arg, &token);
2746 if (type == EVENT_ERROR)
2749 if (test_type_token(type, token, EVENT_DELIM, ")"))
2753 type = read_token_item(&token);
2756 * If the next token is an item or another open paren, then
2757 * this was a typecast.
2759 if (event_item_type(type) ||
2760 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2762 /* make this a typecast and contine */
2764 /* prevous must be an atom */
2765 if (arg->type != PRINT_ATOM) {
2766 do_warning_event(event, "previous needed to be PRINT_ATOM");
2770 item_arg = alloc_arg();
2772 do_warning_event(event, "%s: not enough memory!",
2777 arg->type = PRINT_TYPE;
2778 arg->typecast.type = arg->atom.atom;
2779 arg->typecast.item = item_arg;
2780 type = process_arg_token(event, item_arg, &token, type);
2794 static enum event_type
2795 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2798 enum event_type type;
2801 if (read_expect_type(EVENT_ITEM, &token) < 0)
2804 arg->type = PRINT_STRING;
2805 arg->string.string = token;
2806 arg->string.offset = -1;
2808 if (read_expected(EVENT_DELIM, ")") < 0)
2811 type = read_token(&token);
2823 static enum event_type
2824 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2827 enum event_type type;
2830 if (read_expect_type(EVENT_ITEM, &token) < 0)
2833 arg->type = PRINT_BITMASK;
2834 arg->bitmask.bitmask = token;
2835 arg->bitmask.offset = -1;
2837 if (read_expected(EVENT_DELIM, ")") < 0)
2840 type = read_token(&token);
2852 static struct pevent_function_handler *
2853 find_func_handler(struct pevent *pevent, char *func_name)
2855 struct pevent_function_handler *func;
2860 for (func = pevent->func_handlers; func; func = func->next) {
2861 if (strcmp(func->name, func_name) == 0)
2868 static void remove_func_handler(struct pevent *pevent, char *func_name)
2870 struct pevent_function_handler *func;
2871 struct pevent_function_handler **next;
2873 next = &pevent->func_handlers;
2874 while ((func = *next)) {
2875 if (strcmp(func->name, func_name) == 0) {
2877 free_func_handle(func);
2884 static enum event_type
2885 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2886 struct print_arg *arg, char **tok)
2888 struct print_arg **next_arg;
2889 struct print_arg *farg;
2890 enum event_type type;
2894 arg->type = PRINT_FUNC;
2895 arg->func.func = func;
2899 next_arg = &(arg->func.args);
2900 for (i = 0; i < func->nr_args; i++) {
2903 do_warning_event(event, "%s: not enough memory!",
2908 type = process_arg(event, farg, &token);
2909 if (i < (func->nr_args - 1)) {
2910 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2911 do_warning_event(event,
2912 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2913 func->name, func->nr_args,
2914 event->name, i + 1);
2918 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2919 do_warning_event(event,
2920 "Error: function '%s()' only expects %d arguments but event %s has more",
2921 func->name, func->nr_args, event->name);
2927 next_arg = &(farg->next);
2931 type = read_token(&token);
2942 static enum event_type
2943 process_function(struct event_format *event, struct print_arg *arg,
2944 char *token, char **tok)
2946 struct pevent_function_handler *func;
2948 if (strcmp(token, "__print_flags") == 0) {
2951 return process_flags(event, arg, tok);
2953 if (strcmp(token, "__print_symbolic") == 0) {
2955 is_symbolic_field = 1;
2956 return process_symbols(event, arg, tok);
2958 if (strcmp(token, "__print_hex") == 0) {
2960 return process_hex(event, arg, tok);
2962 if (strcmp(token, "__print_array") == 0) {
2964 return process_int_array(event, arg, tok);
2966 if (strcmp(token, "__get_str") == 0) {
2968 return process_str(event, arg, tok);
2970 if (strcmp(token, "__get_bitmask") == 0) {
2972 return process_bitmask(event, arg, tok);
2974 if (strcmp(token, "__get_dynamic_array") == 0) {
2976 return process_dynamic_array(event, arg, tok);
2979 func = find_func_handler(event->pevent, token);
2982 return process_func_handler(event, func, arg, tok);
2985 do_warning_event(event, "function %s not defined", token);
2990 static enum event_type
2991 process_arg_token(struct event_format *event, struct print_arg *arg,
2992 char **tok, enum event_type type)
3001 if (strcmp(token, "REC") == 0) {
3003 type = process_entry(event, arg, &token);
3007 /* test the next token */
3008 type = read_token_item(&token);
3011 * If the next token is a parenthesis, then this
3014 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3017 /* this will free atom. */
3018 type = process_function(event, arg, atom, &token);
3021 /* atoms can be more than one token long */
3022 while (type == EVENT_ITEM) {
3024 new_atom = realloc(atom,
3025 strlen(atom) + strlen(token) + 2);
3034 strcat(atom, token);
3036 type = read_token_item(&token);
3039 arg->type = PRINT_ATOM;
3040 arg->atom.atom = atom;
3045 arg->type = PRINT_ATOM;
3046 arg->atom.atom = token;
3047 type = read_token_item(&token);
3050 if (strcmp(token, "(") == 0) {
3052 type = process_paren(event, arg, &token);
3056 /* handle single ops */
3057 arg->type = PRINT_OP;
3059 arg->op.left = NULL;
3060 type = process_op(event, arg, &token);
3062 /* On error, the op is freed */
3063 if (type == EVENT_ERROR)
3066 /* return error type if errored */
3069 case EVENT_ERROR ... EVENT_NEWLINE:
3071 do_warning_event(event, "unexpected type %d", type);
3079 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3081 enum event_type type = EVENT_ERROR;
3082 struct print_arg *arg;
3087 if (type == EVENT_NEWLINE) {
3088 type = read_token_item(&token);
3094 do_warning_event(event, "%s: not enough memory!",
3099 type = process_arg(event, arg, &token);
3101 if (type == EVENT_ERROR) {
3110 if (type == EVENT_OP) {
3111 type = process_op(event, arg, &token);
3113 if (type == EVENT_ERROR) {
3122 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3129 } while (type != EVENT_NONE);
3131 if (type != EVENT_NONE && type != EVENT_ERROR)
3137 static int event_read_print(struct event_format *event)
3139 enum event_type type;
3143 if (read_expected_item(EVENT_ITEM, "print") < 0)
3146 if (read_expected(EVENT_ITEM, "fmt") < 0)
3149 if (read_expected(EVENT_OP, ":") < 0)
3152 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3156 event->print_fmt.format = token;
3157 event->print_fmt.args = NULL;
3159 /* ok to have no arg */
3160 type = read_token_item(&token);
3162 if (type == EVENT_NONE)
3165 /* Handle concatenation of print lines */
3166 if (type == EVENT_DQUOTE) {
3169 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3172 free_token(event->print_fmt.format);
3173 event->print_fmt.format = NULL;
3178 if (test_type_token(type, token, EVENT_DELIM, ","))
3183 ret = event_read_print_args(event, &event->print_fmt.args);
3195 * pevent_find_common_field - return a common field by event
3196 * @event: handle for the event
3197 * @name: the name of the common field to return
3199 * Returns a common field from the event by the given @name.
3200 * This only searchs the common fields and not all field.
3202 struct format_field *
3203 pevent_find_common_field(struct event_format *event, const char *name)
3205 struct format_field *format;
3207 for (format = event->format.common_fields;
3208 format; format = format->next) {
3209 if (strcmp(format->name, name) == 0)
3217 * pevent_find_field - find a non-common field
3218 * @event: handle for the event
3219 * @name: the name of the non-common field
3221 * Returns a non-common field by the given @name.
3222 * This does not search common fields.
3224 struct format_field *
3225 pevent_find_field(struct event_format *event, const char *name)
3227 struct format_field *format;
3229 for (format = event->format.fields;
3230 format; format = format->next) {
3231 if (strcmp(format->name, name) == 0)
3239 * pevent_find_any_field - find any field by name
3240 * @event: handle for the event
3241 * @name: the name of the field
3243 * Returns a field by the given @name.
3244 * This searchs the common field names first, then
3245 * the non-common ones if a common one was not found.
3247 struct format_field *
3248 pevent_find_any_field(struct event_format *event, const char *name)
3250 struct format_field *format;
3252 format = pevent_find_common_field(event, name);
3255 return pevent_find_field(event, name);
3259 * pevent_read_number - read a number from data
3260 * @pevent: handle for the pevent
3261 * @ptr: the raw data
3262 * @size: the size of the data that holds the number
3264 * Returns the number (converted to host) from the
3267 unsigned long long pevent_read_number(struct pevent *pevent,
3268 const void *ptr, int size)
3272 return *(unsigned char *)ptr;
3274 return data2host2(pevent, ptr);
3276 return data2host4(pevent, ptr);
3278 return data2host8(pevent, ptr);
3286 * pevent_read_number_field - read a number from data
3287 * @field: a handle to the field
3288 * @data: the raw data to read
3289 * @value: the value to place the number in
3291 * Reads raw data according to a field offset and size,
3292 * and translates it into @value.
3294 * Returns 0 on success, -1 otherwise.
3296 int pevent_read_number_field(struct format_field *field, const void *data,
3297 unsigned long long *value)
3301 switch (field->size) {
3306 *value = pevent_read_number(field->event->pevent,
3307 data + field->offset, field->size);
3314 static int get_common_info(struct pevent *pevent,
3315 const char *type, int *offset, int *size)
3317 struct event_format *event;
3318 struct format_field *field;
3321 * All events should have the same common elements.
3322 * Pick any event to find where the type is;
3324 if (!pevent->events) {
3325 do_warning("no event_list!");
3329 event = pevent->events[0];
3330 field = pevent_find_common_field(event, type);
3334 *offset = field->offset;
3335 *size = field->size;
3340 static int __parse_common(struct pevent *pevent, void *data,
3341 int *size, int *offset, const char *name)
3346 ret = get_common_info(pevent, name, offset, size);
3350 return pevent_read_number(pevent, data + *offset, *size);
3353 static int trace_parse_common_type(struct pevent *pevent, void *data)
3355 return __parse_common(pevent, data,
3356 &pevent->type_size, &pevent->type_offset,
3360 static int parse_common_pid(struct pevent *pevent, void *data)
3362 return __parse_common(pevent, data,
3363 &pevent->pid_size, &pevent->pid_offset,
3367 static int parse_common_pc(struct pevent *pevent, void *data)
3369 return __parse_common(pevent, data,
3370 &pevent->pc_size, &pevent->pc_offset,
3371 "common_preempt_count");
3374 static int parse_common_flags(struct pevent *pevent, void *data)
3376 return __parse_common(pevent, data,
3377 &pevent->flags_size, &pevent->flags_offset,
3381 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3383 return __parse_common(pevent, data,
3384 &pevent->ld_size, &pevent->ld_offset,
3385 "common_lock_depth");
3388 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3390 return __parse_common(pevent, data,
3391 &pevent->ld_size, &pevent->ld_offset,
3392 "common_migrate_disable");
3395 static int events_id_cmp(const void *a, const void *b);
3398 * pevent_find_event - find an event by given id
3399 * @pevent: a handle to the pevent
3400 * @id: the id of the event
3402 * Returns an event that has a given @id.
3404 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3406 struct event_format **eventptr;
3407 struct event_format key;
3408 struct event_format *pkey = &key;
3410 /* Check cache first */
3411 if (pevent->last_event && pevent->last_event->id == id)
3412 return pevent->last_event;
3416 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3417 sizeof(*pevent->events), events_id_cmp);
3420 pevent->last_event = *eventptr;
3428 * pevent_find_event_by_name - find an event by given name
3429 * @pevent: a handle to the pevent
3430 * @sys: the system name to search for
3431 * @name: the name of the event to search for
3433 * This returns an event with a given @name and under the system
3434 * @sys. If @sys is NULL the first event with @name is returned.
3436 struct event_format *
3437 pevent_find_event_by_name(struct pevent *pevent,
3438 const char *sys, const char *name)
3440 struct event_format *event;
3443 if (pevent->last_event &&
3444 strcmp(pevent->last_event->name, name) == 0 &&
3445 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3446 return pevent->last_event;
3448 for (i = 0; i < pevent->nr_events; i++) {
3449 event = pevent->events[i];
3450 if (strcmp(event->name, name) == 0) {
3453 if (strcmp(event->system, sys) == 0)
3457 if (i == pevent->nr_events)
3460 pevent->last_event = event;
3464 static unsigned long long
3465 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3467 struct pevent *pevent = event->pevent;
3468 unsigned long long val = 0;
3469 unsigned long long left, right;
3470 struct print_arg *typearg = NULL;
3471 struct print_arg *larg;
3472 unsigned long offset;
3473 unsigned int field_size;
3475 switch (arg->type) {
3480 return strtoull(arg->atom.atom, NULL, 0);
3482 if (!arg->field.field) {
3483 arg->field.field = pevent_find_any_field(event, arg->field.name);
3484 if (!arg->field.field)
3485 goto out_warning_field;
3488 /* must be a number */
3489 val = pevent_read_number(pevent, data + arg->field.field->offset,
3490 arg->field.field->size);
3494 case PRINT_INT_ARRAY:
3498 val = eval_num_arg(data, size, event, arg->typecast.item);
3499 return eval_type(val, arg, 0);
3507 val = process_defined_func(&s, data, size, event, arg);
3508 trace_seq_destroy(&s);
3512 if (strcmp(arg->op.op, "[") == 0) {
3514 * Arrays are special, since we don't want
3515 * to read the arg as is.
3517 right = eval_num_arg(data, size, event, arg->op.right);
3519 /* handle typecasts */
3520 larg = arg->op.left;
3521 while (larg->type == PRINT_TYPE) {
3524 larg = larg->typecast.item;
3527 /* Default to long size */
3528 field_size = pevent->long_size;
3530 switch (larg->type) {
3531 case PRINT_DYNAMIC_ARRAY:
3532 offset = pevent_read_number(pevent,
3533 data + larg->dynarray.field->offset,
3534 larg->dynarray.field->size);
3535 if (larg->dynarray.field->elementsize)
3536 field_size = larg->dynarray.field->elementsize;
3538 * The actual length of the dynamic array is stored
3539 * in the top half of the field, and the offset
3540 * is in the bottom half of the 32 bit field.
3546 if (!larg->field.field) {
3548 pevent_find_any_field(event, larg->field.name);
3549 if (!larg->field.field) {
3551 goto out_warning_field;
3554 field_size = larg->field.field->elementsize;
3555 offset = larg->field.field->offset +
3556 right * larg->field.field->elementsize;
3559 goto default_op; /* oops, all bets off */
3561 val = pevent_read_number(pevent,
3562 data + offset, field_size);
3564 val = eval_type(val, typearg, 1);
3566 } else if (strcmp(arg->op.op, "?") == 0) {
3567 left = eval_num_arg(data, size, event, arg->op.left);
3568 arg = arg->op.right;
3570 val = eval_num_arg(data, size, event, arg->op.left);
3572 val = eval_num_arg(data, size, event, arg->op.right);
3576 left = eval_num_arg(data, size, event, arg->op.left);
3577 right = eval_num_arg(data, size, event, arg->op.right);
3578 switch (arg->op.op[0]) {
3580 switch (arg->op.op[1]) {
3585 val = left != right;
3588 goto out_warning_op;
3596 val = left || right;
3602 val = left && right;
3607 switch (arg->op.op[1]) {
3612 val = left << right;
3615 val = left <= right;
3618 goto out_warning_op;
3622 switch (arg->op.op[1]) {
3627 val = left >> right;
3630 val = left >= right;
3633 goto out_warning_op;
3637 if (arg->op.op[1] != '=')
3638 goto out_warning_op;
3640 val = left == right;
3655 goto out_warning_op;
3658 case PRINT_DYNAMIC_ARRAY:
3659 /* Without [], we pass the address to the dynamic data */
3660 offset = pevent_read_number(pevent,
3661 data + arg->dynarray.field->offset,
3662 arg->dynarray.field->size);
3664 * The actual length of the dynamic array is stored
3665 * in the top half of the field, and the offset
3666 * is in the bottom half of the 32 bit field.
3669 val = (unsigned long long)((unsigned long)data + offset);
3671 default: /* not sure what to do there */
3677 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3681 do_warning_event(event, "%s: field %s not found",
3682 __func__, arg->field.name);
3688 unsigned long long value;
3691 static const struct flag flags[] = {
3692 { "HI_SOFTIRQ", 0 },
3693 { "TIMER_SOFTIRQ", 1 },
3694 { "NET_TX_SOFTIRQ", 2 },
3695 { "NET_RX_SOFTIRQ", 3 },
3696 { "BLOCK_SOFTIRQ", 4 },
3697 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3698 { "TASKLET_SOFTIRQ", 6 },
3699 { "SCHED_SOFTIRQ", 7 },
3700 { "HRTIMER_SOFTIRQ", 8 },
3701 { "RCU_SOFTIRQ", 9 },
3703 { "HRTIMER_NORESTART", 0 },
3704 { "HRTIMER_RESTART", 1 },
3707 static long long eval_flag(const char *flag)
3712 * Some flags in the format files do not get converted.
3713 * If the flag is not numeric, see if it is something that
3714 * we already know about.
3716 if (isdigit(flag[0]))
3717 return strtoull(flag, NULL, 0);
3719 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3720 if (strcmp(flags[i].name, flag) == 0)
3721 return flags[i].value;
3726 static void print_str_to_seq(struct trace_seq *s, const char *format,
3727 int len_arg, const char *str)
3730 trace_seq_printf(s, format, len_arg, str);
3732 trace_seq_printf(s, format, str);
3735 static void print_bitmask_to_seq(struct pevent *pevent,
3736 struct trace_seq *s, const char *format,
3737 int len_arg, const void *data, int size)
3739 int nr_bits = size * 8;
3740 int str_size = (nr_bits + 3) / 4;
3748 * The kernel likes to put in commas every 32 bits, we
3751 str_size += (nr_bits - 1) / 32;
3753 str = malloc(str_size + 1);
3755 do_warning("%s: not enough memory!", __func__);
3760 /* Start out with -2 for the two chars per byte */
3761 for (i = str_size - 2; i >= 0; i -= 2) {
3763 * data points to a bit mask of size bytes.
3764 * In the kernel, this is an array of long words, thus
3765 * endianess is very important.
3767 if (pevent->file_bigendian)
3768 index = size - (len + 1);
3772 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3773 memcpy(str + i, buf, 2);
3775 if (!(len & 3) && i > 0) {
3782 trace_seq_printf(s, format, len_arg, str);
3784 trace_seq_printf(s, format, str);
3789 static void print_str_arg(struct trace_seq *s, void *data, int size,
3790 struct event_format *event, const char *format,
3791 int len_arg, struct print_arg *arg)
3793 struct pevent *pevent = event->pevent;
3794 struct print_flag_sym *flag;
3795 struct format_field *field;
3796 struct printk_map *printk;
3797 long long val, fval;
3804 switch (arg->type) {
3809 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3812 field = arg->field.field;
3814 field = pevent_find_any_field(event, arg->field.name);
3816 str = arg->field.name;
3817 goto out_warning_field;
3819 arg->field.field = field;
3821 /* Zero sized fields, mean the rest of the data */
3822 len = field->size ? : size - field->offset;
3825 * Some events pass in pointers. If this is not an array
3826 * and the size is the same as long_size, assume that it
3829 if (!(field->flags & FIELD_IS_ARRAY) &&
3830 field->size == pevent->long_size) {
3831 addr = *(unsigned long *)(data + field->offset);
3832 /* Check if it matches a print format */
3833 printk = find_printk(pevent, addr);
3835 trace_seq_puts(s, printk->printk);
3837 trace_seq_printf(s, "%lx", addr);
3840 str = malloc(len + 1);
3842 do_warning_event(event, "%s: not enough memory!",
3846 memcpy(str, data + field->offset, len);
3848 print_str_to_seq(s, format, len_arg, str);
3852 val = eval_num_arg(data, size, event, arg->flags.field);
3854 for (flag = arg->flags.flags; flag; flag = flag->next) {
3855 fval = eval_flag(flag->value);
3856 if (!val && fval < 0) {
3857 print_str_to_seq(s, format, len_arg, flag->str);
3860 if (fval > 0 && (val & fval) == fval) {
3861 if (print && arg->flags.delim)
3862 trace_seq_puts(s, arg->flags.delim);
3863 print_str_to_seq(s, format, len_arg, flag->str);
3870 val = eval_num_arg(data, size, event, arg->symbol.field);
3871 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3872 fval = eval_flag(flag->value);
3874 print_str_to_seq(s, format, len_arg, flag->str);
3880 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3881 unsigned long offset;
3882 offset = pevent_read_number(pevent,
3883 data + arg->hex.field->dynarray.field->offset,
3884 arg->hex.field->dynarray.field->size);
3885 hex = data + (offset & 0xffff);
3887 field = arg->hex.field->field.field;
3889 str = arg->hex.field->field.name;
3890 field = pevent_find_any_field(event, str);
3892 goto out_warning_field;
3893 arg->hex.field->field.field = field;
3895 hex = data + field->offset;
3897 len = eval_num_arg(data, size, event, arg->hex.size);
3898 for (i = 0; i < len; i++) {
3900 trace_seq_putc(s, ' ');
3901 trace_seq_printf(s, "%02x", hex[i]);
3905 case PRINT_INT_ARRAY: {
3909 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3910 unsigned long offset;
3911 struct format_field *field =
3912 arg->int_array.field->dynarray.field;
3913 offset = pevent_read_number(pevent,
3914 data + field->offset,
3916 num = data + (offset & 0xffff);
3918 field = arg->int_array.field->field.field;
3920 str = arg->int_array.field->field.name;
3921 field = pevent_find_any_field(event, str);
3923 goto out_warning_field;
3924 arg->int_array.field->field.field = field;
3926 num = data + field->offset;
3928 len = eval_num_arg(data, size, event, arg->int_array.count);
3929 el_size = eval_num_arg(data, size, event,
3930 arg->int_array.el_size);
3931 for (i = 0; i < len; i++) {
3933 trace_seq_putc(s, ' ');
3936 trace_seq_printf(s, "%u", *(uint8_t *)num);
3937 } else if (el_size == 2) {
3938 trace_seq_printf(s, "%u", *(uint16_t *)num);
3939 } else if (el_size == 4) {
3940 trace_seq_printf(s, "%u", *(uint32_t *)num);
3941 } else if (el_size == 8) {
3942 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
3944 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3945 el_size, *(uint8_t *)num);
3955 case PRINT_STRING: {
3958 if (arg->string.offset == -1) {
3959 struct format_field *f;
3961 f = pevent_find_any_field(event, arg->string.string);
3962 arg->string.offset = f->offset;
3964 str_offset = data2host4(pevent, data + arg->string.offset);
3965 str_offset &= 0xffff;
3966 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3970 print_str_to_seq(s, format, len_arg, arg->string.string);
3972 case PRINT_BITMASK: {
3976 if (arg->bitmask.offset == -1) {
3977 struct format_field *f;
3979 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3980 arg->bitmask.offset = f->offset;
3982 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3983 bitmask_size = bitmask_offset >> 16;
3984 bitmask_offset &= 0xffff;
3985 print_bitmask_to_seq(pevent, s, format, len_arg,
3986 data + bitmask_offset, bitmask_size);
3991 * The only op for string should be ? :
3993 if (arg->op.op[0] != '?')
3995 val = eval_num_arg(data, size, event, arg->op.left);
3997 print_str_arg(s, data, size, event,
3998 format, len_arg, arg->op.right->op.left);
4000 print_str_arg(s, data, size, event,
4001 format, len_arg, arg->op.right->op.right);
4004 process_defined_func(s, data, size, event, arg);
4014 do_warning_event(event, "%s: field %s not found",
4015 __func__, arg->field.name);
4018 static unsigned long long
4019 process_defined_func(struct trace_seq *s, void *data, int size,
4020 struct event_format *event, struct print_arg *arg)
4022 struct pevent_function_handler *func_handle = arg->func.func;
4023 struct pevent_func_params *param;
4024 unsigned long long *args;
4025 unsigned long long ret;
4026 struct print_arg *farg;
4027 struct trace_seq str;
4029 struct save_str *next;
4031 } *strings = NULL, *string;
4034 if (!func_handle->nr_args) {
4035 ret = (*func_handle->func)(s, NULL);
4039 farg = arg->func.args;
4040 param = func_handle->params;
4043 args = malloc(sizeof(*args) * func_handle->nr_args);
4047 for (i = 0; i < func_handle->nr_args; i++) {
4048 switch (param->type) {
4049 case PEVENT_FUNC_ARG_INT:
4050 case PEVENT_FUNC_ARG_LONG:
4051 case PEVENT_FUNC_ARG_PTR:
4052 args[i] = eval_num_arg(data, size, event, farg);
4054 case PEVENT_FUNC_ARG_STRING:
4055 trace_seq_init(&str);
4056 print_str_arg(&str, data, size, event, "%s", -1, farg);
4057 trace_seq_terminate(&str);
4058 string = malloc(sizeof(*string));
4060 do_warning_event(event, "%s(%d): malloc str",
4061 __func__, __LINE__);
4064 string->next = strings;
4065 string->str = strdup(str.buffer);
4068 do_warning_event(event, "%s(%d): malloc str",
4069 __func__, __LINE__);
4072 args[i] = (uintptr_t)string->str;
4074 trace_seq_destroy(&str);
4078 * Something went totally wrong, this is not
4079 * an input error, something in this code broke.
4081 do_warning_event(event, "Unexpected end of arguments\n");
4085 param = param->next;
4088 ret = (*func_handle->func)(s, args);
4093 strings = string->next;
4099 /* TBD : handle return type here */
4103 static void free_args(struct print_arg *args)
4105 struct print_arg *next;
4115 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4117 struct pevent *pevent = event->pevent;
4118 struct format_field *field, *ip_field;
4119 struct print_arg *args, *arg, **next;
4120 unsigned long long ip, val;
4125 field = pevent->bprint_buf_field;
4126 ip_field = pevent->bprint_ip_field;
4129 field = pevent_find_field(event, "buf");
4131 do_warning_event(event, "can't find buffer field for binary printk");
4134 ip_field = pevent_find_field(event, "ip");
4136 do_warning_event(event, "can't find ip field for binary printk");
4139 pevent->bprint_buf_field = field;
4140 pevent->bprint_ip_field = ip_field;
4143 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4146 * The first arg is the IP pointer.
4150 do_warning_event(event, "%s(%d): not enough memory!",
4151 __func__, __LINE__);
4158 arg->type = PRINT_ATOM;
4160 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4163 /* skip the first "%ps: " */
4164 for (ptr = fmt + 5, bptr = data + field->offset;
4165 bptr < data + size && *ptr; ptr++) {
4200 vsize = pevent->long_size;
4214 /* the pointers are always 4 bytes aligned */
4215 bptr = (void *)(((unsigned long)bptr + 3) &
4217 val = pevent_read_number(pevent, bptr, vsize);
4221 do_warning_event(event, "%s(%d): not enough memory!",
4222 __func__, __LINE__);
4226 arg->type = PRINT_ATOM;
4227 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4234 * The '*' case means that an arg is used as the length.
4235 * We need to continue to figure out for what.
4244 do_warning_event(event, "%s(%d): not enough memory!",
4245 __func__, __LINE__);
4249 arg->type = PRINT_BSTRING;
4250 arg->string.string = strdup(bptr);
4251 if (!arg->string.string)
4253 bptr += strlen(bptr) + 1;
4270 get_bprint_format(void *data, int size __maybe_unused,
4271 struct event_format *event)
4273 struct pevent *pevent = event->pevent;
4274 unsigned long long addr;
4275 struct format_field *field;
4276 struct printk_map *printk;
4279 field = pevent->bprint_fmt_field;
4282 field = pevent_find_field(event, "fmt");
4284 do_warning_event(event, "can't find format field for binary printk");
4287 pevent->bprint_fmt_field = field;
4290 addr = pevent_read_number(pevent, data + field->offset, field->size);
4292 printk = find_printk(pevent, addr);
4294 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4299 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4305 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4306 struct event_format *event, struct print_arg *arg)
4309 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4311 if (arg->type == PRINT_FUNC) {
4312 process_defined_func(s, data, size, event, arg);
4316 if (arg->type != PRINT_FIELD) {
4317 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4323 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4324 if (!arg->field.field) {
4326 pevent_find_any_field(event, arg->field.name);
4327 if (!arg->field.field) {
4328 do_warning_event(event, "%s: field %s not found",
4329 __func__, arg->field.name);
4333 if (arg->field.field->size != 6) {
4334 trace_seq_printf(s, "INVALIDMAC");
4337 buf = data + arg->field.field->offset;
4338 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4341 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4346 fmt = "%03d.%03d.%03d.%03d";
4348 fmt = "%d.%d.%d.%d";
4350 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4353 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4355 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4356 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4359 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4361 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4364 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4367 unsigned char zerolength[8];
4372 bool needcolon = false;
4374 struct in6_addr in6;
4376 memcpy(&in6, addr, sizeof(struct in6_addr));
4378 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4380 memset(zerolength, 0, sizeof(zerolength));
4387 /* find position of longest 0 run */
4388 for (i = 0; i < range; i++) {
4389 for (j = i; j < range; j++) {
4390 if (in6.s6_addr16[j] != 0)
4395 for (i = 0; i < range; i++) {
4396 if (zerolength[i] > longest) {
4397 longest = zerolength[i];
4401 if (longest == 1) /* don't compress a single 0 */
4405 for (i = 0; i < range; i++) {
4406 if (i == colonpos) {
4407 if (needcolon || i == 0)
4408 trace_seq_printf(s, ":");
4409 trace_seq_printf(s, ":");
4415 trace_seq_printf(s, ":");
4418 /* hex u16 without leading 0s */
4419 word = ntohs(in6.s6_addr16[i]);
4423 trace_seq_printf(s, "%x%02x", hi, lo);
4425 trace_seq_printf(s, "%x", lo);
4432 trace_seq_printf(s, ":");
4433 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4439 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4443 for (j = 0; j < 16; j += 2) {
4444 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4445 if (i == 'I' && j < 14)
4446 trace_seq_printf(s, ":");
4451 * %pi4 print an IPv4 address with leading zeros
4452 * %pI4 print an IPv4 address without leading zeros
4453 * %pi6 print an IPv6 address without colons
4454 * %pI6 print an IPv6 address with colons
4455 * %pI6c print an IPv6 address in compressed form with colons
4456 * %pISpc print an IP address based on sockaddr; p adds port.
4458 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4459 void *data, int size, struct event_format *event,
4460 struct print_arg *arg)
4464 if (arg->type == PRINT_FUNC) {
4465 process_defined_func(s, data, size, event, arg);
4469 if (arg->type != PRINT_FIELD) {
4470 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4474 if (!arg->field.field) {
4476 pevent_find_any_field(event, arg->field.name);
4477 if (!arg->field.field) {
4478 do_warning("%s: field %s not found",
4479 __func__, arg->field.name);
4484 buf = data + arg->field.field->offset;
4486 if (arg->field.field->size != 4) {
4487 trace_seq_printf(s, "INVALIDIPv4");
4490 print_ip4_addr(s, i, buf);
4495 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4496 void *data, int size, struct event_format *event,
4497 struct print_arg *arg)
4504 if (i == 'I' && *ptr == 'c') {
4510 if (arg->type == PRINT_FUNC) {
4511 process_defined_func(s, data, size, event, arg);
4515 if (arg->type != PRINT_FIELD) {
4516 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4520 if (!arg->field.field) {
4522 pevent_find_any_field(event, arg->field.name);
4523 if (!arg->field.field) {
4524 do_warning("%s: field %s not found",
4525 __func__, arg->field.name);
4530 buf = data + arg->field.field->offset;
4532 if (arg->field.field->size != 16) {
4533 trace_seq_printf(s, "INVALIDIPv6");
4538 print_ip6c_addr(s, buf);
4540 print_ip6_addr(s, i, buf);
4545 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4546 void *data, int size, struct event_format *event,
4547 struct print_arg *arg)
4549 char have_c = 0, have_p = 0;
4551 struct sockaddr_storage *sa;
4568 if (arg->type == PRINT_FUNC) {
4569 process_defined_func(s, data, size, event, arg);
4573 if (arg->type != PRINT_FIELD) {
4574 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4578 if (!arg->field.field) {
4580 pevent_find_any_field(event, arg->field.name);
4581 if (!arg->field.field) {
4582 do_warning("%s: field %s not found",
4583 __func__, arg->field.name);
4588 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4590 if (sa->ss_family == AF_INET) {
4591 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4593 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4594 trace_seq_printf(s, "INVALIDIPv4");
4598 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4600 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4603 } else if (sa->ss_family == AF_INET6) {
4604 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4606 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4607 trace_seq_printf(s, "INVALIDIPv6");
4612 trace_seq_printf(s, "[");
4614 buf = (unsigned char *) &sa6->sin6_addr;
4616 print_ip6c_addr(s, buf);
4618 print_ip6_addr(s, i, buf);
4621 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4627 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4628 void *data, int size, struct event_format *event,
4629 struct print_arg *arg)
4631 char i = *ptr; /* 'i' or 'I' */
4644 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4647 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4650 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4659 static int is_printable_array(char *p, unsigned int len)
4663 for (i = 0; i < len && p[i]; i++)
4664 if (!isprint(p[i]) && !isspace(p[i]))
4669 static void print_event_fields(struct trace_seq *s, void *data,
4670 int size __maybe_unused,
4671 struct event_format *event)
4673 struct format_field *field;
4674 unsigned long long val;
4675 unsigned int offset, len, i;
4677 field = event->format.fields;
4679 trace_seq_printf(s, " %s=", field->name);
4680 if (field->flags & FIELD_IS_ARRAY) {
4681 offset = field->offset;
4683 if (field->flags & FIELD_IS_DYNAMIC) {
4684 val = pevent_read_number(event->pevent, data + offset, len);
4689 if (field->flags & FIELD_IS_STRING &&
4690 is_printable_array(data + offset, len)) {
4691 trace_seq_printf(s, "%s", (char *)data + offset);
4693 trace_seq_puts(s, "ARRAY[");
4694 for (i = 0; i < len; i++) {
4696 trace_seq_puts(s, ", ");
4697 trace_seq_printf(s, "%02x",
4698 *((unsigned char *)data + offset + i));
4700 trace_seq_putc(s, ']');
4701 field->flags &= ~FIELD_IS_STRING;
4704 val = pevent_read_number(event->pevent, data + field->offset,
4706 if (field->flags & FIELD_IS_POINTER) {
4707 trace_seq_printf(s, "0x%llx", val);
4708 } else if (field->flags & FIELD_IS_SIGNED) {
4709 switch (field->size) {
4712 * If field is long then print it in hex.
4713 * A long usually stores pointers.
4715 if (field->flags & FIELD_IS_LONG)
4716 trace_seq_printf(s, "0x%x", (int)val);
4718 trace_seq_printf(s, "%d", (int)val);
4721 trace_seq_printf(s, "%2d", (short)val);
4724 trace_seq_printf(s, "%1d", (char)val);
4727 trace_seq_printf(s, "%lld", val);
4730 if (field->flags & FIELD_IS_LONG)
4731 trace_seq_printf(s, "0x%llx", val);
4733 trace_seq_printf(s, "%llu", val);
4736 field = field->next;
4740 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4742 struct pevent *pevent = event->pevent;
4743 struct print_fmt *print_fmt = &event->print_fmt;
4744 struct print_arg *arg = print_fmt->args;
4745 struct print_arg *args = NULL;
4746 const char *ptr = print_fmt->format;
4747 unsigned long long val;
4748 struct func_map *func;
4749 const char *saveptr;
4751 char *bprint_fmt = NULL;
4759 if (event->flags & EVENT_FL_FAILED) {
4760 trace_seq_printf(s, "[FAILED TO PARSE]");
4761 print_event_fields(s, data, size, event);
4765 if (event->flags & EVENT_FL_ISBPRINT) {
4766 bprint_fmt = get_bprint_format(data, size, event);
4767 args = make_bprint_args(bprint_fmt, data, size, event);
4772 for (; *ptr; ptr++) {
4778 trace_seq_putc(s, '\n');
4781 trace_seq_putc(s, '\t');
4784 trace_seq_putc(s, '\r');
4787 trace_seq_putc(s, '\\');
4790 trace_seq_putc(s, *ptr);
4794 } else if (*ptr == '%') {
4802 trace_seq_putc(s, '%');
4805 /* FIXME: need to handle properly */
4817 /* The argument is the length. */
4819 do_warning_event(event, "no argument match");
4820 event->flags |= EVENT_FL_FAILED;
4823 len_arg = eval_num_arg(data, size, event, arg);
4834 if (pevent->long_size == 4)
4839 if (*(ptr+1) == 'F' ||
4843 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4844 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4848 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4851 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4866 do_warning_event(event, "no argument match");
4867 event->flags |= EVENT_FL_FAILED;
4871 len = ((unsigned long)ptr + 1) -
4872 (unsigned long)saveptr;
4874 /* should never happen */
4876 do_warning_event(event, "bad format!");
4877 event->flags |= EVENT_FL_FAILED;
4881 memcpy(format, saveptr, len);
4884 val = eval_num_arg(data, size, event, arg);
4888 func = find_func(pevent, val);
4890 trace_seq_puts(s, func->func);
4891 if (show_func == 'F')
4898 if (pevent->long_size == 8 && ls &&
4899 sizeof(long) != 8) {
4903 /* make %l into %ll */
4904 p = strchr(format, 'l');
4906 memmove(p+1, p, strlen(p)+1);
4907 else if (strcmp(format, "%p") == 0)
4908 strcpy(format, "0x%llx");
4913 trace_seq_printf(s, format, len_arg, (char)val);
4915 trace_seq_printf(s, format, (char)val);
4919 trace_seq_printf(s, format, len_arg, (short)val);
4921 trace_seq_printf(s, format, (short)val);
4925 trace_seq_printf(s, format, len_arg, (int)val);
4927 trace_seq_printf(s, format, (int)val);
4931 trace_seq_printf(s, format, len_arg, (long)val);
4933 trace_seq_printf(s, format, (long)val);
4937 trace_seq_printf(s, format, len_arg,
4940 trace_seq_printf(s, format, (long long)val);
4943 do_warning_event(event, "bad count (%d)", ls);
4944 event->flags |= EVENT_FL_FAILED;
4949 do_warning_event(event, "no matching argument");
4950 event->flags |= EVENT_FL_FAILED;
4954 len = ((unsigned long)ptr + 1) -
4955 (unsigned long)saveptr;
4957 /* should never happen */
4959 do_warning_event(event, "bad format!");
4960 event->flags |= EVENT_FL_FAILED;
4964 memcpy(format, saveptr, len);
4968 /* Use helper trace_seq */
4970 print_str_arg(&p, data, size, event,
4971 format, len_arg, arg);
4972 trace_seq_terminate(&p);
4973 trace_seq_puts(s, p.buffer);
4974 trace_seq_destroy(&p);
4978 trace_seq_printf(s, ">%c<", *ptr);
4982 trace_seq_putc(s, *ptr);
4985 if (event->flags & EVENT_FL_FAILED) {
4987 trace_seq_printf(s, "[FAILED TO PARSE]");
4997 * pevent_data_lat_fmt - parse the data for the latency format
4998 * @pevent: a handle to the pevent
4999 * @s: the trace_seq to write to
5000 * @record: the record to read from
5002 * This parses out the Latency format (interrupts disabled,
5003 * need rescheduling, in hard/soft interrupt, preempt count
5004 * and lock depth) and places it into the trace_seq.
5006 void pevent_data_lat_fmt(struct pevent *pevent,
5007 struct trace_seq *s, struct pevent_record *record)
5009 static int check_lock_depth = 1;
5010 static int check_migrate_disable = 1;
5011 static int lock_depth_exists;
5012 static int migrate_disable_exists;
5013 unsigned int lat_flags;
5016 int migrate_disable;
5019 void *data = record->data;
5021 lat_flags = parse_common_flags(pevent, data);
5022 pc = parse_common_pc(pevent, data);
5023 /* lock_depth may not always exist */
5024 if (lock_depth_exists)
5025 lock_depth = parse_common_lock_depth(pevent, data);
5026 else if (check_lock_depth) {
5027 lock_depth = parse_common_lock_depth(pevent, data);
5029 check_lock_depth = 0;
5031 lock_depth_exists = 1;
5034 /* migrate_disable may not always exist */
5035 if (migrate_disable_exists)
5036 migrate_disable = parse_common_migrate_disable(pevent, data);
5037 else if (check_migrate_disable) {
5038 migrate_disable = parse_common_migrate_disable(pevent, data);
5039 if (migrate_disable < 0)
5040 check_migrate_disable = 0;
5042 migrate_disable_exists = 1;
5045 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5046 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5048 trace_seq_printf(s, "%c%c%c",
5049 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5050 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5052 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5054 (hardirq && softirq) ? 'H' :
5055 hardirq ? 'h' : softirq ? 's' : '.');
5058 trace_seq_printf(s, "%x", pc);
5060 trace_seq_putc(s, '.');
5062 if (migrate_disable_exists) {
5063 if (migrate_disable < 0)
5064 trace_seq_putc(s, '.');
5066 trace_seq_printf(s, "%d", migrate_disable);
5069 if (lock_depth_exists) {
5071 trace_seq_putc(s, '.');
5073 trace_seq_printf(s, "%d", lock_depth);
5076 trace_seq_terminate(s);
5080 * pevent_data_type - parse out the given event type
5081 * @pevent: a handle to the pevent
5082 * @rec: the record to read from
5084 * This returns the event id from the @rec.
5086 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5088 return trace_parse_common_type(pevent, rec->data);
5092 * pevent_data_event_from_type - find the event by a given type
5093 * @pevent: a handle to the pevent
5094 * @type: the type of the event.
5096 * This returns the event form a given @type;
5098 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5100 return pevent_find_event(pevent, type);
5104 * pevent_data_pid - parse the PID from raw data
5105 * @pevent: a handle to the pevent
5106 * @rec: the record to parse
5108 * This returns the PID from a raw data.
5110 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5112 return parse_common_pid(pevent, rec->data);
5116 * pevent_data_comm_from_pid - return the command line from PID
5117 * @pevent: a handle to the pevent
5118 * @pid: the PID of the task to search for
5120 * This returns a pointer to the command line that has the given
5123 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5127 comm = find_cmdline(pevent, pid);
5131 static struct cmdline *
5132 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5134 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5137 cmdlist = cmdlist->next;
5139 cmdlist = pevent->cmdlist;
5141 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5142 cmdlist = cmdlist->next;
5144 return (struct cmdline *)cmdlist;
5148 * pevent_data_pid_from_comm - return the pid from a given comm
5149 * @pevent: a handle to the pevent
5150 * @comm: the cmdline to find the pid from
5151 * @next: the cmdline structure to find the next comm
5153 * This returns the cmdline structure that holds a pid for a given
5154 * comm, or NULL if none found. As there may be more than one pid for
5155 * a given comm, the result of this call can be passed back into
5156 * a recurring call in the @next paramater, and then it will find the
5158 * Also, it does a linear seach, so it may be slow.
5160 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5161 struct cmdline *next)
5163 struct cmdline *cmdline;
5166 * If the cmdlines have not been converted yet, then use
5169 if (!pevent->cmdlines)
5170 return pid_from_cmdlist(pevent, comm, next);
5174 * The next pointer could have been still from
5175 * a previous call before cmdlines were created
5177 if (next < pevent->cmdlines ||
5178 next >= pevent->cmdlines + pevent->cmdline_count)
5185 cmdline = pevent->cmdlines;
5187 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5188 if (strcmp(cmdline->comm, comm) == 0)
5196 * pevent_cmdline_pid - return the pid associated to a given cmdline
5197 * @cmdline: The cmdline structure to get the pid from
5199 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5202 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5204 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5210 * If cmdlines have not been created yet, or cmdline is
5211 * not part of the array, then treat it as a cmdlist instead.
5213 if (!pevent->cmdlines ||
5214 cmdline < pevent->cmdlines ||
5215 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5216 return cmdlist->pid;
5218 return cmdline->pid;
5222 * pevent_data_comm_from_pid - parse the data into the print format
5223 * @s: the trace_seq to write to
5224 * @event: the handle to the event
5225 * @record: the record to read from
5227 * This parses the raw @data using the given @event information and
5228 * writes the print format into the trace_seq.
5230 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5231 struct pevent_record *record)
5233 int print_pretty = 1;
5235 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5236 print_event_fields(s, record->data, record->size, event);
5239 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5240 print_pretty = event->handler(s, record, event,
5244 pretty_print(s, record->data, record->size, event);
5247 trace_seq_terminate(s);
5250 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5252 if (!use_trace_clock)
5255 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5256 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5259 /* trace_clock is setting in tsc or counter mode */
5263 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5264 struct pevent_record *record, bool use_trace_clock)
5266 static const char *spaces = " "; /* 20 spaces */
5267 struct event_format *event;
5269 unsigned long usecs;
5270 unsigned long nsecs;
5272 void *data = record->data;
5277 bool use_usec_format;
5279 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5281 if (use_usec_format) {
5282 secs = record->ts / NSECS_PER_SEC;
5283 nsecs = record->ts - secs * NSECS_PER_SEC;
5286 if (record->size < 0) {
5287 do_warning("ug! negative record size %d", record->size);
5291 type = trace_parse_common_type(pevent, data);
5293 event = pevent_find_event(pevent, type);
5295 do_warning("ug! no event found for type %d", type);
5299 pid = parse_common_pid(pevent, data);
5300 comm = find_cmdline(pevent, pid);
5302 if (pevent->latency_format) {
5303 trace_seq_printf(s, "%8.8s-%-5d %3d",
5304 comm, pid, record->cpu);
5305 pevent_data_lat_fmt(pevent, s, record);
5307 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5309 if (use_usec_format) {
5310 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5314 usecs = (nsecs + 500) / NSECS_PER_USEC;
5318 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5319 secs, p, usecs, event->name);
5321 trace_seq_printf(s, " %12llu: %s: ",
5322 record->ts, event->name);
5324 /* Space out the event names evenly. */
5325 len = strlen(event->name);
5327 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5329 pevent_event_info(s, event, record);
5332 static int events_id_cmp(const void *a, const void *b)
5334 struct event_format * const * ea = a;
5335 struct event_format * const * eb = b;
5337 if ((*ea)->id < (*eb)->id)
5340 if ((*ea)->id > (*eb)->id)
5346 static int events_name_cmp(const void *a, const void *b)
5348 struct event_format * const * ea = a;
5349 struct event_format * const * eb = b;
5352 res = strcmp((*ea)->name, (*eb)->name);
5356 res = strcmp((*ea)->system, (*eb)->system);
5360 return events_id_cmp(a, b);
5363 static int events_system_cmp(const void *a, const void *b)
5365 struct event_format * const * ea = a;
5366 struct event_format * const * eb = b;
5369 res = strcmp((*ea)->system, (*eb)->system);
5373 res = strcmp((*ea)->name, (*eb)->name);
5377 return events_id_cmp(a, b);
5380 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5382 struct event_format **events;
5383 int (*sort)(const void *a, const void *b);
5385 events = pevent->sort_events;
5387 if (events && pevent->last_type == sort_type)
5391 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5395 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5396 events[pevent->nr_events] = NULL;
5398 pevent->sort_events = events;
5400 /* the internal events are sorted by id */
5401 if (sort_type == EVENT_SORT_ID) {
5402 pevent->last_type = sort_type;
5407 switch (sort_type) {
5409 sort = events_id_cmp;
5411 case EVENT_SORT_NAME:
5412 sort = events_name_cmp;
5414 case EVENT_SORT_SYSTEM:
5415 sort = events_system_cmp;
5421 qsort(events, pevent->nr_events, sizeof(*events), sort);
5422 pevent->last_type = sort_type;
5427 static struct format_field **
5428 get_event_fields(const char *type, const char *name,
5429 int count, struct format_field *list)
5431 struct format_field **fields;
5432 struct format_field *field;
5435 fields = malloc(sizeof(*fields) * (count + 1));
5439 for (field = list; field; field = field->next) {
5440 fields[i++] = field;
5441 if (i == count + 1) {
5442 do_warning("event %s has more %s fields than specified",
5450 do_warning("event %s has less %s fields than specified",
5459 * pevent_event_common_fields - return a list of common fields for an event
5460 * @event: the event to return the common fields of.
5462 * Returns an allocated array of fields. The last item in the array is NULL.
5463 * The array must be freed with free().
5465 struct format_field **pevent_event_common_fields(struct event_format *event)
5467 return get_event_fields("common", event->name,
5468 event->format.nr_common,
5469 event->format.common_fields);
5473 * pevent_event_fields - return a list of event specific fields for an event
5474 * @event: the event to return the fields of.
5476 * Returns an allocated array of fields. The last item in the array is NULL.
5477 * The array must be freed with free().
5479 struct format_field **pevent_event_fields(struct event_format *event)
5481 return get_event_fields("event", event->name,
5482 event->format.nr_fields,
5483 event->format.fields);
5486 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5488 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5490 trace_seq_puts(s, ", ");
5491 print_fields(s, field->next);
5496 static void print_args(struct print_arg *args)
5498 int print_paren = 1;
5501 switch (args->type) {
5506 printf("%s", args->atom.atom);
5509 printf("REC->%s", args->field.name);
5512 printf("__print_flags(");
5513 print_args(args->flags.field);
5514 printf(", %s, ", args->flags.delim);
5516 print_fields(&s, args->flags.flags);
5517 trace_seq_do_printf(&s);
5518 trace_seq_destroy(&s);
5522 printf("__print_symbolic(");
5523 print_args(args->symbol.field);
5526 print_fields(&s, args->symbol.symbols);
5527 trace_seq_do_printf(&s);
5528 trace_seq_destroy(&s);
5532 printf("__print_hex(");
5533 print_args(args->hex.field);
5535 print_args(args->hex.size);
5538 case PRINT_INT_ARRAY:
5539 printf("__print_array(");
5540 print_args(args->int_array.field);
5542 print_args(args->int_array.count);
5544 print_args(args->int_array.el_size);
5549 printf("__get_str(%s)", args->string.string);
5552 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5555 printf("(%s)", args->typecast.type);
5556 print_args(args->typecast.item);
5559 if (strcmp(args->op.op, ":") == 0)
5563 print_args(args->op.left);
5564 printf(" %s ", args->op.op);
5565 print_args(args->op.right);
5570 /* we should warn... */
5575 print_args(args->next);
5579 static void parse_header_field(const char *field,
5580 int *offset, int *size, int mandatory)
5582 unsigned long long save_input_buf_ptr;
5583 unsigned long long save_input_buf_siz;
5587 save_input_buf_ptr = input_buf_ptr;
5588 save_input_buf_siz = input_buf_siz;
5590 if (read_expected(EVENT_ITEM, "field") < 0)
5592 if (read_expected(EVENT_OP, ":") < 0)
5596 if (read_expect_type(EVENT_ITEM, &token) < 0)
5601 * If this is not a mandatory field, then test it first.
5604 if (read_expected(EVENT_ITEM, field) < 0)
5607 if (read_expect_type(EVENT_ITEM, &token) < 0)
5609 if (strcmp(token, field) != 0)
5614 if (read_expected(EVENT_OP, ";") < 0)
5616 if (read_expected(EVENT_ITEM, "offset") < 0)
5618 if (read_expected(EVENT_OP, ":") < 0)
5620 if (read_expect_type(EVENT_ITEM, &token) < 0)
5622 *offset = atoi(token);
5624 if (read_expected(EVENT_OP, ";") < 0)
5626 if (read_expected(EVENT_ITEM, "size") < 0)
5628 if (read_expected(EVENT_OP, ":") < 0)
5630 if (read_expect_type(EVENT_ITEM, &token) < 0)
5632 *size = atoi(token);
5634 if (read_expected(EVENT_OP, ";") < 0)
5636 type = read_token(&token);
5637 if (type != EVENT_NEWLINE) {
5638 /* newer versions of the kernel have a "signed" type */
5639 if (type != EVENT_ITEM)
5642 if (strcmp(token, "signed") != 0)
5647 if (read_expected(EVENT_OP, ":") < 0)
5650 if (read_expect_type(EVENT_ITEM, &token))
5654 if (read_expected(EVENT_OP, ";") < 0)
5657 if (read_expect_type(EVENT_NEWLINE, &token))
5665 input_buf_ptr = save_input_buf_ptr;
5666 input_buf_siz = save_input_buf_siz;
5673 * pevent_parse_header_page - parse the data stored in the header page
5674 * @pevent: the handle to the pevent
5675 * @buf: the buffer storing the header page format string
5676 * @size: the size of @buf
5677 * @long_size: the long size to use if there is no header
5679 * This parses the header page format for information on the
5680 * ring buffer used. The @buf should be copied from
5682 * /sys/kernel/debug/tracing/events/header_page
5684 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5691 * Old kernels did not have header page info.
5692 * Sorry but we just use what we find here in user space.
5694 pevent->header_page_ts_size = sizeof(long long);
5695 pevent->header_page_size_size = long_size;
5696 pevent->header_page_data_offset = sizeof(long long) + long_size;
5697 pevent->old_format = 1;
5700 init_input_buf(buf, size);
5702 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5703 &pevent->header_page_ts_size, 1);
5704 parse_header_field("commit", &pevent->header_page_size_offset,
5705 &pevent->header_page_size_size, 1);
5706 parse_header_field("overwrite", &pevent->header_page_overwrite,
5708 parse_header_field("data", &pevent->header_page_data_offset,
5709 &pevent->header_page_data_size, 1);
5714 static int event_matches(struct event_format *event,
5715 int id, const char *sys_name,
5716 const char *event_name)
5718 if (id >= 0 && id != event->id)
5721 if (event_name && (strcmp(event_name, event->name) != 0))
5724 if (sys_name && (strcmp(sys_name, event->system) != 0))
5730 static void free_handler(struct event_handler *handle)
5732 free((void *)handle->sys_name);
5733 free((void *)handle->event_name);
5737 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5739 struct event_handler *handle, **next;
5741 for (next = &pevent->handlers; *next;
5742 next = &(*next)->next) {
5744 if (event_matches(event, handle->id,
5746 handle->event_name))
5753 pr_stat("overriding event (%d) %s:%s with new print handler",
5754 event->id, event->system, event->name);
5756 event->handler = handle->func;
5757 event->context = handle->context;
5759 *next = handle->next;
5760 free_handler(handle);
5766 * __pevent_parse_format - parse the event format
5767 * @buf: the buffer storing the event format string
5768 * @size: the size of @buf
5769 * @sys: the system the event belongs to
5771 * This parses the event format and creates an event structure
5772 * to quickly parse raw data for a given event.
5774 * These files currently come from:
5776 * /sys/kernel/debug/tracing/events/.../.../format
5778 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5779 struct pevent *pevent, const char *buf,
5780 unsigned long size, const char *sys)
5782 struct event_format *event;
5785 init_input_buf(buf, size);
5787 *eventp = event = alloc_event();
5789 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5791 event->name = event_read_name();
5794 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5795 goto event_alloc_failed;
5798 if (strcmp(sys, "ftrace") == 0) {
5799 event->flags |= EVENT_FL_ISFTRACE;
5801 if (strcmp(event->name, "bprint") == 0)
5802 event->flags |= EVENT_FL_ISBPRINT;
5805 event->id = event_read_id();
5806 if (event->id < 0) {
5807 ret = PEVENT_ERRNO__READ_ID_FAILED;
5809 * This isn't an allocation error actually.
5810 * But as the ID is critical, just bail out.
5812 goto event_alloc_failed;
5815 event->system = strdup(sys);
5816 if (!event->system) {
5817 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5818 goto event_alloc_failed;
5821 /* Add pevent to event so that it can be referenced */
5822 event->pevent = pevent;
5824 ret = event_read_format(event);
5826 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5827 goto event_parse_failed;
5831 * If the event has an override, don't print warnings if the event
5832 * print format fails to parse.
5834 if (pevent && find_event_handle(pevent, event))
5837 ret = event_read_print(event);
5841 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5842 goto event_parse_failed;
5845 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5846 struct format_field *field;
5847 struct print_arg *arg, **list;
5849 /* old ftrace had no args */
5850 list = &event->print_fmt.args;
5851 for (field = event->format.fields; field; field = field->next) {
5854 event->flags |= EVENT_FL_FAILED;
5855 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5857 arg->type = PRINT_FIELD;
5858 arg->field.name = strdup(field->name);
5859 if (!arg->field.name) {
5860 event->flags |= EVENT_FL_FAILED;
5862 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5864 arg->field.field = field;
5874 event->flags |= EVENT_FL_FAILED;
5878 free(event->system);
5885 static enum pevent_errno
5886 __pevent_parse_event(struct pevent *pevent,
5887 struct event_format **eventp,
5888 const char *buf, unsigned long size,
5891 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5892 struct event_format *event = *eventp;
5897 if (pevent && add_event(pevent, event)) {
5898 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5899 goto event_add_failed;
5902 #define PRINT_ARGS 0
5903 if (PRINT_ARGS && event->print_fmt.args)
5904 print_args(event->print_fmt.args);
5909 pevent_free_format(event);
5914 * pevent_parse_format - parse the event format
5915 * @pevent: the handle to the pevent
5916 * @eventp: returned format
5917 * @buf: the buffer storing the event format string
5918 * @size: the size of @buf
5919 * @sys: the system the event belongs to
5921 * This parses the event format and creates an event structure
5922 * to quickly parse raw data for a given event.
5924 * These files currently come from:
5926 * /sys/kernel/debug/tracing/events/.../.../format
5928 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5929 struct event_format **eventp,
5931 unsigned long size, const char *sys)
5933 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5937 * pevent_parse_event - parse the event format
5938 * @pevent: the handle to the pevent
5939 * @buf: the buffer storing the event format string
5940 * @size: the size of @buf
5941 * @sys: the system the event belongs to
5943 * This parses the event format and creates an event structure
5944 * to quickly parse raw data for a given event.
5946 * These files currently come from:
5948 * /sys/kernel/debug/tracing/events/.../.../format
5950 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5951 unsigned long size, const char *sys)
5953 struct event_format *event = NULL;
5954 return __pevent_parse_event(pevent, &event, buf, size, sys);
5958 #define _PE(code, str) str
5959 static const char * const pevent_error_str[] = {
5964 int pevent_strerror(struct pevent *pevent __maybe_unused,
5965 enum pevent_errno errnum, char *buf, size_t buflen)
5971 msg = strerror_r(errnum, buf, buflen);
5973 size_t len = strlen(msg);
5974 memcpy(buf, msg, min(buflen - 1, len));
5975 *(buf + min(buflen - 1, len)) = '\0';
5980 if (errnum <= __PEVENT_ERRNO__START ||
5981 errnum >= __PEVENT_ERRNO__END)
5984 idx = errnum - __PEVENT_ERRNO__START - 1;
5985 msg = pevent_error_str[idx];
5986 snprintf(buf, buflen, "%s", msg);
5991 int get_field_val(struct trace_seq *s, struct format_field *field,
5992 const char *name, struct pevent_record *record,
5993 unsigned long long *val, int err)
5997 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6001 if (pevent_read_number_field(field, record->data, val)) {
6003 trace_seq_printf(s, " %s=INVALID", name);
6011 * pevent_get_field_raw - return the raw pointer into the data field
6012 * @s: The seq to print to on error
6013 * @event: the event that the field is for
6014 * @name: The name of the field
6015 * @record: The record with the field name.
6016 * @len: place to store the field length.
6017 * @err: print default error if failed.
6019 * Returns a pointer into record->data of the field and places
6020 * the length of the field in @len.
6022 * On failure, it returns NULL.
6024 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6025 const char *name, struct pevent_record *record,
6028 struct format_field *field;
6029 void *data = record->data;
6036 field = pevent_find_field(event, name);
6040 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6044 /* Allow @len to be NULL */
6048 offset = field->offset;
6049 if (field->flags & FIELD_IS_DYNAMIC) {
6050 offset = pevent_read_number(event->pevent,
6051 data + offset, field->size);
6052 *len = offset >> 16;
6057 return data + offset;
6061 * pevent_get_field_val - find a field and return its value
6062 * @s: The seq to print to on error
6063 * @event: the event that the field is for
6064 * @name: The name of the field
6065 * @record: The record with the field name.
6066 * @val: place to store the value of the field.
6067 * @err: print default error if failed.
6069 * Returns 0 on success -1 on field not found.
6071 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6072 const char *name, struct pevent_record *record,
6073 unsigned long long *val, int err)
6075 struct format_field *field;
6080 field = pevent_find_field(event, name);
6082 return get_field_val(s, field, name, record, val, err);
6086 * pevent_get_common_field_val - find a common field and return its value
6087 * @s: The seq to print to on error
6088 * @event: the event that the field is for
6089 * @name: The name of the field
6090 * @record: The record with the field name.
6091 * @val: place to store the value of the field.
6092 * @err: print default error if failed.
6094 * Returns 0 on success -1 on field not found.
6096 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6097 const char *name, struct pevent_record *record,
6098 unsigned long long *val, int err)
6100 struct format_field *field;
6105 field = pevent_find_common_field(event, name);
6107 return get_field_val(s, field, name, record, val, err);
6111 * pevent_get_any_field_val - find a any field and return its value
6112 * @s: The seq to print to on error
6113 * @event: the event that the field is for
6114 * @name: The name of the field
6115 * @record: The record with the field name.
6116 * @val: place to store the value of the field.
6117 * @err: print default error if failed.
6119 * Returns 0 on success -1 on field not found.
6121 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6122 const char *name, struct pevent_record *record,
6123 unsigned long long *val, int err)
6125 struct format_field *field;
6130 field = pevent_find_any_field(event, name);
6132 return get_field_val(s, field, name, record, val, err);
6136 * pevent_print_num_field - print a field and a format
6137 * @s: The seq to print to
6138 * @fmt: The printf format to print the field with.
6139 * @event: the event that the field is for
6140 * @name: The name of the field
6141 * @record: The record with the field name.
6142 * @err: print default error if failed.
6144 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6146 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6147 struct event_format *event, const char *name,
6148 struct pevent_record *record, int err)
6150 struct format_field *field = pevent_find_field(event, name);
6151 unsigned long long val;
6156 if (pevent_read_number_field(field, record->data, &val))
6159 return trace_seq_printf(s, fmt, val);
6163 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6168 * pevent_print_func_field - print a field and a format for function pointers
6169 * @s: The seq to print to
6170 * @fmt: The printf format to print the field with.
6171 * @event: the event that the field is for
6172 * @name: The name of the field
6173 * @record: The record with the field name.
6174 * @err: print default error if failed.
6176 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6178 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6179 struct event_format *event, const char *name,
6180 struct pevent_record *record, int err)
6182 struct format_field *field = pevent_find_field(event, name);
6183 struct pevent *pevent = event->pevent;
6184 unsigned long long val;
6185 struct func_map *func;
6191 if (pevent_read_number_field(field, record->data, &val))
6194 func = find_func(pevent, val);
6197 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6199 sprintf(tmp, "0x%08llx", val);
6201 return trace_seq_printf(s, fmt, tmp);
6205 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6209 static void free_func_handle(struct pevent_function_handler *func)
6211 struct pevent_func_params *params;
6215 while (func->params) {
6216 params = func->params;
6217 func->params = params->next;
6225 * pevent_register_print_function - register a helper function
6226 * @pevent: the handle to the pevent
6227 * @func: the function to process the helper function
6228 * @ret_type: the return type of the helper function
6229 * @name: the name of the helper function
6230 * @parameters: A list of enum pevent_func_arg_type
6232 * Some events may have helper functions in the print format arguments.
6233 * This allows a plugin to dynamically create a way to process one
6234 * of these functions.
6236 * The @parameters is a variable list of pevent_func_arg_type enums that
6237 * must end with PEVENT_FUNC_ARG_VOID.
6239 int pevent_register_print_function(struct pevent *pevent,
6240 pevent_func_handler func,
6241 enum pevent_func_arg_type ret_type,
6244 struct pevent_function_handler *func_handle;
6245 struct pevent_func_params **next_param;
6246 struct pevent_func_params *param;
6247 enum pevent_func_arg_type type;
6251 func_handle = find_func_handler(pevent, name);
6254 * This is most like caused by the users own
6255 * plugins updating the function. This overrides the
6258 pr_stat("override of function helper '%s'", name);
6259 remove_func_handler(pevent, name);
6262 func_handle = calloc(1, sizeof(*func_handle));
6264 do_warning("Failed to allocate function handler");
6265 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6268 func_handle->ret_type = ret_type;
6269 func_handle->name = strdup(name);
6270 func_handle->func = func;
6271 if (!func_handle->name) {
6272 do_warning("Failed to allocate function name");
6274 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6277 next_param = &(func_handle->params);
6280 type = va_arg(ap, enum pevent_func_arg_type);
6281 if (type == PEVENT_FUNC_ARG_VOID)
6284 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6285 do_warning("Invalid argument type %d", type);
6286 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6290 param = malloc(sizeof(*param));
6292 do_warning("Failed to allocate function param");
6293 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6299 *next_param = param;
6300 next_param = &(param->next);
6302 func_handle->nr_args++;
6306 func_handle->next = pevent->func_handlers;
6307 pevent->func_handlers = func_handle;
6312 free_func_handle(func_handle);
6317 * pevent_unregister_print_function - unregister a helper function
6318 * @pevent: the handle to the pevent
6319 * @func: the function to process the helper function
6320 * @name: the name of the helper function
6322 * This function removes existing print handler for function @name.
6324 * Returns 0 if the handler was removed successully, -1 otherwise.
6326 int pevent_unregister_print_function(struct pevent *pevent,
6327 pevent_func_handler func, char *name)
6329 struct pevent_function_handler *func_handle;
6331 func_handle = find_func_handler(pevent, name);
6332 if (func_handle && func_handle->func == func) {
6333 remove_func_handler(pevent, name);
6339 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6340 const char *sys_name,
6341 const char *event_name)
6343 struct event_format *event;
6347 event = pevent_find_event(pevent, id);
6350 if (event_name && (strcmp(event_name, event->name) != 0))
6352 if (sys_name && (strcmp(sys_name, event->system) != 0))
6355 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6363 * pevent_register_event_handler - register a way to parse an event
6364 * @pevent: the handle to the pevent
6365 * @id: the id of the event to register
6366 * @sys_name: the system name the event belongs to
6367 * @event_name: the name of the event
6368 * @func: the function to call to parse the event information
6369 * @context: the data to be passed to @func
6371 * This function allows a developer to override the parsing of
6372 * a given event. If for some reason the default print format
6373 * is not sufficient, this function will register a function
6374 * for an event to be used to parse the data instead.
6376 * If @id is >= 0, then it is used to find the event.
6377 * else @sys_name and @event_name are used.
6379 int pevent_register_event_handler(struct pevent *pevent, int id,
6380 const char *sys_name, const char *event_name,
6381 pevent_event_handler_func func, void *context)
6383 struct event_format *event;
6384 struct event_handler *handle;
6386 event = pevent_search_event(pevent, id, sys_name, event_name);
6390 pr_stat("overriding event (%d) %s:%s with new print handler",
6391 event->id, event->system, event->name);
6393 event->handler = func;
6394 event->context = context;
6398 /* Save for later use. */
6399 handle = calloc(1, sizeof(*handle));
6401 do_warning("Failed to allocate event handler");
6402 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6407 handle->event_name = strdup(event_name);
6409 handle->sys_name = strdup(sys_name);
6411 if ((event_name && !handle->event_name) ||
6412 (sys_name && !handle->sys_name)) {
6413 do_warning("Failed to allocate event/sys name");
6414 free((void *)handle->event_name);
6415 free((void *)handle->sys_name);
6417 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6420 handle->func = func;
6421 handle->next = pevent->handlers;
6422 pevent->handlers = handle;
6423 handle->context = context;
6428 static int handle_matches(struct event_handler *handler, int id,
6429 const char *sys_name, const char *event_name,
6430 pevent_event_handler_func func, void *context)
6432 if (id >= 0 && id != handler->id)
6435 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6438 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6441 if (func != handler->func || context != handler->context)
6448 * pevent_unregister_event_handler - unregister an existing event handler
6449 * @pevent: the handle to the pevent
6450 * @id: the id of the event to unregister
6451 * @sys_name: the system name the handler belongs to
6452 * @event_name: the name of the event handler
6453 * @func: the function to call to parse the event information
6454 * @context: the data to be passed to @func
6456 * This function removes existing event handler (parser).
6458 * If @id is >= 0, then it is used to find the event.
6459 * else @sys_name and @event_name are used.
6461 * Returns 0 if handler was removed successfully, -1 if event was not found.
6463 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6464 const char *sys_name, const char *event_name,
6465 pevent_event_handler_func func, void *context)
6467 struct event_format *event;
6468 struct event_handler *handle;
6469 struct event_handler **next;
6471 event = pevent_search_event(pevent, id, sys_name, event_name);
6475 if (event->handler == func && event->context == context) {
6476 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6477 event->id, event->system, event->name);
6479 event->handler = NULL;
6480 event->context = NULL;
6485 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6487 if (handle_matches(handle, id, sys_name, event_name,
6495 *next = handle->next;
6496 free_handler(handle);
6502 * pevent_alloc - create a pevent handle
6504 struct pevent *pevent_alloc(void)
6506 struct pevent *pevent = calloc(1, sizeof(*pevent));
6509 pevent->ref_count = 1;
6514 void pevent_ref(struct pevent *pevent)
6516 pevent->ref_count++;
6519 void pevent_free_format_field(struct format_field *field)
6522 if (field->alias != field->name)
6528 static void free_format_fields(struct format_field *field)
6530 struct format_field *next;
6534 pevent_free_format_field(field);
6539 static void free_formats(struct format *format)
6541 free_format_fields(format->common_fields);
6542 free_format_fields(format->fields);
6545 void pevent_free_format(struct event_format *event)
6548 free(event->system);
6550 free_formats(&event->format);
6552 free(event->print_fmt.format);
6553 free_args(event->print_fmt.args);
6559 * pevent_free - free a pevent handle
6560 * @pevent: the pevent handle to free
6562 void pevent_free(struct pevent *pevent)
6564 struct cmdline_list *cmdlist, *cmdnext;
6565 struct func_list *funclist, *funcnext;
6566 struct printk_list *printklist, *printknext;
6567 struct pevent_function_handler *func_handler;
6568 struct event_handler *handle;
6574 cmdlist = pevent->cmdlist;
6575 funclist = pevent->funclist;
6576 printklist = pevent->printklist;
6578 pevent->ref_count--;
6579 if (pevent->ref_count)
6582 if (pevent->cmdlines) {
6583 for (i = 0; i < pevent->cmdline_count; i++)
6584 free(pevent->cmdlines[i].comm);
6585 free(pevent->cmdlines);
6589 cmdnext = cmdlist->next;
6590 free(cmdlist->comm);
6595 if (pevent->func_map) {
6596 for (i = 0; i < (int)pevent->func_count; i++) {
6597 free(pevent->func_map[i].func);
6598 free(pevent->func_map[i].mod);
6600 free(pevent->func_map);
6604 funcnext = funclist->next;
6605 free(funclist->func);
6606 free(funclist->mod);
6608 funclist = funcnext;
6611 while (pevent->func_handlers) {
6612 func_handler = pevent->func_handlers;
6613 pevent->func_handlers = func_handler->next;
6614 free_func_handle(func_handler);
6617 if (pevent->printk_map) {
6618 for (i = 0; i < (int)pevent->printk_count; i++)
6619 free(pevent->printk_map[i].printk);
6620 free(pevent->printk_map);
6623 while (printklist) {
6624 printknext = printklist->next;
6625 free(printklist->printk);
6627 printklist = printknext;
6630 for (i = 0; i < pevent->nr_events; i++)
6631 pevent_free_format(pevent->events[i]);
6633 while (pevent->handlers) {
6634 handle = pevent->handlers;
6635 pevent->handlers = handle->next;
6636 free_handler(handle);
6639 free(pevent->trace_clock);
6640 free(pevent->events);
6641 free(pevent->sort_events);
6642 free(pevent->func_resolver);
6647 void pevent_unref(struct pevent *pevent)
6649 pevent_free(pevent);