map.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = vdso__map_ip;
  45. else
  46. self->map_ip = map__map_ip;
  47. }
  48. return self;
  49. out_delete:
  50. free(self);
  51. return NULL;
  52. }
  53. struct map *map__clone(struct map *self)
  54. {
  55. struct map *map = malloc(sizeof(*self));
  56. if (!map)
  57. return NULL;
  58. memcpy(map, self, sizeof(*self));
  59. return map;
  60. }
  61. int map__overlap(struct map *l, struct map *r)
  62. {
  63. if (l->start > r->start) {
  64. struct map *t = l;
  65. l = r;
  66. r = t;
  67. }
  68. if (l->end > r->start)
  69. return 1;
  70. return 0;
  71. }
  72. size_t map__fprintf(struct map *self, FILE *fp)
  73. {
  74. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  75. self->start, self->end, self->pgoff, self->dso->name);
  76. }