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