xref: /linux/tools/perf/util/dsos.c (revision 3daee2e4b3568f0ed88b0598df96547fcf21cb9b)
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 
15 void 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 
25 static 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 
44 void dsos__exit(struct dsos *dsos)
45 {
46 	dsos__purge(dsos);
47 	exit_rwsem(&dsos->lock);
48 }
49 
50 
51 static 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 
66 struct dsos__read_build_ids_cb_args {
67 	bool with_hits;
68 	bool have_build_id;
69 };
70 
71 static 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 
99 bool 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 
110 static 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 
117 static 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 
124 static 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 
138 struct dsos__key {
139 	const char *long_name;
140 	const struct dso_id *id;
141 };
142 
143 static 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  */
156 static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
157 					       const char *name,
158 					       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->sorted) {
168 		if (!write_locked) {
169 			struct dso *dso;
170 
171 			up_read(&dsos->lock);
172 			down_write(&dsos->lock);
173 			dso = __dsos__find_by_longname_id(dsos, name, id,
174 							  /*write_locked=*/true);
175 			up_write(&dsos->lock);
176 			down_read(&dsos->lock);
177 			return dso;
178 		}
179 		qsort(dsos->dsos, dsos->cnt, sizeof(struct dso *),
180 		      dsos__cmp_long_name_id_short_name);
181 		dsos->sorted = true;
182 	}
183 
184 	res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *),
185 		      dsos__cmp_key_long_name_id);
186 	if (!res)
187 		return NULL;
188 
189 	return dso__get(*res);
190 }
191 
192 int __dsos__add(struct dsos *dsos, struct dso *dso)
193 {
194 	if (dsos->cnt == dsos->allocated) {
195 		unsigned int to_allocate = 2;
196 		struct dso **temp;
197 
198 		if (dsos->allocated > 0)
199 			to_allocate = dsos->allocated * 2;
200 		temp = realloc(dsos->dsos, sizeof(struct dso *) * to_allocate);
201 		if (!temp)
202 			return -ENOMEM;
203 		dsos->dsos = temp;
204 		dsos->allocated = to_allocate;
205 	}
206 	if (!dsos->sorted) {
207 		dsos->dsos[dsos->cnt++] = dso__get(dso);
208 	} else {
209 		int low = 0, high = dsos->cnt - 1;
210 		int insert = dsos->cnt; /* Default to inserting at the end. */
211 
212 		while (low <= high) {
213 			int mid = low + (high - low) / 2;
214 			int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
215 
216 			if (cmp < 0) {
217 				low = mid + 1;
218 			} else {
219 				high = mid - 1;
220 				insert = mid;
221 			}
222 		}
223 		memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
224 			(dsos->cnt - insert) * sizeof(struct dso *));
225 		dsos->cnt++;
226 		dsos->dsos[insert] = dso__get(dso);
227 	}
228 	dso__set_dsos(dso, dsos);
229 	return 0;
230 }
231 
232 int dsos__add(struct dsos *dsos, struct dso *dso)
233 {
234 	int ret;
235 
236 	down_write(&dsos->lock);
237 	ret = __dsos__add(dsos, dso);
238 	up_write(&dsos->lock);
239 	return ret;
240 }
241 
242 struct dsos__find_id_cb_args {
243 	const char *name;
244 	struct dso_id *id;
245 	struct dso *res;
246 };
247 
248 static int dsos__find_id_cb(struct dso *dso, void *data)
249 {
250 	struct dsos__find_id_cb_args *args = data;
251 
252 	if (__dso__cmp_short_name(args->name, args->id, dso) == 0) {
253 		args->res = dso__get(dso);
254 		return 1;
255 	}
256 	return 0;
257 
258 }
259 
260 static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, struct dso_id *id,
261 				   bool cmp_short, bool write_locked)
262 {
263 	struct dso *res;
264 
265 	if (cmp_short) {
266 		struct dsos__find_id_cb_args args = {
267 			.name = name,
268 			.id = id,
269 			.res = NULL,
270 		};
271 
272 		__dsos__for_each_dso(dsos, dsos__find_id_cb, &args);
273 		return args.res;
274 	}
275 	res = __dsos__find_by_longname_id(dsos, name, id, write_locked);
276 	return res;
277 }
278 
279 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
280 {
281 	struct dso *res;
282 
283 	down_read(&dsos->lock);
284 	res = __dsos__find_id(dsos, name, NULL, cmp_short, /*write_locked=*/false);
285 	up_read(&dsos->lock);
286 	return res;
287 }
288 
289 static void dso__set_basename(struct dso *dso)
290 {
291 	char *base, *lname;
292 	int tid;
293 
294 	if (sscanf(dso__long_name(dso), "/tmp/perf-%d.map", &tid) == 1) {
295 		if (asprintf(&base, "[JIT] tid %d", tid) < 0)
296 			return;
297 	} else {
298 	      /*
299 	       * basename() may modify path buffer, so we must pass
300                * a copy.
301                */
302 		lname = strdup(dso__long_name(dso));
303 		if (!lname)
304 			return;
305 
306 		/*
307 		 * basename() may return a pointer to internal
308 		 * storage which is reused in subsequent calls
309 		 * so copy the result.
310 		 */
311 		base = strdup(basename(lname));
312 
313 		free(lname);
314 
315 		if (!base)
316 			return;
317 	}
318 	dso__set_short_name(dso, base, true);
319 }
320 
321 static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
322 {
323 	struct dso *dso = dso__new_id(name, id);
324 
325 	if (dso != NULL) {
326 		/*
327 		 * The dsos lock is held on entry, so rename the dso before
328 		 * adding it to avoid needing to take the dsos lock again to say
329 		 * the array isn't sorted.
330 		 */
331 		dso__set_basename(dso);
332 		__dsos__add(dsos, dso);
333 	}
334 	return dso;
335 }
336 
337 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
338 {
339 	struct dso *dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true);
340 
341 	if (dso && dso_id__empty(dso__id(dso)) && !dso_id__empty(id))
342 		__dso__inject_id(dso, id);
343 
344 	return dso ? dso : __dsos__addnew_id(dsos, name, id);
345 }
346 
347 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct dso_id *id)
348 {
349 	struct dso *dso;
350 	down_write(&dsos->lock);
351 	dso = __dsos__findnew_id(dsos, name, id);
352 	up_write(&dsos->lock);
353 	return dso;
354 }
355 
356 struct dsos__fprintf_buildid_cb_args {
357 	FILE *fp;
358 	bool (*skip)(struct dso *dso, int parm);
359 	int parm;
360 	size_t ret;
361 };
362 
363 static int dsos__fprintf_buildid_cb(struct dso *dso, void *data)
364 {
365 	struct dsos__fprintf_buildid_cb_args *args = data;
366 	char sbuild_id[SBUILD_ID_SIZE];
367 
368 	if (args->skip && args->skip(dso, args->parm))
369 		return 0;
370 	build_id__sprintf(dso__bid(dso), sbuild_id);
371 	args->ret += fprintf(args->fp, "%-40s %s\n", sbuild_id, dso__long_name(dso));
372 	return 0;
373 }
374 
375 size_t dsos__fprintf_buildid(struct dsos *dsos, FILE *fp,
376 			       bool (*skip)(struct dso *dso, int parm), int parm)
377 {
378 	struct dsos__fprintf_buildid_cb_args args = {
379 		.fp = fp,
380 		.skip = skip,
381 		.parm = parm,
382 		.ret = 0,
383 	};
384 
385 	dsos__for_each_dso(dsos, dsos__fprintf_buildid_cb, &args);
386 	return args.ret;
387 }
388 
389 struct dsos__fprintf_cb_args {
390 	FILE *fp;
391 	size_t ret;
392 };
393 
394 static int dsos__fprintf_cb(struct dso *dso, void *data)
395 {
396 	struct dsos__fprintf_cb_args *args = data;
397 
398 	args->ret += dso__fprintf(dso, args->fp);
399 	return 0;
400 }
401 
402 size_t dsos__fprintf(struct dsos *dsos, FILE *fp)
403 {
404 	struct dsos__fprintf_cb_args args = {
405 		.fp = fp,
406 		.ret = 0,
407 	};
408 
409 	dsos__for_each_dso(dsos, dsos__fprintf_cb, &args);
410 	return args.ret;
411 }
412 
413 static int dsos__hit_all_cb(struct dso *dso, void *data __maybe_unused)
414 {
415 	dso__set_hit(dso);
416 	return 0;
417 }
418 
419 int dsos__hit_all(struct dsos *dsos)
420 {
421 	return dsos__for_each_dso(dsos, dsos__hit_all_cb, NULL);
422 }
423 
424 struct dso *dsos__findnew_module_dso(struct dsos *dsos,
425 				     struct machine *machine,
426 				     struct kmod_path *m,
427 				     const char *filename)
428 {
429 	struct dso *dso;
430 
431 	down_write(&dsos->lock);
432 
433 	dso = __dsos__find_id(dsos, m->name, NULL, /*cmp_short=*/true, /*write_locked=*/true);
434 	if (dso) {
435 		up_write(&dsos->lock);
436 		return dso;
437 	}
438 	/*
439 	 * Failed to find the dso so create it. Change the name before adding it
440 	 * to the array, to avoid unnecessary sorts and potential locking
441 	 * issues.
442 	 */
443 	dso = dso__new_id(m->name, /*id=*/NULL);
444 	if (!dso) {
445 		up_write(&dsos->lock);
446 		return NULL;
447 	}
448 	dso__set_basename(dso);
449 	dso__set_module_info(dso, m, machine);
450 	dso__set_long_name(dso,	strdup(filename), true);
451 	dso__set_kernel(dso, DSO_SPACE__KERNEL);
452 	__dsos__add(dsos, dso);
453 
454 	up_write(&dsos->lock);
455 	return dso;
456 }
457 
458 static int dsos__find_kernel_dso_cb(struct dso *dso, void *data)
459 {
460 	struct dso **res = data;
461 	/*
462 	 * The cpumode passed to is_kernel_module is not the cpumode of *this*
463 	 * event. If we insist on passing correct cpumode to is_kernel_module,
464 	 * we should record the cpumode when we adding this dso to the linked
465 	 * list.
466 	 *
467 	 * However we don't really need passing correct cpumode.  We know the
468 	 * correct cpumode must be kernel mode (if not, we should not link it
469 	 * onto kernel_dsos list).
470 	 *
471 	 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
472 	 * is_kernel_module() treats it as a kernel cpumode.
473 	 */
474 	if (!dso__kernel(dso) ||
475 	    is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOWN))
476 		return 0;
477 
478 	*res = dso__get(dso);
479 	return 1;
480 }
481 
482 struct dso *dsos__find_kernel_dso(struct dsos *dsos)
483 {
484 	struct dso *res = NULL;
485 
486 	dsos__for_each_dso(dsos, dsos__find_kernel_dso_cb, &res);
487 	return res;
488 }
489 
490 int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data)
491 {
492 	int err;
493 
494 	down_read(&dsos->lock);
495 	err = __dsos__for_each_dso(dsos, cb, data);
496 	up_read(&dsos->lock);
497 	return err;
498 }
499