session.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <linux/kernel.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include "session.h"
  5. #include "sort.h"
  6. #include "util.h"
  7. static int perf_session__open(struct perf_session *self, bool force)
  8. {
  9. struct stat input_stat;
  10. self->fd = open(self->filename, O_RDONLY);
  11. if (self->fd < 0) {
  12. pr_err("failed to open file: %s", self->filename);
  13. if (!strcmp(self->filename, "perf.data"))
  14. pr_err(" (try 'perf record' first)");
  15. pr_err("\n");
  16. return -errno;
  17. }
  18. if (fstat(self->fd, &input_stat) < 0)
  19. goto out_close;
  20. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  21. pr_err("file %s not owned by current user or root\n",
  22. self->filename);
  23. goto out_close;
  24. }
  25. if (!input_stat.st_size) {
  26. pr_info("zero-sized file (%s), nothing to do!\n",
  27. self->filename);
  28. goto out_close;
  29. }
  30. if (perf_header__read(&self->header, self->fd) < 0) {
  31. pr_err("incompatible file format");
  32. goto out_close;
  33. }
  34. self->size = input_stat.st_size;
  35. return 0;
  36. out_close:
  37. close(self->fd);
  38. self->fd = -1;
  39. return -1;
  40. }
  41. struct perf_session *perf_session__new(const char *filename, int mode, bool force)
  42. {
  43. size_t len = filename ? strlen(filename) + 1 : 0;
  44. struct perf_session *self = zalloc(sizeof(*self) + len);
  45. if (self == NULL)
  46. goto out;
  47. if (perf_header__init(&self->header) < 0)
  48. goto out_free;
  49. memcpy(self->filename, filename, len);
  50. self->threads = RB_ROOT;
  51. self->last_match = NULL;
  52. self->mmap_window = 32;
  53. self->cwd = NULL;
  54. self->cwdlen = 0;
  55. map_groups__init(&self->kmaps);
  56. if (perf_session__create_kernel_maps(self) < 0)
  57. goto out_delete;
  58. if (mode == O_RDONLY && perf_session__open(self, force) < 0)
  59. goto out_delete;
  60. out:
  61. return self;
  62. out_free:
  63. free(self);
  64. return NULL;
  65. out_delete:
  66. perf_session__delete(self);
  67. return NULL;
  68. }
  69. void perf_session__delete(struct perf_session *self)
  70. {
  71. perf_header__exit(&self->header);
  72. close(self->fd);
  73. free(self->cwd);
  74. free(self);
  75. }
  76. static bool symbol__match_parent_regex(struct symbol *sym)
  77. {
  78. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  79. return 1;
  80. return 0;
  81. }
  82. struct symbol **perf_session__resolve_callchain(struct perf_session *self,
  83. struct thread *thread,
  84. struct ip_callchain *chain,
  85. struct symbol **parent)
  86. {
  87. u8 cpumode = PERF_RECORD_MISC_USER;
  88. struct symbol **syms = NULL;
  89. unsigned int i;
  90. if (symbol_conf.use_callchain) {
  91. syms = calloc(chain->nr, sizeof(*syms));
  92. if (!syms) {
  93. fprintf(stderr, "Can't allocate memory for symbols\n");
  94. exit(-1);
  95. }
  96. }
  97. for (i = 0; i < chain->nr; i++) {
  98. u64 ip = chain->ips[i];
  99. struct addr_location al;
  100. if (ip >= PERF_CONTEXT_MAX) {
  101. switch (ip) {
  102. case PERF_CONTEXT_HV:
  103. cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
  104. case PERF_CONTEXT_KERNEL:
  105. cpumode = PERF_RECORD_MISC_KERNEL; break;
  106. case PERF_CONTEXT_USER:
  107. cpumode = PERF_RECORD_MISC_USER; break;
  108. default:
  109. break;
  110. }
  111. continue;
  112. }
  113. thread__find_addr_location(thread, self, cpumode,
  114. MAP__FUNCTION, ip, &al, NULL);
  115. if (al.sym != NULL) {
  116. if (sort__has_parent && !*parent &&
  117. symbol__match_parent_regex(al.sym))
  118. *parent = al.sym;
  119. if (!symbol_conf.use_callchain)
  120. break;
  121. syms[i] = al.sym;
  122. }
  123. }
  124. return syms;
  125. }