builtin-report.c 20 KB

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