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 if (strtoul(token
, NULL
, 0))
945 field
->flags
|= FIELD_IS_SIGNED
;
948 if (read_expected(EVENT_OP
, ";") < 0)
951 if (read_expect_type(EVENT_NEWLINE
, &token
))
958 fields
= &field
->next
;
972 static int event_read_format(struct event
*event
)
977 if (read_expected_item(EVENT_ITEM
, "format") < 0)
980 if (read_expected(EVENT_OP
, ":") < 0)
983 if (read_expect_type(EVENT_NEWLINE
, &token
))
987 ret
= event_read_fields(event
, &event
->format
.common_fields
);
990 event
->format
.nr_common
= ret
;
992 ret
= event_read_fields(event
, &event
->format
.fields
);
995 event
->format
.nr_fields
= ret
;
1005 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1006 char **tok
, enum event_type type
);
1008 static enum event_type
1009 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1011 enum event_type type
;
1014 type
= read_token(&token
);
1017 return process_arg_token(event
, arg
, tok
, type
);
1020 static enum event_type
1021 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1023 struct print_arg
*arg
, *left
, *right
;
1024 enum event_type type
;
1027 arg
= malloc_or_die(sizeof(*arg
));
1028 memset(arg
, 0, sizeof(*arg
));
1030 left
= malloc_or_die(sizeof(*left
));
1032 right
= malloc_or_die(sizeof(*right
));
1034 arg
->type
= PRINT_OP
;
1035 arg
->op
.left
= left
;
1036 arg
->op
.right
= right
;
1039 type
= process_arg(event
, left
, &token
);
1040 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1045 type
= process_arg(event
, right
, &token
);
1047 top
->op
.right
= arg
;
1060 static enum event_type
1061 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1063 struct print_arg
*arg
;
1064 enum event_type type
;
1067 arg
= malloc_or_die(sizeof(*arg
));
1068 memset(arg
, 0, sizeof(*arg
));
1071 type
= process_arg(event
, arg
, &token
);
1072 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1075 top
->op
.right
= arg
;
1078 type
= read_token_item(&token
);
1089 static int get_op_prio(char *op
)
1100 /* '>>' and '<<' are 8 */
1104 /* '==' and '!=' are 10 */
1114 die("unknown op '%c'", op
[0]);
1118 if (strcmp(op
, "++") == 0 ||
1119 strcmp(op
, "--") == 0) {
1121 } else if (strcmp(op
, ">>") == 0 ||
1122 strcmp(op
, "<<") == 0) {
1124 } else if (strcmp(op
, ">=") == 0 ||
1125 strcmp(op
, "<=") == 0) {
1127 } else if (strcmp(op
, "==") == 0 ||
1128 strcmp(op
, "!=") == 0) {
1130 } else if (strcmp(op
, "&&") == 0) {
1132 } else if (strcmp(op
, "||") == 0) {
1135 die("unknown op '%s'", op
);
1141 static void set_op_prio(struct print_arg
*arg
)
1144 /* single ops are the greatest */
1145 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1150 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1153 static enum event_type
1154 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1156 struct print_arg
*left
, *right
= NULL
;
1157 enum event_type type
;
1160 /* the op is passed in via tok */
1163 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1164 /* handle single op */
1166 die("bad op token %s", token
);
1175 die("bad op token %s", token
);
1179 /* make an empty left */
1180 left
= malloc_or_die(sizeof(*left
));
1181 left
->type
= PRINT_NULL
;
1182 arg
->op
.left
= left
;
1184 right
= malloc_or_die(sizeof(*right
));
1185 arg
->op
.right
= right
;
1187 type
= process_arg(event
, right
, tok
);
1189 } else if (strcmp(token
, "?") == 0) {
1191 left
= malloc_or_die(sizeof(*left
));
1192 /* copy the top arg to the left */
1195 arg
->type
= PRINT_OP
;
1197 arg
->op
.left
= left
;
1200 type
= process_cond(event
, arg
, tok
);
1202 } else if (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 ||
1216 strcmp(token
, "!=") == 0) {
1218 left
= malloc_or_die(sizeof(*left
));
1220 /* copy the top arg to the left */
1223 arg
->type
= PRINT_OP
;
1225 arg
->op
.left
= left
;
1229 right
= malloc_or_die(sizeof(*right
));
1231 type
= read_token_item(&token
);
1234 /* could just be a type pointer */
1235 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1236 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1237 if (left
->type
!= PRINT_ATOM
)
1238 die("bad pointer type");
1239 left
->atom
.atom
= realloc(left
->atom
.atom
,
1240 sizeof(left
->atom
.atom
) + 3);
1241 strcat(left
->atom
.atom
, " *");
1248 type
= process_arg_token(event
, right
, tok
, type
);
1250 arg
->op
.right
= right
;
1252 } else if (strcmp(token
, "[") == 0) {
1254 left
= malloc_or_die(sizeof(*left
));
1257 arg
->type
= PRINT_OP
;
1259 arg
->op
.left
= left
;
1262 type
= process_array(event
, arg
, tok
);
1265 warning("unknown op '%s'", token
);
1266 event
->flags
|= EVENT_FL_FAILED
;
1267 /* the arg is now the left side */
1271 if (type
== EVENT_OP
) {
1274 /* higher prios need to be closer to the root */
1275 prio
= get_op_prio(*tok
);
1277 if (prio
> arg
->op
.prio
)
1278 return process_op(event
, arg
, tok
);
1280 return process_op(event
, right
, tok
);
1286 static enum event_type
1287 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1290 enum event_type type
;
1294 if (read_expected(EVENT_OP
, "->") < 0)
1297 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1301 arg
->type
= PRINT_FIELD
;
1302 arg
->field
.name
= field
;
1304 type
= read_token(&token
);
1314 static char *arg_eval (struct print_arg
*arg
);
1316 static long long arg_num_eval(struct print_arg
*arg
)
1318 long long left
, right
;
1321 switch (arg
->type
) {
1323 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1326 val
= arg_num_eval(arg
->typecast
.item
);
1329 switch (arg
->op
.op
[0]) {
1331 left
= arg_num_eval(arg
->op
.left
);
1332 right
= arg_num_eval(arg
->op
.right
);
1334 val
= left
|| right
;
1339 left
= arg_num_eval(arg
->op
.left
);
1340 right
= arg_num_eval(arg
->op
.right
);
1342 val
= left
&& right
;
1347 left
= arg_num_eval(arg
->op
.left
);
1348 right
= arg_num_eval(arg
->op
.right
);
1349 switch (arg
->op
.op
[1]) {
1354 val
= left
<< right
;
1357 val
= left
<= right
;
1360 die("unknown op '%s'", arg
->op
.op
);
1364 left
= arg_num_eval(arg
->op
.left
);
1365 right
= arg_num_eval(arg
->op
.right
);
1366 switch (arg
->op
.op
[1]) {
1371 val
= left
>> right
;
1374 val
= left
>= right
;
1377 die("unknown op '%s'", arg
->op
.op
);
1381 left
= arg_num_eval(arg
->op
.left
);
1382 right
= arg_num_eval(arg
->op
.right
);
1384 if (arg
->op
.op
[1] != '=')
1385 die("unknown op '%s'", arg
->op
.op
);
1387 val
= left
== right
;
1390 left
= arg_num_eval(arg
->op
.left
);
1391 right
= arg_num_eval(arg
->op
.right
);
1393 switch (arg
->op
.op
[1]) {
1395 val
= left
!= right
;
1398 die("unknown op '%s'", arg
->op
.op
);
1402 die("unknown op '%s'", arg
->op
.op
);
1407 case PRINT_FIELD
... PRINT_SYMBOL
:
1410 die("invalid eval type %d", arg
->type
);
1416 static char *arg_eval (struct print_arg
*arg
)
1419 static char buf
[20];
1421 switch (arg
->type
) {
1423 return arg
->atom
.atom
;
1425 return arg_eval(arg
->typecast
.item
);
1427 val
= arg_num_eval(arg
);
1428 sprintf(buf
, "%lld", val
);
1432 case PRINT_FIELD
... PRINT_SYMBOL
:
1435 die("invalid eval type %d", arg
->type
);
1442 static enum event_type
1443 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1445 enum event_type type
;
1446 struct print_arg
*arg
= NULL
;
1447 struct print_flag_sym
*field
;
1453 type
= read_token_item(&token
);
1454 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1457 arg
= malloc_or_die(sizeof(*arg
));
1460 type
= process_arg(event
, arg
, &token
);
1461 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1464 field
= malloc_or_die(sizeof(*field
));
1465 memset(field
, 0, sizeof(field
));
1467 value
= arg_eval(arg
);
1468 field
->value
= strdup(value
);
1471 type
= process_arg(event
, arg
, &token
);
1472 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1475 value
= arg_eval(arg
);
1476 field
->str
= strdup(value
);
1481 list
= &field
->next
;
1484 type
= read_token_item(&token
);
1485 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1497 static enum event_type
1498 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1500 struct print_arg
*field
;
1501 enum event_type type
;
1504 memset(arg
, 0, sizeof(*arg
));
1505 arg
->type
= PRINT_FLAGS
;
1507 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1510 field
= malloc_or_die(sizeof(*field
));
1512 type
= process_arg(event
, field
, &token
);
1513 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1516 arg
->flags
.field
= field
;
1518 type
= read_token_item(&token
);
1519 if (event_item_type(type
)) {
1520 arg
->flags
.delim
= token
;
1521 type
= read_token_item(&token
);
1524 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1527 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1528 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1532 type
= read_token_item(tok
);
1540 static enum event_type
1541 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1543 struct print_arg
*field
;
1544 enum event_type type
;
1547 memset(arg
, 0, sizeof(*arg
));
1548 arg
->type
= PRINT_SYMBOL
;
1550 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1553 field
= malloc_or_die(sizeof(*field
));
1555 type
= process_arg(event
, field
, &token
);
1556 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1559 arg
->symbol
.field
= field
;
1561 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1562 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1566 type
= read_token_item(tok
);
1574 static enum event_type
1575 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1577 struct print_arg
*item_arg
;
1578 enum event_type type
;
1581 type
= process_arg(event
, arg
, &token
);
1583 if (type
== EVENT_ERROR
)
1586 if (type
== EVENT_OP
)
1587 type
= process_op(event
, arg
, &token
);
1589 if (type
== EVENT_ERROR
)
1592 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1598 type
= read_token_item(&token
);
1601 * If the next token is an item or another open paren, then
1602 * this was a typecast.
1604 if (event_item_type(type
) ||
1605 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1607 /* make this a typecast and contine */
1609 /* prevous must be an atom */
1610 if (arg
->type
!= PRINT_ATOM
)
1611 die("previous needed to be PRINT_ATOM");
1613 item_arg
= malloc_or_die(sizeof(*item_arg
));
1615 arg
->type
= PRINT_TYPE
;
1616 arg
->typecast
.type
= arg
->atom
.atom
;
1617 arg
->typecast
.item
= item_arg
;
1618 type
= process_arg_token(event
, item_arg
, &token
, type
);
1627 static enum event_type
1628 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1630 enum event_type type
;
1633 if (read_expected(EVENT_DELIM
, "(") < 0)
1636 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1639 arg
->type
= PRINT_STRING
;
1640 arg
->string
.string
= token
;
1641 arg
->string
.offset
= -1;
1643 if (read_expected(EVENT_DELIM
, ")") < 0)
1646 type
= read_token(&token
);
1656 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1657 char **tok
, enum event_type type
)
1666 if (strcmp(token
, "REC") == 0) {
1668 type
= process_entry(event
, arg
, &token
);
1669 } else if (strcmp(token
, "__print_flags") == 0) {
1671 type
= process_flags(event
, arg
, &token
);
1672 } else if (strcmp(token
, "__print_symbolic") == 0) {
1674 type
= process_symbols(event
, arg
, &token
);
1675 } else if (strcmp(token
, "__get_str") == 0) {
1677 type
= process_str(event
, arg
, &token
);
1680 /* test the next token */
1681 type
= read_token_item(&token
);
1683 /* atoms can be more than one token long */
1684 while (type
== EVENT_ITEM
) {
1685 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1687 strcat(atom
, token
);
1689 type
= read_token_item(&token
);
1692 /* todo, test for function */
1694 arg
->type
= PRINT_ATOM
;
1695 arg
->atom
.atom
= atom
;
1700 arg
->type
= PRINT_ATOM
;
1701 arg
->atom
.atom
= token
;
1702 type
= read_token_item(&token
);
1705 if (strcmp(token
, "(") == 0) {
1707 type
= process_paren(event
, arg
, &token
);
1711 /* handle single ops */
1712 arg
->type
= PRINT_OP
;
1714 arg
->op
.left
= NULL
;
1715 type
= process_op(event
, arg
, &token
);
1719 case EVENT_ERROR
... EVENT_NEWLINE
:
1721 die("unexpected type %d", type
);
1728 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1730 enum event_type type
= EVENT_ERROR
;
1731 struct print_arg
*arg
;
1736 if (type
== EVENT_NEWLINE
) {
1738 type
= read_token_item(&token
);
1742 arg
= malloc_or_die(sizeof(*arg
));
1743 memset(arg
, 0, sizeof(*arg
));
1745 type
= process_arg(event
, arg
, &token
);
1747 if (type
== EVENT_ERROR
) {
1755 if (type
== EVENT_OP
) {
1756 type
= process_op(event
, arg
, &token
);
1761 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1768 } while (type
!= EVENT_NONE
);
1770 if (type
!= EVENT_NONE
)
1776 static int event_read_print(struct event
*event
)
1778 enum event_type type
;
1782 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1785 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1788 if (read_expected(EVENT_OP
, ":") < 0)
1791 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1795 event
->print_fmt
.format
= token
;
1796 event
->print_fmt
.args
= NULL
;
1798 /* ok to have no arg */
1799 type
= read_token_item(&token
);
1801 if (type
== EVENT_NONE
)
1804 /* Handle concatination of print lines */
1805 if (type
== EVENT_DQUOTE
) {
1808 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1810 strcpy(cat
, event
->print_fmt
.format
);
1813 free_token(event
->print_fmt
.format
);
1814 event
->print_fmt
.format
= NULL
;
1819 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1824 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1835 static struct format_field
*
1836 find_common_field(struct event
*event
, const char *name
)
1838 struct format_field
*format
;
1840 for (format
= event
->format
.common_fields
;
1841 format
; format
= format
->next
) {
1842 if (strcmp(format
->name
, name
) == 0)
1849 static struct format_field
*
1850 find_field(struct event
*event
, const char *name
)
1852 struct format_field
*format
;
1854 for (format
= event
->format
.fields
;
1855 format
; format
= format
->next
) {
1856 if (strcmp(format
->name
, name
) == 0)
1863 static struct format_field
*
1864 find_any_field(struct event
*event
, const char *name
)
1866 struct format_field
*format
;
1868 format
= find_common_field(event
, name
);
1871 return find_field(event
, name
);
1874 static unsigned long long read_size(void *ptr
, int size
)
1878 return *(unsigned char *)ptr
;
1880 return data2host2(ptr
);
1882 return data2host4(ptr
);
1884 return data2host8(ptr
);
1892 raw_field_value(struct event
*event
, const char *name
, void *data
)
1894 struct format_field
*field
;
1896 field
= find_any_field(event
, name
);
1900 return read_size(data
+ field
->offset
, field
->size
);
1903 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1905 struct format_field
*field
;
1907 field
= find_any_field(event
, name
);
1911 return data
+ field
->offset
;
1914 static int get_common_info(const char *type
, int *offset
, int *size
)
1916 struct event
*event
;
1917 struct format_field
*field
;
1920 * All events should have the same common elements.
1921 * Pick any event to find where the type is;
1924 die("no event_list!");
1927 field
= find_common_field(event
, type
);
1929 die("field '%s' not found", type
);
1931 *offset
= field
->offset
;
1932 *size
= field
->size
;
1937 static int __parse_common(void *data
, int *size
, int *offset
,
1943 ret
= get_common_info(name
, offset
, size
);
1947 return read_size(data
+ *offset
, *size
);
1950 int trace_parse_common_type(void *data
)
1952 static int type_offset
;
1953 static int type_size
;
1955 return __parse_common(data
, &type_size
, &type_offset
,
1959 static int parse_common_pid(void *data
)
1961 static int pid_offset
;
1962 static int pid_size
;
1964 return __parse_common(data
, &pid_size
, &pid_offset
,
1968 static int parse_common_pc(void *data
)
1970 static int pc_offset
;
1973 return __parse_common(data
, &pc_size
, &pc_offset
,
1974 "common_preempt_count");
1977 static int parse_common_flags(void *data
)
1979 static int flags_offset
;
1980 static int flags_size
;
1982 return __parse_common(data
, &flags_size
, &flags_offset
,
1986 static int parse_common_lock_depth(void *data
)
1988 static int ld_offset
;
1992 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
1993 "common_lock_depth");
2000 struct event
*trace_find_event(int id
)
2002 struct event
*event
;
2004 for (event
= event_list
; event
; event
= event
->next
) {
2005 if (event
->id
== id
)
2011 static unsigned long long eval_num_arg(void *data
, int size
,
2012 struct event
*event
, struct print_arg
*arg
)
2014 unsigned long long val
= 0;
2015 unsigned long long left
, right
;
2016 struct print_arg
*larg
;
2018 switch (arg
->type
) {
2023 return strtoull(arg
->atom
.atom
, NULL
, 0);
2025 if (!arg
->field
.field
) {
2026 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2027 if (!arg
->field
.field
)
2028 die("field %s not found", arg
->field
.name
);
2030 /* must be a number */
2031 val
= read_size(data
+ arg
->field
.field
->offset
,
2032 arg
->field
.field
->size
);
2038 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2043 if (strcmp(arg
->op
.op
, "[") == 0) {
2045 * Arrays are special, since we don't want
2046 * to read the arg as is.
2048 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2049 goto default_op
; /* oops, all bets off */
2050 larg
= arg
->op
.left
;
2051 if (!larg
->field
.field
) {
2053 find_any_field(event
, larg
->field
.name
);
2054 if (!larg
->field
.field
)
2055 die("field %s not found", larg
->field
.name
);
2057 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2058 val
= read_size(data
+ larg
->field
.field
->offset
+
2059 right
* long_size
, long_size
);
2063 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2064 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2065 switch (arg
->op
.op
[0]) {
2068 val
= left
|| right
;
2074 val
= left
&& right
;
2079 switch (arg
->op
.op
[1]) {
2084 val
= left
<< right
;
2087 val
= left
<= right
;
2090 die("unknown op '%s'", arg
->op
.op
);
2094 switch (arg
->op
.op
[1]) {
2099 val
= left
>> right
;
2102 val
= left
>= right
;
2105 die("unknown op '%s'", arg
->op
.op
);
2109 if (arg
->op
.op
[1] != '=')
2110 die("unknown op '%s'", arg
->op
.op
);
2111 val
= left
== right
;
2120 die("unknown op '%s'", arg
->op
.op
);
2123 default: /* not sure what to do there */
2131 unsigned long long value
;
2134 static const struct flag flags
[] = {
2135 { "HI_SOFTIRQ", 0 },
2136 { "TIMER_SOFTIRQ", 1 },
2137 { "NET_TX_SOFTIRQ", 2 },
2138 { "NET_RX_SOFTIRQ", 3 },
2139 { "BLOCK_SOFTIRQ", 4 },
2140 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2141 { "TASKLET_SOFTIRQ", 6 },
2142 { "SCHED_SOFTIRQ", 7 },
2143 { "HRTIMER_SOFTIRQ", 8 },
2144 { "RCU_SOFTIRQ", 9 },
2146 { "HRTIMER_NORESTART", 0 },
2147 { "HRTIMER_RESTART", 1 },
2150 static unsigned long long eval_flag(const char *flag
)
2155 * Some flags in the format files do not get converted.
2156 * If the flag is not numeric, see if it is something that
2157 * we already know about.
2159 if (isdigit(flag
[0]))
2160 return strtoull(flag
, NULL
, 0);
2162 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2163 if (strcmp(flags
[i
].name
, flag
) == 0)
2164 return flags
[i
].value
;
2169 static void print_str_arg(void *data
, int size
,
2170 struct event
*event
, struct print_arg
*arg
)
2172 struct print_flag_sym
*flag
;
2173 unsigned long long val
, fval
;
2177 switch (arg
->type
) {
2182 printf("%s", arg
->atom
.atom
);
2185 if (!arg
->field
.field
) {
2186 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2187 if (!arg
->field
.field
)
2188 die("field %s not found", arg
->field
.name
);
2190 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2191 memcpy(str
, data
+ arg
->field
.field
->offset
,
2192 arg
->field
.field
->size
);
2193 str
[arg
->field
.field
->size
] = 0;
2198 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2200 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2201 fval
= eval_flag(flag
->value
);
2202 if (!val
&& !fval
) {
2203 printf("%s", flag
->str
);
2206 if (fval
&& (val
& fval
) == fval
) {
2207 if (print
&& arg
->flags
.delim
)
2208 printf("%s", arg
->flags
.delim
);
2209 printf("%s", flag
->str
);
2216 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2217 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2218 fval
= eval_flag(flag
->value
);
2220 printf("%s", flag
->str
);
2228 case PRINT_STRING
: {
2231 if (arg
->string
.offset
== -1) {
2232 struct format_field
*f
;
2234 f
= find_any_field(event
, arg
->string
.string
);
2235 arg
->string
.offset
= f
->offset
;
2237 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2238 str_offset
&= 0xffff;
2239 printf("%s", ((char *)data
) + str_offset
);
2244 * The only op for string should be ? :
2246 if (arg
->op
.op
[0] != '?')
2248 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2250 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2252 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2260 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2262 static struct format_field
*field
, *ip_field
;
2263 struct print_arg
*args
, *arg
, **next
;
2264 unsigned long long ip
, val
;
2269 field
= find_field(event
, "buf");
2271 die("can't find buffer field for binary printk");
2272 ip_field
= find_field(event
, "ip");
2274 die("can't find ip field for binary printk");
2277 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2280 * The first arg is the IP pointer.
2282 args
= malloc_or_die(sizeof(*args
));
2287 arg
->type
= PRINT_ATOM
;
2288 arg
->atom
.atom
= malloc_or_die(32);
2289 sprintf(arg
->atom
.atom
, "%lld", ip
);
2291 /* skip the first "%pf : " */
2292 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2293 bptr
< data
+ size
&& *ptr
; ptr
++) {
2317 /* the pointers are always 4 bytes aligned */
2318 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2330 val
= read_size(bptr
, ls
);
2332 arg
= malloc_or_die(sizeof(*arg
));
2334 arg
->type
= PRINT_ATOM
;
2335 arg
->atom
.atom
= malloc_or_die(32);
2336 sprintf(arg
->atom
.atom
, "%lld", val
);
2341 arg
= malloc_or_die(sizeof(*arg
));
2343 arg
->type
= PRINT_STRING
;
2344 arg
->string
.string
= strdup(bptr
);
2345 bptr
+= strlen(bptr
) + 1;
2357 static void free_args(struct print_arg
*args
)
2359 struct print_arg
*next
;
2364 if (args
->type
== PRINT_ATOM
)
2365 free(args
->atom
.atom
);
2367 free(args
->string
.string
);
2373 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2375 unsigned long long addr
;
2376 static struct format_field
*field
;
2377 struct printk_map
*printk
;
2382 field
= find_field(event
, "fmt");
2384 die("can't find format field for binary printk");
2385 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2388 addr
= read_size(data
+ field
->offset
, field
->size
);
2390 printk
= find_printk(addr
);
2392 format
= malloc_or_die(45);
2393 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2399 /* Remove any quotes. */
2402 format
= malloc_or_die(strlen(p
) + 10);
2403 sprintf(format
, "%s : %s", "%pf", p
);
2404 /* remove ending quotes and new line since we will add one too */
2405 p
= format
+ strlen(format
) - 1;
2410 if (strcmp(p
, "\\n") == 0)
2416 static void pretty_print(void *data
, int size
, struct event
*event
)
2418 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2419 struct print_arg
*arg
= print_fmt
->args
;
2420 struct print_arg
*args
= NULL
;
2421 const char *ptr
= print_fmt
->format
;
2422 unsigned long long val
;
2423 struct func_map
*func
;
2424 const char *saveptr
;
2425 char *bprint_fmt
= NULL
;
2431 if (event
->flags
& EVENT_FL_ISFUNC
)
2432 ptr
= " %pF <-- %pF";
2434 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2435 bprint_fmt
= get_bprint_format(data
, size
, event
);
2436 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2441 for (; *ptr
; ptr
++) {
2463 } else if (*ptr
== '%') {
2488 if (*(ptr
+1) == 'F' ||
2501 die("no argument match");
2503 len
= ((unsigned long)ptr
+ 1) -
2504 (unsigned long)saveptr
;
2506 /* should never happen */
2510 memcpy(format
, saveptr
, len
);
2513 val
= eval_num_arg(data
, size
, event
, arg
);
2517 func
= find_func(val
);
2519 printf("%s", func
->func
);
2520 if (show_func
== 'F')
2528 printf(format
, (int)val
);
2531 printf(format
, (long)val
);
2534 printf(format
, (long long)val
);
2537 die("bad count (%d)", ls
);
2542 die("no matching argument");
2544 print_str_arg(data
, size
, event
, arg
);
2548 printf(">%c<", *ptr
);
2561 static inline int log10_cpu(int nb
)
2570 static void print_lat_fmt(void *data
, int size __unused
)
2572 unsigned int lat_flags
;
2578 lat_flags
= parse_common_flags(data
);
2579 pc
= parse_common_pc(data
);
2580 lock_depth
= parse_common_lock_depth(data
);
2582 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2583 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2586 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2587 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2589 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2591 (hardirq
&& softirq
) ? 'H' :
2592 hardirq
? 'h' : softirq
? 's' : '.');
2602 printf("%d", lock_depth
);
2605 /* taken from Linux, written by Frederic Weisbecker */
2606 static void print_graph_cpu(int cpu
)
2609 int log10_this
= log10_cpu(cpu
);
2610 int log10_all
= log10_cpu(cpus
);
2614 * Start with a space character - to make it stand out
2615 * to the right a bit when trace output is pasted into
2621 * Tricky - we space the CPU field according to the max
2622 * number of online CPUs. On a 2-cpu system it would take
2623 * a maximum of 1 digit - on a 128 cpu system it would
2624 * take up to 3 digits:
2626 for (i
= 0; i
< log10_all
- log10_this
; i
++)
2629 printf("%d) ", cpu
);
2632 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2633 #define TRACE_GRAPH_INDENT 2
2635 static void print_graph_proc(int pid
, const char *comm
)
2637 /* sign + log10(MAX_INT) + '\0' */
2643 sprintf(pid_str
, "%d", pid
);
2645 /* 1 stands for the "-" character */
2646 len
= strlen(comm
) + strlen(pid_str
) + 1;
2648 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
2649 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
2651 /* First spaces to align center */
2652 for (i
= 0; i
< spaces
/ 2; i
++)
2655 printf("%s-%s", comm
, pid_str
);
2657 /* Last spaces to align center */
2658 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++)
2662 static struct record
*
2663 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2664 struct record
*next
)
2666 struct format_field
*field
;
2667 struct event
*event
;
2672 type
= trace_parse_common_type(next
->data
);
2673 event
= trace_find_event(type
);
2677 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2680 pid
= parse_common_pid(next
->data
);
2681 field
= find_field(event
, "func");
2683 die("function return does not have field func");
2685 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2687 if (cur_pid
!= pid
|| cur_func
!= val
)
2690 /* this is a leaf, now advance the iterator */
2691 return trace_read_data(cpu
);
2694 /* Signal a overhead of time execution to the output */
2695 static void print_graph_overhead(unsigned long long duration
)
2697 /* Non nested entry or return */
2698 if (duration
== ~0ULL)
2699 return (void)printf(" ");
2701 /* Duration exceeded 100 msecs */
2702 if (duration
> 100000ULL)
2703 return (void)printf("! ");
2705 /* Duration exceeded 10 msecs */
2706 if (duration
> 10000ULL)
2707 return (void)printf("+ ");
2712 static void print_graph_duration(unsigned long long duration
)
2714 unsigned long usecs
= duration
/ 1000;
2715 unsigned long nsecs_rem
= duration
% 1000;
2716 /* log10(ULONG_MAX) + '\0' */
2722 sprintf(msecs_str
, "%lu", usecs
);
2725 len
= printf("%lu", usecs
);
2727 /* Print nsecs (we don't want to exceed 7 numbers) */
2729 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2730 len
+= printf(".%s", nsecs_str
);
2735 /* Print remaining spaces to fit the row's width */
2736 for (i
= len
; i
< 7; i
++)
2743 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2745 unsigned long long rettime
, calltime
;
2746 unsigned long long duration
, depth
;
2747 unsigned long long val
;
2748 struct format_field
*field
;
2749 struct func_map
*func
;
2750 struct event
*ret_event
;
2754 type
= trace_parse_common_type(ret_rec
->data
);
2755 ret_event
= trace_find_event(type
);
2757 field
= find_field(ret_event
, "rettime");
2759 die("can't find rettime in return graph");
2760 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2762 field
= find_field(ret_event
, "calltime");
2764 die("can't find rettime in return graph");
2765 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2767 duration
= rettime
- calltime
;
2770 print_graph_overhead(duration
);
2773 print_graph_duration(duration
);
2775 field
= find_field(event
, "depth");
2777 die("can't find depth in entry graph");
2778 depth
= read_size(data
+ field
->offset
, field
->size
);
2781 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2784 field
= find_field(event
, "func");
2786 die("can't find func in entry graph");
2787 val
= read_size(data
+ field
->offset
, field
->size
);
2788 func
= find_func(val
);
2791 printf("%s();", func
->func
);
2793 printf("%llx();", val
);
2796 static void print_graph_nested(struct event
*event
, void *data
)
2798 struct format_field
*field
;
2799 unsigned long long depth
;
2800 unsigned long long val
;
2801 struct func_map
*func
;
2805 print_graph_overhead(-1);
2810 field
= find_field(event
, "depth");
2812 die("can't find depth in entry graph");
2813 depth
= read_size(data
+ field
->offset
, field
->size
);
2816 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2819 field
= find_field(event
, "func");
2821 die("can't find func in entry graph");
2822 val
= read_size(data
+ field
->offset
, field
->size
);
2823 func
= find_func(val
);
2826 printf("%s() {", func
->func
);
2828 printf("%llx() {", val
);
2832 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2833 int cpu
, int pid
, const char *comm
,
2834 unsigned long secs
, unsigned long usecs
)
2836 struct format_field
*field
;
2841 printf("%5lu.%06lu | ", secs
, usecs
);
2843 print_graph_cpu(cpu
);
2844 print_graph_proc(pid
, comm
);
2848 if (latency_format
) {
2849 print_lat_fmt(data
, size
);
2853 field
= find_field(event
, "func");
2855 die("function entry does not have func field");
2857 val
= read_size(data
+ field
->offset
, field
->size
);
2860 * peek_data may unmap the data pointer. Copy it first.
2862 copy_data
= malloc_or_die(size
);
2863 memcpy(copy_data
, data
, size
);
2866 rec
= trace_peek_data(cpu
);
2868 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2870 print_graph_entry_leaf(event
, data
, rec
);
2874 print_graph_nested(event
, data
);
2880 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
,
2881 int cpu
, int pid
, const char *comm
,
2882 unsigned long secs
, unsigned long usecs
)
2884 unsigned long long rettime
, calltime
;
2885 unsigned long long duration
, depth
;
2886 struct format_field
*field
;
2889 printf("%5lu.%06lu | ", secs
, usecs
);
2891 print_graph_cpu(cpu
);
2892 print_graph_proc(pid
, comm
);
2896 if (latency_format
) {
2897 print_lat_fmt(data
, size
);
2901 field
= find_field(event
, "rettime");
2903 die("can't find rettime in return graph");
2904 rettime
= read_size(data
+ field
->offset
, field
->size
);
2906 field
= find_field(event
, "calltime");
2908 die("can't find calltime in return graph");
2909 calltime
= read_size(data
+ field
->offset
, field
->size
);
2911 duration
= rettime
- calltime
;
2914 print_graph_overhead(duration
);
2917 print_graph_duration(duration
);
2919 field
= find_field(event
, "depth");
2921 die("can't find depth in entry graph");
2922 depth
= read_size(data
+ field
->offset
, field
->size
);
2925 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2932 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2933 int cpu
, int pid
, const char *comm
,
2934 unsigned long secs
, unsigned long usecs
)
2936 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2937 pretty_print_func_ent(data
, size
, event
,
2938 cpu
, pid
, comm
, secs
, usecs
);
2939 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2940 pretty_print_func_ret(data
, size
, event
,
2941 cpu
, pid
, comm
, secs
, usecs
);
2945 void print_event(int cpu
, void *data
, int size
, unsigned long long nsecs
,
2948 struct event
*event
;
2950 unsigned long usecs
;
2954 secs
= nsecs
/ NSECS_PER_SEC
;
2955 nsecs
-= secs
* NSECS_PER_SEC
;
2956 usecs
= nsecs
/ NSECS_PER_USEC
;
2958 type
= trace_parse_common_type(data
);
2960 event
= trace_find_event(type
);
2962 warning("ug! no event found for type %d", type
);
2966 pid
= parse_common_pid(data
);
2968 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2969 return pretty_print_func_graph(data
, size
, event
, cpu
,
2970 pid
, comm
, secs
, usecs
);
2972 if (latency_format
) {
2973 printf("%8.8s-%-5d %3d",
2975 print_lat_fmt(data
, size
);
2977 printf("%16s-%-5d [%03d]", comm
, pid
, cpu
);
2979 printf(" %5lu.%06lu: %s: ", secs
, usecs
, event
->name
);
2981 if (event
->flags
& EVENT_FL_FAILED
) {
2982 printf("EVENT '%s' FAILED TO PARSE\n",
2987 pretty_print(data
, size
, event
);
2991 static void print_fields(struct print_flag_sym
*field
)
2993 printf("{ %s, %s }", field
->value
, field
->str
);
2996 print_fields(field
->next
);
3000 static void print_args(struct print_arg
*args
)
3002 int print_paren
= 1;
3004 switch (args
->type
) {
3009 printf("%s", args
->atom
.atom
);
3012 printf("REC->%s", args
->field
.name
);
3015 printf("__print_flags(");
3016 print_args(args
->flags
.field
);
3017 printf(", %s, ", args
->flags
.delim
);
3018 print_fields(args
->flags
.flags
);
3022 printf("__print_symbolic(");
3023 print_args(args
->symbol
.field
);
3025 print_fields(args
->symbol
.symbols
);
3029 printf("__get_str(%s)", args
->string
.string
);
3032 printf("(%s)", args
->typecast
.type
);
3033 print_args(args
->typecast
.item
);
3036 if (strcmp(args
->op
.op
, ":") == 0)
3040 print_args(args
->op
.left
);
3041 printf(" %s ", args
->op
.op
);
3042 print_args(args
->op
.right
);
3047 /* we should warn... */
3052 print_args(args
->next
);
3056 static void parse_header_field(const char *field
,
3057 int *offset
, int *size
)
3062 if (read_expected(EVENT_ITEM
, "field") < 0)
3064 if (read_expected(EVENT_OP
, ":") < 0)
3068 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3072 if (read_expected(EVENT_ITEM
, field
) < 0)
3074 if (read_expected(EVENT_OP
, ";") < 0)
3076 if (read_expected(EVENT_ITEM
, "offset") < 0)
3078 if (read_expected(EVENT_OP
, ":") < 0)
3080 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3082 *offset
= atoi(token
);
3084 if (read_expected(EVENT_OP
, ";") < 0)
3086 if (read_expected(EVENT_ITEM
, "size") < 0)
3088 if (read_expected(EVENT_OP
, ":") < 0)
3090 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
3092 *size
= atoi(token
);
3094 if (read_expected(EVENT_OP
, ";") < 0)
3096 type
= read_token(&token
);
3097 if (type
!= EVENT_NEWLINE
) {
3098 /* newer versions of the kernel have a "signed" type */
3099 if (type
!= EVENT_ITEM
)
3102 if (strcmp(token
, "signed") != 0)
3107 if (read_expected(EVENT_OP
, ":") < 0)
3110 if (read_expect_type(EVENT_ITEM
, &token
))
3114 if (read_expected(EVENT_OP
, ";") < 0)
3117 if (read_expect_type(EVENT_NEWLINE
, &token
))
3124 int parse_header_page(char *buf
, unsigned long size
)
3126 init_input_buf(buf
, size
);
3128 parse_header_field("timestamp", &header_page_ts_offset
,
3129 &header_page_ts_size
);
3130 parse_header_field("commit", &header_page_size_offset
,
3131 &header_page_size_size
);
3132 parse_header_field("data", &header_page_data_offset
,
3133 &header_page_data_size
);
3138 int parse_ftrace_file(char *buf
, unsigned long size
)
3140 struct format_field
*field
;
3141 struct print_arg
*arg
, **list
;
3142 struct event
*event
;
3145 init_input_buf(buf
, size
);
3147 event
= alloc_event();
3151 event
->flags
|= EVENT_FL_ISFTRACE
;
3153 event
->name
= event_read_name();
3155 die("failed to read ftrace event name");
3157 if (strcmp(event
->name
, "function") == 0)
3158 event
->flags
|= EVENT_FL_ISFUNC
;
3160 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3161 event
->flags
|= EVENT_FL_ISFUNCENT
;
3163 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3164 event
->flags
|= EVENT_FL_ISFUNCRET
;
3166 else if (strcmp(event
->name
, "bprint") == 0)
3167 event
->flags
|= EVENT_FL_ISBPRINT
;
3169 event
->id
= event_read_id();
3171 die("failed to read ftrace event id");
3175 ret
= event_read_format(event
);
3177 die("failed to read ftrace event format");
3179 ret
= event_read_print(event
);
3181 die("failed to read ftrace event print fmt");
3183 /* New ftrace handles args */
3187 * The arguments for ftrace files are parsed by the fields.
3188 * Set up the fields as their arguments.
3190 list
= &event
->print_fmt
.args
;
3191 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3192 arg
= malloc_or_die(sizeof(*arg
));
3193 memset(arg
, 0, sizeof(*arg
));
3196 arg
->type
= PRINT_FIELD
;
3197 arg
->field
.name
= field
->name
;
3198 arg
->field
.field
= field
;
3203 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3205 struct event
*event
;
3208 init_input_buf(buf
, size
);
3210 event
= alloc_event();
3214 event
->name
= event_read_name();
3216 die("failed to read event name");
3218 event
->id
= event_read_id();
3220 die("failed to read event id");
3222 ret
= event_read_format(event
);
3224 warning("failed to read event format for %s", event
->name
);
3228 ret
= event_read_print(event
);
3230 warning("failed to read event print fmt for %s", event
->name
);
3234 event
->system
= strdup(sys
);
3236 #define PRINT_ARGS 0
3237 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3238 print_args(event
->print_fmt
.args
);
3244 event
->flags
|= EVENT_FL_FAILED
;
3245 /* still add it even if it failed */
3250 void parse_set_info(int nr_cpus
, int long_sz
)
3253 long_size
= long_sz
;