builtin-annotate.c 16 KB

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