map.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct symbol *
  64. map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter)
  65. {
  66. if (!self->dso->loaded) {
  67. int nr = dso__load(self->dso, self, filter);
  68. if (nr < 0) {
  69. pr_warning("Failed to open %s, continuing without symbols\n",
  70. self->dso->long_name);
  71. return NULL;
  72. } else if (nr == 0) {
  73. pr_warning("No symbols found in %s, maybe install a debug package?\n",
  74. self->dso->long_name);
  75. return NULL;
  76. }
  77. }
  78. return self->dso->find_symbol(self->dso, ip);
  79. }
  80. struct map *map__clone(struct map *self)
  81. {
  82. struct map *map = malloc(sizeof(*self));
  83. if (!map)
  84. return NULL;
  85. memcpy(map, self, sizeof(*self));
  86. return map;
  87. }
  88. int map__overlap(struct map *l, struct map *r)
  89. {
  90. if (l->start > r->start) {
  91. struct map *t = l;
  92. l = r;
  93. r = t;
  94. }
  95. if (l->end > r->start)
  96. return 1;
  97. return 0;
  98. }
  99. size_t map__fprintf(struct map *self, FILE *fp)
  100. {
  101. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  102. self->start, self->end, self->pgoff, self->dso->name);
  103. }