builtin-report.c 17 KB

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