Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2#include "debug.h"
  3#include "dsos.h"
  4#include "dso.h"
  5#include "util.h"
  6#include "vdso.h"
  7#include "namespaces.h"
  8#include <errno.h>
  9#include <libgen.h>
 10#include <stdlib.h>
 11#include <string.h>
 12#include <symbol.h> // filename__read_build_id
 13#include <unistd.h>
 14
 15static int __dso_id__cmp(struct dso_id *a, struct dso_id *b)
 16{
 17	if (a->maj > b->maj) return -1;
 18	if (a->maj < b->maj) return 1;
 19
 20	if (a->min > b->min) return -1;
 21	if (a->min < b->min) return 1;
 22
 23	if (a->ino > b->ino) return -1;
 24	if (a->ino < b->ino) return 1;
 25
 26	/*
 27	 * Synthesized MMAP events have zero ino_generation, avoid comparing
 28	 * them with MMAP events with actual ino_generation.
 29	 *
 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
 33	 * otherwise. - Adrian
 34	 */
 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;
 38	}
 39
 40	return 0;
 41}
 42
 43static bool dso_id__empty(struct dso_id *id)
 44{
 45	if (!id)
 46		return true;
 47
 48	return !id->maj && !id->min && !id->ino && !id->ino_generation;
 49}
 50
 51static void dso__inject_id(struct dso *dso, struct dso_id *id)
 52{
 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;
 57}
 58
 59static int dso_id__cmp(struct dso_id *a, struct dso_id *b)
 60{
 61	/*
 62	 * The second is always dso->id, so zeroes if not set, assume passing
 63	 * NULL for a means a zeroed id
 64	 */
 65	if (dso_id__empty(a) || dso_id__empty(b))
 66		return 0;
 67
 68	return __dso_id__cmp(a, b);
 69}
 70
 71int dso__cmp_id(struct dso *a, struct dso *b)
 72{
 73	return __dso_id__cmp(&a->id, &b->id);
 74}
 75
 76bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
 77{
 78	bool have_build_id = false;
 79	struct dso *pos;
 80	struct nscookie nsc;
 81
 82	list_for_each_entry(pos, head, node) {
 83		if (with_hits && !pos->hit && !dso__is_vdso(pos))
 84			continue;
 85		if (pos->has_build_id) {
 86			have_build_id = true;
 87			continue;
 88		}
 89		nsinfo__mountns_enter(pos->nsinfo, &nsc);
 90		if (filename__read_build_id(pos->long_name, &pos->bid) > 0) {
 
 91			have_build_id	  = true;
 92			pos->has_build_id = true;
 93		} else if (errno == ENOENT && pos->nsinfo) {
 94			char *new_name = filename_with_chroot(pos->nsinfo->pid,
 95							      pos->long_name);
 96
 97			if (new_name && filename__read_build_id(new_name,
 98								&pos->bid) > 0) {
 99				have_build_id = true;
100				pos->has_build_id = true;
101			}
102			free(new_name);
103		}
104		nsinfo__mountns_exit(&nsc);
105	}
106
107	return have_build_id;
108}
109
110static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, struct dso *b)
111{
112	int rc = strcmp(long_name, b->long_name);
113	return rc ?: dso_id__cmp(id, &b->id);
114}
115
116static int __dso__cmp_short_name(const char *short_name, struct dso_id *id, struct dso *b)
117{
118	int rc = strcmp(short_name, b->short_name);
119	return rc ?: dso_id__cmp(id, &b->id);
120}
121
122static int dso__cmp_short_name(struct dso *a, struct dso *b)
123{
124	return __dso__cmp_short_name(a->short_name, &a->id, b);
125}
126
127/*
128 * Find a matching entry and/or link current entry to RB tree.
129 * Either one of the dso or name parameter must be non-NULL or the
130 * function will not work.
131 */
132struct dso *__dsos__findnew_link_by_longname_id(struct rb_root *root, struct dso *dso,
133						const char *name, struct dso_id *id)
134{
135	struct rb_node **p = &root->rb_node;
136	struct rb_node  *parent = NULL;
137
138	if (!name)
139		name = dso->long_name;
140	/*
141	 * Find node with the matching name
142	 */
143	while (*p) {
144		struct dso *this = rb_entry(*p, struct dso, rb_node);
145		int rc = __dso__cmp_long_name(name, id, this);
146
147		parent = *p;
148		if (rc == 0) {
149			/*
150			 * In case the new DSO is a duplicate of an existing
151			 * one, print a one-time warning & put the new entry
152			 * at the end of the list of duplicates.
153			 */
154			if (!dso || (dso == this))
155				return this;	/* Find matching dso */
156			/*
157			 * The core kernel DSOs may have duplicated long name.
158			 * In this case, the short name should be different.
159			 * Comparing the short names to differentiate the DSOs.
160			 */
161			rc = dso__cmp_short_name(dso, this);
162			if (rc == 0) {
163				pr_err("Duplicated dso name: %s\n", name);
164				return NULL;
165			}
166		}
167		if (rc < 0)
168			p = &parent->rb_left;
169		else
170			p = &parent->rb_right;
171	}
172	if (dso) {
173		/* Add new node and rebalance tree */
174		rb_link_node(&dso->rb_node, parent, p);
175		rb_insert_color(&dso->rb_node, root);
176		dso->root = root;
177	}
178	return NULL;
179}
180
181void __dsos__add(struct dsos *dsos, struct dso *dso)
182{
183	list_add_tail(&dso->node, &dsos->head);
184	__dsos__findnew_link_by_longname_id(&dsos->root, dso, NULL, &dso->id);
185	/*
186	 * It is now in the linked list, grab a reference, then garbage collect
187	 * this when needing memory, by looking at LRU dso instances in the
188	 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
189	 * anywhere besides the one for the list, do, under a lock for the
190	 * list: remove it from the list, then a dso__put(), that probably will
191	 * be the last and will then call dso__delete(), end of life.
192	 *
193	 * That, or at the end of the 'struct machine' lifetime, when all
194	 * 'struct dso' instances will be removed from the list, in
195	 * dsos__exit(), if they have no other reference from some other data
196	 * structure.
197	 *
198	 * E.g.: after processing a 'perf.data' file and storing references
199	 * to objects instantiated while processing events, we will have
200	 * references to the 'thread', 'map', 'dso' structs all from 'struct
201	 * hist_entry' instances, but we may not need anything not referenced,
202	 * so we might as well call machines__exit()/machines__delete() and
203	 * garbage collect it.
204	 */
205	dso__get(dso);
206}
207
208void dsos__add(struct dsos *dsos, struct dso *dso)
209{
210	down_write(&dsos->lock);
211	__dsos__add(dsos, dso);
212	up_write(&dsos->lock);
213}
214
215static struct dso *__dsos__findnew_by_longname_id(struct rb_root *root, const char *name, struct dso_id *id)
216{
217	return __dsos__findnew_link_by_longname_id(root, NULL, name, id);
218}
219
220static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, struct dso_id *id, bool cmp_short)
221{
222	struct dso *pos;
223
224	if (cmp_short) {
225		list_for_each_entry(pos, &dsos->head, node)
226			if (__dso__cmp_short_name(name, id, pos) == 0)
227				return pos;
228		return NULL;
229	}
230	return __dsos__findnew_by_longname_id(&dsos->root, name, id);
231}
232
233struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
234{
235	return __dsos__find_id(dsos, name, NULL, cmp_short);
236}
237
238static void dso__set_basename(struct dso *dso)
239{
240	char *base, *lname;
241	int tid;
242
243	if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
244		if (asprintf(&base, "[JIT] tid %d", tid) < 0)
245			return;
246	} else {
247	      /*
248	       * basename() may modify path buffer, so we must pass
249               * a copy.
250               */
251		lname = strdup(dso->long_name);
252		if (!lname)
253			return;
254
255		/*
256		 * basename() may return a pointer to internal
257		 * storage which is reused in subsequent calls
258		 * so copy the result.
259		 */
260		base = strdup(basename(lname));
261
262		free(lname);
263
264		if (!base)
265			return;
266	}
267	dso__set_short_name(dso, base, true);
268}
269
270static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
271{
272	struct dso *dso = dso__new_id(name, id);
273
274	if (dso != NULL) {
275		__dsos__add(dsos, dso);
276		dso__set_basename(dso);
277		/* Put dso here because __dsos_add already got it */
278		dso__put(dso);
279	}
280	return dso;
281}
282
283struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
284{
285	return __dsos__addnew_id(dsos, name, NULL);
286}
287
288static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
289{
290	struct dso *dso = __dsos__find_id(dsos, name, id, false);
291
292	if (dso && dso_id__empty(&dso->id) && !dso_id__empty(id))
293		dso__inject_id(dso, id);
294
295	return dso ? dso : __dsos__addnew_id(dsos, name, id);
296}
297
298struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
299{
300	struct dso *dso;
301	down_write(&dsos->lock);
302	dso = dso__get(__dsos__findnew_id(dsos, name, id));
303	up_write(&dsos->lock);
304	return dso;
305}
306
307size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
308			       bool (skip)(struct dso *dso, int parm), int parm)
309{
310	struct dso *pos;
311	size_t ret = 0;
312
313	list_for_each_entry(pos, head, node) {
314		char sbuild_id[SBUILD_ID_SIZE];
315
316		if (skip && skip(pos, parm))
317			continue;
318		build_id__sprintf(&pos->bid, sbuild_id);
319		ret += fprintf(fp, "%-40s %s\n", sbuild_id, pos->long_name);
320	}
321	return ret;
322}
323
324size_t __dsos__fprintf(struct list_head *head, FILE *fp)
325{
326	struct dso *pos;
327	size_t ret = 0;
328
329	list_for_each_entry(pos, head, node) {
330		ret += dso__fprintf(pos, fp);
331	}
332
333	return ret;
334}
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2#include "debug.h"
  3#include "dsos.h"
  4#include "dso.h"
 
  5#include "vdso.h"
  6#include "namespaces.h"
 
  7#include <libgen.h>
  8#include <stdlib.h>
  9#include <string.h>
 10#include <symbol.h> // filename__read_build_id
 
 11
 12static int __dso_id__cmp(struct dso_id *a, struct dso_id *b)
 13{
 14	if (a->maj > b->maj) return -1;
 15	if (a->maj < b->maj) return 1;
 16
 17	if (a->min > b->min) return -1;
 18	if (a->min < b->min) return 1;
 19
 20	if (a->ino > b->ino) return -1;
 21	if (a->ino < b->ino) return 1;
 22
 23	if (a->ino_generation > b->ino_generation) return -1;
 24	if (a->ino_generation < b->ino_generation) return 1;
 
 
 
 
 
 
 
 
 
 
 
 25
 26	return 0;
 27}
 28
 29static bool dso_id__empty(struct dso_id *id)
 30{
 31	if (!id)
 32		return true;
 33
 34	return !id->maj && !id->min && !id->ino && !id->ino_generation;
 35}
 36
 37static void dso__inject_id(struct dso *dso, struct dso_id *id)
 38{
 39	dso->id.maj = id->maj;
 40	dso->id.min = id->min;
 41	dso->id.ino = id->ino;
 42	dso->id.ino_generation = id->ino_generation;
 43}
 44
 45static int dso_id__cmp(struct dso_id *a, struct dso_id *b)
 46{
 47	/*
 48	 * The second is always dso->id, so zeroes if not set, assume passing
 49	 * NULL for a means a zeroed id
 50	 */
 51	if (dso_id__empty(a) || dso_id__empty(b))
 52		return 0;
 53
 54	return __dso_id__cmp(a, b);
 55}
 56
 57int dso__cmp_id(struct dso *a, struct dso *b)
 58{
 59	return __dso_id__cmp(&a->id, &b->id);
 60}
 61
 62bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
 63{
 64	bool have_build_id = false;
 65	struct dso *pos;
 66	struct nscookie nsc;
 67
 68	list_for_each_entry(pos, head, node) {
 69		if (with_hits && !pos->hit && !dso__is_vdso(pos))
 70			continue;
 71		if (pos->has_build_id) {
 72			have_build_id = true;
 73			continue;
 74		}
 75		nsinfo__mountns_enter(pos->nsinfo, &nsc);
 76		if (filename__read_build_id(pos->long_name, pos->build_id,
 77					    sizeof(pos->build_id)) > 0) {
 78			have_build_id	  = true;
 79			pos->has_build_id = true;
 
 
 
 
 
 
 
 
 
 
 80		}
 81		nsinfo__mountns_exit(&nsc);
 82	}
 83
 84	return have_build_id;
 85}
 86
 87static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, struct dso *b)
 88{
 89	int rc = strcmp(long_name, b->long_name);
 90	return rc ?: dso_id__cmp(id, &b->id);
 91}
 92
 93static int __dso__cmp_short_name(const char *short_name, struct dso_id *id, struct dso *b)
 94{
 95	int rc = strcmp(short_name, b->short_name);
 96	return rc ?: dso_id__cmp(id, &b->id);
 97}
 98
 99static int dso__cmp_short_name(struct dso *a, struct dso *b)
100{
101	return __dso__cmp_short_name(a->short_name, &a->id, b);
102}
103
104/*
105 * Find a matching entry and/or link current entry to RB tree.
106 * Either one of the dso or name parameter must be non-NULL or the
107 * function will not work.
108 */
109struct dso *__dsos__findnew_link_by_longname_id(struct rb_root *root, struct dso *dso,
110						const char *name, struct dso_id *id)
111{
112	struct rb_node **p = &root->rb_node;
113	struct rb_node  *parent = NULL;
114
115	if (!name)
116		name = dso->long_name;
117	/*
118	 * Find node with the matching name
119	 */
120	while (*p) {
121		struct dso *this = rb_entry(*p, struct dso, rb_node);
122		int rc = __dso__cmp_long_name(name, id, this);
123
124		parent = *p;
125		if (rc == 0) {
126			/*
127			 * In case the new DSO is a duplicate of an existing
128			 * one, print a one-time warning & put the new entry
129			 * at the end of the list of duplicates.
130			 */
131			if (!dso || (dso == this))
132				return this;	/* Find matching dso */
133			/*
134			 * The core kernel DSOs may have duplicated long name.
135			 * In this case, the short name should be different.
136			 * Comparing the short names to differentiate the DSOs.
137			 */
138			rc = dso__cmp_short_name(dso, this);
139			if (rc == 0) {
140				pr_err("Duplicated dso name: %s\n", name);
141				return NULL;
142			}
143		}
144		if (rc < 0)
145			p = &parent->rb_left;
146		else
147			p = &parent->rb_right;
148	}
149	if (dso) {
150		/* Add new node and rebalance tree */
151		rb_link_node(&dso->rb_node, parent, p);
152		rb_insert_color(&dso->rb_node, root);
153		dso->root = root;
154	}
155	return NULL;
156}
157
158void __dsos__add(struct dsos *dsos, struct dso *dso)
159{
160	list_add_tail(&dso->node, &dsos->head);
161	__dsos__findnew_link_by_longname_id(&dsos->root, dso, NULL, &dso->id);
162	/*
163	 * It is now in the linked list, grab a reference, then garbage collect
164	 * this when needing memory, by looking at LRU dso instances in the
165	 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
166	 * anywhere besides the one for the list, do, under a lock for the
167	 * list: remove it from the list, then a dso__put(), that probably will
168	 * be the last and will then call dso__delete(), end of life.
169	 *
170	 * That, or at the end of the 'struct machine' lifetime, when all
171	 * 'struct dso' instances will be removed from the list, in
172	 * dsos__exit(), if they have no other reference from some other data
173	 * structure.
174	 *
175	 * E.g.: after processing a 'perf.data' file and storing references
176	 * to objects instantiated while processing events, we will have
177	 * references to the 'thread', 'map', 'dso' structs all from 'struct
178	 * hist_entry' instances, but we may not need anything not referenced,
179	 * so we might as well call machines__exit()/machines__delete() and
180	 * garbage collect it.
181	 */
182	dso__get(dso);
183}
184
185void dsos__add(struct dsos *dsos, struct dso *dso)
186{
187	down_write(&dsos->lock);
188	__dsos__add(dsos, dso);
189	up_write(&dsos->lock);
190}
191
192static struct dso *__dsos__findnew_by_longname_id(struct rb_root *root, const char *name, struct dso_id *id)
193{
194	return __dsos__findnew_link_by_longname_id(root, NULL, name, id);
195}
196
197static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, struct dso_id *id, bool cmp_short)
198{
199	struct dso *pos;
200
201	if (cmp_short) {
202		list_for_each_entry(pos, &dsos->head, node)
203			if (__dso__cmp_short_name(name, id, pos) == 0)
204				return pos;
205		return NULL;
206	}
207	return __dsos__findnew_by_longname_id(&dsos->root, name, id);
208}
209
210struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
211{
212	return __dsos__find_id(dsos, name, NULL, cmp_short);
213}
214
215static void dso__set_basename(struct dso *dso)
216{
217	char *base, *lname;
218	int tid;
219
220	if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
221		if (asprintf(&base, "[JIT] tid %d", tid) < 0)
222			return;
223	} else {
224	      /*
225	       * basename() may modify path buffer, so we must pass
226               * a copy.
227               */
228		lname = strdup(dso->long_name);
229		if (!lname)
230			return;
231
232		/*
233		 * basename() may return a pointer to internal
234		 * storage which is reused in subsequent calls
235		 * so copy the result.
236		 */
237		base = strdup(basename(lname));
238
239		free(lname);
240
241		if (!base)
242			return;
243	}
244	dso__set_short_name(dso, base, true);
245}
246
247static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
248{
249	struct dso *dso = dso__new_id(name, id);
250
251	if (dso != NULL) {
252		__dsos__add(dsos, dso);
253		dso__set_basename(dso);
254		/* Put dso here because __dsos_add already got it */
255		dso__put(dso);
256	}
257	return dso;
258}
259
260struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
261{
262	return __dsos__addnew_id(dsos, name, NULL);
263}
264
265static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
266{
267	struct dso *dso = __dsos__find_id(dsos, name, id, false);
268
269	if (dso && dso_id__empty(&dso->id) && !dso_id__empty(id))
270		dso__inject_id(dso, id);
271
272	return dso ? dso : __dsos__addnew_id(dsos, name, id);
273}
274
275struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
276{
277	struct dso *dso;
278	down_write(&dsos->lock);
279	dso = dso__get(__dsos__findnew_id(dsos, name, id));
280	up_write(&dsos->lock);
281	return dso;
282}
283
284size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
285			       bool (skip)(struct dso *dso, int parm), int parm)
286{
287	struct dso *pos;
288	size_t ret = 0;
289
290	list_for_each_entry(pos, head, node) {
 
 
291		if (skip && skip(pos, parm))
292			continue;
293		ret += dso__fprintf_buildid(pos, fp);
294		ret += fprintf(fp, " %s\n", pos->long_name);
295	}
296	return ret;
297}
298
299size_t __dsos__fprintf(struct list_head *head, FILE *fp)
300{
301	struct dso *pos;
302	size_t ret = 0;
303
304	list_for_each_entry(pos, head, node) {
305		ret += dso__fprintf(pos, fp);
306	}
307
308	return ret;
309}