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