map.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "event.h"
  2. #include "symbol.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "debug.h"
  7. static inline int is_anon_memory(const char *filename)
  8. {
  9. return strcmp(filename, "//anon") == 0;
  10. }
  11. static int strcommon(const char *pathname, char *cwd, int cwdlen)
  12. {
  13. int n = 0;
  14. while (n < cwdlen && pathname[n] == cwd[n])
  15. ++n;
  16. return n;
  17. }
  18. void map__init(struct map *self, enum map_type type,
  19. u64 start, u64 end, u64 pgoff, struct dso *dso)
  20. {
  21. self->type = type;
  22. self->start = start;
  23. self->end = end;
  24. self->pgoff = pgoff;
  25. self->dso = dso;
  26. self->map_ip = map__map_ip;
  27. self->unmap_ip = map__unmap_ip;
  28. RB_CLEAR_NODE(&self->rb_node);
  29. }
  30. struct map *map__new(struct mmap_event *event, enum map_type type,
  31. char *cwd, int cwdlen)
  32. {
  33. struct map *self = malloc(sizeof(*self));
  34. if (self != NULL) {
  35. const char *filename = event->filename;
  36. char newfilename[PATH_MAX];
  37. struct dso *dso;
  38. int anon;
  39. if (cwd) {
  40. int n = strcommon(filename, cwd, cwdlen);
  41. if (n == cwdlen) {
  42. snprintf(newfilename, sizeof(newfilename),
  43. ".%s", filename + n);
  44. filename = newfilename;
  45. }
  46. }
  47. anon = is_anon_memory(filename);
  48. if (anon) {
  49. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  50. filename = newfilename;
  51. }
  52. dso = dsos__findnew(filename);
  53. if (dso == NULL)
  54. goto out_delete;
  55. map__init(self, type, event->start, event->start + event->len,
  56. event->pgoff, dso);
  57. if (anon) {
  58. set_identity:
  59. self->map_ip = self->unmap_ip = identity__map_ip;
  60. } else if (strcmp(filename, "[vdso]") == 0) {
  61. dso__set_loaded(dso, self->type);
  62. goto set_identity;
  63. }
  64. }
  65. return self;
  66. out_delete:
  67. free(self);
  68. return NULL;
  69. }
  70. void map__delete(struct map *self)
  71. {
  72. free(self);
  73. }
  74. void map__fixup_start(struct map *self)
  75. {
  76. struct rb_root *symbols = &self->dso->symbols[self->type];
  77. struct rb_node *nd = rb_first(symbols);
  78. if (nd != NULL) {
  79. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  80. self->start = sym->start;
  81. }
  82. }
  83. void map__fixup_end(struct map *self)
  84. {
  85. struct rb_root *symbols = &self->dso->symbols[self->type];
  86. struct rb_node *nd = rb_last(symbols);
  87. if (nd != NULL) {
  88. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  89. self->end = sym->end;
  90. }
  91. }
  92. #define DSO__DELETED "(deleted)"
  93. int map__load(struct map *self, symbol_filter_t filter)
  94. {
  95. const char *name = self->dso->long_name;
  96. int nr;
  97. if (dso__loaded(self->dso, self->type))
  98. return 0;
  99. nr = dso__load(self->dso, self, filter);
  100. if (nr < 0) {
  101. if (self->dso->has_build_id) {
  102. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  103. build_id__sprintf(self->dso->build_id,
  104. sizeof(self->dso->build_id),
  105. sbuild_id);
  106. pr_warning("%s with build id %s not found",
  107. name, sbuild_id);
  108. } else
  109. pr_warning("Failed to open %s", name);
  110. pr_warning(", continuing without symbols\n");
  111. return -1;
  112. } else if (nr == 0) {
  113. const size_t len = strlen(name);
  114. const size_t real_len = len - sizeof(DSO__DELETED);
  115. if (len > sizeof(DSO__DELETED) &&
  116. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  117. pr_warning("%.*s was updated, restart the long "
  118. "running apps that use it!\n",
  119. (int)real_len, name);
  120. } else {
  121. pr_warning("no symbols found in %s, maybe install "
  122. "a debug package?\n", name);
  123. }
  124. return -1;
  125. }
  126. /*
  127. * Only applies to the kernel, as its symtabs aren't relative like the
  128. * module ones.
  129. */
  130. if (self->dso->kernel)
  131. map__reloc_vmlinux(self);
  132. return 0;
  133. }
  134. struct symbol *map__find_symbol(struct map *self, u64 addr,
  135. symbol_filter_t filter)
  136. {
  137. if (map__load(self, filter) < 0)
  138. return NULL;
  139. return dso__find_symbol(self->dso, self->type, addr);
  140. }
  141. struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
  142. symbol_filter_t filter)
  143. {
  144. if (map__load(self, filter) < 0)
  145. return NULL;
  146. if (!dso__sorted_by_name(self->dso, self->type))
  147. dso__sort_by_name(self->dso, self->type);
  148. return dso__find_symbol_by_name(self->dso, self->type, name);
  149. }
  150. struct map *map__clone(struct map *self)
  151. {
  152. struct map *map = malloc(sizeof(*self));
  153. if (!map)
  154. return NULL;
  155. memcpy(map, self, sizeof(*self));
  156. return map;
  157. }
  158. int map__overlap(struct map *l, struct map *r)
  159. {
  160. if (l->start > r->start) {
  161. struct map *t = l;
  162. l = r;
  163. r = t;
  164. }
  165. if (l->end > r->start)
  166. return 1;
  167. return 0;
  168. }
  169. size_t map__fprintf(struct map *self, FILE *fp)
  170. {
  171. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  172. self->start, self->end, self->pgoff, self->dso->name);
  173. }
  174. /*
  175. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  176. * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
  177. */
  178. u64 map__rip_2objdump(struct map *map, u64 rip)
  179. {
  180. u64 addr = map->dso->adjust_symbols ?
  181. map->unmap_ip(map, rip) : /* RIP -> IP */
  182. rip;
  183. return addr;
  184. }
  185. u64 map__objdump_2ip(struct map *map, u64 addr)
  186. {
  187. u64 ip = map->dso->adjust_symbols ?
  188. addr :
  189. map->unmap_ip(map, addr); /* RIP -> IP */
  190. return ip;
  191. }