perf tools: Unify debug messages mechanisms
[linux-2.6/x86.git] / tools / perf / util / map.c
blobc1c5568253432db23aa0b0abc3b26e44127910c7
1 #include "event.h"
2 #include "symbol.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "debug.h"
8 static inline int is_anon_memory(const char *filename)
10 return strcmp(filename, "//anon") == 0;
13 static int strcommon(const char *pathname, char *cwd, int cwdlen)
15 int n = 0;
17 while (n < cwdlen && pathname[n] == cwd[n])
18 ++n;
20 return n;
23 struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen,
24 unsigned int sym_priv_size, symbol_filter_t filter)
26 struct map *self = malloc(sizeof(*self));
28 if (self != NULL) {
29 const char *filename = event->filename;
30 char newfilename[PATH_MAX];
31 int anon;
32 bool new_dso;
34 if (cwd) {
35 int n = strcommon(filename, cwd, cwdlen);
37 if (n == cwdlen) {
38 snprintf(newfilename, sizeof(newfilename),
39 ".%s", filename + n);
40 filename = newfilename;
44 anon = is_anon_memory(filename);
46 if (anon) {
47 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
48 filename = newfilename;
51 self->start = event->start;
52 self->end = event->start + event->len;
53 self->pgoff = event->pgoff;
55 self->dso = dsos__findnew(filename, sym_priv_size, &new_dso);
56 if (self->dso == NULL)
57 goto out_delete;
59 if (new_dso) {
60 int nr = dso__load(self->dso, self, filter);
62 if (nr < 0)
63 pr_warning("Failed to open %s, continuing "
64 "without symbols\n",
65 self->dso->long_name);
66 else if (nr == 0)
67 pr_warning("No symbols found in %s, maybe "
68 "install a debug package?\n",
69 self->dso->long_name);
72 if (self->dso == vdso || anon)
73 self->map_ip = self->unmap_ip = identity__map_ip;
74 else {
75 self->map_ip = map__map_ip;
76 self->unmap_ip = map__unmap_ip;
79 return self;
80 out_delete:
81 free(self);
82 return NULL;
85 struct map *map__clone(struct map *self)
87 struct map *map = malloc(sizeof(*self));
89 if (!map)
90 return NULL;
92 memcpy(map, self, sizeof(*self));
94 return map;
97 int map__overlap(struct map *l, struct map *r)
99 if (l->start > r->start) {
100 struct map *t = l;
101 l = r;
102 r = t;
105 if (l->end > r->start)
106 return 1;
108 return 0;
111 size_t map__fprintf(struct map *self, FILE *fp)
113 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
114 self->start, self->end, self->pgoff, self->dso->name);