Linux Audio

Check our new training course

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