builtin-annotate.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/evlist.h"
  18. #include "util/evsel.h"
  19. #include "util/annotate.h"
  20. #include "util/event.h"
  21. #include "util/parse-options.h"
  22. #include "util/parse-events.h"
  23. #include "util/thread.h"
  24. #include "util/sort.h"
  25. #include "util/hist.h"
  26. #include "util/session.h"
  27. #include "util/tool.h"
  28. #include <linux/bitmap.h>
  29. struct perf_annotate {
  30. struct perf_tool tool;
  31. char const *input_name;
  32. bool force, use_tui, use_stdio;
  33. bool full_paths;
  34. bool print_line;
  35. const char *sym_hist_filter;
  36. const char *cpu_list;
  37. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  38. };
  39. static int perf_evsel__add_sample(struct perf_evsel *evsel,
  40. struct perf_sample *sample,
  41. struct addr_location *al,
  42. struct perf_annotate *ann)
  43. {
  44. struct hist_entry *he;
  45. int ret;
  46. if (ann->sym_hist_filter != NULL &&
  47. (al->sym == NULL ||
  48. strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
  49. /* We're only interested in a symbol named sym_hist_filter */
  50. if (al->sym != NULL) {
  51. rb_erase(&al->sym->rb_node,
  52. &al->map->dso->symbols[al->map->type]);
  53. symbol__delete(al->sym);
  54. }
  55. return 0;
  56. }
  57. he = __hists__add_entry(&evsel->hists, al, NULL, 1);
  58. if (he == NULL)
  59. return -ENOMEM;
  60. ret = 0;
  61. if (he->ms.sym != NULL) {
  62. struct annotation *notes = symbol__annotation(he->ms.sym);
  63. if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0)
  64. return -ENOMEM;
  65. ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  66. }
  67. evsel->hists.stats.total_period += sample->period;
  68. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  69. return ret;
  70. }
  71. static int process_sample_event(struct perf_tool *tool,
  72. union perf_event *event,
  73. struct perf_sample *sample,
  74. struct perf_evsel *evsel,
  75. struct machine *machine)
  76. {
  77. struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
  78. struct addr_location al;
  79. if (perf_event__preprocess_sample(event, machine, &al, sample,
  80. symbol__annotate_init) < 0) {
  81. pr_warning("problem processing %d event, skipping it.\n",
  82. event->header.type);
  83. return -1;
  84. }
  85. if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
  86. return 0;
  87. if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
  88. pr_warning("problem incrementing symbol count, "
  89. "skipping event\n");
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. static int hist_entry__tty_annotate(struct hist_entry *he, int evidx,
  95. struct perf_annotate *ann)
  96. {
  97. return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
  98. ann->print_line, ann->full_paths, 0, 0);
  99. }
  100. static void hists__find_annotations(struct hists *self, int evidx,
  101. struct perf_annotate *ann)
  102. {
  103. struct rb_node *nd = rb_first(&self->entries), *next;
  104. int key = K_RIGHT;
  105. while (nd) {
  106. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  107. struct annotation *notes;
  108. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  109. goto find_next;
  110. notes = symbol__annotation(he->ms.sym);
  111. if (notes->src == NULL) {
  112. find_next:
  113. if (key == K_LEFT)
  114. nd = rb_prev(nd);
  115. else
  116. nd = rb_next(nd);
  117. continue;
  118. }
  119. if (use_browser > 0) {
  120. key = hist_entry__tui_annotate(he, evidx, NULL, NULL, 0);
  121. switch (key) {
  122. case K_RIGHT:
  123. next = rb_next(nd);
  124. break;
  125. case K_LEFT:
  126. next = rb_prev(nd);
  127. break;
  128. default:
  129. return;
  130. }
  131. if (next != NULL)
  132. nd = next;
  133. } else {
  134. hist_entry__tty_annotate(he, evidx, ann);
  135. nd = rb_next(nd);
  136. /*
  137. * Since we have a hist_entry per IP for the same
  138. * symbol, free he->ms.sym->src to signal we already
  139. * processed this symbol.
  140. */
  141. free(notes->src);
  142. notes->src = NULL;
  143. }
  144. }
  145. }
  146. static int __cmd_annotate(struct perf_annotate *ann)
  147. {
  148. int ret;
  149. struct perf_session *session;
  150. struct perf_evsel *pos;
  151. u64 total_nr_samples;
  152. session = perf_session__new(ann->input_name, O_RDONLY,
  153. ann->force, false, &ann->tool);
  154. if (session == NULL)
  155. return -ENOMEM;
  156. if (ann->cpu_list) {
  157. ret = perf_session__cpu_bitmap(session, ann->cpu_list,
  158. ann->cpu_bitmap);
  159. if (ret)
  160. goto out_delete;
  161. }
  162. ret = perf_session__process_events(session, &ann->tool);
  163. if (ret)
  164. goto out_delete;
  165. if (dump_trace) {
  166. perf_session__fprintf_nr_events(session, stdout);
  167. goto out_delete;
  168. }
  169. if (verbose > 3)
  170. perf_session__fprintf(session, stdout);
  171. if (verbose > 2)
  172. perf_session__fprintf_dsos(session, stdout);
  173. total_nr_samples = 0;
  174. list_for_each_entry(pos, &session->evlist->entries, node) {
  175. struct hists *hists = &pos->hists;
  176. u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  177. if (nr_samples > 0) {
  178. total_nr_samples += nr_samples;
  179. hists__collapse_resort(hists);
  180. hists__output_resort(hists);
  181. hists__find_annotations(hists, pos->idx, ann);
  182. }
  183. }
  184. if (total_nr_samples == 0) {
  185. ui__warning("The %s file has no samples!\n", session->filename);
  186. goto out_delete;
  187. }
  188. out_delete:
  189. /*
  190. * Speed up the exit process, for large files this can
  191. * take quite a while.
  192. *
  193. * XXX Enable this when using valgrind or if we ever
  194. * librarize this command.
  195. *
  196. * Also experiment with obstacks to see how much speed
  197. * up we'll get here.
  198. *
  199. * perf_session__delete(session);
  200. */
  201. return ret;
  202. }
  203. static const char * const annotate_usage[] = {
  204. "perf annotate [<options>]",
  205. NULL
  206. };
  207. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  208. {
  209. struct perf_annotate annotate = {
  210. .tool = {
  211. .sample = process_sample_event,
  212. .mmap = perf_event__process_mmap,
  213. .comm = perf_event__process_comm,
  214. .fork = perf_event__process_task,
  215. .ordered_samples = true,
  216. .ordering_requires_timestamps = true,
  217. },
  218. };
  219. const struct option options[] = {
  220. OPT_STRING('i', "input", &annotate.input_name, "file",
  221. "input file name"),
  222. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  223. "only consider symbols in these dsos"),
  224. OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
  225. "symbol to annotate"),
  226. OPT_BOOLEAN('f', "force", &annotate.force, "don't complain, do it"),
  227. OPT_INCR('v', "verbose", &verbose,
  228. "be more verbose (show symbol address, etc)"),
  229. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  230. "dump raw trace in ASCII"),
  231. OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
  232. OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
  233. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  234. "file", "vmlinux pathname"),
  235. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  236. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  237. OPT_BOOLEAN('l', "print-line", &annotate.print_line,
  238. "print matching source lines (may be slow)"),
  239. OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
  240. "Don't shorten the displayed pathnames"),
  241. OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
  242. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  243. "Look for files with symbols relative to this directory"),
  244. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  245. "Interleave source code with assembly code (default)"),
  246. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  247. "Display raw encoding of assembly instructions (default)"),
  248. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  249. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  250. OPT_END()
  251. };
  252. argc = parse_options(argc, argv, options, annotate_usage, 0);
  253. if (annotate.use_stdio)
  254. use_browser = 0;
  255. else if (annotate.use_tui)
  256. use_browser = 1;
  257. setup_browser(true);
  258. symbol_conf.priv_size = sizeof(struct annotation);
  259. symbol_conf.try_vmlinux_path = true;
  260. if (symbol__init() < 0)
  261. return -1;
  262. setup_sorting(annotate_usage, options);
  263. if (argc) {
  264. /*
  265. * Special case: if there's an argument left then assume tha
  266. * it's a symbol filter:
  267. */
  268. if (argc > 1)
  269. usage_with_options(annotate_usage, options);
  270. annotate.sym_hist_filter = argv[0];
  271. }
  272. return __cmd_annotate(&annotate);
  273. }