/tools/perf/util/map.c

https://github.com/kevjames3/linux-2.6 · C · 237 lines · 187 code · 42 blank · 8 comment · 36 complexity · 2bf4d67dcd28d7df67a295d399df589d MD5 · raw file

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