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);
1609 static enum event_type
1610 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1612 struct print_arg *arg, *left, *right;
1613 enum event_type type;
1618 right = alloc_arg();
1620 if (!arg || !left || !right) {
1621 do_warning("%s: not enough memory!", __func__);
1622 /* arg will be freed at out_free */
1628 arg->type = PRINT_OP;
1629 arg->op.left = left;
1630 arg->op.right = right;
1633 type = process_arg(event, left, &token);
1636 /* Handle other operations in the arguments */
1637 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1638 type = process_op(event, left, &token);
1642 if (test_type_token(type, token, EVENT_OP, ":"))
1647 type = process_arg(event, right, &token);
1649 top->op.right = arg;
1655 /* Top may point to itself */
1656 top->op.right = NULL;
1662 static enum event_type
1663 process_array(struct event_format *event, struct print_arg *top, char **tok)
1665 struct print_arg *arg;
1666 enum event_type type;
1671 do_warning("%s: not enough memory!", __func__);
1672 /* '*tok' is set to top->op.op. No need to free. */
1678 type = process_arg(event, arg, &token);
1679 if (test_type_token(type, token, EVENT_OP, "]"))
1682 top->op.right = arg;
1685 type = read_token_item(&token);
1696 static int get_op_prio(char *op)
1710 /* '>>' and '<<' are 8 */
1714 /* '==' and '!=' are 10 */
1724 do_warning("unknown op '%c'", op[0]);
1728 if (strcmp(op, "++") == 0 ||
1729 strcmp(op, "--") == 0) {
1731 } else if (strcmp(op, ">>") == 0 ||
1732 strcmp(op, "<<") == 0) {
1734 } else if (strcmp(op, ">=") == 0 ||
1735 strcmp(op, "<=") == 0) {
1737 } else if (strcmp(op, "==") == 0 ||
1738 strcmp(op, "!=") == 0) {
1740 } else if (strcmp(op, "&&") == 0) {
1742 } else if (strcmp(op, "||") == 0) {
1745 do_warning("unknown op '%s'", op);
1751 static int set_op_prio(struct print_arg *arg)
1754 /* single ops are the greatest */
1755 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1758 arg->op.prio = get_op_prio(arg->op.op);
1760 return arg->op.prio;
1763 /* Note, *tok does not get freed, but will most likely be saved */
1764 static enum event_type
1765 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1767 struct print_arg *left, *right = NULL;
1768 enum event_type type;
1771 /* the op is passed in via tok */
1774 if (arg->type == PRINT_OP && !arg->op.left) {
1775 /* handle single op */
1777 do_warning("bad op token %s", token);
1787 do_warning("bad op token %s", token);
1792 /* make an empty left */
1797 left->type = PRINT_NULL;
1798 arg->op.left = left;
1800 right = alloc_arg();
1804 arg->op.right = right;
1806 /* do not free the token, it belongs to an op */
1808 type = process_arg(event, right, tok);
1810 } else if (strcmp(token, "?") == 0) {
1816 /* copy the top arg to the left */
1819 arg->type = PRINT_OP;
1821 arg->op.left = left;
1824 /* it will set arg->op.right */
1825 type = process_cond(event, arg, tok);
1827 } else if (strcmp(token, ">>") == 0 ||
1828 strcmp(token, "<<") == 0 ||
1829 strcmp(token, "&") == 0 ||
1830 strcmp(token, "|") == 0 ||
1831 strcmp(token, "&&") == 0 ||
1832 strcmp(token, "||") == 0 ||
1833 strcmp(token, "-") == 0 ||
1834 strcmp(token, "+") == 0 ||
1835 strcmp(token, "*") == 0 ||
1836 strcmp(token, "^") == 0 ||
1837 strcmp(token, "/") == 0 ||
1838 strcmp(token, "<") == 0 ||
1839 strcmp(token, ">") == 0 ||
1840 strcmp(token, "<=") == 0 ||
1841 strcmp(token, ">=") == 0 ||
1842 strcmp(token, "==") == 0 ||
1843 strcmp(token, "!=") == 0) {
1849 /* copy the top arg to the left */
1852 arg->type = PRINT_OP;
1854 arg->op.left = left;
1855 arg->op.right = NULL;
1857 if (set_op_prio(arg) == -1) {
1858 event->flags |= EVENT_FL_FAILED;
1859 /* arg->op.op (= token) will be freed at out_free */
1864 type = read_token_item(&token);
1867 /* could just be a type pointer */
1868 if ((strcmp(arg->op.op, "*") == 0) &&
1869 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1872 if (left->type != PRINT_ATOM) {
1873 do_warning("bad pointer type");
1876 new_atom = realloc(left->atom.atom,
1877 strlen(left->atom.atom) + 3);
1881 left->atom.atom = new_atom;
1882 strcat(left->atom.atom, " *");
1890 right = alloc_arg();
1894 type = process_arg_token(event, right, tok, type);
1895 arg->op.right = right;
1897 } else if (strcmp(token, "[") == 0) {
1905 arg->type = PRINT_OP;
1907 arg->op.left = left;
1911 /* it will set arg->op.right */
1912 type = process_array(event, arg, tok);
1915 do_warning("unknown op '%s'", token);
1916 event->flags |= EVENT_FL_FAILED;
1917 /* the arg is now the left side */
1921 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1924 /* higher prios need to be closer to the root */
1925 prio = get_op_prio(*tok);
1927 if (prio > arg->op.prio)
1928 return process_op(event, arg, tok);
1930 return process_op(event, right, tok);
1936 do_warning("%s: not enough memory!", __func__);
1943 static enum event_type
1944 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1947 enum event_type type;
1951 if (read_expected(EVENT_OP, "->") < 0)
1954 if (read_expect_type(EVENT_ITEM, &token) < 0)
1958 arg->type = PRINT_FIELD;
1959 arg->field.name = field;
1961 if (is_flag_field) {
1962 arg->field.field = pevent_find_any_field(event, arg->field.name);
1963 arg->field.field->flags |= FIELD_IS_FLAG;
1965 } else if (is_symbolic_field) {
1966 arg->field.field = pevent_find_any_field(event, arg->field.name);
1967 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1968 is_symbolic_field = 0;
1971 type = read_token(&token);
1983 static char *arg_eval (struct print_arg *arg);
1985 static unsigned long long
1986 eval_type_str(unsigned long long val, const char *type, int pointer)
1996 if (type[len-1] != '*') {
1997 do_warning("pointer expected with non pointer type");
2003 do_warning("%s: not enough memory!", __func__);
2006 memcpy(ref, type, len);
2008 /* chop off the " *" */
2011 val = eval_type_str(val, ref, 0);
2016 /* check if this is a pointer */
2017 if (type[len - 1] == '*')
2020 /* Try to figure out the arg size*/
2021 if (strncmp(type, "struct", 6) == 0)
2025 if (strcmp(type, "u8") == 0)
2028 if (strcmp(type, "u16") == 0)
2029 return val & 0xffff;
2031 if (strcmp(type, "u32") == 0)
2032 return val & 0xffffffff;
2034 if (strcmp(type, "u64") == 0 ||
2035 strcmp(type, "s64"))
2038 if (strcmp(type, "s8") == 0)
2039 return (unsigned long long)(char)val & 0xff;
2041 if (strcmp(type, "s16") == 0)
2042 return (unsigned long long)(short)val & 0xffff;
2044 if (strcmp(type, "s32") == 0)
2045 return (unsigned long long)(int)val & 0xffffffff;
2047 if (strncmp(type, "unsigned ", 9) == 0) {
2052 if (strcmp(type, "char") == 0) {
2054 return (unsigned long long)(char)val & 0xff;
2059 if (strcmp(type, "short") == 0) {
2061 return (unsigned long long)(short)val & 0xffff;
2063 return val & 0xffff;
2066 if (strcmp(type, "int") == 0) {
2068 return (unsigned long long)(int)val & 0xffffffff;
2070 return val & 0xffffffff;
2077 * Try to figure out the type.
2079 static unsigned long long
2080 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2082 if (arg->type != PRINT_TYPE) {
2083 do_warning("expected type argument");
2087 return eval_type_str(val, arg->typecast.type, pointer);
2090 static int arg_num_eval(struct print_arg *arg, long long *val)
2092 long long left, right;
2095 switch (arg->type) {
2097 *val = strtoll(arg->atom.atom, NULL, 0);
2100 ret = arg_num_eval(arg->typecast.item, val);
2103 *val = eval_type(*val, arg, 0);
2106 switch (arg->op.op[0]) {
2108 ret = arg_num_eval(arg->op.left, &left);
2111 ret = arg_num_eval(arg->op.right, &right);
2115 *val = left || right;
2117 *val = left | right;
2120 ret = arg_num_eval(arg->op.left, &left);
2123 ret = arg_num_eval(arg->op.right, &right);
2127 *val = left && right;
2129 *val = left & right;
2132 ret = arg_num_eval(arg->op.left, &left);
2135 ret = arg_num_eval(arg->op.right, &right);
2138 switch (arg->op.op[1]) {
2140 *val = left < right;
2143 *val = left << right;
2146 *val = left <= right;
2149 do_warning("unknown op '%s'", arg->op.op);
2154 ret = arg_num_eval(arg->op.left, &left);
2157 ret = arg_num_eval(arg->op.right, &right);
2160 switch (arg->op.op[1]) {
2162 *val = left > right;
2165 *val = left >> right;
2168 *val = left >= right;
2171 do_warning("unknown op '%s'", arg->op.op);
2176 ret = arg_num_eval(arg->op.left, &left);
2179 ret = arg_num_eval(arg->op.right, &right);
2183 if (arg->op.op[1] != '=') {
2184 do_warning("unknown op '%s'", arg->op.op);
2187 *val = left == right;
2190 ret = arg_num_eval(arg->op.left, &left);
2193 ret = arg_num_eval(arg->op.right, &right);
2197 switch (arg->op.op[1]) {
2199 *val = left != right;
2202 do_warning("unknown op '%s'", arg->op.op);
2207 /* check for negative */
2208 if (arg->op.left->type == PRINT_NULL)
2211 ret = arg_num_eval(arg->op.left, &left);
2214 ret = arg_num_eval(arg->op.right, &right);
2217 *val = left - right;
2220 if (arg->op.left->type == PRINT_NULL)
2223 ret = arg_num_eval(arg->op.left, &left);
2226 ret = arg_num_eval(arg->op.right, &right);
2229 *val = left + right;
2232 do_warning("unknown op '%s'", arg->op.op);
2238 case PRINT_FIELD ... PRINT_SYMBOL:
2242 do_warning("invalid eval type %d", arg->type);
2249 static char *arg_eval (struct print_arg *arg)
2252 static char buf[20];
2254 switch (arg->type) {
2256 return arg->atom.atom;
2258 return arg_eval(arg->typecast.item);
2260 if (!arg_num_eval(arg, &val))
2262 sprintf(buf, "%lld", val);
2266 case PRINT_FIELD ... PRINT_SYMBOL:
2270 do_warning("invalid eval type %d", arg->type);
2277 static enum event_type
2278 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2280 enum event_type type;
2281 struct print_arg *arg = NULL;
2282 struct print_flag_sym *field;
2288 type = read_token_item(&token);
2289 if (test_type_token(type, token, EVENT_OP, "{"))
2297 type = process_arg(event, arg, &token);
2299 if (type == EVENT_OP)
2300 type = process_op(event, arg, &token);
2302 if (type == EVENT_ERROR)
2305 if (test_type_token(type, token, EVENT_DELIM, ","))
2308 field = calloc(1, sizeof(*field));
2312 value = arg_eval(arg);
2314 goto out_free_field;
2315 field->value = strdup(value);
2316 if (field->value == NULL)
2317 goto out_free_field;
2325 type = process_arg(event, arg, &token);
2326 if (test_type_token(type, token, EVENT_OP, "}"))
2327 goto out_free_field;
2329 value = arg_eval(arg);
2331 goto out_free_field;
2332 field->str = strdup(value);
2333 if (field->str == NULL)
2334 goto out_free_field;
2339 list = &field->next;
2342 type = read_token_item(&token);
2343 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2349 free_flag_sym(field);
2358 static enum event_type
2359 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2361 struct print_arg *field;
2362 enum event_type type;
2365 memset(arg, 0, sizeof(*arg));
2366 arg->type = PRINT_FLAGS;
2368 field = alloc_arg();
2370 do_warning("%s: not enough memory!", __func__);
2374 type = process_arg(event, field, &token);
2376 /* Handle operations in the first argument */
2377 while (type == EVENT_OP)
2378 type = process_op(event, field, &token);
2380 if (test_type_token(type, token, EVENT_DELIM, ","))
2381 goto out_free_field;
2384 arg->flags.field = field;
2386 type = read_token_item(&token);
2387 if (event_item_type(type)) {
2388 arg->flags.delim = token;
2389 type = read_token_item(&token);
2392 if (test_type_token(type, token, EVENT_DELIM, ","))
2395 type = process_fields(event, &arg->flags.flags, &token);
2396 if (test_type_token(type, token, EVENT_DELIM, ")"))
2400 type = read_token_item(tok);
2411 static enum event_type
2412 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2414 struct print_arg *field;
2415 enum event_type type;
2418 memset(arg, 0, sizeof(*arg));
2419 arg->type = PRINT_SYMBOL;
2421 field = alloc_arg();
2423 do_warning("%s: not enough memory!", __func__);
2427 type = process_arg(event, field, &token);
2428 if (test_type_token(type, token, EVENT_DELIM, ","))
2429 goto out_free_field;
2431 arg->symbol.field = field;
2433 type = process_fields(event, &arg->symbol.symbols, &token);
2434 if (test_type_token(type, token, EVENT_DELIM, ")"))
2438 type = read_token_item(tok);
2449 static enum event_type
2450 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2452 struct print_arg *field;
2453 enum event_type type;
2456 memset(arg, 0, sizeof(*arg));
2457 arg->type = PRINT_HEX;
2459 field = alloc_arg();
2461 do_warning("%s: not enough memory!", __func__);
2465 type = process_arg(event, field, &token);
2467 if (test_type_token(type, token, EVENT_DELIM, ","))
2470 arg->hex.field = field;
2474 field = alloc_arg();
2476 do_warning("%s: not enough memory!", __func__);
2481 type = process_arg(event, field, &token);
2483 if (test_type_token(type, token, EVENT_DELIM, ")"))
2486 arg->hex.size = field;
2489 type = read_token_item(tok);
2499 static enum event_type
2500 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2502 struct format_field *field;
2503 enum event_type type;
2506 memset(arg, 0, sizeof(*arg));
2507 arg->type = PRINT_DYNAMIC_ARRAY;
2510 * The item within the parenthesis is another field that holds
2511 * the index into where the array starts.
2513 type = read_token(&token);
2515 if (type != EVENT_ITEM)
2518 /* Find the field */
2520 field = pevent_find_field(event, token);
2524 arg->dynarray.field = field;
2525 arg->dynarray.index = 0;
2527 if (read_expected(EVENT_DELIM, ")") < 0)
2531 type = read_token_item(&token);
2533 if (type != EVENT_OP || strcmp(token, "[") != 0)
2539 do_warning("%s: not enough memory!", __func__);
2544 type = process_arg(event, arg, &token);
2545 if (type == EVENT_ERROR)
2548 if (!test_type_token(type, token, EVENT_OP, "]"))
2552 type = read_token_item(tok);
2563 static enum event_type
2564 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2566 struct print_arg *item_arg;
2567 enum event_type type;
2570 type = process_arg(event, arg, &token);
2572 if (type == EVENT_ERROR)
2575 if (type == EVENT_OP)
2576 type = process_op(event, arg, &token);
2578 if (type == EVENT_ERROR)
2581 if (test_type_token(type, token, EVENT_DELIM, ")"))
2585 type = read_token_item(&token);
2588 * If the next token is an item or another open paren, then
2589 * this was a typecast.
2591 if (event_item_type(type) ||
2592 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2594 /* make this a typecast and contine */
2596 /* prevous must be an atom */
2597 if (arg->type != PRINT_ATOM) {
2598 do_warning("previous needed to be PRINT_ATOM");
2602 item_arg = alloc_arg();
2604 do_warning("%s: not enough memory!", __func__);
2608 arg->type = PRINT_TYPE;
2609 arg->typecast.type = arg->atom.atom;
2610 arg->typecast.item = item_arg;
2611 type = process_arg_token(event, item_arg, &token, type);
2625 static enum event_type
2626 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2629 enum event_type type;
2632 if (read_expect_type(EVENT_ITEM, &token) < 0)
2635 arg->type = PRINT_STRING;
2636 arg->string.string = token;
2637 arg->string.offset = -1;
2639 if (read_expected(EVENT_DELIM, ")") < 0)
2642 type = read_token(&token);
2654 static struct pevent_function_handler *
2655 find_func_handler(struct pevent *pevent, char *func_name)
2657 struct pevent_function_handler *func;
2662 for (func = pevent->func_handlers; func; func = func->next) {
2663 if (strcmp(func->name, func_name) == 0)
2670 static void remove_func_handler(struct pevent *pevent, char *func_name)
2672 struct pevent_function_handler *func;
2673 struct pevent_function_handler **next;
2675 next = &pevent->func_handlers;
2676 while ((func = *next)) {
2677 if (strcmp(func->name, func_name) == 0) {
2679 free_func_handle(func);
2686 static enum event_type
2687 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2688 struct print_arg *arg, char **tok)
2690 struct print_arg **next_arg;
2691 struct print_arg *farg;
2692 enum event_type type;
2697 arg->type = PRINT_FUNC;
2698 arg->func.func = func;
2702 next_arg = &(arg->func.args);
2703 for (i = 0; i < func->nr_args; i++) {
2706 do_warning("%s: not enough memory!", __func__);
2710 type = process_arg(event, farg, &token);
2711 if (i < (func->nr_args - 1))
2716 if (test_type_token(type, token, EVENT_DELIM, test)) {
2723 next_arg = &(farg->next);
2727 type = read_token(&token);
2733 static enum event_type
2734 process_function(struct event_format *event, struct print_arg *arg,
2735 char *token, char **tok)
2737 struct pevent_function_handler *func;
2739 if (strcmp(token, "__print_flags") == 0) {
2742 return process_flags(event, arg, tok);
2744 if (strcmp(token, "__print_symbolic") == 0) {
2746 is_symbolic_field = 1;
2747 return process_symbols(event, arg, tok);
2749 if (strcmp(token, "__print_hex") == 0) {
2751 return process_hex(event, arg, tok);
2753 if (strcmp(token, "__get_str") == 0) {
2755 return process_str(event, arg, tok);
2757 if (strcmp(token, "__get_dynamic_array") == 0) {
2759 return process_dynamic_array(event, arg, tok);
2762 func = find_func_handler(event->pevent, token);
2765 return process_func_handler(event, func, arg, tok);
2768 do_warning("function %s not defined", token);
2773 static enum event_type
2774 process_arg_token(struct event_format *event, struct print_arg *arg,
2775 char **tok, enum event_type type)
2784 if (strcmp(token, "REC") == 0) {
2786 type = process_entry(event, arg, &token);
2790 /* test the next token */
2791 type = read_token_item(&token);
2794 * If the next token is a parenthesis, then this
2797 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2800 /* this will free atom. */
2801 type = process_function(event, arg, atom, &token);
2804 /* atoms can be more than one token long */
2805 while (type == EVENT_ITEM) {
2807 new_atom = realloc(atom,
2808 strlen(atom) + strlen(token) + 2);
2817 strcat(atom, token);
2819 type = read_token_item(&token);
2822 arg->type = PRINT_ATOM;
2823 arg->atom.atom = atom;
2828 arg->type = PRINT_ATOM;
2829 arg->atom.atom = token;
2830 type = read_token_item(&token);
2833 if (strcmp(token, "(") == 0) {
2835 type = process_paren(event, arg, &token);
2839 /* handle single ops */
2840 arg->type = PRINT_OP;
2842 arg->op.left = NULL;
2843 type = process_op(event, arg, &token);
2845 /* On error, the op is freed */
2846 if (type == EVENT_ERROR)
2849 /* return error type if errored */
2852 case EVENT_ERROR ... EVENT_NEWLINE:
2854 do_warning("unexpected type %d", type);
2862 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2864 enum event_type type = EVENT_ERROR;
2865 struct print_arg *arg;
2870 if (type == EVENT_NEWLINE) {
2871 type = read_token_item(&token);
2877 do_warning("%s: not enough memory!", __func__);
2881 type = process_arg(event, arg, &token);
2883 if (type == EVENT_ERROR) {
2892 if (type == EVENT_OP) {
2893 type = process_op(event, arg, &token);
2895 if (type == EVENT_ERROR) {
2904 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2911 } while (type != EVENT_NONE);
2913 if (type != EVENT_NONE && type != EVENT_ERROR)
2919 static int event_read_print(struct event_format *event)
2921 enum event_type type;
2925 if (read_expected_item(EVENT_ITEM, "print") < 0)
2928 if (read_expected(EVENT_ITEM, "fmt") < 0)
2931 if (read_expected(EVENT_OP, ":") < 0)
2934 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2938 event->print_fmt.format = token;
2939 event->print_fmt.args = NULL;
2941 /* ok to have no arg */
2942 type = read_token_item(&token);
2944 if (type == EVENT_NONE)
2947 /* Handle concatenation of print lines */
2948 if (type == EVENT_DQUOTE) {
2951 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2954 free_token(event->print_fmt.format);
2955 event->print_fmt.format = NULL;
2960 if (test_type_token(type, token, EVENT_DELIM, ","))
2965 ret = event_read_print_args(event, &event->print_fmt.args);
2977 * pevent_find_common_field - return a common field by event
2978 * @event: handle for the event
2979 * @name: the name of the common field to return
2981 * Returns a common field from the event by the given @name.
2982 * This only searchs the common fields and not all field.
2984 struct format_field *
2985 pevent_find_common_field(struct event_format *event, const char *name)
2987 struct format_field *format;
2989 for (format = event->format.common_fields;
2990 format; format = format->next) {
2991 if (strcmp(format->name, name) == 0)
2999 * pevent_find_field - find a non-common field
3000 * @event: handle for the event
3001 * @name: the name of the non-common field
3003 * Returns a non-common field by the given @name.
3004 * This does not search common fields.
3006 struct format_field *
3007 pevent_find_field(struct event_format *event, const char *name)
3009 struct format_field *format;
3011 for (format = event->format.fields;
3012 format; format = format->next) {
3013 if (strcmp(format->name, name) == 0)
3021 * pevent_find_any_field - find any field by name
3022 * @event: handle for the event
3023 * @name: the name of the field
3025 * Returns a field by the given @name.
3026 * This searchs the common field names first, then
3027 * the non-common ones if a common one was not found.
3029 struct format_field *
3030 pevent_find_any_field(struct event_format *event, const char *name)
3032 struct format_field *format;
3034 format = pevent_find_common_field(event, name);
3037 return pevent_find_field(event, name);
3041 * pevent_read_number - read a number from data
3042 * @pevent: handle for the pevent
3043 * @ptr: the raw data
3044 * @size: the size of the data that holds the number
3046 * Returns the number (converted to host) from the
3049 unsigned long long pevent_read_number(struct pevent *pevent,
3050 const void *ptr, int size)
3054 return *(unsigned char *)ptr;
3056 return data2host2(pevent, ptr);
3058 return data2host4(pevent, ptr);
3060 return data2host8(pevent, ptr);
3068 * pevent_read_number_field - read a number from data
3069 * @field: a handle to the field
3070 * @data: the raw data to read
3071 * @value: the value to place the number in
3073 * Reads raw data according to a field offset and size,
3074 * and translates it into @value.
3076 * Returns 0 on success, -1 otherwise.
3078 int pevent_read_number_field(struct format_field *field, const void *data,
3079 unsigned long long *value)
3083 switch (field->size) {
3088 *value = pevent_read_number(field->event->pevent,
3089 data + field->offset, field->size);
3096 static int get_common_info(struct pevent *pevent,
3097 const char *type, int *offset, int *size)
3099 struct event_format *event;
3100 struct format_field *field;
3103 * All events should have the same common elements.
3104 * Pick any event to find where the type is;
3106 if (!pevent->events) {
3107 do_warning("no event_list!");
3111 event = pevent->events[0];
3112 field = pevent_find_common_field(event, type);
3116 *offset = field->offset;
3117 *size = field->size;
3122 static int __parse_common(struct pevent *pevent, void *data,
3123 int *size, int *offset, const char *name)
3128 ret = get_common_info(pevent, name, offset, size);
3132 return pevent_read_number(pevent, data + *offset, *size);
3135 static int trace_parse_common_type(struct pevent *pevent, void *data)
3137 return __parse_common(pevent, data,
3138 &pevent->type_size, &pevent->type_offset,
3142 static int parse_common_pid(struct pevent *pevent, void *data)
3144 return __parse_common(pevent, data,
3145 &pevent->pid_size, &pevent->pid_offset,
3149 static int parse_common_pc(struct pevent *pevent, void *data)
3151 return __parse_common(pevent, data,
3152 &pevent->pc_size, &pevent->pc_offset,
3153 "common_preempt_count");
3156 static int parse_common_flags(struct pevent *pevent, void *data)
3158 return __parse_common(pevent, data,
3159 &pevent->flags_size, &pevent->flags_offset,
3163 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3165 return __parse_common(pevent, data,
3166 &pevent->ld_size, &pevent->ld_offset,
3167 "common_lock_depth");
3170 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3172 return __parse_common(pevent, data,
3173 &pevent->ld_size, &pevent->ld_offset,
3174 "common_migrate_disable");
3177 static int events_id_cmp(const void *a, const void *b);
3180 * pevent_find_event - find an event by given id
3181 * @pevent: a handle to the pevent
3182 * @id: the id of the event
3184 * Returns an event that has a given @id.
3186 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3188 struct event_format **eventptr;
3189 struct event_format key;
3190 struct event_format *pkey = &key;
3192 /* Check cache first */
3193 if (pevent->last_event && pevent->last_event->id == id)
3194 return pevent->last_event;
3198 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3199 sizeof(*pevent->events), events_id_cmp);
3202 pevent->last_event = *eventptr;
3210 * pevent_find_event_by_name - find an event by given name
3211 * @pevent: a handle to the pevent
3212 * @sys: the system name to search for
3213 * @name: the name of the event to search for
3215 * This returns an event with a given @name and under the system
3216 * @sys. If @sys is NULL the first event with @name is returned.
3218 struct event_format *
3219 pevent_find_event_by_name(struct pevent *pevent,
3220 const char *sys, const char *name)
3222 struct event_format *event;
3225 if (pevent->last_event &&
3226 strcmp(pevent->last_event->name, name) == 0 &&
3227 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3228 return pevent->last_event;
3230 for (i = 0; i < pevent->nr_events; i++) {
3231 event = pevent->events[i];
3232 if (strcmp(event->name, name) == 0) {
3235 if (strcmp(event->system, sys) == 0)
3239 if (i == pevent->nr_events)
3242 pevent->last_event = event;
3246 static unsigned long long
3247 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3249 struct pevent *pevent = event->pevent;
3250 unsigned long long val = 0;
3251 unsigned long long left, right;
3252 struct print_arg *typearg = NULL;
3253 struct print_arg *larg;
3254 unsigned long offset;
3255 unsigned int field_size;
3257 switch (arg->type) {
3262 return strtoull(arg->atom.atom, NULL, 0);
3264 if (!arg->field.field) {
3265 arg->field.field = pevent_find_any_field(event, arg->field.name);
3266 if (!arg->field.field)
3267 goto out_warning_field;
3270 /* must be a number */
3271 val = pevent_read_number(pevent, data + arg->field.field->offset,
3272 arg->field.field->size);
3279 val = eval_num_arg(data, size, event, arg->typecast.item);
3280 return eval_type(val, arg, 0);
3287 val = process_defined_func(&s, data, size, event, arg);
3288 trace_seq_destroy(&s);
3292 if (strcmp(arg->op.op, "[") == 0) {
3294 * Arrays are special, since we don't want
3295 * to read the arg as is.
3297 right = eval_num_arg(data, size, event, arg->op.right);
3299 /* handle typecasts */
3300 larg = arg->op.left;
3301 while (larg->type == PRINT_TYPE) {
3304 larg = larg->typecast.item;
3307 /* Default to long size */
3308 field_size = pevent->long_size;
3310 switch (larg->type) {
3311 case PRINT_DYNAMIC_ARRAY:
3312 offset = pevent_read_number(pevent,
3313 data + larg->dynarray.field->offset,
3314 larg->dynarray.field->size);
3315 if (larg->dynarray.field->elementsize)
3316 field_size = larg->dynarray.field->elementsize;
3318 * The actual length of the dynamic array is stored
3319 * in the top half of the field, and the offset
3320 * is in the bottom half of the 32 bit field.
3326 if (!larg->field.field) {
3328 pevent_find_any_field(event, larg->field.name);
3329 if (!larg->field.field) {
3331 goto out_warning_field;
3334 field_size = larg->field.field->elementsize;
3335 offset = larg->field.field->offset +
3336 right * larg->field.field->elementsize;
3339 goto default_op; /* oops, all bets off */
3341 val = pevent_read_number(pevent,
3342 data + offset, field_size);
3344 val = eval_type(val, typearg, 1);
3346 } else if (strcmp(arg->op.op, "?") == 0) {
3347 left = eval_num_arg(data, size, event, arg->op.left);
3348 arg = arg->op.right;
3350 val = eval_num_arg(data, size, event, arg->op.left);
3352 val = eval_num_arg(data, size, event, arg->op.right);
3356 left = eval_num_arg(data, size, event, arg->op.left);
3357 right = eval_num_arg(data, size, event, arg->op.right);
3358 switch (arg->op.op[0]) {
3360 switch (arg->op.op[1]) {
3365 val = left != right;
3368 goto out_warning_op;
3376 val = left || right;
3382 val = left && right;
3387 switch (arg->op.op[1]) {
3392 val = left << right;
3395 val = left <= right;
3398 goto out_warning_op;
3402 switch (arg->op.op[1]) {
3407 val = left >> right;
3410 val = left >= right;
3413 goto out_warning_op;
3417 if (arg->op.op[1] != '=')
3418 goto out_warning_op;
3420 val = left == right;
3435 goto out_warning_op;
3438 default: /* not sure what to do there */
3444 do_warning("%s: unknown op '%s'", __func__, arg->op.op);
3448 do_warning("%s: field %s not found", __func__, arg->field.name);
3454 unsigned long long value;
3457 static const struct flag flags[] = {
3458 { "HI_SOFTIRQ", 0 },
3459 { "TIMER_SOFTIRQ", 1 },
3460 { "NET_TX_SOFTIRQ", 2 },
3461 { "NET_RX_SOFTIRQ", 3 },
3462 { "BLOCK_SOFTIRQ", 4 },
3463 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3464 { "TASKLET_SOFTIRQ", 6 },
3465 { "SCHED_SOFTIRQ", 7 },
3466 { "HRTIMER_SOFTIRQ", 8 },
3467 { "RCU_SOFTIRQ", 9 },
3469 { "HRTIMER_NORESTART", 0 },
3470 { "HRTIMER_RESTART", 1 },
3473 static unsigned long long eval_flag(const char *flag)
3478 * Some flags in the format files do not get converted.
3479 * If the flag is not numeric, see if it is something that
3480 * we already know about.
3482 if (isdigit(flag[0]))
3483 return strtoull(flag, NULL, 0);
3485 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3486 if (strcmp(flags[i].name, flag) == 0)
3487 return flags[i].value;
3492 static void print_str_to_seq(struct trace_seq *s, const char *format,
3493 int len_arg, const char *str)
3496 trace_seq_printf(s, format, len_arg, str);
3498 trace_seq_printf(s, format, str);
3501 static void print_str_arg(struct trace_seq *s, void *data, int size,
3502 struct event_format *event, const char *format,
3503 int len_arg, struct print_arg *arg)
3505 struct pevent *pevent = event->pevent;
3506 struct print_flag_sym *flag;
3507 struct format_field *field;
3508 struct printk_map *printk;
3509 unsigned long long val, fval;
3516 switch (arg->type) {
3521 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3524 field = arg->field.field;
3526 field = pevent_find_any_field(event, arg->field.name);
3528 str = arg->field.name;
3529 goto out_warning_field;
3531 arg->field.field = field;
3533 /* Zero sized fields, mean the rest of the data */
3534 len = field->size ? : size - field->offset;
3537 * Some events pass in pointers. If this is not an array
3538 * and the size is the same as long_size, assume that it
3541 if (!(field->flags & FIELD_IS_ARRAY) &&
3542 field->size == pevent->long_size) {
3543 addr = *(unsigned long *)(data + field->offset);
3544 /* Check if it matches a print format */
3545 printk = find_printk(pevent, addr);
3547 trace_seq_puts(s, printk->printk);
3549 trace_seq_printf(s, "%lx", addr);
3552 str = malloc(len + 1);
3554 do_warning("%s: not enough memory!", __func__);
3557 memcpy(str, data + field->offset, len);
3559 print_str_to_seq(s, format, len_arg, str);
3563 val = eval_num_arg(data, size, event, arg->flags.field);
3565 for (flag = arg->flags.flags; flag; flag = flag->next) {
3566 fval = eval_flag(flag->value);
3567 if (!val && !fval) {
3568 print_str_to_seq(s, format, len_arg, flag->str);
3571 if (fval && (val & fval) == fval) {
3572 if (print && arg->flags.delim)
3573 trace_seq_puts(s, arg->flags.delim);
3574 print_str_to_seq(s, format, len_arg, flag->str);
3581 val = eval_num_arg(data, size, event, arg->symbol.field);
3582 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3583 fval = eval_flag(flag->value);
3585 print_str_to_seq(s, format, len_arg, flag->str);
3591 field = arg->hex.field->field.field;
3593 str = arg->hex.field->field.name;
3594 field = pevent_find_any_field(event, str);
3596 goto out_warning_field;
3597 arg->hex.field->field.field = field;
3599 hex = data + field->offset;
3600 len = eval_num_arg(data, size, event, arg->hex.size);
3601 for (i = 0; i < len; i++) {
3603 trace_seq_putc(s, ' ');
3604 trace_seq_printf(s, "%02x", hex[i]);
3610 case PRINT_STRING: {
3613 if (arg->string.offset == -1) {
3614 struct format_field *f;
3616 f = pevent_find_any_field(event, arg->string.string);
3617 arg->string.offset = f->offset;
3619 str_offset = data2host4(pevent, data + arg->string.offset);
3620 str_offset &= 0xffff;
3621 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3625 print_str_to_seq(s, format, len_arg, arg->string.string);
3629 * The only op for string should be ? :
3631 if (arg->op.op[0] != '?')
3633 val = eval_num_arg(data, size, event, arg->op.left);
3635 print_str_arg(s, data, size, event,
3636 format, len_arg, arg->op.right->op.left);
3638 print_str_arg(s, data, size, event,
3639 format, len_arg, arg->op.right->op.right);
3642 process_defined_func(s, data, size, event, arg);
3652 do_warning("%s: field %s not found", __func__, arg->field.name);
3655 static unsigned long long
3656 process_defined_func(struct trace_seq *s, void *data, int size,
3657 struct event_format *event, struct print_arg *arg)
3659 struct pevent_function_handler *func_handle = arg->func.func;
3660 struct pevent_func_params *param;
3661 unsigned long long *args;
3662 unsigned long long ret;
3663 struct print_arg *farg;
3664 struct trace_seq str;
3666 struct save_str *next;
3668 } *strings = NULL, *string;
3671 if (!func_handle->nr_args) {
3672 ret = (*func_handle->func)(s, NULL);
3676 farg = arg->func.args;
3677 param = func_handle->params;
3680 args = malloc(sizeof(*args) * func_handle->nr_args);
3684 for (i = 0; i < func_handle->nr_args; i++) {
3685 switch (param->type) {
3686 case PEVENT_FUNC_ARG_INT:
3687 case PEVENT_FUNC_ARG_LONG:
3688 case PEVENT_FUNC_ARG_PTR:
3689 args[i] = eval_num_arg(data, size, event, farg);
3691 case PEVENT_FUNC_ARG_STRING:
3692 trace_seq_init(&str);
3693 print_str_arg(&str, data, size, event, "%s", -1, farg);
3694 trace_seq_terminate(&str);
3695 string = malloc(sizeof(*string));
3697 do_warning("%s(%d): malloc str", __func__, __LINE__);
3700 string->next = strings;
3701 string->str = strdup(str.buffer);
3704 do_warning("%s(%d): malloc str", __func__, __LINE__);
3707 args[i] = (uintptr_t)string->str;
3709 trace_seq_destroy(&str);
3713 * Something went totally wrong, this is not
3714 * an input error, something in this code broke.
3716 do_warning("Unexpected end of arguments\n");
3720 param = param->next;
3723 ret = (*func_handle->func)(s, args);
3728 strings = string->next;
3734 /* TBD : handle return type here */
3738 static void free_args(struct print_arg *args)
3740 struct print_arg *next;
3750 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3752 struct pevent *pevent = event->pevent;
3753 struct format_field *field, *ip_field;
3754 struct print_arg *args, *arg, **next;
3755 unsigned long long ip, val;
3760 field = pevent->bprint_buf_field;
3761 ip_field = pevent->bprint_ip_field;
3764 field = pevent_find_field(event, "buf");
3766 do_warning("can't find buffer field for binary printk");
3769 ip_field = pevent_find_field(event, "ip");
3771 do_warning("can't find ip field for binary printk");
3774 pevent->bprint_buf_field = field;
3775 pevent->bprint_ip_field = ip_field;
3778 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3781 * The first arg is the IP pointer.
3785 do_warning("%s(%d): not enough memory!", __func__, __LINE__);
3792 arg->type = PRINT_ATOM;
3794 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3797 /* skip the first "%pf : " */
3798 for (ptr = fmt + 6, bptr = data + field->offset;
3799 bptr < data + size && *ptr; ptr++) {
3830 vsize = pevent->long_size;
3844 /* the pointers are always 4 bytes aligned */
3845 bptr = (void *)(((unsigned long)bptr + 3) &
3847 val = pevent_read_number(pevent, bptr, vsize);
3851 do_warning("%s(%d): not enough memory!",
3852 __func__, __LINE__);
3856 arg->type = PRINT_ATOM;
3857 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3864 * The '*' case means that an arg is used as the length.
3865 * We need to continue to figure out for what.
3874 do_warning("%s(%d): not enough memory!",
3875 __func__, __LINE__);
3879 arg->type = PRINT_BSTRING;
3880 arg->string.string = strdup(bptr);
3881 if (!arg->string.string)
3883 bptr += strlen(bptr) + 1;
3900 get_bprint_format(void *data, int size __maybe_unused,
3901 struct event_format *event)
3903 struct pevent *pevent = event->pevent;
3904 unsigned long long addr;
3905 struct format_field *field;
3906 struct printk_map *printk;
3909 field = pevent->bprint_fmt_field;
3912 field = pevent_find_field(event, "fmt");
3914 do_warning("can't find format field for binary printk");
3917 pevent->bprint_fmt_field = field;
3920 addr = pevent_read_number(pevent, data + field->offset, field->size);
3922 printk = find_printk(pevent, addr);
3924 if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0)
3929 if (asprintf(&format, "%s : %s", "%pf", printk->printk) < 0)
3935 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3936 struct event_format *event, struct print_arg *arg)
3939 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3941 if (arg->type == PRINT_FUNC) {
3942 process_defined_func(s, data, size, event, arg);
3946 if (arg->type != PRINT_FIELD) {
3947 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3953 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3954 if (!arg->field.field) {
3956 pevent_find_any_field(event, arg->field.name);
3957 if (!arg->field.field) {
3958 do_warning("%s: field %s not found",
3959 __func__, arg->field.name);
3963 if (arg->field.field->size != 6) {
3964 trace_seq_printf(s, "INVALIDMAC");
3967 buf = data + arg->field.field->offset;
3968 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3971 static int is_printable_array(char *p, unsigned int len)
3975 for (i = 0; i < len && p[i]; i++)
3981 static void print_event_fields(struct trace_seq *s, void *data,
3982 int size __maybe_unused,
3983 struct event_format *event)
3985 struct format_field *field;
3986 unsigned long long val;
3987 unsigned int offset, len, i;
3989 field = event->format.fields;
3991 trace_seq_printf(s, " %s=", field->name);
3992 if (field->flags & FIELD_IS_ARRAY) {
3993 offset = field->offset;
3995 if (field->flags & FIELD_IS_DYNAMIC) {
3996 val = pevent_read_number(event->pevent, data + offset, len);
4001 if (field->flags & FIELD_IS_STRING &&
4002 is_printable_array(data + offset, len)) {
4003 trace_seq_printf(s, "%s", (char *)data + offset);
4005 trace_seq_puts(s, "ARRAY[");
4006 for (i = 0; i < len; i++) {
4008 trace_seq_puts(s, ", ");
4009 trace_seq_printf(s, "%02x",
4010 *((unsigned char *)data + offset + i));
4012 trace_seq_putc(s, ']');
4013 field->flags &= ~FIELD_IS_STRING;
4016 val = pevent_read_number(event->pevent, data + field->offset,
4018 if (field->flags & FIELD_IS_POINTER) {
4019 trace_seq_printf(s, "0x%llx", val);
4020 } else if (field->flags & FIELD_IS_SIGNED) {
4021 switch (field->size) {
4024 * If field is long then print it in hex.
4025 * A long usually stores pointers.
4027 if (field->flags & FIELD_IS_LONG)
4028 trace_seq_printf(s, "0x%x", (int)val);
4030 trace_seq_printf(s, "%d", (int)val);
4033 trace_seq_printf(s, "%2d", (short)val);
4036 trace_seq_printf(s, "%1d", (char)val);
4039 trace_seq_printf(s, "%lld", val);
4042 if (field->flags & FIELD_IS_LONG)
4043 trace_seq_printf(s, "0x%llx", val);
4045 trace_seq_printf(s, "%llu", val);
4048 field = field->next;
4052 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4054 struct pevent *pevent = event->pevent;
4055 struct print_fmt *print_fmt = &event->print_fmt;
4056 struct print_arg *arg = print_fmt->args;
4057 struct print_arg *args = NULL;
4058 const char *ptr = print_fmt->format;
4059 unsigned long long val;
4060 struct func_map *func;
4061 const char *saveptr;
4062 char *bprint_fmt = NULL;
4070 if (event->flags & EVENT_FL_FAILED) {
4071 trace_seq_printf(s, "[FAILED TO PARSE]");
4072 print_event_fields(s, data, size, event);
4076 if (event->flags & EVENT_FL_ISBPRINT) {
4077 bprint_fmt = get_bprint_format(data, size, event);
4078 args = make_bprint_args(bprint_fmt, data, size, event);
4083 for (; *ptr; ptr++) {
4089 trace_seq_putc(s, '\n');
4092 trace_seq_putc(s, '\t');
4095 trace_seq_putc(s, '\r');
4098 trace_seq_putc(s, '\\');
4101 trace_seq_putc(s, *ptr);
4105 } else if (*ptr == '%') {
4113 trace_seq_putc(s, '%');
4116 /* FIXME: need to handle properly */
4128 /* The argument is the length. */
4130 do_warning("no argument match");
4131 event->flags |= EVENT_FL_FAILED;
4134 len_arg = eval_num_arg(data, size, event, arg);
4144 if (pevent->long_size == 4)
4149 if (*(ptr+1) == 'F' ||
4153 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4154 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4167 do_warning("no argument match");
4168 event->flags |= EVENT_FL_FAILED;
4172 len = ((unsigned long)ptr + 1) -
4173 (unsigned long)saveptr;
4175 /* should never happen */
4177 do_warning("bad format!");
4178 event->flags |= EVENT_FL_FAILED;
4182 memcpy(format, saveptr, len);
4185 val = eval_num_arg(data, size, event, arg);
4189 func = find_func(pevent, val);
4191 trace_seq_puts(s, func->func);
4192 if (show_func == 'F')
4199 if (pevent->long_size == 8 && ls &&
4200 sizeof(long) != 8) {
4204 /* make %l into %ll */
4205 p = strchr(format, 'l');
4207 memmove(p+1, p, strlen(p)+1);
4208 else if (strcmp(format, "%p") == 0)
4209 strcpy(format, "0x%llx");
4214 trace_seq_printf(s, format, len_arg, (char)val);
4216 trace_seq_printf(s, format, (char)val);
4220 trace_seq_printf(s, format, len_arg, (short)val);
4222 trace_seq_printf(s, format, (short)val);
4226 trace_seq_printf(s, format, len_arg, (int)val);
4228 trace_seq_printf(s, format, (int)val);
4232 trace_seq_printf(s, format, len_arg, (long)val);
4234 trace_seq_printf(s, format, (long)val);
4238 trace_seq_printf(s, format, len_arg,
4241 trace_seq_printf(s, format, (long long)val);
4244 do_warning("bad count (%d)", ls);
4245 event->flags |= EVENT_FL_FAILED;
4250 do_warning("no matching argument");
4251 event->flags |= EVENT_FL_FAILED;
4255 len = ((unsigned long)ptr + 1) -
4256 (unsigned long)saveptr;
4258 /* should never happen */
4260 do_warning("bad format!");
4261 event->flags |= EVENT_FL_FAILED;
4265 memcpy(format, saveptr, len);
4269 print_str_arg(s, data, size, event,
4270 format, len_arg, arg);
4274 trace_seq_printf(s, ">%c<", *ptr);
4278 trace_seq_putc(s, *ptr);
4281 if (event->flags & EVENT_FL_FAILED) {
4283 trace_seq_printf(s, "[FAILED TO PARSE]");
4293 * pevent_data_lat_fmt - parse the data for the latency format
4294 * @pevent: a handle to the pevent
4295 * @s: the trace_seq to write to
4296 * @record: the record to read from
4298 * This parses out the Latency format (interrupts disabled,
4299 * need rescheduling, in hard/soft interrupt, preempt count
4300 * and lock depth) and places it into the trace_seq.
4302 void pevent_data_lat_fmt(struct pevent *pevent,
4303 struct trace_seq *s, struct pevent_record *record)
4305 static int check_lock_depth = 1;
4306 static int check_migrate_disable = 1;
4307 static int lock_depth_exists;
4308 static int migrate_disable_exists;
4309 unsigned int lat_flags;
4312 int migrate_disable;
4315 void *data = record->data;
4317 lat_flags = parse_common_flags(pevent, data);
4318 pc = parse_common_pc(pevent, data);
4319 /* lock_depth may not always exist */
4320 if (lock_depth_exists)
4321 lock_depth = parse_common_lock_depth(pevent, data);
4322 else if (check_lock_depth) {
4323 lock_depth = parse_common_lock_depth(pevent, data);
4325 check_lock_depth = 0;
4327 lock_depth_exists = 1;
4330 /* migrate_disable may not always exist */
4331 if (migrate_disable_exists)
4332 migrate_disable = parse_common_migrate_disable(pevent, data);
4333 else if (check_migrate_disable) {
4334 migrate_disable = parse_common_migrate_disable(pevent, data);
4335 if (migrate_disable < 0)
4336 check_migrate_disable = 0;
4338 migrate_disable_exists = 1;
4341 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4342 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4344 trace_seq_printf(s, "%c%c%c",
4345 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4346 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4348 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4350 (hardirq && softirq) ? 'H' :
4351 hardirq ? 'h' : softirq ? 's' : '.');
4354 trace_seq_printf(s, "%x", pc);
4356 trace_seq_putc(s, '.');
4358 if (migrate_disable_exists) {
4359 if (migrate_disable < 0)
4360 trace_seq_putc(s, '.');
4362 trace_seq_printf(s, "%d", migrate_disable);
4365 if (lock_depth_exists) {
4367 trace_seq_putc(s, '.');
4369 trace_seq_printf(s, "%d", lock_depth);
4372 trace_seq_terminate(s);
4376 * pevent_data_type - parse out the given event type
4377 * @pevent: a handle to the pevent
4378 * @rec: the record to read from
4380 * This returns the event id from the @rec.
4382 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4384 return trace_parse_common_type(pevent, rec->data);
4388 * pevent_data_event_from_type - find the event by a given type
4389 * @pevent: a handle to the pevent
4390 * @type: the type of the event.
4392 * This returns the event form a given @type;
4394 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4396 return pevent_find_event(pevent, type);
4400 * pevent_data_pid - parse the PID from raw data
4401 * @pevent: a handle to the pevent
4402 * @rec: the record to parse
4404 * This returns the PID from a raw data.
4406 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4408 return parse_common_pid(pevent, rec->data);
4412 * pevent_data_comm_from_pid - return the command line from PID
4413 * @pevent: a handle to the pevent
4414 * @pid: the PID of the task to search for
4416 * This returns a pointer to the command line that has the given
4419 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4423 comm = find_cmdline(pevent, pid);
4428 * pevent_data_comm_from_pid - parse the data into the print format
4429 * @s: the trace_seq to write to
4430 * @event: the handle to the event
4431 * @record: the record to read from
4433 * This parses the raw @data using the given @event information and
4434 * writes the print format into the trace_seq.
4436 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4437 struct pevent_record *record)
4439 int print_pretty = 1;
4441 if (event->pevent->print_raw)
4442 print_event_fields(s, record->data, record->size, event);
4446 print_pretty = event->handler(s, record, event,
4450 pretty_print(s, record->data, record->size, event);
4453 trace_seq_terminate(s);
4456 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4458 if (!use_trace_clock)
4461 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4462 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4465 /* trace_clock is setting in tsc or counter mode */
4469 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4470 struct pevent_record *record, bool use_trace_clock)
4472 static const char *spaces = " "; /* 20 spaces */
4473 struct event_format *event;
4475 unsigned long usecs;
4476 unsigned long nsecs;
4478 void *data = record->data;
4483 bool use_usec_format;
4485 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4487 if (use_usec_format) {
4488 secs = record->ts / NSECS_PER_SEC;
4489 nsecs = record->ts - secs * NSECS_PER_SEC;
4492 if (record->size < 0) {
4493 do_warning("ug! negative record size %d", record->size);
4497 type = trace_parse_common_type(pevent, data);
4499 event = pevent_find_event(pevent, type);
4501 do_warning("ug! no event found for type %d", type);
4505 pid = parse_common_pid(pevent, data);
4506 comm = find_cmdline(pevent, pid);
4508 if (pevent->latency_format) {
4509 trace_seq_printf(s, "%8.8s-%-5d %3d",
4510 comm, pid, record->cpu);
4511 pevent_data_lat_fmt(pevent, s, record);
4513 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4515 if (use_usec_format) {
4516 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4520 usecs = (nsecs + 500) / NSECS_PER_USEC;
4524 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4525 secs, p, usecs, event->name);
4527 trace_seq_printf(s, " %12llu: %s: ",
4528 record->ts, event->name);
4530 /* Space out the event names evenly. */
4531 len = strlen(event->name);
4533 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4535 pevent_event_info(s, event, record);
4538 static int events_id_cmp(const void *a, const void *b)
4540 struct event_format * const * ea = a;
4541 struct event_format * const * eb = b;
4543 if ((*ea)->id < (*eb)->id)
4546 if ((*ea)->id > (*eb)->id)
4552 static int events_name_cmp(const void *a, const void *b)
4554 struct event_format * const * ea = a;
4555 struct event_format * const * eb = b;
4558 res = strcmp((*ea)->name, (*eb)->name);
4562 res = strcmp((*ea)->system, (*eb)->system);
4566 return events_id_cmp(a, b);
4569 static int events_system_cmp(const void *a, const void *b)
4571 struct event_format * const * ea = a;
4572 struct event_format * const * eb = b;
4575 res = strcmp((*ea)->system, (*eb)->system);
4579 res = strcmp((*ea)->name, (*eb)->name);
4583 return events_id_cmp(a, b);
4586 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4588 struct event_format **events;
4589 int (*sort)(const void *a, const void *b);
4591 events = pevent->sort_events;
4593 if (events && pevent->last_type == sort_type)
4597 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4601 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4602 events[pevent->nr_events] = NULL;
4604 pevent->sort_events = events;
4606 /* the internal events are sorted by id */
4607 if (sort_type == EVENT_SORT_ID) {
4608 pevent->last_type = sort_type;
4613 switch (sort_type) {
4615 sort = events_id_cmp;
4617 case EVENT_SORT_NAME:
4618 sort = events_name_cmp;
4620 case EVENT_SORT_SYSTEM:
4621 sort = events_system_cmp;
4627 qsort(events, pevent->nr_events, sizeof(*events), sort);
4628 pevent->last_type = sort_type;
4633 static struct format_field **
4634 get_event_fields(const char *type, const char *name,
4635 int count, struct format_field *list)
4637 struct format_field **fields;
4638 struct format_field *field;
4641 fields = malloc(sizeof(*fields) * (count + 1));
4645 for (field = list; field; field = field->next) {
4646 fields[i++] = field;
4647 if (i == count + 1) {
4648 do_warning("event %s has more %s fields than specified",
4656 do_warning("event %s has less %s fields than specified",
4665 * pevent_event_common_fields - return a list of common fields for an event
4666 * @event: the event to return the common fields of.
4668 * Returns an allocated array of fields. The last item in the array is NULL.
4669 * The array must be freed with free().
4671 struct format_field **pevent_event_common_fields(struct event_format *event)
4673 return get_event_fields("common", event->name,
4674 event->format.nr_common,
4675 event->format.common_fields);
4679 * pevent_event_fields - return a list of event specific fields for an event
4680 * @event: the event to return the fields of.
4682 * Returns an allocated array of fields. The last item in the array is NULL.
4683 * The array must be freed with free().
4685 struct format_field **pevent_event_fields(struct event_format *event)
4687 return get_event_fields("event", event->name,
4688 event->format.nr_fields,
4689 event->format.fields);
4692 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4694 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4696 trace_seq_puts(s, ", ");
4697 print_fields(s, field->next);
4702 static void print_args(struct print_arg *args)
4704 int print_paren = 1;
4707 switch (args->type) {
4712 printf("%s", args->atom.atom);
4715 printf("REC->%s", args->field.name);
4718 printf("__print_flags(");
4719 print_args(args->flags.field);
4720 printf(", %s, ", args->flags.delim);
4722 print_fields(&s, args->flags.flags);
4723 trace_seq_do_printf(&s);
4724 trace_seq_destroy(&s);
4728 printf("__print_symbolic(");
4729 print_args(args->symbol.field);
4732 print_fields(&s, args->symbol.symbols);
4733 trace_seq_do_printf(&s);
4734 trace_seq_destroy(&s);
4738 printf("__print_hex(");
4739 print_args(args->hex.field);
4741 print_args(args->hex.size);
4746 printf("__get_str(%s)", args->string.string);
4749 printf("(%s)", args->typecast.type);
4750 print_args(args->typecast.item);
4753 if (strcmp(args->op.op, ":") == 0)
4757 print_args(args->op.left);
4758 printf(" %s ", args->op.op);
4759 print_args(args->op.right);
4764 /* we should warn... */
4769 print_args(args->next);
4773 static void parse_header_field(const char *field,
4774 int *offset, int *size, int mandatory)
4776 unsigned long long save_input_buf_ptr;
4777 unsigned long long save_input_buf_siz;
4781 save_input_buf_ptr = input_buf_ptr;
4782 save_input_buf_siz = input_buf_siz;
4784 if (read_expected(EVENT_ITEM, "field") < 0)
4786 if (read_expected(EVENT_OP, ":") < 0)
4790 if (read_expect_type(EVENT_ITEM, &token) < 0)
4795 * If this is not a mandatory field, then test it first.
4798 if (read_expected(EVENT_ITEM, field) < 0)
4801 if (read_expect_type(EVENT_ITEM, &token) < 0)
4803 if (strcmp(token, field) != 0)
4808 if (read_expected(EVENT_OP, ";") < 0)
4810 if (read_expected(EVENT_ITEM, "offset") < 0)
4812 if (read_expected(EVENT_OP, ":") < 0)
4814 if (read_expect_type(EVENT_ITEM, &token) < 0)
4816 *offset = atoi(token);
4818 if (read_expected(EVENT_OP, ";") < 0)
4820 if (read_expected(EVENT_ITEM, "size") < 0)
4822 if (read_expected(EVENT_OP, ":") < 0)
4824 if (read_expect_type(EVENT_ITEM, &token) < 0)
4826 *size = atoi(token);
4828 if (read_expected(EVENT_OP, ";") < 0)
4830 type = read_token(&token);
4831 if (type != EVENT_NEWLINE) {
4832 /* newer versions of the kernel have a "signed" type */
4833 if (type != EVENT_ITEM)
4836 if (strcmp(token, "signed") != 0)
4841 if (read_expected(EVENT_OP, ":") < 0)
4844 if (read_expect_type(EVENT_ITEM, &token))
4848 if (read_expected(EVENT_OP, ";") < 0)
4851 if (read_expect_type(EVENT_NEWLINE, &token))
4859 input_buf_ptr = save_input_buf_ptr;
4860 input_buf_siz = save_input_buf_siz;
4867 * pevent_parse_header_page - parse the data stored in the header page
4868 * @pevent: the handle to the pevent
4869 * @buf: the buffer storing the header page format string
4870 * @size: the size of @buf
4871 * @long_size: the long size to use if there is no header
4873 * This parses the header page format for information on the
4874 * ring buffer used. The @buf should be copied from
4876 * /sys/kernel/debug/tracing/events/header_page
4878 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4885 * Old kernels did not have header page info.
4886 * Sorry but we just use what we find here in user space.
4888 pevent->header_page_ts_size = sizeof(long long);
4889 pevent->header_page_size_size = long_size;
4890 pevent->header_page_data_offset = sizeof(long long) + long_size;
4891 pevent->old_format = 1;
4894 init_input_buf(buf, size);
4896 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4897 &pevent->header_page_ts_size, 1);
4898 parse_header_field("commit", &pevent->header_page_size_offset,
4899 &pevent->header_page_size_size, 1);
4900 parse_header_field("overwrite", &pevent->header_page_overwrite,
4902 parse_header_field("data", &pevent->header_page_data_offset,
4903 &pevent->header_page_data_size, 1);
4908 static int event_matches(struct event_format *event,
4909 int id, const char *sys_name,
4910 const char *event_name)
4912 if (id >= 0 && id != event->id)
4915 if (event_name && (strcmp(event_name, event->name) != 0))
4918 if (sys_name && (strcmp(sys_name, event->system) != 0))
4924 static void free_handler(struct event_handler *handle)
4926 free((void *)handle->sys_name);
4927 free((void *)handle->event_name);
4931 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4933 struct event_handler *handle, **next;
4935 for (next = &pevent->handlers; *next;
4936 next = &(*next)->next) {
4938 if (event_matches(event, handle->id,
4940 handle->event_name))
4947 pr_stat("overriding event (%d) %s:%s with new print handler",
4948 event->id, event->system, event->name);
4950 event->handler = handle->func;
4951 event->context = handle->context;
4953 *next = handle->next;
4954 free_handler(handle);
4960 * __pevent_parse_format - parse the event format
4961 * @buf: the buffer storing the event format string
4962 * @size: the size of @buf
4963 * @sys: the system the event belongs to
4965 * This parses the event format and creates an event structure
4966 * to quickly parse raw data for a given event.
4968 * These files currently come from:
4970 * /sys/kernel/debug/tracing/events/.../.../format
4972 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
4973 struct pevent *pevent, const char *buf,
4974 unsigned long size, const char *sys)
4976 struct event_format *event;
4979 init_input_buf(buf, size);
4981 *eventp = event = alloc_event();
4983 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
4985 event->name = event_read_name();
4988 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
4989 goto event_alloc_failed;
4992 if (strcmp(sys, "ftrace") == 0) {
4993 event->flags |= EVENT_FL_ISFTRACE;
4995 if (strcmp(event->name, "bprint") == 0)
4996 event->flags |= EVENT_FL_ISBPRINT;
4999 event->id = event_read_id();
5000 if (event->id < 0) {
5001 ret = PEVENT_ERRNO__READ_ID_FAILED;
5003 * This isn't an allocation error actually.
5004 * But as the ID is critical, just bail out.
5006 goto event_alloc_failed;
5009 event->system = strdup(sys);
5010 if (!event->system) {
5011 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5012 goto event_alloc_failed;
5015 /* Add pevent to event so that it can be referenced */
5016 event->pevent = pevent;
5018 ret = event_read_format(event);
5020 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5021 goto event_parse_failed;
5025 * If the event has an override, don't print warnings if the event
5026 * print format fails to parse.
5028 if (pevent && find_event_handle(pevent, event))
5031 ret = event_read_print(event);
5035 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5036 goto event_parse_failed;
5039 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5040 struct format_field *field;
5041 struct print_arg *arg, **list;
5043 /* old ftrace had no args */
5044 list = &event->print_fmt.args;
5045 for (field = event->format.fields; field; field = field->next) {
5048 event->flags |= EVENT_FL_FAILED;
5049 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5051 arg->type = PRINT_FIELD;
5052 arg->field.name = strdup(field->name);
5053 if (!arg->field.name) {
5054 event->flags |= EVENT_FL_FAILED;
5056 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5058 arg->field.field = field;
5068 event->flags |= EVENT_FL_FAILED;
5072 free(event->system);
5080 * pevent_parse_format - parse the event format
5081 * @buf: the buffer storing the event format string
5082 * @size: the size of @buf
5083 * @sys: the system the event belongs to
5085 * This parses the event format and creates an event structure
5086 * to quickly parse raw data for a given event.
5088 * These files currently come from:
5090 * /sys/kernel/debug/tracing/events/.../.../format
5092 enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf,
5093 unsigned long size, const char *sys)
5095 return __pevent_parse_format(eventp, NULL, buf, size, sys);
5099 * pevent_parse_event - parse the event format
5100 * @pevent: the handle to the pevent
5101 * @buf: the buffer storing the event format string
5102 * @size: the size of @buf
5103 * @sys: the system the event belongs to
5105 * This parses the event format and creates an event structure
5106 * to quickly parse raw data for a given event.
5108 * These files currently come from:
5110 * /sys/kernel/debug/tracing/events/.../.../format
5112 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5113 unsigned long size, const char *sys)
5115 struct event_format *event = NULL;
5116 int ret = __pevent_parse_format(&event, pevent, buf, size, sys);
5121 if (add_event(pevent, event)) {
5122 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5123 goto event_add_failed;
5126 #define PRINT_ARGS 0
5127 if (PRINT_ARGS && event->print_fmt.args)
5128 print_args(event->print_fmt.args);
5133 pevent_free_format(event);
5138 #define _PE(code, str) str
5139 static const char * const pevent_error_str[] = {
5144 int pevent_strerror(struct pevent *pevent __maybe_unused,
5145 enum pevent_errno errnum, char *buf, size_t buflen)
5151 msg = strerror_r(errnum, buf, buflen);
5153 size_t len = strlen(msg);
5154 memcpy(buf, msg, min(buflen - 1, len));
5155 *(buf + min(buflen - 1, len)) = '\0';
5160 if (errnum <= __PEVENT_ERRNO__START ||
5161 errnum >= __PEVENT_ERRNO__END)
5164 idx = errnum - __PEVENT_ERRNO__START - 1;
5165 msg = pevent_error_str[idx];
5168 case PEVENT_ERRNO__MEM_ALLOC_FAILED:
5169 case PEVENT_ERRNO__PARSE_EVENT_FAILED:
5170 case PEVENT_ERRNO__READ_ID_FAILED:
5171 case PEVENT_ERRNO__READ_FORMAT_FAILED:
5172 case PEVENT_ERRNO__READ_PRINT_FAILED:
5173 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
5174 case PEVENT_ERRNO__INVALID_ARG_TYPE:
5175 snprintf(buf, buflen, "%s", msg);
5179 /* cannot reach here */
5186 int get_field_val(struct trace_seq *s, struct format_field *field,
5187 const char *name, struct pevent_record *record,
5188 unsigned long long *val, int err)
5192 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5196 if (pevent_read_number_field(field, record->data, val)) {
5198 trace_seq_printf(s, " %s=INVALID", name);
5206 * pevent_get_field_raw - return the raw pointer into the data field
5207 * @s: The seq to print to on error
5208 * @event: the event that the field is for
5209 * @name: The name of the field
5210 * @record: The record with the field name.
5211 * @len: place to store the field length.
5212 * @err: print default error if failed.
5214 * Returns a pointer into record->data of the field and places
5215 * the length of the field in @len.
5217 * On failure, it returns NULL.
5219 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5220 const char *name, struct pevent_record *record,
5223 struct format_field *field;
5224 void *data = record->data;
5231 field = pevent_find_field(event, name);
5235 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5239 /* Allow @len to be NULL */
5243 offset = field->offset;
5244 if (field->flags & FIELD_IS_DYNAMIC) {
5245 offset = pevent_read_number(event->pevent,
5246 data + offset, field->size);
5247 *len = offset >> 16;
5252 return data + offset;
5256 * pevent_get_field_val - find a field and return its value
5257 * @s: The seq to print to on error
5258 * @event: the event that the field is for
5259 * @name: The name of the field
5260 * @record: The record with the field name.
5261 * @val: place to store the value of the field.
5262 * @err: print default error if failed.
5264 * Returns 0 on success -1 on field not found.
5266 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5267 const char *name, struct pevent_record *record,
5268 unsigned long long *val, int err)
5270 struct format_field *field;
5275 field = pevent_find_field(event, name);
5277 return get_field_val(s, field, name, record, val, err);
5281 * pevent_get_common_field_val - find a common field and return its value
5282 * @s: The seq to print to on error
5283 * @event: the event that the field is for
5284 * @name: The name of the field
5285 * @record: The record with the field name.
5286 * @val: place to store the value of the field.
5287 * @err: print default error if failed.
5289 * Returns 0 on success -1 on field not found.
5291 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5292 const char *name, struct pevent_record *record,
5293 unsigned long long *val, int err)
5295 struct format_field *field;
5300 field = pevent_find_common_field(event, name);
5302 return get_field_val(s, field, name, record, val, err);
5306 * pevent_get_any_field_val - find a any field and return its value
5307 * @s: The seq to print to on error
5308 * @event: the event that the field is for
5309 * @name: The name of the field
5310 * @record: The record with the field name.
5311 * @val: place to store the value of the field.
5312 * @err: print default error if failed.
5314 * Returns 0 on success -1 on field not found.
5316 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5317 const char *name, struct pevent_record *record,
5318 unsigned long long *val, int err)
5320 struct format_field *field;
5325 field = pevent_find_any_field(event, name);
5327 return get_field_val(s, field, name, record, val, err);
5331 * pevent_print_num_field - print a field and a format
5332 * @s: The seq to print to
5333 * @fmt: The printf format to print the field with.
5334 * @event: the event that the field is for
5335 * @name: The name of the field
5336 * @record: The record with the field name.
5337 * @err: print default error if failed.
5339 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5341 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5342 struct event_format *event, const char *name,
5343 struct pevent_record *record, int err)
5345 struct format_field *field = pevent_find_field(event, name);
5346 unsigned long long val;
5351 if (pevent_read_number_field(field, record->data, &val))
5354 return trace_seq_printf(s, fmt, val);
5358 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5362 static void free_func_handle(struct pevent_function_handler *func)
5364 struct pevent_func_params *params;
5368 while (func->params) {
5369 params = func->params;
5370 func->params = params->next;
5378 * pevent_register_print_function - register a helper function
5379 * @pevent: the handle to the pevent
5380 * @func: the function to process the helper function
5381 * @ret_type: the return type of the helper function
5382 * @name: the name of the helper function
5383 * @parameters: A list of enum pevent_func_arg_type
5385 * Some events may have helper functions in the print format arguments.
5386 * This allows a plugin to dynamically create a way to process one
5387 * of these functions.
5389 * The @parameters is a variable list of pevent_func_arg_type enums that
5390 * must end with PEVENT_FUNC_ARG_VOID.
5392 int pevent_register_print_function(struct pevent *pevent,
5393 pevent_func_handler func,
5394 enum pevent_func_arg_type ret_type,
5397 struct pevent_function_handler *func_handle;
5398 struct pevent_func_params **next_param;
5399 struct pevent_func_params *param;
5400 enum pevent_func_arg_type type;
5404 func_handle = find_func_handler(pevent, name);
5407 * This is most like caused by the users own
5408 * plugins updating the function. This overrides the
5411 pr_stat("override of function helper '%s'", name);
5412 remove_func_handler(pevent, name);
5415 func_handle = calloc(1, sizeof(*func_handle));
5417 do_warning("Failed to allocate function handler");
5418 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5421 func_handle->ret_type = ret_type;
5422 func_handle->name = strdup(name);
5423 func_handle->func = func;
5424 if (!func_handle->name) {
5425 do_warning("Failed to allocate function name");
5427 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5430 next_param = &(func_handle->params);
5433 type = va_arg(ap, enum pevent_func_arg_type);
5434 if (type == PEVENT_FUNC_ARG_VOID)
5437 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5438 do_warning("Invalid argument type %d", type);
5439 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5443 param = malloc(sizeof(*param));
5445 do_warning("Failed to allocate function param");
5446 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5452 *next_param = param;
5453 next_param = &(param->next);
5455 func_handle->nr_args++;
5459 func_handle->next = pevent->func_handlers;
5460 pevent->func_handlers = func_handle;
5465 free_func_handle(func_handle);
5470 * pevent_register_event_handler - register a way to parse an event
5471 * @pevent: the handle to the pevent
5472 * @id: the id of the event to register
5473 * @sys_name: the system name the event belongs to
5474 * @event_name: the name of the event
5475 * @func: the function to call to parse the event information
5476 * @context: the data to be passed to @func
5478 * This function allows a developer to override the parsing of
5479 * a given event. If for some reason the default print format
5480 * is not sufficient, this function will register a function
5481 * for an event to be used to parse the data instead.
5483 * If @id is >= 0, then it is used to find the event.
5484 * else @sys_name and @event_name are used.
5486 int pevent_register_event_handler(struct pevent *pevent, int id,
5487 const char *sys_name, const char *event_name,
5488 pevent_event_handler_func func, void *context)
5490 struct event_format *event;
5491 struct event_handler *handle;
5495 event = pevent_find_event(pevent, id);
5498 if (event_name && (strcmp(event_name, event->name) != 0))
5500 if (sys_name && (strcmp(sys_name, event->system) != 0))
5503 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5508 pr_stat("overriding event (%d) %s:%s with new print handler",
5509 event->id, event->system, event->name);
5511 event->handler = func;
5512 event->context = context;
5516 /* Save for later use. */
5517 handle = calloc(1, sizeof(*handle));
5519 do_warning("Failed to allocate event handler");
5520 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5525 handle->event_name = strdup(event_name);
5527 handle->sys_name = strdup(sys_name);
5529 if ((event_name && !handle->event_name) ||
5530 (sys_name && !handle->sys_name)) {
5531 do_warning("Failed to allocate event/sys name");
5532 free((void *)handle->event_name);
5533 free((void *)handle->sys_name);
5535 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5538 handle->func = func;
5539 handle->next = pevent->handlers;
5540 pevent->handlers = handle;
5541 handle->context = context;
5547 * pevent_alloc - create a pevent handle
5549 struct pevent *pevent_alloc(void)
5551 struct pevent *pevent = calloc(1, sizeof(*pevent));
5554 pevent->ref_count = 1;
5559 void pevent_ref(struct pevent *pevent)
5561 pevent->ref_count++;
5564 static void free_format_fields(struct format_field *field)
5566 struct format_field *next;
5577 static void free_formats(struct format *format)
5579 free_format_fields(format->common_fields);
5580 free_format_fields(format->fields);
5583 void pevent_free_format(struct event_format *event)
5586 free(event->system);
5588 free_formats(&event->format);
5590 free(event->print_fmt.format);
5591 free_args(event->print_fmt.args);
5597 * pevent_free - free a pevent handle
5598 * @pevent: the pevent handle to free
5600 void pevent_free(struct pevent *pevent)
5602 struct cmdline_list *cmdlist, *cmdnext;
5603 struct func_list *funclist, *funcnext;
5604 struct printk_list *printklist, *printknext;
5605 struct pevent_function_handler *func_handler;
5606 struct event_handler *handle;
5612 cmdlist = pevent->cmdlist;
5613 funclist = pevent->funclist;
5614 printklist = pevent->printklist;
5616 pevent->ref_count--;
5617 if (pevent->ref_count)
5620 if (pevent->cmdlines) {
5621 for (i = 0; i < pevent->cmdline_count; i++)
5622 free(pevent->cmdlines[i].comm);
5623 free(pevent->cmdlines);
5627 cmdnext = cmdlist->next;
5628 free(cmdlist->comm);
5633 if (pevent->func_map) {
5634 for (i = 0; i < (int)pevent->func_count; i++) {
5635 free(pevent->func_map[i].func);
5636 free(pevent->func_map[i].mod);
5638 free(pevent->func_map);
5642 funcnext = funclist->next;
5643 free(funclist->func);
5644 free(funclist->mod);
5646 funclist = funcnext;
5649 while (pevent->func_handlers) {
5650 func_handler = pevent->func_handlers;
5651 pevent->func_handlers = func_handler->next;
5652 free_func_handle(func_handler);
5655 if (pevent->printk_map) {
5656 for (i = 0; i < (int)pevent->printk_count; i++)
5657 free(pevent->printk_map[i].printk);
5658 free(pevent->printk_map);
5661 while (printklist) {
5662 printknext = printklist->next;
5663 free(printklist->printk);
5665 printklist = printknext;
5668 for (i = 0; i < pevent->nr_events; i++)
5669 pevent_free_format(pevent->events[i]);
5671 while (pevent->handlers) {
5672 handle = pevent->handlers;
5673 pevent->handlers = handle->next;
5674 free_handler(handle);
5677 free(pevent->events);
5678 free(pevent->sort_events);
5683 void pevent_unref(struct pevent *pevent)
5685 pevent_free(pevent);