blob: 7d39bd2b19b83d4b59973f68a04d7531c0249967 [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
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020022#include "util/event.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020023#include "util/parse-options.h"
24#include "util/parse-events.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020025#include "util/thread.h"
John Kacurdd68ada2009-09-24 18:02:49 +020026#include "util/sort.h"
John Kacur3d1d07e2009-09-28 15:32:55 +020027#include "util/hist.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020028
Ingo Molnar8035e422009-06-06 15:19:13 +020029static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020030
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020031static int force;
Ingo Molnar8035e422009-06-06 15:19:13 +020032static int input;
Ingo Molnar8035e422009-06-06 15:19:13 +020033
Mike Galbraith42976482009-07-02 08:09:46 +020034static int full_paths;
35
Frederic Weisbecker301406b2009-06-13 00:11:21 +020036static int print_line;
37
Ingo Molnar8035e422009-06-06 15:19:13 +020038static unsigned long page_size;
39static unsigned long mmap_window = 32;
40
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020041struct sym_hist {
42 u64 sum;
43 u64 ip[0];
44};
45
Frederic Weisbecker301406b2009-06-13 00:11:21 +020046struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020047 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020048 double percent;
49 char *path;
50};
51
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020052struct sym_priv {
53 struct sym_hist *hist;
54 struct sym_ext *ext;
55};
56
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020057static struct symbol_conf symbol_conf = {
58 .priv_size = sizeof(struct sym_priv),
59 .try_vmlinux_path = true,
60};
61
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020062static const char *sym_hist_filter;
63
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020064static int symbol_filter(struct map *map __used, struct symbol *sym)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020065{
Arnaldo Carvalho de Melo8f0b0372009-10-20 15:08:29 -020066 if (sym_hist_filter == NULL ||
67 strcmp(sym->name, sym_hist_filter) == 0) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020068 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020069 const int size = (sizeof(*priv->hist) +
70 (sym->end - sym->start) * sizeof(u64));
71
72 priv->hist = malloc(size);
73 if (priv->hist)
74 memset(priv->hist, 0, size);
75 return 0;
76 }
77 /*
78 * FIXME: We should really filter it out, as we don't want to go thru symbols
79 * we're not interested, and if a DSO ends up with no symbols, delete it too,
80 * but right now the kernel loading routines in symbol.c bail out if no symbols
81 * are found, fix it later.
82 */
83 return 0;
84}
Ingo Molnar8035e422009-06-06 15:19:13 +020085
Ingo Molnar8035e422009-06-06 15:19:13 +020086/*
87 * collect histogram counts
88 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100089static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020090{
91 unsigned int sym_size, offset;
92 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020093 struct sym_priv *priv;
94 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020095
96 he->count++;
97
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020098 if (!sym || !he->map)
99 return;
100
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200101 priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200102 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200103 return;
104
105 sym_size = sym->end - sym->start;
106 offset = ip - sym->start;
107
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200108 if (verbose)
109 fprintf(stderr, "%s: ip=%Lx\n", __func__,
110 he->map->unmap_ip(he->map, ip));
111
Ingo Molnar0b73da32009-06-06 15:48:52 +0200112 if (offset >= sym_size)
113 return;
114
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200115 h = priv->hist;
116 h->sum++;
117 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200118
119 if (verbose >= 3)
120 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200121 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200122 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200123 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200124 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200125}
Ingo Molnar8035e422009-06-06 15:19:13 +0200126
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300127static int hist_entry__add(struct thread *thread, struct map *map,
128 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200129{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300130 bool hit;
131 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
132 count, level, &hit);
133 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200134 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200135 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200136 return 0;
137}
138
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200139static int process_sample_event(event_t *event)
Ingo Molnar8035e422009-06-06 15:19:13 +0200140{
141 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000142 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200143 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300144 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300145 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200146
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200147 dump_printf("(IP, %d): %d: %p\n", event->header.misc,
148 event->ip.pid, (void *)(long)ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200149
Ingo Molnar8035e422009-06-06 15:19:13 +0200150 if (thread == NULL) {
151 fprintf(stderr, "problem processing %d event, skipping it.\n",
152 event->header.type);
153 return -1;
154 }
155
Julia Lawallf39cdf22009-10-17 08:43:17 +0200156 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
157
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200159 level = 'k';
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200160 sym = kernel_maps__find_function(ip, &map, symbol_filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300161 dump_printf(" ...... dso: %s\n",
162 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200163 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200164 level = '.';
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200165 map = thread__find_map(thread, MAP__FUNCTION, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200166 if (map != NULL) {
167 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -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 Melo605ca4b2009-11-27 16:29:15 -0200180 if ((long long)ip < 0)
181 sym = kernel_maps__find_function(ip, &map,
182 symbol_filter);
Ingo Molnar8035e422009-06-06 15:19:13 +0200183 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300184 dump_printf(" ...... dso: %s\n",
185 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200186 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200187 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200188 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200189 }
190
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300191 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
192 fprintf(stderr, "problem incrementing symbol count, "
193 "skipping event\n");
194 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200195 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200196
197 return 0;
198}
199
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200200static int event__process(event_t *self)
Ingo Molnar8035e422009-06-06 15:19:13 +0200201{
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200202 switch (self->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203 case PERF_RECORD_SAMPLE:
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200204 return process_sample_event(self);
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200205
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200206 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200207 return event__process_mmap(self);
Ingo Molnar8035e422009-06-06 15:19:13 +0200208
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200210 return event__process_comm(self);
Ingo Molnar8035e422009-06-06 15:19:13 +0200211
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200212 case PERF_RECORD_FORK:
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200213 return event__process_task(self);
Ingo Molnar8035e422009-06-06 15:19:13 +0200214 /*
215 * We dont process them right now but they are fine:
216 */
217
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200218 case PERF_RECORD_THROTTLE:
219 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200220 return 0;
221
222 default:
223 return -1;
224 }
225
226 return 0;
227}
228
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200229static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200230{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200231 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200232 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200233 static const char *prev_line;
234 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200235 unsigned int offset;
236 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200237 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200238 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200239 int ret;
240 char *c;
241
242 if (getline(&line, &line_len, file) < 0)
243 return -1;
244 if (!line)
245 return -1;
246
247 c = strchr(line, '\n');
248 if (c)
249 *c = 0;
250
251 line_ip = -1;
252 offset = 0;
253 ret = -2;
254
255 /*
256 * Strip leading spaces:
257 */
258 tmp = line;
259 while (*tmp) {
260 if (*tmp != ' ')
261 break;
262 tmp++;
263 }
264
265 if (*tmp) {
266 /*
267 * Parse hexa addresses followed by ':'
268 */
269 line_ip = strtoull(tmp, &tmp2, 16);
270 if (*tmp2 != ':')
271 line_ip = -1;
272 }
273
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200274 start = he->map->unmap_ip(he->map, sym->start);
275
Ingo Molnar0b73da32009-06-06 15:48:52 +0200276 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200277 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200278 unsigned int hits = 0;
279 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200280 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200281 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200282 struct sym_ext *sym_ext = priv->ext;
283 struct sym_hist *h = priv->hist;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200284
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200285 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200286 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200287 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200288
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200289 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200290 path = sym_ext[offset].path;
291 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200292 } else if (h->sum)
293 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200294
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200295 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200296
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200297 /*
298 * Also color the filename and line if needed, with
299 * the same color than the percentage. Don't print it
300 * twice for close colored ip with the same filename:line
301 */
302 if (path) {
303 if (!prev_line || strcmp(prev_line, path)
304 || color != prev_color) {
305 color_fprintf(stdout, color, " %s", path);
306 prev_line = path;
307 prev_color = color;
308 }
309 }
310
Ingo Molnar0b73da32009-06-06 15:48:52 +0200311 color_fprintf(stdout, color, " %7.2f", percent);
312 printf(" : ");
313 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
314 } else {
315 if (!*line)
316 printf(" :\n");
317 else
318 printf(" : %s\n", line);
319 }
320
321 return 0;
322}
323
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200324static struct rb_root root_sym_ext;
325
326static void insert_source_line(struct sym_ext *sym_ext)
327{
328 struct sym_ext *iter;
329 struct rb_node **p = &root_sym_ext.rb_node;
330 struct rb_node *parent = NULL;
331
332 while (*p != NULL) {
333 parent = *p;
334 iter = rb_entry(parent, struct sym_ext, node);
335
336 if (sym_ext->percent > iter->percent)
337 p = &(*p)->rb_left;
338 else
339 p = &(*p)->rb_right;
340 }
341
342 rb_link_node(&sym_ext->node, parent, p);
343 rb_insert_color(&sym_ext->node, &root_sym_ext);
344}
345
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200346static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200347{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200348 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200349 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200350 int i;
351
352 if (!sym_ext)
353 return;
354
355 for (i = 0; i < len; i++)
356 free(sym_ext[i].path);
357 free(sym_ext);
358
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200359 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200360 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200361}
362
363/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200364static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200365get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200366{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200367 struct symbol *sym = he->sym;
368 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200369 int i;
370 char cmd[PATH_MAX * 2];
371 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200372 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200373 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200374
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200375 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200376 return;
377
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200378 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
379 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200380 return;
381
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200382 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200383
384 for (i = 0; i < len; i++) {
385 char *path = NULL;
386 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000387 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200388 FILE *fp;
389
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200390 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200391 if (sym_ext[i].percent <= 0.5)
392 continue;
393
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200394 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200395 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200396 fp = popen(cmd, "r");
397 if (!fp)
398 continue;
399
400 if (getline(&path, &line_len, fp) < 0 || !line_len)
401 goto next;
402
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200403 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200404 if (!sym_ext[i].path)
405 goto next;
406
407 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200408 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200409
410 next:
411 pclose(fp);
412 }
413}
414
Ingo Molnar83a09442009-08-15 12:26:57 +0200415static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200416{
417 struct sym_ext *sym_ext;
418 struct rb_node *node;
419
420 printf("\nSorted summary for file %s\n", filename);
421 printf("----------------------------------------------\n\n");
422
423 if (RB_EMPTY_ROOT(&root_sym_ext)) {
424 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
425 return;
426 }
427
428 node = rb_first(&root_sym_ext);
429 while (node) {
430 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200431 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200432 char *path;
433
434 sym_ext = rb_entry(node, struct sym_ext, node);
435 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200436 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200437 path = sym_ext->path;
438
439 color_fprintf(stdout, color, " %7.2f %s", percent, path);
440 node = rb_next(node);
441 }
442}
443
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200444static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200445{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200446 struct map *map = he->map;
447 struct dso *dso = map->dso;
448 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300449 const char *filename = dso->long_name, *d_filename;
450 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200451 char command[PATH_MAX*2];
452 FILE *file;
453
454 if (!filename)
455 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200456
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200457 if (verbose)
458 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
459 __func__, filename, sym->name,
460 map->unmap_ip(map, sym->start),
461 map->unmap_ip(map, sym->end));
462
Mike Galbraith42976482009-07-02 08:09:46 +0200463 if (full_paths)
464 d_filename = filename;
465 else
466 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200467
Ingo Molnar0b73da32009-06-06 15:48:52 +0200468 len = sym->end - sym->start;
469
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200470 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200471 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200472 print_summary(filename);
473 }
474
475 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200476 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200477 printf("------------------------------------------------\n");
478
479 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300480 printf("annotating [%p] %30s : [%p] %30s\n",
481 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200482
Mike Galbraith42976482009-07-02 08:09:46 +0200483 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 -0200484 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
485 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200486
487 if (verbose >= 3)
488 printf("doing: %s\n", command);
489
490 file = popen(command, "r");
491 if (!file)
492 return;
493
494 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200495 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200496 break;
497 }
498
499 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200500 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200501 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200502}
503
504static void find_annotations(void)
505{
506 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200507
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200508 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
509 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200510 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200511
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200512 if (he->sym == NULL)
513 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200514
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200515 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200516 if (priv->hist == NULL)
517 continue;
518
519 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200520 /*
521 * Since we have a hist_entry per IP for the same symbol, free
522 * he->sym->hist to signal we already processed this symbol.
523 */
524 free(priv->hist);
525 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200526 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200527}
528
Ingo Molnar8035e422009-06-06 15:19:13 +0200529static int __cmd_annotate(void)
530{
531 int ret, rc = EXIT_FAILURE;
532 unsigned long offset = 0;
533 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200534 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200535 event_t *event;
536 uint32_t size;
537 char *buf;
538
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300539 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200540
541 input = open(input_name, O_RDONLY);
542 if (input < 0) {
543 perror("failed to open file");
544 exit(-1);
545 }
546
Ingo Molnar83a09442009-08-15 12:26:57 +0200547 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200548 if (ret < 0) {
549 perror("failed to stat file");
550 exit(-1);
551 }
552
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200553 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
554 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200555 exit(-1);
556 }
557
Ingo Molnar83a09442009-08-15 12:26:57 +0200558 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200559 fprintf(stderr, "zero-sized file, nothing to do!\n");
560 exit(0);
561 }
562
Ingo Molnar8035e422009-06-06 15:19:13 +0200563remap:
564 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
565 MAP_SHARED, input, offset);
566 if (buf == MAP_FAILED) {
567 perror("failed to mmap file");
568 exit(-1);
569 }
570
571more:
572 event = (event_t *)(buf + head);
573
574 size = event->header.size;
575 if (!size)
576 size = 8;
577
578 if (head + event->header.size >= page_size * mmap_window) {
579 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200580 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200581
Ingo Molnar83a09442009-08-15 12:26:57 +0200582 munmap_ret = munmap(buf, page_size * mmap_window);
583 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200584
585 offset += shift;
586 head -= shift;
587 goto remap;
588 }
589
590 size = event->header.size;
591
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200592 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200593 (void *)(offset + head),
594 (void *)(long)event->header.size,
595 event->header.type);
596
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200597 if (!size || event__process(event) < 0) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200598
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200599 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200600 (void *)(offset + head),
601 (void *)(long)(event->header.size),
602 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +0200603 /*
604 * assume we lost track of the stream, check alignment, and
605 * increment a single u64 in the hope to catch on again 'soon'.
606 */
607
608 if (unlikely(head & 7))
609 head &= ~7ULL;
610
611 size = 8;
612 }
613
614 head += size;
615
Ingo Molnar83a09442009-08-15 12:26:57 +0200616 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200617 goto more;
618
619 rc = EXIT_SUCCESS;
620 close(input);
621
Ingo Molnar8035e422009-06-06 15:19:13 +0200622
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200623 if (dump_trace) {
624 event__print_totals();
Ingo Molnar8035e422009-06-06 15:19:13 +0200625 return 0;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200626 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200627
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300628 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300629 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200630
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300631 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200632 dsos__fprintf(stdout);
633
634 collapse__resort();
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200635 output__resort(event__total[0]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200636
637 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200638
639 return rc;
640}
641
642static const char * const annotate_usage[] = {
643 "perf annotate [<options>] <command>",
644 NULL
645};
646
647static const struct option options[] = {
648 OPT_STRING('i', "input", &input_name, "file",
649 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200650 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200651 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200652 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200653 OPT_BOOLEAN('v', "verbose", &verbose,
654 "be more verbose (show symbol address, etc)"),
655 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
656 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200657 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
658 "file", "vmlinux pathname"),
659 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200660 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200661 OPT_BOOLEAN('l', "print-line", &print_line,
662 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200663 OPT_BOOLEAN('P', "full-paths", &full_paths,
664 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200665 OPT_END()
666};
667
668static void setup_sorting(void)
669{
670 char *tmp, *tok, *str = strdup(sort_order);
671
672 for (tok = strtok_r(str, ", ", &tmp);
673 tok; tok = strtok_r(NULL, ", ", &tmp)) {
674 if (sort_dimension__add(tok) < 0) {
675 error("Unknown --sort key: `%s'", tok);
676 usage_with_options(annotate_usage, options);
677 }
678 }
679
680 free(str);
681}
682
Ingo Molnarf37a2912009-07-01 12:37:06 +0200683int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200684{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200685 if (symbol__init(&symbol_conf) < 0)
686 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200687
688 page_size = getpagesize();
689
690 argc = parse_options(argc, argv, options, annotate_usage, 0);
691
692 setup_sorting();
693
Ingo Molnar0b73da32009-06-06 15:48:52 +0200694 if (argc) {
695 /*
696 * Special case: if there's an argument left then assume tha
697 * it's a symbol filter:
698 */
699 if (argc > 1)
700 usage_with_options(annotate_usage, options);
701
702 sym_hist_filter = argv[0];
703 }
704
Ingo Molnar8035e422009-06-06 15:19:13 +0200705 setup_pager();
706
John Kacurdd68ada2009-09-24 18:02:49 +0200707 if (field_sep && *field_sep == '.') {
708 fputs("'.' is the only non valid --field-separator argument\n",
709 stderr);
710 exit(129);
711 }
712
Ingo Molnar8035e422009-06-06 15:19:13 +0200713 return __cmd_annotate();
714}