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.
34 #include "trace-event.h"
36 int header_page_ts_offset
;
37 int header_page_ts_size
;
38 int header_page_size_offset
;
39 int header_page_size_size
;
40 int header_page_overwrite_offset
;
41 int header_page_overwrite_size
;
42 int header_page_data_offset
;
43 int header_page_data_size
;
47 static char *input_buf
;
48 static unsigned long long input_buf_ptr
;
49 static unsigned long long input_buf_siz
;
53 static int is_flag_field
;
54 static int is_symbolic_field
;
56 static struct format_field
*
57 find_any_field(struct event
*event
, const char *name
);
59 static void init_input_buf(char *buf
, unsigned long long size
)
71 static struct cmdline
*cmdlines
;
72 static int cmdline_count
;
74 static int cmdline_cmp(const void *a
, const void *b
)
76 const struct cmdline
*ca
= a
;
77 const struct cmdline
*cb
= b
;
79 if (ca
->pid
< cb
->pid
)
81 if (ca
->pid
> cb
->pid
)
87 void parse_cmdlines(char *file
, int size __unused
)
90 struct cmdline_list
*next
;
93 } *list
= NULL
, *item
;
98 line
= strtok_r(file
, "\n", &next
);
100 item
= malloc_or_die(sizeof(*item
));
101 sscanf(line
, "%d %as", &item
->pid
,
102 (float *)(void *)&item
->comm
); /* workaround gcc warning */
105 line
= strtok_r(NULL
, "\n", &next
);
109 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
113 cmdlines
[i
].pid
= list
->pid
;
114 cmdlines
[i
].comm
= list
->comm
;
121 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
124 static struct func_map
{
125 unsigned long long addr
;
129 static unsigned int func_count
;
131 static int func_cmp(const void *a
, const void *b
)
133 const struct func_map
*fa
= a
;
134 const struct func_map
*fb
= b
;
136 if (fa
->addr
< fb
->addr
)
138 if (fa
->addr
> fb
->addr
)
144 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
147 struct func_list
*next
;
148 unsigned long long addr
;
151 } *list
= NULL
, *item
;
159 line
= strtok_r(file
, "\n", &next
);
161 item
= malloc_or_die(sizeof(*item
));
163 ret
= sscanf(line
, "%as %c %as\t[%as",
164 (float *)(void *)&addr_str
, /* workaround gcc warning */
166 (float *)(void *)&item
->func
,
167 (float *)(void *)&item
->mod
);
168 item
->addr
= strtoull(addr_str
, NULL
, 16);
171 /* truncate the extra ']' */
173 item
->mod
[strlen(item
->mod
) - 1] = 0;
178 line
= strtok_r(NULL
, "\n", &next
);
182 func_list
= malloc_or_die(sizeof(*func_list
) * (func_count
+ 1));
186 func_list
[i
].func
= list
->func
;
187 func_list
[i
].addr
= list
->addr
;
188 func_list
[i
].mod
= list
->mod
;
195 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
198 * Add a special record at the end.
200 func_list
[func_count
].func
= NULL
;
201 func_list
[func_count
].addr
= 0;
202 func_list
[func_count
].mod
= NULL
;
206 * We are searching for a record in between, not an exact
209 static int func_bcmp(const void *a
, const void *b
)
211 const struct func_map
*fa
= a
;
212 const struct func_map
*fb
= b
;
214 if ((fa
->addr
== fb
->addr
) ||
216 (fa
->addr
> fb
->addr
&&
217 fa
->addr
< (fb
+1)->addr
))
220 if (fa
->addr
< fb
->addr
)
226 static struct func_map
*find_func(unsigned long long addr
)
228 struct func_map
*func
;
233 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
239 void print_funcs(void)
243 for (i
= 0; i
< (int)func_count
; i
++) {
247 if (func_list
[i
].mod
)
248 printf(" [%s]\n", func_list
[i
].mod
);
254 static struct printk_map
{
255 unsigned long long addr
;
258 static unsigned int printk_count
;
260 static int printk_cmp(const void *a
, const void *b
)
262 const struct func_map
*fa
= a
;
263 const struct func_map
*fb
= b
;
265 if (fa
->addr
< fb
->addr
)
267 if (fa
->addr
> fb
->addr
)
273 static struct printk_map
*find_printk(unsigned long long addr
)
275 struct printk_map
*printk
;
276 struct printk_map key
;
280 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
286 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
289 struct printk_list
*next
;
290 unsigned long long addr
;
292 } *list
= NULL
, *item
;
298 line
= strtok_r(file
, "\n", &next
);
300 addr_str
= strsep(&line
, ":");
302 warning("error parsing print strings");
305 item
= malloc_or_die(sizeof(*item
));
306 item
->addr
= strtoull(addr_str
, NULL
, 16);
307 /* fmt still has a space, skip it */
308 item
->printk
= strdup(line
+1);
311 line
= strtok_r(NULL
, "\n", &next
);
315 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
319 printk_list
[i
].printk
= list
->printk
;
320 printk_list
[i
].addr
= list
->addr
;
327 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
330 void print_printk(void)
334 for (i
= 0; i
< (int)printk_count
; i
++) {
335 printf("%016llx %s\n",
337 printk_list
[i
].printk
);
341 static struct event
*alloc_event(void)
345 event
= malloc_or_die(sizeof(*event
));
346 memset(event
, 0, sizeof(*event
));
363 static struct event
*event_list
;
365 static void add_event(struct event
*event
)
367 event
->next
= event_list
;
371 static int event_item_type(enum event_type type
)
374 case EVENT_ITEM
... EVENT_SQUOTE
:
376 case EVENT_ERROR
... EVENT_DELIM
:
382 static void free_arg(struct print_arg
*arg
)
390 free(arg
->atom
.atom
);
393 case PRINT_FIELD
... PRINT_OP
:
402 static enum event_type
get_type(int ch
)
405 return EVENT_NEWLINE
;
408 if (isalnum(ch
) || ch
== '_')
416 if (ch
== '(' || ch
== ')' || ch
== ',')
422 static int __read_char(void)
424 if (input_buf_ptr
>= input_buf_siz
)
427 return input_buf
[input_buf_ptr
++];
430 static int __peek_char(void)
432 if (input_buf_ptr
>= input_buf_siz
)
435 return input_buf
[input_buf_ptr
];
438 static enum event_type
__read_token(char **tok
)
441 int ch
, last_ch
, quote_ch
, next_ch
;
444 enum event_type type
;
454 if (type
== EVENT_NONE
)
462 *tok
= malloc_or_die(2);
470 next_ch
= __peek_char();
471 if (next_ch
== '>') {
472 buf
[i
++] = __read_char();
485 buf
[i
++] = __read_char();
497 default: /* what should we do instead? */
507 buf
[i
++] = __read_char();
512 /* don't keep quotes */
517 if (i
== (BUFSIZ
- 1)) {
520 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
535 /* the '\' '\' will cancel itself */
536 if (ch
== '\\' && last_ch
== '\\')
538 } while (ch
!= quote_ch
|| last_ch
== '\\');
539 /* remove the last quote */
543 case EVENT_ERROR
... EVENT_SPACE
:
549 while (get_type(__peek_char()) == type
) {
550 if (i
== (BUFSIZ
- 1)) {
553 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
572 *tok
= realloc(*tok
, tok_size
+ i
);
584 static void free_token(char *tok
)
590 static enum event_type
read_token(char **tok
)
592 enum event_type type
;
595 type
= __read_token(tok
);
596 if (type
!= EVENT_SPACE
)
607 static enum event_type
read_token_item(char **tok
)
609 enum event_type type
;
612 type
= __read_token(tok
);
613 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
623 static int test_type(enum event_type type
, enum event_type expect
)
625 if (type
!= expect
) {
626 warning("Error: expected type %d but read %d",
633 static int __test_type_token(enum event_type type
, char *token
,
634 enum event_type expect
, const char *expect_tok
,
637 if (type
!= expect
) {
639 warning("Error: expected type %d but read %d",
644 if (strcmp(token
, expect_tok
) != 0) {
646 warning("Error: expected '%s' but read '%s'",
653 static int test_type_token(enum event_type type
, char *token
,
654 enum event_type expect
, const char *expect_tok
)
656 return __test_type_token(type
, token
, expect
, expect_tok
, true);
659 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
661 enum event_type type
;
664 type
= read_token(tok
);
666 type
= read_token_item(tok
);
667 return test_type(type
, expect
);
670 static int read_expect_type(enum event_type expect
, char **tok
)
672 return __read_expect_type(expect
, tok
, 1);
675 static int __read_expected(enum event_type expect
, const char *str
,
676 int newline_ok
, bool warn
)
678 enum event_type type
;
683 type
= read_token(&token
);
685 type
= read_token_item(&token
);
687 ret
= __test_type_token(type
, token
, expect
, str
, warn
);
694 static int read_expected(enum event_type expect
, const char *str
)
696 return __read_expected(expect
, str
, 1, true);
699 static int read_expected_item(enum event_type expect
, const char *str
)
701 return __read_expected(expect
, str
, 0, true);
704 static char *event_read_name(void)
708 if (read_expected(EVENT_ITEM
, "name") < 0)
711 if (read_expected(EVENT_OP
, ":") < 0)
714 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
724 static int event_read_id(void)
729 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
732 if (read_expected(EVENT_OP
, ":") < 0)
735 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
738 id
= strtoul(token
, NULL
, 0);
747 static int field_is_string(struct format_field
*field
)
749 if ((field
->flags
& FIELD_IS_ARRAY
) &&
750 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
751 !strstr(field
->type
, "s8")))
757 static int field_is_dynamic(struct format_field
*field
)
759 if (!strncmp(field
->type
, "__data_loc", 10))
765 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
767 struct format_field
*field
= NULL
;
768 enum event_type type
;
774 type
= read_token(&token
);
775 if (type
== EVENT_NEWLINE
) {
782 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
786 type
= read_token(&token
);
788 * The ftrace fields may still use the "special" name.
791 if (event
->flags
& EVENT_FL_ISFTRACE
&&
792 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
794 type
= read_token(&token
);
797 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
800 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
805 field
= malloc_or_die(sizeof(*field
));
806 memset(field
, 0, sizeof(*field
));
808 /* read the rest of the type */
810 type
= read_token(&token
);
811 if (type
== EVENT_ITEM
||
812 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
814 * Some of the ftrace fields are broken and have
815 * an illegal "." in them.
817 (event
->flags
& EVENT_FL_ISFTRACE
&&
818 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
820 if (strcmp(token
, "*") == 0)
821 field
->flags
|= FIELD_IS_POINTER
;
824 field
->type
= realloc(field
->type
,
825 strlen(field
->type
) +
826 strlen(last_token
) + 2);
827 strcat(field
->type
, " ");
828 strcat(field
->type
, last_token
);
830 field
->type
= last_token
;
839 die("no type found");
842 field
->name
= last_token
;
844 if (test_type(type
, EVENT_OP
))
847 if (strcmp(token
, "[") == 0) {
848 enum event_type last_type
= type
;
849 char *brackets
= token
;
852 field
->flags
|= FIELD_IS_ARRAY
;
854 type
= read_token(&token
);
855 while (strcmp(token
, "]") != 0) {
856 if (last_type
== EVENT_ITEM
&&
863 brackets
= realloc(brackets
,
865 strlen(token
) + len
);
867 strcat(brackets
, " ");
868 strcat(brackets
, token
);
870 type
= read_token(&token
);
871 if (type
== EVENT_NONE
) {
872 die("failed to find token");
879 brackets
= realloc(brackets
, strlen(brackets
) + 2);
880 strcat(brackets
, "]");
882 /* add brackets to type */
884 type
= read_token(&token
);
886 * If the next token is not an OP, then it is of
887 * the format: type [] item;
889 if (type
== EVENT_ITEM
) {
890 field
->type
= realloc(field
->type
,
891 strlen(field
->type
) +
892 strlen(field
->name
) +
893 strlen(brackets
) + 2);
894 strcat(field
->type
, " ");
895 strcat(field
->type
, field
->name
);
896 free_token(field
->name
);
897 strcat(field
->type
, brackets
);
899 type
= read_token(&token
);
901 field
->type
= realloc(field
->type
,
902 strlen(field
->type
) +
903 strlen(brackets
) + 1);
904 strcat(field
->type
, brackets
);
909 if (field_is_string(field
)) {
910 field
->flags
|= FIELD_IS_STRING
;
911 if (field_is_dynamic(field
))
912 field
->flags
|= FIELD_IS_DYNAMIC
;
915 if (test_type_token(type
, token
, EVENT_OP
, ";"))
919 if (read_expected(EVENT_ITEM
, "offset") < 0)
922 if (read_expected(EVENT_OP
, ":") < 0)
925 if (read_expect_type(EVENT_ITEM
, &token
))
927 field
->offset
= strtoul(token
, NULL
, 0);
930 if (read_expected(EVENT_OP
, ";") < 0)
933 if (read_expected(EVENT_ITEM
, "size") < 0)
936 if (read_expected(EVENT_OP
, ":") < 0)
939 if (read_expect_type(EVENT_ITEM
, &token
))
941 field
->size
= strtoul(token
, NULL
, 0);
944 if (read_expected(EVENT_OP
, ";") < 0)
947 type
= read_token(&token
);
948 if (type
!= EVENT_NEWLINE
) {
949 /* newer versions of the kernel have a "signed" type */
950 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
955 if (read_expected(EVENT_OP
, ":") < 0)
958 if (read_expect_type(EVENT_ITEM
, &token
))
961 if (strtoul(token
, NULL
, 0))
962 field
->flags
|= FIELD_IS_SIGNED
;
965 if (read_expected(EVENT_OP
, ";") < 0)
968 if (read_expect_type(EVENT_NEWLINE
, &token
))
975 fields
= &field
->next
;
989 static int event_read_format(struct event
*event
)
994 if (read_expected_item(EVENT_ITEM
, "format") < 0)
997 if (read_expected(EVENT_OP
, ":") < 0)
1000 if (read_expect_type(EVENT_NEWLINE
, &token
))
1004 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1007 event
->format
.nr_common
= ret
;
1009 ret
= event_read_fields(event
, &event
->format
.fields
);
1012 event
->format
.nr_fields
= ret
;
1022 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1023 char **tok
, enum event_type type
);
1025 static enum event_type
1026 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1028 enum event_type type
;
1031 type
= read_token(&token
);
1034 return process_arg_token(event
, arg
, tok
, type
);
1037 static enum event_type
1038 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1040 struct print_arg
*arg
, *left
, *right
;
1041 enum event_type type
;
1044 arg
= malloc_or_die(sizeof(*arg
));
1045 memset(arg
, 0, sizeof(*arg
));
1047 left
= malloc_or_die(sizeof(*left
));
1049 right
= malloc_or_die(sizeof(*right
));
1051 arg
->type
= PRINT_OP
;
1052 arg
->op
.left
= left
;
1053 arg
->op
.right
= right
;
1056 type
= process_arg(event
, left
, &token
);
1057 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1062 type
= process_arg(event
, right
, &token
);
1064 top
->op
.right
= arg
;
1077 static enum event_type
1078 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1080 struct print_arg
*arg
;
1081 enum event_type type
;
1084 arg
= malloc_or_die(sizeof(*arg
));
1085 memset(arg
, 0, sizeof(*arg
));
1088 type
= process_arg(event
, arg
, &token
);
1089 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1092 top
->op
.right
= arg
;
1095 type
= read_token_item(&token
);
1106 static int get_op_prio(char *op
)
1117 /* '>>' and '<<' are 8 */
1121 /* '==' and '!=' are 10 */
1131 die("unknown op '%c'", op
[0]);
1135 if (strcmp(op
, "++") == 0 ||
1136 strcmp(op
, "--") == 0) {
1138 } else if (strcmp(op
, ">>") == 0 ||
1139 strcmp(op
, "<<") == 0) {
1141 } else if (strcmp(op
, ">=") == 0 ||
1142 strcmp(op
, "<=") == 0) {
1144 } else if (strcmp(op
, "==") == 0 ||
1145 strcmp(op
, "!=") == 0) {
1147 } else if (strcmp(op
, "&&") == 0) {
1149 } else if (strcmp(op
, "||") == 0) {
1152 die("unknown op '%s'", op
);
1158 static void set_op_prio(struct print_arg
*arg
)
1161 /* single ops are the greatest */
1162 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1167 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1170 static enum event_type
1171 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1173 struct print_arg
*left
, *right
= NULL
;
1174 enum event_type type
;
1177 /* the op is passed in via tok */
1180 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1181 /* handle single op */
1183 die("bad op token %s", token
);
1192 die("bad op token %s", token
);
1196 /* make an empty left */
1197 left
= malloc_or_die(sizeof(*left
));
1198 left
->type
= PRINT_NULL
;
1199 arg
->op
.left
= left
;
1201 right
= malloc_or_die(sizeof(*right
));
1202 arg
->op
.right
= right
;
1204 type
= process_arg(event
, right
, tok
);
1206 } else if (strcmp(token
, "?") == 0) {
1208 left
= malloc_or_die(sizeof(*left
));
1209 /* copy the top arg to the left */
1212 arg
->type
= PRINT_OP
;
1214 arg
->op
.left
= left
;
1217 type
= process_cond(event
, arg
, tok
);
1219 } else if (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 ||
1232 strcmp(token
, "==") == 0 ||
1233 strcmp(token
, "!=") == 0) {
1235 left
= malloc_or_die(sizeof(*left
));
1237 /* copy the top arg to the left */
1240 arg
->type
= PRINT_OP
;
1242 arg
->op
.left
= left
;
1246 right
= malloc_or_die(sizeof(*right
));
1248 type
= read_token_item(&token
);
1251 /* could just be a type pointer */
1252 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1253 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1254 if (left
->type
!= PRINT_ATOM
)
1255 die("bad pointer type");
1256 left
->atom
.atom
= realloc(left
->atom
.atom
,
1257 sizeof(left
->atom
.atom
) + 3);
1258 strcat(left
->atom
.atom
, " *");
1265 type
= process_arg_token(event
, right
, tok
, type
);
1267 arg
->op
.right
= right
;
1269 } else if (strcmp(token
, "[") == 0) {
1271 left
= malloc_or_die(sizeof(*left
));
1274 arg
->type
= PRINT_OP
;
1276 arg
->op
.left
= left
;
1279 type
= process_array(event
, arg
, tok
);
1282 warning("unknown op '%s'", token
);
1283 event
->flags
|= EVENT_FL_FAILED
;
1284 /* the arg is now the left side */
1288 if (type
== EVENT_OP
) {
1291 /* higher prios need to be closer to the root */
1292 prio
= get_op_prio(*tok
);
1294 if (prio
> arg
->op
.prio
)
1295 return process_op(event
, arg
, tok
);
1297 return process_op(event
, right
, tok
);
1303 static enum event_type
1304 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1307 enum event_type type
;
1311 if (read_expected(EVENT_OP
, "->") < 0)
1314 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1318 arg
->type
= PRINT_FIELD
;
1319 arg
->field
.name
= field
;
1321 if (is_flag_field
) {
1322 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1323 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1325 } else if (is_symbolic_field
) {
1326 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1327 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1328 is_symbolic_field
= 0;
1331 type
= read_token(&token
);
1341 static char *arg_eval (struct print_arg
*arg
);
1343 static long long arg_num_eval(struct print_arg
*arg
)
1345 long long left
, right
;
1348 switch (arg
->type
) {
1350 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1353 val
= arg_num_eval(arg
->typecast
.item
);
1356 switch (arg
->op
.op
[0]) {
1358 left
= arg_num_eval(arg
->op
.left
);
1359 right
= arg_num_eval(arg
->op
.right
);
1361 val
= left
|| right
;
1366 left
= arg_num_eval(arg
->op
.left
);
1367 right
= arg_num_eval(arg
->op
.right
);
1369 val
= left
&& right
;
1374 left
= arg_num_eval(arg
->op
.left
);
1375 right
= arg_num_eval(arg
->op
.right
);
1376 switch (arg
->op
.op
[1]) {
1381 val
= left
<< right
;
1384 val
= left
<= right
;
1387 die("unknown op '%s'", arg
->op
.op
);
1391 left
= arg_num_eval(arg
->op
.left
);
1392 right
= arg_num_eval(arg
->op
.right
);
1393 switch (arg
->op
.op
[1]) {
1398 val
= left
>> right
;
1401 val
= left
>= right
;
1404 die("unknown op '%s'", arg
->op
.op
);
1408 left
= arg_num_eval(arg
->op
.left
);
1409 right
= arg_num_eval(arg
->op
.right
);
1411 if (arg
->op
.op
[1] != '=')
1412 die("unknown op '%s'", arg
->op
.op
);
1414 val
= left
== right
;
1417 left
= arg_num_eval(arg
->op
.left
);
1418 right
= arg_num_eval(arg
->op
.right
);
1420 switch (arg
->op
.op
[1]) {
1422 val
= left
!= right
;
1425 die("unknown op '%s'", arg
->op
.op
);
1429 die("unknown op '%s'", arg
->op
.op
);
1434 case PRINT_FIELD
... PRINT_SYMBOL
:
1437 die("invalid eval type %d", arg
->type
);
1443 static char *arg_eval (struct print_arg
*arg
)
1446 static char buf
[20];
1448 switch (arg
->type
) {
1450 return arg
->atom
.atom
;
1452 return arg_eval(arg
->typecast
.item
);
1454 val
= arg_num_eval(arg
);
1455 sprintf(buf
, "%lld", val
);
1459 case PRINT_FIELD
... PRINT_SYMBOL
:
1462 die("invalid eval type %d", arg
->type
);
1469 static enum event_type
1470 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1472 enum event_type type
;
1473 struct print_arg
*arg
= NULL
;
1474 struct print_flag_sym
*field
;
1480 type
= read_token_item(&token
);
1481 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1484 arg
= malloc_or_die(sizeof(*arg
));
1487 type
= process_arg(event
, arg
, &token
);
1488 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1491 field
= malloc_or_die(sizeof(*field
));
1492 memset(field
, 0, sizeof(*field
));
1494 value
= arg_eval(arg
);
1495 field
->value
= strdup(value
);
1498 type
= process_arg(event
, arg
, &token
);
1499 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1502 value
= arg_eval(arg
);
1503 field
->str
= strdup(value
);
1508 list
= &field
->next
;
1511 type
= read_token_item(&token
);
1512 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1524 static enum event_type
1525 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1527 struct print_arg
*field
;
1528 enum event_type type
;
1531 memset(arg
, 0, sizeof(*arg
));
1532 arg
->type
= PRINT_FLAGS
;
1534 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1537 field
= malloc_or_die(sizeof(*field
));
1539 type
= process_arg(event
, field
, &token
);
1540 while (type
== EVENT_OP
)
1541 type
= process_op(event
, field
, &token
);
1542 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1545 arg
->flags
.field
= field
;
1547 type
= read_token_item(&token
);
1548 if (event_item_type(type
)) {
1549 arg
->flags
.delim
= token
;
1550 type
= read_token_item(&token
);
1553 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1556 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1557 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1561 type
= read_token_item(tok
);
1569 static enum event_type
1570 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1572 struct print_arg
*field
;
1573 enum event_type type
;
1576 memset(arg
, 0, sizeof(*arg
));
1577 arg
->type
= PRINT_SYMBOL
;
1579 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1582 field
= malloc_or_die(sizeof(*field
));
1584 type
= process_arg(event
, field
, &token
);
1585 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1588 arg
->symbol
.field
= field
;
1590 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1591 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1595 type
= read_token_item(tok
);
1603 static enum event_type
1604 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1606 struct print_arg
*item_arg
;
1607 enum event_type type
;
1610 type
= process_arg(event
, arg
, &token
);
1612 if (type
== EVENT_ERROR
)
1615 if (type
== EVENT_OP
)
1616 type
= process_op(event
, arg
, &token
);
1618 if (type
== EVENT_ERROR
)
1621 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1627 type
= read_token_item(&token
);
1630 * If the next token is an item or another open paren, then
1631 * this was a typecast.
1633 if (event_item_type(type
) ||
1634 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1636 /* make this a typecast and contine */
1638 /* prevous must be an atom */
1639 if (arg
->type
!= PRINT_ATOM
)
1640 die("previous needed to be PRINT_ATOM");
1642 item_arg
= malloc_or_die(sizeof(*item_arg
));
1644 arg
->type
= PRINT_TYPE
;
1645 arg
->typecast
.type
= arg
->atom
.atom
;
1646 arg
->typecast
.item
= item_arg
;
1647 type
= process_arg_token(event
, item_arg
, &token
, type
);
1656 static enum event_type
1657 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1659 enum event_type type
;
1662 if (read_expected(EVENT_DELIM
, "(") < 0)
1665 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1668 arg
->type
= PRINT_STRING
;
1669 arg
->string
.string
= token
;
1670 arg
->string
.offset
= -1;
1672 if (read_expected(EVENT_DELIM
, ")") < 0)
1675 type
= read_token(&token
);
1685 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1686 char **tok
, enum event_type type
)
1695 if (strcmp(token
, "REC") == 0) {
1697 type
= process_entry(event
, arg
, &token
);
1698 } else if (strcmp(token
, "__print_flags") == 0) {
1701 type
= process_flags(event
, arg
, &token
);
1702 } else if (strcmp(token
, "__print_symbolic") == 0) {
1704 is_symbolic_field
= 1;
1705 type
= process_symbols(event
, arg
, &token
);
1706 } else if (strcmp(token
, "__get_str") == 0) {
1708 type
= process_str(event
, arg
, &token
);
1711 /* test the next token */
1712 type
= read_token_item(&token
);
1714 /* atoms can be more than one token long */
1715 while (type
== EVENT_ITEM
) {
1716 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1718 strcat(atom
, token
);
1720 type
= read_token_item(&token
);
1723 /* todo, test for function */
1725 arg
->type
= PRINT_ATOM
;
1726 arg
->atom
.atom
= atom
;
1731 arg
->type
= PRINT_ATOM
;
1732 arg
->atom
.atom
= token
;
1733 type
= read_token_item(&token
);
1736 if (strcmp(token
, "(") == 0) {
1738 type
= process_paren(event
, arg
, &token
);
1742 /* handle single ops */
1743 arg
->type
= PRINT_OP
;
1745 arg
->op
.left
= NULL
;
1746 type
= process_op(event
, arg
, &token
);
1750 case EVENT_ERROR
... EVENT_NEWLINE
:
1752 die("unexpected type %d", type
);
1759 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1761 enum event_type type
= EVENT_ERROR
;
1762 struct print_arg
*arg
;
1767 if (type
== EVENT_NEWLINE
) {
1769 type
= read_token_item(&token
);
1773 arg
= malloc_or_die(sizeof(*arg
));
1774 memset(arg
, 0, sizeof(*arg
));
1776 type
= process_arg(event
, arg
, &token
);
1778 if (type
== EVENT_ERROR
) {
1786 if (type
== EVENT_OP
) {
1787 type
= process_op(event
, arg
, &token
);
1792 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1799 } while (type
!= EVENT_NONE
);
1801 if (type
!= EVENT_NONE
)
1807 static int event_read_print(struct event
*event
)
1809 enum event_type type
;
1813 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1816 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1819 if (read_expected(EVENT_OP
, ":") < 0)
1822 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1826 event
->print_fmt
.format
= token
;
1827 event
->print_fmt
.args
= NULL
;
1829 /* ok to have no arg */
1830 type
= read_token_item(&token
);
1832 if (type
== EVENT_NONE
)
1835 /* Handle concatination of print lines */
1836 if (type
== EVENT_DQUOTE
) {
1839 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1841 strcpy(cat
, event
->print_fmt
.format
);
1844 free_token(event
->print_fmt
.format
);
1845 event
->print_fmt
.format
= NULL
;
1850 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1855 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1866 static struct format_field
*
1867 find_common_field(struct event
*event
, const char *name
)
1869 struct format_field
*format
;
1871 for (format
= event
->format
.common_fields
;
1872 format
; format
= format
->next
) {
1873 if (strcmp(format
->name
, name
) == 0)
1880 static struct format_field
*
1881 find_field(struct event
*event
, const char *name
)
1883 struct format_field
*format
;
1885 for (format
= event
->format
.fields
;
1886 format
; format
= format
->next
) {
1887 if (strcmp(format
->name
, name
) == 0)
1894 static struct format_field
*
1895 find_any_field(struct event
*event
, const char *name
)
1897 struct format_field
*format
;
1899 format
= find_common_field(event
, name
);
1902 return find_field(event
, name
);
1905 unsigned long long read_size(void *ptr
, int size
)
1909 return *(unsigned char *)ptr
;
1911 return data2host2(ptr
);
1913 return data2host4(ptr
);
1915 return data2host8(ptr
);
1923 raw_field_value(struct event
*event
, const char *name
, void *data
)
1925 struct format_field
*field
;
1927 field
= find_any_field(event
, name
);
1931 return read_size(data
+ field
->offset
, field
->size
);
1934 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1936 struct format_field
*field
;
1938 field
= find_any_field(event
, name
);
1942 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1945 offset
= *(int *)(data
+ field
->offset
);
1948 return data
+ offset
;
1951 return data
+ field
->offset
;
1954 static int get_common_info(const char *type
, int *offset
, int *size
)
1956 struct event
*event
;
1957 struct format_field
*field
;
1960 * All events should have the same common elements.
1961 * Pick any event to find where the type is;
1964 die("no event_list!");
1967 field
= find_common_field(event
, type
);
1969 die("field '%s' not found", type
);
1971 *offset
= field
->offset
;
1972 *size
= field
->size
;
1977 static int __parse_common(void *data
, int *size
, int *offset
,
1983 ret
= get_common_info(name
, offset
, size
);
1987 return read_size(data
+ *offset
, *size
);
1990 int trace_parse_common_type(void *data
)
1992 static int type_offset
;
1993 static int type_size
;
1995 return __parse_common(data
, &type_size
, &type_offset
,
1999 int trace_parse_common_pid(void *data
)
2001 static int pid_offset
;
2002 static int pid_size
;
2004 return __parse_common(data
, &pid_size
, &pid_offset
,
2008 int parse_common_pc(void *data
)
2010 static int pc_offset
;
2013 return __parse_common(data
, &pc_size
, &pc_offset
,
2014 "common_preempt_count");
2017 int parse_common_flags(void *data
)
2019 static int flags_offset
;
2020 static int flags_size
;
2022 return __parse_common(data
, &flags_size
, &flags_offset
,
2026 int parse_common_lock_depth(void *data
)
2028 static int ld_offset
;
2032 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
2033 "common_lock_depth");
2040 struct event
*trace_find_event(int id
)
2042 struct event
*event
;
2044 for (event
= event_list
; event
; event
= event
->next
) {
2045 if (event
->id
== id
)
2051 struct event
*trace_find_next_event(struct event
*event
)
2059 static unsigned long long eval_num_arg(void *data
, int size
,
2060 struct event
*event
, struct print_arg
*arg
)
2062 unsigned long long val
= 0;
2063 unsigned long long left
, right
;
2064 struct print_arg
*larg
;
2066 switch (arg
->type
) {
2071 return strtoull(arg
->atom
.atom
, NULL
, 0);
2073 if (!arg
->field
.field
) {
2074 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2075 if (!arg
->field
.field
)
2076 die("field %s not found", arg
->field
.name
);
2078 /* must be a number */
2079 val
= read_size(data
+ arg
->field
.field
->offset
,
2080 arg
->field
.field
->size
);
2086 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2091 if (strcmp(arg
->op
.op
, "[") == 0) {
2093 * Arrays are special, since we don't want
2094 * to read the arg as is.
2096 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2097 goto default_op
; /* oops, all bets off */
2098 larg
= arg
->op
.left
;
2099 if (!larg
->field
.field
) {
2101 find_any_field(event
, larg
->field
.name
);
2102 if (!larg
->field
.field
)
2103 die("field %s not found", larg
->field
.name
);
2105 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2106 val
= read_size(data
+ larg
->field
.field
->offset
+
2107 right
* long_size
, long_size
);
2111 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2112 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2113 switch (arg
->op
.op
[0]) {
2116 val
= left
|| right
;
2122 val
= left
&& right
;
2127 switch (arg
->op
.op
[1]) {
2132 val
= left
<< right
;
2135 val
= left
<= right
;
2138 die("unknown op '%s'", arg
->op
.op
);
2142 switch (arg
->op
.op
[1]) {
2147 val
= left
>> right
;
2150 val
= left
>= right
;
2153 die("unknown op '%s'", arg
->op
.op
);
2157 if (arg
->op
.op
[1] != '=')
2158 die("unknown op '%s'", arg
->op
.op
);
2159 val
= left
== right
;
2168 die("unknown op '%s'", arg
->op
.op
);
2171 default: /* not sure what to do there */
2179 unsigned long long value
;
2182 static const struct flag flags
[] = {
2183 { "HI_SOFTIRQ", 0 },
2184 { "TIMER_SOFTIRQ", 1 },
2185 { "NET_TX_SOFTIRQ", 2 },
2186 { "NET_RX_SOFTIRQ", 3 },
2187 { "BLOCK_SOFTIRQ", 4 },
2188 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2189 { "TASKLET_SOFTIRQ", 6 },
2190 { "SCHED_SOFTIRQ", 7 },
2191 { "HRTIMER_SOFTIRQ", 8 },
2192 { "RCU_SOFTIRQ", 9 },
2194 { "HRTIMER_NORESTART", 0 },
2195 { "HRTIMER_RESTART", 1 },
2198 unsigned long long eval_flag(const char *flag
)
2203 * Some flags in the format files do not get converted.
2204 * If the flag is not numeric, see if it is something that
2205 * we already know about.
2207 if (isdigit(flag
[0]))
2208 return strtoull(flag
, NULL
, 0);
2210 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2211 if (strcmp(flags
[i
].name
, flag
) == 0)
2212 return flags
[i
].value
;
2217 static void print_str_arg(void *data
, int size
,
2218 struct event
*event
, struct print_arg
*arg
)
2220 struct print_flag_sym
*flag
;
2221 unsigned long long val
, fval
;
2225 switch (arg
->type
) {
2230 printf("%s", arg
->atom
.atom
);
2233 if (!arg
->field
.field
) {
2234 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2235 if (!arg
->field
.field
)
2236 die("field %s not found", arg
->field
.name
);
2238 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2239 memcpy(str
, data
+ arg
->field
.field
->offset
,
2240 arg
->field
.field
->size
);
2241 str
[arg
->field
.field
->size
] = 0;
2246 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2248 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2249 fval
= eval_flag(flag
->value
);
2250 if (!val
&& !fval
) {
2251 printf("%s", flag
->str
);
2254 if (fval
&& (val
& fval
) == fval
) {
2255 if (print
&& arg
->flags
.delim
)
2256 printf("%s", arg
->flags
.delim
);
2257 printf("%s", flag
->str
);
2264 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2265 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2266 fval
= eval_flag(flag
->value
);
2268 printf("%s", flag
->str
);
2276 case PRINT_STRING
: {
2279 if (arg
->string
.offset
== -1) {
2280 struct format_field
*f
;
2282 f
= find_any_field(event
, arg
->string
.string
);
2283 arg
->string
.offset
= f
->offset
;
2285 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2286 str_offset
&= 0xffff;
2287 printf("%s", ((char *)data
) + str_offset
);
2292 * The only op for string should be ? :
2294 if (arg
->op
.op
[0] != '?')
2296 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2298 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2300 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2308 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2310 static struct format_field
*field
, *ip_field
;
2311 struct print_arg
*args
, *arg
, **next
;
2312 unsigned long long ip
, val
;
2317 field
= find_field(event
, "buf");
2319 die("can't find buffer field for binary printk");
2320 ip_field
= find_field(event
, "ip");
2322 die("can't find ip field for binary printk");
2325 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2328 * The first arg is the IP pointer.
2330 args
= malloc_or_die(sizeof(*args
));
2335 arg
->type
= PRINT_ATOM
;
2336 arg
->atom
.atom
= malloc_or_die(32);
2337 sprintf(arg
->atom
.atom
, "%lld", ip
);
2339 /* skip the first "%pf : " */
2340 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2341 bptr
< data
+ size
&& *ptr
; ptr
++) {
2365 /* the pointers are always 4 bytes aligned */
2366 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2378 val
= read_size(bptr
, ls
);
2380 arg
= malloc_or_die(sizeof(*arg
));
2382 arg
->type
= PRINT_ATOM
;
2383 arg
->atom
.atom
= malloc_or_die(32);
2384 sprintf(arg
->atom
.atom
, "%lld", val
);
2389 arg
= malloc_or_die(sizeof(*arg
));
2391 arg
->type
= PRINT_STRING
;
2392 arg
->string
.string
= strdup(bptr
);
2393 bptr
+= strlen(bptr
) + 1;
2405 static void free_args(struct print_arg
*args
)
2407 struct print_arg
*next
;
2412 if (args
->type
== PRINT_ATOM
)
2413 free(args
->atom
.atom
);
2415 free(args
->string
.string
);
2421 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2423 unsigned long long addr
;
2424 static struct format_field
*field
;
2425 struct printk_map
*printk
;
2430 field
= find_field(event
, "fmt");
2432 die("can't find format field for binary printk");
2433 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2436 addr
= read_size(data
+ field
->offset
, field
->size
);
2438 printk
= find_printk(addr
);
2440 format
= malloc_or_die(45);
2441 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2447 /* Remove any quotes. */
2450 format
= malloc_or_die(strlen(p
) + 10);
2451 sprintf(format
, "%s : %s", "%pf", p
);
2452 /* remove ending quotes and new line since we will add one too */
2453 p
= format
+ strlen(format
) - 1;
2458 if (strcmp(p
, "\\n") == 0)
2464 static void pretty_print(void *data
, int size
, struct event
*event
)
2466 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2467 struct print_arg
*arg
= print_fmt
->args
;
2468 struct print_arg
*args
= NULL
;
2469 const char *ptr
= print_fmt
->format
;
2470 unsigned long long val
;
2471 struct func_map
*func
;
2472 const char *saveptr
;
2473 char *bprint_fmt
= NULL
;
2479 if (event
->flags
& EVENT_FL_ISFUNC
)
2480 ptr
= " %pF <-- %pF";
2482 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2483 bprint_fmt
= get_bprint_format(data
, size
, event
);
2484 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2489 for (; *ptr
; ptr
++) {
2511 } else if (*ptr
== '%') {
2536 if (*(ptr
+1) == 'F' ||
2549 die("no argument match");
2551 len
= ((unsigned long)ptr
+ 1) -
2552 (unsigned long)saveptr
;
2554 /* should never happen */
2558 memcpy(format
, saveptr
, len
);
2561 val
= eval_num_arg(data
, size
, event
, arg
);
2565 func
= find_func(val
);
2567 printf("%s", func
->func
);
2568 if (show_func
== 'F')
2576 printf(format
, (int)val
);
2579 printf(format
, (long)val
);
2582 printf(format
, (long long)val
);
2585 die("bad count (%d)", ls
);
2590 die("no matching argument");
2592 print_str_arg(data
, size
, event
, arg
);
2596 printf(">%c<", *ptr
);
2609 static inline int log10_cpu(int nb
)
2618 static void print_lat_fmt(void *data
, int size __unused
)
2620 unsigned int lat_flags
;
2626 lat_flags
= parse_common_flags(data
);
2627 pc
= parse_common_pc(data
);
2628 lock_depth
= parse_common_lock_depth(data
);
2630 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2631 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2634 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2635 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2637 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2639 (hardirq
&& softirq
) ? 'H' :
2640 hardirq
? 'h' : softirq
? 's' : '.');
2650 printf("%d ", lock_depth
);
2653 #define TRACE_GRAPH_INDENT 2
2655 static struct record
*
2656 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2657 struct record
*next
)
2659 struct format_field
*field
;
2660 struct event
*event
;
2665 type
= trace_parse_common_type(next
->data
);
2666 event
= trace_find_event(type
);
2670 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2673 pid
= trace_parse_common_pid(next
->data
);
2674 field
= find_field(event
, "func");
2676 die("function return does not have field func");
2678 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2680 if (cur_pid
!= pid
|| cur_func
!= val
)
2683 /* this is a leaf, now advance the iterator */
2684 return trace_read_data(cpu
);
2687 /* Signal a overhead of time execution to the output */
2688 static void print_graph_overhead(unsigned long long duration
)
2690 /* Non nested entry or return */
2691 if (duration
== ~0ULL)
2692 return (void)printf(" ");
2694 /* Duration exceeded 100 msecs */
2695 if (duration
> 100000ULL)
2696 return (void)printf("! ");
2698 /* Duration exceeded 10 msecs */
2699 if (duration
> 10000ULL)
2700 return (void)printf("+ ");
2705 static void print_graph_duration(unsigned long long duration
)
2707 unsigned long usecs
= duration
/ 1000;
2708 unsigned long nsecs_rem
= duration
% 1000;
2709 /* log10(ULONG_MAX) + '\0' */
2715 sprintf(msecs_str
, "%lu", usecs
);
2718 len
= printf("%lu", usecs
);
2720 /* Print nsecs (we don't want to exceed 7 numbers) */
2722 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2723 len
+= printf(".%s", nsecs_str
);
2728 /* Print remaining spaces to fit the row's width */
2729 for (i
= len
; i
< 7; i
++)
2736 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2738 unsigned long long rettime
, calltime
;
2739 unsigned long long duration
, depth
;
2740 unsigned long long val
;
2741 struct format_field
*field
;
2742 struct func_map
*func
;
2743 struct event
*ret_event
;
2747 type
= trace_parse_common_type(ret_rec
->data
);
2748 ret_event
= trace_find_event(type
);
2750 field
= find_field(ret_event
, "rettime");
2752 die("can't find rettime in return graph");
2753 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2755 field
= find_field(ret_event
, "calltime");
2757 die("can't find rettime in return graph");
2758 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2760 duration
= rettime
- calltime
;
2763 print_graph_overhead(duration
);
2766 print_graph_duration(duration
);
2768 field
= find_field(event
, "depth");
2770 die("can't find depth in entry graph");
2771 depth
= read_size(data
+ field
->offset
, field
->size
);
2774 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2777 field
= find_field(event
, "func");
2779 die("can't find func in entry graph");
2780 val
= read_size(data
+ field
->offset
, field
->size
);
2781 func
= find_func(val
);
2784 printf("%s();", func
->func
);
2786 printf("%llx();", val
);
2789 static void print_graph_nested(struct event
*event
, void *data
)
2791 struct format_field
*field
;
2792 unsigned long long depth
;
2793 unsigned long long val
;
2794 struct func_map
*func
;
2798 print_graph_overhead(-1);
2803 field
= find_field(event
, "depth");
2805 die("can't find depth in entry graph");
2806 depth
= read_size(data
+ field
->offset
, field
->size
);
2809 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2812 field
= find_field(event
, "func");
2814 die("can't find func in entry graph");
2815 val
= read_size(data
+ field
->offset
, field
->size
);
2816 func
= find_func(val
);
2819 printf("%s() {", func
->func
);
2821 printf("%llx() {", val
);
2825 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2828 struct format_field
*field
;
2833 if (latency_format
) {
2834 print_lat_fmt(data
, size
);
2838 field
= find_field(event
, "func");
2840 die("function entry does not have func field");
2842 val
= read_size(data
+ field
->offset
, field
->size
);
2845 * peek_data may unmap the data pointer. Copy it first.
2847 copy_data
= malloc_or_die(size
);
2848 memcpy(copy_data
, data
, size
);
2851 rec
= trace_peek_data(cpu
);
2853 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2855 print_graph_entry_leaf(event
, data
, rec
);
2859 print_graph_nested(event
, data
);
2865 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
)
2867 unsigned long long rettime
, calltime
;
2868 unsigned long long duration
, depth
;
2869 struct format_field
*field
;
2872 if (latency_format
) {
2873 print_lat_fmt(data
, size
);
2877 field
= find_field(event
, "rettime");
2879 die("can't find rettime in return graph");
2880 rettime
= read_size(data
+ field
->offset
, field
->size
);
2882 field
= find_field(event
, "calltime");
2884 die("can't find calltime in return graph");
2885 calltime
= read_size(data
+ field
->offset
, field
->size
);
2887 duration
= rettime
- calltime
;
2890 print_graph_overhead(duration
);
2893 print_graph_duration(duration
);
2895 field
= find_field(event
, "depth");
2897 die("can't find depth in entry graph");
2898 depth
= read_size(data
+ field
->offset
, field
->size
);
2901 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2908 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2911 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2912 pretty_print_func_ent(data
, size
, event
, cpu
, pid
);
2913 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2914 pretty_print_func_ret(data
, size
, event
);
2918 void print_trace_event(int cpu
, void *data
, int size
)
2920 struct event
*event
;
2924 type
= trace_parse_common_type(data
);
2926 event
= trace_find_event(type
);
2928 warning("ug! no event found for type %d", type
);
2932 pid
= trace_parse_common_pid(data
);
2934 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2935 return pretty_print_func_graph(data
, size
, event
, cpu
, pid
);
2938 print_lat_fmt(data
, size
);
2940 if (event
->flags
& EVENT_FL_FAILED
) {
2941 printf("EVENT '%s' FAILED TO PARSE\n",
2946 pretty_print(data
, size
, event
);
2949 static void print_fields(struct print_flag_sym
*field
)
2951 printf("{ %s, %s }", field
->value
, field
->str
);
2954 print_fields(field
->next
);
2958 static void print_args(struct print_arg
*args
)
2960 int print_paren
= 1;
2962 switch (args
->type
) {
2967 printf("%s", args
->atom
.atom
);
2970 printf("REC->%s", args
->field
.name
);
2973 printf("__print_flags(");
2974 print_args(args
->flags
.field
);
2975 printf(", %s, ", args
->flags
.delim
);
2976 print_fields(args
->flags
.flags
);
2980 printf("__print_symbolic(");
2981 print_args(args
->symbol
.field
);
2983 print_fields(args
->symbol
.symbols
);
2987 printf("__get_str(%s)", args
->string
.string
);
2990 printf("(%s)", args
->typecast
.type
);
2991 print_args(args
->typecast
.item
);
2994 if (strcmp(args
->op
.op
, ":") == 0)
2998 print_args(args
->op
.left
);
2999 printf(" %s ", args
->op
.op
);
3000 print_args(args
->op
.right
);
3005 /* we should warn... */
3010 print_args(args
->next
);
3014 int parse_ftrace_file(char *buf
, unsigned long size
)
3016 struct format_field
*field
;
3017 struct print_arg
*arg
, **list
;
3018 struct event
*event
;
3021 init_input_buf(buf
, size
);
3023 event
= alloc_event();
3027 event
->flags
|= EVENT_FL_ISFTRACE
;
3029 event
->name
= event_read_name();
3031 die("failed to read ftrace event name");
3033 if (strcmp(event
->name
, "function") == 0)
3034 event
->flags
|= EVENT_FL_ISFUNC
;
3036 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3037 event
->flags
|= EVENT_FL_ISFUNCENT
;
3039 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3040 event
->flags
|= EVENT_FL_ISFUNCRET
;
3042 else if (strcmp(event
->name
, "bprint") == 0)
3043 event
->flags
|= EVENT_FL_ISBPRINT
;
3045 event
->id
= event_read_id();
3047 die("failed to read ftrace event id");
3051 ret
= event_read_format(event
);
3053 die("failed to read ftrace event format");
3055 ret
= event_read_print(event
);
3057 die("failed to read ftrace event print fmt");
3059 /* New ftrace handles args */
3063 * The arguments for ftrace files are parsed by the fields.
3064 * Set up the fields as their arguments.
3066 list
= &event
->print_fmt
.args
;
3067 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3068 arg
= malloc_or_die(sizeof(*arg
));
3069 memset(arg
, 0, sizeof(*arg
));
3072 arg
->type
= PRINT_FIELD
;
3073 arg
->field
.name
= field
->name
;
3074 arg
->field
.field
= field
;
3079 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3081 struct event
*event
;
3084 init_input_buf(buf
, size
);
3086 event
= alloc_event();
3090 event
->name
= event_read_name();
3092 die("failed to read event name");
3094 event
->id
= event_read_id();
3096 die("failed to read event id");
3098 ret
= event_read_format(event
);
3100 warning("failed to read event format for %s", event
->name
);
3104 ret
= event_read_print(event
);
3106 warning("failed to read event print fmt for %s", event
->name
);
3110 event
->system
= strdup(sys
);
3112 #define PRINT_ARGS 0
3113 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3114 print_args(event
->print_fmt
.args
);
3120 event
->flags
|= EVENT_FL_FAILED
;
3121 /* still add it even if it failed */
3126 void parse_set_info(int nr_cpus
, int long_sz
)
3129 long_size
= long_sz
;
3132 int common_pc(struct scripting_context
*context
)
3134 return parse_common_pc(context
->event_data
);
3137 int common_flags(struct scripting_context
*context
)
3139 return parse_common_flags(context
->event_data
);
3142 int common_lock_depth(struct scripting_context
*context
)
3144 return parse_common_lock_depth(context
->event_data
);