map.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "symbol.h"
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "map.h"
  7. const char *map_type__name[MAP__NR_TYPES] = {
  8. [MAP__FUNCTION] = "Functions",
  9. [MAP__VARIABLE] = "Variables",
  10. };
  11. static inline int is_anon_memory(const char *filename)
  12. {
  13. return strcmp(filename, "//anon") == 0;
  14. }
  15. static int strcommon(const char *pathname, char *cwd, int cwdlen)
  16. {
  17. int n = 0;
  18. while (n < cwdlen && pathname[n] == cwd[n])
  19. ++n;
  20. return n;
  21. }
  22. void map__init(struct map *self, enum map_type type,
  23. u64 start, u64 end, u64 pgoff, struct dso *dso)
  24. {
  25. self->type = type;
  26. self->start = start;
  27. self->end = end;
  28. self->pgoff = pgoff;
  29. self->dso = dso;
  30. self->map_ip = map__map_ip;
  31. self->unmap_ip = map__unmap_ip;
  32. RB_CLEAR_NODE(&self->rb_node);
  33. }
  34. struct map *map__new(u64 start, u64 len, u64 pgoff, u32 pid, char *filename,
  35. enum map_type type, char *cwd, int cwdlen)
  36. {
  37. struct map *self = malloc(sizeof(*self));
  38. if (self != NULL) {
  39. char newfilename[PATH_MAX];
  40. struct dso *dso;
  41. int anon;
  42. if (cwd) {
  43. int n = strcommon(filename, cwd, cwdlen);
  44. if (n == cwdlen) {
  45. snprintf(newfilename, sizeof(newfilename),
  46. ".%s", filename + n);
  47. filename = newfilename;
  48. }
  49. }
  50. anon = is_anon_memory(filename);
  51. if (anon) {
  52. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
  53. filename = newfilename;
  54. }
  55. dso = dsos__findnew(filename);
  56. if (dso == NULL)
  57. goto out_delete;
  58. map__init(self, type, start, start + len, pgoff, dso);
  59. if (anon) {
  60. set_identity:
  61. self->map_ip = self->unmap_ip = identity__map_ip;
  62. } else if (strcmp(filename, "[vdso]") == 0) {
  63. dso__set_loaded(dso, self->type);
  64. goto set_identity;
  65. }
  66. }
  67. return self;
  68. out_delete:
  69. free(self);
  70. return NULL;
  71. }
  72. void map__delete(struct map *self)
  73. {
  74. free(self);
  75. }
  76. void map__fixup_start(struct map *self)
  77. {
  78. struct rb_root *symbols = &self->dso->symbols[self->type];
  79. struct rb_node *nd = rb_first(symbols);
  80. if (nd != NULL) {
  81. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  82. self->start = sym->start;
  83. }
  84. }
  85. void map__fixup_end(struct map *self)
  86. {
  87. struct rb_root *symbols = &self->dso->symbols[self->type];
  88. struct rb_node *nd = rb_last(symbols);
  89. if (nd != NULL) {
  90. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  91. self->end = sym->end;
  92. }
  93. }
  94. #define DSO__DELETED "(deleted)"
  95. int map__load(struct map *self, symbol_filter_t filter)
  96. {
  97. const char *name = self->dso->long_name;
  98. int nr;
  99. if (dso__loaded(self->dso, self->type))
  100. return 0;
  101. nr = dso__load(self->dso, self, filter);
  102. if (nr < 0) {
  103. if (self->dso->has_build_id) {
  104. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  105. build_id__sprintf(self->dso->build_id,
  106. sizeof(self->dso->build_id),
  107. sbuild_id);
  108. pr_warning("%s with build id %s not found",
  109. name, sbuild_id);
  110. } else
  111. pr_warning("Failed to open %s", name);
  112. pr_warning(", continuing without symbols\n");
  113. return -1;
  114. } else if (nr == 0) {
  115. const size_t len = strlen(name);
  116. const size_t real_len = len - sizeof(DSO__DELETED);
  117. if (len > sizeof(DSO__DELETED) &&
  118. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  119. pr_warning("%.*s was updated, restart the long "
  120. "running apps that use it!\n",
  121. (int)real_len, name);
  122. } else {
  123. pr_warning("no symbols found in %s, maybe install "
  124. "a debug package?\n", name);
  125. }
  126. return -1;
  127. }
  128. /*
  129. * Only applies to the kernel, as its symtabs aren't relative like the
  130. * module ones.
  131. */
  132. if (self->dso->kernel)
  133. map__reloc_vmlinux(self);
  134. return 0;
  135. }
  136. struct symbol *map__find_symbol(struct map *self, u64 addr,
  137. symbol_filter_t filter)
  138. {
  139. if (map__load(self, filter) < 0)
  140. return NULL;
  141. return dso__find_symbol(self->dso, self->type, addr);
  142. }
  143. struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
  144. symbol_filter_t filter)
  145. {
  146. if (map__load(self, filter) < 0)
  147. return NULL;
  148. if (!dso__sorted_by_name(self->dso, self->type))
  149. dso__sort_by_name(self->dso, self->type);
  150. return dso__find_symbol_by_name(self->dso, self->type, name);
  151. }
  152. struct map *map__clone(struct map *self)
  153. {
  154. struct map *map = malloc(sizeof(*self));
  155. if (!map)
  156. return NULL;
  157. memcpy(map, self, sizeof(*self));
  158. return map;
  159. }
  160. int map__overlap(struct map *l, struct map *r)
  161. {
  162. if (l->start > r->start) {
  163. struct map *t = l;
  164. l = r;
  165. r = t;
  166. }
  167. if (l->end > r->start)
  168. return 1;
  169. return 0;
  170. }
  171. size_t map__fprintf(struct map *self, FILE *fp)
  172. {
  173. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  174. self->start, self->end, self->pgoff, self->dso->name);
  175. }
  176. /*
  177. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  178. * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
  179. */
  180. u64 map__rip_2objdump(struct map *map, u64 rip)
  181. {
  182. u64 addr = map->dso->adjust_symbols ?
  183. map->unmap_ip(map, rip) : /* RIP -> IP */
  184. rip;
  185. return addr;
  186. }
  187. u64 map__objdump_2ip(struct map *map, u64 addr)
  188. {
  189. u64 ip = map->dso->adjust_symbols ?
  190. addr :
  191. map->unmap_ip(map, addr); /* RIP -> IP */
  192. return ip;
  193. }
  194. struct symbol *map_groups__find_symbol(struct map_groups *self,
  195. enum map_type type, u64 addr,
  196. symbol_filter_t filter)
  197. {
  198. struct map *map = map_groups__find(self, type, addr);
  199. if (map != NULL)
  200. return map__find_symbol(map, map->map_ip(map, addr), filter);
  201. return NULL;
  202. }
  203. static u64 map__reloc_map_ip(struct map *map, u64 ip)
  204. {
  205. return ip + (s64)map->pgoff;
  206. }
  207. static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
  208. {
  209. return ip - (s64)map->pgoff;
  210. }
  211. void map__reloc_vmlinux(struct map *self)
  212. {
  213. struct kmap *kmap = map__kmap(self);
  214. s64 reloc;
  215. if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
  216. return;
  217. reloc = (kmap->ref_reloc_sym->unrelocated_addr -
  218. kmap->ref_reloc_sym->addr);
  219. if (!reloc)
  220. return;
  221. self->map_ip = map__reloc_map_ip;
  222. self->unmap_ip = map__reloc_unmap_ip;
  223. self->pgoff = reloc;
  224. }
  225. void maps__insert(struct rb_root *maps, struct map *map)
  226. {
  227. struct rb_node **p = &maps->rb_node;
  228. struct rb_node *parent = NULL;
  229. const u64 ip = map->start;
  230. struct map *m;
  231. while (*p != NULL) {
  232. parent = *p;
  233. m = rb_entry(parent, struct map, rb_node);
  234. if (ip < m->start)
  235. p = &(*p)->rb_left;
  236. else
  237. p = &(*p)->rb_right;
  238. }
  239. rb_link_node(&map->rb_node, parent, p);
  240. rb_insert_color(&map->rb_node, maps);
  241. }
  242. struct map *maps__find(struct rb_root *maps, u64 ip)
  243. {
  244. struct rb_node **p = &maps->rb_node;
  245. struct rb_node *parent = NULL;
  246. struct map *m;
  247. while (*p != NULL) {
  248. parent = *p;
  249. m = rb_entry(parent, struct map, rb_node);
  250. if (ip < m->start)
  251. p = &(*p)->rb_left;
  252. else if (ip > m->end)
  253. p = &(*p)->rb_right;
  254. else
  255. return m;
  256. }
  257. return NULL;
  258. }