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 if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
930 if (read_expected(EVENT_OP, (char *)":") < 0)
933 if (read_expect_type(EVENT_ITEM, &token))
935 if (strtoul(token, NULL, 0))
936 field->flags |= FIELD_IS_SIGNED;
939 if (read_expected(EVENT_OP, (char *)";") < 0)
942 if (read_expect_type(EVENT_NEWLINE, &token) < 0)
947 fields = &field->next;
961 static int event_read_format(struct event *event)
966 if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
969 if (read_expected(EVENT_OP, (char *)":") < 0)
972 if (read_expect_type(EVENT_NEWLINE, &token))
976 ret = event_read_fields(event, &event->format.common_fields);
979 event->format.nr_common = ret;
981 ret = event_read_fields(event, &event->format.fields);
984 event->format.nr_fields = ret;
994 process_arg_token(struct event *event, struct print_arg *arg,
995 char **tok, enum event_type type);
997 static enum event_type
998 process_arg(struct event *event, struct print_arg *arg, char **tok)
1000 enum event_type type;
1003 type = read_token(&token);
1006 return process_arg_token(event, arg, tok, type);
1009 static enum event_type
1010 process_cond(struct event *event, struct print_arg *top, char **tok)
1012 struct print_arg *arg, *left, *right;
1013 enum event_type type;
1016 arg = malloc_or_die(sizeof(*arg));
1017 memset(arg, 0, sizeof(*arg));
1019 left = malloc_or_die(sizeof(*left));
1021 right = malloc_or_die(sizeof(*right));
1023 arg->type = PRINT_OP;
1024 arg->op.left = left;
1025 arg->op.right = right;
1028 type = process_arg(event, left, &token);
1029 if (test_type_token(type, token, EVENT_OP, (char *)":"))
1034 type = process_arg(event, right, &token);
1036 top->op.right = arg;
1049 static int get_op_prio(char *op)
1060 /* '>>' and '<<' are 8 */
1064 /* '==' and '!=' are 10 */
1074 die("unknown op '%c'", op[0]);
1078 if (strcmp(op, "++") == 0 ||
1079 strcmp(op, "--") == 0) {
1081 } else if (strcmp(op, ">>") == 0 ||
1082 strcmp(op, "<<") == 0) {
1084 } else if (strcmp(op, ">=") == 0 ||
1085 strcmp(op, "<=") == 0) {
1087 } else if (strcmp(op, "==") == 0 ||
1088 strcmp(op, "!=") == 0) {
1090 } else if (strcmp(op, "&&") == 0) {
1092 } else if (strcmp(op, "||") == 0) {
1095 die("unknown op '%s'", op);
1101 static void set_op_prio(struct print_arg *arg)
1104 /* single ops are the greatest */
1105 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1110 arg->op.prio = get_op_prio(arg->op.op);
1113 static enum event_type
1114 process_op(struct event *event, struct print_arg *arg, char **tok)
1116 struct print_arg *left, *right = NULL;
1117 enum event_type type;
1120 /* the op is passed in via tok */
1123 if (arg->type == PRINT_OP && !arg->op.left) {
1124 /* handle single op */
1126 die("bad op token %s", token);
1135 die("bad op token %s", token);
1139 /* make an empty left */
1140 left = malloc_or_die(sizeof(*left));
1141 left->type = PRINT_NULL;
1142 arg->op.left = left;
1144 right = malloc_or_die(sizeof(*right));
1145 arg->op.right = right;
1147 type = process_arg(event, right, tok);
1149 } else if (strcmp(token, "?") == 0) {
1151 left = malloc_or_die(sizeof(*left));
1152 /* copy the top arg to the left */
1155 arg->type = PRINT_OP;
1157 arg->op.left = left;
1160 type = process_cond(event, arg, tok);
1162 } else if (strcmp(token, ">>") == 0 ||
1163 strcmp(token, "<<") == 0 ||
1164 strcmp(token, "&") == 0 ||
1165 strcmp(token, "|") == 0 ||
1166 strcmp(token, "&&") == 0 ||
1167 strcmp(token, "||") == 0 ||
1168 strcmp(token, "-") == 0 ||
1169 strcmp(token, "+") == 0 ||
1170 strcmp(token, "*") == 0 ||
1171 strcmp(token, "^") == 0 ||
1172 strcmp(token, "/") == 0 ||
1173 strcmp(token, "<") == 0 ||
1174 strcmp(token, ">") == 0 ||
1175 strcmp(token, "==") == 0 ||
1176 strcmp(token, "!=") == 0) {
1178 left = malloc_or_die(sizeof(*left));
1180 /* copy the top arg to the left */
1183 arg->type = PRINT_OP;
1185 arg->op.left = left;
1189 right = malloc_or_die(sizeof(*right));
1191 type = process_arg(event, right, tok);
1193 arg->op.right = right;
1196 die("unknown op '%s'", token);
1197 /* the arg is now the left side */
1202 if (type == EVENT_OP) {
1205 /* higher prios need to be closer to the root */
1206 prio = get_op_prio(*tok);
1208 if (prio > arg->op.prio)
1209 return process_op(event, arg, tok);
1211 return process_op(event, right, tok);
1217 static enum event_type
1218 process_entry(struct event *event __unused, struct print_arg *arg,
1221 enum event_type type;
1225 if (read_expected(EVENT_OP, (char *)"->") < 0)
1228 if (read_expect_type(EVENT_ITEM, &token) < 0)
1232 arg->type = PRINT_FIELD;
1233 arg->field.name = field;
1235 type = read_token(&token);
1245 static char *arg_eval (struct print_arg *arg);
1247 static long long arg_num_eval(struct print_arg *arg)
1249 long long left, right;
1252 switch (arg->type) {
1254 val = strtoll(arg->atom.atom, NULL, 0);
1257 val = arg_num_eval(arg->typecast.item);
1260 switch (arg->op.op[0]) {
1262 left = arg_num_eval(arg->op.left);
1263 right = arg_num_eval(arg->op.right);
1265 val = left || right;
1270 left = arg_num_eval(arg->op.left);
1271 right = arg_num_eval(arg->op.right);
1273 val = left && right;
1278 left = arg_num_eval(arg->op.left);
1279 right = arg_num_eval(arg->op.right);
1280 switch (arg->op.op[1]) {
1285 val = left << right;
1288 val = left <= right;
1291 die("unknown op '%s'", arg->op.op);
1295 left = arg_num_eval(arg->op.left);
1296 right = arg_num_eval(arg->op.right);
1297 switch (arg->op.op[1]) {
1302 val = left >> right;
1305 val = left >= right;
1308 die("unknown op '%s'", arg->op.op);
1312 left = arg_num_eval(arg->op.left);
1313 right = arg_num_eval(arg->op.right);
1315 if (arg->op.op[1] != '=')
1316 die("unknown op '%s'", arg->op.op);
1318 val = left == right;
1321 left = arg_num_eval(arg->op.left);
1322 right = arg_num_eval(arg->op.right);
1324 switch (arg->op.op[1]) {
1326 val = left != right;
1329 die("unknown op '%s'", arg->op.op);
1333 die("unknown op '%s'", arg->op.op);
1338 case PRINT_FIELD ... PRINT_SYMBOL:
1341 die("invalid eval type %d", arg->type);
1347 static char *arg_eval (struct print_arg *arg)
1350 static char buf[20];
1352 switch (arg->type) {
1354 return arg->atom.atom;
1356 return arg_eval(arg->typecast.item);
1358 val = arg_num_eval(arg);
1359 sprintf(buf, "%lld", val);
1363 case PRINT_FIELD ... PRINT_SYMBOL:
1366 die("invalid eval type %d", arg->type);
1373 static enum event_type
1374 process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1376 enum event_type type;
1377 struct print_arg *arg = NULL;
1378 struct print_flag_sym *field;
1384 type = read_token_item(&token);
1385 if (test_type_token(type, token, EVENT_OP, (char *)"{"))
1388 arg = malloc_or_die(sizeof(*arg));
1391 type = process_arg(event, arg, &token);
1392 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1395 field = malloc_or_die(sizeof(*field));
1396 memset(field, 0, sizeof(field));
1398 value = arg_eval(arg);
1399 field->value = strdup(value);
1402 type = process_arg(event, arg, &token);
1403 if (test_type_token(type, token, EVENT_OP, (char *)"}"))
1406 value = arg_eval(arg);
1407 field->str = strdup(value);
1412 list = &field->next;
1415 type = read_token_item(&token);
1416 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1428 static enum event_type
1429 process_flags(struct event *event, struct print_arg *arg, char **tok)
1431 struct print_arg *field;
1432 enum event_type type;
1435 memset(arg, 0, sizeof(*arg));
1436 arg->type = PRINT_FLAGS;
1438 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1441 field = malloc_or_die(sizeof(*field));
1443 type = process_arg(event, field, &token);
1444 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1447 arg->flags.field = field;
1449 type = read_token_item(&token);
1450 if (event_item_type(type)) {
1451 arg->flags.delim = token;
1452 type = read_token_item(&token);
1455 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1458 type = process_fields(event, &arg->flags.flags, &token);
1459 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1463 type = read_token_item(tok);
1471 static enum event_type
1472 process_symbols(struct event *event, struct print_arg *arg, char **tok)
1474 struct print_arg *field;
1475 enum event_type type;
1478 memset(arg, 0, sizeof(*arg));
1479 arg->type = PRINT_SYMBOL;
1481 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1484 field = malloc_or_die(sizeof(*field));
1486 type = process_arg(event, field, &token);
1487 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1490 arg->symbol.field = field;
1492 type = process_fields(event, &arg->symbol.symbols, &token);
1493 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1497 type = read_token_item(tok);
1505 static enum event_type
1506 process_paren(struct event *event, struct print_arg *arg, char **tok)
1508 struct print_arg *item_arg;
1509 enum event_type type;
1513 type = process_arg(event, arg, &token);
1515 if (type == EVENT_ERROR)
1518 if (type == EVENT_OP) {
1519 /* handle the ptr casts */
1520 if (!strcmp(token, "*")) {
1522 * FIXME: should we zapp whitespaces before ')' ?
1523 * (may require a peek_token_item())
1525 if (__peek_char() == ')') {
1528 type = read_token_item(&token);
1532 type = process_op(event, arg, &token);
1534 if (type == EVENT_ERROR)
1539 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
1545 type = read_token_item(&token);
1548 * If the next token is an item or another open paren, then
1549 * this was a typecast.
1551 if (event_item_type(type) ||
1552 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1554 /* make this a typecast and contine */
1556 /* prevous must be an atom */
1557 if (arg->type != PRINT_ATOM)
1558 die("previous needed to be PRINT_ATOM");
1560 item_arg = malloc_or_die(sizeof(*item_arg));
1562 arg->type = PRINT_TYPE;
1564 char *old = arg->atom.atom;
1566 arg->atom.atom = malloc_or_die(strlen(old + 3));
1567 sprintf(arg->atom.atom, "%s *", old);
1570 arg->typecast.type = arg->atom.atom;
1571 arg->typecast.item = item_arg;
1572 type = process_arg_token(event, item_arg, &token, type);
1581 static enum event_type
1582 process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1584 enum event_type type;
1587 if (read_expected(EVENT_DELIM, (char *)"(") < 0)
1590 if (read_expect_type(EVENT_ITEM, &token) < 0)
1593 arg->type = PRINT_STRING;
1594 arg->string.string = token;
1595 arg->string.offset = -1;
1597 if (read_expected(EVENT_DELIM, (char *)")") < 0)
1600 type = read_token(&token);
1610 process_arg_token(struct event *event, struct print_arg *arg,
1611 char **tok, enum event_type type)
1620 if (strcmp(token, "REC") == 0) {
1622 type = process_entry(event, arg, &token);
1623 } else if (strcmp(token, "__print_flags") == 0) {
1625 type = process_flags(event, arg, &token);
1626 } else if (strcmp(token, "__print_symbolic") == 0) {
1628 type = process_symbols(event, arg, &token);
1629 } else if (strcmp(token, "__get_str") == 0) {
1631 type = process_str(event, arg, &token);
1634 /* test the next token */
1635 type = read_token_item(&token);
1637 /* atoms can be more than one token long */
1638 while (type == EVENT_ITEM) {
1639 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1641 strcat(atom, token);
1643 type = read_token_item(&token);
1646 /* todo, test for function */
1648 arg->type = PRINT_ATOM;
1649 arg->atom.atom = atom;
1654 arg->type = PRINT_ATOM;
1655 arg->atom.atom = token;
1656 type = read_token_item(&token);
1659 if (strcmp(token, "(") == 0) {
1661 type = process_paren(event, arg, &token);
1665 /* handle single ops */
1666 arg->type = PRINT_OP;
1668 arg->op.left = NULL;
1669 type = process_op(event, arg, &token);
1673 case EVENT_ERROR ... EVENT_NEWLINE:
1675 die("unexpected type %d", type);
1682 static int event_read_print_args(struct event *event, struct print_arg **list)
1684 enum event_type type;
1685 struct print_arg *arg;
1690 arg = malloc_or_die(sizeof(*arg));
1691 memset(arg, 0, sizeof(*arg));
1693 type = process_arg(event, arg, &token);
1695 if (type == EVENT_ERROR) {
1703 if (type == EVENT_OP) {
1704 type = process_op(event, arg, &token);
1709 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1716 } while (type != EVENT_NONE);
1718 if (type != EVENT_NONE)
1724 static int event_read_print(struct event *event)
1726 enum event_type type;
1730 if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
1733 if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
1736 if (read_expected(EVENT_OP, (char *)":") < 0)
1739 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1743 event->print_fmt.format = token;
1744 event->print_fmt.args = NULL;
1746 /* ok to have no arg */
1747 type = read_token_item(&token);
1749 if (type == EVENT_NONE)
1752 /* Handle concatination of print lines */
1753 if (type == EVENT_DQUOTE) {
1756 cat = malloc_or_die(strlen(event->print_fmt.format) +
1758 strcpy(cat, event->print_fmt.format);
1761 free_token(event->print_fmt.format);
1762 event->print_fmt.format = NULL;
1767 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1772 ret = event_read_print_args(event, &event->print_fmt.args);
1783 static struct format_field *
1784 find_common_field(struct event *event, const char *name)
1786 struct format_field *format;
1788 for (format = event->format.common_fields;
1789 format; format = format->next) {
1790 if (strcmp(format->name, name) == 0)
1797 static struct format_field *
1798 find_field(struct event *event, const char *name)
1800 struct format_field *format;
1802 for (format = event->format.fields;
1803 format; format = format->next) {
1804 if (strcmp(format->name, name) == 0)
1811 static struct format_field *
1812 find_any_field(struct event *event, const char *name)
1814 struct format_field *format;
1816 format = find_common_field(event, name);
1819 return find_field(event, name);
1822 static unsigned long long read_size(void *ptr, int size)
1826 return *(unsigned char *)ptr;
1828 return data2host2(ptr);
1830 return data2host4(ptr);
1832 return data2host8(ptr);
1840 raw_field_value(struct event *event, const char *name, void *data)
1842 struct format_field *field;
1844 field = find_any_field(event, name);
1848 return read_size(data + field->offset, field->size);
1851 void *raw_field_ptr(struct event *event, const char *name, void *data)
1853 struct format_field *field;
1855 field = find_any_field(event, name);
1859 return data + field->offset;
1862 static int get_common_info(const char *type, int *offset, int *size)
1864 struct event *event;
1865 struct format_field *field;
1868 * All events should have the same common elements.
1869 * Pick any event to find where the type is;
1872 die("no event_list!");
1875 field = find_common_field(event, type);
1877 die("field '%s' not found", type);
1879 *offset = field->offset;
1880 *size = field->size;
1885 int trace_parse_common_type(void *data)
1887 static int type_offset;
1888 static int type_size;
1892 ret = get_common_info("common_type",
1898 return read_size(data + type_offset, type_size);
1901 static int parse_common_pid(void *data)
1903 static int pid_offset;
1904 static int pid_size;
1908 ret = get_common_info("common_pid",
1915 return read_size(data + pid_offset, pid_size);
1918 struct event *trace_find_event(int id)
1920 struct event *event;
1922 for (event = event_list; event; event = event->next) {
1923 if (event->id == id)
1929 static unsigned long long eval_num_arg(void *data, int size,
1930 struct event *event, struct print_arg *arg)
1932 unsigned long long val = 0;
1933 unsigned long long left, right;
1935 switch (arg->type) {
1940 return strtoull(arg->atom.atom, NULL, 0);
1942 if (!arg->field.field) {
1943 arg->field.field = find_any_field(event, arg->field.name);
1944 if (!arg->field.field)
1945 die("field %s not found", arg->field.name);
1947 /* must be a number */
1948 val = read_size(data + arg->field.field->offset,
1949 arg->field.field->size);
1955 return eval_num_arg(data, size, event, arg->typecast.item);
1960 left = eval_num_arg(data, size, event, arg->op.left);
1961 right = eval_num_arg(data, size, event, arg->op.right);
1962 switch (arg->op.op[0]) {
1965 val = left || right;
1971 val = left && right;
1976 switch (arg->op.op[1]) {
1981 val = left << right;
1984 val = left <= right;
1987 die("unknown op '%s'", arg->op.op);
1991 switch (arg->op.op[1]) {
1996 val = left >> right;
1999 val = left >= right;
2002 die("unknown op '%s'", arg->op.op);
2006 if (arg->op.op[1] != '=')
2007 die("unknown op '%s'", arg->op.op);
2008 val = left == right;
2011 die("unknown op '%s'", arg->op.op);
2014 default: /* not sure what to do there */
2022 unsigned long long value;
2025 static const struct flag flags[] = {
2026 { "HI_SOFTIRQ", 0 },
2027 { "TIMER_SOFTIRQ", 1 },
2028 { "NET_TX_SOFTIRQ", 2 },
2029 { "NET_RX_SOFTIRQ", 3 },
2030 { "BLOCK_SOFTIRQ", 4 },
2031 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
2032 { "TASKLET_SOFTIRQ", 6 },
2033 { "SCHED_SOFTIRQ", 7 },
2034 { "HRTIMER_SOFTIRQ", 8 },
2035 { "RCU_SOFTIRQ", 9 },
2037 { "HRTIMER_NORESTART", 0 },
2038 { "HRTIMER_RESTART", 1 },
2041 static unsigned long long eval_flag(const char *flag)
2046 * Some flags in the format files do not get converted.
2047 * If the flag is not numeric, see if it is something that
2048 * we already know about.
2050 if (isdigit(flag[0]))
2051 return strtoull(flag, NULL, 0);
2053 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
2054 if (strcmp(flags[i].name, flag) == 0)
2055 return flags[i].value;
2060 static void print_str_arg(void *data, int size,
2061 struct event *event, struct print_arg *arg)
2063 struct print_flag_sym *flag;
2064 unsigned long long val, fval;
2068 switch (arg->type) {
2073 printf("%s", arg->atom.atom);
2076 if (!arg->field.field) {
2077 arg->field.field = find_any_field(event, arg->field.name);
2078 if (!arg->field.field)
2079 die("field %s not found", arg->field.name);
2081 str = malloc_or_die(arg->field.field->size + 1);
2082 memcpy(str, data + arg->field.field->offset,
2083 arg->field.field->size);
2084 str[arg->field.field->size] = 0;
2089 val = eval_num_arg(data, size, event, arg->flags.field);
2091 for (flag = arg->flags.flags; flag; flag = flag->next) {
2092 fval = eval_flag(flag->value);
2093 if (!val && !fval) {
2094 printf("%s", flag->str);
2097 if (fval && (val & fval) == fval) {
2098 if (print && arg->flags.delim)
2099 printf("%s", arg->flags.delim);
2100 printf("%s", flag->str);
2107 val = eval_num_arg(data, size, event, arg->symbol.field);
2108 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2109 fval = eval_flag(flag->value);
2111 printf("%s", flag->str);
2119 case PRINT_STRING: {
2122 if (arg->string.offset == -1) {
2123 struct format_field *f;
2125 f = find_any_field(event, arg->string.string);
2126 arg->string.offset = f->offset;
2128 str_offset = *(int *)(data + arg->string.offset);
2129 str_offset &= 0xffff;
2130 printf("%s", ((char *)data) + str_offset);
2135 * The only op for string should be ? :
2137 if (arg->op.op[0] != '?')
2139 val = eval_num_arg(data, size, event, arg->op.left);
2141 print_str_arg(data, size, event, arg->op.right->op.left);
2143 print_str_arg(data, size, event, arg->op.right->op.right);
2151 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2153 static struct format_field *field, *ip_field;
2154 struct print_arg *args, *arg, **next;
2155 unsigned long long ip, val;
2160 field = find_field(event, "buf");
2162 die("can't find buffer field for binary printk");
2163 ip_field = find_field(event, "ip");
2165 die("can't find ip field for binary printk");
2168 ip = read_size(data + ip_field->offset, ip_field->size);
2171 * The first arg is the IP pointer.
2173 args = malloc_or_die(sizeof(*args));
2178 arg->type = PRINT_ATOM;
2179 arg->atom.atom = malloc_or_die(32);
2180 sprintf(arg->atom.atom, "%lld", ip);
2182 /* skip the first "%pf : " */
2183 for (ptr = fmt + 6, bptr = data + field->offset;
2184 bptr < data + size && *ptr; ptr++) {
2208 bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
2220 val = read_size(bptr, ls);
2222 arg = malloc_or_die(sizeof(*arg));
2224 arg->type = PRINT_ATOM;
2225 arg->atom.atom = malloc_or_die(32);
2226 sprintf(arg->atom.atom, "%lld", val);
2231 arg = malloc_or_die(sizeof(*arg));
2233 arg->type = PRINT_STRING;
2234 arg->string.string = strdup(bptr);
2235 bptr += strlen(bptr) + 1;
2247 static void free_args(struct print_arg *args)
2249 struct print_arg *next;
2254 if (args->type == PRINT_ATOM)
2255 free(args->atom.atom);
2257 free(args->string.string);
2263 static char *get_bprint_format(void *data, int size __unused, struct event *event)
2265 unsigned long long addr;
2266 static struct format_field *field;
2267 struct printk_map *printk;
2272 field = find_field(event, "fmt");
2274 die("can't find format field for binary printk");
2275 printf("field->offset = %d size=%d\n", field->offset, field->size);
2278 addr = read_size(data + field->offset, field->size);
2280 printk = find_printk(addr);
2282 format = malloc_or_die(45);
2283 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2289 /* Remove any quotes. */
2292 format = malloc_or_die(strlen(p) + 10);
2293 sprintf(format, "%s : %s", "%pf", p);
2294 /* remove ending quotes and new line since we will add one too */
2295 p = format + strlen(format) - 1;
2300 if (strcmp(p, "\\n") == 0)
2306 static void pretty_print(void *data, int size, struct event *event)
2308 struct print_fmt *print_fmt = &event->print_fmt;
2309 struct print_arg *arg = print_fmt->args;
2310 struct print_arg *args = NULL;
2311 const char *ptr = print_fmt->format;
2312 unsigned long long val;
2313 struct func_map *func;
2314 const char *saveptr;
2315 char *bprint_fmt = NULL;
2321 if (event->flags & EVENT_FL_ISFUNC)
2322 ptr = " %pF <-- %pF";
2324 if (event->flags & EVENT_FL_ISBPRINT) {
2325 bprint_fmt = get_bprint_format(data, size, event);
2326 args = make_bprint_args(bprint_fmt, data, size, event);
2331 for (; *ptr; ptr++) {
2353 } else if (*ptr == '%') {
2378 if (*(ptr+1) == 'F' ||
2391 die("no argument match");
2393 len = ((unsigned long)ptr + 1) -
2394 (unsigned long)saveptr;
2396 /* should never happen */
2400 memcpy(format, saveptr, len);
2403 val = eval_num_arg(data, size, event, arg);
2407 func = find_func(val);
2409 printf("%s", func->func);
2410 if (show_func == 'F')
2418 printf(format, (int)val);
2421 printf(format, (long)val);
2424 printf(format, (long long)val);
2427 die("bad count (%d)", ls);
2432 die("no matching argument");
2434 print_str_arg(data, size, event, arg);
2438 printf(">%c<", *ptr);
2451 static inline int log10_cpu(int nb)
2460 /* taken from Linux, written by Frederic Weisbecker */
2461 static void print_graph_cpu(int cpu)
2464 int log10_this = log10_cpu(cpu);
2465 int log10_all = log10_cpu(cpus);
2469 * Start with a space character - to make it stand out
2470 * to the right a bit when trace output is pasted into
2476 * Tricky - we space the CPU field according to the max
2477 * number of online CPUs. On a 2-cpu system it would take
2478 * a maximum of 1 digit - on a 128 cpu system it would
2479 * take up to 3 digits:
2481 for (i = 0; i < log10_all - log10_this; i++)
2484 printf("%d) ", cpu);
2487 #define TRACE_GRAPH_PROCINFO_LENGTH 14
2488 #define TRACE_GRAPH_INDENT 2
2490 static void print_graph_proc(int pid, const char *comm)
2492 /* sign + log10(MAX_INT) + '\0' */
2498 sprintf(pid_str, "%d", pid);
2500 /* 1 stands for the "-" character */
2501 len = strlen(comm) + strlen(pid_str) + 1;
2503 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2504 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2506 /* First spaces to align center */
2507 for (i = 0; i < spaces / 2; i++)
2510 printf("%s-%s", comm, pid_str);
2512 /* Last spaces to align center */
2513 for (i = 0; i < spaces - (spaces / 2); i++)
2517 static struct record *
2518 get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2519 struct record *next)
2521 struct format_field *field;
2522 struct event *event;
2527 type = trace_parse_common_type(next->data);
2528 event = trace_find_event(type);
2532 if (!(event->flags & EVENT_FL_ISFUNCRET))
2535 pid = parse_common_pid(next->data);
2536 field = find_field(event, "func");
2538 die("function return does not have field func");
2540 val = read_size(next->data + field->offset, field->size);
2542 if (cur_pid != pid || cur_func != val)
2545 /* this is a leaf, now advance the iterator */
2546 return trace_read_data(cpu);
2549 /* Signal a overhead of time execution to the output */
2550 static void print_graph_overhead(unsigned long long duration)
2552 /* Non nested entry or return */
2553 if (duration == ~0ULL)
2554 return (void)printf(" ");
2556 /* Duration exceeded 100 msecs */
2557 if (duration > 100000ULL)
2558 return (void)printf("! ");
2560 /* Duration exceeded 10 msecs */
2561 if (duration > 10000ULL)
2562 return (void)printf("+ ");
2567 static void print_graph_duration(unsigned long long duration)
2569 unsigned long usecs = duration / 1000;
2570 unsigned long nsecs_rem = duration % 1000;
2571 /* log10(ULONG_MAX) + '\0' */
2577 sprintf(msecs_str, "%lu", usecs);
2580 len = printf("%lu", usecs);
2582 /* Print nsecs (we don't want to exceed 7 numbers) */
2584 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2585 len += printf(".%s", nsecs_str);
2590 /* Print remaining spaces to fit the row's width */
2591 for (i = len; i < 7; i++)
2598 print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2600 unsigned long long rettime, calltime;
2601 unsigned long long duration, depth;
2602 unsigned long long val;
2603 struct format_field *field;
2604 struct func_map *func;
2605 struct event *ret_event;
2609 type = trace_parse_common_type(ret_rec->data);
2610 ret_event = trace_find_event(type);
2612 field = find_field(ret_event, "rettime");
2614 die("can't find rettime in return graph");
2615 rettime = read_size(ret_rec->data + field->offset, field->size);
2617 field = find_field(ret_event, "calltime");
2619 die("can't find rettime in return graph");
2620 calltime = read_size(ret_rec->data + field->offset, field->size);
2622 duration = rettime - calltime;
2625 print_graph_overhead(duration);
2628 print_graph_duration(duration);
2630 field = find_field(event, "depth");
2632 die("can't find depth in entry graph");
2633 depth = read_size(data + field->offset, field->size);
2636 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2639 field = find_field(event, "func");
2641 die("can't find func in entry graph");
2642 val = read_size(data + field->offset, field->size);
2643 func = find_func(val);
2646 printf("%s();", func->func);
2648 printf("%llx();", val);
2651 static void print_graph_nested(struct event *event, void *data)
2653 struct format_field *field;
2654 unsigned long long depth;
2655 unsigned long long val;
2656 struct func_map *func;
2660 print_graph_overhead(-1);
2665 field = find_field(event, "depth");
2667 die("can't find depth in entry graph");
2668 depth = read_size(data + field->offset, field->size);
2671 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2674 field = find_field(event, "func");
2676 die("can't find func in entry graph");
2677 val = read_size(data + field->offset, field->size);
2678 func = find_func(val);
2681 printf("%s() {", func->func);
2683 printf("%llx() {", val);
2687 pretty_print_func_ent(void *data, int size, struct event *event,
2688 int cpu, int pid, const char *comm,
2689 unsigned long secs, unsigned long usecs)
2691 struct format_field *field;
2696 printf("%5lu.%06lu | ", secs, usecs);
2698 print_graph_cpu(cpu);
2699 print_graph_proc(pid, comm);
2703 field = find_field(event, "func");
2705 die("function entry does not have func field");
2707 val = read_size(data + field->offset, field->size);
2710 * peek_data may unmap the data pointer. Copy it first.
2712 copy_data = malloc_or_die(size);
2713 memcpy(copy_data, data, size);
2716 rec = trace_peek_data(cpu);
2718 rec = get_return_for_leaf(cpu, pid, val, rec);
2720 print_graph_entry_leaf(event, data, rec);
2724 print_graph_nested(event, data);
2730 pretty_print_func_ret(void *data, int size __unused, struct event *event,
2731 int cpu, int pid, const char *comm,
2732 unsigned long secs, unsigned long usecs)
2734 unsigned long long rettime, calltime;
2735 unsigned long long duration, depth;
2736 struct format_field *field;
2739 printf("%5lu.%06lu | ", secs, usecs);
2741 print_graph_cpu(cpu);
2742 print_graph_proc(pid, comm);
2746 field = find_field(event, "rettime");
2748 die("can't find rettime in return graph");
2749 rettime = read_size(data + field->offset, field->size);
2751 field = find_field(event, "calltime");
2753 die("can't find calltime in return graph");
2754 calltime = read_size(data + field->offset, field->size);
2756 duration = rettime - calltime;
2759 print_graph_overhead(duration);
2762 print_graph_duration(duration);
2764 field = find_field(event, "depth");
2766 die("can't find depth in entry graph");
2767 depth = read_size(data + field->offset, field->size);
2770 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2777 pretty_print_func_graph(void *data, int size, struct event *event,
2778 int cpu, int pid, const char *comm,
2779 unsigned long secs, unsigned long usecs)
2781 if (event->flags & EVENT_FL_ISFUNCENT)
2782 pretty_print_func_ent(data, size, event,
2783 cpu, pid, comm, secs, usecs);
2784 else if (event->flags & EVENT_FL_ISFUNCRET)
2785 pretty_print_func_ret(data, size, event,
2786 cpu, pid, comm, secs, usecs);
2790 void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2793 struct event *event;
2795 unsigned long usecs;
2799 secs = nsecs / NSECS_PER_SEC;
2800 nsecs -= secs * NSECS_PER_SEC;
2801 usecs = nsecs / NSECS_PER_USEC;
2803 type = trace_parse_common_type(data);
2805 event = trace_find_event(type);
2807 printf("ug! no event found for type %d\n", type);
2811 pid = parse_common_pid(data);
2813 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2814 return pretty_print_func_graph(data, size, event, cpu,
2815 pid, comm, secs, usecs);
2817 printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ",
2819 secs, nsecs, event->name);
2821 pretty_print(data, size, event);
2825 static void print_fields(struct print_flag_sym *field)
2827 printf("{ %s, %s }", field->value, field->str);
2830 print_fields(field->next);
2834 static void print_args(struct print_arg *args)
2836 int print_paren = 1;
2838 switch (args->type) {
2843 printf("%s", args->atom.atom);
2846 printf("REC->%s", args->field.name);
2849 printf("__print_flags(");
2850 print_args(args->flags.field);
2851 printf(", %s, ", args->flags.delim);
2852 print_fields(args->flags.flags);
2856 printf("__print_symbolic(");
2857 print_args(args->symbol.field);
2859 print_fields(args->symbol.symbols);
2863 printf("__get_str(%s)", args->string.string);
2866 printf("(%s)", args->typecast.type);
2867 print_args(args->typecast.item);
2870 if (strcmp(args->op.op, ":") == 0)
2874 print_args(args->op.left);
2875 printf(" %s ", args->op.op);
2876 print_args(args->op.right);
2881 /* we should warn... */
2886 print_args(args->next);
2890 static void parse_header_field(char *type,
2891 int *offset, int *size)
2895 if (read_expected(EVENT_ITEM, (char *)"field") < 0)
2897 if (read_expected(EVENT_OP, (char *)":") < 0)
2900 if (read_expect_type(EVENT_ITEM, &token) < 0)
2904 if (read_expected(EVENT_ITEM, type) < 0)
2906 if (read_expected(EVENT_OP, (char *)";") < 0)
2908 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
2910 if (read_expected(EVENT_OP, (char *)":") < 0)
2912 if (read_expect_type(EVENT_ITEM, &token) < 0)
2914 *offset = atoi(token);
2916 if (read_expected(EVENT_OP, (char *)";") < 0)
2918 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
2920 if (read_expected(EVENT_OP, (char *)":") < 0)
2922 if (read_expect_type(EVENT_ITEM, &token) < 0)
2924 *size = atoi(token);
2926 if (read_expected(EVENT_OP, (char *)";") < 0)
2928 if (read_expected(EVENT_ITEM, (char *)"signed") < 0)
2930 if (read_expected(EVENT_OP, (char *)":") < 0)
2932 if (read_expect_type(EVENT_ITEM, &token) < 0)
2935 if (read_expected(EVENT_OP, (char *)";") < 0)
2937 if (read_expect_type(EVENT_NEWLINE, &token) < 0)
2942 int parse_header_page(char *buf, unsigned long size)
2944 init_input_buf(buf, size);
2946 parse_header_field((char *)"timestamp", &header_page_ts_offset,
2947 &header_page_ts_size);
2948 parse_header_field((char *)"commit", &header_page_size_offset,
2949 &header_page_size_size);
2950 parse_header_field((char *)"data", &header_page_data_offset,
2951 &header_page_data_size);
2956 int parse_ftrace_file(char *buf, unsigned long size)
2958 struct format_field *field;
2959 struct print_arg *arg, **list;
2960 struct event *event;
2963 init_input_buf(buf, size);
2965 event = alloc_event();
2969 event->flags |= EVENT_FL_ISFTRACE;
2971 event->name = event_read_name();
2973 die("failed to read ftrace event name");
2975 if (strcmp(event->name, "function") == 0)
2976 event->flags |= EVENT_FL_ISFUNC;
2978 else if (strcmp(event->name, "funcgraph_entry") == 0)
2979 event->flags |= EVENT_FL_ISFUNCENT;
2981 else if (strcmp(event->name, "funcgraph_exit") == 0)
2982 event->flags |= EVENT_FL_ISFUNCRET;
2984 else if (strcmp(event->name, "bprint") == 0)
2985 event->flags |= EVENT_FL_ISBPRINT;
2987 event->id = event_read_id();
2989 die("failed to read ftrace event id");
2993 ret = event_read_format(event);
2995 die("failed to read ftrace event format");
2997 ret = event_read_print(event);
2999 die("failed to read ftrace event print fmt");
3002 * The arguments for ftrace files are parsed by the fields.
3003 * Set up the fields as their arguments.
3005 list = &event->print_fmt.args;
3006 for (field = event->format.fields; field; field = field->next) {
3007 arg = malloc_or_die(sizeof(*arg));
3008 memset(arg, 0, sizeof(*arg));
3011 arg->type = PRINT_FIELD;
3012 arg->field.name = field->name;
3013 arg->field.field = field;
3018 int parse_event_file(char *buf, unsigned long size, char *sys)
3020 struct event *event;
3023 init_input_buf(buf, size);
3025 event = alloc_event();
3029 event->name = event_read_name();
3031 die("failed to read event name");
3033 event->id = event_read_id();
3035 die("failed to read event id");
3037 ret = event_read_format(event);
3039 die("failed to read event format");
3041 ret = event_read_print(event);
3043 die("failed to read event print fmt");
3045 event->system = strdup(sys);
3047 #define PRINT_ARGS 0
3048 if (PRINT_ARGS && event->print_fmt.args)
3049 print_args(event->print_fmt.args);
3055 void parse_set_info(int nr_cpus, int long_sz)
3058 long_size = long_sz;