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