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 <netinet/ip6.h>
36 #include "event-parse.h"
37 #include "event-utils.h"
39 static const char *input_buf
;
40 static unsigned long long input_buf_ptr
;
41 static unsigned long long input_buf_siz
;
43 static int is_flag_field
;
44 static int is_symbolic_field
;
46 static int show_warning
= 1;
48 #define do_warning(fmt, ...) \
51 warning(fmt, ##__VA_ARGS__); \
54 #define do_warning_event(event, fmt, ...) \
60 warning("[%s:%s] " fmt, event->system, \
61 event->name, ##__VA_ARGS__); \
63 warning(fmt, ##__VA_ARGS__); \
66 static void init_input_buf(const char *buf
, unsigned long long size
)
73 const char *pevent_get_input_buf(void)
78 unsigned long long pevent_get_input_buf_ptr(void)
83 struct event_handler
{
84 struct event_handler
*next
;
87 const char *event_name
;
88 pevent_event_handler_func func
;
92 struct pevent_func_params
{
93 struct pevent_func_params
*next
;
94 enum pevent_func_arg_type type
;
97 struct pevent_function_handler
{
98 struct pevent_function_handler
*next
;
99 enum pevent_func_arg_type ret_type
;
101 pevent_func_handler func
;
102 struct pevent_func_params
*params
;
106 static unsigned long long
107 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
108 struct event_format
*event
, struct print_arg
*arg
);
110 static void free_func_handle(struct pevent_function_handler
*func
);
113 * pevent_buffer_init - init buffer for parsing
114 * @buf: buffer to parse
115 * @size: the size of the buffer
117 * For use with pevent_read_token(), this initializes the internal
118 * buffer that pevent_read_token() will parse.
120 void pevent_buffer_init(const char *buf
, unsigned long long size
)
122 init_input_buf(buf
, size
);
125 void breakpoint(void)
131 struct print_arg
*alloc_arg(void)
133 return calloc(1, sizeof(struct print_arg
));
141 static int cmdline_cmp(const void *a
, const void *b
)
143 const struct cmdline
*ca
= a
;
144 const struct cmdline
*cb
= b
;
146 if (ca
->pid
< cb
->pid
)
148 if (ca
->pid
> cb
->pid
)
154 struct cmdline_list
{
155 struct cmdline_list
*next
;
160 static int cmdline_init(struct pevent
*pevent
)
162 struct cmdline_list
*cmdlist
= pevent
->cmdlist
;
163 struct cmdline_list
*item
;
164 struct cmdline
*cmdlines
;
167 cmdlines
= malloc(sizeof(*cmdlines
) * pevent
->cmdline_count
);
173 cmdlines
[i
].pid
= cmdlist
->pid
;
174 cmdlines
[i
].comm
= cmdlist
->comm
;
177 cmdlist
= cmdlist
->next
;
181 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
183 pevent
->cmdlines
= cmdlines
;
184 pevent
->cmdlist
= NULL
;
189 static const char *find_cmdline(struct pevent
*pevent
, int pid
)
191 const struct cmdline
*comm
;
197 if (!pevent
->cmdlines
&& cmdline_init(pevent
))
198 return "<not enough memory for cmdlines!>";
202 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
203 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
211 * pevent_pid_is_registered - return if a pid has a cmdline registered
212 * @pevent: handle for the pevent
213 * @pid: The pid to check if it has a cmdline registered with.
215 * Returns 1 if the pid has a cmdline mapped to it
218 int pevent_pid_is_registered(struct pevent
*pevent
, int pid
)
220 const struct cmdline
*comm
;
226 if (!pevent
->cmdlines
&& cmdline_init(pevent
))
231 comm
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
232 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
240 * If the command lines have been converted to an array, then
241 * we must add this pid. This is much slower than when cmdlines
242 * are added before the array is initialized.
244 static int add_new_comm(struct pevent
*pevent
, const char *comm
, int pid
)
246 struct cmdline
*cmdlines
= pevent
->cmdlines
;
247 const struct cmdline
*cmdline
;
253 /* avoid duplicates */
256 cmdline
= bsearch(&key
, pevent
->cmdlines
, pevent
->cmdline_count
,
257 sizeof(*pevent
->cmdlines
), cmdline_cmp
);
263 cmdlines
= realloc(cmdlines
, sizeof(*cmdlines
) * (pevent
->cmdline_count
+ 1));
269 cmdlines
[pevent
->cmdline_count
].comm
= strdup(comm
);
270 if (!cmdlines
[pevent
->cmdline_count
].comm
) {
276 cmdlines
[pevent
->cmdline_count
].pid
= pid
;
278 if (cmdlines
[pevent
->cmdline_count
].comm
)
279 pevent
->cmdline_count
++;
281 qsort(cmdlines
, pevent
->cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
282 pevent
->cmdlines
= cmdlines
;
288 * pevent_register_comm - register a pid / comm mapping
289 * @pevent: handle for the pevent
290 * @comm: the command line to register
291 * @pid: the pid to map the command line to
293 * This adds a mapping to search for command line names with
294 * a given pid. The comm is duplicated.
296 int pevent_register_comm(struct pevent
*pevent
, const char *comm
, int pid
)
298 struct cmdline_list
*item
;
300 if (pevent
->cmdlines
)
301 return add_new_comm(pevent
, comm
, pid
);
303 item
= malloc(sizeof(*item
));
308 item
->comm
= strdup(comm
);
310 item
->comm
= strdup("<...>");
316 item
->next
= pevent
->cmdlist
;
318 pevent
->cmdlist
= item
;
319 pevent
->cmdline_count
++;
324 int pevent_register_trace_clock(struct pevent
*pevent
, const char *trace_clock
)
326 pevent
->trace_clock
= strdup(trace_clock
);
327 if (!pevent
->trace_clock
) {
335 unsigned long long addr
;
341 struct func_list
*next
;
342 unsigned long long addr
;
347 static int func_cmp(const void *a
, const void *b
)
349 const struct func_map
*fa
= a
;
350 const struct func_map
*fb
= b
;
352 if (fa
->addr
< fb
->addr
)
354 if (fa
->addr
> fb
->addr
)
361 * We are searching for a record in between, not an exact
364 static int func_bcmp(const void *a
, const void *b
)
366 const struct func_map
*fa
= a
;
367 const struct func_map
*fb
= b
;
369 if ((fa
->addr
== fb
->addr
) ||
371 (fa
->addr
> fb
->addr
&&
372 fa
->addr
< (fb
+1)->addr
))
375 if (fa
->addr
< fb
->addr
)
381 static int func_map_init(struct pevent
*pevent
)
383 struct func_list
*funclist
;
384 struct func_list
*item
;
385 struct func_map
*func_map
;
388 func_map
= malloc(sizeof(*func_map
) * (pevent
->func_count
+ 1));
392 funclist
= pevent
->funclist
;
396 func_map
[i
].func
= funclist
->func
;
397 func_map
[i
].addr
= funclist
->addr
;
398 func_map
[i
].mod
= funclist
->mod
;
401 funclist
= funclist
->next
;
405 qsort(func_map
, pevent
->func_count
, sizeof(*func_map
), func_cmp
);
408 * Add a special record at the end.
410 func_map
[pevent
->func_count
].func
= NULL
;
411 func_map
[pevent
->func_count
].addr
= 0;
412 func_map
[pevent
->func_count
].mod
= NULL
;
414 pevent
->func_map
= func_map
;
415 pevent
->funclist
= NULL
;
420 static struct func_map
*
421 find_func(struct pevent
*pevent
, unsigned long long addr
)
423 struct func_map
*func
;
426 if (!pevent
->func_map
)
427 func_map_init(pevent
);
431 func
= bsearch(&key
, pevent
->func_map
, pevent
->func_count
,
432 sizeof(*pevent
->func_map
), func_bcmp
);
438 * pevent_find_function - find a function by a given address
439 * @pevent: handle for the pevent
440 * @addr: the address to find the function with
442 * Returns a pointer to the function stored that has the given
443 * address. Note, the address does not have to be exact, it
444 * will select the function that would contain the address.
446 const char *pevent_find_function(struct pevent
*pevent
, unsigned long long addr
)
448 struct func_map
*map
;
450 map
= find_func(pevent
, addr
);
458 * pevent_find_function_address - find a function address by a given address
459 * @pevent: handle for the pevent
460 * @addr: the address to find the function with
462 * Returns the address the function starts at. This can be used in
463 * conjunction with pevent_find_function to print both the function
464 * name and the function offset.
467 pevent_find_function_address(struct pevent
*pevent
, unsigned long long addr
)
469 struct func_map
*map
;
471 map
= find_func(pevent
, addr
);
479 * pevent_register_function - register a function with a given address
480 * @pevent: handle for the pevent
481 * @function: the function name to register
482 * @addr: the address the function starts at
483 * @mod: the kernel module the function may be in (NULL for none)
485 * This registers a function name with an address and module.
486 * The @func passed in is duplicated.
488 int pevent_register_function(struct pevent
*pevent
, char *func
,
489 unsigned long long addr
, char *mod
)
491 struct func_list
*item
= malloc(sizeof(*item
));
496 item
->next
= pevent
->funclist
;
497 item
->func
= strdup(func
);
502 item
->mod
= strdup(mod
);
509 pevent
->funclist
= item
;
510 pevent
->func_count
++;
524 * pevent_print_funcs - print out the stored functions
525 * @pevent: handle for the pevent
527 * This prints out the stored functions.
529 void pevent_print_funcs(struct pevent
*pevent
)
533 if (!pevent
->func_map
)
534 func_map_init(pevent
);
536 for (i
= 0; i
< (int)pevent
->func_count
; i
++) {
538 pevent
->func_map
[i
].addr
,
539 pevent
->func_map
[i
].func
);
540 if (pevent
->func_map
[i
].mod
)
541 printf(" [%s]\n", pevent
->func_map
[i
].mod
);
548 unsigned long long addr
;
553 struct printk_list
*next
;
554 unsigned long long addr
;
558 static int printk_cmp(const void *a
, const void *b
)
560 const struct printk_map
*pa
= a
;
561 const struct printk_map
*pb
= b
;
563 if (pa
->addr
< pb
->addr
)
565 if (pa
->addr
> pb
->addr
)
571 static int printk_map_init(struct pevent
*pevent
)
573 struct printk_list
*printklist
;
574 struct printk_list
*item
;
575 struct printk_map
*printk_map
;
578 printk_map
= malloc(sizeof(*printk_map
) * (pevent
->printk_count
+ 1));
582 printklist
= pevent
->printklist
;
586 printk_map
[i
].printk
= printklist
->printk
;
587 printk_map
[i
].addr
= printklist
->addr
;
590 printklist
= printklist
->next
;
594 qsort(printk_map
, pevent
->printk_count
, sizeof(*printk_map
), printk_cmp
);
596 pevent
->printk_map
= printk_map
;
597 pevent
->printklist
= NULL
;
602 static struct printk_map
*
603 find_printk(struct pevent
*pevent
, unsigned long long addr
)
605 struct printk_map
*printk
;
606 struct printk_map key
;
608 if (!pevent
->printk_map
&& printk_map_init(pevent
))
613 printk
= bsearch(&key
, pevent
->printk_map
, pevent
->printk_count
,
614 sizeof(*pevent
->printk_map
), printk_cmp
);
620 * pevent_register_print_string - register a string by its address
621 * @pevent: handle for the pevent
622 * @fmt: the string format to register
623 * @addr: the address the string was located at
625 * This registers a string by the address it was stored in the kernel.
626 * The @fmt passed in is duplicated.
628 int pevent_register_print_string(struct pevent
*pevent
, const char *fmt
,
629 unsigned long long addr
)
631 struct printk_list
*item
= malloc(sizeof(*item
));
637 item
->next
= pevent
->printklist
;
640 /* Strip off quotes and '\n' from the end */
643 item
->printk
= strdup(fmt
);
647 p
= item
->printk
+ strlen(item
->printk
) - 1;
652 if (strcmp(p
, "\\n") == 0)
655 pevent
->printklist
= item
;
656 pevent
->printk_count
++;
667 * pevent_print_printk - print out the stored strings
668 * @pevent: handle for the pevent
670 * This prints the string formats that were stored.
672 void pevent_print_printk(struct pevent
*pevent
)
676 if (!pevent
->printk_map
)
677 printk_map_init(pevent
);
679 for (i
= 0; i
< (int)pevent
->printk_count
; i
++) {
680 printf("%016llx %s\n",
681 pevent
->printk_map
[i
].addr
,
682 pevent
->printk_map
[i
].printk
);
686 static struct event_format
*alloc_event(void)
688 return calloc(1, sizeof(struct event_format
));
691 static int add_event(struct pevent
*pevent
, struct event_format
*event
)
694 struct event_format
**events
= realloc(pevent
->events
, sizeof(event
) *
695 (pevent
->nr_events
+ 1));
699 pevent
->events
= events
;
701 for (i
= 0; i
< pevent
->nr_events
; i
++) {
702 if (pevent
->events
[i
]->id
> event
->id
)
705 if (i
< pevent
->nr_events
)
706 memmove(&pevent
->events
[i
+ 1],
708 sizeof(event
) * (pevent
->nr_events
- i
));
710 pevent
->events
[i
] = event
;
713 event
->pevent
= pevent
;
718 static int event_item_type(enum event_type type
)
721 case EVENT_ITEM
... EVENT_SQUOTE
:
723 case EVENT_ERROR
... EVENT_DELIM
:
729 static void free_flag_sym(struct print_flag_sym
*fsym
)
731 struct print_flag_sym
*next
;
742 static void free_arg(struct print_arg
*arg
)
744 struct print_arg
*farg
;
751 free(arg
->atom
.atom
);
754 free(arg
->field
.name
);
757 free_arg(arg
->flags
.field
);
758 free(arg
->flags
.delim
);
759 free_flag_sym(arg
->flags
.flags
);
762 free_arg(arg
->symbol
.field
);
763 free_flag_sym(arg
->symbol
.symbols
);
766 free_arg(arg
->hex
.field
);
767 free_arg(arg
->hex
.size
);
769 case PRINT_INT_ARRAY
:
770 free_arg(arg
->int_array
.field
);
771 free_arg(arg
->int_array
.count
);
772 free_arg(arg
->int_array
.el_size
);
775 free(arg
->typecast
.type
);
776 free_arg(arg
->typecast
.item
);
780 free(arg
->string
.string
);
783 free(arg
->bitmask
.bitmask
);
785 case PRINT_DYNAMIC_ARRAY
:
786 free(arg
->dynarray
.index
);
790 free_arg(arg
->op
.left
);
791 free_arg(arg
->op
.right
);
794 while (arg
->func
.args
) {
795 farg
= arg
->func
.args
;
796 arg
->func
.args
= farg
->next
;
809 static enum event_type
get_type(int ch
)
812 return EVENT_NEWLINE
;
815 if (isalnum(ch
) || ch
== '_')
823 if (ch
== '(' || ch
== ')' || ch
== ',')
829 static int __read_char(void)
831 if (input_buf_ptr
>= input_buf_siz
)
834 return input_buf
[input_buf_ptr
++];
837 static int __peek_char(void)
839 if (input_buf_ptr
>= input_buf_siz
)
842 return input_buf
[input_buf_ptr
];
846 * pevent_peek_char - peek at the next character that will be read
848 * Returns the next character read, or -1 if end of buffer.
850 int pevent_peek_char(void)
852 return __peek_char();
855 static int extend_token(char **tok
, char *buf
, int size
)
857 char *newtok
= realloc(*tok
, size
);
874 static enum event_type
force_token(const char *str
, char **tok
);
876 static enum event_type
__read_token(char **tok
)
879 int ch
, last_ch
, quote_ch
, next_ch
;
882 enum event_type type
;
892 if (type
== EVENT_NONE
)
900 if (asprintf(tok
, "%c", ch
) < 0)
908 next_ch
= __peek_char();
909 if (next_ch
== '>') {
910 buf
[i
++] = __read_char();
923 buf
[i
++] = __read_char();
935 default: /* what should we do instead? */
945 buf
[i
++] = __read_char();
950 /* don't keep quotes */
956 if (i
== (BUFSIZ
- 1)) {
960 if (extend_token(tok
, buf
, tok_size
) < 0)
967 /* the '\' '\' will cancel itself */
968 if (ch
== '\\' && last_ch
== '\\')
970 } while (ch
!= quote_ch
|| last_ch
== '\\');
971 /* remove the last quote */
975 * For strings (double quotes) check the next token.
976 * If it is another string, concatinate the two.
978 if (type
== EVENT_DQUOTE
) {
979 unsigned long long save_input_buf_ptr
= input_buf_ptr
;
983 } while (isspace(ch
));
986 input_buf_ptr
= save_input_buf_ptr
;
991 case EVENT_ERROR
... EVENT_SPACE
:
997 while (get_type(__peek_char()) == type
) {
998 if (i
== (BUFSIZ
- 1)) {
1002 if (extend_token(tok
, buf
, tok_size
) < 0)
1012 if (extend_token(tok
, buf
, tok_size
+ i
+ 1) < 0)
1015 if (type
== EVENT_ITEM
) {
1017 * Older versions of the kernel has a bug that
1018 * creates invalid symbols and will break the mac80211
1019 * parsing. This is a work around to that bug.
1021 * See Linux kernel commit:
1022 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1024 if (strcmp(*tok
, "LOCAL_PR_FMT") == 0) {
1027 return force_token("\"\%s\" ", tok
);
1028 } else if (strcmp(*tok
, "STA_PR_FMT") == 0) {
1031 return force_token("\" sta:%pM\" ", tok
);
1032 } else if (strcmp(*tok
, "VIF_PR_FMT") == 0) {
1035 return force_token("\" vif:%p(%d)\" ", tok
);
1042 static enum event_type
force_token(const char *str
, char **tok
)
1044 const char *save_input_buf
;
1045 unsigned long long save_input_buf_ptr
;
1046 unsigned long long save_input_buf_siz
;
1047 enum event_type type
;
1049 /* save off the current input pointers */
1050 save_input_buf
= input_buf
;
1051 save_input_buf_ptr
= input_buf_ptr
;
1052 save_input_buf_siz
= input_buf_siz
;
1054 init_input_buf(str
, strlen(str
));
1056 type
= __read_token(tok
);
1058 /* reset back to original token */
1059 input_buf
= save_input_buf
;
1060 input_buf_ptr
= save_input_buf_ptr
;
1061 input_buf_siz
= save_input_buf_siz
;
1066 static void free_token(char *tok
)
1072 static enum event_type
read_token(char **tok
)
1074 enum event_type type
;
1077 type
= __read_token(tok
);
1078 if (type
!= EVENT_SPACE
)
1090 * pevent_read_token - access to utilites to use the pevent parser
1091 * @tok: The token to return
1093 * This will parse tokens from the string given by
1094 * pevent_init_data().
1096 * Returns the token type.
1098 enum event_type
pevent_read_token(char **tok
)
1100 return read_token(tok
);
1104 * pevent_free_token - free a token returned by pevent_read_token
1105 * @token: the token to free
1107 void pevent_free_token(char *token
)
1113 static enum event_type
read_token_item(char **tok
)
1115 enum event_type type
;
1118 type
= __read_token(tok
);
1119 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
1130 static int test_type(enum event_type type
, enum event_type expect
)
1132 if (type
!= expect
) {
1133 do_warning("Error: expected type %d but read %d",
1140 static int test_type_token(enum event_type type
, const char *token
,
1141 enum event_type expect
, const char *expect_tok
)
1143 if (type
!= expect
) {
1144 do_warning("Error: expected type %d but read %d",
1149 if (strcmp(token
, expect_tok
) != 0) {
1150 do_warning("Error: expected '%s' but read '%s'",
1157 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
1159 enum event_type type
;
1162 type
= read_token(tok
);
1164 type
= read_token_item(tok
);
1165 return test_type(type
, expect
);
1168 static int read_expect_type(enum event_type expect
, char **tok
)
1170 return __read_expect_type(expect
, tok
, 1);
1173 static int __read_expected(enum event_type expect
, const char *str
,
1176 enum event_type type
;
1181 type
= read_token(&token
);
1183 type
= read_token_item(&token
);
1185 ret
= test_type_token(type
, token
, expect
, str
);
1192 static int read_expected(enum event_type expect
, const char *str
)
1194 return __read_expected(expect
, str
, 1);
1197 static int read_expected_item(enum event_type expect
, const char *str
)
1199 return __read_expected(expect
, str
, 0);
1202 static char *event_read_name(void)
1206 if (read_expected(EVENT_ITEM
, "name") < 0)
1209 if (read_expected(EVENT_OP
, ":") < 0)
1212 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1222 static int event_read_id(void)
1227 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
1230 if (read_expected(EVENT_OP
, ":") < 0)
1233 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1236 id
= strtoul(token
, NULL
, 0);
1245 static int field_is_string(struct format_field
*field
)
1247 if ((field
->flags
& FIELD_IS_ARRAY
) &&
1248 (strstr(field
->type
, "char") || strstr(field
->type
, "u8") ||
1249 strstr(field
->type
, "s8")))
1255 static int field_is_dynamic(struct format_field
*field
)
1257 if (strncmp(field
->type
, "__data_loc", 10) == 0)
1263 static int field_is_long(struct format_field
*field
)
1265 /* includes long long */
1266 if (strstr(field
->type
, "long"))
1272 static unsigned int type_size(const char *name
)
1274 /* This covers all FIELD_IS_STRING types. */
1292 for (i
= 0; table
[i
].type
; i
++) {
1293 if (!strcmp(table
[i
].type
, name
))
1294 return table
[i
].size
;
1300 static int event_read_fields(struct event_format
*event
, struct format_field
**fields
)
1302 struct format_field
*field
= NULL
;
1303 enum event_type type
;
1309 unsigned int size_dynamic
= 0;
1311 type
= read_token(&token
);
1312 if (type
== EVENT_NEWLINE
) {
1319 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
1323 type
= read_token(&token
);
1325 * The ftrace fields may still use the "special" name.
1328 if (event
->flags
& EVENT_FL_ISFTRACE
&&
1329 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
1331 type
= read_token(&token
);
1334 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
1338 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1343 field
= calloc(1, sizeof(*field
));
1347 field
->event
= event
;
1349 /* read the rest of the type */
1351 type
= read_token(&token
);
1352 if (type
== EVENT_ITEM
||
1353 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
1355 * Some of the ftrace fields are broken and have
1356 * an illegal "." in them.
1358 (event
->flags
& EVENT_FL_ISFTRACE
&&
1359 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
1361 if (strcmp(token
, "*") == 0)
1362 field
->flags
|= FIELD_IS_POINTER
;
1366 new_type
= realloc(field
->type
,
1367 strlen(field
->type
) +
1368 strlen(last_token
) + 2);
1373 field
->type
= new_type
;
1374 strcat(field
->type
, " ");
1375 strcat(field
->type
, last_token
);
1378 field
->type
= last_token
;
1387 do_warning_event(event
, "%s: no type found", __func__
);
1390 field
->name
= last_token
;
1392 if (test_type(type
, EVENT_OP
))
1395 if (strcmp(token
, "[") == 0) {
1396 enum event_type last_type
= type
;
1397 char *brackets
= token
;
1401 field
->flags
|= FIELD_IS_ARRAY
;
1403 type
= read_token(&token
);
1405 if (type
== EVENT_ITEM
)
1406 field
->arraylen
= strtoul(token
, NULL
, 0);
1408 field
->arraylen
= 0;
1410 while (strcmp(token
, "]") != 0) {
1411 if (last_type
== EVENT_ITEM
&&
1418 new_brackets
= realloc(brackets
,
1420 strlen(token
) + len
);
1421 if (!new_brackets
) {
1425 brackets
= new_brackets
;
1427 strcat(brackets
, " ");
1428 strcat(brackets
, token
);
1429 /* We only care about the last token */
1430 field
->arraylen
= strtoul(token
, NULL
, 0);
1432 type
= read_token(&token
);
1433 if (type
== EVENT_NONE
) {
1434 do_warning_event(event
, "failed to find token");
1441 new_brackets
= realloc(brackets
, strlen(brackets
) + 2);
1442 if (!new_brackets
) {
1446 brackets
= new_brackets
;
1447 strcat(brackets
, "]");
1449 /* add brackets to type */
1451 type
= read_token(&token
);
1453 * If the next token is not an OP, then it is of
1454 * the format: type [] item;
1456 if (type
== EVENT_ITEM
) {
1458 new_type
= realloc(field
->type
,
1459 strlen(field
->type
) +
1460 strlen(field
->name
) +
1461 strlen(brackets
) + 2);
1466 field
->type
= new_type
;
1467 strcat(field
->type
, " ");
1468 strcat(field
->type
, field
->name
);
1469 size_dynamic
= type_size(field
->name
);
1470 free_token(field
->name
);
1471 strcat(field
->type
, brackets
);
1472 field
->name
= token
;
1473 type
= read_token(&token
);
1476 new_type
= realloc(field
->type
,
1477 strlen(field
->type
) +
1478 strlen(brackets
) + 1);
1483 field
->type
= new_type
;
1484 strcat(field
->type
, brackets
);
1489 if (field_is_string(field
))
1490 field
->flags
|= FIELD_IS_STRING
;
1491 if (field_is_dynamic(field
))
1492 field
->flags
|= FIELD_IS_DYNAMIC
;
1493 if (field_is_long(field
))
1494 field
->flags
|= FIELD_IS_LONG
;
1496 if (test_type_token(type
, token
, EVENT_OP
, ";"))
1500 if (read_expected(EVENT_ITEM
, "offset") < 0)
1503 if (read_expected(EVENT_OP
, ":") < 0)
1506 if (read_expect_type(EVENT_ITEM
, &token
))
1508 field
->offset
= strtoul(token
, NULL
, 0);
1511 if (read_expected(EVENT_OP
, ";") < 0)
1514 if (read_expected(EVENT_ITEM
, "size") < 0)
1517 if (read_expected(EVENT_OP
, ":") < 0)
1520 if (read_expect_type(EVENT_ITEM
, &token
))
1522 field
->size
= strtoul(token
, NULL
, 0);
1525 if (read_expected(EVENT_OP
, ";") < 0)
1528 type
= read_token(&token
);
1529 if (type
!= EVENT_NEWLINE
) {
1530 /* newer versions of the kernel have a "signed" type */
1531 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
1536 if (read_expected(EVENT_OP
, ":") < 0)
1539 if (read_expect_type(EVENT_ITEM
, &token
))
1542 if (strtoul(token
, NULL
, 0))
1543 field
->flags
|= FIELD_IS_SIGNED
;
1546 if (read_expected(EVENT_OP
, ";") < 0)
1549 if (read_expect_type(EVENT_NEWLINE
, &token
))
1555 if (field
->flags
& FIELD_IS_ARRAY
) {
1556 if (field
->arraylen
)
1557 field
->elementsize
= field
->size
/ field
->arraylen
;
1558 else if (field
->flags
& FIELD_IS_DYNAMIC
)
1559 field
->elementsize
= size_dynamic
;
1560 else if (field
->flags
& FIELD_IS_STRING
)
1561 field
->elementsize
= 1;
1562 else if (field
->flags
& FIELD_IS_LONG
)
1563 field
->elementsize
= event
->pevent
?
1564 event
->pevent
->long_size
:
1567 field
->elementsize
= field
->size
;
1570 fields
= &field
->next
;
1587 static int event_read_format(struct event_format
*event
)
1592 if (read_expected_item(EVENT_ITEM
, "format") < 0)
1595 if (read_expected(EVENT_OP
, ":") < 0)
1598 if (read_expect_type(EVENT_NEWLINE
, &token
))
1602 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1605 event
->format
.nr_common
= ret
;
1607 ret
= event_read_fields(event
, &event
->format
.fields
);
1610 event
->format
.nr_fields
= ret
;
1619 static enum event_type
1620 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
1621 char **tok
, enum event_type type
);
1623 static enum event_type
1624 process_arg(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1626 enum event_type type
;
1629 type
= read_token(&token
);
1632 return process_arg_token(event
, arg
, tok
, type
);
1635 static enum event_type
1636 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
);
1639 * For __print_symbolic() and __print_flags, we need to completely
1640 * evaluate the first argument, which defines what to print next.
1642 static enum event_type
1643 process_field_arg(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1645 enum event_type type
;
1647 type
= process_arg(event
, arg
, tok
);
1649 while (type
== EVENT_OP
) {
1650 type
= process_op(event
, arg
, tok
);
1656 static enum event_type
1657 process_cond(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1659 struct print_arg
*arg
, *left
, *right
;
1660 enum event_type type
;
1665 right
= alloc_arg();
1667 if (!arg
|| !left
|| !right
) {
1668 do_warning_event(event
, "%s: not enough memory!", __func__
);
1669 /* arg will be freed at out_free */
1675 arg
->type
= PRINT_OP
;
1676 arg
->op
.left
= left
;
1677 arg
->op
.right
= right
;
1680 type
= process_arg(event
, left
, &token
);
1683 /* Handle other operations in the arguments */
1684 if (type
== EVENT_OP
&& strcmp(token
, ":") != 0) {
1685 type
= process_op(event
, left
, &token
);
1689 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1694 type
= process_arg(event
, right
, &token
);
1696 top
->op
.right
= arg
;
1702 /* Top may point to itself */
1703 top
->op
.right
= NULL
;
1709 static enum event_type
1710 process_array(struct event_format
*event
, struct print_arg
*top
, char **tok
)
1712 struct print_arg
*arg
;
1713 enum event_type type
;
1718 do_warning_event(event
, "%s: not enough memory!", __func__
);
1719 /* '*tok' is set to top->op.op. No need to free. */
1725 type
= process_arg(event
, arg
, &token
);
1726 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1729 top
->op
.right
= arg
;
1732 type
= read_token_item(&token
);
1743 static int get_op_prio(char *op
)
1757 /* '>>' and '<<' are 8 */
1761 /* '==' and '!=' are 10 */
1771 do_warning("unknown op '%c'", op
[0]);
1775 if (strcmp(op
, "++") == 0 ||
1776 strcmp(op
, "--") == 0) {
1778 } else if (strcmp(op
, ">>") == 0 ||
1779 strcmp(op
, "<<") == 0) {
1781 } else if (strcmp(op
, ">=") == 0 ||
1782 strcmp(op
, "<=") == 0) {
1784 } else if (strcmp(op
, "==") == 0 ||
1785 strcmp(op
, "!=") == 0) {
1787 } else if (strcmp(op
, "&&") == 0) {
1789 } else if (strcmp(op
, "||") == 0) {
1792 do_warning("unknown op '%s'", op
);
1798 static int set_op_prio(struct print_arg
*arg
)
1801 /* single ops are the greatest */
1802 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
)
1805 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1807 return arg
->op
.prio
;
1810 /* Note, *tok does not get freed, but will most likely be saved */
1811 static enum event_type
1812 process_op(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
1814 struct print_arg
*left
, *right
= NULL
;
1815 enum event_type type
;
1818 /* the op is passed in via tok */
1821 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1822 /* handle single op */
1824 do_warning_event(event
, "bad op token %s", token
);
1834 do_warning_event(event
, "bad op token %s", token
);
1839 /* make an empty left */
1844 left
->type
= PRINT_NULL
;
1845 arg
->op
.left
= left
;
1847 right
= alloc_arg();
1851 arg
->op
.right
= right
;
1853 /* do not free the token, it belongs to an op */
1855 type
= process_arg(event
, right
, tok
);
1857 } else if (strcmp(token
, "?") == 0) {
1863 /* copy the top arg to the left */
1866 arg
->type
= PRINT_OP
;
1868 arg
->op
.left
= left
;
1871 /* it will set arg->op.right */
1872 type
= process_cond(event
, arg
, tok
);
1874 } else if (strcmp(token
, ">>") == 0 ||
1875 strcmp(token
, "<<") == 0 ||
1876 strcmp(token
, "&") == 0 ||
1877 strcmp(token
, "|") == 0 ||
1878 strcmp(token
, "&&") == 0 ||
1879 strcmp(token
, "||") == 0 ||
1880 strcmp(token
, "-") == 0 ||
1881 strcmp(token
, "+") == 0 ||
1882 strcmp(token
, "*") == 0 ||
1883 strcmp(token
, "^") == 0 ||
1884 strcmp(token
, "/") == 0 ||
1885 strcmp(token
, "<") == 0 ||
1886 strcmp(token
, ">") == 0 ||
1887 strcmp(token
, "<=") == 0 ||
1888 strcmp(token
, ">=") == 0 ||
1889 strcmp(token
, "==") == 0 ||
1890 strcmp(token
, "!=") == 0) {
1896 /* copy the top arg to the left */
1899 arg
->type
= PRINT_OP
;
1901 arg
->op
.left
= left
;
1902 arg
->op
.right
= NULL
;
1904 if (set_op_prio(arg
) == -1) {
1905 event
->flags
|= EVENT_FL_FAILED
;
1906 /* arg->op.op (= token) will be freed at out_free */
1911 type
= read_token_item(&token
);
1914 /* could just be a type pointer */
1915 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1916 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1919 if (left
->type
!= PRINT_ATOM
) {
1920 do_warning_event(event
, "bad pointer type");
1923 new_atom
= realloc(left
->atom
.atom
,
1924 strlen(left
->atom
.atom
) + 3);
1928 left
->atom
.atom
= new_atom
;
1929 strcat(left
->atom
.atom
, " *");
1937 right
= alloc_arg();
1941 type
= process_arg_token(event
, right
, tok
, type
);
1942 arg
->op
.right
= right
;
1944 } else if (strcmp(token
, "[") == 0) {
1952 arg
->type
= PRINT_OP
;
1954 arg
->op
.left
= left
;
1958 /* it will set arg->op.right */
1959 type
= process_array(event
, arg
, tok
);
1962 do_warning_event(event
, "unknown op '%s'", token
);
1963 event
->flags
|= EVENT_FL_FAILED
;
1964 /* the arg is now the left side */
1968 if (type
== EVENT_OP
&& strcmp(*tok
, ":") != 0) {
1971 /* higher prios need to be closer to the root */
1972 prio
= get_op_prio(*tok
);
1974 if (prio
> arg
->op
.prio
)
1975 return process_op(event
, arg
, tok
);
1977 return process_op(event
, right
, tok
);
1983 do_warning_event(event
, "%s: not enough memory!", __func__
);
1990 static enum event_type
1991 process_entry(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
1994 enum event_type type
;
1998 if (read_expected(EVENT_OP
, "->") < 0)
2001 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2005 arg
->type
= PRINT_FIELD
;
2006 arg
->field
.name
= field
;
2008 if (is_flag_field
) {
2009 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
2010 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
2012 } else if (is_symbolic_field
) {
2013 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
2014 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
2015 is_symbolic_field
= 0;
2018 type
= read_token(&token
);
2030 static int alloc_and_process_delim(struct event_format
*event
, char *next_token
,
2031 struct print_arg
**print_arg
)
2033 struct print_arg
*field
;
2034 enum event_type type
;
2038 field
= alloc_arg();
2040 do_warning_event(event
, "%s: not enough memory!", __func__
);
2045 type
= process_arg(event
, field
, &token
);
2047 if (test_type_token(type
, token
, EVENT_DELIM
, next_token
)) {
2051 goto out_free_token
;
2062 static char *arg_eval (struct print_arg
*arg
);
2064 static unsigned long long
2065 eval_type_str(unsigned long long val
, const char *type
, int pointer
)
2075 if (type
[len
-1] != '*') {
2076 do_warning("pointer expected with non pointer type");
2082 do_warning("%s: not enough memory!", __func__
);
2085 memcpy(ref
, type
, len
);
2087 /* chop off the " *" */
2090 val
= eval_type_str(val
, ref
, 0);
2095 /* check if this is a pointer */
2096 if (type
[len
- 1] == '*')
2099 /* Try to figure out the arg size*/
2100 if (strncmp(type
, "struct", 6) == 0)
2104 if (strcmp(type
, "u8") == 0)
2107 if (strcmp(type
, "u16") == 0)
2108 return val
& 0xffff;
2110 if (strcmp(type
, "u32") == 0)
2111 return val
& 0xffffffff;
2113 if (strcmp(type
, "u64") == 0 ||
2114 strcmp(type
, "s64"))
2117 if (strcmp(type
, "s8") == 0)
2118 return (unsigned long long)(char)val
& 0xff;
2120 if (strcmp(type
, "s16") == 0)
2121 return (unsigned long long)(short)val
& 0xffff;
2123 if (strcmp(type
, "s32") == 0)
2124 return (unsigned long long)(int)val
& 0xffffffff;
2126 if (strncmp(type
, "unsigned ", 9) == 0) {
2131 if (strcmp(type
, "char") == 0) {
2133 return (unsigned long long)(char)val
& 0xff;
2138 if (strcmp(type
, "short") == 0) {
2140 return (unsigned long long)(short)val
& 0xffff;
2142 return val
& 0xffff;
2145 if (strcmp(type
, "int") == 0) {
2147 return (unsigned long long)(int)val
& 0xffffffff;
2149 return val
& 0xffffffff;
2156 * Try to figure out the type.
2158 static unsigned long long
2159 eval_type(unsigned long long val
, struct print_arg
*arg
, int pointer
)
2161 if (arg
->type
!= PRINT_TYPE
) {
2162 do_warning("expected type argument");
2166 return eval_type_str(val
, arg
->typecast
.type
, pointer
);
2169 static int arg_num_eval(struct print_arg
*arg
, long long *val
)
2171 long long left
, right
;
2174 switch (arg
->type
) {
2176 *val
= strtoll(arg
->atom
.atom
, NULL
, 0);
2179 ret
= arg_num_eval(arg
->typecast
.item
, val
);
2182 *val
= eval_type(*val
, arg
, 0);
2185 switch (arg
->op
.op
[0]) {
2187 ret
= arg_num_eval(arg
->op
.left
, &left
);
2190 ret
= arg_num_eval(arg
->op
.right
, &right
);
2194 *val
= left
|| right
;
2196 *val
= left
| right
;
2199 ret
= arg_num_eval(arg
->op
.left
, &left
);
2202 ret
= arg_num_eval(arg
->op
.right
, &right
);
2206 *val
= left
&& right
;
2208 *val
= left
& right
;
2211 ret
= arg_num_eval(arg
->op
.left
, &left
);
2214 ret
= arg_num_eval(arg
->op
.right
, &right
);
2217 switch (arg
->op
.op
[1]) {
2219 *val
= left
< right
;
2222 *val
= left
<< right
;
2225 *val
= left
<= right
;
2228 do_warning("unknown op '%s'", arg
->op
.op
);
2233 ret
= arg_num_eval(arg
->op
.left
, &left
);
2236 ret
= arg_num_eval(arg
->op
.right
, &right
);
2239 switch (arg
->op
.op
[1]) {
2241 *val
= left
> right
;
2244 *val
= left
>> right
;
2247 *val
= left
>= right
;
2250 do_warning("unknown op '%s'", arg
->op
.op
);
2255 ret
= arg_num_eval(arg
->op
.left
, &left
);
2258 ret
= arg_num_eval(arg
->op
.right
, &right
);
2262 if (arg
->op
.op
[1] != '=') {
2263 do_warning("unknown op '%s'", arg
->op
.op
);
2266 *val
= left
== right
;
2269 ret
= arg_num_eval(arg
->op
.left
, &left
);
2272 ret
= arg_num_eval(arg
->op
.right
, &right
);
2276 switch (arg
->op
.op
[1]) {
2278 *val
= left
!= right
;
2281 do_warning("unknown op '%s'", arg
->op
.op
);
2286 /* check for negative */
2287 if (arg
->op
.left
->type
== PRINT_NULL
)
2290 ret
= arg_num_eval(arg
->op
.left
, &left
);
2293 ret
= arg_num_eval(arg
->op
.right
, &right
);
2296 *val
= left
- right
;
2299 if (arg
->op
.left
->type
== PRINT_NULL
)
2302 ret
= arg_num_eval(arg
->op
.left
, &left
);
2305 ret
= arg_num_eval(arg
->op
.right
, &right
);
2308 *val
= left
+ right
;
2311 do_warning("unknown op '%s'", arg
->op
.op
);
2317 case PRINT_FIELD
... PRINT_SYMBOL
:
2322 do_warning("invalid eval type %d", arg
->type
);
2329 static char *arg_eval (struct print_arg
*arg
)
2332 static char buf
[20];
2334 switch (arg
->type
) {
2336 return arg
->atom
.atom
;
2338 return arg_eval(arg
->typecast
.item
);
2340 if (!arg_num_eval(arg
, &val
))
2342 sprintf(buf
, "%lld", val
);
2346 case PRINT_FIELD
... PRINT_SYMBOL
:
2351 do_warning("invalid eval type %d", arg
->type
);
2358 static enum event_type
2359 process_fields(struct event_format
*event
, struct print_flag_sym
**list
, char **tok
)
2361 enum event_type type
;
2362 struct print_arg
*arg
= NULL
;
2363 struct print_flag_sym
*field
;
2369 type
= read_token_item(&token
);
2370 if (test_type_token(type
, token
, EVENT_OP
, "{"))
2378 type
= process_arg(event
, arg
, &token
);
2380 if (type
== EVENT_OP
)
2381 type
= process_op(event
, arg
, &token
);
2383 if (type
== EVENT_ERROR
)
2386 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2389 field
= calloc(1, sizeof(*field
));
2393 value
= arg_eval(arg
);
2395 goto out_free_field
;
2396 field
->value
= strdup(value
);
2397 if (field
->value
== NULL
)
2398 goto out_free_field
;
2406 type
= process_arg(event
, arg
, &token
);
2407 if (test_type_token(type
, token
, EVENT_OP
, "}"))
2408 goto out_free_field
;
2410 value
= arg_eval(arg
);
2412 goto out_free_field
;
2413 field
->str
= strdup(value
);
2414 if (field
->str
== NULL
)
2415 goto out_free_field
;
2420 list
= &field
->next
;
2423 type
= read_token_item(&token
);
2424 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
2430 free_flag_sym(field
);
2439 static enum event_type
2440 process_flags(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2442 struct print_arg
*field
;
2443 enum event_type type
;
2446 memset(arg
, 0, sizeof(*arg
));
2447 arg
->type
= PRINT_FLAGS
;
2449 field
= alloc_arg();
2451 do_warning_event(event
, "%s: not enough memory!", __func__
);
2455 type
= process_field_arg(event
, field
, &token
);
2457 /* Handle operations in the first argument */
2458 while (type
== EVENT_OP
)
2459 type
= process_op(event
, field
, &token
);
2461 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2462 goto out_free_field
;
2465 arg
->flags
.field
= field
;
2467 type
= read_token_item(&token
);
2468 if (event_item_type(type
)) {
2469 arg
->flags
.delim
= token
;
2470 type
= read_token_item(&token
);
2473 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2476 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
2477 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2481 type
= read_token_item(tok
);
2492 static enum event_type
2493 process_symbols(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2495 struct print_arg
*field
;
2496 enum event_type type
;
2499 memset(arg
, 0, sizeof(*arg
));
2500 arg
->type
= PRINT_SYMBOL
;
2502 field
= alloc_arg();
2504 do_warning_event(event
, "%s: not enough memory!", __func__
);
2508 type
= process_field_arg(event
, field
, &token
);
2510 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
2511 goto out_free_field
;
2513 arg
->symbol
.field
= field
;
2515 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
2516 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2520 type
= read_token_item(tok
);
2531 static enum event_type
2532 process_hex(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2534 memset(arg
, 0, sizeof(*arg
));
2535 arg
->type
= PRINT_HEX
;
2537 if (alloc_and_process_delim(event
, ",", &arg
->hex
.field
))
2540 if (alloc_and_process_delim(event
, ")", &arg
->hex
.size
))
2543 return read_token_item(tok
);
2546 free_arg(arg
->hex
.field
);
2552 static enum event_type
2553 process_int_array(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2555 memset(arg
, 0, sizeof(*arg
));
2556 arg
->type
= PRINT_INT_ARRAY
;
2558 if (alloc_and_process_delim(event
, ",", &arg
->int_array
.field
))
2561 if (alloc_and_process_delim(event
, ",", &arg
->int_array
.count
))
2564 if (alloc_and_process_delim(event
, ")", &arg
->int_array
.el_size
))
2567 return read_token_item(tok
);
2570 free_arg(arg
->int_array
.count
);
2572 free_arg(arg
->int_array
.field
);
2578 static enum event_type
2579 process_dynamic_array(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2581 struct format_field
*field
;
2582 enum event_type type
;
2585 memset(arg
, 0, sizeof(*arg
));
2586 arg
->type
= PRINT_DYNAMIC_ARRAY
;
2589 * The item within the parenthesis is another field that holds
2590 * the index into where the array starts.
2592 type
= read_token(&token
);
2594 if (type
!= EVENT_ITEM
)
2597 /* Find the field */
2599 field
= pevent_find_field(event
, token
);
2603 arg
->dynarray
.field
= field
;
2604 arg
->dynarray
.index
= 0;
2606 if (read_expected(EVENT_DELIM
, ")") < 0)
2610 type
= read_token_item(&token
);
2612 if (type
!= EVENT_OP
|| strcmp(token
, "[") != 0)
2618 do_warning_event(event
, "%s: not enough memory!", __func__
);
2623 type
= process_arg(event
, arg
, &token
);
2624 if (type
== EVENT_ERROR
)
2627 if (!test_type_token(type
, token
, EVENT_OP
, "]"))
2631 type
= read_token_item(tok
);
2642 static enum event_type
2643 process_paren(struct event_format
*event
, struct print_arg
*arg
, char **tok
)
2645 struct print_arg
*item_arg
;
2646 enum event_type type
;
2649 type
= process_arg(event
, arg
, &token
);
2651 if (type
== EVENT_ERROR
)
2654 if (type
== EVENT_OP
)
2655 type
= process_op(event
, arg
, &token
);
2657 if (type
== EVENT_ERROR
)
2660 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
2664 type
= read_token_item(&token
);
2667 * If the next token is an item or another open paren, then
2668 * this was a typecast.
2670 if (event_item_type(type
) ||
2671 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
2673 /* make this a typecast and contine */
2675 /* prevous must be an atom */
2676 if (arg
->type
!= PRINT_ATOM
) {
2677 do_warning_event(event
, "previous needed to be PRINT_ATOM");
2681 item_arg
= alloc_arg();
2683 do_warning_event(event
, "%s: not enough memory!",
2688 arg
->type
= PRINT_TYPE
;
2689 arg
->typecast
.type
= arg
->atom
.atom
;
2690 arg
->typecast
.item
= item_arg
;
2691 type
= process_arg_token(event
, item_arg
, &token
, type
);
2705 static enum event_type
2706 process_str(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
2709 enum event_type type
;
2712 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2715 arg
->type
= PRINT_STRING
;
2716 arg
->string
.string
= token
;
2717 arg
->string
.offset
= -1;
2719 if (read_expected(EVENT_DELIM
, ")") < 0)
2722 type
= read_token(&token
);
2734 static enum event_type
2735 process_bitmask(struct event_format
*event __maybe_unused
, struct print_arg
*arg
,
2738 enum event_type type
;
2741 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2744 arg
->type
= PRINT_BITMASK
;
2745 arg
->bitmask
.bitmask
= token
;
2746 arg
->bitmask
.offset
= -1;
2748 if (read_expected(EVENT_DELIM
, ")") < 0)
2751 type
= read_token(&token
);
2763 static struct pevent_function_handler
*
2764 find_func_handler(struct pevent
*pevent
, char *func_name
)
2766 struct pevent_function_handler
*func
;
2771 for (func
= pevent
->func_handlers
; func
; func
= func
->next
) {
2772 if (strcmp(func
->name
, func_name
) == 0)
2779 static void remove_func_handler(struct pevent
*pevent
, char *func_name
)
2781 struct pevent_function_handler
*func
;
2782 struct pevent_function_handler
**next
;
2784 next
= &pevent
->func_handlers
;
2785 while ((func
= *next
)) {
2786 if (strcmp(func
->name
, func_name
) == 0) {
2788 free_func_handle(func
);
2795 static enum event_type
2796 process_func_handler(struct event_format
*event
, struct pevent_function_handler
*func
,
2797 struct print_arg
*arg
, char **tok
)
2799 struct print_arg
**next_arg
;
2800 struct print_arg
*farg
;
2801 enum event_type type
;
2805 arg
->type
= PRINT_FUNC
;
2806 arg
->func
.func
= func
;
2810 next_arg
= &(arg
->func
.args
);
2811 for (i
= 0; i
< func
->nr_args
; i
++) {
2814 do_warning_event(event
, "%s: not enough memory!",
2819 type
= process_arg(event
, farg
, &token
);
2820 if (i
< (func
->nr_args
- 1)) {
2821 if (type
!= EVENT_DELIM
|| strcmp(token
, ",") != 0) {
2822 do_warning_event(event
,
2823 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2824 func
->name
, func
->nr_args
,
2825 event
->name
, i
+ 1);
2829 if (type
!= EVENT_DELIM
|| strcmp(token
, ")") != 0) {
2830 do_warning_event(event
,
2831 "Error: function '%s()' only expects %d arguments but event %s has more",
2832 func
->name
, func
->nr_args
, event
->name
);
2838 next_arg
= &(farg
->next
);
2842 type
= read_token(&token
);
2853 static enum event_type
2854 process_function(struct event_format
*event
, struct print_arg
*arg
,
2855 char *token
, char **tok
)
2857 struct pevent_function_handler
*func
;
2859 if (strcmp(token
, "__print_flags") == 0) {
2862 return process_flags(event
, arg
, tok
);
2864 if (strcmp(token
, "__print_symbolic") == 0) {
2866 is_symbolic_field
= 1;
2867 return process_symbols(event
, arg
, tok
);
2869 if (strcmp(token
, "__print_hex") == 0) {
2871 return process_hex(event
, arg
, tok
);
2873 if (strcmp(token
, "__print_array") == 0) {
2875 return process_int_array(event
, arg
, tok
);
2877 if (strcmp(token
, "__get_str") == 0) {
2879 return process_str(event
, arg
, tok
);
2881 if (strcmp(token
, "__get_bitmask") == 0) {
2883 return process_bitmask(event
, arg
, tok
);
2885 if (strcmp(token
, "__get_dynamic_array") == 0) {
2887 return process_dynamic_array(event
, arg
, tok
);
2890 func
= find_func_handler(event
->pevent
, token
);
2893 return process_func_handler(event
, func
, arg
, tok
);
2896 do_warning_event(event
, "function %s not defined", token
);
2901 static enum event_type
2902 process_arg_token(struct event_format
*event
, struct print_arg
*arg
,
2903 char **tok
, enum event_type type
)
2912 if (strcmp(token
, "REC") == 0) {
2914 type
= process_entry(event
, arg
, &token
);
2918 /* test the next token */
2919 type
= read_token_item(&token
);
2922 * If the next token is a parenthesis, then this
2925 if (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0) {
2928 /* this will free atom. */
2929 type
= process_function(event
, arg
, atom
, &token
);
2932 /* atoms can be more than one token long */
2933 while (type
== EVENT_ITEM
) {
2935 new_atom
= realloc(atom
,
2936 strlen(atom
) + strlen(token
) + 2);
2945 strcat(atom
, token
);
2947 type
= read_token_item(&token
);
2950 arg
->type
= PRINT_ATOM
;
2951 arg
->atom
.atom
= atom
;
2956 arg
->type
= PRINT_ATOM
;
2957 arg
->atom
.atom
= token
;
2958 type
= read_token_item(&token
);
2961 if (strcmp(token
, "(") == 0) {
2963 type
= process_paren(event
, arg
, &token
);
2967 /* handle single ops */
2968 arg
->type
= PRINT_OP
;
2970 arg
->op
.left
= NULL
;
2971 type
= process_op(event
, arg
, &token
);
2973 /* On error, the op is freed */
2974 if (type
== EVENT_ERROR
)
2977 /* return error type if errored */
2980 case EVENT_ERROR
... EVENT_NEWLINE
:
2982 do_warning_event(event
, "unexpected type %d", type
);
2990 static int event_read_print_args(struct event_format
*event
, struct print_arg
**list
)
2992 enum event_type type
= EVENT_ERROR
;
2993 struct print_arg
*arg
;
2998 if (type
== EVENT_NEWLINE
) {
2999 type
= read_token_item(&token
);
3005 do_warning_event(event
, "%s: not enough memory!",
3010 type
= process_arg(event
, arg
, &token
);
3012 if (type
== EVENT_ERROR
) {
3021 if (type
== EVENT_OP
) {
3022 type
= process_op(event
, arg
, &token
);
3024 if (type
== EVENT_ERROR
) {
3033 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
3040 } while (type
!= EVENT_NONE
);
3042 if (type
!= EVENT_NONE
&& type
!= EVENT_ERROR
)
3048 static int event_read_print(struct event_format
*event
)
3050 enum event_type type
;
3054 if (read_expected_item(EVENT_ITEM
, "print") < 0)
3057 if (read_expected(EVENT_ITEM
, "fmt") < 0)
3060 if (read_expected(EVENT_OP
, ":") < 0)
3063 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
3067 event
->print_fmt
.format
= token
;
3068 event
->print_fmt
.args
= NULL
;
3070 /* ok to have no arg */
3071 type
= read_token_item(&token
);
3073 if (type
== EVENT_NONE
)
3076 /* Handle concatenation of print lines */
3077 if (type
== EVENT_DQUOTE
) {
3080 if (asprintf(&cat
, "%s%s", event
->print_fmt
.format
, token
) < 0)
3083 free_token(event
->print_fmt
.format
);
3084 event
->print_fmt
.format
= NULL
;
3089 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
3094 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
3106 * pevent_find_common_field - return a common field by event
3107 * @event: handle for the event
3108 * @name: the name of the common field to return
3110 * Returns a common field from the event by the given @name.
3111 * This only searchs the common fields and not all field.
3113 struct format_field
*
3114 pevent_find_common_field(struct event_format
*event
, const char *name
)
3116 struct format_field
*format
;
3118 for (format
= event
->format
.common_fields
;
3119 format
; format
= format
->next
) {
3120 if (strcmp(format
->name
, name
) == 0)
3128 * pevent_find_field - find a non-common field
3129 * @event: handle for the event
3130 * @name: the name of the non-common field
3132 * Returns a non-common field by the given @name.
3133 * This does not search common fields.
3135 struct format_field
*
3136 pevent_find_field(struct event_format
*event
, const char *name
)
3138 struct format_field
*format
;
3140 for (format
= event
->format
.fields
;
3141 format
; format
= format
->next
) {
3142 if (strcmp(format
->name
, name
) == 0)
3150 * pevent_find_any_field - find any field by name
3151 * @event: handle for the event
3152 * @name: the name of the field
3154 * Returns a field by the given @name.
3155 * This searchs the common field names first, then
3156 * the non-common ones if a common one was not found.
3158 struct format_field
*
3159 pevent_find_any_field(struct event_format
*event
, const char *name
)
3161 struct format_field
*format
;
3163 format
= pevent_find_common_field(event
, name
);
3166 return pevent_find_field(event
, name
);
3170 * pevent_read_number - read a number from data
3171 * @pevent: handle for the pevent
3172 * @ptr: the raw data
3173 * @size: the size of the data that holds the number
3175 * Returns the number (converted to host) from the
3178 unsigned long long pevent_read_number(struct pevent
*pevent
,
3179 const void *ptr
, int size
)
3183 return *(unsigned char *)ptr
;
3185 return data2host2(pevent
, ptr
);
3187 return data2host4(pevent
, ptr
);
3189 return data2host8(pevent
, ptr
);
3197 * pevent_read_number_field - read a number from data
3198 * @field: a handle to the field
3199 * @data: the raw data to read
3200 * @value: the value to place the number in
3202 * Reads raw data according to a field offset and size,
3203 * and translates it into @value.
3205 * Returns 0 on success, -1 otherwise.
3207 int pevent_read_number_field(struct format_field
*field
, const void *data
,
3208 unsigned long long *value
)
3212 switch (field
->size
) {
3217 *value
= pevent_read_number(field
->event
->pevent
,
3218 data
+ field
->offset
, field
->size
);
3225 static int get_common_info(struct pevent
*pevent
,
3226 const char *type
, int *offset
, int *size
)
3228 struct event_format
*event
;
3229 struct format_field
*field
;
3232 * All events should have the same common elements.
3233 * Pick any event to find where the type is;
3235 if (!pevent
->events
) {
3236 do_warning("no event_list!");
3240 event
= pevent
->events
[0];
3241 field
= pevent_find_common_field(event
, type
);
3245 *offset
= field
->offset
;
3246 *size
= field
->size
;
3251 static int __parse_common(struct pevent
*pevent
, void *data
,
3252 int *size
, int *offset
, const char *name
)
3257 ret
= get_common_info(pevent
, name
, offset
, size
);
3261 return pevent_read_number(pevent
, data
+ *offset
, *size
);
3264 static int trace_parse_common_type(struct pevent
*pevent
, void *data
)
3266 return __parse_common(pevent
, data
,
3267 &pevent
->type_size
, &pevent
->type_offset
,
3271 static int parse_common_pid(struct pevent
*pevent
, void *data
)
3273 return __parse_common(pevent
, data
,
3274 &pevent
->pid_size
, &pevent
->pid_offset
,
3278 static int parse_common_pc(struct pevent
*pevent
, void *data
)
3280 return __parse_common(pevent
, data
,
3281 &pevent
->pc_size
, &pevent
->pc_offset
,
3282 "common_preempt_count");
3285 static int parse_common_flags(struct pevent
*pevent
, void *data
)
3287 return __parse_common(pevent
, data
,
3288 &pevent
->flags_size
, &pevent
->flags_offset
,
3292 static int parse_common_lock_depth(struct pevent
*pevent
, void *data
)
3294 return __parse_common(pevent
, data
,
3295 &pevent
->ld_size
, &pevent
->ld_offset
,
3296 "common_lock_depth");
3299 static int parse_common_migrate_disable(struct pevent
*pevent
, void *data
)
3301 return __parse_common(pevent
, data
,
3302 &pevent
->ld_size
, &pevent
->ld_offset
,
3303 "common_migrate_disable");
3306 static int events_id_cmp(const void *a
, const void *b
);
3309 * pevent_find_event - find an event by given id
3310 * @pevent: a handle to the pevent
3311 * @id: the id of the event
3313 * Returns an event that has a given @id.
3315 struct event_format
*pevent_find_event(struct pevent
*pevent
, int id
)
3317 struct event_format
**eventptr
;
3318 struct event_format key
;
3319 struct event_format
*pkey
= &key
;
3321 /* Check cache first */
3322 if (pevent
->last_event
&& pevent
->last_event
->id
== id
)
3323 return pevent
->last_event
;
3327 eventptr
= bsearch(&pkey
, pevent
->events
, pevent
->nr_events
,
3328 sizeof(*pevent
->events
), events_id_cmp
);
3331 pevent
->last_event
= *eventptr
;
3339 * pevent_find_event_by_name - find an event by given name
3340 * @pevent: a handle to the pevent
3341 * @sys: the system name to search for
3342 * @name: the name of the event to search for
3344 * This returns an event with a given @name and under the system
3345 * @sys. If @sys is NULL the first event with @name is returned.
3347 struct event_format
*
3348 pevent_find_event_by_name(struct pevent
*pevent
,
3349 const char *sys
, const char *name
)
3351 struct event_format
*event
;
3354 if (pevent
->last_event
&&
3355 strcmp(pevent
->last_event
->name
, name
) == 0 &&
3356 (!sys
|| strcmp(pevent
->last_event
->system
, sys
) == 0))
3357 return pevent
->last_event
;
3359 for (i
= 0; i
< pevent
->nr_events
; i
++) {
3360 event
= pevent
->events
[i
];
3361 if (strcmp(event
->name
, name
) == 0) {
3364 if (strcmp(event
->system
, sys
) == 0)
3368 if (i
== pevent
->nr_events
)
3371 pevent
->last_event
= event
;
3375 static unsigned long long
3376 eval_num_arg(void *data
, int size
, struct event_format
*event
, struct print_arg
*arg
)
3378 struct pevent
*pevent
= event
->pevent
;
3379 unsigned long long val
= 0;
3380 unsigned long long left
, right
;
3381 struct print_arg
*typearg
= NULL
;
3382 struct print_arg
*larg
;
3383 unsigned long offset
;
3384 unsigned int field_size
;
3386 switch (arg
->type
) {
3391 return strtoull(arg
->atom
.atom
, NULL
, 0);
3393 if (!arg
->field
.field
) {
3394 arg
->field
.field
= pevent_find_any_field(event
, arg
->field
.name
);
3395 if (!arg
->field
.field
)
3396 goto out_warning_field
;
3399 /* must be a number */
3400 val
= pevent_read_number(pevent
, data
+ arg
->field
.field
->offset
,
3401 arg
->field
.field
->size
);
3405 case PRINT_INT_ARRAY
:
3409 val
= eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
3410 return eval_type(val
, arg
, 0);
3418 val
= process_defined_func(&s
, data
, size
, event
, arg
);
3419 trace_seq_destroy(&s
);
3423 if (strcmp(arg
->op
.op
, "[") == 0) {
3425 * Arrays are special, since we don't want
3426 * to read the arg as is.
3428 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3430 /* handle typecasts */
3431 larg
= arg
->op
.left
;
3432 while (larg
->type
== PRINT_TYPE
) {
3435 larg
= larg
->typecast
.item
;
3438 /* Default to long size */
3439 field_size
= pevent
->long_size
;
3441 switch (larg
->type
) {
3442 case PRINT_DYNAMIC_ARRAY
:
3443 offset
= pevent_read_number(pevent
,
3444 data
+ larg
->dynarray
.field
->offset
,
3445 larg
->dynarray
.field
->size
);
3446 if (larg
->dynarray
.field
->elementsize
)
3447 field_size
= larg
->dynarray
.field
->elementsize
;
3449 * The actual length of the dynamic array is stored
3450 * in the top half of the field, and the offset
3451 * is in the bottom half of the 32 bit field.
3457 if (!larg
->field
.field
) {
3459 pevent_find_any_field(event
, larg
->field
.name
);
3460 if (!larg
->field
.field
) {
3462 goto out_warning_field
;
3465 field_size
= larg
->field
.field
->elementsize
;
3466 offset
= larg
->field
.field
->offset
+
3467 right
* larg
->field
.field
->elementsize
;
3470 goto default_op
; /* oops, all bets off */
3472 val
= pevent_read_number(pevent
,
3473 data
+ offset
, field_size
);
3475 val
= eval_type(val
, typearg
, 1);
3477 } else if (strcmp(arg
->op
.op
, "?") == 0) {
3478 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3479 arg
= arg
->op
.right
;
3481 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3483 val
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3487 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3488 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
3489 switch (arg
->op
.op
[0]) {
3491 switch (arg
->op
.op
[1]) {
3496 val
= left
!= right
;
3499 goto out_warning_op
;
3507 val
= left
|| right
;
3513 val
= left
&& right
;
3518 switch (arg
->op
.op
[1]) {
3523 val
= left
<< right
;
3526 val
= left
<= right
;
3529 goto out_warning_op
;
3533 switch (arg
->op
.op
[1]) {
3538 val
= left
>> right
;
3541 val
= left
>= right
;
3544 goto out_warning_op
;
3548 if (arg
->op
.op
[1] != '=')
3549 goto out_warning_op
;
3551 val
= left
== right
;
3566 goto out_warning_op
;
3569 case PRINT_DYNAMIC_ARRAY
:
3570 /* Without [], we pass the address to the dynamic data */
3571 offset
= pevent_read_number(pevent
,
3572 data
+ arg
->dynarray
.field
->offset
,
3573 arg
->dynarray
.field
->size
);
3575 * The actual length of the dynamic array is stored
3576 * in the top half of the field, and the offset
3577 * is in the bottom half of the 32 bit field.
3580 val
= (unsigned long long)((unsigned long)data
+ offset
);
3582 default: /* not sure what to do there */
3588 do_warning_event(event
, "%s: unknown op '%s'", __func__
, arg
->op
.op
);
3592 do_warning_event(event
, "%s: field %s not found",
3593 __func__
, arg
->field
.name
);
3599 unsigned long long value
;
3602 static const struct flag flags
[] = {
3603 { "HI_SOFTIRQ", 0 },
3604 { "TIMER_SOFTIRQ", 1 },
3605 { "NET_TX_SOFTIRQ", 2 },
3606 { "NET_RX_SOFTIRQ", 3 },
3607 { "BLOCK_SOFTIRQ", 4 },
3608 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3609 { "TASKLET_SOFTIRQ", 6 },
3610 { "SCHED_SOFTIRQ", 7 },
3611 { "HRTIMER_SOFTIRQ", 8 },
3612 { "RCU_SOFTIRQ", 9 },
3614 { "HRTIMER_NORESTART", 0 },
3615 { "HRTIMER_RESTART", 1 },
3618 static unsigned long long eval_flag(const char *flag
)
3623 * Some flags in the format files do not get converted.
3624 * If the flag is not numeric, see if it is something that
3625 * we already know about.
3627 if (isdigit(flag
[0]))
3628 return strtoull(flag
, NULL
, 0);
3630 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
3631 if (strcmp(flags
[i
].name
, flag
) == 0)
3632 return flags
[i
].value
;
3637 static void print_str_to_seq(struct trace_seq
*s
, const char *format
,
3638 int len_arg
, const char *str
)
3641 trace_seq_printf(s
, format
, len_arg
, str
);
3643 trace_seq_printf(s
, format
, str
);
3646 static void print_bitmask_to_seq(struct pevent
*pevent
,
3647 struct trace_seq
*s
, const char *format
,
3648 int len_arg
, const void *data
, int size
)
3650 int nr_bits
= size
* 8;
3651 int str_size
= (nr_bits
+ 3) / 4;
3659 * The kernel likes to put in commas every 32 bits, we
3662 str_size
+= (nr_bits
- 1) / 32;
3664 str
= malloc(str_size
+ 1);
3666 do_warning("%s: not enough memory!", __func__
);
3671 /* Start out with -2 for the two chars per byte */
3672 for (i
= str_size
- 2; i
>= 0; i
-= 2) {
3674 * data points to a bit mask of size bytes.
3675 * In the kernel, this is an array of long words, thus
3676 * endianess is very important.
3678 if (pevent
->file_bigendian
)
3679 index
= size
- (len
+ 1);
3683 snprintf(buf
, 3, "%02x", *((unsigned char *)data
+ index
));
3684 memcpy(str
+ i
, buf
, 2);
3686 if (!(len
& 3) && i
> 0) {
3693 trace_seq_printf(s
, format
, len_arg
, str
);
3695 trace_seq_printf(s
, format
, str
);
3700 static void print_str_arg(struct trace_seq
*s
, void *data
, int size
,
3701 struct event_format
*event
, const char *format
,
3702 int len_arg
, struct print_arg
*arg
)
3704 struct pevent
*pevent
= event
->pevent
;
3705 struct print_flag_sym
*flag
;
3706 struct format_field
*field
;
3707 struct printk_map
*printk
;
3708 unsigned long long val
, fval
;
3715 switch (arg
->type
) {
3720 print_str_to_seq(s
, format
, len_arg
, arg
->atom
.atom
);
3723 field
= arg
->field
.field
;
3725 field
= pevent_find_any_field(event
, arg
->field
.name
);
3727 str
= arg
->field
.name
;
3728 goto out_warning_field
;
3730 arg
->field
.field
= field
;
3732 /* Zero sized fields, mean the rest of the data */
3733 len
= field
->size
? : size
- field
->offset
;
3736 * Some events pass in pointers. If this is not an array
3737 * and the size is the same as long_size, assume that it
3740 if (!(field
->flags
& FIELD_IS_ARRAY
) &&
3741 field
->size
== pevent
->long_size
) {
3742 addr
= *(unsigned long *)(data
+ field
->offset
);
3743 /* Check if it matches a print format */
3744 printk
= find_printk(pevent
, addr
);
3746 trace_seq_puts(s
, printk
->printk
);
3748 trace_seq_printf(s
, "%lx", addr
);
3751 str
= malloc(len
+ 1);
3753 do_warning_event(event
, "%s: not enough memory!",
3757 memcpy(str
, data
+ field
->offset
, len
);
3759 print_str_to_seq(s
, format
, len_arg
, str
);
3763 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
3765 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
3766 fval
= eval_flag(flag
->value
);
3767 if (!val
&& !fval
) {
3768 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3771 if (fval
&& (val
& fval
) == fval
) {
3772 if (print
&& arg
->flags
.delim
)
3773 trace_seq_puts(s
, arg
->flags
.delim
);
3774 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3781 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
3782 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
3783 fval
= eval_flag(flag
->value
);
3785 print_str_to_seq(s
, format
, len_arg
, flag
->str
);
3791 if (arg
->hex
.field
->type
== PRINT_DYNAMIC_ARRAY
) {
3792 unsigned long offset
;
3793 offset
= pevent_read_number(pevent
,
3794 data
+ arg
->hex
.field
->dynarray
.field
->offset
,
3795 arg
->hex
.field
->dynarray
.field
->size
);
3796 hex
= data
+ (offset
& 0xffff);
3798 field
= arg
->hex
.field
->field
.field
;
3800 str
= arg
->hex
.field
->field
.name
;
3801 field
= pevent_find_any_field(event
, str
);
3803 goto out_warning_field
;
3804 arg
->hex
.field
->field
.field
= field
;
3806 hex
= data
+ field
->offset
;
3808 len
= eval_num_arg(data
, size
, event
, arg
->hex
.size
);
3809 for (i
= 0; i
< len
; i
++) {
3811 trace_seq_putc(s
, ' ');
3812 trace_seq_printf(s
, "%02x", hex
[i
]);
3816 case PRINT_INT_ARRAY
: {
3820 if (arg
->int_array
.field
->type
== PRINT_DYNAMIC_ARRAY
) {
3821 unsigned long offset
;
3822 struct format_field
*field
=
3823 arg
->int_array
.field
->dynarray
.field
;
3824 offset
= pevent_read_number(pevent
,
3825 data
+ field
->offset
,
3827 num
= data
+ (offset
& 0xffff);
3829 field
= arg
->int_array
.field
->field
.field
;
3831 str
= arg
->int_array
.field
->field
.name
;
3832 field
= pevent_find_any_field(event
, str
);
3834 goto out_warning_field
;
3835 arg
->int_array
.field
->field
.field
= field
;
3837 num
= data
+ field
->offset
;
3839 len
= eval_num_arg(data
, size
, event
, arg
->int_array
.count
);
3840 el_size
= eval_num_arg(data
, size
, event
,
3841 arg
->int_array
.el_size
);
3842 for (i
= 0; i
< len
; i
++) {
3844 trace_seq_putc(s
, ' ');
3847 trace_seq_printf(s
, "%u", *(uint8_t *)num
);
3848 } else if (el_size
== 2) {
3849 trace_seq_printf(s
, "%u", *(uint16_t *)num
);
3850 } else if (el_size
== 4) {
3851 trace_seq_printf(s
, "%u", *(uint32_t *)num
);
3852 } else if (el_size
== 8) {
3853 trace_seq_printf(s
, "%lu", *(uint64_t *)num
);
3855 trace_seq_printf(s
, "BAD SIZE:%d 0x%x",
3856 el_size
, *(uint8_t *)num
);
3866 case PRINT_STRING
: {
3869 if (arg
->string
.offset
== -1) {
3870 struct format_field
*f
;
3872 f
= pevent_find_any_field(event
, arg
->string
.string
);
3873 arg
->string
.offset
= f
->offset
;
3875 str_offset
= data2host4(pevent
, data
+ arg
->string
.offset
);
3876 str_offset
&= 0xffff;
3877 print_str_to_seq(s
, format
, len_arg
, ((char *)data
) + str_offset
);
3881 print_str_to_seq(s
, format
, len_arg
, arg
->string
.string
);
3883 case PRINT_BITMASK
: {
3887 if (arg
->bitmask
.offset
== -1) {
3888 struct format_field
*f
;
3890 f
= pevent_find_any_field(event
, arg
->bitmask
.bitmask
);
3891 arg
->bitmask
.offset
= f
->offset
;
3893 bitmask_offset
= data2host4(pevent
, data
+ arg
->bitmask
.offset
);
3894 bitmask_size
= bitmask_offset
>> 16;
3895 bitmask_offset
&= 0xffff;
3896 print_bitmask_to_seq(pevent
, s
, format
, len_arg
,
3897 data
+ bitmask_offset
, bitmask_size
);
3902 * The only op for string should be ? :
3904 if (arg
->op
.op
[0] != '?')
3906 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
3908 print_str_arg(s
, data
, size
, event
,
3909 format
, len_arg
, arg
->op
.right
->op
.left
);
3911 print_str_arg(s
, data
, size
, event
,
3912 format
, len_arg
, arg
->op
.right
->op
.right
);
3915 process_defined_func(s
, data
, size
, event
, arg
);
3925 do_warning_event(event
, "%s: field %s not found",
3926 __func__
, arg
->field
.name
);
3929 static unsigned long long
3930 process_defined_func(struct trace_seq
*s
, void *data
, int size
,
3931 struct event_format
*event
, struct print_arg
*arg
)
3933 struct pevent_function_handler
*func_handle
= arg
->func
.func
;
3934 struct pevent_func_params
*param
;
3935 unsigned long long *args
;
3936 unsigned long long ret
;
3937 struct print_arg
*farg
;
3938 struct trace_seq str
;
3940 struct save_str
*next
;
3942 } *strings
= NULL
, *string
;
3945 if (!func_handle
->nr_args
) {
3946 ret
= (*func_handle
->func
)(s
, NULL
);
3950 farg
= arg
->func
.args
;
3951 param
= func_handle
->params
;
3954 args
= malloc(sizeof(*args
) * func_handle
->nr_args
);
3958 for (i
= 0; i
< func_handle
->nr_args
; i
++) {
3959 switch (param
->type
) {
3960 case PEVENT_FUNC_ARG_INT
:
3961 case PEVENT_FUNC_ARG_LONG
:
3962 case PEVENT_FUNC_ARG_PTR
:
3963 args
[i
] = eval_num_arg(data
, size
, event
, farg
);
3965 case PEVENT_FUNC_ARG_STRING
:
3966 trace_seq_init(&str
);
3967 print_str_arg(&str
, data
, size
, event
, "%s", -1, farg
);
3968 trace_seq_terminate(&str
);
3969 string
= malloc(sizeof(*string
));
3971 do_warning_event(event
, "%s(%d): malloc str",
3972 __func__
, __LINE__
);
3975 string
->next
= strings
;
3976 string
->str
= strdup(str
.buffer
);
3979 do_warning_event(event
, "%s(%d): malloc str",
3980 __func__
, __LINE__
);
3983 args
[i
] = (uintptr_t)string
->str
;
3985 trace_seq_destroy(&str
);
3989 * Something went totally wrong, this is not
3990 * an input error, something in this code broke.
3992 do_warning_event(event
, "Unexpected end of arguments\n");
3996 param
= param
->next
;
3999 ret
= (*func_handle
->func
)(s
, args
);
4004 strings
= string
->next
;
4010 /* TBD : handle return type here */
4014 static void free_args(struct print_arg
*args
)
4016 struct print_arg
*next
;
4026 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event_format
*event
)
4028 struct pevent
*pevent
= event
->pevent
;
4029 struct format_field
*field
, *ip_field
;
4030 struct print_arg
*args
, *arg
, **next
;
4031 unsigned long long ip
, val
;
4036 field
= pevent
->bprint_buf_field
;
4037 ip_field
= pevent
->bprint_ip_field
;
4040 field
= pevent_find_field(event
, "buf");
4042 do_warning_event(event
, "can't find buffer field for binary printk");
4045 ip_field
= pevent_find_field(event
, "ip");
4047 do_warning_event(event
, "can't find ip field for binary printk");
4050 pevent
->bprint_buf_field
= field
;
4051 pevent
->bprint_ip_field
= ip_field
;
4054 ip
= pevent_read_number(pevent
, data
+ ip_field
->offset
, ip_field
->size
);
4057 * The first arg is the IP pointer.
4061 do_warning_event(event
, "%s(%d): not enough memory!",
4062 __func__
, __LINE__
);
4069 arg
->type
= PRINT_ATOM
;
4071 if (asprintf(&arg
->atom
.atom
, "%lld", ip
) < 0)
4074 /* skip the first "%pf: " */
4075 for (ptr
= fmt
+ 5, bptr
= data
+ field
->offset
;
4076 bptr
< data
+ size
&& *ptr
; ptr
++) {
4111 vsize
= pevent
->long_size
;
4125 /* the pointers are always 4 bytes aligned */
4126 bptr
= (void *)(((unsigned long)bptr
+ 3) &
4128 val
= pevent_read_number(pevent
, bptr
, vsize
);
4132 do_warning_event(event
, "%s(%d): not enough memory!",
4133 __func__
, __LINE__
);
4137 arg
->type
= PRINT_ATOM
;
4138 if (asprintf(&arg
->atom
.atom
, "%lld", val
) < 0) {
4145 * The '*' case means that an arg is used as the length.
4146 * We need to continue to figure out for what.
4155 do_warning_event(event
, "%s(%d): not enough memory!",
4156 __func__
, __LINE__
);
4160 arg
->type
= PRINT_BSTRING
;
4161 arg
->string
.string
= strdup(bptr
);
4162 if (!arg
->string
.string
)
4164 bptr
+= strlen(bptr
) + 1;
4181 get_bprint_format(void *data
, int size __maybe_unused
,
4182 struct event_format
*event
)
4184 struct pevent
*pevent
= event
->pevent
;
4185 unsigned long long addr
;
4186 struct format_field
*field
;
4187 struct printk_map
*printk
;
4190 field
= pevent
->bprint_fmt_field
;
4193 field
= pevent_find_field(event
, "fmt");
4195 do_warning_event(event
, "can't find format field for binary printk");
4198 pevent
->bprint_fmt_field
= field
;
4201 addr
= pevent_read_number(pevent
, data
+ field
->offset
, field
->size
);
4203 printk
= find_printk(pevent
, addr
);
4205 if (asprintf(&format
, "%%pf: (NO FORMAT FOUND at %llx)\n", addr
) < 0)
4210 if (asprintf(&format
, "%s: %s", "%pf", printk
->printk
) < 0)
4216 static void print_mac_arg(struct trace_seq
*s
, int mac
, void *data
, int size
,
4217 struct event_format
*event
, struct print_arg
*arg
)
4220 const char *fmt
= "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4222 if (arg
->type
== PRINT_FUNC
) {
4223 process_defined_func(s
, data
, size
, event
, arg
);
4227 if (arg
->type
!= PRINT_FIELD
) {
4228 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d",
4234 fmt
= "%.2x%.2x%.2x%.2x%.2x%.2x";
4235 if (!arg
->field
.field
) {
4237 pevent_find_any_field(event
, arg
->field
.name
);
4238 if (!arg
->field
.field
) {
4239 do_warning_event(event
, "%s: field %s not found",
4240 __func__
, arg
->field
.name
);
4244 if (arg
->field
.field
->size
!= 6) {
4245 trace_seq_printf(s
, "INVALIDMAC");
4248 buf
= data
+ arg
->field
.field
->offset
;
4249 trace_seq_printf(s
, fmt
, buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5]);
4252 static void print_ip4_addr(struct trace_seq
*s
, char i
, unsigned char *buf
)
4257 fmt
= "%03d.%03d.%03d.%03d";
4259 fmt
= "%d.%d.%d.%d";
4261 trace_seq_printf(s
, fmt
, buf
[0], buf
[1], buf
[2], buf
[3]);
4264 static inline bool ipv6_addr_v4mapped(const struct in6_addr
*a
)
4266 return ((unsigned long)(a
->s6_addr32
[0] | a
->s6_addr32
[1]) |
4267 (unsigned long)(a
->s6_addr32
[2] ^ htonl(0x0000ffff))) == 0UL;
4270 static inline bool ipv6_addr_is_isatap(const struct in6_addr
*addr
)
4272 return (addr
->s6_addr32
[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4275 static void print_ip6c_addr(struct trace_seq
*s
, unsigned char *addr
)
4278 unsigned char zerolength
[8];
4283 bool needcolon
= false;
4285 struct in6_addr in6
;
4287 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
4289 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
4291 memset(zerolength
, 0, sizeof(zerolength
));
4298 /* find position of longest 0 run */
4299 for (i
= 0; i
< range
; i
++) {
4300 for (j
= i
; j
< range
; j
++) {
4301 if (in6
.s6_addr16
[j
] != 0)
4306 for (i
= 0; i
< range
; i
++) {
4307 if (zerolength
[i
] > longest
) {
4308 longest
= zerolength
[i
];
4312 if (longest
== 1) /* don't compress a single 0 */
4316 for (i
= 0; i
< range
; i
++) {
4317 if (i
== colonpos
) {
4318 if (needcolon
|| i
== 0)
4319 trace_seq_printf(s
, ":");
4320 trace_seq_printf(s
, ":");
4326 trace_seq_printf(s
, ":");
4329 /* hex u16 without leading 0s */
4330 word
= ntohs(in6
.s6_addr16
[i
]);
4334 trace_seq_printf(s
, "%x%02x", hi
, lo
);
4336 trace_seq_printf(s
, "%x", lo
);
4343 trace_seq_printf(s
, ":");
4344 print_ip4_addr(s
, 'I', &in6
.s6_addr
[12]);
4350 static void print_ip6_addr(struct trace_seq
*s
, char i
, unsigned char *buf
)
4354 for (j
= 0; j
< 16; j
+= 2) {
4355 trace_seq_printf(s
, "%02x%02x", buf
[j
], buf
[j
+1]);
4356 if (i
== 'I' && j
< 14)
4357 trace_seq_printf(s
, ":");
4362 * %pi4 print an IPv4 address with leading zeros
4363 * %pI4 print an IPv4 address without leading zeros
4364 * %pi6 print an IPv6 address without colons
4365 * %pI6 print an IPv6 address with colons
4366 * %pI6c print an IPv6 address in compressed form with colons
4367 * %pISpc print an IP address based on sockaddr; p adds port.
4369 static int print_ipv4_arg(struct trace_seq
*s
, const char *ptr
, char i
,
4370 void *data
, int size
, struct event_format
*event
,
4371 struct print_arg
*arg
)
4375 if (arg
->type
== PRINT_FUNC
) {
4376 process_defined_func(s
, data
, size
, event
, arg
);
4380 if (arg
->type
!= PRINT_FIELD
) {
4381 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d", arg
->type
);
4385 if (!arg
->field
.field
) {
4387 pevent_find_any_field(event
, arg
->field
.name
);
4388 if (!arg
->field
.field
) {
4389 do_warning("%s: field %s not found",
4390 __func__
, arg
->field
.name
);
4395 buf
= data
+ arg
->field
.field
->offset
;
4397 if (arg
->field
.field
->size
!= 4) {
4398 trace_seq_printf(s
, "INVALIDIPv4");
4401 print_ip4_addr(s
, i
, buf
);
4406 static int print_ipv6_arg(struct trace_seq
*s
, const char *ptr
, char i
,
4407 void *data
, int size
, struct event_format
*event
,
4408 struct print_arg
*arg
)
4415 if (i
== 'I' && *ptr
== 'c') {
4421 if (arg
->type
== PRINT_FUNC
) {
4422 process_defined_func(s
, data
, size
, event
, arg
);
4426 if (arg
->type
!= PRINT_FIELD
) {
4427 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d", arg
->type
);
4431 if (!arg
->field
.field
) {
4433 pevent_find_any_field(event
, arg
->field
.name
);
4434 if (!arg
->field
.field
) {
4435 do_warning("%s: field %s not found",
4436 __func__
, arg
->field
.name
);
4441 buf
= data
+ arg
->field
.field
->offset
;
4443 if (arg
->field
.field
->size
!= 16) {
4444 trace_seq_printf(s
, "INVALIDIPv6");
4449 print_ip6c_addr(s
, buf
);
4451 print_ip6_addr(s
, i
, buf
);
4456 static int print_ipsa_arg(struct trace_seq
*s
, const char *ptr
, char i
,
4457 void *data
, int size
, struct event_format
*event
,
4458 struct print_arg
*arg
)
4460 char have_c
= 0, have_p
= 0;
4462 struct sockaddr_storage
*sa
;
4479 if (arg
->type
== PRINT_FUNC
) {
4480 process_defined_func(s
, data
, size
, event
, arg
);
4484 if (arg
->type
!= PRINT_FIELD
) {
4485 trace_seq_printf(s
, "ARG TYPE NOT FIELD BUT %d", arg
->type
);
4489 if (!arg
->field
.field
) {
4491 pevent_find_any_field(event
, arg
->field
.name
);
4492 if (!arg
->field
.field
) {
4493 do_warning("%s: field %s not found",
4494 __func__
, arg
->field
.name
);
4499 sa
= (struct sockaddr_storage
*) (data
+ arg
->field
.field
->offset
);
4501 if (sa
->ss_family
== AF_INET
) {
4502 struct sockaddr_in
*sa4
= (struct sockaddr_in
*) sa
;
4504 if (arg
->field
.field
->size
< sizeof(struct sockaddr_in
)) {
4505 trace_seq_printf(s
, "INVALIDIPv4");
4509 print_ip4_addr(s
, i
, (unsigned char *) &sa4
->sin_addr
);
4511 trace_seq_printf(s
, ":%d", ntohs(sa4
->sin_port
));
4514 } else if (sa
->ss_family
== AF_INET6
) {
4515 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*) sa
;
4517 if (arg
->field
.field
->size
< sizeof(struct sockaddr_in6
)) {
4518 trace_seq_printf(s
, "INVALIDIPv6");
4523 trace_seq_printf(s
, "[");
4525 buf
= (unsigned char *) &sa6
->sin6_addr
;
4527 print_ip6c_addr(s
, buf
);
4529 print_ip6_addr(s
, i
, buf
);
4532 trace_seq_printf(s
, "]:%d", ntohs(sa6
->sin6_port
));
4538 static int print_ip_arg(struct trace_seq
*s
, const char *ptr
,
4539 void *data
, int size
, struct event_format
*event
,
4540 struct print_arg
*arg
)
4542 char i
= *ptr
; /* 'i' or 'I' */
4555 rc
+= print_ipv4_arg(s
, ptr
, i
, data
, size
, event
, arg
);
4558 rc
+= print_ipv6_arg(s
, ptr
, i
, data
, size
, event
, arg
);
4561 rc
+= print_ipsa_arg(s
, ptr
, i
, data
, size
, event
, arg
);
4570 static int is_printable_array(char *p
, unsigned int len
)
4574 for (i
= 0; i
< len
&& p
[i
]; i
++)
4575 if (!isprint(p
[i
]) && !isspace(p
[i
]))
4580 static void print_event_fields(struct trace_seq
*s
, void *data
,
4581 int size __maybe_unused
,
4582 struct event_format
*event
)
4584 struct format_field
*field
;
4585 unsigned long long val
;
4586 unsigned int offset
, len
, i
;
4588 field
= event
->format
.fields
;
4590 trace_seq_printf(s
, " %s=", field
->name
);
4591 if (field
->flags
& FIELD_IS_ARRAY
) {
4592 offset
= field
->offset
;
4594 if (field
->flags
& FIELD_IS_DYNAMIC
) {
4595 val
= pevent_read_number(event
->pevent
, data
+ offset
, len
);
4600 if (field
->flags
& FIELD_IS_STRING
&&
4601 is_printable_array(data
+ offset
, len
)) {
4602 trace_seq_printf(s
, "%s", (char *)data
+ offset
);
4604 trace_seq_puts(s
, "ARRAY[");
4605 for (i
= 0; i
< len
; i
++) {
4607 trace_seq_puts(s
, ", ");
4608 trace_seq_printf(s
, "%02x",
4609 *((unsigned char *)data
+ offset
+ i
));
4611 trace_seq_putc(s
, ']');
4612 field
->flags
&= ~FIELD_IS_STRING
;
4615 val
= pevent_read_number(event
->pevent
, data
+ field
->offset
,
4617 if (field
->flags
& FIELD_IS_POINTER
) {
4618 trace_seq_printf(s
, "0x%llx", val
);
4619 } else if (field
->flags
& FIELD_IS_SIGNED
) {
4620 switch (field
->size
) {
4623 * If field is long then print it in hex.
4624 * A long usually stores pointers.
4626 if (field
->flags
& FIELD_IS_LONG
)
4627 trace_seq_printf(s
, "0x%x", (int)val
);
4629 trace_seq_printf(s
, "%d", (int)val
);
4632 trace_seq_printf(s
, "%2d", (short)val
);
4635 trace_seq_printf(s
, "%1d", (char)val
);
4638 trace_seq_printf(s
, "%lld", val
);
4641 if (field
->flags
& FIELD_IS_LONG
)
4642 trace_seq_printf(s
, "0x%llx", val
);
4644 trace_seq_printf(s
, "%llu", val
);
4647 field
= field
->next
;
4651 static void pretty_print(struct trace_seq
*s
, void *data
, int size
, struct event_format
*event
)
4653 struct pevent
*pevent
= event
->pevent
;
4654 struct print_fmt
*print_fmt
= &event
->print_fmt
;
4655 struct print_arg
*arg
= print_fmt
->args
;
4656 struct print_arg
*args
= NULL
;
4657 const char *ptr
= print_fmt
->format
;
4658 unsigned long long val
;
4659 struct func_map
*func
;
4660 const char *saveptr
;
4662 char *bprint_fmt
= NULL
;
4670 if (event
->flags
& EVENT_FL_FAILED
) {
4671 trace_seq_printf(s
, "[FAILED TO PARSE]");
4672 print_event_fields(s
, data
, size
, event
);
4676 if (event
->flags
& EVENT_FL_ISBPRINT
) {
4677 bprint_fmt
= get_bprint_format(data
, size
, event
);
4678 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
4683 for (; *ptr
; ptr
++) {
4689 trace_seq_putc(s
, '\n');
4692 trace_seq_putc(s
, '\t');
4695 trace_seq_putc(s
, '\r');
4698 trace_seq_putc(s
, '\\');
4701 trace_seq_putc(s
, *ptr
);
4705 } else if (*ptr
== '%') {
4713 trace_seq_putc(s
, '%');
4716 /* FIXME: need to handle properly */
4728 /* The argument is the length. */
4730 do_warning_event(event
, "no argument match");
4731 event
->flags
|= EVENT_FL_FAILED
;
4734 len_arg
= eval_num_arg(data
, size
, event
, arg
);
4744 if (pevent
->long_size
== 4)
4749 if (*(ptr
+1) == 'F' ||
4753 } else if (*(ptr
+1) == 'M' || *(ptr
+1) == 'm') {
4754 print_mac_arg(s
, *(ptr
+1), data
, size
, event
, arg
);
4758 } else if (*(ptr
+1) == 'I' || *(ptr
+1) == 'i') {
4761 n
= print_ip_arg(s
, ptr
+1, data
, size
, event
, arg
);
4776 do_warning_event(event
, "no argument match");
4777 event
->flags
|= EVENT_FL_FAILED
;
4781 len
= ((unsigned long)ptr
+ 1) -
4782 (unsigned long)saveptr
;
4784 /* should never happen */
4786 do_warning_event(event
, "bad format!");
4787 event
->flags
|= EVENT_FL_FAILED
;
4791 memcpy(format
, saveptr
, len
);
4794 val
= eval_num_arg(data
, size
, event
, arg
);
4798 func
= find_func(pevent
, val
);
4800 trace_seq_puts(s
, func
->func
);
4801 if (show_func
== 'F')
4808 if (pevent
->long_size
== 8 && ls
&&
4809 sizeof(long) != 8) {
4813 /* make %l into %ll */
4814 p
= strchr(format
, 'l');
4816 memmove(p
+1, p
, strlen(p
)+1);
4817 else if (strcmp(format
, "%p") == 0)
4818 strcpy(format
, "0x%llx");
4823 trace_seq_printf(s
, format
, len_arg
, (char)val
);
4825 trace_seq_printf(s
, format
, (char)val
);
4829 trace_seq_printf(s
, format
, len_arg
, (short)val
);
4831 trace_seq_printf(s
, format
, (short)val
);
4835 trace_seq_printf(s
, format
, len_arg
, (int)val
);
4837 trace_seq_printf(s
, format
, (int)val
);
4841 trace_seq_printf(s
, format
, len_arg
, (long)val
);
4843 trace_seq_printf(s
, format
, (long)val
);
4847 trace_seq_printf(s
, format
, len_arg
,
4850 trace_seq_printf(s
, format
, (long long)val
);
4853 do_warning_event(event
, "bad count (%d)", ls
);
4854 event
->flags
|= EVENT_FL_FAILED
;
4859 do_warning_event(event
, "no matching argument");
4860 event
->flags
|= EVENT_FL_FAILED
;
4864 len
= ((unsigned long)ptr
+ 1) -
4865 (unsigned long)saveptr
;
4867 /* should never happen */
4869 do_warning_event(event
, "bad format!");
4870 event
->flags
|= EVENT_FL_FAILED
;
4874 memcpy(format
, saveptr
, len
);
4878 /* Use helper trace_seq */
4880 print_str_arg(&p
, data
, size
, event
,
4881 format
, len_arg
, arg
);
4882 trace_seq_terminate(&p
);
4883 trace_seq_puts(s
, p
.buffer
);
4884 trace_seq_destroy(&p
);
4888 trace_seq_printf(s
, ">%c<", *ptr
);
4892 trace_seq_putc(s
, *ptr
);
4895 if (event
->flags
& EVENT_FL_FAILED
) {
4897 trace_seq_printf(s
, "[FAILED TO PARSE]");
4907 * pevent_data_lat_fmt - parse the data for the latency format
4908 * @pevent: a handle to the pevent
4909 * @s: the trace_seq to write to
4910 * @record: the record to read from
4912 * This parses out the Latency format (interrupts disabled,
4913 * need rescheduling, in hard/soft interrupt, preempt count
4914 * and lock depth) and places it into the trace_seq.
4916 void pevent_data_lat_fmt(struct pevent
*pevent
,
4917 struct trace_seq
*s
, struct pevent_record
*record
)
4919 static int check_lock_depth
= 1;
4920 static int check_migrate_disable
= 1;
4921 static int lock_depth_exists
;
4922 static int migrate_disable_exists
;
4923 unsigned int lat_flags
;
4926 int migrate_disable
;
4929 void *data
= record
->data
;
4931 lat_flags
= parse_common_flags(pevent
, data
);
4932 pc
= parse_common_pc(pevent
, data
);
4933 /* lock_depth may not always exist */
4934 if (lock_depth_exists
)
4935 lock_depth
= parse_common_lock_depth(pevent
, data
);
4936 else if (check_lock_depth
) {
4937 lock_depth
= parse_common_lock_depth(pevent
, data
);
4939 check_lock_depth
= 0;
4941 lock_depth_exists
= 1;
4944 /* migrate_disable may not always exist */
4945 if (migrate_disable_exists
)
4946 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4947 else if (check_migrate_disable
) {
4948 migrate_disable
= parse_common_migrate_disable(pevent
, data
);
4949 if (migrate_disable
< 0)
4950 check_migrate_disable
= 0;
4952 migrate_disable_exists
= 1;
4955 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
4956 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
4958 trace_seq_printf(s
, "%c%c%c",
4959 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
4960 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
4962 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
4964 (hardirq
&& softirq
) ? 'H' :
4965 hardirq
? 'h' : softirq
? 's' : '.');
4968 trace_seq_printf(s
, "%x", pc
);
4970 trace_seq_putc(s
, '.');
4972 if (migrate_disable_exists
) {
4973 if (migrate_disable
< 0)
4974 trace_seq_putc(s
, '.');
4976 trace_seq_printf(s
, "%d", migrate_disable
);
4979 if (lock_depth_exists
) {
4981 trace_seq_putc(s
, '.');
4983 trace_seq_printf(s
, "%d", lock_depth
);
4986 trace_seq_terminate(s
);
4990 * pevent_data_type - parse out the given event type
4991 * @pevent: a handle to the pevent
4992 * @rec: the record to read from
4994 * This returns the event id from the @rec.
4996 int pevent_data_type(struct pevent
*pevent
, struct pevent_record
*rec
)
4998 return trace_parse_common_type(pevent
, rec
->data
);
5002 * pevent_data_event_from_type - find the event by a given type
5003 * @pevent: a handle to the pevent
5004 * @type: the type of the event.
5006 * This returns the event form a given @type;
5008 struct event_format
*pevent_data_event_from_type(struct pevent
*pevent
, int type
)
5010 return pevent_find_event(pevent
, type
);
5014 * pevent_data_pid - parse the PID from raw data
5015 * @pevent: a handle to the pevent
5016 * @rec: the record to parse
5018 * This returns the PID from a raw data.
5020 int pevent_data_pid(struct pevent
*pevent
, struct pevent_record
*rec
)
5022 return parse_common_pid(pevent
, rec
->data
);
5026 * pevent_data_comm_from_pid - return the command line from PID
5027 * @pevent: a handle to the pevent
5028 * @pid: the PID of the task to search for
5030 * This returns a pointer to the command line that has the given
5033 const char *pevent_data_comm_from_pid(struct pevent
*pevent
, int pid
)
5037 comm
= find_cmdline(pevent
, pid
);
5041 static struct cmdline
*
5042 pid_from_cmdlist(struct pevent
*pevent
, const char *comm
, struct cmdline
*next
)
5044 struct cmdline_list
*cmdlist
= (struct cmdline_list
*)next
;
5047 cmdlist
= cmdlist
->next
;
5049 cmdlist
= pevent
->cmdlist
;
5051 while (cmdlist
&& strcmp(cmdlist
->comm
, comm
) != 0)
5052 cmdlist
= cmdlist
->next
;
5054 return (struct cmdline
*)cmdlist
;
5058 * pevent_data_pid_from_comm - return the pid from a given comm
5059 * @pevent: a handle to the pevent
5060 * @comm: the cmdline to find the pid from
5061 * @next: the cmdline structure to find the next comm
5063 * This returns the cmdline structure that holds a pid for a given
5064 * comm, or NULL if none found. As there may be more than one pid for
5065 * a given comm, the result of this call can be passed back into
5066 * a recurring call in the @next paramater, and then it will find the
5068 * Also, it does a linear seach, so it may be slow.
5070 struct cmdline
*pevent_data_pid_from_comm(struct pevent
*pevent
, const char *comm
,
5071 struct cmdline
*next
)
5073 struct cmdline
*cmdline
;
5076 * If the cmdlines have not been converted yet, then use
5079 if (!pevent
->cmdlines
)
5080 return pid_from_cmdlist(pevent
, comm
, next
);
5084 * The next pointer could have been still from
5085 * a previous call before cmdlines were created
5087 if (next
< pevent
->cmdlines
||
5088 next
>= pevent
->cmdlines
+ pevent
->cmdline_count
)
5095 cmdline
= pevent
->cmdlines
;
5097 while (cmdline
< pevent
->cmdlines
+ pevent
->cmdline_count
) {
5098 if (strcmp(cmdline
->comm
, comm
) == 0)
5106 * pevent_cmdline_pid - return the pid associated to a given cmdline
5107 * @cmdline: The cmdline structure to get the pid from
5109 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5112 int pevent_cmdline_pid(struct pevent
*pevent
, struct cmdline
*cmdline
)
5114 struct cmdline_list
*cmdlist
= (struct cmdline_list
*)cmdline
;
5120 * If cmdlines have not been created yet, or cmdline is
5121 * not part of the array, then treat it as a cmdlist instead.
5123 if (!pevent
->cmdlines
||
5124 cmdline
< pevent
->cmdlines
||
5125 cmdline
>= pevent
->cmdlines
+ pevent
->cmdline_count
)
5126 return cmdlist
->pid
;
5128 return cmdline
->pid
;
5132 * pevent_data_comm_from_pid - parse the data into the print format
5133 * @s: the trace_seq to write to
5134 * @event: the handle to the event
5135 * @record: the record to read from
5137 * This parses the raw @data using the given @event information and
5138 * writes the print format into the trace_seq.
5140 void pevent_event_info(struct trace_seq
*s
, struct event_format
*event
,
5141 struct pevent_record
*record
)
5143 int print_pretty
= 1;
5145 if (event
->pevent
->print_raw
|| (event
->flags
& EVENT_FL_PRINTRAW
))
5146 print_event_fields(s
, record
->data
, record
->size
, event
);
5149 if (event
->handler
&& !(event
->flags
& EVENT_FL_NOHANDLE
))
5150 print_pretty
= event
->handler(s
, record
, event
,
5154 pretty_print(s
, record
->data
, record
->size
, event
);
5157 trace_seq_terminate(s
);
5160 static bool is_timestamp_in_us(char *trace_clock
, bool use_trace_clock
)
5162 if (!use_trace_clock
)
5165 if (!strcmp(trace_clock
, "local") || !strcmp(trace_clock
, "global")
5166 || !strcmp(trace_clock
, "uptime") || !strcmp(trace_clock
, "perf"))
5169 /* trace_clock is setting in tsc or counter mode */
5173 void pevent_print_event(struct pevent
*pevent
, struct trace_seq
*s
,
5174 struct pevent_record
*record
, bool use_trace_clock
)
5176 static const char *spaces
= " "; /* 20 spaces */
5177 struct event_format
*event
;
5179 unsigned long usecs
;
5180 unsigned long nsecs
;
5182 void *data
= record
->data
;
5187 bool use_usec_format
;
5189 use_usec_format
= is_timestamp_in_us(pevent
->trace_clock
,
5191 if (use_usec_format
) {
5192 secs
= record
->ts
/ NSECS_PER_SEC
;
5193 nsecs
= record
->ts
- secs
* NSECS_PER_SEC
;
5196 if (record
->size
< 0) {
5197 do_warning("ug! negative record size %d", record
->size
);
5201 type
= trace_parse_common_type(pevent
, data
);
5203 event
= pevent_find_event(pevent
, type
);
5205 do_warning("ug! no event found for type %d", type
);
5209 pid
= parse_common_pid(pevent
, data
);
5210 comm
= find_cmdline(pevent
, pid
);
5212 if (pevent
->latency_format
) {
5213 trace_seq_printf(s
, "%8.8s-%-5d %3d",
5214 comm
, pid
, record
->cpu
);
5215 pevent_data_lat_fmt(pevent
, s
, record
);
5217 trace_seq_printf(s
, "%16s-%-5d [%03d]", comm
, pid
, record
->cpu
);
5219 if (use_usec_format
) {
5220 if (pevent
->flags
& PEVENT_NSEC_OUTPUT
) {
5224 usecs
= (nsecs
+ 500) / NSECS_PER_USEC
;
5228 trace_seq_printf(s
, " %5lu.%0*lu: %s: ",
5229 secs
, p
, usecs
, event
->name
);
5231 trace_seq_printf(s
, " %12llu: %s: ",
5232 record
->ts
, event
->name
);
5234 /* Space out the event names evenly. */
5235 len
= strlen(event
->name
);
5237 trace_seq_printf(s
, "%.*s", 20 - len
, spaces
);
5239 pevent_event_info(s
, event
, record
);
5242 static int events_id_cmp(const void *a
, const void *b
)
5244 struct event_format
* const * ea
= a
;
5245 struct event_format
* const * eb
= b
;
5247 if ((*ea
)->id
< (*eb
)->id
)
5250 if ((*ea
)->id
> (*eb
)->id
)
5256 static int events_name_cmp(const void *a
, const void *b
)
5258 struct event_format
* const * ea
= a
;
5259 struct event_format
* const * eb
= b
;
5262 res
= strcmp((*ea
)->name
, (*eb
)->name
);
5266 res
= strcmp((*ea
)->system
, (*eb
)->system
);
5270 return events_id_cmp(a
, b
);
5273 static int events_system_cmp(const void *a
, const void *b
)
5275 struct event_format
* const * ea
= a
;
5276 struct event_format
* const * eb
= b
;
5279 res
= strcmp((*ea
)->system
, (*eb
)->system
);
5283 res
= strcmp((*ea
)->name
, (*eb
)->name
);
5287 return events_id_cmp(a
, b
);
5290 struct event_format
**pevent_list_events(struct pevent
*pevent
, enum event_sort_type sort_type
)
5292 struct event_format
**events
;
5293 int (*sort
)(const void *a
, const void *b
);
5295 events
= pevent
->sort_events
;
5297 if (events
&& pevent
->last_type
== sort_type
)
5301 events
= malloc(sizeof(*events
) * (pevent
->nr_events
+ 1));
5305 memcpy(events
, pevent
->events
, sizeof(*events
) * pevent
->nr_events
);
5306 events
[pevent
->nr_events
] = NULL
;
5308 pevent
->sort_events
= events
;
5310 /* the internal events are sorted by id */
5311 if (sort_type
== EVENT_SORT_ID
) {
5312 pevent
->last_type
= sort_type
;
5317 switch (sort_type
) {
5319 sort
= events_id_cmp
;
5321 case EVENT_SORT_NAME
:
5322 sort
= events_name_cmp
;
5324 case EVENT_SORT_SYSTEM
:
5325 sort
= events_system_cmp
;
5331 qsort(events
, pevent
->nr_events
, sizeof(*events
), sort
);
5332 pevent
->last_type
= sort_type
;
5337 static struct format_field
**
5338 get_event_fields(const char *type
, const char *name
,
5339 int count
, struct format_field
*list
)
5341 struct format_field
**fields
;
5342 struct format_field
*field
;
5345 fields
= malloc(sizeof(*fields
) * (count
+ 1));
5349 for (field
= list
; field
; field
= field
->next
) {
5350 fields
[i
++] = field
;
5351 if (i
== count
+ 1) {
5352 do_warning("event %s has more %s fields than specified",
5360 do_warning("event %s has less %s fields than specified",
5369 * pevent_event_common_fields - return a list of common fields for an event
5370 * @event: the event to return the common fields of.
5372 * Returns an allocated array of fields. The last item in the array is NULL.
5373 * The array must be freed with free().
5375 struct format_field
**pevent_event_common_fields(struct event_format
*event
)
5377 return get_event_fields("common", event
->name
,
5378 event
->format
.nr_common
,
5379 event
->format
.common_fields
);
5383 * pevent_event_fields - return a list of event specific fields for an event
5384 * @event: the event to return the fields of.
5386 * Returns an allocated array of fields. The last item in the array is NULL.
5387 * The array must be freed with free().
5389 struct format_field
**pevent_event_fields(struct event_format
*event
)
5391 return get_event_fields("event", event
->name
,
5392 event
->format
.nr_fields
,
5393 event
->format
.fields
);
5396 static void print_fields(struct trace_seq
*s
, struct print_flag_sym
*field
)
5398 trace_seq_printf(s
, "{ %s, %s }", field
->value
, field
->str
);
5400 trace_seq_puts(s
, ", ");
5401 print_fields(s
, field
->next
);
5406 static void print_args(struct print_arg
*args
)
5408 int print_paren
= 1;
5411 switch (args
->type
) {
5416 printf("%s", args
->atom
.atom
);
5419 printf("REC->%s", args
->field
.name
);
5422 printf("__print_flags(");
5423 print_args(args
->flags
.field
);
5424 printf(", %s, ", args
->flags
.delim
);
5426 print_fields(&s
, args
->flags
.flags
);
5427 trace_seq_do_printf(&s
);
5428 trace_seq_destroy(&s
);
5432 printf("__print_symbolic(");
5433 print_args(args
->symbol
.field
);
5436 print_fields(&s
, args
->symbol
.symbols
);
5437 trace_seq_do_printf(&s
);
5438 trace_seq_destroy(&s
);
5442 printf("__print_hex(");
5443 print_args(args
->hex
.field
);
5445 print_args(args
->hex
.size
);
5448 case PRINT_INT_ARRAY
:
5449 printf("__print_array(");
5450 print_args(args
->int_array
.field
);
5452 print_args(args
->int_array
.count
);
5454 print_args(args
->int_array
.el_size
);
5459 printf("__get_str(%s)", args
->string
.string
);
5462 printf("__get_bitmask(%s)", args
->bitmask
.bitmask
);
5465 printf("(%s)", args
->typecast
.type
);
5466 print_args(args
->typecast
.item
);
5469 if (strcmp(args
->op
.op
, ":") == 0)
5473 print_args(args
->op
.left
);
5474 printf(" %s ", args
->op
.op
);
5475 print_args(args
->op
.right
);
5480 /* we should warn... */
5485 print_args(args
->next
);
5489 static void parse_header_field(const char *field
,
5490 int *offset
, int *size
, int mandatory
)
5492 unsigned long long save_input_buf_ptr
;
5493 unsigned long long save_input_buf_siz
;
5497 save_input_buf_ptr
= input_buf_ptr
;
5498 save_input_buf_siz
= input_buf_siz
;
5500 if (read_expected(EVENT_ITEM
, "field") < 0)
5502 if (read_expected(EVENT_OP
, ":") < 0)
5506 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
5511 * If this is not a mandatory field, then test it first.
5514 if (read_expected(EVENT_ITEM
, field
) < 0)
5517 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
5519 if (strcmp(token
, field
) != 0)
5524 if (read_expected(EVENT_OP
, ";") < 0)
5526 if (read_expected(EVENT_ITEM
, "offset") < 0)
5528 if (read_expected(EVENT_OP
, ":") < 0)
5530 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
5532 *offset
= atoi(token
);
5534 if (read_expected(EVENT_OP
, ";") < 0)
5536 if (read_expected(EVENT_ITEM
, "size") < 0)
5538 if (read_expected(EVENT_OP
, ":") < 0)
5540 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
5542 *size
= atoi(token
);
5544 if (read_expected(EVENT_OP
, ";") < 0)
5546 type
= read_token(&token
);
5547 if (type
!= EVENT_NEWLINE
) {
5548 /* newer versions of the kernel have a "signed" type */
5549 if (type
!= EVENT_ITEM
)
5552 if (strcmp(token
, "signed") != 0)
5557 if (read_expected(EVENT_OP
, ":") < 0)
5560 if (read_expect_type(EVENT_ITEM
, &token
))
5564 if (read_expected(EVENT_OP
, ";") < 0)
5567 if (read_expect_type(EVENT_NEWLINE
, &token
))
5575 input_buf_ptr
= save_input_buf_ptr
;
5576 input_buf_siz
= save_input_buf_siz
;
5583 * pevent_parse_header_page - parse the data stored in the header page
5584 * @pevent: the handle to the pevent
5585 * @buf: the buffer storing the header page format string
5586 * @size: the size of @buf
5587 * @long_size: the long size to use if there is no header
5589 * This parses the header page format for information on the
5590 * ring buffer used. The @buf should be copied from
5592 * /sys/kernel/debug/tracing/events/header_page
5594 int pevent_parse_header_page(struct pevent
*pevent
, char *buf
, unsigned long size
,
5601 * Old kernels did not have header page info.
5602 * Sorry but we just use what we find here in user space.
5604 pevent
->header_page_ts_size
= sizeof(long long);
5605 pevent
->header_page_size_size
= long_size
;
5606 pevent
->header_page_data_offset
= sizeof(long long) + long_size
;
5607 pevent
->old_format
= 1;
5610 init_input_buf(buf
, size
);
5612 parse_header_field("timestamp", &pevent
->header_page_ts_offset
,
5613 &pevent
->header_page_ts_size
, 1);
5614 parse_header_field("commit", &pevent
->header_page_size_offset
,
5615 &pevent
->header_page_size_size
, 1);
5616 parse_header_field("overwrite", &pevent
->header_page_overwrite
,
5618 parse_header_field("data", &pevent
->header_page_data_offset
,
5619 &pevent
->header_page_data_size
, 1);
5624 static int event_matches(struct event_format
*event
,
5625 int id
, const char *sys_name
,
5626 const char *event_name
)
5628 if (id
>= 0 && id
!= event
->id
)
5631 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
5634 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
5640 static void free_handler(struct event_handler
*handle
)
5642 free((void *)handle
->sys_name
);
5643 free((void *)handle
->event_name
);
5647 static int find_event_handle(struct pevent
*pevent
, struct event_format
*event
)
5649 struct event_handler
*handle
, **next
;
5651 for (next
= &pevent
->handlers
; *next
;
5652 next
= &(*next
)->next
) {
5654 if (event_matches(event
, handle
->id
,
5656 handle
->event_name
))
5663 pr_stat("overriding event (%d) %s:%s with new print handler",
5664 event
->id
, event
->system
, event
->name
);
5666 event
->handler
= handle
->func
;
5667 event
->context
= handle
->context
;
5669 *next
= handle
->next
;
5670 free_handler(handle
);
5676 * __pevent_parse_format - parse the event format
5677 * @buf: the buffer storing the event format string
5678 * @size: the size of @buf
5679 * @sys: the system the event belongs to
5681 * This parses the event format and creates an event structure
5682 * to quickly parse raw data for a given event.
5684 * These files currently come from:
5686 * /sys/kernel/debug/tracing/events/.../.../format
5688 enum pevent_errno
__pevent_parse_format(struct event_format
**eventp
,
5689 struct pevent
*pevent
, const char *buf
,
5690 unsigned long size
, const char *sys
)
5692 struct event_format
*event
;
5695 init_input_buf(buf
, size
);
5697 *eventp
= event
= alloc_event();
5699 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5701 event
->name
= event_read_name();
5704 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5705 goto event_alloc_failed
;
5708 if (strcmp(sys
, "ftrace") == 0) {
5709 event
->flags
|= EVENT_FL_ISFTRACE
;
5711 if (strcmp(event
->name
, "bprint") == 0)
5712 event
->flags
|= EVENT_FL_ISBPRINT
;
5715 event
->id
= event_read_id();
5716 if (event
->id
< 0) {
5717 ret
= PEVENT_ERRNO__READ_ID_FAILED
;
5719 * This isn't an allocation error actually.
5720 * But as the ID is critical, just bail out.
5722 goto event_alloc_failed
;
5725 event
->system
= strdup(sys
);
5726 if (!event
->system
) {
5727 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5728 goto event_alloc_failed
;
5731 /* Add pevent to event so that it can be referenced */
5732 event
->pevent
= pevent
;
5734 ret
= event_read_format(event
);
5736 ret
= PEVENT_ERRNO__READ_FORMAT_FAILED
;
5737 goto event_parse_failed
;
5741 * If the event has an override, don't print warnings if the event
5742 * print format fails to parse.
5744 if (pevent
&& find_event_handle(pevent
, event
))
5747 ret
= event_read_print(event
);
5751 ret
= PEVENT_ERRNO__READ_PRINT_FAILED
;
5752 goto event_parse_failed
;
5755 if (!ret
&& (event
->flags
& EVENT_FL_ISFTRACE
)) {
5756 struct format_field
*field
;
5757 struct print_arg
*arg
, **list
;
5759 /* old ftrace had no args */
5760 list
= &event
->print_fmt
.args
;
5761 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
5764 event
->flags
|= EVENT_FL_FAILED
;
5765 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
;
5767 arg
->type
= PRINT_FIELD
;
5768 arg
->field
.name
= strdup(field
->name
);
5769 if (!arg
->field
.name
) {
5770 event
->flags
|= EVENT_FL_FAILED
;
5772 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED
;
5774 arg
->field
.field
= field
;
5784 event
->flags
|= EVENT_FL_FAILED
;
5788 free(event
->system
);
5795 static enum pevent_errno
5796 __pevent_parse_event(struct pevent
*pevent
,
5797 struct event_format
**eventp
,
5798 const char *buf
, unsigned long size
,
5801 int ret
= __pevent_parse_format(eventp
, pevent
, buf
, size
, sys
);
5802 struct event_format
*event
= *eventp
;
5807 if (pevent
&& add_event(pevent
, event
)) {
5808 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
5809 goto event_add_failed
;
5812 #define PRINT_ARGS 0
5813 if (PRINT_ARGS
&& event
->print_fmt
.args
)
5814 print_args(event
->print_fmt
.args
);
5819 pevent_free_format(event
);
5824 * pevent_parse_format - parse the event format
5825 * @pevent: the handle to the pevent
5826 * @eventp: returned format
5827 * @buf: the buffer storing the event format string
5828 * @size: the size of @buf
5829 * @sys: the system the event belongs to
5831 * This parses the event format and creates an event structure
5832 * to quickly parse raw data for a given event.
5834 * These files currently come from:
5836 * /sys/kernel/debug/tracing/events/.../.../format
5838 enum pevent_errno
pevent_parse_format(struct pevent
*pevent
,
5839 struct event_format
**eventp
,
5841 unsigned long size
, const char *sys
)
5843 return __pevent_parse_event(pevent
, eventp
, buf
, size
, sys
);
5847 * pevent_parse_event - parse the event format
5848 * @pevent: the handle to the pevent
5849 * @buf: the buffer storing the event format string
5850 * @size: the size of @buf
5851 * @sys: the system the event belongs to
5853 * This parses the event format and creates an event structure
5854 * to quickly parse raw data for a given event.
5856 * These files currently come from:
5858 * /sys/kernel/debug/tracing/events/.../.../format
5860 enum pevent_errno
pevent_parse_event(struct pevent
*pevent
, const char *buf
,
5861 unsigned long size
, const char *sys
)
5863 struct event_format
*event
= NULL
;
5864 return __pevent_parse_event(pevent
, &event
, buf
, size
, sys
);
5868 #define _PE(code, str) str
5869 static const char * const pevent_error_str
[] = {
5874 int pevent_strerror(struct pevent
*pevent __maybe_unused
,
5875 enum pevent_errno errnum
, char *buf
, size_t buflen
)
5881 msg
= strerror_r(errnum
, buf
, buflen
);
5883 size_t len
= strlen(msg
);
5884 memcpy(buf
, msg
, min(buflen
- 1, len
));
5885 *(buf
+ min(buflen
- 1, len
)) = '\0';
5890 if (errnum
<= __PEVENT_ERRNO__START
||
5891 errnum
>= __PEVENT_ERRNO__END
)
5894 idx
= errnum
- __PEVENT_ERRNO__START
- 1;
5895 msg
= pevent_error_str
[idx
];
5896 snprintf(buf
, buflen
, "%s", msg
);
5901 int get_field_val(struct trace_seq
*s
, struct format_field
*field
,
5902 const char *name
, struct pevent_record
*record
,
5903 unsigned long long *val
, int err
)
5907 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
5911 if (pevent_read_number_field(field
, record
->data
, val
)) {
5913 trace_seq_printf(s
, " %s=INVALID", name
);
5921 * pevent_get_field_raw - return the raw pointer into the data field
5922 * @s: The seq to print to on error
5923 * @event: the event that the field is for
5924 * @name: The name of the field
5925 * @record: The record with the field name.
5926 * @len: place to store the field length.
5927 * @err: print default error if failed.
5929 * Returns a pointer into record->data of the field and places
5930 * the length of the field in @len.
5932 * On failure, it returns NULL.
5934 void *pevent_get_field_raw(struct trace_seq
*s
, struct event_format
*event
,
5935 const char *name
, struct pevent_record
*record
,
5938 struct format_field
*field
;
5939 void *data
= record
->data
;
5946 field
= pevent_find_field(event
, name
);
5950 trace_seq_printf(s
, "<CANT FIND FIELD %s>", name
);
5954 /* Allow @len to be NULL */
5958 offset
= field
->offset
;
5959 if (field
->flags
& FIELD_IS_DYNAMIC
) {
5960 offset
= pevent_read_number(event
->pevent
,
5961 data
+ offset
, field
->size
);
5962 *len
= offset
>> 16;
5967 return data
+ offset
;
5971 * pevent_get_field_val - find a field and return its value
5972 * @s: The seq to print to on error
5973 * @event: the event that the field is for
5974 * @name: The name of the field
5975 * @record: The record with the field name.
5976 * @val: place to store the value of the field.
5977 * @err: print default error if failed.
5979 * Returns 0 on success -1 on field not found.
5981 int pevent_get_field_val(struct trace_seq
*s
, struct event_format
*event
,
5982 const char *name
, struct pevent_record
*record
,
5983 unsigned long long *val
, int err
)
5985 struct format_field
*field
;
5990 field
= pevent_find_field(event
, name
);
5992 return get_field_val(s
, field
, name
, record
, val
, err
);
5996 * pevent_get_common_field_val - find a common field and return its value
5997 * @s: The seq to print to on error
5998 * @event: the event that the field is for
5999 * @name: The name of the field
6000 * @record: The record with the field name.
6001 * @val: place to store the value of the field.
6002 * @err: print default error if failed.
6004 * Returns 0 on success -1 on field not found.
6006 int pevent_get_common_field_val(struct trace_seq
*s
, struct event_format
*event
,
6007 const char *name
, struct pevent_record
*record
,
6008 unsigned long long *val
, int err
)
6010 struct format_field
*field
;
6015 field
= pevent_find_common_field(event
, name
);
6017 return get_field_val(s
, field
, name
, record
, val
, err
);
6021 * pevent_get_any_field_val - find a any field and return its value
6022 * @s: The seq to print to on error
6023 * @event: the event that the field is for
6024 * @name: The name of the field
6025 * @record: The record with the field name.
6026 * @val: place to store the value of the field.
6027 * @err: print default error if failed.
6029 * Returns 0 on success -1 on field not found.
6031 int pevent_get_any_field_val(struct trace_seq
*s
, struct event_format
*event
,
6032 const char *name
, struct pevent_record
*record
,
6033 unsigned long long *val
, int err
)
6035 struct format_field
*field
;
6040 field
= pevent_find_any_field(event
, name
);
6042 return get_field_val(s
, field
, name
, record
, val
, err
);
6046 * pevent_print_num_field - print a field and a format
6047 * @s: The seq to print to
6048 * @fmt: The printf format to print the field with.
6049 * @event: the event that the field is for
6050 * @name: The name of the field
6051 * @record: The record with the field name.
6052 * @err: print default error if failed.
6054 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6056 int pevent_print_num_field(struct trace_seq
*s
, const char *fmt
,
6057 struct event_format
*event
, const char *name
,
6058 struct pevent_record
*record
, int err
)
6060 struct format_field
*field
= pevent_find_field(event
, name
);
6061 unsigned long long val
;
6066 if (pevent_read_number_field(field
, record
->data
, &val
))
6069 return trace_seq_printf(s
, fmt
, val
);
6073 trace_seq_printf(s
, "CAN'T FIND FIELD \"%s\"", name
);
6078 * pevent_print_func_field - print a field and a format for function pointers
6079 * @s: The seq to print to
6080 * @fmt: The printf format to print the field with.
6081 * @event: the event that the field is for
6082 * @name: The name of the field
6083 * @record: The record with the field name.
6084 * @err: print default error if failed.
6086 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6088 int pevent_print_func_field(struct trace_seq
*s
, const char *fmt
,
6089 struct event_format
*event
, const char *name
,
6090 struct pevent_record
*record
, int err
)
6092 struct format_field
*field
= pevent_find_field(event
, name
);
6093 struct pevent
*pevent
= event
->pevent
;
6094 unsigned long long val
;
6095 struct func_map
*func
;
6101 if (pevent_read_number_field(field
, record
->data
, &val
))
6104 func
= find_func(pevent
, val
);
6107 snprintf(tmp
, 128, "%s/0x%llx", func
->func
, func
->addr
- val
);
6109 sprintf(tmp
, "0x%08llx", val
);
6111 return trace_seq_printf(s
, fmt
, tmp
);
6115 trace_seq_printf(s
, "CAN'T FIND FIELD \"%s\"", name
);
6119 static void free_func_handle(struct pevent_function_handler
*func
)
6121 struct pevent_func_params
*params
;
6125 while (func
->params
) {
6126 params
= func
->params
;
6127 func
->params
= params
->next
;
6135 * pevent_register_print_function - register a helper function
6136 * @pevent: the handle to the pevent
6137 * @func: the function to process the helper function
6138 * @ret_type: the return type of the helper function
6139 * @name: the name of the helper function
6140 * @parameters: A list of enum pevent_func_arg_type
6142 * Some events may have helper functions in the print format arguments.
6143 * This allows a plugin to dynamically create a way to process one
6144 * of these functions.
6146 * The @parameters is a variable list of pevent_func_arg_type enums that
6147 * must end with PEVENT_FUNC_ARG_VOID.
6149 int pevent_register_print_function(struct pevent
*pevent
,
6150 pevent_func_handler func
,
6151 enum pevent_func_arg_type ret_type
,
6154 struct pevent_function_handler
*func_handle
;
6155 struct pevent_func_params
**next_param
;
6156 struct pevent_func_params
*param
;
6157 enum pevent_func_arg_type type
;
6161 func_handle
= find_func_handler(pevent
, name
);
6164 * This is most like caused by the users own
6165 * plugins updating the function. This overrides the
6168 pr_stat("override of function helper '%s'", name
);
6169 remove_func_handler(pevent
, name
);
6172 func_handle
= calloc(1, sizeof(*func_handle
));
6174 do_warning("Failed to allocate function handler");
6175 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
6178 func_handle
->ret_type
= ret_type
;
6179 func_handle
->name
= strdup(name
);
6180 func_handle
->func
= func
;
6181 if (!func_handle
->name
) {
6182 do_warning("Failed to allocate function name");
6184 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
6187 next_param
= &(func_handle
->params
);
6190 type
= va_arg(ap
, enum pevent_func_arg_type
);
6191 if (type
== PEVENT_FUNC_ARG_VOID
)
6194 if (type
>= PEVENT_FUNC_ARG_MAX_TYPES
) {
6195 do_warning("Invalid argument type %d", type
);
6196 ret
= PEVENT_ERRNO__INVALID_ARG_TYPE
;
6200 param
= malloc(sizeof(*param
));
6202 do_warning("Failed to allocate function param");
6203 ret
= PEVENT_ERRNO__MEM_ALLOC_FAILED
;
6209 *next_param
= param
;
6210 next_param
= &(param
->next
);
6212 func_handle
->nr_args
++;
6216 func_handle
->next
= pevent
->func_handlers
;
6217 pevent
->func_handlers
= func_handle
;
6222 free_func_handle(func_handle
);
6227 * pevent_unregister_print_function - unregister a helper function
6228 * @pevent: the handle to the pevent
6229 * @func: the function to process the helper function
6230 * @name: the name of the helper function
6232 * This function removes existing print handler for function @name.
6234 * Returns 0 if the handler was removed successully, -1 otherwise.
6236 int pevent_unregister_print_function(struct pevent
*pevent
,
6237 pevent_func_handler func
, char *name
)
6239 struct pevent_function_handler
*func_handle
;
6241 func_handle
= find_func_handler(pevent
, name
);
6242 if (func_handle
&& func_handle
->func
== func
) {
6243 remove_func_handler(pevent
, name
);
6249 static struct event_format
*pevent_search_event(struct pevent
*pevent
, int id
,
6250 const char *sys_name
,
6251 const char *event_name
)
6253 struct event_format
*event
;
6257 event
= pevent_find_event(pevent
, id
);
6260 if (event_name
&& (strcmp(event_name
, event
->name
) != 0))
6262 if (sys_name
&& (strcmp(sys_name
, event
->system
) != 0))
6265 event
= pevent_find_event_by_name(pevent
, sys_name
, event_name
);
6273 * pevent_register_event_handler - register a way to parse an event
6274 * @pevent: the handle to the pevent
6275 * @id: the id of the event to register
6276 * @sys_name: the system name the event belongs to
6277 * @event_name: the name of the event
6278 * @func: the function to call to parse the event information
6279 * @context: the data to be passed to @func
6281 * This function allows a developer to override the parsing of
6282 * a given event. If for some reason the default print format
6283 * is not sufficient, this function will register a function
6284 * for an event to be used to parse the data instead.
6286 * If @id is >= 0, then it is used to find the event.
6287 * else @sys_name and @event_name are used.
6289 int pevent_register_event_handler(struct pevent
*pevent
, int id
,
6290 const char *sys_name
, const char *event_name
,
6291 pevent_event_handler_func func
, void *context
)
6293 struct event_format
*event
;
6294 struct event_handler
*handle
;
6296 event
= pevent_search_event(pevent
, id
, sys_name
, event_name
);
6300 pr_stat("overriding event (%d) %s:%s with new print handler",
6301 event
->id
, event
->system
, event
->name
);
6303 event
->handler
= func
;
6304 event
->context
= context
;
6308 /* Save for later use. */
6309 handle
= calloc(1, sizeof(*handle
));
6311 do_warning("Failed to allocate event handler");
6312 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
6317 handle
->event_name
= strdup(event_name
);
6319 handle
->sys_name
= strdup(sys_name
);
6321 if ((event_name
&& !handle
->event_name
) ||
6322 (sys_name
&& !handle
->sys_name
)) {
6323 do_warning("Failed to allocate event/sys name");
6324 free((void *)handle
->event_name
);
6325 free((void *)handle
->sys_name
);
6327 return PEVENT_ERRNO__MEM_ALLOC_FAILED
;
6330 handle
->func
= func
;
6331 handle
->next
= pevent
->handlers
;
6332 pevent
->handlers
= handle
;
6333 handle
->context
= context
;
6338 static int handle_matches(struct event_handler
*handler
, int id
,
6339 const char *sys_name
, const char *event_name
,
6340 pevent_event_handler_func func
, void *context
)
6342 if (id
>= 0 && id
!= handler
->id
)
6345 if (event_name
&& (strcmp(event_name
, handler
->event_name
) != 0))
6348 if (sys_name
&& (strcmp(sys_name
, handler
->sys_name
) != 0))
6351 if (func
!= handler
->func
|| context
!= handler
->context
)
6358 * pevent_unregister_event_handler - unregister an existing event handler
6359 * @pevent: the handle to the pevent
6360 * @id: the id of the event to unregister
6361 * @sys_name: the system name the handler belongs to
6362 * @event_name: the name of the event handler
6363 * @func: the function to call to parse the event information
6364 * @context: the data to be passed to @func
6366 * This function removes existing event handler (parser).
6368 * If @id is >= 0, then it is used to find the event.
6369 * else @sys_name and @event_name are used.
6371 * Returns 0 if handler was removed successfully, -1 if event was not found.
6373 int pevent_unregister_event_handler(struct pevent
*pevent
, int id
,
6374 const char *sys_name
, const char *event_name
,
6375 pevent_event_handler_func func
, void *context
)
6377 struct event_format
*event
;
6378 struct event_handler
*handle
;
6379 struct event_handler
**next
;
6381 event
= pevent_search_event(pevent
, id
, sys_name
, event_name
);
6385 if (event
->handler
== func
&& event
->context
== context
) {
6386 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6387 event
->id
, event
->system
, event
->name
);
6389 event
->handler
= NULL
;
6390 event
->context
= NULL
;
6395 for (next
= &pevent
->handlers
; *next
; next
= &(*next
)->next
) {
6397 if (handle_matches(handle
, id
, sys_name
, event_name
,
6405 *next
= handle
->next
;
6406 free_handler(handle
);
6412 * pevent_alloc - create a pevent handle
6414 struct pevent
*pevent_alloc(void)
6416 struct pevent
*pevent
= calloc(1, sizeof(*pevent
));
6419 pevent
->ref_count
= 1;
6424 void pevent_ref(struct pevent
*pevent
)
6426 pevent
->ref_count
++;
6429 void pevent_free_format_field(struct format_field
*field
)
6436 static void free_format_fields(struct format_field
*field
)
6438 struct format_field
*next
;
6442 pevent_free_format_field(field
);
6447 static void free_formats(struct format
*format
)
6449 free_format_fields(format
->common_fields
);
6450 free_format_fields(format
->fields
);
6453 void pevent_free_format(struct event_format
*event
)
6456 free(event
->system
);
6458 free_formats(&event
->format
);
6460 free(event
->print_fmt
.format
);
6461 free_args(event
->print_fmt
.args
);
6467 * pevent_free - free a pevent handle
6468 * @pevent: the pevent handle to free
6470 void pevent_free(struct pevent
*pevent
)
6472 struct cmdline_list
*cmdlist
, *cmdnext
;
6473 struct func_list
*funclist
, *funcnext
;
6474 struct printk_list
*printklist
, *printknext
;
6475 struct pevent_function_handler
*func_handler
;
6476 struct event_handler
*handle
;
6482 cmdlist
= pevent
->cmdlist
;
6483 funclist
= pevent
->funclist
;
6484 printklist
= pevent
->printklist
;
6486 pevent
->ref_count
--;
6487 if (pevent
->ref_count
)
6490 if (pevent
->cmdlines
) {
6491 for (i
= 0; i
< pevent
->cmdline_count
; i
++)
6492 free(pevent
->cmdlines
[i
].comm
);
6493 free(pevent
->cmdlines
);
6497 cmdnext
= cmdlist
->next
;
6498 free(cmdlist
->comm
);
6503 if (pevent
->func_map
) {
6504 for (i
= 0; i
< (int)pevent
->func_count
; i
++) {
6505 free(pevent
->func_map
[i
].func
);
6506 free(pevent
->func_map
[i
].mod
);
6508 free(pevent
->func_map
);
6512 funcnext
= funclist
->next
;
6513 free(funclist
->func
);
6514 free(funclist
->mod
);
6516 funclist
= funcnext
;
6519 while (pevent
->func_handlers
) {
6520 func_handler
= pevent
->func_handlers
;
6521 pevent
->func_handlers
= func_handler
->next
;
6522 free_func_handle(func_handler
);
6525 if (pevent
->printk_map
) {
6526 for (i
= 0; i
< (int)pevent
->printk_count
; i
++)
6527 free(pevent
->printk_map
[i
].printk
);
6528 free(pevent
->printk_map
);
6531 while (printklist
) {
6532 printknext
= printklist
->next
;
6533 free(printklist
->printk
);
6535 printklist
= printknext
;
6538 for (i
= 0; i
< pevent
->nr_events
; i
++)
6539 pevent_free_format(pevent
->events
[i
]);
6541 while (pevent
->handlers
) {
6542 handle
= pevent
->handlers
;
6543 pevent
->handlers
= handle
->next
;
6544 free_handler(handle
);
6547 free(pevent
->trace_clock
);
6548 free(pevent
->events
);
6549 free(pevent
->sort_events
);
6554 void pevent_unref(struct pevent
*pevent
)
6556 pevent_free(pevent
);