builtin-annotate.c 14 KB

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