]> Git Repo - linux.git/blob - tools/perf/util/dsos.c
1d38d6ac6e5a71a5e1643b087b13162c9bebb690
[linux.git] / tools / perf / util / dsos.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "debug.h"
3 #include "dsos.h"
4 #include "dso.h"
5 #include "map.h"
6 #include "vdso.h"
7 #include "namespaces.h"
8 #include <libgen.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <symbol.h> // filename__read_build_id
12
13 int dso_id__cmp(struct dso_id *a, struct dso_id *b)
14 {
15         /*
16          * The second is always dso->id, so zeroes if not set, assume passing
17          * NULL for a means a zeroed id
18          */
19         if (a == NULL)
20                 return 0;
21
22         if (a->maj > b->maj) return -1;
23         if (a->maj < b->maj) return 1;
24
25         if (a->min > b->min) return -1;
26         if (a->min < b->min) return 1;
27
28         if (a->ino > b->ino) return -1;
29         if (a->ino < b->ino) return 1;
30
31         if (a->ino_generation > b->ino_generation) return -1;
32         if (a->ino_generation < b->ino_generation) return 1;
33
34         return 0;
35 }
36
37 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
38 {
39         bool have_build_id = false;
40         struct dso *pos;
41         struct nscookie nsc;
42
43         list_for_each_entry(pos, head, node) {
44                 if (with_hits && !pos->hit && !dso__is_vdso(pos))
45                         continue;
46                 if (pos->has_build_id) {
47                         have_build_id = true;
48                         continue;
49                 }
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) {
53                         have_build_id     = true;
54                         pos->has_build_id = true;
55                 }
56                 nsinfo__mountns_exit(&nsc);
57         }
58
59         return have_build_id;
60 }
61
62 /*
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.
66  */
67 struct dso *__dsos__findnew_link_by_longname(struct rb_root *root, struct dso *dso, const char *name)
68 {
69         struct rb_node **p = &root->rb_node;
70         struct rb_node  *parent = NULL;
71
72         if (!name)
73                 name = dso->long_name;
74         /*
75          * Find node with the matching name
76          */
77         while (*p) {
78                 struct dso *this = rb_entry(*p, struct dso, rb_node);
79                 int rc = strcmp(name, this->long_name);
80
81                 parent = *p;
82                 if (rc == 0) {
83                         /*
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.
87                          */
88                         if (!dso || (dso == this))
89                                 return this;    /* Find matching dso */
90                         /*
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.
94                          */
95                         rc = strcmp(dso->short_name, this->short_name);
96                         if (rc == 0) {
97                                 pr_err("Duplicated dso name: %s\n", name);
98                                 return NULL;
99                         }
100                 }
101                 if (rc < 0)
102                         p = &parent->rb_left;
103                 else
104                         p = &parent->rb_right;
105         }
106         if (dso) {
107                 /* Add new node and rebalance tree */
108                 rb_link_node(&dso->rb_node, parent, p);
109                 rb_insert_color(&dso->rb_node, root);
110                 dso->root = root;
111         }
112         return NULL;
113 }
114
115 void __dsos__add(struct dsos *dsos, struct dso *dso)
116 {
117         list_add_tail(&dso->node, &dsos->head);
118         __dsos__findnew_link_by_longname(&dsos->root, dso, NULL);
119         /*
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.
126          *
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
130          * structure.
131          *
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.
138          */
139         dso__get(dso);
140 }
141
142 void dsos__add(struct dsos *dsos, struct dso *dso)
143 {
144         down_write(&dsos->lock);
145         __dsos__add(dsos, dso);
146         up_write(&dsos->lock);
147 }
148
149 struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
150 {
151         struct dso *pos;
152
153         if (cmp_short) {
154                 list_for_each_entry(pos, &dsos->head, node)
155                         if (strcmp(pos->short_name, name) == 0)
156                                 return pos;
157                 return NULL;
158         }
159         return __dsos__findnew_by_longname(&dsos->root, name);
160 }
161
162 static void dso__set_basename(struct dso *dso)
163 {
164         char *base, *lname;
165         int tid;
166
167         if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
168                 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
169                         return;
170         } else {
171               /*
172                * basename() may modify path buffer, so we must pass
173                * a copy.
174                */
175                 lname = strdup(dso->long_name);
176                 if (!lname)
177                         return;
178
179                 /*
180                  * basename() may return a pointer to internal
181                  * storage which is reused in subsequent calls
182                  * so copy the result.
183                  */
184                 base = strdup(basename(lname));
185
186                 free(lname);
187
188                 if (!base)
189                         return;
190         }
191         dso__set_short_name(dso, base, true);
192 }
193
194 struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
195 {
196         struct dso *dso = dso__new(name);
197
198         if (dso != NULL) {
199                 __dsos__add(dsos, dso);
200                 dso__set_basename(dso);
201                 /* Put dso here because __dsos_add already got it */
202                 dso__put(dso);
203         }
204         return dso;
205 }
206
207 struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
208 {
209         struct dso *dso = __dsos__find(dsos, name, false);
210
211         return dso ? dso : __dsos__addnew(dsos, name);
212 }
213
214 struct dso *dsos__findnew(struct dsos *dsos, const char *name)
215 {
216         struct dso *dso;
217         down_write(&dsos->lock);
218         dso = dso__get(__dsos__findnew(dsos, name));
219         up_write(&dsos->lock);
220         return dso;
221 }
222
223 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
224                                bool (skip)(struct dso *dso, int parm), int parm)
225 {
226         struct dso *pos;
227         size_t ret = 0;
228
229         list_for_each_entry(pos, head, node) {
230                 if (skip && skip(pos, parm))
231                         continue;
232                 ret += dso__fprintf_buildid(pos, fp);
233                 ret += fprintf(fp, " %s\n", pos->long_name);
234         }
235         return ret;
236 }
237
238 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
239 {
240         struct dso *pos;
241         size_t ret = 0;
242
243         list_for_each_entry(pos, head, node) {
244                 ret += dso__fprintf(pos, fp);
245         }
246
247         return ret;
248 }
This page took 0.030359 seconds and 2 git commands to generate.