map.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 (self->dso == vdso || anon)
  58. self->map_ip = self->unmap_ip = identity__map_ip;
  59. }
  60. return self;
  61. out_delete:
  62. free(self);
  63. return NULL;
  64. }
  65. void map__delete(struct map *self)
  66. {
  67. free(self);
  68. }
  69. void map__fixup_start(struct map *self)
  70. {
  71. struct rb_root *symbols = &self->dso->symbols[self->type];
  72. struct rb_node *nd = rb_first(symbols);
  73. if (nd != NULL) {
  74. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  75. self->start = sym->start;
  76. }
  77. }
  78. void map__fixup_end(struct map *self)
  79. {
  80. struct rb_root *symbols = &self->dso->symbols[self->type];
  81. struct rb_node *nd = rb_last(symbols);
  82. if (nd != NULL) {
  83. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  84. self->end = sym->end;
  85. }
  86. }
  87. #define DSO__DELETED "(deleted)"
  88. int map__load(struct map *self, struct perf_session *session,
  89. symbol_filter_t filter)
  90. {
  91. const char *name = self->dso->long_name;
  92. int nr;
  93. if (dso__loaded(self->dso, self->type))
  94. return 0;
  95. nr = dso__load(self->dso, self, session, filter);
  96. if (nr < 0) {
  97. if (self->dso->has_build_id) {
  98. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  99. build_id__sprintf(self->dso->build_id,
  100. sizeof(self->dso->build_id),
  101. sbuild_id);
  102. pr_warning("%s with build id %s not found",
  103. name, sbuild_id);
  104. } else
  105. pr_warning("Failed to open %s", name);
  106. pr_warning(", continuing without symbols\n");
  107. return -1;
  108. } else if (nr == 0) {
  109. const size_t len = strlen(name);
  110. const size_t real_len = len - sizeof(DSO__DELETED);
  111. if (len > sizeof(DSO__DELETED) &&
  112. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  113. pr_warning("%.*s was updated, restart the long "
  114. "running apps that use it!\n",
  115. (int)real_len, name);
  116. } else {
  117. pr_warning("no symbols found in %s, maybe install "
  118. "a debug package?\n", name);
  119. }
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. struct symbol *map__find_symbol(struct map *self, struct perf_session *session,
  125. u64 addr, symbol_filter_t filter)
  126. {
  127. if (map__load(self, session, filter) < 0)
  128. return NULL;
  129. return dso__find_symbol(self->dso, self->type, addr);
  130. }
  131. struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
  132. struct perf_session *session,
  133. symbol_filter_t filter)
  134. {
  135. if (map__load(self, session, filter) < 0)
  136. return NULL;
  137. if (!dso__sorted_by_name(self->dso, self->type))
  138. dso__sort_by_name(self->dso, self->type);
  139. return dso__find_symbol_by_name(self->dso, self->type, name);
  140. }
  141. struct map *map__clone(struct map *self)
  142. {
  143. struct map *map = malloc(sizeof(*self));
  144. if (!map)
  145. return NULL;
  146. memcpy(map, self, sizeof(*self));
  147. return map;
  148. }
  149. int map__overlap(struct map *l, struct map *r)
  150. {
  151. if (l->start > r->start) {
  152. struct map *t = l;
  153. l = r;
  154. r = t;
  155. }
  156. if (l->end > r->start)
  157. return 1;
  158. return 0;
  159. }
  160. size_t map__fprintf(struct map *self, FILE *fp)
  161. {
  162. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  163. self->start, self->end, self->pgoff, self->dso->name);
  164. }