builtin-annotate.c 16 KB

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