builtin-annotate.c 23 KB

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