2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include "event-parse.h"
36 #include "event-utils.h"
38 static const char *input_buf
;
39 static unsigned long long input_buf_ptr
;
40 static unsigned long long input_buf_siz
;
42 static int is_flag_field
;
43 static int is_symbolic_field
;
45 static int show_warning
= 1;
47 #define do_warning(fmt, ...) \
50 warning(fmt, ##__VA_ARGS__); \
53 static void init_input_buf(const char *buf
, unsigned long long size
)
60 const char *pevent_get_input_buf(void)
65 unsigned long long pevent_get_input_buf_ptr(void)
70 struct event_handler
{
71 struct event_handler
*next
;
74 const char *event_name
;
75 pevent_event_handler_func func
;
79 struct pevent_func_params
{
80 struct pevent_func_params
*next
;
81 enum pevent_func_arg_type type
;
84 struct pevent_function_handler
{
85 struct pevent_function_handler
*next
;
86 enum pevent_func_arg_type ret_type
;
88 pevent_func_handler func
;
89 struct pevent_func_params
*params
;
93 static unsigned long long
94 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
95 struct event_format
*event
, struct print_arg
*arg
);
97 static void free_func_handle(struct pevent_function_handler
*func
);
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
107 void pevent_buffer_init(const char *buf
, unsigned long long size
)
109 init_input_buf(buf
, size
);
112 void breakpoint(void)
118 struct print_arg
*alloc_arg(void)
120 return calloc(1, sizeof(struct print_arg
));
128 static int cmdline_cmp(const void *a
, const void *b
)
130 const struct cmdline
*ca
= a
;
131 const struct cmdline
*cb
= b
;
133 if (ca
->pid
< cb
->pid
)
135 if (ca
->pid
> cb
->pid
)
141 struct cmdline_list
{
142 struct cmdline_list
*next
;
147 static int cmdline_init(struct pevent
*pevent
)
149 struct cmdline_list
*cmdlist
= pevent
->cmdlist
;
150 struct cmdline_list
*item
;
151 struct cmdline
*cmdlines
;
154 cmdlines
= malloc(sizeof(*cmdlines
) * pevent
->cmdline_count
);
160 cmdlines
[i
].pid
= cmdlist
->pid
;
161 cmdlines
[i
].comm
= cmdlist
->comm
;
164 cmdlist
= cmdlist
->next
;
168 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
170 pevent
->cmdlines
= cmdlines
;
171 pevent
->cmdlist
= NULL
;
176 static const char *find_cmdline(struct pevent
*pevent
, int pid
)
178 const struct cmdline
*comm
;
184 if (!pevent
->cmdlines
&& cmdline_init(pevent
))
185 return "<not enough memory for cmdlines!>";
189 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
190 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
198 * pevent_pid_is_registered - return if a pid has a cmdline registered
199 * @pevent: handle for the pevent
200 * @pid: The pid to check if it has a cmdline registered with.
202 * Returns 1 if the pid has a cmdline mapped to it
205 int pevent_pid_is_registered(struct pevent
*pevent
, int pid
)
207 const struct cmdline
*comm
;
213 if (!pevent
->cmdlines
&& cmdline_init(pevent
))
218 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
219 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
227 * If the command lines have been converted to an array, then
228 * we must add this pid. This is much slower than when cmdlines
229 * are added before the array is initialized.
231 static int add_new_comm(struct pevent
*pevent
, const char *comm
, int pid
)
233 struct cmdline
*cmdlines
= pevent
->cmdlines
;
234 const struct cmdline
*cmdline
;
240 /* avoid duplicates */
243 cmdline
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
244 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
250 cmdlines
= realloc(cmdlines
, sizeof(*cmdlines
) * (pevent
->cmdline_count
+ 1));
256 cmdlines
[pevent
->cmdline_count
].comm
= strdup(comm
);
257 if (!cmdlines
[pevent
->cmdline_count
].comm
) {
263 cmdlines
[pevent
->cmdline_count
].pid
= pid
;
265 if (cmdlines
[pevent
->cmdline_count
].comm
)
266 pevent
->cmdline_count
++;
268 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
269 pevent
->cmdlines
= cmdlines
;
275 * pevent_register_comm - register a pid / comm mapping
276 * @pevent: handle for the pevent
277 * @comm: the command line to register
278 * @pid: the pid to map the command line to
280 * This adds a mapping to search for command line names with
281 * a given pid. The comm is duplicated.
283 int pevent_register_comm(struct pevent
*pevent
, const char *comm
, int pid
)
285 struct cmdline_list
*item
;
287 if (pevent
->cmdlines
)
288 return add_new_comm(pevent
, comm
, pid
);
290 item
= malloc(sizeof(*item
));
294 item
->comm
= strdup(comm
);
300 item
->next
= pevent
->cmdlist
;
302 pevent
->cmdlist
= item
;
303 pevent
->cmdline_count
++;
309 unsigned long long addr
;
315 struct func_list
*next
;
316 unsigned long long addr
;
321 static int func_cmp(const void *a
, const void *b
)
323 const struct func_map
*fa
= a
;
324 const struct func_map
*fb
= b
;
326 if (fa
->addr
< fb
->addr
)
328 if (fa
->addr
> fb
->addr
)
335 * We are searching for a record in between, not an exact
338 static int func_bcmp(const void *a
, const void *b
)
340 const struct func_map
*fa
= a
;
341 const struct func_map
*fb
= b
;
343 if ((fa
->addr
== fb
->addr
) ||
345 (fa
->addr
> fb
->addr
&&
346 fa
->addr
< (fb
+1)->addr
))
349 if (fa
->addr
< fb
->addr
)
355 static int func_map_init(struct pevent
*pevent
)
357 struct func_list
*funclist
;
358 struct func_list
*item
;
359 struct func_map
*func_map
;
362 func_map
= malloc(sizeof(*func_map
) * (pevent
->func_count
+ 1));
366 funclist
= pevent
->funclist
;
370 func_map
[i
].func
= funclist
->func
;
371 func_map
[i
].addr
= funclist
->addr
;
372 func_map
[i
].mod
= funclist
->mod
;
375 funclist
= funclist
->next
;
379 qsort(func_map
, pevent
->func_count
, sizeof(*func_map
), func_cmp
);
382 * Add a special record at the end.
384 func_map
[pevent
->func_count
].func
= NULL
;
385 func_map
[pevent
->func_count
].addr
= 0;
386 func_map
[pevent
->func_count
].mod
= NULL
;
388 pevent
->func_map
= func_map
;
389 pevent
->funclist
= NULL
;
394 static struct func_map
*
395 find_func(struct pevent
*pevent
, unsigned long long addr
)
397 struct func_map
*func
;
400 if (!pevent
->func_map
)
401 func_map_init(pevent
);
405 func
= bsearch(&key
, pevent
->func_map
, pevent
->func_count
,
406 sizeof(*pevent
->func_map
), func_bcmp
);
412 * pevent_find_function - find a function by a given address
413 * @pevent: handle for the pevent
414 * @addr: the address to find the function with
416 * Returns a pointer to the function stored that has the given
417 * address. Note, the address does not have to be exact, it
418 * will select the function that would contain the address.
420 const char *pevent_find_function(struct pevent
*pevent
, unsigned long long addr
)
422 struct func_map
*map
;
424 map
= find_func(pevent
, addr
);
432 * pevent_find_function_address - find a function address by a given address
433 * @pevent: handle for the pevent
434 * @addr: the address to find the function with
436 * Returns the address the function starts at. This can be used in
437 * conjunction with pevent_find_function to print both the function
438 * name and the function offset.
441 pevent_find_function_address(struct pevent
*pevent
, unsigned long long addr
)
443 struct func_map
*map
;
445 map
= find_func(pevent
, addr
);
453 * pevent_register_function - register a function with a given address
454 * @pevent: handle for the pevent
455 * @function: the function name to register
456 * @addr: the address the function starts at
457 * @mod: the kernel module the function may be in (NULL for none)
459 * This registers a function name with an address and module.
460 * The @func passed in is duplicated.
462 int pevent_register_function(struct pevent
*pevent
, char *func
,
463 unsigned long long addr
, char *mod
)
465 struct func_list
*item
= malloc(sizeof(*item
));
470 item
->next
= pevent
->funclist
;
471 item
->func
= strdup(func
);
476 item
->mod
= strdup(mod
);
483 pevent
->funclist
= item
;
484 pevent
->func_count
++;
498 * pevent_print_funcs - print out the stored functions
499 * @pevent: handle for the pevent
501 * This prints out the stored functions.
503 void pevent_print_funcs(struct pevent
*pevent
)
507 if (!pevent
->func_map
)
508 func_map_init(pevent
);
510 for (i
= 0; i
< (int)pevent
->func_count
; i
++) {
512 pevent
->func_map
[i
].addr
,
513 pevent
->func_map
[i
].func
);
514 if (pevent
->func_map
[i
].mod
)
515 printf(" [%s]\n", pevent
->func_map
[i
].mod
);
522 unsigned long long addr
;
527 struct printk_list
*next
;
528 unsigned long long addr
;
532 static int printk_cmp(const void *a
, const void *b
)
534 const struct printk_map
*pa
= a
;
535 const struct printk_map
*pb
= b
;
537 if (pa
->addr
< pb
->addr
)
539 if (pa
->addr
> pb
->addr
)
545 static int printk_map_init(struct pevent
*pevent
)
547 struct printk_list
*printklist
;
548 struct printk_list
*item
;
549 struct printk_map
*printk_map
;
552 printk_map
= malloc(sizeof(*printk_map
) * (pevent
->printk_count
+ 1));
556 printklist
= pevent
->printklist
;
560 printk_map
[i
].printk
= printklist
->printk
;
561 printk_map
[i
].addr
= printklist
->addr
;
564 printklist
= printklist
->next
;
568 qsort(printk_map
, pevent
->printk_count
, sizeof(*printk_map
), printk_cmp
);
570 pevent
->printk_map
= printk_map
;
571 pevent
->printklist
= NULL
;
576 static struct printk_map
*
577 find_printk(struct pevent
*pevent
, unsigned long long addr
)
579 struct printk_map
*printk
;
580 struct printk_map key
;
582 if (!pevent
->printk_map
&& printk_map_init(pevent
))
587 printk
= bsearch(&key
, pevent
->printk_map
, pevent
->printk_count
,
588 sizeof(*pevent
->printk_map
), printk_cmp
);
594 * pevent_register_print_string - register a string by its address
595 * @pevent: handle for the pevent
596 * @fmt: the string format to register
597 * @addr: the address the string was located at
599 * This registers a string by the address it was stored in the kernel.
600 * The @fmt passed in is duplicated.
602 int pevent_register_print_string(struct pevent
*pevent
, char *fmt
,
603 unsigned long long addr
)
605 struct printk_list
*item
= malloc(sizeof(*item
));
610 item
->next
= pevent
->printklist
;
613 item
->printk
= strdup(fmt
);
617 pevent
->printklist
= item
;
618 pevent
->printk_count
++;
629 * pevent_print_printk - print out the stored strings
630 * @pevent: handle for the pevent
632 * This prints the string formats that were stored.
634 void pevent_print_printk(struct pevent
*pevent
)
638 if (!pevent
->printk_map
)
639 printk_map_init(pevent
);
641 for (i
= 0; i
< (int)pevent
->printk_count
; i
++) {
642 printf("%016llx %s\n",
643 pevent
->printk_map
[i
].addr
,
644 pevent
->printk_map
[i
].printk
);
648 static struct event_format
*alloc_event(void)
650 return calloc(1, sizeof(struct event_format
));
653 static int add_event(struct pevent
*pevent
, struct event_format
*event
)
656 struct event_format
**events
= realloc(pevent
->events
, sizeof(event
) *
657 (pevent
->nr_events
+ 1));
661 pevent
->events
= events
;
663 for (i
= 0; i
< pevent
->nr_events
; i
++) {
664 if (pevent
->events
[i
]->id
> event
->id
)
667 if (i
< pevent
->nr_events
)
668 memmove(&pevent
->events
[i
+ 1],
670 sizeof(event
) * (pevent
->nr_events
- i
));
672 pevent
->events
[i
] = event
;
675 event
->pevent
= pevent
;
680 static int event_item_type(enum event_type type
)
683 case EVENT_ITEM
... EVENT_SQUOTE
:
685 case EVENT_ERROR
... EVENT_DELIM
:
691 static void free_flag_sym(struct print_flag_sym
*fsym
)
693 struct print_flag_sym
*next
;
704 static void free_arg(struct print_arg
*arg
)
706 struct print_arg
*farg
;
713 free(arg
->atom
.atom
);
716 free(arg
->field
.name
);
719 free_arg(arg
->flags
.field
);
720 free(arg
->flags
.delim
);
721 free_flag_sym(arg
->flags
.flags
);
724 free_arg(arg
->symbol
.field
);
725 free_flag_sym(arg
->symbol
.symbols
);
728 free_arg(arg
->hex
.field
);
729 free_arg(arg
->hex
.size
);
732 free(arg
->typecast
.type
);
733 free_arg(arg
->typecast
.item
);
737 free(arg
->string
.string
);
739 case PRINT_DYNAMIC_ARRAY
:
740 free(arg
->dynarray
.index
);
744 free_arg(arg
->op
.left
);
745 free_arg(arg
->op
.right
);
748 while (arg
->func
.args
) {
749 farg
= arg
->func
.args
;
750 arg
->func
.args
= farg
->next
;
763 static enum event_type
get_type(int ch
)
766 return EVENT_NEWLINE
;
769 if (isalnum(ch
) || ch
== '_')
777 if (ch
== '(' || ch
== ')' || ch
== ',')
783 static int __read_char(void)
785 if (input_buf_ptr
>= input_buf_siz
)
788 return input_buf
[input_buf_ptr
++];
791 static int __peek_char(void)
793 if (input_buf_ptr
>= input_buf_siz
)
796 return input_buf
[input_buf_ptr
];
800 * pevent_peek_char - peek at the next character that will be read
802 * Returns the next character read, or -1 if end of buffer.
804 int pevent_peek_char(void)
806 return __peek_char();
809 static int extend_token(char **tok
, char *buf
, int size
)
811 char *newtok
= realloc(*tok
, size
);
828 static enum event_type
force_token(const char *str
, char **tok
);
830 static enum event_type
__read_token(char **tok
)
833 int ch
, last_ch
, quote_ch
, next_ch
;
836 enum event_type type
;
846 if (type
== EVENT_NONE
)
854 if (asprintf(tok
, "%c", ch
) < 0)
862 next_ch
= __peek_char();
863 if (next_ch
== '>') {
864 buf
[i
++] = __read_char();
877 buf
[i
++] = __read_char();
889 default: /* what should we do instead? */
899 buf
[i
++] = __read_char();
904 /* don't keep quotes */
910 if (i
== (BUFSIZ
- 1)) {
914 if (extend_token(tok
, buf
, tok_size
) < 0)
921 /* the '\' '\' will cancel itself */
922 if (ch
== '\\' && last_ch
== '\\')
924 } while (ch
!= quote_ch
|| last_ch
== '\\');
925 /* remove the last quote */
929 * For strings (double quotes) check the next token.
930 * If it is another string, concatinate the two.
932 if (type
== EVENT_DQUOTE
) {
933 unsigned long long save_input_buf_ptr
= input_buf_ptr
;
937 } while (isspace(ch
));
940 input_buf_ptr
= save_input_buf_ptr
;
945 case EVENT_ERROR
... EVENT_SPACE
:
951 while (get_type(__peek_char()) == type
) {
952 if (i
== (BUFSIZ
- 1)) {
956 if (extend_token(tok
, buf
, tok_size
) < 0)
966 if (extend_token(tok
, buf
, tok_size
+ i
+ 1) < 0)
969 if (type
== EVENT_ITEM
) {
971 * Older versions of the kernel has a bug that
972 * creates invalid symbols and will break the mac80211
973 * parsing. This is a work around to that bug.
975 * See Linux kernel commit:
976 * 811cb50baf63461ce0bdb234927046131fc7fa8b
978 if (strcmp(*tok
, "LOCAL_PR_FMT") == 0) {
981 return force_token("\"\%s\" ", tok
);
982 } else if (strcmp(*tok
, "STA_PR_FMT") == 0) {
985 return force_token("\" sta:%pM\" ", tok
);
986 } else if (strcmp(*tok
, "VIF_PR_FMT") == 0) {
989 return force_token("\" vif:%p(%d)\" ", tok
);
996 static enum event_type
force_token(const char *str
, char **tok
)
998 const char *save_input_buf
;
999 unsigned long long save_input_buf_ptr
;
1000 unsigned long long save_input_buf_siz
;
1001 enum event_type type
;
1003 /* save off the current input pointers */
1004 save_input_buf
= input_buf
;
1005 save_input_buf_ptr
= input_buf_ptr
;
1006 save_input_buf_siz
= input_buf_siz
;
1008 init_input_buf(str
, strlen(str
));
1010 type
= __read_token(tok
);
1012 /* reset back to original token */
1013 input_buf
= save_input_buf
;
1014 input_buf_ptr
= save_input_buf_ptr
;
1015 input_buf_siz
= save_input_buf_siz
;
1020 static void free_token(char *tok
)
1026 static enum event_type
read_token(char **tok
)
1028 enum event_type type
;
1031 type
= __read_token(tok
);
1032 if (type
!= EVENT_SPACE
)
1044 * pevent_read_token - access to utilites to use the pevent parser
1045 * @tok: The token to return
1047 * This will parse tokens from the string given by
1048 * pevent_init_data().
1050 * Returns the token type.
1052 enum event_type
pevent_read_token(char **tok
)
1054 return read_token(tok
);
1058 * pevent_free_token - free a token returned by pevent_read_token
1059 * @token: the token to free
1061 void pevent_free_token(char *token
)
1067 static enum event_type
read_token_item(char **tok
)
1069 enum event_type type
;
1072 type
= __read_token(tok
);
1073 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
1084 static int test_type(enum event_type type
, enum event_type expect
)
1086 if (type
!= expect
) {
1087 do_warning("Error: expected type %d but read %d",
1094 static int test_type_token(enum event_type type
, const char *token
,
1095 enum event_type expect
, const char *expect_tok
)
1097 if (type
!= expect
) {
1098 do_warning("Error: expected type %d but read %d",
1103 if (strcmp(token
, expect_tok
) != 0) {
1104 do_warning("Error: expected '%s' but read '%s'",
1111 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
1113 enum event_type type
;
1116 type
= read_token(tok
);
1118 type
= read_token_item(tok
);
1119 return test_type(type
, expect
);
1122 static int read_expect_type(enum event_type expect
, char **tok
)
1124 return __read_expect_type(expect
, tok
, 1);
1127 static int __read_expected(enum event_type expect
, const char *str
,
1130 enum event_type type
;
1135 type
= read_token(&token
);
1137 type
= read_token_item(&token
);
1139 ret
= test_type_token(type
, token
, expect
, str
);
1146 static int read_expected(enum event_type expect
, const char *str
)
1148 return __read_expected(expect
, str
, 1);
1151 static int read_expected_item(enum event_type expect
, const char *str
)
1153 return __read_expected(expect
, str
, 0);
1156 static char *event_read_name(void)
1160 if (read_expected(EVENT_ITEM
, "name") < 0)
1163 if (read_expected(EVENT_OP
, ":") < 0)
1166 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1176 static int event_read_id(void)
1181 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
1184 if (read_expected(EVENT_OP
, ":") < 0)
1187 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1190 id
= strtoul(token
, NULL
, 0);
1199 static int field_is_string(struct format_field
*field
)
1201 if ((field
->flags
& FIELD_IS_ARRAY
) &&
1202 (strstr(field
->type
, "char") || strstr(field
->type
, "u8") ||
1203 strstr(field
->type
, "s8")))
1209 static int field_is_dynamic(struct format_field
*field
)
1211 if (strncmp(field
->type
, "__data_loc", 10) == 0)
1217 static int field_is_long(struct format_field
*field
)
1219 /* includes long long */
1220 if (strstr(field
->type
, "long"))
1226 static unsigned int type_size(const char *name
)
1228 /* This covers all FIELD_IS_STRING types. */
1246 for (i
= 0; table
[i
].type
; i
++) {
1247 if (!strcmp(table
[i
].type
, name
))
1248 return table
[i
].size
;
1254 static int event_read_fields(struct event_format
*event
, struct format_field
**fields
)
1256 struct format_field
*field
= NULL
;
1257 enum event_type type
;
1263 unsigned int size_dynamic
= 0;
1265 type
= read_token(&token
);
1266 if (type
== EVENT_NEWLINE
) {
1273 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
1277 type
= read_token(&token
);
1279 * The ftrace fields may still use the "special" name.
1282 if (event
->flags
& EVENT_FL_ISFTRACE
&&
1283 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
1285 type
= read_token(&token
);
1288 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
1292 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1297 field
= calloc(1, sizeof(*field
));
1301 field
->event
= event
;
1303 /* read the rest of the type */
1305 type
= read_token(&token
);
1306 if (type
== EVENT_ITEM
||
1307 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
1309 * Some of the ftrace fields are broken and have
1310 * an illegal "." in them.
1312 (event
->flags
& EVENT_FL_ISFTRACE
&&
1313 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
1315 if (strcmp(token
, "*") == 0)
1316 field
->flags
|= FIELD_IS_POINTER
;
1320 new_type
= realloc(field
->type
,
1321 strlen(field
->type
) +
1322 strlen(last_token
) + 2);
1327 field
->type
= new_type
;
1328 strcat(field
->type
, " ");
1329 strcat(field
->type
, last_token
);
1332 field
->type
= last_token
;
1341 do_warning("%s: no type found", __func__
);
1344 field
->name
= last_token
;
1346 if (test_type(type
, EVENT_OP
))
1349 if (strcmp(token
, "[") == 0) {
1350 enum event_type last_type
= type
;
1351 char *brackets
= token
;
1355 field
->flags
|= FIELD_IS_ARRAY
;
1357 type
= read_token(&token
);
1359 if (type
== EVENT_ITEM
)
1360 field
->arraylen
= strtoul(token
, NULL
, 0);
1362 field
->arraylen
= 0;
1364 while (strcmp(token
, "]") != 0) {
1365 if (last_type
== EVENT_ITEM
&&
1372 new_brackets
= realloc(brackets
,
1374 strlen(token
) + len
);
1375 if (!new_brackets
) {
1379 brackets
= new_brackets
;
1381 strcat(brackets
, " ");
1382 strcat(brackets
, token
);
1383 /* We only care about the last token */
1384 field
->arraylen
= strtoul(token
, NULL
, 0);
1386 type
= read_token(&token
);
1387 if (type
== EVENT_NONE
) {
1388 do_warning("failed to find token");
1395 new_brackets
= realloc(brackets
, strlen(brackets
) + 2);
1396 if (!new_brackets
) {
1400 brackets
= new_brackets
;
1401 strcat(brackets
, "]");
1403 /* add brackets to type */
1405 type
= read_token(&token
);
1407 * If the next token is not an OP, then it is of
1408 * the format: type [] item;
1410 if (type
== EVENT_ITEM
) {
1412 new_type
= realloc(field
->type
,
1413 strlen(field
->type
) +
1414 strlen(field
->name
) +
1415 strlen(brackets
) + 2);
1420 field
->type
= new_type
;
1421 strcat(field
->type
, " ");
1422 strcat(field
->type
, field
->name
);
1423 size_dynamic
= type_size(field
->name
);
1424 free_token(field
->name
);
1425 strcat(field
->type
, brackets
);
1426 field
->name
= token
;
1427 type
= read_token(&token
);
1430 new_type
= realloc(field
->type
,
1431 strlen(field
->type
) +
1432 strlen(brackets
) + 1);
1437 field
->type
= new_type
;
1438 strcat(field
->type
, brackets
);
1443 if (field_is_string(field
))
1444 field
->flags
|= FIELD_IS_STRING
;
1445 if (field_is_dynamic(field
))
1446 field
->flags
|= FIELD_IS_DYNAMIC
;
1447 if (field_is_long(field
))
1448 field
->flags
|= FIELD_IS_LONG
;
1450 if (test_type_token(type
, token
, EVENT_OP
, ";"))
1454 if (read_expected(EVENT_ITEM
, "offset") < 0)
1457 if (read_expected(EVENT_OP
, ":") < 0)
1460 if (read_expect_type(EVENT_ITEM
, &token
))
1462 field
->offset
= strtoul(token
, NULL
, 0);
1465 if (read_expected(EVENT_OP
, ";") < 0)
1468 if (read_expected(EVENT_ITEM
, "size") < 0)
1471 if (read_expected(EVENT_OP
, ":") < 0)
1474 if (read_expect_type(EVENT_ITEM
, &token
))
1476 field
->size
= strtoul(token
, NULL
, 0);
1479 if (read_expected(EVENT_OP
, ";") < 0)
1482 type
= read_token(&token
);
1483 if (type
!= EVENT_NEWLINE
) {
1484 /* newer versions of the kernel have a "signed" type */
1485 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
1490 if (read_expected(EVENT_OP
, ":") < 0)
1493 if (read_expect_type(EVENT_ITEM
, &token
))
1496 if (strtoul(token
, NULL
, 0))
1497 field
->flags
|= FIELD_IS_SIGNED
;
1500 if (read_expected(EVENT_OP
, ";") < 0)
1503 if (read_expect_type(EVENT_NEWLINE
, &token
))
1509 if (field
->flags
& FIELD_IS_ARRAY
) {
1510 if (field
->arraylen
)
1511 field
->elementsize
= field
->size
/ field
->arraylen
;
1512 else if (field
->flags
& FIELD_IS_DYNAMIC
)
1513 field
->elementsize
= size_dynamic
;
1514 else if (field
->flags
& FIELD_IS_STRING
)
1515 field
->elementsize
= 1;
1516 else if (field
->flags
& FIELD_IS_LONG
)
1517 field
->elementsize
= event
->pevent
?
1518 event
->pevent
->long_size
:
1521 field
->elementsize
= field
->size
;
1524 fields
= &field
->next
;
1541 static int event_read_format(struct event_format
*event
)
1546 if (read_expected_item(EVENT_ITEM
, "format") < 0)
1549 if (read_expected(EVENT_OP
, ":") < 0)
1552 if (read_expect_type(EVENT_NEWLINE
, &token
))
1556 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1559 event
->format
.nr_common
= ret
;
1561 ret
= event_read_fields(event
, &event
->format
.fields
);
1564 event
->format
.nr_fields
= ret
;
1573 static enum event_type
1574 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
1575 char **tok
, enum event_type type
);
1577 static enum event_type
1578 process_arg(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1580 enum event_type type
;
1583 type
= read_token(&token
);
1586 return process_arg_token(event
, arg
, tok
, type
);
1589 static enum event_type
1590 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
);
1592 static enum event_type
1593 process_cond(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1595 struct print_arg
*arg
, *left
, *right
;
1596 enum event_type type
;
1601 right
= alloc_arg();
1603 if (!arg
|| !left
|| !right
) {
1604 do_warning("%s: not enough memory!", __func__
);
1605 /* arg will be freed at out_free */
1611 arg
->type
= PRINT_OP
;
1612 arg
->op
.left
= left
;
1613 arg
->op
.right
= right
;
1616 type
= process_arg(event
, left
, &token
);
1619 /* Handle other operations in the arguments */
1620 if (type
== EVENT_OP
&& strcmp(token
, ":") != 0) {
1621 type
= process_op(event
, left
, &token
);
1625 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1630 type
= process_arg(event
, right
, &token
);
1632 top
->op
.right
= arg
;
1638 /* Top may point to itself */
1639 top
->op
.right
= NULL
;
1645 static enum event_type
1646 process_array(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1648 struct print_arg
*arg
;
1649 enum event_type type
;
1654 do_warning("%s: not enough memory!", __func__
);
1655 /* '*tok' is set to top->op.op. No need to free. */
1661 type
= process_arg(event
, arg
, &token
);
1662 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1665 top
->op
.right
= arg
;
1668 type
= read_token_item(&token
);
1679 static int get_op_prio(char *op
)
1693 /* '>>' and '<<' are 8 */
1697 /* '==' and '!=' are 10 */
1707 do_warning("unknown op '%c'", op
[0]);
1711 if (strcmp(op
, "++") == 0 ||
1712 strcmp(op
, "--") == 0) {
1714 } else if (strcmp(op
, ">>") == 0 ||
1715 strcmp(op
, "<<") == 0) {
1717 } else if (strcmp(op
, ">=") == 0 ||
1718 strcmp(op
, "<=") == 0) {
1720 } else if (strcmp(op
, "==") == 0 ||
1721 strcmp(op
, "!=") == 0) {
1723 } else if (strcmp(op
, "&&") == 0) {
1725 } else if (strcmp(op
, "||") == 0) {
1728 do_warning("unknown op '%s'", op
);
1734 static int set_op_prio(struct print_arg
*arg
)
1737 /* single ops are the greatest */
1738 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
)
1741 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1743 return arg
->op
.prio
;
1746 /* Note, *tok does not get freed, but will most likely be saved */
1747 static enum event_type
1748 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1750 struct print_arg
*left
, *right
= NULL
;
1751 enum event_type type
;
1754 /* the op is passed in via tok */
1757 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1758 /* handle single op */
1760 do_warning("bad op token %s", token
);
1770 do_warning("bad op token %s", token
);
1775 /* make an empty left */
1780 left
->type
= PRINT_NULL
;
1781 arg
->op
.left
= left
;
1783 right
= alloc_arg();
1787 arg
->op
.right
= right
;
1789 /* do not free the token, it belongs to an op */
1791 type
= process_arg(event
, right
, tok
);
1793 } else if (strcmp(token
, "?") == 0) {
1799 /* copy the top arg to the left */
1802 arg
->type
= PRINT_OP
;
1804 arg
->op
.left
= left
;
1807 /* it will set arg->op.right */
1808 type
= process_cond(event
, arg
, tok
);
1810 } else if (strcmp(token
, ">>") == 0 ||
1811 strcmp(token
, "<<") == 0 ||
1812 strcmp(token
, "&") == 0 ||
1813 strcmp(token
, "|") == 0 ||
1814 strcmp(token
, "&&") == 0 ||
1815 strcmp(token
, "||") == 0 ||
1816 strcmp(token
, "-") == 0 ||
1817 strcmp(token
, "+") == 0 ||
1818 strcmp(token
, "*") == 0 ||
1819 strcmp(token
, "^") == 0 ||
1820 strcmp(token
, "/") == 0 ||
1821 strcmp(token
, "<") == 0 ||
1822 strcmp(token
, ">") == 0 ||
1823 strcmp(token
, "<=") == 0 ||
1824 strcmp(token
, ">=") == 0 ||
1825 strcmp(token
, "==") == 0 ||
1826 strcmp(token
, "!=") == 0) {
1832 /* copy the top arg to the left */
1835 arg
->type
= PRINT_OP
;
1837 arg
->op
.left
= left
;
1838 arg
->op
.right
= NULL
;
1840 if (set_op_prio(arg
) == -1) {
1841 event
->flags
|= EVENT_FL_FAILED
;
1842 /* arg->op.op (= token) will be freed at out_free */
1847 type
= read_token_item(&token
);
1850 /* could just be a type pointer */
1851 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1852 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1855 if (left
->type
!= PRINT_ATOM
) {
1856 do_warning("bad pointer type");
1859 new_atom
= realloc(left
->atom
.atom
,
1860 strlen(left
->atom
.atom
) + 3);
1864 left
->atom
.atom
= new_atom
;
1865 strcat(left
->atom
.atom
, " *");
1873 right
= alloc_arg();
1877 type
= process_arg_token(event
, right
, tok
, type
);
1878 arg
->op
.right
= right
;
1880 } else if (strcmp(token
, "[") == 0) {
1888 arg
->type
= PRINT_OP
;
1890 arg
->op
.left
= left
;
1894 /* it will set arg->op.right */
1895 type
= process_array(event
, arg
, tok
);
1898 do_warning("unknown op '%s'", token
);
1899 event
->flags
|= EVENT_FL_FAILED
;
1900 /* the arg is now the left side */
1904 if (type
== EVENT_OP
&& strcmp(*tok
, ":") != 0) {
1907 /* higher prios need to be closer to the root */
1908 prio
= get_op_prio(*tok
);
1910 if (prio
> arg
->op
.prio
)
1911 return process_op(event
, arg
, tok
);
1913 return process_op(event
, right
, tok
);
1919 do_warning("%s: not enough memory!", __func__
);
1926 static enum event_type
1927 process_entry(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
1930 enum event_type type
;
1934 if (read_expected(EVENT_OP
, "->") < 0)
1937 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1941 arg
->type
= PRINT_FIELD
;
1942 arg
->field
.name
= field
;
1944 if (is_flag_field
) {
1945 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
1946 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1948 } else if (is_symbolic_field
) {
1949 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
1950 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1951 is_symbolic_field
= 0;
1954 type
= read_token(&token
);
1966 static char *arg_eval (struct print_arg
*arg
);
1968 static unsigned long long
1969 eval_type_str(unsigned long long val
, const char *type
, int pointer
)
1979 if (type
[len
-1] != '*') {
1980 do_warning("pointer expected with non pointer type");
1986 do_warning("%s: not enough memory!", __func__
);
1989 memcpy(ref
, type
, len
);
1991 /* chop off the " *" */
1994 val
= eval_type_str(val
, ref
, 0);
1999 /* check if this is a pointer */
2000 if (type
[len
- 1] == '*')
2003 /* Try to figure out the arg size*/
2004 if (strncmp(type
, "struct", 6) == 0)
2008 if (strcmp(type
, "u8") == 0)
2011 if (strcmp(type
, "u16") == 0)
2012 return val
& 0xffff;
2014 if (strcmp(type
, "u32") == 0)
2015 return val
& 0xffffffff;
2017 if (strcmp(type
, "u64") == 0 ||
2018 strcmp(type
, "s64"))
2021 if (strcmp(type
, "s8") == 0)
2022 return (unsigned long long)(char)val
& 0xff;
2024 if (strcmp(type
, "s16") == 0)
2025 return (unsigned long long)(short)val
& 0xffff;
2027 if (strcmp(type
, "s32") == 0)
2028 return (unsigned long long)(int)val
& 0xffffffff;
2030 if (strncmp(type
, "unsigned ", 9) == 0) {
2035 if (strcmp(type
, "char") == 0) {
2037 return (unsigned long long)(char)val
& 0xff;
2042 if (strcmp(type
, "short") == 0) {
2044 return (unsigned long long)(short)val
& 0xffff;
2046 return val
& 0xffff;
2049 if (strcmp(type
, "int") == 0) {
2051 return (unsigned long long)(int)val
& 0xffffffff;
2053 return val
& 0xffffffff;
2060 * Try to figure out the type.
2062 static unsigned long long
2063 eval_type(unsigned long long val
, struct print_arg
*arg
, int pointer
)
2065 if (arg
->type
!= PRINT_TYPE
) {
2066 do_warning("expected type argument");
2070 return eval_type_str(val
, arg
->typecast
.type
, pointer
);
2073 static int arg_num_eval(struct print_arg
*arg
, long long *val
)
2075 long long left
, right
;
2078 switch (arg
->type
) {
2080 *val
= strtoll(arg
->atom
.atom
, NULL
, 0);
2083 ret
= arg_num_eval(arg
->typecast
.item
, val
);
2086 *val
= eval_type(*val
, arg
, 0);
2089 switch (arg
->op
.op
[0]) {
2091 ret
= arg_num_eval(arg
->op
.left
, &left
);
2094 ret
= arg_num_eval(arg
->op
.right
, &right
);
2098 *val
= left
|| right
;
2100 *val
= left
| right
;
2103 ret
= arg_num_eval(arg
->op
.left
, &left
);
2106 ret
= arg_num_eval(arg
->op
.right
, &right
);
2110 *val
= left
&& right
;
2112 *val
= left
& right
;
2115 ret
= arg_num_eval(arg
->op
.left
, &left
);
2118 ret
= arg_num_eval(arg
->op
.right
, &right
);
2121 switch (arg
->op
.op
[1]) {
2123 *val
= left
< right
;
2126 *val
= left
<< right
;
2129 *val
= left
<= right
;
2132 do_warning("unknown op '%s'", arg
->op
.op
);
2137 ret
= arg_num_eval(arg
->op
.left
, &left
);
2140 ret
= arg_num_eval(arg
->op
.right
, &right
);
2143 switch (arg
->op
.op
[1]) {
2145 *val
= left
> right
;
2148 *val
= left
>> right
;
2151 *val
= left
>= right
;
2154 do_warning("unknown op '%s'", arg
->op
.op
);
2159 ret
= arg_num_eval(arg
->op
.left
, &left
);
2162 ret
= arg_num_eval(arg
->op
.right
, &right
);
2166 if (arg
->op
.op
[1] != '=') {
2167 do_warning("unknown op '%s'", arg
->op
.op
);
2170 *val
= left
== right
;
2173 ret
= arg_num_eval(arg
->op
.left
, &left
);
2176 ret
= arg_num_eval(arg
->op
.right
, &right
);
2180 switch (arg
->op
.op
[1]) {
2182 *val
= left
!= right
;
2185 do_warning("unknown op '%s'", arg
->op
.op
);
2190 /* check for negative */
2191 if (arg
->op
.left
->type
== PRINT_NULL
)
2194 ret
= arg_num_eval(arg
->op
.left
, &left
);
2197 ret
= arg_num_eval(arg
->op
.right
, &right
);
2200 *val
= left
- right
;
2203 if (arg
->op
.left
->type
== PRINT_NULL
)
2206 ret
= arg_num_eval(arg
->op
.left
, &left
);
2209 ret
= arg_num_eval(arg
->op
.right
, &right
);
2212 *val
= left
+ right
;
2215 do_warning("unknown op '%s'", arg
->op
.op
);
2221 case PRINT_FIELD
... PRINT_SYMBOL
:
2225 do_warning("invalid eval type %d", arg
->type
);
2232 static char *arg_eval (struct print_arg
*arg
)
2235 static char buf
[20];
2237 switch (arg
->type
) {
2239 return arg
->atom
.atom
;
2241 return arg_eval(arg
->typecast
.item
);
2243 if (!arg_num_eval(arg
, &val
))
2245 sprintf(buf
, "%lld", val
);
2249 case PRINT_FIELD
... PRINT_SYMBOL
:
2253 do_warning("invalid eval type %d", arg
->type
);
2260 static enum event_type
2261 process_fields(struct event_format
*event
, struct print_flag_sym
**list
, char **tok
)
2263 enum event_type type
;
2264 struct print_arg
*arg
= NULL
;
2265 struct print_flag_sym
*field
;
2271 type
= read_token_item(&token
);
2272 if (test_type_token(type
, token
, EVENT_OP
, "{"))
2280 type
= process_arg(event
, arg
, &token
);
2282 if (type
== EVENT_OP
)
2283 type
= process_op(event
, arg
, &token
);
2285 if (type
== EVENT_ERROR
)
2288 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2291 field
= calloc(1, sizeof(*field
));
2295 value
= arg_eval(arg
);
2297 goto out_free_field
;
2298 field
->value
= strdup(value
);
2299 if (field
->value
== NULL
)
2300 goto out_free_field
;
2308 type
= process_arg(event
, arg
, &token
);
2309 if (test_type_token(type
, token
, EVENT_OP
, "}"))
2310 goto out_free_field
;
2312 value
= arg_eval(arg
);
2314 goto out_free_field
;
2315 field
->str
= strdup(value
);
2316 if (field
->str
== NULL
)
2317 goto out_free_field
;
2322 list
= &field
->next
;
2325 type
= read_token_item(&token
);
2326 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
2332 free_flag_sym(field
);
2341 static enum event_type
2342 process_flags(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2344 struct print_arg
*field
;
2345 enum event_type type
;
2348 memset(arg
, 0, sizeof(*arg
));
2349 arg
->type
= PRINT_FLAGS
;
2351 field
= alloc_arg();
2353 do_warning("%s: not enough memory!", __func__
);
2357 type
= process_arg(event
, field
, &token
);
2359 /* Handle operations in the first argument */
2360 while (type
== EVENT_OP
)
2361 type
= process_op(event
, field
, &token
);
2363 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2364 goto out_free_field
;
2367 arg
->flags
.field
= field
;
2369 type
= read_token_item(&token
);
2370 if (event_item_type(type
)) {
2371 arg
->flags
.delim
= token
;
2372 type
= read_token_item(&token
);
2375 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2378 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
2379 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2383 type
= read_token_item(tok
);
2394 static enum event_type
2395 process_symbols(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2397 struct print_arg
*field
;
2398 enum event_type type
;
2401 memset(arg
, 0, sizeof(*arg
));
2402 arg
->type
= PRINT_SYMBOL
;
2404 field
= alloc_arg();
2406 do_warning("%s: not enough memory!", __func__
);
2410 type
= process_arg(event
, field
, &token
);
2411 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2412 goto out_free_field
;
2414 arg
->symbol
.field
= field
;
2416 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
2417 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2421 type
= read_token_item(tok
);
2432 static enum event_type
2433 process_hex(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2435 struct print_arg
*field
;
2436 enum event_type type
;
2439 memset(arg
, 0, sizeof(*arg
));
2440 arg
->type
= PRINT_HEX
;
2442 field
= alloc_arg();
2444 do_warning("%s: not enough memory!", __func__
);
2448 type
= process_arg(event
, field
, &token
);
2450 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2453 arg
->hex
.field
= field
;
2457 field
= alloc_arg();
2459 do_warning("%s: not enough memory!", __func__
);
2464 type
= process_arg(event
, field
, &token
);
2466 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2469 arg
->hex
.size
= field
;
2472 type
= read_token_item(tok
);
2482 static enum event_type
2483 process_dynamic_array(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2485 struct format_field
*field
;
2486 enum event_type type
;
2489 memset(arg
, 0, sizeof(*arg
));
2490 arg
->type
= PRINT_DYNAMIC_ARRAY
;
2493 * The item within the parenthesis is another field that holds
2494 * the index into where the array starts.
2496 type
= read_token(&token
);
2498 if (type
!= EVENT_ITEM
)
2501 /* Find the field */
2503 field
= pevent_find_field(event
, token
);
2507 arg
->dynarray
.field
= field
;
2508 arg
->dynarray
.index
= 0;
2510 if (read_expected(EVENT_DELIM
, ")") < 0)
2514 type
= read_token_item(&token
);
2516 if (type
!= EVENT_OP
|| strcmp(token
, "[") != 0)
2522 do_warning("%s: not enough memory!", __func__
);
2527 type
= process_arg(event
, arg
, &token
);
2528 if (type
== EVENT_ERROR
)
2531 if (!test_type_token(type
, token
, EVENT_OP
, "]"))
2535 type
= read_token_item(tok
);
2546 static enum event_type
2547 process_paren(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2549 struct print_arg
*item_arg
;
2550 enum event_type type
;
2553 type
= process_arg(event
, arg
, &token
);
2555 if (type
== EVENT_ERROR
)
2558 if (type
== EVENT_OP
)
2559 type
= process_op(event
, arg
, &token
);
2561 if (type
== EVENT_ERROR
)
2564 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2568 type
= read_token_item(&token
);
2571 * If the next token is an item or another open paren, then
2572 * this was a typecast.
2574 if (event_item_type(type
) ||
2575 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
2577 /* make this a typecast and contine */
2579 /* prevous must be an atom */
2580 if (arg
->type
!= PRINT_ATOM
) {
2581 do_warning("previous needed to be PRINT_ATOM");
2585 item_arg
= alloc_arg();
2587 do_warning("%s: not enough memory!", __func__
);
2591 arg
->type
= PRINT_TYPE
;
2592 arg
->typecast
.type
= arg
->atom
.atom
;
2593 arg
->typecast
.item
= item_arg
;
2594 type
= process_arg_token(event
, item_arg
, &token
, type
);
2608 static enum event_type
2609 process_str(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
2612 enum event_type type
;
2615 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2618 arg
->type
= PRINT_STRING
;
2619 arg
->string
.string
= token
;
2620 arg
->string
.offset
= -1;
2622 if (read_expected(EVENT_DELIM
, ")") < 0)
2625 type
= read_token(&token
);
2637 static struct pevent_function_handler
*
2638 find_func_handler(struct pevent
*pevent
, char *func_name
)
2640 struct pevent_function_handler
*func
;
2645 for (func
= pevent
->func_handlers
; func
; func
= func
->next
) {
2646 if (strcmp(func
->name
, func_name
) == 0)
2653 static void remove_func_handler(struct pevent
*pevent
, char *func_name
)
2655 struct pevent_function_handler
*func
;
2656 struct pevent_function_handler
**next
;
2658 next
= &pevent
->func_handlers
;
2659 while ((func
= *next
)) {
2660 if (strcmp(func
->name
, func_name
) == 0) {
2662 free_func_handle(func
);
2669 static enum event_type
2670 process_func_handler(struct event_format
*event
, struct pevent_function_handler
*func
,
2671 struct print_arg
*arg
, char **tok
)
2673 struct print_arg
**next_arg
;
2674 struct print_arg
*farg
;
2675 enum event_type type
;
2680 arg
->type
= PRINT_FUNC
;
2681 arg
->func
.func
= func
;
2685 next_arg
= &(arg
->func
.args
);
2686 for (i
= 0; i
< func
->nr_args
; i
++) {
2689 do_warning("%s: not enough memory!", __func__
);
2693 type
= process_arg(event
, farg
, &token
);
2694 if (i
< (func
->nr_args
- 1))
2699 if (test_type_token(type
, token
, EVENT_DELIM
, test
)) {
2706 next_arg
= &(farg
->next
);
2710 type
= read_token(&token
);
2716 static enum event_type
2717 process_function(struct event_format
*event
, struct print_arg
*arg
,
2718 char *token
, char **tok
)
2720 struct pevent_function_handler
*func
;
2722 if (strcmp(token
, "__print_flags") == 0) {
2725 return process_flags(event
, arg
, tok
);
2727 if (strcmp(token
, "__print_symbolic") == 0) {
2729 is_symbolic_field
= 1;
2730 return process_symbols(event
, arg
, tok
);
2732 if (strcmp(token
, "__print_hex") == 0) {
2734 return process_hex(event
, arg
, tok
);
2736 if (strcmp(token
, "__get_str") == 0) {
2738 return process_str(event
, arg
, tok
);
2740 if (strcmp(token
, "__get_dynamic_array") == 0) {
2742 return process_dynamic_array(event
, arg
, tok
);
2745 func
= find_func_handler(event
->pevent
, token
);
2748 return process_func_handler(event
, func
, arg
, tok
);
2751 do_warning("function %s not defined", token
);
2756 static enum event_type
2757 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
2758 char **tok
, enum event_type type
)
2767 if (strcmp(token
, "REC") == 0) {
2769 type
= process_entry(event
, arg
, &token
);
2773 /* test the next token */
2774 type
= read_token_item(&token
);
2777 * If the next token is a parenthesis, then this
2780 if (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0) {
2783 /* this will free atom. */
2784 type
= process_function(event
, arg
, atom
, &token
);
2787 /* atoms can be more than one token long */
2788 while (type
== EVENT_ITEM
) {
2790 new_atom
= realloc(atom
,
2791 strlen(atom
) + strlen(token
) + 2);
2800 strcat(atom
, token
);
2802 type
= read_token_item(&token
);
2805 arg
->type
= PRINT_ATOM
;
2806 arg
->atom
.atom
= atom
;
2811 arg
->type
= PRINT_ATOM
;
2812 arg
->atom
.atom
= token
;
2813 type
= read_token_item(&token
);
2816 if (strcmp(token
, "(") == 0) {
2818 type
= process_paren(event
, arg
, &token
);
2822 /* handle single ops */
2823 arg
->type
= PRINT_OP
;
2825 arg
->op
.left
= NULL
;
2826 type
= process_op(event
, arg
, &token
);
2828 /* On error, the op is freed */
2829 if (type
== EVENT_ERROR
)
2832 /* return error type if errored */
2835 case EVENT_ERROR
... EVENT_NEWLINE
:
2837 do_warning("unexpected type %d", type
);
2845 static int event_read_print_args(struct event_format
*event
, struct print_arg
**list
)
2847 enum event_type type
= EVENT_ERROR
;
2848 struct print_arg
*arg
;
2853 if (type
== EVENT_NEWLINE
) {
2854 type
= read_token_item(&token
);
2860 do_warning("%s: not enough memory!", __func__
);
2864 type
= process_arg(event
, arg
, &token
);
2866 if (type
== EVENT_ERROR
) {
2875 if (type
== EVENT_OP
) {
2876 type
= process_op(event
, arg
, &token
);
2878 if (type
== EVENT_ERROR
) {
2887 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
2894 } while (type
!= EVENT_NONE
);
2896 if (type
!= EVENT_NONE
&& type
!= EVENT_ERROR
)
2902 static int event_read_print(struct event_format
*event
)
2904 enum event_type type
;
2908 if (read_expected_item(EVENT_ITEM
, "print") < 0)
2911 if (read_expected(EVENT_ITEM
, "fmt") < 0)
2914 if (read_expected(EVENT_OP
, ":") < 0)
2917 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
2921 event
->print_fmt
.format
= token
;
2922 event
->print_fmt
.args
= NULL
;
2924 /* ok to have no arg */
2925 type
= read_token_item(&token
);
2927 if (type
== EVENT_NONE
)
2930 /* Handle concatenation of print lines */
2931 if (type
== EVENT_DQUOTE
) {
2934 if (asprintf(&cat
, "%s%s", event
->print_fmt
.format
, token
) < 0)
2937 free_token(event
->print_fmt
.format
);
2938 event
->print_fmt
.format
= NULL
;
2943 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2948 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
2960 * pevent_find_common_field - return a common field by event
2961 * @event: handle for the event
2962 * @name: the name of the common field to return
2964 * Returns a common field from the event by the given @name.
2965 * This only searchs the common fields and not all field.
2967 struct format_field
*
2968 pevent_find_common_field(struct event_format
*event
, const char *name
)
2970 struct format_field
*format
;
2972 for (format
= event
->format
.common_fields
;
2973 format
; format
= format
->next
) {
2974 if (strcmp(format
->name
, name
) == 0)
2982 * pevent_find_field - find a non-common field
2983 * @event: handle for the event
2984 * @name: the name of the non-common field
2986 * Returns a non-common field by the given @name.
2987 * This does not search common fields.
2989 struct format_field
*
2990 pevent_find_field(struct event_format
*event
, const char *name
)
2992 struct format_field
*format
;
2994 for (format
= event
->format
.fields
;
2995 format
; format
= format
->next
) {
2996 if (strcmp(format
->name
, name
) == 0)
3004 * pevent_find_any_field - find any field by name
3005 * @event: handle for the event
3006 * @name: the name of the field
3008 * Returns a field by the given @name.
3009 * This searchs the common field names first, then
3010 * the non-common ones if a common one was not found.
3012 struct format_field
*
3013 pevent_find_any_field(struct event_format
*event
, const char *name
)
3015 struct format_field
*format
;
3017 format
= pevent_find_common_field(event
, name
);
3020 return pevent_find_field(event
, name
);
3024 * pevent_read_number - read a number from data
3025 * @pevent: handle for the pevent
3026 * @ptr: the raw data
3027 * @size: the size of the data that holds the number
3029 * Returns the number (converted to host) from the
3032 unsigned long long pevent_read_number(struct pevent
*pevent
,
3033 const void *ptr
, int size
)
3037 return *(unsigned char *)ptr
;
3039 return data2host2(pevent
, ptr
);
3041 return data2host4(pevent
, ptr
);
3043 return data2host8(pevent
, ptr
);
3051 * pevent_read_number_field - read a number from data
3052 * @field: a handle to the field
3053 * @data: the raw data to read
3054 * @value: the value to place the number in
3056 * Reads raw data according to a field offset and size,
3057 * and translates it into @value.
3059 * Returns 0 on success, -1 otherwise.
3061 int pevent_read_number_field(struct format_field
*field
, const void *data
,
3062 unsigned long long *value
)
3066 switch (field
->size
) {
3071 *value
= pevent_read_number(field
->event
->pevent
,
3072 data
+ field
->offset
, field
->size
);
3079 static int get_common_info(struct pevent
*pevent
,
3080 const char *type
, int *offset
, int *size
)
3082 struct event_format
*event
;
3083 struct format_field
*field
;
3086 * All events should have the same common elements.
3087 * Pick any event to find where the type is;
3089 if (!pevent
->events
) {
3090 do_warning("no event_list!");
3094 event
= pevent
->events
[0];
3095 field
= pevent_find_common_field(event
, type
);
3099 *offset
= field
->offset
;
3100 *size
= field
->size
;
3105 static int __parse_common(struct pevent
*pevent
, void *data
,
3106 int *size
, int *offset
, const char *name
)
3111 ret
= get_common_info(pevent
, name
, offset
, size
);
3115 return pevent_read_number(pevent
, data
+ *offset
, *size
);
3118 static int trace_parse_common_type(struct pevent
*pevent
, void *data
)
3120 return __parse_common(pevent
, data
,
3121 &pevent
->type_size
, &pevent
->type_offset
,
3125 static int parse_common_pid(struct pevent
*pevent
, void *data
)
3127 return __parse_common(pevent
, data
,
3128 &pevent
->pid_size
, &pevent
->pid_offset
,
3132 static int parse_common_pc(struct pevent
*pevent
, void *data
)
3134 return __parse_common(pevent
, data
,
3135 &pevent
->pc_size
, &pevent
->pc_offset
,
3136 "common_preempt_count");
3139 static int parse_common_flags(struct pevent
*pevent
, void *data
)
3141 return __parse_common(pevent
, data
,
3142 &pevent
->flags_size
, &pevent
->flags_offset
,
3146 static int parse_common_lock_depth(struct pevent
*pevent
, void *data
)
3148 return __parse_common(pevent
, data
,
3149 &pevent
->ld_size
, &pevent
->ld_offset
,
3150 "common_lock_depth");
3153 static int parse_common_migrate_disable(struct pevent
*pevent
, void *data
)
3155 return __parse_common(pevent
, data
,
3156 &pevent
->ld_size
, &pevent
->ld_offset
,
3157 "common_migrate_disable");
3160 static int events_id_cmp(const void *a
, const void *b
);
3163 * pevent_find_event - find an event by given id
3164 * @pevent: a handle to the pevent
3165 * @id: the id of the event
3167 * Returns an event that has a given @id.
3169 struct event_format
*pevent_find_event(struct pevent
*pevent
, int id
)
3171 struct event_format
**eventptr
;
3172 struct event_format key
;
3173 struct event_format
*pkey
= &key
;
3175 /* Check cache first */
3176 if (pevent
->last_event
&& pevent
->last_event
->id
== id
)
3177 return pevent
->last_event
;
3181 eventptr
= bsearch(&pkey
, pevent
->events
, pevent
->nr_events
,
3182 sizeof(*pevent
->events
), events_id_cmp
);
3185 pevent
->last_event
= *eventptr
;
3193 * pevent_find_event_by_name - find an event by given name
3194 * @pevent: a handle to the pevent
3195 * @sys: the system name to search for
3196 * @name: the name of the event to search for
3198 * This returns an event with a given @name and under the system
3199 * @sys. If @sys is NULL the first event with @name is returned.
3201 struct event_format
*
3202 pevent_find_event_by_name(struct pevent
*pevent
,
3203 const char *sys
, const char *name
)
3205 struct event_format
*event
;
3208 if (pevent
->last_event
&&
3209 strcmp(pevent
->last_event
->name
, name
) == 0 &&
3210 (!sys
|| strcmp(pevent
->last_event
->system
, sys
) == 0))
3211 return pevent
->last_event
;
3213 for (i
= 0; i
< pevent
->nr_events
; i
++) {
3214 event
= pevent
->events
[i
];
3215 if (strcmp(event
->name
, name
) == 0) {
3218 if (strcmp(event
->system
, sys
) == 0)
3222 if (i
== pevent
->nr_events
)
3225 pevent
->last_event
= event
;
3229 static unsigned long long
3230 eval_num_arg(void *data
, int size
, struct event_format
*event
, struct print_arg
*arg
)
3232 struct pevent
*pevent
= event
->pevent
;
3233 unsigned long long val
= 0;
3234 unsigned long long left
, right
;
3235 struct print_arg
*typearg
= NULL
;
3236 struct print_arg
*larg
;
3237 unsigned long offset
;
3238 unsigned int field_size
;
3240 switch (arg
->type
) {
3245 return strtoull(arg
->atom
.atom
, NULL
, 0);
3247 if (!arg
->field
.field
) {
3248 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
3249 if (!arg
->field
.field
)
3250 goto out_warning_field
;
3253 /* must be a number */
3254 val
= pevent_read_number(pevent
, data
+ arg
->field
.field
->offset
,
3255 arg
->field
.field
->size
);
3262 val
= eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
3263 return eval_type(val
, arg
, 0);
3270 val
= process_defined_func(&s
, data
, size
, event
, arg
);
3271 trace_seq_destroy(&s
);
3275 if (strcmp(arg
->op
.op
, "[") == 0) {
3277 * Arrays are special, since we don't want
3278 * to read the arg as is.
3280 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3282 /* handle typecasts */
3283 larg
= arg
->op
.left
;
3284 while (larg
->type
== PRINT_TYPE
) {
3287 larg
= larg
->typecast
.item
;
3290 /* Default to long size */
3291 field_size
= pevent
->long_size
;
3293 switch (larg
->type
) {
3294 case PRINT_DYNAMIC_ARRAY
:
3295 offset
= pevent_read_number(pevent
,
3296 data
+ larg
->dynarray
.field
->offset
,
3297 larg
->dynarray
.field
->size
);
3298 if (larg
->dynarray
.field
->elementsize
)
3299 field_size
= larg
->dynarray
.field
->elementsize
;
3301 * The actual length of the dynamic array is stored
3302 * in the top half of the field, and the offset
3303 * is in the bottom half of the 32 bit field.
3309 if (!larg
->field
.field
) {
3311 pevent_find_any_field(event
, larg
->field
.name
);
3312 if (!larg
->field
.field
) {
3314 goto out_warning_field
;
3317 field_size
= larg
->field
.field
->elementsize
;
3318 offset
= larg
->field
.field
->offset
+
3319 right
* larg
->field
.field
->elementsize
;
3322 goto default_op
; /* oops, all bets off */
3324 val
= pevent_read_number(pevent
,
3325 data
+ offset
, field_size
);
3327 val
= eval_type(val
, typearg
, 1);
3329 } else if (strcmp(arg
->op
.op
, "?") == 0) {
3330 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3331 arg
= arg
->op
.right
;
3333 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3335 val
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3339 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3340 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3341 switch (arg
->op
.op
[0]) {
3343 switch (arg
->op
.op
[1]) {
3348 val
= left
!= right
;
3351 goto out_warning_op
;
3359 val
= left
|| right
;
3365 val
= left
&& right
;
3370 switch (arg
->op
.op
[1]) {
3375 val
= left
<< right
;
3378 val
= left
<= right
;
3381 goto out_warning_op
;
3385 switch (arg
->op
.op
[1]) {
3390 val
= left
>> right
;
3393 val
= left
>= right
;
3396 goto out_warning_op
;
3400 if (arg
->op
.op
[1] != '=')
3401 goto out_warning_op
;
3403 val
= left
== right
;
3418 goto out_warning_op
;
3421 default: /* not sure what to do there */
3427 do_warning("%s: unknown op '%s'", __func__
, arg
->op
.op
);
3431 do_warning("%s: field %s not found", __func__
, arg
->field
.name
);
3437 unsigned long long value
;
3440 static const struct flag flags
[] = {
3441 { "HI_SOFTIRQ", 0 },
3442 { "TIMER_SOFTIRQ", 1 },
3443 { "NET_TX_SOFTIRQ", 2 },
3444 { "NET_RX_SOFTIRQ", 3 },
3445 { "BLOCK_SOFTIRQ", 4 },
3446 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3447 { "TASKLET_SOFTIRQ", 6 },
3448 { "SCHED_SOFTIRQ", 7 },
3449 { "HRTIMER_SOFTIRQ", 8 },
3450 { "RCU_SOFTIRQ", 9 },
3452 { "HRTIMER_NORESTART", 0 },
3453 { "HRTIMER_RESTART", 1 },
3456 static unsigned long long eval_flag(const char *flag
)
3461 * Some flags in the format files do not get converted.
3462 * If the flag is not numeric, see if it is something that
3463 * we already know about.
3465 if (isdigit(flag
[0]))
3466 return strtoull(flag
, NULL
, 0);
3468 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
3469 if (strcmp(flags
[i
].name
, flag
) == 0)
3470 return flags
[i
].value
;
3475 static void print_str_to_seq(struct trace_seq
*s
, const char *format
,
3476 int len_arg
, const char *str
)
3479 trace_seq_printf(s
, format
, len_arg
, str
);
3481 trace_seq_printf(s
, format
, str
);
3484 static void print_str_arg(struct trace_seq
*s
, void *data
, int size
,
3485 struct event_format
*event
, const char *format
,
3486 int len_arg
, struct print_arg
*arg
)
3488 struct pevent
*pevent
= event
->pevent
;
3489 struct print_flag_sym
*flag
;
3490 struct format_field
*field
;
3491 unsigned long long val
, fval
;
3498 switch (arg
->type
) {
3503 print_str_to_seq(s
, format
, len_arg
, arg
->atom
.atom
);
3506 field
= arg
->field
.field
;
3508 field
= pevent_find_any_field(event
, arg
->field
.name
);
3510 str
= arg
->field
.name
;
3511 goto out_warning_field
;
3513 arg
->field
.field
= field
;
3515 /* Zero sized fields, mean the rest of the data */
3516 len
= field
->size
? : size
- field
->offset
;
3519 * Some events pass in pointers. If this is not an array
3520 * and the size is the same as long_size, assume that it
3523 if (!(field
->flags
& FIELD_IS_ARRAY
) &&
3524 field
->size
== pevent
->long_size
) {
3525 addr
= *(unsigned long *)(data
+ field
->offset
);
3526 trace_seq_printf(s
, "%lx", addr
);
3529 str
= malloc(len
+ 1);
3531 do_warning("%s: not enough memory!", __func__
);
3534 memcpy(str
, data
+ field
->offset
, len
);
3536 print_str_to_seq(s
, format
, len_arg
, str
);
3540 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
3542 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
3543 fval
= eval_flag(flag
->value
);
3544 if (!val
&& !fval
) {
3545 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3548 if (fval
&& (val
& fval
) == fval
) {
3549 if (print
&& arg
->flags
.delim
)
3550 trace_seq_puts(s
, arg
->flags
.delim
);
3551 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3558 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
3559 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
3560 fval
= eval_flag(flag
->value
);
3562 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3568 field
= arg
->hex
.field
->field
.field
;
3570 str
= arg
->hex
.field
->field
.name
;
3571 field
= pevent_find_any_field(event
, str
);
3573 goto out_warning_field
;
3574 arg
->hex
.field
->field
.field
= field
;
3576 hex
= data
+ field
->offset
;
3577 len
= eval_num_arg(data
, size
, event
, arg
->hex
.size
);
3578 for (i
= 0; i
< len
; i
++) {
3580 trace_seq_putc(s
, ' ');
3581 trace_seq_printf(s
, "%02x", hex
[i
]);
3587 case PRINT_STRING
: {
3590 if (arg
->string
.offset
== -1) {
3591 struct format_field
*f
;
3593 f
= pevent_find_any_field(event
, arg
->string
.string
);
3594 arg
->string
.offset
= f
->offset
;
3596 str_offset
= data2host4(pevent
, data
+ arg
->string
.offset
);
3597 str_offset
&= 0xffff;
3598 print_str_to_seq(s
, format
, len_arg
, ((char *)data
) + str_offset
);
3602 print_str_to_seq(s
, format
, len_arg
, arg
->string
.string
);
3606 * The only op for string should be ? :
3608 if (arg
->op
.op
[0] != '?')
3610 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3612 print_str_arg(s
, data
, size
, event
,
3613 format
, len_arg
, arg
->op
.right
->op
.left
);
3615 print_str_arg(s
, data
, size
, event
,
3616 format
, len_arg
, arg
->op
.right
->op
.right
);
3619 process_defined_func(s
, data
, size
, event
, arg
);
3629 do_warning("%s: field %s not found", __func__
, arg
->field
.name
);
3632 static unsigned long long
3633 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
3634 struct event_format
*event
, struct print_arg
*arg
)
3636 struct pevent_function_handler
*func_handle
= arg
->func
.func
;
3637 struct pevent_func_params
*param
;
3638 unsigned long long *args
;
3639 unsigned long long ret
;
3640 struct print_arg
*farg
;
3641 struct trace_seq str
;
3643 struct save_str
*next
;
3645 } *strings
= NULL
, *string
;
3648 if (!func_handle
->nr_args
) {
3649 ret
= (*func_handle
->func
)(s
, NULL
);
3653 farg
= arg
->func
.args
;
3654 param
= func_handle
->params
;
3657 args
= malloc(sizeof(*args
) * func_handle
->nr_args
);
3661 for (i
= 0; i
< func_handle
->nr_args
; i
++) {
3662 switch (param
->type
) {
3663 case PEVENT_FUNC_ARG_INT
:
3664 case PEVENT_FUNC_ARG_LONG
:
3665 case PEVENT_FUNC_ARG_PTR
:
3666 args
[i
] = eval_num_arg(data
, size
, event
, farg
);
3668 case PEVENT_FUNC_ARG_STRING
:
3669 trace_seq_init(&str
);
3670 print_str_arg(&str
, data
, size
, event
, "%s", -1, farg
);
3671 trace_seq_terminate(&str
);
3672 string
= malloc(sizeof(*string
));
3674 do_warning("%s(%d): malloc str", __func__
, __LINE__
);
3677 string
->next
= strings
;
3678 string
->str
= strdup(str
.buffer
);
3681 do_warning("%s(%d): malloc str", __func__
, __LINE__
);
3684 args
[i
] = (uintptr_t)string
->str
;
3686 trace_seq_destroy(&str
);
3690 * Something went totally wrong, this is not
3691 * an input error, something in this code broke.
3693 do_warning("Unexpected end of arguments\n");
3697 param
= param
->next
;
3700 ret
= (*func_handle
->func
)(s
, args
);
3705 strings
= string
->next
;
3711 /* TBD : handle return type here */
3715 static void free_args(struct print_arg
*args
)
3717 struct print_arg
*next
;
3727 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event_format
*event
)
3729 struct pevent
*pevent
= event
->pevent
;
3730 struct format_field
*field
, *ip_field
;
3731 struct print_arg
*args
, *arg
, **next
;
3732 unsigned long long ip
, val
;
3737 field
= pevent
->bprint_buf_field
;
3738 ip_field
= pevent
->bprint_ip_field
;
3741 field
= pevent_find_field(event
, "buf");
3743 do_warning("can't find buffer field for binary printk");
3746 ip_field
= pevent_find_field(event
, "ip");
3748 do_warning("can't find ip field for binary printk");
3751 pevent
->bprint_buf_field
= field
;
3752 pevent
->bprint_ip_field
= ip_field
;
3755 ip
= pevent_read_number(pevent
, data
+ ip_field
->offset
, ip_field
->size
);
3758 * The first arg is the IP pointer.
3762 do_warning("%s(%d): not enough memory!", __func__
, __LINE__
);
3769 arg
->type
= PRINT_ATOM
;
3771 if (asprintf(&arg
->atom
.atom
, "%lld", ip
) < 0)
3774 /* skip the first "%pf : " */
3775 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
3776 bptr
< data
+ size
&& *ptr
; ptr
++) {
3807 vsize
= pevent
->long_size
;
3821 /* the pointers are always 4 bytes aligned */
3822 bptr
= (void *)(((unsigned long)bptr
+ 3) &
3824 val
= pevent_read_number(pevent
, bptr
, vsize
);
3828 do_warning("%s(%d): not enough memory!",
3829 __func__
, __LINE__
);
3833 arg
->type
= PRINT_ATOM
;
3834 if (asprintf(&arg
->atom
.atom
, "%lld", val
) < 0) {
3841 * The '*' case means that an arg is used as the length.
3842 * We need to continue to figure out for what.
3851 do_warning("%s(%d): not enough memory!",
3852 __func__
, __LINE__
);
3856 arg
->type
= PRINT_BSTRING
;
3857 arg
->string
.string
= strdup(bptr
);
3858 if (!arg
->string
.string
)
3860 bptr
+= strlen(bptr
) + 1;
3877 get_bprint_format(void *data
, int size __maybe_unused
,
3878 struct event_format
*event
)
3880 struct pevent
*pevent
= event
->pevent
;
3881 unsigned long long addr
;
3882 struct format_field
*field
;
3883 struct printk_map
*printk
;
3887 field
= pevent
->bprint_fmt_field
;
3890 field
= pevent_find_field(event
, "fmt");
3892 do_warning("can't find format field for binary printk");
3895 pevent
->bprint_fmt_field
= field
;
3898 addr
= pevent_read_number(pevent
, data
+ field
->offset
, field
->size
);
3900 printk
= find_printk(pevent
, addr
);
3902 if (asprintf(&format
, "%%pf : (NO FORMAT FOUND at %llx)\n", addr
) < 0)
3908 /* Remove any quotes. */
3911 if (asprintf(&format
, "%s : %s", "%pf", p
) < 0)
3913 /* remove ending quotes and new line since we will add one too */
3914 p
= format
+ strlen(format
) - 1;
3919 if (strcmp(p
, "\\n") == 0)
3925 static void print_mac_arg(struct trace_seq
*s
, int mac
, void *data
, int size
,
3926 struct event_format
*event
, struct print_arg
*arg
)
3929 const char *fmt
= "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3931 if (arg
->type
== PRINT_FUNC
) {
3932 process_defined_func(s
, data
, size
, event
, arg
);
3936 if (arg
->type
!= PRINT_FIELD
) {
3937 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d",
3943 fmt
= "%.2x%.2x%.2x%.2x%.2x%.2x";
3944 if (!arg
->field
.field
) {
3946 pevent_find_any_field(event
, arg
->field
.name
);
3947 if (!arg
->field
.field
) {
3948 do_warning("%s: field %s not found",
3949 __func__
, arg
->field
.name
);
3953 if (arg
->field
.field
->size
!= 6) {
3954 trace_seq_printf(s
, "INVALIDMAC");
3957 buf
= data
+ arg
->field
.field
->offset
;
3958 trace_seq_printf(s
, fmt
, buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5]);
3961 static int is_printable_array(char *p
, unsigned int len
)
3965 for (i
= 0; i
< len
&& p
[i
]; i
++)
3971 static void print_event_fields(struct trace_seq
*s
, void *data
,
3972 int size __maybe_unused
,
3973 struct event_format
*event
)
3975 struct format_field
*field
;
3976 unsigned long long val
;
3977 unsigned int offset
, len
, i
;
3979 field
= event
->format
.fields
;
3981 trace_seq_printf(s
, " %s=", field
->name
);
3982 if (field
->flags
& FIELD_IS_ARRAY
) {
3983 offset
= field
->offset
;
3985 if (field
->flags
& FIELD_IS_DYNAMIC
) {
3986 val
= pevent_read_number(event
->pevent
, data
+ offset
, len
);
3991 if (field
->flags
& FIELD_IS_STRING
&&
3992 is_printable_array(data
+ offset
, len
)) {
3993 trace_seq_printf(s
, "%s", (char *)data
+ offset
);
3995 trace_seq_puts(s
, "ARRAY[");
3996 for (i
= 0; i
< len
; i
++) {
3998 trace_seq_puts(s
, ", ");
3999 trace_seq_printf(s
, "%02x",
4000 *((unsigned char *)data
+ offset
+ i
));
4002 trace_seq_putc(s
, ']');
4003 field
->flags
&= ~FIELD_IS_STRING
;
4006 val
= pevent_read_number(event
->pevent
, data
+ field
->offset
,
4008 if (field
->flags
& FIELD_IS_POINTER
) {
4009 trace_seq_printf(s
, "0x%llx", val
);
4010 } else if (field
->flags
& FIELD_IS_SIGNED
) {
4011 switch (field
->size
) {
4014 * If field is long then print it in hex.
4015 * A long usually stores pointers.
4017 if (field
->flags
& FIELD_IS_LONG
)
4018 trace_seq_printf(s
, "0x%x", (int)val
);
4020 trace_seq_printf(s
, "%d", (int)val
);
4023 trace_seq_printf(s
, "%2d", (short)val
);
4026 trace_seq_printf(s
, "%1d", (char)val
);
4029 trace_seq_printf(s
, "%lld", val
);
4032 if (field
->flags
& FIELD_IS_LONG
)
4033 trace_seq_printf(s
, "0x%llx", val
);
4035 trace_seq_printf(s
, "%llu", val
);
4038 field
= field
->next
;
4042 static void pretty_print(struct trace_seq
*s
, void *data
, int size
, struct event_format
*event
)
4044 struct pevent
*pevent
= event
->pevent
;
4045 struct print_fmt
*print_fmt
= &event
->print_fmt
;
4046 struct print_arg
*arg
= print_fmt
->args
;
4047 struct print_arg
*args
= NULL
;
4048 const char *ptr
= print_fmt
->format
;
4049 unsigned long long val
;
4050 struct func_map
*func
;
4051 const char *saveptr
;
4052 char *bprint_fmt
= NULL
;
4060 if (event
->flags
& EVENT_FL_FAILED
) {
4061 trace_seq_printf(s
, "[FAILED TO PARSE]");
4062 print_event_fields(s
, data
, size
, event
);
4066 if (event
->flags
& EVENT_FL_ISBPRINT
) {
4067 bprint_fmt
= get_bprint_format(data
, size
, event
);
4068 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
4073 for (; *ptr
; ptr
++) {
4079 trace_seq_putc(s
, '\n');
4082 trace_seq_putc(s
, '\t');
4085 trace_seq_putc(s
, '\r');
4088 trace_seq_putc(s
, '\\');
4091 trace_seq_putc(s
, *ptr
);
4095 } else if (*ptr
== '%') {
4103 trace_seq_putc(s
, '%');
4106 /* FIXME: need to handle properly */
4118 /* The argument is the length. */
4120 do_warning("no argument match");
4121 event
->flags
|= EVENT_FL_FAILED
;
4124 len_arg
= eval_num_arg(data
, size
, event
, arg
);
4134 if (pevent
->long_size
== 4)
4139 if (*(ptr
+1) == 'F' ||
4143 } else if (*(ptr
+1) == 'M' || *(ptr
+1) == 'm') {
4144 print_mac_arg(s
, *(ptr
+1), data
, size
, event
, arg
);
4157 do_warning("no argument match");
4158 event
->flags
|= EVENT_FL_FAILED
;
4162 len
= ((unsigned long)ptr
+ 1) -
4163 (unsigned long)saveptr
;
4165 /* should never happen */
4167 do_warning("bad format!");
4168 event
->flags
|= EVENT_FL_FAILED
;
4172 memcpy(format
, saveptr
, len
);
4175 val
= eval_num_arg(data
, size
, event
, arg
);
4179 func
= find_func(pevent
, val
);
4181 trace_seq_puts(s
, func
->func
);
4182 if (show_func
== 'F')
4189 if (pevent
->long_size
== 8 && ls
&&
4190 sizeof(long) != 8) {
4194 /* make %l into %ll */
4195 p
= strchr(format
, 'l');
4197 memmove(p
+1, p
, strlen(p
)+1);
4198 else if (strcmp(format
, "%p") == 0)
4199 strcpy(format
, "0x%llx");
4204 trace_seq_printf(s
, format
, len_arg
, (char)val
);
4206 trace_seq_printf(s
, format
, (char)val
);
4210 trace_seq_printf(s
, format
, len_arg
, (short)val
);
4212 trace_seq_printf(s
, format
, (short)val
);
4216 trace_seq_printf(s
, format
, len_arg
, (int)val
);
4218 trace_seq_printf(s
, format
, (int)val
);
4222 trace_seq_printf(s
, format
, len_arg
, (long)val
);
4224 trace_seq_printf(s
, format
, (long)val
);
4228 trace_seq_printf(s
, format
, len_arg
,
4231 trace_seq_printf(s
, format
, (long long)val
);
4234 do_warning("bad count (%d)", ls
);
4235 event
->flags
|= EVENT_FL_FAILED
;
4240 do_warning("no matching argument");
4241 event
->flags
|= EVENT_FL_FAILED
;
4245 len
= ((unsigned long)ptr
+ 1) -
4246 (unsigned long)saveptr
;
4248 /* should never happen */
4250 do_warning("bad format!");
4251 event
->flags
|= EVENT_FL_FAILED
;
4255 memcpy(format
, saveptr
, len
);
4259 print_str_arg(s
, data
, size
, event
,
4260 format
, len_arg
, arg
);
4264 trace_seq_printf(s
, ">%c<", *ptr
);
4268 trace_seq_putc(s
, *ptr
);
4271 if (event
->flags
& EVENT_FL_FAILED
) {
4273 trace_seq_printf(s
, "[FAILED TO PARSE]");
4283 * pevent_data_lat_fmt - parse the data for the latency format
4284 * @pevent: a handle to the pevent
4285 * @s: the trace_seq to write to
4286 * @record: the record to read from
4288 * This parses out the Latency format (interrupts disabled,
4289 * need rescheduling, in hard/soft interrupt, preempt count
4290 * and lock depth) and places it into the trace_seq.
4292 void pevent_data_lat_fmt(struct pevent
*pevent
,
4293 struct trace_seq
*s
, struct pevent_record
*record
)
4295 static int check_lock_depth
= 1;
4296 static int check_migrate_disable
= 1;
4297 static int lock_depth_exists
;
4298 static int migrate_disable_exists
;
4299 unsigned int lat_flags
;
4302 int migrate_disable
;
4305 void *data
= record
->data
;
4307 lat_flags
= parse_common_flags(pevent
, data
);
4308 pc
= parse_common_pc(pevent
, data
);
4309 /* lock_depth may not always exist */
4310 if (lock_depth_exists
)
4311 lock_depth
= parse_common_lock_depth(pevent
, data
);
4312 else if (check_lock_depth
) {
4313 lock_depth
= parse_common_lock_depth(pevent
, data
);
4315 check_lock_depth
= 0;
4317 lock_depth_exists
= 1;
4320 /* migrate_disable may not always exist */
4321 if (migrate_disable_exists
)
4322 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4323 else if (check_migrate_disable
) {
4324 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4325 if (migrate_disable
< 0)
4326 check_migrate_disable
= 0;
4328 migrate_disable_exists
= 1;
4331 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
4332 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
4334 trace_seq_printf(s
, "%c%c%c",
4335 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
4336 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
4338 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
4340 (hardirq
&& softirq
) ? 'H' :
4341 hardirq
? 'h' : softirq
? 's' : '.');
4344 trace_seq_printf(s
, "%x", pc
);
4346 trace_seq_putc(s
, '.');
4348 if (migrate_disable_exists
) {
4349 if (migrate_disable
< 0)
4350 trace_seq_putc(s
, '.');
4352 trace_seq_printf(s
, "%d", migrate_disable
);
4355 if (lock_depth_exists
) {
4357 trace_seq_putc(s
, '.');
4359 trace_seq_printf(s
, "%d", lock_depth
);
4362 trace_seq_terminate(s
);
4366 * pevent_data_type - parse out the given event type
4367 * @pevent: a handle to the pevent
4368 * @rec: the record to read from
4370 * This returns the event id from the @rec.
4372 int pevent_data_type(struct pevent
*pevent
, struct pevent_record
*rec
)
4374 return trace_parse_common_type(pevent
, rec
->data
);
4378 * pevent_data_event_from_type - find the event by a given type
4379 * @pevent: a handle to the pevent
4380 * @type: the type of the event.
4382 * This returns the event form a given @type;
4384 struct event_format
*pevent_data_event_from_type(struct pevent
*pevent
, int type
)
4386 return pevent_find_event(pevent
, type
);
4390 * pevent_data_pid - parse the PID from raw data
4391 * @pevent: a handle to the pevent
4392 * @rec: the record to parse
4394 * This returns the PID from a raw data.
4396 int pevent_data_pid(struct pevent
*pevent
, struct pevent_record
*rec
)
4398 return parse_common_pid(pevent
, rec
->data
);
4402 * pevent_data_comm_from_pid - return the command line from PID
4403 * @pevent: a handle to the pevent
4404 * @pid: the PID of the task to search for
4406 * This returns a pointer to the command line that has the given
4409 const char *pevent_data_comm_from_pid(struct pevent
*pevent
, int pid
)
4413 comm
= find_cmdline(pevent
, pid
);
4418 * pevent_data_comm_from_pid - parse the data into the print format
4419 * @s: the trace_seq to write to
4420 * @event: the handle to the event
4421 * @record: the record to read from
4423 * This parses the raw @data using the given @event information and
4424 * writes the print format into the trace_seq.
4426 void pevent_event_info(struct trace_seq
*s
, struct event_format
*event
,
4427 struct pevent_record
*record
)
4429 int print_pretty
= 1;
4431 if (event
->pevent
->print_raw
)
4432 print_event_fields(s
, record
->data
, record
->size
, event
);
4436 print_pretty
= event
->handler(s
, record
, event
,
4440 pretty_print(s
, record
->data
, record
->size
, event
);
4443 trace_seq_terminate(s
);
4446 void pevent_print_event(struct pevent
*pevent
, struct trace_seq
*s
,
4447 struct pevent_record
*record
)
4449 static const char *spaces
= " "; /* 20 spaces */
4450 struct event_format
*event
;
4452 unsigned long usecs
;
4453 unsigned long nsecs
;
4455 void *data
= record
->data
;
4461 secs
= record
->ts
/ NSECS_PER_SEC
;
4462 nsecs
= record
->ts
- secs
* NSECS_PER_SEC
;
4464 if (record
->size
< 0) {
4465 do_warning("ug! negative record size %d", record
->size
);
4469 type
= trace_parse_common_type(pevent
, data
);
4471 event
= pevent_find_event(pevent
, type
);
4473 do_warning("ug! no event found for type %d", type
);
4477 pid
= parse_common_pid(pevent
, data
);
4478 comm
= find_cmdline(pevent
, pid
);
4480 if (pevent
->latency_format
) {
4481 trace_seq_printf(s
, "%8.8s-%-5d %3d",
4482 comm
, pid
, record
->cpu
);
4483 pevent_data_lat_fmt(pevent
, s
, record
);
4485 trace_seq_printf(s
, "%16s-%-5d [%03d]", comm
, pid
, record
->cpu
);
4487 if (pevent
->flags
& PEVENT_NSEC_OUTPUT
) {
4491 usecs
= (nsecs
+ 500) / NSECS_PER_USEC
;
4495 trace_seq_printf(s
, " %5lu.%0*lu: %s: ", secs
, p
, usecs
, event
->name
);
4497 /* Space out the event names evenly. */
4498 len
= strlen(event
->name
);
4500 trace_seq_printf(s
, "%.*s", 20 - len
, spaces
);
4502 pevent_event_info(s
, event
, record
);
4505 static int events_id_cmp(const void *a
, const void *b
)
4507 struct event_format
* const * ea
= a
;
4508 struct event_format
* const * eb
= b
;
4510 if ((*ea
)->id
< (*eb
)->id
)
4513 if ((*ea
)->id
> (*eb
)->id
)
4519 static int events_name_cmp(const void *a
, const void *b
)
4521 struct event_format
* const * ea
= a
;
4522 struct event_format
* const * eb
= b
;
4525 res
= strcmp((*ea
)->name
, (*eb
)->name
);
4529 res
= strcmp((*ea
)->system
, (*eb
)->system
);
4533 return events_id_cmp(a
, b
);
4536 static int events_system_cmp(const void *a
, const void *b
)
4538 struct event_format
* const * ea
= a
;
4539 struct event_format
* const * eb
= b
;
4542 res
= strcmp((*ea
)->system
, (*eb
)->system
);
4546 res
= strcmp((*ea
)->name
, (*eb
)->name
);
4550 return events_id_cmp(a
, b
);
4553 struct event_format
**pevent_list_events(struct pevent
*pevent
, enum event_sort_type sort_type
)
4555 struct event_format
**events
;
4556 int (*sort
)(const void *a
, const void *b
);
4558 events
= pevent
->sort_events
;
4560 if (events
&& pevent
->last_type
== sort_type
)
4564 events
= malloc(sizeof(*events
) * (pevent
->nr_events
+ 1));
4568 memcpy(events
, pevent
->events
, sizeof(*events
) * pevent
->nr_events
);
4569 events
[pevent
->nr_events
] = NULL
;
4571 pevent
->sort_events
= events
;
4573 /* the internal events are sorted by id */
4574 if (sort_type
== EVENT_SORT_ID
) {
4575 pevent
->last_type
= sort_type
;
4580 switch (sort_type
) {
4582 sort
= events_id_cmp
;
4584 case EVENT_SORT_NAME
:
4585 sort
= events_name_cmp
;
4587 case EVENT_SORT_SYSTEM
:
4588 sort
= events_system_cmp
;
4594 qsort(events
, pevent
->nr_events
, sizeof(*events
), sort
);
4595 pevent
->last_type
= sort_type
;
4600 static struct format_field
**
4601 get_event_fields(const char *type
, const char *name
,
4602 int count
, struct format_field
*list
)
4604 struct format_field
**fields
;
4605 struct format_field
*field
;
4608 fields
= malloc(sizeof(*fields
) * (count
+ 1));
4612 for (field
= list
; field
; field
= field
->next
) {
4613 fields
[i
++] = field
;
4614 if (i
== count
+ 1) {
4615 do_warning("event %s has more %s fields than specified",
4623 do_warning("event %s has less %s fields than specified",
4632 * pevent_event_common_fields - return a list of common fields for an event
4633 * @event: the event to return the common fields of.
4635 * Returns an allocated array of fields. The last item in the array is NULL.
4636 * The array must be freed with free().
4638 struct format_field
**pevent_event_common_fields(struct event_format
*event
)
4640 return get_event_fields("common", event
->name
,
4641 event
->format
.nr_common
,
4642 event
->format
.common_fields
);
4646 * pevent_event_fields - return a list of event specific fields for an event
4647 * @event: the event to return the fields of.
4649 * Returns an allocated array of fields. The last item in the array is NULL.
4650 * The array must be freed with free().
4652 struct format_field
**pevent_event_fields(struct event_format
*event
)
4654 return get_event_fields("event", event
->name
,
4655 event
->format
.nr_fields
,
4656 event
->format
.fields
);
4659 static void print_fields(struct trace_seq
*s
, struct print_flag_sym
*field
)
4661 trace_seq_printf(s
, "{ %s, %s }", field
->value
, field
->str
);
4663 trace_seq_puts(s
, ", ");
4664 print_fields(s
, field
->next
);
4669 static void print_args(struct print_arg
*args
)
4671 int print_paren
= 1;
4674 switch (args
->type
) {
4679 printf("%s", args
->atom
.atom
);
4682 printf("REC->%s", args
->field
.name
);
4685 printf("__print_flags(");
4686 print_args(args
->flags
.field
);
4687 printf(", %s, ", args
->flags
.delim
);
4689 print_fields(&s
, args
->flags
.flags
);
4690 trace_seq_do_printf(&s
);
4691 trace_seq_destroy(&s
);
4695 printf("__print_symbolic(");
4696 print_args(args
->symbol
.field
);
4699 print_fields(&s
, args
->symbol
.symbols
);
4700 trace_seq_do_printf(&s
);
4701 trace_seq_destroy(&s
);
4705 printf("__print_hex(");
4706 print_args(args
->hex
.field
);
4708 print_args(args
->hex
.size
);
4713 printf("__get_str(%s)", args
->string
.string
);
4716 printf("(%s)", args
->typecast
.type
);
4717 print_args(args
->typecast
.item
);
4720 if (strcmp(args
->op
.op
, ":") == 0)
4724 print_args(args
->op
.left
);
4725 printf(" %s ", args
->op
.op
);
4726 print_args(args
->op
.right
);
4731 /* we should warn... */
4736 print_args(args
->next
);
4740 static void parse_header_field(const char *field
,
4741 int *offset
, int *size
, int mandatory
)
4743 unsigned long long save_input_buf_ptr
;
4744 unsigned long long save_input_buf_siz
;
4748 save_input_buf_ptr
= input_buf_ptr
;
4749 save_input_buf_siz
= input_buf_siz
;
4751 if (read_expected(EVENT_ITEM
, "field") < 0)
4753 if (read_expected(EVENT_OP
, ":") < 0)
4757 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4762 * If this is not a mandatory field, then test it first.
4765 if (read_expected(EVENT_ITEM
, field
) < 0)
4768 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4770 if (strcmp(token
, field
) != 0)
4775 if (read_expected(EVENT_OP
, ";") < 0)
4777 if (read_expected(EVENT_ITEM
, "offset") < 0)
4779 if (read_expected(EVENT_OP
, ":") < 0)
4781 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4783 *offset
= atoi(token
);
4785 if (read_expected(EVENT_OP
, ";") < 0)
4787 if (read_expected(EVENT_ITEM
, "size") < 0)
4789 if (read_expected(EVENT_OP
, ":") < 0)
4791 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
4793 *size
= atoi(token
);
4795 if (read_expected(EVENT_OP
, ";") < 0)
4797 type
= read_token(&token
);
4798 if (type
!= EVENT_NEWLINE
) {
4799 /* newer versions of the kernel have a "signed" type */
4800 if (type
!= EVENT_ITEM
)
4803 if (strcmp(token
, "signed") != 0)
4808 if (read_expected(EVENT_OP
, ":") < 0)
4811 if (read_expect_type(EVENT_ITEM
, &token
))
4815 if (read_expected(EVENT_OP
, ";") < 0)
4818 if (read_expect_type(EVENT_NEWLINE
, &token
))
4826 input_buf_ptr
= save_input_buf_ptr
;
4827 input_buf_siz
= save_input_buf_siz
;
4834 * pevent_parse_header_page - parse the data stored in the header page
4835 * @pevent: the handle to the pevent
4836 * @buf: the buffer storing the header page format string
4837 * @size: the size of @buf
4838 * @long_size: the long size to use if there is no header
4840 * This parses the header page format for information on the
4841 * ring buffer used. The @buf should be copied from
4843 * /sys/kernel/debug/tracing/events/header_page
4845 int pevent_parse_header_page(struct pevent
*pevent
, char *buf
, unsigned long size
,
4852 * Old kernels did not have header page info.
4853 * Sorry but we just use what we find here in user space.
4855 pevent
->header_page_ts_size
= sizeof(long long);
4856 pevent
->header_page_size_size
= long_size
;
4857 pevent
->header_page_data_offset
= sizeof(long long) + long_size
;
4858 pevent
->old_format
= 1;
4861 init_input_buf(buf
, size
);
4863 parse_header_field("timestamp", &pevent
->header_page_ts_offset
,
4864 &pevent
->header_page_ts_size
, 1);
4865 parse_header_field("commit", &pevent
->header_page_size_offset
,
4866 &pevent
->header_page_size_size
, 1);
4867 parse_header_field("overwrite", &pevent
->header_page_overwrite
,
4869 parse_header_field("data", &pevent
->header_page_data_offset
,
4870 &pevent
->header_page_data_size
, 1);
4875 static int event_matches(struct event_format
*event
,
4876 int id
, const char *sys_name
,
4877 const char *event_name
)
4879 if (id
>= 0 && id
!= event
->id
)
4882 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
4885 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
4891 static void free_handler(struct event_handler
*handle
)
4893 free((void *)handle
->sys_name
);
4894 free((void *)handle
->event_name
);
4898 static int find_event_handle(struct pevent
*pevent
, struct event_format
*event
)
4900 struct event_handler
*handle
, **next
;
4902 for (next
= &pevent
->handlers
; *next
;
4903 next
= &(*next
)->next
) {
4905 if (event_matches(event
, handle
->id
,
4907 handle
->event_name
))
4914 pr_stat("overriding event (%d) %s:%s with new print handler",
4915 event
->id
, event
->system
, event
->name
);
4917 event
->handler
= handle
->func
;
4918 event
->context
= handle
->context
;
4920 *next
= handle
->next
;
4921 free_handler(handle
);
4927 * __pevent_parse_format - parse the event format
4928 * @buf: the buffer storing the event format string
4929 * @size: the size of @buf
4930 * @sys: the system the event belongs to
4932 * This parses the event format and creates an event structure
4933 * to quickly parse raw data for a given event.
4935 * These files currently come from:
4937 * /sys/kernel/debug/tracing/events/.../.../format
4939 enum pevent_errno
__pevent_parse_format(struct event_format
**eventp
,
4940 struct pevent
*pevent
, const char *buf
,
4941 unsigned long size
, const char *sys
)
4943 struct event_format
*event
;
4946 init_input_buf(buf
, size
);
4948 *eventp
= event
= alloc_event();
4950 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4952 event
->name
= event_read_name();
4955 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4956 goto event_alloc_failed
;
4959 if (strcmp(sys
, "ftrace") == 0) {
4960 event
->flags
|= EVENT_FL_ISFTRACE
;
4962 if (strcmp(event
->name
, "bprint") == 0)
4963 event
->flags
|= EVENT_FL_ISBPRINT
;
4966 event
->id
= event_read_id();
4967 if (event
->id
< 0) {
4968 ret
= PEVENT_ERRNO__READ_ID_FAILED
;
4970 * This isn't an allocation error actually.
4971 * But as the ID is critical, just bail out.
4973 goto event_alloc_failed
;
4976 event
->system
= strdup(sys
);
4977 if (!event
->system
) {
4978 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
4979 goto event_alloc_failed
;
4982 /* Add pevent to event so that it can be referenced */
4983 event
->pevent
= pevent
;
4985 ret
= event_read_format(event
);
4987 ret
= PEVENT_ERRNO__READ_FORMAT_FAILED
;
4988 goto event_parse_failed
;
4992 * If the event has an override, don't print warnings if the event
4993 * print format fails to parse.
4995 if (pevent
&& find_event_handle(pevent
, event
))
4998 ret
= event_read_print(event
);
5002 ret
= PEVENT_ERRNO__READ_PRINT_FAILED
;
5003 goto event_parse_failed
;
5006 if (!ret
&& (event
->flags
& EVENT_FL_ISFTRACE
)) {
5007 struct format_field
*field
;
5008 struct print_arg
*arg
, **list
;
5010 /* old ftrace had no args */
5011 list
= &event
->print_fmt
.args
;
5012 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
5015 event
->flags
|= EVENT_FL_FAILED
;
5016 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
;
5018 arg
->type
= PRINT_FIELD
;
5019 arg
->field
.name
= strdup(field
->name
);
5020 if (!arg
->field
.name
) {
5021 event
->flags
|= EVENT_FL_FAILED
;
5023 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
;
5025 arg
->field
.field
= field
;
5035 event
->flags
|= EVENT_FL_FAILED
;
5039 free(event
->system
);
5047 * pevent_parse_format - parse the event format
5048 * @buf: the buffer storing the event format string
5049 * @size: the size of @buf
5050 * @sys: the system the event belongs to
5052 * This parses the event format and creates an event structure
5053 * to quickly parse raw data for a given event.
5055 * These files currently come from:
5057 * /sys/kernel/debug/tracing/events/.../.../format
5059 enum pevent_errno
pevent_parse_format(struct event_format
**eventp
, const char *buf
,
5060 unsigned long size
, const char *sys
)
5062 return __pevent_parse_format(eventp
, NULL
, buf
, size
, sys
);
5066 * pevent_parse_event - parse the event format
5067 * @pevent: the handle to the pevent
5068 * @buf: the buffer storing the event format string
5069 * @size: the size of @buf
5070 * @sys: the system the event belongs to
5072 * This parses the event format and creates an event structure
5073 * to quickly parse raw data for a given event.
5075 * These files currently come from:
5077 * /sys/kernel/debug/tracing/events/.../.../format
5079 enum pevent_errno
pevent_parse_event(struct pevent
*pevent
, const char *buf
,
5080 unsigned long size
, const char *sys
)
5082 struct event_format
*event
= NULL
;
5083 int ret
= __pevent_parse_format(&event
, pevent
, buf
, size
, sys
);
5088 if (add_event(pevent
, event
)) {
5089 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5090 goto event_add_failed
;
5093 #define PRINT_ARGS 0
5094 if (PRINT_ARGS
&& event
->print_fmt
.args
)
5095 print_args(event
->print_fmt
.args
);
5100 pevent_free_format(event
);
5105 #define _PE(code, str) str
5106 static const char * const pevent_error_str
[] = {
5111 int pevent_strerror(struct pevent
*pevent __maybe_unused
,
5112 enum pevent_errno errnum
, char *buf
, size_t buflen
)
5118 msg
= strerror_r(errnum
, buf
, buflen
);
5120 size_t len
= strlen(msg
);
5121 memcpy(buf
, msg
, min(buflen
- 1, len
));
5122 *(buf
+ min(buflen
- 1, len
)) = '\0';
5127 if (errnum
<= __PEVENT_ERRNO__START
||
5128 errnum
>= __PEVENT_ERRNO__END
)
5131 idx
= errnum
- __PEVENT_ERRNO__START
- 1;
5132 msg
= pevent_error_str
[idx
];
5135 case PEVENT_ERRNO__MEM_ALLOC_FAILED
:
5136 case PEVENT_ERRNO__PARSE_EVENT_FAILED
:
5137 case PEVENT_ERRNO__READ_ID_FAILED
:
5138 case PEVENT_ERRNO__READ_FORMAT_FAILED
:
5139 case PEVENT_ERRNO__READ_PRINT_FAILED
:
5140 case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
:
5141 case PEVENT_ERRNO__INVALID_ARG_TYPE
:
5142 snprintf(buf
, buflen
, "%s", msg
);
5146 /* cannot reach here */
5153 int get_field_val(struct trace_seq
*s
, struct format_field
*field
,
5154 const char *name
, struct pevent_record
*record
,
5155 unsigned long long *val
, int err
)
5159 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
5163 if (pevent_read_number_field(field
, record
->data
, val
)) {
5165 trace_seq_printf(s
, " %s=INVALID", name
);
5173 * pevent_get_field_raw - return the raw pointer into the data field
5174 * @s: The seq to print to on error
5175 * @event: the event that the field is for
5176 * @name: The name of the field
5177 * @record: The record with the field name.
5178 * @len: place to store the field length.
5179 * @err: print default error if failed.
5181 * Returns a pointer into record->data of the field and places
5182 * the length of the field in @len.
5184 * On failure, it returns NULL.
5186 void *pevent_get_field_raw(struct trace_seq
*s
, struct event_format
*event
,
5187 const char *name
, struct pevent_record
*record
,
5190 struct format_field
*field
;
5191 void *data
= record
->data
;
5198 field
= pevent_find_field(event
, name
);
5202 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
5206 /* Allow @len to be NULL */
5210 offset
= field
->offset
;
5211 if (field
->flags
& FIELD_IS_DYNAMIC
) {
5212 offset
= pevent_read_number(event
->pevent
,
5213 data
+ offset
, field
->size
);
5214 *len
= offset
>> 16;
5219 return data
+ offset
;
5223 * pevent_get_field_val - find a field and return its value
5224 * @s: The seq to print to on error
5225 * @event: the event that the field is for
5226 * @name: The name of the field
5227 * @record: The record with the field name.
5228 * @val: place to store the value of the field.
5229 * @err: print default error if failed.
5231 * Returns 0 on success -1 on field not found.
5233 int pevent_get_field_val(struct trace_seq
*s
, struct event_format
*event
,
5234 const char *name
, struct pevent_record
*record
,
5235 unsigned long long *val
, int err
)
5237 struct format_field
*field
;
5242 field
= pevent_find_field(event
, name
);
5244 return get_field_val(s
, field
, name
, record
, val
, err
);
5248 * pevent_get_common_field_val - find a common field and return its value
5249 * @s: The seq to print to on error
5250 * @event: the event that the field is for
5251 * @name: The name of the field
5252 * @record: The record with the field name.
5253 * @val: place to store the value of the field.
5254 * @err: print default error if failed.
5256 * Returns 0 on success -1 on field not found.
5258 int pevent_get_common_field_val(struct trace_seq
*s
, struct event_format
*event
,
5259 const char *name
, struct pevent_record
*record
,
5260 unsigned long long *val
, int err
)
5262 struct format_field
*field
;
5267 field
= pevent_find_common_field(event
, name
);
5269 return get_field_val(s
, field
, name
, record
, val
, err
);
5273 * pevent_get_any_field_val - find a any field and return its value
5274 * @s: The seq to print to on error
5275 * @event: the event that the field is for
5276 * @name: The name of the field
5277 * @record: The record with the field name.
5278 * @val: place to store the value of the field.
5279 * @err: print default error if failed.
5281 * Returns 0 on success -1 on field not found.
5283 int pevent_get_any_field_val(struct trace_seq
*s
, struct event_format
*event
,
5284 const char *name
, struct pevent_record
*record
,
5285 unsigned long long *val
, int err
)
5287 struct format_field
*field
;
5292 field
= pevent_find_any_field(event
, name
);
5294 return get_field_val(s
, field
, name
, record
, val
, err
);
5298 * pevent_print_num_field - print a field and a format
5299 * @s: The seq to print to
5300 * @fmt: The printf format to print the field with.
5301 * @event: the event that the field is for
5302 * @name: The name of the field
5303 * @record: The record with the field name.
5304 * @err: print default error if failed.
5306 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
5308 int pevent_print_num_field(struct trace_seq
*s
, const char *fmt
,
5309 struct event_format
*event
, const char *name
,
5310 struct pevent_record
*record
, int err
)
5312 struct format_field
*field
= pevent_find_field(event
, name
);
5313 unsigned long long val
;
5318 if (pevent_read_number_field(field
, record
->data
, &val
))
5321 return trace_seq_printf(s
, fmt
, val
);
5325 trace_seq_printf(s
, "CAN'T FIND FIELD \"%s\"", name
);
5329 static void free_func_handle(struct pevent_function_handler
*func
)
5331 struct pevent_func_params
*params
;
5335 while (func
->params
) {
5336 params
= func
->params
;
5337 func
->params
= params
->next
;
5345 * pevent_register_print_function - register a helper function
5346 * @pevent: the handle to the pevent
5347 * @func: the function to process the helper function
5348 * @ret_type: the return type of the helper function
5349 * @name: the name of the helper function
5350 * @parameters: A list of enum pevent_func_arg_type
5352 * Some events may have helper functions in the print format arguments.
5353 * This allows a plugin to dynamically create a way to process one
5354 * of these functions.
5356 * The @parameters is a variable list of pevent_func_arg_type enums that
5357 * must end with PEVENT_FUNC_ARG_VOID.
5359 int pevent_register_print_function(struct pevent
*pevent
,
5360 pevent_func_handler func
,
5361 enum pevent_func_arg_type ret_type
,
5364 struct pevent_function_handler
*func_handle
;
5365 struct pevent_func_params
**next_param
;
5366 struct pevent_func_params
*param
;
5367 enum pevent_func_arg_type type
;
5371 func_handle
= find_func_handler(pevent
, name
);
5374 * This is most like caused by the users own
5375 * plugins updating the function. This overrides the
5378 pr_stat("override of function helper '%s'", name
);
5379 remove_func_handler(pevent
, name
);
5382 func_handle
= calloc(1, sizeof(*func_handle
));
5384 do_warning("Failed to allocate function handler");
5385 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5388 func_handle
->ret_type
= ret_type
;
5389 func_handle
->name
= strdup(name
);
5390 func_handle
->func
= func
;
5391 if (!func_handle
->name
) {
5392 do_warning("Failed to allocate function name");
5394 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5397 next_param
= &(func_handle
->params
);
5400 type
= va_arg(ap
, enum pevent_func_arg_type
);
5401 if (type
== PEVENT_FUNC_ARG_VOID
)
5404 if (type
>= PEVENT_FUNC_ARG_MAX_TYPES
) {
5405 do_warning("Invalid argument type %d", type
);
5406 ret
= PEVENT_ERRNO__INVALID_ARG_TYPE
;
5410 param
= malloc(sizeof(*param
));
5412 do_warning("Failed to allocate function param");
5413 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5419 *next_param
= param
;
5420 next_param
= &(param
->next
);
5422 func_handle
->nr_args
++;
5426 func_handle
->next
= pevent
->func_handlers
;
5427 pevent
->func_handlers
= func_handle
;
5432 free_func_handle(func_handle
);
5437 * pevent_register_event_handler - register a way to parse an event
5438 * @pevent: the handle to the pevent
5439 * @id: the id of the event to register
5440 * @sys_name: the system name the event belongs to
5441 * @event_name: the name of the event
5442 * @func: the function to call to parse the event information
5443 * @context: the data to be passed to @func
5445 * This function allows a developer to override the parsing of
5446 * a given event. If for some reason the default print format
5447 * is not sufficient, this function will register a function
5448 * for an event to be used to parse the data instead.
5450 * If @id is >= 0, then it is used to find the event.
5451 * else @sys_name and @event_name are used.
5453 int pevent_register_event_handler(struct pevent
*pevent
,
5454 int id
, char *sys_name
, char *event_name
,
5455 pevent_event_handler_func func
,
5458 struct event_format
*event
;
5459 struct event_handler
*handle
;
5463 event
= pevent_find_event(pevent
, id
);
5466 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
5468 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
5471 event
= pevent_find_event_by_name(pevent
, sys_name
, event_name
);
5476 pr_stat("overriding event (%d) %s:%s with new print handler",
5477 event
->id
, event
->system
, event
->name
);
5479 event
->handler
= func
;
5480 event
->context
= context
;
5484 /* Save for later use. */
5485 handle
= calloc(1, sizeof(*handle
));
5487 do_warning("Failed to allocate event handler");
5488 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5493 handle
->event_name
= strdup(event_name
);
5495 handle
->sys_name
= strdup(sys_name
);
5497 if ((event_name
&& !handle
->event_name
) ||
5498 (sys_name
&& !handle
->sys_name
)) {
5499 do_warning("Failed to allocate event/sys name");
5500 free((void *)handle
->event_name
);
5501 free((void *)handle
->sys_name
);
5503 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5506 handle
->func
= func
;
5507 handle
->next
= pevent
->handlers
;
5508 pevent
->handlers
= handle
;
5509 handle
->context
= context
;
5515 * pevent_alloc - create a pevent handle
5517 struct pevent
*pevent_alloc(void)
5519 struct pevent
*pevent
= calloc(1, sizeof(*pevent
));
5522 pevent
->ref_count
= 1;
5527 void pevent_ref(struct pevent
*pevent
)
5529 pevent
->ref_count
++;
5532 static void free_format_fields(struct format_field
*field
)
5534 struct format_field
*next
;
5545 static void free_formats(struct format
*format
)
5547 free_format_fields(format
->common_fields
);
5548 free_format_fields(format
->fields
);
5551 void pevent_free_format(struct event_format
*event
)
5554 free(event
->system
);
5556 free_formats(&event
->format
);
5558 free(event
->print_fmt
.format
);
5559 free_args(event
->print_fmt
.args
);
5565 * pevent_free - free a pevent handle
5566 * @pevent: the pevent handle to free
5568 void pevent_free(struct pevent
*pevent
)
5570 struct cmdline_list
*cmdlist
, *cmdnext
;
5571 struct func_list
*funclist
, *funcnext
;
5572 struct printk_list
*printklist
, *printknext
;
5573 struct pevent_function_handler
*func_handler
;
5574 struct event_handler
*handle
;
5580 cmdlist
= pevent
->cmdlist
;
5581 funclist
= pevent
->funclist
;
5582 printklist
= pevent
->printklist
;
5584 pevent
->ref_count
--;
5585 if (pevent
->ref_count
)
5588 if (pevent
->cmdlines
) {
5589 for (i
= 0; i
< pevent
->cmdline_count
; i
++)
5590 free(pevent
->cmdlines
[i
].comm
);
5591 free(pevent
->cmdlines
);
5595 cmdnext
= cmdlist
->next
;
5596 free(cmdlist
->comm
);
5601 if (pevent
->func_map
) {
5602 for (i
= 0; i
< (int)pevent
->func_count
; i
++) {
5603 free(pevent
->func_map
[i
].func
);
5604 free(pevent
->func_map
[i
].mod
);
5606 free(pevent
->func_map
);
5610 funcnext
= funclist
->next
;
5611 free(funclist
->func
);
5612 free(funclist
->mod
);
5614 funclist
= funcnext
;
5617 while (pevent
->func_handlers
) {
5618 func_handler
= pevent
->func_handlers
;
5619 pevent
->func_handlers
= func_handler
->next
;
5620 free_func_handle(func_handler
);
5623 if (pevent
->printk_map
) {
5624 for (i
= 0; i
< (int)pevent
->printk_count
; i
++)
5625 free(pevent
->printk_map
[i
].printk
);
5626 free(pevent
->printk_map
);
5629 while (printklist
) {
5630 printknext
= printklist
->next
;
5631 free(printklist
->printk
);
5633 printklist
= printknext
;
5636 for (i
= 0; i
< pevent
->nr_events
; i
++)
5637 pevent_free_format(pevent
->events
[i
]);
5639 while (pevent
->handlers
) {
5640 handle
= pevent
->handlers
;
5641 pevent
->handlers
= handle
->next
;
5642 free_handler(handle
);
5645 free(pevent
->events
);
5646 free(pevent
->sort_events
);
5651 void pevent_unref(struct pevent
*pevent
)
5653 pevent_free(pevent
);