14cb8465eb089817aa38b195c526c8b716fa344a
[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 ret, 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         ret = fstat(input, &input_stat);
136         if (ret < 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         *pheader = perf_header__read(input);
153         header = *pheader;
154         head = header->data_offset;
155
156         sample_type = perf_header__sample_type(header);
157
158         if (curr_handler->sample_type_check)
159                 if (curr_handler->sample_type_check(sample_type) < 0)
160                         exit(-1);
161
162         if (load_kernel(NULL) < 0) {
163                 perror("failed to load kernel symbols");
164                 return EXIT_FAILURE;
165         }
166
167         if (!full_paths) {
168                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
169                         perror("failed to get the current directory");
170                         return EXIT_FAILURE;
171                 }
172                 *cwd = __cwd;
173                 *cwdlen = strlen(*cwd);
174         } else {
175                 *cwd = NULL;
176                 *cwdlen = 0;
177         }
178
179         shift = page_size * (head / page_size);
180         offset += shift;
181         head -= shift;
182
183 remap:
184         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
185                            MAP_SHARED, input, offset);
186         if (buf == MAP_FAILED) {
187                 perror("failed to mmap file");
188                 exit(-1);
189         }
190
191 more:
192         event = (event_t *)(buf + head);
193
194         size = event->header.size;
195         if (!size)
196                 size = 8;
197
198         if (head + event->header.size >= page_size * mmap_window) {
199                 int munmap_ret;
200
201                 shift = page_size * (head / page_size);
202
203                 munmap_ret = munmap(buf, page_size * mmap_window);
204                 assert(munmap_ret == 0);
205
206                 offset += shift;
207                 head -= shift;
208                 goto remap;
209         }
210
211         size = event->header.size;
212
213         dump_printf("\n%p [%p]: event: %d\n",
214                         (void *)(offset + head),
215                         (void *)(long)event->header.size,
216                         event->header.type);
217
218         if (!size || process_event(event, offset, head) < 0) {
219
220                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
221                         (void *)(offset + head),
222                         (void *)(long)(event->header.size),
223                         event->header.type);
224
225                 /*
226                  * assume we lost track of the stream, check alignment, and
227                  * increment a single u64 in the hope to catch on again 'soon'.
228                  */
229
230                 if (unlikely(head & 7))
231                         head &= ~7ULL;
232
233                 size = 8;
234         }
235
236         head += size;
237
238         if (offset + head >= header->data_offset + header->data_size)
239                 goto done;
240
241         if (offset + head < (unsigned long)input_stat.st_size)
242                 goto more;
243
244 done:
245         rc = EXIT_SUCCESS;
246         close(input);
247
248         return rc;
249 }
250
251