5d7c7bcc6276c297316772d1d85646c958b51994
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / dso.c
1 #include "symbol.h"
2 #include "dso.h"
3 #include "machine.h"
4 #include "util.h"
5 #include "debug.h"
6
7 char dso__symtab_origin(const struct dso *dso)
8 {
9         static const char origin[] = {
10                 [DSO_BINARY_TYPE__KALLSYMS]                     = 'k',
11                 [DSO_BINARY_TYPE__VMLINUX]                      = 'v',
12                 [DSO_BINARY_TYPE__JAVA_JIT]                     = 'j',
13                 [DSO_BINARY_TYPE__DEBUGLINK]                    = 'l',
14                 [DSO_BINARY_TYPE__BUILD_ID_CACHE]               = 'B',
15                 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO]             = 'f',
16                 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO]             = 'u',
17                 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO]       = 'o',
18                 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO]            = 'b',
19                 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO]              = 'd',
20                 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE]          = 'K',
21                 [DSO_BINARY_TYPE__GUEST_KALLSYMS]               = 'g',
22                 [DSO_BINARY_TYPE__GUEST_KMODULE]                = 'G',
23                 [DSO_BINARY_TYPE__GUEST_VMLINUX]                = 'V',
24         };
25
26         if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
27                 return '!';
28         return origin[dso->symtab_type];
29 }
30
31 int dso__read_binary_type_filename(const struct dso *dso,
32                                    enum dso_binary_type type,
33                                    char *root_dir, char *filename, size_t size)
34 {
35         char build_id_hex[BUILD_ID_SIZE * 2 + 1];
36         int ret = 0;
37
38         switch (type) {
39         case DSO_BINARY_TYPE__DEBUGLINK: {
40                 char *debuglink;
41
42                 strncpy(filename, dso->long_name, size);
43                 debuglink = filename + dso->long_name_len;
44                 while (debuglink != filename && *debuglink != '/')
45                         debuglink--;
46                 if (*debuglink == '/')
47                         debuglink++;
48                 ret = filename__read_debuglink(dso->long_name, debuglink,
49                                                size - (debuglink - filename));
50                 }
51                 break;
52         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
53                 /* skip the locally configured cache if a symfs is given */
54                 if (symbol_conf.symfs[0] ||
55                     (dso__build_id_filename(dso, filename, size) == NULL))
56                         ret = -1;
57                 break;
58
59         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
60                 snprintf(filename, size, "%s/usr/lib/debug%s.debug",
61                          symbol_conf.symfs, dso->long_name);
62                 break;
63
64         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
65                 snprintf(filename, size, "%s/usr/lib/debug%s",
66                          symbol_conf.symfs, dso->long_name);
67                 break;
68
69         case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
70         {
71                 const char *last_slash;
72                 size_t len;
73                 size_t dir_size;
74
75                 last_slash = dso->long_name + dso->long_name_len;
76                 while (last_slash != dso->long_name && *last_slash != '/')
77                         last_slash--;
78
79                 len = scnprintf(filename, size, "%s", symbol_conf.symfs);
80                 dir_size = last_slash - dso->long_name + 2;
81                 if (dir_size > (size - len)) {
82                         ret = -1;
83                         break;
84                 }
85                 len += scnprintf(filename + len, dir_size, "%s",  dso->long_name);
86                 len += scnprintf(filename + len , size - len, ".debug%s",
87                                                                 last_slash);
88                 break;
89         }
90
91         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
92                 if (!dso->has_build_id) {
93                         ret = -1;
94                         break;
95                 }
96
97                 build_id__sprintf(dso->build_id,
98                                   sizeof(dso->build_id),
99                                   build_id_hex);
100                 snprintf(filename, size,
101                          "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
102                          symbol_conf.symfs, build_id_hex, build_id_hex + 2);
103                 break;
104
105         case DSO_BINARY_TYPE__VMLINUX:
106         case DSO_BINARY_TYPE__GUEST_VMLINUX:
107         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
108                 snprintf(filename, size, "%s%s",
109                          symbol_conf.symfs, dso->long_name);
110                 break;
111
112         case DSO_BINARY_TYPE__GUEST_KMODULE:
113                 snprintf(filename, size, "%s%s%s", symbol_conf.symfs,
114                          root_dir, dso->long_name);
115                 break;
116
117         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
118                 snprintf(filename, size, "%s%s", symbol_conf.symfs,
119                          dso->long_name);
120                 break;
121
122         case DSO_BINARY_TYPE__KCORE:
123         case DSO_BINARY_TYPE__GUEST_KCORE:
124                 snprintf(filename, size, "%s", dso->long_name);
125                 break;
126
127         default:
128         case DSO_BINARY_TYPE__KALLSYMS:
129         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
130         case DSO_BINARY_TYPE__JAVA_JIT:
131         case DSO_BINARY_TYPE__NOT_FOUND:
132                 ret = -1;
133                 break;
134         }
135
136         return ret;
137 }
138
139 /*
140  * Global list of open DSOs.
141  */
142 static LIST_HEAD(dso__data_open);
143
144 static void dso__list_add(struct dso *dso)
145 {
146         list_add_tail(&dso->data.open_entry, &dso__data_open);
147 }
148
149 static void dso__list_del(struct dso *dso)
150 {
151         list_del(&dso->data.open_entry);
152 }
153
154 static int __open_dso(struct dso *dso, struct machine *machine)
155 {
156         int fd;
157         char *root_dir = (char *)"";
158         char *name = malloc(PATH_MAX);
159
160         if (!name)
161                 return -ENOMEM;
162
163         if (machine)
164                 root_dir = machine->root_dir;
165
166         if (dso__read_binary_type_filename(dso, dso->binary_type,
167                                             root_dir, name, PATH_MAX)) {
168                 free(name);
169                 return -EINVAL;
170         }
171
172         fd = open(name, O_RDONLY);
173         free(name);
174         return fd;
175 }
176
177 static int open_dso(struct dso *dso, struct machine *machine)
178 {
179         int fd = __open_dso(dso, machine);
180
181         if (fd > 0)
182                 dso__list_add(dso);
183
184         return fd;
185 }
186
187 static void close_data_fd(struct dso *dso)
188 {
189         if (dso->data.fd >= 0) {
190                 close(dso->data.fd);
191                 dso->data.fd = -1;
192                 dso__list_del(dso);
193         }
194 }
195
196 static void close_dso(struct dso *dso)
197 {
198         close_data_fd(dso);
199 }
200
201 void dso__data_close(struct dso *dso)
202 {
203         close_dso(dso);
204 }
205
206 int dso__data_fd(struct dso *dso, struct machine *machine)
207 {
208         enum dso_binary_type binary_type_data[] = {
209                 DSO_BINARY_TYPE__BUILD_ID_CACHE,
210                 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
211                 DSO_BINARY_TYPE__NOT_FOUND,
212         };
213         int i = 0;
214
215         if (dso->data.fd >= 0)
216                 return dso->data.fd;
217
218         if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
219                 dso->data.fd = open_dso(dso, machine);
220                 return dso->data.fd;
221         }
222
223         do {
224                 int fd;
225
226                 dso->binary_type = binary_type_data[i++];
227
228                 fd = open_dso(dso, machine);
229                 if (fd >= 0)
230                         return dso->data.fd = fd;
231
232         } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
233
234         return -EINVAL;
235 }
236
237 static void
238 dso_cache__free(struct rb_root *root)
239 {
240         struct rb_node *next = rb_first(root);
241
242         while (next) {
243                 struct dso_cache *cache;
244
245                 cache = rb_entry(next, struct dso_cache, rb_node);
246                 next = rb_next(&cache->rb_node);
247                 rb_erase(&cache->rb_node, root);
248                 free(cache);
249         }
250 }
251
252 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
253 {
254         struct rb_node * const *p = &root->rb_node;
255         const struct rb_node *parent = NULL;
256         struct dso_cache *cache;
257
258         while (*p != NULL) {
259                 u64 end;
260
261                 parent = *p;
262                 cache = rb_entry(parent, struct dso_cache, rb_node);
263                 end = cache->offset + DSO__DATA_CACHE_SIZE;
264
265                 if (offset < cache->offset)
266                         p = &(*p)->rb_left;
267                 else if (offset >= end)
268                         p = &(*p)->rb_right;
269                 else
270                         return cache;
271         }
272         return NULL;
273 }
274
275 static void
276 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
277 {
278         struct rb_node **p = &root->rb_node;
279         struct rb_node *parent = NULL;
280         struct dso_cache *cache;
281         u64 offset = new->offset;
282
283         while (*p != NULL) {
284                 u64 end;
285
286                 parent = *p;
287                 cache = rb_entry(parent, struct dso_cache, rb_node);
288                 end = cache->offset + DSO__DATA_CACHE_SIZE;
289
290                 if (offset < cache->offset)
291                         p = &(*p)->rb_left;
292                 else if (offset >= end)
293                         p = &(*p)->rb_right;
294         }
295
296         rb_link_node(&new->rb_node, parent, p);
297         rb_insert_color(&new->rb_node, root);
298 }
299
300 static ssize_t
301 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
302                   u8 *data, u64 size)
303 {
304         u64 cache_offset = offset - cache->offset;
305         u64 cache_size   = min(cache->size - cache_offset, size);
306
307         memcpy(data, cache->data + cache_offset, cache_size);
308         return cache_size;
309 }
310
311 static ssize_t
312 dso_cache__read(struct dso *dso, struct machine *machine,
313                  u64 offset, u8 *data, ssize_t size)
314 {
315         struct dso_cache *cache;
316         ssize_t ret;
317         int fd;
318
319         fd = dso__data_fd(dso, machine);
320         if (fd < 0)
321                 return -1;
322
323         do {
324                 u64 cache_offset;
325
326                 ret = -ENOMEM;
327
328                 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
329                 if (!cache)
330                         break;
331
332                 cache_offset = offset & DSO__DATA_CACHE_MASK;
333                 ret = -EINVAL;
334
335                 if (-1 == lseek(fd, cache_offset, SEEK_SET))
336                         break;
337
338                 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
339                 if (ret <= 0)
340                         break;
341
342                 cache->offset = cache_offset;
343                 cache->size   = ret;
344                 dso_cache__insert(&dso->data.cache, cache);
345
346                 ret = dso_cache__memcpy(cache, offset, data, size);
347
348         } while (0);
349
350         if (ret <= 0)
351                 free(cache);
352
353         dso__data_close(dso);
354         return ret;
355 }
356
357 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
358                               u64 offset, u8 *data, ssize_t size)
359 {
360         struct dso_cache *cache;
361
362         cache = dso_cache__find(&dso->data.cache, offset);
363         if (cache)
364                 return dso_cache__memcpy(cache, offset, data, size);
365         else
366                 return dso_cache__read(dso, machine, offset, data, size);
367 }
368
369 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
370                               u64 offset, u8 *data, ssize_t size)
371 {
372         ssize_t r = 0;
373         u8 *p = data;
374
375         do {
376                 ssize_t ret;
377
378                 ret = dso_cache_read(dso, machine, offset, p, size);
379                 if (ret < 0)
380                         return ret;
381
382                 /* Reached EOF, return what we have. */
383                 if (!ret)
384                         break;
385
386                 BUG_ON(ret > size);
387
388                 r      += ret;
389                 p      += ret;
390                 offset += ret;
391                 size   -= ret;
392
393         } while (size);
394
395         return r;
396 }
397
398 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
399                             struct machine *machine, u64 addr,
400                             u8 *data, ssize_t size)
401 {
402         u64 offset = map->map_ip(map, addr);
403         return dso__data_read_offset(dso, machine, offset, data, size);
404 }
405
406 struct map *dso__new_map(const char *name)
407 {
408         struct map *map = NULL;
409         struct dso *dso = dso__new(name);
410
411         if (dso)
412                 map = map__new2(0, dso, MAP__FUNCTION);
413
414         return map;
415 }
416
417 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
418                     const char *short_name, int dso_type)
419 {
420         /*
421          * The kernel dso could be created by build_id processing.
422          */
423         struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
424
425         /*
426          * We need to run this in all cases, since during the build_id
427          * processing we had no idea this was the kernel dso.
428          */
429         if (dso != NULL) {
430                 dso__set_short_name(dso, short_name, false);
431                 dso->kernel = dso_type;
432         }
433
434         return dso;
435 }
436
437 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
438 {
439         if (name == NULL)
440                 return;
441
442         if (dso->long_name_allocated)
443                 free((char *)dso->long_name);
444
445         dso->long_name           = name;
446         dso->long_name_len       = strlen(name);
447         dso->long_name_allocated = name_allocated;
448 }
449
450 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
451 {
452         if (name == NULL)
453                 return;
454
455         if (dso->short_name_allocated)
456                 free((char *)dso->short_name);
457
458         dso->short_name           = name;
459         dso->short_name_len       = strlen(name);
460         dso->short_name_allocated = name_allocated;
461 }
462
463 static void dso__set_basename(struct dso *dso)
464 {
465        /*
466         * basename() may modify path buffer, so we must pass
467         * a copy.
468         */
469        char *base, *lname = strdup(dso->long_name);
470
471        if (!lname)
472                return;
473
474        /*
475         * basename() may return a pointer to internal
476         * storage which is reused in subsequent calls
477         * so copy the result.
478         */
479        base = strdup(basename(lname));
480
481        free(lname);
482
483        if (!base)
484                return;
485
486        dso__set_short_name(dso, base, true);
487 }
488
489 int dso__name_len(const struct dso *dso)
490 {
491         if (!dso)
492                 return strlen("[unknown]");
493         if (verbose)
494                 return dso->long_name_len;
495
496         return dso->short_name_len;
497 }
498
499 bool dso__loaded(const struct dso *dso, enum map_type type)
500 {
501         return dso->loaded & (1 << type);
502 }
503
504 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
505 {
506         return dso->sorted_by_name & (1 << type);
507 }
508
509 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
510 {
511         dso->sorted_by_name |= (1 << type);
512 }
513
514 struct dso *dso__new(const char *name)
515 {
516         struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
517
518         if (dso != NULL) {
519                 int i;
520                 strcpy(dso->name, name);
521                 dso__set_long_name(dso, dso->name, false);
522                 dso__set_short_name(dso, dso->name, false);
523                 for (i = 0; i < MAP__NR_TYPES; ++i)
524                         dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
525                 dso->data.cache = RB_ROOT;
526                 dso->data.fd = -1;
527                 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
528                 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
529                 dso->loaded = 0;
530                 dso->rel = 0;
531                 dso->sorted_by_name = 0;
532                 dso->has_build_id = 0;
533                 dso->has_srcline = 1;
534                 dso->a2l_fails = 1;
535                 dso->kernel = DSO_TYPE_USER;
536                 dso->needs_swap = DSO_SWAP__UNSET;
537                 INIT_LIST_HEAD(&dso->node);
538                 INIT_LIST_HEAD(&dso->data.open_entry);
539         }
540
541         return dso;
542 }
543
544 void dso__delete(struct dso *dso)
545 {
546         int i;
547         for (i = 0; i < MAP__NR_TYPES; ++i)
548                 symbols__delete(&dso->symbols[i]);
549
550         if (dso->short_name_allocated) {
551                 zfree((char **)&dso->short_name);
552                 dso->short_name_allocated = false;
553         }
554
555         if (dso->long_name_allocated) {
556                 zfree((char **)&dso->long_name);
557                 dso->long_name_allocated = false;
558         }
559
560         dso__data_close(dso);
561         dso_cache__free(&dso->data.cache);
562         dso__free_a2l(dso);
563         zfree(&dso->symsrc_filename);
564         free(dso);
565 }
566
567 void dso__set_build_id(struct dso *dso, void *build_id)
568 {
569         memcpy(dso->build_id, build_id, sizeof(dso->build_id));
570         dso->has_build_id = 1;
571 }
572
573 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
574 {
575         return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
576 }
577
578 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
579 {
580         char path[PATH_MAX];
581
582         if (machine__is_default_guest(machine))
583                 return;
584         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
585         if (sysfs__read_build_id(path, dso->build_id,
586                                  sizeof(dso->build_id)) == 0)
587                 dso->has_build_id = true;
588 }
589
590 int dso__kernel_module_get_build_id(struct dso *dso,
591                                     const char *root_dir)
592 {
593         char filename[PATH_MAX];
594         /*
595          * kernel module short names are of the form "[module]" and
596          * we need just "module" here.
597          */
598         const char *name = dso->short_name + 1;
599
600         snprintf(filename, sizeof(filename),
601                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
602                  root_dir, (int)strlen(name) - 1, name);
603
604         if (sysfs__read_build_id(filename, dso->build_id,
605                                  sizeof(dso->build_id)) == 0)
606                 dso->has_build_id = true;
607
608         return 0;
609 }
610
611 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
612 {
613         bool have_build_id = false;
614         struct dso *pos;
615
616         list_for_each_entry(pos, head, node) {
617                 if (with_hits && !pos->hit)
618                         continue;
619                 if (pos->has_build_id) {
620                         have_build_id = true;
621                         continue;
622                 }
623                 if (filename__read_build_id(pos->long_name, pos->build_id,
624                                             sizeof(pos->build_id)) > 0) {
625                         have_build_id     = true;
626                         pos->has_build_id = true;
627                 }
628         }
629
630         return have_build_id;
631 }
632
633 void dsos__add(struct list_head *head, struct dso *dso)
634 {
635         list_add_tail(&dso->node, head);
636 }
637
638 struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
639 {
640         struct dso *pos;
641
642         if (cmp_short) {
643                 list_for_each_entry(pos, head, node)
644                         if (strcmp(pos->short_name, name) == 0)
645                                 return pos;
646                 return NULL;
647         }
648         list_for_each_entry(pos, head, node)
649                 if (strcmp(pos->long_name, name) == 0)
650                         return pos;
651         return NULL;
652 }
653
654 struct dso *__dsos__findnew(struct list_head *head, const char *name)
655 {
656         struct dso *dso = dsos__find(head, name, false);
657
658         if (!dso) {
659                 dso = dso__new(name);
660                 if (dso != NULL) {
661                         dsos__add(head, dso);
662                         dso__set_basename(dso);
663                 }
664         }
665
666         return dso;
667 }
668
669 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
670                                bool (skip)(struct dso *dso, int parm), int parm)
671 {
672         struct dso *pos;
673         size_t ret = 0;
674
675         list_for_each_entry(pos, head, node) {
676                 if (skip && skip(pos, parm))
677                         continue;
678                 ret += dso__fprintf_buildid(pos, fp);
679                 ret += fprintf(fp, " %s\n", pos->long_name);
680         }
681         return ret;
682 }
683
684 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
685 {
686         struct dso *pos;
687         size_t ret = 0;
688
689         list_for_each_entry(pos, head, node) {
690                 int i;
691                 for (i = 0; i < MAP__NR_TYPES; ++i)
692                         ret += dso__fprintf(pos, i, fp);
693         }
694
695         return ret;
696 }
697
698 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
699 {
700         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
701
702         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
703         return fprintf(fp, "%s", sbuild_id);
704 }
705
706 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
707 {
708         struct rb_node *nd;
709         size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
710
711         if (dso->short_name != dso->long_name)
712                 ret += fprintf(fp, "%s, ", dso->long_name);
713         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
714                        dso__loaded(dso, type) ? "" : "NOT ");
715         ret += dso__fprintf_buildid(dso, fp);
716         ret += fprintf(fp, ")\n");
717         for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
718                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
719                 ret += symbol__fprintf(pos, fp);
720         }
721
722         return ret;
723 }