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
;
51 static int is_flag_field
;
52 static int is_symbolic_field
;
54 static struct format_field
*
55 find_any_field(struct event
*event
, const char *name
);
57 static void init_input_buf(char *buf
, unsigned long long size
)
69 static struct cmdline
*cmdlines
;
70 static int cmdline_count
;
72 static int cmdline_cmp(const void *a
, const void *b
)
74 const struct cmdline
*ca
= a
;
75 const struct cmdline
*cb
= b
;
77 if (ca
->pid
< cb
->pid
)
79 if (ca
->pid
> cb
->pid
)
85 void parse_cmdlines(char *file
, int size __unused
)
88 struct cmdline_list
*next
;
91 } *list
= NULL
, *item
;
96 line
= strtok_r(file
, "\n", &next
);
98 item
= malloc_or_die(sizeof(*item
));
99 sscanf(line
, "%d %as", &item
->pid
,
100 (float *)(void *)&item
->comm
); /* workaround gcc warning */
103 line
= strtok_r(NULL
, "\n", &next
);
107 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
111 cmdlines
[i
].pid
= list
->pid
;
112 cmdlines
[i
].comm
= list
->comm
;
119 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
122 static struct func_map
{
123 unsigned long long addr
;
127 static unsigned int func_count
;
129 static int func_cmp(const void *a
, const void *b
)
131 const struct func_map
*fa
= a
;
132 const struct func_map
*fb
= b
;
134 if (fa
->addr
< fb
->addr
)
136 if (fa
->addr
> fb
->addr
)
142 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
145 struct func_list
*next
;
146 unsigned long long addr
;
149 } *list
= NULL
, *item
;
157 line
= strtok_r(file
, "\n", &next
);
159 item
= malloc_or_die(sizeof(*item
));
161 ret
= sscanf(line
, "%as %c %as\t[%as",
162 (float *)(void *)&addr_str
, /* workaround gcc warning */
164 (float *)(void *)&item
->func
,
165 (float *)(void *)&item
->mod
);
166 item
->addr
= strtoull(addr_str
, NULL
, 16);
169 /* truncate the extra ']' */
171 item
->mod
[strlen(item
->mod
) - 1] = 0;
176 line
= strtok_r(NULL
, "\n", &next
);
180 func_list
= malloc_or_die(sizeof(*func_list
) * func_count
+ 1);
184 func_list
[i
].func
= list
->func
;
185 func_list
[i
].addr
= list
->addr
;
186 func_list
[i
].mod
= list
->mod
;
193 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
196 * Add a special record at the end.
198 func_list
[func_count
].func
= NULL
;
199 func_list
[func_count
].addr
= 0;
200 func_list
[func_count
].mod
= NULL
;
204 * We are searching for a record in between, not an exact
207 static int func_bcmp(const void *a
, const void *b
)
209 const struct func_map
*fa
= a
;
210 const struct func_map
*fb
= b
;
212 if ((fa
->addr
== fb
->addr
) ||
214 (fa
->addr
> fb
->addr
&&
215 fa
->addr
< (fb
+1)->addr
))
218 if (fa
->addr
< fb
->addr
)
224 static struct func_map
*find_func(unsigned long long addr
)
226 struct func_map
*func
;
231 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
237 void print_funcs(void)
241 for (i
= 0; i
< (int)func_count
; i
++) {
245 if (func_list
[i
].mod
)
246 printf(" [%s]\n", func_list
[i
].mod
);
252 static struct printk_map
{
253 unsigned long long addr
;
256 static unsigned int printk_count
;
258 static int printk_cmp(const void *a
, const void *b
)
260 const struct func_map
*fa
= a
;
261 const struct func_map
*fb
= b
;
263 if (fa
->addr
< fb
->addr
)
265 if (fa
->addr
> fb
->addr
)
271 static struct printk_map
*find_printk(unsigned long long addr
)
273 struct printk_map
*printk
;
274 struct printk_map key
;
278 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
284 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
287 struct printk_list
*next
;
288 unsigned long long addr
;
290 } *list
= NULL
, *item
;
296 line
= strtok_r(file
, "\n", &next
);
298 addr_str
= strsep(&line
, ":");
300 warning("error parsing print strings");
303 item
= malloc_or_die(sizeof(*item
));
304 item
->addr
= strtoull(addr_str
, NULL
, 16);
305 /* fmt still has a space, skip it */
306 item
->printk
= strdup(line
+1);
309 line
= strtok_r(NULL
, "\n", &next
);
313 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
317 printk_list
[i
].printk
= list
->printk
;
318 printk_list
[i
].addr
= list
->addr
;
325 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
328 void print_printk(void)
332 for (i
= 0; i
< (int)printk_count
; i
++) {
333 printf("%016llx %s\n",
335 printk_list
[i
].printk
);
339 static struct event
*alloc_event(void)
343 event
= malloc_or_die(sizeof(*event
));
344 memset(event
, 0, sizeof(*event
));
361 static struct event
*event_list
;
363 static void add_event(struct event
*event
)
365 event
->next
= event_list
;
369 static int event_item_type(enum event_type type
)
372 case EVENT_ITEM
... EVENT_SQUOTE
:
374 case EVENT_ERROR
... EVENT_DELIM
:
380 static void free_arg(struct print_arg
*arg
)
388 free(arg
->atom
.atom
);
391 case PRINT_FIELD
... PRINT_OP
:
400 static enum event_type
get_type(int ch
)
403 return EVENT_NEWLINE
;
406 if (isalnum(ch
) || ch
== '_')
414 if (ch
== '(' || ch
== ')' || ch
== ',')
420 static int __read_char(void)
422 if (input_buf_ptr
>= input_buf_siz
)
425 return input_buf
[input_buf_ptr
++];
428 static int __peek_char(void)
430 if (input_buf_ptr
>= input_buf_siz
)
433 return input_buf
[input_buf_ptr
];
436 static enum event_type
__read_token(char **tok
)
439 int ch
, last_ch
, quote_ch
, next_ch
;
442 enum event_type type
;
452 if (type
== EVENT_NONE
)
460 *tok
= malloc_or_die(2);
468 next_ch
= __peek_char();
469 if (next_ch
== '>') {
470 buf
[i
++] = __read_char();
483 buf
[i
++] = __read_char();
495 default: /* what should we do instead? */
505 buf
[i
++] = __read_char();
510 /* don't keep quotes */
515 if (i
== (BUFSIZ
- 1)) {
518 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
533 /* the '\' '\' will cancel itself */
534 if (ch
== '\\' && last_ch
== '\\')
536 } while (ch
!= quote_ch
|| last_ch
== '\\');
537 /* remove the last quote */
541 case EVENT_ERROR
... EVENT_SPACE
:
547 while (get_type(__peek_char()) == type
) {
548 if (i
== (BUFSIZ
- 1)) {
551 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
570 *tok
= realloc(*tok
, tok_size
+ i
);
582 static void free_token(char *tok
)
588 static enum event_type
read_token(char **tok
)
590 enum event_type type
;
593 type
= __read_token(tok
);
594 if (type
!= EVENT_SPACE
)
605 static enum event_type
read_token_item(char **tok
)
607 enum event_type type
;
610 type
= __read_token(tok
);
611 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
621 static int test_type(enum event_type type
, enum event_type expect
)
623 if (type
!= expect
) {
624 warning("Error: expected type %d but read %d",
631 static int test_type_token(enum event_type type
, char *token
,
632 enum event_type expect
, const char *expect_tok
)
634 if (type
!= expect
) {
635 warning("Error: expected type %d but read %d",
640 if (strcmp(token
, expect_tok
) != 0) {
641 warning("Error: expected '%s' but read '%s'",
648 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
650 enum event_type type
;
653 type
= read_token(tok
);
655 type
= read_token_item(tok
);
656 return test_type(type
, expect
);
659 static int read_expect_type(enum event_type expect
, char **tok
)
661 return __read_expect_type(expect
, tok
, 1);
664 static int __read_expected(enum event_type expect
, const char *str
, int newline_ok
)
666 enum event_type type
;
671 type
= read_token(&token
);
673 type
= read_token_item(&token
);
675 ret
= test_type_token(type
, token
, expect
, str
);
682 static int read_expected(enum event_type expect
, const char *str
)
684 return __read_expected(expect
, str
, 1);
687 static int read_expected_item(enum event_type expect
, const char *str
)
689 return __read_expected(expect
, str
, 0);
692 static char *event_read_name(void)
696 if (read_expected(EVENT_ITEM
, "name") < 0)
699 if (read_expected(EVENT_OP
, ":") < 0)
702 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
712 static int event_read_id(void)
717 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
720 if (read_expected(EVENT_OP
, ":") < 0)
723 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
726 id
= strtoul(token
, NULL
, 0);
735 static int field_is_string(struct format_field
*field
)
737 if ((field
->flags
& FIELD_IS_ARRAY
) &&
738 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
739 !strstr(field
->type
, "s8")))
745 static int field_is_dynamic(struct format_field
*field
)
747 if (!strcmp(field
->type
, "__data_loc"))
753 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
755 struct format_field
*field
= NULL
;
756 enum event_type type
;
762 type
= read_token(&token
);
763 if (type
== EVENT_NEWLINE
) {
770 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
774 type
= read_token(&token
);
776 * The ftrace fields may still use the "special" name.
779 if (event
->flags
& EVENT_FL_ISFTRACE
&&
780 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
782 type
= read_token(&token
);
785 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
788 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
793 field
= malloc_or_die(sizeof(*field
));
794 memset(field
, 0, sizeof(*field
));
796 /* read the rest of the type */
798 type
= read_token(&token
);
799 if (type
== EVENT_ITEM
||
800 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
802 * Some of the ftrace fields are broken and have
803 * an illegal "." in them.
805 (event
->flags
& EVENT_FL_ISFTRACE
&&
806 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
808 if (strcmp(token
, "*") == 0)
809 field
->flags
|= FIELD_IS_POINTER
;
812 field
->type
= realloc(field
->type
,
813 strlen(field
->type
) +
814 strlen(last_token
) + 2);
815 strcat(field
->type
, " ");
816 strcat(field
->type
, last_token
);
818 field
->type
= last_token
;
827 die("no type found");
830 field
->name
= last_token
;
832 if (test_type(type
, EVENT_OP
))
835 if (strcmp(token
, "[") == 0) {
836 enum event_type last_type
= type
;
837 char *brackets
= token
;
840 field
->flags
|= FIELD_IS_ARRAY
;
842 type
= read_token(&token
);
843 while (strcmp(token
, "]") != 0) {
844 if (last_type
== EVENT_ITEM
&&
851 brackets
= realloc(brackets
,
853 strlen(token
) + len
);
855 strcat(brackets
, " ");
856 strcat(brackets
, token
);
858 type
= read_token(&token
);
859 if (type
== EVENT_NONE
) {
860 die("failed to find token");
867 brackets
= realloc(brackets
, strlen(brackets
) + 2);
868 strcat(brackets
, "]");
870 /* add brackets to type */
872 type
= read_token(&token
);
874 * If the next token is not an OP, then it is of
875 * the format: type [] item;
877 if (type
== EVENT_ITEM
) {
878 field
->type
= realloc(field
->type
,
879 strlen(field
->type
) +
880 strlen(field
->name
) +
881 strlen(brackets
) + 2);
882 strcat(field
->type
, " ");
883 strcat(field
->type
, field
->name
);
884 free_token(field
->name
);
885 strcat(field
->type
, brackets
);
887 type
= read_token(&token
);
889 field
->type
= realloc(field
->type
,
890 strlen(field
->type
) +
891 strlen(brackets
) + 1);
892 strcat(field
->type
, brackets
);
897 if (field_is_string(field
)) {
898 field
->flags
|= FIELD_IS_STRING
;
899 if (field_is_dynamic(field
))
900 field
->flags
|= FIELD_IS_DYNAMIC
;
903 if (test_type_token(type
, token
, EVENT_OP
, ";"))
907 if (read_expected(EVENT_ITEM
, "offset") < 0)
910 if (read_expected(EVENT_OP
, ":") < 0)
913 if (read_expect_type(EVENT_ITEM
, &token
))
915 field
->offset
= strtoul(token
, NULL
, 0);
918 if (read_expected(EVENT_OP
, ";") < 0)
921 if (read_expected(EVENT_ITEM
, "size") < 0)
924 if (read_expected(EVENT_OP
, ":") < 0)
927 if (read_expect_type(EVENT_ITEM
, &token
))
929 field
->size
= strtoul(token
, NULL
, 0);
932 if (read_expected(EVENT_OP
, ";") < 0)
935 type
= read_token(&token
);
936 if (type
!= EVENT_NEWLINE
) {
937 /* newer versions of the kernel have a "signed" type */
938 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
943 if (read_expected(EVENT_OP
, ":") < 0)
946 if (read_expect_type(EVENT_ITEM
, &token
))
949 if (strtoul(token
, NULL
, 0))
950 field
->flags
|= FIELD_IS_SIGNED
;
953 if (read_expected(EVENT_OP
, ";") < 0)
956 if (read_expect_type(EVENT_NEWLINE
, &token
))
963 fields
= &field
->next
;
977 static int event_read_format(struct event
*event
)
982 if (read_expected_item(EVENT_ITEM
, "format") < 0)
985 if (read_expected(EVENT_OP
, ":") < 0)
988 if (read_expect_type(EVENT_NEWLINE
, &token
))
992 ret
= event_read_fields(event
, &event
->format
.common_fields
);
995 event
->format
.nr_common
= ret
;
997 ret
= event_read_fields(event
, &event
->format
.fields
);
1000 event
->format
.nr_fields
= ret
;
1010 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1011 char **tok
, enum event_type type
);
1013 static enum event_type
1014 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1016 enum event_type type
;
1019 type
= read_token(&token
);
1022 return process_arg_token(event
, arg
, tok
, type
);
1025 static enum event_type
1026 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1028 struct print_arg
*arg
, *left
, *right
;
1029 enum event_type type
;
1032 arg
= malloc_or_die(sizeof(*arg
));
1033 memset(arg
, 0, sizeof(*arg
));
1035 left
= malloc_or_die(sizeof(*left
));
1037 right
= malloc_or_die(sizeof(*right
));
1039 arg
->type
= PRINT_OP
;
1040 arg
->op
.left
= left
;
1041 arg
->op
.right
= right
;
1044 type
= process_arg(event
, left
, &token
);
1045 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1050 type
= process_arg(event
, right
, &token
);
1052 top
->op
.right
= arg
;
1065 static enum event_type
1066 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1068 struct print_arg
*arg
;
1069 enum event_type type
;
1072 arg
= malloc_or_die(sizeof(*arg
));
1073 memset(arg
, 0, sizeof(*arg
));
1076 type
= process_arg(event
, arg
, &token
);
1077 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1080 top
->op
.right
= arg
;
1083 type
= read_token_item(&token
);
1094 static int get_op_prio(char *op
)
1105 /* '>>' and '<<' are 8 */
1109 /* '==' and '!=' are 10 */
1119 die("unknown op '%c'", op
[0]);
1123 if (strcmp(op
, "++") == 0 ||
1124 strcmp(op
, "--") == 0) {
1126 } else if (strcmp(op
, ">>") == 0 ||
1127 strcmp(op
, "<<") == 0) {
1129 } else if (strcmp(op
, ">=") == 0 ||
1130 strcmp(op
, "<=") == 0) {
1132 } else if (strcmp(op
, "==") == 0 ||
1133 strcmp(op
, "!=") == 0) {
1135 } else if (strcmp(op
, "&&") == 0) {
1137 } else if (strcmp(op
, "||") == 0) {
1140 die("unknown op '%s'", op
);
1146 static void set_op_prio(struct print_arg
*arg
)
1149 /* single ops are the greatest */
1150 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1155 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1158 static enum event_type
1159 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1161 struct print_arg
*left
, *right
= NULL
;
1162 enum event_type type
;
1165 /* the op is passed in via tok */
1168 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1169 /* handle single op */
1171 die("bad op token %s", token
);
1180 die("bad op token %s", token
);
1184 /* make an empty left */
1185 left
= malloc_or_die(sizeof(*left
));
1186 left
->type
= PRINT_NULL
;
1187 arg
->op
.left
= left
;
1189 right
= malloc_or_die(sizeof(*right
));
1190 arg
->op
.right
= right
;
1192 type
= process_arg(event
, right
, tok
);
1194 } else if (strcmp(token
, "?") == 0) {
1196 left
= malloc_or_die(sizeof(*left
));
1197 /* copy the top arg to the left */
1200 arg
->type
= PRINT_OP
;
1202 arg
->op
.left
= left
;
1205 type
= process_cond(event
, arg
, tok
);
1207 } else if (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 ||
1216 strcmp(token
, "^") == 0 ||
1217 strcmp(token
, "/") == 0 ||
1218 strcmp(token
, "<") == 0 ||
1219 strcmp(token
, ">") == 0 ||
1220 strcmp(token
, "==") == 0 ||
1221 strcmp(token
, "!=") == 0) {
1223 left
= malloc_or_die(sizeof(*left
));
1225 /* copy the top arg to the left */
1228 arg
->type
= PRINT_OP
;
1230 arg
->op
.left
= left
;
1234 right
= malloc_or_die(sizeof(*right
));
1236 type
= read_token_item(&token
);
1239 /* could just be a type pointer */
1240 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1241 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1242 if (left
->type
!= PRINT_ATOM
)
1243 die("bad pointer type");
1244 left
->atom
.atom
= realloc(left
->atom
.atom
,
1245 sizeof(left
->atom
.atom
) + 3);
1246 strcat(left
->atom
.atom
, " *");
1253 type
= process_arg_token(event
, right
, tok
, type
);
1255 arg
->op
.right
= right
;
1257 } else if (strcmp(token
, "[") == 0) {
1259 left
= malloc_or_die(sizeof(*left
));
1262 arg
->type
= PRINT_OP
;
1264 arg
->op
.left
= left
;
1267 type
= process_array(event
, arg
, tok
);
1270 warning("unknown op '%s'", token
);
1271 event
->flags
|= EVENT_FL_FAILED
;
1272 /* the arg is now the left side */
1276 if (type
== EVENT_OP
) {
1279 /* higher prios need to be closer to the root */
1280 prio
= get_op_prio(*tok
);
1282 if (prio
> arg
->op
.prio
)
1283 return process_op(event
, arg
, tok
);
1285 return process_op(event
, right
, tok
);
1291 static enum event_type
1292 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1295 enum event_type type
;
1299 if (read_expected(EVENT_OP
, "->") < 0)
1302 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1306 arg
->type
= PRINT_FIELD
;
1307 arg
->field
.name
= field
;
1309 if (is_flag_field
) {
1310 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1311 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1313 } else if (is_symbolic_field
) {
1314 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1315 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1316 is_symbolic_field
= 0;
1319 type
= read_token(&token
);
1329 static char *arg_eval (struct print_arg
*arg
);
1331 static long long arg_num_eval(struct print_arg
*arg
)
1333 long long left
, right
;
1336 switch (arg
->type
) {
1338 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1341 val
= arg_num_eval(arg
->typecast
.item
);
1344 switch (arg
->op
.op
[0]) {
1346 left
= arg_num_eval(arg
->op
.left
);
1347 right
= arg_num_eval(arg
->op
.right
);
1349 val
= left
|| right
;
1354 left
= arg_num_eval(arg
->op
.left
);
1355 right
= arg_num_eval(arg
->op
.right
);
1357 val
= left
&& right
;
1362 left
= arg_num_eval(arg
->op
.left
);
1363 right
= arg_num_eval(arg
->op
.right
);
1364 switch (arg
->op
.op
[1]) {
1369 val
= left
<< right
;
1372 val
= left
<= right
;
1375 die("unknown op '%s'", arg
->op
.op
);
1379 left
= arg_num_eval(arg
->op
.left
);
1380 right
= arg_num_eval(arg
->op
.right
);
1381 switch (arg
->op
.op
[1]) {
1386 val
= left
>> right
;
1389 val
= left
>= right
;
1392 die("unknown op '%s'", arg
->op
.op
);
1396 left
= arg_num_eval(arg
->op
.left
);
1397 right
= arg_num_eval(arg
->op
.right
);
1399 if (arg
->op
.op
[1] != '=')
1400 die("unknown op '%s'", arg
->op
.op
);
1402 val
= left
== right
;
1405 left
= arg_num_eval(arg
->op
.left
);
1406 right
= arg_num_eval(arg
->op
.right
);
1408 switch (arg
->op
.op
[1]) {
1410 val
= left
!= right
;
1413 die("unknown op '%s'", arg
->op
.op
);
1417 die("unknown op '%s'", arg
->op
.op
);
1422 case PRINT_FIELD
... PRINT_SYMBOL
:
1425 die("invalid eval type %d", arg
->type
);
1431 static char *arg_eval (struct print_arg
*arg
)
1434 static char buf
[20];
1436 switch (arg
->type
) {
1438 return arg
->atom
.atom
;
1440 return arg_eval(arg
->typecast
.item
);
1442 val
= arg_num_eval(arg
);
1443 sprintf(buf
, "%lld", val
);
1447 case PRINT_FIELD
... PRINT_SYMBOL
:
1450 die("invalid eval type %d", arg
->type
);
1457 static enum event_type
1458 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1460 enum event_type type
;
1461 struct print_arg
*arg
= NULL
;
1462 struct print_flag_sym
*field
;
1468 type
= read_token_item(&token
);
1469 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1472 arg
= malloc_or_die(sizeof(*arg
));
1475 type
= process_arg(event
, arg
, &token
);
1476 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1479 field
= malloc_or_die(sizeof(*field
));
1480 memset(field
, 0, sizeof(field
));
1482 value
= arg_eval(arg
);
1483 field
->value
= strdup(value
);
1486 type
= process_arg(event
, arg
, &token
);
1487 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1490 value
= arg_eval(arg
);
1491 field
->str
= strdup(value
);
1496 list
= &field
->next
;
1499 type
= read_token_item(&token
);
1500 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1512 static enum event_type
1513 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1515 struct print_arg
*field
;
1516 enum event_type type
;
1519 memset(arg
, 0, sizeof(*arg
));
1520 arg
->type
= PRINT_FLAGS
;
1522 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1525 field
= malloc_or_die(sizeof(*field
));
1527 type
= process_arg(event
, field
, &token
);
1528 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1531 arg
->flags
.field
= field
;
1533 type
= read_token_item(&token
);
1534 if (event_item_type(type
)) {
1535 arg
->flags
.delim
= token
;
1536 type
= read_token_item(&token
);
1539 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1542 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1543 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1547 type
= read_token_item(tok
);
1555 static enum event_type
1556 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1558 struct print_arg
*field
;
1559 enum event_type type
;
1562 memset(arg
, 0, sizeof(*arg
));
1563 arg
->type
= PRINT_SYMBOL
;
1565 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1568 field
= malloc_or_die(sizeof(*field
));
1570 type
= process_arg(event
, field
, &token
);
1571 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1574 arg
->symbol
.field
= field
;
1576 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1577 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1581 type
= read_token_item(tok
);
1589 static enum event_type
1590 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1592 struct print_arg
*item_arg
;
1593 enum event_type type
;
1596 type
= process_arg(event
, arg
, &token
);
1598 if (type
== EVENT_ERROR
)
1601 if (type
== EVENT_OP
)
1602 type
= process_op(event
, arg
, &token
);
1604 if (type
== EVENT_ERROR
)
1607 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1613 type
= read_token_item(&token
);
1616 * If the next token is an item or another open paren, then
1617 * this was a typecast.
1619 if (event_item_type(type
) ||
1620 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1622 /* make this a typecast and contine */
1624 /* prevous must be an atom */
1625 if (arg
->type
!= PRINT_ATOM
)
1626 die("previous needed to be PRINT_ATOM");
1628 item_arg
= malloc_or_die(sizeof(*item_arg
));
1630 arg
->type
= PRINT_TYPE
;
1631 arg
->typecast
.type
= arg
->atom
.atom
;
1632 arg
->typecast
.item
= item_arg
;
1633 type
= process_arg_token(event
, item_arg
, &token
, type
);
1642 static enum event_type
1643 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1645 enum event_type type
;
1648 if (read_expected(EVENT_DELIM
, "(") < 0)
1651 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1654 arg
->type
= PRINT_STRING
;
1655 arg
->string
.string
= token
;
1656 arg
->string
.offset
= -1;
1658 if (read_expected(EVENT_DELIM
, ")") < 0)
1661 type
= read_token(&token
);
1671 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1672 char **tok
, enum event_type type
)
1681 if (strcmp(token
, "REC") == 0) {
1683 type
= process_entry(event
, arg
, &token
);
1684 } else if (strcmp(token
, "__print_flags") == 0) {
1687 type
= process_flags(event
, arg
, &token
);
1688 } else if (strcmp(token
, "__print_symbolic") == 0) {
1690 is_symbolic_field
= 1;
1691 type
= process_symbols(event
, arg
, &token
);
1692 } else if (strcmp(token
, "__get_str") == 0) {
1694 type
= process_str(event
, arg
, &token
);
1697 /* test the next token */
1698 type
= read_token_item(&token
);
1700 /* atoms can be more than one token long */
1701 while (type
== EVENT_ITEM
) {
1702 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1704 strcat(atom
, token
);
1706 type
= read_token_item(&token
);
1709 /* todo, test for function */
1711 arg
->type
= PRINT_ATOM
;
1712 arg
->atom
.atom
= atom
;
1717 arg
->type
= PRINT_ATOM
;
1718 arg
->atom
.atom
= token
;
1719 type
= read_token_item(&token
);
1722 if (strcmp(token
, "(") == 0) {
1724 type
= process_paren(event
, arg
, &token
);
1728 /* handle single ops */
1729 arg
->type
= PRINT_OP
;
1731 arg
->op
.left
= NULL
;
1732 type
= process_op(event
, arg
, &token
);
1736 case EVENT_ERROR
... EVENT_NEWLINE
:
1738 die("unexpected type %d", type
);
1745 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1747 enum event_type type
= EVENT_ERROR
;
1748 struct print_arg
*arg
;
1753 if (type
== EVENT_NEWLINE
) {
1755 type
= read_token_item(&token
);
1759 arg
= malloc_or_die(sizeof(*arg
));
1760 memset(arg
, 0, sizeof(*arg
));
1762 type
= process_arg(event
, arg
, &token
);
1764 if (type
== EVENT_ERROR
) {
1772 if (type
== EVENT_OP
) {
1773 type
= process_op(event
, arg
, &token
);
1778 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1785 } while (type
!= EVENT_NONE
);
1787 if (type
!= EVENT_NONE
)
1793 static int event_read_print(struct event
*event
)
1795 enum event_type type
;
1799 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1802 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1805 if (read_expected(EVENT_OP
, ":") < 0)
1808 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1812 event
->print_fmt
.format
= token
;
1813 event
->print_fmt
.args
= NULL
;
1815 /* ok to have no arg */
1816 type
= read_token_item(&token
);
1818 if (type
== EVENT_NONE
)
1821 /* Handle concatination of print lines */
1822 if (type
== EVENT_DQUOTE
) {
1825 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1827 strcpy(cat
, event
->print_fmt
.format
);
1830 free_token(event
->print_fmt
.format
);
1831 event
->print_fmt
.format
= NULL
;
1836 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1841 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1852 static struct format_field
*
1853 find_common_field(struct event
*event
, const char *name
)
1855 struct format_field
*format
;
1857 for (format
= event
->format
.common_fields
;
1858 format
; format
= format
->next
) {
1859 if (strcmp(format
->name
, name
) == 0)
1866 static struct format_field
*
1867 find_field(struct event
*event
, const char *name
)
1869 struct format_field
*format
;
1871 for (format
= event
->format
.fields
;
1872 format
; format
= format
->next
) {
1873 if (strcmp(format
->name
, name
) == 0)
1880 static struct format_field
*
1881 find_any_field(struct event
*event
, const char *name
)
1883 struct format_field
*format
;
1885 format
= find_common_field(event
, name
);
1888 return find_field(event
, name
);
1891 static unsigned long long read_size(void *ptr
, int size
)
1895 return *(unsigned char *)ptr
;
1897 return data2host2(ptr
);
1899 return data2host4(ptr
);
1901 return data2host8(ptr
);
1909 raw_field_value(struct event
*event
, const char *name
, void *data
)
1911 struct format_field
*field
;
1913 field
= find_any_field(event
, name
);
1917 return read_size(data
+ field
->offset
, field
->size
);
1920 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1922 struct format_field
*field
;
1924 field
= find_any_field(event
, name
);
1928 return data
+ field
->offset
;
1931 static int get_common_info(const char *type
, int *offset
, int *size
)
1933 struct event
*event
;
1934 struct format_field
*field
;
1937 * All events should have the same common elements.
1938 * Pick any event to find where the type is;
1941 die("no event_list!");
1944 field
= find_common_field(event
, type
);
1946 die("field '%s' not found", type
);
1948 *offset
= field
->offset
;
1949 *size
= field
->size
;
1954 static int __parse_common(void *data
, int *size
, int *offset
,
1960 ret
= get_common_info(name
, offset
, size
);
1964 return read_size(data
+ *offset
, *size
);
1967 int trace_parse_common_type(void *data
)
1969 static int type_offset
;
1970 static int type_size
;
1972 return __parse_common(data
, &type_size
, &type_offset
,
1976 static int parse_common_pid(void *data
)
1978 static int pid_offset
;
1979 static int pid_size
;
1981 return __parse_common(data
, &pid_size
, &pid_offset
,
1985 static int parse_common_pc(void *data
)
1987 static int pc_offset
;
1990 return __parse_common(data
, &pc_size
, &pc_offset
,
1991 "common_preempt_count");
1994 static int parse_common_flags(void *data
)
1996 static int flags_offset
;
1997 static int flags_size
;
1999 return __parse_common(data
, &flags_size
, &flags_offset
,
2003 static int parse_common_lock_depth(void *data
)
2005 static int ld_offset
;
2009 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
2010 "common_lock_depth");
2017 struct event
*trace_find_event(int id
)
2019 struct event
*event
;
2021 for (event
= event_list
; event
; event
= event
->next
) {
2022 if (event
->id
== id
)
2028 static unsigned long long eval_num_arg(void *data
, int size
,
2029 struct event
*event
, struct print_arg
*arg
)
2031 unsigned long long val
= 0;
2032 unsigned long long left
, right
;
2033 struct print_arg
*larg
;
2035 switch (arg
->type
) {
2040 return strtoull(arg
->atom
.atom
, NULL
, 0);
2042 if (!arg
->field
.field
) {
2043 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2044 if (!arg
->field
.field
)
2045 die("field %s not found", arg
->field
.name
);
2047 /* must be a number */
2048 val
= read_size(data
+ arg
->field
.field
->offset
,
2049 arg
->field
.field
->size
);
2055 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2060 if (strcmp(arg
->op
.op
, "[") == 0) {
2062 * Arrays are special, since we don't want
2063 * to read the arg as is.
2065 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2066 goto default_op
; /* oops, all bets off */
2067 larg
= arg
->op
.left
;
2068 if (!larg
->field
.field
) {
2070 find_any_field(event
, larg
->field
.name
);
2071 if (!larg
->field
.field
)
2072 die("field %s not found", larg
->field
.name
);
2074 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2075 val
= read_size(data
+ larg
->field
.field
->offset
+
2076 right
* long_size
, long_size
);
2080 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2081 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2082 switch (arg
->op
.op
[0]) {
2085 val
= left
|| right
;
2091 val
= left
&& right
;
2096 switch (arg
->op
.op
[1]) {
2101 val
= left
<< right
;
2104 val
= left
<= right
;
2107 die("unknown op '%s'", arg
->op
.op
);
2111 switch (arg
->op
.op
[1]) {
2116 val
= left
>> right
;
2119 val
= left
>= right
;
2122 die("unknown op '%s'", arg
->op
.op
);
2126 if (arg
->op
.op
[1] != '=')
2127 die("unknown op '%s'", arg
->op
.op
);
2128 val
= left
== right
;
2137 die("unknown op '%s'", arg
->op
.op
);
2140 default: /* not sure what to do there */
2148 unsigned long long value
;
2151 static const struct flag flags
[] = {
2152 { "HI_SOFTIRQ", 0 },
2153 { "TIMER_SOFTIRQ", 1 },
2154 { "NET_TX_SOFTIRQ", 2 },
2155 { "NET_RX_SOFTIRQ", 3 },
2156 { "BLOCK_SOFTIRQ", 4 },
2157 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2158 { "TASKLET_SOFTIRQ", 6 },
2159 { "SCHED_SOFTIRQ", 7 },
2160 { "HRTIMER_SOFTIRQ", 8 },
2161 { "RCU_SOFTIRQ", 9 },
2163 { "HRTIMER_NORESTART", 0 },
2164 { "HRTIMER_RESTART", 1 },
2167 static unsigned long long eval_flag(const char *flag
)
2172 * Some flags in the format files do not get converted.
2173 * If the flag is not numeric, see if it is something that
2174 * we already know about.
2176 if (isdigit(flag
[0]))
2177 return strtoull(flag
, NULL
, 0);
2179 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2180 if (strcmp(flags
[i
].name
, flag
) == 0)
2181 return flags
[i
].value
;
2186 static void print_str_arg(void *data
, int size
,
2187 struct event
*event
, struct print_arg
*arg
)
2189 struct print_flag_sym
*flag
;
2190 unsigned long long val
, fval
;
2194 switch (arg
->type
) {
2199 printf("%s", arg
->atom
.atom
);
2202 if (!arg
->field
.field
) {
2203 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2204 if (!arg
->field
.field
)
2205 die("field %s not found", arg
->field
.name
);
2207 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2208 memcpy(str
, data
+ arg
->field
.field
->offset
,
2209 arg
->field
.field
->size
);
2210 str
[arg
->field
.field
->size
] = 0;
2215 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2217 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2218 fval
= eval_flag(flag
->value
);
2219 if (!val
&& !fval
) {
2220 printf("%s", flag
->str
);
2223 if (fval
&& (val
& fval
) == fval
) {
2224 if (print
&& arg
->flags
.delim
)
2225 printf("%s", arg
->flags
.delim
);
2226 printf("%s", flag
->str
);
2233 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2234 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2235 fval
= eval_flag(flag
->value
);
2237 printf("%s", flag
->str
);
2245 case PRINT_STRING
: {
2248 if (arg
->string
.offset
== -1) {
2249 struct format_field
*f
;
2251 f
= find_any_field(event
, arg
->string
.string
);
2252 arg
->string
.offset
= f
->offset
;
2254 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2255 str_offset
&= 0xffff;
2256 printf("%s", ((char *)data
) + str_offset
);
2261 * The only op for string should be ? :
2263 if (arg
->op
.op
[0] != '?')
2265 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2267 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2269 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2277 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2279 static struct format_field
*field
, *ip_field
;
2280 struct print_arg
*args
, *arg
, **next
;
2281 unsigned long long ip
, val
;
2286 field
= find_field(event
, "buf");
2288 die("can't find buffer field for binary printk");
2289 ip_field
= find_field(event
, "ip");
2291 die("can't find ip field for binary printk");
2294 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2297 * The first arg is the IP pointer.
2299 args
= malloc_or_die(sizeof(*args
));
2304 arg
->type
= PRINT_ATOM
;
2305 arg
->atom
.atom
= malloc_or_die(32);
2306 sprintf(arg
->atom
.atom
, "%lld", ip
);
2308 /* skip the first "%pf : " */
2309 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2310 bptr
< data
+ size
&& *ptr
; ptr
++) {
2334 /* the pointers are always 4 bytes aligned */
2335 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2347 val
= read_size(bptr
, ls
);
2349 arg
= malloc_or_die(sizeof(*arg
));
2351 arg
->type
= PRINT_ATOM
;
2352 arg
->atom
.atom
= malloc_or_die(32);
2353 sprintf(arg
->atom
.atom
, "%lld", val
);
2358 arg
= malloc_or_die(sizeof(*arg
));
2360 arg
->type
= PRINT_STRING
;
2361 arg
->string
.string
= strdup(bptr
);
2362 bptr
+= strlen(bptr
) + 1;
2374 static void free_args(struct print_arg
*args
)
2376 struct print_arg
*next
;
2381 if (args
->type
== PRINT_ATOM
)
2382 free(args
->atom
.atom
);
2384 free(args
->string
.string
);
2390 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2392 unsigned long long addr
;
2393 static struct format_field
*field
;
2394 struct printk_map
*printk
;
2399 field
= find_field(event
, "fmt");
2401 die("can't find format field for binary printk");
2402 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2405 addr
= read_size(data
+ field
->offset
, field
->size
);
2407 printk
= find_printk(addr
);
2409 format
= malloc_or_die(45);
2410 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2416 /* Remove any quotes. */
2419 format
= malloc_or_die(strlen(p
) + 10);
2420 sprintf(format
, "%s : %s", "%pf", p
);
2421 /* remove ending quotes and new line since we will add one too */
2422 p
= format
+ strlen(format
) - 1;
2427 if (strcmp(p
, "\\n") == 0)
2433 static void pretty_print(void *data
, int size
, struct event
*event
)
2435 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2436 struct print_arg
*arg
= print_fmt
->args
;
2437 struct print_arg
*args
= NULL
;
2438 const char *ptr
= print_fmt
->format
;
2439 unsigned long long val
;
2440 struct func_map
*func
;
2441 const char *saveptr
;
2442 char *bprint_fmt
= NULL
;
2448 if (event
->flags
& EVENT_FL_ISFUNC
)
2449 ptr
= " %pF <-- %pF";
2451 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2452 bprint_fmt
= get_bprint_format(data
, size
, event
);
2453 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2458 for (; *ptr
; ptr
++) {
2480 } else if (*ptr
== '%') {
2505 if (*(ptr
+1) == 'F' ||
2518 die("no argument match");
2520 len
= ((unsigned long)ptr
+ 1) -
2521 (unsigned long)saveptr
;
2523 /* should never happen */
2527 memcpy(format
, saveptr
, len
);
2530 val
= eval_num_arg(data
, size
, event
, arg
);
2534 func
= find_func(val
);
2536 printf("%s", func
->func
);
2537 if (show_func
== 'F')
2545 printf(format
, (int)val
);
2548 printf(format
, (long)val
);
2551 printf(format
, (long long)val
);
2554 die("bad count (%d)", ls
);
2559 die("no matching argument");
2561 print_str_arg(data
, size
, event
, arg
);
2565 printf(">%c<", *ptr
);
2578 static inline int log10_cpu(int nb
)
2587 static void print_lat_fmt(void *data
, int size __unused
)
2589 unsigned int lat_flags
;
2595 lat_flags
= parse_common_flags(data
);
2596 pc
= parse_common_pc(data
);
2597 lock_depth
= parse_common_lock_depth(data
);
2599 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2600 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2603 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2604 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2606 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2608 (hardirq
&& softirq
) ? 'H' :
2609 hardirq
? 'h' : softirq
? 's' : '.');
2619 printf("%d", lock_depth
);
2622 /* taken from Linux, written by Frederic Weisbecker */
2623 static void print_graph_cpu(int cpu
)
2626 int log10_this
= log10_cpu(cpu
);
2627 int log10_all
= log10_cpu(cpus
);
2631 * Start with a space character - to make it stand out
2632 * to the right a bit when trace output is pasted into
2638 * Tricky - we space the CPU field according to the max
2639 * number of online CPUs. On a 2-cpu system it would take
2640 * a maximum of 1 digit - on a 128 cpu system it would
2641 * take up to 3 digits:
2643 for (i
= 0; i
< log10_all
- log10_this
; i
++)
2646 printf("%d) ", cpu
);
2649 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2650 #define TRACE_GRAPH_INDENT 2
2652 static void print_graph_proc(int pid
, const char *comm
)
2654 /* sign + log10(MAX_INT) + '\0' */
2660 sprintf(pid_str
, "%d", pid
);
2662 /* 1 stands for the "-" character */
2663 len
= strlen(comm
) + strlen(pid_str
) + 1;
2665 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
2666 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
2668 /* First spaces to align center */
2669 for (i
= 0; i
< spaces
/ 2; i
++)
2672 printf("%s-%s", comm
, pid_str
);
2674 /* Last spaces to align center */
2675 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++)
2679 static struct record
*
2680 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2681 struct record
*next
)
2683 struct format_field
*field
;
2684 struct event
*event
;
2689 type
= trace_parse_common_type(next
->data
);
2690 event
= trace_find_event(type
);
2694 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2697 pid
= parse_common_pid(next
->data
);
2698 field
= find_field(event
, "func");
2700 die("function return does not have field func");
2702 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2704 if (cur_pid
!= pid
|| cur_func
!= val
)
2707 /* this is a leaf, now advance the iterator */
2708 return trace_read_data(cpu
);
2711 /* Signal a overhead of time execution to the output */
2712 static void print_graph_overhead(unsigned long long duration
)
2714 /* Non nested entry or return */
2715 if (duration
== ~0ULL)
2716 return (void)printf(" ");
2718 /* Duration exceeded 100 msecs */
2719 if (duration
> 100000ULL)
2720 return (void)printf("! ");
2722 /* Duration exceeded 10 msecs */
2723 if (duration
> 10000ULL)
2724 return (void)printf("+ ");
2729 static void print_graph_duration(unsigned long long duration
)
2731 unsigned long usecs
= duration
/ 1000;
2732 unsigned long nsecs_rem
= duration
% 1000;
2733 /* log10(ULONG_MAX) + '\0' */
2739 sprintf(msecs_str
, "%lu", usecs
);
2742 len
= printf("%lu", usecs
);
2744 /* Print nsecs (we don't want to exceed 7 numbers) */
2746 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2747 len
+= printf(".%s", nsecs_str
);
2752 /* Print remaining spaces to fit the row's width */
2753 for (i
= len
; i
< 7; i
++)
2760 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2762 unsigned long long rettime
, calltime
;
2763 unsigned long long duration
, depth
;
2764 unsigned long long val
;
2765 struct format_field
*field
;
2766 struct func_map
*func
;
2767 struct event
*ret_event
;
2771 type
= trace_parse_common_type(ret_rec
->data
);
2772 ret_event
= trace_find_event(type
);
2774 field
= find_field(ret_event
, "rettime");
2776 die("can't find rettime in return graph");
2777 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2779 field
= find_field(ret_event
, "calltime");
2781 die("can't find rettime in return graph");
2782 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2784 duration
= rettime
- calltime
;
2787 print_graph_overhead(duration
);
2790 print_graph_duration(duration
);
2792 field
= find_field(event
, "depth");
2794 die("can't find depth in entry graph");
2795 depth
= read_size(data
+ field
->offset
, field
->size
);
2798 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2801 field
= find_field(event
, "func");
2803 die("can't find func in entry graph");
2804 val
= read_size(data
+ field
->offset
, field
->size
);
2805 func
= find_func(val
);
2808 printf("%s();", func
->func
);
2810 printf("%llx();", val
);
2813 static void print_graph_nested(struct event
*event
, void *data
)
2815 struct format_field
*field
;
2816 unsigned long long depth
;
2817 unsigned long long val
;
2818 struct func_map
*func
;
2822 print_graph_overhead(-1);
2827 field
= find_field(event
, "depth");
2829 die("can't find depth in entry graph");
2830 depth
= read_size(data
+ field
->offset
, field
->size
);
2833 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2836 field
= find_field(event
, "func");
2838 die("can't find func in entry graph");
2839 val
= read_size(data
+ field
->offset
, field
->size
);
2840 func
= find_func(val
);
2843 printf("%s() {", func
->func
);
2845 printf("%llx() {", val
);
2849 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2850 int cpu
, int pid
, const char *comm
,
2851 unsigned long secs
, unsigned long usecs
)
2853 struct format_field
*field
;
2858 printf("%5lu.%06lu | ", secs
, usecs
);
2860 print_graph_cpu(cpu
);
2861 print_graph_proc(pid
, comm
);
2865 if (latency_format
) {
2866 print_lat_fmt(data
, size
);
2870 field
= find_field(event
, "func");
2872 die("function entry does not have func field");
2874 val
= read_size(data
+ field
->offset
, field
->size
);
2877 * peek_data may unmap the data pointer. Copy it first.
2879 copy_data
= malloc_or_die(size
);
2880 memcpy(copy_data
, data
, size
);
2883 rec
= trace_peek_data(cpu
);
2885 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2887 print_graph_entry_leaf(event
, data
, rec
);
2891 print_graph_nested(event
, data
);
2897 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
,
2898 int cpu
, int pid
, const char *comm
,
2899 unsigned long secs
, unsigned long usecs
)
2901 unsigned long long rettime
, calltime
;
2902 unsigned long long duration
, depth
;
2903 struct format_field
*field
;
2906 printf("%5lu.%06lu | ", secs
, usecs
);
2908 print_graph_cpu(cpu
);
2909 print_graph_proc(pid
, comm
);
2913 if (latency_format
) {
2914 print_lat_fmt(data
, size
);
2918 field
= find_field(event
, "rettime");
2920 die("can't find rettime in return graph");
2921 rettime
= read_size(data
+ field
->offset
, field
->size
);
2923 field
= find_field(event
, "calltime");
2925 die("can't find calltime in return graph");
2926 calltime
= read_size(data
+ field
->offset
, field
->size
);
2928 duration
= rettime
- calltime
;
2931 print_graph_overhead(duration
);
2934 print_graph_duration(duration
);
2936 field
= find_field(event
, "depth");
2938 die("can't find depth in entry graph");
2939 depth
= read_size(data
+ field
->offset
, field
->size
);
2942 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2949 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2950 int cpu
, int pid
, const char *comm
,
2951 unsigned long secs
, unsigned long usecs
)
2953 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2954 pretty_print_func_ent(data
, size
, event
,
2955 cpu
, pid
, comm
, secs
, usecs
);
2956 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2957 pretty_print_func_ret(data
, size
, event
,
2958 cpu
, pid
, comm
, secs
, usecs
);
2962 void print_event(int cpu
, void *data
, int size
, unsigned long long nsecs
,
2965 struct event
*event
;
2967 unsigned long usecs
;
2971 secs
= nsecs
/ NSECS_PER_SEC
;
2972 nsecs
-= secs
* NSECS_PER_SEC
;
2973 usecs
= nsecs
/ NSECS_PER_USEC
;
2975 type
= trace_parse_common_type(data
);
2977 event
= trace_find_event(type
);
2979 warning("ug! no event found for type %d", type
);
2983 pid
= parse_common_pid(data
);
2985 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2986 return pretty_print_func_graph(data
, size
, event
, cpu
,
2987 pid
, comm
, secs
, usecs
);
2989 if (latency_format
) {
2990 printf("%8.8s-%-5d %3d",
2992 print_lat_fmt(data
, size
);
2994 printf("%16s-%-5d [%03d]", comm
, pid
, cpu
);
2996 printf(" %5lu.%06lu: %s: ", secs
, usecs
, event
->name
);
2998 if (event
->flags
& EVENT_FL_FAILED
) {
2999 printf("EVENT '%s' FAILED TO PARSE\n",
3004 pretty_print(data
, size
, event
);
3008 static void print_fields(struct print_flag_sym
*field
)
3010 printf("{ %s, %s }", field
->value
, field
->str
);
3013 print_fields(field
->next
);
3017 static void print_args(struct print_arg
*args
)
3019 int print_paren
= 1;
3021 switch (args
->type
) {
3026 printf("%s", args
->atom
.atom
);
3029 printf("REC->%s", args
->field
.name
);
3032 printf("__print_flags(");
3033 print_args(args
->flags
.field
);
3034 printf(", %s, ", args
->flags
.delim
);
3035 print_fields(args
->flags
.flags
);
3039 printf("__print_symbolic(");
3040 print_args(args
->symbol
.field
);
3042 print_fields(args
->symbol
.symbols
);
3046 printf("__get_str(%s)", args
->string
.string
);
3049 printf("(%s)", args
->typecast
.type
);
3050 print_args(args
->typecast
.item
);
3053 if (strcmp(args
->op
.op
, ":") == 0)
3057 print_args(args
->op
.left
);
3058 printf(" %s ", args
->op
.op
);
3059 print_args(args
->op
.right
);
3064 /* we should warn... */
3069 print_args(args
->next
);
3073 static void parse_header_field(const char *field
,
3074 int *offset
, int *size
)
3079 if (read_expected(EVENT_ITEM
, "field") < 0)
3081 if (read_expected(EVENT_OP
, ":") < 0)
3085 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3089 if (read_expected(EVENT_ITEM
, field
) < 0)
3091 if (read_expected(EVENT_OP
, ";") < 0)
3093 if (read_expected(EVENT_ITEM
, "offset") < 0)
3095 if (read_expected(EVENT_OP
, ":") < 0)
3097 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3099 *offset
= atoi(token
);
3101 if (read_expected(EVENT_OP
, ";") < 0)
3103 if (read_expected(EVENT_ITEM
, "size") < 0)
3105 if (read_expected(EVENT_OP
, ":") < 0)
3107 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3109 *size
= atoi(token
);
3111 if (read_expected(EVENT_OP
, ";") < 0)
3113 type
= read_token(&token
);
3114 if (type
!= EVENT_NEWLINE
) {
3115 /* newer versions of the kernel have a "signed" type */
3116 if (type
!= EVENT_ITEM
)
3119 if (strcmp(token
, "signed") != 0)
3124 if (read_expected(EVENT_OP
, ":") < 0)
3127 if (read_expect_type(EVENT_ITEM
, &token
))
3131 if (read_expected(EVENT_OP
, ";") < 0)
3134 if (read_expect_type(EVENT_NEWLINE
, &token
))
3141 int parse_header_page(char *buf
, unsigned long size
)
3143 init_input_buf(buf
, size
);
3145 parse_header_field("timestamp", &header_page_ts_offset
,
3146 &header_page_ts_size
);
3147 parse_header_field("commit", &header_page_size_offset
,
3148 &header_page_size_size
);
3149 parse_header_field("data", &header_page_data_offset
,
3150 &header_page_data_size
);
3155 int parse_ftrace_file(char *buf
, unsigned long size
)
3157 struct format_field
*field
;
3158 struct print_arg
*arg
, **list
;
3159 struct event
*event
;
3162 init_input_buf(buf
, size
);
3164 event
= alloc_event();
3168 event
->flags
|= EVENT_FL_ISFTRACE
;
3170 event
->name
= event_read_name();
3172 die("failed to read ftrace event name");
3174 if (strcmp(event
->name
, "function") == 0)
3175 event
->flags
|= EVENT_FL_ISFUNC
;
3177 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3178 event
->flags
|= EVENT_FL_ISFUNCENT
;
3180 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3181 event
->flags
|= EVENT_FL_ISFUNCRET
;
3183 else if (strcmp(event
->name
, "bprint") == 0)
3184 event
->flags
|= EVENT_FL_ISBPRINT
;
3186 event
->id
= event_read_id();
3188 die("failed to read ftrace event id");
3192 ret
= event_read_format(event
);
3194 die("failed to read ftrace event format");
3196 ret
= event_read_print(event
);
3198 die("failed to read ftrace event print fmt");
3200 /* New ftrace handles args */
3204 * The arguments for ftrace files are parsed by the fields.
3205 * Set up the fields as their arguments.
3207 list
= &event
->print_fmt
.args
;
3208 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3209 arg
= malloc_or_die(sizeof(*arg
));
3210 memset(arg
, 0, sizeof(*arg
));
3213 arg
->type
= PRINT_FIELD
;
3214 arg
->field
.name
= field
->name
;
3215 arg
->field
.field
= field
;
3220 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3222 struct event
*event
;
3225 init_input_buf(buf
, size
);
3227 event
= alloc_event();
3231 event
->name
= event_read_name();
3233 die("failed to read event name");
3235 event
->id
= event_read_id();
3237 die("failed to read event id");
3239 ret
= event_read_format(event
);
3241 warning("failed to read event format for %s", event
->name
);
3245 ret
= event_read_print(event
);
3247 warning("failed to read event print fmt for %s", event
->name
);
3251 event
->system
= strdup(sys
);
3253 #define PRINT_ARGS 0
3254 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3255 print_args(event
->print_fmt
.args
);
3261 event
->flags
|= EVENT_FL_FAILED
;
3262 /* still add it even if it failed */
3267 void parse_set_info(int nr_cpus
, int long_sz
)
3270 long_size
= long_sz
;