blob: d302e513e0622b486ecfc55d343d064cada24ac2 [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "event.h"
2#include "symbol.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02006#include "debug.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02007
8static inline int is_anon_memory(const char *filename)
9{
10 return strcmp(filename, "//anon") == 0;
11}
12
13static int strcommon(const char *pathname, char *cwd, int cwdlen)
14{
15 int n = 0;
16
17 while (n < cwdlen && pathname[n] == cwd[n])
18 ++n;
19
20 return n;
21}
22
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020023struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen,
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -020024 unsigned int sym_priv_size)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020025{
26 struct map *self = malloc(sizeof(*self));
27
28 if (self != NULL) {
29 const char *filename = event->filename;
30 char newfilename[PATH_MAX];
31 int anon;
32
33 if (cwd) {
34 int n = strcommon(filename, cwd, cwdlen);
35
36 if (n == cwdlen) {
37 snprintf(newfilename, sizeof(newfilename),
38 ".%s", filename + n);
39 filename = newfilename;
40 }
41 }
42
43 anon = is_anon_memory(filename);
44
45 if (anon) {
46 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
47 filename = newfilename;
48 }
49
50 self->start = event->start;
51 self->end = event->start + event->len;
52 self->pgoff = event->pgoff;
53
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -020054 self->dso = dsos__findnew(filename, sym_priv_size);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055 if (self->dso == NULL)
56 goto out_delete;
57
58 if (self->dso == vdso || anon)
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020059 self->map_ip = self->unmap_ip = identity__map_ip;
60 else {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020061 self->map_ip = map__map_ip;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020062 self->unmap_ip = map__unmap_ip;
63 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020064 }
65 return self;
66out_delete:
67 free(self);
68 return NULL;
69}
70
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -020071struct symbol *
72map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter)
73{
74 if (!self->dso->loaded) {
75 int nr = dso__load(self->dso, self, filter);
76
77 if (nr < 0) {
78 pr_warning("Failed to open %s, continuing without symbols\n",
79 self->dso->long_name);
80 return NULL;
81 } else if (nr == 0) {
82 pr_warning("No symbols found in %s, maybe install a debug package?\n",
83 self->dso->long_name);
84 return NULL;
85 }
86 }
87
88 return self->dso->find_symbol(self->dso, ip);
89}
90
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020091struct map *map__clone(struct map *self)
92{
93 struct map *map = malloc(sizeof(*self));
94
95 if (!map)
96 return NULL;
97
98 memcpy(map, self, sizeof(*self));
99
100 return map;
101}
102
103int map__overlap(struct map *l, struct map *r)
104{
105 if (l->start > r->start) {
106 struct map *t = l;
107 l = r;
108 r = t;
109 }
110
111 if (l->end > r->start)
112 return 1;
113
114 return 0;
115}
116
117size_t map__fprintf(struct map *self, FILE *fp)
118{
119 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
120 self->start, self->end, self->pgoff, self->dso->name);
121}