builtin-annotate.c 16 KB

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