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