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 #define do_warning_event(event, fmt, ...) \
59 warning("[%s:%s] " fmt, event->system, \
60 event->name, ##__VA_ARGS__); \
62 warning(fmt, ##__VA_ARGS__); \
65 static void init_input_buf(const char *buf, unsigned long long size)
72 const char *pevent_get_input_buf(void)
77 unsigned long long pevent_get_input_buf_ptr(void)
82 struct event_handler {
83 struct event_handler *next;
86 const char *event_name;
87 pevent_event_handler_func func;
91 struct pevent_func_params {
92 struct pevent_func_params *next;
93 enum pevent_func_arg_type type;
96 struct pevent_function_handler {
97 struct pevent_function_handler *next;
98 enum pevent_func_arg_type ret_type;
100 pevent_func_handler func;
101 struct pevent_func_params *params;
105 static unsigned long long
106 process_defined_func(struct trace_seq *s, void *data, int size,
107 struct event_format *event, struct print_arg *arg);
109 static void free_func_handle(struct pevent_function_handler *func);
112 * pevent_buffer_init - init buffer for parsing
113 * @buf: buffer to parse
114 * @size: the size of the buffer
116 * For use with pevent_read_token(), this initializes the internal
117 * buffer that pevent_read_token() will parse.
119 void pevent_buffer_init(const char *buf, unsigned long long size)
121 init_input_buf(buf, size);
124 void breakpoint(void)
130 struct print_arg *alloc_arg(void)
132 return calloc(1, sizeof(struct print_arg));
140 static int cmdline_cmp(const void *a, const void *b)
142 const struct cmdline *ca = a;
143 const struct cmdline *cb = b;
145 if (ca->pid < cb->pid)
147 if (ca->pid > cb->pid)
153 struct cmdline_list {
154 struct cmdline_list *next;
159 static int cmdline_init(struct pevent *pevent)
161 struct cmdline_list *cmdlist = pevent->cmdlist;
162 struct cmdline_list *item;
163 struct cmdline *cmdlines;
166 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
172 cmdlines[i].pid = cmdlist->pid;
173 cmdlines[i].comm = cmdlist->comm;
176 cmdlist = cmdlist->next;
180 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
182 pevent->cmdlines = cmdlines;
183 pevent->cmdlist = NULL;
188 static const char *find_cmdline(struct pevent *pevent, int pid)
190 const struct cmdline *comm;
196 if (!pevent->cmdlines && cmdline_init(pevent))
197 return "<not enough memory for cmdlines!>";
201 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
202 sizeof(*pevent->cmdlines), cmdline_cmp);
210 * pevent_pid_is_registered - return if a pid has a cmdline registered
211 * @pevent: handle for the pevent
212 * @pid: The pid to check if it has a cmdline registered with.
214 * Returns 1 if the pid has a cmdline mapped to it
217 int pevent_pid_is_registered(struct pevent *pevent, int pid)
219 const struct cmdline *comm;
225 if (!pevent->cmdlines && cmdline_init(pevent))
230 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
231 sizeof(*pevent->cmdlines), cmdline_cmp);
239 * If the command lines have been converted to an array, then
240 * we must add this pid. This is much slower than when cmdlines
241 * are added before the array is initialized.
243 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
245 struct cmdline *cmdlines = pevent->cmdlines;
246 const struct cmdline *cmdline;
252 /* avoid duplicates */
255 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
256 sizeof(*pevent->cmdlines), cmdline_cmp);
262 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
268 cmdlines[pevent->cmdline_count].comm = strdup(comm);
269 if (!cmdlines[pevent->cmdline_count].comm) {
275 cmdlines[pevent->cmdline_count].pid = pid;
277 if (cmdlines[pevent->cmdline_count].comm)
278 pevent->cmdline_count++;
280 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
281 pevent->cmdlines = cmdlines;
287 * pevent_register_comm - register a pid / comm mapping
288 * @pevent: handle for the pevent
289 * @comm: the command line to register
290 * @pid: the pid to map the command line to
292 * This adds a mapping to search for command line names with
293 * a given pid. The comm is duplicated.
295 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
297 struct cmdline_list *item;
299 if (pevent->cmdlines)
300 return add_new_comm(pevent, comm, pid);
302 item = malloc(sizeof(*item));
306 item->comm = strdup(comm);
312 item->next = pevent->cmdlist;
314 pevent->cmdlist = item;
315 pevent->cmdline_count++;
320 void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock)
322 pevent->trace_clock = trace_clock;
326 unsigned long long addr;
332 struct func_list *next;
333 unsigned long long addr;
338 static int func_cmp(const void *a, const void *b)
340 const struct func_map *fa = a;
341 const struct func_map *fb = b;
343 if (fa->addr < fb->addr)
345 if (fa->addr > fb->addr)
352 * We are searching for a record in between, not an exact
355 static int func_bcmp(const void *a, const void *b)
357 const struct func_map *fa = a;
358 const struct func_map *fb = b;
360 if ((fa->addr == fb->addr) ||
362 (fa->addr > fb->addr &&
363 fa->addr < (fb+1)->addr))
366 if (fa->addr < fb->addr)
372 static int func_map_init(struct pevent *pevent)
374 struct func_list *funclist;
375 struct func_list *item;
376 struct func_map *func_map;
379 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
383 funclist = pevent->funclist;
387 func_map[i].func = funclist->func;
388 func_map[i].addr = funclist->addr;
389 func_map[i].mod = funclist->mod;
392 funclist = funclist->next;
396 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
399 * Add a special record at the end.
401 func_map[pevent->func_count].func = NULL;
402 func_map[pevent->func_count].addr = 0;
403 func_map[pevent->func_count].mod = NULL;
405 pevent->func_map = func_map;
406 pevent->funclist = NULL;
411 static struct func_map *
412 find_func(struct pevent *pevent, unsigned long long addr)
414 struct func_map *func;
417 if (!pevent->func_map)
418 func_map_init(pevent);
422 func = bsearch(&key, pevent->func_map, pevent->func_count,
423 sizeof(*pevent->func_map), func_bcmp);
429 * pevent_find_function - find a function by a given address
430 * @pevent: handle for the pevent
431 * @addr: the address to find the function with
433 * Returns a pointer to the function stored that has the given
434 * address. Note, the address does not have to be exact, it
435 * will select the function that would contain the address.
437 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
439 struct func_map *map;
441 map = find_func(pevent, addr);
449 * pevent_find_function_address - find a function address by a given address
450 * @pevent: handle for the pevent
451 * @addr: the address to find the function with
453 * Returns the address the function starts at. This can be used in
454 * conjunction with pevent_find_function to print both the function
455 * name and the function offset.
458 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
460 struct func_map *map;
462 map = find_func(pevent, addr);
470 * pevent_register_function - register a function with a given address
471 * @pevent: handle for the pevent
472 * @function: the function name to register
473 * @addr: the address the function starts at
474 * @mod: the kernel module the function may be in (NULL for none)
476 * This registers a function name with an address and module.
477 * The @func passed in is duplicated.
479 int pevent_register_function(struct pevent *pevent, char *func,
480 unsigned long long addr, char *mod)
482 struct func_list *item = malloc(sizeof(*item));
487 item->next = pevent->funclist;
488 item->func = strdup(func);
493 item->mod = strdup(mod);
500 pevent->funclist = item;
501 pevent->func_count++;
515 * pevent_print_funcs - print out the stored functions
516 * @pevent: handle for the pevent
518 * This prints out the stored functions.
520 void pevent_print_funcs(struct pevent *pevent)
524 if (!pevent->func_map)
525 func_map_init(pevent);
527 for (i = 0; i < (int)pevent->func_count; i++) {
529 pevent->func_map[i].addr,
530 pevent->func_map[i].func);
531 if (pevent->func_map[i].mod)
532 printf(" [%s]\n", pevent->func_map[i].mod);
539 unsigned long long addr;
544 struct printk_list *next;
545 unsigned long long addr;
549 static int printk_cmp(const void *a, const void *b)
551 const struct printk_map *pa = a;
552 const struct printk_map *pb = b;
554 if (pa->addr < pb->addr)
556 if (pa->addr > pb->addr)
562 static int printk_map_init(struct pevent *pevent)
564 struct printk_list *printklist;
565 struct printk_list *item;
566 struct printk_map *printk_map;
569 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
573 printklist = pevent->printklist;
577 printk_map[i].printk = printklist->printk;
578 printk_map[i].addr = printklist->addr;
581 printklist = printklist->next;
585 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
587 pevent->printk_map = printk_map;
588 pevent->printklist = NULL;
593 static struct printk_map *
594 find_printk(struct pevent *pevent, unsigned long long addr)
596 struct printk_map *printk;
597 struct printk_map key;
599 if (!pevent->printk_map && printk_map_init(pevent))
604 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
605 sizeof(*pevent->printk_map), printk_cmp);
611 * pevent_register_print_string - register a string by its address
612 * @pevent: handle for the pevent
613 * @fmt: the string format to register
614 * @addr: the address the string was located at
616 * This registers a string by the address it was stored in the kernel.
617 * The @fmt passed in is duplicated.
619 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
620 unsigned long long addr)
622 struct printk_list *item = malloc(sizeof(*item));
628 item->next = pevent->printklist;
631 /* Strip off quotes and '\n' from the end */
634 item->printk = strdup(fmt);
638 p = item->printk + strlen(item->printk) - 1;
643 if (strcmp(p, "\\n") == 0)
646 pevent->printklist = item;
647 pevent->printk_count++;
658 * pevent_print_printk - print out the stored strings
659 * @pevent: handle for the pevent
661 * This prints the string formats that were stored.
663 void pevent_print_printk(struct pevent *pevent)
667 if (!pevent->printk_map)
668 printk_map_init(pevent);
670 for (i = 0; i < (int)pevent->printk_count; i++) {
671 printf("%016llx %s\n",
672 pevent->printk_map[i].addr,
673 pevent->printk_map[i].printk);
677 static struct event_format *alloc_event(void)
679 return calloc(1, sizeof(struct event_format));
682 static int add_event(struct pevent *pevent, struct event_format *event)
685 struct event_format **events = realloc(pevent->events, sizeof(event) *
686 (pevent->nr_events + 1));
690 pevent->events = events;
692 for (i = 0; i < pevent->nr_events; i++) {
693 if (pevent->events[i]->id > event->id)
696 if (i < pevent->nr_events)
697 memmove(&pevent->events[i + 1],
699 sizeof(event) * (pevent->nr_events - i));
701 pevent->events[i] = event;
704 event->pevent = pevent;
709 static int event_item_type(enum event_type type)
712 case EVENT_ITEM ... EVENT_SQUOTE:
714 case EVENT_ERROR ... EVENT_DELIM:
720 static void free_flag_sym(struct print_flag_sym *fsym)
722 struct print_flag_sym *next;
733 static void free_arg(struct print_arg *arg)
735 struct print_arg *farg;
742 free(arg->atom.atom);
745 free(arg->field.name);
748 free_arg(arg->flags.field);
749 free(arg->flags.delim);
750 free_flag_sym(arg->flags.flags);
753 free_arg(arg->symbol.field);
754 free_flag_sym(arg->symbol.symbols);
757 free_arg(arg->hex.field);
758 free_arg(arg->hex.size);
761 free(arg->typecast.type);
762 free_arg(arg->typecast.item);
766 free(arg->string.string);
768 case PRINT_DYNAMIC_ARRAY:
769 free(arg->dynarray.index);
773 free_arg(arg->op.left);
774 free_arg(arg->op.right);
777 while (arg->func.args) {
778 farg = arg->func.args;
779 arg->func.args = farg->next;
792 static enum event_type get_type(int ch)
795 return EVENT_NEWLINE;
798 if (isalnum(ch) || ch == '_')
806 if (ch == '(' || ch == ')' || ch == ',')
812 static int __read_char(void)
814 if (input_buf_ptr >= input_buf_siz)
817 return input_buf[input_buf_ptr++];
820 static int __peek_char(void)
822 if (input_buf_ptr >= input_buf_siz)
825 return input_buf[input_buf_ptr];
829 * pevent_peek_char - peek at the next character that will be read
831 * Returns the next character read, or -1 if end of buffer.
833 int pevent_peek_char(void)
835 return __peek_char();
838 static int extend_token(char **tok, char *buf, int size)
840 char *newtok = realloc(*tok, size);
857 static enum event_type force_token(const char *str, char **tok);
859 static enum event_type __read_token(char **tok)
862 int ch, last_ch, quote_ch, next_ch;
865 enum event_type type;
875 if (type == EVENT_NONE)
883 if (asprintf(tok, "%c", ch) < 0)
891 next_ch = __peek_char();
892 if (next_ch == '>') {
893 buf[i++] = __read_char();
906 buf[i++] = __read_char();
918 default: /* what should we do instead? */
928 buf[i++] = __read_char();
933 /* don't keep quotes */
939 if (i == (BUFSIZ - 1)) {
943 if (extend_token(tok, buf, tok_size) < 0)
950 /* the '\' '\' will cancel itself */
951 if (ch == '\\' && last_ch == '\\')
953 } while (ch != quote_ch || last_ch == '\\');
954 /* remove the last quote */
958 * For strings (double quotes) check the next token.
959 * If it is another string, concatinate the two.
961 if (type == EVENT_DQUOTE) {
962 unsigned long long save_input_buf_ptr = input_buf_ptr;
966 } while (isspace(ch));
969 input_buf_ptr = save_input_buf_ptr;
974 case EVENT_ERROR ... EVENT_SPACE:
980 while (get_type(__peek_char()) == type) {
981 if (i == (BUFSIZ - 1)) {
985 if (extend_token(tok, buf, tok_size) < 0)
995 if (extend_token(tok, buf, tok_size + i + 1) < 0)
998 if (type == EVENT_ITEM) {
1000 * Older versions of the kernel has a bug that
1001 * creates invalid symbols and will break the mac80211
1002 * parsing. This is a work around to that bug.
1004 * See Linux kernel commit:
1005 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1007 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1010 return force_token("\"\%s\" ", tok);
1011 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1014 return force_token("\" sta:%pM\" ", tok);
1015 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1018 return force_token("\" vif:%p(%d)\" ", tok);
1025 static enum event_type force_token(const char *str, char **tok)
1027 const char *save_input_buf;
1028 unsigned long long save_input_buf_ptr;
1029 unsigned long long save_input_buf_siz;
1030 enum event_type type;
1032 /* save off the current input pointers */
1033 save_input_buf = input_buf;
1034 save_input_buf_ptr = input_buf_ptr;
1035 save_input_buf_siz = input_buf_siz;
1037 init_input_buf(str, strlen(str));
1039 type = __read_token(tok);
1041 /* reset back to original token */
1042 input_buf = save_input_buf;
1043 input_buf_ptr = save_input_buf_ptr;
1044 input_buf_siz = save_input_buf_siz;
1049 static void free_token(char *tok)
1055 static enum event_type read_token(char **tok)
1057 enum event_type type;
1060 type = __read_token(tok);
1061 if (type != EVENT_SPACE)
1073 * pevent_read_token - access to utilites to use the pevent parser
1074 * @tok: The token to return
1076 * This will parse tokens from the string given by
1077 * pevent_init_data().
1079 * Returns the token type.
1081 enum event_type pevent_read_token(char **tok)
1083 return read_token(tok);
1087 * pevent_free_token - free a token returned by pevent_read_token
1088 * @token: the token to free
1090 void pevent_free_token(char *token)
1096 static enum event_type read_token_item(char **tok)
1098 enum event_type type;
1101 type = __read_token(tok);
1102 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1113 static int test_type(enum event_type type, enum event_type expect)
1115 if (type != expect) {
1116 do_warning("Error: expected type %d but read %d",
1123 static int test_type_token(enum event_type type, const char *token,
1124 enum event_type expect, const char *expect_tok)
1126 if (type != expect) {
1127 do_warning("Error: expected type %d but read %d",
1132 if (strcmp(token, expect_tok) != 0) {
1133 do_warning("Error: expected '%s' but read '%s'",
1140 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1142 enum event_type type;
1145 type = read_token(tok);
1147 type = read_token_item(tok);
1148 return test_type(type, expect);
1151 static int read_expect_type(enum event_type expect, char **tok)
1153 return __read_expect_type(expect, tok, 1);
1156 static int __read_expected(enum event_type expect, const char *str,
1159 enum event_type type;
1164 type = read_token(&token);
1166 type = read_token_item(&token);
1168 ret = test_type_token(type, token, expect, str);
1175 static int read_expected(enum event_type expect, const char *str)
1177 return __read_expected(expect, str, 1);
1180 static int read_expected_item(enum event_type expect, const char *str)
1182 return __read_expected(expect, str, 0);
1185 static char *event_read_name(void)
1189 if (read_expected(EVENT_ITEM, "name") < 0)
1192 if (read_expected(EVENT_OP, ":") < 0)
1195 if (read_expect_type(EVENT_ITEM, &token) < 0)
1205 static int event_read_id(void)
1210 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1213 if (read_expected(EVENT_OP, ":") < 0)
1216 if (read_expect_type(EVENT_ITEM, &token) < 0)
1219 id = strtoul(token, NULL, 0);
1228 static int field_is_string(struct format_field *field)
1230 if ((field->flags & FIELD_IS_ARRAY) &&
1231 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1232 strstr(field->type, "s8")))
1238 static int field_is_dynamic(struct format_field *field)
1240 if (strncmp(field->type, "__data_loc", 10) == 0)
1246 static int field_is_long(struct format_field *field)
1248 /* includes long long */
1249 if (strstr(field->type, "long"))
1255 static unsigned int type_size(const char *name)
1257 /* This covers all FIELD_IS_STRING types. */
1275 for (i = 0; table[i].type; i++) {
1276 if (!strcmp(table[i].type, name))
1277 return table[i].size;
1283 static int event_read_fields(struct event_format *event, struct format_field **fields)
1285 struct format_field *field = NULL;
1286 enum event_type type;
1292 unsigned int size_dynamic = 0;
1294 type = read_token(&token);
1295 if (type == EVENT_NEWLINE) {
1302 if (test_type_token(type, token, EVENT_ITEM, "field"))
1306 type = read_token(&token);
1308 * The ftrace fields may still use the "special" name.
1311 if (event->flags & EVENT_FL_ISFTRACE &&
1312 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1314 type = read_token(&token);
1317 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1321 if (read_expect_type(EVENT_ITEM, &token) < 0)
1326 field = calloc(1, sizeof(*field));
1330 field->event = event;
1332 /* read the rest of the type */
1334 type = read_token(&token);
1335 if (type == EVENT_ITEM ||
1336 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1338 * Some of the ftrace fields are broken and have
1339 * an illegal "." in them.
1341 (event->flags & EVENT_FL_ISFTRACE &&
1342 type == EVENT_OP && strcmp(token, ".") == 0)) {
1344 if (strcmp(token, "*") == 0)
1345 field->flags |= FIELD_IS_POINTER;
1349 new_type = realloc(field->type,
1350 strlen(field->type) +
1351 strlen(last_token) + 2);
1356 field->type = new_type;
1357 strcat(field->type, " ");
1358 strcat(field->type, last_token);
1361 field->type = last_token;
1370 do_warning_event(event, "%s: no type found", __func__);
1373 field->name = last_token;
1375 if (test_type(type, EVENT_OP))
1378 if (strcmp(token, "[") == 0) {
1379 enum event_type last_type = type;
1380 char *brackets = token;
1384 field->flags |= FIELD_IS_ARRAY;
1386 type = read_token(&token);
1388 if (type == EVENT_ITEM)
1389 field->arraylen = strtoul(token, NULL, 0);
1391 field->arraylen = 0;
1393 while (strcmp(token, "]") != 0) {
1394 if (last_type == EVENT_ITEM &&
1401 new_brackets = realloc(brackets,
1403 strlen(token) + len);
1404 if (!new_brackets) {
1408 brackets = new_brackets;
1410 strcat(brackets, " ");
1411 strcat(brackets, token);
1412 /* We only care about the last token */
1413 field->arraylen = strtoul(token, NULL, 0);
1415 type = read_token(&token);
1416 if (type == EVENT_NONE) {
1417 do_warning_event(event, "failed to find token");
1424 new_brackets = realloc(brackets, strlen(brackets) + 2);
1425 if (!new_brackets) {
1429 brackets = new_brackets;
1430 strcat(brackets, "]");
1432 /* add brackets to type */
1434 type = read_token(&token);
1436 * If the next token is not an OP, then it is of
1437 * the format: type [] item;
1439 if (type == EVENT_ITEM) {
1441 new_type = realloc(field->type,
1442 strlen(field->type) +
1443 strlen(field->name) +
1444 strlen(brackets) + 2);
1449 field->type = new_type;
1450 strcat(field->type, " ");
1451 strcat(field->type, field->name);
1452 size_dynamic = type_size(field->name);
1453 free_token(field->name);
1454 strcat(field->type, brackets);
1455 field->name = token;
1456 type = read_token(&token);
1459 new_type = realloc(field->type,
1460 strlen(field->type) +
1461 strlen(brackets) + 1);
1466 field->type = new_type;
1467 strcat(field->type, brackets);
1472 if (field_is_string(field))
1473 field->flags |= FIELD_IS_STRING;
1474 if (field_is_dynamic(field))
1475 field->flags |= FIELD_IS_DYNAMIC;
1476 if (field_is_long(field))
1477 field->flags |= FIELD_IS_LONG;
1479 if (test_type_token(type, token, EVENT_OP, ";"))
1483 if (read_expected(EVENT_ITEM, "offset") < 0)
1486 if (read_expected(EVENT_OP, ":") < 0)
1489 if (read_expect_type(EVENT_ITEM, &token))
1491 field->offset = strtoul(token, NULL, 0);
1494 if (read_expected(EVENT_OP, ";") < 0)
1497 if (read_expected(EVENT_ITEM, "size") < 0)
1500 if (read_expected(EVENT_OP, ":") < 0)
1503 if (read_expect_type(EVENT_ITEM, &token))
1505 field->size = strtoul(token, NULL, 0);
1508 if (read_expected(EVENT_OP, ";") < 0)
1511 type = read_token(&token);
1512 if (type != EVENT_NEWLINE) {
1513 /* newer versions of the kernel have a "signed" type */
1514 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1519 if (read_expected(EVENT_OP, ":") < 0)
1522 if (read_expect_type(EVENT_ITEM, &token))
1525 if (strtoul(token, NULL, 0))
1526 field->flags |= FIELD_IS_SIGNED;
1529 if (read_expected(EVENT_OP, ";") < 0)
1532 if (read_expect_type(EVENT_NEWLINE, &token))
1538 if (field->flags & FIELD_IS_ARRAY) {
1539 if (field->arraylen)
1540 field->elementsize = field->size / field->arraylen;
1541 else if (field->flags & FIELD_IS_DYNAMIC)
1542 field->elementsize = size_dynamic;
1543 else if (field->flags & FIELD_IS_STRING)
1544 field->elementsize = 1;
1545 else if (field->flags & FIELD_IS_LONG)
1546 field->elementsize = event->pevent ?
1547 event->pevent->long_size :
1550 field->elementsize = field->size;
1553 fields = &field->next;
1570 static int event_read_format(struct event_format *event)
1575 if (read_expected_item(EVENT_ITEM, "format") < 0)
1578 if (read_expected(EVENT_OP, ":") < 0)
1581 if (read_expect_type(EVENT_NEWLINE, &token))
1585 ret = event_read_fields(event, &event->format.common_fields);
1588 event->format.nr_common = ret;
1590 ret = event_read_fields(event, &event->format.fields);
1593 event->format.nr_fields = ret;
1602 static enum event_type
1603 process_arg_token(struct event_format *event, struct print_arg *arg,
1604 char **tok, enum event_type type);
1606 static enum event_type
1607 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1609 enum event_type type;
1612 type = read_token(&token);
1615 return process_arg_token(event, arg, tok, type);
1618 static enum event_type
1619 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1622 * For __print_symbolic() and __print_flags, we need to completely
1623 * evaluate the first argument, which defines what to print next.
1625 static enum event_type
1626 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1628 enum event_type type;
1630 type = process_arg(event, arg, tok);
1632 while (type == EVENT_OP) {
1633 type = process_op(event, arg, tok);
1639 static enum event_type
1640 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1642 struct print_arg *arg, *left, *right;
1643 enum event_type type;
1648 right = alloc_arg();
1650 if (!arg || !left || !right) {
1651 do_warning_event(event, "%s: not enough memory!", __func__);
1652 /* arg will be freed at out_free */
1658 arg->type = PRINT_OP;
1659 arg->op.left = left;
1660 arg->op.right = right;
1663 type = process_arg(event, left, &token);
1666 /* Handle other operations in the arguments */
1667 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1668 type = process_op(event, left, &token);
1672 if (test_type_token(type, token, EVENT_OP, ":"))
1677 type = process_arg(event, right, &token);
1679 top->op.right = arg;
1685 /* Top may point to itself */
1686 top->op.right = NULL;
1692 static enum event_type
1693 process_array(struct event_format *event, struct print_arg *top, char **tok)
1695 struct print_arg *arg;
1696 enum event_type type;
1701 do_warning_event(event, "%s: not enough memory!", __func__);
1702 /* '*tok' is set to top->op.op. No need to free. */
1708 type = process_arg(event, arg, &token);
1709 if (test_type_token(type, token, EVENT_OP, "]"))
1712 top->op.right = arg;
1715 type = read_token_item(&token);
1726 static int get_op_prio(char *op)
1740 /* '>>' and '<<' are 8 */
1744 /* '==' and '!=' are 10 */
1754 do_warning("unknown op '%c'", op[0]);
1758 if (strcmp(op, "++") == 0 ||
1759 strcmp(op, "--") == 0) {
1761 } else if (strcmp(op, ">>") == 0 ||
1762 strcmp(op, "<<") == 0) {
1764 } else if (strcmp(op, ">=") == 0 ||
1765 strcmp(op, "<=") == 0) {
1767 } else if (strcmp(op, "==") == 0 ||
1768 strcmp(op, "!=") == 0) {
1770 } else if (strcmp(op, "&&") == 0) {
1772 } else if (strcmp(op, "||") == 0) {
1775 do_warning("unknown op '%s'", op);
1781 static int set_op_prio(struct print_arg *arg)
1784 /* single ops are the greatest */
1785 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1788 arg->op.prio = get_op_prio(arg->op.op);
1790 return arg->op.prio;
1793 /* Note, *tok does not get freed, but will most likely be saved */
1794 static enum event_type
1795 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1797 struct print_arg *left, *right = NULL;
1798 enum event_type type;
1801 /* the op is passed in via tok */
1804 if (arg->type == PRINT_OP && !arg->op.left) {
1805 /* handle single op */
1807 do_warning_event(event, "bad op token %s", token);
1817 do_warning_event(event, "bad op token %s", token);
1822 /* make an empty left */
1827 left->type = PRINT_NULL;
1828 arg->op.left = left;
1830 right = alloc_arg();
1834 arg->op.right = right;
1836 /* do not free the token, it belongs to an op */
1838 type = process_arg(event, right, tok);
1840 } else if (strcmp(token, "?") == 0) {
1846 /* copy the top arg to the left */
1849 arg->type = PRINT_OP;
1851 arg->op.left = left;
1854 /* it will set arg->op.right */
1855 type = process_cond(event, arg, tok);
1857 } else if (strcmp(token, ">>") == 0 ||
1858 strcmp(token, "<<") == 0 ||
1859 strcmp(token, "&") == 0 ||
1860 strcmp(token, "|") == 0 ||
1861 strcmp(token, "&&") == 0 ||
1862 strcmp(token, "||") == 0 ||
1863 strcmp(token, "-") == 0 ||
1864 strcmp(token, "+") == 0 ||
1865 strcmp(token, "*") == 0 ||
1866 strcmp(token, "^") == 0 ||
1867 strcmp(token, "/") == 0 ||
1868 strcmp(token, "<") == 0 ||
1869 strcmp(token, ">") == 0 ||
1870 strcmp(token, "<=") == 0 ||
1871 strcmp(token, ">=") == 0 ||
1872 strcmp(token, "==") == 0 ||
1873 strcmp(token, "!=") == 0) {
1879 /* copy the top arg to the left */
1882 arg->type = PRINT_OP;
1884 arg->op.left = left;
1885 arg->op.right = NULL;
1887 if (set_op_prio(arg) == -1) {
1888 event->flags |= EVENT_FL_FAILED;
1889 /* arg->op.op (= token) will be freed at out_free */
1894 type = read_token_item(&token);
1897 /* could just be a type pointer */
1898 if ((strcmp(arg->op.op, "*") == 0) &&
1899 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1902 if (left->type != PRINT_ATOM) {
1903 do_warning_event(event, "bad pointer type");
1906 new_atom = realloc(left->atom.atom,
1907 strlen(left->atom.atom) + 3);
1911 left->atom.atom = new_atom;
1912 strcat(left->atom.atom, " *");
1920 right = alloc_arg();
1924 type = process_arg_token(event, right, tok, type);
1925 arg->op.right = right;
1927 } else if (strcmp(token, "[") == 0) {
1935 arg->type = PRINT_OP;
1937 arg->op.left = left;
1941 /* it will set arg->op.right */
1942 type = process_array(event, arg, tok);
1945 do_warning_event(event, "unknown op '%s'", token);
1946 event->flags |= EVENT_FL_FAILED;
1947 /* the arg is now the left side */
1951 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1954 /* higher prios need to be closer to the root */
1955 prio = get_op_prio(*tok);
1957 if (prio > arg->op.prio)
1958 return process_op(event, arg, tok);
1960 return process_op(event, right, tok);
1966 do_warning_event(event, "%s: not enough memory!", __func__);
1973 static enum event_type
1974 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
1977 enum event_type type;
1981 if (read_expected(EVENT_OP, "->") < 0)
1984 if (read_expect_type(EVENT_ITEM, &token) < 0)
1988 arg->type = PRINT_FIELD;
1989 arg->field.name = field;
1991 if (is_flag_field) {
1992 arg->field.field = pevent_find_any_field(event, arg->field.name);
1993 arg->field.field->flags |= FIELD_IS_FLAG;
1995 } else if (is_symbolic_field) {
1996 arg->field.field = pevent_find_any_field(event, arg->field.name);
1997 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1998 is_symbolic_field = 0;
2001 type = read_token(&token);
2013 static char *arg_eval (struct print_arg *arg);
2015 static unsigned long long
2016 eval_type_str(unsigned long long val, const char *type, int pointer)
2026 if (type[len-1] != '*') {
2027 do_warning("pointer expected with non pointer type");
2033 do_warning("%s: not enough memory!", __func__);
2036 memcpy(ref, type, len);
2038 /* chop off the " *" */
2041 val = eval_type_str(val, ref, 0);
2046 /* check if this is a pointer */
2047 if (type[len - 1] == '*')
2050 /* Try to figure out the arg size*/
2051 if (strncmp(type, "struct", 6) == 0)
2055 if (strcmp(type, "u8") == 0)
2058 if (strcmp(type, "u16") == 0)
2059 return val & 0xffff;
2061 if (strcmp(type, "u32") == 0)
2062 return val & 0xffffffff;
2064 if (strcmp(type, "u64") == 0 ||
2065 strcmp(type, "s64"))
2068 if (strcmp(type, "s8") == 0)
2069 return (unsigned long long)(char)val & 0xff;
2071 if (strcmp(type, "s16") == 0)
2072 return (unsigned long long)(short)val & 0xffff;
2074 if (strcmp(type, "s32") == 0)
2075 return (unsigned long long)(int)val & 0xffffffff;
2077 if (strncmp(type, "unsigned ", 9) == 0) {
2082 if (strcmp(type, "char") == 0) {
2084 return (unsigned long long)(char)val & 0xff;
2089 if (strcmp(type, "short") == 0) {
2091 return (unsigned long long)(short)val & 0xffff;
2093 return val & 0xffff;
2096 if (strcmp(type, "int") == 0) {
2098 return (unsigned long long)(int)val & 0xffffffff;
2100 return val & 0xffffffff;
2107 * Try to figure out the type.
2109 static unsigned long long
2110 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2112 if (arg->type != PRINT_TYPE) {
2113 do_warning("expected type argument");
2117 return eval_type_str(val, arg->typecast.type, pointer);
2120 static int arg_num_eval(struct print_arg *arg, long long *val)
2122 long long left, right;
2125 switch (arg->type) {
2127 *val = strtoll(arg->atom.atom, NULL, 0);
2130 ret = arg_num_eval(arg->typecast.item, val);
2133 *val = eval_type(*val, arg, 0);
2136 switch (arg->op.op[0]) {
2138 ret = arg_num_eval(arg->op.left, &left);
2141 ret = arg_num_eval(arg->op.right, &right);
2145 *val = left || right;
2147 *val = left | right;
2150 ret = arg_num_eval(arg->op.left, &left);
2153 ret = arg_num_eval(arg->op.right, &right);
2157 *val = left && right;
2159 *val = left & right;
2162 ret = arg_num_eval(arg->op.left, &left);
2165 ret = arg_num_eval(arg->op.right, &right);
2168 switch (arg->op.op[1]) {
2170 *val = left < right;
2173 *val = left << right;
2176 *val = left <= right;
2179 do_warning("unknown op '%s'", arg->op.op);
2184 ret = arg_num_eval(arg->op.left, &left);
2187 ret = arg_num_eval(arg->op.right, &right);
2190 switch (arg->op.op[1]) {
2192 *val = left > right;
2195 *val = left >> right;
2198 *val = left >= right;
2201 do_warning("unknown op '%s'", arg->op.op);
2206 ret = arg_num_eval(arg->op.left, &left);
2209 ret = arg_num_eval(arg->op.right, &right);
2213 if (arg->op.op[1] != '=') {
2214 do_warning("unknown op '%s'", arg->op.op);
2217 *val = left == right;
2220 ret = arg_num_eval(arg->op.left, &left);
2223 ret = arg_num_eval(arg->op.right, &right);
2227 switch (arg->op.op[1]) {
2229 *val = left != right;
2232 do_warning("unknown op '%s'", arg->op.op);
2237 /* check for negative */
2238 if (arg->op.left->type == PRINT_NULL)
2241 ret = arg_num_eval(arg->op.left, &left);
2244 ret = arg_num_eval(arg->op.right, &right);
2247 *val = left - right;
2250 if (arg->op.left->type == PRINT_NULL)
2253 ret = arg_num_eval(arg->op.left, &left);
2256 ret = arg_num_eval(arg->op.right, &right);
2259 *val = left + right;
2262 do_warning("unknown op '%s'", arg->op.op);
2268 case PRINT_FIELD ... PRINT_SYMBOL:
2272 do_warning("invalid eval type %d", arg->type);
2279 static char *arg_eval (struct print_arg *arg)
2282 static char buf[20];
2284 switch (arg->type) {
2286 return arg->atom.atom;
2288 return arg_eval(arg->typecast.item);
2290 if (!arg_num_eval(arg, &val))
2292 sprintf(buf, "%lld", val);
2296 case PRINT_FIELD ... PRINT_SYMBOL:
2300 do_warning("invalid eval type %d", arg->type);
2307 static enum event_type
2308 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2310 enum event_type type;
2311 struct print_arg *arg = NULL;
2312 struct print_flag_sym *field;
2318 type = read_token_item(&token);
2319 if (test_type_token(type, token, EVENT_OP, "{"))
2327 type = process_arg(event, arg, &token);
2329 if (type == EVENT_OP)
2330 type = process_op(event, arg, &token);
2332 if (type == EVENT_ERROR)
2335 if (test_type_token(type, token, EVENT_DELIM, ","))
2338 field = calloc(1, sizeof(*field));
2342 value = arg_eval(arg);
2344 goto out_free_field;
2345 field->value = strdup(value);
2346 if (field->value == NULL)
2347 goto out_free_field;
2355 type = process_arg(event, arg, &token);
2356 if (test_type_token(type, token, EVENT_OP, "}"))
2357 goto out_free_field;
2359 value = arg_eval(arg);
2361 goto out_free_field;
2362 field->str = strdup(value);
2363 if (field->str == NULL)
2364 goto out_free_field;
2369 list = &field->next;
2372 type = read_token_item(&token);
2373 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2379 free_flag_sym(field);
2388 static enum event_type
2389 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2391 struct print_arg *field;
2392 enum event_type type;
2395 memset(arg, 0, sizeof(*arg));
2396 arg->type = PRINT_FLAGS;
2398 field = alloc_arg();
2400 do_warning_event(event, "%s: not enough memory!", __func__);
2404 type = process_field_arg(event, field, &token);
2406 /* Handle operations in the first argument */
2407 while (type == EVENT_OP)
2408 type = process_op(event, field, &token);
2410 if (test_type_token(type, token, EVENT_DELIM, ","))
2411 goto out_free_field;
2414 arg->flags.field = field;
2416 type = read_token_item(&token);
2417 if (event_item_type(type)) {
2418 arg->flags.delim = token;
2419 type = read_token_item(&token);
2422 if (test_type_token(type, token, EVENT_DELIM, ","))
2425 type = process_fields(event, &arg->flags.flags, &token);
2426 if (test_type_token(type, token, EVENT_DELIM, ")"))
2430 type = read_token_item(tok);
2441 static enum event_type
2442 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2444 struct print_arg *field;
2445 enum event_type type;
2448 memset(arg, 0, sizeof(*arg));
2449 arg->type = PRINT_SYMBOL;
2451 field = alloc_arg();
2453 do_warning_event(event, "%s: not enough memory!", __func__);
2457 type = process_field_arg(event, field, &token);
2459 if (test_type_token(type, token, EVENT_DELIM, ","))
2460 goto out_free_field;
2462 arg->symbol.field = field;
2464 type = process_fields(event, &arg->symbol.symbols, &token);
2465 if (test_type_token(type, token, EVENT_DELIM, ")"))
2469 type = read_token_item(tok);
2480 static enum event_type
2481 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2483 struct print_arg *field;
2484 enum event_type type;
2487 memset(arg, 0, sizeof(*arg));
2488 arg->type = PRINT_HEX;
2490 field = alloc_arg();
2492 do_warning_event(event, "%s: not enough memory!", __func__);
2496 type = process_arg(event, field, &token);
2498 if (test_type_token(type, token, EVENT_DELIM, ","))
2501 arg->hex.field = field;
2505 field = alloc_arg();
2507 do_warning_event(event, "%s: not enough memory!", __func__);
2512 type = process_arg(event, field, &token);
2514 if (test_type_token(type, token, EVENT_DELIM, ")"))
2517 arg->hex.size = field;
2520 type = read_token_item(tok);
2530 static enum event_type
2531 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2533 struct format_field *field;
2534 enum event_type type;
2537 memset(arg, 0, sizeof(*arg));
2538 arg->type = PRINT_DYNAMIC_ARRAY;
2541 * The item within the parenthesis is another field that holds
2542 * the index into where the array starts.
2544 type = read_token(&token);
2546 if (type != EVENT_ITEM)
2549 /* Find the field */
2551 field = pevent_find_field(event, token);
2555 arg->dynarray.field = field;
2556 arg->dynarray.index = 0;
2558 if (read_expected(EVENT_DELIM, ")") < 0)
2562 type = read_token_item(&token);
2564 if (type != EVENT_OP || strcmp(token, "[") != 0)
2570 do_warning_event(event, "%s: not enough memory!", __func__);
2575 type = process_arg(event, arg, &token);
2576 if (type == EVENT_ERROR)
2579 if (!test_type_token(type, token, EVENT_OP, "]"))
2583 type = read_token_item(tok);
2594 static enum event_type
2595 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2597 struct print_arg *item_arg;
2598 enum event_type type;
2601 type = process_arg(event, arg, &token);
2603 if (type == EVENT_ERROR)
2606 if (type == EVENT_OP)
2607 type = process_op(event, arg, &token);
2609 if (type == EVENT_ERROR)
2612 if (test_type_token(type, token, EVENT_DELIM, ")"))
2616 type = read_token_item(&token);
2619 * If the next token is an item or another open paren, then
2620 * this was a typecast.
2622 if (event_item_type(type) ||
2623 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2625 /* make this a typecast and contine */
2627 /* prevous must be an atom */
2628 if (arg->type != PRINT_ATOM) {
2629 do_warning_event(event, "previous needed to be PRINT_ATOM");
2633 item_arg = alloc_arg();
2635 do_warning_event(event, "%s: not enough memory!",
2640 arg->type = PRINT_TYPE;
2641 arg->typecast.type = arg->atom.atom;
2642 arg->typecast.item = item_arg;
2643 type = process_arg_token(event, item_arg, &token, type);
2657 static enum event_type
2658 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2661 enum event_type type;
2664 if (read_expect_type(EVENT_ITEM, &token) < 0)
2667 arg->type = PRINT_STRING;
2668 arg->string.string = token;
2669 arg->string.offset = -1;
2671 if (read_expected(EVENT_DELIM, ")") < 0)
2674 type = read_token(&token);
2686 static struct pevent_function_handler *
2687 find_func_handler(struct pevent *pevent, char *func_name)
2689 struct pevent_function_handler *func;
2694 for (func = pevent->func_handlers; func; func = func->next) {
2695 if (strcmp(func->name, func_name) == 0)
2702 static void remove_func_handler(struct pevent *pevent, char *func_name)
2704 struct pevent_function_handler *func;
2705 struct pevent_function_handler **next;
2707 next = &pevent->func_handlers;
2708 while ((func = *next)) {
2709 if (strcmp(func->name, func_name) == 0) {
2711 free_func_handle(func);
2718 static enum event_type
2719 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2720 struct print_arg *arg, char **tok)
2722 struct print_arg **next_arg;
2723 struct print_arg *farg;
2724 enum event_type type;
2728 arg->type = PRINT_FUNC;
2729 arg->func.func = func;
2733 next_arg = &(arg->func.args);
2734 for (i = 0; i < func->nr_args; i++) {
2737 do_warning_event(event, "%s: not enough memory!",
2742 type = process_arg(event, farg, &token);
2743 if (i < (func->nr_args - 1)) {
2744 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2745 do_warning_event(event,
2746 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2747 func->name, func->nr_args,
2748 event->name, i + 1);
2752 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2753 do_warning_event(event,
2754 "Error: function '%s()' only expects %d arguments but event %s has more",
2755 func->name, func->nr_args, event->name);
2761 next_arg = &(farg->next);
2765 type = read_token(&token);
2776 static enum event_type
2777 process_function(struct event_format *event, struct print_arg *arg,
2778 char *token, char **tok)
2780 struct pevent_function_handler *func;
2782 if (strcmp(token, "__print_flags") == 0) {
2785 return process_flags(event, arg, tok);
2787 if (strcmp(token, "__print_symbolic") == 0) {
2789 is_symbolic_field = 1;
2790 return process_symbols(event, arg, tok);
2792 if (strcmp(token, "__print_hex") == 0) {
2794 return process_hex(event, arg, tok);
2796 if (strcmp(token, "__get_str") == 0) {
2798 return process_str(event, arg, tok);
2800 if (strcmp(token, "__get_dynamic_array") == 0) {
2802 return process_dynamic_array(event, arg, tok);
2805 func = find_func_handler(event->pevent, token);
2808 return process_func_handler(event, func, arg, tok);
2811 do_warning_event(event, "function %s not defined", token);
2816 static enum event_type
2817 process_arg_token(struct event_format *event, struct print_arg *arg,
2818 char **tok, enum event_type type)
2827 if (strcmp(token, "REC") == 0) {
2829 type = process_entry(event, arg, &token);
2833 /* test the next token */
2834 type = read_token_item(&token);
2837 * If the next token is a parenthesis, then this
2840 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2843 /* this will free atom. */
2844 type = process_function(event, arg, atom, &token);
2847 /* atoms can be more than one token long */
2848 while (type == EVENT_ITEM) {
2850 new_atom = realloc(atom,
2851 strlen(atom) + strlen(token) + 2);
2860 strcat(atom, token);
2862 type = read_token_item(&token);
2865 arg->type = PRINT_ATOM;
2866 arg->atom.atom = atom;
2871 arg->type = PRINT_ATOM;
2872 arg->atom.atom = token;
2873 type = read_token_item(&token);
2876 if (strcmp(token, "(") == 0) {
2878 type = process_paren(event, arg, &token);
2882 /* handle single ops */
2883 arg->type = PRINT_OP;
2885 arg->op.left = NULL;
2886 type = process_op(event, arg, &token);
2888 /* On error, the op is freed */
2889 if (type == EVENT_ERROR)
2892 /* return error type if errored */
2895 case EVENT_ERROR ... EVENT_NEWLINE:
2897 do_warning_event(event, "unexpected type %d", type);
2905 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2907 enum event_type type = EVENT_ERROR;
2908 struct print_arg *arg;
2913 if (type == EVENT_NEWLINE) {
2914 type = read_token_item(&token);
2920 do_warning_event(event, "%s: not enough memory!",
2925 type = process_arg(event, arg, &token);
2927 if (type == EVENT_ERROR) {
2936 if (type == EVENT_OP) {
2937 type = process_op(event, arg, &token);
2939 if (type == EVENT_ERROR) {
2948 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2955 } while (type != EVENT_NONE);
2957 if (type != EVENT_NONE && type != EVENT_ERROR)
2963 static int event_read_print(struct event_format *event)
2965 enum event_type type;
2969 if (read_expected_item(EVENT_ITEM, "print") < 0)
2972 if (read_expected(EVENT_ITEM, "fmt") < 0)
2975 if (read_expected(EVENT_OP, ":") < 0)
2978 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2982 event->print_fmt.format = token;
2983 event->print_fmt.args = NULL;
2985 /* ok to have no arg */
2986 type = read_token_item(&token);
2988 if (type == EVENT_NONE)
2991 /* Handle concatenation of print lines */
2992 if (type == EVENT_DQUOTE) {
2995 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
2998 free_token(event->print_fmt.format);
2999 event->print_fmt.format = NULL;
3004 if (test_type_token(type, token, EVENT_DELIM, ","))
3009 ret = event_read_print_args(event, &event->print_fmt.args);
3021 * pevent_find_common_field - return a common field by event
3022 * @event: handle for the event
3023 * @name: the name of the common field to return
3025 * Returns a common field from the event by the given @name.
3026 * This only searchs the common fields and not all field.
3028 struct format_field *
3029 pevent_find_common_field(struct event_format *event, const char *name)
3031 struct format_field *format;
3033 for (format = event->format.common_fields;
3034 format; format = format->next) {
3035 if (strcmp(format->name, name) == 0)
3043 * pevent_find_field - find a non-common field
3044 * @event: handle for the event
3045 * @name: the name of the non-common field
3047 * Returns a non-common field by the given @name.
3048 * This does not search common fields.
3050 struct format_field *
3051 pevent_find_field(struct event_format *event, const char *name)
3053 struct format_field *format;
3055 for (format = event->format.fields;
3056 format; format = format->next) {
3057 if (strcmp(format->name, name) == 0)
3065 * pevent_find_any_field - find any field by name
3066 * @event: handle for the event
3067 * @name: the name of the field
3069 * Returns a field by the given @name.
3070 * This searchs the common field names first, then
3071 * the non-common ones if a common one was not found.
3073 struct format_field *
3074 pevent_find_any_field(struct event_format *event, const char *name)
3076 struct format_field *format;
3078 format = pevent_find_common_field(event, name);
3081 return pevent_find_field(event, name);
3085 * pevent_read_number - read a number from data
3086 * @pevent: handle for the pevent
3087 * @ptr: the raw data
3088 * @size: the size of the data that holds the number
3090 * Returns the number (converted to host) from the
3093 unsigned long long pevent_read_number(struct pevent *pevent,
3094 const void *ptr, int size)
3098 return *(unsigned char *)ptr;
3100 return data2host2(pevent, ptr);
3102 return data2host4(pevent, ptr);
3104 return data2host8(pevent, ptr);
3112 * pevent_read_number_field - read a number from data
3113 * @field: a handle to the field
3114 * @data: the raw data to read
3115 * @value: the value to place the number in
3117 * Reads raw data according to a field offset and size,
3118 * and translates it into @value.
3120 * Returns 0 on success, -1 otherwise.
3122 int pevent_read_number_field(struct format_field *field, const void *data,
3123 unsigned long long *value)
3127 switch (field->size) {
3132 *value = pevent_read_number(field->event->pevent,
3133 data + field->offset, field->size);
3140 static int get_common_info(struct pevent *pevent,
3141 const char *type, int *offset, int *size)
3143 struct event_format *event;
3144 struct format_field *field;
3147 * All events should have the same common elements.
3148 * Pick any event to find where the type is;
3150 if (!pevent->events) {
3151 do_warning("no event_list!");
3155 event = pevent->events[0];
3156 field = pevent_find_common_field(event, type);
3160 *offset = field->offset;
3161 *size = field->size;
3166 static int __parse_common(struct pevent *pevent, void *data,
3167 int *size, int *offset, const char *name)
3172 ret = get_common_info(pevent, name, offset, size);
3176 return pevent_read_number(pevent, data + *offset, *size);
3179 static int trace_parse_common_type(struct pevent *pevent, void *data)
3181 return __parse_common(pevent, data,
3182 &pevent->type_size, &pevent->type_offset,
3186 static int parse_common_pid(struct pevent *pevent, void *data)
3188 return __parse_common(pevent, data,
3189 &pevent->pid_size, &pevent->pid_offset,
3193 static int parse_common_pc(struct pevent *pevent, void *data)
3195 return __parse_common(pevent, data,
3196 &pevent->pc_size, &pevent->pc_offset,
3197 "common_preempt_count");
3200 static int parse_common_flags(struct pevent *pevent, void *data)
3202 return __parse_common(pevent, data,
3203 &pevent->flags_size, &pevent->flags_offset,
3207 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3209 return __parse_common(pevent, data,
3210 &pevent->ld_size, &pevent->ld_offset,
3211 "common_lock_depth");
3214 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3216 return __parse_common(pevent, data,
3217 &pevent->ld_size, &pevent->ld_offset,
3218 "common_migrate_disable");
3221 static int events_id_cmp(const void *a, const void *b);
3224 * pevent_find_event - find an event by given id
3225 * @pevent: a handle to the pevent
3226 * @id: the id of the event
3228 * Returns an event that has a given @id.
3230 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3232 struct event_format **eventptr;
3233 struct event_format key;
3234 struct event_format *pkey = &key;
3236 /* Check cache first */
3237 if (pevent->last_event && pevent->last_event->id == id)
3238 return pevent->last_event;
3242 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3243 sizeof(*pevent->events), events_id_cmp);
3246 pevent->last_event = *eventptr;
3254 * pevent_find_event_by_name - find an event by given name
3255 * @pevent: a handle to the pevent
3256 * @sys: the system name to search for
3257 * @name: the name of the event to search for
3259 * This returns an event with a given @name and under the system
3260 * @sys. If @sys is NULL the first event with @name is returned.
3262 struct event_format *
3263 pevent_find_event_by_name(struct pevent *pevent,
3264 const char *sys, const char *name)
3266 struct event_format *event;
3269 if (pevent->last_event &&
3270 strcmp(pevent->last_event->name, name) == 0 &&
3271 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3272 return pevent->last_event;
3274 for (i = 0; i < pevent->nr_events; i++) {
3275 event = pevent->events[i];
3276 if (strcmp(event->name, name) == 0) {
3279 if (strcmp(event->system, sys) == 0)
3283 if (i == pevent->nr_events)
3286 pevent->last_event = event;
3290 static unsigned long long
3291 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3293 struct pevent *pevent = event->pevent;
3294 unsigned long long val = 0;
3295 unsigned long long left, right;
3296 struct print_arg *typearg = NULL;
3297 struct print_arg *larg;
3298 unsigned long offset;
3299 unsigned int field_size;
3301 switch (arg->type) {
3306 return strtoull(arg->atom.atom, NULL, 0);
3308 if (!arg->field.field) {
3309 arg->field.field = pevent_find_any_field(event, arg->field.name);
3310 if (!arg->field.field)
3311 goto out_warning_field;
3314 /* must be a number */
3315 val = pevent_read_number(pevent, data + arg->field.field->offset,
3316 arg->field.field->size);
3323 val = eval_num_arg(data, size, event, arg->typecast.item);
3324 return eval_type(val, arg, 0);
3331 val = process_defined_func(&s, data, size, event, arg);
3332 trace_seq_destroy(&s);
3336 if (strcmp(arg->op.op, "[") == 0) {
3338 * Arrays are special, since we don't want
3339 * to read the arg as is.
3341 right = eval_num_arg(data, size, event, arg->op.right);
3343 /* handle typecasts */
3344 larg = arg->op.left;
3345 while (larg->type == PRINT_TYPE) {
3348 larg = larg->typecast.item;
3351 /* Default to long size */
3352 field_size = pevent->long_size;
3354 switch (larg->type) {
3355 case PRINT_DYNAMIC_ARRAY:
3356 offset = pevent_read_number(pevent,
3357 data + larg->dynarray.field->offset,
3358 larg->dynarray.field->size);
3359 if (larg->dynarray.field->elementsize)
3360 field_size = larg->dynarray.field->elementsize;
3362 * The actual length of the dynamic array is stored
3363 * in the top half of the field, and the offset
3364 * is in the bottom half of the 32 bit field.
3370 if (!larg->field.field) {
3372 pevent_find_any_field(event, larg->field.name);
3373 if (!larg->field.field) {
3375 goto out_warning_field;
3378 field_size = larg->field.field->elementsize;
3379 offset = larg->field.field->offset +
3380 right * larg->field.field->elementsize;
3383 goto default_op; /* oops, all bets off */
3385 val = pevent_read_number(pevent,
3386 data + offset, field_size);
3388 val = eval_type(val, typearg, 1);
3390 } else if (strcmp(arg->op.op, "?") == 0) {
3391 left = eval_num_arg(data, size, event, arg->op.left);
3392 arg = arg->op.right;
3394 val = eval_num_arg(data, size, event, arg->op.left);
3396 val = eval_num_arg(data, size, event, arg->op.right);
3400 left = eval_num_arg(data, size, event, arg->op.left);
3401 right = eval_num_arg(data, size, event, arg->op.right);
3402 switch (arg->op.op[0]) {
3404 switch (arg->op.op[1]) {
3409 val = left != right;
3412 goto out_warning_op;
3420 val = left || right;
3426 val = left && right;
3431 switch (arg->op.op[1]) {
3436 val = left << right;
3439 val = left <= right;
3442 goto out_warning_op;
3446 switch (arg->op.op[1]) {
3451 val = left >> right;
3454 val = left >= right;
3457 goto out_warning_op;
3461 if (arg->op.op[1] != '=')
3462 goto out_warning_op;
3464 val = left == right;
3479 goto out_warning_op;
3482 case PRINT_DYNAMIC_ARRAY:
3483 /* Without [], we pass the address to the dynamic data */
3484 offset = pevent_read_number(pevent,
3485 data + arg->dynarray.field->offset,
3486 arg->dynarray.field->size);
3488 * The actual length of the dynamic array is stored
3489 * in the top half of the field, and the offset
3490 * is in the bottom half of the 32 bit field.
3493 val = (unsigned long long)((unsigned long)data + offset);
3495 default: /* not sure what to do there */
3501 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3505 do_warning_event(event, "%s: field %s not found",
3506 __func__, arg->field.name);
3512 unsigned long long value;
3515 static const struct flag flags[] = {
3516 { "HI_SOFTIRQ", 0 },
3517 { "TIMER_SOFTIRQ", 1 },
3518 { "NET_TX_SOFTIRQ", 2 },
3519 { "NET_RX_SOFTIRQ", 3 },
3520 { "BLOCK_SOFTIRQ", 4 },
3521 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3522 { "TASKLET_SOFTIRQ", 6 },
3523 { "SCHED_SOFTIRQ", 7 },
3524 { "HRTIMER_SOFTIRQ", 8 },
3525 { "RCU_SOFTIRQ", 9 },
3527 { "HRTIMER_NORESTART", 0 },
3528 { "HRTIMER_RESTART", 1 },
3531 static unsigned long long eval_flag(const char *flag)
3536 * Some flags in the format files do not get converted.
3537 * If the flag is not numeric, see if it is something that
3538 * we already know about.
3540 if (isdigit(flag[0]))
3541 return strtoull(flag, NULL, 0);
3543 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3544 if (strcmp(flags[i].name, flag) == 0)
3545 return flags[i].value;
3550 static void print_str_to_seq(struct trace_seq *s, const char *format,
3551 int len_arg, const char *str)
3554 trace_seq_printf(s, format, len_arg, str);
3556 trace_seq_printf(s, format, str);
3559 static void print_str_arg(struct trace_seq *s, void *data, int size,
3560 struct event_format *event, const char *format,
3561 int len_arg, struct print_arg *arg)
3563 struct pevent *pevent = event->pevent;
3564 struct print_flag_sym *flag;
3565 struct format_field *field;
3566 struct printk_map *printk;
3567 unsigned long long val, fval;
3574 switch (arg->type) {
3579 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3582 field = arg->field.field;
3584 field = pevent_find_any_field(event, arg->field.name);
3586 str = arg->field.name;
3587 goto out_warning_field;
3589 arg->field.field = field;
3591 /* Zero sized fields, mean the rest of the data */
3592 len = field->size ? : size - field->offset;
3595 * Some events pass in pointers. If this is not an array
3596 * and the size is the same as long_size, assume that it
3599 if (!(field->flags & FIELD_IS_ARRAY) &&
3600 field->size == pevent->long_size) {
3601 addr = *(unsigned long *)(data + field->offset);
3602 /* Check if it matches a print format */
3603 printk = find_printk(pevent, addr);
3605 trace_seq_puts(s, printk->printk);
3607 trace_seq_printf(s, "%lx", addr);
3610 str = malloc(len + 1);
3612 do_warning_event(event, "%s: not enough memory!",
3616 memcpy(str, data + field->offset, len);
3618 print_str_to_seq(s, format, len_arg, str);
3622 val = eval_num_arg(data, size, event, arg->flags.field);
3624 for (flag = arg->flags.flags; flag; flag = flag->next) {
3625 fval = eval_flag(flag->value);
3626 if (!val && !fval) {
3627 print_str_to_seq(s, format, len_arg, flag->str);
3630 if (fval && (val & fval) == fval) {
3631 if (print && arg->flags.delim)
3632 trace_seq_puts(s, arg->flags.delim);
3633 print_str_to_seq(s, format, len_arg, flag->str);
3640 val = eval_num_arg(data, size, event, arg->symbol.field);
3641 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3642 fval = eval_flag(flag->value);
3644 print_str_to_seq(s, format, len_arg, flag->str);
3650 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3651 unsigned long offset;
3652 offset = pevent_read_number(pevent,
3653 data + arg->hex.field->dynarray.field->offset,
3654 arg->hex.field->dynarray.field->size);
3655 hex = data + (offset & 0xffff);
3657 field = arg->hex.field->field.field;
3659 str = arg->hex.field->field.name;
3660 field = pevent_find_any_field(event, str);
3662 goto out_warning_field;
3663 arg->hex.field->field.field = field;
3665 hex = data + field->offset;
3667 len = eval_num_arg(data, size, event, arg->hex.size);
3668 for (i = 0; i < len; i++) {
3670 trace_seq_putc(s, ' ');
3671 trace_seq_printf(s, "%02x", hex[i]);
3677 case PRINT_STRING: {
3680 if (arg->string.offset == -1) {
3681 struct format_field *f;
3683 f = pevent_find_any_field(event, arg->string.string);
3684 arg->string.offset = f->offset;
3686 str_offset = data2host4(pevent, data + arg->string.offset);
3687 str_offset &= 0xffff;
3688 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3692 print_str_to_seq(s, format, len_arg, arg->string.string);
3696 * The only op for string should be ? :
3698 if (arg->op.op[0] != '?')
3700 val = eval_num_arg(data, size, event, arg->op.left);
3702 print_str_arg(s, data, size, event,
3703 format, len_arg, arg->op.right->op.left);
3705 print_str_arg(s, data, size, event,
3706 format, len_arg, arg->op.right->op.right);
3709 process_defined_func(s, data, size, event, arg);
3719 do_warning_event(event, "%s: field %s not found",
3720 __func__, arg->field.name);
3723 static unsigned long long
3724 process_defined_func(struct trace_seq *s, void *data, int size,
3725 struct event_format *event, struct print_arg *arg)
3727 struct pevent_function_handler *func_handle = arg->func.func;
3728 struct pevent_func_params *param;
3729 unsigned long long *args;
3730 unsigned long long ret;
3731 struct print_arg *farg;
3732 struct trace_seq str;
3734 struct save_str *next;
3736 } *strings = NULL, *string;
3739 if (!func_handle->nr_args) {
3740 ret = (*func_handle->func)(s, NULL);
3744 farg = arg->func.args;
3745 param = func_handle->params;
3748 args = malloc(sizeof(*args) * func_handle->nr_args);
3752 for (i = 0; i < func_handle->nr_args; i++) {
3753 switch (param->type) {
3754 case PEVENT_FUNC_ARG_INT:
3755 case PEVENT_FUNC_ARG_LONG:
3756 case PEVENT_FUNC_ARG_PTR:
3757 args[i] = eval_num_arg(data, size, event, farg);
3759 case PEVENT_FUNC_ARG_STRING:
3760 trace_seq_init(&str);
3761 print_str_arg(&str, data, size, event, "%s", -1, farg);
3762 trace_seq_terminate(&str);
3763 string = malloc(sizeof(*string));
3765 do_warning_event(event, "%s(%d): malloc str",
3766 __func__, __LINE__);
3769 string->next = strings;
3770 string->str = strdup(str.buffer);
3773 do_warning_event(event, "%s(%d): malloc str",
3774 __func__, __LINE__);
3777 args[i] = (uintptr_t)string->str;
3779 trace_seq_destroy(&str);
3783 * Something went totally wrong, this is not
3784 * an input error, something in this code broke.
3786 do_warning_event(event, "Unexpected end of arguments\n");
3790 param = param->next;
3793 ret = (*func_handle->func)(s, args);
3798 strings = string->next;
3804 /* TBD : handle return type here */
3808 static void free_args(struct print_arg *args)
3810 struct print_arg *next;
3820 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3822 struct pevent *pevent = event->pevent;
3823 struct format_field *field, *ip_field;
3824 struct print_arg *args, *arg, **next;
3825 unsigned long long ip, val;
3830 field = pevent->bprint_buf_field;
3831 ip_field = pevent->bprint_ip_field;
3834 field = pevent_find_field(event, "buf");
3836 do_warning_event(event, "can't find buffer field for binary printk");
3839 ip_field = pevent_find_field(event, "ip");
3841 do_warning_event(event, "can't find ip field for binary printk");
3844 pevent->bprint_buf_field = field;
3845 pevent->bprint_ip_field = ip_field;
3848 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3851 * The first arg is the IP pointer.
3855 do_warning_event(event, "%s(%d): not enough memory!",
3856 __func__, __LINE__);
3863 arg->type = PRINT_ATOM;
3865 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
3868 /* skip the first "%pf: " */
3869 for (ptr = fmt + 5, bptr = data + field->offset;
3870 bptr < data + size && *ptr; ptr++) {
3901 vsize = pevent->long_size;
3915 /* the pointers are always 4 bytes aligned */
3916 bptr = (void *)(((unsigned long)bptr + 3) &
3918 val = pevent_read_number(pevent, bptr, vsize);
3922 do_warning_event(event, "%s(%d): not enough memory!",
3923 __func__, __LINE__);
3927 arg->type = PRINT_ATOM;
3928 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
3935 * The '*' case means that an arg is used as the length.
3936 * We need to continue to figure out for what.
3945 do_warning_event(event, "%s(%d): not enough memory!",
3946 __func__, __LINE__);
3950 arg->type = PRINT_BSTRING;
3951 arg->string.string = strdup(bptr);
3952 if (!arg->string.string)
3954 bptr += strlen(bptr) + 1;
3971 get_bprint_format(void *data, int size __maybe_unused,
3972 struct event_format *event)
3974 struct pevent *pevent = event->pevent;
3975 unsigned long long addr;
3976 struct format_field *field;
3977 struct printk_map *printk;
3980 field = pevent->bprint_fmt_field;
3983 field = pevent_find_field(event, "fmt");
3985 do_warning_event(event, "can't find format field for binary printk");
3988 pevent->bprint_fmt_field = field;
3991 addr = pevent_read_number(pevent, data + field->offset, field->size);
3993 printk = find_printk(pevent, addr);
3995 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4000 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4006 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4007 struct event_format *event, struct print_arg *arg)
4010 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4012 if (arg->type == PRINT_FUNC) {
4013 process_defined_func(s, data, size, event, arg);
4017 if (arg->type != PRINT_FIELD) {
4018 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4024 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4025 if (!arg->field.field) {
4027 pevent_find_any_field(event, arg->field.name);
4028 if (!arg->field.field) {
4029 do_warning_event(event, "%s: field %s not found",
4030 __func__, arg->field.name);
4034 if (arg->field.field->size != 6) {
4035 trace_seq_printf(s, "INVALIDMAC");
4038 buf = data + arg->field.field->offset;
4039 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4042 static int is_printable_array(char *p, unsigned int len)
4046 for (i = 0; i < len && p[i]; i++)
4047 if (!isprint(p[i]) && !isspace(p[i]))
4052 static void print_event_fields(struct trace_seq *s, void *data,
4053 int size __maybe_unused,
4054 struct event_format *event)
4056 struct format_field *field;
4057 unsigned long long val;
4058 unsigned int offset, len, i;
4060 field = event->format.fields;
4062 trace_seq_printf(s, " %s=", field->name);
4063 if (field->flags & FIELD_IS_ARRAY) {
4064 offset = field->offset;
4066 if (field->flags & FIELD_IS_DYNAMIC) {
4067 val = pevent_read_number(event->pevent, data + offset, len);
4072 if (field->flags & FIELD_IS_STRING &&
4073 is_printable_array(data + offset, len)) {
4074 trace_seq_printf(s, "%s", (char *)data + offset);
4076 trace_seq_puts(s, "ARRAY[");
4077 for (i = 0; i < len; i++) {
4079 trace_seq_puts(s, ", ");
4080 trace_seq_printf(s, "%02x",
4081 *((unsigned char *)data + offset + i));
4083 trace_seq_putc(s, ']');
4084 field->flags &= ~FIELD_IS_STRING;
4087 val = pevent_read_number(event->pevent, data + field->offset,
4089 if (field->flags & FIELD_IS_POINTER) {
4090 trace_seq_printf(s, "0x%llx", val);
4091 } else if (field->flags & FIELD_IS_SIGNED) {
4092 switch (field->size) {
4095 * If field is long then print it in hex.
4096 * A long usually stores pointers.
4098 if (field->flags & FIELD_IS_LONG)
4099 trace_seq_printf(s, "0x%x", (int)val);
4101 trace_seq_printf(s, "%d", (int)val);
4104 trace_seq_printf(s, "%2d", (short)val);
4107 trace_seq_printf(s, "%1d", (char)val);
4110 trace_seq_printf(s, "%lld", val);
4113 if (field->flags & FIELD_IS_LONG)
4114 trace_seq_printf(s, "0x%llx", val);
4116 trace_seq_printf(s, "%llu", val);
4119 field = field->next;
4123 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4125 struct pevent *pevent = event->pevent;
4126 struct print_fmt *print_fmt = &event->print_fmt;
4127 struct print_arg *arg = print_fmt->args;
4128 struct print_arg *args = NULL;
4129 const char *ptr = print_fmt->format;
4130 unsigned long long val;
4131 struct func_map *func;
4132 const char *saveptr;
4134 char *bprint_fmt = NULL;
4142 if (event->flags & EVENT_FL_FAILED) {
4143 trace_seq_printf(s, "[FAILED TO PARSE]");
4144 print_event_fields(s, data, size, event);
4148 if (event->flags & EVENT_FL_ISBPRINT) {
4149 bprint_fmt = get_bprint_format(data, size, event);
4150 args = make_bprint_args(bprint_fmt, data, size, event);
4155 for (; *ptr; ptr++) {
4161 trace_seq_putc(s, '\n');
4164 trace_seq_putc(s, '\t');
4167 trace_seq_putc(s, '\r');
4170 trace_seq_putc(s, '\\');
4173 trace_seq_putc(s, *ptr);
4177 } else if (*ptr == '%') {
4185 trace_seq_putc(s, '%');
4188 /* FIXME: need to handle properly */
4200 /* The argument is the length. */
4202 do_warning_event(event, "no argument match");
4203 event->flags |= EVENT_FL_FAILED;
4206 len_arg = eval_num_arg(data, size, event, arg);
4216 if (pevent->long_size == 4)
4221 if (*(ptr+1) == 'F' ||
4225 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4226 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4239 do_warning_event(event, "no argument match");
4240 event->flags |= EVENT_FL_FAILED;
4244 len = ((unsigned long)ptr + 1) -
4245 (unsigned long)saveptr;
4247 /* should never happen */
4249 do_warning_event(event, "bad format!");
4250 event->flags |= EVENT_FL_FAILED;
4254 memcpy(format, saveptr, len);
4257 val = eval_num_arg(data, size, event, arg);
4261 func = find_func(pevent, val);
4263 trace_seq_puts(s, func->func);
4264 if (show_func == 'F')
4271 if (pevent->long_size == 8 && ls &&
4272 sizeof(long) != 8) {
4276 /* make %l into %ll */
4277 p = strchr(format, 'l');
4279 memmove(p+1, p, strlen(p)+1);
4280 else if (strcmp(format, "%p") == 0)
4281 strcpy(format, "0x%llx");
4286 trace_seq_printf(s, format, len_arg, (char)val);
4288 trace_seq_printf(s, format, (char)val);
4292 trace_seq_printf(s, format, len_arg, (short)val);
4294 trace_seq_printf(s, format, (short)val);
4298 trace_seq_printf(s, format, len_arg, (int)val);
4300 trace_seq_printf(s, format, (int)val);
4304 trace_seq_printf(s, format, len_arg, (long)val);
4306 trace_seq_printf(s, format, (long)val);
4310 trace_seq_printf(s, format, len_arg,
4313 trace_seq_printf(s, format, (long long)val);
4316 do_warning_event(event, "bad count (%d)", ls);
4317 event->flags |= EVENT_FL_FAILED;
4322 do_warning_event(event, "no matching argument");
4323 event->flags |= EVENT_FL_FAILED;
4327 len = ((unsigned long)ptr + 1) -
4328 (unsigned long)saveptr;
4330 /* should never happen */
4332 do_warning_event(event, "bad format!");
4333 event->flags |= EVENT_FL_FAILED;
4337 memcpy(format, saveptr, len);
4341 /* Use helper trace_seq */
4343 print_str_arg(&p, data, size, event,
4344 format, len_arg, arg);
4345 trace_seq_terminate(&p);
4346 trace_seq_puts(s, p.buffer);
4347 trace_seq_destroy(&p);
4351 trace_seq_printf(s, ">%c<", *ptr);
4355 trace_seq_putc(s, *ptr);
4358 if (event->flags & EVENT_FL_FAILED) {
4360 trace_seq_printf(s, "[FAILED TO PARSE]");
4370 * pevent_data_lat_fmt - parse the data for the latency format
4371 * @pevent: a handle to the pevent
4372 * @s: the trace_seq to write to
4373 * @record: the record to read from
4375 * This parses out the Latency format (interrupts disabled,
4376 * need rescheduling, in hard/soft interrupt, preempt count
4377 * and lock depth) and places it into the trace_seq.
4379 void pevent_data_lat_fmt(struct pevent *pevent,
4380 struct trace_seq *s, struct pevent_record *record)
4382 static int check_lock_depth = 1;
4383 static int check_migrate_disable = 1;
4384 static int lock_depth_exists;
4385 static int migrate_disable_exists;
4386 unsigned int lat_flags;
4389 int migrate_disable;
4392 void *data = record->data;
4394 lat_flags = parse_common_flags(pevent, data);
4395 pc = parse_common_pc(pevent, data);
4396 /* lock_depth may not always exist */
4397 if (lock_depth_exists)
4398 lock_depth = parse_common_lock_depth(pevent, data);
4399 else if (check_lock_depth) {
4400 lock_depth = parse_common_lock_depth(pevent, data);
4402 check_lock_depth = 0;
4404 lock_depth_exists = 1;
4407 /* migrate_disable may not always exist */
4408 if (migrate_disable_exists)
4409 migrate_disable = parse_common_migrate_disable(pevent, data);
4410 else if (check_migrate_disable) {
4411 migrate_disable = parse_common_migrate_disable(pevent, data);
4412 if (migrate_disable < 0)
4413 check_migrate_disable = 0;
4415 migrate_disable_exists = 1;
4418 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4419 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4421 trace_seq_printf(s, "%c%c%c",
4422 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4423 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4425 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4427 (hardirq && softirq) ? 'H' :
4428 hardirq ? 'h' : softirq ? 's' : '.');
4431 trace_seq_printf(s, "%x", pc);
4433 trace_seq_putc(s, '.');
4435 if (migrate_disable_exists) {
4436 if (migrate_disable < 0)
4437 trace_seq_putc(s, '.');
4439 trace_seq_printf(s, "%d", migrate_disable);
4442 if (lock_depth_exists) {
4444 trace_seq_putc(s, '.');
4446 trace_seq_printf(s, "%d", lock_depth);
4449 trace_seq_terminate(s);
4453 * pevent_data_type - parse out the given event type
4454 * @pevent: a handle to the pevent
4455 * @rec: the record to read from
4457 * This returns the event id from the @rec.
4459 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4461 return trace_parse_common_type(pevent, rec->data);
4465 * pevent_data_event_from_type - find the event by a given type
4466 * @pevent: a handle to the pevent
4467 * @type: the type of the event.
4469 * This returns the event form a given @type;
4471 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4473 return pevent_find_event(pevent, type);
4477 * pevent_data_pid - parse the PID from raw data
4478 * @pevent: a handle to the pevent
4479 * @rec: the record to parse
4481 * This returns the PID from a raw data.
4483 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4485 return parse_common_pid(pevent, rec->data);
4489 * pevent_data_comm_from_pid - return the command line from PID
4490 * @pevent: a handle to the pevent
4491 * @pid: the PID of the task to search for
4493 * This returns a pointer to the command line that has the given
4496 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4500 comm = find_cmdline(pevent, pid);
4505 * pevent_data_comm_from_pid - parse the data into the print format
4506 * @s: the trace_seq to write to
4507 * @event: the handle to the event
4508 * @record: the record to read from
4510 * This parses the raw @data using the given @event information and
4511 * writes the print format into the trace_seq.
4513 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4514 struct pevent_record *record)
4516 int print_pretty = 1;
4518 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
4519 print_event_fields(s, record->data, record->size, event);
4522 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
4523 print_pretty = event->handler(s, record, event,
4527 pretty_print(s, record->data, record->size, event);
4530 trace_seq_terminate(s);
4533 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
4535 if (!use_trace_clock)
4538 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
4539 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
4542 /* trace_clock is setting in tsc or counter mode */
4546 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4547 struct pevent_record *record, bool use_trace_clock)
4549 static const char *spaces = " "; /* 20 spaces */
4550 struct event_format *event;
4552 unsigned long usecs;
4553 unsigned long nsecs;
4555 void *data = record->data;
4560 bool use_usec_format;
4562 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
4564 if (use_usec_format) {
4565 secs = record->ts / NSECS_PER_SEC;
4566 nsecs = record->ts - secs * NSECS_PER_SEC;
4569 if (record->size < 0) {
4570 do_warning("ug! negative record size %d", record->size);
4574 type = trace_parse_common_type(pevent, data);
4576 event = pevent_find_event(pevent, type);
4578 do_warning("ug! no event found for type %d", type);
4582 pid = parse_common_pid(pevent, data);
4583 comm = find_cmdline(pevent, pid);
4585 if (pevent->latency_format) {
4586 trace_seq_printf(s, "%8.8s-%-5d %3d",
4587 comm, pid, record->cpu);
4588 pevent_data_lat_fmt(pevent, s, record);
4590 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4592 if (use_usec_format) {
4593 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4597 usecs = (nsecs + 500) / NSECS_PER_USEC;
4601 trace_seq_printf(s, " %5lu.%0*lu: %s: ",
4602 secs, p, usecs, event->name);
4604 trace_seq_printf(s, " %12llu: %s: ",
4605 record->ts, event->name);
4607 /* Space out the event names evenly. */
4608 len = strlen(event->name);
4610 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4612 pevent_event_info(s, event, record);
4615 static int events_id_cmp(const void *a, const void *b)
4617 struct event_format * const * ea = a;
4618 struct event_format * const * eb = b;
4620 if ((*ea)->id < (*eb)->id)
4623 if ((*ea)->id > (*eb)->id)
4629 static int events_name_cmp(const void *a, const void *b)
4631 struct event_format * const * ea = a;
4632 struct event_format * const * eb = b;
4635 res = strcmp((*ea)->name, (*eb)->name);
4639 res = strcmp((*ea)->system, (*eb)->system);
4643 return events_id_cmp(a, b);
4646 static int events_system_cmp(const void *a, const void *b)
4648 struct event_format * const * ea = a;
4649 struct event_format * const * eb = b;
4652 res = strcmp((*ea)->system, (*eb)->system);
4656 res = strcmp((*ea)->name, (*eb)->name);
4660 return events_id_cmp(a, b);
4663 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4665 struct event_format **events;
4666 int (*sort)(const void *a, const void *b);
4668 events = pevent->sort_events;
4670 if (events && pevent->last_type == sort_type)
4674 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4678 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4679 events[pevent->nr_events] = NULL;
4681 pevent->sort_events = events;
4683 /* the internal events are sorted by id */
4684 if (sort_type == EVENT_SORT_ID) {
4685 pevent->last_type = sort_type;
4690 switch (sort_type) {
4692 sort = events_id_cmp;
4694 case EVENT_SORT_NAME:
4695 sort = events_name_cmp;
4697 case EVENT_SORT_SYSTEM:
4698 sort = events_system_cmp;
4704 qsort(events, pevent->nr_events, sizeof(*events), sort);
4705 pevent->last_type = sort_type;
4710 static struct format_field **
4711 get_event_fields(const char *type, const char *name,
4712 int count, struct format_field *list)
4714 struct format_field **fields;
4715 struct format_field *field;
4718 fields = malloc(sizeof(*fields) * (count + 1));
4722 for (field = list; field; field = field->next) {
4723 fields[i++] = field;
4724 if (i == count + 1) {
4725 do_warning("event %s has more %s fields than specified",
4733 do_warning("event %s has less %s fields than specified",
4742 * pevent_event_common_fields - return a list of common fields for an event
4743 * @event: the event to return the common fields of.
4745 * Returns an allocated array of fields. The last item in the array is NULL.
4746 * The array must be freed with free().
4748 struct format_field **pevent_event_common_fields(struct event_format *event)
4750 return get_event_fields("common", event->name,
4751 event->format.nr_common,
4752 event->format.common_fields);
4756 * pevent_event_fields - return a list of event specific fields for an event
4757 * @event: the event to return the fields of.
4759 * Returns an allocated array of fields. The last item in the array is NULL.
4760 * The array must be freed with free().
4762 struct format_field **pevent_event_fields(struct event_format *event)
4764 return get_event_fields("event", event->name,
4765 event->format.nr_fields,
4766 event->format.fields);
4769 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4771 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4773 trace_seq_puts(s, ", ");
4774 print_fields(s, field->next);
4779 static void print_args(struct print_arg *args)
4781 int print_paren = 1;
4784 switch (args->type) {
4789 printf("%s", args->atom.atom);
4792 printf("REC->%s", args->field.name);
4795 printf("__print_flags(");
4796 print_args(args->flags.field);
4797 printf(", %s, ", args->flags.delim);
4799 print_fields(&s, args->flags.flags);
4800 trace_seq_do_printf(&s);
4801 trace_seq_destroy(&s);
4805 printf("__print_symbolic(");
4806 print_args(args->symbol.field);
4809 print_fields(&s, args->symbol.symbols);
4810 trace_seq_do_printf(&s);
4811 trace_seq_destroy(&s);
4815 printf("__print_hex(");
4816 print_args(args->hex.field);
4818 print_args(args->hex.size);
4823 printf("__get_str(%s)", args->string.string);
4826 printf("(%s)", args->typecast.type);
4827 print_args(args->typecast.item);
4830 if (strcmp(args->op.op, ":") == 0)
4834 print_args(args->op.left);
4835 printf(" %s ", args->op.op);
4836 print_args(args->op.right);
4841 /* we should warn... */
4846 print_args(args->next);
4850 static void parse_header_field(const char *field,
4851 int *offset, int *size, int mandatory)
4853 unsigned long long save_input_buf_ptr;
4854 unsigned long long save_input_buf_siz;
4858 save_input_buf_ptr = input_buf_ptr;
4859 save_input_buf_siz = input_buf_siz;
4861 if (read_expected(EVENT_ITEM, "field") < 0)
4863 if (read_expected(EVENT_OP, ":") < 0)
4867 if (read_expect_type(EVENT_ITEM, &token) < 0)
4872 * If this is not a mandatory field, then test it first.
4875 if (read_expected(EVENT_ITEM, field) < 0)
4878 if (read_expect_type(EVENT_ITEM, &token) < 0)
4880 if (strcmp(token, field) != 0)
4885 if (read_expected(EVENT_OP, ";") < 0)
4887 if (read_expected(EVENT_ITEM, "offset") < 0)
4889 if (read_expected(EVENT_OP, ":") < 0)
4891 if (read_expect_type(EVENT_ITEM, &token) < 0)
4893 *offset = atoi(token);
4895 if (read_expected(EVENT_OP, ";") < 0)
4897 if (read_expected(EVENT_ITEM, "size") < 0)
4899 if (read_expected(EVENT_OP, ":") < 0)
4901 if (read_expect_type(EVENT_ITEM, &token) < 0)
4903 *size = atoi(token);
4905 if (read_expected(EVENT_OP, ";") < 0)
4907 type = read_token(&token);
4908 if (type != EVENT_NEWLINE) {
4909 /* newer versions of the kernel have a "signed" type */
4910 if (type != EVENT_ITEM)
4913 if (strcmp(token, "signed") != 0)
4918 if (read_expected(EVENT_OP, ":") < 0)
4921 if (read_expect_type(EVENT_ITEM, &token))
4925 if (read_expected(EVENT_OP, ";") < 0)
4928 if (read_expect_type(EVENT_NEWLINE, &token))
4936 input_buf_ptr = save_input_buf_ptr;
4937 input_buf_siz = save_input_buf_siz;
4944 * pevent_parse_header_page - parse the data stored in the header page
4945 * @pevent: the handle to the pevent
4946 * @buf: the buffer storing the header page format string
4947 * @size: the size of @buf
4948 * @long_size: the long size to use if there is no header
4950 * This parses the header page format for information on the
4951 * ring buffer used. The @buf should be copied from
4953 * /sys/kernel/debug/tracing/events/header_page
4955 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4962 * Old kernels did not have header page info.
4963 * Sorry but we just use what we find here in user space.
4965 pevent->header_page_ts_size = sizeof(long long);
4966 pevent->header_page_size_size = long_size;
4967 pevent->header_page_data_offset = sizeof(long long) + long_size;
4968 pevent->old_format = 1;
4971 init_input_buf(buf, size);
4973 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4974 &pevent->header_page_ts_size, 1);
4975 parse_header_field("commit", &pevent->header_page_size_offset,
4976 &pevent->header_page_size_size, 1);
4977 parse_header_field("overwrite", &pevent->header_page_overwrite,
4979 parse_header_field("data", &pevent->header_page_data_offset,
4980 &pevent->header_page_data_size, 1);
4985 static int event_matches(struct event_format *event,
4986 int id, const char *sys_name,
4987 const char *event_name)
4989 if (id >= 0 && id != event->id)
4992 if (event_name && (strcmp(event_name, event->name) != 0))
4995 if (sys_name && (strcmp(sys_name, event->system) != 0))
5001 static void free_handler(struct event_handler *handle)
5003 free((void *)handle->sys_name);
5004 free((void *)handle->event_name);
5008 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5010 struct event_handler *handle, **next;
5012 for (next = &pevent->handlers; *next;
5013 next = &(*next)->next) {
5015 if (event_matches(event, handle->id,
5017 handle->event_name))
5024 pr_stat("overriding event (%d) %s:%s with new print handler",
5025 event->id, event->system, event->name);
5027 event->handler = handle->func;
5028 event->context = handle->context;
5030 *next = handle->next;
5031 free_handler(handle);
5037 * __pevent_parse_format - parse the event format
5038 * @buf: the buffer storing the event format string
5039 * @size: the size of @buf
5040 * @sys: the system the event belongs to
5042 * This parses the event format and creates an event structure
5043 * to quickly parse raw data for a given event.
5045 * These files currently come from:
5047 * /sys/kernel/debug/tracing/events/.../.../format
5049 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5050 struct pevent *pevent, const char *buf,
5051 unsigned long size, const char *sys)
5053 struct event_format *event;
5056 init_input_buf(buf, size);
5058 *eventp = event = alloc_event();
5060 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5062 event->name = event_read_name();
5065 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5066 goto event_alloc_failed;
5069 if (strcmp(sys, "ftrace") == 0) {
5070 event->flags |= EVENT_FL_ISFTRACE;
5072 if (strcmp(event->name, "bprint") == 0)
5073 event->flags |= EVENT_FL_ISBPRINT;
5076 event->id = event_read_id();
5077 if (event->id < 0) {
5078 ret = PEVENT_ERRNO__READ_ID_FAILED;
5080 * This isn't an allocation error actually.
5081 * But as the ID is critical, just bail out.
5083 goto event_alloc_failed;
5086 event->system = strdup(sys);
5087 if (!event->system) {
5088 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5089 goto event_alloc_failed;
5092 /* Add pevent to event so that it can be referenced */
5093 event->pevent = pevent;
5095 ret = event_read_format(event);
5097 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5098 goto event_parse_failed;
5102 * If the event has an override, don't print warnings if the event
5103 * print format fails to parse.
5105 if (pevent && find_event_handle(pevent, event))
5108 ret = event_read_print(event);
5112 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
5113 goto event_parse_failed;
5116 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
5117 struct format_field *field;
5118 struct print_arg *arg, **list;
5120 /* old ftrace had no args */
5121 list = &event->print_fmt.args;
5122 for (field = event->format.fields; field; field = field->next) {
5125 event->flags |= EVENT_FL_FAILED;
5126 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5128 arg->type = PRINT_FIELD;
5129 arg->field.name = strdup(field->name);
5130 if (!arg->field.name) {
5131 event->flags |= EVENT_FL_FAILED;
5133 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
5135 arg->field.field = field;
5145 event->flags |= EVENT_FL_FAILED;
5149 free(event->system);
5156 static enum pevent_errno
5157 __pevent_parse_event(struct pevent *pevent,
5158 struct event_format **eventp,
5159 const char *buf, unsigned long size,
5162 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
5163 struct event_format *event = *eventp;
5168 if (pevent && add_event(pevent, event)) {
5169 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5170 goto event_add_failed;
5173 #define PRINT_ARGS 0
5174 if (PRINT_ARGS && event->print_fmt.args)
5175 print_args(event->print_fmt.args);
5180 pevent_free_format(event);
5185 * pevent_parse_format - parse the event format
5186 * @pevent: the handle to the pevent
5187 * @eventp: returned format
5188 * @buf: the buffer storing the event format string
5189 * @size: the size of @buf
5190 * @sys: the system the event belongs to
5192 * This parses the event format and creates an event structure
5193 * to quickly parse raw data for a given event.
5195 * These files currently come from:
5197 * /sys/kernel/debug/tracing/events/.../.../format
5199 enum pevent_errno pevent_parse_format(struct pevent *pevent,
5200 struct event_format **eventp,
5202 unsigned long size, const char *sys)
5204 return __pevent_parse_event(pevent, eventp, buf, size, sys);
5208 * pevent_parse_event - parse the event format
5209 * @pevent: the handle to the pevent
5210 * @buf: the buffer storing the event format string
5211 * @size: the size of @buf
5212 * @sys: the system the event belongs to
5214 * This parses the event format and creates an event structure
5215 * to quickly parse raw data for a given event.
5217 * These files currently come from:
5219 * /sys/kernel/debug/tracing/events/.../.../format
5221 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
5222 unsigned long size, const char *sys)
5224 struct event_format *event = NULL;
5225 return __pevent_parse_event(pevent, &event, buf, size, sys);
5229 #define _PE(code, str) str
5230 static const char * const pevent_error_str[] = {
5235 int pevent_strerror(struct pevent *pevent __maybe_unused,
5236 enum pevent_errno errnum, char *buf, size_t buflen)
5242 msg = strerror_r(errnum, buf, buflen);
5244 size_t len = strlen(msg);
5245 memcpy(buf, msg, min(buflen - 1, len));
5246 *(buf + min(buflen - 1, len)) = '\0';
5251 if (errnum <= __PEVENT_ERRNO__START ||
5252 errnum >= __PEVENT_ERRNO__END)
5255 idx = errnum - __PEVENT_ERRNO__START - 1;
5256 msg = pevent_error_str[idx];
5257 snprintf(buf, buflen, "%s", msg);
5262 int get_field_val(struct trace_seq *s, struct format_field *field,
5263 const char *name, struct pevent_record *record,
5264 unsigned long long *val, int err)
5268 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5272 if (pevent_read_number_field(field, record->data, val)) {
5274 trace_seq_printf(s, " %s=INVALID", name);
5282 * pevent_get_field_raw - return the raw pointer into the data field
5283 * @s: The seq to print to on error
5284 * @event: the event that the field is for
5285 * @name: The name of the field
5286 * @record: The record with the field name.
5287 * @len: place to store the field length.
5288 * @err: print default error if failed.
5290 * Returns a pointer into record->data of the field and places
5291 * the length of the field in @len.
5293 * On failure, it returns NULL.
5295 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
5296 const char *name, struct pevent_record *record,
5299 struct format_field *field;
5300 void *data = record->data;
5307 field = pevent_find_field(event, name);
5311 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
5315 /* Allow @len to be NULL */
5319 offset = field->offset;
5320 if (field->flags & FIELD_IS_DYNAMIC) {
5321 offset = pevent_read_number(event->pevent,
5322 data + offset, field->size);
5323 *len = offset >> 16;
5328 return data + offset;
5332 * pevent_get_field_val - find a field and return its value
5333 * @s: The seq to print to on error
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 * @val: place to store the value of the field.
5338 * @err: print default error if failed.
5340 * Returns 0 on success -1 on field not found.
5342 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
5343 const char *name, struct pevent_record *record,
5344 unsigned long long *val, int err)
5346 struct format_field *field;
5351 field = pevent_find_field(event, name);
5353 return get_field_val(s, field, name, record, val, err);
5357 * pevent_get_common_field_val - find a common field and return its value
5358 * @s: The seq to print to on error
5359 * @event: the event that the field is for
5360 * @name: The name of the field
5361 * @record: The record with the field name.
5362 * @val: place to store the value of the field.
5363 * @err: print default error if failed.
5365 * Returns 0 on success -1 on field not found.
5367 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
5368 const char *name, struct pevent_record *record,
5369 unsigned long long *val, int err)
5371 struct format_field *field;
5376 field = pevent_find_common_field(event, name);
5378 return get_field_val(s, field, name, record, val, err);
5382 * pevent_get_any_field_val - find a any field and return its value
5383 * @s: The seq to print to on error
5384 * @event: the event that the field is for
5385 * @name: The name of the field
5386 * @record: The record with the field name.
5387 * @val: place to store the value of the field.
5388 * @err: print default error if failed.
5390 * Returns 0 on success -1 on field not found.
5392 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
5393 const char *name, struct pevent_record *record,
5394 unsigned long long *val, int err)
5396 struct format_field *field;
5401 field = pevent_find_any_field(event, name);
5403 return get_field_val(s, field, name, record, val, err);
5407 * pevent_print_num_field - print a field and a format
5408 * @s: The seq to print to
5409 * @fmt: The printf format to print the field with.
5410 * @event: the event that the field is for
5411 * @name: The name of the field
5412 * @record: The record with the field name.
5413 * @err: print default error if failed.
5415 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5417 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
5418 struct event_format *event, const char *name,
5419 struct pevent_record *record, int err)
5421 struct format_field *field = pevent_find_field(event, name);
5422 unsigned long long val;
5427 if (pevent_read_number_field(field, record->data, &val))
5430 return trace_seq_printf(s, fmt, val);
5434 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5439 * pevent_print_func_field - print a field and a format for function pointers
5440 * @s: The seq to print to
5441 * @fmt: The printf format to print the field with.
5442 * @event: the event that the field is for
5443 * @name: The name of the field
5444 * @record: The record with the field name.
5445 * @err: print default error if failed.
5447 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5449 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
5450 struct event_format *event, const char *name,
5451 struct pevent_record *record, int err)
5453 struct format_field *field = pevent_find_field(event, name);
5454 struct pevent *pevent = event->pevent;
5455 unsigned long long val;
5456 struct func_map *func;
5462 if (pevent_read_number_field(field, record->data, &val))
5465 func = find_func(pevent, val);
5468 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
5470 sprintf(tmp, "0x%08llx", val);
5472 return trace_seq_printf(s, fmt, tmp);
5476 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
5480 static void free_func_handle(struct pevent_function_handler *func)
5482 struct pevent_func_params *params;
5486 while (func->params) {
5487 params = func->params;
5488 func->params = params->next;
5496 * pevent_register_print_function - register a helper function
5497 * @pevent: the handle to the pevent
5498 * @func: the function to process the helper function
5499 * @ret_type: the return type of the helper function
5500 * @name: the name of the helper function
5501 * @parameters: A list of enum pevent_func_arg_type
5503 * Some events may have helper functions in the print format arguments.
5504 * This allows a plugin to dynamically create a way to process one
5505 * of these functions.
5507 * The @parameters is a variable list of pevent_func_arg_type enums that
5508 * must end with PEVENT_FUNC_ARG_VOID.
5510 int pevent_register_print_function(struct pevent *pevent,
5511 pevent_func_handler func,
5512 enum pevent_func_arg_type ret_type,
5515 struct pevent_function_handler *func_handle;
5516 struct pevent_func_params **next_param;
5517 struct pevent_func_params *param;
5518 enum pevent_func_arg_type type;
5522 func_handle = find_func_handler(pevent, name);
5525 * This is most like caused by the users own
5526 * plugins updating the function. This overrides the
5529 pr_stat("override of function helper '%s'", name);
5530 remove_func_handler(pevent, name);
5533 func_handle = calloc(1, sizeof(*func_handle));
5535 do_warning("Failed to allocate function handler");
5536 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5539 func_handle->ret_type = ret_type;
5540 func_handle->name = strdup(name);
5541 func_handle->func = func;
5542 if (!func_handle->name) {
5543 do_warning("Failed to allocate function name");
5545 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5548 next_param = &(func_handle->params);
5551 type = va_arg(ap, enum pevent_func_arg_type);
5552 if (type == PEVENT_FUNC_ARG_VOID)
5555 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5556 do_warning("Invalid argument type %d", type);
5557 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
5561 param = malloc(sizeof(*param));
5563 do_warning("Failed to allocate function param");
5564 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5570 *next_param = param;
5571 next_param = &(param->next);
5573 func_handle->nr_args++;
5577 func_handle->next = pevent->func_handlers;
5578 pevent->func_handlers = func_handle;
5583 free_func_handle(func_handle);
5588 * pevent_unregister_print_function - unregister a helper function
5589 * @pevent: the handle to the pevent
5590 * @func: the function to process the helper function
5591 * @name: the name of the helper function
5593 * This function removes existing print handler for function @name.
5595 * Returns 0 if the handler was removed successully, -1 otherwise.
5597 int pevent_unregister_print_function(struct pevent *pevent,
5598 pevent_func_handler func, char *name)
5600 struct pevent_function_handler *func_handle;
5602 func_handle = find_func_handler(pevent, name);
5603 if (func_handle && func_handle->func == func) {
5604 remove_func_handler(pevent, name);
5610 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
5611 const char *sys_name,
5612 const char *event_name)
5614 struct event_format *event;
5618 event = pevent_find_event(pevent, id);
5621 if (event_name && (strcmp(event_name, event->name) != 0))
5623 if (sys_name && (strcmp(sys_name, event->system) != 0))
5626 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5634 * pevent_register_event_handler - register a way to parse an event
5635 * @pevent: the handle to the pevent
5636 * @id: the id of the event to register
5637 * @sys_name: the system name the event belongs to
5638 * @event_name: the name of the event
5639 * @func: the function to call to parse the event information
5640 * @context: the data to be passed to @func
5642 * This function allows a developer to override the parsing of
5643 * a given event. If for some reason the default print format
5644 * is not sufficient, this function will register a function
5645 * for an event to be used to parse the data instead.
5647 * If @id is >= 0, then it is used to find the event.
5648 * else @sys_name and @event_name are used.
5650 int pevent_register_event_handler(struct pevent *pevent, int id,
5651 const char *sys_name, const char *event_name,
5652 pevent_event_handler_func func, void *context)
5654 struct event_format *event;
5655 struct event_handler *handle;
5657 event = pevent_search_event(pevent, id, sys_name, event_name);
5661 pr_stat("overriding event (%d) %s:%s with new print handler",
5662 event->id, event->system, event->name);
5664 event->handler = func;
5665 event->context = context;
5669 /* Save for later use. */
5670 handle = calloc(1, sizeof(*handle));
5672 do_warning("Failed to allocate event handler");
5673 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5678 handle->event_name = strdup(event_name);
5680 handle->sys_name = strdup(sys_name);
5682 if ((event_name && !handle->event_name) ||
5683 (sys_name && !handle->sys_name)) {
5684 do_warning("Failed to allocate event/sys name");
5685 free((void *)handle->event_name);
5686 free((void *)handle->sys_name);
5688 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5691 handle->func = func;
5692 handle->next = pevent->handlers;
5693 pevent->handlers = handle;
5694 handle->context = context;
5699 static int handle_matches(struct event_handler *handler, int id,
5700 const char *sys_name, const char *event_name,
5701 pevent_event_handler_func func, void *context)
5703 if (id >= 0 && id != handler->id)
5706 if (event_name && (strcmp(event_name, handler->event_name) != 0))
5709 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
5712 if (func != handler->func || context != handler->context)
5719 * pevent_unregister_event_handler - unregister an existing event handler
5720 * @pevent: the handle to the pevent
5721 * @id: the id of the event to unregister
5722 * @sys_name: the system name the handler belongs to
5723 * @event_name: the name of the event handler
5724 * @func: the function to call to parse the event information
5725 * @context: the data to be passed to @func
5727 * This function removes existing event handler (parser).
5729 * If @id is >= 0, then it is used to find the event.
5730 * else @sys_name and @event_name are used.
5732 * Returns 0 if handler was removed successfully, -1 if event was not found.
5734 int pevent_unregister_event_handler(struct pevent *pevent, int id,
5735 const char *sys_name, const char *event_name,
5736 pevent_event_handler_func func, void *context)
5738 struct event_format *event;
5739 struct event_handler *handle;
5740 struct event_handler **next;
5742 event = pevent_search_event(pevent, id, sys_name, event_name);
5746 if (event->handler == func && event->context == context) {
5747 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
5748 event->id, event->system, event->name);
5750 event->handler = NULL;
5751 event->context = NULL;
5756 for (next = &pevent->handlers; *next; next = &(*next)->next) {
5758 if (handle_matches(handle, id, sys_name, event_name,
5766 *next = handle->next;
5767 free_handler(handle);
5773 * pevent_alloc - create a pevent handle
5775 struct pevent *pevent_alloc(void)
5777 struct pevent *pevent = calloc(1, sizeof(*pevent));
5780 pevent->ref_count = 1;
5785 void pevent_ref(struct pevent *pevent)
5787 pevent->ref_count++;
5790 static void free_format_fields(struct format_field *field)
5792 struct format_field *next;
5803 static void free_formats(struct format *format)
5805 free_format_fields(format->common_fields);
5806 free_format_fields(format->fields);
5809 void pevent_free_format(struct event_format *event)
5812 free(event->system);
5814 free_formats(&event->format);
5816 free(event->print_fmt.format);
5817 free_args(event->print_fmt.args);
5823 * pevent_free - free a pevent handle
5824 * @pevent: the pevent handle to free
5826 void pevent_free(struct pevent *pevent)
5828 struct cmdline_list *cmdlist, *cmdnext;
5829 struct func_list *funclist, *funcnext;
5830 struct printk_list *printklist, *printknext;
5831 struct pevent_function_handler *func_handler;
5832 struct event_handler *handle;
5838 cmdlist = pevent->cmdlist;
5839 funclist = pevent->funclist;
5840 printklist = pevent->printklist;
5842 pevent->ref_count--;
5843 if (pevent->ref_count)
5846 if (pevent->cmdlines) {
5847 for (i = 0; i < pevent->cmdline_count; i++)
5848 free(pevent->cmdlines[i].comm);
5849 free(pevent->cmdlines);
5853 cmdnext = cmdlist->next;
5854 free(cmdlist->comm);
5859 if (pevent->func_map) {
5860 for (i = 0; i < (int)pevent->func_count; i++) {
5861 free(pevent->func_map[i].func);
5862 free(pevent->func_map[i].mod);
5864 free(pevent->func_map);
5868 funcnext = funclist->next;
5869 free(funclist->func);
5870 free(funclist->mod);
5872 funclist = funcnext;
5875 while (pevent->func_handlers) {
5876 func_handler = pevent->func_handlers;
5877 pevent->func_handlers = func_handler->next;
5878 free_func_handle(func_handler);
5881 if (pevent->printk_map) {
5882 for (i = 0; i < (int)pevent->printk_count; i++)
5883 free(pevent->printk_map[i].printk);
5884 free(pevent->printk_map);
5887 while (printklist) {
5888 printknext = printklist->next;
5889 free(printklist->printk);
5891 printklist = printknext;
5894 for (i = 0; i < pevent->nr_events; i++)
5895 pevent_free_format(pevent->events[i]);
5897 while (pevent->handlers) {
5898 handle = pevent->handlers;
5899 pevent->handlers = handle->next;
5900 free_handler(handle);
5903 free(pevent->events);
5904 free(pevent->sort_events);
5909 void pevent_unref(struct pevent *pevent)
5911 pevent_free(pevent);