blob: ed1f89d7044e20c6bd042b897396a26f8333dd65 [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
10#include "util.h"
11#include "build-id.h"
12#include "color.h"
13#include "cache.h"
14#include "symbol.h"
15#include "debug.h"
16#include "annotate.h"
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020017#include <pthread.h>
18
Andi Kleenf69b64f2011-09-15 14:31:41 -070019const char *disassembler_style;
20
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030021static int call_ops__parse_target(const char *operands, u64 *target)
22{
23 *target = strtoull(operands, NULL, 16);
24 return 0;
25}
26
27static struct ins_ops call_ops = {
28 .parse_target = call_ops__parse_target,
29};
30
31bool ins__is_call(const struct ins *ins)
32{
33 return ins->ops == &call_ops;
34}
35
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030036static int jump_ops__parse_target(const char *operands, u64 *target)
37{
38 const char *s = strchr(operands, '+');
39
40 if (s++ == NULL)
41 return -1;
42
43 *target = strtoll(s, NULL, 16);
44 return 0;
45}
46
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030047static int jump_ops__scnprintf(struct ins *ins, char *bf, size_t size,
48 const char *operands, u64 target)
49{
50 if (operands)
51 return scnprintf(bf, size, "%-6.6s %s", ins->name, operands);
52
53 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, target);
54}
55
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030056static struct ins_ops jump_ops = {
57 .parse_target = jump_ops__parse_target,
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030058 .scnprintf = jump_ops__scnprintf,
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030059};
60
61bool ins__is_jump(const struct ins *ins)
62{
63 return ins->ops == &jump_ops;
64}
65
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030066/*
67 * Must be sorted by name!
68 */
69static struct ins instructions[] = {
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030070 { .name = "call", .ops = &call_ops, },
71 { .name = "callq", .ops = &call_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030072 { .name = "ja", .ops = &jump_ops, },
73 { .name = "je", .ops = &jump_ops, },
74 { .name = "jmp", .ops = &jump_ops, },
75 { .name = "jmpq", .ops = &jump_ops, },
76 { .name = "jne", .ops = &jump_ops, },
77 { .name = "js", .ops = &jump_ops, },
78};
79
80static int ins__cmp(const void *name, const void *insp)
81{
82 const struct ins *ins = insp;
83
84 return strcmp(name, ins->name);
85}
86
87static struct ins *ins__find(const char *name)
88{
89 const int nmemb = ARRAY_SIZE(instructions);
90
91 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
92}
93
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020094int symbol__annotate_init(struct map *map __used, struct symbol *sym)
95{
96 struct annotation *notes = symbol__annotation(sym);
97 pthread_mutex_init(&notes->lock, NULL);
98 return 0;
99}
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200100
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200101int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200102{
103 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo64c17be2012-03-28 12:49:35 -0300104 const size_t size = sym->end - sym->start + 1;
105 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200106
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200107 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200108 if (notes->src == NULL)
109 return -1;
110 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200111 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200112 INIT_LIST_HEAD(&notes->src->source);
113 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200114}
115
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200116void symbol__annotate_zero_histograms(struct symbol *sym)
117{
118 struct annotation *notes = symbol__annotation(sym);
119
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200120 pthread_mutex_lock(&notes->lock);
121 if (notes->src != NULL)
122 memset(notes->src->histograms, 0,
123 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
124 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200125}
126
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200127int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
128 int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200129{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200130 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200131 struct annotation *notes;
132 struct sym_hist *h;
133
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200134 notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200135 if (notes->src == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200136 return -ENOMEM;
137
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200138 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
139
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300140 if (addr < sym->start || addr > sym->end)
141 return -ERANGE;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200142
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200143 offset = addr - sym->start;
144 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200145 h->sum++;
146 h->addr[offset]++;
147
148 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200149 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
150 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200151 return 0;
152}
153
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300154static void disasm_line__init_ins(struct disasm_line *dl)
155{
156 dl->ins = ins__find(dl->name);
157
158 if (dl->ins == NULL)
159 return;
160
161 if (!dl->ins->ops)
162 return;
163
164 if (dl->ins->ops->parse_target)
165 dl->ins->ops->parse_target(dl->operands, &dl->target);
166}
167
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300168static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200169{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300170 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200171
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300172 if (dl != NULL) {
173 dl->offset = offset;
174 dl->line = strdup(line);
175 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300176 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300177
178 if (offset != -1) {
179 char *name = dl->line, tmp;
180
181 while (isspace(name[0]))
182 ++name;
183
184 if (name[0] == '\0')
185 goto out_delete;
186
187 dl->operands = name + 1;
188
189 while (dl->operands[0] != '\0' &&
190 !isspace(dl->operands[0]))
191 ++dl->operands;
192
193 tmp = dl->operands[0];
194 dl->operands[0] = '\0';
195 dl->name = strdup(name);
196
197 if (dl->name == NULL)
198 goto out_free_line;
199
200 dl->operands[0] = tmp;
201
202 if (dl->operands[0] != '\0') {
203 dl->operands++;
204 while (isspace(dl->operands[0]))
205 ++dl->operands;
206 }
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300207
208 disasm_line__init_ins(dl);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300209 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200210 }
211
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300212 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300213
214out_free_line:
215 free(dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300216out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300217 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300218 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200219}
220
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300221void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200222{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300223 free(dl->line);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300224 free(dl->name);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300225 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200226}
227
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300228static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200229{
230 list_add_tail(&line->node, head);
231}
232
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300233struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200234{
235 list_for_each_entry_continue(pos, head, node)
236 if (pos->offset >= 0)
237 return pos;
238
239 return NULL;
240}
241
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300242static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
243 int evidx, u64 len, int min_pcnt, int printed,
244 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200245{
246 static const char *prev_line;
247 static const char *prev_color;
248
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300249 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200250 const char *path = NULL;
251 unsigned int hits = 0;
252 double percent = 0.0;
253 const char *color;
254 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200255 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200256 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300257 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300258 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300259 struct disasm_line *next;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200260
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300261 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200262
263 while (offset < (s64)len &&
264 (next == NULL || offset < next->offset)) {
265 if (src_line) {
266 if (path == NULL)
267 path = src_line[offset].path;
268 percent += src_line[offset].percent;
269 } else
270 hits += h->addr[offset];
271
272 ++offset;
273 }
274
275 if (src_line == NULL && h->sum)
276 percent = 100.0 * hits / h->sum;
277
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200278 if (percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200279 return -1;
280
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200281 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200282 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200283
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200284 if (queue != NULL) {
285 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300286 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200287 break;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300288 disasm_line__print(queue, sym, start, evidx, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200289 0, 0, 1, NULL);
290 }
291 }
292
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200293 color = get_percent_color(percent);
294
295 /*
296 * Also color the filename and line if needed, with
297 * the same color than the percentage. Don't print it
298 * twice for close colored addr with the same filename:line
299 */
300 if (path) {
301 if (!prev_line || strcmp(prev_line, path)
302 || color != prev_color) {
303 color_fprintf(stdout, color, " %s", path);
304 prev_line = path;
305 prev_color = color;
306 }
307 }
308
309 color_fprintf(stdout, color, " %7.2f", percent);
310 printf(" : ");
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300311 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300312 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200313 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200314 return 1;
315 else {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200316 if (queue)
317 return -1;
318
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300319 if (!*dl->line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200320 printf(" :\n");
321 else
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300322 printf(" : %s\n", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200323 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200324
325 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200326}
327
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200328static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
329 FILE *file, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200330{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200331 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300332 struct disasm_line *dl;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300333 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200334 size_t line_len;
335 s64 line_ip, offset = -1;
336
337 if (getline(&line, &line_len, file) < 0)
338 return -1;
339
340 if (!line)
341 return -1;
342
343 while (line_len != 0 && isspace(line[line_len - 1]))
344 line[--line_len] = '\0';
345
346 c = strchr(line, '\n');
347 if (c)
348 *c = 0;
349
350 line_ip = -1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300351 parsed_line = line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200352
353 /*
354 * Strip leading spaces:
355 */
356 tmp = line;
357 while (*tmp) {
358 if (*tmp != ' ')
359 break;
360 tmp++;
361 }
362
363 if (*tmp) {
364 /*
365 * Parse hexa addresses followed by ':'
366 */
367 line_ip = strtoull(tmp, &tmp2, 16);
368 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
369 line_ip = -1;
370 }
371
372 if (line_ip != -1) {
373 u64 start = map__rip_2objdump(map, sym->start),
374 end = map__rip_2objdump(map, sym->end);
375
376 offset = line_ip - start;
377 if (offset < 0 || (u64)line_ip > end)
378 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300379 else
380 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300381 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200382
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300383 dl = disasm_line__new(offset, parsed_line, privsize);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300384 free(line);
385
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300386 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200387 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300388
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300389 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200390
391 return 0;
392}
393
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200394int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200395{
396 struct dso *dso = map->dso;
397 char *filename = dso__build_id_filename(dso, NULL, 0);
398 bool free_filename = true;
399 char command[PATH_MAX * 2];
400 FILE *file;
401 int err = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200402 char symfs_filename[PATH_MAX];
403
404 if (filename) {
405 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
406 symbol_conf.symfs, filename);
407 }
408
409 if (filename == NULL) {
410 if (dso->has_build_id) {
411 pr_err("Can't annotate %s: not enough memory\n",
412 sym->name);
413 return -ENOMEM;
414 }
415 goto fallback;
416 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
417 strstr(command, "[kernel.kallsyms]") ||
418 access(symfs_filename, R_OK)) {
419 free(filename);
420fallback:
421 /*
422 * If we don't have build-ids or the build-id file isn't in the
423 * cache, or is just a kallsyms file, well, lets hope that this
424 * DSO is the same as when 'perf record' ran.
425 */
426 filename = dso->long_name;
427 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
428 symbol_conf.symfs, filename);
429 free_filename = false;
430 }
431
Arnaldo Carvalho de Melo878b4392011-03-11 13:13:36 -0300432 if (dso->symtab_type == SYMTAB__KALLSYMS) {
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300433 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
434 char *build_id_msg = NULL;
435
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200436 if (dso->annotate_warned)
437 goto out_free_filename;
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300438
439 if (dso->has_build_id) {
440 build_id__sprintf(dso->build_id,
441 sizeof(dso->build_id), bf + 15);
442 build_id_msg = bf;
443 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200444 err = -ENOENT;
445 dso->annotate_warned = 1;
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200446 pr_err("Can't annotate %s:\n\n"
447 "No vmlinux file%s\nwas found in the path.\n\n"
448 "Please use:\n\n"
449 " perf buildid-cache -av vmlinux\n\n"
450 "or:\n\n"
Namhyung Kimff2a6612012-02-23 17:46:24 +0900451 " --vmlinux vmlinux\n",
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300452 sym->name, build_id_msg ?: "");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200453 goto out_free_filename;
454 }
455
456 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
457 filename, sym->name, map->unmap_ip(map, sym->start),
458 map->unmap_ip(map, sym->end));
459
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200460 pr_debug("annotating [%p] %30s : [%p] %30s\n",
461 dso, dso->long_name, sym, sym->name);
462
463 snprintf(command, sizeof(command),
Andi Kleenf69b64f2011-09-15 14:31:41 -0700464 "objdump %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200465 " --stop-address=0x%016" PRIx64
466 " -d %s %s -C %s|grep -v %s|expand",
Andi Kleenf69b64f2011-09-15 14:31:41 -0700467 disassembler_style ? "-M " : "",
468 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200469 map__rip_2objdump(map, sym->start),
Ingo Molnarf41612f2011-12-23 14:08:04 +0100470 map__rip_2objdump(map, sym->end+1),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200471 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
472 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200473 symfs_filename, filename);
474
475 pr_debug("Executing: %s\n", command);
476
477 file = popen(command, "r");
478 if (!file)
479 goto out_free_filename;
480
481 while (!feof(file))
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200482 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200483 break;
484
485 pclose(file);
486out_free_filename:
487 if (free_filename)
488 free(filename);
489 return err;
490}
491
492static void insert_source_line(struct rb_root *root, struct source_line *src_line)
493{
494 struct source_line *iter;
495 struct rb_node **p = &root->rb_node;
496 struct rb_node *parent = NULL;
497
498 while (*p != NULL) {
499 parent = *p;
500 iter = rb_entry(parent, struct source_line, node);
501
502 if (src_line->percent > iter->percent)
503 p = &(*p)->rb_left;
504 else
505 p = &(*p)->rb_right;
506 }
507
508 rb_link_node(&src_line->node, parent, p);
509 rb_insert_color(&src_line->node, root);
510}
511
512static void symbol__free_source_line(struct symbol *sym, int len)
513{
514 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200515 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200516 int i;
517
518 for (i = 0; i < len; i++)
519 free(src_line[i].path);
520
521 free(src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200522 notes->src->lines = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200523}
524
525/* Get the filename:line for the colored entries */
526static int symbol__get_source_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200527 int evidx, struct rb_root *root, int len,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200528 const char *filename)
529{
530 u64 start;
531 int i;
532 char cmd[PATH_MAX * 2];
533 struct source_line *src_line;
534 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200535 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200536
537 if (!h->sum)
538 return 0;
539
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200540 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
541 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200542 return -1;
543
David Millerf40a0632012-03-25 16:28:12 -0400544 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200545
546 for (i = 0; i < len; i++) {
547 char *path = NULL;
548 size_t line_len;
549 u64 offset;
550 FILE *fp;
551
552 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
553 if (src_line[i].percent <= 0.5)
554 continue;
555
556 offset = start + i;
557 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
558 fp = popen(cmd, "r");
559 if (!fp)
560 continue;
561
562 if (getline(&path, &line_len, fp) < 0 || !line_len)
563 goto next;
564
565 src_line[i].path = malloc(sizeof(char) * line_len + 1);
566 if (!src_line[i].path)
567 goto next;
568
569 strcpy(src_line[i].path, path);
570 insert_source_line(root, &src_line[i]);
571
572 next:
573 pclose(fp);
574 }
575
576 return 0;
577}
578
579static void print_summary(struct rb_root *root, const char *filename)
580{
581 struct source_line *src_line;
582 struct rb_node *node;
583
584 printf("\nSorted summary for file %s\n", filename);
585 printf("----------------------------------------------\n\n");
586
587 if (RB_EMPTY_ROOT(root)) {
588 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
589 return;
590 }
591
592 node = rb_first(root);
593 while (node) {
594 double percent;
595 const char *color;
596 char *path;
597
598 src_line = rb_entry(node, struct source_line, node);
599 percent = src_line->percent;
600 color = get_percent_color(percent);
601 path = src_line->path;
602
603 color_fprintf(stdout, color, " %7.2f %s", percent, path);
604 node = rb_next(node);
605 }
606}
607
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200608static void symbol__annotate_hits(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200609{
610 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200611 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200612 u64 len = sym->end - sym->start, offset;
613
614 for (offset = 0; offset < len; ++offset)
615 if (h->addr[offset] != 0)
616 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
617 sym->start + offset, h->addr[offset]);
618 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
619}
620
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200621int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200622 bool full_paths, int min_pcnt, int max_lines,
623 int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200624{
625 struct dso *dso = map->dso;
626 const char *filename = dso->long_name, *d_filename;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200627 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300628 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300629 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200630 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200631 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200632 u64 len;
633
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200634 if (full_paths)
635 d_filename = filename;
636 else
637 d_filename = basename(filename);
638
639 len = sym->end - sym->start;
640
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200641 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
642 printf("------------------------------------------------\n");
643
644 if (verbose)
645 symbol__annotate_hits(sym, evidx);
646
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200647 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200648 if (context && queue == NULL) {
649 queue = pos;
650 queue_len = 0;
651 }
652
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300653 switch (disasm_line__print(pos, sym, start, evidx, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300654 min_pcnt, printed, max_lines,
655 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200656 case 0:
657 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200658 if (context) {
659 printed += queue_len;
660 queue = NULL;
661 queue_len = 0;
662 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200663 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200664 case 1:
665 /* filtered by max_lines */
666 ++more;
667 break;
668 case -1:
669 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200670 /*
671 * Filtered by min_pcnt or non IP lines when
672 * context != 0
673 */
674 if (!context)
675 break;
676 if (queue_len == context)
677 queue = list_entry(queue->node.next, typeof(*queue), node);
678 else
679 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200680 break;
681 }
682 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200683
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200684 return more;
685}
686
687void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
688{
689 struct annotation *notes = symbol__annotation(sym);
690 struct sym_hist *h = annotation__histogram(notes, evidx);
691
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200692 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200693}
694
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200695void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200696{
697 struct annotation *notes = symbol__annotation(sym);
698 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -0300699 int len = sym->end - sym->start, offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200700
701 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -0300702 for (offset = 0; offset < len; ++offset) {
703 h->addr[offset] = h->addr[offset] * 7 / 8;
704 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200705 }
706}
707
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300708void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200709{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300710 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200711
712 list_for_each_entry_safe(pos, n, head, node) {
713 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300714 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200715 }
716}
717
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300718static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
719{
720 size_t printed;
721
722 if (dl->offset == -1)
723 return fprintf(fp, "%s\n", dl->line);
724
725 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
726
727 if (dl->operands[0] != '\0') {
728 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
729 dl->operands);
730 }
731
732 return printed + fprintf(fp, "\n");
733}
734
735size_t disasm__fprintf(struct list_head *head, FILE *fp)
736{
737 struct disasm_line *pos;
738 size_t printed = 0;
739
740 list_for_each_entry(pos, head, node)
741 printed += disasm_line__fprintf(pos, fp);
742
743 return printed;
744}
745
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200746int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
747 bool print_lines, bool full_paths, int min_pcnt,
748 int max_lines)
749{
750 struct dso *dso = map->dso;
751 const char *filename = dso->long_name;
752 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200753 u64 len;
754
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200755 if (symbol__annotate(sym, map, 0) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200756 return -1;
757
758 len = sym->end - sym->start;
759
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200760 if (print_lines) {
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200761 symbol__get_source_line(sym, map, evidx, &source_line,
762 len, filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200763 print_summary(&source_line, filename);
764 }
765
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200766 symbol__annotate_printf(sym, map, evidx, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200767 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200768 if (print_lines)
769 symbol__free_source_line(sym, len);
770
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300771 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200772
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200773 return 0;
774}