builtin-annotate.c 12 KB

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