builtin-annotate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include "util/session.h"
  25. static char const *input_name = "perf.data";
  26. static int force;
  27. static int full_paths;
  28. static int print_line;
  29. struct sym_hist {
  30. u64 sum;
  31. u64 ip[0];
  32. };
  33. struct sym_ext {
  34. struct rb_node node;
  35. double percent;
  36. char *path;
  37. };
  38. struct sym_priv {
  39. struct sym_hist *hist;
  40. struct sym_ext *ext;
  41. };
  42. static const char *sym_hist_filter;
  43. static int symbol_filter(struct map *map __used, struct symbol *sym)
  44. {
  45. if (sym_hist_filter == NULL ||
  46. strcmp(sym->name, sym_hist_filter) == 0) {
  47. struct sym_priv *priv = symbol__priv(sym);
  48. const int size = (sizeof(*priv->hist) +
  49. (sym->end - sym->start) * sizeof(u64));
  50. priv->hist = malloc(size);
  51. if (priv->hist)
  52. memset(priv->hist, 0, size);
  53. return 0;
  54. }
  55. /*
  56. * FIXME: We should really filter it out, as we don't want to go thru symbols
  57. * we're not interested, and if a DSO ends up with no symbols, delete it too,
  58. * but right now the kernel loading routines in symbol.c bail out if no symbols
  59. * are found, fix it later.
  60. */
  61. return 0;
  62. }
  63. /*
  64. * collect histogram counts
  65. */
  66. static void hist_hit(struct hist_entry *he, u64 ip)
  67. {
  68. unsigned int sym_size, offset;
  69. struct symbol *sym = he->sym;
  70. struct sym_priv *priv;
  71. struct sym_hist *h;
  72. he->count++;
  73. if (!sym || !he->map)
  74. return;
  75. priv = symbol__priv(sym);
  76. if (!priv->hist)
  77. return;
  78. sym_size = sym->end - sym->start;
  79. offset = ip - sym->start;
  80. if (verbose)
  81. fprintf(stderr, "%s: ip=%Lx\n", __func__,
  82. he->map->unmap_ip(he->map, ip));
  83. if (offset >= sym_size)
  84. return;
  85. h = priv->hist;
  86. h->sum++;
  87. h->ip[offset]++;
  88. if (verbose >= 3)
  89. printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
  90. (void *)(unsigned long)he->sym->start,
  91. he->sym->name,
  92. (void *)(unsigned long)ip, ip - he->sym->start,
  93. h->ip[offset]);
  94. }
  95. static int perf_session__add_hist_entry(struct perf_session *self,
  96. struct addr_location *al, u64 count)
  97. {
  98. bool hit;
  99. struct hist_entry *he = __perf_session__add_hist_entry(self, al, NULL,
  100. count, &hit);
  101. if (he == NULL)
  102. return -ENOMEM;
  103. hist_hit(he, al->addr);
  104. return 0;
  105. }
  106. static int process_sample_event(event_t *event, struct perf_session *session)
  107. {
  108. struct addr_location al;
  109. dump_printf("(IP, %d): %d: %p\n", event->header.misc,
  110. event->ip.pid, (void *)(long)event->ip.ip);
  111. if (event__preprocess_sample(event, session, &al, symbol_filter) < 0) {
  112. fprintf(stderr, "problem processing %d event, skipping it.\n",
  113. event->header.type);
  114. return -1;
  115. }
  116. if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
  117. fprintf(stderr, "problem incrementing symbol count, "
  118. "skipping event\n");
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. static int parse_line(FILE *file, struct hist_entry *he, u64 len)
  124. {
  125. struct symbol *sym = he->sym;
  126. char *line = NULL, *tmp, *tmp2;
  127. static const char *prev_line;
  128. static const char *prev_color;
  129. unsigned int offset;
  130. size_t line_len;
  131. u64 start;
  132. s64 line_ip;
  133. int ret;
  134. char *c;
  135. if (getline(&line, &line_len, file) < 0)
  136. return -1;
  137. if (!line)
  138. return -1;
  139. c = strchr(line, '\n');
  140. if (c)
  141. *c = 0;
  142. line_ip = -1;
  143. offset = 0;
  144. ret = -2;
  145. /*
  146. * Strip leading spaces:
  147. */
  148. tmp = line;
  149. while (*tmp) {
  150. if (*tmp != ' ')
  151. break;
  152. tmp++;
  153. }
  154. if (*tmp) {
  155. /*
  156. * Parse hexa addresses followed by ':'
  157. */
  158. line_ip = strtoull(tmp, &tmp2, 16);
  159. if (*tmp2 != ':')
  160. line_ip = -1;
  161. }
  162. start = he->map->unmap_ip(he->map, sym->start);
  163. if (line_ip != -1) {
  164. const char *path = NULL;
  165. unsigned int hits = 0;
  166. double percent = 0.0;
  167. const char *color;
  168. struct sym_priv *priv = symbol__priv(sym);
  169. struct sym_ext *sym_ext = priv->ext;
  170. struct sym_hist *h = priv->hist;
  171. offset = line_ip - start;
  172. if (offset < len)
  173. hits = h->ip[offset];
  174. if (offset < len && sym_ext) {
  175. path = sym_ext[offset].path;
  176. percent = sym_ext[offset].percent;
  177. } else if (h->sum)
  178. percent = 100.0 * hits / h->sum;
  179. color = get_percent_color(percent);
  180. /*
  181. * Also color the filename and line if needed, with
  182. * the same color than the percentage. Don't print it
  183. * twice for close colored ip with the same filename:line
  184. */
  185. if (path) {
  186. if (!prev_line || strcmp(prev_line, path)
  187. || color != prev_color) {
  188. color_fprintf(stdout, color, " %s", path);
  189. prev_line = path;
  190. prev_color = color;
  191. }
  192. }
  193. color_fprintf(stdout, color, " %7.2f", percent);
  194. printf(" : ");
  195. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  196. } else {
  197. if (!*line)
  198. printf(" :\n");
  199. else
  200. printf(" : %s\n", line);
  201. }
  202. return 0;
  203. }
  204. static struct rb_root root_sym_ext;
  205. static void insert_source_line(struct sym_ext *sym_ext)
  206. {
  207. struct sym_ext *iter;
  208. struct rb_node **p = &root_sym_ext.rb_node;
  209. struct rb_node *parent = NULL;
  210. while (*p != NULL) {
  211. parent = *p;
  212. iter = rb_entry(parent, struct sym_ext, node);
  213. if (sym_ext->percent > iter->percent)
  214. p = &(*p)->rb_left;
  215. else
  216. p = &(*p)->rb_right;
  217. }
  218. rb_link_node(&sym_ext->node, parent, p);
  219. rb_insert_color(&sym_ext->node, &root_sym_ext);
  220. }
  221. static void free_source_line(struct hist_entry *he, int len)
  222. {
  223. struct sym_priv *priv = symbol__priv(he->sym);
  224. struct sym_ext *sym_ext = priv->ext;
  225. int i;
  226. if (!sym_ext)
  227. return;
  228. for (i = 0; i < len; i++)
  229. free(sym_ext[i].path);
  230. free(sym_ext);
  231. priv->ext = NULL;
  232. root_sym_ext = RB_ROOT;
  233. }
  234. /* Get the filename:line for the colored entries */
  235. static void
  236. get_source_line(struct hist_entry *he, int len, const char *filename)
  237. {
  238. struct symbol *sym = he->sym;
  239. u64 start;
  240. int i;
  241. char cmd[PATH_MAX * 2];
  242. struct sym_ext *sym_ext;
  243. struct sym_priv *priv = symbol__priv(sym);
  244. struct sym_hist *h = priv->hist;
  245. if (!h->sum)
  246. return;
  247. sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
  248. if (!priv->ext)
  249. return;
  250. start = he->map->unmap_ip(he->map, sym->start);
  251. for (i = 0; i < len; i++) {
  252. char *path = NULL;
  253. size_t line_len;
  254. u64 offset;
  255. FILE *fp;
  256. sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
  257. if (sym_ext[i].percent <= 0.5)
  258. continue;
  259. offset = start + i;
  260. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  261. fp = popen(cmd, "r");
  262. if (!fp)
  263. continue;
  264. if (getline(&path, &line_len, fp) < 0 || !line_len)
  265. goto next;
  266. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  267. if (!sym_ext[i].path)
  268. goto next;
  269. strcpy(sym_ext[i].path, path);
  270. insert_source_line(&sym_ext[i]);
  271. next:
  272. pclose(fp);
  273. }
  274. }
  275. static void print_summary(const char *filename)
  276. {
  277. struct sym_ext *sym_ext;
  278. struct rb_node *node;
  279. printf("\nSorted summary for file %s\n", filename);
  280. printf("----------------------------------------------\n\n");
  281. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  282. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  283. return;
  284. }
  285. node = rb_first(&root_sym_ext);
  286. while (node) {
  287. double percent;
  288. const char *color;
  289. char *path;
  290. sym_ext = rb_entry(node, struct sym_ext, node);
  291. percent = sym_ext->percent;
  292. color = get_percent_color(percent);
  293. path = sym_ext->path;
  294. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  295. node = rb_next(node);
  296. }
  297. }
  298. static void annotate_sym(struct hist_entry *he)
  299. {
  300. struct map *map = he->map;
  301. struct dso *dso = map->dso;
  302. struct symbol *sym = he->sym;
  303. const char *filename = dso->long_name, *d_filename;
  304. u64 len;
  305. char command[PATH_MAX*2];
  306. FILE *file;
  307. if (!filename)
  308. return;
  309. if (verbose)
  310. fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
  311. __func__, filename, sym->name,
  312. map->unmap_ip(map, sym->start),
  313. map->unmap_ip(map, sym->end));
  314. if (full_paths)
  315. d_filename = filename;
  316. else
  317. d_filename = basename(filename);
  318. len = sym->end - sym->start;
  319. if (print_line) {
  320. get_source_line(he, len, filename);
  321. print_summary(filename);
  322. }
  323. printf("\n\n------------------------------------------------\n");
  324. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  325. printf("------------------------------------------------\n");
  326. if (verbose >= 2)
  327. printf("annotating [%p] %30s : [%p] %30s\n",
  328. dso, dso->long_name, sym, sym->name);
  329. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
  330. map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
  331. filename, filename);
  332. if (verbose >= 3)
  333. printf("doing: %s\n", command);
  334. file = popen(command, "r");
  335. if (!file)
  336. return;
  337. while (!feof(file)) {
  338. if (parse_line(file, he, len) < 0)
  339. break;
  340. }
  341. pclose(file);
  342. if (print_line)
  343. free_source_line(he, len);
  344. }
  345. static void perf_session__find_annotations(struct perf_session *self)
  346. {
  347. struct rb_node *nd;
  348. for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
  349. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  350. struct sym_priv *priv;
  351. if (he->sym == NULL)
  352. continue;
  353. priv = symbol__priv(he->sym);
  354. if (priv->hist == NULL)
  355. continue;
  356. annotate_sym(he);
  357. /*
  358. * Since we have a hist_entry per IP for the same symbol, free
  359. * he->sym->hist to signal we already processed this symbol.
  360. */
  361. free(priv->hist);
  362. priv->hist = NULL;
  363. }
  364. }
  365. static struct perf_event_ops event_ops = {
  366. .process_sample_event = process_sample_event,
  367. .process_mmap_event = event__process_mmap,
  368. .process_comm_event = event__process_comm,
  369. .process_fork_event = event__process_task,
  370. };
  371. static int __cmd_annotate(void)
  372. {
  373. int ret;
  374. struct perf_session *session;
  375. session = perf_session__new(input_name, O_RDONLY, force);
  376. if (session == NULL)
  377. return -ENOMEM;
  378. ret = perf_session__process_events(session, &event_ops);
  379. if (ret)
  380. goto out_delete;
  381. if (dump_trace) {
  382. event__print_totals();
  383. goto out_delete;
  384. }
  385. if (verbose > 3)
  386. perf_session__fprintf(session, stdout);
  387. if (verbose > 2)
  388. dsos__fprintf(stdout);
  389. perf_session__collapse_resort(session);
  390. perf_session__output_resort(session, session->event_total[0]);
  391. perf_session__find_annotations(session);
  392. out_delete:
  393. perf_session__delete(session);
  394. return ret;
  395. }
  396. static const char * const annotate_usage[] = {
  397. "perf annotate [<options>] <command>",
  398. NULL
  399. };
  400. static const struct option options[] = {
  401. OPT_STRING('i', "input", &input_name, "file",
  402. "input file name"),
  403. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  404. "symbol to annotate"),
  405. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  406. OPT_BOOLEAN('v', "verbose", &verbose,
  407. "be more verbose (show symbol address, etc)"),
  408. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  409. "dump raw trace in ASCII"),
  410. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  411. "file", "vmlinux pathname"),
  412. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  413. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  414. OPT_BOOLEAN('l', "print-line", &print_line,
  415. "print matching source lines (may be slow)"),
  416. OPT_BOOLEAN('P', "full-paths", &full_paths,
  417. "Don't shorten the displayed pathnames"),
  418. OPT_END()
  419. };
  420. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  421. {
  422. argc = parse_options(argc, argv, options, annotate_usage, 0);
  423. symbol_conf.priv_size = sizeof(struct sym_priv);
  424. symbol_conf.try_vmlinux_path = true;
  425. if (symbol__init() < 0)
  426. return -1;
  427. setup_sorting(annotate_usage, options);
  428. if (argc) {
  429. /*
  430. * Special case: if there's an argument left then assume tha
  431. * it's a symbol filter:
  432. */
  433. if (argc > 1)
  434. usage_with_options(annotate_usage, options);
  435. sym_hist_filter = argv[0];
  436. }
  437. setup_pager();
  438. if (field_sep && *field_sep == '.') {
  439. fputs("'.' is the only non valid --field-separator argument\n",
  440. stderr);
  441. exit(129);
  442. }
  443. return __cmd_annotate();
  444. }