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_overwrite_offset;
41 int header_page_overwrite_size;
42 int header_page_data_offset;
43 int header_page_data_size;
47 static char *input_buf;
48 static unsigned long long input_buf_ptr;
49 static unsigned long long input_buf_siz;
53 static int is_flag_field;
54 static int is_symbolic_field;
56 static struct format_field *
57 find_any_field(struct event *event, const char *name);
59 static void init_input_buf(char *buf, unsigned long long size)
71 static struct cmdline *cmdlines;
72 static int cmdline_count;
74 static int cmdline_cmp(const void *a, const void *b)
76 const struct cmdline *ca = a;
77 const struct cmdline *cb = b;
79 if (ca->pid < cb->pid)
81 if (ca->pid > cb->pid)
87 void parse_cmdlines(char *file, int size __unused)
90 struct cmdline_list *next;
93 } *list = NULL, *item;
98 line = strtok_r(file, "\n", &next);
100 item = malloc_or_die(sizeof(*item));
101 sscanf(line, "%d %as", &item->pid,
102 (float *)(void *)&item->comm); /* workaround gcc warning */
105 line = strtok_r(NULL, "\n", &next);
109 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
113 cmdlines[i].pid = list->pid;
114 cmdlines[i].comm = list->comm;
121 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
124 static struct func_map {
125 unsigned long long addr;
129 static unsigned int func_count;
131 static int func_cmp(const void *a, const void *b)
133 const struct func_map *fa = a;
134 const struct func_map *fb = b;
136 if (fa->addr < fb->addr)
138 if (fa->addr > fb->addr)
144 void parse_proc_kallsyms(char *file, unsigned int size __unused)
147 struct func_list *next;
148 unsigned long long addr;
151 } *list = NULL, *item;
159 line = strtok_r(file, "\n", &next);
161 item = malloc_or_die(sizeof(*item));
163 ret = sscanf(line, "%as %c %as\t[%as",
164 (float *)(void *)&addr_str, /* workaround gcc warning */
166 (float *)(void *)&item->func,
167 (float *)(void *)&item->mod);
168 item->addr = strtoull(addr_str, NULL, 16);
171 /* truncate the extra ']' */
173 item->mod[strlen(item->mod) - 1] = 0;
178 line = strtok_r(NULL, "\n", &next);
182 func_list = malloc_or_die(sizeof(*func_list) * (func_count + 1));
186 func_list[i].func = list->func;
187 func_list[i].addr = list->addr;
188 func_list[i].mod = list->mod;
195 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
198 * Add a special record at the end.
200 func_list[func_count].func = NULL;
201 func_list[func_count].addr = 0;
202 func_list[func_count].mod = NULL;
206 * We are searching for a record in between, not an exact
209 static int func_bcmp(const void *a, const void *b)
211 const struct func_map *fa = a;
212 const struct func_map *fb = b;
214 if ((fa->addr == fb->addr) ||
216 (fa->addr > fb->addr &&
217 fa->addr < (fb+1)->addr))
220 if (fa->addr < fb->addr)
226 static struct func_map *find_func(unsigned long long addr)
228 struct func_map *func;
233 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
239 void print_funcs(void)
243 for (i = 0; i < (int)func_count; i++) {
247 if (func_list[i].mod)
248 printf(" [%s]\n", func_list[i].mod);
254 static struct printk_map {
255 unsigned long long addr;
258 static unsigned int printk_count;
260 static int printk_cmp(const void *a, const void *b)
262 const struct func_map *fa = a;
263 const struct func_map *fb = b;
265 if (fa->addr < fb->addr)
267 if (fa->addr > fb->addr)
273 static struct printk_map *find_printk(unsigned long long addr)
275 struct printk_map *printk;
276 struct printk_map key;
280 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
286 void parse_ftrace_printk(char *file, unsigned int size __unused)
289 struct printk_list *next;
290 unsigned long long addr;
292 } *list = NULL, *item;
298 line = strtok_r(file, "\n", &next);
300 addr_str = strsep(&line, ":");
302 warning("error parsing print strings");
305 item = malloc_or_die(sizeof(*item));
306 item->addr = strtoull(addr_str, NULL, 16);
307 /* fmt still has a space, skip it */
308 item->printk = strdup(line+1);
311 line = strtok_r(NULL, "\n", &next);
315 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
319 printk_list[i].printk = list->printk;
320 printk_list[i].addr = list->addr;
327 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
330 void print_printk(void)
334 for (i = 0; i < (int)printk_count; i++) {
335 printf("%016llx %s\n",
337 printk_list[i].printk);
341 static struct event *alloc_event(void)
345 event = malloc_or_die(sizeof(*event));
346 memset(event, 0, sizeof(*event));
363 static struct event *event_list;
365 static void add_event(struct event *event)
367 event->next = event_list;
371 static int event_item_type(enum event_type type)
374 case EVENT_ITEM ... EVENT_SQUOTE:
376 case EVENT_ERROR ... EVENT_DELIM:
382 static void free_arg(struct print_arg *arg)
390 free(arg->atom.atom);
393 case PRINT_FIELD ... PRINT_OP:
402 static enum event_type get_type(int ch)
405 return EVENT_NEWLINE;
408 if (isalnum(ch) || ch == '_')
416 if (ch == '(' || ch == ')' || ch == ',')
422 static int __read_char(void)
424 if (input_buf_ptr >= input_buf_siz)
427 return input_buf[input_buf_ptr++];
430 static int __peek_char(void)
432 if (input_buf_ptr >= input_buf_siz)
435 return input_buf[input_buf_ptr];
438 static enum event_type __read_token(char **tok)
441 int ch, last_ch, quote_ch, next_ch;
444 enum event_type type;
454 if (type == EVENT_NONE)
462 *tok = malloc_or_die(2);
470 next_ch = __peek_char();
471 if (next_ch == '>') {
472 buf[i++] = __read_char();
485 buf[i++] = __read_char();
497 default: /* what should we do instead? */
507 buf[i++] = __read_char();
512 /* don't keep quotes */
517 if (i == (BUFSIZ - 1)) {
520 *tok = realloc(*tok, tok_size + BUFSIZ);
535 /* the '\' '\' will cancel itself */
536 if (ch == '\\' && last_ch == '\\')
538 } while (ch != quote_ch || last_ch == '\\');
539 /* remove the last quote */
543 case EVENT_ERROR ... EVENT_SPACE:
549 while (get_type(__peek_char()) == type) {
550 if (i == (BUFSIZ - 1)) {
553 *tok = realloc(*tok, tok_size + BUFSIZ);
572 *tok = realloc(*tok, tok_size + i);
584 static void free_token(char *tok)
590 static enum event_type read_token(char **tok)
592 enum event_type type;
595 type = __read_token(tok);
596 if (type != EVENT_SPACE)
607 static enum event_type read_token_item(char **tok)
609 enum event_type type;
612 type = __read_token(tok);
613 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
623 static int test_type(enum event_type type, enum event_type expect)
625 if (type != expect) {
626 warning("Error: expected type %d but read %d",
633 static int __test_type_token(enum event_type type, char *token,
634 enum event_type expect, const char *expect_tok,
637 if (type != expect) {
639 warning("Error: expected type %d but read %d",
644 if (strcmp(token, expect_tok) != 0) {
646 warning("Error: expected '%s' but read '%s'",
653 static int test_type_token(enum event_type type, char *token,
654 enum event_type expect, const char *expect_tok)
656 return __test_type_token(type, token, expect, expect_tok, true);
659 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
661 enum event_type type;
664 type = read_token(tok);
666 type = read_token_item(tok);
667 return test_type(type, expect);
670 static int read_expect_type(enum event_type expect, char **tok)
672 return __read_expect_type(expect, tok, 1);
675 static int __read_expected(enum event_type expect, const char *str,
676 int newline_ok, bool warn)
678 enum event_type type;
683 type = read_token(&token);
685 type = read_token_item(&token);
687 ret = __test_type_token(type, token, expect, str, warn);
694 static int read_expected_warn(enum event_type expect, const char *str, bool warn)
696 return __read_expected(expect, str, 1, warn);
699 static int read_expected(enum event_type expect, const char *str)
701 return __read_expected(expect, str, 1, true);
704 static int read_expected_item(enum event_type expect, const char *str)
706 return __read_expected(expect, str, 0, true);
709 static char *event_read_name(void)
713 if (read_expected(EVENT_ITEM, "name") < 0)
716 if (read_expected(EVENT_OP, ":") < 0)
719 if (read_expect_type(EVENT_ITEM, &token) < 0)
729 static int event_read_id(void)
734 if (read_expected_item(EVENT_ITEM, "ID") < 0)
737 if (read_expected(EVENT_OP, ":") < 0)
740 if (read_expect_type(EVENT_ITEM, &token) < 0)
743 id = strtoul(token, NULL, 0);
752 static int field_is_string(struct format_field *field)
754 if ((field->flags & FIELD_IS_ARRAY) &&
755 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
756 !strstr(field->type, "s8")))
762 static int field_is_dynamic(struct format_field *field)
764 if (!strncmp(field->type, "__data_loc", 10))
770 static int event_read_fields(struct event *event, struct format_field **fields)
772 struct format_field *field = NULL;
773 enum event_type type;
779 type = read_token(&token);
780 if (type == EVENT_NEWLINE) {
787 if (test_type_token(type, token, EVENT_ITEM, "field"))
791 type = read_token(&token);
793 * The ftrace fields may still use the "special" name.
796 if (event->flags & EVENT_FL_ISFTRACE &&
797 type == EVENT_ITEM && strcmp(token, "special") == 0) {
799 type = read_token(&token);
802 if (test_type_token(type, token, EVENT_OP, ":") < 0)
805 if (read_expect_type(EVENT_ITEM, &token) < 0)
810 field = malloc_or_die(sizeof(*field));
811 memset(field, 0, sizeof(*field));
813 /* read the rest of the type */
815 type = read_token(&token);
816 if (type == EVENT_ITEM ||
817 (type == EVENT_OP && strcmp(token, "*") == 0) ||
819 * Some of the ftrace fields are broken and have
820 * an illegal "." in them.
822 (event->flags & EVENT_FL_ISFTRACE &&
823 type == EVENT_OP && strcmp(token, ".") == 0)) {
825 if (strcmp(token, "*") == 0)
826 field->flags |= FIELD_IS_POINTER;
829 field->type = realloc(field->type,
830 strlen(field->type) +
831 strlen(last_token) + 2);
832 strcat(field->type, " ");
833 strcat(field->type, last_token);
835 field->type = last_token;
844 die("no type found");
847 field->name = last_token;
849 if (test_type(type, EVENT_OP))
852 if (strcmp(token, "[") == 0) {
853 enum event_type last_type = type;
854 char *brackets = token;
857 field->flags |= FIELD_IS_ARRAY;
859 type = read_token(&token);
860 while (strcmp(token, "]") != 0) {
861 if (last_type == EVENT_ITEM &&
868 brackets = realloc(brackets,
870 strlen(token) + len);
872 strcat(brackets, " ");
873 strcat(brackets, token);
875 type = read_token(&token);
876 if (type == EVENT_NONE) {
877 die("failed to find token");
884 brackets = realloc(brackets, strlen(brackets) + 2);
885 strcat(brackets, "]");
887 /* add brackets to type */
889 type = read_token(&token);
891 * If the next token is not an OP, then it is of
892 * the format: type [] item;
894 if (type == EVENT_ITEM) {
895 field->type = realloc(field->type,
896 strlen(field->type) +
897 strlen(field->name) +
898 strlen(brackets) + 2);
899 strcat(field->type, " ");
900 strcat(field->type, field->name);
901 free_token(field->name);
902 strcat(field->type, brackets);
904 type = read_token(&token);
906 field->type = realloc(field->type,
907 strlen(field->type) +
908 strlen(brackets) + 1);
909 strcat(field->type, brackets);
914 if (field_is_string(field)) {
915 field->flags |= FIELD_IS_STRING;
916 if (field_is_dynamic(field))
917 field->flags |= FIELD_IS_DYNAMIC;
920 if (test_type_token(type, token, EVENT_OP, ";"))
924 if (read_expected(EVENT_ITEM, "offset") < 0)
927 if (read_expected(EVENT_OP, ":") < 0)
930 if (read_expect_type(EVENT_ITEM, &token))
932 field->offset = strtoul(token, NULL, 0);
935 if (read_expected(EVENT_OP, ";") < 0)
938 if (read_expected(EVENT_ITEM, "size") < 0)
941 if (read_expected(EVENT_OP, ":") < 0)
944 if (read_expect_type(EVENT_ITEM, &token))
946 field->size = strtoul(token, NULL, 0);
949 if (read_expected(EVENT_OP, ";") < 0)
952 type = read_token(&token);
953 if (type != EVENT_NEWLINE) {
954 /* newer versions of the kernel have a "signed" type */
955 if (test_type_token(type, token, EVENT_ITEM, "signed"))
960 if (read_expected(EVENT_OP, ":") < 0)
963 if (read_expect_type(EVENT_ITEM, &token))
966 if (strtoul(token, NULL, 0))
967 field->flags |= FIELD_IS_SIGNED;
970 if (read_expected(EVENT_OP, ";") < 0)
973 if (read_expect_type(EVENT_NEWLINE, &token))
980 fields = &field->next;
994 static int event_read_format(struct event *event)
999 if (read_expected_item(EVENT_ITEM, "format") < 0)
1002 if (read_expected(EVENT_OP, ":") < 0)
1005 if (read_expect_type(EVENT_NEWLINE, &token))
1009 ret = event_read_fields(event, &event->format.common_fields);
1012 event->format.nr_common = ret;
1014 ret = event_read_fields(event, &event->format.fields);
1017 event->format.nr_fields = ret;
1027 process_arg_token(struct event *event, struct print_arg *arg,
1028 char **tok, enum event_type type);
1030 static enum event_type
1031 process_arg(struct event *event, struct print_arg *arg, char **tok)
1033 enum event_type type;
1036 type = read_token(&token);
1039 return process_arg_token(event, arg, tok, type);
1042 static enum event_type
1043 process_cond(struct event *event, struct print_arg *top, char **tok)
1045 struct print_arg *arg, *left, *right;
1046 enum event_type type;
1049 arg = malloc_or_die(sizeof(*arg));
1050 memset(arg, 0, sizeof(*arg));
1052 left = malloc_or_die(sizeof(*left));
1054 right = malloc_or_die(sizeof(*right));
1056 arg->type = PRINT_OP;
1057 arg->op.left = left;
1058 arg->op.right = right;
1061 type = process_arg(event, left, &token);
1062 if (test_type_token(type, token, EVENT_OP, ":"))
1067 type = process_arg(event, right, &token);
1069 top->op.right = arg;
1082 static enum event_type
1083 process_array(struct event *event, struct print_arg *top, char **tok)
1085 struct print_arg *arg;
1086 enum event_type type;
1089 arg = malloc_or_die(sizeof(*arg));
1090 memset(arg, 0, sizeof(*arg));
1093 type = process_arg(event, arg, &token);
1094 if (test_type_token(type, token, EVENT_OP, "]"))
1097 top->op.right = arg;
1100 type = read_token_item(&token);
1111 static int get_op_prio(char *op)
1122 /* '>>' and '<<' are 8 */
1126 /* '==' and '!=' are 10 */
1136 die("unknown op '%c'", op[0]);
1140 if (strcmp(op, "++") == 0 ||
1141 strcmp(op, "--") == 0) {
1143 } else if (strcmp(op, ">>") == 0 ||
1144 strcmp(op, "<<") == 0) {
1146 } else if (strcmp(op, ">=") == 0 ||
1147 strcmp(op, "<=") == 0) {
1149 } else if (strcmp(op, "==") == 0 ||
1150 strcmp(op, "!=") == 0) {
1152 } else if (strcmp(op, "&&") == 0) {
1154 } else if (strcmp(op, "||") == 0) {
1157 die("unknown op '%s'", op);
1163 static void set_op_prio(struct print_arg *arg)
1166 /* single ops are the greatest */
1167 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1172 arg->op.prio = get_op_prio(arg->op.op);
1175 static enum event_type
1176 process_op(struct event *event, struct print_arg *arg, char **tok)
1178 struct print_arg *left, *right = NULL;
1179 enum event_type type;
1182 /* the op is passed in via tok */
1185 if (arg->type == PRINT_OP && !arg->op.left) {
1186 /* handle single op */
1188 die("bad op token %s", token);
1197 die("bad op token %s", token);
1201 /* make an empty left */
1202 left = malloc_or_die(sizeof(*left));
1203 left->type = PRINT_NULL;
1204 arg->op.left = left;
1206 right = malloc_or_die(sizeof(*right));
1207 arg->op.right = right;
1209 type = process_arg(event, right, tok);
1211 } else if (strcmp(token, "?") == 0) {
1213 left = malloc_or_die(sizeof(*left));
1214 /* copy the top arg to the left */
1217 arg->type = PRINT_OP;
1219 arg->op.left = left;
1222 type = process_cond(event, arg, tok);
1224 } else if (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 ||
1233 strcmp(token, "^") == 0 ||
1234 strcmp(token, "/") == 0 ||
1235 strcmp(token, "<") == 0 ||
1236 strcmp(token, ">") == 0 ||
1237 strcmp(token, "==") == 0 ||
1238 strcmp(token, "!=") == 0) {
1240 left = malloc_or_die(sizeof(*left));
1242 /* copy the top arg to the left */
1245 arg->type = PRINT_OP;
1247 arg->op.left = left;
1251 right = malloc_or_die(sizeof(*right));
1253 type = read_token_item(&token);
1256 /* could just be a type pointer */
1257 if ((strcmp(arg->op.op, "*") == 0) &&
1258 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1259 if (left->type != PRINT_ATOM)
1260 die("bad pointer type");
1261 left->atom.atom = realloc(left->atom.atom,
1262 sizeof(left->atom.atom) + 3);
1263 strcat(left->atom.atom, " *");
1270 type = process_arg_token(event, right, tok, type);
1272 arg->op.right = right;
1274 } else if (strcmp(token, "[") == 0) {
1276 left = malloc_or_die(sizeof(*left));
1279 arg->type = PRINT_OP;
1281 arg->op.left = left;
1284 type = process_array(event, arg, tok);
1287 warning("unknown op '%s'", token);
1288 event->flags |= EVENT_FL_FAILED;
1289 /* the arg is now the left side */
1293 if (type == EVENT_OP) {
1296 /* higher prios need to be closer to the root */
1297 prio = get_op_prio(*tok);
1299 if (prio > arg->op.prio)
1300 return process_op(event, arg, tok);
1302 return process_op(event, right, tok);
1308 static enum event_type
1309 process_entry(struct event *event __unused, struct print_arg *arg,
1312 enum event_type type;
1316 if (read_expected(EVENT_OP, "->") < 0)
1319 if (read_expect_type(EVENT_ITEM, &token) < 0)
1323 arg->type = PRINT_FIELD;
1324 arg->field.name = field;
1326 if (is_flag_field) {
1327 arg->field.field = find_any_field(event, arg->field.name);
1328 arg->field.field->flags |= FIELD_IS_FLAG;
1330 } else if (is_symbolic_field) {
1331 arg->field.field = find_any_field(event, arg->field.name);
1332 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1333 is_symbolic_field = 0;
1336 type = read_token(&token);
1346 static char *arg_eval (struct print_arg *arg);
1348 static long long arg_num_eval(struct print_arg *arg)
1350 long long left, right;
1353 switch (arg->type) {
1355 val = strtoll(arg->atom.atom, NULL, 0);
1358 val = arg_num_eval(arg->typecast.item);
1361 switch (arg->op.op[0]) {
1363 left = arg_num_eval(arg->op.left);
1364 right = arg_num_eval(arg->op.right);
1366 val = left || right;
1371 left = arg_num_eval(arg->op.left);
1372 right = arg_num_eval(arg->op.right);
1374 val = left && right;
1379 left = arg_num_eval(arg->op.left);
1380 right = arg_num_eval(arg->op.right);
1381 switch (arg->op.op[1]) {
1386 val = left << right;
1389 val = left <= right;
1392 die("unknown op '%s'", arg->op.op);
1396 left = arg_num_eval(arg->op.left);
1397 right = arg_num_eval(arg->op.right);
1398 switch (arg->op.op[1]) {
1403 val = left >> right;
1406 val = left >= right;
1409 die("unknown op '%s'", arg->op.op);
1413 left = arg_num_eval(arg->op.left);
1414 right = arg_num_eval(arg->op.right);
1416 if (arg->op.op[1] != '=')
1417 die("unknown op '%s'", arg->op.op);
1419 val = left == right;
1422 left = arg_num_eval(arg->op.left);
1423 right = arg_num_eval(arg->op.right);
1425 switch (arg->op.op[1]) {
1427 val = left != right;
1430 die("unknown op '%s'", arg->op.op);
1434 die("unknown op '%s'", arg->op.op);
1439 case PRINT_FIELD ... PRINT_SYMBOL:
1442 die("invalid eval type %d", arg->type);
1448 static char *arg_eval (struct print_arg *arg)
1451 static char buf[20];
1453 switch (arg->type) {
1455 return arg->atom.atom;
1457 return arg_eval(arg->typecast.item);
1459 val = arg_num_eval(arg);
1460 sprintf(buf, "%lld", val);
1464 case PRINT_FIELD ... PRINT_SYMBOL:
1467 die("invalid eval type %d", arg->type);
1474 static enum event_type
1475 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1477 enum event_type type;
1478 struct print_arg *arg = NULL;
1479 struct print_flag_sym *field;
1485 type = read_token_item(&token);
1486 if (test_type_token(type, token, EVENT_OP, "{"))
1489 arg = malloc_or_die(sizeof(*arg));
1492 type = process_arg(event, arg, &token);
1493 if (test_type_token(type, token, EVENT_DELIM, ","))
1496 field = malloc_or_die(sizeof(*field));
1497 memset(field, 0, sizeof(*field));
1499 value = arg_eval(arg);
1500 field->value = strdup(value);
1503 type = process_arg(event, arg, &token);
1504 if (test_type_token(type, token, EVENT_OP, "}"))
1507 value = arg_eval(arg);
1508 field->str = strdup(value);
1513 list = &field->next;
1516 type = read_token_item(&token);
1517 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1529 static enum event_type
1530 process_flags(struct event *event, struct print_arg *arg, char **tok)
1532 struct print_arg *field;
1533 enum event_type type;
1536 memset(arg, 0, sizeof(*arg));
1537 arg->type = PRINT_FLAGS;
1539 if (read_expected_item(EVENT_DELIM, "(") < 0)
1542 field = malloc_or_die(sizeof(*field));
1544 type = process_arg(event, field, &token);
1545 if (test_type_token(type, token, EVENT_DELIM, ","))
1548 arg->flags.field = field;
1550 type = read_token_item(&token);
1551 if (event_item_type(type)) {
1552 arg->flags.delim = token;
1553 type = read_token_item(&token);
1556 if (test_type_token(type, token, EVENT_DELIM, ","))
1559 type = process_fields(event, &arg->flags.flags, &token);
1560 if (test_type_token(type, token, EVENT_DELIM, ")"))
1564 type = read_token_item(tok);
1572 static enum event_type
1573 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1575 struct print_arg *field;
1576 enum event_type type;
1579 memset(arg, 0, sizeof(*arg));
1580 arg->type = PRINT_SYMBOL;
1582 if (read_expected_item(EVENT_DELIM, "(") < 0)
1585 field = malloc_or_die(sizeof(*field));
1587 type = process_arg(event, field, &token);
1588 if (test_type_token(type, token, EVENT_DELIM, ","))
1591 arg->symbol.field = field;
1593 type = process_fields(event, &arg->symbol.symbols, &token);
1594 if (test_type_token(type, token, EVENT_DELIM, ")"))
1598 type = read_token_item(tok);
1606 static enum event_type
1607 process_paren(struct event *event, struct print_arg *arg, char **tok)
1609 struct print_arg *item_arg;
1610 enum event_type type;
1613 type = process_arg(event, arg, &token);
1615 if (type == EVENT_ERROR)
1618 if (type == EVENT_OP)
1619 type = process_op(event, arg, &token);
1621 if (type == EVENT_ERROR)
1624 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1630 type = read_token_item(&token);
1633 * If the next token is an item or another open paren, then
1634 * this was a typecast.
1636 if (event_item_type(type) ||
1637 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1639 /* make this a typecast and contine */
1641 /* prevous must be an atom */
1642 if (arg->type != PRINT_ATOM)
1643 die("previous needed to be PRINT_ATOM");
1645 item_arg = malloc_or_die(sizeof(*item_arg));
1647 arg->type = PRINT_TYPE;
1648 arg->typecast.type = arg->atom.atom;
1649 arg->typecast.item = item_arg;
1650 type = process_arg_token(event, item_arg, &token, type);
1659 static enum event_type
1660 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1662 enum event_type type;
1665 if (read_expected(EVENT_DELIM, "(") < 0)
1668 if (read_expect_type(EVENT_ITEM, &token) < 0)
1671 arg->type = PRINT_STRING;
1672 arg->string.string = token;
1673 arg->string.offset = -1;
1675 if (read_expected(EVENT_DELIM, ")") < 0)
1678 type = read_token(&token);
1688 process_arg_token(struct event *event, struct print_arg *arg,
1689 char **tok, enum event_type type)
1698 if (strcmp(token, "REC") == 0) {
1700 type = process_entry(event, arg, &token);
1701 } else if (strcmp(token, "__print_flags") == 0) {
1704 type = process_flags(event, arg, &token);
1705 } else if (strcmp(token, "__print_symbolic") == 0) {
1707 is_symbolic_field = 1;
1708 type = process_symbols(event, arg, &token);
1709 } else if (strcmp(token, "__get_str") == 0) {
1711 type = process_str(event, arg, &token);
1714 /* test the next token */
1715 type = read_token_item(&token);
1717 /* atoms can be more than one token long */
1718 while (type == EVENT_ITEM) {
1719 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1721 strcat(atom, token);
1723 type = read_token_item(&token);
1726 /* todo, test for function */
1728 arg->type = PRINT_ATOM;
1729 arg->atom.atom = atom;
1734 arg->type = PRINT_ATOM;
1735 arg->atom.atom = token;
1736 type = read_token_item(&token);
1739 if (strcmp(token, "(") == 0) {
1741 type = process_paren(event, arg, &token);
1745 /* handle single ops */
1746 arg->type = PRINT_OP;
1748 arg->op.left = NULL;
1749 type = process_op(event, arg, &token);
1753 case EVENT_ERROR ... EVENT_NEWLINE:
1755 die("unexpected type %d", type);
1762 static int event_read_print_args(struct event *event, struct print_arg **list)
1764 enum event_type type = EVENT_ERROR;
1765 struct print_arg *arg;
1770 if (type == EVENT_NEWLINE) {
1772 type = read_token_item(&token);
1776 arg = malloc_or_die(sizeof(*arg));
1777 memset(arg, 0, sizeof(*arg));
1779 type = process_arg(event, arg, &token);
1781 if (type == EVENT_ERROR) {
1789 if (type == EVENT_OP) {
1790 type = process_op(event, arg, &token);
1795 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1802 } while (type != EVENT_NONE);
1804 if (type != EVENT_NONE)
1810 static int event_read_print(struct event *event)
1812 enum event_type type;
1816 if (read_expected_item(EVENT_ITEM, "print") < 0)
1819 if (read_expected(EVENT_ITEM, "fmt") < 0)
1822 if (read_expected(EVENT_OP, ":") < 0)
1825 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1829 event->print_fmt.format = token;
1830 event->print_fmt.args = NULL;
1832 /* ok to have no arg */
1833 type = read_token_item(&token);
1835 if (type == EVENT_NONE)
1838 /* Handle concatination of print lines */
1839 if (type == EVENT_DQUOTE) {
1842 cat = malloc_or_die(strlen(event->print_fmt.format) +
1844 strcpy(cat, event->print_fmt.format);
1847 free_token(event->print_fmt.format);
1848 event->print_fmt.format = NULL;
1853 if (test_type_token(type, token, EVENT_DELIM, ","))
1858 ret = event_read_print_args(event, &event->print_fmt.args);
1869 static struct format_field *
1870 find_common_field(struct event *event, const char *name)
1872 struct format_field *format;
1874 for (format = event->format.common_fields;
1875 format; format = format->next) {
1876 if (strcmp(format->name, name) == 0)
1883 static struct format_field *
1884 find_field(struct event *event, const char *name)
1886 struct format_field *format;
1888 for (format = event->format.fields;
1889 format; format = format->next) {
1890 if (strcmp(format->name, name) == 0)
1897 static struct format_field *
1898 find_any_field(struct event *event, const char *name)
1900 struct format_field *format;
1902 format = find_common_field(event, name);
1905 return find_field(event, name);
1908 unsigned long long read_size(void *ptr, int size)
1912 return *(unsigned char *)ptr;
1914 return data2host2(ptr);
1916 return data2host4(ptr);
1918 return data2host8(ptr);
1926 raw_field_value(struct event *event, const char *name, void *data)
1928 struct format_field *field;
1930 field = find_any_field(event, name);
1934 return read_size(data + field->offset, field->size);
1937 void *raw_field_ptr(struct event *event, const char *name, void *data)
1939 struct format_field *field;
1941 field = find_any_field(event, name);
1945 if (field->flags & FIELD_IS_STRING) {
1948 offset = *(int *)(data + field->offset);
1951 return data + offset;
1954 return data + field->offset;
1957 static int get_common_info(const char *type, int *offset, int *size)
1959 struct event *event;
1960 struct format_field *field;
1963 * All events should have the same common elements.
1964 * Pick any event to find where the type is;
1967 die("no event_list!");
1970 field = find_common_field(event, type);
1972 die("field '%s' not found", type);
1974 *offset = field->offset;
1975 *size = field->size;
1980 static int __parse_common(void *data, int *size, int *offset,
1986 ret = get_common_info(name, offset, size);
1990 return read_size(data + *offset, *size);
1993 int trace_parse_common_type(void *data)
1995 static int type_offset;
1996 static int type_size;
1998 return __parse_common(data, &type_size, &type_offset,
2002 int trace_parse_common_pid(void *data)
2004 static int pid_offset;
2005 static int pid_size;
2007 return __parse_common(data, &pid_size, &pid_offset,
2011 int parse_common_pc(void *data)
2013 static int pc_offset;
2016 return __parse_common(data, &pc_size, &pc_offset,
2017 "common_preempt_count");
2020 int parse_common_flags(void *data)
2022 static int flags_offset;
2023 static int flags_size;
2025 return __parse_common(data, &flags_size, &flags_offset,
2029 int parse_common_lock_depth(void *data)
2031 static int ld_offset;
2035 ret = __parse_common(data, &ld_size, &ld_offset,
2036 "common_lock_depth");
2043 struct event *trace_find_event(int id)
2045 struct event *event;
2047 for (event = event_list; event; event = event->next) {
2048 if (event->id == id)
2054 struct event *trace_find_next_event(struct event *event)
2062 static unsigned long long eval_num_arg(void *data, int size,
2063 struct event *event, struct print_arg *arg)
2065 unsigned long long val = 0;
2066 unsigned long long left, right;
2067 struct print_arg *larg;
2069 switch (arg->type) {
2074 return strtoull(arg->atom.atom, NULL, 0);
2076 if (!arg->field.field) {
2077 arg->field.field = find_any_field(event, arg->field.name);
2078 if (!arg->field.field)
2079 die("field %s not found", arg->field.name);
2081 /* must be a number */
2082 val = read_size(data + arg->field.field->offset,
2083 arg->field.field->size);
2089 return eval_num_arg(data, size, event, arg->typecast.item);
2094 if (strcmp(arg->op.op, "[") == 0) {
2096 * Arrays are special, since we don't want
2097 * to read the arg as is.
2099 if (arg->op.left->type != PRINT_FIELD)
2100 goto default_op; /* oops, all bets off */
2101 larg = arg->op.left;
2102 if (!larg->field.field) {
2104 find_any_field(event, larg->field.name);
2105 if (!larg->field.field)
2106 die("field %s not found", larg->field.name);
2108 right = eval_num_arg(data, size, event, arg->op.right);
2109 val = read_size(data + larg->field.field->offset +
2110 right * long_size, long_size);
2114 left = eval_num_arg(data, size, event, arg->op.left);
2115 right = eval_num_arg(data, size, event, arg->op.right);
2116 switch (arg->op.op[0]) {
2119 val = left || right;
2125 val = left && right;
2130 switch (arg->op.op[1]) {
2135 val = left << right;
2138 val = left <= right;
2141 die("unknown op '%s'", arg->op.op);
2145 switch (arg->op.op[1]) {
2150 val = left >> right;
2153 val = left >= right;
2156 die("unknown op '%s'", arg->op.op);
2160 if (arg->op.op[1] != '=')
2161 die("unknown op '%s'", arg->op.op);
2162 val = left == right;
2171 die("unknown op '%s'", arg->op.op);
2174 default: /* not sure what to do there */
2182 unsigned long long value;
2185 static const struct flag flags[] = {
2186 { "HI_SOFTIRQ", 0 },
2187 { "TIMER_SOFTIRQ", 1 },
2188 { "NET_TX_SOFTIRQ", 2 },
2189 { "NET_RX_SOFTIRQ", 3 },
2190 { "BLOCK_SOFTIRQ", 4 },
2191 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2192 { "TASKLET_SOFTIRQ", 6 },
2193 { "SCHED_SOFTIRQ", 7 },
2194 { "HRTIMER_SOFTIRQ", 8 },
2195 { "RCU_SOFTIRQ", 9 },
2197 { "HRTIMER_NORESTART", 0 },
2198 { "HRTIMER_RESTART", 1 },
2201 unsigned long long eval_flag(const char *flag)
2206 * Some flags in the format files do not get converted.
2207 * If the flag is not numeric, see if it is something that
2208 * we already know about.
2210 if (isdigit(flag[0]))
2211 return strtoull(flag, NULL, 0);
2213 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2214 if (strcmp(flags[i].name, flag) == 0)
2215 return flags[i].value;
2220 static void print_str_arg(void *data, int size,
2221 struct event *event, struct print_arg *arg)
2223 struct print_flag_sym *flag;
2224 unsigned long long val, fval;
2228 switch (arg->type) {
2233 printf("%s", arg->atom.atom);
2236 if (!arg->field.field) {
2237 arg->field.field = find_any_field(event, arg->field.name);
2238 if (!arg->field.field)
2239 die("field %s not found", arg->field.name);
2241 str = malloc_or_die(arg->field.field->size + 1);
2242 memcpy(str, data + arg->field.field->offset,
2243 arg->field.field->size);
2244 str[arg->field.field->size] = 0;
2249 val = eval_num_arg(data, size, event, arg->flags.field);
2251 for (flag = arg->flags.flags; flag; flag = flag->next) {
2252 fval = eval_flag(flag->value);
2253 if (!val && !fval) {
2254 printf("%s", flag->str);
2257 if (fval && (val & fval) == fval) {
2258 if (print && arg->flags.delim)
2259 printf("%s", arg->flags.delim);
2260 printf("%s", flag->str);
2267 val = eval_num_arg(data, size, event, arg->symbol.field);
2268 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2269 fval = eval_flag(flag->value);
2271 printf("%s", flag->str);
2279 case PRINT_STRING: {
2282 if (arg->string.offset == -1) {
2283 struct format_field *f;
2285 f = find_any_field(event, arg->string.string);
2286 arg->string.offset = f->offset;
2288 str_offset = *(int *)(data + arg->string.offset);
2289 str_offset &= 0xffff;
2290 printf("%s", ((char *)data) + str_offset);
2295 * The only op for string should be ? :
2297 if (arg->op.op[0] != '?')
2299 val = eval_num_arg(data, size, event, arg->op.left);
2301 print_str_arg(data, size, event, arg->op.right->op.left);
2303 print_str_arg(data, size, event, arg->op.right->op.right);
2311 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2313 static struct format_field *field, *ip_field;
2314 struct print_arg *args, *arg, **next;
2315 unsigned long long ip, val;
2320 field = find_field(event, "buf");
2322 die("can't find buffer field for binary printk");
2323 ip_field = find_field(event, "ip");
2325 die("can't find ip field for binary printk");
2328 ip = read_size(data + ip_field->offset, ip_field->size);
2331 * The first arg is the IP pointer.
2333 args = malloc_or_die(sizeof(*args));
2338 arg->type = PRINT_ATOM;
2339 arg->atom.atom = malloc_or_die(32);
2340 sprintf(arg->atom.atom, "%lld", ip);
2342 /* skip the first "%pf : " */
2343 for (ptr = fmt + 6, bptr = data + field->offset;
2344 bptr < data + size && *ptr; ptr++) {
2368 /* the pointers are always 4 bytes aligned */
2369 bptr = (void *)(((unsigned long)bptr + 3) &
2381 val = read_size(bptr, ls);
2383 arg = malloc_or_die(sizeof(*arg));
2385 arg->type = PRINT_ATOM;
2386 arg->atom.atom = malloc_or_die(32);
2387 sprintf(arg->atom.atom, "%lld", val);
2392 arg = malloc_or_die(sizeof(*arg));
2394 arg->type = PRINT_STRING;
2395 arg->string.string = strdup(bptr);
2396 bptr += strlen(bptr) + 1;
2408 static void free_args(struct print_arg *args)
2410 struct print_arg *next;
2415 if (args->type == PRINT_ATOM)
2416 free(args->atom.atom);
2418 free(args->string.string);
2424 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2426 unsigned long long addr;
2427 static struct format_field *field;
2428 struct printk_map *printk;
2433 field = find_field(event, "fmt");
2435 die("can't find format field for binary printk");
2436 printf("field->offset = %d size=%d\n", field->offset, field->size);
2439 addr = read_size(data + field->offset, field->size);
2441 printk = find_printk(addr);
2443 format = malloc_or_die(45);
2444 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2450 /* Remove any quotes. */
2453 format = malloc_or_die(strlen(p) + 10);
2454 sprintf(format, "%s : %s", "%pf", p);
2455 /* remove ending quotes and new line since we will add one too */
2456 p = format + strlen(format) - 1;
2461 if (strcmp(p, "\\n") == 0)
2467 static void pretty_print(void *data, int size, struct event *event)
2469 struct print_fmt *print_fmt = &event->print_fmt;
2470 struct print_arg *arg = print_fmt->args;
2471 struct print_arg *args = NULL;
2472 const char *ptr = print_fmt->format;
2473 unsigned long long val;
2474 struct func_map *func;
2475 const char *saveptr;
2476 char *bprint_fmt = NULL;
2482 if (event->flags & EVENT_FL_ISFUNC)
2483 ptr = " %pF <-- %pF";
2485 if (event->flags & EVENT_FL_ISBPRINT) {
2486 bprint_fmt = get_bprint_format(data, size, event);
2487 args = make_bprint_args(bprint_fmt, data, size, event);
2492 for (; *ptr; ptr++) {
2514 } else if (*ptr == '%') {
2539 if (*(ptr+1) == 'F' ||
2552 die("no argument match");
2554 len = ((unsigned long)ptr + 1) -
2555 (unsigned long)saveptr;
2557 /* should never happen */
2561 memcpy(format, saveptr, len);
2564 val = eval_num_arg(data, size, event, arg);
2568 func = find_func(val);
2570 printf("%s", func->func);
2571 if (show_func == 'F')
2579 printf(format, (int)val);
2582 printf(format, (long)val);
2585 printf(format, (long long)val);
2588 die("bad count (%d)", ls);
2593 die("no matching argument");
2595 print_str_arg(data, size, event, arg);
2599 printf(">%c<", *ptr);
2612 static inline int log10_cpu(int nb)
2621 static void print_lat_fmt(void *data, int size __unused)
2623 unsigned int lat_flags;
2629 lat_flags = parse_common_flags(data);
2630 pc = parse_common_pc(data);
2631 lock_depth = parse_common_lock_depth(data);
2633 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2634 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2637 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2638 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2640 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2642 (hardirq && softirq) ? 'H' :
2643 hardirq ? 'h' : softirq ? 's' : '.');
2653 printf("%d", lock_depth);
2656 /* taken from Linux, written by Frederic Weisbecker */
2657 static void print_graph_cpu(int cpu)
2660 int log10_this = log10_cpu(cpu);
2661 int log10_all = log10_cpu(cpus);
2665 * Start with a space character - to make it stand out
2666 * to the right a bit when trace output is pasted into
2672 * Tricky - we space the CPU field according to the max
2673 * number of online CPUs. On a 2-cpu system it would take
2674 * a maximum of 1 digit - on a 128 cpu system it would
2675 * take up to 3 digits:
2677 for (i = 0; i < log10_all - log10_this; i++)
2680 printf("%d) ", cpu);
2683 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2684 #define TRACE_GRAPH_INDENT 2
2686 static void print_graph_proc(int pid, const char *comm)
2688 /* sign + log10(MAX_INT) + '\0' */
2694 sprintf(pid_str, "%d", pid);
2696 /* 1 stands for the "-" character */
2697 len = strlen(comm) + strlen(pid_str) + 1;
2699 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2700 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2702 /* First spaces to align center */
2703 for (i = 0; i < spaces / 2; i++)
2706 printf("%s-%s", comm, pid_str);
2708 /* Last spaces to align center */
2709 for (i = 0; i < spaces - (spaces / 2); i++)
2713 static struct record *
2714 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2715 struct record *next)
2717 struct format_field *field;
2718 struct event *event;
2723 type = trace_parse_common_type(next->data);
2724 event = trace_find_event(type);
2728 if (!(event->flags & EVENT_FL_ISFUNCRET))
2731 pid = trace_parse_common_pid(next->data);
2732 field = find_field(event, "func");
2734 die("function return does not have field func");
2736 val = read_size(next->data + field->offset, field->size);
2738 if (cur_pid != pid || cur_func != val)
2741 /* this is a leaf, now advance the iterator */
2742 return trace_read_data(cpu);
2745 /* Signal a overhead of time execution to the output */
2746 static void print_graph_overhead(unsigned long long duration)
2748 /* Non nested entry or return */
2749 if (duration == ~0ULL)
2750 return (void)printf(" ");
2752 /* Duration exceeded 100 msecs */
2753 if (duration > 100000ULL)
2754 return (void)printf("! ");
2756 /* Duration exceeded 10 msecs */
2757 if (duration > 10000ULL)
2758 return (void)printf("+ ");
2763 static void print_graph_duration(unsigned long long duration)
2765 unsigned long usecs = duration / 1000;
2766 unsigned long nsecs_rem = duration % 1000;
2767 /* log10(ULONG_MAX) + '\0' */
2773 sprintf(msecs_str, "%lu", usecs);
2776 len = printf("%lu", usecs);
2778 /* Print nsecs (we don't want to exceed 7 numbers) */
2780 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2781 len += printf(".%s", nsecs_str);
2786 /* Print remaining spaces to fit the row's width */
2787 for (i = len; i < 7; i++)
2794 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2796 unsigned long long rettime, calltime;
2797 unsigned long long duration, depth;
2798 unsigned long long val;
2799 struct format_field *field;
2800 struct func_map *func;
2801 struct event *ret_event;
2805 type = trace_parse_common_type(ret_rec->data);
2806 ret_event = trace_find_event(type);
2808 field = find_field(ret_event, "rettime");
2810 die("can't find rettime in return graph");
2811 rettime = read_size(ret_rec->data + field->offset, field->size);
2813 field = find_field(ret_event, "calltime");
2815 die("can't find rettime in return graph");
2816 calltime = read_size(ret_rec->data + field->offset, field->size);
2818 duration = rettime - calltime;
2821 print_graph_overhead(duration);
2824 print_graph_duration(duration);
2826 field = find_field(event, "depth");
2828 die("can't find depth in entry graph");
2829 depth = read_size(data + field->offset, field->size);
2832 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2835 field = find_field(event, "func");
2837 die("can't find func in entry graph");
2838 val = read_size(data + field->offset, field->size);
2839 func = find_func(val);
2842 printf("%s();", func->func);
2844 printf("%llx();", val);
2847 static void print_graph_nested(struct event *event, void *data)
2849 struct format_field *field;
2850 unsigned long long depth;
2851 unsigned long long val;
2852 struct func_map *func;
2856 print_graph_overhead(-1);
2861 field = find_field(event, "depth");
2863 die("can't find depth in entry graph");
2864 depth = read_size(data + field->offset, field->size);
2867 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2870 field = find_field(event, "func");
2872 die("can't find func in entry graph");
2873 val = read_size(data + field->offset, field->size);
2874 func = find_func(val);
2877 printf("%s() {", func->func);
2879 printf("%llx() {", val);
2883 pretty_print_func_ent(void *data, int size, struct event *event,
2884 int cpu, int pid, const char *comm,
2885 unsigned long secs, unsigned long usecs)
2887 struct format_field *field;
2892 printf("%5lu.%06lu | ", secs, usecs);
2894 print_graph_cpu(cpu);
2895 print_graph_proc(pid, comm);
2899 if (latency_format) {
2900 print_lat_fmt(data, size);
2904 field = find_field(event, "func");
2906 die("function entry does not have func field");
2908 val = read_size(data + field->offset, field->size);
2911 * peek_data may unmap the data pointer. Copy it first.
2913 copy_data = malloc_or_die(size);
2914 memcpy(copy_data, data, size);
2917 rec = trace_peek_data(cpu);
2919 rec = get_return_for_leaf(cpu, pid, val, rec);
2921 print_graph_entry_leaf(event, data, rec);
2925 print_graph_nested(event, data);
2931 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2932 int cpu, int pid, const char *comm,
2933 unsigned long secs, unsigned long usecs)
2935 unsigned long long rettime, calltime;
2936 unsigned long long duration, depth;
2937 struct format_field *field;
2940 printf("%5lu.%06lu | ", secs, usecs);
2942 print_graph_cpu(cpu);
2943 print_graph_proc(pid, comm);
2947 if (latency_format) {
2948 print_lat_fmt(data, size);
2952 field = find_field(event, "rettime");
2954 die("can't find rettime in return graph");
2955 rettime = read_size(data + field->offset, field->size);
2957 field = find_field(event, "calltime");
2959 die("can't find calltime in return graph");
2960 calltime = read_size(data + field->offset, field->size);
2962 duration = rettime - calltime;
2965 print_graph_overhead(duration);
2968 print_graph_duration(duration);
2970 field = find_field(event, "depth");
2972 die("can't find depth in entry graph");
2973 depth = read_size(data + field->offset, field->size);
2976 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2983 pretty_print_func_graph(void *data, int size, struct event *event,
2984 int cpu, int pid, const char *comm,
2985 unsigned long secs, unsigned long usecs)
2987 if (event->flags & EVENT_FL_ISFUNCENT)
2988 pretty_print_func_ent(data, size, event,
2989 cpu, pid, comm, secs, usecs);
2990 else if (event->flags & EVENT_FL_ISFUNCRET)
2991 pretty_print_func_ret(data, size, event,
2992 cpu, pid, comm, secs, usecs);
2996 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2999 struct event *event;
3001 unsigned long usecs;
3005 secs = nsecs / NSECS_PER_SEC;
3006 nsecs -= secs * NSECS_PER_SEC;
3007 usecs = nsecs / NSECS_PER_USEC;
3009 type = trace_parse_common_type(data);
3011 event = trace_find_event(type);
3013 warning("ug! no event found for type %d", type);
3017 pid = trace_parse_common_pid(data);
3019 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
3020 return pretty_print_func_graph(data, size, event, cpu,
3021 pid, comm, secs, usecs);
3023 if (latency_format) {
3024 printf("%8.8s-%-5d %3d",
3026 print_lat_fmt(data, size);
3028 printf("%16s-%-5d [%03d]", comm, pid, cpu);
3030 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name);
3032 if (event->flags & EVENT_FL_FAILED) {
3033 printf("EVENT '%s' FAILED TO PARSE\n",
3038 pretty_print(data, size, event);
3042 static void print_fields(struct print_flag_sym *field)
3044 printf("{ %s, %s }", field->value, field->str);
3047 print_fields(field->next);
3051 static void print_args(struct print_arg *args)
3053 int print_paren = 1;
3055 switch (args->type) {
3060 printf("%s", args->atom.atom);
3063 printf("REC->%s", args->field.name);
3066 printf("__print_flags(");
3067 print_args(args->flags.field);
3068 printf(", %s, ", args->flags.delim);
3069 print_fields(args->flags.flags);
3073 printf("__print_symbolic(");
3074 print_args(args->symbol.field);
3076 print_fields(args->symbol.symbols);
3080 printf("__get_str(%s)", args->string.string);
3083 printf("(%s)", args->typecast.type);
3084 print_args(args->typecast.item);
3087 if (strcmp(args->op.op, ":") == 0)
3091 print_args(args->op.left);
3092 printf(" %s ", args->op.op);
3093 print_args(args->op.right);
3098 /* we should warn... */
3103 print_args(args->next);
3107 static void parse_header_field(const char *field,
3108 int *offset, int *size, bool warn)
3113 if (read_expected(EVENT_ITEM, "field") < 0)
3115 if (read_expected(EVENT_OP, ":") < 0)
3119 if (read_expect_type(EVENT_ITEM, &token) < 0)
3123 if (read_expected_warn(EVENT_ITEM, field, warn) < 0)
3125 if (read_expected(EVENT_OP, ";") < 0)
3127 if (read_expected(EVENT_ITEM, "offset") < 0)
3129 if (read_expected(EVENT_OP, ":") < 0)
3131 if (read_expect_type(EVENT_ITEM, &token) < 0)
3133 *offset = atoi(token);
3135 if (read_expected(EVENT_OP, ";") < 0)
3137 if (read_expected(EVENT_ITEM, "size") < 0)
3139 if (read_expected(EVENT_OP, ":") < 0)
3141 if (read_expect_type(EVENT_ITEM, &token) < 0)
3143 *size = atoi(token);
3145 if (read_expected(EVENT_OP, ";") < 0)
3147 type = read_token(&token);
3148 if (type != EVENT_NEWLINE) {
3149 /* newer versions of the kernel have a "signed" type */
3150 if (type != EVENT_ITEM)
3153 if (strcmp(token, "signed") != 0)
3158 if (read_expected(EVENT_OP, ":") < 0)
3161 if (read_expect_type(EVENT_ITEM, &token))
3165 if (read_expected(EVENT_OP, ";") < 0)
3168 if (read_expect_type(EVENT_NEWLINE, &token))
3175 int parse_header_page(char *buf, unsigned long size)
3177 init_input_buf(buf, size);
3179 parse_header_field("timestamp", &header_page_ts_offset,
3180 &header_page_ts_size, true);
3181 parse_header_field("commit", &header_page_size_offset,
3182 &header_page_size_size, true);
3183 parse_header_field("overwrite", &header_page_overwrite_offset,
3184 &header_page_overwrite_size, false);
3185 parse_header_field("data", &header_page_data_offset,
3186 &header_page_data_size, true);
3191 int parse_ftrace_file(char *buf, unsigned long size)
3193 struct format_field *field;
3194 struct print_arg *arg, **list;
3195 struct event *event;
3198 init_input_buf(buf, size);
3200 event = alloc_event();
3204 event->flags |= EVENT_FL_ISFTRACE;
3206 event->name = event_read_name();
3208 die("failed to read ftrace event name");
3210 if (strcmp(event->name, "function") == 0)
3211 event->flags |= EVENT_FL_ISFUNC;
3213 else if (strcmp(event->name, "funcgraph_entry") == 0)
3214 event->flags |= EVENT_FL_ISFUNCENT;
3216 else if (strcmp(event->name, "funcgraph_exit") == 0)
3217 event->flags |= EVENT_FL_ISFUNCRET;
3219 else if (strcmp(event->name, "bprint") == 0)
3220 event->flags |= EVENT_FL_ISBPRINT;
3222 event->id = event_read_id();
3224 die("failed to read ftrace event id");
3228 ret = event_read_format(event);
3230 die("failed to read ftrace event format");
3232 ret = event_read_print(event);
3234 die("failed to read ftrace event print fmt");
3236 /* New ftrace handles args */
3240 * The arguments for ftrace files are parsed by the fields.
3241 * Set up the fields as their arguments.
3243 list = &event->print_fmt.args;
3244 for (field = event->format.fields; field; field = field->next) {
3245 arg = malloc_or_die(sizeof(*arg));
3246 memset(arg, 0, sizeof(*arg));
3249 arg->type = PRINT_FIELD;
3250 arg->field.name = field->name;
3251 arg->field.field = field;
3256 int parse_event_file(char *buf, unsigned long size, char *sys)
3258 struct event *event;
3261 init_input_buf(buf, size);
3263 event = alloc_event();
3267 event->name = event_read_name();
3269 die("failed to read event name");
3271 event->id = event_read_id();
3273 die("failed to read event id");
3275 ret = event_read_format(event);
3277 warning("failed to read event format for %s", event->name);
3281 ret = event_read_print(event);
3283 warning("failed to read event print fmt for %s", event->name);
3287 event->system = strdup(sys);
3289 #define PRINT_ARGS 0
3290 if (PRINT_ARGS && event->print_fmt.args)
3291 print_args(event->print_fmt.args);
3297 event->flags |= EVENT_FL_FAILED;
3298 /* still add it even if it failed */
3303 void parse_set_info(int nr_cpus, int long_sz)
3306 long_size = long_sz;
3309 int common_pc(struct scripting_context *context)
3311 return parse_common_pc(context->event_data);
3314 int common_flags(struct scripting_context *context)
3316 return parse_common_flags(context->event_data);
3319 int common_lock_depth(struct scripting_context *context)
3321 return parse_common_lock_depth(context->event_data);