File Index
Files and directories
Sign in
FILE

dsos.c

File preview with syntax highlighting.

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