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}
v6.13.7
  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
 15void dsos__init(struct dsos *dsos)
 16{
 17	init_rwsem(&dsos->lock);
 
 18
 19	dsos->cnt = 0;
 20	dsos->allocated = 0;
 21	dsos->dsos = NULL;
 22	dsos->sorted = true;
 23}
 24
 25static void dsos__purge(struct dsos *dsos)
 26{
 27	down_write(&dsos->lock);
 28
 29	for (unsigned int i = 0; i < dsos->cnt; i++) {
 30		struct dso *dso = dsos->dsos[i];
 
 
 
 
 
 
 
 
 
 
 
 31
 32		dso__set_dsos(dso, NULL);
 33		dso__put(dso);
 34	}
 35
 36	zfree(&dsos->dsos);
 37	dsos->cnt = 0;
 38	dsos->allocated = 0;
 39	dsos->sorted = true;
 40
 41	up_write(&dsos->lock);
 42}
 43
 44void dsos__exit(struct dsos *dsos)
 45{
 46	dsos__purge(dsos);
 47	exit_rwsem(&dsos->lock);
 
 
 48}
 49
 50
 51static int __dsos__for_each_dso(struct dsos *dsos,
 52				int (*cb)(struct dso *dso, void *data),
 53				void *data)
 54{
 55	for (unsigned int i = 0; i < dsos->cnt; i++) {
 56		struct dso *dso = dsos->dsos[i];
 57		int err;
 
 
 
 58
 59		err = cb(dso, data);
 60		if (err)
 61			return err;
 62	}
 63	return 0;
 64}
 65
 66struct dsos__read_build_ids_cb_args {
 67	bool with_hits;
 68	bool have_build_id;
 69};
 70
 71static int dsos__read_build_ids_cb(struct dso *dso, void *data)
 72{
 73	struct dsos__read_build_ids_cb_args *args = data;
 
 74	struct nscookie nsc;
 75
 76	if (args->with_hits && !dso__hit(dso) && !dso__is_vdso(dso))
 77		return 0;
 78	if (dso__has_build_id(dso)) {
 79		args->have_build_id = true;
 80		return 0;
 81	}
 82	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 83	if (filename__read_build_id(dso__long_name(dso), dso__bid(dso)) > 0) {
 84		args->have_build_id = true;
 85		dso__set_has_build_id(dso);
 86	} else if (errno == ENOENT && dso__nsinfo(dso)) {
 87		char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
 88
 89		if (new_name && filename__read_build_id(new_name, dso__bid(dso)) > 0) {
 90			args->have_build_id = true;
 91			dso__set_has_build_id(dso);
 
 
 
 
 92		}
 93		free(new_name);
 94	}
 95	nsinfo__mountns_exit(&nsc);
 96	return 0;
 97}
 98
 99bool dsos__read_build_ids(struct dsos *dsos, bool with_hits)
100{
101	struct dsos__read_build_ids_cb_args args = {
102		.with_hits = with_hits,
103		.have_build_id = false,
104	};
105
106	dsos__for_each_dso(dsos, dsos__read_build_ids_cb, &args);
107	return args.have_build_id;
108}
109
110static int __dso__cmp_long_name(const char *long_name, const struct dso_id *id,
111				const struct dso *b)
112{
113	int rc = strcmp(long_name, dso__long_name(b));
114	return rc ?: dso_id__cmp(id, dso__id_const(b));
115}
116
117static int __dso__cmp_short_name(const char *short_name, const struct dso_id *id,
118				 const struct dso *b)
119{
120	int rc = strcmp(short_name, dso__short_name(b));
121	return rc ?: dso_id__cmp(id, dso__id_const(b));
122}
123
124static int dsos__cmp_long_name_id_short_name(const void *va, const void *vb)
125{
126	const struct dso *a = *((const struct dso **)va);
127	const struct dso *b = *((const struct dso **)vb);
128	int rc = strcmp(dso__long_name(a), dso__long_name(b));
129
130	if (!rc) {
131		rc = dso_id__cmp(dso__id_const(a), dso__id_const(b));
132		if (!rc)
133			rc = strcmp(dso__short_name(a), dso__short_name(b));
134	}
135	return rc;
136}
137
138struct dsos__key {
139	const char *long_name;
140	const struct dso_id *id;
141};
142
143static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso)
144{
145	const struct dsos__key *key = vkey;
146	const struct dso *dso = *((const struct dso **)vdso);
147
148	return __dso__cmp_long_name(key->long_name, key->id, dso);
149}
150
151/*
152 * Find a matching entry and/or link current entry to RB tree.
153 * Either one of the dso or name parameter must be non-NULL or the
154 * function will not work.
155 */
156static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
157					       const char *name,
158					       const struct dso_id *id,
159					       bool write_locked)
160{
161	struct dsos__key key = {
162		.long_name = name,
163		.id = id,
164	};
165	struct dso **res;
166
167	if (dsos->dsos == NULL)
168		return NULL;
169
170	if (!dsos->sorted) {
171		if (!write_locked) {
172			struct dso *dso;
173
174			up_read(&dsos->lock);
175			down_write(&dsos->lock);
176			dso = __dsos__find_by_longname_id(dsos, name, id,
177							  /*write_locked=*/true);
178			up_write(&dsos->lock);
179			down_read(&dsos->lock);
180			return dso;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181		}
182		qsort(dsos->dsos, dsos->cnt, sizeof(struct dso *),
183		      dsos__cmp_long_name_id_short_name);
184		dsos->sorted = true;
 
 
 
 
 
 
 
185	}
186
187	res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *),
188		      dsos__cmp_key_long_name_id);
189	if (!res)
190		return NULL;
191
192	return dso__get(*res);
193}
194
195int __dsos__add(struct dsos *dsos, struct dso *dso)
196{
197	if (dsos->cnt == dsos->allocated) {
198		unsigned int to_allocate = 2;
199		struct dso **temp;
200
201		if (dsos->allocated > 0)
202			to_allocate = dsos->allocated * 2;
203		temp = realloc(dsos->dsos, sizeof(struct dso *) * to_allocate);
204		if (!temp)
205			return -ENOMEM;
206		dsos->dsos = temp;
207		dsos->allocated = to_allocate;
208	}
209	if (!dsos->sorted) {
210		dsos->dsos[dsos->cnt++] = dso__get(dso);
211	} else {
212		int low = 0, high = dsos->cnt - 1;
213		int insert = dsos->cnt; /* Default to inserting at the end. */
214
215		while (low <= high) {
216			int mid = low + (high - low) / 2;
217			int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
218
219			if (cmp < 0) {
220				low = mid + 1;
221			} else {
222				high = mid - 1;
223				insert = mid;
224			}
225		}
226		memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
227			(dsos->cnt - insert) * sizeof(struct dso *));
228		dsos->cnt++;
229		dsos->dsos[insert] = dso__get(dso);
230	}
231	dso__set_dsos(dso, dsos);
232	return 0;
233}
234
235int dsos__add(struct dsos *dsos, struct dso *dso)
236{
237	int ret;
238
239	down_write(&dsos->lock);
240	ret = __dsos__add(dsos, dso);
241	up_write(&dsos->lock);
242	return ret;
243}
244
245struct dsos__find_id_cb_args {
246	const char *name;
247	const struct dso_id *id;
248	struct dso *res;
249};
250
251static int dsos__find_id_cb(struct dso *dso, void *data)
252{
253	struct dsos__find_id_cb_args *args = data;
254
255	if (__dso__cmp_short_name(args->name, args->id, dso) == 0) {
256		args->res = dso__get(dso);
257		return 1;
258	}
259	return 0;
260
261}
262
263static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const struct dso_id *id,
264				   bool cmp_short, bool write_locked)
265{
266	struct dso *res;
267
268	if (cmp_short) {
269		struct dsos__find_id_cb_args args = {
270			.name = name,
271			.id = id,
272			.res = NULL,
273		};
274
275		__dsos__for_each_dso(dsos, dsos__find_id_cb, &args);
276		return args.res;
277	}
278	res = __dsos__find_by_longname_id(dsos, name, id, write_locked);
279	return res;
280}
281
282struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
283{
284	struct dso *res;
285
286	down_read(&dsos->lock);
287	res = __dsos__find_id(dsos, name, NULL, cmp_short, /*write_locked=*/false);
288	up_read(&dsos->lock);
289	return res;
290}
291
292static void dso__set_basename(struct dso *dso)
293{
294	char *base, *lname;
295	int tid;
296
297	if (perf_pid_map_tid(dso__long_name(dso), &tid)) {
298		if (asprintf(&base, "[JIT] tid %d", tid) < 0)
299			return;
300	} else {
301	      /*
302	       * basename() may modify path buffer, so we must pass
303               * a copy.
304               */
305		lname = strdup(dso__long_name(dso));
306		if (!lname)
307			return;
308
309		/*
310		 * basename() may return a pointer to internal
311		 * storage which is reused in subsequent calls
312		 * so copy the result.
313		 */
314		base = strdup(basename(lname));
315
316		free(lname);
317
318		if (!base)
319			return;
320	}
321	dso__set_short_name(dso, base, true);
322}
323
324static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
325{
326	struct dso *dso = dso__new_id(name, id);
327
328	if (dso != NULL) {
329		/*
330		 * The dsos lock is held on entry, so rename the dso before
331		 * adding it to avoid needing to take the dsos lock again to say
332		 * the array isn't sorted.
333		 */
334		dso__set_basename(dso);
335		__dsos__add(dsos, dso);
 
336	}
337	return dso;
338}
339
340static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
341{
342	struct dso *dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true);
 
343
344	if (dso && dso_id__empty(dso__id(dso)) && !dso_id__empty(id))
345		__dso__inject_id(dso, id);
 
 
 
 
346
347	return dso ? dso : __dsos__addnew_id(dsos, name, id);
348}
349
350struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
351{
352	struct dso *dso;
353	down_write(&dsos->lock);
354	dso = __dsos__findnew_id(dsos, name, id);
355	up_write(&dsos->lock);
356	return dso;
357}
358
359struct dsos__fprintf_buildid_cb_args {
360	FILE *fp;
361	bool (*skip)(struct dso *dso, int parm);
362	int parm;
363	size_t ret;
364};
365
366static int dsos__fprintf_buildid_cb(struct dso *dso, void *data)
367{
368	struct dsos__fprintf_buildid_cb_args *args = data;
369	char sbuild_id[SBUILD_ID_SIZE];
370
371	if (args->skip && args->skip(dso, args->parm))
372		return 0;
373	build_id__sprintf(dso__bid(dso), sbuild_id);
374	args->ret += fprintf(args->fp, "%-40s %s\n", sbuild_id, dso__long_name(dso));
375	return 0;
376}
377
378size_t dsos__fprintf_buildid(struct dsos *dsos, FILE *fp,
379			       bool (*skip)(struct dso *dso, int parm), int parm)
380{
381	struct dsos__fprintf_buildid_cb_args args = {
382		.fp = fp,
383		.skip = skip,
384		.parm = parm,
385		.ret = 0,
386	};
387
388	dsos__for_each_dso(dsos, dsos__fprintf_buildid_cb, &args);
389	return args.ret;
390}
391
392struct dsos__fprintf_cb_args {
393	FILE *fp;
394	size_t ret;
395};
396
397static int dsos__fprintf_cb(struct dso *dso, void *data)
398{
399	struct dsos__fprintf_cb_args *args = data;
400
401	args->ret += dso__fprintf(dso, args->fp);
402	return 0;
403}
404
405size_t dsos__fprintf(struct dsos *dsos, FILE *fp)
406{
407	struct dsos__fprintf_cb_args args = {
408		.fp = fp,
409		.ret = 0,
410	};
411
412	dsos__for_each_dso(dsos, dsos__fprintf_cb, &args);
413	return args.ret;
414}
415
416static int dsos__hit_all_cb(struct dso *dso, void *data __maybe_unused)
417{
418	dso__set_hit(dso);
419	return 0;
420}
421
422int dsos__hit_all(struct dsos *dsos)
423{
424	return dsos__for_each_dso(dsos, dsos__hit_all_cb, NULL);
425}
426
427struct dso *dsos__findnew_module_dso(struct dsos *dsos,
428				     struct machine *machine,
429				     struct kmod_path *m,
430				     const char *filename)
431{
432	struct dso *dso;
433
434	down_write(&dsos->lock);
435
436	dso = __dsos__find_id(dsos, m->name, NULL, /*cmp_short=*/true, /*write_locked=*/true);
437	if (dso) {
438		up_write(&dsos->lock);
439		return dso;
440	}
441	/*
442	 * Failed to find the dso so create it. Change the name before adding it
443	 * to the array, to avoid unnecessary sorts and potential locking
444	 * issues.
445	 */
446	dso = dso__new_id(m->name, /*id=*/NULL);
447	if (!dso) {
448		up_write(&dsos->lock);
449		return NULL;
450	}
451	dso__set_basename(dso);
452	dso__set_module_info(dso, m, machine);
453	dso__set_long_name(dso,	strdup(filename), true);
454	dso__set_kernel(dso, DSO_SPACE__KERNEL);
455	__dsos__add(dsos, dso);
456
457	up_write(&dsos->lock);
458	return dso;
459}
460
461static int dsos__find_kernel_dso_cb(struct dso *dso, void *data)
462{
463	struct dso **res = data;
464	/*
465	 * The cpumode passed to is_kernel_module is not the cpumode of *this*
466	 * event. If we insist on passing correct cpumode to is_kernel_module,
467	 * we should record the cpumode when we adding this dso to the linked
468	 * list.
469	 *
470	 * However we don't really need passing correct cpumode.  We know the
471	 * correct cpumode must be kernel mode (if not, we should not link it
472	 * onto kernel_dsos list).
473	 *
474	 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
475	 * is_kernel_module() treats it as a kernel cpumode.
476	 */
477	if (!dso__kernel(dso) ||
478	    is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOWN))
479		return 0;
480
481	*res = dso__get(dso);
482	return 1;
483}
484
485struct dso *dsos__find_kernel_dso(struct dsos *dsos)
486{
487	struct dso *res = NULL;
488
489	dsos__for_each_dso(dsos, dsos__find_kernel_dso_cb, &res);
490	return res;
491}
492
493int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data)
494{
495	int err;
496
497	down_read(&dsos->lock);
498	err = __dsos__for_each_dso(dsos, cb, data);
499	up_read(&dsos->lock);
500	return err;
501}