blob: ee0d917269914589264b9817bb1f835d099d93f4 [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"
Ingo Molnar8035e422009-06-06 15:19:13 +020017
18#include "perf.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020019#include "util/debug.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020020
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020021#include "util/event.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020022#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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020027#include "util/session.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 +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
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020037struct sym_hist {
38 u64 sum;
39 u64 ip[0];
40};
41
Frederic Weisbecker301406b2009-06-13 00:11:21 +020042struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020043 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020044 double percent;
45 char *path;
46};
47
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020048struct sym_priv {
49 struct sym_hist *hist;
50 struct sym_ext *ext;
51};
52
53static const char *sym_hist_filter;
54
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030055static int sym__alloc_hist(struct symbol *self)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020056{
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030057 struct sym_priv *priv = symbol__priv(self);
58 const int size = (sizeof(*priv->hist) +
59 (self->end - self->start) * sizeof(u64));
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020060
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030061 priv->hist = zalloc(size);
62 return priv->hist == NULL ? -1 : 0;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020063}
Ingo Molnar8035e422009-06-06 15:19:13 +020064
Ingo Molnar8035e422009-06-06 15:19:13 +020065/*
66 * collect histogram counts
67 */
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030068static int annotate__hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020069{
70 unsigned int sym_size, offset;
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030071 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020072 struct sym_priv *priv;
73 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020074
75 he->count++;
76
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030077 if (!sym || !he->ms.map)
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030078 return 0;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020079
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020080 priv = symbol__priv(sym);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030081 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
82 return -ENOMEM;
Ingo Molnar0b73da32009-06-06 15:48:52 +020083
84 sym_size = sym->end - sym->start;
85 offset = ip - sym->start;
86
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030087 pr_debug3("%s: ip=%#Lx\n", __func__, he->ms.map->unmap_ip(he->ms.map, ip));
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020088
Ingo Molnar0b73da32009-06-06 15:48:52 +020089 if (offset >= sym_size)
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030090 return 0;
Ingo Molnar0b73da32009-06-06 15:48:52 +020091
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020092 h = priv->hist;
93 h->sum++;
94 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +020095
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030096 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->ms.sym->start,
97 he->ms.sym->name, ip, ip - he->ms.sym->start, h->ip[offset]);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030098 return 0;
Ingo Molnar0b73da32009-06-06 15:48:52 +020099}
Ingo Molnar8035e422009-06-06 15:19:13 +0200100
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200101static int perf_session__add_hist_entry(struct perf_session *self,
102 struct addr_location *al, u64 count)
Ingo Molnar8035e422009-06-06 15:19:13 +0200103{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300104 bool hit;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300105 struct hist_entry *he;
106
107 if (sym_hist_filter != NULL &&
108 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
109 /* We're only interested in a symbol named sym_hist_filter */
110 if (al->sym != NULL) {
111 rb_erase(&al->sym->rb_node,
112 &al->map->dso->symbols[al->map->type]);
113 symbol__delete(al->sym);
114 }
115 return 0;
116 }
117
Eric B Munsond403d0a2010-03-05 12:51:06 -0300118 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300119 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200120 return -ENOMEM;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300121
122 return annotate__hist_hit(he, al->addr);
Ingo Molnar8035e422009-06-06 15:19:13 +0200123}
124
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200125static int process_sample_event(event_t *event, struct perf_session *session)
Ingo Molnar8035e422009-06-06 15:19:13 +0200126{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200127 struct addr_location al;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200128
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -0200129 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
130 event->ip.pid, event->ip.ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200131
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300132 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200133 pr_warning("problem processing %d event, skipping it.\n",
134 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +0200135 return -1;
136 }
137
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200138 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200139 pr_warning("problem incrementing symbol count, "
140 "skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300141 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200142 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200143
144 return 0;
145}
146
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300147struct objdump_line {
148 struct list_head node;
149 s64 offset;
150 char *line;
151};
152
153static struct objdump_line *objdump_line__new(s64 offset, char *line)
154{
155 struct objdump_line *self = malloc(sizeof(*self));
156
157 if (self != NULL) {
158 self->offset = offset;
159 self->line = line;
160 }
161
162 return self;
163}
164
165static void objdump_line__free(struct objdump_line *self)
166{
167 free(self->line);
168 free(self);
169}
170
171static void objdump__add_line(struct list_head *head, struct objdump_line *line)
172{
173 list_add_tail(&line->node, head);
174}
175
176static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
177 struct objdump_line *pos)
178{
179 list_for_each_entry_continue(pos, head, node)
180 if (pos->offset >= 0)
181 return pos;
182
183 return NULL;
184}
185
186static int parse_line(FILE *file, struct hist_entry *he,
187 struct list_head *head)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200188{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300189 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300190 struct objdump_line *objdump_line;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200191 char *line = NULL, *tmp, *tmp2;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200192 size_t line_len;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300193 s64 line_ip, offset = -1;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200194 char *c;
195
196 if (getline(&line, &line_len, file) < 0)
197 return -1;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300198
Ingo Molnar0b73da32009-06-06 15:48:52 +0200199 if (!line)
200 return -1;
201
202 c = strchr(line, '\n');
203 if (c)
204 *c = 0;
205
206 line_ip = -1;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200207
208 /*
209 * Strip leading spaces:
210 */
211 tmp = line;
212 while (*tmp) {
213 if (*tmp != ' ')
214 break;
215 tmp++;
216 }
217
218 if (*tmp) {
219 /*
220 * Parse hexa addresses followed by ':'
221 */
222 line_ip = strtoull(tmp, &tmp2, 16);
223 if (*tmp2 != ':')
224 line_ip = -1;
225 }
226
227 if (line_ip != -1) {
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300228 u64 start = map__rip_2objdump(he->ms.map, sym->start);
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300229 offset = line_ip - start;
230 }
231
232 objdump_line = objdump_line__new(offset, line);
233 if (objdump_line == NULL) {
234 free(line);
235 return -1;
236 }
237 objdump__add_line(head, objdump_line);
238
239 return 0;
240}
241
242static int objdump_line__print(struct objdump_line *self,
243 struct list_head *head,
244 struct hist_entry *he, u64 len)
245{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300246 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300247 static const char *prev_line;
248 static const char *prev_color;
249
250 if (self->offset != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200251 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200252 unsigned int hits = 0;
253 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200254 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200255 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200256 struct sym_ext *sym_ext = priv->ext;
257 struct sym_hist *h = priv->hist;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300258 s64 offset = self->offset;
259 struct objdump_line *next = objdump__get_next_ip_line(head, self);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200260
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300261 while (offset < (s64)len &&
262 (next == NULL || offset < next->offset)) {
263 if (sym_ext) {
264 if (path == NULL)
265 path = sym_ext[offset].path;
266 percent += sym_ext[offset].percent;
267 } else
268 hits += h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200269
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300270 ++offset;
271 }
272
273 if (sym_ext == NULL && h->sum)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200274 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200275
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200276 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200277
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200278 /*
279 * Also color the filename and line if needed, with
280 * the same color than the percentage. Don't print it
281 * twice for close colored ip with the same filename:line
282 */
283 if (path) {
284 if (!prev_line || strcmp(prev_line, path)
285 || color != prev_color) {
286 color_fprintf(stdout, color, " %s", path);
287 prev_line = path;
288 prev_color = color;
289 }
290 }
291
Ingo Molnar0b73da32009-06-06 15:48:52 +0200292 color_fprintf(stdout, color, " %7.2f", percent);
293 printf(" : ");
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300294 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200295 } else {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300296 if (!*self->line)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200297 printf(" :\n");
298 else
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300299 printf(" : %s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200300 }
301
302 return 0;
303}
304
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200305static struct rb_root root_sym_ext;
306
307static void insert_source_line(struct sym_ext *sym_ext)
308{
309 struct sym_ext *iter;
310 struct rb_node **p = &root_sym_ext.rb_node;
311 struct rb_node *parent = NULL;
312
313 while (*p != NULL) {
314 parent = *p;
315 iter = rb_entry(parent, struct sym_ext, node);
316
317 if (sym_ext->percent > iter->percent)
318 p = &(*p)->rb_left;
319 else
320 p = &(*p)->rb_right;
321 }
322
323 rb_link_node(&sym_ext->node, parent, p);
324 rb_insert_color(&sym_ext->node, &root_sym_ext);
325}
326
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200327static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200328{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300329 struct sym_priv *priv = symbol__priv(he->ms.sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200330 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200331 int i;
332
333 if (!sym_ext)
334 return;
335
336 for (i = 0; i < len; i++)
337 free(sym_ext[i].path);
338 free(sym_ext);
339
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200340 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200341 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200342}
343
344/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200345static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200346get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200347{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300348 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200349 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200350 int i;
351 char cmd[PATH_MAX * 2];
352 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200353 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200354 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200355
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200356 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200357 return;
358
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200359 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
360 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200361 return;
362
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300363 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200364
365 for (i = 0; i < len; i++) {
366 char *path = NULL;
367 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000368 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200369 FILE *fp;
370
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200371 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200372 if (sym_ext[i].percent <= 0.5)
373 continue;
374
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200375 offset = start + i;
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200376 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200377 fp = popen(cmd, "r");
378 if (!fp)
379 continue;
380
381 if (getline(&path, &line_len, fp) < 0 || !line_len)
382 goto next;
383
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200384 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200385 if (!sym_ext[i].path)
386 goto next;
387
388 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200389 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200390
391 next:
392 pclose(fp);
393 }
394}
395
Ingo Molnar83a09442009-08-15 12:26:57 +0200396static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200397{
398 struct sym_ext *sym_ext;
399 struct rb_node *node;
400
401 printf("\nSorted summary for file %s\n", filename);
402 printf("----------------------------------------------\n\n");
403
404 if (RB_EMPTY_ROOT(&root_sym_ext)) {
405 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
406 return;
407 }
408
409 node = rb_first(&root_sym_ext);
410 while (node) {
411 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200412 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200413 char *path;
414
415 sym_ext = rb_entry(node, struct sym_ext, node);
416 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200417 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200418 path = sym_ext->path;
419
420 color_fprintf(stdout, color, " %7.2f %s", percent, path);
421 node = rb_next(node);
422 }
423}
424
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300425static void hist_entry__print_hits(struct hist_entry *self)
426{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300427 struct symbol *sym = self->ms.sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300428 struct sym_priv *priv = symbol__priv(sym);
429 struct sym_hist *h = priv->hist;
430 u64 len = sym->end - sym->start, offset;
431
432 for (offset = 0; offset < len; ++offset)
433 if (h->ip[offset] != 0)
434 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
435 sym->start + offset, h->ip[offset]);
436 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
437}
438
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200439static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200440{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300441 struct map *map = he->ms.map;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200442 struct dso *dso = map->dso;
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300443 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300444 const char *filename = dso->long_name, *d_filename;
445 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200446 char command[PATH_MAX*2];
447 FILE *file;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300448 LIST_HEAD(head);
449 struct objdump_line *pos, *n;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200450
451 if (!filename)
452 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200453
Arnaldo Carvalho de Melod06d92b2010-03-15 13:04:33 -0300454 if (dso->origin == DSO__ORIG_KERNEL) {
455 if (dso->annotate_warned)
456 return;
457 dso->annotate_warned = 1;
458 pr_err("Can't annotate %s: No vmlinux file was found in the "
459 "path:\n", sym->name);
460 vmlinux_path__fprintf(stderr);
461 return;
462 }
463
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200464 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
465 filename, sym->name, map->unmap_ip(map, sym->start),
466 map->unmap_ip(map, sym->end));
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200467
Mike Galbraith42976482009-07-02 08:09:46 +0200468 if (full_paths)
469 d_filename = filename;
470 else
471 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200472
Ingo Molnar0b73da32009-06-06 15:48:52 +0200473 len = sym->end - sym->start;
474
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200475 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200476 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200477 print_summary(filename);
478 }
479
480 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200481 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200482 printf("------------------------------------------------\n");
483
484 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300485 printf("annotating [%p] %30s : [%p] %30s\n",
486 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200487
Mike Galbraith42976482009-07-02 08:09:46 +0200488 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200489 map__rip_2objdump(map, sym->start),
490 map__rip_2objdump(map, sym->end),
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200491 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200492
493 if (verbose >= 3)
494 printf("doing: %s\n", command);
495
496 file = popen(command, "r");
497 if (!file)
498 return;
499
500 while (!feof(file)) {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300501 if (parse_line(file, he, &head) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200502 break;
503 }
504
505 pclose(file);
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300506
507 if (verbose)
508 hist_entry__print_hits(he);
509
510 list_for_each_entry_safe(pos, n, &head, node) {
511 objdump_line__print(pos, &head, he, len);
512 list_del(&pos->node);
513 objdump_line__free(pos);
514 }
515
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200516 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200517 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200518}
519
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200520static void perf_session__find_annotations(struct perf_session *self)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200521{
522 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200523
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200524 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200525 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200526 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200527
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300528 if (he->ms.sym == NULL)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200529 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200530
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300531 priv = symbol__priv(he->ms.sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200532 if (priv->hist == NULL)
533 continue;
534
535 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200536 /*
537 * Since we have a hist_entry per IP for the same symbol, free
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300538 * he->ms.sym->hist to signal we already processed this symbol.
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200539 */
540 free(priv->hist);
541 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200542 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200543}
544
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200545static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200546 .sample = process_sample_event,
547 .mmap = event__process_mmap,
548 .comm = event__process_comm,
549 .fork = event__process_task,
Li Zefanbab81b62009-12-01 14:04:49 +0800550};
551
Ingo Molnar8035e422009-06-06 15:19:13 +0200552static int __cmd_annotate(void)
553{
Li Zefanbab81b62009-12-01 14:04:49 +0800554 int ret;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200555 struct perf_session *session;
Ingo Molnar8035e422009-06-06 15:19:13 +0200556
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200557 session = perf_session__new(input_name, O_RDONLY, force);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200558 if (session == NULL)
559 return -ENOMEM;
560
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200561 ret = perf_session__process_events(session, &event_ops);
Li Zefanbab81b62009-12-01 14:04:49 +0800562 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200563 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200564
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200565 if (dump_trace) {
566 event__print_totals();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200567 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200568 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200569
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300570 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200571 perf_session__fprintf(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200572
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300573 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200574 dsos__fprintf(stdout);
575
Eric B Munsoneefc4652010-03-05 12:51:08 -0300576 perf_session__collapse_resort(&session->hists);
577 perf_session__output_resort(&session->hists, session->event_total[0]);
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200578 perf_session__find_annotations(session);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200579out_delete:
580 perf_session__delete(session);
Ingo Molnar8035e422009-06-06 15:19:13 +0200581
Li Zefanbab81b62009-12-01 14:04:49 +0800582 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200583}
584
585static const char * const annotate_usage[] = {
586 "perf annotate [<options>] <command>",
587 NULL
588};
589
590static const struct option options[] = {
591 OPT_STRING('i', "input", &input_name, "file",
592 "input file name"),
Arnaldo Carvalho de Meloac73c5a2010-03-24 16:40:16 -0300593 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
594 "only consider symbols in these dsos"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200595 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200596 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200597 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200598 OPT_BOOLEAN('v', "verbose", &verbose,
599 "be more verbose (show symbol address, etc)"),
600 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
601 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200602 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
603 "file", "vmlinux pathname"),
604 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200605 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200606 OPT_BOOLEAN('l', "print-line", &print_line,
607 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200608 OPT_BOOLEAN('P', "full-paths", &full_paths,
609 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200610 OPT_END()
611};
612
Ingo Molnarf37a2912009-07-01 12:37:06 +0200613int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200614{
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200615 argc = parse_options(argc, argv, options, annotate_usage, 0);
616
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200617 symbol_conf.priv_size = sizeof(struct sym_priv);
618 symbol_conf.try_vmlinux_path = true;
619
620 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200621 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200622
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200623 setup_sorting(annotate_usage, options);
Ingo Molnar8035e422009-06-06 15:19:13 +0200624
Ingo Molnar0b73da32009-06-06 15:48:52 +0200625 if (argc) {
626 /*
627 * Special case: if there's an argument left then assume tha
628 * it's a symbol filter:
629 */
630 if (argc > 1)
631 usage_with_options(annotate_usage, options);
632
633 sym_hist_filter = argv[0];
634 }
635
Ingo Molnar8035e422009-06-06 15:19:13 +0200636 setup_pager();
637
John Kacurdd68ada2009-09-24 18:02:49 +0200638 if (field_sep && *field_sep == '.') {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200639 pr_err("'.' is the only non valid --field-separator argument\n");
640 return -1;
John Kacurdd68ada2009-09-24 18:02:49 +0200641 }
642
Ingo Molnar8035e422009-06-06 15:19:13 +0200643 return __cmd_annotate();
644}