]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/dso.c
perf symbols: Preparation for compressed kernel module support
[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"
cdd059d7
JO
7#include "util.h"
8#include "debug.h"
9
10char dso__symtab_origin(const struct dso *dso)
11{
12 static const char origin[] = {
9cd00941
RRD
13 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
14 [DSO_BINARY_TYPE__VMLINUX] = 'v',
15 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
16 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
17 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
18 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
19 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
20 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
21 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
22 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
c00c48fc 24 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
9cd00941
RRD
25 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
26 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
c00c48fc 27 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
9cd00941 28 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
cdd059d7
JO
29 };
30
31 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
32 return '!';
33 return origin[dso->symtab_type];
34}
35
ee4e9625
ACM
36int dso__read_binary_type_filename(const struct dso *dso,
37 enum dso_binary_type type,
38 char *root_dir, char *filename, size_t size)
cdd059d7
JO
39{
40 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
41 int ret = 0;
972f393b 42 size_t len;
cdd059d7
JO
43
44 switch (type) {
45 case DSO_BINARY_TYPE__DEBUGLINK: {
46 char *debuglink;
47
7d2a5122
ACM
48 strncpy(filename, dso->long_name, size);
49 debuglink = filename + dso->long_name_len;
50 while (debuglink != filename && *debuglink != '/')
cdd059d7
JO
51 debuglink--;
52 if (*debuglink == '/')
53 debuglink++;
0d3dc5e8
SE
54 ret = filename__read_debuglink(dso->long_name, debuglink,
55 size - (debuglink - filename));
cdd059d7
JO
56 }
57 break;
58 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
59 /* skip the locally configured cache if a symfs is given */
60 if (symbol_conf.symfs[0] ||
7d2a5122 61 (dso__build_id_filename(dso, filename, size) == NULL))
cdd059d7
JO
62 ret = -1;
63 break;
64
65 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
972f393b
ACM
66 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
67 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
cdd059d7
JO
68 break;
69
70 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
972f393b
ACM
71 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
72 snprintf(filename + len, size - len, "%s", dso->long_name);
cdd059d7
JO
73 break;
74
9cd00941
RRD
75 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
76 {
bf4414ae 77 const char *last_slash;
9cd00941
RRD
78 size_t dir_size;
79
80 last_slash = dso->long_name + dso->long_name_len;
81 while (last_slash != dso->long_name && *last_slash != '/')
82 last_slash--;
83
972f393b 84 len = __symbol__join_symfs(filename, size, "");
9cd00941
RRD
85 dir_size = last_slash - dso->long_name + 2;
86 if (dir_size > (size - len)) {
87 ret = -1;
88 break;
89 }
7d2a5122
ACM
90 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
91 len += scnprintf(filename + len , size - len, ".debug%s",
9cd00941
RRD
92 last_slash);
93 break;
94 }
95
cdd059d7
JO
96 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
97 if (!dso->has_build_id) {
98 ret = -1;
99 break;
100 }
101
102 build_id__sprintf(dso->build_id,
103 sizeof(dso->build_id),
104 build_id_hex);
972f393b
ACM
105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
106 snprintf(filename + len, size - len, "%.2s/%s.debug",
107 build_id_hex, build_id_hex + 2);
cdd059d7
JO
108 break;
109
39b12f78
AH
110 case DSO_BINARY_TYPE__VMLINUX:
111 case DSO_BINARY_TYPE__GUEST_VMLINUX:
cdd059d7 112 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
972f393b 113 __symbol__join_symfs(filename, size, dso->long_name);
cdd059d7
JO
114 break;
115
116 case DSO_BINARY_TYPE__GUEST_KMODULE:
c00c48fc 117 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
972f393b
ACM
118 path__join3(filename, size, symbol_conf.symfs,
119 root_dir, dso->long_name);
cdd059d7
JO
120 break;
121
122 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
c00c48fc 123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
972f393b 124 __symbol__join_symfs(filename, size, dso->long_name);
cdd059d7
JO
125 break;
126
8e0cf965
AH
127 case DSO_BINARY_TYPE__KCORE:
128 case DSO_BINARY_TYPE__GUEST_KCORE:
7d2a5122 129 snprintf(filename, size, "%s", dso->long_name);
8e0cf965
AH
130 break;
131
cdd059d7
JO
132 default:
133 case DSO_BINARY_TYPE__KALLSYMS:
cdd059d7 134 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
cdd059d7
JO
135 case DSO_BINARY_TYPE__JAVA_JIT:
136 case DSO_BINARY_TYPE__NOT_FOUND:
137 ret = -1;
138 break;
139 }
140
141 return ret;
142}
143
c00c48fc
NK
144static int decompress_dummy(const char *input __maybe_unused,
145 int output __maybe_unused)
146{
147 return -1;
148}
149
150static const struct {
151 const char *fmt;
152 int (*decompress)(const char *input, int output);
153} compressions[] = {
154 { "gz", decompress_dummy },
155 { NULL, },
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
169bool is_kmodule_extension(const char *ext)
170{
171 if (strncmp(ext, "ko", 2))
172 return false;
173
174 if (ext[2] == '\0' || (ext[2] == '.' && is_supported_compression(ext+3)))
175 return true;
176
177 return false;
178}
179
180bool is_kernel_module(const char *pathname, bool *compressed)
181{
182 const char *ext = strrchr(pathname, '.');
183
184 if (ext == NULL)
185 return false;
186
187 if (is_supported_compression(ext + 1)) {
188 if (compressed)
189 *compressed = true;
190 ext -= 3;
191 } else if (compressed)
192 *compressed = false;
193
194 return is_kmodule_extension(ext + 1);
195}
196
197bool decompress_to_file(const char *ext, const char *filename, int output_fd)
198{
199 unsigned i;
200
201 for (i = 0; compressions[i].fmt; i++) {
202 if (!strcmp(ext, compressions[i].fmt))
203 return !compressions[i].decompress(filename,
204 output_fd);
205 }
206 return false;
207}
208
209bool dso__needs_decompress(struct dso *dso)
210{
211 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
212 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
213}
214
eba5102d 215/*
bda6ee4a 216 * Global list of open DSOs and the counter.
eba5102d
JO
217 */
218static LIST_HEAD(dso__data_open);
bda6ee4a 219static long dso__data_open_cnt;
eba5102d
JO
220
221static void dso__list_add(struct dso *dso)
222{
223 list_add_tail(&dso->data.open_entry, &dso__data_open);
bda6ee4a 224 dso__data_open_cnt++;
eba5102d
JO
225}
226
227static void dso__list_del(struct dso *dso)
228{
229 list_del(&dso->data.open_entry);
bda6ee4a
JO
230 WARN_ONCE(dso__data_open_cnt <= 0,
231 "DSO data fd counter out of bounds.");
232 dso__data_open_cnt--;
eba5102d
JO
233}
234
a08cae03
JO
235static void close_first_dso(void);
236
237static int do_open(char *name)
238{
239 int fd;
6e81c74c 240 char sbuf[STRERR_BUFSIZE];
a08cae03
JO
241
242 do {
243 fd = open(name, O_RDONLY);
244 if (fd >= 0)
245 return fd;
246
6e81c74c
MH
247 pr_debug("dso open failed, mmap: %s\n",
248 strerror_r(errno, sbuf, sizeof(sbuf)));
a08cae03
JO
249 if (!dso__data_open_cnt || errno != EMFILE)
250 break;
251
252 close_first_dso();
253 } while (1);
254
255 return -1;
256}
257
eba5102d 258static int __open_dso(struct dso *dso, struct machine *machine)
cdd059d7 259{
cdd059d7 260 int fd;
ee4e9625
ACM
261 char *root_dir = (char *)"";
262 char *name = malloc(PATH_MAX);
cdd059d7 263
cdd059d7
JO
264 if (!name)
265 return -ENOMEM;
266
267 if (machine)
268 root_dir = machine->root_dir;
269
5f70619d 270 if (dso__read_binary_type_filename(dso, dso->binary_type,
ee4e9625 271 root_dir, name, PATH_MAX)) {
cdd059d7
JO
272 free(name);
273 return -EINVAL;
274 }
275
a08cae03 276 fd = do_open(name);
cdd059d7
JO
277 free(name);
278 return fd;
279}
280
c6580451
JO
281static void check_data_close(void);
282
c1f9aa0a
JO
283/**
284 * dso_close - Open DSO data file
285 * @dso: dso object
286 *
287 * Open @dso's data file descriptor and updates
288 * list/count of open DSO objects.
289 */
eba5102d
JO
290static int open_dso(struct dso *dso, struct machine *machine)
291{
292 int fd = __open_dso(dso, machine);
293
a6f6ae99 294 if (fd >= 0) {
eba5102d 295 dso__list_add(dso);
c6580451
JO
296 /*
297 * Check if we crossed the allowed number
298 * of opened DSOs and close one if needed.
299 */
300 check_data_close();
301 }
eba5102d
JO
302
303 return fd;
304}
305
306static void close_data_fd(struct dso *dso)
53fa8eaa
JO
307{
308 if (dso->data.fd >= 0) {
309 close(dso->data.fd);
310 dso->data.fd = -1;
c3fbd2a6 311 dso->data.file_size = 0;
eba5102d 312 dso__list_del(dso);
53fa8eaa
JO
313 }
314}
315
c1f9aa0a
JO
316/**
317 * dso_close - Close DSO data file
318 * @dso: dso object
319 *
320 * Close @dso's data file descriptor and updates
321 * list/count of open DSO objects.
322 */
eba5102d
JO
323static void close_dso(struct dso *dso)
324{
325 close_data_fd(dso);
326}
327
c6580451
JO
328static void close_first_dso(void)
329{
330 struct dso *dso;
331
332 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
333 close_dso(dso);
334}
335
336static rlim_t get_fd_limit(void)
337{
338 struct rlimit l;
339 rlim_t limit = 0;
340
341 /* Allow half of the current open fd limit. */
342 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
343 if (l.rlim_cur == RLIM_INFINITY)
344 limit = l.rlim_cur;
345 else
346 limit = l.rlim_cur / 2;
347 } else {
348 pr_err("failed to get fd limit\n");
349 limit = 1;
350 }
351
352 return limit;
353}
354
355static bool may_cache_fd(void)
356{
357 static rlim_t limit;
358
359 if (!limit)
360 limit = get_fd_limit();
361
362 if (limit == RLIM_INFINITY)
363 return true;
364
365 return limit > (rlim_t) dso__data_open_cnt;
366}
367
c1f9aa0a
JO
368/*
369 * Check and close LRU dso if we crossed allowed limit
370 * for opened dso file descriptors. The limit is half
371 * of the RLIMIT_NOFILE files opened.
372*/
c6580451
JO
373static void check_data_close(void)
374{
375 bool cache_fd = may_cache_fd();
376
377 if (!cache_fd)
378 close_first_dso();
379}
380
c1f9aa0a
JO
381/**
382 * dso__data_close - Close DSO data file
383 * @dso: dso object
384 *
385 * External interface to close @dso's data file descriptor.
386 */
eba5102d
JO
387void dso__data_close(struct dso *dso)
388{
389 close_dso(dso);
390}
391
c1f9aa0a
JO
392/**
393 * dso__data_fd - Get dso's data file descriptor
394 * @dso: dso object
395 * @machine: machine object
396 *
397 * External interface to find dso's file, open it and
398 * returns file descriptor.
399 */
cdd059d7
JO
400int dso__data_fd(struct dso *dso, struct machine *machine)
401{
631d34b5 402 enum dso_binary_type binary_type_data[] = {
cdd059d7
JO
403 DSO_BINARY_TYPE__BUILD_ID_CACHE,
404 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
405 DSO_BINARY_TYPE__NOT_FOUND,
406 };
407 int i = 0;
408
c27697d6
AH
409 if (dso->data.status == DSO_DATA_STATUS_ERROR)
410 return -1;
411
53fa8eaa 412 if (dso->data.fd >= 0)
c27697d6 413 goto out;
53fa8eaa
JO
414
415 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
416 dso->data.fd = open_dso(dso, machine);
c27697d6 417 goto out;
53fa8eaa 418 }
cdd059d7
JO
419
420 do {
5f70619d 421 dso->binary_type = binary_type_data[i++];
cdd059d7 422
c27697d6
AH
423 dso->data.fd = open_dso(dso, machine);
424 if (dso->data.fd >= 0)
425 goto out;
cdd059d7 426
5f70619d 427 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
c27697d6
AH
428out:
429 if (dso->data.fd >= 0)
430 dso->data.status = DSO_DATA_STATUS_OK;
431 else
432 dso->data.status = DSO_DATA_STATUS_ERROR;
cdd059d7 433
c27697d6 434 return dso->data.fd;
cdd059d7
JO
435}
436
288be943
AH
437bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
438{
439 u32 flag = 1 << by;
440
441 if (dso->data.status_seen & flag)
442 return true;
443
444 dso->data.status_seen |= flag;
445
446 return false;
447}
448
cdd059d7
JO
449static void
450dso_cache__free(struct rb_root *root)
451{
452 struct rb_node *next = rb_first(root);
453
454 while (next) {
455 struct dso_cache *cache;
456
457 cache = rb_entry(next, struct dso_cache, rb_node);
458 next = rb_next(&cache->rb_node);
459 rb_erase(&cache->rb_node, root);
460 free(cache);
461 }
462}
463
3344996e 464static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
cdd059d7 465{
3344996e
ACM
466 struct rb_node * const *p = &root->rb_node;
467 const struct rb_node *parent = NULL;
cdd059d7
JO
468 struct dso_cache *cache;
469
470 while (*p != NULL) {
471 u64 end;
472
473 parent = *p;
474 cache = rb_entry(parent, struct dso_cache, rb_node);
475 end = cache->offset + DSO__DATA_CACHE_SIZE;
476
477 if (offset < cache->offset)
478 p = &(*p)->rb_left;
479 else if (offset >= end)
480 p = &(*p)->rb_right;
481 else
482 return cache;
483 }
484 return NULL;
485}
486
487static void
488dso_cache__insert(struct rb_root *root, struct dso_cache *new)
489{
490 struct rb_node **p = &root->rb_node;
491 struct rb_node *parent = NULL;
492 struct dso_cache *cache;
493 u64 offset = new->offset;
494
495 while (*p != NULL) {
496 u64 end;
497
498 parent = *p;
499 cache = rb_entry(parent, struct dso_cache, rb_node);
500 end = cache->offset + DSO__DATA_CACHE_SIZE;
501
502 if (offset < cache->offset)
503 p = &(*p)->rb_left;
504 else if (offset >= end)
505 p = &(*p)->rb_right;
506 }
507
508 rb_link_node(&new->rb_node, parent, p);
509 rb_insert_color(&new->rb_node, root);
510}
511
512static ssize_t
513dso_cache__memcpy(struct dso_cache *cache, u64 offset,
514 u8 *data, u64 size)
515{
516 u64 cache_offset = offset - cache->offset;
517 u64 cache_size = min(cache->size - cache_offset, size);
518
519 memcpy(data, cache->data + cache_offset, cache_size);
520 return cache_size;
521}
522
523static ssize_t
c3fbd2a6 524dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
cdd059d7
JO
525{
526 struct dso_cache *cache;
527 ssize_t ret;
cdd059d7
JO
528
529 do {
530 u64 cache_offset;
531
532 ret = -ENOMEM;
533
534 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
535 if (!cache)
536 break;
537
538 cache_offset = offset & DSO__DATA_CACHE_MASK;
539 ret = -EINVAL;
540
c3fbd2a6 541 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
cdd059d7
JO
542 break;
543
c3fbd2a6 544 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
cdd059d7
JO
545 if (ret <= 0)
546 break;
547
548 cache->offset = cache_offset;
549 cache->size = ret;
ca40e2af 550 dso_cache__insert(&dso->data.cache, cache);
cdd059d7
JO
551
552 ret = dso_cache__memcpy(cache, offset, data, size);
553
554 } while (0);
555
556 if (ret <= 0)
557 free(cache);
558
cdd059d7
JO
559 return ret;
560}
561
c3fbd2a6
JO
562static ssize_t dso_cache_read(struct dso *dso, u64 offset,
563 u8 *data, ssize_t size)
cdd059d7
JO
564{
565 struct dso_cache *cache;
566
ca40e2af 567 cache = dso_cache__find(&dso->data.cache, offset);
cdd059d7
JO
568 if (cache)
569 return dso_cache__memcpy(cache, offset, data, size);
570 else
c3fbd2a6 571 return dso_cache__read(dso, offset, data, size);
cdd059d7
JO
572}
573
c1f9aa0a
JO
574/*
575 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
576 * in the rb_tree. Any read to already cached data is served
577 * by cached data.
578 */
c3fbd2a6 579static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
cdd059d7
JO
580{
581 ssize_t r = 0;
582 u8 *p = data;
583
584 do {
585 ssize_t ret;
586
c3fbd2a6 587 ret = dso_cache_read(dso, offset, p, size);
cdd059d7
JO
588 if (ret < 0)
589 return ret;
590
591 /* Reached EOF, return what we have. */
592 if (!ret)
593 break;
594
595 BUG_ON(ret > size);
596
597 r += ret;
598 p += ret;
599 offset += ret;
600 size -= ret;
601
602 } while (size);
603
604 return r;
605}
606
c3fbd2a6
JO
607static int data_file_size(struct dso *dso)
608{
609 struct stat st;
6e81c74c 610 char sbuf[STRERR_BUFSIZE];
c3fbd2a6
JO
611
612 if (!dso->data.file_size) {
613 if (fstat(dso->data.fd, &st)) {
6e81c74c
MH
614 pr_err("dso mmap failed, fstat: %s\n",
615 strerror_r(errno, sbuf, sizeof(sbuf)));
c3fbd2a6
JO
616 return -1;
617 }
618 dso->data.file_size = st.st_size;
619 }
620
621 return 0;
622}
623
6d363459
AH
624/**
625 * dso__data_size - Return dso data size
626 * @dso: dso object
627 * @machine: machine object
628 *
629 * Return: dso data size
630 */
631off_t dso__data_size(struct dso *dso, struct machine *machine)
632{
633 int fd;
634
635 fd = dso__data_fd(dso, machine);
636 if (fd < 0)
637 return fd;
638
639 if (data_file_size(dso))
640 return -1;
641
642 /* For now just estimate dso data size is close to file size */
643 return dso->data.file_size;
644}
645
c3fbd2a6
JO
646static ssize_t data_read_offset(struct dso *dso, u64 offset,
647 u8 *data, ssize_t size)
648{
649 if (data_file_size(dso))
650 return -1;
651
652 /* Check the offset sanity. */
653 if (offset > dso->data.file_size)
654 return -1;
655
656 if (offset + size < offset)
657 return -1;
658
659 return cached_read(dso, offset, data, size);
660}
661
c1f9aa0a
JO
662/**
663 * dso__data_read_offset - Read data from dso file offset
664 * @dso: dso object
665 * @machine: machine object
666 * @offset: file offset
667 * @data: buffer to store data
668 * @size: size of the @data buffer
669 *
670 * External interface to read data from dso file offset. Open
671 * dso data file and use cached_read to get the data.
672 */
c3fbd2a6
JO
673ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
674 u64 offset, u8 *data, ssize_t size)
675{
676 if (dso__data_fd(dso, machine) < 0)
677 return -1;
678
679 return data_read_offset(dso, offset, data, size);
680}
681
c1f9aa0a
JO
682/**
683 * dso__data_read_addr - Read data from dso address
684 * @dso: dso object
685 * @machine: machine object
686 * @add: virtual memory address
687 * @data: buffer to store data
688 * @size: size of the @data buffer
689 *
690 * External interface to read data from dso address.
691 */
cdd059d7
JO
692ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
693 struct machine *machine, u64 addr,
694 u8 *data, ssize_t size)
695{
696 u64 offset = map->map_ip(map, addr);
697 return dso__data_read_offset(dso, machine, offset, data, size);
698}
699
700struct map *dso__new_map(const char *name)
701{
702 struct map *map = NULL;
703 struct dso *dso = dso__new(name);
704
705 if (dso)
706 map = map__new2(0, dso, MAP__FUNCTION);
707
708 return map;
709}
710
711struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
712 const char *short_name, int dso_type)
713{
714 /*
715 * The kernel dso could be created by build_id processing.
716 */
717 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
718
719 /*
720 * We need to run this in all cases, since during the build_id
721 * processing we had no idea this was the kernel dso.
722 */
723 if (dso != NULL) {
58a98c9c 724 dso__set_short_name(dso, short_name, false);
cdd059d7
JO
725 dso->kernel = dso_type;
726 }
727
728 return dso;
729}
730
4598a0a6
WL
731/*
732 * Find a matching entry and/or link current entry to RB tree.
733 * Either one of the dso or name parameter must be non-NULL or the
734 * function will not work.
735 */
736static struct dso *dso__findlink_by_longname(struct rb_root *root,
737 struct dso *dso, const char *name)
738{
739 struct rb_node **p = &root->rb_node;
740 struct rb_node *parent = NULL;
741
742 if (!name)
743 name = dso->long_name;
744 /*
745 * Find node with the matching name
746 */
747 while (*p) {
748 struct dso *this = rb_entry(*p, struct dso, rb_node);
749 int rc = strcmp(name, this->long_name);
750
751 parent = *p;
752 if (rc == 0) {
753 /*
754 * In case the new DSO is a duplicate of an existing
755 * one, print an one-time warning & put the new entry
756 * at the end of the list of duplicates.
757 */
758 if (!dso || (dso == this))
759 return this; /* Find matching dso */
760 /*
761 * The core kernel DSOs may have duplicated long name.
762 * In this case, the short name should be different.
763 * Comparing the short names to differentiate the DSOs.
764 */
765 rc = strcmp(dso->short_name, this->short_name);
766 if (rc == 0) {
767 pr_err("Duplicated dso name: %s\n", name);
768 return NULL;
769 }
770 }
771 if (rc < 0)
772 p = &parent->rb_left;
773 else
774 p = &parent->rb_right;
775 }
776 if (dso) {
777 /* Add new node and rebalance tree */
778 rb_link_node(&dso->rb_node, parent, p);
779 rb_insert_color(&dso->rb_node, root);
780 }
781 return NULL;
782}
783
784static inline struct dso *
785dso__find_by_longname(const struct rb_root *root, const char *name)
786{
787 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
788}
789
bf4414ae 790void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
cdd059d7
JO
791{
792 if (name == NULL)
793 return;
7e155d4d
ACM
794
795 if (dso->long_name_allocated)
bf4414ae 796 free((char *)dso->long_name);
7e155d4d
ACM
797
798 dso->long_name = name;
799 dso->long_name_len = strlen(name);
800 dso->long_name_allocated = name_allocated;
cdd059d7
JO
801}
802
58a98c9c 803void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
cdd059d7
JO
804{
805 if (name == NULL)
806 return;
58a98c9c
AH
807
808 if (dso->short_name_allocated)
809 free((char *)dso->short_name);
810
811 dso->short_name = name;
812 dso->short_name_len = strlen(name);
813 dso->short_name_allocated = name_allocated;
cdd059d7
JO
814}
815
816static void dso__set_basename(struct dso *dso)
817{
ac5e7f84
SE
818 /*
819 * basename() may modify path buffer, so we must pass
820 * a copy.
821 */
822 char *base, *lname = strdup(dso->long_name);
823
824 if (!lname)
825 return;
826
827 /*
828 * basename() may return a pointer to internal
829 * storage which is reused in subsequent calls
830 * so copy the result.
831 */
832 base = strdup(basename(lname));
833
834 free(lname);
835
836 if (!base)
837 return;
838
839 dso__set_short_name(dso, base, true);
cdd059d7
JO
840}
841
842int dso__name_len(const struct dso *dso)
843{
844 if (!dso)
845 return strlen("[unknown]");
846 if (verbose)
847 return dso->long_name_len;
848
849 return dso->short_name_len;
850}
851
852bool dso__loaded(const struct dso *dso, enum map_type type)
853{
854 return dso->loaded & (1 << type);
855}
856
857bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
858{
859 return dso->sorted_by_name & (1 << type);
860}
861
862void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
863{
864 dso->sorted_by_name |= (1 << type);
865}
866
867struct dso *dso__new(const char *name)
868{
869 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
870
871 if (dso != NULL) {
872 int i;
873 strcpy(dso->name, name);
7e155d4d 874 dso__set_long_name(dso, dso->name, false);
58a98c9c 875 dso__set_short_name(dso, dso->name, false);
cdd059d7
JO
876 for (i = 0; i < MAP__NR_TYPES; ++i)
877 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
ca40e2af 878 dso->data.cache = RB_ROOT;
53fa8eaa 879 dso->data.fd = -1;
c27697d6 880 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
cdd059d7 881 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
5f70619d 882 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
c6d8f2a4 883 dso->is_64_bit = (sizeof(void *) == 8);
cdd059d7 884 dso->loaded = 0;
0131c4ec 885 dso->rel = 0;
cdd059d7
JO
886 dso->sorted_by_name = 0;
887 dso->has_build_id = 0;
2cc9d0ef 888 dso->has_srcline = 1;
906049c8 889 dso->a2l_fails = 1;
cdd059d7
JO
890 dso->kernel = DSO_TYPE_USER;
891 dso->needs_swap = DSO_SWAP__UNSET;
4598a0a6 892 RB_CLEAR_NODE(&dso->rb_node);
cdd059d7 893 INIT_LIST_HEAD(&dso->node);
eba5102d 894 INIT_LIST_HEAD(&dso->data.open_entry);
cdd059d7
JO
895 }
896
897 return dso;
898}
899
900void dso__delete(struct dso *dso)
901{
902 int i;
4598a0a6
WL
903
904 if (!RB_EMPTY_NODE(&dso->rb_node))
905 pr_err("DSO %s is still in rbtree when being deleted!\n",
906 dso->long_name);
cdd059d7
JO
907 for (i = 0; i < MAP__NR_TYPES; ++i)
908 symbols__delete(&dso->symbols[i]);
ee021d42
ACM
909
910 if (dso->short_name_allocated) {
04662523 911 zfree((char **)&dso->short_name);
ee021d42
ACM
912 dso->short_name_allocated = false;
913 }
914
915 if (dso->long_name_allocated) {
04662523 916 zfree((char **)&dso->long_name);
ee021d42
ACM
917 dso->long_name_allocated = false;
918 }
919
53fa8eaa 920 dso__data_close(dso);
ca40e2af 921 dso_cache__free(&dso->data.cache);
454ff00f 922 dso__free_a2l(dso);
04662523 923 zfree(&dso->symsrc_filename);
cdd059d7
JO
924 free(dso);
925}
926
927void dso__set_build_id(struct dso *dso, void *build_id)
928{
929 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
930 dso->has_build_id = 1;
931}
932
933bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
934{
935 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
936}
937
938void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
939{
940 char path[PATH_MAX];
941
942 if (machine__is_default_guest(machine))
943 return;
944 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
945 if (sysfs__read_build_id(path, dso->build_id,
946 sizeof(dso->build_id)) == 0)
947 dso->has_build_id = true;
948}
949
950int dso__kernel_module_get_build_id(struct dso *dso,
951 const char *root_dir)
952{
953 char filename[PATH_MAX];
954 /*
955 * kernel module short names are of the form "[module]" and
956 * we need just "module" here.
957 */
958 const char *name = dso->short_name + 1;
959
960 snprintf(filename, sizeof(filename),
961 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
962 root_dir, (int)strlen(name) - 1, name);
963
964 if (sysfs__read_build_id(filename, dso->build_id,
965 sizeof(dso->build_id)) == 0)
966 dso->has_build_id = true;
967
968 return 0;
969}
970
971bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
972{
973 bool have_build_id = false;
974 struct dso *pos;
975
976 list_for_each_entry(pos, head, node) {
977 if (with_hits && !pos->hit)
978 continue;
979 if (pos->has_build_id) {
980 have_build_id = true;
981 continue;
982 }
983 if (filename__read_build_id(pos->long_name, pos->build_id,
984 sizeof(pos->build_id)) > 0) {
985 have_build_id = true;
986 pos->has_build_id = true;
987 }
988 }
989
990 return have_build_id;
991}
992
8fa7d87f 993void dsos__add(struct dsos *dsos, struct dso *dso)
cdd059d7 994{
8fa7d87f 995 list_add_tail(&dso->node, &dsos->head);
4598a0a6 996 dso__findlink_by_longname(&dsos->root, dso, NULL);
cdd059d7
JO
997}
998
8fa7d87f
WL
999struct dso *dsos__find(const struct dsos *dsos, const char *name,
1000 bool cmp_short)
cdd059d7
JO
1001{
1002 struct dso *pos;
1003
f9ceffb6 1004 if (cmp_short) {
8fa7d87f 1005 list_for_each_entry(pos, &dsos->head, node)
f9ceffb6
WL
1006 if (strcmp(pos->short_name, name) == 0)
1007 return pos;
1008 return NULL;
1009 }
4598a0a6 1010 return dso__find_by_longname(&dsos->root, name);
cdd059d7
JO
1011}
1012
8fa7d87f 1013struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
cdd059d7 1014{
8fa7d87f 1015 struct dso *dso = dsos__find(dsos, name, false);
cdd059d7
JO
1016
1017 if (!dso) {
1018 dso = dso__new(name);
1019 if (dso != NULL) {
8fa7d87f 1020 dsos__add(dsos, dso);
cdd059d7
JO
1021 dso__set_basename(dso);
1022 }
1023 }
1024
1025 return dso;
1026}
1027
1028size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
417c2ff6 1029 bool (skip)(struct dso *dso, int parm), int parm)
cdd059d7
JO
1030{
1031 struct dso *pos;
1032 size_t ret = 0;
1033
1034 list_for_each_entry(pos, head, node) {
417c2ff6 1035 if (skip && skip(pos, parm))
cdd059d7
JO
1036 continue;
1037 ret += dso__fprintf_buildid(pos, fp);
1038 ret += fprintf(fp, " %s\n", pos->long_name);
1039 }
1040 return ret;
1041}
1042
1043size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1044{
1045 struct dso *pos;
1046 size_t ret = 0;
1047
1048 list_for_each_entry(pos, head, node) {
1049 int i;
1050 for (i = 0; i < MAP__NR_TYPES; ++i)
1051 ret += dso__fprintf(pos, i, fp);
1052 }
1053
1054 return ret;
1055}
1056
1057size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1058{
1059 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1060
1061 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1062 return fprintf(fp, "%s", sbuild_id);
1063}
1064
1065size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1066{
1067 struct rb_node *nd;
1068 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1069
1070 if (dso->short_name != dso->long_name)
1071 ret += fprintf(fp, "%s, ", dso->long_name);
1072 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
919d590f 1073 dso__loaded(dso, type) ? "" : "NOT ");
cdd059d7
JO
1074 ret += dso__fprintf_buildid(dso, fp);
1075 ret += fprintf(fp, ")\n");
1076 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1077 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1078 ret += symbol__fprintf(pos, fp);
1079 }
1080
1081 return ret;
1082}
2b5b8bb2
AH
1083
1084enum dso_type dso__type(struct dso *dso, struct machine *machine)
1085{
1086 int fd;
1087
1088 fd = dso__data_fd(dso, machine);
1089 if (fd < 0)
1090 return DSO__TYPE_UNKNOWN;
1091
1092 return dso__type_fd(fd);
1093}