aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-08-31 04:03:25 -0400
committerIngo Molnar <mingo@elte.hu>2009-08-31 04:03:27 -0400
commit19c959627a3477a8487d08afd47fdc1f4fea60e5 (patch)
treeaaeda0f81bc100ce8f71af129f39b5f2e6e934bf /tools
parent119e7a22bb70d84849384e5113792cd45afa4f85 (diff)
parentd498bc1f6261dd6f655440eb2f1c7fa25694d3ba (diff)
Merge branch 'perfcounters/tracing' into perfcounters/core
Merge reason: this topic is ready now to merge into the main development branch for .32, with functional perf trace output. Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/Makefile4
-rw-r--r--tools/perf/builtin-record.c12
-rw-r--r--tools/perf/builtin-report.c2
-rw-r--r--tools/perf/builtin-trace.c282
-rw-r--r--tools/perf/perf.c1
-rw-r--r--tools/perf/util/parse-events.c47
-rw-r--r--tools/perf/util/parse-events.h13
-rw-r--r--tools/perf/util/trace-event-info.c539
-rw-r--r--tools/perf/util/trace-event-parse.c2930
-rw-r--r--tools/perf/util/trace-event-read.c509
-rw-r--r--tools/perf/util/trace-event.h239
-rw-r--r--tools/perf/util/util.h1
12 files changed, 4569 insertions, 10 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 5d54ddb83ab1..c481a513f551 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -347,6 +347,9 @@ LIB_OBJS += util/values.o
347LIB_OBJS += util/debug.o 347LIB_OBJS += util/debug.o
348LIB_OBJS += util/map.o 348LIB_OBJS += util/map.o
349LIB_OBJS += util/thread.o 349LIB_OBJS += util/thread.o
350LIB_OBJS += util/trace-event-parse.o
351LIB_OBJS += util/trace-event-read.o
352LIB_OBJS += util/trace-event-info.o
350 353
351BUILTIN_OBJS += builtin-annotate.o 354BUILTIN_OBJS += builtin-annotate.o
352BUILTIN_OBJS += builtin-help.o 355BUILTIN_OBJS += builtin-help.o
@@ -355,6 +358,7 @@ BUILTIN_OBJS += builtin-record.o
355BUILTIN_OBJS += builtin-report.o 358BUILTIN_OBJS += builtin-report.o
356BUILTIN_OBJS += builtin-stat.o 359BUILTIN_OBJS += builtin-stat.o
357BUILTIN_OBJS += builtin-top.o 360BUILTIN_OBJS += builtin-top.o
361BUILTIN_OBJS += builtin-trace.o
358 362
359PERFLIBS = $(LIB_FILE) 363PERFLIBS = $(LIB_FILE)
360 364
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 6a5db675ee4f..add514d53d2e 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -17,6 +17,7 @@
17#include "util/header.h" 17#include "util/header.h"
18#include "util/event.h" 18#include "util/event.h"
19#include "util/debug.h" 19#include "util/debug.h"
20#include "util/trace-event.h"
20 21
21#include <unistd.h> 22#include <unistd.h>
22#include <sched.h> 23#include <sched.h>
@@ -546,6 +547,17 @@ static int __cmd_record(int argc, const char **argv)
546 else 547 else
547 header = perf_header__new(); 548 header = perf_header__new();
548 549
550
551 if (raw_samples) {
552 read_tracing_data(attrs, nr_counters);
553 } else {
554 for (i = 0; i < nr_counters; i++) {
555 if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
556 read_tracing_data(attrs, nr_counters);
557 break;
558 }
559 }
560 }
549 atexit(atexit_header); 561 atexit(atexit_header);
550 562
551 if (!system_wide) { 563 if (!system_wide) {
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index ea6328a893cc..cdd46ab11bd5 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -1428,7 +1428,7 @@ static int __cmd_report(void)
1428 exit(-1); 1428 exit(-1);
1429 } 1429 }
1430 if (callchain) { 1430 if (callchain) {
1431 fprintf(stderr, "selected -c but no callchain data." 1431 fprintf(stderr, "selected -g but no callchain data."
1432 " Did you call perf record without" 1432 " Did you call perf record without"
1433 " -g?\n"); 1433 " -g?\n");
1434 exit(-1); 1434 exit(-1);
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
new file mode 100644
index 000000000000..dd3c2e7c9a18
--- /dev/null
+++ b/tools/perf/builtin-trace.c
@@ -0,0 +1,282 @@
1#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
8
9#include "util/parse-options.h"
10
11#include "perf.h"
12#include "util/debug.h"
13
14#include "util/trace-event.h"
15
16static char const *input_name = "perf.data";
17static int input;
18static unsigned long page_size;
19static unsigned long mmap_window = 32;
20
21static unsigned long total = 0;
22static unsigned long total_comm = 0;
23
24static struct rb_root threads;
25static struct thread *last_match;
26
27static struct perf_header *header;
28static u64 sample_type;
29
30
31static int
32process_comm_event(event_t *event, unsigned long offset, unsigned long head)
33{
34 struct thread *thread;
35
36 thread = threads__findnew(event->comm.pid, &threads, &last_match);
37
38 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
39 (void *)(offset + head),
40 (void *)(long)(event->header.size),
41 event->comm.comm, event->comm.pid);
42
43 if (thread == NULL ||
44 thread__set_comm(thread, event->comm.comm)) {
45 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
46 return -1;
47 }
48 total_comm++;
49
50 return 0;
51}
52
53static int
54process_sample_event(event_t *event, unsigned long offset, unsigned long head)
55{
56 char level;
57 int show = 0;
58 struct dso *dso = NULL;
59 struct thread *thread;
60 u64 ip = event->ip.ip;
61 u64 period = 1;
62 void *more_data = event->ip.__more_data;
63 int cpumode;
64
65 thread = threads__findnew(event->ip.pid, &threads, &last_match);
66
67 if (sample_type & PERF_SAMPLE_PERIOD) {
68 period = *(u64 *)more_data;
69 more_data += sizeof(u64);
70 }
71
72 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
73 (void *)(offset + head),
74 (void *)(long)(event->header.size),
75 event->header.misc,
76 event->ip.pid, event->ip.tid,
77 (void *)(long)ip,
78 (long long)period);
79
80 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
81
82 if (thread == NULL) {
83 eprintf("problem processing %d event, skipping it.\n",
84 event->header.type);
85 return -1;
86 }
87
88 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
89
90 if (cpumode == PERF_EVENT_MISC_KERNEL) {
91 show = SHOW_KERNEL;
92 level = 'k';
93
94 dso = kernel_dso;
95
96 dump_printf(" ...... dso: %s\n", dso->name);
97
98 } else if (cpumode == PERF_EVENT_MISC_USER) {
99
100 show = SHOW_USER;
101 level = '.';
102
103 } else {
104 show = SHOW_HV;
105 level = 'H';
106
107 dso = hypervisor_dso;
108
109 dump_printf(" ...... dso: [hypervisor]\n");
110 }
111
112 if (sample_type & PERF_SAMPLE_RAW) {
113 struct {
114 u32 size;
115 char data[0];
116 } *raw = more_data;
117
118 /*
119 * FIXME: better resolve from pid from the struct trace_entry
120 * field, although it should be the same than this perf
121 * event pid
122 */
123 print_event(0, raw->data, raw->size, 0, thread->comm);
124 }
125 total += period;
126
127 return 0;
128}
129
130static int
131process_event(event_t *event, unsigned long offset, unsigned long head)
132{
133 trace_event(event);
134
135 switch (event->header.type) {
136 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
137 return 0;
138
139 case PERF_EVENT_COMM:
140 return process_comm_event(event, offset, head);
141
142 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
143 return 0;
144
145 case PERF_EVENT_SAMPLE:
146 return process_sample_event(event, offset, head);
147
148 case PERF_EVENT_MAX:
149 default:
150 return -1;
151 }
152
153 return 0;
154}
155
156static int __cmd_trace(void)
157{
158 int ret, rc = EXIT_FAILURE;
159 unsigned long offset = 0;
160 unsigned long head = 0;
161 struct stat perf_stat;
162 event_t *event;
163 uint32_t size;
164 char *buf;
165
166 trace_report();
167
168 input = open(input_name, O_RDONLY);
169 if (input < 0) {
170 perror("failed to open file");
171 exit(-1);
172 }
173
174 ret = fstat(input, &perf_stat);
175 if (ret < 0) {
176 perror("failed to stat file");
177 exit(-1);
178 }
179
180 if (!perf_stat.st_size) {
181 fprintf(stderr, "zero-sized file, nothing to do!\n");
182 exit(0);
183 }
184 header = perf_header__read(input);
185 sample_type = perf_header__sample_type(header);
186
187 if (!(sample_type & PERF_SAMPLE_RAW))
188 die("No trace sample to read. Did you call perf record "
189 "without -R?");
190
191 if (load_kernel() < 0) {
192 perror("failed to load kernel symbols");
193 return EXIT_FAILURE;
194 }
195
196remap:
197 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
198 MAP_SHARED, input, offset);
199 if (buf == MAP_FAILED) {
200 perror("failed to mmap file");
201 exit(-1);
202 }
203
204more:
205 event = (event_t *)(buf + head);
206
207 size = event->header.size;
208 if (!size)
209 size = 8;
210
211 if (head + event->header.size >= page_size * mmap_window) {
212 unsigned long shift = page_size * (head / page_size);
213 int res;
214
215 res = munmap(buf, page_size * mmap_window);
216 assert(res == 0);
217
218 offset += shift;
219 head -= shift;
220 goto remap;
221 }
222
223 size = event->header.size;
224
225
226 if (!size || process_event(event, offset, head) < 0) {
227
228 /*
229 * assume we lost track of the stream, check alignment, and
230 * increment a single u64 in the hope to catch on again 'soon'.
231 */
232
233 if (unlikely(head & 7))
234 head &= ~7ULL;
235
236 size = 8;
237 }
238
239 head += size;
240
241 if (offset + head < (unsigned long)perf_stat.st_size)
242 goto more;
243
244 rc = EXIT_SUCCESS;
245 close(input);
246
247 return rc;
248}
249
250static const char * const annotate_usage[] = {
251 "perf trace [<options>] <command>",
252 NULL
253};
254
255static const struct option options[] = {
256 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
257 "dump raw trace in ASCII"),
258 OPT_BOOLEAN('v', "verbose", &verbose,
259 "be more verbose (show symbol address, etc)"),
260 OPT_END()
261};
262
263int cmd_trace(int argc, const char **argv, const char *prefix __used)
264{
265 symbol__init();
266 page_size = getpagesize();
267
268 argc = parse_options(argc, argv, options, annotate_usage, 0);
269 if (argc) {
270 /*
271 * Special case: if there's an argument left then assume tha
272 * it's a symbol filter:
273 */
274 if (argc > 1)
275 usage_with_options(annotate_usage, options);
276 }
277
278
279 setup_pager();
280
281 return __cmd_trace();
282}
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 31982ad064b4..fe4589dde950 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -292,6 +292,7 @@ static void handle_internal_command(int argc, const char **argv)
292 { "top", cmd_top, 0 }, 292 { "top", cmd_top, 0 },
293 { "annotate", cmd_annotate, 0 }, 293 { "annotate", cmd_annotate, 0 },
294 { "version", cmd_version, 0 }, 294 { "version", cmd_version, 0 },
295 { "trace", cmd_trace, 0 },
295 }; 296 };
296 unsigned int i; 297 unsigned int i;
297 static const char ext[] = STRIP_EXTENSION; 298 static const char ext[] = STRIP_EXTENSION;
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 1cda97b39118..89d46c99bc9c 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -158,9 +158,9 @@ int valid_debugfs_mount(const char *debugfs)
158 return 0; 158 return 0;
159} 159}
160 160
161static const char *tracepoint_id_to_name(u64 config) 161struct tracepoint_path *tracepoint_id_to_path(u64 config)
162{ 162{
163 static char tracepoint_name[2 * MAX_EVENT_LENGTH]; 163 struct tracepoint_path *path = NULL;
164 DIR *sys_dir, *evt_dir; 164 DIR *sys_dir, *evt_dir;
165 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 165 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
166 struct stat st; 166 struct stat st;
@@ -170,7 +170,7 @@ static const char *tracepoint_id_to_name(u64 config)
170 char evt_path[MAXPATHLEN]; 170 char evt_path[MAXPATHLEN];
171 171
172 if (valid_debugfs_mount(debugfs_path)) 172 if (valid_debugfs_mount(debugfs_path))
173 return "unkown"; 173 return NULL;
174 174
175 sys_dir = opendir(debugfs_path); 175 sys_dir = opendir(debugfs_path);
176 if (!sys_dir) 176 if (!sys_dir)
@@ -197,10 +197,23 @@ static const char *tracepoint_id_to_name(u64 config)
197 if (id == config) { 197 if (id == config) {
198 closedir(evt_dir); 198 closedir(evt_dir);
199 closedir(sys_dir); 199 closedir(sys_dir);
200 snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH, 200 path = calloc(1, sizeof(path));
201 "%s:%s", sys_dirent.d_name, 201 path->system = malloc(MAX_EVENT_LENGTH);
202 evt_dirent.d_name); 202 if (!path->system) {
203 return tracepoint_name; 203 free(path);
204 return NULL;
205 }
206 path->name = malloc(MAX_EVENT_LENGTH);
207 if (!path->name) {
208 free(path->system);
209 free(path);
210 return NULL;
211 }
212 strncpy(path->system, sys_dirent.d_name,
213 MAX_EVENT_LENGTH);
214 strncpy(path->name, evt_dirent.d_name,
215 MAX_EVENT_LENGTH);
216 return path;
204 } 217 }
205 } 218 }
206 closedir(evt_dir); 219 closedir(evt_dir);
@@ -208,7 +221,25 @@ static const char *tracepoint_id_to_name(u64 config)
208 221
209cleanup: 222cleanup:
210 closedir(sys_dir); 223 closedir(sys_dir);
211 return "unkown"; 224 return NULL;
225}
226
227#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
228static const char *tracepoint_id_to_name(u64 config)
229{
230 static char buf[TP_PATH_LEN];
231 struct tracepoint_path *path;
232
233 path = tracepoint_id_to_path(config);
234 if (path) {
235 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
236 free(path->name);
237 free(path->system);
238 free(path);
239 } else
240 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
241
242 return buf;
212} 243}
213 244
214static int is_cache_op_valid(u8 cache_type, u8 cache_op) 245static int is_cache_op_valid(u8 cache_type, u8 cache_op)
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index 9b1aeea01636..60704c15961f 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -1,10 +1,19 @@
1 1#ifndef _PARSE_EVENTS_H
2#define _PARSE_EVENTS_H
2/* 3/*
3 * Parse symbolic events/counts passed in as options: 4 * Parse symbolic events/counts passed in as options:
4 */ 5 */
5 6
6struct option; 7struct option;
7 8
9struct tracepoint_path {
10 char *system;
11 char *name;
12 struct tracepoint_path *next;
13};
14
15extern struct tracepoint_path *tracepoint_id_to_path(u64 config);
16
8extern int nr_counters; 17extern int nr_counters;
9 18
10extern struct perf_counter_attr attrs[MAX_COUNTERS]; 19extern struct perf_counter_attr attrs[MAX_COUNTERS];
@@ -21,3 +30,5 @@ extern void print_events(void);
21extern char debugfs_path[]; 30extern char debugfs_path[];
22extern int valid_debugfs_mount(const char *debugfs); 31extern int valid_debugfs_mount(const char *debugfs);
23 32
33
34#endif /* _PARSE_EVENTS_H */
diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c
new file mode 100644
index 000000000000..81615279b876
--- /dev/null
+++ b/tools/perf/util/trace-event-info.c
@@ -0,0 +1,539 @@
1/*
2 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
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!)
9 *
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.
14 *
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
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21#define _GNU_SOURCE
22#include <dirent.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/wait.h>
30#include <pthread.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <ctype.h>
34#include <errno.h>
35#include <stdbool.h>
36
37#include "../perf.h"
38#include "trace-event.h"
39
40
41#define VERSION "0.5"
42
43#define _STR(x) #x
44#define STR(x) _STR(x)
45#define MAX_PATH 256
46
47#define TRACE_CTRL "tracing_on"
48#define TRACE "trace"
49#define AVAILABLE "available_tracers"
50#define CURRENT "current_tracer"
51#define ITER_CTRL "trace_options"
52#define MAX_LATENCY "tracing_max_latency"
53
54unsigned int page_size;
55
56static const char *output_file = "trace.info";
57static int output_fd;
58
59struct event_list {
60 struct event_list *next;
61 const char *event;
62};
63
64struct events {
65 struct events *sibling;
66 struct events *children;
67 struct events *next;
68 char *name;
69};
70
71
72
73static void die(const char *fmt, ...)
74{
75 va_list ap;
76 int ret = errno;
77
78 if (errno)
79 perror("trace-cmd");
80 else
81 ret = -1;
82
83 va_start(ap, fmt);
84 fprintf(stderr, " ");
85 vfprintf(stderr, fmt, ap);
86 va_end(ap);
87
88 fprintf(stderr, "\n");
89 exit(ret);
90}
91
92void *malloc_or_die(unsigned int size)
93{
94 void *data;
95
96 data = malloc(size);
97 if (!data)
98 die("malloc");
99 return data;
100}
101
102static const char *find_debugfs(void)
103{
104 static char debugfs[MAX_PATH+1];
105 static int debugfs_found;
106 char type[100];
107 FILE *fp;
108
109 if (debugfs_found)
110 return debugfs;
111
112 if ((fp = fopen("/proc/mounts","r")) == NULL)
113 die("Can't open /proc/mounts for read");
114
115 while (fscanf(fp, "%*s %"
116 STR(MAX_PATH)
117 "s %99s %*s %*d %*d\n",
118 debugfs, type) == 2) {
119 if (strcmp(type, "debugfs") == 0)
120 break;
121 }
122 fclose(fp);
123
124 if (strcmp(type, "debugfs") != 0)
125 die("debugfs not mounted, please mount");
126
127 debugfs_found = 1;
128
129 return debugfs;
130}
131
132/*
133 * Finds the path to the debugfs/tracing
134 * Allocates the string and stores it.
135 */
136static const char *find_tracing_dir(void)
137{
138 static char *tracing;
139 static int tracing_found;
140 const char *debugfs;
141
142 if (tracing_found)
143 return tracing;
144
145 debugfs = find_debugfs();
146
147 tracing = malloc_or_die(strlen(debugfs) + 9);
148
149 sprintf(tracing, "%s/tracing", debugfs);
150
151 tracing_found = 1;
152 return tracing;
153}
154
155static char *get_tracing_file(const char *name)
156{
157 const char *tracing;
158 char *file;
159
160 tracing = find_tracing_dir();
161 if (!tracing)
162 return NULL;
163
164 file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
165
166 sprintf(file, "%s/%s", tracing, name);
167 return file;
168}
169
170static void put_tracing_file(char *file)
171{
172 free(file);
173}
174
175static ssize_t write_or_die(const void *buf, size_t len)
176{
177 int ret;
178
179 ret = write(output_fd, buf, len);
180 if (ret < 0)
181 die("writing to '%s'", output_file);
182
183 return ret;
184}
185
186int bigendian(void)
187{
188 unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
189 unsigned int *ptr;
190
191 ptr = (unsigned int *)str;
192 return *ptr == 0x01020304;
193}
194
195static unsigned long long copy_file_fd(int fd)
196{
197 unsigned long long size = 0;
198 char buf[BUFSIZ];
199 int r;
200
201 do {
202 r = read(fd, buf, BUFSIZ);
203 if (r > 0) {
204 size += r;
205 write_or_die(buf, r);
206 }
207 } while (r > 0);
208
209 return size;
210}
211
212static unsigned long long copy_file(const char *file)
213{
214 unsigned long long size = 0;
215 int fd;
216
217 fd = open(file, O_RDONLY);
218 if (fd < 0)
219 die("Can't read '%s'", file);
220 size = copy_file_fd(fd);
221 close(fd);
222
223 return size;
224}
225
226static unsigned long get_size_fd(int fd)
227{
228 unsigned long long size = 0;
229 char buf[BUFSIZ];
230 int r;
231
232 do {
233 r = read(fd, buf, BUFSIZ);
234 if (r > 0)
235 size += r;
236 } while (r > 0);
237
238 lseek(fd, 0, SEEK_SET);
239
240 return size;
241}
242
243static unsigned long get_size(const char *file)
244{
245 unsigned long long size = 0;
246 int fd;
247
248 fd = open(file, O_RDONLY);
249 if (fd < 0)
250 die("Can't read '%s'", file);
251 size = get_size_fd(fd);
252 close(fd);
253
254 return size;
255}
256
257static void read_header_files(void)
258{
259 unsigned long long size, check_size;
260 char *path;
261 int fd;
262
263 path = get_tracing_file("events/header_page");
264 fd = open(path, O_RDONLY);
265 if (fd < 0)
266 die("can't read '%s'", path);
267
268 /* unfortunately, you can not stat debugfs files for size */
269 size = get_size_fd(fd);
270
271 write_or_die("header_page", 12);
272 write_or_die(&size, 8);
273 check_size = copy_file_fd(fd);
274 if (size != check_size)
275 die("wrong size for '%s' size=%lld read=%lld",
276 path, size, check_size);
277 put_tracing_file(path);
278
279 path = get_tracing_file("events/header_event");
280 fd = open(path, O_RDONLY);
281 if (fd < 0)
282 die("can't read '%s'", path);
283
284 size = get_size_fd(fd);
285
286 write_or_die("header_event", 13);
287 write_or_die(&size, 8);
288 check_size = copy_file_fd(fd);
289 if (size != check_size)
290 die("wrong size for '%s'", path);
291 put_tracing_file(path);
292}
293
294static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
295{
296 while (tps) {
297 if (!strcmp(sys, tps->name))
298 return true;
299 tps = tps->next;
300 }
301
302 return false;
303}
304
305static void copy_event_system(const char *sys, struct tracepoint_path *tps)
306{
307 unsigned long long size, check_size;
308 struct dirent *dent;
309 struct stat st;
310 char *format;
311 DIR *dir;
312 int count = 0;
313 int ret;
314
315 dir = opendir(sys);
316 if (!dir)
317 die("can't read directory '%s'", sys);
318
319 while ((dent = readdir(dir))) {
320 if (strcmp(dent->d_name, ".") == 0 ||
321 strcmp(dent->d_name, "..") == 0 ||
322 !name_in_tp_list(dent->d_name, tps))
323 continue;
324 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
325 sprintf(format, "%s/%s/format", sys, dent->d_name);
326 ret = stat(format, &st);
327 free(format);
328 if (ret < 0)
329 continue;
330 count++;
331 }
332
333 write_or_die(&count, 4);
334
335 rewinddir(dir);
336 while ((dent = readdir(dir))) {
337 if (strcmp(dent->d_name, ".") == 0 ||
338 strcmp(dent->d_name, "..") == 0 ||
339 !name_in_tp_list(dent->d_name, tps))
340 continue;
341 format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
342 sprintf(format, "%s/%s/format", sys, dent->d_name);
343 ret = stat(format, &st);
344
345 if (ret >= 0) {
346 /* unfortunately, you can not stat debugfs files for size */
347 size = get_size(format);
348 write_or_die(&size, 8);
349 check_size = copy_file(format);
350 if (size != check_size)
351 die("error in size of file '%s'", format);
352 }
353
354 free(format);
355 }
356}
357
358static void read_ftrace_files(struct tracepoint_path *tps)
359{
360 char *path;
361
362 path = get_tracing_file("events/ftrace");
363
364 copy_event_system(path, tps);
365
366 put_tracing_file(path);
367}
368
369static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
370{
371 while (tps) {
372 if (!strcmp(sys, tps->system))
373 return true;
374 tps = tps->next;
375 }
376
377 return false;
378}
379
380static void read_event_files(struct tracepoint_path *tps)
381{
382 struct dirent *dent;
383 struct stat st;
384 char *path;
385 char *sys;
386 DIR *dir;
387 int count = 0;
388 int ret;
389
390 path = get_tracing_file("events");
391
392 dir = opendir(path);
393 if (!dir)
394 die("can't read directory '%s'", path);
395
396 while ((dent = readdir(dir))) {
397 if (strcmp(dent->d_name, ".") == 0 ||
398 strcmp(dent->d_name, "..") == 0 ||
399 strcmp(dent->d_name, "ftrace") == 0 ||
400 !system_in_tp_list(dent->d_name, tps))
401 continue;
402 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
403 sprintf(sys, "%s/%s", path, dent->d_name);
404 ret = stat(sys, &st);
405 free(sys);
406 if (ret < 0)
407 continue;
408 if (S_ISDIR(st.st_mode))
409 count++;
410 }
411
412 write_or_die(&count, 4);
413
414 rewinddir(dir);
415 while ((dent = readdir(dir))) {
416 if (strcmp(dent->d_name, ".") == 0 ||
417 strcmp(dent->d_name, "..") == 0 ||
418 strcmp(dent->d_name, "ftrace") == 0 ||
419 !system_in_tp_list(dent->d_name, tps))
420 continue;
421 sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
422 sprintf(sys, "%s/%s", path, dent->d_name);
423 ret = stat(sys, &st);
424 if (ret >= 0) {
425 if (S_ISDIR(st.st_mode)) {
426 write_or_die(dent->d_name, strlen(dent->d_name) + 1);
427 copy_event_system(sys, tps);
428 }
429 }
430 free(sys);
431 }
432
433 put_tracing_file(path);
434}
435
436static void read_proc_kallsyms(void)
437{
438 unsigned int size, check_size;
439 const char *path = "/proc/kallsyms";
440 struct stat st;
441 int ret;
442
443 ret = stat(path, &st);
444 if (ret < 0) {
445 /* not found */
446 size = 0;
447 write_or_die(&size, 4);
448 return;
449 }
450 size = get_size(path);
451 write_or_die(&size, 4);
452 check_size = copy_file(path);
453 if (size != check_size)
454 die("error in size of file '%s'", path);
455
456}
457
458static void read_ftrace_printk(void)
459{
460 unsigned int size, check_size;
461 const char *path;
462 struct stat st;
463 int ret;
464
465 path = get_tracing_file("printk_formats");
466 ret = stat(path, &st);
467 if (ret < 0) {
468 /* not found */
469 size = 0;
470 write_or_die(&size, 4);
471 return;
472 }
473 size = get_size(path);
474 write_or_die(&size, 4);
475 check_size = copy_file(path);
476 if (size != check_size)
477 die("error in size of file '%s'", path);
478
479}
480
481static struct tracepoint_path *
482get_tracepoints_path(struct perf_counter_attr *pattrs, int nb_counters)
483{
484 struct tracepoint_path path, *ppath = &path;
485 int i;
486
487 for (i = 0; i < nb_counters; i++) {
488 if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
489 continue;
490 ppath->next = tracepoint_id_to_path(pattrs[i].config);
491 if (!ppath->next)
492 die("%s\n", "No memory to alloc tracepoints list");
493 ppath = ppath->next;
494 }
495
496 return path.next;
497}
498void read_tracing_data(struct perf_counter_attr *pattrs, int nb_counters)
499{
500 char buf[BUFSIZ];
501 struct tracepoint_path *tps;
502
503 output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
504 if (output_fd < 0)
505 die("creating file '%s'", output_file);
506
507 buf[0] = 23;
508 buf[1] = 8;
509 buf[2] = 68;
510 memcpy(buf + 3, "tracing", 7);
511
512 write_or_die(buf, 10);
513
514 write_or_die(VERSION, strlen(VERSION) + 1);
515
516 /* save endian */
517 if (bigendian())
518 buf[0] = 1;
519 else
520 buf[0] = 0;
521
522 write_or_die(buf, 1);
523
524 /* save size of long */
525 buf[0] = sizeof(long);
526 write_or_die(buf, 1);
527
528 /* save page_size */
529 page_size = getpagesize();
530 write_or_die(&page_size, 4);
531
532 tps = get_tracepoints_path(pattrs, nb_counters);
533
534 read_header_files();
535 read_ftrace_files(tps);
536 read_event_files(tps);
537 read_proc_kallsyms();
538 read_ftrace_printk();
539}
diff --git a/tools/perf/util/trace-event-parse.c b/tools/perf/util/trace-event-parse.c
new file mode 100644
index 000000000000..96c5e97ffe7b
--- /dev/null
+++ b/tools/perf/util/trace-event-parse.c
@@ -0,0 +1,2930 @@
1/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
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!)
9 *
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.
14 *
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
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 *
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by Frederic Weisbecker.
23 */
24#define _GNU_SOURCE
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <ctype.h>
29#include <errno.h>
30
31#undef _GNU_SOURCE
32#include "../perf.h"
33#include "util.h"
34#include "trace-event.h"
35
36int header_page_ts_offset;
37int header_page_ts_size;
38int header_page_size_offset;
39int header_page_size_size;
40int header_page_data_offset;
41int header_page_data_size;
42
43static char *input_buf;
44static unsigned long long input_buf_ptr;
45static unsigned long long input_buf_siz;
46
47static int cpus;
48static int long_size;
49
50static void init_input_buf(char *buf, unsigned long long size)
51{
52 input_buf = buf;
53 input_buf_siz = size;
54 input_buf_ptr = 0;
55}
56
57struct cmdline {
58 char *comm;
59 int pid;
60};
61
62static struct cmdline *cmdlines;
63static int cmdline_count;
64
65static int cmdline_cmp(const void *a, const void *b)
66{
67 const struct cmdline *ca = a;
68 const struct cmdline *cb = b;
69
70 if (ca->pid < cb->pid)
71 return -1;
72 if (ca->pid > cb->pid)
73 return 1;
74
75 return 0;
76}
77
78void parse_cmdlines(char *file, int size __unused)
79{
80 struct cmdline_list {
81 struct cmdline_list *next;
82 char *comm;
83 int pid;
84 } *list = NULL, *item;
85 char *line;
86 char *next = NULL;
87 int i;
88
89 line = strtok_r(file, "\n", &next);
90 while (line) {
91 item = malloc_or_die(sizeof(*item));
92 sscanf(line, "%d %as", &item->pid,
93 (float *)&item->comm); /* workaround gcc warning */
94 item->next = list;
95 list = item;
96 line = strtok_r(NULL, "\n", &next);
97 cmdline_count++;
98 }
99
100 cmdlines = malloc_or_die(sizeof(*cmdlines) * cmdline_count);
101
102 i = 0;
103 while (list) {
104 cmdlines[i].pid = list->pid;
105 cmdlines[i].comm = list->comm;
106 i++;
107 item = list;
108 list = list->next;
109 free(item);
110 }
111
112 qsort(cmdlines, cmdline_count, sizeof(*cmdlines), cmdline_cmp);
113}
114
115static struct func_map {
116 unsigned long long addr;
117 char *func;
118 char *mod;
119} *func_list;
120static unsigned int func_count;
121
122static int func_cmp(const void *a, const void *b)
123{
124 const struct func_map *fa = a;
125 const struct func_map *fb = b;
126
127 if (fa->addr < fb->addr)
128 return -1;
129 if (fa->addr > fb->addr)
130 return 1;
131
132 return 0;
133}
134
135void parse_proc_kallsyms(char *file, unsigned int size __unused)
136{
137 struct func_list {
138 struct func_list *next;
139 unsigned long long addr;
140 char *func;
141 char *mod;
142 } *list = NULL, *item;
143 char *line;
144 char *next = NULL;
145 char *addr_str;
146 char ch;
147 int ret;
148 int i;
149
150 line = strtok_r(file, "\n", &next);
151 while (line) {
152 item = malloc_or_die(sizeof(*item));
153 item->mod = NULL;
154 ret = sscanf(line, "%as %c %as\t[%as",
155 (float *)&addr_str, /* workaround gcc warning */
156 &ch,
157 (float *)&item->func,
158 (float *)&item->mod);
159 item->addr = strtoull(addr_str, NULL, 16);
160 free(addr_str);
161
162 /* truncate the extra ']' */
163 if (item->mod)
164 item->mod[strlen(item->mod) - 1] = 0;
165
166
167 item->next = list;
168 list = item;
169 line = strtok_r(NULL, "\n", &next);
170 func_count++;
171 }
172
173 func_list = malloc_or_die(sizeof(*func_list) * func_count + 1);
174
175 i = 0;
176 while (list) {
177 func_list[i].func = list->func;
178 func_list[i].addr = list->addr;
179 func_list[i].mod = list->mod;
180 i++;
181 item = list;
182 list = list->next;
183 free(item);
184 }
185
186 qsort(func_list, func_count, sizeof(*func_list), func_cmp);
187
188 /*
189 * Add a special record at the end.
190 */
191 func_list[func_count].func = NULL;
192 func_list[func_count].addr = 0;
193 func_list[func_count].mod = NULL;
194}
195
196/*
197 * We are searching for a record in between, not an exact
198 * match.
199 */
200static int func_bcmp(const void *a, const void *b)
201{
202 const struct func_map *fa = a;
203 const struct func_map *fb = b;
204
205 if ((fa->addr == fb->addr) ||
206
207 (fa->addr > fb->addr &&
208 fa->addr < (fb+1)->addr))
209 return 0;
210
211 if (fa->addr < fb->addr)
212 return -1;
213
214 return 1;
215}
216
217static struct func_map *find_func(unsigned long long addr)
218{
219 struct func_map *func;
220 struct func_map key;
221
222 key.addr = addr;
223
224 func = bsearch(&key, func_list, func_count, sizeof(*func_list),
225 func_bcmp);
226
227 return func;
228}
229
230void print_funcs(void)
231{
232 int i;
233
234 for (i = 0; i < (int)func_count; i++) {
235 printf("%016llx %s",
236 func_list[i].addr,
237 func_list[i].func);
238 if (func_list[i].mod)
239 printf(" [%s]\n", func_list[i].mod);
240 else
241 printf("\n");
242 }
243}
244
245static struct printk_map {
246 unsigned long long addr;
247 char *printk;
248} *printk_list;
249static unsigned int printk_count;
250
251static int printk_cmp(const void *a, const void *b)
252{
253 const struct func_map *fa = a;
254 const struct func_map *fb = b;
255
256 if (fa->addr < fb->addr)
257 return -1;
258 if (fa->addr > fb->addr)
259 return 1;
260
261 return 0;
262}
263
264static struct printk_map *find_printk(unsigned long long addr)
265{
266 struct printk_map *printk;
267 struct printk_map key;
268
269 key.addr = addr;
270
271 printk = bsearch(&key, printk_list, printk_count, sizeof(*printk_list),
272 printk_cmp);
273
274 return printk;
275}
276
277void parse_ftrace_printk(char *file, unsigned int size __unused)
278{
279 struct printk_list {
280 struct printk_list *next;
281 unsigned long long addr;
282 char *printk;
283 } *list = NULL, *item;
284 char *line;
285 char *next = NULL;
286 char *addr_str;
287 int ret;
288 int i;
289
290 line = strtok_r(file, "\n", &next);
291 while (line) {
292 item = malloc_or_die(sizeof(*item));
293 ret = sscanf(line, "%as : %as",
294 (float *)&addr_str, /* workaround gcc warning */
295 (float *)&item->printk);
296 item->addr = strtoull(addr_str, NULL, 16);
297 free(addr_str);
298
299 item->next = list;
300 list = item;
301 line = strtok_r(NULL, "\n", &next);
302 printk_count++;
303 }
304
305 printk_list = malloc_or_die(sizeof(*printk_list) * printk_count + 1);
306
307 i = 0;
308 while (list) {
309 printk_list[i].printk = list->printk;
310 printk_list[i].addr = list->addr;
311 i++;
312 item = list;
313 list = list->next;
314 free(item);
315 }
316
317 qsort(printk_list, printk_count, sizeof(*printk_list), printk_cmp);
318}
319
320void print_printk(void)
321{
322 int i;
323
324 for (i = 0; i < (int)printk_count; i++) {
325 printf("%016llx %s\n",
326 printk_list[i].addr,
327 printk_list[i].printk);
328 }
329}
330
331static struct event *alloc_event(void)
332{
333 struct event *event;
334
335 event = malloc_or_die(sizeof(*event));
336 memset(event, 0, sizeof(*event));
337
338 return event;
339}
340
341enum event_type {
342 EVENT_ERROR,
343 EVENT_NONE,
344 EVENT_SPACE,
345 EVENT_NEWLINE,
346 EVENT_OP,
347 EVENT_DELIM,
348 EVENT_ITEM,
349 EVENT_DQUOTE,
350 EVENT_SQUOTE,
351};
352
353static struct event *event_list;
354
355static void add_event(struct event *event)
356{
357 event->next = event_list;
358 event_list = event;
359}
360
361static int event_item_type(enum event_type type)
362{
363 switch (type) {
364 case EVENT_ITEM ... EVENT_SQUOTE:
365 return 1;
366 case EVENT_ERROR ... EVENT_DELIM:
367 default:
368 return 0;
369 }
370}
371
372static void free_arg(struct print_arg *arg)
373{
374 if (!arg)
375 return;
376
377 switch (arg->type) {
378 case PRINT_ATOM:
379 if (arg->atom.atom)
380 free(arg->atom.atom);
381 break;
382 case PRINT_NULL:
383 case PRINT_FIELD ... PRINT_OP:
384 default:
385 /* todo */
386 break;
387 }
388
389 free(arg);
390}
391
392static enum event_type get_type(int ch)
393{
394 if (ch == '\n')
395 return EVENT_NEWLINE;
396 if (isspace(ch))
397 return EVENT_SPACE;
398 if (isalnum(ch) || ch == '_')
399 return EVENT_ITEM;
400 if (ch == '\'')
401 return EVENT_SQUOTE;
402 if (ch == '"')
403 return EVENT_DQUOTE;
404 if (!isprint(ch))
405 return EVENT_NONE;
406 if (ch == '(' || ch == ')' || ch == ',')
407 return EVENT_DELIM;
408
409 return EVENT_OP;
410}
411
412static int __read_char(void)
413{
414 if (input_buf_ptr >= input_buf_siz)
415 return -1;
416
417 return input_buf[input_buf_ptr++];
418}
419
420static int __peek_char(void)
421{
422 if (input_buf_ptr >= input_buf_siz)
423 return -1;
424
425 return input_buf[input_buf_ptr];
426}
427
428static enum event_type __read_token(char **tok)
429{
430 char buf[BUFSIZ];
431 int ch, last_ch, quote_ch, next_ch;
432 int i = 0;
433 int tok_size = 0;
434 enum event_type type;
435
436 *tok = NULL;
437
438
439 ch = __read_char();
440 if (ch < 0)
441 return EVENT_NONE;
442
443 type = get_type(ch);
444 if (type == EVENT_NONE)
445 return type;
446
447 buf[i++] = ch;
448
449 switch (type) {
450 case EVENT_NEWLINE:
451 case EVENT_DELIM:
452 *tok = malloc_or_die(2);
453 (*tok)[0] = ch;
454 (*tok)[1] = 0;
455 return type;
456
457 case EVENT_OP:
458 switch (ch) {
459 case '-':
460 next_ch = __peek_char();
461 if (next_ch == '>') {
462 buf[i++] = __read_char();
463 break;
464 }
465 /* fall through */
466 case '+':
467 case '|':
468 case '&':
469 case '>':
470 case '<':
471 last_ch = ch;
472 ch = __peek_char();
473 if (ch != last_ch)
474 goto test_equal;
475 buf[i++] = __read_char();
476 switch (last_ch) {
477 case '>':
478 case '<':
479 goto test_equal;
480 default:
481 break;
482 }
483 break;
484 case '!':
485 case '=':
486 goto test_equal;
487 default: /* what should we do instead? */
488 break;
489 }
490 buf[i] = 0;
491 *tok = strdup(buf);
492 return type;
493
494 test_equal:
495 ch = __peek_char();
496 if (ch == '=')
497 buf[i++] = __read_char();
498 break;
499
500 case EVENT_DQUOTE:
501 case EVENT_SQUOTE:
502 /* don't keep quotes */
503 i--;
504 quote_ch = ch;
505 last_ch = 0;
506 do {
507 if (i == (BUFSIZ - 1)) {
508 buf[i] = 0;
509 if (*tok) {
510 *tok = realloc(*tok, tok_size + BUFSIZ);
511 if (!*tok)
512 return EVENT_NONE;
513 strcat(*tok, buf);
514 } else
515 *tok = strdup(buf);
516
517 if (!*tok)
518 return EVENT_NONE;
519 tok_size += BUFSIZ;
520 i = 0;
521 }
522 last_ch = ch;
523 ch = __read_char();
524 buf[i++] = ch;
525 } while (ch != quote_ch && last_ch != '\\');
526 /* remove the last quote */
527 i--;
528 goto out;
529
530 case EVENT_ERROR ... EVENT_SPACE:
531 case EVENT_ITEM:
532 default:
533 break;
534 }
535
536 while (get_type(__peek_char()) == type) {
537 if (i == (BUFSIZ - 1)) {
538 buf[i] = 0;
539 if (*tok) {
540 *tok = realloc(*tok, tok_size + BUFSIZ);
541 if (!*tok)
542 return EVENT_NONE;
543 strcat(*tok, buf);
544 } else
545 *tok = strdup(buf);
546
547 if (!*tok)
548 return EVENT_NONE;
549 tok_size += BUFSIZ;
550 i = 0;
551 }
552 ch = __read_char();
553 buf[i++] = ch;
554 }
555
556 out:
557 buf[i] = 0;
558 if (*tok) {
559 *tok = realloc(*tok, tok_size + i);
560 if (!*tok)
561 return EVENT_NONE;
562 strcat(*tok, buf);
563 } else
564 *tok = strdup(buf);
565 if (!*tok)
566 return EVENT_NONE;
567
568 return type;
569}
570
571static void free_token(char *tok)
572{
573 if (tok)
574 free(tok);
575}
576
577static enum event_type read_token(char **tok)
578{
579 enum event_type type;
580
581 for (;;) {
582 type = __read_token(tok);
583 if (type != EVENT_SPACE)
584 return type;
585
586 free_token(*tok);
587 }
588
589 /* not reached */
590 return EVENT_NONE;
591}
592
593/* no newline */
594static enum event_type read_token_item(char **tok)
595{
596 enum event_type type;
597
598 for (;;) {
599 type = __read_token(tok);
600 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
601 return type;
602
603 free_token(*tok);
604 }
605
606 /* not reached */
607 return EVENT_NONE;
608}
609
610static int test_type(enum event_type type, enum event_type expect)
611{
612 if (type != expect) {
613 die("Error: expected type %d but read %d",
614 expect, type);
615 return -1;
616 }
617 return 0;
618}
619
620static int test_type_token(enum event_type type, char *token,
621 enum event_type expect, char *expect_tok)
622{
623 if (type != expect) {
624 die("Error: expected type %d but read %d",
625 expect, type);
626 return -1;
627 }
628
629 if (strcmp(token, expect_tok) != 0) {
630 die("Error: expected '%s' but read '%s'",
631 expect_tok, token);
632 return -1;
633 }
634 return 0;
635}
636
637static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
638{
639 enum event_type type;
640
641 if (newline_ok)
642 type = read_token(tok);
643 else
644 type = read_token_item(tok);
645 return test_type(type, expect);
646}
647
648static int read_expect_type(enum event_type expect, char **tok)
649{
650 return __read_expect_type(expect, tok, 1);
651}
652
653static int __read_expected(enum event_type expect, char *str, int newline_ok)
654{
655 enum event_type type;
656 char *token;
657 int ret;
658
659 if (newline_ok)
660 type = read_token(&token);
661 else
662 type = read_token_item(&token);
663
664 ret = test_type_token(type, token, expect, str);
665
666 free_token(token);
667
668 return 0;
669}
670
671static int read_expected(enum event_type expect, char *str)
672{
673 return __read_expected(expect, str, 1);
674}
675
676static int read_expected_item(enum event_type expect, char *str)
677{
678 return __read_expected(expect, str, 0);
679}
680
681static char *event_read_name(void)
682{
683 char *token;
684
685 if (read_expected(EVENT_ITEM, (char *)"name") < 0)
686 return NULL;
687
688 if (read_expected(EVENT_OP, (char *)":") < 0)
689 return NULL;
690
691 if (read_expect_type(EVENT_ITEM, &token) < 0)
692 goto fail;
693
694 return token;
695
696 fail:
697 free_token(token);
698 return NULL;
699}
700
701static int event_read_id(void)
702{
703 char *token;
704 int id;
705
706 if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0)
707 return -1;
708
709 if (read_expected(EVENT_OP, (char *)":") < 0)
710 return -1;
711
712 if (read_expect_type(EVENT_ITEM, &token) < 0)
713 goto fail;
714
715 id = strtoul(token, NULL, 0);
716 free_token(token);
717 return id;
718
719 fail:
720 free_token(token);
721 return -1;
722}
723
724static int event_read_fields(struct event *event, struct format_field **fields)
725{
726 struct format_field *field = NULL;
727 enum event_type type;
728 char *token;
729 char *last_token;
730 int count = 0;
731
732 do {
733 type = read_token(&token);
734 if (type == EVENT_NEWLINE) {
735 free_token(token);
736 return count;
737 }
738
739 count++;
740
741 if (test_type_token(type, token, EVENT_ITEM, (char *)"field"))
742 goto fail;
743 free_token(token);
744
745 type = read_token(&token);
746 /*
747 * The ftrace fields may still use the "special" name.
748 * Just ignore it.
749 */
750 if (event->flags & EVENT_FL_ISFTRACE &&
751 type == EVENT_ITEM && strcmp(token, "special") == 0) {
752 free_token(token);
753 type = read_token(&token);
754 }
755
756 if (test_type_token(type, token, EVENT_OP, (char *)":") < 0)
757 return -1;
758
759 if (read_expect_type(EVENT_ITEM, &token) < 0)
760 goto fail;
761
762 last_token = token;
763
764 field = malloc_or_die(sizeof(*field));
765 memset(field, 0, sizeof(*field));
766
767 /* read the rest of the type */
768 for (;;) {
769 type = read_token(&token);
770 if (type == EVENT_ITEM ||
771 (type == EVENT_OP && strcmp(token, "*") == 0) ||
772 /*
773 * Some of the ftrace fields are broken and have
774 * an illegal "." in them.
775 */
776 (event->flags & EVENT_FL_ISFTRACE &&
777 type == EVENT_OP && strcmp(token, ".") == 0)) {
778
779 if (strcmp(token, "*") == 0)
780 field->flags |= FIELD_IS_POINTER;
781
782 if (field->type) {
783 field->type = realloc(field->type,
784 strlen(field->type) +
785 strlen(last_token) + 2);
786 strcat(field->type, " ");
787 strcat(field->type, last_token);
788 } else
789 field->type = last_token;
790 last_token = token;
791 continue;
792 }
793
794 break;
795 }
796
797 if (!field->type) {
798 die("no type found");
799 goto fail;
800 }
801 field->name = last_token;
802
803 if (test_type(type, EVENT_OP))
804 goto fail;
805
806 if (strcmp(token, "[") == 0) {
807 enum event_type last_type = type;
808 char *brackets = token;
809 int len;
810
811 field->flags |= FIELD_IS_ARRAY;
812
813 type = read_token(&token);
814 while (strcmp(token, "]") != 0) {
815 if (last_type == EVENT_ITEM &&
816 type == EVENT_ITEM)
817 len = 2;
818 else
819 len = 1;
820 last_type = type;
821
822 brackets = realloc(brackets,
823 strlen(brackets) +
824 strlen(token) + len);
825 if (len == 2)
826 strcat(brackets, " ");
827 strcat(brackets, token);
828 free_token(token);
829 type = read_token(&token);
830 if (type == EVENT_NONE) {
831 die("failed to find token");
832 goto fail;
833 }
834 }
835
836 free_token(token);
837
838 brackets = realloc(brackets, strlen(brackets) + 2);
839 strcat(brackets, "]");
840
841 /* add brackets to type */
842
843 type = read_token(&token);
844 /*
845 * If the next token is not an OP, then it is of
846 * the format: type [] item;
847 */
848 if (type == EVENT_ITEM) {
849 field->type = realloc(field->type,
850 strlen(field->type) +
851 strlen(field->name) +
852 strlen(brackets) + 2);
853 strcat(field->type, " ");
854 strcat(field->type, field->name);
855 free_token(field->name);
856 strcat(field->type, brackets);
857 field->name = token;
858 type = read_token(&token);
859 } else {
860 field->type = realloc(field->type,
861 strlen(field->type) +
862 strlen(brackets) + 1);
863 strcat(field->type, brackets);
864 }
865 free(brackets);
866 }
867
868 if (test_type_token(type, token, EVENT_OP, (char *)";"))
869 goto fail;
870 free_token(token);
871
872 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
873 goto fail_expect;
874
875 if (read_expected(EVENT_OP, (char *)":") < 0)
876 goto fail_expect;
877
878 if (read_expect_type(EVENT_ITEM, &token))
879 goto fail;
880 field->offset = strtoul(token, NULL, 0);
881 free_token(token);
882
883 if (read_expected(EVENT_OP, (char *)";") < 0)
884 goto fail_expect;
885
886 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
887 goto fail_expect;
888
889 if (read_expected(EVENT_OP, (char *)":") < 0)
890 goto fail_expect;
891
892 if (read_expect_type(EVENT_ITEM, &token))
893 goto fail;
894 field->size = strtoul(token, NULL, 0);
895 free_token(token);
896
897 if (read_expected(EVENT_OP, (char *)";") < 0)
898 goto fail_expect;
899
900 if (read_expect_type(EVENT_NEWLINE, &token) < 0)
901 goto fail;
902 free_token(token);
903
904 *fields = field;
905 fields = &field->next;
906
907 } while (1);
908
909 return 0;
910
911fail:
912 free_token(token);
913fail_expect:
914 if (field)
915 free(field);
916 return -1;
917}
918
919static int event_read_format(struct event *event)
920{
921 char *token;
922 int ret;
923
924 if (read_expected_item(EVENT_ITEM, (char *)"format") < 0)
925 return -1;
926
927 if (read_expected(EVENT_OP, (char *)":") < 0)
928 return -1;
929
930 if (read_expect_type(EVENT_NEWLINE, &token))
931 goto fail;
932 free_token(token);
933
934 ret = event_read_fields(event, &event->format.common_fields);
935 if (ret < 0)
936 return ret;
937 event->format.nr_common = ret;
938
939 ret = event_read_fields(event, &event->format.fields);
940 if (ret < 0)
941 return ret;
942 event->format.nr_fields = ret;
943
944 return 0;
945
946 fail:
947 free_token(token);
948 return -1;
949}
950
951enum event_type
952process_arg_token(struct event *event, struct print_arg *arg,
953 char **tok, enum event_type type);
954
955static enum event_type
956process_arg(struct event *event, struct print_arg *arg, char **tok)
957{
958 enum event_type type;
959 char *token;
960
961 type = read_token(&token);
962 *tok = token;
963
964 return process_arg_token(event, arg, tok, type);
965}
966
967static enum event_type
968process_cond(struct event *event, struct print_arg *top, char **tok)
969{
970 struct print_arg *arg, *left, *right;
971 enum event_type type;
972 char *token = NULL;
973
974 arg = malloc_or_die(sizeof(*arg));
975 memset(arg, 0, sizeof(*arg));
976
977 left = malloc_or_die(sizeof(*left));
978
979 right = malloc_or_die(sizeof(*right));
980
981 arg->type = PRINT_OP;
982 arg->op.left = left;
983 arg->op.right = right;
984
985 *tok = NULL;
986 type = process_arg(event, left, &token);
987 if (test_type_token(type, token, EVENT_OP, (char *)":"))
988 goto out_free;
989
990 arg->op.op = token;
991
992 type = process_arg(event, right, &token);
993
994 top->op.right = arg;
995
996 *tok = token;
997 return type;
998
999out_free:
1000 free_token(*tok);
1001 free(right);
1002 free(left);
1003 free_arg(arg);
1004 return EVENT_ERROR;
1005}
1006
1007static int get_op_prio(char *op)
1008{
1009 if (!op[1]) {
1010 switch (op[0]) {
1011 case '*':
1012 case '/':
1013 case '%':
1014 return 6;
1015 case '+':
1016 case '-':
1017 return 7;
1018 /* '>>' and '<<' are 8 */
1019 case '<':
1020 case '>':
1021 return 9;
1022 /* '==' and '!=' are 10 */
1023 case '&':
1024 return 11;
1025 case '^':
1026 return 12;
1027 case '|':
1028 return 13;
1029 case '?':
1030 return 16;
1031 default:
1032 die("unknown op '%c'", op[0]);
1033 return -1;
1034 }
1035 } else {
1036 if (strcmp(op, "++") == 0 ||
1037 strcmp(op, "--") == 0) {
1038 return 3;
1039 } else if (strcmp(op, ">>") == 0 ||
1040 strcmp(op, "<<") == 0) {
1041 return 8;
1042 } else if (strcmp(op, ">=") == 0 ||
1043 strcmp(op, "<=") == 0) {
1044 return 9;
1045 } else if (strcmp(op, "==") == 0 ||
1046 strcmp(op, "!=") == 0) {
1047 return 10;
1048 } else if (strcmp(op, "&&") == 0) {
1049 return 14;
1050 } else if (strcmp(op, "||") == 0) {
1051 return 15;
1052 } else {
1053 die("unknown op '%s'", op);
1054 return -1;
1055 }
1056 }
1057}
1058
1059static void set_op_prio(struct print_arg *arg)
1060{
1061
1062 /* single ops are the greatest */
1063 if (!arg->op.left || arg->op.left->type == PRINT_NULL) {
1064 arg->op.prio = 0;
1065 return;
1066 }
1067
1068 arg->op.prio = get_op_prio(arg->op.op);
1069}
1070
1071static enum event_type
1072process_op(struct event *event, struct print_arg *arg, char **tok)
1073{
1074 struct print_arg *left, *right = NULL;
1075 enum event_type type;
1076 char *token;
1077
1078 /* the op is passed in via tok */
1079 token = *tok;
1080
1081 if (arg->type == PRINT_OP && !arg->op.left) {
1082 /* handle single op */
1083 if (token[1]) {
1084 die("bad op token %s", token);
1085 return EVENT_ERROR;
1086 }
1087 switch (token[0]) {
1088 case '!':
1089 case '+':
1090 case '-':
1091 break;
1092 default:
1093 die("bad op token %s", token);
1094 return EVENT_ERROR;
1095 }
1096
1097 /* make an empty left */
1098 left = malloc_or_die(sizeof(*left));
1099 left->type = PRINT_NULL;
1100 arg->op.left = left;
1101
1102 right = malloc_or_die(sizeof(*right));
1103 arg->op.right = right;
1104
1105 type = process_arg(event, right, tok);
1106
1107 } else if (strcmp(token, "?") == 0) {
1108
1109 left = malloc_or_die(sizeof(*left));
1110 /* copy the top arg to the left */
1111 *left = *arg;
1112
1113 arg->type = PRINT_OP;
1114 arg->op.op = token;
1115 arg->op.left = left;
1116 arg->op.prio = 0;
1117
1118 type = process_cond(event, arg, tok);
1119
1120 } else if (strcmp(token, ">>") == 0 ||
1121 strcmp(token, "<<") == 0 ||
1122 strcmp(token, "&") == 0 ||
1123 strcmp(token, "|") == 0 ||
1124 strcmp(token, "&&") == 0 ||
1125 strcmp(token, "||") == 0 ||
1126 strcmp(token, "-") == 0 ||
1127 strcmp(token, "+") == 0 ||
1128 strcmp(token, "*") == 0 ||
1129 strcmp(token, "^") == 0 ||
1130 strcmp(token, "/") == 0 ||
1131 strcmp(token, "==") == 0 ||
1132 strcmp(token, "!=") == 0) {
1133
1134 left = malloc_or_die(sizeof(*left));
1135
1136 /* copy the top arg to the left */
1137 *left = *arg;
1138
1139 arg->type = PRINT_OP;
1140 arg->op.op = token;
1141 arg->op.left = left;
1142
1143 set_op_prio(arg);
1144
1145 right = malloc_or_die(sizeof(*right));
1146
1147 type = process_arg(event, right, tok);
1148
1149 arg->op.right = right;
1150
1151 } else {
1152 die("unknown op '%s'", token);
1153 /* the arg is now the left side */
1154 return EVENT_NONE;
1155 }
1156
1157
1158 if (type == EVENT_OP) {
1159 int prio;
1160
1161 /* higher prios need to be closer to the root */
1162 prio = get_op_prio(*tok);
1163
1164 if (prio > arg->op.prio)
1165 return process_op(event, arg, tok);
1166
1167 return process_op(event, right, tok);
1168 }
1169
1170 return type;
1171}
1172
1173static enum event_type
1174process_entry(struct event *event __unused, struct print_arg *arg,
1175 char **tok)
1176{
1177 enum event_type type;
1178 char *field;
1179 char *token;
1180
1181 if (read_expected(EVENT_OP, (char *)"->") < 0)
1182 return EVENT_ERROR;
1183
1184 if (read_expect_type(EVENT_ITEM, &token) < 0)
1185 goto fail;
1186 field = token;
1187
1188 arg->type = PRINT_FIELD;
1189 arg->field.name = field;
1190
1191 type = read_token(&token);
1192 *tok = token;
1193
1194 return type;
1195
1196fail:
1197 free_token(token);
1198 return EVENT_ERROR;
1199}
1200
1201static char *arg_eval (struct print_arg *arg);
1202
1203static long long arg_num_eval(struct print_arg *arg)
1204{
1205 long long left, right;
1206 long long val = 0;
1207
1208 switch (arg->type) {
1209 case PRINT_ATOM:
1210 val = strtoll(arg->atom.atom, NULL, 0);
1211 break;
1212 case PRINT_TYPE:
1213 val = arg_num_eval(arg->typecast.item);
1214 break;
1215 case PRINT_OP:
1216 switch (arg->op.op[0]) {
1217 case '|':
1218 left = arg_num_eval(arg->op.left);
1219 right = arg_num_eval(arg->op.right);
1220 if (arg->op.op[1])
1221 val = left || right;
1222 else
1223 val = left | right;
1224 break;
1225 case '&':
1226 left = arg_num_eval(arg->op.left);
1227 right = arg_num_eval(arg->op.right);
1228 if (arg->op.op[1])
1229 val = left && right;
1230 else
1231 val = left & right;
1232 break;
1233 case '<':
1234 left = arg_num_eval(arg->op.left);
1235 right = arg_num_eval(arg->op.right);
1236 switch (arg->op.op[1]) {
1237 case 0:
1238 val = left < right;
1239 break;
1240 case '<':
1241 val = left << right;
1242 break;
1243 case '=':
1244 val = left <= right;
1245 break;
1246 default:
1247 die("unknown op '%s'", arg->op.op);
1248 }
1249 break;
1250 case '>':
1251 left = arg_num_eval(arg->op.left);
1252 right = arg_num_eval(arg->op.right);
1253 switch (arg->op.op[1]) {
1254 case 0:
1255 val = left > right;
1256 break;
1257 case '>':
1258 val = left >> right;
1259 break;
1260 case '=':
1261 val = left >= right;
1262 break;
1263 default:
1264 die("unknown op '%s'", arg->op.op);
1265 }
1266 break;
1267 case '=':
1268 left = arg_num_eval(arg->op.left);
1269 right = arg_num_eval(arg->op.right);
1270
1271 if (arg->op.op[1] != '=')
1272 die("unknown op '%s'", arg->op.op);
1273
1274 val = left == right;
1275 break;
1276 case '!':
1277 left = arg_num_eval(arg->op.left);
1278 right = arg_num_eval(arg->op.right);
1279
1280 switch (arg->op.op[1]) {
1281 case '=':
1282 val = left != right;
1283 break;
1284 default:
1285 die("unknown op '%s'", arg->op.op);
1286 }
1287 break;
1288 default:
1289 die("unknown op '%s'", arg->op.op);
1290 }
1291 break;
1292
1293 case PRINT_NULL:
1294 case PRINT_FIELD ... PRINT_SYMBOL:
1295 case PRINT_STRING:
1296 default:
1297 die("invalid eval type %d", arg->type);
1298
1299 }
1300 return val;
1301}
1302
1303static char *arg_eval (struct print_arg *arg)
1304{
1305 long long val;
1306 static char buf[20];
1307
1308 switch (arg->type) {
1309 case PRINT_ATOM:
1310 return arg->atom.atom;
1311 case PRINT_TYPE:
1312 return arg_eval(arg->typecast.item);
1313 case PRINT_OP:
1314 val = arg_num_eval(arg);
1315 sprintf(buf, "%lld", val);
1316 return buf;
1317
1318 case PRINT_NULL:
1319 case PRINT_FIELD ... PRINT_SYMBOL:
1320 case PRINT_STRING:
1321 default:
1322 die("invalid eval type %d", arg->type);
1323 break;
1324 }
1325
1326 return NULL;
1327}
1328
1329static enum event_type
1330process_fields(struct event *event, struct print_flag_sym **list, char **tok)
1331{
1332 enum event_type type;
1333 struct print_arg *arg = NULL;
1334 struct print_flag_sym *field;
1335 char *token = NULL;
1336 char *value;
1337
1338 do {
1339 free_token(token);
1340 type = read_token_item(&token);
1341 if (test_type_token(type, token, EVENT_OP, (char *)"{"))
1342 break;
1343
1344 arg = malloc_or_die(sizeof(*arg));
1345
1346 free_token(token);
1347 type = process_arg(event, arg, &token);
1348 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1349 goto out_free;
1350
1351 field = malloc_or_die(sizeof(*field));
1352 memset(field, 0, sizeof(field));
1353
1354 value = arg_eval(arg);
1355 field->value = strdup(value);
1356
1357 free_token(token);
1358 type = process_arg(event, arg, &token);
1359 if (test_type_token(type, token, EVENT_OP, (char *)"}"))
1360 goto out_free;
1361
1362 value = arg_eval(arg);
1363 field->str = strdup(value);
1364 free_arg(arg);
1365 arg = NULL;
1366
1367 *list = field;
1368 list = &field->next;
1369
1370 free_token(token);
1371 type = read_token_item(&token);
1372 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
1373
1374 *tok = token;
1375 return type;
1376
1377out_free:
1378 free_arg(arg);
1379 free_token(token);
1380
1381 return EVENT_ERROR;
1382}
1383
1384static enum event_type
1385process_flags(struct event *event, struct print_arg *arg, char **tok)
1386{
1387 struct print_arg *field;
1388 enum event_type type;
1389 char *token;
1390
1391 memset(arg, 0, sizeof(*arg));
1392 arg->type = PRINT_FLAGS;
1393
1394 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1395 return EVENT_ERROR;
1396
1397 field = malloc_or_die(sizeof(*field));
1398
1399 type = process_arg(event, field, &token);
1400 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1401 goto out_free;
1402
1403 arg->flags.field = field;
1404
1405 type = read_token_item(&token);
1406 if (event_item_type(type)) {
1407 arg->flags.delim = token;
1408 type = read_token_item(&token);
1409 }
1410
1411 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1412 goto out_free;
1413
1414 type = process_fields(event, &arg->flags.flags, &token);
1415 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1416 goto out_free;
1417
1418 free_token(token);
1419 type = read_token_item(tok);
1420 return type;
1421
1422out_free:
1423 free_token(token);
1424 return EVENT_ERROR;
1425}
1426
1427static enum event_type
1428process_symbols(struct event *event, struct print_arg *arg, char **tok)
1429{
1430 struct print_arg *field;
1431 enum event_type type;
1432 char *token;
1433
1434 memset(arg, 0, sizeof(*arg));
1435 arg->type = PRINT_SYMBOL;
1436
1437 if (read_expected_item(EVENT_DELIM, (char *)"(") < 0)
1438 return EVENT_ERROR;
1439
1440 field = malloc_or_die(sizeof(*field));
1441
1442 type = process_arg(event, field, &token);
1443 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1444 goto out_free;
1445
1446 arg->symbol.field = field;
1447
1448 type = process_fields(event, &arg->symbol.symbols, &token);
1449 if (test_type_token(type, token, EVENT_DELIM, (char *)")"))
1450 goto out_free;
1451
1452 free_token(token);
1453 type = read_token_item(tok);
1454 return type;
1455
1456out_free:
1457 free_token(token);
1458 return EVENT_ERROR;
1459}
1460
1461static enum event_type
1462process_paren(struct event *event, struct print_arg *arg, char **tok)
1463{
1464 struct print_arg *item_arg;
1465 enum event_type type;
1466 int ptr_cast = 0;
1467 char *token;
1468
1469 type = process_arg(event, arg, &token);
1470
1471 if (type == EVENT_ERROR)
1472 return EVENT_ERROR;
1473
1474 if (type == EVENT_OP) {
1475 /* handle the ptr casts */
1476 if (!strcmp(token, "*")) {
1477 /*
1478 * FIXME: should we zapp whitespaces before ')' ?
1479 * (may require a peek_token_item())
1480 */
1481 if (__peek_char() == ')') {
1482 ptr_cast = 1;
1483 free_token(token);
1484 type = read_token_item(&token);
1485 }
1486 }
1487 if (!ptr_cast) {
1488 type = process_op(event, arg, &token);
1489
1490 if (type == EVENT_ERROR)
1491 return EVENT_ERROR;
1492 }
1493 }
1494
1495 if (test_type_token(type, token, EVENT_DELIM, (char *)")")) {
1496 free_token(token);
1497 return EVENT_ERROR;
1498 }
1499
1500 free_token(token);
1501 type = read_token_item(&token);
1502
1503 /*
1504 * If the next token is an item or another open paren, then
1505 * this was a typecast.
1506 */
1507 if (event_item_type(type) ||
1508 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
1509
1510 /* make this a typecast and contine */
1511
1512 /* prevous must be an atom */
1513 if (arg->type != PRINT_ATOM)
1514 die("previous needed to be PRINT_ATOM");
1515
1516 item_arg = malloc_or_die(sizeof(*item_arg));
1517
1518 arg->type = PRINT_TYPE;
1519 if (ptr_cast) {
1520 char *old = arg->atom.atom;
1521
1522 arg->atom.atom = malloc_or_die(strlen(old + 3));
1523 sprintf(arg->atom.atom, "%s *", old);
1524 free(old);
1525 }
1526 arg->typecast.type = arg->atom.atom;
1527 arg->typecast.item = item_arg;
1528 type = process_arg_token(event, item_arg, &token, type);
1529
1530 }
1531
1532 *tok = token;
1533 return type;
1534}
1535
1536
1537static enum event_type
1538process_str(struct event *event __unused, struct print_arg *arg, char **tok)
1539{
1540 enum event_type type;
1541 char *token;
1542
1543 if (read_expected(EVENT_DELIM, (char *)"(") < 0)
1544 return EVENT_ERROR;
1545
1546 if (read_expect_type(EVENT_ITEM, &token) < 0)
1547 goto fail;
1548
1549 arg->type = PRINT_STRING;
1550 arg->string.string = token;
1551
1552 if (read_expected(EVENT_DELIM, (char *)")") < 0)
1553 return EVENT_ERROR;
1554
1555 type = read_token(&token);
1556 *tok = token;
1557
1558 return type;
1559fail:
1560 free_token(token);
1561 return EVENT_ERROR;
1562}
1563
1564enum event_type
1565process_arg_token(struct event *event, struct print_arg *arg,
1566 char **tok, enum event_type type)
1567{
1568 char *token;
1569 char *atom;
1570
1571 token = *tok;
1572
1573 switch (type) {
1574 case EVENT_ITEM:
1575 if (strcmp(token, "REC") == 0) {
1576 free_token(token);
1577 type = process_entry(event, arg, &token);
1578 } else if (strcmp(token, "__print_flags") == 0) {
1579 free_token(token);
1580 type = process_flags(event, arg, &token);
1581 } else if (strcmp(token, "__print_symbolic") == 0) {
1582 free_token(token);
1583 type = process_symbols(event, arg, &token);
1584 } else if (strcmp(token, "__get_str") == 0) {
1585 free_token(token);
1586 type = process_str(event, arg, &token);
1587 } else {
1588 atom = token;
1589 /* test the next token */
1590 type = read_token_item(&token);
1591
1592 /* atoms can be more than one token long */
1593 while (type == EVENT_ITEM) {
1594 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
1595 strcat(atom, " ");
1596 strcat(atom, token);
1597 free_token(token);
1598 type = read_token_item(&token);
1599 }
1600
1601 /* todo, test for function */
1602
1603 arg->type = PRINT_ATOM;
1604 arg->atom.atom = atom;
1605 }
1606 break;
1607 case EVENT_DQUOTE:
1608 case EVENT_SQUOTE:
1609 arg->type = PRINT_ATOM;
1610 arg->atom.atom = token;
1611 type = read_token_item(&token);
1612 break;
1613 case EVENT_DELIM:
1614 if (strcmp(token, "(") == 0) {
1615 free_token(token);
1616 type = process_paren(event, arg, &token);
1617 break;
1618 }
1619 case EVENT_OP:
1620 /* handle single ops */
1621 arg->type = PRINT_OP;
1622 arg->op.op = token;
1623 arg->op.left = NULL;
1624 type = process_op(event, arg, &token);
1625
1626 break;
1627
1628 case EVENT_ERROR ... EVENT_NEWLINE:
1629 default:
1630 die("unexpected type %d", type);
1631 }
1632 *tok = token;
1633
1634 return type;
1635}
1636
1637static int event_read_print_args(struct event *event, struct print_arg **list)
1638{
1639 enum event_type type;
1640 struct print_arg *arg;
1641 char *token;
1642 int args = 0;
1643
1644 do {
1645 arg = malloc_or_die(sizeof(*arg));
1646 memset(arg, 0, sizeof(*arg));
1647
1648 type = process_arg(event, arg, &token);
1649
1650 if (type == EVENT_ERROR) {
1651 free_arg(arg);
1652 return -1;
1653 }
1654
1655 *list = arg;
1656 args++;
1657
1658 if (type == EVENT_OP) {
1659 type = process_op(event, arg, &token);
1660 list = &arg->next;
1661 continue;
1662 }
1663
1664 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
1665 free_token(token);
1666 *list = arg;
1667 list = &arg->next;
1668 continue;
1669 }
1670 break;
1671 } while (type != EVENT_NONE);
1672
1673 if (type != EVENT_NONE)
1674 free_token(token);
1675
1676 return args;
1677}
1678
1679static int event_read_print(struct event *event)
1680{
1681 enum event_type type;
1682 char *token;
1683 int ret;
1684
1685 if (read_expected_item(EVENT_ITEM, (char *)"print") < 0)
1686 return -1;
1687
1688 if (read_expected(EVENT_ITEM, (char *)"fmt") < 0)
1689 return -1;
1690
1691 if (read_expected(EVENT_OP, (char *)":") < 0)
1692 return -1;
1693
1694 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
1695 goto fail;
1696
1697 event->print_fmt.format = token;
1698 event->print_fmt.args = NULL;
1699
1700 /* ok to have no arg */
1701 type = read_token_item(&token);
1702
1703 if (type == EVENT_NONE)
1704 return 0;
1705
1706 if (test_type_token(type, token, EVENT_DELIM, (char *)","))
1707 goto fail;
1708
1709 free_token(token);
1710
1711 ret = event_read_print_args(event, &event->print_fmt.args);
1712 if (ret < 0)
1713 return -1;
1714
1715 return 0;
1716
1717 fail:
1718 free_token(token);
1719 return -1;
1720}
1721
1722static struct format_field *
1723find_common_field(struct event *event, const char *name)
1724{
1725 struct format_field *format;
1726
1727 for (format = event->format.common_fields;
1728 format; format = format->next) {
1729 if (strcmp(format->name, name) == 0)
1730 break;
1731 }
1732
1733 return format;
1734}
1735
1736static struct format_field *
1737find_field(struct event *event, const char *name)
1738{
1739 struct format_field *format;
1740
1741 for (format = event->format.fields;
1742 format; format = format->next) {
1743 if (strcmp(format->name, name) == 0)
1744 break;
1745 }
1746
1747 return format;
1748}
1749
1750static struct format_field *
1751find_any_field(struct event *event, const char *name)
1752{
1753 struct format_field *format;
1754
1755 format = find_common_field(event, name);
1756 if (format)
1757 return format;
1758 return find_field(event, name);
1759}
1760
1761static unsigned long long read_size(void *ptr, int size)
1762{
1763 switch (size) {
1764 case 1:
1765 return *(unsigned char *)ptr;
1766 case 2:
1767 return data2host2(ptr);
1768 case 4:
1769 return data2host4(ptr);
1770 case 8:
1771 return data2host8(ptr);
1772 default:
1773 /* BUG! */
1774 return 0;
1775 }
1776}
1777
1778static int get_common_info(const char *type, int *offset, int *size)
1779{
1780 struct event *event;
1781 struct format_field *field;
1782
1783 /*
1784 * All events should have the same common elements.
1785 * Pick any event to find where the type is;
1786 */
1787 if (!event_list)
1788 die("no event_list!");
1789
1790 event = event_list;
1791 field = find_common_field(event, type);
1792 if (!field)
1793 die("field '%s' not found", type);
1794
1795 *offset = field->offset;
1796 *size = field->size;
1797
1798 return 0;
1799}
1800
1801static int parse_common_type(void *data)
1802{
1803 static int type_offset;
1804 static int type_size;
1805 int ret;
1806
1807 if (!type_size) {
1808 ret = get_common_info("common_type",
1809 &type_offset,
1810 &type_size);
1811 if (ret < 0)
1812 return ret;
1813 }
1814 return read_size(data + type_offset, type_size);
1815}
1816
1817static int parse_common_pid(void *data)
1818{
1819 static int pid_offset;
1820 static int pid_size;
1821 int ret;
1822
1823 if (!pid_size) {
1824 ret = get_common_info("common_pid",
1825 &pid_offset,
1826 &pid_size);
1827 if (ret < 0)
1828 return ret;
1829 }
1830
1831 return read_size(data + pid_offset, pid_size);
1832}
1833
1834static struct event *find_event(int id)
1835{
1836 struct event *event;
1837
1838 for (event = event_list; event; event = event->next) {
1839 if (event->id == id)
1840 break;
1841 }
1842 return event;
1843}
1844
1845static unsigned long long eval_num_arg(void *data, int size,
1846 struct event *event, struct print_arg *arg)
1847{
1848 unsigned long long val = 0;
1849 unsigned long long left, right;
1850
1851 switch (arg->type) {
1852 case PRINT_NULL:
1853 /* ?? */
1854 return 0;
1855 case PRINT_ATOM:
1856 return strtoull(arg->atom.atom, NULL, 0);
1857 case PRINT_FIELD:
1858 if (!arg->field.field) {
1859 arg->field.field = find_any_field(event, arg->field.name);
1860 if (!arg->field.field)
1861 die("field %s not found", arg->field.name);
1862 }
1863 /* must be a number */
1864 val = read_size(data + arg->field.field->offset,
1865 arg->field.field->size);
1866 break;
1867 case PRINT_FLAGS:
1868 case PRINT_SYMBOL:
1869 break;
1870 case PRINT_TYPE:
1871 return eval_num_arg(data, size, event, arg->typecast.item);
1872 case PRINT_STRING:
1873 return 0;
1874 break;
1875 case PRINT_OP:
1876 left = eval_num_arg(data, size, event, arg->op.left);
1877 right = eval_num_arg(data, size, event, arg->op.right);
1878 switch (arg->op.op[0]) {
1879 case '|':
1880 if (arg->op.op[1])
1881 val = left || right;
1882 else
1883 val = left | right;
1884 break;
1885 case '&':
1886 if (arg->op.op[1])
1887 val = left && right;
1888 else
1889 val = left & right;
1890 break;
1891 case '<':
1892 switch (arg->op.op[1]) {
1893 case 0:
1894 val = left < right;
1895 break;
1896 case '<':
1897 val = left << right;
1898 break;
1899 case '=':
1900 val = left <= right;
1901 break;
1902 default:
1903 die("unknown op '%s'", arg->op.op);
1904 }
1905 break;
1906 case '>':
1907 switch (arg->op.op[1]) {
1908 case 0:
1909 val = left > right;
1910 break;
1911 case '>':
1912 val = left >> right;
1913 break;
1914 case '=':
1915 val = left >= right;
1916 break;
1917 default:
1918 die("unknown op '%s'", arg->op.op);
1919 }
1920 break;
1921 case '=':
1922 if (arg->op.op[1] != '=')
1923 die("unknown op '%s'", arg->op.op);
1924 val = left == right;
1925 break;
1926 default:
1927 die("unknown op '%s'", arg->op.op);
1928 }
1929 break;
1930 default: /* not sure what to do there */
1931 return 0;
1932 }
1933 return val;
1934}
1935
1936struct flag {
1937 const char *name;
1938 unsigned long long value;
1939};
1940
1941static const struct flag flags[] = {
1942 { "HI_SOFTIRQ", 0 },
1943 { "TIMER_SOFTIRQ", 1 },
1944 { "NET_TX_SOFTIRQ", 2 },
1945 { "NET_RX_SOFTIRQ", 3 },
1946 { "BLOCK_SOFTIRQ", 4 },
1947 { "TASKLET_SOFTIRQ", 5 },
1948 { "SCHED_SOFTIRQ", 6 },
1949 { "HRTIMER_SOFTIRQ", 7 },
1950 { "RCU_SOFTIRQ", 8 },
1951
1952 { "HRTIMER_NORESTART", 0 },
1953 { "HRTIMER_RESTART", 1 },
1954};
1955
1956static unsigned long long eval_flag(const char *flag)
1957{
1958 int i;
1959
1960 /*
1961 * Some flags in the format files do not get converted.
1962 * If the flag is not numeric, see if it is something that
1963 * we already know about.
1964 */
1965 if (isdigit(flag[0]))
1966 return strtoull(flag, NULL, 0);
1967
1968 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
1969 if (strcmp(flags[i].name, flag) == 0)
1970 return flags[i].value;
1971
1972 return 0;
1973}
1974
1975static void print_str_arg(void *data, int size,
1976 struct event *event, struct print_arg *arg)
1977{
1978 struct print_flag_sym *flag;
1979 unsigned long long val, fval;
1980 char *str;
1981 int print;
1982
1983 switch (arg->type) {
1984 case PRINT_NULL:
1985 /* ?? */
1986 return;
1987 case PRINT_ATOM:
1988 printf("%s", arg->atom.atom);
1989 return;
1990 case PRINT_FIELD:
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);
1995 }
1996 str = malloc_or_die(arg->field.field->size + 1);
1997 memcpy(str, data + arg->field.field->offset,
1998 arg->field.field->size);
1999 str[arg->field.field->size] = 0;
2000 printf("%s", str);
2001 free(str);
2002 break;
2003 case PRINT_FLAGS:
2004 val = eval_num_arg(data, size, event, arg->flags.field);
2005 print = 0;
2006 for (flag = arg->flags.flags; flag; flag = flag->next) {
2007 fval = eval_flag(flag->value);
2008 if (!val && !fval) {
2009 printf("%s", flag->str);
2010 break;
2011 }
2012 if (fval && (val & fval) == fval) {
2013 if (print && arg->flags.delim)
2014 printf("%s", arg->flags.delim);
2015 printf("%s", flag->str);
2016 print = 1;
2017 val &= ~fval;
2018 }
2019 }
2020 break;
2021 case PRINT_SYMBOL:
2022 val = eval_num_arg(data, size, event, arg->symbol.field);
2023 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
2024 fval = eval_flag(flag->value);
2025 if (val == fval) {
2026 printf("%s", flag->str);
2027 break;
2028 }
2029 }
2030 break;
2031
2032 case PRINT_TYPE:
2033 break;
2034 case PRINT_STRING:
2035 printf("%s", arg->string.string);
2036 break;
2037 case PRINT_OP:
2038 /*
2039 * The only op for string should be ? :
2040 */
2041 if (arg->op.op[0] != '?')
2042 return;
2043 val = eval_num_arg(data, size, event, arg->op.left);
2044 if (val)
2045 print_str_arg(data, size, event, arg->op.right->op.left);
2046 else
2047 print_str_arg(data, size, event, arg->op.right->op.right);
2048 break;
2049 default:
2050 /* well... */
2051 break;
2052 }
2053}
2054
2055static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event *event)
2056{
2057 static struct format_field *field, *ip_field;
2058 struct print_arg *args, *arg, **next;
2059 unsigned long long ip, val;
2060 char *ptr;
2061 void *bptr;
2062
2063 if (!field) {
2064 field = find_field(event, "buf");
2065 if (!field)
2066 die("can't find buffer field for binary printk");
2067 ip_field = find_field(event, "ip");
2068 if (!ip_field)
2069 die("can't find ip field for binary printk");
2070 }
2071
2072 ip = read_size(data + ip_field->offset, ip_field->size);
2073
2074 /*
2075 * The first arg is the IP pointer.
2076 */
2077 args = malloc_or_die(sizeof(*args));
2078 arg = args;
2079 arg->next = NULL;
2080 next = &arg->next;
2081
2082 arg->type = PRINT_ATOM;
2083 arg->atom.atom = malloc_or_die(32);
2084 sprintf(arg->atom.atom, "%lld", ip);
2085
2086 /* skip the first "%pf : " */
2087 for (ptr = fmt + 6, bptr = data + field->offset;
2088 bptr < data + size && *ptr; ptr++) {
2089 int ls = 0;
2090
2091 if (*ptr == '%') {
2092 process_again:
2093 ptr++;
2094 switch (*ptr) {
2095 case '%':
2096 break;
2097 case 'l':
2098 ls++;
2099 goto process_again;
2100 case 'L':
2101 ls = 2;
2102 goto process_again;
2103 case '0' ... '9':
2104 goto process_again;
2105 case 'p':
2106 ls = 1;
2107 /* fall through */
2108 case 'd':
2109 case 'u':
2110 case 'x':
2111 case 'i':
2112 bptr = (void *)(((unsigned long)bptr + (long_size - 1)) &
2113 ~(long_size - 1));
2114 switch (ls) {
2115 case 0:
2116 case 1:
2117 ls = long_size;
2118 break;
2119 case 2:
2120 ls = 8;
2121 default:
2122 break;
2123 }
2124 val = read_size(bptr, ls);
2125 bptr += ls;
2126 arg = malloc_or_die(sizeof(*arg));
2127 arg->next = NULL;
2128 arg->type = PRINT_ATOM;
2129 arg->atom.atom = malloc_or_die(32);
2130 sprintf(arg->atom.atom, "%lld", val);
2131 *next = arg;
2132 next = &arg->next;
2133 break;
2134 case 's':
2135 arg = malloc_or_die(sizeof(*arg));
2136 arg->next = NULL;
2137 arg->type = PRINT_STRING;
2138 arg->string.string = strdup(bptr);
2139 bptr += strlen(bptr) + 1;
2140 *next = arg;
2141 next = &arg->next;
2142 default:
2143 break;
2144 }
2145 }
2146 }
2147
2148 return args;
2149}
2150
2151static void free_args(struct print_arg *args)
2152{
2153 struct print_arg *next;
2154
2155 while (args) {
2156 next = args->next;
2157
2158 if (args->type == PRINT_ATOM)
2159 free(args->atom.atom);
2160 else
2161 free(args->string.string);
2162 free(args);
2163 args = next;
2164 }
2165}
2166
2167static char *get_bprint_format(void *data, int size __unused, struct event *event)
2168{
2169 unsigned long long addr;
2170 static struct format_field *field;
2171 struct printk_map *printk;
2172 char *format;
2173 char *p;
2174
2175 if (!field) {
2176 field = find_field(event, "fmt");
2177 if (!field)
2178 die("can't find format field for binary printk");
2179 printf("field->offset = %d size=%d\n", field->offset, field->size);
2180 }
2181
2182 addr = read_size(data + field->offset, field->size);
2183
2184 printk = find_printk(addr);
2185 if (!printk) {
2186 format = malloc_or_die(45);
2187 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
2188 addr);
2189 return format;
2190 }
2191
2192 p = printk->printk;
2193 /* Remove any quotes. */
2194 if (*p == '"')
2195 p++;
2196 format = malloc_or_die(strlen(p) + 10);
2197 sprintf(format, "%s : %s", "%pf", p);
2198 /* remove ending quotes and new line since we will add one too */
2199 p = format + strlen(format) - 1;
2200 if (*p == '"')
2201 *p = 0;
2202
2203 p -= 2;
2204 if (strcmp(p, "\\n") == 0)
2205 *p = 0;
2206
2207 return format;
2208}
2209
2210static void pretty_print(void *data, int size, struct event *event)
2211{
2212 struct print_fmt *print_fmt = &event->print_fmt;
2213 struct print_arg *arg = print_fmt->args;
2214 struct print_arg *args = NULL;
2215 const char *ptr = print_fmt->format;
2216 unsigned long long val;
2217 struct func_map *func;
2218 const char *saveptr;
2219 char *bprint_fmt = NULL;
2220 char format[32];
2221 int show_func;
2222 int len;
2223 int ls;
2224
2225 if (event->flags & EVENT_FL_ISFUNC)
2226 ptr = " %pF <-- %pF";
2227
2228 if (event->flags & EVENT_FL_ISBPRINT) {
2229 bprint_fmt = get_bprint_format(data, size, event);
2230 args = make_bprint_args(bprint_fmt, data, size, event);
2231 arg = args;
2232 ptr = bprint_fmt;
2233 }
2234
2235 for (; *ptr; ptr++) {
2236 ls = 0;
2237 if (*ptr == '%') {
2238 saveptr = ptr;
2239 show_func = 0;
2240 cont_process:
2241 ptr++;
2242 switch (*ptr) {
2243 case '%':
2244 printf("%%");
2245 break;
2246 case 'l':
2247 ls++;
2248 goto cont_process;
2249 case 'L':
2250 ls = 2;
2251 goto cont_process;
2252 case 'z':
2253 case 'Z':
2254 case '0' ... '9':
2255 goto cont_process;
2256 case 'p':
2257 if (long_size == 4)
2258 ls = 1;
2259 else
2260 ls = 2;
2261
2262 if (*(ptr+1) == 'F' ||
2263 *(ptr+1) == 'f') {
2264 ptr++;
2265 show_func = *ptr;
2266 }
2267
2268 /* fall through */
2269 case 'd':
2270 case 'i':
2271 case 'x':
2272 case 'X':
2273 case 'u':
2274 if (!arg)
2275 die("no argument match");
2276
2277 len = ((unsigned long)ptr + 1) -
2278 (unsigned long)saveptr;
2279
2280 /* should never happen */
2281 if (len > 32)
2282 die("bad format!");
2283
2284 memcpy(format, saveptr, len);
2285 format[len] = 0;
2286
2287 val = eval_num_arg(data, size, event, arg);
2288 arg = arg->next;
2289
2290 if (show_func) {
2291 func = find_func(val);
2292 if (func) {
2293 printf("%s", func->func);
2294 if (show_func == 'F')
2295 printf("+0x%llx",
2296 val - func->addr);
2297 break;
2298 }
2299 }
2300 switch (ls) {
2301 case 0:
2302 printf(format, (int)val);
2303 break;
2304 case 1:
2305 printf(format, (long)val);
2306 break;
2307 case 2:
2308 printf(format, (long long)val);
2309 break;
2310 default:
2311 die("bad count (%d)", ls);
2312 }
2313 break;
2314 case 's':
2315 if (!arg)
2316 die("no matching argument");
2317
2318 print_str_arg(data, size, event, arg);
2319 arg = arg->next;
2320 break;
2321 default:
2322 printf(">%c<", *ptr);
2323
2324 }
2325 } else
2326 printf("%c", *ptr);
2327 }
2328
2329 if (args) {
2330 free_args(args);
2331 free(bprint_fmt);
2332 }
2333}
2334
2335static inline int log10_cpu(int nb)
2336{
2337 if (nb / 100)
2338 return 3;
2339 if (nb / 10)
2340 return 2;
2341 return 1;
2342}
2343
2344/* taken from Linux, written by Frederic Weisbecker */
2345static void print_graph_cpu(int cpu)
2346{
2347 int i;
2348 int log10_this = log10_cpu(cpu);
2349 int log10_all = log10_cpu(cpus);
2350
2351
2352 /*
2353 * Start with a space character - to make it stand out
2354 * to the right a bit when trace output is pasted into
2355 * email:
2356 */
2357 printf(" ");
2358
2359 /*
2360 * Tricky - we space the CPU field according to the max
2361 * number of online CPUs. On a 2-cpu system it would take
2362 * a maximum of 1 digit - on a 128 cpu system it would
2363 * take up to 3 digits:
2364 */
2365 for (i = 0; i < log10_all - log10_this; i++)
2366 printf(" ");
2367
2368 printf("%d) ", cpu);
2369}
2370
2371#define TRACE_GRAPH_PROCINFO_LENGTH 14
2372#define TRACE_GRAPH_INDENT 2
2373
2374static void print_graph_proc(int pid, const char *comm)
2375{
2376 /* sign + log10(MAX_INT) + '\0' */
2377 char pid_str[11];
2378 int spaces = 0;
2379 int len;
2380 int i;
2381
2382 sprintf(pid_str, "%d", pid);
2383
2384 /* 1 stands for the "-" character */
2385 len = strlen(comm) + strlen(pid_str) + 1;
2386
2387 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
2388 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
2389
2390 /* First spaces to align center */
2391 for (i = 0; i < spaces / 2; i++)
2392 printf(" ");
2393
2394 printf("%s-%s", comm, pid_str);
2395
2396 /* Last spaces to align center */
2397 for (i = 0; i < spaces - (spaces / 2); i++)
2398 printf(" ");
2399}
2400
2401static struct record *
2402get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func,
2403 struct record *next)
2404{
2405 struct format_field *field;
2406 struct event *event;
2407 unsigned long val;
2408 int type;
2409 int pid;
2410
2411 type = parse_common_type(next->data);
2412 event = find_event(type);
2413 if (!event)
2414 return NULL;
2415
2416 if (!(event->flags & EVENT_FL_ISFUNCRET))
2417 return NULL;
2418
2419 pid = parse_common_pid(next->data);
2420 field = find_field(event, "func");
2421 if (!field)
2422 die("function return does not have field func");
2423
2424 val = read_size(next->data + field->offset, field->size);
2425
2426 if (cur_pid != pid || cur_func != val)
2427 return NULL;
2428
2429 /* this is a leaf, now advance the iterator */
2430 return trace_read_data(cpu);
2431}
2432
2433/* Signal a overhead of time execution to the output */
2434static void print_graph_overhead(unsigned long long duration)
2435{
2436 /* Non nested entry or return */
2437 if (duration == ~0ULL)
2438 return (void)printf(" ");
2439
2440 /* Duration exceeded 100 msecs */
2441 if (duration > 100000ULL)
2442 return (void)printf("! ");
2443
2444 /* Duration exceeded 10 msecs */
2445 if (duration > 10000ULL)
2446 return (void)printf("+ ");
2447
2448 printf(" ");
2449}
2450
2451static void print_graph_duration(unsigned long long duration)
2452{
2453 unsigned long usecs = duration / 1000;
2454 unsigned long nsecs_rem = duration % 1000;
2455 /* log10(ULONG_MAX) + '\0' */
2456 char msecs_str[21];
2457 char nsecs_str[5];
2458 int len;
2459 int i;
2460
2461 sprintf(msecs_str, "%lu", usecs);
2462
2463 /* Print msecs */
2464 len = printf("%lu", usecs);
2465
2466 /* Print nsecs (we don't want to exceed 7 numbers) */
2467 if (len < 7) {
2468 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
2469 len += printf(".%s", nsecs_str);
2470 }
2471
2472 printf(" us ");
2473
2474 /* Print remaining spaces to fit the row's width */
2475 for (i = len; i < 7; i++)
2476 printf(" ");
2477
2478 printf("| ");
2479}
2480
2481static void
2482print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec)
2483{
2484 unsigned long long rettime, calltime;
2485 unsigned long long duration, depth;
2486 unsigned long long val;
2487 struct format_field *field;
2488 struct func_map *func;
2489 struct event *ret_event;
2490 int type;
2491 int i;
2492
2493 type = parse_common_type(ret_rec->data);
2494 ret_event = find_event(type);
2495
2496 field = find_field(ret_event, "rettime");
2497 if (!field)
2498 die("can't find rettime in return graph");
2499 rettime = read_size(ret_rec->data + field->offset, field->size);
2500
2501 field = find_field(ret_event, "calltime");
2502 if (!field)
2503 die("can't find rettime in return graph");
2504 calltime = read_size(ret_rec->data + field->offset, field->size);
2505
2506 duration = rettime - calltime;
2507
2508 /* Overhead */
2509 print_graph_overhead(duration);
2510
2511 /* Duration */
2512 print_graph_duration(duration);
2513
2514 field = find_field(event, "depth");
2515 if (!field)
2516 die("can't find depth in entry graph");
2517 depth = read_size(data + field->offset, field->size);
2518
2519 /* Function */
2520 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2521 printf(" ");
2522
2523 field = find_field(event, "func");
2524 if (!field)
2525 die("can't find func in entry graph");
2526 val = read_size(data + field->offset, field->size);
2527 func = find_func(val);
2528
2529 if (func)
2530 printf("%s();", func->func);
2531 else
2532 printf("%llx();", val);
2533}
2534
2535static void print_graph_nested(struct event *event, void *data)
2536{
2537 struct format_field *field;
2538 unsigned long long depth;
2539 unsigned long long val;
2540 struct func_map *func;
2541 int i;
2542
2543 /* No overhead */
2544 print_graph_overhead(-1);
2545
2546 /* No time */
2547 printf(" | ");
2548
2549 field = find_field(event, "depth");
2550 if (!field)
2551 die("can't find depth in entry graph");
2552 depth = read_size(data + field->offset, field->size);
2553
2554 /* Function */
2555 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2556 printf(" ");
2557
2558 field = find_field(event, "func");
2559 if (!field)
2560 die("can't find func in entry graph");
2561 val = read_size(data + field->offset, field->size);
2562 func = find_func(val);
2563
2564 if (func)
2565 printf("%s() {", func->func);
2566 else
2567 printf("%llx() {", val);
2568}
2569
2570static void
2571pretty_print_func_ent(void *data, int size, struct event *event,
2572 int cpu, int pid, const char *comm,
2573 unsigned long secs, unsigned long usecs)
2574{
2575 struct format_field *field;
2576 struct record *rec;
2577 void *copy_data;
2578 unsigned long val;
2579
2580 printf("%5lu.%06lu | ", secs, usecs);
2581
2582 print_graph_cpu(cpu);
2583 print_graph_proc(pid, comm);
2584
2585 printf(" | ");
2586
2587 field = find_field(event, "func");
2588 if (!field)
2589 die("function entry does not have func field");
2590
2591 val = read_size(data + field->offset, field->size);
2592
2593 /*
2594 * peek_data may unmap the data pointer. Copy it first.
2595 */
2596 copy_data = malloc_or_die(size);
2597 memcpy(copy_data, data, size);
2598 data = copy_data;
2599
2600 rec = trace_peek_data(cpu);
2601 if (rec) {
2602 rec = get_return_for_leaf(cpu, pid, val, rec);
2603 if (rec) {
2604 print_graph_entry_leaf(event, data, rec);
2605 goto out_free;
2606 }
2607 }
2608 print_graph_nested(event, data);
2609out_free:
2610 free(data);
2611}
2612
2613static void
2614pretty_print_func_ret(void *data, int size __unused, struct event *event,
2615 int cpu, int pid, const char *comm,
2616 unsigned long secs, unsigned long usecs)
2617{
2618 unsigned long long rettime, calltime;
2619 unsigned long long duration, depth;
2620 struct format_field *field;
2621 int i;
2622
2623 printf("%5lu.%06lu | ", secs, usecs);
2624
2625 print_graph_cpu(cpu);
2626 print_graph_proc(pid, comm);
2627
2628 printf(" | ");
2629
2630 field = find_field(event, "rettime");
2631 if (!field)
2632 die("can't find rettime in return graph");
2633 rettime = read_size(data + field->offset, field->size);
2634
2635 field = find_field(event, "calltime");
2636 if (!field)
2637 die("can't find calltime in return graph");
2638 calltime = read_size(data + field->offset, field->size);
2639
2640 duration = rettime - calltime;
2641
2642 /* Overhead */
2643 print_graph_overhead(duration);
2644
2645 /* Duration */
2646 print_graph_duration(duration);
2647
2648 field = find_field(event, "depth");
2649 if (!field)
2650 die("can't find depth in entry graph");
2651 depth = read_size(data + field->offset, field->size);
2652
2653 /* Function */
2654 for (i = 0; i < (int)(depth * TRACE_GRAPH_INDENT); i++)
2655 printf(" ");
2656
2657 printf("}");
2658}
2659
2660static void
2661pretty_print_func_graph(void *data, int size, struct event *event,
2662 int cpu, int pid, const char *comm,
2663 unsigned long secs, unsigned long usecs)
2664{
2665 if (event->flags & EVENT_FL_ISFUNCENT)
2666 pretty_print_func_ent(data, size, event,
2667 cpu, pid, comm, secs, usecs);
2668 else if (event->flags & EVENT_FL_ISFUNCRET)
2669 pretty_print_func_ret(data, size, event,
2670 cpu, pid, comm, secs, usecs);
2671 printf("\n");
2672}
2673
2674void print_event(int cpu, void *data, int size, unsigned long long nsecs,
2675 char *comm)
2676{
2677 struct event *event;
2678 unsigned long secs;
2679 unsigned long usecs;
2680 int type;
2681 int pid;
2682
2683 secs = nsecs / NSECS_PER_SEC;
2684 nsecs -= secs * NSECS_PER_SEC;
2685 usecs = nsecs / NSECS_PER_USEC;
2686
2687 type = parse_common_type(data);
2688
2689 event = find_event(type);
2690 if (!event)
2691 die("ug! no event found for type %d", type);
2692
2693 pid = parse_common_pid(data);
2694
2695 if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET))
2696 return pretty_print_func_graph(data, size, event, cpu,
2697 pid, comm, secs, usecs);
2698
2699 printf("%16s-%-5d [%03d] %5lu.%06lu: %s: ",
2700 comm, pid, cpu,
2701 secs, usecs, event->name);
2702
2703 pretty_print(data, size, event);
2704 printf("\n");
2705}
2706
2707static void print_fields(struct print_flag_sym *field)
2708{
2709 printf("{ %s, %s }", field->value, field->str);
2710 if (field->next) {
2711 printf(", ");
2712 print_fields(field->next);
2713 }
2714}
2715
2716static void print_args(struct print_arg *args)
2717{
2718 int print_paren = 1;
2719
2720 switch (args->type) {
2721 case PRINT_NULL:
2722 printf("null");
2723 break;
2724 case PRINT_ATOM:
2725 printf("%s", args->atom.atom);
2726 break;
2727 case PRINT_FIELD:
2728 printf("REC->%s", args->field.name);
2729 break;
2730 case PRINT_FLAGS:
2731 printf("__print_flags(");
2732 print_args(args->flags.field);
2733 printf(", %s, ", args->flags.delim);
2734 print_fields(args->flags.flags);
2735 printf(")");
2736 break;
2737 case PRINT_SYMBOL:
2738 printf("__print_symbolic(");
2739 print_args(args->symbol.field);
2740 printf(", ");
2741 print_fields(args->symbol.symbols);
2742 printf(")");
2743 break;
2744 case PRINT_STRING:
2745 printf("__get_str(%s)", args->string.string);
2746 break;
2747 case PRINT_TYPE:
2748 printf("(%s)", args->typecast.type);
2749 print_args(args->typecast.item);
2750 break;
2751 case PRINT_OP:
2752 if (strcmp(args->op.op, ":") == 0)
2753 print_paren = 0;
2754 if (print_paren)
2755 printf("(");
2756 print_args(args->op.left);
2757 printf(" %s ", args->op.op);
2758 print_args(args->op.right);
2759 if (print_paren)
2760 printf(")");
2761 break;
2762 default:
2763 /* we should warn... */
2764 return;
2765 }
2766 if (args->next) {
2767 printf("\n");
2768 print_args(args->next);
2769 }
2770}
2771
2772static void parse_header_field(char *type,
2773 int *offset, int *size)
2774{
2775 char *token;
2776
2777 if (read_expected(EVENT_ITEM, (char *)"field") < 0)
2778 return;
2779 if (read_expected(EVENT_OP, (char *)":") < 0)
2780 return;
2781 /* type */
2782 if (read_expect_type(EVENT_ITEM, &token) < 0)
2783 return;
2784 free_token(token);
2785
2786 if (read_expected(EVENT_ITEM, type) < 0)
2787 return;
2788 if (read_expected(EVENT_OP, (char *)";") < 0)
2789 return;
2790 if (read_expected(EVENT_ITEM, (char *)"offset") < 0)
2791 return;
2792 if (read_expected(EVENT_OP, (char *)":") < 0)
2793 return;
2794 if (read_expect_type(EVENT_ITEM, &token) < 0)
2795 return;
2796 *offset = atoi(token);
2797 free_token(token);
2798 if (read_expected(EVENT_OP, (char *)";") < 0)
2799 return;
2800 if (read_expected(EVENT_ITEM, (char *)"size") < 0)
2801 return;
2802 if (read_expected(EVENT_OP, (char *)":") < 0)
2803 return;
2804 if (read_expect_type(EVENT_ITEM, &token) < 0)
2805 return;
2806 *size = atoi(token);
2807 free_token(token);
2808 if (read_expected(EVENT_OP, (char *)";") < 0)
2809 return;
2810 if (read_expect_type(EVENT_NEWLINE, &token) < 0)
2811 return;
2812 free_token(token);
2813}
2814
2815int parse_header_page(char *buf, unsigned long size)
2816{
2817 init_input_buf(buf, size);
2818
2819 parse_header_field((char *)"timestamp", &header_page_ts_offset,
2820 &header_page_ts_size);
2821 parse_header_field((char *)"commit", &header_page_size_offset,
2822 &header_page_size_size);
2823 parse_header_field((char *)"data", &header_page_data_offset,
2824 &header_page_data_size);
2825
2826 return 0;
2827}
2828
2829int parse_ftrace_file(char *buf, unsigned long size)
2830{
2831 struct format_field *field;
2832 struct print_arg *arg, **list;
2833 struct event *event;
2834 int ret;
2835
2836 init_input_buf(buf, size);
2837
2838 event = alloc_event();
2839 if (!event)
2840 return -ENOMEM;
2841
2842 event->flags |= EVENT_FL_ISFTRACE;
2843
2844 event->name = event_read_name();
2845 if (!event->name)
2846 die("failed to read ftrace event name");
2847
2848 if (strcmp(event->name, "function") == 0)
2849 event->flags |= EVENT_FL_ISFUNC;
2850
2851 else if (strcmp(event->name, "funcgraph_entry") == 0)
2852 event->flags |= EVENT_FL_ISFUNCENT;
2853
2854 else if (strcmp(event->name, "funcgraph_exit") == 0)
2855 event->flags |= EVENT_FL_ISFUNCRET;
2856
2857 else if (strcmp(event->name, "bprint") == 0)
2858 event->flags |= EVENT_FL_ISBPRINT;
2859
2860 event->id = event_read_id();
2861 if (event->id < 0)
2862 die("failed to read ftrace event id");
2863
2864 add_event(event);
2865
2866 ret = event_read_format(event);
2867 if (ret < 0)
2868 die("failed to read ftrace event format");
2869
2870 ret = event_read_print(event);
2871 if (ret < 0)
2872 die("failed to read ftrace event print fmt");
2873
2874 /*
2875 * The arguments for ftrace files are parsed by the fields.
2876 * Set up the fields as their arguments.
2877 */
2878 list = &event->print_fmt.args;
2879 for (field = event->format.fields; field; field = field->next) {
2880 arg = malloc_or_die(sizeof(*arg));
2881 memset(arg, 0, sizeof(*arg));
2882 *list = arg;
2883 list = &arg->next;
2884 arg->type = PRINT_FIELD;
2885 arg->field.name = field->name;
2886 arg->field.field = field;
2887 }
2888 return 0;
2889}
2890
2891int parse_event_file(char *buf, unsigned long size, char *system__unused __unused)
2892{
2893 struct event *event;
2894 int ret;
2895
2896 init_input_buf(buf, size);
2897
2898 event = alloc_event();
2899 if (!event)
2900 return -ENOMEM;
2901
2902 event->name = event_read_name();
2903 if (!event->name)
2904 die("failed to read event name");
2905
2906 event->id = event_read_id();
2907 if (event->id < 0)
2908 die("failed to read event id");
2909
2910 ret = event_read_format(event);
2911 if (ret < 0)
2912 die("failed to read event format");
2913
2914 ret = event_read_print(event);
2915 if (ret < 0)
2916 die("failed to read event print fmt");
2917
2918#define PRINT_ARGS 0
2919 if (PRINT_ARGS && event->print_fmt.args)
2920 print_args(event->print_fmt.args);
2921
2922 add_event(event);
2923 return 0;
2924}
2925
2926void parse_set_info(int nr_cpus, int long_sz)
2927{
2928 cpus = nr_cpus;
2929 long_size = long_sz;
2930}
diff --git a/tools/perf/util/trace-event-read.c b/tools/perf/util/trace-event-read.c
new file mode 100644
index 000000000000..b12e4903739a
--- /dev/null
+++ b/tools/perf/util/trace-event-read.c
@@ -0,0 +1,509 @@
1/*
2 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
3 *
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 *
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!)
9 *
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.
14 *
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
18 *
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21#define _LARGEFILE64_SOURCE
22
23#include <dirent.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <stdarg.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32#include <sys/mman.h>
33#include <pthread.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <errno.h>
38
39#include "../perf.h"
40#include "util.h"
41#include "trace-event.h"
42
43static int input_fd;
44
45static int read_page;
46
47int file_bigendian;
48int host_bigendian;
49static int long_size;
50
51static unsigned long page_size;
52
53static int read_or_die(void *data, int size)
54{
55 int r;
56
57 r = read(input_fd, data, size);
58 if (r != size)
59 die("reading input file (size expected=%d received=%d)",
60 size, r);
61 return r;
62}
63
64static unsigned int read4(void)
65{
66 unsigned int data;
67
68 read_or_die(&data, 4);
69 return __data2host4(data);
70}
71
72static unsigned long long read8(void)
73{
74 unsigned long long data;
75
76 read_or_die(&data, 8);
77 return __data2host8(data);
78}
79
80static char *read_string(void)
81{
82 char buf[BUFSIZ];
83 char *str = NULL;
84 int size = 0;
85 int i;
86 int r;
87
88 for (;;) {
89 r = read(input_fd, buf, BUFSIZ);
90 if (r < 0)
91 die("reading input file");
92
93 if (!r)
94 die("no data");
95
96 for (i = 0; i < r; i++) {
97 if (!buf[i])
98 break;
99 }
100 if (i < r)
101 break;
102
103 if (str) {
104 size += BUFSIZ;
105 str = realloc(str, size);
106 if (!str)
107 die("malloc of size %d", size);
108 memcpy(str + (size - BUFSIZ), buf, BUFSIZ);
109 } else {
110 size = BUFSIZ;
111 str = malloc_or_die(size);
112 memcpy(str, buf, size);
113 }
114 }
115
116 /* move the file descriptor to the end of the string */
117 r = lseek(input_fd, -(r - (i+1)), SEEK_CUR);
118 if (r < 0)
119 die("lseek");
120
121 if (str) {
122 size += i;
123 str = realloc(str, size);
124 if (!str)
125 die("malloc of size %d", size);
126 memcpy(str + (size - i), buf, i);
127 } else {
128 size = i;
129 str = malloc_or_die(i);
130 memcpy(str, buf, i);
131 }
132
133 return str;
134}
135
136static void read_proc_kallsyms(void)
137{
138 unsigned int size;
139 char *buf;
140
141 size = read4();
142 if (!size)
143 return;
144
145 buf = malloc_or_die(size);
146 read_or_die(buf, size);
147
148 parse_proc_kallsyms(buf, size);
149
150 free(buf);
151}
152
153static void read_ftrace_printk(void)
154{
155 unsigned int size;
156 char *buf;
157
158 size = read4();
159 if (!size)
160 return;
161
162 buf = malloc_or_die(size);
163 read_or_die(buf, size);
164
165 parse_ftrace_printk(buf, size);
166
167 free(buf);
168}
169
170static void read_header_files(void)
171{
172 unsigned long long size;
173 char *header_page;
174 char *header_event;
175 char buf[BUFSIZ];
176
177 read_or_die(buf, 12);
178
179 if (memcmp(buf, "header_page", 12) != 0)
180 die("did not read header page");
181
182 size = read8();
183 header_page = malloc_or_die(size);
184 read_or_die(header_page, size);
185 parse_header_page(header_page, size);
186 free(header_page);
187
188 /*
189 * The size field in the page is of type long,
190 * use that instead, since it represents the kernel.
191 */
192 long_size = header_page_size_size;
193
194 read_or_die(buf, 13);
195 if (memcmp(buf, "header_event", 13) != 0)
196 die("did not read header event");
197
198 size = read8();
199 header_event = malloc_or_die(size);
200 read_or_die(header_event, size);
201 free(header_event);
202}
203
204static void read_ftrace_file(unsigned long long size)
205{
206 char *buf;
207
208 buf = malloc_or_die(size);
209 read_or_die(buf, size);
210 parse_ftrace_file(buf, size);
211 free(buf);
212}
213
214static void read_event_file(char *sys, unsigned long long size)
215{
216 char *buf;
217
218 buf = malloc_or_die(size);
219 read_or_die(buf, size);
220 parse_event_file(buf, size, sys);
221 free(buf);
222}
223
224static void read_ftrace_files(void)
225{
226 unsigned long long size;
227 int count;
228 int i;
229
230 count = read4();
231
232 for (i = 0; i < count; i++) {
233 size = read8();
234 read_ftrace_file(size);
235 }
236}
237
238static void read_event_files(void)
239{
240 unsigned long long size;
241 char *sys;
242 int systems;
243 int count;
244 int i,x;
245
246 systems = read4();
247
248 for (i = 0; i < systems; i++) {
249 sys = read_string();
250
251 count = read4();
252 for (x=0; x < count; x++) {
253 size = read8();
254 read_event_file(sys, size);
255 }
256 }
257}
258
259struct cpu_data {
260 unsigned long long offset;
261 unsigned long long size;
262 unsigned long long timestamp;
263 struct record *next;
264 char *page;
265 int cpu;
266 int index;
267 int page_size;
268};
269
270static struct cpu_data *cpu_data;
271
272static void update_cpu_data_index(int cpu)
273{
274 cpu_data[cpu].offset += page_size;
275 cpu_data[cpu].size -= page_size;
276 cpu_data[cpu].index = 0;
277}
278
279static void get_next_page(int cpu)
280{
281 off64_t save_seek;
282 off64_t ret;
283
284 if (!cpu_data[cpu].page)
285 return;
286
287 if (read_page) {
288 if (cpu_data[cpu].size <= page_size) {
289 free(cpu_data[cpu].page);
290 cpu_data[cpu].page = NULL;
291 return;
292 }
293
294 update_cpu_data_index(cpu);
295
296 /* other parts of the code may expect the pointer to not move */
297 save_seek = lseek64(input_fd, 0, SEEK_CUR);
298
299 ret = lseek64(input_fd, cpu_data[cpu].offset, SEEK_SET);
300 if (ret < 0)
301 die("failed to lseek");
302 ret = read(input_fd, cpu_data[cpu].page, page_size);
303 if (ret < 0)
304 die("failed to read page");
305
306 /* reset the file pointer back */
307 lseek64(input_fd, save_seek, SEEK_SET);
308
309 return;
310 }
311
312 munmap(cpu_data[cpu].page, page_size);
313 cpu_data[cpu].page = NULL;
314
315 if (cpu_data[cpu].size <= page_size)
316 return;
317
318 update_cpu_data_index(cpu);
319
320 cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
321 input_fd, cpu_data[cpu].offset);
322 if (cpu_data[cpu].page == MAP_FAILED)
323 die("failed to mmap cpu %d at offset 0x%llx",
324 cpu, cpu_data[cpu].offset);
325}
326
327static unsigned int type_len4host(unsigned int type_len_ts)
328{
329 if (file_bigendian)
330 return (type_len_ts >> 27) & ((1 << 5) - 1);
331 else
332 return type_len_ts & ((1 << 5) - 1);
333}
334
335static unsigned int ts4host(unsigned int type_len_ts)
336{
337 if (file_bigendian)
338 return type_len_ts & ((1 << 27) - 1);
339 else
340 return type_len_ts >> 5;
341}
342
343static int calc_index(void *ptr, int cpu)
344{
345 return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
346}
347
348struct record *trace_peek_data(int cpu)
349{
350 struct record *data;
351 void *page = cpu_data[cpu].page;
352 int idx = cpu_data[cpu].index;
353 void *ptr = page + idx;
354 unsigned long long extend;
355 unsigned int type_len_ts;
356 unsigned int type_len;
357 unsigned int delta;
358 unsigned int length = 0;
359
360 if (cpu_data[cpu].next)
361 return cpu_data[cpu].next;
362
363 if (!page)
364 return NULL;
365
366 if (!idx) {
367 /* FIXME: handle header page */
368 if (header_page_ts_size != 8)
369 die("expected a long long type for timestamp");
370 cpu_data[cpu].timestamp = data2host8(ptr);
371 ptr += 8;
372 switch (header_page_size_size) {
373 case 4:
374 cpu_data[cpu].page_size = data2host4(ptr);
375 ptr += 4;
376 break;
377 case 8:
378 cpu_data[cpu].page_size = data2host8(ptr);
379 ptr += 8;
380 break;
381 default:
382 die("bad long size");
383 }
384 ptr = cpu_data[cpu].page + header_page_data_offset;
385 }
386
387read_again:
388 idx = calc_index(ptr, cpu);
389
390 if (idx >= cpu_data[cpu].page_size) {
391 get_next_page(cpu);
392 return trace_peek_data(cpu);
393 }
394
395 type_len_ts = data2host4(ptr);
396 ptr += 4;
397
398 type_len = type_len4host(type_len_ts);
399 delta = ts4host(type_len_ts);
400
401 switch (type_len) {
402 case RINGBUF_TYPE_PADDING:
403 if (!delta)
404 die("error, hit unexpected end of page");
405 length = data2host4(ptr);
406 ptr += 4;
407 length *= 4;
408 ptr += length;
409 goto read_again;
410
411 case RINGBUF_TYPE_TIME_EXTEND:
412 extend = data2host4(ptr);
413 ptr += 4;
414 extend <<= TS_SHIFT;
415 extend += delta;
416 cpu_data[cpu].timestamp += extend;
417 goto read_again;
418
419 case RINGBUF_TYPE_TIME_STAMP:
420 ptr += 12;
421 break;
422 case 0:
423 length = data2host4(ptr);
424 ptr += 4;
425 die("here! length=%d", length);
426 break;
427 default:
428 length = type_len * 4;
429 break;
430 }
431
432 cpu_data[cpu].timestamp += delta;
433
434 data = malloc_or_die(sizeof(*data));
435 memset(data, 0, sizeof(*data));
436
437 data->ts = cpu_data[cpu].timestamp;
438 data->size = length;
439 data->data = ptr;
440 ptr += length;
441
442 cpu_data[cpu].index = calc_index(ptr, cpu);
443 cpu_data[cpu].next = data;
444
445 return data;
446}
447
448struct record *trace_read_data(int cpu)
449{
450 struct record *data;
451
452 data = trace_peek_data(cpu);
453 cpu_data[cpu].next = NULL;
454
455 return data;
456}
457
458void trace_report (void)
459{
460 const char *input_file = "trace.info";
461 char buf[BUFSIZ];
462 char test[] = { 23, 8, 68 };
463 char *version;
464 int show_funcs = 0;
465 int show_printk = 0;
466
467 input_fd = open(input_file, O_RDONLY);
468 if (input_fd < 0)
469 die("opening '%s'\n", input_file);
470
471 read_or_die(buf, 3);
472 if (memcmp(buf, test, 3) != 0)
473 die("not an trace data file");
474
475 read_or_die(buf, 7);
476 if (memcmp(buf, "tracing", 7) != 0)
477 die("not a trace file (missing tracing)");
478
479 version = read_string();
480 printf("version = %s\n", version);
481 free(version);
482
483 read_or_die(buf, 1);
484 file_bigendian = buf[0];
485 host_bigendian = bigendian();
486
487 read_or_die(buf, 1);
488 long_size = buf[0];
489
490 page_size = read4();
491
492 read_header_files();
493
494 read_ftrace_files();
495 read_event_files();
496 read_proc_kallsyms();
497 read_ftrace_printk();
498
499 if (show_funcs) {
500 print_funcs();
501 return;
502 }
503 if (show_printk) {
504 print_printk();
505 return;
506 }
507
508 return;
509}
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
new file mode 100644
index 000000000000..051fcf363825
--- /dev/null
+++ b/tools/perf/util/trace-event.h
@@ -0,0 +1,239 @@
1#ifndef _TRACE_EVENTS_H
2#define _TRACE_EVENTS_H
3
4#include "parse-events.h"
5
6#define __unused __attribute__((unused))
7
8
9#ifndef PAGE_MASK
10#define PAGE_MASK (page_size - 1)
11#endif
12
13enum {
14 RINGBUF_TYPE_PADDING = 29,
15 RINGBUF_TYPE_TIME_EXTEND = 30,
16 RINGBUF_TYPE_TIME_STAMP = 31,
17};
18
19#ifndef TS_SHIFT
20#define TS_SHIFT 27
21#endif
22
23#define NSECS_PER_SEC 1000000000ULL
24#define NSECS_PER_USEC 1000ULL
25
26enum format_flags {
27 FIELD_IS_ARRAY = 1,
28 FIELD_IS_POINTER = 2,
29};
30
31struct format_field {
32 struct format_field *next;
33 char *type;
34 char *name;
35 int offset;
36 int size;
37 unsigned long flags;
38};
39
40struct format {
41 int nr_common;
42 int nr_fields;
43 struct format_field *common_fields;
44 struct format_field *fields;
45};
46
47struct print_arg_atom {
48 char *atom;
49};
50
51struct print_arg_string {
52 char *string;
53};
54
55struct print_arg_field {
56 char *name;
57 struct format_field *field;
58};
59
60struct print_flag_sym {
61 struct print_flag_sym *next;
62 char *value;
63 char *str;
64};
65
66struct print_arg_typecast {
67 char *type;
68 struct print_arg *item;
69};
70
71struct print_arg_flags {
72 struct print_arg *field;
73 char *delim;
74 struct print_flag_sym *flags;
75};
76
77struct print_arg_symbol {
78 struct print_arg *field;
79 struct print_flag_sym *symbols;
80};
81
82struct print_arg;
83
84struct print_arg_op {
85 char *op;
86 int prio;
87 struct print_arg *left;
88 struct print_arg *right;
89};
90
91struct print_arg_func {
92 char *name;
93 struct print_arg *args;
94};
95
96enum print_arg_type {
97 PRINT_NULL,
98 PRINT_ATOM,
99 PRINT_FIELD,
100 PRINT_FLAGS,
101 PRINT_SYMBOL,
102 PRINT_TYPE,
103 PRINT_STRING,
104 PRINT_OP,
105};
106
107struct print_arg {
108 struct print_arg *next;
109 enum print_arg_type type;
110 union {
111 struct print_arg_atom atom;
112 struct print_arg_field field;
113 struct print_arg_typecast typecast;
114 struct print_arg_flags flags;
115 struct print_arg_symbol symbol;
116 struct print_arg_func func;
117 struct print_arg_string string;
118 struct print_arg_op op;
119 };
120};
121
122struct print_fmt {
123 char *format;
124 struct print_arg *args;
125};
126
127struct event {
128 struct event *next;
129 char *name;
130 int id;
131 int flags;
132 struct format format;
133 struct print_fmt print_fmt;
134};
135
136enum {
137 EVENT_FL_ISFTRACE = 1,
138 EVENT_FL_ISPRINT = 2,
139 EVENT_FL_ISBPRINT = 4,
140 EVENT_FL_ISFUNC = 8,
141 EVENT_FL_ISFUNCENT = 16,
142 EVENT_FL_ISFUNCRET = 32,
143};
144
145struct record {
146 unsigned long long ts;
147 int size;
148 void *data;
149};
150
151struct record *trace_peek_data(int cpu);
152struct record *trace_read_data(int cpu);
153
154void parse_set_info(int nr_cpus, int long_sz);
155
156void trace_report(void);
157
158void *malloc_or_die(unsigned int size);
159
160void parse_cmdlines(char *file, int size);
161void parse_proc_kallsyms(char *file, unsigned int size);
162void parse_ftrace_printk(char *file, unsigned int size);
163
164void print_funcs(void);
165void print_printk(void);
166
167int parse_ftrace_file(char *buf, unsigned long size);
168int parse_event_file(char *buf, unsigned long size, char *system);
169void print_event(int cpu, void *data, int size, unsigned long long nsecs,
170 char *comm);
171
172extern int file_bigendian;
173extern int host_bigendian;
174
175int bigendian(void);
176
177static inline unsigned short __data2host2(unsigned short data)
178{
179 unsigned short swap;
180
181 if (host_bigendian == file_bigendian)
182 return data;
183
184 swap = ((data & 0xffULL) << 8) |
185 ((data & (0xffULL << 8)) >> 8);
186
187 return swap;
188}
189
190static inline unsigned int __data2host4(unsigned int data)
191{
192 unsigned int swap;
193
194 if (host_bigendian == file_bigendian)
195 return data;
196
197 swap = ((data & 0xffULL) << 24) |
198 ((data & (0xffULL << 8)) << 8) |
199 ((data & (0xffULL << 16)) >> 8) |
200 ((data & (0xffULL << 24)) >> 24);
201
202 return swap;
203}
204
205static inline unsigned long long __data2host8(unsigned long long data)
206{
207 unsigned long long swap;
208
209 if (host_bigendian == file_bigendian)
210 return data;
211
212 swap = ((data & 0xffULL) << 56) |
213 ((data & (0xffULL << 8)) << 40) |
214 ((data & (0xffULL << 16)) << 24) |
215 ((data & (0xffULL << 24)) << 8) |
216 ((data & (0xffULL << 32)) >> 8) |
217 ((data & (0xffULL << 40)) >> 24) |
218 ((data & (0xffULL << 48)) >> 40) |
219 ((data & (0xffULL << 56)) >> 56);
220
221 return swap;
222}
223
224#define data2host2(ptr) __data2host2(*(unsigned short *)ptr)
225#define data2host4(ptr) __data2host4(*(unsigned int *)ptr)
226#define data2host8(ptr) __data2host8(*(unsigned long long *)ptr)
227
228extern int header_page_ts_offset;
229extern int header_page_ts_size;
230extern int header_page_size_offset;
231extern int header_page_size_size;
232extern int header_page_data_offset;
233extern int header_page_data_size;
234
235int parse_header_page(char *buf, unsigned long size);
236
237void read_tracing_data(struct perf_counter_attr *pattrs, int nb_counters);
238
239#endif /* _TRACE_EVENTS_H */
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 15004d211663..9de2329dd44d 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -307,6 +307,7 @@ static inline int has_extension(const char *filename, const char *ext)
307#undef isspace 307#undef isspace
308#undef isdigit 308#undef isdigit
309#undef isalpha 309#undef isalpha
310#undef isprint
310#undef isalnum 311#undef isalnum
311#undef tolower 312#undef tolower
312#undef toupper 313#undef toupper