blob: 3b7ce1bf9f8e8e8cacf54c616631fcdccfa42a4c [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "event.h"
2#include "symbol.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02006#include "debug.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02007
8static inline int is_anon_memory(const char *filename)
9{
10 return strcmp(filename, "//anon") == 0;
11}
12
13static int strcommon(const char *pathname, char *cwd, int cwdlen)
14{
15 int n = 0;
16
17 while (n < cwdlen && pathname[n] == cwd[n])
18 ++n;
19
20 return n;
21}
22
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020023void map__init(struct map *self, u64 start, u64 end, u64 pgoff,
24 struct dso *dso)
25{
26 self->start = start;
27 self->end = end;
28 self->pgoff = pgoff;
29 self->dso = dso;
30 self->map_ip = map__map_ip;
31 self->unmap_ip = map__unmap_ip;
32 RB_CLEAR_NODE(&self->rb_node);
33}
34
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020035struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen,
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -020036 unsigned int sym_priv_size)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020037{
38 struct map *self = malloc(sizeof(*self));
39
40 if (self != NULL) {
41 const char *filename = event->filename;
42 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020043 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020044 int anon;
45
46 if (cwd) {
47 int n = strcommon(filename, cwd, cwdlen);
48
49 if (n == cwdlen) {
50 snprintf(newfilename, sizeof(newfilename),
51 ".%s", filename + n);
52 filename = newfilename;
53 }
54 }
55
56 anon = is_anon_memory(filename);
57
58 if (anon) {
59 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
60 filename = newfilename;
61 }
62
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020063 dso = dsos__findnew(filename, sym_priv_size);
64 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020065 goto out_delete;
66
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020067 map__init(self, event->start, event->start + event->len,
68 event->pgoff, dso);
69
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020070 if (self->dso == vdso || anon)
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020071 self->map_ip = self->unmap_ip = identity__map_ip;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020072 }
73 return self;
74out_delete:
75 free(self);
76 return NULL;
77}
78
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -020079struct symbol *
80map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter)
81{
82 if (!self->dso->loaded) {
83 int nr = dso__load(self->dso, self, filter);
84
85 if (nr < 0) {
86 pr_warning("Failed to open %s, continuing without symbols\n",
87 self->dso->long_name);
88 return NULL;
89 } else if (nr == 0) {
90 pr_warning("No symbols found in %s, maybe install a debug package?\n",
91 self->dso->long_name);
92 return NULL;
93 }
94 }
95
96 return self->dso->find_symbol(self->dso, ip);
97}
98
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020099struct map *map__clone(struct map *self)
100{
101 struct map *map = malloc(sizeof(*self));
102
103 if (!map)
104 return NULL;
105
106 memcpy(map, self, sizeof(*self));
107
108 return map;
109}
110
111int map__overlap(struct map *l, struct map *r)
112{
113 if (l->start > r->start) {
114 struct map *t = l;
115 l = r;
116 r = t;
117 }
118
119 if (l->end > r->start)
120 return 1;
121
122 return 0;
123}
124
125size_t map__fprintf(struct map *self, FILE *fp)
126{
127 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
128 self->start, self->end, self->pgoff, self->dso->name);
129}