10 void map_groups__init(struct map_groups *self)
13 for (i = 0; i < MAP__NR_TYPES; ++i) {
14 self->maps[i] = RB_ROOT;
15 INIT_LIST_HEAD(&self->removed_maps[i]);
19 static struct thread *thread__new(pid_t pid)
21 struct thread *self = zalloc(sizeof(*self));
24 map_groups__init(&self->mg);
26 self->comm = malloc(32);
28 snprintf(self->comm, 32, ":%d", self->pid);
34 int thread__set_comm(struct thread *self, const char *comm)
38 self->comm = strdup(comm);
39 if (self->comm == NULL)
41 self->comm_set = true;
45 int thread__comm_len(struct thread *self)
47 if (!self->comm_len) {
50 self->comm_len = strlen(self->comm);
53 return self->comm_len;
56 static const char *map_type__name[MAP__NR_TYPES] = {
57 [MAP__FUNCTION] = "Functions",
58 [MAP__VARIABLE] = "Variables",
61 static size_t __map_groups__fprintf_maps(struct map_groups *self,
62 enum map_type type, FILE *fp)
64 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
67 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
68 struct map *pos = rb_entry(nd, struct map, rb_node);
69 printed += fprintf(fp, "Map:");
70 printed += map__fprintf(pos, fp);
72 printed += dso__fprintf(pos->dso, type, fp);
73 printed += fprintf(fp, "--\n");
80 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
82 size_t printed = 0, i;
83 for (i = 0; i < MAP__NR_TYPES; ++i)
84 printed += __map_groups__fprintf_maps(self, i, fp);
88 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
89 enum map_type type, FILE *fp)
94 list_for_each_entry(pos, &self->removed_maps[type], node) {
95 printed += fprintf(fp, "Map:");
96 printed += map__fprintf(pos, fp);
98 printed += dso__fprintf(pos->dso, type, fp);
99 printed += fprintf(fp, "--\n");
105 static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
107 size_t printed = 0, i;
108 for (i = 0; i < MAP__NR_TYPES; ++i)
109 printed += __map_groups__fprintf_removed_maps(self, i, fp);
113 static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
115 size_t printed = map_groups__fprintf_maps(self, fp);
116 printed += fprintf(fp, "Removed maps:\n");
117 return printed + map_groups__fprintf_removed_maps(self, fp);
120 static size_t thread__fprintf(struct thread *self, FILE *fp)
122 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
123 map_groups__fprintf(&self->mg, fp);
126 struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
128 struct rb_node **p = &self->threads.rb_node;
129 struct rb_node *parent = NULL;
133 * Font-end cache - PID lookups come in blocks,
134 * so most of the time we dont have to look up
137 if (self->last_match && self->last_match->pid == pid)
138 return self->last_match;
142 th = rb_entry(parent, struct thread, rb_node);
144 if (th->pid == pid) {
145 self->last_match = th;
155 th = thread__new(pid);
157 rb_link_node(&th->rb_node, parent, p);
158 rb_insert_color(&th->rb_node, &self->threads);
159 self->last_match = th;
165 static void map_groups__remove_overlappings(struct map_groups *self,
168 struct rb_root *root = &self->maps[map->type];
169 struct rb_node *next = rb_first(root);
172 struct map *pos = rb_entry(next, struct map, rb_node);
173 next = rb_next(&pos->rb_node);
175 if (!map__overlap(pos, map))
179 fputs("overlapping maps:\n", stderr);
180 map__fprintf(map, stderr);
181 map__fprintf(pos, stderr);
184 rb_erase(&pos->rb_node, root);
186 * We may have references to this map, for instance in some
187 * hist_entry instances, so just move them to a separate
190 list_add_tail(&pos->node, &self->removed_maps[map->type]);
194 void maps__insert(struct rb_root *maps, struct map *map)
196 struct rb_node **p = &maps->rb_node;
197 struct rb_node *parent = NULL;
198 const u64 ip = map->start;
203 m = rb_entry(parent, struct map, rb_node);
210 rb_link_node(&map->rb_node, parent, p);
211 rb_insert_color(&map->rb_node, maps);
214 struct map *maps__find(struct rb_root *maps, u64 ip)
216 struct rb_node **p = &maps->rb_node;
217 struct rb_node *parent = NULL;
222 m = rb_entry(parent, struct map, rb_node);
225 else if (ip > m->end)
234 void thread__insert_map(struct thread *self, struct map *map)
236 map_groups__remove_overlappings(&self->mg, map);
237 map_groups__insert(&self->mg, map);
241 * XXX This should not really _copy_ te maps, but refcount them.
243 static int map_groups__clone(struct map_groups *self,
244 struct map_groups *parent, enum map_type type)
247 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
248 struct map *map = rb_entry(nd, struct map, rb_node);
249 struct map *new = map__clone(map);
252 map_groups__insert(self, new);
257 int thread__fork(struct thread *self, struct thread *parent)
261 if (parent->comm_set) {
264 self->comm = strdup(parent->comm);
267 self->comm_set = true;
270 for (i = 0; i < MAP__NR_TYPES; ++i)
271 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
276 size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
281 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
282 struct thread *pos = rb_entry(nd, struct thread, rb_node);
284 ret += thread__fprintf(pos, fp);
290 struct symbol *map_groups__find_symbol(struct map_groups *self,
291 enum map_type type, u64 addr,
292 symbol_filter_t filter)
294 struct map *map = map_groups__find(self, type, addr);
297 return map__find_symbol(map, map->map_ip(map, addr), filter);