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 const 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 if (!arg || !left || !right) {
1569 do_warning("%s: not enough memory!", __func__);
1570 /* arg will be freed at out_free */
1576 arg->type = PRINT_OP;
1577 arg->op.left = left;
1578 arg->op.right = right;
1581 type = process_arg(event, left, &token);
1584 /* Handle other operations in the arguments */
1585 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1586 type = process_op(event, left, &token);
1590 if (test_type_token(type, token, EVENT_OP, ":"))
1595 type = process_arg(event, right, &token);
1597 top->op.right = arg;
1603 /* Top may point to itself */
1604 top->op.right = NULL;
1610 static enum event_type
1611 process_array(struct event_format *event, struct print_arg *top, char **tok)
1613 struct print_arg *arg;
1614 enum event_type type;
1619 do_warning("%s: not enough memory!", __func__);
1620 /* '*tok' is set to top->op.op. No need to free. */
1626 type = process_arg(event, arg, &token);
1627 if (test_type_token(type, token, EVENT_OP, "]"))
1630 top->op.right = arg;
1633 type = read_token_item(&token);
1644 static int get_op_prio(char *op)
1658 /* '>>' and '<<' are 8 */
1662 /* '==' and '!=' are 10 */
1672 do_warning("unknown op '%c'", op[0]);
1676 if (strcmp(op, "++") == 0 ||
1677 strcmp(op, "--") == 0) {
1679 } else if (strcmp(op, ">>") == 0 ||
1680 strcmp(op, "<<") == 0) {
1682 } else if (strcmp(op, ">=") == 0 ||
1683 strcmp(op, "<=") == 0) {
1685 } else if (strcmp(op, "==") == 0 ||
1686 strcmp(op, "!=") == 0) {
1688 } else if (strcmp(op, "&&") == 0) {
1690 } else if (strcmp(op, "||") == 0) {
1693 do_warning("unknown op '%s'", op);
1699 static int set_op_prio(struct print_arg *arg)
1702 /* single ops are the greatest */
1703 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1706 arg->op.prio = get_op_prio(arg->op.op);
1708 return arg->op.prio;
1711 /* Note, *tok does not get freed, but will most likely be saved */
1712 static enum event_type
1713 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1715 struct print_arg *left, *right = NULL;
1716 enum event_type type;
1719 /* the op is passed in via tok */
1722 if (arg->type == PRINT_OP && !arg->op.left) {
1723 /* handle single op */
1725 do_warning("bad op token %s", token);
1735 do_warning("bad op token %s", token);
1740 /* make an empty left */
1745 left->type = PRINT_NULL;
1746 arg->op.left = left;
1748 right = alloc_arg();
1752 arg->op.right = right;
1754 /* do not free the token, it belongs to an op */
1756 type = process_arg(event, right, tok);
1758 } else if (strcmp(token, "?") == 0) {
1764 /* copy the top arg to the left */
1767 arg->type = PRINT_OP;
1769 arg->op.left = left;
1772 /* it will set arg->op.right */
1773 type = process_cond(event, arg, tok);
1775 } else if (strcmp(token, ">>") == 0 ||
1776 strcmp(token, "<<") == 0 ||
1777 strcmp(token, "&") == 0 ||
1778 strcmp(token, "|") == 0 ||
1779 strcmp(token, "&&") == 0 ||
1780 strcmp(token, "||") == 0 ||
1781 strcmp(token, "-") == 0 ||
1782 strcmp(token, "+") == 0 ||
1783 strcmp(token, "*") == 0 ||
1784 strcmp(token, "^") == 0 ||
1785 strcmp(token, "/") == 0 ||
1786 strcmp(token, "<") == 0 ||
1787 strcmp(token, ">") == 0 ||
1788 strcmp(token, "==") == 0 ||
1789 strcmp(token, "!=") == 0) {
1795 /* copy the top arg to the left */
1798 arg->type = PRINT_OP;
1800 arg->op.left = left;
1801 arg->op.right = NULL;
1803 if (set_op_prio(arg) == -1) {
1804 event->flags |= EVENT_FL_FAILED;
1805 /* arg->op.op (= token) will be freed at out_free */
1810 type = read_token_item(&token);
1813 /* could just be a type pointer */
1814 if ((strcmp(arg->op.op, "*") == 0) &&
1815 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1818 if (left->type != PRINT_ATOM) {
1819 do_warning("bad pointer type");
1822 new_atom = realloc(left->atom.atom,
1823 strlen(left->atom.atom) + 3);
1827 left->atom.atom = new_atom;
1828 strcat(left->atom.atom, " *");
1836 right = alloc_arg();
1840 type = process_arg_token(event, right, tok, type);
1841 arg->op.right = right;
1843 } else if (strcmp(token, "[") == 0) {
1851 arg->type = PRINT_OP;
1853 arg->op.left = left;
1857 /* it will set arg->op.right */
1858 type = process_array(event, arg, tok);
1861 do_warning("unknown op '%s'", token);
1862 event->flags |= EVENT_FL_FAILED;
1863 /* the arg is now the left side */
1867 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1870 /* higher prios need to be closer to the root */
1871 prio = get_op_prio(*tok);
1873 if (prio > arg->op.prio)
1874 return process_op(event, arg, tok);
1876 return process_op(event, right, tok);
1882 do_warning("%s: not enough memory!", __func__);
1889 static enum event_type
1890 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1893 enum event_type type;
1897 if (read_expected(EVENT_OP, "->") < 0)
1900 if (read_expect_type(EVENT_ITEM, &token) < 0)
1904 arg->type = PRINT_FIELD;
1905 arg->field.name = field;
1907 if (is_flag_field) {
1908 arg->field.field = pevent_find_any_field(event, arg->field.name);
1909 arg->field.field->flags |= FIELD_IS_FLAG;
1911 } else if (is_symbolic_field) {
1912 arg->field.field = pevent_find_any_field(event, arg->field.name);
1913 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1914 is_symbolic_field = 0;
1917 type = read_token(&token);
1929 static char *arg_eval (struct print_arg *arg);
1931 static unsigned long long
1932 eval_type_str(unsigned long long val, const char *type, int pointer)
1942 if (type[len-1] != '*') {
1943 do_warning("pointer expected with non pointer type");
1949 do_warning("%s: not enough memory!", __func__);
1952 memcpy(ref, type, len);
1954 /* chop off the " *" */
1957 val = eval_type_str(val, ref, 0);
1962 /* check if this is a pointer */
1963 if (type[len - 1] == '*')
1966 /* Try to figure out the arg size*/
1967 if (strncmp(type, "struct", 6) == 0)
1971 if (strcmp(type, "u8") == 0)
1974 if (strcmp(type, "u16") == 0)
1975 return val & 0xffff;
1977 if (strcmp(type, "u32") == 0)
1978 return val & 0xffffffff;
1980 if (strcmp(type, "u64") == 0 ||
1981 strcmp(type, "s64"))
1984 if (strcmp(type, "s8") == 0)
1985 return (unsigned long long)(char)val & 0xff;
1987 if (strcmp(type, "s16") == 0)
1988 return (unsigned long long)(short)val & 0xffff;
1990 if (strcmp(type, "s32") == 0)
1991 return (unsigned long long)(int)val & 0xffffffff;
1993 if (strncmp(type, "unsigned ", 9) == 0) {
1998 if (strcmp(type, "char") == 0) {
2000 return (unsigned long long)(char)val & 0xff;
2005 if (strcmp(type, "short") == 0) {
2007 return (unsigned long long)(short)val & 0xffff;
2009 return val & 0xffff;
2012 if (strcmp(type, "int") == 0) {
2014 return (unsigned long long)(int)val & 0xffffffff;
2016 return val & 0xffffffff;
2023 * Try to figure out the type.
2025 static unsigned long long
2026 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2028 if (arg->type != PRINT_TYPE) {
2029 do_warning("expected type argument");
2033 return eval_type_str(val, arg->typecast.type, pointer);
2036 static int arg_num_eval(struct print_arg *arg, long long *val)
2038 long long left, right;
2041 switch (arg->type) {
2043 *val = strtoll(arg->atom.atom, NULL, 0);
2046 ret = arg_num_eval(arg->typecast.item, val);
2049 *val = eval_type(*val, arg, 0);
2052 switch (arg->op.op[0]) {
2054 ret = arg_num_eval(arg->op.left, &left);
2057 ret = arg_num_eval(arg->op.right, &right);
2061 *val = left || right;
2063 *val = left | right;
2066 ret = arg_num_eval(arg->op.left, &left);
2069 ret = arg_num_eval(arg->op.right, &right);
2073 *val = left && right;
2075 *val = left & right;
2078 ret = arg_num_eval(arg->op.left, &left);
2081 ret = arg_num_eval(arg->op.right, &right);
2084 switch (arg->op.op[1]) {
2086 *val = left < right;
2089 *val = left << right;
2092 *val = left <= right;
2095 do_warning("unknown op '%s'", arg->op.op);
2100 ret = arg_num_eval(arg->op.left, &left);
2103 ret = arg_num_eval(arg->op.right, &right);
2106 switch (arg->op.op[1]) {
2108 *val = left > right;
2111 *val = left >> right;
2114 *val = left >= right;
2117 do_warning("unknown op '%s'", arg->op.op);
2122 ret = arg_num_eval(arg->op.left, &left);
2125 ret = arg_num_eval(arg->op.right, &right);
2129 if (arg->op.op[1] != '=') {
2130 do_warning("unknown op '%s'", arg->op.op);
2133 *val = left == right;
2136 ret = arg_num_eval(arg->op.left, &left);
2139 ret = arg_num_eval(arg->op.right, &right);
2143 switch (arg->op.op[1]) {
2145 *val = left != right;
2148 do_warning("unknown op '%s'", arg->op.op);
2153 /* check for negative */
2154 if (arg->op.left->type == PRINT_NULL)
2157 ret = arg_num_eval(arg->op.left, &left);
2160 ret = arg_num_eval(arg->op.right, &right);
2163 *val = left - right;
2166 if (arg->op.left->type == PRINT_NULL)
2169 ret = arg_num_eval(arg->op.left, &left);
2172 ret = arg_num_eval(arg->op.right, &right);
2175 *val = left + right;
2178 do_warning("unknown op '%s'", arg->op.op);
2184 case PRINT_FIELD ... PRINT_SYMBOL:
2188 do_warning("invalid eval type %d", arg->type);
2195 static char *arg_eval (struct print_arg *arg)
2198 static char buf[20];
2200 switch (arg->type) {
2202 return arg->atom.atom;
2204 return arg_eval(arg->typecast.item);
2206 if (!arg_num_eval(arg, &val))
2208 sprintf(buf, "%lld", val);
2212 case PRINT_FIELD ... PRINT_SYMBOL:
2216 do_warning("invalid eval type %d", arg->type);
2223 static enum event_type
2224 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2226 enum event_type type;
2227 struct print_arg *arg = NULL;
2228 struct print_flag_sym *field;
2234 type = read_token_item(&token);
2235 if (test_type_token(type, token, EVENT_OP, "{"))
2243 type = process_arg(event, arg, &token);
2245 if (type == EVENT_OP)
2246 type = process_op(event, arg, &token);
2248 if (type == EVENT_ERROR)
2251 if (test_type_token(type, token, EVENT_DELIM, ","))
2254 field = calloc(1, sizeof(*field));
2258 value = arg_eval(arg);
2260 goto out_free_field;
2261 field->value = strdup(value);
2262 if (field->value == NULL)
2263 goto out_free_field;
2271 type = process_arg(event, arg, &token);
2272 if (test_type_token(type, token, EVENT_OP, "}"))
2273 goto out_free_field;
2275 value = arg_eval(arg);
2277 goto out_free_field;
2278 field->str = strdup(value);
2279 if (field->str == NULL)
2280 goto out_free_field;
2285 list = &field->next;
2288 type = read_token_item(&token);
2289 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2295 free_flag_sym(field);
2304 static enum event_type
2305 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2307 struct print_arg *field;
2308 enum event_type type;
2311 memset(arg, 0, sizeof(*arg));
2312 arg->type = PRINT_FLAGS;
2314 field = alloc_arg();
2316 do_warning("%s: not enough memory!", __func__);
2320 type = process_arg(event, field, &token);
2322 /* Handle operations in the first argument */
2323 while (type == EVENT_OP)
2324 type = process_op(event, field, &token);
2326 if (test_type_token(type, token, EVENT_DELIM, ","))
2327 goto out_free_field;
2330 arg->flags.field = field;
2332 type = read_token_item(&token);
2333 if (event_item_type(type)) {
2334 arg->flags.delim = token;
2335 type = read_token_item(&token);
2338 if (test_type_token(type, token, EVENT_DELIM, ","))
2341 type = process_fields(event, &arg->flags.flags, &token);
2342 if (test_type_token(type, token, EVENT_DELIM, ")"))
2346 type = read_token_item(tok);
2357 static enum event_type
2358 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2360 struct print_arg *field;
2361 enum event_type type;
2364 memset(arg, 0, sizeof(*arg));
2365 arg->type = PRINT_SYMBOL;
2367 field = alloc_arg();
2369 do_warning("%s: not enough memory!", __func__);
2373 type = process_arg(event, field, &token);
2374 if (test_type_token(type, token, EVENT_DELIM, ","))
2375 goto out_free_field;
2377 arg->symbol.field = field;
2379 type = process_fields(event, &arg->symbol.symbols, &token);
2380 if (test_type_token(type, token, EVENT_DELIM, ")"))
2384 type = read_token_item(tok);
2395 static enum event_type
2396 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2398 struct print_arg *field;
2399 enum event_type type;
2402 memset(arg, 0, sizeof(*arg));
2403 arg->type = PRINT_HEX;
2405 field = alloc_arg();
2407 do_warning("%s: not enough memory!", __func__);
2411 type = process_arg(event, field, &token);
2413 if (test_type_token(type, token, EVENT_DELIM, ","))
2416 arg->hex.field = field;
2420 field = alloc_arg();
2422 do_warning("%s: not enough memory!", __func__);
2427 type = process_arg(event, field, &token);
2429 if (test_type_token(type, token, EVENT_DELIM, ")"))
2432 arg->hex.size = field;
2435 type = read_token_item(tok);
2445 static enum event_type
2446 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2448 struct format_field *field;
2449 enum event_type type;
2452 memset(arg, 0, sizeof(*arg));
2453 arg->type = PRINT_DYNAMIC_ARRAY;
2456 * The item within the parenthesis is another field that holds
2457 * the index into where the array starts.
2459 type = read_token(&token);
2461 if (type != EVENT_ITEM)
2464 /* Find the field */
2466 field = pevent_find_field(event, token);
2470 arg->dynarray.field = field;
2471 arg->dynarray.index = 0;
2473 if (read_expected(EVENT_DELIM, ")") < 0)
2477 type = read_token_item(&token);
2479 if (type != EVENT_OP || strcmp(token, "[") != 0)
2485 do_warning("%s: not enough memory!", __func__);
2490 type = process_arg(event, arg, &token);
2491 if (type == EVENT_ERROR)
2494 if (!test_type_token(type, token, EVENT_OP, "]"))
2498 type = read_token_item(tok);
2509 static enum event_type
2510 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2512 struct print_arg *item_arg;
2513 enum event_type type;
2516 type = process_arg(event, arg, &token);
2518 if (type == EVENT_ERROR)
2521 if (type == EVENT_OP)
2522 type = process_op(event, arg, &token);
2524 if (type == EVENT_ERROR)
2527 if (test_type_token(type, token, EVENT_DELIM, ")"))
2531 type = read_token_item(&token);
2534 * If the next token is an item or another open paren, then
2535 * this was a typecast.
2537 if (event_item_type(type) ||
2538 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2540 /* make this a typecast and contine */
2542 /* prevous must be an atom */
2543 if (arg->type != PRINT_ATOM) {
2544 do_warning("previous needed to be PRINT_ATOM");
2548 item_arg = alloc_arg();
2550 do_warning("%s: not enough memory!", __func__);
2554 arg->type = PRINT_TYPE;
2555 arg->typecast.type = arg->atom.atom;
2556 arg->typecast.item = item_arg;
2557 type = process_arg_token(event, item_arg, &token, type);
2571 static enum event_type
2572 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2575 enum event_type type;
2578 if (read_expect_type(EVENT_ITEM, &token) < 0)
2581 arg->type = PRINT_STRING;
2582 arg->string.string = token;
2583 arg->string.offset = -1;
2585 if (read_expected(EVENT_DELIM, ")") < 0)
2588 type = read_token(&token);
2600 static struct pevent_function_handler *
2601 find_func_handler(struct pevent *pevent, char *func_name)
2603 struct pevent_function_handler *func;
2608 for (func = pevent->func_handlers; func; func = func->next) {
2609 if (strcmp(func->name, func_name) == 0)
2616 static void remove_func_handler(struct pevent *pevent, char *func_name)
2618 struct pevent_function_handler *func;
2619 struct pevent_function_handler **next;
2621 next = &pevent->func_handlers;
2622 while ((func = *next)) {
2623 if (strcmp(func->name, func_name) == 0) {
2625 free_func_handle(func);
2632 static enum event_type
2633 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2634 struct print_arg *arg, char **tok)
2636 struct print_arg **next_arg;
2637 struct print_arg *farg;
2638 enum event_type type;
2643 arg->type = PRINT_FUNC;
2644 arg->func.func = func;
2648 next_arg = &(arg->func.args);
2649 for (i = 0; i < func->nr_args; i++) {
2652 do_warning("%s: not enough memory!", __func__);
2656 type = process_arg(event, farg, &token);
2657 if (i < (func->nr_args - 1))
2662 if (test_type_token(type, token, EVENT_DELIM, test)) {
2669 next_arg = &(farg->next);
2673 type = read_token(&token);
2679 static enum event_type
2680 process_function(struct event_format *event, struct print_arg *arg,
2681 char *token, char **tok)
2683 struct pevent_function_handler *func;
2685 if (strcmp(token, "__print_flags") == 0) {
2688 return process_flags(event, arg, tok);
2690 if (strcmp(token, "__print_symbolic") == 0) {
2692 is_symbolic_field = 1;
2693 return process_symbols(event, arg, tok);
2695 if (strcmp(token, "__print_hex") == 0) {
2697 return process_hex(event, arg, tok);
2699 if (strcmp(token, "__get_str") == 0) {
2701 return process_str(event, arg, tok);
2703 if (strcmp(token, "__get_dynamic_array") == 0) {
2705 return process_dynamic_array(event, arg, tok);
2708 func = find_func_handler(event->pevent, token);
2711 return process_func_handler(event, func, arg, tok);
2714 do_warning("function %s not defined", token);
2719 static enum event_type
2720 process_arg_token(struct event_format *event, struct print_arg *arg,
2721 char **tok, enum event_type type)
2730 if (strcmp(token, "REC") == 0) {
2732 type = process_entry(event, arg, &token);
2736 /* test the next token */
2737 type = read_token_item(&token);
2740 * If the next token is a parenthesis, then this
2743 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2746 /* this will free atom. */
2747 type = process_function(event, arg, atom, &token);
2750 /* atoms can be more than one token long */
2751 while (type == EVENT_ITEM) {
2753 new_atom = realloc(atom,
2754 strlen(atom) + strlen(token) + 2);
2763 strcat(atom, token);
2765 type = read_token_item(&token);
2768 arg->type = PRINT_ATOM;
2769 arg->atom.atom = atom;
2774 arg->type = PRINT_ATOM;
2775 arg->atom.atom = token;
2776 type = read_token_item(&token);
2779 if (strcmp(token, "(") == 0) {
2781 type = process_paren(event, arg, &token);
2785 /* handle single ops */
2786 arg->type = PRINT_OP;
2788 arg->op.left = NULL;
2789 type = process_op(event, arg, &token);
2791 /* On error, the op is freed */
2792 if (type == EVENT_ERROR)
2795 /* return error type if errored */
2798 case EVENT_ERROR ... EVENT_NEWLINE:
2800 do_warning("unexpected type %d", type);
2808 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2810 enum event_type type = EVENT_ERROR;
2811 struct print_arg *arg;
2816 if (type == EVENT_NEWLINE) {
2817 type = read_token_item(&token);
2823 do_warning("%s: not enough memory!", __func__);
2827 type = process_arg(event, arg, &token);
2829 if (type == EVENT_ERROR) {
2838 if (type == EVENT_OP) {
2839 type = process_op(event, arg, &token);
2841 if (type == EVENT_ERROR) {
2850 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2857 } while (type != EVENT_NONE);
2859 if (type != EVENT_NONE && type != EVENT_ERROR)
2865 static int event_read_print(struct event_format *event)
2867 enum event_type type;
2871 if (read_expected_item(EVENT_ITEM, "print") < 0)
2874 if (read_expected(EVENT_ITEM, "fmt") < 0)
2877 if (read_expected(EVENT_OP, ":") < 0)
2880 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2884 event->print_fmt.format = token;
2885 event->print_fmt.args = NULL;
2887 /* ok to have no arg */
2888 type = read_token_item(&token);
2890 if (type == EVENT_NONE)
2893 /* Handle concatenation of print lines */
2894 if (type == EVENT_DQUOTE) {
2897 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2900 free_token(event->print_fmt.format);
2901 event->print_fmt.format = NULL;
2906 if (test_type_token(type, token, EVENT_DELIM, ","))
2911 ret = event_read_print_args(event, &event->print_fmt.args);
2923 * pevent_find_common_field - return a common field by event
2924 * @event: handle for the event
2925 * @name: the name of the common field to return
2927 * Returns a common field from the event by the given @name.
2928 * This only searchs the common fields and not all field.
2930 struct format_field *
2931 pevent_find_common_field(struct event_format *event, const char *name)
2933 struct format_field *format;
2935 for (format = event->format.common_fields;
2936 format; format = format->next) {
2937 if (strcmp(format->name, name) == 0)
2945 * pevent_find_field - find a non-common field
2946 * @event: handle for the event
2947 * @name: the name of the non-common field
2949 * Returns a non-common field by the given @name.
2950 * This does not search common fields.
2952 struct format_field *
2953 pevent_find_field(struct event_format *event, const char *name)
2955 struct format_field *format;
2957 for (format = event->format.fields;
2958 format; format = format->next) {
2959 if (strcmp(format->name, name) == 0)
2967 * pevent_find_any_field - find any field by name
2968 * @event: handle for the event
2969 * @name: the name of the field
2971 * Returns a field by the given @name.
2972 * This searchs the common field names first, then
2973 * the non-common ones if a common one was not found.
2975 struct format_field *
2976 pevent_find_any_field(struct event_format *event, const char *name)
2978 struct format_field *format;
2980 format = pevent_find_common_field(event, name);
2983 return pevent_find_field(event, name);
2987 * pevent_read_number - read a number from data
2988 * @pevent: handle for the pevent
2989 * @ptr: the raw data
2990 * @size: the size of the data that holds the number
2992 * Returns the number (converted to host) from the
2995 unsigned long long pevent_read_number(struct pevent *pevent,
2996 const void *ptr, int size)
3000 return *(unsigned char *)ptr;
3002 return data2host2(pevent, ptr);
3004 return data2host4(pevent, ptr);
3006 return data2host8(pevent, ptr);
3014 * pevent_read_number_field - read a number from data
3015 * @field: a handle to the field
3016 * @data: the raw data to read
3017 * @value: the value to place the number in
3019 * Reads raw data according to a field offset and size,
3020 * and translates it into @value.
3022 * Returns 0 on success, -1 otherwise.
3024 int pevent_read_number_field(struct format_field *field, const void *data,
3025 unsigned long long *value)
3029 switch (field->size) {
3034 *value = pevent_read_number(field->event->pevent,
3035 data + field->offset, field->size);
3042 static int get_common_info(struct pevent *pevent,
3043 const char *type, int *offset, int *size)
3045 struct event_format *event;
3046 struct format_field *field;
3049 * All events should have the same common elements.
3050 * Pick any event to find where the type is;
3052 if (!pevent->events) {
3053 do_warning("no event_list!");
3057 event = pevent->events[0];
3058 field = pevent_find_common_field(event, type);
3062 *offset = field->offset;
3063 *size = field->size;
3068 static int __parse_common(struct pevent *pevent, void *data,
3069 int *size, int *offset, const char *name)
3074 ret = get_common_info(pevent, name, offset, size);
3078 return pevent_read_number(pevent, data + *offset, *size);
3081 static int trace_parse_common_type(struct pevent *pevent, void *data)
3083 return __parse_common(pevent, data,
3084 &pevent->type_size, &pevent->type_offset,
3088 static int parse_common_pid(struct pevent *pevent, void *data)
3090 return __parse_common(pevent, data,
3091 &pevent->pid_size, &pevent->pid_offset,
3095 static int parse_common_pc(struct pevent *pevent, void *data)
3097 return __parse_common(pevent, data,
3098 &pevent->pc_size, &pevent->pc_offset,
3099 "common_preempt_count");
3102 static int parse_common_flags(struct pevent *pevent, void *data)
3104 return __parse_common(pevent, data,
3105 &pevent->flags_size, &pevent->flags_offset,
3109 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3111 return __parse_common(pevent, data,
3112 &pevent->ld_size, &pevent->ld_offset,
3113 "common_lock_depth");
3116 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3118 return __parse_common(pevent, data,
3119 &pevent->ld_size, &pevent->ld_offset,
3120 "common_migrate_disable");
3123 static int events_id_cmp(const void *a, const void *b);
3126 * pevent_find_event - find an event by given id
3127 * @pevent: a handle to the pevent
3128 * @id: the id of the event
3130 * Returns an event that has a given @id.
3132 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3134 struct event_format **eventptr;
3135 struct event_format key;
3136 struct event_format *pkey = &key;
3138 /* Check cache first */
3139 if (pevent->last_event && pevent->last_event->id == id)
3140 return pevent->last_event;
3144 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3145 sizeof(*pevent->events), events_id_cmp);
3148 pevent->last_event = *eventptr;
3156 * pevent_find_event_by_name - find an event by given name
3157 * @pevent: a handle to the pevent
3158 * @sys: the system name to search for
3159 * @name: the name of the event to search for
3161 * This returns an event with a given @name and under the system
3162 * @sys. If @sys is NULL the first event with @name is returned.
3164 struct event_format *
3165 pevent_find_event_by_name(struct pevent *pevent,
3166 const char *sys, const char *name)
3168 struct event_format *event;
3171 if (pevent->last_event &&
3172 strcmp(pevent->last_event->name, name) == 0 &&
3173 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3174 return pevent->last_event;
3176 for (i = 0; i < pevent->nr_events; i++) {
3177 event = pevent->events[i];
3178 if (strcmp(event->name, name) == 0) {
3181 if (strcmp(event->system, sys) == 0)
3185 if (i == pevent->nr_events)
3188 pevent->last_event = event;
3192 static unsigned long long
3193 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3195 struct pevent *pevent = event->pevent;
3196 unsigned long long val = 0;
3197 unsigned long long left, right;
3198 struct print_arg *typearg = NULL;
3199 struct print_arg *larg;
3200 unsigned long offset;
3201 unsigned int field_size;
3203 switch (arg->type) {
3208 return strtoull(arg->atom.atom, NULL, 0);
3210 if (!arg->field.field) {
3211 arg->field.field = pevent_find_any_field(event, arg->field.name);
3212 if (!arg->field.field)
3213 goto out_warning_field;
3216 /* must be a number */
3217 val = pevent_read_number(pevent, data + arg->field.field->offset,
3218 arg->field.field->size);
3225 val = eval_num_arg(data, size, event, arg->typecast.item);
3226 return eval_type(val, arg, 0);
3233 val = process_defined_func(&s, data, size, event, arg);
3234 trace_seq_destroy(&s);
3238 if (strcmp(arg->op.op, "[") == 0) {
3240 * Arrays are special, since we don't want
3241 * to read the arg as is.
3243 right = eval_num_arg(data, size, event, arg->op.right);
3245 /* handle typecasts */
3246 larg = arg->op.left;
3247 while (larg->type == PRINT_TYPE) {
3250 larg = larg->typecast.item;
3253 /* Default to long size */
3254 field_size = pevent->long_size;
3256 switch (larg->type) {
3257 case PRINT_DYNAMIC_ARRAY:
3258 offset = pevent_read_number(pevent,
3259 data + larg->dynarray.field->offset,
3260 larg->dynarray.field->size);
3261 if (larg->dynarray.field->elementsize)
3262 field_size = larg->dynarray.field->elementsize;
3264 * The actual length of the dynamic array is stored
3265 * in the top half of the field, and the offset
3266 * is in the bottom half of the 32 bit field.
3272 if (!larg->field.field) {
3274 pevent_find_any_field(event, larg->field.name);
3275 if (!larg->field.field) {
3277 goto out_warning_field;
3280 field_size = larg->field.field->elementsize;
3281 offset = larg->field.field->offset +
3282 right * larg->field.field->elementsize;
3285 goto default_op; /* oops, all bets off */
3287 val = pevent_read_number(pevent,
3288 data + offset, field_size);
3290 val = eval_type(val, typearg, 1);
3292 } else if (strcmp(arg->op.op, "?") == 0) {
3293 left = eval_num_arg(data, size, event, arg->op.left);
3294 arg = arg->op.right;
3296 val = eval_num_arg(data, size, event, arg->op.left);
3298 val = eval_num_arg(data, size, event, arg->op.right);
3302 left = eval_num_arg(data, size, event, arg->op.left);
3303 right = eval_num_arg(data, size, event, arg->op.right);
3304 switch (arg->op.op[0]) {
3306 switch (arg->op.op[1]) {
3311 val = left != right;
3314 goto out_warning_op;
3322 val = left || right;
3328 val = left && right;
3333 switch (arg->op.op[1]) {
3338 val = left << right;
3341 val = left <= right;
3344 goto out_warning_op;
3348 switch (arg->op.op[1]) {
3353 val = left >> right;
3356 val = left >= right;
3359 goto out_warning_op;
3363 if (arg->op.op[1] != '=')
3364 goto out_warning_op;
3366 val = left == right;
3381 goto out_warning_op;
3384 default: /* not sure what to do there */
3390 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3394 do_warning("%s: field %s not found", __func__, arg->field.name);
3400 unsigned long long value;
3403 static const struct flag flags[] = {
3404 { "HI_SOFTIRQ", 0 },
3405 { "TIMER_SOFTIRQ", 1 },
3406 { "NET_TX_SOFTIRQ", 2 },
3407 { "NET_RX_SOFTIRQ", 3 },
3408 { "BLOCK_SOFTIRQ", 4 },
3409 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3410 { "TASKLET_SOFTIRQ", 6 },
3411 { "SCHED_SOFTIRQ", 7 },
3412 { "HRTIMER_SOFTIRQ", 8 },
3413 { "RCU_SOFTIRQ", 9 },
3415 { "HRTIMER_NORESTART", 0 },
3416 { "HRTIMER_RESTART", 1 },
3419 static unsigned long long eval_flag(const char *flag)
3424 * Some flags in the format files do not get converted.
3425 * If the flag is not numeric, see if it is something that
3426 * we already know about.
3428 if (isdigit(flag[0]))
3429 return strtoull(flag, NULL, 0);
3431 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3432 if (strcmp(flags[i].name, flag) == 0)
3433 return flags[i].value;
3438 static void print_str_to_seq(struct trace_seq *s, const char *format,
3439 int len_arg, const char *str)
3442 trace_seq_printf(s, format, len_arg, str);
3444 trace_seq_printf(s, format, str);
3447 static void print_str_arg(struct trace_seq *s, void *data, int size,
3448 struct event_format *event, const char *format,
3449 int len_arg, struct print_arg *arg)
3451 struct pevent *pevent = event->pevent;
3452 struct print_flag_sym *flag;
3453 struct format_field *field;
3454 unsigned long long val, fval;
3461 switch (arg->type) {
3466 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3469 field = arg->field.field;
3471 field = pevent_find_any_field(event, arg->field.name);
3473 str = arg->field.name;
3474 goto out_warning_field;
3476 arg->field.field = field;
3478 /* Zero sized fields, mean the rest of the data */
3479 len = field->size ? : size - field->offset;
3482 * Some events pass in pointers. If this is not an array
3483 * and the size is the same as long_size, assume that it
3486 if (!(field->flags & FIELD_IS_ARRAY) &&
3487 field->size == pevent->long_size) {
3488 addr = *(unsigned long *)(data + field->offset);
3489 trace_seq_printf(s, "%lx", addr);
3492 str = malloc(len + 1);
3494 do_warning("%s: not enough memory!", __func__);
3497 memcpy(str, data + field->offset, len);
3499 print_str_to_seq(s, format, len_arg, str);
3503 val = eval_num_arg(data, size, event, arg->flags.field);
3505 for (flag = arg->flags.flags; flag; flag = flag->next) {
3506 fval = eval_flag(flag->value);
3507 if (!val && !fval) {
3508 print_str_to_seq(s, format, len_arg, flag->str);
3511 if (fval && (val & fval) == fval) {
3512 if (print && arg->flags.delim)
3513 trace_seq_puts(s, arg->flags.delim);
3514 print_str_to_seq(s, format, len_arg, flag->str);
3521 val = eval_num_arg(data, size, event, arg->symbol.field);
3522 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3523 fval = eval_flag(flag->value);
3525 print_str_to_seq(s, format, len_arg, flag->str);
3531 field = arg->hex.field->field.field;
3533 str = arg->hex.field->field.name;
3534 field = pevent_find_any_field(event, str);
3536 goto out_warning_field;
3537 arg->hex.field->field.field = field;
3539 hex = data + field->offset;
3540 len = eval_num_arg(data, size, event, arg->hex.size);
3541 for (i = 0; i < len; i++) {
3543 trace_seq_putc(s, ' ');
3544 trace_seq_printf(s, "%02x", hex[i]);
3550 case PRINT_STRING: {
3553 if (arg->string.offset == -1) {
3554 struct format_field *f;
3556 f = pevent_find_any_field(event, arg->string.string);
3557 arg->string.offset = f->offset;
3559 str_offset = data2host4(pevent, data + arg->string.offset);
3560 str_offset &= 0xffff;
3561 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3565 print_str_to_seq(s, format, len_arg, arg->string.string);
3569 * The only op for string should be ? :
3571 if (arg->op.op[0] != '?')
3573 val = eval_num_arg(data, size, event, arg->op.left);
3575 print_str_arg(s, data, size, event,
3576 format, len_arg, arg->op.right->op.left);
3578 print_str_arg(s, data, size, event,
3579 format, len_arg, arg->op.right->op.right);
3582 process_defined_func(s, data, size, event, arg);
3592 do_warning("%s: field %s not found", __func__, arg->field.name);
3595 static unsigned long long
3596 process_defined_func(struct trace_seq *s, void *data, int size,
3597 struct event_format *event, struct print_arg *arg)
3599 struct pevent_function_handler *func_handle = arg->func.func;
3600 struct pevent_func_params *param;
3601 unsigned long long *args;
3602 unsigned long long ret;
3603 struct print_arg *farg;
3604 struct trace_seq str;
3606 struct save_str *next;
3608 } *strings = NULL, *string;
3611 if (!func_handle->nr_args) {
3612 ret = (*func_handle->func)(s, NULL);
3616 farg = arg->func.args;
3617 param = func_handle->params;
3620 args = malloc(sizeof(*args) * func_handle->nr_args);
3624 for (i = 0; i < func_handle->nr_args; i++) {
3625 switch (param->type) {
3626 case PEVENT_FUNC_ARG_INT:
3627 case PEVENT_FUNC_ARG_LONG:
3628 case PEVENT_FUNC_ARG_PTR:
3629 args[i] = eval_num_arg(data, size, event, farg);
3631 case PEVENT_FUNC_ARG_STRING:
3632 trace_seq_init(&str);
3633 print_str_arg(&str, data, size, event, "%s", -1, farg);
3634 trace_seq_terminate(&str);
3635 string = malloc(sizeof(*string));
3637 do_warning("%s(%d): malloc str", __func__, __LINE__);
3640 string->next = strings;
3641 string->str = strdup(str.buffer);
3644 do_warning("%s(%d): malloc str", __func__, __LINE__);
3647 args[i] = (uintptr_t)string->str;
3649 trace_seq_destroy(&str);
3653 * Something went totally wrong, this is not
3654 * an input error, something in this code broke.
3656 do_warning("Unexpected end of arguments\n");
3660 param = param->next;
3663 ret = (*func_handle->func)(s, args);
3668 strings = string->next;
3674 /* TBD : handle return type here */
3678 static void free_args(struct print_arg *args)
3680 struct print_arg *next;
3690 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3692 struct pevent *pevent = event->pevent;
3693 struct format_field *field, *ip_field;
3694 struct print_arg *args, *arg, **next;
3695 unsigned long long ip, val;
3700 field = pevent->bprint_buf_field;
3701 ip_field = pevent->bprint_ip_field;
3704 field = pevent_find_field(event, "buf");
3706 do_warning("can't find buffer field for binary printk");
3709 ip_field = pevent_find_field(event, "ip");
3711 do_warning("can't find ip field for binary printk");
3714 pevent->bprint_buf_field = field;
3715 pevent->bprint_ip_field = ip_field;
3718 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3721 * The first arg is the IP pointer.
3725 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3732 arg->type = PRINT_ATOM;
3734 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3737 /* skip the first "%pf : " */
3738 for (ptr = fmt + 6, bptr = data + field->offset;
3739 bptr < data + size && *ptr; ptr++) {
3770 vsize = pevent->long_size;
3784 /* the pointers are always 4 bytes aligned */
3785 bptr = (void *)(((unsigned long)bptr + 3) &
3787 val = pevent_read_number(pevent, bptr, vsize);
3791 do_warning("%s(%d): not enough memory!",
3792 __func__, __LINE__);
3796 arg->type = PRINT_ATOM;
3797 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3804 * The '*' case means that an arg is used as the length.
3805 * We need to continue to figure out for what.
3814 do_warning("%s(%d): not enough memory!",
3815 __func__, __LINE__);
3819 arg->type = PRINT_BSTRING;
3820 arg->string.string = strdup(bptr);
3821 if (!arg->string.string)
3823 bptr += strlen(bptr) + 1;
3840 get_bprint_format(void *data, int size __maybe_unused,
3841 struct event_format *event)
3843 struct pevent *pevent = event->pevent;
3844 unsigned long long addr;
3845 struct format_field *field;
3846 struct printk_map *printk;
3850 field = pevent->bprint_fmt_field;
3853 field = pevent_find_field(event, "fmt");
3855 do_warning("can't find format field for binary printk");
3858 pevent->bprint_fmt_field = field;
3861 addr = pevent_read_number(pevent, data + field->offset, field->size);
3863 printk = find_printk(pevent, addr);
3865 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3871 /* Remove any quotes. */
3874 if (asprintf(&format, "%s : %s", "%pf", p) < 0)
3876 /* remove ending quotes and new line since we will add one too */
3877 p = format + strlen(format) - 1;
3882 if (strcmp(p, "\\n") == 0)
3888 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3889 struct event_format *event, struct print_arg *arg)
3892 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3894 if (arg->type == PRINT_FUNC) {
3895 process_defined_func(s, data, size, event, arg);
3899 if (arg->type != PRINT_FIELD) {
3900 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3906 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3907 if (!arg->field.field) {
3909 pevent_find_any_field(event, arg->field.name);
3910 if (!arg->field.field) {
3911 do_warning("%s: field %s not found",
3912 __func__, arg->field.name);
3916 if (arg->field.field->size != 6) {
3917 trace_seq_printf(s, "INVALIDMAC");
3920 buf = data + arg->field.field->offset;
3921 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3924 static int is_printable_array(char *p, unsigned int len)
3928 for (i = 0; i < len && p[i]; i++)
3934 static void print_event_fields(struct trace_seq *s, void *data,
3935 int size __maybe_unused,
3936 struct event_format *event)
3938 struct format_field *field;
3939 unsigned long long val;
3940 unsigned int offset, len, i;
3942 field = event->format.fields;
3944 trace_seq_printf(s, " %s=", field->name);
3945 if (field->flags & FIELD_IS_ARRAY) {
3946 offset = field->offset;
3948 if (field->flags & FIELD_IS_DYNAMIC) {
3949 val = pevent_read_number(event->pevent, data + offset, len);
3954 if (field->flags & FIELD_IS_STRING &&
3955 is_printable_array(data + offset, len)) {
3956 trace_seq_printf(s, "%s", (char *)data + offset);
3958 trace_seq_puts(s, "ARRAY[");
3959 for (i = 0; i < len; i++) {
3961 trace_seq_puts(s, ", ");
3962 trace_seq_printf(s, "%02x",
3963 *((unsigned char *)data + offset + i));
3965 trace_seq_putc(s, ']');
3966 field->flags &= ~FIELD_IS_STRING;
3969 val = pevent_read_number(event->pevent, data + field->offset,
3971 if (field->flags & FIELD_IS_POINTER) {
3972 trace_seq_printf(s, "0x%llx", val);
3973 } else if (field->flags & FIELD_IS_SIGNED) {
3974 switch (field->size) {
3977 * If field is long then print it in hex.
3978 * A long usually stores pointers.
3980 if (field->flags & FIELD_IS_LONG)
3981 trace_seq_printf(s, "0x%x", (int)val);
3983 trace_seq_printf(s, "%d", (int)val);
3986 trace_seq_printf(s, "%2d", (short)val);
3989 trace_seq_printf(s, "%1d", (char)val);
3992 trace_seq_printf(s, "%lld", val);
3995 if (field->flags & FIELD_IS_LONG)
3996 trace_seq_printf(s, "0x%llx", val);
3998 trace_seq_printf(s, "%llu", val);
4001 field = field->next;
4005 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4007 struct pevent *pevent = event->pevent;
4008 struct print_fmt *print_fmt = &event->print_fmt;
4009 struct print_arg *arg = print_fmt->args;
4010 struct print_arg *args = NULL;
4011 const char *ptr = print_fmt->format;
4012 unsigned long long val;
4013 struct func_map *func;
4014 const char *saveptr;
4015 char *bprint_fmt = NULL;
4023 if (event->flags & EVENT_FL_FAILED) {
4024 trace_seq_printf(s, "[FAILED TO PARSE]");
4025 print_event_fields(s, data, size, event);
4029 if (event->flags & EVENT_FL_ISBPRINT) {
4030 bprint_fmt = get_bprint_format(data, size, event);
4031 args = make_bprint_args(bprint_fmt, data, size, event);
4036 for (; *ptr; ptr++) {
4042 trace_seq_putc(s, '\n');
4045 trace_seq_putc(s, '\t');
4048 trace_seq_putc(s, '\r');
4051 trace_seq_putc(s, '\\');
4054 trace_seq_putc(s, *ptr);
4058 } else if (*ptr == '%') {
4066 trace_seq_putc(s, '%');
4069 /* FIXME: need to handle properly */
4081 /* The argument is the length. */
4083 do_warning("no argument match");
4084 event->flags |= EVENT_FL_FAILED;
4087 len_arg = eval_num_arg(data, size, event, arg);
4097 if (pevent->long_size == 4)
4102 if (*(ptr+1) == 'F' ||
4106 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4107 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4120 do_warning("no argument match");
4121 event->flags |= EVENT_FL_FAILED;
4125 len = ((unsigned long)ptr + 1) -
4126 (unsigned long)saveptr;
4128 /* should never happen */
4130 do_warning("bad format!");
4131 event->flags |= EVENT_FL_FAILED;
4135 memcpy(format, saveptr, len);
4138 val = eval_num_arg(data, size, event, arg);
4142 func = find_func(pevent, val);
4144 trace_seq_puts(s, func->func);
4145 if (show_func == 'F')
4152 if (pevent->long_size == 8 && ls &&
4153 sizeof(long) != 8) {
4157 /* make %l into %ll */
4158 p = strchr(format, 'l');
4160 memmove(p+1, p, strlen(p)+1);
4161 else if (strcmp(format, "%p") == 0)
4162 strcpy(format, "0x%llx");
4167 trace_seq_printf(s, format, len_arg, (char)val);
4169 trace_seq_printf(s, format, (char)val);
4173 trace_seq_printf(s, format, len_arg, (short)val);
4175 trace_seq_printf(s, format, (short)val);
4179 trace_seq_printf(s, format, len_arg, (int)val);
4181 trace_seq_printf(s, format, (int)val);
4185 trace_seq_printf(s, format, len_arg, (long)val);
4187 trace_seq_printf(s, format, (long)val);
4191 trace_seq_printf(s, format, len_arg,
4194 trace_seq_printf(s, format, (long long)val);
4197 do_warning("bad count (%d)", ls);
4198 event->flags |= EVENT_FL_FAILED;
4203 do_warning("no matching argument");
4204 event->flags |= EVENT_FL_FAILED;
4208 len = ((unsigned long)ptr + 1) -
4209 (unsigned long)saveptr;
4211 /* should never happen */
4213 do_warning("bad format!");
4214 event->flags |= EVENT_FL_FAILED;
4218 memcpy(format, saveptr, len);
4222 print_str_arg(s, data, size, event,
4223 format, len_arg, arg);
4227 trace_seq_printf(s, ">%c<", *ptr);
4231 trace_seq_putc(s, *ptr);
4234 if (event->flags & EVENT_FL_FAILED) {
4236 trace_seq_printf(s, "[FAILED TO PARSE]");
4246 * pevent_data_lat_fmt - parse the data for the latency format
4247 * @pevent: a handle to the pevent
4248 * @s: the trace_seq to write to
4249 * @record: the record to read from
4251 * This parses out the Latency format (interrupts disabled,
4252 * need rescheduling, in hard/soft interrupt, preempt count
4253 * and lock depth) and places it into the trace_seq.
4255 void pevent_data_lat_fmt(struct pevent *pevent,
4256 struct trace_seq *s, struct pevent_record *record)
4258 static int check_lock_depth = 1;
4259 static int check_migrate_disable = 1;
4260 static int lock_depth_exists;
4261 static int migrate_disable_exists;
4262 unsigned int lat_flags;
4265 int migrate_disable;
4268 void *data = record->data;
4270 lat_flags = parse_common_flags(pevent, data);
4271 pc = parse_common_pc(pevent, data);
4272 /* lock_depth may not always exist */
4273 if (lock_depth_exists)
4274 lock_depth = parse_common_lock_depth(pevent, data);
4275 else if (check_lock_depth) {
4276 lock_depth = parse_common_lock_depth(pevent, data);
4278 check_lock_depth = 0;
4280 lock_depth_exists = 1;
4283 /* migrate_disable may not always exist */
4284 if (migrate_disable_exists)
4285 migrate_disable = parse_common_migrate_disable(pevent, data);
4286 else if (check_migrate_disable) {
4287 migrate_disable = parse_common_migrate_disable(pevent, data);
4288 if (migrate_disable < 0)
4289 check_migrate_disable = 0;
4291 migrate_disable_exists = 1;
4294 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4295 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4297 trace_seq_printf(s, "%c%c%c",
4298 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4299 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4301 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4303 (hardirq && softirq) ? 'H' :
4304 hardirq ? 'h' : softirq ? 's' : '.');
4307 trace_seq_printf(s, "%x", pc);
4309 trace_seq_putc(s, '.');
4311 if (migrate_disable_exists) {
4312 if (migrate_disable < 0)
4313 trace_seq_putc(s, '.');
4315 trace_seq_printf(s, "%d", migrate_disable);
4318 if (lock_depth_exists) {
4320 trace_seq_putc(s, '.');
4322 trace_seq_printf(s, "%d", lock_depth);
4325 trace_seq_terminate(s);
4329 * pevent_data_type - parse out the given event type
4330 * @pevent: a handle to the pevent
4331 * @rec: the record to read from
4333 * This returns the event id from the @rec.
4335 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4337 return trace_parse_common_type(pevent, rec->data);
4341 * pevent_data_event_from_type - find the event by a given type
4342 * @pevent: a handle to the pevent
4343 * @type: the type of the event.
4345 * This returns the event form a given @type;
4347 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4349 return pevent_find_event(pevent, type);
4353 * pevent_data_pid - parse the PID from raw data
4354 * @pevent: a handle to the pevent
4355 * @rec: the record to parse
4357 * This returns the PID from a raw data.
4359 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4361 return parse_common_pid(pevent, rec->data);
4365 * pevent_data_comm_from_pid - return the command line from PID
4366 * @pevent: a handle to the pevent
4367 * @pid: the PID of the task to search for
4369 * This returns a pointer to the command line that has the given
4372 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4376 comm = find_cmdline(pevent, pid);
4381 * pevent_data_comm_from_pid - parse the data into the print format
4382 * @s: the trace_seq to write to
4383 * @event: the handle to the event
4384 * @record: the record to read from
4386 * This parses the raw @data using the given @event information and
4387 * writes the print format into the trace_seq.
4389 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4390 struct pevent_record *record)
4392 int print_pretty = 1;
4394 if (event->pevent->print_raw)
4395 print_event_fields(s, record->data, record->size, event);
4399 print_pretty = event->handler(s, record, event,
4403 pretty_print(s, record->data, record->size, event);
4406 trace_seq_terminate(s);
4409 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4410 struct pevent_record *record)
4412 static const char *spaces = " "; /* 20 spaces */
4413 struct event_format *event;
4415 unsigned long usecs;
4416 unsigned long nsecs;
4418 void *data = record->data;
4424 secs = record->ts / NSECS_PER_SEC;
4425 nsecs = record->ts - secs * NSECS_PER_SEC;
4427 if (record->size < 0) {
4428 do_warning("ug! negative record size %d", record->size);
4432 type = trace_parse_common_type(pevent, data);
4434 event = pevent_find_event(pevent, type);
4436 do_warning("ug! no event found for type %d", type);
4440 pid = parse_common_pid(pevent, data);
4441 comm = find_cmdline(pevent, pid);
4443 if (pevent->latency_format) {
4444 trace_seq_printf(s, "%8.8s-%-5d %3d",
4445 comm, pid, record->cpu);
4446 pevent_data_lat_fmt(pevent, s, record);
4448 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4450 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4454 usecs = (nsecs + 500) / NSECS_PER_USEC;
4458 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4460 /* Space out the event names evenly. */
4461 len = strlen(event->name);
4463 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4465 pevent_event_info(s, event, record);
4468 static int events_id_cmp(const void *a, const void *b)
4470 struct event_format * const * ea = a;
4471 struct event_format * const * eb = b;
4473 if ((*ea)->id < (*eb)->id)
4476 if ((*ea)->id > (*eb)->id)
4482 static int events_name_cmp(const void *a, const void *b)
4484 struct event_format * const * ea = a;
4485 struct event_format * const * eb = b;
4488 res = strcmp((*ea)->name, (*eb)->name);
4492 res = strcmp((*ea)->system, (*eb)->system);
4496 return events_id_cmp(a, b);
4499 static int events_system_cmp(const void *a, const void *b)
4501 struct event_format * const * ea = a;
4502 struct event_format * const * eb = b;
4505 res = strcmp((*ea)->system, (*eb)->system);
4509 res = strcmp((*ea)->name, (*eb)->name);
4513 return events_id_cmp(a, b);
4516 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4518 struct event_format **events;
4519 int (*sort)(const void *a, const void *b);
4521 events = pevent->sort_events;
4523 if (events && pevent->last_type == sort_type)
4527 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4531 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4532 events[pevent->nr_events] = NULL;
4534 pevent->sort_events = events;
4536 /* the internal events are sorted by id */
4537 if (sort_type == EVENT_SORT_ID) {
4538 pevent->last_type = sort_type;
4543 switch (sort_type) {
4545 sort = events_id_cmp;
4547 case EVENT_SORT_NAME:
4548 sort = events_name_cmp;
4550 case EVENT_SORT_SYSTEM:
4551 sort = events_system_cmp;
4557 qsort(events, pevent->nr_events, sizeof(*events), sort);
4558 pevent->last_type = sort_type;
4563 static struct format_field **
4564 get_event_fields(const char *type, const char *name,
4565 int count, struct format_field *list)
4567 struct format_field **fields;
4568 struct format_field *field;
4571 fields = malloc(sizeof(*fields) * (count + 1));
4575 for (field = list; field; field = field->next) {
4576 fields[i++] = field;
4577 if (i == count + 1) {
4578 do_warning("event %s has more %s fields than specified",
4586 do_warning("event %s has less %s fields than specified",
4595 * pevent_event_common_fields - return a list of common fields for an event
4596 * @event: the event to return the common fields of.
4598 * Returns an allocated array of fields. The last item in the array is NULL.
4599 * The array must be freed with free().
4601 struct format_field **pevent_event_common_fields(struct event_format *event)
4603 return get_event_fields("common", event->name,
4604 event->format.nr_common,
4605 event->format.common_fields);
4609 * pevent_event_fields - return a list of event specific fields for an event
4610 * @event: the event to return the fields of.
4612 * Returns an allocated array of fields. The last item in the array is NULL.
4613 * The array must be freed with free().
4615 struct format_field **pevent_event_fields(struct event_format *event)
4617 return get_event_fields("event", event->name,
4618 event->format.nr_fields,
4619 event->format.fields);
4622 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4624 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4626 trace_seq_puts(s, ", ");
4627 print_fields(s, field->next);
4632 static void print_args(struct print_arg *args)
4634 int print_paren = 1;
4637 switch (args->type) {
4642 printf("%s", args->atom.atom);
4645 printf("REC->%s", args->field.name);
4648 printf("__print_flags(");
4649 print_args(args->flags.field);
4650 printf(", %s, ", args->flags.delim);
4652 print_fields(&s, args->flags.flags);
4653 trace_seq_do_printf(&s);
4654 trace_seq_destroy(&s);
4658 printf("__print_symbolic(");
4659 print_args(args->symbol.field);
4662 print_fields(&s, args->symbol.symbols);
4663 trace_seq_do_printf(&s);
4664 trace_seq_destroy(&s);
4668 printf("__print_hex(");
4669 print_args(args->hex.field);
4671 print_args(args->hex.size);
4676 printf("__get_str(%s)", args->string.string);
4679 printf("(%s)", args->typecast.type);
4680 print_args(args->typecast.item);
4683 if (strcmp(args->op.op, ":") == 0)
4687 print_args(args->op.left);
4688 printf(" %s ", args->op.op);
4689 print_args(args->op.right);
4694 /* we should warn... */
4699 print_args(args->next);
4703 static void parse_header_field(const char *field,
4704 int *offset, int *size, int mandatory)
4706 unsigned long long save_input_buf_ptr;
4707 unsigned long long save_input_buf_siz;
4711 save_input_buf_ptr = input_buf_ptr;
4712 save_input_buf_siz = input_buf_siz;
4714 if (read_expected(EVENT_ITEM, "field") < 0)
4716 if (read_expected(EVENT_OP, ":") < 0)
4720 if (read_expect_type(EVENT_ITEM, &token) < 0)
4725 * If this is not a mandatory field, then test it first.
4728 if (read_expected(EVENT_ITEM, field) < 0)
4731 if (read_expect_type(EVENT_ITEM, &token) < 0)
4733 if (strcmp(token, field) != 0)
4738 if (read_expected(EVENT_OP, ";") < 0)
4740 if (read_expected(EVENT_ITEM, "offset") < 0)
4742 if (read_expected(EVENT_OP, ":") < 0)
4744 if (read_expect_type(EVENT_ITEM, &token) < 0)
4746 *offset = atoi(token);
4748 if (read_expected(EVENT_OP, ";") < 0)
4750 if (read_expected(EVENT_ITEM, "size") < 0)
4752 if (read_expected(EVENT_OP, ":") < 0)
4754 if (read_expect_type(EVENT_ITEM, &token) < 0)
4756 *size = atoi(token);
4758 if (read_expected(EVENT_OP, ";") < 0)
4760 type = read_token(&token);
4761 if (type != EVENT_NEWLINE) {
4762 /* newer versions of the kernel have a "signed" type */
4763 if (type != EVENT_ITEM)
4766 if (strcmp(token, "signed") != 0)
4771 if (read_expected(EVENT_OP, ":") < 0)
4774 if (read_expect_type(EVENT_ITEM, &token))
4778 if (read_expected(EVENT_OP, ";") < 0)
4781 if (read_expect_type(EVENT_NEWLINE, &token))
4789 input_buf_ptr = save_input_buf_ptr;
4790 input_buf_siz = save_input_buf_siz;
4797 * pevent_parse_header_page - parse the data stored in the header page
4798 * @pevent: the handle to the pevent
4799 * @buf: the buffer storing the header page format string
4800 * @size: the size of @buf
4801 * @long_size: the long size to use if there is no header
4803 * This parses the header page format for information on the
4804 * ring buffer used. The @buf should be copied from
4806 * /sys/kernel/debug/tracing/events/header_page
4808 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4815 * Old kernels did not have header page info.
4816 * Sorry but we just use what we find here in user space.
4818 pevent->header_page_ts_size = sizeof(long long);
4819 pevent->header_page_size_size = long_size;
4820 pevent->header_page_data_offset = sizeof(long long) + long_size;
4821 pevent->old_format = 1;
4824 init_input_buf(buf, size);
4826 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4827 &pevent->header_page_ts_size, 1);
4828 parse_header_field("commit", &pevent->header_page_size_offset,
4829 &pevent->header_page_size_size, 1);
4830 parse_header_field("overwrite", &pevent->header_page_overwrite,
4832 parse_header_field("data", &pevent->header_page_data_offset,
4833 &pevent->header_page_data_size, 1);
4838 static int event_matches(struct event_format *event,
4839 int id, const char *sys_name,
4840 const char *event_name)
4842 if (id >= 0 && id != event->id)
4845 if (event_name && (strcmp(event_name, event->name) != 0))
4848 if (sys_name && (strcmp(sys_name, event->system) != 0))
4854 static void free_handler(struct event_handler *handle)
4856 free((void *)handle->sys_name);
4857 free((void *)handle->event_name);
4861 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4863 struct event_handler *handle, **next;
4865 for (next = &pevent->handlers; *next;
4866 next = &(*next)->next) {
4868 if (event_matches(event, handle->id,
4870 handle->event_name))
4877 pr_stat("overriding event (%d) %s:%s with new print handler",
4878 event->id, event->system, event->name);
4880 event->handler = handle->func;
4881 event->context = handle->context;
4883 *next = handle->next;
4884 free_handler(handle);
4890 * __pevent_parse_format - parse the event format
4891 * @buf: the buffer storing the event format string
4892 * @size: the size of @buf
4893 * @sys: the system the event belongs to
4895 * This parses the event format and creates an event structure
4896 * to quickly parse raw data for a given event.
4898 * These files currently come from:
4900 * /sys/kernel/debug/tracing/events/.../.../format
4902 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4903 struct pevent *pevent, const char *buf,
4904 unsigned long size, const char *sys)
4906 struct event_format *event;
4909 init_input_buf(buf, size);
4911 *eventp = event = alloc_event();
4913 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4915 event->name = event_read_name();
4918 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4919 goto event_alloc_failed;
4922 if (strcmp(sys, "ftrace") == 0) {
4923 event->flags |= EVENT_FL_ISFTRACE;
4925 if (strcmp(event->name, "bprint") == 0)
4926 event->flags |= EVENT_FL_ISBPRINT;
4929 event->id = event_read_id();
4930 if (event->id < 0) {
4931 ret = PEVENT_ERRNO__READ_ID_FAILED;
4933 * This isn't an allocation error actually.
4934 * But as the ID is critical, just bail out.
4936 goto event_alloc_failed;
4939 event->system = strdup(sys);
4940 if (!event->system) {
4941 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4942 goto event_alloc_failed;
4945 /* Add pevent to event so that it can be referenced */
4946 event->pevent = pevent;
4948 ret = event_read_format(event);
4950 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
4951 goto event_parse_failed;
4955 * If the event has an override, don't print warnings if the event
4956 * print format fails to parse.
4958 if (pevent && find_event_handle(pevent, event))
4961 ret = event_read_print(event);
4965 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
4966 goto event_parse_failed;
4969 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4970 struct format_field *field;
4971 struct print_arg *arg, **list;
4973 /* old ftrace had no args */
4974 list = &event->print_fmt.args;
4975 for (field = event->format.fields; field; field = field->next) {
4978 event->flags |= EVENT_FL_FAILED;
4979 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4981 arg->type = PRINT_FIELD;
4982 arg->field.name = strdup(field->name);
4983 if (!arg->field.name) {
4984 event->flags |= EVENT_FL_FAILED;
4986 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
4988 arg->field.field = field;
4998 event->flags |= EVENT_FL_FAILED;
5002 free(event->system);
5010 * pevent_parse_format - parse the event format
5011 * @buf: the buffer storing the event format string
5012 * @size: the size of @buf
5013 * @sys: the system the event belongs to
5015 * This parses the event format and creates an event structure
5016 * to quickly parse raw data for a given event.
5018 * These files currently come from:
5020 * /sys/kernel/debug/tracing/events/.../.../format
5022 enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5023 unsigned long size, const char *sys)
5025 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5029 * pevent_parse_event - parse the event format
5030 * @pevent: the handle to the pevent
5031 * @buf: the buffer storing the event format string
5032 * @size: the size of @buf
5033 * @sys: the system the event belongs to
5035 * This parses the event format and creates an event structure
5036 * to quickly parse raw data for a given event.
5038 * These files currently come from:
5040 * /sys/kernel/debug/tracing/events/.../.../format
5042 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5043 unsigned long size, const char *sys)
5045 struct event_format *event = NULL;
5046 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5051 if (add_event(pevent, event)) {
5052 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5053 goto event_add_failed;
5056 #define PRINT_ARGS 0
5057 if (PRINT_ARGS && event->print_fmt.args)
5058 print_args(event->print_fmt.args);
5063 pevent_free_format(event);
5068 #define _PE(code, str) str
5069 static const char * const pevent_error_str[] = {
5074 int pevent_strerror(struct pevent *pevent __maybe_unused,
5075 enum pevent_errno errnum, char *buf, size_t buflen)
5081 msg = strerror_r(errnum, buf, buflen);
5083 size_t len = strlen(msg);
5084 memcpy(buf, msg, min(buflen - 1, len));
5085 *(buf + min(buflen - 1, len)) = '\0';
5090 if (errnum <= __PEVENT_ERRNO__START ||
5091 errnum >= __PEVENT_ERRNO__END)
5094 idx = errnum - __PEVENT_ERRNO__START - 1;
5095 msg = pevent_error_str[idx];
5098 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5099 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5100 case PEVENT_ERRNO__READ_ID_FAILED:
5101 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5102 case PEVENT_ERRNO__READ_PRINT_FAILED:
5103 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
5104 case PEVENT_ERRNO__INVALID_ARG_TYPE:
5105 snprintf(buf, buflen, "%s", msg);
5109 /* cannot reach here */
5116 int get_field_val(struct trace_seq *s, struct format_field *field,
5117 const char *name, struct pevent_record *record,
5118 unsigned long long *val, int err)
5122 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5126 if (pevent_read_number_field(field, record->data, val)) {
5128 trace_seq_printf(s, " %s=INVALID", name);
5136 * pevent_get_field_raw - return the raw pointer into the data field
5137 * @s: The seq to print to on error
5138 * @event: the event that the field is for
5139 * @name: The name of the field
5140 * @record: The record with the field name.
5141 * @len: place to store the field length.
5142 * @err: print default error if failed.
5144 * Returns a pointer into record->data of the field and places
5145 * the length of the field in @len.
5147 * On failure, it returns NULL.
5149 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5150 const char *name, struct pevent_record *record,
5153 struct format_field *field;
5154 void *data = record->data;
5161 field = pevent_find_field(event, name);
5165 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5169 /* Allow @len to be NULL */
5173 offset = field->offset;
5174 if (field->flags & FIELD_IS_DYNAMIC) {
5175 offset = pevent_read_number(event->pevent,
5176 data + offset, field->size);
5177 *len = offset >> 16;
5182 return data + offset;
5186 * pevent_get_field_val - find a field and return its value
5187 * @s: The seq to print to on error
5188 * @event: the event that the field is for
5189 * @name: The name of the field
5190 * @record: The record with the field name.
5191 * @val: place to store the value of the field.
5192 * @err: print default error if failed.
5194 * Returns 0 on success -1 on field not found.
5196 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5197 const char *name, struct pevent_record *record,
5198 unsigned long long *val, int err)
5200 struct format_field *field;
5205 field = pevent_find_field(event, name);
5207 return get_field_val(s, field, name, record, val, err);
5211 * pevent_get_common_field_val - find a common field and return its value
5212 * @s: The seq to print to on error
5213 * @event: the event that the field is for
5214 * @name: The name of the field
5215 * @record: The record with the field name.
5216 * @val: place to store the value of the field.
5217 * @err: print default error if failed.
5219 * Returns 0 on success -1 on field not found.
5221 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5222 const char *name, struct pevent_record *record,
5223 unsigned long long *val, int err)
5225 struct format_field *field;
5230 field = pevent_find_common_field(event, name);
5232 return get_field_val(s, field, name, record, val, err);
5236 * pevent_get_any_field_val - find a any field and return its value
5237 * @s: The seq to print to on error
5238 * @event: the event that the field is for
5239 * @name: The name of the field
5240 * @record: The record with the field name.
5241 * @val: place to store the value of the field.
5242 * @err: print default error if failed.
5244 * Returns 0 on success -1 on field not found.
5246 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5247 const char *name, struct pevent_record *record,
5248 unsigned long long *val, int err)
5250 struct format_field *field;
5255 field = pevent_find_any_field(event, name);
5257 return get_field_val(s, field, name, record, val, err);
5261 * pevent_print_num_field - print a field and a format
5262 * @s: The seq to print to
5263 * @fmt: The printf format to print the field with.
5264 * @event: the event that the field is for
5265 * @name: The name of the field
5266 * @record: The record with the field name.
5267 * @err: print default error if failed.
5269 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5271 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5272 struct event_format *event, const char *name,
5273 struct pevent_record *record, int err)
5275 struct format_field *field = pevent_find_field(event, name);
5276 unsigned long long val;
5281 if (pevent_read_number_field(field, record->data, &val))
5284 return trace_seq_printf(s, fmt, val);
5288 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5292 static void free_func_handle(struct pevent_function_handler *func)
5294 struct pevent_func_params *params;
5298 while (func->params) {
5299 params = func->params;
5300 func->params = params->next;
5308 * pevent_register_print_function - register a helper function
5309 * @pevent: the handle to the pevent
5310 * @func: the function to process the helper function
5311 * @ret_type: the return type of the helper function
5312 * @name: the name of the helper function
5313 * @parameters: A list of enum pevent_func_arg_type
5315 * Some events may have helper functions in the print format arguments.
5316 * This allows a plugin to dynamically create a way to process one
5317 * of these functions.
5319 * The @parameters is a variable list of pevent_func_arg_type enums that
5320 * must end with PEVENT_FUNC_ARG_VOID.
5322 int pevent_register_print_function(struct pevent *pevent,
5323 pevent_func_handler func,
5324 enum pevent_func_arg_type ret_type,
5327 struct pevent_function_handler *func_handle;
5328 struct pevent_func_params **next_param;
5329 struct pevent_func_params *param;
5330 enum pevent_func_arg_type type;
5334 func_handle = find_func_handler(pevent, name);
5337 * This is most like caused by the users own
5338 * plugins updating the function. This overrides the
5341 pr_stat("override of function helper '%s'", name);
5342 remove_func_handler(pevent, name);
5345 func_handle = calloc(1, sizeof(*func_handle));
5347 do_warning("Failed to allocate function handler");
5348 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5351 func_handle->ret_type = ret_type;
5352 func_handle->name = strdup(name);
5353 func_handle->func = func;
5354 if (!func_handle->name) {
5355 do_warning("Failed to allocate function name");
5357 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5360 next_param = &(func_handle->params);
5363 type = va_arg(ap, enum pevent_func_arg_type);
5364 if (type == PEVENT_FUNC_ARG_VOID)
5367 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5368 do_warning("Invalid argument type %d", type);
5369 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5373 param = malloc(sizeof(*param));
5375 do_warning("Failed to allocate function param");
5376 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5382 *next_param = param;
5383 next_param = &(param->next);
5385 func_handle->nr_args++;
5389 func_handle->next = pevent->func_handlers;
5390 pevent->func_handlers = func_handle;
5395 free_func_handle(func_handle);
5400 * pevent_register_event_handler - register a way to parse an event
5401 * @pevent: the handle to the pevent
5402 * @id: the id of the event to register
5403 * @sys_name: the system name the event belongs to
5404 * @event_name: the name of the event
5405 * @func: the function to call to parse the event information
5406 * @context: the data to be passed to @func
5408 * This function allows a developer to override the parsing of
5409 * a given event. If for some reason the default print format
5410 * is not sufficient, this function will register a function
5411 * for an event to be used to parse the data instead.
5413 * If @id is >= 0, then it is used to find the event.
5414 * else @sys_name and @event_name are used.
5416 int pevent_register_event_handler(struct pevent *pevent,
5417 int id, char *sys_name, char *event_name,
5418 pevent_event_handler_func func,
5421 struct event_format *event;
5422 struct event_handler *handle;
5426 event = pevent_find_event(pevent, id);
5429 if (event_name && (strcmp(event_name, event->name) != 0))
5431 if (sys_name && (strcmp(sys_name, event->system) != 0))
5434 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5439 pr_stat("overriding event (%d) %s:%s with new print handler",
5440 event->id, event->system, event->name);
5442 event->handler = func;
5443 event->context = context;
5447 /* Save for later use. */
5448 handle = calloc(1, sizeof(*handle));
5450 do_warning("Failed to allocate event handler");
5451 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5456 handle->event_name = strdup(event_name);
5458 handle->sys_name = strdup(sys_name);
5460 if ((event_name && !handle->event_name) ||
5461 (sys_name && !handle->sys_name)) {
5462 do_warning("Failed to allocate event/sys name");
5463 free((void *)handle->event_name);
5464 free((void *)handle->sys_name);
5466 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5469 handle->func = func;
5470 handle->next = pevent->handlers;
5471 pevent->handlers = handle;
5472 handle->context = context;
5478 * pevent_alloc - create a pevent handle
5480 struct pevent *pevent_alloc(void)
5482 struct pevent *pevent = calloc(1, sizeof(*pevent));
5485 pevent->ref_count = 1;
5490 void pevent_ref(struct pevent *pevent)
5492 pevent->ref_count++;
5495 static void free_format_fields(struct format_field *field)
5497 struct format_field *next;
5508 static void free_formats(struct format *format)
5510 free_format_fields(format->common_fields);
5511 free_format_fields(format->fields);
5514 void pevent_free_format(struct event_format *event)
5517 free(event->system);
5519 free_formats(&event->format);
5521 free(event->print_fmt.format);
5522 free_args(event->print_fmt.args);
5528 * pevent_free - free a pevent handle
5529 * @pevent: the pevent handle to free
5531 void pevent_free(struct pevent *pevent)
5533 struct cmdline_list *cmdlist, *cmdnext;
5534 struct func_list *funclist, *funcnext;
5535 struct printk_list *printklist, *printknext;
5536 struct pevent_function_handler *func_handler;
5537 struct event_handler *handle;
5543 cmdlist = pevent->cmdlist;
5544 funclist = pevent->funclist;
5545 printklist = pevent->printklist;
5547 pevent->ref_count--;
5548 if (pevent->ref_count)
5551 if (pevent->cmdlines) {
5552 for (i = 0; i < pevent->cmdline_count; i++)
5553 free(pevent->cmdlines[i].comm);
5554 free(pevent->cmdlines);
5558 cmdnext = cmdlist->next;
5559 free(cmdlist->comm);
5564 if (pevent->func_map) {
5565 for (i = 0; i < (int)pevent->func_count; i++) {
5566 free(pevent->func_map[i].func);
5567 free(pevent->func_map[i].mod);
5569 free(pevent->func_map);
5573 funcnext = funclist->next;
5574 free(funclist->func);
5575 free(funclist->mod);
5577 funclist = funcnext;
5580 while (pevent->func_handlers) {
5581 func_handler = pevent->func_handlers;
5582 pevent->func_handlers = func_handler->next;
5583 free_func_handle(func_handler);
5586 if (pevent->printk_map) {
5587 for (i = 0; i < (int)pevent->printk_count; i++)
5588 free(pevent->printk_map[i].printk);
5589 free(pevent->printk_map);
5592 while (printklist) {
5593 printknext = printklist->next;
5594 free(printklist->printk);
5596 printklist = printknext;
5599 for (i = 0; i < pevent->nr_events; i++)
5600 pevent_free_format(pevent->events[i]);
5602 while (pevent->handlers) {
5603 handle = pevent->handlers;
5604 pevent->handlers = handle->next;
5605 free_handler(handle);
5608 free(pevent->events);
5609 free(pevent->sort_events);
5614 void pevent_unref(struct pevent *pevent)
5616 pevent_free(pevent);