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 int get_op_prio(char *op
)
1060 /* '>>' and '<<' are 8 */
1064 /* '==' and '!=' are 10 */
1074 die("unknown op '%c'", op
[0]);
1078 if (strcmp(op
, "++") == 0 ||
1079 strcmp(op
, "--") == 0) {
1081 } else if (strcmp(op
, ">>") == 0 ||
1082 strcmp(op
, "<<") == 0) {
1084 } else if (strcmp(op
, ">=") == 0 ||
1085 strcmp(op
, "<=") == 0) {
1087 } else if (strcmp(op
, "==") == 0 ||
1088 strcmp(op
, "!=") == 0) {
1090 } else if (strcmp(op
, "&&") == 0) {
1092 } else if (strcmp(op
, "||") == 0) {
1095 die("unknown op '%s'", op
);
1101 static void set_op_prio(struct print_arg
*arg
)
1104 /* single ops are the greatest */
1105 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1110 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1113 static enum event_type
1114 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1116 struct print_arg
*left
, *right
= NULL
;
1117 enum event_type type
;
1120 /* the op is passed in via tok */
1123 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1124 /* handle single op */
1126 die("bad op token %s", token
);
1135 die("bad op token %s", token
);
1139 /* make an empty left */
1140 left
= malloc_or_die(sizeof(*left
));
1141 left
->type
= PRINT_NULL
;
1142 arg
->op
.left
= left
;
1144 right
= malloc_or_die(sizeof(*right
));
1145 arg
->op
.right
= right
;
1147 type
= process_arg(event
, right
, tok
);
1149 } else if (strcmp(token
, "?") == 0) {
1151 left
= malloc_or_die(sizeof(*left
));
1152 /* copy the top arg to the left */
1155 arg
->type
= PRINT_OP
;
1157 arg
->op
.left
= left
;
1160 type
= process_cond(event
, arg
, tok
);
1162 } else if (strcmp(token
, ">>") == 0 ||
1163 strcmp(token
, "<<") == 0 ||
1164 strcmp(token
, "&") == 0 ||
1165 strcmp(token
, "|") == 0 ||
1166 strcmp(token
, "&&") == 0 ||
1167 strcmp(token
, "||") == 0 ||
1168 strcmp(token
, "-") == 0 ||
1169 strcmp(token
, "+") == 0 ||
1170 strcmp(token
, "*") == 0 ||
1171 strcmp(token
, "^") == 0 ||
1172 strcmp(token
, "/") == 0 ||
1173 strcmp(token
, "==") == 0 ||
1174 strcmp(token
, "!=") == 0) {
1176 left
= malloc_or_die(sizeof(*left
));
1178 /* copy the top arg to the left */
1181 arg
->type
= PRINT_OP
;
1183 arg
->op
.left
= left
;
1187 right
= malloc_or_die(sizeof(*right
));
1189 type
= process_arg(event
, right
, tok
);
1191 arg
->op
.right
= right
;
1194 die("unknown op '%s'", token
);
1195 /* the arg is now the left side */
1200 if (type
== EVENT_OP
) {
1203 /* higher prios need to be closer to the root */
1204 prio
= get_op_prio(*tok
);
1206 if (prio
> arg
->op
.prio
)
1207 return process_op(event
, arg
, tok
);
1209 return process_op(event
, right
, tok
);
1215 static enum event_type
1216 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1219 enum event_type type
;
1223 if (read_expected(EVENT_OP
, (char *)"->") < 0)
1226 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1230 arg
->type
= PRINT_FIELD
;
1231 arg
->field
.name
= field
;
1233 type
= read_token(&token
);
1243 static char *arg_eval (struct print_arg
*arg
);
1245 static long long arg_num_eval(struct print_arg
*arg
)
1247 long long left
, right
;
1250 switch (arg
->type
) {
1252 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1255 val
= arg_num_eval(arg
->typecast
.item
);
1258 switch (arg
->op
.op
[0]) {
1260 left
= arg_num_eval(arg
->op
.left
);
1261 right
= arg_num_eval(arg
->op
.right
);
1263 val
= left
|| right
;
1268 left
= arg_num_eval(arg
->op
.left
);
1269 right
= arg_num_eval(arg
->op
.right
);
1271 val
= left
&& right
;
1276 left
= arg_num_eval(arg
->op
.left
);
1277 right
= arg_num_eval(arg
->op
.right
);
1278 switch (arg
->op
.op
[1]) {
1283 val
= left
<< right
;
1286 val
= left
<= right
;
1289 die("unknown op '%s'", arg
->op
.op
);
1293 left
= arg_num_eval(arg
->op
.left
);
1294 right
= arg_num_eval(arg
->op
.right
);
1295 switch (arg
->op
.op
[1]) {
1300 val
= left
>> right
;
1303 val
= left
>= right
;
1306 die("unknown op '%s'", arg
->op
.op
);
1310 left
= arg_num_eval(arg
->op
.left
);
1311 right
= arg_num_eval(arg
->op
.right
);
1313 if (arg
->op
.op
[1] != '=')
1314 die("unknown op '%s'", arg
->op
.op
);
1316 val
= left
== right
;
1319 left
= arg_num_eval(arg
->op
.left
);
1320 right
= arg_num_eval(arg
->op
.right
);
1322 switch (arg
->op
.op
[1]) {
1324 val
= left
!= right
;
1327 die("unknown op '%s'", arg
->op
.op
);
1331 die("unknown op '%s'", arg
->op
.op
);
1336 case PRINT_FIELD
... PRINT_SYMBOL
:
1339 die("invalid eval type %d", arg
->type
);
1345 static char *arg_eval (struct print_arg
*arg
)
1348 static char buf
[20];
1350 switch (arg
->type
) {
1352 return arg
->atom
.atom
;
1354 return arg_eval(arg
->typecast
.item
);
1356 val
= arg_num_eval(arg
);
1357 sprintf(buf
, "%lld", val
);
1361 case PRINT_FIELD
... PRINT_SYMBOL
:
1364 die("invalid eval type %d", arg
->type
);
1371 static enum event_type
1372 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1374 enum event_type type
;
1375 struct print_arg
*arg
= NULL
;
1376 struct print_flag_sym
*field
;
1382 type
= read_token_item(&token
);
1383 if (test_type_token(type
, token
, EVENT_OP
, (char *)"{"))
1386 arg
= malloc_or_die(sizeof(*arg
));
1389 type
= process_arg(event
, arg
, &token
);
1390 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1393 field
= malloc_or_die(sizeof(*field
));
1394 memset(field
, 0, sizeof(field
));
1396 value
= arg_eval(arg
);
1397 field
->value
= strdup(value
);
1400 type
= process_arg(event
, arg
, &token
);
1401 if (test_type_token(type
, token
, EVENT_OP
, (char *)"}"))
1404 value
= arg_eval(arg
);
1405 field
->str
= strdup(value
);
1410 list
= &field
->next
;
1413 type
= read_token_item(&token
);
1414 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1426 static enum event_type
1427 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1429 struct print_arg
*field
;
1430 enum event_type type
;
1433 memset(arg
, 0, sizeof(*arg
));
1434 arg
->type
= PRINT_FLAGS
;
1436 if (read_expected_item(EVENT_DELIM
, (char *)"(") < 0)
1439 field
= malloc_or_die(sizeof(*field
));
1441 type
= process_arg(event
, field
, &token
);
1442 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1445 arg
->flags
.field
= field
;
1447 type
= read_token_item(&token
);
1448 if (event_item_type(type
)) {
1449 arg
->flags
.delim
= token
;
1450 type
= read_token_item(&token
);
1453 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1456 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1457 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")"))
1461 type
= read_token_item(tok
);
1469 static enum event_type
1470 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1472 struct print_arg
*field
;
1473 enum event_type type
;
1476 memset(arg
, 0, sizeof(*arg
));
1477 arg
->type
= PRINT_SYMBOL
;
1479 if (read_expected_item(EVENT_DELIM
, (char *)"(") < 0)
1482 field
= malloc_or_die(sizeof(*field
));
1484 type
= process_arg(event
, field
, &token
);
1485 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1488 arg
->symbol
.field
= field
;
1490 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1491 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")"))
1495 type
= read_token_item(tok
);
1503 static enum event_type
1504 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1506 struct print_arg
*item_arg
;
1507 enum event_type type
;
1511 type
= process_arg(event
, arg
, &token
);
1513 if (type
== EVENT_ERROR
)
1516 if (type
== EVENT_OP
) {
1517 /* handle the ptr casts */
1518 if (!strcmp(token
, "*")) {
1520 * FIXME: should we zapp whitespaces before ')' ?
1521 * (may require a peek_token_item())
1523 if (__peek_char() == ')') {
1526 type
= read_token_item(&token
);
1530 type
= process_op(event
, arg
, &token
);
1532 if (type
== EVENT_ERROR
)
1537 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)")")) {
1543 type
= read_token_item(&token
);
1546 * If the next token is an item or another open paren, then
1547 * this was a typecast.
1549 if (event_item_type(type
) ||
1550 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1552 /* make this a typecast and contine */
1554 /* prevous must be an atom */
1555 if (arg
->type
!= PRINT_ATOM
)
1556 die("previous needed to be PRINT_ATOM");
1558 item_arg
= malloc_or_die(sizeof(*item_arg
));
1560 arg
->type
= PRINT_TYPE
;
1562 char *old
= arg
->atom
.atom
;
1564 arg
->atom
.atom
= malloc_or_die(strlen(old
+ 3));
1565 sprintf(arg
->atom
.atom
, "%s *", old
);
1568 arg
->typecast
.type
= arg
->atom
.atom
;
1569 arg
->typecast
.item
= item_arg
;
1570 type
= process_arg_token(event
, item_arg
, &token
, type
);
1579 static enum event_type
1580 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1582 enum event_type type
;
1585 if (read_expected(EVENT_DELIM
, (char *)"(") < 0)
1588 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1591 arg
->type
= PRINT_STRING
;
1592 arg
->string
.string
= token
;
1593 arg
->string
.offset
= -1;
1595 if (read_expected(EVENT_DELIM
, (char *)")") < 0)
1598 type
= read_token(&token
);
1608 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1609 char **tok
, enum event_type type
)
1618 if (strcmp(token
, "REC") == 0) {
1620 type
= process_entry(event
, arg
, &token
);
1621 } else if (strcmp(token
, "__print_flags") == 0) {
1623 type
= process_flags(event
, arg
, &token
);
1624 } else if (strcmp(token
, "__print_symbolic") == 0) {
1626 type
= process_symbols(event
, arg
, &token
);
1627 } else if (strcmp(token
, "__get_str") == 0) {
1629 type
= process_str(event
, arg
, &token
);
1632 /* test the next token */
1633 type
= read_token_item(&token
);
1635 /* atoms can be more than one token long */
1636 while (type
== EVENT_ITEM
) {
1637 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1639 strcat(atom
, token
);
1641 type
= read_token_item(&token
);
1644 /* todo, test for function */
1646 arg
->type
= PRINT_ATOM
;
1647 arg
->atom
.atom
= atom
;
1652 arg
->type
= PRINT_ATOM
;
1653 arg
->atom
.atom
= token
;
1654 type
= read_token_item(&token
);
1657 if (strcmp(token
, "(") == 0) {
1659 type
= process_paren(event
, arg
, &token
);
1663 /* handle single ops */
1664 arg
->type
= PRINT_OP
;
1666 arg
->op
.left
= NULL
;
1667 type
= process_op(event
, arg
, &token
);
1671 case EVENT_ERROR
... EVENT_NEWLINE
:
1673 die("unexpected type %d", type
);
1680 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1682 enum event_type type
;
1683 struct print_arg
*arg
;
1688 arg
= malloc_or_die(sizeof(*arg
));
1689 memset(arg
, 0, sizeof(*arg
));
1691 type
= process_arg(event
, arg
, &token
);
1693 if (type
== EVENT_ERROR
) {
1701 if (type
== EVENT_OP
) {
1702 type
= process_op(event
, arg
, &token
);
1707 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1714 } while (type
!= EVENT_NONE
);
1716 if (type
!= EVENT_NONE
)
1722 static int event_read_print(struct event
*event
)
1724 enum event_type type
;
1728 if (read_expected_item(EVENT_ITEM
, (char *)"print") < 0)
1731 if (read_expected(EVENT_ITEM
, (char *)"fmt") < 0)
1734 if (read_expected(EVENT_OP
, (char *)":") < 0)
1737 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1741 event
->print_fmt
.format
= token
;
1742 event
->print_fmt
.args
= NULL
;
1744 /* ok to have no arg */
1745 type
= read_token_item(&token
);
1747 if (type
== EVENT_NONE
)
1750 /* Handle concatination of print lines */
1751 if (type
== EVENT_DQUOTE
) {
1754 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1756 strcpy(cat
, event
->print_fmt
.format
);
1759 free_token(event
->print_fmt
.format
);
1760 event
->print_fmt
.format
= NULL
;
1765 if (test_type_token(type
, token
, EVENT_DELIM
, (char *)","))
1770 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1781 static struct format_field
*
1782 find_common_field(struct event
*event
, const char *name
)
1784 struct format_field
*format
;
1786 for (format
= event
->format
.common_fields
;
1787 format
; format
= format
->next
) {
1788 if (strcmp(format
->name
, name
) == 0)
1795 static struct format_field
*
1796 find_field(struct event
*event
, const char *name
)
1798 struct format_field
*format
;
1800 for (format
= event
->format
.fields
;
1801 format
; format
= format
->next
) {
1802 if (strcmp(format
->name
, name
) == 0)
1809 static struct format_field
*
1810 find_any_field(struct event
*event
, const char *name
)
1812 struct format_field
*format
;
1814 format
= find_common_field(event
, name
);
1817 return find_field(event
, name
);
1820 static unsigned long long read_size(void *ptr
, int size
)
1824 return *(unsigned char *)ptr
;
1826 return data2host2(ptr
);
1828 return data2host4(ptr
);
1830 return data2host8(ptr
);
1838 raw_field_value(struct event
*event
, const char *name
, void *data
)
1840 struct format_field
*field
;
1842 field
= find_any_field(event
, name
);
1846 return read_size(data
+ field
->offset
, field
->size
);
1849 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1851 struct format_field
*field
;
1853 field
= find_any_field(event
, name
);
1857 return data
+ field
->offset
;
1860 static int get_common_info(const char *type
, int *offset
, int *size
)
1862 struct event
*event
;
1863 struct format_field
*field
;
1866 * All events should have the same common elements.
1867 * Pick any event to find where the type is;
1870 die("no event_list!");
1873 field
= find_common_field(event
, type
);
1875 die("field '%s' not found", type
);
1877 *offset
= field
->offset
;
1878 *size
= field
->size
;
1883 int trace_parse_common_type(void *data
)
1885 static int type_offset
;
1886 static int type_size
;
1890 ret
= get_common_info("common_type",
1896 return read_size(data
+ type_offset
, type_size
);
1899 static int parse_common_pid(void *data
)
1901 static int pid_offset
;
1902 static int pid_size
;
1906 ret
= get_common_info("common_pid",
1913 return read_size(data
+ pid_offset
, pid_size
);
1916 struct event
*trace_find_event(int id
)
1918 struct event
*event
;
1920 for (event
= event_list
; event
; event
= event
->next
) {
1921 if (event
->id
== id
)
1927 static unsigned long long eval_num_arg(void *data
, int size
,
1928 struct event
*event
, struct print_arg
*arg
)
1930 unsigned long long val
= 0;
1931 unsigned long long left
, right
;
1933 switch (arg
->type
) {
1938 return strtoull(arg
->atom
.atom
, NULL
, 0);
1940 if (!arg
->field
.field
) {
1941 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1942 if (!arg
->field
.field
)
1943 die("field %s not found", arg
->field
.name
);
1945 /* must be a number */
1946 val
= read_size(data
+ arg
->field
.field
->offset
,
1947 arg
->field
.field
->size
);
1953 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
1958 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
1959 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
1960 switch (arg
->op
.op
[0]) {
1963 val
= left
|| right
;
1969 val
= left
&& right
;
1974 switch (arg
->op
.op
[1]) {
1979 val
= left
<< right
;
1982 val
= left
<= right
;
1985 die("unknown op '%s'", arg
->op
.op
);
1989 switch (arg
->op
.op
[1]) {
1994 val
= left
>> right
;
1997 val
= left
>= right
;
2000 die("unknown op '%s'", arg
->op
.op
);
2004 if (arg
->op
.op
[1] != '=')
2005 die("unknown op '%s'", arg
->op
.op
);
2006 val
= left
== right
;
2009 die("unknown op '%s'", arg
->op
.op
);
2012 default: /* not sure what to do there */
2020 unsigned long long value
;
2023 static const struct flag flags
[] = {
2024 { "HI_SOFTIRQ", 0 },
2025 { "TIMER_SOFTIRQ", 1 },
2026 { "NET_TX_SOFTIRQ", 2 },
2027 { "NET_RX_SOFTIRQ", 3 },
2028 { "BLOCK_SOFTIRQ", 4 },
2029 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2030 { "TASKLET_SOFTIRQ", 6 },
2031 { "SCHED_SOFTIRQ", 7 },
2032 { "HRTIMER_SOFTIRQ", 8 },
2033 { "RCU_SOFTIRQ", 9 },
2035 { "HRTIMER_NORESTART", 0 },
2036 { "HRTIMER_RESTART", 1 },
2039 static unsigned long long eval_flag(const char *flag
)
2044 * Some flags in the format files do not get converted.
2045 * If the flag is not numeric, see if it is something that
2046 * we already know about.
2048 if (isdigit(flag
[0]))
2049 return strtoull(flag
, NULL
, 0);
2051 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2052 if (strcmp(flags
[i
].name
, flag
) == 0)
2053 return flags
[i
].value
;
2058 static void print_str_arg(void *data
, int size
,
2059 struct event
*event
, struct print_arg
*arg
)
2061 struct print_flag_sym
*flag
;
2062 unsigned long long val
, fval
;
2066 switch (arg
->type
) {
2071 printf("%s", arg
->atom
.atom
);
2074 if (!arg
->field
.field
) {
2075 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2076 if (!arg
->field
.field
)
2077 die("field %s not found", arg
->field
.name
);
2079 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2080 memcpy(str
, data
+ arg
->field
.field
->offset
,
2081 arg
->field
.field
->size
);
2082 str
[arg
->field
.field
->size
] = 0;
2087 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2089 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2090 fval
= eval_flag(flag
->value
);
2091 if (!val
&& !fval
) {
2092 printf("%s", flag
->str
);
2095 if (fval
&& (val
& fval
) == fval
) {
2096 if (print
&& arg
->flags
.delim
)
2097 printf("%s", arg
->flags
.delim
);
2098 printf("%s", flag
->str
);
2105 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2106 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2107 fval
= eval_flag(flag
->value
);
2109 printf("%s", flag
->str
);
2117 case PRINT_STRING
: {
2120 if (arg
->string
.offset
== -1) {
2121 struct format_field
*f
;
2123 f
= find_any_field(event
, arg
->string
.string
);
2124 arg
->string
.offset
= f
->offset
;
2126 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2127 str_offset
&= 0xffff;
2128 printf("%s", ((char *)data
) + str_offset
);
2133 * The only op for string should be ? :
2135 if (arg
->op
.op
[0] != '?')
2137 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2139 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2141 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2149 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2151 static struct format_field
*field
, *ip_field
;
2152 struct print_arg
*args
, *arg
, **next
;
2153 unsigned long long ip
, val
;
2158 field
= find_field(event
, "buf");
2160 die("can't find buffer field for binary printk");
2161 ip_field
= find_field(event
, "ip");
2163 die("can't find ip field for binary printk");
2166 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2169 * The first arg is the IP pointer.
2171 args
= malloc_or_die(sizeof(*args
));
2176 arg
->type
= PRINT_ATOM
;
2177 arg
->atom
.atom
= malloc_or_die(32);
2178 sprintf(arg
->atom
.atom
, "%lld", ip
);
2180 /* skip the first "%pf : " */
2181 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2182 bptr
< data
+ size
&& *ptr
; ptr
++) {
2206 bptr
= (void *)(((unsigned long)bptr
+ (long_size
- 1)) &
2218 val
= read_size(bptr
, ls
);
2220 arg
= malloc_or_die(sizeof(*arg
));
2222 arg
->type
= PRINT_ATOM
;
2223 arg
->atom
.atom
= malloc_or_die(32);
2224 sprintf(arg
->atom
.atom
, "%lld", val
);
2229 arg
= malloc_or_die(sizeof(*arg
));
2231 arg
->type
= PRINT_STRING
;
2232 arg
->string
.string
= strdup(bptr
);
2233 bptr
+= strlen(bptr
) + 1;
2245 static void free_args(struct print_arg
*args
)
2247 struct print_arg
*next
;
2252 if (args
->type
== PRINT_ATOM
)
2253 free(args
->atom
.atom
);
2255 free(args
->string
.string
);
2261 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2263 unsigned long long addr
;
2264 static struct format_field
*field
;
2265 struct printk_map
*printk
;
2270 field
= find_field(event
, "fmt");
2272 die("can't find format field for binary printk");
2273 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2276 addr
= read_size(data
+ field
->offset
, field
->size
);
2278 printk
= find_printk(addr
);
2280 format
= malloc_or_die(45);
2281 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2287 /* Remove any quotes. */
2290 format
= malloc_or_die(strlen(p
) + 10);
2291 sprintf(format
, "%s : %s", "%pf", p
);
2292 /* remove ending quotes and new line since we will add one too */
2293 p
= format
+ strlen(format
) - 1;
2298 if (strcmp(p
, "\\n") == 0)
2304 static void pretty_print(void *data
, int size
, struct event
*event
)
2306 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2307 struct print_arg
*arg
= print_fmt
->args
;
2308 struct print_arg
*args
= NULL
;
2309 const char *ptr
= print_fmt
->format
;
2310 unsigned long long val
;
2311 struct func_map
*func
;
2312 const char *saveptr
;
2313 char *bprint_fmt
= NULL
;
2319 if (event
->flags
& EVENT_FL_ISFUNC
)
2320 ptr
= " %pF <-- %pF";
2322 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2323 bprint_fmt
= get_bprint_format(data
, size
, event
);
2324 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2329 for (; *ptr
; ptr
++) {
2351 } else if (*ptr
== '%') {
2376 if (*(ptr
+1) == 'F' ||
2389 die("no argument match");
2391 len
= ((unsigned long)ptr
+ 1) -
2392 (unsigned long)saveptr
;
2394 /* should never happen */
2398 memcpy(format
, saveptr
, len
);
2401 val
= eval_num_arg(data
, size
, event
, arg
);
2405 func
= find_func(val
);
2407 printf("%s", func
->func
);
2408 if (show_func
== 'F')
2416 printf(format
, (int)val
);
2419 printf(format
, (long)val
);
2422 printf(format
, (long long)val
);
2425 die("bad count (%d)", ls
);
2430 die("no matching argument");
2432 print_str_arg(data
, size
, event
, arg
);
2436 printf(">%c<", *ptr
);
2449 static inline int log10_cpu(int nb
)
2458 /* taken from Linux, written by Frederic Weisbecker */
2459 static void print_graph_cpu(int cpu
)
2462 int log10_this
= log10_cpu(cpu
);
2463 int log10_all
= log10_cpu(cpus
);
2467 * Start with a space character - to make it stand out
2468 * to the right a bit when trace output is pasted into
2474 * Tricky - we space the CPU field according to the max
2475 * number of online CPUs. On a 2-cpu system it would take
2476 * a maximum of 1 digit - on a 128 cpu system it would
2477 * take up to 3 digits:
2479 for (i
= 0; i
< log10_all
- log10_this
; i
++)
2482 printf("%d) ", cpu
);
2485 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2486 #define TRACE_GRAPH_INDENT 2
2488 static void print_graph_proc(int pid
, const char *comm
)
2490 /* sign + log10(MAX_INT) + '\0' */
2496 sprintf(pid_str
, "%d", pid
);
2498 /* 1 stands for the "-" character */
2499 len
= strlen(comm
) + strlen(pid_str
) + 1;
2501 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
2502 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
2504 /* First spaces to align center */
2505 for (i
= 0; i
< spaces
/ 2; i
++)
2508 printf("%s-%s", comm
, pid_str
);
2510 /* Last spaces to align center */
2511 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++)
2515 static struct record
*
2516 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2517 struct record
*next
)
2519 struct format_field
*field
;
2520 struct event
*event
;
2525 type
= trace_parse_common_type(next
->data
);
2526 event
= trace_find_event(type
);
2530 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2533 pid
= parse_common_pid(next
->data
);
2534 field
= find_field(event
, "func");
2536 die("function return does not have field func");
2538 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2540 if (cur_pid
!= pid
|| cur_func
!= val
)
2543 /* this is a leaf, now advance the iterator */
2544 return trace_read_data(cpu
);
2547 /* Signal a overhead of time execution to the output */
2548 static void print_graph_overhead(unsigned long long duration
)
2550 /* Non nested entry or return */
2551 if (duration
== ~0ULL)
2552 return (void)printf(" ");
2554 /* Duration exceeded 100 msecs */
2555 if (duration
> 100000ULL)
2556 return (void)printf("! ");
2558 /* Duration exceeded 10 msecs */
2559 if (duration
> 10000ULL)
2560 return (void)printf("+ ");
2565 static void print_graph_duration(unsigned long long duration
)
2567 unsigned long usecs
= duration
/ 1000;
2568 unsigned long nsecs_rem
= duration
% 1000;
2569 /* log10(ULONG_MAX) + '\0' */
2575 sprintf(msecs_str
, "%lu", usecs
);
2578 len
= printf("%lu", usecs
);
2580 /* Print nsecs (we don't want to exceed 7 numbers) */
2582 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2583 len
+= printf(".%s", nsecs_str
);
2588 /* Print remaining spaces to fit the row's width */
2589 for (i
= len
; i
< 7; i
++)
2596 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2598 unsigned long long rettime
, calltime
;
2599 unsigned long long duration
, depth
;
2600 unsigned long long val
;
2601 struct format_field
*field
;
2602 struct func_map
*func
;
2603 struct event
*ret_event
;
2607 type
= trace_parse_common_type(ret_rec
->data
);
2608 ret_event
= trace_find_event(type
);
2610 field
= find_field(ret_event
, "rettime");
2612 die("can't find rettime in return graph");
2613 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2615 field
= find_field(ret_event
, "calltime");
2617 die("can't find rettime in return graph");
2618 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2620 duration
= rettime
- calltime
;
2623 print_graph_overhead(duration
);
2626 print_graph_duration(duration
);
2628 field
= find_field(event
, "depth");
2630 die("can't find depth in entry graph");
2631 depth
= read_size(data
+ field
->offset
, field
->size
);
2634 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2637 field
= find_field(event
, "func");
2639 die("can't find func in entry graph");
2640 val
= read_size(data
+ field
->offset
, field
->size
);
2641 func
= find_func(val
);
2644 printf("%s();", func
->func
);
2646 printf("%llx();", val
);
2649 static void print_graph_nested(struct event
*event
, void *data
)
2651 struct format_field
*field
;
2652 unsigned long long depth
;
2653 unsigned long long val
;
2654 struct func_map
*func
;
2658 print_graph_overhead(-1);
2663 field
= find_field(event
, "depth");
2665 die("can't find depth in entry graph");
2666 depth
= read_size(data
+ field
->offset
, field
->size
);
2669 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2672 field
= find_field(event
, "func");
2674 die("can't find func in entry graph");
2675 val
= read_size(data
+ field
->offset
, field
->size
);
2676 func
= find_func(val
);
2679 printf("%s() {", func
->func
);
2681 printf("%llx() {", val
);
2685 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2686 int cpu
, int pid
, const char *comm
,
2687 unsigned long secs
, unsigned long usecs
)
2689 struct format_field
*field
;
2694 printf("%5lu.%06lu | ", secs
, usecs
);
2696 print_graph_cpu(cpu
);
2697 print_graph_proc(pid
, comm
);
2701 field
= find_field(event
, "func");
2703 die("function entry does not have func field");
2705 val
= read_size(data
+ field
->offset
, field
->size
);
2708 * peek_data may unmap the data pointer. Copy it first.
2710 copy_data
= malloc_or_die(size
);
2711 memcpy(copy_data
, data
, size
);
2714 rec
= trace_peek_data(cpu
);
2716 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2718 print_graph_entry_leaf(event
, data
, rec
);
2722 print_graph_nested(event
, data
);
2728 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
,
2729 int cpu
, int pid
, const char *comm
,
2730 unsigned long secs
, unsigned long usecs
)
2732 unsigned long long rettime
, calltime
;
2733 unsigned long long duration
, depth
;
2734 struct format_field
*field
;
2737 printf("%5lu.%06lu | ", secs
, usecs
);
2739 print_graph_cpu(cpu
);
2740 print_graph_proc(pid
, comm
);
2744 field
= find_field(event
, "rettime");
2746 die("can't find rettime in return graph");
2747 rettime
= read_size(data
+ field
->offset
, field
->size
);
2749 field
= find_field(event
, "calltime");
2751 die("can't find calltime in return graph");
2752 calltime
= read_size(data
+ field
->offset
, field
->size
);
2754 duration
= rettime
- calltime
;
2757 print_graph_overhead(duration
);
2760 print_graph_duration(duration
);
2762 field
= find_field(event
, "depth");
2764 die("can't find depth in entry graph");
2765 depth
= read_size(data
+ field
->offset
, field
->size
);
2768 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2775 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2776 int cpu
, int pid
, const char *comm
,
2777 unsigned long secs
, unsigned long usecs
)
2779 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2780 pretty_print_func_ent(data
, size
, event
,
2781 cpu
, pid
, comm
, secs
, usecs
);
2782 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2783 pretty_print_func_ret(data
, size
, event
,
2784 cpu
, pid
, comm
, secs
, usecs
);
2788 void print_event(int cpu
, void *data
, int size
, unsigned long long nsecs
,
2791 struct event
*event
;
2793 unsigned long usecs
;
2797 secs
= nsecs
/ NSECS_PER_SEC
;
2798 nsecs
-= secs
* NSECS_PER_SEC
;
2799 usecs
= nsecs
/ NSECS_PER_USEC
;
2801 type
= trace_parse_common_type(data
);
2803 event
= trace_find_event(type
);
2805 printf("ug! no event found for type %d\n", type
);
2809 pid
= parse_common_pid(data
);
2811 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2812 return pretty_print_func_graph(data
, size
, event
, cpu
,
2813 pid
, comm
, secs
, usecs
);
2815 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
2817 secs
, nsecs
, event
->name
);
2819 pretty_print(data
, size
, event
);
2823 static void print_fields(struct print_flag_sym
*field
)
2825 printf("{ %s, %s }", field
->value
, field
->str
);
2828 print_fields(field
->next
);
2832 static void print_args(struct print_arg
*args
)
2834 int print_paren
= 1;
2836 switch (args
->type
) {
2841 printf("%s", args
->atom
.atom
);
2844 printf("REC->%s", args
->field
.name
);
2847 printf("__print_flags(");
2848 print_args(args
->flags
.field
);
2849 printf(", %s, ", args
->flags
.delim
);
2850 print_fields(args
->flags
.flags
);
2854 printf("__print_symbolic(");
2855 print_args(args
->symbol
.field
);
2857 print_fields(args
->symbol
.symbols
);
2861 printf("__get_str(%s)", args
->string
.string
);
2864 printf("(%s)", args
->typecast
.type
);
2865 print_args(args
->typecast
.item
);
2868 if (strcmp(args
->op
.op
, ":") == 0)
2872 print_args(args
->op
.left
);
2873 printf(" %s ", args
->op
.op
);
2874 print_args(args
->op
.right
);
2879 /* we should warn... */
2884 print_args(args
->next
);
2888 static void parse_header_field(char *type
,
2889 int *offset
, int *size
)
2893 if (read_expected(EVENT_ITEM
, (char *)"field") < 0)
2895 if (read_expected(EVENT_OP
, (char *)":") < 0)
2898 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2902 if (read_expected(EVENT_ITEM
, type
) < 0)
2904 if (read_expected(EVENT_OP
, (char *)";") < 0)
2906 if (read_expected(EVENT_ITEM
, (char *)"offset") < 0)
2908 if (read_expected(EVENT_OP
, (char *)":") < 0)
2910 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2912 *offset
= atoi(token
);
2914 if (read_expected(EVENT_OP
, (char *)";") < 0)
2916 if (read_expected(EVENT_ITEM
, (char *)"size") < 0)
2918 if (read_expected(EVENT_OP
, (char *)":") < 0)
2920 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2922 *size
= atoi(token
);
2924 if (read_expected(EVENT_OP
, (char *)";") < 0)
2926 if (read_expected(EVENT_ITEM
, (char *)"signed") < 0)
2928 if (read_expected(EVENT_OP
, (char *)":") < 0)
2930 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
2933 if (read_expected(EVENT_OP
, (char *)";") < 0)
2935 if (read_expect_type(EVENT_NEWLINE
, &token
) < 0)
2940 int parse_header_page(char *buf
, unsigned long size
)
2942 init_input_buf(buf
, size
);
2944 parse_header_field((char *)"timestamp", &header_page_ts_offset
,
2945 &header_page_ts_size
);
2946 parse_header_field((char *)"commit", &header_page_size_offset
,
2947 &header_page_size_size
);
2948 parse_header_field((char *)"data", &header_page_data_offset
,
2949 &header_page_data_size
);
2954 int parse_ftrace_file(char *buf
, unsigned long size
)
2956 struct format_field
*field
;
2957 struct print_arg
*arg
, **list
;
2958 struct event
*event
;
2961 init_input_buf(buf
, size
);
2963 event
= alloc_event();
2967 event
->flags
|= EVENT_FL_ISFTRACE
;
2969 event
->name
= event_read_name();
2971 die("failed to read ftrace event name");
2973 if (strcmp(event
->name
, "function") == 0)
2974 event
->flags
|= EVENT_FL_ISFUNC
;
2976 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
2977 event
->flags
|= EVENT_FL_ISFUNCENT
;
2979 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
2980 event
->flags
|= EVENT_FL_ISFUNCRET
;
2982 else if (strcmp(event
->name
, "bprint") == 0)
2983 event
->flags
|= EVENT_FL_ISBPRINT
;
2985 event
->id
= event_read_id();
2987 die("failed to read ftrace event id");
2991 ret
= event_read_format(event
);
2993 die("failed to read ftrace event format");
2995 ret
= event_read_print(event
);
2997 die("failed to read ftrace event print fmt");
3000 * The arguments for ftrace files are parsed by the fields.
3001 * Set up the fields as their arguments.
3003 list
= &event
->print_fmt
.args
;
3004 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3005 arg
= malloc_or_die(sizeof(*arg
));
3006 memset(arg
, 0, sizeof(*arg
));
3009 arg
->type
= PRINT_FIELD
;
3010 arg
->field
.name
= field
->name
;
3011 arg
->field
.field
= field
;
3016 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3018 struct event
*event
;
3021 init_input_buf(buf
, size
);
3023 event
= alloc_event();
3027 event
->name
= event_read_name();
3029 die("failed to read event name");
3031 event
->id
= event_read_id();
3033 die("failed to read event id");
3035 ret
= event_read_format(event
);
3037 die("failed to read event format");
3039 ret
= event_read_print(event
);
3041 die("failed to read event print fmt");
3043 event
->system
= strdup(sys
);
3045 #define PRINT_ARGS 0
3046 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3047 print_args(event
->print_fmt
.args
);
3053 void parse_set_info(int nr_cpus
, int long_sz
)
3056 long_size
= long_sz
;