Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 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 | |
| 10 | #include "util/util.h" |
| 11 | |
| 12 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 5da5025 | 2009-07-01 14:46:08 -0300 | [diff] [blame] | 13 | #include <linux/list.h> |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 14 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 15 | #include <linux/rbtree.h> |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 16 | #include "util/symbol.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 17 | |
| 18 | #include "perf.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 19 | #include "util/debug.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 20 | |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 21 | #include "util/event.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 22 | #include "util/parse-options.h" |
| 23 | #include "util/parse-events.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 24 | #include "util/thread.h" |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 25 | #include "util/sort.h" |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 26 | #include "util/hist.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 27 | #include "util/session.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 28 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 29 | static char const *input_name = "perf.data"; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 30 | |
Arnaldo Carvalho de Melo | 8b9e74e | 2010-08-21 10:38:16 -0300 | [diff] [blame] | 31 | static bool force, use_tui, use_stdio; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 32 | |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 33 | static bool full_paths; |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 34 | |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 35 | static bool print_line; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 36 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 37 | static const char *sym_hist_filter; |
| 38 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 39 | static int hists__add_entry(struct hists *self, struct addr_location *al) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 40 | { |
Arnaldo Carvalho de Melo | 628ada0 | 2010-02-25 12:57:40 -0300 | [diff] [blame] | 41 | struct hist_entry *he; |
| 42 | |
| 43 | if (sym_hist_filter != NULL && |
| 44 | (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { |
| 45 | /* We're only interested in a symbol named sym_hist_filter */ |
| 46 | if (al->sym != NULL) { |
| 47 | rb_erase(&al->sym->rb_node, |
| 48 | &al->map->dso->symbols[al->map->type]); |
| 49 | symbol__delete(al->sym); |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 54 | he = __hists__add_entry(self, al, NULL, 1); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 55 | if (he == NULL) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 56 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 628ada0 | 2010-02-25 12:57:40 -0300 | [diff] [blame] | 57 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 58 | return hist_entry__inc_addr_samples(he, al->addr); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 61 | static int process_sample_event(union perf_event *event, |
| 62 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 63 | struct perf_session *session) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 64 | { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 65 | struct addr_location al; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 66 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 67 | if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) { |
Arnaldo Carvalho de Melo | 29a9f66 | 2010-02-03 16:52:06 -0200 | [diff] [blame] | 68 | pr_warning("problem processing %d event, skipping it.\n", |
| 69 | event->header.type); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 73 | if (!al.filtered && hists__add_entry(&session->hists, &al)) { |
Arnaldo Carvalho de Melo | 29a9f66 | 2010-02-03 16:52:06 -0200 | [diff] [blame] | 74 | pr_warning("problem incrementing symbol count, " |
| 75 | "skipping event\n"); |
Arnaldo Carvalho de Melo | ec218fc | 2009-10-03 20:30:48 -0300 | [diff] [blame] | 76 | return -1; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 77 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 82 | static int objdump_line__print(struct objdump_line *self, |
| 83 | struct list_head *head, |
| 84 | struct hist_entry *he, u64 len) |
| 85 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 86 | struct symbol *sym = he->ms.sym; |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 87 | static const char *prev_line; |
| 88 | static const char *prev_color; |
| 89 | |
| 90 | if (self->offset != -1) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 91 | const char *path = NULL; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 92 | unsigned int hits = 0; |
| 93 | double percent = 0.0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 94 | const char *color; |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 95 | struct sym_priv *priv = symbol__priv(sym); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 96 | struct sym_ext *sym_ext = priv->ext; |
| 97 | struct sym_hist *h = priv->hist; |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 98 | s64 offset = self->offset; |
| 99 | struct objdump_line *next = objdump__get_next_ip_line(head, self); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 101 | while (offset < (s64)len && |
| 102 | (next == NULL || offset < next->offset)) { |
| 103 | if (sym_ext) { |
| 104 | if (path == NULL) |
| 105 | path = sym_ext[offset].path; |
| 106 | percent += sym_ext[offset].percent; |
| 107 | } else |
| 108 | hits += h->ip[offset]; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 109 | |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 110 | ++offset; |
| 111 | } |
| 112 | |
| 113 | if (sym_ext == NULL && h->sum) |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 114 | percent = 100.0 * hits / h->sum; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 115 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 116 | color = get_percent_color(percent); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 117 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 118 | /* |
| 119 | * Also color the filename and line if needed, with |
| 120 | * the same color than the percentage. Don't print it |
| 121 | * twice for close colored ip with the same filename:line |
| 122 | */ |
| 123 | if (path) { |
| 124 | if (!prev_line || strcmp(prev_line, path) |
| 125 | || color != prev_color) { |
| 126 | color_fprintf(stdout, color, " %s", path); |
| 127 | prev_line = path; |
| 128 | prev_color = color; |
| 129 | } |
| 130 | } |
| 131 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 132 | color_fprintf(stdout, color, " %7.2f", percent); |
| 133 | printf(" : "); |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 134 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 135 | } else { |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 136 | if (!*self->line) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 137 | printf(" :\n"); |
| 138 | else |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 139 | printf(" : %s\n", self->line); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 145 | static struct rb_root root_sym_ext; |
| 146 | |
| 147 | static void insert_source_line(struct sym_ext *sym_ext) |
| 148 | { |
| 149 | struct sym_ext *iter; |
| 150 | struct rb_node **p = &root_sym_ext.rb_node; |
| 151 | struct rb_node *parent = NULL; |
| 152 | |
| 153 | while (*p != NULL) { |
| 154 | parent = *p; |
| 155 | iter = rb_entry(parent, struct sym_ext, node); |
| 156 | |
| 157 | if (sym_ext->percent > iter->percent) |
| 158 | p = &(*p)->rb_left; |
| 159 | else |
| 160 | p = &(*p)->rb_right; |
| 161 | } |
| 162 | |
| 163 | rb_link_node(&sym_ext->node, parent, p); |
| 164 | rb_insert_color(&sym_ext->node, &root_sym_ext); |
| 165 | } |
| 166 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 167 | static void free_source_line(struct hist_entry *he, int len) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 168 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 169 | struct sym_priv *priv = symbol__priv(he->ms.sym); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 170 | struct sym_ext *sym_ext = priv->ext; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 171 | int i; |
| 172 | |
| 173 | if (!sym_ext) |
| 174 | return; |
| 175 | |
| 176 | for (i = 0; i < len; i++) |
| 177 | free(sym_ext[i].path); |
| 178 | free(sym_ext); |
| 179 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 180 | priv->ext = NULL; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 181 | root_sym_ext = RB_ROOT; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Get the filename:line for the colored entries */ |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 185 | static void |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 186 | get_source_line(struct hist_entry *he, int len, const char *filename) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 187 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 188 | struct symbol *sym = he->ms.sym; |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 189 | u64 start; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 190 | int i; |
| 191 | char cmd[PATH_MAX * 2]; |
| 192 | struct sym_ext *sym_ext; |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 193 | struct sym_priv *priv = symbol__priv(sym); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 194 | struct sym_hist *h = priv->hist; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 195 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 196 | if (!h->sum) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 197 | return; |
| 198 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 199 | sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); |
| 200 | if (!priv->ext) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 201 | return; |
| 202 | |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 203 | start = he->ms.map->unmap_ip(he->ms.map, sym->start); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < len; i++) { |
| 206 | char *path = NULL; |
| 207 | size_t line_len; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 208 | u64 offset; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 209 | FILE *fp; |
| 210 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 211 | sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 212 | if (sym_ext[i].percent <= 0.5) |
| 213 | continue; |
| 214 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 215 | offset = start + i; |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 216 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 217 | fp = popen(cmd, "r"); |
| 218 | if (!fp) |
| 219 | continue; |
| 220 | |
| 221 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 222 | goto next; |
| 223 | |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 224 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 225 | if (!sym_ext[i].path) |
| 226 | goto next; |
| 227 | |
| 228 | strcpy(sym_ext[i].path, path); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 229 | insert_source_line(&sym_ext[i]); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 230 | |
| 231 | next: |
| 232 | pclose(fp); |
| 233 | } |
| 234 | } |
| 235 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 236 | static void print_summary(const char *filename) |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 237 | { |
| 238 | struct sym_ext *sym_ext; |
| 239 | struct rb_node *node; |
| 240 | |
| 241 | printf("\nSorted summary for file %s\n", filename); |
| 242 | printf("----------------------------------------------\n\n"); |
| 243 | |
| 244 | if (RB_EMPTY_ROOT(&root_sym_ext)) { |
| 245 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | node = rb_first(&root_sym_ext); |
| 250 | while (node) { |
| 251 | double percent; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 252 | const char *color; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 253 | char *path; |
| 254 | |
| 255 | sym_ext = rb_entry(node, struct sym_ext, node); |
| 256 | percent = sym_ext->percent; |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 257 | color = get_percent_color(percent); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 258 | path = sym_ext->path; |
| 259 | |
| 260 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 261 | node = rb_next(node); |
| 262 | } |
| 263 | } |
| 264 | |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 265 | static void hist_entry__print_hits(struct hist_entry *self) |
| 266 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 267 | struct symbol *sym = self->ms.sym; |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 268 | struct sym_priv *priv = symbol__priv(sym); |
| 269 | struct sym_hist *h = priv->hist; |
| 270 | u64 len = sym->end - sym->start, offset; |
| 271 | |
| 272 | for (offset = 0; offset < len; ++offset) |
| 273 | if (h->ip[offset] != 0) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 274 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 275 | sym->start + offset, h->ip[offset]); |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 276 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 277 | } |
| 278 | |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 279 | static int hist_entry__tty_annotate(struct hist_entry *he) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 280 | { |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 281 | struct map *map = he->ms.map; |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 282 | struct dso *dso = map->dso; |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 283 | struct symbol *sym = he->ms.sym; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 284 | const char *filename = dso->long_name, *d_filename; |
| 285 | u64 len; |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 286 | LIST_HEAD(head); |
| 287 | struct objdump_line *pos, *n; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 288 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 289 | if (hist_entry__annotate(he, &head, 0) < 0) |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 290 | return -1; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 291 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 292 | if (full_paths) |
| 293 | d_filename = filename; |
| 294 | else |
| 295 | d_filename = basename(filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 296 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 297 | len = sym->end - sym->start; |
| 298 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 299 | if (print_line) { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 300 | get_source_line(he, len, filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 301 | print_summary(filename); |
| 302 | } |
| 303 | |
| 304 | printf("\n\n------------------------------------------------\n"); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 305 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 306 | printf("------------------------------------------------\n"); |
| 307 | |
Arnaldo Carvalho de Melo | 48fb4fd | 2010-02-26 11:23:14 -0300 | [diff] [blame] | 308 | if (verbose) |
| 309 | hist_entry__print_hits(he); |
| 310 | |
| 311 | list_for_each_entry_safe(pos, n, &head, node) { |
| 312 | objdump_line__print(pos, &head, he, len); |
| 313 | list_del(&pos->node); |
| 314 | objdump_line__free(pos); |
| 315 | } |
| 316 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 317 | if (print_line) |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 318 | free_source_line(he, len); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 319 | |
| 320 | return 0; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 323 | static void hists__find_annotations(struct hists *self) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 324 | { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 325 | struct rb_node *nd = rb_first(&self->entries), *next; |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 326 | int key = KEY_RIGHT; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 327 | |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 328 | while (nd) { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 329 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 330 | struct sym_priv *priv; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 331 | |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 332 | if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) |
| 333 | goto find_next; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 334 | |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 335 | priv = symbol__priv(he->ms.sym); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 336 | if (priv->hist == NULL) { |
| 337 | find_next: |
| 338 | if (key == KEY_LEFT) |
| 339 | nd = rb_prev(nd); |
| 340 | else |
| 341 | nd = rb_next(nd); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 342 | continue; |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 343 | } |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 344 | |
Arnaldo Carvalho de Melo | 62e3436 | 2010-05-26 13:22:26 -0300 | [diff] [blame] | 345 | if (use_browser > 0) { |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 346 | key = hist_entry__tui_annotate(he); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 347 | switch (key) { |
| 348 | case KEY_RIGHT: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 349 | next = rb_next(nd); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 350 | break; |
| 351 | case KEY_LEFT: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 352 | next = rb_prev(nd); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 353 | break; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 354 | default: |
| 355 | return; |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 356 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 357 | |
| 358 | if (next != NULL) |
| 359 | nd = next; |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 360 | } else { |
| 361 | hist_entry__tty_annotate(he); |
| 362 | nd = rb_next(nd); |
| 363 | /* |
| 364 | * Since we have a hist_entry per IP for the same |
| 365 | * symbol, free he->ms.sym->hist to signal we already |
| 366 | * processed this symbol. |
| 367 | */ |
| 368 | free(priv->hist); |
| 369 | priv->hist = NULL; |
| 370 | } |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 371 | } |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 374 | static struct perf_event_ops event_ops = { |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 375 | .sample = process_sample_event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 376 | .mmap = perf_event__process_mmap, |
| 377 | .comm = perf_event__process_comm, |
| 378 | .fork = perf_event__process_task, |
Ian Munsie | eac23d1 | 2010-12-09 16:33:53 +1100 | [diff] [blame] | 379 | .ordered_samples = true, |
| 380 | .ordering_requires_timestamps = true, |
Li Zefan | bab81b6 | 2009-12-01 14:04:49 +0800 | [diff] [blame] | 381 | }; |
| 382 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 383 | static int __cmd_annotate(void) |
| 384 | { |
Li Zefan | bab81b6 | 2009-12-01 14:04:49 +0800 | [diff] [blame] | 385 | int ret; |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 386 | struct perf_session *session; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 387 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 388 | session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 389 | if (session == NULL) |
| 390 | return -ENOMEM; |
| 391 | |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 392 | ret = perf_session__process_events(session, &event_ops); |
Li Zefan | bab81b6 | 2009-12-01 14:04:49 +0800 | [diff] [blame] | 393 | if (ret) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 394 | goto out_delete; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 395 | |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 396 | if (dump_trace) { |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 397 | perf_session__fprintf_nr_events(session, stdout); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 398 | goto out_delete; |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 399 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 400 | |
Arnaldo Carvalho de Melo | da21d1b | 2009-10-07 10:49:00 -0300 | [diff] [blame] | 401 | if (verbose > 3) |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 402 | perf_session__fprintf(session, stdout); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 403 | |
Arnaldo Carvalho de Melo | da21d1b | 2009-10-07 10:49:00 -0300 | [diff] [blame] | 404 | if (verbose > 2) |
Arnaldo Carvalho de Melo | cbf6968 | 2010-04-27 21:22:44 -0300 | [diff] [blame] | 405 | perf_session__fprintf_dsos(session, stdout); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 406 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 407 | hists__collapse_resort(&session->hists); |
| 408 | hists__output_resort(&session->hists); |
| 409 | hists__find_annotations(&session->hists); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 410 | out_delete: |
| 411 | perf_session__delete(session); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 412 | |
Li Zefan | bab81b6 | 2009-12-01 14:04:49 +0800 | [diff] [blame] | 413 | return ret; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static const char * const annotate_usage[] = { |
| 417 | "perf annotate [<options>] <command>", |
| 418 | NULL |
| 419 | }; |
| 420 | |
| 421 | static const struct option options[] = { |
| 422 | OPT_STRING('i', "input", &input_name, "file", |
| 423 | "input file name"), |
Arnaldo Carvalho de Melo | ac73c5a | 2010-03-24 16:40:16 -0300 | [diff] [blame] | 424 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
| 425 | "only consider symbols in these dsos"), |
Ingo Molnar | 23b8711 | 2009-06-06 21:25:29 +0200 | [diff] [blame] | 426 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 427 | "symbol to annotate"), |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 428 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 429 | OPT_INCR('v', "verbose", &verbose, |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 430 | "be more verbose (show symbol address, etc)"), |
| 431 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 432 | "dump raw trace in ASCII"), |
Arnaldo Carvalho de Melo | 8b9e74e | 2010-08-21 10:38:16 -0300 | [diff] [blame] | 433 | OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), |
| 434 | OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 435 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 436 | "file", "vmlinux pathname"), |
| 437 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 438 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 439 | OPT_BOOLEAN('l', "print-line", &print_line, |
| 440 | "print matching source lines (may be slow)"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 441 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 442 | "Don't shorten the displayed pathnames"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 443 | OPT_END() |
| 444 | }; |
| 445 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 446 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 447 | { |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 448 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 449 | |
Arnaldo Carvalho de Melo | 8b9e74e | 2010-08-21 10:38:16 -0300 | [diff] [blame] | 450 | if (use_stdio) |
| 451 | use_browser = 0; |
| 452 | else if (use_tui) |
| 453 | use_browser = 1; |
| 454 | |
Arnaldo Carvalho de Melo | 229ade9 | 2011-01-31 18:08:39 -0200 | [diff] [blame] | 455 | setup_browser(true); |
Arnaldo Carvalho de Melo | 46e3e05 | 2010-05-22 11:25:40 -0300 | [diff] [blame] | 456 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 457 | symbol_conf.priv_size = sizeof(struct sym_priv); |
| 458 | symbol_conf.try_vmlinux_path = true; |
| 459 | |
| 460 | if (symbol__init() < 0) |
Arnaldo Carvalho de Melo | b32d133 | 2009-11-24 12:05:15 -0200 | [diff] [blame] | 461 | return -1; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 462 | |
Arnaldo Carvalho de Melo | c8829c7 | 2009-12-14 20:09:29 -0200 | [diff] [blame] | 463 | setup_sorting(annotate_usage, options); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 464 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 465 | if (argc) { |
| 466 | /* |
| 467 | * Special case: if there's an argument left then assume tha |
| 468 | * it's a symbol filter: |
| 469 | */ |
| 470 | if (argc > 1) |
| 471 | usage_with_options(annotate_usage, options); |
| 472 | |
| 473 | sym_hist_filter = argv[0]; |
| 474 | } |
| 475 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 476 | if (field_sep && *field_sep == '.') { |
Arnaldo Carvalho de Melo | 29a9f66 | 2010-02-03 16:52:06 -0200 | [diff] [blame] | 477 | pr_err("'.' is the only non valid --field-separator argument\n"); |
| 478 | return -1; |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 481 | return __cmd_annotate(); |
| 482 | } |