map.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "event.h"
  2. #include "symbol.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. static inline int is_anon_memory(const char *filename)
  7. {
  8. return strcmp(filename, "//anon") == 0;
  9. }
  10. static int strcommon(const char *pathname, char *cwd, int cwdlen)
  11. {
  12. int n = 0;
  13. while (n < cwdlen && pathname[n] == cwd[n])
  14. ++n;
  15. return n;
  16. }
  17. struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
  18. {
  19. struct map *self = malloc(sizeof(*self));
  20. if (self != NULL) {
  21. const char *filename = event->filename;
  22. char newfilename[PATH_MAX];
  23. int anon;
  24. if (cwd) {
  25. int n = strcommon(filename, cwd, cwdlen);
  26. if (n == cwdlen) {
  27. snprintf(newfilename, sizeof(newfilename),
  28. ".%s", filename + n);
  29. filename = newfilename;
  30. }
  31. }
  32. anon = is_anon_memory(filename);
  33. if (anon) {
  34. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  35. filename = newfilename;
  36. }
  37. self->start = event->start;
  38. self->end = event->start + event->len;
  39. self->pgoff = event->pgoff;
  40. self->dso = dsos__findnew(filename);
  41. if (self->dso == NULL)
  42. goto out_delete;
  43. if (self->dso == vdso || anon)
  44. self->map_ip = self->unmap_ip = identity__map_ip;
  45. else {
  46. self->map_ip = map__map_ip;
  47. self->unmap_ip = map__unmap_ip;
  48. }
  49. }
  50. return self;
  51. out_delete:
  52. free(self);
  53. return NULL;
  54. }
  55. struct map *map__clone(struct map *self)
  56. {
  57. struct map *map = malloc(sizeof(*self));
  58. if (!map)
  59. return NULL;
  60. memcpy(map, self, sizeof(*self));
  61. return map;
  62. }
  63. int map__overlap(struct map *l, struct map *r)
  64. {
  65. if (l->start > r->start) {
  66. struct map *t = l;
  67. l = r;
  68. r = t;
  69. }
  70. if (l->end > r->start)
  71. return 1;
  72. return 0;
  73. }
  74. size_t map__fprintf(struct map *self, FILE *fp)
  75. {
  76. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  77. self->start, self->end, self->pgoff, self->dso->name);
  78. }