map.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. struct symbol *map__find_symbol(struct map *self, u64 addr,
  89. symbol_filter_t filter)
  90. {
  91. if (!dso__loaded(self->dso, self->type)) {
  92. int nr = dso__load(self->dso, self, filter);
  93. if (nr < 0) {
  94. if (self->dso->has_build_id) {
  95. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  96. build_id__sprintf(self->dso->build_id,
  97. sizeof(self->dso->build_id),
  98. sbuild_id);
  99. pr_warning("%s with build id %s not found",
  100. self->dso->long_name, sbuild_id);
  101. } else
  102. pr_warning("Failed to open %s",
  103. self->dso->long_name);
  104. pr_warning(", continuing without symbols\n");
  105. return NULL;
  106. } else if (nr == 0) {
  107. const char *name = self->dso->long_name;
  108. const size_t len = strlen(name);
  109. const size_t real_len = len - sizeof(DSO__DELETED);
  110. if (len > sizeof(DSO__DELETED) &&
  111. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  112. pr_warning("%.*s was updated, restart the long running apps that use it!\n",
  113. (int)real_len, name);
  114. } else {
  115. pr_warning("no symbols found in %s, maybe install a debug package?\n", name);
  116. }
  117. return NULL;
  118. }
  119. }
  120. return self->dso->find_symbol(self->dso, self->type, addr);
  121. }
  122. struct map *map__clone(struct map *self)
  123. {
  124. struct map *map = malloc(sizeof(*self));
  125. if (!map)
  126. return NULL;
  127. memcpy(map, self, sizeof(*self));
  128. return map;
  129. }
  130. int map__overlap(struct map *l, struct map *r)
  131. {
  132. if (l->start > r->start) {
  133. struct map *t = l;
  134. l = r;
  135. r = t;
  136. }
  137. if (l->end > r->start)
  138. return 1;
  139. return 0;
  140. }
  141. size_t map__fprintf(struct map *self, FILE *fp)
  142. {
  143. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  144. self->start, self->end, self->pgoff, self->dso->name);
  145. }