builtin-annotate.c 14 KB

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