blob: 6ad7148451c5bcc32995c08e75a920d0898e4c7e [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 Weisbecker8f28827a2009-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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028#include "util/session.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020029
Ingo Molnar8035e422009-06-06 15:19:13 +020030static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020031
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020032static int force;
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
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020038struct sym_hist {
39 u64 sum;
40 u64 ip[0];
41};
42
Frederic Weisbecker301406b2009-06-13 00:11:21 +020043struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020044 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020045 double percent;
46 char *path;
47};
48
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020049struct sym_priv {
50 struct sym_hist *hist;
51 struct sym_ext *ext;
52};
53
54static const char *sym_hist_filter;
55
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030056static int sym__alloc_hist(struct symbol *self)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020057{
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030058 struct sym_priv *priv = symbol__priv(self);
59 const int size = (sizeof(*priv->hist) +
60 (self->end - self->start) * sizeof(u64));
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020061
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030062 priv->hist = zalloc(size);
63 return priv->hist == NULL ? -1 : 0;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020064}
Ingo Molnar8035e422009-06-06 15:19:13 +020065
Ingo Molnar8035e422009-06-06 15:19:13 +020066/*
67 * collect histogram counts
68 */
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030069static int annotate__hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020070{
71 unsigned int sym_size, offset;
72 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020073 struct sym_priv *priv;
74 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020075
76 he->count++;
77
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020078 if (!sym || !he->map)
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030079 return 0;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020080
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020081 priv = symbol__priv(sym);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030082 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
83 return -ENOMEM;
Ingo Molnar0b73da32009-06-06 15:48:52 +020084
85 sym_size = sym->end - sym->start;
86 offset = ip - sym->start;
87
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -020088 pr_debug3("%s: ip=%#Lx\n", __func__, he->map->unmap_ip(he->map, ip));
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020089
Ingo Molnar0b73da32009-06-06 15:48:52 +020090 if (offset >= sym_size)
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030091 return 0;
Ingo Molnar0b73da32009-06-06 15:48:52 +020092
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020093 h = priv->hist;
94 h->sum++;
95 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +020096
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -020097 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->sym->start,
98 he->sym->name, ip, ip - he->sym->start, h->ip[offset]);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030099 return 0;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200100}
Ingo Molnar8035e422009-06-06 15:19:13 +0200101
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200102static int perf_session__add_hist_entry(struct perf_session *self,
103 struct addr_location *al, u64 count)
Ingo Molnar8035e422009-06-06 15:19:13 +0200104{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300105 bool hit;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300106 struct hist_entry *he;
107
108 if (sym_hist_filter != NULL &&
109 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
110 /* We're only interested in a symbol named sym_hist_filter */
111 if (al->sym != NULL) {
112 rb_erase(&al->sym->rb_node,
113 &al->map->dso->symbols[al->map->type]);
114 symbol__delete(al->sym);
115 }
116 return 0;
117 }
118
Eric B Munsond403d0a2010-03-05 12:51:06 -0300119 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300120 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200121 return -ENOMEM;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300122
123 return annotate__hist_hit(he, al->addr);
Ingo Molnar8035e422009-06-06 15:19:13 +0200124}
125
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200126static int process_sample_event(event_t *event, struct perf_session *session)
Ingo Molnar8035e422009-06-06 15:19:13 +0200127{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200128 struct addr_location al;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200129
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200130 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
131 event->ip.pid, event->ip.ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200132
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300133 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200134 pr_warning("problem processing %d event, skipping it.\n",
135 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +0200136 return -1;
137 }
138
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200139 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200140 pr_warning("problem incrementing symbol count, "
141 "skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300142 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200143 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200144
145 return 0;
146}
147
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300148struct objdump_line {
149 struct list_head node;
150 s64 offset;
151 char *line;
152};
153
154static struct objdump_line *objdump_line__new(s64 offset, char *line)
155{
156 struct objdump_line *self = malloc(sizeof(*self));
157
158 if (self != NULL) {
159 self->offset = offset;
160 self->line = line;
161 }
162
163 return self;
164}
165
166static void objdump_line__free(struct objdump_line *self)
167{
168 free(self->line);
169 free(self);
170}
171
172static void objdump__add_line(struct list_head *head, struct objdump_line *line)
173{
174 list_add_tail(&line->node, head);
175}
176
177static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
178 struct objdump_line *pos)
179{
180 list_for_each_entry_continue(pos, head, node)
181 if (pos->offset >= 0)
182 return pos;
183
184 return NULL;
185}
186
187static int parse_line(FILE *file, struct hist_entry *he,
188 struct list_head *head)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200189{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200190 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300191 struct objdump_line *objdump_line;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200192 char *line = NULL, *tmp, *tmp2;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200193 size_t line_len;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300194 s64 line_ip, offset = -1;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200195 char *c;
196
197 if (getline(&line, &line_len, file) < 0)
198 return -1;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300199
Ingo Molnar0b73da32009-06-06 15:48:52 +0200200 if (!line)
201 return -1;
202
203 c = strchr(line, '\n');
204 if (c)
205 *c = 0;
206
207 line_ip = -1;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200208
209 /*
210 * Strip leading spaces:
211 */
212 tmp = line;
213 while (*tmp) {
214 if (*tmp != ' ')
215 break;
216 tmp++;
217 }
218
219 if (*tmp) {
220 /*
221 * Parse hexa addresses followed by ':'
222 */
223 line_ip = strtoull(tmp, &tmp2, 16);
224 if (*tmp2 != ':')
225 line_ip = -1;
226 }
227
228 if (line_ip != -1) {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300229 u64 start = map__rip_2objdump(he->map, sym->start);
230 offset = line_ip - start;
231 }
232
233 objdump_line = objdump_line__new(offset, line);
234 if (objdump_line == NULL) {
235 free(line);
236 return -1;
237 }
238 objdump__add_line(head, objdump_line);
239
240 return 0;
241}
242
243static int objdump_line__print(struct objdump_line *self,
244 struct list_head *head,
245 struct hist_entry *he, u64 len)
246{
247 struct symbol *sym = he->sym;
248 static const char *prev_line;
249 static const char *prev_color;
250
251 if (self->offset != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200252 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200253 unsigned int hits = 0;
254 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200255 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200256 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200257 struct sym_ext *sym_ext = priv->ext;
258 struct sym_hist *h = priv->hist;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300259 s64 offset = self->offset;
260 struct objdump_line *next = objdump__get_next_ip_line(head, self);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200261
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300262 while (offset < (s64)len &&
263 (next == NULL || offset < next->offset)) {
264 if (sym_ext) {
265 if (path == NULL)
266 path = sym_ext[offset].path;
267 percent += sym_ext[offset].percent;
268 } else
269 hits += h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200270
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300271 ++offset;
272 }
273
274 if (sym_ext == NULL && h->sum)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200275 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200276
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200277 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200278
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200279 /*
280 * Also color the filename and line if needed, with
281 * the same color than the percentage. Don't print it
282 * twice for close colored ip with the same filename:line
283 */
284 if (path) {
285 if (!prev_line || strcmp(prev_line, path)
286 || color != prev_color) {
287 color_fprintf(stdout, color, " %s", path);
288 prev_line = path;
289 prev_color = color;
290 }
291 }
292
Ingo Molnar0b73da32009-06-06 15:48:52 +0200293 color_fprintf(stdout, color, " %7.2f", percent);
294 printf(" : ");
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300295 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200296 } else {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300297 if (!*self->line)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200298 printf(" :\n");
299 else
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300300 printf(" : %s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200301 }
302
303 return 0;
304}
305
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200306static struct rb_root root_sym_ext;
307
308static void insert_source_line(struct sym_ext *sym_ext)
309{
310 struct sym_ext *iter;
311 struct rb_node **p = &root_sym_ext.rb_node;
312 struct rb_node *parent = NULL;
313
314 while (*p != NULL) {
315 parent = *p;
316 iter = rb_entry(parent, struct sym_ext, node);
317
318 if (sym_ext->percent > iter->percent)
319 p = &(*p)->rb_left;
320 else
321 p = &(*p)->rb_right;
322 }
323
324 rb_link_node(&sym_ext->node, parent, p);
325 rb_insert_color(&sym_ext->node, &root_sym_ext);
326}
327
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200328static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200329{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200330 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200331 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200332 int i;
333
334 if (!sym_ext)
335 return;
336
337 for (i = 0; i < len; i++)
338 free(sym_ext[i].path);
339 free(sym_ext);
340
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200341 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200342 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200343}
344
345/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200346static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200347get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200348{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200349 struct symbol *sym = he->sym;
350 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200351 int i;
352 char cmd[PATH_MAX * 2];
353 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200354 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200355 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200356
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200357 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200358 return;
359
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200360 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
361 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200362 return;
363
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200364 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200365
366 for (i = 0; i < len; i++) {
367 char *path = NULL;
368 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000369 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200370 FILE *fp;
371
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200372 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200373 if (sym_ext[i].percent <= 0.5)
374 continue;
375
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200376 offset = start + i;
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200377 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200378 fp = popen(cmd, "r");
379 if (!fp)
380 continue;
381
382 if (getline(&path, &line_len, fp) < 0 || !line_len)
383 goto next;
384
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200385 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200386 if (!sym_ext[i].path)
387 goto next;
388
389 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200390 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200391
392 next:
393 pclose(fp);
394 }
395}
396
Ingo Molnar83a09442009-08-15 12:26:57 +0200397static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200398{
399 struct sym_ext *sym_ext;
400 struct rb_node *node;
401
402 printf("\nSorted summary for file %s\n", filename);
403 printf("----------------------------------------------\n\n");
404
405 if (RB_EMPTY_ROOT(&root_sym_ext)) {
406 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
407 return;
408 }
409
410 node = rb_first(&root_sym_ext);
411 while (node) {
412 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200413 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200414 char *path;
415
416 sym_ext = rb_entry(node, struct sym_ext, node);
417 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200418 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200419 path = sym_ext->path;
420
421 color_fprintf(stdout, color, " %7.2f %s", percent, path);
422 node = rb_next(node);
423 }
424}
425
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300426static void hist_entry__print_hits(struct hist_entry *self)
427{
428 struct symbol *sym = self->sym;
429 struct sym_priv *priv = symbol__priv(sym);
430 struct sym_hist *h = priv->hist;
431 u64 len = sym->end - sym->start, offset;
432
433 for (offset = 0; offset < len; ++offset)
434 if (h->ip[offset] != 0)
435 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
436 sym->start + offset, h->ip[offset]);
437 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
438}
439
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200440static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200441{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200442 struct map *map = he->map;
443 struct dso *dso = map->dso;
444 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300445 const char *filename = dso->long_name, *d_filename;
446 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200447 char command[PATH_MAX*2];
448 FILE *file;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300449 LIST_HEAD(head);
450 struct objdump_line *pos, *n;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200451
452 if (!filename)
453 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200454
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200455 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
456 filename, sym->name, map->unmap_ip(map, sym->start),
457 map->unmap_ip(map, sym->end));
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200458
Mike Galbraith42976482009-07-02 08:09:46 +0200459 if (full_paths)
460 d_filename = filename;
461 else
462 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200463
Ingo Molnar0b73da32009-06-06 15:48:52 +0200464 len = sym->end - sym->start;
465
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200466 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200467 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200468 print_summary(filename);
469 }
470
471 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200472 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200473 printf("------------------------------------------------\n");
474
475 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300476 printf("annotating [%p] %30s : [%p] %30s\n",
477 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200478
Mike Galbraith42976482009-07-02 08:09:46 +0200479 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200480 map__rip_2objdump(map, sym->start),
481 map__rip_2objdump(map, sym->end),
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200482 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200483
484 if (verbose >= 3)
485 printf("doing: %s\n", command);
486
487 file = popen(command, "r");
488 if (!file)
489 return;
490
491 while (!feof(file)) {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300492 if (parse_line(file, he, &head) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200493 break;
494 }
495
496 pclose(file);
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300497
498 if (verbose)
499 hist_entry__print_hits(he);
500
501 list_for_each_entry_safe(pos, n, &head, node) {
502 objdump_line__print(pos, &head, he, len);
503 list_del(&pos->node);
504 objdump_line__free(pos);
505 }
506
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200507 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200508 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200509}
510
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200511static void perf_session__find_annotations(struct perf_session *self)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200512{
513 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200514
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200515 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200516 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200517 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200518
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200519 if (he->sym == NULL)
520 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200521
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200522 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200523 if (priv->hist == NULL)
524 continue;
525
526 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200527 /*
528 * Since we have a hist_entry per IP for the same symbol, free
529 * he->sym->hist to signal we already processed this symbol.
530 */
531 free(priv->hist);
532 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200533 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200534}
535
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200536static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200537 .sample = process_sample_event,
538 .mmap = event__process_mmap,
539 .comm = event__process_comm,
540 .fork = event__process_task,
Li Zefanbab81b62009-12-01 14:04:49 +0800541};
542
Ingo Molnar8035e422009-06-06 15:19:13 +0200543static int __cmd_annotate(void)
544{
Li Zefanbab81b62009-12-01 14:04:49 +0800545 int ret;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200546 struct perf_session *session;
Ingo Molnar8035e422009-06-06 15:19:13 +0200547
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200548 session = perf_session__new(input_name, O_RDONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200549 if (session == NULL)
550 return -ENOMEM;
551
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200552 ret = perf_session__process_events(session, &event_ops);
Li Zefanbab81b62009-12-01 14:04:49 +0800553 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200554 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200555
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200556 if (dump_trace) {
557 event__print_totals();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200558 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200559 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200560
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300561 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200562 perf_session__fprintf(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200563
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300564 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200565 dsos__fprintf(stdout);
566
Eric B Munsoneefc4652010-03-05 12:51:08 -0300567 perf_session__collapse_resort(&session->hists);
568 perf_session__output_resort(&session->hists, session->event_total[0]);
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200569 perf_session__find_annotations(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200570out_delete:
571 perf_session__delete(session);
Ingo Molnar8035e422009-06-06 15:19:13 +0200572
Li Zefanbab81b62009-12-01 14:04:49 +0800573 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200574}
575
576static const char * const annotate_usage[] = {
577 "perf annotate [<options>] <command>",
578 NULL
579};
580
581static const struct option options[] = {
582 OPT_STRING('i', "input", &input_name, "file",
583 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200584 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200585 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200586 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200587 OPT_BOOLEAN('v', "verbose", &verbose,
588 "be more verbose (show symbol address, etc)"),
589 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
590 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200591 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
592 "file", "vmlinux pathname"),
593 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200594 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200595 OPT_BOOLEAN('l', "print-line", &print_line,
596 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200597 OPT_BOOLEAN('P', "full-paths", &full_paths,
598 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200599 OPT_END()
600};
601
Ingo Molnarf37a2912009-07-01 12:37:06 +0200602int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200603{
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200604 argc = parse_options(argc, argv, options, annotate_usage, 0);
605
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200606 symbol_conf.priv_size = sizeof(struct sym_priv);
607 symbol_conf.try_vmlinux_path = true;
608
609 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200610 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200611
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200612 setup_sorting(annotate_usage, options);
Ingo Molnar8035e422009-06-06 15:19:13 +0200613
Ingo Molnar0b73da32009-06-06 15:48:52 +0200614 if (argc) {
615 /*
616 * Special case: if there's an argument left then assume tha
617 * it's a symbol filter:
618 */
619 if (argc > 1)
620 usage_with_options(annotate_usage, options);
621
622 sym_hist_filter = argv[0];
623 }
624
Ingo Molnar8035e422009-06-06 15:19:13 +0200625 setup_pager();
626
John Kacurdd68ada2009-09-24 18:02:49 +0200627 if (field_sep && *field_sep == '.') {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200628 pr_err("'.' is the only non valid --field-separator argument\n");
629 return -1;
John Kacurdd68ada2009-09-24 18:02:49 +0200630 }
631
Ingo Molnar8035e422009-06-06 15:19:13 +0200632 return __cmd_annotate();
633}