map.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, u64 start, u64 end, u64 pgoff,
  19. struct dso *dso)
  20. {
  21. self->start = start;
  22. self->end = end;
  23. self->pgoff = pgoff;
  24. self->dso = dso;
  25. self->map_ip = map__map_ip;
  26. self->unmap_ip = map__unmap_ip;
  27. RB_CLEAR_NODE(&self->rb_node);
  28. }
  29. struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
  30. {
  31. struct map *self = malloc(sizeof(*self));
  32. if (self != NULL) {
  33. const char *filename = event->filename;
  34. char newfilename[PATH_MAX];
  35. struct dso *dso;
  36. int anon;
  37. if (cwd) {
  38. int n = strcommon(filename, cwd, cwdlen);
  39. if (n == cwdlen) {
  40. snprintf(newfilename, sizeof(newfilename),
  41. ".%s", filename + n);
  42. filename = newfilename;
  43. }
  44. }
  45. anon = is_anon_memory(filename);
  46. if (anon) {
  47. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  48. filename = newfilename;
  49. }
  50. dso = dsos__findnew(filename);
  51. if (dso == NULL)
  52. goto out_delete;
  53. map__init(self, event->start, event->start + event->len,
  54. event->pgoff, dso);
  55. if (self->dso == vdso || anon)
  56. self->map_ip = self->unmap_ip = identity__map_ip;
  57. }
  58. return self;
  59. out_delete:
  60. free(self);
  61. return NULL;
  62. }
  63. #define DSO__DELETED "(deleted)"
  64. struct symbol *
  65. map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter)
  66. {
  67. if (!self->dso->loaded) {
  68. int nr = dso__load(self->dso, self, filter);
  69. if (nr < 0) {
  70. if (self->dso->has_build_id) {
  71. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  72. build_id__sprintf(self->dso->build_id,
  73. sizeof(self->dso->build_id),
  74. sbuild_id);
  75. pr_warning("%s with build id %s not found",
  76. self->dso->long_name, sbuild_id);
  77. } else
  78. pr_warning("Failed to open %s",
  79. self->dso->long_name);
  80. pr_warning(", continuing without symbols\n");
  81. return NULL;
  82. } else if (nr == 0) {
  83. const char *name = self->dso->long_name;
  84. const size_t len = strlen(name);
  85. const size_t real_len = len - sizeof(DSO__DELETED);
  86. if (len > sizeof(DSO__DELETED) &&
  87. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  88. pr_warning("%.*s was updated, restart the long running apps that use it!\n",
  89. (int)real_len, name);
  90. } else {
  91. pr_warning("no symbols found in %s, maybe install a debug package?\n", name);
  92. }
  93. return NULL;
  94. }
  95. }
  96. return self->dso->find_symbol(self->dso, ip);
  97. }
  98. struct map *map__clone(struct map *self)
  99. {
  100. struct map *map = malloc(sizeof(*self));
  101. if (!map)
  102. return NULL;
  103. memcpy(map, self, sizeof(*self));
  104. return map;
  105. }
  106. int map__overlap(struct map *l, struct map *r)
  107. {
  108. if (l->start > r->start) {
  109. struct map *t = l;
  110. l = r;
  111. r = t;
  112. }
  113. if (l->end > r->start)
  114. return 1;
  115. return 0;
  116. }
  117. size_t map__fprintf(struct map *self, FILE *fp)
  118. {
  119. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  120. self->start, self->end, self->pgoff, self->dso->name);
  121. }