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.
33 #include "trace-event.h"
35 int header_page_ts_offset
;
36 int header_page_ts_size
;
37 int header_page_size_offset
;
38 int header_page_size_size
;
39 int header_page_overwrite_offset
;
40 int header_page_overwrite_size
;
41 int header_page_data_offset
;
42 int header_page_data_size
;
46 static char *input_buf
;
47 static unsigned long long input_buf_ptr
;
48 static unsigned long long input_buf_siz
;
52 static int is_flag_field
;
53 static int is_symbolic_field
;
55 static struct format_field
*
56 find_any_field(struct event
*event
, const char *name
);
58 static void init_input_buf(char *buf
, unsigned long long size
)
70 static struct cmdline
*cmdlines
;
71 static int cmdline_count
;
73 static int cmdline_cmp(const void *a
, const void *b
)
75 const struct cmdline
*ca
= a
;
76 const struct cmdline
*cb
= b
;
78 if (ca
->pid
< cb
->pid
)
80 if (ca
->pid
> cb
->pid
)
86 void parse_cmdlines(char *file
, int size __unused
)
89 struct cmdline_list
*next
;
92 } *list
= NULL
, *item
;
97 line
= strtok_r(file
, "\n", &next
);
99 item
= malloc_or_die(sizeof(*item
));
100 sscanf(line
, "%d %as", &item
->pid
,
101 (float *)(void *)&item
->comm
); /* workaround gcc warning */
104 line
= strtok_r(NULL
, "\n", &next
);
108 cmdlines
= malloc_or_die(sizeof(*cmdlines
) * cmdline_count
);
112 cmdlines
[i
].pid
= list
->pid
;
113 cmdlines
[i
].comm
= list
->comm
;
120 qsort(cmdlines
, cmdline_count
, sizeof(*cmdlines
), cmdline_cmp
);
123 static struct func_map
{
124 unsigned long long addr
;
128 static unsigned int func_count
;
130 static int func_cmp(const void *a
, const void *b
)
132 const struct func_map
*fa
= a
;
133 const struct func_map
*fb
= b
;
135 if (fa
->addr
< fb
->addr
)
137 if (fa
->addr
> fb
->addr
)
143 void parse_proc_kallsyms(char *file
, unsigned int size __unused
)
146 struct func_list
*next
;
147 unsigned long long addr
;
150 } *list
= NULL
, *item
;
158 line
= strtok_r(file
, "\n", &next
);
160 item
= malloc_or_die(sizeof(*item
));
162 ret
= sscanf(line
, "%as %c %as\t[%as",
163 (float *)(void *)&addr_str
, /* workaround gcc warning */
165 (float *)(void *)&item
->func
,
166 (float *)(void *)&item
->mod
);
167 item
->addr
= strtoull(addr_str
, NULL
, 16);
170 /* truncate the extra ']' */
172 item
->mod
[strlen(item
->mod
) - 1] = 0;
177 line
= strtok_r(NULL
, "\n", &next
);
181 func_list
= malloc_or_die(sizeof(*func_list
) * (func_count
+ 1));
185 func_list
[i
].func
= list
->func
;
186 func_list
[i
].addr
= list
->addr
;
187 func_list
[i
].mod
= list
->mod
;
194 qsort(func_list
, func_count
, sizeof(*func_list
), func_cmp
);
197 * Add a special record at the end.
199 func_list
[func_count
].func
= NULL
;
200 func_list
[func_count
].addr
= 0;
201 func_list
[func_count
].mod
= NULL
;
205 * We are searching for a record in between, not an exact
208 static int func_bcmp(const void *a
, const void *b
)
210 const struct func_map
*fa
= a
;
211 const struct func_map
*fb
= b
;
213 if ((fa
->addr
== fb
->addr
) ||
215 (fa
->addr
> fb
->addr
&&
216 fa
->addr
< (fb
+1)->addr
))
219 if (fa
->addr
< fb
->addr
)
225 static struct func_map
*find_func(unsigned long long addr
)
227 struct func_map
*func
;
232 func
= bsearch(&key
, func_list
, func_count
, sizeof(*func_list
),
238 void print_funcs(void)
242 for (i
= 0; i
< (int)func_count
; i
++) {
246 if (func_list
[i
].mod
)
247 printf(" [%s]\n", func_list
[i
].mod
);
253 static struct printk_map
{
254 unsigned long long addr
;
257 static unsigned int printk_count
;
259 static int printk_cmp(const void *a
, const void *b
)
261 const struct func_map
*fa
= a
;
262 const struct func_map
*fb
= b
;
264 if (fa
->addr
< fb
->addr
)
266 if (fa
->addr
> fb
->addr
)
272 static struct printk_map
*find_printk(unsigned long long addr
)
274 struct printk_map
*printk
;
275 struct printk_map key
;
279 printk
= bsearch(&key
, printk_list
, printk_count
, sizeof(*printk_list
),
285 void parse_ftrace_printk(char *file
, unsigned int size __unused
)
288 struct printk_list
*next
;
289 unsigned long long addr
;
291 } *list
= NULL
, *item
;
297 line
= strtok_r(file
, "\n", &next
);
299 addr_str
= strsep(&line
, ":");
301 warning("error parsing print strings");
304 item
= malloc_or_die(sizeof(*item
));
305 item
->addr
= strtoull(addr_str
, NULL
, 16);
306 /* fmt still has a space, skip it */
307 item
->printk
= strdup(line
+1);
310 line
= strtok_r(NULL
, "\n", &next
);
314 printk_list
= malloc_or_die(sizeof(*printk_list
) * printk_count
+ 1);
318 printk_list
[i
].printk
= list
->printk
;
319 printk_list
[i
].addr
= list
->addr
;
326 qsort(printk_list
, printk_count
, sizeof(*printk_list
), printk_cmp
);
329 void print_printk(void)
333 for (i
= 0; i
< (int)printk_count
; i
++) {
334 printf("%016llx %s\n",
336 printk_list
[i
].printk
);
340 static struct event
*alloc_event(void)
344 event
= malloc_or_die(sizeof(*event
));
345 memset(event
, 0, sizeof(*event
));
362 static struct event
*event_list
;
364 static void add_event(struct event
*event
)
366 event
->next
= event_list
;
370 static int event_item_type(enum event_type type
)
373 case EVENT_ITEM
... EVENT_SQUOTE
:
375 case EVENT_ERROR
... EVENT_DELIM
:
381 static void free_arg(struct print_arg
*arg
)
389 free(arg
->atom
.atom
);
392 case PRINT_FIELD
... PRINT_OP
:
401 static enum event_type
get_type(int ch
)
404 return EVENT_NEWLINE
;
407 if (isalnum(ch
) || ch
== '_')
415 if (ch
== '(' || ch
== ')' || ch
== ',')
421 static int __read_char(void)
423 if (input_buf_ptr
>= input_buf_siz
)
426 return input_buf
[input_buf_ptr
++];
429 static int __peek_char(void)
431 if (input_buf_ptr
>= input_buf_siz
)
434 return input_buf
[input_buf_ptr
];
437 static enum event_type
__read_token(char **tok
)
440 int ch
, last_ch
, quote_ch
, next_ch
;
443 enum event_type type
;
453 if (type
== EVENT_NONE
)
461 *tok
= malloc_or_die(2);
469 next_ch
= __peek_char();
470 if (next_ch
== '>') {
471 buf
[i
++] = __read_char();
484 buf
[i
++] = __read_char();
496 default: /* what should we do instead? */
506 buf
[i
++] = __read_char();
511 /* don't keep quotes */
516 if (i
== (BUFSIZ
- 1)) {
519 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
534 /* the '\' '\' will cancel itself */
535 if (ch
== '\\' && last_ch
== '\\')
537 } while (ch
!= quote_ch
|| last_ch
== '\\');
538 /* remove the last quote */
542 case EVENT_ERROR
... EVENT_SPACE
:
548 while (get_type(__peek_char()) == type
) {
549 if (i
== (BUFSIZ
- 1)) {
552 *tok
= realloc(*tok
, tok_size
+ BUFSIZ
);
571 *tok
= realloc(*tok
, tok_size
+ i
);
583 static void free_token(char *tok
)
589 static enum event_type
read_token(char **tok
)
591 enum event_type type
;
594 type
= __read_token(tok
);
595 if (type
!= EVENT_SPACE
)
606 static enum event_type
read_token_item(char **tok
)
608 enum event_type type
;
611 type
= __read_token(tok
);
612 if (type
!= EVENT_SPACE
&& type
!= EVENT_NEWLINE
)
622 static int test_type(enum event_type type
, enum event_type expect
)
624 if (type
!= expect
) {
625 warning("Error: expected type %d but read %d",
632 static int __test_type_token(enum event_type type
, char *token
,
633 enum event_type expect
, const char *expect_tok
,
636 if (type
!= expect
) {
638 warning("Error: expected type %d but read %d",
643 if (strcmp(token
, expect_tok
) != 0) {
645 warning("Error: expected '%s' but read '%s'",
652 static int test_type_token(enum event_type type
, char *token
,
653 enum event_type expect
, const char *expect_tok
)
655 return __test_type_token(type
, token
, expect
, expect_tok
, true);
658 static int __read_expect_type(enum event_type expect
, char **tok
, int newline_ok
)
660 enum event_type type
;
663 type
= read_token(tok
);
665 type
= read_token_item(tok
);
666 return test_type(type
, expect
);
669 static int read_expect_type(enum event_type expect
, char **tok
)
671 return __read_expect_type(expect
, tok
, 1);
674 static int __read_expected(enum event_type expect
, const char *str
,
675 int newline_ok
, bool warn
)
677 enum event_type type
;
682 type
= read_token(&token
);
684 type
= read_token_item(&token
);
686 ret
= __test_type_token(type
, token
, expect
, str
, warn
);
693 static int read_expected(enum event_type expect
, const char *str
)
695 return __read_expected(expect
, str
, 1, true);
698 static int read_expected_item(enum event_type expect
, const char *str
)
700 return __read_expected(expect
, str
, 0, true);
703 static char *event_read_name(void)
707 if (read_expected(EVENT_ITEM
, "name") < 0)
710 if (read_expected(EVENT_OP
, ":") < 0)
713 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
723 static int event_read_id(void)
728 if (read_expected_item(EVENT_ITEM
, "ID") < 0)
731 if (read_expected(EVENT_OP
, ":") < 0)
734 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
737 id
= strtoul(token
, NULL
, 0);
746 static int field_is_string(struct format_field
*field
)
748 if ((field
->flags
& FIELD_IS_ARRAY
) &&
749 (!strstr(field
->type
, "char") || !strstr(field
->type
, "u8") ||
750 !strstr(field
->type
, "s8")))
756 static int field_is_dynamic(struct format_field
*field
)
758 if (!strncmp(field
->type
, "__data_loc", 10))
764 static int event_read_fields(struct event
*event
, struct format_field
**fields
)
766 struct format_field
*field
= NULL
;
767 enum event_type type
;
773 type
= read_token(&token
);
774 if (type
== EVENT_NEWLINE
) {
781 if (test_type_token(type
, token
, EVENT_ITEM
, "field"))
785 type
= read_token(&token
);
787 * The ftrace fields may still use the "special" name.
790 if (event
->flags
& EVENT_FL_ISFTRACE
&&
791 type
== EVENT_ITEM
&& strcmp(token
, "special") == 0) {
793 type
= read_token(&token
);
796 if (test_type_token(type
, token
, EVENT_OP
, ":") < 0)
799 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
804 field
= malloc_or_die(sizeof(*field
));
805 memset(field
, 0, sizeof(*field
));
807 /* read the rest of the type */
809 type
= read_token(&token
);
810 if (type
== EVENT_ITEM
||
811 (type
== EVENT_OP
&& strcmp(token
, "*") == 0) ||
813 * Some of the ftrace fields are broken and have
814 * an illegal "." in them.
816 (event
->flags
& EVENT_FL_ISFTRACE
&&
817 type
== EVENT_OP
&& strcmp(token
, ".") == 0)) {
819 if (strcmp(token
, "*") == 0)
820 field
->flags
|= FIELD_IS_POINTER
;
823 field
->type
= realloc(field
->type
,
824 strlen(field
->type
) +
825 strlen(last_token
) + 2);
826 strcat(field
->type
, " ");
827 strcat(field
->type
, last_token
);
829 field
->type
= last_token
;
838 die("no type found");
841 field
->name
= last_token
;
843 if (test_type(type
, EVENT_OP
))
846 if (strcmp(token
, "[") == 0) {
847 enum event_type last_type
= type
;
848 char *brackets
= token
;
851 field
->flags
|= FIELD_IS_ARRAY
;
853 type
= read_token(&token
);
854 while (strcmp(token
, "]") != 0) {
855 if (last_type
== EVENT_ITEM
&&
862 brackets
= realloc(brackets
,
864 strlen(token
) + len
);
866 strcat(brackets
, " ");
867 strcat(brackets
, token
);
869 type
= read_token(&token
);
870 if (type
== EVENT_NONE
) {
871 die("failed to find token");
878 brackets
= realloc(brackets
, strlen(brackets
) + 2);
879 strcat(brackets
, "]");
881 /* add brackets to type */
883 type
= read_token(&token
);
885 * If the next token is not an OP, then it is of
886 * the format: type [] item;
888 if (type
== EVENT_ITEM
) {
889 field
->type
= realloc(field
->type
,
890 strlen(field
->type
) +
891 strlen(field
->name
) +
892 strlen(brackets
) + 2);
893 strcat(field
->type
, " ");
894 strcat(field
->type
, field
->name
);
895 free_token(field
->name
);
896 strcat(field
->type
, brackets
);
898 type
= read_token(&token
);
900 field
->type
= realloc(field
->type
,
901 strlen(field
->type
) +
902 strlen(brackets
) + 1);
903 strcat(field
->type
, brackets
);
908 if (field_is_string(field
)) {
909 field
->flags
|= FIELD_IS_STRING
;
910 if (field_is_dynamic(field
))
911 field
->flags
|= FIELD_IS_DYNAMIC
;
914 if (test_type_token(type
, token
, EVENT_OP
, ";"))
918 if (read_expected(EVENT_ITEM
, "offset") < 0)
921 if (read_expected(EVENT_OP
, ":") < 0)
924 if (read_expect_type(EVENT_ITEM
, &token
))
926 field
->offset
= strtoul(token
, NULL
, 0);
929 if (read_expected(EVENT_OP
, ";") < 0)
932 if (read_expected(EVENT_ITEM
, "size") < 0)
935 if (read_expected(EVENT_OP
, ":") < 0)
938 if (read_expect_type(EVENT_ITEM
, &token
))
940 field
->size
= strtoul(token
, NULL
, 0);
943 if (read_expected(EVENT_OP
, ";") < 0)
946 type
= read_token(&token
);
947 if (type
!= EVENT_NEWLINE
) {
948 /* newer versions of the kernel have a "signed" type */
949 if (test_type_token(type
, token
, EVENT_ITEM
, "signed"))
954 if (read_expected(EVENT_OP
, ":") < 0)
957 if (read_expect_type(EVENT_ITEM
, &token
))
960 if (strtoul(token
, NULL
, 0))
961 field
->flags
|= FIELD_IS_SIGNED
;
964 if (read_expected(EVENT_OP
, ";") < 0)
967 if (read_expect_type(EVENT_NEWLINE
, &token
))
974 fields
= &field
->next
;
988 static int event_read_format(struct event
*event
)
993 if (read_expected_item(EVENT_ITEM
, "format") < 0)
996 if (read_expected(EVENT_OP
, ":") < 0)
999 if (read_expect_type(EVENT_NEWLINE
, &token
))
1003 ret
= event_read_fields(event
, &event
->format
.common_fields
);
1006 event
->format
.nr_common
= ret
;
1008 ret
= event_read_fields(event
, &event
->format
.fields
);
1011 event
->format
.nr_fields
= ret
;
1021 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1022 char **tok
, enum event_type type
);
1024 static enum event_type
1025 process_arg(struct event
*event
, struct print_arg
*arg
, char **tok
)
1027 enum event_type type
;
1030 type
= read_token(&token
);
1033 return process_arg_token(event
, arg
, tok
, type
);
1036 static enum event_type
1037 process_cond(struct event
*event
, struct print_arg
*top
, char **tok
)
1039 struct print_arg
*arg
, *left
, *right
;
1040 enum event_type type
;
1043 arg
= malloc_or_die(sizeof(*arg
));
1044 memset(arg
, 0, sizeof(*arg
));
1046 left
= malloc_or_die(sizeof(*left
));
1048 right
= malloc_or_die(sizeof(*right
));
1050 arg
->type
= PRINT_OP
;
1051 arg
->op
.left
= left
;
1052 arg
->op
.right
= right
;
1055 type
= process_arg(event
, left
, &token
);
1056 if (test_type_token(type
, token
, EVENT_OP
, ":"))
1061 type
= process_arg(event
, right
, &token
);
1063 top
->op
.right
= arg
;
1076 static enum event_type
1077 process_array(struct event
*event
, struct print_arg
*top
, char **tok
)
1079 struct print_arg
*arg
;
1080 enum event_type type
;
1083 arg
= malloc_or_die(sizeof(*arg
));
1084 memset(arg
, 0, sizeof(*arg
));
1087 type
= process_arg(event
, arg
, &token
);
1088 if (test_type_token(type
, token
, EVENT_OP
, "]"))
1091 top
->op
.right
= arg
;
1094 type
= read_token_item(&token
);
1105 static int get_op_prio(char *op
)
1116 /* '>>' and '<<' are 8 */
1120 /* '==' and '!=' are 10 */
1130 die("unknown op '%c'", op
[0]);
1134 if (strcmp(op
, "++") == 0 ||
1135 strcmp(op
, "--") == 0) {
1137 } else if (strcmp(op
, ">>") == 0 ||
1138 strcmp(op
, "<<") == 0) {
1140 } else if (strcmp(op
, ">=") == 0 ||
1141 strcmp(op
, "<=") == 0) {
1143 } else if (strcmp(op
, "==") == 0 ||
1144 strcmp(op
, "!=") == 0) {
1146 } else if (strcmp(op
, "&&") == 0) {
1148 } else if (strcmp(op
, "||") == 0) {
1151 die("unknown op '%s'", op
);
1157 static void set_op_prio(struct print_arg
*arg
)
1160 /* single ops are the greatest */
1161 if (!arg
->op
.left
|| arg
->op
.left
->type
== PRINT_NULL
) {
1166 arg
->op
.prio
= get_op_prio(arg
->op
.op
);
1169 static enum event_type
1170 process_op(struct event
*event
, struct print_arg
*arg
, char **tok
)
1172 struct print_arg
*left
, *right
= NULL
;
1173 enum event_type type
;
1176 /* the op is passed in via tok */
1179 if (arg
->type
== PRINT_OP
&& !arg
->op
.left
) {
1180 /* handle single op */
1182 die("bad op token %s", token
);
1191 die("bad op token %s", token
);
1195 /* make an empty left */
1196 left
= malloc_or_die(sizeof(*left
));
1197 left
->type
= PRINT_NULL
;
1198 arg
->op
.left
= left
;
1200 right
= malloc_or_die(sizeof(*right
));
1201 arg
->op
.right
= right
;
1203 type
= process_arg(event
, right
, tok
);
1205 } else if (strcmp(token
, "?") == 0) {
1207 left
= malloc_or_die(sizeof(*left
));
1208 /* copy the top arg to the left */
1211 arg
->type
= PRINT_OP
;
1213 arg
->op
.left
= left
;
1216 type
= process_cond(event
, arg
, tok
);
1218 } else if (strcmp(token
, ">>") == 0 ||
1219 strcmp(token
, "<<") == 0 ||
1220 strcmp(token
, "&") == 0 ||
1221 strcmp(token
, "|") == 0 ||
1222 strcmp(token
, "&&") == 0 ||
1223 strcmp(token
, "||") == 0 ||
1224 strcmp(token
, "-") == 0 ||
1225 strcmp(token
, "+") == 0 ||
1226 strcmp(token
, "*") == 0 ||
1227 strcmp(token
, "^") == 0 ||
1228 strcmp(token
, "/") == 0 ||
1229 strcmp(token
, "<") == 0 ||
1230 strcmp(token
, ">") == 0 ||
1231 strcmp(token
, "==") == 0 ||
1232 strcmp(token
, "!=") == 0) {
1234 left
= malloc_or_die(sizeof(*left
));
1236 /* copy the top arg to the left */
1239 arg
->type
= PRINT_OP
;
1241 arg
->op
.left
= left
;
1245 right
= malloc_or_die(sizeof(*right
));
1247 type
= read_token_item(&token
);
1250 /* could just be a type pointer */
1251 if ((strcmp(arg
->op
.op
, "*") == 0) &&
1252 type
== EVENT_DELIM
&& (strcmp(token
, ")") == 0)) {
1253 if (left
->type
!= PRINT_ATOM
)
1254 die("bad pointer type");
1255 left
->atom
.atom
= realloc(left
->atom
.atom
,
1256 sizeof(left
->atom
.atom
) + 3);
1257 strcat(left
->atom
.atom
, " *");
1264 type
= process_arg_token(event
, right
, tok
, type
);
1266 arg
->op
.right
= right
;
1268 } else if (strcmp(token
, "[") == 0) {
1270 left
= malloc_or_die(sizeof(*left
));
1273 arg
->type
= PRINT_OP
;
1275 arg
->op
.left
= left
;
1278 type
= process_array(event
, arg
, tok
);
1281 warning("unknown op '%s'", token
);
1282 event
->flags
|= EVENT_FL_FAILED
;
1283 /* the arg is now the left side */
1287 if (type
== EVENT_OP
) {
1290 /* higher prios need to be closer to the root */
1291 prio
= get_op_prio(*tok
);
1293 if (prio
> arg
->op
.prio
)
1294 return process_op(event
, arg
, tok
);
1296 return process_op(event
, right
, tok
);
1302 static enum event_type
1303 process_entry(struct event
*event __unused
, struct print_arg
*arg
,
1306 enum event_type type
;
1310 if (read_expected(EVENT_OP
, "->") < 0)
1313 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1317 arg
->type
= PRINT_FIELD
;
1318 arg
->field
.name
= field
;
1320 if (is_flag_field
) {
1321 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1322 arg
->field
.field
->flags
|= FIELD_IS_FLAG
;
1324 } else if (is_symbolic_field
) {
1325 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
1326 arg
->field
.field
->flags
|= FIELD_IS_SYMBOLIC
;
1327 is_symbolic_field
= 0;
1330 type
= read_token(&token
);
1340 static char *arg_eval (struct print_arg
*arg
);
1342 static long long arg_num_eval(struct print_arg
*arg
)
1344 long long left
, right
;
1347 switch (arg
->type
) {
1349 val
= strtoll(arg
->atom
.atom
, NULL
, 0);
1352 val
= arg_num_eval(arg
->typecast
.item
);
1355 switch (arg
->op
.op
[0]) {
1357 left
= arg_num_eval(arg
->op
.left
);
1358 right
= arg_num_eval(arg
->op
.right
);
1360 val
= left
|| right
;
1365 left
= arg_num_eval(arg
->op
.left
);
1366 right
= arg_num_eval(arg
->op
.right
);
1368 val
= left
&& right
;
1373 left
= arg_num_eval(arg
->op
.left
);
1374 right
= arg_num_eval(arg
->op
.right
);
1375 switch (arg
->op
.op
[1]) {
1380 val
= left
<< right
;
1383 val
= left
<= right
;
1386 die("unknown op '%s'", arg
->op
.op
);
1390 left
= arg_num_eval(arg
->op
.left
);
1391 right
= arg_num_eval(arg
->op
.right
);
1392 switch (arg
->op
.op
[1]) {
1397 val
= left
>> right
;
1400 val
= left
>= right
;
1403 die("unknown op '%s'", arg
->op
.op
);
1407 left
= arg_num_eval(arg
->op
.left
);
1408 right
= arg_num_eval(arg
->op
.right
);
1410 if (arg
->op
.op
[1] != '=')
1411 die("unknown op '%s'", arg
->op
.op
);
1413 val
= left
== right
;
1416 left
= arg_num_eval(arg
->op
.left
);
1417 right
= arg_num_eval(arg
->op
.right
);
1419 switch (arg
->op
.op
[1]) {
1421 val
= left
!= right
;
1424 die("unknown op '%s'", arg
->op
.op
);
1428 die("unknown op '%s'", arg
->op
.op
);
1433 case PRINT_FIELD
... PRINT_SYMBOL
:
1436 die("invalid eval type %d", arg
->type
);
1442 static char *arg_eval (struct print_arg
*arg
)
1445 static char buf
[20];
1447 switch (arg
->type
) {
1449 return arg
->atom
.atom
;
1451 return arg_eval(arg
->typecast
.item
);
1453 val
= arg_num_eval(arg
);
1454 sprintf(buf
, "%lld", val
);
1458 case PRINT_FIELD
... PRINT_SYMBOL
:
1461 die("invalid eval type %d", arg
->type
);
1468 static enum event_type
1469 process_fields(struct event
*event
, struct print_flag_sym
**list
, char **tok
)
1471 enum event_type type
;
1472 struct print_arg
*arg
= NULL
;
1473 struct print_flag_sym
*field
;
1479 type
= read_token_item(&token
);
1480 if (test_type_token(type
, token
, EVENT_OP
, "{"))
1483 arg
= malloc_or_die(sizeof(*arg
));
1486 type
= process_arg(event
, arg
, &token
);
1487 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1490 field
= malloc_or_die(sizeof(*field
));
1491 memset(field
, 0, sizeof(*field
));
1493 value
= arg_eval(arg
);
1494 field
->value
= strdup(value
);
1497 type
= process_arg(event
, arg
, &token
);
1498 if (test_type_token(type
, token
, EVENT_OP
, "}"))
1501 value
= arg_eval(arg
);
1502 field
->str
= strdup(value
);
1507 list
= &field
->next
;
1510 type
= read_token_item(&token
);
1511 } while (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0);
1523 static enum event_type
1524 process_flags(struct event
*event
, struct print_arg
*arg
, char **tok
)
1526 struct print_arg
*field
;
1527 enum event_type type
;
1530 memset(arg
, 0, sizeof(*arg
));
1531 arg
->type
= PRINT_FLAGS
;
1533 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1536 field
= malloc_or_die(sizeof(*field
));
1538 type
= process_arg(event
, field
, &token
);
1539 while (type
== EVENT_OP
)
1540 type
= process_op(event
, field
, &token
);
1541 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1544 arg
->flags
.field
= field
;
1546 type
= read_token_item(&token
);
1547 if (event_item_type(type
)) {
1548 arg
->flags
.delim
= token
;
1549 type
= read_token_item(&token
);
1552 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1555 type
= process_fields(event
, &arg
->flags
.flags
, &token
);
1556 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1560 type
= read_token_item(tok
);
1568 static enum event_type
1569 process_symbols(struct event
*event
, struct print_arg
*arg
, char **tok
)
1571 struct print_arg
*field
;
1572 enum event_type type
;
1575 memset(arg
, 0, sizeof(*arg
));
1576 arg
->type
= PRINT_SYMBOL
;
1578 if (read_expected_item(EVENT_DELIM
, "(") < 0)
1581 field
= malloc_or_die(sizeof(*field
));
1583 type
= process_arg(event
, field
, &token
);
1584 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1587 arg
->symbol
.field
= field
;
1589 type
= process_fields(event
, &arg
->symbol
.symbols
, &token
);
1590 if (test_type_token(type
, token
, EVENT_DELIM
, ")"))
1594 type
= read_token_item(tok
);
1602 static enum event_type
1603 process_paren(struct event
*event
, struct print_arg
*arg
, char **tok
)
1605 struct print_arg
*item_arg
;
1606 enum event_type type
;
1609 type
= process_arg(event
, arg
, &token
);
1611 if (type
== EVENT_ERROR
)
1614 if (type
== EVENT_OP
)
1615 type
= process_op(event
, arg
, &token
);
1617 if (type
== EVENT_ERROR
)
1620 if (test_type_token(type
, token
, EVENT_DELIM
, ")")) {
1626 type
= read_token_item(&token
);
1629 * If the next token is an item or another open paren, then
1630 * this was a typecast.
1632 if (event_item_type(type
) ||
1633 (type
== EVENT_DELIM
&& strcmp(token
, "(") == 0)) {
1635 /* make this a typecast and contine */
1637 /* prevous must be an atom */
1638 if (arg
->type
!= PRINT_ATOM
)
1639 die("previous needed to be PRINT_ATOM");
1641 item_arg
= malloc_or_die(sizeof(*item_arg
));
1643 arg
->type
= PRINT_TYPE
;
1644 arg
->typecast
.type
= arg
->atom
.atom
;
1645 arg
->typecast
.item
= item_arg
;
1646 type
= process_arg_token(event
, item_arg
, &token
, type
);
1655 static enum event_type
1656 process_str(struct event
*event __unused
, struct print_arg
*arg
, char **tok
)
1658 enum event_type type
;
1661 if (read_expected(EVENT_DELIM
, "(") < 0)
1664 if (read_expect_type(EVENT_ITEM
, &token
) < 0)
1667 arg
->type
= PRINT_STRING
;
1668 arg
->string
.string
= token
;
1669 arg
->string
.offset
= -1;
1671 if (read_expected(EVENT_DELIM
, ")") < 0)
1674 type
= read_token(&token
);
1684 process_arg_token(struct event
*event
, struct print_arg
*arg
,
1685 char **tok
, enum event_type type
)
1694 if (strcmp(token
, "REC") == 0) {
1696 type
= process_entry(event
, arg
, &token
);
1697 } else if (strcmp(token
, "__print_flags") == 0) {
1700 type
= process_flags(event
, arg
, &token
);
1701 } else if (strcmp(token
, "__print_symbolic") == 0) {
1703 is_symbolic_field
= 1;
1704 type
= process_symbols(event
, arg
, &token
);
1705 } else if (strcmp(token
, "__get_str") == 0) {
1707 type
= process_str(event
, arg
, &token
);
1710 /* test the next token */
1711 type
= read_token_item(&token
);
1713 /* atoms can be more than one token long */
1714 while (type
== EVENT_ITEM
) {
1715 atom
= realloc(atom
, strlen(atom
) + strlen(token
) + 2);
1717 strcat(atom
, token
);
1719 type
= read_token_item(&token
);
1722 /* todo, test for function */
1724 arg
->type
= PRINT_ATOM
;
1725 arg
->atom
.atom
= atom
;
1730 arg
->type
= PRINT_ATOM
;
1731 arg
->atom
.atom
= token
;
1732 type
= read_token_item(&token
);
1735 if (strcmp(token
, "(") == 0) {
1737 type
= process_paren(event
, arg
, &token
);
1741 /* handle single ops */
1742 arg
->type
= PRINT_OP
;
1744 arg
->op
.left
= NULL
;
1745 type
= process_op(event
, arg
, &token
);
1749 case EVENT_ERROR
... EVENT_NEWLINE
:
1751 die("unexpected type %d", type
);
1758 static int event_read_print_args(struct event
*event
, struct print_arg
**list
)
1760 enum event_type type
= EVENT_ERROR
;
1761 struct print_arg
*arg
;
1766 if (type
== EVENT_NEWLINE
) {
1768 type
= read_token_item(&token
);
1772 arg
= malloc_or_die(sizeof(*arg
));
1773 memset(arg
, 0, sizeof(*arg
));
1775 type
= process_arg(event
, arg
, &token
);
1777 if (type
== EVENT_ERROR
) {
1785 if (type
== EVENT_OP
) {
1786 type
= process_op(event
, arg
, &token
);
1791 if (type
== EVENT_DELIM
&& strcmp(token
, ",") == 0) {
1798 } while (type
!= EVENT_NONE
);
1800 if (type
!= EVENT_NONE
)
1806 static int event_read_print(struct event
*event
)
1808 enum event_type type
;
1812 if (read_expected_item(EVENT_ITEM
, "print") < 0)
1815 if (read_expected(EVENT_ITEM
, "fmt") < 0)
1818 if (read_expected(EVENT_OP
, ":") < 0)
1821 if (read_expect_type(EVENT_DQUOTE
, &token
) < 0)
1825 event
->print_fmt
.format
= token
;
1826 event
->print_fmt
.args
= NULL
;
1828 /* ok to have no arg */
1829 type
= read_token_item(&token
);
1831 if (type
== EVENT_NONE
)
1834 /* Handle concatination of print lines */
1835 if (type
== EVENT_DQUOTE
) {
1838 cat
= malloc_or_die(strlen(event
->print_fmt
.format
) +
1840 strcpy(cat
, event
->print_fmt
.format
);
1843 free_token(event
->print_fmt
.format
);
1844 event
->print_fmt
.format
= NULL
;
1849 if (test_type_token(type
, token
, EVENT_DELIM
, ","))
1854 ret
= event_read_print_args(event
, &event
->print_fmt
.args
);
1865 static struct format_field
*
1866 find_common_field(struct event
*event
, const char *name
)
1868 struct format_field
*format
;
1870 for (format
= event
->format
.common_fields
;
1871 format
; format
= format
->next
) {
1872 if (strcmp(format
->name
, name
) == 0)
1879 static struct format_field
*
1880 find_field(struct event
*event
, const char *name
)
1882 struct format_field
*format
;
1884 for (format
= event
->format
.fields
;
1885 format
; format
= format
->next
) {
1886 if (strcmp(format
->name
, name
) == 0)
1893 static struct format_field
*
1894 find_any_field(struct event
*event
, const char *name
)
1896 struct format_field
*format
;
1898 format
= find_common_field(event
, name
);
1901 return find_field(event
, name
);
1904 unsigned long long read_size(void *ptr
, int size
)
1908 return *(unsigned char *)ptr
;
1910 return data2host2(ptr
);
1912 return data2host4(ptr
);
1914 return data2host8(ptr
);
1922 raw_field_value(struct event
*event
, const char *name
, void *data
)
1924 struct format_field
*field
;
1926 field
= find_any_field(event
, name
);
1930 return read_size(data
+ field
->offset
, field
->size
);
1933 void *raw_field_ptr(struct event
*event
, const char *name
, void *data
)
1935 struct format_field
*field
;
1937 field
= find_any_field(event
, name
);
1941 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1944 offset
= *(int *)(data
+ field
->offset
);
1947 return data
+ offset
;
1950 return data
+ field
->offset
;
1953 static int get_common_info(const char *type
, int *offset
, int *size
)
1955 struct event
*event
;
1956 struct format_field
*field
;
1959 * All events should have the same common elements.
1960 * Pick any event to find where the type is;
1963 die("no event_list!");
1966 field
= find_common_field(event
, type
);
1968 die("field '%s' not found", type
);
1970 *offset
= field
->offset
;
1971 *size
= field
->size
;
1976 static int __parse_common(void *data
, int *size
, int *offset
,
1982 ret
= get_common_info(name
, offset
, size
);
1986 return read_size(data
+ *offset
, *size
);
1989 int trace_parse_common_type(void *data
)
1991 static int type_offset
;
1992 static int type_size
;
1994 return __parse_common(data
, &type_size
, &type_offset
,
1998 int trace_parse_common_pid(void *data
)
2000 static int pid_offset
;
2001 static int pid_size
;
2003 return __parse_common(data
, &pid_size
, &pid_offset
,
2007 int parse_common_pc(void *data
)
2009 static int pc_offset
;
2012 return __parse_common(data
, &pc_size
, &pc_offset
,
2013 "common_preempt_count");
2016 int parse_common_flags(void *data
)
2018 static int flags_offset
;
2019 static int flags_size
;
2021 return __parse_common(data
, &flags_size
, &flags_offset
,
2025 int parse_common_lock_depth(void *data
)
2027 static int ld_offset
;
2031 ret
= __parse_common(data
, &ld_size
, &ld_offset
,
2032 "common_lock_depth");
2039 struct event
*trace_find_event(int id
)
2041 struct event
*event
;
2043 for (event
= event_list
; event
; event
= event
->next
) {
2044 if (event
->id
== id
)
2050 struct event
*trace_find_next_event(struct event
*event
)
2058 static unsigned long long eval_num_arg(void *data
, int size
,
2059 struct event
*event
, struct print_arg
*arg
)
2061 unsigned long long val
= 0;
2062 unsigned long long left
, right
;
2063 struct print_arg
*larg
;
2065 switch (arg
->type
) {
2070 return strtoull(arg
->atom
.atom
, NULL
, 0);
2072 if (!arg
->field
.field
) {
2073 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2074 if (!arg
->field
.field
)
2075 die("field %s not found", arg
->field
.name
);
2077 /* must be a number */
2078 val
= read_size(data
+ arg
->field
.field
->offset
,
2079 arg
->field
.field
->size
);
2085 return eval_num_arg(data
, size
, event
, arg
->typecast
.item
);
2090 if (strcmp(arg
->op
.op
, "[") == 0) {
2092 * Arrays are special, since we don't want
2093 * to read the arg as is.
2095 if (arg
->op
.left
->type
!= PRINT_FIELD
)
2096 goto default_op
; /* oops, all bets off */
2097 larg
= arg
->op
.left
;
2098 if (!larg
->field
.field
) {
2100 find_any_field(event
, larg
->field
.name
);
2101 if (!larg
->field
.field
)
2102 die("field %s not found", larg
->field
.name
);
2104 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2105 val
= read_size(data
+ larg
->field
.field
->offset
+
2106 right
* long_size
, long_size
);
2110 left
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2111 right
= eval_num_arg(data
, size
, event
, arg
->op
.right
);
2112 switch (arg
->op
.op
[0]) {
2115 val
= left
|| right
;
2121 val
= left
&& right
;
2126 switch (arg
->op
.op
[1]) {
2131 val
= left
<< right
;
2134 val
= left
<= right
;
2137 die("unknown op '%s'", arg
->op
.op
);
2141 switch (arg
->op
.op
[1]) {
2146 val
= left
>> right
;
2149 val
= left
>= right
;
2152 die("unknown op '%s'", arg
->op
.op
);
2156 if (arg
->op
.op
[1] != '=')
2157 die("unknown op '%s'", arg
->op
.op
);
2158 val
= left
== right
;
2167 die("unknown op '%s'", arg
->op
.op
);
2170 default: /* not sure what to do there */
2178 unsigned long long value
;
2181 static const struct flag flags
[] = {
2182 { "HI_SOFTIRQ", 0 },
2183 { "TIMER_SOFTIRQ", 1 },
2184 { "NET_TX_SOFTIRQ", 2 },
2185 { "NET_RX_SOFTIRQ", 3 },
2186 { "BLOCK_SOFTIRQ", 4 },
2187 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2188 { "TASKLET_SOFTIRQ", 6 },
2189 { "SCHED_SOFTIRQ", 7 },
2190 { "HRTIMER_SOFTIRQ", 8 },
2191 { "RCU_SOFTIRQ", 9 },
2193 { "HRTIMER_NORESTART", 0 },
2194 { "HRTIMER_RESTART", 1 },
2197 unsigned long long eval_flag(const char *flag
)
2202 * Some flags in the format files do not get converted.
2203 * If the flag is not numeric, see if it is something that
2204 * we already know about.
2206 if (isdigit(flag
[0]))
2207 return strtoull(flag
, NULL
, 0);
2209 for (i
= 0; i
< (int)(sizeof(flags
)/sizeof(flags
[0])); i
++)
2210 if (strcmp(flags
[i
].name
, flag
) == 0)
2211 return flags
[i
].value
;
2216 static void print_str_arg(void *data
, int size
,
2217 struct event
*event
, struct print_arg
*arg
)
2219 struct print_flag_sym
*flag
;
2220 unsigned long long val
, fval
;
2224 switch (arg
->type
) {
2229 printf("%s", arg
->atom
.atom
);
2232 if (!arg
->field
.field
) {
2233 arg
->field
.field
= find_any_field(event
, arg
->field
.name
);
2234 if (!arg
->field
.field
)
2235 die("field %s not found", arg
->field
.name
);
2237 str
= malloc_or_die(arg
->field
.field
->size
+ 1);
2238 memcpy(str
, data
+ arg
->field
.field
->offset
,
2239 arg
->field
.field
->size
);
2240 str
[arg
->field
.field
->size
] = 0;
2245 val
= eval_num_arg(data
, size
, event
, arg
->flags
.field
);
2247 for (flag
= arg
->flags
.flags
; flag
; flag
= flag
->next
) {
2248 fval
= eval_flag(flag
->value
);
2249 if (!val
&& !fval
) {
2250 printf("%s", flag
->str
);
2253 if (fval
&& (val
& fval
) == fval
) {
2254 if (print
&& arg
->flags
.delim
)
2255 printf("%s", arg
->flags
.delim
);
2256 printf("%s", flag
->str
);
2263 val
= eval_num_arg(data
, size
, event
, arg
->symbol
.field
);
2264 for (flag
= arg
->symbol
.symbols
; flag
; flag
= flag
->next
) {
2265 fval
= eval_flag(flag
->value
);
2267 printf("%s", flag
->str
);
2275 case PRINT_STRING
: {
2278 if (arg
->string
.offset
== -1) {
2279 struct format_field
*f
;
2281 f
= find_any_field(event
, arg
->string
.string
);
2282 arg
->string
.offset
= f
->offset
;
2284 str_offset
= *(int *)(data
+ arg
->string
.offset
);
2285 str_offset
&= 0xffff;
2286 printf("%s", ((char *)data
) + str_offset
);
2291 * The only op for string should be ? :
2293 if (arg
->op
.op
[0] != '?')
2295 val
= eval_num_arg(data
, size
, event
, arg
->op
.left
);
2297 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.left
);
2299 print_str_arg(data
, size
, event
, arg
->op
.right
->op
.right
);
2307 static struct print_arg
*make_bprint_args(char *fmt
, void *data
, int size
, struct event
*event
)
2309 static struct format_field
*field
, *ip_field
;
2310 struct print_arg
*args
, *arg
, **next
;
2311 unsigned long long ip
, val
;
2316 field
= find_field(event
, "buf");
2318 die("can't find buffer field for binary printk");
2319 ip_field
= find_field(event
, "ip");
2321 die("can't find ip field for binary printk");
2324 ip
= read_size(data
+ ip_field
->offset
, ip_field
->size
);
2327 * The first arg is the IP pointer.
2329 args
= malloc_or_die(sizeof(*args
));
2334 arg
->type
= PRINT_ATOM
;
2335 arg
->atom
.atom
= malloc_or_die(32);
2336 sprintf(arg
->atom
.atom
, "%lld", ip
);
2338 /* skip the first "%pf : " */
2339 for (ptr
= fmt
+ 6, bptr
= data
+ field
->offset
;
2340 bptr
< data
+ size
&& *ptr
; ptr
++) {
2364 /* the pointers are always 4 bytes aligned */
2365 bptr
= (void *)(((unsigned long)bptr
+ 3) &
2377 val
= read_size(bptr
, ls
);
2379 arg
= malloc_or_die(sizeof(*arg
));
2381 arg
->type
= PRINT_ATOM
;
2382 arg
->atom
.atom
= malloc_or_die(32);
2383 sprintf(arg
->atom
.atom
, "%lld", val
);
2388 arg
= malloc_or_die(sizeof(*arg
));
2390 arg
->type
= PRINT_STRING
;
2391 arg
->string
.string
= strdup(bptr
);
2392 bptr
+= strlen(bptr
) + 1;
2404 static void free_args(struct print_arg
*args
)
2406 struct print_arg
*next
;
2411 if (args
->type
== PRINT_ATOM
)
2412 free(args
->atom
.atom
);
2414 free(args
->string
.string
);
2420 static char *get_bprint_format(void *data
, int size __unused
, struct event
*event
)
2422 unsigned long long addr
;
2423 static struct format_field
*field
;
2424 struct printk_map
*printk
;
2429 field
= find_field(event
, "fmt");
2431 die("can't find format field for binary printk");
2432 printf("field->offset = %d size=%d\n", field
->offset
, field
->size
);
2435 addr
= read_size(data
+ field
->offset
, field
->size
);
2437 printk
= find_printk(addr
);
2439 format
= malloc_or_die(45);
2440 sprintf(format
, "%%pf : (NO FORMAT FOUND at %llx)\n",
2446 /* Remove any quotes. */
2449 format
= malloc_or_die(strlen(p
) + 10);
2450 sprintf(format
, "%s : %s", "%pf", p
);
2451 /* remove ending quotes and new line since we will add one too */
2452 p
= format
+ strlen(format
) - 1;
2457 if (strcmp(p
, "\\n") == 0)
2463 static void pretty_print(void *data
, int size
, struct event
*event
)
2465 struct print_fmt
*print_fmt
= &event
->print_fmt
;
2466 struct print_arg
*arg
= print_fmt
->args
;
2467 struct print_arg
*args
= NULL
;
2468 const char *ptr
= print_fmt
->format
;
2469 unsigned long long val
;
2470 struct func_map
*func
;
2471 const char *saveptr
;
2472 char *bprint_fmt
= NULL
;
2478 if (event
->flags
& EVENT_FL_ISFUNC
)
2479 ptr
= " %pF <-- %pF";
2481 if (event
->flags
& EVENT_FL_ISBPRINT
) {
2482 bprint_fmt
= get_bprint_format(data
, size
, event
);
2483 args
= make_bprint_args(bprint_fmt
, data
, size
, event
);
2488 for (; *ptr
; ptr
++) {
2510 } else if (*ptr
== '%') {
2535 if (*(ptr
+1) == 'F' ||
2548 die("no argument match");
2550 len
= ((unsigned long)ptr
+ 1) -
2551 (unsigned long)saveptr
;
2553 /* should never happen */
2557 memcpy(format
, saveptr
, len
);
2560 val
= eval_num_arg(data
, size
, event
, arg
);
2564 func
= find_func(val
);
2566 printf("%s", func
->func
);
2567 if (show_func
== 'F')
2575 printf(format
, (int)val
);
2578 printf(format
, (long)val
);
2581 printf(format
, (long long)val
);
2584 die("bad count (%d)", ls
);
2589 die("no matching argument");
2591 print_str_arg(data
, size
, event
, arg
);
2595 printf(">%c<", *ptr
);
2608 static inline int log10_cpu(int nb
)
2617 static void print_lat_fmt(void *data
, int size __unused
)
2619 unsigned int lat_flags
;
2625 lat_flags
= parse_common_flags(data
);
2626 pc
= parse_common_pc(data
);
2627 lock_depth
= parse_common_lock_depth(data
);
2629 hardirq
= lat_flags
& TRACE_FLAG_HARDIRQ
;
2630 softirq
= lat_flags
& TRACE_FLAG_SOFTIRQ
;
2633 (lat_flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
2634 (lat_flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ?
2636 (lat_flags
& TRACE_FLAG_NEED_RESCHED
) ?
2638 (hardirq
&& softirq
) ? 'H' :
2639 hardirq
? 'h' : softirq
? 's' : '.');
2649 printf("%d ", lock_depth
);
2652 #define TRACE_GRAPH_INDENT 2
2654 static struct record
*
2655 get_return_for_leaf(int cpu
, int cur_pid
, unsigned long long cur_func
,
2656 struct record
*next
)
2658 struct format_field
*field
;
2659 struct event
*event
;
2664 type
= trace_parse_common_type(next
->data
);
2665 event
= trace_find_event(type
);
2669 if (!(event
->flags
& EVENT_FL_ISFUNCRET
))
2672 pid
= trace_parse_common_pid(next
->data
);
2673 field
= find_field(event
, "func");
2675 die("function return does not have field func");
2677 val
= read_size(next
->data
+ field
->offset
, field
->size
);
2679 if (cur_pid
!= pid
|| cur_func
!= val
)
2682 /* this is a leaf, now advance the iterator */
2683 return trace_read_data(cpu
);
2686 /* Signal a overhead of time execution to the output */
2687 static void print_graph_overhead(unsigned long long duration
)
2689 /* Non nested entry or return */
2690 if (duration
== ~0ULL)
2691 return (void)printf(" ");
2693 /* Duration exceeded 100 msecs */
2694 if (duration
> 100000ULL)
2695 return (void)printf("! ");
2697 /* Duration exceeded 10 msecs */
2698 if (duration
> 10000ULL)
2699 return (void)printf("+ ");
2704 static void print_graph_duration(unsigned long long duration
)
2706 unsigned long usecs
= duration
/ 1000;
2707 unsigned long nsecs_rem
= duration
% 1000;
2708 /* log10(ULONG_MAX) + '\0' */
2714 sprintf(msecs_str
, "%lu", usecs
);
2717 len
= printf("%lu", usecs
);
2719 /* Print nsecs (we don't want to exceed 7 numbers) */
2721 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
2722 len
+= printf(".%s", nsecs_str
);
2727 /* Print remaining spaces to fit the row's width */
2728 for (i
= len
; i
< 7; i
++)
2735 print_graph_entry_leaf(struct event
*event
, void *data
, struct record
*ret_rec
)
2737 unsigned long long rettime
, calltime
;
2738 unsigned long long duration
, depth
;
2739 unsigned long long val
;
2740 struct format_field
*field
;
2741 struct func_map
*func
;
2742 struct event
*ret_event
;
2746 type
= trace_parse_common_type(ret_rec
->data
);
2747 ret_event
= trace_find_event(type
);
2749 field
= find_field(ret_event
, "rettime");
2751 die("can't find rettime in return graph");
2752 rettime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2754 field
= find_field(ret_event
, "calltime");
2756 die("can't find rettime in return graph");
2757 calltime
= read_size(ret_rec
->data
+ field
->offset
, field
->size
);
2759 duration
= rettime
- calltime
;
2762 print_graph_overhead(duration
);
2765 print_graph_duration(duration
);
2767 field
= find_field(event
, "depth");
2769 die("can't find depth in entry graph");
2770 depth
= read_size(data
+ field
->offset
, field
->size
);
2773 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2776 field
= find_field(event
, "func");
2778 die("can't find func in entry graph");
2779 val
= read_size(data
+ field
->offset
, field
->size
);
2780 func
= find_func(val
);
2783 printf("%s();", func
->func
);
2785 printf("%llx();", val
);
2788 static void print_graph_nested(struct event
*event
, void *data
)
2790 struct format_field
*field
;
2791 unsigned long long depth
;
2792 unsigned long long val
;
2793 struct func_map
*func
;
2797 print_graph_overhead(-1);
2802 field
= find_field(event
, "depth");
2804 die("can't find depth in entry graph");
2805 depth
= read_size(data
+ field
->offset
, field
->size
);
2808 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2811 field
= find_field(event
, "func");
2813 die("can't find func in entry graph");
2814 val
= read_size(data
+ field
->offset
, field
->size
);
2815 func
= find_func(val
);
2818 printf("%s() {", func
->func
);
2820 printf("%llx() {", val
);
2824 pretty_print_func_ent(void *data
, int size
, struct event
*event
,
2827 struct format_field
*field
;
2832 if (latency_format
) {
2833 print_lat_fmt(data
, size
);
2837 field
= find_field(event
, "func");
2839 die("function entry does not have func field");
2841 val
= read_size(data
+ field
->offset
, field
->size
);
2844 * peek_data may unmap the data pointer. Copy it first.
2846 copy_data
= malloc_or_die(size
);
2847 memcpy(copy_data
, data
, size
);
2850 rec
= trace_peek_data(cpu
);
2852 rec
= get_return_for_leaf(cpu
, pid
, val
, rec
);
2854 print_graph_entry_leaf(event
, data
, rec
);
2858 print_graph_nested(event
, data
);
2864 pretty_print_func_ret(void *data
, int size __unused
, struct event
*event
)
2866 unsigned long long rettime
, calltime
;
2867 unsigned long long duration
, depth
;
2868 struct format_field
*field
;
2871 if (latency_format
) {
2872 print_lat_fmt(data
, size
);
2876 field
= find_field(event
, "rettime");
2878 die("can't find rettime in return graph");
2879 rettime
= read_size(data
+ field
->offset
, field
->size
);
2881 field
= find_field(event
, "calltime");
2883 die("can't find calltime in return graph");
2884 calltime
= read_size(data
+ field
->offset
, field
->size
);
2886 duration
= rettime
- calltime
;
2889 print_graph_overhead(duration
);
2892 print_graph_duration(duration
);
2894 field
= find_field(event
, "depth");
2896 die("can't find depth in entry graph");
2897 depth
= read_size(data
+ field
->offset
, field
->size
);
2900 for (i
= 0; i
< (int)(depth
* TRACE_GRAPH_INDENT
); i
++)
2907 pretty_print_func_graph(void *data
, int size
, struct event
*event
,
2910 if (event
->flags
& EVENT_FL_ISFUNCENT
)
2911 pretty_print_func_ent(data
, size
, event
, cpu
, pid
);
2912 else if (event
->flags
& EVENT_FL_ISFUNCRET
)
2913 pretty_print_func_ret(data
, size
, event
);
2917 void print_trace_event(int cpu
, void *data
, int size
)
2919 struct event
*event
;
2923 type
= trace_parse_common_type(data
);
2925 event
= trace_find_event(type
);
2927 warning("ug! no event found for type %d", type
);
2931 pid
= trace_parse_common_pid(data
);
2933 if (event
->flags
& (EVENT_FL_ISFUNCENT
| EVENT_FL_ISFUNCRET
))
2934 return pretty_print_func_graph(data
, size
, event
, cpu
, pid
);
2937 print_lat_fmt(data
, size
);
2939 if (event
->flags
& EVENT_FL_FAILED
) {
2940 printf("EVENT '%s' FAILED TO PARSE\n",
2945 pretty_print(data
, size
, event
);
2948 static void print_fields(struct print_flag_sym
*field
)
2950 printf("{ %s, %s }", field
->value
, field
->str
);
2953 print_fields(field
->next
);
2957 static void print_args(struct print_arg
*args
)
2959 int print_paren
= 1;
2961 switch (args
->type
) {
2966 printf("%s", args
->atom
.atom
);
2969 printf("REC->%s", args
->field
.name
);
2972 printf("__print_flags(");
2973 print_args(args
->flags
.field
);
2974 printf(", %s, ", args
->flags
.delim
);
2975 print_fields(args
->flags
.flags
);
2979 printf("__print_symbolic(");
2980 print_args(args
->symbol
.field
);
2982 print_fields(args
->symbol
.symbols
);
2986 printf("__get_str(%s)", args
->string
.string
);
2989 printf("(%s)", args
->typecast
.type
);
2990 print_args(args
->typecast
.item
);
2993 if (strcmp(args
->op
.op
, ":") == 0)
2997 print_args(args
->op
.left
);
2998 printf(" %s ", args
->op
.op
);
2999 print_args(args
->op
.right
);
3004 /* we should warn... */
3009 print_args(args
->next
);
3013 int parse_ftrace_file(char *buf
, unsigned long size
)
3015 struct format_field
*field
;
3016 struct print_arg
*arg
, **list
;
3017 struct event
*event
;
3020 init_input_buf(buf
, size
);
3022 event
= alloc_event();
3026 event
->flags
|= EVENT_FL_ISFTRACE
;
3028 event
->name
= event_read_name();
3030 die("failed to read ftrace event name");
3032 if (strcmp(event
->name
, "function") == 0)
3033 event
->flags
|= EVENT_FL_ISFUNC
;
3035 else if (strcmp(event
->name
, "funcgraph_entry") == 0)
3036 event
->flags
|= EVENT_FL_ISFUNCENT
;
3038 else if (strcmp(event
->name
, "funcgraph_exit") == 0)
3039 event
->flags
|= EVENT_FL_ISFUNCRET
;
3041 else if (strcmp(event
->name
, "bprint") == 0)
3042 event
->flags
|= EVENT_FL_ISBPRINT
;
3044 event
->id
= event_read_id();
3046 die("failed to read ftrace event id");
3050 ret
= event_read_format(event
);
3052 die("failed to read ftrace event format");
3054 ret
= event_read_print(event
);
3056 die("failed to read ftrace event print fmt");
3058 /* New ftrace handles args */
3062 * The arguments for ftrace files are parsed by the fields.
3063 * Set up the fields as their arguments.
3065 list
= &event
->print_fmt
.args
;
3066 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
3067 arg
= malloc_or_die(sizeof(*arg
));
3068 memset(arg
, 0, sizeof(*arg
));
3071 arg
->type
= PRINT_FIELD
;
3072 arg
->field
.name
= field
->name
;
3073 arg
->field
.field
= field
;
3078 int parse_event_file(char *buf
, unsigned long size
, char *sys
)
3080 struct event
*event
;
3083 init_input_buf(buf
, size
);
3085 event
= alloc_event();
3089 event
->name
= event_read_name();
3091 die("failed to read event name");
3093 event
->id
= event_read_id();
3095 die("failed to read event id");
3097 ret
= event_read_format(event
);
3099 warning("failed to read event format for %s", event
->name
);
3103 ret
= event_read_print(event
);
3105 warning("failed to read event print fmt for %s", event
->name
);
3109 event
->system
= strdup(sys
);
3111 #define PRINT_ARGS 0
3112 if (PRINT_ARGS
&& event
->print_fmt
.args
)
3113 print_args(event
->print_fmt
.args
);
3119 event
->flags
|= EVENT_FL_FAILED
;
3120 /* still add it even if it failed */
3125 void parse_set_info(int nr_cpus
, int long_sz
)
3128 long_size
= long_sz
;
3131 int common_pc(struct scripting_context
*context
)
3133 return parse_common_pc(context
->event_data
);
3136 int common_flags(struct scripting_context
*context
)
3138 return parse_common_flags(context
->event_data
);
3141 int common_lock_depth(struct scripting_context
*context
)
3143 return parse_common_lock_depth(context
->event_data
);