builtin-report.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. sym->st_size != 0;
  160. }
  161. static inline const char *elf_sym__name(const GElf_Sym *sym,
  162. const Elf_Data *symstrs)
  163. {
  164. return symstrs->d_buf + sym->st_name;
  165. }
  166. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  167. GElf_Shdr *shp, const char *name,
  168. size_t *index)
  169. {
  170. Elf_Scn *sec = NULL;
  171. size_t cnt = 1;
  172. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  173. char *str;
  174. gelf_getshdr(sec, shp);
  175. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  176. if (!strcmp(name, str)) {
  177. if (index)
  178. *index = cnt;
  179. break;
  180. }
  181. ++cnt;
  182. }
  183. return sec;
  184. }
  185. static int dso__load_sym(struct dso *self, int fd, char *name)
  186. {
  187. Elf_Data *symstrs;
  188. uint32_t nr_syms;
  189. int err = -1;
  190. uint32_t index;
  191. GElf_Ehdr ehdr;
  192. GElf_Shdr shdr;
  193. Elf_Data *syms;
  194. GElf_Sym sym;
  195. Elf_Scn *sec;
  196. Elf *elf;
  197. int nr = 0;
  198. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  199. if (elf == NULL) {
  200. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  201. __func__, name);
  202. goto out_close;
  203. }
  204. if (gelf_getehdr(elf, &ehdr) == NULL) {
  205. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  206. goto out_elf_end;
  207. }
  208. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  209. if (sec == NULL)
  210. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  211. if (sec == NULL)
  212. goto out_elf_end;
  213. syms = elf_getdata(sec, NULL);
  214. if (syms == NULL)
  215. goto out_elf_end;
  216. sec = elf_getscn(elf, shdr.sh_link);
  217. if (sec == NULL)
  218. goto out_elf_end;
  219. symstrs = elf_getdata(sec, NULL);
  220. if (symstrs == NULL)
  221. goto out_elf_end;
  222. nr_syms = shdr.sh_size / shdr.sh_entsize;
  223. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  224. struct symbol *f;
  225. if (!elf_sym__is_function(&sym))
  226. continue;
  227. sec = elf_getscn(elf, sym.st_shndx);
  228. if (!sec)
  229. goto out_elf_end;
  230. gelf_getshdr(sec, &shdr);
  231. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  232. f = symbol__new(sym.st_value, sym.st_size,
  233. elf_sym__name(&sym, symstrs));
  234. if (!f)
  235. goto out_elf_end;
  236. dso__insert_symbol(self, f);
  237. nr++;
  238. }
  239. err = nr;
  240. out_elf_end:
  241. elf_end(elf);
  242. out_close:
  243. return err;
  244. }
  245. static int dso__load(struct dso *self)
  246. {
  247. int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
  248. char *name = malloc(size);
  249. int variant = 0;
  250. int ret = -1;
  251. int fd;
  252. if (!name)
  253. return -1;
  254. more:
  255. do {
  256. switch (variant) {
  257. case 0: /* Fedora */
  258. snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
  259. break;
  260. case 1: /* Ubuntu */
  261. snprintf(name, size, "/usr/lib/debug%s", self->name);
  262. break;
  263. case 2: /* Sane people */
  264. snprintf(name, size, "%s", self->name);
  265. break;
  266. default:
  267. goto out;
  268. }
  269. variant++;
  270. fd = open(name, O_RDONLY);
  271. } while (fd < 0);
  272. ret = dso__load_sym(self, fd, name);
  273. close(fd);
  274. /*
  275. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  276. */
  277. if (!ret)
  278. goto more;
  279. out:
  280. free(name);
  281. return ret;
  282. }
  283. static size_t dso__fprintf(struct dso *self, FILE *fp)
  284. {
  285. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  286. struct rb_node *nd;
  287. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  288. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  289. ret += symbol__fprintf(pos, fp);
  290. }
  291. return ret;
  292. }
  293. static LIST_HEAD(dsos);
  294. static struct dso *kernel_dso;
  295. static void dsos__add(struct dso *dso)
  296. {
  297. list_add_tail(&dso->node, &dsos);
  298. }
  299. static struct dso *dsos__find(const char *name)
  300. {
  301. struct dso *pos;
  302. list_for_each_entry(pos, &dsos, node)
  303. if (strcmp(pos->name, name) == 0)
  304. return pos;
  305. return NULL;
  306. }
  307. static struct dso *dsos__findnew(const char *name)
  308. {
  309. struct dso *dso = dsos__find(name);
  310. int nr;
  311. if (dso == NULL) {
  312. dso = dso__new(name);
  313. if (!dso)
  314. goto out_delete_dso;
  315. nr = dso__load(dso);
  316. if (nr < 0) {
  317. fprintf(stderr, "Failed to open: %s\n", name);
  318. goto out_delete_dso;
  319. }
  320. if (!nr) {
  321. fprintf(stderr,
  322. "Failed to find debug symbols for: %s, maybe install a debug package?\n",
  323. name);
  324. }
  325. dsos__add(dso);
  326. }
  327. return dso;
  328. out_delete_dso:
  329. dso__delete(dso);
  330. return NULL;
  331. }
  332. static void dsos__fprintf(FILE *fp)
  333. {
  334. struct dso *pos;
  335. list_for_each_entry(pos, &dsos, node)
  336. dso__fprintf(pos, fp);
  337. }
  338. static int hex(char ch)
  339. {
  340. if ((ch >= '0') && (ch <= '9'))
  341. return ch - '0';
  342. if ((ch >= 'a') && (ch <= 'f'))
  343. return ch - 'a' + 10;
  344. if ((ch >= 'A') && (ch <= 'F'))
  345. return ch - 'A' + 10;
  346. return -1;
  347. }
  348. /*
  349. * While we find nice hex chars, build a long_val.
  350. * Return number of chars processed.
  351. */
  352. static int hex2long(char *ptr, unsigned long *long_val)
  353. {
  354. const char *p = ptr;
  355. *long_val = 0;
  356. while (*p) {
  357. const int hex_val = hex(*p);
  358. if (hex_val < 0)
  359. break;
  360. *long_val = (*long_val << 4) | hex_val;
  361. p++;
  362. }
  363. return p - ptr;
  364. }
  365. static int load_kallsyms(void)
  366. {
  367. struct rb_node *nd, *prevnd;
  368. char *line = NULL;
  369. FILE *file;
  370. size_t n;
  371. kernel_dso = dso__new("[kernel]");
  372. if (kernel_dso == NULL)
  373. return -1;
  374. file = fopen("/proc/kallsyms", "r");
  375. if (file == NULL)
  376. goto out_delete_dso;
  377. while (!feof(file)) {
  378. unsigned long start;
  379. struct symbol *sym;
  380. int line_len, len;
  381. char symbol_type;
  382. line_len = getline(&line, &n, file);
  383. if (line_len < 0)
  384. break;
  385. if (!line)
  386. goto out_delete_dso;
  387. line[--line_len] = '\0'; /* \n */
  388. len = hex2long(line, &start);
  389. len++;
  390. if (len + 2 >= line_len)
  391. continue;
  392. symbol_type = toupper(line[len]);
  393. /*
  394. * We're interested only in code ('T'ext)
  395. */
  396. if (symbol_type != 'T' && symbol_type != 'W')
  397. continue;
  398. /*
  399. * Well fix up the end later, when we have all sorted.
  400. */
  401. sym = symbol__new(start, 0xdead, line + len + 2);
  402. if (sym == NULL)
  403. goto out_delete_dso;
  404. dso__insert_symbol(kernel_dso, sym);
  405. }
  406. /*
  407. * Now that we have all sorted out, just set the ->end of all
  408. * symbols
  409. */
  410. prevnd = rb_first(&kernel_dso->syms);
  411. if (prevnd == NULL)
  412. goto out_delete_line;
  413. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  414. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  415. *curr = rb_entry(nd, struct symbol, rb_node);
  416. prev->end = curr->start - 1;
  417. prevnd = nd;
  418. }
  419. dsos__add(kernel_dso);
  420. free(line);
  421. fclose(file);
  422. return 0;
  423. out_delete_line:
  424. free(line);
  425. out_delete_dso:
  426. dso__delete(kernel_dso);
  427. return -1;
  428. }
  429. struct map {
  430. struct list_head node;
  431. uint64_t start;
  432. uint64_t end;
  433. uint64_t pgoff;
  434. struct dso *dso;
  435. };
  436. static struct map *map__new(struct mmap_event *event)
  437. {
  438. struct map *self = malloc(sizeof(*self));
  439. if (self != NULL) {
  440. self->start = event->start;
  441. self->end = event->start + event->len;
  442. self->pgoff = event->pgoff;
  443. self->dso = dsos__findnew(event->filename);
  444. if (self->dso == NULL)
  445. goto out_delete;
  446. }
  447. return self;
  448. out_delete:
  449. free(self);
  450. return NULL;
  451. }
  452. struct thread;
  453. static const char *thread__name(struct thread *self, char *bf, size_t size);
  454. struct symhist {
  455. struct rb_node rb_node;
  456. struct dso *dso;
  457. struct symbol *sym;
  458. struct thread *thread;
  459. uint64_t ip;
  460. uint32_t count;
  461. char level;
  462. };
  463. static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
  464. struct thread *thread, struct dso *dso,
  465. char level)
  466. {
  467. struct symhist *self = malloc(sizeof(*self));
  468. if (self != NULL) {
  469. self->sym = sym;
  470. self->thread = thread;
  471. self->ip = ip;
  472. self->dso = dso;
  473. self->level = level;
  474. self->count = 1;
  475. }
  476. return self;
  477. }
  478. static void symhist__inc(struct symhist *self)
  479. {
  480. ++self->count;
  481. }
  482. static size_t
  483. symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
  484. {
  485. char bf[32];
  486. size_t ret;
  487. if (total_samples)
  488. ret = fprintf(fp, "%5.2f%% ", (self->count * 100.0) / total_samples);
  489. else
  490. ret = fprintf(fp, "%12d ", self->count);
  491. ret += fprintf(fp, "%14s [%c] ",
  492. thread__name(self->thread, bf, sizeof(bf)),
  493. self->level);
  494. if (verbose)
  495. ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
  496. if (self->level != '.')
  497. ret += fprintf(fp, "%s\n",
  498. self->sym ? self->sym->name : "<unknown>");
  499. else
  500. ret += fprintf(fp, "%s: %s\n",
  501. self->dso ? self->dso->name : "<unknown>",
  502. self->sym ? self->sym->name : "<unknown>");
  503. return ret;
  504. }
  505. struct thread {
  506. struct rb_node rb_node;
  507. struct list_head maps;
  508. struct rb_root symhists;
  509. pid_t pid;
  510. char *comm;
  511. };
  512. static const char *thread__name(struct thread *self, char *bf, size_t size)
  513. {
  514. if (self->comm)
  515. return self->comm;
  516. snprintf(bf, sizeof(bf), ":%u", self->pid);
  517. return bf;
  518. }
  519. static struct thread *thread__new(pid_t pid)
  520. {
  521. struct thread *self = malloc(sizeof(*self));
  522. if (self != NULL) {
  523. self->pid = pid;
  524. self->comm = NULL;
  525. INIT_LIST_HEAD(&self->maps);
  526. self->symhists = RB_ROOT;
  527. }
  528. return self;
  529. }
  530. static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
  531. uint64_t ip, struct dso *dso, char level)
  532. {
  533. struct rb_node **p = &self->symhists.rb_node;
  534. struct rb_node *parent = NULL;
  535. struct symhist *sh;
  536. while (*p != NULL) {
  537. uint64_t start;
  538. parent = *p;
  539. sh = rb_entry(parent, struct symhist, rb_node);
  540. if (sh->sym == sym || ip == sh->ip) {
  541. symhist__inc(sh);
  542. return 0;
  543. }
  544. /* Handle unresolved symbols too */
  545. start = !sh->sym ? sh->ip : sh->sym->start;
  546. if (ip < start)
  547. p = &(*p)->rb_left;
  548. else
  549. p = &(*p)->rb_right;
  550. }
  551. sh = symhist__new(sym, ip, self, dso, level);
  552. if (sh == NULL)
  553. return -ENOMEM;
  554. rb_link_node(&sh->rb_node, parent, p);
  555. rb_insert_color(&sh->rb_node, &self->symhists);
  556. return 0;
  557. }
  558. static int thread__set_comm(struct thread *self, const char *comm)
  559. {
  560. self->comm = strdup(comm);
  561. return self->comm ? 0 : -ENOMEM;
  562. }
  563. static size_t thread__fprintf(struct thread *self, FILE *fp)
  564. {
  565. int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
  566. struct rb_node *nd;
  567. for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
  568. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  569. ret += symhist__fprintf(pos, 0, fp);
  570. }
  571. return ret;
  572. }
  573. static struct rb_root threads;
  574. static struct thread *threads__findnew(pid_t pid)
  575. {
  576. struct rb_node **p = &threads.rb_node;
  577. struct rb_node *parent = NULL;
  578. struct thread *th;
  579. while (*p != NULL) {
  580. parent = *p;
  581. th = rb_entry(parent, struct thread, rb_node);
  582. if (th->pid == pid)
  583. return th;
  584. if (pid < th->pid)
  585. p = &(*p)->rb_left;
  586. else
  587. p = &(*p)->rb_right;
  588. }
  589. th = thread__new(pid);
  590. if (th != NULL) {
  591. rb_link_node(&th->rb_node, parent, p);
  592. rb_insert_color(&th->rb_node, &threads);
  593. }
  594. return th;
  595. }
  596. static void thread__insert_map(struct thread *self, struct map *map)
  597. {
  598. list_add_tail(&map->node, &self->maps);
  599. }
  600. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  601. {
  602. struct map *pos;
  603. if (self == NULL)
  604. return NULL;
  605. list_for_each_entry(pos, &self->maps, node)
  606. if (ip >= pos->start && ip <= pos->end)
  607. return pos;
  608. return NULL;
  609. }
  610. static void threads__fprintf(FILE *fp)
  611. {
  612. struct rb_node *nd;
  613. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  614. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  615. thread__fprintf(pos, fp);
  616. }
  617. }
  618. static struct rb_root global_symhists;
  619. static void threads__insert_symhist(struct symhist *sh)
  620. {
  621. struct rb_node **p = &global_symhists.rb_node;
  622. struct rb_node *parent = NULL;
  623. struct symhist *iter;
  624. while (*p != NULL) {
  625. parent = *p;
  626. iter = rb_entry(parent, struct symhist, rb_node);
  627. /* Reverse order */
  628. if (sh->count > iter->count)
  629. p = &(*p)->rb_left;
  630. else
  631. p = &(*p)->rb_right;
  632. }
  633. rb_link_node(&sh->rb_node, parent, p);
  634. rb_insert_color(&sh->rb_node, &global_symhists);
  635. }
  636. static void threads__sort_symhists(void)
  637. {
  638. struct rb_node *nd;
  639. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  640. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  641. struct rb_node *next = rb_first(&thread->symhists);
  642. while (next) {
  643. struct symhist *n = rb_entry(next, struct symhist,
  644. rb_node);
  645. next = rb_next(&n->rb_node);
  646. rb_erase(&n->rb_node, &thread->symhists);
  647. threads__insert_symhist(n);
  648. }
  649. }
  650. }
  651. static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
  652. {
  653. struct rb_node *nd;
  654. size_t ret = 0;
  655. for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
  656. struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
  657. ret += symhist__fprintf(pos, total_samples, fp);
  658. }
  659. return ret;
  660. }
  661. static int __cmd_report(void)
  662. {
  663. unsigned long offset = 0;
  664. unsigned long head = 0;
  665. struct stat stat;
  666. char *buf;
  667. event_t *event;
  668. int ret, rc = EXIT_FAILURE;
  669. uint32_t size;
  670. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  671. input = open(input_name, O_RDONLY);
  672. if (input < 0) {
  673. perror("failed to open file");
  674. exit(-1);
  675. }
  676. ret = fstat(input, &stat);
  677. if (ret < 0) {
  678. perror("failed to stat file");
  679. exit(-1);
  680. }
  681. if (!stat.st_size) {
  682. fprintf(stderr, "zero-sized file, nothing to do!\n");
  683. exit(0);
  684. }
  685. if (load_kallsyms() < 0) {
  686. perror("failed to open kallsyms");
  687. return EXIT_FAILURE;
  688. }
  689. remap:
  690. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  691. MAP_SHARED, input, offset);
  692. if (buf == MAP_FAILED) {
  693. perror("failed to mmap file");
  694. exit(-1);
  695. }
  696. more:
  697. event = (event_t *)(buf + head);
  698. size = event->header.size;
  699. if (!size)
  700. size = 8;
  701. if (head + event->header.size >= page_size * mmap_window) {
  702. unsigned long shift = page_size * (head / page_size);
  703. int ret;
  704. ret = munmap(buf, page_size * mmap_window);
  705. assert(ret == 0);
  706. offset += shift;
  707. head -= shift;
  708. goto remap;
  709. }
  710. size = event->header.size;
  711. if (!size)
  712. goto broken_event;
  713. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  714. char level;
  715. int show = 0;
  716. struct dso *dso = NULL;
  717. struct thread *thread = threads__findnew(event->ip.pid);
  718. uint64_t ip = event->ip.ip;
  719. if (dump_trace) {
  720. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  721. (void *)(offset + head),
  722. (void *)(long)(event->header.size),
  723. event->header.misc,
  724. event->ip.pid,
  725. (void *)(long)ip);
  726. }
  727. if (thread == NULL) {
  728. fprintf(stderr, "problem processing %d event, bailing out\n",
  729. event->header.type);
  730. goto done;
  731. }
  732. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  733. show = SHOW_KERNEL;
  734. level = 'k';
  735. dso = kernel_dso;
  736. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  737. struct map *map;
  738. show = SHOW_USER;
  739. level = '.';
  740. map = thread__find_map(thread, ip);
  741. if (map != NULL) {
  742. dso = map->dso;
  743. ip -= map->start + map->pgoff;
  744. }
  745. } else {
  746. show = SHOW_HV;
  747. level = 'H';
  748. }
  749. if (show & show_mask) {
  750. struct symbol *sym = dso__find_symbol(dso, ip);
  751. if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
  752. fprintf(stderr, "problem incrementing symbol count, bailing out\n");
  753. goto done;
  754. }
  755. }
  756. total++;
  757. } else switch (event->header.type) {
  758. case PERF_EVENT_MMAP: {
  759. struct thread *thread = threads__findnew(event->mmap.pid);
  760. struct map *map = map__new(&event->mmap);
  761. if (dump_trace) {
  762. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  763. (void *)(offset + head),
  764. (void *)(long)(event->header.size),
  765. (void *)(long)event->mmap.start,
  766. (void *)(long)event->mmap.len,
  767. (void *)(long)event->mmap.pgoff,
  768. event->mmap.filename);
  769. }
  770. if (thread == NULL || map == NULL) {
  771. fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
  772. goto done;
  773. }
  774. thread__insert_map(thread, map);
  775. total_mmap++;
  776. break;
  777. }
  778. case PERF_EVENT_COMM: {
  779. struct thread *thread = threads__findnew(event->comm.pid);
  780. if (dump_trace) {
  781. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  782. (void *)(offset + head),
  783. (void *)(long)(event->header.size),
  784. event->comm.comm, event->comm.pid);
  785. }
  786. if (thread == NULL ||
  787. thread__set_comm(thread, event->comm.comm)) {
  788. fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
  789. goto done;
  790. }
  791. total_comm++;
  792. break;
  793. }
  794. default: {
  795. broken_event:
  796. if (dump_trace)
  797. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  798. (void *)(offset + head),
  799. (void *)(long)(event->header.size),
  800. event->header.type);
  801. total_unknown++;
  802. /*
  803. * assume we lost track of the stream, check alignment, and
  804. * increment a single u64 in the hope to catch on again 'soon'.
  805. */
  806. if (unlikely(head & 7))
  807. head &= ~7ULL;
  808. size = 8;
  809. }
  810. }
  811. head += size;
  812. if (offset + head < stat.st_size)
  813. goto more;
  814. rc = EXIT_SUCCESS;
  815. done:
  816. close(input);
  817. if (dump_trace) {
  818. fprintf(stderr, " IP events: %10ld\n", total);
  819. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  820. fprintf(stderr, " comm events: %10ld\n", total_comm);
  821. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  822. return 0;
  823. }
  824. if (verbose >= 2) {
  825. dsos__fprintf(stdout);
  826. threads__fprintf(stdout);
  827. }
  828. threads__sort_symhists();
  829. threads__symhists_fprintf(total, stdout);
  830. return rc;
  831. }
  832. static const char * const report_usage[] = {
  833. "perf report [<options>] <command>",
  834. NULL
  835. };
  836. static const struct option options[] = {
  837. OPT_STRING('i', "input", &input_name, "file",
  838. "input file name"),
  839. OPT_BOOLEAN('v', "verbose", &verbose,
  840. "be more verbose (show symbol address, etc)"),
  841. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  842. "dump raw trace in ASCII"),
  843. OPT_END()
  844. };
  845. int cmd_report(int argc, const char **argv, const char *prefix)
  846. {
  847. elf_version(EV_CURRENT);
  848. page_size = getpagesize();
  849. parse_options(argc, argv, options, report_usage, 0);
  850. setup_pager();
  851. return __cmd_report();
  852. }