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
;
45 static char *input_buf
;
46 static unsigned long long input_buf_ptr
;
47 static unsigned long long input_buf_siz
;
52 static void init_input_buf(char *buf
, unsigned long long size
)
64 static struct cmdline
*cmdlines
;
65 static int cmdline_count
;
67 static int cmdline_cmp(const void *a
, const void *b
)
69 const struct cmdline
*ca
= a
;
70 const struct cmdline
*cb
= b
;
72 if (ca
->pid
< cb
->pid
)
74 if (ca
->pid
> cb
->pid
)
80 void parse_cmdlines(char *file
, int size __unused
)
83 struct cmdline_list
*next
;
86 } *list
= NULL
, *item
;
91 line
= strtok_r(file
, "\n", &next
);
93 item
= malloc_or_die(sizeof(*item
));
94 sscanf(line
, "%d %as", &item
->pid
,
95 (float *)(void *)&item
->comm
); /* workaround gcc warning */
98 line
= strtok_r(NULL
, "\n", &next
);
102 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
106 cmdlines
[i
].pid
= list
->pid
;
107 cmdlines
[i
].comm
= list
->comm
;
114 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
117 static struct func_map
{
118 unsigned long long addr
;
122 static unsigned int func_count
;
124 static int func_cmp(const void *a
, const void *b
)
126 const struct func_map
*fa
= a
;
127 const struct func_map
*fb
= b
;
129 if (fa
->addr
< fb
->addr
)
131 if (fa
->addr
> fb
->addr
)
137 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
140 struct func_list
*next
;
141 unsigned long long addr
;
144 } *list
= NULL
, *item
;
152 line
= strtok_r(file
, "\n", &next
);
154 item
= malloc_or_die(sizeof(*item
));
156 ret
= sscanf(line
, "%as %c %as\t[%as",
157 (float *)(void *)&addr_str
, /* workaround gcc warning */
159 (float *)(void *)&item
->func
,
160 (float *)(void *)&item
->mod
);
161 item
->addr
= strtoull(addr_str
, NULL
, 16);
164 /* truncate the extra ']' */
166 item
->mod
[strlen(item
->mod
) - 1] = 0;
171 line
= strtok_r(NULL
, "\n", &next
);
175 func_list
= malloc_or_die(sizeof(*func_list
) * func_count
+ 1);
179 func_list
[i
].func
= list
->func
;
180 func_list
[i
].addr
= list
->addr
;
181 func_list
[i
].mod
= list
->mod
;
188 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
191 * Add a special record at the end.
193 func_list
[func_count
].func
= NULL
;
194 func_list
[func_count
].addr
= 0;
195 func_list
[func_count
].mod
= NULL
;
199 * We are searching for a record in between, not an exact
202 static int func_bcmp(const void *a
, const void *b
)
204 const struct func_map
*fa
= a
;
205 const struct func_map
*fb
= b
;
207 if ((fa
->addr
== fb
->addr
) ||
209 (fa
->addr
> fb
->addr
&&
210 fa
->addr
< (fb
+1)->addr
))
213 if (fa
->addr
< fb
->addr
)
219 static struct func_map
*find_func(unsigned long long addr
)
221 struct func_map
*func
;
226 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
232 void print_funcs(void)
236 for (i
= 0; i
< (int)func_count
; i
++) {
240 if (func_list
[i
].mod
)
241 printf(" [%s]\n", func_list
[i
].mod
);
247 static struct printk_map
{
248 unsigned long long addr
;
251 static unsigned int printk_count
;
253 static int printk_cmp(const void *a
, const void *b
)
255 const struct func_map
*fa
= a
;
256 const struct func_map
*fb
= b
;
258 if (fa
->addr
< fb
->addr
)
260 if (fa
->addr
> fb
->addr
)
266 static struct printk_map
*find_printk(unsigned long long addr
)
268 struct printk_map
*printk
;
269 struct printk_map key
;
273 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
279 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
282 struct printk_list
*next
;
283 unsigned long long addr
;
285 } *list
= NULL
, *item
;
291 line
= strtok_r(file
, "\n", &next
);
293 addr_str
= strsep(&line
, ":");
295 warning("error parsing print strings");
298 item
= malloc_or_die(sizeof(*item
));
299 item
->addr
= strtoull(addr_str
, NULL
, 16);
300 /* fmt still has a space, skip it */
301 item
->printk
= strdup(line
+1);
304 line
= strtok_r(NULL
, "\n", &next
);
308 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
312 printk_list
[i
].printk
= list
->printk
;
313 printk_list
[i
].addr
= list
->addr
;
320 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
323 void print_printk(void)
327 for (i
= 0; i
< (int)printk_count
; i
++) {
328 printf("%016llx %s\n",
330 printk_list
[i
].printk
);
334 static struct event
*alloc_event(void)
338 event
= malloc_or_die(sizeof(*event
));
339 memset(event
, 0, sizeof(*event
));
356 static struct event
*event_list
;
358 static void add_event(struct event
*event
)
360 event
->next
= event_list
;
364 static int event_item_type(enum event_type type
)
367 case EVENT_ITEM
... EVENT_SQUOTE
:
369 case EVENT_ERROR
... EVENT_DELIM
:
375 static void free_arg(struct print_arg
*arg
)
383 free(arg
->atom
.atom
);
386 case PRINT_FIELD
... PRINT_OP
:
395 static enum event_type
get_type(int ch
)
398 return EVENT_NEWLINE
;
401 if (isalnum(ch
) || ch
== '_')
409 if (ch
== '(' || ch
== ')' || ch
== ',')
415 static int __read_char(void)
417 if (input_buf_ptr
>= input_buf_siz
)
420 return input_buf
[input_buf_ptr
++];
423 static int __peek_char(void)
425 if (input_buf_ptr
>= input_buf_siz
)
428 return input_buf
[input_buf_ptr
];
431 static enum event_type
__read_token(char **tok
)
434 int ch
, last_ch
, quote_ch
, next_ch
;
437 enum event_type type
;
447 if (type
== EVENT_NONE
)
455 *tok
= malloc_or_die(2);
463 next_ch
= __peek_char();
464 if (next_ch
== '>') {
465 buf
[i
++] = __read_char();
478 buf
[i
++] = __read_char();
490 default: /* what should we do instead? */
500 buf
[i
++] = __read_char();
505 /* don't keep quotes */
510 if (i
== (BUFSIZ
- 1)) {
513 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
528 /* the '\' '\' will cancel itself */
529 if (ch
== '\\' && last_ch
== '\\')
531 } while (ch
!= quote_ch
|| last_ch
== '\\');
532 /* remove the last quote */
536 case EVENT_ERROR
... EVENT_SPACE
:
542 while (get_type(__peek_char()) == type
) {
543 if (i
== (BUFSIZ
- 1)) {
546 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
565 *tok
= realloc(*tok
, tok_size
+ i
);
577 static void free_token(char *tok
)
583 static enum event_type
read_token(char **tok
)
585 enum event_type type
;
588 type
= __read_token(tok
);
589 if (type
!= EVENT_SPACE
)
600 static enum event_type
read_token_item(char **tok
)
602 enum event_type type
;
605 type
= __read_token(tok
);
606 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
616 static int test_type(enum event_type type
, enum event_type expect
)
618 if (type
!= expect
) {
619 warning("Error: expected type %d but read %d",
626 static int test_type_token(enum event_type type
, char *token
,
627 enum event_type expect
, const char *expect_tok
)
629 if (type
!= expect
) {
630 warning("Error: expected type %d but read %d",
635 if (strcmp(token
, expect_tok
) != 0) {
636 warning("Error: expected '%s' but read '%s'",
643 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
645 enum event_type type
;
648 type
= read_token(tok
);
650 type
= read_token_item(tok
);
651 return test_type(type
, expect
);
654 static int read_expect_type(enum event_type expect
, char **tok
)
656 return __read_expect_type(expect
, tok
, 1);
659 static int __read_expected(enum event_type expect
, const char *str
, int newline_ok
)
661 enum event_type type
;
666 type
= read_token(&token
);
668 type
= read_token_item(&token
);
670 ret
= test_type_token(type
, token
, expect
, str
);
677 static int read_expected(enum event_type expect
, const char *str
)
679 return __read_expected(expect
, str
, 1);
682 static int read_expected_item(enum event_type expect
, const char *str
)
684 return __read_expected(expect
, str
, 0);
687 static char *event_read_name(void)
691 if (read_expected(EVENT_ITEM
, "name") < 0)
694 if (read_expected(EVENT_OP
, ":") < 0)
697 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
707 static int event_read_id(void)
712 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
715 if (read_expected(EVENT_OP
, ":") < 0)
718 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
721 id
= strtoul(token
, NULL
, 0);
730 static int field_is_string(struct format_field
*field
)
732 if ((field
->flags
& FIELD_IS_ARRAY
) &&
733 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
734 !strstr(field
->type
, "s8")))
740 static int field_is_dynamic(struct format_field
*field
)
742 if (!strcmp(field
->type
, "__data_loc"))
748 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
750 struct format_field
*field
= NULL
;
751 enum event_type type
;
757 type
= read_token(&token
);
758 if (type
== EVENT_NEWLINE
) {
765 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
769 type
= read_token(&token
);
771 * The ftrace fields may still use the "special" name.
774 if (event
->flags
& EVENT_FL_ISFTRACE
&&
775 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
777 type
= read_token(&token
);
780 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
783 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
788 field
= malloc_or_die(sizeof(*field
));
789 memset(field
, 0, sizeof(*field
));
791 /* read the rest of the type */
793 type
= read_token(&token
);
794 if (type
== EVENT_ITEM
||
795 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
797 * Some of the ftrace fields are broken and have
798 * an illegal "." in them.
800 (event
->flags
& EVENT_FL_ISFTRACE
&&
801 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
803 if (strcmp(token
, "*") == 0)
804 field
->flags
|= FIELD_IS_POINTER
;
807 field
->type
= realloc(field
->type
,
808 strlen(field
->type
) +
809 strlen(last_token
) + 2);
810 strcat(field
->type
, " ");
811 strcat(field
->type
, last_token
);
813 field
->type
= last_token
;
822 die("no type found");
825 field
->name
= last_token
;
827 if (test_type(type
, EVENT_OP
))
830 if (strcmp(token
, "[") == 0) {
831 enum event_type last_type
= type
;
832 char *brackets
= token
;
835 field
->flags
|= FIELD_IS_ARRAY
;
837 type
= read_token(&token
);
838 while (strcmp(token
, "]") != 0) {
839 if (last_type
== EVENT_ITEM
&&
846 brackets
= realloc(brackets
,
848 strlen(token
) + len
);
850 strcat(brackets
, " ");
851 strcat(brackets
, token
);
853 type
= read_token(&token
);
854 if (type
== EVENT_NONE
) {
855 die("failed to find token");
862 brackets
= realloc(brackets
, strlen(brackets
) + 2);
863 strcat(brackets
, "]");
865 /* add brackets to type */
867 type
= read_token(&token
);
869 * If the next token is not an OP, then it is of
870 * the format: type [] item;
872 if (type
== EVENT_ITEM
) {
873 field
->type
= realloc(field
->type
,
874 strlen(field
->type
) +
875 strlen(field
->name
) +
876 strlen(brackets
) + 2);
877 strcat(field
->type
, " ");
878 strcat(field
->type
, field
->name
);
879 free_token(field
->name
);
880 strcat(field
->type
, brackets
);
882 type
= read_token(&token
);
884 field
->type
= realloc(field
->type
,
885 strlen(field
->type
) +
886 strlen(brackets
) + 1);
887 strcat(field
->type
, brackets
);
892 if (field_is_string(field
)) {
893 field
->flags
|= FIELD_IS_STRING
;
894 if (field_is_dynamic(field
))
895 field
->flags
|= FIELD_IS_DYNAMIC
;
898 if (test_type_token(type
, token
, EVENT_OP
, ";"))
902 if (read_expected(EVENT_ITEM
, "offset") < 0)
905 if (read_expected(EVENT_OP
, ":") < 0)
908 if (read_expect_type(EVENT_ITEM
, &token
))
910 field
->offset
= strtoul(token
, NULL
, 0);
913 if (read_expected(EVENT_OP
, ";") < 0)
916 if (read_expected(EVENT_ITEM
, "size") < 0)
919 if (read_expected(EVENT_OP
, ":") < 0)
922 if (read_expect_type(EVENT_ITEM
, &token
))
924 field
->size
= strtoul(token
, NULL
, 0);
927 if (read_expected(EVENT_OP
, ";") < 0)
930 type
= read_token(&token
);
931 if (type
!= EVENT_NEWLINE
) {
932 /* newer versions of the kernel have a "signed" type */
933 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
938 if (read_expected(EVENT_OP
, ":") < 0)
941 if (read_expect_type(EVENT_ITEM
, &token
))
944 /* add signed type */
947 if (read_expected(EVENT_OP
, ";") < 0)
950 if (read_expect_type(EVENT_NEWLINE
, &token
))
957 fields
= &field
->next
;
971 static int event_read_format(struct event
*event
)
976 if (read_expected_item(EVENT_ITEM
, "format") < 0)
979 if (read_expected(EVENT_OP
, ":") < 0)
982 if (read_expect_type(EVENT_NEWLINE
, &token
))
986 ret
= event_read_fields(event
, &event
->format
.common_fields
);
989 event
->format
.nr_common
= ret
;
991 ret
= event_read_fields(event
, &event
->format
.fields
);
994 event
->format
.nr_fields
= ret
;
1004 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1005 char **tok
, enum event_type type
);
1007 static enum event_type
1008 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1010 enum event_type type
;
1013 type
= read_token(&token
);
1016 return process_arg_token(event
, arg
, tok
, type
);
1019 static enum event_type
1020 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1022 struct print_arg
*arg
, *left
, *right
;
1023 enum event_type type
;
1026 arg
= malloc_or_die(sizeof(*arg
));
1027 memset(arg
, 0, sizeof(*arg
));
1029 left
= malloc_or_die(sizeof(*left
));
1031 right
= malloc_or_die(sizeof(*right
));
1033 arg
->type
= PRINT_OP
;
1034 arg
->op
.left
= left
;
1035 arg
->op
.right
= right
;
1038 type
= process_arg(event
, left
, &token
);
1039 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1044 type
= process_arg(event
, right
, &token
);
1046 top
->op
.right
= arg
;
1059 static enum event_type
1060 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1062 struct print_arg
*arg
;
1063 enum event_type type
;
1066 arg
= malloc_or_die(sizeof(*arg
));
1067 memset(arg
, 0, sizeof(*arg
));
1070 type
= process_arg(event
, arg
, &token
);
1071 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1074 top
->op
.right
= arg
;
1077 type
= read_token_item(&token
);
1088 static int get_op_prio(char *op
)
1099 /* '>>' and '<<' are 8 */
1103 /* '==' and '!=' are 10 */
1113 die("unknown op '%c'", op
[0]);
1117 if (strcmp(op
, "++") == 0 ||
1118 strcmp(op
, "--") == 0) {
1120 } else if (strcmp(op
, ">>") == 0 ||
1121 strcmp(op
, "<<") == 0) {
1123 } else if (strcmp(op
, ">=") == 0 ||
1124 strcmp(op
, "<=") == 0) {
1126 } else if (strcmp(op
, "==") == 0 ||
1127 strcmp(op
, "!=") == 0) {
1129 } else if (strcmp(op
, "&&") == 0) {
1131 } else if (strcmp(op
, "||") == 0) {
1134 die("unknown op '%s'", op
);
1140 static void set_op_prio(struct print_arg
*arg
)
1143 /* single ops are the greatest */
1144 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1149 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1152 static enum event_type
1153 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1155 struct print_arg
*left
, *right
= NULL
;
1156 enum event_type type
;
1159 /* the op is passed in via tok */
1162 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1163 /* handle single op */
1165 die("bad op token %s", token
);
1174 die("bad op token %s", token
);
1178 /* make an empty left */
1179 left
= malloc_or_die(sizeof(*left
));
1180 left
->type
= PRINT_NULL
;
1181 arg
->op
.left
= left
;
1183 right
= malloc_or_die(sizeof(*right
));
1184 arg
->op
.right
= right
;
1186 type
= process_arg(event
, right
, tok
);
1188 } else if (strcmp(token
, "?") == 0) {
1190 left
= malloc_or_die(sizeof(*left
));
1191 /* copy the top arg to the left */
1194 arg
->type
= PRINT_OP
;
1196 arg
->op
.left
= left
;
1199 type
= process_cond(event
, arg
, tok
);
1201 } else if (strcmp(token
, ">>") == 0 ||
1202 strcmp(token
, "<<") == 0 ||
1203 strcmp(token
, "&") == 0 ||
1204 strcmp(token
, "|") == 0 ||
1205 strcmp(token
, "&&") == 0 ||
1206 strcmp(token
, "||") == 0 ||
1207 strcmp(token
, "-") == 0 ||
1208 strcmp(token
, "+") == 0 ||
1209 strcmp(token
, "*") == 0 ||
1210 strcmp(token
, "^") == 0 ||
1211 strcmp(token
, "/") == 0 ||
1212 strcmp(token
, "<") == 0 ||
1213 strcmp(token
, ">") == 0 ||
1214 strcmp(token
, "==") == 0 ||
1215 strcmp(token
, "!=") == 0) {
1217 left
= malloc_or_die(sizeof(*left
));
1219 /* copy the top arg to the left */
1222 arg
->type
= PRINT_OP
;
1224 arg
->op
.left
= left
;
1228 right
= malloc_or_die(sizeof(*right
));
1230 type
= read_token_item(&token
);
1233 /* could just be a type pointer */
1234 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1235 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1236 if (left
->type
!= PRINT_ATOM
)
1237 die("bad pointer type");
1238 left
->atom
.atom
= realloc(left
->atom
.atom
,
1239 sizeof(left
->atom
.atom
) + 3);
1240 strcat(left
->atom
.atom
, " *");
1247 type
= process_arg_token(event
, right
, tok
, type
);
1249 arg
->op
.right
= right
;
1251 } else if (strcmp(token
, "[") == 0) {
1253 left
= malloc_or_die(sizeof(*left
));
1256 arg
->type
= PRINT_OP
;
1258 arg
->op
.left
= left
;
1261 type
= process_array(event
, arg
, tok
);
1264 warning("unknown op '%s'", token
);
1265 event
->flags
|= EVENT_FL_FAILED
;
1266 /* the arg is now the left side */
1270 if (type
== EVENT_OP
) {
1273 /* higher prios need to be closer to the root */
1274 prio
= get_op_prio(*tok
);
1276 if (prio
> arg
->op
.prio
)
1277 return process_op(event
, arg
, tok
);
1279 return process_op(event
, right
, tok
);
1285 static enum event_type
1286 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1289 enum event_type type
;
1293 if (read_expected(EVENT_OP
, "->") < 0)
1296 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1300 arg
->type
= PRINT_FIELD
;
1301 arg
->field
.name
= field
;
1303 type
= read_token(&token
);
1313 static char *arg_eval (struct print_arg
*arg
);
1315 static long long arg_num_eval(struct print_arg
*arg
)
1317 long long left
, right
;
1320 switch (arg
->type
) {
1322 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1325 val
= arg_num_eval(arg
->typecast
.item
);
1328 switch (arg
->op
.op
[0]) {
1330 left
= arg_num_eval(arg
->op
.left
);
1331 right
= arg_num_eval(arg
->op
.right
);
1333 val
= left
|| right
;
1338 left
= arg_num_eval(arg
->op
.left
);
1339 right
= arg_num_eval(arg
->op
.right
);
1341 val
= left
&& right
;
1346 left
= arg_num_eval(arg
->op
.left
);
1347 right
= arg_num_eval(arg
->op
.right
);
1348 switch (arg
->op
.op
[1]) {
1353 val
= left
<< right
;
1356 val
= left
<= right
;
1359 die("unknown op '%s'", arg
->op
.op
);
1363 left
= arg_num_eval(arg
->op
.left
);
1364 right
= arg_num_eval(arg
->op
.right
);
1365 switch (arg
->op
.op
[1]) {
1370 val
= left
>> right
;
1373 val
= left
>= right
;
1376 die("unknown op '%s'", arg
->op
.op
);
1380 left
= arg_num_eval(arg
->op
.left
);
1381 right
= arg_num_eval(arg
->op
.right
);
1383 if (arg
->op
.op
[1] != '=')
1384 die("unknown op '%s'", arg
->op
.op
);
1386 val
= left
== right
;
1389 left
= arg_num_eval(arg
->op
.left
);
1390 right
= arg_num_eval(arg
->op
.right
);
1392 switch (arg
->op
.op
[1]) {
1394 val
= left
!= right
;
1397 die("unknown op '%s'", arg
->op
.op
);
1401 die("unknown op '%s'", arg
->op
.op
);
1406 case PRINT_FIELD
... PRINT_SYMBOL
:
1409 die("invalid eval type %d", arg
->type
);
1415 static char *arg_eval (struct print_arg
*arg
)
1418 static char buf
[20];
1420 switch (arg
->type
) {
1422 return arg
->atom
.atom
;
1424 return arg_eval(arg
->typecast
.item
);
1426 val
= arg_num_eval(arg
);
1427 sprintf(buf
, "%lld", val
);
1431 case PRINT_FIELD
... PRINT_SYMBOL
:
1434 die("invalid eval type %d", arg
->type
);
1441 static enum event_type
1442 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1444 enum event_type type
;
1445 struct print_arg
*arg
= NULL
;
1446 struct print_flag_sym
*field
;
1452 type
= read_token_item(&token
);
1453 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1456 arg
= malloc_or_die(sizeof(*arg
));
1459 type
= process_arg(event
, arg
, &token
);
1460 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1463 field
= malloc_or_die(sizeof(*field
));
1464 memset(field
, 0, sizeof(field
));
1466 value
= arg_eval(arg
);
1467 field
->value
= strdup(value
);
1470 type
= process_arg(event
, arg
, &token
);
1471 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1474 value
= arg_eval(arg
);
1475 field
->str
= strdup(value
);
1480 list
= &field
->next
;
1483 type
= read_token_item(&token
);
1484 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1496 static enum event_type
1497 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1499 struct print_arg
*field
;
1500 enum event_type type
;
1503 memset(arg
, 0, sizeof(*arg
));
1504 arg
->type
= PRINT_FLAGS
;
1506 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1509 field
= malloc_or_die(sizeof(*field
));
1511 type
= process_arg(event
, field
, &token
);
1512 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1515 arg
->flags
.field
= field
;
1517 type
= read_token_item(&token
);
1518 if (event_item_type(type
)) {
1519 arg
->flags
.delim
= token
;
1520 type
= read_token_item(&token
);
1523 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1526 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1527 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1531 type
= read_token_item(tok
);
1539 static enum event_type
1540 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1542 struct print_arg
*field
;
1543 enum event_type type
;
1546 memset(arg
, 0, sizeof(*arg
));
1547 arg
->type
= PRINT_SYMBOL
;
1549 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1552 field
= malloc_or_die(sizeof(*field
));
1554 type
= process_arg(event
, field
, &token
);
1555 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1558 arg
->symbol
.field
= field
;
1560 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1561 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1565 type
= read_token_item(tok
);
1573 static enum event_type
1574 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1576 struct print_arg
*item_arg
;
1577 enum event_type type
;
1580 type
= process_arg(event
, arg
, &token
);
1582 if (type
== EVENT_ERROR
)
1585 if (type
== EVENT_OP
)
1586 type
= process_op(event
, arg
, &token
);
1588 if (type
== EVENT_ERROR
)
1591 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1597 type
= read_token_item(&token
);
1600 * If the next token is an item or another open paren, then
1601 * this was a typecast.
1603 if (event_item_type(type
) ||
1604 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1606 /* make this a typecast and contine */
1608 /* prevous must be an atom */
1609 if (arg
->type
!= PRINT_ATOM
)
1610 die("previous needed to be PRINT_ATOM");
1612 item_arg
= malloc_or_die(sizeof(*item_arg
));
1614 arg
->type
= PRINT_TYPE
;
1615 arg
->typecast
.type
= arg
->atom
.atom
;
1616 arg
->typecast
.item
= item_arg
;
1617 type
= process_arg_token(event
, item_arg
, &token
, type
);
1626 static enum event_type
1627 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1629 enum event_type type
;
1632 if (read_expected(EVENT_DELIM
, "(") < 0)
1635 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1638 arg
->type
= PRINT_STRING
;
1639 arg
->string
.string
= token
;
1640 arg
->string
.offset
= -1;
1642 if (read_expected(EVENT_DELIM
, ")") < 0)
1645 type
= read_token(&token
);
1655 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1656 char **tok
, enum event_type type
)
1665 if (strcmp(token
, "REC") == 0) {
1667 type
= process_entry(event
, arg
, &token
);
1668 } else if (strcmp(token
, "__print_flags") == 0) {
1670 type
= process_flags(event
, arg
, &token
);
1671 } else if (strcmp(token
, "__print_symbolic") == 0) {
1673 type
= process_symbols(event
, arg
, &token
);
1674 } else if (strcmp(token
, "__get_str") == 0) {
1676 type
= process_str(event
, arg
, &token
);
1679 /* test the next token */
1680 type
= read_token_item(&token
);
1682 /* atoms can be more than one token long */
1683 while (type
== EVENT_ITEM
) {
1684 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1686 strcat(atom
, token
);
1688 type
= read_token_item(&token
);
1691 /* todo, test for function */
1693 arg
->type
= PRINT_ATOM
;
1694 arg
->atom
.atom
= atom
;
1699 arg
->type
= PRINT_ATOM
;
1700 arg
->atom
.atom
= token
;
1701 type
= read_token_item(&token
);
1704 if (strcmp(token
, "(") == 0) {
1706 type
= process_paren(event
, arg
, &token
);
1710 /* handle single ops */
1711 arg
->type
= PRINT_OP
;
1713 arg
->op
.left
= NULL
;
1714 type
= process_op(event
, arg
, &token
);
1718 case EVENT_ERROR
... EVENT_NEWLINE
:
1720 die("unexpected type %d", type
);
1727 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1729 enum event_type type
= EVENT_ERROR
;
1730 struct print_arg
*arg
;
1735 if (type
== EVENT_NEWLINE
) {
1737 type
= read_token_item(&token
);
1741 arg
= malloc_or_die(sizeof(*arg
));
1742 memset(arg
, 0, sizeof(*arg
));
1744 type
= process_arg(event
, arg
, &token
);
1746 if (type
== EVENT_ERROR
) {
1754 if (type
== EVENT_OP
) {
1755 type
= process_op(event
, arg
, &token
);
1760 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1767 } while (type
!= EVENT_NONE
);
1769 if (type
!= EVENT_NONE
)
1775 static int event_read_print(struct event
*event
)
1777 enum event_type type
;
1781 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1784 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1787 if (read_expected(EVENT_OP
, ":") < 0)
1790 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1794 event
->print_fmt
.format
= token
;
1795 event
->print_fmt
.args
= NULL
;
1797 /* ok to have no arg */
1798 type
= read_token_item(&token
);
1800 if (type
== EVENT_NONE
)
1803 /* Handle concatination of print lines */
1804 if (type
== EVENT_DQUOTE
) {
1807 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1809 strcpy(cat
, event
->print_fmt
.format
);
1812 free_token(event
->print_fmt
.format
);
1813 event
->print_fmt
.format
= NULL
;
1818 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1823 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1834 static struct format_field
*
1835 find_common_field(struct event
*event
, const char *name
)
1837 struct format_field
*format
;
1839 for (format
= event
->format
.common_fields
;
1840 format
; format
= format
->next
) {
1841 if (strcmp(format
->name
, name
) == 0)
1848 static struct format_field
*
1849 find_field(struct event
*event
, const char *name
)
1851 struct format_field
*format
;
1853 for (format
= event
->format
.fields
;
1854 format
; format
= format
->next
) {
1855 if (strcmp(format
->name
, name
) == 0)
1862 static struct format_field
*
1863 find_any_field(struct event
*event
, const char *name
)
1865 struct format_field
*format
;
1867 format
= find_common_field(event
, name
);
1870 return find_field(event
, name
);
1873 static unsigned long long read_size(void *ptr
, int size
)
1877 return *(unsigned char *)ptr
;
1879 return data2host2(ptr
);
1881 return data2host4(ptr
);
1883 return data2host8(ptr
);
1891 raw_field_value(struct event
*event
, const char *name
, void *data
)
1893 struct format_field
*field
;
1895 field
= find_any_field(event
, name
);
1899 return read_size(data
+ field
->offset
, field
->size
);
1902 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1904 struct format_field
*field
;
1906 field
= find_any_field(event
, name
);
1910 return data
+ field
->offset
;
1913 static int get_common_info(const char *type
, int *offset
, int *size
)
1915 struct event
*event
;
1916 struct format_field
*field
;
1919 * All events should have the same common elements.
1920 * Pick any event to find where the type is;
1923 die("no event_list!");
1926 field
= find_common_field(event
, type
);
1928 die("field '%s' not found", type
);
1930 *offset
= field
->offset
;
1931 *size
= field
->size
;
1936 static int __parse_common(void *data
, int *size
, int *offset
,
1942 ret
= get_common_info(name
, offset
, size
);
1946 return read_size(data
+ *offset
, *size
);
1949 int trace_parse_common_type(void *data
)
1951 static int type_offset
;
1952 static int type_size
;
1954 return __parse_common(data
, &type_size
, &type_offset
,
1958 static int parse_common_pid(void *data
)
1960 static int pid_offset
;
1961 static int pid_size
;
1963 return __parse_common(data
, &pid_size
, &pid_offset
,
1967 static int parse_common_pc(void *data
)
1969 static int pc_offset
;
1972 return __parse_common(data
, &pc_size
, &pc_offset
,
1973 "common_preempt_count");
1976 static int parse_common_flags(void *data
)
1978 static int flags_offset
;
1979 static int flags_size
;
1981 return __parse_common(data
, &flags_size
, &flags_offset
,
1985 static int parse_common_lock_depth(void *data
)
1987 static int ld_offset
;
1991 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
1992 "common_lock_depth");
1999 struct event
*trace_find_event(int id
)
2001 struct event
*event
;
2003 for (event
= event_list
; event
; event
= event
->next
) {
2004 if (event
->id
== id
)
2010 static unsigned long long eval_num_arg(void *data
, int size
,
2011 struct event
*event
, struct print_arg
*arg
)
2013 unsigned long long val
= 0;
2014 unsigned long long left
, right
;
2015 struct print_arg
*larg
;
2017 switch (arg
->type
) {
2022 return strtoull(arg
->atom
.atom
, NULL
, 0);
2024 if (!arg
->field
.field
) {
2025 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2026 if (!arg
->field
.field
)
2027 die("field %s not found", arg
->field
.name
);
2029 /* must be a number */
2030 val
= read_size(data
+ arg
->field
.field
->offset
,
2031 arg
->field
.field
->size
);
2037 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2042 if (strcmp(arg
->op
.op
, "[") == 0) {
2044 * Arrays are special, since we don't want
2045 * to read the arg as is.
2047 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2048 goto default_op
; /* oops, all bets off */
2049 larg
= arg
->op
.left
;
2050 if (!larg
->field
.field
) {
2052 find_any_field(event
, larg
->field
.name
);
2053 if (!larg
->field
.field
)
2054 die("field %s not found", larg
->field
.name
);
2056 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2057 val
= read_size(data
+ larg
->field
.field
->offset
+
2058 right
* long_size
, long_size
);
2062 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2063 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2064 switch (arg
->op
.op
[0]) {
2067 val
= left
|| right
;
2073 val
= left
&& right
;
2078 switch (arg
->op
.op
[1]) {
2083 val
= left
<< right
;
2086 val
= left
<= right
;
2089 die("unknown op '%s'", arg
->op
.op
);
2093 switch (arg
->op
.op
[1]) {
2098 val
= left
>> right
;
2101 val
= left
>= right
;
2104 die("unknown op '%s'", arg
->op
.op
);
2108 if (arg
->op
.op
[1] != '=')
2109 die("unknown op '%s'", arg
->op
.op
);
2110 val
= left
== right
;
2119 die("unknown op '%s'", arg
->op
.op
);
2122 default: /* not sure what to do there */
2130 unsigned long long value
;
2133 static const struct flag flags
[] = {
2134 { "HI_SOFTIRQ", 0 },
2135 { "TIMER_SOFTIRQ", 1 },
2136 { "NET_TX_SOFTIRQ", 2 },
2137 { "NET_RX_SOFTIRQ", 3 },
2138 { "BLOCK_SOFTIRQ", 4 },
2139 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2140 { "TASKLET_SOFTIRQ", 6 },
2141 { "SCHED_SOFTIRQ", 7 },
2142 { "HRTIMER_SOFTIRQ", 8 },
2143 { "RCU_SOFTIRQ", 9 },
2145 { "HRTIMER_NORESTART", 0 },
2146 { "HRTIMER_RESTART", 1 },
2149 static unsigned long long eval_flag(const char *flag
)
2154 * Some flags in the format files do not get converted.
2155 * If the flag is not numeric, see if it is something that
2156 * we already know about.
2158 if (isdigit(flag
[0]))
2159 return strtoull(flag
, NULL
, 0);
2161 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2162 if (strcmp(flags
[i
].name
, flag
) == 0)
2163 return flags
[i
].value
;
2168 static void print_str_arg(void *data
, int size
,
2169 struct event
*event
, struct print_arg
*arg
)
2171 struct print_flag_sym
*flag
;
2172 unsigned long long val
, fval
;
2176 switch (arg
->type
) {
2181 printf("%s", arg
->atom
.atom
);
2184 if (!arg
->field
.field
) {
2185 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2186 if (!arg
->field
.field
)
2187 die("field %s not found", arg
->field
.name
);
2189 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2190 memcpy(str
, data
+ arg
->field
.field
->offset
,
2191 arg
->field
.field
->size
);
2192 str
[arg
->field
.field
->size
] = 0;
2197 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2199 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2200 fval
= eval_flag(flag
->value
);
2201 if (!val
&& !fval
) {
2202 printf("%s", flag
->str
);
2205 if (fval
&& (val
& fval
) == fval
) {
2206 if (print
&& arg
->flags
.delim
)
2207 printf("%s", arg
->flags
.delim
);
2208 printf("%s", flag
->str
);
2215 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2216 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2217 fval
= eval_flag(flag
->value
);
2219 printf("%s", flag
->str
);
2227 case PRINT_STRING
: {
2230 if (arg
->string
.offset
== -1) {
2231 struct format_field
*f
;
2233 f
= find_any_field(event
, arg
->string
.string
);
2234 arg
->string
.offset
= f
->offset
;
2236 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2237 str_offset
&= 0xffff;
2238 printf("%s", ((char *)data
) + str_offset
);
2243 * The only op for string should be ? :
2245 if (arg
->op
.op
[0] != '?')
2247 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2249 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2251 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2259 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2261 static struct format_field
*field
, *ip_field
;
2262 struct print_arg
*args
, *arg
, **next
;
2263 unsigned long long ip
, val
;
2268 field
= find_field(event
, "buf");
2270 die("can't find buffer field for binary printk");
2271 ip_field
= find_field(event
, "ip");
2273 die("can't find ip field for binary printk");
2276 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2279 * The first arg is the IP pointer.
2281 args
= malloc_or_die(sizeof(*args
));
2286 arg
->type
= PRINT_ATOM
;
2287 arg
->atom
.atom
= malloc_or_die(32);
2288 sprintf(arg
->atom
.atom
, "%lld", ip
);
2290 /* skip the first "%pf : " */
2291 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2292 bptr
< data
+ size
&& *ptr
; ptr
++) {
2316 /* the pointers are always 4 bytes aligned */
2317 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2329 val
= read_size(bptr
, ls
);
2331 arg
= malloc_or_die(sizeof(*arg
));
2333 arg
->type
= PRINT_ATOM
;
2334 arg
->atom
.atom
= malloc_or_die(32);
2335 sprintf(arg
->atom
.atom
, "%lld", val
);
2340 arg
= malloc_or_die(sizeof(*arg
));
2342 arg
->type
= PRINT_STRING
;
2343 arg
->string
.string
= strdup(bptr
);
2344 bptr
+= strlen(bptr
) + 1;
2356 static void free_args(struct print_arg
*args
)
2358 struct print_arg
*next
;
2363 if (args
->type
== PRINT_ATOM
)
2364 free(args
->atom
.atom
);
2366 free(args
->string
.string
);
2372 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2374 unsigned long long addr
;
2375 static struct format_field
*field
;
2376 struct printk_map
*printk
;
2381 field
= find_field(event
, "fmt");
2383 die("can't find format field for binary printk");
2384 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2387 addr
= read_size(data
+ field
->offset
, field
->size
);
2389 printk
= find_printk(addr
);
2391 format
= malloc_or_die(45);
2392 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2398 /* Remove any quotes. */
2401 format
= malloc_or_die(strlen(p
) + 10);
2402 sprintf(format
, "%s : %s", "%pf", p
);
2403 /* remove ending quotes and new line since we will add one too */
2404 p
= format
+ strlen(format
) - 1;
2409 if (strcmp(p
, "\\n") == 0)
2415 static void pretty_print(void *data
, int size
, struct event
*event
)
2417 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2418 struct print_arg
*arg
= print_fmt
->args
;
2419 struct print_arg
*args
= NULL
;
2420 const char *ptr
= print_fmt
->format
;
2421 unsigned long long val
;
2422 struct func_map
*func
;
2423 const char *saveptr
;
2424 char *bprint_fmt
= NULL
;
2430 if (event
->flags
& EVENT_FL_ISFUNC
)
2431 ptr
= " %pF <-- %pF";
2433 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2434 bprint_fmt
= get_bprint_format(data
, size
, event
);
2435 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2440 for (; *ptr
; ptr
++) {
2462 } else if (*ptr
== '%') {
2487 if (*(ptr
+1) == 'F' ||
2500 die("no argument match");
2502 len
= ((unsigned long)ptr
+ 1) -
2503 (unsigned long)saveptr
;
2505 /* should never happen */
2509 memcpy(format
, saveptr
, len
);
2512 val
= eval_num_arg(data
, size
, event
, arg
);
2516 func
= find_func(val
);
2518 printf("%s", func
->func
);
2519 if (show_func
== 'F')
2527 printf(format
, (int)val
);
2530 printf(format
, (long)val
);
2533 printf(format
, (long long)val
);
2536 die("bad count (%d)", ls
);
2541 die("no matching argument");
2543 print_str_arg(data
, size
, event
, arg
);
2547 printf(">%c<", *ptr
);
2560 static inline int log10_cpu(int nb
)
2569 static void print_lat_fmt(void *data
, int size __unused
)
2571 unsigned int lat_flags
;
2577 lat_flags
= parse_common_flags(data
);
2578 pc
= parse_common_pc(data
);
2579 lock_depth
= parse_common_lock_depth(data
);
2581 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2582 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2585 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2586 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2588 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2590 (hardirq
&& softirq
) ? 'H' :
2591 hardirq
? 'h' : softirq
? 's' : '.');
2601 printf("%d", lock_depth
);
2604 /* taken from Linux, written by Frederic Weisbecker */
2605 static void print_graph_cpu(int cpu
)
2608 int log10_this
= log10_cpu(cpu
);
2609 int log10_all
= log10_cpu(cpus
);
2613 * Start with a space character - to make it stand out
2614 * to the right a bit when trace output is pasted into
2620 * Tricky - we space the CPU field according to the max
2621 * number of online CPUs. On a 2-cpu system it would take
2622 * a maximum of 1 digit - on a 128 cpu system it would
2623 * take up to 3 digits:
2625 for (i
= 0; i
< log10_all
- log10_this
; i
++)
2628 printf("%d) ", cpu
);
2631 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2632 #define TRACE_GRAPH_INDENT 2
2634 static void print_graph_proc(int pid
, const char *comm
)
2636 /* sign + log10(MAX_INT) + '\0' */
2642 sprintf(pid_str
, "%d", pid
);
2644 /* 1 stands for the "-" character */
2645 len
= strlen(comm
) + strlen(pid_str
) + 1;
2647 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
2648 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
2650 /* First spaces to align center */
2651 for (i
= 0; i
< spaces
/ 2; i
++)
2654 printf("%s-%s", comm
, pid_str
);
2656 /* Last spaces to align center */
2657 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++)
2661 static struct record
*
2662 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2663 struct record
*next
)
2665 struct format_field
*field
;
2666 struct event
*event
;
2671 type
= trace_parse_common_type(next
->data
);
2672 event
= trace_find_event(type
);
2676 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2679 pid
= parse_common_pid(next
->data
);
2680 field
= find_field(event
, "func");
2682 die("function return does not have field func");
2684 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2686 if (cur_pid
!= pid
|| cur_func
!= val
)
2689 /* this is a leaf, now advance the iterator */
2690 return trace_read_data(cpu
);
2693 /* Signal a overhead of time execution to the output */
2694 static void print_graph_overhead(unsigned long long duration
)
2696 /* Non nested entry or return */
2697 if (duration
== ~0ULL)
2698 return (void)printf(" ");
2700 /* Duration exceeded 100 msecs */
2701 if (duration
> 100000ULL)
2702 return (void)printf("! ");
2704 /* Duration exceeded 10 msecs */
2705 if (duration
> 10000ULL)
2706 return (void)printf("+ ");
2711 static void print_graph_duration(unsigned long long duration
)
2713 unsigned long usecs
= duration
/ 1000;
2714 unsigned long nsecs_rem
= duration
% 1000;
2715 /* log10(ULONG_MAX) + '\0' */
2721 sprintf(msecs_str
, "%lu", usecs
);
2724 len
= printf("%lu", usecs
);
2726 /* Print nsecs (we don't want to exceed 7 numbers) */
2728 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2729 len
+= printf(".%s", nsecs_str
);
2734 /* Print remaining spaces to fit the row's width */
2735 for (i
= len
; i
< 7; i
++)
2742 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2744 unsigned long long rettime
, calltime
;
2745 unsigned long long duration
, depth
;
2746 unsigned long long val
;
2747 struct format_field
*field
;
2748 struct func_map
*func
;
2749 struct event
*ret_event
;
2753 type
= trace_parse_common_type(ret_rec
->data
);
2754 ret_event
= trace_find_event(type
);
2756 field
= find_field(ret_event
, "rettime");
2758 die("can't find rettime in return graph");
2759 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2761 field
= find_field(ret_event
, "calltime");
2763 die("can't find rettime in return graph");
2764 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2766 duration
= rettime
- calltime
;
2769 print_graph_overhead(duration
);
2772 print_graph_duration(duration
);
2774 field
= find_field(event
, "depth");
2776 die("can't find depth in entry graph");
2777 depth
= read_size(data
+ field
->offset
, field
->size
);
2780 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2783 field
= find_field(event
, "func");
2785 die("can't find func in entry graph");
2786 val
= read_size(data
+ field
->offset
, field
->size
);
2787 func
= find_func(val
);
2790 printf("%s();", func
->func
);
2792 printf("%llx();", val
);
2795 static void print_graph_nested(struct event
*event
, void *data
)
2797 struct format_field
*field
;
2798 unsigned long long depth
;
2799 unsigned long long val
;
2800 struct func_map
*func
;
2804 print_graph_overhead(-1);
2809 field
= find_field(event
, "depth");
2811 die("can't find depth in entry graph");
2812 depth
= read_size(data
+ field
->offset
, field
->size
);
2815 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2818 field
= find_field(event
, "func");
2820 die("can't find func in entry graph");
2821 val
= read_size(data
+ field
->offset
, field
->size
);
2822 func
= find_func(val
);
2825 printf("%s() {", func
->func
);
2827 printf("%llx() {", val
);
2831 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2832 int cpu
, int pid
, const char *comm
,
2833 unsigned long secs
, unsigned long usecs
)
2835 struct format_field
*field
;
2840 printf("%5lu.%06lu | ", secs
, usecs
);
2842 print_graph_cpu(cpu
);
2843 print_graph_proc(pid
, comm
);
2847 if (latency_format
) {
2848 print_lat_fmt(data
, size
);
2852 field
= find_field(event
, "func");
2854 die("function entry does not have func field");
2856 val
= read_size(data
+ field
->offset
, field
->size
);
2859 * peek_data may unmap the data pointer. Copy it first.
2861 copy_data
= malloc_or_die(size
);
2862 memcpy(copy_data
, data
, size
);
2865 rec
= trace_peek_data(cpu
);
2867 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2869 print_graph_entry_leaf(event
, data
, rec
);
2873 print_graph_nested(event
, data
);
2879 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
,
2880 int cpu
, int pid
, const char *comm
,
2881 unsigned long secs
, unsigned long usecs
)
2883 unsigned long long rettime
, calltime
;
2884 unsigned long long duration
, depth
;
2885 struct format_field
*field
;
2888 printf("%5lu.%06lu | ", secs
, usecs
);
2890 print_graph_cpu(cpu
);
2891 print_graph_proc(pid
, comm
);
2895 if (latency_format
) {
2896 print_lat_fmt(data
, size
);
2900 field
= find_field(event
, "rettime");
2902 die("can't find rettime in return graph");
2903 rettime
= read_size(data
+ field
->offset
, field
->size
);
2905 field
= find_field(event
, "calltime");
2907 die("can't find calltime in return graph");
2908 calltime
= read_size(data
+ field
->offset
, field
->size
);
2910 duration
= rettime
- calltime
;
2913 print_graph_overhead(duration
);
2916 print_graph_duration(duration
);
2918 field
= find_field(event
, "depth");
2920 die("can't find depth in entry graph");
2921 depth
= read_size(data
+ field
->offset
, field
->size
);
2924 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2931 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2932 int cpu
, int pid
, const char *comm
,
2933 unsigned long secs
, unsigned long usecs
)
2935 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2936 pretty_print_func_ent(data
, size
, event
,
2937 cpu
, pid
, comm
, secs
, usecs
);
2938 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2939 pretty_print_func_ret(data
, size
, event
,
2940 cpu
, pid
, comm
, secs
, usecs
);
2944 void print_event(int cpu
, void *data
, int size
, unsigned long long nsecs
,
2947 struct event
*event
;
2949 unsigned long usecs
;
2953 secs
= nsecs
/ NSECS_PER_SEC
;
2954 nsecs
-= secs
* NSECS_PER_SEC
;
2955 usecs
= nsecs
/ NSECS_PER_USEC
;
2957 type
= trace_parse_common_type(data
);
2959 event
= trace_find_event(type
);
2961 warning("ug! no event found for type %d", type
);
2965 pid
= parse_common_pid(data
);
2967 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2968 return pretty_print_func_graph(data
, size
, event
, cpu
,
2969 pid
, comm
, secs
, usecs
);
2971 if (latency_format
) {
2972 printf("%8.8s-%-5d %3d",
2974 print_lat_fmt(data
, size
);
2976 printf("%16s-%-5d [%03d]", comm
, pid
, cpu
);
2978 printf(" %5lu.%06lu: %s: ", secs
, usecs
, event
->name
);
2980 if (event
->flags
& EVENT_FL_FAILED
) {
2981 printf("EVENT '%s' FAILED TO PARSE\n",
2986 pretty_print(data
, size
, event
);
2990 static void print_fields(struct print_flag_sym
*field
)
2992 printf("{ %s, %s }", field
->value
, field
->str
);
2995 print_fields(field
->next
);
2999 static void print_args(struct print_arg
*args
)
3001 int print_paren
= 1;
3003 switch (args
->type
) {
3008 printf("%s", args
->atom
.atom
);
3011 printf("REC->%s", args
->field
.name
);
3014 printf("__print_flags(");
3015 print_args(args
->flags
.field
);
3016 printf(", %s, ", args
->flags
.delim
);
3017 print_fields(args
->flags
.flags
);
3021 printf("__print_symbolic(");
3022 print_args(args
->symbol
.field
);
3024 print_fields(args
->symbol
.symbols
);
3028 printf("__get_str(%s)", args
->string
.string
);
3031 printf("(%s)", args
->typecast
.type
);
3032 print_args(args
->typecast
.item
);
3035 if (strcmp(args
->op
.op
, ":") == 0)
3039 print_args(args
->op
.left
);
3040 printf(" %s ", args
->op
.op
);
3041 print_args(args
->op
.right
);
3046 /* we should warn... */
3051 print_args(args
->next
);
3055 static void parse_header_field(const char *field
,
3056 int *offset
, int *size
)
3061 if (read_expected(EVENT_ITEM
, "field") < 0)
3063 if (read_expected(EVENT_OP
, ":") < 0)
3067 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3071 if (read_expected(EVENT_ITEM
, field
) < 0)
3073 if (read_expected(EVENT_OP
, ";") < 0)
3075 if (read_expected(EVENT_ITEM
, "offset") < 0)
3077 if (read_expected(EVENT_OP
, ":") < 0)
3079 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3081 *offset
= atoi(token
);
3083 if (read_expected(EVENT_OP
, ";") < 0)
3085 if (read_expected(EVENT_ITEM
, "size") < 0)
3087 if (read_expected(EVENT_OP
, ":") < 0)
3089 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3091 *size
= atoi(token
);
3093 if (read_expected(EVENT_OP
, ";") < 0)
3095 type
= read_token(&token
);
3096 if (type
!= EVENT_NEWLINE
) {
3097 /* newer versions of the kernel have a "signed" type */
3098 if (type
!= EVENT_ITEM
)
3101 if (strcmp(token
, "signed") != 0)
3106 if (read_expected(EVENT_OP
, ":") < 0)
3109 if (read_expect_type(EVENT_ITEM
, &token
))
3113 if (read_expected(EVENT_OP
, ";") < 0)
3116 if (read_expect_type(EVENT_NEWLINE
, &token
))
3123 int parse_header_page(char *buf
, unsigned long size
)
3125 init_input_buf(buf
, size
);
3127 parse_header_field("timestamp", &header_page_ts_offset
,
3128 &header_page_ts_size
);
3129 parse_header_field("commit", &header_page_size_offset
,
3130 &header_page_size_size
);
3131 parse_header_field("data", &header_page_data_offset
,
3132 &header_page_data_size
);
3137 int parse_ftrace_file(char *buf
, unsigned long size
)
3139 struct format_field
*field
;
3140 struct print_arg
*arg
, **list
;
3141 struct event
*event
;
3144 init_input_buf(buf
, size
);
3146 event
= alloc_event();
3150 event
->flags
|= EVENT_FL_ISFTRACE
;
3152 event
->name
= event_read_name();
3154 die("failed to read ftrace event name");
3156 if (strcmp(event
->name
, "function") == 0)
3157 event
->flags
|= EVENT_FL_ISFUNC
;
3159 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3160 event
->flags
|= EVENT_FL_ISFUNCENT
;
3162 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3163 event
->flags
|= EVENT_FL_ISFUNCRET
;
3165 else if (strcmp(event
->name
, "bprint") == 0)
3166 event
->flags
|= EVENT_FL_ISBPRINT
;
3168 event
->id
= event_read_id();
3170 die("failed to read ftrace event id");
3174 ret
= event_read_format(event
);
3176 die("failed to read ftrace event format");
3178 ret
= event_read_print(event
);
3180 die("failed to read ftrace event print fmt");
3182 /* New ftrace handles args */
3186 * The arguments for ftrace files are parsed by the fields.
3187 * Set up the fields as their arguments.
3189 list
= &event
->print_fmt
.args
;
3190 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3191 arg
= malloc_or_die(sizeof(*arg
));
3192 memset(arg
, 0, sizeof(*arg
));
3195 arg
->type
= PRINT_FIELD
;
3196 arg
->field
.name
= field
->name
;
3197 arg
->field
.field
= field
;
3202 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3204 struct event
*event
;
3207 init_input_buf(buf
, size
);
3209 event
= alloc_event();
3213 event
->name
= event_read_name();
3215 die("failed to read event name");
3217 event
->id
= event_read_id();
3219 die("failed to read event id");
3221 ret
= event_read_format(event
);
3223 warning("failed to read event format for %s", event
->name
);
3227 ret
= event_read_print(event
);
3229 warning("failed to read event print fmt for %s", event
->name
);
3233 event
->system
= strdup(sys
);
3235 #define PRINT_ARGS 0
3236 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3237 print_args(event
->print_fmt
.args
);
3243 event
->flags
|= EVENT_FL_FAILED
;
3244 /* still add it even if it failed */
3249 void parse_set_info(int nr_cpus
, int long_sz
)
3252 long_size
= long_sz
;