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