builtin-annotate.c 12 KB

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