builtin-annotate.c 16 KB

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