blob: df516dce954032ac112c46b31f220c5647e53852 [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;
66 offset = ip - sym->start;
67
68 if (offset >= sym_size)
69 return;
70
71 sym->hist_sum++;
72 sym->hist[offset]++;
73
74 if (verbose >= 3)
75 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020076 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020077 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +020078 (void *)(unsigned long)ip, ip - he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +020079 sym->hist[offset]);
80}
Ingo Molnar8035e422009-06-06 15:19:13 +020081
82static int
83hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100084 struct symbol *sym, u64 ip, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +020085{
86 struct rb_node **p = &hist.rb_node;
87 struct rb_node *parent = NULL;
88 struct hist_entry *he;
89 struct hist_entry entry = {
90 .thread = thread,
91 .map = map,
92 .dso = dso,
93 .sym = sym,
94 .ip = ip,
95 .level = level,
96 .count = 1,
97 };
98 int cmp;
99
100 while (*p != NULL) {
101 parent = *p;
102 he = rb_entry(parent, struct hist_entry, rb_node);
103
104 cmp = hist_entry__cmp(&entry, he);
105
106 if (!cmp) {
Ingo Molnar0b73da32009-06-06 15:48:52 +0200107 hist_hit(he, ip);
108
Ingo Molnar8035e422009-06-06 15:19:13 +0200109 return 0;
110 }
111
112 if (cmp < 0)
113 p = &(*p)->rb_left;
114 else
115 p = &(*p)->rb_right;
116 }
117
118 he = malloc(sizeof(*he));
119 if (!he)
120 return -ENOMEM;
121 *he = entry;
122 rb_link_node(&he->rb_node, parent, p);
123 rb_insert_color(&he->rb_node, &hist);
124
125 return 0;
126}
127
Ingo Molnar8035e422009-06-06 15:19:13 +0200128static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200129process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200130{
131 char level;
132 int show = 0;
133 struct dso *dso = NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200134 struct thread *thread;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000135 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200136 struct map *map = NULL;
137
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200138 thread = threads__findnew(event->ip.pid, &threads, &last_match);
139
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200140 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200141 (void *)(offset + head),
142 (void *)(long)(event->header.size),
143 event->header.misc,
144 event->ip.pid,
145 (void *)(long)ip);
146
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200147 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200148
149 if (thread == NULL) {
150 fprintf(stderr, "problem processing %d event, skipping it.\n",
151 event->header.type);
152 return -1;
153 }
154
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200155 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200156 show = SHOW_KERNEL;
157 level = 'k';
158
159 dso = kernel_dso;
160
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200161 dump_printf(" ...... dso: %s\n", dso->name);
Ingo Molnar8035e422009-06-06 15:19:13 +0200162
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200163 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200164
165 show = SHOW_USER;
166 level = '.';
167
168 map = thread__find_map(thread, ip);
169 if (map != NULL) {
170 ip = map->map_ip(map, ip);
171 dso = map->dso;
172 } 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
177 * vsyscall (which executes in user-mode):
178 */
179 if ((long long)ip < 0)
180 dso = kernel_dso;
181 }
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200182 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200183
184 } else {
185 show = SHOW_HV;
186 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200187 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200188 }
189
190 if (show & show_mask) {
191 struct symbol *sym = NULL;
192
193 if (dso)
194 sym = dso->find_symbol(dso, ip);
195
196 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
197 fprintf(stderr,
198 "problem incrementing symbol count, skipping event\n");
199 return -1;
200 }
201 }
202 total++;
203
204 return 0;
205}
206
207static int
208process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
209{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200210 struct thread *thread;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200211 struct map *map = map__new(&event->mmap, NULL, 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200212
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200213 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
214
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200215 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200216 (void *)(offset + head),
217 (void *)(long)(event->header.size),
218 event->mmap.pid,
219 (void *)(long)event->mmap.start,
220 (void *)(long)event->mmap.len,
221 (void *)(long)event->mmap.pgoff,
222 event->mmap.filename);
223
224 if (thread == NULL || map == NULL) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200225 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200226 return 0;
227 }
228
229 thread__insert_map(thread, map);
230 total_mmap++;
231
232 return 0;
233}
234
235static int
236process_comm_event(event_t *event, unsigned long offset, unsigned long head)
237{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200238 struct thread *thread;
Ingo Molnar8035e422009-06-06 15:19:13 +0200239
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200240 thread = threads__findnew(event->comm.pid, &threads, &last_match);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200241 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200242 (void *)(offset + head),
243 (void *)(long)(event->header.size),
244 event->comm.comm, event->comm.pid);
245
246 if (thread == NULL ||
247 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200248 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200249 return -1;
250 }
251 total_comm++;
252
253 return 0;
254}
255
256static int
257process_fork_event(event_t *event, unsigned long offset, unsigned long head)
258{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200259 struct thread *thread;
260 struct thread *parent;
Ingo Molnar8035e422009-06-06 15:19:13 +0200261
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200262 thread = threads__findnew(event->fork.pid, &threads, &last_match);
263 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200264 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200265 (void *)(offset + head),
266 (void *)(long)(event->header.size),
267 event->fork.pid, event->fork.ppid);
268
Ingo Molnar15f3fa42009-08-18 13:52:28 +0200269 /*
270 * A thread clone will have the same PID for both
271 * parent and child.
272 */
273 if (thread == parent)
274 return 0;
275
Ingo Molnar8035e422009-06-06 15:19:13 +0200276 if (!thread || !parent || thread__fork(thread, parent)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200277 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200278 return -1;
279 }
280 total_fork++;
281
282 return 0;
283}
284
285static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200286process_event(event_t *event, unsigned long offset, unsigned long head)
287{
Ingo Molnar8035e422009-06-06 15:19:13 +0200288 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200289 case PERF_RECORD_SAMPLE:
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200290 return process_sample_event(event, offset, head);
291
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200292 case PERF_RECORD_MMAP:
Ingo Molnar8035e422009-06-06 15:19:13 +0200293 return process_mmap_event(event, offset, head);
294
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200295 case PERF_RECORD_COMM:
Ingo Molnar8035e422009-06-06 15:19:13 +0200296 return process_comm_event(event, offset, head);
297
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200298 case PERF_RECORD_FORK:
Ingo Molnar8035e422009-06-06 15:19:13 +0200299 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200300 /*
301 * We dont process them right now but they are fine:
302 */
303
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200304 case PERF_RECORD_THROTTLE:
305 case PERF_RECORD_UNTHROTTLE:
Ingo Molnar8035e422009-06-06 15:19:13 +0200306 return 0;
307
308 default:
309 return -1;
310 }
311
312 return 0;
313}
314
Ingo Molnar0b73da32009-06-06 15:48:52 +0200315static int
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000316parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200317{
318 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200319 static const char *prev_line;
320 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200321 unsigned int offset;
322 size_t line_len;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200323 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200324 int ret;
325 char *c;
326
327 if (getline(&line, &line_len, file) < 0)
328 return -1;
329 if (!line)
330 return -1;
331
332 c = strchr(line, '\n');
333 if (c)
334 *c = 0;
335
336 line_ip = -1;
337 offset = 0;
338 ret = -2;
339
340 /*
341 * Strip leading spaces:
342 */
343 tmp = line;
344 while (*tmp) {
345 if (*tmp != ' ')
346 break;
347 tmp++;
348 }
349
350 if (*tmp) {
351 /*
352 * Parse hexa addresses followed by ':'
353 */
354 line_ip = strtoull(tmp, &tmp2, 16);
355 if (*tmp2 != ':')
356 line_ip = -1;
357 }
358
359 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200360 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200361 unsigned int hits = 0;
362 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200363 const char *color;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200364 struct sym_ext *sym_ext = sym->priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200365
366 offset = line_ip - start;
367 if (offset < len)
368 hits = sym->hist[offset];
369
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200370 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200371 path = sym_ext[offset].path;
372 percent = sym_ext[offset].percent;
373 } else if (sym->hist_sum)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200374 percent = 100.0 * hits / sym->hist_sum;
375
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200376 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200377
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200378 /*
379 * Also color the filename and line if needed, with
380 * the same color than the percentage. Don't print it
381 * twice for close colored ip with the same filename:line
382 */
383 if (path) {
384 if (!prev_line || strcmp(prev_line, path)
385 || color != prev_color) {
386 color_fprintf(stdout, color, " %s", path);
387 prev_line = path;
388 prev_color = color;
389 }
390 }
391
Ingo Molnar0b73da32009-06-06 15:48:52 +0200392 color_fprintf(stdout, color, " %7.2f", percent);
393 printf(" : ");
394 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
395 } else {
396 if (!*line)
397 printf(" :\n");
398 else
399 printf(" : %s\n", line);
400 }
401
402 return 0;
403}
404
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200405static struct rb_root root_sym_ext;
406
407static void insert_source_line(struct sym_ext *sym_ext)
408{
409 struct sym_ext *iter;
410 struct rb_node **p = &root_sym_ext.rb_node;
411 struct rb_node *parent = NULL;
412
413 while (*p != NULL) {
414 parent = *p;
415 iter = rb_entry(parent, struct sym_ext, node);
416
417 if (sym_ext->percent > iter->percent)
418 p = &(*p)->rb_left;
419 else
420 p = &(*p)->rb_right;
421 }
422
423 rb_link_node(&sym_ext->node, parent, p);
424 rb_insert_color(&sym_ext->node, &root_sym_ext);
425}
426
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200427static void free_source_line(struct symbol *sym, int len)
428{
429 struct sym_ext *sym_ext = sym->priv;
430 int i;
431
432 if (!sym_ext)
433 return;
434
435 for (i = 0; i < len; i++)
436 free(sym_ext[i].path);
437 free(sym_ext);
438
439 sym->priv = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200440 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200441}
442
443/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200444static void
Ingo Molnar83a09442009-08-15 12:26:57 +0200445get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200446{
447 int i;
448 char cmd[PATH_MAX * 2];
449 struct sym_ext *sym_ext;
450
451 if (!sym->hist_sum)
452 return;
453
454 sym->priv = calloc(len, sizeof(struct sym_ext));
455 if (!sym->priv)
456 return;
457
458 sym_ext = sym->priv;
459
460 for (i = 0; i < len; i++) {
461 char *path = NULL;
462 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000463 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200464 FILE *fp;
465
466 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
467 if (sym_ext[i].percent <= 0.5)
468 continue;
469
470 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200471 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200472 fp = popen(cmd, "r");
473 if (!fp)
474 continue;
475
476 if (getline(&path, &line_len, fp) < 0 || !line_len)
477 goto next;
478
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200479 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200480 if (!sym_ext[i].path)
481 goto next;
482
483 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200484 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200485
486 next:
487 pclose(fp);
488 }
489}
490
Ingo Molnar83a09442009-08-15 12:26:57 +0200491static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200492{
493 struct sym_ext *sym_ext;
494 struct rb_node *node;
495
496 printf("\nSorted summary for file %s\n", filename);
497 printf("----------------------------------------------\n\n");
498
499 if (RB_EMPTY_ROOT(&root_sym_ext)) {
500 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
501 return;
502 }
503
504 node = rb_first(&root_sym_ext);
505 while (node) {
506 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200507 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200508 char *path;
509
510 sym_ext = rb_entry(node, struct sym_ext, node);
511 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200512 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200513 path = sym_ext->path;
514
515 color_fprintf(stdout, color, " %7.2f %s", percent, path);
516 node = rb_next(node);
517 }
518}
519
Ingo Molnar0b73da32009-06-06 15:48:52 +0200520static void annotate_sym(struct dso *dso, struct symbol *sym)
521{
Ingo Molnar83a09442009-08-15 12:26:57 +0200522 const char *filename = dso->name, *d_filename;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000523 u64 start, end, len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200524 char command[PATH_MAX*2];
525 FILE *file;
526
527 if (!filename)
528 return;
Mike Galbraith42976482009-07-02 08:09:46 +0200529 if (sym->module)
530 filename = sym->module->path;
531 else if (dso == kernel_dso)
Ingo Molnar83a09442009-08-15 12:26:57 +0200532 filename = vmlinux_name;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200533
Ingo Molnar0b73da32009-06-06 15:48:52 +0200534 start = sym->obj_start;
535 if (!start)
536 start = sym->start;
Mike Galbraith42976482009-07-02 08:09:46 +0200537 if (full_paths)
538 d_filename = filename;
539 else
540 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200541
542 end = start + sym->end - sym->start + 1;
543 len = sym->end - sym->start;
544
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200545 if (print_line) {
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200546 get_source_line(sym, start, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200547 print_summary(filename);
548 }
549
550 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200551 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200552 printf("------------------------------------------------\n");
553
554 if (verbose >= 2)
555 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200556
Mike Galbraith42976482009-07-02 08:09:46 +0200557 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
558 (u64)start, (u64)end, filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200559
560 if (verbose >= 3)
561 printf("doing: %s\n", command);
562
563 file = popen(command, "r");
564 if (!file)
565 return;
566
567 while (!feof(file)) {
568 if (parse_line(file, sym, start, len) < 0)
569 break;
570 }
571
572 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200573 if (print_line)
574 free_source_line(sym, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200575}
576
577static void find_annotations(void)
578{
579 struct rb_node *nd;
580 struct dso *dso;
581 int count = 0;
582
583 list_for_each_entry(dso, &dsos, node) {
584
585 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
586 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
587
588 if (sym->hist) {
589 annotate_sym(dso, sym);
590 count++;
591 }
592 }
593 }
594
595 if (!count)
596 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
597}
598
Ingo Molnar8035e422009-06-06 15:19:13 +0200599static int __cmd_annotate(void)
600{
601 int ret, rc = EXIT_FAILURE;
602 unsigned long offset = 0;
603 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200604 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200605 event_t *event;
606 uint32_t size;
607 char *buf;
608
Frederic Weisbecker5b447a62009-08-31 06:45:18 +0200609 register_idle_thread(&threads, &last_match);
Ingo Molnar8035e422009-06-06 15:19:13 +0200610
611 input = open(input_name, O_RDONLY);
612 if (input < 0) {
613 perror("failed to open file");
614 exit(-1);
615 }
616
Ingo Molnar83a09442009-08-15 12:26:57 +0200617 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200618 if (ret < 0) {
619 perror("failed to stat file");
620 exit(-1);
621 }
622
Pierre Habouzit119e7a22009-08-27 09:59:02 +0200623 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
624 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200625 exit(-1);
626 }
627
Ingo Molnar83a09442009-08-15 12:26:57 +0200628 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200629 fprintf(stderr, "zero-sized file, nothing to do!\n");
630 exit(0);
631 }
632
633 if (load_kernel() < 0) {
634 perror("failed to load kernel symbols");
635 return EXIT_FAILURE;
636 }
637
Ingo Molnar8035e422009-06-06 15:19:13 +0200638remap:
639 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
640 MAP_SHARED, input, offset);
641 if (buf == MAP_FAILED) {
642 perror("failed to mmap file");
643 exit(-1);
644 }
645
646more:
647 event = (event_t *)(buf + head);
648
649 size = event->header.size;
650 if (!size)
651 size = 8;
652
653 if (head + event->header.size >= page_size * mmap_window) {
654 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +0200655 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200656
Ingo Molnar83a09442009-08-15 12:26:57 +0200657 munmap_ret = munmap(buf, page_size * mmap_window);
658 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200659
660 offset += shift;
661 head -= shift;
662 goto remap;
663 }
664
665 size = event->header.size;
666
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200667 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200668 (void *)(offset + head),
669 (void *)(long)event->header.size,
670 event->header.type);
671
672 if (!size || process_event(event, offset, head) < 0) {
673
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200674 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200675 (void *)(offset + head),
676 (void *)(long)(event->header.size),
677 event->header.type);
678
679 total_unknown++;
680
681 /*
682 * assume we lost track of the stream, check alignment, and
683 * increment a single u64 in the hope to catch on again 'soon'.
684 */
685
686 if (unlikely(head & 7))
687 head &= ~7ULL;
688
689 size = 8;
690 }
691
692 head += size;
693
Ingo Molnar83a09442009-08-15 12:26:57 +0200694 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +0200695 goto more;
696
697 rc = EXIT_SUCCESS;
698 close(input);
699
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200700 dump_printf(" IP events: %10ld\n", total);
701 dump_printf(" mmap events: %10ld\n", total_mmap);
702 dump_printf(" comm events: %10ld\n", total_comm);
703 dump_printf(" fork events: %10ld\n", total_fork);
704 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +0200705
706 if (dump_trace)
707 return 0;
708
709 if (verbose >= 3)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200710 threads__fprintf(stdout, &threads);
Ingo Molnar8035e422009-06-06 15:19:13 +0200711
712 if (verbose >= 2)
713 dsos__fprintf(stdout);
714
715 collapse__resort();
John Kacur3d1d07e2009-09-28 15:32:55 +0200716 output__resort(total);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200717
718 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +0200719
720 return rc;
721}
722
723static const char * const annotate_usage[] = {
724 "perf annotate [<options>] <command>",
725 NULL
726};
727
728static const struct option options[] = {
729 OPT_STRING('i', "input", &input_name, "file",
730 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +0200731 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200732 "symbol to annotate"),
Peter Zijlstrafa6963b2009-08-19 11:18:26 +0200733 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200734 OPT_BOOLEAN('v', "verbose", &verbose,
735 "be more verbose (show symbol address, etc)"),
736 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
737 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +0200738 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +0200739 OPT_BOOLEAN('m', "modules", &modules,
740 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200741 OPT_BOOLEAN('l', "print-line", &print_line,
742 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +0200743 OPT_BOOLEAN('P', "full-paths", &full_paths,
744 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200745 OPT_END()
746};
747
748static void setup_sorting(void)
749{
750 char *tmp, *tok, *str = strdup(sort_order);
751
752 for (tok = strtok_r(str, ", ", &tmp);
753 tok; tok = strtok_r(NULL, ", ", &tmp)) {
754 if (sort_dimension__add(tok) < 0) {
755 error("Unknown --sort key: `%s'", tok);
756 usage_with_options(annotate_usage, options);
757 }
758 }
759
760 free(str);
761}
762
Ingo Molnarf37a2912009-07-01 12:37:06 +0200763int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +0200764{
765 symbol__init();
766
767 page_size = getpagesize();
768
769 argc = parse_options(argc, argv, options, annotate_usage, 0);
770
771 setup_sorting();
772
Ingo Molnar0b73da32009-06-06 15:48:52 +0200773 if (argc) {
774 /*
775 * Special case: if there's an argument left then assume tha
776 * it's a symbol filter:
777 */
778 if (argc > 1)
779 usage_with_options(annotate_usage, options);
780
781 sym_hist_filter = argv[0];
782 }
783
784 if (!sym_hist_filter)
Ingo Molnar8035e422009-06-06 15:19:13 +0200785 usage_with_options(annotate_usage, options);
786
787 setup_pager();
788
John Kacurdd68ada2009-09-24 18:02:49 +0200789 if (field_sep && *field_sep == '.') {
790 fputs("'.' is the only non valid --field-separator argument\n",
791 stderr);
792 exit(129);
793 }
794
Ingo Molnar8035e422009-06-06 15:19:13 +0200795 return __cmd_annotate();
796}