1 #include <linux/kernel.h>
10 static int perf_session__open(struct perf_session *self, bool force)
12 struct stat input_stat;
14 self->fd = open(self->filename, O_RDONLY);
16 pr_err("failed to open file: %s", self->filename);
17 if (!strcmp(self->filename, "perf.data"))
18 pr_err(" (try 'perf record' first)");
23 if (fstat(self->fd, &input_stat) < 0)
26 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
27 pr_err("file %s not owned by current user or root\n",
32 if (!input_stat.st_size) {
33 pr_info("zero-sized file (%s), nothing to do!\n",
38 if (perf_header__read(&self->header, self->fd) < 0) {
39 pr_err("incompatible file format");
43 self->size = input_stat.st_size;
52 struct perf_session *perf_session__new(const char *filename, int mode, bool force)
54 size_t len = filename ? strlen(filename) + 1 : 0;
55 struct perf_session *self = zalloc(sizeof(*self) + len);
60 if (perf_header__init(&self->header) < 0)
63 memcpy(self->filename, filename, len);
64 self->threads = RB_ROOT;
65 self->last_match = NULL;
66 self->mmap_window = 32;
69 self->unknown_events = 0;
70 map_groups__init(&self->kmaps);
72 if (mode == O_RDONLY && perf_session__open(self, force) < 0)
75 self->sample_type = perf_header__sample_type(&self->header);
82 perf_session__delete(self);
86 void perf_session__delete(struct perf_session *self)
88 perf_header__exit(&self->header);
94 static bool symbol__match_parent_regex(struct symbol *sym)
96 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
102 struct symbol **perf_session__resolve_callchain(struct perf_session *self,
103 struct thread *thread,
104 struct ip_callchain *chain,
105 struct symbol **parent)
107 u8 cpumode = PERF_RECORD_MISC_USER;
108 struct symbol **syms = NULL;
111 if (symbol_conf.use_callchain) {
112 syms = calloc(chain->nr, sizeof(*syms));
114 fprintf(stderr, "Can't allocate memory for symbols\n");
119 for (i = 0; i < chain->nr; i++) {
120 u64 ip = chain->ips[i];
121 struct addr_location al;
123 if (ip >= PERF_CONTEXT_MAX) {
125 case PERF_CONTEXT_HV:
126 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
127 case PERF_CONTEXT_KERNEL:
128 cpumode = PERF_RECORD_MISC_KERNEL; break;
129 case PERF_CONTEXT_USER:
130 cpumode = PERF_RECORD_MISC_USER; break;
137 thread__find_addr_location(thread, self, cpumode,
138 MAP__FUNCTION, ip, &al, NULL);
139 if (al.sym != NULL) {
140 if (sort__has_parent && !*parent &&
141 symbol__match_parent_regex(al.sym))
143 if (!symbol_conf.use_callchain)
152 static int process_event_stub(event_t *event __used,
153 struct perf_session *session __used)
155 dump_printf(": unhandled!\n");
159 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
161 if (handler->sample == NULL)
162 handler->sample = process_event_stub;
163 if (handler->mmap == NULL)
164 handler->mmap = process_event_stub;
165 if (handler->comm == NULL)
166 handler->comm = process_event_stub;
167 if (handler->fork == NULL)
168 handler->fork = process_event_stub;
169 if (handler->exit == NULL)
170 handler->exit = process_event_stub;
171 if (handler->lost == NULL)
172 handler->lost = process_event_stub;
173 if (handler->read == NULL)
174 handler->read = process_event_stub;
175 if (handler->throttle == NULL)
176 handler->throttle = process_event_stub;
177 if (handler->unthrottle == NULL)
178 handler->unthrottle = process_event_stub;
181 static const char *event__name[] = {
183 [PERF_RECORD_MMAP] = "MMAP",
184 [PERF_RECORD_LOST] = "LOST",
185 [PERF_RECORD_COMM] = "COMM",
186 [PERF_RECORD_EXIT] = "EXIT",
187 [PERF_RECORD_THROTTLE] = "THROTTLE",
188 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
189 [PERF_RECORD_FORK] = "FORK",
190 [PERF_RECORD_READ] = "READ",
191 [PERF_RECORD_SAMPLE] = "SAMPLE",
194 unsigned long event__total[PERF_RECORD_MAX];
196 void event__print_totals(void)
199 for (i = 0; i < PERF_RECORD_MAX; ++i)
200 pr_info("%10s events: %10ld\n",
201 event__name[i], event__total[i]);
204 static int perf_session__process_event(struct perf_session *self,
206 struct perf_event_ops *ops,
207 unsigned long offset, unsigned long head)
211 if (event->header.type < PERF_RECORD_MAX) {
212 dump_printf("%#lx [%#x]: PERF_RECORD_%s",
213 offset + head, event->header.size,
214 event__name[event->header.type]);
216 ++event__total[event->header.type];
219 switch (event->header.type) {
220 case PERF_RECORD_SAMPLE:
221 return ops->sample(event, self);
222 case PERF_RECORD_MMAP:
223 return ops->mmap(event, self);
224 case PERF_RECORD_COMM:
225 return ops->comm(event, self);
226 case PERF_RECORD_FORK:
227 return ops->fork(event, self);
228 case PERF_RECORD_EXIT:
229 return ops->exit(event, self);
230 case PERF_RECORD_LOST:
231 return ops->lost(event, self);
232 case PERF_RECORD_READ:
233 return ops->read(event, self);
234 case PERF_RECORD_THROTTLE:
235 return ops->throttle(event, self);
236 case PERF_RECORD_UNTHROTTLE:
237 return ops->unthrottle(event, self);
239 self->unknown_events++;
244 int perf_header__read_build_ids(int input, u64 offset, u64 size)
246 struct build_id_event bev;
247 char filename[PATH_MAX];
248 u64 limit = offset + size;
251 while (offset < limit) {
254 struct list_head *head = &dsos__user;
256 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
259 len = bev.header.size - sizeof(bev);
260 if (read(input, filename, len) != len)
263 if (bev.header.misc & PERF_RECORD_MISC_KERNEL)
264 head = &dsos__kernel;
266 dso = __dsos__findnew(head, filename);
268 dso__set_build_id(dso, &bev.build_id);
269 if (head == &dsos__kernel && filename[0] == '[')
273 offset += bev.header.size;
280 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
282 struct thread *thread = perf_session__findnew(self, 0);
284 if (thread == NULL || thread__set_comm(thread, "swapper")) {
285 pr_err("problem inserting idle task.\n");
292 int perf_session__process_events(struct perf_session *self,
293 struct perf_event_ops *ops)
296 unsigned long head, shift;
297 unsigned long offset = 0;
303 if (perf_session__register_idle_thread(self) == NULL)
306 perf_event_ops__fill_defaults(ops);
308 page_size = getpagesize();
310 head = self->header.data_offset;
312 if (!symbol_conf.full_paths) {
315 if (getcwd(bf, sizeof(bf)) == NULL) {
318 pr_err("failed to get the current directory\n");
321 self->cwd = strdup(bf);
322 if (self->cwd == NULL) {
326 self->cwdlen = strlen(self->cwd);
329 shift = page_size * (head / page_size);
334 buf = mmap(NULL, page_size * self->mmap_window, PROT_READ,
335 MAP_SHARED, self->fd, offset);
336 if (buf == MAP_FAILED) {
337 pr_err("failed to mmap file\n");
343 event = (event_t *)(buf + head);
345 size = event->header.size;
349 if (head + event->header.size >= page_size * self->mmap_window) {
352 shift = page_size * (head / page_size);
354 munmap_ret = munmap(buf, page_size * self->mmap_window);
355 assert(munmap_ret == 0);
362 size = event->header.size;
364 dump_printf("\n%#lx [%#x]: event: %d\n",
365 offset + head, event->header.size, event->header.type);
368 perf_session__process_event(self, event, ops, offset, head) < 0) {
369 dump_printf("%#lx [%#x]: skipping unknown header type: %d\n",
370 offset + head, event->header.size,
373 * assume we lost track of the stream, check alignment, and
374 * increment a single u64 in the hope to catch on again 'soon'.
376 if (unlikely(head & 7))
384 if (offset + head >= self->header.data_offset + self->header.data_size)
387 if (offset + head < self->size)
395 bool perf_session__has_traces(struct perf_session *self, const char *msg)
397 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
398 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
405 int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self,
406 const char *symbol_name,
411 self->ref_reloc_sym.name = strdup(symbol_name);
412 if (self->ref_reloc_sym.name == NULL)
415 bracket = strchr(self->ref_reloc_sym.name, ']');
419 self->ref_reloc_sym.addr = addr;
423 static u64 map__reloc_map_ip(struct map *map, u64 ip)
425 return ip + (s64)map->pgoff;
428 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
430 return ip - (s64)map->pgoff;
433 void perf_session__reloc_vmlinux_maps(struct perf_session *self,
434 u64 unrelocated_addr)
437 s64 reloc = unrelocated_addr - self->ref_reloc_sym.addr;
442 for (type = 0; type < MAP__NR_TYPES; ++type) {
443 struct map *map = self->vmlinux_maps[type];
445 map->map_ip = map__reloc_map_ip;
446 map->unmap_ip = map__reloc_unmap_ip;