2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
32 #include "trace-event.h"
34 int header_page_ts_offset
;
35 int header_page_ts_size
;
36 int header_page_size_offset
;
37 int header_page_size_size
;
38 int header_page_overwrite_offset
;
39 int header_page_overwrite_size
;
40 int header_page_data_offset
;
41 int header_page_data_size
;
45 static char *input_buf
;
46 static unsigned long long input_buf_ptr
;
47 static unsigned long long input_buf_siz
;
51 static int is_flag_field
;
52 static int is_symbolic_field
;
54 static struct format_field
*
55 find_any_field(struct event
*event
, const char *name
);
57 static void init_input_buf(char *buf
, unsigned long long size
)
69 static struct cmdline
*cmdlines
;
70 static int cmdline_count
;
72 static int cmdline_cmp(const void *a
, const void *b
)
74 const struct cmdline
*ca
= a
;
75 const struct cmdline
*cb
= b
;
77 if (ca
->pid
< cb
->pid
)
79 if (ca
->pid
> cb
->pid
)
85 void parse_cmdlines(char *file
, int size __unused
)
88 struct cmdline_list
*next
;
91 } *list
= NULL
, *item
;
96 line
= strtok_r(file
, "\n", &next
);
98 item
= malloc_or_die(sizeof(*item
));
99 sscanf(line
, "%d %as", &item
->pid
,
100 (float *)(void *)&item
->comm
); /* workaround gcc warning */
103 line
= strtok_r(NULL
, "\n", &next
);
107 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
111 cmdlines
[i
].pid
= list
->pid
;
112 cmdlines
[i
].comm
= list
->comm
;
119 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
122 static struct func_map
{
123 unsigned long long addr
;
127 static unsigned int func_count
;
129 static int func_cmp(const void *a
, const void *b
)
131 const struct func_map
*fa
= a
;
132 const struct func_map
*fb
= b
;
134 if (fa
->addr
< fb
->addr
)
136 if (fa
->addr
> fb
->addr
)
142 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
145 struct func_list
*next
;
146 unsigned long long addr
;
149 } *list
= NULL
, *item
;
157 line
= strtok_r(file
, "\n", &next
);
159 item
= malloc_or_die(sizeof(*item
));
161 ret
= sscanf(line
, "%as %c %as\t[%as",
162 (float *)(void *)&addr_str
, /* workaround gcc warning */
164 (float *)(void *)&item
->func
,
165 (float *)(void *)&item
->mod
);
166 item
->addr
= strtoull(addr_str
, NULL
, 16);
169 /* truncate the extra ']' */
171 item
->mod
[strlen(item
->mod
) - 1] = 0;
176 line
= strtok_r(NULL
, "\n", &next
);
180 func_list
= malloc_or_die(sizeof(*func_list
) * (func_count
+ 1));
184 func_list
[i
].func
= list
->func
;
185 func_list
[i
].addr
= list
->addr
;
186 func_list
[i
].mod
= list
->mod
;
193 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
196 * Add a special record at the end.
198 func_list
[func_count
].func
= NULL
;
199 func_list
[func_count
].addr
= 0;
200 func_list
[func_count
].mod
= NULL
;
204 * We are searching for a record in between, not an exact
207 static int func_bcmp(const void *a
, const void *b
)
209 const struct func_map
*fa
= a
;
210 const struct func_map
*fb
= b
;
212 if ((fa
->addr
== fb
->addr
) ||
214 (fa
->addr
> fb
->addr
&&
215 fa
->addr
< (fb
+1)->addr
))
218 if (fa
->addr
< fb
->addr
)
224 static struct func_map
*find_func(unsigned long long addr
)
226 struct func_map
*func
;
231 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
237 void print_funcs(void)
241 for (i
= 0; i
< (int)func_count
; i
++) {
245 if (func_list
[i
].mod
)
246 printf(" [%s]\n", func_list
[i
].mod
);
252 static struct printk_map
{
253 unsigned long long addr
;
256 static unsigned int printk_count
;
258 static int printk_cmp(const void *a
, const void *b
)
260 const struct func_map
*fa
= a
;
261 const struct func_map
*fb
= b
;
263 if (fa
->addr
< fb
->addr
)
265 if (fa
->addr
> fb
->addr
)
271 static struct printk_map
*find_printk(unsigned long long addr
)
273 struct printk_map
*printk
;
274 struct printk_map key
;
278 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
284 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
287 struct printk_list
*next
;
288 unsigned long long addr
;
290 } *list
= NULL
, *item
;
296 line
= strtok_r(file
, "\n", &next
);
298 addr_str
= strsep(&line
, ":");
300 warning("error parsing print strings");
303 item
= malloc_or_die(sizeof(*item
));
304 item
->addr
= strtoull(addr_str
, NULL
, 16);
305 /* fmt still has a space, skip it */
306 item
->printk
= strdup(line
+1);
309 line
= strtok_r(NULL
, "\n", &next
);
313 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
317 printk_list
[i
].printk
= list
->printk
;
318 printk_list
[i
].addr
= list
->addr
;
325 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
328 void print_printk(void)
332 for (i
= 0; i
< (int)printk_count
; i
++) {
333 printf("%016llx %s\n",
335 printk_list
[i
].printk
);
339 static struct event
*alloc_event(void)
343 event
= malloc_or_die(sizeof(*event
));
344 memset(event
, 0, sizeof(*event
));
361 static struct event
*event_list
;
363 static void add_event(struct event
*event
)
365 event
->next
= event_list
;
369 static int event_item_type(enum event_type type
)
372 case EVENT_ITEM
... EVENT_SQUOTE
:
374 case EVENT_ERROR
... EVENT_DELIM
:
380 static void free_arg(struct print_arg
*arg
)
388 free(arg
->atom
.atom
);
391 case PRINT_FIELD
... PRINT_OP
:
400 static enum event_type
get_type(int ch
)
403 return EVENT_NEWLINE
;
406 if (isalnum(ch
) || ch
== '_')
414 if (ch
== '(' || ch
== ')' || ch
== ',')
420 static int __read_char(void)
422 if (input_buf_ptr
>= input_buf_siz
)
425 return input_buf
[input_buf_ptr
++];
428 static int __peek_char(void)
430 if (input_buf_ptr
>= input_buf_siz
)
433 return input_buf
[input_buf_ptr
];
436 static enum event_type
__read_token(char **tok
)
439 int ch
, last_ch
, quote_ch
, next_ch
;
442 enum event_type type
;
452 if (type
== EVENT_NONE
)
460 *tok
= malloc_or_die(2);
468 next_ch
= __peek_char();
469 if (next_ch
== '>') {
470 buf
[i
++] = __read_char();
483 buf
[i
++] = __read_char();
495 default: /* what should we do instead? */
505 buf
[i
++] = __read_char();
510 /* don't keep quotes */
515 if (i
== (BUFSIZ
- 1)) {
518 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
533 /* the '\' '\' will cancel itself */
534 if (ch
== '\\' && last_ch
== '\\')
536 } while (ch
!= quote_ch
|| last_ch
== '\\');
537 /* remove the last quote */
541 case EVENT_ERROR
... EVENT_SPACE
:
547 while (get_type(__peek_char()) == type
) {
548 if (i
== (BUFSIZ
- 1)) {
551 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
570 *tok
= realloc(*tok
, tok_size
+ i
);
582 static void free_token(char *tok
)
588 static enum event_type
read_token(char **tok
)
590 enum event_type type
;
593 type
= __read_token(tok
);
594 if (type
!= EVENT_SPACE
)
605 static enum event_type
read_token_item(char **tok
)
607 enum event_type type
;
610 type
= __read_token(tok
);
611 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
621 static int test_type(enum event_type type
, enum event_type expect
)
623 if (type
!= expect
) {
624 warning("Error: expected type %d but read %d",
631 static int __test_type_token(enum event_type type
, char *token
,
632 enum event_type expect
, const char *expect_tok
,
635 if (type
!= expect
) {
637 warning("Error: expected type %d but read %d",
642 if (strcmp(token
, expect_tok
) != 0) {
644 warning("Error: expected '%s' but read '%s'",
651 static int test_type_token(enum event_type type
, char *token
,
652 enum event_type expect
, const char *expect_tok
)
654 return __test_type_token(type
, token
, expect
, expect_tok
, true);
657 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
659 enum event_type type
;
662 type
= read_token(tok
);
664 type
= read_token_item(tok
);
665 return test_type(type
, expect
);
668 static int read_expect_type(enum event_type expect
, char **tok
)
670 return __read_expect_type(expect
, tok
, 1);
673 static int __read_expected(enum event_type expect
, const char *str
,
674 int newline_ok
, bool warn
)
676 enum event_type type
;
681 type
= read_token(&token
);
683 type
= read_token_item(&token
);
685 ret
= __test_type_token(type
, token
, expect
, str
, warn
);
692 static int read_expected(enum event_type expect
, const char *str
)
694 return __read_expected(expect
, str
, 1, true);
697 static int read_expected_item(enum event_type expect
, const char *str
)
699 return __read_expected(expect
, str
, 0, true);
702 static char *event_read_name(void)
706 if (read_expected(EVENT_ITEM
, "name") < 0)
709 if (read_expected(EVENT_OP
, ":") < 0)
712 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
722 static int event_read_id(void)
727 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
730 if (read_expected(EVENT_OP
, ":") < 0)
733 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
736 id
= strtoul(token
, NULL
, 0);
745 static int field_is_string(struct format_field
*field
)
747 if ((field
->flags
& FIELD_IS_ARRAY
) &&
748 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
749 !strstr(field
->type
, "s8")))
755 static int field_is_dynamic(struct format_field
*field
)
757 if (!strncmp(field
->type
, "__data_loc", 10))
763 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
765 struct format_field
*field
= NULL
;
766 enum event_type type
;
772 type
= read_token(&token
);
773 if (type
== EVENT_NEWLINE
) {
780 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
784 type
= read_token(&token
);
786 * The ftrace fields may still use the "special" name.
789 if (event
->flags
& EVENT_FL_ISFTRACE
&&
790 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
792 type
= read_token(&token
);
795 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
798 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
803 field
= malloc_or_die(sizeof(*field
));
804 memset(field
, 0, sizeof(*field
));
806 /* read the rest of the type */
808 type
= read_token(&token
);
809 if (type
== EVENT_ITEM
||
810 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
812 * Some of the ftrace fields are broken and have
813 * an illegal "." in them.
815 (event
->flags
& EVENT_FL_ISFTRACE
&&
816 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
818 if (strcmp(token
, "*") == 0)
819 field
->flags
|= FIELD_IS_POINTER
;
822 field
->type
= realloc(field
->type
,
823 strlen(field
->type
) +
824 strlen(last_token
) + 2);
825 strcat(field
->type
, " ");
826 strcat(field
->type
, last_token
);
828 field
->type
= last_token
;
837 die("no type found");
840 field
->name
= last_token
;
842 if (test_type(type
, EVENT_OP
))
845 if (strcmp(token
, "[") == 0) {
846 enum event_type last_type
= type
;
847 char *brackets
= token
;
850 field
->flags
|= FIELD_IS_ARRAY
;
852 type
= read_token(&token
);
853 while (strcmp(token
, "]") != 0) {
854 if (last_type
== EVENT_ITEM
&&
861 brackets
= realloc(brackets
,
863 strlen(token
) + len
);
865 strcat(brackets
, " ");
866 strcat(brackets
, token
);
868 type
= read_token(&token
);
869 if (type
== EVENT_NONE
) {
870 die("failed to find token");
877 brackets
= realloc(brackets
, strlen(brackets
) + 2);
878 strcat(brackets
, "]");
880 /* add brackets to type */
882 type
= read_token(&token
);
884 * If the next token is not an OP, then it is of
885 * the format: type [] item;
887 if (type
== EVENT_ITEM
) {
888 field
->type
= realloc(field
->type
,
889 strlen(field
->type
) +
890 strlen(field
->name
) +
891 strlen(brackets
) + 2);
892 strcat(field
->type
, " ");
893 strcat(field
->type
, field
->name
);
894 free_token(field
->name
);
895 strcat(field
->type
, brackets
);
897 type
= read_token(&token
);
899 field
->type
= realloc(field
->type
,
900 strlen(field
->type
) +
901 strlen(brackets
) + 1);
902 strcat(field
->type
, brackets
);
907 if (field_is_string(field
)) {
908 field
->flags
|= FIELD_IS_STRING
;
909 if (field_is_dynamic(field
))
910 field
->flags
|= FIELD_IS_DYNAMIC
;
913 if (test_type_token(type
, token
, EVENT_OP
, ";"))
917 if (read_expected(EVENT_ITEM
, "offset") < 0)
920 if (read_expected(EVENT_OP
, ":") < 0)
923 if (read_expect_type(EVENT_ITEM
, &token
))
925 field
->offset
= strtoul(token
, NULL
, 0);
928 if (read_expected(EVENT_OP
, ";") < 0)
931 if (read_expected(EVENT_ITEM
, "size") < 0)
934 if (read_expected(EVENT_OP
, ":") < 0)
937 if (read_expect_type(EVENT_ITEM
, &token
))
939 field
->size
= strtoul(token
, NULL
, 0);
942 if (read_expected(EVENT_OP
, ";") < 0)
945 type
= read_token(&token
);
946 if (type
!= EVENT_NEWLINE
) {
947 /* newer versions of the kernel have a "signed" type */
948 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
953 if (read_expected(EVENT_OP
, ":") < 0)
956 if (read_expect_type(EVENT_ITEM
, &token
))
959 if (strtoul(token
, NULL
, 0))
960 field
->flags
|= FIELD_IS_SIGNED
;
963 if (read_expected(EVENT_OP
, ";") < 0)
966 if (read_expect_type(EVENT_NEWLINE
, &token
))
973 fields
= &field
->next
;
987 static int event_read_format(struct event
*event
)
992 if (read_expected_item(EVENT_ITEM
, "format") < 0)
995 if (read_expected(EVENT_OP
, ":") < 0)
998 if (read_expect_type(EVENT_NEWLINE
, &token
))
1002 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1005 event
->format
.nr_common
= ret
;
1007 ret
= event_read_fields(event
, &event
->format
.fields
);
1010 event
->format
.nr_fields
= ret
;
1020 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1021 char **tok
, enum event_type type
);
1023 static enum event_type
1024 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1026 enum event_type type
;
1029 type
= read_token(&token
);
1032 return process_arg_token(event
, arg
, tok
, type
);
1035 static enum event_type
1036 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1038 struct print_arg
*arg
, *left
, *right
;
1039 enum event_type type
;
1042 arg
= malloc_or_die(sizeof(*arg
));
1043 memset(arg
, 0, sizeof(*arg
));
1045 left
= malloc_or_die(sizeof(*left
));
1047 right
= malloc_or_die(sizeof(*right
));
1049 arg
->type
= PRINT_OP
;
1050 arg
->op
.left
= left
;
1051 arg
->op
.right
= right
;
1054 type
= process_arg(event
, left
, &token
);
1055 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1060 type
= process_arg(event
, right
, &token
);
1062 top
->op
.right
= arg
;
1075 static enum event_type
1076 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1078 struct print_arg
*arg
;
1079 enum event_type type
;
1082 arg
= malloc_or_die(sizeof(*arg
));
1083 memset(arg
, 0, sizeof(*arg
));
1086 type
= process_arg(event
, arg
, &token
);
1087 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1090 top
->op
.right
= arg
;
1093 type
= read_token_item(&token
);
1104 static int get_op_prio(char *op
)
1115 /* '>>' and '<<' are 8 */
1119 /* '==' and '!=' are 10 */
1129 die("unknown op '%c'", op
[0]);
1133 if (strcmp(op
, "++") == 0 ||
1134 strcmp(op
, "--") == 0) {
1136 } else if (strcmp(op
, ">>") == 0 ||
1137 strcmp(op
, "<<") == 0) {
1139 } else if (strcmp(op
, ">=") == 0 ||
1140 strcmp(op
, "<=") == 0) {
1142 } else if (strcmp(op
, "==") == 0 ||
1143 strcmp(op
, "!=") == 0) {
1145 } else if (strcmp(op
, "&&") == 0) {
1147 } else if (strcmp(op
, "||") == 0) {
1150 die("unknown op '%s'", op
);
1156 static void set_op_prio(struct print_arg
*arg
)
1159 /* single ops are the greatest */
1160 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1165 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1168 static enum event_type
1169 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1171 struct print_arg
*left
, *right
= NULL
;
1172 enum event_type type
;
1175 /* the op is passed in via tok */
1178 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1179 /* handle single op */
1181 die("bad op token %s", token
);
1190 die("bad op token %s", token
);
1194 /* make an empty left */
1195 left
= malloc_or_die(sizeof(*left
));
1196 left
->type
= PRINT_NULL
;
1197 arg
->op
.left
= left
;
1199 right
= malloc_or_die(sizeof(*right
));
1200 arg
->op
.right
= right
;
1202 type
= process_arg(event
, right
, tok
);
1204 } else if (strcmp(token
, "?") == 0) {
1206 left
= malloc_or_die(sizeof(*left
));
1207 /* copy the top arg to the left */
1210 arg
->type
= PRINT_OP
;
1212 arg
->op
.left
= left
;
1215 type
= process_cond(event
, arg
, tok
);
1217 } else if (strcmp(token
, ">>") == 0 ||
1218 strcmp(token
, "<<") == 0 ||
1219 strcmp(token
, "&") == 0 ||
1220 strcmp(token
, "|") == 0 ||
1221 strcmp(token
, "&&") == 0 ||
1222 strcmp(token
, "||") == 0 ||
1223 strcmp(token
, "-") == 0 ||
1224 strcmp(token
, "+") == 0 ||
1225 strcmp(token
, "*") == 0 ||
1226 strcmp(token
, "^") == 0 ||
1227 strcmp(token
, "/") == 0 ||
1228 strcmp(token
, "<") == 0 ||
1229 strcmp(token
, ">") == 0 ||
1230 strcmp(token
, "==") == 0 ||
1231 strcmp(token
, "!=") == 0) {
1233 left
= malloc_or_die(sizeof(*left
));
1235 /* copy the top arg to the left */
1238 arg
->type
= PRINT_OP
;
1240 arg
->op
.left
= left
;
1244 right
= malloc_or_die(sizeof(*right
));
1246 type
= read_token_item(&token
);
1249 /* could just be a type pointer */
1250 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1251 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1252 if (left
->type
!= PRINT_ATOM
)
1253 die("bad pointer type");
1254 left
->atom
.atom
= realloc(left
->atom
.atom
,
1255 sizeof(left
->atom
.atom
) + 3);
1256 strcat(left
->atom
.atom
, " *");
1263 type
= process_arg_token(event
, right
, tok
, type
);
1265 arg
->op
.right
= right
;
1267 } else if (strcmp(token
, "[") == 0) {
1269 left
= malloc_or_die(sizeof(*left
));
1272 arg
->type
= PRINT_OP
;
1274 arg
->op
.left
= left
;
1277 type
= process_array(event
, arg
, tok
);
1280 warning("unknown op '%s'", token
);
1281 event
->flags
|= EVENT_FL_FAILED
;
1282 /* the arg is now the left side */
1286 if (type
== EVENT_OP
) {
1289 /* higher prios need to be closer to the root */
1290 prio
= get_op_prio(*tok
);
1292 if (prio
> arg
->op
.prio
)
1293 return process_op(event
, arg
, tok
);
1295 return process_op(event
, right
, tok
);
1301 static enum event_type
1302 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1305 enum event_type type
;
1309 if (read_expected(EVENT_OP
, "->") < 0)
1312 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1316 arg
->type
= PRINT_FIELD
;
1317 arg
->field
.name
= field
;
1319 if (is_flag_field
) {
1320 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1321 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1323 } else if (is_symbolic_field
) {
1324 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1325 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1326 is_symbolic_field
= 0;
1329 type
= read_token(&token
);
1339 static char *arg_eval (struct print_arg
*arg
);
1341 static long long arg_num_eval(struct print_arg
*arg
)
1343 long long left
, right
;
1346 switch (arg
->type
) {
1348 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1351 val
= arg_num_eval(arg
->typecast
.item
);
1354 switch (arg
->op
.op
[0]) {
1356 left
= arg_num_eval(arg
->op
.left
);
1357 right
= arg_num_eval(arg
->op
.right
);
1359 val
= left
|| right
;
1364 left
= arg_num_eval(arg
->op
.left
);
1365 right
= arg_num_eval(arg
->op
.right
);
1367 val
= left
&& right
;
1372 left
= arg_num_eval(arg
->op
.left
);
1373 right
= arg_num_eval(arg
->op
.right
);
1374 switch (arg
->op
.op
[1]) {
1379 val
= left
<< right
;
1382 val
= left
<= right
;
1385 die("unknown op '%s'", arg
->op
.op
);
1389 left
= arg_num_eval(arg
->op
.left
);
1390 right
= arg_num_eval(arg
->op
.right
);
1391 switch (arg
->op
.op
[1]) {
1396 val
= left
>> right
;
1399 val
= left
>= right
;
1402 die("unknown op '%s'", arg
->op
.op
);
1406 left
= arg_num_eval(arg
->op
.left
);
1407 right
= arg_num_eval(arg
->op
.right
);
1409 if (arg
->op
.op
[1] != '=')
1410 die("unknown op '%s'", arg
->op
.op
);
1412 val
= left
== right
;
1415 left
= arg_num_eval(arg
->op
.left
);
1416 right
= arg_num_eval(arg
->op
.right
);
1418 switch (arg
->op
.op
[1]) {
1420 val
= left
!= right
;
1423 die("unknown op '%s'", arg
->op
.op
);
1427 left
= arg_num_eval(arg
->op
.left
);
1428 right
= arg_num_eval(arg
->op
.right
);
1432 die("unknown op '%s'", arg
->op
.op
);
1437 case PRINT_FIELD
... PRINT_SYMBOL
:
1440 die("invalid eval type %d", arg
->type
);
1446 static char *arg_eval (struct print_arg
*arg
)
1449 static char buf
[20];
1451 switch (arg
->type
) {
1453 return arg
->atom
.atom
;
1455 return arg_eval(arg
->typecast
.item
);
1457 val
= arg_num_eval(arg
);
1458 sprintf(buf
, "%lld", val
);
1462 case PRINT_FIELD
... PRINT_SYMBOL
:
1465 die("invalid eval type %d", arg
->type
);
1472 static enum event_type
1473 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1475 enum event_type type
;
1476 struct print_arg
*arg
= NULL
;
1477 struct print_flag_sym
*field
;
1483 type
= read_token_item(&token
);
1484 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1487 arg
= malloc_or_die(sizeof(*arg
));
1490 type
= process_arg(event
, arg
, &token
);
1492 if (type
== EVENT_OP
)
1493 type
= process_op(event
, arg
, &token
);
1495 if (type
== EVENT_ERROR
)
1498 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1501 field
= malloc_or_die(sizeof(*field
));
1502 memset(field
, 0, sizeof(*field
));
1504 value
= arg_eval(arg
);
1505 field
->value
= strdup(value
);
1508 type
= process_arg(event
, arg
, &token
);
1509 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1512 value
= arg_eval(arg
);
1513 field
->str
= strdup(value
);
1518 list
= &field
->next
;
1521 type
= read_token_item(&token
);
1522 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1534 static enum event_type
1535 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1537 struct print_arg
*field
;
1538 enum event_type type
;
1541 memset(arg
, 0, sizeof(*arg
));
1542 arg
->type
= PRINT_FLAGS
;
1544 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1547 field
= malloc_or_die(sizeof(*field
));
1549 type
= process_arg(event
, field
, &token
);
1550 while (type
== EVENT_OP
)
1551 type
= process_op(event
, field
, &token
);
1552 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1555 arg
->flags
.field
= field
;
1557 type
= read_token_item(&token
);
1558 if (event_item_type(type
)) {
1559 arg
->flags
.delim
= token
;
1560 type
= read_token_item(&token
);
1563 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1566 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1567 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1571 type
= read_token_item(tok
);
1579 static enum event_type
1580 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1582 struct print_arg
*field
;
1583 enum event_type type
;
1586 memset(arg
, 0, sizeof(*arg
));
1587 arg
->type
= PRINT_SYMBOL
;
1589 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1592 field
= malloc_or_die(sizeof(*field
));
1594 type
= process_arg(event
, field
, &token
);
1595 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1598 arg
->symbol
.field
= field
;
1600 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1601 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1605 type
= read_token_item(tok
);
1613 static enum event_type
1614 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1616 struct print_arg
*item_arg
;
1617 enum event_type type
;
1620 type
= process_arg(event
, arg
, &token
);
1622 if (type
== EVENT_ERROR
)
1625 if (type
== EVENT_OP
)
1626 type
= process_op(event
, arg
, &token
);
1628 if (type
== EVENT_ERROR
)
1631 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1637 type
= read_token_item(&token
);
1640 * If the next token is an item or another open paren, then
1641 * this was a typecast.
1643 if (event_item_type(type
) ||
1644 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1646 /* make this a typecast and contine */
1648 /* prevous must be an atom */
1649 if (arg
->type
!= PRINT_ATOM
)
1650 die("previous needed to be PRINT_ATOM");
1652 item_arg
= malloc_or_die(sizeof(*item_arg
));
1654 arg
->type
= PRINT_TYPE
;
1655 arg
->typecast
.type
= arg
->atom
.atom
;
1656 arg
->typecast
.item
= item_arg
;
1657 type
= process_arg_token(event
, item_arg
, &token
, type
);
1666 static enum event_type
1667 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1669 enum event_type type
;
1672 if (read_expected(EVENT_DELIM
, "(") < 0)
1675 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1678 arg
->type
= PRINT_STRING
;
1679 arg
->string
.string
= token
;
1680 arg
->string
.offset
= -1;
1682 if (read_expected(EVENT_DELIM
, ")") < 0)
1685 type
= read_token(&token
);
1695 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1696 char **tok
, enum event_type type
)
1705 if (strcmp(token
, "REC") == 0) {
1707 type
= process_entry(event
, arg
, &token
);
1708 } else if (strcmp(token
, "__print_flags") == 0) {
1711 type
= process_flags(event
, arg
, &token
);
1712 } else if (strcmp(token
, "__print_symbolic") == 0) {
1714 is_symbolic_field
= 1;
1715 type
= process_symbols(event
, arg
, &token
);
1716 } else if (strcmp(token
, "__get_str") == 0) {
1718 type
= process_str(event
, arg
, &token
);
1721 /* test the next token */
1722 type
= read_token_item(&token
);
1724 /* atoms can be more than one token long */
1725 while (type
== EVENT_ITEM
) {
1726 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1728 strcat(atom
, token
);
1730 type
= read_token_item(&token
);
1733 /* todo, test for function */
1735 arg
->type
= PRINT_ATOM
;
1736 arg
->atom
.atom
= atom
;
1741 arg
->type
= PRINT_ATOM
;
1742 arg
->atom
.atom
= token
;
1743 type
= read_token_item(&token
);
1746 if (strcmp(token
, "(") == 0) {
1748 type
= process_paren(event
, arg
, &token
);
1752 /* handle single ops */
1753 arg
->type
= PRINT_OP
;
1755 arg
->op
.left
= NULL
;
1756 type
= process_op(event
, arg
, &token
);
1760 case EVENT_ERROR
... EVENT_NEWLINE
:
1762 die("unexpected type %d", type
);
1769 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1771 enum event_type type
= EVENT_ERROR
;
1772 struct print_arg
*arg
;
1777 if (type
== EVENT_NEWLINE
) {
1779 type
= read_token_item(&token
);
1783 arg
= malloc_or_die(sizeof(*arg
));
1784 memset(arg
, 0, sizeof(*arg
));
1786 type
= process_arg(event
, arg
, &token
);
1788 if (type
== EVENT_ERROR
) {
1796 if (type
== EVENT_OP
) {
1797 type
= process_op(event
, arg
, &token
);
1802 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1809 } while (type
!= EVENT_NONE
);
1811 if (type
!= EVENT_NONE
)
1817 static int event_read_print(struct event
*event
)
1819 enum event_type type
;
1823 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1826 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1829 if (read_expected(EVENT_OP
, ":") < 0)
1832 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1836 event
->print_fmt
.format
= token
;
1837 event
->print_fmt
.args
= NULL
;
1839 /* ok to have no arg */
1840 type
= read_token_item(&token
);
1842 if (type
== EVENT_NONE
)
1845 /* Handle concatination of print lines */
1846 if (type
== EVENT_DQUOTE
) {
1849 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1851 strcpy(cat
, event
->print_fmt
.format
);
1854 free_token(event
->print_fmt
.format
);
1855 event
->print_fmt
.format
= NULL
;
1860 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1865 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1876 static struct format_field
*
1877 find_common_field(struct event
*event
, const char *name
)
1879 struct format_field
*format
;
1881 for (format
= event
->format
.common_fields
;
1882 format
; format
= format
->next
) {
1883 if (strcmp(format
->name
, name
) == 0)
1890 static struct format_field
*
1891 find_field(struct event
*event
, const char *name
)
1893 struct format_field
*format
;
1895 for (format
= event
->format
.fields
;
1896 format
; format
= format
->next
) {
1897 if (strcmp(format
->name
, name
) == 0)
1904 static struct format_field
*
1905 find_any_field(struct event
*event
, const char *name
)
1907 struct format_field
*format
;
1909 format
= find_common_field(event
, name
);
1912 return find_field(event
, name
);
1915 unsigned long long read_size(void *ptr
, int size
)
1919 return *(unsigned char *)ptr
;
1921 return data2host2(ptr
);
1923 return data2host4(ptr
);
1925 return data2host8(ptr
);
1933 raw_field_value(struct event
*event
, const char *name
, void *data
)
1935 struct format_field
*field
;
1937 field
= find_any_field(event
, name
);
1941 return read_size(data
+ field
->offset
, field
->size
);
1944 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1946 struct format_field
*field
;
1948 field
= find_any_field(event
, name
);
1952 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1955 offset
= *(int *)(data
+ field
->offset
);
1958 return data
+ offset
;
1961 return data
+ field
->offset
;
1964 static int get_common_info(const char *type
, int *offset
, int *size
)
1966 struct event
*event
;
1967 struct format_field
*field
;
1970 * All events should have the same common elements.
1971 * Pick any event to find where the type is;
1974 die("no event_list!");
1977 field
= find_common_field(event
, type
);
1979 die("field '%s' not found", type
);
1981 *offset
= field
->offset
;
1982 *size
= field
->size
;
1987 static int __parse_common(void *data
, int *size
, int *offset
,
1993 ret
= get_common_info(name
, offset
, size
);
1997 return read_size(data
+ *offset
, *size
);
2000 int trace_parse_common_type(void *data
)
2002 static int type_offset
;
2003 static int type_size
;
2005 return __parse_common(data
, &type_size
, &type_offset
,
2009 int trace_parse_common_pid(void *data
)
2011 static int pid_offset
;
2012 static int pid_size
;
2014 return __parse_common(data
, &pid_size
, &pid_offset
,
2018 int parse_common_pc(void *data
)
2020 static int pc_offset
;
2023 return __parse_common(data
, &pc_size
, &pc_offset
,
2024 "common_preempt_count");
2027 int parse_common_flags(void *data
)
2029 static int flags_offset
;
2030 static int flags_size
;
2032 return __parse_common(data
, &flags_size
, &flags_offset
,
2036 int parse_common_lock_depth(void *data
)
2038 static int ld_offset
;
2042 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
2043 "common_lock_depth");
2050 struct event
*trace_find_event(int id
)
2052 struct event
*event
;
2054 for (event
= event_list
; event
; event
= event
->next
) {
2055 if (event
->id
== id
)
2061 struct event
*trace_find_next_event(struct event
*event
)
2069 static unsigned long long eval_num_arg(void *data
, int size
,
2070 struct event
*event
, struct print_arg
*arg
)
2072 unsigned long long val
= 0;
2073 unsigned long long left
, right
;
2074 struct print_arg
*larg
;
2076 switch (arg
->type
) {
2081 return strtoull(arg
->atom
.atom
, NULL
, 0);
2083 if (!arg
->field
.field
) {
2084 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2085 if (!arg
->field
.field
)
2086 die("field %s not found", arg
->field
.name
);
2088 /* must be a number */
2089 val
= read_size(data
+ arg
->field
.field
->offset
,
2090 arg
->field
.field
->size
);
2096 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2101 if (strcmp(arg
->op
.op
, "[") == 0) {
2103 * Arrays are special, since we don't want
2104 * to read the arg as is.
2106 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2107 goto default_op
; /* oops, all bets off */
2108 larg
= arg
->op
.left
;
2109 if (!larg
->field
.field
) {
2111 find_any_field(event
, larg
->field
.name
);
2112 if (!larg
->field
.field
)
2113 die("field %s not found", larg
->field
.name
);
2115 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2116 val
= read_size(data
+ larg
->field
.field
->offset
+
2117 right
* long_size
, long_size
);
2121 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2122 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2123 switch (arg
->op
.op
[0]) {
2126 val
= left
|| right
;
2132 val
= left
&& right
;
2137 switch (arg
->op
.op
[1]) {
2142 val
= left
<< right
;
2145 val
= left
<= right
;
2148 die("unknown op '%s'", arg
->op
.op
);
2152 switch (arg
->op
.op
[1]) {
2157 val
= left
>> right
;
2160 val
= left
>= right
;
2163 die("unknown op '%s'", arg
->op
.op
);
2167 if (arg
->op
.op
[1] != '=')
2168 die("unknown op '%s'", arg
->op
.op
);
2169 val
= left
== right
;
2178 die("unknown op '%s'", arg
->op
.op
);
2181 default: /* not sure what to do there */
2189 unsigned long long value
;
2192 static const struct flag flags
[] = {
2193 { "HI_SOFTIRQ", 0 },
2194 { "TIMER_SOFTIRQ", 1 },
2195 { "NET_TX_SOFTIRQ", 2 },
2196 { "NET_RX_SOFTIRQ", 3 },
2197 { "BLOCK_SOFTIRQ", 4 },
2198 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2199 { "TASKLET_SOFTIRQ", 6 },
2200 { "SCHED_SOFTIRQ", 7 },
2201 { "HRTIMER_SOFTIRQ", 8 },
2202 { "RCU_SOFTIRQ", 9 },
2204 { "HRTIMER_NORESTART", 0 },
2205 { "HRTIMER_RESTART", 1 },
2208 unsigned long long eval_flag(const char *flag
)
2213 * Some flags in the format files do not get converted.
2214 * If the flag is not numeric, see if it is something that
2215 * we already know about.
2217 if (isdigit(flag
[0]))
2218 return strtoull(flag
, NULL
, 0);
2220 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2221 if (strcmp(flags
[i
].name
, flag
) == 0)
2222 return flags
[i
].value
;
2227 static void print_str_arg(void *data
, int size
,
2228 struct event
*event
, struct print_arg
*arg
)
2230 struct print_flag_sym
*flag
;
2231 unsigned long long val
, fval
;
2235 switch (arg
->type
) {
2240 printf("%s", arg
->atom
.atom
);
2243 if (!arg
->field
.field
) {
2244 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2245 if (!arg
->field
.field
)
2246 die("field %s not found", arg
->field
.name
);
2248 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2249 memcpy(str
, data
+ arg
->field
.field
->offset
,
2250 arg
->field
.field
->size
);
2251 str
[arg
->field
.field
->size
] = 0;
2256 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2258 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2259 fval
= eval_flag(flag
->value
);
2260 if (!val
&& !fval
) {
2261 printf("%s", flag
->str
);
2264 if (fval
&& (val
& fval
) == fval
) {
2265 if (print
&& arg
->flags
.delim
)
2266 printf("%s", arg
->flags
.delim
);
2267 printf("%s", flag
->str
);
2274 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2275 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2276 fval
= eval_flag(flag
->value
);
2278 printf("%s", flag
->str
);
2286 case PRINT_STRING
: {
2289 if (arg
->string
.offset
== -1) {
2290 struct format_field
*f
;
2292 f
= find_any_field(event
, arg
->string
.string
);
2293 arg
->string
.offset
= f
->offset
;
2295 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2296 str_offset
&= 0xffff;
2297 printf("%s", ((char *)data
) + str_offset
);
2302 * The only op for string should be ? :
2304 if (arg
->op
.op
[0] != '?')
2306 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2308 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2310 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2318 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2320 static struct format_field
*field
, *ip_field
;
2321 struct print_arg
*args
, *arg
, **next
;
2322 unsigned long long ip
, val
;
2327 field
= find_field(event
, "buf");
2329 die("can't find buffer field for binary printk");
2330 ip_field
= find_field(event
, "ip");
2332 die("can't find ip field for binary printk");
2335 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2338 * The first arg is the IP pointer.
2340 args
= malloc_or_die(sizeof(*args
));
2345 arg
->type
= PRINT_ATOM
;
2346 arg
->atom
.atom
= malloc_or_die(32);
2347 sprintf(arg
->atom
.atom
, "%lld", ip
);
2349 /* skip the first "%pf : " */
2350 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2351 bptr
< data
+ size
&& *ptr
; ptr
++) {
2375 /* the pointers are always 4 bytes aligned */
2376 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2388 val
= read_size(bptr
, ls
);
2390 arg
= malloc_or_die(sizeof(*arg
));
2392 arg
->type
= PRINT_ATOM
;
2393 arg
->atom
.atom
= malloc_or_die(32);
2394 sprintf(arg
->atom
.atom
, "%lld", val
);
2399 arg
= malloc_or_die(sizeof(*arg
));
2401 arg
->type
= PRINT_STRING
;
2402 arg
->string
.string
= strdup(bptr
);
2403 bptr
+= strlen(bptr
) + 1;
2415 static void free_args(struct print_arg
*args
)
2417 struct print_arg
*next
;
2422 if (args
->type
== PRINT_ATOM
)
2423 free(args
->atom
.atom
);
2425 free(args
->string
.string
);
2431 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2433 unsigned long long addr
;
2434 static struct format_field
*field
;
2435 struct printk_map
*printk
;
2440 field
= find_field(event
, "fmt");
2442 die("can't find format field for binary printk");
2443 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2446 addr
= read_size(data
+ field
->offset
, field
->size
);
2448 printk
= find_printk(addr
);
2450 format
= malloc_or_die(45);
2451 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2457 /* Remove any quotes. */
2460 format
= malloc_or_die(strlen(p
) + 10);
2461 sprintf(format
, "%s : %s", "%pf", p
);
2462 /* remove ending quotes and new line since we will add one too */
2463 p
= format
+ strlen(format
) - 1;
2468 if (strcmp(p
, "\\n") == 0)
2474 static void pretty_print(void *data
, int size
, struct event
*event
)
2476 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2477 struct print_arg
*arg
= print_fmt
->args
;
2478 struct print_arg
*args
= NULL
;
2479 const char *ptr
= print_fmt
->format
;
2480 unsigned long long val
;
2481 struct func_map
*func
;
2482 const char *saveptr
;
2483 char *bprint_fmt
= NULL
;
2489 if (event
->flags
& EVENT_FL_ISFUNC
)
2490 ptr
= " %pF <-- %pF";
2492 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2493 bprint_fmt
= get_bprint_format(data
, size
, event
);
2494 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2499 for (; *ptr
; ptr
++) {
2521 } else if (*ptr
== '%') {
2546 if (*(ptr
+1) == 'F' ||
2559 die("no argument match");
2561 len
= ((unsigned long)ptr
+ 1) -
2562 (unsigned long)saveptr
;
2564 /* should never happen */
2568 memcpy(format
, saveptr
, len
);
2571 val
= eval_num_arg(data
, size
, event
, arg
);
2575 func
= find_func(val
);
2577 printf("%s", func
->func
);
2578 if (show_func
== 'F')
2586 printf(format
, (int)val
);
2589 printf(format
, (long)val
);
2592 printf(format
, (long long)val
);
2595 die("bad count (%d)", ls
);
2600 die("no matching argument");
2602 print_str_arg(data
, size
, event
, arg
);
2606 printf(">%c<", *ptr
);
2619 static inline int log10_cpu(int nb
)
2628 static void print_lat_fmt(void *data
, int size __unused
)
2630 unsigned int lat_flags
;
2636 lat_flags
= parse_common_flags(data
);
2637 pc
= parse_common_pc(data
);
2638 lock_depth
= parse_common_lock_depth(data
);
2640 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2641 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2644 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2645 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2647 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2649 (hardirq
&& softirq
) ? 'H' :
2650 hardirq
? 'h' : softirq
? 's' : '.');
2660 printf("%d ", lock_depth
);
2663 #define TRACE_GRAPH_INDENT 2
2665 static struct record
*
2666 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2667 struct record
*next
)
2669 struct format_field
*field
;
2670 struct event
*event
;
2675 type
= trace_parse_common_type(next
->data
);
2676 event
= trace_find_event(type
);
2680 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2683 pid
= trace_parse_common_pid(next
->data
);
2684 field
= find_field(event
, "func");
2686 die("function return does not have field func");
2688 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2690 if (cur_pid
!= pid
|| cur_func
!= val
)
2693 /* this is a leaf, now advance the iterator */
2694 return trace_read_data(cpu
);
2697 /* Signal a overhead of time execution to the output */
2698 static void print_graph_overhead(unsigned long long duration
)
2700 /* Non nested entry or return */
2701 if (duration
== ~0ULL)
2702 return (void)printf(" ");
2704 /* Duration exceeded 100 msecs */
2705 if (duration
> 100000ULL)
2706 return (void)printf("! ");
2708 /* Duration exceeded 10 msecs */
2709 if (duration
> 10000ULL)
2710 return (void)printf("+ ");
2715 static void print_graph_duration(unsigned long long duration
)
2717 unsigned long usecs
= duration
/ 1000;
2718 unsigned long nsecs_rem
= duration
% 1000;
2719 /* log10(ULONG_MAX) + '\0' */
2725 sprintf(msecs_str
, "%lu", usecs
);
2728 len
= printf("%lu", usecs
);
2730 /* Print nsecs (we don't want to exceed 7 numbers) */
2732 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2733 len
+= printf(".%s", nsecs_str
);
2738 /* Print remaining spaces to fit the row's width */
2739 for (i
= len
; i
< 7; i
++)
2746 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2748 unsigned long long rettime
, calltime
;
2749 unsigned long long duration
, depth
;
2750 unsigned long long val
;
2751 struct format_field
*field
;
2752 struct func_map
*func
;
2753 struct event
*ret_event
;
2757 type
= trace_parse_common_type(ret_rec
->data
);
2758 ret_event
= trace_find_event(type
);
2760 field
= find_field(ret_event
, "rettime");
2762 die("can't find rettime in return graph");
2763 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2765 field
= find_field(ret_event
, "calltime");
2767 die("can't find rettime in return graph");
2768 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2770 duration
= rettime
- calltime
;
2773 print_graph_overhead(duration
);
2776 print_graph_duration(duration
);
2778 field
= find_field(event
, "depth");
2780 die("can't find depth in entry graph");
2781 depth
= read_size(data
+ field
->offset
, field
->size
);
2784 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2787 field
= find_field(event
, "func");
2789 die("can't find func in entry graph");
2790 val
= read_size(data
+ field
->offset
, field
->size
);
2791 func
= find_func(val
);
2794 printf("%s();", func
->func
);
2796 printf("%llx();", val
);
2799 static void print_graph_nested(struct event
*event
, void *data
)
2801 struct format_field
*field
;
2802 unsigned long long depth
;
2803 unsigned long long val
;
2804 struct func_map
*func
;
2808 print_graph_overhead(-1);
2813 field
= find_field(event
, "depth");
2815 die("can't find depth in entry graph");
2816 depth
= read_size(data
+ field
->offset
, field
->size
);
2819 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2822 field
= find_field(event
, "func");
2824 die("can't find func in entry graph");
2825 val
= read_size(data
+ field
->offset
, field
->size
);
2826 func
= find_func(val
);
2829 printf("%s() {", func
->func
);
2831 printf("%llx() {", val
);
2835 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2838 struct format_field
*field
;
2843 if (latency_format
) {
2844 print_lat_fmt(data
, size
);
2848 field
= find_field(event
, "func");
2850 die("function entry does not have func field");
2852 val
= read_size(data
+ field
->offset
, field
->size
);
2855 * peek_data may unmap the data pointer. Copy it first.
2857 copy_data
= malloc_or_die(size
);
2858 memcpy(copy_data
, data
, size
);
2861 rec
= trace_peek_data(cpu
);
2863 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2865 print_graph_entry_leaf(event
, data
, rec
);
2869 print_graph_nested(event
, data
);
2875 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
)
2877 unsigned long long rettime
, calltime
;
2878 unsigned long long duration
, depth
;
2879 struct format_field
*field
;
2882 if (latency_format
) {
2883 print_lat_fmt(data
, size
);
2887 field
= find_field(event
, "rettime");
2889 die("can't find rettime in return graph");
2890 rettime
= read_size(data
+ field
->offset
, field
->size
);
2892 field
= find_field(event
, "calltime");
2894 die("can't find calltime in return graph");
2895 calltime
= read_size(data
+ field
->offset
, field
->size
);
2897 duration
= rettime
- calltime
;
2900 print_graph_overhead(duration
);
2903 print_graph_duration(duration
);
2905 field
= find_field(event
, "depth");
2907 die("can't find depth in entry graph");
2908 depth
= read_size(data
+ field
->offset
, field
->size
);
2911 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2918 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2921 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2922 pretty_print_func_ent(data
, size
, event
, cpu
, pid
);
2923 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2924 pretty_print_func_ret(data
, size
, event
);
2928 void print_trace_event(int cpu
, void *data
, int size
)
2930 struct event
*event
;
2934 type
= trace_parse_common_type(data
);
2936 event
= trace_find_event(type
);
2938 warning("ug! no event found for type %d", type
);
2942 pid
= trace_parse_common_pid(data
);
2944 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2945 return pretty_print_func_graph(data
, size
, event
, cpu
, pid
);
2948 print_lat_fmt(data
, size
);
2950 if (event
->flags
& EVENT_FL_FAILED
) {
2951 printf("EVENT '%s' FAILED TO PARSE\n",
2956 pretty_print(data
, size
, event
);
2959 static void print_fields(struct print_flag_sym
*field
)
2961 printf("{ %s, %s }", field
->value
, field
->str
);
2964 print_fields(field
->next
);
2968 static void print_args(struct print_arg
*args
)
2970 int print_paren
= 1;
2972 switch (args
->type
) {
2977 printf("%s", args
->atom
.atom
);
2980 printf("REC->%s", args
->field
.name
);
2983 printf("__print_flags(");
2984 print_args(args
->flags
.field
);
2985 printf(", %s, ", args
->flags
.delim
);
2986 print_fields(args
->flags
.flags
);
2990 printf("__print_symbolic(");
2991 print_args(args
->symbol
.field
);
2993 print_fields(args
->symbol
.symbols
);
2997 printf("__get_str(%s)", args
->string
.string
);
3000 printf("(%s)", args
->typecast
.type
);
3001 print_args(args
->typecast
.item
);
3004 if (strcmp(args
->op
.op
, ":") == 0)
3008 print_args(args
->op
.left
);
3009 printf(" %s ", args
->op
.op
);
3010 print_args(args
->op
.right
);
3015 /* we should warn... */
3020 print_args(args
->next
);
3024 int parse_ftrace_file(char *buf
, unsigned long size
)
3026 struct format_field
*field
;
3027 struct print_arg
*arg
, **list
;
3028 struct event
*event
;
3031 init_input_buf(buf
, size
);
3033 event
= alloc_event();
3037 event
->flags
|= EVENT_FL_ISFTRACE
;
3039 event
->name
= event_read_name();
3041 die("failed to read ftrace event name");
3043 if (strcmp(event
->name
, "function") == 0)
3044 event
->flags
|= EVENT_FL_ISFUNC
;
3046 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3047 event
->flags
|= EVENT_FL_ISFUNCENT
;
3049 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3050 event
->flags
|= EVENT_FL_ISFUNCRET
;
3052 else if (strcmp(event
->name
, "bprint") == 0)
3053 event
->flags
|= EVENT_FL_ISBPRINT
;
3055 event
->id
= event_read_id();
3057 die("failed to read ftrace event id");
3061 ret
= event_read_format(event
);
3063 die("failed to read ftrace event format");
3065 ret
= event_read_print(event
);
3067 die("failed to read ftrace event print fmt");
3069 /* New ftrace handles args */
3073 * The arguments for ftrace files are parsed by the fields.
3074 * Set up the fields as their arguments.
3076 list
= &event
->print_fmt
.args
;
3077 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3078 arg
= malloc_or_die(sizeof(*arg
));
3079 memset(arg
, 0, sizeof(*arg
));
3082 arg
->type
= PRINT_FIELD
;
3083 arg
->field
.name
= field
->name
;
3084 arg
->field
.field
= field
;
3089 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3091 struct event
*event
;
3094 init_input_buf(buf
, size
);
3096 event
= alloc_event();
3100 event
->name
= event_read_name();
3102 die("failed to read event name");
3104 event
->id
= event_read_id();
3106 die("failed to read event id");
3108 ret
= event_read_format(event
);
3110 warning("failed to read event format for %s", event
->name
);
3114 ret
= event_read_print(event
);
3116 warning("failed to read event print fmt for %s", event
->name
);
3120 event
->system
= strdup(sys
);
3122 #define PRINT_ARGS 0
3123 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3124 print_args(event
->print_fmt
.args
);
3130 event
->flags
|= EVENT_FL_FAILED
;
3131 /* still add it even if it failed */
3136 void parse_set_info(int nr_cpus
, int long_sz
)
3139 long_size
= long_sz
;
3142 int common_pc(struct scripting_context
*context
)
3144 return parse_common_pc(context
->event_data
);
3147 int common_flags(struct scripting_context
*context
)
3149 return parse_common_flags(context
->event_data
);
3152 int common_lock_depth(struct scripting_context
*context
)
3154 return parse_common_lock_depth(context
->event_data
);