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));
307 item->comm = strdup(comm);
313 item->next = pevent->cmdlist;
315 pevent->cmdlist = item;
316 pevent->cmdline_count++;
321 void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
323 pevent->trace_clock = trace_clock;
327 unsigned long long addr;
333 struct func_list *next;
334 unsigned long long addr;
339 static int func_cmp(const void *a, const void *b)
341 const struct func_map *fa = a;
342 const struct func_map *fb = b;
344 if (fa->addr < fb->addr)
346 if (fa->addr > fb->addr)
353 * We are searching for a record in between, not an exact
356 static int func_bcmp(const void *a, const void *b)
358 const struct func_map *fa = a;
359 const struct func_map *fb = b;
361 if ((fa->addr == fb->addr) ||
363 (fa->addr > fb->addr &&
364 fa->addr < (fb+1)->addr))
367 if (fa->addr < fb->addr)
373 static int func_map_init(struct pevent *pevent)
375 struct func_list *funclist;
376 struct func_list *item;
377 struct func_map *func_map;
380 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
384 funclist = pevent->funclist;
388 func_map[i].func = funclist->func;
389 func_map[i].addr = funclist->addr;
390 func_map[i].mod = funclist->mod;
393 funclist = funclist->next;
397 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
400 * Add a special record at the end.
402 func_map[pevent->func_count].func = NULL;
403 func_map[pevent->func_count].addr = 0;
404 func_map[pevent->func_count].mod = NULL;
406 pevent->func_map = func_map;
407 pevent->funclist = NULL;
412 static struct func_map *
413 find_func(struct pevent *pevent, unsigned long long addr)
415 struct func_map *func;
418 if (!pevent->func_map)
419 func_map_init(pevent);
423 func = bsearch(&key, pevent->func_map, pevent->func_count,
424 sizeof(*pevent->func_map), func_bcmp);
430 * pevent_find_function - find a function by a given address
431 * @pevent: handle for the pevent
432 * @addr: the address to find the function with
434 * Returns a pointer to the function stored that has the given
435 * address. Note, the address does not have to be exact, it
436 * will select the function that would contain the address.
438 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
440 struct func_map *map;
442 map = find_func(pevent, addr);
450 * pevent_find_function_address - find a function address by a given address
451 * @pevent: handle for the pevent
452 * @addr: the address to find the function with
454 * Returns the address the function starts at. This can be used in
455 * conjunction with pevent_find_function to print both the function
456 * name and the function offset.
459 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
461 struct func_map *map;
463 map = find_func(pevent, addr);
471 * pevent_register_function - register a function with a given address
472 * @pevent: handle for the pevent
473 * @function: the function name to register
474 * @addr: the address the function starts at
475 * @mod: the kernel module the function may be in (NULL for none)
477 * This registers a function name with an address and module.
478 * The @func passed in is duplicated.
480 int pevent_register_function(struct pevent *pevent, char *func,
481 unsigned long long addr, char *mod)
483 struct func_list *item = malloc(sizeof(*item));
488 item->next = pevent->funclist;
489 item->func = strdup(func);
494 item->mod = strdup(mod);
501 pevent->funclist = item;
502 pevent->func_count++;
516 * pevent_print_funcs - print out the stored functions
517 * @pevent: handle for the pevent
519 * This prints out the stored functions.
521 void pevent_print_funcs(struct pevent *pevent)
525 if (!pevent->func_map)
526 func_map_init(pevent);
528 for (i = 0; i < (int)pevent->func_count; i++) {
530 pevent->func_map[i].addr,
531 pevent->func_map[i].func);
532 if (pevent->func_map[i].mod)
533 printf(" [%s]\n", pevent->func_map[i].mod);
540 unsigned long long addr;
545 struct printk_list *next;
546 unsigned long long addr;
550 static int printk_cmp(const void *a, const void *b)
552 const struct printk_map *pa = a;
553 const struct printk_map *pb = b;
555 if (pa->addr < pb->addr)
557 if (pa->addr > pb->addr)
563 static int printk_map_init(struct pevent *pevent)
565 struct printk_list *printklist;
566 struct printk_list *item;
567 struct printk_map *printk_map;
570 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
574 printklist = pevent->printklist;
578 printk_map[i].printk = printklist->printk;
579 printk_map[i].addr = printklist->addr;
582 printklist = printklist->next;
586 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
588 pevent->printk_map = printk_map;
589 pevent->printklist = NULL;
594 static struct printk_map *
595 find_printk(struct pevent *pevent, unsigned long long addr)
597 struct printk_map *printk;
598 struct printk_map key;
600 if (!pevent->printk_map && printk_map_init(pevent))
605 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
606 sizeof(*pevent->printk_map), printk_cmp);
612 * pevent_register_print_string - register a string by its address
613 * @pevent: handle for the pevent
614 * @fmt: the string format to register
615 * @addr: the address the string was located at
617 * This registers a string by the address it was stored in the kernel.
618 * The @fmt passed in is duplicated.
620 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
621 unsigned long long addr)
623 struct printk_list *item = malloc(sizeof(*item));
629 item->next = pevent->printklist;
632 /* Strip off quotes and '\n' from the end */
635 item->printk = strdup(fmt);
639 p = item->printk + strlen(item->printk) - 1;
644 if (strcmp(p, "\\n") == 0)
647 pevent->printklist = item;
648 pevent->printk_count++;
659 * pevent_print_printk - print out the stored strings
660 * @pevent: handle for the pevent
662 * This prints the string formats that were stored.
664 void pevent_print_printk(struct pevent *pevent)
668 if (!pevent->printk_map)
669 printk_map_init(pevent);
671 for (i = 0; i < (int)pevent->printk_count; i++) {
672 printf("%016llx %s\n",
673 pevent->printk_map[i].addr,
674 pevent->printk_map[i].printk);
678 static struct event_format *alloc_event(void)
680 return calloc(1, sizeof(struct event_format));
683 static int add_event(struct pevent *pevent, struct event_format *event)
686 struct event_format **events = realloc(pevent->events, sizeof(event) *
687 (pevent->nr_events + 1));
691 pevent->events = events;
693 for (i = 0; i < pevent->nr_events; i++) {
694 if (pevent->events[i]->id > event->id)
697 if (i < pevent->nr_events)
698 memmove(&pevent->events[i + 1],
700 sizeof(event) * (pevent->nr_events - i));
702 pevent->events[i] = event;
705 event->pevent = pevent;
710 static int event_item_type(enum event_type type)
713 case EVENT_ITEM ... EVENT_SQUOTE:
715 case EVENT_ERROR ... EVENT_DELIM:
721 static void free_flag_sym(struct print_flag_sym *fsym)
723 struct print_flag_sym *next;
734 static void free_arg(struct print_arg *arg)
736 struct print_arg *farg;
743 free(arg->atom.atom);
746 free(arg->field.name);
749 free_arg(arg->flags.field);
750 free(arg->flags.delim);
751 free_flag_sym(arg->flags.flags);
754 free_arg(arg->symbol.field);
755 free_flag_sym(arg->symbol.symbols);
758 free_arg(arg->hex.field);
759 free_arg(arg->hex.size);
762 free(arg->typecast.type);
763 free_arg(arg->typecast.item);
767 free(arg->string.string);
770 free(arg->bitmask.bitmask);
772 case PRINT_DYNAMIC_ARRAY:
773 free(arg->dynarray.index);
777 free_arg(arg->op.left);
778 free_arg(arg->op.right);
781 while (arg->func.args) {
782 farg = arg->func.args;
783 arg->func.args = farg->next;
796 static enum event_type get_type(int ch)
799 return EVENT_NEWLINE;
802 if (isalnum(ch) || ch == '_')
810 if (ch == '(' || ch == ')' || ch == ',')
816 static int __read_char(void)
818 if (input_buf_ptr >= input_buf_siz)
821 return input_buf[input_buf_ptr++];
824 static int __peek_char(void)
826 if (input_buf_ptr >= input_buf_siz)
829 return input_buf[input_buf_ptr];
833 * pevent_peek_char - peek at the next character that will be read
835 * Returns the next character read, or -1 if end of buffer.
837 int pevent_peek_char(void)
839 return __peek_char();
842 static int extend_token(char **tok, char *buf, int size)
844 char *newtok = realloc(*tok, size);
861 static enum event_type force_token(const char *str, char **tok);
863 static enum event_type __read_token(char **tok)
866 int ch, last_ch, quote_ch, next_ch;
869 enum event_type type;
879 if (type == EVENT_NONE)
887 if (asprintf(tok, "%c", ch) < 0)
895 next_ch = __peek_char();
896 if (next_ch == '>') {
897 buf[i++] = __read_char();
910 buf[i++] = __read_char();
922 default: /* what should we do instead? */
932 buf[i++] = __read_char();
937 /* don't keep quotes */
943 if (i == (BUFSIZ - 1)) {
947 if (extend_token(tok, buf, tok_size) < 0)
954 /* the '\' '\' will cancel itself */
955 if (ch == '\\' && last_ch == '\\')
957 } while (ch != quote_ch || last_ch == '\\');
958 /* remove the last quote */
962 * For strings (double quotes) check the next token.
963 * If it is another string, concatinate the two.
965 if (type == EVENT_DQUOTE) {
966 unsigned long long save_input_buf_ptr = input_buf_ptr;
970 } while (isspace(ch));
973 input_buf_ptr = save_input_buf_ptr;
978 case EVENT_ERROR ... EVENT_SPACE:
984 while (get_type(__peek_char()) == type) {
985 if (i == (BUFSIZ - 1)) {
989 if (extend_token(tok, buf, tok_size) < 0)
999 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1002 if (type == EVENT_ITEM) {
1004 * Older versions of the kernel has a bug that
1005 * creates invalid symbols and will break the mac80211
1006 * parsing. This is a work around to that bug.
1008 * See Linux kernel commit:
1009 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1011 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1014 return force_token("\"\%s\" ", tok);
1015 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1018 return force_token("\" sta:%pM\" ", tok);
1019 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1022 return force_token("\" vif:%p(%d)\" ", tok);
1029 static enum event_type force_token(const char *str, char **tok)
1031 const char *save_input_buf;
1032 unsigned long long save_input_buf_ptr;
1033 unsigned long long save_input_buf_siz;
1034 enum event_type type;
1036 /* save off the current input pointers */
1037 save_input_buf = input_buf;
1038 save_input_buf_ptr = input_buf_ptr;
1039 save_input_buf_siz = input_buf_siz;
1041 init_input_buf(str, strlen(str));
1043 type = __read_token(tok);
1045 /* reset back to original token */
1046 input_buf = save_input_buf;
1047 input_buf_ptr = save_input_buf_ptr;
1048 input_buf_siz = save_input_buf_siz;
1053 static void free_token(char *tok)
1059 static enum event_type read_token(char **tok)
1061 enum event_type type;
1064 type = __read_token(tok);
1065 if (type != EVENT_SPACE)
1077 * pevent_read_token - access to utilites to use the pevent parser
1078 * @tok: The token to return
1080 * This will parse tokens from the string given by
1081 * pevent_init_data().
1083 * Returns the token type.
1085 enum event_type pevent_read_token(char **tok)
1087 return read_token(tok);
1091 * pevent_free_token - free a token returned by pevent_read_token
1092 * @token: the token to free
1094 void pevent_free_token(char *token)
1100 static enum event_type read_token_item(char **tok)
1102 enum event_type type;
1105 type = __read_token(tok);
1106 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1117 static int test_type(enum event_type type, enum event_type expect)
1119 if (type != expect) {
1120 do_warning("Error: expected type %d but read %d",
1127 static int test_type_token(enum event_type type, const char *token,
1128 enum event_type expect, const char *expect_tok)
1130 if (type != expect) {
1131 do_warning("Error: expected type %d but read %d",
1136 if (strcmp(token, expect_tok) != 0) {
1137 do_warning("Error: expected '%s' but read '%s'",
1144 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1146 enum event_type type;
1149 type = read_token(tok);
1151 type = read_token_item(tok);
1152 return test_type(type, expect);
1155 static int read_expect_type(enum event_type expect, char **tok)
1157 return __read_expect_type(expect, tok, 1);
1160 static int __read_expected(enum event_type expect, const char *str,
1163 enum event_type type;
1168 type = read_token(&token);
1170 type = read_token_item(&token);
1172 ret = test_type_token(type, token, expect, str);
1179 static int read_expected(enum event_type expect, const char *str)
1181 return __read_expected(expect, str, 1);
1184 static int read_expected_item(enum event_type expect, const char *str)
1186 return __read_expected(expect, str, 0);
1189 static char *event_read_name(void)
1193 if (read_expected(EVENT_ITEM, "name") < 0)
1196 if (read_expected(EVENT_OP, ":") < 0)
1199 if (read_expect_type(EVENT_ITEM, &token) < 0)
1209 static int event_read_id(void)
1214 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1217 if (read_expected(EVENT_OP, ":") < 0)
1220 if (read_expect_type(EVENT_ITEM, &token) < 0)
1223 id = strtoul(token, NULL, 0);
1232 static int field_is_string(struct format_field *field)
1234 if ((field->flags & FIELD_IS_ARRAY) &&
1235 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1236 strstr(field->type, "s8")))
1242 static int field_is_dynamic(struct format_field *field)
1244 if (strncmp(field->type, "__data_loc", 10) == 0)
1250 static int field_is_long(struct format_field *field)
1252 /* includes long long */
1253 if (strstr(field->type, "long"))
1259 static unsigned int type_size(const char *name)
1261 /* This covers all FIELD_IS_STRING types. */
1279 for (i = 0; table[i].type; i++) {
1280 if (!strcmp(table[i].type, name))
1281 return table[i].size;
1287 static int event_read_fields(struct event_format *event, struct format_field **fields)
1289 struct format_field *field = NULL;
1290 enum event_type type;
1296 unsigned int size_dynamic = 0;
1298 type = read_token(&token);
1299 if (type == EVENT_NEWLINE) {
1306 if (test_type_token(type, token, EVENT_ITEM, "field"))
1310 type = read_token(&token);
1312 * The ftrace fields may still use the "special" name.
1315 if (event->flags & EVENT_FL_ISFTRACE &&
1316 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1318 type = read_token(&token);
1321 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1325 if (read_expect_type(EVENT_ITEM, &token) < 0)
1330 field = calloc(1, sizeof(*field));
1334 field->event = event;
1336 /* read the rest of the type */
1338 type = read_token(&token);
1339 if (type == EVENT_ITEM ||
1340 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1342 * Some of the ftrace fields are broken and have
1343 * an illegal "." in them.
1345 (event->flags & EVENT_FL_ISFTRACE &&
1346 type == EVENT_OP && strcmp(token, ".") == 0)) {
1348 if (strcmp(token, "*") == 0)
1349 field->flags |= FIELD_IS_POINTER;
1353 new_type = realloc(field->type,
1354 strlen(field->type) +
1355 strlen(last_token) + 2);
1360 field->type = new_type;
1361 strcat(field->type, " ");
1362 strcat(field->type, last_token);
1365 field->type = last_token;
1374 do_warning_event(event, "%s: no type found", __func__);
1377 field->name = last_token;
1379 if (test_type(type, EVENT_OP))
1382 if (strcmp(token, "[") == 0) {
1383 enum event_type last_type = type;
1384 char *brackets = token;
1388 field->flags |= FIELD_IS_ARRAY;
1390 type = read_token(&token);
1392 if (type == EVENT_ITEM)
1393 field->arraylen = strtoul(token, NULL, 0);
1395 field->arraylen = 0;
1397 while (strcmp(token, "]") != 0) {
1398 if (last_type == EVENT_ITEM &&
1405 new_brackets = realloc(brackets,
1407 strlen(token) + len);
1408 if (!new_brackets) {
1412 brackets = new_brackets;
1414 strcat(brackets, " ");
1415 strcat(brackets, token);
1416 /* We only care about the last token */
1417 field->arraylen = strtoul(token, NULL, 0);
1419 type = read_token(&token);
1420 if (type == EVENT_NONE) {
1421 do_warning_event(event, "failed to find token");
1428 new_brackets = realloc(brackets, strlen(brackets) + 2);
1429 if (!new_brackets) {
1433 brackets = new_brackets;
1434 strcat(brackets, "]");
1436 /* add brackets to type */
1438 type = read_token(&token);
1440 * If the next token is not an OP, then it is of
1441 * the format: type [] item;
1443 if (type == EVENT_ITEM) {
1445 new_type = realloc(field->type,
1446 strlen(field->type) +
1447 strlen(field->name) +
1448 strlen(brackets) + 2);
1453 field->type = new_type;
1454 strcat(field->type, " ");
1455 strcat(field->type, field->name);
1456 size_dynamic = type_size(field->name);
1457 free_token(field->name);
1458 strcat(field->type, brackets);
1459 field->name = token;
1460 type = read_token(&token);
1463 new_type = realloc(field->type,
1464 strlen(field->type) +
1465 strlen(brackets) + 1);
1470 field->type = new_type;
1471 strcat(field->type, brackets);
1476 if (field_is_string(field))
1477 field->flags |= FIELD_IS_STRING;
1478 if (field_is_dynamic(field))
1479 field->flags |= FIELD_IS_DYNAMIC;
1480 if (field_is_long(field))
1481 field->flags |= FIELD_IS_LONG;
1483 if (test_type_token(type, token, EVENT_OP, ";"))
1487 if (read_expected(EVENT_ITEM, "offset") < 0)
1490 if (read_expected(EVENT_OP, ":") < 0)
1493 if (read_expect_type(EVENT_ITEM, &token))
1495 field->offset = strtoul(token, NULL, 0);
1498 if (read_expected(EVENT_OP, ";") < 0)
1501 if (read_expected(EVENT_ITEM, "size") < 0)
1504 if (read_expected(EVENT_OP, ":") < 0)
1507 if (read_expect_type(EVENT_ITEM, &token))
1509 field->size = strtoul(token, NULL, 0);
1512 if (read_expected(EVENT_OP, ";") < 0)
1515 type = read_token(&token);
1516 if (type != EVENT_NEWLINE) {
1517 /* newer versions of the kernel have a "signed" type */
1518 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1523 if (read_expected(EVENT_OP, ":") < 0)
1526 if (read_expect_type(EVENT_ITEM, &token))
1529 if (strtoul(token, NULL, 0))
1530 field->flags |= FIELD_IS_SIGNED;
1533 if (read_expected(EVENT_OP, ";") < 0)
1536 if (read_expect_type(EVENT_NEWLINE, &token))
1542 if (field->flags & FIELD_IS_ARRAY) {
1543 if (field->arraylen)
1544 field->elementsize = field->size / field->arraylen;
1545 else if (field->flags & FIELD_IS_DYNAMIC)
1546 field->elementsize = size_dynamic;
1547 else if (field->flags & FIELD_IS_STRING)
1548 field->elementsize = 1;
1549 else if (field->flags & FIELD_IS_LONG)
1550 field->elementsize = event->pevent ?
1551 event->pevent->long_size :
1554 field->elementsize = field->size;
1557 fields = &field->next;
1574 static int event_read_format(struct event_format *event)
1579 if (read_expected_item(EVENT_ITEM, "format") < 0)
1582 if (read_expected(EVENT_OP, ":") < 0)
1585 if (read_expect_type(EVENT_NEWLINE, &token))
1589 ret = event_read_fields(event, &event->format.common_fields);
1592 event->format.nr_common = ret;
1594 ret = event_read_fields(event, &event->format.fields);
1597 event->format.nr_fields = ret;
1606 static enum event_type
1607 process_arg_token(struct event_format *event, struct print_arg *arg,
1608 char **tok, enum event_type type);
1610 static enum event_type
1611 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1613 enum event_type type;
1616 type = read_token(&token);
1619 return process_arg_token(event, arg, tok, type);
1622 static enum event_type
1623 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1626 * For __print_symbolic() and __print_flags, we need to completely
1627 * evaluate the first argument, which defines what to print next.
1629 static enum event_type
1630 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1632 enum event_type type;
1634 type = process_arg(event, arg, tok);
1636 while (type == EVENT_OP) {
1637 type = process_op(event, arg, tok);
1643 static enum event_type
1644 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1646 struct print_arg *arg, *left, *right;
1647 enum event_type type;
1652 right = alloc_arg();
1654 if (!arg || !left || !right) {
1655 do_warning_event(event, "%s: not enough memory!", __func__);
1656 /* arg will be freed at out_free */
1662 arg->type = PRINT_OP;
1663 arg->op.left = left;
1664 arg->op.right = right;
1667 type = process_arg(event, left, &token);
1670 /* Handle other operations in the arguments */
1671 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1672 type = process_op(event, left, &token);
1676 if (test_type_token(type, token, EVENT_OP, ":"))
1681 type = process_arg(event, right, &token);
1683 top->op.right = arg;
1689 /* Top may point to itself */
1690 top->op.right = NULL;
1696 static enum event_type
1697 process_array(struct event_format *event, struct print_arg *top, char **tok)
1699 struct print_arg *arg;
1700 enum event_type type;
1705 do_warning_event(event, "%s: not enough memory!", __func__);
1706 /* '*tok' is set to top->op.op. No need to free. */
1712 type = process_arg(event, arg, &token);
1713 if (test_type_token(type, token, EVENT_OP, "]"))
1716 top->op.right = arg;
1719 type = read_token_item(&token);
1730 static int get_op_prio(char *op)
1744 /* '>>' and '<<' are 8 */
1748 /* '==' and '!=' are 10 */
1758 do_warning("unknown op '%c'", op[0]);
1762 if (strcmp(op, "++") == 0 ||
1763 strcmp(op, "--") == 0) {
1765 } else if (strcmp(op, ">>") == 0 ||
1766 strcmp(op, "<<") == 0) {
1768 } else if (strcmp(op, ">=") == 0 ||
1769 strcmp(op, "<=") == 0) {
1771 } else if (strcmp(op, "==") == 0 ||
1772 strcmp(op, "!=") == 0) {
1774 } else if (strcmp(op, "&&") == 0) {
1776 } else if (strcmp(op, "||") == 0) {
1779 do_warning("unknown op '%s'", op);
1785 static int set_op_prio(struct print_arg *arg)
1788 /* single ops are the greatest */
1789 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1792 arg->op.prio = get_op_prio(arg->op.op);
1794 return arg->op.prio;
1797 /* Note, *tok does not get freed, but will most likely be saved */
1798 static enum event_type
1799 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1801 struct print_arg *left, *right = NULL;
1802 enum event_type type;
1805 /* the op is passed in via tok */
1808 if (arg->type == PRINT_OP && !arg->op.left) {
1809 /* handle single op */
1811 do_warning_event(event, "bad op token %s", token);
1821 do_warning_event(event, "bad op token %s", token);
1826 /* make an empty left */
1831 left->type = PRINT_NULL;
1832 arg->op.left = left;
1834 right = alloc_arg();
1838 arg->op.right = right;
1840 /* do not free the token, it belongs to an op */
1842 type = process_arg(event, right, tok);
1844 } else if (strcmp(token, "?") == 0) {
1850 /* copy the top arg to the left */
1853 arg->type = PRINT_OP;
1855 arg->op.left = left;
1858 /* it will set arg->op.right */
1859 type = process_cond(event, arg, tok);
1861 } else if (strcmp(token, ">>") == 0 ||
1862 strcmp(token, "<<") == 0 ||
1863 strcmp(token, "&") == 0 ||
1864 strcmp(token, "|") == 0 ||
1865 strcmp(token, "&&") == 0 ||
1866 strcmp(token, "||") == 0 ||
1867 strcmp(token, "-") == 0 ||
1868 strcmp(token, "+") == 0 ||
1869 strcmp(token, "*") == 0 ||
1870 strcmp(token, "^") == 0 ||
1871 strcmp(token, "/") == 0 ||
1872 strcmp(token, "<") == 0 ||
1873 strcmp(token, ">") == 0 ||
1874 strcmp(token, "<=") == 0 ||
1875 strcmp(token, ">=") == 0 ||
1876 strcmp(token, "==") == 0 ||
1877 strcmp(token, "!=") == 0) {
1883 /* copy the top arg to the left */
1886 arg->type = PRINT_OP;
1888 arg->op.left = left;
1889 arg->op.right = NULL;
1891 if (set_op_prio(arg) == -1) {
1892 event->flags |= EVENT_FL_FAILED;
1893 /* arg->op.op (= token) will be freed at out_free */
1898 type = read_token_item(&token);
1901 /* could just be a type pointer */
1902 if ((strcmp(arg->op.op, "*") == 0) &&
1903 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1906 if (left->type != PRINT_ATOM) {
1907 do_warning_event(event, "bad pointer type");
1910 new_atom = realloc(left->atom.atom,
1911 strlen(left->atom.atom) + 3);
1915 left->atom.atom = new_atom;
1916 strcat(left->atom.atom, " *");
1924 right = alloc_arg();
1928 type = process_arg_token(event, right, tok, type);
1929 arg->op.right = right;
1931 } else if (strcmp(token, "[") == 0) {
1939 arg->type = PRINT_OP;
1941 arg->op.left = left;
1945 /* it will set arg->op.right */
1946 type = process_array(event, arg, tok);
1949 do_warning_event(event, "unknown op '%s'", token);
1950 event->flags |= EVENT_FL_FAILED;
1951 /* the arg is now the left side */
1955 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1958 /* higher prios need to be closer to the root */
1959 prio = get_op_prio(*tok);
1961 if (prio > arg->op.prio)
1962 return process_op(event, arg, tok);
1964 return process_op(event, right, tok);
1970 do_warning_event(event, "%s: not enough memory!", __func__);
1977 static enum event_type
1978 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1981 enum event_type type;
1985 if (read_expected(EVENT_OP, "->") < 0)
1988 if (read_expect_type(EVENT_ITEM, &token) < 0)
1992 arg->type = PRINT_FIELD;
1993 arg->field.name = field;
1995 if (is_flag_field) {
1996 arg->field.field = pevent_find_any_field(event, arg->field.name);
1997 arg->field.field->flags |= FIELD_IS_FLAG;
1999 } else if (is_symbolic_field) {
2000 arg->field.field = pevent_find_any_field(event, arg->field.name);
2001 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2002 is_symbolic_field = 0;
2005 type = read_token(&token);
2017 static char *arg_eval (struct print_arg *arg);
2019 static unsigned long long
2020 eval_type_str(unsigned long long val, const char *type, int pointer)
2030 if (type[len-1] != '*') {
2031 do_warning("pointer expected with non pointer type");
2037 do_warning("%s: not enough memory!", __func__);
2040 memcpy(ref, type, len);
2042 /* chop off the " *" */
2045 val = eval_type_str(val, ref, 0);
2050 /* check if this is a pointer */
2051 if (type[len - 1] == '*')
2054 /* Try to figure out the arg size*/
2055 if (strncmp(type, "struct", 6) == 0)
2059 if (strcmp(type, "u8") == 0)
2062 if (strcmp(type, "u16") == 0)
2063 return val & 0xffff;
2065 if (strcmp(type, "u32") == 0)
2066 return val & 0xffffffff;
2068 if (strcmp(type, "u64") == 0 ||
2069 strcmp(type, "s64"))
2072 if (strcmp(type, "s8") == 0)
2073 return (unsigned long long)(char)val & 0xff;
2075 if (strcmp(type, "s16") == 0)
2076 return (unsigned long long)(short)val & 0xffff;
2078 if (strcmp(type, "s32") == 0)
2079 return (unsigned long long)(int)val & 0xffffffff;
2081 if (strncmp(type, "unsigned ", 9) == 0) {
2086 if (strcmp(type, "char") == 0) {
2088 return (unsigned long long)(char)val & 0xff;
2093 if (strcmp(type, "short") == 0) {
2095 return (unsigned long long)(short)val & 0xffff;
2097 return val & 0xffff;
2100 if (strcmp(type, "int") == 0) {
2102 return (unsigned long long)(int)val & 0xffffffff;
2104 return val & 0xffffffff;
2111 * Try to figure out the type.
2113 static unsigned long long
2114 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2116 if (arg->type != PRINT_TYPE) {
2117 do_warning("expected type argument");
2121 return eval_type_str(val, arg->typecast.type, pointer);
2124 static int arg_num_eval(struct print_arg *arg, long long *val)
2126 long long left, right;
2129 switch (arg->type) {
2131 *val = strtoll(arg->atom.atom, NULL, 0);
2134 ret = arg_num_eval(arg->typecast.item, val);
2137 *val = eval_type(*val, arg, 0);
2140 switch (arg->op.op[0]) {
2142 ret = arg_num_eval(arg->op.left, &left);
2145 ret = arg_num_eval(arg->op.right, &right);
2149 *val = left || right;
2151 *val = left | right;
2154 ret = arg_num_eval(arg->op.left, &left);
2157 ret = arg_num_eval(arg->op.right, &right);
2161 *val = left && right;
2163 *val = left & right;
2166 ret = arg_num_eval(arg->op.left, &left);
2169 ret = arg_num_eval(arg->op.right, &right);
2172 switch (arg->op.op[1]) {
2174 *val = left < right;
2177 *val = left << right;
2180 *val = left <= right;
2183 do_warning("unknown op '%s'", arg->op.op);
2188 ret = arg_num_eval(arg->op.left, &left);
2191 ret = arg_num_eval(arg->op.right, &right);
2194 switch (arg->op.op[1]) {
2196 *val = left > right;
2199 *val = left >> right;
2202 *val = left >= right;
2205 do_warning("unknown op '%s'", arg->op.op);
2210 ret = arg_num_eval(arg->op.left, &left);
2213 ret = arg_num_eval(arg->op.right, &right);
2217 if (arg->op.op[1] != '=') {
2218 do_warning("unknown op '%s'", arg->op.op);
2221 *val = left == right;
2224 ret = arg_num_eval(arg->op.left, &left);
2227 ret = arg_num_eval(arg->op.right, &right);
2231 switch (arg->op.op[1]) {
2233 *val = left != right;
2236 do_warning("unknown op '%s'", arg->op.op);
2241 /* check for negative */
2242 if (arg->op.left->type == PRINT_NULL)
2245 ret = arg_num_eval(arg->op.left, &left);
2248 ret = arg_num_eval(arg->op.right, &right);
2251 *val = left - right;
2254 if (arg->op.left->type == PRINT_NULL)
2257 ret = arg_num_eval(arg->op.left, &left);
2260 ret = arg_num_eval(arg->op.right, &right);
2263 *val = left + right;
2266 do_warning("unknown op '%s'", arg->op.op);
2272 case PRINT_FIELD ... PRINT_SYMBOL:
2277 do_warning("invalid eval type %d", arg->type);
2284 static char *arg_eval (struct print_arg *arg)
2287 static char buf[20];
2289 switch (arg->type) {
2291 return arg->atom.atom;
2293 return arg_eval(arg->typecast.item);
2295 if (!arg_num_eval(arg, &val))
2297 sprintf(buf, "%lld", val);
2301 case PRINT_FIELD ... PRINT_SYMBOL:
2306 do_warning("invalid eval type %d", arg->type);
2313 static enum event_type
2314 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2316 enum event_type type;
2317 struct print_arg *arg = NULL;
2318 struct print_flag_sym *field;
2324 type = read_token_item(&token);
2325 if (test_type_token(type, token, EVENT_OP, "{"))
2333 type = process_arg(event, arg, &token);
2335 if (type == EVENT_OP)
2336 type = process_op(event, arg, &token);
2338 if (type == EVENT_ERROR)
2341 if (test_type_token(type, token, EVENT_DELIM, ","))
2344 field = calloc(1, sizeof(*field));
2348 value = arg_eval(arg);
2350 goto out_free_field;
2351 field->value = strdup(value);
2352 if (field->value == NULL)
2353 goto out_free_field;
2361 type = process_arg(event, arg, &token);
2362 if (test_type_token(type, token, EVENT_OP, "}"))
2363 goto out_free_field;
2365 value = arg_eval(arg);
2367 goto out_free_field;
2368 field->str = strdup(value);
2369 if (field->str == NULL)
2370 goto out_free_field;
2375 list = &field->next;
2378 type = read_token_item(&token);
2379 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2385 free_flag_sym(field);
2394 static enum event_type
2395 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2397 struct print_arg *field;
2398 enum event_type type;
2401 memset(arg, 0, sizeof(*arg));
2402 arg->type = PRINT_FLAGS;
2404 field = alloc_arg();
2406 do_warning_event(event, "%s: not enough memory!", __func__);
2410 type = process_field_arg(event, field, &token);
2412 /* Handle operations in the first argument */
2413 while (type == EVENT_OP)
2414 type = process_op(event, field, &token);
2416 if (test_type_token(type, token, EVENT_DELIM, ","))
2417 goto out_free_field;
2420 arg->flags.field = field;
2422 type = read_token_item(&token);
2423 if (event_item_type(type)) {
2424 arg->flags.delim = token;
2425 type = read_token_item(&token);
2428 if (test_type_token(type, token, EVENT_DELIM, ","))
2431 type = process_fields(event, &arg->flags.flags, &token);
2432 if (test_type_token(type, token, EVENT_DELIM, ")"))
2436 type = read_token_item(tok);
2447 static enum event_type
2448 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2450 struct print_arg *field;
2451 enum event_type type;
2454 memset(arg, 0, sizeof(*arg));
2455 arg->type = PRINT_SYMBOL;
2457 field = alloc_arg();
2459 do_warning_event(event, "%s: not enough memory!", __func__);
2463 type = process_field_arg(event, field, &token);
2465 if (test_type_token(type, token, EVENT_DELIM, ","))
2466 goto out_free_field;
2468 arg->symbol.field = field;
2470 type = process_fields(event, &arg->symbol.symbols, &token);
2471 if (test_type_token(type, token, EVENT_DELIM, ")"))
2475 type = read_token_item(tok);
2486 static enum event_type
2487 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2489 struct print_arg *field;
2490 enum event_type type;
2493 memset(arg, 0, sizeof(*arg));
2494 arg->type = PRINT_HEX;
2496 field = alloc_arg();
2498 do_warning_event(event, "%s: not enough memory!", __func__);
2502 type = process_arg(event, field, &token);
2504 if (test_type_token(type, token, EVENT_DELIM, ","))
2507 arg->hex.field = field;
2511 field = alloc_arg();
2513 do_warning_event(event, "%s: not enough memory!", __func__);
2518 type = process_arg(event, field, &token);
2520 if (test_type_token(type, token, EVENT_DELIM, ")"))
2523 arg->hex.size = field;
2526 type = read_token_item(tok);
2536 static enum event_type
2537 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2539 struct format_field *field;
2540 enum event_type type;
2543 memset(arg, 0, sizeof(*arg));
2544 arg->type = PRINT_DYNAMIC_ARRAY;
2547 * The item within the parenthesis is another field that holds
2548 * the index into where the array starts.
2550 type = read_token(&token);
2552 if (type != EVENT_ITEM)
2555 /* Find the field */
2557 field = pevent_find_field(event, token);
2561 arg->dynarray.field = field;
2562 arg->dynarray.index = 0;
2564 if (read_expected(EVENT_DELIM, ")") < 0)
2568 type = read_token_item(&token);
2570 if (type != EVENT_OP || strcmp(token, "[") != 0)
2576 do_warning_event(event, "%s: not enough memory!", __func__);
2581 type = process_arg(event, arg, &token);
2582 if (type == EVENT_ERROR)
2585 if (!test_type_token(type, token, EVENT_OP, "]"))
2589 type = read_token_item(tok);
2600 static enum event_type
2601 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2603 struct print_arg *item_arg;
2604 enum event_type type;
2607 type = process_arg(event, arg, &token);
2609 if (type == EVENT_ERROR)
2612 if (type == EVENT_OP)
2613 type = process_op(event, arg, &token);
2615 if (type == EVENT_ERROR)
2618 if (test_type_token(type, token, EVENT_DELIM, ")"))
2622 type = read_token_item(&token);
2625 * If the next token is an item or another open paren, then
2626 * this was a typecast.
2628 if (event_item_type(type) ||
2629 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2631 /* make this a typecast and contine */
2633 /* prevous must be an atom */
2634 if (arg->type != PRINT_ATOM) {
2635 do_warning_event(event, "previous needed to be PRINT_ATOM");
2639 item_arg = alloc_arg();
2641 do_warning_event(event, "%s: not enough memory!",
2646 arg->type = PRINT_TYPE;
2647 arg->typecast.type = arg->atom.atom;
2648 arg->typecast.item = item_arg;
2649 type = process_arg_token(event, item_arg, &token, type);
2663 static enum event_type
2664 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2667 enum event_type type;
2670 if (read_expect_type(EVENT_ITEM, &token) < 0)
2673 arg->type = PRINT_STRING;
2674 arg->string.string = token;
2675 arg->string.offset = -1;
2677 if (read_expected(EVENT_DELIM, ")") < 0)
2680 type = read_token(&token);
2692 static enum event_type
2693 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2696 enum event_type type;
2699 if (read_expect_type(EVENT_ITEM, &token) < 0)
2702 arg->type = PRINT_BITMASK;
2703 arg->bitmask.bitmask = token;
2704 arg->bitmask.offset = -1;
2706 if (read_expected(EVENT_DELIM, ")") < 0)
2709 type = read_token(&token);
2721 static struct pevent_function_handler *
2722 find_func_handler(struct pevent *pevent, char *func_name)
2724 struct pevent_function_handler *func;
2729 for (func = pevent->func_handlers; func; func = func->next) {
2730 if (strcmp(func->name, func_name) == 0)
2737 static void remove_func_handler(struct pevent *pevent, char *func_name)
2739 struct pevent_function_handler *func;
2740 struct pevent_function_handler **next;
2742 next = &pevent->func_handlers;
2743 while ((func = *next)) {
2744 if (strcmp(func->name, func_name) == 0) {
2746 free_func_handle(func);
2753 static enum event_type
2754 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2755 struct print_arg *arg, char **tok)
2757 struct print_arg **next_arg;
2758 struct print_arg *farg;
2759 enum event_type type;
2763 arg->type = PRINT_FUNC;
2764 arg->func.func = func;
2768 next_arg = &(arg->func.args);
2769 for (i = 0; i < func->nr_args; i++) {
2772 do_warning_event(event, "%s: not enough memory!",
2777 type = process_arg(event, farg, &token);
2778 if (i < (func->nr_args - 1)) {
2779 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2780 do_warning_event(event,
2781 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2782 func->name, func->nr_args,
2783 event->name, i + 1);
2787 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2788 do_warning_event(event,
2789 "Error: function '%s()' only expects %d arguments but event %s has more",
2790 func->name, func->nr_args, event->name);
2796 next_arg = &(farg->next);
2800 type = read_token(&token);
2811 static enum event_type
2812 process_function(struct event_format *event, struct print_arg *arg,
2813 char *token, char **tok)
2815 struct pevent_function_handler *func;
2817 if (strcmp(token, "__print_flags") == 0) {
2820 return process_flags(event, arg, tok);
2822 if (strcmp(token, "__print_symbolic") == 0) {
2824 is_symbolic_field = 1;
2825 return process_symbols(event, arg, tok);
2827 if (strcmp(token, "__print_hex") == 0) {
2829 return process_hex(event, arg, tok);
2831 if (strcmp(token, "__get_str") == 0) {
2833 return process_str(event, arg, tok);
2835 if (strcmp(token, "__get_bitmask") == 0) {
2837 return process_bitmask(event, arg, tok);
2839 if (strcmp(token, "__get_dynamic_array") == 0) {
2841 return process_dynamic_array(event, arg, tok);
2844 func = find_func_handler(event->pevent, token);
2847 return process_func_handler(event, func, arg, tok);
2850 do_warning_event(event, "function %s not defined", token);
2855 static enum event_type
2856 process_arg_token(struct event_format *event, struct print_arg *arg,
2857 char **tok, enum event_type type)
2866 if (strcmp(token, "REC") == 0) {
2868 type = process_entry(event, arg, &token);
2872 /* test the next token */
2873 type = read_token_item(&token);
2876 * If the next token is a parenthesis, then this
2879 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2882 /* this will free atom. */
2883 type = process_function(event, arg, atom, &token);
2886 /* atoms can be more than one token long */
2887 while (type == EVENT_ITEM) {
2889 new_atom = realloc(atom,
2890 strlen(atom) + strlen(token) + 2);
2899 strcat(atom, token);
2901 type = read_token_item(&token);
2904 arg->type = PRINT_ATOM;
2905 arg->atom.atom = atom;
2910 arg->type = PRINT_ATOM;
2911 arg->atom.atom = token;
2912 type = read_token_item(&token);
2915 if (strcmp(token, "(") == 0) {
2917 type = process_paren(event, arg, &token);
2921 /* handle single ops */
2922 arg->type = PRINT_OP;
2924 arg->op.left = NULL;
2925 type = process_op(event, arg, &token);
2927 /* On error, the op is freed */
2928 if (type == EVENT_ERROR)
2931 /* return error type if errored */
2934 case EVENT_ERROR ... EVENT_NEWLINE:
2936 do_warning_event(event, "unexpected type %d", type);
2944 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2946 enum event_type type = EVENT_ERROR;
2947 struct print_arg *arg;
2952 if (type == EVENT_NEWLINE) {
2953 type = read_token_item(&token);
2959 do_warning_event(event, "%s: not enough memory!",
2964 type = process_arg(event, arg, &token);
2966 if (type == EVENT_ERROR) {
2975 if (type == EVENT_OP) {
2976 type = process_op(event, arg, &token);
2978 if (type == EVENT_ERROR) {
2987 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2994 } while (type != EVENT_NONE);
2996 if (type != EVENT_NONE && type != EVENT_ERROR)
3002 static int event_read_print(struct event_format *event)
3004 enum event_type type;
3008 if (read_expected_item(EVENT_ITEM, "print") < 0)
3011 if (read_expected(EVENT_ITEM, "fmt") < 0)
3014 if (read_expected(EVENT_OP, ":") < 0)
3017 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3021 event->print_fmt.format = token;
3022 event->print_fmt.args = NULL;
3024 /* ok to have no arg */
3025 type = read_token_item(&token);
3027 if (type == EVENT_NONE)
3030 /* Handle concatenation of print lines */
3031 if (type == EVENT_DQUOTE) {
3034 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3037 free_token(event->print_fmt.format);
3038 event->print_fmt.format = NULL;
3043 if (test_type_token(type, token, EVENT_DELIM, ","))
3048 ret = event_read_print_args(event, &event->print_fmt.args);
3060 * pevent_find_common_field - return a common field by event
3061 * @event: handle for the event
3062 * @name: the name of the common field to return
3064 * Returns a common field from the event by the given @name.
3065 * This only searchs the common fields and not all field.
3067 struct format_field *
3068 pevent_find_common_field(struct event_format *event, const char *name)
3070 struct format_field *format;
3072 for (format = event->format.common_fields;
3073 format; format = format->next) {
3074 if (strcmp(format->name, name) == 0)
3082 * pevent_find_field - find a non-common field
3083 * @event: handle for the event
3084 * @name: the name of the non-common field
3086 * Returns a non-common field by the given @name.
3087 * This does not search common fields.
3089 struct format_field *
3090 pevent_find_field(struct event_format *event, const char *name)
3092 struct format_field *format;
3094 for (format = event->format.fields;
3095 format; format = format->next) {
3096 if (strcmp(format->name, name) == 0)
3104 * pevent_find_any_field - find any field by name
3105 * @event: handle for the event
3106 * @name: the name of the field
3108 * Returns a field by the given @name.
3109 * This searchs the common field names first, then
3110 * the non-common ones if a common one was not found.
3112 struct format_field *
3113 pevent_find_any_field(struct event_format *event, const char *name)
3115 struct format_field *format;
3117 format = pevent_find_common_field(event, name);
3120 return pevent_find_field(event, name);
3124 * pevent_read_number - read a number from data
3125 * @pevent: handle for the pevent
3126 * @ptr: the raw data
3127 * @size: the size of the data that holds the number
3129 * Returns the number (converted to host) from the
3132 unsigned long long pevent_read_number(struct pevent *pevent,
3133 const void *ptr, int size)
3137 return *(unsigned char *)ptr;
3139 return data2host2(pevent, ptr);
3141 return data2host4(pevent, ptr);
3143 return data2host8(pevent, ptr);
3151 * pevent_read_number_field - read a number from data
3152 * @field: a handle to the field
3153 * @data: the raw data to read
3154 * @value: the value to place the number in
3156 * Reads raw data according to a field offset and size,
3157 * and translates it into @value.
3159 * Returns 0 on success, -1 otherwise.
3161 int pevent_read_number_field(struct format_field *field, const void *data,
3162 unsigned long long *value)
3166 switch (field->size) {
3171 *value = pevent_read_number(field->event->pevent,
3172 data + field->offset, field->size);
3179 static int get_common_info(struct pevent *pevent,
3180 const char *type, int *offset, int *size)
3182 struct event_format *event;
3183 struct format_field *field;
3186 * All events should have the same common elements.
3187 * Pick any event to find where the type is;
3189 if (!pevent->events) {
3190 do_warning("no event_list!");
3194 event = pevent->events[0];
3195 field = pevent_find_common_field(event, type);
3199 *offset = field->offset;
3200 *size = field->size;
3205 static int __parse_common(struct pevent *pevent, void *data,
3206 int *size, int *offset, const char *name)
3211 ret = get_common_info(pevent, name, offset, size);
3215 return pevent_read_number(pevent, data + *offset, *size);
3218 static int trace_parse_common_type(struct pevent *pevent, void *data)
3220 return __parse_common(pevent, data,
3221 &pevent->type_size, &pevent->type_offset,
3225 static int parse_common_pid(struct pevent *pevent, void *data)
3227 return __parse_common(pevent, data,
3228 &pevent->pid_size, &pevent->pid_offset,
3232 static int parse_common_pc(struct pevent *pevent, void *data)
3234 return __parse_common(pevent, data,
3235 &pevent->pc_size, &pevent->pc_offset,
3236 "common_preempt_count");
3239 static int parse_common_flags(struct pevent *pevent, void *data)
3241 return __parse_common(pevent, data,
3242 &pevent->flags_size, &pevent->flags_offset,
3246 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3248 return __parse_common(pevent, data,
3249 &pevent->ld_size, &pevent->ld_offset,
3250 "common_lock_depth");
3253 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3255 return __parse_common(pevent, data,
3256 &pevent->ld_size, &pevent->ld_offset,
3257 "common_migrate_disable");
3260 static int events_id_cmp(const void *a, const void *b);
3263 * pevent_find_event - find an event by given id
3264 * @pevent: a handle to the pevent
3265 * @id: the id of the event
3267 * Returns an event that has a given @id.
3269 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3271 struct event_format **eventptr;
3272 struct event_format key;
3273 struct event_format *pkey = &key;
3275 /* Check cache first */
3276 if (pevent->last_event && pevent->last_event->id == id)
3277 return pevent->last_event;
3281 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3282 sizeof(*pevent->events), events_id_cmp);
3285 pevent->last_event = *eventptr;
3293 * pevent_find_event_by_name - find an event by given name
3294 * @pevent: a handle to the pevent
3295 * @sys: the system name to search for
3296 * @name: the name of the event to search for
3298 * This returns an event with a given @name and under the system
3299 * @sys. If @sys is NULL the first event with @name is returned.
3301 struct event_format *
3302 pevent_find_event_by_name(struct pevent *pevent,
3303 const char *sys, const char *name)
3305 struct event_format *event;
3308 if (pevent->last_event &&
3309 strcmp(pevent->last_event->name, name) == 0 &&
3310 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3311 return pevent->last_event;
3313 for (i = 0; i < pevent->nr_events; i++) {
3314 event = pevent->events[i];
3315 if (strcmp(event->name, name) == 0) {
3318 if (strcmp(event->system, sys) == 0)
3322 if (i == pevent->nr_events)
3325 pevent->last_event = event;
3329 static unsigned long long
3330 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3332 struct pevent *pevent = event->pevent;
3333 unsigned long long val = 0;
3334 unsigned long long left, right;
3335 struct print_arg *typearg = NULL;
3336 struct print_arg *larg;
3337 unsigned long offset;
3338 unsigned int field_size;
3340 switch (arg->type) {
3345 return strtoull(arg->atom.atom, NULL, 0);
3347 if (!arg->field.field) {
3348 arg->field.field = pevent_find_any_field(event, arg->field.name);
3349 if (!arg->field.field)
3350 goto out_warning_field;
3353 /* must be a number */
3354 val = pevent_read_number(pevent, data + arg->field.field->offset,
3355 arg->field.field->size);
3362 val = eval_num_arg(data, size, event, arg->typecast.item);
3363 return eval_type(val, arg, 0);
3371 val = process_defined_func(&s, data, size, event, arg);
3372 trace_seq_destroy(&s);
3376 if (strcmp(arg->op.op, "[") == 0) {
3378 * Arrays are special, since we don't want
3379 * to read the arg as is.
3381 right = eval_num_arg(data, size, event, arg->op.right);
3383 /* handle typecasts */
3384 larg = arg->op.left;
3385 while (larg->type == PRINT_TYPE) {
3388 larg = larg->typecast.item;
3391 /* Default to long size */
3392 field_size = pevent->long_size;
3394 switch (larg->type) {
3395 case PRINT_DYNAMIC_ARRAY:
3396 offset = pevent_read_number(pevent,
3397 data + larg->dynarray.field->offset,
3398 larg->dynarray.field->size);
3399 if (larg->dynarray.field->elementsize)
3400 field_size = larg->dynarray.field->elementsize;
3402 * The actual length of the dynamic array is stored
3403 * in the top half of the field, and the offset
3404 * is in the bottom half of the 32 bit field.
3410 if (!larg->field.field) {
3412 pevent_find_any_field(event, larg->field.name);
3413 if (!larg->field.field) {
3415 goto out_warning_field;
3418 field_size = larg->field.field->elementsize;
3419 offset = larg->field.field->offset +
3420 right * larg->field.field->elementsize;
3423 goto default_op; /* oops, all bets off */
3425 val = pevent_read_number(pevent,
3426 data + offset, field_size);
3428 val = eval_type(val, typearg, 1);
3430 } else if (strcmp(arg->op.op, "?") == 0) {
3431 left = eval_num_arg(data, size, event, arg->op.left);
3432 arg = arg->op.right;
3434 val = eval_num_arg(data, size, event, arg->op.left);
3436 val = eval_num_arg(data, size, event, arg->op.right);
3440 left = eval_num_arg(data, size, event, arg->op.left);
3441 right = eval_num_arg(data, size, event, arg->op.right);
3442 switch (arg->op.op[0]) {
3444 switch (arg->op.op[1]) {
3449 val = left != right;
3452 goto out_warning_op;
3460 val = left || right;
3466 val = left && right;
3471 switch (arg->op.op[1]) {
3476 val = left << right;
3479 val = left <= right;
3482 goto out_warning_op;
3486 switch (arg->op.op[1]) {
3491 val = left >> right;
3494 val = left >= right;
3497 goto out_warning_op;
3501 if (arg->op.op[1] != '=')
3502 goto out_warning_op;
3504 val = left == right;
3519 goto out_warning_op;
3522 case PRINT_DYNAMIC_ARRAY:
3523 /* Without [], we pass the address to the dynamic data */
3524 offset = pevent_read_number(pevent,
3525 data + arg->dynarray.field->offset,
3526 arg->dynarray.field->size);
3528 * The actual length of the dynamic array is stored
3529 * in the top half of the field, and the offset
3530 * is in the bottom half of the 32 bit field.
3533 val = (unsigned long long)((unsigned long)data + offset);
3535 default: /* not sure what to do there */
3541 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3545 do_warning_event(event, "%s: field %s not found",
3546 __func__, arg->field.name);
3552 unsigned long long value;
3555 static const struct flag flags[] = {
3556 { "HI_SOFTIRQ", 0 },
3557 { "TIMER_SOFTIRQ", 1 },
3558 { "NET_TX_SOFTIRQ", 2 },
3559 { "NET_RX_SOFTIRQ", 3 },
3560 { "BLOCK_SOFTIRQ", 4 },
3561 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3562 { "TASKLET_SOFTIRQ", 6 },
3563 { "SCHED_SOFTIRQ", 7 },
3564 { "HRTIMER_SOFTIRQ", 8 },
3565 { "RCU_SOFTIRQ", 9 },
3567 { "HRTIMER_NORESTART", 0 },
3568 { "HRTIMER_RESTART", 1 },
3571 static unsigned long long eval_flag(const char *flag)
3576 * Some flags in the format files do not get converted.
3577 * If the flag is not numeric, see if it is something that
3578 * we already know about.
3580 if (isdigit(flag[0]))
3581 return strtoull(flag, NULL, 0);
3583 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3584 if (strcmp(flags[i].name, flag) == 0)
3585 return flags[i].value;
3590 static void print_str_to_seq(struct trace_seq *s, const char *format,
3591 int len_arg, const char *str)
3594 trace_seq_printf(s, format, len_arg, str);
3596 trace_seq_printf(s, format, str);
3599 static void print_bitmask_to_seq(struct pevent *pevent,
3600 struct trace_seq *s, const char *format,
3601 int len_arg, const void *data, int size)
3603 int nr_bits = size * 8;
3604 int str_size = (nr_bits + 3) / 4;
3612 * The kernel likes to put in commas every 32 bits, we
3615 str_size += (nr_bits - 1) / 32;
3617 str = malloc(str_size + 1);
3619 do_warning("%s: not enough memory!", __func__);
3624 /* Start out with -2 for the two chars per byte */
3625 for (i = str_size - 2; i >= 0; i -= 2) {
3627 * data points to a bit mask of size bytes.
3628 * In the kernel, this is an array of long words, thus
3629 * endianess is very important.
3631 if (pevent->file_bigendian)
3632 index = size - (len + 1);
3636 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3637 memcpy(str + i, buf, 2);
3639 if (!(len & 3) && i > 0) {
3646 trace_seq_printf(s, format, len_arg, str);
3648 trace_seq_printf(s, format, str);
3653 static void print_str_arg(struct trace_seq *s, void *data, int size,
3654 struct event_format *event, const char *format,
3655 int len_arg, struct print_arg *arg)
3657 struct pevent *pevent = event->pevent;
3658 struct print_flag_sym *flag;
3659 struct format_field *field;
3660 struct printk_map *printk;
3661 unsigned long long val, fval;
3668 switch (arg->type) {
3673 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3676 field = arg->field.field;
3678 field = pevent_find_any_field(event, arg->field.name);
3680 str = arg->field.name;
3681 goto out_warning_field;
3683 arg->field.field = field;
3685 /* Zero sized fields, mean the rest of the data */
3686 len = field->size ? : size - field->offset;
3689 * Some events pass in pointers. If this is not an array
3690 * and the size is the same as long_size, assume that it
3693 if (!(field->flags & FIELD_IS_ARRAY) &&
3694 field->size == pevent->long_size) {
3695 addr = *(unsigned long *)(data + field->offset);
3696 /* Check if it matches a print format */
3697 printk = find_printk(pevent, addr);
3699 trace_seq_puts(s, printk->printk);
3701 trace_seq_printf(s, "%lx", addr);
3704 str = malloc(len + 1);
3706 do_warning_event(event, "%s: not enough memory!",
3710 memcpy(str, data + field->offset, len);
3712 print_str_to_seq(s, format, len_arg, str);
3716 val = eval_num_arg(data, size, event, arg->flags.field);
3718 for (flag = arg->flags.flags; flag; flag = flag->next) {
3719 fval = eval_flag(flag->value);
3720 if (!val && !fval) {
3721 print_str_to_seq(s, format, len_arg, flag->str);
3724 if (fval && (val & fval) == fval) {
3725 if (print && arg->flags.delim)
3726 trace_seq_puts(s, arg->flags.delim);
3727 print_str_to_seq(s, format, len_arg, flag->str);
3734 val = eval_num_arg(data, size, event, arg->symbol.field);
3735 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3736 fval = eval_flag(flag->value);
3738 print_str_to_seq(s, format, len_arg, flag->str);
3744 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3745 unsigned long offset;
3746 offset = pevent_read_number(pevent,
3747 data + arg->hex.field->dynarray.field->offset,
3748 arg->hex.field->dynarray.field->size);
3749 hex = data + (offset & 0xffff);
3751 field = arg->hex.field->field.field;
3753 str = arg->hex.field->field.name;
3754 field = pevent_find_any_field(event, str);
3756 goto out_warning_field;
3757 arg->hex.field->field.field = field;
3759 hex = data + field->offset;
3761 len = eval_num_arg(data, size, event, arg->hex.size);
3762 for (i = 0; i < len; i++) {
3764 trace_seq_putc(s, ' ');
3765 trace_seq_printf(s, "%02x", hex[i]);
3771 case PRINT_STRING: {
3774 if (arg->string.offset == -1) {
3775 struct format_field *f;
3777 f = pevent_find_any_field(event, arg->string.string);
3778 arg->string.offset = f->offset;
3780 str_offset = data2host4(pevent, data + arg->string.offset);
3781 str_offset &= 0xffff;
3782 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3786 print_str_to_seq(s, format, len_arg, arg->string.string);
3788 case PRINT_BITMASK: {
3792 if (arg->bitmask.offset == -1) {
3793 struct format_field *f;
3795 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3796 arg->bitmask.offset = f->offset;
3798 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3799 bitmask_size = bitmask_offset >> 16;
3800 bitmask_offset &= 0xffff;
3801 print_bitmask_to_seq(pevent, s, format, len_arg,
3802 data + bitmask_offset, bitmask_size);
3807 * The only op for string should be ? :
3809 if (arg->op.op[0] != '?')
3811 val = eval_num_arg(data, size, event, arg->op.left);
3813 print_str_arg(s, data, size, event,
3814 format, len_arg, arg->op.right->op.left);
3816 print_str_arg(s, data, size, event,
3817 format, len_arg, arg->op.right->op.right);
3820 process_defined_func(s, data, size, event, arg);
3830 do_warning_event(event, "%s: field %s not found",
3831 __func__, arg->field.name);
3834 static unsigned long long
3835 process_defined_func(struct trace_seq *s, void *data, int size,
3836 struct event_format *event, struct print_arg *arg)
3838 struct pevent_function_handler *func_handle = arg->func.func;
3839 struct pevent_func_params *param;
3840 unsigned long long *args;
3841 unsigned long long ret;
3842 struct print_arg *farg;
3843 struct trace_seq str;
3845 struct save_str *next;
3847 } *strings = NULL, *string;
3850 if (!func_handle->nr_args) {
3851 ret = (*func_handle->func)(s, NULL);
3855 farg = arg->func.args;
3856 param = func_handle->params;
3859 args = malloc(sizeof(*args) * func_handle->nr_args);
3863 for (i = 0; i < func_handle->nr_args; i++) {
3864 switch (param->type) {
3865 case PEVENT_FUNC_ARG_INT:
3866 case PEVENT_FUNC_ARG_LONG:
3867 case PEVENT_FUNC_ARG_PTR:
3868 args[i] = eval_num_arg(data, size, event, farg);
3870 case PEVENT_FUNC_ARG_STRING:
3871 trace_seq_init(&str);
3872 print_str_arg(&str, data, size, event, "%s", -1, farg);
3873 trace_seq_terminate(&str);
3874 string = malloc(sizeof(*string));
3876 do_warning_event(event, "%s(%d): malloc str",
3877 __func__, __LINE__);
3880 string->next = strings;
3881 string->str = strdup(str.buffer);
3884 do_warning_event(event, "%s(%d): malloc str",
3885 __func__, __LINE__);
3888 args[i] = (uintptr_t)string->str;
3890 trace_seq_destroy(&str);
3894 * Something went totally wrong, this is not
3895 * an input error, something in this code broke.
3897 do_warning_event(event, "Unexpected end of arguments\n");
3901 param = param->next;
3904 ret = (*func_handle->func)(s, args);
3909 strings = string->next;
3915 /* TBD : handle return type here */
3919 static void free_args(struct print_arg *args)
3921 struct print_arg *next;
3931 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3933 struct pevent *pevent = event->pevent;
3934 struct format_field *field, *ip_field;
3935 struct print_arg *args, *arg, **next;
3936 unsigned long long ip, val;
3941 field = pevent->bprint_buf_field;
3942 ip_field = pevent->bprint_ip_field;
3945 field = pevent_find_field(event, "buf");
3947 do_warning_event(event, "can't find buffer field for binary printk");
3950 ip_field = pevent_find_field(event, "ip");
3952 do_warning_event(event, "can't find ip field for binary printk");
3955 pevent->bprint_buf_field = field;
3956 pevent->bprint_ip_field = ip_field;
3959 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3962 * The first arg is the IP pointer.
3966 do_warning_event(event, "%s(%d): not enough memory!",
3967 __func__, __LINE__);
3974 arg->type = PRINT_ATOM;
3976 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3979 /* skip the first "%pf: " */
3980 for (ptr = fmt + 5, bptr = data + field->offset;
3981 bptr < data + size && *ptr; ptr++) {
4012 vsize = pevent->long_size;
4026 /* the pointers are always 4 bytes aligned */
4027 bptr = (void *)(((unsigned long)bptr + 3) &
4029 val = pevent_read_number(pevent, bptr, vsize);
4033 do_warning_event(event, "%s(%d): not enough memory!",
4034 __func__, __LINE__);
4038 arg->type = PRINT_ATOM;
4039 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4046 * The '*' case means that an arg is used as the length.
4047 * We need to continue to figure out for what.
4056 do_warning_event(event, "%s(%d): not enough memory!",
4057 __func__, __LINE__);
4061 arg->type = PRINT_BSTRING;
4062 arg->string.string = strdup(bptr);
4063 if (!arg->string.string)
4065 bptr += strlen(bptr) + 1;
4082 get_bprint_format(void *data, int size __maybe_unused,
4083 struct event_format *event)
4085 struct pevent *pevent = event->pevent;
4086 unsigned long long addr;
4087 struct format_field *field;
4088 struct printk_map *printk;
4091 field = pevent->bprint_fmt_field;
4094 field = pevent_find_field(event, "fmt");
4096 do_warning_event(event, "can't find format field for binary printk");
4099 pevent->bprint_fmt_field = field;
4102 addr = pevent_read_number(pevent, data + field->offset, field->size);
4104 printk = find_printk(pevent, addr);
4106 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4111 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4117 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4118 struct event_format *event, struct print_arg *arg)
4121 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4123 if (arg->type == PRINT_FUNC) {
4124 process_defined_func(s, data, size, event, arg);
4128 if (arg->type != PRINT_FIELD) {
4129 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4135 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4136 if (!arg->field.field) {
4138 pevent_find_any_field(event, arg->field.name);
4139 if (!arg->field.field) {
4140 do_warning_event(event, "%s: field %s not found",
4141 __func__, arg->field.name);
4145 if (arg->field.field->size != 6) {
4146 trace_seq_printf(s, "INVALIDMAC");
4149 buf = data + arg->field.field->offset;
4150 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4153 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4158 fmt = "%03d.%03d.%03d.%03d";
4160 fmt = "%d.%d.%d.%d";
4162 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4165 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4167 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4168 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4171 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4173 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4176 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4179 unsigned char zerolength[8];
4184 bool needcolon = false;
4186 struct in6_addr in6;
4188 memcpy(&in6, addr, sizeof(struct in6_addr));
4190 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4192 memset(zerolength, 0, sizeof(zerolength));
4199 /* find position of longest 0 run */
4200 for (i = 0; i < range; i++) {
4201 for (j = i; j < range; j++) {
4202 if (in6.s6_addr16[j] != 0)
4207 for (i = 0; i < range; i++) {
4208 if (zerolength[i] > longest) {
4209 longest = zerolength[i];
4213 if (longest == 1) /* don't compress a single 0 */
4217 for (i = 0; i < range; i++) {
4218 if (i == colonpos) {
4219 if (needcolon || i == 0)
4220 trace_seq_printf(s, ":");
4221 trace_seq_printf(s, ":");
4227 trace_seq_printf(s, ":");
4230 /* hex u16 without leading 0s */
4231 word = ntohs(in6.s6_addr16[i]);
4235 trace_seq_printf(s, "%x%02x", hi, lo);
4237 trace_seq_printf(s, "%x", lo);
4244 trace_seq_printf(s, ":");
4245 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4251 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4255 for (j = 0; j < 16; j += 2) {
4256 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4257 if (i == 'I' && j < 14)
4258 trace_seq_printf(s, ":");
4263 * %pi4 print an IPv4 address with leading zeros
4264 * %pI4 print an IPv4 address without leading zeros
4265 * %pi6 print an IPv6 address without colons
4266 * %pI6 print an IPv6 address with colons
4267 * %pI6c print an IPv6 address in compressed form with colons
4268 * %pISpc print an IP address based on sockaddr; p adds port.
4270 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4271 void *data, int size, struct event_format *event,
4272 struct print_arg *arg)
4276 if (arg->type == PRINT_FUNC) {
4277 process_defined_func(s, data, size, event, arg);
4281 if (arg->type != PRINT_FIELD) {
4282 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4286 if (!arg->field.field) {
4288 pevent_find_any_field(event, arg->field.name);
4289 if (!arg->field.field) {
4290 do_warning("%s: field %s not found",
4291 __func__, arg->field.name);
4296 buf = data + arg->field.field->offset;
4298 if (arg->field.field->size != 4) {
4299 trace_seq_printf(s, "INVALIDIPv4");
4302 print_ip4_addr(s, i, buf);
4307 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4308 void *data, int size, struct event_format *event,
4309 struct print_arg *arg)
4316 if (i == 'I' && *ptr == 'c') {
4322 if (arg->type == PRINT_FUNC) {
4323 process_defined_func(s, data, size, event, arg);
4327 if (arg->type != PRINT_FIELD) {
4328 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4332 if (!arg->field.field) {
4334 pevent_find_any_field(event, arg->field.name);
4335 if (!arg->field.field) {
4336 do_warning("%s: field %s not found",
4337 __func__, arg->field.name);
4342 buf = data + arg->field.field->offset;
4344 if (arg->field.field->size != 16) {
4345 trace_seq_printf(s, "INVALIDIPv6");
4350 print_ip6c_addr(s, buf);
4352 print_ip6_addr(s, i, buf);
4357 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4358 void *data, int size, struct event_format *event,
4359 struct print_arg *arg)
4361 char have_c = 0, have_p = 0;
4363 struct sockaddr_storage *sa;
4380 if (arg->type == PRINT_FUNC) {
4381 process_defined_func(s, data, size, event, arg);
4385 if (arg->type != PRINT_FIELD) {
4386 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4390 if (!arg->field.field) {
4392 pevent_find_any_field(event, arg->field.name);
4393 if (!arg->field.field) {
4394 do_warning("%s: field %s not found",
4395 __func__, arg->field.name);
4400 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4402 if (sa->ss_family == AF_INET) {
4403 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4405 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4406 trace_seq_printf(s, "INVALIDIPv4");
4410 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4412 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4415 } else if (sa->ss_family == AF_INET6) {
4416 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4418 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4419 trace_seq_printf(s, "INVALIDIPv6");
4424 trace_seq_printf(s, "[");
4426 buf = (unsigned char *) &sa6->sin6_addr;
4428 print_ip6c_addr(s, buf);
4430 print_ip6_addr(s, i, buf);
4433 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4439 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4440 void *data, int size, struct event_format *event,
4441 struct print_arg *arg)
4443 char i = *ptr; /* 'i' or 'I' */
4456 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4459 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4462 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4471 static int is_printable_array(char *p, unsigned int len)
4475 for (i = 0; i < len && p[i]; i++)
4476 if (!isprint(p[i]) && !isspace(p[i]))
4481 static void print_event_fields(struct trace_seq *s, void *data,
4482 int size __maybe_unused,
4483 struct event_format *event)
4485 struct format_field *field;
4486 unsigned long long val;
4487 unsigned int offset, len, i;
4489 field = event->format.fields;
4491 trace_seq_printf(s, " %s=", field->name);
4492 if (field->flags & FIELD_IS_ARRAY) {
4493 offset = field->offset;
4495 if (field->flags & FIELD_IS_DYNAMIC) {
4496 val = pevent_read_number(event->pevent, data + offset, len);
4501 if (field->flags & FIELD_IS_STRING &&
4502 is_printable_array(data + offset, len)) {
4503 trace_seq_printf(s, "%s", (char *)data + offset);
4505 trace_seq_puts(s, "ARRAY[");
4506 for (i = 0; i < len; i++) {
4508 trace_seq_puts(s, ", ");
4509 trace_seq_printf(s, "%02x",
4510 *((unsigned char *)data + offset + i));
4512 trace_seq_putc(s, ']');
4513 field->flags &= ~FIELD_IS_STRING;
4516 val = pevent_read_number(event->pevent, data + field->offset,
4518 if (field->flags & FIELD_IS_POINTER) {
4519 trace_seq_printf(s, "0x%llx", val);
4520 } else if (field->flags & FIELD_IS_SIGNED) {
4521 switch (field->size) {
4524 * If field is long then print it in hex.
4525 * A long usually stores pointers.
4527 if (field->flags & FIELD_IS_LONG)
4528 trace_seq_printf(s, "0x%x", (int)val);
4530 trace_seq_printf(s, "%d", (int)val);
4533 trace_seq_printf(s, "%2d", (short)val);
4536 trace_seq_printf(s, "%1d", (char)val);
4539 trace_seq_printf(s, "%lld", val);
4542 if (field->flags & FIELD_IS_LONG)
4543 trace_seq_printf(s, "0x%llx", val);
4545 trace_seq_printf(s, "%llu", val);
4548 field = field->next;
4552 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4554 struct pevent *pevent = event->pevent;
4555 struct print_fmt *print_fmt = &event->print_fmt;
4556 struct print_arg *arg = print_fmt->args;
4557 struct print_arg *args = NULL;
4558 const char *ptr = print_fmt->format;
4559 unsigned long long val;
4560 struct func_map *func;
4561 const char *saveptr;
4563 char *bprint_fmt = NULL;
4571 if (event->flags & EVENT_FL_FAILED) {
4572 trace_seq_printf(s, "[FAILED TO PARSE]");
4573 print_event_fields(s, data, size, event);
4577 if (event->flags & EVENT_FL_ISBPRINT) {
4578 bprint_fmt = get_bprint_format(data, size, event);
4579 args = make_bprint_args(bprint_fmt, data, size, event);
4584 for (; *ptr; ptr++) {
4590 trace_seq_putc(s, '\n');
4593 trace_seq_putc(s, '\t');
4596 trace_seq_putc(s, '\r');
4599 trace_seq_putc(s, '\\');
4602 trace_seq_putc(s, *ptr);
4606 } else if (*ptr == '%') {
4614 trace_seq_putc(s, '%');
4617 /* FIXME: need to handle properly */
4629 /* The argument is the length. */
4631 do_warning_event(event, "no argument match");
4632 event->flags |= EVENT_FL_FAILED;
4635 len_arg = eval_num_arg(data, size, event, arg);
4645 if (pevent->long_size == 4)
4650 if (*(ptr+1) == 'F' ||
4654 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4655 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4659 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4662 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4677 do_warning_event(event, "no argument match");
4678 event->flags |= EVENT_FL_FAILED;
4682 len = ((unsigned long)ptr + 1) -
4683 (unsigned long)saveptr;
4685 /* should never happen */
4687 do_warning_event(event, "bad format!");
4688 event->flags |= EVENT_FL_FAILED;
4692 memcpy(format, saveptr, len);
4695 val = eval_num_arg(data, size, event, arg);
4699 func = find_func(pevent, val);
4701 trace_seq_puts(s, func->func);
4702 if (show_func == 'F')
4709 if (pevent->long_size == 8 && ls &&
4710 sizeof(long) != 8) {
4714 /* make %l into %ll */
4715 p = strchr(format, 'l');
4717 memmove(p+1, p, strlen(p)+1);
4718 else if (strcmp(format, "%p") == 0)
4719 strcpy(format, "0x%llx");
4724 trace_seq_printf(s, format, len_arg, (char)val);
4726 trace_seq_printf(s, format, (char)val);
4730 trace_seq_printf(s, format, len_arg, (short)val);
4732 trace_seq_printf(s, format, (short)val);
4736 trace_seq_printf(s, format, len_arg, (int)val);
4738 trace_seq_printf(s, format, (int)val);
4742 trace_seq_printf(s, format, len_arg, (long)val);
4744 trace_seq_printf(s, format, (long)val);
4748 trace_seq_printf(s, format, len_arg,
4751 trace_seq_printf(s, format, (long long)val);
4754 do_warning_event(event, "bad count (%d)", ls);
4755 event->flags |= EVENT_FL_FAILED;
4760 do_warning_event(event, "no matching argument");
4761 event->flags |= EVENT_FL_FAILED;
4765 len = ((unsigned long)ptr + 1) -
4766 (unsigned long)saveptr;
4768 /* should never happen */
4770 do_warning_event(event, "bad format!");
4771 event->flags |= EVENT_FL_FAILED;
4775 memcpy(format, saveptr, len);
4779 /* Use helper trace_seq */
4781 print_str_arg(&p, data, size, event,
4782 format, len_arg, arg);
4783 trace_seq_terminate(&p);
4784 trace_seq_puts(s, p.buffer);
4785 trace_seq_destroy(&p);
4789 trace_seq_printf(s, ">%c<", *ptr);
4793 trace_seq_putc(s, *ptr);
4796 if (event->flags & EVENT_FL_FAILED) {
4798 trace_seq_printf(s, "[FAILED TO PARSE]");
4808 * pevent_data_lat_fmt - parse the data for the latency format
4809 * @pevent: a handle to the pevent
4810 * @s: the trace_seq to write to
4811 * @record: the record to read from
4813 * This parses out the Latency format (interrupts disabled,
4814 * need rescheduling, in hard/soft interrupt, preempt count
4815 * and lock depth) and places it into the trace_seq.
4817 void pevent_data_lat_fmt(struct pevent *pevent,
4818 struct trace_seq *s, struct pevent_record *record)
4820 static int check_lock_depth = 1;
4821 static int check_migrate_disable = 1;
4822 static int lock_depth_exists;
4823 static int migrate_disable_exists;
4824 unsigned int lat_flags;
4827 int migrate_disable;
4830 void *data = record->data;
4832 lat_flags = parse_common_flags(pevent, data);
4833 pc = parse_common_pc(pevent, data);
4834 /* lock_depth may not always exist */
4835 if (lock_depth_exists)
4836 lock_depth = parse_common_lock_depth(pevent, data);
4837 else if (check_lock_depth) {
4838 lock_depth = parse_common_lock_depth(pevent, data);
4840 check_lock_depth = 0;
4842 lock_depth_exists = 1;
4845 /* migrate_disable may not always exist */
4846 if (migrate_disable_exists)
4847 migrate_disable = parse_common_migrate_disable(pevent, data);
4848 else if (check_migrate_disable) {
4849 migrate_disable = parse_common_migrate_disable(pevent, data);
4850 if (migrate_disable < 0)
4851 check_migrate_disable = 0;
4853 migrate_disable_exists = 1;
4856 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4857 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4859 trace_seq_printf(s, "%c%c%c",
4860 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4861 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4863 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4865 (hardirq && softirq) ? 'H' :
4866 hardirq ? 'h' : softirq ? 's' : '.');
4869 trace_seq_printf(s, "%x", pc);
4871 trace_seq_putc(s, '.');
4873 if (migrate_disable_exists) {
4874 if (migrate_disable < 0)
4875 trace_seq_putc(s, '.');
4877 trace_seq_printf(s, "%d", migrate_disable);
4880 if (lock_depth_exists) {
4882 trace_seq_putc(s, '.');
4884 trace_seq_printf(s, "%d", lock_depth);
4887 trace_seq_terminate(s);
4891 * pevent_data_type - parse out the given event type
4892 * @pevent: a handle to the pevent
4893 * @rec: the record to read from
4895 * This returns the event id from the @rec.
4897 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4899 return trace_parse_common_type(pevent, rec->data);
4903 * pevent_data_event_from_type - find the event by a given type
4904 * @pevent: a handle to the pevent
4905 * @type: the type of the event.
4907 * This returns the event form a given @type;
4909 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4911 return pevent_find_event(pevent, type);
4915 * pevent_data_pid - parse the PID from raw data
4916 * @pevent: a handle to the pevent
4917 * @rec: the record to parse
4919 * This returns the PID from a raw data.
4921 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4923 return parse_common_pid(pevent, rec->data);
4927 * pevent_data_comm_from_pid - return the command line from PID
4928 * @pevent: a handle to the pevent
4929 * @pid: the PID of the task to search for
4931 * This returns a pointer to the command line that has the given
4934 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4938 comm = find_cmdline(pevent, pid);
4943 * pevent_data_comm_from_pid - parse the data into the print format
4944 * @s: the trace_seq to write to
4945 * @event: the handle to the event
4946 * @record: the record to read from
4948 * This parses the raw @data using the given @event information and
4949 * writes the print format into the trace_seq.
4951 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4952 struct pevent_record *record)
4954 int print_pretty = 1;
4956 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
4957 print_event_fields(s, record->data, record->size, event);
4960 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
4961 print_pretty = event->handler(s, record, event,
4965 pretty_print(s, record->data, record->size, event);
4968 trace_seq_terminate(s);
4971 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4973 if (!use_trace_clock)
4976 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4977 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4980 /* trace_clock is setting in tsc or counter mode */
4984 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4985 struct pevent_record *record, bool use_trace_clock)
4987 static const char *spaces = " "; /* 20 spaces */
4988 struct event_format *event;
4990 unsigned long usecs;
4991 unsigned long nsecs;
4993 void *data = record->data;
4998 bool use_usec_format;
5000 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5002 if (use_usec_format) {
5003 secs = record->ts / NSECS_PER_SEC;
5004 nsecs = record->ts - secs * NSECS_PER_SEC;
5007 if (record->size < 0) {
5008 do_warning("ug! negative record size %d", record->size);
5012 type = trace_parse_common_type(pevent, data);
5014 event = pevent_find_event(pevent, type);
5016 do_warning("ug! no event found for type %d", type);
5020 pid = parse_common_pid(pevent, data);
5021 comm = find_cmdline(pevent, pid);
5023 if (pevent->latency_format) {
5024 trace_seq_printf(s, "%8.8s-%-5d %3d",
5025 comm, pid, record->cpu);
5026 pevent_data_lat_fmt(pevent, s, record);
5028 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5030 if (use_usec_format) {
5031 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5035 usecs = (nsecs + 500) / NSECS_PER_USEC;
5039 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5040 secs, p, usecs, event->name);
5042 trace_seq_printf(s, " %12llu: %s: ",
5043 record->ts, event->name);
5045 /* Space out the event names evenly. */
5046 len = strlen(event->name);
5048 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5050 pevent_event_info(s, event, record);
5053 static int events_id_cmp(const void *a, const void *b)
5055 struct event_format * const * ea = a;
5056 struct event_format * const * eb = b;
5058 if ((*ea)->id < (*eb)->id)
5061 if ((*ea)->id > (*eb)->id)
5067 static int events_name_cmp(const void *a, const void *b)
5069 struct event_format * const * ea = a;
5070 struct event_format * const * eb = b;
5073 res = strcmp((*ea)->name, (*eb)->name);
5077 res = strcmp((*ea)->system, (*eb)->system);
5081 return events_id_cmp(a, b);
5084 static int events_system_cmp(const void *a, const void *b)
5086 struct event_format * const * ea = a;
5087 struct event_format * const * eb = b;
5090 res = strcmp((*ea)->system, (*eb)->system);
5094 res = strcmp((*ea)->name, (*eb)->name);
5098 return events_id_cmp(a, b);
5101 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5103 struct event_format **events;
5104 int (*sort)(const void *a, const void *b);
5106 events = pevent->sort_events;
5108 if (events && pevent->last_type == sort_type)
5112 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5116 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5117 events[pevent->nr_events] = NULL;
5119 pevent->sort_events = events;
5121 /* the internal events are sorted by id */
5122 if (sort_type == EVENT_SORT_ID) {
5123 pevent->last_type = sort_type;
5128 switch (sort_type) {
5130 sort = events_id_cmp;
5132 case EVENT_SORT_NAME:
5133 sort = events_name_cmp;
5135 case EVENT_SORT_SYSTEM:
5136 sort = events_system_cmp;
5142 qsort(events, pevent->nr_events, sizeof(*events), sort);
5143 pevent->last_type = sort_type;
5148 static struct format_field **
5149 get_event_fields(const char *type, const char *name,
5150 int count, struct format_field *list)
5152 struct format_field **fields;
5153 struct format_field *field;
5156 fields = malloc(sizeof(*fields) * (count + 1));
5160 for (field = list; field; field = field->next) {
5161 fields[i++] = field;
5162 if (i == count + 1) {
5163 do_warning("event %s has more %s fields than specified",
5171 do_warning("event %s has less %s fields than specified",
5180 * pevent_event_common_fields - return a list of common fields for an event
5181 * @event: the event to return the common fields of.
5183 * Returns an allocated array of fields. The last item in the array is NULL.
5184 * The array must be freed with free().
5186 struct format_field **pevent_event_common_fields(struct event_format *event)
5188 return get_event_fields("common", event->name,
5189 event->format.nr_common,
5190 event->format.common_fields);
5194 * pevent_event_fields - return a list of event specific fields for an event
5195 * @event: the event to return the fields of.
5197 * Returns an allocated array of fields. The last item in the array is NULL.
5198 * The array must be freed with free().
5200 struct format_field **pevent_event_fields(struct event_format *event)
5202 return get_event_fields("event", event->name,
5203 event->format.nr_fields,
5204 event->format.fields);
5207 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5209 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5211 trace_seq_puts(s, ", ");
5212 print_fields(s, field->next);
5217 static void print_args(struct print_arg *args)
5219 int print_paren = 1;
5222 switch (args->type) {
5227 printf("%s", args->atom.atom);
5230 printf("REC->%s", args->field.name);
5233 printf("__print_flags(");
5234 print_args(args->flags.field);
5235 printf(", %s, ", args->flags.delim);
5237 print_fields(&s, args->flags.flags);
5238 trace_seq_do_printf(&s);
5239 trace_seq_destroy(&s);
5243 printf("__print_symbolic(");
5244 print_args(args->symbol.field);
5247 print_fields(&s, args->symbol.symbols);
5248 trace_seq_do_printf(&s);
5249 trace_seq_destroy(&s);
5253 printf("__print_hex(");
5254 print_args(args->hex.field);
5256 print_args(args->hex.size);
5261 printf("__get_str(%s)", args->string.string);
5264 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5267 printf("(%s)", args->typecast.type);
5268 print_args(args->typecast.item);
5271 if (strcmp(args->op.op, ":") == 0)
5275 print_args(args->op.left);
5276 printf(" %s ", args->op.op);
5277 print_args(args->op.right);
5282 /* we should warn... */
5287 print_args(args->next);
5291 static void parse_header_field(const char *field,
5292 int *offset, int *size, int mandatory)
5294 unsigned long long save_input_buf_ptr;
5295 unsigned long long save_input_buf_siz;
5299 save_input_buf_ptr = input_buf_ptr;
5300 save_input_buf_siz = input_buf_siz;
5302 if (read_expected(EVENT_ITEM, "field") < 0)
5304 if (read_expected(EVENT_OP, ":") < 0)
5308 if (read_expect_type(EVENT_ITEM, &token) < 0)
5313 * If this is not a mandatory field, then test it first.
5316 if (read_expected(EVENT_ITEM, field) < 0)
5319 if (read_expect_type(EVENT_ITEM, &token) < 0)
5321 if (strcmp(token, field) != 0)
5326 if (read_expected(EVENT_OP, ";") < 0)
5328 if (read_expected(EVENT_ITEM, "offset") < 0)
5330 if (read_expected(EVENT_OP, ":") < 0)
5332 if (read_expect_type(EVENT_ITEM, &token) < 0)
5334 *offset = atoi(token);
5336 if (read_expected(EVENT_OP, ";") < 0)
5338 if (read_expected(EVENT_ITEM, "size") < 0)
5340 if (read_expected(EVENT_OP, ":") < 0)
5342 if (read_expect_type(EVENT_ITEM, &token) < 0)
5344 *size = atoi(token);
5346 if (read_expected(EVENT_OP, ";") < 0)
5348 type = read_token(&token);
5349 if (type != EVENT_NEWLINE) {
5350 /* newer versions of the kernel have a "signed" type */
5351 if (type != EVENT_ITEM)
5354 if (strcmp(token, "signed") != 0)
5359 if (read_expected(EVENT_OP, ":") < 0)
5362 if (read_expect_type(EVENT_ITEM, &token))
5366 if (read_expected(EVENT_OP, ";") < 0)
5369 if (read_expect_type(EVENT_NEWLINE, &token))
5377 input_buf_ptr = save_input_buf_ptr;
5378 input_buf_siz = save_input_buf_siz;
5385 * pevent_parse_header_page - parse the data stored in the header page
5386 * @pevent: the handle to the pevent
5387 * @buf: the buffer storing the header page format string
5388 * @size: the size of @buf
5389 * @long_size: the long size to use if there is no header
5391 * This parses the header page format for information on the
5392 * ring buffer used. The @buf should be copied from
5394 * /sys/kernel/debug/tracing/events/header_page
5396 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5403 * Old kernels did not have header page info.
5404 * Sorry but we just use what we find here in user space.
5406 pevent->header_page_ts_size = sizeof(long long);
5407 pevent->header_page_size_size = long_size;
5408 pevent->header_page_data_offset = sizeof(long long) + long_size;
5409 pevent->old_format = 1;
5412 init_input_buf(buf, size);
5414 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5415 &pevent->header_page_ts_size, 1);
5416 parse_header_field("commit", &pevent->header_page_size_offset,
5417 &pevent->header_page_size_size, 1);
5418 parse_header_field("overwrite", &pevent->header_page_overwrite,
5420 parse_header_field("data", &pevent->header_page_data_offset,
5421 &pevent->header_page_data_size, 1);
5426 static int event_matches(struct event_format *event,
5427 int id, const char *sys_name,
5428 const char *event_name)
5430 if (id >= 0 && id != event->id)
5433 if (event_name && (strcmp(event_name, event->name) != 0))
5436 if (sys_name && (strcmp(sys_name, event->system) != 0))
5442 static void free_handler(struct event_handler *handle)
5444 free((void *)handle->sys_name);
5445 free((void *)handle->event_name);
5449 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5451 struct event_handler *handle, **next;
5453 for (next = &pevent->handlers; *next;
5454 next = &(*next)->next) {
5456 if (event_matches(event, handle->id,
5458 handle->event_name))
5465 pr_stat("overriding event (%d) %s:%s with new print handler",
5466 event->id, event->system, event->name);
5468 event->handler = handle->func;
5469 event->context = handle->context;
5471 *next = handle->next;
5472 free_handler(handle);
5478 * __pevent_parse_format - parse the event format
5479 * @buf: the buffer storing the event format string
5480 * @size: the size of @buf
5481 * @sys: the system the event belongs to
5483 * This parses the event format and creates an event structure
5484 * to quickly parse raw data for a given event.
5486 * These files currently come from:
5488 * /sys/kernel/debug/tracing/events/.../.../format
5490 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5491 struct pevent *pevent, const char *buf,
5492 unsigned long size, const char *sys)
5494 struct event_format *event;
5497 init_input_buf(buf, size);
5499 *eventp = event = alloc_event();
5501 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5503 event->name = event_read_name();
5506 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5507 goto event_alloc_failed;
5510 if (strcmp(sys, "ftrace") == 0) {
5511 event->flags |= EVENT_FL_ISFTRACE;
5513 if (strcmp(event->name, "bprint") == 0)
5514 event->flags |= EVENT_FL_ISBPRINT;
5517 event->id = event_read_id();
5518 if (event->id < 0) {
5519 ret = PEVENT_ERRNO__READ_ID_FAILED;
5521 * This isn't an allocation error actually.
5522 * But as the ID is critical, just bail out.
5524 goto event_alloc_failed;
5527 event->system = strdup(sys);
5528 if (!event->system) {
5529 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5530 goto event_alloc_failed;
5533 /* Add pevent to event so that it can be referenced */
5534 event->pevent = pevent;
5536 ret = event_read_format(event);
5538 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5539 goto event_parse_failed;
5543 * If the event has an override, don't print warnings if the event
5544 * print format fails to parse.
5546 if (pevent && find_event_handle(pevent, event))
5549 ret = event_read_print(event);
5553 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5554 goto event_parse_failed;
5557 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5558 struct format_field *field;
5559 struct print_arg *arg, **list;
5561 /* old ftrace had no args */
5562 list = &event->print_fmt.args;
5563 for (field = event->format.fields; field; field = field->next) {
5566 event->flags |= EVENT_FL_FAILED;
5567 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5569 arg->type = PRINT_FIELD;
5570 arg->field.name = strdup(field->name);
5571 if (!arg->field.name) {
5572 event->flags |= EVENT_FL_FAILED;
5574 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5576 arg->field.field = field;
5586 event->flags |= EVENT_FL_FAILED;
5590 free(event->system);
5597 static enum pevent_errno
5598 __pevent_parse_event(struct pevent *pevent,
5599 struct event_format **eventp,
5600 const char *buf, unsigned long size,
5603 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5604 struct event_format *event = *eventp;
5609 if (pevent && add_event(pevent, event)) {
5610 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5611 goto event_add_failed;
5614 #define PRINT_ARGS 0
5615 if (PRINT_ARGS && event->print_fmt.args)
5616 print_args(event->print_fmt.args);
5621 pevent_free_format(event);
5626 * pevent_parse_format - parse the event format
5627 * @pevent: the handle to the pevent
5628 * @eventp: returned format
5629 * @buf: the buffer storing the event format string
5630 * @size: the size of @buf
5631 * @sys: the system the event belongs to
5633 * This parses the event format and creates an event structure
5634 * to quickly parse raw data for a given event.
5636 * These files currently come from:
5638 * /sys/kernel/debug/tracing/events/.../.../format
5640 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5641 struct event_format **eventp,
5643 unsigned long size, const char *sys)
5645 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5649 * pevent_parse_event - parse the event format
5650 * @pevent: the handle to the pevent
5651 * @buf: the buffer storing the event format string
5652 * @size: the size of @buf
5653 * @sys: the system the event belongs to
5655 * This parses the event format and creates an event structure
5656 * to quickly parse raw data for a given event.
5658 * These files currently come from:
5660 * /sys/kernel/debug/tracing/events/.../.../format
5662 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5663 unsigned long size, const char *sys)
5665 struct event_format *event = NULL;
5666 return __pevent_parse_event(pevent, &event, buf, size, sys);
5670 #define _PE(code, str) str
5671 static const char * const pevent_error_str[] = {
5676 int pevent_strerror(struct pevent *pevent __maybe_unused,
5677 enum pevent_errno errnum, char *buf, size_t buflen)
5683 msg = strerror_r(errnum, buf, buflen);
5685 size_t len = strlen(msg);
5686 memcpy(buf, msg, min(buflen - 1, len));
5687 *(buf + min(buflen - 1, len)) = '\0';
5692 if (errnum <= __PEVENT_ERRNO__START ||
5693 errnum >= __PEVENT_ERRNO__END)
5696 idx = errnum - __PEVENT_ERRNO__START - 1;
5697 msg = pevent_error_str[idx];
5698 snprintf(buf, buflen, "%s", msg);
5703 int get_field_val(struct trace_seq *s, struct format_field *field,
5704 const char *name, struct pevent_record *record,
5705 unsigned long long *val, int err)
5709 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5713 if (pevent_read_number_field(field, record->data, val)) {
5715 trace_seq_printf(s, " %s=INVALID", name);
5723 * pevent_get_field_raw - return the raw pointer into the data field
5724 * @s: The seq to print to on error
5725 * @event: the event that the field is for
5726 * @name: The name of the field
5727 * @record: The record with the field name.
5728 * @len: place to store the field length.
5729 * @err: print default error if failed.
5731 * Returns a pointer into record->data of the field and places
5732 * the length of the field in @len.
5734 * On failure, it returns NULL.
5736 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5737 const char *name, struct pevent_record *record,
5740 struct format_field *field;
5741 void *data = record->data;
5748 field = pevent_find_field(event, name);
5752 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5756 /* Allow @len to be NULL */
5760 offset = field->offset;
5761 if (field->flags & FIELD_IS_DYNAMIC) {
5762 offset = pevent_read_number(event->pevent,
5763 data + offset, field->size);
5764 *len = offset >> 16;
5769 return data + offset;
5773 * pevent_get_field_val - find a field and return its value
5774 * @s: The seq to print to on error
5775 * @event: the event that the field is for
5776 * @name: The name of the field
5777 * @record: The record with the field name.
5778 * @val: place to store the value of the field.
5779 * @err: print default error if failed.
5781 * Returns 0 on success -1 on field not found.
5783 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5784 const char *name, struct pevent_record *record,
5785 unsigned long long *val, int err)
5787 struct format_field *field;
5792 field = pevent_find_field(event, name);
5794 return get_field_val(s, field, name, record, val, err);
5798 * pevent_get_common_field_val - find a common field and return its value
5799 * @s: The seq to print to on error
5800 * @event: the event that the field is for
5801 * @name: The name of the field
5802 * @record: The record with the field name.
5803 * @val: place to store the value of the field.
5804 * @err: print default error if failed.
5806 * Returns 0 on success -1 on field not found.
5808 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5809 const char *name, struct pevent_record *record,
5810 unsigned long long *val, int err)
5812 struct format_field *field;
5817 field = pevent_find_common_field(event, name);
5819 return get_field_val(s, field, name, record, val, err);
5823 * pevent_get_any_field_val - find a any field and return its value
5824 * @s: The seq to print to on error
5825 * @event: the event that the field is for
5826 * @name: The name of the field
5827 * @record: The record with the field name.
5828 * @val: place to store the value of the field.
5829 * @err: print default error if failed.
5831 * Returns 0 on success -1 on field not found.
5833 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5834 const char *name, struct pevent_record *record,
5835 unsigned long long *val, int err)
5837 struct format_field *field;
5842 field = pevent_find_any_field(event, name);
5844 return get_field_val(s, field, name, record, val, err);
5848 * pevent_print_num_field - print a field and a format
5849 * @s: The seq to print to
5850 * @fmt: The printf format to print the field with.
5851 * @event: the event that the field is for
5852 * @name: The name of the field
5853 * @record: The record with the field name.
5854 * @err: print default error if failed.
5856 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5858 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5859 struct event_format *event, const char *name,
5860 struct pevent_record *record, int err)
5862 struct format_field *field = pevent_find_field(event, name);
5863 unsigned long long val;
5868 if (pevent_read_number_field(field, record->data, &val))
5871 return trace_seq_printf(s, fmt, val);
5875 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5880 * pevent_print_func_field - print a field and a format for function pointers
5881 * @s: The seq to print to
5882 * @fmt: The printf format to print the field with.
5883 * @event: the event that the field is for
5884 * @name: The name of the field
5885 * @record: The record with the field name.
5886 * @err: print default error if failed.
5888 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5890 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5891 struct event_format *event, const char *name,
5892 struct pevent_record *record, int err)
5894 struct format_field *field = pevent_find_field(event, name);
5895 struct pevent *pevent = event->pevent;
5896 unsigned long long val;
5897 struct func_map *func;
5903 if (pevent_read_number_field(field, record->data, &val))
5906 func = find_func(pevent, val);
5909 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5911 sprintf(tmp, "0x%08llx", val);
5913 return trace_seq_printf(s, fmt, tmp);
5917 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5921 static void free_func_handle(struct pevent_function_handler *func)
5923 struct pevent_func_params *params;
5927 while (func->params) {
5928 params = func->params;
5929 func->params = params->next;
5937 * pevent_register_print_function - register a helper function
5938 * @pevent: the handle to the pevent
5939 * @func: the function to process the helper function
5940 * @ret_type: the return type of the helper function
5941 * @name: the name of the helper function
5942 * @parameters: A list of enum pevent_func_arg_type
5944 * Some events may have helper functions in the print format arguments.
5945 * This allows a plugin to dynamically create a way to process one
5946 * of these functions.
5948 * The @parameters is a variable list of pevent_func_arg_type enums that
5949 * must end with PEVENT_FUNC_ARG_VOID.
5951 int pevent_register_print_function(struct pevent *pevent,
5952 pevent_func_handler func,
5953 enum pevent_func_arg_type ret_type,
5956 struct pevent_function_handler *func_handle;
5957 struct pevent_func_params **next_param;
5958 struct pevent_func_params *param;
5959 enum pevent_func_arg_type type;
5963 func_handle = find_func_handler(pevent, name);
5966 * This is most like caused by the users own
5967 * plugins updating the function. This overrides the
5970 pr_stat("override of function helper '%s'", name);
5971 remove_func_handler(pevent, name);
5974 func_handle = calloc(1, sizeof(*func_handle));
5976 do_warning("Failed to allocate function handler");
5977 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5980 func_handle->ret_type = ret_type;
5981 func_handle->name = strdup(name);
5982 func_handle->func = func;
5983 if (!func_handle->name) {
5984 do_warning("Failed to allocate function name");
5986 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5989 next_param = &(func_handle->params);
5992 type = va_arg(ap, enum pevent_func_arg_type);
5993 if (type == PEVENT_FUNC_ARG_VOID)
5996 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5997 do_warning("Invalid argument type %d", type);
5998 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6002 param = malloc(sizeof(*param));
6004 do_warning("Failed to allocate function param");
6005 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6011 *next_param = param;
6012 next_param = &(param->next);
6014 func_handle->nr_args++;
6018 func_handle->next = pevent->func_handlers;
6019 pevent->func_handlers = func_handle;
6024 free_func_handle(func_handle);
6029 * pevent_unregister_print_function - unregister a helper function
6030 * @pevent: the handle to the pevent
6031 * @func: the function to process the helper function
6032 * @name: the name of the helper function
6034 * This function removes existing print handler for function @name.
6036 * Returns 0 if the handler was removed successully, -1 otherwise.
6038 int pevent_unregister_print_function(struct pevent *pevent,
6039 pevent_func_handler func, char *name)
6041 struct pevent_function_handler *func_handle;
6043 func_handle = find_func_handler(pevent, name);
6044 if (func_handle && func_handle->func == func) {
6045 remove_func_handler(pevent, name);
6051 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6052 const char *sys_name,
6053 const char *event_name)
6055 struct event_format *event;
6059 event = pevent_find_event(pevent, id);
6062 if (event_name && (strcmp(event_name, event->name) != 0))
6064 if (sys_name && (strcmp(sys_name, event->system) != 0))
6067 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6075 * pevent_register_event_handler - register a way to parse an event
6076 * @pevent: the handle to the pevent
6077 * @id: the id of the event to register
6078 * @sys_name: the system name the event belongs to
6079 * @event_name: the name of the event
6080 * @func: the function to call to parse the event information
6081 * @context: the data to be passed to @func
6083 * This function allows a developer to override the parsing of
6084 * a given event. If for some reason the default print format
6085 * is not sufficient, this function will register a function
6086 * for an event to be used to parse the data instead.
6088 * If @id is >= 0, then it is used to find the event.
6089 * else @sys_name and @event_name are used.
6091 int pevent_register_event_handler(struct pevent *pevent, int id,
6092 const char *sys_name, const char *event_name,
6093 pevent_event_handler_func func, void *context)
6095 struct event_format *event;
6096 struct event_handler *handle;
6098 event = pevent_search_event(pevent, id, sys_name, event_name);
6102 pr_stat("overriding event (%d) %s:%s with new print handler",
6103 event->id, event->system, event->name);
6105 event->handler = func;
6106 event->context = context;
6110 /* Save for later use. */
6111 handle = calloc(1, sizeof(*handle));
6113 do_warning("Failed to allocate event handler");
6114 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6119 handle->event_name = strdup(event_name);
6121 handle->sys_name = strdup(sys_name);
6123 if ((event_name && !handle->event_name) ||
6124 (sys_name && !handle->sys_name)) {
6125 do_warning("Failed to allocate event/sys name");
6126 free((void *)handle->event_name);
6127 free((void *)handle->sys_name);
6129 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6132 handle->func = func;
6133 handle->next = pevent->handlers;
6134 pevent->handlers = handle;
6135 handle->context = context;
6140 static int handle_matches(struct event_handler *handler, int id,
6141 const char *sys_name, const char *event_name,
6142 pevent_event_handler_func func, void *context)
6144 if (id >= 0 && id != handler->id)
6147 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6150 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6153 if (func != handler->func || context != handler->context)
6160 * pevent_unregister_event_handler - unregister an existing event handler
6161 * @pevent: the handle to the pevent
6162 * @id: the id of the event to unregister
6163 * @sys_name: the system name the handler belongs to
6164 * @event_name: the name of the event handler
6165 * @func: the function to call to parse the event information
6166 * @context: the data to be passed to @func
6168 * This function removes existing event handler (parser).
6170 * If @id is >= 0, then it is used to find the event.
6171 * else @sys_name and @event_name are used.
6173 * Returns 0 if handler was removed successfully, -1 if event was not found.
6175 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6176 const char *sys_name, const char *event_name,
6177 pevent_event_handler_func func, void *context)
6179 struct event_format *event;
6180 struct event_handler *handle;
6181 struct event_handler **next;
6183 event = pevent_search_event(pevent, id, sys_name, event_name);
6187 if (event->handler == func && event->context == context) {
6188 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6189 event->id, event->system, event->name);
6191 event->handler = NULL;
6192 event->context = NULL;
6197 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6199 if (handle_matches(handle, id, sys_name, event_name,
6207 *next = handle->next;
6208 free_handler(handle);
6214 * pevent_alloc - create a pevent handle
6216 struct pevent *pevent_alloc(void)
6218 struct pevent *pevent = calloc(1, sizeof(*pevent));
6221 pevent->ref_count = 1;
6226 void pevent_ref(struct pevent *pevent)
6228 pevent->ref_count++;
6231 static void free_format_fields(struct format_field *field)
6233 struct format_field *next;
6244 static void free_formats(struct format *format)
6246 free_format_fields(format->common_fields);
6247 free_format_fields(format->fields);
6250 void pevent_free_format(struct event_format *event)
6253 free(event->system);
6255 free_formats(&event->format);
6257 free(event->print_fmt.format);
6258 free_args(event->print_fmt.args);
6264 * pevent_free - free a pevent handle
6265 * @pevent: the pevent handle to free
6267 void pevent_free(struct pevent *pevent)
6269 struct cmdline_list *cmdlist, *cmdnext;
6270 struct func_list *funclist, *funcnext;
6271 struct printk_list *printklist, *printknext;
6272 struct pevent_function_handler *func_handler;
6273 struct event_handler *handle;
6279 cmdlist = pevent->cmdlist;
6280 funclist = pevent->funclist;
6281 printklist = pevent->printklist;
6283 pevent->ref_count--;
6284 if (pevent->ref_count)
6287 if (pevent->cmdlines) {
6288 for (i = 0; i < pevent->cmdline_count; i++)
6289 free(pevent->cmdlines[i].comm);
6290 free(pevent->cmdlines);
6294 cmdnext = cmdlist->next;
6295 free(cmdlist->comm);
6300 if (pevent->func_map) {
6301 for (i = 0; i < (int)pevent->func_count; i++) {
6302 free(pevent->func_map[i].func);
6303 free(pevent->func_map[i].mod);
6305 free(pevent->func_map);
6309 funcnext = funclist->next;
6310 free(funclist->func);
6311 free(funclist->mod);
6313 funclist = funcnext;
6316 while (pevent->func_handlers) {
6317 func_handler = pevent->func_handlers;
6318 pevent->func_handlers = func_handler->next;
6319 free_func_handle(func_handler);
6322 if (pevent->printk_map) {
6323 for (i = 0; i < (int)pevent->printk_count; i++)
6324 free(pevent->printk_map[i].printk);
6325 free(pevent->printk_map);
6328 while (printklist) {
6329 printknext = printklist->next;
6330 free(printklist->printk);
6332 printklist = printknext;
6335 for (i = 0; i < pevent->nr_events; i++)
6336 pevent_free_format(pevent->events[i]);
6338 while (pevent->handlers) {
6339 handle = pevent->handlers;
6340 pevent->handlers = handle->next;
6341 free_handler(handle);
6344 free(pevent->events);
6345 free(pevent->sort_events);
6350 void pevent_unref(struct pevent *pevent)
6352 pevent_free(pevent);