builtin-annotate.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. #include "util/thread.h"
  20. static char const *input_name = "perf.data";
  21. static char default_sort_order[] = "comm,symbol";
  22. static char *sort_order = default_sort_order;
  23. static int input;
  24. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  25. static int full_paths;
  26. static int print_line;
  27. static unsigned long page_size;
  28. static unsigned long mmap_window = 32;
  29. static struct rb_root threads;
  30. static struct thread *last_match;
  31. struct sym_ext {
  32. struct rb_node node;
  33. double percent;
  34. char *path;
  35. };
  36. /*
  37. * histogram, sorted on item, collects counts
  38. */
  39. static struct rb_root hist;
  40. struct hist_entry {
  41. struct rb_node rb_node;
  42. struct thread *thread;
  43. struct map *map;
  44. struct dso *dso;
  45. struct symbol *sym;
  46. u64 ip;
  47. char level;
  48. uint32_t count;
  49. };
  50. /*
  51. * configurable sorting bits
  52. */
  53. struct sort_entry {
  54. struct list_head list;
  55. const char *header;
  56. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  57. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  58. size_t (*print)(FILE *fp, struct hist_entry *);
  59. };
  60. /* --sort pid */
  61. static int64_t
  62. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  63. {
  64. return right->thread->pid - left->thread->pid;
  65. }
  66. static size_t
  67. sort__thread_print(FILE *fp, struct hist_entry *self)
  68. {
  69. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  70. }
  71. static struct sort_entry sort_thread = {
  72. .header = " Command: Pid",
  73. .cmp = sort__thread_cmp,
  74. .print = sort__thread_print,
  75. };
  76. /* --sort comm */
  77. static int64_t
  78. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  79. {
  80. return right->thread->pid - left->thread->pid;
  81. }
  82. static int64_t
  83. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  84. {
  85. char *comm_l = left->thread->comm;
  86. char *comm_r = right->thread->comm;
  87. if (!comm_l || !comm_r) {
  88. if (!comm_l && !comm_r)
  89. return 0;
  90. else if (!comm_l)
  91. return -1;
  92. else
  93. return 1;
  94. }
  95. return strcmp(comm_l, comm_r);
  96. }
  97. static size_t
  98. sort__comm_print(FILE *fp, struct hist_entry *self)
  99. {
  100. return fprintf(fp, "%16s", self->thread->comm);
  101. }
  102. static struct sort_entry sort_comm = {
  103. .header = " Command",
  104. .cmp = sort__comm_cmp,
  105. .collapse = sort__comm_collapse,
  106. .print = sort__comm_print,
  107. };
  108. /* --sort dso */
  109. static int64_t
  110. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  111. {
  112. struct dso *dso_l = left->dso;
  113. struct dso *dso_r = right->dso;
  114. if (!dso_l || !dso_r) {
  115. if (!dso_l && !dso_r)
  116. return 0;
  117. else if (!dso_l)
  118. return -1;
  119. else
  120. return 1;
  121. }
  122. return strcmp(dso_l->name, dso_r->name);
  123. }
  124. static size_t
  125. sort__dso_print(FILE *fp, struct hist_entry *self)
  126. {
  127. if (self->dso)
  128. return fprintf(fp, "%-25s", self->dso->name);
  129. return fprintf(fp, "%016llx ", (u64)self->ip);
  130. }
  131. static struct sort_entry sort_dso = {
  132. .header = "Shared Object ",
  133. .cmp = sort__dso_cmp,
  134. .print = sort__dso_print,
  135. };
  136. /* --sort symbol */
  137. static int64_t
  138. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  139. {
  140. u64 ip_l, ip_r;
  141. if (left->sym == right->sym)
  142. return 0;
  143. ip_l = left->sym ? left->sym->start : left->ip;
  144. ip_r = right->sym ? right->sym->start : right->ip;
  145. return (int64_t)(ip_r - ip_l);
  146. }
  147. static size_t
  148. sort__sym_print(FILE *fp, struct hist_entry *self)
  149. {
  150. size_t ret = 0;
  151. if (verbose)
  152. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  153. if (self->sym) {
  154. ret += fprintf(fp, "[%c] %s",
  155. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  156. } else {
  157. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  158. }
  159. return ret;
  160. }
  161. static struct sort_entry sort_sym = {
  162. .header = "Symbol",
  163. .cmp = sort__sym_cmp,
  164. .print = sort__sym_print,
  165. };
  166. static int sort__need_collapse = 0;
  167. struct sort_dimension {
  168. const char *name;
  169. struct sort_entry *entry;
  170. int taken;
  171. };
  172. static struct sort_dimension sort_dimensions[] = {
  173. { .name = "pid", .entry = &sort_thread, },
  174. { .name = "comm", .entry = &sort_comm, },
  175. { .name = "dso", .entry = &sort_dso, },
  176. { .name = "symbol", .entry = &sort_sym, },
  177. };
  178. static LIST_HEAD(hist_entry__sort_list);
  179. static int sort_dimension__add(char *tok)
  180. {
  181. unsigned int i;
  182. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  183. struct sort_dimension *sd = &sort_dimensions[i];
  184. if (sd->taken)
  185. continue;
  186. if (strncasecmp(tok, sd->name, strlen(tok)))
  187. continue;
  188. if (sd->entry->collapse)
  189. sort__need_collapse = 1;
  190. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  191. sd->taken = 1;
  192. return 0;
  193. }
  194. return -ESRCH;
  195. }
  196. static int64_t
  197. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  198. {
  199. struct sort_entry *se;
  200. int64_t cmp = 0;
  201. list_for_each_entry(se, &hist_entry__sort_list, list) {
  202. cmp = se->cmp(left, right);
  203. if (cmp)
  204. break;
  205. }
  206. return cmp;
  207. }
  208. static int64_t
  209. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  210. {
  211. struct sort_entry *se;
  212. int64_t cmp = 0;
  213. list_for_each_entry(se, &hist_entry__sort_list, list) {
  214. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  215. f = se->collapse ?: se->cmp;
  216. cmp = f(left, right);
  217. if (cmp)
  218. break;
  219. }
  220. return cmp;
  221. }
  222. /*
  223. * collect histogram counts
  224. */
  225. static void hist_hit(struct hist_entry *he, u64 ip)
  226. {
  227. unsigned int sym_size, offset;
  228. struct symbol *sym = he->sym;
  229. he->count++;
  230. if (!sym || !sym->hist)
  231. return;
  232. sym_size = sym->end - sym->start;
  233. offset = ip - sym->start;
  234. if (offset >= sym_size)
  235. return;
  236. sym->hist_sum++;
  237. sym->hist[offset]++;
  238. if (verbose >= 3)
  239. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  240. (void *)(unsigned long)he->sym->start,
  241. he->sym->name,
  242. (void *)(unsigned long)ip, ip - he->sym->start,
  243. sym->hist[offset]);
  244. }
  245. static int
  246. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  247. struct symbol *sym, u64 ip, char level)
  248. {
  249. struct rb_node **p = &hist.rb_node;
  250. struct rb_node *parent = NULL;
  251. struct hist_entry *he;
  252. struct hist_entry entry = {
  253. .thread = thread,
  254. .map = map,
  255. .dso = dso,
  256. .sym = sym,
  257. .ip = ip,
  258. .level = level,
  259. .count = 1,
  260. };
  261. int cmp;
  262. while (*p != NULL) {
  263. parent = *p;
  264. he = rb_entry(parent, struct hist_entry, rb_node);
  265. cmp = hist_entry__cmp(&entry, he);
  266. if (!cmp) {
  267. hist_hit(he, ip);
  268. return 0;
  269. }
  270. if (cmp < 0)
  271. p = &(*p)->rb_left;
  272. else
  273. p = &(*p)->rb_right;
  274. }
  275. he = malloc(sizeof(*he));
  276. if (!he)
  277. return -ENOMEM;
  278. *he = entry;
  279. rb_link_node(&he->rb_node, parent, p);
  280. rb_insert_color(&he->rb_node, &hist);
  281. return 0;
  282. }
  283. static void hist_entry__free(struct hist_entry *he)
  284. {
  285. free(he);
  286. }
  287. /*
  288. * collapse the histogram
  289. */
  290. static struct rb_root collapse_hists;
  291. static void collapse__insert_entry(struct hist_entry *he)
  292. {
  293. struct rb_node **p = &collapse_hists.rb_node;
  294. struct rb_node *parent = NULL;
  295. struct hist_entry *iter;
  296. int64_t cmp;
  297. while (*p != NULL) {
  298. parent = *p;
  299. iter = rb_entry(parent, struct hist_entry, rb_node);
  300. cmp = hist_entry__collapse(iter, he);
  301. if (!cmp) {
  302. iter->count += he->count;
  303. hist_entry__free(he);
  304. return;
  305. }
  306. if (cmp < 0)
  307. p = &(*p)->rb_left;
  308. else
  309. p = &(*p)->rb_right;
  310. }
  311. rb_link_node(&he->rb_node, parent, p);
  312. rb_insert_color(&he->rb_node, &collapse_hists);
  313. }
  314. static void collapse__resort(void)
  315. {
  316. struct rb_node *next;
  317. struct hist_entry *n;
  318. if (!sort__need_collapse)
  319. return;
  320. next = rb_first(&hist);
  321. while (next) {
  322. n = rb_entry(next, struct hist_entry, rb_node);
  323. next = rb_next(&n->rb_node);
  324. rb_erase(&n->rb_node, &hist);
  325. collapse__insert_entry(n);
  326. }
  327. }
  328. /*
  329. * reverse the map, sort on count.
  330. */
  331. static struct rb_root output_hists;
  332. static void output__insert_entry(struct hist_entry *he)
  333. {
  334. struct rb_node **p = &output_hists.rb_node;
  335. struct rb_node *parent = NULL;
  336. struct hist_entry *iter;
  337. while (*p != NULL) {
  338. parent = *p;
  339. iter = rb_entry(parent, struct hist_entry, rb_node);
  340. if (he->count > iter->count)
  341. p = &(*p)->rb_left;
  342. else
  343. p = &(*p)->rb_right;
  344. }
  345. rb_link_node(&he->rb_node, parent, p);
  346. rb_insert_color(&he->rb_node, &output_hists);
  347. }
  348. static void output__resort(void)
  349. {
  350. struct rb_node *next;
  351. struct hist_entry *n;
  352. struct rb_root *tree = &hist;
  353. if (sort__need_collapse)
  354. tree = &collapse_hists;
  355. next = rb_first(tree);
  356. while (next) {
  357. n = rb_entry(next, struct hist_entry, rb_node);
  358. next = rb_next(&n->rb_node);
  359. rb_erase(&n->rb_node, tree);
  360. output__insert_entry(n);
  361. }
  362. }
  363. static void register_idle_thread(void)
  364. {
  365. struct thread *thread = threads__findnew(0, &threads, &last_match);
  366. if (thread == NULL ||
  367. thread__set_comm(thread, "[idle]")) {
  368. fprintf(stderr, "problem inserting idle task.\n");
  369. exit(-1);
  370. }
  371. }
  372. static unsigned long total = 0,
  373. total_mmap = 0,
  374. total_comm = 0,
  375. total_fork = 0,
  376. total_unknown = 0;
  377. static int
  378. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  379. {
  380. char level;
  381. int show = 0;
  382. struct dso *dso = NULL;
  383. struct thread *thread;
  384. u64 ip = event->ip.ip;
  385. struct map *map = NULL;
  386. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  387. dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  388. (void *)(offset + head),
  389. (void *)(long)(event->header.size),
  390. event->header.misc,
  391. event->ip.pid,
  392. (void *)(long)ip);
  393. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  394. if (thread == NULL) {
  395. fprintf(stderr, "problem processing %d event, skipping it.\n",
  396. event->header.type);
  397. return -1;
  398. }
  399. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  400. show = SHOW_KERNEL;
  401. level = 'k';
  402. dso = kernel_dso;
  403. dump_printf(" ...... dso: %s\n", dso->name);
  404. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  405. show = SHOW_USER;
  406. level = '.';
  407. map = thread__find_map(thread, ip);
  408. if (map != NULL) {
  409. ip = map->map_ip(map, ip);
  410. dso = map->dso;
  411. } else {
  412. /*
  413. * If this is outside of all known maps,
  414. * and is a negative address, try to look it
  415. * up in the kernel dso, as it might be a
  416. * vsyscall (which executes in user-mode):
  417. */
  418. if ((long long)ip < 0)
  419. dso = kernel_dso;
  420. }
  421. dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  422. } else {
  423. show = SHOW_HV;
  424. level = 'H';
  425. dump_printf(" ...... dso: [hypervisor]\n");
  426. }
  427. if (show & show_mask) {
  428. struct symbol *sym = NULL;
  429. if (dso)
  430. sym = dso->find_symbol(dso, ip);
  431. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  432. fprintf(stderr,
  433. "problem incrementing symbol count, skipping event\n");
  434. return -1;
  435. }
  436. }
  437. total++;
  438. return 0;
  439. }
  440. static int
  441. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  442. {
  443. struct thread *thread;
  444. struct map *map = map__new(&event->mmap, NULL, 0);
  445. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  446. dump_printf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  447. (void *)(offset + head),
  448. (void *)(long)(event->header.size),
  449. event->mmap.pid,
  450. (void *)(long)event->mmap.start,
  451. (void *)(long)event->mmap.len,
  452. (void *)(long)event->mmap.pgoff,
  453. event->mmap.filename);
  454. if (thread == NULL || map == NULL) {
  455. dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  456. return 0;
  457. }
  458. thread__insert_map(thread, map);
  459. total_mmap++;
  460. return 0;
  461. }
  462. static int
  463. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  464. {
  465. struct thread *thread;
  466. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  467. dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  468. (void *)(offset + head),
  469. (void *)(long)(event->header.size),
  470. event->comm.comm, event->comm.pid);
  471. if (thread == NULL ||
  472. thread__set_comm(thread, event->comm.comm)) {
  473. dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
  474. return -1;
  475. }
  476. total_comm++;
  477. return 0;
  478. }
  479. static int
  480. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  481. {
  482. struct thread *thread;
  483. struct thread *parent;
  484. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  485. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  486. dump_printf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  487. (void *)(offset + head),
  488. (void *)(long)(event->header.size),
  489. event->fork.pid, event->fork.ppid);
  490. if (!thread || !parent || thread__fork(thread, parent)) {
  491. dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
  492. return -1;
  493. }
  494. total_fork++;
  495. return 0;
  496. }
  497. static int
  498. process_event(event_t *event, unsigned long offset, unsigned long head)
  499. {
  500. switch (event->header.type) {
  501. case PERF_EVENT_SAMPLE:
  502. return process_sample_event(event, offset, head);
  503. case PERF_EVENT_MMAP:
  504. return process_mmap_event(event, offset, head);
  505. case PERF_EVENT_COMM:
  506. return process_comm_event(event, offset, head);
  507. case PERF_EVENT_FORK:
  508. return process_fork_event(event, offset, head);
  509. /*
  510. * We dont process them right now but they are fine:
  511. */
  512. case PERF_EVENT_THROTTLE:
  513. case PERF_EVENT_UNTHROTTLE:
  514. return 0;
  515. default:
  516. return -1;
  517. }
  518. return 0;
  519. }
  520. static int
  521. parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
  522. {
  523. char *line = NULL, *tmp, *tmp2;
  524. static const char *prev_line;
  525. static const char *prev_color;
  526. unsigned int offset;
  527. size_t line_len;
  528. s64 line_ip;
  529. int ret;
  530. char *c;
  531. if (getline(&line, &line_len, file) < 0)
  532. return -1;
  533. if (!line)
  534. return -1;
  535. c = strchr(line, '\n');
  536. if (c)
  537. *c = 0;
  538. line_ip = -1;
  539. offset = 0;
  540. ret = -2;
  541. /*
  542. * Strip leading spaces:
  543. */
  544. tmp = line;
  545. while (*tmp) {
  546. if (*tmp != ' ')
  547. break;
  548. tmp++;
  549. }
  550. if (*tmp) {
  551. /*
  552. * Parse hexa addresses followed by ':'
  553. */
  554. line_ip = strtoull(tmp, &tmp2, 16);
  555. if (*tmp2 != ':')
  556. line_ip = -1;
  557. }
  558. if (line_ip != -1) {
  559. const char *path = NULL;
  560. unsigned int hits = 0;
  561. double percent = 0.0;
  562. const char *color;
  563. struct sym_ext *sym_ext = sym->priv;
  564. offset = line_ip - start;
  565. if (offset < len)
  566. hits = sym->hist[offset];
  567. if (offset < len && sym_ext) {
  568. path = sym_ext[offset].path;
  569. percent = sym_ext[offset].percent;
  570. } else if (sym->hist_sum)
  571. percent = 100.0 * hits / sym->hist_sum;
  572. color = get_percent_color(percent);
  573. /*
  574. * Also color the filename and line if needed, with
  575. * the same color than the percentage. Don't print it
  576. * twice for close colored ip with the same filename:line
  577. */
  578. if (path) {
  579. if (!prev_line || strcmp(prev_line, path)
  580. || color != prev_color) {
  581. color_fprintf(stdout, color, " %s", path);
  582. prev_line = path;
  583. prev_color = color;
  584. }
  585. }
  586. color_fprintf(stdout, color, " %7.2f", percent);
  587. printf(" : ");
  588. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  589. } else {
  590. if (!*line)
  591. printf(" :\n");
  592. else
  593. printf(" : %s\n", line);
  594. }
  595. return 0;
  596. }
  597. static struct rb_root root_sym_ext;
  598. static void insert_source_line(struct sym_ext *sym_ext)
  599. {
  600. struct sym_ext *iter;
  601. struct rb_node **p = &root_sym_ext.rb_node;
  602. struct rb_node *parent = NULL;
  603. while (*p != NULL) {
  604. parent = *p;
  605. iter = rb_entry(parent, struct sym_ext, node);
  606. if (sym_ext->percent > iter->percent)
  607. p = &(*p)->rb_left;
  608. else
  609. p = &(*p)->rb_right;
  610. }
  611. rb_link_node(&sym_ext->node, parent, p);
  612. rb_insert_color(&sym_ext->node, &root_sym_ext);
  613. }
  614. static void free_source_line(struct symbol *sym, int len)
  615. {
  616. struct sym_ext *sym_ext = sym->priv;
  617. int i;
  618. if (!sym_ext)
  619. return;
  620. for (i = 0; i < len; i++)
  621. free(sym_ext[i].path);
  622. free(sym_ext);
  623. sym->priv = NULL;
  624. root_sym_ext = RB_ROOT;
  625. }
  626. /* Get the filename:line for the colored entries */
  627. static void
  628. get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
  629. {
  630. int i;
  631. char cmd[PATH_MAX * 2];
  632. struct sym_ext *sym_ext;
  633. if (!sym->hist_sum)
  634. return;
  635. sym->priv = calloc(len, sizeof(struct sym_ext));
  636. if (!sym->priv)
  637. return;
  638. sym_ext = sym->priv;
  639. for (i = 0; i < len; i++) {
  640. char *path = NULL;
  641. size_t line_len;
  642. u64 offset;
  643. FILE *fp;
  644. sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
  645. if (sym_ext[i].percent <= 0.5)
  646. continue;
  647. offset = start + i;
  648. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  649. fp = popen(cmd, "r");
  650. if (!fp)
  651. continue;
  652. if (getline(&path, &line_len, fp) < 0 || !line_len)
  653. goto next;
  654. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  655. if (!sym_ext[i].path)
  656. goto next;
  657. strcpy(sym_ext[i].path, path);
  658. insert_source_line(&sym_ext[i]);
  659. next:
  660. pclose(fp);
  661. }
  662. }
  663. static void print_summary(const char *filename)
  664. {
  665. struct sym_ext *sym_ext;
  666. struct rb_node *node;
  667. printf("\nSorted summary for file %s\n", filename);
  668. printf("----------------------------------------------\n\n");
  669. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  670. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  671. return;
  672. }
  673. node = rb_first(&root_sym_ext);
  674. while (node) {
  675. double percent;
  676. const char *color;
  677. char *path;
  678. sym_ext = rb_entry(node, struct sym_ext, node);
  679. percent = sym_ext->percent;
  680. color = get_percent_color(percent);
  681. path = sym_ext->path;
  682. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  683. node = rb_next(node);
  684. }
  685. }
  686. static void annotate_sym(struct dso *dso, struct symbol *sym)
  687. {
  688. const char *filename = dso->name, *d_filename;
  689. u64 start, end, len;
  690. char command[PATH_MAX*2];
  691. FILE *file;
  692. if (!filename)
  693. return;
  694. if (sym->module)
  695. filename = sym->module->path;
  696. else if (dso == kernel_dso)
  697. filename = vmlinux_name;
  698. start = sym->obj_start;
  699. if (!start)
  700. start = sym->start;
  701. if (full_paths)
  702. d_filename = filename;
  703. else
  704. d_filename = basename(filename);
  705. end = start + sym->end - sym->start + 1;
  706. len = sym->end - sym->start;
  707. if (print_line) {
  708. get_source_line(sym, start, len, filename);
  709. print_summary(filename);
  710. }
  711. printf("\n\n------------------------------------------------\n");
  712. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  713. printf("------------------------------------------------\n");
  714. if (verbose >= 2)
  715. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  716. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  717. (u64)start, (u64)end, filename, filename);
  718. if (verbose >= 3)
  719. printf("doing: %s\n", command);
  720. file = popen(command, "r");
  721. if (!file)
  722. return;
  723. while (!feof(file)) {
  724. if (parse_line(file, sym, start, len) < 0)
  725. break;
  726. }
  727. pclose(file);
  728. if (print_line)
  729. free_source_line(sym, len);
  730. }
  731. static void find_annotations(void)
  732. {
  733. struct rb_node *nd;
  734. struct dso *dso;
  735. int count = 0;
  736. list_for_each_entry(dso, &dsos, node) {
  737. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  738. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  739. if (sym->hist) {
  740. annotate_sym(dso, sym);
  741. count++;
  742. }
  743. }
  744. }
  745. if (!count)
  746. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  747. }
  748. static int __cmd_annotate(void)
  749. {
  750. int ret, rc = EXIT_FAILURE;
  751. unsigned long offset = 0;
  752. unsigned long head = 0;
  753. struct stat input_stat;
  754. event_t *event;
  755. uint32_t size;
  756. char *buf;
  757. register_idle_thread();
  758. input = open(input_name, O_RDONLY);
  759. if (input < 0) {
  760. perror("failed to open file");
  761. exit(-1);
  762. }
  763. ret = fstat(input, &input_stat);
  764. if (ret < 0) {
  765. perror("failed to stat file");
  766. exit(-1);
  767. }
  768. if (!input_stat.st_size) {
  769. fprintf(stderr, "zero-sized file, nothing to do!\n");
  770. exit(0);
  771. }
  772. if (load_kernel() < 0) {
  773. perror("failed to load kernel symbols");
  774. return EXIT_FAILURE;
  775. }
  776. remap:
  777. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  778. MAP_SHARED, input, offset);
  779. if (buf == MAP_FAILED) {
  780. perror("failed to mmap file");
  781. exit(-1);
  782. }
  783. more:
  784. event = (event_t *)(buf + head);
  785. size = event->header.size;
  786. if (!size)
  787. size = 8;
  788. if (head + event->header.size >= page_size * mmap_window) {
  789. unsigned long shift = page_size * (head / page_size);
  790. int munmap_ret;
  791. munmap_ret = munmap(buf, page_size * mmap_window);
  792. assert(munmap_ret == 0);
  793. offset += shift;
  794. head -= shift;
  795. goto remap;
  796. }
  797. size = event->header.size;
  798. dump_printf("%p [%p]: event: %d\n",
  799. (void *)(offset + head),
  800. (void *)(long)event->header.size,
  801. event->header.type);
  802. if (!size || process_event(event, offset, head) < 0) {
  803. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  804. (void *)(offset + head),
  805. (void *)(long)(event->header.size),
  806. event->header.type);
  807. total_unknown++;
  808. /*
  809. * assume we lost track of the stream, check alignment, and
  810. * increment a single u64 in the hope to catch on again 'soon'.
  811. */
  812. if (unlikely(head & 7))
  813. head &= ~7ULL;
  814. size = 8;
  815. }
  816. head += size;
  817. if (offset + head < (unsigned long)input_stat.st_size)
  818. goto more;
  819. rc = EXIT_SUCCESS;
  820. close(input);
  821. dump_printf(" IP events: %10ld\n", total);
  822. dump_printf(" mmap events: %10ld\n", total_mmap);
  823. dump_printf(" comm events: %10ld\n", total_comm);
  824. dump_printf(" fork events: %10ld\n", total_fork);
  825. dump_printf(" unknown events: %10ld\n", total_unknown);
  826. if (dump_trace)
  827. return 0;
  828. if (verbose >= 3)
  829. threads__fprintf(stdout, &threads);
  830. if (verbose >= 2)
  831. dsos__fprintf(stdout);
  832. collapse__resort();
  833. output__resort();
  834. find_annotations();
  835. return rc;
  836. }
  837. static const char * const annotate_usage[] = {
  838. "perf annotate [<options>] <command>",
  839. NULL
  840. };
  841. static const struct option options[] = {
  842. OPT_STRING('i', "input", &input_name, "file",
  843. "input file name"),
  844. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  845. "symbol to annotate"),
  846. OPT_BOOLEAN('v', "verbose", &verbose,
  847. "be more verbose (show symbol address, etc)"),
  848. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  849. "dump raw trace in ASCII"),
  850. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  851. OPT_BOOLEAN('m', "modules", &modules,
  852. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  853. OPT_BOOLEAN('l', "print-line", &print_line,
  854. "print matching source lines (may be slow)"),
  855. OPT_BOOLEAN('P', "full-paths", &full_paths,
  856. "Don't shorten the displayed pathnames"),
  857. OPT_END()
  858. };
  859. static void setup_sorting(void)
  860. {
  861. char *tmp, *tok, *str = strdup(sort_order);
  862. for (tok = strtok_r(str, ", ", &tmp);
  863. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  864. if (sort_dimension__add(tok) < 0) {
  865. error("Unknown --sort key: `%s'", tok);
  866. usage_with_options(annotate_usage, options);
  867. }
  868. }
  869. free(str);
  870. }
  871. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  872. {
  873. symbol__init();
  874. page_size = getpagesize();
  875. argc = parse_options(argc, argv, options, annotate_usage, 0);
  876. setup_sorting();
  877. if (argc) {
  878. /*
  879. * Special case: if there's an argument left then assume tha
  880. * it's a symbol filter:
  881. */
  882. if (argc > 1)
  883. usage_with_options(annotate_usage, options);
  884. sym_hist_filter = argv[0];
  885. }
  886. if (!sym_hist_filter)
  887. usage_with_options(annotate_usage, options);
  888. setup_pager();
  889. return __cmd_annotate();
  890. }