]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - tools/perf/util/dso.c
UBUNTU: Ubuntu-5.3.0-29.31
[mirror_ubuntu-eoan-kernel.git] / tools / perf / util / dso.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
bda6ee4a 2#include <asm/bug.h>
877a7a11 3#include <linux/kernel.h>
af0de0c5 4#include <linux/string.h>
7f7c536f 5#include <linux/zalloc.h>
c6580451
JO
6#include <sys/time.h>
7#include <sys/resource.h>
7a8ef4c4
ACM
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
a43783ae 11#include <errno.h>
c23c2a0f 12#include <fcntl.h>
68c0188e 13#include <libgen.h>
6c398d72
JO
14#include <bpf/libbpf.h>
15#include "bpf-event.h"
611f0afe 16#include "compress.h"
40f3b2d2 17#include "namespaces.h"
9a3993d4 18#include "path.h"
1101f69a 19#include "map.h"
cdd059d7 20#include "symbol.h"
11ea2515 21#include "srcline.h"
cdd059d7 22#include "dso.h"
69d2591a 23#include "machine.h"
cfe9174f 24#include "auxtrace.h"
7f7c536f 25#include "util.h" /* O_CLOEXEC for older systems */
cdd059d7 26#include "debug.h"
a067558e 27#include "string2.h"
6ae98ba6 28#include "vdso.h"
cdd059d7 29
9343e45b
MGP
30static const char * const debuglink_paths[] = {
31 "%.0s%s",
32 "%s/%s",
33 "%s/.debug/%s",
34 "/usr/lib/debug%s/%s"
35};
36
cdd059d7
JO
37char dso__symtab_origin(const struct dso *dso)
38{
39 static const char origin[] = {
9cd00941
RRD
40 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
41 [DSO_BINARY_TYPE__VMLINUX] = 'v',
42 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
43 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
44 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
d2396999 45 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
9cd00941
RRD
46 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
47 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
48 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
49 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
50 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
51 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
c00c48fc 52 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
9cd00941
RRD
53 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
54 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
c00c48fc 55 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
9cd00941 56 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
cdd059d7
JO
57 };
58
59 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
60 return '!';
61 return origin[dso->symtab_type];
62}
63
ee4e9625
ACM
64int dso__read_binary_type_filename(const struct dso *dso,
65 enum dso_binary_type type,
66 char *root_dir, char *filename, size_t size)
cdd059d7 67{
b5d8bbe8 68 char build_id_hex[SBUILD_ID_SIZE];
cdd059d7 69 int ret = 0;
972f393b 70 size_t len;
cdd059d7
JO
71
72 switch (type) {
9343e45b
MGP
73 case DSO_BINARY_TYPE__DEBUGLINK:
74 {
75 const char *last_slash;
76 char dso_dir[PATH_MAX];
77 char symfile[PATH_MAX];
78 unsigned int i;
cdd059d7 79
dc6254cf 80 len = __symbol__join_symfs(filename, size, dso->long_name);
9343e45b
MGP
81 last_slash = filename + len;
82 while (last_slash != filename && *last_slash != '/')
83 last_slash--;
40356721 84
9343e45b
MGP
85 strncpy(dso_dir, filename, last_slash - filename);
86 dso_dir[last_slash-filename] = '\0';
87
88 if (!is_regular_file(filename)) {
89 ret = -1;
40356721 90 break;
9343e45b 91 }
40356721 92
9343e45b
MGP
93 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
94 if (ret)
95 break;
96
97 /* Check predefined locations where debug file might reside */
98 ret = -1;
99 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
100 snprintf(filename, size,
101 debuglink_paths[i], dso_dir, symfile);
102 if (is_regular_file(filename)) {
103 ret = 0;
104 break;
105 }
cdd059d7 106 }
9343e45b 107
cdd059d7 108 break;
9343e45b 109 }
cdd059d7 110 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
d2396999
KJ
111 if (dso__build_id_filename(dso, filename, size, false) == NULL)
112 ret = -1;
113 break;
114
115 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
116 if (dso__build_id_filename(dso, filename, size, true) == NULL)
cdd059d7
JO
117 ret = -1;
118 break;
119
120 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
972f393b
ACM
121 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
122 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
cdd059d7
JO
123 break;
124
125 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
972f393b
ACM
126 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
127 snprintf(filename + len, size - len, "%s", dso->long_name);
cdd059d7
JO
128 break;
129
9cd00941
RRD
130 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
131 {
bf4414ae 132 const char *last_slash;
9cd00941
RRD
133 size_t dir_size;
134
135 last_slash = dso->long_name + dso->long_name_len;
136 while (last_slash != dso->long_name && *last_slash != '/')
137 last_slash--;
138
972f393b 139 len = __symbol__join_symfs(filename, size, "");
9cd00941
RRD
140 dir_size = last_slash - dso->long_name + 2;
141 if (dir_size > (size - len)) {
142 ret = -1;
143 break;
144 }
7d2a5122
ACM
145 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
146 len += scnprintf(filename + len , size - len, ".debug%s",
9cd00941
RRD
147 last_slash);
148 break;
149 }
150
cdd059d7
JO
151 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
152 if (!dso->has_build_id) {
153 ret = -1;
154 break;
155 }
156
157 build_id__sprintf(dso->build_id,
158 sizeof(dso->build_id),
159 build_id_hex);
972f393b
ACM
160 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
161 snprintf(filename + len, size - len, "%.2s/%s.debug",
162 build_id_hex, build_id_hex + 2);
cdd059d7
JO
163 break;
164
39b12f78
AH
165 case DSO_BINARY_TYPE__VMLINUX:
166 case DSO_BINARY_TYPE__GUEST_VMLINUX:
cdd059d7 167 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
972f393b 168 __symbol__join_symfs(filename, size, dso->long_name);
cdd059d7
JO
169 break;
170
171 case DSO_BINARY_TYPE__GUEST_KMODULE:
c00c48fc 172 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
972f393b
ACM
173 path__join3(filename, size, symbol_conf.symfs,
174 root_dir, dso->long_name);
cdd059d7
JO
175 break;
176
177 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
c00c48fc 178 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
972f393b 179 __symbol__join_symfs(filename, size, dso->long_name);
cdd059d7
JO
180 break;
181
8e0cf965
AH
182 case DSO_BINARY_TYPE__KCORE:
183 case DSO_BINARY_TYPE__GUEST_KCORE:
7d2a5122 184 snprintf(filename, size, "%s", dso->long_name);
8e0cf965
AH
185 break;
186
cdd059d7
JO
187 default:
188 case DSO_BINARY_TYPE__KALLSYMS:
cdd059d7 189 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
cdd059d7 190 case DSO_BINARY_TYPE__JAVA_JIT:
9b86d04d 191 case DSO_BINARY_TYPE__BPF_PROG_INFO:
cdd059d7
JO
192 case DSO_BINARY_TYPE__NOT_FOUND:
193 ret = -1;
194 break;
195 }
196
197 return ret;
198}
199
4b838b0d
JO
200enum {
201 COMP_ID__NONE = 0,
202};
203
c00c48fc
NK
204static const struct {
205 const char *fmt;
206 int (*decompress)(const char *input, int output);
8b42b7e5 207 bool (*is_compressed)(const char *input);
c00c48fc 208} compressions[] = {
4b838b0d 209 [COMP_ID__NONE] = { .fmt = NULL, },
e92ce12e 210#ifdef HAVE_ZLIB_SUPPORT
8b42b7e5 211 { "gz", gzip_decompress_to_file, gzip_is_compressed },
80a32e5b
JO
212#endif
213#ifdef HAVE_LZMA_SUPPORT
8b42b7e5 214 { "xz", lzma_decompress_to_file, lzma_is_compressed },
e92ce12e 215#endif
8b42b7e5 216 { NULL, NULL, NULL },
c00c48fc
NK
217};
218
4b838b0d 219static int is_supported_compression(const char *ext)
c00c48fc
NK
220{
221 unsigned i;
222
4b838b0d 223 for (i = 1; compressions[i].fmt; i++) {
c00c48fc 224 if (!strcmp(ext, compressions[i].fmt))
4b838b0d 225 return i;
c00c48fc 226 }
4b838b0d 227 return COMP_ID__NONE;
c00c48fc
NK
228}
229
1f121b03 230bool is_kernel_module(const char *pathname, int cpumode)
c00c48fc 231{
8dee9ff1 232 struct kmod_path m;
1f121b03
WN
233 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
234
235 WARN_ONCE(mode != cpumode,
236 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
237 cpumode);
238
239 switch (mode) {
240 case PERF_RECORD_MISC_USER:
241 case PERF_RECORD_MISC_HYPERVISOR:
242 case PERF_RECORD_MISC_GUEST_USER:
243 return false;
244 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
245 default:
246 if (kmod_path__parse(&m, pathname)) {
247 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
248 pathname);
249 return true;
250 }
251 }
c00c48fc 252
8dee9ff1 253 return m.kmod;
c00c48fc
NK
254}
255
c00c48fc
NK
256bool dso__needs_decompress(struct dso *dso)
257{
258 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
259 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
260}
261
c9a8a613
JO
262static int decompress_kmodule(struct dso *dso, const char *name,
263 char *pathname, size_t len)
42b3fa67 264{
c9a8a613 265 char tmpbuf[] = KMOD_DECOMP_NAME;
42b3fa67 266 int fd = -1;
42b3fa67
NK
267
268 if (!dso__needs_decompress(dso))
269 return -1;
270
dde755a9 271 if (dso->comp == COMP_ID__NONE)
42b3fa67
NK
272 return -1;
273
8b42b7e5
JO
274 /*
275 * We have proper compression id for DSO and yet the file
276 * behind the 'name' can still be plain uncompressed object.
277 *
278 * The reason is behind the logic we open the DSO object files,
279 * when we try all possible 'debug' objects until we find the
280 * data. So even if the DSO is represented by 'krava.xz' module,
281 * we can end up here opening ~/.debug/....23432432/debug' file
282 * which is not compressed.
283 *
284 * To keep this transparent, we detect this and return the file
285 * descriptor to the uncompressed file.
286 */
287 if (!compressions[dso->comp].is_compressed(name))
288 return open(name, O_RDONLY);
289
42b3fa67
NK
290 fd = mkstemp(tmpbuf);
291 if (fd < 0) {
292 dso->load_errno = errno;
dde755a9 293 return -1;
42b3fa67
NK
294 }
295
dde755a9 296 if (compressions[dso->comp].decompress(name, fd)) {
42b3fa67
NK
297 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
298 close(fd);
299 fd = -1;
300 }
301
c9a8a613
JO
302 if (!pathname || (fd < 0))
303 unlink(tmpbuf);
304
305 if (pathname && (fd >= 0))
fca5085c 306 strlcpy(pathname, tmpbuf, len);
c9a8a613 307
42b3fa67
NK
308 return fd;
309}
310
311int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
312{
c9a8a613 313 return decompress_kmodule(dso, name, NULL, 0);
42b3fa67
NK
314}
315
316int dso__decompress_kmodule_path(struct dso *dso, const char *name,
317 char *pathname, size_t len)
318{
c9a8a613 319 int fd = decompress_kmodule(dso, name, pathname, len);
42b3fa67 320
42b3fa67 321 close(fd);
c9a8a613 322 return fd >= 0 ? 0 : -1;
42b3fa67
NK
323}
324
3c8a67f5
JO
325/*
326 * Parses kernel module specified in @path and updates
327 * @m argument like:
328 *
329 * @comp - true if @path contains supported compression suffix,
330 * false otherwise
331 * @kmod - true if @path contains '.ko' suffix in right position,
332 * false otherwise
333 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
334 * of the kernel module without suffixes, otherwise strudup-ed
335 * base name of @path
336 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
337 * the compression suffix
338 *
339 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
340 */
341int __kmod_path__parse(struct kmod_path *m, const char *path,
b946cd37 342 bool alloc_name)
3c8a67f5
JO
343{
344 const char *name = strrchr(path, '/');
345 const char *ext = strrchr(path, '.');
1f121b03 346 bool is_simple_name = false;
3c8a67f5
JO
347
348 memset(m, 0x0, sizeof(*m));
349 name = name ? name + 1 : path;
350
1f121b03
WN
351 /*
352 * '.' is also a valid character for module name. For example:
353 * [aaa.bbb] is a valid module name. '[' should have higher
354 * priority than '.ko' suffix.
355 *
356 * The kernel names are from machine__mmap_name. Such
357 * name should belong to kernel itself, not kernel module.
358 */
359 if (name[0] == '[') {
360 is_simple_name = true;
361 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
362 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
363 (strncmp(name, "[vdso]", 6) == 0) ||
aef4feac
AH
364 (strncmp(name, "[vdso32]", 8) == 0) ||
365 (strncmp(name, "[vdsox32]", 9) == 0) ||
1f121b03
WN
366 (strncmp(name, "[vsyscall]", 10) == 0)) {
367 m->kmod = false;
368
369 } else
370 m->kmod = true;
371 }
372
3c8a67f5 373 /* No extension, just return name. */
1f121b03 374 if ((ext == NULL) || is_simple_name) {
3c8a67f5
JO
375 if (alloc_name) {
376 m->name = strdup(name);
377 return m->name ? 0 : -ENOMEM;
378 }
379 return 0;
380 }
381
4b838b0d
JO
382 m->comp = is_supported_compression(ext + 1);
383 if (m->comp > COMP_ID__NONE)
3c8a67f5 384 ext -= 3;
3c8a67f5
JO
385
386 /* Check .ko extension only if there's enough name left. */
387 if (ext > name)
388 m->kmod = !strncmp(ext, ".ko", 3);
389
390 if (alloc_name) {
391 if (m->kmod) {
392 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
393 return -ENOMEM;
394 } else {
395 if (asprintf(&m->name, "%s", name) == -1)
396 return -ENOMEM;
397 }
398
af0de0c5 399 strreplace(m->name, '-', '_');
3c8a67f5
JO
400 }
401
3c8a67f5
JO
402 return 0;
403}
404
6b335e8f
NK
405void dso__set_module_info(struct dso *dso, struct kmod_path *m,
406 struct machine *machine)
407{
408 if (machine__is_host(machine))
409 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
410 else
411 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
412
413 /* _KMODULE_COMP should be next to _KMODULE */
2af52475 414 if (m->kmod && m->comp) {
6b335e8f 415 dso->symtab_type++;
2af52475
JO
416 dso->comp = m->comp;
417 }
6b335e8f
NK
418
419 dso__set_short_name(dso, strdup(m->name), true);
420}
421
eba5102d 422/*
bda6ee4a 423 * Global list of open DSOs and the counter.
eba5102d
JO
424 */
425static LIST_HEAD(dso__data_open);
bda6ee4a 426static long dso__data_open_cnt;
33bdedce 427static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
eba5102d
JO
428
429static void dso__list_add(struct dso *dso)
430{
431 list_add_tail(&dso->data.open_entry, &dso__data_open);
bda6ee4a 432 dso__data_open_cnt++;
eba5102d
JO
433}
434
435static void dso__list_del(struct dso *dso)
436{
e56fbc9d 437 list_del_init(&dso->data.open_entry);
bda6ee4a
JO
438 WARN_ONCE(dso__data_open_cnt <= 0,
439 "DSO data fd counter out of bounds.");
440 dso__data_open_cnt--;
eba5102d
JO
441}
442
a08cae03
JO
443static void close_first_dso(void);
444
445static int do_open(char *name)
446{
447 int fd;
6e81c74c 448 char sbuf[STRERR_BUFSIZE];
a08cae03
JO
449
450 do {
4c0d8d27 451 fd = open(name, O_RDONLY|O_CLOEXEC);
a08cae03
JO
452 if (fd >= 0)
453 return fd;
454
a3c0cc2a 455 pr_debug("dso open failed: %s\n",
c8b5f2c9 456 str_error_r(errno, sbuf, sizeof(sbuf)));
a08cae03
JO
457 if (!dso__data_open_cnt || errno != EMFILE)
458 break;
459
460 close_first_dso();
461 } while (1);
462
463 return -1;
464}
465
eba5102d 466static int __open_dso(struct dso *dso, struct machine *machine)
cdd059d7 467{
8ba29adf 468 int fd = -EINVAL;
ee4e9625
ACM
469 char *root_dir = (char *)"";
470 char *name = malloc(PATH_MAX);
d68a29c2 471 bool decomp = false;
cdd059d7 472
cdd059d7
JO
473 if (!name)
474 return -ENOMEM;
475
476 if (machine)
477 root_dir = machine->root_dir;
478
5f70619d 479 if (dso__read_binary_type_filename(dso, dso->binary_type,
8ba29adf
NK
480 root_dir, name, PATH_MAX))
481 goto out;
cdd059d7 482
8ba29adf
NK
483 if (!is_regular_file(name))
484 goto out;
3c028a0c 485
1d6b3c9b
NK
486 if (dso__needs_decompress(dso)) {
487 char newpath[KMOD_DECOMP_LEN];
488 size_t len = sizeof(newpath);
489
490 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
8ba29adf
NK
491 fd = -dso->load_errno;
492 goto out;
1d6b3c9b
NK
493 }
494
d68a29c2 495 decomp = true;
1d6b3c9b
NK
496 strcpy(name, newpath);
497 }
498
a08cae03 499 fd = do_open(name);
1d6b3c9b 500
d68a29c2 501 if (decomp)
1d6b3c9b
NK
502 unlink(name);
503
8ba29adf 504out:
cdd059d7
JO
505 free(name);
506 return fd;
507}
508
c6580451
JO
509static void check_data_close(void);
510
c1f9aa0a
JO
511/**
512 * dso_close - Open DSO data file
513 * @dso: dso object
514 *
515 * Open @dso's data file descriptor and updates
516 * list/count of open DSO objects.
517 */
eba5102d
JO
518static int open_dso(struct dso *dso, struct machine *machine)
519{
f045b8c4
KJ
520 int fd;
521 struct nscookie nsc;
522
523 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
524 nsinfo__mountns_enter(dso->nsinfo, &nsc);
525 fd = __open_dso(dso, machine);
526 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
527 nsinfo__mountns_exit(&nsc);
eba5102d 528
a6f6ae99 529 if (fd >= 0) {
eba5102d 530 dso__list_add(dso);
c6580451
JO
531 /*
532 * Check if we crossed the allowed number
533 * of opened DSOs and close one if needed.
534 */
535 check_data_close();
536 }
eba5102d
JO
537
538 return fd;
539}
540
541static void close_data_fd(struct dso *dso)
53fa8eaa
JO
542{
543 if (dso->data.fd >= 0) {
544 close(dso->data.fd);
545 dso->data.fd = -1;
c3fbd2a6 546 dso->data.file_size = 0;
eba5102d 547 dso__list_del(dso);
53fa8eaa
JO
548 }
549}
550
c1f9aa0a
JO
551/**
552 * dso_close - Close DSO data file
553 * @dso: dso object
554 *
555 * Close @dso's data file descriptor and updates
556 * list/count of open DSO objects.
557 */
eba5102d
JO
558static void close_dso(struct dso *dso)
559{
560 close_data_fd(dso);
561}
562
c6580451
JO
563static void close_first_dso(void)
564{
565 struct dso *dso;
566
567 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
568 close_dso(dso);
569}
570
571static rlim_t get_fd_limit(void)
572{
573 struct rlimit l;
574 rlim_t limit = 0;
575
576 /* Allow half of the current open fd limit. */
577 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
578 if (l.rlim_cur == RLIM_INFINITY)
579 limit = l.rlim_cur;
580 else
581 limit = l.rlim_cur / 2;
582 } else {
583 pr_err("failed to get fd limit\n");
584 limit = 1;
585 }
586
587 return limit;
588}
589
f3069249
JO
590static rlim_t fd_limit;
591
592/*
593 * Used only by tests/dso-data.c to reset the environment
594 * for tests. I dont expect we should change this during
595 * standard runtime.
596 */
597void reset_fd_limit(void)
c6580451 598{
f3069249
JO
599 fd_limit = 0;
600}
c6580451 601
f3069249
JO
602static bool may_cache_fd(void)
603{
604 if (!fd_limit)
605 fd_limit = get_fd_limit();
c6580451 606
f3069249 607 if (fd_limit == RLIM_INFINITY)
c6580451
JO
608 return true;
609
f3069249 610 return fd_limit > (rlim_t) dso__data_open_cnt;
c6580451
JO
611}
612
c1f9aa0a
JO
613/*
614 * Check and close LRU dso if we crossed allowed limit
615 * for opened dso file descriptors. The limit is half
616 * of the RLIMIT_NOFILE files opened.
617*/
c6580451
JO
618static void check_data_close(void)
619{
620 bool cache_fd = may_cache_fd();
621
622 if (!cache_fd)
623 close_first_dso();
624}
625
c1f9aa0a
JO
626/**
627 * dso__data_close - Close DSO data file
628 * @dso: dso object
629 *
630 * External interface to close @dso's data file descriptor.
631 */
eba5102d
JO
632void dso__data_close(struct dso *dso)
633{
33bdedce 634 pthread_mutex_lock(&dso__data_open_lock);
eba5102d 635 close_dso(dso);
33bdedce 636 pthread_mutex_unlock(&dso__data_open_lock);
eba5102d
JO
637}
638
71ff824a 639static void try_to_open_dso(struct dso *dso, struct machine *machine)
cdd059d7 640{
631d34b5 641 enum dso_binary_type binary_type_data[] = {
cdd059d7
JO
642 DSO_BINARY_TYPE__BUILD_ID_CACHE,
643 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
644 DSO_BINARY_TYPE__NOT_FOUND,
645 };
646 int i = 0;
647
53fa8eaa 648 if (dso->data.fd >= 0)
71ff824a 649 return;
53fa8eaa
JO
650
651 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
652 dso->data.fd = open_dso(dso, machine);
c27697d6 653 goto out;
53fa8eaa 654 }
cdd059d7
JO
655
656 do {
5f70619d 657 dso->binary_type = binary_type_data[i++];
cdd059d7 658
c27697d6
AH
659 dso->data.fd = open_dso(dso, machine);
660 if (dso->data.fd >= 0)
661 goto out;
cdd059d7 662
5f70619d 663 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
c27697d6
AH
664out:
665 if (dso->data.fd >= 0)
666 dso->data.status = DSO_DATA_STATUS_OK;
667 else
668 dso->data.status = DSO_DATA_STATUS_ERROR;
71ff824a
NK
669}
670
671/**
4bb11d01 672 * dso__data_get_fd - Get dso's data file descriptor
71ff824a
NK
673 * @dso: dso object
674 * @machine: machine object
675 *
676 * External interface to find dso's file, open it and
4bb11d01
NK
677 * returns file descriptor. It should be paired with
678 * dso__data_put_fd() if it returns non-negative value.
71ff824a 679 */
4bb11d01 680int dso__data_get_fd(struct dso *dso, struct machine *machine)
71ff824a
NK
681{
682 if (dso->data.status == DSO_DATA_STATUS_ERROR)
683 return -1;
cdd059d7 684
4bb11d01
NK
685 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
686 return -1;
687
71ff824a 688 try_to_open_dso(dso, machine);
4bb11d01
NK
689
690 if (dso->data.fd < 0)
691 pthread_mutex_unlock(&dso__data_open_lock);
71ff824a 692
c27697d6 693 return dso->data.fd;
cdd059d7
JO
694}
695
4bb11d01
NK
696void dso__data_put_fd(struct dso *dso __maybe_unused)
697{
698 pthread_mutex_unlock(&dso__data_open_lock);
699}
700
288be943
AH
701bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
702{
703 u32 flag = 1 << by;
704
705 if (dso->data.status_seen & flag)
706 return true;
707
708 dso->data.status_seen |= flag;
709
710 return false;
711}
712
6c398d72
JO
713static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
714{
715 struct bpf_prog_info_node *node;
716 ssize_t size = DSO__DATA_CACHE_SIZE;
717 u64 len;
718 u8 *buf;
719
720 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
721 if (!node || !node->info_linear) {
722 dso->data.status = DSO_DATA_STATUS_ERROR;
723 return -1;
724 }
725
726 len = node->info_linear->info.jited_prog_len;
727 buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
728
729 if (offset >= len)
730 return -1;
731
732 size = (ssize_t)min(len - offset, (u64)size);
733 memcpy(data, buf + offset, size);
734 return size;
735}
736
737static int bpf_size(struct dso *dso)
738{
739 struct bpf_prog_info_node *node;
740
741 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
742 if (!node || !node->info_linear) {
743 dso->data.status = DSO_DATA_STATUS_ERROR;
744 return -1;
745 }
746
747 dso->data.file_size = node->info_linear->info.jited_prog_len;
748 return 0;
749}
750
cdd059d7 751static void
8e67b725 752dso_cache__free(struct dso *dso)
cdd059d7 753{
8e67b725 754 struct rb_root *root = &dso->data.cache;
cdd059d7
JO
755 struct rb_node *next = rb_first(root);
756
8e67b725 757 pthread_mutex_lock(&dso->lock);
cdd059d7
JO
758 while (next) {
759 struct dso_cache *cache;
760
761 cache = rb_entry(next, struct dso_cache, rb_node);
762 next = rb_next(&cache->rb_node);
763 rb_erase(&cache->rb_node, root);
764 free(cache);
765 }
8e67b725 766 pthread_mutex_unlock(&dso->lock);
cdd059d7
JO
767}
768
8e67b725 769static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
cdd059d7 770{
8e67b725 771 const struct rb_root *root = &dso->data.cache;
3344996e
ACM
772 struct rb_node * const *p = &root->rb_node;
773 const struct rb_node *parent = NULL;
cdd059d7
JO
774 struct dso_cache *cache;
775
776 while (*p != NULL) {
777 u64 end;
778
779 parent = *p;
780 cache = rb_entry(parent, struct dso_cache, rb_node);
781 end = cache->offset + DSO__DATA_CACHE_SIZE;
782
783 if (offset < cache->offset)
784 p = &(*p)->rb_left;
785 else if (offset >= end)
786 p = &(*p)->rb_right;
787 else
788 return cache;
789 }
8e67b725 790
cdd059d7
JO
791 return NULL;
792}
793
8e67b725
NK
794static struct dso_cache *
795dso_cache__insert(struct dso *dso, struct dso_cache *new)
cdd059d7 796{
8e67b725 797 struct rb_root *root = &dso->data.cache;
cdd059d7
JO
798 struct rb_node **p = &root->rb_node;
799 struct rb_node *parent = NULL;
800 struct dso_cache *cache;
801 u64 offset = new->offset;
802
8e67b725 803 pthread_mutex_lock(&dso->lock);
cdd059d7
JO
804 while (*p != NULL) {
805 u64 end;
806
807 parent = *p;
808 cache = rb_entry(parent, struct dso_cache, rb_node);
809 end = cache->offset + DSO__DATA_CACHE_SIZE;
810
811 if (offset < cache->offset)
812 p = &(*p)->rb_left;
813 else if (offset >= end)
814 p = &(*p)->rb_right;
8e67b725
NK
815 else
816 goto out;
cdd059d7
JO
817 }
818
819 rb_link_node(&new->rb_node, parent, p);
820 rb_insert_color(&new->rb_node, root);
8e67b725
NK
821
822 cache = NULL;
823out:
824 pthread_mutex_unlock(&dso->lock);
825 return cache;
cdd059d7
JO
826}
827
828static ssize_t
829dso_cache__memcpy(struct dso_cache *cache, u64 offset,
830 u8 *data, u64 size)
831{
832 u64 cache_offset = offset - cache->offset;
833 u64 cache_size = min(cache->size - cache_offset, size);
834
835 memcpy(data, cache->data + cache_offset, cache_size);
836 return cache_size;
837}
838
ea5db1bd
JO
839static ssize_t file_read(struct dso *dso, struct machine *machine,
840 u64 offset, char *data)
841{
842 ssize_t ret;
843
844 pthread_mutex_lock(&dso__data_open_lock);
845
846 /*
847 * dso->data.fd might be closed if other thread opened another
848 * file (dso) due to open file limit (RLIMIT_NOFILE).
849 */
850 try_to_open_dso(dso, machine);
851
852 if (dso->data.fd < 0) {
853 dso->data.status = DSO_DATA_STATUS_ERROR;
854 ret = -errno;
855 goto out;
856 }
857
858 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
859out:
860 pthread_mutex_unlock(&dso__data_open_lock);
861 return ret;
862}
863
cdd059d7 864static ssize_t
33bdedce
NK
865dso_cache__read(struct dso *dso, struct machine *machine,
866 u64 offset, u8 *data, ssize_t size)
cdd059d7 867{
cacddfe7 868 u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
cdd059d7 869 struct dso_cache *cache;
8e67b725 870 struct dso_cache *old;
cdd059d7 871 ssize_t ret;
cdd059d7 872
cacddfe7
JO
873 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
874 if (!cache)
875 return -ENOMEM;
cdd059d7 876
6c398d72
JO
877 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
878 ret = bpf_read(dso, cache_offset, cache->data);
879 else
880 ret = file_read(dso, machine, cache_offset, cache->data);
881
cacddfe7 882 if (ret > 0) {
cdd059d7
JO
883 cache->offset = cache_offset;
884 cache->size = ret;
33bdedce 885
8e67b725
NK
886 old = dso_cache__insert(dso, cache);
887 if (old) {
888 /* we lose the race */
889 free(cache);
890 cache = old;
891 }
cdd059d7
JO
892
893 ret = dso_cache__memcpy(cache, offset, data, size);
33bdedce 894 }
cdd059d7
JO
895
896 if (ret <= 0)
897 free(cache);
898
cdd059d7
JO
899 return ret;
900}
901
33bdedce
NK
902static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
903 u64 offset, u8 *data, ssize_t size)
cdd059d7
JO
904{
905 struct dso_cache *cache;
906
8e67b725 907 cache = dso_cache__find(dso, offset);
cdd059d7
JO
908 if (cache)
909 return dso_cache__memcpy(cache, offset, data, size);
910 else
33bdedce 911 return dso_cache__read(dso, machine, offset, data, size);
cdd059d7
JO
912}
913
c1f9aa0a
JO
914/*
915 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
916 * in the rb_tree. Any read to already cached data is served
917 * by cached data.
918 */
33bdedce
NK
919static ssize_t cached_read(struct dso *dso, struct machine *machine,
920 u64 offset, u8 *data, ssize_t size)
cdd059d7
JO
921{
922 ssize_t r = 0;
923 u8 *p = data;
924
925 do {
926 ssize_t ret;
927
33bdedce 928 ret = dso_cache_read(dso, machine, offset, p, size);
cdd059d7
JO
929 if (ret < 0)
930 return ret;
931
932 /* Reached EOF, return what we have. */
933 if (!ret)
934 break;
935
936 BUG_ON(ret > size);
937
938 r += ret;
939 p += ret;
940 offset += ret;
941 size -= ret;
942
943 } while (size);
944
945 return r;
946}
947
5523769e 948static int file_size(struct dso *dso, struct machine *machine)
c3fbd2a6 949{
33bdedce 950 int ret = 0;
c3fbd2a6 951 struct stat st;
6e81c74c 952 char sbuf[STRERR_BUFSIZE];
c3fbd2a6 953
33bdedce
NK
954 pthread_mutex_lock(&dso__data_open_lock);
955
956 /*
957 * dso->data.fd might be closed if other thread opened another
958 * file (dso) due to open file limit (RLIMIT_NOFILE).
959 */
71ff824a
NK
960 try_to_open_dso(dso, machine);
961
33bdedce 962 if (dso->data.fd < 0) {
71ff824a
NK
963 ret = -errno;
964 dso->data.status = DSO_DATA_STATUS_ERROR;
965 goto out;
c3fbd2a6
JO
966 }
967
33bdedce
NK
968 if (fstat(dso->data.fd, &st) < 0) {
969 ret = -errno;
970 pr_err("dso cache fstat failed: %s\n",
c8b5f2c9 971 str_error_r(errno, sbuf, sizeof(sbuf)));
33bdedce
NK
972 dso->data.status = DSO_DATA_STATUS_ERROR;
973 goto out;
974 }
975 dso->data.file_size = st.st_size;
976
977out:
978 pthread_mutex_unlock(&dso__data_open_lock);
979 return ret;
c3fbd2a6
JO
980}
981
5523769e
JO
982int dso__data_file_size(struct dso *dso, struct machine *machine)
983{
984 if (dso->data.file_size)
985 return 0;
986
987 if (dso->data.status == DSO_DATA_STATUS_ERROR)
988 return -1;
989
6c398d72
JO
990 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
991 return bpf_size(dso);
992
5523769e
JO
993 return file_size(dso, machine);
994}
995
6d363459
AH
996/**
997 * dso__data_size - Return dso data size
998 * @dso: dso object
999 * @machine: machine object
1000 *
1001 * Return: dso data size
1002 */
1003off_t dso__data_size(struct dso *dso, struct machine *machine)
1004{
b5c2161c 1005 if (dso__data_file_size(dso, machine))
6d363459
AH
1006 return -1;
1007
1008 /* For now just estimate dso data size is close to file size */
1009 return dso->data.file_size;
1010}
1011
33bdedce
NK
1012static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
1013 u64 offset, u8 *data, ssize_t size)
c3fbd2a6 1014{
b5c2161c 1015 if (dso__data_file_size(dso, machine))
c3fbd2a6
JO
1016 return -1;
1017
1018 /* Check the offset sanity. */
1019 if (offset > dso->data.file_size)
1020 return -1;
1021
1022 if (offset + size < offset)
1023 return -1;
1024
33bdedce 1025 return cached_read(dso, machine, offset, data, size);
c3fbd2a6
JO
1026}
1027
c1f9aa0a
JO
1028/**
1029 * dso__data_read_offset - Read data from dso file offset
1030 * @dso: dso object
1031 * @machine: machine object
1032 * @offset: file offset
1033 * @data: buffer to store data
1034 * @size: size of the @data buffer
1035 *
1036 * External interface to read data from dso file offset. Open
1037 * dso data file and use cached_read to get the data.
1038 */
c3fbd2a6
JO
1039ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1040 u64 offset, u8 *data, ssize_t size)
1041{
33bdedce 1042 if (dso->data.status == DSO_DATA_STATUS_ERROR)
c3fbd2a6
JO
1043 return -1;
1044
33bdedce 1045 return data_read_offset(dso, machine, offset, data, size);
c3fbd2a6
JO
1046}
1047
c1f9aa0a
JO
1048/**
1049 * dso__data_read_addr - Read data from dso address
1050 * @dso: dso object
1051 * @machine: machine object
1052 * @add: virtual memory address
1053 * @data: buffer to store data
1054 * @size: size of the @data buffer
1055 *
1056 * External interface to read data from dso address.
1057 */
cdd059d7
JO
1058ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1059 struct machine *machine, u64 addr,
1060 u8 *data, ssize_t size)
1061{
1062 u64 offset = map->map_ip(map, addr);
1063 return dso__data_read_offset(dso, machine, offset, data, size);
1064}
1065
1066struct map *dso__new_map(const char *name)
1067{
1068 struct map *map = NULL;
1069 struct dso *dso = dso__new(name);
1070
1071 if (dso)
3183f8ca 1072 map = map__new2(0, dso);
cdd059d7
JO
1073
1074 return map;
1075}
1076
459ce518
ACM
1077struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1078 const char *short_name, int dso_type)
cdd059d7
JO
1079{
1080 /*
1081 * The kernel dso could be created by build_id processing.
1082 */
aa7cc2ae 1083 struct dso *dso = machine__findnew_dso(machine, name);
cdd059d7
JO
1084
1085 /*
1086 * We need to run this in all cases, since during the build_id
1087 * processing we had no idea this was the kernel dso.
1088 */
1089 if (dso != NULL) {
58a98c9c 1090 dso__set_short_name(dso, short_name, false);
cdd059d7
JO
1091 dso->kernel = dso_type;
1092 }
1093
1094 return dso;
1095}
1096
4598a0a6
WL
1097/*
1098 * Find a matching entry and/or link current entry to RB tree.
1099 * Either one of the dso or name parameter must be non-NULL or the
1100 * function will not work.
1101 */
e8807844
ACM
1102static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1103 struct dso *dso, const char *name)
4598a0a6
WL
1104{
1105 struct rb_node **p = &root->rb_node;
1106 struct rb_node *parent = NULL;
1107
1108 if (!name)
1109 name = dso->long_name;
1110 /*
1111 * Find node with the matching name
1112 */
1113 while (*p) {
1114 struct dso *this = rb_entry(*p, struct dso, rb_node);
1115 int rc = strcmp(name, this->long_name);
1116
1117 parent = *p;
1118 if (rc == 0) {
1119 /*
1120 * In case the new DSO is a duplicate of an existing
0f5e1558 1121 * one, print a one-time warning & put the new entry
4598a0a6
WL
1122 * at the end of the list of duplicates.
1123 */
1124 if (!dso || (dso == this))
1125 return this; /* Find matching dso */
1126 /*
1127 * The core kernel DSOs may have duplicated long name.
1128 * In this case, the short name should be different.
1129 * Comparing the short names to differentiate the DSOs.
1130 */
1131 rc = strcmp(dso->short_name, this->short_name);
1132 if (rc == 0) {
1133 pr_err("Duplicated dso name: %s\n", name);
1134 return NULL;
1135 }
1136 }
1137 if (rc < 0)
1138 p = &parent->rb_left;
1139 else
1140 p = &parent->rb_right;
1141 }
1142 if (dso) {
1143 /* Add new node and rebalance tree */
1144 rb_link_node(&dso->rb_node, parent, p);
1145 rb_insert_color(&dso->rb_node, root);
e266a753 1146 dso->root = root;
4598a0a6
WL
1147 }
1148 return NULL;
1149}
1150
e8807844
ACM
1151static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1152 const char *name)
4598a0a6 1153{
e8807844 1154 return __dso__findlink_by_longname(root, NULL, name);
4598a0a6
WL
1155}
1156
bf4414ae 1157void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
cdd059d7 1158{
e266a753
AH
1159 struct rb_root *root = dso->root;
1160
cdd059d7
JO
1161 if (name == NULL)
1162 return;
7e155d4d
ACM
1163
1164 if (dso->long_name_allocated)
bf4414ae 1165 free((char *)dso->long_name);
7e155d4d 1166
e266a753
AH
1167 if (root) {
1168 rb_erase(&dso->rb_node, root);
1169 /*
1170 * __dso__findlink_by_longname() isn't guaranteed to add it
1171 * back, so a clean removal is required here.
1172 */
1173 RB_CLEAR_NODE(&dso->rb_node);
1174 dso->root = NULL;
1175 }
1176
7e155d4d
ACM
1177 dso->long_name = name;
1178 dso->long_name_len = strlen(name);
1179 dso->long_name_allocated = name_allocated;
e266a753
AH
1180
1181 if (root)
1182 __dso__findlink_by_longname(root, dso, NULL);
cdd059d7
JO
1183}
1184
58a98c9c 1185void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
cdd059d7
JO
1186{
1187 if (name == NULL)
1188 return;
58a98c9c
AH
1189
1190 if (dso->short_name_allocated)
1191 free((char *)dso->short_name);
1192
1193 dso->short_name = name;
1194 dso->short_name_len = strlen(name);
1195 dso->short_name_allocated = name_allocated;
cdd059d7
JO
1196}
1197
1198static void dso__set_basename(struct dso *dso)
1199{
a4e7e6ef
AK
1200 char *base, *lname;
1201 int tid;
ac5e7f84 1202
a4e7e6ef
AK
1203 if (sscanf(dso->long_name, "/tmp/perf-%d.map", &tid) == 1) {
1204 if (asprintf(&base, "[JIT] tid %d", tid) < 0)
1205 return;
1206 } else {
1207 /*
1208 * basename() may modify path buffer, so we must pass
1209 * a copy.
1210 */
1211 lname = strdup(dso->long_name);
1212 if (!lname)
1213 return;
ac5e7f84 1214
a4e7e6ef
AK
1215 /*
1216 * basename() may return a pointer to internal
1217 * storage which is reused in subsequent calls
1218 * so copy the result.
1219 */
1220 base = strdup(basename(lname));
ac5e7f84 1221
a4e7e6ef 1222 free(lname);
ac5e7f84 1223
a4e7e6ef
AK
1224 if (!base)
1225 return;
1226 }
1227 dso__set_short_name(dso, base, true);
cdd059d7
JO
1228}
1229
1230int dso__name_len(const struct dso *dso)
1231{
1232 if (!dso)
1233 return strlen("[unknown]");
bb963e16 1234 if (verbose > 0)
cdd059d7
JO
1235 return dso->long_name_len;
1236
1237 return dso->short_name_len;
1238}
1239
3183f8ca 1240bool dso__loaded(const struct dso *dso)
cdd059d7 1241{
3183f8ca 1242 return dso->loaded;
cdd059d7
JO
1243}
1244
3183f8ca 1245bool dso__sorted_by_name(const struct dso *dso)
cdd059d7 1246{
3183f8ca 1247 return dso->sorted_by_name;
cdd059d7
JO
1248}
1249
3183f8ca 1250void dso__set_sorted_by_name(struct dso *dso)
cdd059d7 1251{
3183f8ca 1252 dso->sorted_by_name = true;
cdd059d7
JO
1253}
1254
1255struct dso *dso__new(const char *name)
1256{
1257 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1258
1259 if (dso != NULL) {
cdd059d7 1260 strcpy(dso->name, name);
7e155d4d 1261 dso__set_long_name(dso, dso->name, false);
58a98c9c 1262 dso__set_short_name(dso, dso->name, false);
7137ff50 1263 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
ca40e2af 1264 dso->data.cache = RB_ROOT;
55ecd631
DB
1265 dso->inlined_nodes = RB_ROOT_CACHED;
1266 dso->srclines = RB_ROOT_CACHED;
53fa8eaa 1267 dso->data.fd = -1;
c27697d6 1268 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
cdd059d7 1269 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
5f70619d 1270 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
c6d8f2a4 1271 dso->is_64_bit = (sizeof(void *) == 8);
cdd059d7 1272 dso->loaded = 0;
0131c4ec 1273 dso->rel = 0;
cdd059d7
JO
1274 dso->sorted_by_name = 0;
1275 dso->has_build_id = 0;
2cc9d0ef 1276 dso->has_srcline = 1;
906049c8 1277 dso->a2l_fails = 1;
cdd059d7
JO
1278 dso->kernel = DSO_TYPE_USER;
1279 dso->needs_swap = DSO_SWAP__UNSET;
2af52475 1280 dso->comp = COMP_ID__NONE;
4598a0a6 1281 RB_CLEAR_NODE(&dso->rb_node);
e266a753 1282 dso->root = NULL;
cdd059d7 1283 INIT_LIST_HEAD(&dso->node);
eba5102d 1284 INIT_LIST_HEAD(&dso->data.open_entry);
4a936edc 1285 pthread_mutex_init(&dso->lock, NULL);
7100810a 1286 refcount_set(&dso->refcnt, 1);
cdd059d7
JO
1287 }
1288
1289 return dso;
1290}
1291
1292void dso__delete(struct dso *dso)
1293{
4598a0a6
WL
1294 if (!RB_EMPTY_NODE(&dso->rb_node))
1295 pr_err("DSO %s is still in rbtree when being deleted!\n",
1296 dso->long_name);
11ea2515
MW
1297
1298 /* free inlines first, as they reference symbols */
1299 inlines__tree_delete(&dso->inlined_nodes);
21ac9d54 1300 srcline__tree_delete(&dso->srclines);
3183f8ca 1301 symbols__delete(&dso->symbols);
ee021d42
ACM
1302
1303 if (dso->short_name_allocated) {
04662523 1304 zfree((char **)&dso->short_name);
ee021d42
ACM
1305 dso->short_name_allocated = false;
1306 }
1307
1308 if (dso->long_name_allocated) {
04662523 1309 zfree((char **)&dso->long_name);
ee021d42
ACM
1310 dso->long_name_allocated = false;
1311 }
1312
53fa8eaa 1313 dso__data_close(dso);
cfe9174f 1314 auxtrace_cache__free(dso->auxtrace_cache);
8e67b725 1315 dso_cache__free(dso);
454ff00f 1316 dso__free_a2l(dso);
04662523 1317 zfree(&dso->symsrc_filename);
843ff37b 1318 nsinfo__zput(dso->nsinfo);
4a936edc 1319 pthread_mutex_destroy(&dso->lock);
cdd059d7
JO
1320 free(dso);
1321}
1322
d3a7c489
ACM
1323struct dso *dso__get(struct dso *dso)
1324{
1325 if (dso)
7100810a 1326 refcount_inc(&dso->refcnt);
d3a7c489
ACM
1327 return dso;
1328}
1329
1330void dso__put(struct dso *dso)
1331{
7100810a 1332 if (dso && refcount_dec_and_test(&dso->refcnt))
d3a7c489
ACM
1333 dso__delete(dso);
1334}
1335
cdd059d7
JO
1336void dso__set_build_id(struct dso *dso, void *build_id)
1337{
1338 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1339 dso->has_build_id = 1;
1340}
1341
1342bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1343{
1344 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1345}
1346
1347void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1348{
1349 char path[PATH_MAX];
1350
1351 if (machine__is_default_guest(machine))
1352 return;
1353 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1354 if (sysfs__read_build_id(path, dso->build_id,
1355 sizeof(dso->build_id)) == 0)
1356 dso->has_build_id = true;
1357}
1358
1359int dso__kernel_module_get_build_id(struct dso *dso,
1360 const char *root_dir)
1361{
1362 char filename[PATH_MAX];
1363 /*
1364 * kernel module short names are of the form "[module]" and
1365 * we need just "module" here.
1366 */
1367 const char *name = dso->short_name + 1;
1368
1369 snprintf(filename, sizeof(filename),
1370 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1371 root_dir, (int)strlen(name) - 1, name);
1372
1373 if (sysfs__read_build_id(filename, dso->build_id,
1374 sizeof(dso->build_id)) == 0)
1375 dso->has_build_id = true;
1376
1377 return 0;
1378}
1379
1380bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1381{
1382 bool have_build_id = false;
1383 struct dso *pos;
f045b8c4 1384 struct nscookie nsc;
cdd059d7
JO
1385
1386 list_for_each_entry(pos, head, node) {
6ae98ba6 1387 if (with_hits && !pos->hit && !dso__is_vdso(pos))
cdd059d7
JO
1388 continue;
1389 if (pos->has_build_id) {
1390 have_build_id = true;
1391 continue;
1392 }
f045b8c4 1393 nsinfo__mountns_enter(pos->nsinfo, &nsc);
cdd059d7
JO
1394 if (filename__read_build_id(pos->long_name, pos->build_id,
1395 sizeof(pos->build_id)) > 0) {
1396 have_build_id = true;
1397 pos->has_build_id = true;
1398 }
f045b8c4 1399 nsinfo__mountns_exit(&nsc);
cdd059d7
JO
1400 }
1401
1402 return have_build_id;
1403}
1404
e8807844 1405void __dsos__add(struct dsos *dsos, struct dso *dso)
cdd059d7 1406{
8fa7d87f 1407 list_add_tail(&dso->node, &dsos->head);
e8807844 1408 __dso__findlink_by_longname(&dsos->root, dso, NULL);
d3a7c489
ACM
1409 /*
1410 * It is now in the linked list, grab a reference, then garbage collect
1411 * this when needing memory, by looking at LRU dso instances in the
1412 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1413 * anywhere besides the one for the list, do, under a lock for the
1414 * list: remove it from the list, then a dso__put(), that probably will
1415 * be the last and will then call dso__delete(), end of life.
1416 *
1417 * That, or at the end of the 'struct machine' lifetime, when all
1418 * 'struct dso' instances will be removed from the list, in
1419 * dsos__exit(), if they have no other reference from some other data
1420 * structure.
1421 *
1422 * E.g.: after processing a 'perf.data' file and storing references
1423 * to objects instantiated while processing events, we will have
1424 * references to the 'thread', 'map', 'dso' structs all from 'struct
1425 * hist_entry' instances, but we may not need anything not referenced,
1426 * so we might as well call machines__exit()/machines__delete() and
1427 * garbage collect it.
1428 */
1429 dso__get(dso);
e8807844
ACM
1430}
1431
1432void dsos__add(struct dsos *dsos, struct dso *dso)
1433{
0a7c74ea 1434 down_write(&dsos->lock);
e8807844 1435 __dsos__add(dsos, dso);
0a7c74ea 1436 up_write(&dsos->lock);
cdd059d7
JO
1437}
1438
e8807844 1439struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
cdd059d7
JO
1440{
1441 struct dso *pos;
1442
f9ceffb6 1443 if (cmp_short) {
8fa7d87f 1444 list_for_each_entry(pos, &dsos->head, node)
f9ceffb6
WL
1445 if (strcmp(pos->short_name, name) == 0)
1446 return pos;
1447 return NULL;
1448 }
e8807844 1449 return __dso__find_by_longname(&dsos->root, name);
cdd059d7
JO
1450}
1451
e8807844
ACM
1452struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1453{
1454 struct dso *dso;
0a7c74ea 1455 down_read(&dsos->lock);
e8807844 1456 dso = __dsos__find(dsos, name, cmp_short);
0a7c74ea 1457 up_read(&dsos->lock);
e8807844
ACM
1458 return dso;
1459}
1460
1461struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
cdd059d7 1462{
701d8d7f 1463 struct dso *dso = dso__new(name);
cdd059d7 1464
701d8d7f 1465 if (dso != NULL) {
e8807844 1466 __dsos__add(dsos, dso);
701d8d7f 1467 dso__set_basename(dso);
82de26ab
MH
1468 /* Put dso here because __dsos_add already got it */
1469 dso__put(dso);
cdd059d7 1470 }
cdd059d7
JO
1471 return dso;
1472}
1473
701d8d7f
JO
1474struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1475{
e8807844
ACM
1476 struct dso *dso = __dsos__find(dsos, name, false);
1477
1478 return dso ? dso : __dsos__addnew(dsos, name);
1479}
701d8d7f 1480
e8807844
ACM
1481struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1482{
1483 struct dso *dso;
0a7c74ea 1484 down_write(&dsos->lock);
d3a7c489 1485 dso = dso__get(__dsos__findnew(dsos, name));
0a7c74ea 1486 up_write(&dsos->lock);
e8807844 1487 return dso;
701d8d7f
JO
1488}
1489
cdd059d7 1490size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
417c2ff6 1491 bool (skip)(struct dso *dso, int parm), int parm)
cdd059d7
JO
1492{
1493 struct dso *pos;
1494 size_t ret = 0;
1495
1496 list_for_each_entry(pos, head, node) {
417c2ff6 1497 if (skip && skip(pos, parm))
cdd059d7
JO
1498 continue;
1499 ret += dso__fprintf_buildid(pos, fp);
1500 ret += fprintf(fp, " %s\n", pos->long_name);
1501 }
1502 return ret;
1503}
1504
1505size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1506{
1507 struct dso *pos;
1508 size_t ret = 0;
1509
1510 list_for_each_entry(pos, head, node) {
3183f8ca 1511 ret += dso__fprintf(pos, fp);
cdd059d7
JO
1512 }
1513
1514 return ret;
1515}
1516
1517size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1518{
b5d8bbe8 1519 char sbuild_id[SBUILD_ID_SIZE];
cdd059d7
JO
1520
1521 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1522 return fprintf(fp, "%s", sbuild_id);
1523}
1524
3183f8ca 1525size_t dso__fprintf(struct dso *dso, FILE *fp)
cdd059d7
JO
1526{
1527 struct rb_node *nd;
1528 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1529
1530 if (dso->short_name != dso->long_name)
1531 ret += fprintf(fp, "%s, ", dso->long_name);
3183f8ca 1532 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
cdd059d7
JO
1533 ret += dso__fprintf_buildid(dso, fp);
1534 ret += fprintf(fp, ")\n");
7137ff50 1535 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
cdd059d7
JO
1536 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1537 ret += symbol__fprintf(pos, fp);
1538 }
1539
1540 return ret;
1541}
2b5b8bb2
AH
1542
1543enum dso_type dso__type(struct dso *dso, struct machine *machine)
1544{
1545 int fd;
4bb11d01 1546 enum dso_type type = DSO__TYPE_UNKNOWN;
2b5b8bb2 1547
4bb11d01
NK
1548 fd = dso__data_get_fd(dso, machine);
1549 if (fd >= 0) {
1550 type = dso__type_fd(fd);
1551 dso__data_put_fd(dso);
1552 }
2b5b8bb2 1553
4bb11d01 1554 return type;
2b5b8bb2 1555}
18425f13
ACM
1556
1557int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1558{
1559 int idx, errnum = dso->load_errno;
1560 /*
1561 * This must have a same ordering as the enum dso_load_errno.
1562 */
1563 static const char *dso_load__error_str[] = {
1564 "Internal tools/perf/ library error",
1565 "Invalid ELF file",
1566 "Can not read build id",
1567 "Mismatching build id",
1568 "Decompression failure",
1569 };
1570
1571 BUG_ON(buflen == 0);
1572
1573 if (errnum >= 0) {
c8b5f2c9 1574 const char *err = str_error_r(errnum, buf, buflen);
18425f13
ACM
1575
1576 if (err != buf)
1577 scnprintf(buf, buflen, "%s", err);
1578
1579 return 0;
1580 }
1581
1582 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1583 return -1;
1584
1585 idx = errnum - __DSO_LOAD_ERRNO__START;
1586 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1587 return 0;
1588}