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