map.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. pr_warning("Failed to open %s, continuing without symbols\n",
  71. self->dso->long_name);
  72. return NULL;
  73. } else if (nr == 0) {
  74. const char *name = self->dso->long_name;
  75. const size_t len = strlen(name);
  76. const size_t real_len = len - sizeof(DSO__DELETED);
  77. if (len > sizeof(DSO__DELETED) &&
  78. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  79. pr_warning("%.*s was updated, restart the long running apps that use it!\n",
  80. (int)real_len, name);
  81. } else {
  82. pr_warning("no symbols found in %s, maybe install a debug package?\n", name);
  83. }
  84. return NULL;
  85. }
  86. }
  87. return self->dso->find_symbol(self->dso, ip);
  88. }
  89. struct map *map__clone(struct map *self)
  90. {
  91. struct map *map = malloc(sizeof(*self));
  92. if (!map)
  93. return NULL;
  94. memcpy(map, self, sizeof(*self));
  95. return map;
  96. }
  97. int map__overlap(struct map *l, struct map *r)
  98. {
  99. if (l->start > r->start) {
  100. struct map *t = l;
  101. l = r;
  102. r = t;
  103. }
  104. if (l->end > r->start)
  105. return 1;
  106. return 0;
  107. }
  108. size_t map__fprintf(struct map *self, FILE *fp)
  109. {
  110. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  111. self->start, self->end, self->pgoff, self->dso->name);
  112. }