blob: 6b13a1ecf1e774bc6f8d613a017bf696edbdb760 [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;
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -020036static bool use_modules;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020037
Ingo Molnar8035e422009-06-06 15:19:13 +020038static unsigned long page_size;
39static unsigned long mmap_window = 32;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020040const char *vmlinux_name;
Ingo Molnar8035e422009-06-06 15:19:13 +020041
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020042struct sym_hist {
43 u64 sum;
44 u64 ip[0];
45};
46
Frederic Weisbecker301406b2009-06-13 00:11:21 +020047struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020048 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020049 double percent;
50 char *path;
51};
52
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020053struct sym_priv {
54 struct sym_hist *hist;
55 struct sym_ext *ext;
56};
57
58static const char *sym_hist_filter;
59
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020060static int symbol_filter(struct map *map __used, struct symbol *sym)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020061{
Arnaldo Carvalho de Melo8f0b0372009-10-20 15:08:29 -020062 if (sym_hist_filter == NULL ||
63 strcmp(sym->name, sym_hist_filter) == 0) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020064 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020065 const int size = (sizeof(*priv->hist) +
66 (sym->end - sym->start) * sizeof(u64));
67
68 priv->hist = malloc(size);
69 if (priv->hist)
70 memset(priv->hist, 0, size);
71 return 0;
72 }
73 /*
74 * FIXME: We should really filter it out, as we don't want to go thru symbols
75 * we're not interested, and if a DSO ends up with no symbols, delete it too,
76 * but right now the kernel loading routines in symbol.c bail out if no symbols
77 * are found, fix it later.
78 */
79 return 0;
80}
Ingo Molnar8035e422009-06-06 15:19:13 +020081
Ingo Molnar8035e422009-06-06 15:19:13 +020082/*
83 * collect histogram counts
84 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100085static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020086{
87 unsigned int sym_size, offset;
88 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020089 struct sym_priv *priv;
90 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020091
92 he->count++;
93
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020094 if (!sym || !he->map)
95 return;
96
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020097 priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020098 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +020099 return;
100
101 sym_size = sym->end - sym->start;
102 offset = ip - sym->start;
103
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200104 if (verbose)
105 fprintf(stderr, "%s: ip=%Lx\n", __func__,
106 he->map->unmap_ip(he->map, ip));
107
Ingo Molnar0b73da32009-06-06 15:48:52 +0200108 if (offset >= sym_size)
109 return;
110
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200111 h = priv->hist;
112 h->sum++;
113 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200114
115 if (verbose >= 3)
116 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200117 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200118 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200119 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200120 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200121}
Ingo Molnar8035e422009-06-06 15:19:13 +0200122
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300123static int hist_entry__add(struct thread *thread, struct map *map,
124 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200125{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300126 bool hit;
127 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
128 count, level, &hit);
129 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200130 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200131 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200132 return 0;
133}
134
Ingo Molnar8035e422009-06-06 15:19:13 +0200135static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200136process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200137{
138 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000139 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200140 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300141 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300142 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200143
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200144 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200145 (void *)(offset + head),
146 (void *)(long)(event->header.size),
147 event->header.misc,
148 event->ip.pid,
149 (void *)(long)ip);
150
Ingo Molnar8035e422009-06-06 15:19:13 +0200151 if (thread == NULL) {
152 fprintf(stderr, "problem processing %d event, skipping it.\n",
153 event->header.type);
154 return -1;
155 }
156
Julia Lawallf39cdf22009-10-17 08:43:17 +0200157 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
158
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200159 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200160 level = 'k';
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200161 sym = kernel_maps__find_symbol(ip, &map, symbol_filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300162 dump_printf(" ...... dso: %s\n",
163 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200165 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200166 map = thread__find_map(thread, ip);
167 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300168got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200169 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200170 sym = map__find_symbol(map, ip, symbol_filter);
Ingo Molnar8035e422009-06-06 15:19:13 +0200171 } else {
172 /*
173 * If this is outside of all known maps,
174 * and is a negative address, try to look it
175 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300176 * vsyscall or vdso (which executes in user-mode).
177 *
178 * XXX This is nasty, we should have a symbol list in
179 * the "[vdso]" dso, but for now lets use the old
180 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200181 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300182 if ((long long)ip < 0) {
183 map = kernel_map;
184 goto got_map;
185 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200186 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300187 dump_printf(" ...... dso: %s\n",
188 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200189 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200190 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200191 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200192 }
193
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300194 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
195 fprintf(stderr, "problem incrementing symbol count, "
196 "skipping event\n");
197 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200198 }
199 total++;
200
201 return 0;
202}
203
204static int
205process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
206{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200207 struct map *map = map__new(&event->mmap, NULL, 0);
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300208 struct thread *thread = threads__findnew(event->mmap.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200209
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200211 (void *)(offset + head),
212 (void *)(long)(event->header.size),
213 event->mmap.pid,
214 (void *)(long)event->mmap.start,
215 (void *)(long)event->mmap.len,
216 (void *)(long)event->mmap.pgoff,
217 event->mmap.filename);
218
219 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200220 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200221 return 0;
222 }
223
224 thread__insert_map(thread, map);
225 total_mmap++;
226
227 return 0;
228}
229
230static int
231process_comm_event(event_t *event, unsigned long offset, unsigned long head)
232{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300233 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200234
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200235 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200236 (void *)(offset + head),
237 (void *)(long)(event->header.size),
238 event->comm.comm, event->comm.pid);
239
240 if (thread == NULL ||
241 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200242 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200243 return -1;
244 }
245 total_comm++;
246
247 return 0;
248}
249
250static int
251process_fork_event(event_t *event, unsigned long offset, unsigned long head)
252{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300253 struct thread *thread = threads__findnew(event->fork.pid);
254 struct thread *parent = threads__findnew(event->fork.ppid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200255
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200256 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200257 (void *)(offset + head),
258 (void *)(long)(event->header.size),
259 event->fork.pid, event->fork.ppid);
260
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200261 /*
262 * A thread clone will have the same PID for both
263 * parent and child.
264 */
265 if (thread == parent)
266 return 0;
267
Ingo Molnar8035e422009-06-06 15:19:13 +0200268 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200269 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200270 return -1;
271 }
272 total_fork++;
273
274 return 0;
275}
276
277static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200278process_event(event_t *event, unsigned long offset, unsigned long head)
279{
Ingo Molnar8035e422009-06-06 15:19:13 +0200280 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200281 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200282 return process_sample_event(event, offset, head);
283
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200285 return process_mmap_event(event, offset, head);
286
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200287 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200288 return process_comm_event(event, offset, head);
289
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200290 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200291 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200292 /*
293 * We dont process them right now but they are fine:
294 */
295
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200296 case PERF_RECORD_THROTTLE:
297 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200298 return 0;
299
300 default:
301 return -1;
302 }
303
304 return 0;
305}
306
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200307static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200308{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200309 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200310 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200311 static const char *prev_line;
312 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200313 unsigned int offset;
314 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200315 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200316 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200317 int ret;
318 char *c;
319
320 if (getline(&line, &line_len, file) < 0)
321 return -1;
322 if (!line)
323 return -1;
324
325 c = strchr(line, '\n');
326 if (c)
327 *c = 0;
328
329 line_ip = -1;
330 offset = 0;
331 ret = -2;
332
333 /*
334 * Strip leading spaces:
335 */
336 tmp = line;
337 while (*tmp) {
338 if (*tmp != ' ')
339 break;
340 tmp++;
341 }
342
343 if (*tmp) {
344 /*
345 * Parse hexa addresses followed by ':'
346 */
347 line_ip = strtoull(tmp, &tmp2, 16);
348 if (*tmp2 != ':')
349 line_ip = -1;
350 }
351
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200352 start = he->map->unmap_ip(he->map, sym->start);
353
Ingo Molnar0b73da32009-06-06 15:48:52 +0200354 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200355 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200356 unsigned int hits = 0;
357 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200358 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200359 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200360 struct sym_ext *sym_ext = priv->ext;
361 struct sym_hist *h = priv->hist;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200362
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200363 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200364 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200365 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200366
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200367 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200368 path = sym_ext[offset].path;
369 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200370 } else if (h->sum)
371 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200372
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200373 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200374
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200375 /*
376 * Also color the filename and line if needed, with
377 * the same color than the percentage. Don't print it
378 * twice for close colored ip with the same filename:line
379 */
380 if (path) {
381 if (!prev_line || strcmp(prev_line, path)
382 || color != prev_color) {
383 color_fprintf(stdout, color, " %s", path);
384 prev_line = path;
385 prev_color = color;
386 }
387 }
388
Ingo Molnar0b73da32009-06-06 15:48:52 +0200389 color_fprintf(stdout, color, " %7.2f", percent);
390 printf(" : ");
391 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
392 } else {
393 if (!*line)
394 printf(" :\n");
395 else
396 printf(" : %s\n", line);
397 }
398
399 return 0;
400}
401
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200402static struct rb_root root_sym_ext;
403
404static void insert_source_line(struct sym_ext *sym_ext)
405{
406 struct sym_ext *iter;
407 struct rb_node **p = &root_sym_ext.rb_node;
408 struct rb_node *parent = NULL;
409
410 while (*p != NULL) {
411 parent = *p;
412 iter = rb_entry(parent, struct sym_ext, node);
413
414 if (sym_ext->percent > iter->percent)
415 p = &(*p)->rb_left;
416 else
417 p = &(*p)->rb_right;
418 }
419
420 rb_link_node(&sym_ext->node, parent, p);
421 rb_insert_color(&sym_ext->node, &root_sym_ext);
422}
423
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200424static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200425{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200426 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200427 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200428 int i;
429
430 if (!sym_ext)
431 return;
432
433 for (i = 0; i < len; i++)
434 free(sym_ext[i].path);
435 free(sym_ext);
436
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200437 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200438 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200439}
440
441/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200442static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200443get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200444{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200445 struct symbol *sym = he->sym;
446 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200447 int i;
448 char cmd[PATH_MAX * 2];
449 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200450 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200451 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200452
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200453 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200454 return;
455
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200456 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
457 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200458 return;
459
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200460 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200461
462 for (i = 0; i < len; i++) {
463 char *path = NULL;
464 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000465 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200466 FILE *fp;
467
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200468 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200469 if (sym_ext[i].percent <= 0.5)
470 continue;
471
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200472 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200473 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200474 fp = popen(cmd, "r");
475 if (!fp)
476 continue;
477
478 if (getline(&path, &line_len, fp) < 0 || !line_len)
479 goto next;
480
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200481 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200482 if (!sym_ext[i].path)
483 goto next;
484
485 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200486 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200487
488 next:
489 pclose(fp);
490 }
491}
492
Ingo Molnar83a09442009-08-15 12:26:57 +0200493static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200494{
495 struct sym_ext *sym_ext;
496 struct rb_node *node;
497
498 printf("\nSorted summary for file %s\n", filename);
499 printf("----------------------------------------------\n\n");
500
501 if (RB_EMPTY_ROOT(&root_sym_ext)) {
502 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
503 return;
504 }
505
506 node = rb_first(&root_sym_ext);
507 while (node) {
508 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200509 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200510 char *path;
511
512 sym_ext = rb_entry(node, struct sym_ext, node);
513 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200514 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200515 path = sym_ext->path;
516
517 color_fprintf(stdout, color, " %7.2f %s", percent, path);
518 node = rb_next(node);
519 }
520}
521
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200522static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200523{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200524 struct map *map = he->map;
525 struct dso *dso = map->dso;
526 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300527 const char *filename = dso->long_name, *d_filename;
528 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200529 char command[PATH_MAX*2];
530 FILE *file;
531
532 if (!filename)
533 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200534
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200535 if (verbose)
536 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
537 __func__, filename, sym->name,
538 map->unmap_ip(map, sym->start),
539 map->unmap_ip(map, sym->end));
540
Mike Galbraith42976482009-07-02 08:09:46 +0200541 if (full_paths)
542 d_filename = filename;
543 else
544 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200545
Ingo Molnar0b73da32009-06-06 15:48:52 +0200546 len = sym->end - sym->start;
547
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200548 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200549 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200550 print_summary(filename);
551 }
552
553 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200554 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200555 printf("------------------------------------------------\n");
556
557 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300558 printf("annotating [%p] %30s : [%p] %30s\n",
559 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200560
Mike Galbraith42976482009-07-02 08:09:46 +0200561 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 -0200562 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
563 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200564
565 if (verbose >= 3)
566 printf("doing: %s\n", command);
567
568 file = popen(command, "r");
569 if (!file)
570 return;
571
572 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200573 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200574 break;
575 }
576
577 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200578 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200579 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200580}
581
582static void find_annotations(void)
583{
584 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200585
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 Melo00a192b2009-10-30 16:28:24 -0200593 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200594 if (priv->hist == NULL)
595 continue;
596
597 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200598 /*
599 * Since we have a hist_entry per IP for the same symbol, free
600 * he->sym->hist to signal we already processed this symbol.
601 */
602 free(priv->hist);
603 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200604 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200605}
606
Ingo Molnar8035e422009-06-06 15:19:13 +0200607static int __cmd_annotate(void)
608{
609 int ret, rc = EXIT_FAILURE;
610 unsigned long offset = 0;
611 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200612 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200613 event_t *event;
614 uint32_t size;
615 char *buf;
616
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300617 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200618
619 input = open(input_name, O_RDONLY);
620 if (input < 0) {
621 perror("failed to open file");
622 exit(-1);
623 }
624
Ingo Molnar83a09442009-08-15 12:26:57 +0200625 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200626 if (ret < 0) {
627 perror("failed to stat file");
628 exit(-1);
629 }
630
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200631 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
632 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200633 exit(-1);
634 }
635
Ingo Molnar83a09442009-08-15 12:26:57 +0200636 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200637 fprintf(stderr, "zero-sized file, nothing to do!\n");
638 exit(0);
639 }
640
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -0200641 if (kernel_maps__init(vmlinux_name, true, use_modules) < 0) {
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200642 pr_err("failed to create kernel maps for symbol resolution\b");
643 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200644 }
645
Ingo Molnar8035e422009-06-06 15:19:13 +0200646remap:
647 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
648 MAP_SHARED, input, offset);
649 if (buf == MAP_FAILED) {
650 perror("failed to mmap file");
651 exit(-1);
652 }
653
654more:
655 event = (event_t *)(buf + head);
656
657 size = event->header.size;
658 if (!size)
659 size = 8;
660
661 if (head + event->header.size >= page_size * mmap_window) {
662 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200663 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200664
Ingo Molnar83a09442009-08-15 12:26:57 +0200665 munmap_ret = munmap(buf, page_size * mmap_window);
666 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200667
668 offset += shift;
669 head -= shift;
670 goto remap;
671 }
672
673 size = event->header.size;
674
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200675 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200676 (void *)(offset + head),
677 (void *)(long)event->header.size,
678 event->header.type);
679
680 if (!size || process_event(event, offset, head) < 0) {
681
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200682 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200683 (void *)(offset + head),
684 (void *)(long)(event->header.size),
685 event->header.type);
686
687 total_unknown++;
688
689 /*
690 * assume we lost track of the stream, check alignment, and
691 * increment a single u64 in the hope to catch on again 'soon'.
692 */
693
694 if (unlikely(head & 7))
695 head &= ~7ULL;
696
697 size = 8;
698 }
699
700 head += size;
701
Ingo Molnar83a09442009-08-15 12:26:57 +0200702 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200703 goto more;
704
705 rc = EXIT_SUCCESS;
706 close(input);
707
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200708 dump_printf(" IP events: %10ld\n", total);
709 dump_printf(" mmap events: %10ld\n", total_mmap);
710 dump_printf(" comm events: %10ld\n", total_comm);
711 dump_printf(" fork events: %10ld\n", total_fork);
712 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200713
714 if (dump_trace)
715 return 0;
716
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300717 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300718 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200719
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300720 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200721 dsos__fprintf(stdout);
722
723 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200724 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200725
726 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200727
728 return rc;
729}
730
731static const char * const annotate_usage[] = {
732 "perf annotate [<options>] <command>",
733 NULL
734};
735
736static const struct option options[] = {
737 OPT_STRING('i', "input", &input_name, "file",
738 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200739 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200740 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200741 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200742 OPT_BOOLEAN('v', "verbose", &verbose,
743 "be more verbose (show symbol address, etc)"),
744 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
745 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200746 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -0200747 OPT_BOOLEAN('m', "modules", &use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200748 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200749 OPT_BOOLEAN('l', "print-line", &print_line,
750 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200751 OPT_BOOLEAN('P', "full-paths", &full_paths,
752 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200753 OPT_END()
754};
755
756static void setup_sorting(void)
757{
758 char *tmp, *tok, *str = strdup(sort_order);
759
760 for (tok = strtok_r(str, ", ", &tmp);
761 tok; tok = strtok_r(NULL, ", ", &tmp)) {
762 if (sort_dimension__add(tok) < 0) {
763 error("Unknown --sort key: `%s'", tok);
764 usage_with_options(annotate_usage, options);
765 }
766 }
767
768 free(str);
769}
770
Ingo Molnarf37a2912009-07-01 12:37:06 +0200771int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200772{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200773 symbol__init(sizeof(struct sym_priv));
Ingo Molnar8035e422009-06-06 15:19:13 +0200774
775 page_size = getpagesize();
776
777 argc = parse_options(argc, argv, options, annotate_usage, 0);
778
779 setup_sorting();
780
Ingo Molnar0b73da32009-06-06 15:48:52 +0200781 if (argc) {
782 /*
783 * Special case: if there's an argument left then assume tha
784 * it's a symbol filter:
785 */
786 if (argc > 1)
787 usage_with_options(annotate_usage, options);
788
789 sym_hist_filter = argv[0];
790 }
791
Ingo Molnar8035e422009-06-06 15:19:13 +0200792 setup_pager();
793
John Kacurdd68ada2009-09-24 18:02:49 +0200794 if (field_sep && *field_sep == '.') {
795 fputs("'.' is the only non valid --field-separator argument\n",
796 stderr);
797 exit(129);
798 }
799
Ingo Molnar8035e422009-06-06 15:19:13 +0200800 return __cmd_annotate();
801}