builtin-annotate.c 16 KB

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