2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include <netinet/ip6.h>
36 #include "event-parse.h"
37 #include "event-utils.h"
39 static const char *input_buf;
40 static unsigned long long input_buf_ptr;
41 static unsigned long long input_buf_siz;
43 static int is_flag_field;
44 static int is_symbolic_field;
46 static int show_warning = 1;
48 #define do_warning(fmt, ...) \
51 warning(fmt, ##__VA_ARGS__); \
54 #define do_warning_event(event, fmt, ...) \
60 warning("[%s:%s] " fmt, event->system, \
61 event->name, ##__VA_ARGS__); \
63 warning(fmt, ##__VA_ARGS__); \
66 static void init_input_buf(const char *buf, unsigned long long size)
73 const char *pevent_get_input_buf(void)
78 unsigned long long pevent_get_input_buf_ptr(void)
83 struct event_handler {
84 struct event_handler *next;
87 const char *event_name;
88 pevent_event_handler_func func;
92 struct pevent_func_params {
93 struct pevent_func_params *next;
94 enum pevent_func_arg_type type;
97 struct pevent_function_handler {
98 struct pevent_function_handler *next;
99 enum pevent_func_arg_type ret_type;
101 pevent_func_handler func;
102 struct pevent_func_params *params;
106 static unsigned long long
107 process_defined_func(struct trace_seq *s, void *data, int size,
108 struct event_format *event, struct print_arg *arg);
110 static void free_func_handle(struct pevent_function_handler *func);
113 * pevent_buffer_init - init buffer for parsing
114 * @buf: buffer to parse
115 * @size: the size of the buffer
117 * For use with pevent_read_token(), this initializes the internal
118 * buffer that pevent_read_token() will parse.
120 void pevent_buffer_init(const char *buf, unsigned long long size)
122 init_input_buf(buf, size);
125 void breakpoint(void)
131 struct print_arg *alloc_arg(void)
133 return calloc(1, sizeof(struct print_arg));
141 static int cmdline_cmp(const void *a, const void *b)
143 const struct cmdline *ca = a;
144 const struct cmdline *cb = b;
146 if (ca->pid < cb->pid)
148 if (ca->pid > cb->pid)
154 struct cmdline_list {
155 struct cmdline_list *next;
160 static int cmdline_init(struct pevent *pevent)
162 struct cmdline_list *cmdlist = pevent->cmdlist;
163 struct cmdline_list *item;
164 struct cmdline *cmdlines;
167 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
173 cmdlines[i].pid = cmdlist->pid;
174 cmdlines[i].comm = cmdlist->comm;
177 cmdlist = cmdlist->next;
181 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
183 pevent->cmdlines = cmdlines;
184 pevent->cmdlist = NULL;
189 static const char *find_cmdline(struct pevent *pevent, int pid)
191 const struct cmdline *comm;
197 if (!pevent->cmdlines && cmdline_init(pevent))
198 return "<not enough memory for cmdlines!>";
202 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
203 sizeof(*pevent->cmdlines), cmdline_cmp);
211 * pevent_pid_is_registered - return if a pid has a cmdline registered
212 * @pevent: handle for the pevent
213 * @pid: The pid to check if it has a cmdline registered with.
215 * Returns 1 if the pid has a cmdline mapped to it
218 int pevent_pid_is_registered(struct pevent *pevent, int pid)
220 const struct cmdline *comm;
226 if (!pevent->cmdlines && cmdline_init(pevent))
231 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
232 sizeof(*pevent->cmdlines), cmdline_cmp);
240 * If the command lines have been converted to an array, then
241 * we must add this pid. This is much slower than when cmdlines
242 * are added before the array is initialized.
244 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
246 struct cmdline *cmdlines = pevent->cmdlines;
247 const struct cmdline *cmdline;
253 /* avoid duplicates */
256 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
257 sizeof(*pevent->cmdlines), cmdline_cmp);
263 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
269 cmdlines[pevent->cmdline_count].comm = strdup(comm);
270 if (!cmdlines[pevent->cmdline_count].comm) {
276 cmdlines[pevent->cmdline_count].pid = pid;
278 if (cmdlines[pevent->cmdline_count].comm)
279 pevent->cmdline_count++;
281 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
282 pevent->cmdlines = cmdlines;
288 * pevent_register_comm - register a pid / comm mapping
289 * @pevent: handle for the pevent
290 * @comm: the command line to register
291 * @pid: the pid to map the command line to
293 * This adds a mapping to search for command line names with
294 * a given pid. The comm is duplicated.
296 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
298 struct cmdline_list *item;
300 if (pevent->cmdlines)
301 return add_new_comm(pevent, comm, pid);
303 item = malloc(sizeof(*item));
308 item->comm = strdup(comm);
310 item->comm = strdup("<...>");
316 item->next = pevent->cmdlist;
318 pevent->cmdlist = item;
319 pevent->cmdline_count++;
324 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
326 pevent->trace_clock = strdup(trace_clock);
327 if (!pevent->trace_clock) {
335 unsigned long long addr;
341 struct func_list *next;
342 unsigned long long addr;
347 static int func_cmp(const void *a, const void *b)
349 const struct func_map *fa = a;
350 const struct func_map *fb = b;
352 if (fa->addr < fb->addr)
354 if (fa->addr > fb->addr)
361 * We are searching for a record in between, not an exact
364 static int func_bcmp(const void *a, const void *b)
366 const struct func_map *fa = a;
367 const struct func_map *fb = b;
369 if ((fa->addr == fb->addr) ||
371 (fa->addr > fb->addr &&
372 fa->addr < (fb+1)->addr))
375 if (fa->addr < fb->addr)
381 static int func_map_init(struct pevent *pevent)
383 struct func_list *funclist;
384 struct func_list *item;
385 struct func_map *func_map;
388 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
392 funclist = pevent->funclist;
396 func_map[i].func = funclist->func;
397 func_map[i].addr = funclist->addr;
398 func_map[i].mod = funclist->mod;
401 funclist = funclist->next;
405 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
408 * Add a special record at the end.
410 func_map[pevent->func_count].func = NULL;
411 func_map[pevent->func_count].addr = 0;
412 func_map[pevent->func_count].mod = NULL;
414 pevent->func_map = func_map;
415 pevent->funclist = NULL;
420 static struct func_map *
421 find_func(struct pevent *pevent, unsigned long long addr)
423 struct func_map *func;
426 if (!pevent->func_map)
427 func_map_init(pevent);
431 func = bsearch(&key, pevent->func_map, pevent->func_count,
432 sizeof(*pevent->func_map), func_bcmp);
438 * pevent_find_function - find a function by a given address
439 * @pevent: handle for the pevent
440 * @addr: the address to find the function with
442 * Returns a pointer to the function stored that has the given
443 * address. Note, the address does not have to be exact, it
444 * will select the function that would contain the address.
446 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
448 struct func_map *map;
450 map = find_func(pevent, addr);
458 * pevent_find_function_address - find a function address by a given address
459 * @pevent: handle for the pevent
460 * @addr: the address to find the function with
462 * Returns the address the function starts at. This can be used in
463 * conjunction with pevent_find_function to print both the function
464 * name and the function offset.
467 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
469 struct func_map *map;
471 map = find_func(pevent, addr);
479 * pevent_register_function - register a function with a given address
480 * @pevent: handle for the pevent
481 * @function: the function name to register
482 * @addr: the address the function starts at
483 * @mod: the kernel module the function may be in (NULL for none)
485 * This registers a function name with an address and module.
486 * The @func passed in is duplicated.
488 int pevent_register_function(struct pevent *pevent, char *func,
489 unsigned long long addr, char *mod)
491 struct func_list *item = malloc(sizeof(*item));
496 item->next = pevent->funclist;
497 item->func = strdup(func);
502 item->mod = strdup(mod);
509 pevent->funclist = item;
510 pevent->func_count++;
524 * pevent_print_funcs - print out the stored functions
525 * @pevent: handle for the pevent
527 * This prints out the stored functions.
529 void pevent_print_funcs(struct pevent *pevent)
533 if (!pevent->func_map)
534 func_map_init(pevent);
536 for (i = 0; i < (int)pevent->func_count; i++) {
538 pevent->func_map[i].addr,
539 pevent->func_map[i].func);
540 if (pevent->func_map[i].mod)
541 printf(" [%s]\n", pevent->func_map[i].mod);
548 unsigned long long addr;
553 struct printk_list *next;
554 unsigned long long addr;
558 static int printk_cmp(const void *a, const void *b)
560 const struct printk_map *pa = a;
561 const struct printk_map *pb = b;
563 if (pa->addr < pb->addr)
565 if (pa->addr > pb->addr)
571 static int printk_map_init(struct pevent *pevent)
573 struct printk_list *printklist;
574 struct printk_list *item;
575 struct printk_map *printk_map;
578 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
582 printklist = pevent->printklist;
586 printk_map[i].printk = printklist->printk;
587 printk_map[i].addr = printklist->addr;
590 printklist = printklist->next;
594 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
596 pevent->printk_map = printk_map;
597 pevent->printklist = NULL;
602 static struct printk_map *
603 find_printk(struct pevent *pevent, unsigned long long addr)
605 struct printk_map *printk;
606 struct printk_map key;
608 if (!pevent->printk_map && printk_map_init(pevent))
613 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
614 sizeof(*pevent->printk_map), printk_cmp);
620 * pevent_register_print_string - register a string by its address
621 * @pevent: handle for the pevent
622 * @fmt: the string format to register
623 * @addr: the address the string was located at
625 * This registers a string by the address it was stored in the kernel.
626 * The @fmt passed in is duplicated.
628 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
629 unsigned long long addr)
631 struct printk_list *item = malloc(sizeof(*item));
637 item->next = pevent->printklist;
640 /* Strip off quotes and '\n' from the end */
643 item->printk = strdup(fmt);
647 p = item->printk + strlen(item->printk) - 1;
652 if (strcmp(p, "\\n") == 0)
655 pevent->printklist = item;
656 pevent->printk_count++;
667 * pevent_print_printk - print out the stored strings
668 * @pevent: handle for the pevent
670 * This prints the string formats that were stored.
672 void pevent_print_printk(struct pevent *pevent)
676 if (!pevent->printk_map)
677 printk_map_init(pevent);
679 for (i = 0; i < (int)pevent->printk_count; i++) {
680 printf("%016llx %s\n",
681 pevent->printk_map[i].addr,
682 pevent->printk_map[i].printk);
686 static struct event_format *alloc_event(void)
688 return calloc(1, sizeof(struct event_format));
691 static int add_event(struct pevent *pevent, struct event_format *event)
694 struct event_format **events = realloc(pevent->events, sizeof(event) *
695 (pevent->nr_events + 1));
699 pevent->events = events;
701 for (i = 0; i < pevent->nr_events; i++) {
702 if (pevent->events[i]->id > event->id)
705 if (i < pevent->nr_events)
706 memmove(&pevent->events[i + 1],
708 sizeof(event) * (pevent->nr_events - i));
710 pevent->events[i] = event;
713 event->pevent = pevent;
718 static int event_item_type(enum event_type type)
721 case EVENT_ITEM ... EVENT_SQUOTE:
723 case EVENT_ERROR ... EVENT_DELIM:
729 static void free_flag_sym(struct print_flag_sym *fsym)
731 struct print_flag_sym *next;
742 static void free_arg(struct print_arg *arg)
744 struct print_arg *farg;
751 free(arg->atom.atom);
754 free(arg->field.name);
757 free_arg(arg->flags.field);
758 free(arg->flags.delim);
759 free_flag_sym(arg->flags.flags);
762 free_arg(arg->symbol.field);
763 free_flag_sym(arg->symbol.symbols);
766 free_arg(arg->hex.field);
767 free_arg(arg->hex.size);
769 case PRINT_INT_ARRAY:
770 free_arg(arg->int_array.field);
771 free_arg(arg->int_array.count);
772 free_arg(arg->int_array.el_size);
775 free(arg->typecast.type);
776 free_arg(arg->typecast.item);
780 free(arg->string.string);
783 free(arg->bitmask.bitmask);
785 case PRINT_DYNAMIC_ARRAY:
786 free(arg->dynarray.index);
790 free_arg(arg->op.left);
791 free_arg(arg->op.right);
794 while (arg->func.args) {
795 farg = arg->func.args;
796 arg->func.args = farg->next;
809 static enum event_type get_type(int ch)
812 return EVENT_NEWLINE;
815 if (isalnum(ch) || ch == '_')
823 if (ch == '(' || ch == ')' || ch == ',')
829 static int __read_char(void)
831 if (input_buf_ptr >= input_buf_siz)
834 return input_buf[input_buf_ptr++];
837 static int __peek_char(void)
839 if (input_buf_ptr >= input_buf_siz)
842 return input_buf[input_buf_ptr];
846 * pevent_peek_char - peek at the next character that will be read
848 * Returns the next character read, or -1 if end of buffer.
850 int pevent_peek_char(void)
852 return __peek_char();
855 static int extend_token(char **tok, char *buf, int size)
857 char *newtok = realloc(*tok, size);
874 static enum event_type force_token(const char *str, char **tok);
876 static enum event_type __read_token(char **tok)
879 int ch, last_ch, quote_ch, next_ch;
882 enum event_type type;
892 if (type == EVENT_NONE)
900 if (asprintf(tok, "%c", ch) < 0)
908 next_ch = __peek_char();
909 if (next_ch == '>') {
910 buf[i++] = __read_char();
923 buf[i++] = __read_char();
935 default: /* what should we do instead? */
945 buf[i++] = __read_char();
950 /* don't keep quotes */
956 if (i == (BUFSIZ - 1)) {
960 if (extend_token(tok, buf, tok_size) < 0)
967 /* the '\' '\' will cancel itself */
968 if (ch == '\\' && last_ch == '\\')
970 } while (ch != quote_ch || last_ch == '\\');
971 /* remove the last quote */
975 * For strings (double quotes) check the next token.
976 * If it is another string, concatinate the two.
978 if (type == EVENT_DQUOTE) {
979 unsigned long long save_input_buf_ptr = input_buf_ptr;
983 } while (isspace(ch));
986 input_buf_ptr = save_input_buf_ptr;
991 case EVENT_ERROR ... EVENT_SPACE:
997 while (get_type(__peek_char()) == type) {
998 if (i == (BUFSIZ - 1)) {
1002 if (extend_token(tok, buf, tok_size) < 0)
1012 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1015 if (type == EVENT_ITEM) {
1017 * Older versions of the kernel has a bug that
1018 * creates invalid symbols and will break the mac80211
1019 * parsing. This is a work around to that bug.
1021 * See Linux kernel commit:
1022 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1024 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1027 return force_token("\"\%s\" ", tok);
1028 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1031 return force_token("\" sta:%pM\" ", tok);
1032 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1035 return force_token("\" vif:%p(%d)\" ", tok);
1042 static enum event_type force_token(const char *str, char **tok)
1044 const char *save_input_buf;
1045 unsigned long long save_input_buf_ptr;
1046 unsigned long long save_input_buf_siz;
1047 enum event_type type;
1049 /* save off the current input pointers */
1050 save_input_buf = input_buf;
1051 save_input_buf_ptr = input_buf_ptr;
1052 save_input_buf_siz = input_buf_siz;
1054 init_input_buf(str, strlen(str));
1056 type = __read_token(tok);
1058 /* reset back to original token */
1059 input_buf = save_input_buf;
1060 input_buf_ptr = save_input_buf_ptr;
1061 input_buf_siz = save_input_buf_siz;
1066 static void free_token(char *tok)
1072 static enum event_type read_token(char **tok)
1074 enum event_type type;
1077 type = __read_token(tok);
1078 if (type != EVENT_SPACE)
1090 * pevent_read_token - access to utilites to use the pevent parser
1091 * @tok: The token to return
1093 * This will parse tokens from the string given by
1094 * pevent_init_data().
1096 * Returns the token type.
1098 enum event_type pevent_read_token(char **tok)
1100 return read_token(tok);
1104 * pevent_free_token - free a token returned by pevent_read_token
1105 * @token: the token to free
1107 void pevent_free_token(char *token)
1113 static enum event_type read_token_item(char **tok)
1115 enum event_type type;
1118 type = __read_token(tok);
1119 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1130 static int test_type(enum event_type type, enum event_type expect)
1132 if (type != expect) {
1133 do_warning("Error: expected type %d but read %d",
1140 static int test_type_token(enum event_type type, const char *token,
1141 enum event_type expect, const char *expect_tok)
1143 if (type != expect) {
1144 do_warning("Error: expected type %d but read %d",
1149 if (strcmp(token, expect_tok) != 0) {
1150 do_warning("Error: expected '%s' but read '%s'",
1157 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1159 enum event_type type;
1162 type = read_token(tok);
1164 type = read_token_item(tok);
1165 return test_type(type, expect);
1168 static int read_expect_type(enum event_type expect, char **tok)
1170 return __read_expect_type(expect, tok, 1);
1173 static int __read_expected(enum event_type expect, const char *str,
1176 enum event_type type;
1181 type = read_token(&token);
1183 type = read_token_item(&token);
1185 ret = test_type_token(type, token, expect, str);
1192 static int read_expected(enum event_type expect, const char *str)
1194 return __read_expected(expect, str, 1);
1197 static int read_expected_item(enum event_type expect, const char *str)
1199 return __read_expected(expect, str, 0);
1202 static char *event_read_name(void)
1206 if (read_expected(EVENT_ITEM, "name") < 0)
1209 if (read_expected(EVENT_OP, ":") < 0)
1212 if (read_expect_type(EVENT_ITEM, &token) < 0)
1222 static int event_read_id(void)
1227 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1230 if (read_expected(EVENT_OP, ":") < 0)
1233 if (read_expect_type(EVENT_ITEM, &token) < 0)
1236 id = strtoul(token, NULL, 0);
1245 static int field_is_string(struct format_field *field)
1247 if ((field->flags & FIELD_IS_ARRAY) &&
1248 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1249 strstr(field->type, "s8")))
1255 static int field_is_dynamic(struct format_field *field)
1257 if (strncmp(field->type, "__data_loc", 10) == 0)
1263 static int field_is_long(struct format_field *field)
1265 /* includes long long */
1266 if (strstr(field->type, "long"))
1272 static unsigned int type_size(const char *name)
1274 /* This covers all FIELD_IS_STRING types. */
1292 for (i = 0; table[i].type; i++) {
1293 if (!strcmp(table[i].type, name))
1294 return table[i].size;
1300 static int event_read_fields(struct event_format *event, struct format_field **fields)
1302 struct format_field *field = NULL;
1303 enum event_type type;
1309 unsigned int size_dynamic = 0;
1311 type = read_token(&token);
1312 if (type == EVENT_NEWLINE) {
1319 if (test_type_token(type, token, EVENT_ITEM, "field"))
1323 type = read_token(&token);
1325 * The ftrace fields may still use the "special" name.
1328 if (event->flags & EVENT_FL_ISFTRACE &&
1329 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1331 type = read_token(&token);
1334 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1338 if (read_expect_type(EVENT_ITEM, &token) < 0)
1343 field = calloc(1, sizeof(*field));
1347 field->event = event;
1349 /* read the rest of the type */
1351 type = read_token(&token);
1352 if (type == EVENT_ITEM ||
1353 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1355 * Some of the ftrace fields are broken and have
1356 * an illegal "." in them.
1358 (event->flags & EVENT_FL_ISFTRACE &&
1359 type == EVENT_OP && strcmp(token, ".") == 0)) {
1361 if (strcmp(token, "*") == 0)
1362 field->flags |= FIELD_IS_POINTER;
1366 new_type = realloc(field->type,
1367 strlen(field->type) +
1368 strlen(last_token) + 2);
1373 field->type = new_type;
1374 strcat(field->type, " ");
1375 strcat(field->type, last_token);
1378 field->type = last_token;
1387 do_warning_event(event, "%s: no type found", __func__);
1390 field->name = last_token;
1392 if (test_type(type, EVENT_OP))
1395 if (strcmp(token, "[") == 0) {
1396 enum event_type last_type = type;
1397 char *brackets = token;
1401 field->flags |= FIELD_IS_ARRAY;
1403 type = read_token(&token);
1405 if (type == EVENT_ITEM)
1406 field->arraylen = strtoul(token, NULL, 0);
1408 field->arraylen = 0;
1410 while (strcmp(token, "]") != 0) {
1411 if (last_type == EVENT_ITEM &&
1418 new_brackets = realloc(brackets,
1420 strlen(token) + len);
1421 if (!new_brackets) {
1425 brackets = new_brackets;
1427 strcat(brackets, " ");
1428 strcat(brackets, token);
1429 /* We only care about the last token */
1430 field->arraylen = strtoul(token, NULL, 0);
1432 type = read_token(&token);
1433 if (type == EVENT_NONE) {
1434 do_warning_event(event, "failed to find token");
1441 new_brackets = realloc(brackets, strlen(brackets) + 2);
1442 if (!new_brackets) {
1446 brackets = new_brackets;
1447 strcat(brackets, "]");
1449 /* add brackets to type */
1451 type = read_token(&token);
1453 * If the next token is not an OP, then it is of
1454 * the format: type [] item;
1456 if (type == EVENT_ITEM) {
1458 new_type = realloc(field->type,
1459 strlen(field->type) +
1460 strlen(field->name) +
1461 strlen(brackets) + 2);
1466 field->type = new_type;
1467 strcat(field->type, " ");
1468 strcat(field->type, field->name);
1469 size_dynamic = type_size(field->name);
1470 free_token(field->name);
1471 strcat(field->type, brackets);
1472 field->name = token;
1473 type = read_token(&token);
1476 new_type = realloc(field->type,
1477 strlen(field->type) +
1478 strlen(brackets) + 1);
1483 field->type = new_type;
1484 strcat(field->type, brackets);
1489 if (field_is_string(field))
1490 field->flags |= FIELD_IS_STRING;
1491 if (field_is_dynamic(field))
1492 field->flags |= FIELD_IS_DYNAMIC;
1493 if (field_is_long(field))
1494 field->flags |= FIELD_IS_LONG;
1496 if (test_type_token(type, token, EVENT_OP, ";"))
1500 if (read_expected(EVENT_ITEM, "offset") < 0)
1503 if (read_expected(EVENT_OP, ":") < 0)
1506 if (read_expect_type(EVENT_ITEM, &token))
1508 field->offset = strtoul(token, NULL, 0);
1511 if (read_expected(EVENT_OP, ";") < 0)
1514 if (read_expected(EVENT_ITEM, "size") < 0)
1517 if (read_expected(EVENT_OP, ":") < 0)
1520 if (read_expect_type(EVENT_ITEM, &token))
1522 field->size = strtoul(token, NULL, 0);
1525 if (read_expected(EVENT_OP, ";") < 0)
1528 type = read_token(&token);
1529 if (type != EVENT_NEWLINE) {
1530 /* newer versions of the kernel have a "signed" type */
1531 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1536 if (read_expected(EVENT_OP, ":") < 0)
1539 if (read_expect_type(EVENT_ITEM, &token))
1542 if (strtoul(token, NULL, 0))
1543 field->flags |= FIELD_IS_SIGNED;
1546 if (read_expected(EVENT_OP, ";") < 0)
1549 if (read_expect_type(EVENT_NEWLINE, &token))
1555 if (field->flags & FIELD_IS_ARRAY) {
1556 if (field->arraylen)
1557 field->elementsize = field->size / field->arraylen;
1558 else if (field->flags & FIELD_IS_DYNAMIC)
1559 field->elementsize = size_dynamic;
1560 else if (field->flags & FIELD_IS_STRING)
1561 field->elementsize = 1;
1562 else if (field->flags & FIELD_IS_LONG)
1563 field->elementsize = event->pevent ?
1564 event->pevent->long_size :
1567 field->elementsize = field->size;
1570 fields = &field->next;
1587 static int event_read_format(struct event_format *event)
1592 if (read_expected_item(EVENT_ITEM, "format") < 0)
1595 if (read_expected(EVENT_OP, ":") < 0)
1598 if (read_expect_type(EVENT_NEWLINE, &token))
1602 ret = event_read_fields(event, &event->format.common_fields);
1605 event->format.nr_common = ret;
1607 ret = event_read_fields(event, &event->format.fields);
1610 event->format.nr_fields = ret;
1619 static enum event_type
1620 process_arg_token(struct event_format *event, struct print_arg *arg,
1621 char **tok, enum event_type type);
1623 static enum event_type
1624 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1626 enum event_type type;
1629 type = read_token(&token);
1632 return process_arg_token(event, arg, tok, type);
1635 static enum event_type
1636 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1639 * For __print_symbolic() and __print_flags, we need to completely
1640 * evaluate the first argument, which defines what to print next.
1642 static enum event_type
1643 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1645 enum event_type type;
1647 type = process_arg(event, arg, tok);
1649 while (type == EVENT_OP) {
1650 type = process_op(event, arg, tok);
1656 static enum event_type
1657 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1659 struct print_arg *arg, *left, *right;
1660 enum event_type type;
1665 right = alloc_arg();
1667 if (!arg || !left || !right) {
1668 do_warning_event(event, "%s: not enough memory!", __func__);
1669 /* arg will be freed at out_free */
1675 arg->type = PRINT_OP;
1676 arg->op.left = left;
1677 arg->op.right = right;
1680 type = process_arg(event, left, &token);
1683 /* Handle other operations in the arguments */
1684 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1685 type = process_op(event, left, &token);
1689 if (test_type_token(type, token, EVENT_OP, ":"))
1694 type = process_arg(event, right, &token);
1696 top->op.right = arg;
1702 /* Top may point to itself */
1703 top->op.right = NULL;
1709 static enum event_type
1710 process_array(struct event_format *event, struct print_arg *top, char **tok)
1712 struct print_arg *arg;
1713 enum event_type type;
1718 do_warning_event(event, "%s: not enough memory!", __func__);
1719 /* '*tok' is set to top->op.op. No need to free. */
1725 type = process_arg(event, arg, &token);
1726 if (test_type_token(type, token, EVENT_OP, "]"))
1729 top->op.right = arg;
1732 type = read_token_item(&token);
1743 static int get_op_prio(char *op)
1757 /* '>>' and '<<' are 8 */
1761 /* '==' and '!=' are 10 */
1771 do_warning("unknown op '%c'", op[0]);
1775 if (strcmp(op, "++") == 0 ||
1776 strcmp(op, "--") == 0) {
1778 } else if (strcmp(op, ">>") == 0 ||
1779 strcmp(op, "<<") == 0) {
1781 } else if (strcmp(op, ">=") == 0 ||
1782 strcmp(op, "<=") == 0) {
1784 } else if (strcmp(op, "==") == 0 ||
1785 strcmp(op, "!=") == 0) {
1787 } else if (strcmp(op, "&&") == 0) {
1789 } else if (strcmp(op, "||") == 0) {
1792 do_warning("unknown op '%s'", op);
1798 static int set_op_prio(struct print_arg *arg)
1801 /* single ops are the greatest */
1802 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1805 arg->op.prio = get_op_prio(arg->op.op);
1807 return arg->op.prio;
1810 /* Note, *tok does not get freed, but will most likely be saved */
1811 static enum event_type
1812 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1814 struct print_arg *left, *right = NULL;
1815 enum event_type type;
1818 /* the op is passed in via tok */
1821 if (arg->type == PRINT_OP && !arg->op.left) {
1822 /* handle single op */
1824 do_warning_event(event, "bad op token %s", token);
1834 do_warning_event(event, "bad op token %s", token);
1839 /* make an empty left */
1844 left->type = PRINT_NULL;
1845 arg->op.left = left;
1847 right = alloc_arg();
1851 arg->op.right = right;
1853 /* do not free the token, it belongs to an op */
1855 type = process_arg(event, right, tok);
1857 } else if (strcmp(token, "?") == 0) {
1863 /* copy the top arg to the left */
1866 arg->type = PRINT_OP;
1868 arg->op.left = left;
1871 /* it will set arg->op.right */
1872 type = process_cond(event, arg, tok);
1874 } else if (strcmp(token, ">>") == 0 ||
1875 strcmp(token, "<<") == 0 ||
1876 strcmp(token, "&") == 0 ||
1877 strcmp(token, "|") == 0 ||
1878 strcmp(token, "&&") == 0 ||
1879 strcmp(token, "||") == 0 ||
1880 strcmp(token, "-") == 0 ||
1881 strcmp(token, "+") == 0 ||
1882 strcmp(token, "*") == 0 ||
1883 strcmp(token, "^") == 0 ||
1884 strcmp(token, "/") == 0 ||
1885 strcmp(token, "<") == 0 ||
1886 strcmp(token, ">") == 0 ||
1887 strcmp(token, "<=") == 0 ||
1888 strcmp(token, ">=") == 0 ||
1889 strcmp(token, "==") == 0 ||
1890 strcmp(token, "!=") == 0) {
1896 /* copy the top arg to the left */
1899 arg->type = PRINT_OP;
1901 arg->op.left = left;
1902 arg->op.right = NULL;
1904 if (set_op_prio(arg) == -1) {
1905 event->flags |= EVENT_FL_FAILED;
1906 /* arg->op.op (= token) will be freed at out_free */
1911 type = read_token_item(&token);
1914 /* could just be a type pointer */
1915 if ((strcmp(arg->op.op, "*") == 0) &&
1916 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1919 if (left->type != PRINT_ATOM) {
1920 do_warning_event(event, "bad pointer type");
1923 new_atom = realloc(left->atom.atom,
1924 strlen(left->atom.atom) + 3);
1928 left->atom.atom = new_atom;
1929 strcat(left->atom.atom, " *");
1937 right = alloc_arg();
1941 type = process_arg_token(event, right, tok, type);
1943 if (right->type == PRINT_OP &&
1944 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
1945 struct print_arg tmp;
1947 /* rotate ops according to the priority */
1948 arg->op.right = right->op.left;
1954 arg->op.left = right;
1956 arg->op.right = right;
1959 } else if (strcmp(token, "[") == 0) {
1967 arg->type = PRINT_OP;
1969 arg->op.left = left;
1973 /* it will set arg->op.right */
1974 type = process_array(event, arg, tok);
1977 do_warning_event(event, "unknown op '%s'", token);
1978 event->flags |= EVENT_FL_FAILED;
1979 /* the arg is now the left side */
1983 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1986 /* higher prios need to be closer to the root */
1987 prio = get_op_prio(*tok);
1989 if (prio > arg->op.prio)
1990 return process_op(event, arg, tok);
1992 return process_op(event, right, tok);
1998 do_warning_event(event, "%s: not enough memory!", __func__);
2005 static enum event_type
2006 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2009 enum event_type type;
2013 if (read_expected(EVENT_OP, "->") < 0)
2016 if (read_expect_type(EVENT_ITEM, &token) < 0)
2020 arg->type = PRINT_FIELD;
2021 arg->field.name = field;
2023 if (is_flag_field) {
2024 arg->field.field = pevent_find_any_field(event, arg->field.name);
2025 arg->field.field->flags |= FIELD_IS_FLAG;
2027 } else if (is_symbolic_field) {
2028 arg->field.field = pevent_find_any_field(event, arg->field.name);
2029 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2030 is_symbolic_field = 0;
2033 type = read_token(&token);
2045 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2046 struct print_arg **print_arg)
2048 struct print_arg *field;
2049 enum event_type type;
2053 field = alloc_arg();
2055 do_warning_event(event, "%s: not enough memory!", __func__);
2060 type = process_arg(event, field, &token);
2062 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2066 goto out_free_token;
2077 static char *arg_eval (struct print_arg *arg);
2079 static unsigned long long
2080 eval_type_str(unsigned long long val, const char *type, int pointer)
2090 if (type[len-1] != '*') {
2091 do_warning("pointer expected with non pointer type");
2097 do_warning("%s: not enough memory!", __func__);
2100 memcpy(ref, type, len);
2102 /* chop off the " *" */
2105 val = eval_type_str(val, ref, 0);
2110 /* check if this is a pointer */
2111 if (type[len - 1] == '*')
2114 /* Try to figure out the arg size*/
2115 if (strncmp(type, "struct", 6) == 0)
2119 if (strcmp(type, "u8") == 0)
2122 if (strcmp(type, "u16") == 0)
2123 return val & 0xffff;
2125 if (strcmp(type, "u32") == 0)
2126 return val & 0xffffffff;
2128 if (strcmp(type, "u64") == 0 ||
2129 strcmp(type, "s64"))
2132 if (strcmp(type, "s8") == 0)
2133 return (unsigned long long)(char)val & 0xff;
2135 if (strcmp(type, "s16") == 0)
2136 return (unsigned long long)(short)val & 0xffff;
2138 if (strcmp(type, "s32") == 0)
2139 return (unsigned long long)(int)val & 0xffffffff;
2141 if (strncmp(type, "unsigned ", 9) == 0) {
2146 if (strcmp(type, "char") == 0) {
2148 return (unsigned long long)(char)val & 0xff;
2153 if (strcmp(type, "short") == 0) {
2155 return (unsigned long long)(short)val & 0xffff;
2157 return val & 0xffff;
2160 if (strcmp(type, "int") == 0) {
2162 return (unsigned long long)(int)val & 0xffffffff;
2164 return val & 0xffffffff;
2171 * Try to figure out the type.
2173 static unsigned long long
2174 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2176 if (arg->type != PRINT_TYPE) {
2177 do_warning("expected type argument");
2181 return eval_type_str(val, arg->typecast.type, pointer);
2184 static int arg_num_eval(struct print_arg *arg, long long *val)
2186 long long left, right;
2189 switch (arg->type) {
2191 *val = strtoll(arg->atom.atom, NULL, 0);
2194 ret = arg_num_eval(arg->typecast.item, val);
2197 *val = eval_type(*val, arg, 0);
2200 switch (arg->op.op[0]) {
2202 ret = arg_num_eval(arg->op.left, &left);
2205 ret = arg_num_eval(arg->op.right, &right);
2209 *val = left || right;
2211 *val = left | right;
2214 ret = arg_num_eval(arg->op.left, &left);
2217 ret = arg_num_eval(arg->op.right, &right);
2221 *val = left && right;
2223 *val = left & right;
2226 ret = arg_num_eval(arg->op.left, &left);
2229 ret = arg_num_eval(arg->op.right, &right);
2232 switch (arg->op.op[1]) {
2234 *val = left < right;
2237 *val = left << right;
2240 *val = left <= right;
2243 do_warning("unknown op '%s'", arg->op.op);
2248 ret = arg_num_eval(arg->op.left, &left);
2251 ret = arg_num_eval(arg->op.right, &right);
2254 switch (arg->op.op[1]) {
2256 *val = left > right;
2259 *val = left >> right;
2262 *val = left >= right;
2265 do_warning("unknown op '%s'", arg->op.op);
2270 ret = arg_num_eval(arg->op.left, &left);
2273 ret = arg_num_eval(arg->op.right, &right);
2277 if (arg->op.op[1] != '=') {
2278 do_warning("unknown op '%s'", arg->op.op);
2281 *val = left == right;
2284 ret = arg_num_eval(arg->op.left, &left);
2287 ret = arg_num_eval(arg->op.right, &right);
2291 switch (arg->op.op[1]) {
2293 *val = left != right;
2296 do_warning("unknown op '%s'", arg->op.op);
2301 /* check for negative */
2302 if (arg->op.left->type == PRINT_NULL)
2305 ret = arg_num_eval(arg->op.left, &left);
2308 ret = arg_num_eval(arg->op.right, &right);
2311 *val = left - right;
2314 if (arg->op.left->type == PRINT_NULL)
2317 ret = arg_num_eval(arg->op.left, &left);
2320 ret = arg_num_eval(arg->op.right, &right);
2323 *val = left + right;
2326 do_warning("unknown op '%s'", arg->op.op);
2332 case PRINT_FIELD ... PRINT_SYMBOL:
2337 do_warning("invalid eval type %d", arg->type);
2344 static char *arg_eval (struct print_arg *arg)
2347 static char buf[20];
2349 switch (arg->type) {
2351 return arg->atom.atom;
2353 return arg_eval(arg->typecast.item);
2355 if (!arg_num_eval(arg, &val))
2357 sprintf(buf, "%lld", val);
2361 case PRINT_FIELD ... PRINT_SYMBOL:
2366 do_warning("invalid eval type %d", arg->type);
2373 static enum event_type
2374 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2376 enum event_type type;
2377 struct print_arg *arg = NULL;
2378 struct print_flag_sym *field;
2384 type = read_token_item(&token);
2385 if (test_type_token(type, token, EVENT_OP, "{"))
2393 type = process_arg(event, arg, &token);
2395 if (type == EVENT_OP)
2396 type = process_op(event, arg, &token);
2398 if (type == EVENT_ERROR)
2401 if (test_type_token(type, token, EVENT_DELIM, ","))
2404 field = calloc(1, sizeof(*field));
2408 value = arg_eval(arg);
2410 goto out_free_field;
2411 field->value = strdup(value);
2412 if (field->value == NULL)
2413 goto out_free_field;
2421 type = process_arg(event, arg, &token);
2422 if (test_type_token(type, token, EVENT_OP, "}"))
2423 goto out_free_field;
2425 value = arg_eval(arg);
2427 goto out_free_field;
2428 field->str = strdup(value);
2429 if (field->str == NULL)
2430 goto out_free_field;
2435 list = &field->next;
2438 type = read_token_item(&token);
2439 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2445 free_flag_sym(field);
2454 static enum event_type
2455 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2457 struct print_arg *field;
2458 enum event_type type;
2461 memset(arg, 0, sizeof(*arg));
2462 arg->type = PRINT_FLAGS;
2464 field = alloc_arg();
2466 do_warning_event(event, "%s: not enough memory!", __func__);
2470 type = process_field_arg(event, field, &token);
2472 /* Handle operations in the first argument */
2473 while (type == EVENT_OP)
2474 type = process_op(event, field, &token);
2476 if (test_type_token(type, token, EVENT_DELIM, ","))
2477 goto out_free_field;
2480 arg->flags.field = field;
2482 type = read_token_item(&token);
2483 if (event_item_type(type)) {
2484 arg->flags.delim = token;
2485 type = read_token_item(&token);
2488 if (test_type_token(type, token, EVENT_DELIM, ","))
2491 type = process_fields(event, &arg->flags.flags, &token);
2492 if (test_type_token(type, token, EVENT_DELIM, ")"))
2496 type = read_token_item(tok);
2507 static enum event_type
2508 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2510 struct print_arg *field;
2511 enum event_type type;
2514 memset(arg, 0, sizeof(*arg));
2515 arg->type = PRINT_SYMBOL;
2517 field = alloc_arg();
2519 do_warning_event(event, "%s: not enough memory!", __func__);
2523 type = process_field_arg(event, field, &token);
2525 if (test_type_token(type, token, EVENT_DELIM, ","))
2526 goto out_free_field;
2528 arg->symbol.field = field;
2530 type = process_fields(event, &arg->symbol.symbols, &token);
2531 if (test_type_token(type, token, EVENT_DELIM, ")"))
2535 type = read_token_item(tok);
2546 static enum event_type
2547 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2549 memset(arg, 0, sizeof(*arg));
2550 arg->type = PRINT_HEX;
2552 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2555 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2558 return read_token_item(tok);
2561 free_arg(arg->hex.field);
2567 static enum event_type
2568 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2570 memset(arg, 0, sizeof(*arg));
2571 arg->type = PRINT_INT_ARRAY;
2573 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2576 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2579 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2582 return read_token_item(tok);
2585 free_arg(arg->int_array.count);
2587 free_arg(arg->int_array.field);
2593 static enum event_type
2594 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2596 struct format_field *field;
2597 enum event_type type;
2600 memset(arg, 0, sizeof(*arg));
2601 arg->type = PRINT_DYNAMIC_ARRAY;
2604 * The item within the parenthesis is another field that holds
2605 * the index into where the array starts.
2607 type = read_token(&token);
2609 if (type != EVENT_ITEM)
2612 /* Find the field */
2614 field = pevent_find_field(event, token);
2618 arg->dynarray.field = field;
2619 arg->dynarray.index = 0;
2621 if (read_expected(EVENT_DELIM, ")") < 0)
2625 type = read_token_item(&token);
2627 if (type != EVENT_OP || strcmp(token, "[") != 0)
2633 do_warning_event(event, "%s: not enough memory!", __func__);
2638 type = process_arg(event, arg, &token);
2639 if (type == EVENT_ERROR)
2642 if (!test_type_token(type, token, EVENT_OP, "]"))
2646 type = read_token_item(tok);
2657 static enum event_type
2658 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2660 struct print_arg *item_arg;
2661 enum event_type type;
2664 type = process_arg(event, arg, &token);
2666 if (type == EVENT_ERROR)
2669 if (type == EVENT_OP)
2670 type = process_op(event, arg, &token);
2672 if (type == EVENT_ERROR)
2675 if (test_type_token(type, token, EVENT_DELIM, ")"))
2679 type = read_token_item(&token);
2682 * If the next token is an item or another open paren, then
2683 * this was a typecast.
2685 if (event_item_type(type) ||
2686 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2688 /* make this a typecast and contine */
2690 /* prevous must be an atom */
2691 if (arg->type != PRINT_ATOM) {
2692 do_warning_event(event, "previous needed to be PRINT_ATOM");
2696 item_arg = alloc_arg();
2698 do_warning_event(event, "%s: not enough memory!",
2703 arg->type = PRINT_TYPE;
2704 arg->typecast.type = arg->atom.atom;
2705 arg->typecast.item = item_arg;
2706 type = process_arg_token(event, item_arg, &token, type);
2720 static enum event_type
2721 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2724 enum event_type type;
2727 if (read_expect_type(EVENT_ITEM, &token) < 0)
2730 arg->type = PRINT_STRING;
2731 arg->string.string = token;
2732 arg->string.offset = -1;
2734 if (read_expected(EVENT_DELIM, ")") < 0)
2737 type = read_token(&token);
2749 static enum event_type
2750 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2753 enum event_type type;
2756 if (read_expect_type(EVENT_ITEM, &token) < 0)
2759 arg->type = PRINT_BITMASK;
2760 arg->bitmask.bitmask = token;
2761 arg->bitmask.offset = -1;
2763 if (read_expected(EVENT_DELIM, ")") < 0)
2766 type = read_token(&token);
2778 static struct pevent_function_handler *
2779 find_func_handler(struct pevent *pevent, char *func_name)
2781 struct pevent_function_handler *func;
2786 for (func = pevent->func_handlers; func; func = func->next) {
2787 if (strcmp(func->name, func_name) == 0)
2794 static void remove_func_handler(struct pevent *pevent, char *func_name)
2796 struct pevent_function_handler *func;
2797 struct pevent_function_handler **next;
2799 next = &pevent->func_handlers;
2800 while ((func = *next)) {
2801 if (strcmp(func->name, func_name) == 0) {
2803 free_func_handle(func);
2810 static enum event_type
2811 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2812 struct print_arg *arg, char **tok)
2814 struct print_arg **next_arg;
2815 struct print_arg *farg;
2816 enum event_type type;
2820 arg->type = PRINT_FUNC;
2821 arg->func.func = func;
2825 next_arg = &(arg->func.args);
2826 for (i = 0; i < func->nr_args; i++) {
2829 do_warning_event(event, "%s: not enough memory!",
2834 type = process_arg(event, farg, &token);
2835 if (i < (func->nr_args - 1)) {
2836 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2837 do_warning_event(event,
2838 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2839 func->name, func->nr_args,
2840 event->name, i + 1);
2844 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2845 do_warning_event(event,
2846 "Error: function '%s()' only expects %d arguments but event %s has more",
2847 func->name, func->nr_args, event->name);
2853 next_arg = &(farg->next);
2857 type = read_token(&token);
2868 static enum event_type
2869 process_function(struct event_format *event, struct print_arg *arg,
2870 char *token, char **tok)
2872 struct pevent_function_handler *func;
2874 if (strcmp(token, "__print_flags") == 0) {
2877 return process_flags(event, arg, tok);
2879 if (strcmp(token, "__print_symbolic") == 0) {
2881 is_symbolic_field = 1;
2882 return process_symbols(event, arg, tok);
2884 if (strcmp(token, "__print_hex") == 0) {
2886 return process_hex(event, arg, tok);
2888 if (strcmp(token, "__print_array") == 0) {
2890 return process_int_array(event, arg, tok);
2892 if (strcmp(token, "__get_str") == 0) {
2894 return process_str(event, arg, tok);
2896 if (strcmp(token, "__get_bitmask") == 0) {
2898 return process_bitmask(event, arg, tok);
2900 if (strcmp(token, "__get_dynamic_array") == 0) {
2902 return process_dynamic_array(event, arg, tok);
2905 func = find_func_handler(event->pevent, token);
2908 return process_func_handler(event, func, arg, tok);
2911 do_warning_event(event, "function %s not defined", token);
2916 static enum event_type
2917 process_arg_token(struct event_format *event, struct print_arg *arg,
2918 char **tok, enum event_type type)
2927 if (strcmp(token, "REC") == 0) {
2929 type = process_entry(event, arg, &token);
2933 /* test the next token */
2934 type = read_token_item(&token);
2937 * If the next token is a parenthesis, then this
2940 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2943 /* this will free atom. */
2944 type = process_function(event, arg, atom, &token);
2947 /* atoms can be more than one token long */
2948 while (type == EVENT_ITEM) {
2950 new_atom = realloc(atom,
2951 strlen(atom) + strlen(token) + 2);
2960 strcat(atom, token);
2962 type = read_token_item(&token);
2965 arg->type = PRINT_ATOM;
2966 arg->atom.atom = atom;
2971 arg->type = PRINT_ATOM;
2972 arg->atom.atom = token;
2973 type = read_token_item(&token);
2976 if (strcmp(token, "(") == 0) {
2978 type = process_paren(event, arg, &token);
2982 /* handle single ops */
2983 arg->type = PRINT_OP;
2985 arg->op.left = NULL;
2986 type = process_op(event, arg, &token);
2988 /* On error, the op is freed */
2989 if (type == EVENT_ERROR)
2992 /* return error type if errored */
2995 case EVENT_ERROR ... EVENT_NEWLINE:
2997 do_warning_event(event, "unexpected type %d", type);
3005 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3007 enum event_type type = EVENT_ERROR;
3008 struct print_arg *arg;
3013 if (type == EVENT_NEWLINE) {
3014 type = read_token_item(&token);
3020 do_warning_event(event, "%s: not enough memory!",
3025 type = process_arg(event, arg, &token);
3027 if (type == EVENT_ERROR) {
3036 if (type == EVENT_OP) {
3037 type = process_op(event, arg, &token);
3039 if (type == EVENT_ERROR) {
3048 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3055 } while (type != EVENT_NONE);
3057 if (type != EVENT_NONE && type != EVENT_ERROR)
3063 static int event_read_print(struct event_format *event)
3065 enum event_type type;
3069 if (read_expected_item(EVENT_ITEM, "print") < 0)
3072 if (read_expected(EVENT_ITEM, "fmt") < 0)
3075 if (read_expected(EVENT_OP, ":") < 0)
3078 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3082 event->print_fmt.format = token;
3083 event->print_fmt.args = NULL;
3085 /* ok to have no arg */
3086 type = read_token_item(&token);
3088 if (type == EVENT_NONE)
3091 /* Handle concatenation of print lines */
3092 if (type == EVENT_DQUOTE) {
3095 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3098 free_token(event->print_fmt.format);
3099 event->print_fmt.format = NULL;
3104 if (test_type_token(type, token, EVENT_DELIM, ","))
3109 ret = event_read_print_args(event, &event->print_fmt.args);
3121 * pevent_find_common_field - return a common field by event
3122 * @event: handle for the event
3123 * @name: the name of the common field to return
3125 * Returns a common field from the event by the given @name.
3126 * This only searchs the common fields and not all field.
3128 struct format_field *
3129 pevent_find_common_field(struct event_format *event, const char *name)
3131 struct format_field *format;
3133 for (format = event->format.common_fields;
3134 format; format = format->next) {
3135 if (strcmp(format->name, name) == 0)
3143 * pevent_find_field - find a non-common field
3144 * @event: handle for the event
3145 * @name: the name of the non-common field
3147 * Returns a non-common field by the given @name.
3148 * This does not search common fields.
3150 struct format_field *
3151 pevent_find_field(struct event_format *event, const char *name)
3153 struct format_field *format;
3155 for (format = event->format.fields;
3156 format; format = format->next) {
3157 if (strcmp(format->name, name) == 0)
3165 * pevent_find_any_field - find any field by name
3166 * @event: handle for the event
3167 * @name: the name of the field
3169 * Returns a field by the given @name.
3170 * This searchs the common field names first, then
3171 * the non-common ones if a common one was not found.
3173 struct format_field *
3174 pevent_find_any_field(struct event_format *event, const char *name)
3176 struct format_field *format;
3178 format = pevent_find_common_field(event, name);
3181 return pevent_find_field(event, name);
3185 * pevent_read_number - read a number from data
3186 * @pevent: handle for the pevent
3187 * @ptr: the raw data
3188 * @size: the size of the data that holds the number
3190 * Returns the number (converted to host) from the
3193 unsigned long long pevent_read_number(struct pevent *pevent,
3194 const void *ptr, int size)
3198 return *(unsigned char *)ptr;
3200 return data2host2(pevent, ptr);
3202 return data2host4(pevent, ptr);
3204 return data2host8(pevent, ptr);
3212 * pevent_read_number_field - read a number from data
3213 * @field: a handle to the field
3214 * @data: the raw data to read
3215 * @value: the value to place the number in
3217 * Reads raw data according to a field offset and size,
3218 * and translates it into @value.
3220 * Returns 0 on success, -1 otherwise.
3222 int pevent_read_number_field(struct format_field *field, const void *data,
3223 unsigned long long *value)
3227 switch (field->size) {
3232 *value = pevent_read_number(field->event->pevent,
3233 data + field->offset, field->size);
3240 static int get_common_info(struct pevent *pevent,
3241 const char *type, int *offset, int *size)
3243 struct event_format *event;
3244 struct format_field *field;
3247 * All events should have the same common elements.
3248 * Pick any event to find where the type is;
3250 if (!pevent->events) {
3251 do_warning("no event_list!");
3255 event = pevent->events[0];
3256 field = pevent_find_common_field(event, type);
3260 *offset = field->offset;
3261 *size = field->size;
3266 static int __parse_common(struct pevent *pevent, void *data,
3267 int *size, int *offset, const char *name)
3272 ret = get_common_info(pevent, name, offset, size);
3276 return pevent_read_number(pevent, data + *offset, *size);
3279 static int trace_parse_common_type(struct pevent *pevent, void *data)
3281 return __parse_common(pevent, data,
3282 &pevent->type_size, &pevent->type_offset,
3286 static int parse_common_pid(struct pevent *pevent, void *data)
3288 return __parse_common(pevent, data,
3289 &pevent->pid_size, &pevent->pid_offset,
3293 static int parse_common_pc(struct pevent *pevent, void *data)
3295 return __parse_common(pevent, data,
3296 &pevent->pc_size, &pevent->pc_offset,
3297 "common_preempt_count");
3300 static int parse_common_flags(struct pevent *pevent, void *data)
3302 return __parse_common(pevent, data,
3303 &pevent->flags_size, &pevent->flags_offset,
3307 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3309 return __parse_common(pevent, data,
3310 &pevent->ld_size, &pevent->ld_offset,
3311 "common_lock_depth");
3314 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3316 return __parse_common(pevent, data,
3317 &pevent->ld_size, &pevent->ld_offset,
3318 "common_migrate_disable");
3321 static int events_id_cmp(const void *a, const void *b);
3324 * pevent_find_event - find an event by given id
3325 * @pevent: a handle to the pevent
3326 * @id: the id of the event
3328 * Returns an event that has a given @id.
3330 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3332 struct event_format **eventptr;
3333 struct event_format key;
3334 struct event_format *pkey = &key;
3336 /* Check cache first */
3337 if (pevent->last_event && pevent->last_event->id == id)
3338 return pevent->last_event;
3342 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3343 sizeof(*pevent->events), events_id_cmp);
3346 pevent->last_event = *eventptr;
3354 * pevent_find_event_by_name - find an event by given name
3355 * @pevent: a handle to the pevent
3356 * @sys: the system name to search for
3357 * @name: the name of the event to search for
3359 * This returns an event with a given @name and under the system
3360 * @sys. If @sys is NULL the first event with @name is returned.
3362 struct event_format *
3363 pevent_find_event_by_name(struct pevent *pevent,
3364 const char *sys, const char *name)
3366 struct event_format *event;
3369 if (pevent->last_event &&
3370 strcmp(pevent->last_event->name, name) == 0 &&
3371 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3372 return pevent->last_event;
3374 for (i = 0; i < pevent->nr_events; i++) {
3375 event = pevent->events[i];
3376 if (strcmp(event->name, name) == 0) {
3379 if (strcmp(event->system, sys) == 0)
3383 if (i == pevent->nr_events)
3386 pevent->last_event = event;
3390 static unsigned long long
3391 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3393 struct pevent *pevent = event->pevent;
3394 unsigned long long val = 0;
3395 unsigned long long left, right;
3396 struct print_arg *typearg = NULL;
3397 struct print_arg *larg;
3398 unsigned long offset;
3399 unsigned int field_size;
3401 switch (arg->type) {
3406 return strtoull(arg->atom.atom, NULL, 0);
3408 if (!arg->field.field) {
3409 arg->field.field = pevent_find_any_field(event, arg->field.name);
3410 if (!arg->field.field)
3411 goto out_warning_field;
3414 /* must be a number */
3415 val = pevent_read_number(pevent, data + arg->field.field->offset,
3416 arg->field.field->size);
3420 case PRINT_INT_ARRAY:
3424 val = eval_num_arg(data, size, event, arg->typecast.item);
3425 return eval_type(val, arg, 0);
3433 val = process_defined_func(&s, data, size, event, arg);
3434 trace_seq_destroy(&s);
3438 if (strcmp(arg->op.op, "[") == 0) {
3440 * Arrays are special, since we don't want
3441 * to read the arg as is.
3443 right = eval_num_arg(data, size, event, arg->op.right);
3445 /* handle typecasts */
3446 larg = arg->op.left;
3447 while (larg->type == PRINT_TYPE) {
3450 larg = larg->typecast.item;
3453 /* Default to long size */
3454 field_size = pevent->long_size;
3456 switch (larg->type) {
3457 case PRINT_DYNAMIC_ARRAY:
3458 offset = pevent_read_number(pevent,
3459 data + larg->dynarray.field->offset,
3460 larg->dynarray.field->size);
3461 if (larg->dynarray.field->elementsize)
3462 field_size = larg->dynarray.field->elementsize;
3464 * The actual length of the dynamic array is stored
3465 * in the top half of the field, and the offset
3466 * is in the bottom half of the 32 bit field.
3472 if (!larg->field.field) {
3474 pevent_find_any_field(event, larg->field.name);
3475 if (!larg->field.field) {
3477 goto out_warning_field;
3480 field_size = larg->field.field->elementsize;
3481 offset = larg->field.field->offset +
3482 right * larg->field.field->elementsize;
3485 goto default_op; /* oops, all bets off */
3487 val = pevent_read_number(pevent,
3488 data + offset, field_size);
3490 val = eval_type(val, typearg, 1);
3492 } else if (strcmp(arg->op.op, "?") == 0) {
3493 left = eval_num_arg(data, size, event, arg->op.left);
3494 arg = arg->op.right;
3496 val = eval_num_arg(data, size, event, arg->op.left);
3498 val = eval_num_arg(data, size, event, arg->op.right);
3502 left = eval_num_arg(data, size, event, arg->op.left);
3503 right = eval_num_arg(data, size, event, arg->op.right);
3504 switch (arg->op.op[0]) {
3506 switch (arg->op.op[1]) {
3511 val = left != right;
3514 goto out_warning_op;
3522 val = left || right;
3528 val = left && right;
3533 switch (arg->op.op[1]) {
3538 val = left << right;
3541 val = left <= right;
3544 goto out_warning_op;
3548 switch (arg->op.op[1]) {
3553 val = left >> right;
3556 val = left >= right;
3559 goto out_warning_op;
3563 if (arg->op.op[1] != '=')
3564 goto out_warning_op;
3566 val = left == right;
3581 goto out_warning_op;
3584 case PRINT_DYNAMIC_ARRAY:
3585 /* Without [], we pass the address to the dynamic data */
3586 offset = pevent_read_number(pevent,
3587 data + arg->dynarray.field->offset,
3588 arg->dynarray.field->size);
3590 * The actual length of the dynamic array is stored
3591 * in the top half of the field, and the offset
3592 * is in the bottom half of the 32 bit field.
3595 val = (unsigned long long)((unsigned long)data + offset);
3597 default: /* not sure what to do there */
3603 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3607 do_warning_event(event, "%s: field %s not found",
3608 __func__, arg->field.name);
3614 unsigned long long value;
3617 static const struct flag flags[] = {
3618 { "HI_SOFTIRQ", 0 },
3619 { "TIMER_SOFTIRQ", 1 },
3620 { "NET_TX_SOFTIRQ", 2 },
3621 { "NET_RX_SOFTIRQ", 3 },
3622 { "BLOCK_SOFTIRQ", 4 },
3623 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3624 { "TASKLET_SOFTIRQ", 6 },
3625 { "SCHED_SOFTIRQ", 7 },
3626 { "HRTIMER_SOFTIRQ", 8 },
3627 { "RCU_SOFTIRQ", 9 },
3629 { "HRTIMER_NORESTART", 0 },
3630 { "HRTIMER_RESTART", 1 },
3633 static long long eval_flag(const char *flag)
3638 * Some flags in the format files do not get converted.
3639 * If the flag is not numeric, see if it is something that
3640 * we already know about.
3642 if (isdigit(flag[0]))
3643 return strtoull(flag, NULL, 0);
3645 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3646 if (strcmp(flags[i].name, flag) == 0)
3647 return flags[i].value;
3652 static void print_str_to_seq(struct trace_seq *s, const char *format,
3653 int len_arg, const char *str)
3656 trace_seq_printf(s, format, len_arg, str);
3658 trace_seq_printf(s, format, str);
3661 static void print_bitmask_to_seq(struct pevent *pevent,
3662 struct trace_seq *s, const char *format,
3663 int len_arg, const void *data, int size)
3665 int nr_bits = size * 8;
3666 int str_size = (nr_bits + 3) / 4;
3674 * The kernel likes to put in commas every 32 bits, we
3677 str_size += (nr_bits - 1) / 32;
3679 str = malloc(str_size + 1);
3681 do_warning("%s: not enough memory!", __func__);
3686 /* Start out with -2 for the two chars per byte */
3687 for (i = str_size - 2; i >= 0; i -= 2) {
3689 * data points to a bit mask of size bytes.
3690 * In the kernel, this is an array of long words, thus
3691 * endianess is very important.
3693 if (pevent->file_bigendian)
3694 index = size - (len + 1);
3698 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3699 memcpy(str + i, buf, 2);
3701 if (!(len & 3) && i > 0) {
3708 trace_seq_printf(s, format, len_arg, str);
3710 trace_seq_printf(s, format, str);
3715 static void print_str_arg(struct trace_seq *s, void *data, int size,
3716 struct event_format *event, const char *format,
3717 int len_arg, struct print_arg *arg)
3719 struct pevent *pevent = event->pevent;
3720 struct print_flag_sym *flag;
3721 struct format_field *field;
3722 struct printk_map *printk;
3723 long long val, fval;
3730 switch (arg->type) {
3735 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3738 field = arg->field.field;
3740 field = pevent_find_any_field(event, arg->field.name);
3742 str = arg->field.name;
3743 goto out_warning_field;
3745 arg->field.field = field;
3747 /* Zero sized fields, mean the rest of the data */
3748 len = field->size ? : size - field->offset;
3751 * Some events pass in pointers. If this is not an array
3752 * and the size is the same as long_size, assume that it
3755 if (!(field->flags & FIELD_IS_ARRAY) &&
3756 field->size == pevent->long_size) {
3757 addr = *(unsigned long *)(data + field->offset);
3758 /* Check if it matches a print format */
3759 printk = find_printk(pevent, addr);
3761 trace_seq_puts(s, printk->printk);
3763 trace_seq_printf(s, "%lx", addr);
3766 str = malloc(len + 1);
3768 do_warning_event(event, "%s: not enough memory!",
3772 memcpy(str, data + field->offset, len);
3774 print_str_to_seq(s, format, len_arg, str);
3778 val = eval_num_arg(data, size, event, arg->flags.field);
3780 for (flag = arg->flags.flags; flag; flag = flag->next) {
3781 fval = eval_flag(flag->value);
3782 if (!val && fval < 0) {
3783 print_str_to_seq(s, format, len_arg, flag->str);
3786 if (fval > 0 && (val & fval) == fval) {
3787 if (print && arg->flags.delim)
3788 trace_seq_puts(s, arg->flags.delim);
3789 print_str_to_seq(s, format, len_arg, flag->str);
3796 val = eval_num_arg(data, size, event, arg->symbol.field);
3797 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3798 fval = eval_flag(flag->value);
3800 print_str_to_seq(s, format, len_arg, flag->str);
3806 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3807 unsigned long offset;
3808 offset = pevent_read_number(pevent,
3809 data + arg->hex.field->dynarray.field->offset,
3810 arg->hex.field->dynarray.field->size);
3811 hex = data + (offset & 0xffff);
3813 field = arg->hex.field->field.field;
3815 str = arg->hex.field->field.name;
3816 field = pevent_find_any_field(event, str);
3818 goto out_warning_field;
3819 arg->hex.field->field.field = field;
3821 hex = data + field->offset;
3823 len = eval_num_arg(data, size, event, arg->hex.size);
3824 for (i = 0; i < len; i++) {
3826 trace_seq_putc(s, ' ');
3827 trace_seq_printf(s, "%02x", hex[i]);
3831 case PRINT_INT_ARRAY: {
3835 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3836 unsigned long offset;
3837 struct format_field *field =
3838 arg->int_array.field->dynarray.field;
3839 offset = pevent_read_number(pevent,
3840 data + field->offset,
3842 num = data + (offset & 0xffff);
3844 field = arg->int_array.field->field.field;
3846 str = arg->int_array.field->field.name;
3847 field = pevent_find_any_field(event, str);
3849 goto out_warning_field;
3850 arg->int_array.field->field.field = field;
3852 num = data + field->offset;
3854 len = eval_num_arg(data, size, event, arg->int_array.count);
3855 el_size = eval_num_arg(data, size, event,
3856 arg->int_array.el_size);
3857 for (i = 0; i < len; i++) {
3859 trace_seq_putc(s, ' ');
3862 trace_seq_printf(s, "%u", *(uint8_t *)num);
3863 } else if (el_size == 2) {
3864 trace_seq_printf(s, "%u", *(uint16_t *)num);
3865 } else if (el_size == 4) {
3866 trace_seq_printf(s, "%u", *(uint32_t *)num);
3867 } else if (el_size == 8) {
3868 trace_seq_printf(s, "%lu", *(uint64_t *)num);
3870 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3871 el_size, *(uint8_t *)num);
3881 case PRINT_STRING: {
3884 if (arg->string.offset == -1) {
3885 struct format_field *f;
3887 f = pevent_find_any_field(event, arg->string.string);
3888 arg->string.offset = f->offset;
3890 str_offset = data2host4(pevent, data + arg->string.offset);
3891 str_offset &= 0xffff;
3892 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3896 print_str_to_seq(s, format, len_arg, arg->string.string);
3898 case PRINT_BITMASK: {
3902 if (arg->bitmask.offset == -1) {
3903 struct format_field *f;
3905 f = pevent_find_any_field(event, arg->bitmask.bitmask);
3906 arg->bitmask.offset = f->offset;
3908 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
3909 bitmask_size = bitmask_offset >> 16;
3910 bitmask_offset &= 0xffff;
3911 print_bitmask_to_seq(pevent, s, format, len_arg,
3912 data + bitmask_offset, bitmask_size);
3917 * The only op for string should be ? :
3919 if (arg->op.op[0] != '?')
3921 val = eval_num_arg(data, size, event, arg->op.left);
3923 print_str_arg(s, data, size, event,
3924 format, len_arg, arg->op.right->op.left);
3926 print_str_arg(s, data, size, event,
3927 format, len_arg, arg->op.right->op.right);
3930 process_defined_func(s, data, size, event, arg);
3940 do_warning_event(event, "%s: field %s not found",
3941 __func__, arg->field.name);
3944 static unsigned long long
3945 process_defined_func(struct trace_seq *s, void *data, int size,
3946 struct event_format *event, struct print_arg *arg)
3948 struct pevent_function_handler *func_handle = arg->func.func;
3949 struct pevent_func_params *param;
3950 unsigned long long *args;
3951 unsigned long long ret;
3952 struct print_arg *farg;
3953 struct trace_seq str;
3955 struct save_str *next;
3957 } *strings = NULL, *string;
3960 if (!func_handle->nr_args) {
3961 ret = (*func_handle->func)(s, NULL);
3965 farg = arg->func.args;
3966 param = func_handle->params;
3969 args = malloc(sizeof(*args) * func_handle->nr_args);
3973 for (i = 0; i < func_handle->nr_args; i++) {
3974 switch (param->type) {
3975 case PEVENT_FUNC_ARG_INT:
3976 case PEVENT_FUNC_ARG_LONG:
3977 case PEVENT_FUNC_ARG_PTR:
3978 args[i] = eval_num_arg(data, size, event, farg);
3980 case PEVENT_FUNC_ARG_STRING:
3981 trace_seq_init(&str);
3982 print_str_arg(&str, data, size, event, "%s", -1, farg);
3983 trace_seq_terminate(&str);
3984 string = malloc(sizeof(*string));
3986 do_warning_event(event, "%s(%d): malloc str",
3987 __func__, __LINE__);
3990 string->next = strings;
3991 string->str = strdup(str.buffer);
3994 do_warning_event(event, "%s(%d): malloc str",
3995 __func__, __LINE__);
3998 args[i] = (uintptr_t)string->str;
4000 trace_seq_destroy(&str);
4004 * Something went totally wrong, this is not
4005 * an input error, something in this code broke.
4007 do_warning_event(event, "Unexpected end of arguments\n");
4011 param = param->next;
4014 ret = (*func_handle->func)(s, args);
4019 strings = string->next;
4025 /* TBD : handle return type here */
4029 static void free_args(struct print_arg *args)
4031 struct print_arg *next;
4041 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4043 struct pevent *pevent = event->pevent;
4044 struct format_field *field, *ip_field;
4045 struct print_arg *args, *arg, **next;
4046 unsigned long long ip, val;
4051 field = pevent->bprint_buf_field;
4052 ip_field = pevent->bprint_ip_field;
4055 field = pevent_find_field(event, "buf");
4057 do_warning_event(event, "can't find buffer field for binary printk");
4060 ip_field = pevent_find_field(event, "ip");
4062 do_warning_event(event, "can't find ip field for binary printk");
4065 pevent->bprint_buf_field = field;
4066 pevent->bprint_ip_field = ip_field;
4069 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4072 * The first arg is the IP pointer.
4076 do_warning_event(event, "%s(%d): not enough memory!",
4077 __func__, __LINE__);
4084 arg->type = PRINT_ATOM;
4086 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4089 /* skip the first "%ps: " */
4090 for (ptr = fmt + 5, bptr = data + field->offset;
4091 bptr < data + size && *ptr; ptr++) {
4126 vsize = pevent->long_size;
4140 /* the pointers are always 4 bytes aligned */
4141 bptr = (void *)(((unsigned long)bptr + 3) &
4143 val = pevent_read_number(pevent, bptr, vsize);
4147 do_warning_event(event, "%s(%d): not enough memory!",
4148 __func__, __LINE__);
4152 arg->type = PRINT_ATOM;
4153 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4160 * The '*' case means that an arg is used as the length.
4161 * We need to continue to figure out for what.
4170 do_warning_event(event, "%s(%d): not enough memory!",
4171 __func__, __LINE__);
4175 arg->type = PRINT_BSTRING;
4176 arg->string.string = strdup(bptr);
4177 if (!arg->string.string)
4179 bptr += strlen(bptr) + 1;
4196 get_bprint_format(void *data, int size __maybe_unused,
4197 struct event_format *event)
4199 struct pevent *pevent = event->pevent;
4200 unsigned long long addr;
4201 struct format_field *field;
4202 struct printk_map *printk;
4205 field = pevent->bprint_fmt_field;
4208 field = pevent_find_field(event, "fmt");
4210 do_warning_event(event, "can't find format field for binary printk");
4213 pevent->bprint_fmt_field = field;
4216 addr = pevent_read_number(pevent, data + field->offset, field->size);
4218 printk = find_printk(pevent, addr);
4220 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4225 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4231 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4232 struct event_format *event, struct print_arg *arg)
4235 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4237 if (arg->type == PRINT_FUNC) {
4238 process_defined_func(s, data, size, event, arg);
4242 if (arg->type != PRINT_FIELD) {
4243 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4249 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4250 if (!arg->field.field) {
4252 pevent_find_any_field(event, arg->field.name);
4253 if (!arg->field.field) {
4254 do_warning_event(event, "%s: field %s not found",
4255 __func__, arg->field.name);
4259 if (arg->field.field->size != 6) {
4260 trace_seq_printf(s, "INVALIDMAC");
4263 buf = data + arg->field.field->offset;
4264 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4267 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4272 fmt = "%03d.%03d.%03d.%03d";
4274 fmt = "%d.%d.%d.%d";
4276 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4279 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4281 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4282 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4285 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4287 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4290 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4293 unsigned char zerolength[8];
4298 bool needcolon = false;
4300 struct in6_addr in6;
4302 memcpy(&in6, addr, sizeof(struct in6_addr));
4304 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4306 memset(zerolength, 0, sizeof(zerolength));
4313 /* find position of longest 0 run */
4314 for (i = 0; i < range; i++) {
4315 for (j = i; j < range; j++) {
4316 if (in6.s6_addr16[j] != 0)
4321 for (i = 0; i < range; i++) {
4322 if (zerolength[i] > longest) {
4323 longest = zerolength[i];
4327 if (longest == 1) /* don't compress a single 0 */
4331 for (i = 0; i < range; i++) {
4332 if (i == colonpos) {
4333 if (needcolon || i == 0)
4334 trace_seq_printf(s, ":");
4335 trace_seq_printf(s, ":");
4341 trace_seq_printf(s, ":");
4344 /* hex u16 without leading 0s */
4345 word = ntohs(in6.s6_addr16[i]);
4349 trace_seq_printf(s, "%x%02x", hi, lo);
4351 trace_seq_printf(s, "%x", lo);
4358 trace_seq_printf(s, ":");
4359 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4365 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4369 for (j = 0; j < 16; j += 2) {
4370 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4371 if (i == 'I' && j < 14)
4372 trace_seq_printf(s, ":");
4377 * %pi4 print an IPv4 address with leading zeros
4378 * %pI4 print an IPv4 address without leading zeros
4379 * %pi6 print an IPv6 address without colons
4380 * %pI6 print an IPv6 address with colons
4381 * %pI6c print an IPv6 address in compressed form with colons
4382 * %pISpc print an IP address based on sockaddr; p adds port.
4384 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4385 void *data, int size, struct event_format *event,
4386 struct print_arg *arg)
4390 if (arg->type == PRINT_FUNC) {
4391 process_defined_func(s, data, size, event, arg);
4395 if (arg->type != PRINT_FIELD) {
4396 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4400 if (!arg->field.field) {
4402 pevent_find_any_field(event, arg->field.name);
4403 if (!arg->field.field) {
4404 do_warning("%s: field %s not found",
4405 __func__, arg->field.name);
4410 buf = data + arg->field.field->offset;
4412 if (arg->field.field->size != 4) {
4413 trace_seq_printf(s, "INVALIDIPv4");
4416 print_ip4_addr(s, i, buf);
4421 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4422 void *data, int size, struct event_format *event,
4423 struct print_arg *arg)
4430 if (i == 'I' && *ptr == 'c') {
4436 if (arg->type == PRINT_FUNC) {
4437 process_defined_func(s, data, size, event, arg);
4441 if (arg->type != PRINT_FIELD) {
4442 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4446 if (!arg->field.field) {
4448 pevent_find_any_field(event, arg->field.name);
4449 if (!arg->field.field) {
4450 do_warning("%s: field %s not found",
4451 __func__, arg->field.name);
4456 buf = data + arg->field.field->offset;
4458 if (arg->field.field->size != 16) {
4459 trace_seq_printf(s, "INVALIDIPv6");
4464 print_ip6c_addr(s, buf);
4466 print_ip6_addr(s, i, buf);
4471 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4472 void *data, int size, struct event_format *event,
4473 struct print_arg *arg)
4475 char have_c = 0, have_p = 0;
4477 struct sockaddr_storage *sa;
4494 if (arg->type == PRINT_FUNC) {
4495 process_defined_func(s, data, size, event, arg);
4499 if (arg->type != PRINT_FIELD) {
4500 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4504 if (!arg->field.field) {
4506 pevent_find_any_field(event, arg->field.name);
4507 if (!arg->field.field) {
4508 do_warning("%s: field %s not found",
4509 __func__, arg->field.name);
4514 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4516 if (sa->ss_family == AF_INET) {
4517 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4519 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4520 trace_seq_printf(s, "INVALIDIPv4");
4524 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4526 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4529 } else if (sa->ss_family == AF_INET6) {
4530 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4532 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4533 trace_seq_printf(s, "INVALIDIPv6");
4538 trace_seq_printf(s, "[");
4540 buf = (unsigned char *) &sa6->sin6_addr;
4542 print_ip6c_addr(s, buf);
4544 print_ip6_addr(s, i, buf);
4547 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4553 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4554 void *data, int size, struct event_format *event,
4555 struct print_arg *arg)
4557 char i = *ptr; /* 'i' or 'I' */
4570 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4573 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4576 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4585 static int is_printable_array(char *p, unsigned int len)
4589 for (i = 0; i < len && p[i]; i++)
4590 if (!isprint(p[i]) && !isspace(p[i]))
4595 static void print_event_fields(struct trace_seq *s, void *data,
4596 int size __maybe_unused,
4597 struct event_format *event)
4599 struct format_field *field;
4600 unsigned long long val;
4601 unsigned int offset, len, i;
4603 field = event->format.fields;
4605 trace_seq_printf(s, " %s=", field->name);
4606 if (field->flags & FIELD_IS_ARRAY) {
4607 offset = field->offset;
4609 if (field->flags & FIELD_IS_DYNAMIC) {
4610 val = pevent_read_number(event->pevent, data + offset, len);
4615 if (field->flags & FIELD_IS_STRING &&
4616 is_printable_array(data + offset, len)) {
4617 trace_seq_printf(s, "%s", (char *)data + offset);
4619 trace_seq_puts(s, "ARRAY[");
4620 for (i = 0; i < len; i++) {
4622 trace_seq_puts(s, ", ");
4623 trace_seq_printf(s, "%02x",
4624 *((unsigned char *)data + offset + i));
4626 trace_seq_putc(s, ']');
4627 field->flags &= ~FIELD_IS_STRING;
4630 val = pevent_read_number(event->pevent, data + field->offset,
4632 if (field->flags & FIELD_IS_POINTER) {
4633 trace_seq_printf(s, "0x%llx", val);
4634 } else if (field->flags & FIELD_IS_SIGNED) {
4635 switch (field->size) {
4638 * If field is long then print it in hex.
4639 * A long usually stores pointers.
4641 if (field->flags & FIELD_IS_LONG)
4642 trace_seq_printf(s, "0x%x", (int)val);
4644 trace_seq_printf(s, "%d", (int)val);
4647 trace_seq_printf(s, "%2d", (short)val);
4650 trace_seq_printf(s, "%1d", (char)val);
4653 trace_seq_printf(s, "%lld", val);
4656 if (field->flags & FIELD_IS_LONG)
4657 trace_seq_printf(s, "0x%llx", val);
4659 trace_seq_printf(s, "%llu", val);
4662 field = field->next;
4666 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4668 struct pevent *pevent = event->pevent;
4669 struct print_fmt *print_fmt = &event->print_fmt;
4670 struct print_arg *arg = print_fmt->args;
4671 struct print_arg *args = NULL;
4672 const char *ptr = print_fmt->format;
4673 unsigned long long val;
4674 struct func_map *func;
4675 const char *saveptr;
4677 char *bprint_fmt = NULL;
4685 if (event->flags & EVENT_FL_FAILED) {
4686 trace_seq_printf(s, "[FAILED TO PARSE]");
4687 print_event_fields(s, data, size, event);
4691 if (event->flags & EVENT_FL_ISBPRINT) {
4692 bprint_fmt = get_bprint_format(data, size, event);
4693 args = make_bprint_args(bprint_fmt, data, size, event);
4698 for (; *ptr; ptr++) {
4704 trace_seq_putc(s, '\n');
4707 trace_seq_putc(s, '\t');
4710 trace_seq_putc(s, '\r');
4713 trace_seq_putc(s, '\\');
4716 trace_seq_putc(s, *ptr);
4720 } else if (*ptr == '%') {
4728 trace_seq_putc(s, '%');
4731 /* FIXME: need to handle properly */
4743 /* The argument is the length. */
4745 do_warning_event(event, "no argument match");
4746 event->flags |= EVENT_FL_FAILED;
4749 len_arg = eval_num_arg(data, size, event, arg);
4759 if (pevent->long_size == 4)
4764 if (*(ptr+1) == 'F' ||
4768 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4769 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4773 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4776 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4791 do_warning_event(event, "no argument match");
4792 event->flags |= EVENT_FL_FAILED;
4796 len = ((unsigned long)ptr + 1) -
4797 (unsigned long)saveptr;
4799 /* should never happen */
4801 do_warning_event(event, "bad format!");
4802 event->flags |= EVENT_FL_FAILED;
4806 memcpy(format, saveptr, len);
4809 val = eval_num_arg(data, size, event, arg);
4813 func = find_func(pevent, val);
4815 trace_seq_puts(s, func->func);
4816 if (show_func == 'F')
4823 if (pevent->long_size == 8 && ls &&
4824 sizeof(long) != 8) {
4828 /* make %l into %ll */
4829 p = strchr(format, 'l');
4831 memmove(p+1, p, strlen(p)+1);
4832 else if (strcmp(format, "%p") == 0)
4833 strcpy(format, "0x%llx");
4838 trace_seq_printf(s, format, len_arg, (char)val);
4840 trace_seq_printf(s, format, (char)val);
4844 trace_seq_printf(s, format, len_arg, (short)val);
4846 trace_seq_printf(s, format, (short)val);
4850 trace_seq_printf(s, format, len_arg, (int)val);
4852 trace_seq_printf(s, format, (int)val);
4856 trace_seq_printf(s, format, len_arg, (long)val);
4858 trace_seq_printf(s, format, (long)val);
4862 trace_seq_printf(s, format, len_arg,
4865 trace_seq_printf(s, format, (long long)val);
4868 do_warning_event(event, "bad count (%d)", ls);
4869 event->flags |= EVENT_FL_FAILED;
4874 do_warning_event(event, "no matching argument");
4875 event->flags |= EVENT_FL_FAILED;
4879 len = ((unsigned long)ptr + 1) -
4880 (unsigned long)saveptr;
4882 /* should never happen */
4884 do_warning_event(event, "bad format!");
4885 event->flags |= EVENT_FL_FAILED;
4889 memcpy(format, saveptr, len);
4893 /* Use helper trace_seq */
4895 print_str_arg(&p, data, size, event,
4896 format, len_arg, arg);
4897 trace_seq_terminate(&p);
4898 trace_seq_puts(s, p.buffer);
4899 trace_seq_destroy(&p);
4903 trace_seq_printf(s, ">%c<", *ptr);
4907 trace_seq_putc(s, *ptr);
4910 if (event->flags & EVENT_FL_FAILED) {
4912 trace_seq_printf(s, "[FAILED TO PARSE]");
4922 * pevent_data_lat_fmt - parse the data for the latency format
4923 * @pevent: a handle to the pevent
4924 * @s: the trace_seq to write to
4925 * @record: the record to read from
4927 * This parses out the Latency format (interrupts disabled,
4928 * need rescheduling, in hard/soft interrupt, preempt count
4929 * and lock depth) and places it into the trace_seq.
4931 void pevent_data_lat_fmt(struct pevent *pevent,
4932 struct trace_seq *s, struct pevent_record *record)
4934 static int check_lock_depth = 1;
4935 static int check_migrate_disable = 1;
4936 static int lock_depth_exists;
4937 static int migrate_disable_exists;
4938 unsigned int lat_flags;
4941 int migrate_disable;
4944 void *data = record->data;
4946 lat_flags = parse_common_flags(pevent, data);
4947 pc = parse_common_pc(pevent, data);
4948 /* lock_depth may not always exist */
4949 if (lock_depth_exists)
4950 lock_depth = parse_common_lock_depth(pevent, data);
4951 else if (check_lock_depth) {
4952 lock_depth = parse_common_lock_depth(pevent, data);
4954 check_lock_depth = 0;
4956 lock_depth_exists = 1;
4959 /* migrate_disable may not always exist */
4960 if (migrate_disable_exists)
4961 migrate_disable = parse_common_migrate_disable(pevent, data);
4962 else if (check_migrate_disable) {
4963 migrate_disable = parse_common_migrate_disable(pevent, data);
4964 if (migrate_disable < 0)
4965 check_migrate_disable = 0;
4967 migrate_disable_exists = 1;
4970 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4971 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4973 trace_seq_printf(s, "%c%c%c",
4974 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4975 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4977 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4979 (hardirq && softirq) ? 'H' :
4980 hardirq ? 'h' : softirq ? 's' : '.');
4983 trace_seq_printf(s, "%x", pc);
4985 trace_seq_putc(s, '.');
4987 if (migrate_disable_exists) {
4988 if (migrate_disable < 0)
4989 trace_seq_putc(s, '.');
4991 trace_seq_printf(s, "%d", migrate_disable);
4994 if (lock_depth_exists) {
4996 trace_seq_putc(s, '.');
4998 trace_seq_printf(s, "%d", lock_depth);
5001 trace_seq_terminate(s);
5005 * pevent_data_type - parse out the given event type
5006 * @pevent: a handle to the pevent
5007 * @rec: the record to read from
5009 * This returns the event id from the @rec.
5011 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5013 return trace_parse_common_type(pevent, rec->data);
5017 * pevent_data_event_from_type - find the event by a given type
5018 * @pevent: a handle to the pevent
5019 * @type: the type of the event.
5021 * This returns the event form a given @type;
5023 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5025 return pevent_find_event(pevent, type);
5029 * pevent_data_pid - parse the PID from raw data
5030 * @pevent: a handle to the pevent
5031 * @rec: the record to parse
5033 * This returns the PID from a raw data.
5035 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5037 return parse_common_pid(pevent, rec->data);
5041 * pevent_data_comm_from_pid - return the command line from PID
5042 * @pevent: a handle to the pevent
5043 * @pid: the PID of the task to search for
5045 * This returns a pointer to the command line that has the given
5048 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5052 comm = find_cmdline(pevent, pid);
5056 static struct cmdline *
5057 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5059 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5062 cmdlist = cmdlist->next;
5064 cmdlist = pevent->cmdlist;
5066 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5067 cmdlist = cmdlist->next;
5069 return (struct cmdline *)cmdlist;
5073 * pevent_data_pid_from_comm - return the pid from a given comm
5074 * @pevent: a handle to the pevent
5075 * @comm: the cmdline to find the pid from
5076 * @next: the cmdline structure to find the next comm
5078 * This returns the cmdline structure that holds a pid for a given
5079 * comm, or NULL if none found. As there may be more than one pid for
5080 * a given comm, the result of this call can be passed back into
5081 * a recurring call in the @next paramater, and then it will find the
5083 * Also, it does a linear seach, so it may be slow.
5085 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5086 struct cmdline *next)
5088 struct cmdline *cmdline;
5091 * If the cmdlines have not been converted yet, then use
5094 if (!pevent->cmdlines)
5095 return pid_from_cmdlist(pevent, comm, next);
5099 * The next pointer could have been still from
5100 * a previous call before cmdlines were created
5102 if (next < pevent->cmdlines ||
5103 next >= pevent->cmdlines + pevent->cmdline_count)
5110 cmdline = pevent->cmdlines;
5112 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5113 if (strcmp(cmdline->comm, comm) == 0)
5121 * pevent_cmdline_pid - return the pid associated to a given cmdline
5122 * @cmdline: The cmdline structure to get the pid from
5124 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5127 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5129 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5135 * If cmdlines have not been created yet, or cmdline is
5136 * not part of the array, then treat it as a cmdlist instead.
5138 if (!pevent->cmdlines ||
5139 cmdline < pevent->cmdlines ||
5140 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5141 return cmdlist->pid;
5143 return cmdline->pid;
5147 * pevent_data_comm_from_pid - parse the data into the print format
5148 * @s: the trace_seq to write to
5149 * @event: the handle to the event
5150 * @record: the record to read from
5152 * This parses the raw @data using the given @event information and
5153 * writes the print format into the trace_seq.
5155 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5156 struct pevent_record *record)
5158 int print_pretty = 1;
5160 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5161 print_event_fields(s, record->data, record->size, event);
5164 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5165 print_pretty = event->handler(s, record, event,
5169 pretty_print(s, record->data, record->size, event);
5172 trace_seq_terminate(s);
5175 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5177 if (!use_trace_clock)
5180 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5181 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5184 /* trace_clock is setting in tsc or counter mode */
5188 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5189 struct pevent_record *record, bool use_trace_clock)
5191 static const char *spaces = " "; /* 20 spaces */
5192 struct event_format *event;
5194 unsigned long usecs;
5195 unsigned long nsecs;
5197 void *data = record->data;
5202 bool use_usec_format;
5204 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5206 if (use_usec_format) {
5207 secs = record->ts / NSECS_PER_SEC;
5208 nsecs = record->ts - secs * NSECS_PER_SEC;
5211 if (record->size < 0) {
5212 do_warning("ug! negative record size %d", record->size);
5216 type = trace_parse_common_type(pevent, data);
5218 event = pevent_find_event(pevent, type);
5220 do_warning("ug! no event found for type %d", type);
5224 pid = parse_common_pid(pevent, data);
5225 comm = find_cmdline(pevent, pid);
5227 if (pevent->latency_format) {
5228 trace_seq_printf(s, "%8.8s-%-5d %3d",
5229 comm, pid, record->cpu);
5230 pevent_data_lat_fmt(pevent, s, record);
5232 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5234 if (use_usec_format) {
5235 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5239 usecs = (nsecs + 500) / NSECS_PER_USEC;
5243 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
5244 secs, p, usecs, event->name);
5246 trace_seq_printf(s, " %12llu: %s: ",
5247 record->ts, event->name);
5249 /* Space out the event names evenly. */
5250 len = strlen(event->name);
5252 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5254 pevent_event_info(s, event, record);
5257 static int events_id_cmp(const void *a, const void *b)
5259 struct event_format * const * ea = a;
5260 struct event_format * const * eb = b;
5262 if ((*ea)->id < (*eb)->id)
5265 if ((*ea)->id > (*eb)->id)
5271 static int events_name_cmp(const void *a, const void *b)
5273 struct event_format * const * ea = a;
5274 struct event_format * const * eb = b;
5277 res = strcmp((*ea)->name, (*eb)->name);
5281 res = strcmp((*ea)->system, (*eb)->system);
5285 return events_id_cmp(a, b);
5288 static int events_system_cmp(const void *a, const void *b)
5290 struct event_format * const * ea = a;
5291 struct event_format * const * eb = b;
5294 res = strcmp((*ea)->system, (*eb)->system);
5298 res = strcmp((*ea)->name, (*eb)->name);
5302 return events_id_cmp(a, b);
5305 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5307 struct event_format **events;
5308 int (*sort)(const void *a, const void *b);
5310 events = pevent->sort_events;
5312 if (events && pevent->last_type == sort_type)
5316 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5320 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5321 events[pevent->nr_events] = NULL;
5323 pevent->sort_events = events;
5325 /* the internal events are sorted by id */
5326 if (sort_type == EVENT_SORT_ID) {
5327 pevent->last_type = sort_type;
5332 switch (sort_type) {
5334 sort = events_id_cmp;
5336 case EVENT_SORT_NAME:
5337 sort = events_name_cmp;
5339 case EVENT_SORT_SYSTEM:
5340 sort = events_system_cmp;
5346 qsort(events, pevent->nr_events, sizeof(*events), sort);
5347 pevent->last_type = sort_type;
5352 static struct format_field **
5353 get_event_fields(const char *type, const char *name,
5354 int count, struct format_field *list)
5356 struct format_field **fields;
5357 struct format_field *field;
5360 fields = malloc(sizeof(*fields) * (count + 1));
5364 for (field = list; field; field = field->next) {
5365 fields[i++] = field;
5366 if (i == count + 1) {
5367 do_warning("event %s has more %s fields than specified",
5375 do_warning("event %s has less %s fields than specified",
5384 * pevent_event_common_fields - return a list of common fields for an event
5385 * @event: the event to return the common fields of.
5387 * Returns an allocated array of fields. The last item in the array is NULL.
5388 * The array must be freed with free().
5390 struct format_field **pevent_event_common_fields(struct event_format *event)
5392 return get_event_fields("common", event->name,
5393 event->format.nr_common,
5394 event->format.common_fields);
5398 * pevent_event_fields - return a list of event specific fields for an event
5399 * @event: the event to return the fields of.
5401 * Returns an allocated array of fields. The last item in the array is NULL.
5402 * The array must be freed with free().
5404 struct format_field **pevent_event_fields(struct event_format *event)
5406 return get_event_fields("event", event->name,
5407 event->format.nr_fields,
5408 event->format.fields);
5411 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5413 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5415 trace_seq_puts(s, ", ");
5416 print_fields(s, field->next);
5421 static void print_args(struct print_arg *args)
5423 int print_paren = 1;
5426 switch (args->type) {
5431 printf("%s", args->atom.atom);
5434 printf("REC->%s", args->field.name);
5437 printf("__print_flags(");
5438 print_args(args->flags.field);
5439 printf(", %s, ", args->flags.delim);
5441 print_fields(&s, args->flags.flags);
5442 trace_seq_do_printf(&s);
5443 trace_seq_destroy(&s);
5447 printf("__print_symbolic(");
5448 print_args(args->symbol.field);
5451 print_fields(&s, args->symbol.symbols);
5452 trace_seq_do_printf(&s);
5453 trace_seq_destroy(&s);
5457 printf("__print_hex(");
5458 print_args(args->hex.field);
5460 print_args(args->hex.size);
5463 case PRINT_INT_ARRAY:
5464 printf("__print_array(");
5465 print_args(args->int_array.field);
5467 print_args(args->int_array.count);
5469 print_args(args->int_array.el_size);
5474 printf("__get_str(%s)", args->string.string);
5477 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5480 printf("(%s)", args->typecast.type);
5481 print_args(args->typecast.item);
5484 if (strcmp(args->op.op, ":") == 0)
5488 print_args(args->op.left);
5489 printf(" %s ", args->op.op);
5490 print_args(args->op.right);
5495 /* we should warn... */
5500 print_args(args->next);
5504 static void parse_header_field(const char *field,
5505 int *offset, int *size, int mandatory)
5507 unsigned long long save_input_buf_ptr;
5508 unsigned long long save_input_buf_siz;
5512 save_input_buf_ptr = input_buf_ptr;
5513 save_input_buf_siz = input_buf_siz;
5515 if (read_expected(EVENT_ITEM, "field") < 0)
5517 if (read_expected(EVENT_OP, ":") < 0)
5521 if (read_expect_type(EVENT_ITEM, &token) < 0)
5526 * If this is not a mandatory field, then test it first.
5529 if (read_expected(EVENT_ITEM, field) < 0)
5532 if (read_expect_type(EVENT_ITEM, &token) < 0)
5534 if (strcmp(token, field) != 0)
5539 if (read_expected(EVENT_OP, ";") < 0)
5541 if (read_expected(EVENT_ITEM, "offset") < 0)
5543 if (read_expected(EVENT_OP, ":") < 0)
5545 if (read_expect_type(EVENT_ITEM, &token) < 0)
5547 *offset = atoi(token);
5549 if (read_expected(EVENT_OP, ";") < 0)
5551 if (read_expected(EVENT_ITEM, "size") < 0)
5553 if (read_expected(EVENT_OP, ":") < 0)
5555 if (read_expect_type(EVENT_ITEM, &token) < 0)
5557 *size = atoi(token);
5559 if (read_expected(EVENT_OP, ";") < 0)
5561 type = read_token(&token);
5562 if (type != EVENT_NEWLINE) {
5563 /* newer versions of the kernel have a "signed" type */
5564 if (type != EVENT_ITEM)
5567 if (strcmp(token, "signed") != 0)
5572 if (read_expected(EVENT_OP, ":") < 0)
5575 if (read_expect_type(EVENT_ITEM, &token))
5579 if (read_expected(EVENT_OP, ";") < 0)
5582 if (read_expect_type(EVENT_NEWLINE, &token))
5590 input_buf_ptr = save_input_buf_ptr;
5591 input_buf_siz = save_input_buf_siz;
5598 * pevent_parse_header_page - parse the data stored in the header page
5599 * @pevent: the handle to the pevent
5600 * @buf: the buffer storing the header page format string
5601 * @size: the size of @buf
5602 * @long_size: the long size to use if there is no header
5604 * This parses the header page format for information on the
5605 * ring buffer used. The @buf should be copied from
5607 * /sys/kernel/debug/tracing/events/header_page
5609 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5616 * Old kernels did not have header page info.
5617 * Sorry but we just use what we find here in user space.
5619 pevent->header_page_ts_size = sizeof(long long);
5620 pevent->header_page_size_size = long_size;
5621 pevent->header_page_data_offset = sizeof(long long) + long_size;
5622 pevent->old_format = 1;
5625 init_input_buf(buf, size);
5627 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5628 &pevent->header_page_ts_size, 1);
5629 parse_header_field("commit", &pevent->header_page_size_offset,
5630 &pevent->header_page_size_size, 1);
5631 parse_header_field("overwrite", &pevent->header_page_overwrite,
5633 parse_header_field("data", &pevent->header_page_data_offset,
5634 &pevent->header_page_data_size, 1);
5639 static int event_matches(struct event_format *event,
5640 int id, const char *sys_name,
5641 const char *event_name)
5643 if (id >= 0 && id != event->id)
5646 if (event_name && (strcmp(event_name, event->name) != 0))
5649 if (sys_name && (strcmp(sys_name, event->system) != 0))
5655 static void free_handler(struct event_handler *handle)
5657 free((void *)handle->sys_name);
5658 free((void *)handle->event_name);
5662 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5664 struct event_handler *handle, **next;
5666 for (next = &pevent->handlers; *next;
5667 next = &(*next)->next) {
5669 if (event_matches(event, handle->id,
5671 handle->event_name))
5678 pr_stat("overriding event (%d) %s:%s with new print handler",
5679 event->id, event->system, event->name);
5681 event->handler = handle->func;
5682 event->context = handle->context;
5684 *next = handle->next;
5685 free_handler(handle);
5691 * __pevent_parse_format - parse the event format
5692 * @buf: the buffer storing the event format string
5693 * @size: the size of @buf
5694 * @sys: the system the event belongs to
5696 * This parses the event format and creates an event structure
5697 * to quickly parse raw data for a given event.
5699 * These files currently come from:
5701 * /sys/kernel/debug/tracing/events/.../.../format
5703 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5704 struct pevent *pevent, const char *buf,
5705 unsigned long size, const char *sys)
5707 struct event_format *event;
5710 init_input_buf(buf, size);
5712 *eventp = event = alloc_event();
5714 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5716 event->name = event_read_name();
5719 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5720 goto event_alloc_failed;
5723 if (strcmp(sys, "ftrace") == 0) {
5724 event->flags |= EVENT_FL_ISFTRACE;
5726 if (strcmp(event->name, "bprint") == 0)
5727 event->flags |= EVENT_FL_ISBPRINT;
5730 event->id = event_read_id();
5731 if (event->id < 0) {
5732 ret = PEVENT_ERRNO__READ_ID_FAILED;
5734 * This isn't an allocation error actually.
5735 * But as the ID is critical, just bail out.
5737 goto event_alloc_failed;
5740 event->system = strdup(sys);
5741 if (!event->system) {
5742 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5743 goto event_alloc_failed;
5746 /* Add pevent to event so that it can be referenced */
5747 event->pevent = pevent;
5749 ret = event_read_format(event);
5751 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5752 goto event_parse_failed;
5756 * If the event has an override, don't print warnings if the event
5757 * print format fails to parse.
5759 if (pevent && find_event_handle(pevent, event))
5762 ret = event_read_print(event);
5766 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5767 goto event_parse_failed;
5770 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5771 struct format_field *field;
5772 struct print_arg *arg, **list;
5774 /* old ftrace had no args */
5775 list = &event->print_fmt.args;
5776 for (field = event->format.fields; field; field = field->next) {
5779 event->flags |= EVENT_FL_FAILED;
5780 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5782 arg->type = PRINT_FIELD;
5783 arg->field.name = strdup(field->name);
5784 if (!arg->field.name) {
5785 event->flags |= EVENT_FL_FAILED;
5787 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5789 arg->field.field = field;
5799 event->flags |= EVENT_FL_FAILED;
5803 free(event->system);
5810 static enum pevent_errno
5811 __pevent_parse_event(struct pevent *pevent,
5812 struct event_format **eventp,
5813 const char *buf, unsigned long size,
5816 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5817 struct event_format *event = *eventp;
5822 if (pevent && add_event(pevent, event)) {
5823 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5824 goto event_add_failed;
5827 #define PRINT_ARGS 0
5828 if (PRINT_ARGS && event->print_fmt.args)
5829 print_args(event->print_fmt.args);
5834 pevent_free_format(event);
5839 * pevent_parse_format - parse the event format
5840 * @pevent: the handle to the pevent
5841 * @eventp: returned format
5842 * @buf: the buffer storing the event format string
5843 * @size: the size of @buf
5844 * @sys: the system the event belongs to
5846 * This parses the event format and creates an event structure
5847 * to quickly parse raw data for a given event.
5849 * These files currently come from:
5851 * /sys/kernel/debug/tracing/events/.../.../format
5853 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5854 struct event_format **eventp,
5856 unsigned long size, const char *sys)
5858 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5862 * pevent_parse_event - parse the event format
5863 * @pevent: the handle to the pevent
5864 * @buf: the buffer storing the event format string
5865 * @size: the size of @buf
5866 * @sys: the system the event belongs to
5868 * This parses the event format and creates an event structure
5869 * to quickly parse raw data for a given event.
5871 * These files currently come from:
5873 * /sys/kernel/debug/tracing/events/.../.../format
5875 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5876 unsigned long size, const char *sys)
5878 struct event_format *event = NULL;
5879 return __pevent_parse_event(pevent, &event, buf, size, sys);
5883 #define _PE(code, str) str
5884 static const char * const pevent_error_str[] = {
5889 int pevent_strerror(struct pevent *pevent __maybe_unused,
5890 enum pevent_errno errnum, char *buf, size_t buflen)
5896 msg = strerror_r(errnum, buf, buflen);
5898 size_t len = strlen(msg);
5899 memcpy(buf, msg, min(buflen - 1, len));
5900 *(buf + min(buflen - 1, len)) = '\0';
5905 if (errnum <= __PEVENT_ERRNO__START ||
5906 errnum >= __PEVENT_ERRNO__END)
5909 idx = errnum - __PEVENT_ERRNO__START - 1;
5910 msg = pevent_error_str[idx];
5911 snprintf(buf, buflen, "%s", msg);
5916 int get_field_val(struct trace_seq *s, struct format_field *field,
5917 const char *name, struct pevent_record *record,
5918 unsigned long long *val, int err)
5922 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5926 if (pevent_read_number_field(field, record->data, val)) {
5928 trace_seq_printf(s, " %s=INVALID", name);
5936 * pevent_get_field_raw - return the raw pointer into the data field
5937 * @s: The seq to print to on error
5938 * @event: the event that the field is for
5939 * @name: The name of the field
5940 * @record: The record with the field name.
5941 * @len: place to store the field length.
5942 * @err: print default error if failed.
5944 * Returns a pointer into record->data of the field and places
5945 * the length of the field in @len.
5947 * On failure, it returns NULL.
5949 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5950 const char *name, struct pevent_record *record,
5953 struct format_field *field;
5954 void *data = record->data;
5961 field = pevent_find_field(event, name);
5965 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5969 /* Allow @len to be NULL */
5973 offset = field->offset;
5974 if (field->flags & FIELD_IS_DYNAMIC) {
5975 offset = pevent_read_number(event->pevent,
5976 data + offset, field->size);
5977 *len = offset >> 16;
5982 return data + offset;
5986 * pevent_get_field_val - find a field and return its value
5987 * @s: The seq to print to on error
5988 * @event: the event that the field is for
5989 * @name: The name of the field
5990 * @record: The record with the field name.
5991 * @val: place to store the value of the field.
5992 * @err: print default error if failed.
5994 * Returns 0 on success -1 on field not found.
5996 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5997 const char *name, struct pevent_record *record,
5998 unsigned long long *val, int err)
6000 struct format_field *field;
6005 field = pevent_find_field(event, name);
6007 return get_field_val(s, field, name, record, val, err);
6011 * pevent_get_common_field_val - find a common field and return its value
6012 * @s: The seq to print to on error
6013 * @event: the event that the field is for
6014 * @name: The name of the field
6015 * @record: The record with the field name.
6016 * @val: place to store the value of the field.
6017 * @err: print default error if failed.
6019 * Returns 0 on success -1 on field not found.
6021 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6022 const char *name, struct pevent_record *record,
6023 unsigned long long *val, int err)
6025 struct format_field *field;
6030 field = pevent_find_common_field(event, name);
6032 return get_field_val(s, field, name, record, val, err);
6036 * pevent_get_any_field_val - find a any field and return its value
6037 * @s: The seq to print to on error
6038 * @event: the event that the field is for
6039 * @name: The name of the field
6040 * @record: The record with the field name.
6041 * @val: place to store the value of the field.
6042 * @err: print default error if failed.
6044 * Returns 0 on success -1 on field not found.
6046 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6047 const char *name, struct pevent_record *record,
6048 unsigned long long *val, int err)
6050 struct format_field *field;
6055 field = pevent_find_any_field(event, name);
6057 return get_field_val(s, field, name, record, val, err);
6061 * pevent_print_num_field - print a field and a format
6062 * @s: The seq to print to
6063 * @fmt: The printf format to print the field with.
6064 * @event: the event that the field is for
6065 * @name: The name of the field
6066 * @record: The record with the field name.
6067 * @err: print default error if failed.
6069 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6071 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6072 struct event_format *event, const char *name,
6073 struct pevent_record *record, int err)
6075 struct format_field *field = pevent_find_field(event, name);
6076 unsigned long long val;
6081 if (pevent_read_number_field(field, record->data, &val))
6084 return trace_seq_printf(s, fmt, val);
6088 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6093 * pevent_print_func_field - print a field and a format for function pointers
6094 * @s: The seq to print to
6095 * @fmt: The printf format to print the field with.
6096 * @event: the event that the field is for
6097 * @name: The name of the field
6098 * @record: The record with the field name.
6099 * @err: print default error if failed.
6101 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6103 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6104 struct event_format *event, const char *name,
6105 struct pevent_record *record, int err)
6107 struct format_field *field = pevent_find_field(event, name);
6108 struct pevent *pevent = event->pevent;
6109 unsigned long long val;
6110 struct func_map *func;
6116 if (pevent_read_number_field(field, record->data, &val))
6119 func = find_func(pevent, val);
6122 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6124 sprintf(tmp, "0x%08llx", val);
6126 return trace_seq_printf(s, fmt, tmp);
6130 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6134 static void free_func_handle(struct pevent_function_handler *func)
6136 struct pevent_func_params *params;
6140 while (func->params) {
6141 params = func->params;
6142 func->params = params->next;
6150 * pevent_register_print_function - register a helper function
6151 * @pevent: the handle to the pevent
6152 * @func: the function to process the helper function
6153 * @ret_type: the return type of the helper function
6154 * @name: the name of the helper function
6155 * @parameters: A list of enum pevent_func_arg_type
6157 * Some events may have helper functions in the print format arguments.
6158 * This allows a plugin to dynamically create a way to process one
6159 * of these functions.
6161 * The @parameters is a variable list of pevent_func_arg_type enums that
6162 * must end with PEVENT_FUNC_ARG_VOID.
6164 int pevent_register_print_function(struct pevent *pevent,
6165 pevent_func_handler func,
6166 enum pevent_func_arg_type ret_type,
6169 struct pevent_function_handler *func_handle;
6170 struct pevent_func_params **next_param;
6171 struct pevent_func_params *param;
6172 enum pevent_func_arg_type type;
6176 func_handle = find_func_handler(pevent, name);
6179 * This is most like caused by the users own
6180 * plugins updating the function. This overrides the
6183 pr_stat("override of function helper '%s'", name);
6184 remove_func_handler(pevent, name);
6187 func_handle = calloc(1, sizeof(*func_handle));
6189 do_warning("Failed to allocate function handler");
6190 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6193 func_handle->ret_type = ret_type;
6194 func_handle->name = strdup(name);
6195 func_handle->func = func;
6196 if (!func_handle->name) {
6197 do_warning("Failed to allocate function name");
6199 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6202 next_param = &(func_handle->params);
6205 type = va_arg(ap, enum pevent_func_arg_type);
6206 if (type == PEVENT_FUNC_ARG_VOID)
6209 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6210 do_warning("Invalid argument type %d", type);
6211 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6215 param = malloc(sizeof(*param));
6217 do_warning("Failed to allocate function param");
6218 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6224 *next_param = param;
6225 next_param = &(param->next);
6227 func_handle->nr_args++;
6231 func_handle->next = pevent->func_handlers;
6232 pevent->func_handlers = func_handle;
6237 free_func_handle(func_handle);
6242 * pevent_unregister_print_function - unregister a helper function
6243 * @pevent: the handle to the pevent
6244 * @func: the function to process the helper function
6245 * @name: the name of the helper function
6247 * This function removes existing print handler for function @name.
6249 * Returns 0 if the handler was removed successully, -1 otherwise.
6251 int pevent_unregister_print_function(struct pevent *pevent,
6252 pevent_func_handler func, char *name)
6254 struct pevent_function_handler *func_handle;
6256 func_handle = find_func_handler(pevent, name);
6257 if (func_handle && func_handle->func == func) {
6258 remove_func_handler(pevent, name);
6264 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6265 const char *sys_name,
6266 const char *event_name)
6268 struct event_format *event;
6272 event = pevent_find_event(pevent, id);
6275 if (event_name && (strcmp(event_name, event->name) != 0))
6277 if (sys_name && (strcmp(sys_name, event->system) != 0))
6280 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6288 * pevent_register_event_handler - register a way to parse an event
6289 * @pevent: the handle to the pevent
6290 * @id: the id of the event to register
6291 * @sys_name: the system name the event belongs to
6292 * @event_name: the name of the event
6293 * @func: the function to call to parse the event information
6294 * @context: the data to be passed to @func
6296 * This function allows a developer to override the parsing of
6297 * a given event. If for some reason the default print format
6298 * is not sufficient, this function will register a function
6299 * for an event to be used to parse the data instead.
6301 * If @id is >= 0, then it is used to find the event.
6302 * else @sys_name and @event_name are used.
6304 int pevent_register_event_handler(struct pevent *pevent, int id,
6305 const char *sys_name, const char *event_name,
6306 pevent_event_handler_func func, void *context)
6308 struct event_format *event;
6309 struct event_handler *handle;
6311 event = pevent_search_event(pevent, id, sys_name, event_name);
6315 pr_stat("overriding event (%d) %s:%s with new print handler",
6316 event->id, event->system, event->name);
6318 event->handler = func;
6319 event->context = context;
6323 /* Save for later use. */
6324 handle = calloc(1, sizeof(*handle));
6326 do_warning("Failed to allocate event handler");
6327 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6332 handle->event_name = strdup(event_name);
6334 handle->sys_name = strdup(sys_name);
6336 if ((event_name && !handle->event_name) ||
6337 (sys_name && !handle->sys_name)) {
6338 do_warning("Failed to allocate event/sys name");
6339 free((void *)handle->event_name);
6340 free((void *)handle->sys_name);
6342 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6345 handle->func = func;
6346 handle->next = pevent->handlers;
6347 pevent->handlers = handle;
6348 handle->context = context;
6353 static int handle_matches(struct event_handler *handler, int id,
6354 const char *sys_name, const char *event_name,
6355 pevent_event_handler_func func, void *context)
6357 if (id >= 0 && id != handler->id)
6360 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6363 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6366 if (func != handler->func || context != handler->context)
6373 * pevent_unregister_event_handler - unregister an existing event handler
6374 * @pevent: the handle to the pevent
6375 * @id: the id of the event to unregister
6376 * @sys_name: the system name the handler belongs to
6377 * @event_name: the name of the event handler
6378 * @func: the function to call to parse the event information
6379 * @context: the data to be passed to @func
6381 * This function removes existing event handler (parser).
6383 * If @id is >= 0, then it is used to find the event.
6384 * else @sys_name and @event_name are used.
6386 * Returns 0 if handler was removed successfully, -1 if event was not found.
6388 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6389 const char *sys_name, const char *event_name,
6390 pevent_event_handler_func func, void *context)
6392 struct event_format *event;
6393 struct event_handler *handle;
6394 struct event_handler **next;
6396 event = pevent_search_event(pevent, id, sys_name, event_name);
6400 if (event->handler == func && event->context == context) {
6401 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6402 event->id, event->system, event->name);
6404 event->handler = NULL;
6405 event->context = NULL;
6410 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6412 if (handle_matches(handle, id, sys_name, event_name,
6420 *next = handle->next;
6421 free_handler(handle);
6427 * pevent_alloc - create a pevent handle
6429 struct pevent *pevent_alloc(void)
6431 struct pevent *pevent = calloc(1, sizeof(*pevent));
6434 pevent->ref_count = 1;
6439 void pevent_ref(struct pevent *pevent)
6441 pevent->ref_count++;
6444 void pevent_free_format_field(struct format_field *field)
6451 static void free_format_fields(struct format_field *field)
6453 struct format_field *next;
6457 pevent_free_format_field(field);
6462 static void free_formats(struct format *format)
6464 free_format_fields(format->common_fields);
6465 free_format_fields(format->fields);
6468 void pevent_free_format(struct event_format *event)
6471 free(event->system);
6473 free_formats(&event->format);
6475 free(event->print_fmt.format);
6476 free_args(event->print_fmt.args);
6482 * pevent_free - free a pevent handle
6483 * @pevent: the pevent handle to free
6485 void pevent_free(struct pevent *pevent)
6487 struct cmdline_list *cmdlist, *cmdnext;
6488 struct func_list *funclist, *funcnext;
6489 struct printk_list *printklist, *printknext;
6490 struct pevent_function_handler *func_handler;
6491 struct event_handler *handle;
6497 cmdlist = pevent->cmdlist;
6498 funclist = pevent->funclist;
6499 printklist = pevent->printklist;
6501 pevent->ref_count--;
6502 if (pevent->ref_count)
6505 if (pevent->cmdlines) {
6506 for (i = 0; i < pevent->cmdline_count; i++)
6507 free(pevent->cmdlines[i].comm);
6508 free(pevent->cmdlines);
6512 cmdnext = cmdlist->next;
6513 free(cmdlist->comm);
6518 if (pevent->func_map) {
6519 for (i = 0; i < (int)pevent->func_count; i++) {
6520 free(pevent->func_map[i].func);
6521 free(pevent->func_map[i].mod);
6523 free(pevent->func_map);
6527 funcnext = funclist->next;
6528 free(funclist->func);
6529 free(funclist->mod);
6531 funclist = funcnext;
6534 while (pevent->func_handlers) {
6535 func_handler = pevent->func_handlers;
6536 pevent->func_handlers = func_handler->next;
6537 free_func_handle(func_handler);
6540 if (pevent->printk_map) {
6541 for (i = 0; i < (int)pevent->printk_count; i++)
6542 free(pevent->printk_map[i].printk);
6543 free(pevent->printk_map);
6546 while (printklist) {
6547 printknext = printklist->next;
6548 free(printklist->printk);
6550 printklist = printknext;
6553 for (i = 0; i < pevent->nr_events; i++)
6554 pevent_free_format(pevent->events[i]);
6556 while (pevent->handlers) {
6557 handle = pevent->handlers;
6558 pevent->handlers = handle->next;
6559 free_handler(handle);
6562 free(pevent->trace_clock);
6563 free(pevent->events);
6564 free(pevent->sort_events);
6569 void pevent_unref(struct pevent *pevent)
6571 pevent_free(pevent);