blob: 245692530de18368674b832487aef8d9a89b98f6 [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 Weisbecker8f288272009-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
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020040struct sym_hist {
41 u64 sum;
42 u64 ip[0];
43};
44
Frederic Weisbecker301406b2009-06-13 00:11:21 +020045struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020046 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020047 double percent;
48 char *path;
49};
50
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020051struct sym_priv {
52 struct sym_hist *hist;
53 struct sym_ext *ext;
54};
55
56static const char *sym_hist_filter;
57
58static int symbol_filter(struct map *map, struct symbol *sym)
59{
60 if (strcmp(sym->name, sym_hist_filter) == 0) {
61 struct sym_priv *priv = dso__sym_priv(map->dso, sym);
62 const int size = (sizeof(*priv->hist) +
63 (sym->end - sym->start) * sizeof(u64));
64
65 priv->hist = malloc(size);
66 if (priv->hist)
67 memset(priv->hist, 0, size);
68 return 0;
69 }
70 /*
71 * FIXME: We should really filter it out, as we don't want to go thru symbols
72 * we're not interested, and if a DSO ends up with no symbols, delete it too,
73 * but right now the kernel loading routines in symbol.c bail out if no symbols
74 * are found, fix it later.
75 */
76 return 0;
77}
Ingo Molnar8035e422009-06-06 15:19:13 +020078
Ingo Molnar8035e422009-06-06 15:19:13 +020079/*
80 * collect histogram counts
81 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100082static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020083{
84 unsigned int sym_size, offset;
85 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020086 struct sym_priv *priv;
87 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020088
89 he->count++;
90
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020091 if (!sym || !he->map)
92 return;
93
94 priv = dso__sym_priv(he->map->dso, sym);
95 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +020096 return;
97
98 sym_size = sym->end - sym->start;
99 offset = ip - sym->start;
100
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200101 if (verbose)
102 fprintf(stderr, "%s: ip=%Lx\n", __func__,
103 he->map->unmap_ip(he->map, ip));
104
Ingo Molnar0b73da32009-06-06 15:48:52 +0200105 if (offset >= sym_size)
106 return;
107
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200108 h = priv->hist;
109 h->sum++;
110 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200111
112 if (verbose >= 3)
113 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200114 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200115 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200116 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200117 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200118}
Ingo Molnar8035e422009-06-06 15:19:13 +0200119
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300120static int hist_entry__add(struct thread *thread, struct map *map,
121 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200122{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300123 bool hit;
124 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
125 count, level, &hit);
126 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200127 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200128 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200129 return 0;
130}
131
Ingo Molnar8035e422009-06-06 15:19:13 +0200132static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200133process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200134{
135 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000136 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200137 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300138 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300139 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200140
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200141 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200142 (void *)(offset + head),
143 (void *)(long)(event->header.size),
144 event->header.misc,
145 event->ip.pid,
146 (void *)(long)ip);
147
Ingo Molnar8035e422009-06-06 15:19:13 +0200148 if (thread == NULL) {
149 fprintf(stderr, "problem processing %d event, skipping it.\n",
150 event->header.type);
151 return -1;
152 }
153
Julia Lawallf39cdf22009-10-17 08:43:17 +0200154 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
155
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200156 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200157 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300158 sym = kernel_maps__find_symbol(ip, &map);
159 dump_printf(" ...... dso: %s\n",
160 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200161 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200162 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200163 map = thread__find_map(thread, ip);
164 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300165got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200166 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300167 sym = map->dso->find_symbol(map->dso, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200168 } else {
169 /*
170 * If this is outside of all known maps,
171 * and is a negative address, try to look it
172 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300173 * vsyscall or vdso (which executes in user-mode).
174 *
175 * XXX This is nasty, we should have a symbol list in
176 * the "[vdso]" dso, but for now lets use the old
177 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200178 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300179 if ((long long)ip < 0) {
180 map = kernel_map;
181 goto got_map;
182 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200183 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300184 dump_printf(" ...... dso: %s\n",
185 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200186 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200187 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200188 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200189 }
190
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300191 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
192 fprintf(stderr, "problem incrementing symbol count, "
193 "skipping event\n");
194 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200195 }
196 total++;
197
198 return 0;
199}
200
201static int
202process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
203{
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200204 struct map *map = map__new(&event->mmap, NULL, 0,
205 sizeof(struct sym_priv), symbol_filter,
206 verbose);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300207 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200208
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200210 (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) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200219 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200220 return 0;
221 }
222
223 thread__insert_map(thread, map);
224 total_mmap++;
225
226 return 0;
227}
228
229static int
230process_comm_event(event_t *event, unsigned long offset, unsigned long head)
231{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300232 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200233
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200234 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200235 (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)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200241 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200242 return -1;
243 }
244 total_comm++;
245
246 return 0;
247}
248
249static int
250process_fork_event(event_t *event, unsigned long offset, unsigned long head)
251{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300252 struct thread *thread = threads__findnew(event->fork.pid);
253 struct thread *parent = threads__findnew(event->fork.ppid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200254
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200255 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200256 (void *)(offset + head),
257 (void *)(long)(event->header.size),
258 event->fork.pid, event->fork.ppid);
259
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200260 /*
261 * A thread clone will have the same PID for both
262 * parent and child.
263 */
264 if (thread == parent)
265 return 0;
266
Ingo Molnar8035e422009-06-06 15:19:13 +0200267 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200268 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200269 return -1;
270 }
271 total_fork++;
272
273 return 0;
274}
275
276static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200277process_event(event_t *event, unsigned long offset, unsigned long head)
278{
Ingo Molnar8035e422009-06-06 15:19:13 +0200279 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200280 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200281 return process_sample_event(event, offset, head);
282
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200283 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200284 return process_mmap_event(event, offset, head);
285
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200286 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200287 return process_comm_event(event, offset, head);
288
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200289 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200290 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200291 /*
292 * We dont process them right now but they are fine:
293 */
294
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200295 case PERF_RECORD_THROTTLE:
296 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200297 return 0;
298
299 default:
300 return -1;
301 }
302
303 return 0;
304}
305
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200306static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200307{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200308 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200309 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200310 static const char *prev_line;
311 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200312 unsigned int offset;
313 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200314 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200315 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200316 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
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200351 start = he->map->unmap_ip(he->map, sym->start);
352
Ingo Molnar0b73da32009-06-06 15:48:52 +0200353 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200354 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200355 unsigned int hits = 0;
356 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200357 const char *color;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200358 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;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200361
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200362 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200363 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200364 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200365
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200366 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200367 path = sym_ext[offset].path;
368 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200369 } else if (h->sum)
370 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200371
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200372 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200373
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200374 /*
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
Ingo Molnar0b73da32009-06-06 15:48:52 +0200388 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
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200401static struct rb_root root_sym_ext;
402
403static 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
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200423static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200424{
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200425 struct sym_priv *priv = dso__sym_priv(he->map->dso, he->sym);
426 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200427 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
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200436 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200437 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200438}
439
440/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200441static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200442get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200443{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200444 struct symbol *sym = he->sym;
445 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200446 int i;
447 char cmd[PATH_MAX * 2];
448 struct sym_ext *sym_ext;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200449 struct sym_priv *priv = dso__sym_priv(he->map->dso, sym);
450 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200451
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200452 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200453 return;
454
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200455 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
456 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200457 return;
458
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200459 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200460
461 for (i = 0; i < len; i++) {
462 char *path = NULL;
463 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000464 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200465 FILE *fp;
466
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200467 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200468 if (sym_ext[i].percent <= 0.5)
469 continue;
470
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200471 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200472 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200473 fp = popen(cmd, "r");
474 if (!fp)
475 continue;
476
477 if (getline(&path, &line_len, fp) < 0 || !line_len)
478 goto next;
479
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200480 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200481 if (!sym_ext[i].path)
482 goto next;
483
484 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200485 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200486
487 next:
488 pclose(fp);
489 }
490}
491
Ingo Molnar83a09442009-08-15 12:26:57 +0200492static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200493{
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;
Ingo Molnar83a09442009-08-15 12:26:57 +0200508 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200509 char *path;
510
511 sym_ext = rb_entry(node, struct sym_ext, node);
512 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200513 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200514 path = sym_ext->path;
515
516 color_fprintf(stdout, color, " %7.2f %s", percent, path);
517 node = rb_next(node);
518 }
519}
520
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200521static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200522{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200523 struct map *map = he->map;
524 struct dso *dso = map->dso;
525 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300526 const char *filename = dso->long_name, *d_filename;
527 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200528 char command[PATH_MAX*2];
529 FILE *file;
530
531 if (!filename)
532 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200533
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200534 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
Mike Galbraith42976482009-07-02 08:09:46 +0200540 if (full_paths)
541 d_filename = filename;
542 else
543 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200544
Ingo Molnar0b73da32009-06-06 15:48:52 +0200545 len = sym->end - sym->start;
546
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200547 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200548 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200549 print_summary(filename);
550 }
551
552 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200553 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200554 printf("------------------------------------------------\n");
555
556 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300557 printf("annotating [%p] %30s : [%p] %30s\n",
558 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200559
Mike Galbraith42976482009-07-02 08:09:46 +0200560 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 -0200561 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
562 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200563
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)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200572 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200573 break;
574 }
575
576 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200577 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200578 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200579}
580
581static void find_annotations(void)
582{
583 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200584 int count = 0;
585
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200586 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
587 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200588 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200589
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200590 if (he->sym == NULL)
591 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200592
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200593 priv = dso__sym_priv(he->map->dso, he->sym);
594 if (priv->hist == NULL)
595 continue;
596
597 annotate_sym(he);
598 count++;
599 /*
600 * Since we have a hist_entry per IP for the same symbol, free
601 * he->sym->hist to signal we already processed this symbol.
602 */
603 free(priv->hist);
604 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200605 }
606
607 if (!count)
608 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
609}
610
Ingo Molnar8035e422009-06-06 15:19:13 +0200611static int __cmd_annotate(void)
612{
613 int ret, rc = EXIT_FAILURE;
614 unsigned long offset = 0;
615 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200616 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200617 event_t *event;
618 uint32_t size;
619 char *buf;
620
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300621 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200622
623 input = open(input_name, O_RDONLY);
624 if (input < 0) {
625 perror("failed to open file");
626 exit(-1);
627 }
628
Ingo Molnar83a09442009-08-15 12:26:57 +0200629 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200630 if (ret < 0) {
631 perror("failed to stat file");
632 exit(-1);
633 }
634
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200635 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
636 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200637 exit(-1);
638 }
639
Ingo Molnar83a09442009-08-15 12:26:57 +0200640 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200641 fprintf(stderr, "zero-sized file, nothing to do!\n");
642 exit(0);
643 }
644
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200645 if (load_kernel(sizeof(struct sym_priv), symbol_filter) < 0) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200646 perror("failed to load kernel symbols");
647 return EXIT_FAILURE;
648 }
649
Ingo Molnar8035e422009-06-06 15:19:13 +0200650remap:
651 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
652 MAP_SHARED, input, offset);
653 if (buf == MAP_FAILED) {
654 perror("failed to mmap file");
655 exit(-1);
656 }
657
658more:
659 event = (event_t *)(buf + head);
660
661 size = event->header.size;
662 if (!size)
663 size = 8;
664
665 if (head + event->header.size >= page_size * mmap_window) {
666 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200667 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200668
Ingo Molnar83a09442009-08-15 12:26:57 +0200669 munmap_ret = munmap(buf, page_size * mmap_window);
670 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200671
672 offset += shift;
673 head -= shift;
674 goto remap;
675 }
676
677 size = event->header.size;
678
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200679 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200680 (void *)(offset + head),
681 (void *)(long)event->header.size,
682 event->header.type);
683
684 if (!size || process_event(event, offset, head) < 0) {
685
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200686 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200687 (void *)(offset + head),
688 (void *)(long)(event->header.size),
689 event->header.type);
690
691 total_unknown++;
692
693 /*
694 * assume we lost track of the stream, check alignment, and
695 * increment a single u64 in the hope to catch on again 'soon'.
696 */
697
698 if (unlikely(head & 7))
699 head &= ~7ULL;
700
701 size = 8;
702 }
703
704 head += size;
705
Ingo Molnar83a09442009-08-15 12:26:57 +0200706 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200707 goto more;
708
709 rc = EXIT_SUCCESS;
710 close(input);
711
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200712 dump_printf(" IP events: %10ld\n", total);
713 dump_printf(" mmap events: %10ld\n", total_mmap);
714 dump_printf(" comm events: %10ld\n", total_comm);
715 dump_printf(" fork events: %10ld\n", total_fork);
716 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200717
718 if (dump_trace)
719 return 0;
720
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300721 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300722 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200723
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300724 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200725 dsos__fprintf(stdout);
726
727 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200728 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200729
730 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200731
732 return rc;
733}
734
735static const char * const annotate_usage[] = {
736 "perf annotate [<options>] <command>",
737 NULL
738};
739
740static const struct option options[] = {
741 OPT_STRING('i', "input", &input_name, "file",
742 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200743 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200744 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200745 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200746 OPT_BOOLEAN('v', "verbose", &verbose,
747 "be more verbose (show symbol address, etc)"),
748 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
749 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200750 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200751 OPT_BOOLEAN('m', "modules", &modules,
752 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200753 OPT_BOOLEAN('l', "print-line", &print_line,
754 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200755 OPT_BOOLEAN('P', "full-paths", &full_paths,
756 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200757 OPT_END()
758};
759
760static void setup_sorting(void)
761{
762 char *tmp, *tok, *str = strdup(sort_order);
763
764 for (tok = strtok_r(str, ", ", &tmp);
765 tok; tok = strtok_r(NULL, ", ", &tmp)) {
766 if (sort_dimension__add(tok) < 0) {
767 error("Unknown --sort key: `%s'", tok);
768 usage_with_options(annotate_usage, options);
769 }
770 }
771
772 free(str);
773}
774
Ingo Molnarf37a2912009-07-01 12:37:06 +0200775int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200776{
777 symbol__init();
778
779 page_size = getpagesize();
780
781 argc = parse_options(argc, argv, options, annotate_usage, 0);
782
783 setup_sorting();
784
Ingo Molnar0b73da32009-06-06 15:48:52 +0200785 if (argc) {
786 /*
787 * Special case: if there's an argument left then assume tha
788 * it's a symbol filter:
789 */
790 if (argc > 1)
791 usage_with_options(annotate_usage, options);
792
793 sym_hist_filter = argv[0];
794 }
795
796 if (!sym_hist_filter)
Ingo Molnar8035e422009-06-06 15:19:13 +0200797 usage_with_options(annotate_usage, options);
798
799 setup_pager();
800
John Kacurdd68ada2009-09-24 18:02:49 +0200801 if (field_sep && *field_sep == '.') {
802 fputs("'.' is the only non valid --field-separator argument\n",
803 stderr);
804 exit(129);
805 }
806
Ingo Molnar8035e422009-06-06 15:19:13 +0200807 return __cmd_annotate();
808}