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 return calloc(1, sizeof(struct print_arg));
129 static int cmdline_cmp(const void *a, const void *b)
131 const struct cmdline *ca = a;
132 const struct cmdline *cb = b;
134 if (ca->pid < cb->pid)
136 if (ca->pid > cb->pid)
142 struct cmdline_list {
143 struct cmdline_list *next;
148 static int cmdline_init(struct pevent *pevent)
150 struct cmdline_list *cmdlist = pevent->cmdlist;
151 struct cmdline_list *item;
152 struct cmdline *cmdlines;
155 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
161 cmdlines[i].pid = cmdlist->pid;
162 cmdlines[i].comm = cmdlist->comm;
165 cmdlist = cmdlist->next;
169 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
171 pevent->cmdlines = cmdlines;
172 pevent->cmdlist = NULL;
177 static char *find_cmdline(struct pevent *pevent, int pid)
179 const struct cmdline *comm;
185 if (!pevent->cmdlines && cmdline_init(pevent))
186 return "<not enough memory for cmdlines!>";
190 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
191 sizeof(*pevent->cmdlines), cmdline_cmp);
199 * pevent_pid_is_registered - return if a pid has a cmdline registered
200 * @pevent: handle for the pevent
201 * @pid: The pid to check if it has a cmdline registered with.
203 * Returns 1 if the pid has a cmdline mapped to it
206 int pevent_pid_is_registered(struct pevent *pevent, int pid)
208 const struct cmdline *comm;
214 if (!pevent->cmdlines && cmdline_init(pevent))
219 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
220 sizeof(*pevent->cmdlines), cmdline_cmp);
228 * If the command lines have been converted to an array, then
229 * we must add this pid. This is much slower than when cmdlines
230 * are added before the array is initialized.
232 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
234 struct cmdline *cmdlines = pevent->cmdlines;
235 const struct cmdline *cmdline;
241 /* avoid duplicates */
244 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
245 sizeof(*pevent->cmdlines), cmdline_cmp);
251 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
257 cmdlines[pevent->cmdline_count].comm = strdup(comm);
258 if (!cmdlines[pevent->cmdline_count].comm) {
264 cmdlines[pevent->cmdline_count].pid = pid;
266 if (cmdlines[pevent->cmdline_count].comm)
267 pevent->cmdline_count++;
269 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
270 pevent->cmdlines = cmdlines;
276 * pevent_register_comm - register a pid / comm mapping
277 * @pevent: handle for the pevent
278 * @comm: the command line to register
279 * @pid: the pid to map the command line to
281 * This adds a mapping to search for command line names with
282 * a given pid. The comm is duplicated.
284 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
286 struct cmdline_list *item;
288 if (pevent->cmdlines)
289 return add_new_comm(pevent, comm, pid);
291 item = malloc(sizeof(*item));
295 item->comm = strdup(comm);
301 item->next = pevent->cmdlist;
303 pevent->cmdlist = item;
304 pevent->cmdline_count++;
310 unsigned long long addr;
316 struct func_list *next;
317 unsigned long long addr;
322 static int func_cmp(const void *a, const void *b)
324 const struct func_map *fa = a;
325 const struct func_map *fb = b;
327 if (fa->addr < fb->addr)
329 if (fa->addr > fb->addr)
336 * We are searching for a record in between, not an exact
339 static int func_bcmp(const void *a, const void *b)
341 const struct func_map *fa = a;
342 const struct func_map *fb = b;
344 if ((fa->addr == fb->addr) ||
346 (fa->addr > fb->addr &&
347 fa->addr < (fb+1)->addr))
350 if (fa->addr < fb->addr)
356 static int func_map_init(struct pevent *pevent)
358 struct func_list *funclist;
359 struct func_list *item;
360 struct func_map *func_map;
363 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
367 funclist = pevent->funclist;
371 func_map[i].func = funclist->func;
372 func_map[i].addr = funclist->addr;
373 func_map[i].mod = funclist->mod;
376 funclist = funclist->next;
380 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
383 * Add a special record at the end.
385 func_map[pevent->func_count].func = NULL;
386 func_map[pevent->func_count].addr = 0;
387 func_map[pevent->func_count].mod = NULL;
389 pevent->func_map = func_map;
390 pevent->funclist = NULL;
395 static struct func_map *
396 find_func(struct pevent *pevent, unsigned long long addr)
398 struct func_map *func;
401 if (!pevent->func_map)
402 func_map_init(pevent);
406 func = bsearch(&key, pevent->func_map, pevent->func_count,
407 sizeof(*pevent->func_map), func_bcmp);
413 * pevent_find_function - find a function by a given address
414 * @pevent: handle for the pevent
415 * @addr: the address to find the function with
417 * Returns a pointer to the function stored that has the given
418 * address. Note, the address does not have to be exact, it
419 * will select the function that would contain the address.
421 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
423 struct func_map *map;
425 map = find_func(pevent, addr);
433 * pevent_find_function_address - find a function address by a given address
434 * @pevent: handle for the pevent
435 * @addr: the address to find the function with
437 * Returns the address the function starts at. This can be used in
438 * conjunction with pevent_find_function to print both the function
439 * name and the function offset.
442 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
444 struct func_map *map;
446 map = find_func(pevent, addr);
454 * pevent_register_function - register a function with a given address
455 * @pevent: handle for the pevent
456 * @function: the function name to register
457 * @addr: the address the function starts at
458 * @mod: the kernel module the function may be in (NULL for none)
460 * This registers a function name with an address and module.
461 * The @func passed in is duplicated.
463 int pevent_register_function(struct pevent *pevent, char *func,
464 unsigned long long addr, char *mod)
466 struct func_list *item = malloc(sizeof(*item));
471 item->next = pevent->funclist;
472 item->func = strdup(func);
477 item->mod = strdup(mod);
484 pevent->funclist = item;
485 pevent->func_count++;
499 * pevent_print_funcs - print out the stored functions
500 * @pevent: handle for the pevent
502 * This prints out the stored functions.
504 void pevent_print_funcs(struct pevent *pevent)
508 if (!pevent->func_map)
509 func_map_init(pevent);
511 for (i = 0; i < (int)pevent->func_count; i++) {
513 pevent->func_map[i].addr,
514 pevent->func_map[i].func);
515 if (pevent->func_map[i].mod)
516 printf(" [%s]\n", pevent->func_map[i].mod);
523 unsigned long long addr;
528 struct printk_list *next;
529 unsigned long long addr;
533 static int printk_cmp(const void *a, const void *b)
535 const struct printk_map *pa = a;
536 const struct printk_map *pb = b;
538 if (pa->addr < pb->addr)
540 if (pa->addr > pb->addr)
546 static int printk_map_init(struct pevent *pevent)
548 struct printk_list *printklist;
549 struct printk_list *item;
550 struct printk_map *printk_map;
553 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
557 printklist = pevent->printklist;
561 printk_map[i].printk = printklist->printk;
562 printk_map[i].addr = printklist->addr;
565 printklist = printklist->next;
569 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
571 pevent->printk_map = printk_map;
572 pevent->printklist = NULL;
577 static struct printk_map *
578 find_printk(struct pevent *pevent, unsigned long long addr)
580 struct printk_map *printk;
581 struct printk_map key;
583 if (!pevent->printk_map && printk_map_init(pevent))
588 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
589 sizeof(*pevent->printk_map), printk_cmp);
595 * pevent_register_print_string - register a string by its address
596 * @pevent: handle for the pevent
597 * @fmt: the string format to register
598 * @addr: the address the string was located at
600 * This registers a string by the address it was stored in the kernel.
601 * The @fmt passed in is duplicated.
603 int pevent_register_print_string(struct pevent *pevent, char *fmt,
604 unsigned long long addr)
606 struct printk_list *item = malloc(sizeof(*item));
611 item->next = pevent->printklist;
614 item->printk = strdup(fmt);
618 pevent->printklist = item;
619 pevent->printk_count++;
630 * pevent_print_printk - print out the stored strings
631 * @pevent: handle for the pevent
633 * This prints the string formats that were stored.
635 void pevent_print_printk(struct pevent *pevent)
639 if (!pevent->printk_map)
640 printk_map_init(pevent);
642 for (i = 0; i < (int)pevent->printk_count; i++) {
643 printf("%016llx %s\n",
644 pevent->printk_map[i].addr,
645 pevent->printk_map[i].printk);
649 static struct event_format *alloc_event(void)
651 return calloc(1, sizeof(struct event_format));
654 static int add_event(struct pevent *pevent, struct event_format *event)
657 struct event_format **events = realloc(pevent->events, sizeof(event) *
658 (pevent->nr_events + 1));
662 pevent->events = events;
664 for (i = 0; i < pevent->nr_events; i++) {
665 if (pevent->events[i]->id > event->id)
668 if (i < pevent->nr_events)
669 memmove(&pevent->events[i + 1],
671 sizeof(event) * (pevent->nr_events - i));
673 pevent->events[i] = event;
676 event->pevent = pevent;
681 static int event_item_type(enum event_type type)
684 case EVENT_ITEM ... EVENT_SQUOTE:
686 case EVENT_ERROR ... EVENT_DELIM:
692 static void free_flag_sym(struct print_flag_sym *fsym)
694 struct print_flag_sym *next;
705 static void free_arg(struct print_arg *arg)
707 struct print_arg *farg;
714 free(arg->atom.atom);
717 free(arg->field.name);
720 free_arg(arg->flags.field);
721 free(arg->flags.delim);
722 free_flag_sym(arg->flags.flags);
725 free_arg(arg->symbol.field);
726 free_flag_sym(arg->symbol.symbols);
729 free_arg(arg->hex.field);
730 free_arg(arg->hex.size);
733 free(arg->typecast.type);
734 free_arg(arg->typecast.item);
738 free(arg->string.string);
740 case PRINT_DYNAMIC_ARRAY:
741 free(arg->dynarray.index);
745 free_arg(arg->op.left);
746 free_arg(arg->op.right);
749 while (arg->func.args) {
750 farg = arg->func.args;
751 arg->func.args = farg->next;
764 static enum event_type get_type(int ch)
767 return EVENT_NEWLINE;
770 if (isalnum(ch) || ch == '_')
778 if (ch == '(' || ch == ')' || ch == ',')
784 static int __read_char(void)
786 if (input_buf_ptr >= input_buf_siz)
789 return input_buf[input_buf_ptr++];
792 static int __peek_char(void)
794 if (input_buf_ptr >= input_buf_siz)
797 return input_buf[input_buf_ptr];
801 * pevent_peek_char - peek at the next character that will be read
803 * Returns the next character read, or -1 if end of buffer.
805 int pevent_peek_char(void)
807 return __peek_char();
810 static int extend_token(char **tok, char *buf, int size)
812 char *newtok = realloc(*tok, size);
829 static enum event_type force_token(const char *str, char **tok);
831 static enum event_type __read_token(char **tok)
834 int ch, last_ch, quote_ch, next_ch;
837 enum event_type type;
847 if (type == EVENT_NONE)
855 if (asprintf(tok, "%c", ch) < 0)
863 next_ch = __peek_char();
864 if (next_ch == '>') {
865 buf[i++] = __read_char();
878 buf[i++] = __read_char();
890 default: /* what should we do instead? */
900 buf[i++] = __read_char();
905 /* don't keep quotes */
911 if (i == (BUFSIZ - 1)) {
915 if (extend_token(tok, buf, tok_size) < 0)
922 /* the '\' '\' will cancel itself */
923 if (ch == '\\' && last_ch == '\\')
925 } while (ch != quote_ch || last_ch == '\\');
926 /* remove the last quote */
930 * For strings (double quotes) check the next token.
931 * If it is another string, concatinate the two.
933 if (type == EVENT_DQUOTE) {
934 unsigned long long save_input_buf_ptr = input_buf_ptr;
938 } while (isspace(ch));
941 input_buf_ptr = save_input_buf_ptr;
946 case EVENT_ERROR ... EVENT_SPACE:
952 while (get_type(__peek_char()) == type) {
953 if (i == (BUFSIZ - 1)) {
957 if (extend_token(tok, buf, tok_size) < 0)
967 if (extend_token(tok, buf, tok_size + i + 1) < 0)
970 if (type == EVENT_ITEM) {
972 * Older versions of the kernel has a bug that
973 * creates invalid symbols and will break the mac80211
974 * parsing. This is a work around to that bug.
976 * See Linux kernel commit:
977 * 811cb50baf63461ce0bdb234927046131fc7fa8b
979 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
982 return force_token("\"\%s\" ", tok);
983 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
986 return force_token("\" sta:%pM\" ", tok);
987 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
990 return force_token("\" vif:%p(%d)\" ", tok);
997 static enum event_type force_token(const char *str, char **tok)
999 const char *save_input_buf;
1000 unsigned long long save_input_buf_ptr;
1001 unsigned long long save_input_buf_siz;
1002 enum event_type type;
1004 /* save off the current input pointers */
1005 save_input_buf = input_buf;
1006 save_input_buf_ptr = input_buf_ptr;
1007 save_input_buf_siz = input_buf_siz;
1009 init_input_buf(str, strlen(str));
1011 type = __read_token(tok);
1013 /* reset back to original token */
1014 input_buf = save_input_buf;
1015 input_buf_ptr = save_input_buf_ptr;
1016 input_buf_siz = save_input_buf_siz;
1021 static void free_token(char *tok)
1027 static enum event_type read_token(char **tok)
1029 enum event_type type;
1032 type = __read_token(tok);
1033 if (type != EVENT_SPACE)
1045 * pevent_read_token - access to utilites to use the pevent parser
1046 * @tok: The token to return
1048 * This will parse tokens from the string given by
1049 * pevent_init_data().
1051 * Returns the token type.
1053 enum event_type pevent_read_token(char **tok)
1055 return read_token(tok);
1059 * pevent_free_token - free a token returned by pevent_read_token
1060 * @token: the token to free
1062 void pevent_free_token(char *token)
1068 static enum event_type read_token_item(char **tok)
1070 enum event_type type;
1073 type = __read_token(tok);
1074 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1085 static int test_type(enum event_type type, enum event_type expect)
1087 if (type != expect) {
1088 do_warning("Error: expected type %d but read %d",
1095 static int test_type_token(enum event_type type, const char *token,
1096 enum event_type expect, const char *expect_tok)
1098 if (type != expect) {
1099 do_warning("Error: expected type %d but read %d",
1104 if (strcmp(token, expect_tok) != 0) {
1105 do_warning("Error: expected '%s' but read '%s'",
1112 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1114 enum event_type type;
1117 type = read_token(tok);
1119 type = read_token_item(tok);
1120 return test_type(type, expect);
1123 static int read_expect_type(enum event_type expect, char **tok)
1125 return __read_expect_type(expect, tok, 1);
1128 static int __read_expected(enum event_type expect, const char *str,
1131 enum event_type type;
1136 type = read_token(&token);
1138 type = read_token_item(&token);
1140 ret = test_type_token(type, token, expect, str);
1147 static int read_expected(enum event_type expect, const char *str)
1149 return __read_expected(expect, str, 1);
1152 static int read_expected_item(enum event_type expect, const char *str)
1154 return __read_expected(expect, str, 0);
1157 static char *event_read_name(void)
1161 if (read_expected(EVENT_ITEM, "name") < 0)
1164 if (read_expected(EVENT_OP, ":") < 0)
1167 if (read_expect_type(EVENT_ITEM, &token) < 0)
1177 static int event_read_id(void)
1182 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1185 if (read_expected(EVENT_OP, ":") < 0)
1188 if (read_expect_type(EVENT_ITEM, &token) < 0)
1191 id = strtoul(token, NULL, 0);
1200 static int field_is_string(struct format_field *field)
1202 if ((field->flags & FIELD_IS_ARRAY) &&
1203 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1204 strstr(field->type, "s8")))
1210 static int field_is_dynamic(struct format_field *field)
1212 if (strncmp(field->type, "__data_loc", 10) == 0)
1218 static int field_is_long(struct format_field *field)
1220 /* includes long long */
1221 if (strstr(field->type, "long"))
1227 static int event_read_fields(struct event_format *event, struct format_field **fields)
1229 struct format_field *field = NULL;
1230 enum event_type type;
1236 type = read_token(&token);
1237 if (type == EVENT_NEWLINE) {
1244 if (test_type_token(type, token, EVENT_ITEM, "field"))
1248 type = read_token(&token);
1250 * The ftrace fields may still use the "special" name.
1253 if (event->flags & EVENT_FL_ISFTRACE &&
1254 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1256 type = read_token(&token);
1259 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1263 if (read_expect_type(EVENT_ITEM, &token) < 0)
1268 field = calloc(1, sizeof(*field));
1272 field->event = event;
1274 /* read the rest of the type */
1276 type = read_token(&token);
1277 if (type == EVENT_ITEM ||
1278 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1280 * Some of the ftrace fields are broken and have
1281 * an illegal "." in them.
1283 (event->flags & EVENT_FL_ISFTRACE &&
1284 type == EVENT_OP && strcmp(token, ".") == 0)) {
1286 if (strcmp(token, "*") == 0)
1287 field->flags |= FIELD_IS_POINTER;
1291 new_type = realloc(field->type,
1292 strlen(field->type) +
1293 strlen(last_token) + 2);
1298 field->type = new_type;
1299 strcat(field->type, " ");
1300 strcat(field->type, last_token);
1303 field->type = last_token;
1312 do_warning("%s: no type found", __func__);
1315 field->name = last_token;
1317 if (test_type(type, EVENT_OP))
1320 if (strcmp(token, "[") == 0) {
1321 enum event_type last_type = type;
1322 char *brackets = token;
1326 field->flags |= FIELD_IS_ARRAY;
1328 type = read_token(&token);
1330 if (type == EVENT_ITEM)
1331 field->arraylen = strtoul(token, NULL, 0);
1333 field->arraylen = 0;
1335 while (strcmp(token, "]") != 0) {
1336 if (last_type == EVENT_ITEM &&
1343 new_brackets = realloc(brackets,
1345 strlen(token) + len);
1346 if (!new_brackets) {
1350 brackets = new_brackets;
1352 strcat(brackets, " ");
1353 strcat(brackets, token);
1354 /* We only care about the last token */
1355 field->arraylen = strtoul(token, NULL, 0);
1357 type = read_token(&token);
1358 if (type == EVENT_NONE) {
1359 do_warning("failed to find token");
1366 new_brackets = realloc(brackets, strlen(brackets) + 2);
1367 if (!new_brackets) {
1371 brackets = new_brackets;
1372 strcat(brackets, "]");
1374 /* add brackets to type */
1376 type = read_token(&token);
1378 * If the next token is not an OP, then it is of
1379 * the format: type [] item;
1381 if (type == EVENT_ITEM) {
1383 new_type = realloc(field->type,
1384 strlen(field->type) +
1385 strlen(field->name) +
1386 strlen(brackets) + 2);
1391 field->type = new_type;
1392 strcat(field->type, " ");
1393 strcat(field->type, field->name);
1394 free_token(field->name);
1395 strcat(field->type, brackets);
1396 field->name = token;
1397 type = read_token(&token);
1400 new_type = realloc(field->type,
1401 strlen(field->type) +
1402 strlen(brackets) + 1);
1407 field->type = new_type;
1408 strcat(field->type, brackets);
1413 if (field_is_string(field))
1414 field->flags |= FIELD_IS_STRING;
1415 if (field_is_dynamic(field))
1416 field->flags |= FIELD_IS_DYNAMIC;
1417 if (field_is_long(field))
1418 field->flags |= FIELD_IS_LONG;
1420 if (test_type_token(type, token, EVENT_OP, ";"))
1424 if (read_expected(EVENT_ITEM, "offset") < 0)
1427 if (read_expected(EVENT_OP, ":") < 0)
1430 if (read_expect_type(EVENT_ITEM, &token))
1432 field->offset = strtoul(token, NULL, 0);
1435 if (read_expected(EVENT_OP, ";") < 0)
1438 if (read_expected(EVENT_ITEM, "size") < 0)
1441 if (read_expected(EVENT_OP, ":") < 0)
1444 if (read_expect_type(EVENT_ITEM, &token))
1446 field->size = strtoul(token, NULL, 0);
1449 if (read_expected(EVENT_OP, ";") < 0)
1452 type = read_token(&token);
1453 if (type != EVENT_NEWLINE) {
1454 /* newer versions of the kernel have a "signed" type */
1455 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1460 if (read_expected(EVENT_OP, ":") < 0)
1463 if (read_expect_type(EVENT_ITEM, &token))
1466 /* add signed type */
1469 if (read_expected(EVENT_OP, ";") < 0)
1472 if (read_expect_type(EVENT_NEWLINE, &token))
1478 if (field->flags & FIELD_IS_ARRAY) {
1479 if (field->arraylen)
1480 field->elementsize = field->size / field->arraylen;
1481 else if (field->flags & FIELD_IS_STRING)
1482 field->elementsize = 1;
1484 field->elementsize = event->pevent->long_size;
1486 field->elementsize = field->size;
1489 fields = &field->next;
1506 static int event_read_format(struct event_format *event)
1511 if (read_expected_item(EVENT_ITEM, "format") < 0)
1514 if (read_expected(EVENT_OP, ":") < 0)
1517 if (read_expect_type(EVENT_NEWLINE, &token))
1521 ret = event_read_fields(event, &event->format.common_fields);
1524 event->format.nr_common = ret;
1526 ret = event_read_fields(event, &event->format.fields);
1529 event->format.nr_fields = ret;
1538 static enum event_type
1539 process_arg_token(struct event_format *event, struct print_arg *arg,
1540 char **tok, enum event_type type);
1542 static enum event_type
1543 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1545 enum event_type type;
1548 type = read_token(&token);
1551 return process_arg_token(event, arg, tok, type);
1554 static enum event_type
1555 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1557 static enum event_type
1558 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1560 struct print_arg *arg, *left, *right;
1561 enum event_type type;
1566 right = alloc_arg();
1568 arg->type = PRINT_OP;
1569 arg->op.left = left;
1570 arg->op.right = right;
1573 type = process_arg(event, left, &token);
1576 /* Handle other operations in the arguments */
1577 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1578 type = process_op(event, left, &token);
1582 if (test_type_token(type, token, EVENT_OP, ":"))
1587 type = process_arg(event, right, &token);
1589 top->op.right = arg;
1595 /* Top may point to itself */
1596 top->op.right = NULL;
1602 static enum event_type
1603 process_array(struct event_format *event, struct print_arg *top, char **tok)
1605 struct print_arg *arg;
1606 enum event_type type;
1612 type = process_arg(event, arg, &token);
1613 if (test_type_token(type, token, EVENT_OP, "]"))
1616 top->op.right = arg;
1619 type = read_token_item(&token);
1630 static int get_op_prio(char *op)
1644 /* '>>' and '<<' are 8 */
1648 /* '==' and '!=' are 10 */
1658 do_warning("unknown op '%c'", op[0]);
1662 if (strcmp(op, "++") == 0 ||
1663 strcmp(op, "--") == 0) {
1665 } else if (strcmp(op, ">>") == 0 ||
1666 strcmp(op, "<<") == 0) {
1668 } else if (strcmp(op, ">=") == 0 ||
1669 strcmp(op, "<=") == 0) {
1671 } else if (strcmp(op, "==") == 0 ||
1672 strcmp(op, "!=") == 0) {
1674 } else if (strcmp(op, "&&") == 0) {
1676 } else if (strcmp(op, "||") == 0) {
1679 do_warning("unknown op '%s'", op);
1685 static int set_op_prio(struct print_arg *arg)
1688 /* single ops are the greatest */
1689 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1692 arg->op.prio = get_op_prio(arg->op.op);
1694 return arg->op.prio;
1697 /* Note, *tok does not get freed, but will most likely be saved */
1698 static enum event_type
1699 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1701 struct print_arg *left, *right = NULL;
1702 enum event_type type;
1705 /* the op is passed in via tok */
1708 if (arg->type == PRINT_OP && !arg->op.left) {
1709 /* handle single op */
1711 do_warning("bad op token %s", token);
1721 do_warning("bad op token %s", token);
1726 /* make an empty left */
1728 left->type = PRINT_NULL;
1729 arg->op.left = left;
1731 right = alloc_arg();
1732 arg->op.right = right;
1734 /* do not free the token, it belongs to an op */
1736 type = process_arg(event, right, tok);
1738 } else if (strcmp(token, "?") == 0) {
1741 /* copy the top arg to the left */
1744 arg->type = PRINT_OP;
1746 arg->op.left = left;
1749 /* it will set arg->op.right */
1750 type = process_cond(event, arg, tok);
1752 } else if (strcmp(token, ">>") == 0 ||
1753 strcmp(token, "<<") == 0 ||
1754 strcmp(token, "&") == 0 ||
1755 strcmp(token, "|") == 0 ||
1756 strcmp(token, "&&") == 0 ||
1757 strcmp(token, "||") == 0 ||
1758 strcmp(token, "-") == 0 ||
1759 strcmp(token, "+") == 0 ||
1760 strcmp(token, "*") == 0 ||
1761 strcmp(token, "^") == 0 ||
1762 strcmp(token, "/") == 0 ||
1763 strcmp(token, "<") == 0 ||
1764 strcmp(token, ">") == 0 ||
1765 strcmp(token, "==") == 0 ||
1766 strcmp(token, "!=") == 0) {
1770 /* copy the top arg to the left */
1773 arg->type = PRINT_OP;
1775 arg->op.left = left;
1776 arg->op.right = NULL;
1778 if (set_op_prio(arg) == -1) {
1779 event->flags |= EVENT_FL_FAILED;
1780 /* arg->op.op (= token) will be freed at out_free */
1785 type = read_token_item(&token);
1788 /* could just be a type pointer */
1789 if ((strcmp(arg->op.op, "*") == 0) &&
1790 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1793 if (left->type != PRINT_ATOM) {
1794 do_warning("bad pointer type");
1797 new_atom = realloc(left->atom.atom,
1798 strlen(left->atom.atom) + 3);
1802 left->atom.atom = new_atom;
1803 strcat(left->atom.atom, " *");
1811 right = alloc_arg();
1812 type = process_arg_token(event, right, tok, type);
1813 arg->op.right = right;
1815 } else if (strcmp(token, "[") == 0) {
1820 arg->type = PRINT_OP;
1822 arg->op.left = left;
1826 /* it will set arg->op.right */
1827 type = process_array(event, arg, tok);
1830 do_warning("unknown op '%s'", token);
1831 event->flags |= EVENT_FL_FAILED;
1832 /* the arg is now the left side */
1836 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1839 /* higher prios need to be closer to the root */
1840 prio = get_op_prio(*tok);
1842 if (prio > arg->op.prio)
1843 return process_op(event, arg, tok);
1845 return process_op(event, right, tok);
1856 static enum event_type
1857 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1860 enum event_type type;
1864 if (read_expected(EVENT_OP, "->") < 0)
1867 if (read_expect_type(EVENT_ITEM, &token) < 0)
1871 arg->type = PRINT_FIELD;
1872 arg->field.name = field;
1874 if (is_flag_field) {
1875 arg->field.field = pevent_find_any_field(event, arg->field.name);
1876 arg->field.field->flags |= FIELD_IS_FLAG;
1878 } else if (is_symbolic_field) {
1879 arg->field.field = pevent_find_any_field(event, arg->field.name);
1880 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1881 is_symbolic_field = 0;
1884 type = read_token(&token);
1896 static char *arg_eval (struct print_arg *arg);
1898 static unsigned long long
1899 eval_type_str(unsigned long long val, const char *type, int pointer)
1909 if (type[len-1] != '*') {
1910 do_warning("pointer expected with non pointer type");
1916 do_warning("%s: not enough memory!", __func__);
1919 memcpy(ref, type, len);
1921 /* chop off the " *" */
1924 val = eval_type_str(val, ref, 0);
1929 /* check if this is a pointer */
1930 if (type[len - 1] == '*')
1933 /* Try to figure out the arg size*/
1934 if (strncmp(type, "struct", 6) == 0)
1938 if (strcmp(type, "u8") == 0)
1941 if (strcmp(type, "u16") == 0)
1942 return val & 0xffff;
1944 if (strcmp(type, "u32") == 0)
1945 return val & 0xffffffff;
1947 if (strcmp(type, "u64") == 0 ||
1948 strcmp(type, "s64"))
1951 if (strcmp(type, "s8") == 0)
1952 return (unsigned long long)(char)val & 0xff;
1954 if (strcmp(type, "s16") == 0)
1955 return (unsigned long long)(short)val & 0xffff;
1957 if (strcmp(type, "s32") == 0)
1958 return (unsigned long long)(int)val & 0xffffffff;
1960 if (strncmp(type, "unsigned ", 9) == 0) {
1965 if (strcmp(type, "char") == 0) {
1967 return (unsigned long long)(char)val & 0xff;
1972 if (strcmp(type, "short") == 0) {
1974 return (unsigned long long)(short)val & 0xffff;
1976 return val & 0xffff;
1979 if (strcmp(type, "int") == 0) {
1981 return (unsigned long long)(int)val & 0xffffffff;
1983 return val & 0xffffffff;
1990 * Try to figure out the type.
1992 static unsigned long long
1993 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1995 if (arg->type != PRINT_TYPE) {
1996 do_warning("expected type argument");
2000 return eval_type_str(val, arg->typecast.type, pointer);
2003 static int arg_num_eval(struct print_arg *arg, long long *val)
2005 long long left, right;
2008 switch (arg->type) {
2010 *val = strtoll(arg->atom.atom, NULL, 0);
2013 ret = arg_num_eval(arg->typecast.item, val);
2016 *val = eval_type(*val, arg, 0);
2019 switch (arg->op.op[0]) {
2021 ret = arg_num_eval(arg->op.left, &left);
2024 ret = arg_num_eval(arg->op.right, &right);
2028 *val = left || right;
2030 *val = left | right;
2033 ret = arg_num_eval(arg->op.left, &left);
2036 ret = arg_num_eval(arg->op.right, &right);
2040 *val = left && right;
2042 *val = left & right;
2045 ret = arg_num_eval(arg->op.left, &left);
2048 ret = arg_num_eval(arg->op.right, &right);
2051 switch (arg->op.op[1]) {
2053 *val = left < right;
2056 *val = left << right;
2059 *val = left <= right;
2062 do_warning("unknown op '%s'", arg->op.op);
2067 ret = arg_num_eval(arg->op.left, &left);
2070 ret = arg_num_eval(arg->op.right, &right);
2073 switch (arg->op.op[1]) {
2075 *val = left > right;
2078 *val = left >> right;
2081 *val = left >= right;
2084 do_warning("unknown op '%s'", arg->op.op);
2089 ret = arg_num_eval(arg->op.left, &left);
2092 ret = arg_num_eval(arg->op.right, &right);
2096 if (arg->op.op[1] != '=') {
2097 do_warning("unknown op '%s'", arg->op.op);
2100 *val = left == right;
2103 ret = arg_num_eval(arg->op.left, &left);
2106 ret = arg_num_eval(arg->op.right, &right);
2110 switch (arg->op.op[1]) {
2112 *val = left != right;
2115 do_warning("unknown op '%s'", arg->op.op);
2120 /* check for negative */
2121 if (arg->op.left->type == PRINT_NULL)
2124 ret = arg_num_eval(arg->op.left, &left);
2127 ret = arg_num_eval(arg->op.right, &right);
2130 *val = left - right;
2133 if (arg->op.left->type == PRINT_NULL)
2136 ret = arg_num_eval(arg->op.left, &left);
2139 ret = arg_num_eval(arg->op.right, &right);
2142 *val = left + right;
2145 do_warning("unknown op '%s'", arg->op.op);
2151 case PRINT_FIELD ... PRINT_SYMBOL:
2155 do_warning("invalid eval type %d", arg->type);
2162 static char *arg_eval (struct print_arg *arg)
2165 static char buf[20];
2167 switch (arg->type) {
2169 return arg->atom.atom;
2171 return arg_eval(arg->typecast.item);
2173 if (!arg_num_eval(arg, &val))
2175 sprintf(buf, "%lld", val);
2179 case PRINT_FIELD ... PRINT_SYMBOL:
2183 do_warning("invalid eval type %d", arg->type);
2190 static enum event_type
2191 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2193 enum event_type type;
2194 struct print_arg *arg = NULL;
2195 struct print_flag_sym *field;
2201 type = read_token_item(&token);
2202 if (test_type_token(type, token, EVENT_OP, "{"))
2208 type = process_arg(event, arg, &token);
2210 if (type == EVENT_OP)
2211 type = process_op(event, arg, &token);
2213 if (type == EVENT_ERROR)
2216 if (test_type_token(type, token, EVENT_DELIM, ","))
2219 field = calloc(1, sizeof(*field));
2223 value = arg_eval(arg);
2225 goto out_free_field;
2226 field->value = strdup(value);
2227 if (field->value == NULL)
2228 goto out_free_field;
2234 type = process_arg(event, arg, &token);
2235 if (test_type_token(type, token, EVENT_OP, "}"))
2236 goto out_free_field;
2238 value = arg_eval(arg);
2240 goto out_free_field;
2241 field->str = strdup(value);
2242 if (field->str == NULL)
2243 goto out_free_field;
2248 list = &field->next;
2251 type = read_token_item(&token);
2252 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2258 free_flag_sym(field);
2267 static enum event_type
2268 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2270 struct print_arg *field;
2271 enum event_type type;
2274 memset(arg, 0, sizeof(*arg));
2275 arg->type = PRINT_FLAGS;
2277 field = alloc_arg();
2279 type = process_arg(event, field, &token);
2281 /* Handle operations in the first argument */
2282 while (type == EVENT_OP)
2283 type = process_op(event, field, &token);
2285 if (test_type_token(type, token, EVENT_DELIM, ","))
2286 goto out_free_field;
2289 arg->flags.field = field;
2291 type = read_token_item(&token);
2292 if (event_item_type(type)) {
2293 arg->flags.delim = token;
2294 type = read_token_item(&token);
2297 if (test_type_token(type, token, EVENT_DELIM, ","))
2300 type = process_fields(event, &arg->flags.flags, &token);
2301 if (test_type_token(type, token, EVENT_DELIM, ")"))
2305 type = read_token_item(tok);
2316 static enum event_type
2317 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2319 struct print_arg *field;
2320 enum event_type type;
2323 memset(arg, 0, sizeof(*arg));
2324 arg->type = PRINT_SYMBOL;
2326 field = alloc_arg();
2328 type = process_arg(event, field, &token);
2329 if (test_type_token(type, token, EVENT_DELIM, ","))
2330 goto out_free_field;
2332 arg->symbol.field = field;
2334 type = process_fields(event, &arg->symbol.symbols, &token);
2335 if (test_type_token(type, token, EVENT_DELIM, ")"))
2339 type = read_token_item(tok);
2350 static enum event_type
2351 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2353 struct print_arg *field;
2354 enum event_type type;
2357 memset(arg, 0, sizeof(*arg));
2358 arg->type = PRINT_HEX;
2360 field = alloc_arg();
2361 type = process_arg(event, field, &token);
2363 if (test_type_token(type, token, EVENT_DELIM, ","))
2366 arg->hex.field = field;
2370 field = alloc_arg();
2371 type = process_arg(event, field, &token);
2373 if (test_type_token(type, token, EVENT_DELIM, ")"))
2376 arg->hex.size = field;
2379 type = read_token_item(tok);
2389 static enum event_type
2390 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2392 struct format_field *field;
2393 enum event_type type;
2396 memset(arg, 0, sizeof(*arg));
2397 arg->type = PRINT_DYNAMIC_ARRAY;
2400 * The item within the parenthesis is another field that holds
2401 * the index into where the array starts.
2403 type = read_token(&token);
2405 if (type != EVENT_ITEM)
2408 /* Find the field */
2410 field = pevent_find_field(event, token);
2414 arg->dynarray.field = field;
2415 arg->dynarray.index = 0;
2417 if (read_expected(EVENT_DELIM, ")") < 0)
2421 type = read_token_item(&token);
2423 if (type != EVENT_OP || strcmp(token, "[") != 0)
2428 type = process_arg(event, arg, &token);
2429 if (type == EVENT_ERROR)
2432 if (!test_type_token(type, token, EVENT_OP, "]"))
2436 type = read_token_item(tok);
2447 static enum event_type
2448 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2450 struct print_arg *item_arg;
2451 enum event_type type;
2454 type = process_arg(event, arg, &token);
2456 if (type == EVENT_ERROR)
2459 if (type == EVENT_OP)
2460 type = process_op(event, arg, &token);
2462 if (type == EVENT_ERROR)
2465 if (test_type_token(type, token, EVENT_DELIM, ")"))
2469 type = read_token_item(&token);
2472 * If the next token is an item or another open paren, then
2473 * this was a typecast.
2475 if (event_item_type(type) ||
2476 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2478 /* make this a typecast and contine */
2480 /* prevous must be an atom */
2481 if (arg->type != PRINT_ATOM) {
2482 do_warning("previous needed to be PRINT_ATOM");
2486 item_arg = alloc_arg();
2488 arg->type = PRINT_TYPE;
2489 arg->typecast.type = arg->atom.atom;
2490 arg->typecast.item = item_arg;
2491 type = process_arg_token(event, item_arg, &token, type);
2505 static enum event_type
2506 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2509 enum event_type type;
2512 if (read_expect_type(EVENT_ITEM, &token) < 0)
2515 arg->type = PRINT_STRING;
2516 arg->string.string = token;
2517 arg->string.offset = -1;
2519 if (read_expected(EVENT_DELIM, ")") < 0)
2522 type = read_token(&token);
2534 static struct pevent_function_handler *
2535 find_func_handler(struct pevent *pevent, char *func_name)
2537 struct pevent_function_handler *func;
2539 for (func = pevent->func_handlers; func; func = func->next) {
2540 if (strcmp(func->name, func_name) == 0)
2547 static void remove_func_handler(struct pevent *pevent, char *func_name)
2549 struct pevent_function_handler *func;
2550 struct pevent_function_handler **next;
2552 next = &pevent->func_handlers;
2553 while ((func = *next)) {
2554 if (strcmp(func->name, func_name) == 0) {
2556 free_func_handle(func);
2563 static enum event_type
2564 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2565 struct print_arg *arg, char **tok)
2567 struct print_arg **next_arg;
2568 struct print_arg *farg;
2569 enum event_type type;
2574 arg->type = PRINT_FUNC;
2575 arg->func.func = func;
2579 next_arg = &(arg->func.args);
2580 for (i = 0; i < func->nr_args; i++) {
2582 type = process_arg(event, farg, &token);
2583 if (i < (func->nr_args - 1))
2588 if (test_type_token(type, token, EVENT_DELIM, test)) {
2595 next_arg = &(farg->next);
2599 type = read_token(&token);
2605 static enum event_type
2606 process_function(struct event_format *event, struct print_arg *arg,
2607 char *token, char **tok)
2609 struct pevent_function_handler *func;
2611 if (strcmp(token, "__print_flags") == 0) {
2614 return process_flags(event, arg, tok);
2616 if (strcmp(token, "__print_symbolic") == 0) {
2618 is_symbolic_field = 1;
2619 return process_symbols(event, arg, tok);
2621 if (strcmp(token, "__print_hex") == 0) {
2623 return process_hex(event, arg, tok);
2625 if (strcmp(token, "__get_str") == 0) {
2627 return process_str(event, arg, tok);
2629 if (strcmp(token, "__get_dynamic_array") == 0) {
2631 return process_dynamic_array(event, arg, tok);
2634 func = find_func_handler(event->pevent, token);
2637 return process_func_handler(event, func, arg, tok);
2640 do_warning("function %s not defined", token);
2645 static enum event_type
2646 process_arg_token(struct event_format *event, struct print_arg *arg,
2647 char **tok, enum event_type type)
2656 if (strcmp(token, "REC") == 0) {
2658 type = process_entry(event, arg, &token);
2662 /* test the next token */
2663 type = read_token_item(&token);
2666 * If the next token is a parenthesis, then this
2669 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2672 /* this will free atom. */
2673 type = process_function(event, arg, atom, &token);
2676 /* atoms can be more than one token long */
2677 while (type == EVENT_ITEM) {
2679 new_atom = realloc(atom,
2680 strlen(atom) + strlen(token) + 2);
2689 strcat(atom, token);
2691 type = read_token_item(&token);
2694 arg->type = PRINT_ATOM;
2695 arg->atom.atom = atom;
2700 arg->type = PRINT_ATOM;
2701 arg->atom.atom = token;
2702 type = read_token_item(&token);
2705 if (strcmp(token, "(") == 0) {
2707 type = process_paren(event, arg, &token);
2711 /* handle single ops */
2712 arg->type = PRINT_OP;
2714 arg->op.left = NULL;
2715 type = process_op(event, arg, &token);
2717 /* On error, the op is freed */
2718 if (type == EVENT_ERROR)
2721 /* return error type if errored */
2724 case EVENT_ERROR ... EVENT_NEWLINE:
2726 do_warning("unexpected type %d", type);
2734 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2736 enum event_type type = EVENT_ERROR;
2737 struct print_arg *arg;
2742 if (type == EVENT_NEWLINE) {
2743 type = read_token_item(&token);
2749 type = process_arg(event, arg, &token);
2751 if (type == EVENT_ERROR) {
2760 if (type == EVENT_OP) {
2761 type = process_op(event, arg, &token);
2763 if (type == EVENT_ERROR) {
2772 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2779 } while (type != EVENT_NONE);
2781 if (type != EVENT_NONE && type != EVENT_ERROR)
2787 static int event_read_print(struct event_format *event)
2789 enum event_type type;
2793 if (read_expected_item(EVENT_ITEM, "print") < 0)
2796 if (read_expected(EVENT_ITEM, "fmt") < 0)
2799 if (read_expected(EVENT_OP, ":") < 0)
2802 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2806 event->print_fmt.format = token;
2807 event->print_fmt.args = NULL;
2809 /* ok to have no arg */
2810 type = read_token_item(&token);
2812 if (type == EVENT_NONE)
2815 /* Handle concatenation of print lines */
2816 if (type == EVENT_DQUOTE) {
2819 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2822 free_token(event->print_fmt.format);
2823 event->print_fmt.format = NULL;
2828 if (test_type_token(type, token, EVENT_DELIM, ","))
2833 ret = event_read_print_args(event, &event->print_fmt.args);
2845 * pevent_find_common_field - return a common field by event
2846 * @event: handle for the event
2847 * @name: the name of the common field to return
2849 * Returns a common field from the event by the given @name.
2850 * This only searchs the common fields and not all field.
2852 struct format_field *
2853 pevent_find_common_field(struct event_format *event, const char *name)
2855 struct format_field *format;
2857 for (format = event->format.common_fields;
2858 format; format = format->next) {
2859 if (strcmp(format->name, name) == 0)
2867 * pevent_find_field - find a non-common field
2868 * @event: handle for the event
2869 * @name: the name of the non-common field
2871 * Returns a non-common field by the given @name.
2872 * This does not search common fields.
2874 struct format_field *
2875 pevent_find_field(struct event_format *event, const char *name)
2877 struct format_field *format;
2879 for (format = event->format.fields;
2880 format; format = format->next) {
2881 if (strcmp(format->name, name) == 0)
2889 * pevent_find_any_field - find any field by name
2890 * @event: handle for the event
2891 * @name: the name of the field
2893 * Returns a field by the given @name.
2894 * This searchs the common field names first, then
2895 * the non-common ones if a common one was not found.
2897 struct format_field *
2898 pevent_find_any_field(struct event_format *event, const char *name)
2900 struct format_field *format;
2902 format = pevent_find_common_field(event, name);
2905 return pevent_find_field(event, name);
2909 * pevent_read_number - read a number from data
2910 * @pevent: handle for the pevent
2911 * @ptr: the raw data
2912 * @size: the size of the data that holds the number
2914 * Returns the number (converted to host) from the
2917 unsigned long long pevent_read_number(struct pevent *pevent,
2918 const void *ptr, int size)
2922 return *(unsigned char *)ptr;
2924 return data2host2(pevent, ptr);
2926 return data2host4(pevent, ptr);
2928 return data2host8(pevent, ptr);
2936 * pevent_read_number_field - read a number from data
2937 * @field: a handle to the field
2938 * @data: the raw data to read
2939 * @value: the value to place the number in
2941 * Reads raw data according to a field offset and size,
2942 * and translates it into @value.
2944 * Returns 0 on success, -1 otherwise.
2946 int pevent_read_number_field(struct format_field *field, const void *data,
2947 unsigned long long *value)
2951 switch (field->size) {
2956 *value = pevent_read_number(field->event->pevent,
2957 data + field->offset, field->size);
2964 static int get_common_info(struct pevent *pevent,
2965 const char *type, int *offset, int *size)
2967 struct event_format *event;
2968 struct format_field *field;
2971 * All events should have the same common elements.
2972 * Pick any event to find where the type is;
2974 if (!pevent->events) {
2975 do_warning("no event_list!");
2979 event = pevent->events[0];
2980 field = pevent_find_common_field(event, type);
2984 *offset = field->offset;
2985 *size = field->size;
2990 static int __parse_common(struct pevent *pevent, void *data,
2991 int *size, int *offset, const char *name)
2996 ret = get_common_info(pevent, name, offset, size);
3000 return pevent_read_number(pevent, data + *offset, *size);
3003 static int trace_parse_common_type(struct pevent *pevent, void *data)
3005 return __parse_common(pevent, data,
3006 &pevent->type_size, &pevent->type_offset,
3010 static int parse_common_pid(struct pevent *pevent, void *data)
3012 return __parse_common(pevent, data,
3013 &pevent->pid_size, &pevent->pid_offset,
3017 static int parse_common_pc(struct pevent *pevent, void *data)
3019 return __parse_common(pevent, data,
3020 &pevent->pc_size, &pevent->pc_offset,
3021 "common_preempt_count");
3024 static int parse_common_flags(struct pevent *pevent, void *data)
3026 return __parse_common(pevent, data,
3027 &pevent->flags_size, &pevent->flags_offset,
3031 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3033 return __parse_common(pevent, data,
3034 &pevent->ld_size, &pevent->ld_offset,
3035 "common_lock_depth");
3038 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3040 return __parse_common(pevent, data,
3041 &pevent->ld_size, &pevent->ld_offset,
3042 "common_migrate_disable");
3045 static int events_id_cmp(const void *a, const void *b);
3048 * pevent_find_event - find an event by given id
3049 * @pevent: a handle to the pevent
3050 * @id: the id of the event
3052 * Returns an event that has a given @id.
3054 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3056 struct event_format **eventptr;
3057 struct event_format key;
3058 struct event_format *pkey = &key;
3060 /* Check cache first */
3061 if (pevent->last_event && pevent->last_event->id == id)
3062 return pevent->last_event;
3066 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3067 sizeof(*pevent->events), events_id_cmp);
3070 pevent->last_event = *eventptr;
3078 * pevent_find_event_by_name - find an event by given name
3079 * @pevent: a handle to the pevent
3080 * @sys: the system name to search for
3081 * @name: the name of the event to search for
3083 * This returns an event with a given @name and under the system
3084 * @sys. If @sys is NULL the first event with @name is returned.
3086 struct event_format *
3087 pevent_find_event_by_name(struct pevent *pevent,
3088 const char *sys, const char *name)
3090 struct event_format *event;
3093 if (pevent->last_event &&
3094 strcmp(pevent->last_event->name, name) == 0 &&
3095 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3096 return pevent->last_event;
3098 for (i = 0; i < pevent->nr_events; i++) {
3099 event = pevent->events[i];
3100 if (strcmp(event->name, name) == 0) {
3103 if (strcmp(event->system, sys) == 0)
3107 if (i == pevent->nr_events)
3110 pevent->last_event = event;
3114 static unsigned long long
3115 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3117 struct pevent *pevent = event->pevent;
3118 unsigned long long val = 0;
3119 unsigned long long left, right;
3120 struct print_arg *typearg = NULL;
3121 struct print_arg *larg;
3122 unsigned long offset;
3123 unsigned int field_size;
3125 switch (arg->type) {
3130 return strtoull(arg->atom.atom, NULL, 0);
3132 if (!arg->field.field) {
3133 arg->field.field = pevent_find_any_field(event, arg->field.name);
3134 if (!arg->field.field)
3135 goto out_warning_field;
3138 /* must be a number */
3139 val = pevent_read_number(pevent, data + arg->field.field->offset,
3140 arg->field.field->size);
3147 val = eval_num_arg(data, size, event, arg->typecast.item);
3148 return eval_type(val, arg, 0);
3155 val = process_defined_func(&s, data, size, event, arg);
3156 trace_seq_destroy(&s);
3160 if (strcmp(arg->op.op, "[") == 0) {
3162 * Arrays are special, since we don't want
3163 * to read the arg as is.
3165 right = eval_num_arg(data, size, event, arg->op.right);
3167 /* handle typecasts */
3168 larg = arg->op.left;
3169 while (larg->type == PRINT_TYPE) {
3172 larg = larg->typecast.item;
3175 /* Default to long size */
3176 field_size = pevent->long_size;
3178 switch (larg->type) {
3179 case PRINT_DYNAMIC_ARRAY:
3180 offset = pevent_read_number(pevent,
3181 data + larg->dynarray.field->offset,
3182 larg->dynarray.field->size);
3183 if (larg->dynarray.field->elementsize)
3184 field_size = larg->dynarray.field->elementsize;
3186 * The actual length of the dynamic array is stored
3187 * in the top half of the field, and the offset
3188 * is in the bottom half of the 32 bit field.
3194 if (!larg->field.field) {
3196 pevent_find_any_field(event, larg->field.name);
3197 if (!larg->field.field) {
3199 goto out_warning_field;
3202 field_size = larg->field.field->elementsize;
3203 offset = larg->field.field->offset +
3204 right * larg->field.field->elementsize;
3207 goto default_op; /* oops, all bets off */
3209 val = pevent_read_number(pevent,
3210 data + offset, field_size);
3212 val = eval_type(val, typearg, 1);
3214 } else if (strcmp(arg->op.op, "?") == 0) {
3215 left = eval_num_arg(data, size, event, arg->op.left);
3216 arg = arg->op.right;
3218 val = eval_num_arg(data, size, event, arg->op.left);
3220 val = eval_num_arg(data, size, event, arg->op.right);
3224 left = eval_num_arg(data, size, event, arg->op.left);
3225 right = eval_num_arg(data, size, event, arg->op.right);
3226 switch (arg->op.op[0]) {
3228 switch (arg->op.op[1]) {
3233 val = left != right;
3236 goto out_warning_op;
3244 val = left || right;
3250 val = left && right;
3255 switch (arg->op.op[1]) {
3260 val = left << right;
3263 val = left <= right;
3266 goto out_warning_op;
3270 switch (arg->op.op[1]) {
3275 val = left >> right;
3278 val = left >= right;
3281 goto out_warning_op;
3285 if (arg->op.op[1] != '=')
3286 goto out_warning_op;
3288 val = left == right;
3303 goto out_warning_op;
3306 default: /* not sure what to do there */
3312 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3316 do_warning("%s: field %s not found", __func__, arg->field.name);
3322 unsigned long long value;
3325 static const struct flag flags[] = {
3326 { "HI_SOFTIRQ", 0 },
3327 { "TIMER_SOFTIRQ", 1 },
3328 { "NET_TX_SOFTIRQ", 2 },
3329 { "NET_RX_SOFTIRQ", 3 },
3330 { "BLOCK_SOFTIRQ", 4 },
3331 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3332 { "TASKLET_SOFTIRQ", 6 },
3333 { "SCHED_SOFTIRQ", 7 },
3334 { "HRTIMER_SOFTIRQ", 8 },
3335 { "RCU_SOFTIRQ", 9 },
3337 { "HRTIMER_NORESTART", 0 },
3338 { "HRTIMER_RESTART", 1 },
3341 static unsigned long long eval_flag(const char *flag)
3346 * Some flags in the format files do not get converted.
3347 * If the flag is not numeric, see if it is something that
3348 * we already know about.
3350 if (isdigit(flag[0]))
3351 return strtoull(flag, NULL, 0);
3353 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3354 if (strcmp(flags[i].name, flag) == 0)
3355 return flags[i].value;
3360 static void print_str_to_seq(struct trace_seq *s, const char *format,
3361 int len_arg, const char *str)
3364 trace_seq_printf(s, format, len_arg, str);
3366 trace_seq_printf(s, format, str);
3369 static void print_str_arg(struct trace_seq *s, void *data, int size,
3370 struct event_format *event, const char *format,
3371 int len_arg, struct print_arg *arg)
3373 struct pevent *pevent = event->pevent;
3374 struct print_flag_sym *flag;
3375 struct format_field *field;
3376 unsigned long long val, fval;
3383 switch (arg->type) {
3388 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3391 field = arg->field.field;
3393 field = pevent_find_any_field(event, arg->field.name);
3395 str = arg->field.name;
3396 goto out_warning_field;
3398 arg->field.field = field;
3400 /* Zero sized fields, mean the rest of the data */
3401 len = field->size ? : size - field->offset;
3404 * Some events pass in pointers. If this is not an array
3405 * and the size is the same as long_size, assume that it
3408 if (!(field->flags & FIELD_IS_ARRAY) &&
3409 field->size == pevent->long_size) {
3410 addr = *(unsigned long *)(data + field->offset);
3411 trace_seq_printf(s, "%lx", addr);
3414 str = malloc(len + 1);
3416 do_warning("%s: not enough memory!", __func__);
3419 memcpy(str, data + field->offset, len);
3421 print_str_to_seq(s, format, len_arg, str);
3425 val = eval_num_arg(data, size, event, arg->flags.field);
3427 for (flag = arg->flags.flags; flag; flag = flag->next) {
3428 fval = eval_flag(flag->value);
3429 if (!val && !fval) {
3430 print_str_to_seq(s, format, len_arg, flag->str);
3433 if (fval && (val & fval) == fval) {
3434 if (print && arg->flags.delim)
3435 trace_seq_puts(s, arg->flags.delim);
3436 print_str_to_seq(s, format, len_arg, flag->str);
3443 val = eval_num_arg(data, size, event, arg->symbol.field);
3444 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3445 fval = eval_flag(flag->value);
3447 print_str_to_seq(s, format, len_arg, flag->str);
3453 field = arg->hex.field->field.field;
3455 str = arg->hex.field->field.name;
3456 field = pevent_find_any_field(event, str);
3458 goto out_warning_field;
3459 arg->hex.field->field.field = field;
3461 hex = data + field->offset;
3462 len = eval_num_arg(data, size, event, arg->hex.size);
3463 for (i = 0; i < len; i++) {
3465 trace_seq_putc(s, ' ');
3466 trace_seq_printf(s, "%02x", hex[i]);
3472 case PRINT_STRING: {
3475 if (arg->string.offset == -1) {
3476 struct format_field *f;
3478 f = pevent_find_any_field(event, arg->string.string);
3479 arg->string.offset = f->offset;
3481 str_offset = data2host4(pevent, data + arg->string.offset);
3482 str_offset &= 0xffff;
3483 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3487 print_str_to_seq(s, format, len_arg, arg->string.string);
3491 * The only op for string should be ? :
3493 if (arg->op.op[0] != '?')
3495 val = eval_num_arg(data, size, event, arg->op.left);
3497 print_str_arg(s, data, size, event,
3498 format, len_arg, arg->op.right->op.left);
3500 print_str_arg(s, data, size, event,
3501 format, len_arg, arg->op.right->op.right);
3504 process_defined_func(s, data, size, event, arg);
3514 do_warning("%s: field %s not found", __func__, arg->field.name);
3517 static unsigned long long
3518 process_defined_func(struct trace_seq *s, void *data, int size,
3519 struct event_format *event, struct print_arg *arg)
3521 struct pevent_function_handler *func_handle = arg->func.func;
3522 struct pevent_func_params *param;
3523 unsigned long long *args;
3524 unsigned long long ret;
3525 struct print_arg *farg;
3526 struct trace_seq str;
3528 struct save_str *next;
3530 } *strings = NULL, *string;
3533 if (!func_handle->nr_args) {
3534 ret = (*func_handle->func)(s, NULL);
3538 farg = arg->func.args;
3539 param = func_handle->params;
3542 args = malloc(sizeof(*args) * func_handle->nr_args);
3546 for (i = 0; i < func_handle->nr_args; i++) {
3547 switch (param->type) {
3548 case PEVENT_FUNC_ARG_INT:
3549 case PEVENT_FUNC_ARG_LONG:
3550 case PEVENT_FUNC_ARG_PTR:
3551 args[i] = eval_num_arg(data, size, event, farg);
3553 case PEVENT_FUNC_ARG_STRING:
3554 trace_seq_init(&str);
3555 print_str_arg(&str, data, size, event, "%s", -1, farg);
3556 trace_seq_terminate(&str);
3557 string = malloc(sizeof(*string));
3559 do_warning("%s(%d): malloc str", __func__, __LINE__);
3562 string->next = strings;
3563 string->str = strdup(str.buffer);
3566 do_warning("%s(%d): malloc str", __func__, __LINE__);
3569 args[i] = (uintptr_t)string->str;
3571 trace_seq_destroy(&str);
3575 * Something went totally wrong, this is not
3576 * an input error, something in this code broke.
3578 do_warning("Unexpected end of arguments\n");
3582 param = param->next;
3585 ret = (*func_handle->func)(s, args);
3590 strings = string->next;
3596 /* TBD : handle return type here */
3600 static void free_args(struct print_arg *args)
3602 struct print_arg *next;
3612 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3614 struct pevent *pevent = event->pevent;
3615 struct format_field *field, *ip_field;
3616 struct print_arg *args, *arg, **next;
3617 unsigned long long ip, val;
3622 field = pevent->bprint_buf_field;
3623 ip_field = pevent->bprint_ip_field;
3626 field = pevent_find_field(event, "buf");
3628 do_warning("can't find buffer field for binary printk");
3631 ip_field = pevent_find_field(event, "ip");
3633 do_warning("can't find ip field for binary printk");
3636 pevent->bprint_buf_field = field;
3637 pevent->bprint_ip_field = ip_field;
3640 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3643 * The first arg is the IP pointer.
3650 arg->type = PRINT_ATOM;
3652 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3655 /* skip the first "%pf : " */
3656 for (ptr = fmt + 6, bptr = data + field->offset;
3657 bptr < data + size && *ptr; ptr++) {
3688 vsize = pevent->long_size;
3702 /* the pointers are always 4 bytes aligned */
3703 bptr = (void *)(((unsigned long)bptr + 3) &
3705 val = pevent_read_number(pevent, bptr, vsize);
3709 arg->type = PRINT_ATOM;
3710 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3717 * The '*' case means that an arg is used as the length.
3718 * We need to continue to figure out for what.
3727 arg->type = PRINT_BSTRING;
3728 arg->string.string = strdup(bptr);
3729 if (!arg->string.string)
3731 bptr += strlen(bptr) + 1;
3748 get_bprint_format(void *data, int size __maybe_unused,
3749 struct event_format *event)
3751 struct pevent *pevent = event->pevent;
3752 unsigned long long addr;
3753 struct format_field *field;
3754 struct printk_map *printk;
3758 field = pevent->bprint_fmt_field;
3761 field = pevent_find_field(event, "fmt");
3763 do_warning("can't find format field for binary printk");
3766 pevent->bprint_fmt_field = field;
3769 addr = pevent_read_number(pevent, data + field->offset, field->size);
3771 printk = find_printk(pevent, addr);
3773 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3779 /* Remove any quotes. */
3782 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3784 /* remove ending quotes and new line since we will add one too */
3785 p = format + strlen(format) - 1;
3790 if (strcmp(p, "\\n") == 0)
3796 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3797 struct event_format *event, struct print_arg *arg)
3800 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3802 if (arg->type == PRINT_FUNC) {
3803 process_defined_func(s, data, size, event, arg);
3807 if (arg->type != PRINT_FIELD) {
3808 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3814 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3815 if (!arg->field.field) {
3817 pevent_find_any_field(event, arg->field.name);
3818 if (!arg->field.field) {
3819 do_warning("%s: field %s not found",
3820 __func__, arg->field.name);
3824 if (arg->field.field->size != 6) {
3825 trace_seq_printf(s, "INVALIDMAC");
3828 buf = data + arg->field.field->offset;
3829 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3832 static int is_printable_array(char *p, unsigned int len)
3836 for (i = 0; i < len && p[i]; i++)
3842 static void print_event_fields(struct trace_seq *s, void *data, int size,
3843 struct event_format *event)
3845 struct format_field *field;
3846 unsigned long long val;
3847 unsigned int offset, len, i;
3849 field = event->format.fields;
3851 trace_seq_printf(s, " %s=", field->name);
3852 if (field->flags & FIELD_IS_ARRAY) {
3853 offset = field->offset;
3855 if (field->flags & FIELD_IS_DYNAMIC) {
3856 val = pevent_read_number(event->pevent, data + offset, len);
3861 if (field->flags & FIELD_IS_STRING &&
3862 is_printable_array(data + offset, len)) {
3863 trace_seq_printf(s, "%s", (char *)data + offset);
3865 trace_seq_puts(s, "ARRAY[");
3866 for (i = 0; i < len; i++) {
3868 trace_seq_puts(s, ", ");
3869 trace_seq_printf(s, "%02x",
3870 *((unsigned char *)data + offset + i));
3872 trace_seq_putc(s, ']');
3873 field->flags &= ~FIELD_IS_STRING;
3876 val = pevent_read_number(event->pevent, data + field->offset,
3878 if (field->flags & FIELD_IS_POINTER) {
3879 trace_seq_printf(s, "0x%llx", val);
3880 } else if (field->flags & FIELD_IS_SIGNED) {
3881 switch (field->size) {
3884 * If field is long then print it in hex.
3885 * A long usually stores pointers.
3887 if (field->flags & FIELD_IS_LONG)
3888 trace_seq_printf(s, "0x%x", (int)val);
3890 trace_seq_printf(s, "%d", (int)val);
3893 trace_seq_printf(s, "%2d", (short)val);
3896 trace_seq_printf(s, "%1d", (char)val);
3899 trace_seq_printf(s, "%lld", val);
3902 if (field->flags & FIELD_IS_LONG)
3903 trace_seq_printf(s, "0x%llx", val);
3905 trace_seq_printf(s, "%llu", val);
3908 field = field->next;
3912 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3914 struct pevent *pevent = event->pevent;
3915 struct print_fmt *print_fmt = &event->print_fmt;
3916 struct print_arg *arg = print_fmt->args;
3917 struct print_arg *args = NULL;
3918 const char *ptr = print_fmt->format;
3919 unsigned long long val;
3920 struct func_map *func;
3921 const char *saveptr;
3922 char *bprint_fmt = NULL;
3930 if (event->flags & EVENT_FL_FAILED) {
3931 trace_seq_printf(s, "[FAILED TO PARSE]");
3932 print_event_fields(s, data, size, event);
3936 if (event->flags & EVENT_FL_ISBPRINT) {
3937 bprint_fmt = get_bprint_format(data, size, event);
3938 args = make_bprint_args(bprint_fmt, data, size, event);
3943 for (; *ptr; ptr++) {
3949 trace_seq_putc(s, '\n');
3952 trace_seq_putc(s, '\t');
3955 trace_seq_putc(s, '\r');
3958 trace_seq_putc(s, '\\');
3961 trace_seq_putc(s, *ptr);
3965 } else if (*ptr == '%') {
3973 trace_seq_putc(s, '%');
3976 /* FIXME: need to handle properly */
3988 /* The argument is the length. */
3990 do_warning("no argument match");
3991 event->flags |= EVENT_FL_FAILED;
3994 len_arg = eval_num_arg(data, size, event, arg);
4004 if (pevent->long_size == 4)
4009 if (*(ptr+1) == 'F' ||
4013 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4014 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4027 do_warning("no argument match");
4028 event->flags |= EVENT_FL_FAILED;
4032 len = ((unsigned long)ptr + 1) -
4033 (unsigned long)saveptr;
4035 /* should never happen */
4037 do_warning("bad format!");
4038 event->flags |= EVENT_FL_FAILED;
4042 memcpy(format, saveptr, len);
4045 val = eval_num_arg(data, size, event, arg);
4049 func = find_func(pevent, val);
4051 trace_seq_puts(s, func->func);
4052 if (show_func == 'F')
4059 if (pevent->long_size == 8 && ls &&
4060 sizeof(long) != 8) {
4064 /* make %l into %ll */
4065 p = strchr(format, 'l');
4067 memmove(p+1, p, strlen(p)+1);
4068 else if (strcmp(format, "%p") == 0)
4069 strcpy(format, "0x%llx");
4074 trace_seq_printf(s, format, len_arg, (char)val);
4076 trace_seq_printf(s, format, (char)val);
4080 trace_seq_printf(s, format, len_arg, (short)val);
4082 trace_seq_printf(s, format, (short)val);
4086 trace_seq_printf(s, format, len_arg, (int)val);
4088 trace_seq_printf(s, format, (int)val);
4092 trace_seq_printf(s, format, len_arg, (long)val);
4094 trace_seq_printf(s, format, (long)val);
4098 trace_seq_printf(s, format, len_arg,
4101 trace_seq_printf(s, format, (long long)val);
4104 do_warning("bad count (%d)", ls);
4105 event->flags |= EVENT_FL_FAILED;
4110 do_warning("no matching argument");
4111 event->flags |= EVENT_FL_FAILED;
4115 len = ((unsigned long)ptr + 1) -
4116 (unsigned long)saveptr;
4118 /* should never happen */
4120 do_warning("bad format!");
4121 event->flags |= EVENT_FL_FAILED;
4125 memcpy(format, saveptr, len);
4129 print_str_arg(s, data, size, event,
4130 format, len_arg, arg);
4134 trace_seq_printf(s, ">%c<", *ptr);
4138 trace_seq_putc(s, *ptr);
4141 if (event->flags & EVENT_FL_FAILED) {
4143 trace_seq_printf(s, "[FAILED TO PARSE]");
4153 * pevent_data_lat_fmt - parse the data for the latency format
4154 * @pevent: a handle to the pevent
4155 * @s: the trace_seq to write to
4156 * @record: the record to read from
4158 * This parses out the Latency format (interrupts disabled,
4159 * need rescheduling, in hard/soft interrupt, preempt count
4160 * and lock depth) and places it into the trace_seq.
4162 void pevent_data_lat_fmt(struct pevent *pevent,
4163 struct trace_seq *s, struct pevent_record *record)
4165 static int check_lock_depth = 1;
4166 static int check_migrate_disable = 1;
4167 static int lock_depth_exists;
4168 static int migrate_disable_exists;
4169 unsigned int lat_flags;
4172 int migrate_disable;
4175 void *data = record->data;
4177 lat_flags = parse_common_flags(pevent, data);
4178 pc = parse_common_pc(pevent, data);
4179 /* lock_depth may not always exist */
4180 if (lock_depth_exists)
4181 lock_depth = parse_common_lock_depth(pevent, data);
4182 else if (check_lock_depth) {
4183 lock_depth = parse_common_lock_depth(pevent, data);
4185 check_lock_depth = 0;
4187 lock_depth_exists = 1;
4190 /* migrate_disable may not always exist */
4191 if (migrate_disable_exists)
4192 migrate_disable = parse_common_migrate_disable(pevent, data);
4193 else if (check_migrate_disable) {
4194 migrate_disable = parse_common_migrate_disable(pevent, data);
4195 if (migrate_disable < 0)
4196 check_migrate_disable = 0;
4198 migrate_disable_exists = 1;
4201 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4202 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4204 trace_seq_printf(s, "%c%c%c",
4205 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4206 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4208 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4210 (hardirq && softirq) ? 'H' :
4211 hardirq ? 'h' : softirq ? 's' : '.');
4214 trace_seq_printf(s, "%x", pc);
4216 trace_seq_putc(s, '.');
4218 if (migrate_disable_exists) {
4219 if (migrate_disable < 0)
4220 trace_seq_putc(s, '.');
4222 trace_seq_printf(s, "%d", migrate_disable);
4225 if (lock_depth_exists) {
4227 trace_seq_putc(s, '.');
4229 trace_seq_printf(s, "%d", lock_depth);
4232 trace_seq_terminate(s);
4236 * pevent_data_type - parse out the given event type
4237 * @pevent: a handle to the pevent
4238 * @rec: the record to read from
4240 * This returns the event id from the @rec.
4242 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4244 return trace_parse_common_type(pevent, rec->data);
4248 * pevent_data_event_from_type - find the event by a given type
4249 * @pevent: a handle to the pevent
4250 * @type: the type of the event.
4252 * This returns the event form a given @type;
4254 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4256 return pevent_find_event(pevent, type);
4260 * pevent_data_pid - parse the PID from raw data
4261 * @pevent: a handle to the pevent
4262 * @rec: the record to parse
4264 * This returns the PID from a raw data.
4266 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4268 return parse_common_pid(pevent, rec->data);
4272 * pevent_data_comm_from_pid - return the command line from PID
4273 * @pevent: a handle to the pevent
4274 * @pid: the PID of the task to search for
4276 * This returns a pointer to the command line that has the given
4279 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4283 comm = find_cmdline(pevent, pid);
4288 * pevent_data_comm_from_pid - parse the data into the print format
4289 * @s: the trace_seq to write to
4290 * @event: the handle to the event
4291 * @record: the record to read from
4293 * This parses the raw @data using the given @event information and
4294 * writes the print format into the trace_seq.
4296 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4297 struct pevent_record *record)
4299 int print_pretty = 1;
4301 if (event->pevent->print_raw)
4302 print_event_fields(s, record->data, record->size, event);
4306 print_pretty = event->handler(s, record, event,
4310 pretty_print(s, record->data, record->size, event);
4313 trace_seq_terminate(s);
4316 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4317 struct pevent_record *record)
4319 static char *spaces = " "; /* 20 spaces */
4320 struct event_format *event;
4322 unsigned long usecs;
4323 unsigned long nsecs;
4325 void *data = record->data;
4331 secs = record->ts / NSECS_PER_SEC;
4332 nsecs = record->ts - secs * NSECS_PER_SEC;
4334 if (record->size < 0) {
4335 do_warning("ug! negative record size %d", record->size);
4339 type = trace_parse_common_type(pevent, data);
4341 event = pevent_find_event(pevent, type);
4343 do_warning("ug! no event found for type %d", type);
4347 pid = parse_common_pid(pevent, data);
4348 comm = find_cmdline(pevent, pid);
4350 if (pevent->latency_format) {
4351 trace_seq_printf(s, "%8.8s-%-5d %3d",
4352 comm, pid, record->cpu);
4353 pevent_data_lat_fmt(pevent, s, record);
4355 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4357 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4361 usecs = (nsecs + 500) / NSECS_PER_USEC;
4365 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4367 /* Space out the event names evenly. */
4368 len = strlen(event->name);
4370 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4372 pevent_event_info(s, event, record);
4375 static int events_id_cmp(const void *a, const void *b)
4377 struct event_format * const * ea = a;
4378 struct event_format * const * eb = b;
4380 if ((*ea)->id < (*eb)->id)
4383 if ((*ea)->id > (*eb)->id)
4389 static int events_name_cmp(const void *a, const void *b)
4391 struct event_format * const * ea = a;
4392 struct event_format * const * eb = b;
4395 res = strcmp((*ea)->name, (*eb)->name);
4399 res = strcmp((*ea)->system, (*eb)->system);
4403 return events_id_cmp(a, b);
4406 static int events_system_cmp(const void *a, const void *b)
4408 struct event_format * const * ea = a;
4409 struct event_format * const * eb = b;
4412 res = strcmp((*ea)->system, (*eb)->system);
4416 res = strcmp((*ea)->name, (*eb)->name);
4420 return events_id_cmp(a, b);
4423 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4425 struct event_format **events;
4426 int (*sort)(const void *a, const void *b);
4428 events = pevent->sort_events;
4430 if (events && pevent->last_type == sort_type)
4434 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4438 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4439 events[pevent->nr_events] = NULL;
4441 pevent->sort_events = events;
4443 /* the internal events are sorted by id */
4444 if (sort_type == EVENT_SORT_ID) {
4445 pevent->last_type = sort_type;
4450 switch (sort_type) {
4452 sort = events_id_cmp;
4454 case EVENT_SORT_NAME:
4455 sort = events_name_cmp;
4457 case EVENT_SORT_SYSTEM:
4458 sort = events_system_cmp;
4464 qsort(events, pevent->nr_events, sizeof(*events), sort);
4465 pevent->last_type = sort_type;
4470 static struct format_field **
4471 get_event_fields(const char *type, const char *name,
4472 int count, struct format_field *list)
4474 struct format_field **fields;
4475 struct format_field *field;
4478 fields = malloc(sizeof(*fields) * (count + 1));
4482 for (field = list; field; field = field->next) {
4483 fields[i++] = field;
4484 if (i == count + 1) {
4485 do_warning("event %s has more %s fields than specified",
4493 do_warning("event %s has less %s fields than specified",
4502 * pevent_event_common_fields - return a list of common fields for an event
4503 * @event: the event to return the common fields of.
4505 * Returns an allocated array of fields. The last item in the array is NULL.
4506 * The array must be freed with free().
4508 struct format_field **pevent_event_common_fields(struct event_format *event)
4510 return get_event_fields("common", event->name,
4511 event->format.nr_common,
4512 event->format.common_fields);
4516 * pevent_event_fields - return a list of event specific fields for an event
4517 * @event: the event to return the fields of.
4519 * Returns an allocated array of fields. The last item in the array is NULL.
4520 * The array must be freed with free().
4522 struct format_field **pevent_event_fields(struct event_format *event)
4524 return get_event_fields("event", event->name,
4525 event->format.nr_fields,
4526 event->format.fields);
4529 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4531 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4533 trace_seq_puts(s, ", ");
4534 print_fields(s, field->next);
4539 static void print_args(struct print_arg *args)
4541 int print_paren = 1;
4544 switch (args->type) {
4549 printf("%s", args->atom.atom);
4552 printf("REC->%s", args->field.name);
4555 printf("__print_flags(");
4556 print_args(args->flags.field);
4557 printf(", %s, ", args->flags.delim);
4559 print_fields(&s, args->flags.flags);
4560 trace_seq_do_printf(&s);
4561 trace_seq_destroy(&s);
4565 printf("__print_symbolic(");
4566 print_args(args->symbol.field);
4569 print_fields(&s, args->symbol.symbols);
4570 trace_seq_do_printf(&s);
4571 trace_seq_destroy(&s);
4575 printf("__print_hex(");
4576 print_args(args->hex.field);
4578 print_args(args->hex.size);
4583 printf("__get_str(%s)", args->string.string);
4586 printf("(%s)", args->typecast.type);
4587 print_args(args->typecast.item);
4590 if (strcmp(args->op.op, ":") == 0)
4594 print_args(args->op.left);
4595 printf(" %s ", args->op.op);
4596 print_args(args->op.right);
4601 /* we should warn... */
4606 print_args(args->next);
4610 static void parse_header_field(const char *field,
4611 int *offset, int *size, int mandatory)
4613 unsigned long long save_input_buf_ptr;
4614 unsigned long long save_input_buf_siz;
4618 save_input_buf_ptr = input_buf_ptr;
4619 save_input_buf_siz = input_buf_siz;
4621 if (read_expected(EVENT_ITEM, "field") < 0)
4623 if (read_expected(EVENT_OP, ":") < 0)
4627 if (read_expect_type(EVENT_ITEM, &token) < 0)
4632 * If this is not a mandatory field, then test it first.
4635 if (read_expected(EVENT_ITEM, field) < 0)
4638 if (read_expect_type(EVENT_ITEM, &token) < 0)
4640 if (strcmp(token, field) != 0)
4645 if (read_expected(EVENT_OP, ";") < 0)
4647 if (read_expected(EVENT_ITEM, "offset") < 0)
4649 if (read_expected(EVENT_OP, ":") < 0)
4651 if (read_expect_type(EVENT_ITEM, &token) < 0)
4653 *offset = atoi(token);
4655 if (read_expected(EVENT_OP, ";") < 0)
4657 if (read_expected(EVENT_ITEM, "size") < 0)
4659 if (read_expected(EVENT_OP, ":") < 0)
4661 if (read_expect_type(EVENT_ITEM, &token) < 0)
4663 *size = atoi(token);
4665 if (read_expected(EVENT_OP, ";") < 0)
4667 type = read_token(&token);
4668 if (type != EVENT_NEWLINE) {
4669 /* newer versions of the kernel have a "signed" type */
4670 if (type != EVENT_ITEM)
4673 if (strcmp(token, "signed") != 0)
4678 if (read_expected(EVENT_OP, ":") < 0)
4681 if (read_expect_type(EVENT_ITEM, &token))
4685 if (read_expected(EVENT_OP, ";") < 0)
4688 if (read_expect_type(EVENT_NEWLINE, &token))
4696 input_buf_ptr = save_input_buf_ptr;
4697 input_buf_siz = save_input_buf_siz;
4704 * pevent_parse_header_page - parse the data stored in the header page
4705 * @pevent: the handle to the pevent
4706 * @buf: the buffer storing the header page format string
4707 * @size: the size of @buf
4708 * @long_size: the long size to use if there is no header
4710 * This parses the header page format for information on the
4711 * ring buffer used. The @buf should be copied from
4713 * /sys/kernel/debug/tracing/events/header_page
4715 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4722 * Old kernels did not have header page info.
4723 * Sorry but we just use what we find here in user space.
4725 pevent->header_page_ts_size = sizeof(long long);
4726 pevent->header_page_size_size = long_size;
4727 pevent->header_page_data_offset = sizeof(long long) + long_size;
4728 pevent->old_format = 1;
4731 init_input_buf(buf, size);
4733 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4734 &pevent->header_page_ts_size, 1);
4735 parse_header_field("commit", &pevent->header_page_size_offset,
4736 &pevent->header_page_size_size, 1);
4737 parse_header_field("overwrite", &pevent->header_page_overwrite,
4739 parse_header_field("data", &pevent->header_page_data_offset,
4740 &pevent->header_page_data_size, 1);
4745 static int event_matches(struct event_format *event,
4746 int id, const char *sys_name,
4747 const char *event_name)
4749 if (id >= 0 && id != event->id)
4752 if (event_name && (strcmp(event_name, event->name) != 0))
4755 if (sys_name && (strcmp(sys_name, event->system) != 0))
4761 static void free_handler(struct event_handler *handle)
4763 free((void *)handle->sys_name);
4764 free((void *)handle->event_name);
4768 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4770 struct event_handler *handle, **next;
4772 for (next = &pevent->handlers; *next;
4773 next = &(*next)->next) {
4775 if (event_matches(event, handle->id,
4777 handle->event_name))
4784 pr_stat("overriding event (%d) %s:%s with new print handler",
4785 event->id, event->system, event->name);
4787 event->handler = handle->func;
4788 event->context = handle->context;
4790 *next = handle->next;
4791 free_handler(handle);
4797 * pevent_parse_event - parse the event format
4798 * @pevent: the handle to the pevent
4799 * @buf: the buffer storing the event format string
4800 * @size: the size of @buf
4801 * @sys: the system the event belongs to
4803 * This parses the event format and creates an event structure
4804 * to quickly parse raw data for a given event.
4806 * These files currently come from:
4808 * /sys/kernel/debug/tracing/events/.../.../format
4810 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
4811 unsigned long size, const char *sys)
4813 struct event_format *event;
4816 init_input_buf(buf, size);
4818 event = alloc_event();
4820 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4822 event->name = event_read_name();
4825 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4826 goto event_alloc_failed;
4829 if (strcmp(sys, "ftrace") == 0) {
4830 event->flags |= EVENT_FL_ISFTRACE;
4832 if (strcmp(event->name, "bprint") == 0)
4833 event->flags |= EVENT_FL_ISBPRINT;
4836 event->id = event_read_id();
4837 if (event->id < 0) {
4838 ret = PEVENT_ERRNO__READ_ID_FAILED;
4840 * This isn't an allocation error actually.
4841 * But as the ID is critical, just bail out.
4843 goto event_alloc_failed;
4846 event->system = strdup(sys);
4847 if (!event->system) {
4848 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4849 goto event_alloc_failed;
4852 /* Add pevent to event so that it can be referenced */
4853 event->pevent = pevent;
4855 ret = event_read_format(event);
4857 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4858 goto event_parse_failed;
4862 * If the event has an override, don't print warnings if the event
4863 * print format fails to parse.
4865 if (find_event_handle(pevent, event))
4868 ret = event_read_print(event);
4871 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4872 goto event_parse_failed;
4876 if (add_event(pevent, event))
4877 goto event_alloc_failed;
4879 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4880 struct format_field *field;
4881 struct print_arg *arg, **list;
4883 /* old ftrace had no args */
4884 list = &event->print_fmt.args;
4885 for (field = event->format.fields; field; field = field->next) {
4887 arg->type = PRINT_FIELD;
4888 arg->field.name = strdup(field->name);
4889 if (!arg->field.name) {
4890 event->flags |= EVENT_FL_FAILED;
4892 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4894 arg->field.field = field;
4901 #define PRINT_ARGS 0
4902 if (PRINT_ARGS && event->print_fmt.args)
4903 print_args(event->print_fmt.args);
4908 event->flags |= EVENT_FL_FAILED;
4909 /* still add it even if it failed */
4910 if (add_event(pevent, event))
4911 goto event_alloc_failed;
4916 free(event->system);
4923 #define _PE(code, str) str
4924 static const char * const pevent_error_str[] = {
4929 int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
4930 char *buf, size_t buflen)
4936 msg = strerror_r(errnum, buf, buflen);
4938 size_t len = strlen(msg);
4939 memcpy(buf, msg, min(buflen - 1, len));
4940 *(buf + min(buflen - 1, len)) = '\0';
4945 if (errnum <= __PEVENT_ERRNO__START ||
4946 errnum >= __PEVENT_ERRNO__END)
4949 idx = errnum - __PEVENT_ERRNO__START - 1;
4950 msg = pevent_error_str[idx];
4953 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
4954 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
4955 case PEVENT_ERRNO__READ_ID_FAILED:
4956 case PEVENT_ERRNO__READ_FORMAT_FAILED:
4957 case PEVENT_ERRNO__READ_PRINT_FAILED:
4958 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
4959 snprintf(buf, buflen, "%s", msg);
4963 /* cannot reach here */
4970 int get_field_val(struct trace_seq *s, struct format_field *field,
4971 const char *name, struct pevent_record *record,
4972 unsigned long long *val, int err)
4976 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4980 if (pevent_read_number_field(field, record->data, val)) {
4982 trace_seq_printf(s, " %s=INVALID", name);
4990 * pevent_get_field_raw - return the raw pointer into the data field
4991 * @s: The seq to print to on error
4992 * @event: the event that the field is for
4993 * @name: The name of the field
4994 * @record: The record with the field name.
4995 * @len: place to store the field length.
4996 * @err: print default error if failed.
4998 * Returns a pointer into record->data of the field and places
4999 * the length of the field in @len.
5001 * On failure, it returns NULL.
5003 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5004 const char *name, struct pevent_record *record,
5007 struct format_field *field;
5008 void *data = record->data;
5015 field = pevent_find_field(event, name);
5019 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5023 /* Allow @len to be NULL */
5027 offset = field->offset;
5028 if (field->flags & FIELD_IS_DYNAMIC) {
5029 offset = pevent_read_number(event->pevent,
5030 data + offset, field->size);
5031 *len = offset >> 16;
5036 return data + offset;
5040 * pevent_get_field_val - find a field and return its value
5041 * @s: The seq to print to on error
5042 * @event: the event that the field is for
5043 * @name: The name of the field
5044 * @record: The record with the field name.
5045 * @val: place to store the value of the field.
5046 * @err: print default error if failed.
5048 * Returns 0 on success -1 on field not found.
5050 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5051 const char *name, struct pevent_record *record,
5052 unsigned long long *val, int err)
5054 struct format_field *field;
5059 field = pevent_find_field(event, name);
5061 return get_field_val(s, field, name, record, val, err);
5065 * pevent_get_common_field_val - find a common field and return its value
5066 * @s: The seq to print to on error
5067 * @event: the event that the field is for
5068 * @name: The name of the field
5069 * @record: The record with the field name.
5070 * @val: place to store the value of the field.
5071 * @err: print default error if failed.
5073 * Returns 0 on success -1 on field not found.
5075 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5076 const char *name, struct pevent_record *record,
5077 unsigned long long *val, int err)
5079 struct format_field *field;
5084 field = pevent_find_common_field(event, name);
5086 return get_field_val(s, field, name, record, val, err);
5090 * pevent_get_any_field_val - find a any field and return its value
5091 * @s: The seq to print to on error
5092 * @event: the event that the field is for
5093 * @name: The name of the field
5094 * @record: The record with the field name.
5095 * @val: place to store the value of the field.
5096 * @err: print default error if failed.
5098 * Returns 0 on success -1 on field not found.
5100 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5101 const char *name, struct pevent_record *record,
5102 unsigned long long *val, int err)
5104 struct format_field *field;
5109 field = pevent_find_any_field(event, name);
5111 return get_field_val(s, field, name, record, val, err);
5115 * pevent_print_num_field - print a field and a format
5116 * @s: The seq to print to
5117 * @fmt: The printf format to print the field with.
5118 * @event: the event that the field is for
5119 * @name: The name of the field
5120 * @record: The record with the field name.
5121 * @err: print default error if failed.
5123 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5125 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5126 struct event_format *event, const char *name,
5127 struct pevent_record *record, int err)
5129 struct format_field *field = pevent_find_field(event, name);
5130 unsigned long long val;
5135 if (pevent_read_number_field(field, record->data, &val))
5138 return trace_seq_printf(s, fmt, val);
5142 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5146 static void free_func_handle(struct pevent_function_handler *func)
5148 struct pevent_func_params *params;
5152 while (func->params) {
5153 params = func->params;
5154 func->params = params->next;
5162 * pevent_register_print_function - register a helper function
5163 * @pevent: the handle to the pevent
5164 * @func: the function to process the helper function
5165 * @ret_type: the return type of the helper function
5166 * @name: the name of the helper function
5167 * @parameters: A list of enum pevent_func_arg_type
5169 * Some events may have helper functions in the print format arguments.
5170 * This allows a plugin to dynamically create a way to process one
5171 * of these functions.
5173 * The @parameters is a variable list of pevent_func_arg_type enums that
5174 * must end with PEVENT_FUNC_ARG_VOID.
5176 int pevent_register_print_function(struct pevent *pevent,
5177 pevent_func_handler func,
5178 enum pevent_func_arg_type ret_type,
5181 struct pevent_function_handler *func_handle;
5182 struct pevent_func_params **next_param;
5183 struct pevent_func_params *param;
5184 enum pevent_func_arg_type type;
5188 func_handle = find_func_handler(pevent, name);
5191 * This is most like caused by the users own
5192 * plugins updating the function. This overrides the
5195 pr_stat("override of function helper '%s'", name);
5196 remove_func_handler(pevent, name);
5199 func_handle = calloc(1, sizeof(*func_handle));
5201 do_warning("Failed to allocate function handler");
5202 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5205 func_handle->ret_type = ret_type;
5206 func_handle->name = strdup(name);
5207 func_handle->func = func;
5208 if (!func_handle->name) {
5209 do_warning("Failed to allocate function name");
5211 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5214 next_param = &(func_handle->params);
5217 type = va_arg(ap, enum pevent_func_arg_type);
5218 if (type == PEVENT_FUNC_ARG_VOID)
5221 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5222 do_warning("Invalid argument type %d", type);
5223 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5227 param = malloc(sizeof(*param));
5229 do_warning("Failed to allocate function param");
5230 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5236 *next_param = param;
5237 next_param = &(param->next);
5239 func_handle->nr_args++;
5243 func_handle->next = pevent->func_handlers;
5244 pevent->func_handlers = func_handle;
5249 free_func_handle(func_handle);
5254 * pevent_register_event_handler - register a way to parse an event
5255 * @pevent: the handle to the pevent
5256 * @id: the id of the event to register
5257 * @sys_name: the system name the event belongs to
5258 * @event_name: the name of the event
5259 * @func: the function to call to parse the event information
5260 * @context: the data to be passed to @func
5262 * This function allows a developer to override the parsing of
5263 * a given event. If for some reason the default print format
5264 * is not sufficient, this function will register a function
5265 * for an event to be used to parse the data instead.
5267 * If @id is >= 0, then it is used to find the event.
5268 * else @sys_name and @event_name are used.
5270 int pevent_register_event_handler(struct pevent *pevent,
5271 int id, char *sys_name, char *event_name,
5272 pevent_event_handler_func func,
5275 struct event_format *event;
5276 struct event_handler *handle;
5280 event = pevent_find_event(pevent, id);
5283 if (event_name && (strcmp(event_name, event->name) != 0))
5285 if (sys_name && (strcmp(sys_name, event->system) != 0))
5288 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5293 pr_stat("overriding event (%d) %s:%s with new print handler",
5294 event->id, event->system, event->name);
5296 event->handler = func;
5297 event->context = context;
5301 /* Save for later use. */
5302 handle = calloc(1, sizeof(*handle));
5304 do_warning("Failed to allocate event handler");
5305 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5310 handle->event_name = strdup(event_name);
5312 handle->sys_name = strdup(sys_name);
5314 if ((event_name && !handle->event_name) ||
5315 (sys_name && !handle->sys_name)) {
5316 do_warning("Failed to allocate event/sys name");
5317 free((void *)handle->event_name);
5318 free((void *)handle->sys_name);
5320 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5323 handle->func = func;
5324 handle->next = pevent->handlers;
5325 pevent->handlers = handle;
5326 handle->context = context;
5332 * pevent_alloc - create a pevent handle
5334 struct pevent *pevent_alloc(void)
5336 struct pevent *pevent = calloc(1, sizeof(*pevent));
5339 pevent->ref_count = 1;
5344 void pevent_ref(struct pevent *pevent)
5346 pevent->ref_count++;
5349 static void free_format_fields(struct format_field *field)
5351 struct format_field *next;
5362 static void free_formats(struct format *format)
5364 free_format_fields(format->common_fields);
5365 free_format_fields(format->fields);
5368 static void free_event(struct event_format *event)
5371 free(event->system);
5373 free_formats(&event->format);
5375 free(event->print_fmt.format);
5376 free_args(event->print_fmt.args);
5382 * pevent_free - free a pevent handle
5383 * @pevent: the pevent handle to free
5385 void pevent_free(struct pevent *pevent)
5387 struct cmdline_list *cmdlist, *cmdnext;
5388 struct func_list *funclist, *funcnext;
5389 struct printk_list *printklist, *printknext;
5390 struct pevent_function_handler *func_handler;
5391 struct event_handler *handle;
5397 cmdlist = pevent->cmdlist;
5398 funclist = pevent->funclist;
5399 printklist = pevent->printklist;
5401 pevent->ref_count--;
5402 if (pevent->ref_count)
5405 if (pevent->cmdlines) {
5406 for (i = 0; i < pevent->cmdline_count; i++)
5407 free(pevent->cmdlines[i].comm);
5408 free(pevent->cmdlines);
5412 cmdnext = cmdlist->next;
5413 free(cmdlist->comm);
5418 if (pevent->func_map) {
5419 for (i = 0; i < pevent->func_count; i++) {
5420 free(pevent->func_map[i].func);
5421 free(pevent->func_map[i].mod);
5423 free(pevent->func_map);
5427 funcnext = funclist->next;
5428 free(funclist->func);
5429 free(funclist->mod);
5431 funclist = funcnext;
5434 while (pevent->func_handlers) {
5435 func_handler = pevent->func_handlers;
5436 pevent->func_handlers = func_handler->next;
5437 free_func_handle(func_handler);
5440 if (pevent->printk_map) {
5441 for (i = 0; i < pevent->printk_count; i++)
5442 free(pevent->printk_map[i].printk);
5443 free(pevent->printk_map);
5446 while (printklist) {
5447 printknext = printklist->next;
5448 free(printklist->printk);
5450 printklist = printknext;
5453 for (i = 0; i < pevent->nr_events; i++)
5454 free_event(pevent->events[i]);
5456 while (pevent->handlers) {
5457 handle = pevent->handlers;
5458 pevent->handlers = handle->next;
5459 free_handler(handle);
5462 free(pevent->events);
5463 free(pevent->sort_events);
5468 void pevent_unref(struct pevent *pevent)
5470 pevent_free(pevent);