1 // SPDX-License-Identifier: GPL-2.0
6 #include "namespaces.h"
10 #include <symbol.h> // filename__read_build_id
12 static int __dso_id__cmp(struct dso_id
*a
, struct dso_id
*b
)
14 if (a
->maj
> b
->maj
) return -1;
15 if (a
->maj
< b
->maj
) return 1;
17 if (a
->min
> b
->min
) return -1;
18 if (a
->min
< b
->min
) return 1;
20 if (a
->ino
> b
->ino
) return -1;
21 if (a
->ino
< b
->ino
) return 1;
23 if (a
->ino_generation
> b
->ino_generation
) return -1;
24 if (a
->ino_generation
< b
->ino_generation
) return 1;
29 static int dso_id__cmp(struct dso_id
*a
, struct dso_id
*b
)
32 * The second is always dso->id, so zeroes if not set, assume passing
33 * NULL for a means a zeroed id
38 return __dso_id__cmp(a
, b
);
41 int dso__cmp_id(struct dso
*a
, struct dso
*b
)
43 return __dso_id__cmp(&a
->id
, &b
->id
);
46 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
48 bool have_build_id
= false;
52 list_for_each_entry(pos
, head
, node
) {
53 if (with_hits
&& !pos
->hit
&& !dso__is_vdso(pos
))
55 if (pos
->has_build_id
) {
59 nsinfo__mountns_enter(pos
->nsinfo
, &nsc
);
60 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
61 sizeof(pos
->build_id
)) > 0) {
63 pos
->has_build_id
= true;
65 nsinfo__mountns_exit(&nsc
);
71 static int __dso__cmp_long_name(const char *long_name
, struct dso_id
*id
, struct dso
*b
)
73 int rc
= strcmp(long_name
, b
->long_name
);
74 return rc
?: dso_id__cmp(id
, &b
->id
);
77 static int __dso__cmp_short_name(const char *short_name
, struct dso_id
*id
, struct dso
*b
)
79 int rc
= strcmp(short_name
, b
->short_name
);
80 return rc
?: dso_id__cmp(id
, &b
->id
);
83 static int dso__cmp_short_name(struct dso
*a
, struct dso
*b
)
85 return __dso__cmp_short_name(a
->short_name
, &a
->id
, b
);
89 * Find a matching entry and/or link current entry to RB tree.
90 * Either one of the dso or name parameter must be non-NULL or the
91 * function will not work.
93 struct dso
*__dsos__findnew_link_by_longname_id(struct rb_root
*root
, struct dso
*dso
,
94 const char *name
, struct dso_id
*id
)
96 struct rb_node
**p
= &root
->rb_node
;
97 struct rb_node
*parent
= NULL
;
100 name
= dso
->long_name
;
102 * Find node with the matching name
105 struct dso
*this = rb_entry(*p
, struct dso
, rb_node
);
106 int rc
= __dso__cmp_long_name(name
, id
, this);
111 * In case the new DSO is a duplicate of an existing
112 * one, print a one-time warning & put the new entry
113 * at the end of the list of duplicates.
115 if (!dso
|| (dso
== this))
116 return this; /* Find matching dso */
118 * The core kernel DSOs may have duplicated long name.
119 * In this case, the short name should be different.
120 * Comparing the short names to differentiate the DSOs.
122 rc
= dso__cmp_short_name(dso
, this);
124 pr_err("Duplicated dso name: %s\n", name
);
129 p
= &parent
->rb_left
;
131 p
= &parent
->rb_right
;
134 /* Add new node and rebalance tree */
135 rb_link_node(&dso
->rb_node
, parent
, p
);
136 rb_insert_color(&dso
->rb_node
, root
);
142 void __dsos__add(struct dsos
*dsos
, struct dso
*dso
)
144 list_add_tail(&dso
->node
, &dsos
->head
);
145 __dsos__findnew_link_by_longname_id(&dsos
->root
, dso
, NULL
, &dso
->id
);
147 * It is now in the linked list, grab a reference, then garbage collect
148 * this when needing memory, by looking at LRU dso instances in the
149 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
150 * anywhere besides the one for the list, do, under a lock for the
151 * list: remove it from the list, then a dso__put(), that probably will
152 * be the last and will then call dso__delete(), end of life.
154 * That, or at the end of the 'struct machine' lifetime, when all
155 * 'struct dso' instances will be removed from the list, in
156 * dsos__exit(), if they have no other reference from some other data
159 * E.g.: after processing a 'perf.data' file and storing references
160 * to objects instantiated while processing events, we will have
161 * references to the 'thread', 'map', 'dso' structs all from 'struct
162 * hist_entry' instances, but we may not need anything not referenced,
163 * so we might as well call machines__exit()/machines__delete() and
164 * garbage collect it.
169 void dsos__add(struct dsos
*dsos
, struct dso
*dso
)
171 down_write(&dsos
->lock
);
172 __dsos__add(dsos
, dso
);
173 up_write(&dsos
->lock
);
176 static struct dso
*__dsos__findnew_by_longname_id(struct rb_root
*root
, const char *name
, struct dso_id
*id
)
178 return __dsos__findnew_link_by_longname_id(root
, NULL
, name
, id
);
181 static struct dso
*__dsos__find_id(struct dsos
*dsos
, const char *name
, struct dso_id
*id
, bool cmp_short
)
186 list_for_each_entry(pos
, &dsos
->head
, node
)
187 if (__dso__cmp_short_name(name
, id
, pos
) == 0)
191 return __dsos__findnew_by_longname_id(&dsos
->root
, name
, id
);
194 struct dso
*__dsos__find(struct dsos
*dsos
, const char *name
, bool cmp_short
)
196 return __dsos__find_id(dsos
, name
, NULL
, cmp_short
);
199 static void dso__set_basename(struct dso
*dso
)
204 if (sscanf(dso
->long_name
, "/tmp/perf-%d.map", &tid
) == 1) {
205 if (asprintf(&base
, "[JIT] tid %d", tid
) < 0)
209 * basename() may modify path buffer, so we must pass
212 lname
= strdup(dso
->long_name
);
217 * basename() may return a pointer to internal
218 * storage which is reused in subsequent calls
219 * so copy the result.
221 base
= strdup(basename(lname
));
228 dso__set_short_name(dso
, base
, true);
231 static struct dso
*__dsos__addnew_id(struct dsos
*dsos
, const char *name
, struct dso_id
*id
)
233 struct dso
*dso
= dso__new_id(name
, id
);
236 __dsos__add(dsos
, dso
);
237 dso__set_basename(dso
);
238 /* Put dso here because __dsos_add already got it */
244 struct dso
*__dsos__addnew(struct dsos
*dsos
, const char *name
)
246 return __dsos__addnew_id(dsos
, name
, NULL
);
249 static struct dso
*__dsos__findnew_id(struct dsos
*dsos
, const char *name
, struct dso_id
*id
)
251 struct dso
*dso
= __dsos__find_id(dsos
, name
, id
, false);
252 return dso
? dso
: __dsos__addnew_id(dsos
, name
, id
);
255 struct dso
*dsos__findnew_id(struct dsos
*dsos
, const char *name
, struct dso_id
*id
)
258 down_write(&dsos
->lock
);
259 dso
= dso__get(__dsos__findnew_id(dsos
, name
, id
));
260 up_write(&dsos
->lock
);
264 size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
265 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
270 list_for_each_entry(pos
, head
, node
) {
271 if (skip
&& skip(pos
, parm
))
273 ret
+= dso__fprintf_buildid(pos
, fp
);
274 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
279 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
284 list_for_each_entry(pos
, head
, node
) {
285 ret
+= dso__fprintf(pos
, fp
);