session.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. self->unknown_events = 0;
  56. map_groups__init(&self->kmaps);
  57. if (perf_session__create_kernel_maps(self) < 0)
  58. goto out_delete;
  59. if (mode == O_RDONLY && perf_session__open(self, force) < 0)
  60. goto out_delete;
  61. self->sample_type = perf_header__sample_type(&self->header);
  62. out:
  63. return self;
  64. out_free:
  65. free(self);
  66. return NULL;
  67. out_delete:
  68. perf_session__delete(self);
  69. return NULL;
  70. }
  71. void perf_session__delete(struct perf_session *self)
  72. {
  73. perf_header__exit(&self->header);
  74. close(self->fd);
  75. free(self->cwd);
  76. free(self);
  77. }
  78. static bool symbol__match_parent_regex(struct symbol *sym)
  79. {
  80. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  81. return 1;
  82. return 0;
  83. }
  84. struct symbol **perf_session__resolve_callchain(struct perf_session *self,
  85. struct thread *thread,
  86. struct ip_callchain *chain,
  87. struct symbol **parent)
  88. {
  89. u8 cpumode = PERF_RECORD_MISC_USER;
  90. struct symbol **syms = NULL;
  91. unsigned int i;
  92. if (symbol_conf.use_callchain) {
  93. syms = calloc(chain->nr, sizeof(*syms));
  94. if (!syms) {
  95. fprintf(stderr, "Can't allocate memory for symbols\n");
  96. exit(-1);
  97. }
  98. }
  99. for (i = 0; i < chain->nr; i++) {
  100. u64 ip = chain->ips[i];
  101. struct addr_location al;
  102. if (ip >= PERF_CONTEXT_MAX) {
  103. switch (ip) {
  104. case PERF_CONTEXT_HV:
  105. cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
  106. case PERF_CONTEXT_KERNEL:
  107. cpumode = PERF_RECORD_MISC_KERNEL; break;
  108. case PERF_CONTEXT_USER:
  109. cpumode = PERF_RECORD_MISC_USER; break;
  110. default:
  111. break;
  112. }
  113. continue;
  114. }
  115. thread__find_addr_location(thread, self, cpumode,
  116. MAP__FUNCTION, ip, &al, NULL);
  117. if (al.sym != NULL) {
  118. if (sort__has_parent && !*parent &&
  119. symbol__match_parent_regex(al.sym))
  120. *parent = al.sym;
  121. if (!symbol_conf.use_callchain)
  122. break;
  123. syms[i] = al.sym;
  124. }
  125. }
  126. return syms;
  127. }
  128. static int process_event_stub(event_t *event __used,
  129. struct perf_session *session __used)
  130. {
  131. dump_printf(": unhandled!\n");
  132. return 0;
  133. }
  134. static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
  135. {
  136. if (handler->sample == NULL)
  137. handler->sample = process_event_stub;
  138. if (handler->mmap == NULL)
  139. handler->mmap = process_event_stub;
  140. if (handler->comm == NULL)
  141. handler->comm = process_event_stub;
  142. if (handler->fork == NULL)
  143. handler->fork = process_event_stub;
  144. if (handler->exit == NULL)
  145. handler->exit = process_event_stub;
  146. if (handler->lost == NULL)
  147. handler->lost = process_event_stub;
  148. if (handler->read == NULL)
  149. handler->read = process_event_stub;
  150. if (handler->throttle == NULL)
  151. handler->throttle = process_event_stub;
  152. if (handler->unthrottle == NULL)
  153. handler->unthrottle = process_event_stub;
  154. }
  155. static const char *event__name[] = {
  156. [0] = "TOTAL",
  157. [PERF_RECORD_MMAP] = "MMAP",
  158. [PERF_RECORD_LOST] = "LOST",
  159. [PERF_RECORD_COMM] = "COMM",
  160. [PERF_RECORD_EXIT] = "EXIT",
  161. [PERF_RECORD_THROTTLE] = "THROTTLE",
  162. [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
  163. [PERF_RECORD_FORK] = "FORK",
  164. [PERF_RECORD_READ] = "READ",
  165. [PERF_RECORD_SAMPLE] = "SAMPLE",
  166. };
  167. unsigned long event__total[PERF_RECORD_MAX];
  168. void event__print_totals(void)
  169. {
  170. int i;
  171. for (i = 0; i < PERF_RECORD_MAX; ++i)
  172. pr_info("%10s events: %10ld\n",
  173. event__name[i], event__total[i]);
  174. }
  175. static int perf_session__process_event(struct perf_session *self,
  176. event_t *event,
  177. struct perf_event_ops *ops,
  178. unsigned long offset, unsigned long head)
  179. {
  180. trace_event(event);
  181. if (event->header.type < PERF_RECORD_MAX) {
  182. dump_printf("%p [%p]: PERF_RECORD_%s",
  183. (void *)(offset + head),
  184. (void *)(long)(event->header.size),
  185. event__name[event->header.type]);
  186. ++event__total[0];
  187. ++event__total[event->header.type];
  188. }
  189. switch (event->header.type) {
  190. case PERF_RECORD_SAMPLE:
  191. return ops->sample(event, self);
  192. case PERF_RECORD_MMAP:
  193. return ops->mmap(event, self);
  194. case PERF_RECORD_COMM:
  195. return ops->comm(event, self);
  196. case PERF_RECORD_FORK:
  197. return ops->fork(event, self);
  198. case PERF_RECORD_EXIT:
  199. return ops->exit(event, self);
  200. case PERF_RECORD_LOST:
  201. return ops->lost(event, self);
  202. case PERF_RECORD_READ:
  203. return ops->read(event, self);
  204. case PERF_RECORD_THROTTLE:
  205. return ops->throttle(event, self);
  206. case PERF_RECORD_UNTHROTTLE:
  207. return ops->unthrottle(event, self);
  208. default:
  209. self->unknown_events++;
  210. return -1;
  211. }
  212. }
  213. int perf_header__read_build_ids(int input, u64 offset, u64 size)
  214. {
  215. struct build_id_event bev;
  216. char filename[PATH_MAX];
  217. u64 limit = offset + size;
  218. int err = -1;
  219. while (offset < limit) {
  220. struct dso *dso;
  221. ssize_t len;
  222. if (read(input, &bev, sizeof(bev)) != sizeof(bev))
  223. goto out;
  224. len = bev.header.size - sizeof(bev);
  225. if (read(input, filename, len) != len)
  226. goto out;
  227. dso = dsos__findnew(filename);
  228. if (dso != NULL)
  229. dso__set_build_id(dso, &bev.build_id);
  230. offset += bev.header.size;
  231. }
  232. err = 0;
  233. out:
  234. return err;
  235. }
  236. static struct thread *perf_session__register_idle_thread(struct perf_session *self)
  237. {
  238. struct thread *thread = perf_session__findnew(self, 0);
  239. if (thread == NULL || thread__set_comm(thread, "swapper")) {
  240. pr_err("problem inserting idle task.\n");
  241. thread = NULL;
  242. }
  243. return thread;
  244. }
  245. int perf_session__process_events(struct perf_session *self,
  246. struct perf_event_ops *ops)
  247. {
  248. int err;
  249. unsigned long head, shift;
  250. unsigned long offset = 0;
  251. size_t page_size;
  252. event_t *event;
  253. uint32_t size;
  254. char *buf;
  255. if (perf_session__register_idle_thread(self) == NULL)
  256. return -ENOMEM;
  257. perf_event_ops__fill_defaults(ops);
  258. page_size = getpagesize();
  259. head = self->header.data_offset;
  260. if (!symbol_conf.full_paths) {
  261. char bf[PATH_MAX];
  262. if (getcwd(bf, sizeof(bf)) == NULL) {
  263. err = -errno;
  264. out_getcwd_err:
  265. pr_err("failed to get the current directory\n");
  266. goto out_err;
  267. }
  268. self->cwd = strdup(bf);
  269. if (self->cwd == NULL) {
  270. err = -ENOMEM;
  271. goto out_getcwd_err;
  272. }
  273. self->cwdlen = strlen(self->cwd);
  274. }
  275. shift = page_size * (head / page_size);
  276. offset += shift;
  277. head -= shift;
  278. remap:
  279. buf = mmap(NULL, page_size * self->mmap_window, PROT_READ,
  280. MAP_SHARED, self->fd, offset);
  281. if (buf == MAP_FAILED) {
  282. pr_err("failed to mmap file\n");
  283. err = -errno;
  284. goto out_err;
  285. }
  286. more:
  287. event = (event_t *)(buf + head);
  288. size = event->header.size;
  289. if (size == 0)
  290. size = 8;
  291. if (head + event->header.size >= page_size * self->mmap_window) {
  292. int munmap_ret;
  293. shift = page_size * (head / page_size);
  294. munmap_ret = munmap(buf, page_size * self->mmap_window);
  295. assert(munmap_ret == 0);
  296. offset += shift;
  297. head -= shift;
  298. goto remap;
  299. }
  300. size = event->header.size;
  301. dump_printf("\n%p [%p]: event: %d\n",
  302. (void *)(offset + head),
  303. (void *)(long)event->header.size,
  304. event->header.type);
  305. if (size == 0 ||
  306. perf_session__process_event(self, event, ops, offset, head) < 0) {
  307. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  308. (void *)(offset + head),
  309. (void *)(long)(event->header.size),
  310. event->header.type);
  311. /*
  312. * assume we lost track of the stream, check alignment, and
  313. * increment a single u64 in the hope to catch on again 'soon'.
  314. */
  315. if (unlikely(head & 7))
  316. head &= ~7ULL;
  317. size = 8;
  318. }
  319. head += size;
  320. if (offset + head >= self->header.data_offset + self->header.data_size)
  321. goto done;
  322. if (offset + head < self->size)
  323. goto more;
  324. done:
  325. err = 0;
  326. out_err:
  327. return err;
  328. }
  329. bool perf_session__has_traces(struct perf_session *self, const char *msg)
  330. {
  331. if (!(self->sample_type & PERF_SAMPLE_RAW)) {
  332. pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
  333. return false;
  334. }
  335. return true;
  336. }
  337. int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self,
  338. const char *symbol_name,
  339. u64 addr)
  340. {
  341. char *bracket;
  342. self->ref_reloc_sym.name = strdup(symbol_name);
  343. if (self->ref_reloc_sym.name == NULL)
  344. return -ENOMEM;
  345. bracket = strchr(self->ref_reloc_sym.name, ']');
  346. if (bracket)
  347. *bracket = '\0';
  348. self->ref_reloc_sym.addr = addr;
  349. return 0;
  350. }
  351. static u64 map__reloc_map_ip(struct map *map, u64 ip)
  352. {
  353. return ip + (s64)map->pgoff;
  354. }
  355. static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
  356. {
  357. return ip - (s64)map->pgoff;
  358. }
  359. void perf_session__reloc_vmlinux_maps(struct perf_session *self,
  360. u64 unrelocated_addr)
  361. {
  362. enum map_type type;
  363. s64 reloc = unrelocated_addr - self->ref_reloc_sym.addr;
  364. if (!reloc)
  365. return;
  366. for (type = 0; type < MAP__NR_TYPES; ++type) {
  367. struct map *map = self->vmlinux_maps[type];
  368. map->map_ip = map__reloc_map_ip;
  369. map->unmap_ip = map__reloc_unmap_ip;
  370. map->pgoff = reloc;
  371. }
  372. }