]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-annotate.c
perf symbols: Improve debugging information about symtab origins
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-annotate.c
CommitLineData
8035e428
IM
1/*
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"
5da50258 13#include <linux/list.h>
8035e428 14#include "util/cache.h"
43cbcd8a 15#include <linux/rbtree.h>
8035e428
IM
16#include "util/symbol.h"
17#include "util/string.h"
18
19#include "perf.h"
8f28827a 20#include "util/debug.h"
8035e428 21
62daacb5 22#include "util/event.h"
8035e428
IM
23#include "util/parse-options.h"
24#include "util/parse-events.h"
6baa0a5a 25#include "util/thread.h"
dd68ada2 26#include "util/sort.h"
3d1d07ec 27#include "util/hist.h"
94c744b6 28#include "util/session.h"
8035e428 29
8035e428 30static char const *input_name = "perf.data";
8035e428 31
fa6963b2 32static int force;
8035e428 33
42976487
MG
34static int full_paths;
35
301406b9
FW
36static int print_line;
37
e4204992
ACM
38struct sym_hist {
39 u64 sum;
40 u64 ip[0];
41};
42
301406b9 43struct sym_ext {
971738f3 44 struct rb_node node;
301406b9
FW
45 double percent;
46 char *path;
47};
48
e4204992
ACM
49struct sym_priv {
50 struct sym_hist *hist;
51 struct sym_ext *ext;
52};
53
54static const char *sym_hist_filter;
55
00a192b3 56static int symbol_filter(struct map *map __used, struct symbol *sym)
e4204992 57{
8f0b0373
ACM
58 if (sym_hist_filter == NULL ||
59 strcmp(sym->name, sym_hist_filter) == 0) {
00a192b3 60 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
61 const int size = (sizeof(*priv->hist) +
62 (sym->end - sym->start) * sizeof(u64));
63
64 priv->hist = malloc(size);
65 if (priv->hist)
66 memset(priv->hist, 0, size);
67 return 0;
68 }
69 /*
70 * FIXME: We should really filter it out, as we don't want to go thru symbols
71 * we're not interested, and if a DSO ends up with no symbols, delete it too,
72 * but right now the kernel loading routines in symbol.c bail out if no symbols
73 * are found, fix it later.
74 */
75 return 0;
76}
8035e428 77
0b73da3f
IM
78/*
79 * collect histogram counts
80 */
9cffa8d5 81static void hist_hit(struct hist_entry *he, u64 ip)
8035e428 82{
0b73da3f
IM
83 unsigned int sym_size, offset;
84 struct symbol *sym = he->sym;
e4204992
ACM
85 struct sym_priv *priv;
86 struct sym_hist *h;
8035e428 87
0b73da3f 88 he->count++;
8035e428 89
e4204992
ACM
90 if (!sym || !he->map)
91 return;
92
00a192b3 93 priv = symbol__priv(sym);
e4204992 94 if (!priv->hist)
0b73da3f 95 return;
8035e428 96
0b73da3f
IM
97 sym_size = sym->end - sym->start;
98 offset = ip - sym->start;
8035e428 99
29a9f66d 100 pr_debug3("%s: ip=%#Lx\n", __func__, he->map->unmap_ip(he->map, ip));
ed52ce2e 101
0b73da3f
IM
102 if (offset >= sym_size)
103 return;
8035e428 104
e4204992
ACM
105 h = priv->hist;
106 h->sum++;
107 h->ip[offset]++;
8035e428 108
29a9f66d
ACM
109 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->sym->start,
110 he->sym->name, ip, ip - he->sym->start, h->ip[offset]);
8035e428
IM
111}
112
4e4f06e4
ACM
113static int perf_session__add_hist_entry(struct perf_session *self,
114 struct addr_location *al, u64 count)
8035e428 115{
9735abf1 116 bool hit;
4e4f06e4
ACM
117 struct hist_entry *he = __perf_session__add_hist_entry(self, al, NULL,
118 count, &hit);
9735abf1 119 if (he == NULL)
8035e428 120 return -ENOMEM;
1ed091c4 121 hist_hit(he, al->addr);
8035e428
IM
122 return 0;
123}
124
b3165f41 125static int process_sample_event(event_t *event, struct perf_session *session)
8035e428 126{
1ed091c4 127 struct addr_location al;
6baa0a5a 128
0d755034
ACM
129 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
130 event->ip.pid, event->ip.ip);
8035e428 131
b3165f41 132 if (event__preprocess_sample(event, session, &al, symbol_filter) < 0) {
29a9f66d
ACM
133 pr_warning("problem processing %d event, skipping it.\n",
134 event->header.type);
8035e428
IM
135 return -1;
136 }
137
c410a338 138 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
29a9f66d
ACM
139 pr_warning("problem incrementing symbol count, "
140 "skipping event\n");
ec218fc4 141 return -1;
8035e428 142 }
8035e428
IM
143
144 return 0;
145}
146
ed52ce2e 147static int parse_line(FILE *file, struct hist_entry *he, u64 len)
0b73da3f 148{
ed52ce2e 149 struct symbol *sym = he->sym;
0b73da3f 150 char *line = NULL, *tmp, *tmp2;
301406b9
FW
151 static const char *prev_line;
152 static const char *prev_color;
0b73da3f
IM
153 unsigned int offset;
154 size_t line_len;
ed52ce2e 155 u64 start;
f37a291c 156 s64 line_ip;
0b73da3f
IM
157 int ret;
158 char *c;
159
160 if (getline(&line, &line_len, file) < 0)
161 return -1;
162 if (!line)
163 return -1;
164
165 c = strchr(line, '\n');
166 if (c)
167 *c = 0;
168
169 line_ip = -1;
170 offset = 0;
171 ret = -2;
172
173 /*
174 * Strip leading spaces:
175 */
176 tmp = line;
177 while (*tmp) {
178 if (*tmp != ' ')
179 break;
180 tmp++;
181 }
182
183 if (*tmp) {
184 /*
185 * Parse hexa addresses followed by ':'
186 */
187 line_ip = strtoull(tmp, &tmp2, 16);
188 if (*tmp2 != ':')
189 line_ip = -1;
190 }
191
7a2b6209 192 start = map__rip_2objdump(he->map, sym->start);
ed52ce2e 193
0b73da3f 194 if (line_ip != -1) {
301406b9 195 const char *path = NULL;
0b73da3f
IM
196 unsigned int hits = 0;
197 double percent = 0.0;
83a0944f 198 const char *color;
00a192b3 199 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
200 struct sym_ext *sym_ext = priv->ext;
201 struct sym_hist *h = priv->hist;
0b73da3f 202
ed52ce2e 203 offset = line_ip - start;
0b73da3f 204 if (offset < len)
e4204992 205 hits = h->ip[offset];
0b73da3f 206
c17c2db1 207 if (offset < len && sym_ext) {
301406b9
FW
208 path = sym_ext[offset].path;
209 percent = sym_ext[offset].percent;
e4204992
ACM
210 } else if (h->sum)
211 percent = 100.0 * hits / h->sum;
0b73da3f 212
1e11fd82 213 color = get_percent_color(percent);
0b73da3f 214
301406b9
FW
215 /*
216 * Also color the filename and line if needed, with
217 * the same color than the percentage. Don't print it
218 * twice for close colored ip with the same filename:line
219 */
220 if (path) {
221 if (!prev_line || strcmp(prev_line, path)
222 || color != prev_color) {
223 color_fprintf(stdout, color, " %s", path);
224 prev_line = path;
225 prev_color = color;
226 }
227 }
228
0b73da3f
IM
229 color_fprintf(stdout, color, " %7.2f", percent);
230 printf(" : ");
231 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
232 } else {
233 if (!*line)
234 printf(" :\n");
235 else
236 printf(" : %s\n", line);
237 }
238
239 return 0;
240}
241
971738f3
FW
242static struct rb_root root_sym_ext;
243
244static void insert_source_line(struct sym_ext *sym_ext)
245{
246 struct sym_ext *iter;
247 struct rb_node **p = &root_sym_ext.rb_node;
248 struct rb_node *parent = NULL;
249
250 while (*p != NULL) {
251 parent = *p;
252 iter = rb_entry(parent, struct sym_ext, node);
253
254 if (sym_ext->percent > iter->percent)
255 p = &(*p)->rb_left;
256 else
257 p = &(*p)->rb_right;
258 }
259
260 rb_link_node(&sym_ext->node, parent, p);
261 rb_insert_color(&sym_ext->node, &root_sym_ext);
262}
263
e4204992 264static void free_source_line(struct hist_entry *he, int len)
301406b9 265{
00a192b3 266 struct sym_priv *priv = symbol__priv(he->sym);
e4204992 267 struct sym_ext *sym_ext = priv->ext;
301406b9
FW
268 int i;
269
270 if (!sym_ext)
271 return;
272
273 for (i = 0; i < len; i++)
274 free(sym_ext[i].path);
275 free(sym_ext);
276
e4204992 277 priv->ext = NULL;
971738f3 278 root_sym_ext = RB_ROOT;
301406b9
FW
279}
280
281/* Get the filename:line for the colored entries */
c17c2db1 282static void
ed52ce2e 283get_source_line(struct hist_entry *he, int len, const char *filename)
301406b9 284{
ed52ce2e
ACM
285 struct symbol *sym = he->sym;
286 u64 start;
301406b9
FW
287 int i;
288 char cmd[PATH_MAX * 2];
289 struct sym_ext *sym_ext;
00a192b3 290 struct sym_priv *priv = symbol__priv(sym);
e4204992 291 struct sym_hist *h = priv->hist;
301406b9 292
e4204992 293 if (!h->sum)
301406b9
FW
294 return;
295
e4204992
ACM
296 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
297 if (!priv->ext)
301406b9
FW
298 return;
299
ed52ce2e 300 start = he->map->unmap_ip(he->map, sym->start);
301406b9
FW
301
302 for (i = 0; i < len; i++) {
303 char *path = NULL;
304 size_t line_len;
9cffa8d5 305 u64 offset;
301406b9
FW
306 FILE *fp;
307
e4204992 308 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
301406b9
FW
309 if (sym_ext[i].percent <= 0.5)
310 continue;
311
ed52ce2e 312 offset = start + i;
c17c2db1 313 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
301406b9
FW
314 fp = popen(cmd, "r");
315 if (!fp)
316 continue;
317
318 if (getline(&path, &line_len, fp) < 0 || !line_len)
319 goto next;
320
c17c2db1 321 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
301406b9
FW
322 if (!sym_ext[i].path)
323 goto next;
324
325 strcpy(sym_ext[i].path, path);
971738f3 326 insert_source_line(&sym_ext[i]);
301406b9
FW
327
328 next:
329 pclose(fp);
330 }
331}
332
83a0944f 333static void print_summary(const char *filename)
971738f3
FW
334{
335 struct sym_ext *sym_ext;
336 struct rb_node *node;
337
338 printf("\nSorted summary for file %s\n", filename);
339 printf("----------------------------------------------\n\n");
340
341 if (RB_EMPTY_ROOT(&root_sym_ext)) {
342 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
343 return;
344 }
345
346 node = rb_first(&root_sym_ext);
347 while (node) {
348 double percent;
83a0944f 349 const char *color;
971738f3
FW
350 char *path;
351
352 sym_ext = rb_entry(node, struct sym_ext, node);
353 percent = sym_ext->percent;
1e11fd82 354 color = get_percent_color(percent);
971738f3
FW
355 path = sym_ext->path;
356
357 color_fprintf(stdout, color, " %7.2f %s", percent, path);
358 node = rb_next(node);
359 }
360}
361
ed52ce2e 362static void annotate_sym(struct hist_entry *he)
0b73da3f 363{
ed52ce2e
ACM
364 struct map *map = he->map;
365 struct dso *dso = map->dso;
366 struct symbol *sym = he->sym;
439d473b
ACM
367 const char *filename = dso->long_name, *d_filename;
368 u64 len;
0b73da3f
IM
369 char command[PATH_MAX*2];
370 FILE *file;
371
372 if (!filename)
373 return;
439d473b 374
29a9f66d
ACM
375 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
376 filename, sym->name, map->unmap_ip(map, sym->start),
377 map->unmap_ip(map, sym->end));
ed52ce2e 378
42976487
MG
379 if (full_paths)
380 d_filename = filename;
381 else
382 d_filename = basename(filename);
0b73da3f 383
0b73da3f
IM
384 len = sym->end - sym->start;
385
971738f3 386 if (print_line) {
ed52ce2e 387 get_source_line(he, len, filename);
971738f3
FW
388 print_summary(filename);
389 }
390
391 printf("\n\n------------------------------------------------\n");
42976487 392 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
971738f3
FW
393 printf("------------------------------------------------\n");
394
395 if (verbose >= 2)
439d473b
ACM
396 printf("annotating [%p] %30s : [%p] %30s\n",
397 dso, dso->long_name, sym, sym->name);
301406b9 398
42976487 399 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
7a2b6209
KS
400 map__rip_2objdump(map, sym->start),
401 map__rip_2objdump(map, sym->end),
ed52ce2e 402 filename, filename);
0b73da3f
IM
403
404 if (verbose >= 3)
405 printf("doing: %s\n", command);
406
407 file = popen(command, "r");
408 if (!file)
409 return;
410
411 while (!feof(file)) {
ed52ce2e 412 if (parse_line(file, he, len) < 0)
0b73da3f
IM
413 break;
414 }
415
416 pclose(file);
971738f3 417 if (print_line)
e4204992 418 free_source_line(he, len);
0b73da3f
IM
419}
420
4e4f06e4 421static void perf_session__find_annotations(struct perf_session *self)
0b73da3f
IM
422{
423 struct rb_node *nd;
0b73da3f 424
4e4f06e4 425 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
ed52ce2e 426 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
e4204992 427 struct sym_priv *priv;
0b73da3f 428
e4204992
ACM
429 if (he->sym == NULL)
430 continue;
0b73da3f 431
00a192b3 432 priv = symbol__priv(he->sym);
e4204992
ACM
433 if (priv->hist == NULL)
434 continue;
435
436 annotate_sym(he);
e4204992
ACM
437 /*
438 * Since we have a hist_entry per IP for the same symbol, free
439 * he->sym->hist to signal we already processed this symbol.
440 */
441 free(priv->hist);
442 priv->hist = NULL;
0b73da3f 443 }
0b73da3f
IM
444}
445
301a0b02 446static struct perf_event_ops event_ops = {
55aa640f
ACM
447 .sample = process_sample_event,
448 .mmap = event__process_mmap,
449 .comm = event__process_comm,
450 .fork = event__process_task,
bab81b62
LZ
451};
452
8035e428
IM
453static int __cmd_annotate(void)
454{
bab81b62 455 int ret;
75be6cf4 456 struct perf_session *session;
8035e428 457
75be6cf4 458 session = perf_session__new(input_name, O_RDONLY, force);
94c744b6
ACM
459 if (session == NULL)
460 return -ENOMEM;
461
ec913369 462 ret = perf_session__process_events(session, &event_ops);
bab81b62 463 if (ret)
94c744b6 464 goto out_delete;
8035e428 465
62daacb5
ACM
466 if (dump_trace) {
467 event__print_totals();
94c744b6 468 goto out_delete;
62daacb5 469 }
8035e428 470
da21d1b5 471 if (verbose > 3)
b3165f41 472 perf_session__fprintf(session, stdout);
8035e428 473
da21d1b5 474 if (verbose > 2)
8035e428
IM
475 dsos__fprintf(stdout);
476
4e4f06e4 477 perf_session__collapse_resort(session);
f823e441 478 perf_session__output_resort(session, session->event_total[0]);
4e4f06e4 479 perf_session__find_annotations(session);
94c744b6
ACM
480out_delete:
481 perf_session__delete(session);
8035e428 482
bab81b62 483 return ret;
8035e428
IM
484}
485
486static const char * const annotate_usage[] = {
487 "perf annotate [<options>] <command>",
488 NULL
489};
490
491static const struct option options[] = {
492 OPT_STRING('i', "input", &input_name, "file",
493 "input file name"),
23b87116 494 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
0b73da3f 495 "symbol to annotate"),
fa6963b2 496 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
8035e428
IM
497 OPT_BOOLEAN('v', "verbose", &verbose,
498 "be more verbose (show symbol address, etc)"),
499 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
500 "dump raw trace in ASCII"),
b32d133a
ACM
501 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
502 "file", "vmlinux pathname"),
503 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 504 "load module symbols - WARNING: use only with -k and LIVE kernel"),
301406b9
FW
505 OPT_BOOLEAN('l', "print-line", &print_line,
506 "print matching source lines (may be slow)"),
42976487
MG
507 OPT_BOOLEAN('P', "full-paths", &full_paths,
508 "Don't shorten the displayed pathnames"),
8035e428
IM
509 OPT_END()
510};
511
f37a291c 512int cmd_annotate(int argc, const char **argv, const char *prefix __used)
8035e428 513{
655000e7
ACM
514 argc = parse_options(argc, argv, options, annotate_usage, 0);
515
75be6cf4
ACM
516 symbol_conf.priv_size = sizeof(struct sym_priv);
517 symbol_conf.try_vmlinux_path = true;
518
519 if (symbol__init() < 0)
b32d133a 520 return -1;
8035e428 521
c8829c7a 522 setup_sorting(annotate_usage, options);
8035e428 523
0b73da3f
IM
524 if (argc) {
525 /*
526 * Special case: if there's an argument left then assume tha
527 * it's a symbol filter:
528 */
529 if (argc > 1)
530 usage_with_options(annotate_usage, options);
531
532 sym_hist_filter = argv[0];
533 }
534
8035e428
IM
535 setup_pager();
536
dd68ada2 537 if (field_sep && *field_sep == '.') {
29a9f66d
ACM
538 pr_err("'.' is the only non valid --field-separator argument\n");
539 return -1;
dd68ada2
JK
540 }
541
8035e428
IM
542 return __cmd_annotate();
543}