builtin-report.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 load_kallsyms(void)
  281. {
  282. kernel_dso = dso__new("[kernel]");
  283. if (kernel_dso == NULL)
  284. return -1;
  285. FILE *file = fopen("/proc/kallsyms", "r");
  286. if (file == NULL)
  287. goto out_delete_dso;
  288. char *line = NULL;
  289. size_t n;
  290. while (!feof(file)) {
  291. unsigned long long start;
  292. char c, symbf[4096];
  293. if (getline(&line, &n, file) < 0)
  294. break;
  295. if (!line)
  296. goto out_delete_dso;
  297. if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
  298. /*
  299. * Well fix up the end later, when we have all sorted.
  300. */
  301. struct symbol *sym = symbol__new(start, 0xdead, symbf);
  302. if (sym == NULL)
  303. goto out_delete_dso;
  304. dso__insert_symbol(kernel_dso, sym);
  305. }
  306. }
  307. /*
  308. * Now that we have all sorted out, just set the ->end of all
  309. * symbols
  310. */
  311. struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
  312. if (prevnd == NULL)
  313. goto out_delete_line;
  314. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  315. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  316. *curr = rb_entry(nd, struct symbol, rb_node);
  317. prev->end = curr->start - 1;
  318. prevnd = nd;
  319. }
  320. dsos__add(kernel_dso);
  321. free(line);
  322. fclose(file);
  323. return 0;
  324. out_delete_line:
  325. free(line);
  326. out_delete_dso:
  327. dso__delete(kernel_dso);
  328. return -1;
  329. }
  330. struct map {
  331. struct list_head node;
  332. uint64_t start;
  333. uint64_t end;
  334. uint64_t pgoff;
  335. struct dso *dso;
  336. };
  337. static struct map *map__new(struct mmap_event *event)
  338. {
  339. struct map *self = malloc(sizeof(*self));
  340. if (self != NULL) {
  341. self->start = event->start;
  342. self->end = event->start + event->len;
  343. self->pgoff = event->pgoff;
  344. self->dso = dsos__findnew(event->filename);
  345. if (self->dso == NULL)
  346. goto out_delete;
  347. }
  348. return self;
  349. out_delete:
  350. free(self);
  351. return NULL;
  352. }
  353. static size_t map__fprintf(struct map *self, FILE *fp)
  354. {
  355. return fprintf(fp, " %lx-%lx %lx %s\n",
  356. self->start, self->end, self->pgoff, self->dso->name);
  357. }
  358. struct thread;
  359. static const char *thread__name(struct thread *self, char *bf, size_t size);
  360. struct symhist {
  361. struct rb_node rb_node;
  362. struct dso *dso;
  363. struct symbol *sym;
  364. struct thread *thread;
  365. uint64_t ip;
  366. uint32_t count;
  367. char level;
  368. };
  369. static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
  370. struct thread *thread, struct dso *dso,
  371. char level)
  372. {
  373. struct symhist *self = malloc(sizeof(*self));
  374. if (self != NULL) {
  375. self->sym = sym;
  376. self->thread = thread;
  377. self->ip = ip;
  378. self->dso = dso;
  379. self->level = level;
  380. self->count = 1;
  381. }
  382. return self;
  383. }
  384. void symhist__delete(struct symhist *self)
  385. {
  386. free(self);
  387. }
  388. static void symhist__inc(struct symhist *self)
  389. {
  390. ++self->count;
  391. }
  392. static size_t
  393. symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
  394. {
  395. char bf[32];
  396. size_t ret;
  397. if (total_samples)
  398. ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
  399. else
  400. ret = fprintf(fp, "%12d", self->count);
  401. ret += fprintf(fp, "%14s [%c] %#018llx ",
  402. thread__name(self->thread, bf, sizeof(bf)),
  403. self->level, (unsigned long long)self->ip);
  404. if (self->level != '.')
  405. ret += fprintf(fp, "%s\n",
  406. self->sym ? self->sym->name : "<unknown>");
  407. else
  408. ret += fprintf(fp, "%s: %s\n",
  409. self->dso ? self->dso->name : "<unknown>",
  410. self->sym ? self->sym->name : "<unknown>");
  411. return ret;
  412. }
  413. struct thread {
  414. struct rb_node rb_node;
  415. struct list_head maps;
  416. struct rb_root symhists;
  417. pid_t pid;
  418. char *comm;
  419. };
  420. static const char *thread__name(struct thread *self, char *bf, size_t size)
  421. {
  422. if (self->comm)
  423. return self->comm;
  424. snprintf(bf, sizeof(bf), ":%u", self->pid);
  425. return bf;
  426. }
  427. static struct thread *thread__new(pid_t pid)
  428. {
  429. struct thread *self = malloc(sizeof(*self));
  430. if (self != NULL) {
  431. self->pid = pid;
  432. self->comm = NULL;
  433. INIT_LIST_HEAD(&self->maps);
  434. self->symhists = RB_ROOT;
  435. }
  436. return self;
  437. }
  438. static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
  439. uint64_t ip, struct dso *dso, char level)
  440. {
  441. struct rb_node **p = &self->symhists.rb_node;
  442. struct rb_node *parent = NULL;
  443. struct symhist *sh;
  444. while (*p != NULL) {
  445. parent = *p;
  446. sh = rb_entry(parent, struct symhist, rb_node);
  447. if (sh->sym == sym || ip == sh->ip) {
  448. symhist__inc(sh);
  449. return 0;
  450. }
  451. /* Handle unresolved symbols too */
  452. const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
  453. if (ip < start)
  454. p = &(*p)->rb_left;
  455. else
  456. p = &(*p)->rb_right;
  457. }
  458. sh = symhist__new(sym, ip, self, dso, level);
  459. if (sh == NULL)
  460. return -ENOMEM;
  461. rb_link_node(&sh->rb_node, parent, p);
  462. rb_insert_color(&sh->rb_node, &self->symhists);
  463. return 0;
  464. }
  465. static int thread__set_comm(struct thread *self, const char *comm)
  466. {
  467. self->comm = strdup(comm);
  468. return self->comm ? 0 : -ENOMEM;
  469. }
  470. size_t thread__maps_fprintf(struct thread *self, FILE *fp)
  471. {
  472. struct map *pos;
  473. size_t ret = 0;
  474. list_for_each_entry(pos, &self->maps, node)
  475. ret += map__fprintf(pos, fp);
  476. return ret;
  477. }
  478. static size_t thread__fprintf(struct thread *self, FILE *fp)
  479. {
  480. int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
  481. struct rb_node *nd;
  482. for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
  483. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  484. ret += symhist__fprintf(pos, 0, fp);
  485. }
  486. return ret;
  487. }
  488. static struct rb_root threads = RB_ROOT;
  489. static struct thread *threads__findnew(pid_t pid)
  490. {
  491. struct rb_node **p = &threads.rb_node;
  492. struct rb_node *parent = NULL;
  493. struct thread *th;
  494. while (*p != NULL) {
  495. parent = *p;
  496. th = rb_entry(parent, struct thread, rb_node);
  497. if (th->pid == pid)
  498. return th;
  499. if (pid < th->pid)
  500. p = &(*p)->rb_left;
  501. else
  502. p = &(*p)->rb_right;
  503. }
  504. th = thread__new(pid);
  505. if (th != NULL) {
  506. rb_link_node(&th->rb_node, parent, p);
  507. rb_insert_color(&th->rb_node, &threads);
  508. }
  509. return th;
  510. }
  511. static void thread__insert_map(struct thread *self, struct map *map)
  512. {
  513. list_add_tail(&map->node, &self->maps);
  514. }
  515. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  516. {
  517. if (self == NULL)
  518. return NULL;
  519. struct map *pos;
  520. list_for_each_entry(pos, &self->maps, node)
  521. if (ip >= pos->start && ip <= pos->end)
  522. return pos;
  523. return NULL;
  524. }
  525. void threads__fprintf(FILE *fp)
  526. {
  527. struct rb_node *nd;
  528. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  529. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  530. thread__fprintf(pos, fp);
  531. }
  532. }
  533. static struct rb_root global_symhists = RB_ROOT;
  534. static void threads__insert_symhist(struct symhist *sh)
  535. {
  536. struct rb_node **p = &global_symhists.rb_node;
  537. struct rb_node *parent = NULL;
  538. struct symhist *iter;
  539. while (*p != NULL) {
  540. parent = *p;
  541. iter = rb_entry(parent, struct symhist, rb_node);
  542. /* Reverse order */
  543. if (sh->count > iter->count)
  544. p = &(*p)->rb_left;
  545. else
  546. p = &(*p)->rb_right;
  547. }
  548. rb_link_node(&sh->rb_node, parent, p);
  549. rb_insert_color(&sh->rb_node, &global_symhists);
  550. }
  551. static void threads__sort_symhists(void)
  552. {
  553. struct rb_node *nd;
  554. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  555. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  556. struct rb_node *next = rb_first(&thread->symhists);
  557. while (next) {
  558. struct symhist *n = rb_entry(next, struct symhist,
  559. rb_node);
  560. next = rb_next(&n->rb_node);
  561. rb_erase(&n->rb_node, &thread->symhists);
  562. threads__insert_symhist(n);
  563. }
  564. }
  565. }
  566. static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
  567. {
  568. struct rb_node *nd;
  569. size_t ret = 0;
  570. for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
  571. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  572. ret += symhist__fprintf(pos, total_samples, fp);
  573. }
  574. return ret;
  575. }
  576. static int __cmd_report(void)
  577. {
  578. unsigned long offset = 0;
  579. unsigned long head = 0;
  580. struct stat stat;
  581. char *buf;
  582. event_t *event;
  583. int ret, rc = EXIT_FAILURE;
  584. uint32_t size;
  585. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  586. input = open(input_name, O_RDONLY);
  587. if (input < 0) {
  588. perror("failed to open file");
  589. exit(-1);
  590. }
  591. ret = fstat(input, &stat);
  592. if (ret < 0) {
  593. perror("failed to stat file");
  594. exit(-1);
  595. }
  596. if (!stat.st_size) {
  597. fprintf(stderr, "zero-sized file, nothing to do!\n");
  598. exit(0);
  599. }
  600. if (load_kallsyms() < 0) {
  601. perror("failed to open kallsyms");
  602. return EXIT_FAILURE;
  603. }
  604. remap:
  605. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  606. MAP_SHARED, input, offset);
  607. if (buf == MAP_FAILED) {
  608. perror("failed to mmap file");
  609. exit(-1);
  610. }
  611. more:
  612. event = (event_t *)(buf + head);
  613. size = event->header.size;
  614. if (!size)
  615. size = 8;
  616. if (head + event->header.size >= page_size * mmap_window) {
  617. unsigned long shift = page_size * (head / page_size);
  618. int ret;
  619. ret = munmap(buf, page_size * mmap_window);
  620. assert(ret == 0);
  621. offset += shift;
  622. head -= shift;
  623. goto remap;
  624. }
  625. size = event->header.size;
  626. if (!size)
  627. goto broken_event;
  628. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  629. char level;
  630. int show = 0;
  631. struct dso *dso = NULL;
  632. struct thread *thread = threads__findnew(event->ip.pid);
  633. uint64_t ip = event->ip.ip;
  634. if (dump_trace) {
  635. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  636. (void *)(offset + head),
  637. (void *)(long)(event->header.size),
  638. event->header.misc,
  639. event->ip.pid,
  640. (void *)event->ip.ip);
  641. }
  642. if (thread == NULL) {
  643. fprintf(stderr, "problem processing %d event, bailing out\n",
  644. event->header.type);
  645. goto done;
  646. }
  647. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  648. show = SHOW_KERNEL;
  649. level = 'k';
  650. dso = kernel_dso;
  651. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  652. show = SHOW_USER;
  653. level = '.';
  654. struct map *map = thread__find_map(thread, ip);
  655. if (map != NULL) {
  656. dso = map->dso;
  657. ip -= map->start + map->pgoff;
  658. }
  659. } else {
  660. show = SHOW_HV;
  661. level = 'H';
  662. }
  663. if (show & show_mask) {
  664. struct symbol *sym = dso__find_symbol(dso, ip);
  665. if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
  666. fprintf(stderr, "problem incrementing symbol count, bailing out\n");
  667. goto done;
  668. }
  669. }
  670. total++;
  671. } else switch (event->header.type) {
  672. case PERF_EVENT_MMAP: {
  673. struct thread *thread = threads__findnew(event->mmap.pid);
  674. struct map *map = map__new(&event->mmap);
  675. if (dump_trace) {
  676. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  677. (void *)(offset + head),
  678. (void *)(long)(event->header.size),
  679. (void *)event->mmap.start,
  680. (void *)event->mmap.len,
  681. (void *)event->mmap.pgoff,
  682. event->mmap.filename);
  683. }
  684. if (thread == NULL || map == NULL) {
  685. fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
  686. goto done;
  687. }
  688. thread__insert_map(thread, map);
  689. total_mmap++;
  690. break;
  691. }
  692. case PERF_EVENT_COMM: {
  693. struct thread *thread = threads__findnew(event->comm.pid);
  694. if (dump_trace) {
  695. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  696. (void *)(offset + head),
  697. (void *)(long)(event->header.size),
  698. event->comm.comm, event->comm.pid);
  699. }
  700. if (thread == NULL ||
  701. thread__set_comm(thread, event->comm.comm)) {
  702. fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
  703. goto done;
  704. }
  705. total_comm++;
  706. break;
  707. }
  708. default: {
  709. broken_event:
  710. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  711. (void *)(offset + head),
  712. (void *)(long)(event->header.size),
  713. event->header.type);
  714. total_unknown++;
  715. /*
  716. * assume we lost track of the stream, check alignment, and
  717. * increment a single u64 in the hope to catch on again 'soon'.
  718. */
  719. if (unlikely(head & 7))
  720. head &= ~7ULL;
  721. size = 8;
  722. }
  723. }
  724. head += size;
  725. if (offset + head < stat.st_size)
  726. goto more;
  727. rc = EXIT_SUCCESS;
  728. done:
  729. close(input);
  730. if (dump_trace) {
  731. fprintf(stderr, " IP events: %10ld\n", total);
  732. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  733. fprintf(stderr, " comm events: %10ld\n", total_comm);
  734. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  735. return 0;
  736. }
  737. threads__sort_symhists();
  738. threads__symhists_fprintf(total, stdout);
  739. return rc;
  740. }
  741. static const char * const report_usage[] = {
  742. "perf report [<options>] <command>",
  743. NULL
  744. };
  745. static const struct option options[] = {
  746. OPT_STRING('i', "input", &input_name, "file",
  747. "input file name"),
  748. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  749. "dump raw trace in ASCII"),
  750. OPT_END()
  751. };
  752. int cmd_report(int argc, const char **argv, const char *prefix)
  753. {
  754. elf_version(EV_CURRENT);
  755. page_size = getpagesize();
  756. parse_options(argc, argv, options, report_usage, 0);
  757. return __cmd_report();
  758. }