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;
43 static char *input_buf;
44 static unsigned long long input_buf_ptr;
45 static unsigned long long input_buf_siz;
50 static void init_input_buf(char *buf, unsigned long long size)
62 static struct cmdline *cmdlines;
63 static int cmdline_count;
65 static int cmdline_cmp(const void *a, const void *b)
67 const struct cmdline *ca = a;
68 const struct cmdline *cb = b;
70 if (ca->pid < cb->pid)
72 if (ca->pid > cb->pid)
78 void parse_cmdlines(char *file, int size __unused)
81 struct cmdline_list *next;
84 } *list = NULL, *item;
89 line = strtok_r(file, "\n", &next);
91 item = malloc_or_die(sizeof(*item));
92 sscanf(line, "%d %as", &item->pid,
93 (float *)(void *)&item->comm); /* workaround gcc warning */
96 line = strtok_r(NULL, "\n", &next);
100 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
104 cmdlines[i].pid = list->pid;
105 cmdlines[i].comm = list->comm;
112 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
115 static struct func_map {
116 unsigned long long addr;
120 static unsigned int func_count;
122 static int func_cmp(const void *a, const void *b)
124 const struct func_map *fa = a;
125 const struct func_map *fb = b;
127 if (fa->addr < fb->addr)
129 if (fa->addr > fb->addr)
135 void parse_proc_kallsyms(char *file, unsigned int size __unused)
138 struct func_list *next;
139 unsigned long long addr;
142 } *list = NULL, *item;
150 line = strtok_r(file, "\n", &next);
152 item = malloc_or_die(sizeof(*item));
154 ret = sscanf(line, "%as %c %as\t[%as",
155 (float *)(void *)&addr_str, /* workaround gcc warning */
157 (float *)(void *)&item->func,
158 (float *)(void *)&item->mod);
159 item->addr = strtoull(addr_str, NULL, 16);
162 /* truncate the extra ']' */
164 item->mod[strlen(item->mod) - 1] = 0;
169 line = strtok_r(NULL, "\n", &next);
173 func_list = malloc_or_die(sizeof(*func_list) * func_count + 1);
177 func_list[i].func = list->func;
178 func_list[i].addr = list->addr;
179 func_list[i].mod = list->mod;
186 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
189 * Add a special record at the end.
191 func_list[func_count].func = NULL;
192 func_list[func_count].addr = 0;
193 func_list[func_count].mod = NULL;
197 * We are searching for a record in between, not an exact
200 static int func_bcmp(const void *a, const void *b)
202 const struct func_map *fa = a;
203 const struct func_map *fb = b;
205 if ((fa->addr == fb->addr) ||
207 (fa->addr > fb->addr &&
208 fa->addr < (fb+1)->addr))
211 if (fa->addr < fb->addr)
217 static struct func_map *find_func(unsigned long long addr)
219 struct func_map *func;
224 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
230 void print_funcs(void)
234 for (i = 0; i < (int)func_count; i++) {
238 if (func_list[i].mod)
239 printf(" [%s]\n", func_list[i].mod);
245 static struct printk_map {
246 unsigned long long addr;
249 static unsigned int printk_count;
251 static int printk_cmp(const void *a, const void *b)
253 const struct func_map *fa = a;
254 const struct func_map *fb = b;
256 if (fa->addr < fb->addr)
258 if (fa->addr > fb->addr)
264 static struct printk_map *find_printk(unsigned long long addr)
266 struct printk_map *printk;
267 struct printk_map key;
271 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
277 void parse_ftrace_printk(char *file, unsigned int size __unused)
280 struct printk_list *next;
281 unsigned long long addr;
283 } *list = NULL, *item;
290 line = strtok_r(file, "\n", &next);
292 item = malloc_or_die(sizeof(*item));
293 addr_str = strtok_r(line, ":", &fmt);
294 item->addr = strtoull(addr_str, NULL, 16);
295 /* fmt still has a space, skip it */
296 item->printk = strdup(fmt+1);
299 line = strtok_r(NULL, "\n", &next);
303 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
307 printk_list[i].printk = list->printk;
308 printk_list[i].addr = list->addr;
315 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
318 void print_printk(void)
322 for (i = 0; i < (int)printk_count; i++) {
323 printf("%016llx %s\n",
325 printk_list[i].printk);
329 static struct event *alloc_event(void)
333 event = malloc_or_die(sizeof(*event));
334 memset(event, 0, sizeof(*event));
351 static struct event *event_list;
353 static void add_event(struct event *event)
355 event->next = event_list;
359 static int event_item_type(enum event_type type)
362 case EVENT_ITEM ... EVENT_SQUOTE:
364 case EVENT_ERROR ... EVENT_DELIM:
370 static void free_arg(struct print_arg *arg)
378 free(arg->atom.atom);
381 case PRINT_FIELD ... PRINT_OP:
390 static enum event_type get_type(int ch)
393 return EVENT_NEWLINE;
396 if (isalnum(ch) || ch == '_')
404 if (ch == '(' || ch == ')' || ch == ',')
410 static int __read_char(void)
412 if (input_buf_ptr >= input_buf_siz)
415 return input_buf[input_buf_ptr++];
418 static int __peek_char(void)
420 if (input_buf_ptr >= input_buf_siz)
423 return input_buf[input_buf_ptr];
426 static enum event_type __read_token(char **tok)
429 int ch, last_ch, quote_ch, next_ch;
432 enum event_type type;
442 if (type == EVENT_NONE)
450 *tok = malloc_or_die(2);
458 next_ch = __peek_char();
459 if (next_ch == '>') {
460 buf[i++] = __read_char();
473 buf[i++] = __read_char();
485 default: /* what should we do instead? */
495 buf[i++] = __read_char();
500 /* don't keep quotes */
505 if (i == (BUFSIZ - 1)) {
508 *tok = realloc(*tok, tok_size + BUFSIZ);
523 /* the '\' '\' will cancel itself */
524 if (ch == '\\' && last_ch == '\\')
526 } while (ch != quote_ch || last_ch == '\\');
527 /* remove the last quote */
531 case EVENT_ERROR ... EVENT_SPACE:
537 while (get_type(__peek_char()) == type) {
538 if (i == (BUFSIZ - 1)) {
541 *tok = realloc(*tok, tok_size + BUFSIZ);
560 *tok = realloc(*tok, tok_size + i);
572 static void free_token(char *tok)
578 static enum event_type read_token(char **tok)
580 enum event_type type;
583 type = __read_token(tok);
584 if (type != EVENT_SPACE)
595 static enum event_type read_token_item(char **tok)
597 enum event_type type;
600 type = __read_token(tok);
601 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
611 static int test_type(enum event_type type, enum event_type expect)
613 if (type != expect) {
614 warning("Error: expected type %d but read %d",
621 static int test_type_token(enum event_type type, char *token,
622 enum event_type expect, const char *expect_tok)
624 if (type != expect) {
625 warning("Error: expected type %d but read %d",
630 if (strcmp(token, expect_tok) != 0) {
631 warning("Error: expected '%s' but read '%s'",
638 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
640 enum event_type type;
643 type = read_token(tok);
645 type = read_token_item(tok);
646 return test_type(type, expect);
649 static int read_expect_type(enum event_type expect, char **tok)
651 return __read_expect_type(expect, tok, 1);
654 static int __read_expected(enum event_type expect, const char *str, int newline_ok)
656 enum event_type type;
661 type = read_token(&token);
663 type = read_token_item(&token);
665 ret = test_type_token(type, token, expect, str);
672 static int read_expected(enum event_type expect, const char *str)
674 return __read_expected(expect, str, 1);
677 static int read_expected_item(enum event_type expect, const char *str)
679 return __read_expected(expect, str, 0);
682 static char *event_read_name(void)
686 if (read_expected(EVENT_ITEM, (char *)"name") < 0)
689 if (read_expected(EVENT_OP, (char *)":") < 0)
692 if (read_expect_type(EVENT_ITEM, &token) < 0)
702 static int event_read_id(void)
707 if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
710 if (read_expected(EVENT_OP, (char *)":") < 0)
713 if (read_expect_type(EVENT_ITEM, &token) < 0)
716 id = strtoul(token, NULL, 0);
725 static int field_is_string(struct format_field *field)
727 if ((field->flags & FIELD_IS_ARRAY) &&
728 (!strstr(field->type, "char") || !strstr(field->type, "u8") ||
729 !strstr(field->type, "s8")))
735 static int field_is_dynamic(struct format_field *field)
737 if (!strcmp(field->type, "__data_loc"))
743 static int event_read_fields(struct event *event, struct format_field **fields)
745 struct format_field *field = NULL;
746 enum event_type type;
752 type = read_token(&token);
753 if (type == EVENT_NEWLINE) {
760 if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
764 type = read_token(&token);
766 * The ftrace fields may still use the "special" name.
769 if (event->flags & EVENT_FL_ISFTRACE &&
770 type == EVENT_ITEM && strcmp(token, "special") == 0) {
772 type = read_token(&token);
775 if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
778 if (read_expect_type(EVENT_ITEM, &token) < 0)
783 field = malloc_or_die(sizeof(*field));
784 memset(field, 0, sizeof(*field));
786 /* read the rest of the type */
788 type = read_token(&token);
789 if (type == EVENT_ITEM ||
790 (type == EVENT_OP && strcmp(token, "*") == 0) ||
792 * Some of the ftrace fields are broken and have
793 * an illegal "." in them.
795 (event->flags & EVENT_FL_ISFTRACE &&
796 type == EVENT_OP && strcmp(token, ".") == 0)) {
798 if (strcmp(token, "*") == 0)
799 field->flags |= FIELD_IS_POINTER;
802 field->type = realloc(field->type,
803 strlen(field->type) +
804 strlen(last_token) + 2);
805 strcat(field->type, " ");
806 strcat(field->type, last_token);
808 field->type = last_token;
817 die("no type found");
820 field->name = last_token;
822 if (test_type(type, EVENT_OP))
825 if (strcmp(token, "[") == 0) {
826 enum event_type last_type = type;
827 char *brackets = token;
830 field->flags |= FIELD_IS_ARRAY;
832 type = read_token(&token);
833 while (strcmp(token, "]") != 0) {
834 if (last_type == EVENT_ITEM &&
841 brackets = realloc(brackets,
843 strlen(token) + len);
845 strcat(brackets, " ");
846 strcat(brackets, token);
848 type = read_token(&token);
849 if (type == EVENT_NONE) {
850 die("failed to find token");
857 brackets = realloc(brackets, strlen(brackets) + 2);
858 strcat(brackets, "]");
860 /* add brackets to type */
862 type = read_token(&token);
864 * If the next token is not an OP, then it is of
865 * the format: type [] item;
867 if (type == EVENT_ITEM) {
868 field->type = realloc(field->type,
869 strlen(field->type) +
870 strlen(field->name) +
871 strlen(brackets) + 2);
872 strcat(field->type, " ");
873 strcat(field->type, field->name);
874 free_token(field->name);
875 strcat(field->type, brackets);
877 type = read_token(&token);
879 field->type = realloc(field->type,
880 strlen(field->type) +
881 strlen(brackets) + 1);
882 strcat(field->type, brackets);
887 if (field_is_string(field)) {
888 field->flags |= FIELD_IS_STRING;
889 if (field_is_dynamic(field))
890 field->flags |= FIELD_IS_DYNAMIC;
893 if (test_type_token(type, token, EVENT_OP, (char *)";"))
897 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
900 if (read_expected(EVENT_OP, (char *)":") < 0)
903 if (read_expect_type(EVENT_ITEM, &token))
905 field->offset = strtoul(token, NULL, 0);
908 if (read_expected(EVENT_OP, (char *)";") < 0)
911 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
914 if (read_expected(EVENT_OP, (char *)":") < 0)
917 if (read_expect_type(EVENT_ITEM, &token))
919 field->size = strtoul(token, NULL, 0);
922 if (read_expected(EVENT_OP, (char *)";") < 0)
925 type = read_token(&token);
926 if (type != EVENT_NEWLINE) {
927 /* newer versions of the kernel have a "signed" type */
928 if (test_type_token(type, token, EVENT_ITEM, (char *)"signed"))
933 if (read_expected(EVENT_OP, (char *)":") < 0)
936 if (read_expect_type(EVENT_ITEM, &token))
939 /* add signed type */
942 if (read_expected(EVENT_OP, (char *)";") < 0)
945 if (read_expect_type(EVENT_NEWLINE, &token))
952 fields = &field->next;
966 static int event_read_format(struct event *event)
971 if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
974 if (read_expected(EVENT_OP, (char *)":") < 0)
977 if (read_expect_type(EVENT_NEWLINE, &token))
981 ret = event_read_fields(event, &event->format.common_fields);
984 event->format.nr_common = ret;
986 ret = event_read_fields(event, &event->format.fields);
989 event->format.nr_fields = ret;
999 process_arg_token(struct event *event, struct print_arg *arg,
1000 char **tok, enum event_type type);
1002 static enum event_type
1003 process_arg(struct event *event, struct print_arg *arg, char **tok)
1005 enum event_type type;
1008 type = read_token(&token);
1011 return process_arg_token(event, arg, tok, type);
1014 static enum event_type
1015 process_cond(struct event *event, struct print_arg *top, char **tok)
1017 struct print_arg *arg, *left, *right;
1018 enum event_type type;
1021 arg = malloc_or_die(sizeof(*arg));
1022 memset(arg, 0, sizeof(*arg));
1024 left = malloc_or_die(sizeof(*left));
1026 right = malloc_or_die(sizeof(*right));
1028 arg->type = PRINT_OP;
1029 arg->op.left = left;
1030 arg->op.right = right;
1033 type = process_arg(event, left, &token);
1034 if (test_type_token(type, token, EVENT_OP, (char *)":"))
1039 type = process_arg(event, right, &token);
1041 top->op.right = arg;
1054 static enum event_type
1055 process_array(struct event *event, struct print_arg *top, char **tok)
1057 struct print_arg *arg;
1058 enum event_type type;
1061 arg = malloc_or_die(sizeof(*arg));
1062 memset(arg, 0, sizeof(*arg));
1065 type = process_arg(event, arg, &token);
1066 if (test_type_token(type, token, EVENT_OP, (char *)"]"))
1069 top->op.right = arg;
1072 type = read_token_item(&token);
1083 static int get_op_prio(char *op)
1094 /* '>>' and '<<' are 8 */
1098 /* '==' and '!=' are 10 */
1108 die("unknown op '%c'", op[0]);
1112 if (strcmp(op, "++") == 0 ||
1113 strcmp(op, "--") == 0) {
1115 } else if (strcmp(op, ">>") == 0 ||
1116 strcmp(op, "<<") == 0) {
1118 } else if (strcmp(op, ">=") == 0 ||
1119 strcmp(op, "<=") == 0) {
1121 } else if (strcmp(op, "==") == 0 ||
1122 strcmp(op, "!=") == 0) {
1124 } else if (strcmp(op, "&&") == 0) {
1126 } else if (strcmp(op, "||") == 0) {
1129 die("unknown op '%s'", op);
1135 static void set_op_prio(struct print_arg *arg)
1138 /* single ops are the greatest */
1139 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1144 arg->op.prio = get_op_prio(arg->op.op);
1147 static enum event_type
1148 process_op(struct event *event, struct print_arg *arg, char **tok)
1150 struct print_arg *left, *right = NULL;
1151 enum event_type type;
1154 /* the op is passed in via tok */
1157 if (arg->type == PRINT_OP && !arg->op.left) {
1158 /* handle single op */
1160 die("bad op token %s", token);
1169 die("bad op token %s", token);
1173 /* make an empty left */
1174 left = malloc_or_die(sizeof(*left));
1175 left->type = PRINT_NULL;
1176 arg->op.left = left;
1178 right = malloc_or_die(sizeof(*right));
1179 arg->op.right = right;
1181 type = process_arg(event, right, tok);
1183 } else if (strcmp(token, "?") == 0) {
1185 left = malloc_or_die(sizeof(*left));
1186 /* copy the top arg to the left */
1189 arg->type = PRINT_OP;
1191 arg->op.left = left;
1194 type = process_cond(event, arg, tok);
1196 } else if (strcmp(token, ">>") == 0 ||
1197 strcmp(token, "<<") == 0 ||
1198 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) {
1212 left = malloc_or_die(sizeof(*left));
1214 /* copy the top arg to the left */
1217 arg->type = PRINT_OP;
1219 arg->op.left = left;
1223 right = malloc_or_die(sizeof(*right));
1225 type = read_token_item(&token);
1228 /* could just be a type pointer */
1229 if ((strcmp(arg->op.op, "*") == 0) &&
1230 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1231 if (left->type != PRINT_ATOM)
1232 die("bad pointer type");
1233 left->atom.atom = realloc(left->atom.atom,
1234 sizeof(left->atom.atom) + 3);
1235 strcat(left->atom.atom, " *");
1242 type = process_arg_token(event, right, tok, type);
1244 arg->op.right = right;
1246 } else if (strcmp(token, "[") == 0) {
1248 left = malloc_or_die(sizeof(*left));
1251 arg->type = PRINT_OP;
1253 arg->op.left = left;
1256 type = process_array(event, arg, tok);
1259 warning("unknown op '%s'", token);
1260 event->flags |= EVENT_FL_FAILED;
1261 /* the arg is now the left side */
1265 if (type == EVENT_OP) {
1268 /* higher prios need to be closer to the root */
1269 prio = get_op_prio(*tok);
1271 if (prio > arg->op.prio)
1272 return process_op(event, arg, tok);
1274 return process_op(event, right, tok);
1280 static enum event_type
1281 process_entry(struct event *event __unused, struct print_arg *arg,
1284 enum event_type type;
1288 if (read_expected(EVENT_OP, (char *)"->") < 0)
1291 if (read_expect_type(EVENT_ITEM, &token) < 0)
1295 arg->type = PRINT_FIELD;
1296 arg->field.name = field;
1298 type = read_token(&token);
1308 static char *arg_eval (struct print_arg *arg);
1310 static long long arg_num_eval(struct print_arg *arg)
1312 long long left, right;
1315 switch (arg->type) {
1317 val = strtoll(arg->atom.atom, NULL, 0);
1320 val = arg_num_eval(arg->typecast.item);
1323 switch (arg->op.op[0]) {
1325 left = arg_num_eval(arg->op.left);
1326 right = arg_num_eval(arg->op.right);
1328 val = left || right;
1333 left = arg_num_eval(arg->op.left);
1334 right = arg_num_eval(arg->op.right);
1336 val = left && right;
1341 left = arg_num_eval(arg->op.left);
1342 right = arg_num_eval(arg->op.right);
1343 switch (arg->op.op[1]) {
1348 val = left << right;
1351 val = left <= right;
1354 die("unknown op '%s'", arg->op.op);
1358 left = arg_num_eval(arg->op.left);
1359 right = arg_num_eval(arg->op.right);
1360 switch (arg->op.op[1]) {
1365 val = left >> right;
1368 val = left >= right;
1371 die("unknown op '%s'", arg->op.op);
1375 left = arg_num_eval(arg->op.left);
1376 right = arg_num_eval(arg->op.right);
1378 if (arg->op.op[1] != '=')
1379 die("unknown op '%s'", arg->op.op);
1381 val = left == right;
1384 left = arg_num_eval(arg->op.left);
1385 right = arg_num_eval(arg->op.right);
1387 switch (arg->op.op[1]) {
1389 val = left != right;
1392 die("unknown op '%s'", arg->op.op);
1396 die("unknown op '%s'", arg->op.op);
1401 case PRINT_FIELD ... PRINT_SYMBOL:
1404 die("invalid eval type %d", arg->type);
1410 static char *arg_eval (struct print_arg *arg)
1413 static char buf[20];
1415 switch (arg->type) {
1417 return arg->atom.atom;
1419 return arg_eval(arg->typecast.item);
1421 val = arg_num_eval(arg);
1422 sprintf(buf, "%lld", val);
1426 case PRINT_FIELD ... PRINT_SYMBOL:
1429 die("invalid eval type %d", arg->type);
1436 static enum event_type
1437 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1439 enum event_type type;
1440 struct print_arg *arg = NULL;
1441 struct print_flag_sym *field;
1447 type = read_token_item(&token);
1448 if (test_type_token(type, token, EVENT_OP, (char *)"{"))
1451 arg = malloc_or_die(sizeof(*arg));
1454 type = process_arg(event, arg, &token);
1455 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1458 field = malloc_or_die(sizeof(*field));
1459 memset(field, 0, sizeof(field));
1461 value = arg_eval(arg);
1462 field->value = strdup(value);
1465 type = process_arg(event, arg, &token);
1466 if (test_type_token(type, token, EVENT_OP, (char *)"}"))
1469 value = arg_eval(arg);
1470 field->str = strdup(value);
1475 list = &field->next;
1478 type = read_token_item(&token);
1479 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1491 static enum event_type
1492 process_flags(struct event *event, struct print_arg *arg, char **tok)
1494 struct print_arg *field;
1495 enum event_type type;
1498 memset(arg, 0, sizeof(*arg));
1499 arg->type = PRINT_FLAGS;
1501 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1504 field = malloc_or_die(sizeof(*field));
1506 type = process_arg(event, field, &token);
1507 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1510 arg->flags.field = field;
1512 type = read_token_item(&token);
1513 if (event_item_type(type)) {
1514 arg->flags.delim = token;
1515 type = read_token_item(&token);
1518 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1521 type = process_fields(event, &arg->flags.flags, &token);
1522 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1526 type = read_token_item(tok);
1534 static enum event_type
1535 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1537 struct print_arg *field;
1538 enum event_type type;
1541 memset(arg, 0, sizeof(*arg));
1542 arg->type = PRINT_SYMBOL;
1544 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1547 field = malloc_or_die(sizeof(*field));
1549 type = process_arg(event, field, &token);
1550 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1553 arg->symbol.field = field;
1555 type = process_fields(event, &arg->symbol.symbols, &token);
1556 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1560 type = read_token_item(tok);
1568 static enum event_type
1569 process_paren(struct event *event, struct print_arg *arg, char **tok)
1571 struct print_arg *item_arg;
1572 enum event_type type;
1575 type = process_arg(event, arg, &token);
1577 if (type == EVENT_ERROR)
1580 if (type == EVENT_OP)
1581 type = process_op(event, arg, &token);
1583 if (type == EVENT_ERROR)
1586 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
1592 type = read_token_item(&token);
1595 * If the next token is an item or another open paren, then
1596 * this was a typecast.
1598 if (event_item_type(type) ||
1599 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1601 /* make this a typecast and contine */
1603 /* prevous must be an atom */
1604 if (arg->type != PRINT_ATOM)
1605 die("previous needed to be PRINT_ATOM");
1607 item_arg = malloc_or_die(sizeof(*item_arg));
1609 arg->type = PRINT_TYPE;
1610 arg->typecast.type = arg->atom.atom;
1611 arg->typecast.item = item_arg;
1612 type = process_arg_token(event, item_arg, &token, type);
1621 static enum event_type
1622 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1624 enum event_type type;
1627 if (read_expected(EVENT_DELIM, (char *)"(") < 0)
1630 if (read_expect_type(EVENT_ITEM, &token) < 0)
1633 arg->type = PRINT_STRING;
1634 arg->string.string = token;
1635 arg->string.offset = -1;
1637 if (read_expected(EVENT_DELIM, (char *)")") < 0)
1640 type = read_token(&token);
1650 process_arg_token(struct event *event, struct print_arg *arg,
1651 char **tok, enum event_type type)
1660 if (strcmp(token, "REC") == 0) {
1662 type = process_entry(event, arg, &token);
1663 } else if (strcmp(token, "__print_flags") == 0) {
1665 type = process_flags(event, arg, &token);
1666 } else if (strcmp(token, "__print_symbolic") == 0) {
1668 type = process_symbols(event, arg, &token);
1669 } else if (strcmp(token, "__get_str") == 0) {
1671 type = process_str(event, arg, &token);
1674 /* test the next token */
1675 type = read_token_item(&token);
1677 /* atoms can be more than one token long */
1678 while (type == EVENT_ITEM) {
1679 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1681 strcat(atom, token);
1683 type = read_token_item(&token);
1686 /* todo, test for function */
1688 arg->type = PRINT_ATOM;
1689 arg->atom.atom = atom;
1694 arg->type = PRINT_ATOM;
1695 arg->atom.atom = token;
1696 type = read_token_item(&token);
1699 if (strcmp(token, "(") == 0) {
1701 type = process_paren(event, arg, &token);
1705 /* handle single ops */
1706 arg->type = PRINT_OP;
1708 arg->op.left = NULL;
1709 type = process_op(event, arg, &token);
1713 case EVENT_ERROR ... EVENT_NEWLINE:
1715 die("unexpected type %d", type);
1722 static int event_read_print_args(struct event *event, struct print_arg **list)
1724 enum event_type type = EVENT_ERROR;
1725 struct print_arg *arg;
1730 if (type == EVENT_NEWLINE) {
1732 type = read_token_item(&token);
1736 arg = malloc_or_die(sizeof(*arg));
1737 memset(arg, 0, sizeof(*arg));
1739 type = process_arg(event, arg, &token);
1741 if (type == EVENT_ERROR) {
1749 if (type == EVENT_OP) {
1750 type = process_op(event, arg, &token);
1755 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1762 } while (type != EVENT_NONE);
1764 if (type != EVENT_NONE)
1770 static int event_read_print(struct event *event)
1772 enum event_type type;
1776 if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
1779 if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
1782 if (read_expected(EVENT_OP, (char *)":") < 0)
1785 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1789 event->print_fmt.format = token;
1790 event->print_fmt.args = NULL;
1792 /* ok to have no arg */
1793 type = read_token_item(&token);
1795 if (type == EVENT_NONE)
1798 /* Handle concatination of print lines */
1799 if (type == EVENT_DQUOTE) {
1802 cat = malloc_or_die(strlen(event->print_fmt.format) +
1804 strcpy(cat, event->print_fmt.format);
1807 free_token(event->print_fmt.format);
1808 event->print_fmt.format = NULL;
1813 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1818 ret = event_read_print_args(event, &event->print_fmt.args);
1829 static struct format_field *
1830 find_common_field(struct event *event, const char *name)
1832 struct format_field *format;
1834 for (format = event->format.common_fields;
1835 format; format = format->next) {
1836 if (strcmp(format->name, name) == 0)
1843 static struct format_field *
1844 find_field(struct event *event, const char *name)
1846 struct format_field *format;
1848 for (format = event->format.fields;
1849 format; format = format->next) {
1850 if (strcmp(format->name, name) == 0)
1857 static struct format_field *
1858 find_any_field(struct event *event, const char *name)
1860 struct format_field *format;
1862 format = find_common_field(event, name);
1865 return find_field(event, name);
1868 static unsigned long long read_size(void *ptr, int size)
1872 return *(unsigned char *)ptr;
1874 return data2host2(ptr);
1876 return data2host4(ptr);
1878 return data2host8(ptr);
1886 raw_field_value(struct event *event, const char *name, void *data)
1888 struct format_field *field;
1890 field = find_any_field(event, name);
1894 return read_size(data + field->offset, field->size);
1897 void *raw_field_ptr(struct event *event, const char *name, void *data)
1899 struct format_field *field;
1901 field = find_any_field(event, name);
1905 return data + field->offset;
1908 static int get_common_info(const char *type, int *offset, int *size)
1910 struct event *event;
1911 struct format_field *field;
1914 * All events should have the same common elements.
1915 * Pick any event to find where the type is;
1918 die("no event_list!");
1921 field = find_common_field(event, type);
1923 die("field '%s' not found", type);
1925 *offset = field->offset;
1926 *size = field->size;
1931 int trace_parse_common_type(void *data)
1933 static int type_offset;
1934 static int type_size;
1938 ret = get_common_info("common_type",
1944 return read_size(data + type_offset, type_size);
1947 static int parse_common_pid(void *data)
1949 static int pid_offset;
1950 static int pid_size;
1954 ret = get_common_info("common_pid",
1961 return read_size(data + pid_offset, pid_size);
1964 struct event *trace_find_event(int id)
1966 struct event *event;
1968 for (event = event_list; event; event = event->next) {
1969 if (event->id == id)
1975 static unsigned long long eval_num_arg(void *data, int size,
1976 struct event *event, struct print_arg *arg)
1978 unsigned long long val = 0;
1979 unsigned long long left, right;
1980 struct print_arg *larg;
1982 switch (arg->type) {
1987 return strtoull(arg->atom.atom, NULL, 0);
1989 if (!arg->field.field) {
1990 arg->field.field = find_any_field(event, arg->field.name);
1991 if (!arg->field.field)
1992 die("field %s not found", arg->field.name);
1994 /* must be a number */
1995 val = read_size(data + arg->field.field->offset,
1996 arg->field.field->size);
2002 return eval_num_arg(data, size, event, arg->typecast.item);
2007 if (strcmp(arg->op.op, "[") == 0) {
2009 * Arrays are special, since we don't want
2010 * to read the arg as is.
2012 if (arg->op.left->type != PRINT_FIELD)
2013 goto default_op; /* oops, all bets off */
2014 larg = arg->op.left;
2015 if (!larg->field.field) {
2017 find_any_field(event, larg->field.name);
2018 if (!larg->field.field)
2019 die("field %s not found", larg->field.name);
2021 right = eval_num_arg(data, size, event, arg->op.right);
2022 val = read_size(data + larg->field.field->offset +
2023 right * long_size, long_size);
2027 left = eval_num_arg(data, size, event, arg->op.left);
2028 right = eval_num_arg(data, size, event, arg->op.right);
2029 switch (arg->op.op[0]) {
2032 val = left || right;
2038 val = left && right;
2043 switch (arg->op.op[1]) {
2048 val = left << right;
2051 val = left <= right;
2054 die("unknown op '%s'", arg->op.op);
2058 switch (arg->op.op[1]) {
2063 val = left >> right;
2066 val = left >= right;
2069 die("unknown op '%s'", arg->op.op);
2073 if (arg->op.op[1] != '=')
2074 die("unknown op '%s'", arg->op.op);
2075 val = left == right;
2078 die("unknown op '%s'", arg->op.op);
2081 default: /* not sure what to do there */
2089 unsigned long long value;
2092 static const struct flag flags[] = {
2093 { "HI_SOFTIRQ", 0 },
2094 { "TIMER_SOFTIRQ", 1 },
2095 { "NET_TX_SOFTIRQ", 2 },
2096 { "NET_RX_SOFTIRQ", 3 },
2097 { "BLOCK_SOFTIRQ", 4 },
2098 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2099 { "TASKLET_SOFTIRQ", 6 },
2100 { "SCHED_SOFTIRQ", 7 },
2101 { "HRTIMER_SOFTIRQ", 8 },
2102 { "RCU_SOFTIRQ", 9 },
2104 { "HRTIMER_NORESTART", 0 },
2105 { "HRTIMER_RESTART", 1 },
2108 static unsigned long long eval_flag(const char *flag)
2113 * Some flags in the format files do not get converted.
2114 * If the flag is not numeric, see if it is something that
2115 * we already know about.
2117 if (isdigit(flag[0]))
2118 return strtoull(flag, NULL, 0);
2120 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2121 if (strcmp(flags[i].name, flag) == 0)
2122 return flags[i].value;
2127 static void print_str_arg(void *data, int size,
2128 struct event *event, struct print_arg *arg)
2130 struct print_flag_sym *flag;
2131 unsigned long long val, fval;
2135 switch (arg->type) {
2140 printf("%s", arg->atom.atom);
2143 if (!arg->field.field) {
2144 arg->field.field = find_any_field(event, arg->field.name);
2145 if (!arg->field.field)
2146 die("field %s not found", arg->field.name);
2148 str = malloc_or_die(arg->field.field->size + 1);
2149 memcpy(str, data + arg->field.field->offset,
2150 arg->field.field->size);
2151 str[arg->field.field->size] = 0;
2156 val = eval_num_arg(data, size, event, arg->flags.field);
2158 for (flag = arg->flags.flags; flag; flag = flag->next) {
2159 fval = eval_flag(flag->value);
2160 if (!val && !fval) {
2161 printf("%s", flag->str);
2164 if (fval && (val & fval) == fval) {
2165 if (print && arg->flags.delim)
2166 printf("%s", arg->flags.delim);
2167 printf("%s", flag->str);
2174 val = eval_num_arg(data, size, event, arg->symbol.field);
2175 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2176 fval = eval_flag(flag->value);
2178 printf("%s", flag->str);
2186 case PRINT_STRING: {
2189 if (arg->string.offset == -1) {
2190 struct format_field *f;
2192 f = find_any_field(event, arg->string.string);
2193 arg->string.offset = f->offset;
2195 str_offset = *(int *)(data + arg->string.offset);
2196 str_offset &= 0xffff;
2197 printf("%s", ((char *)data) + str_offset);
2202 * The only op for string should be ? :
2204 if (arg->op.op[0] != '?')
2206 val = eval_num_arg(data, size, event, arg->op.left);
2208 print_str_arg(data, size, event, arg->op.right->op.left);
2210 print_str_arg(data, size, event, arg->op.right->op.right);
2218 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2220 static struct format_field *field, *ip_field;
2221 struct print_arg *args, *arg, **next;
2222 unsigned long long ip, val;
2227 field = find_field(event, "buf");
2229 die("can't find buffer field for binary printk");
2230 ip_field = find_field(event, "ip");
2232 die("can't find ip field for binary printk");
2235 ip = read_size(data + ip_field->offset, ip_field->size);
2238 * The first arg is the IP pointer.
2240 args = malloc_or_die(sizeof(*args));
2245 arg->type = PRINT_ATOM;
2246 arg->atom.atom = malloc_or_die(32);
2247 sprintf(arg->atom.atom, "%lld", ip);
2249 /* skip the first "%pf : " */
2250 for (ptr = fmt + 6, bptr = data + field->offset;
2251 bptr < data + size && *ptr; ptr++) {
2275 /* the pointers are always 4 bytes aligned */
2276 bptr = (void *)(((unsigned long)bptr + 3) &
2288 val = read_size(bptr, ls);
2290 arg = malloc_or_die(sizeof(*arg));
2292 arg->type = PRINT_ATOM;
2293 arg->atom.atom = malloc_or_die(32);
2294 sprintf(arg->atom.atom, "%lld", val);
2299 arg = malloc_or_die(sizeof(*arg));
2301 arg->type = PRINT_STRING;
2302 arg->string.string = strdup(bptr);
2303 bptr += strlen(bptr) + 1;
2315 static void free_args(struct print_arg *args)
2317 struct print_arg *next;
2322 if (args->type == PRINT_ATOM)
2323 free(args->atom.atom);
2325 free(args->string.string);
2331 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2333 unsigned long long addr;
2334 static struct format_field *field;
2335 struct printk_map *printk;
2340 field = find_field(event, "fmt");
2342 die("can't find format field for binary printk");
2343 printf("field->offset = %d size=%d\n", field->offset, field->size);
2346 addr = read_size(data + field->offset, field->size);
2348 printk = find_printk(addr);
2350 format = malloc_or_die(45);
2351 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2357 /* Remove any quotes. */
2360 format = malloc_or_die(strlen(p) + 10);
2361 sprintf(format, "%s : %s", "%pf", p);
2362 /* remove ending quotes and new line since we will add one too */
2363 p = format + strlen(format) - 1;
2368 if (strcmp(p, "\\n") == 0)
2374 static void pretty_print(void *data, int size, struct event *event)
2376 struct print_fmt *print_fmt = &event->print_fmt;
2377 struct print_arg *arg = print_fmt->args;
2378 struct print_arg *args = NULL;
2379 const char *ptr = print_fmt->format;
2380 unsigned long long val;
2381 struct func_map *func;
2382 const char *saveptr;
2383 char *bprint_fmt = NULL;
2389 if (event->flags & EVENT_FL_ISFUNC)
2390 ptr = " %pF <-- %pF";
2392 if (event->flags & EVENT_FL_ISBPRINT) {
2393 bprint_fmt = get_bprint_format(data, size, event);
2394 args = make_bprint_args(bprint_fmt, data, size, event);
2399 for (; *ptr; ptr++) {
2421 } else if (*ptr == '%') {
2446 if (*(ptr+1) == 'F' ||
2459 die("no argument match");
2461 len = ((unsigned long)ptr + 1) -
2462 (unsigned long)saveptr;
2464 /* should never happen */
2468 memcpy(format, saveptr, len);
2471 val = eval_num_arg(data, size, event, arg);
2475 func = find_func(val);
2477 printf("%s", func->func);
2478 if (show_func == 'F')
2486 printf(format, (int)val);
2489 printf(format, (long)val);
2492 printf(format, (long long)val);
2495 die("bad count (%d)", ls);
2500 die("no matching argument");
2502 print_str_arg(data, size, event, arg);
2506 printf(">%c<", *ptr);
2519 static inline int log10_cpu(int nb)
2528 /* taken from Linux, written by Frederic Weisbecker */
2529 static void print_graph_cpu(int cpu)
2532 int log10_this = log10_cpu(cpu);
2533 int log10_all = log10_cpu(cpus);
2537 * Start with a space character - to make it stand out
2538 * to the right a bit when trace output is pasted into
2544 * Tricky - we space the CPU field according to the max
2545 * number of online CPUs. On a 2-cpu system it would take
2546 * a maximum of 1 digit - on a 128 cpu system it would
2547 * take up to 3 digits:
2549 for (i = 0; i < log10_all - log10_this; i++)
2552 printf("%d) ", cpu);
2555 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2556 #define TRACE_GRAPH_INDENT 2
2558 static void print_graph_proc(int pid, const char *comm)
2560 /* sign + log10(MAX_INT) + '\0' */
2566 sprintf(pid_str, "%d", pid);
2568 /* 1 stands for the "-" character */
2569 len = strlen(comm) + strlen(pid_str) + 1;
2571 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2572 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2574 /* First spaces to align center */
2575 for (i = 0; i < spaces / 2; i++)
2578 printf("%s-%s", comm, pid_str);
2580 /* Last spaces to align center */
2581 for (i = 0; i < spaces - (spaces / 2); i++)
2585 static struct record *
2586 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2587 struct record *next)
2589 struct format_field *field;
2590 struct event *event;
2595 type = trace_parse_common_type(next->data);
2596 event = trace_find_event(type);
2600 if (!(event->flags & EVENT_FL_ISFUNCRET))
2603 pid = parse_common_pid(next->data);
2604 field = find_field(event, "func");
2606 die("function return does not have field func");
2608 val = read_size(next->data + field->offset, field->size);
2610 if (cur_pid != pid || cur_func != val)
2613 /* this is a leaf, now advance the iterator */
2614 return trace_read_data(cpu);
2617 /* Signal a overhead of time execution to the output */
2618 static void print_graph_overhead(unsigned long long duration)
2620 /* Non nested entry or return */
2621 if (duration == ~0ULL)
2622 return (void)printf(" ");
2624 /* Duration exceeded 100 msecs */
2625 if (duration > 100000ULL)
2626 return (void)printf("! ");
2628 /* Duration exceeded 10 msecs */
2629 if (duration > 10000ULL)
2630 return (void)printf("+ ");
2635 static void print_graph_duration(unsigned long long duration)
2637 unsigned long usecs = duration / 1000;
2638 unsigned long nsecs_rem = duration % 1000;
2639 /* log10(ULONG_MAX) + '\0' */
2645 sprintf(msecs_str, "%lu", usecs);
2648 len = printf("%lu", usecs);
2650 /* Print nsecs (we don't want to exceed 7 numbers) */
2652 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2653 len += printf(".%s", nsecs_str);
2658 /* Print remaining spaces to fit the row's width */
2659 for (i = len; i < 7; i++)
2666 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2668 unsigned long long rettime, calltime;
2669 unsigned long long duration, depth;
2670 unsigned long long val;
2671 struct format_field *field;
2672 struct func_map *func;
2673 struct event *ret_event;
2677 type = trace_parse_common_type(ret_rec->data);
2678 ret_event = trace_find_event(type);
2680 field = find_field(ret_event, "rettime");
2682 die("can't find rettime in return graph");
2683 rettime = read_size(ret_rec->data + field->offset, field->size);
2685 field = find_field(ret_event, "calltime");
2687 die("can't find rettime in return graph");
2688 calltime = read_size(ret_rec->data + field->offset, field->size);
2690 duration = rettime - calltime;
2693 print_graph_overhead(duration);
2696 print_graph_duration(duration);
2698 field = find_field(event, "depth");
2700 die("can't find depth in entry graph");
2701 depth = read_size(data + field->offset, field->size);
2704 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2707 field = find_field(event, "func");
2709 die("can't find func in entry graph");
2710 val = read_size(data + field->offset, field->size);
2711 func = find_func(val);
2714 printf("%s();", func->func);
2716 printf("%llx();", val);
2719 static void print_graph_nested(struct event *event, void *data)
2721 struct format_field *field;
2722 unsigned long long depth;
2723 unsigned long long val;
2724 struct func_map *func;
2728 print_graph_overhead(-1);
2733 field = find_field(event, "depth");
2735 die("can't find depth in entry graph");
2736 depth = read_size(data + field->offset, field->size);
2739 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2742 field = find_field(event, "func");
2744 die("can't find func in entry graph");
2745 val = read_size(data + field->offset, field->size);
2746 func = find_func(val);
2749 printf("%s() {", func->func);
2751 printf("%llx() {", val);
2755 pretty_print_func_ent(void *data, int size, struct event *event,
2756 int cpu, int pid, const char *comm,
2757 unsigned long secs, unsigned long usecs)
2759 struct format_field *field;
2764 printf("%5lu.%06lu | ", secs, usecs);
2766 print_graph_cpu(cpu);
2767 print_graph_proc(pid, comm);
2771 field = find_field(event, "func");
2773 die("function entry does not have func field");
2775 val = read_size(data + field->offset, field->size);
2778 * peek_data may unmap the data pointer. Copy it first.
2780 copy_data = malloc_or_die(size);
2781 memcpy(copy_data, data, size);
2784 rec = trace_peek_data(cpu);
2786 rec = get_return_for_leaf(cpu, pid, val, rec);
2788 print_graph_entry_leaf(event, data, rec);
2792 print_graph_nested(event, data);
2798 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2799 int cpu, int pid, const char *comm,
2800 unsigned long secs, unsigned long usecs)
2802 unsigned long long rettime, calltime;
2803 unsigned long long duration, depth;
2804 struct format_field *field;
2807 printf("%5lu.%06lu | ", secs, usecs);
2809 print_graph_cpu(cpu);
2810 print_graph_proc(pid, comm);
2814 field = find_field(event, "rettime");
2816 die("can't find rettime in return graph");
2817 rettime = read_size(data + field->offset, field->size);
2819 field = find_field(event, "calltime");
2821 die("can't find calltime in return graph");
2822 calltime = read_size(data + field->offset, field->size);
2824 duration = rettime - calltime;
2827 print_graph_overhead(duration);
2830 print_graph_duration(duration);
2832 field = find_field(event, "depth");
2834 die("can't find depth in entry graph");
2835 depth = read_size(data + field->offset, field->size);
2838 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2845 pretty_print_func_graph(void *data, int size, struct event *event,
2846 int cpu, int pid, const char *comm,
2847 unsigned long secs, unsigned long usecs)
2849 if (event->flags & EVENT_FL_ISFUNCENT)
2850 pretty_print_func_ent(data, size, event,
2851 cpu, pid, comm, secs, usecs);
2852 else if (event->flags & EVENT_FL_ISFUNCRET)
2853 pretty_print_func_ret(data, size, event,
2854 cpu, pid, comm, secs, usecs);
2858 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2861 struct event *event;
2863 unsigned long usecs;
2867 secs = nsecs / NSECS_PER_SEC;
2868 nsecs -= secs * NSECS_PER_SEC;
2869 usecs = nsecs / NSECS_PER_USEC;
2871 type = trace_parse_common_type(data);
2873 event = trace_find_event(type);
2875 warning("ug! no event found for type %d", type);
2879 pid = parse_common_pid(data);
2881 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2882 return pretty_print_func_graph(data, size, event, cpu,
2883 pid, comm, secs, usecs);
2885 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
2887 secs, nsecs, event->name);
2889 if (event->flags & EVENT_FL_FAILED) {
2890 printf("EVENT '%s' FAILED TO PARSE\n",
2895 pretty_print(data, size, event);
2899 static void print_fields(struct print_flag_sym *field)
2901 printf("{ %s, %s }", field->value, field->str);
2904 print_fields(field->next);
2908 static void print_args(struct print_arg *args)
2910 int print_paren = 1;
2912 switch (args->type) {
2917 printf("%s", args->atom.atom);
2920 printf("REC->%s", args->field.name);
2923 printf("__print_flags(");
2924 print_args(args->flags.field);
2925 printf(", %s, ", args->flags.delim);
2926 print_fields(args->flags.flags);
2930 printf("__print_symbolic(");
2931 print_args(args->symbol.field);
2933 print_fields(args->symbol.symbols);
2937 printf("__get_str(%s)", args->string.string);
2940 printf("(%s)", args->typecast.type);
2941 print_args(args->typecast.item);
2944 if (strcmp(args->op.op, ":") == 0)
2948 print_args(args->op.left);
2949 printf(" %s ", args->op.op);
2950 print_args(args->op.right);
2955 /* we should warn... */
2960 print_args(args->next);
2964 static void parse_header_field(char *field,
2965 int *offset, int *size)
2970 if (read_expected(EVENT_ITEM, (char *)"field") < 0)
2972 if (read_expected(EVENT_OP, (char *)":") < 0)
2976 if (read_expect_type(EVENT_ITEM, &token) < 0)
2980 if (read_expected(EVENT_ITEM, field) < 0)
2982 if (read_expected(EVENT_OP, (char *)";") < 0)
2984 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
2986 if (read_expected(EVENT_OP, (char *)":") < 0)
2988 if (read_expect_type(EVENT_ITEM, &token) < 0)
2990 *offset = atoi(token);
2992 if (read_expected(EVENT_OP, (char *)";") < 0)
2994 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
2996 if (read_expected(EVENT_OP, (char *)":") < 0)
2998 if (read_expect_type(EVENT_ITEM, &token) < 0)
3000 *size = atoi(token);
3002 if (read_expected(EVENT_OP, (char *)";") < 0)
3004 type = read_token(&token);
3005 if (type != EVENT_NEWLINE) {
3006 /* newer versions of the kernel have a "signed" type */
3007 if (type != EVENT_ITEM)
3010 if (strcmp(token, (char *)"signed") != 0)
3015 if (read_expected(EVENT_OP, (char *)":") < 0)
3018 if (read_expect_type(EVENT_ITEM, &token))
3022 if (read_expected(EVENT_OP, (char *)";") < 0)
3025 if (read_expect_type(EVENT_NEWLINE, &token))
3032 int parse_header_page(char *buf, unsigned long size)
3034 init_input_buf(buf, size);
3036 parse_header_field((char *)"timestamp", &header_page_ts_offset,
3037 &header_page_ts_size);
3038 parse_header_field((char *)"commit", &header_page_size_offset,
3039 &header_page_size_size);
3040 parse_header_field((char *)"data", &header_page_data_offset,
3041 &header_page_data_size);
3046 int parse_ftrace_file(char *buf, unsigned long size)
3048 struct format_field *field;
3049 struct print_arg *arg, **list;
3050 struct event *event;
3053 init_input_buf(buf, size);
3055 event = alloc_event();
3059 event->flags |= EVENT_FL_ISFTRACE;
3061 event->name = event_read_name();
3063 die("failed to read ftrace event name");
3065 if (strcmp(event->name, "function") == 0)
3066 event->flags |= EVENT_FL_ISFUNC;
3068 else if (strcmp(event->name, "funcgraph_entry") == 0)
3069 event->flags |= EVENT_FL_ISFUNCENT;
3071 else if (strcmp(event->name, "funcgraph_exit") == 0)
3072 event->flags |= EVENT_FL_ISFUNCRET;
3074 else if (strcmp(event->name, "bprint") == 0)
3075 event->flags |= EVENT_FL_ISBPRINT;
3077 event->id = event_read_id();
3079 die("failed to read ftrace event id");
3083 ret = event_read_format(event);
3085 die("failed to read ftrace event format");
3087 ret = event_read_print(event);
3089 die("failed to read ftrace event print fmt");
3092 * The arguments for ftrace files are parsed by the fields.
3093 * Set up the fields as their arguments.
3095 list = &event->print_fmt.args;
3096 for (field = event->format.fields; field; field = field->next) {
3097 arg = malloc_or_die(sizeof(*arg));
3098 memset(arg, 0, sizeof(*arg));
3101 arg->type = PRINT_FIELD;
3102 arg->field.name = field->name;
3103 arg->field.field = field;
3108 int parse_event_file(char *buf, unsigned long size, char *sys)
3110 struct event *event;
3113 init_input_buf(buf, size);
3115 event = alloc_event();
3119 event->name = event_read_name();
3121 die("failed to read event name");
3123 event->id = event_read_id();
3125 die("failed to read event id");
3127 ret = event_read_format(event);
3129 warning("failed to read event format for %s", event->name);
3133 ret = event_read_print(event);
3135 warning("failed to read event print fmt for %s", event->name);
3139 event->system = strdup(sys);
3141 #define PRINT_ARGS 0
3142 if (PRINT_ARGS && event->print_fmt.args)
3143 print_args(event->print_fmt.args);
3149 event->flags |= EVENT_FL_FAILED;
3150 /* still add it even if it failed */
3155 void parse_set_info(int nr_cpus, int long_sz)
3158 long_size = long_sz;