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