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