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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by
23 * - Copyright (C) 2009 Frederic Weisbecker,
24 * Frederic Weisbecker gave his permission to relicense the code to
25 * the Lesser General Public License.
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 static void init_input_buf(const char *buf, unsigned long long size)
61 const char *pevent_get_input_buf(void)
66 unsigned long long pevent_get_input_buf_ptr(void)
71 struct event_handler {
72 struct event_handler *next;
75 const char *event_name;
76 pevent_event_handler_func func;
80 struct pevent_func_params {
81 struct pevent_func_params *next;
82 enum pevent_func_arg_type type;
85 struct pevent_function_handler {
86 struct pevent_function_handler *next;
87 enum pevent_func_arg_type ret_type;
89 pevent_func_handler func;
90 struct pevent_func_params *params;
94 static unsigned long long
95 process_defined_func(struct trace_seq *s, void *data, int size,
96 struct event_format *event, struct print_arg *arg);
98 static void free_func_handle(struct pevent_function_handler *func);
101 * pevent_buffer_init - init buffer for parsing
102 * @buf: buffer to parse
103 * @size: the size of the buffer
105 * For use with pevent_read_token(), this initializes the internal
106 * buffer that pevent_read_token() will parse.
108 void pevent_buffer_init(const char *buf, unsigned long long size)
110 init_input_buf(buf, size);
113 void breakpoint(void)
119 struct print_arg *alloc_arg(void)
121 struct print_arg *arg;
123 arg = malloc_or_die(sizeof(*arg));
126 memset(arg, 0, sizeof(*arg));
136 static int cmdline_cmp(const void *a, const void *b)
138 const struct cmdline *ca = a;
139 const struct cmdline *cb = b;
141 if (ca->pid < cb->pid)
143 if (ca->pid > cb->pid)
149 struct cmdline_list {
150 struct cmdline_list *next;
155 static int cmdline_init(struct pevent *pevent)
157 struct cmdline_list *cmdlist = pevent->cmdlist;
158 struct cmdline_list *item;
159 struct cmdline *cmdlines;
162 cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
166 cmdlines[i].pid = cmdlist->pid;
167 cmdlines[i].comm = cmdlist->comm;
170 cmdlist = cmdlist->next;
174 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
176 pevent->cmdlines = cmdlines;
177 pevent->cmdlist = NULL;
182 static char *find_cmdline(struct pevent *pevent, int pid)
184 const struct cmdline *comm;
190 if (!pevent->cmdlines)
191 cmdline_init(pevent);
195 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
196 sizeof(*pevent->cmdlines), cmdline_cmp);
204 * pevent_pid_is_registered - return if a pid has a cmdline registered
205 * @pevent: handle for the pevent
206 * @pid: The pid to check if it has a cmdline registered with.
208 * Returns 1 if the pid has a cmdline mapped to it
211 int pevent_pid_is_registered(struct pevent *pevent, int pid)
213 const struct cmdline *comm;
219 if (!pevent->cmdlines)
220 cmdline_init(pevent);
224 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
225 sizeof(*pevent->cmdlines), cmdline_cmp);
233 * If the command lines have been converted to an array, then
234 * we must add this pid. This is much slower than when cmdlines
235 * are added before the array is initialized.
237 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
239 struct cmdline *cmdlines = pevent->cmdlines;
240 const struct cmdline *cmdline;
246 /* avoid duplicates */
249 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
250 sizeof(*pevent->cmdlines), cmdline_cmp);
256 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
262 cmdlines[pevent->cmdline_count].pid = pid;
263 cmdlines[pevent->cmdline_count].comm = strdup(comm);
264 if (!cmdlines[pevent->cmdline_count].comm)
267 if (cmdlines[pevent->cmdline_count].comm)
268 pevent->cmdline_count++;
270 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
271 pevent->cmdlines = cmdlines;
277 * pevent_register_comm - register a pid / comm mapping
278 * @pevent: handle for the pevent
279 * @comm: the command line to register
280 * @pid: the pid to map the command line to
282 * This adds a mapping to search for command line names with
283 * a given pid. The comm is duplicated.
285 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
287 struct cmdline_list *item;
289 if (pevent->cmdlines)
290 return add_new_comm(pevent, comm, pid);
292 item = malloc_or_die(sizeof(*item));
293 item->comm = strdup(comm);
297 item->next = pevent->cmdlist;
299 pevent->cmdlist = item;
300 pevent->cmdline_count++;
306 unsigned long long addr;
312 struct func_list *next;
313 unsigned long long addr;
318 static int func_cmp(const void *a, const void *b)
320 const struct func_map *fa = a;
321 const struct func_map *fb = b;
323 if (fa->addr < fb->addr)
325 if (fa->addr > fb->addr)
332 * We are searching for a record in between, not an exact
335 static int func_bcmp(const void *a, const void *b)
337 const struct func_map *fa = a;
338 const struct func_map *fb = b;
340 if ((fa->addr == fb->addr) ||
342 (fa->addr > fb->addr &&
343 fa->addr < (fb+1)->addr))
346 if (fa->addr < fb->addr)
352 static int func_map_init(struct pevent *pevent)
354 struct func_list *funclist;
355 struct func_list *item;
356 struct func_map *func_map;
359 func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
360 funclist = pevent->funclist;
364 func_map[i].func = funclist->func;
365 func_map[i].addr = funclist->addr;
366 func_map[i].mod = funclist->mod;
369 funclist = funclist->next;
373 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
376 * Add a special record at the end.
378 func_map[pevent->func_count].func = NULL;
379 func_map[pevent->func_count].addr = 0;
380 func_map[pevent->func_count].mod = NULL;
382 pevent->func_map = func_map;
383 pevent->funclist = NULL;
388 static struct func_map *
389 find_func(struct pevent *pevent, unsigned long long addr)
391 struct func_map *func;
394 if (!pevent->func_map)
395 func_map_init(pevent);
399 func = bsearch(&key, pevent->func_map, pevent->func_count,
400 sizeof(*pevent->func_map), func_bcmp);
406 * pevent_find_function - find a function by a given address
407 * @pevent: handle for the pevent
408 * @addr: the address to find the function with
410 * Returns a pointer to the function stored that has the given
411 * address. Note, the address does not have to be exact, it
412 * will select the function that would contain the address.
414 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
416 struct func_map *map;
418 map = find_func(pevent, addr);
426 * pevent_find_function_address - find a function address by a given address
427 * @pevent: handle for the pevent
428 * @addr: the address to find the function with
430 * Returns the address the function starts at. This can be used in
431 * conjunction with pevent_find_function to print both the function
432 * name and the function offset.
435 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
437 struct func_map *map;
439 map = find_func(pevent, addr);
447 * pevent_register_function - register a function with a given address
448 * @pevent: handle for the pevent
449 * @function: the function name to register
450 * @addr: the address the function starts at
451 * @mod: the kernel module the function may be in (NULL for none)
453 * This registers a function name with an address and module.
454 * The @func passed in is duplicated.
456 int pevent_register_function(struct pevent *pevent, char *func,
457 unsigned long long addr, char *mod)
459 struct func_list *item;
461 item = malloc_or_die(sizeof(*item));
463 item->next = pevent->funclist;
464 item->func = strdup(func);
466 item->mod = strdup(mod);
471 if (!item->func || (mod && !item->mod))
474 pevent->funclist = item;
475 pevent->func_count++;
481 * pevent_print_funcs - print out the stored functions
482 * @pevent: handle for the pevent
484 * This prints out the stored functions.
486 void pevent_print_funcs(struct pevent *pevent)
490 if (!pevent->func_map)
491 func_map_init(pevent);
493 for (i = 0; i < (int)pevent->func_count; i++) {
495 pevent->func_map[i].addr,
496 pevent->func_map[i].func);
497 if (pevent->func_map[i].mod)
498 printf(" [%s]\n", pevent->func_map[i].mod);
505 unsigned long long addr;
510 struct printk_list *next;
511 unsigned long long addr;
515 static int printk_cmp(const void *a, const void *b)
517 const struct printk_map *pa = a;
518 const struct printk_map *pb = b;
520 if (pa->addr < pb->addr)
522 if (pa->addr > pb->addr)
528 static void printk_map_init(struct pevent *pevent)
530 struct printk_list *printklist;
531 struct printk_list *item;
532 struct printk_map *printk_map;
535 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
537 printklist = pevent->printklist;
541 printk_map[i].printk = printklist->printk;
542 printk_map[i].addr = printklist->addr;
545 printklist = printklist->next;
549 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
551 pevent->printk_map = printk_map;
552 pevent->printklist = NULL;
555 static struct printk_map *
556 find_printk(struct pevent *pevent, unsigned long long addr)
558 struct printk_map *printk;
559 struct printk_map key;
561 if (!pevent->printk_map)
562 printk_map_init(pevent);
566 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
567 sizeof(*pevent->printk_map), printk_cmp);
573 * pevent_register_print_string - register a string by its address
574 * @pevent: handle for the pevent
575 * @fmt: the string format to register
576 * @addr: the address the string was located at
578 * This registers a string by the address it was stored in the kernel.
579 * The @fmt passed in is duplicated.
581 int pevent_register_print_string(struct pevent *pevent, char *fmt,
582 unsigned long long addr)
584 struct printk_list *item;
586 item = malloc_or_die(sizeof(*item));
588 item->next = pevent->printklist;
589 item->printk = strdup(fmt);
595 pevent->printklist = item;
596 pevent->printk_count++;
602 * pevent_print_printk - print out the stored strings
603 * @pevent: handle for the pevent
605 * This prints the string formats that were stored.
607 void pevent_print_printk(struct pevent *pevent)
611 if (!pevent->printk_map)
612 printk_map_init(pevent);
614 for (i = 0; i < (int)pevent->printk_count; i++) {
615 printf("%016llx %s\n",
616 pevent->printk_map[i].addr,
617 pevent->printk_map[i].printk);
621 static struct event_format *alloc_event(void)
623 struct event_format *event;
625 event = malloc(sizeof(*event));
628 memset(event, 0, sizeof(*event));
633 static void add_event(struct pevent *pevent, struct event_format *event)
637 pevent->events = realloc(pevent->events, sizeof(event) *
638 (pevent->nr_events + 1));
640 die("Can not allocate events");
642 for (i = 0; i < pevent->nr_events; i++) {
643 if (pevent->events[i]->id > event->id)
646 if (i < pevent->nr_events)
647 memmove(&pevent->events[i + 1],
649 sizeof(event) * (pevent->nr_events - i));
651 pevent->events[i] = event;
654 event->pevent = pevent;
657 static int event_item_type(enum event_type type)
660 case EVENT_ITEM ... EVENT_SQUOTE:
662 case EVENT_ERROR ... EVENT_DELIM:
668 static void free_flag_sym(struct print_flag_sym *fsym)
670 struct print_flag_sym *next;
681 static void free_arg(struct print_arg *arg)
683 struct print_arg *farg;
690 free(arg->atom.atom);
693 free(arg->field.name);
696 free_arg(arg->flags.field);
697 free(arg->flags.delim);
698 free_flag_sym(arg->flags.flags);
701 free_arg(arg->symbol.field);
702 free_flag_sym(arg->symbol.symbols);
705 free_arg(arg->hex.field);
706 free_arg(arg->hex.size);
709 free(arg->typecast.type);
710 free_arg(arg->typecast.item);
714 free(arg->string.string);
716 case PRINT_DYNAMIC_ARRAY:
717 free(arg->dynarray.index);
721 free_arg(arg->op.left);
722 free_arg(arg->op.right);
725 while (arg->func.args) {
726 farg = arg->func.args;
727 arg->func.args = farg->next;
740 static enum event_type get_type(int ch)
743 return EVENT_NEWLINE;
746 if (isalnum(ch) || ch == '_')
754 if (ch == '(' || ch == ')' || ch == ',')
760 static int __read_char(void)
762 if (input_buf_ptr >= input_buf_siz)
765 return input_buf[input_buf_ptr++];
768 static int __peek_char(void)
770 if (input_buf_ptr >= input_buf_siz)
773 return input_buf[input_buf_ptr];
777 * pevent_peek_char - peek at the next character that will be read
779 * Returns the next character read, or -1 if end of buffer.
781 int pevent_peek_char(void)
783 return __peek_char();
786 static int extend_token(char **tok, char *buf, int size)
788 char *newtok = realloc(*tok, size);
805 static enum event_type force_token(const char *str, char **tok);
807 static enum event_type __read_token(char **tok)
810 int ch, last_ch, quote_ch, next_ch;
813 enum event_type type;
823 if (type == EVENT_NONE)
831 *tok = malloc_or_die(2);
839 next_ch = __peek_char();
840 if (next_ch == '>') {
841 buf[i++] = __read_char();
854 buf[i++] = __read_char();
866 default: /* what should we do instead? */
876 buf[i++] = __read_char();
881 /* don't keep quotes */
887 if (i == (BUFSIZ - 1)) {
891 if (extend_token(tok, buf, tok_size) < 0)
898 /* the '\' '\' will cancel itself */
899 if (ch == '\\' && last_ch == '\\')
901 } while (ch != quote_ch || last_ch == '\\');
902 /* remove the last quote */
906 * For strings (double quotes) check the next token.
907 * If it is another string, concatinate the two.
909 if (type == EVENT_DQUOTE) {
910 unsigned long long save_input_buf_ptr = input_buf_ptr;
914 } while (isspace(ch));
917 input_buf_ptr = save_input_buf_ptr;
922 case EVENT_ERROR ... EVENT_SPACE:
928 while (get_type(__peek_char()) == type) {
929 if (i == (BUFSIZ - 1)) {
933 if (extend_token(tok, buf, tok_size) < 0)
943 if (extend_token(tok, buf, tok_size + i + 1) < 0)
946 if (type == EVENT_ITEM) {
948 * Older versions of the kernel has a bug that
949 * creates invalid symbols and will break the mac80211
950 * parsing. This is a work around to that bug.
952 * See Linux kernel commit:
953 * 811cb50baf63461ce0bdb234927046131fc7fa8b
955 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
958 return force_token("\"\%s\" ", tok);
959 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
962 return force_token("\" sta:%pM\" ", tok);
963 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
966 return force_token("\" vif:%p(%d)\" ", tok);
973 static enum event_type force_token(const char *str, char **tok)
975 const char *save_input_buf;
976 unsigned long long save_input_buf_ptr;
977 unsigned long long save_input_buf_siz;
978 enum event_type type;
980 /* save off the current input pointers */
981 save_input_buf = input_buf;
982 save_input_buf_ptr = input_buf_ptr;
983 save_input_buf_siz = input_buf_siz;
985 init_input_buf(str, strlen(str));
987 type = __read_token(tok);
989 /* reset back to original token */
990 input_buf = save_input_buf;
991 input_buf_ptr = save_input_buf_ptr;
992 input_buf_siz = save_input_buf_siz;
997 static void free_token(char *tok)
1003 static enum event_type read_token(char **tok)
1005 enum event_type type;
1008 type = __read_token(tok);
1009 if (type != EVENT_SPACE)
1021 * pevent_read_token - access to utilites to use the pevent parser
1022 * @tok: The token to return
1024 * This will parse tokens from the string given by
1025 * pevent_init_data().
1027 * Returns the token type.
1029 enum event_type pevent_read_token(char **tok)
1031 return read_token(tok);
1035 * pevent_free_token - free a token returned by pevent_read_token
1036 * @token: the token to free
1038 void pevent_free_token(char *token)
1044 static enum event_type read_token_item(char **tok)
1046 enum event_type type;
1049 type = __read_token(tok);
1050 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1061 static int test_type(enum event_type type, enum event_type expect)
1063 if (type != expect) {
1064 do_warning("Error: expected type %d but read %d",
1071 static int test_type_token(enum event_type type, const char *token,
1072 enum event_type expect, const char *expect_tok)
1074 if (type != expect) {
1075 do_warning("Error: expected type %d but read %d",
1080 if (strcmp(token, expect_tok) != 0) {
1081 do_warning("Error: expected '%s' but read '%s'",
1088 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1090 enum event_type type;
1093 type = read_token(tok);
1095 type = read_token_item(tok);
1096 return test_type(type, expect);
1099 static int read_expect_type(enum event_type expect, char **tok)
1101 return __read_expect_type(expect, tok, 1);
1104 static int __read_expected(enum event_type expect, const char *str,
1107 enum event_type type;
1112 type = read_token(&token);
1114 type = read_token_item(&token);
1116 ret = test_type_token(type, token, expect, str);
1123 static int read_expected(enum event_type expect, const char *str)
1125 return __read_expected(expect, str, 1);
1128 static int read_expected_item(enum event_type expect, const char *str)
1130 return __read_expected(expect, str, 0);
1133 static char *event_read_name(void)
1137 if (read_expected(EVENT_ITEM, "name") < 0)
1140 if (read_expected(EVENT_OP, ":") < 0)
1143 if (read_expect_type(EVENT_ITEM, &token) < 0)
1153 static int event_read_id(void)
1158 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1161 if (read_expected(EVENT_OP, ":") < 0)
1164 if (read_expect_type(EVENT_ITEM, &token) < 0)
1167 id = strtoul(token, NULL, 0);
1176 static int field_is_string(struct format_field *field)
1178 if ((field->flags & FIELD_IS_ARRAY) &&
1179 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1180 strstr(field->type, "s8")))
1186 static int field_is_dynamic(struct format_field *field)
1188 if (strncmp(field->type, "__data_loc", 10) == 0)
1194 static int field_is_long(struct format_field *field)
1196 /* includes long long */
1197 if (strstr(field->type, "long"))
1203 static int event_read_fields(struct event_format *event, struct format_field **fields)
1205 struct format_field *field = NULL;
1206 enum event_type type;
1212 type = read_token(&token);
1213 if (type == EVENT_NEWLINE) {
1220 if (test_type_token(type, token, EVENT_ITEM, "field"))
1224 type = read_token(&token);
1226 * The ftrace fields may still use the "special" name.
1229 if (event->flags & EVENT_FL_ISFTRACE &&
1230 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1232 type = read_token(&token);
1235 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1239 if (read_expect_type(EVENT_ITEM, &token) < 0)
1244 field = malloc_or_die(sizeof(*field));
1245 memset(field, 0, sizeof(*field));
1246 field->event = event;
1248 /* read the rest of the type */
1250 type = read_token(&token);
1251 if (type == EVENT_ITEM ||
1252 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1254 * Some of the ftrace fields are broken and have
1255 * an illegal "." in them.
1257 (event->flags & EVENT_FL_ISFTRACE &&
1258 type == EVENT_OP && strcmp(token, ".") == 0)) {
1260 if (strcmp(token, "*") == 0)
1261 field->flags |= FIELD_IS_POINTER;
1265 new_type = realloc(field->type,
1266 strlen(field->type) +
1267 strlen(last_token) + 2);
1272 field->type = new_type;
1273 strcat(field->type, " ");
1274 strcat(field->type, last_token);
1277 field->type = last_token;
1286 die("no type found");
1289 field->name = last_token;
1291 if (test_type(type, EVENT_OP))
1294 if (strcmp(token, "[") == 0) {
1295 enum event_type last_type = type;
1296 char *brackets = token;
1300 field->flags |= FIELD_IS_ARRAY;
1302 type = read_token(&token);
1304 if (type == EVENT_ITEM)
1305 field->arraylen = strtoul(token, NULL, 0);
1307 field->arraylen = 0;
1309 while (strcmp(token, "]") != 0) {
1310 if (last_type == EVENT_ITEM &&
1317 new_brackets = realloc(brackets,
1319 strlen(token) + len);
1320 if (!new_brackets) {
1324 brackets = new_brackets;
1326 strcat(brackets, " ");
1327 strcat(brackets, token);
1328 /* We only care about the last token */
1329 field->arraylen = strtoul(token, NULL, 0);
1331 type = read_token(&token);
1332 if (type == EVENT_NONE) {
1333 die("failed to find token");
1340 new_brackets = realloc(brackets, strlen(brackets) + 2);
1341 if (!new_brackets) {
1345 brackets = new_brackets;
1346 strcat(brackets, "]");
1348 /* add brackets to type */
1350 type = read_token(&token);
1352 * If the next token is not an OP, then it is of
1353 * the format: type [] item;
1355 if (type == EVENT_ITEM) {
1357 new_type = realloc(field->type,
1358 strlen(field->type) +
1359 strlen(field->name) +
1360 strlen(brackets) + 2);
1365 field->type = new_type;
1366 strcat(field->type, " ");
1367 strcat(field->type, field->name);
1368 free_token(field->name);
1369 strcat(field->type, brackets);
1370 field->name = token;
1371 type = read_token(&token);
1374 new_type = realloc(field->type,
1375 strlen(field->type) +
1376 strlen(brackets) + 1);
1381 field->type = new_type;
1382 strcat(field->type, brackets);
1387 if (field_is_string(field))
1388 field->flags |= FIELD_IS_STRING;
1389 if (field_is_dynamic(field))
1390 field->flags |= FIELD_IS_DYNAMIC;
1391 if (field_is_long(field))
1392 field->flags |= FIELD_IS_LONG;
1394 if (test_type_token(type, token, EVENT_OP, ";"))
1398 if (read_expected(EVENT_ITEM, "offset") < 0)
1401 if (read_expected(EVENT_OP, ":") < 0)
1404 if (read_expect_type(EVENT_ITEM, &token))
1406 field->offset = strtoul(token, NULL, 0);
1409 if (read_expected(EVENT_OP, ";") < 0)
1412 if (read_expected(EVENT_ITEM, "size") < 0)
1415 if (read_expected(EVENT_OP, ":") < 0)
1418 if (read_expect_type(EVENT_ITEM, &token))
1420 field->size = strtoul(token, NULL, 0);
1423 if (read_expected(EVENT_OP, ";") < 0)
1426 type = read_token(&token);
1427 if (type != EVENT_NEWLINE) {
1428 /* newer versions of the kernel have a "signed" type */
1429 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1434 if (read_expected(EVENT_OP, ":") < 0)
1437 if (read_expect_type(EVENT_ITEM, &token))
1440 /* add signed type */
1443 if (read_expected(EVENT_OP, ";") < 0)
1446 if (read_expect_type(EVENT_NEWLINE, &token))
1452 if (field->flags & FIELD_IS_ARRAY) {
1453 if (field->arraylen)
1454 field->elementsize = field->size / field->arraylen;
1455 else if (field->flags & FIELD_IS_STRING)
1456 field->elementsize = 1;
1458 field->elementsize = event->pevent->long_size;
1460 field->elementsize = field->size;
1463 fields = &field->next;
1480 static int event_read_format(struct event_format *event)
1485 if (read_expected_item(EVENT_ITEM, "format") < 0)
1488 if (read_expected(EVENT_OP, ":") < 0)
1491 if (read_expect_type(EVENT_NEWLINE, &token))
1495 ret = event_read_fields(event, &event->format.common_fields);
1498 event->format.nr_common = ret;
1500 ret = event_read_fields(event, &event->format.fields);
1503 event->format.nr_fields = ret;
1512 static enum event_type
1513 process_arg_token(struct event_format *event, struct print_arg *arg,
1514 char **tok, enum event_type type);
1516 static enum event_type
1517 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1519 enum event_type type;
1522 type = read_token(&token);
1525 return process_arg_token(event, arg, tok, type);
1528 static enum event_type
1529 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1531 static enum event_type
1532 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1534 struct print_arg *arg, *left, *right;
1535 enum event_type type;
1540 right = alloc_arg();
1542 arg->type = PRINT_OP;
1543 arg->op.left = left;
1544 arg->op.right = right;
1547 type = process_arg(event, left, &token);
1550 /* Handle other operations in the arguments */
1551 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1552 type = process_op(event, left, &token);
1556 if (test_type_token(type, token, EVENT_OP, ":"))
1561 type = process_arg(event, right, &token);
1563 top->op.right = arg;
1569 /* Top may point to itself */
1570 top->op.right = NULL;
1576 static enum event_type
1577 process_array(struct event_format *event, struct print_arg *top, char **tok)
1579 struct print_arg *arg;
1580 enum event_type type;
1586 type = process_arg(event, arg, &token);
1587 if (test_type_token(type, token, EVENT_OP, "]"))
1590 top->op.right = arg;
1593 type = read_token_item(&token);
1605 static int get_op_prio(char *op)
1619 /* '>>' and '<<' are 8 */
1623 /* '==' and '!=' are 10 */
1633 do_warning("unknown op '%c'", op[0]);
1637 if (strcmp(op, "++") == 0 ||
1638 strcmp(op, "--") == 0) {
1640 } else if (strcmp(op, ">>") == 0 ||
1641 strcmp(op, "<<") == 0) {
1643 } else if (strcmp(op, ">=") == 0 ||
1644 strcmp(op, "<=") == 0) {
1646 } else if (strcmp(op, "==") == 0 ||
1647 strcmp(op, "!=") == 0) {
1649 } else if (strcmp(op, "&&") == 0) {
1651 } else if (strcmp(op, "||") == 0) {
1654 do_warning("unknown op '%s'", op);
1660 static int set_op_prio(struct print_arg *arg)
1663 /* single ops are the greatest */
1664 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1667 arg->op.prio = get_op_prio(arg->op.op);
1669 return arg->op.prio;
1672 /* Note, *tok does not get freed, but will most likely be saved */
1673 static enum event_type
1674 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1676 struct print_arg *left, *right = NULL;
1677 enum event_type type;
1680 /* the op is passed in via tok */
1683 if (arg->type == PRINT_OP && !arg->op.left) {
1684 /* handle single op */
1686 die("bad op token %s", token);
1696 do_warning("bad op token %s", token);
1701 /* make an empty left */
1703 left->type = PRINT_NULL;
1704 arg->op.left = left;
1706 right = alloc_arg();
1707 arg->op.right = right;
1709 /* do not free the token, it belongs to an op */
1711 type = process_arg(event, right, tok);
1713 } else if (strcmp(token, "?") == 0) {
1716 /* copy the top arg to the left */
1719 arg->type = PRINT_OP;
1721 arg->op.left = left;
1724 type = process_cond(event, arg, tok);
1726 } else if (strcmp(token, ">>") == 0 ||
1727 strcmp(token, "<<") == 0 ||
1728 strcmp(token, "&") == 0 ||
1729 strcmp(token, "|") == 0 ||
1730 strcmp(token, "&&") == 0 ||
1731 strcmp(token, "||") == 0 ||
1732 strcmp(token, "-") == 0 ||
1733 strcmp(token, "+") == 0 ||
1734 strcmp(token, "*") == 0 ||
1735 strcmp(token, "^") == 0 ||
1736 strcmp(token, "/") == 0 ||
1737 strcmp(token, "<") == 0 ||
1738 strcmp(token, ">") == 0 ||
1739 strcmp(token, "==") == 0 ||
1740 strcmp(token, "!=") == 0) {
1744 /* copy the top arg to the left */
1747 arg->type = PRINT_OP;
1749 arg->op.left = left;
1751 if (set_op_prio(arg) == -1) {
1752 event->flags |= EVENT_FL_FAILED;
1753 /* arg->op.op (= token) will be freed at out_free */
1758 type = read_token_item(&token);
1761 /* could just be a type pointer */
1762 if ((strcmp(arg->op.op, "*") == 0) &&
1763 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1766 if (left->type != PRINT_ATOM)
1767 die("bad pointer type");
1768 new_atom = realloc(left->atom.atom,
1769 strlen(left->atom.atom) + 3);
1773 left->atom.atom = new_atom;
1774 strcat(left->atom.atom, " *");
1782 right = alloc_arg();
1783 type = process_arg_token(event, right, tok, type);
1784 arg->op.right = right;
1786 } else if (strcmp(token, "[") == 0) {
1791 arg->type = PRINT_OP;
1793 arg->op.left = left;
1797 type = process_array(event, arg, tok);
1800 do_warning("unknown op '%s'", token);
1801 event->flags |= EVENT_FL_FAILED;
1802 /* the arg is now the left side */
1806 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1809 /* higher prios need to be closer to the root */
1810 prio = get_op_prio(*tok);
1812 if (prio > arg->op.prio)
1813 return process_op(event, arg, tok);
1815 return process_op(event, right, tok);
1826 static enum event_type
1827 process_entry(struct event_format *event __unused, struct print_arg *arg,
1830 enum event_type type;
1834 if (read_expected(EVENT_OP, "->") < 0)
1837 if (read_expect_type(EVENT_ITEM, &token) < 0)
1841 arg->type = PRINT_FIELD;
1842 arg->field.name = field;
1844 if (is_flag_field) {
1845 arg->field.field = pevent_find_any_field(event, arg->field.name);
1846 arg->field.field->flags |= FIELD_IS_FLAG;
1848 } else if (is_symbolic_field) {
1849 arg->field.field = pevent_find_any_field(event, arg->field.name);
1850 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1851 is_symbolic_field = 0;
1854 type = read_token(&token);
1866 static char *arg_eval (struct print_arg *arg);
1868 static unsigned long long
1869 eval_type_str(unsigned long long val, const char *type, int pointer)
1879 if (type[len-1] != '*') {
1880 do_warning("pointer expected with non pointer type");
1884 ref = malloc_or_die(len);
1885 memcpy(ref, type, len);
1887 /* chop off the " *" */
1890 val = eval_type_str(val, ref, 0);
1895 /* check if this is a pointer */
1896 if (type[len - 1] == '*')
1899 /* Try to figure out the arg size*/
1900 if (strncmp(type, "struct", 6) == 0)
1904 if (strcmp(type, "u8") == 0)
1907 if (strcmp(type, "u16") == 0)
1908 return val & 0xffff;
1910 if (strcmp(type, "u32") == 0)
1911 return val & 0xffffffff;
1913 if (strcmp(type, "u64") == 0 ||
1914 strcmp(type, "s64"))
1917 if (strcmp(type, "s8") == 0)
1918 return (unsigned long long)(char)val & 0xff;
1920 if (strcmp(type, "s16") == 0)
1921 return (unsigned long long)(short)val & 0xffff;
1923 if (strcmp(type, "s32") == 0)
1924 return (unsigned long long)(int)val & 0xffffffff;
1926 if (strncmp(type, "unsigned ", 9) == 0) {
1931 if (strcmp(type, "char") == 0) {
1933 return (unsigned long long)(char)val & 0xff;
1938 if (strcmp(type, "short") == 0) {
1940 return (unsigned long long)(short)val & 0xffff;
1942 return val & 0xffff;
1945 if (strcmp(type, "int") == 0) {
1947 return (unsigned long long)(int)val & 0xffffffff;
1949 return val & 0xffffffff;
1956 * Try to figure out the type.
1958 static unsigned long long
1959 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1961 if (arg->type != PRINT_TYPE)
1962 die("expected type argument");
1964 return eval_type_str(val, arg->typecast.type, pointer);
1967 static int arg_num_eval(struct print_arg *arg, long long *val)
1969 long long left, right;
1972 switch (arg->type) {
1974 *val = strtoll(arg->atom.atom, NULL, 0);
1977 ret = arg_num_eval(arg->typecast.item, val);
1980 *val = eval_type(*val, arg, 0);
1983 switch (arg->op.op[0]) {
1985 ret = arg_num_eval(arg->op.left, &left);
1988 ret = arg_num_eval(arg->op.right, &right);
1992 *val = left || right;
1994 *val = left | right;
1997 ret = arg_num_eval(arg->op.left, &left);
2000 ret = arg_num_eval(arg->op.right, &right);
2004 *val = left && right;
2006 *val = left & right;
2009 ret = arg_num_eval(arg->op.left, &left);
2012 ret = arg_num_eval(arg->op.right, &right);
2015 switch (arg->op.op[1]) {
2017 *val = left < right;
2020 *val = left << right;
2023 *val = left <= right;
2026 do_warning("unknown op '%s'", arg->op.op);
2031 ret = arg_num_eval(arg->op.left, &left);
2034 ret = arg_num_eval(arg->op.right, &right);
2037 switch (arg->op.op[1]) {
2039 *val = left > right;
2042 *val = left >> right;
2045 *val = left >= right;
2048 do_warning("unknown op '%s'", arg->op.op);
2053 ret = arg_num_eval(arg->op.left, &left);
2056 ret = arg_num_eval(arg->op.right, &right);
2060 if (arg->op.op[1] != '=') {
2061 do_warning("unknown op '%s'", arg->op.op);
2064 *val = left == right;
2067 ret = arg_num_eval(arg->op.left, &left);
2070 ret = arg_num_eval(arg->op.right, &right);
2074 switch (arg->op.op[1]) {
2076 *val = left != right;
2079 do_warning("unknown op '%s'", arg->op.op);
2084 /* check for negative */
2085 if (arg->op.left->type == PRINT_NULL)
2088 ret = arg_num_eval(arg->op.left, &left);
2091 ret = arg_num_eval(arg->op.right, &right);
2094 *val = left - right;
2097 if (arg->op.left->type == PRINT_NULL)
2100 ret = arg_num_eval(arg->op.left, &left);
2103 ret = arg_num_eval(arg->op.right, &right);
2106 *val = left + right;
2109 do_warning("unknown op '%s'", arg->op.op);
2115 case PRINT_FIELD ... PRINT_SYMBOL:
2119 do_warning("invalid eval type %d", arg->type);
2126 static char *arg_eval (struct print_arg *arg)
2129 static char buf[20];
2131 switch (arg->type) {
2133 return arg->atom.atom;
2135 return arg_eval(arg->typecast.item);
2137 if (!arg_num_eval(arg, &val))
2139 sprintf(buf, "%lld", val);
2143 case PRINT_FIELD ... PRINT_SYMBOL:
2147 die("invalid eval type %d", arg->type);
2154 static enum event_type
2155 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2157 enum event_type type;
2158 struct print_arg *arg = NULL;
2159 struct print_flag_sym *field;
2165 type = read_token_item(&token);
2166 if (test_type_token(type, token, EVENT_OP, "{"))
2172 type = process_arg(event, arg, &token);
2174 if (type == EVENT_OP)
2175 type = process_op(event, arg, &token);
2177 if (type == EVENT_ERROR)
2180 if (test_type_token(type, token, EVENT_DELIM, ","))
2183 field = malloc_or_die(sizeof(*field));
2184 memset(field, 0, sizeof(*field));
2186 value = arg_eval(arg);
2189 field->value = strdup(value);
2190 if (field->value == NULL)
2197 type = process_arg(event, arg, &token);
2198 if (test_type_token(type, token, EVENT_OP, "}"))
2201 value = arg_eval(arg);
2204 field->str = strdup(value);
2205 if (field->str == NULL)
2211 list = &field->next;
2214 type = read_token_item(&token);
2215 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2228 static enum event_type
2229 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2231 struct print_arg *field;
2232 enum event_type type;
2235 memset(arg, 0, sizeof(*arg));
2236 arg->type = PRINT_FLAGS;
2238 field = alloc_arg();
2240 type = process_arg(event, field, &token);
2242 /* Handle operations in the first argument */
2243 while (type == EVENT_OP)
2244 type = process_op(event, field, &token);
2246 if (test_type_token(type, token, EVENT_DELIM, ","))
2250 arg->flags.field = field;
2252 type = read_token_item(&token);
2253 if (event_item_type(type)) {
2254 arg->flags.delim = token;
2255 type = read_token_item(&token);
2258 if (test_type_token(type, token, EVENT_DELIM, ","))
2261 type = process_fields(event, &arg->flags.flags, &token);
2262 if (test_type_token(type, token, EVENT_DELIM, ")"))
2266 type = read_token_item(tok);
2275 static enum event_type
2276 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2278 struct print_arg *field;
2279 enum event_type type;
2282 memset(arg, 0, sizeof(*arg));
2283 arg->type = PRINT_SYMBOL;
2285 field = alloc_arg();
2287 type = process_arg(event, field, &token);
2288 if (test_type_token(type, token, EVENT_DELIM, ","))
2291 arg->symbol.field = field;
2293 type = process_fields(event, &arg->symbol.symbols, &token);
2294 if (test_type_token(type, token, EVENT_DELIM, ")"))
2298 type = read_token_item(tok);
2307 static enum event_type
2308 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2310 struct print_arg *field;
2311 enum event_type type;
2314 memset(arg, 0, sizeof(*arg));
2315 arg->type = PRINT_HEX;
2317 field = alloc_arg();
2318 type = process_arg(event, field, &token);
2320 if (test_type_token(type, token, EVENT_DELIM, ","))
2323 arg->hex.field = field;
2327 field = alloc_arg();
2328 type = process_arg(event, field, &token);
2330 if (test_type_token(type, token, EVENT_DELIM, ")"))
2333 arg->hex.size = field;
2336 type = read_token_item(tok);
2346 static enum event_type
2347 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2349 struct format_field *field;
2350 enum event_type type;
2353 memset(arg, 0, sizeof(*arg));
2354 arg->type = PRINT_DYNAMIC_ARRAY;
2357 * The item within the parenthesis is another field that holds
2358 * the index into where the array starts.
2360 type = read_token(&token);
2362 if (type != EVENT_ITEM)
2365 /* Find the field */
2367 field = pevent_find_field(event, token);
2371 arg->dynarray.field = field;
2372 arg->dynarray.index = 0;
2374 if (read_expected(EVENT_DELIM, ")") < 0)
2378 type = read_token_item(&token);
2380 if (type != EVENT_OP || strcmp(token, "[") != 0)
2385 type = process_arg(event, arg, &token);
2386 if (type == EVENT_ERROR)
2389 if (!test_type_token(type, token, EVENT_OP, "]"))
2393 type = read_token_item(tok);
2404 static enum event_type
2405 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2407 struct print_arg *item_arg;
2408 enum event_type type;
2411 type = process_arg(event, arg, &token);
2413 if (type == EVENT_ERROR)
2416 if (type == EVENT_OP)
2417 type = process_op(event, arg, &token);
2419 if (type == EVENT_ERROR)
2422 if (test_type_token(type, token, EVENT_DELIM, ")"))
2426 type = read_token_item(&token);
2429 * If the next token is an item or another open paren, then
2430 * this was a typecast.
2432 if (event_item_type(type) ||
2433 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2435 /* make this a typecast and contine */
2437 /* prevous must be an atom */
2438 if (arg->type != PRINT_ATOM)
2439 die("previous needed to be PRINT_ATOM");
2441 item_arg = alloc_arg();
2443 arg->type = PRINT_TYPE;
2444 arg->typecast.type = arg->atom.atom;
2445 arg->typecast.item = item_arg;
2446 type = process_arg_token(event, item_arg, &token, type);
2460 static enum event_type
2461 process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2463 enum event_type type;
2466 if (read_expect_type(EVENT_ITEM, &token) < 0)
2469 arg->type = PRINT_STRING;
2470 arg->string.string = token;
2471 arg->string.offset = -1;
2473 if (read_expected(EVENT_DELIM, ")") < 0)
2476 type = read_token(&token);
2488 static struct pevent_function_handler *
2489 find_func_handler(struct pevent *pevent, char *func_name)
2491 struct pevent_function_handler *func;
2493 for (func = pevent->func_handlers; func; func = func->next) {
2494 if (strcmp(func->name, func_name) == 0)
2501 static void remove_func_handler(struct pevent *pevent, char *func_name)
2503 struct pevent_function_handler *func;
2504 struct pevent_function_handler **next;
2506 next = &pevent->func_handlers;
2507 while ((func = *next)) {
2508 if (strcmp(func->name, func_name) == 0) {
2510 free_func_handle(func);
2517 static enum event_type
2518 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2519 struct print_arg *arg, char **tok)
2521 struct print_arg **next_arg;
2522 struct print_arg *farg;
2523 enum event_type type;
2528 arg->type = PRINT_FUNC;
2529 arg->func.func = func;
2533 next_arg = &(arg->func.args);
2534 for (i = 0; i < func->nr_args; i++) {
2536 type = process_arg(event, farg, &token);
2537 if (i < (func->nr_args - 1))
2542 if (test_type_token(type, token, EVENT_DELIM, test)) {
2549 next_arg = &(farg->next);
2553 type = read_token(&token);
2559 static enum event_type
2560 process_function(struct event_format *event, struct print_arg *arg,
2561 char *token, char **tok)
2563 struct pevent_function_handler *func;
2565 if (strcmp(token, "__print_flags") == 0) {
2568 return process_flags(event, arg, tok);
2570 if (strcmp(token, "__print_symbolic") == 0) {
2572 is_symbolic_field = 1;
2573 return process_symbols(event, arg, tok);
2575 if (strcmp(token, "__print_hex") == 0) {
2577 return process_hex(event, arg, tok);
2579 if (strcmp(token, "__get_str") == 0) {
2581 return process_str(event, arg, tok);
2583 if (strcmp(token, "__get_dynamic_array") == 0) {
2585 return process_dynamic_array(event, arg, tok);
2588 func = find_func_handler(event->pevent, token);
2591 return process_func_handler(event, func, arg, tok);
2594 do_warning("function %s not defined", token);
2599 static enum event_type
2600 process_arg_token(struct event_format *event, struct print_arg *arg,
2601 char **tok, enum event_type type)
2610 if (strcmp(token, "REC") == 0) {
2612 type = process_entry(event, arg, &token);
2616 /* test the next token */
2617 type = read_token_item(&token);
2620 * If the next token is a parenthesis, then this
2623 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2626 /* this will free atom. */
2627 type = process_function(event, arg, atom, &token);
2630 /* atoms can be more than one token long */
2631 while (type == EVENT_ITEM) {
2633 new_atom = realloc(atom,
2634 strlen(atom) + strlen(token) + 2);
2643 strcat(atom, token);
2645 type = read_token_item(&token);
2648 arg->type = PRINT_ATOM;
2649 arg->atom.atom = atom;
2654 arg->type = PRINT_ATOM;
2655 arg->atom.atom = token;
2656 type = read_token_item(&token);
2659 if (strcmp(token, "(") == 0) {
2661 type = process_paren(event, arg, &token);
2665 /* handle single ops */
2666 arg->type = PRINT_OP;
2668 arg->op.left = NULL;
2669 type = process_op(event, arg, &token);
2671 /* On error, the op is freed */
2672 if (type == EVENT_ERROR)
2675 /* return error type if errored */
2678 case EVENT_ERROR ... EVENT_NEWLINE:
2680 die("unexpected type %d", type);
2687 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2689 enum event_type type = EVENT_ERROR;
2690 struct print_arg *arg;
2695 if (type == EVENT_NEWLINE) {
2696 type = read_token_item(&token);
2702 type = process_arg(event, arg, &token);
2704 if (type == EVENT_ERROR) {
2713 if (type == EVENT_OP) {
2714 type = process_op(event, arg, &token);
2716 if (type == EVENT_ERROR) {
2725 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2732 } while (type != EVENT_NONE);
2734 if (type != EVENT_NONE && type != EVENT_ERROR)
2740 static int event_read_print(struct event_format *event)
2742 enum event_type type;
2746 if (read_expected_item(EVENT_ITEM, "print") < 0)
2749 if (read_expected(EVENT_ITEM, "fmt") < 0)
2752 if (read_expected(EVENT_OP, ":") < 0)
2755 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2759 event->print_fmt.format = token;
2760 event->print_fmt.args = NULL;
2762 /* ok to have no arg */
2763 type = read_token_item(&token);
2765 if (type == EVENT_NONE)
2768 /* Handle concatenation of print lines */
2769 if (type == EVENT_DQUOTE) {
2772 cat = malloc_or_die(strlen(event->print_fmt.format) +
2774 strcpy(cat, event->print_fmt.format);
2777 free_token(event->print_fmt.format);
2778 event->print_fmt.format = NULL;
2783 if (test_type_token(type, token, EVENT_DELIM, ","))
2788 ret = event_read_print_args(event, &event->print_fmt.args);
2800 * pevent_find_common_field - return a common field by event
2801 * @event: handle for the event
2802 * @name: the name of the common field to return
2804 * Returns a common field from the event by the given @name.
2805 * This only searchs the common fields and not all field.
2807 struct format_field *
2808 pevent_find_common_field(struct event_format *event, const char *name)
2810 struct format_field *format;
2812 for (format = event->format.common_fields;
2813 format; format = format->next) {
2814 if (strcmp(format->name, name) == 0)
2822 * pevent_find_field - find a non-common field
2823 * @event: handle for the event
2824 * @name: the name of the non-common field
2826 * Returns a non-common field by the given @name.
2827 * This does not search common fields.
2829 struct format_field *
2830 pevent_find_field(struct event_format *event, const char *name)
2832 struct format_field *format;
2834 for (format = event->format.fields;
2835 format; format = format->next) {
2836 if (strcmp(format->name, name) == 0)
2844 * pevent_find_any_field - find any field by name
2845 * @event: handle for the event
2846 * @name: the name of the field
2848 * Returns a field by the given @name.
2849 * This searchs the common field names first, then
2850 * the non-common ones if a common one was not found.
2852 struct format_field *
2853 pevent_find_any_field(struct event_format *event, const char *name)
2855 struct format_field *format;
2857 format = pevent_find_common_field(event, name);
2860 return pevent_find_field(event, name);
2864 * pevent_read_number - read a number from data
2865 * @pevent: handle for the pevent
2866 * @ptr: the raw data
2867 * @size: the size of the data that holds the number
2869 * Returns the number (converted to host) from the
2872 unsigned long long pevent_read_number(struct pevent *pevent,
2873 const void *ptr, int size)
2877 return *(unsigned char *)ptr;
2879 return data2host2(pevent, ptr);
2881 return data2host4(pevent, ptr);
2883 return data2host8(pevent, ptr);
2891 * pevent_read_number_field - read a number from data
2892 * @field: a handle to the field
2893 * @data: the raw data to read
2894 * @value: the value to place the number in
2896 * Reads raw data according to a field offset and size,
2897 * and translates it into @value.
2899 * Returns 0 on success, -1 otherwise.
2901 int pevent_read_number_field(struct format_field *field, const void *data,
2902 unsigned long long *value)
2906 switch (field->size) {
2911 *value = pevent_read_number(field->event->pevent,
2912 data + field->offset, field->size);
2919 static int get_common_info(struct pevent *pevent,
2920 const char *type, int *offset, int *size)
2922 struct event_format *event;
2923 struct format_field *field;
2926 * All events should have the same common elements.
2927 * Pick any event to find where the type is;
2929 if (!pevent->events)
2930 die("no event_list!");
2932 event = pevent->events[0];
2933 field = pevent_find_common_field(event, type);
2937 *offset = field->offset;
2938 *size = field->size;
2943 static int __parse_common(struct pevent *pevent, void *data,
2944 int *size, int *offset, const char *name)
2949 ret = get_common_info(pevent, name, offset, size);
2953 return pevent_read_number(pevent, data + *offset, *size);
2956 static int trace_parse_common_type(struct pevent *pevent, void *data)
2958 return __parse_common(pevent, data,
2959 &pevent->type_size, &pevent->type_offset,
2963 static int parse_common_pid(struct pevent *pevent, void *data)
2965 return __parse_common(pevent, data,
2966 &pevent->pid_size, &pevent->pid_offset,
2970 static int parse_common_pc(struct pevent *pevent, void *data)
2972 return __parse_common(pevent, data,
2973 &pevent->pc_size, &pevent->pc_offset,
2974 "common_preempt_count");
2977 static int parse_common_flags(struct pevent *pevent, void *data)
2979 return __parse_common(pevent, data,
2980 &pevent->flags_size, &pevent->flags_offset,
2984 static int parse_common_lock_depth(struct pevent *pevent, void *data)
2986 return __parse_common(pevent, data,
2987 &pevent->ld_size, &pevent->ld_offset,
2988 "common_lock_depth");
2991 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2993 return __parse_common(pevent, data,
2994 &pevent->ld_size, &pevent->ld_offset,
2995 "common_migrate_disable");
2998 static int events_id_cmp(const void *a, const void *b);
3001 * pevent_find_event - find an event by given id
3002 * @pevent: a handle to the pevent
3003 * @id: the id of the event
3005 * Returns an event that has a given @id.
3007 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3009 struct event_format **eventptr;
3010 struct event_format key;
3011 struct event_format *pkey = &key;
3013 /* Check cache first */
3014 if (pevent->last_event && pevent->last_event->id == id)
3015 return pevent->last_event;
3019 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3020 sizeof(*pevent->events), events_id_cmp);
3023 pevent->last_event = *eventptr;
3031 * pevent_find_event_by_name - find an event by given name
3032 * @pevent: a handle to the pevent
3033 * @sys: the system name to search for
3034 * @name: the name of the event to search for
3036 * This returns an event with a given @name and under the system
3037 * @sys. If @sys is NULL the first event with @name is returned.
3039 struct event_format *
3040 pevent_find_event_by_name(struct pevent *pevent,
3041 const char *sys, const char *name)
3043 struct event_format *event;
3046 if (pevent->last_event &&
3047 strcmp(pevent->last_event->name, name) == 0 &&
3048 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3049 return pevent->last_event;
3051 for (i = 0; i < pevent->nr_events; i++) {
3052 event = pevent->events[i];
3053 if (strcmp(event->name, name) == 0) {
3056 if (strcmp(event->system, sys) == 0)
3060 if (i == pevent->nr_events)
3063 pevent->last_event = event;
3067 static unsigned long long
3068 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3070 struct pevent *pevent = event->pevent;
3071 unsigned long long val = 0;
3072 unsigned long long left, right;
3073 struct print_arg *typearg = NULL;
3074 struct print_arg *larg;
3075 unsigned long offset;
3076 unsigned int field_size;
3078 switch (arg->type) {
3083 return strtoull(arg->atom.atom, NULL, 0);
3085 if (!arg->field.field) {
3086 arg->field.field = pevent_find_any_field(event, arg->field.name);
3087 if (!arg->field.field)
3088 die("field %s not found", arg->field.name);
3090 /* must be a number */
3091 val = pevent_read_number(pevent, data + arg->field.field->offset,
3092 arg->field.field->size);
3099 val = eval_num_arg(data, size, event, arg->typecast.item);
3100 return eval_type(val, arg, 0);
3107 val = process_defined_func(&s, data, size, event, arg);
3108 trace_seq_destroy(&s);
3112 if (strcmp(arg->op.op, "[") == 0) {
3114 * Arrays are special, since we don't want
3115 * to read the arg as is.
3117 right = eval_num_arg(data, size, event, arg->op.right);
3119 /* handle typecasts */
3120 larg = arg->op.left;
3121 while (larg->type == PRINT_TYPE) {
3124 larg = larg->typecast.item;
3127 /* Default to long size */
3128 field_size = pevent->long_size;
3130 switch (larg->type) {
3131 case PRINT_DYNAMIC_ARRAY:
3132 offset = pevent_read_number(pevent,
3133 data + larg->dynarray.field->offset,
3134 larg->dynarray.field->size);
3135 if (larg->dynarray.field->elementsize)
3136 field_size = larg->dynarray.field->elementsize;
3138 * The actual length of the dynamic array is stored
3139 * in the top half of the field, and the offset
3140 * is in the bottom half of the 32 bit field.
3146 if (!larg->field.field) {
3148 pevent_find_any_field(event, larg->field.name);
3149 if (!larg->field.field)
3150 die("field %s not found", larg->field.name);
3152 field_size = larg->field.field->elementsize;
3153 offset = larg->field.field->offset +
3154 right * larg->field.field->elementsize;
3157 goto default_op; /* oops, all bets off */
3159 val = pevent_read_number(pevent,
3160 data + offset, field_size);
3162 val = eval_type(val, typearg, 1);
3164 } else if (strcmp(arg->op.op, "?") == 0) {
3165 left = eval_num_arg(data, size, event, arg->op.left);
3166 arg = arg->op.right;
3168 val = eval_num_arg(data, size, event, arg->op.left);
3170 val = eval_num_arg(data, size, event, arg->op.right);
3174 left = eval_num_arg(data, size, event, arg->op.left);
3175 right = eval_num_arg(data, size, event, arg->op.right);
3176 switch (arg->op.op[0]) {
3178 switch (arg->op.op[1]) {
3183 val = left != right;
3186 die("unknown op '%s'", arg->op.op);
3194 val = left || right;
3200 val = left && right;
3205 switch (arg->op.op[1]) {
3210 val = left << right;
3213 val = left <= right;
3216 die("unknown op '%s'", arg->op.op);
3220 switch (arg->op.op[1]) {
3225 val = left >> right;
3228 val = left >= right;
3231 die("unknown op '%s'", arg->op.op);
3235 if (arg->op.op[1] != '=')
3236 die("unknown op '%s'", arg->op.op);
3237 val = left == right;
3252 die("unknown op '%s'", arg->op.op);
3255 default: /* not sure what to do there */
3263 unsigned long long value;
3266 static const struct flag flags[] = {
3267 { "HI_SOFTIRQ", 0 },
3268 { "TIMER_SOFTIRQ", 1 },
3269 { "NET_TX_SOFTIRQ", 2 },
3270 { "NET_RX_SOFTIRQ", 3 },
3271 { "BLOCK_SOFTIRQ", 4 },
3272 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3273 { "TASKLET_SOFTIRQ", 6 },
3274 { "SCHED_SOFTIRQ", 7 },
3275 { "HRTIMER_SOFTIRQ", 8 },
3276 { "RCU_SOFTIRQ", 9 },
3278 { "HRTIMER_NORESTART", 0 },
3279 { "HRTIMER_RESTART", 1 },
3282 static unsigned long long eval_flag(const char *flag)
3287 * Some flags in the format files do not get converted.
3288 * If the flag is not numeric, see if it is something that
3289 * we already know about.
3291 if (isdigit(flag[0]))
3292 return strtoull(flag, NULL, 0);
3294 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3295 if (strcmp(flags[i].name, flag) == 0)
3296 return flags[i].value;
3301 static void print_str_to_seq(struct trace_seq *s, const char *format,
3302 int len_arg, const char *str)
3305 trace_seq_printf(s, format, len_arg, str);
3307 trace_seq_printf(s, format, str);
3310 static void print_str_arg(struct trace_seq *s, void *data, int size,
3311 struct event_format *event, const char *format,
3312 int len_arg, struct print_arg *arg)
3314 struct pevent *pevent = event->pevent;
3315 struct print_flag_sym *flag;
3316 struct format_field *field;
3317 unsigned long long val, fval;
3324 switch (arg->type) {
3329 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3332 field = arg->field.field;
3334 field = pevent_find_any_field(event, arg->field.name);
3336 die("field %s not found", arg->field.name);
3337 arg->field.field = field;
3339 /* Zero sized fields, mean the rest of the data */
3340 len = field->size ? : size - field->offset;
3343 * Some events pass in pointers. If this is not an array
3344 * and the size is the same as long_size, assume that it
3347 if (!(field->flags & FIELD_IS_ARRAY) &&
3348 field->size == pevent->long_size) {
3349 addr = *(unsigned long *)(data + field->offset);
3350 trace_seq_printf(s, "%lx", addr);
3353 str = malloc_or_die(len + 1);
3354 memcpy(str, data + field->offset, len);
3356 print_str_to_seq(s, format, len_arg, str);
3360 val = eval_num_arg(data, size, event, arg->flags.field);
3362 for (flag = arg->flags.flags; flag; flag = flag->next) {
3363 fval = eval_flag(flag->value);
3364 if (!val && !fval) {
3365 print_str_to_seq(s, format, len_arg, flag->str);
3368 if (fval && (val & fval) == fval) {
3369 if (print && arg->flags.delim)
3370 trace_seq_puts(s, arg->flags.delim);
3371 print_str_to_seq(s, format, len_arg, flag->str);
3378 val = eval_num_arg(data, size, event, arg->symbol.field);
3379 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3380 fval = eval_flag(flag->value);
3382 print_str_to_seq(s, format, len_arg, flag->str);
3388 field = arg->hex.field->field.field;
3390 str = arg->hex.field->field.name;
3391 field = pevent_find_any_field(event, str);
3393 die("field %s not found", str);
3394 arg->hex.field->field.field = field;
3396 hex = data + field->offset;
3397 len = eval_num_arg(data, size, event, arg->hex.size);
3398 for (i = 0; i < len; i++) {
3400 trace_seq_putc(s, ' ');
3401 trace_seq_printf(s, "%02x", hex[i]);
3407 case PRINT_STRING: {
3410 if (arg->string.offset == -1) {
3411 struct format_field *f;
3413 f = pevent_find_any_field(event, arg->string.string);
3414 arg->string.offset = f->offset;
3416 str_offset = data2host4(pevent, data + arg->string.offset);
3417 str_offset &= 0xffff;
3418 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3422 print_str_to_seq(s, format, len_arg, arg->string.string);
3426 * The only op for string should be ? :
3428 if (arg->op.op[0] != '?')
3430 val = eval_num_arg(data, size, event, arg->op.left);
3432 print_str_arg(s, data, size, event,
3433 format, len_arg, arg->op.right->op.left);
3435 print_str_arg(s, data, size, event,
3436 format, len_arg, arg->op.right->op.right);
3439 process_defined_func(s, data, size, event, arg);
3447 static unsigned long long
3448 process_defined_func(struct trace_seq *s, void *data, int size,
3449 struct event_format *event, struct print_arg *arg)
3451 struct pevent_function_handler *func_handle = arg->func.func;
3452 struct pevent_func_params *param;
3453 unsigned long long *args;
3454 unsigned long long ret;
3455 struct print_arg *farg;
3456 struct trace_seq str;
3458 struct save_str *next;
3460 } *strings = NULL, *string;
3463 if (!func_handle->nr_args) {
3464 ret = (*func_handle->func)(s, NULL);
3468 farg = arg->func.args;
3469 param = func_handle->params;
3471 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3472 for (i = 0; i < func_handle->nr_args; i++) {
3473 switch (param->type) {
3474 case PEVENT_FUNC_ARG_INT:
3475 case PEVENT_FUNC_ARG_LONG:
3476 case PEVENT_FUNC_ARG_PTR:
3477 args[i] = eval_num_arg(data, size, event, farg);
3479 case PEVENT_FUNC_ARG_STRING:
3480 trace_seq_init(&str);
3481 print_str_arg(&str, data, size, event, "%s", -1, farg);
3482 trace_seq_terminate(&str);
3483 string = malloc_or_die(sizeof(*string));
3484 string->next = strings;
3485 string->str = strdup(str.buffer);
3489 args[i] = (uintptr_t)string->str;
3491 trace_seq_destroy(&str);
3495 * Something went totally wrong, this is not
3496 * an input error, something in this code broke.
3498 die("Unexpected end of arguments\n");
3502 param = param->next;
3505 ret = (*func_handle->func)(s, args);
3509 strings = string->next;
3515 /* TBD : handle return type here */
3519 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3521 struct pevent *pevent = event->pevent;
3522 struct format_field *field, *ip_field;
3523 struct print_arg *args, *arg, **next;
3524 unsigned long long ip, val;
3529 field = pevent->bprint_buf_field;
3530 ip_field = pevent->bprint_ip_field;
3533 field = pevent_find_field(event, "buf");
3535 die("can't find buffer field for binary printk");
3536 ip_field = pevent_find_field(event, "ip");
3538 die("can't find ip field for binary printk");
3539 pevent->bprint_buf_field = field;
3540 pevent->bprint_ip_field = ip_field;
3543 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3546 * The first arg is the IP pointer.
3553 arg->type = PRINT_ATOM;
3554 arg->atom.atom = malloc_or_die(32);
3555 sprintf(arg->atom.atom, "%lld", ip);
3557 /* skip the first "%pf : " */
3558 for (ptr = fmt + 6, bptr = data + field->offset;
3559 bptr < data + size && *ptr; ptr++) {
3590 vsize = pevent->long_size;
3604 /* the pointers are always 4 bytes aligned */
3605 bptr = (void *)(((unsigned long)bptr + 3) &
3607 val = pevent_read_number(pevent, bptr, vsize);
3611 arg->type = PRINT_ATOM;
3612 arg->atom.atom = malloc_or_die(32);
3613 sprintf(arg->atom.atom, "%lld", val);
3617 * The '*' case means that an arg is used as the length.
3618 * We need to continue to figure out for what.
3627 arg->type = PRINT_BSTRING;
3628 arg->string.string = strdup(bptr);
3629 if (!arg->string.string)
3631 bptr += strlen(bptr) + 1;
3643 static void free_args(struct print_arg *args)
3645 struct print_arg *next;
3656 get_bprint_format(void *data, int size __unused, struct event_format *event)
3658 struct pevent *pevent = event->pevent;
3659 unsigned long long addr;
3660 struct format_field *field;
3661 struct printk_map *printk;
3665 field = pevent->bprint_fmt_field;
3668 field = pevent_find_field(event, "fmt");
3670 die("can't find format field for binary printk");
3671 pevent->bprint_fmt_field = field;
3674 addr = pevent_read_number(pevent, data + field->offset, field->size);
3676 printk = find_printk(pevent, addr);
3678 format = malloc_or_die(45);
3679 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3685 /* Remove any quotes. */
3688 format = malloc_or_die(strlen(p) + 10);
3689 sprintf(format, "%s : %s", "%pf", p);
3690 /* remove ending quotes and new line since we will add one too */
3691 p = format + strlen(format) - 1;
3696 if (strcmp(p, "\\n") == 0)
3702 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3703 struct event_format *event, struct print_arg *arg)
3706 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3708 if (arg->type == PRINT_FUNC) {
3709 process_defined_func(s, data, size, event, arg);
3713 if (arg->type != PRINT_FIELD) {
3714 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3720 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3721 if (!arg->field.field) {
3723 pevent_find_any_field(event, arg->field.name);
3724 if (!arg->field.field)
3725 die("field %s not found", arg->field.name);
3727 if (arg->field.field->size != 6) {
3728 trace_seq_printf(s, "INVALIDMAC");
3731 buf = data + arg->field.field->offset;
3732 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3735 static int is_printable_array(char *p, unsigned int len)
3739 for (i = 0; i < len && p[i]; i++)
3745 static void print_event_fields(struct trace_seq *s, void *data, int size,
3746 struct event_format *event)
3748 struct format_field *field;
3749 unsigned long long val;
3750 unsigned int offset, len, i;
3752 field = event->format.fields;
3754 trace_seq_printf(s, " %s=", field->name);
3755 if (field->flags & FIELD_IS_ARRAY) {
3756 offset = field->offset;
3758 if (field->flags & FIELD_IS_DYNAMIC) {
3759 val = pevent_read_number(event->pevent, data + offset, len);
3764 if (field->flags & FIELD_IS_STRING &&
3765 is_printable_array(data + offset, len)) {
3766 trace_seq_printf(s, "%s", (char *)data + offset);
3768 trace_seq_puts(s, "ARRAY[");
3769 for (i = 0; i < len; i++) {
3771 trace_seq_puts(s, ", ");
3772 trace_seq_printf(s, "%02x",
3773 *((unsigned char *)data + offset + i));
3775 trace_seq_putc(s, ']');
3776 field->flags &= ~FIELD_IS_STRING;
3779 val = pevent_read_number(event->pevent, data + field->offset,
3781 if (field->flags & FIELD_IS_POINTER) {
3782 trace_seq_printf(s, "0x%llx", val);
3783 } else if (field->flags & FIELD_IS_SIGNED) {
3784 switch (field->size) {
3787 * If field is long then print it in hex.
3788 * A long usually stores pointers.
3790 if (field->flags & FIELD_IS_LONG)
3791 trace_seq_printf(s, "0x%x", (int)val);
3793 trace_seq_printf(s, "%d", (int)val);
3796 trace_seq_printf(s, "%2d", (short)val);
3799 trace_seq_printf(s, "%1d", (char)val);
3802 trace_seq_printf(s, "%lld", val);
3805 if (field->flags & FIELD_IS_LONG)
3806 trace_seq_printf(s, "0x%llx", val);
3808 trace_seq_printf(s, "%llu", val);
3811 field = field->next;
3815 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3817 struct pevent *pevent = event->pevent;
3818 struct print_fmt *print_fmt = &event->print_fmt;
3819 struct print_arg *arg = print_fmt->args;
3820 struct print_arg *args = NULL;
3821 const char *ptr = print_fmt->format;
3822 unsigned long long val;
3823 struct func_map *func;
3824 const char *saveptr;
3825 char *bprint_fmt = NULL;
3833 if (event->flags & EVENT_FL_FAILED) {
3834 trace_seq_printf(s, "[FAILED TO PARSE]");
3835 print_event_fields(s, data, size, event);
3839 if (event->flags & EVENT_FL_ISBPRINT) {
3840 bprint_fmt = get_bprint_format(data, size, event);
3841 args = make_bprint_args(bprint_fmt, data, size, event);
3846 for (; *ptr; ptr++) {
3852 trace_seq_putc(s, '\n');
3855 trace_seq_putc(s, '\t');
3858 trace_seq_putc(s, '\r');
3861 trace_seq_putc(s, '\\');
3864 trace_seq_putc(s, *ptr);
3868 } else if (*ptr == '%') {
3876 trace_seq_putc(s, '%');
3879 /* FIXME: need to handle properly */
3891 /* The argument is the length. */
3893 do_warning("no argument match");
3894 event->flags |= EVENT_FL_FAILED;
3897 len_arg = eval_num_arg(data, size, event, arg);
3907 if (pevent->long_size == 4)
3912 if (*(ptr+1) == 'F' ||
3916 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3917 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3930 do_warning("no argument match");
3931 event->flags |= EVENT_FL_FAILED;
3935 len = ((unsigned long)ptr + 1) -
3936 (unsigned long)saveptr;
3938 /* should never happen */
3940 do_warning("bad format!");
3941 event->flags |= EVENT_FL_FAILED;
3945 memcpy(format, saveptr, len);
3948 val = eval_num_arg(data, size, event, arg);
3952 func = find_func(pevent, val);
3954 trace_seq_puts(s, func->func);
3955 if (show_func == 'F')
3962 if (pevent->long_size == 8 && ls &&
3963 sizeof(long) != 8) {
3967 /* make %l into %ll */
3968 p = strchr(format, 'l');
3970 memmove(p+1, p, strlen(p)+1);
3971 else if (strcmp(format, "%p") == 0)
3972 strcpy(format, "0x%llx");
3977 trace_seq_printf(s, format, len_arg, (char)val);
3979 trace_seq_printf(s, format, (char)val);
3983 trace_seq_printf(s, format, len_arg, (short)val);
3985 trace_seq_printf(s, format, (short)val);
3989 trace_seq_printf(s, format, len_arg, (int)val);
3991 trace_seq_printf(s, format, (int)val);
3995 trace_seq_printf(s, format, len_arg, (long)val);
3997 trace_seq_printf(s, format, (long)val);
4001 trace_seq_printf(s, format, len_arg,
4004 trace_seq_printf(s, format, (long long)val);
4007 do_warning("bad count (%d)", ls);
4008 event->flags |= EVENT_FL_FAILED;
4013 do_warning("no matching argument");
4014 event->flags |= EVENT_FL_FAILED;
4018 len = ((unsigned long)ptr + 1) -
4019 (unsigned long)saveptr;
4021 /* should never happen */
4023 do_warning("bad format!");
4024 event->flags |= EVENT_FL_FAILED;
4028 memcpy(format, saveptr, len);
4032 print_str_arg(s, data, size, event,
4033 format, len_arg, arg);
4037 trace_seq_printf(s, ">%c<", *ptr);
4041 trace_seq_putc(s, *ptr);
4044 if (event->flags & EVENT_FL_FAILED) {
4046 trace_seq_printf(s, "[FAILED TO PARSE]");
4056 * pevent_data_lat_fmt - parse the data for the latency format
4057 * @pevent: a handle to the pevent
4058 * @s: the trace_seq to write to
4059 * @record: the record to read from
4061 * This parses out the Latency format (interrupts disabled,
4062 * need rescheduling, in hard/soft interrupt, preempt count
4063 * and lock depth) and places it into the trace_seq.
4065 void pevent_data_lat_fmt(struct pevent *pevent,
4066 struct trace_seq *s, struct pevent_record *record)
4068 static int check_lock_depth = 1;
4069 static int check_migrate_disable = 1;
4070 static int lock_depth_exists;
4071 static int migrate_disable_exists;
4072 unsigned int lat_flags;
4075 int migrate_disable;
4078 void *data = record->data;
4080 lat_flags = parse_common_flags(pevent, data);
4081 pc = parse_common_pc(pevent, data);
4082 /* lock_depth may not always exist */
4083 if (lock_depth_exists)
4084 lock_depth = parse_common_lock_depth(pevent, data);
4085 else if (check_lock_depth) {
4086 lock_depth = parse_common_lock_depth(pevent, data);
4088 check_lock_depth = 0;
4090 lock_depth_exists = 1;
4093 /* migrate_disable may not always exist */
4094 if (migrate_disable_exists)
4095 migrate_disable = parse_common_migrate_disable(pevent, data);
4096 else if (check_migrate_disable) {
4097 migrate_disable = parse_common_migrate_disable(pevent, data);
4098 if (migrate_disable < 0)
4099 check_migrate_disable = 0;
4101 migrate_disable_exists = 1;
4104 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4105 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4107 trace_seq_printf(s, "%c%c%c",
4108 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4109 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4111 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4113 (hardirq && softirq) ? 'H' :
4114 hardirq ? 'h' : softirq ? 's' : '.');
4117 trace_seq_printf(s, "%x", pc);
4119 trace_seq_putc(s, '.');
4121 if (migrate_disable_exists) {
4122 if (migrate_disable < 0)
4123 trace_seq_putc(s, '.');
4125 trace_seq_printf(s, "%d", migrate_disable);
4128 if (lock_depth_exists) {
4130 trace_seq_putc(s, '.');
4132 trace_seq_printf(s, "%d", lock_depth);
4135 trace_seq_terminate(s);
4139 * pevent_data_type - parse out the given event type
4140 * @pevent: a handle to the pevent
4141 * @rec: the record to read from
4143 * This returns the event id from the @rec.
4145 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4147 return trace_parse_common_type(pevent, rec->data);
4151 * pevent_data_event_from_type - find the event by a given type
4152 * @pevent: a handle to the pevent
4153 * @type: the type of the event.
4155 * This returns the event form a given @type;
4157 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4159 return pevent_find_event(pevent, type);
4163 * pevent_data_pid - parse the PID from raw data
4164 * @pevent: a handle to the pevent
4165 * @rec: the record to parse
4167 * This returns the PID from a raw data.
4169 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4171 return parse_common_pid(pevent, rec->data);
4175 * pevent_data_comm_from_pid - return the command line from PID
4176 * @pevent: a handle to the pevent
4177 * @pid: the PID of the task to search for
4179 * This returns a pointer to the command line that has the given
4182 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4186 comm = find_cmdline(pevent, pid);
4191 * pevent_data_comm_from_pid - parse the data into the print format
4192 * @s: the trace_seq to write to
4193 * @event: the handle to the event
4194 * @record: the record to read from
4196 * This parses the raw @data using the given @event information and
4197 * writes the print format into the trace_seq.
4199 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4200 struct pevent_record *record)
4202 int print_pretty = 1;
4204 if (event->pevent->print_raw)
4205 print_event_fields(s, record->data, record->size, event);
4209 print_pretty = event->handler(s, record, event,
4213 pretty_print(s, record->data, record->size, event);
4216 trace_seq_terminate(s);
4219 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4220 struct pevent_record *record)
4222 static char *spaces = " "; /* 20 spaces */
4223 struct event_format *event;
4225 unsigned long usecs;
4226 unsigned long nsecs;
4228 void *data = record->data;
4234 secs = record->ts / NSECS_PER_SEC;
4235 nsecs = record->ts - secs * NSECS_PER_SEC;
4237 if (record->size < 0) {
4238 do_warning("ug! negative record size %d", record->size);
4242 type = trace_parse_common_type(pevent, data);
4244 event = pevent_find_event(pevent, type);
4246 do_warning("ug! no event found for type %d", type);
4250 pid = parse_common_pid(pevent, data);
4251 comm = find_cmdline(pevent, pid);
4253 if (pevent->latency_format) {
4254 trace_seq_printf(s, "%8.8s-%-5d %3d",
4255 comm, pid, record->cpu);
4256 pevent_data_lat_fmt(pevent, s, record);
4258 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4260 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4264 usecs = (nsecs + 500) / NSECS_PER_USEC;
4268 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4270 /* Space out the event names evenly. */
4271 len = strlen(event->name);
4273 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4275 pevent_event_info(s, event, record);
4278 static int events_id_cmp(const void *a, const void *b)
4280 struct event_format * const * ea = a;
4281 struct event_format * const * eb = b;
4283 if ((*ea)->id < (*eb)->id)
4286 if ((*ea)->id > (*eb)->id)
4292 static int events_name_cmp(const void *a, const void *b)
4294 struct event_format * const * ea = a;
4295 struct event_format * const * eb = b;
4298 res = strcmp((*ea)->name, (*eb)->name);
4302 res = strcmp((*ea)->system, (*eb)->system);
4306 return events_id_cmp(a, b);
4309 static int events_system_cmp(const void *a, const void *b)
4311 struct event_format * const * ea = a;
4312 struct event_format * const * eb = b;
4315 res = strcmp((*ea)->system, (*eb)->system);
4319 res = strcmp((*ea)->name, (*eb)->name);
4323 return events_id_cmp(a, b);
4326 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4328 struct event_format **events;
4329 int (*sort)(const void *a, const void *b);
4331 events = pevent->sort_events;
4333 if (events && pevent->last_type == sort_type)
4337 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4341 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4342 events[pevent->nr_events] = NULL;
4344 pevent->sort_events = events;
4346 /* the internal events are sorted by id */
4347 if (sort_type == EVENT_SORT_ID) {
4348 pevent->last_type = sort_type;
4353 switch (sort_type) {
4355 sort = events_id_cmp;
4357 case EVENT_SORT_NAME:
4358 sort = events_name_cmp;
4360 case EVENT_SORT_SYSTEM:
4361 sort = events_system_cmp;
4367 qsort(events, pevent->nr_events, sizeof(*events), sort);
4368 pevent->last_type = sort_type;
4373 static struct format_field **
4374 get_event_fields(const char *type, const char *name,
4375 int count, struct format_field *list)
4377 struct format_field **fields;
4378 struct format_field *field;
4381 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4382 for (field = list; field; field = field->next) {
4383 fields[i++] = field;
4384 if (i == count + 1) {
4385 do_warning("event %s has more %s fields than specified",
4393 do_warning("event %s has less %s fields than specified",
4402 * pevent_event_common_fields - return a list of common fields for an event
4403 * @event: the event to return the common fields of.
4405 * Returns an allocated array of fields. The last item in the array is NULL.
4406 * The array must be freed with free().
4408 struct format_field **pevent_event_common_fields(struct event_format *event)
4410 return get_event_fields("common", event->name,
4411 event->format.nr_common,
4412 event->format.common_fields);
4416 * pevent_event_fields - return a list of event specific fields for an event
4417 * @event: the event to return the fields of.
4419 * Returns an allocated array of fields. The last item in the array is NULL.
4420 * The array must be freed with free().
4422 struct format_field **pevent_event_fields(struct event_format *event)
4424 return get_event_fields("event", event->name,
4425 event->format.nr_fields,
4426 event->format.fields);
4429 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4431 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4433 trace_seq_puts(s, ", ");
4434 print_fields(s, field->next);
4439 static void print_args(struct print_arg *args)
4441 int print_paren = 1;
4444 switch (args->type) {
4449 printf("%s", args->atom.atom);
4452 printf("REC->%s", args->field.name);
4455 printf("__print_flags(");
4456 print_args(args->flags.field);
4457 printf(", %s, ", args->flags.delim);
4459 print_fields(&s, args->flags.flags);
4460 trace_seq_do_printf(&s);
4461 trace_seq_destroy(&s);
4465 printf("__print_symbolic(");
4466 print_args(args->symbol.field);
4469 print_fields(&s, args->symbol.symbols);
4470 trace_seq_do_printf(&s);
4471 trace_seq_destroy(&s);
4475 printf("__print_hex(");
4476 print_args(args->hex.field);
4478 print_args(args->hex.size);
4483 printf("__get_str(%s)", args->string.string);
4486 printf("(%s)", args->typecast.type);
4487 print_args(args->typecast.item);
4490 if (strcmp(args->op.op, ":") == 0)
4494 print_args(args->op.left);
4495 printf(" %s ", args->op.op);
4496 print_args(args->op.right);
4501 /* we should warn... */
4506 print_args(args->next);
4510 static void parse_header_field(const char *field,
4511 int *offset, int *size, int mandatory)
4513 unsigned long long save_input_buf_ptr;
4514 unsigned long long save_input_buf_siz;
4518 save_input_buf_ptr = input_buf_ptr;
4519 save_input_buf_siz = input_buf_siz;
4521 if (read_expected(EVENT_ITEM, "field") < 0)
4523 if (read_expected(EVENT_OP, ":") < 0)
4527 if (read_expect_type(EVENT_ITEM, &token) < 0)
4532 * If this is not a mandatory field, then test it first.
4535 if (read_expected(EVENT_ITEM, field) < 0)
4538 if (read_expect_type(EVENT_ITEM, &token) < 0)
4540 if (strcmp(token, field) != 0)
4545 if (read_expected(EVENT_OP, ";") < 0)
4547 if (read_expected(EVENT_ITEM, "offset") < 0)
4549 if (read_expected(EVENT_OP, ":") < 0)
4551 if (read_expect_type(EVENT_ITEM, &token) < 0)
4553 *offset = atoi(token);
4555 if (read_expected(EVENT_OP, ";") < 0)
4557 if (read_expected(EVENT_ITEM, "size") < 0)
4559 if (read_expected(EVENT_OP, ":") < 0)
4561 if (read_expect_type(EVENT_ITEM, &token) < 0)
4563 *size = atoi(token);
4565 if (read_expected(EVENT_OP, ";") < 0)
4567 type = read_token(&token);
4568 if (type != EVENT_NEWLINE) {
4569 /* newer versions of the kernel have a "signed" type */
4570 if (type != EVENT_ITEM)
4573 if (strcmp(token, "signed") != 0)
4578 if (read_expected(EVENT_OP, ":") < 0)
4581 if (read_expect_type(EVENT_ITEM, &token))
4585 if (read_expected(EVENT_OP, ";") < 0)
4588 if (read_expect_type(EVENT_NEWLINE, &token))
4596 input_buf_ptr = save_input_buf_ptr;
4597 input_buf_siz = save_input_buf_siz;
4604 * pevent_parse_header_page - parse the data stored in the header page
4605 * @pevent: the handle to the pevent
4606 * @buf: the buffer storing the header page format string
4607 * @size: the size of @buf
4608 * @long_size: the long size to use if there is no header
4610 * This parses the header page format for information on the
4611 * ring buffer used. The @buf should be copied from
4613 * /sys/kernel/debug/tracing/events/header_page
4615 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4622 * Old kernels did not have header page info.
4623 * Sorry but we just use what we find here in user space.
4625 pevent->header_page_ts_size = sizeof(long long);
4626 pevent->header_page_size_size = long_size;
4627 pevent->header_page_data_offset = sizeof(long long) + long_size;
4628 pevent->old_format = 1;
4631 init_input_buf(buf, size);
4633 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4634 &pevent->header_page_ts_size, 1);
4635 parse_header_field("commit", &pevent->header_page_size_offset,
4636 &pevent->header_page_size_size, 1);
4637 parse_header_field("overwrite", &pevent->header_page_overwrite,
4639 parse_header_field("data", &pevent->header_page_data_offset,
4640 &pevent->header_page_data_size, 1);
4645 static int event_matches(struct event_format *event,
4646 int id, const char *sys_name,
4647 const char *event_name)
4649 if (id >= 0 && id != event->id)
4652 if (event_name && (strcmp(event_name, event->name) != 0))
4655 if (sys_name && (strcmp(sys_name, event->system) != 0))
4661 static void free_handler(struct event_handler *handle)
4663 free((void *)handle->sys_name);
4664 free((void *)handle->event_name);
4668 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4670 struct event_handler *handle, **next;
4672 for (next = &pevent->handlers; *next;
4673 next = &(*next)->next) {
4675 if (event_matches(event, handle->id,
4677 handle->event_name))
4684 pr_stat("overriding event (%d) %s:%s with new print handler",
4685 event->id, event->system, event->name);
4687 event->handler = handle->func;
4688 event->context = handle->context;
4690 *next = handle->next;
4691 free_handler(handle);
4697 * pevent_parse_event - parse the event format
4698 * @pevent: the handle to the pevent
4699 * @buf: the buffer storing the event format string
4700 * @size: the size of @buf
4701 * @sys: the system the event belongs to
4703 * This parses the event format and creates an event structure
4704 * to quickly parse raw data for a given event.
4706 * These files currently come from:
4708 * /sys/kernel/debug/tracing/events/.../.../format
4710 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4711 unsigned long size, const char *sys)
4713 struct event_format *event;
4716 init_input_buf(buf, size);
4718 event = alloc_event();
4720 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4722 event->name = event_read_name();
4725 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4726 goto event_alloc_failed;
4729 if (strcmp(sys, "ftrace") == 0) {
4730 event->flags |= EVENT_FL_ISFTRACE;
4732 if (strcmp(event->name, "bprint") == 0)
4733 event->flags |= EVENT_FL_ISBPRINT;
4736 event->id = event_read_id();
4737 if (event->id < 0) {
4738 ret = PEVENT_ERRNO__READ_ID_FAILED;
4740 * This isn't an allocation error actually.
4741 * But as the ID is critical, just bail out.
4743 goto event_alloc_failed;
4746 event->system = strdup(sys);
4747 if (!event->system) {
4748 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4749 goto event_alloc_failed;
4752 /* Add pevent to event so that it can be referenced */
4753 event->pevent = pevent;
4755 ret = event_read_format(event);
4757 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4758 goto event_parse_failed;
4762 * If the event has an override, don't print warnings if the event
4763 * print format fails to parse.
4765 if (find_event_handle(pevent, event))
4768 ret = event_read_print(event);
4771 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4772 goto event_parse_failed;
4776 add_event(pevent, event);
4778 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4779 struct format_field *field;
4780 struct print_arg *arg, **list;
4782 /* old ftrace had no args */
4783 list = &event->print_fmt.args;
4784 for (field = event->format.fields; field; field = field->next) {
4786 arg->type = PRINT_FIELD;
4787 arg->field.name = strdup(field->name);
4788 if (!arg->field.name) {
4789 event->flags |= EVENT_FL_FAILED;
4791 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4793 arg->field.field = field;
4800 #define PRINT_ARGS 0
4801 if (PRINT_ARGS && event->print_fmt.args)
4802 print_args(event->print_fmt.args);
4807 event->flags |= EVENT_FL_FAILED;
4808 /* still add it even if it failed */
4809 add_event(pevent, event);
4813 free(event->system);
4820 #define _PE(code, str) str
4821 static const char * const pevent_error_str[] = {
4826 int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4827 char *buf, size_t buflen)
4833 msg = strerror_r(errnum, buf, buflen);
4835 size_t len = strlen(msg);
4836 char *c = mempcpy(buf, msg, min(buflen-1, len));
4842 if (errnum <= __PEVENT_ERRNO__START ||
4843 errnum >= __PEVENT_ERRNO__END)
4846 idx = errnum - __PEVENT_ERRNO__START - 1;
4847 msg = pevent_error_str[idx];
4850 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4851 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4852 case PEVENT_ERRNO__READ_ID_FAILED:
4853 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4854 case PEVENT_ERRNO__READ_PRINT_FAILED:
4855 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4856 snprintf(buf, buflen, "%s", msg);
4860 /* cannot reach here */
4867 int get_field_val(struct trace_seq *s, struct format_field *field,
4868 const char *name, struct pevent_record *record,
4869 unsigned long long *val, int err)
4873 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4877 if (pevent_read_number_field(field, record->data, val)) {
4879 trace_seq_printf(s, " %s=INVALID", name);
4887 * pevent_get_field_raw - return the raw pointer into the data field
4888 * @s: The seq to print to on error
4889 * @event: the event that the field is for
4890 * @name: The name of the field
4891 * @record: The record with the field name.
4892 * @len: place to store the field length.
4893 * @err: print default error if failed.
4895 * Returns a pointer into record->data of the field and places
4896 * the length of the field in @len.
4898 * On failure, it returns NULL.
4900 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4901 const char *name, struct pevent_record *record,
4904 struct format_field *field;
4905 void *data = record->data;
4912 field = pevent_find_field(event, name);
4916 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4920 /* Allow @len to be NULL */
4924 offset = field->offset;
4925 if (field->flags & FIELD_IS_DYNAMIC) {
4926 offset = pevent_read_number(event->pevent,
4927 data + offset, field->size);
4928 *len = offset >> 16;
4933 return data + offset;
4937 * pevent_get_field_val - find a field and return its value
4938 * @s: The seq to print to on error
4939 * @event: the event that the field is for
4940 * @name: The name of the field
4941 * @record: The record with the field name.
4942 * @val: place to store the value of the field.
4943 * @err: print default error if failed.
4945 * Returns 0 on success -1 on field not found.
4947 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4948 const char *name, struct pevent_record *record,
4949 unsigned long long *val, int err)
4951 struct format_field *field;
4956 field = pevent_find_field(event, name);
4958 return get_field_val(s, field, name, record, val, err);
4962 * pevent_get_common_field_val - find a common field and return its value
4963 * @s: The seq to print to on error
4964 * @event: the event that the field is for
4965 * @name: The name of the field
4966 * @record: The record with the field name.
4967 * @val: place to store the value of the field.
4968 * @err: print default error if failed.
4970 * Returns 0 on success -1 on field not found.
4972 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4973 const char *name, struct pevent_record *record,
4974 unsigned long long *val, int err)
4976 struct format_field *field;
4981 field = pevent_find_common_field(event, name);
4983 return get_field_val(s, field, name, record, val, err);
4987 * pevent_get_any_field_val - find a any field and return its value
4988 * @s: The seq to print to on error
4989 * @event: the event that the field is for
4990 * @name: The name of the field
4991 * @record: The record with the field name.
4992 * @val: place to store the value of the field.
4993 * @err: print default error if failed.
4995 * Returns 0 on success -1 on field not found.
4997 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
4998 const char *name, struct pevent_record *record,
4999 unsigned long long *val, int err)
5001 struct format_field *field;
5006 field = pevent_find_any_field(event, name);
5008 return get_field_val(s, field, name, record, val, err);
5012 * pevent_print_num_field - print a field and a format
5013 * @s: The seq to print to
5014 * @fmt: The printf format to print the field with.
5015 * @event: the event that the field is for
5016 * @name: The name of the field
5017 * @record: The record with the field name.
5018 * @err: print default error if failed.
5020 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5022 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5023 struct event_format *event, const char *name,
5024 struct pevent_record *record, int err)
5026 struct format_field *field = pevent_find_field(event, name);
5027 unsigned long long val;
5032 if (pevent_read_number_field(field, record->data, &val))
5035 return trace_seq_printf(s, fmt, val);
5039 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5043 static void free_func_handle(struct pevent_function_handler *func)
5045 struct pevent_func_params *params;
5049 while (func->params) {
5050 params = func->params;
5051 func->params = params->next;
5059 * pevent_register_print_function - register a helper function
5060 * @pevent: the handle to the pevent
5061 * @func: the function to process the helper function
5062 * @ret_type: the return type of the helper function
5063 * @name: the name of the helper function
5064 * @parameters: A list of enum pevent_func_arg_type
5066 * Some events may have helper functions in the print format arguments.
5067 * This allows a plugin to dynamically create a way to process one
5068 * of these functions.
5070 * The @parameters is a variable list of pevent_func_arg_type enums that
5071 * must end with PEVENT_FUNC_ARG_VOID.
5073 int pevent_register_print_function(struct pevent *pevent,
5074 pevent_func_handler func,
5075 enum pevent_func_arg_type ret_type,
5078 struct pevent_function_handler *func_handle;
5079 struct pevent_func_params **next_param;
5080 struct pevent_func_params *param;
5081 enum pevent_func_arg_type type;
5084 func_handle = find_func_handler(pevent, name);
5087 * This is most like caused by the users own
5088 * plugins updating the function. This overrides the
5091 pr_stat("override of function helper '%s'", name);
5092 remove_func_handler(pevent, name);
5095 func_handle = malloc_or_die(sizeof(*func_handle));
5096 memset(func_handle, 0, sizeof(*func_handle));
5098 func_handle->ret_type = ret_type;
5099 func_handle->name = strdup(name);
5100 func_handle->func = func;
5101 if (!func_handle->name)
5102 die("Failed to allocate function name");
5104 next_param = &(func_handle->params);
5107 type = va_arg(ap, enum pevent_func_arg_type);
5108 if (type == PEVENT_FUNC_ARG_VOID)
5111 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5112 warning("Invalid argument type %d", type);
5116 param = malloc_or_die(sizeof(*param));
5120 *next_param = param;
5121 next_param = &(param->next);
5123 func_handle->nr_args++;
5127 func_handle->next = pevent->func_handlers;
5128 pevent->func_handlers = func_handle;
5133 free_func_handle(func_handle);
5138 * pevent_register_event_handler - register a way to parse an event
5139 * @pevent: the handle to the pevent
5140 * @id: the id of the event to register
5141 * @sys_name: the system name the event belongs to
5142 * @event_name: the name of the event
5143 * @func: the function to call to parse the event information
5144 * @context: the data to be passed to @func
5146 * This function allows a developer to override the parsing of
5147 * a given event. If for some reason the default print format
5148 * is not sufficient, this function will register a function
5149 * for an event to be used to parse the data instead.
5151 * If @id is >= 0, then it is used to find the event.
5152 * else @sys_name and @event_name are used.
5154 int pevent_register_event_handler(struct pevent *pevent,
5155 int id, char *sys_name, char *event_name,
5156 pevent_event_handler_func func,
5159 struct event_format *event;
5160 struct event_handler *handle;
5164 event = pevent_find_event(pevent, id);
5167 if (event_name && (strcmp(event_name, event->name) != 0))
5169 if (sys_name && (strcmp(sys_name, event->system) != 0))
5172 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5177 pr_stat("overriding event (%d) %s:%s with new print handler",
5178 event->id, event->system, event->name);
5180 event->handler = func;
5181 event->context = context;
5185 /* Save for later use. */
5186 handle = malloc(sizeof(*handle));
5188 do_warning("Failed to allocate event handler");
5189 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5192 memset(handle, 0, sizeof(*handle));
5195 handle->event_name = strdup(event_name);
5197 handle->sys_name = strdup(sys_name);
5199 if ((event_name && !handle->event_name) ||
5200 (sys_name && !handle->sys_name)) {
5201 do_warning("Failed to allocate event/sys name");
5202 free((void *)handle->event_name);
5203 free((void *)handle->sys_name);
5205 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5208 handle->func = func;
5209 handle->next = pevent->handlers;
5210 pevent->handlers = handle;
5211 handle->context = context;
5217 * pevent_alloc - create a pevent handle
5219 struct pevent *pevent_alloc(void)
5221 struct pevent *pevent;
5223 pevent = malloc(sizeof(*pevent));
5226 memset(pevent, 0, sizeof(*pevent));
5227 pevent->ref_count = 1;
5232 void pevent_ref(struct pevent *pevent)
5234 pevent->ref_count++;
5237 static void free_format_fields(struct format_field *field)
5239 struct format_field *next;
5250 static void free_formats(struct format *format)
5252 free_format_fields(format->common_fields);
5253 free_format_fields(format->fields);
5256 static void free_event(struct event_format *event)
5259 free(event->system);
5261 free_formats(&event->format);
5263 free(event->print_fmt.format);
5264 free_args(event->print_fmt.args);
5270 * pevent_free - free a pevent handle
5271 * @pevent: the pevent handle to free
5273 void pevent_free(struct pevent *pevent)
5275 struct cmdline_list *cmdlist, *cmdnext;
5276 struct func_list *funclist, *funcnext;
5277 struct printk_list *printklist, *printknext;
5278 struct pevent_function_handler *func_handler;
5279 struct event_handler *handle;
5285 cmdlist = pevent->cmdlist;
5286 funclist = pevent->funclist;
5287 printklist = pevent->printklist;
5289 pevent->ref_count--;
5290 if (pevent->ref_count)
5293 if (pevent->cmdlines) {
5294 for (i = 0; i < pevent->cmdline_count; i++)
5295 free(pevent->cmdlines[i].comm);
5296 free(pevent->cmdlines);
5300 cmdnext = cmdlist->next;
5301 free(cmdlist->comm);
5306 if (pevent->func_map) {
5307 for (i = 0; i < pevent->func_count; i++) {
5308 free(pevent->func_map[i].func);
5309 free(pevent->func_map[i].mod);
5311 free(pevent->func_map);
5315 funcnext = funclist->next;
5316 free(funclist->func);
5317 free(funclist->mod);
5319 funclist = funcnext;
5322 while (pevent->func_handlers) {
5323 func_handler = pevent->func_handlers;
5324 pevent->func_handlers = func_handler->next;
5325 free_func_handle(func_handler);
5328 if (pevent->printk_map) {
5329 for (i = 0; i < pevent->printk_count; i++)
5330 free(pevent->printk_map[i].printk);
5331 free(pevent->printk_map);
5334 while (printklist) {
5335 printknext = printklist->next;
5336 free(printklist->printk);
5338 printklist = printknext;
5341 for (i = 0; i < pevent->nr_events; i++)
5342 free_event(pevent->events[i]);
5344 while (pevent->handlers) {
5345 handle = pevent->handlers;
5346 pevent->handlers = handle->next;
5347 free_handler(handle);
5350 free(pevent->events);
5351 free(pevent->sort_events);
5356 void pevent_unref(struct pevent *pevent)
5358 pevent_free(pevent);