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