builtin-annotate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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/event.h"
  19. #include "util/parse-options.h"
  20. #include "util/parse-events.h"
  21. #include "util/thread.h"
  22. #include "util/sort.h"
  23. #include "util/hist.h"
  24. static char const *input_name = "perf.data";
  25. static int force;
  26. static int input;
  27. static int full_paths;
  28. static int print_line;
  29. static unsigned long page_size;
  30. static unsigned long mmap_window = 32;
  31. struct sym_hist {
  32. u64 sum;
  33. u64 ip[0];
  34. };
  35. struct sym_ext {
  36. struct rb_node node;
  37. double percent;
  38. char *path;
  39. };
  40. struct sym_priv {
  41. struct sym_hist *hist;
  42. struct sym_ext *ext;
  43. };
  44. static struct symbol_conf symbol_conf = {
  45. .priv_size = sizeof(struct sym_priv),
  46. .try_vmlinux_path = true,
  47. };
  48. static const char *sym_hist_filter;
  49. static int symbol_filter(struct map *map __used, struct symbol *sym)
  50. {
  51. if (sym_hist_filter == NULL ||
  52. strcmp(sym->name, sym_hist_filter) == 0) {
  53. struct sym_priv *priv = symbol__priv(sym);
  54. const int size = (sizeof(*priv->hist) +
  55. (sym->end - sym->start) * sizeof(u64));
  56. priv->hist = malloc(size);
  57. if (priv->hist)
  58. memset(priv->hist, 0, size);
  59. return 0;
  60. }
  61. /*
  62. * FIXME: We should really filter it out, as we don't want to go thru symbols
  63. * we're not interested, and if a DSO ends up with no symbols, delete it too,
  64. * but right now the kernel loading routines in symbol.c bail out if no symbols
  65. * are found, fix it later.
  66. */
  67. return 0;
  68. }
  69. /*
  70. * collect histogram counts
  71. */
  72. static void hist_hit(struct hist_entry *he, u64 ip)
  73. {
  74. unsigned int sym_size, offset;
  75. struct symbol *sym = he->sym;
  76. struct sym_priv *priv;
  77. struct sym_hist *h;
  78. he->count++;
  79. if (!sym || !he->map)
  80. return;
  81. priv = symbol__priv(sym);
  82. if (!priv->hist)
  83. return;
  84. sym_size = sym->end - sym->start;
  85. offset = ip - sym->start;
  86. if (verbose)
  87. fprintf(stderr, "%s: ip=%Lx\n", __func__,
  88. he->map->unmap_ip(he->map, ip));
  89. if (offset >= sym_size)
  90. return;
  91. h = priv->hist;
  92. h->sum++;
  93. h->ip[offset]++;
  94. if (verbose >= 3)
  95. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  96. (void *)(unsigned long)he->sym->start,
  97. he->sym->name,
  98. (void *)(unsigned long)ip, ip - he->sym->start,
  99. h->ip[offset]);
  100. }
  101. static int hist_entry__add(struct addr_location *al, u64 count)
  102. {
  103. bool hit;
  104. struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit);
  105. if (he == NULL)
  106. return -ENOMEM;
  107. hist_hit(he, al->addr);
  108. return 0;
  109. }
  110. static int process_sample_event(event_t *event)
  111. {
  112. struct addr_location al;
  113. dump_printf("(IP, %d): %d: %p\n", event->header.misc,
  114. event->ip.pid, (void *)(long)event->ip.ip);
  115. if (event__preprocess_sample(event, &al, symbol_filter) < 0) {
  116. fprintf(stderr, "problem processing %d event, skipping it.\n",
  117. event->header.type);
  118. return -1;
  119. }
  120. if (hist_entry__add(&al, 1)) {
  121. fprintf(stderr, "problem incrementing symbol count, "
  122. "skipping event\n");
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. static int event__process(event_t *self)
  128. {
  129. switch (self->header.type) {
  130. case PERF_RECORD_SAMPLE:
  131. return process_sample_event(self);
  132. case PERF_RECORD_MMAP:
  133. return event__process_mmap(self);
  134. case PERF_RECORD_COMM:
  135. return event__process_comm(self);
  136. case PERF_RECORD_FORK:
  137. return event__process_task(self);
  138. /*
  139. * We dont process them right now but they are fine:
  140. */
  141. case PERF_RECORD_THROTTLE:
  142. case PERF_RECORD_UNTHROTTLE:
  143. return 0;
  144. default:
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. static int parse_line(FILE *file, struct hist_entry *he, u64 len)
  150. {
  151. struct symbol *sym = he->sym;
  152. char *line = NULL, *tmp, *tmp2;
  153. static const char *prev_line;
  154. static const char *prev_color;
  155. unsigned int offset;
  156. size_t line_len;
  157. u64 start;
  158. s64 line_ip;
  159. int ret;
  160. char *c;
  161. if (getline(&line, &line_len, file) < 0)
  162. return -1;
  163. if (!line)
  164. return -1;
  165. c = strchr(line, '\n');
  166. if (c)
  167. *c = 0;
  168. line_ip = -1;
  169. offset = 0;
  170. ret = -2;
  171. /*
  172. * Strip leading spaces:
  173. */
  174. tmp = line;
  175. while (*tmp) {
  176. if (*tmp != ' ')
  177. break;
  178. tmp++;
  179. }
  180. if (*tmp) {
  181. /*
  182. * Parse hexa addresses followed by ':'
  183. */
  184. line_ip = strtoull(tmp, &tmp2, 16);
  185. if (*tmp2 != ':')
  186. line_ip = -1;
  187. }
  188. start = he->map->unmap_ip(he->map, sym->start);
  189. if (line_ip != -1) {
  190. const char *path = NULL;
  191. unsigned int hits = 0;
  192. double percent = 0.0;
  193. const char *color;
  194. struct sym_priv *priv = symbol__priv(sym);
  195. struct sym_ext *sym_ext = priv->ext;
  196. struct sym_hist *h = priv->hist;
  197. offset = line_ip - start;
  198. if (offset < len)
  199. hits = h->ip[offset];
  200. if (offset < len && sym_ext) {
  201. path = sym_ext[offset].path;
  202. percent = sym_ext[offset].percent;
  203. } else if (h->sum)
  204. percent = 100.0 * hits / h->sum;
  205. color = get_percent_color(percent);
  206. /*
  207. * Also color the filename and line if needed, with
  208. * the same color than the percentage. Don't print it
  209. * twice for close colored ip with the same filename:line
  210. */
  211. if (path) {
  212. if (!prev_line || strcmp(prev_line, path)
  213. || color != prev_color) {
  214. color_fprintf(stdout, color, " %s", path);
  215. prev_line = path;
  216. prev_color = color;
  217. }
  218. }
  219. color_fprintf(stdout, color, " %7.2f", percent);
  220. printf(" : ");
  221. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  222. } else {
  223. if (!*line)
  224. printf(" :\n");
  225. else
  226. printf(" : %s\n", line);
  227. }
  228. return 0;
  229. }
  230. static struct rb_root root_sym_ext;
  231. static void insert_source_line(struct sym_ext *sym_ext)
  232. {
  233. struct sym_ext *iter;
  234. struct rb_node **p = &root_sym_ext.rb_node;
  235. struct rb_node *parent = NULL;
  236. while (*p != NULL) {
  237. parent = *p;
  238. iter = rb_entry(parent, struct sym_ext, node);
  239. if (sym_ext->percent > iter->percent)
  240. p = &(*p)->rb_left;
  241. else
  242. p = &(*p)->rb_right;
  243. }
  244. rb_link_node(&sym_ext->node, parent, p);
  245. rb_insert_color(&sym_ext->node, &root_sym_ext);
  246. }
  247. static void free_source_line(struct hist_entry *he, int len)
  248. {
  249. struct sym_priv *priv = symbol__priv(he->sym);
  250. struct sym_ext *sym_ext = priv->ext;
  251. int i;
  252. if (!sym_ext)
  253. return;
  254. for (i = 0; i < len; i++)
  255. free(sym_ext[i].path);
  256. free(sym_ext);
  257. priv->ext = NULL;
  258. root_sym_ext = RB_ROOT;
  259. }
  260. /* Get the filename:line for the colored entries */
  261. static void
  262. get_source_line(struct hist_entry *he, int len, const char *filename)
  263. {
  264. struct symbol *sym = he->sym;
  265. u64 start;
  266. int i;
  267. char cmd[PATH_MAX * 2];
  268. struct sym_ext *sym_ext;
  269. struct sym_priv *priv = symbol__priv(sym);
  270. struct sym_hist *h = priv->hist;
  271. if (!h->sum)
  272. return;
  273. sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
  274. if (!priv->ext)
  275. return;
  276. start = he->map->unmap_ip(he->map, sym->start);
  277. for (i = 0; i < len; i++) {
  278. char *path = NULL;
  279. size_t line_len;
  280. u64 offset;
  281. FILE *fp;
  282. sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
  283. if (sym_ext[i].percent <= 0.5)
  284. continue;
  285. offset = start + i;
  286. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  287. fp = popen(cmd, "r");
  288. if (!fp)
  289. continue;
  290. if (getline(&path, &line_len, fp) < 0 || !line_len)
  291. goto next;
  292. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  293. if (!sym_ext[i].path)
  294. goto next;
  295. strcpy(sym_ext[i].path, path);
  296. insert_source_line(&sym_ext[i]);
  297. next:
  298. pclose(fp);
  299. }
  300. }
  301. static void print_summary(const char *filename)
  302. {
  303. struct sym_ext *sym_ext;
  304. struct rb_node *node;
  305. printf("\nSorted summary for file %s\n", filename);
  306. printf("----------------------------------------------\n\n");
  307. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  308. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  309. return;
  310. }
  311. node = rb_first(&root_sym_ext);
  312. while (node) {
  313. double percent;
  314. const char *color;
  315. char *path;
  316. sym_ext = rb_entry(node, struct sym_ext, node);
  317. percent = sym_ext->percent;
  318. color = get_percent_color(percent);
  319. path = sym_ext->path;
  320. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  321. node = rb_next(node);
  322. }
  323. }
  324. static void annotate_sym(struct hist_entry *he)
  325. {
  326. struct map *map = he->map;
  327. struct dso *dso = map->dso;
  328. struct symbol *sym = he->sym;
  329. const char *filename = dso->long_name, *d_filename;
  330. u64 len;
  331. char command[PATH_MAX*2];
  332. FILE *file;
  333. if (!filename)
  334. return;
  335. if (verbose)
  336. fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
  337. __func__, filename, sym->name,
  338. map->unmap_ip(map, sym->start),
  339. map->unmap_ip(map, sym->end));
  340. if (full_paths)
  341. d_filename = filename;
  342. else
  343. d_filename = basename(filename);
  344. len = sym->end - sym->start;
  345. if (print_line) {
  346. get_source_line(he, len, filename);
  347. print_summary(filename);
  348. }
  349. printf("\n\n------------------------------------------------\n");
  350. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  351. printf("------------------------------------------------\n");
  352. if (verbose >= 2)
  353. printf("annotating [%p] %30s : [%p] %30s\n",
  354. dso, dso->long_name, sym, sym->name);
  355. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  356. map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
  357. filename, filename);
  358. if (verbose >= 3)
  359. printf("doing: %s\n", command);
  360. file = popen(command, "r");
  361. if (!file)
  362. return;
  363. while (!feof(file)) {
  364. if (parse_line(file, he, len) < 0)
  365. break;
  366. }
  367. pclose(file);
  368. if (print_line)
  369. free_source_line(he, len);
  370. }
  371. static void find_annotations(void)
  372. {
  373. struct rb_node *nd;
  374. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  375. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  376. struct sym_priv *priv;
  377. if (he->sym == NULL)
  378. continue;
  379. priv = symbol__priv(he->sym);
  380. if (priv->hist == NULL)
  381. continue;
  382. annotate_sym(he);
  383. /*
  384. * Since we have a hist_entry per IP for the same symbol, free
  385. * he->sym->hist to signal we already processed this symbol.
  386. */
  387. free(priv->hist);
  388. priv->hist = NULL;
  389. }
  390. }
  391. static int __cmd_annotate(void)
  392. {
  393. int ret, rc = EXIT_FAILURE;
  394. unsigned long offset = 0;
  395. unsigned long head = 0;
  396. struct stat input_stat;
  397. event_t *event;
  398. uint32_t size;
  399. char *buf;
  400. register_idle_thread();
  401. input = open(input_name, O_RDONLY);
  402. if (input < 0) {
  403. perror("failed to open file");
  404. exit(-1);
  405. }
  406. ret = fstat(input, &input_stat);
  407. if (ret < 0) {
  408. perror("failed to stat file");
  409. exit(-1);
  410. }
  411. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  412. fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
  413. exit(-1);
  414. }
  415. if (!input_stat.st_size) {
  416. fprintf(stderr, "zero-sized file, nothing to do!\n");
  417. exit(0);
  418. }
  419. remap:
  420. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  421. MAP_SHARED, input, offset);
  422. if (buf == MAP_FAILED) {
  423. perror("failed to mmap file");
  424. exit(-1);
  425. }
  426. more:
  427. event = (event_t *)(buf + head);
  428. size = event->header.size;
  429. if (!size)
  430. size = 8;
  431. if (head + event->header.size >= page_size * mmap_window) {
  432. unsigned long shift = page_size * (head / page_size);
  433. int munmap_ret;
  434. munmap_ret = munmap(buf, page_size * mmap_window);
  435. assert(munmap_ret == 0);
  436. offset += shift;
  437. head -= shift;
  438. goto remap;
  439. }
  440. size = event->header.size;
  441. dump_printf("%p [%p]: event: %d\n",
  442. (void *)(offset + head),
  443. (void *)(long)event->header.size,
  444. event->header.type);
  445. if (!size || event__process(event) < 0) {
  446. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  447. (void *)(offset + head),
  448. (void *)(long)(event->header.size),
  449. event->header.type);
  450. /*
  451. * assume we lost track of the stream, check alignment, and
  452. * increment a single u64 in the hope to catch on again 'soon'.
  453. */
  454. if (unlikely(head & 7))
  455. head &= ~7ULL;
  456. size = 8;
  457. }
  458. head += size;
  459. if (offset + head < (unsigned long)input_stat.st_size)
  460. goto more;
  461. rc = EXIT_SUCCESS;
  462. close(input);
  463. if (dump_trace) {
  464. event__print_totals();
  465. return 0;
  466. }
  467. if (verbose > 3)
  468. threads__fprintf(stdout);
  469. if (verbose > 2)
  470. dsos__fprintf(stdout);
  471. collapse__resort();
  472. output__resort(event__total[0]);
  473. find_annotations();
  474. return rc;
  475. }
  476. static const char * const annotate_usage[] = {
  477. "perf annotate [<options>] <command>",
  478. NULL
  479. };
  480. static const struct option options[] = {
  481. OPT_STRING('i', "input", &input_name, "file",
  482. "input file name"),
  483. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  484. "symbol to annotate"),
  485. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  486. OPT_BOOLEAN('v', "verbose", &verbose,
  487. "be more verbose (show symbol address, etc)"),
  488. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  489. "dump raw trace in ASCII"),
  490. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  491. "file", "vmlinux pathname"),
  492. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  493. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  494. OPT_BOOLEAN('l', "print-line", &print_line,
  495. "print matching source lines (may be slow)"),
  496. OPT_BOOLEAN('P', "full-paths", &full_paths,
  497. "Don't shorten the displayed pathnames"),
  498. OPT_END()
  499. };
  500. static void setup_sorting(void)
  501. {
  502. char *tmp, *tok, *str = strdup(sort_order);
  503. for (tok = strtok_r(str, ", ", &tmp);
  504. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  505. if (sort_dimension__add(tok) < 0) {
  506. error("Unknown --sort key: `%s'", tok);
  507. usage_with_options(annotate_usage, options);
  508. }
  509. }
  510. free(str);
  511. }
  512. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  513. {
  514. if (symbol__init(&symbol_conf) < 0)
  515. return -1;
  516. page_size = getpagesize();
  517. argc = parse_options(argc, argv, options, annotate_usage, 0);
  518. setup_sorting();
  519. if (argc) {
  520. /*
  521. * Special case: if there's an argument left then assume tha
  522. * it's a symbol filter:
  523. */
  524. if (argc > 1)
  525. usage_with_options(annotate_usage, options);
  526. sym_hist_filter = argv[0];
  527. }
  528. setup_pager();
  529. if (field_sep && *field_sep == '.') {
  530. fputs("'.' is the only non valid --field-separator argument\n",
  531. stderr);
  532. exit(129);
  533. }
  534. return __cmd_annotate();
  535. }