builtin-annotate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. #include "util/sort.h"
  22. #include "util/hist.h"
  23. #include "util/process_events.h"
  24. static char const *input_name = "perf.data";
  25. static int force;
  26. static int input;
  27. static int full_paths;
  28. static int print_line;
  29. static bool use_modules;
  30. static unsigned long page_size;
  31. static unsigned long mmap_window = 32;
  32. const char *vmlinux_name;
  33. struct sym_hist {
  34. u64 sum;
  35. u64 ip[0];
  36. };
  37. struct sym_ext {
  38. struct rb_node node;
  39. double percent;
  40. char *path;
  41. };
  42. struct sym_priv {
  43. struct sym_hist *hist;
  44. struct sym_ext *ext;
  45. };
  46. static const char *sym_hist_filter;
  47. static int symbol_filter(struct map *map __used, struct symbol *sym)
  48. {
  49. if (sym_hist_filter == NULL ||
  50. strcmp(sym->name, sym_hist_filter) == 0) {
  51. struct sym_priv *priv = symbol__priv(sym);
  52. const int size = (sizeof(*priv->hist) +
  53. (sym->end - sym->start) * sizeof(u64));
  54. priv->hist = malloc(size);
  55. if (priv->hist)
  56. memset(priv->hist, 0, size);
  57. return 0;
  58. }
  59. /*
  60. * FIXME: We should really filter it out, as we don't want to go thru symbols
  61. * we're not interested, and if a DSO ends up with no symbols, delete it too,
  62. * but right now the kernel loading routines in symbol.c bail out if no symbols
  63. * are found, fix it later.
  64. */
  65. return 0;
  66. }
  67. /*
  68. * collect histogram counts
  69. */
  70. static void hist_hit(struct hist_entry *he, u64 ip)
  71. {
  72. unsigned int sym_size, offset;
  73. struct symbol *sym = he->sym;
  74. struct sym_priv *priv;
  75. struct sym_hist *h;
  76. he->count++;
  77. if (!sym || !he->map)
  78. return;
  79. priv = symbol__priv(sym);
  80. if (!priv->hist)
  81. return;
  82. sym_size = sym->end - sym->start;
  83. offset = ip - sym->start;
  84. if (verbose)
  85. fprintf(stderr, "%s: ip=%Lx\n", __func__,
  86. he->map->unmap_ip(he->map, ip));
  87. if (offset >= sym_size)
  88. return;
  89. h = priv->hist;
  90. h->sum++;
  91. h->ip[offset]++;
  92. if (verbose >= 3)
  93. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  94. (void *)(unsigned long)he->sym->start,
  95. he->sym->name,
  96. (void *)(unsigned long)ip, ip - he->sym->start,
  97. h->ip[offset]);
  98. }
  99. static int hist_entry__add(struct thread *thread, struct map *map,
  100. struct symbol *sym, u64 ip, u64 count, char level)
  101. {
  102. bool hit;
  103. struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
  104. count, level, &hit);
  105. if (he == NULL)
  106. return -ENOMEM;
  107. hist_hit(he, ip);
  108. return 0;
  109. }
  110. static int
  111. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  112. {
  113. char level;
  114. u64 ip = event->ip.ip;
  115. struct map *map = NULL;
  116. struct symbol *sym = NULL;
  117. struct thread *thread = threads__findnew(event->ip.pid);
  118. dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  119. (void *)(offset + head),
  120. (void *)(long)(event->header.size),
  121. event->header.misc,
  122. event->ip.pid,
  123. (void *)(long)ip);
  124. if (thread == NULL) {
  125. fprintf(stderr, "problem processing %d event, skipping it.\n",
  126. event->header.type);
  127. return -1;
  128. }
  129. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  130. if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
  131. level = 'k';
  132. sym = kernel_maps__find_symbol(ip, &map, symbol_filter);
  133. dump_printf(" ...... dso: %s\n",
  134. map ? map->dso->long_name : "<not found>");
  135. } else if (event->header.misc & PERF_RECORD_MISC_USER) {
  136. level = '.';
  137. map = thread__find_map(thread, ip);
  138. if (map != NULL) {
  139. got_map:
  140. ip = map->map_ip(map, ip);
  141. sym = map__find_symbol(map, ip, symbol_filter);
  142. } else {
  143. /*
  144. * If this is outside of all known maps,
  145. * and is a negative address, try to look it
  146. * up in the kernel dso, as it might be a
  147. * vsyscall or vdso (which executes in user-mode).
  148. *
  149. * XXX This is nasty, we should have a symbol list in
  150. * the "[vdso]" dso, but for now lets use the old
  151. * trick of looking in the whole kernel symbol list.
  152. */
  153. if ((long long)ip < 0) {
  154. map = kernel_map;
  155. goto got_map;
  156. }
  157. }
  158. dump_printf(" ...... dso: %s\n",
  159. map ? map->dso->long_name : "<not found>");
  160. } else {
  161. level = 'H';
  162. dump_printf(" ...... dso: [hypervisor]\n");
  163. }
  164. if (hist_entry__add(thread, map, sym, ip, 1, level)) {
  165. fprintf(stderr, "problem incrementing symbol count, "
  166. "skipping event\n");
  167. return -1;
  168. }
  169. total++;
  170. return 0;
  171. }
  172. static int
  173. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  174. {
  175. struct thread *thread = threads__findnew(event->comm.pid);
  176. dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
  177. (void *)(offset + head),
  178. (void *)(long)(event->header.size),
  179. event->comm.comm, event->comm.pid);
  180. if (thread == NULL ||
  181. thread__set_comm(thread, event->comm.comm)) {
  182. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  183. return -1;
  184. }
  185. total_comm++;
  186. return 0;
  187. }
  188. static int
  189. process_event(event_t *event, unsigned long offset, unsigned long head)
  190. {
  191. switch (event->header.type) {
  192. case PERF_RECORD_SAMPLE:
  193. return process_sample_event(event, offset, head);
  194. case PERF_RECORD_MMAP:
  195. return process_mmap_event(event, offset, head);
  196. case PERF_RECORD_COMM:
  197. return process_comm_event(event, offset, head);
  198. case PERF_RECORD_FORK:
  199. return process_task_event(event, offset, head);
  200. /*
  201. * We dont process them right now but they are fine:
  202. */
  203. case PERF_RECORD_THROTTLE:
  204. case PERF_RECORD_UNTHROTTLE:
  205. return 0;
  206. default:
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. static int parse_line(FILE *file, struct hist_entry *he, u64 len)
  212. {
  213. struct symbol *sym = he->sym;
  214. char *line = NULL, *tmp, *tmp2;
  215. static const char *prev_line;
  216. static const char *prev_color;
  217. unsigned int offset;
  218. size_t line_len;
  219. u64 start;
  220. s64 line_ip;
  221. int ret;
  222. char *c;
  223. if (getline(&line, &line_len, file) < 0)
  224. return -1;
  225. if (!line)
  226. return -1;
  227. c = strchr(line, '\n');
  228. if (c)
  229. *c = 0;
  230. line_ip = -1;
  231. offset = 0;
  232. ret = -2;
  233. /*
  234. * Strip leading spaces:
  235. */
  236. tmp = line;
  237. while (*tmp) {
  238. if (*tmp != ' ')
  239. break;
  240. tmp++;
  241. }
  242. if (*tmp) {
  243. /*
  244. * Parse hexa addresses followed by ':'
  245. */
  246. line_ip = strtoull(tmp, &tmp2, 16);
  247. if (*tmp2 != ':')
  248. line_ip = -1;
  249. }
  250. start = he->map->unmap_ip(he->map, sym->start);
  251. if (line_ip != -1) {
  252. const char *path = NULL;
  253. unsigned int hits = 0;
  254. double percent = 0.0;
  255. const char *color;
  256. struct sym_priv *priv = symbol__priv(sym);
  257. struct sym_ext *sym_ext = priv->ext;
  258. struct sym_hist *h = priv->hist;
  259. offset = line_ip - start;
  260. if (offset < len)
  261. hits = h->ip[offset];
  262. if (offset < len && sym_ext) {
  263. path = sym_ext[offset].path;
  264. percent = sym_ext[offset].percent;
  265. } else if (h->sum)
  266. percent = 100.0 * hits / h->sum;
  267. color = get_percent_color(percent);
  268. /*
  269. * Also color the filename and line if needed, with
  270. * the same color than the percentage. Don't print it
  271. * twice for close colored ip with the same filename:line
  272. */
  273. if (path) {
  274. if (!prev_line || strcmp(prev_line, path)
  275. || color != prev_color) {
  276. color_fprintf(stdout, color, " %s", path);
  277. prev_line = path;
  278. prev_color = color;
  279. }
  280. }
  281. color_fprintf(stdout, color, " %7.2f", percent);
  282. printf(" : ");
  283. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  284. } else {
  285. if (!*line)
  286. printf(" :\n");
  287. else
  288. printf(" : %s\n", line);
  289. }
  290. return 0;
  291. }
  292. static struct rb_root root_sym_ext;
  293. static void insert_source_line(struct sym_ext *sym_ext)
  294. {
  295. struct sym_ext *iter;
  296. struct rb_node **p = &root_sym_ext.rb_node;
  297. struct rb_node *parent = NULL;
  298. while (*p != NULL) {
  299. parent = *p;
  300. iter = rb_entry(parent, struct sym_ext, node);
  301. if (sym_ext->percent > iter->percent)
  302. p = &(*p)->rb_left;
  303. else
  304. p = &(*p)->rb_right;
  305. }
  306. rb_link_node(&sym_ext->node, parent, p);
  307. rb_insert_color(&sym_ext->node, &root_sym_ext);
  308. }
  309. static void free_source_line(struct hist_entry *he, int len)
  310. {
  311. struct sym_priv *priv = symbol__priv(he->sym);
  312. struct sym_ext *sym_ext = priv->ext;
  313. int i;
  314. if (!sym_ext)
  315. return;
  316. for (i = 0; i < len; i++)
  317. free(sym_ext[i].path);
  318. free(sym_ext);
  319. priv->ext = NULL;
  320. root_sym_ext = RB_ROOT;
  321. }
  322. /* Get the filename:line for the colored entries */
  323. static void
  324. get_source_line(struct hist_entry *he, int len, const char *filename)
  325. {
  326. struct symbol *sym = he->sym;
  327. u64 start;
  328. int i;
  329. char cmd[PATH_MAX * 2];
  330. struct sym_ext *sym_ext;
  331. struct sym_priv *priv = symbol__priv(sym);
  332. struct sym_hist *h = priv->hist;
  333. if (!h->sum)
  334. return;
  335. sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
  336. if (!priv->ext)
  337. return;
  338. start = he->map->unmap_ip(he->map, sym->start);
  339. for (i = 0; i < len; i++) {
  340. char *path = NULL;
  341. size_t line_len;
  342. u64 offset;
  343. FILE *fp;
  344. sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
  345. if (sym_ext[i].percent <= 0.5)
  346. continue;
  347. offset = start + i;
  348. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  349. fp = popen(cmd, "r");
  350. if (!fp)
  351. continue;
  352. if (getline(&path, &line_len, fp) < 0 || !line_len)
  353. goto next;
  354. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  355. if (!sym_ext[i].path)
  356. goto next;
  357. strcpy(sym_ext[i].path, path);
  358. insert_source_line(&sym_ext[i]);
  359. next:
  360. pclose(fp);
  361. }
  362. }
  363. static void print_summary(const char *filename)
  364. {
  365. struct sym_ext *sym_ext;
  366. struct rb_node *node;
  367. printf("\nSorted summary for file %s\n", filename);
  368. printf("----------------------------------------------\n\n");
  369. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  370. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  371. return;
  372. }
  373. node = rb_first(&root_sym_ext);
  374. while (node) {
  375. double percent;
  376. const char *color;
  377. char *path;
  378. sym_ext = rb_entry(node, struct sym_ext, node);
  379. percent = sym_ext->percent;
  380. color = get_percent_color(percent);
  381. path = sym_ext->path;
  382. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  383. node = rb_next(node);
  384. }
  385. }
  386. static void annotate_sym(struct hist_entry *he)
  387. {
  388. struct map *map = he->map;
  389. struct dso *dso = map->dso;
  390. struct symbol *sym = he->sym;
  391. const char *filename = dso->long_name, *d_filename;
  392. u64 len;
  393. char command[PATH_MAX*2];
  394. FILE *file;
  395. if (!filename)
  396. return;
  397. if (verbose)
  398. fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
  399. __func__, filename, sym->name,
  400. map->unmap_ip(map, sym->start),
  401. map->unmap_ip(map, sym->end));
  402. if (full_paths)
  403. d_filename = filename;
  404. else
  405. d_filename = basename(filename);
  406. len = sym->end - sym->start;
  407. if (print_line) {
  408. get_source_line(he, len, filename);
  409. print_summary(filename);
  410. }
  411. printf("\n\n------------------------------------------------\n");
  412. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  413. printf("------------------------------------------------\n");
  414. if (verbose >= 2)
  415. printf("annotating [%p] %30s : [%p] %30s\n",
  416. dso, dso->long_name, sym, sym->name);
  417. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  418. map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
  419. filename, filename);
  420. if (verbose >= 3)
  421. printf("doing: %s\n", command);
  422. file = popen(command, "r");
  423. if (!file)
  424. return;
  425. while (!feof(file)) {
  426. if (parse_line(file, he, len) < 0)
  427. break;
  428. }
  429. pclose(file);
  430. if (print_line)
  431. free_source_line(he, len);
  432. }
  433. static void find_annotations(void)
  434. {
  435. struct rb_node *nd;
  436. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  437. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  438. struct sym_priv *priv;
  439. if (he->sym == NULL)
  440. continue;
  441. priv = symbol__priv(he->sym);
  442. if (priv->hist == NULL)
  443. continue;
  444. annotate_sym(he);
  445. /*
  446. * Since we have a hist_entry per IP for the same symbol, free
  447. * he->sym->hist to signal we already processed this symbol.
  448. */
  449. free(priv->hist);
  450. priv->hist = NULL;
  451. }
  452. }
  453. static int __cmd_annotate(void)
  454. {
  455. int ret, rc = EXIT_FAILURE;
  456. unsigned long offset = 0;
  457. unsigned long head = 0;
  458. struct stat input_stat;
  459. event_t *event;
  460. uint32_t size;
  461. char *buf;
  462. register_idle_thread();
  463. input = open(input_name, O_RDONLY);
  464. if (input < 0) {
  465. perror("failed to open file");
  466. exit(-1);
  467. }
  468. ret = fstat(input, &input_stat);
  469. if (ret < 0) {
  470. perror("failed to stat file");
  471. exit(-1);
  472. }
  473. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  474. fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
  475. exit(-1);
  476. }
  477. if (!input_stat.st_size) {
  478. fprintf(stderr, "zero-sized file, nothing to do!\n");
  479. exit(0);
  480. }
  481. if (kernel_maps__init(vmlinux_name, true, use_modules) < 0) {
  482. pr_err("failed to create kernel maps for symbol resolution\b");
  483. return -1;
  484. }
  485. remap:
  486. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  487. MAP_SHARED, input, offset);
  488. if (buf == MAP_FAILED) {
  489. perror("failed to mmap file");
  490. exit(-1);
  491. }
  492. more:
  493. event = (event_t *)(buf + head);
  494. size = event->header.size;
  495. if (!size)
  496. size = 8;
  497. if (head + event->header.size >= page_size * mmap_window) {
  498. unsigned long shift = page_size * (head / page_size);
  499. int munmap_ret;
  500. munmap_ret = munmap(buf, page_size * mmap_window);
  501. assert(munmap_ret == 0);
  502. offset += shift;
  503. head -= shift;
  504. goto remap;
  505. }
  506. size = event->header.size;
  507. dump_printf("%p [%p]: event: %d\n",
  508. (void *)(offset + head),
  509. (void *)(long)event->header.size,
  510. event->header.type);
  511. if (!size || process_event(event, offset, head) < 0) {
  512. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  513. (void *)(offset + head),
  514. (void *)(long)(event->header.size),
  515. event->header.type);
  516. total_unknown++;
  517. /*
  518. * assume we lost track of the stream, check alignment, and
  519. * increment a single u64 in the hope to catch on again 'soon'.
  520. */
  521. if (unlikely(head & 7))
  522. head &= ~7ULL;
  523. size = 8;
  524. }
  525. head += size;
  526. if (offset + head < (unsigned long)input_stat.st_size)
  527. goto more;
  528. rc = EXIT_SUCCESS;
  529. close(input);
  530. dump_printf(" IP events: %10ld\n", total);
  531. dump_printf(" mmap events: %10ld\n", total_mmap);
  532. dump_printf(" comm events: %10ld\n", total_comm);
  533. dump_printf(" fork events: %10ld\n", total_fork);
  534. dump_printf(" unknown events: %10ld\n", total_unknown);
  535. if (dump_trace)
  536. return 0;
  537. if (verbose > 3)
  538. threads__fprintf(stdout);
  539. if (verbose > 2)
  540. dsos__fprintf(stdout);
  541. collapse__resort();
  542. output__resort(total);
  543. find_annotations();
  544. return rc;
  545. }
  546. static const char * const annotate_usage[] = {
  547. "perf annotate [<options>] <command>",
  548. NULL
  549. };
  550. static const struct option options[] = {
  551. OPT_STRING('i', "input", &input_name, "file",
  552. "input file name"),
  553. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  554. "symbol to annotate"),
  555. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  556. OPT_BOOLEAN('v', "verbose", &verbose,
  557. "be more verbose (show symbol address, etc)"),
  558. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  559. "dump raw trace in ASCII"),
  560. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  561. OPT_BOOLEAN('m', "modules", &use_modules,
  562. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  563. OPT_BOOLEAN('l', "print-line", &print_line,
  564. "print matching source lines (may be slow)"),
  565. OPT_BOOLEAN('P', "full-paths", &full_paths,
  566. "Don't shorten the displayed pathnames"),
  567. OPT_END()
  568. };
  569. static void setup_sorting(void)
  570. {
  571. char *tmp, *tok, *str = strdup(sort_order);
  572. for (tok = strtok_r(str, ", ", &tmp);
  573. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  574. if (sort_dimension__add(tok) < 0) {
  575. error("Unknown --sort key: `%s'", tok);
  576. usage_with_options(annotate_usage, options);
  577. }
  578. }
  579. free(str);
  580. }
  581. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  582. {
  583. symbol__init(sizeof(struct sym_priv));
  584. page_size = getpagesize();
  585. argc = parse_options(argc, argv, options, annotate_usage, 0);
  586. setup_sorting();
  587. if (argc) {
  588. /*
  589. * Special case: if there's an argument left then assume tha
  590. * it's a symbol filter:
  591. */
  592. if (argc > 1)
  593. usage_with_options(annotate_usage, options);
  594. sym_hist_filter = argv[0];
  595. }
  596. setup_pager();
  597. if (field_sep && *field_sep == '.') {
  598. fputs("'.' is the only non valid --field-separator argument\n",
  599. stderr);
  600. exit(129);
  601. }
  602. return __cmd_annotate();
  603. }