builtin-annotate.c 12 KB

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