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