builtin-annotate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/util.h"
  11. #include "util/color.h"
  12. #include <linux/list.h>
  13. #include "util/cache.h"
  14. #include <linux/rbtree.h>
  15. #include "util/symbol.h"
  16. #include "perf.h"
  17. #include "util/debug.h"
  18. #include "util/annotate.h"
  19. #include "util/event.h"
  20. #include "util/parse-options.h"
  21. #include "util/parse-events.h"
  22. #include "util/thread.h"
  23. #include "util/sort.h"
  24. #include "util/hist.h"
  25. #include "util/session.h"
  26. static char const *input_name = "perf.data";
  27. static bool force, use_tui, use_stdio;
  28. static bool full_paths;
  29. static bool print_line;
  30. static const char *sym_hist_filter;
  31. static int hists__add_entry(struct hists *self, struct addr_location *al)
  32. {
  33. struct hist_entry *he;
  34. if (sym_hist_filter != NULL &&
  35. (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
  36. /* We're only interested in a symbol named sym_hist_filter */
  37. if (al->sym != NULL) {
  38. rb_erase(&al->sym->rb_node,
  39. &al->map->dso->symbols[al->map->type]);
  40. symbol__delete(al->sym);
  41. }
  42. return 0;
  43. }
  44. he = __hists__add_entry(self, al, NULL, 1);
  45. if (he == NULL)
  46. return -ENOMEM;
  47. if (he->ms.sym != NULL) {
  48. /*
  49. * All aggregated on the first sym_hist.
  50. */
  51. struct annotation *notes = symbol__annotation(he->ms.sym);
  52. if (notes->histograms == NULL && symbol__alloc_hist(he->ms.sym, 1) < 0)
  53. return -ENOMEM;
  54. return hist_entry__inc_addr_samples(he, 0, al->addr);
  55. }
  56. return 0;
  57. }
  58. static int process_sample_event(union perf_event *event,
  59. struct perf_sample *sample,
  60. struct perf_session *session)
  61. {
  62. struct addr_location al;
  63. if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) {
  64. pr_warning("problem processing %d event, skipping it.\n",
  65. event->header.type);
  66. return -1;
  67. }
  68. if (!al.filtered && hists__add_entry(&session->hists, &al)) {
  69. pr_warning("problem incrementing symbol count, "
  70. "skipping event\n");
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
  76. {
  77. return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
  78. print_line, full_paths, 0, 0);
  79. }
  80. static void hists__find_annotations(struct hists *self)
  81. {
  82. struct rb_node *nd = rb_first(&self->entries), *next;
  83. int key = KEY_RIGHT;
  84. while (nd) {
  85. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  86. struct annotation *notes;
  87. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  88. goto find_next;
  89. notes = symbol__annotation(he->ms.sym);
  90. if (notes->histograms == NULL) {
  91. find_next:
  92. if (key == KEY_LEFT)
  93. nd = rb_prev(nd);
  94. else
  95. nd = rb_next(nd);
  96. continue;
  97. }
  98. if (use_browser > 0) {
  99. /* For now all is aggregated on the first */
  100. key = hist_entry__tui_annotate(he, 0);
  101. switch (key) {
  102. case KEY_RIGHT:
  103. next = rb_next(nd);
  104. break;
  105. case KEY_LEFT:
  106. next = rb_prev(nd);
  107. break;
  108. default:
  109. return;
  110. }
  111. if (next != NULL)
  112. nd = next;
  113. } else {
  114. /* For now all is aggregated on the first */
  115. hist_entry__tty_annotate(he, 0);
  116. nd = rb_next(nd);
  117. /*
  118. * Since we have a hist_entry per IP for the same
  119. * symbol, free he->ms.sym->histogram to signal we already
  120. * processed this symbol.
  121. */
  122. free(notes->histograms);
  123. notes->histograms = NULL;
  124. }
  125. }
  126. }
  127. static struct perf_event_ops event_ops = {
  128. .sample = process_sample_event,
  129. .mmap = perf_event__process_mmap,
  130. .comm = perf_event__process_comm,
  131. .fork = perf_event__process_task,
  132. .ordered_samples = true,
  133. .ordering_requires_timestamps = true,
  134. };
  135. static int __cmd_annotate(void)
  136. {
  137. int ret;
  138. struct perf_session *session;
  139. session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
  140. if (session == NULL)
  141. return -ENOMEM;
  142. ret = perf_session__process_events(session, &event_ops);
  143. if (ret)
  144. goto out_delete;
  145. if (dump_trace) {
  146. perf_session__fprintf_nr_events(session, stdout);
  147. goto out_delete;
  148. }
  149. if (verbose > 3)
  150. perf_session__fprintf(session, stdout);
  151. if (verbose > 2)
  152. perf_session__fprintf_dsos(session, stdout);
  153. hists__collapse_resort(&session->hists);
  154. hists__output_resort(&session->hists);
  155. hists__find_annotations(&session->hists);
  156. out_delete:
  157. perf_session__delete(session);
  158. return ret;
  159. }
  160. static const char * const annotate_usage[] = {
  161. "perf annotate [<options>] <command>",
  162. NULL
  163. };
  164. static const struct option options[] = {
  165. OPT_STRING('i', "input", &input_name, "file",
  166. "input file name"),
  167. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  168. "only consider symbols in these dsos"),
  169. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  170. "symbol to annotate"),
  171. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  172. OPT_INCR('v', "verbose", &verbose,
  173. "be more verbose (show symbol address, etc)"),
  174. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  175. "dump raw trace in ASCII"),
  176. OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
  177. OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
  178. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  179. "file", "vmlinux pathname"),
  180. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  181. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  182. OPT_BOOLEAN('l', "print-line", &print_line,
  183. "print matching source lines (may be slow)"),
  184. OPT_BOOLEAN('P', "full-paths", &full_paths,
  185. "Don't shorten the displayed pathnames"),
  186. OPT_END()
  187. };
  188. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  189. {
  190. argc = parse_options(argc, argv, options, annotate_usage, 0);
  191. if (use_stdio)
  192. use_browser = 0;
  193. else if (use_tui)
  194. use_browser = 1;
  195. setup_browser(true);
  196. symbol_conf.priv_size = sizeof(struct annotation);
  197. symbol_conf.try_vmlinux_path = true;
  198. if (symbol__init() < 0)
  199. return -1;
  200. setup_sorting(annotate_usage, options);
  201. if (argc) {
  202. /*
  203. * Special case: if there's an argument left then assume tha
  204. * it's a symbol filter:
  205. */
  206. if (argc > 1)
  207. usage_with_options(annotate_usage, options);
  208. sym_hist_filter = argv[0];
  209. }
  210. if (field_sep && *field_sep == '.') {
  211. pr_err("'.' is the only non valid --field-separator argument\n");
  212. return -1;
  213. }
  214. return __cmd_annotate();
  215. }