perf tools: perf_header__read() shouldn't die()
[linux-2.6.git] / tools / perf / util / data_map.c
1 #include "data_map.h"
2 #include "symbol.h"
3 #include "util.h"
4 #include "debug.h"
5
6
7 static struct perf_file_handler *curr_handler;
8 static unsigned long    mmap_window = 32;
9 static char             __cwd[PATH_MAX];
10
11 static int
12 process_event_stub(event_t *event __used,
13                    unsigned long offset __used,
14                    unsigned long head __used)
15 {
16         return 0;
17 }
18
19 void register_perf_file_handler(struct perf_file_handler *handler)
20 {
21         if (!handler->process_sample_event)
22                 handler->process_sample_event = process_event_stub;
23         if (!handler->process_mmap_event)
24                 handler->process_mmap_event = process_event_stub;
25         if (!handler->process_comm_event)
26                 handler->process_comm_event = process_event_stub;
27         if (!handler->process_fork_event)
28                 handler->process_fork_event = process_event_stub;
29         if (!handler->process_exit_event)
30                 handler->process_exit_event = process_event_stub;
31         if (!handler->process_lost_event)
32                 handler->process_lost_event = process_event_stub;
33         if (!handler->process_read_event)
34                 handler->process_read_event = process_event_stub;
35         if (!handler->process_throttle_event)
36                 handler->process_throttle_event = process_event_stub;
37         if (!handler->process_unthrottle_event)
38                 handler->process_unthrottle_event = process_event_stub;
39
40         curr_handler = handler;
41 }
42
43 static int
44 process_event(event_t *event, unsigned long offset, unsigned long head)
45 {
46         trace_event(event);
47
48         switch (event->header.type) {
49         case PERF_RECORD_SAMPLE:
50                 return curr_handler->process_sample_event(event, offset, head);
51         case PERF_RECORD_MMAP:
52                 return curr_handler->process_mmap_event(event, offset, head);
53         case PERF_RECORD_COMM:
54                 return curr_handler->process_comm_event(event, offset, head);
55         case PERF_RECORD_FORK:
56                 return curr_handler->process_fork_event(event, offset, head);
57         case PERF_RECORD_EXIT:
58                 return curr_handler->process_exit_event(event, offset, head);
59         case PERF_RECORD_LOST:
60                 return curr_handler->process_lost_event(event, offset, head);
61         case PERF_RECORD_READ:
62                 return curr_handler->process_read_event(event, offset, head);
63         case PERF_RECORD_THROTTLE:
64                 return curr_handler->process_throttle_event(event, offset, head);
65         case PERF_RECORD_UNTHROTTLE:
66                 return curr_handler->process_unthrottle_event(event, offset, head);
67         default:
68                 curr_handler->total_unknown++;
69                 return -1;
70         }
71 }
72
73 int perf_header__read_build_ids(int input, off_t offset, off_t size)
74 {
75         struct build_id_event bev;
76         char filename[PATH_MAX];
77         off_t limit = offset + size;
78         int err = -1;
79
80         while (offset < limit) {
81                 struct dso *dso;
82                 ssize_t len;
83
84                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
85                         goto out;
86
87                 len = bev.header.size - sizeof(bev);
88                 if (read(input, filename, len) != len)
89                         goto out;
90
91                 dso = dsos__findnew(filename);
92                 if (dso != NULL)
93                         dso__set_build_id(dso, &bev.build_id);
94
95                 offset += bev.header.size;
96         }
97         err = 0;
98 out:
99         return err;
100 }
101
102 int mmap_dispatch_perf_file(struct perf_header **pheader,
103                             const char *input_name,
104                             int force,
105                             int full_paths,
106                             int *cwdlen,
107                             char **cwd)
108 {
109         int err, rc = EXIT_FAILURE;
110         struct perf_header *header;
111         unsigned long head, shift;
112         unsigned long offset = 0;
113         struct stat input_stat;
114         size_t  page_size;
115         u64 sample_type;
116         event_t *event;
117         uint32_t size;
118         int input;
119         char *buf;
120
121         if (!curr_handler)
122                 die("Forgot to register perf file handler");
123
124         page_size = getpagesize();
125
126         input = open(input_name, O_RDONLY);
127         if (input < 0) {
128                 fprintf(stderr, " failed to open file: %s", input_name);
129                 if (!strcmp(input_name, "perf.data"))
130                         fprintf(stderr, "  (try 'perf record' first)");
131                 fprintf(stderr, "\n");
132                 exit(-1);
133         }
134
135         err = fstat(input, &input_stat);
136         if (err < 0) {
137                 perror("failed to stat file");
138                 exit(-1);
139         }
140
141         if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
142                 fprintf(stderr, "file: %s not owned by current user or root\n",
143                         input_name);
144                 exit(-1);
145         }
146
147         if (!input_stat.st_size) {
148                 fprintf(stderr, "zero-sized file, nothing to do!\n");
149                 exit(0);
150         }
151
152         header = perf_header__new();
153         if (header == NULL)
154                 return -ENOMEM;
155
156         err = perf_header__read(header, input);
157         if (err < 0) {
158                 perf_header__delete(header);
159                 return err;
160         }
161         *pheader = header;
162         head = header->data_offset;
163
164         sample_type = perf_header__sample_type(header);
165
166         if (curr_handler->sample_type_check)
167                 if (curr_handler->sample_type_check(sample_type) < 0)
168                         exit(-1);
169
170         if (load_kernel(NULL) < 0) {
171                 perror("failed to load kernel symbols");
172                 return EXIT_FAILURE;
173         }
174
175         if (!full_paths) {
176                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
177                         perror("failed to get the current directory");
178                         return EXIT_FAILURE;
179                 }
180                 *cwd = __cwd;
181                 *cwdlen = strlen(*cwd);
182         } else {
183                 *cwd = NULL;
184                 *cwdlen = 0;
185         }
186
187         shift = page_size * (head / page_size);
188         offset += shift;
189         head -= shift;
190
191 remap:
192         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
193                            MAP_SHARED, input, offset);
194         if (buf == MAP_FAILED) {
195                 perror("failed to mmap file");
196                 exit(-1);
197         }
198
199 more:
200         event = (event_t *)(buf + head);
201
202         size = event->header.size;
203         if (!size)
204                 size = 8;
205
206         if (head + event->header.size >= page_size * mmap_window) {
207                 int munmap_ret;
208
209                 shift = page_size * (head / page_size);
210
211                 munmap_ret = munmap(buf, page_size * mmap_window);
212                 assert(munmap_ret == 0);
213
214                 offset += shift;
215                 head -= shift;
216                 goto remap;
217         }
218
219         size = event->header.size;
220
221         dump_printf("\n%p [%p]: event: %d\n",
222                         (void *)(offset + head),
223                         (void *)(long)event->header.size,
224                         event->header.type);
225
226         if (!size || process_event(event, offset, head) < 0) {
227
228                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
229                         (void *)(offset + head),
230                         (void *)(long)(event->header.size),
231                         event->header.type);
232
233                 /*
234                  * assume we lost track of the stream, check alignment, and
235                  * increment a single u64 in the hope to catch on again 'soon'.
236                  */
237
238                 if (unlikely(head & 7))
239                         head &= ~7ULL;
240
241                 size = 8;
242         }
243
244         head += size;
245
246         if (offset + head >= header->data_offset + header->data_size)
247                 goto done;
248
249         if (offset + head < (unsigned long)input_stat.st_size)
250                 goto more;
251
252 done:
253         rc = EXIT_SUCCESS;
254         close(input);
255
256         return rc;
257 }
258
259