1 // SPDX-License-Identifier: GPL-2.0
7 #include "namespaces.h"
12 #include <symbol.h> // filename__read_build_id
15 static int __dso_id__cmp(struct dso_id *a, struct dso_id *b)
17 if (a->maj > b->maj) return -1;
18 if (a->maj < b->maj) return 1;
20 if (a->min > b->min) return -1;
21 if (a->min < b->min) return 1;
23 if (a->ino > b->ino) return -1;
24 if (a->ino < b->ino) return 1;
27 * Synthesized MMAP events have zero ino_generation, avoid comparing
28 * them with MMAP events with actual ino_generation.
30 * I found it harmful because the mismatch resulted in a new
31 * dso that did not have a build ID whereas the original dso did have a
32 * build ID. The build ID was essential because the object was not found
35 if (a->ino_generation && b->ino_generation) {
36 if (a->ino_generation > b->ino_generation) return -1;
37 if (a->ino_generation < b->ino_generation) return 1;
43 static bool dso_id__empty(struct dso_id *id)
48 return !id->maj && !id->min && !id->ino && !id->ino_generation;
51 static void dso__inject_id(struct dso *dso, struct dso_id *id)
53 dso->id.maj = id->maj;
54 dso->id.min = id->min;
55 dso->id.ino = id->ino;
56 dso->id.ino_generation = id->ino_generation;
59 static int dso_id__cmp(struct dso_id *a, struct dso_id *b)
62 * The second is always dso->id, so zeroes if not set, assume passing
63 * NULL for a means a zeroed id
65 if (dso_id__empty(a) || dso_id__empty(b))
68 return __dso_id__cmp(a, b);
71 int dso__cmp_id(struct dso *a, struct dso *b)
73 return __dso_id__cmp(&a->id, &b->id);
76 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
78 bool have_build_id = false;
82 list_for_each_entry(pos, head, node) {
83 if (with_hits && !pos->hit && !dso__is_vdso(pos))
85 if (pos->has_build_id) {
89 nsinfo__mountns_enter(pos->nsinfo, &nsc);
90 if (filename__read_build_id(pos->long_name, &pos->bid) > 0) {
92 pos->has_build_id = true;
93 } else if (errno == ENOENT && pos->nsinfo) {
94 char *new_name = dso__filename_with_chroot(pos, pos->long_name);
96 if (new_name && filename__read_build_id(new_name,
99 pos->has_build_id = true;
103 nsinfo__mountns_exit(&nsc);
106 return have_build_id;
109 static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, struct dso *b)
111 int rc = strcmp(long_name, b->long_name);
112 return rc ?: dso_id__cmp(id, &b->id);
115 static int __dso__cmp_short_name(const char *short_name, struct dso_id *id, struct dso *b)
117 int rc = strcmp(short_name, b->short_name);
118 return rc ?: dso_id__cmp(id, &b->id);
121 static int dso__cmp_short_name(struct dso *a, struct dso *b)
123 return __dso__cmp_short_name(a->short_name, &a->id, b);
127 * Find a matching entry and/or link current entry to RB tree.
128 * Either one of the dso or name parameter must be non-NULL or the
129 * function will not work.
131 struct dso *__dsos__findnew_link_by_longname_id(struct rb_root *root, struct dso *dso,
132 const char *name, struct dso_id *id)
134 struct rb_node **p = &root->rb_node;
135 struct rb_node *parent = NULL;
138 name = dso->long_name;
140 * Find node with the matching name
143 struct dso *this = rb_entry(*p, struct dso, rb_node);
144 int rc = __dso__cmp_long_name(name, id, this);
149 * In case the new DSO is a duplicate of an existing
150 * one, print a one-time warning & put the new entry
151 * at the end of the list of duplicates.
153 if (!dso || (dso == this))
154 return this; /* Find matching dso */
156 * The core kernel DSOs may have duplicated long name.
157 * In this case, the short name should be different.
158 * Comparing the short names to differentiate the DSOs.
160 rc = dso__cmp_short_name(dso, this);
162 pr_err("Duplicated dso name: %s\n", name);
167 p = &parent->rb_left;
169 p = &parent->rb_right;
172 /* Add new node and rebalance tree */
173 rb_link_node(&dso->rb_node, parent, p);
174 rb_insert_color(&dso->rb_node, root);
180 void __dsos__add(struct dsos *dsos, struct dso *dso)
182 list_add_tail(&dso->node, &dsos->head);
183 __dsos__findnew_link_by_longname_id(&dsos->root, dso, NULL, &dso->id);
185 * It is now in the linked list, grab a reference, then garbage collect
186 * this when needing memory, by looking at LRU dso instances in the
187 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
188 * anywhere besides the one for the list, do, under a lock for the
189 * list: remove it from the list, then a dso__put(), that probably will
190 * be the last and will then call dso__delete(), end of life.
192 * That, or at the end of the 'struct machine' lifetime, when all
193 * 'struct dso' instances will be removed from the list, in
194 * dsos__exit(), if they have no other reference from some other data
197 * E.g.: after processing a 'perf.data' file and storing references
198 * to objects instantiated while processing events, we will have
199 * references to the 'thread', 'map', 'dso' structs all from 'struct
200 * hist_entry' instances, but we may not need anything not referenced,
201 * so we might as well call machines__exit()/machines__delete() and
202 * garbage collect it.
207 void dsos__add(struct dsos *dsos, struct dso *dso)
209 down_write(&dsos->lock);
210 __dsos__add(dsos, dso);
211 up_write(&dsos->lock);
214 static struct dso *__dsos__findnew_by_longname_id(struct rb_root *root, const char *name, struct dso_id *id)
216 return __dsos__findnew_link_by_longname_id(root, NULL, name, id);
219 static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, struct dso_id *id, bool cmp_short)
224 list_for_each_entry(pos, &dsos->head, node)
225 if (__dso__cmp_short_name(name, id, pos) == 0)
229 return __dsos__findnew_by_longname_id(&dsos->root, name, id);
232 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
234 return __dsos__find_id(dsos, name, NULL, cmp_short);
237 static void dso__set_basename(struct dso *dso)
242 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
243 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
247 * basename() may modify path buffer, so we must pass
250 lname = strdup(dso->long_name);
255 * basename() may return a pointer to internal
256 * storage which is reused in subsequent calls
257 * so copy the result.
259 base = strdup(basename(lname));
266 dso__set_short_name(dso, base, true);
269 static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
271 struct dso *dso = dso__new_id(name, id);
274 __dsos__add(dsos, dso);
275 dso__set_basename(dso);
276 /* Put dso here because __dsos_add already got it */
282 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
284 return __dsos__addnew_id(dsos, name, NULL);
287 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
289 struct dso *dso = __dsos__find_id(dsos, name, id, false);
291 if (dso && dso_id__empty(&dso->id) && !dso_id__empty(id))
292 dso__inject_id(dso, id);
294 return dso ? dso : __dsos__addnew_id(dsos, name, id);
297 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
300 down_write(&dsos->lock);
301 dso = dso__get(__dsos__findnew_id(dsos, name, id));
302 up_write(&dsos->lock);
306 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
307 bool (skip)(struct dso *dso, int parm), int parm)
312 list_for_each_entry(pos, head, node) {
313 char sbuild_id[SBUILD_ID_SIZE];
315 if (skip && skip(pos, parm))
317 build_id__sprintf(&pos->bid, sbuild_id);
318 ret += fprintf(fp, "%-40s %s\n", sbuild_id, pos->long_name);
323 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
328 list_for_each_entry(pos, head, node) {
329 ret += dso__fprintf(pos, fp);