diff options
author | Frederic Weisbecker <fweisbec@gmail.com> | 2009-08-12 05:07:25 -0400 |
---|---|---|
committer | Frederic Weisbecker <fweisbec@gmail.com> | 2009-08-12 06:37:37 -0400 |
commit | 66e274f3b8d7fc89d38997e85b900e188f8d5cc0 (patch) | |
tree | 5a0de899b891b2ce8440d2a3275b4ae7cb88b6c3 /tools/perf/util/map.c | |
parent | 1fe2c1066ce6a30bda7b27785ee3d9b8e62ffbbd (diff) |
perf tools: Factorize the map helpers
Factorize the dso mapping helpers into a single purpose common file
"util/map.c"
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Brice Goglin <Brice.Goglin@inria.fr>
Diffstat (limited to 'tools/perf/util/map.c')
-rw-r--r-- | tools/perf/util/map.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c new file mode 100644 index 000000000000..804e02382739 --- /dev/null +++ b/tools/perf/util/map.c | |||
@@ -0,0 +1,97 @@ | |||
1 | #include "event.h" | ||
2 | #include "symbol.h" | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | static inline int is_anon_memory(const char *filename) | ||
8 | { | ||
9 | return strcmp(filename, "//anon") == 0; | ||
10 | } | ||
11 | |||
12 | static int strcommon(const char *pathname, char *cwd, int cwdlen) | ||
13 | { | ||
14 | int n = 0; | ||
15 | |||
16 | while (n < cwdlen && pathname[n] == cwd[n]) | ||
17 | ++n; | ||
18 | |||
19 | return n; | ||
20 | } | ||
21 | |||
22 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) | ||
23 | { | ||
24 | struct map *self = malloc(sizeof(*self)); | ||
25 | |||
26 | if (self != NULL) { | ||
27 | const char *filename = event->filename; | ||
28 | char newfilename[PATH_MAX]; | ||
29 | int anon; | ||
30 | |||
31 | if (cwd) { | ||
32 | int n = strcommon(filename, cwd, cwdlen); | ||
33 | |||
34 | if (n == cwdlen) { | ||
35 | snprintf(newfilename, sizeof(newfilename), | ||
36 | ".%s", filename + n); | ||
37 | filename = newfilename; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | anon = is_anon_memory(filename); | ||
42 | |||
43 | if (anon) { | ||
44 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid); | ||
45 | filename = newfilename; | ||
46 | } | ||
47 | |||
48 | self->start = event->start; | ||
49 | self->end = event->start + event->len; | ||
50 | self->pgoff = event->pgoff; | ||
51 | |||
52 | self->dso = dsos__findnew(filename); | ||
53 | if (self->dso == NULL) | ||
54 | goto out_delete; | ||
55 | |||
56 | if (self->dso == vdso || anon) | ||
57 | self->map_ip = vdso__map_ip; | ||
58 | else | ||
59 | self->map_ip = map__map_ip; | ||
60 | } | ||
61 | return self; | ||
62 | out_delete: | ||
63 | free(self); | ||
64 | return NULL; | ||
65 | } | ||
66 | |||
67 | struct map *map__clone(struct map *self) | ||
68 | { | ||
69 | struct map *map = malloc(sizeof(*self)); | ||
70 | |||
71 | if (!map) | ||
72 | return NULL; | ||
73 | |||
74 | memcpy(map, self, sizeof(*self)); | ||
75 | |||
76 | return map; | ||
77 | } | ||
78 | |||
79 | int map__overlap(struct map *l, struct map *r) | ||
80 | { | ||
81 | if (l->start > r->start) { | ||
82 | struct map *t = l; | ||
83 | l = r; | ||
84 | r = t; | ||
85 | } | ||
86 | |||
87 | if (l->end > r->start) | ||
88 | return 1; | ||
89 | |||
90 | return 0; | ||
91 | } | ||
92 | |||
93 | size_t map__fprintf(struct map *self, FILE *fp) | ||
94 | { | ||
95 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", | ||
96 | self->start, self->end, self->pgoff, self->dso->name); | ||
97 | } | ||