builtin-annotate.c 16 KB

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