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 | 8f28827a | 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 | |
| 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" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 27 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 28 | static char const *input_name = "perf.data"; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 29 | |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 30 | static int force; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 31 | static int input; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 32 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 33 | static int full_paths; |
| 34 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 35 | static int print_line; |
| 36 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 37 | static unsigned long page_size; |
| 38 | static unsigned long mmap_window = 32; |
| 39 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 40 | struct sym_hist { |
| 41 | u64 sum; |
| 42 | u64 ip[0]; |
| 43 | }; |
| 44 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 45 | struct sym_ext { |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 46 | struct rb_node node; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 47 | double percent; |
| 48 | char *path; |
| 49 | }; |
| 50 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 51 | struct sym_priv { |
| 52 | struct sym_hist *hist; |
| 53 | struct sym_ext *ext; |
| 54 | }; |
| 55 | |
| 56 | static const char *sym_hist_filter; |
| 57 | |
| 58 | static int symbol_filter(struct map *map, struct symbol *sym) |
| 59 | { |
Arnaldo Carvalho de Melo | 8f0b037 | 2009-10-20 15:08:29 -0200 | [diff] [blame^] | 60 | if (sym_hist_filter == NULL || |
| 61 | strcmp(sym->name, sym_hist_filter) == 0) { |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 62 | struct sym_priv *priv = dso__sym_priv(map->dso, sym); |
| 63 | const int size = (sizeof(*priv->hist) + |
| 64 | (sym->end - sym->start) * sizeof(u64)); |
| 65 | |
| 66 | priv->hist = malloc(size); |
| 67 | if (priv->hist) |
| 68 | memset(priv->hist, 0, size); |
| 69 | return 0; |
| 70 | } |
| 71 | /* |
| 72 | * FIXME: We should really filter it out, as we don't want to go thru symbols |
| 73 | * we're not interested, and if a DSO ends up with no symbols, delete it too, |
| 74 | * but right now the kernel loading routines in symbol.c bail out if no symbols |
| 75 | * are found, fix it later. |
| 76 | */ |
| 77 | return 0; |
| 78 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 79 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 80 | /* |
| 81 | * collect histogram counts |
| 82 | */ |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 83 | static void hist_hit(struct hist_entry *he, u64 ip) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 84 | { |
| 85 | unsigned int sym_size, offset; |
| 86 | struct symbol *sym = he->sym; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 87 | struct sym_priv *priv; |
| 88 | struct sym_hist *h; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 89 | |
| 90 | he->count++; |
| 91 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 92 | if (!sym || !he->map) |
| 93 | return; |
| 94 | |
| 95 | priv = dso__sym_priv(he->map->dso, sym); |
| 96 | if (!priv->hist) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 97 | return; |
| 98 | |
| 99 | sym_size = sym->end - sym->start; |
| 100 | offset = ip - sym->start; |
| 101 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 102 | if (verbose) |
| 103 | fprintf(stderr, "%s: ip=%Lx\n", __func__, |
| 104 | he->map->unmap_ip(he->map, ip)); |
| 105 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 106 | if (offset >= sym_size) |
| 107 | return; |
| 108 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 109 | h = priv->hist; |
| 110 | h->sum++; |
| 111 | h->ip[offset]++; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 112 | |
| 113 | if (verbose >= 3) |
| 114 | printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 115 | (void *)(unsigned long)he->sym->start, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 116 | he->sym->name, |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 117 | (void *)(unsigned long)ip, ip - he->sym->start, |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 118 | h->ip[offset]); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 119 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 120 | |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 121 | static int hist_entry__add(struct thread *thread, struct map *map, |
| 122 | struct symbol *sym, u64 ip, u64 count, char level) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 123 | { |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 124 | bool hit; |
| 125 | struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip, |
| 126 | count, level, &hit); |
| 127 | if (he == NULL) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 128 | return -ENOMEM; |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 129 | hist_hit(he, ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 133 | static int |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 134 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 135 | { |
| 136 | char level; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 137 | u64 ip = event->ip.ip; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 138 | struct map *map = NULL; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 139 | struct symbol *sym = NULL; |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 140 | struct thread *thread = threads__findnew(event->ip.pid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 141 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 142 | dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 143 | (void *)(offset + head), |
| 144 | (void *)(long)(event->header.size), |
| 145 | event->header.misc, |
| 146 | event->ip.pid, |
| 147 | (void *)(long)ip); |
| 148 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 149 | if (thread == NULL) { |
| 150 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 151 | event->header.type); |
| 152 | return -1; |
| 153 | } |
| 154 | |
Julia Lawall | f39cdf2 | 2009-10-17 08:43:17 +0200 | [diff] [blame] | 155 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 156 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 157 | if (event->header.misc & PERF_RECORD_MISC_KERNEL) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 158 | level = 'k'; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 159 | sym = kernel_maps__find_symbol(ip, &map); |
| 160 | dump_printf(" ...... dso: %s\n", |
| 161 | map ? map->dso->long_name : "<not found>"); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 162 | } else if (event->header.misc & PERF_RECORD_MISC_USER) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 163 | level = '.'; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 164 | map = thread__find_map(thread, ip); |
| 165 | if (map != NULL) { |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 166 | got_map: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 167 | ip = map->map_ip(map, ip); |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 168 | sym = map->dso->find_symbol(map->dso, ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 169 | } else { |
| 170 | /* |
| 171 | * If this is outside of all known maps, |
| 172 | * and is a negative address, try to look it |
| 173 | * up in the kernel dso, as it might be a |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 174 | * vsyscall or vdso (which executes in user-mode). |
| 175 | * |
| 176 | * XXX This is nasty, we should have a symbol list in |
| 177 | * the "[vdso]" dso, but for now lets use the old |
| 178 | * trick of looking in the whole kernel symbol list. |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 179 | */ |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 180 | if ((long long)ip < 0) { |
| 181 | map = kernel_map; |
| 182 | goto got_map; |
| 183 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 184 | } |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 185 | dump_printf(" ...... dso: %s\n", |
| 186 | map ? map->dso->long_name : "<not found>"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 187 | } else { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 188 | level = 'H'; |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 189 | dump_printf(" ...... dso: [hypervisor]\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | ec218fc | 2009-10-03 20:30:48 -0300 | [diff] [blame] | 192 | if (hist_entry__add(thread, map, sym, ip, 1, level)) { |
| 193 | fprintf(stderr, "problem incrementing symbol count, " |
| 194 | "skipping event\n"); |
| 195 | return -1; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 196 | } |
| 197 | total++; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int |
| 203 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) |
| 204 | { |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 205 | struct map *map = map__new(&event->mmap, NULL, 0, |
| 206 | sizeof(struct sym_priv), symbol_filter, |
| 207 | verbose); |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 208 | struct thread *thread = threads__findnew(event->mmap.pid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 209 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 210 | dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 211 | (void *)(offset + head), |
| 212 | (void *)(long)(event->header.size), |
| 213 | event->mmap.pid, |
| 214 | (void *)(long)event->mmap.start, |
| 215 | (void *)(long)event->mmap.len, |
| 216 | (void *)(long)event->mmap.pgoff, |
| 217 | event->mmap.filename); |
| 218 | |
| 219 | if (thread == NULL || map == NULL) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 220 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | thread__insert_map(thread, map); |
| 225 | total_mmap++; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int |
| 231 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 232 | { |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 233 | struct thread *thread = threads__findnew(event->comm.pid); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 234 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 235 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 236 | (void *)(offset + head), |
| 237 | (void *)(long)(event->header.size), |
| 238 | event->comm.comm, event->comm.pid); |
| 239 | |
| 240 | if (thread == NULL || |
| 241 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 242 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 243 | return -1; |
| 244 | } |
| 245 | total_comm++; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int |
| 251 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) |
| 252 | { |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 253 | struct thread *thread = threads__findnew(event->fork.pid); |
| 254 | struct thread *parent = threads__findnew(event->fork.ppid); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 255 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 256 | dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 257 | (void *)(offset + head), |
| 258 | (void *)(long)(event->header.size), |
| 259 | event->fork.pid, event->fork.ppid); |
| 260 | |
Ingo Molnar | 15f3fa4 | 2009-08-18 13:52:28 +0200 | [diff] [blame] | 261 | /* |
| 262 | * A thread clone will have the same PID for both |
| 263 | * parent and child. |
| 264 | */ |
| 265 | if (thread == parent) |
| 266 | return 0; |
| 267 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 268 | if (!thread || !parent || thread__fork(thread, parent)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 269 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 270 | return -1; |
| 271 | } |
| 272 | total_fork++; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 278 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 279 | { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 280 | switch (event->header.type) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 281 | case PERF_RECORD_SAMPLE: |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 282 | return process_sample_event(event, offset, head); |
| 283 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 284 | case PERF_RECORD_MMAP: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 285 | return process_mmap_event(event, offset, head); |
| 286 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 287 | case PERF_RECORD_COMM: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 288 | return process_comm_event(event, offset, head); |
| 289 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 290 | case PERF_RECORD_FORK: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 291 | return process_fork_event(event, offset, head); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 292 | /* |
| 293 | * We dont process them right now but they are fine: |
| 294 | */ |
| 295 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 296 | case PERF_RECORD_THROTTLE: |
| 297 | case PERF_RECORD_UNTHROTTLE: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 298 | return 0; |
| 299 | |
| 300 | default: |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 307 | static int parse_line(FILE *file, struct hist_entry *he, u64 len) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 308 | { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 309 | struct symbol *sym = he->sym; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 310 | char *line = NULL, *tmp, *tmp2; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 311 | static const char *prev_line; |
| 312 | static const char *prev_color; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 313 | unsigned int offset; |
| 314 | size_t line_len; |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 315 | u64 start; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 316 | s64 line_ip; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 317 | int ret; |
| 318 | char *c; |
| 319 | |
| 320 | if (getline(&line, &line_len, file) < 0) |
| 321 | return -1; |
| 322 | if (!line) |
| 323 | return -1; |
| 324 | |
| 325 | c = strchr(line, '\n'); |
| 326 | if (c) |
| 327 | *c = 0; |
| 328 | |
| 329 | line_ip = -1; |
| 330 | offset = 0; |
| 331 | ret = -2; |
| 332 | |
| 333 | /* |
| 334 | * Strip leading spaces: |
| 335 | */ |
| 336 | tmp = line; |
| 337 | while (*tmp) { |
| 338 | if (*tmp != ' ') |
| 339 | break; |
| 340 | tmp++; |
| 341 | } |
| 342 | |
| 343 | if (*tmp) { |
| 344 | /* |
| 345 | * Parse hexa addresses followed by ':' |
| 346 | */ |
| 347 | line_ip = strtoull(tmp, &tmp2, 16); |
| 348 | if (*tmp2 != ':') |
| 349 | line_ip = -1; |
| 350 | } |
| 351 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 352 | start = he->map->unmap_ip(he->map, sym->start); |
| 353 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 354 | if (line_ip != -1) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 355 | const char *path = NULL; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 356 | unsigned int hits = 0; |
| 357 | double percent = 0.0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 358 | const char *color; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 359 | struct sym_priv *priv = dso__sym_priv(he->map->dso, sym); |
| 360 | struct sym_ext *sym_ext = priv->ext; |
| 361 | struct sym_hist *h = priv->hist; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 362 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 363 | offset = line_ip - start; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 364 | if (offset < len) |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 365 | hits = h->ip[offset]; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 366 | |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 367 | if (offset < len && sym_ext) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 368 | path = sym_ext[offset].path; |
| 369 | percent = sym_ext[offset].percent; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 370 | } else if (h->sum) |
| 371 | percent = 100.0 * hits / h->sum; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 372 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 373 | color = get_percent_color(percent); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 374 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 375 | /* |
| 376 | * Also color the filename and line if needed, with |
| 377 | * the same color than the percentage. Don't print it |
| 378 | * twice for close colored ip with the same filename:line |
| 379 | */ |
| 380 | if (path) { |
| 381 | if (!prev_line || strcmp(prev_line, path) |
| 382 | || color != prev_color) { |
| 383 | color_fprintf(stdout, color, " %s", path); |
| 384 | prev_line = path; |
| 385 | prev_color = color; |
| 386 | } |
| 387 | } |
| 388 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 389 | color_fprintf(stdout, color, " %7.2f", percent); |
| 390 | printf(" : "); |
| 391 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line); |
| 392 | } else { |
| 393 | if (!*line) |
| 394 | printf(" :\n"); |
| 395 | else |
| 396 | printf(" : %s\n", line); |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 402 | static struct rb_root root_sym_ext; |
| 403 | |
| 404 | static void insert_source_line(struct sym_ext *sym_ext) |
| 405 | { |
| 406 | struct sym_ext *iter; |
| 407 | struct rb_node **p = &root_sym_ext.rb_node; |
| 408 | struct rb_node *parent = NULL; |
| 409 | |
| 410 | while (*p != NULL) { |
| 411 | parent = *p; |
| 412 | iter = rb_entry(parent, struct sym_ext, node); |
| 413 | |
| 414 | if (sym_ext->percent > iter->percent) |
| 415 | p = &(*p)->rb_left; |
| 416 | else |
| 417 | p = &(*p)->rb_right; |
| 418 | } |
| 419 | |
| 420 | rb_link_node(&sym_ext->node, parent, p); |
| 421 | rb_insert_color(&sym_ext->node, &root_sym_ext); |
| 422 | } |
| 423 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 424 | static void free_source_line(struct hist_entry *he, int len) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 425 | { |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 426 | struct sym_priv *priv = dso__sym_priv(he->map->dso, he->sym); |
| 427 | struct sym_ext *sym_ext = priv->ext; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 428 | int i; |
| 429 | |
| 430 | if (!sym_ext) |
| 431 | return; |
| 432 | |
| 433 | for (i = 0; i < len; i++) |
| 434 | free(sym_ext[i].path); |
| 435 | free(sym_ext); |
| 436 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 437 | priv->ext = NULL; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 438 | root_sym_ext = RB_ROOT; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* Get the filename:line for the colored entries */ |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 442 | static void |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 443 | get_source_line(struct hist_entry *he, int len, const char *filename) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 444 | { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 445 | struct symbol *sym = he->sym; |
| 446 | u64 start; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 447 | int i; |
| 448 | char cmd[PATH_MAX * 2]; |
| 449 | struct sym_ext *sym_ext; |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 450 | struct sym_priv *priv = dso__sym_priv(he->map->dso, sym); |
| 451 | struct sym_hist *h = priv->hist; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 452 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 453 | if (!h->sum) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 454 | return; |
| 455 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 456 | sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); |
| 457 | if (!priv->ext) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 458 | return; |
| 459 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 460 | start = he->map->unmap_ip(he->map, sym->start); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < len; i++) { |
| 463 | char *path = NULL; |
| 464 | size_t line_len; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 465 | u64 offset; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 466 | FILE *fp; |
| 467 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 468 | sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 469 | if (sym_ext[i].percent <= 0.5) |
| 470 | continue; |
| 471 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 472 | offset = start + i; |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 473 | sprintf(cmd, "addr2line -e %s %016llx", filename, offset); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 474 | fp = popen(cmd, "r"); |
| 475 | if (!fp) |
| 476 | continue; |
| 477 | |
| 478 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 479 | goto next; |
| 480 | |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 481 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 482 | if (!sym_ext[i].path) |
| 483 | goto next; |
| 484 | |
| 485 | strcpy(sym_ext[i].path, path); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 486 | insert_source_line(&sym_ext[i]); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 487 | |
| 488 | next: |
| 489 | pclose(fp); |
| 490 | } |
| 491 | } |
| 492 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 493 | static void print_summary(const char *filename) |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 494 | { |
| 495 | struct sym_ext *sym_ext; |
| 496 | struct rb_node *node; |
| 497 | |
| 498 | printf("\nSorted summary for file %s\n", filename); |
| 499 | printf("----------------------------------------------\n\n"); |
| 500 | |
| 501 | if (RB_EMPTY_ROOT(&root_sym_ext)) { |
| 502 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | node = rb_first(&root_sym_ext); |
| 507 | while (node) { |
| 508 | double percent; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 509 | const char *color; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 510 | char *path; |
| 511 | |
| 512 | sym_ext = rb_entry(node, struct sym_ext, node); |
| 513 | percent = sym_ext->percent; |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 514 | color = get_percent_color(percent); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 515 | path = sym_ext->path; |
| 516 | |
| 517 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 518 | node = rb_next(node); |
| 519 | } |
| 520 | } |
| 521 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 522 | static void annotate_sym(struct hist_entry *he) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 523 | { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 524 | struct map *map = he->map; |
| 525 | struct dso *dso = map->dso; |
| 526 | struct symbol *sym = he->sym; |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 527 | const char *filename = dso->long_name, *d_filename; |
| 528 | u64 len; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 529 | char command[PATH_MAX*2]; |
| 530 | FILE *file; |
| 531 | |
| 532 | if (!filename) |
| 533 | return; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 534 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 535 | if (verbose) |
| 536 | fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n", |
| 537 | __func__, filename, sym->name, |
| 538 | map->unmap_ip(map, sym->start), |
| 539 | map->unmap_ip(map, sym->end)); |
| 540 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 541 | if (full_paths) |
| 542 | d_filename = filename; |
| 543 | else |
| 544 | d_filename = basename(filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 545 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 546 | len = sym->end - sym->start; |
| 547 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 548 | if (print_line) { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 549 | get_source_line(he, len, filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 550 | print_summary(filename); |
| 551 | } |
| 552 | |
| 553 | printf("\n\n------------------------------------------------\n"); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 554 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 555 | printf("------------------------------------------------\n"); |
| 556 | |
| 557 | if (verbose >= 2) |
Arnaldo Carvalho de Melo | 439d473 | 2009-10-02 03:29:58 -0300 | [diff] [blame] | 558 | printf("annotating [%p] %30s : [%p] %30s\n", |
| 559 | dso, dso->long_name, sym, sym->name); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 560 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 561 | 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] | 562 | map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end), |
| 563 | filename, filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 564 | |
| 565 | if (verbose >= 3) |
| 566 | printf("doing: %s\n", command); |
| 567 | |
| 568 | file = popen(command, "r"); |
| 569 | if (!file) |
| 570 | return; |
| 571 | |
| 572 | while (!feof(file)) { |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 573 | if (parse_line(file, he, len) < 0) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | |
| 577 | pclose(file); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 578 | if (print_line) |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 579 | free_source_line(he, len); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static void find_annotations(void) |
| 583 | { |
| 584 | struct rb_node *nd; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 585 | |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 586 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 587 | 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] | 588 | struct sym_priv *priv; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 589 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 590 | if (he->sym == NULL) |
| 591 | continue; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 592 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 593 | priv = dso__sym_priv(he->map->dso, he->sym); |
| 594 | if (priv->hist == NULL) |
| 595 | continue; |
| 596 | |
| 597 | annotate_sym(he); |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 598 | /* |
| 599 | * Since we have a hist_entry per IP for the same symbol, free |
| 600 | * he->sym->hist to signal we already processed this symbol. |
| 601 | */ |
| 602 | free(priv->hist); |
| 603 | priv->hist = NULL; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 604 | } |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 607 | static int __cmd_annotate(void) |
| 608 | { |
| 609 | int ret, rc = EXIT_FAILURE; |
| 610 | unsigned long offset = 0; |
| 611 | unsigned long head = 0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 612 | struct stat input_stat; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 613 | event_t *event; |
| 614 | uint32_t size; |
| 615 | char *buf; |
| 616 | |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 617 | register_idle_thread(); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 618 | |
| 619 | input = open(input_name, O_RDONLY); |
| 620 | if (input < 0) { |
| 621 | perror("failed to open file"); |
| 622 | exit(-1); |
| 623 | } |
| 624 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 625 | ret = fstat(input, &input_stat); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 626 | if (ret < 0) { |
| 627 | perror("failed to stat file"); |
| 628 | exit(-1); |
| 629 | } |
| 630 | |
Pierre Habouzit | 119e7a2 | 2009-08-27 09:59:02 +0200 | [diff] [blame] | 631 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 632 | fprintf(stderr, "file: %s not owned by current user or root\n", input_name); |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 633 | exit(-1); |
| 634 | } |
| 635 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 636 | if (!input_stat.st_size) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 637 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 638 | exit(0); |
| 639 | } |
| 640 | |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 641 | if (load_kernel(sizeof(struct sym_priv), symbol_filter) < 0) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 642 | perror("failed to load kernel symbols"); |
| 643 | return EXIT_FAILURE; |
| 644 | } |
| 645 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 646 | remap: |
| 647 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 648 | MAP_SHARED, input, offset); |
| 649 | if (buf == MAP_FAILED) { |
| 650 | perror("failed to mmap file"); |
| 651 | exit(-1); |
| 652 | } |
| 653 | |
| 654 | more: |
| 655 | event = (event_t *)(buf + head); |
| 656 | |
| 657 | size = event->header.size; |
| 658 | if (!size) |
| 659 | size = 8; |
| 660 | |
| 661 | if (head + event->header.size >= page_size * mmap_window) { |
| 662 | unsigned long shift = page_size * (head / page_size); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 663 | int munmap_ret; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 664 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 665 | munmap_ret = munmap(buf, page_size * mmap_window); |
| 666 | assert(munmap_ret == 0); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 667 | |
| 668 | offset += shift; |
| 669 | head -= shift; |
| 670 | goto remap; |
| 671 | } |
| 672 | |
| 673 | size = event->header.size; |
| 674 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 675 | dump_printf("%p [%p]: event: %d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 676 | (void *)(offset + head), |
| 677 | (void *)(long)event->header.size, |
| 678 | event->header.type); |
| 679 | |
| 680 | if (!size || process_event(event, offset, head) < 0) { |
| 681 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 682 | dump_printf("%p [%p]: skipping unknown header type: %d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 683 | (void *)(offset + head), |
| 684 | (void *)(long)(event->header.size), |
| 685 | event->header.type); |
| 686 | |
| 687 | total_unknown++; |
| 688 | |
| 689 | /* |
| 690 | * assume we lost track of the stream, check alignment, and |
| 691 | * increment a single u64 in the hope to catch on again 'soon'. |
| 692 | */ |
| 693 | |
| 694 | if (unlikely(head & 7)) |
| 695 | head &= ~7ULL; |
| 696 | |
| 697 | size = 8; |
| 698 | } |
| 699 | |
| 700 | head += size; |
| 701 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 702 | if (offset + head < (unsigned long)input_stat.st_size) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 703 | goto more; |
| 704 | |
| 705 | rc = EXIT_SUCCESS; |
| 706 | close(input); |
| 707 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 708 | dump_printf(" IP events: %10ld\n", total); |
| 709 | dump_printf(" mmap events: %10ld\n", total_mmap); |
| 710 | dump_printf(" comm events: %10ld\n", total_comm); |
| 711 | dump_printf(" fork events: %10ld\n", total_fork); |
| 712 | dump_printf(" unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 713 | |
| 714 | if (dump_trace) |
| 715 | return 0; |
| 716 | |
Arnaldo Carvalho de Melo | da21d1b | 2009-10-07 10:49:00 -0300 | [diff] [blame] | 717 | if (verbose > 3) |
Arnaldo Carvalho de Melo | d5b889f | 2009-10-13 11:16:29 -0300 | [diff] [blame] | 718 | threads__fprintf(stdout); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 719 | |
Arnaldo Carvalho de Melo | da21d1b | 2009-10-07 10:49:00 -0300 | [diff] [blame] | 720 | if (verbose > 2) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 721 | dsos__fprintf(stdout); |
| 722 | |
| 723 | collapse__resort(); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 724 | output__resort(total); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 725 | |
| 726 | find_annotations(); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 727 | |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | static const char * const annotate_usage[] = { |
| 732 | "perf annotate [<options>] <command>", |
| 733 | NULL |
| 734 | }; |
| 735 | |
| 736 | static const struct option options[] = { |
| 737 | OPT_STRING('i', "input", &input_name, "file", |
| 738 | "input file name"), |
Ingo Molnar | 23b8711 | 2009-06-06 21:25:29 +0200 | [diff] [blame] | 739 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 740 | "symbol to annotate"), |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 741 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 742 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 743 | "be more verbose (show symbol address, etc)"), |
| 744 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 745 | "dump raw trace in ASCII"), |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 746 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 747 | OPT_BOOLEAN('m', "modules", &modules, |
| 748 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 749 | OPT_BOOLEAN('l', "print-line", &print_line, |
| 750 | "print matching source lines (may be slow)"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 751 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 752 | "Don't shorten the displayed pathnames"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 753 | OPT_END() |
| 754 | }; |
| 755 | |
| 756 | static void setup_sorting(void) |
| 757 | { |
| 758 | char *tmp, *tok, *str = strdup(sort_order); |
| 759 | |
| 760 | for (tok = strtok_r(str, ", ", &tmp); |
| 761 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 762 | if (sort_dimension__add(tok) < 0) { |
| 763 | error("Unknown --sort key: `%s'", tok); |
| 764 | usage_with_options(annotate_usage, options); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | free(str); |
| 769 | } |
| 770 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 771 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 772 | { |
| 773 | symbol__init(); |
| 774 | |
| 775 | page_size = getpagesize(); |
| 776 | |
| 777 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 778 | |
| 779 | setup_sorting(); |
| 780 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 781 | if (argc) { |
| 782 | /* |
| 783 | * Special case: if there's an argument left then assume tha |
| 784 | * it's a symbol filter: |
| 785 | */ |
| 786 | if (argc > 1) |
| 787 | usage_with_options(annotate_usage, options); |
| 788 | |
| 789 | sym_hist_filter = argv[0]; |
| 790 | } |
| 791 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 792 | setup_pager(); |
| 793 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame] | 794 | if (field_sep && *field_sep == '.') { |
| 795 | fputs("'.' is the only non valid --field-separator argument\n", |
| 796 | stderr); |
| 797 | exit(129); |
| 798 | } |
| 799 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 800 | return __cmd_annotate(); |
| 801 | } |