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