1 // SPDX-License-Identifier: GPL-2.0
7 #include "namespaces.h"
11 #include <symbol.h> // filename__read_build_id
13 int dso_id__cmp(struct dso_id *a, struct dso_id *b)
16 * The second is always dso->id, so zeroes if not set, assume passing
17 * NULL for a means a zeroed id
22 if (a->maj > b->maj) return -1;
23 if (a->maj < b->maj) return 1;
25 if (a->min > b->min) return -1;
26 if (a->min < b->min) return 1;
28 if (a->ino > b->ino) return -1;
29 if (a->ino < b->ino) return 1;
31 if (a->ino_generation > b->ino_generation) return -1;
32 if (a->ino_generation < b->ino_generation) return 1;
37 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
39 bool have_build_id = false;
43 list_for_each_entry(pos, head, node) {
44 if (with_hits && !pos->hit && !dso__is_vdso(pos))
46 if (pos->has_build_id) {
50 nsinfo__mountns_enter(pos->nsinfo, &nsc);
51 if (filename__read_build_id(pos->long_name, pos->build_id,
52 sizeof(pos->build_id)) > 0) {
54 pos->has_build_id = true;
56 nsinfo__mountns_exit(&nsc);
63 * Find a matching entry and/or link current entry to RB tree.
64 * Either one of the dso or name parameter must be non-NULL or the
65 * function will not work.
67 struct dso *__dsos__findnew_link_by_longname(struct rb_root *root, struct dso *dso, const char *name)
69 struct rb_node **p = &root->rb_node;
70 struct rb_node *parent = NULL;
73 name = dso->long_name;
75 * Find node with the matching name
78 struct dso *this = rb_entry(*p, struct dso, rb_node);
79 int rc = strcmp(name, this->long_name);
84 * In case the new DSO is a duplicate of an existing
85 * one, print a one-time warning & put the new entry
86 * at the end of the list of duplicates.
88 if (!dso || (dso == this))
89 return this; /* Find matching dso */
91 * The core kernel DSOs may have duplicated long name.
92 * In this case, the short name should be different.
93 * Comparing the short names to differentiate the DSOs.
95 rc = strcmp(dso->short_name, this->short_name);
97 pr_err("Duplicated dso name: %s\n", name);
102 p = &parent->rb_left;
104 p = &parent->rb_right;
107 /* Add new node and rebalance tree */
108 rb_link_node(&dso->rb_node, parent, p);
109 rb_insert_color(&dso->rb_node, root);
115 void __dsos__add(struct dsos *dsos, struct dso *dso)
117 list_add_tail(&dso->node, &dsos->head);
118 __dsos__findnew_link_by_longname(&dsos->root, dso, NULL);
120 * It is now in the linked list, grab a reference, then garbage collect
121 * this when needing memory, by looking at LRU dso instances in the
122 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
123 * anywhere besides the one for the list, do, under a lock for the
124 * list: remove it from the list, then a dso__put(), that probably will
125 * be the last and will then call dso__delete(), end of life.
127 * That, or at the end of the 'struct machine' lifetime, when all
128 * 'struct dso' instances will be removed from the list, in
129 * dsos__exit(), if they have no other reference from some other data
132 * E.g.: after processing a 'perf.data' file and storing references
133 * to objects instantiated while processing events, we will have
134 * references to the 'thread', 'map', 'dso' structs all from 'struct
135 * hist_entry' instances, but we may not need anything not referenced,
136 * so we might as well call machines__exit()/machines__delete() and
137 * garbage collect it.
142 void dsos__add(struct dsos *dsos, struct dso *dso)
144 down_write(&dsos->lock);
145 __dsos__add(dsos, dso);
146 up_write(&dsos->lock);
149 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
154 list_for_each_entry(pos, &dsos->head, node)
155 if (strcmp(pos->short_name, name) == 0)
159 return __dsos__findnew_by_longname(&dsos->root, name);
162 static void dso__set_basename(struct dso *dso)
167 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
168 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
172 * basename() may modify path buffer, so we must pass
175 lname = strdup(dso->long_name);
180 * basename() may return a pointer to internal
181 * storage which is reused in subsequent calls
182 * so copy the result.
184 base = strdup(basename(lname));
191 dso__set_short_name(dso, base, true);
194 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
196 struct dso *dso = dso__new(name);
199 __dsos__add(dsos, dso);
200 dso__set_basename(dso);
201 /* Put dso here because __dsos_add already got it */
207 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
209 struct dso *dso = __dsos__find(dsos, name, false);
211 return dso ? dso : __dsos__addnew(dsos, name);
214 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
217 down_write(&dsos->lock);
218 dso = dso__get(__dsos__findnew(dsos, name));
219 up_write(&dsos->lock);
223 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
224 bool (skip)(struct dso *dso, int parm), int parm)
229 list_for_each_entry(pos, head, node) {
230 if (skip && skip(pos, parm))
232 ret += dso__fprintf_buildid(pos, fp);
233 ret += fprintf(fp, " %s\n", pos->long_name);
238 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
243 list_for_each_entry(pos, head, node) {
244 ret += dso__fprintf(pos, fp);