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