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