blob: 77d50a6d68027cf9a176882ff4a28dc720a95711 [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
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020058static int symbol_filter(struct map *map __used, struct symbol *sym)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020059{
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 Melo00a192b2009-10-30 16:28:24 -020062 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020063 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
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020095 priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020096 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 Melo00a192b2009-10-30 16:28:24 -0200205 struct map *map = map__new(&event->mmap, NULL, 0);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300206 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200207
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200208 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200209 (void *)(offset + head),
210 (void *)(long)(event->header.size),
211 event->mmap.pid,
212 (void *)(long)event->mmap.start,
213 (void *)(long)event->mmap.len,
214 (void *)(long)event->mmap.pgoff,
215 event->mmap.filename);
216
217 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200218 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200219 return 0;
220 }
221
222 thread__insert_map(thread, map);
223 total_mmap++;
224
225 return 0;
226}
227
228static int
229process_comm_event(event_t *event, unsigned long offset, unsigned long head)
230{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300231 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200232
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200233 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200234 (void *)(offset + head),
235 (void *)(long)(event->header.size),
236 event->comm.comm, event->comm.pid);
237
238 if (thread == NULL ||
239 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200240 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200241 return -1;
242 }
243 total_comm++;
244
245 return 0;
246}
247
248static int
249process_fork_event(event_t *event, unsigned long offset, unsigned long head)
250{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300251 struct thread *thread = threads__findnew(event->fork.pid);
252 struct thread *parent = threads__findnew(event->fork.ppid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200253
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200254 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200255 (void *)(offset + head),
256 (void *)(long)(event->header.size),
257 event->fork.pid, event->fork.ppid);
258
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200259 /*
260 * A thread clone will have the same PID for both
261 * parent and child.
262 */
263 if (thread == parent)
264 return 0;
265
Ingo Molnar8035e422009-06-06 15:19:13 +0200266 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200267 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200268 return -1;
269 }
270 total_fork++;
271
272 return 0;
273}
274
275static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200276process_event(event_t *event, unsigned long offset, unsigned long head)
277{
Ingo Molnar8035e422009-06-06 15:19:13 +0200278 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200280 return process_sample_event(event, offset, head);
281
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200282 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200283 return process_mmap_event(event, offset, head);
284
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200285 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200286 return process_comm_event(event, offset, head);
287
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200288 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200289 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200290 /*
291 * We dont process them right now but they are fine:
292 */
293
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200294 case PERF_RECORD_THROTTLE:
295 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200296 return 0;
297
298 default:
299 return -1;
300 }
301
302 return 0;
303}
304
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200305static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200306{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200307 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200308 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200309 static const char *prev_line;
310 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200311 unsigned int offset;
312 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200313 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200314 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200315 int ret;
316 char *c;
317
318 if (getline(&line, &line_len, file) < 0)
319 return -1;
320 if (!line)
321 return -1;
322
323 c = strchr(line, '\n');
324 if (c)
325 *c = 0;
326
327 line_ip = -1;
328 offset = 0;
329 ret = -2;
330
331 /*
332 * Strip leading spaces:
333 */
334 tmp = line;
335 while (*tmp) {
336 if (*tmp != ' ')
337 break;
338 tmp++;
339 }
340
341 if (*tmp) {
342 /*
343 * Parse hexa addresses followed by ':'
344 */
345 line_ip = strtoull(tmp, &tmp2, 16);
346 if (*tmp2 != ':')
347 line_ip = -1;
348 }
349
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200350 start = he->map->unmap_ip(he->map, sym->start);
351
Ingo Molnar0b73da32009-06-06 15:48:52 +0200352 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200353 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200354 unsigned int hits = 0;
355 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200356 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200357 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200358 struct sym_ext *sym_ext = priv->ext;
359 struct sym_hist *h = priv->hist;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200360
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200361 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200362 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200363 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200364
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200365 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200366 path = sym_ext[offset].path;
367 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200368 } else if (h->sum)
369 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200370
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200371 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200372
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200373 /*
374 * Also color the filename and line if needed, with
375 * the same color than the percentage. Don't print it
376 * twice for close colored ip with the same filename:line
377 */
378 if (path) {
379 if (!prev_line || strcmp(prev_line, path)
380 || color != prev_color) {
381 color_fprintf(stdout, color, " %s", path);
382 prev_line = path;
383 prev_color = color;
384 }
385 }
386
Ingo Molnar0b73da32009-06-06 15:48:52 +0200387 color_fprintf(stdout, color, " %7.2f", percent);
388 printf(" : ");
389 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
390 } else {
391 if (!*line)
392 printf(" :\n");
393 else
394 printf(" : %s\n", line);
395 }
396
397 return 0;
398}
399
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200400static struct rb_root root_sym_ext;
401
402static void insert_source_line(struct sym_ext *sym_ext)
403{
404 struct sym_ext *iter;
405 struct rb_node **p = &root_sym_ext.rb_node;
406 struct rb_node *parent = NULL;
407
408 while (*p != NULL) {
409 parent = *p;
410 iter = rb_entry(parent, struct sym_ext, node);
411
412 if (sym_ext->percent > iter->percent)
413 p = &(*p)->rb_left;
414 else
415 p = &(*p)->rb_right;
416 }
417
418 rb_link_node(&sym_ext->node, parent, p);
419 rb_insert_color(&sym_ext->node, &root_sym_ext);
420}
421
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200422static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200423{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200424 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200425 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200426 int i;
427
428 if (!sym_ext)
429 return;
430
431 for (i = 0; i < len; i++)
432 free(sym_ext[i].path);
433 free(sym_ext);
434
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200435 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200436 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200437}
438
439/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200440static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200441get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200442{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200443 struct symbol *sym = he->sym;
444 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200445 int i;
446 char cmd[PATH_MAX * 2];
447 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200448 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200449 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200450
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200451 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200452 return;
453
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200454 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
455 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200456 return;
457
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200458 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200459
460 for (i = 0; i < len; i++) {
461 char *path = NULL;
462 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000463 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200464 FILE *fp;
465
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200466 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200467 if (sym_ext[i].percent <= 0.5)
468 continue;
469
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200470 offset = start + i;
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200471 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200472 fp = popen(cmd, "r");
473 if (!fp)
474 continue;
475
476 if (getline(&path, &line_len, fp) < 0 || !line_len)
477 goto next;
478
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200479 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200480 if (!sym_ext[i].path)
481 goto next;
482
483 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200484 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200485
486 next:
487 pclose(fp);
488 }
489}
490
Ingo Molnar83a09442009-08-15 12:26:57 +0200491static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200492{
493 struct sym_ext *sym_ext;
494 struct rb_node *node;
495
496 printf("\nSorted summary for file %s\n", filename);
497 printf("----------------------------------------------\n\n");
498
499 if (RB_EMPTY_ROOT(&root_sym_ext)) {
500 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
501 return;
502 }
503
504 node = rb_first(&root_sym_ext);
505 while (node) {
506 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200507 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200508 char *path;
509
510 sym_ext = rb_entry(node, struct sym_ext, node);
511 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200512 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200513 path = sym_ext->path;
514
515 color_fprintf(stdout, color, " %7.2f %s", percent, path);
516 node = rb_next(node);
517 }
518}
519
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200520static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200521{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200522 struct map *map = he->map;
523 struct dso *dso = map->dso;
524 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300525 const char *filename = dso->long_name, *d_filename;
526 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200527 char command[PATH_MAX*2];
528 FILE *file;
529
530 if (!filename)
531 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200532
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200533 if (verbose)
534 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
535 __func__, filename, sym->name,
536 map->unmap_ip(map, sym->start),
537 map->unmap_ip(map, sym->end));
538
Mike Galbraith42976482009-07-02 08:09:46 +0200539 if (full_paths)
540 d_filename = filename;
541 else
542 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200543
Ingo Molnar0b73da32009-06-06 15:48:52 +0200544 len = sym->end - sym->start;
545
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200546 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200547 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200548 print_summary(filename);
549 }
550
551 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200552 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200553 printf("------------------------------------------------\n");
554
555 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300556 printf("annotating [%p] %30s : [%p] %30s\n",
557 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200558
Mike Galbraith42976482009-07-02 08:09:46 +0200559 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 -0200560 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
561 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200562
563 if (verbose >= 3)
564 printf("doing: %s\n", command);
565
566 file = popen(command, "r");
567 if (!file)
568 return;
569
570 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200571 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200572 break;
573 }
574
575 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200576 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200577 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200578}
579
580static void find_annotations(void)
581{
582 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200583
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200584 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
585 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200586 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200587
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200588 if (he->sym == NULL)
589 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200590
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200591 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200592 if (priv->hist == NULL)
593 continue;
594
595 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200596 /*
597 * Since we have a hist_entry per IP for the same symbol, free
598 * he->sym->hist to signal we already processed this symbol.
599 */
600 free(priv->hist);
601 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200602 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200603}
604
Ingo Molnar8035e422009-06-06 15:19:13 +0200605static int __cmd_annotate(void)
606{
607 int ret, rc = EXIT_FAILURE;
608 unsigned long offset = 0;
609 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200610 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200611 event_t *event;
612 uint32_t size;
613 char *buf;
614
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300615 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200616
617 input = open(input_name, O_RDONLY);
618 if (input < 0) {
619 perror("failed to open file");
620 exit(-1);
621 }
622
Ingo Molnar83a09442009-08-15 12:26:57 +0200623 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200624 if (ret < 0) {
625 perror("failed to stat file");
626 exit(-1);
627 }
628
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200629 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
630 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200631 exit(-1);
632 }
633
Ingo Molnar83a09442009-08-15 12:26:57 +0200634 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200635 fprintf(stderr, "zero-sized file, nothing to do!\n");
636 exit(0);
637 }
638
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200639 if (load_kernel(symbol_filter) < 0) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200640 perror("failed to load kernel symbols");
641 return EXIT_FAILURE;
642 }
643
Ingo Molnar8035e422009-06-06 15:19:13 +0200644remap:
645 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
646 MAP_SHARED, input, offset);
647 if (buf == MAP_FAILED) {
648 perror("failed to mmap file");
649 exit(-1);
650 }
651
652more:
653 event = (event_t *)(buf + head);
654
655 size = event->header.size;
656 if (!size)
657 size = 8;
658
659 if (head + event->header.size >= page_size * mmap_window) {
660 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200661 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200662
Ingo Molnar83a09442009-08-15 12:26:57 +0200663 munmap_ret = munmap(buf, page_size * mmap_window);
664 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200665
666 offset += shift;
667 head -= shift;
668 goto remap;
669 }
670
671 size = event->header.size;
672
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200673 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200674 (void *)(offset + head),
675 (void *)(long)event->header.size,
676 event->header.type);
677
678 if (!size || process_event(event, offset, head) < 0) {
679
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200680 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200681 (void *)(offset + head),
682 (void *)(long)(event->header.size),
683 event->header.type);
684
685 total_unknown++;
686
687 /*
688 * assume we lost track of the stream, check alignment, and
689 * increment a single u64 in the hope to catch on again 'soon'.
690 */
691
692 if (unlikely(head & 7))
693 head &= ~7ULL;
694
695 size = 8;
696 }
697
698 head += size;
699
Ingo Molnar83a09442009-08-15 12:26:57 +0200700 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200701 goto more;
702
703 rc = EXIT_SUCCESS;
704 close(input);
705
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200706 dump_printf(" IP events: %10ld\n", total);
707 dump_printf(" mmap events: %10ld\n", total_mmap);
708 dump_printf(" comm events: %10ld\n", total_comm);
709 dump_printf(" fork events: %10ld\n", total_fork);
710 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200711
712 if (dump_trace)
713 return 0;
714
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300715 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300716 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200717
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300718 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200719 dsos__fprintf(stdout);
720
721 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200722 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200723
724 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200725
726 return rc;
727}
728
729static const char * const annotate_usage[] = {
730 "perf annotate [<options>] <command>",
731 NULL
732};
733
734static const struct option options[] = {
735 OPT_STRING('i', "input", &input_name, "file",
736 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200737 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200738 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200739 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200740 OPT_BOOLEAN('v', "verbose", &verbose,
741 "be more verbose (show symbol address, etc)"),
742 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
743 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200744 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200745 OPT_BOOLEAN('m', "modules", &modules,
746 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200747 OPT_BOOLEAN('l', "print-line", &print_line,
748 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200749 OPT_BOOLEAN('P', "full-paths", &full_paths,
750 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200751 OPT_END()
752};
753
754static void setup_sorting(void)
755{
756 char *tmp, *tok, *str = strdup(sort_order);
757
758 for (tok = strtok_r(str, ", ", &tmp);
759 tok; tok = strtok_r(NULL, ", ", &tmp)) {
760 if (sort_dimension__add(tok) < 0) {
761 error("Unknown --sort key: `%s'", tok);
762 usage_with_options(annotate_usage, options);
763 }
764 }
765
766 free(str);
767}
768
Ingo Molnarf37a2912009-07-01 12:37:06 +0200769int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200770{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200771 symbol__init(sizeof(struct sym_priv));
Ingo Molnar8035e422009-06-06 15:19:13 +0200772
773 page_size = getpagesize();
774
775 argc = parse_options(argc, argv, options, annotate_usage, 0);
776
777 setup_sorting();
778
Ingo Molnar0b73da32009-06-06 15:48:52 +0200779 if (argc) {
780 /*
781 * Special case: if there's an argument left then assume tha
782 * it's a symbol filter:
783 */
784 if (argc > 1)
785 usage_with_options(annotate_usage, options);
786
787 sym_hist_filter = argv[0];
788 }
789
Ingo Molnar8035e422009-06-06 15:19:13 +0200790 setup_pager();
791
John Kacurdd68ada2009-09-24 18:02:49 +0200792 if (field_sep && *field_sep == '.') {
793 fputs("'.' is the only non valid --field-separator argument\n",
794 stderr);
795 exit(129);
796 }
797
Ingo Molnar8035e422009-06-06 15:19:13 +0200798 return __cmd_annotate();
799}