builtin-report.c 20 KB

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