blob: 06f10278b28ea189d12f85b0ee10188a80e0207e [file] [log] [blame]
Ingo Molnar8035e422009-06-06 15:19:13 +02001/*
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 Melo5da50252009-07-01 14:46:08 -030013#include <linux/list.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030015#include <linux/rbtree.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020016#include "util/symbol.h"
17#include "util/string.h"
18
19#include "perf.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020020#include "util/debug.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020021
22#include "util/parse-options.h"
23#include "util/parse-events.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020024#include "util/thread.h"
John Kacurdd68ada2009-09-24 18:02:49 +020025#include "util/sort.h"
John Kacur3d1d07e2009-09-28 15:32:55 +020026#include "util/hist.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020027
Ingo Molnar8035e422009-06-06 15:19:13 +020028static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020029
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020030static int force;
Ingo Molnar8035e422009-06-06 15:19:13 +020031static int input;
Ingo Molnar8035e422009-06-06 15:19:13 +020032
Mike Galbraith42976482009-07-02 08:09:46 +020033static int full_paths;
34
Frederic Weisbecker301406b2009-06-13 00:11:21 +020035static int print_line;
36
Ingo Molnar8035e422009-06-06 15:19:13 +020037static unsigned long page_size;
38static unsigned long mmap_window = 32;
39
Frederic Weisbecker301406b2009-06-13 00:11:21 +020040struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020041 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020042 double percent;
43 char *path;
44};
45
Ingo Molnar8035e422009-06-06 15:19:13 +020046
Ingo Molnar8035e422009-06-06 15:19:13 +020047/*
48 * collect histogram counts
49 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100050static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020051{
52 unsigned int sym_size, offset;
53 struct symbol *sym = he->sym;
54
55 he->count++;
56
57 if (!sym || !sym->hist)
58 return;
59
60 sym_size = sym->end - sym->start;
61 offset = ip - sym->start;
62
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020063 if (verbose)
64 fprintf(stderr, "%s: ip=%Lx\n", __func__,
65 he->map->unmap_ip(he->map, ip));
66
Ingo Molnar0b73da32009-06-06 15:48:52 +020067 if (offset >= sym_size)
68 return;
69
70 sym->hist_sum++;
71 sym->hist[offset]++;
72
73 if (verbose >= 3)
74 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020075 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020076 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020077 (void *)(unsigned long)ip, ip - he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020078 sym->hist[offset]);
79}
Ingo Molnar8035e422009-06-06 15:19:13 +020080
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030081static int hist_entry__add(struct thread *thread, struct map *map,
82 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +020083{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030084 bool hit;
85 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
86 count, level, &hit);
87 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +020088 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020089 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +020090 return 0;
91}
92
Ingo Molnar8035e422009-06-06 15:19:13 +020093static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020094process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +020095{
96 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100097 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +020098 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030099 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300100 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200101
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200102 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200103 (void *)(offset + head),
104 (void *)(long)(event->header.size),
105 event->header.misc,
106 event->ip.pid,
107 (void *)(long)ip);
108
Ingo Molnar8035e422009-06-06 15:19:13 +0200109 if (thread == NULL) {
110 fprintf(stderr, "problem processing %d event, skipping it.\n",
111 event->header.type);
112 return -1;
113 }
114
Julia Lawallf39cdf22009-10-17 08:43:17 +0200115 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
116
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200117 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200118 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300119 sym = kernel_maps__find_symbol(ip, &map);
120 dump_printf(" ...... dso: %s\n",
121 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200122 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200123 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200124 map = thread__find_map(thread, ip);
125 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300126got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200127 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300128 sym = map->dso->find_symbol(map->dso, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200129 } else {
130 /*
131 * If this is outside of all known maps,
132 * and is a negative address, try to look it
133 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300134 * vsyscall or vdso (which executes in user-mode).
135 *
136 * XXX This is nasty, we should have a symbol list in
137 * the "[vdso]" dso, but for now lets use the old
138 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200139 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300140 if ((long long)ip < 0) {
141 map = kernel_map;
142 goto got_map;
143 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200144 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300145 dump_printf(" ...... dso: %s\n",
146 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200147 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200148 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200149 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200150 }
151
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300152 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
153 fprintf(stderr, "problem incrementing symbol count, "
154 "skipping event\n");
155 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200156 }
157 total++;
158
159 return 0;
160}
161
162static int
163process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
164{
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200165 struct map *map = map__new(&event->mmap, NULL, 0);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300166 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200167
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200168 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200169 (void *)(offset + head),
170 (void *)(long)(event->header.size),
171 event->mmap.pid,
172 (void *)(long)event->mmap.start,
173 (void *)(long)event->mmap.len,
174 (void *)(long)event->mmap.pgoff,
175 event->mmap.filename);
176
177 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200179 return 0;
180 }
181
182 thread__insert_map(thread, map);
183 total_mmap++;
184
185 return 0;
186}
187
188static int
189process_comm_event(event_t *event, unsigned long offset, unsigned long head)
190{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300191 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200192
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200193 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200194 (void *)(offset + head),
195 (void *)(long)(event->header.size),
196 event->comm.comm, event->comm.pid);
197
198 if (thread == NULL ||
199 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200200 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200201 return -1;
202 }
203 total_comm++;
204
205 return 0;
206}
207
208static int
209process_fork_event(event_t *event, unsigned long offset, unsigned long head)
210{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300211 struct thread *thread = threads__findnew(event->fork.pid);
212 struct thread *parent = threads__findnew(event->fork.ppid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200213
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200215 (void *)(offset + head),
216 (void *)(long)(event->header.size),
217 event->fork.pid, event->fork.ppid);
218
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200219 /*
220 * A thread clone will have the same PID for both
221 * parent and child.
222 */
223 if (thread == parent)
224 return 0;
225
Ingo Molnar8035e422009-06-06 15:19:13 +0200226 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200227 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200228 return -1;
229 }
230 total_fork++;
231
232 return 0;
233}
234
235static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200236process_event(event_t *event, unsigned long offset, unsigned long head)
237{
Ingo Molnar8035e422009-06-06 15:19:13 +0200238 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200239 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200240 return process_sample_event(event, offset, head);
241
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200242 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200243 return process_mmap_event(event, offset, head);
244
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200245 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200246 return process_comm_event(event, offset, head);
247
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200248 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200249 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200250 /*
251 * We dont process them right now but they are fine:
252 */
253
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200254 case PERF_RECORD_THROTTLE:
255 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200256 return 0;
257
258 default:
259 return -1;
260 }
261
262 return 0;
263}
264
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200265static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200266{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200267 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200268 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200269 static const char *prev_line;
270 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200271 unsigned int offset;
272 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200273 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200274 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200275 int ret;
276 char *c;
277
278 if (getline(&line, &line_len, file) < 0)
279 return -1;
280 if (!line)
281 return -1;
282
283 c = strchr(line, '\n');
284 if (c)
285 *c = 0;
286
287 line_ip = -1;
288 offset = 0;
289 ret = -2;
290
291 /*
292 * Strip leading spaces:
293 */
294 tmp = line;
295 while (*tmp) {
296 if (*tmp != ' ')
297 break;
298 tmp++;
299 }
300
301 if (*tmp) {
302 /*
303 * Parse hexa addresses followed by ':'
304 */
305 line_ip = strtoull(tmp, &tmp2, 16);
306 if (*tmp2 != ':')
307 line_ip = -1;
308 }
309
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200310 start = he->map->unmap_ip(he->map, sym->start);
311
Ingo Molnar0b73da32009-06-06 15:48:52 +0200312 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200313 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200314 unsigned int hits = 0;
315 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200316 const char *color;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200317 struct sym_ext *sym_ext = sym->priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200318
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200319 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200320 if (offset < len)
321 hits = sym->hist[offset];
322
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200323 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200324 path = sym_ext[offset].path;
325 percent = sym_ext[offset].percent;
326 } else if (sym->hist_sum)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200327 percent = 100.0 * hits / sym->hist_sum;
328
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200329 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200330
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200331 /*
332 * Also color the filename and line if needed, with
333 * the same color than the percentage. Don't print it
334 * twice for close colored ip with the same filename:line
335 */
336 if (path) {
337 if (!prev_line || strcmp(prev_line, path)
338 || color != prev_color) {
339 color_fprintf(stdout, color, " %s", path);
340 prev_line = path;
341 prev_color = color;
342 }
343 }
344
Ingo Molnar0b73da32009-06-06 15:48:52 +0200345 color_fprintf(stdout, color, " %7.2f", percent);
346 printf(" : ");
347 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
348 } else {
349 if (!*line)
350 printf(" :\n");
351 else
352 printf(" : %s\n", line);
353 }
354
355 return 0;
356}
357
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200358static struct rb_root root_sym_ext;
359
360static void insert_source_line(struct sym_ext *sym_ext)
361{
362 struct sym_ext *iter;
363 struct rb_node **p = &root_sym_ext.rb_node;
364 struct rb_node *parent = NULL;
365
366 while (*p != NULL) {
367 parent = *p;
368 iter = rb_entry(parent, struct sym_ext, node);
369
370 if (sym_ext->percent > iter->percent)
371 p = &(*p)->rb_left;
372 else
373 p = &(*p)->rb_right;
374 }
375
376 rb_link_node(&sym_ext->node, parent, p);
377 rb_insert_color(&sym_ext->node, &root_sym_ext);
378}
379
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200380static void free_source_line(struct symbol *sym, int len)
381{
382 struct sym_ext *sym_ext = sym->priv;
383 int i;
384
385 if (!sym_ext)
386 return;
387
388 for (i = 0; i < len; i++)
389 free(sym_ext[i].path);
390 free(sym_ext);
391
392 sym->priv = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200393 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200394}
395
396/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200397static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200398get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200399{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200400 struct symbol *sym = he->sym;
401 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200402 int i;
403 char cmd[PATH_MAX * 2];
404 struct sym_ext *sym_ext;
405
406 if (!sym->hist_sum)
407 return;
408
409 sym->priv = calloc(len, sizeof(struct sym_ext));
410 if (!sym->priv)
411 return;
412
413 sym_ext = sym->priv;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200414 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200415
416 for (i = 0; i < len; i++) {
417 char *path = NULL;
418 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000419 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200420 FILE *fp;
421
422 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
423 if (sym_ext[i].percent <= 0.5)
424 continue;
425
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200426 offset = start + i;
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200427 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200428 fp = popen(cmd, "r");
429 if (!fp)
430 continue;
431
432 if (getline(&path, &line_len, fp) < 0 || !line_len)
433 goto next;
434
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200435 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200436 if (!sym_ext[i].path)
437 goto next;
438
439 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200440 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200441
442 next:
443 pclose(fp);
444 }
445}
446
Ingo Molnar83a09442009-08-15 12:26:57 +0200447static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200448{
449 struct sym_ext *sym_ext;
450 struct rb_node *node;
451
452 printf("\nSorted summary for file %s\n", filename);
453 printf("----------------------------------------------\n\n");
454
455 if (RB_EMPTY_ROOT(&root_sym_ext)) {
456 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
457 return;
458 }
459
460 node = rb_first(&root_sym_ext);
461 while (node) {
462 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200463 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200464 char *path;
465
466 sym_ext = rb_entry(node, struct sym_ext, node);
467 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200468 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200469 path = sym_ext->path;
470
471 color_fprintf(stdout, color, " %7.2f %s", percent, path);
472 node = rb_next(node);
473 }
474}
475
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200476static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200477{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200478 struct map *map = he->map;
479 struct dso *dso = map->dso;
480 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300481 const char *filename = dso->long_name, *d_filename;
482 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200483 char command[PATH_MAX*2];
484 FILE *file;
485
486 if (!filename)
487 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200488
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200489 if (verbose)
490 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
491 __func__, filename, sym->name,
492 map->unmap_ip(map, sym->start),
493 map->unmap_ip(map, sym->end));
494
Mike Galbraith42976482009-07-02 08:09:46 +0200495 if (full_paths)
496 d_filename = filename;
497 else
498 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200499
Ingo Molnar0b73da32009-06-06 15:48:52 +0200500 len = sym->end - sym->start;
501
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200502 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200503 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200504 print_summary(filename);
505 }
506
507 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200508 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200509 printf("------------------------------------------------\n");
510
511 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300512 printf("annotating [%p] %30s : [%p] %30s\n",
513 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200514
Mike Galbraith42976482009-07-02 08:09:46 +0200515 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200516 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
517 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200518
519 if (verbose >= 3)
520 printf("doing: %s\n", command);
521
522 file = popen(command, "r");
523 if (!file)
524 return;
525
526 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200527 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200528 break;
529 }
530
531 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200532 if (print_line)
533 free_source_line(sym, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200534}
535
536static void find_annotations(void)
537{
538 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200539 int count = 0;
540
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200541 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
542 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200543
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200544 if (he->sym && he->sym->hist) {
545 annotate_sym(he);
546 count++;
547 /*
548 * Since we have a hist_entry per IP for the same
549 * symbol, free he->sym->hist to signal we already
550 * processed this symbol.
551 */
552 free(he->sym->hist);
553 he->sym->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200554
Ingo Molnar0b73da32009-06-06 15:48:52 +0200555 }
556 }
557
558 if (!count)
559 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
560}
561
Ingo Molnar8035e422009-06-06 15:19:13 +0200562static int __cmd_annotate(void)
563{
564 int ret, rc = EXIT_FAILURE;
565 unsigned long offset = 0;
566 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200567 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200568 event_t *event;
569 uint32_t size;
570 char *buf;
571
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300572 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200573
574 input = open(input_name, O_RDONLY);
575 if (input < 0) {
576 perror("failed to open file");
577 exit(-1);
578 }
579
Ingo Molnar83a09442009-08-15 12:26:57 +0200580 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200581 if (ret < 0) {
582 perror("failed to stat file");
583 exit(-1);
584 }
585
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200586 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
587 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200588 exit(-1);
589 }
590
Ingo Molnar83a09442009-08-15 12:26:57 +0200591 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200592 fprintf(stderr, "zero-sized file, nothing to do!\n");
593 exit(0);
594 }
595
596 if (load_kernel() < 0) {
597 perror("failed to load kernel symbols");
598 return EXIT_FAILURE;
599 }
600
Ingo Molnar8035e422009-06-06 15:19:13 +0200601remap:
602 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
603 MAP_SHARED, input, offset);
604 if (buf == MAP_FAILED) {
605 perror("failed to mmap file");
606 exit(-1);
607 }
608
609more:
610 event = (event_t *)(buf + head);
611
612 size = event->header.size;
613 if (!size)
614 size = 8;
615
616 if (head + event->header.size >= page_size * mmap_window) {
617 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200618 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200619
Ingo Molnar83a09442009-08-15 12:26:57 +0200620 munmap_ret = munmap(buf, page_size * mmap_window);
621 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200622
623 offset += shift;
624 head -= shift;
625 goto remap;
626 }
627
628 size = event->header.size;
629
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200630 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200631 (void *)(offset + head),
632 (void *)(long)event->header.size,
633 event->header.type);
634
635 if (!size || process_event(event, offset, head) < 0) {
636
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200637 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200638 (void *)(offset + head),
639 (void *)(long)(event->header.size),
640 event->header.type);
641
642 total_unknown++;
643
644 /*
645 * assume we lost track of the stream, check alignment, and
646 * increment a single u64 in the hope to catch on again 'soon'.
647 */
648
649 if (unlikely(head & 7))
650 head &= ~7ULL;
651
652 size = 8;
653 }
654
655 head += size;
656
Ingo Molnar83a09442009-08-15 12:26:57 +0200657 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200658 goto more;
659
660 rc = EXIT_SUCCESS;
661 close(input);
662
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200663 dump_printf(" IP events: %10ld\n", total);
664 dump_printf(" mmap events: %10ld\n", total_mmap);
665 dump_printf(" comm events: %10ld\n", total_comm);
666 dump_printf(" fork events: %10ld\n", total_fork);
667 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200668
669 if (dump_trace)
670 return 0;
671
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300672 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300673 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200674
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300675 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200676 dsos__fprintf(stdout);
677
678 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200679 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200680
681 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200682
683 return rc;
684}
685
686static const char * const annotate_usage[] = {
687 "perf annotate [<options>] <command>",
688 NULL
689};
690
691static const struct option options[] = {
692 OPT_STRING('i', "input", &input_name, "file",
693 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200694 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200695 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200696 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200697 OPT_BOOLEAN('v', "verbose", &verbose,
698 "be more verbose (show symbol address, etc)"),
699 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
700 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200701 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200702 OPT_BOOLEAN('m', "modules", &modules,
703 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200704 OPT_BOOLEAN('l', "print-line", &print_line,
705 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200706 OPT_BOOLEAN('P', "full-paths", &full_paths,
707 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200708 OPT_END()
709};
710
711static void setup_sorting(void)
712{
713 char *tmp, *tok, *str = strdup(sort_order);
714
715 for (tok = strtok_r(str, ", ", &tmp);
716 tok; tok = strtok_r(NULL, ", ", &tmp)) {
717 if (sort_dimension__add(tok) < 0) {
718 error("Unknown --sort key: `%s'", tok);
719 usage_with_options(annotate_usage, options);
720 }
721 }
722
723 free(str);
724}
725
Ingo Molnarf37a2912009-07-01 12:37:06 +0200726int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200727{
728 symbol__init();
729
730 page_size = getpagesize();
731
732 argc = parse_options(argc, argv, options, annotate_usage, 0);
733
734 setup_sorting();
735
Ingo Molnar0b73da32009-06-06 15:48:52 +0200736 if (argc) {
737 /*
738 * Special case: if there's an argument left then assume tha
739 * it's a symbol filter:
740 */
741 if (argc > 1)
742 usage_with_options(annotate_usage, options);
743
744 sym_hist_filter = argv[0];
745 }
746
747 if (!sym_hist_filter)
Ingo Molnar8035e422009-06-06 15:19:13 +0200748 usage_with_options(annotate_usage, options);
749
750 setup_pager();
751
John Kacurdd68ada2009-09-24 18:02:49 +0200752 if (field_sep && *field_sep == '.') {
753 fputs("'.' is the only non valid --field-separator argument\n",
754 stderr);
755 exit(129);
756 }
757
Ingo Molnar8035e422009-06-06 15:19:13 +0200758 return __cmd_annotate();
759}