Commit | Line | Data |
---|---|---|
6baa0a5a FW |
1 | #include "../perf.h" |
2 | #include <stdlib.h> | |
3 | #include <stdio.h> | |
4 | #include <string.h> | |
b3165f41 | 5 | #include "session.h" |
6baa0a5a FW |
6 | #include "thread.h" |
7 | #include "util.h" | |
6e086437 | 8 | #include "debug.h" |
6baa0a5a | 9 | |
9958e1f0 | 10 | void map_groups__init(struct map_groups *self) |
95011c60 ACM |
11 | { |
12 | int i; | |
95011c60 ACM |
13 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
14 | self->maps[i] = RB_ROOT; | |
15 | INIT_LIST_HEAD(&self->removed_maps[i]); | |
16 | } | |
17 | } | |
18 | ||
97ea1a7f | 19 | static struct thread *thread__new(pid_t pid) |
6baa0a5a | 20 | { |
36479484 | 21 | struct thread *self = zalloc(sizeof(*self)); |
6baa0a5a FW |
22 | |
23 | if (self != NULL) { | |
9958e1f0 ACM |
24 | map_groups__init(&self->mg); |
25 | self->pid = pid; | |
97ea1a7f FW |
26 | self->comm = malloc(32); |
27 | if (self->comm) | |
28 | snprintf(self->comm, 32, ":%d", self->pid); | |
6baa0a5a FW |
29 | } |
30 | ||
31 | return self; | |
32 | } | |
33 | ||
34 | int thread__set_comm(struct thread *self, const char *comm) | |
35 | { | |
36 | if (self->comm) | |
37 | free(self->comm); | |
38 | self->comm = strdup(comm); | |
39 | return self->comm ? 0 : -ENOMEM; | |
40 | } | |
41 | ||
a4fb581b FW |
42 | int thread__comm_len(struct thread *self) |
43 | { | |
44 | if (!self->comm_len) { | |
45 | if (!self->comm) | |
46 | return 0; | |
47 | self->comm_len = strlen(self->comm); | |
48 | } | |
49 | ||
50 | return self->comm_len; | |
51 | } | |
52 | ||
95011c60 ACM |
53 | static const char *map_type__name[MAP__NR_TYPES] = { |
54 | [MAP__FUNCTION] = "Functions", | |
22ccec57 | 55 | [MAP__VARIABLE] = "Variables", |
95011c60 ACM |
56 | }; |
57 | ||
9958e1f0 ACM |
58 | static size_t __map_groups__fprintf_maps(struct map_groups *self, |
59 | enum map_type type, FILE *fp) | |
6baa0a5a | 60 | { |
95011c60 | 61 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); |
1b46cddf | 62 | struct rb_node *nd; |
6baa0a5a | 63 | |
95011c60 ACM |
64 | for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { |
65 | struct map *pos = rb_entry(nd, struct map, rb_node); | |
66 | printed += fprintf(fp, "Map:"); | |
67 | printed += map__fprintf(pos, fp); | |
68 | if (verbose > 1) { | |
69 | printed += dso__fprintf(pos->dso, type, fp); | |
70 | printed += fprintf(fp, "--\n"); | |
71 | } | |
1b46cddf | 72 | } |
6baa0a5a | 73 | |
95011c60 ACM |
74 | return printed; |
75 | } | |
76 | ||
9958e1f0 | 77 | size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp) |
95011c60 ACM |
78 | { |
79 | size_t printed = 0, i; | |
80 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
9958e1f0 | 81 | printed += __map_groups__fprintf_maps(self, i, fp); |
95011c60 ACM |
82 | return printed; |
83 | } | |
439d473b | 84 | |
9958e1f0 ACM |
85 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *self, |
86 | enum map_type type, FILE *fp) | |
95011c60 ACM |
87 | { |
88 | struct map *pos; | |
89 | size_t printed = 0; | |
90 | ||
91 | list_for_each_entry(pos, &self->removed_maps[type], node) { | |
92 | printed += fprintf(fp, "Map:"); | |
93 | printed += map__fprintf(pos, fp); | |
94 | if (verbose > 1) { | |
95 | printed += dso__fprintf(pos->dso, type, fp); | |
96 | printed += fprintf(fp, "--\n"); | |
97 | } | |
98 | } | |
99 | return printed; | |
100 | } | |
439d473b | 101 | |
9958e1f0 | 102 | static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp) |
95011c60 ACM |
103 | { |
104 | size_t printed = 0, i; | |
105 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
9958e1f0 | 106 | printed += __map_groups__fprintf_removed_maps(self, i, fp); |
95011c60 ACM |
107 | return printed; |
108 | } | |
109 | ||
9958e1f0 | 110 | static size_t map_groups__fprintf(struct map_groups *self, FILE *fp) |
95011c60 | 111 | { |
9958e1f0 | 112 | size_t printed = map_groups__fprintf_maps(self, fp); |
95011c60 | 113 | printed += fprintf(fp, "Removed maps:\n"); |
9958e1f0 ACM |
114 | return printed + map_groups__fprintf_removed_maps(self, fp); |
115 | } | |
116 | ||
117 | static size_t thread__fprintf(struct thread *self, FILE *fp) | |
118 | { | |
119 | return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) + | |
120 | map_groups__fprintf(&self->mg, fp); | |
6baa0a5a FW |
121 | } |
122 | ||
b3165f41 | 123 | struct thread *perf_session__findnew(struct perf_session *self, pid_t pid) |
6baa0a5a | 124 | { |
b3165f41 | 125 | struct rb_node **p = &self->threads.rb_node; |
6baa0a5a FW |
126 | struct rb_node *parent = NULL; |
127 | struct thread *th; | |
128 | ||
129 | /* | |
130 | * Font-end cache - PID lookups come in blocks, | |
131 | * so most of the time we dont have to look up | |
132 | * the full rbtree: | |
133 | */ | |
b3165f41 ACM |
134 | if (self->last_match && self->last_match->pid == pid) |
135 | return self->last_match; | |
6baa0a5a FW |
136 | |
137 | while (*p != NULL) { | |
138 | parent = *p; | |
139 | th = rb_entry(parent, struct thread, rb_node); | |
140 | ||
141 | if (th->pid == pid) { | |
b3165f41 | 142 | self->last_match = th; |
6baa0a5a FW |
143 | return th; |
144 | } | |
145 | ||
146 | if (pid < th->pid) | |
147 | p = &(*p)->rb_left; | |
148 | else | |
149 | p = &(*p)->rb_right; | |
150 | } | |
151 | ||
97ea1a7f | 152 | th = thread__new(pid); |
6baa0a5a FW |
153 | if (th != NULL) { |
154 | rb_link_node(&th->rb_node, parent, p); | |
b3165f41 ACM |
155 | rb_insert_color(&th->rb_node, &self->threads); |
156 | self->last_match = th; | |
6baa0a5a FW |
157 | } |
158 | ||
159 | return th; | |
160 | } | |
161 | ||
9958e1f0 ACM |
162 | static void map_groups__remove_overlappings(struct map_groups *self, |
163 | struct map *map) | |
6baa0a5a | 164 | { |
95011c60 ACM |
165 | struct rb_root *root = &self->maps[map->type]; |
166 | struct rb_node *next = rb_first(root); | |
1b46cddf ACM |
167 | |
168 | while (next) { | |
169 | struct map *pos = rb_entry(next, struct map, rb_node); | |
170 | next = rb_next(&pos->rb_node); | |
171 | ||
172 | if (!map__overlap(pos, map)) | |
173 | continue; | |
174 | ||
175 | if (verbose >= 2) { | |
6beba7ad ACM |
176 | fputs("overlapping maps:\n", stderr); |
177 | map__fprintf(map, stderr); | |
178 | map__fprintf(pos, stderr); | |
1b46cddf ACM |
179 | } |
180 | ||
95011c60 | 181 | rb_erase(&pos->rb_node, root); |
439d473b ACM |
182 | /* |
183 | * We may have references to this map, for instance in some | |
184 | * hist_entry instances, so just move them to a separate | |
185 | * list. | |
186 | */ | |
95011c60 | 187 | list_add_tail(&pos->node, &self->removed_maps[map->type]); |
6baa0a5a | 188 | } |
1b46cddf ACM |
189 | } |
190 | ||
191 | void maps__insert(struct rb_root *maps, struct map *map) | |
192 | { | |
193 | struct rb_node **p = &maps->rb_node; | |
194 | struct rb_node *parent = NULL; | |
195 | const u64 ip = map->start; | |
196 | struct map *m; | |
197 | ||
198 | while (*p != NULL) { | |
199 | parent = *p; | |
200 | m = rb_entry(parent, struct map, rb_node); | |
201 | if (ip < m->start) | |
202 | p = &(*p)->rb_left; | |
203 | else | |
204 | p = &(*p)->rb_right; | |
205 | } | |
206 | ||
207 | rb_link_node(&map->rb_node, parent, p); | |
208 | rb_insert_color(&map->rb_node, maps); | |
209 | } | |
210 | ||
211 | struct map *maps__find(struct rb_root *maps, u64 ip) | |
212 | { | |
213 | struct rb_node **p = &maps->rb_node; | |
214 | struct rb_node *parent = NULL; | |
215 | struct map *m; | |
216 | ||
217 | while (*p != NULL) { | |
218 | parent = *p; | |
219 | m = rb_entry(parent, struct map, rb_node); | |
220 | if (ip < m->start) | |
221 | p = &(*p)->rb_left; | |
222 | else if (ip > m->end) | |
223 | p = &(*p)->rb_right; | |
224 | else | |
225 | return m; | |
226 | } | |
227 | ||
228 | return NULL; | |
229 | } | |
6baa0a5a | 230 | |
1b46cddf ACM |
231 | void thread__insert_map(struct thread *self, struct map *map) |
232 | { | |
9958e1f0 ACM |
233 | map_groups__remove_overlappings(&self->mg, map); |
234 | map_groups__insert(&self->mg, map); | |
6baa0a5a FW |
235 | } |
236 | ||
9958e1f0 ACM |
237 | /* |
238 | * XXX This should not really _copy_ te maps, but refcount them. | |
239 | */ | |
240 | static int map_groups__clone(struct map_groups *self, | |
241 | struct map_groups *parent, enum map_type type) | |
6baa0a5a | 242 | { |
1b46cddf | 243 | struct rb_node *nd; |
95011c60 ACM |
244 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { |
245 | struct map *map = rb_entry(nd, struct map, rb_node); | |
246 | struct map *new = map__clone(map); | |
247 | if (new == NULL) | |
248 | return -ENOMEM; | |
9958e1f0 | 249 | map_groups__insert(self, new); |
95011c60 ACM |
250 | } |
251 | return 0; | |
252 | } | |
253 | ||
254 | int thread__fork(struct thread *self, struct thread *parent) | |
255 | { | |
256 | int i; | |
6baa0a5a FW |
257 | |
258 | if (self->comm) | |
259 | free(self->comm); | |
260 | self->comm = strdup(parent->comm); | |
261 | if (!self->comm) | |
262 | return -ENOMEM; | |
263 | ||
95011c60 | 264 | for (i = 0; i < MAP__NR_TYPES; ++i) |
9958e1f0 | 265 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
6baa0a5a | 266 | return -ENOMEM; |
6baa0a5a FW |
267 | return 0; |
268 | } | |
269 | ||
b3165f41 | 270 | size_t perf_session__fprintf(struct perf_session *self, FILE *fp) |
6baa0a5a FW |
271 | { |
272 | size_t ret = 0; | |
273 | struct rb_node *nd; | |
274 | ||
b3165f41 | 275 | for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) { |
6baa0a5a FW |
276 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
277 | ||
278 | ret += thread__fprintf(pos, fp); | |
279 | } | |
280 | ||
281 | return ret; | |
282 | } | |
1ed091c4 | 283 | |
9958e1f0 | 284 | struct symbol *map_groups__find_symbol(struct map_groups *self, |
4aa65636 | 285 | struct perf_session *session, |
9958e1f0 ACM |
286 | enum map_type type, u64 addr, |
287 | symbol_filter_t filter) | |
1ed091c4 | 288 | { |
9958e1f0 | 289 | struct map *map = map_groups__find(self, type, addr); |
1ed091c4 ACM |
290 | |
291 | if (map != NULL) | |
4aa65636 | 292 | return map__find_symbol(map, session, map->map_ip(map, addr), filter); |
1ed091c4 ACM |
293 | |
294 | return NULL; | |
295 | } |