perf probe: Reject second attempt of adding same-name event
[linux-2.6-block.git] / tools / perf / util / map.c
CommitLineData
66e274f3
FW
1#include "event.h"
2#include "symbol.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
e4204992 6#include "debug.h"
66e274f3
FW
7
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
3610583c
ACM
23void map__init(struct map *self, enum map_type type,
24 u64 start, u64 end, u64 pgoff, struct dso *dso)
afb7b4f0 25{
3610583c 26 self->type = type;
afb7b4f0
ACM
27 self->start = start;
28 self->end = end;
29 self->pgoff = pgoff;
30 self->dso = dso;
31 self->map_ip = map__map_ip;
32 self->unmap_ip = map__unmap_ip;
33 RB_CLEAR_NODE(&self->rb_node);
34}
35
3610583c
ACM
36struct map *map__new(struct mmap_event *event, enum map_type type,
37 char *cwd, int cwdlen)
66e274f3
FW
38{
39 struct map *self = malloc(sizeof(*self));
40
41 if (self != NULL) {
42 const char *filename = event->filename;
43 char newfilename[PATH_MAX];
afb7b4f0 44 struct dso *dso;
66e274f3
FW
45 int anon;
46
47 if (cwd) {
48 int n = strcommon(filename, cwd, cwdlen);
49
50 if (n == cwdlen) {
51 snprintf(newfilename, sizeof(newfilename),
52 ".%s", filename + n);
53 filename = newfilename;
54 }
55 }
56
57 anon = is_anon_memory(filename);
58
59 if (anon) {
60 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
61 filename = newfilename;
62 }
63
00a192b3 64 dso = dsos__findnew(filename);
afb7b4f0 65 if (dso == NULL)
66e274f3
FW
66 goto out_delete;
67
3610583c 68 map__init(self, type, event->start, event->start + event->len,
afb7b4f0
ACM
69 event->pgoff, dso);
70
66e274f3 71 if (self->dso == vdso || anon)
ed52ce2e 72 self->map_ip = self->unmap_ip = identity__map_ip;
66e274f3
FW
73 }
74 return self;
75out_delete:
76 free(self);
77 return NULL;
78}
79
c338aee8
ACM
80void map__delete(struct map *self)
81{
82 free(self);
83}
84
6a4694a4 85void map__fixup_start(struct map *self)
c338aee8 86{
6a4694a4 87 struct rb_root *symbols = &self->dso->symbols[self->type];
fcf1203a 88 struct rb_node *nd = rb_first(symbols);
c338aee8
ACM
89 if (nd != NULL) {
90 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
91 self->start = sym->start;
92 }
93}
94
6a4694a4 95void map__fixup_end(struct map *self)
c338aee8 96{
6a4694a4 97 struct rb_root *symbols = &self->dso->symbols[self->type];
fcf1203a 98 struct rb_node *nd = rb_last(symbols);
c338aee8
ACM
99 if (nd != NULL) {
100 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
101 self->end = sym->end;
102 }
103}
104
d70a5402
ACM
105#define DSO__DELETED "(deleted)"
106
4aa65636
ACM
107static int map__load(struct map *self, struct perf_session *session,
108 symbol_filter_t filter)
66bd8424 109{
79406cd7 110 const char *name = self->dso->long_name;
4aa65636 111 int nr = dso__load(self->dso, self, session, filter);
79406cd7
ACM
112
113 if (nr < 0) {
114 if (self->dso->has_build_id) {
115 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
116
117 build_id__sprintf(self->dso->build_id,
118 sizeof(self->dso->build_id),
119 sbuild_id);
120 pr_warning("%s with build id %s not found",
121 name, sbuild_id);
122 } else
123 pr_warning("Failed to open %s", name);
124
125 pr_warning(", continuing without symbols\n");
126 return -1;
127 } else if (nr == 0) {
128 const size_t len = strlen(name);
129 const size_t real_len = len - sizeof(DSO__DELETED);
130
131 if (len > sizeof(DSO__DELETED) &&
132 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
133 pr_warning("%.*s was updated, restart the long "
134 "running apps that use it!\n",
135 (int)real_len, name);
136 } else {
137 pr_warning("no symbols found in %s, maybe install "
138 "a debug package?\n", name);
66bd8424 139 }
79406cd7
ACM
140
141 return -1;
66bd8424
ACM
142 }
143
79406cd7
ACM
144 return 0;
145}
146
4aa65636
ACM
147struct symbol *map__find_symbol(struct map *self, struct perf_session *session,
148 u64 addr, symbol_filter_t filter)
79406cd7 149{
4aa65636 150 if (!dso__loaded(self->dso, self->type) && map__load(self, session, filter) < 0)
79406cd7
ACM
151 return NULL;
152
ea08d8cb 153 return dso__find_symbol(self->dso, self->type, addr);
66bd8424
ACM
154}
155
79406cd7 156struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
4aa65636 157 struct perf_session *session,
79406cd7
ACM
158 symbol_filter_t filter)
159{
4aa65636 160 if (!dso__loaded(self->dso, self->type) && map__load(self, session, filter) < 0)
79406cd7
ACM
161 return NULL;
162
163 if (!dso__sorted_by_name(self->dso, self->type))
164 dso__sort_by_name(self->dso, self->type);
165
166 return dso__find_symbol_by_name(self->dso, self->type, name);
167}
168
66e274f3
FW
169struct map *map__clone(struct map *self)
170{
171 struct map *map = malloc(sizeof(*self));
172
173 if (!map)
174 return NULL;
175
176 memcpy(map, self, sizeof(*self));
177
178 return map;
179}
180
181int map__overlap(struct map *l, struct map *r)
182{
183 if (l->start > r->start) {
184 struct map *t = l;
185 l = r;
186 r = t;
187 }
188
189 if (l->end > r->start)
190 return 1;
191
192 return 0;
193}
194
195size_t map__fprintf(struct map *self, FILE *fp)
196{
197 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
198 self->start, self->end, self->pgoff, self->dso->name);
199}