blob: 7f6c14b3fd7f59a8cb6cd13ba3f0f4f3a43ddf6e [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 Meloc7e6ead2012-04-20 14:38:46 -030021static int call__parse(struct ins_operands *ops)
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030022{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030023 ops->target = strtoull(ops->raw, NULL, 16);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030024 return 0;
25}
26
27static struct ins_ops call_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030028 .parse = call__parse,
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030029};
30
31bool ins__is_call(const struct ins *ins)
32{
33 return ins->ops == &call_ops;
34}
35
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030036static int jump__parse(struct ins_operands *ops)
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030037{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030038 const char *s = strchr(ops->raw, '+');
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030039
40 if (s++ == NULL)
41 return -1;
42
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030043 ops->target = strtoll(s, NULL, 16);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030044 return 0;
45}
46
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030047static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
48 struct ins_operands *ops, bool addrs)
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030049{
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030050 if (addrs)
51 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030052
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030053 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030054}
55
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030056static struct ins_ops jump_ops = {
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030057 .parse = jump__parse,
58 .scnprintf = jump__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, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -030073 { .name = "jae", .ops = &jump_ops, },
74 { .name = "jb", .ops = &jump_ops, },
75 { .name = "jbe", .ops = &jump_ops, },
76 { .name = "jc", .ops = &jump_ops, },
77 { .name = "jcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030078 { .name = "je", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -030079 { .name = "jecxz", .ops = &jump_ops, },
80 { .name = "jg", .ops = &jump_ops, },
81 { .name = "jge", .ops = &jump_ops, },
82 { .name = "jl", .ops = &jump_ops, },
83 { .name = "jle", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030084 { .name = "jmp", .ops = &jump_ops, },
85 { .name = "jmpq", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -030086 { .name = "jna", .ops = &jump_ops, },
87 { .name = "jnae", .ops = &jump_ops, },
88 { .name = "jnb", .ops = &jump_ops, },
89 { .name = "jnbe", .ops = &jump_ops, },
90 { .name = "jnc", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030091 { .name = "jne", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -030092 { .name = "jng", .ops = &jump_ops, },
93 { .name = "jnge", .ops = &jump_ops, },
94 { .name = "jnl", .ops = &jump_ops, },
95 { .name = "jnle", .ops = &jump_ops, },
96 { .name = "jno", .ops = &jump_ops, },
97 { .name = "jnp", .ops = &jump_ops, },
98 { .name = "jns", .ops = &jump_ops, },
99 { .name = "jnz", .ops = &jump_ops, },
100 { .name = "jo", .ops = &jump_ops, },
101 { .name = "jp", .ops = &jump_ops, },
102 { .name = "jpe", .ops = &jump_ops, },
103 { .name = "jpo", .ops = &jump_ops, },
104 { .name = "jrcxz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300105 { .name = "js", .ops = &jump_ops, },
Arnaldo Carvalho de Melo3f862fd2012-04-19 17:10:12 -0300106 { .name = "jz", .ops = &jump_ops, },
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300107};
108
109static int ins__cmp(const void *name, const void *insp)
110{
111 const struct ins *ins = insp;
112
113 return strcmp(name, ins->name);
114}
115
116static struct ins *ins__find(const char *name)
117{
118 const int nmemb = ARRAY_SIZE(instructions);
119
120 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
121}
122
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200123int symbol__annotate_init(struct map *map __used, struct symbol *sym)
124{
125 struct annotation *notes = symbol__annotation(sym);
126 pthread_mutex_init(&notes->lock, NULL);
127 return 0;
128}
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200129
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200130int symbol__alloc_hist(struct symbol *sym)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200131{
132 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300133 const size_t size = symbol__size(sym);
Arnaldo Carvalho de Melo64c17be2012-03-28 12:49:35 -0300134 size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200135
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200136 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200137 if (notes->src == NULL)
138 return -1;
139 notes->src->sizeof_sym_hist = sizeof_sym_hist;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200140 notes->src->nr_histograms = symbol_conf.nr_events;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200141 INIT_LIST_HEAD(&notes->src->source);
142 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200143}
144
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200145void symbol__annotate_zero_histograms(struct symbol *sym)
146{
147 struct annotation *notes = symbol__annotation(sym);
148
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200149 pthread_mutex_lock(&notes->lock);
150 if (notes->src != NULL)
151 memset(notes->src->histograms, 0,
152 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
153 pthread_mutex_unlock(&notes->lock);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200154}
155
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200156int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
157 int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200158{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200159 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200160 struct annotation *notes;
161 struct sym_hist *h;
162
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200163 notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200164 if (notes->src == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200165 return -ENOMEM;
166
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200167 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
168
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -0300169 if (addr < sym->start || addr > sym->end)
170 return -ERANGE;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200171
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200172 offset = addr - sym->start;
173 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200174 h->sum++;
175 h->addr[offset]++;
176
177 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200178 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
179 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200180 return 0;
181}
182
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300183static void disasm_line__init_ins(struct disasm_line *dl)
184{
185 dl->ins = ins__find(dl->name);
186
187 if (dl->ins == NULL)
188 return;
189
190 if (!dl->ins->ops)
191 return;
192
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300193 if (dl->ins->ops->parse)
194 dl->ins->ops->parse(&dl->ops);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300195}
196
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300197static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200198{
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300199 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200200
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300201 if (dl != NULL) {
202 dl->offset = offset;
203 dl->line = strdup(line);
204 if (dl->line == NULL)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300205 goto out_delete;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300206
207 if (offset != -1) {
208 char *name = dl->line, tmp;
209
210 while (isspace(name[0]))
211 ++name;
212
213 if (name[0] == '\0')
214 goto out_delete;
215
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300216 dl->ops.raw = name + 1;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300217
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300218 while (dl->ops.raw[0] != '\0' &&
219 !isspace(dl->ops.raw[0]))
220 ++dl->ops.raw;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300221
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300222 tmp = dl->ops.raw[0];
223 dl->ops.raw[0] = '\0';
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300224 dl->name = strdup(name);
225
226 if (dl->name == NULL)
227 goto out_free_line;
228
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300229 dl->ops.raw[0] = tmp;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300230
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300231 if (dl->ops.raw[0] != '\0') {
232 dl->ops.raw++;
233 while (isspace(dl->ops.raw[0]))
234 ++dl->ops.raw;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300235 }
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300236
237 disasm_line__init_ins(dl);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300238 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200239 }
240
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300241 return dl;
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300242
243out_free_line:
244 free(dl->line);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300245out_delete:
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300246 free(dl);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300247 return NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200248}
249
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300250void disasm_line__free(struct disasm_line *dl)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200251{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300252 free(dl->line);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300253 free(dl->name);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300254 free(dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200255}
256
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300257static void disasm__add(struct list_head *head, struct disasm_line *line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200258{
259 list_add_tail(&line->node, head);
260}
261
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300262struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200263{
264 list_for_each_entry_continue(pos, head, node)
265 if (pos->offset >= 0)
266 return pos;
267
268 return NULL;
269}
270
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300271static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
272 int evidx, u64 len, int min_pcnt, int printed,
273 int max_lines, struct disasm_line *queue)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200274{
275 static const char *prev_line;
276 static const char *prev_color;
277
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300278 if (dl->offset != -1) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200279 const char *path = NULL;
280 unsigned int hits = 0;
281 double percent = 0.0;
282 const char *color;
283 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200284 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200285 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300286 s64 offset = dl->offset;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300287 const u64 addr = start + offset;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300288 struct disasm_line *next;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200289
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300290 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200291
292 while (offset < (s64)len &&
293 (next == NULL || offset < next->offset)) {
294 if (src_line) {
295 if (path == NULL)
296 path = src_line[offset].path;
297 percent += src_line[offset].percent;
298 } else
299 hits += h->addr[offset];
300
301 ++offset;
302 }
303
304 if (src_line == NULL && h->sum)
305 percent = 100.0 * hits / h->sum;
306
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200307 if (percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200308 return -1;
309
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200310 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200311 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200312
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200313 if (queue != NULL) {
314 list_for_each_entry_from(queue, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300315 if (queue == dl)
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200316 break;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300317 disasm_line__print(queue, sym, start, evidx, len,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200318 0, 0, 1, NULL);
319 }
320 }
321
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200322 color = get_percent_color(percent);
323
324 /*
325 * Also color the filename and line if needed, with
326 * the same color than the percentage. Don't print it
327 * twice for close colored addr with the same filename:line
328 */
329 if (path) {
330 if (!prev_line || strcmp(prev_line, path)
331 || color != prev_color) {
332 color_fprintf(stdout, color, " %s", path);
333 prev_line = path;
334 prev_color = color;
335 }
336 }
337
338 color_fprintf(stdout, color, " %7.2f", percent);
339 printf(" : ");
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300340 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300341 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200342 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200343 return 1;
344 else {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200345 if (queue)
346 return -1;
347
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300348 if (!*dl->line)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200349 printf(" :\n");
350 else
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300351 printf(" : %s\n", dl->line);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200352 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200353
354 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200355}
356
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200357static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
358 FILE *file, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200359{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200360 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300361 struct disasm_line *dl;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300362 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200363 size_t line_len;
364 s64 line_ip, offset = -1;
365
366 if (getline(&line, &line_len, file) < 0)
367 return -1;
368
369 if (!line)
370 return -1;
371
372 while (line_len != 0 && isspace(line[line_len - 1]))
373 line[--line_len] = '\0';
374
375 c = strchr(line, '\n');
376 if (c)
377 *c = 0;
378
379 line_ip = -1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300380 parsed_line = line;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200381
382 /*
383 * Strip leading spaces:
384 */
385 tmp = line;
386 while (*tmp) {
387 if (*tmp != ' ')
388 break;
389 tmp++;
390 }
391
392 if (*tmp) {
393 /*
394 * Parse hexa addresses followed by ':'
395 */
396 line_ip = strtoull(tmp, &tmp2, 16);
397 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
398 line_ip = -1;
399 }
400
401 if (line_ip != -1) {
402 u64 start = map__rip_2objdump(map, sym->start),
403 end = map__rip_2objdump(map, sym->end);
404
405 offset = line_ip - start;
406 if (offset < 0 || (u64)line_ip > end)
407 offset = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300408 else
409 parsed_line = tmp2 + 1;
Namhyung Kima31b7cc2012-04-11 17:04:59 -0300410 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200411
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300412 dl = disasm_line__new(offset, parsed_line, privsize);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300413 free(line);
414
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300415 if (dl == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200416 return -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300417
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300418 disasm__add(&notes->src->source, dl);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200419
420 return 0;
421}
422
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200423int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200424{
425 struct dso *dso = map->dso;
426 char *filename = dso__build_id_filename(dso, NULL, 0);
427 bool free_filename = true;
428 char command[PATH_MAX * 2];
429 FILE *file;
430 int err = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200431 char symfs_filename[PATH_MAX];
432
433 if (filename) {
434 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
435 symbol_conf.symfs, filename);
436 }
437
438 if (filename == NULL) {
439 if (dso->has_build_id) {
440 pr_err("Can't annotate %s: not enough memory\n",
441 sym->name);
442 return -ENOMEM;
443 }
444 goto fallback;
445 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
446 strstr(command, "[kernel.kallsyms]") ||
447 access(symfs_filename, R_OK)) {
448 free(filename);
449fallback:
450 /*
451 * If we don't have build-ids or the build-id file isn't in the
452 * cache, or is just a kallsyms file, well, lets hope that this
453 * DSO is the same as when 'perf record' ran.
454 */
455 filename = dso->long_name;
456 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
457 symbol_conf.symfs, filename);
458 free_filename = false;
459 }
460
Arnaldo Carvalho de Melo878b4392011-03-11 13:13:36 -0300461 if (dso->symtab_type == SYMTAB__KALLSYMS) {
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300462 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
463 char *build_id_msg = NULL;
464
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200465 if (dso->annotate_warned)
466 goto out_free_filename;
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300467
468 if (dso->has_build_id) {
469 build_id__sprintf(dso->build_id,
470 sizeof(dso->build_id), bf + 15);
471 build_id_msg = bf;
472 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200473 err = -ENOENT;
474 dso->annotate_warned = 1;
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200475 pr_err("Can't annotate %s:\n\n"
476 "No vmlinux file%s\nwas found in the path.\n\n"
477 "Please use:\n\n"
478 " perf buildid-cache -av vmlinux\n\n"
479 "or:\n\n"
Namhyung Kimff2a6612012-02-23 17:46:24 +0900480 " --vmlinux vmlinux\n",
Arnaldo Carvalho de Melo170ae6b2011-02-23 11:08:59 -0300481 sym->name, build_id_msg ?: "");
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200482 goto out_free_filename;
483 }
484
485 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
486 filename, sym->name, map->unmap_ip(map, sym->start),
487 map->unmap_ip(map, sym->end));
488
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200489 pr_debug("annotating [%p] %30s : [%p] %30s\n",
490 dso, dso->long_name, sym, sym->name);
491
492 snprintf(command, sizeof(command),
Andi Kleenf69b64f2011-09-15 14:31:41 -0700493 "objdump %s%s --start-address=0x%016" PRIx64
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200494 " --stop-address=0x%016" PRIx64
495 " -d %s %s -C %s|grep -v %s|expand",
Andi Kleenf69b64f2011-09-15 14:31:41 -0700496 disassembler_style ? "-M " : "",
497 disassembler_style ? disassembler_style : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200498 map__rip_2objdump(map, sym->start),
Ingo Molnarf41612f2011-12-23 14:08:04 +0100499 map__rip_2objdump(map, sym->end+1),
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200500 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
501 symbol_conf.annotate_src ? "-S" : "",
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200502 symfs_filename, filename);
503
504 pr_debug("Executing: %s\n", command);
505
506 file = popen(command, "r");
507 if (!file)
508 goto out_free_filename;
509
510 while (!feof(file))
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200511 if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200512 break;
513
514 pclose(file);
515out_free_filename:
516 if (free_filename)
517 free(filename);
518 return err;
519}
520
521static void insert_source_line(struct rb_root *root, struct source_line *src_line)
522{
523 struct source_line *iter;
524 struct rb_node **p = &root->rb_node;
525 struct rb_node *parent = NULL;
526
527 while (*p != NULL) {
528 parent = *p;
529 iter = rb_entry(parent, struct source_line, node);
530
531 if (src_line->percent > iter->percent)
532 p = &(*p)->rb_left;
533 else
534 p = &(*p)->rb_right;
535 }
536
537 rb_link_node(&src_line->node, parent, p);
538 rb_insert_color(&src_line->node, root);
539}
540
541static void symbol__free_source_line(struct symbol *sym, int len)
542{
543 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200544 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200545 int i;
546
547 for (i = 0; i < len; i++)
548 free(src_line[i].path);
549
550 free(src_line);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200551 notes->src->lines = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200552}
553
554/* Get the filename:line for the colored entries */
555static int symbol__get_source_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200556 int evidx, struct rb_root *root, int len,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200557 const char *filename)
558{
559 u64 start;
560 int i;
561 char cmd[PATH_MAX * 2];
562 struct source_line *src_line;
563 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200564 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200565
566 if (!h->sum)
567 return 0;
568
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200569 src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
570 if (!notes->src->lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200571 return -1;
572
David Millerf40a0632012-03-25 16:28:12 -0400573 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200574
575 for (i = 0; i < len; i++) {
576 char *path = NULL;
577 size_t line_len;
578 u64 offset;
579 FILE *fp;
580
581 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
582 if (src_line[i].percent <= 0.5)
583 continue;
584
585 offset = start + i;
586 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
587 fp = popen(cmd, "r");
588 if (!fp)
589 continue;
590
591 if (getline(&path, &line_len, fp) < 0 || !line_len)
592 goto next;
593
594 src_line[i].path = malloc(sizeof(char) * line_len + 1);
595 if (!src_line[i].path)
596 goto next;
597
598 strcpy(src_line[i].path, path);
599 insert_source_line(root, &src_line[i]);
600
601 next:
602 pclose(fp);
603 }
604
605 return 0;
606}
607
608static void print_summary(struct rb_root *root, const char *filename)
609{
610 struct source_line *src_line;
611 struct rb_node *node;
612
613 printf("\nSorted summary for file %s\n", filename);
614 printf("----------------------------------------------\n\n");
615
616 if (RB_EMPTY_ROOT(root)) {
617 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
618 return;
619 }
620
621 node = rb_first(root);
622 while (node) {
623 double percent;
624 const char *color;
625 char *path;
626
627 src_line = rb_entry(node, struct source_line, node);
628 percent = src_line->percent;
629 color = get_percent_color(percent);
630 path = src_line->path;
631
632 color_fprintf(stdout, color, " %7.2f %s", percent, path);
633 node = rb_next(node);
634 }
635}
636
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200637static void symbol__annotate_hits(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200638{
639 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200640 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300641 u64 len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200642
643 for (offset = 0; offset < len; ++offset)
644 if (h->addr[offset] != 0)
645 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
646 sym->start + offset, h->addr[offset]);
647 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
648}
649
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200650int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200651 bool full_paths, int min_pcnt, int max_lines,
652 int context)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200653{
654 struct dso *dso = map->dso;
655 const char *filename = dso->long_name, *d_filename;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200656 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300657 struct disasm_line *pos, *queue = NULL;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300658 u64 start = map__rip_2objdump(map, sym->start);
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200659 int printed = 2, queue_len = 0;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200660 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200661 u64 len;
662
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200663 if (full_paths)
664 d_filename = filename;
665 else
666 d_filename = basename(filename);
667
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300668 len = symbol__size(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200669
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200670 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
671 printf("------------------------------------------------\n");
672
673 if (verbose)
674 symbol__annotate_hits(sym, evidx);
675
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200676 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200677 if (context && queue == NULL) {
678 queue = pos;
679 queue_len = 0;
680 }
681
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300682 switch (disasm_line__print(pos, sym, start, evidx, len,
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300683 min_pcnt, printed, max_lines,
684 queue)) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200685 case 0:
686 ++printed;
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200687 if (context) {
688 printed += queue_len;
689 queue = NULL;
690 queue_len = 0;
691 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200692 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200693 case 1:
694 /* filtered by max_lines */
695 ++more;
696 break;
697 case -1:
698 default:
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200699 /*
700 * Filtered by min_pcnt or non IP lines when
701 * context != 0
702 */
703 if (!context)
704 break;
705 if (queue_len == context)
706 queue = list_entry(queue->node.next, typeof(*queue), node);
707 else
708 ++queue_len;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200709 break;
710 }
711 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200712
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200713 return more;
714}
715
716void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
717{
718 struct annotation *notes = symbol__annotation(sym);
719 struct sym_hist *h = annotation__histogram(notes, evidx);
720
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200721 memset(h, 0, notes->src->sizeof_sym_hist);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200722}
723
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200724void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200725{
726 struct annotation *notes = symbol__annotation(sym);
727 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300728 int len = symbol__size(sym), offset;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200729
730 h->sum = 0;
Arnaldo Carvalho de Melo8b84a562012-04-05 16:15:59 -0300731 for (offset = 0; offset < len; ++offset) {
732 h->addr[offset] = h->addr[offset] * 7 / 8;
733 h->sum += h->addr[offset];
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200734 }
735}
736
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300737void disasm__purge(struct list_head *head)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200738{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300739 struct disasm_line *pos, *n;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200740
741 list_for_each_entry_safe(pos, n, head, node) {
742 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300743 disasm_line__free(pos);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200744 }
745}
746
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300747static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
748{
749 size_t printed;
750
751 if (dl->offset == -1)
752 return fprintf(fp, "%s\n", dl->line);
753
754 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
755
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300756 if (dl->ops.raw[0] != '\0') {
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300757 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300758 dl->ops.raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300759 }
760
761 return printed + fprintf(fp, "\n");
762}
763
764size_t disasm__fprintf(struct list_head *head, FILE *fp)
765{
766 struct disasm_line *pos;
767 size_t printed = 0;
768
769 list_for_each_entry(pos, head, node)
770 printed += disasm_line__fprintf(pos, fp);
771
772 return printed;
773}
774
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200775int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
776 bool print_lines, bool full_paths, int min_pcnt,
777 int max_lines)
778{
779 struct dso *dso = map->dso;
780 const char *filename = dso->long_name;
781 struct rb_root source_line = RB_ROOT;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200782 u64 len;
783
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200784 if (symbol__annotate(sym, map, 0) < 0)
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200785 return -1;
786
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -0300787 len = symbol__size(sym);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200788
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200789 if (print_lines) {
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200790 symbol__get_source_line(sym, map, evidx, &source_line,
791 len, filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200792 print_summary(&source_line, filename);
793 }
794
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200795 symbol__annotate_printf(sym, map, evidx, full_paths,
Arnaldo Carvalho de Melod5e3d742011-02-08 15:29:25 -0200796 min_pcnt, max_lines, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200797 if (print_lines)
798 symbol__free_source_line(sym, len);
799
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300800 disasm__purge(&symbol__annotation(sym)->src->source);
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200801
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200802 return 0;
803}