2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
34 #include "trace-event.h"
36 int header_page_ts_offset;
37 int header_page_ts_size;
38 int header_page_size_offset;
39 int header_page_size_size;
40 int header_page_data_offset;
41 int header_page_data_size;
45 static char *input_buf;
46 static unsigned long long input_buf_ptr;
47 static unsigned long long input_buf_siz;
52 static void init_input_buf(char *buf, unsigned long long size)
64 static struct cmdline *cmdlines;
65 static int cmdline_count;
67 static int cmdline_cmp(const void *a, const void *b)
69 const struct cmdline *ca = a;
70 const struct cmdline *cb = b;
72 if (ca->pid < cb->pid)
74 if (ca->pid > cb->pid)
80 void parse_cmdlines(char *file, int size __unused)
83 struct cmdline_list *next;
86 } *list = NULL, *item;
91 line = strtok_r(file, "\n", &next);
93 item = malloc_or_die(sizeof(*item));
94 sscanf(line, "%d %as", &item->pid,
95 (float *)(void *)&item->comm); /* workaround gcc warning */
98 line = strtok_r(NULL, "\n", &next);
102 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
106 cmdlines[i].pid = list->pid;
107 cmdlines[i].comm = list->comm;
114 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
117 static struct func_map {
118 unsigned long long addr;
122 static unsigned int func_count;
124 static int func_cmp(const void *a, const void *b)
126 const struct func_map *fa = a;
127 const struct func_map *fb = b;
129 if (fa->addr < fb->addr)
131 if (fa->addr > fb->addr)
137 void parse_proc_kallsyms(char *file, unsigned int size __unused)
140 struct func_list *next;
141 unsigned long long addr;
144 } *list = NULL, *item;
152 line = strtok_r(file, "\n", &next);
154 item = malloc_or_die(sizeof(*item));
156 ret = sscanf(line, "%as %c %as\t[%as",
157 (float *)(void *)&addr_str, /* workaround gcc warning */
159 (float *)(void *)&item->func,
160 (float *)(void *)&item->mod);
161 item->addr = strtoull(addr_str, NULL, 16);
164 /* truncate the extra ']' */
166 item->mod[strlen(item->mod) - 1] = 0;
171 line = strtok_r(NULL, "\n", &next);
175 func_list = malloc_or_die(sizeof(*func_list) * func_count + 1);
179 func_list[i].func = list->func;
180 func_list[i].addr = list->addr;
181 func_list[i].mod = list->mod;
188 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
191 * Add a special record at the end.
193 func_list[func_count].func = NULL;
194 func_list[func_count].addr = 0;
195 func_list[func_count].mod = NULL;
199 * We are searching for a record in between, not an exact
202 static int func_bcmp(const void *a, const void *b)
204 const struct func_map *fa = a;
205 const struct func_map *fb = b;
207 if ((fa->addr == fb->addr) ||
209 (fa->addr > fb->addr &&
210 fa->addr < (fb+1)->addr))
213 if (fa->addr < fb->addr)
219 static struct func_map *find_func(unsigned long long addr)
221 struct func_map *func;
226 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
232 void print_funcs(void)
236 for (i = 0; i < (int)func_count; i++) {
240 if (func_list[i].mod)
241 printf(" [%s]\n", func_list[i].mod);
247 static struct printk_map {
248 unsigned long long addr;
251 static unsigned int printk_count;
253 static int printk_cmp(const void *a, const void *b)
255 const struct func_map *fa = a;
256 const struct func_map *fb = b;
258 if (fa->addr < fb->addr)
260 if (fa->addr > fb->addr)
266 static struct printk_map *find_printk(unsigned long long addr)
268 struct printk_map *printk;
269 struct printk_map key;
273 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
279 void parse_ftrace_printk(char *file, unsigned int size __unused)
282 struct printk_list *next;
283 unsigned long long addr;
285 } *list = NULL, *item;
292 line = strtok_r(file, "\n", &next);
294 item = malloc_or_die(sizeof(*item));
295 addr_str = strtok_r(line, ":", &fmt);
296 item->addr = strtoull(addr_str, NULL, 16);
297 /* fmt still has a space, skip it */
298 item->printk = strdup(fmt+1);
301 line = strtok_r(NULL, "\n", &next);
305 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
309 printk_list[i].printk = list->printk;
310 printk_list[i].addr = list->addr;
317 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
320 void print_printk(void)
324 for (i = 0; i < (int)printk_count; i++) {
325 printf("%016llx %s\n",
327 printk_list[i].printk);
331 static struct event *alloc_event(void)
335 event = malloc_or_die(sizeof(*event));
336 memset(event, 0, sizeof(*event));
353 static struct event *event_list;
355 static void add_event(struct event *event)
357 event->next = event_list;
361 static int event_item_type(enum event_type type)
364 case EVENT_ITEM ... EVENT_SQUOTE:
366 case EVENT_ERROR ... EVENT_DELIM:
372 static void free_arg(struct print_arg *arg)
380 free(arg->atom.atom);
383 case PRINT_FIELD ... PRINT_OP:
392 static enum event_type get_type(int ch)
395 return EVENT_NEWLINE;
398 if (isalnum(ch) || ch == '_')
406 if (ch == '(' || ch == ')' || ch == ',')
412 static int __read_char(void)
414 if (input_buf_ptr >= input_buf_siz)
417 return input_buf[input_buf_ptr++];
420 static int __peek_char(void)
422 if (input_buf_ptr >= input_buf_siz)
425 return input_buf[input_buf_ptr];
428 static enum event_type __read_token(char **tok)
431 int ch, last_ch, quote_ch, next_ch;
434 enum event_type type;
444 if (type == EVENT_NONE)
452 *tok = malloc_or_die(2);
460 next_ch = __peek_char();
461 if (next_ch == '>') {
462 buf[i++] = __read_char();
475 buf[i++] = __read_char();
487 default: /* what should we do instead? */
497 buf[i++] = __read_char();
502 /* don't keep quotes */
507 if (i == (BUFSIZ - 1)) {
510 *tok = realloc(*tok, tok_size + BUFSIZ);
525 /* the '\' '\' will cancel itself */
526 if (ch == '\\' && last_ch == '\\')
528 } while (ch != quote_ch || last_ch == '\\');
529 /* remove the last quote */
533 case EVENT_ERROR ... EVENT_SPACE:
539 while (get_type(__peek_char()) == type) {
540 if (i == (BUFSIZ - 1)) {
543 *tok = realloc(*tok, tok_size + BUFSIZ);
562 *tok = realloc(*tok, tok_size + i);
574 static void free_token(char *tok)
580 static enum event_type read_token(char **tok)
582 enum event_type type;
585 type = __read_token(tok);
586 if (type != EVENT_SPACE)
597 static enum event_type read_token_item(char **tok)
599 enum event_type type;
602 type = __read_token(tok);
603 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
613 static int test_type(enum event_type type, enum event_type expect)
615 if (type != expect) {
616 warning("Error: expected type %d but read %d",
623 static int test_type_token(enum event_type type, char *token,
624 enum event_type expect, const char *expect_tok)
626 if (type != expect) {
627 warning("Error: expected type %d but read %d",
632 if (strcmp(token, expect_tok) != 0) {
633 warning("Error: expected '%s' but read '%s'",
640 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
642 enum event_type type;
645 type = read_token(tok);
647 type = read_token_item(tok);
648 return test_type(type, expect);
651 static int read_expect_type(enum event_type expect, char **tok)
653 return __read_expect_type(expect, tok, 1);
656 static int __read_expected(enum event_type expect, const char *str, int newline_ok)
658 enum event_type type;
663 type = read_token(&token);
665 type = read_token_item(&token);
667 ret = test_type_token(type, token, expect, str);
674 static int read_expected(enum event_type expect, const char *str)
676 return __read_expected(expect, str, 1);
679 static int read_expected_item(enum event_type expect, const char *str)
681 return __read_expected(expect, str, 0);
684 static char *event_read_name(void)
688 if (read_expected(EVENT_ITEM, "name") < 0)
691 if (read_expected(EVENT_OP, ":") < 0)
694 if (read_expect_type(EVENT_ITEM, &token) < 0)
704 static int event_read_id(void)
709 if (read_expected_item(EVENT_ITEM, "ID") < 0)
712 if (read_expected(EVENT_OP, ":") < 0)
715 if (read_expect_type(EVENT_ITEM, &token) < 0)
718 id = strtoul(token, NULL, 0);
727 static int field_is_string(struct format_field *field)
729 if ((field->flags & FIELD_IS_ARRAY) &&
730 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
731 !strstr(field->type, "s8")))
737 static int field_is_dynamic(struct format_field *field)
739 if (!strcmp(field->type, "__data_loc"))
745 static int event_read_fields(struct event *event, struct format_field **fields)
747 struct format_field *field = NULL;
748 enum event_type type;
754 type = read_token(&token);
755 if (type == EVENT_NEWLINE) {
762 if (test_type_token(type, token, EVENT_ITEM, "field"))
766 type = read_token(&token);
768 * The ftrace fields may still use the "special" name.
771 if (event->flags & EVENT_FL_ISFTRACE &&
772 type == EVENT_ITEM && strcmp(token, "special") == 0) {
774 type = read_token(&token);
777 if (test_type_token(type, token, EVENT_OP, ":") < 0)
780 if (read_expect_type(EVENT_ITEM, &token) < 0)
785 field = malloc_or_die(sizeof(*field));
786 memset(field, 0, sizeof(*field));
788 /* read the rest of the type */
790 type = read_token(&token);
791 if (type == EVENT_ITEM ||
792 (type == EVENT_OP && strcmp(token, "*") == 0) ||
794 * Some of the ftrace fields are broken and have
795 * an illegal "." in them.
797 (event->flags & EVENT_FL_ISFTRACE &&
798 type == EVENT_OP && strcmp(token, ".") == 0)) {
800 if (strcmp(token, "*") == 0)
801 field->flags |= FIELD_IS_POINTER;
804 field->type = realloc(field->type,
805 strlen(field->type) +
806 strlen(last_token) + 2);
807 strcat(field->type, " ");
808 strcat(field->type, last_token);
810 field->type = last_token;
819 die("no type found");
822 field->name = last_token;
824 if (test_type(type, EVENT_OP))
827 if (strcmp(token, "[") == 0) {
828 enum event_type last_type = type;
829 char *brackets = token;
832 field->flags |= FIELD_IS_ARRAY;
834 type = read_token(&token);
835 while (strcmp(token, "]") != 0) {
836 if (last_type == EVENT_ITEM &&
843 brackets = realloc(brackets,
845 strlen(token) + len);
847 strcat(brackets, " ");
848 strcat(brackets, token);
850 type = read_token(&token);
851 if (type == EVENT_NONE) {
852 die("failed to find token");
859 brackets = realloc(brackets, strlen(brackets) + 2);
860 strcat(brackets, "]");
862 /* add brackets to type */
864 type = read_token(&token);
866 * If the next token is not an OP, then it is of
867 * the format: type [] item;
869 if (type == EVENT_ITEM) {
870 field->type = realloc(field->type,
871 strlen(field->type) +
872 strlen(field->name) +
873 strlen(brackets) + 2);
874 strcat(field->type, " ");
875 strcat(field->type, field->name);
876 free_token(field->name);
877 strcat(field->type, brackets);
879 type = read_token(&token);
881 field->type = realloc(field->type,
882 strlen(field->type) +
883 strlen(brackets) + 1);
884 strcat(field->type, brackets);
889 if (field_is_string(field)) {
890 field->flags |= FIELD_IS_STRING;
891 if (field_is_dynamic(field))
892 field->flags |= FIELD_IS_DYNAMIC;
895 if (test_type_token(type, token, EVENT_OP, ";"))
899 if (read_expected(EVENT_ITEM, "offset") < 0)
902 if (read_expected(EVENT_OP, ":") < 0)
905 if (read_expect_type(EVENT_ITEM, &token))
907 field->offset = strtoul(token, NULL, 0);
910 if (read_expected(EVENT_OP, ";") < 0)
913 if (read_expected(EVENT_ITEM, "size") < 0)
916 if (read_expected(EVENT_OP, ":") < 0)
919 if (read_expect_type(EVENT_ITEM, &token))
921 field->size = strtoul(token, NULL, 0);
924 if (read_expected(EVENT_OP, ";") < 0)
927 type = read_token(&token);
928 if (type != EVENT_NEWLINE) {
929 /* newer versions of the kernel have a "signed" type */
930 if (test_type_token(type, token, EVENT_ITEM, "signed"))
935 if (read_expected(EVENT_OP, ":") < 0)
938 if (read_expect_type(EVENT_ITEM, &token))
941 /* add signed type */
944 if (read_expected(EVENT_OP, ";") < 0)
947 if (read_expect_type(EVENT_NEWLINE, &token))
954 fields = &field->next;
968 static int event_read_format(struct event *event)
973 if (read_expected_item(EVENT_ITEM, "format") < 0)
976 if (read_expected(EVENT_OP, ":") < 0)
979 if (read_expect_type(EVENT_NEWLINE, &token))
983 ret = event_read_fields(event, &event->format.common_fields);
986 event->format.nr_common = ret;
988 ret = event_read_fields(event, &event->format.fields);
991 event->format.nr_fields = ret;
1001 process_arg_token(struct event *event, struct print_arg *arg,
1002 char **tok, enum event_type type);
1004 static enum event_type
1005 process_arg(struct event *event, struct print_arg *arg, char **tok)
1007 enum event_type type;
1010 type = read_token(&token);
1013 return process_arg_token(event, arg, tok, type);
1016 static enum event_type
1017 process_cond(struct event *event, struct print_arg *top, char **tok)
1019 struct print_arg *arg, *left, *right;
1020 enum event_type type;
1023 arg = malloc_or_die(sizeof(*arg));
1024 memset(arg, 0, sizeof(*arg));
1026 left = malloc_or_die(sizeof(*left));
1028 right = malloc_or_die(sizeof(*right));
1030 arg->type = PRINT_OP;
1031 arg->op.left = left;
1032 arg->op.right = right;
1035 type = process_arg(event, left, &token);
1036 if (test_type_token(type, token, EVENT_OP, ":"))
1041 type = process_arg(event, right, &token);
1043 top->op.right = arg;
1056 static enum event_type
1057 process_array(struct event *event, struct print_arg *top, char **tok)
1059 struct print_arg *arg;
1060 enum event_type type;
1063 arg = malloc_or_die(sizeof(*arg));
1064 memset(arg, 0, sizeof(*arg));
1067 type = process_arg(event, arg, &token);
1068 if (test_type_token(type, token, EVENT_OP, "]"))
1071 top->op.right = arg;
1074 type = read_token_item(&token);
1085 static int get_op_prio(char *op)
1096 /* '>>' and '<<' are 8 */
1100 /* '==' and '!=' are 10 */
1110 die("unknown op '%c'", op[0]);
1114 if (strcmp(op, "++") == 0 ||
1115 strcmp(op, "--") == 0) {
1117 } else if (strcmp(op, ">>") == 0 ||
1118 strcmp(op, "<<") == 0) {
1120 } else if (strcmp(op, ">=") == 0 ||
1121 strcmp(op, "<=") == 0) {
1123 } else if (strcmp(op, "==") == 0 ||
1124 strcmp(op, "!=") == 0) {
1126 } else if (strcmp(op, "&&") == 0) {
1128 } else if (strcmp(op, "||") == 0) {
1131 die("unknown op '%s'", op);
1137 static void set_op_prio(struct print_arg *arg)
1140 /* single ops are the greatest */
1141 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1146 arg->op.prio = get_op_prio(arg->op.op);
1149 static enum event_type
1150 process_op(struct event *event, struct print_arg *arg, char **tok)
1152 struct print_arg *left, *right = NULL;
1153 enum event_type type;
1156 /* the op is passed in via tok */
1159 if (arg->type == PRINT_OP && !arg->op.left) {
1160 /* handle single op */
1162 die("bad op token %s", token);
1171 die("bad op token %s", token);
1175 /* make an empty left */
1176 left = malloc_or_die(sizeof(*left));
1177 left->type = PRINT_NULL;
1178 arg->op.left = left;
1180 right = malloc_or_die(sizeof(*right));
1181 arg->op.right = right;
1183 type = process_arg(event, right, tok);
1185 } else if (strcmp(token, "?") == 0) {
1187 left = malloc_or_die(sizeof(*left));
1188 /* copy the top arg to the left */
1191 arg->type = PRINT_OP;
1193 arg->op.left = left;
1196 type = process_cond(event, arg, tok);
1198 } else if (strcmp(token, ">>") == 0 ||
1199 strcmp(token, "<<") == 0 ||
1200 strcmp(token, "&") == 0 ||
1201 strcmp(token, "|") == 0 ||
1202 strcmp(token, "&&") == 0 ||
1203 strcmp(token, "||") == 0 ||
1204 strcmp(token, "-") == 0 ||
1205 strcmp(token, "+") == 0 ||
1206 strcmp(token, "*") == 0 ||
1207 strcmp(token, "^") == 0 ||
1208 strcmp(token, "/") == 0 ||
1209 strcmp(token, "<") == 0 ||
1210 strcmp(token, ">") == 0 ||
1211 strcmp(token, "==") == 0 ||
1212 strcmp(token, "!=") == 0) {
1214 left = malloc_or_die(sizeof(*left));
1216 /* copy the top arg to the left */
1219 arg->type = PRINT_OP;
1221 arg->op.left = left;
1225 right = malloc_or_die(sizeof(*right));
1227 type = read_token_item(&token);
1230 /* could just be a type pointer */
1231 if ((strcmp(arg->op.op, "*") == 0) &&
1232 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1233 if (left->type != PRINT_ATOM)
1234 die("bad pointer type");
1235 left->atom.atom = realloc(left->atom.atom,
1236 sizeof(left->atom.atom) + 3);
1237 strcat(left->atom.atom, " *");
1244 type = process_arg_token(event, right, tok, type);
1246 arg->op.right = right;
1248 } else if (strcmp(token, "[") == 0) {
1250 left = malloc_or_die(sizeof(*left));
1253 arg->type = PRINT_OP;
1255 arg->op.left = left;
1258 type = process_array(event, arg, tok);
1261 warning("unknown op '%s'", token);
1262 event->flags |= EVENT_FL_FAILED;
1263 /* the arg is now the left side */
1267 if (type == EVENT_OP) {
1270 /* higher prios need to be closer to the root */
1271 prio = get_op_prio(*tok);
1273 if (prio > arg->op.prio)
1274 return process_op(event, arg, tok);
1276 return process_op(event, right, tok);
1282 static enum event_type
1283 process_entry(struct event *event __unused, struct print_arg *arg,
1286 enum event_type type;
1290 if (read_expected(EVENT_OP, "->") < 0)
1293 if (read_expect_type(EVENT_ITEM, &token) < 0)
1297 arg->type = PRINT_FIELD;
1298 arg->field.name = field;
1300 type = read_token(&token);
1310 static char *arg_eval (struct print_arg *arg);
1312 static long long arg_num_eval(struct print_arg *arg)
1314 long long left, right;
1317 switch (arg->type) {
1319 val = strtoll(arg->atom.atom, NULL, 0);
1322 val = arg_num_eval(arg->typecast.item);
1325 switch (arg->op.op[0]) {
1327 left = arg_num_eval(arg->op.left);
1328 right = arg_num_eval(arg->op.right);
1330 val = left || right;
1335 left = arg_num_eval(arg->op.left);
1336 right = arg_num_eval(arg->op.right);
1338 val = left && right;
1343 left = arg_num_eval(arg->op.left);
1344 right = arg_num_eval(arg->op.right);
1345 switch (arg->op.op[1]) {
1350 val = left << right;
1353 val = left <= right;
1356 die("unknown op '%s'", arg->op.op);
1360 left = arg_num_eval(arg->op.left);
1361 right = arg_num_eval(arg->op.right);
1362 switch (arg->op.op[1]) {
1367 val = left >> right;
1370 val = left >= right;
1373 die("unknown op '%s'", arg->op.op);
1377 left = arg_num_eval(arg->op.left);
1378 right = arg_num_eval(arg->op.right);
1380 if (arg->op.op[1] != '=')
1381 die("unknown op '%s'", arg->op.op);
1383 val = left == right;
1386 left = arg_num_eval(arg->op.left);
1387 right = arg_num_eval(arg->op.right);
1389 switch (arg->op.op[1]) {
1391 val = left != right;
1394 die("unknown op '%s'", arg->op.op);
1398 die("unknown op '%s'", arg->op.op);
1403 case PRINT_FIELD ... PRINT_SYMBOL:
1406 die("invalid eval type %d", arg->type);
1412 static char *arg_eval (struct print_arg *arg)
1415 static char buf[20];
1417 switch (arg->type) {
1419 return arg->atom.atom;
1421 return arg_eval(arg->typecast.item);
1423 val = arg_num_eval(arg);
1424 sprintf(buf, "%lld", val);
1428 case PRINT_FIELD ... PRINT_SYMBOL:
1431 die("invalid eval type %d", arg->type);
1438 static enum event_type
1439 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1441 enum event_type type;
1442 struct print_arg *arg = NULL;
1443 struct print_flag_sym *field;
1449 type = read_token_item(&token);
1450 if (test_type_token(type, token, EVENT_OP, "{"))
1453 arg = malloc_or_die(sizeof(*arg));
1456 type = process_arg(event, arg, &token);
1457 if (test_type_token(type, token, EVENT_DELIM, ","))
1460 field = malloc_or_die(sizeof(*field));
1461 memset(field, 0, sizeof(field));
1463 value = arg_eval(arg);
1464 field->value = strdup(value);
1467 type = process_arg(event, arg, &token);
1468 if (test_type_token(type, token, EVENT_OP, "}"))
1471 value = arg_eval(arg);
1472 field->str = strdup(value);
1477 list = &field->next;
1480 type = read_token_item(&token);
1481 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1493 static enum event_type
1494 process_flags(struct event *event, struct print_arg *arg, char **tok)
1496 struct print_arg *field;
1497 enum event_type type;
1500 memset(arg, 0, sizeof(*arg));
1501 arg->type = PRINT_FLAGS;
1503 if (read_expected_item(EVENT_DELIM, "(") < 0)
1506 field = malloc_or_die(sizeof(*field));
1508 type = process_arg(event, field, &token);
1509 if (test_type_token(type, token, EVENT_DELIM, ","))
1512 arg->flags.field = field;
1514 type = read_token_item(&token);
1515 if (event_item_type(type)) {
1516 arg->flags.delim = token;
1517 type = read_token_item(&token);
1520 if (test_type_token(type, token, EVENT_DELIM, ","))
1523 type = process_fields(event, &arg->flags.flags, &token);
1524 if (test_type_token(type, token, EVENT_DELIM, ")"))
1528 type = read_token_item(tok);
1536 static enum event_type
1537 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1539 struct print_arg *field;
1540 enum event_type type;
1543 memset(arg, 0, sizeof(*arg));
1544 arg->type = PRINT_SYMBOL;
1546 if (read_expected_item(EVENT_DELIM, "(") < 0)
1549 field = malloc_or_die(sizeof(*field));
1551 type = process_arg(event, field, &token);
1552 if (test_type_token(type, token, EVENT_DELIM, ","))
1555 arg->symbol.field = field;
1557 type = process_fields(event, &arg->symbol.symbols, &token);
1558 if (test_type_token(type, token, EVENT_DELIM, ")"))
1562 type = read_token_item(tok);
1570 static enum event_type
1571 process_paren(struct event *event, struct print_arg *arg, char **tok)
1573 struct print_arg *item_arg;
1574 enum event_type type;
1577 type = process_arg(event, arg, &token);
1579 if (type == EVENT_ERROR)
1582 if (type == EVENT_OP)
1583 type = process_op(event, arg, &token);
1585 if (type == EVENT_ERROR)
1588 if (test_type_token(type, token, EVENT_DELIM, ")")) {
1594 type = read_token_item(&token);
1597 * If the next token is an item or another open paren, then
1598 * this was a typecast.
1600 if (event_item_type(type) ||
1601 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1603 /* make this a typecast and contine */
1605 /* prevous must be an atom */
1606 if (arg->type != PRINT_ATOM)
1607 die("previous needed to be PRINT_ATOM");
1609 item_arg = malloc_or_die(sizeof(*item_arg));
1611 arg->type = PRINT_TYPE;
1612 arg->typecast.type = arg->atom.atom;
1613 arg->typecast.item = item_arg;
1614 type = process_arg_token(event, item_arg, &token, type);
1623 static enum event_type
1624 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1626 enum event_type type;
1629 if (read_expected(EVENT_DELIM, "(") < 0)
1632 if (read_expect_type(EVENT_ITEM, &token) < 0)
1635 arg->type = PRINT_STRING;
1636 arg->string.string = token;
1637 arg->string.offset = -1;
1639 if (read_expected(EVENT_DELIM, ")") < 0)
1642 type = read_token(&token);
1652 process_arg_token(struct event *event, struct print_arg *arg,
1653 char **tok, enum event_type type)
1662 if (strcmp(token, "REC") == 0) {
1664 type = process_entry(event, arg, &token);
1665 } else if (strcmp(token, "__print_flags") == 0) {
1667 type = process_flags(event, arg, &token);
1668 } else if (strcmp(token, "__print_symbolic") == 0) {
1670 type = process_symbols(event, arg, &token);
1671 } else if (strcmp(token, "__get_str") == 0) {
1673 type = process_str(event, arg, &token);
1676 /* test the next token */
1677 type = read_token_item(&token);
1679 /* atoms can be more than one token long */
1680 while (type == EVENT_ITEM) {
1681 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1683 strcat(atom, token);
1685 type = read_token_item(&token);
1688 /* todo, test for function */
1690 arg->type = PRINT_ATOM;
1691 arg->atom.atom = atom;
1696 arg->type = PRINT_ATOM;
1697 arg->atom.atom = token;
1698 type = read_token_item(&token);
1701 if (strcmp(token, "(") == 0) {
1703 type = process_paren(event, arg, &token);
1707 /* handle single ops */
1708 arg->type = PRINT_OP;
1710 arg->op.left = NULL;
1711 type = process_op(event, arg, &token);
1715 case EVENT_ERROR ... EVENT_NEWLINE:
1717 die("unexpected type %d", type);
1724 static int event_read_print_args(struct event *event, struct print_arg **list)
1726 enum event_type type = EVENT_ERROR;
1727 struct print_arg *arg;
1732 if (type == EVENT_NEWLINE) {
1734 type = read_token_item(&token);
1738 arg = malloc_or_die(sizeof(*arg));
1739 memset(arg, 0, sizeof(*arg));
1741 type = process_arg(event, arg, &token);
1743 if (type == EVENT_ERROR) {
1751 if (type == EVENT_OP) {
1752 type = process_op(event, arg, &token);
1757 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1764 } while (type != EVENT_NONE);
1766 if (type != EVENT_NONE)
1772 static int event_read_print(struct event *event)
1774 enum event_type type;
1778 if (read_expected_item(EVENT_ITEM, "print") < 0)
1781 if (read_expected(EVENT_ITEM, "fmt") < 0)
1784 if (read_expected(EVENT_OP, ":") < 0)
1787 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1791 event->print_fmt.format = token;
1792 event->print_fmt.args = NULL;
1794 /* ok to have no arg */
1795 type = read_token_item(&token);
1797 if (type == EVENT_NONE)
1800 /* Handle concatination of print lines */
1801 if (type == EVENT_DQUOTE) {
1804 cat = malloc_or_die(strlen(event->print_fmt.format) +
1806 strcpy(cat, event->print_fmt.format);
1809 free_token(event->print_fmt.format);
1810 event->print_fmt.format = NULL;
1815 if (test_type_token(type, token, EVENT_DELIM, ","))
1820 ret = event_read_print_args(event, &event->print_fmt.args);
1831 static struct format_field *
1832 find_common_field(struct event *event, const char *name)
1834 struct format_field *format;
1836 for (format = event->format.common_fields;
1837 format; format = format->next) {
1838 if (strcmp(format->name, name) == 0)
1845 static struct format_field *
1846 find_field(struct event *event, const char *name)
1848 struct format_field *format;
1850 for (format = event->format.fields;
1851 format; format = format->next) {
1852 if (strcmp(format->name, name) == 0)
1859 static struct format_field *
1860 find_any_field(struct event *event, const char *name)
1862 struct format_field *format;
1864 format = find_common_field(event, name);
1867 return find_field(event, name);
1870 static unsigned long long read_size(void *ptr, int size)
1874 return *(unsigned char *)ptr;
1876 return data2host2(ptr);
1878 return data2host4(ptr);
1880 return data2host8(ptr);
1888 raw_field_value(struct event *event, const char *name, void *data)
1890 struct format_field *field;
1892 field = find_any_field(event, name);
1896 return read_size(data + field->offset, field->size);
1899 void *raw_field_ptr(struct event *event, const char *name, void *data)
1901 struct format_field *field;
1903 field = find_any_field(event, name);
1907 return data + field->offset;
1910 static int get_common_info(const char *type, int *offset, int *size)
1912 struct event *event;
1913 struct format_field *field;
1916 * All events should have the same common elements.
1917 * Pick any event to find where the type is;
1920 die("no event_list!");
1923 field = find_common_field(event, type);
1925 die("field '%s' not found", type);
1927 *offset = field->offset;
1928 *size = field->size;
1933 static int __parse_common(void *data, int *size, int *offset,
1939 ret = get_common_info(name, offset, size);
1943 return read_size(data + *offset, *size);
1946 int trace_parse_common_type(void *data)
1948 static int type_offset;
1949 static int type_size;
1951 return __parse_common(data, &type_size, &type_offset,
1955 static int parse_common_pid(void *data)
1957 static int pid_offset;
1958 static int pid_size;
1960 return __parse_common(data, &pid_size, &pid_offset,
1964 static int parse_common_pc(void *data)
1966 static int pc_offset;
1969 return __parse_common(data, &pc_size, &pc_offset,
1970 "common_preempt_count");
1973 static int parse_common_flags(void *data)
1975 static int flags_offset;
1976 static int flags_size;
1978 return __parse_common(data, &flags_size, &flags_offset,
1982 static int parse_common_lock_depth(void *data)
1984 static int ld_offset;
1988 ret = __parse_common(data, &ld_size, &ld_offset,
1989 "common_lock_depth");
1996 struct event *trace_find_event(int id)
1998 struct event *event;
2000 for (event = event_list; event; event = event->next) {
2001 if (event->id == id)
2007 static unsigned long long eval_num_arg(void *data, int size,
2008 struct event *event, struct print_arg *arg)
2010 unsigned long long val = 0;
2011 unsigned long long left, right;
2012 struct print_arg *larg;
2014 switch (arg->type) {
2019 return strtoull(arg->atom.atom, NULL, 0);
2021 if (!arg->field.field) {
2022 arg->field.field = find_any_field(event, arg->field.name);
2023 if (!arg->field.field)
2024 die("field %s not found", arg->field.name);
2026 /* must be a number */
2027 val = read_size(data + arg->field.field->offset,
2028 arg->field.field->size);
2034 return eval_num_arg(data, size, event, arg->typecast.item);
2039 if (strcmp(arg->op.op, "[") == 0) {
2041 * Arrays are special, since we don't want
2042 * to read the arg as is.
2044 if (arg->op.left->type != PRINT_FIELD)
2045 goto default_op; /* oops, all bets off */
2046 larg = arg->op.left;
2047 if (!larg->field.field) {
2049 find_any_field(event, larg->field.name);
2050 if (!larg->field.field)
2051 die("field %s not found", larg->field.name);
2053 right = eval_num_arg(data, size, event, arg->op.right);
2054 val = read_size(data + larg->field.field->offset +
2055 right * long_size, long_size);
2059 left = eval_num_arg(data, size, event, arg->op.left);
2060 right = eval_num_arg(data, size, event, arg->op.right);
2061 switch (arg->op.op[0]) {
2064 val = left || right;
2070 val = left && right;
2075 switch (arg->op.op[1]) {
2080 val = left << right;
2083 val = left <= right;
2086 die("unknown op '%s'", arg->op.op);
2090 switch (arg->op.op[1]) {
2095 val = left >> right;
2098 val = left >= right;
2101 die("unknown op '%s'", arg->op.op);
2105 if (arg->op.op[1] != '=')
2106 die("unknown op '%s'", arg->op.op);
2107 val = left == right;
2116 die("unknown op '%s'", arg->op.op);
2119 default: /* not sure what to do there */
2127 unsigned long long value;
2130 static const struct flag flags[] = {
2131 { "HI_SOFTIRQ", 0 },
2132 { "TIMER_SOFTIRQ", 1 },
2133 { "NET_TX_SOFTIRQ", 2 },
2134 { "NET_RX_SOFTIRQ", 3 },
2135 { "BLOCK_SOFTIRQ", 4 },
2136 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2137 { "TASKLET_SOFTIRQ", 6 },
2138 { "SCHED_SOFTIRQ", 7 },
2139 { "HRTIMER_SOFTIRQ", 8 },
2140 { "RCU_SOFTIRQ", 9 },
2142 { "HRTIMER_NORESTART", 0 },
2143 { "HRTIMER_RESTART", 1 },
2146 static unsigned long long eval_flag(const char *flag)
2151 * Some flags in the format files do not get converted.
2152 * If the flag is not numeric, see if it is something that
2153 * we already know about.
2155 if (isdigit(flag[0]))
2156 return strtoull(flag, NULL, 0);
2158 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2159 if (strcmp(flags[i].name, flag) == 0)
2160 return flags[i].value;
2165 static void print_str_arg(void *data, int size,
2166 struct event *event, struct print_arg *arg)
2168 struct print_flag_sym *flag;
2169 unsigned long long val, fval;
2173 switch (arg->type) {
2178 printf("%s", arg->atom.atom);
2181 if (!arg->field.field) {
2182 arg->field.field = find_any_field(event, arg->field.name);
2183 if (!arg->field.field)
2184 die("field %s not found", arg->field.name);
2186 str = malloc_or_die(arg->field.field->size + 1);
2187 memcpy(str, data + arg->field.field->offset,
2188 arg->field.field->size);
2189 str[arg->field.field->size] = 0;
2194 val = eval_num_arg(data, size, event, arg->flags.field);
2196 for (flag = arg->flags.flags; flag; flag = flag->next) {
2197 fval = eval_flag(flag->value);
2198 if (!val && !fval) {
2199 printf("%s", flag->str);
2202 if (fval && (val & fval) == fval) {
2203 if (print && arg->flags.delim)
2204 printf("%s", arg->flags.delim);
2205 printf("%s", flag->str);
2212 val = eval_num_arg(data, size, event, arg->symbol.field);
2213 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2214 fval = eval_flag(flag->value);
2216 printf("%s", flag->str);
2224 case PRINT_STRING: {
2227 if (arg->string.offset == -1) {
2228 struct format_field *f;
2230 f = find_any_field(event, arg->string.string);
2231 arg->string.offset = f->offset;
2233 str_offset = *(int *)(data + arg->string.offset);
2234 str_offset &= 0xffff;
2235 printf("%s", ((char *)data) + str_offset);
2240 * The only op for string should be ? :
2242 if (arg->op.op[0] != '?')
2244 val = eval_num_arg(data, size, event, arg->op.left);
2246 print_str_arg(data, size, event, arg->op.right->op.left);
2248 print_str_arg(data, size, event, arg->op.right->op.right);
2256 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2258 static struct format_field *field, *ip_field;
2259 struct print_arg *args, *arg, **next;
2260 unsigned long long ip, val;
2265 field = find_field(event, "buf");
2267 die("can't find buffer field for binary printk");
2268 ip_field = find_field(event, "ip");
2270 die("can't find ip field for binary printk");
2273 ip = read_size(data + ip_field->offset, ip_field->size);
2276 * The first arg is the IP pointer.
2278 args = malloc_or_die(sizeof(*args));
2283 arg->type = PRINT_ATOM;
2284 arg->atom.atom = malloc_or_die(32);
2285 sprintf(arg->atom.atom, "%lld", ip);
2287 /* skip the first "%pf : " */
2288 for (ptr = fmt + 6, bptr = data + field->offset;
2289 bptr < data + size && *ptr; ptr++) {
2313 /* the pointers are always 4 bytes aligned */
2314 bptr = (void *)(((unsigned long)bptr + 3) &
2326 val = read_size(bptr, ls);
2328 arg = malloc_or_die(sizeof(*arg));
2330 arg->type = PRINT_ATOM;
2331 arg->atom.atom = malloc_or_die(32);
2332 sprintf(arg->atom.atom, "%lld", val);
2337 arg = malloc_or_die(sizeof(*arg));
2339 arg->type = PRINT_STRING;
2340 arg->string.string = strdup(bptr);
2341 bptr += strlen(bptr) + 1;
2353 static void free_args(struct print_arg *args)
2355 struct print_arg *next;
2360 if (args->type == PRINT_ATOM)
2361 free(args->atom.atom);
2363 free(args->string.string);
2369 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2371 unsigned long long addr;
2372 static struct format_field *field;
2373 struct printk_map *printk;
2378 field = find_field(event, "fmt");
2380 die("can't find format field for binary printk");
2381 printf("field->offset = %d size=%d\n", field->offset, field->size);
2384 addr = read_size(data + field->offset, field->size);
2386 printk = find_printk(addr);
2388 format = malloc_or_die(45);
2389 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2395 /* Remove any quotes. */
2398 format = malloc_or_die(strlen(p) + 10);
2399 sprintf(format, "%s : %s", "%pf", p);
2400 /* remove ending quotes and new line since we will add one too */
2401 p = format + strlen(format) - 1;
2406 if (strcmp(p, "\\n") == 0)
2412 static void pretty_print(void *data, int size, struct event *event)
2414 struct print_fmt *print_fmt = &event->print_fmt;
2415 struct print_arg *arg = print_fmt->args;
2416 struct print_arg *args = NULL;
2417 const char *ptr = print_fmt->format;
2418 unsigned long long val;
2419 struct func_map *func;
2420 const char *saveptr;
2421 char *bprint_fmt = NULL;
2427 if (event->flags & EVENT_FL_ISFUNC)
2428 ptr = " %pF <-- %pF";
2430 if (event->flags & EVENT_FL_ISBPRINT) {
2431 bprint_fmt = get_bprint_format(data, size, event);
2432 args = make_bprint_args(bprint_fmt, data, size, event);
2437 for (; *ptr; ptr++) {
2459 } else if (*ptr == '%') {
2484 if (*(ptr+1) == 'F' ||
2497 die("no argument match");
2499 len = ((unsigned long)ptr + 1) -
2500 (unsigned long)saveptr;
2502 /* should never happen */
2506 memcpy(format, saveptr, len);
2509 val = eval_num_arg(data, size, event, arg);
2513 func = find_func(val);
2515 printf("%s", func->func);
2516 if (show_func == 'F')
2524 printf(format, (int)val);
2527 printf(format, (long)val);
2530 printf(format, (long long)val);
2533 die("bad count (%d)", ls);
2538 die("no matching argument");
2540 print_str_arg(data, size, event, arg);
2544 printf(">%c<", *ptr);
2557 static inline int log10_cpu(int nb)
2566 static void print_lat_fmt(void *data, int size __unused)
2568 unsigned int lat_flags;
2574 lat_flags = parse_common_flags(data);
2575 pc = parse_common_pc(data);
2576 lock_depth = parse_common_lock_depth(data);
2578 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
2579 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
2582 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
2583 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
2585 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
2587 (hardirq && softirq) ? 'H' :
2588 hardirq ? 'h' : softirq ? 's' : '.');
2598 printf("%d", lock_depth);
2601 /* taken from Linux, written by Frederic Weisbecker */
2602 static void print_graph_cpu(int cpu)
2605 int log10_this = log10_cpu(cpu);
2606 int log10_all = log10_cpu(cpus);
2610 * Start with a space character - to make it stand out
2611 * to the right a bit when trace output is pasted into
2617 * Tricky - we space the CPU field according to the max
2618 * number of online CPUs. On a 2-cpu system it would take
2619 * a maximum of 1 digit - on a 128 cpu system it would
2620 * take up to 3 digits:
2622 for (i = 0; i < log10_all - log10_this; i++)
2625 printf("%d) ", cpu);
2628 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2629 #define TRACE_GRAPH_INDENT 2
2631 static void print_graph_proc(int pid, const char *comm)
2633 /* sign + log10(MAX_INT) + '\0' */
2639 sprintf(pid_str, "%d", pid);
2641 /* 1 stands for the "-" character */
2642 len = strlen(comm) + strlen(pid_str) + 1;
2644 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2645 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2647 /* First spaces to align center */
2648 for (i = 0; i < spaces / 2; i++)
2651 printf("%s-%s", comm, pid_str);
2653 /* Last spaces to align center */
2654 for (i = 0; i < spaces - (spaces / 2); i++)
2658 static struct record *
2659 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2660 struct record *next)
2662 struct format_field *field;
2663 struct event *event;
2668 type = trace_parse_common_type(next->data);
2669 event = trace_find_event(type);
2673 if (!(event->flags & EVENT_FL_ISFUNCRET))
2676 pid = parse_common_pid(next->data);
2677 field = find_field(event, "func");
2679 die("function return does not have field func");
2681 val = read_size(next->data + field->offset, field->size);
2683 if (cur_pid != pid || cur_func != val)
2686 /* this is a leaf, now advance the iterator */
2687 return trace_read_data(cpu);
2690 /* Signal a overhead of time execution to the output */
2691 static void print_graph_overhead(unsigned long long duration)
2693 /* Non nested entry or return */
2694 if (duration == ~0ULL)
2695 return (void)printf(" ");
2697 /* Duration exceeded 100 msecs */
2698 if (duration > 100000ULL)
2699 return (void)printf("! ");
2701 /* Duration exceeded 10 msecs */
2702 if (duration > 10000ULL)
2703 return (void)printf("+ ");
2708 static void print_graph_duration(unsigned long long duration)
2710 unsigned long usecs = duration / 1000;
2711 unsigned long nsecs_rem = duration % 1000;
2712 /* log10(ULONG_MAX) + '\0' */
2718 sprintf(msecs_str, "%lu", usecs);
2721 len = printf("%lu", usecs);
2723 /* Print nsecs (we don't want to exceed 7 numbers) */
2725 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2726 len += printf(".%s", nsecs_str);
2731 /* Print remaining spaces to fit the row's width */
2732 for (i = len; i < 7; i++)
2739 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2741 unsigned long long rettime, calltime;
2742 unsigned long long duration, depth;
2743 unsigned long long val;
2744 struct format_field *field;
2745 struct func_map *func;
2746 struct event *ret_event;
2750 type = trace_parse_common_type(ret_rec->data);
2751 ret_event = trace_find_event(type);
2753 field = find_field(ret_event, "rettime");
2755 die("can't find rettime in return graph");
2756 rettime = read_size(ret_rec->data + field->offset, field->size);
2758 field = find_field(ret_event, "calltime");
2760 die("can't find rettime in return graph");
2761 calltime = read_size(ret_rec->data + field->offset, field->size);
2763 duration = rettime - calltime;
2766 print_graph_overhead(duration);
2769 print_graph_duration(duration);
2771 field = find_field(event, "depth");
2773 die("can't find depth in entry graph");
2774 depth = read_size(data + field->offset, field->size);
2777 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2780 field = find_field(event, "func");
2782 die("can't find func in entry graph");
2783 val = read_size(data + field->offset, field->size);
2784 func = find_func(val);
2787 printf("%s();", func->func);
2789 printf("%llx();", val);
2792 static void print_graph_nested(struct event *event, void *data)
2794 struct format_field *field;
2795 unsigned long long depth;
2796 unsigned long long val;
2797 struct func_map *func;
2801 print_graph_overhead(-1);
2806 field = find_field(event, "depth");
2808 die("can't find depth in entry graph");
2809 depth = read_size(data + field->offset, field->size);
2812 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2815 field = find_field(event, "func");
2817 die("can't find func in entry graph");
2818 val = read_size(data + field->offset, field->size);
2819 func = find_func(val);
2822 printf("%s() {", func->func);
2824 printf("%llx() {", val);
2828 pretty_print_func_ent(void *data, int size, struct event *event,
2829 int cpu, int pid, const char *comm,
2830 unsigned long secs, unsigned long usecs)
2832 struct format_field *field;
2837 printf("%5lu.%06lu | ", secs, usecs);
2839 print_graph_cpu(cpu);
2840 print_graph_proc(pid, comm);
2844 if (latency_format) {
2845 print_lat_fmt(data, size);
2849 field = find_field(event, "func");
2851 die("function entry does not have func field");
2853 val = read_size(data + field->offset, field->size);
2856 * peek_data may unmap the data pointer. Copy it first.
2858 copy_data = malloc_or_die(size);
2859 memcpy(copy_data, data, size);
2862 rec = trace_peek_data(cpu);
2864 rec = get_return_for_leaf(cpu, pid, val, rec);
2866 print_graph_entry_leaf(event, data, rec);
2870 print_graph_nested(event, data);
2876 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2877 int cpu, int pid, const char *comm,
2878 unsigned long secs, unsigned long usecs)
2880 unsigned long long rettime, calltime;
2881 unsigned long long duration, depth;
2882 struct format_field *field;
2885 printf("%5lu.%06lu | ", secs, usecs);
2887 print_graph_cpu(cpu);
2888 print_graph_proc(pid, comm);
2892 if (latency_format) {
2893 print_lat_fmt(data, size);
2897 field = find_field(event, "rettime");
2899 die("can't find rettime in return graph");
2900 rettime = read_size(data + field->offset, field->size);
2902 field = find_field(event, "calltime");
2904 die("can't find calltime in return graph");
2905 calltime = read_size(data + field->offset, field->size);
2907 duration = rettime - calltime;
2910 print_graph_overhead(duration);
2913 print_graph_duration(duration);
2915 field = find_field(event, "depth");
2917 die("can't find depth in entry graph");
2918 depth = read_size(data + field->offset, field->size);
2921 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2928 pretty_print_func_graph(void *data, int size, struct event *event,
2929 int cpu, int pid, const char *comm,
2930 unsigned long secs, unsigned long usecs)
2932 if (event->flags & EVENT_FL_ISFUNCENT)
2933 pretty_print_func_ent(data, size, event,
2934 cpu, pid, comm, secs, usecs);
2935 else if (event->flags & EVENT_FL_ISFUNCRET)
2936 pretty_print_func_ret(data, size, event,
2937 cpu, pid, comm, secs, usecs);
2941 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2944 struct event *event;
2946 unsigned long usecs;
2950 secs = nsecs / NSECS_PER_SEC;
2951 nsecs -= secs * NSECS_PER_SEC;
2952 usecs = nsecs / NSECS_PER_USEC;
2954 type = trace_parse_common_type(data);
2956 event = trace_find_event(type);
2958 warning("ug! no event found for type %d", type);
2962 pid = parse_common_pid(data);
2964 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2965 return pretty_print_func_graph(data, size, event, cpu,
2966 pid, comm, secs, usecs);
2968 if (latency_format) {
2969 printf("%8.8s-%-5d %3d",
2971 print_lat_fmt(data, size);
2973 printf("%16s-%-5d [%03d]", comm, pid, cpu);
2975 printf(" %5lu.%06lu: %s: ", secs, usecs, event->name);
2977 if (event->flags & EVENT_FL_FAILED) {
2978 printf("EVENT '%s' FAILED TO PARSE\n",
2983 pretty_print(data, size, event);
2987 static void print_fields(struct print_flag_sym *field)
2989 printf("{ %s, %s }", field->value, field->str);
2992 print_fields(field->next);
2996 static void print_args(struct print_arg *args)
2998 int print_paren = 1;
3000 switch (args->type) {
3005 printf("%s", args->atom.atom);
3008 printf("REC->%s", args->field.name);
3011 printf("__print_flags(");
3012 print_args(args->flags.field);
3013 printf(", %s, ", args->flags.delim);
3014 print_fields(args->flags.flags);
3018 printf("__print_symbolic(");
3019 print_args(args->symbol.field);
3021 print_fields(args->symbol.symbols);
3025 printf("__get_str(%s)", args->string.string);
3028 printf("(%s)", args->typecast.type);
3029 print_args(args->typecast.item);
3032 if (strcmp(args->op.op, ":") == 0)
3036 print_args(args->op.left);
3037 printf(" %s ", args->op.op);
3038 print_args(args->op.right);
3043 /* we should warn... */
3048 print_args(args->next);
3052 static void parse_header_field(const char *field,
3053 int *offset, int *size)
3058 if (read_expected(EVENT_ITEM, "field") < 0)
3060 if (read_expected(EVENT_OP, ":") < 0)
3064 if (read_expect_type(EVENT_ITEM, &token) < 0)
3068 if (read_expected(EVENT_ITEM, field) < 0)
3070 if (read_expected(EVENT_OP, ";") < 0)
3072 if (read_expected(EVENT_ITEM, "offset") < 0)
3074 if (read_expected(EVENT_OP, ":") < 0)
3076 if (read_expect_type(EVENT_ITEM, &token) < 0)
3078 *offset = atoi(token);
3080 if (read_expected(EVENT_OP, ";") < 0)
3082 if (read_expected(EVENT_ITEM, "size") < 0)
3084 if (read_expected(EVENT_OP, ":") < 0)
3086 if (read_expect_type(EVENT_ITEM, &token) < 0)
3088 *size = atoi(token);
3090 if (read_expected(EVENT_OP, ";") < 0)
3092 type = read_token(&token);
3093 if (type != EVENT_NEWLINE) {
3094 /* newer versions of the kernel have a "signed" type */
3095 if (type != EVENT_ITEM)
3098 if (strcmp(token, "signed") != 0)
3103 if (read_expected(EVENT_OP, ":") < 0)
3106 if (read_expect_type(EVENT_ITEM, &token))
3110 if (read_expected(EVENT_OP, ";") < 0)
3113 if (read_expect_type(EVENT_NEWLINE, &token))
3120 int parse_header_page(char *buf, unsigned long size)
3122 init_input_buf(buf, size);
3124 parse_header_field("timestamp", &header_page_ts_offset,
3125 &header_page_ts_size);
3126 parse_header_field("commit", &header_page_size_offset,
3127 &header_page_size_size);
3128 parse_header_field("data", &header_page_data_offset,
3129 &header_page_data_size);
3134 int parse_ftrace_file(char *buf, unsigned long size)
3136 struct format_field *field;
3137 struct print_arg *arg, **list;
3138 struct event *event;
3141 init_input_buf(buf, size);
3143 event = alloc_event();
3147 event->flags |= EVENT_FL_ISFTRACE;
3149 event->name = event_read_name();
3151 die("failed to read ftrace event name");
3153 if (strcmp(event->name, "function") == 0)
3154 event->flags |= EVENT_FL_ISFUNC;
3156 else if (strcmp(event->name, "funcgraph_entry") == 0)
3157 event->flags |= EVENT_FL_ISFUNCENT;
3159 else if (strcmp(event->name, "funcgraph_exit") == 0)
3160 event->flags |= EVENT_FL_ISFUNCRET;
3162 else if (strcmp(event->name, "bprint") == 0)
3163 event->flags |= EVENT_FL_ISBPRINT;
3165 event->id = event_read_id();
3167 die("failed to read ftrace event id");
3171 ret = event_read_format(event);
3173 die("failed to read ftrace event format");
3175 ret = event_read_print(event);
3177 die("failed to read ftrace event print fmt");
3179 /* New ftrace handles args */
3183 * The arguments for ftrace files are parsed by the fields.
3184 * Set up the fields as their arguments.
3186 list = &event->print_fmt.args;
3187 for (field = event->format.fields; field; field = field->next) {
3188 arg = malloc_or_die(sizeof(*arg));
3189 memset(arg, 0, sizeof(*arg));
3192 arg->type = PRINT_FIELD;
3193 arg->field.name = field->name;
3194 arg->field.field = field;
3199 int parse_event_file(char *buf, unsigned long size, char *sys)
3201 struct event *event;
3204 init_input_buf(buf, size);
3206 event = alloc_event();
3210 event->name = event_read_name();
3212 die("failed to read event name");
3214 event->id = event_read_id();
3216 die("failed to read event id");
3218 ret = event_read_format(event);
3220 warning("failed to read event format for %s", event->name);
3224 ret = event_read_print(event);
3226 warning("failed to read event print fmt for %s", event->name);
3230 event->system = strdup(sys);
3232 #define PRINT_ARGS 0
3233 if (PRINT_ARGS && event->print_fmt.args)
3234 print_args(event->print_fmt.args);
3240 event->flags |= EVENT_FL_FAILED;
3241 /* still add it even if it failed */
3246 void parse_set_info(int nr_cpus, int long_sz)
3249 long_size = long_sz;