blob: 96db5248e99569914c62427a3466aa9b91559530 [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 Weisbecker8f28827a2009-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
Ian Munsiec0555642010-04-13 18:37:33 +100031static bool force;
Ingo Molnar8035e422009-06-06 15:19:13 +020032
Ian Munsiec0555642010-04-13 18:37:33 +100033static bool full_paths;
Mike Galbraith42976482009-07-02 08:09:46 +020034
Ian Munsiec0555642010-04-13 18:37:33 +100035static bool print_line;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020036
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020037static const char *sym_hist_filter;
38
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030039static int hists__add_entry(struct hists *self, struct addr_location *al)
Ingo Molnar8035e422009-06-06 15:19:13 +020040{
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030041 struct hist_entry *he;
42
43 if (sym_hist_filter != NULL &&
44 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
45 /* We're only interested in a symbol named sym_hist_filter */
46 if (al->sym != NULL) {
47 rb_erase(&al->sym->rb_node,
48 &al->map->dso->symbols[al->map->type]);
49 symbol__delete(al->sym);
50 }
51 return 0;
52 }
53
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030054 he = __hists__add_entry(self, al, NULL, 1);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030055 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +020056 return -ENOMEM;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030057
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -030058 return hist_entry__inc_addr_samples(he, al->addr);
Ingo Molnar8035e422009-06-06 15:19:13 +020059}
60
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020061static int process_sample_event(event_t *event, struct perf_session *session)
Ingo Molnar8035e422009-06-06 15:19:13 +020062{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020063 struct addr_location al;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020064
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020065 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
66 event->ip.pid, event->ip.ip);
Ingo Molnar8035e422009-06-06 15:19:13 +020067
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030068 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -020069 pr_warning("problem processing %d event, skipping it.\n",
70 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +020071 return -1;
72 }
73
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030074 if (!al.filtered && hists__add_entry(&session->hists, &al)) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -020075 pr_warning("problem incrementing symbol count, "
76 "skipping event\n");
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -030077 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +020078 }
Ingo Molnar8035e422009-06-06 15:19:13 +020079
80 return 0;
81}
82
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -030083static int objdump_line__print(struct objdump_line *self,
84 struct list_head *head,
85 struct hist_entry *he, u64 len)
86{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030087 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -030088 static const char *prev_line;
89 static const char *prev_color;
90
91 if (self->offset != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +020092 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +020093 unsigned int hits = 0;
94 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +020095 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020096 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020097 struct sym_ext *sym_ext = priv->ext;
98 struct sym_hist *h = priv->hist;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -030099 s64 offset = self->offset;
100 struct objdump_line *next = objdump__get_next_ip_line(head, self);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200101
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300102 while (offset < (s64)len &&
103 (next == NULL || offset < next->offset)) {
104 if (sym_ext) {
105 if (path == NULL)
106 path = sym_ext[offset].path;
107 percent += sym_ext[offset].percent;
108 } else
109 hits += h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200110
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300111 ++offset;
112 }
113
114 if (sym_ext == NULL && h->sum)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200115 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200116
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200117 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200118
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200119 /*
120 * Also color the filename and line if needed, with
121 * the same color than the percentage. Don't print it
122 * twice for close colored ip with the same filename:line
123 */
124 if (path) {
125 if (!prev_line || strcmp(prev_line, path)
126 || color != prev_color) {
127 color_fprintf(stdout, color, " %s", path);
128 prev_line = path;
129 prev_color = color;
130 }
131 }
132
Ingo Molnar0b73da32009-06-06 15:48:52 +0200133 color_fprintf(stdout, color, " %7.2f", percent);
134 printf(" : ");
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300135 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200136 } else {
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300137 if (!*self->line)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200138 printf(" :\n");
139 else
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300140 printf(" : %s\n", self->line);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200141 }
142
143 return 0;
144}
145
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200146static struct rb_root root_sym_ext;
147
148static void insert_source_line(struct sym_ext *sym_ext)
149{
150 struct sym_ext *iter;
151 struct rb_node **p = &root_sym_ext.rb_node;
152 struct rb_node *parent = NULL;
153
154 while (*p != NULL) {
155 parent = *p;
156 iter = rb_entry(parent, struct sym_ext, node);
157
158 if (sym_ext->percent > iter->percent)
159 p = &(*p)->rb_left;
160 else
161 p = &(*p)->rb_right;
162 }
163
164 rb_link_node(&sym_ext->node, parent, p);
165 rb_insert_color(&sym_ext->node, &root_sym_ext);
166}
167
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200168static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200169{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300170 struct sym_priv *priv = symbol__priv(he->ms.sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200171 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200172 int i;
173
174 if (!sym_ext)
175 return;
176
177 for (i = 0; i < len; i++)
178 free(sym_ext[i].path);
179 free(sym_ext);
180
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200181 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200182 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200183}
184
185/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200186static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200187get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200188{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300189 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200190 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200191 int i;
192 char cmd[PATH_MAX * 2];
193 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200194 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200195 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200196
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200197 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200198 return;
199
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200200 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
201 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200202 return;
203
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300204 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200205
206 for (i = 0; i < len; i++) {
207 char *path = NULL;
208 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000209 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200210 FILE *fp;
211
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200212 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200213 if (sym_ext[i].percent <= 0.5)
214 continue;
215
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200216 offset = start + i;
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200217 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200218 fp = popen(cmd, "r");
219 if (!fp)
220 continue;
221
222 if (getline(&path, &line_len, fp) < 0 || !line_len)
223 goto next;
224
Frederic Weisbeckerc17c2db12009-06-13 17:39:23 +0200225 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200226 if (!sym_ext[i].path)
227 goto next;
228
229 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200230 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200231
232 next:
233 pclose(fp);
234 }
235}
236
Ingo Molnar83a09442009-08-15 12:26:57 +0200237static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200238{
239 struct sym_ext *sym_ext;
240 struct rb_node *node;
241
242 printf("\nSorted summary for file %s\n", filename);
243 printf("----------------------------------------------\n\n");
244
245 if (RB_EMPTY_ROOT(&root_sym_ext)) {
246 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
247 return;
248 }
249
250 node = rb_first(&root_sym_ext);
251 while (node) {
252 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200253 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200254 char *path;
255
256 sym_ext = rb_entry(node, struct sym_ext, node);
257 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200258 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200259 path = sym_ext->path;
260
261 color_fprintf(stdout, color, " %7.2f %s", percent, path);
262 node = rb_next(node);
263 }
264}
265
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300266static void hist_entry__print_hits(struct hist_entry *self)
267{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300268 struct symbol *sym = self->ms.sym;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300269 struct sym_priv *priv = symbol__priv(sym);
270 struct sym_hist *h = priv->hist;
271 u64 len = sym->end - sym->start, offset;
272
273 for (offset = 0; offset < len; ++offset)
274 if (h->ip[offset] != 0)
275 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
276 sym->start + offset, h->ip[offset]);
277 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
278}
279
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300280static int hist_entry__tty_annotate(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200281{
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300282 struct map *map = he->ms.map;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200283 struct dso *dso = map->dso;
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300284 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300285 const char *filename = dso->long_name, *d_filename;
286 u64 len;
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300287 LIST_HEAD(head);
288 struct objdump_line *pos, *n;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200289
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300290 if (hist_entry__annotate(he, &head) < 0)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300291 return -1;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200292
Mike Galbraith42976482009-07-02 08:09:46 +0200293 if (full_paths)
294 d_filename = filename;
295 else
296 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200297
Ingo Molnar0b73da32009-06-06 15:48:52 +0200298 len = sym->end - sym->start;
299
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200300 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200301 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200302 print_summary(filename);
303 }
304
305 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200306 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200307 printf("------------------------------------------------\n");
308
Arnaldo Carvalho de Melo48fb4fd2010-02-26 11:23:14 -0300309 if (verbose)
310 hist_entry__print_hits(he);
311
312 list_for_each_entry_safe(pos, n, &head, node) {
313 objdump_line__print(pos, &head, he, len);
314 list_del(&pos->node);
315 objdump_line__free(pos);
316 }
317
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200318 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200319 free_source_line(he, len);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300320
321 return 0;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200322}
323
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300324static void hists__find_annotations(struct hists *self)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200325{
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300326 struct rb_node *first = rb_first(&self->entries), *nd = first;
327 int key = KEY_RIGHT;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200328
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300329 while (nd) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200330 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200331 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200332
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300333 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
334 goto find_next;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200335
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300336 priv = symbol__priv(he->ms.sym);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300337 if (priv->hist == NULL) {
338find_next:
339 if (key == KEY_LEFT)
340 nd = rb_prev(nd);
341 else
342 nd = rb_next(nd);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200343 continue;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300344 }
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200345
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -0300346 if (use_browser > 0) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300347 key = hist_entry__tui_annotate(he);
348 if (is_exit_key(key))
349 break;
350 switch (key) {
351 case KEY_RIGHT:
352 case '\t':
353 nd = rb_next(nd);
354 break;
355 case KEY_LEFT:
356 if (nd == first)
357 continue;
358 nd = rb_prev(nd);
359 default:
360 break;
361 }
362 } else {
363 hist_entry__tty_annotate(he);
364 nd = rb_next(nd);
365 /*
366 * Since we have a hist_entry per IP for the same
367 * symbol, free he->ms.sym->hist to signal we already
368 * processed this symbol.
369 */
370 free(priv->hist);
371 priv->hist = NULL;
372 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200373 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200374}
375
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200376static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200377 .sample = process_sample_event,
378 .mmap = event__process_mmap,
379 .comm = event__process_comm,
380 .fork = event__process_task,
Li Zefanbab81b62009-12-01 14:04:49 +0800381};
382
Ingo Molnar8035e422009-06-06 15:19:13 +0200383static int __cmd_annotate(void)
384{
Li Zefanbab81b62009-12-01 14:04:49 +0800385 int ret;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200386 struct perf_session *session;
Ingo Molnar8035e422009-06-06 15:19:13 +0200387
Tom Zanussi454c4072010-05-01 01:41:20 -0500388 session = perf_session__new(input_name, O_RDONLY, force, false);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200389 if (session == NULL)
390 return -ENOMEM;
391
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200392 ret = perf_session__process_events(session, &event_ops);
Li Zefanbab81b62009-12-01 14:04:49 +0800393 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200394 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200395
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200396 if (dump_trace) {
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300397 perf_session__fprintf_nr_events(session, stdout);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200398 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200399 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200400
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300401 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200402 perf_session__fprintf(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200403
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300404 if (verbose > 2)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300405 perf_session__fprintf_dsos(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200406
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300407 hists__collapse_resort(&session->hists);
408 hists__output_resort(&session->hists);
409 hists__find_annotations(&session->hists);
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200410out_delete:
411 perf_session__delete(session);
Ingo Molnar8035e422009-06-06 15:19:13 +0200412
Li Zefanbab81b62009-12-01 14:04:49 +0800413 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200414}
415
416static const char * const annotate_usage[] = {
417 "perf annotate [<options>] <command>",
418 NULL
419};
420
421static const struct option options[] = {
422 OPT_STRING('i', "input", &input_name, "file",
423 "input file name"),
Arnaldo Carvalho de Meloac73c5a2010-03-24 16:40:16 -0300424 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
425 "only consider symbols in these dsos"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200426 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200427 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200428 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ian Munsiec0555642010-04-13 18:37:33 +1000429 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar8035e422009-06-06 15:19:13 +0200430 "be more verbose (show symbol address, etc)"),
431 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
432 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200433 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
434 "file", "vmlinux pathname"),
435 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200436 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200437 OPT_BOOLEAN('l', "print-line", &print_line,
438 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200439 OPT_BOOLEAN('P', "full-paths", &full_paths,
440 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200441 OPT_END()
442};
443
Ingo Molnarf37a2912009-07-01 12:37:06 +0200444int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200445{
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200446 argc = parse_options(argc, argv, options, annotate_usage, 0);
447
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300448 setup_browser();
449
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200450 symbol_conf.priv_size = sizeof(struct sym_priv);
451 symbol_conf.try_vmlinux_path = true;
452
453 if (symbol__init() < 0)
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200454 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200455
Arnaldo Carvalho de Meloc8829c72009-12-14 20:09:29 -0200456 setup_sorting(annotate_usage, options);
Ingo Molnar8035e422009-06-06 15:19:13 +0200457
Ingo Molnar0b73da32009-06-06 15:48:52 +0200458 if (argc) {
459 /*
460 * Special case: if there's an argument left then assume tha
461 * it's a symbol filter:
462 */
463 if (argc > 1)
464 usage_with_options(annotate_usage, options);
465
466 sym_hist_filter = argv[0];
467 }
468
John Kacurdd68ada2009-09-24 18:02:49 +0200469 if (field_sep && *field_sep == '.') {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200470 pr_err("'.' is the only non valid --field-separator argument\n");
471 return -1;
John Kacurdd68ada2009-09-24 18:02:49 +0200472 }
473
Ingo Molnar8035e422009-06-06 15:19:13 +0200474 return __cmd_annotate();
475}