2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by
23 * - Copyright (C) 2009 Frederic Weisbecker,
24 * Frederic Weisbecker gave his permission to relicense the code to
25 * the Lesser General Public License.
35 #include "event-parse.h"
36 #include "event-utils.h"
38 static const char *input_buf
;
39 static unsigned long long input_buf_ptr
;
40 static unsigned long long input_buf_siz
;
42 static int is_flag_field
;
43 static int is_symbolic_field
;
45 static int show_warning
= 1;
47 #define do_warning(fmt, ...) \
50 warning(fmt, ##__VA_ARGS__); \
53 static void init_input_buf(const char *buf
, unsigned long long size
)
60 const char *pevent_get_input_buf(void)
65 unsigned long long pevent_get_input_buf_ptr(void)
70 struct event_handler
{
71 struct event_handler
*next
;
74 const char *event_name
;
75 pevent_event_handler_func func
;
79 struct pevent_func_params
{
80 struct pevent_func_params
*next
;
81 enum pevent_func_arg_type type
;
84 struct pevent_function_handler
{
85 struct pevent_function_handler
*next
;
86 enum pevent_func_arg_type ret_type
;
88 pevent_func_handler func
;
89 struct pevent_func_params
*params
;
93 static unsigned long long
94 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
95 struct event_format
*event
, struct print_arg
*arg
);
97 static void free_func_handle(struct pevent_function_handler
*func
);
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
107 void pevent_buffer_init(const char *buf
, unsigned long long size
)
109 init_input_buf(buf
, size
);
112 void breakpoint(void)
118 struct print_arg
*alloc_arg(void)
120 struct print_arg
*arg
;
122 arg
= malloc_or_die(sizeof(*arg
));
125 memset(arg
, 0, sizeof(*arg
));
135 static int cmdline_cmp(const void *a
, const void *b
)
137 const struct cmdline
*ca
= a
;
138 const struct cmdline
*cb
= b
;
140 if (ca
->pid
< cb
->pid
)
142 if (ca
->pid
> cb
->pid
)
148 struct cmdline_list
{
149 struct cmdline_list
*next
;
154 static int cmdline_init(struct pevent
*pevent
)
156 struct cmdline_list
*cmdlist
= pevent
->cmdlist
;
157 struct cmdline_list
*item
;
158 struct cmdline
*cmdlines
;
161 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * pevent
->cmdline_count
);
165 cmdlines
[i
].pid
= cmdlist
->pid
;
166 cmdlines
[i
].comm
= cmdlist
->comm
;
169 cmdlist
= cmdlist
->next
;
173 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
175 pevent
->cmdlines
= cmdlines
;
176 pevent
->cmdlist
= NULL
;
181 static char *find_cmdline(struct pevent
*pevent
, int pid
)
183 const struct cmdline
*comm
;
189 if (!pevent
->cmdlines
)
190 cmdline_init(pevent
);
194 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
195 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
203 * pevent_pid_is_registered - return if a pid has a cmdline registered
204 * @pevent: handle for the pevent
205 * @pid: The pid to check if it has a cmdline registered with.
207 * Returns 1 if the pid has a cmdline mapped to it
210 int pevent_pid_is_registered(struct pevent
*pevent
, int pid
)
212 const struct cmdline
*comm
;
218 if (!pevent
->cmdlines
)
219 cmdline_init(pevent
);
223 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
224 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
232 * If the command lines have been converted to an array, then
233 * we must add this pid. This is much slower than when cmdlines
234 * are added before the array is initialized.
236 static int add_new_comm(struct pevent
*pevent
, const char *comm
, int pid
)
238 struct cmdline
*cmdlines
= pevent
->cmdlines
;
239 const struct cmdline
*cmdline
;
245 /* avoid duplicates */
248 cmdline
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
249 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
255 cmdlines
= realloc(cmdlines
, sizeof(*cmdlines
) * (pevent
->cmdline_count
+ 1));
261 cmdlines
[pevent
->cmdline_count
].pid
= pid
;
262 cmdlines
[pevent
->cmdline_count
].comm
= strdup(comm
);
263 if (!cmdlines
[pevent
->cmdline_count
].comm
)
266 if (cmdlines
[pevent
->cmdline_count
].comm
)
267 pevent
->cmdline_count
++;
269 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
270 pevent
->cmdlines
= cmdlines
;
276 * pevent_register_comm - register a pid / comm mapping
277 * @pevent: handle for the pevent
278 * @comm: the command line to register
279 * @pid: the pid to map the command line to
281 * This adds a mapping to search for command line names with
282 * a given pid. The comm is duplicated.
284 int pevent_register_comm(struct pevent
*pevent
, const char *comm
, int pid
)
286 struct cmdline_list
*item
;
288 if (pevent
->cmdlines
)
289 return add_new_comm(pevent
, comm
, pid
);
291 item
= malloc_or_die(sizeof(*item
));
292 item
->comm
= strdup(comm
);
296 item
->next
= pevent
->cmdlist
;
298 pevent
->cmdlist
= item
;
299 pevent
->cmdline_count
++;
305 unsigned long long addr
;
311 struct func_list
*next
;
312 unsigned long long addr
;
317 static int func_cmp(const void *a
, const void *b
)
319 const struct func_map
*fa
= a
;
320 const struct func_map
*fb
= b
;
322 if (fa
->addr
< fb
->addr
)
324 if (fa
->addr
> fb
->addr
)
331 * We are searching for a record in between, not an exact
334 static int func_bcmp(const void *a
, const void *b
)
336 const struct func_map
*fa
= a
;
337 const struct func_map
*fb
= b
;
339 if ((fa
->addr
== fb
->addr
) ||
341 (fa
->addr
> fb
->addr
&&
342 fa
->addr
< (fb
+1)->addr
))
345 if (fa
->addr
< fb
->addr
)
351 static int func_map_init(struct pevent
*pevent
)
353 struct func_list
*funclist
;
354 struct func_list
*item
;
355 struct func_map
*func_map
;
358 func_map
= malloc_or_die(sizeof(*func_map
) * (pevent
->func_count
+ 1));
359 funclist
= pevent
->funclist
;
363 func_map
[i
].func
= funclist
->func
;
364 func_map
[i
].addr
= funclist
->addr
;
365 func_map
[i
].mod
= funclist
->mod
;
368 funclist
= funclist
->next
;
372 qsort(func_map
, pevent
->func_count
, sizeof(*func_map
), func_cmp
);
375 * Add a special record at the end.
377 func_map
[pevent
->func_count
].func
= NULL
;
378 func_map
[pevent
->func_count
].addr
= 0;
379 func_map
[pevent
->func_count
].mod
= NULL
;
381 pevent
->func_map
= func_map
;
382 pevent
->funclist
= NULL
;
387 static struct func_map
*
388 find_func(struct pevent
*pevent
, unsigned long long addr
)
390 struct func_map
*func
;
393 if (!pevent
->func_map
)
394 func_map_init(pevent
);
398 func
= bsearch(&key
, pevent
->func_map
, pevent
->func_count
,
399 sizeof(*pevent
->func_map
), func_bcmp
);
405 * pevent_find_function - find a function by a given address
406 * @pevent: handle for the pevent
407 * @addr: the address to find the function with
409 * Returns a pointer to the function stored that has the given
410 * address. Note, the address does not have to be exact, it
411 * will select the function that would contain the address.
413 const char *pevent_find_function(struct pevent
*pevent
, unsigned long long addr
)
415 struct func_map
*map
;
417 map
= find_func(pevent
, addr
);
425 * pevent_find_function_address - find a function address by a given address
426 * @pevent: handle for the pevent
427 * @addr: the address to find the function with
429 * Returns the address the function starts at. This can be used in
430 * conjunction with pevent_find_function to print both the function
431 * name and the function offset.
434 pevent_find_function_address(struct pevent
*pevent
, unsigned long long addr
)
436 struct func_map
*map
;
438 map
= find_func(pevent
, addr
);
446 * pevent_register_function - register a function with a given address
447 * @pevent: handle for the pevent
448 * @function: the function name to register
449 * @addr: the address the function starts at
450 * @mod: the kernel module the function may be in (NULL for none)
452 * This registers a function name with an address and module.
453 * The @func passed in is duplicated.
455 int pevent_register_function(struct pevent
*pevent
, char *func
,
456 unsigned long long addr
, char *mod
)
458 struct func_list
*item
;
460 item
= malloc_or_die(sizeof(*item
));
462 item
->next
= pevent
->funclist
;
463 item
->func
= strdup(func
);
465 item
->mod
= strdup(mod
);
470 if (!item
->func
|| (mod
&& !item
->mod
))
473 pevent
->funclist
= item
;
474 pevent
->func_count
++;
480 * pevent_print_funcs - print out the stored functions
481 * @pevent: handle for the pevent
483 * This prints out the stored functions.
485 void pevent_print_funcs(struct pevent
*pevent
)
489 if (!pevent
->func_map
)
490 func_map_init(pevent
);
492 for (i
= 0; i
< (int)pevent
->func_count
; i
++) {
494 pevent
->func_map
[i
].addr
,
495 pevent
->func_map
[i
].func
);
496 if (pevent
->func_map
[i
].mod
)
497 printf(" [%s]\n", pevent
->func_map
[i
].mod
);
504 unsigned long long addr
;
509 struct printk_list
*next
;
510 unsigned long long addr
;
514 static int printk_cmp(const void *a
, const void *b
)
516 const struct printk_map
*pa
= a
;
517 const struct printk_map
*pb
= b
;
519 if (pa
->addr
< pb
->addr
)
521 if (pa
->addr
> pb
->addr
)
527 static void printk_map_init(struct pevent
*pevent
)
529 struct printk_list
*printklist
;
530 struct printk_list
*item
;
531 struct printk_map
*printk_map
;
534 printk_map
= malloc_or_die(sizeof(*printk_map
) * (pevent
->printk_count
+ 1));
536 printklist
= pevent
->printklist
;
540 printk_map
[i
].printk
= printklist
->printk
;
541 printk_map
[i
].addr
= printklist
->addr
;
544 printklist
= printklist
->next
;
548 qsort(printk_map
, pevent
->printk_count
, sizeof(*printk_map
), printk_cmp
);
550 pevent
->printk_map
= printk_map
;
551 pevent
->printklist
= NULL
;
554 static struct printk_map
*
555 find_printk(struct pevent
*pevent
, unsigned long long addr
)
557 struct printk_map
*printk
;
558 struct printk_map key
;
560 if (!pevent
->printk_map
)
561 printk_map_init(pevent
);
565 printk
= bsearch(&key
, pevent
->printk_map
, pevent
->printk_count
,
566 sizeof(*pevent
->printk_map
), printk_cmp
);
572 * pevent_register_print_string - register a string by its address
573 * @pevent: handle for the pevent
574 * @fmt: the string format to register
575 * @addr: the address the string was located at
577 * This registers a string by the address it was stored in the kernel.
578 * The @fmt passed in is duplicated.
580 int pevent_register_print_string(struct pevent
*pevent
, char *fmt
,
581 unsigned long long addr
)
583 struct printk_list
*item
;
585 item
= malloc_or_die(sizeof(*item
));
587 item
->next
= pevent
->printklist
;
588 item
->printk
= strdup(fmt
);
594 pevent
->printklist
= item
;
595 pevent
->printk_count
++;
601 * pevent_print_printk - print out the stored strings
602 * @pevent: handle for the pevent
604 * This prints the string formats that were stored.
606 void pevent_print_printk(struct pevent
*pevent
)
610 if (!pevent
->printk_map
)
611 printk_map_init(pevent
);
613 for (i
= 0; i
< (int)pevent
->printk_count
; i
++) {
614 printf("%016llx %s\n",
615 pevent
->printk_map
[i
].addr
,
616 pevent
->printk_map
[i
].printk
);
620 static struct event_format
*alloc_event(void)
622 struct event_format
*event
;
624 event
= malloc(sizeof(*event
));
627 memset(event
, 0, sizeof(*event
));
632 static void add_event(struct pevent
*pevent
, struct event_format
*event
)
636 pevent
->events
= realloc(pevent
->events
, sizeof(event
) *
637 (pevent
->nr_events
+ 1));
639 die("Can not allocate events");
641 for (i
= 0; i
< pevent
->nr_events
; i
++) {
642 if (pevent
->events
[i
]->id
> event
->id
)
645 if (i
< pevent
->nr_events
)
646 memmove(&pevent
->events
[i
+ 1],
648 sizeof(event
) * (pevent
->nr_events
- i
));
650 pevent
->events
[i
] = event
;
653 event
->pevent
= pevent
;
656 static int event_item_type(enum event_type type
)
659 case EVENT_ITEM
... EVENT_SQUOTE
:
661 case EVENT_ERROR
... EVENT_DELIM
:
667 static void free_flag_sym(struct print_flag_sym
*fsym
)
669 struct print_flag_sym
*next
;
680 static void free_arg(struct print_arg
*arg
)
682 struct print_arg
*farg
;
689 free(arg
->atom
.atom
);
692 free(arg
->field
.name
);
695 free_arg(arg
->flags
.field
);
696 free(arg
->flags
.delim
);
697 free_flag_sym(arg
->flags
.flags
);
700 free_arg(arg
->symbol
.field
);
701 free_flag_sym(arg
->symbol
.symbols
);
704 free_arg(arg
->hex
.field
);
705 free_arg(arg
->hex
.size
);
708 free(arg
->typecast
.type
);
709 free_arg(arg
->typecast
.item
);
713 free(arg
->string
.string
);
715 case PRINT_DYNAMIC_ARRAY
:
716 free(arg
->dynarray
.index
);
720 free_arg(arg
->op
.left
);
721 free_arg(arg
->op
.right
);
724 while (arg
->func
.args
) {
725 farg
= arg
->func
.args
;
726 arg
->func
.args
= farg
->next
;
739 static enum event_type
get_type(int ch
)
742 return EVENT_NEWLINE
;
745 if (isalnum(ch
) || ch
== '_')
753 if (ch
== '(' || ch
== ')' || ch
== ',')
759 static int __read_char(void)
761 if (input_buf_ptr
>= input_buf_siz
)
764 return input_buf
[input_buf_ptr
++];
767 static int __peek_char(void)
769 if (input_buf_ptr
>= input_buf_siz
)
772 return input_buf
[input_buf_ptr
];
776 * pevent_peek_char - peek at the next character that will be read
778 * Returns the next character read, or -1 if end of buffer.
780 int pevent_peek_char(void)
782 return __peek_char();
785 static int extend_token(char **tok
, char *buf
, int size
)
787 char *newtok
= realloc(*tok
, size
);
804 static enum event_type
force_token(const char *str
, char **tok
);
806 static enum event_type
__read_token(char **tok
)
809 int ch
, last_ch
, quote_ch
, next_ch
;
812 enum event_type type
;
822 if (type
== EVENT_NONE
)
830 *tok
= malloc_or_die(2);
838 next_ch
= __peek_char();
839 if (next_ch
== '>') {
840 buf
[i
++] = __read_char();
853 buf
[i
++] = __read_char();
865 default: /* what should we do instead? */
875 buf
[i
++] = __read_char();
880 /* don't keep quotes */
886 if (i
== (BUFSIZ
- 1)) {
890 if (extend_token(tok
, buf
, tok_size
) < 0)
897 /* the '\' '\' will cancel itself */
898 if (ch
== '\\' && last_ch
== '\\')
900 } while (ch
!= quote_ch
|| last_ch
== '\\');
901 /* remove the last quote */
905 * For strings (double quotes) check the next token.
906 * If it is another string, concatinate the two.
908 if (type
== EVENT_DQUOTE
) {
909 unsigned long long save_input_buf_ptr
= input_buf_ptr
;
913 } while (isspace(ch
));
916 input_buf_ptr
= save_input_buf_ptr
;
921 case EVENT_ERROR
... EVENT_SPACE
:
927 while (get_type(__peek_char()) == type
) {
928 if (i
== (BUFSIZ
- 1)) {
932 if (extend_token(tok
, buf
, tok_size
) < 0)
942 if (extend_token(tok
, buf
, tok_size
+ i
+ 1) < 0)
945 if (type
== EVENT_ITEM
) {
947 * Older versions of the kernel has a bug that
948 * creates invalid symbols and will break the mac80211
949 * parsing. This is a work around to that bug.
951 * See Linux kernel commit:
952 * 811cb50baf63461ce0bdb234927046131fc7fa8b
954 if (strcmp(*tok
, "LOCAL_PR_FMT") == 0) {
957 return force_token("\"\%s\" ", tok
);
958 } else if (strcmp(*tok
, "STA_PR_FMT") == 0) {
961 return force_token("\" sta:%pM\" ", tok
);
962 } else if (strcmp(*tok
, "VIF_PR_FMT") == 0) {
965 return force_token("\" vif:%p(%d)\" ", tok
);
972 static enum event_type
force_token(const char *str
, char **tok
)
974 const char *save_input_buf
;
975 unsigned long long save_input_buf_ptr
;
976 unsigned long long save_input_buf_siz
;
977 enum event_type type
;
979 /* save off the current input pointers */
980 save_input_buf
= input_buf
;
981 save_input_buf_ptr
= input_buf_ptr
;
982 save_input_buf_siz
= input_buf_siz
;
984 init_input_buf(str
, strlen(str
));
986 type
= __read_token(tok
);
988 /* reset back to original token */
989 input_buf
= save_input_buf
;
990 input_buf_ptr
= save_input_buf_ptr
;
991 input_buf_siz
= save_input_buf_siz
;
996 static void free_token(char *tok
)
1002 static enum event_type
read_token(char **tok
)
1004 enum event_type type
;
1007 type
= __read_token(tok
);
1008 if (type
!= EVENT_SPACE
)
1020 * pevent_read_token - access to utilites to use the pevent parser
1021 * @tok: The token to return
1023 * This will parse tokens from the string given by
1024 * pevent_init_data().
1026 * Returns the token type.
1028 enum event_type
pevent_read_token(char **tok
)
1030 return read_token(tok
);
1034 * pevent_free_token - free a token returned by pevent_read_token
1035 * @token: the token to free
1037 void pevent_free_token(char *token
)
1043 static enum event_type
read_token_item(char **tok
)
1045 enum event_type type
;
1048 type
= __read_token(tok
);
1049 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
1060 static int test_type(enum event_type type
, enum event_type expect
)
1062 if (type
!= expect
) {
1063 do_warning("Error: expected type %d but read %d",
1070 static int test_type_token(enum event_type type
, const char *token
,
1071 enum event_type expect
, const char *expect_tok
)
1073 if (type
!= expect
) {
1074 do_warning("Error: expected type %d but read %d",
1079 if (strcmp(token
, expect_tok
) != 0) {
1080 do_warning("Error: expected '%s' but read '%s'",
1087 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
1089 enum event_type type
;
1092 type
= read_token(tok
);
1094 type
= read_token_item(tok
);
1095 return test_type(type
, expect
);
1098 static int read_expect_type(enum event_type expect
, char **tok
)
1100 return __read_expect_type(expect
, tok
, 1);
1103 static int __read_expected(enum event_type expect
, const char *str
,
1106 enum event_type type
;
1111 type
= read_token(&token
);
1113 type
= read_token_item(&token
);
1115 ret
= test_type_token(type
, token
, expect
, str
);
1122 static int read_expected(enum event_type expect
, const char *str
)
1124 return __read_expected(expect
, str
, 1);
1127 static int read_expected_item(enum event_type expect
, const char *str
)
1129 return __read_expected(expect
, str
, 0);
1132 static char *event_read_name(void)
1136 if (read_expected(EVENT_ITEM
, "name") < 0)
1139 if (read_expected(EVENT_OP
, ":") < 0)
1142 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1152 static int event_read_id(void)
1157 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
1160 if (read_expected(EVENT_OP
, ":") < 0)
1163 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1166 id
= strtoul(token
, NULL
, 0);
1175 static int field_is_string(struct format_field
*field
)
1177 if ((field
->flags
& FIELD_IS_ARRAY
) &&
1178 (strstr(field
->type
, "char") || strstr(field
->type
, "u8") ||
1179 strstr(field
->type
, "s8")))
1185 static int field_is_dynamic(struct format_field
*field
)
1187 if (strncmp(field
->type
, "__data_loc", 10) == 0)
1193 static int field_is_long(struct format_field
*field
)
1195 /* includes long long */
1196 if (strstr(field
->type
, "long"))
1202 static int event_read_fields(struct event_format
*event
, struct format_field
**fields
)
1204 struct format_field
*field
= NULL
;
1205 enum event_type type
;
1211 type
= read_token(&token
);
1212 if (type
== EVENT_NEWLINE
) {
1219 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
1223 type
= read_token(&token
);
1225 * The ftrace fields may still use the "special" name.
1228 if (event
->flags
& EVENT_FL_ISFTRACE
&&
1229 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
1231 type
= read_token(&token
);
1234 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
1238 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1243 field
= malloc_or_die(sizeof(*field
));
1244 memset(field
, 0, sizeof(*field
));
1245 field
->event
= event
;
1247 /* read the rest of the type */
1249 type
= read_token(&token
);
1250 if (type
== EVENT_ITEM
||
1251 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
1253 * Some of the ftrace fields are broken and have
1254 * an illegal "." in them.
1256 (event
->flags
& EVENT_FL_ISFTRACE
&&
1257 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
1259 if (strcmp(token
, "*") == 0)
1260 field
->flags
|= FIELD_IS_POINTER
;
1264 new_type
= realloc(field
->type
,
1265 strlen(field
->type
) +
1266 strlen(last_token
) + 2);
1271 field
->type
= new_type
;
1272 strcat(field
->type
, " ");
1273 strcat(field
->type
, last_token
);
1276 field
->type
= last_token
;
1285 die("no type found");
1288 field
->name
= last_token
;
1290 if (test_type(type
, EVENT_OP
))
1293 if (strcmp(token
, "[") == 0) {
1294 enum event_type last_type
= type
;
1295 char *brackets
= token
;
1299 field
->flags
|= FIELD_IS_ARRAY
;
1301 type
= read_token(&token
);
1303 if (type
== EVENT_ITEM
)
1304 field
->arraylen
= strtoul(token
, NULL
, 0);
1306 field
->arraylen
= 0;
1308 while (strcmp(token
, "]") != 0) {
1309 if (last_type
== EVENT_ITEM
&&
1316 new_brackets
= realloc(brackets
,
1318 strlen(token
) + len
);
1319 if (!new_brackets
) {
1323 brackets
= new_brackets
;
1325 strcat(brackets
, " ");
1326 strcat(brackets
, token
);
1327 /* We only care about the last token */
1328 field
->arraylen
= strtoul(token
, NULL
, 0);
1330 type
= read_token(&token
);
1331 if (type
== EVENT_NONE
) {
1332 die("failed to find token");
1339 new_brackets
= realloc(brackets
, strlen(brackets
) + 2);
1340 if (!new_brackets
) {
1344 brackets
= new_brackets
;
1345 strcat(brackets
, "]");
1347 /* add brackets to type */
1349 type
= read_token(&token
);
1351 * If the next token is not an OP, then it is of
1352 * the format: type [] item;
1354 if (type
== EVENT_ITEM
) {
1356 new_type
= realloc(field
->type
,
1357 strlen(field
->type
) +
1358 strlen(field
->name
) +
1359 strlen(brackets
) + 2);
1364 field
->type
= new_type
;
1365 strcat(field
->type
, " ");
1366 strcat(field
->type
, field
->name
);
1367 free_token(field
->name
);
1368 strcat(field
->type
, brackets
);
1369 field
->name
= token
;
1370 type
= read_token(&token
);
1373 new_type
= realloc(field
->type
,
1374 strlen(field
->type
) +
1375 strlen(brackets
) + 1);
1380 field
->type
= new_type
;
1381 strcat(field
->type
, brackets
);
1386 if (field_is_string(field
))
1387 field
->flags
|= FIELD_IS_STRING
;
1388 if (field_is_dynamic(field
))
1389 field
->flags
|= FIELD_IS_DYNAMIC
;
1390 if (field_is_long(field
))
1391 field
->flags
|= FIELD_IS_LONG
;
1393 if (test_type_token(type
, token
, EVENT_OP
, ";"))
1397 if (read_expected(EVENT_ITEM
, "offset") < 0)
1400 if (read_expected(EVENT_OP
, ":") < 0)
1403 if (read_expect_type(EVENT_ITEM
, &token
))
1405 field
->offset
= strtoul(token
, NULL
, 0);
1408 if (read_expected(EVENT_OP
, ";") < 0)
1411 if (read_expected(EVENT_ITEM
, "size") < 0)
1414 if (read_expected(EVENT_OP
, ":") < 0)
1417 if (read_expect_type(EVENT_ITEM
, &token
))
1419 field
->size
= strtoul(token
, NULL
, 0);
1422 if (read_expected(EVENT_OP
, ";") < 0)
1425 type
= read_token(&token
);
1426 if (type
!= EVENT_NEWLINE
) {
1427 /* newer versions of the kernel have a "signed" type */
1428 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
1433 if (read_expected(EVENT_OP
, ":") < 0)
1436 if (read_expect_type(EVENT_ITEM
, &token
))
1439 /* add signed type */
1442 if (read_expected(EVENT_OP
, ";") < 0)
1445 if (read_expect_type(EVENT_NEWLINE
, &token
))
1451 if (field
->flags
& FIELD_IS_ARRAY
) {
1452 if (field
->arraylen
)
1453 field
->elementsize
= field
->size
/ field
->arraylen
;
1454 else if (field
->flags
& FIELD_IS_STRING
)
1455 field
->elementsize
= 1;
1457 field
->elementsize
= event
->pevent
->long_size
;
1459 field
->elementsize
= field
->size
;
1462 fields
= &field
->next
;
1479 static int event_read_format(struct event_format
*event
)
1484 if (read_expected_item(EVENT_ITEM
, "format") < 0)
1487 if (read_expected(EVENT_OP
, ":") < 0)
1490 if (read_expect_type(EVENT_NEWLINE
, &token
))
1494 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1497 event
->format
.nr_common
= ret
;
1499 ret
= event_read_fields(event
, &event
->format
.fields
);
1502 event
->format
.nr_fields
= ret
;
1511 static enum event_type
1512 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
1513 char **tok
, enum event_type type
);
1515 static enum event_type
1516 process_arg(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1518 enum event_type type
;
1521 type
= read_token(&token
);
1524 return process_arg_token(event
, arg
, tok
, type
);
1527 static enum event_type
1528 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
);
1530 static enum event_type
1531 process_cond(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1533 struct print_arg
*arg
, *left
, *right
;
1534 enum event_type type
;
1539 right
= alloc_arg();
1541 arg
->type
= PRINT_OP
;
1542 arg
->op
.left
= left
;
1543 arg
->op
.right
= right
;
1546 type
= process_arg(event
, left
, &token
);
1549 /* Handle other operations in the arguments */
1550 if (type
== EVENT_OP
&& strcmp(token
, ":") != 0) {
1551 type
= process_op(event
, left
, &token
);
1555 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1560 type
= process_arg(event
, right
, &token
);
1562 top
->op
.right
= arg
;
1568 /* Top may point to itself */
1569 top
->op
.right
= NULL
;
1575 static enum event_type
1576 process_array(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1578 struct print_arg
*arg
;
1579 enum event_type type
;
1585 type
= process_arg(event
, arg
, &token
);
1586 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1589 top
->op
.right
= arg
;
1592 type
= read_token_item(&token
);
1604 static int get_op_prio(char *op
)
1618 /* '>>' and '<<' are 8 */
1622 /* '==' and '!=' are 10 */
1632 do_warning("unknown op '%c'", op
[0]);
1636 if (strcmp(op
, "++") == 0 ||
1637 strcmp(op
, "--") == 0) {
1639 } else if (strcmp(op
, ">>") == 0 ||
1640 strcmp(op
, "<<") == 0) {
1642 } else if (strcmp(op
, ">=") == 0 ||
1643 strcmp(op
, "<=") == 0) {
1645 } else if (strcmp(op
, "==") == 0 ||
1646 strcmp(op
, "!=") == 0) {
1648 } else if (strcmp(op
, "&&") == 0) {
1650 } else if (strcmp(op
, "||") == 0) {
1653 do_warning("unknown op '%s'", op
);
1659 static int set_op_prio(struct print_arg
*arg
)
1662 /* single ops are the greatest */
1663 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
)
1666 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1668 return arg
->op
.prio
;
1671 /* Note, *tok does not get freed, but will most likely be saved */
1672 static enum event_type
1673 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1675 struct print_arg
*left
, *right
= NULL
;
1676 enum event_type type
;
1679 /* the op is passed in via tok */
1682 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1683 /* handle single op */
1685 die("bad op token %s", token
);
1695 do_warning("bad op token %s", token
);
1700 /* make an empty left */
1702 left
->type
= PRINT_NULL
;
1703 arg
->op
.left
= left
;
1705 right
= alloc_arg();
1706 arg
->op
.right
= right
;
1708 /* do not free the token, it belongs to an op */
1710 type
= process_arg(event
, right
, tok
);
1712 } else if (strcmp(token
, "?") == 0) {
1715 /* copy the top arg to the left */
1718 arg
->type
= PRINT_OP
;
1720 arg
->op
.left
= left
;
1723 type
= process_cond(event
, arg
, tok
);
1725 } else if (strcmp(token
, ">>") == 0 ||
1726 strcmp(token
, "<<") == 0 ||
1727 strcmp(token
, "&") == 0 ||
1728 strcmp(token
, "|") == 0 ||
1729 strcmp(token
, "&&") == 0 ||
1730 strcmp(token
, "||") == 0 ||
1731 strcmp(token
, "-") == 0 ||
1732 strcmp(token
, "+") == 0 ||
1733 strcmp(token
, "*") == 0 ||
1734 strcmp(token
, "^") == 0 ||
1735 strcmp(token
, "/") == 0 ||
1736 strcmp(token
, "<") == 0 ||
1737 strcmp(token
, ">") == 0 ||
1738 strcmp(token
, "==") == 0 ||
1739 strcmp(token
, "!=") == 0) {
1743 /* copy the top arg to the left */
1746 arg
->type
= PRINT_OP
;
1748 arg
->op
.left
= left
;
1750 if (set_op_prio(arg
) == -1) {
1751 event
->flags
|= EVENT_FL_FAILED
;
1752 /* arg->op.op (= token) will be freed at out_free */
1757 type
= read_token_item(&token
);
1760 /* could just be a type pointer */
1761 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1762 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1765 if (left
->type
!= PRINT_ATOM
)
1766 die("bad pointer type");
1767 new_atom
= realloc(left
->atom
.atom
,
1768 strlen(left
->atom
.atom
) + 3);
1772 left
->atom
.atom
= new_atom
;
1773 strcat(left
->atom
.atom
, " *");
1781 right
= alloc_arg();
1782 type
= process_arg_token(event
, right
, tok
, type
);
1783 arg
->op
.right
= right
;
1785 } else if (strcmp(token
, "[") == 0) {
1790 arg
->type
= PRINT_OP
;
1792 arg
->op
.left
= left
;
1796 type
= process_array(event
, arg
, tok
);
1799 do_warning("unknown op '%s'", token
);
1800 event
->flags
|= EVENT_FL_FAILED
;
1801 /* the arg is now the left side */
1805 if (type
== EVENT_OP
&& strcmp(*tok
, ":") != 0) {
1808 /* higher prios need to be closer to the root */
1809 prio
= get_op_prio(*tok
);
1811 if (prio
> arg
->op
.prio
)
1812 return process_op(event
, arg
, tok
);
1814 return process_op(event
, right
, tok
);
1825 static enum event_type
1826 process_entry(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
1829 enum event_type type
;
1833 if (read_expected(EVENT_OP
, "->") < 0)
1836 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1840 arg
->type
= PRINT_FIELD
;
1841 arg
->field
.name
= field
;
1843 if (is_flag_field
) {
1844 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
1845 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1847 } else if (is_symbolic_field
) {
1848 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
1849 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1850 is_symbolic_field
= 0;
1853 type
= read_token(&token
);
1865 static char *arg_eval (struct print_arg
*arg
);
1867 static unsigned long long
1868 eval_type_str(unsigned long long val
, const char *type
, int pointer
)
1878 if (type
[len
-1] != '*') {
1879 do_warning("pointer expected with non pointer type");
1883 ref
= malloc_or_die(len
);
1884 memcpy(ref
, type
, len
);
1886 /* chop off the " *" */
1889 val
= eval_type_str(val
, ref
, 0);
1894 /* check if this is a pointer */
1895 if (type
[len
- 1] == '*')
1898 /* Try to figure out the arg size*/
1899 if (strncmp(type
, "struct", 6) == 0)
1903 if (strcmp(type
, "u8") == 0)
1906 if (strcmp(type
, "u16") == 0)
1907 return val
& 0xffff;
1909 if (strcmp(type
, "u32") == 0)
1910 return val
& 0xffffffff;
1912 if (strcmp(type
, "u64") == 0 ||
1913 strcmp(type
, "s64"))
1916 if (strcmp(type
, "s8") == 0)
1917 return (unsigned long long)(char)val
& 0xff;
1919 if (strcmp(type
, "s16") == 0)
1920 return (unsigned long long)(short)val
& 0xffff;
1922 if (strcmp(type
, "s32") == 0)
1923 return (unsigned long long)(int)val
& 0xffffffff;
1925 if (strncmp(type
, "unsigned ", 9) == 0) {
1930 if (strcmp(type
, "char") == 0) {
1932 return (unsigned long long)(char)val
& 0xff;
1937 if (strcmp(type
, "short") == 0) {
1939 return (unsigned long long)(short)val
& 0xffff;
1941 return val
& 0xffff;
1944 if (strcmp(type
, "int") == 0) {
1946 return (unsigned long long)(int)val
& 0xffffffff;
1948 return val
& 0xffffffff;
1955 * Try to figure out the type.
1957 static unsigned long long
1958 eval_type(unsigned long long val
, struct print_arg
*arg
, int pointer
)
1960 if (arg
->type
!= PRINT_TYPE
)
1961 die("expected type argument");
1963 return eval_type_str(val
, arg
->typecast
.type
, pointer
);
1966 static int arg_num_eval(struct print_arg
*arg
, long long *val
)
1968 long long left
, right
;
1971 switch (arg
->type
) {
1973 *val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1976 ret
= arg_num_eval(arg
->typecast
.item
, val
);
1979 *val
= eval_type(*val
, arg
, 0);
1982 switch (arg
->op
.op
[0]) {
1984 ret
= arg_num_eval(arg
->op
.left
, &left
);
1987 ret
= arg_num_eval(arg
->op
.right
, &right
);
1991 *val
= left
|| right
;
1993 *val
= left
| right
;
1996 ret
= arg_num_eval(arg
->op
.left
, &left
);
1999 ret
= arg_num_eval(arg
->op
.right
, &right
);
2003 *val
= left
&& right
;
2005 *val
= left
& right
;
2008 ret
= arg_num_eval(arg
->op
.left
, &left
);
2011 ret
= arg_num_eval(arg
->op
.right
, &right
);
2014 switch (arg
->op
.op
[1]) {
2016 *val
= left
< right
;
2019 *val
= left
<< right
;
2022 *val
= left
<= right
;
2025 do_warning("unknown op '%s'", arg
->op
.op
);
2030 ret
= arg_num_eval(arg
->op
.left
, &left
);
2033 ret
= arg_num_eval(arg
->op
.right
, &right
);
2036 switch (arg
->op
.op
[1]) {
2038 *val
= left
> right
;
2041 *val
= left
>> right
;
2044 *val
= left
>= right
;
2047 do_warning("unknown op '%s'", arg
->op
.op
);
2052 ret
= arg_num_eval(arg
->op
.left
, &left
);
2055 ret
= arg_num_eval(arg
->op
.right
, &right
);
2059 if (arg
->op
.op
[1] != '=') {
2060 do_warning("unknown op '%s'", arg
->op
.op
);
2063 *val
= left
== right
;
2066 ret
= arg_num_eval(arg
->op
.left
, &left
);
2069 ret
= arg_num_eval(arg
->op
.right
, &right
);
2073 switch (arg
->op
.op
[1]) {
2075 *val
= left
!= right
;
2078 do_warning("unknown op '%s'", arg
->op
.op
);
2083 /* check for negative */
2084 if (arg
->op
.left
->type
== PRINT_NULL
)
2087 ret
= arg_num_eval(arg
->op
.left
, &left
);
2090 ret
= arg_num_eval(arg
->op
.right
, &right
);
2093 *val
= left
- right
;
2096 if (arg
->op
.left
->type
== PRINT_NULL
)
2099 ret
= arg_num_eval(arg
->op
.left
, &left
);
2102 ret
= arg_num_eval(arg
->op
.right
, &right
);
2105 *val
= left
+ right
;
2108 do_warning("unknown op '%s'", arg
->op
.op
);
2114 case PRINT_FIELD
... PRINT_SYMBOL
:
2118 do_warning("invalid eval type %d", arg
->type
);
2125 static char *arg_eval (struct print_arg
*arg
)
2128 static char buf
[20];
2130 switch (arg
->type
) {
2132 return arg
->atom
.atom
;
2134 return arg_eval(arg
->typecast
.item
);
2136 if (!arg_num_eval(arg
, &val
))
2138 sprintf(buf
, "%lld", val
);
2142 case PRINT_FIELD
... PRINT_SYMBOL
:
2146 die("invalid eval type %d", arg
->type
);
2153 static enum event_type
2154 process_fields(struct event_format
*event
, struct print_flag_sym
**list
, char **tok
)
2156 enum event_type type
;
2157 struct print_arg
*arg
= NULL
;
2158 struct print_flag_sym
*field
;
2164 type
= read_token_item(&token
);
2165 if (test_type_token(type
, token
, EVENT_OP
, "{"))
2171 type
= process_arg(event
, arg
, &token
);
2173 if (type
== EVENT_OP
)
2174 type
= process_op(event
, arg
, &token
);
2176 if (type
== EVENT_ERROR
)
2179 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2182 field
= malloc_or_die(sizeof(*field
));
2183 memset(field
, 0, sizeof(*field
));
2185 value
= arg_eval(arg
);
2188 field
->value
= strdup(value
);
2189 if (field
->value
== NULL
)
2196 type
= process_arg(event
, arg
, &token
);
2197 if (test_type_token(type
, token
, EVENT_OP
, "}"))
2200 value
= arg_eval(arg
);
2203 field
->str
= strdup(value
);
2204 if (field
->str
== NULL
)
2210 list
= &field
->next
;
2213 type
= read_token_item(&token
);
2214 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
2227 static enum event_type
2228 process_flags(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2230 struct print_arg
*field
;
2231 enum event_type type
;
2234 memset(arg
, 0, sizeof(*arg
));
2235 arg
->type
= PRINT_FLAGS
;
2237 field
= alloc_arg();
2239 type
= process_arg(event
, field
, &token
);
2241 /* Handle operations in the first argument */
2242 while (type
== EVENT_OP
)
2243 type
= process_op(event
, field
, &token
);
2245 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2249 arg
->flags
.field
= field
;
2251 type
= read_token_item(&token
);
2252 if (event_item_type(type
)) {
2253 arg
->flags
.delim
= token
;
2254 type
= read_token_item(&token
);
2257 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2260 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
2261 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2265 type
= read_token_item(tok
);
2274 static enum event_type
2275 process_symbols(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2277 struct print_arg
*field
;
2278 enum event_type type
;
2281 memset(arg
, 0, sizeof(*arg
));
2282 arg
->type
= PRINT_SYMBOL
;
2284 field
= alloc_arg();
2286 type
= process_arg(event
, field
, &token
);
2287 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2290 arg
->symbol
.field
= field
;
2292 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
2293 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2297 type
= read_token_item(tok
);
2306 static enum event_type
2307 process_hex(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2309 struct print_arg
*field
;
2310 enum event_type type
;
2313 memset(arg
, 0, sizeof(*arg
));
2314 arg
->type
= PRINT_HEX
;
2316 field
= alloc_arg();
2317 type
= process_arg(event
, field
, &token
);
2319 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2322 arg
->hex
.field
= field
;
2326 field
= alloc_arg();
2327 type
= process_arg(event
, field
, &token
);
2329 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2332 arg
->hex
.size
= field
;
2335 type
= read_token_item(tok
);
2345 static enum event_type
2346 process_dynamic_array(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2348 struct format_field
*field
;
2349 enum event_type type
;
2352 memset(arg
, 0, sizeof(*arg
));
2353 arg
->type
= PRINT_DYNAMIC_ARRAY
;
2356 * The item within the parenthesis is another field that holds
2357 * the index into where the array starts.
2359 type
= read_token(&token
);
2361 if (type
!= EVENT_ITEM
)
2364 /* Find the field */
2366 field
= pevent_find_field(event
, token
);
2370 arg
->dynarray
.field
= field
;
2371 arg
->dynarray
.index
= 0;
2373 if (read_expected(EVENT_DELIM
, ")") < 0)
2377 type
= read_token_item(&token
);
2379 if (type
!= EVENT_OP
|| strcmp(token
, "[") != 0)
2384 type
= process_arg(event
, arg
, &token
);
2385 if (type
== EVENT_ERROR
)
2388 if (!test_type_token(type
, token
, EVENT_OP
, "]"))
2392 type
= read_token_item(tok
);
2403 static enum event_type
2404 process_paren(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2406 struct print_arg
*item_arg
;
2407 enum event_type type
;
2410 type
= process_arg(event
, arg
, &token
);
2412 if (type
== EVENT_ERROR
)
2415 if (type
== EVENT_OP
)
2416 type
= process_op(event
, arg
, &token
);
2418 if (type
== EVENT_ERROR
)
2421 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2425 type
= read_token_item(&token
);
2428 * If the next token is an item or another open paren, then
2429 * this was a typecast.
2431 if (event_item_type(type
) ||
2432 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
2434 /* make this a typecast and contine */
2436 /* prevous must be an atom */
2437 if (arg
->type
!= PRINT_ATOM
)
2438 die("previous needed to be PRINT_ATOM");
2440 item_arg
= alloc_arg();
2442 arg
->type
= PRINT_TYPE
;
2443 arg
->typecast
.type
= arg
->atom
.atom
;
2444 arg
->typecast
.item
= item_arg
;
2445 type
= process_arg_token(event
, item_arg
, &token
, type
);
2459 static enum event_type
2460 process_str(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
2463 enum event_type type
;
2466 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2469 arg
->type
= PRINT_STRING
;
2470 arg
->string
.string
= token
;
2471 arg
->string
.offset
= -1;
2473 if (read_expected(EVENT_DELIM
, ")") < 0)
2476 type
= read_token(&token
);
2488 static struct pevent_function_handler
*
2489 find_func_handler(struct pevent
*pevent
, char *func_name
)
2491 struct pevent_function_handler
*func
;
2493 for (func
= pevent
->func_handlers
; func
; func
= func
->next
) {
2494 if (strcmp(func
->name
, func_name
) == 0)
2501 static void remove_func_handler(struct pevent
*pevent
, char *func_name
)
2503 struct pevent_function_handler
*func
;
2504 struct pevent_function_handler
**next
;
2506 next
= &pevent
->func_handlers
;
2507 while ((func
= *next
)) {
2508 if (strcmp(func
->name
, func_name
) == 0) {
2510 free_func_handle(func
);
2517 static enum event_type
2518 process_func_handler(struct event_format
*event
, struct pevent_function_handler
*func
,
2519 struct print_arg
*arg
, char **tok
)
2521 struct print_arg
**next_arg
;
2522 struct print_arg
*farg
;
2523 enum event_type type
;
2528 arg
->type
= PRINT_FUNC
;
2529 arg
->func
.func
= func
;
2533 next_arg
= &(arg
->func
.args
);
2534 for (i
= 0; i
< func
->nr_args
; i
++) {
2536 type
= process_arg(event
, farg
, &token
);
2537 if (i
< (func
->nr_args
- 1))
2542 if (test_type_token(type
, token
, EVENT_DELIM
, test
)) {
2549 next_arg
= &(farg
->next
);
2553 type
= read_token(&token
);
2559 static enum event_type
2560 process_function(struct event_format
*event
, struct print_arg
*arg
,
2561 char *token
, char **tok
)
2563 struct pevent_function_handler
*func
;
2565 if (strcmp(token
, "__print_flags") == 0) {
2568 return process_flags(event
, arg
, tok
);
2570 if (strcmp(token
, "__print_symbolic") == 0) {
2572 is_symbolic_field
= 1;
2573 return process_symbols(event
, arg
, tok
);
2575 if (strcmp(token
, "__print_hex") == 0) {
2577 return process_hex(event
, arg
, tok
);
2579 if (strcmp(token
, "__get_str") == 0) {
2581 return process_str(event
, arg
, tok
);
2583 if (strcmp(token
, "__get_dynamic_array") == 0) {
2585 return process_dynamic_array(event
, arg
, tok
);
2588 func
= find_func_handler(event
->pevent
, token
);
2591 return process_func_handler(event
, func
, arg
, tok
);
2594 do_warning("function %s not defined", token
);
2599 static enum event_type
2600 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
2601 char **tok
, enum event_type type
)
2610 if (strcmp(token
, "REC") == 0) {
2612 type
= process_entry(event
, arg
, &token
);
2616 /* test the next token */
2617 type
= read_token_item(&token
);
2620 * If the next token is a parenthesis, then this
2623 if (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0) {
2626 /* this will free atom. */
2627 type
= process_function(event
, arg
, atom
, &token
);
2630 /* atoms can be more than one token long */
2631 while (type
== EVENT_ITEM
) {
2633 new_atom
= realloc(atom
,
2634 strlen(atom
) + strlen(token
) + 2);
2643 strcat(atom
, token
);
2645 type
= read_token_item(&token
);
2648 arg
->type
= PRINT_ATOM
;
2649 arg
->atom
.atom
= atom
;
2654 arg
->type
= PRINT_ATOM
;
2655 arg
->atom
.atom
= token
;
2656 type
= read_token_item(&token
);
2659 if (strcmp(token
, "(") == 0) {
2661 type
= process_paren(event
, arg
, &token
);
2665 /* handle single ops */
2666 arg
->type
= PRINT_OP
;
2668 arg
->op
.left
= NULL
;
2669 type
= process_op(event
, arg
, &token
);
2671 /* On error, the op is freed */
2672 if (type
== EVENT_ERROR
)
2675 /* return error type if errored */
2678 case EVENT_ERROR
... EVENT_NEWLINE
:
2680 die("unexpected type %d", type
);
2687 static int event_read_print_args(struct event_format
*event
, struct print_arg
**list
)
2689 enum event_type type
= EVENT_ERROR
;
2690 struct print_arg
*arg
;
2695 if (type
== EVENT_NEWLINE
) {
2696 type
= read_token_item(&token
);
2702 type
= process_arg(event
, arg
, &token
);
2704 if (type
== EVENT_ERROR
) {
2713 if (type
== EVENT_OP
) {
2714 type
= process_op(event
, arg
, &token
);
2716 if (type
== EVENT_ERROR
) {
2725 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
2732 } while (type
!= EVENT_NONE
);
2734 if (type
!= EVENT_NONE
&& type
!= EVENT_ERROR
)
2740 static int event_read_print(struct event_format
*event
)
2742 enum event_type type
;
2746 if (read_expected_item(EVENT_ITEM
, "print") < 0)
2749 if (read_expected(EVENT_ITEM
, "fmt") < 0)
2752 if (read_expected(EVENT_OP
, ":") < 0)
2755 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
2759 event
->print_fmt
.format
= token
;
2760 event
->print_fmt
.args
= NULL
;
2762 /* ok to have no arg */
2763 type
= read_token_item(&token
);
2765 if (type
== EVENT_NONE
)
2768 /* Handle concatenation of print lines */
2769 if (type
== EVENT_DQUOTE
) {
2772 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
2774 strcpy(cat
, event
->print_fmt
.format
);
2777 free_token(event
->print_fmt
.format
);
2778 event
->print_fmt
.format
= NULL
;
2783 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2788 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
2800 * pevent_find_common_field - return a common field by event
2801 * @event: handle for the event
2802 * @name: the name of the common field to return
2804 * Returns a common field from the event by the given @name.
2805 * This only searchs the common fields and not all field.
2807 struct format_field
*
2808 pevent_find_common_field(struct event_format
*event
, const char *name
)
2810 struct format_field
*format
;
2812 for (format
= event
->format
.common_fields
;
2813 format
; format
= format
->next
) {
2814 if (strcmp(format
->name
, name
) == 0)
2822 * pevent_find_field - find a non-common field
2823 * @event: handle for the event
2824 * @name: the name of the non-common field
2826 * Returns a non-common field by the given @name.
2827 * This does not search common fields.
2829 struct format_field
*
2830 pevent_find_field(struct event_format
*event
, const char *name
)
2832 struct format_field
*format
;
2834 for (format
= event
->format
.fields
;
2835 format
; format
= format
->next
) {
2836 if (strcmp(format
->name
, name
) == 0)
2844 * pevent_find_any_field - find any field by name
2845 * @event: handle for the event
2846 * @name: the name of the field
2848 * Returns a field by the given @name.
2849 * This searchs the common field names first, then
2850 * the non-common ones if a common one was not found.
2852 struct format_field
*
2853 pevent_find_any_field(struct event_format
*event
, const char *name
)
2855 struct format_field
*format
;
2857 format
= pevent_find_common_field(event
, name
);
2860 return pevent_find_field(event
, name
);
2864 * pevent_read_number - read a number from data
2865 * @pevent: handle for the pevent
2866 * @ptr: the raw data
2867 * @size: the size of the data that holds the number
2869 * Returns the number (converted to host) from the
2872 unsigned long long pevent_read_number(struct pevent
*pevent
,
2873 const void *ptr
, int size
)
2877 return *(unsigned char *)ptr
;
2879 return data2host2(pevent
, ptr
);
2881 return data2host4(pevent
, ptr
);
2883 return data2host8(pevent
, ptr
);
2891 * pevent_read_number_field - read a number from data
2892 * @field: a handle to the field
2893 * @data: the raw data to read
2894 * @value: the value to place the number in
2896 * Reads raw data according to a field offset and size,
2897 * and translates it into @value.
2899 * Returns 0 on success, -1 otherwise.
2901 int pevent_read_number_field(struct format_field
*field
, const void *data
,
2902 unsigned long long *value
)
2906 switch (field
->size
) {
2911 *value
= pevent_read_number(field
->event
->pevent
,
2912 data
+ field
->offset
, field
->size
);
2919 static int get_common_info(struct pevent
*pevent
,
2920 const char *type
, int *offset
, int *size
)
2922 struct event_format
*event
;
2923 struct format_field
*field
;
2926 * All events should have the same common elements.
2927 * Pick any event to find where the type is;
2929 if (!pevent
->events
)
2930 die("no event_list!");
2932 event
= pevent
->events
[0];
2933 field
= pevent_find_common_field(event
, type
);
2937 *offset
= field
->offset
;
2938 *size
= field
->size
;
2943 static int __parse_common(struct pevent
*pevent
, void *data
,
2944 int *size
, int *offset
, const char *name
)
2949 ret
= get_common_info(pevent
, name
, offset
, size
);
2953 return pevent_read_number(pevent
, data
+ *offset
, *size
);
2956 static int trace_parse_common_type(struct pevent
*pevent
, void *data
)
2958 return __parse_common(pevent
, data
,
2959 &pevent
->type_size
, &pevent
->type_offset
,
2963 static int parse_common_pid(struct pevent
*pevent
, void *data
)
2965 return __parse_common(pevent
, data
,
2966 &pevent
->pid_size
, &pevent
->pid_offset
,
2970 static int parse_common_pc(struct pevent
*pevent
, void *data
)
2972 return __parse_common(pevent
, data
,
2973 &pevent
->pc_size
, &pevent
->pc_offset
,
2974 "common_preempt_count");
2977 static int parse_common_flags(struct pevent
*pevent
, void *data
)
2979 return __parse_common(pevent
, data
,
2980 &pevent
->flags_size
, &pevent
->flags_offset
,
2984 static int parse_common_lock_depth(struct pevent
*pevent
, void *data
)
2986 return __parse_common(pevent
, data
,
2987 &pevent
->ld_size
, &pevent
->ld_offset
,
2988 "common_lock_depth");
2991 static int parse_common_migrate_disable(struct pevent
*pevent
, void *data
)
2993 return __parse_common(pevent
, data
,
2994 &pevent
->ld_size
, &pevent
->ld_offset
,
2995 "common_migrate_disable");
2998 static int events_id_cmp(const void *a
, const void *b
);
3001 * pevent_find_event - find an event by given id
3002 * @pevent: a handle to the pevent
3003 * @id: the id of the event
3005 * Returns an event that has a given @id.
3007 struct event_format
*pevent_find_event(struct pevent
*pevent
, int id
)
3009 struct event_format
**eventptr
;
3010 struct event_format key
;
3011 struct event_format
*pkey
= &key
;
3013 /* Check cache first */
3014 if (pevent
->last_event
&& pevent
->last_event
->id
== id
)
3015 return pevent
->last_event
;
3019 eventptr
= bsearch(&pkey
, pevent
->events
, pevent
->nr_events
,
3020 sizeof(*pevent
->events
), events_id_cmp
);
3023 pevent
->last_event
= *eventptr
;
3031 * pevent_find_event_by_name - find an event by given name
3032 * @pevent: a handle to the pevent
3033 * @sys: the system name to search for
3034 * @name: the name of the event to search for
3036 * This returns an event with a given @name and under the system
3037 * @sys. If @sys is NULL the first event with @name is returned.
3039 struct event_format
*
3040 pevent_find_event_by_name(struct pevent
*pevent
,
3041 const char *sys
, const char *name
)
3043 struct event_format
*event
;
3046 if (pevent
->last_event
&&
3047 strcmp(pevent
->last_event
->name
, name
) == 0 &&
3048 (!sys
|| strcmp(pevent
->last_event
->system
, sys
) == 0))
3049 return pevent
->last_event
;
3051 for (i
= 0; i
< pevent
->nr_events
; i
++) {
3052 event
= pevent
->events
[i
];
3053 if (strcmp(event
->name
, name
) == 0) {
3056 if (strcmp(event
->system
, sys
) == 0)
3060 if (i
== pevent
->nr_events
)
3063 pevent
->last_event
= event
;
3067 static unsigned long long
3068 eval_num_arg(void *data
, int size
, struct event_format
*event
, struct print_arg
*arg
)
3070 struct pevent
*pevent
= event
->pevent
;
3071 unsigned long long val
= 0;
3072 unsigned long long left
, right
;
3073 struct print_arg
*typearg
= NULL
;
3074 struct print_arg
*larg
;
3075 unsigned long offset
;
3076 unsigned int field_size
;
3078 switch (arg
->type
) {
3083 return strtoull(arg
->atom
.atom
, NULL
, 0);
3085 if (!arg
->field
.field
) {
3086 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
3087 if (!arg
->field
.field
)
3088 die("field %s not found", arg
->field
.name
);
3090 /* must be a number */
3091 val
= pevent_read_number(pevent
, data
+ arg
->field
.field
->offset
,
3092 arg
->field
.field
->size
);
3099 val
= eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
3100 return eval_type(val
, arg
, 0);
3107 val
= process_defined_func(&s
, data
, size
, event
, arg
);
3108 trace_seq_destroy(&s
);
3112 if (strcmp(arg
->op
.op
, "[") == 0) {
3114 * Arrays are special, since we don't want
3115 * to read the arg as is.
3117 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3119 /* handle typecasts */
3120 larg
= arg
->op
.left
;
3121 while (larg
->type
== PRINT_TYPE
) {
3124 larg
= larg
->typecast
.item
;
3127 /* Default to long size */
3128 field_size
= pevent
->long_size
;
3130 switch (larg
->type
) {
3131 case PRINT_DYNAMIC_ARRAY
:
3132 offset
= pevent_read_number(pevent
,
3133 data
+ larg
->dynarray
.field
->offset
,
3134 larg
->dynarray
.field
->size
);
3135 if (larg
->dynarray
.field
->elementsize
)
3136 field_size
= larg
->dynarray
.field
->elementsize
;
3138 * The actual length of the dynamic array is stored
3139 * in the top half of the field, and the offset
3140 * is in the bottom half of the 32 bit field.
3146 if (!larg
->field
.field
) {
3148 pevent_find_any_field(event
, larg
->field
.name
);
3149 if (!larg
->field
.field
)
3150 die("field %s not found", larg
->field
.name
);
3152 field_size
= larg
->field
.field
->elementsize
;
3153 offset
= larg
->field
.field
->offset
+
3154 right
* larg
->field
.field
->elementsize
;
3157 goto default_op
; /* oops, all bets off */
3159 val
= pevent_read_number(pevent
,
3160 data
+ offset
, field_size
);
3162 val
= eval_type(val
, typearg
, 1);
3164 } else if (strcmp(arg
->op
.op
, "?") == 0) {
3165 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3166 arg
= arg
->op
.right
;
3168 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3170 val
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3174 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3175 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3176 switch (arg
->op
.op
[0]) {
3178 switch (arg
->op
.op
[1]) {
3183 val
= left
!= right
;
3186 die("unknown op '%s'", arg
->op
.op
);
3194 val
= left
|| right
;
3200 val
= left
&& right
;
3205 switch (arg
->op
.op
[1]) {
3210 val
= left
<< right
;
3213 val
= left
<= right
;
3216 die("unknown op '%s'", arg
->op
.op
);
3220 switch (arg
->op
.op
[1]) {
3225 val
= left
>> right
;
3228 val
= left
>= right
;
3231 die("unknown op '%s'", arg
->op
.op
);
3235 if (arg
->op
.op
[1] != '=')
3236 die("unknown op '%s'", arg
->op
.op
);
3237 val
= left
== right
;
3252 die("unknown op '%s'", arg
->op
.op
);
3255 default: /* not sure what to do there */
3263 unsigned long long value
;
3266 static const struct flag flags
[] = {
3267 { "HI_SOFTIRQ", 0 },
3268 { "TIMER_SOFTIRQ", 1 },
3269 { "NET_TX_SOFTIRQ", 2 },
3270 { "NET_RX_SOFTIRQ", 3 },
3271 { "BLOCK_SOFTIRQ", 4 },
3272 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3273 { "TASKLET_SOFTIRQ", 6 },
3274 { "SCHED_SOFTIRQ", 7 },
3275 { "HRTIMER_SOFTIRQ", 8 },
3276 { "RCU_SOFTIRQ", 9 },
3278 { "HRTIMER_NORESTART", 0 },
3279 { "HRTIMER_RESTART", 1 },
3282 static unsigned long long eval_flag(const char *flag
)
3287 * Some flags in the format files do not get converted.
3288 * If the flag is not numeric, see if it is something that
3289 * we already know about.
3291 if (isdigit(flag
[0]))
3292 return strtoull(flag
, NULL
, 0);
3294 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
3295 if (strcmp(flags
[i
].name
, flag
) == 0)
3296 return flags
[i
].value
;
3301 static void print_str_to_seq(struct trace_seq
*s
, const char *format
,
3302 int len_arg
, const char *str
)
3305 trace_seq_printf(s
, format
, len_arg
, str
);
3307 trace_seq_printf(s
, format
, str
);
3310 static void print_str_arg(struct trace_seq
*s
, void *data
, int size
,
3311 struct event_format
*event
, const char *format
,
3312 int len_arg
, struct print_arg
*arg
)
3314 struct pevent
*pevent
= event
->pevent
;
3315 struct print_flag_sym
*flag
;
3316 struct format_field
*field
;
3317 unsigned long long val
, fval
;
3324 switch (arg
->type
) {
3329 print_str_to_seq(s
, format
, len_arg
, arg
->atom
.atom
);
3332 field
= arg
->field
.field
;
3334 field
= pevent_find_any_field(event
, arg
->field
.name
);
3336 die("field %s not found", arg
->field
.name
);
3337 arg
->field
.field
= field
;
3339 /* Zero sized fields, mean the rest of the data */
3340 len
= field
->size
? : size
- field
->offset
;
3343 * Some events pass in pointers. If this is not an array
3344 * and the size is the same as long_size, assume that it
3347 if (!(field
->flags
& FIELD_IS_ARRAY
) &&
3348 field
->size
== pevent
->long_size
) {
3349 addr
= *(unsigned long *)(data
+ field
->offset
);
3350 trace_seq_printf(s
, "%lx", addr
);
3353 str
= malloc_or_die(len
+ 1);
3354 memcpy(str
, data
+ field
->offset
, len
);
3356 print_str_to_seq(s
, format
, len_arg
, str
);
3360 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
3362 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
3363 fval
= eval_flag(flag
->value
);
3364 if (!val
&& !fval
) {
3365 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3368 if (fval
&& (val
& fval
) == fval
) {
3369 if (print
&& arg
->flags
.delim
)
3370 trace_seq_puts(s
, arg
->flags
.delim
);
3371 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3378 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
3379 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
3380 fval
= eval_flag(flag
->value
);
3382 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3388 field
= arg
->hex
.field
->field
.field
;
3390 str
= arg
->hex
.field
->field
.name
;
3391 field
= pevent_find_any_field(event
, str
);
3393 die("field %s not found", str
);
3394 arg
->hex
.field
->field
.field
= field
;
3396 hex
= data
+ field
->offset
;
3397 len
= eval_num_arg(data
, size
, event
, arg
->hex
.size
);
3398 for (i
= 0; i
< len
; i
++) {
3400 trace_seq_putc(s
, ' ');
3401 trace_seq_printf(s
, "%02x", hex
[i
]);
3407 case PRINT_STRING
: {
3410 if (arg
->string
.offset
== -1) {
3411 struct format_field
*f
;
3413 f
= pevent_find_any_field(event
, arg
->string
.string
);
3414 arg
->string
.offset
= f
->offset
;
3416 str_offset
= data2host4(pevent
, data
+ arg
->string
.offset
);
3417 str_offset
&= 0xffff;
3418 print_str_to_seq(s
, format
, len_arg
, ((char *)data
) + str_offset
);
3422 print_str_to_seq(s
, format
, len_arg
, arg
->string
.string
);
3426 * The only op for string should be ? :
3428 if (arg
->op
.op
[0] != '?')
3430 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3432 print_str_arg(s
, data
, size
, event
,
3433 format
, len_arg
, arg
->op
.right
->op
.left
);
3435 print_str_arg(s
, data
, size
, event
,
3436 format
, len_arg
, arg
->op
.right
->op
.right
);
3439 process_defined_func(s
, data
, size
, event
, arg
);
3447 static unsigned long long
3448 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
3449 struct event_format
*event
, struct print_arg
*arg
)
3451 struct pevent_function_handler
*func_handle
= arg
->func
.func
;
3452 struct pevent_func_params
*param
;
3453 unsigned long long *args
;
3454 unsigned long long ret
;
3455 struct print_arg
*farg
;
3456 struct trace_seq str
;
3458 struct save_str
*next
;
3460 } *strings
= NULL
, *string
;
3463 if (!func_handle
->nr_args
) {
3464 ret
= (*func_handle
->func
)(s
, NULL
);
3468 farg
= arg
->func
.args
;
3469 param
= func_handle
->params
;
3471 args
= malloc_or_die(sizeof(*args
) * func_handle
->nr_args
);
3472 for (i
= 0; i
< func_handle
->nr_args
; i
++) {
3473 switch (param
->type
) {
3474 case PEVENT_FUNC_ARG_INT
:
3475 case PEVENT_FUNC_ARG_LONG
:
3476 case PEVENT_FUNC_ARG_PTR
:
3477 args
[i
] = eval_num_arg(data
, size
, event
, farg
);
3479 case PEVENT_FUNC_ARG_STRING
:
3480 trace_seq_init(&str
);
3481 print_str_arg(&str
, data
, size
, event
, "%s", -1, farg
);
3482 trace_seq_terminate(&str
);
3483 string
= malloc_or_die(sizeof(*string
));
3484 string
->next
= strings
;
3485 string
->str
= strdup(str
.buffer
);
3489 args
[i
] = (uintptr_t)string
->str
;
3491 trace_seq_destroy(&str
);
3495 * Something went totally wrong, this is not
3496 * an input error, something in this code broke.
3498 die("Unexpected end of arguments\n");
3502 param
= param
->next
;
3505 ret
= (*func_handle
->func
)(s
, args
);
3509 strings
= string
->next
;
3515 /* TBD : handle return type here */
3519 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event_format
*event
)
3521 struct pevent
*pevent
= event
->pevent
;
3522 struct format_field
*field
, *ip_field
;
3523 struct print_arg
*args
, *arg
, **next
;
3524 unsigned long long ip
, val
;
3529 field
= pevent
->bprint_buf_field
;
3530 ip_field
= pevent
->bprint_ip_field
;
3533 field
= pevent_find_field(event
, "buf");
3535 die("can't find buffer field for binary printk");
3536 ip_field
= pevent_find_field(event
, "ip");
3538 die("can't find ip field for binary printk");
3539 pevent
->bprint_buf_field
= field
;
3540 pevent
->bprint_ip_field
= ip_field
;
3543 ip
= pevent_read_number(pevent
, data
+ ip_field
->offset
, ip_field
->size
);
3546 * The first arg is the IP pointer.
3553 arg
->type
= PRINT_ATOM
;
3554 arg
->atom
.atom
= malloc_or_die(32);
3555 sprintf(arg
->atom
.atom
, "%lld", ip
);
3557 /* skip the first "%pf : " */
3558 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
3559 bptr
< data
+ size
&& *ptr
; ptr
++) {
3590 vsize
= pevent
->long_size
;
3604 /* the pointers are always 4 bytes aligned */
3605 bptr
= (void *)(((unsigned long)bptr
+ 3) &
3607 val
= pevent_read_number(pevent
, bptr
, vsize
);
3611 arg
->type
= PRINT_ATOM
;
3612 arg
->atom
.atom
= malloc_or_die(32);
3613 sprintf(arg
->atom
.atom
, "%lld", val
);
3617 * The '*' case means that an arg is used as the length.
3618 * We need to continue to figure out for what.
3627 arg
->type
= PRINT_BSTRING
;
3628 arg
->string
.string
= strdup(bptr
);
3629 if (!arg
->string
.string
)
3631 bptr
+= strlen(bptr
) + 1;
3643 static void free_args(struct print_arg
*args
)
3645 struct print_arg
*next
;
3656 get_bprint_format(void *data
, int size __maybe_unused
,
3657 struct event_format
*event
)
3659 struct pevent
*pevent
= event
->pevent
;
3660 unsigned long long addr
;
3661 struct format_field
*field
;
3662 struct printk_map
*printk
;
3666 field
= pevent
->bprint_fmt_field
;
3669 field
= pevent_find_field(event
, "fmt");
3671 die("can't find format field for binary printk");
3672 pevent
->bprint_fmt_field
= field
;
3675 addr
= pevent_read_number(pevent
, data
+ field
->offset
, field
->size
);
3677 printk
= find_printk(pevent
, addr
);
3679 format
= malloc_or_die(45);
3680 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
3686 /* Remove any quotes. */
3689 format
= malloc_or_die(strlen(p
) + 10);
3690 sprintf(format
, "%s : %s", "%pf", p
);
3691 /* remove ending quotes and new line since we will add one too */
3692 p
= format
+ strlen(format
) - 1;
3697 if (strcmp(p
, "\\n") == 0)
3703 static void print_mac_arg(struct trace_seq
*s
, int mac
, void *data
, int size
,
3704 struct event_format
*event
, struct print_arg
*arg
)
3707 char *fmt
= "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3709 if (arg
->type
== PRINT_FUNC
) {
3710 process_defined_func(s
, data
, size
, event
, arg
);
3714 if (arg
->type
!= PRINT_FIELD
) {
3715 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d",
3721 fmt
= "%.2x%.2x%.2x%.2x%.2x%.2x";
3722 if (!arg
->field
.field
) {
3724 pevent_find_any_field(event
, arg
->field
.name
);
3725 if (!arg
->field
.field
)
3726 die("field %s not found", arg
->field
.name
);
3728 if (arg
->field
.field
->size
!= 6) {
3729 trace_seq_printf(s
, "INVALIDMAC");
3732 buf
= data
+ arg
->field
.field
->offset
;
3733 trace_seq_printf(s
, fmt
, buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5]);
3736 static int is_printable_array(char *p
, unsigned int len
)
3740 for (i
= 0; i
< len
&& p
[i
]; i
++)
3746 static void print_event_fields(struct trace_seq
*s
, void *data
, int size
,
3747 struct event_format
*event
)
3749 struct format_field
*field
;
3750 unsigned long long val
;
3751 unsigned int offset
, len
, i
;
3753 field
= event
->format
.fields
;
3755 trace_seq_printf(s
, " %s=", field
->name
);
3756 if (field
->flags
& FIELD_IS_ARRAY
) {
3757 offset
= field
->offset
;
3759 if (field
->flags
& FIELD_IS_DYNAMIC
) {
3760 val
= pevent_read_number(event
->pevent
, data
+ offset
, len
);
3765 if (field
->flags
& FIELD_IS_STRING
&&
3766 is_printable_array(data
+ offset
, len
)) {
3767 trace_seq_printf(s
, "%s", (char *)data
+ offset
);
3769 trace_seq_puts(s
, "ARRAY[");
3770 for (i
= 0; i
< len
; i
++) {
3772 trace_seq_puts(s
, ", ");
3773 trace_seq_printf(s
, "%02x",
3774 *((unsigned char *)data
+ offset
+ i
));
3776 trace_seq_putc(s
, ']');
3777 field
->flags
&= ~FIELD_IS_STRING
;
3780 val
= pevent_read_number(event
->pevent
, data
+ field
->offset
,
3782 if (field
->flags
& FIELD_IS_POINTER
) {
3783 trace_seq_printf(s
, "0x%llx", val
);
3784 } else if (field
->flags
& FIELD_IS_SIGNED
) {
3785 switch (field
->size
) {
3788 * If field is long then print it in hex.
3789 * A long usually stores pointers.
3791 if (field
->flags
& FIELD_IS_LONG
)
3792 trace_seq_printf(s
, "0x%x", (int)val
);
3794 trace_seq_printf(s
, "%d", (int)val
);
3797 trace_seq_printf(s
, "%2d", (short)val
);
3800 trace_seq_printf(s
, "%1d", (char)val
);
3803 trace_seq_printf(s
, "%lld", val
);
3806 if (field
->flags
& FIELD_IS_LONG
)
3807 trace_seq_printf(s
, "0x%llx", val
);
3809 trace_seq_printf(s
, "%llu", val
);
3812 field
= field
->next
;
3816 static void pretty_print(struct trace_seq
*s
, void *data
, int size
, struct event_format
*event
)
3818 struct pevent
*pevent
= event
->pevent
;
3819 struct print_fmt
*print_fmt
= &event
->print_fmt
;
3820 struct print_arg
*arg
= print_fmt
->args
;
3821 struct print_arg
*args
= NULL
;
3822 const char *ptr
= print_fmt
->format
;
3823 unsigned long long val
;
3824 struct func_map
*func
;
3825 const char *saveptr
;
3826 char *bprint_fmt
= NULL
;
3834 if (event
->flags
& EVENT_FL_FAILED
) {
3835 trace_seq_printf(s
, "[FAILED TO PARSE]");
3836 print_event_fields(s
, data
, size
, event
);
3840 if (event
->flags
& EVENT_FL_ISBPRINT
) {
3841 bprint_fmt
= get_bprint_format(data
, size
, event
);
3842 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
3847 for (; *ptr
; ptr
++) {
3853 trace_seq_putc(s
, '\n');
3856 trace_seq_putc(s
, '\t');
3859 trace_seq_putc(s
, '\r');
3862 trace_seq_putc(s
, '\\');
3865 trace_seq_putc(s
, *ptr
);
3869 } else if (*ptr
== '%') {
3877 trace_seq_putc(s
, '%');
3880 /* FIXME: need to handle properly */
3892 /* The argument is the length. */
3894 do_warning("no argument match");
3895 event
->flags
|= EVENT_FL_FAILED
;
3898 len_arg
= eval_num_arg(data
, size
, event
, arg
);
3908 if (pevent
->long_size
== 4)
3913 if (*(ptr
+1) == 'F' ||
3917 } else if (*(ptr
+1) == 'M' || *(ptr
+1) == 'm') {
3918 print_mac_arg(s
, *(ptr
+1), data
, size
, event
, arg
);
3931 do_warning("no argument match");
3932 event
->flags
|= EVENT_FL_FAILED
;
3936 len
= ((unsigned long)ptr
+ 1) -
3937 (unsigned long)saveptr
;
3939 /* should never happen */
3941 do_warning("bad format!");
3942 event
->flags
|= EVENT_FL_FAILED
;
3946 memcpy(format
, saveptr
, len
);
3949 val
= eval_num_arg(data
, size
, event
, arg
);
3953 func
= find_func(pevent
, val
);
3955 trace_seq_puts(s
, func
->func
);
3956 if (show_func
== 'F')
3963 if (pevent
->long_size
== 8 && ls
&&
3964 sizeof(long) != 8) {
3968 /* make %l into %ll */
3969 p
= strchr(format
, 'l');
3971 memmove(p
+1, p
, strlen(p
)+1);
3972 else if (strcmp(format
, "%p") == 0)
3973 strcpy(format
, "0x%llx");
3978 trace_seq_printf(s
, format
, len_arg
, (char)val
);
3980 trace_seq_printf(s
, format
, (char)val
);
3984 trace_seq_printf(s
, format
, len_arg
, (short)val
);
3986 trace_seq_printf(s
, format
, (short)val
);
3990 trace_seq_printf(s
, format
, len_arg
, (int)val
);
3992 trace_seq_printf(s
, format
, (int)val
);
3996 trace_seq_printf(s
, format
, len_arg
, (long)val
);
3998 trace_seq_printf(s
, format
, (long)val
);
4002 trace_seq_printf(s
, format
, len_arg
,
4005 trace_seq_printf(s
, format
, (long long)val
);
4008 do_warning("bad count (%d)", ls
);
4009 event
->flags
|= EVENT_FL_FAILED
;
4014 do_warning("no matching argument");
4015 event
->flags
|= EVENT_FL_FAILED
;
4019 len
= ((unsigned long)ptr
+ 1) -
4020 (unsigned long)saveptr
;
4022 /* should never happen */
4024 do_warning("bad format!");
4025 event
->flags
|= EVENT_FL_FAILED
;
4029 memcpy(format
, saveptr
, len
);
4033 print_str_arg(s
, data
, size
, event
,
4034 format
, len_arg
, arg
);
4038 trace_seq_printf(s
, ">%c<", *ptr
);
4042 trace_seq_putc(s
, *ptr
);
4045 if (event
->flags
& EVENT_FL_FAILED
) {
4047 trace_seq_printf(s
, "[FAILED TO PARSE]");
4057 * pevent_data_lat_fmt - parse the data for the latency format
4058 * @pevent: a handle to the pevent
4059 * @s: the trace_seq to write to
4060 * @record: the record to read from
4062 * This parses out the Latency format (interrupts disabled,
4063 * need rescheduling, in hard/soft interrupt, preempt count
4064 * and lock depth) and places it into the trace_seq.
4066 void pevent_data_lat_fmt(struct pevent
*pevent
,
4067 struct trace_seq
*s
, struct pevent_record
*record
)
4069 static int check_lock_depth
= 1;
4070 static int check_migrate_disable
= 1;
4071 static int lock_depth_exists
;
4072 static int migrate_disable_exists
;
4073 unsigned int lat_flags
;
4076 int migrate_disable
;
4079 void *data
= record
->data
;
4081 lat_flags
= parse_common_flags(pevent
, data
);
4082 pc
= parse_common_pc(pevent
, data
);
4083 /* lock_depth may not always exist */
4084 if (lock_depth_exists
)
4085 lock_depth
= parse_common_lock_depth(pevent
, data
);
4086 else if (check_lock_depth
) {
4087 lock_depth
= parse_common_lock_depth(pevent
, data
);
4089 check_lock_depth
= 0;
4091 lock_depth_exists
= 1;
4094 /* migrate_disable may not always exist */
4095 if (migrate_disable_exists
)
4096 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4097 else if (check_migrate_disable
) {
4098 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4099 if (migrate_disable
< 0)
4100 check_migrate_disable
= 0;
4102 migrate_disable_exists
= 1;
4105 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
4106 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
4108 trace_seq_printf(s
, "%c%c%c",
4109 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
4110 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
4112 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
4114 (hardirq
&& softirq
) ? 'H' :
4115 hardirq
? 'h' : softirq
? 's' : '.');
4118 trace_seq_printf(s
, "%x", pc
);
4120 trace_seq_putc(s
, '.');
4122 if (migrate_disable_exists
) {
4123 if (migrate_disable
< 0)
4124 trace_seq_putc(s
, '.');
4126 trace_seq_printf(s
, "%d", migrate_disable
);
4129 if (lock_depth_exists
) {
4131 trace_seq_putc(s
, '.');
4133 trace_seq_printf(s
, "%d", lock_depth
);
4136 trace_seq_terminate(s
);
4140 * pevent_data_type - parse out the given event type
4141 * @pevent: a handle to the pevent
4142 * @rec: the record to read from
4144 * This returns the event id from the @rec.
4146 int pevent_data_type(struct pevent
*pevent
, struct pevent_record
*rec
)
4148 return trace_parse_common_type(pevent
, rec
->data
);
4152 * pevent_data_event_from_type - find the event by a given type
4153 * @pevent: a handle to the pevent
4154 * @type: the type of the event.
4156 * This returns the event form a given @type;
4158 struct event_format
*pevent_data_event_from_type(struct pevent
*pevent
, int type
)
4160 return pevent_find_event(pevent
, type
);
4164 * pevent_data_pid - parse the PID from raw data
4165 * @pevent: a handle to the pevent
4166 * @rec: the record to parse
4168 * This returns the PID from a raw data.
4170 int pevent_data_pid(struct pevent
*pevent
, struct pevent_record
*rec
)
4172 return parse_common_pid(pevent
, rec
->data
);
4176 * pevent_data_comm_from_pid - return the command line from PID
4177 * @pevent: a handle to the pevent
4178 * @pid: the PID of the task to search for
4180 * This returns a pointer to the command line that has the given
4183 const char *pevent_data_comm_from_pid(struct pevent
*pevent
, int pid
)
4187 comm
= find_cmdline(pevent
, pid
);
4192 * pevent_data_comm_from_pid - parse the data into the print format
4193 * @s: the trace_seq to write to
4194 * @event: the handle to the event
4195 * @record: the record to read from
4197 * This parses the raw @data using the given @event information and
4198 * writes the print format into the trace_seq.
4200 void pevent_event_info(struct trace_seq
*s
, struct event_format
*event
,
4201 struct pevent_record
*record
)
4203 int print_pretty
= 1;
4205 if (event
->pevent
->print_raw
)
4206 print_event_fields(s
, record
->data
, record
->size
, event
);
4210 print_pretty
= event
->handler(s
, record
, event
,
4214 pretty_print(s
, record
->data
, record
->size
, event
);
4217 trace_seq_terminate(s
);
4220 void pevent_print_event(struct pevent
*pevent
, struct trace_seq
*s
,
4221 struct pevent_record
*record
)
4223 static char *spaces
= " "; /* 20 spaces */
4224 struct event_format
*event
;
4226 unsigned long usecs
;
4227 unsigned long nsecs
;
4229 void *data
= record
->data
;
4235 secs
= record
->ts
/ NSECS_PER_SEC
;
4236 nsecs
= record
->ts
- secs
* NSECS_PER_SEC
;
4238 if (record
->size
< 0) {
4239 do_warning("ug! negative record size %d", record
->size
);
4243 type
= trace_parse_common_type(pevent
, data
);
4245 event
= pevent_find_event(pevent
, type
);
4247 do_warning("ug! no event found for type %d", type
);
4251 pid
= parse_common_pid(pevent
, data
);
4252 comm
= find_cmdline(pevent
, pid
);
4254 if (pevent
->latency_format
) {
4255 trace_seq_printf(s
, "%8.8s-%-5d %3d",
4256 comm
, pid
, record
->cpu
);
4257 pevent_data_lat_fmt(pevent
, s
, record
);
4259 trace_seq_printf(s
, "%16s-%-5d [%03d]", comm
, pid
, record
->cpu
);
4261 if (pevent
->flags
& PEVENT_NSEC_OUTPUT
) {
4265 usecs
= (nsecs
+ 500) / NSECS_PER_USEC
;
4269 trace_seq_printf(s
, " %5lu.%0*lu: %s: ", secs
, p
, usecs
, event
->name
);
4271 /* Space out the event names evenly. */
4272 len
= strlen(event
->name
);
4274 trace_seq_printf(s
, "%.*s", 20 - len
, spaces
);
4276 pevent_event_info(s
, event
, record
);
4279 static int events_id_cmp(const void *a
, const void *b
)
4281 struct event_format
* const * ea
= a
;
4282 struct event_format
* const * eb
= b
;
4284 if ((*ea
)->id
< (*eb
)->id
)
4287 if ((*ea
)->id
> (*eb
)->id
)
4293 static int events_name_cmp(const void *a
, const void *b
)
4295 struct event_format
* const * ea
= a
;
4296 struct event_format
* const * eb
= b
;
4299 res
= strcmp((*ea
)->name
, (*eb
)->name
);
4303 res
= strcmp((*ea
)->system
, (*eb
)->system
);
4307 return events_id_cmp(a
, b
);
4310 static int events_system_cmp(const void *a
, const void *b
)
4312 struct event_format
* const * ea
= a
;
4313 struct event_format
* const * eb
= b
;
4316 res
= strcmp((*ea
)->system
, (*eb
)->system
);
4320 res
= strcmp((*ea
)->name
, (*eb
)->name
);
4324 return events_id_cmp(a
, b
);
4327 struct event_format
**pevent_list_events(struct pevent
*pevent
, enum event_sort_type sort_type
)
4329 struct event_format
**events
;
4330 int (*sort
)(const void *a
, const void *b
);
4332 events
= pevent
->sort_events
;
4334 if (events
&& pevent
->last_type
== sort_type
)
4338 events
= malloc(sizeof(*events
) * (pevent
->nr_events
+ 1));
4342 memcpy(events
, pevent
->events
, sizeof(*events
) * pevent
->nr_events
);
4343 events
[pevent
->nr_events
] = NULL
;
4345 pevent
->sort_events
= events
;
4347 /* the internal events are sorted by id */
4348 if (sort_type
== EVENT_SORT_ID
) {
4349 pevent
->last_type
= sort_type
;
4354 switch (sort_type
) {
4356 sort
= events_id_cmp
;
4358 case EVENT_SORT_NAME
:
4359 sort
= events_name_cmp
;
4361 case EVENT_SORT_SYSTEM
:
4362 sort
= events_system_cmp
;
4368 qsort(events
, pevent
->nr_events
, sizeof(*events
), sort
);
4369 pevent
->last_type
= sort_type
;
4374 static struct format_field
**
4375 get_event_fields(const char *type
, const char *name
,
4376 int count
, struct format_field
*list
)
4378 struct format_field
**fields
;
4379 struct format_field
*field
;
4382 fields
= malloc_or_die(sizeof(*fields
) * (count
+ 1));
4383 for (field
= list
; field
; field
= field
->next
) {
4384 fields
[i
++] = field
;
4385 if (i
== count
+ 1) {
4386 do_warning("event %s has more %s fields than specified",
4394 do_warning("event %s has less %s fields than specified",
4403 * pevent_event_common_fields - return a list of common fields for an event
4404 * @event: the event to return the common fields of.
4406 * Returns an allocated array of fields. The last item in the array is NULL.
4407 * The array must be freed with free().
4409 struct format_field
**pevent_event_common_fields(struct event_format
*event
)
4411 return get_event_fields("common", event
->name
,
4412 event
->format
.nr_common
,
4413 event
->format
.common_fields
);
4417 * pevent_event_fields - return a list of event specific fields for an event
4418 * @event: the event to return the fields of.
4420 * Returns an allocated array of fields. The last item in the array is NULL.
4421 * The array must be freed with free().
4423 struct format_field
**pevent_event_fields(struct event_format
*event
)
4425 return get_event_fields("event", event
->name
,
4426 event
->format
.nr_fields
,
4427 event
->format
.fields
);
4430 static void print_fields(struct trace_seq
*s
, struct print_flag_sym
*field
)
4432 trace_seq_printf(s
, "{ %s, %s }", field
->value
, field
->str
);
4434 trace_seq_puts(s
, ", ");
4435 print_fields(s
, field
->next
);
4440 static void print_args(struct print_arg
*args
)
4442 int print_paren
= 1;
4445 switch (args
->type
) {
4450 printf("%s", args
->atom
.atom
);
4453 printf("REC->%s", args
->field
.name
);
4456 printf("__print_flags(");
4457 print_args(args
->flags
.field
);
4458 printf(", %s, ", args
->flags
.delim
);
4460 print_fields(&s
, args
->flags
.flags
);
4461 trace_seq_do_printf(&s
);
4462 trace_seq_destroy(&s
);
4466 printf("__print_symbolic(");
4467 print_args(args
->symbol
.field
);
4470 print_fields(&s
, args
->symbol
.symbols
);
4471 trace_seq_do_printf(&s
);
4472 trace_seq_destroy(&s
);
4476 printf("__print_hex(");
4477 print_args(args
->hex
.field
);
4479 print_args(args
->hex
.size
);
4484 printf("__get_str(%s)", args
->string
.string
);
4487 printf("(%s)", args
->typecast
.type
);
4488 print_args(args
->typecast
.item
);
4491 if (strcmp(args
->op
.op
, ":") == 0)
4495 print_args(args
->op
.left
);
4496 printf(" %s ", args
->op
.op
);
4497 print_args(args
->op
.right
);
4502 /* we should warn... */
4507 print_args(args
->next
);
4511 static void parse_header_field(const char *field
,
4512 int *offset
, int *size
, int mandatory
)
4514 unsigned long long save_input_buf_ptr
;
4515 unsigned long long save_input_buf_siz
;
4519 save_input_buf_ptr
= input_buf_ptr
;
4520 save_input_buf_siz
= input_buf_siz
;
4522 if (read_expected(EVENT_ITEM
, "field") < 0)
4524 if (read_expected(EVENT_OP
, ":") < 0)
4528 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4533 * If this is not a mandatory field, then test it first.
4536 if (read_expected(EVENT_ITEM
, field
) < 0)
4539 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4541 if (strcmp(token
, field
) != 0)
4546 if (read_expected(EVENT_OP
, ";") < 0)
4548 if (read_expected(EVENT_ITEM
, "offset") < 0)
4550 if (read_expected(EVENT_OP
, ":") < 0)
4552 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4554 *offset
= atoi(token
);
4556 if (read_expected(EVENT_OP
, ";") < 0)
4558 if (read_expected(EVENT_ITEM
, "size") < 0)
4560 if (read_expected(EVENT_OP
, ":") < 0)
4562 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4564 *size
= atoi(token
);
4566 if (read_expected(EVENT_OP
, ";") < 0)
4568 type
= read_token(&token
);
4569 if (type
!= EVENT_NEWLINE
) {
4570 /* newer versions of the kernel have a "signed" type */
4571 if (type
!= EVENT_ITEM
)
4574 if (strcmp(token
, "signed") != 0)
4579 if (read_expected(EVENT_OP
, ":") < 0)
4582 if (read_expect_type(EVENT_ITEM
, &token
))
4586 if (read_expected(EVENT_OP
, ";") < 0)
4589 if (read_expect_type(EVENT_NEWLINE
, &token
))
4597 input_buf_ptr
= save_input_buf_ptr
;
4598 input_buf_siz
= save_input_buf_siz
;
4605 * pevent_parse_header_page - parse the data stored in the header page
4606 * @pevent: the handle to the pevent
4607 * @buf: the buffer storing the header page format string
4608 * @size: the size of @buf
4609 * @long_size: the long size to use if there is no header
4611 * This parses the header page format for information on the
4612 * ring buffer used. The @buf should be copied from
4614 * /sys/kernel/debug/tracing/events/header_page
4616 int pevent_parse_header_page(struct pevent
*pevent
, char *buf
, unsigned long size
,
4623 * Old kernels did not have header page info.
4624 * Sorry but we just use what we find here in user space.
4626 pevent
->header_page_ts_size
= sizeof(long long);
4627 pevent
->header_page_size_size
= long_size
;
4628 pevent
->header_page_data_offset
= sizeof(long long) + long_size
;
4629 pevent
->old_format
= 1;
4632 init_input_buf(buf
, size
);
4634 parse_header_field("timestamp", &pevent
->header_page_ts_offset
,
4635 &pevent
->header_page_ts_size
, 1);
4636 parse_header_field("commit", &pevent
->header_page_size_offset
,
4637 &pevent
->header_page_size_size
, 1);
4638 parse_header_field("overwrite", &pevent
->header_page_overwrite
,
4640 parse_header_field("data", &pevent
->header_page_data_offset
,
4641 &pevent
->header_page_data_size
, 1);
4646 static int event_matches(struct event_format
*event
,
4647 int id
, const char *sys_name
,
4648 const char *event_name
)
4650 if (id
>= 0 && id
!= event
->id
)
4653 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
4656 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
4662 static void free_handler(struct event_handler
*handle
)
4664 free((void *)handle
->sys_name
);
4665 free((void *)handle
->event_name
);
4669 static int find_event_handle(struct pevent
*pevent
, struct event_format
*event
)
4671 struct event_handler
*handle
, **next
;
4673 for (next
= &pevent
->handlers
; *next
;
4674 next
= &(*next
)->next
) {
4676 if (event_matches(event
, handle
->id
,
4678 handle
->event_name
))
4685 pr_stat("overriding event (%d) %s:%s with new print handler",
4686 event
->id
, event
->system
, event
->name
);
4688 event
->handler
= handle
->func
;
4689 event
->context
= handle
->context
;
4691 *next
= handle
->next
;
4692 free_handler(handle
);
4698 * pevent_parse_event - parse the event format
4699 * @pevent: the handle to the pevent
4700 * @buf: the buffer storing the event format string
4701 * @size: the size of @buf
4702 * @sys: the system the event belongs to
4704 * This parses the event format and creates an event structure
4705 * to quickly parse raw data for a given event.
4707 * These files currently come from:
4709 * /sys/kernel/debug/tracing/events/.../.../format
4711 enum pevent_errno
pevent_parse_event(struct pevent
*pevent
, const char *buf
,
4712 unsigned long size
, const char *sys
)
4714 struct event_format
*event
;
4717 init_input_buf(buf
, size
);
4719 event
= alloc_event();
4721 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4723 event
->name
= event_read_name();
4726 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4727 goto event_alloc_failed
;
4730 if (strcmp(sys
, "ftrace") == 0) {
4731 event
->flags
|= EVENT_FL_ISFTRACE
;
4733 if (strcmp(event
->name
, "bprint") == 0)
4734 event
->flags
|= EVENT_FL_ISBPRINT
;
4737 event
->id
= event_read_id();
4738 if (event
->id
< 0) {
4739 ret
= PEVENT_ERRNO__READ_ID_FAILED
;
4741 * This isn't an allocation error actually.
4742 * But as the ID is critical, just bail out.
4744 goto event_alloc_failed
;
4747 event
->system
= strdup(sys
);
4748 if (!event
->system
) {
4749 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4750 goto event_alloc_failed
;
4753 /* Add pevent to event so that it can be referenced */
4754 event
->pevent
= pevent
;
4756 ret
= event_read_format(event
);
4758 ret
= PEVENT_ERRNO__READ_FORMAT_FAILED
;
4759 goto event_parse_failed
;
4763 * If the event has an override, don't print warnings if the event
4764 * print format fails to parse.
4766 if (find_event_handle(pevent
, event
))
4769 ret
= event_read_print(event
);
4772 ret
= PEVENT_ERRNO__READ_PRINT_FAILED
;
4773 goto event_parse_failed
;
4777 add_event(pevent
, event
);
4779 if (!ret
&& (event
->flags
& EVENT_FL_ISFTRACE
)) {
4780 struct format_field
*field
;
4781 struct print_arg
*arg
, **list
;
4783 /* old ftrace had no args */
4784 list
= &event
->print_fmt
.args
;
4785 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
4787 arg
->type
= PRINT_FIELD
;
4788 arg
->field
.name
= strdup(field
->name
);
4789 if (!arg
->field
.name
) {
4790 event
->flags
|= EVENT_FL_FAILED
;
4792 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
;
4794 arg
->field
.field
= field
;
4801 #define PRINT_ARGS 0
4802 if (PRINT_ARGS
&& event
->print_fmt
.args
)
4803 print_args(event
->print_fmt
.args
);
4808 event
->flags
|= EVENT_FL_FAILED
;
4809 /* still add it even if it failed */
4810 add_event(pevent
, event
);
4814 free(event
->system
);
4821 #define _PE(code, str) str
4822 static const char * const pevent_error_str
[] = {
4827 int pevent_strerror(struct pevent
*pevent
, enum pevent_errno errnum
,
4828 char *buf
, size_t buflen
)
4834 msg
= strerror_r(errnum
, buf
, buflen
);
4836 size_t len
= strlen(msg
);
4837 memcpy(buf
, msg
, min(buflen
- 1, len
));
4838 *(buf
+ min(buflen
- 1, len
)) = '\0';
4843 if (errnum
<= __PEVENT_ERRNO__START
||
4844 errnum
>= __PEVENT_ERRNO__END
)
4847 idx
= errnum
- __PEVENT_ERRNO__START
- 1;
4848 msg
= pevent_error_str
[idx
];
4851 case PEVENT_ERRNO__MEM_ALLOC_FAILED
:
4852 case PEVENT_ERRNO__PARSE_EVENT_FAILED
:
4853 case PEVENT_ERRNO__READ_ID_FAILED
:
4854 case PEVENT_ERRNO__READ_FORMAT_FAILED
:
4855 case PEVENT_ERRNO__READ_PRINT_FAILED
:
4856 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
:
4857 snprintf(buf
, buflen
, "%s", msg
);
4861 /* cannot reach here */
4868 int get_field_val(struct trace_seq
*s
, struct format_field
*field
,
4869 const char *name
, struct pevent_record
*record
,
4870 unsigned long long *val
, int err
)
4874 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
4878 if (pevent_read_number_field(field
, record
->data
, val
)) {
4880 trace_seq_printf(s
, " %s=INVALID", name
);
4888 * pevent_get_field_raw - return the raw pointer into the data field
4889 * @s: The seq to print to on error
4890 * @event: the event that the field is for
4891 * @name: The name of the field
4892 * @record: The record with the field name.
4893 * @len: place to store the field length.
4894 * @err: print default error if failed.
4896 * Returns a pointer into record->data of the field and places
4897 * the length of the field in @len.
4899 * On failure, it returns NULL.
4901 void *pevent_get_field_raw(struct trace_seq
*s
, struct event_format
*event
,
4902 const char *name
, struct pevent_record
*record
,
4905 struct format_field
*field
;
4906 void *data
= record
->data
;
4913 field
= pevent_find_field(event
, name
);
4917 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
4921 /* Allow @len to be NULL */
4925 offset
= field
->offset
;
4926 if (field
->flags
& FIELD_IS_DYNAMIC
) {
4927 offset
= pevent_read_number(event
->pevent
,
4928 data
+ offset
, field
->size
);
4929 *len
= offset
>> 16;
4934 return data
+ offset
;
4938 * pevent_get_field_val - find a field and return its value
4939 * @s: The seq to print to on error
4940 * @event: the event that the field is for
4941 * @name: The name of the field
4942 * @record: The record with the field name.
4943 * @val: place to store the value of the field.
4944 * @err: print default error if failed.
4946 * Returns 0 on success -1 on field not found.
4948 int pevent_get_field_val(struct trace_seq
*s
, struct event_format
*event
,
4949 const char *name
, struct pevent_record
*record
,
4950 unsigned long long *val
, int err
)
4952 struct format_field
*field
;
4957 field
= pevent_find_field(event
, name
);
4959 return get_field_val(s
, field
, name
, record
, val
, err
);
4963 * pevent_get_common_field_val - find a common field and return its value
4964 * @s: The seq to print to on error
4965 * @event: the event that the field is for
4966 * @name: The name of the field
4967 * @record: The record with the field name.
4968 * @val: place to store the value of the field.
4969 * @err: print default error if failed.
4971 * Returns 0 on success -1 on field not found.
4973 int pevent_get_common_field_val(struct trace_seq
*s
, struct event_format
*event
,
4974 const char *name
, struct pevent_record
*record
,
4975 unsigned long long *val
, int err
)
4977 struct format_field
*field
;
4982 field
= pevent_find_common_field(event
, name
);
4984 return get_field_val(s
, field
, name
, record
, val
, err
);
4988 * pevent_get_any_field_val - find a any field and return its value
4989 * @s: The seq to print to on error
4990 * @event: the event that the field is for
4991 * @name: The name of the field
4992 * @record: The record with the field name.
4993 * @val: place to store the value of the field.
4994 * @err: print default error if failed.
4996 * Returns 0 on success -1 on field not found.
4998 int pevent_get_any_field_val(struct trace_seq
*s
, struct event_format
*event
,
4999 const char *name
, struct pevent_record
*record
,
5000 unsigned long long *val
, int err
)
5002 struct format_field
*field
;
5007 field
= pevent_find_any_field(event
, name
);
5009 return get_field_val(s
, field
, name
, record
, val
, err
);
5013 * pevent_print_num_field - print a field and a format
5014 * @s: The seq to print to
5015 * @fmt: The printf format to print the field with.
5016 * @event: the event that the field is for
5017 * @name: The name of the field
5018 * @record: The record with the field name.
5019 * @err: print default error if failed.
5021 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5023 int pevent_print_num_field(struct trace_seq
*s
, const char *fmt
,
5024 struct event_format
*event
, const char *name
,
5025 struct pevent_record
*record
, int err
)
5027 struct format_field
*field
= pevent_find_field(event
, name
);
5028 unsigned long long val
;
5033 if (pevent_read_number_field(field
, record
->data
, &val
))
5036 return trace_seq_printf(s
, fmt
, val
);
5040 trace_seq_printf(s
, "CAN'T FIND FIELD \"%s\"", name
);
5044 static void free_func_handle(struct pevent_function_handler
*func
)
5046 struct pevent_func_params
*params
;
5050 while (func
->params
) {
5051 params
= func
->params
;
5052 func
->params
= params
->next
;
5060 * pevent_register_print_function - register a helper function
5061 * @pevent: the handle to the pevent
5062 * @func: the function to process the helper function
5063 * @ret_type: the return type of the helper function
5064 * @name: the name of the helper function
5065 * @parameters: A list of enum pevent_func_arg_type
5067 * Some events may have helper functions in the print format arguments.
5068 * This allows a plugin to dynamically create a way to process one
5069 * of these functions.
5071 * The @parameters is a variable list of pevent_func_arg_type enums that
5072 * must end with PEVENT_FUNC_ARG_VOID.
5074 int pevent_register_print_function(struct pevent
*pevent
,
5075 pevent_func_handler func
,
5076 enum pevent_func_arg_type ret_type
,
5079 struct pevent_function_handler
*func_handle
;
5080 struct pevent_func_params
**next_param
;
5081 struct pevent_func_params
*param
;
5082 enum pevent_func_arg_type type
;
5086 func_handle
= find_func_handler(pevent
, name
);
5089 * This is most like caused by the users own
5090 * plugins updating the function. This overrides the
5093 pr_stat("override of function helper '%s'", name
);
5094 remove_func_handler(pevent
, name
);
5097 func_handle
= malloc(sizeof(*func_handle
));
5099 do_warning("Failed to allocate function handler");
5100 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5102 memset(func_handle
, 0, sizeof(*func_handle
));
5104 func_handle
->ret_type
= ret_type
;
5105 func_handle
->name
= strdup(name
);
5106 func_handle
->func
= func
;
5107 if (!func_handle
->name
) {
5108 do_warning("Failed to allocate function name");
5110 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5113 next_param
= &(func_handle
->params
);
5116 type
= va_arg(ap
, enum pevent_func_arg_type
);
5117 if (type
== PEVENT_FUNC_ARG_VOID
)
5120 if (type
< 0 || type
>= PEVENT_FUNC_ARG_MAX_TYPES
) {
5121 do_warning("Invalid argument type %d", type
);
5122 ret
= PEVENT_ERRNO__INVALID_ARG_TYPE
;
5126 param
= malloc(sizeof(*param
));
5128 do_warning("Failed to allocate function param");
5129 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5135 *next_param
= param
;
5136 next_param
= &(param
->next
);
5138 func_handle
->nr_args
++;
5142 func_handle
->next
= pevent
->func_handlers
;
5143 pevent
->func_handlers
= func_handle
;
5148 free_func_handle(func_handle
);
5153 * pevent_register_event_handler - register a way to parse an event
5154 * @pevent: the handle to the pevent
5155 * @id: the id of the event to register
5156 * @sys_name: the system name the event belongs to
5157 * @event_name: the name of the event
5158 * @func: the function to call to parse the event information
5159 * @context: the data to be passed to @func
5161 * This function allows a developer to override the parsing of
5162 * a given event. If for some reason the default print format
5163 * is not sufficient, this function will register a function
5164 * for an event to be used to parse the data instead.
5166 * If @id is >= 0, then it is used to find the event.
5167 * else @sys_name and @event_name are used.
5169 int pevent_register_event_handler(struct pevent
*pevent
,
5170 int id
, char *sys_name
, char *event_name
,
5171 pevent_event_handler_func func
,
5174 struct event_format
*event
;
5175 struct event_handler
*handle
;
5179 event
= pevent_find_event(pevent
, id
);
5182 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
5184 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
5187 event
= pevent_find_event_by_name(pevent
, sys_name
, event_name
);
5192 pr_stat("overriding event (%d) %s:%s with new print handler",
5193 event
->id
, event
->system
, event
->name
);
5195 event
->handler
= func
;
5196 event
->context
= context
;
5200 /* Save for later use. */
5201 handle
= malloc(sizeof(*handle
));
5203 do_warning("Failed to allocate event handler");
5204 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5207 memset(handle
, 0, sizeof(*handle
));
5210 handle
->event_name
= strdup(event_name
);
5212 handle
->sys_name
= strdup(sys_name
);
5214 if ((event_name
&& !handle
->event_name
) ||
5215 (sys_name
&& !handle
->sys_name
)) {
5216 do_warning("Failed to allocate event/sys name");
5217 free((void *)handle
->event_name
);
5218 free((void *)handle
->sys_name
);
5220 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5223 handle
->func
= func
;
5224 handle
->next
= pevent
->handlers
;
5225 pevent
->handlers
= handle
;
5226 handle
->context
= context
;
5232 * pevent_alloc - create a pevent handle
5234 struct pevent
*pevent_alloc(void)
5236 struct pevent
*pevent
;
5238 pevent
= malloc(sizeof(*pevent
));
5241 memset(pevent
, 0, sizeof(*pevent
));
5242 pevent
->ref_count
= 1;
5247 void pevent_ref(struct pevent
*pevent
)
5249 pevent
->ref_count
++;
5252 static void free_format_fields(struct format_field
*field
)
5254 struct format_field
*next
;
5265 static void free_formats(struct format
*format
)
5267 free_format_fields(format
->common_fields
);
5268 free_format_fields(format
->fields
);
5271 static void free_event(struct event_format
*event
)
5274 free(event
->system
);
5276 free_formats(&event
->format
);
5278 free(event
->print_fmt
.format
);
5279 free_args(event
->print_fmt
.args
);
5285 * pevent_free - free a pevent handle
5286 * @pevent: the pevent handle to free
5288 void pevent_free(struct pevent
*pevent
)
5290 struct cmdline_list
*cmdlist
, *cmdnext
;
5291 struct func_list
*funclist
, *funcnext
;
5292 struct printk_list
*printklist
, *printknext
;
5293 struct pevent_function_handler
*func_handler
;
5294 struct event_handler
*handle
;
5300 cmdlist
= pevent
->cmdlist
;
5301 funclist
= pevent
->funclist
;
5302 printklist
= pevent
->printklist
;
5304 pevent
->ref_count
--;
5305 if (pevent
->ref_count
)
5308 if (pevent
->cmdlines
) {
5309 for (i
= 0; i
< pevent
->cmdline_count
; i
++)
5310 free(pevent
->cmdlines
[i
].comm
);
5311 free(pevent
->cmdlines
);
5315 cmdnext
= cmdlist
->next
;
5316 free(cmdlist
->comm
);
5321 if (pevent
->func_map
) {
5322 for (i
= 0; i
< pevent
->func_count
; i
++) {
5323 free(pevent
->func_map
[i
].func
);
5324 free(pevent
->func_map
[i
].mod
);
5326 free(pevent
->func_map
);
5330 funcnext
= funclist
->next
;
5331 free(funclist
->func
);
5332 free(funclist
->mod
);
5334 funclist
= funcnext
;
5337 while (pevent
->func_handlers
) {
5338 func_handler
= pevent
->func_handlers
;
5339 pevent
->func_handlers
= func_handler
->next
;
5340 free_func_handle(func_handler
);
5343 if (pevent
->printk_map
) {
5344 for (i
= 0; i
< pevent
->printk_count
; i
++)
5345 free(pevent
->printk_map
[i
].printk
);
5346 free(pevent
->printk_map
);
5349 while (printklist
) {
5350 printknext
= printklist
->next
;
5351 free(printklist
->printk
);
5353 printklist
= printknext
;
5356 for (i
= 0; i
< pevent
->nr_events
; i
++)
5357 free_event(pevent
->events
[i
]);
5359 while (pevent
->handlers
) {
5360 handle
= pevent
->handlers
;
5361 pevent
->handlers
= handle
->next
;
5362 free_handler(handle
);
5365 free(pevent
->events
);
5366 free(pevent
->sort_events
);
5371 void pevent_unref(struct pevent
*pevent
)
5373 pevent_free(pevent
);