blob: 8688bfee42ab7fd4c971974f7a67137d98a5a41e [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{
Arnaldo Carvalho de Melo8f0b0372009-10-20 15:08:29 -020060 if (sym_hist_filter == NULL ||
61 strcmp(sym->name, sym_hist_filter) == 0) {
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020062 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}
Ingo Molnar8035e422009-06-06 15:19:13 +020079
Ingo Molnar8035e422009-06-06 15:19:13 +020080/*
81 * collect histogram counts
82 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100083static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020084{
85 unsigned int sym_size, offset;
86 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020087 struct sym_priv *priv;
88 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020089
90 he->count++;
91
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020092 if (!sym || !he->map)
93 return;
94
95 priv = dso__sym_priv(he->map->dso, sym);
96 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +020097 return;
98
99 sym_size = sym->end - sym->start;
100 offset = ip - sym->start;
101
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200102 if (verbose)
103 fprintf(stderr, "%s: ip=%Lx\n", __func__,
104 he->map->unmap_ip(he->map, ip));
105
Ingo Molnar0b73da32009-06-06 15:48:52 +0200106 if (offset >= sym_size)
107 return;
108
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200109 h = priv->hist;
110 h->sum++;
111 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200112
113 if (verbose >= 3)
114 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200115 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200116 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200117 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200118 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200119}
Ingo Molnar8035e422009-06-06 15:19:13 +0200120
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300121static int hist_entry__add(struct thread *thread, struct map *map,
122 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200123{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300124 bool hit;
125 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
126 count, level, &hit);
127 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200128 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200129 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200130 return 0;
131}
132
Ingo Molnar8035e422009-06-06 15:19:13 +0200133static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200134process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200135{
136 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000137 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200138 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300139 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300140 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200141
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200142 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200143 (void *)(offset + head),
144 (void *)(long)(event->header.size),
145 event->header.misc,
146 event->ip.pid,
147 (void *)(long)ip);
148
Ingo Molnar8035e422009-06-06 15:19:13 +0200149 if (thread == NULL) {
150 fprintf(stderr, "problem processing %d event, skipping it.\n",
151 event->header.type);
152 return -1;
153 }
154
Julia Lawallf39cdf22009-10-17 08:43:17 +0200155 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
156
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200158 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300159 sym = kernel_maps__find_symbol(ip, &map);
160 dump_printf(" ...... dso: %s\n",
161 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200163 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200164 map = thread__find_map(thread, ip);
165 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300166got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200167 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200168 sym = map__find_symbol(map, ip, symbol_filter);
Ingo Molnar8035e422009-06-06 15:19:13 +0200169 } else {
170 /*
171 * If this is outside of all known maps,
172 * and is a negative address, try to look it
173 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300174 * vsyscall or vdso (which executes in user-mode).
175 *
176 * XXX This is nasty, we should have a symbol list in
177 * the "[vdso]" dso, but for now lets use the old
178 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200179 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300180 if ((long long)ip < 0) {
181 map = kernel_map;
182 goto got_map;
183 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200184 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300185 dump_printf(" ...... dso: %s\n",
186 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200187 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200188 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200189 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200190 }
191
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300192 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
193 fprintf(stderr, "problem incrementing symbol count, "
194 "skipping event\n");
195 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200196 }
197 total++;
198
199 return 0;
200}
201
202static int
203process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
204{
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200205 struct map *map = map__new(&event->mmap, NULL, 0,
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200206 sizeof(struct sym_priv));
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
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200585 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
586 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200587 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200588
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200589 if (he->sym == NULL)
590 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200591
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200592 priv = dso__sym_priv(he->map->dso, he->sym);
593 if (priv->hist == NULL)
594 continue;
595
596 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200597 /*
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;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200603 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200604}
605
Ingo Molnar8035e422009-06-06 15:19:13 +0200606static int __cmd_annotate(void)
607{
608 int ret, rc = EXIT_FAILURE;
609 unsigned long offset = 0;
610 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200611 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200612 event_t *event;
613 uint32_t size;
614 char *buf;
615
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300616 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200617
618 input = open(input_name, O_RDONLY);
619 if (input < 0) {
620 perror("failed to open file");
621 exit(-1);
622 }
623
Ingo Molnar83a09442009-08-15 12:26:57 +0200624 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200625 if (ret < 0) {
626 perror("failed to stat file");
627 exit(-1);
628 }
629
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200630 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);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200632 exit(-1);
633 }
634
Ingo Molnar83a09442009-08-15 12:26:57 +0200635 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200636 fprintf(stderr, "zero-sized file, nothing to do!\n");
637 exit(0);
638 }
639
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200640 if (load_kernel(sizeof(struct sym_priv), symbol_filter) < 0) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200641 perror("failed to load kernel symbols");
642 return EXIT_FAILURE;
643 }
644
Ingo Molnar8035e422009-06-06 15:19:13 +0200645remap:
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
653more:
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);
Ingo Molnar83a09442009-08-15 12:26:57 +0200662 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200663
Ingo Molnar83a09442009-08-15 12:26:57 +0200664 munmap_ret = munmap(buf, page_size * mmap_window);
665 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200666
667 offset += shift;
668 head -= shift;
669 goto remap;
670 }
671
672 size = event->header.size;
673
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200674 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200675 (void *)(offset + head),
676 (void *)(long)event->header.size,
677 event->header.type);
678
679 if (!size || process_event(event, offset, head) < 0) {
680
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200681 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200682 (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
Ingo Molnar83a09442009-08-15 12:26:57 +0200701 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200702 goto more;
703
704 rc = EXIT_SUCCESS;
705 close(input);
706
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200707 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);
Ingo Molnar8035e422009-06-06 15:19:13 +0200712
713 if (dump_trace)
714 return 0;
715
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300716 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300717 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200718
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300719 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200720 dsos__fprintf(stdout);
721
722 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200723 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200724
725 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200726
727 return rc;
728}
729
730static const char * const annotate_usage[] = {
731 "perf annotate [<options>] <command>",
732 NULL
733};
734
735static const struct option options[] = {
736 OPT_STRING('i', "input", &input_name, "file",
737 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200738 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200739 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200740 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200741 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"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200745 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200746 OPT_BOOLEAN('m', "modules", &modules,
747 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200748 OPT_BOOLEAN('l', "print-line", &print_line,
749 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200750 OPT_BOOLEAN('P', "full-paths", &full_paths,
751 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200752 OPT_END()
753};
754
755static 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
Ingo Molnarf37a2912009-07-01 12:37:06 +0200770int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200771{
772 symbol__init();
773
774 page_size = getpagesize();
775
776 argc = parse_options(argc, argv, options, annotate_usage, 0);
777
778 setup_sorting();
779
Ingo Molnar0b73da32009-06-06 15:48:52 +0200780 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
Ingo Molnar8035e422009-06-06 15:19:13 +0200791 setup_pager();
792
John Kacurdd68ada2009-09-24 18:02:49 +0200793 if (field_sep && *field_sep == '.') {
794 fputs("'.' is the only non valid --field-separator argument\n",
795 stderr);
796 exit(129);
797 }
798
Ingo Molnar8035e422009-06-06 15:19:13 +0200799 return __cmd_annotate();
800}