builtin-report.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. #include "util/util.h"
  2. #include <libelf.h>
  3. #include <gelf.h>
  4. #include <elf.h>
  5. #include "util/list.h"
  6. #include "util/rbtree.h"
  7. #include "perf.h"
  8. #include "util/parse-options.h"
  9. #include "util/parse-events.h"
  10. #define SHOW_KERNEL 1
  11. #define SHOW_USER 2
  12. #define SHOW_HV 4
  13. static char const *input_name = "output.perf";
  14. static int input;
  15. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  16. static int dump_trace = 0;
  17. static unsigned long page_size;
  18. static unsigned long mmap_window = 32;
  19. const char *perf_event_names[] = {
  20. [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
  21. [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
  22. [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
  23. };
  24. struct ip_event {
  25. struct perf_event_header header;
  26. __u64 ip;
  27. __u32 pid, tid;
  28. };
  29. struct mmap_event {
  30. struct perf_event_header header;
  31. __u32 pid, tid;
  32. __u64 start;
  33. __u64 len;
  34. __u64 pgoff;
  35. char filename[PATH_MAX];
  36. };
  37. struct comm_event {
  38. struct perf_event_header header;
  39. __u32 pid,tid;
  40. char comm[16];
  41. };
  42. typedef union event_union {
  43. struct perf_event_header header;
  44. struct ip_event ip;
  45. struct mmap_event mmap;
  46. struct comm_event comm;
  47. } event_t;
  48. struct symbol {
  49. struct rb_node rb_node;
  50. uint64_t start;
  51. uint64_t end;
  52. char name[0];
  53. };
  54. static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
  55. {
  56. struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
  57. if (self != NULL) {
  58. self->start = start;
  59. self->end = start + len;
  60. strcpy(self->name, name);
  61. }
  62. return self;
  63. }
  64. static void symbol__delete(struct symbol *self)
  65. {
  66. free(self);
  67. }
  68. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  69. {
  70. return fprintf(fp, " %lx-%lx %s\n",
  71. self->start, self->end, self->name);
  72. }
  73. struct dso {
  74. struct list_head node;
  75. struct rb_root syms;
  76. char name[0];
  77. };
  78. static struct dso *dso__new(const char *name)
  79. {
  80. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  81. if (self != NULL) {
  82. strcpy(self->name, name);
  83. self->syms = RB_ROOT;
  84. }
  85. return self;
  86. }
  87. static void dso__delete_symbols(struct dso *self)
  88. {
  89. struct symbol *pos;
  90. struct rb_node *next = rb_first(&self->syms);
  91. while (next) {
  92. pos = rb_entry(next, struct symbol, rb_node);
  93. next = rb_next(&pos->rb_node);
  94. symbol__delete(pos);
  95. }
  96. }
  97. static void dso__delete(struct dso *self)
  98. {
  99. dso__delete_symbols(self);
  100. free(self);
  101. }
  102. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  103. {
  104. struct rb_node **p = &self->syms.rb_node;
  105. struct rb_node *parent = NULL;
  106. const uint64_t ip = sym->start;
  107. struct symbol *s;
  108. while (*p != NULL) {
  109. parent = *p;
  110. s = rb_entry(parent, struct symbol, rb_node);
  111. if (ip < s->start)
  112. p = &(*p)->rb_left;
  113. else
  114. p = &(*p)->rb_right;
  115. }
  116. rb_link_node(&sym->rb_node, parent, p);
  117. rb_insert_color(&sym->rb_node, &self->syms);
  118. }
  119. static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
  120. {
  121. if (self == NULL)
  122. return NULL;
  123. struct rb_node *n = self->syms.rb_node;
  124. while (n) {
  125. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  126. if (ip < s->start)
  127. n = n->rb_left;
  128. else if (ip > s->end)
  129. n = n->rb_right;
  130. else
  131. return s;
  132. }
  133. return NULL;
  134. }
  135. /**
  136. * elf_symtab__for_each_symbol - iterate thru all the symbols
  137. *
  138. * @self: struct elf_symtab instance to iterate
  139. * @index: uint32_t index
  140. * @sym: GElf_Sym iterator
  141. */
  142. #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
  143. for (index = 0, gelf_getsym(syms, index, &sym);\
  144. index < nr_syms; \
  145. index++, gelf_getsym(syms, index, &sym))
  146. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  147. {
  148. return GELF_ST_TYPE(sym->st_info);
  149. }
  150. static inline int elf_sym__is_function(const GElf_Sym *sym)
  151. {
  152. return elf_sym__type(sym) == STT_FUNC &&
  153. sym->st_name != 0 &&
  154. sym->st_shndx != SHN_UNDEF;
  155. }
  156. static inline const char *elf_sym__name(const GElf_Sym *sym,
  157. const Elf_Data *symstrs)
  158. {
  159. return symstrs->d_buf + sym->st_name;
  160. }
  161. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  162. GElf_Shdr *shp, const char *name,
  163. size_t *index)
  164. {
  165. Elf_Scn *sec = NULL;
  166. size_t cnt = 1;
  167. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  168. char *str;
  169. gelf_getshdr(sec, shp);
  170. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  171. if (!strcmp(name, str)) {
  172. if (index)
  173. *index = cnt;
  174. break;
  175. }
  176. ++cnt;
  177. }
  178. return sec;
  179. }
  180. static int dso__load(struct dso *self)
  181. {
  182. int fd = open(self->name, O_RDONLY), err = -1;
  183. if (fd == -1)
  184. return -1;
  185. Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  186. if (elf == NULL) {
  187. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  188. __func__, self->name);
  189. goto out_close;
  190. }
  191. GElf_Ehdr ehdr;
  192. if (gelf_getehdr(elf, &ehdr) == NULL) {
  193. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  194. goto out_elf_end;
  195. }
  196. GElf_Shdr shdr;
  197. Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  198. if (sec == NULL)
  199. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  200. if (sec == NULL)
  201. goto out_elf_end;
  202. Elf_Data *syms = elf_getdata(sec, NULL);
  203. if (syms == NULL)
  204. goto out_elf_end;
  205. sec = elf_getscn(elf, shdr.sh_link);
  206. if (sec == NULL)
  207. goto out_elf_end;
  208. Elf_Data *symstrs = elf_getdata(sec, NULL);
  209. if (symstrs == NULL)
  210. goto out_elf_end;
  211. const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
  212. GElf_Sym sym;
  213. uint32_t index;
  214. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  215. struct symbol *f;
  216. if (!elf_sym__is_function(&sym))
  217. continue;
  218. sec = elf_getscn(elf, sym.st_shndx);
  219. if (!sec)
  220. goto out_elf_end;
  221. gelf_getshdr(sec, &shdr);
  222. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  223. f = symbol__new(sym.st_value, sym.st_size,
  224. elf_sym__name(&sym, symstrs));
  225. if (!f)
  226. goto out_elf_end;
  227. dso__insert_symbol(self, f);
  228. }
  229. err = 0;
  230. out_elf_end:
  231. elf_end(elf);
  232. out_close:
  233. close(fd);
  234. return err;
  235. }
  236. static size_t dso__fprintf(struct dso *self, FILE *fp)
  237. {
  238. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  239. struct rb_node *nd;
  240. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  241. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  242. ret += symbol__fprintf(pos, fp);
  243. }
  244. return ret;
  245. }
  246. static LIST_HEAD(dsos);
  247. static struct dso *kernel_dso;
  248. static void dsos__add(struct dso *dso)
  249. {
  250. list_add_tail(&dso->node, &dsos);
  251. }
  252. static struct dso *dsos__find(const char *name)
  253. {
  254. struct dso *pos;
  255. list_for_each_entry(pos, &dsos, node)
  256. if (strcmp(pos->name, name) == 0)
  257. return pos;
  258. return NULL;
  259. }
  260. static struct dso *dsos__findnew(const char *name)
  261. {
  262. struct dso *dso = dsos__find(name);
  263. if (dso == NULL) {
  264. dso = dso__new(name);
  265. if (dso != NULL && dso__load(dso) < 0)
  266. goto out_delete_dso;
  267. dsos__add(dso);
  268. }
  269. return dso;
  270. out_delete_dso:
  271. dso__delete(dso);
  272. return NULL;
  273. }
  274. void dsos__fprintf(FILE *fp)
  275. {
  276. struct dso *pos;
  277. list_for_each_entry(pos, &dsos, node)
  278. dso__fprintf(pos, fp);
  279. }
  280. static int hex(char ch)
  281. {
  282. if ((ch >= '0') && (ch <= '9'))
  283. return ch - '0';
  284. if ((ch >= 'a') && (ch <= 'f'))
  285. return ch - 'a' + 10;
  286. if ((ch >= 'A') && (ch <= 'F'))
  287. return ch - 'A' + 10;
  288. return -1;
  289. }
  290. /*
  291. * While we find nice hex chars, build a long_val.
  292. * Return number of chars processed.
  293. */
  294. int hex2long(char *ptr, unsigned long *long_val)
  295. {
  296. const char *p = ptr;
  297. *long_val = 0;
  298. while (*p) {
  299. const int hex_val = hex(*p);
  300. if (hex_val < 0)
  301. break;
  302. *long_val = (*long_val << 4) | hex_val;
  303. p++;
  304. }
  305. return p - ptr;
  306. }
  307. static int load_kallsyms(void)
  308. {
  309. kernel_dso = dso__new("[kernel]");
  310. if (kernel_dso == NULL)
  311. return -1;
  312. FILE *file = fopen("/proc/kallsyms", "r");
  313. if (file == NULL)
  314. goto out_delete_dso;
  315. char *line = NULL;
  316. size_t n;
  317. while (!feof(file)) {
  318. unsigned long start;
  319. int line_len = getline(&line, &n, file);
  320. if (line_len < 0)
  321. break;
  322. if (!line)
  323. goto out_delete_dso;
  324. line[--line_len] = '\0'; /* \n */
  325. int len = hex2long(line, &start);
  326. len += 3; /* ' t ' */
  327. if (len >= line_len)
  328. continue;
  329. /*
  330. * Well fix up the end later, when we have all sorted.
  331. */
  332. struct symbol *sym = symbol__new(start, 0xdead, line + len);
  333. if (sym == NULL)
  334. goto out_delete_dso;
  335. dso__insert_symbol(kernel_dso, sym);
  336. }
  337. /*
  338. * Now that we have all sorted out, just set the ->end of all
  339. * symbols
  340. */
  341. struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
  342. if (prevnd == NULL)
  343. goto out_delete_line;
  344. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  345. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  346. *curr = rb_entry(nd, struct symbol, rb_node);
  347. prev->end = curr->start - 1;
  348. prevnd = nd;
  349. }
  350. dsos__add(kernel_dso);
  351. free(line);
  352. fclose(file);
  353. return 0;
  354. out_delete_line:
  355. free(line);
  356. out_delete_dso:
  357. dso__delete(kernel_dso);
  358. return -1;
  359. }
  360. struct map {
  361. struct list_head node;
  362. uint64_t start;
  363. uint64_t end;
  364. uint64_t pgoff;
  365. struct dso *dso;
  366. };
  367. static struct map *map__new(struct mmap_event *event)
  368. {
  369. struct map *self = malloc(sizeof(*self));
  370. if (self != NULL) {
  371. self->start = event->start;
  372. self->end = event->start + event->len;
  373. self->pgoff = event->pgoff;
  374. self->dso = dsos__findnew(event->filename);
  375. if (self->dso == NULL)
  376. goto out_delete;
  377. }
  378. return self;
  379. out_delete:
  380. free(self);
  381. return NULL;
  382. }
  383. static size_t map__fprintf(struct map *self, FILE *fp)
  384. {
  385. return fprintf(fp, " %lx-%lx %lx %s\n",
  386. self->start, self->end, self->pgoff, self->dso->name);
  387. }
  388. struct thread;
  389. static const char *thread__name(struct thread *self, char *bf, size_t size);
  390. struct symhist {
  391. struct rb_node rb_node;
  392. struct dso *dso;
  393. struct symbol *sym;
  394. struct thread *thread;
  395. uint64_t ip;
  396. uint32_t count;
  397. char level;
  398. };
  399. static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
  400. struct thread *thread, struct dso *dso,
  401. char level)
  402. {
  403. struct symhist *self = malloc(sizeof(*self));
  404. if (self != NULL) {
  405. self->sym = sym;
  406. self->thread = thread;
  407. self->ip = ip;
  408. self->dso = dso;
  409. self->level = level;
  410. self->count = 1;
  411. }
  412. return self;
  413. }
  414. void symhist__delete(struct symhist *self)
  415. {
  416. free(self);
  417. }
  418. static void symhist__inc(struct symhist *self)
  419. {
  420. ++self->count;
  421. }
  422. static size_t
  423. symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
  424. {
  425. char bf[32];
  426. size_t ret;
  427. if (total_samples)
  428. ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
  429. else
  430. ret = fprintf(fp, "%12d", self->count);
  431. ret += fprintf(fp, "%14s [%c] %#018llx ",
  432. thread__name(self->thread, bf, sizeof(bf)),
  433. self->level, (unsigned long long)self->ip);
  434. if (self->level != '.')
  435. ret += fprintf(fp, "%s\n",
  436. self->sym ? self->sym->name : "<unknown>");
  437. else
  438. ret += fprintf(fp, "%s: %s\n",
  439. self->dso ? self->dso->name : "<unknown>",
  440. self->sym ? self->sym->name : "<unknown>");
  441. return ret;
  442. }
  443. struct thread {
  444. struct rb_node rb_node;
  445. struct list_head maps;
  446. struct rb_root symhists;
  447. pid_t pid;
  448. char *comm;
  449. };
  450. static const char *thread__name(struct thread *self, char *bf, size_t size)
  451. {
  452. if (self->comm)
  453. return self->comm;
  454. snprintf(bf, sizeof(bf), ":%u", self->pid);
  455. return bf;
  456. }
  457. static struct thread *thread__new(pid_t pid)
  458. {
  459. struct thread *self = malloc(sizeof(*self));
  460. if (self != NULL) {
  461. self->pid = pid;
  462. self->comm = NULL;
  463. INIT_LIST_HEAD(&self->maps);
  464. self->symhists = RB_ROOT;
  465. }
  466. return self;
  467. }
  468. static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
  469. uint64_t ip, struct dso *dso, char level)
  470. {
  471. struct rb_node **p = &self->symhists.rb_node;
  472. struct rb_node *parent = NULL;
  473. struct symhist *sh;
  474. while (*p != NULL) {
  475. parent = *p;
  476. sh = rb_entry(parent, struct symhist, rb_node);
  477. if (sh->sym == sym || ip == sh->ip) {
  478. symhist__inc(sh);
  479. return 0;
  480. }
  481. /* Handle unresolved symbols too */
  482. const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
  483. if (ip < start)
  484. p = &(*p)->rb_left;
  485. else
  486. p = &(*p)->rb_right;
  487. }
  488. sh = symhist__new(sym, ip, self, dso, level);
  489. if (sh == NULL)
  490. return -ENOMEM;
  491. rb_link_node(&sh->rb_node, parent, p);
  492. rb_insert_color(&sh->rb_node, &self->symhists);
  493. return 0;
  494. }
  495. static int thread__set_comm(struct thread *self, const char *comm)
  496. {
  497. self->comm = strdup(comm);
  498. return self->comm ? 0 : -ENOMEM;
  499. }
  500. size_t thread__maps_fprintf(struct thread *self, FILE *fp)
  501. {
  502. struct map *pos;
  503. size_t ret = 0;
  504. list_for_each_entry(pos, &self->maps, node)
  505. ret += map__fprintf(pos, fp);
  506. return ret;
  507. }
  508. static size_t thread__fprintf(struct thread *self, FILE *fp)
  509. {
  510. int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
  511. struct rb_node *nd;
  512. for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
  513. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  514. ret += symhist__fprintf(pos, 0, fp);
  515. }
  516. return ret;
  517. }
  518. static struct rb_root threads = RB_ROOT;
  519. static struct thread *threads__findnew(pid_t pid)
  520. {
  521. struct rb_node **p = &threads.rb_node;
  522. struct rb_node *parent = NULL;
  523. struct thread *th;
  524. while (*p != NULL) {
  525. parent = *p;
  526. th = rb_entry(parent, struct thread, rb_node);
  527. if (th->pid == pid)
  528. return th;
  529. if (pid < th->pid)
  530. p = &(*p)->rb_left;
  531. else
  532. p = &(*p)->rb_right;
  533. }
  534. th = thread__new(pid);
  535. if (th != NULL) {
  536. rb_link_node(&th->rb_node, parent, p);
  537. rb_insert_color(&th->rb_node, &threads);
  538. }
  539. return th;
  540. }
  541. static void thread__insert_map(struct thread *self, struct map *map)
  542. {
  543. list_add_tail(&map->node, &self->maps);
  544. }
  545. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  546. {
  547. if (self == NULL)
  548. return NULL;
  549. struct map *pos;
  550. list_for_each_entry(pos, &self->maps, node)
  551. if (ip >= pos->start && ip <= pos->end)
  552. return pos;
  553. return NULL;
  554. }
  555. void threads__fprintf(FILE *fp)
  556. {
  557. struct rb_node *nd;
  558. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  559. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  560. thread__fprintf(pos, fp);
  561. }
  562. }
  563. static struct rb_root global_symhists = RB_ROOT;
  564. static void threads__insert_symhist(struct symhist *sh)
  565. {
  566. struct rb_node **p = &global_symhists.rb_node;
  567. struct rb_node *parent = NULL;
  568. struct symhist *iter;
  569. while (*p != NULL) {
  570. parent = *p;
  571. iter = rb_entry(parent, struct symhist, rb_node);
  572. /* Reverse order */
  573. if (sh->count > iter->count)
  574. p = &(*p)->rb_left;
  575. else
  576. p = &(*p)->rb_right;
  577. }
  578. rb_link_node(&sh->rb_node, parent, p);
  579. rb_insert_color(&sh->rb_node, &global_symhists);
  580. }
  581. static void threads__sort_symhists(void)
  582. {
  583. struct rb_node *nd;
  584. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  585. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  586. struct rb_node *next = rb_first(&thread->symhists);
  587. while (next) {
  588. struct symhist *n = rb_entry(next, struct symhist,
  589. rb_node);
  590. next = rb_next(&n->rb_node);
  591. rb_erase(&n->rb_node, &thread->symhists);
  592. threads__insert_symhist(n);
  593. }
  594. }
  595. }
  596. static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
  597. {
  598. struct rb_node *nd;
  599. size_t ret = 0;
  600. for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
  601. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  602. ret += symhist__fprintf(pos, total_samples, fp);
  603. }
  604. return ret;
  605. }
  606. static int __cmd_report(void)
  607. {
  608. unsigned long offset = 0;
  609. unsigned long head = 0;
  610. struct stat stat;
  611. char *buf;
  612. event_t *event;
  613. int ret, rc = EXIT_FAILURE;
  614. uint32_t size;
  615. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  616. input = open(input_name, O_RDONLY);
  617. if (input < 0) {
  618. perror("failed to open file");
  619. exit(-1);
  620. }
  621. ret = fstat(input, &stat);
  622. if (ret < 0) {
  623. perror("failed to stat file");
  624. exit(-1);
  625. }
  626. if (!stat.st_size) {
  627. fprintf(stderr, "zero-sized file, nothing to do!\n");
  628. exit(0);
  629. }
  630. if (load_kallsyms() < 0) {
  631. perror("failed to open kallsyms");
  632. return EXIT_FAILURE;
  633. }
  634. remap:
  635. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  636. MAP_SHARED, input, offset);
  637. if (buf == MAP_FAILED) {
  638. perror("failed to mmap file");
  639. exit(-1);
  640. }
  641. more:
  642. event = (event_t *)(buf + head);
  643. size = event->header.size;
  644. if (!size)
  645. size = 8;
  646. if (head + event->header.size >= page_size * mmap_window) {
  647. unsigned long shift = page_size * (head / page_size);
  648. int ret;
  649. ret = munmap(buf, page_size * mmap_window);
  650. assert(ret == 0);
  651. offset += shift;
  652. head -= shift;
  653. goto remap;
  654. }
  655. size = event->header.size;
  656. if (!size)
  657. goto broken_event;
  658. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  659. char level;
  660. int show = 0;
  661. struct dso *dso = NULL;
  662. struct thread *thread = threads__findnew(event->ip.pid);
  663. uint64_t ip = event->ip.ip;
  664. if (dump_trace) {
  665. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  666. (void *)(offset + head),
  667. (void *)(long)(event->header.size),
  668. event->header.misc,
  669. event->ip.pid,
  670. (void *)event->ip.ip);
  671. }
  672. if (thread == NULL) {
  673. fprintf(stderr, "problem processing %d event, bailing out\n",
  674. event->header.type);
  675. goto done;
  676. }
  677. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  678. show = SHOW_KERNEL;
  679. level = 'k';
  680. dso = kernel_dso;
  681. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  682. show = SHOW_USER;
  683. level = '.';
  684. struct map *map = thread__find_map(thread, ip);
  685. if (map != NULL) {
  686. dso = map->dso;
  687. ip -= map->start + map->pgoff;
  688. }
  689. } else {
  690. show = SHOW_HV;
  691. level = 'H';
  692. }
  693. if (show & show_mask) {
  694. struct symbol *sym = dso__find_symbol(dso, ip);
  695. if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
  696. fprintf(stderr, "problem incrementing symbol count, bailing out\n");
  697. goto done;
  698. }
  699. }
  700. total++;
  701. } else switch (event->header.type) {
  702. case PERF_EVENT_MMAP: {
  703. struct thread *thread = threads__findnew(event->mmap.pid);
  704. struct map *map = map__new(&event->mmap);
  705. if (dump_trace) {
  706. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  707. (void *)(offset + head),
  708. (void *)(long)(event->header.size),
  709. (void *)event->mmap.start,
  710. (void *)event->mmap.len,
  711. (void *)event->mmap.pgoff,
  712. event->mmap.filename);
  713. }
  714. if (thread == NULL || map == NULL) {
  715. fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
  716. goto done;
  717. }
  718. thread__insert_map(thread, map);
  719. total_mmap++;
  720. break;
  721. }
  722. case PERF_EVENT_COMM: {
  723. struct thread *thread = threads__findnew(event->comm.pid);
  724. if (dump_trace) {
  725. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  726. (void *)(offset + head),
  727. (void *)(long)(event->header.size),
  728. event->comm.comm, event->comm.pid);
  729. }
  730. if (thread == NULL ||
  731. thread__set_comm(thread, event->comm.comm)) {
  732. fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
  733. goto done;
  734. }
  735. total_comm++;
  736. break;
  737. }
  738. default: {
  739. broken_event:
  740. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  741. (void *)(offset + head),
  742. (void *)(long)(event->header.size),
  743. event->header.type);
  744. total_unknown++;
  745. /*
  746. * assume we lost track of the stream, check alignment, and
  747. * increment a single u64 in the hope to catch on again 'soon'.
  748. */
  749. if (unlikely(head & 7))
  750. head &= ~7ULL;
  751. size = 8;
  752. }
  753. }
  754. head += size;
  755. if (offset + head < stat.st_size)
  756. goto more;
  757. rc = EXIT_SUCCESS;
  758. done:
  759. close(input);
  760. if (dump_trace) {
  761. fprintf(stderr, " IP events: %10ld\n", total);
  762. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  763. fprintf(stderr, " comm events: %10ld\n", total_comm);
  764. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  765. return 0;
  766. }
  767. threads__sort_symhists();
  768. threads__symhists_fprintf(total, stdout);
  769. return rc;
  770. }
  771. static const char * const report_usage[] = {
  772. "perf report [<options>] <command>",
  773. NULL
  774. };
  775. static const struct option options[] = {
  776. OPT_STRING('i', "input", &input_name, "file",
  777. "input file name"),
  778. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  779. "dump raw trace in ASCII"),
  780. OPT_END()
  781. };
  782. int cmd_report(int argc, const char **argv, const char *prefix)
  783. {
  784. elf_version(EV_CURRENT);
  785. page_size = getpagesize();
  786. parse_options(argc, argv, options, report_usage, 0);
  787. return __cmd_report();
  788. }