9 static struct rb_root threads;
10 static struct thread *last_match;
12 void map_groups__init(struct map_groups *self)
15 for (i = 0; i < MAP__NR_TYPES; ++i) {
16 self->maps[i] = RB_ROOT;
17 INIT_LIST_HEAD(&self->removed_maps[i]);
21 static struct thread *thread__new(pid_t pid)
23 struct thread *self = zalloc(sizeof(*self));
26 map_groups__init(&self->mg);
28 self->comm = malloc(32);
30 snprintf(self->comm, 32, ":%d", self->pid);
36 int thread__set_comm(struct thread *self, const char *comm)
40 self->comm = strdup(comm);
41 return self->comm ? 0 : -ENOMEM;
44 int thread__comm_len(struct thread *self)
46 if (!self->comm_len) {
49 self->comm_len = strlen(self->comm);
52 return self->comm_len;
55 static const char *map_type__name[MAP__NR_TYPES] = {
56 [MAP__FUNCTION] = "Functions",
57 [MAP__VARIABLE] = "Variables",
60 static size_t __map_groups__fprintf_maps(struct map_groups *self,
61 enum map_type type, FILE *fp)
63 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
66 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
67 struct map *pos = rb_entry(nd, struct map, rb_node);
68 printed += fprintf(fp, "Map:");
69 printed += map__fprintf(pos, fp);
71 printed += dso__fprintf(pos->dso, type, fp);
72 printed += fprintf(fp, "--\n");
79 size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
81 size_t printed = 0, i;
82 for (i = 0; i < MAP__NR_TYPES; ++i)
83 printed += __map_groups__fprintf_maps(self, i, fp);
87 static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
88 enum map_type type, FILE *fp)
93 list_for_each_entry(pos, &self->removed_maps[type], node) {
94 printed += fprintf(fp, "Map:");
95 printed += map__fprintf(pos, fp);
97 printed += dso__fprintf(pos->dso, type, fp);
98 printed += fprintf(fp, "--\n");
104 static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
106 size_t printed = 0, i;
107 for (i = 0; i < MAP__NR_TYPES; ++i)
108 printed += __map_groups__fprintf_removed_maps(self, i, fp);
112 static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
114 size_t printed = map_groups__fprintf_maps(self, fp);
115 printed += fprintf(fp, "Removed maps:\n");
116 return printed + map_groups__fprintf_removed_maps(self, fp);
119 static size_t thread__fprintf(struct thread *self, FILE *fp)
121 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
122 map_groups__fprintf(&self->mg, fp);
125 struct thread *threads__findnew(pid_t pid)
127 struct rb_node **p = &threads.rb_node;
128 struct rb_node *parent = NULL;
132 * Font-end cache - PID lookups come in blocks,
133 * so most of the time we dont have to look up
136 if (last_match && last_match->pid == pid)
141 th = rb_entry(parent, struct thread, rb_node);
143 if (th->pid == pid) {
154 th = thread__new(pid);
156 rb_link_node(&th->rb_node, parent, p);
157 rb_insert_color(&th->rb_node, &threads);
164 static void map_groups__remove_overlappings(struct map_groups *self,
167 struct rb_root *root = &self->maps[map->type];
168 struct rb_node *next = rb_first(root);
171 struct map *pos = rb_entry(next, struct map, rb_node);
172 next = rb_next(&pos->rb_node);
174 if (!map__overlap(pos, map))
178 fputs("overlapping maps:\n", stderr);
179 map__fprintf(map, stderr);
180 map__fprintf(pos, stderr);
183 rb_erase(&pos->rb_node, root);
185 * We may have references to this map, for instance in some
186 * hist_entry instances, so just move them to a separate
189 list_add_tail(&pos->node, &self->removed_maps[map->type]);
193 void maps__insert(struct rb_root *maps, struct map *map)
195 struct rb_node **p = &maps->rb_node;
196 struct rb_node *parent = NULL;
197 const u64 ip = map->start;
202 m = rb_entry(parent, struct map, rb_node);
209 rb_link_node(&map->rb_node, parent, p);
210 rb_insert_color(&map->rb_node, maps);
213 struct map *maps__find(struct rb_root *maps, u64 ip)
215 struct rb_node **p = &maps->rb_node;
216 struct rb_node *parent = NULL;
221 m = rb_entry(parent, struct map, rb_node);
224 else if (ip > m->end)
233 void thread__insert_map(struct thread *self, struct map *map)
235 map_groups__remove_overlappings(&self->mg, map);
236 map_groups__insert(&self->mg, map);
240 * XXX This should not really _copy_ te maps, but refcount them.
242 static int map_groups__clone(struct map_groups *self,
243 struct map_groups *parent, enum map_type type)
246 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
247 struct map *map = rb_entry(nd, struct map, rb_node);
248 struct map *new = map__clone(map);
251 map_groups__insert(self, new);
256 int thread__fork(struct thread *self, struct thread *parent)
262 self->comm = strdup(parent->comm);
266 for (i = 0; i < MAP__NR_TYPES; ++i)
267 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
272 size_t threads__fprintf(FILE *fp)
277 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
278 struct thread *pos = rb_entry(nd, struct thread, rb_node);
280 ret += thread__fprintf(pos, fp);
286 struct symbol *map_groups__find_symbol(struct map_groups *self,
287 enum map_type type, u64 addr,
288 symbol_filter_t filter)
290 struct map *map = map_groups__find(self, type, addr);
293 return map__find_symbol(map, map->map_ip(map, addr), filter);