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