map.c 4.9 KB

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