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