builtin-annotate.c 12 KB

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