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_data_offset
;
41 int header_page_data_size
;
43 static char *input_buf
;
44 static unsigned long long input_buf_ptr
;
45 static unsigned long long input_buf_siz
;
50 static void init_input_buf(char *buf
, unsigned long long size
)
62 static struct cmdline
*cmdlines
;
63 static int cmdline_count
;
65 static int cmdline_cmp(const void *a
, const void *b
)
67 const struct cmdline
*ca
= a
;
68 const struct cmdline
*cb
= b
;
70 if (ca
->pid
< cb
->pid
)
72 if (ca
->pid
> cb
->pid
)
78 void parse_cmdlines(char *file
, int size __unused
)
81 struct cmdline_list
*next
;
84 } *list
= NULL
, *item
;
89 line
= strtok_r(file
, "\n", &next
);
91 item
= malloc_or_die(sizeof(*item
));
92 sscanf(line
, "%d %as", &item
->pid
,
93 (float *)(void *)&item
->comm
); /* workaround gcc warning */
96 line
= strtok_r(NULL
, "\n", &next
);
100 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
104 cmdlines
[i
].pid
= list
->pid
;
105 cmdlines
[i
].comm
= list
->comm
;
112 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
115 static struct func_map
{
116 unsigned long long addr
;
120 static unsigned int func_count
;
122 static int func_cmp(const void *a
, const void *b
)
124 const struct func_map
*fa
= a
;
125 const struct func_map
*fb
= b
;
127 if (fa
->addr
< fb
->addr
)
129 if (fa
->addr
> fb
->addr
)
135 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
138 struct func_list
*next
;
139 unsigned long long addr
;
142 } *list
= NULL
, *item
;
150 line
= strtok_r(file
, "\n", &next
);
152 item
= malloc_or_die(sizeof(*item
));
154 ret
= sscanf(line
, "%as %c %as\t[%as",
155 (float *)(void *)&addr_str
, /* workaround gcc warning */
157 (float *)(void *)&item
->func
,
158 (float *)(void *)&item
->mod
);
159 item
->addr
= strtoull(addr_str
, NULL
, 16);
162 /* truncate the extra ']' */
164 item
->mod
[strlen(item
->mod
) - 1] = 0;
169 line
= strtok_r(NULL
, "\n", &next
);
173 func_list
= malloc_or_die(sizeof(*func_list
) * func_count
+ 1);
177 func_list
[i
].func
= list
->func
;
178 func_list
[i
].addr
= list
->addr
;
179 func_list
[i
].mod
= list
->mod
;
186 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
189 * Add a special record at the end.
191 func_list
[func_count
].func
= NULL
;
192 func_list
[func_count
].addr
= 0;
193 func_list
[func_count
].mod
= NULL
;
197 * We are searching for a record in between, not an exact
200 static int func_bcmp(const void *a
, const void *b
)
202 const struct func_map
*fa
= a
;
203 const struct func_map
*fb
= b
;
205 if ((fa
->addr
== fb
->addr
) ||
207 (fa
->addr
> fb
->addr
&&
208 fa
->addr
< (fb
+1)->addr
))
211 if (fa
->addr
< fb
->addr
)
217 static struct func_map
*find_func(unsigned long long addr
)
219 struct func_map
*func
;
224 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
230 void print_funcs(void)
234 for (i
= 0; i
< (int)func_count
; i
++) {
238 if (func_list
[i
].mod
)
239 printf(" [%s]\n", func_list
[i
].mod
);
245 static struct printk_map
{
246 unsigned long long addr
;
249 static unsigned int printk_count
;
251 static int printk_cmp(const void *a
, const void *b
)
253 const struct func_map
*fa
= a
;
254 const struct func_map
*fb
= b
;
256 if (fa
->addr
< fb
->addr
)
258 if (fa
->addr
> fb
->addr
)
264 static struct printk_map
*find_printk(unsigned long long addr
)
266 struct printk_map
*printk
;
267 struct printk_map key
;
271 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
277 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
280 struct printk_list
*next
;
281 unsigned long long addr
;
283 } *list
= NULL
, *item
;
290 line
= strtok_r(file
, "\n", &next
);
292 item
= malloc_or_die(sizeof(*item
));
293 ret
= sscanf(line
, "%as : %as",
294 (float *)(void *)&addr_str
, /* workaround gcc warning */
295 (float *)(void *)&item
->printk
);
296 item
->addr
= strtoull(addr_str
, NULL
, 16);
301 line
= strtok_r(NULL
, "\n", &next
);
305 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
309 printk_list
[i
].printk
= list
->printk
;
310 printk_list
[i
].addr
= list
->addr
;
317 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
320 void print_printk(void)
324 for (i
= 0; i
< (int)printk_count
; i
++) {
325 printf("%016llx %s\n",
327 printk_list
[i
].printk
);
331 static struct event
*alloc_event(void)
335 event
= malloc_or_die(sizeof(*event
));
336 memset(event
, 0, sizeof(*event
));
353 static struct event
*event_list
;
355 static void add_event(struct event
*event
)
357 event
->next
= event_list
;
361 static int event_item_type(enum event_type type
)
364 case EVENT_ITEM
... EVENT_SQUOTE
:
366 case EVENT_ERROR
... EVENT_DELIM
:
372 static void free_arg(struct print_arg
*arg
)
380 free(arg
->atom
.atom
);
383 case PRINT_FIELD
... PRINT_OP
:
392 static enum event_type
get_type(int ch
)
395 return EVENT_NEWLINE
;
398 if (isalnum(ch
) || ch
== '_')
406 if (ch
== '(' || ch
== ')' || ch
== ',')
412 static int __read_char(void)
414 if (input_buf_ptr
>= input_buf_siz
)
417 return input_buf
[input_buf_ptr
++];
420 static int __peek_char(void)
422 if (input_buf_ptr
>= input_buf_siz
)
425 return input_buf
[input_buf_ptr
];
428 static enum event_type
__read_token(char **tok
)
431 int ch
, last_ch
, quote_ch
, next_ch
;
434 enum event_type type
;
444 if (type
== EVENT_NONE
)
452 *tok
= malloc_or_die(2);
460 next_ch
= __peek_char();
461 if (next_ch
== '>') {
462 buf
[i
++] = __read_char();
475 buf
[i
++] = __read_char();
487 default: /* what should we do instead? */
497 buf
[i
++] = __read_char();
502 /* don't keep quotes */
507 if (i
== (BUFSIZ
- 1)) {
510 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
525 /* the '\' '\' will cancel itself */
526 if (ch
== '\\' && last_ch
== '\\')
528 } while (ch
!= quote_ch
|| last_ch
== '\\');
529 /* remove the last quote */
533 case EVENT_ERROR
... EVENT_SPACE
:
539 while (get_type(__peek_char()) == type
) {
540 if (i
== (BUFSIZ
- 1)) {
543 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
562 *tok
= realloc(*tok
, tok_size
+ i
);
574 static void free_token(char *tok
)
580 static enum event_type
read_token(char **tok
)
582 enum event_type type
;
585 type
= __read_token(tok
);
586 if (type
!= EVENT_SPACE
)
597 static enum event_type
read_token_item(char **tok
)
599 enum event_type type
;
602 type
= __read_token(tok
);
603 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
613 static int test_type(enum event_type type
, enum event_type expect
)
615 if (type
!= expect
) {
616 die("Error: expected type %d but read %d",
623 static int test_type_token(enum event_type type
, char *token
,
624 enum event_type expect
, const char *expect_tok
)
626 if (type
!= expect
) {
627 die("Error: expected type %d but read %d",
632 if (strcmp(token
, expect_tok
) != 0) {
633 die("Error: expected '%s' but read '%s'",
640 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
642 enum event_type type
;
645 type
= read_token(tok
);
647 type
= read_token_item(tok
);
648 return test_type(type
, expect
);
651 static int read_expect_type(enum event_type expect
, char **tok
)
653 return __read_expect_type(expect
, tok
, 1);
656 static int __read_expected(enum event_type expect
, const char *str
, int newline_ok
)
658 enum event_type type
;
663 type
= read_token(&token
);
665 type
= read_token_item(&token
);
667 ret
= test_type_token(type
, token
, expect
, str
);
674 static int read_expected(enum event_type expect
, const char *str
)
676 return __read_expected(expect
, str
, 1);
679 static int read_expected_item(enum event_type expect
, const char *str
)
681 return __read_expected(expect
, str
, 0);
684 static char *event_read_name(void)
688 if (read_expected(EVENT_ITEM
, (char *)"name") < 0)
691 if (read_expected(EVENT_OP
, (char *)":") < 0)
694 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
704 static int event_read_id(void)
709 if (read_expected_item(EVENT_ITEM
, (char *)"ID") < 0)
712 if (read_expected(EVENT_OP
, (char *)":") < 0)
715 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
718 id
= strtoul(token
, NULL
, 0);
727 static int field_is_string(struct format_field
*field
)
729 if ((field
->flags
& FIELD_IS_ARRAY
) &&
730 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
731 !strstr(field
->type
, "s8")))
737 static int field_is_dynamic(struct format_field
*field
)
739 if (!strcmp(field
->type
, "__data_loc"))
745 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
747 struct format_field
*field
= NULL
;
748 enum event_type type
;
754 type
= read_token(&token
);
755 if (type
== EVENT_NEWLINE
) {
762 if (test_type_token(type
, token
, EVENT_ITEM
, (char *)"field"))
766 type
= read_token(&token
);
768 * The ftrace fields may still use the "special" name.
771 if (event
->flags
& EVENT_FL_ISFTRACE
&&
772 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
774 type
= read_token(&token
);
777 if (test_type_token(type
, token
, EVENT_OP
, (char *)":") < 0)
780 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
785 field
= malloc_or_die(sizeof(*field
));
786 memset(field
, 0, sizeof(*field
));
788 /* read the rest of the type */
790 type
= read_token(&token
);
791 if (type
== EVENT_ITEM
||
792 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
794 * Some of the ftrace fields are broken and have
795 * an illegal "." in them.
797 (event
->flags
& EVENT_FL_ISFTRACE
&&
798 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
800 if (strcmp(token
, "*") == 0)
801 field
->flags
|= FIELD_IS_POINTER
;
804 field
->type
= realloc(field
->type
,
805 strlen(field
->type
) +
806 strlen(last_token
) + 2);
807 strcat(field
->type
, " ");
808 strcat(field
->type
, last_token
);
810 field
->type
= last_token
;
819 die("no type found");
822 field
->name
= last_token
;
824 if (test_type(type
, EVENT_OP
))
827 if (strcmp(token
, "[") == 0) {
828 enum event_type last_type
= type
;
829 char *brackets
= token
;
832 field
->flags
|= FIELD_IS_ARRAY
;
834 type
= read_token(&token
);
835 while (strcmp(token
, "]") != 0) {
836 if (last_type
== EVENT_ITEM
&&
843 brackets
= realloc(brackets
,
845 strlen(token
) + len
);
847 strcat(brackets
, " ");
848 strcat(brackets
, token
);
850 type
= read_token(&token
);
851 if (type
== EVENT_NONE
) {
852 die("failed to find token");
859 brackets
= realloc(brackets
, strlen(brackets
) + 2);
860 strcat(brackets
, "]");
862 /* add brackets to type */
864 type
= read_token(&token
);
866 * If the next token is not an OP, then it is of
867 * the format: type [] item;
869 if (type
== EVENT_ITEM
) {
870 field
->type
= realloc(field
->type
,
871 strlen(field
->type
) +
872 strlen(field
->name
) +
873 strlen(brackets
) + 2);
874 strcat(field
->type
, " ");
875 strcat(field
->type
, field
->name
);
876 free_token(field
->name
);
877 strcat(field
->type
, brackets
);
879 type
= read_token(&token
);
881 field
->type
= realloc(field
->type
,
882 strlen(field
->type
) +
883 strlen(brackets
) + 1);
884 strcat(field
->type
, brackets
);
889 if (field_is_string(field
)) {
890 field
->flags
|= FIELD_IS_STRING
;
891 if (field_is_dynamic(field
))
892 field
->flags
|= FIELD_IS_DYNAMIC
;
895 if (test_type_token(type
, token
, EVENT_OP
, (char *)";"))
899 if (read_expected(EVENT_ITEM
, (char *)"offset") < 0)
902 if (read_expected(EVENT_OP
, (char *)":") < 0)
905 if (read_expect_type(EVENT_ITEM
, &token
))
907 field
->offset
= strtoul(token
, NULL
, 0);
910 if (read_expected(EVENT_OP
, (char *)";") < 0)
913 if (read_expected(EVENT_ITEM
, (char *)"size") < 0)
916 if (read_expected(EVENT_OP
, (char *)":") < 0)
919 if (read_expect_type(EVENT_ITEM
, &token
))
921 field
->size
= strtoul(token
, NULL
, 0);
924 if (read_expected(EVENT_OP
, (char *)";") < 0)
927 if (read_expected(EVENT_ITEM
, (char *)"signed") < 0)
930 if (read_expected(EVENT_OP
, (char *)":") < 0)
933 if (read_expect_type(EVENT_ITEM
, &token
))
935 if (strtoul(token
, NULL
, 0))
936 field
->flags
|= FIELD_IS_SIGNED
;
939 if (read_expected(EVENT_OP
, (char *)";") < 0)
942 if (read_expect_type(EVENT_NEWLINE
, &token
) < 0)
947 fields
= &field
->next
;
961 static int event_read_format(struct event
*event
)
966 if (read_expected_item(EVENT_ITEM
, (char *)"format") < 0)
969 if (read_expected(EVENT_OP
, (char *)":") < 0)
972 if (read_expect_type(EVENT_NEWLINE
, &token
))
976 ret
= event_read_fields(event
, &event
->format
.common_fields
);
979 event
->format
.nr_common
= ret
;
981 ret
= event_read_fields(event
, &event
->format
.fields
);
984 event
->format
.nr_fields
= ret
;
994 process_arg_token(struct event
*event
, struct print_arg
*arg
,
995 char **tok
, enum event_type type
);
997 static enum event_type
998 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1000 enum event_type type
;
1003 type
= read_token(&token
);
1006 return process_arg_token(event
, arg
, tok
, type
);
1009 static enum event_type
1010 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1012 struct print_arg
*arg
, *left
, *right
;
1013 enum event_type type
;
1016 arg
= malloc_or_die(sizeof(*arg
));
1017 memset(arg
, 0, sizeof(*arg
));
1019 left
= malloc_or_die(sizeof(*left
));
1021 right
= malloc_or_die(sizeof(*right
));
1023 arg
->type
= PRINT_OP
;
1024 arg
->op
.left
= left
;
1025 arg
->op
.right
= right
;
1028 type
= process_arg(event
, left
, &token
);
1029 if (test_type_token(type
, token
, EVENT_OP
, (char *)":"))
1034 type
= process_arg(event
, right
, &token
);
1036 top
->op
.right
= arg
;
1049 static enum event_type
1050 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1052 struct print_arg
*arg
;
1053 enum event_type type
;
1056 arg
= malloc_or_die(sizeof(*arg
));
1057 memset(arg
, 0, sizeof(*arg
));
1060 type
= process_arg(event
, arg
, &token
);
1061 if (test_type_token(type
, token
, EVENT_OP
, (char *)"]"))
1064 top
->op
.right
= arg
;
1067 type
= read_token_item(&token
);
1078 static int get_op_prio(char *op
)
1089 /* '>>' and '<<' are 8 */
1093 /* '==' and '!=' are 10 */
1103 die("unknown op '%c'", op
[0]);
1107 if (strcmp(op
, "++") == 0 ||
1108 strcmp(op
, "--") == 0) {
1110 } else if (strcmp(op
, ">>") == 0 ||
1111 strcmp(op
, "<<") == 0) {
1113 } else if (strcmp(op
, ">=") == 0 ||
1114 strcmp(op
, "<=") == 0) {
1116 } else if (strcmp(op
, "==") == 0 ||
1117 strcmp(op
, "!=") == 0) {
1119 } else if (strcmp(op
, "&&") == 0) {
1121 } else if (strcmp(op
, "||") == 0) {
1124 die("unknown op '%s'", op
);
1130 static void set_op_prio(struct print_arg
*arg
)
1133 /* single ops are the greatest */
1134 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1139 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1142 static enum event_type
1143 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1145 struct print_arg
*left
, *right
= NULL
;
1146 enum event_type type
;
1149 /* the op is passed in via tok */
1152 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1153 /* handle single op */
1155 die("bad op token %s", token
);
1164 die("bad op token %s", token
);
1168 /* make an empty left */
1169 left
= malloc_or_die(sizeof(*left
));
1170 left
->type
= PRINT_NULL
;
1171 arg
->op
.left
= left
;
1173 right
= malloc_or_die(sizeof(*right
));
1174 arg
->op
.right
= right
;
1176 type
= process_arg(event
, right
, tok
);
1178 } else if (strcmp(token
, "?") == 0) {
1180 left
= malloc_or_die(sizeof(*left
));
1181 /* copy the top arg to the left */
1184 arg
->type
= PRINT_OP
;
1186 arg
->op
.left
= left
;
1189 type
= process_cond(event
, arg
, tok
);
1191 } else if (strcmp(token
, ">>") == 0 ||
1192 strcmp(token
, "<<") == 0 ||
1193 strcmp(token
, "&") == 0 ||
1194 strcmp(token
, "|") == 0 ||
1195 strcmp(token
, "&&") == 0 ||
1196 strcmp(token
, "||") == 0 ||
1197 strcmp(token
, "-") == 0 ||
1198 strcmp(token
, "+") == 0 ||
1199 strcmp(token
, "*") == 0 ||
1200 strcmp(token
, "^") == 0 ||
1201 strcmp(token
, "/") == 0 ||
1202 strcmp(token
, "<") == 0 ||
1203 strcmp(token
, ">") == 0 ||
1204 strcmp(token
, "==") == 0 ||
1205 strcmp(token
, "!=") == 0) {
1207 left
= malloc_or_die(sizeof(*left
));
1209 /* copy the top arg to the left */
1212 arg
->type
= PRINT_OP
;
1214 arg
->op
.left
= left
;
1218 right
= malloc_or_die(sizeof(*right
));
1220 type
= read_token_item(&token
);
1223 /* could just be a type pointer */
1224 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1225 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1226 if (left
->type
!= PRINT_ATOM
)
1227 die("bad pointer type");
1228 left
->atom
.atom
= realloc(left
->atom
.atom
,
1229 sizeof(left
->atom
.atom
) + 3);
1230 strcat(left
->atom
.atom
, " *");
1237 type
= process_arg_token(event
, right
, tok
, type
);
1239 arg
->op
.right
= right
;
1241 } else if (strcmp(token
, "[") == 0) {
1243 left
= malloc_or_die(sizeof(*left
));
1246 arg
->type
= PRINT_OP
;
1248 arg
->op
.left
= left
;
1251 type
= process_array(event
, arg
, tok
);
1254 die("unknown op '%s'", token
);
1255 /* the arg is now the left side */
1260 if (type
== EVENT_OP
) {
1263 /* higher prios need to be closer to the root */
1264 prio
= get_op_prio(*tok
);
1266 if (prio
> arg
->op
.prio
)
1267 return process_op(event
, arg
, tok
);
1269 return process_op(event
, right
, tok
);
1275 static enum event_type
1276 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1279 enum event_type type
;
1283 if (read_expected(EVENT_OP
, (char *)"->") < 0)
1286 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1290 arg
->type
= PRINT_FIELD
;
1291 arg
->field
.name
= field
;
1293 type
= read_token(&token
);
1303 static char *arg_eval (struct print_arg
*arg
);
1305 static long long arg_num_eval(struct print_arg
*arg
)
1307 long long left
, right
;
1310 switch (arg
->type
) {
1312 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1315 val
= arg_num_eval(arg
->typecast
.item
);
1318 switch (arg
->op
.op
[0]) {
1320 left
= arg_num_eval(arg
->op
.left
);
1321 right
= arg_num_eval(arg
->op
.right
);
1323 val
= left
|| right
;
1328 left
= arg_num_eval(arg
->op
.left
);
1329 right
= arg_num_eval(arg
->op
.right
);
1331 val
= left
&& right
;
1336 left
= arg_num_eval(arg
->op
.left
);
1337 right
= arg_num_eval(arg
->op
.right
);
1338 switch (arg
->op
.op
[1]) {
1343 val
= left
<< right
;
1346 val
= left
<= right
;
1349 die("unknown op '%s'", arg
->op
.op
);
1353 left
= arg_num_eval(arg
->op
.left
);
1354 right
= arg_num_eval(arg
->op
.right
);
1355 switch (arg
->op
.op
[1]) {
1360 val
= left
>> right
;
1363 val
= left
>= right
;
1366 die("unknown op '%s'", arg
->op
.op
);
1370 left
= arg_num_eval(arg
->op
.left
);
1371 right
= arg_num_eval(arg
->op
.right
);
1373 if (arg
->op
.op
[1] != '=')
1374 die("unknown op '%s'", arg
->op
.op
);
1376 val
= left
== right
;
1379 left
= arg_num_eval(arg
->op
.left
);
1380 right
= arg_num_eval(arg
->op
.right
);
1382 switch (arg
->op
.op
[1]) {
1384 val
= left
!= right
;
1387 die("unknown op '%s'", arg
->op
.op
);
1391 die("unknown op '%s'", arg
->op
.op
);
1396 case PRINT_FIELD
... PRINT_SYMBOL
:
1399 die("invalid eval type %d", arg
->type
);
1405 static char *arg_eval (struct print_arg
*arg
)
1408 static char buf
[20];
1410 switch (arg
->type
) {
1412 return arg
->atom
.atom
;
1414 return arg_eval(arg
->typecast
.item
);
1416 val
= arg_num_eval(arg
);
1417 sprintf(buf
, "%lld", val
);
1421 case PRINT_FIELD
... PRINT_SYMBOL
:
1424 die("invalid eval type %d", arg
->type
);
1431 static enum event_type
1432 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1434 enum event_type type
;
1435 struct print_arg
*arg
= NULL
;
1436 struct print_flag_sym
*field
;
1442 type
= read_token_item(&token
);
1443 if (test_type_token(type
, token
, EVENT_OP
, (char *)"{"))
1446 arg
= malloc_or_die(sizeof(*arg
));
1449 type
= process_arg(event
, arg
, &token
);
1450 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1453 field
= malloc_or_die(sizeof(*field
));
1454 memset(field
, 0, sizeof(field
));
1456 value
= arg_eval(arg
);
1457 field
->value
= strdup(value
);
1460 type
= process_arg(event
, arg
, &token
);
1461 if (test_type_token(type
, token
, EVENT_OP
, (char *)"}"))
1464 value
= arg_eval(arg
);
1465 field
->str
= strdup(value
);
1470 list
= &field
->next
;
1473 type
= read_token_item(&token
);
1474 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1486 static enum event_type
1487 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1489 struct print_arg
*field
;
1490 enum event_type type
;
1493 memset(arg
, 0, sizeof(*arg
));
1494 arg
->type
= PRINT_FLAGS
;
1496 if (read_expected_item(EVENT_DELIM
, (char *)"(") < 0)
1499 field
= malloc_or_die(sizeof(*field
));
1501 type
= process_arg(event
, field
, &token
);
1502 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1505 arg
->flags
.field
= field
;
1507 type
= read_token_item(&token
);
1508 if (event_item_type(type
)) {
1509 arg
->flags
.delim
= token
;
1510 type
= read_token_item(&token
);
1513 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1516 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1517 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")"))
1521 type
= read_token_item(tok
);
1529 static enum event_type
1530 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1532 struct print_arg
*field
;
1533 enum event_type type
;
1536 memset(arg
, 0, sizeof(*arg
));
1537 arg
->type
= PRINT_SYMBOL
;
1539 if (read_expected_item(EVENT_DELIM
, (char *)"(") < 0)
1542 field
= malloc_or_die(sizeof(*field
));
1544 type
= process_arg(event
, field
, &token
);
1545 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1548 arg
->symbol
.field
= field
;
1550 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1551 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")"))
1555 type
= read_token_item(tok
);
1563 static enum event_type
1564 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1566 struct print_arg
*item_arg
;
1567 enum event_type type
;
1570 type
= process_arg(event
, arg
, &token
);
1572 if (type
== EVENT_ERROR
)
1575 if (type
== EVENT_OP
)
1576 type
= process_op(event
, arg
, &token
);
1578 if (type
== EVENT_ERROR
)
1581 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")")) {
1587 type
= read_token_item(&token
);
1590 * If the next token is an item or another open paren, then
1591 * this was a typecast.
1593 if (event_item_type(type
) ||
1594 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1596 /* make this a typecast and contine */
1598 /* prevous must be an atom */
1599 if (arg
->type
!= PRINT_ATOM
)
1600 die("previous needed to be PRINT_ATOM");
1602 item_arg
= malloc_or_die(sizeof(*item_arg
));
1604 arg
->type
= PRINT_TYPE
;
1605 arg
->typecast
.type
= arg
->atom
.atom
;
1606 arg
->typecast
.item
= item_arg
;
1607 type
= process_arg_token(event
, item_arg
, &token
, type
);
1616 static enum event_type
1617 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1619 enum event_type type
;
1622 if (read_expected(EVENT_DELIM
, (char *)"(") < 0)
1625 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1628 arg
->type
= PRINT_STRING
;
1629 arg
->string
.string
= token
;
1630 arg
->string
.offset
= -1;
1632 if (read_expected(EVENT_DELIM
, (char *)")") < 0)
1635 type
= read_token(&token
);
1645 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1646 char **tok
, enum event_type type
)
1655 if (strcmp(token
, "REC") == 0) {
1657 type
= process_entry(event
, arg
, &token
);
1658 } else if (strcmp(token
, "__print_flags") == 0) {
1660 type
= process_flags(event
, arg
, &token
);
1661 } else if (strcmp(token
, "__print_symbolic") == 0) {
1663 type
= process_symbols(event
, arg
, &token
);
1664 } else if (strcmp(token
, "__get_str") == 0) {
1666 type
= process_str(event
, arg
, &token
);
1669 /* test the next token */
1670 type
= read_token_item(&token
);
1672 /* atoms can be more than one token long */
1673 while (type
== EVENT_ITEM
) {
1674 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1676 strcat(atom
, token
);
1678 type
= read_token_item(&token
);
1681 /* todo, test for function */
1683 arg
->type
= PRINT_ATOM
;
1684 arg
->atom
.atom
= atom
;
1689 arg
->type
= PRINT_ATOM
;
1690 arg
->atom
.atom
= token
;
1691 type
= read_token_item(&token
);
1694 if (strcmp(token
, "(") == 0) {
1696 type
= process_paren(event
, arg
, &token
);
1700 /* handle single ops */
1701 arg
->type
= PRINT_OP
;
1703 arg
->op
.left
= NULL
;
1704 type
= process_op(event
, arg
, &token
);
1708 case EVENT_ERROR
... EVENT_NEWLINE
:
1710 die("unexpected type %d", type
);
1717 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1719 enum event_type type
= EVENT_ERROR
;
1720 struct print_arg
*arg
;
1725 if (type
== EVENT_NEWLINE
) {
1727 type
= read_token_item(&token
);
1731 arg
= malloc_or_die(sizeof(*arg
));
1732 memset(arg
, 0, sizeof(*arg
));
1734 type
= process_arg(event
, arg
, &token
);
1736 if (type
== EVENT_ERROR
) {
1744 if (type
== EVENT_OP
) {
1745 type
= process_op(event
, arg
, &token
);
1750 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1757 } while (type
!= EVENT_NONE
);
1759 if (type
!= EVENT_NONE
)
1765 static int event_read_print(struct event
*event
)
1767 enum event_type type
;
1771 if (read_expected_item(EVENT_ITEM
, (char *)"print") < 0)
1774 if (read_expected(EVENT_ITEM
, (char *)"fmt") < 0)
1777 if (read_expected(EVENT_OP
, (char *)":") < 0)
1780 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1784 event
->print_fmt
.format
= token
;
1785 event
->print_fmt
.args
= NULL
;
1787 /* ok to have no arg */
1788 type
= read_token_item(&token
);
1790 if (type
== EVENT_NONE
)
1793 /* Handle concatination of print lines */
1794 if (type
== EVENT_DQUOTE
) {
1797 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1799 strcpy(cat
, event
->print_fmt
.format
);
1802 free_token(event
->print_fmt
.format
);
1803 event
->print_fmt
.format
= NULL
;
1808 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1813 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1824 static struct format_field
*
1825 find_common_field(struct event
*event
, const char *name
)
1827 struct format_field
*format
;
1829 for (format
= event
->format
.common_fields
;
1830 format
; format
= format
->next
) {
1831 if (strcmp(format
->name
, name
) == 0)
1838 static struct format_field
*
1839 find_field(struct event
*event
, const char *name
)
1841 struct format_field
*format
;
1843 for (format
= event
->format
.fields
;
1844 format
; format
= format
->next
) {
1845 if (strcmp(format
->name
, name
) == 0)
1852 static struct format_field
*
1853 find_any_field(struct event
*event
, const char *name
)
1855 struct format_field
*format
;
1857 format
= find_common_field(event
, name
);
1860 return find_field(event
, name
);
1863 static unsigned long long read_size(void *ptr
, int size
)
1867 return *(unsigned char *)ptr
;
1869 return data2host2(ptr
);
1871 return data2host4(ptr
);
1873 return data2host8(ptr
);
1881 raw_field_value(struct event
*event
, const char *name
, void *data
)
1883 struct format_field
*field
;
1885 field
= find_any_field(event
, name
);
1889 return read_size(data
+ field
->offset
, field
->size
);
1892 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1894 struct format_field
*field
;
1896 field
= find_any_field(event
, name
);
1900 return data
+ field
->offset
;
1903 static int get_common_info(const char *type
, int *offset
, int *size
)
1905 struct event
*event
;
1906 struct format_field
*field
;
1909 * All events should have the same common elements.
1910 * Pick any event to find where the type is;
1913 die("no event_list!");
1916 field
= find_common_field(event
, type
);
1918 die("field '%s' not found", type
);
1920 *offset
= field
->offset
;
1921 *size
= field
->size
;
1926 int trace_parse_common_type(void *data
)
1928 static int type_offset
;
1929 static int type_size
;
1933 ret
= get_common_info("common_type",
1939 return read_size(data
+ type_offset
, type_size
);
1942 static int parse_common_pid(void *data
)
1944 static int pid_offset
;
1945 static int pid_size
;
1949 ret
= get_common_info("common_pid",
1956 return read_size(data
+ pid_offset
, pid_size
);
1959 struct event
*trace_find_event(int id
)
1961 struct event
*event
;
1963 for (event
= event_list
; event
; event
= event
->next
) {
1964 if (event
->id
== id
)
1970 static unsigned long long eval_num_arg(void *data
, int size
,
1971 struct event
*event
, struct print_arg
*arg
)
1973 unsigned long long val
= 0;
1974 unsigned long long left
, right
;
1975 struct print_arg
*larg
;
1977 switch (arg
->type
) {
1982 return strtoull(arg
->atom
.atom
, NULL
, 0);
1984 if (!arg
->field
.field
) {
1985 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1986 if (!arg
->field
.field
)
1987 die("field %s not found", arg
->field
.name
);
1989 /* must be a number */
1990 val
= read_size(data
+ arg
->field
.field
->offset
,
1991 arg
->field
.field
->size
);
1997 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2002 if (strcmp(arg
->op
.op
, "[") == 0) {
2004 * Arrays are special, since we don't want
2005 * to read the arg as is.
2007 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2008 goto default_op
; /* oops, all bets off */
2009 larg
= arg
->op
.left
;
2010 if (!larg
->field
.field
) {
2012 find_any_field(event
, larg
->field
.name
);
2013 if (!larg
->field
.field
)
2014 die("field %s not found", larg
->field
.name
);
2016 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2017 val
= read_size(data
+ larg
->field
.field
->offset
+
2018 right
* long_size
, long_size
);
2022 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2023 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2024 switch (arg
->op
.op
[0]) {
2027 val
= left
|| right
;
2033 val
= left
&& right
;
2038 switch (arg
->op
.op
[1]) {
2043 val
= left
<< right
;
2046 val
= left
<= right
;
2049 die("unknown op '%s'", arg
->op
.op
);
2053 switch (arg
->op
.op
[1]) {
2058 val
= left
>> right
;
2061 val
= left
>= right
;
2064 die("unknown op '%s'", arg
->op
.op
);
2068 if (arg
->op
.op
[1] != '=')
2069 die("unknown op '%s'", arg
->op
.op
);
2070 val
= left
== right
;
2073 die("unknown op '%s'", arg
->op
.op
);
2076 default: /* not sure what to do there */
2084 unsigned long long value
;
2087 static const struct flag flags
[] = {
2088 { "HI_SOFTIRQ", 0 },
2089 { "TIMER_SOFTIRQ", 1 },
2090 { "NET_TX_SOFTIRQ", 2 },
2091 { "NET_RX_SOFTIRQ", 3 },
2092 { "BLOCK_SOFTIRQ", 4 },
2093 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2094 { "TASKLET_SOFTIRQ", 6 },
2095 { "SCHED_SOFTIRQ", 7 },
2096 { "HRTIMER_SOFTIRQ", 8 },
2097 { "RCU_SOFTIRQ", 9 },
2099 { "HRTIMER_NORESTART", 0 },
2100 { "HRTIMER_RESTART", 1 },
2103 static unsigned long long eval_flag(const char *flag
)
2108 * Some flags in the format files do not get converted.
2109 * If the flag is not numeric, see if it is something that
2110 * we already know about.
2112 if (isdigit(flag
[0]))
2113 return strtoull(flag
, NULL
, 0);
2115 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2116 if (strcmp(flags
[i
].name
, flag
) == 0)
2117 return flags
[i
].value
;
2122 static void print_str_arg(void *data
, int size
,
2123 struct event
*event
, struct print_arg
*arg
)
2125 struct print_flag_sym
*flag
;
2126 unsigned long long val
, fval
;
2130 switch (arg
->type
) {
2135 printf("%s", arg
->atom
.atom
);
2138 if (!arg
->field
.field
) {
2139 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2140 if (!arg
->field
.field
)
2141 die("field %s not found", arg
->field
.name
);
2143 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2144 memcpy(str
, data
+ arg
->field
.field
->offset
,
2145 arg
->field
.field
->size
);
2146 str
[arg
->field
.field
->size
] = 0;
2151 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2153 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2154 fval
= eval_flag(flag
->value
);
2155 if (!val
&& !fval
) {
2156 printf("%s", flag
->str
);
2159 if (fval
&& (val
& fval
) == fval
) {
2160 if (print
&& arg
->flags
.delim
)
2161 printf("%s", arg
->flags
.delim
);
2162 printf("%s", flag
->str
);
2169 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2170 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2171 fval
= eval_flag(flag
->value
);
2173 printf("%s", flag
->str
);
2181 case PRINT_STRING
: {
2184 if (arg
->string
.offset
== -1) {
2185 struct format_field
*f
;
2187 f
= find_any_field(event
, arg
->string
.string
);
2188 arg
->string
.offset
= f
->offset
;
2190 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2191 str_offset
&= 0xffff;
2192 printf("%s", ((char *)data
) + str_offset
);
2197 * The only op for string should be ? :
2199 if (arg
->op
.op
[0] != '?')
2201 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2203 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2205 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2213 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2215 static struct format_field
*field
, *ip_field
;
2216 struct print_arg
*args
, *arg
, **next
;
2217 unsigned long long ip
, val
;
2222 field
= find_field(event
, "buf");
2224 die("can't find buffer field for binary printk");
2225 ip_field
= find_field(event
, "ip");
2227 die("can't find ip field for binary printk");
2230 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2233 * The first arg is the IP pointer.
2235 args
= malloc_or_die(sizeof(*args
));
2240 arg
->type
= PRINT_ATOM
;
2241 arg
->atom
.atom
= malloc_or_die(32);
2242 sprintf(arg
->atom
.atom
, "%lld", ip
);
2244 /* skip the first "%pf : " */
2245 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2246 bptr
< data
+ size
&& *ptr
; ptr
++) {
2270 bptr
= (void *)(((unsigned long)bptr
+ (long_size
- 1)) &
2282 val
= read_size(bptr
, ls
);
2284 arg
= malloc_or_die(sizeof(*arg
));
2286 arg
->type
= PRINT_ATOM
;
2287 arg
->atom
.atom
= malloc_or_die(32);
2288 sprintf(arg
->atom
.atom
, "%lld", val
);
2293 arg
= malloc_or_die(sizeof(*arg
));
2295 arg
->type
= PRINT_STRING
;
2296 arg
->string
.string
= strdup(bptr
);
2297 bptr
+= strlen(bptr
) + 1;
2309 static void free_args(struct print_arg
*args
)
2311 struct print_arg
*next
;
2316 if (args
->type
== PRINT_ATOM
)
2317 free(args
->atom
.atom
);
2319 free(args
->string
.string
);
2325 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2327 unsigned long long addr
;
2328 static struct format_field
*field
;
2329 struct printk_map
*printk
;
2334 field
= find_field(event
, "fmt");
2336 die("can't find format field for binary printk");
2337 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2340 addr
= read_size(data
+ field
->offset
, field
->size
);
2342 printk
= find_printk(addr
);
2344 format
= malloc_or_die(45);
2345 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2351 /* Remove any quotes. */
2354 format
= malloc_or_die(strlen(p
) + 10);
2355 sprintf(format
, "%s : %s", "%pf", p
);
2356 /* remove ending quotes and new line since we will add one too */
2357 p
= format
+ strlen(format
) - 1;
2362 if (strcmp(p
, "\\n") == 0)
2368 static void pretty_print(void *data
, int size
, struct event
*event
)
2370 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2371 struct print_arg
*arg
= print_fmt
->args
;
2372 struct print_arg
*args
= NULL
;
2373 const char *ptr
= print_fmt
->format
;
2374 unsigned long long val
;
2375 struct func_map
*func
;
2376 const char *saveptr
;
2377 char *bprint_fmt
= NULL
;
2383 if (event
->flags
& EVENT_FL_ISFUNC
)
2384 ptr
= " %pF <-- %pF";
2386 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2387 bprint_fmt
= get_bprint_format(data
, size
, event
);
2388 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2393 for (; *ptr
; ptr
++) {
2415 } else if (*ptr
== '%') {
2440 if (*(ptr
+1) == 'F' ||
2453 die("no argument match");
2455 len
= ((unsigned long)ptr
+ 1) -
2456 (unsigned long)saveptr
;
2458 /* should never happen */
2462 memcpy(format
, saveptr
, len
);
2465 val
= eval_num_arg(data
, size
, event
, arg
);
2469 func
= find_func(val
);
2471 printf("%s", func
->func
);
2472 if (show_func
== 'F')
2480 printf(format
, (int)val
);
2483 printf(format
, (long)val
);
2486 printf(format
, (long long)val
);
2489 die("bad count (%d)", ls
);
2494 die("no matching argument");
2496 print_str_arg(data
, size
, event
, arg
);
2500 printf(">%c<", *ptr
);
2513 static inline int log10_cpu(int nb
)
2522 /* taken from Linux, written by Frederic Weisbecker */
2523 static void print_graph_cpu(int cpu
)
2526 int log10_this
= log10_cpu(cpu
);
2527 int log10_all
= log10_cpu(cpus
);
2531 * Start with a space character - to make it stand out
2532 * to the right a bit when trace output is pasted into
2538 * Tricky - we space the CPU field according to the max
2539 * number of online CPUs. On a 2-cpu system it would take
2540 * a maximum of 1 digit - on a 128 cpu system it would
2541 * take up to 3 digits:
2543 for (i
= 0; i
< log10_all
- log10_this
; i
++)
2546 printf("%d) ", cpu
);
2549 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2550 #define TRACE_GRAPH_INDENT 2
2552 static void print_graph_proc(int pid
, const char *comm
)
2554 /* sign + log10(MAX_INT) + '\0' */
2560 sprintf(pid_str
, "%d", pid
);
2562 /* 1 stands for the "-" character */
2563 len
= strlen(comm
) + strlen(pid_str
) + 1;
2565 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
2566 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
2568 /* First spaces to align center */
2569 for (i
= 0; i
< spaces
/ 2; i
++)
2572 printf("%s-%s", comm
, pid_str
);
2574 /* Last spaces to align center */
2575 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++)
2579 static struct record
*
2580 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2581 struct record
*next
)
2583 struct format_field
*field
;
2584 struct event
*event
;
2589 type
= trace_parse_common_type(next
->data
);
2590 event
= trace_find_event(type
);
2594 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2597 pid
= parse_common_pid(next
->data
);
2598 field
= find_field(event
, "func");
2600 die("function return does not have field func");
2602 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2604 if (cur_pid
!= pid
|| cur_func
!= val
)
2607 /* this is a leaf, now advance the iterator */
2608 return trace_read_data(cpu
);
2611 /* Signal a overhead of time execution to the output */
2612 static void print_graph_overhead(unsigned long long duration
)
2614 /* Non nested entry or return */
2615 if (duration
== ~0ULL)
2616 return (void)printf(" ");
2618 /* Duration exceeded 100 msecs */
2619 if (duration
> 100000ULL)
2620 return (void)printf("! ");
2622 /* Duration exceeded 10 msecs */
2623 if (duration
> 10000ULL)
2624 return (void)printf("+ ");
2629 static void print_graph_duration(unsigned long long duration
)
2631 unsigned long usecs
= duration
/ 1000;
2632 unsigned long nsecs_rem
= duration
% 1000;
2633 /* log10(ULONG_MAX) + '\0' */
2639 sprintf(msecs_str
, "%lu", usecs
);
2642 len
= printf("%lu", usecs
);
2644 /* Print nsecs (we don't want to exceed 7 numbers) */
2646 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2647 len
+= printf(".%s", nsecs_str
);
2652 /* Print remaining spaces to fit the row's width */
2653 for (i
= len
; i
< 7; i
++)
2660 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2662 unsigned long long rettime
, calltime
;
2663 unsigned long long duration
, depth
;
2664 unsigned long long val
;
2665 struct format_field
*field
;
2666 struct func_map
*func
;
2667 struct event
*ret_event
;
2671 type
= trace_parse_common_type(ret_rec
->data
);
2672 ret_event
= trace_find_event(type
);
2674 field
= find_field(ret_event
, "rettime");
2676 die("can't find rettime in return graph");
2677 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2679 field
= find_field(ret_event
, "calltime");
2681 die("can't find rettime in return graph");
2682 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2684 duration
= rettime
- calltime
;
2687 print_graph_overhead(duration
);
2690 print_graph_duration(duration
);
2692 field
= find_field(event
, "depth");
2694 die("can't find depth in entry graph");
2695 depth
= read_size(data
+ field
->offset
, field
->size
);
2698 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2701 field
= find_field(event
, "func");
2703 die("can't find func in entry graph");
2704 val
= read_size(data
+ field
->offset
, field
->size
);
2705 func
= find_func(val
);
2708 printf("%s();", func
->func
);
2710 printf("%llx();", val
);
2713 static void print_graph_nested(struct event
*event
, void *data
)
2715 struct format_field
*field
;
2716 unsigned long long depth
;
2717 unsigned long long val
;
2718 struct func_map
*func
;
2722 print_graph_overhead(-1);
2727 field
= find_field(event
, "depth");
2729 die("can't find depth in entry graph");
2730 depth
= read_size(data
+ field
->offset
, field
->size
);
2733 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2736 field
= find_field(event
, "func");
2738 die("can't find func in entry graph");
2739 val
= read_size(data
+ field
->offset
, field
->size
);
2740 func
= find_func(val
);
2743 printf("%s() {", func
->func
);
2745 printf("%llx() {", val
);
2749 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2750 int cpu
, int pid
, const char *comm
,
2751 unsigned long secs
, unsigned long usecs
)
2753 struct format_field
*field
;
2758 printf("%5lu.%06lu | ", secs
, usecs
);
2760 print_graph_cpu(cpu
);
2761 print_graph_proc(pid
, comm
);
2765 field
= find_field(event
, "func");
2767 die("function entry does not have func field");
2769 val
= read_size(data
+ field
->offset
, field
->size
);
2772 * peek_data may unmap the data pointer. Copy it first.
2774 copy_data
= malloc_or_die(size
);
2775 memcpy(copy_data
, data
, size
);
2778 rec
= trace_peek_data(cpu
);
2780 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2782 print_graph_entry_leaf(event
, data
, rec
);
2786 print_graph_nested(event
, data
);
2792 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
,
2793 int cpu
, int pid
, const char *comm
,
2794 unsigned long secs
, unsigned long usecs
)
2796 unsigned long long rettime
, calltime
;
2797 unsigned long long duration
, depth
;
2798 struct format_field
*field
;
2801 printf("%5lu.%06lu | ", secs
, usecs
);
2803 print_graph_cpu(cpu
);
2804 print_graph_proc(pid
, comm
);
2808 field
= find_field(event
, "rettime");
2810 die("can't find rettime in return graph");
2811 rettime
= read_size(data
+ field
->offset
, field
->size
);
2813 field
= find_field(event
, "calltime");
2815 die("can't find calltime in return graph");
2816 calltime
= read_size(data
+ field
->offset
, field
->size
);
2818 duration
= rettime
- calltime
;
2821 print_graph_overhead(duration
);
2824 print_graph_duration(duration
);
2826 field
= find_field(event
, "depth");
2828 die("can't find depth in entry graph");
2829 depth
= read_size(data
+ field
->offset
, field
->size
);
2832 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2839 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2840 int cpu
, int pid
, const char *comm
,
2841 unsigned long secs
, unsigned long usecs
)
2843 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2844 pretty_print_func_ent(data
, size
, event
,
2845 cpu
, pid
, comm
, secs
, usecs
);
2846 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2847 pretty_print_func_ret(data
, size
, event
,
2848 cpu
, pid
, comm
, secs
, usecs
);
2852 void print_event(int cpu
, void *data
, int size
, unsigned long long nsecs
,
2855 struct event
*event
;
2857 unsigned long usecs
;
2861 secs
= nsecs
/ NSECS_PER_SEC
;
2862 nsecs
-= secs
* NSECS_PER_SEC
;
2863 usecs
= nsecs
/ NSECS_PER_USEC
;
2865 type
= trace_parse_common_type(data
);
2867 event
= trace_find_event(type
);
2869 printf("ug! no event found for type %d\n", type
);
2873 pid
= parse_common_pid(data
);
2875 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2876 return pretty_print_func_graph(data
, size
, event
, cpu
,
2877 pid
, comm
, secs
, usecs
);
2879 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
2881 secs
, nsecs
, event
->name
);
2883 pretty_print(data
, size
, event
);
2887 static void print_fields(struct print_flag_sym
*field
)
2889 printf("{ %s, %s }", field
->value
, field
->str
);
2892 print_fields(field
->next
);
2896 static void print_args(struct print_arg
*args
)
2898 int print_paren
= 1;
2900 switch (args
->type
) {
2905 printf("%s", args
->atom
.atom
);
2908 printf("REC->%s", args
->field
.name
);
2911 printf("__print_flags(");
2912 print_args(args
->flags
.field
);
2913 printf(", %s, ", args
->flags
.delim
);
2914 print_fields(args
->flags
.flags
);
2918 printf("__print_symbolic(");
2919 print_args(args
->symbol
.field
);
2921 print_fields(args
->symbol
.symbols
);
2925 printf("__get_str(%s)", args
->string
.string
);
2928 printf("(%s)", args
->typecast
.type
);
2929 print_args(args
->typecast
.item
);
2932 if (strcmp(args
->op
.op
, ":") == 0)
2936 print_args(args
->op
.left
);
2937 printf(" %s ", args
->op
.op
);
2938 print_args(args
->op
.right
);
2943 /* we should warn... */
2948 print_args(args
->next
);
2952 static void parse_header_field(char *type
,
2953 int *offset
, int *size
)
2957 if (read_expected(EVENT_ITEM
, (char *)"field") < 0)
2959 if (read_expected(EVENT_OP
, (char *)":") < 0)
2962 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2966 if (read_expected(EVENT_ITEM
, type
) < 0)
2968 if (read_expected(EVENT_OP
, (char *)";") < 0)
2970 if (read_expected(EVENT_ITEM
, (char *)"offset") < 0)
2972 if (read_expected(EVENT_OP
, (char *)":") < 0)
2974 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2976 *offset
= atoi(token
);
2978 if (read_expected(EVENT_OP
, (char *)";") < 0)
2980 if (read_expected(EVENT_ITEM
, (char *)"size") < 0)
2982 if (read_expected(EVENT_OP
, (char *)":") < 0)
2984 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2986 *size
= atoi(token
);
2988 if (read_expected(EVENT_OP
, (char *)";") < 0)
2990 if (read_expected(EVENT_ITEM
, (char *)"signed") < 0)
2992 if (read_expected(EVENT_OP
, (char *)":") < 0)
2994 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2997 if (read_expected(EVENT_OP
, (char *)";") < 0)
2999 if (read_expect_type(EVENT_NEWLINE
, &token
) < 0)
3004 int parse_header_page(char *buf
, unsigned long size
)
3006 init_input_buf(buf
, size
);
3008 parse_header_field((char *)"timestamp", &header_page_ts_offset
,
3009 &header_page_ts_size
);
3010 parse_header_field((char *)"commit", &header_page_size_offset
,
3011 &header_page_size_size
);
3012 parse_header_field((char *)"data", &header_page_data_offset
,
3013 &header_page_data_size
);
3018 int parse_ftrace_file(char *buf
, unsigned long size
)
3020 struct format_field
*field
;
3021 struct print_arg
*arg
, **list
;
3022 struct event
*event
;
3025 init_input_buf(buf
, size
);
3027 event
= alloc_event();
3031 event
->flags
|= EVENT_FL_ISFTRACE
;
3033 event
->name
= event_read_name();
3035 die("failed to read ftrace event name");
3037 if (strcmp(event
->name
, "function") == 0)
3038 event
->flags
|= EVENT_FL_ISFUNC
;
3040 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3041 event
->flags
|= EVENT_FL_ISFUNCENT
;
3043 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3044 event
->flags
|= EVENT_FL_ISFUNCRET
;
3046 else if (strcmp(event
->name
, "bprint") == 0)
3047 event
->flags
|= EVENT_FL_ISBPRINT
;
3049 event
->id
= event_read_id();
3051 die("failed to read ftrace event id");
3055 ret
= event_read_format(event
);
3057 die("failed to read ftrace event format");
3059 ret
= event_read_print(event
);
3061 die("failed to read ftrace event print fmt");
3064 * The arguments for ftrace files are parsed by the fields.
3065 * Set up the fields as their arguments.
3067 list
= &event
->print_fmt
.args
;
3068 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3069 arg
= malloc_or_die(sizeof(*arg
));
3070 memset(arg
, 0, sizeof(*arg
));
3073 arg
->type
= PRINT_FIELD
;
3074 arg
->field
.name
= field
->name
;
3075 arg
->field
.field
= field
;
3080 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3082 struct event
*event
;
3085 init_input_buf(buf
, size
);
3087 event
= alloc_event();
3091 event
->name
= event_read_name();
3093 die("failed to read event name");
3095 event
->id
= event_read_id();
3097 die("failed to read event id");
3099 ret
= event_read_format(event
);
3101 die("failed to read event format");
3103 ret
= event_read_print(event
);
3105 die("failed to read event print fmt");
3107 event
->system
= strdup(sys
);
3109 #define PRINT_ARGS 0
3110 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3111 print_args(event
->print_fmt
.args
);
3117 void parse_set_info(int nr_cpus
, int long_sz
)
3120 long_size
= long_sz
;