2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include "event-parse.h"
36 #include "event-utils.h"
38 static const char *input_buf;
39 static unsigned long long input_buf_ptr;
40 static unsigned long long input_buf_siz;
42 static int is_flag_field;
43 static int is_symbolic_field;
45 static int show_warning = 1;
47 #define do_warning(fmt, ...) \
50 warning(fmt, ##__VA_ARGS__); \
53 static void init_input_buf(const char *buf, unsigned long long size)
60 const char *pevent_get_input_buf(void)
65 unsigned long long pevent_get_input_buf_ptr(void)
70 struct event_handler {
71 struct event_handler *next;
74 const char *event_name;
75 pevent_event_handler_func func;
79 struct pevent_func_params {
80 struct pevent_func_params *next;
81 enum pevent_func_arg_type type;
84 struct pevent_function_handler {
85 struct pevent_function_handler *next;
86 enum pevent_func_arg_type ret_type;
88 pevent_func_handler func;
89 struct pevent_func_params *params;
93 static unsigned long long
94 process_defined_func(struct trace_seq *s, void *data, int size,
95 struct event_format *event, struct print_arg *arg);
97 static void free_func_handle(struct pevent_function_handler *func);
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
107 void pevent_buffer_init(const char *buf, unsigned long long size)
109 init_input_buf(buf, size);
112 void breakpoint(void)
118 struct print_arg *alloc_arg(void)
120 return calloc(1, sizeof(struct print_arg));
128 static int cmdline_cmp(const void *a, const void *b)
130 const struct cmdline *ca = a;
131 const struct cmdline *cb = b;
133 if (ca->pid < cb->pid)
135 if (ca->pid > cb->pid)
141 struct cmdline_list {
142 struct cmdline_list *next;
147 static int cmdline_init(struct pevent *pevent)
149 struct cmdline_list *cmdlist = pevent->cmdlist;
150 struct cmdline_list *item;
151 struct cmdline *cmdlines;
154 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
160 cmdlines[i].pid = cmdlist->pid;
161 cmdlines[i].comm = cmdlist->comm;
164 cmdlist = cmdlist->next;
168 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
170 pevent->cmdlines = cmdlines;
171 pevent->cmdlist = NULL;
176 static const char *find_cmdline(struct pevent *pevent, int pid)
178 const struct cmdline *comm;
184 if (!pevent->cmdlines && cmdline_init(pevent))
185 return "<not enough memory for cmdlines!>";
189 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
190 sizeof(*pevent->cmdlines), cmdline_cmp);
198 * pevent_pid_is_registered - return if a pid has a cmdline registered
199 * @pevent: handle for the pevent
200 * @pid: The pid to check if it has a cmdline registered with.
202 * Returns 1 if the pid has a cmdline mapped to it
205 int pevent_pid_is_registered(struct pevent *pevent, int pid)
207 const struct cmdline *comm;
213 if (!pevent->cmdlines && cmdline_init(pevent))
218 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
219 sizeof(*pevent->cmdlines), cmdline_cmp);
227 * If the command lines have been converted to an array, then
228 * we must add this pid. This is much slower than when cmdlines
229 * are added before the array is initialized.
231 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
233 struct cmdline *cmdlines = pevent->cmdlines;
234 const struct cmdline *cmdline;
240 /* avoid duplicates */
243 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
244 sizeof(*pevent->cmdlines), cmdline_cmp);
250 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
256 cmdlines[pevent->cmdline_count].comm = strdup(comm);
257 if (!cmdlines[pevent->cmdline_count].comm) {
263 cmdlines[pevent->cmdline_count].pid = pid;
265 if (cmdlines[pevent->cmdline_count].comm)
266 pevent->cmdline_count++;
268 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
269 pevent->cmdlines = cmdlines;
275 * pevent_register_comm - register a pid / comm mapping
276 * @pevent: handle for the pevent
277 * @comm: the command line to register
278 * @pid: the pid to map the command line to
280 * This adds a mapping to search for command line names with
281 * a given pid. The comm is duplicated.
283 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
285 struct cmdline_list *item;
287 if (pevent->cmdlines)
288 return add_new_comm(pevent, comm, pid);
290 item = malloc(sizeof(*item));
294 item->comm = strdup(comm);
300 item->next = pevent->cmdlist;
302 pevent->cmdlist = item;
303 pevent->cmdline_count++;
308 void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
310 pevent->trace_clock = trace_clock;
314 unsigned long long addr;
320 struct func_list *next;
321 unsigned long long addr;
326 static int func_cmp(const void *a, const void *b)
328 const struct func_map *fa = a;
329 const struct func_map *fb = b;
331 if (fa->addr < fb->addr)
333 if (fa->addr > fb->addr)
340 * We are searching for a record in between, not an exact
343 static int func_bcmp(const void *a, const void *b)
345 const struct func_map *fa = a;
346 const struct func_map *fb = b;
348 if ((fa->addr == fb->addr) ||
350 (fa->addr > fb->addr &&
351 fa->addr < (fb+1)->addr))
354 if (fa->addr < fb->addr)
360 static int func_map_init(struct pevent *pevent)
362 struct func_list *funclist;
363 struct func_list *item;
364 struct func_map *func_map;
367 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
371 funclist = pevent->funclist;
375 func_map[i].func = funclist->func;
376 func_map[i].addr = funclist->addr;
377 func_map[i].mod = funclist->mod;
380 funclist = funclist->next;
384 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
387 * Add a special record at the end.
389 func_map[pevent->func_count].func = NULL;
390 func_map[pevent->func_count].addr = 0;
391 func_map[pevent->func_count].mod = NULL;
393 pevent->func_map = func_map;
394 pevent->funclist = NULL;
399 static struct func_map *
400 find_func(struct pevent *pevent, unsigned long long addr)
402 struct func_map *func;
405 if (!pevent->func_map)
406 func_map_init(pevent);
410 func = bsearch(&key, pevent->func_map, pevent->func_count,
411 sizeof(*pevent->func_map), func_bcmp);
417 * pevent_find_function - find a function by a given address
418 * @pevent: handle for the pevent
419 * @addr: the address to find the function with
421 * Returns a pointer to the function stored that has the given
422 * address. Note, the address does not have to be exact, it
423 * will select the function that would contain the address.
425 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
427 struct func_map *map;
429 map = find_func(pevent, addr);
437 * pevent_find_function_address - find a function address by a given address
438 * @pevent: handle for the pevent
439 * @addr: the address to find the function with
441 * Returns the address the function starts at. This can be used in
442 * conjunction with pevent_find_function to print both the function
443 * name and the function offset.
446 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
448 struct func_map *map;
450 map = find_func(pevent, addr);
458 * pevent_register_function - register a function with a given address
459 * @pevent: handle for the pevent
460 * @function: the function name to register
461 * @addr: the address the function starts at
462 * @mod: the kernel module the function may be in (NULL for none)
464 * This registers a function name with an address and module.
465 * The @func passed in is duplicated.
467 int pevent_register_function(struct pevent *pevent, char *func,
468 unsigned long long addr, char *mod)
470 struct func_list *item = malloc(sizeof(*item));
475 item->next = pevent->funclist;
476 item->func = strdup(func);
481 item->mod = strdup(mod);
488 pevent->funclist = item;
489 pevent->func_count++;
503 * pevent_print_funcs - print out the stored functions
504 * @pevent: handle for the pevent
506 * This prints out the stored functions.
508 void pevent_print_funcs(struct pevent *pevent)
512 if (!pevent->func_map)
513 func_map_init(pevent);
515 for (i = 0; i < (int)pevent->func_count; i++) {
517 pevent->func_map[i].addr,
518 pevent->func_map[i].func);
519 if (pevent->func_map[i].mod)
520 printf(" [%s]\n", pevent->func_map[i].mod);
527 unsigned long long addr;
532 struct printk_list *next;
533 unsigned long long addr;
537 static int printk_cmp(const void *a, const void *b)
539 const struct printk_map *pa = a;
540 const struct printk_map *pb = b;
542 if (pa->addr < pb->addr)
544 if (pa->addr > pb->addr)
550 static int printk_map_init(struct pevent *pevent)
552 struct printk_list *printklist;
553 struct printk_list *item;
554 struct printk_map *printk_map;
557 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
561 printklist = pevent->printklist;
565 printk_map[i].printk = printklist->printk;
566 printk_map[i].addr = printklist->addr;
569 printklist = printklist->next;
573 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
575 pevent->printk_map = printk_map;
576 pevent->printklist = NULL;
581 static struct printk_map *
582 find_printk(struct pevent *pevent, unsigned long long addr)
584 struct printk_map *printk;
585 struct printk_map key;
587 if (!pevent->printk_map && printk_map_init(pevent))
592 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
593 sizeof(*pevent->printk_map), printk_cmp);
599 * pevent_register_print_string - register a string by its address
600 * @pevent: handle for the pevent
601 * @fmt: the string format to register
602 * @addr: the address the string was located at
604 * This registers a string by the address it was stored in the kernel.
605 * The @fmt passed in is duplicated.
607 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
608 unsigned long long addr)
610 struct printk_list *item = malloc(sizeof(*item));
616 item->next = pevent->printklist;
619 /* Strip off quotes and '\n' from the end */
622 item->printk = strdup(fmt);
626 p = item->printk + strlen(item->printk) - 1;
631 if (strcmp(p, "\\n") == 0)
634 pevent->printklist = item;
635 pevent->printk_count++;
646 * pevent_print_printk - print out the stored strings
647 * @pevent: handle for the pevent
649 * This prints the string formats that were stored.
651 void pevent_print_printk(struct pevent *pevent)
655 if (!pevent->printk_map)
656 printk_map_init(pevent);
658 for (i = 0; i < (int)pevent->printk_count; i++) {
659 printf("%016llx %s\n",
660 pevent->printk_map[i].addr,
661 pevent->printk_map[i].printk);
665 static struct event_format *alloc_event(void)
667 return calloc(1, sizeof(struct event_format));
670 static int add_event(struct pevent *pevent, struct event_format *event)
673 struct event_format **events = realloc(pevent->events, sizeof(event) *
674 (pevent->nr_events + 1));
678 pevent->events = events;
680 for (i = 0; i < pevent->nr_events; i++) {
681 if (pevent->events[i]->id > event->id)
684 if (i < pevent->nr_events)
685 memmove(&pevent->events[i + 1],
687 sizeof(event) * (pevent->nr_events - i));
689 pevent->events[i] = event;
692 event->pevent = pevent;
697 static int event_item_type(enum event_type type)
700 case EVENT_ITEM ... EVENT_SQUOTE:
702 case EVENT_ERROR ... EVENT_DELIM:
708 static void free_flag_sym(struct print_flag_sym *fsym)
710 struct print_flag_sym *next;
721 static void free_arg(struct print_arg *arg)
723 struct print_arg *farg;
730 free(arg->atom.atom);
733 free(arg->field.name);
736 free_arg(arg->flags.field);
737 free(arg->flags.delim);
738 free_flag_sym(arg->flags.flags);
741 free_arg(arg->symbol.field);
742 free_flag_sym(arg->symbol.symbols);
745 free_arg(arg->hex.field);
746 free_arg(arg->hex.size);
749 free(arg->typecast.type);
750 free_arg(arg->typecast.item);
754 free(arg->string.string);
756 case PRINT_DYNAMIC_ARRAY:
757 free(arg->dynarray.index);
761 free_arg(arg->op.left);
762 free_arg(arg->op.right);
765 while (arg->func.args) {
766 farg = arg->func.args;
767 arg->func.args = farg->next;
780 static enum event_type get_type(int ch)
783 return EVENT_NEWLINE;
786 if (isalnum(ch) || ch == '_')
794 if (ch == '(' || ch == ')' || ch == ',')
800 static int __read_char(void)
802 if (input_buf_ptr >= input_buf_siz)
805 return input_buf[input_buf_ptr++];
808 static int __peek_char(void)
810 if (input_buf_ptr >= input_buf_siz)
813 return input_buf[input_buf_ptr];
817 * pevent_peek_char - peek at the next character that will be read
819 * Returns the next character read, or -1 if end of buffer.
821 int pevent_peek_char(void)
823 return __peek_char();
826 static int extend_token(char **tok, char *buf, int size)
828 char *newtok = realloc(*tok, size);
845 static enum event_type force_token(const char *str, char **tok);
847 static enum event_type __read_token(char **tok)
850 int ch, last_ch, quote_ch, next_ch;
853 enum event_type type;
863 if (type == EVENT_NONE)
871 if (asprintf(tok, "%c", ch) < 0)
879 next_ch = __peek_char();
880 if (next_ch == '>') {
881 buf[i++] = __read_char();
894 buf[i++] = __read_char();
906 default: /* what should we do instead? */
916 buf[i++] = __read_char();
921 /* don't keep quotes */
927 if (i == (BUFSIZ - 1)) {
931 if (extend_token(tok, buf, tok_size) < 0)
938 /* the '\' '\' will cancel itself */
939 if (ch == '\\' && last_ch == '\\')
941 } while (ch != quote_ch || last_ch == '\\');
942 /* remove the last quote */
946 * For strings (double quotes) check the next token.
947 * If it is another string, concatinate the two.
949 if (type == EVENT_DQUOTE) {
950 unsigned long long save_input_buf_ptr = input_buf_ptr;
954 } while (isspace(ch));
957 input_buf_ptr = save_input_buf_ptr;
962 case EVENT_ERROR ... EVENT_SPACE:
968 while (get_type(__peek_char()) == type) {
969 if (i == (BUFSIZ - 1)) {
973 if (extend_token(tok, buf, tok_size) < 0)
983 if (extend_token(tok, buf, tok_size + i + 1) < 0)
986 if (type == EVENT_ITEM) {
988 * Older versions of the kernel has a bug that
989 * creates invalid symbols and will break the mac80211
990 * parsing. This is a work around to that bug.
992 * See Linux kernel commit:
993 * 811cb50baf63461ce0bdb234927046131fc7fa8b
995 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
998 return force_token("\"\%s\" ", tok);
999 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1002 return force_token("\" sta:%pM\" ", tok);
1003 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1006 return force_token("\" vif:%p(%d)\" ", tok);
1013 static enum event_type force_token(const char *str, char **tok)
1015 const char *save_input_buf;
1016 unsigned long long save_input_buf_ptr;
1017 unsigned long long save_input_buf_siz;
1018 enum event_type type;
1020 /* save off the current input pointers */
1021 save_input_buf = input_buf;
1022 save_input_buf_ptr = input_buf_ptr;
1023 save_input_buf_siz = input_buf_siz;
1025 init_input_buf(str, strlen(str));
1027 type = __read_token(tok);
1029 /* reset back to original token */
1030 input_buf = save_input_buf;
1031 input_buf_ptr = save_input_buf_ptr;
1032 input_buf_siz = save_input_buf_siz;
1037 static void free_token(char *tok)
1043 static enum event_type read_token(char **tok)
1045 enum event_type type;
1048 type = __read_token(tok);
1049 if (type != EVENT_SPACE)
1061 * pevent_read_token - access to utilites to use the pevent parser
1062 * @tok: The token to return
1064 * This will parse tokens from the string given by
1065 * pevent_init_data().
1067 * Returns the token type.
1069 enum event_type pevent_read_token(char **tok)
1071 return read_token(tok);
1075 * pevent_free_token - free a token returned by pevent_read_token
1076 * @token: the token to free
1078 void pevent_free_token(char *token)
1084 static enum event_type read_token_item(char **tok)
1086 enum event_type type;
1089 type = __read_token(tok);
1090 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1101 static int test_type(enum event_type type, enum event_type expect)
1103 if (type != expect) {
1104 do_warning("Error: expected type %d but read %d",
1111 static int test_type_token(enum event_type type, const char *token,
1112 enum event_type expect, const char *expect_tok)
1114 if (type != expect) {
1115 do_warning("Error: expected type %d but read %d",
1120 if (strcmp(token, expect_tok) != 0) {
1121 do_warning("Error: expected '%s' but read '%s'",
1128 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1130 enum event_type type;
1133 type = read_token(tok);
1135 type = read_token_item(tok);
1136 return test_type(type, expect);
1139 static int read_expect_type(enum event_type expect, char **tok)
1141 return __read_expect_type(expect, tok, 1);
1144 static int __read_expected(enum event_type expect, const char *str,
1147 enum event_type type;
1152 type = read_token(&token);
1154 type = read_token_item(&token);
1156 ret = test_type_token(type, token, expect, str);
1163 static int read_expected(enum event_type expect, const char *str)
1165 return __read_expected(expect, str, 1);
1168 static int read_expected_item(enum event_type expect, const char *str)
1170 return __read_expected(expect, str, 0);
1173 static char *event_read_name(void)
1177 if (read_expected(EVENT_ITEM, "name") < 0)
1180 if (read_expected(EVENT_OP, ":") < 0)
1183 if (read_expect_type(EVENT_ITEM, &token) < 0)
1193 static int event_read_id(void)
1198 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1201 if (read_expected(EVENT_OP, ":") < 0)
1204 if (read_expect_type(EVENT_ITEM, &token) < 0)
1207 id = strtoul(token, NULL, 0);
1216 static int field_is_string(struct format_field *field)
1218 if ((field->flags & FIELD_IS_ARRAY) &&
1219 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1220 strstr(field->type, "s8")))
1226 static int field_is_dynamic(struct format_field *field)
1228 if (strncmp(field->type, "__data_loc", 10) == 0)
1234 static int field_is_long(struct format_field *field)
1236 /* includes long long */
1237 if (strstr(field->type, "long"))
1243 static unsigned int type_size(const char *name)
1245 /* This covers all FIELD_IS_STRING types. */
1263 for (i = 0; table[i].type; i++) {
1264 if (!strcmp(table[i].type, name))
1265 return table[i].size;
1271 static int event_read_fields(struct event_format *event, struct format_field **fields)
1273 struct format_field *field = NULL;
1274 enum event_type type;
1280 unsigned int size_dynamic = 0;
1282 type = read_token(&token);
1283 if (type == EVENT_NEWLINE) {
1290 if (test_type_token(type, token, EVENT_ITEM, "field"))
1294 type = read_token(&token);
1296 * The ftrace fields may still use the "special" name.
1299 if (event->flags & EVENT_FL_ISFTRACE &&
1300 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1302 type = read_token(&token);
1305 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1309 if (read_expect_type(EVENT_ITEM, &token) < 0)
1314 field = calloc(1, sizeof(*field));
1318 field->event = event;
1320 /* read the rest of the type */
1322 type = read_token(&token);
1323 if (type == EVENT_ITEM ||
1324 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1326 * Some of the ftrace fields are broken and have
1327 * an illegal "." in them.
1329 (event->flags & EVENT_FL_ISFTRACE &&
1330 type == EVENT_OP && strcmp(token, ".") == 0)) {
1332 if (strcmp(token, "*") == 0)
1333 field->flags |= FIELD_IS_POINTER;
1337 new_type = realloc(field->type,
1338 strlen(field->type) +
1339 strlen(last_token) + 2);
1344 field->type = new_type;
1345 strcat(field->type, " ");
1346 strcat(field->type, last_token);
1349 field->type = last_token;
1358 do_warning("%s: no type found", __func__);
1361 field->name = last_token;
1363 if (test_type(type, EVENT_OP))
1366 if (strcmp(token, "[") == 0) {
1367 enum event_type last_type = type;
1368 char *brackets = token;
1372 field->flags |= FIELD_IS_ARRAY;
1374 type = read_token(&token);
1376 if (type == EVENT_ITEM)
1377 field->arraylen = strtoul(token, NULL, 0);
1379 field->arraylen = 0;
1381 while (strcmp(token, "]") != 0) {
1382 if (last_type == EVENT_ITEM &&
1389 new_brackets = realloc(brackets,
1391 strlen(token) + len);
1392 if (!new_brackets) {
1396 brackets = new_brackets;
1398 strcat(brackets, " ");
1399 strcat(brackets, token);
1400 /* We only care about the last token */
1401 field->arraylen = strtoul(token, NULL, 0);
1403 type = read_token(&token);
1404 if (type == EVENT_NONE) {
1405 do_warning("failed to find token");
1412 new_brackets = realloc(brackets, strlen(brackets) + 2);
1413 if (!new_brackets) {
1417 brackets = new_brackets;
1418 strcat(brackets, "]");
1420 /* add brackets to type */
1422 type = read_token(&token);
1424 * If the next token is not an OP, then it is of
1425 * the format: type [] item;
1427 if (type == EVENT_ITEM) {
1429 new_type = realloc(field->type,
1430 strlen(field->type) +
1431 strlen(field->name) +
1432 strlen(brackets) + 2);
1437 field->type = new_type;
1438 strcat(field->type, " ");
1439 strcat(field->type, field->name);
1440 size_dynamic = type_size(field->name);
1441 free_token(field->name);
1442 strcat(field->type, brackets);
1443 field->name = token;
1444 type = read_token(&token);
1447 new_type = realloc(field->type,
1448 strlen(field->type) +
1449 strlen(brackets) + 1);
1454 field->type = new_type;
1455 strcat(field->type, brackets);
1460 if (field_is_string(field))
1461 field->flags |= FIELD_IS_STRING;
1462 if (field_is_dynamic(field))
1463 field->flags |= FIELD_IS_DYNAMIC;
1464 if (field_is_long(field))
1465 field->flags |= FIELD_IS_LONG;
1467 if (test_type_token(type, token, EVENT_OP, ";"))
1471 if (read_expected(EVENT_ITEM, "offset") < 0)
1474 if (read_expected(EVENT_OP, ":") < 0)
1477 if (read_expect_type(EVENT_ITEM, &token))
1479 field->offset = strtoul(token, NULL, 0);
1482 if (read_expected(EVENT_OP, ";") < 0)
1485 if (read_expected(EVENT_ITEM, "size") < 0)
1488 if (read_expected(EVENT_OP, ":") < 0)
1491 if (read_expect_type(EVENT_ITEM, &token))
1493 field->size = strtoul(token, NULL, 0);
1496 if (read_expected(EVENT_OP, ";") < 0)
1499 type = read_token(&token);
1500 if (type != EVENT_NEWLINE) {
1501 /* newer versions of the kernel have a "signed" type */
1502 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1507 if (read_expected(EVENT_OP, ":") < 0)
1510 if (read_expect_type(EVENT_ITEM, &token))
1513 if (strtoul(token, NULL, 0))
1514 field->flags |= FIELD_IS_SIGNED;
1517 if (read_expected(EVENT_OP, ";") < 0)
1520 if (read_expect_type(EVENT_NEWLINE, &token))
1526 if (field->flags & FIELD_IS_ARRAY) {
1527 if (field->arraylen)
1528 field->elementsize = field->size / field->arraylen;
1529 else if (field->flags & FIELD_IS_DYNAMIC)
1530 field->elementsize = size_dynamic;
1531 else if (field->flags & FIELD_IS_STRING)
1532 field->elementsize = 1;
1533 else if (field->flags & FIELD_IS_LONG)
1534 field->elementsize = event->pevent ?
1535 event->pevent->long_size :
1538 field->elementsize = field->size;
1541 fields = &field->next;
1558 static int event_read_format(struct event_format *event)
1563 if (read_expected_item(EVENT_ITEM, "format") < 0)
1566 if (read_expected(EVENT_OP, ":") < 0)
1569 if (read_expect_type(EVENT_NEWLINE, &token))
1573 ret = event_read_fields(event, &event->format.common_fields);
1576 event->format.nr_common = ret;
1578 ret = event_read_fields(event, &event->format.fields);
1581 event->format.nr_fields = ret;
1590 static enum event_type
1591 process_arg_token(struct event_format *event, struct print_arg *arg,
1592 char **tok, enum event_type type);
1594 static enum event_type
1595 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1597 enum event_type type;
1600 type = read_token(&token);
1603 return process_arg_token(event, arg, tok, type);
1606 static enum event_type
1607 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1610 * For __print_symbolic() and __print_flags, we need to completely
1611 * evaluate the first argument, which defines what to print next.
1613 static enum event_type
1614 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1616 enum event_type type;
1618 type = process_arg(event, arg, tok);
1620 while (type == EVENT_OP) {
1621 type = process_op(event, arg, tok);
1627 static enum event_type
1628 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1630 struct print_arg *arg, *left, *right;
1631 enum event_type type;
1636 right = alloc_arg();
1638 if (!arg || !left || !right) {
1639 do_warning("%s: not enough memory!", __func__);
1640 /* arg will be freed at out_free */
1646 arg->type = PRINT_OP;
1647 arg->op.left = left;
1648 arg->op.right = right;
1651 type = process_arg(event, left, &token);
1654 /* Handle other operations in the arguments */
1655 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1656 type = process_op(event, left, &token);
1660 if (test_type_token(type, token, EVENT_OP, ":"))
1665 type = process_arg(event, right, &token);
1667 top->op.right = arg;
1673 /* Top may point to itself */
1674 top->op.right = NULL;
1680 static enum event_type
1681 process_array(struct event_format *event, struct print_arg *top, char **tok)
1683 struct print_arg *arg;
1684 enum event_type type;
1689 do_warning("%s: not enough memory!", __func__);
1690 /* '*tok' is set to top->op.op. No need to free. */
1696 type = process_arg(event, arg, &token);
1697 if (test_type_token(type, token, EVENT_OP, "]"))
1700 top->op.right = arg;
1703 type = read_token_item(&token);
1714 static int get_op_prio(char *op)
1728 /* '>>' and '<<' are 8 */
1732 /* '==' and '!=' are 10 */
1742 do_warning("unknown op '%c'", op[0]);
1746 if (strcmp(op, "++") == 0 ||
1747 strcmp(op, "--") == 0) {
1749 } else if (strcmp(op, ">>") == 0 ||
1750 strcmp(op, "<<") == 0) {
1752 } else if (strcmp(op, ">=") == 0 ||
1753 strcmp(op, "<=") == 0) {
1755 } else if (strcmp(op, "==") == 0 ||
1756 strcmp(op, "!=") == 0) {
1758 } else if (strcmp(op, "&&") == 0) {
1760 } else if (strcmp(op, "||") == 0) {
1763 do_warning("unknown op '%s'", op);
1769 static int set_op_prio(struct print_arg *arg)
1772 /* single ops are the greatest */
1773 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1776 arg->op.prio = get_op_prio(arg->op.op);
1778 return arg->op.prio;
1781 /* Note, *tok does not get freed, but will most likely be saved */
1782 static enum event_type
1783 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1785 struct print_arg *left, *right = NULL;
1786 enum event_type type;
1789 /* the op is passed in via tok */
1792 if (arg->type == PRINT_OP && !arg->op.left) {
1793 /* handle single op */
1795 do_warning("bad op token %s", token);
1805 do_warning("bad op token %s", token);
1810 /* make an empty left */
1815 left->type = PRINT_NULL;
1816 arg->op.left = left;
1818 right = alloc_arg();
1822 arg->op.right = right;
1824 /* do not free the token, it belongs to an op */
1826 type = process_arg(event, right, tok);
1828 } else if (strcmp(token, "?") == 0) {
1834 /* copy the top arg to the left */
1837 arg->type = PRINT_OP;
1839 arg->op.left = left;
1842 /* it will set arg->op.right */
1843 type = process_cond(event, arg, tok);
1845 } else if (strcmp(token, ">>") == 0 ||
1846 strcmp(token, "<<") == 0 ||
1847 strcmp(token, "&") == 0 ||
1848 strcmp(token, "|") == 0 ||
1849 strcmp(token, "&&") == 0 ||
1850 strcmp(token, "||") == 0 ||
1851 strcmp(token, "-") == 0 ||
1852 strcmp(token, "+") == 0 ||
1853 strcmp(token, "*") == 0 ||
1854 strcmp(token, "^") == 0 ||
1855 strcmp(token, "/") == 0 ||
1856 strcmp(token, "<") == 0 ||
1857 strcmp(token, ">") == 0 ||
1858 strcmp(token, "<=") == 0 ||
1859 strcmp(token, ">=") == 0 ||
1860 strcmp(token, "==") == 0 ||
1861 strcmp(token, "!=") == 0) {
1867 /* copy the top arg to the left */
1870 arg->type = PRINT_OP;
1872 arg->op.left = left;
1873 arg->op.right = NULL;
1875 if (set_op_prio(arg) == -1) {
1876 event->flags |= EVENT_FL_FAILED;
1877 /* arg->op.op (= token) will be freed at out_free */
1882 type = read_token_item(&token);
1885 /* could just be a type pointer */
1886 if ((strcmp(arg->op.op, "*") == 0) &&
1887 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1890 if (left->type != PRINT_ATOM) {
1891 do_warning("bad pointer type");
1894 new_atom = realloc(left->atom.atom,
1895 strlen(left->atom.atom) + 3);
1899 left->atom.atom = new_atom;
1900 strcat(left->atom.atom, " *");
1908 right = alloc_arg();
1912 type = process_arg_token(event, right, tok, type);
1913 arg->op.right = right;
1915 } else if (strcmp(token, "[") == 0) {
1923 arg->type = PRINT_OP;
1925 arg->op.left = left;
1929 /* it will set arg->op.right */
1930 type = process_array(event, arg, tok);
1933 do_warning("unknown op '%s'", token);
1934 event->flags |= EVENT_FL_FAILED;
1935 /* the arg is now the left side */
1939 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1942 /* higher prios need to be closer to the root */
1943 prio = get_op_prio(*tok);
1945 if (prio > arg->op.prio)
1946 return process_op(event, arg, tok);
1948 return process_op(event, right, tok);
1954 do_warning("%s: not enough memory!", __func__);
1961 static enum event_type
1962 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1965 enum event_type type;
1969 if (read_expected(EVENT_OP, "->") < 0)
1972 if (read_expect_type(EVENT_ITEM, &token) < 0)
1976 arg->type = PRINT_FIELD;
1977 arg->field.name = field;
1979 if (is_flag_field) {
1980 arg->field.field = pevent_find_any_field(event, arg->field.name);
1981 arg->field.field->flags |= FIELD_IS_FLAG;
1983 } else if (is_symbolic_field) {
1984 arg->field.field = pevent_find_any_field(event, arg->field.name);
1985 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1986 is_symbolic_field = 0;
1989 type = read_token(&token);
2001 static char *arg_eval (struct print_arg *arg);
2003 static unsigned long long
2004 eval_type_str(unsigned long long val, const char *type, int pointer)
2014 if (type[len-1] != '*') {
2015 do_warning("pointer expected with non pointer type");
2021 do_warning("%s: not enough memory!", __func__);
2024 memcpy(ref, type, len);
2026 /* chop off the " *" */
2029 val = eval_type_str(val, ref, 0);
2034 /* check if this is a pointer */
2035 if (type[len - 1] == '*')
2038 /* Try to figure out the arg size*/
2039 if (strncmp(type, "struct", 6) == 0)
2043 if (strcmp(type, "u8") == 0)
2046 if (strcmp(type, "u16") == 0)
2047 return val & 0xffff;
2049 if (strcmp(type, "u32") == 0)
2050 return val & 0xffffffff;
2052 if (strcmp(type, "u64") == 0 ||
2053 strcmp(type, "s64"))
2056 if (strcmp(type, "s8") == 0)
2057 return (unsigned long long)(char)val & 0xff;
2059 if (strcmp(type, "s16") == 0)
2060 return (unsigned long long)(short)val & 0xffff;
2062 if (strcmp(type, "s32") == 0)
2063 return (unsigned long long)(int)val & 0xffffffff;
2065 if (strncmp(type, "unsigned ", 9) == 0) {
2070 if (strcmp(type, "char") == 0) {
2072 return (unsigned long long)(char)val & 0xff;
2077 if (strcmp(type, "short") == 0) {
2079 return (unsigned long long)(short)val & 0xffff;
2081 return val & 0xffff;
2084 if (strcmp(type, "int") == 0) {
2086 return (unsigned long long)(int)val & 0xffffffff;
2088 return val & 0xffffffff;
2095 * Try to figure out the type.
2097 static unsigned long long
2098 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2100 if (arg->type != PRINT_TYPE) {
2101 do_warning("expected type argument");
2105 return eval_type_str(val, arg->typecast.type, pointer);
2108 static int arg_num_eval(struct print_arg *arg, long long *val)
2110 long long left, right;
2113 switch (arg->type) {
2115 *val = strtoll(arg->atom.atom, NULL, 0);
2118 ret = arg_num_eval(arg->typecast.item, val);
2121 *val = eval_type(*val, arg, 0);
2124 switch (arg->op.op[0]) {
2126 ret = arg_num_eval(arg->op.left, &left);
2129 ret = arg_num_eval(arg->op.right, &right);
2133 *val = left || right;
2135 *val = left | right;
2138 ret = arg_num_eval(arg->op.left, &left);
2141 ret = arg_num_eval(arg->op.right, &right);
2145 *val = left && right;
2147 *val = left & right;
2150 ret = arg_num_eval(arg->op.left, &left);
2153 ret = arg_num_eval(arg->op.right, &right);
2156 switch (arg->op.op[1]) {
2158 *val = left < right;
2161 *val = left << right;
2164 *val = left <= right;
2167 do_warning("unknown op '%s'", arg->op.op);
2172 ret = arg_num_eval(arg->op.left, &left);
2175 ret = arg_num_eval(arg->op.right, &right);
2178 switch (arg->op.op[1]) {
2180 *val = left > right;
2183 *val = left >> right;
2186 *val = left >= right;
2189 do_warning("unknown op '%s'", arg->op.op);
2194 ret = arg_num_eval(arg->op.left, &left);
2197 ret = arg_num_eval(arg->op.right, &right);
2201 if (arg->op.op[1] != '=') {
2202 do_warning("unknown op '%s'", arg->op.op);
2205 *val = left == right;
2208 ret = arg_num_eval(arg->op.left, &left);
2211 ret = arg_num_eval(arg->op.right, &right);
2215 switch (arg->op.op[1]) {
2217 *val = left != right;
2220 do_warning("unknown op '%s'", arg->op.op);
2225 /* check for negative */
2226 if (arg->op.left->type == PRINT_NULL)
2229 ret = arg_num_eval(arg->op.left, &left);
2232 ret = arg_num_eval(arg->op.right, &right);
2235 *val = left - right;
2238 if (arg->op.left->type == PRINT_NULL)
2241 ret = arg_num_eval(arg->op.left, &left);
2244 ret = arg_num_eval(arg->op.right, &right);
2247 *val = left + right;
2250 do_warning("unknown op '%s'", arg->op.op);
2256 case PRINT_FIELD ... PRINT_SYMBOL:
2260 do_warning("invalid eval type %d", arg->type);
2267 static char *arg_eval (struct print_arg *arg)
2270 static char buf[20];
2272 switch (arg->type) {
2274 return arg->atom.atom;
2276 return arg_eval(arg->typecast.item);
2278 if (!arg_num_eval(arg, &val))
2280 sprintf(buf, "%lld", val);
2284 case PRINT_FIELD ... PRINT_SYMBOL:
2288 do_warning("invalid eval type %d", arg->type);
2295 static enum event_type
2296 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2298 enum event_type type;
2299 struct print_arg *arg = NULL;
2300 struct print_flag_sym *field;
2306 type = read_token_item(&token);
2307 if (test_type_token(type, token, EVENT_OP, "{"))
2315 type = process_arg(event, arg, &token);
2317 if (type == EVENT_OP)
2318 type = process_op(event, arg, &token);
2320 if (type == EVENT_ERROR)
2323 if (test_type_token(type, token, EVENT_DELIM, ","))
2326 field = calloc(1, sizeof(*field));
2330 value = arg_eval(arg);
2332 goto out_free_field;
2333 field->value = strdup(value);
2334 if (field->value == NULL)
2335 goto out_free_field;
2343 type = process_arg(event, arg, &token);
2344 if (test_type_token(type, token, EVENT_OP, "}"))
2345 goto out_free_field;
2347 value = arg_eval(arg);
2349 goto out_free_field;
2350 field->str = strdup(value);
2351 if (field->str == NULL)
2352 goto out_free_field;
2357 list = &field->next;
2360 type = read_token_item(&token);
2361 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2367 free_flag_sym(field);
2376 static enum event_type
2377 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2379 struct print_arg *field;
2380 enum event_type type;
2383 memset(arg, 0, sizeof(*arg));
2384 arg->type = PRINT_FLAGS;
2386 field = alloc_arg();
2388 do_warning("%s: not enough memory!", __func__);
2392 type = process_field_arg(event, field, &token);
2394 /* Handle operations in the first argument */
2395 while (type == EVENT_OP)
2396 type = process_op(event, field, &token);
2398 if (test_type_token(type, token, EVENT_DELIM, ","))
2399 goto out_free_field;
2402 arg->flags.field = field;
2404 type = read_token_item(&token);
2405 if (event_item_type(type)) {
2406 arg->flags.delim = token;
2407 type = read_token_item(&token);
2410 if (test_type_token(type, token, EVENT_DELIM, ","))
2413 type = process_fields(event, &arg->flags.flags, &token);
2414 if (test_type_token(type, token, EVENT_DELIM, ")"))
2418 type = read_token_item(tok);
2429 static enum event_type
2430 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2432 struct print_arg *field;
2433 enum event_type type;
2436 memset(arg, 0, sizeof(*arg));
2437 arg->type = PRINT_SYMBOL;
2439 field = alloc_arg();
2441 do_warning("%s: not enough memory!", __func__);
2445 type = process_field_arg(event, field, &token);
2447 if (test_type_token(type, token, EVENT_DELIM, ","))
2448 goto out_free_field;
2450 arg->symbol.field = field;
2452 type = process_fields(event, &arg->symbol.symbols, &token);
2453 if (test_type_token(type, token, EVENT_DELIM, ")"))
2457 type = read_token_item(tok);
2468 static enum event_type
2469 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2471 struct print_arg *field;
2472 enum event_type type;
2475 memset(arg, 0, sizeof(*arg));
2476 arg->type = PRINT_HEX;
2478 field = alloc_arg();
2480 do_warning("%s: not enough memory!", __func__);
2484 type = process_arg(event, field, &token);
2486 if (test_type_token(type, token, EVENT_DELIM, ","))
2489 arg->hex.field = field;
2493 field = alloc_arg();
2495 do_warning("%s: not enough memory!", __func__);
2500 type = process_arg(event, field, &token);
2502 if (test_type_token(type, token, EVENT_DELIM, ")"))
2505 arg->hex.size = field;
2508 type = read_token_item(tok);
2518 static enum event_type
2519 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2521 struct format_field *field;
2522 enum event_type type;
2525 memset(arg, 0, sizeof(*arg));
2526 arg->type = PRINT_DYNAMIC_ARRAY;
2529 * The item within the parenthesis is another field that holds
2530 * the index into where the array starts.
2532 type = read_token(&token);
2534 if (type != EVENT_ITEM)
2537 /* Find the field */
2539 field = pevent_find_field(event, token);
2543 arg->dynarray.field = field;
2544 arg->dynarray.index = 0;
2546 if (read_expected(EVENT_DELIM, ")") < 0)
2550 type = read_token_item(&token);
2552 if (type != EVENT_OP || strcmp(token, "[") != 0)
2558 do_warning("%s: not enough memory!", __func__);
2563 type = process_arg(event, arg, &token);
2564 if (type == EVENT_ERROR)
2567 if (!test_type_token(type, token, EVENT_OP, "]"))
2571 type = read_token_item(tok);
2582 static enum event_type
2583 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2585 struct print_arg *item_arg;
2586 enum event_type type;
2589 type = process_arg(event, arg, &token);
2591 if (type == EVENT_ERROR)
2594 if (type == EVENT_OP)
2595 type = process_op(event, arg, &token);
2597 if (type == EVENT_ERROR)
2600 if (test_type_token(type, token, EVENT_DELIM, ")"))
2604 type = read_token_item(&token);
2607 * If the next token is an item or another open paren, then
2608 * this was a typecast.
2610 if (event_item_type(type) ||
2611 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2613 /* make this a typecast and contine */
2615 /* prevous must be an atom */
2616 if (arg->type != PRINT_ATOM) {
2617 do_warning("previous needed to be PRINT_ATOM");
2621 item_arg = alloc_arg();
2623 do_warning("%s: not enough memory!", __func__);
2627 arg->type = PRINT_TYPE;
2628 arg->typecast.type = arg->atom.atom;
2629 arg->typecast.item = item_arg;
2630 type = process_arg_token(event, item_arg, &token, type);
2644 static enum event_type
2645 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2648 enum event_type type;
2651 if (read_expect_type(EVENT_ITEM, &token) < 0)
2654 arg->type = PRINT_STRING;
2655 arg->string.string = token;
2656 arg->string.offset = -1;
2658 if (read_expected(EVENT_DELIM, ")") < 0)
2661 type = read_token(&token);
2673 static struct pevent_function_handler *
2674 find_func_handler(struct pevent *pevent, char *func_name)
2676 struct pevent_function_handler *func;
2681 for (func = pevent->func_handlers; func; func = func->next) {
2682 if (strcmp(func->name, func_name) == 0)
2689 static void remove_func_handler(struct pevent *pevent, char *func_name)
2691 struct pevent_function_handler *func;
2692 struct pevent_function_handler **next;
2694 next = &pevent->func_handlers;
2695 while ((func = *next)) {
2696 if (strcmp(func->name, func_name) == 0) {
2698 free_func_handle(func);
2705 static enum event_type
2706 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2707 struct print_arg *arg, char **tok)
2709 struct print_arg **next_arg;
2710 struct print_arg *farg;
2711 enum event_type type;
2716 arg->type = PRINT_FUNC;
2717 arg->func.func = func;
2721 next_arg = &(arg->func.args);
2722 for (i = 0; i < func->nr_args; i++) {
2725 do_warning("%s: not enough memory!", __func__);
2729 type = process_arg(event, farg, &token);
2730 if (i < (func->nr_args - 1))
2735 if (test_type_token(type, token, EVENT_DELIM, test)) {
2742 next_arg = &(farg->next);
2746 type = read_token(&token);
2752 static enum event_type
2753 process_function(struct event_format *event, struct print_arg *arg,
2754 char *token, char **tok)
2756 struct pevent_function_handler *func;
2758 if (strcmp(token, "__print_flags") == 0) {
2761 return process_flags(event, arg, tok);
2763 if (strcmp(token, "__print_symbolic") == 0) {
2765 is_symbolic_field = 1;
2766 return process_symbols(event, arg, tok);
2768 if (strcmp(token, "__print_hex") == 0) {
2770 return process_hex(event, arg, tok);
2772 if (strcmp(token, "__get_str") == 0) {
2774 return process_str(event, arg, tok);
2776 if (strcmp(token, "__get_dynamic_array") == 0) {
2778 return process_dynamic_array(event, arg, tok);
2781 func = find_func_handler(event->pevent, token);
2784 return process_func_handler(event, func, arg, tok);
2787 do_warning("function %s not defined", token);
2792 static enum event_type
2793 process_arg_token(struct event_format *event, struct print_arg *arg,
2794 char **tok, enum event_type type)
2803 if (strcmp(token, "REC") == 0) {
2805 type = process_entry(event, arg, &token);
2809 /* test the next token */
2810 type = read_token_item(&token);
2813 * If the next token is a parenthesis, then this
2816 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2819 /* this will free atom. */
2820 type = process_function(event, arg, atom, &token);
2823 /* atoms can be more than one token long */
2824 while (type == EVENT_ITEM) {
2826 new_atom = realloc(atom,
2827 strlen(atom) + strlen(token) + 2);
2836 strcat(atom, token);
2838 type = read_token_item(&token);
2841 arg->type = PRINT_ATOM;
2842 arg->atom.atom = atom;
2847 arg->type = PRINT_ATOM;
2848 arg->atom.atom = token;
2849 type = read_token_item(&token);
2852 if (strcmp(token, "(") == 0) {
2854 type = process_paren(event, arg, &token);
2858 /* handle single ops */
2859 arg->type = PRINT_OP;
2861 arg->op.left = NULL;
2862 type = process_op(event, arg, &token);
2864 /* On error, the op is freed */
2865 if (type == EVENT_ERROR)
2868 /* return error type if errored */
2871 case EVENT_ERROR ... EVENT_NEWLINE:
2873 do_warning("unexpected type %d", type);
2881 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2883 enum event_type type = EVENT_ERROR;
2884 struct print_arg *arg;
2889 if (type == EVENT_NEWLINE) {
2890 type = read_token_item(&token);
2896 do_warning("%s: not enough memory!", __func__);
2900 type = process_arg(event, arg, &token);
2902 if (type == EVENT_ERROR) {
2911 if (type == EVENT_OP) {
2912 type = process_op(event, arg, &token);
2914 if (type == EVENT_ERROR) {
2923 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2930 } while (type != EVENT_NONE);
2932 if (type != EVENT_NONE && type != EVENT_ERROR)
2938 static int event_read_print(struct event_format *event)
2940 enum event_type type;
2944 if (read_expected_item(EVENT_ITEM, "print") < 0)
2947 if (read_expected(EVENT_ITEM, "fmt") < 0)
2950 if (read_expected(EVENT_OP, ":") < 0)
2953 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2957 event->print_fmt.format = token;
2958 event->print_fmt.args = NULL;
2960 /* ok to have no arg */
2961 type = read_token_item(&token);
2963 if (type == EVENT_NONE)
2966 /* Handle concatenation of print lines */
2967 if (type == EVENT_DQUOTE) {
2970 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2973 free_token(event->print_fmt.format);
2974 event->print_fmt.format = NULL;
2979 if (test_type_token(type, token, EVENT_DELIM, ","))
2984 ret = event_read_print_args(event, &event->print_fmt.args);
2996 * pevent_find_common_field - return a common field by event
2997 * @event: handle for the event
2998 * @name: the name of the common field to return
3000 * Returns a common field from the event by the given @name.
3001 * This only searchs the common fields and not all field.
3003 struct format_field *
3004 pevent_find_common_field(struct event_format *event, const char *name)
3006 struct format_field *format;
3008 for (format = event->format.common_fields;
3009 format; format = format->next) {
3010 if (strcmp(format->name, name) == 0)
3018 * pevent_find_field - find a non-common field
3019 * @event: handle for the event
3020 * @name: the name of the non-common field
3022 * Returns a non-common field by the given @name.
3023 * This does not search common fields.
3025 struct format_field *
3026 pevent_find_field(struct event_format *event, const char *name)
3028 struct format_field *format;
3030 for (format = event->format.fields;
3031 format; format = format->next) {
3032 if (strcmp(format->name, name) == 0)
3040 * pevent_find_any_field - find any field by name
3041 * @event: handle for the event
3042 * @name: the name of the field
3044 * Returns a field by the given @name.
3045 * This searchs the common field names first, then
3046 * the non-common ones if a common one was not found.
3048 struct format_field *
3049 pevent_find_any_field(struct event_format *event, const char *name)
3051 struct format_field *format;
3053 format = pevent_find_common_field(event, name);
3056 return pevent_find_field(event, name);
3060 * pevent_read_number - read a number from data
3061 * @pevent: handle for the pevent
3062 * @ptr: the raw data
3063 * @size: the size of the data that holds the number
3065 * Returns the number (converted to host) from the
3068 unsigned long long pevent_read_number(struct pevent *pevent,
3069 const void *ptr, int size)
3073 return *(unsigned char *)ptr;
3075 return data2host2(pevent, ptr);
3077 return data2host4(pevent, ptr);
3079 return data2host8(pevent, ptr);
3087 * pevent_read_number_field - read a number from data
3088 * @field: a handle to the field
3089 * @data: the raw data to read
3090 * @value: the value to place the number in
3092 * Reads raw data according to a field offset and size,
3093 * and translates it into @value.
3095 * Returns 0 on success, -1 otherwise.
3097 int pevent_read_number_field(struct format_field *field, const void *data,
3098 unsigned long long *value)
3102 switch (field->size) {
3107 *value = pevent_read_number(field->event->pevent,
3108 data + field->offset, field->size);
3115 static int get_common_info(struct pevent *pevent,
3116 const char *type, int *offset, int *size)
3118 struct event_format *event;
3119 struct format_field *field;
3122 * All events should have the same common elements.
3123 * Pick any event to find where the type is;
3125 if (!pevent->events) {
3126 do_warning("no event_list!");
3130 event = pevent->events[0];
3131 field = pevent_find_common_field(event, type);
3135 *offset = field->offset;
3136 *size = field->size;
3141 static int __parse_common(struct pevent *pevent, void *data,
3142 int *size, int *offset, const char *name)
3147 ret = get_common_info(pevent, name, offset, size);
3151 return pevent_read_number(pevent, data + *offset, *size);
3154 static int trace_parse_common_type(struct pevent *pevent, void *data)
3156 return __parse_common(pevent, data,
3157 &pevent->type_size, &pevent->type_offset,
3161 static int parse_common_pid(struct pevent *pevent, void *data)
3163 return __parse_common(pevent, data,
3164 &pevent->pid_size, &pevent->pid_offset,
3168 static int parse_common_pc(struct pevent *pevent, void *data)
3170 return __parse_common(pevent, data,
3171 &pevent->pc_size, &pevent->pc_offset,
3172 "common_preempt_count");
3175 static int parse_common_flags(struct pevent *pevent, void *data)
3177 return __parse_common(pevent, data,
3178 &pevent->flags_size, &pevent->flags_offset,
3182 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3184 return __parse_common(pevent, data,
3185 &pevent->ld_size, &pevent->ld_offset,
3186 "common_lock_depth");
3189 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3191 return __parse_common(pevent, data,
3192 &pevent->ld_size, &pevent->ld_offset,
3193 "common_migrate_disable");
3196 static int events_id_cmp(const void *a, const void *b);
3199 * pevent_find_event - find an event by given id
3200 * @pevent: a handle to the pevent
3201 * @id: the id of the event
3203 * Returns an event that has a given @id.
3205 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3207 struct event_format **eventptr;
3208 struct event_format key;
3209 struct event_format *pkey = &key;
3211 /* Check cache first */
3212 if (pevent->last_event && pevent->last_event->id == id)
3213 return pevent->last_event;
3217 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3218 sizeof(*pevent->events), events_id_cmp);
3221 pevent->last_event = *eventptr;
3229 * pevent_find_event_by_name - find an event by given name
3230 * @pevent: a handle to the pevent
3231 * @sys: the system name to search for
3232 * @name: the name of the event to search for
3234 * This returns an event with a given @name and under the system
3235 * @sys. If @sys is NULL the first event with @name is returned.
3237 struct event_format *
3238 pevent_find_event_by_name(struct pevent *pevent,
3239 const char *sys, const char *name)
3241 struct event_format *event;
3244 if (pevent->last_event &&
3245 strcmp(pevent->last_event->name, name) == 0 &&
3246 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3247 return pevent->last_event;
3249 for (i = 0; i < pevent->nr_events; i++) {
3250 event = pevent->events[i];
3251 if (strcmp(event->name, name) == 0) {
3254 if (strcmp(event->system, sys) == 0)
3258 if (i == pevent->nr_events)
3261 pevent->last_event = event;
3265 static unsigned long long
3266 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3268 struct pevent *pevent = event->pevent;
3269 unsigned long long val = 0;
3270 unsigned long long left, right;
3271 struct print_arg *typearg = NULL;
3272 struct print_arg *larg;
3273 unsigned long offset;
3274 unsigned int field_size;
3276 switch (arg->type) {
3281 return strtoull(arg->atom.atom, NULL, 0);
3283 if (!arg->field.field) {
3284 arg->field.field = pevent_find_any_field(event, arg->field.name);
3285 if (!arg->field.field)
3286 goto out_warning_field;
3289 /* must be a number */
3290 val = pevent_read_number(pevent, data + arg->field.field->offset,
3291 arg->field.field->size);
3298 val = eval_num_arg(data, size, event, arg->typecast.item);
3299 return eval_type(val, arg, 0);
3306 val = process_defined_func(&s, data, size, event, arg);
3307 trace_seq_destroy(&s);
3311 if (strcmp(arg->op.op, "[") == 0) {
3313 * Arrays are special, since we don't want
3314 * to read the arg as is.
3316 right = eval_num_arg(data, size, event, arg->op.right);
3318 /* handle typecasts */
3319 larg = arg->op.left;
3320 while (larg->type == PRINT_TYPE) {
3323 larg = larg->typecast.item;
3326 /* Default to long size */
3327 field_size = pevent->long_size;
3329 switch (larg->type) {
3330 case PRINT_DYNAMIC_ARRAY:
3331 offset = pevent_read_number(pevent,
3332 data + larg->dynarray.field->offset,
3333 larg->dynarray.field->size);
3334 if (larg->dynarray.field->elementsize)
3335 field_size = larg->dynarray.field->elementsize;
3337 * The actual length of the dynamic array is stored
3338 * in the top half of the field, and the offset
3339 * is in the bottom half of the 32 bit field.
3345 if (!larg->field.field) {
3347 pevent_find_any_field(event, larg->field.name);
3348 if (!larg->field.field) {
3350 goto out_warning_field;
3353 field_size = larg->field.field->elementsize;
3354 offset = larg->field.field->offset +
3355 right * larg->field.field->elementsize;
3358 goto default_op; /* oops, all bets off */
3360 val = pevent_read_number(pevent,
3361 data + offset, field_size);
3363 val = eval_type(val, typearg, 1);
3365 } else if (strcmp(arg->op.op, "?") == 0) {
3366 left = eval_num_arg(data, size, event, arg->op.left);
3367 arg = arg->op.right;
3369 val = eval_num_arg(data, size, event, arg->op.left);
3371 val = eval_num_arg(data, size, event, arg->op.right);
3375 left = eval_num_arg(data, size, event, arg->op.left);
3376 right = eval_num_arg(data, size, event, arg->op.right);
3377 switch (arg->op.op[0]) {
3379 switch (arg->op.op[1]) {
3384 val = left != right;
3387 goto out_warning_op;
3395 val = left || right;
3401 val = left && right;
3406 switch (arg->op.op[1]) {
3411 val = left << right;
3414 val = left <= right;
3417 goto out_warning_op;
3421 switch (arg->op.op[1]) {
3426 val = left >> right;
3429 val = left >= right;
3432 goto out_warning_op;
3436 if (arg->op.op[1] != '=')
3437 goto out_warning_op;
3439 val = left == right;
3454 goto out_warning_op;
3457 case PRINT_DYNAMIC_ARRAY:
3458 /* Without [], we pass the address to the dynamic data */
3459 offset = pevent_read_number(pevent,
3460 data + arg->dynarray.field->offset,
3461 arg->dynarray.field->size);
3463 * The actual length of the dynamic array is stored
3464 * in the top half of the field, and the offset
3465 * is in the bottom half of the 32 bit field.
3468 val = (unsigned long long)((unsigned long)data + offset);
3470 default: /* not sure what to do there */
3476 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3480 do_warning("%s: field %s not found", __func__, arg->field.name);
3486 unsigned long long value;
3489 static const struct flag flags[] = {
3490 { "HI_SOFTIRQ", 0 },
3491 { "TIMER_SOFTIRQ", 1 },
3492 { "NET_TX_SOFTIRQ", 2 },
3493 { "NET_RX_SOFTIRQ", 3 },
3494 { "BLOCK_SOFTIRQ", 4 },
3495 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3496 { "TASKLET_SOFTIRQ", 6 },
3497 { "SCHED_SOFTIRQ", 7 },
3498 { "HRTIMER_SOFTIRQ", 8 },
3499 { "RCU_SOFTIRQ", 9 },
3501 { "HRTIMER_NORESTART", 0 },
3502 { "HRTIMER_RESTART", 1 },
3505 static unsigned long long eval_flag(const char *flag)
3510 * Some flags in the format files do not get converted.
3511 * If the flag is not numeric, see if it is something that
3512 * we already know about.
3514 if (isdigit(flag[0]))
3515 return strtoull(flag, NULL, 0);
3517 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3518 if (strcmp(flags[i].name, flag) == 0)
3519 return flags[i].value;
3524 static void print_str_to_seq(struct trace_seq *s, const char *format,
3525 int len_arg, const char *str)
3528 trace_seq_printf(s, format, len_arg, str);
3530 trace_seq_printf(s, format, str);
3533 static void print_str_arg(struct trace_seq *s, void *data, int size,
3534 struct event_format *event, const char *format,
3535 int len_arg, struct print_arg *arg)
3537 struct pevent *pevent = event->pevent;
3538 struct print_flag_sym *flag;
3539 struct format_field *field;
3540 struct printk_map *printk;
3541 unsigned long long val, fval;
3548 switch (arg->type) {
3553 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3556 field = arg->field.field;
3558 field = pevent_find_any_field(event, arg->field.name);
3560 str = arg->field.name;
3561 goto out_warning_field;
3563 arg->field.field = field;
3565 /* Zero sized fields, mean the rest of the data */
3566 len = field->size ? : size - field->offset;
3569 * Some events pass in pointers. If this is not an array
3570 * and the size is the same as long_size, assume that it
3573 if (!(field->flags & FIELD_IS_ARRAY) &&
3574 field->size == pevent->long_size) {
3575 addr = *(unsigned long *)(data + field->offset);
3576 /* Check if it matches a print format */
3577 printk = find_printk(pevent, addr);
3579 trace_seq_puts(s, printk->printk);
3581 trace_seq_printf(s, "%lx", addr);
3584 str = malloc(len + 1);
3586 do_warning("%s: not enough memory!", __func__);
3589 memcpy(str, data + field->offset, len);
3591 print_str_to_seq(s, format, len_arg, str);
3595 val = eval_num_arg(data, size, event, arg->flags.field);
3597 for (flag = arg->flags.flags; flag; flag = flag->next) {
3598 fval = eval_flag(flag->value);
3599 if (!val && !fval) {
3600 print_str_to_seq(s, format, len_arg, flag->str);
3603 if (fval && (val & fval) == fval) {
3604 if (print && arg->flags.delim)
3605 trace_seq_puts(s, arg->flags.delim);
3606 print_str_to_seq(s, format, len_arg, flag->str);
3613 val = eval_num_arg(data, size, event, arg->symbol.field);
3614 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3615 fval = eval_flag(flag->value);
3617 print_str_to_seq(s, format, len_arg, flag->str);
3623 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3624 unsigned long offset;
3625 offset = pevent_read_number(pevent,
3626 data + arg->hex.field->dynarray.field->offset,
3627 arg->hex.field->dynarray.field->size);
3628 hex = data + (offset & 0xffff);
3630 field = arg->hex.field->field.field;
3632 str = arg->hex.field->field.name;
3633 field = pevent_find_any_field(event, str);
3635 goto out_warning_field;
3636 arg->hex.field->field.field = field;
3638 hex = data + field->offset;
3640 len = eval_num_arg(data, size, event, arg->hex.size);
3641 for (i = 0; i < len; i++) {
3643 trace_seq_putc(s, ' ');
3644 trace_seq_printf(s, "%02x", hex[i]);
3650 case PRINT_STRING: {
3653 if (arg->string.offset == -1) {
3654 struct format_field *f;
3656 f = pevent_find_any_field(event, arg->string.string);
3657 arg->string.offset = f->offset;
3659 str_offset = data2host4(pevent, data + arg->string.offset);
3660 str_offset &= 0xffff;
3661 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3665 print_str_to_seq(s, format, len_arg, arg->string.string);
3669 * The only op for string should be ? :
3671 if (arg->op.op[0] != '?')
3673 val = eval_num_arg(data, size, event, arg->op.left);
3675 print_str_arg(s, data, size, event,
3676 format, len_arg, arg->op.right->op.left);
3678 print_str_arg(s, data, size, event,
3679 format, len_arg, arg->op.right->op.right);
3682 process_defined_func(s, data, size, event, arg);
3692 do_warning("%s: field %s not found", __func__, arg->field.name);
3695 static unsigned long long
3696 process_defined_func(struct trace_seq *s, void *data, int size,
3697 struct event_format *event, struct print_arg *arg)
3699 struct pevent_function_handler *func_handle = arg->func.func;
3700 struct pevent_func_params *param;
3701 unsigned long long *args;
3702 unsigned long long ret;
3703 struct print_arg *farg;
3704 struct trace_seq str;
3706 struct save_str *next;
3708 } *strings = NULL, *string;
3711 if (!func_handle->nr_args) {
3712 ret = (*func_handle->func)(s, NULL);
3716 farg = arg->func.args;
3717 param = func_handle->params;
3720 args = malloc(sizeof(*args) * func_handle->nr_args);
3724 for (i = 0; i < func_handle->nr_args; i++) {
3725 switch (param->type) {
3726 case PEVENT_FUNC_ARG_INT:
3727 case PEVENT_FUNC_ARG_LONG:
3728 case PEVENT_FUNC_ARG_PTR:
3729 args[i] = eval_num_arg(data, size, event, farg);
3731 case PEVENT_FUNC_ARG_STRING:
3732 trace_seq_init(&str);
3733 print_str_arg(&str, data, size, event, "%s", -1, farg);
3734 trace_seq_terminate(&str);
3735 string = malloc(sizeof(*string));
3737 do_warning("%s(%d): malloc str", __func__, __LINE__);
3740 string->next = strings;
3741 string->str = strdup(str.buffer);
3744 do_warning("%s(%d): malloc str", __func__, __LINE__);
3747 args[i] = (uintptr_t)string->str;
3749 trace_seq_destroy(&str);
3753 * Something went totally wrong, this is not
3754 * an input error, something in this code broke.
3756 do_warning("Unexpected end of arguments\n");
3760 param = param->next;
3763 ret = (*func_handle->func)(s, args);
3768 strings = string->next;
3774 /* TBD : handle return type here */
3778 static void free_args(struct print_arg *args)
3780 struct print_arg *next;
3790 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3792 struct pevent *pevent = event->pevent;
3793 struct format_field *field, *ip_field;
3794 struct print_arg *args, *arg, **next;
3795 unsigned long long ip, val;
3800 field = pevent->bprint_buf_field;
3801 ip_field = pevent->bprint_ip_field;
3804 field = pevent_find_field(event, "buf");
3806 do_warning("can't find buffer field for binary printk");
3809 ip_field = pevent_find_field(event, "ip");
3811 do_warning("can't find ip field for binary printk");
3814 pevent->bprint_buf_field = field;
3815 pevent->bprint_ip_field = ip_field;
3818 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3821 * The first arg is the IP pointer.
3825 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3832 arg->type = PRINT_ATOM;
3834 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3837 /* skip the first "%pf: " */
3838 for (ptr = fmt + 5, bptr = data + field->offset;
3839 bptr < data + size && *ptr; ptr++) {
3870 vsize = pevent->long_size;
3884 /* the pointers are always 4 bytes aligned */
3885 bptr = (void *)(((unsigned long)bptr + 3) &
3887 val = pevent_read_number(pevent, bptr, vsize);
3891 do_warning("%s(%d): not enough memory!",
3892 __func__, __LINE__);
3896 arg->type = PRINT_ATOM;
3897 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3904 * The '*' case means that an arg is used as the length.
3905 * We need to continue to figure out for what.
3914 do_warning("%s(%d): not enough memory!",
3915 __func__, __LINE__);
3919 arg->type = PRINT_BSTRING;
3920 arg->string.string = strdup(bptr);
3921 if (!arg->string.string)
3923 bptr += strlen(bptr) + 1;
3940 get_bprint_format(void *data, int size __maybe_unused,
3941 struct event_format *event)
3943 struct pevent *pevent = event->pevent;
3944 unsigned long long addr;
3945 struct format_field *field;
3946 struct printk_map *printk;
3949 field = pevent->bprint_fmt_field;
3952 field = pevent_find_field(event, "fmt");
3954 do_warning("can't find format field for binary printk");
3957 pevent->bprint_fmt_field = field;
3960 addr = pevent_read_number(pevent, data + field->offset, field->size);
3962 printk = find_printk(pevent, addr);
3964 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
3969 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
3975 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3976 struct event_format *event, struct print_arg *arg)
3979 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3981 if (arg->type == PRINT_FUNC) {
3982 process_defined_func(s, data, size, event, arg);
3986 if (arg->type != PRINT_FIELD) {
3987 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3993 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3994 if (!arg->field.field) {
3996 pevent_find_any_field(event, arg->field.name);
3997 if (!arg->field.field) {
3998 do_warning("%s: field %s not found",
3999 __func__, arg->field.name);
4003 if (arg->field.field->size != 6) {
4004 trace_seq_printf(s, "INVALIDMAC");
4007 buf = data + arg->field.field->offset;
4008 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4011 static int is_printable_array(char *p, unsigned int len)
4015 for (i = 0; i < len && p[i]; i++)
4016 if (!isprint(p[i]) && !isspace(p[i]))
4021 static void print_event_fields(struct trace_seq *s, void *data,
4022 int size __maybe_unused,
4023 struct event_format *event)
4025 struct format_field *field;
4026 unsigned long long val;
4027 unsigned int offset, len, i;
4029 field = event->format.fields;
4031 trace_seq_printf(s, " %s=", field->name);
4032 if (field->flags & FIELD_IS_ARRAY) {
4033 offset = field->offset;
4035 if (field->flags & FIELD_IS_DYNAMIC) {
4036 val = pevent_read_number(event->pevent, data + offset, len);
4041 if (field->flags & FIELD_IS_STRING &&
4042 is_printable_array(data + offset, len)) {
4043 trace_seq_printf(s, "%s", (char *)data + offset);
4045 trace_seq_puts(s, "ARRAY[");
4046 for (i = 0; i < len; i++) {
4048 trace_seq_puts(s, ", ");
4049 trace_seq_printf(s, "%02x",
4050 *((unsigned char *)data + offset + i));
4052 trace_seq_putc(s, ']');
4053 field->flags &= ~FIELD_IS_STRING;
4056 val = pevent_read_number(event->pevent, data + field->offset,
4058 if (field->flags & FIELD_IS_POINTER) {
4059 trace_seq_printf(s, "0x%llx", val);
4060 } else if (field->flags & FIELD_IS_SIGNED) {
4061 switch (field->size) {
4064 * If field is long then print it in hex.
4065 * A long usually stores pointers.
4067 if (field->flags & FIELD_IS_LONG)
4068 trace_seq_printf(s, "0x%x", (int)val);
4070 trace_seq_printf(s, "%d", (int)val);
4073 trace_seq_printf(s, "%2d", (short)val);
4076 trace_seq_printf(s, "%1d", (char)val);
4079 trace_seq_printf(s, "%lld", val);
4082 if (field->flags & FIELD_IS_LONG)
4083 trace_seq_printf(s, "0x%llx", val);
4085 trace_seq_printf(s, "%llu", val);
4088 field = field->next;
4092 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4094 struct pevent *pevent = event->pevent;
4095 struct print_fmt *print_fmt = &event->print_fmt;
4096 struct print_arg *arg = print_fmt->args;
4097 struct print_arg *args = NULL;
4098 const char *ptr = print_fmt->format;
4099 unsigned long long val;
4100 struct func_map *func;
4101 const char *saveptr;
4103 char *bprint_fmt = NULL;
4111 if (event->flags & EVENT_FL_FAILED) {
4112 trace_seq_printf(s, "[FAILED TO PARSE]");
4113 print_event_fields(s, data, size, event);
4117 if (event->flags & EVENT_FL_ISBPRINT) {
4118 bprint_fmt = get_bprint_format(data, size, event);
4119 args = make_bprint_args(bprint_fmt, data, size, event);
4124 for (; *ptr; ptr++) {
4130 trace_seq_putc(s, '\n');
4133 trace_seq_putc(s, '\t');
4136 trace_seq_putc(s, '\r');
4139 trace_seq_putc(s, '\\');
4142 trace_seq_putc(s, *ptr);
4146 } else if (*ptr == '%') {
4154 trace_seq_putc(s, '%');
4157 /* FIXME: need to handle properly */
4169 /* The argument is the length. */
4171 do_warning("no argument match");
4172 event->flags |= EVENT_FL_FAILED;
4175 len_arg = eval_num_arg(data, size, event, arg);
4185 if (pevent->long_size == 4)
4190 if (*(ptr+1) == 'F' ||
4194 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4195 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4208 do_warning("no argument match");
4209 event->flags |= EVENT_FL_FAILED;
4213 len = ((unsigned long)ptr + 1) -
4214 (unsigned long)saveptr;
4216 /* should never happen */
4218 do_warning("bad format!");
4219 event->flags |= EVENT_FL_FAILED;
4223 memcpy(format, saveptr, len);
4226 val = eval_num_arg(data, size, event, arg);
4230 func = find_func(pevent, val);
4232 trace_seq_puts(s, func->func);
4233 if (show_func == 'F')
4240 if (pevent->long_size == 8 && ls &&
4241 sizeof(long) != 8) {
4245 /* make %l into %ll */
4246 p = strchr(format, 'l');
4248 memmove(p+1, p, strlen(p)+1);
4249 else if (strcmp(format, "%p") == 0)
4250 strcpy(format, "0x%llx");
4255 trace_seq_printf(s, format, len_arg, (char)val);
4257 trace_seq_printf(s, format, (char)val);
4261 trace_seq_printf(s, format, len_arg, (short)val);
4263 trace_seq_printf(s, format, (short)val);
4267 trace_seq_printf(s, format, len_arg, (int)val);
4269 trace_seq_printf(s, format, (int)val);
4273 trace_seq_printf(s, format, len_arg, (long)val);
4275 trace_seq_printf(s, format, (long)val);
4279 trace_seq_printf(s, format, len_arg,
4282 trace_seq_printf(s, format, (long long)val);
4285 do_warning("bad count (%d)", ls);
4286 event->flags |= EVENT_FL_FAILED;
4291 do_warning("no matching argument");
4292 event->flags |= EVENT_FL_FAILED;
4296 len = ((unsigned long)ptr + 1) -
4297 (unsigned long)saveptr;
4299 /* should never happen */
4301 do_warning("bad format!");
4302 event->flags |= EVENT_FL_FAILED;
4306 memcpy(format, saveptr, len);
4310 /* Use helper trace_seq */
4312 print_str_arg(&p, data, size, event,
4313 format, len_arg, arg);
4314 trace_seq_terminate(&p);
4315 trace_seq_puts(s, p.buffer);
4319 trace_seq_printf(s, ">%c<", *ptr);
4323 trace_seq_putc(s, *ptr);
4326 if (event->flags & EVENT_FL_FAILED) {
4328 trace_seq_printf(s, "[FAILED TO PARSE]");
4338 * pevent_data_lat_fmt - parse the data for the latency format
4339 * @pevent: a handle to the pevent
4340 * @s: the trace_seq to write to
4341 * @record: the record to read from
4343 * This parses out the Latency format (interrupts disabled,
4344 * need rescheduling, in hard/soft interrupt, preempt count
4345 * and lock depth) and places it into the trace_seq.
4347 void pevent_data_lat_fmt(struct pevent *pevent,
4348 struct trace_seq *s, struct pevent_record *record)
4350 static int check_lock_depth = 1;
4351 static int check_migrate_disable = 1;
4352 static int lock_depth_exists;
4353 static int migrate_disable_exists;
4354 unsigned int lat_flags;
4357 int migrate_disable;
4360 void *data = record->data;
4362 lat_flags = parse_common_flags(pevent, data);
4363 pc = parse_common_pc(pevent, data);
4364 /* lock_depth may not always exist */
4365 if (lock_depth_exists)
4366 lock_depth = parse_common_lock_depth(pevent, data);
4367 else if (check_lock_depth) {
4368 lock_depth = parse_common_lock_depth(pevent, data);
4370 check_lock_depth = 0;
4372 lock_depth_exists = 1;
4375 /* migrate_disable may not always exist */
4376 if (migrate_disable_exists)
4377 migrate_disable = parse_common_migrate_disable(pevent, data);
4378 else if (check_migrate_disable) {
4379 migrate_disable = parse_common_migrate_disable(pevent, data);
4380 if (migrate_disable < 0)
4381 check_migrate_disable = 0;
4383 migrate_disable_exists = 1;
4386 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4387 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4389 trace_seq_printf(s, "%c%c%c",
4390 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4391 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4393 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4395 (hardirq && softirq) ? 'H' :
4396 hardirq ? 'h' : softirq ? 's' : '.');
4399 trace_seq_printf(s, "%x", pc);
4401 trace_seq_putc(s, '.');
4403 if (migrate_disable_exists) {
4404 if (migrate_disable < 0)
4405 trace_seq_putc(s, '.');
4407 trace_seq_printf(s, "%d", migrate_disable);
4410 if (lock_depth_exists) {
4412 trace_seq_putc(s, '.');
4414 trace_seq_printf(s, "%d", lock_depth);
4417 trace_seq_terminate(s);
4421 * pevent_data_type - parse out the given event type
4422 * @pevent: a handle to the pevent
4423 * @rec: the record to read from
4425 * This returns the event id from the @rec.
4427 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4429 return trace_parse_common_type(pevent, rec->data);
4433 * pevent_data_event_from_type - find the event by a given type
4434 * @pevent: a handle to the pevent
4435 * @type: the type of the event.
4437 * This returns the event form a given @type;
4439 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4441 return pevent_find_event(pevent, type);
4445 * pevent_data_pid - parse the PID from raw data
4446 * @pevent: a handle to the pevent
4447 * @rec: the record to parse
4449 * This returns the PID from a raw data.
4451 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4453 return parse_common_pid(pevent, rec->data);
4457 * pevent_data_comm_from_pid - return the command line from PID
4458 * @pevent: a handle to the pevent
4459 * @pid: the PID of the task to search for
4461 * This returns a pointer to the command line that has the given
4464 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4468 comm = find_cmdline(pevent, pid);
4473 * pevent_data_comm_from_pid - parse the data into the print format
4474 * @s: the trace_seq to write to
4475 * @event: the handle to the event
4476 * @record: the record to read from
4478 * This parses the raw @data using the given @event information and
4479 * writes the print format into the trace_seq.
4481 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4482 struct pevent_record *record)
4484 int print_pretty = 1;
4486 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
4487 print_event_fields(s, record->data, record->size, event);
4490 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
4491 print_pretty = event->handler(s, record, event,
4495 pretty_print(s, record->data, record->size, event);
4498 trace_seq_terminate(s);
4501 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4503 if (!use_trace_clock)
4506 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4507 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4510 /* trace_clock is setting in tsc or counter mode */
4514 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4515 struct pevent_record *record, bool use_trace_clock)
4517 static const char *spaces = " "; /* 20 spaces */
4518 struct event_format *event;
4520 unsigned long usecs;
4521 unsigned long nsecs;
4523 void *data = record->data;
4528 bool use_usec_format;
4530 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4532 if (use_usec_format) {
4533 secs = record->ts / NSECS_PER_SEC;
4534 nsecs = record->ts - secs * NSECS_PER_SEC;
4537 if (record->size < 0) {
4538 do_warning("ug! negative record size %d", record->size);
4542 type = trace_parse_common_type(pevent, data);
4544 event = pevent_find_event(pevent, type);
4546 do_warning("ug! no event found for type %d", type);
4550 pid = parse_common_pid(pevent, data);
4551 comm = find_cmdline(pevent, pid);
4553 if (pevent->latency_format) {
4554 trace_seq_printf(s, "%8.8s-%-5d %3d",
4555 comm, pid, record->cpu);
4556 pevent_data_lat_fmt(pevent, s, record);
4558 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4560 if (use_usec_format) {
4561 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4565 usecs = (nsecs + 500) / NSECS_PER_USEC;
4569 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4570 secs, p, usecs, event->name);
4572 trace_seq_printf(s, " %12llu: %s: ",
4573 record->ts, event->name);
4575 /* Space out the event names evenly. */
4576 len = strlen(event->name);
4578 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4580 pevent_event_info(s, event, record);
4583 static int events_id_cmp(const void *a, const void *b)
4585 struct event_format * const * ea = a;
4586 struct event_format * const * eb = b;
4588 if ((*ea)->id < (*eb)->id)
4591 if ((*ea)->id > (*eb)->id)
4597 static int events_name_cmp(const void *a, const void *b)
4599 struct event_format * const * ea = a;
4600 struct event_format * const * eb = b;
4603 res = strcmp((*ea)->name, (*eb)->name);
4607 res = strcmp((*ea)->system, (*eb)->system);
4611 return events_id_cmp(a, b);
4614 static int events_system_cmp(const void *a, const void *b)
4616 struct event_format * const * ea = a;
4617 struct event_format * const * eb = b;
4620 res = strcmp((*ea)->system, (*eb)->system);
4624 res = strcmp((*ea)->name, (*eb)->name);
4628 return events_id_cmp(a, b);
4631 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4633 struct event_format **events;
4634 int (*sort)(const void *a, const void *b);
4636 events = pevent->sort_events;
4638 if (events && pevent->last_type == sort_type)
4642 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4646 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4647 events[pevent->nr_events] = NULL;
4649 pevent->sort_events = events;
4651 /* the internal events are sorted by id */
4652 if (sort_type == EVENT_SORT_ID) {
4653 pevent->last_type = sort_type;
4658 switch (sort_type) {
4660 sort = events_id_cmp;
4662 case EVENT_SORT_NAME:
4663 sort = events_name_cmp;
4665 case EVENT_SORT_SYSTEM:
4666 sort = events_system_cmp;
4672 qsort(events, pevent->nr_events, sizeof(*events), sort);
4673 pevent->last_type = sort_type;
4678 static struct format_field **
4679 get_event_fields(const char *type, const char *name,
4680 int count, struct format_field *list)
4682 struct format_field **fields;
4683 struct format_field *field;
4686 fields = malloc(sizeof(*fields) * (count + 1));
4690 for (field = list; field; field = field->next) {
4691 fields[i++] = field;
4692 if (i == count + 1) {
4693 do_warning("event %s has more %s fields than specified",
4701 do_warning("event %s has less %s fields than specified",
4710 * pevent_event_common_fields - return a list of common fields for an event
4711 * @event: the event to return the common fields of.
4713 * Returns an allocated array of fields. The last item in the array is NULL.
4714 * The array must be freed with free().
4716 struct format_field **pevent_event_common_fields(struct event_format *event)
4718 return get_event_fields("common", event->name,
4719 event->format.nr_common,
4720 event->format.common_fields);
4724 * pevent_event_fields - return a list of event specific fields for an event
4725 * @event: the event to return the fields of.
4727 * Returns an allocated array of fields. The last item in the array is NULL.
4728 * The array must be freed with free().
4730 struct format_field **pevent_event_fields(struct event_format *event)
4732 return get_event_fields("event", event->name,
4733 event->format.nr_fields,
4734 event->format.fields);
4737 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4739 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4741 trace_seq_puts(s, ", ");
4742 print_fields(s, field->next);
4747 static void print_args(struct print_arg *args)
4749 int print_paren = 1;
4752 switch (args->type) {
4757 printf("%s", args->atom.atom);
4760 printf("REC->%s", args->field.name);
4763 printf("__print_flags(");
4764 print_args(args->flags.field);
4765 printf(", %s, ", args->flags.delim);
4767 print_fields(&s, args->flags.flags);
4768 trace_seq_do_printf(&s);
4769 trace_seq_destroy(&s);
4773 printf("__print_symbolic(");
4774 print_args(args->symbol.field);
4777 print_fields(&s, args->symbol.symbols);
4778 trace_seq_do_printf(&s);
4779 trace_seq_destroy(&s);
4783 printf("__print_hex(");
4784 print_args(args->hex.field);
4786 print_args(args->hex.size);
4791 printf("__get_str(%s)", args->string.string);
4794 printf("(%s)", args->typecast.type);
4795 print_args(args->typecast.item);
4798 if (strcmp(args->op.op, ":") == 0)
4802 print_args(args->op.left);
4803 printf(" %s ", args->op.op);
4804 print_args(args->op.right);
4809 /* we should warn... */
4814 print_args(args->next);
4818 static void parse_header_field(const char *field,
4819 int *offset, int *size, int mandatory)
4821 unsigned long long save_input_buf_ptr;
4822 unsigned long long save_input_buf_siz;
4826 save_input_buf_ptr = input_buf_ptr;
4827 save_input_buf_siz = input_buf_siz;
4829 if (read_expected(EVENT_ITEM, "field") < 0)
4831 if (read_expected(EVENT_OP, ":") < 0)
4835 if (read_expect_type(EVENT_ITEM, &token) < 0)
4840 * If this is not a mandatory field, then test it first.
4843 if (read_expected(EVENT_ITEM, field) < 0)
4846 if (read_expect_type(EVENT_ITEM, &token) < 0)
4848 if (strcmp(token, field) != 0)
4853 if (read_expected(EVENT_OP, ";") < 0)
4855 if (read_expected(EVENT_ITEM, "offset") < 0)
4857 if (read_expected(EVENT_OP, ":") < 0)
4859 if (read_expect_type(EVENT_ITEM, &token) < 0)
4861 *offset = atoi(token);
4863 if (read_expected(EVENT_OP, ";") < 0)
4865 if (read_expected(EVENT_ITEM, "size") < 0)
4867 if (read_expected(EVENT_OP, ":") < 0)
4869 if (read_expect_type(EVENT_ITEM, &token) < 0)
4871 *size = atoi(token);
4873 if (read_expected(EVENT_OP, ";") < 0)
4875 type = read_token(&token);
4876 if (type != EVENT_NEWLINE) {
4877 /* newer versions of the kernel have a "signed" type */
4878 if (type != EVENT_ITEM)
4881 if (strcmp(token, "signed") != 0)
4886 if (read_expected(EVENT_OP, ":") < 0)
4889 if (read_expect_type(EVENT_ITEM, &token))
4893 if (read_expected(EVENT_OP, ";") < 0)
4896 if (read_expect_type(EVENT_NEWLINE, &token))
4904 input_buf_ptr = save_input_buf_ptr;
4905 input_buf_siz = save_input_buf_siz;
4912 * pevent_parse_header_page - parse the data stored in the header page
4913 * @pevent: the handle to the pevent
4914 * @buf: the buffer storing the header page format string
4915 * @size: the size of @buf
4916 * @long_size: the long size to use if there is no header
4918 * This parses the header page format for information on the
4919 * ring buffer used. The @buf should be copied from
4921 * /sys/kernel/debug/tracing/events/header_page
4923 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4930 * Old kernels did not have header page info.
4931 * Sorry but we just use what we find here in user space.
4933 pevent->header_page_ts_size = sizeof(long long);
4934 pevent->header_page_size_size = long_size;
4935 pevent->header_page_data_offset = sizeof(long long) + long_size;
4936 pevent->old_format = 1;
4939 init_input_buf(buf, size);
4941 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4942 &pevent->header_page_ts_size, 1);
4943 parse_header_field("commit", &pevent->header_page_size_offset,
4944 &pevent->header_page_size_size, 1);
4945 parse_header_field("overwrite", &pevent->header_page_overwrite,
4947 parse_header_field("data", &pevent->header_page_data_offset,
4948 &pevent->header_page_data_size, 1);
4953 static int event_matches(struct event_format *event,
4954 int id, const char *sys_name,
4955 const char *event_name)
4957 if (id >= 0 && id != event->id)
4960 if (event_name && (strcmp(event_name, event->name) != 0))
4963 if (sys_name && (strcmp(sys_name, event->system) != 0))
4969 static void free_handler(struct event_handler *handle)
4971 free((void *)handle->sys_name);
4972 free((void *)handle->event_name);
4976 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4978 struct event_handler *handle, **next;
4980 for (next = &pevent->handlers; *next;
4981 next = &(*next)->next) {
4983 if (event_matches(event, handle->id,
4985 handle->event_name))
4992 pr_stat("overriding event (%d) %s:%s with new print handler",
4993 event->id, event->system, event->name);
4995 event->handler = handle->func;
4996 event->context = handle->context;
4998 *next = handle->next;
4999 free_handler(handle);
5005 * __pevent_parse_format - parse the event format
5006 * @buf: the buffer storing the event format string
5007 * @size: the size of @buf
5008 * @sys: the system the event belongs to
5010 * This parses the event format and creates an event structure
5011 * to quickly parse raw data for a given event.
5013 * These files currently come from:
5015 * /sys/kernel/debug/tracing/events/.../.../format
5017 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5018 struct pevent *pevent, const char *buf,
5019 unsigned long size, const char *sys)
5021 struct event_format *event;
5024 init_input_buf(buf, size);
5026 *eventp = event = alloc_event();
5028 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5030 event->name = event_read_name();
5033 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5034 goto event_alloc_failed;
5037 if (strcmp(sys, "ftrace") == 0) {
5038 event->flags |= EVENT_FL_ISFTRACE;
5040 if (strcmp(event->name, "bprint") == 0)
5041 event->flags |= EVENT_FL_ISBPRINT;
5044 event->id = event_read_id();
5045 if (event->id < 0) {
5046 ret = PEVENT_ERRNO__READ_ID_FAILED;
5048 * This isn't an allocation error actually.
5049 * But as the ID is critical, just bail out.
5051 goto event_alloc_failed;
5054 event->system = strdup(sys);
5055 if (!event->system) {
5056 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5057 goto event_alloc_failed;
5060 /* Add pevent to event so that it can be referenced */
5061 event->pevent = pevent;
5063 ret = event_read_format(event);
5065 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5066 goto event_parse_failed;
5070 * If the event has an override, don't print warnings if the event
5071 * print format fails to parse.
5073 if (pevent && find_event_handle(pevent, event))
5076 ret = event_read_print(event);
5080 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5081 goto event_parse_failed;
5084 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5085 struct format_field *field;
5086 struct print_arg *arg, **list;
5088 /* old ftrace had no args */
5089 list = &event->print_fmt.args;
5090 for (field = event->format.fields; field; field = field->next) {
5093 event->flags |= EVENT_FL_FAILED;
5094 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5096 arg->type = PRINT_FIELD;
5097 arg->field.name = strdup(field->name);
5098 if (!arg->field.name) {
5099 event->flags |= EVENT_FL_FAILED;
5101 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5103 arg->field.field = field;
5113 event->flags |= EVENT_FL_FAILED;
5117 free(event->system);
5125 * pevent_parse_format - parse the event format
5126 * @buf: the buffer storing the event format string
5127 * @size: the size of @buf
5128 * @sys: the system the event belongs to
5130 * This parses the event format and creates an event structure
5131 * to quickly parse raw data for a given event.
5133 * These files currently come from:
5135 * /sys/kernel/debug/tracing/events/.../.../format
5137 enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5138 unsigned long size, const char *sys)
5140 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5144 * pevent_parse_event - parse the event format
5145 * @pevent: the handle to the pevent
5146 * @buf: the buffer storing the event format string
5147 * @size: the size of @buf
5148 * @sys: the system the event belongs to
5150 * This parses the event format and creates an event structure
5151 * to quickly parse raw data for a given event.
5153 * These files currently come from:
5155 * /sys/kernel/debug/tracing/events/.../.../format
5157 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5158 unsigned long size, const char *sys)
5160 struct event_format *event = NULL;
5161 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5166 if (add_event(pevent, event)) {
5167 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5168 goto event_add_failed;
5171 #define PRINT_ARGS 0
5172 if (PRINT_ARGS && event->print_fmt.args)
5173 print_args(event->print_fmt.args);
5178 pevent_free_format(event);
5183 #define _PE(code, str) str
5184 static const char * const pevent_error_str[] = {
5189 int pevent_strerror(struct pevent *pevent __maybe_unused,
5190 enum pevent_errno errnum, char *buf, size_t buflen)
5196 msg = strerror_r(errnum, buf, buflen);
5198 size_t len = strlen(msg);
5199 memcpy(buf, msg, min(buflen - 1, len));
5200 *(buf + min(buflen - 1, len)) = '\0';
5205 if (errnum <= __PEVENT_ERRNO__START ||
5206 errnum >= __PEVENT_ERRNO__END)
5209 idx = errnum - __PEVENT_ERRNO__START - 1;
5210 msg = pevent_error_str[idx];
5213 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5214 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5215 case PEVENT_ERRNO__READ_ID_FAILED:
5216 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5217 case PEVENT_ERRNO__READ_PRINT_FAILED:
5218 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
5219 case PEVENT_ERRNO__INVALID_ARG_TYPE:
5220 snprintf(buf, buflen, "%s", msg);
5224 /* cannot reach here */
5231 int get_field_val(struct trace_seq *s, struct format_field *field,
5232 const char *name, struct pevent_record *record,
5233 unsigned long long *val, int err)
5237 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5241 if (pevent_read_number_field(field, record->data, val)) {
5243 trace_seq_printf(s, " %s=INVALID", name);
5251 * pevent_get_field_raw - return the raw pointer into the data field
5252 * @s: The seq to print to on error
5253 * @event: the event that the field is for
5254 * @name: The name of the field
5255 * @record: The record with the field name.
5256 * @len: place to store the field length.
5257 * @err: print default error if failed.
5259 * Returns a pointer into record->data of the field and places
5260 * the length of the field in @len.
5262 * On failure, it returns NULL.
5264 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5265 const char *name, struct pevent_record *record,
5268 struct format_field *field;
5269 void *data = record->data;
5276 field = pevent_find_field(event, name);
5280 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5284 /* Allow @len to be NULL */
5288 offset = field->offset;
5289 if (field->flags & FIELD_IS_DYNAMIC) {
5290 offset = pevent_read_number(event->pevent,
5291 data + offset, field->size);
5292 *len = offset >> 16;
5297 return data + offset;
5301 * pevent_get_field_val - find a field and return its value
5302 * @s: The seq to print to on error
5303 * @event: the event that the field is for
5304 * @name: The name of the field
5305 * @record: The record with the field name.
5306 * @val: place to store the value of the field.
5307 * @err: print default error if failed.
5309 * Returns 0 on success -1 on field not found.
5311 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5312 const char *name, struct pevent_record *record,
5313 unsigned long long *val, int err)
5315 struct format_field *field;
5320 field = pevent_find_field(event, name);
5322 return get_field_val(s, field, name, record, val, err);
5326 * pevent_get_common_field_val - find a common field and return its value
5327 * @s: The seq to print to on error
5328 * @event: the event that the field is for
5329 * @name: The name of the field
5330 * @record: The record with the field name.
5331 * @val: place to store the value of the field.
5332 * @err: print default error if failed.
5334 * Returns 0 on success -1 on field not found.
5336 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5337 const char *name, struct pevent_record *record,
5338 unsigned long long *val, int err)
5340 struct format_field *field;
5345 field = pevent_find_common_field(event, name);
5347 return get_field_val(s, field, name, record, val, err);
5351 * pevent_get_any_field_val - find a any field and return its value
5352 * @s: The seq to print to on error
5353 * @event: the event that the field is for
5354 * @name: The name of the field
5355 * @record: The record with the field name.
5356 * @val: place to store the value of the field.
5357 * @err: print default error if failed.
5359 * Returns 0 on success -1 on field not found.
5361 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5362 const char *name, struct pevent_record *record,
5363 unsigned long long *val, int err)
5365 struct format_field *field;
5370 field = pevent_find_any_field(event, name);
5372 return get_field_val(s, field, name, record, val, err);
5376 * pevent_print_num_field - print a field and a format
5377 * @s: The seq to print to
5378 * @fmt: The printf format to print the field with.
5379 * @event: the event that the field is for
5380 * @name: The name of the field
5381 * @record: The record with the field name.
5382 * @err: print default error if failed.
5384 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5386 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5387 struct event_format *event, const char *name,
5388 struct pevent_record *record, int err)
5390 struct format_field *field = pevent_find_field(event, name);
5391 unsigned long long val;
5396 if (pevent_read_number_field(field, record->data, &val))
5399 return trace_seq_printf(s, fmt, val);
5403 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5408 * pevent_print_func_field - print a field and a format for function pointers
5409 * @s: The seq to print to
5410 * @fmt: The printf format to print the field with.
5411 * @event: the event that the field is for
5412 * @name: The name of the field
5413 * @record: The record with the field name.
5414 * @err: print default error if failed.
5416 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5418 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5419 struct event_format *event, const char *name,
5420 struct pevent_record *record, int err)
5422 struct format_field *field = pevent_find_field(event, name);
5423 struct pevent *pevent = event->pevent;
5424 unsigned long long val;
5425 struct func_map *func;
5431 if (pevent_read_number_field(field, record->data, &val))
5434 func = find_func(pevent, val);
5437 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5439 sprintf(tmp, "0x%08llx", val);
5441 return trace_seq_printf(s, fmt, tmp);
5445 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5449 static void free_func_handle(struct pevent_function_handler *func)
5451 struct pevent_func_params *params;
5455 while (func->params) {
5456 params = func->params;
5457 func->params = params->next;
5465 * pevent_register_print_function - register a helper function
5466 * @pevent: the handle to the pevent
5467 * @func: the function to process the helper function
5468 * @ret_type: the return type of the helper function
5469 * @name: the name of the helper function
5470 * @parameters: A list of enum pevent_func_arg_type
5472 * Some events may have helper functions in the print format arguments.
5473 * This allows a plugin to dynamically create a way to process one
5474 * of these functions.
5476 * The @parameters is a variable list of pevent_func_arg_type enums that
5477 * must end with PEVENT_FUNC_ARG_VOID.
5479 int pevent_register_print_function(struct pevent *pevent,
5480 pevent_func_handler func,
5481 enum pevent_func_arg_type ret_type,
5484 struct pevent_function_handler *func_handle;
5485 struct pevent_func_params **next_param;
5486 struct pevent_func_params *param;
5487 enum pevent_func_arg_type type;
5491 func_handle = find_func_handler(pevent, name);
5494 * This is most like caused by the users own
5495 * plugins updating the function. This overrides the
5498 pr_stat("override of function helper '%s'", name);
5499 remove_func_handler(pevent, name);
5502 func_handle = calloc(1, sizeof(*func_handle));
5504 do_warning("Failed to allocate function handler");
5505 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5508 func_handle->ret_type = ret_type;
5509 func_handle->name = strdup(name);
5510 func_handle->func = func;
5511 if (!func_handle->name) {
5512 do_warning("Failed to allocate function name");
5514 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5517 next_param = &(func_handle->params);
5520 type = va_arg(ap, enum pevent_func_arg_type);
5521 if (type == PEVENT_FUNC_ARG_VOID)
5524 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5525 do_warning("Invalid argument type %d", type);
5526 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5530 param = malloc(sizeof(*param));
5532 do_warning("Failed to allocate function param");
5533 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5539 *next_param = param;
5540 next_param = &(param->next);
5542 func_handle->nr_args++;
5546 func_handle->next = pevent->func_handlers;
5547 pevent->func_handlers = func_handle;
5552 free_func_handle(func_handle);
5557 * pevent_register_event_handler - register a way to parse an event
5558 * @pevent: the handle to the pevent
5559 * @id: the id of the event to register
5560 * @sys_name: the system name the event belongs to
5561 * @event_name: the name of the event
5562 * @func: the function to call to parse the event information
5563 * @context: the data to be passed to @func
5565 * This function allows a developer to override the parsing of
5566 * a given event. If for some reason the default print format
5567 * is not sufficient, this function will register a function
5568 * for an event to be used to parse the data instead.
5570 * If @id is >= 0, then it is used to find the event.
5571 * else @sys_name and @event_name are used.
5573 int pevent_register_event_handler(struct pevent *pevent, int id,
5574 const char *sys_name, const char *event_name,
5575 pevent_event_handler_func func, void *context)
5577 struct event_format *event;
5578 struct event_handler *handle;
5582 event = pevent_find_event(pevent, id);
5585 if (event_name && (strcmp(event_name, event->name) != 0))
5587 if (sys_name && (strcmp(sys_name, event->system) != 0))
5590 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5595 pr_stat("overriding event (%d) %s:%s with new print handler",
5596 event->id, event->system, event->name);
5598 event->handler = func;
5599 event->context = context;
5603 /* Save for later use. */
5604 handle = calloc(1, sizeof(*handle));
5606 do_warning("Failed to allocate event handler");
5607 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5612 handle->event_name = strdup(event_name);
5614 handle->sys_name = strdup(sys_name);
5616 if ((event_name && !handle->event_name) ||
5617 (sys_name && !handle->sys_name)) {
5618 do_warning("Failed to allocate event/sys name");
5619 free((void *)handle->event_name);
5620 free((void *)handle->sys_name);
5622 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5625 handle->func = func;
5626 handle->next = pevent->handlers;
5627 pevent->handlers = handle;
5628 handle->context = context;
5634 * pevent_alloc - create a pevent handle
5636 struct pevent *pevent_alloc(void)
5638 struct pevent *pevent = calloc(1, sizeof(*pevent));
5641 pevent->ref_count = 1;
5646 void pevent_ref(struct pevent *pevent)
5648 pevent->ref_count++;
5651 static void free_format_fields(struct format_field *field)
5653 struct format_field *next;
5664 static void free_formats(struct format *format)
5666 free_format_fields(format->common_fields);
5667 free_format_fields(format->fields);
5670 void pevent_free_format(struct event_format *event)
5673 free(event->system);
5675 free_formats(&event->format);
5677 free(event->print_fmt.format);
5678 free_args(event->print_fmt.args);
5684 * pevent_free - free a pevent handle
5685 * @pevent: the pevent handle to free
5687 void pevent_free(struct pevent *pevent)
5689 struct cmdline_list *cmdlist, *cmdnext;
5690 struct func_list *funclist, *funcnext;
5691 struct printk_list *printklist, *printknext;
5692 struct pevent_function_handler *func_handler;
5693 struct event_handler *handle;
5699 cmdlist = pevent->cmdlist;
5700 funclist = pevent->funclist;
5701 printklist = pevent->printklist;
5703 pevent->ref_count--;
5704 if (pevent->ref_count)
5707 if (pevent->cmdlines) {
5708 for (i = 0; i < pevent->cmdline_count; i++)
5709 free(pevent->cmdlines[i].comm);
5710 free(pevent->cmdlines);
5714 cmdnext = cmdlist->next;
5715 free(cmdlist->comm);
5720 if (pevent->func_map) {
5721 for (i = 0; i < (int)pevent->func_count; i++) {
5722 free(pevent->func_map[i].func);
5723 free(pevent->func_map[i].mod);
5725 free(pevent->func_map);
5729 funcnext = funclist->next;
5730 free(funclist->func);
5731 free(funclist->mod);
5733 funclist = funcnext;
5736 while (pevent->func_handlers) {
5737 func_handler = pevent->func_handlers;
5738 pevent->func_handlers = func_handler->next;
5739 free_func_handle(func_handler);
5742 if (pevent->printk_map) {
5743 for (i = 0; i < (int)pevent->printk_count; i++)
5744 free(pevent->printk_map[i].printk);
5745 free(pevent->printk_map);
5748 while (printklist) {
5749 printknext = printklist->next;
5750 free(printklist->printk);
5752 printklist = printknext;
5755 for (i = 0; i < pevent->nr_events; i++)
5756 pevent_free_format(pevent->events[i]);
5758 while (pevent->handlers) {
5759 handle = pevent->handlers;
5760 pevent->handlers = handle->next;
5761 free_handler(handle);
5764 free(pevent->events);
5765 free(pevent->sort_events);
5770 void pevent_unref(struct pevent *pevent)
5772 pevent_free(pevent);