builtin-annotate.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * builtin-annotate.c
  3. *
  4. * Builtin annotate command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "perf.h"
  17. #include "util/parse-options.h"
  18. #include "util/parse-events.h"
  19. #define SHOW_KERNEL 1
  20. #define SHOW_USER 2
  21. #define SHOW_HV 4
  22. static char const *input_name = "perf.data";
  23. static char *vmlinux = "vmlinux";
  24. static char default_sort_order[] = "comm,symbol";
  25. static char *sort_order = default_sort_order;
  26. static int force;
  27. static int input;
  28. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  29. static int dump_trace = 0;
  30. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  31. static int verbose;
  32. static int modules;
  33. static int full_paths;
  34. static int print_line;
  35. static unsigned long page_size;
  36. static unsigned long mmap_window = 32;
  37. struct ip_event {
  38. struct perf_event_header header;
  39. u64 ip;
  40. u32 pid, tid;
  41. };
  42. struct mmap_event {
  43. struct perf_event_header header;
  44. u32 pid, tid;
  45. u64 start;
  46. u64 len;
  47. u64 pgoff;
  48. char filename[PATH_MAX];
  49. };
  50. struct comm_event {
  51. struct perf_event_header header;
  52. u32 pid, tid;
  53. char comm[16];
  54. };
  55. struct fork_event {
  56. struct perf_event_header header;
  57. u32 pid, ppid;
  58. };
  59. typedef union event_union {
  60. struct perf_event_header header;
  61. struct ip_event ip;
  62. struct mmap_event mmap;
  63. struct comm_event comm;
  64. struct fork_event fork;
  65. } event_t;
  66. struct sym_ext {
  67. struct rb_node node;
  68. double percent;
  69. char *path;
  70. };
  71. static LIST_HEAD(dsos);
  72. static struct dso *kernel_dso;
  73. static struct dso *vdso;
  74. static void dsos__add(struct dso *dso)
  75. {
  76. list_add_tail(&dso->node, &dsos);
  77. }
  78. static struct dso *dsos__find(const char *name)
  79. {
  80. struct dso *pos;
  81. list_for_each_entry(pos, &dsos, node)
  82. if (strcmp(pos->name, name) == 0)
  83. return pos;
  84. return NULL;
  85. }
  86. static struct dso *dsos__findnew(const char *name)
  87. {
  88. struct dso *dso = dsos__find(name);
  89. int nr;
  90. if (dso)
  91. return dso;
  92. dso = dso__new(name, 0);
  93. if (!dso)
  94. goto out_delete_dso;
  95. nr = dso__load(dso, NULL, verbose);
  96. if (nr < 0) {
  97. if (verbose)
  98. fprintf(stderr, "Failed to open: %s\n", name);
  99. goto out_delete_dso;
  100. }
  101. if (!nr && verbose) {
  102. fprintf(stderr,
  103. "No symbols found in: %s, maybe install a debug package?\n",
  104. name);
  105. }
  106. dsos__add(dso);
  107. return dso;
  108. out_delete_dso:
  109. dso__delete(dso);
  110. return NULL;
  111. }
  112. static void dsos__fprintf(FILE *fp)
  113. {
  114. struct dso *pos;
  115. list_for_each_entry(pos, &dsos, node)
  116. dso__fprintf(pos, fp);
  117. }
  118. static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
  119. {
  120. return dso__find_symbol(dso, ip);
  121. }
  122. static int load_kernel(void)
  123. {
  124. int err;
  125. kernel_dso = dso__new("[kernel]", 0);
  126. if (!kernel_dso)
  127. return -1;
  128. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
  129. if (err <= 0) {
  130. dso__delete(kernel_dso);
  131. kernel_dso = NULL;
  132. } else
  133. dsos__add(kernel_dso);
  134. vdso = dso__new("[vdso]", 0);
  135. if (!vdso)
  136. return -1;
  137. vdso->find_symbol = vdso__find_symbol;
  138. dsos__add(vdso);
  139. return err;
  140. }
  141. struct map {
  142. struct list_head node;
  143. u64 start;
  144. u64 end;
  145. u64 pgoff;
  146. u64 (*map_ip)(struct map *, u64);
  147. struct dso *dso;
  148. };
  149. static u64 map__map_ip(struct map *map, u64 ip)
  150. {
  151. return ip - map->start + map->pgoff;
  152. }
  153. static u64 vdso__map_ip(struct map *map __used, u64 ip)
  154. {
  155. return ip;
  156. }
  157. static struct map *map__new(struct mmap_event *event)
  158. {
  159. struct map *self = malloc(sizeof(*self));
  160. if (self != NULL) {
  161. const char *filename = event->filename;
  162. self->start = event->start;
  163. self->end = event->start + event->len;
  164. self->pgoff = event->pgoff;
  165. self->dso = dsos__findnew(filename);
  166. if (self->dso == NULL)
  167. goto out_delete;
  168. if (self->dso == vdso)
  169. self->map_ip = vdso__map_ip;
  170. else
  171. self->map_ip = map__map_ip;
  172. }
  173. return self;
  174. out_delete:
  175. free(self);
  176. return NULL;
  177. }
  178. static struct map *map__clone(struct map *self)
  179. {
  180. struct map *map = malloc(sizeof(*self));
  181. if (!map)
  182. return NULL;
  183. memcpy(map, self, sizeof(*self));
  184. return map;
  185. }
  186. static int map__overlap(struct map *l, struct map *r)
  187. {
  188. if (l->start > r->start) {
  189. struct map *t = l;
  190. l = r;
  191. r = t;
  192. }
  193. if (l->end > r->start)
  194. return 1;
  195. return 0;
  196. }
  197. static size_t map__fprintf(struct map *self, FILE *fp)
  198. {
  199. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  200. self->start, self->end, self->pgoff, self->dso->name);
  201. }
  202. struct thread {
  203. struct rb_node rb_node;
  204. struct list_head maps;
  205. pid_t pid;
  206. char *comm;
  207. };
  208. static struct thread *thread__new(pid_t pid)
  209. {
  210. struct thread *self = malloc(sizeof(*self));
  211. if (self != NULL) {
  212. self->pid = pid;
  213. self->comm = malloc(32);
  214. if (self->comm)
  215. snprintf(self->comm, 32, ":%d", self->pid);
  216. INIT_LIST_HEAD(&self->maps);
  217. }
  218. return self;
  219. }
  220. static int thread__set_comm(struct thread *self, const char *comm)
  221. {
  222. if (self->comm)
  223. free(self->comm);
  224. self->comm = strdup(comm);
  225. return self->comm ? 0 : -ENOMEM;
  226. }
  227. static size_t thread__fprintf(struct thread *self, FILE *fp)
  228. {
  229. struct map *pos;
  230. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  231. list_for_each_entry(pos, &self->maps, node)
  232. ret += map__fprintf(pos, fp);
  233. return ret;
  234. }
  235. static struct rb_root threads;
  236. static struct thread *last_match;
  237. static struct thread *threads__findnew(pid_t pid)
  238. {
  239. struct rb_node **p = &threads.rb_node;
  240. struct rb_node *parent = NULL;
  241. struct thread *th;
  242. /*
  243. * Font-end cache - PID lookups come in blocks,
  244. * so most of the time we dont have to look up
  245. * the full rbtree:
  246. */
  247. if (last_match && last_match->pid == pid)
  248. return last_match;
  249. while (*p != NULL) {
  250. parent = *p;
  251. th = rb_entry(parent, struct thread, rb_node);
  252. if (th->pid == pid) {
  253. last_match = th;
  254. return th;
  255. }
  256. if (pid < th->pid)
  257. p = &(*p)->rb_left;
  258. else
  259. p = &(*p)->rb_right;
  260. }
  261. th = thread__new(pid);
  262. if (th != NULL) {
  263. rb_link_node(&th->rb_node, parent, p);
  264. rb_insert_color(&th->rb_node, &threads);
  265. last_match = th;
  266. }
  267. return th;
  268. }
  269. static void thread__insert_map(struct thread *self, struct map *map)
  270. {
  271. struct map *pos, *tmp;
  272. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  273. if (map__overlap(pos, map)) {
  274. list_del_init(&pos->node);
  275. /* XXX leaks dsos */
  276. free(pos);
  277. }
  278. }
  279. list_add_tail(&map->node, &self->maps);
  280. }
  281. static int thread__fork(struct thread *self, struct thread *parent)
  282. {
  283. struct map *map;
  284. if (self->comm)
  285. free(self->comm);
  286. self->comm = strdup(parent->comm);
  287. if (!self->comm)
  288. return -ENOMEM;
  289. list_for_each_entry(map, &parent->maps, node) {
  290. struct map *new = map__clone(map);
  291. if (!new)
  292. return -ENOMEM;
  293. thread__insert_map(self, new);
  294. }
  295. return 0;
  296. }
  297. static struct map *thread__find_map(struct thread *self, u64 ip)
  298. {
  299. struct map *pos;
  300. if (self == NULL)
  301. return NULL;
  302. list_for_each_entry(pos, &self->maps, node)
  303. if (ip >= pos->start && ip <= pos->end)
  304. return pos;
  305. return NULL;
  306. }
  307. static size_t threads__fprintf(FILE *fp)
  308. {
  309. size_t ret = 0;
  310. struct rb_node *nd;
  311. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  312. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  313. ret += thread__fprintf(pos, fp);
  314. }
  315. return ret;
  316. }
  317. /*
  318. * histogram, sorted on item, collects counts
  319. */
  320. static struct rb_root hist;
  321. struct hist_entry {
  322. struct rb_node rb_node;
  323. struct thread *thread;
  324. struct map *map;
  325. struct dso *dso;
  326. struct symbol *sym;
  327. u64 ip;
  328. char level;
  329. uint32_t count;
  330. };
  331. /*
  332. * configurable sorting bits
  333. */
  334. struct sort_entry {
  335. struct list_head list;
  336. char *header;
  337. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  338. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  339. size_t (*print)(FILE *fp, struct hist_entry *);
  340. };
  341. /* --sort pid */
  342. static int64_t
  343. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  344. {
  345. return right->thread->pid - left->thread->pid;
  346. }
  347. static size_t
  348. sort__thread_print(FILE *fp, struct hist_entry *self)
  349. {
  350. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  351. }
  352. static struct sort_entry sort_thread = {
  353. .header = " Command: Pid",
  354. .cmp = sort__thread_cmp,
  355. .print = sort__thread_print,
  356. };
  357. /* --sort comm */
  358. static int64_t
  359. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  360. {
  361. return right->thread->pid - left->thread->pid;
  362. }
  363. static int64_t
  364. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  365. {
  366. char *comm_l = left->thread->comm;
  367. char *comm_r = right->thread->comm;
  368. if (!comm_l || !comm_r) {
  369. if (!comm_l && !comm_r)
  370. return 0;
  371. else if (!comm_l)
  372. return -1;
  373. else
  374. return 1;
  375. }
  376. return strcmp(comm_l, comm_r);
  377. }
  378. static size_t
  379. sort__comm_print(FILE *fp, struct hist_entry *self)
  380. {
  381. return fprintf(fp, "%16s", self->thread->comm);
  382. }
  383. static struct sort_entry sort_comm = {
  384. .header = " Command",
  385. .cmp = sort__comm_cmp,
  386. .collapse = sort__comm_collapse,
  387. .print = sort__comm_print,
  388. };
  389. /* --sort dso */
  390. static int64_t
  391. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  392. {
  393. struct dso *dso_l = left->dso;
  394. struct dso *dso_r = right->dso;
  395. if (!dso_l || !dso_r) {
  396. if (!dso_l && !dso_r)
  397. return 0;
  398. else if (!dso_l)
  399. return -1;
  400. else
  401. return 1;
  402. }
  403. return strcmp(dso_l->name, dso_r->name);
  404. }
  405. static size_t
  406. sort__dso_print(FILE *fp, struct hist_entry *self)
  407. {
  408. if (self->dso)
  409. return fprintf(fp, "%-25s", self->dso->name);
  410. return fprintf(fp, "%016llx ", (u64)self->ip);
  411. }
  412. static struct sort_entry sort_dso = {
  413. .header = "Shared Object ",
  414. .cmp = sort__dso_cmp,
  415. .print = sort__dso_print,
  416. };
  417. /* --sort symbol */
  418. static int64_t
  419. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  420. {
  421. u64 ip_l, ip_r;
  422. if (left->sym == right->sym)
  423. return 0;
  424. ip_l = left->sym ? left->sym->start : left->ip;
  425. ip_r = right->sym ? right->sym->start : right->ip;
  426. return (int64_t)(ip_r - ip_l);
  427. }
  428. static size_t
  429. sort__sym_print(FILE *fp, struct hist_entry *self)
  430. {
  431. size_t ret = 0;
  432. if (verbose)
  433. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  434. if (self->sym) {
  435. ret += fprintf(fp, "[%c] %s",
  436. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  437. } else {
  438. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  439. }
  440. return ret;
  441. }
  442. static struct sort_entry sort_sym = {
  443. .header = "Symbol",
  444. .cmp = sort__sym_cmp,
  445. .print = sort__sym_print,
  446. };
  447. static int sort__need_collapse = 0;
  448. struct sort_dimension {
  449. char *name;
  450. struct sort_entry *entry;
  451. int taken;
  452. };
  453. static struct sort_dimension sort_dimensions[] = {
  454. { .name = "pid", .entry = &sort_thread, },
  455. { .name = "comm", .entry = &sort_comm, },
  456. { .name = "dso", .entry = &sort_dso, },
  457. { .name = "symbol", .entry = &sort_sym, },
  458. };
  459. static LIST_HEAD(hist_entry__sort_list);
  460. static int sort_dimension__add(char *tok)
  461. {
  462. unsigned int i;
  463. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  464. struct sort_dimension *sd = &sort_dimensions[i];
  465. if (sd->taken)
  466. continue;
  467. if (strncasecmp(tok, sd->name, strlen(tok)))
  468. continue;
  469. if (sd->entry->collapse)
  470. sort__need_collapse = 1;
  471. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  472. sd->taken = 1;
  473. return 0;
  474. }
  475. return -ESRCH;
  476. }
  477. static int64_t
  478. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  479. {
  480. struct sort_entry *se;
  481. int64_t cmp = 0;
  482. list_for_each_entry(se, &hist_entry__sort_list, list) {
  483. cmp = se->cmp(left, right);
  484. if (cmp)
  485. break;
  486. }
  487. return cmp;
  488. }
  489. static int64_t
  490. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  491. {
  492. struct sort_entry *se;
  493. int64_t cmp = 0;
  494. list_for_each_entry(se, &hist_entry__sort_list, list) {
  495. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  496. f = se->collapse ?: se->cmp;
  497. cmp = f(left, right);
  498. if (cmp)
  499. break;
  500. }
  501. return cmp;
  502. }
  503. /*
  504. * collect histogram counts
  505. */
  506. static void hist_hit(struct hist_entry *he, u64 ip)
  507. {
  508. unsigned int sym_size, offset;
  509. struct symbol *sym = he->sym;
  510. he->count++;
  511. if (!sym || !sym->hist)
  512. return;
  513. sym_size = sym->end - sym->start;
  514. offset = ip - sym->start;
  515. if (offset >= sym_size)
  516. return;
  517. sym->hist_sum++;
  518. sym->hist[offset]++;
  519. if (verbose >= 3)
  520. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  521. (void *)(unsigned long)he->sym->start,
  522. he->sym->name,
  523. (void *)(unsigned long)ip, ip - he->sym->start,
  524. sym->hist[offset]);
  525. }
  526. static int
  527. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  528. struct symbol *sym, u64 ip, char level)
  529. {
  530. struct rb_node **p = &hist.rb_node;
  531. struct rb_node *parent = NULL;
  532. struct hist_entry *he;
  533. struct hist_entry entry = {
  534. .thread = thread,
  535. .map = map,
  536. .dso = dso,
  537. .sym = sym,
  538. .ip = ip,
  539. .level = level,
  540. .count = 1,
  541. };
  542. int cmp;
  543. while (*p != NULL) {
  544. parent = *p;
  545. he = rb_entry(parent, struct hist_entry, rb_node);
  546. cmp = hist_entry__cmp(&entry, he);
  547. if (!cmp) {
  548. hist_hit(he, ip);
  549. return 0;
  550. }
  551. if (cmp < 0)
  552. p = &(*p)->rb_left;
  553. else
  554. p = &(*p)->rb_right;
  555. }
  556. he = malloc(sizeof(*he));
  557. if (!he)
  558. return -ENOMEM;
  559. *he = entry;
  560. rb_link_node(&he->rb_node, parent, p);
  561. rb_insert_color(&he->rb_node, &hist);
  562. return 0;
  563. }
  564. static void hist_entry__free(struct hist_entry *he)
  565. {
  566. free(he);
  567. }
  568. /*
  569. * collapse the histogram
  570. */
  571. static struct rb_root collapse_hists;
  572. static void collapse__insert_entry(struct hist_entry *he)
  573. {
  574. struct rb_node **p = &collapse_hists.rb_node;
  575. struct rb_node *parent = NULL;
  576. struct hist_entry *iter;
  577. int64_t cmp;
  578. while (*p != NULL) {
  579. parent = *p;
  580. iter = rb_entry(parent, struct hist_entry, rb_node);
  581. cmp = hist_entry__collapse(iter, he);
  582. if (!cmp) {
  583. iter->count += he->count;
  584. hist_entry__free(he);
  585. return;
  586. }
  587. if (cmp < 0)
  588. p = &(*p)->rb_left;
  589. else
  590. p = &(*p)->rb_right;
  591. }
  592. rb_link_node(&he->rb_node, parent, p);
  593. rb_insert_color(&he->rb_node, &collapse_hists);
  594. }
  595. static void collapse__resort(void)
  596. {
  597. struct rb_node *next;
  598. struct hist_entry *n;
  599. if (!sort__need_collapse)
  600. return;
  601. next = rb_first(&hist);
  602. while (next) {
  603. n = rb_entry(next, struct hist_entry, rb_node);
  604. next = rb_next(&n->rb_node);
  605. rb_erase(&n->rb_node, &hist);
  606. collapse__insert_entry(n);
  607. }
  608. }
  609. /*
  610. * reverse the map, sort on count.
  611. */
  612. static struct rb_root output_hists;
  613. static void output__insert_entry(struct hist_entry *he)
  614. {
  615. struct rb_node **p = &output_hists.rb_node;
  616. struct rb_node *parent = NULL;
  617. struct hist_entry *iter;
  618. while (*p != NULL) {
  619. parent = *p;
  620. iter = rb_entry(parent, struct hist_entry, rb_node);
  621. if (he->count > iter->count)
  622. p = &(*p)->rb_left;
  623. else
  624. p = &(*p)->rb_right;
  625. }
  626. rb_link_node(&he->rb_node, parent, p);
  627. rb_insert_color(&he->rb_node, &output_hists);
  628. }
  629. static void output__resort(void)
  630. {
  631. struct rb_node *next;
  632. struct hist_entry *n;
  633. struct rb_root *tree = &hist;
  634. if (sort__need_collapse)
  635. tree = &collapse_hists;
  636. next = rb_first(tree);
  637. while (next) {
  638. n = rb_entry(next, struct hist_entry, rb_node);
  639. next = rb_next(&n->rb_node);
  640. rb_erase(&n->rb_node, tree);
  641. output__insert_entry(n);
  642. }
  643. }
  644. static void register_idle_thread(void)
  645. {
  646. struct thread *thread = threads__findnew(0);
  647. if (thread == NULL ||
  648. thread__set_comm(thread, "[idle]")) {
  649. fprintf(stderr, "problem inserting idle task.\n");
  650. exit(-1);
  651. }
  652. }
  653. static unsigned long total = 0,
  654. total_mmap = 0,
  655. total_comm = 0,
  656. total_fork = 0,
  657. total_unknown = 0;
  658. static int
  659. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  660. {
  661. char level;
  662. int show = 0;
  663. struct dso *dso = NULL;
  664. struct thread *thread = threads__findnew(event->ip.pid);
  665. u64 ip = event->ip.ip;
  666. struct map *map = NULL;
  667. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  668. (void *)(offset + head),
  669. (void *)(long)(event->header.size),
  670. event->header.misc,
  671. event->ip.pid,
  672. (void *)(long)ip);
  673. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  674. if (thread == NULL) {
  675. fprintf(stderr, "problem processing %d event, skipping it.\n",
  676. event->header.type);
  677. return -1;
  678. }
  679. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  680. show = SHOW_KERNEL;
  681. level = 'k';
  682. dso = kernel_dso;
  683. dprintf(" ...... dso: %s\n", dso->name);
  684. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  685. show = SHOW_USER;
  686. level = '.';
  687. map = thread__find_map(thread, ip);
  688. if (map != NULL) {
  689. ip = map->map_ip(map, ip);
  690. dso = map->dso;
  691. } else {
  692. /*
  693. * If this is outside of all known maps,
  694. * and is a negative address, try to look it
  695. * up in the kernel dso, as it might be a
  696. * vsyscall (which executes in user-mode):
  697. */
  698. if ((long long)ip < 0)
  699. dso = kernel_dso;
  700. }
  701. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  702. } else {
  703. show = SHOW_HV;
  704. level = 'H';
  705. dprintf(" ...... dso: [hypervisor]\n");
  706. }
  707. if (show & show_mask) {
  708. struct symbol *sym = NULL;
  709. if (dso)
  710. sym = dso->find_symbol(dso, ip);
  711. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  712. fprintf(stderr,
  713. "problem incrementing symbol count, skipping event\n");
  714. return -1;
  715. }
  716. }
  717. total++;
  718. return 0;
  719. }
  720. static int
  721. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  722. {
  723. struct thread *thread = threads__findnew(event->mmap.pid);
  724. struct map *map = map__new(&event->mmap);
  725. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  726. (void *)(offset + head),
  727. (void *)(long)(event->header.size),
  728. event->mmap.pid,
  729. (void *)(long)event->mmap.start,
  730. (void *)(long)event->mmap.len,
  731. (void *)(long)event->mmap.pgoff,
  732. event->mmap.filename);
  733. if (thread == NULL || map == NULL) {
  734. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  735. return 0;
  736. }
  737. thread__insert_map(thread, map);
  738. total_mmap++;
  739. return 0;
  740. }
  741. static int
  742. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  743. {
  744. struct thread *thread = threads__findnew(event->comm.pid);
  745. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  746. (void *)(offset + head),
  747. (void *)(long)(event->header.size),
  748. event->comm.comm, event->comm.pid);
  749. if (thread == NULL ||
  750. thread__set_comm(thread, event->comm.comm)) {
  751. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  752. return -1;
  753. }
  754. total_comm++;
  755. return 0;
  756. }
  757. static int
  758. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  759. {
  760. struct thread *thread = threads__findnew(event->fork.pid);
  761. struct thread *parent = threads__findnew(event->fork.ppid);
  762. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  763. (void *)(offset + head),
  764. (void *)(long)(event->header.size),
  765. event->fork.pid, event->fork.ppid);
  766. /*
  767. * A thread clone will have the same PID for both
  768. * parent and child.
  769. */
  770. if (thread == parent)
  771. return 0;
  772. if (!thread || !parent || thread__fork(thread, parent)) {
  773. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  774. return -1;
  775. }
  776. total_fork++;
  777. return 0;
  778. }
  779. static int
  780. process_event(event_t *event, unsigned long offset, unsigned long head)
  781. {
  782. switch (event->header.type) {
  783. case PERF_EVENT_SAMPLE:
  784. return process_sample_event(event, offset, head);
  785. case PERF_EVENT_MMAP:
  786. return process_mmap_event(event, offset, head);
  787. case PERF_EVENT_COMM:
  788. return process_comm_event(event, offset, head);
  789. case PERF_EVENT_FORK:
  790. return process_fork_event(event, offset, head);
  791. /*
  792. * We dont process them right now but they are fine:
  793. */
  794. case PERF_EVENT_THROTTLE:
  795. case PERF_EVENT_UNTHROTTLE:
  796. return 0;
  797. default:
  798. return -1;
  799. }
  800. return 0;
  801. }
  802. static int
  803. parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
  804. {
  805. char *line = NULL, *tmp, *tmp2;
  806. static const char *prev_line;
  807. static const char *prev_color;
  808. unsigned int offset;
  809. size_t line_len;
  810. s64 line_ip;
  811. int ret;
  812. char *c;
  813. if (getline(&line, &line_len, file) < 0)
  814. return -1;
  815. if (!line)
  816. return -1;
  817. c = strchr(line, '\n');
  818. if (c)
  819. *c = 0;
  820. line_ip = -1;
  821. offset = 0;
  822. ret = -2;
  823. /*
  824. * Strip leading spaces:
  825. */
  826. tmp = line;
  827. while (*tmp) {
  828. if (*tmp != ' ')
  829. break;
  830. tmp++;
  831. }
  832. if (*tmp) {
  833. /*
  834. * Parse hexa addresses followed by ':'
  835. */
  836. line_ip = strtoull(tmp, &tmp2, 16);
  837. if (*tmp2 != ':')
  838. line_ip = -1;
  839. }
  840. if (line_ip != -1) {
  841. const char *path = NULL;
  842. unsigned int hits = 0;
  843. double percent = 0.0;
  844. char *color;
  845. struct sym_ext *sym_ext = sym->priv;
  846. offset = line_ip - start;
  847. if (offset < len)
  848. hits = sym->hist[offset];
  849. if (offset < len && sym_ext) {
  850. path = sym_ext[offset].path;
  851. percent = sym_ext[offset].percent;
  852. } else if (sym->hist_sum)
  853. percent = 100.0 * hits / sym->hist_sum;
  854. color = get_percent_color(percent);
  855. /*
  856. * Also color the filename and line if needed, with
  857. * the same color than the percentage. Don't print it
  858. * twice for close colored ip with the same filename:line
  859. */
  860. if (path) {
  861. if (!prev_line || strcmp(prev_line, path)
  862. || color != prev_color) {
  863. color_fprintf(stdout, color, " %s", path);
  864. prev_line = path;
  865. prev_color = color;
  866. }
  867. }
  868. color_fprintf(stdout, color, " %7.2f", percent);
  869. printf(" : ");
  870. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  871. } else {
  872. if (!*line)
  873. printf(" :\n");
  874. else
  875. printf(" : %s\n", line);
  876. }
  877. return 0;
  878. }
  879. static struct rb_root root_sym_ext;
  880. static void insert_source_line(struct sym_ext *sym_ext)
  881. {
  882. struct sym_ext *iter;
  883. struct rb_node **p = &root_sym_ext.rb_node;
  884. struct rb_node *parent = NULL;
  885. while (*p != NULL) {
  886. parent = *p;
  887. iter = rb_entry(parent, struct sym_ext, node);
  888. if (sym_ext->percent > iter->percent)
  889. p = &(*p)->rb_left;
  890. else
  891. p = &(*p)->rb_right;
  892. }
  893. rb_link_node(&sym_ext->node, parent, p);
  894. rb_insert_color(&sym_ext->node, &root_sym_ext);
  895. }
  896. static void free_source_line(struct symbol *sym, int len)
  897. {
  898. struct sym_ext *sym_ext = sym->priv;
  899. int i;
  900. if (!sym_ext)
  901. return;
  902. for (i = 0; i < len; i++)
  903. free(sym_ext[i].path);
  904. free(sym_ext);
  905. sym->priv = NULL;
  906. root_sym_ext = RB_ROOT;
  907. }
  908. /* Get the filename:line for the colored entries */
  909. static void
  910. get_source_line(struct symbol *sym, u64 start, int len, char *filename)
  911. {
  912. int i;
  913. char cmd[PATH_MAX * 2];
  914. struct sym_ext *sym_ext;
  915. if (!sym->hist_sum)
  916. return;
  917. sym->priv = calloc(len, sizeof(struct sym_ext));
  918. if (!sym->priv)
  919. return;
  920. sym_ext = sym->priv;
  921. for (i = 0; i < len; i++) {
  922. char *path = NULL;
  923. size_t line_len;
  924. u64 offset;
  925. FILE *fp;
  926. sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
  927. if (sym_ext[i].percent <= 0.5)
  928. continue;
  929. offset = start + i;
  930. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  931. fp = popen(cmd, "r");
  932. if (!fp)
  933. continue;
  934. if (getline(&path, &line_len, fp) < 0 || !line_len)
  935. goto next;
  936. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  937. if (!sym_ext[i].path)
  938. goto next;
  939. strcpy(sym_ext[i].path, path);
  940. insert_source_line(&sym_ext[i]);
  941. next:
  942. pclose(fp);
  943. }
  944. }
  945. static void print_summary(char *filename)
  946. {
  947. struct sym_ext *sym_ext;
  948. struct rb_node *node;
  949. printf("\nSorted summary for file %s\n", filename);
  950. printf("----------------------------------------------\n\n");
  951. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  952. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  953. return;
  954. }
  955. node = rb_first(&root_sym_ext);
  956. while (node) {
  957. double percent;
  958. char *color;
  959. char *path;
  960. sym_ext = rb_entry(node, struct sym_ext, node);
  961. percent = sym_ext->percent;
  962. color = get_percent_color(percent);
  963. path = sym_ext->path;
  964. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  965. node = rb_next(node);
  966. }
  967. }
  968. static void annotate_sym(struct dso *dso, struct symbol *sym)
  969. {
  970. char *filename = dso->name, *d_filename;
  971. u64 start, end, len;
  972. char command[PATH_MAX*2];
  973. FILE *file;
  974. if (!filename)
  975. return;
  976. if (sym->module)
  977. filename = sym->module->path;
  978. else if (dso == kernel_dso)
  979. filename = vmlinux;
  980. start = sym->obj_start;
  981. if (!start)
  982. start = sym->start;
  983. if (full_paths)
  984. d_filename = filename;
  985. else
  986. d_filename = basename(filename);
  987. end = start + sym->end - sym->start + 1;
  988. len = sym->end - sym->start;
  989. if (print_line) {
  990. get_source_line(sym, start, len, filename);
  991. print_summary(filename);
  992. }
  993. printf("\n\n------------------------------------------------\n");
  994. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  995. printf("------------------------------------------------\n");
  996. if (verbose >= 2)
  997. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  998. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  999. (u64)start, (u64)end, filename, filename);
  1000. if (verbose >= 3)
  1001. printf("doing: %s\n", command);
  1002. file = popen(command, "r");
  1003. if (!file)
  1004. return;
  1005. while (!feof(file)) {
  1006. if (parse_line(file, sym, start, len) < 0)
  1007. break;
  1008. }
  1009. pclose(file);
  1010. if (print_line)
  1011. free_source_line(sym, len);
  1012. }
  1013. static void find_annotations(void)
  1014. {
  1015. struct rb_node *nd;
  1016. struct dso *dso;
  1017. int count = 0;
  1018. list_for_each_entry(dso, &dsos, node) {
  1019. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  1020. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  1021. if (sym->hist) {
  1022. annotate_sym(dso, sym);
  1023. count++;
  1024. }
  1025. }
  1026. }
  1027. if (!count)
  1028. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  1029. }
  1030. static int __cmd_annotate(void)
  1031. {
  1032. int ret, rc = EXIT_FAILURE;
  1033. unsigned long offset = 0;
  1034. unsigned long head = 0;
  1035. struct stat stat;
  1036. event_t *event;
  1037. uint32_t size;
  1038. char *buf;
  1039. register_idle_thread();
  1040. input = open(input_name, O_RDONLY);
  1041. if (input < 0) {
  1042. perror("failed to open file");
  1043. exit(-1);
  1044. }
  1045. ret = fstat(input, &stat);
  1046. if (ret < 0) {
  1047. perror("failed to stat file");
  1048. exit(-1);
  1049. }
  1050. if (!force && (stat.st_uid != geteuid())) {
  1051. fprintf(stderr, "file: %s not owned by current user\n", input_name);
  1052. exit(-1);
  1053. }
  1054. if (!stat.st_size) {
  1055. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1056. exit(0);
  1057. }
  1058. if (load_kernel() < 0) {
  1059. perror("failed to load kernel symbols");
  1060. return EXIT_FAILURE;
  1061. }
  1062. remap:
  1063. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1064. MAP_SHARED, input, offset);
  1065. if (buf == MAP_FAILED) {
  1066. perror("failed to mmap file");
  1067. exit(-1);
  1068. }
  1069. more:
  1070. event = (event_t *)(buf + head);
  1071. size = event->header.size;
  1072. if (!size)
  1073. size = 8;
  1074. if (head + event->header.size >= page_size * mmap_window) {
  1075. unsigned long shift = page_size * (head / page_size);
  1076. int ret;
  1077. ret = munmap(buf, page_size * mmap_window);
  1078. assert(ret == 0);
  1079. offset += shift;
  1080. head -= shift;
  1081. goto remap;
  1082. }
  1083. size = event->header.size;
  1084. dprintf("%p [%p]: event: %d\n",
  1085. (void *)(offset + head),
  1086. (void *)(long)event->header.size,
  1087. event->header.type);
  1088. if (!size || process_event(event, offset, head) < 0) {
  1089. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1090. (void *)(offset + head),
  1091. (void *)(long)(event->header.size),
  1092. event->header.type);
  1093. total_unknown++;
  1094. /*
  1095. * assume we lost track of the stream, check alignment, and
  1096. * increment a single u64 in the hope to catch on again 'soon'.
  1097. */
  1098. if (unlikely(head & 7))
  1099. head &= ~7ULL;
  1100. size = 8;
  1101. }
  1102. head += size;
  1103. if (offset + head < (unsigned long)stat.st_size)
  1104. goto more;
  1105. rc = EXIT_SUCCESS;
  1106. close(input);
  1107. dprintf(" IP events: %10ld\n", total);
  1108. dprintf(" mmap events: %10ld\n", total_mmap);
  1109. dprintf(" comm events: %10ld\n", total_comm);
  1110. dprintf(" fork events: %10ld\n", total_fork);
  1111. dprintf(" unknown events: %10ld\n", total_unknown);
  1112. if (dump_trace)
  1113. return 0;
  1114. if (verbose >= 3)
  1115. threads__fprintf(stdout);
  1116. if (verbose >= 2)
  1117. dsos__fprintf(stdout);
  1118. collapse__resort();
  1119. output__resort();
  1120. find_annotations();
  1121. return rc;
  1122. }
  1123. static const char * const annotate_usage[] = {
  1124. "perf annotate [<options>] <command>",
  1125. NULL
  1126. };
  1127. static const struct option options[] = {
  1128. OPT_STRING('i', "input", &input_name, "file",
  1129. "input file name"),
  1130. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  1131. "symbol to annotate"),
  1132. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  1133. OPT_BOOLEAN('v', "verbose", &verbose,
  1134. "be more verbose (show symbol address, etc)"),
  1135. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1136. "dump raw trace in ASCII"),
  1137. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1138. OPT_BOOLEAN('m', "modules", &modules,
  1139. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1140. OPT_BOOLEAN('l', "print-line", &print_line,
  1141. "print matching source lines (may be slow)"),
  1142. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1143. "Don't shorten the displayed pathnames"),
  1144. OPT_END()
  1145. };
  1146. static void setup_sorting(void)
  1147. {
  1148. char *tmp, *tok, *str = strdup(sort_order);
  1149. for (tok = strtok_r(str, ", ", &tmp);
  1150. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1151. if (sort_dimension__add(tok) < 0) {
  1152. error("Unknown --sort key: `%s'", tok);
  1153. usage_with_options(annotate_usage, options);
  1154. }
  1155. }
  1156. free(str);
  1157. }
  1158. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  1159. {
  1160. symbol__init();
  1161. page_size = getpagesize();
  1162. argc = parse_options(argc, argv, options, annotate_usage, 0);
  1163. setup_sorting();
  1164. if (argc) {
  1165. /*
  1166. * Special case: if there's an argument left then assume tha
  1167. * it's a symbol filter:
  1168. */
  1169. if (argc > 1)
  1170. usage_with_options(annotate_usage, options);
  1171. sym_hist_filter = argv[0];
  1172. }
  1173. if (!sym_hist_filter)
  1174. usage_with_options(annotate_usage, options);
  1175. setup_pager();
  1176. return __cmd_annotate();
  1177. }