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