builtin-annotate.c 12 KB

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