builtin-annotate.c 14 KB

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