perf tools: Add data_fd into dso object
[cascardo/linux.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 static int open_dso(struct dso *dso, struct machine *machine)
140 {
141         int fd;
142         char *root_dir = (char *)"";
143         char *name = malloc(PATH_MAX);
144
145         if (!name)
146                 return -ENOMEM;
147
148         if (machine)
149                 root_dir = machine->root_dir;
150
151         if (dso__read_binary_type_filename(dso, dso->binary_type,
152                                             root_dir, name, PATH_MAX)) {
153                 free(name);
154                 return -EINVAL;
155         }
156
157         fd = open(name, O_RDONLY);
158         free(name);
159         return fd;
160 }
161
162 void dso__data_close(struct dso *dso)
163 {
164         if (dso->data.fd >= 0) {
165                 close(dso->data.fd);
166                 dso->data.fd = -1;
167         }
168 }
169
170 int dso__data_fd(struct dso *dso, struct machine *machine)
171 {
172         enum dso_binary_type binary_type_data[] = {
173                 DSO_BINARY_TYPE__BUILD_ID_CACHE,
174                 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
175                 DSO_BINARY_TYPE__NOT_FOUND,
176         };
177         int i = 0;
178
179         if (dso->data.fd >= 0)
180                 return dso->data.fd;
181
182         if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
183                 dso->data.fd = open_dso(dso, machine);
184                 return dso->data.fd;
185         }
186
187         do {
188                 int fd;
189
190                 dso->binary_type = binary_type_data[i++];
191
192                 fd = open_dso(dso, machine);
193                 if (fd >= 0)
194                         return dso->data.fd = fd;
195
196         } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
197
198         return -EINVAL;
199 }
200
201 static void
202 dso_cache__free(struct rb_root *root)
203 {
204         struct rb_node *next = rb_first(root);
205
206         while (next) {
207                 struct dso_cache *cache;
208
209                 cache = rb_entry(next, struct dso_cache, rb_node);
210                 next = rb_next(&cache->rb_node);
211                 rb_erase(&cache->rb_node, root);
212                 free(cache);
213         }
214 }
215
216 static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
217 {
218         struct rb_node * const *p = &root->rb_node;
219         const struct rb_node *parent = NULL;
220         struct dso_cache *cache;
221
222         while (*p != NULL) {
223                 u64 end;
224
225                 parent = *p;
226                 cache = rb_entry(parent, struct dso_cache, rb_node);
227                 end = cache->offset + DSO__DATA_CACHE_SIZE;
228
229                 if (offset < cache->offset)
230                         p = &(*p)->rb_left;
231                 else if (offset >= end)
232                         p = &(*p)->rb_right;
233                 else
234                         return cache;
235         }
236         return NULL;
237 }
238
239 static void
240 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
241 {
242         struct rb_node **p = &root->rb_node;
243         struct rb_node *parent = NULL;
244         struct dso_cache *cache;
245         u64 offset = new->offset;
246
247         while (*p != NULL) {
248                 u64 end;
249
250                 parent = *p;
251                 cache = rb_entry(parent, struct dso_cache, rb_node);
252                 end = cache->offset + DSO__DATA_CACHE_SIZE;
253
254                 if (offset < cache->offset)
255                         p = &(*p)->rb_left;
256                 else if (offset >= end)
257                         p = &(*p)->rb_right;
258         }
259
260         rb_link_node(&new->rb_node, parent, p);
261         rb_insert_color(&new->rb_node, root);
262 }
263
264 static ssize_t
265 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
266                   u8 *data, u64 size)
267 {
268         u64 cache_offset = offset - cache->offset;
269         u64 cache_size   = min(cache->size - cache_offset, size);
270
271         memcpy(data, cache->data + cache_offset, cache_size);
272         return cache_size;
273 }
274
275 static ssize_t
276 dso_cache__read(struct dso *dso, struct machine *machine,
277                  u64 offset, u8 *data, ssize_t size)
278 {
279         struct dso_cache *cache;
280         ssize_t ret;
281         int fd;
282
283         fd = dso__data_fd(dso, machine);
284         if (fd < 0)
285                 return -1;
286
287         do {
288                 u64 cache_offset;
289
290                 ret = -ENOMEM;
291
292                 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
293                 if (!cache)
294                         break;
295
296                 cache_offset = offset & DSO__DATA_CACHE_MASK;
297                 ret = -EINVAL;
298
299                 if (-1 == lseek(fd, cache_offset, SEEK_SET))
300                         break;
301
302                 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
303                 if (ret <= 0)
304                         break;
305
306                 cache->offset = cache_offset;
307                 cache->size   = ret;
308                 dso_cache__insert(&dso->data.cache, cache);
309
310                 ret = dso_cache__memcpy(cache, offset, data, size);
311
312         } while (0);
313
314         if (ret <= 0)
315                 free(cache);
316
317         dso__data_close(dso);
318         return ret;
319 }
320
321 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
322                               u64 offset, u8 *data, ssize_t size)
323 {
324         struct dso_cache *cache;
325
326         cache = dso_cache__find(&dso->data.cache, offset);
327         if (cache)
328                 return dso_cache__memcpy(cache, offset, data, size);
329         else
330                 return dso_cache__read(dso, machine, offset, data, size);
331 }
332
333 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
334                               u64 offset, u8 *data, ssize_t size)
335 {
336         ssize_t r = 0;
337         u8 *p = data;
338
339         do {
340                 ssize_t ret;
341
342                 ret = dso_cache_read(dso, machine, offset, p, size);
343                 if (ret < 0)
344                         return ret;
345
346                 /* Reached EOF, return what we have. */
347                 if (!ret)
348                         break;
349
350                 BUG_ON(ret > size);
351
352                 r      += ret;
353                 p      += ret;
354                 offset += ret;
355                 size   -= ret;
356
357         } while (size);
358
359         return r;
360 }
361
362 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
363                             struct machine *machine, u64 addr,
364                             u8 *data, ssize_t size)
365 {
366         u64 offset = map->map_ip(map, addr);
367         return dso__data_read_offset(dso, machine, offset, data, size);
368 }
369
370 struct map *dso__new_map(const char *name)
371 {
372         struct map *map = NULL;
373         struct dso *dso = dso__new(name);
374
375         if (dso)
376                 map = map__new2(0, dso, MAP__FUNCTION);
377
378         return map;
379 }
380
381 struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
382                     const char *short_name, int dso_type)
383 {
384         /*
385          * The kernel dso could be created by build_id processing.
386          */
387         struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
388
389         /*
390          * We need to run this in all cases, since during the build_id
391          * processing we had no idea this was the kernel dso.
392          */
393         if (dso != NULL) {
394                 dso__set_short_name(dso, short_name, false);
395                 dso->kernel = dso_type;
396         }
397
398         return dso;
399 }
400
401 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
402 {
403         if (name == NULL)
404                 return;
405
406         if (dso->long_name_allocated)
407                 free((char *)dso->long_name);
408
409         dso->long_name           = name;
410         dso->long_name_len       = strlen(name);
411         dso->long_name_allocated = name_allocated;
412 }
413
414 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
415 {
416         if (name == NULL)
417                 return;
418
419         if (dso->short_name_allocated)
420                 free((char *)dso->short_name);
421
422         dso->short_name           = name;
423         dso->short_name_len       = strlen(name);
424         dso->short_name_allocated = name_allocated;
425 }
426
427 static void dso__set_basename(struct dso *dso)
428 {
429        /*
430         * basename() may modify path buffer, so we must pass
431         * a copy.
432         */
433        char *base, *lname = strdup(dso->long_name);
434
435        if (!lname)
436                return;
437
438        /*
439         * basename() may return a pointer to internal
440         * storage which is reused in subsequent calls
441         * so copy the result.
442         */
443        base = strdup(basename(lname));
444
445        free(lname);
446
447        if (!base)
448                return;
449
450        dso__set_short_name(dso, base, true);
451 }
452
453 int dso__name_len(const struct dso *dso)
454 {
455         if (!dso)
456                 return strlen("[unknown]");
457         if (verbose)
458                 return dso->long_name_len;
459
460         return dso->short_name_len;
461 }
462
463 bool dso__loaded(const struct dso *dso, enum map_type type)
464 {
465         return dso->loaded & (1 << type);
466 }
467
468 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
469 {
470         return dso->sorted_by_name & (1 << type);
471 }
472
473 void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
474 {
475         dso->sorted_by_name |= (1 << type);
476 }
477
478 struct dso *dso__new(const char *name)
479 {
480         struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
481
482         if (dso != NULL) {
483                 int i;
484                 strcpy(dso->name, name);
485                 dso__set_long_name(dso, dso->name, false);
486                 dso__set_short_name(dso, dso->name, false);
487                 for (i = 0; i < MAP__NR_TYPES; ++i)
488                         dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
489                 dso->data.cache = RB_ROOT;
490                 dso->data.fd = -1;
491                 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
492                 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
493                 dso->loaded = 0;
494                 dso->rel = 0;
495                 dso->sorted_by_name = 0;
496                 dso->has_build_id = 0;
497                 dso->has_srcline = 1;
498                 dso->a2l_fails = 1;
499                 dso->kernel = DSO_TYPE_USER;
500                 dso->needs_swap = DSO_SWAP__UNSET;
501                 INIT_LIST_HEAD(&dso->node);
502         }
503
504         return dso;
505 }
506
507 void dso__delete(struct dso *dso)
508 {
509         int i;
510         for (i = 0; i < MAP__NR_TYPES; ++i)
511                 symbols__delete(&dso->symbols[i]);
512
513         if (dso->short_name_allocated) {
514                 zfree((char **)&dso->short_name);
515                 dso->short_name_allocated = false;
516         }
517
518         if (dso->long_name_allocated) {
519                 zfree((char **)&dso->long_name);
520                 dso->long_name_allocated = false;
521         }
522
523         dso__data_close(dso);
524         dso_cache__free(&dso->data.cache);
525         dso__free_a2l(dso);
526         zfree(&dso->symsrc_filename);
527         free(dso);
528 }
529
530 void dso__set_build_id(struct dso *dso, void *build_id)
531 {
532         memcpy(dso->build_id, build_id, sizeof(dso->build_id));
533         dso->has_build_id = 1;
534 }
535
536 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
537 {
538         return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
539 }
540
541 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
542 {
543         char path[PATH_MAX];
544
545         if (machine__is_default_guest(machine))
546                 return;
547         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
548         if (sysfs__read_build_id(path, dso->build_id,
549                                  sizeof(dso->build_id)) == 0)
550                 dso->has_build_id = true;
551 }
552
553 int dso__kernel_module_get_build_id(struct dso *dso,
554                                     const char *root_dir)
555 {
556         char filename[PATH_MAX];
557         /*
558          * kernel module short names are of the form "[module]" and
559          * we need just "module" here.
560          */
561         const char *name = dso->short_name + 1;
562
563         snprintf(filename, sizeof(filename),
564                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
565                  root_dir, (int)strlen(name) - 1, name);
566
567         if (sysfs__read_build_id(filename, dso->build_id,
568                                  sizeof(dso->build_id)) == 0)
569                 dso->has_build_id = true;
570
571         return 0;
572 }
573
574 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
575 {
576         bool have_build_id = false;
577         struct dso *pos;
578
579         list_for_each_entry(pos, head, node) {
580                 if (with_hits && !pos->hit)
581                         continue;
582                 if (pos->has_build_id) {
583                         have_build_id = true;
584                         continue;
585                 }
586                 if (filename__read_build_id(pos->long_name, pos->build_id,
587                                             sizeof(pos->build_id)) > 0) {
588                         have_build_id     = true;
589                         pos->has_build_id = true;
590                 }
591         }
592
593         return have_build_id;
594 }
595
596 void dsos__add(struct list_head *head, struct dso *dso)
597 {
598         list_add_tail(&dso->node, head);
599 }
600
601 struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
602 {
603         struct dso *pos;
604
605         if (cmp_short) {
606                 list_for_each_entry(pos, head, node)
607                         if (strcmp(pos->short_name, name) == 0)
608                                 return pos;
609                 return NULL;
610         }
611         list_for_each_entry(pos, head, node)
612                 if (strcmp(pos->long_name, name) == 0)
613                         return pos;
614         return NULL;
615 }
616
617 struct dso *__dsos__findnew(struct list_head *head, const char *name)
618 {
619         struct dso *dso = dsos__find(head, name, false);
620
621         if (!dso) {
622                 dso = dso__new(name);
623                 if (dso != NULL) {
624                         dsos__add(head, dso);
625                         dso__set_basename(dso);
626                 }
627         }
628
629         return dso;
630 }
631
632 size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
633                                bool (skip)(struct dso *dso, int parm), int parm)
634 {
635         struct dso *pos;
636         size_t ret = 0;
637
638         list_for_each_entry(pos, head, node) {
639                 if (skip && skip(pos, parm))
640                         continue;
641                 ret += dso__fprintf_buildid(pos, fp);
642                 ret += fprintf(fp, " %s\n", pos->long_name);
643         }
644         return ret;
645 }
646
647 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
648 {
649         struct dso *pos;
650         size_t ret = 0;
651
652         list_for_each_entry(pos, head, node) {
653                 int i;
654                 for (i = 0; i < MAP__NR_TYPES; ++i)
655                         ret += dso__fprintf(pos, i, fp);
656         }
657
658         return ret;
659 }
660
661 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
662 {
663         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
664
665         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
666         return fprintf(fp, "%s", sbuild_id);
667 }
668
669 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
670 {
671         struct rb_node *nd;
672         size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
673
674         if (dso->short_name != dso->long_name)
675                 ret += fprintf(fp, "%s, ", dso->long_name);
676         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
677                        dso__loaded(dso, type) ? "" : "NOT ");
678         ret += dso__fprintf_buildid(dso, fp);
679         ret += fprintf(fp, ")\n");
680         for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
681                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
682                 ret += symbol__fprintf(pos, fp);
683         }
684
685         return ret;
686 }