blob: 59b6123abec2d9ec0d11193951642886b50a9fac [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
22#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"
John Kacure74328d2009-11-24 15:35:01 +010027#include "util/process_events.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020028
Ingo Molnar8035e422009-06-06 15:19:13 +020029static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020030
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020031static int force;
Ingo Molnar8035e422009-06-06 15:19:13 +020032static int input;
Ingo Molnar8035e422009-06-06 15:19:13 +020033
Mike Galbraith42976482009-07-02 08:09:46 +020034static int full_paths;
35
Frederic Weisbecker301406b2009-06-13 00:11:21 +020036static int print_line;
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -020037static bool use_modules;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020038
Ingo Molnar8035e422009-06-06 15:19:13 +020039static unsigned long page_size;
40static unsigned long mmap_window = 32;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020041const char *vmlinux_name;
Ingo Molnar8035e422009-06-06 15:19:13 +020042
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020043struct sym_hist {
44 u64 sum;
45 u64 ip[0];
46};
47
Frederic Weisbecker301406b2009-06-13 00:11:21 +020048struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020049 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020050 double percent;
51 char *path;
52};
53
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020054struct sym_priv {
55 struct sym_hist *hist;
56 struct sym_ext *ext;
57};
58
59static const char *sym_hist_filter;
60
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020061static int symbol_filter(struct map *map __used, struct symbol *sym)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020062{
Arnaldo Carvalho de Melo8f0b0372009-10-20 15:08:29 -020063 if (sym_hist_filter == NULL ||
64 strcmp(sym->name, sym_hist_filter) == 0) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020065 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020066 const int size = (sizeof(*priv->hist) +
67 (sym->end - sym->start) * sizeof(u64));
68
69 priv->hist = malloc(size);
70 if (priv->hist)
71 memset(priv->hist, 0, size);
72 return 0;
73 }
74 /*
75 * FIXME: We should really filter it out, as we don't want to go thru symbols
76 * we're not interested, and if a DSO ends up with no symbols, delete it too,
77 * but right now the kernel loading routines in symbol.c bail out if no symbols
78 * are found, fix it later.
79 */
80 return 0;
81}
Ingo Molnar8035e422009-06-06 15:19:13 +020082
Ingo Molnar8035e422009-06-06 15:19:13 +020083/*
84 * collect histogram counts
85 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100086static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020087{
88 unsigned int sym_size, offset;
89 struct symbol *sym = he->sym;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020090 struct sym_priv *priv;
91 struct sym_hist *h;
Ingo Molnar0b73da32009-06-06 15:48:52 +020092
93 he->count++;
94
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020095 if (!sym || !he->map)
96 return;
97
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020098 priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -020099 if (!priv->hist)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200100 return;
101
102 sym_size = sym->end - sym->start;
103 offset = ip - sym->start;
104
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200105 if (verbose)
106 fprintf(stderr, "%s: ip=%Lx\n", __func__,
107 he->map->unmap_ip(he->map, ip));
108
Ingo Molnar0b73da32009-06-06 15:48:52 +0200109 if (offset >= sym_size)
110 return;
111
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200112 h = priv->hist;
113 h->sum++;
114 h->ip[offset]++;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200115
116 if (verbose >= 3)
117 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200118 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200119 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200120 (void *)(unsigned long)ip, ip - he->sym->start,
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200121 h->ip[offset]);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200122}
Ingo Molnar8035e422009-06-06 15:19:13 +0200123
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300124static int hist_entry__add(struct thread *thread, struct map *map,
125 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200126{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300127 bool hit;
128 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
129 count, level, &hit);
130 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200131 return -ENOMEM;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200132 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200133 return 0;
134}
135
Ingo Molnar8035e422009-06-06 15:19:13 +0200136static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200137process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200138{
139 char level;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000140 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200141 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300142 struct symbol *sym = NULL;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300143 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200144
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200145 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200146 (void *)(offset + head),
147 (void *)(long)(event->header.size),
148 event->header.misc,
149 event->ip.pid,
150 (void *)(long)ip);
151
Ingo Molnar8035e422009-06-06 15:19:13 +0200152 if (thread == NULL) {
153 fprintf(stderr, "problem processing %d event, skipping it.\n",
154 event->header.type);
155 return -1;
156 }
157
Julia Lawallf39cdf22009-10-17 08:43:17 +0200158 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
159
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200160 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200161 level = 'k';
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200162 sym = kernel_maps__find_symbol(ip, &map, symbol_filter);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300163 dump_printf(" ...... dso: %s\n",
164 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200165 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200166 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200167 map = thread__find_map(thread, ip);
168 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300169got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200170 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200171 sym = map__find_symbol(map, ip, symbol_filter);
Ingo Molnar8035e422009-06-06 15:19:13 +0200172 } else {
173 /*
174 * If this is outside of all known maps,
175 * and is a negative address, try to look it
176 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300177 * vsyscall or vdso (which executes in user-mode).
178 *
179 * XXX This is nasty, we should have a symbol list in
180 * the "[vdso]" dso, but for now lets use the old
181 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200182 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300183 if ((long long)ip < 0) {
184 map = kernel_map;
185 goto got_map;
186 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200187 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300188 dump_printf(" ...... dso: %s\n",
189 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200190 } else {
Ingo Molnar8035e422009-06-06 15:19:13 +0200191 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200192 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200193 }
194
Arnaldo Carvalho de Meloec218fc2009-10-03 20:30:48 -0300195 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
196 fprintf(stderr, "problem incrementing symbol count, "
197 "skipping event\n");
198 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200199 }
200 total++;
201
202 return 0;
203}
204
205static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200206process_comm_event(event_t *event, unsigned long offset, unsigned long head)
207{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300208 struct thread *thread = threads__findnew(event->comm.pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200209
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200211 (void *)(offset + head),
212 (void *)(long)(event->header.size),
213 event->comm.comm, event->comm.pid);
214
215 if (thread == NULL ||
216 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200217 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200218 return -1;
219 }
220 total_comm++;
221
222 return 0;
223}
224
225static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200226process_event(event_t *event, unsigned long offset, unsigned long head)
227{
Ingo Molnar8035e422009-06-06 15:19:13 +0200228 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200229 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200230 return process_sample_event(event, offset, head);
231
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200232 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200233 return process_mmap_event(event, offset, head);
234
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200235 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200236 return process_comm_event(event, offset, head);
237
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200238 case PERF_RECORD_FORK:
John Kacure74328d2009-11-24 15:35:01 +0100239 return process_task_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200240 /*
241 * We dont process them right now but they are fine:
242 */
243
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200244 case PERF_RECORD_THROTTLE:
245 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200246 return 0;
247
248 default:
249 return -1;
250 }
251
252 return 0;
253}
254
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200255static int parse_line(FILE *file, struct hist_entry *he, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200256{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200257 struct symbol *sym = he->sym;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200258 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200259 static const char *prev_line;
260 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200261 unsigned int offset;
262 size_t line_len;
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200263 u64 start;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200264 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200265 int ret;
266 char *c;
267
268 if (getline(&line, &line_len, file) < 0)
269 return -1;
270 if (!line)
271 return -1;
272
273 c = strchr(line, '\n');
274 if (c)
275 *c = 0;
276
277 line_ip = -1;
278 offset = 0;
279 ret = -2;
280
281 /*
282 * Strip leading spaces:
283 */
284 tmp = line;
285 while (*tmp) {
286 if (*tmp != ' ')
287 break;
288 tmp++;
289 }
290
291 if (*tmp) {
292 /*
293 * Parse hexa addresses followed by ':'
294 */
295 line_ip = strtoull(tmp, &tmp2, 16);
296 if (*tmp2 != ':')
297 line_ip = -1;
298 }
299
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200300 start = he->map->unmap_ip(he->map, sym->start);
301
Ingo Molnar0b73da32009-06-06 15:48:52 +0200302 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200303 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200304 unsigned int hits = 0;
305 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200306 const char *color;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200307 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200308 struct sym_ext *sym_ext = priv->ext;
309 struct sym_hist *h = priv->hist;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200310
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200311 offset = line_ip - start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200312 if (offset < len)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200313 hits = h->ip[offset];
Ingo Molnar0b73da32009-06-06 15:48:52 +0200314
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200315 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200316 path = sym_ext[offset].path;
317 percent = sym_ext[offset].percent;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200318 } else if (h->sum)
319 percent = 100.0 * hits / h->sum;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200320
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200321 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200322
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200323 /*
324 * Also color the filename and line if needed, with
325 * the same color than the percentage. Don't print it
326 * twice for close colored ip with the same filename:line
327 */
328 if (path) {
329 if (!prev_line || strcmp(prev_line, path)
330 || color != prev_color) {
331 color_fprintf(stdout, color, " %s", path);
332 prev_line = path;
333 prev_color = color;
334 }
335 }
336
Ingo Molnar0b73da32009-06-06 15:48:52 +0200337 color_fprintf(stdout, color, " %7.2f", percent);
338 printf(" : ");
339 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
340 } else {
341 if (!*line)
342 printf(" :\n");
343 else
344 printf(" : %s\n", line);
345 }
346
347 return 0;
348}
349
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200350static struct rb_root root_sym_ext;
351
352static void insert_source_line(struct sym_ext *sym_ext)
353{
354 struct sym_ext *iter;
355 struct rb_node **p = &root_sym_ext.rb_node;
356 struct rb_node *parent = NULL;
357
358 while (*p != NULL) {
359 parent = *p;
360 iter = rb_entry(parent, struct sym_ext, node);
361
362 if (sym_ext->percent > iter->percent)
363 p = &(*p)->rb_left;
364 else
365 p = &(*p)->rb_right;
366 }
367
368 rb_link_node(&sym_ext->node, parent, p);
369 rb_insert_color(&sym_ext->node, &root_sym_ext);
370}
371
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200372static void free_source_line(struct hist_entry *he, int len)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200373{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200374 struct sym_priv *priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200375 struct sym_ext *sym_ext = priv->ext;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200376 int i;
377
378 if (!sym_ext)
379 return;
380
381 for (i = 0; i < len; i++)
382 free(sym_ext[i].path);
383 free(sym_ext);
384
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200385 priv->ext = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200386 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200387}
388
389/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200390static void
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200391get_source_line(struct hist_entry *he, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200392{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200393 struct symbol *sym = he->sym;
394 u64 start;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200395 int i;
396 char cmd[PATH_MAX * 2];
397 struct sym_ext *sym_ext;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200398 struct sym_priv *priv = symbol__priv(sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200399 struct sym_hist *h = priv->hist;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200400
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200401 if (!h->sum)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200402 return;
403
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200404 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
405 if (!priv->ext)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200406 return;
407
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200408 start = he->map->unmap_ip(he->map, sym->start);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200409
410 for (i = 0; i < len; i++) {
411 char *path = NULL;
412 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000413 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200414 FILE *fp;
415
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200416 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200417 if (sym_ext[i].percent <= 0.5)
418 continue;
419
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200420 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200421 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200422 fp = popen(cmd, "r");
423 if (!fp)
424 continue;
425
426 if (getline(&path, &line_len, fp) < 0 || !line_len)
427 goto next;
428
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200429 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200430 if (!sym_ext[i].path)
431 goto next;
432
433 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200434 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200435
436 next:
437 pclose(fp);
438 }
439}
440
Ingo Molnar83a09442009-08-15 12:26:57 +0200441static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200442{
443 struct sym_ext *sym_ext;
444 struct rb_node *node;
445
446 printf("\nSorted summary for file %s\n", filename);
447 printf("----------------------------------------------\n\n");
448
449 if (RB_EMPTY_ROOT(&root_sym_ext)) {
450 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
451 return;
452 }
453
454 node = rb_first(&root_sym_ext);
455 while (node) {
456 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200457 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200458 char *path;
459
460 sym_ext = rb_entry(node, struct sym_ext, node);
461 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200462 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200463 path = sym_ext->path;
464
465 color_fprintf(stdout, color, " %7.2f %s", percent, path);
466 node = rb_next(node);
467 }
468}
469
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200470static void annotate_sym(struct hist_entry *he)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200471{
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200472 struct map *map = he->map;
473 struct dso *dso = map->dso;
474 struct symbol *sym = he->sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300475 const char *filename = dso->long_name, *d_filename;
476 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200477 char command[PATH_MAX*2];
478 FILE *file;
479
480 if (!filename)
481 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200482
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200483 if (verbose)
484 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
485 __func__, filename, sym->name,
486 map->unmap_ip(map, sym->start),
487 map->unmap_ip(map, sym->end));
488
Mike Galbraith42976482009-07-02 08:09:46 +0200489 if (full_paths)
490 d_filename = filename;
491 else
492 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200493
Ingo Molnar0b73da32009-06-06 15:48:52 +0200494 len = sym->end - sym->start;
495
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200496 if (print_line) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200497 get_source_line(he, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200498 print_summary(filename);
499 }
500
501 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200502 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200503 printf("------------------------------------------------\n");
504
505 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300506 printf("annotating [%p] %30s : [%p] %30s\n",
507 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200508
Mike Galbraith42976482009-07-02 08:09:46 +0200509 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 -0200510 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
511 filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200512
513 if (verbose >= 3)
514 printf("doing: %s\n", command);
515
516 file = popen(command, "r");
517 if (!file)
518 return;
519
520 while (!feof(file)) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200521 if (parse_line(file, he, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200522 break;
523 }
524
525 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200526 if (print_line)
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200527 free_source_line(he, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200528}
529
530static void find_annotations(void)
531{
532 struct rb_node *nd;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200533
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200534 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
535 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200536 struct sym_priv *priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200537
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200538 if (he->sym == NULL)
539 continue;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200540
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200541 priv = symbol__priv(he->sym);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200542 if (priv->hist == NULL)
543 continue;
544
545 annotate_sym(he);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200546 /*
547 * Since we have a hist_entry per IP for the same symbol, free
548 * he->sym->hist to signal we already processed this symbol.
549 */
550 free(priv->hist);
551 priv->hist = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200552 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200553}
554
Ingo Molnar8035e422009-06-06 15:19:13 +0200555static int __cmd_annotate(void)
556{
557 int ret, rc = EXIT_FAILURE;
558 unsigned long offset = 0;
559 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200560 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200561 event_t *event;
562 uint32_t size;
563 char *buf;
564
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300565 register_idle_thread();
Ingo Molnar8035e422009-06-06 15:19:13 +0200566
567 input = open(input_name, O_RDONLY);
568 if (input < 0) {
569 perror("failed to open file");
570 exit(-1);
571 }
572
Ingo Molnar83a09442009-08-15 12:26:57 +0200573 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200574 if (ret < 0) {
575 perror("failed to stat file");
576 exit(-1);
577 }
578
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200579 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
580 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200581 exit(-1);
582 }
583
Ingo Molnar83a09442009-08-15 12:26:57 +0200584 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200585 fprintf(stderr, "zero-sized file, nothing to do!\n");
586 exit(0);
587 }
588
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -0200589 if (kernel_maps__init(vmlinux_name, true, use_modules) < 0) {
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200590 pr_err("failed to create kernel maps for symbol resolution\b");
591 return -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200592 }
593
Ingo Molnar8035e422009-06-06 15:19:13 +0200594remap:
595 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
596 MAP_SHARED, input, offset);
597 if (buf == MAP_FAILED) {
598 perror("failed to mmap file");
599 exit(-1);
600 }
601
602more:
603 event = (event_t *)(buf + head);
604
605 size = event->header.size;
606 if (!size)
607 size = 8;
608
609 if (head + event->header.size >= page_size * mmap_window) {
610 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200611 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200612
Ingo Molnar83a09442009-08-15 12:26:57 +0200613 munmap_ret = munmap(buf, page_size * mmap_window);
614 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200615
616 offset += shift;
617 head -= shift;
618 goto remap;
619 }
620
621 size = event->header.size;
622
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200623 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200624 (void *)(offset + head),
625 (void *)(long)event->header.size,
626 event->header.type);
627
628 if (!size || process_event(event, offset, head) < 0) {
629
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200630 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200631 (void *)(offset + head),
632 (void *)(long)(event->header.size),
633 event->header.type);
634
635 total_unknown++;
636
637 /*
638 * assume we lost track of the stream, check alignment, and
639 * increment a single u64 in the hope to catch on again 'soon'.
640 */
641
642 if (unlikely(head & 7))
643 head &= ~7ULL;
644
645 size = 8;
646 }
647
648 head += size;
649
Ingo Molnar83a09442009-08-15 12:26:57 +0200650 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200651 goto more;
652
653 rc = EXIT_SUCCESS;
654 close(input);
655
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200656 dump_printf(" IP events: %10ld\n", total);
657 dump_printf(" mmap events: %10ld\n", total_mmap);
658 dump_printf(" comm events: %10ld\n", total_comm);
659 dump_printf(" fork events: %10ld\n", total_fork);
660 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200661
662 if (dump_trace)
663 return 0;
664
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300665 if (verbose > 3)
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300666 threads__fprintf(stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200667
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300668 if (verbose > 2)
Ingo Molnar8035e422009-06-06 15:19:13 +0200669 dsos__fprintf(stdout);
670
671 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200672 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200673
674 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200675
676 return rc;
677}
678
679static const char * const annotate_usage[] = {
680 "perf annotate [<options>] <command>",
681 NULL
682};
683
684static const struct option options[] = {
685 OPT_STRING('i', "input", &input_name, "file",
686 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200687 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200688 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200689 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200690 OPT_BOOLEAN('v', "verbose", &verbose,
691 "be more verbose (show symbol address, etc)"),
692 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
693 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200694 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -0200695 OPT_BOOLEAN('m', "modules", &use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200696 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200697 OPT_BOOLEAN('l', "print-line", &print_line,
698 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200699 OPT_BOOLEAN('P', "full-paths", &full_paths,
700 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200701 OPT_END()
702};
703
704static void setup_sorting(void)
705{
706 char *tmp, *tok, *str = strdup(sort_order);
707
708 for (tok = strtok_r(str, ", ", &tmp);
709 tok; tok = strtok_r(NULL, ", ", &tmp)) {
710 if (sort_dimension__add(tok) < 0) {
711 error("Unknown --sort key: `%s'", tok);
712 usage_with_options(annotate_usage, options);
713 }
714 }
715
716 free(str);
717}
718
Ingo Molnarf37a2912009-07-01 12:37:06 +0200719int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200720{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200721 symbol__init(sizeof(struct sym_priv));
Ingo Molnar8035e422009-06-06 15:19:13 +0200722
723 page_size = getpagesize();
724
725 argc = parse_options(argc, argv, options, annotate_usage, 0);
726
727 setup_sorting();
728
Ingo Molnar0b73da32009-06-06 15:48:52 +0200729 if (argc) {
730 /*
731 * Special case: if there's an argument left then assume tha
732 * it's a symbol filter:
733 */
734 if (argc > 1)
735 usage_with_options(annotate_usage, options);
736
737 sym_hist_filter = argv[0];
738 }
739
Ingo Molnar8035e422009-06-06 15:19:13 +0200740 setup_pager();
741
John Kacurdd68ada2009-09-24 18:02:49 +0200742 if (field_sep && *field_sep == '.') {
743 fputs("'.' is the only non valid --field-separator argument\n",
744 stderr);
745 exit(129);
746 }
747
Ingo Molnar8035e422009-06-06 15:19:13 +0200748 return __cmd_annotate();
749}