blob: 855094234f2d25fa652b8e97762b731c8ba293a6 [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"
Ingo Molnar8035e422009-06-06 15:19:13 +020027
Ingo Molnar8035e422009-06-06 15:19:13 +020028static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020029
Peter Zijlstrafa6963b2009-08-19 11:18:26 +020030static int force;
Ingo Molnar8035e422009-06-06 15:19:13 +020031static int input;
32static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
33
Mike Galbraith42976482009-07-02 08:09:46 +020034static int full_paths;
35
Frederic Weisbecker301406b2009-06-13 00:11:21 +020036static int print_line;
37
Ingo Molnar8035e422009-06-06 15:19:13 +020038static unsigned long page_size;
39static unsigned long mmap_window = 32;
40
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020041static struct rb_root threads;
42static struct thread *last_match;
43
Frederic Weisbecker301406b2009-06-13 00:11:21 +020044
45struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020046 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020047 double percent;
48 char *path;
49};
50
Ingo Molnar8035e422009-06-06 15:19:13 +020051
Ingo Molnar8035e422009-06-06 15:19:13 +020052/*
53 * collect histogram counts
54 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100055static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +020056{
57 unsigned int sym_size, offset;
58 struct symbol *sym = he->sym;
59
60 he->count++;
61
62 if (!sym || !sym->hist)
63 return;
64
65 sym_size = sym->end - sym->start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030066 ip = he->map->map_ip(he->map, ip);
Ingo Molnar0b73da32009-06-06 15:48:52 +020067 offset = ip - sym->start;
68
69 if (offset >= sym_size)
70 return;
71
72 sym->hist_sum++;
73 sym->hist[offset]++;
74
75 if (verbose >= 3)
76 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020077 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020078 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020079 (void *)(unsigned long)ip, ip - he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020080 sym->hist[offset]);
81}
Ingo Molnar8035e422009-06-06 15:19:13 +020082
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030083static int hist_entry__add(struct thread *thread, struct map *map,
84 struct symbol *sym, u64 ip, u64 count, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +020085{
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030086 bool hit;
87 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
88 count, level, &hit);
89 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +020090 return -ENOMEM;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030091 if (hit)
92 hist_hit(he, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +020093 return 0;
94}
95
Ingo Molnar8035e422009-06-06 15:19:13 +020096static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +020097process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +020098{
99 char level;
100 int show = 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200101 struct thread *thread;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000102 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200103 struct map *map = NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300104 struct symbol *sym = NULL;
Ingo Molnar8035e422009-06-06 15:19:13 +0200105
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200106 thread = threads__findnew(event->ip.pid, &threads, &last_match);
107
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200108 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200109 (void *)(offset + head),
110 (void *)(long)(event->header.size),
111 event->header.misc,
112 event->ip.pid,
113 (void *)(long)ip);
114
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200115 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200116
117 if (thread == NULL) {
118 fprintf(stderr, "problem processing %d event, skipping it.\n",
119 event->header.type);
120 return -1;
121 }
122
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200123 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200124 show = SHOW_KERNEL;
125 level = 'k';
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300126 sym = kernel_maps__find_symbol(ip, &map);
127 dump_printf(" ...... dso: %s\n",
128 map ? map->dso->long_name : "<not found>");
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200129 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200130 show = SHOW_USER;
131 level = '.';
Ingo Molnar8035e422009-06-06 15:19:13 +0200132 map = thread__find_map(thread, ip);
133 if (map != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300134got_map:
Ingo Molnar8035e422009-06-06 15:19:13 +0200135 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300136 sym = map->dso->find_symbol(map->dso, ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200137 } else {
138 /*
139 * If this is outside of all known maps,
140 * and is a negative address, try to look it
141 * up in the kernel dso, as it might be a
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300142 * vsyscall or vdso (which executes in user-mode).
143 *
144 * XXX This is nasty, we should have a symbol list in
145 * the "[vdso]" dso, but for now lets use the old
146 * trick of looking in the whole kernel symbol list.
Ingo Molnar8035e422009-06-06 15:19:13 +0200147 */
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300148 if ((long long)ip < 0) {
149 map = kernel_map;
150 goto got_map;
151 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200152 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300153 dump_printf(" ...... dso: %s\n",
154 map ? map->dso->long_name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200155 } else {
156 show = SHOW_HV;
157 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200158 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200159 }
160
161 if (show & show_mask) {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300162 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200163 fprintf(stderr,
164 "problem incrementing symbol count, skipping event\n");
165 return -1;
166 }
167 }
168 total++;
169
170 return 0;
171}
172
173static int
174process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
175{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200176 struct thread *thread;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200177 struct map *map = map__new(&event->mmap, NULL, 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200178
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200179 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
180
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200181 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200182 (void *)(offset + head),
183 (void *)(long)(event->header.size),
184 event->mmap.pid,
185 (void *)(long)event->mmap.start,
186 (void *)(long)event->mmap.len,
187 (void *)(long)event->mmap.pgoff,
188 event->mmap.filename);
189
190 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200191 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200192 return 0;
193 }
194
195 thread__insert_map(thread, map);
196 total_mmap++;
197
198 return 0;
199}
200
201static int
202process_comm_event(event_t *event, unsigned long offset, unsigned long head)
203{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200204 struct thread *thread;
Ingo Molnar8035e422009-06-06 15:19:13 +0200205
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200206 thread = threads__findnew(event->comm.pid, &threads, &last_match);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200208 (void *)(offset + head),
209 (void *)(long)(event->header.size),
210 event->comm.comm, event->comm.pid);
211
212 if (thread == NULL ||
213 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200215 return -1;
216 }
217 total_comm++;
218
219 return 0;
220}
221
222static int
223process_fork_event(event_t *event, unsigned long offset, unsigned long head)
224{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200225 struct thread *thread;
226 struct thread *parent;
Ingo Molnar8035e422009-06-06 15:19:13 +0200227
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200228 thread = threads__findnew(event->fork.pid, &threads, &last_match);
229 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200230 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200231 (void *)(offset + head),
232 (void *)(long)(event->header.size),
233 event->fork.pid, event->fork.ppid);
234
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200235 /*
236 * A thread clone will have the same PID for both
237 * parent and child.
238 */
239 if (thread == parent)
240 return 0;
241
Ingo Molnar8035e422009-06-06 15:19:13 +0200242 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200243 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200244 return -1;
245 }
246 total_fork++;
247
248 return 0;
249}
250
251static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200252process_event(event_t *event, unsigned long offset, unsigned long head)
253{
Ingo Molnar8035e422009-06-06 15:19:13 +0200254 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200255 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200256 return process_sample_event(event, offset, head);
257
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200258 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200259 return process_mmap_event(event, offset, head);
260
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200261 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200262 return process_comm_event(event, offset, head);
263
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200264 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200265 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200266 /*
267 * We dont process them right now but they are fine:
268 */
269
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270 case PERF_RECORD_THROTTLE:
271 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200272 return 0;
273
274 default:
275 return -1;
276 }
277
278 return 0;
279}
280
Ingo Molnar0b73da32009-06-06 15:48:52 +0200281static int
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300282parse_line(FILE *file, struct symbol *sym, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200283{
284 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200285 static const char *prev_line;
286 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200287 unsigned int offset;
288 size_t line_len;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200289 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200290 int ret;
291 char *c;
292
293 if (getline(&line, &line_len, file) < 0)
294 return -1;
295 if (!line)
296 return -1;
297
298 c = strchr(line, '\n');
299 if (c)
300 *c = 0;
301
302 line_ip = -1;
303 offset = 0;
304 ret = -2;
305
306 /*
307 * Strip leading spaces:
308 */
309 tmp = line;
310 while (*tmp) {
311 if (*tmp != ' ')
312 break;
313 tmp++;
314 }
315
316 if (*tmp) {
317 /*
318 * Parse hexa addresses followed by ':'
319 */
320 line_ip = strtoull(tmp, &tmp2, 16);
321 if (*tmp2 != ':')
322 line_ip = -1;
323 }
324
325 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200326 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200327 unsigned int hits = 0;
328 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200329 const char *color;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200330 struct sym_ext *sym_ext = sym->priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200331
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300332 offset = line_ip - sym->start;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200333 if (offset < len)
334 hits = sym->hist[offset];
335
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200336 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200337 path = sym_ext[offset].path;
338 percent = sym_ext[offset].percent;
339 } else if (sym->hist_sum)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200340 percent = 100.0 * hits / sym->hist_sum;
341
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200342 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200343
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200344 /*
345 * Also color the filename and line if needed, with
346 * the same color than the percentage. Don't print it
347 * twice for close colored ip with the same filename:line
348 */
349 if (path) {
350 if (!prev_line || strcmp(prev_line, path)
351 || color != prev_color) {
352 color_fprintf(stdout, color, " %s", path);
353 prev_line = path;
354 prev_color = color;
355 }
356 }
357
Ingo Molnar0b73da32009-06-06 15:48:52 +0200358 color_fprintf(stdout, color, " %7.2f", percent);
359 printf(" : ");
360 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
361 } else {
362 if (!*line)
363 printf(" :\n");
364 else
365 printf(" : %s\n", line);
366 }
367
368 return 0;
369}
370
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200371static struct rb_root root_sym_ext;
372
373static void insert_source_line(struct sym_ext *sym_ext)
374{
375 struct sym_ext *iter;
376 struct rb_node **p = &root_sym_ext.rb_node;
377 struct rb_node *parent = NULL;
378
379 while (*p != NULL) {
380 parent = *p;
381 iter = rb_entry(parent, struct sym_ext, node);
382
383 if (sym_ext->percent > iter->percent)
384 p = &(*p)->rb_left;
385 else
386 p = &(*p)->rb_right;
387 }
388
389 rb_link_node(&sym_ext->node, parent, p);
390 rb_insert_color(&sym_ext->node, &root_sym_ext);
391}
392
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200393static void free_source_line(struct symbol *sym, int len)
394{
395 struct sym_ext *sym_ext = sym->priv;
396 int i;
397
398 if (!sym_ext)
399 return;
400
401 for (i = 0; i < len; i++)
402 free(sym_ext[i].path);
403 free(sym_ext);
404
405 sym->priv = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200406 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200407}
408
409/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200410static void
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300411get_source_line(struct symbol *sym, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200412{
413 int i;
414 char cmd[PATH_MAX * 2];
415 struct sym_ext *sym_ext;
416
417 if (!sym->hist_sum)
418 return;
419
420 sym->priv = calloc(len, sizeof(struct sym_ext));
421 if (!sym->priv)
422 return;
423
424 sym_ext = sym->priv;
425
426 for (i = 0; i < len; i++) {
427 char *path = NULL;
428 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000429 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200430 FILE *fp;
431
432 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
433 if (sym_ext[i].percent <= 0.5)
434 continue;
435
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300436 offset = sym->start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200437 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200438 fp = popen(cmd, "r");
439 if (!fp)
440 continue;
441
442 if (getline(&path, &line_len, fp) < 0 || !line_len)
443 goto next;
444
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200445 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200446 if (!sym_ext[i].path)
447 goto next;
448
449 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200450 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200451
452 next:
453 pclose(fp);
454 }
455}
456
Ingo Molnar83a09442009-08-15 12:26:57 +0200457static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200458{
459 struct sym_ext *sym_ext;
460 struct rb_node *node;
461
462 printf("\nSorted summary for file %s\n", filename);
463 printf("----------------------------------------------\n\n");
464
465 if (RB_EMPTY_ROOT(&root_sym_ext)) {
466 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
467 return;
468 }
469
470 node = rb_first(&root_sym_ext);
471 while (node) {
472 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200473 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200474 char *path;
475
476 sym_ext = rb_entry(node, struct sym_ext, node);
477 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200478 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200479 path = sym_ext->path;
480
481 color_fprintf(stdout, color, " %7.2f %s", percent, path);
482 node = rb_next(node);
483 }
484}
485
Ingo Molnar0b73da32009-06-06 15:48:52 +0200486static void annotate_sym(struct dso *dso, struct symbol *sym)
487{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300488 const char *filename = dso->long_name, *d_filename;
489 u64 len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200490 char command[PATH_MAX*2];
491 FILE *file;
492
493 if (!filename)
494 return;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200495
Mike Galbraith42976482009-07-02 08:09:46 +0200496 if (full_paths)
497 d_filename = filename;
498 else
499 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200500
Ingo Molnar0b73da32009-06-06 15:48:52 +0200501 len = sym->end - sym->start;
502
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200503 if (print_line) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300504 get_source_line(sym, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200505 print_summary(filename);
506 }
507
508 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200509 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200510 printf("------------------------------------------------\n");
511
512 if (verbose >= 2)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300513 printf("annotating [%p] %30s : [%p] %30s\n",
514 dso, dso->long_name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200515
Mike Galbraith42976482009-07-02 08:09:46 +0200516 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300517 sym->start, sym->end, filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200518
519 if (verbose >= 3)
520 printf("doing: %s\n", command);
521
522 file = popen(command, "r");
523 if (!file)
524 return;
525
526 while (!feof(file)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300527 if (parse_line(file, sym, len) < 0)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200528 break;
529 }
530
531 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200532 if (print_line)
533 free_source_line(sym, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200534}
535
536static void find_annotations(void)
537{
538 struct rb_node *nd;
539 struct dso *dso;
540 int count = 0;
541
542 list_for_each_entry(dso, &dsos, node) {
543
544 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
545 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
546
547 if (sym->hist) {
548 annotate_sym(dso, sym);
549 count++;
550 }
551 }
552 }
553
554 if (!count)
555 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
556}
557
Ingo Molnar8035e422009-06-06 15:19:13 +0200558static int __cmd_annotate(void)
559{
560 int ret, rc = EXIT_FAILURE;
561 unsigned long offset = 0;
562 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200563 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200564 event_t *event;
565 uint32_t size;
566 char *buf;
567
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200568 register_idle_thread(&threads, &last_match);
Ingo Molnar8035e422009-06-06 15:19:13 +0200569
570 input = open(input_name, O_RDONLY);
571 if (input < 0) {
572 perror("failed to open file");
573 exit(-1);
574 }
575
Ingo Molnar83a09442009-08-15 12:26:57 +0200576 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200577 if (ret < 0) {
578 perror("failed to stat file");
579 exit(-1);
580 }
581
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200582 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
583 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200584 exit(-1);
585 }
586
Ingo Molnar83a09442009-08-15 12:26:57 +0200587 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200588 fprintf(stderr, "zero-sized file, nothing to do!\n");
589 exit(0);
590 }
591
592 if (load_kernel() < 0) {
593 perror("failed to load kernel symbols");
594 return EXIT_FAILURE;
595 }
596
Ingo Molnar8035e422009-06-06 15:19:13 +0200597remap:
598 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
599 MAP_SHARED, input, offset);
600 if (buf == MAP_FAILED) {
601 perror("failed to mmap file");
602 exit(-1);
603 }
604
605more:
606 event = (event_t *)(buf + head);
607
608 size = event->header.size;
609 if (!size)
610 size = 8;
611
612 if (head + event->header.size >= page_size * mmap_window) {
613 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200614 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200615
Ingo Molnar83a09442009-08-15 12:26:57 +0200616 munmap_ret = munmap(buf, page_size * mmap_window);
617 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200618
619 offset += shift;
620 head -= shift;
621 goto remap;
622 }
623
624 size = event->header.size;
625
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200626 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200627 (void *)(offset + head),
628 (void *)(long)event->header.size,
629 event->header.type);
630
631 if (!size || process_event(event, offset, head) < 0) {
632
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200633 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200634 (void *)(offset + head),
635 (void *)(long)(event->header.size),
636 event->header.type);
637
638 total_unknown++;
639
640 /*
641 * assume we lost track of the stream, check alignment, and
642 * increment a single u64 in the hope to catch on again 'soon'.
643 */
644
645 if (unlikely(head & 7))
646 head &= ~7ULL;
647
648 size = 8;
649 }
650
651 head += size;
652
Ingo Molnar83a09442009-08-15 12:26:57 +0200653 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200654 goto more;
655
656 rc = EXIT_SUCCESS;
657 close(input);
658
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200659 dump_printf(" IP events: %10ld\n", total);
660 dump_printf(" mmap events: %10ld\n", total_mmap);
661 dump_printf(" comm events: %10ld\n", total_comm);
662 dump_printf(" fork events: %10ld\n", total_fork);
663 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200664
665 if (dump_trace)
666 return 0;
667
668 if (verbose >= 3)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200669 threads__fprintf(stdout, &threads);
Ingo Molnar8035e422009-06-06 15:19:13 +0200670
671 if (verbose >= 2)
672 dsos__fprintf(stdout);
673
674 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200675 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200676
677 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200678
679 return rc;
680}
681
682static const char * const annotate_usage[] = {
683 "perf annotate [<options>] <command>",
684 NULL
685};
686
687static const struct option options[] = {
688 OPT_STRING('i', "input", &input_name, "file",
689 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200690 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200691 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200692 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200693 OPT_BOOLEAN('v', "verbose", &verbose,
694 "be more verbose (show symbol address, etc)"),
695 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
696 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200697 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200698 OPT_BOOLEAN('m', "modules", &modules,
699 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200700 OPT_BOOLEAN('l', "print-line", &print_line,
701 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200702 OPT_BOOLEAN('P', "full-paths", &full_paths,
703 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200704 OPT_END()
705};
706
707static void setup_sorting(void)
708{
709 char *tmp, *tok, *str = strdup(sort_order);
710
711 for (tok = strtok_r(str, ", ", &tmp);
712 tok; tok = strtok_r(NULL, ", ", &tmp)) {
713 if (sort_dimension__add(tok) < 0) {
714 error("Unknown --sort key: `%s'", tok);
715 usage_with_options(annotate_usage, options);
716 }
717 }
718
719 free(str);
720}
721
Ingo Molnarf37a2912009-07-01 12:37:06 +0200722int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200723{
724 symbol__init();
725
726 page_size = getpagesize();
727
728 argc = parse_options(argc, argv, options, annotate_usage, 0);
729
730 setup_sorting();
731
Ingo Molnar0b73da32009-06-06 15:48:52 +0200732 if (argc) {
733 /*
734 * Special case: if there's an argument left then assume tha
735 * it's a symbol filter:
736 */
737 if (argc > 1)
738 usage_with_options(annotate_usage, options);
739
740 sym_hist_filter = argv[0];
741 }
742
743 if (!sym_hist_filter)
Ingo Molnar8035e422009-06-06 15:19:13 +0200744 usage_with_options(annotate_usage, options);
745
746 setup_pager();
747
John Kacurdd68ada2009-09-24 18:02:49 +0200748 if (field_sep && *field_sep == '.') {
749 fputs("'.' is the only non valid --field-separator argument\n",
750 stderr);
751 exit(129);
752 }
753
Ingo Molnar8035e422009-06-06 15:19:13 +0200754 return __cmd_annotate();
755}