builtin-report.c 21 KB

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