builtin-annotate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. ip = map->map_ip(map, ip);
  142. sym = map__find_function(map, ip, symbol_filter);
  143. } else {
  144. /*
  145. * If this is outside of all known maps,
  146. * and is a negative address, try to look it
  147. * up in the kernel dso, as it might be a
  148. * vsyscall or vdso (which executes in user-mode).
  149. *
  150. * XXX This is nasty, we should have a symbol list in
  151. * the "[vdso]" dso, but for now lets use the old
  152. * trick of looking in the whole kernel symbol list.
  153. */
  154. if ((long long)ip < 0)
  155. sym = kernel_maps__find_function(ip, &map,
  156. symbol_filter);
  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. remap:
  482. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  483. MAP_SHARED, input, offset);
  484. if (buf == MAP_FAILED) {
  485. perror("failed to mmap file");
  486. exit(-1);
  487. }
  488. more:
  489. event = (event_t *)(buf + head);
  490. size = event->header.size;
  491. if (!size)
  492. size = 8;
  493. if (head + event->header.size >= page_size * mmap_window) {
  494. unsigned long shift = page_size * (head / page_size);
  495. int munmap_ret;
  496. munmap_ret = munmap(buf, page_size * mmap_window);
  497. assert(munmap_ret == 0);
  498. offset += shift;
  499. head -= shift;
  500. goto remap;
  501. }
  502. size = event->header.size;
  503. dump_printf("%p [%p]: event: %d\n",
  504. (void *)(offset + head),
  505. (void *)(long)event->header.size,
  506. event->header.type);
  507. if (!size || process_event(event, offset, head) < 0) {
  508. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  509. (void *)(offset + head),
  510. (void *)(long)(event->header.size),
  511. event->header.type);
  512. total_unknown++;
  513. /*
  514. * assume we lost track of the stream, check alignment, and
  515. * increment a single u64 in the hope to catch on again 'soon'.
  516. */
  517. if (unlikely(head & 7))
  518. head &= ~7ULL;
  519. size = 8;
  520. }
  521. head += size;
  522. if (offset + head < (unsigned long)input_stat.st_size)
  523. goto more;
  524. rc = EXIT_SUCCESS;
  525. close(input);
  526. dump_printf(" IP events: %10ld\n", total);
  527. dump_printf(" mmap events: %10ld\n", total_mmap);
  528. dump_printf(" comm events: %10ld\n", total_comm);
  529. dump_printf(" fork events: %10ld\n", total_fork);
  530. dump_printf(" unknown events: %10ld\n", total_unknown);
  531. if (dump_trace)
  532. return 0;
  533. if (verbose > 3)
  534. threads__fprintf(stdout);
  535. if (verbose > 2)
  536. dsos__fprintf(stdout);
  537. collapse__resort();
  538. output__resort(total);
  539. find_annotations();
  540. return rc;
  541. }
  542. static const char * const annotate_usage[] = {
  543. "perf annotate [<options>] <command>",
  544. NULL
  545. };
  546. static const struct option options[] = {
  547. OPT_STRING('i', "input", &input_name, "file",
  548. "input file name"),
  549. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  550. "symbol to annotate"),
  551. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  552. OPT_BOOLEAN('v', "verbose", &verbose,
  553. "be more verbose (show symbol address, etc)"),
  554. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  555. "dump raw trace in ASCII"),
  556. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  557. "file", "vmlinux pathname"),
  558. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  559. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  560. OPT_BOOLEAN('l', "print-line", &print_line,
  561. "print matching source lines (may be slow)"),
  562. OPT_BOOLEAN('P', "full-paths", &full_paths,
  563. "Don't shorten the displayed pathnames"),
  564. OPT_END()
  565. };
  566. static void setup_sorting(void)
  567. {
  568. char *tmp, *tok, *str = strdup(sort_order);
  569. for (tok = strtok_r(str, ", ", &tmp);
  570. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  571. if (sort_dimension__add(tok) < 0) {
  572. error("Unknown --sort key: `%s'", tok);
  573. usage_with_options(annotate_usage, options);
  574. }
  575. }
  576. free(str);
  577. }
  578. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  579. {
  580. if (symbol__init(&symbol_conf) < 0)
  581. return -1;
  582. page_size = getpagesize();
  583. argc = parse_options(argc, argv, options, annotate_usage, 0);
  584. setup_sorting();
  585. if (argc) {
  586. /*
  587. * Special case: if there's an argument left then assume tha
  588. * it's a symbol filter:
  589. */
  590. if (argc > 1)
  591. usage_with_options(annotate_usage, options);
  592. sym_hist_filter = argv[0];
  593. }
  594. setup_pager();
  595. if (field_sep && *field_sep == '.') {
  596. fputs("'.' is the only non valid --field-separator argument\n",
  597. stderr);
  598. exit(129);
  599. }
  600. return __cmd_annotate();
  601. }