2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include <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;
3798 unsigned long long addr;
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) {
3832 /* Handle heterogeneous recording and processing
3836 * Traces recorded on 32-bit devices (32-bit
3837 * addressing) and processed on 64-bit devices:
3838 * In this case, only 32 bits should be read.
3841 * Traces recorded on 64 bit devices and processed
3842 * on 32-bit devices:
3843 * In this case, 64 bits must be read.
3845 addr = (pevent->long_size == 8) ?
3846 *(unsigned long long *)(data + field->offset) :
3847 (unsigned long long)*(unsigned int *)(data + field->offset);
3849 /* Check if it matches a print format */
3850 printk = find_printk(pevent, addr);
3852 trace_seq_puts(s, printk->printk);
3854 trace_seq_printf(s, "%llx", addr);
3857 str = malloc(len + 1);
3859 do_warning_event(event, "%s: not enough memory!",
3863 memcpy(str, data + field->offset, len);
3865 print_str_to_seq(s, format, len_arg, str);
3869 val = eval_num_arg(data, size, event, arg->flags.field);
3871 for (flag = arg->flags.flags; flag; flag = flag->next) {
3872 fval = eval_flag(flag->value);
3873 if (!val && fval < 0) {
3874 print_str_to_seq(s, format, len_arg, flag->str);
3877 if (fval > 0 && (val & fval) == fval) {
3878 if (print && arg->flags.delim)
3879 trace_seq_puts(s, arg->flags.delim);
3880 print_str_to_seq(s, format, len_arg, flag->str);
3887 val = eval_num_arg(data, size, event, arg->symbol.field);
3888 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3889 fval = eval_flag(flag->value);
3891 print_str_to_seq(s, format, len_arg, flag->str);
3897 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3898 unsigned long offset;
3899 offset = pevent_read_number(pevent,
3900 data + arg->hex.field->dynarray.field->offset,
3901 arg->hex.field->dynarray.field->size);
3902 hex = data + (offset & 0xffff);
3904 field = arg->hex.field->field.field;
3906 str = arg->hex.field->field.name;
3907 field = pevent_find_any_field(event, str);
3909 goto out_warning_field;
3910 arg->hex.field->field.field = field;
3912 hex = data + field->offset;
3914 len = eval_num_arg(data, size, event, arg->hex.size);
3915 for (i = 0; i < len; i++) {
3917 trace_seq_putc(s, ' ');
3918 trace_seq_printf(s, "%02x", hex[i]);
3922 case PRINT_INT_ARRAY: {
3926 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3927 unsigned long offset;
3928 struct format_field *field =
3929 arg->int_array.field->dynarray.field;
3930 offset = pevent_read_number(pevent,
3931 data + field->offset,
3933 num = data + (offset & 0xffff);
3935 field = arg->int_array.field->field.field;
3937 str = arg->int_array.field->field.name;
3938 field = pevent_find_any_field(event, str);
3940 goto out_warning_field;
3941 arg->int_array.field->field.field = field;
3943 num = data + field->offset;
3945 len = eval_num_arg(data, size, event, arg->int_array.count);
3946 el_size = eval_num_arg(data, size, event,
3947 arg->int_array.el_size);
3948 for (i = 0; i < len; i++) {
3950 trace_seq_putc(s, ' ');
3953 trace_seq_printf(s, "%u", *(uint8_t *)num);
3954 } else if (el_size == 2) {
3955 trace_seq_printf(s, "%u", *(uint16_t *)num);
3956 } else if (el_size == 4) {
3957 trace_seq_printf(s, "%u", *(uint32_t *)num);
3958 } else if (el_size == 8) {
3959 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
3961 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3962 el_size, *(uint8_t *)num);
3972 case PRINT_STRING: {
3975 if (arg->string.offset == -1) {
3976 struct format_field *f;
3978 f = pevent_find_any_field(event, arg->string.string);
3979 arg->string.offset = f->offset;
3981 str_offset = data2host4(pevent, data + arg->string.offset);
3982 str_offset &= 0xffff;
3983 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3987 print_str_to_seq(s, format, len_arg, arg->string.string);
3989 case PRINT_BITMASK: {
3993 if (arg->bitmask.offset == -1) {
3994 struct format_field *f;
3996 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3997 arg->bitmask.offset = f->offset;
3999 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4000 bitmask_size = bitmask_offset >> 16;
4001 bitmask_offset &= 0xffff;
4002 print_bitmask_to_seq(pevent, s, format, len_arg,
4003 data + bitmask_offset, bitmask_size);
4008 * The only op for string should be ? :
4010 if (arg->op.op[0] != '?')
4012 val = eval_num_arg(data, size, event, arg->op.left);
4014 print_str_arg(s, data, size, event,
4015 format, len_arg, arg->op.right->op.left);
4017 print_str_arg(s, data, size, event,
4018 format, len_arg, arg->op.right->op.right);
4021 process_defined_func(s, data, size, event, arg);
4031 do_warning_event(event, "%s: field %s not found",
4032 __func__, arg->field.name);
4035 static unsigned long long
4036 process_defined_func(struct trace_seq *s, void *data, int size,
4037 struct event_format *event, struct print_arg *arg)
4039 struct pevent_function_handler *func_handle = arg->func.func;
4040 struct pevent_func_params *param;
4041 unsigned long long *args;
4042 unsigned long long ret;
4043 struct print_arg *farg;
4044 struct trace_seq str;
4046 struct save_str *next;
4048 } *strings = NULL, *string;
4051 if (!func_handle->nr_args) {
4052 ret = (*func_handle->func)(s, NULL);
4056 farg = arg->func.args;
4057 param = func_handle->params;
4060 args = malloc(sizeof(*args) * func_handle->nr_args);
4064 for (i = 0; i < func_handle->nr_args; i++) {
4065 switch (param->type) {
4066 case PEVENT_FUNC_ARG_INT:
4067 case PEVENT_FUNC_ARG_LONG:
4068 case PEVENT_FUNC_ARG_PTR:
4069 args[i] = eval_num_arg(data, size, event, farg);
4071 case PEVENT_FUNC_ARG_STRING:
4072 trace_seq_init(&str);
4073 print_str_arg(&str, data, size, event, "%s", -1, farg);
4074 trace_seq_terminate(&str);
4075 string = malloc(sizeof(*string));
4077 do_warning_event(event, "%s(%d): malloc str",
4078 __func__, __LINE__);
4081 string->next = strings;
4082 string->str = strdup(str.buffer);
4085 do_warning_event(event, "%s(%d): malloc str",
4086 __func__, __LINE__);
4089 args[i] = (uintptr_t)string->str;
4091 trace_seq_destroy(&str);
4095 * Something went totally wrong, this is not
4096 * an input error, something in this code broke.
4098 do_warning_event(event, "Unexpected end of arguments\n");
4102 param = param->next;
4105 ret = (*func_handle->func)(s, args);
4110 strings = string->next;
4116 /* TBD : handle return type here */
4120 static void free_args(struct print_arg *args)
4122 struct print_arg *next;
4132 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4134 struct pevent *pevent = event->pevent;
4135 struct format_field *field, *ip_field;
4136 struct print_arg *args, *arg, **next;
4137 unsigned long long ip, val;
4142 field = pevent->bprint_buf_field;
4143 ip_field = pevent->bprint_ip_field;
4146 field = pevent_find_field(event, "buf");
4148 do_warning_event(event, "can't find buffer field for binary printk");
4151 ip_field = pevent_find_field(event, "ip");
4153 do_warning_event(event, "can't find ip field for binary printk");
4156 pevent->bprint_buf_field = field;
4157 pevent->bprint_ip_field = ip_field;
4160 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4163 * The first arg is the IP pointer.
4167 do_warning_event(event, "%s(%d): not enough memory!",
4168 __func__, __LINE__);
4175 arg->type = PRINT_ATOM;
4177 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4180 /* skip the first "%ps: " */
4181 for (ptr = fmt + 5, bptr = data + field->offset;
4182 bptr < data + size && *ptr; ptr++) {
4217 vsize = pevent->long_size;
4231 /* the pointers are always 4 bytes aligned */
4232 bptr = (void *)(((unsigned long)bptr + 3) &
4234 val = pevent_read_number(pevent, bptr, vsize);
4238 do_warning_event(event, "%s(%d): not enough memory!",
4239 __func__, __LINE__);
4243 arg->type = PRINT_ATOM;
4244 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4251 * The '*' case means that an arg is used as the length.
4252 * We need to continue to figure out for what.
4261 do_warning_event(event, "%s(%d): not enough memory!",
4262 __func__, __LINE__);
4266 arg->type = PRINT_BSTRING;
4267 arg->string.string = strdup(bptr);
4268 if (!arg->string.string)
4270 bptr += strlen(bptr) + 1;
4287 get_bprint_format(void *data, int size __maybe_unused,
4288 struct event_format *event)
4290 struct pevent *pevent = event->pevent;
4291 unsigned long long addr;
4292 struct format_field *field;
4293 struct printk_map *printk;
4296 field = pevent->bprint_fmt_field;
4299 field = pevent_find_field(event, "fmt");
4301 do_warning_event(event, "can't find format field for binary printk");
4304 pevent->bprint_fmt_field = field;
4307 addr = pevent_read_number(pevent, data + field->offset, field->size);
4309 printk = find_printk(pevent, addr);
4311 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4316 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4322 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4323 struct event_format *event, struct print_arg *arg)
4326 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4328 if (arg->type == PRINT_FUNC) {
4329 process_defined_func(s, data, size, event, arg);
4333 if (arg->type != PRINT_FIELD) {
4334 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4340 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4341 if (!arg->field.field) {
4343 pevent_find_any_field(event, arg->field.name);
4344 if (!arg->field.field) {
4345 do_warning_event(event, "%s: field %s not found",
4346 __func__, arg->field.name);
4350 if (arg->field.field->size != 6) {
4351 trace_seq_printf(s, "INVALIDMAC");
4354 buf = data + arg->field.field->offset;
4355 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4358 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4363 fmt = "%03d.%03d.%03d.%03d";
4365 fmt = "%d.%d.%d.%d";
4367 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4370 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4372 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4373 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4376 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4378 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4381 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4384 unsigned char zerolength[8];
4389 bool needcolon = false;
4391 struct in6_addr in6;
4393 memcpy(&in6, addr, sizeof(struct in6_addr));
4395 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4397 memset(zerolength, 0, sizeof(zerolength));
4404 /* find position of longest 0 run */
4405 for (i = 0; i < range; i++) {
4406 for (j = i; j < range; j++) {
4407 if (in6.s6_addr16[j] != 0)
4412 for (i = 0; i < range; i++) {
4413 if (zerolength[i] > longest) {
4414 longest = zerolength[i];
4418 if (longest == 1) /* don't compress a single 0 */
4422 for (i = 0; i < range; i++) {
4423 if (i == colonpos) {
4424 if (needcolon || i == 0)
4425 trace_seq_printf(s, ":");
4426 trace_seq_printf(s, ":");
4432 trace_seq_printf(s, ":");
4435 /* hex u16 without leading 0s */
4436 word = ntohs(in6.s6_addr16[i]);
4440 trace_seq_printf(s, "%x%02x", hi, lo);
4442 trace_seq_printf(s, "%x", lo);
4449 trace_seq_printf(s, ":");
4450 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4456 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4460 for (j = 0; j < 16; j += 2) {
4461 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4462 if (i == 'I' && j < 14)
4463 trace_seq_printf(s, ":");
4468 * %pi4 print an IPv4 address with leading zeros
4469 * %pI4 print an IPv4 address without leading zeros
4470 * %pi6 print an IPv6 address without colons
4471 * %pI6 print an IPv6 address with colons
4472 * %pI6c print an IPv6 address in compressed form with colons
4473 * %pISpc print an IP address based on sockaddr; p adds port.
4475 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4476 void *data, int size, struct event_format *event,
4477 struct print_arg *arg)
4481 if (arg->type == PRINT_FUNC) {
4482 process_defined_func(s, data, size, event, arg);
4486 if (arg->type != PRINT_FIELD) {
4487 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4491 if (!arg->field.field) {
4493 pevent_find_any_field(event, arg->field.name);
4494 if (!arg->field.field) {
4495 do_warning("%s: field %s not found",
4496 __func__, arg->field.name);
4501 buf = data + arg->field.field->offset;
4503 if (arg->field.field->size != 4) {
4504 trace_seq_printf(s, "INVALIDIPv4");
4507 print_ip4_addr(s, i, buf);
4512 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4513 void *data, int size, struct event_format *event,
4514 struct print_arg *arg)
4521 if (i == 'I' && *ptr == 'c') {
4527 if (arg->type == PRINT_FUNC) {
4528 process_defined_func(s, data, size, event, arg);
4532 if (arg->type != PRINT_FIELD) {
4533 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4537 if (!arg->field.field) {
4539 pevent_find_any_field(event, arg->field.name);
4540 if (!arg->field.field) {
4541 do_warning("%s: field %s not found",
4542 __func__, arg->field.name);
4547 buf = data + arg->field.field->offset;
4549 if (arg->field.field->size != 16) {
4550 trace_seq_printf(s, "INVALIDIPv6");
4555 print_ip6c_addr(s, buf);
4557 print_ip6_addr(s, i, buf);
4562 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4563 void *data, int size, struct event_format *event,
4564 struct print_arg *arg)
4566 char have_c = 0, have_p = 0;
4568 struct sockaddr_storage *sa;
4585 if (arg->type == PRINT_FUNC) {
4586 process_defined_func(s, data, size, event, arg);
4590 if (arg->type != PRINT_FIELD) {
4591 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4595 if (!arg->field.field) {
4597 pevent_find_any_field(event, arg->field.name);
4598 if (!arg->field.field) {
4599 do_warning("%s: field %s not found",
4600 __func__, arg->field.name);
4605 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4607 if (sa->ss_family == AF_INET) {
4608 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4610 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4611 trace_seq_printf(s, "INVALIDIPv4");
4615 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4617 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4620 } else if (sa->ss_family == AF_INET6) {
4621 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4623 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4624 trace_seq_printf(s, "INVALIDIPv6");
4629 trace_seq_printf(s, "[");
4631 buf = (unsigned char *) &sa6->sin6_addr;
4633 print_ip6c_addr(s, buf);
4635 print_ip6_addr(s, i, buf);
4638 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4644 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4645 void *data, int size, struct event_format *event,
4646 struct print_arg *arg)
4648 char i = *ptr; /* 'i' or 'I' */
4661 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4664 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4667 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4676 static int is_printable_array(char *p, unsigned int len)
4680 for (i = 0; i < len && p[i]; i++)
4681 if (!isprint(p[i]) && !isspace(p[i]))
4686 static void print_event_fields(struct trace_seq *s, void *data,
4687 int size __maybe_unused,
4688 struct event_format *event)
4690 struct format_field *field;
4691 unsigned long long val;
4692 unsigned int offset, len, i;
4694 field = event->format.fields;
4696 trace_seq_printf(s, " %s=", field->name);
4697 if (field->flags & FIELD_IS_ARRAY) {
4698 offset = field->offset;
4700 if (field->flags & FIELD_IS_DYNAMIC) {
4701 val = pevent_read_number(event->pevent, data + offset, len);
4706 if (field->flags & FIELD_IS_STRING &&
4707 is_printable_array(data + offset, len)) {
4708 trace_seq_printf(s, "%s", (char *)data + offset);
4710 trace_seq_puts(s, "ARRAY[");
4711 for (i = 0; i < len; i++) {
4713 trace_seq_puts(s, ", ");
4714 trace_seq_printf(s, "%02x",
4715 *((unsigned char *)data + offset + i));
4717 trace_seq_putc(s, ']');
4718 field->flags &= ~FIELD_IS_STRING;
4721 val = pevent_read_number(event->pevent, data + field->offset,
4723 if (field->flags & FIELD_IS_POINTER) {
4724 trace_seq_printf(s, "0x%llx", val);
4725 } else if (field->flags & FIELD_IS_SIGNED) {
4726 switch (field->size) {
4729 * If field is long then print it in hex.
4730 * A long usually stores pointers.
4732 if (field->flags & FIELD_IS_LONG)
4733 trace_seq_printf(s, "0x%x", (int)val);
4735 trace_seq_printf(s, "%d", (int)val);
4738 trace_seq_printf(s, "%2d", (short)val);
4741 trace_seq_printf(s, "%1d", (char)val);
4744 trace_seq_printf(s, "%lld", val);
4747 if (field->flags & FIELD_IS_LONG)
4748 trace_seq_printf(s, "0x%llx", val);
4750 trace_seq_printf(s, "%llu", val);
4753 field = field->next;
4757 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4759 struct pevent *pevent = event->pevent;
4760 struct print_fmt *print_fmt = &event->print_fmt;
4761 struct print_arg *arg = print_fmt->args;
4762 struct print_arg *args = NULL;
4763 const char *ptr = print_fmt->format;
4764 unsigned long long val;
4765 struct func_map *func;
4766 const char *saveptr;
4768 char *bprint_fmt = NULL;
4776 if (event->flags & EVENT_FL_FAILED) {
4777 trace_seq_printf(s, "[FAILED TO PARSE]");
4778 print_event_fields(s, data, size, event);
4782 if (event->flags & EVENT_FL_ISBPRINT) {
4783 bprint_fmt = get_bprint_format(data, size, event);
4784 args = make_bprint_args(bprint_fmt, data, size, event);
4789 for (; *ptr; ptr++) {
4795 trace_seq_putc(s, '\n');
4798 trace_seq_putc(s, '\t');
4801 trace_seq_putc(s, '\r');
4804 trace_seq_putc(s, '\\');
4807 trace_seq_putc(s, *ptr);
4811 } else if (*ptr == '%') {
4819 trace_seq_putc(s, '%');
4822 /* FIXME: need to handle properly */
4834 /* The argument is the length. */
4836 do_warning_event(event, "no argument match");
4837 event->flags |= EVENT_FL_FAILED;
4840 len_arg = eval_num_arg(data, size, event, arg);
4851 if (pevent->long_size == 4)
4856 if (*(ptr+1) == 'F' ||
4860 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4861 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4865 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4868 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4883 do_warning_event(event, "no argument match");
4884 event->flags |= EVENT_FL_FAILED;
4888 len = ((unsigned long)ptr + 1) -
4889 (unsigned long)saveptr;
4891 /* should never happen */
4893 do_warning_event(event, "bad format!");
4894 event->flags |= EVENT_FL_FAILED;
4898 memcpy(format, saveptr, len);
4901 val = eval_num_arg(data, size, event, arg);
4905 func = find_func(pevent, val);
4907 trace_seq_puts(s, func->func);
4908 if (show_func == 'F')
4915 if (pevent->long_size == 8 && ls &&
4916 sizeof(long) != 8) {
4920 /* make %l into %ll */
4921 p = strchr(format, 'l');
4923 memmove(p+1, p, strlen(p)+1);
4924 else if (strcmp(format, "%p") == 0)
4925 strcpy(format, "0x%llx");
4930 trace_seq_printf(s, format, len_arg, (char)val);
4932 trace_seq_printf(s, format, (char)val);
4936 trace_seq_printf(s, format, len_arg, (short)val);
4938 trace_seq_printf(s, format, (short)val);
4942 trace_seq_printf(s, format, len_arg, (int)val);
4944 trace_seq_printf(s, format, (int)val);
4948 trace_seq_printf(s, format, len_arg, (long)val);
4950 trace_seq_printf(s, format, (long)val);
4954 trace_seq_printf(s, format, len_arg,
4957 trace_seq_printf(s, format, (long long)val);
4960 do_warning_event(event, "bad count (%d)", ls);
4961 event->flags |= EVENT_FL_FAILED;
4966 do_warning_event(event, "no matching argument");
4967 event->flags |= EVENT_FL_FAILED;
4971 len = ((unsigned long)ptr + 1) -
4972 (unsigned long)saveptr;
4974 /* should never happen */
4976 do_warning_event(event, "bad format!");
4977 event->flags |= EVENT_FL_FAILED;
4981 memcpy(format, saveptr, len);
4985 /* Use helper trace_seq */
4987 print_str_arg(&p, data, size, event,
4988 format, len_arg, arg);
4989 trace_seq_terminate(&p);
4990 trace_seq_puts(s, p.buffer);
4991 trace_seq_destroy(&p);
4995 trace_seq_printf(s, ">%c<", *ptr);
4999 trace_seq_putc(s, *ptr);
5002 if (event->flags & EVENT_FL_FAILED) {
5004 trace_seq_printf(s, "[FAILED TO PARSE]");
5014 * pevent_data_lat_fmt - parse the data for the latency format
5015 * @pevent: a handle to the pevent
5016 * @s: the trace_seq to write to
5017 * @record: the record to read from
5019 * This parses out the Latency format (interrupts disabled,
5020 * need rescheduling, in hard/soft interrupt, preempt count
5021 * and lock depth) and places it into the trace_seq.
5023 void pevent_data_lat_fmt(struct pevent *pevent,
5024 struct trace_seq *s, struct pevent_record *record)
5026 static int check_lock_depth = 1;
5027 static int check_migrate_disable = 1;
5028 static int lock_depth_exists;
5029 static int migrate_disable_exists;
5030 unsigned int lat_flags;
5033 int migrate_disable;
5036 void *data = record->data;
5038 lat_flags = parse_common_flags(pevent, data);
5039 pc = parse_common_pc(pevent, data);
5040 /* lock_depth may not always exist */
5041 if (lock_depth_exists)
5042 lock_depth = parse_common_lock_depth(pevent, data);
5043 else if (check_lock_depth) {
5044 lock_depth = parse_common_lock_depth(pevent, data);
5046 check_lock_depth = 0;
5048 lock_depth_exists = 1;
5051 /* migrate_disable may not always exist */
5052 if (migrate_disable_exists)
5053 migrate_disable = parse_common_migrate_disable(pevent, data);
5054 else if (check_migrate_disable) {
5055 migrate_disable = parse_common_migrate_disable(pevent, data);
5056 if (migrate_disable < 0)
5057 check_migrate_disable = 0;
5059 migrate_disable_exists = 1;
5062 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5063 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5065 trace_seq_printf(s, "%c%c%c",
5066 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5067 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5069 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5071 (hardirq && softirq) ? 'H' :
5072 hardirq ? 'h' : softirq ? 's' : '.');
5075 trace_seq_printf(s, "%x", pc);
5077 trace_seq_putc(s, '.');
5079 if (migrate_disable_exists) {
5080 if (migrate_disable < 0)
5081 trace_seq_putc(s, '.');
5083 trace_seq_printf(s, "%d", migrate_disable);
5086 if (lock_depth_exists) {
5088 trace_seq_putc(s, '.');
5090 trace_seq_printf(s, "%d", lock_depth);
5093 trace_seq_terminate(s);
5097 * pevent_data_type - parse out the given event type
5098 * @pevent: a handle to the pevent
5099 * @rec: the record to read from
5101 * This returns the event id from the @rec.
5103 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5105 return trace_parse_common_type(pevent, rec->data);
5109 * pevent_data_event_from_type - find the event by a given type
5110 * @pevent: a handle to the pevent
5111 * @type: the type of the event.
5113 * This returns the event form a given @type;
5115 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5117 return pevent_find_event(pevent, type);
5121 * pevent_data_pid - parse the PID from raw data
5122 * @pevent: a handle to the pevent
5123 * @rec: the record to parse
5125 * This returns the PID from a raw data.
5127 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5129 return parse_common_pid(pevent, rec->data);
5133 * pevent_data_comm_from_pid - return the command line from PID
5134 * @pevent: a handle to the pevent
5135 * @pid: the PID of the task to search for
5137 * This returns a pointer to the command line that has the given
5140 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5144 comm = find_cmdline(pevent, pid);
5148 static struct cmdline *
5149 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5151 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5154 cmdlist = cmdlist->next;
5156 cmdlist = pevent->cmdlist;
5158 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5159 cmdlist = cmdlist->next;
5161 return (struct cmdline *)cmdlist;
5165 * pevent_data_pid_from_comm - return the pid from a given comm
5166 * @pevent: a handle to the pevent
5167 * @comm: the cmdline to find the pid from
5168 * @next: the cmdline structure to find the next comm
5170 * This returns the cmdline structure that holds a pid for a given
5171 * comm, or NULL if none found. As there may be more than one pid for
5172 * a given comm, the result of this call can be passed back into
5173 * a recurring call in the @next paramater, and then it will find the
5175 * Also, it does a linear seach, so it may be slow.
5177 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5178 struct cmdline *next)
5180 struct cmdline *cmdline;
5183 * If the cmdlines have not been converted yet, then use
5186 if (!pevent->cmdlines)
5187 return pid_from_cmdlist(pevent, comm, next);
5191 * The next pointer could have been still from
5192 * a previous call before cmdlines were created
5194 if (next < pevent->cmdlines ||
5195 next >= pevent->cmdlines + pevent->cmdline_count)
5202 cmdline = pevent->cmdlines;
5204 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5205 if (strcmp(cmdline->comm, comm) == 0)
5213 * pevent_cmdline_pid - return the pid associated to a given cmdline
5214 * @cmdline: The cmdline structure to get the pid from
5216 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5219 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5221 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5227 * If cmdlines have not been created yet, or cmdline is
5228 * not part of the array, then treat it as a cmdlist instead.
5230 if (!pevent->cmdlines ||
5231 cmdline < pevent->cmdlines ||
5232 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5233 return cmdlist->pid;
5235 return cmdline->pid;
5239 * pevent_data_comm_from_pid - parse the data into the print format
5240 * @s: the trace_seq to write to
5241 * @event: the handle to the event
5242 * @record: the record to read from
5244 * This parses the raw @data using the given @event information and
5245 * writes the print format into the trace_seq.
5247 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5248 struct pevent_record *record)
5250 int print_pretty = 1;
5252 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5253 print_event_fields(s, record->data, record->size, event);
5256 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5257 print_pretty = event->handler(s, record, event,
5261 pretty_print(s, record->data, record->size, event);
5264 trace_seq_terminate(s);
5267 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5269 if (!use_trace_clock)
5272 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5273 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5276 /* trace_clock is setting in tsc or counter mode */
5280 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5281 struct pevent_record *record, bool use_trace_clock)
5283 static const char *spaces = " "; /* 20 spaces */
5284 struct event_format *event;
5286 unsigned long usecs;
5287 unsigned long nsecs;
5289 void *data = record->data;
5294 bool use_usec_format;
5296 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5298 if (use_usec_format) {
5299 secs = record->ts / NSECS_PER_SEC;
5300 nsecs = record->ts - secs * NSECS_PER_SEC;
5303 if (record->size < 0) {
5304 do_warning("ug! negative record size %d", record->size);
5308 type = trace_parse_common_type(pevent, data);
5310 event = pevent_find_event(pevent, type);
5312 do_warning("ug! no event found for type %d", type);
5316 pid = parse_common_pid(pevent, data);
5317 comm = find_cmdline(pevent, pid);
5319 if (pevent->latency_format) {
5320 trace_seq_printf(s, "%8.8s-%-5d %3d",
5321 comm, pid, record->cpu);
5322 pevent_data_lat_fmt(pevent, s, record);
5324 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5326 if (use_usec_format) {
5327 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5331 usecs = (nsecs + 500) / NSECS_PER_USEC;
5335 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5336 secs, p, usecs, event->name);
5338 trace_seq_printf(s, " %12llu: %s: ",
5339 record->ts, event->name);
5341 /* Space out the event names evenly. */
5342 len = strlen(event->name);
5344 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5346 pevent_event_info(s, event, record);
5349 static int events_id_cmp(const void *a, const void *b)
5351 struct event_format * const * ea = a;
5352 struct event_format * const * eb = b;
5354 if ((*ea)->id < (*eb)->id)
5357 if ((*ea)->id > (*eb)->id)
5363 static int events_name_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)->name, (*eb)->name);
5373 res = strcmp((*ea)->system, (*eb)->system);
5377 return events_id_cmp(a, b);
5380 static int events_system_cmp(const void *a, const void *b)
5382 struct event_format * const * ea = a;
5383 struct event_format * const * eb = b;
5386 res = strcmp((*ea)->system, (*eb)->system);
5390 res = strcmp((*ea)->name, (*eb)->name);
5394 return events_id_cmp(a, b);
5397 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5399 struct event_format **events;
5400 int (*sort)(const void *a, const void *b);
5402 events = pevent->sort_events;
5404 if (events && pevent->last_type == sort_type)
5408 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5412 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5413 events[pevent->nr_events] = NULL;
5415 pevent->sort_events = events;
5417 /* the internal events are sorted by id */
5418 if (sort_type == EVENT_SORT_ID) {
5419 pevent->last_type = sort_type;
5424 switch (sort_type) {
5426 sort = events_id_cmp;
5428 case EVENT_SORT_NAME:
5429 sort = events_name_cmp;
5431 case EVENT_SORT_SYSTEM:
5432 sort = events_system_cmp;
5438 qsort(events, pevent->nr_events, sizeof(*events), sort);
5439 pevent->last_type = sort_type;
5444 static struct format_field **
5445 get_event_fields(const char *type, const char *name,
5446 int count, struct format_field *list)
5448 struct format_field **fields;
5449 struct format_field *field;
5452 fields = malloc(sizeof(*fields) * (count + 1));
5456 for (field = list; field; field = field->next) {
5457 fields[i++] = field;
5458 if (i == count + 1) {
5459 do_warning("event %s has more %s fields than specified",
5467 do_warning("event %s has less %s fields than specified",
5476 * pevent_event_common_fields - return a list of common fields for an event
5477 * @event: the event to return the common fields of.
5479 * Returns an allocated array of fields. The last item in the array is NULL.
5480 * The array must be freed with free().
5482 struct format_field **pevent_event_common_fields(struct event_format *event)
5484 return get_event_fields("common", event->name,
5485 event->format.nr_common,
5486 event->format.common_fields);
5490 * pevent_event_fields - return a list of event specific fields for an event
5491 * @event: the event to return the fields of.
5493 * Returns an allocated array of fields. The last item in the array is NULL.
5494 * The array must be freed with free().
5496 struct format_field **pevent_event_fields(struct event_format *event)
5498 return get_event_fields("event", event->name,
5499 event->format.nr_fields,
5500 event->format.fields);
5503 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5505 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5507 trace_seq_puts(s, ", ");
5508 print_fields(s, field->next);
5513 static void print_args(struct print_arg *args)
5515 int print_paren = 1;
5518 switch (args->type) {
5523 printf("%s", args->atom.atom);
5526 printf("REC->%s", args->field.name);
5529 printf("__print_flags(");
5530 print_args(args->flags.field);
5531 printf(", %s, ", args->flags.delim);
5533 print_fields(&s, args->flags.flags);
5534 trace_seq_do_printf(&s);
5535 trace_seq_destroy(&s);
5539 printf("__print_symbolic(");
5540 print_args(args->symbol.field);
5543 print_fields(&s, args->symbol.symbols);
5544 trace_seq_do_printf(&s);
5545 trace_seq_destroy(&s);
5549 printf("__print_hex(");
5550 print_args(args->hex.field);
5552 print_args(args->hex.size);
5555 case PRINT_INT_ARRAY:
5556 printf("__print_array(");
5557 print_args(args->int_array.field);
5559 print_args(args->int_array.count);
5561 print_args(args->int_array.el_size);
5566 printf("__get_str(%s)", args->string.string);
5569 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5572 printf("(%s)", args->typecast.type);
5573 print_args(args->typecast.item);
5576 if (strcmp(args->op.op, ":") == 0)
5580 print_args(args->op.left);
5581 printf(" %s ", args->op.op);
5582 print_args(args->op.right);
5587 /* we should warn... */
5592 print_args(args->next);
5596 static void parse_header_field(const char *field,
5597 int *offset, int *size, int mandatory)
5599 unsigned long long save_input_buf_ptr;
5600 unsigned long long save_input_buf_siz;
5604 save_input_buf_ptr = input_buf_ptr;
5605 save_input_buf_siz = input_buf_siz;
5607 if (read_expected(EVENT_ITEM, "field") < 0)
5609 if (read_expected(EVENT_OP, ":") < 0)
5613 if (read_expect_type(EVENT_ITEM, &token) < 0)
5618 * If this is not a mandatory field, then test it first.
5621 if (read_expected(EVENT_ITEM, field) < 0)
5624 if (read_expect_type(EVENT_ITEM, &token) < 0)
5626 if (strcmp(token, field) != 0)
5631 if (read_expected(EVENT_OP, ";") < 0)
5633 if (read_expected(EVENT_ITEM, "offset") < 0)
5635 if (read_expected(EVENT_OP, ":") < 0)
5637 if (read_expect_type(EVENT_ITEM, &token) < 0)
5639 *offset = atoi(token);
5641 if (read_expected(EVENT_OP, ";") < 0)
5643 if (read_expected(EVENT_ITEM, "size") < 0)
5645 if (read_expected(EVENT_OP, ":") < 0)
5647 if (read_expect_type(EVENT_ITEM, &token) < 0)
5649 *size = atoi(token);
5651 if (read_expected(EVENT_OP, ";") < 0)
5653 type = read_token(&token);
5654 if (type != EVENT_NEWLINE) {
5655 /* newer versions of the kernel have a "signed" type */
5656 if (type != EVENT_ITEM)
5659 if (strcmp(token, "signed") != 0)
5664 if (read_expected(EVENT_OP, ":") < 0)
5667 if (read_expect_type(EVENT_ITEM, &token))
5671 if (read_expected(EVENT_OP, ";") < 0)
5674 if (read_expect_type(EVENT_NEWLINE, &token))
5682 input_buf_ptr = save_input_buf_ptr;
5683 input_buf_siz = save_input_buf_siz;
5690 * pevent_parse_header_page - parse the data stored in the header page
5691 * @pevent: the handle to the pevent
5692 * @buf: the buffer storing the header page format string
5693 * @size: the size of @buf
5694 * @long_size: the long size to use if there is no header
5696 * This parses the header page format for information on the
5697 * ring buffer used. The @buf should be copied from
5699 * /sys/kernel/debug/tracing/events/header_page
5701 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5708 * Old kernels did not have header page info.
5709 * Sorry but we just use what we find here in user space.
5711 pevent->header_page_ts_size = sizeof(long long);
5712 pevent->header_page_size_size = long_size;
5713 pevent->header_page_data_offset = sizeof(long long) + long_size;
5714 pevent->old_format = 1;
5717 init_input_buf(buf, size);
5719 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5720 &pevent->header_page_ts_size, 1);
5721 parse_header_field("commit", &pevent->header_page_size_offset,
5722 &pevent->header_page_size_size, 1);
5723 parse_header_field("overwrite", &pevent->header_page_overwrite,
5725 parse_header_field("data", &pevent->header_page_data_offset,
5726 &pevent->header_page_data_size, 1);
5731 static int event_matches(struct event_format *event,
5732 int id, const char *sys_name,
5733 const char *event_name)
5735 if (id >= 0 && id != event->id)
5738 if (event_name && (strcmp(event_name, event->name) != 0))
5741 if (sys_name && (strcmp(sys_name, event->system) != 0))
5747 static void free_handler(struct event_handler *handle)
5749 free((void *)handle->sys_name);
5750 free((void *)handle->event_name);
5754 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5756 struct event_handler *handle, **next;
5758 for (next = &pevent->handlers; *next;
5759 next = &(*next)->next) {
5761 if (event_matches(event, handle->id,
5763 handle->event_name))
5770 pr_stat("overriding event (%d) %s:%s with new print handler",
5771 event->id, event->system, event->name);
5773 event->handler = handle->func;
5774 event->context = handle->context;
5776 *next = handle->next;
5777 free_handler(handle);
5783 * __pevent_parse_format - parse the event format
5784 * @buf: the buffer storing the event format string
5785 * @size: the size of @buf
5786 * @sys: the system the event belongs to
5788 * This parses the event format and creates an event structure
5789 * to quickly parse raw data for a given event.
5791 * These files currently come from:
5793 * /sys/kernel/debug/tracing/events/.../.../format
5795 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5796 struct pevent *pevent, const char *buf,
5797 unsigned long size, const char *sys)
5799 struct event_format *event;
5802 init_input_buf(buf, size);
5804 *eventp = event = alloc_event();
5806 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5808 event->name = event_read_name();
5811 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5812 goto event_alloc_failed;
5815 if (strcmp(sys, "ftrace") == 0) {
5816 event->flags |= EVENT_FL_ISFTRACE;
5818 if (strcmp(event->name, "bprint") == 0)
5819 event->flags |= EVENT_FL_ISBPRINT;
5822 event->id = event_read_id();
5823 if (event->id < 0) {
5824 ret = PEVENT_ERRNO__READ_ID_FAILED;
5826 * This isn't an allocation error actually.
5827 * But as the ID is critical, just bail out.
5829 goto event_alloc_failed;
5832 event->system = strdup(sys);
5833 if (!event->system) {
5834 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5835 goto event_alloc_failed;
5838 /* Add pevent to event so that it can be referenced */
5839 event->pevent = pevent;
5841 ret = event_read_format(event);
5843 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5844 goto event_parse_failed;
5848 * If the event has an override, don't print warnings if the event
5849 * print format fails to parse.
5851 if (pevent && find_event_handle(pevent, event))
5854 ret = event_read_print(event);
5858 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5859 goto event_parse_failed;
5862 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5863 struct format_field *field;
5864 struct print_arg *arg, **list;
5866 /* old ftrace had no args */
5867 list = &event->print_fmt.args;
5868 for (field = event->format.fields; field; field = field->next) {
5871 event->flags |= EVENT_FL_FAILED;
5872 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5874 arg->type = PRINT_FIELD;
5875 arg->field.name = strdup(field->name);
5876 if (!arg->field.name) {
5877 event->flags |= EVENT_FL_FAILED;
5879 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5881 arg->field.field = field;
5891 event->flags |= EVENT_FL_FAILED;
5895 free(event->system);
5902 static enum pevent_errno
5903 __pevent_parse_event(struct pevent *pevent,
5904 struct event_format **eventp,
5905 const char *buf, unsigned long size,
5908 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5909 struct event_format *event = *eventp;
5914 if (pevent && add_event(pevent, event)) {
5915 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5916 goto event_add_failed;
5919 #define PRINT_ARGS 0
5920 if (PRINT_ARGS && event->print_fmt.args)
5921 print_args(event->print_fmt.args);
5926 pevent_free_format(event);
5931 * pevent_parse_format - parse the event format
5932 * @pevent: the handle to the pevent
5933 * @eventp: returned format
5934 * @buf: the buffer storing the event format string
5935 * @size: the size of @buf
5936 * @sys: the system the event belongs to
5938 * This parses the event format and creates an event structure
5939 * to quickly parse raw data for a given event.
5941 * These files currently come from:
5943 * /sys/kernel/debug/tracing/events/.../.../format
5945 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5946 struct event_format **eventp,
5948 unsigned long size, const char *sys)
5950 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5954 * pevent_parse_event - parse the event format
5955 * @pevent: the handle to the pevent
5956 * @buf: the buffer storing the event format string
5957 * @size: the size of @buf
5958 * @sys: the system the event belongs to
5960 * This parses the event format and creates an event structure
5961 * to quickly parse raw data for a given event.
5963 * These files currently come from:
5965 * /sys/kernel/debug/tracing/events/.../.../format
5967 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5968 unsigned long size, const char *sys)
5970 struct event_format *event = NULL;
5971 return __pevent_parse_event(pevent, &event, buf, size, sys);
5975 #define _PE(code, str) str
5976 static const char * const pevent_error_str[] = {
5981 int pevent_strerror(struct pevent *pevent __maybe_unused,
5982 enum pevent_errno errnum, char *buf, size_t buflen)
5988 msg = strerror_r(errnum, buf, buflen);
5990 size_t len = strlen(msg);
5991 memcpy(buf, msg, min(buflen - 1, len));
5992 *(buf + min(buflen - 1, len)) = '\0';
5997 if (errnum <= __PEVENT_ERRNO__START ||
5998 errnum >= __PEVENT_ERRNO__END)
6001 idx = errnum - __PEVENT_ERRNO__START - 1;
6002 msg = pevent_error_str[idx];
6003 snprintf(buf, buflen, "%s", msg);
6008 int get_field_val(struct trace_seq *s, struct format_field *field,
6009 const char *name, struct pevent_record *record,
6010 unsigned long long *val, int err)
6014 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6018 if (pevent_read_number_field(field, record->data, val)) {
6020 trace_seq_printf(s, " %s=INVALID", name);
6028 * pevent_get_field_raw - return the raw pointer into the data field
6029 * @s: The seq to print to on error
6030 * @event: the event that the field is for
6031 * @name: The name of the field
6032 * @record: The record with the field name.
6033 * @len: place to store the field length.
6034 * @err: print default error if failed.
6036 * Returns a pointer into record->data of the field and places
6037 * the length of the field in @len.
6039 * On failure, it returns NULL.
6041 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6042 const char *name, struct pevent_record *record,
6045 struct format_field *field;
6046 void *data = record->data;
6053 field = pevent_find_field(event, name);
6057 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6061 /* Allow @len to be NULL */
6065 offset = field->offset;
6066 if (field->flags & FIELD_IS_DYNAMIC) {
6067 offset = pevent_read_number(event->pevent,
6068 data + offset, field->size);
6069 *len = offset >> 16;
6074 return data + offset;
6078 * pevent_get_field_val - find a field and return its value
6079 * @s: The seq to print to on error
6080 * @event: the event that the field is for
6081 * @name: The name of the field
6082 * @record: The record with the field name.
6083 * @val: place to store the value of the field.
6084 * @err: print default error if failed.
6086 * Returns 0 on success -1 on field not found.
6088 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6089 const char *name, struct pevent_record *record,
6090 unsigned long long *val, int err)
6092 struct format_field *field;
6097 field = pevent_find_field(event, name);
6099 return get_field_val(s, field, name, record, val, err);
6103 * pevent_get_common_field_val - find a common field and return its value
6104 * @s: The seq to print to on error
6105 * @event: the event that the field is for
6106 * @name: The name of the field
6107 * @record: The record with the field name.
6108 * @val: place to store the value of the field.
6109 * @err: print default error if failed.
6111 * Returns 0 on success -1 on field not found.
6113 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6114 const char *name, struct pevent_record *record,
6115 unsigned long long *val, int err)
6117 struct format_field *field;
6122 field = pevent_find_common_field(event, name);
6124 return get_field_val(s, field, name, record, val, err);
6128 * pevent_get_any_field_val - find a any field and return its value
6129 * @s: The seq to print to on error
6130 * @event: the event that the field is for
6131 * @name: The name of the field
6132 * @record: The record with the field name.
6133 * @val: place to store the value of the field.
6134 * @err: print default error if failed.
6136 * Returns 0 on success -1 on field not found.
6138 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6139 const char *name, struct pevent_record *record,
6140 unsigned long long *val, int err)
6142 struct format_field *field;
6147 field = pevent_find_any_field(event, name);
6149 return get_field_val(s, field, name, record, val, err);
6153 * pevent_print_num_field - print a field and a format
6154 * @s: The seq to print to
6155 * @fmt: The printf format to print the field with.
6156 * @event: the event that the field is for
6157 * @name: The name of the field
6158 * @record: The record with the field name.
6159 * @err: print default error if failed.
6161 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6163 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6164 struct event_format *event, const char *name,
6165 struct pevent_record *record, int err)
6167 struct format_field *field = pevent_find_field(event, name);
6168 unsigned long long val;
6173 if (pevent_read_number_field(field, record->data, &val))
6176 return trace_seq_printf(s, fmt, val);
6180 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6185 * pevent_print_func_field - print a field and a format for function pointers
6186 * @s: The seq to print to
6187 * @fmt: The printf format to print the field with.
6188 * @event: the event that the field is for
6189 * @name: The name of the field
6190 * @record: The record with the field name.
6191 * @err: print default error if failed.
6193 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6195 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6196 struct event_format *event, const char *name,
6197 struct pevent_record *record, int err)
6199 struct format_field *field = pevent_find_field(event, name);
6200 struct pevent *pevent = event->pevent;
6201 unsigned long long val;
6202 struct func_map *func;
6208 if (pevent_read_number_field(field, record->data, &val))
6211 func = find_func(pevent, val);
6214 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6216 sprintf(tmp, "0x%08llx", val);
6218 return trace_seq_printf(s, fmt, tmp);
6222 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6226 static void free_func_handle(struct pevent_function_handler *func)
6228 struct pevent_func_params *params;
6232 while (func->params) {
6233 params = func->params;
6234 func->params = params->next;
6242 * pevent_register_print_function - register a helper function
6243 * @pevent: the handle to the pevent
6244 * @func: the function to process the helper function
6245 * @ret_type: the return type of the helper function
6246 * @name: the name of the helper function
6247 * @parameters: A list of enum pevent_func_arg_type
6249 * Some events may have helper functions in the print format arguments.
6250 * This allows a plugin to dynamically create a way to process one
6251 * of these functions.
6253 * The @parameters is a variable list of pevent_func_arg_type enums that
6254 * must end with PEVENT_FUNC_ARG_VOID.
6256 int pevent_register_print_function(struct pevent *pevent,
6257 pevent_func_handler func,
6258 enum pevent_func_arg_type ret_type,
6261 struct pevent_function_handler *func_handle;
6262 struct pevent_func_params **next_param;
6263 struct pevent_func_params *param;
6264 enum pevent_func_arg_type type;
6268 func_handle = find_func_handler(pevent, name);
6271 * This is most like caused by the users own
6272 * plugins updating the function. This overrides the
6275 pr_stat("override of function helper '%s'", name);
6276 remove_func_handler(pevent, name);
6279 func_handle = calloc(1, sizeof(*func_handle));
6281 do_warning("Failed to allocate function handler");
6282 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6285 func_handle->ret_type = ret_type;
6286 func_handle->name = strdup(name);
6287 func_handle->func = func;
6288 if (!func_handle->name) {
6289 do_warning("Failed to allocate function name");
6291 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6294 next_param = &(func_handle->params);
6297 type = va_arg(ap, enum pevent_func_arg_type);
6298 if (type == PEVENT_FUNC_ARG_VOID)
6301 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6302 do_warning("Invalid argument type %d", type);
6303 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6307 param = malloc(sizeof(*param));
6309 do_warning("Failed to allocate function param");
6310 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6316 *next_param = param;
6317 next_param = &(param->next);
6319 func_handle->nr_args++;
6323 func_handle->next = pevent->func_handlers;
6324 pevent->func_handlers = func_handle;
6329 free_func_handle(func_handle);
6334 * pevent_unregister_print_function - unregister a helper function
6335 * @pevent: the handle to the pevent
6336 * @func: the function to process the helper function
6337 * @name: the name of the helper function
6339 * This function removes existing print handler for function @name.
6341 * Returns 0 if the handler was removed successully, -1 otherwise.
6343 int pevent_unregister_print_function(struct pevent *pevent,
6344 pevent_func_handler func, char *name)
6346 struct pevent_function_handler *func_handle;
6348 func_handle = find_func_handler(pevent, name);
6349 if (func_handle && func_handle->func == func) {
6350 remove_func_handler(pevent, name);
6356 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6357 const char *sys_name,
6358 const char *event_name)
6360 struct event_format *event;
6364 event = pevent_find_event(pevent, id);
6367 if (event_name && (strcmp(event_name, event->name) != 0))
6369 if (sys_name && (strcmp(sys_name, event->system) != 0))
6372 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6380 * pevent_register_event_handler - register a way to parse an event
6381 * @pevent: the handle to the pevent
6382 * @id: the id of the event to register
6383 * @sys_name: the system name the event belongs to
6384 * @event_name: the name of the event
6385 * @func: the function to call to parse the event information
6386 * @context: the data to be passed to @func
6388 * This function allows a developer to override the parsing of
6389 * a given event. If for some reason the default print format
6390 * is not sufficient, this function will register a function
6391 * for an event to be used to parse the data instead.
6393 * If @id is >= 0, then it is used to find the event.
6394 * else @sys_name and @event_name are used.
6396 int pevent_register_event_handler(struct pevent *pevent, int id,
6397 const char *sys_name, const char *event_name,
6398 pevent_event_handler_func func, void *context)
6400 struct event_format *event;
6401 struct event_handler *handle;
6403 event = pevent_search_event(pevent, id, sys_name, event_name);
6407 pr_stat("overriding event (%d) %s:%s with new print handler",
6408 event->id, event->system, event->name);
6410 event->handler = func;
6411 event->context = context;
6415 /* Save for later use. */
6416 handle = calloc(1, sizeof(*handle));
6418 do_warning("Failed to allocate event handler");
6419 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6424 handle->event_name = strdup(event_name);
6426 handle->sys_name = strdup(sys_name);
6428 if ((event_name && !handle->event_name) ||
6429 (sys_name && !handle->sys_name)) {
6430 do_warning("Failed to allocate event/sys name");
6431 free((void *)handle->event_name);
6432 free((void *)handle->sys_name);
6434 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6437 handle->func = func;
6438 handle->next = pevent->handlers;
6439 pevent->handlers = handle;
6440 handle->context = context;
6445 static int handle_matches(struct event_handler *handler, int id,
6446 const char *sys_name, const char *event_name,
6447 pevent_event_handler_func func, void *context)
6449 if (id >= 0 && id != handler->id)
6452 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6455 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6458 if (func != handler->func || context != handler->context)
6465 * pevent_unregister_event_handler - unregister an existing event handler
6466 * @pevent: the handle to the pevent
6467 * @id: the id of the event to unregister
6468 * @sys_name: the system name the handler belongs to
6469 * @event_name: the name of the event handler
6470 * @func: the function to call to parse the event information
6471 * @context: the data to be passed to @func
6473 * This function removes existing event handler (parser).
6475 * If @id is >= 0, then it is used to find the event.
6476 * else @sys_name and @event_name are used.
6478 * Returns 0 if handler was removed successfully, -1 if event was not found.
6480 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6481 const char *sys_name, const char *event_name,
6482 pevent_event_handler_func func, void *context)
6484 struct event_format *event;
6485 struct event_handler *handle;
6486 struct event_handler **next;
6488 event = pevent_search_event(pevent, id, sys_name, event_name);
6492 if (event->handler == func && event->context == context) {
6493 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6494 event->id, event->system, event->name);
6496 event->handler = NULL;
6497 event->context = NULL;
6502 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6504 if (handle_matches(handle, id, sys_name, event_name,
6512 *next = handle->next;
6513 free_handler(handle);
6519 * pevent_alloc - create a pevent handle
6521 struct pevent *pevent_alloc(void)
6523 struct pevent *pevent = calloc(1, sizeof(*pevent));
6526 pevent->ref_count = 1;
6531 void pevent_ref(struct pevent *pevent)
6533 pevent->ref_count++;
6536 void pevent_free_format_field(struct format_field *field)
6539 if (field->alias != field->name)
6545 static void free_format_fields(struct format_field *field)
6547 struct format_field *next;
6551 pevent_free_format_field(field);
6556 static void free_formats(struct format *format)
6558 free_format_fields(format->common_fields);
6559 free_format_fields(format->fields);
6562 void pevent_free_format(struct event_format *event)
6565 free(event->system);
6567 free_formats(&event->format);
6569 free(event->print_fmt.format);
6570 free_args(event->print_fmt.args);
6576 * pevent_free - free a pevent handle
6577 * @pevent: the pevent handle to free
6579 void pevent_free(struct pevent *pevent)
6581 struct cmdline_list *cmdlist, *cmdnext;
6582 struct func_list *funclist, *funcnext;
6583 struct printk_list *printklist, *printknext;
6584 struct pevent_function_handler *func_handler;
6585 struct event_handler *handle;
6591 cmdlist = pevent->cmdlist;
6592 funclist = pevent->funclist;
6593 printklist = pevent->printklist;
6595 pevent->ref_count--;
6596 if (pevent->ref_count)
6599 if (pevent->cmdlines) {
6600 for (i = 0; i < pevent->cmdline_count; i++)
6601 free(pevent->cmdlines[i].comm);
6602 free(pevent->cmdlines);
6606 cmdnext = cmdlist->next;
6607 free(cmdlist->comm);
6612 if (pevent->func_map) {
6613 for (i = 0; i < (int)pevent->func_count; i++) {
6614 free(pevent->func_map[i].func);
6615 free(pevent->func_map[i].mod);
6617 free(pevent->func_map);
6621 funcnext = funclist->next;
6622 free(funclist->func);
6623 free(funclist->mod);
6625 funclist = funcnext;
6628 while (pevent->func_handlers) {
6629 func_handler = pevent->func_handlers;
6630 pevent->func_handlers = func_handler->next;
6631 free_func_handle(func_handler);
6634 if (pevent->printk_map) {
6635 for (i = 0; i < (int)pevent->printk_count; i++)
6636 free(pevent->printk_map[i].printk);
6637 free(pevent->printk_map);
6640 while (printklist) {
6641 printknext = printklist->next;
6642 free(printklist->printk);
6644 printklist = printknext;
6647 for (i = 0; i < pevent->nr_events; i++)
6648 pevent_free_format(pevent->events[i]);
6650 while (pevent->handlers) {
6651 handle = pevent->handlers;
6652 pevent->handlers = handle->next;
6653 free_handler(handle);
6656 free(pevent->trace_clock);
6657 free(pevent->events);
6658 free(pevent->sort_events);
6659 free(pevent->func_resolver);
6664 void pevent_unref(struct pevent *pevent)
6666 pevent_free(pevent);