builtin-annotate.c 6.2 KB

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