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 ret = sscanf(line, "%as : %as",
294 (float *)(void *)&addr_str, /* workaround gcc warning */
295 (float *)(void *)&item->printk);
296 item->addr = strtoull(addr_str, NULL, 16);
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 die("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 die("Error: expected type %d but read %d",
632 if (strcmp(token, expect_tok) != 0) {
633 die("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, (char *)"name") < 0)
691 if (read_expected(EVENT_OP, (char *)":") < 0)
694 if (read_expect_type(EVENT_ITEM, &token) < 0)
704 static int event_read_id(void)
709 if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
712 if (read_expected(EVENT_OP, (char *)":") < 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, (char *)"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, (char *)":") < 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, (char *)";"))
899 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
902 if (read_expected(EVENT_OP, (char *)":") < 0)
905 if (read_expect_type(EVENT_ITEM, &token))
907 field->offset = strtoul(token, NULL, 0);
910 if (read_expected(EVENT_OP, (char *)";") < 0)
913 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
916 if (read_expected(EVENT_OP, (char *)":") < 0)
919 if (read_expect_type(EVENT_ITEM, &token))
921 field->size = strtoul(token, NULL, 0);
924 if (read_expected(EVENT_OP, (char *)";") < 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, (char *)"signed"))
935 if (read_expected(EVENT_OP, (char *)":") < 0)
938 if (read_expect_type(EVENT_ITEM, &token))
941 /* add signed type */
944 if (read_expected(EVENT_OP, (char *)";") < 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, (char *)"format") < 0)
976 if (read_expected(EVENT_OP, (char *)":") < 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, (char *)":"))
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, (char *)"]"))
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 die("unknown op '%s'", token);
1262 /* 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, (char *)"->") < 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, (char *)"{"))
1453 arg = malloc_or_die(sizeof(*arg));
1456 type = process_arg(event, arg, &token);
1457 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
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, (char *)"}"))
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, (char *)"(") < 0)
1506 field = malloc_or_die(sizeof(*field));
1508 type = process_arg(event, field, &token);
1509 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
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, (char *)","))
1523 type = process_fields(event, &arg->flags.flags, &token);
1524 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
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, (char *)"(") < 0)
1549 field = malloc_or_die(sizeof(*field));
1551 type = process_arg(event, field, &token);
1552 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1555 arg->symbol.field = field;
1557 type = process_fields(event, &arg->symbol.symbols, &token);
1558 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
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, (char *)")")) {
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, (char *)"(") < 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, (char *)")") < 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, (char *)"print") < 0)
1781 if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
1784 if (read_expected(EVENT_OP, (char *)":") < 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, (char *)","))
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 int trace_parse_common_type(void *data)
1935 static int type_offset;
1936 static int type_size;
1940 ret = get_common_info("common_type",
1946 return read_size(data + type_offset, type_size);
1949 static int parse_common_pid(void *data)
1951 static int pid_offset;
1952 static int pid_size;
1956 ret = get_common_info("common_pid",
1963 return read_size(data + pid_offset, pid_size);
1966 struct event *trace_find_event(int id)
1968 struct event *event;
1970 for (event = event_list; event; event = event->next) {
1971 if (event->id == id)
1977 static unsigned long long eval_num_arg(void *data, int size,
1978 struct event *event, struct print_arg *arg)
1980 unsigned long long val = 0;
1981 unsigned long long left, right;
1982 struct print_arg *larg;
1984 switch (arg->type) {
1989 return strtoull(arg->atom.atom, NULL, 0);
1991 if (!arg->field.field) {
1992 arg->field.field = find_any_field(event, arg->field.name);
1993 if (!arg->field.field)
1994 die("field %s not found", arg->field.name);
1996 /* must be a number */
1997 val = read_size(data + arg->field.field->offset,
1998 arg->field.field->size);
2004 return eval_num_arg(data, size, event, arg->typecast.item);
2009 if (strcmp(arg->op.op, "[") == 0) {
2011 * Arrays are special, since we don't want
2012 * to read the arg as is.
2014 if (arg->op.left->type != PRINT_FIELD)
2015 goto default_op; /* oops, all bets off */
2016 larg = arg->op.left;
2017 if (!larg->field.field) {
2019 find_any_field(event, larg->field.name);
2020 if (!larg->field.field)
2021 die("field %s not found", larg->field.name);
2023 right = eval_num_arg(data, size, event, arg->op.right);
2024 val = read_size(data + larg->field.field->offset +
2025 right * long_size, long_size);
2029 left = eval_num_arg(data, size, event, arg->op.left);
2030 right = eval_num_arg(data, size, event, arg->op.right);
2031 switch (arg->op.op[0]) {
2034 val = left || right;
2040 val = left && right;
2045 switch (arg->op.op[1]) {
2050 val = left << right;
2053 val = left <= right;
2056 die("unknown op '%s'", arg->op.op);
2060 switch (arg->op.op[1]) {
2065 val = left >> right;
2068 val = left >= right;
2071 die("unknown op '%s'", arg->op.op);
2075 if (arg->op.op[1] != '=')
2076 die("unknown op '%s'", arg->op.op);
2077 val = left == right;
2080 die("unknown op '%s'", arg->op.op);
2083 default: /* not sure what to do there */
2091 unsigned long long value;
2094 static const struct flag flags[] = {
2095 { "HI_SOFTIRQ", 0 },
2096 { "TIMER_SOFTIRQ", 1 },
2097 { "NET_TX_SOFTIRQ", 2 },
2098 { "NET_RX_SOFTIRQ", 3 },
2099 { "BLOCK_SOFTIRQ", 4 },
2100 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2101 { "TASKLET_SOFTIRQ", 6 },
2102 { "SCHED_SOFTIRQ", 7 },
2103 { "HRTIMER_SOFTIRQ", 8 },
2104 { "RCU_SOFTIRQ", 9 },
2106 { "HRTIMER_NORESTART", 0 },
2107 { "HRTIMER_RESTART", 1 },
2110 static unsigned long long eval_flag(const char *flag)
2115 * Some flags in the format files do not get converted.
2116 * If the flag is not numeric, see if it is something that
2117 * we already know about.
2119 if (isdigit(flag[0]))
2120 return strtoull(flag, NULL, 0);
2122 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2123 if (strcmp(flags[i].name, flag) == 0)
2124 return flags[i].value;
2129 static void print_str_arg(void *data, int size,
2130 struct event *event, struct print_arg *arg)
2132 struct print_flag_sym *flag;
2133 unsigned long long val, fval;
2137 switch (arg->type) {
2142 printf("%s", arg->atom.atom);
2145 if (!arg->field.field) {
2146 arg->field.field = find_any_field(event, arg->field.name);
2147 if (!arg->field.field)
2148 die("field %s not found", arg->field.name);
2150 str = malloc_or_die(arg->field.field->size + 1);
2151 memcpy(str, data + arg->field.field->offset,
2152 arg->field.field->size);
2153 str[arg->field.field->size] = 0;
2158 val = eval_num_arg(data, size, event, arg->flags.field);
2160 for (flag = arg->flags.flags; flag; flag = flag->next) {
2161 fval = eval_flag(flag->value);
2162 if (!val && !fval) {
2163 printf("%s", flag->str);
2166 if (fval && (val & fval) == fval) {
2167 if (print && arg->flags.delim)
2168 printf("%s", arg->flags.delim);
2169 printf("%s", flag->str);
2176 val = eval_num_arg(data, size, event, arg->symbol.field);
2177 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2178 fval = eval_flag(flag->value);
2180 printf("%s", flag->str);
2188 case PRINT_STRING: {
2191 if (arg->string.offset == -1) {
2192 struct format_field *f;
2194 f = find_any_field(event, arg->string.string);
2195 arg->string.offset = f->offset;
2197 str_offset = *(int *)(data + arg->string.offset);
2198 str_offset &= 0xffff;
2199 printf("%s", ((char *)data) + str_offset);
2204 * The only op for string should be ? :
2206 if (arg->op.op[0] != '?')
2208 val = eval_num_arg(data, size, event, arg->op.left);
2210 print_str_arg(data, size, event, arg->op.right->op.left);
2212 print_str_arg(data, size, event, arg->op.right->op.right);
2220 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2222 static struct format_field *field, *ip_field;
2223 struct print_arg *args, *arg, **next;
2224 unsigned long long ip, val;
2229 field = find_field(event, "buf");
2231 die("can't find buffer field for binary printk");
2232 ip_field = find_field(event, "ip");
2234 die("can't find ip field for binary printk");
2237 ip = read_size(data + ip_field->offset, ip_field->size);
2240 * The first arg is the IP pointer.
2242 args = malloc_or_die(sizeof(*args));
2247 arg->type = PRINT_ATOM;
2248 arg->atom.atom = malloc_or_die(32);
2249 sprintf(arg->atom.atom, "%lld", ip);
2251 /* skip the first "%pf : " */
2252 for (ptr = fmt + 6, bptr = data + field->offset;
2253 bptr < data + size && *ptr; ptr++) {
2277 bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
2289 val = read_size(bptr, ls);
2291 arg = malloc_or_die(sizeof(*arg));
2293 arg->type = PRINT_ATOM;
2294 arg->atom.atom = malloc_or_die(32);
2295 sprintf(arg->atom.atom, "%lld", val);
2300 arg = malloc_or_die(sizeof(*arg));
2302 arg->type = PRINT_STRING;
2303 arg->string.string = strdup(bptr);
2304 bptr += strlen(bptr) + 1;
2316 static void free_args(struct print_arg *args)
2318 struct print_arg *next;
2323 if (args->type == PRINT_ATOM)
2324 free(args->atom.atom);
2326 free(args->string.string);
2332 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2334 unsigned long long addr;
2335 static struct format_field *field;
2336 struct printk_map *printk;
2341 field = find_field(event, "fmt");
2343 die("can't find format field for binary printk");
2344 printf("field->offset = %d size=%d\n", field->offset, field->size);
2347 addr = read_size(data + field->offset, field->size);
2349 printk = find_printk(addr);
2351 format = malloc_or_die(45);
2352 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2358 /* Remove any quotes. */
2361 format = malloc_or_die(strlen(p) + 10);
2362 sprintf(format, "%s : %s", "%pf", p);
2363 /* remove ending quotes and new line since we will add one too */
2364 p = format + strlen(format) - 1;
2369 if (strcmp(p, "\\n") == 0)
2375 static void pretty_print(void *data, int size, struct event *event)
2377 struct print_fmt *print_fmt = &event->print_fmt;
2378 struct print_arg *arg = print_fmt->args;
2379 struct print_arg *args = NULL;
2380 const char *ptr = print_fmt->format;
2381 unsigned long long val;
2382 struct func_map *func;
2383 const char *saveptr;
2384 char *bprint_fmt = NULL;
2390 if (event->flags & EVENT_FL_ISFUNC)
2391 ptr = " %pF <-- %pF";
2393 if (event->flags & EVENT_FL_ISBPRINT) {
2394 bprint_fmt = get_bprint_format(data, size, event);
2395 args = make_bprint_args(bprint_fmt, data, size, event);
2400 for (; *ptr; ptr++) {
2422 } else if (*ptr == '%') {
2447 if (*(ptr+1) == 'F' ||
2460 die("no argument match");
2462 len = ((unsigned long)ptr + 1) -
2463 (unsigned long)saveptr;
2465 /* should never happen */
2469 memcpy(format, saveptr, len);
2472 val = eval_num_arg(data, size, event, arg);
2476 func = find_func(val);
2478 printf("%s", func->func);
2479 if (show_func == 'F')
2487 printf(format, (int)val);
2490 printf(format, (long)val);
2493 printf(format, (long long)val);
2496 die("bad count (%d)", ls);
2501 die("no matching argument");
2503 print_str_arg(data, size, event, arg);
2507 printf(">%c<", *ptr);
2520 static inline int log10_cpu(int nb)
2529 /* taken from Linux, written by Frederic Weisbecker */
2530 static void print_graph_cpu(int cpu)
2533 int log10_this = log10_cpu(cpu);
2534 int log10_all = log10_cpu(cpus);
2538 * Start with a space character - to make it stand out
2539 * to the right a bit when trace output is pasted into
2545 * Tricky - we space the CPU field according to the max
2546 * number of online CPUs. On a 2-cpu system it would take
2547 * a maximum of 1 digit - on a 128 cpu system it would
2548 * take up to 3 digits:
2550 for (i = 0; i < log10_all - log10_this; i++)
2553 printf("%d) ", cpu);
2556 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2557 #define TRACE_GRAPH_INDENT 2
2559 static void print_graph_proc(int pid, const char *comm)
2561 /* sign + log10(MAX_INT) + '\0' */
2567 sprintf(pid_str, "%d", pid);
2569 /* 1 stands for the "-" character */
2570 len = strlen(comm) + strlen(pid_str) + 1;
2572 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2573 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2575 /* First spaces to align center */
2576 for (i = 0; i < spaces / 2; i++)
2579 printf("%s-%s", comm, pid_str);
2581 /* Last spaces to align center */
2582 for (i = 0; i < spaces - (spaces / 2); i++)
2586 static struct record *
2587 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2588 struct record *next)
2590 struct format_field *field;
2591 struct event *event;
2596 type = trace_parse_common_type(next->data);
2597 event = trace_find_event(type);
2601 if (!(event->flags & EVENT_FL_ISFUNCRET))
2604 pid = parse_common_pid(next->data);
2605 field = find_field(event, "func");
2607 die("function return does not have field func");
2609 val = read_size(next->data + field->offset, field->size);
2611 if (cur_pid != pid || cur_func != val)
2614 /* this is a leaf, now advance the iterator */
2615 return trace_read_data(cpu);
2618 /* Signal a overhead of time execution to the output */
2619 static void print_graph_overhead(unsigned long long duration)
2621 /* Non nested entry or return */
2622 if (duration == ~0ULL)
2623 return (void)printf(" ");
2625 /* Duration exceeded 100 msecs */
2626 if (duration > 100000ULL)
2627 return (void)printf("! ");
2629 /* Duration exceeded 10 msecs */
2630 if (duration > 10000ULL)
2631 return (void)printf("+ ");
2636 static void print_graph_duration(unsigned long long duration)
2638 unsigned long usecs = duration / 1000;
2639 unsigned long nsecs_rem = duration % 1000;
2640 /* log10(ULONG_MAX) + '\0' */
2646 sprintf(msecs_str, "%lu", usecs);
2649 len = printf("%lu", usecs);
2651 /* Print nsecs (we don't want to exceed 7 numbers) */
2653 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2654 len += printf(".%s", nsecs_str);
2659 /* Print remaining spaces to fit the row's width */
2660 for (i = len; i < 7; i++)
2667 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2669 unsigned long long rettime, calltime;
2670 unsigned long long duration, depth;
2671 unsigned long long val;
2672 struct format_field *field;
2673 struct func_map *func;
2674 struct event *ret_event;
2678 type = trace_parse_common_type(ret_rec->data);
2679 ret_event = trace_find_event(type);
2681 field = find_field(ret_event, "rettime");
2683 die("can't find rettime in return graph");
2684 rettime = read_size(ret_rec->data + field->offset, field->size);
2686 field = find_field(ret_event, "calltime");
2688 die("can't find rettime in return graph");
2689 calltime = read_size(ret_rec->data + field->offset, field->size);
2691 duration = rettime - calltime;
2694 print_graph_overhead(duration);
2697 print_graph_duration(duration);
2699 field = find_field(event, "depth");
2701 die("can't find depth in entry graph");
2702 depth = read_size(data + field->offset, field->size);
2705 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2708 field = find_field(event, "func");
2710 die("can't find func in entry graph");
2711 val = read_size(data + field->offset, field->size);
2712 func = find_func(val);
2715 printf("%s();", func->func);
2717 printf("%llx();", val);
2720 static void print_graph_nested(struct event *event, void *data)
2722 struct format_field *field;
2723 unsigned long long depth;
2724 unsigned long long val;
2725 struct func_map *func;
2729 print_graph_overhead(-1);
2734 field = find_field(event, "depth");
2736 die("can't find depth in entry graph");
2737 depth = read_size(data + field->offset, field->size);
2740 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2743 field = find_field(event, "func");
2745 die("can't find func in entry graph");
2746 val = read_size(data + field->offset, field->size);
2747 func = find_func(val);
2750 printf("%s() {", func->func);
2752 printf("%llx() {", val);
2756 pretty_print_func_ent(void *data, int size, struct event *event,
2757 int cpu, int pid, const char *comm,
2758 unsigned long secs, unsigned long usecs)
2760 struct format_field *field;
2765 printf("%5lu.%06lu | ", secs, usecs);
2767 print_graph_cpu(cpu);
2768 print_graph_proc(pid, comm);
2772 field = find_field(event, "func");
2774 die("function entry does not have func field");
2776 val = read_size(data + field->offset, field->size);
2779 * peek_data may unmap the data pointer. Copy it first.
2781 copy_data = malloc_or_die(size);
2782 memcpy(copy_data, data, size);
2785 rec = trace_peek_data(cpu);
2787 rec = get_return_for_leaf(cpu, pid, val, rec);
2789 print_graph_entry_leaf(event, data, rec);
2793 print_graph_nested(event, data);
2799 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2800 int cpu, int pid, const char *comm,
2801 unsigned long secs, unsigned long usecs)
2803 unsigned long long rettime, calltime;
2804 unsigned long long duration, depth;
2805 struct format_field *field;
2808 printf("%5lu.%06lu | ", secs, usecs);
2810 print_graph_cpu(cpu);
2811 print_graph_proc(pid, comm);
2815 field = find_field(event, "rettime");
2817 die("can't find rettime in return graph");
2818 rettime = read_size(data + field->offset, field->size);
2820 field = find_field(event, "calltime");
2822 die("can't find calltime in return graph");
2823 calltime = read_size(data + field->offset, field->size);
2825 duration = rettime - calltime;
2828 print_graph_overhead(duration);
2831 print_graph_duration(duration);
2833 field = find_field(event, "depth");
2835 die("can't find depth in entry graph");
2836 depth = read_size(data + field->offset, field->size);
2839 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2846 pretty_print_func_graph(void *data, int size, struct event *event,
2847 int cpu, int pid, const char *comm,
2848 unsigned long secs, unsigned long usecs)
2850 if (event->flags & EVENT_FL_ISFUNCENT)
2851 pretty_print_func_ent(data, size, event,
2852 cpu, pid, comm, secs, usecs);
2853 else if (event->flags & EVENT_FL_ISFUNCRET)
2854 pretty_print_func_ret(data, size, event,
2855 cpu, pid, comm, secs, usecs);
2859 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2862 struct event *event;
2864 unsigned long usecs;
2868 secs = nsecs / NSECS_PER_SEC;
2869 nsecs -= secs * NSECS_PER_SEC;
2870 usecs = nsecs / NSECS_PER_USEC;
2872 type = trace_parse_common_type(data);
2874 event = trace_find_event(type);
2876 printf("ug! no event found for type %d\n", type);
2880 pid = parse_common_pid(data);
2882 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2883 return pretty_print_func_graph(data, size, event, cpu,
2884 pid, comm, secs, usecs);
2886 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
2888 secs, nsecs, event->name);
2890 pretty_print(data, size, event);
2894 static void print_fields(struct print_flag_sym *field)
2896 printf("{ %s, %s }", field->value, field->str);
2899 print_fields(field->next);
2903 static void print_args(struct print_arg *args)
2905 int print_paren = 1;
2907 switch (args->type) {
2912 printf("%s", args->atom.atom);
2915 printf("REC->%s", args->field.name);
2918 printf("__print_flags(");
2919 print_args(args->flags.field);
2920 printf(", %s, ", args->flags.delim);
2921 print_fields(args->flags.flags);
2925 printf("__print_symbolic(");
2926 print_args(args->symbol.field);
2928 print_fields(args->symbol.symbols);
2932 printf("__get_str(%s)", args->string.string);
2935 printf("(%s)", args->typecast.type);
2936 print_args(args->typecast.item);
2939 if (strcmp(args->op.op, ":") == 0)
2943 print_args(args->op.left);
2944 printf(" %s ", args->op.op);
2945 print_args(args->op.right);
2950 /* we should warn... */
2955 print_args(args->next);
2959 static void parse_header_field(char *field,
2960 int *offset, int *size)
2965 if (read_expected(EVENT_ITEM, (char *)"field") < 0)
2967 if (read_expected(EVENT_OP, (char *)":") < 0)
2971 if (read_expect_type(EVENT_ITEM, &token) < 0)
2975 if (read_expected(EVENT_ITEM, field) < 0)
2977 if (read_expected(EVENT_OP, (char *)";") < 0)
2979 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
2981 if (read_expected(EVENT_OP, (char *)":") < 0)
2983 if (read_expect_type(EVENT_ITEM, &token) < 0)
2985 *offset = atoi(token);
2987 if (read_expected(EVENT_OP, (char *)";") < 0)
2989 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
2991 if (read_expected(EVENT_OP, (char *)":") < 0)
2993 if (read_expect_type(EVENT_ITEM, &token) < 0)
2995 *size = atoi(token);
2997 if (read_expected(EVENT_OP, (char *)";") < 0)
2999 type = read_token(&token);
3000 if (type != EVENT_NEWLINE) {
3001 /* newer versions of the kernel have a "signed" type */
3002 if (type != EVENT_ITEM)
3005 if (strcmp(token, (char *)"signed") != 0)
3010 if (read_expected(EVENT_OP, (char *)":") < 0)
3013 if (read_expect_type(EVENT_ITEM, &token))
3017 if (read_expected(EVENT_OP, (char *)";") < 0)
3020 if (read_expect_type(EVENT_NEWLINE, &token))
3027 int parse_header_page(char *buf, unsigned long size)
3029 init_input_buf(buf, size);
3031 parse_header_field((char *)"timestamp", &header_page_ts_offset,
3032 &header_page_ts_size);
3033 parse_header_field((char *)"commit", &header_page_size_offset,
3034 &header_page_size_size);
3035 parse_header_field((char *)"data", &header_page_data_offset,
3036 &header_page_data_size);
3041 int parse_ftrace_file(char *buf, unsigned long size)
3043 struct format_field *field;
3044 struct print_arg *arg, **list;
3045 struct event *event;
3048 init_input_buf(buf, size);
3050 event = alloc_event();
3054 event->flags |= EVENT_FL_ISFTRACE;
3056 event->name = event_read_name();
3058 die("failed to read ftrace event name");
3060 if (strcmp(event->name, "function") == 0)
3061 event->flags |= EVENT_FL_ISFUNC;
3063 else if (strcmp(event->name, "funcgraph_entry") == 0)
3064 event->flags |= EVENT_FL_ISFUNCENT;
3066 else if (strcmp(event->name, "funcgraph_exit") == 0)
3067 event->flags |= EVENT_FL_ISFUNCRET;
3069 else if (strcmp(event->name, "bprint") == 0)
3070 event->flags |= EVENT_FL_ISBPRINT;
3072 event->id = event_read_id();
3074 die("failed to read ftrace event id");
3078 ret = event_read_format(event);
3080 die("failed to read ftrace event format");
3082 ret = event_read_print(event);
3084 die("failed to read ftrace event print fmt");
3087 * The arguments for ftrace files are parsed by the fields.
3088 * Set up the fields as their arguments.
3090 list = &event->print_fmt.args;
3091 for (field = event->format.fields; field; field = field->next) {
3092 arg = malloc_or_die(sizeof(*arg));
3093 memset(arg, 0, sizeof(*arg));
3096 arg->type = PRINT_FIELD;
3097 arg->field.name = field->name;
3098 arg->field.field = field;
3103 int parse_event_file(char *buf, unsigned long size, char *sys)
3105 struct event *event;
3108 init_input_buf(buf, size);
3110 event = alloc_event();
3114 event->name = event_read_name();
3116 die("failed to read event name");
3118 event->id = event_read_id();
3120 die("failed to read event id");
3122 ret = event_read_format(event);
3124 die("failed to read event format");
3126 ret = event_read_print(event);
3128 die("failed to read event print fmt");
3130 event->system = strdup(sys);
3132 #define PRINT_ARGS 0
3133 if (PRINT_ARGS && event->print_fmt.args)
3134 print_args(event->print_fmt.args);
3140 void parse_set_info(int nr_cpus, int long_sz)
3143 long_size = long_sz;