perf tools: Delay loading symtabs till we hit a map with it
[linux-2.6/libata-dev.git] / tools / perf / util / map.c
blobd302e513e0622b486ecfc55d343d064cada24ac2
1 #include "event.h"
2 #include "symbol.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "debug.h"
8 static inline int is_anon_memory(const char *filename)
10 return strcmp(filename, "//anon") == 0;
13 static int strcommon(const char *pathname, char *cwd, int cwdlen)
15 int n = 0;
17 while (n < cwdlen && pathname[n] == cwd[n])
18 ++n;
20 return n;
23 struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen,
24 unsigned int sym_priv_size)
26 struct map *self = malloc(sizeof(*self));
28 if (self != NULL) {
29 const char *filename = event->filename;
30 char newfilename[PATH_MAX];
31 int anon;
33 if (cwd) {
34 int n = strcommon(filename, cwd, cwdlen);
36 if (n == cwdlen) {
37 snprintf(newfilename, sizeof(newfilename),
38 ".%s", filename + n);
39 filename = newfilename;
43 anon = is_anon_memory(filename);
45 if (anon) {
46 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
47 filename = newfilename;
50 self->start = event->start;
51 self->end = event->start + event->len;
52 self->pgoff = event->pgoff;
54 self->dso = dsos__findnew(filename, sym_priv_size);
55 if (self->dso == NULL)
56 goto out_delete;
58 if (self->dso == vdso || anon)
59 self->map_ip = self->unmap_ip = identity__map_ip;
60 else {
61 self->map_ip = map__map_ip;
62 self->unmap_ip = map__unmap_ip;
65 return self;
66 out_delete:
67 free(self);
68 return NULL;
71 struct symbol *
72 map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter)
74 if (!self->dso->loaded) {
75 int nr = dso__load(self->dso, self, filter);
77 if (nr < 0) {
78 pr_warning("Failed to open %s, continuing without symbols\n",
79 self->dso->long_name);
80 return NULL;
81 } else if (nr == 0) {
82 pr_warning("No symbols found in %s, maybe install a debug package?\n",
83 self->dso->long_name);
84 return NULL;
88 return self->dso->find_symbol(self->dso, ip);
91 struct map *map__clone(struct map *self)
93 struct map *map = malloc(sizeof(*self));
95 if (!map)
96 return NULL;
98 memcpy(map, self, sizeof(*self));
100 return map;
103 int map__overlap(struct map *l, struct map *r)
105 if (l->start > r->start) {
106 struct map *t = l;
107 l = r;
108 r = t;
111 if (l->end > r->start)
112 return 1;
114 return 0;
117 size_t map__fprintf(struct map *self, FILE *fp)
119 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
120 self->start, self->end, self->pgoff, self->dso->name);