blob: 21a78d30d53dcf584a27ede521b34ddeb53118a3 [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"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020028#include "util/session.h"
Li Zefanbab81b62009-12-01 14:04:49 +080029#include "util/data_map.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020030
Ingo Molnar8035e422009-06-06 15:19:13 +020031static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020032
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020033static int force;
Ingo Molnar8035e422009-06-06 15:19:13 +020034
Mike Galbraith42976482009-07-02 08:09:46 +020035static int full_paths;
36
Frederic Weisbecker301406b2009-06-13 00:11:21 +020037static int print_line;
38
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020039struct sym_hist {
40 u64 sum;
41 u64 ip[0];
42};
43
Frederic Weisbecker301406b2009-06-13 00:11:21 +020044struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020045 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020046 double percent;
47 char *path;
48};
49
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020050struct sym_priv {
51 struct sym_hist *hist;
52 struct sym_ext *ext;
53};
54
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020055static struct symbol_conf symbol_conf = {
56 .priv_size = sizeof(struct sym_priv),
57 .try_vmlinux_path = true,
58};
59
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020060static const char *sym_hist_filter;
61
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020062static int symbol_filter(struct map *map __used, struct symbol *sym)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020063{
Arnaldo Carvalho de Melo8f0b0372009-10-20 15:08:29 -020064 if (sym_hist_filter == NULL ||
65 strcmp(sym->name, sym_hist_filter) == 0) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020066 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020067 const int size = (sizeof(*priv->hist) +
68 (sym->end - sym->start) * sizeof(u64));
69
70 priv->hist = malloc(size);
71 if (priv->hist)
72 memset(priv->hist, 0, size);
73 return 0;
74 }
75 /*
76 * FIXME: We should really filter it out, as we don't want to go thru symbols
77 * we're not interested, and if a DSO ends up with no symbols, delete it too,
78 * but right now the kernel loading routines in symbol.c bail out if no symbols
79 * are found, fix it later.
80 */
81 return 0;
82}
Ingo Molnar8035e422009-06-06 15:19:13 +020083
Ingo Molnar8035e422009-06-06 15:19:13 +020084/*
85 * collect histogram counts
86 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100087static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020088{
89 unsigned int sym_size, offset;
90 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020091 struct sym_priv *priv;
92 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020093
94 he->count++;
95
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020096 if (!sym || !he->map)
97 return;
98
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020099 priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200100 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200101 return;
102
103 sym_size = sym->end - sym->start;
104 offset = ip - sym->start;
105
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200106 if (verbose)
107 fprintf(stderr, "%s: ip=%Lx\n", __func__,
108 he->map->unmap_ip(he->map, ip));
109
Ingo Molnar0b73da32009-06-06 15:48:52 +0200110 if (offset >= sym_size)
111 return;
112
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200113 h = priv->hist;
114 h->sum++;
115 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200116
117 if (verbose >= 3)
118 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200119 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200120 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200121 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200122 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200123}
Ingo Molnar8035e422009-06-06 15:19:13 +0200124
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200125static int hist_entry__add(struct addr_location *al, u64 count)
Ingo Molnar8035e422009-06-06 15:19:13 +0200126{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300127 bool hit;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200128 struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300129 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200130 return -ENOMEM;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200131 hist_hit(he, al->addr);
Ingo Molnar8035e422009-06-06 15:19:13 +0200132 return 0;
133}
134
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200135static int process_sample_event(event_t *event)
Ingo Molnar8035e422009-06-06 15:19:13 +0200136{
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200137 struct addr_location al;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200138
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200139 dump_printf("(IP, %d): %d: %p\n", event->header.misc,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200140 event->ip.pid, (void *)(long)event->ip.ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200141
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200142 if (event__preprocess_sample(event, &al, symbol_filter) < 0) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200143 fprintf(stderr, "problem processing %d event, skipping it.\n",
144 event->header.type);
145 return -1;
146 }
147
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200148 if (hist_entry__add(&al, 1)) {
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300149 fprintf(stderr, "problem incrementing symbol count, "
150 "skipping event\n");
151 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200152 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200153
154 return 0;
155}
156
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200157static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200158{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200159 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200160 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200161 static const char *prev_line;
162 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200163 unsigned int offset;
164 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200165 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200166 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200167 int ret;
168 char *c;
169
170 if (getline(&line, &line_len, file) < 0)
171 return -1;
172 if (!line)
173 return -1;
174
175 c = strchr(line, '\n');
176 if (c)
177 *c = 0;
178
179 line_ip = -1;
180 offset = 0;
181 ret = -2;
182
183 /*
184 * Strip leading spaces:
185 */
186 tmp = line;
187 while (*tmp) {
188 if (*tmp != ' ')
189 break;
190 tmp++;
191 }
192
193 if (*tmp) {
194 /*
195 * Parse hexa addresses followed by ':'
196 */
197 line_ip = strtoull(tmp, &tmp2, 16);
198 if (*tmp2 != ':')
199 line_ip = -1;
200 }
201
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200202 start = he->map->unmap_ip(he->map, sym->start);
203
Ingo Molnar0b73da32009-06-06 15:48:52 +0200204 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200205 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200206 unsigned int hits = 0;
207 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200208 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200209 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200210 struct sym_ext *sym_ext = priv->ext;
211 struct sym_hist *h = priv->hist;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200212
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200213 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200214 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200215 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200216
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200217 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200218 path = sym_ext[offset].path;
219 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200220 } else if (h->sum)
221 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200222
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200223 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200224
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200225 /*
226 * Also color the filename and line if needed, with
227 * the same color than the percentage. Don't print it
228 * twice for close colored ip with the same filename:line
229 */
230 if (path) {
231 if (!prev_line || strcmp(prev_line, path)
232 || color != prev_color) {
233 color_fprintf(stdout, color, " %s", path);
234 prev_line = path;
235 prev_color = color;
236 }
237 }
238
Ingo Molnar0b73da32009-06-06 15:48:52 +0200239 color_fprintf(stdout, color, " %7.2f", percent);
240 printf(" : ");
241 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
242 } else {
243 if (!*line)
244 printf(" :\n");
245 else
246 printf(" : %s\n", line);
247 }
248
249 return 0;
250}
251
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200252static struct rb_root root_sym_ext;
253
254static void insert_source_line(struct sym_ext *sym_ext)
255{
256 struct sym_ext *iter;
257 struct rb_node **p = &root_sym_ext.rb_node;
258 struct rb_node *parent = NULL;
259
260 while (*p != NULL) {
261 parent = *p;
262 iter = rb_entry(parent, struct sym_ext, node);
263
264 if (sym_ext->percent > iter->percent)
265 p = &(*p)->rb_left;
266 else
267 p = &(*p)->rb_right;
268 }
269
270 rb_link_node(&sym_ext->node, parent, p);
271 rb_insert_color(&sym_ext->node, &root_sym_ext);
272}
273
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200274static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200275{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200276 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200277 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200278 int i;
279
280 if (!sym_ext)
281 return;
282
283 for (i = 0; i < len; i++)
284 free(sym_ext[i].path);
285 free(sym_ext);
286
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200287 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200288 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200289}
290
291/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200292static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200293get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200294{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200295 struct symbol *sym = he->sym;
296 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200297 int i;
298 char cmd[PATH_MAX * 2];
299 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200300 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200301 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200302
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200303 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200304 return;
305
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200306 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
307 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200308 return;
309
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200310 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200311
312 for (i = 0; i < len; i++) {
313 char *path = NULL;
314 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000315 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200316 FILE *fp;
317
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200318 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200319 if (sym_ext[i].percent <= 0.5)
320 continue;
321
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200322 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200323 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200324 fp = popen(cmd, "r");
325 if (!fp)
326 continue;
327
328 if (getline(&path, &line_len, fp) < 0 || !line_len)
329 goto next;
330
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200331 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200332 if (!sym_ext[i].path)
333 goto next;
334
335 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200336 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200337
338 next:
339 pclose(fp);
340 }
341}
342
Ingo Molnar83a09442009-08-15 12:26:57 +0200343static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200344{
345 struct sym_ext *sym_ext;
346 struct rb_node *node;
347
348 printf("\nSorted summary for file %s\n", filename);
349 printf("----------------------------------------------\n\n");
350
351 if (RB_EMPTY_ROOT(&root_sym_ext)) {
352 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
353 return;
354 }
355
356 node = rb_first(&root_sym_ext);
357 while (node) {
358 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200359 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200360 char *path;
361
362 sym_ext = rb_entry(node, struct sym_ext, node);
363 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200364 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200365 path = sym_ext->path;
366
367 color_fprintf(stdout, color, " %7.2f %s", percent, path);
368 node = rb_next(node);
369 }
370}
371
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200372static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200373{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200374 struct map *map = he->map;
375 struct dso *dso = map->dso;
376 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300377 const char *filename = dso->long_name, *d_filename;
378 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200379 char command[PATH_MAX*2];
380 FILE *file;
381
382 if (!filename)
383 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200384
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200385 if (verbose)
386 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
387 __func__, filename, sym->name,
388 map->unmap_ip(map, sym->start),
389 map->unmap_ip(map, sym->end));
390
Mike Galbraith42976482009-07-02 08:09:46 +0200391 if (full_paths)
392 d_filename = filename;
393 else
394 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200395
Ingo Molnar0b73da32009-06-06 15:48:52 +0200396 len = sym->end - sym->start;
397
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200398 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200399 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200400 print_summary(filename);
401 }
402
403 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200404 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200405 printf("------------------------------------------------\n");
406
407 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300408 printf("annotating [%p] %30s : [%p] %30s\n",
409 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200410
Mike Galbraith42976482009-07-02 08:09:46 +0200411 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 -0200412 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
413 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200414
415 if (verbose >= 3)
416 printf("doing: %s\n", command);
417
418 file = popen(command, "r");
419 if (!file)
420 return;
421
422 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200423 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200424 break;
425 }
426
427 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200428 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200429 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200430}
431
432static void find_annotations(void)
433{
434 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200435
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200436 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
437 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200438 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200439
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200440 if (he->sym == NULL)
441 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200442
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200443 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200444 if (priv->hist == NULL)
445 continue;
446
447 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200448 /*
449 * Since we have a hist_entry per IP for the same symbol, free
450 * he->sym->hist to signal we already processed this symbol.
451 */
452 free(priv->hist);
453 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200454 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200455}
456
Li Zefanbab81b62009-12-01 14:04:49 +0800457static struct perf_file_handler file_handler = {
458 .process_sample_event = process_sample_event,
459 .process_mmap_event = event__process_mmap,
460 .process_comm_event = event__process_comm,
461 .process_fork_event = event__process_task,
462};
463
Ingo Molnar8035e422009-06-06 15:19:13 +0200464static int __cmd_annotate(void)
465{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200466 struct perf_session *session = perf_session__new(input_name, O_RDONLY, force);
Li Zefanbab81b62009-12-01 14:04:49 +0800467 struct thread *idle;
468 int ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200469
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200470 if (session == NULL)
471 return -ENOMEM;
472
Li Zefanbab81b62009-12-01 14:04:49 +0800473 idle = register_idle_thread();
474 register_perf_file_handler(&file_handler);
Ingo Molnar8035e422009-06-06 15:19:13 +0200475
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200476 ret = perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
Li Zefanbab81b62009-12-01 14:04:49 +0800477 if (ret)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200478 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200479
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200480 if (dump_trace) {
481 event__print_totals();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200482 goto out_delete;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200483 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200484
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300485 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300486 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200487
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300488 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200489 dsos__fprintf(stdout);
490
491 collapse__resort();
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200492 output__resort(event__total[0]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200493
494 find_annotations();
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200495out_delete:
496 perf_session__delete(session);
Ingo Molnar8035e422009-06-06 15:19:13 +0200497
Li Zefanbab81b62009-12-01 14:04:49 +0800498 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200499}
500
501static const char * const annotate_usage[] = {
502 "perf annotate [<options>] <command>",
503 NULL
504};
505
506static const struct option options[] = {
507 OPT_STRING('i', "input", &input_name, "file",
508 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200509 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200510 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200511 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200512 OPT_BOOLEAN('v', "verbose", &verbose,
513 "be more verbose (show symbol address, etc)"),
514 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
515 "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200516 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
517 "file", "vmlinux pathname"),
518 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200519 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200520 OPT_BOOLEAN('l', "print-line", &print_line,
521 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200522 OPT_BOOLEAN('P', "full-paths", &full_paths,
523 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200524 OPT_END()
525};
526
527static void setup_sorting(void)
528{
529 char *tmp, *tok, *str = strdup(sort_order);
530
531 for (tok = strtok_r(str, ", ", &tmp);
532 tok; tok = strtok_r(NULL, ", ", &tmp)) {
533 if (sort_dimension__add(tok) < 0) {
534 error("Unknown --sort key: `%s'", tok);
535 usage_with_options(annotate_usage, options);
536 }
537 }
538
539 free(str);
540}
541
Ingo Molnarf37a2912009-07-01 12:37:06 +0200542int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200543{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200544 if (symbol__init(&symbol_conf) < 0)
545 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200546
Ingo Molnar8035e422009-06-06 15:19:13 +0200547 argc = parse_options(argc, argv, options, annotate_usage, 0);
548
549 setup_sorting();
550
Ingo Molnar0b73da32009-06-06 15:48:52 +0200551 if (argc) {
552 /*
553 * Special case: if there's an argument left then assume tha
554 * it's a symbol filter:
555 */
556 if (argc > 1)
557 usage_with_options(annotate_usage, options);
558
559 sym_hist_filter = argv[0];
560 }
561
Ingo Molnar8035e422009-06-06 15:19:13 +0200562 setup_pager();
563
John Kacurdd68ada2009-09-24 18:02:49 +0200564 if (field_sep && *field_sep == '.') {
565 fputs("'.' is the only non valid --field-separator argument\n",
566 stderr);
567 exit(129);
568 }
569
Ingo Molnar8035e422009-06-06 15:19:13 +0200570 return __cmd_annotate();
571}