builtin-annotate.c 23 KB

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