]>
Commit | Line | Data |
---|---|---|
78f7defe ACM |
1 | /* |
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 | ||
a43783ae | 10 | #include <errno.h> |
fd20e811 | 11 | #include <inttypes.h> |
78f7defe | 12 | #include "util.h" |
48c65bda NK |
13 | #include "ui/ui.h" |
14 | #include "sort.h" | |
78f7defe ACM |
15 | #include "build-id.h" |
16 | #include "color.h" | |
17 | #include "cache.h" | |
18 | #include "symbol.h" | |
19 | #include "debug.h" | |
20 | #include "annotate.h" | |
db8fd07a | 21 | #include "evsel.h" |
70fbe057 | 22 | #include "block-range.h" |
a067558e | 23 | #include "string2.h" |
786c1b51 | 24 | #include "arch/common.h" |
e592488c | 25 | #include <regex.h> |
ce6f4fab | 26 | #include <pthread.h> |
4383db88 | 27 | #include <linux/bitops.h> |
877a7a11 | 28 | #include <linux/kernel.h> |
786c1b51 | 29 | #include <sys/utsname.h> |
78f7defe | 30 | |
3d689ed6 ACM |
31 | #include "sane_ctype.h" |
32 | ||
f69b64f7 | 33 | const char *disassembler_style; |
7a4ec938 | 34 | const char *objdump_path; |
e592488c | 35 | static regex_t file_lineno; |
f69b64f7 | 36 | |
75b49202 | 37 | static struct ins_ops *ins__find(struct arch *arch, const char *name); |
2a1ff812 | 38 | static void ins__sort(struct arch *arch); |
75b49202 | 39 | static int disasm_line__parse(char *line, const char **namep, char **rawp); |
7a997fe4 | 40 | |
786c1b51 ACM |
41 | struct arch { |
42 | const char *name; | |
763d8960 ACM |
43 | struct ins *instructions; |
44 | size_t nr_instructions; | |
2a1ff812 ACM |
45 | size_t nr_instructions_allocated; |
46 | struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); | |
763d8960 | 47 | bool sorted_instructions; |
0781ea92 ACM |
48 | bool initialized; |
49 | void *priv; | |
69fb09f6 JY |
50 | unsigned int model; |
51 | unsigned int family; | |
696e2457 | 52 | int (*init)(struct arch *arch, char *cpuid); |
69fb09f6 JY |
53 | bool (*ins_is_fused)(struct arch *arch, const char *ins1, |
54 | const char *ins2); | |
786c1b51 ACM |
55 | struct { |
56 | char comment_char; | |
9c2fb451 | 57 | char skip_functions_char; |
786c1b51 ACM |
58 | } objdump; |
59 | }; | |
60 | ||
763d8960 ACM |
61 | static struct ins_ops call_ops; |
62 | static struct ins_ops dec_ops; | |
63 | static struct ins_ops jump_ops; | |
64 | static struct ins_ops mov_ops; | |
65 | static struct ins_ops nop_ops; | |
66 | static struct ins_ops lock_ops; | |
67 | static struct ins_ops ret_ops; | |
68 | ||
2a1ff812 ACM |
69 | static int arch__grow_instructions(struct arch *arch) |
70 | { | |
71 | struct ins *new_instructions; | |
72 | size_t new_nr_allocated; | |
73 | ||
74 | if (arch->nr_instructions_allocated == 0 && arch->instructions) | |
75 | goto grow_from_non_allocated_table; | |
76 | ||
77 | new_nr_allocated = arch->nr_instructions_allocated + 128; | |
78 | new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); | |
79 | if (new_instructions == NULL) | |
80 | return -1; | |
81 | ||
82 | out_update_instructions: | |
83 | arch->instructions = new_instructions; | |
84 | arch->nr_instructions_allocated = new_nr_allocated; | |
85 | return 0; | |
86 | ||
87 | grow_from_non_allocated_table: | |
88 | new_nr_allocated = arch->nr_instructions + 128; | |
89 | new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); | |
90 | if (new_instructions == NULL) | |
91 | return -1; | |
92 | ||
93 | memcpy(new_instructions, arch->instructions, arch->nr_instructions); | |
94 | goto out_update_instructions; | |
95 | } | |
96 | ||
acc9bfb5 | 97 | static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) |
2a1ff812 ACM |
98 | { |
99 | struct ins *ins; | |
100 | ||
101 | if (arch->nr_instructions == arch->nr_instructions_allocated && | |
102 | arch__grow_instructions(arch)) | |
103 | return -1; | |
104 | ||
105 | ins = &arch->instructions[arch->nr_instructions]; | |
106 | ins->name = strdup(name); | |
107 | if (!ins->name) | |
108 | return -1; | |
109 | ||
110 | ins->ops = ops; | |
111 | arch->nr_instructions++; | |
112 | ||
113 | ins__sort(arch); | |
114 | return 0; | |
115 | } | |
116 | ||
763d8960 | 117 | #include "arch/arm/annotate/instructions.c" |
0fcb1da4 | 118 | #include "arch/arm64/annotate/instructions.c" |
763d8960 | 119 | #include "arch/x86/annotate/instructions.c" |
dbdebdc5 | 120 | #include "arch/powerpc/annotate/instructions.c" |
d9f8dfa9 | 121 | #include "arch/s390/annotate/instructions.c" |
763d8960 | 122 | |
786c1b51 ACM |
123 | static struct arch architectures[] = { |
124 | { | |
125 | .name = "arm", | |
acc9bfb5 | 126 | .init = arm__annotate_init, |
786c1b51 | 127 | }, |
0fcb1da4 KP |
128 | { |
129 | .name = "arm64", | |
130 | .init = arm64__annotate_init, | |
131 | }, | |
786c1b51 ACM |
132 | { |
133 | .name = "x86", | |
696e2457 | 134 | .init = x86__annotate_init, |
763d8960 ACM |
135 | .instructions = x86__instructions, |
136 | .nr_instructions = ARRAY_SIZE(x86__instructions), | |
69fb09f6 | 137 | .ins_is_fused = x86__ins_is_fused, |
786c1b51 ACM |
138 | .objdump = { |
139 | .comment_char = '#', | |
140 | }, | |
141 | }, | |
dbdebdc5 RB |
142 | { |
143 | .name = "powerpc", | |
144 | .init = powerpc__annotate_init, | |
145 | }, | |
e77852b3 CB |
146 | { |
147 | .name = "s390", | |
d9f8dfa9 | 148 | .init = s390__annotate_init, |
e77852b3 CB |
149 | .objdump = { |
150 | .comment_char = '#', | |
151 | }, | |
152 | }, | |
786c1b51 ACM |
153 | }; |
154 | ||
c46219ac ACM |
155 | static void ins__delete(struct ins_operands *ops) |
156 | { | |
3995614d ACM |
157 | if (ops == NULL) |
158 | return; | |
74cf249d ACM |
159 | zfree(&ops->source.raw); |
160 | zfree(&ops->source.name); | |
161 | zfree(&ops->target.raw); | |
162 | zfree(&ops->target.name); | |
c46219ac ACM |
163 | } |
164 | ||
5417072b ACM |
165 | static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, |
166 | struct ins_operands *ops) | |
167 | { | |
168 | return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw); | |
169 | } | |
170 | ||
171 | int ins__scnprintf(struct ins *ins, char *bf, size_t size, | |
172 | struct ins_operands *ops) | |
173 | { | |
174 | if (ins->ops->scnprintf) | |
175 | return ins->ops->scnprintf(ins, bf, size, ops); | |
176 | ||
177 | return ins__raw_scnprintf(ins, bf, size, ops); | |
178 | } | |
179 | ||
69fb09f6 JY |
180 | bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) |
181 | { | |
182 | if (!arch || !arch->ins_is_fused) | |
183 | return false; | |
184 | ||
185 | return arch->ins_is_fused(arch, ins1, ins2); | |
186 | } | |
187 | ||
9c2fb451 | 188 | static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map) |
d86b0597 | 189 | { |
d2232885 ACM |
190 | char *endptr, *tok, *name; |
191 | ||
44d1a3ed | 192 | ops->target.addr = strtoull(ops->raw, &endptr, 16); |
d2232885 ACM |
193 | |
194 | name = strchr(endptr, '<'); | |
195 | if (name == NULL) | |
196 | goto indirect_call; | |
197 | ||
198 | name++; | |
199 | ||
9c2fb451 ACM |
200 | if (arch->objdump.skip_functions_char && |
201 | strchr(name, arch->objdump.skip_functions_char)) | |
cfef25b8 | 202 | return -1; |
cfef25b8 | 203 | |
d2232885 ACM |
204 | tok = strchr(name, '>'); |
205 | if (tok == NULL) | |
206 | return -1; | |
207 | ||
208 | *tok = '\0'; | |
44d1a3ed | 209 | ops->target.name = strdup(name); |
d2232885 ACM |
210 | *tok = '>'; |
211 | ||
44d1a3ed | 212 | return ops->target.name == NULL ? -1 : 0; |
d2232885 ACM |
213 | |
214 | indirect_call: | |
88a7fcf9 RB |
215 | tok = strchr(endptr, '*'); |
216 | if (tok == NULL) { | |
5f62d4fd ACM |
217 | struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr)); |
218 | if (sym != NULL) | |
219 | ops->target.name = strdup(sym->name); | |
220 | else | |
221 | ops->target.addr = 0; | |
e8ea1561 ACM |
222 | return 0; |
223 | } | |
224 | ||
44d1a3ed | 225 | ops->target.addr = strtoull(tok + 1, NULL, 16); |
d86b0597 ACM |
226 | return 0; |
227 | } | |
228 | ||
d2232885 | 229 | static int call__scnprintf(struct ins *ins, char *bf, size_t size, |
5417072b | 230 | struct ins_operands *ops) |
d2232885 | 231 | { |
44d1a3ed ACM |
232 | if (ops->target.name) |
233 | return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name); | |
d2232885 | 234 | |
e8ea1561 ACM |
235 | if (ops->target.addr == 0) |
236 | return ins__raw_scnprintf(ins, bf, size, ops); | |
237 | ||
44d1a3ed | 238 | return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr); |
d2232885 ACM |
239 | } |
240 | ||
d86b0597 | 241 | static struct ins_ops call_ops = { |
d2232885 ACM |
242 | .parse = call__parse, |
243 | .scnprintf = call__scnprintf, | |
d86b0597 ACM |
244 | }; |
245 | ||
246 | bool ins__is_call(const struct ins *ins) | |
247 | { | |
248 | return ins->ops == &call_ops; | |
249 | } | |
250 | ||
786c1b51 | 251 | static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused) |
4f9d0325 | 252 | { |
c7e6ead7 | 253 | const char *s = strchr(ops->raw, '+'); |
3ee2eb6d | 254 | const char *c = strchr(ops->raw, ','); |
4f9d0325 | 255 | |
b13bbeee KP |
256 | /* |
257 | * skip over possible up to 2 operands to get to address, e.g.: | |
258 | * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> | |
259 | */ | |
260 | if (c++ != NULL) { | |
3ee2eb6d | 261 | ops->target.addr = strtoull(c, NULL, 16); |
b13bbeee KP |
262 | if (!ops->target.addr) { |
263 | c = strchr(c, ','); | |
264 | if (c++ != NULL) | |
265 | ops->target.addr = strtoull(c, NULL, 16); | |
266 | } | |
267 | } else { | |
3ee2eb6d | 268 | ops->target.addr = strtoull(ops->raw, NULL, 16); |
b13bbeee | 269 | } |
fb29fa58 | 270 | |
e216874c | 271 | if (s++ != NULL) { |
bbb7f846 | 272 | ops->target.offset = strtoull(s, NULL, 16); |
e216874c RB |
273 | ops->target.offset_avail = true; |
274 | } else { | |
275 | ops->target.offset_avail = false; | |
276 | } | |
4f9d0325 | 277 | |
4f9d0325 ACM |
278 | return 0; |
279 | } | |
280 | ||
c7e6ead7 | 281 | static int jump__scnprintf(struct ins *ins, char *bf, size_t size, |
5417072b | 282 | struct ins_operands *ops) |
28548d78 | 283 | { |
b13bbeee KP |
284 | const char *c = strchr(ops->raw, ','); |
285 | ||
e216874c | 286 | if (!ops->target.addr || ops->target.offset < 0) |
bec60e50 RB |
287 | return ins__raw_scnprintf(ins, bf, size, ops); |
288 | ||
b13bbeee KP |
289 | if (c != NULL) { |
290 | const char *c2 = strchr(c + 1, ','); | |
291 | ||
292 | /* check for 3-op insn */ | |
293 | if (c2 != NULL) | |
294 | c = c2; | |
295 | c++; | |
296 | ||
297 | /* mirror arch objdump's space-after-comma style */ | |
298 | if (*c == ' ') | |
299 | c++; | |
300 | } | |
301 | ||
302 | return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64, | |
303 | ins->name, c ? c - ops->raw : 0, ops->raw, | |
304 | ops->target.offset); | |
28548d78 ACM |
305 | } |
306 | ||
4f9d0325 | 307 | static struct ins_ops jump_ops = { |
c7e6ead7 ACM |
308 | .parse = jump__parse, |
309 | .scnprintf = jump__scnprintf, | |
4f9d0325 ACM |
310 | }; |
311 | ||
312 | bool ins__is_jump(const struct ins *ins) | |
313 | { | |
314 | return ins->ops == &jump_ops; | |
315 | } | |
316 | ||
6de783b6 ACM |
317 | static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) |
318 | { | |
319 | char *endptr, *name, *t; | |
320 | ||
321 | if (strstr(raw, "(%rip)") == NULL) | |
322 | return 0; | |
323 | ||
324 | *addrp = strtoull(comment, &endptr, 16); | |
325 | name = strchr(endptr, '<'); | |
326 | if (name == NULL) | |
327 | return -1; | |
328 | ||
329 | name++; | |
330 | ||
331 | t = strchr(name, '>'); | |
332 | if (t == NULL) | |
333 | return 0; | |
334 | ||
335 | *t = '\0'; | |
336 | *namep = strdup(name); | |
337 | *t = '>'; | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
786c1b51 | 342 | static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map) |
7a997fe4 | 343 | { |
7a997fe4 ACM |
344 | ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); |
345 | if (ops->locked.ops == NULL) | |
346 | return 0; | |
347 | ||
75b49202 | 348 | if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) |
7a997fe4 ACM |
349 | goto out_free_ops; |
350 | ||
75b49202 | 351 | ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); |
0fb9f2aa | 352 | |
75b49202 | 353 | if (ops->locked.ins.ops == NULL) |
2ba34aaa | 354 | goto out_free_ops; |
7a997fe4 | 355 | |
75b49202 ACM |
356 | if (ops->locked.ins.ops->parse && |
357 | ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0) | |
be81908c | 358 | goto out_free_ops; |
7a997fe4 ACM |
359 | |
360 | return 0; | |
361 | ||
362 | out_free_ops: | |
04662523 | 363 | zfree(&ops->locked.ops); |
7a997fe4 ACM |
364 | return 0; |
365 | } | |
366 | ||
367 | static int lock__scnprintf(struct ins *ins, char *bf, size_t size, | |
368 | struct ins_operands *ops) | |
369 | { | |
370 | int printed; | |
371 | ||
75b49202 | 372 | if (ops->locked.ins.ops == NULL) |
7a997fe4 ACM |
373 | return ins__raw_scnprintf(ins, bf, size, ops); |
374 | ||
375 | printed = scnprintf(bf, size, "%-6.6s ", ins->name); | |
75b49202 | 376 | return printed + ins__scnprintf(&ops->locked.ins, bf + printed, |
7a997fe4 ACM |
377 | size - printed, ops->locked.ops); |
378 | } | |
379 | ||
c46219ac ACM |
380 | static void lock__delete(struct ins_operands *ops) |
381 | { | |
75b49202 | 382 | struct ins *ins = &ops->locked.ins; |
0fb9f2aa | 383 | |
75b49202 | 384 | if (ins->ops && ins->ops->free) |
0fb9f2aa RV |
385 | ins->ops->free(ops->locked.ops); |
386 | else | |
387 | ins__delete(ops->locked.ops); | |
388 | ||
74cf249d ACM |
389 | zfree(&ops->locked.ops); |
390 | zfree(&ops->target.raw); | |
391 | zfree(&ops->target.name); | |
c46219ac ACM |
392 | } |
393 | ||
7a997fe4 | 394 | static struct ins_ops lock_ops = { |
c46219ac | 395 | .free = lock__delete, |
7a997fe4 ACM |
396 | .parse = lock__parse, |
397 | .scnprintf = lock__scnprintf, | |
398 | }; | |
399 | ||
786c1b51 | 400 | static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused) |
6de783b6 ACM |
401 | { |
402 | char *s = strchr(ops->raw, ','), *target, *comment, prev; | |
403 | ||
404 | if (s == NULL) | |
405 | return -1; | |
406 | ||
407 | *s = '\0'; | |
408 | ops->source.raw = strdup(ops->raw); | |
409 | *s = ','; | |
48000a1a | 410 | |
6de783b6 ACM |
411 | if (ops->source.raw == NULL) |
412 | return -1; | |
413 | ||
414 | target = ++s; | |
786c1b51 | 415 | comment = strchr(s, arch->objdump.comment_char); |
1e2bb043 AC |
416 | |
417 | if (comment != NULL) | |
418 | s = comment - 1; | |
419 | else | |
420 | s = strchr(s, '\0') - 1; | |
6de783b6 | 421 | |
1e2bb043 AC |
422 | while (s > target && isspace(s[0])) |
423 | --s; | |
424 | s++; | |
6de783b6 ACM |
425 | prev = *s; |
426 | *s = '\0'; | |
427 | ||
428 | ops->target.raw = strdup(target); | |
429 | *s = prev; | |
430 | ||
431 | if (ops->target.raw == NULL) | |
432 | goto out_free_source; | |
433 | ||
6de783b6 ACM |
434 | if (comment == NULL) |
435 | return 0; | |
436 | ||
4597cf06 | 437 | comment = ltrim(comment); |
6de783b6 ACM |
438 | comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name); |
439 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); | |
440 | ||
441 | return 0; | |
442 | ||
443 | out_free_source: | |
04662523 | 444 | zfree(&ops->source.raw); |
6de783b6 ACM |
445 | return -1; |
446 | } | |
447 | ||
448 | static int mov__scnprintf(struct ins *ins, char *bf, size_t size, | |
449 | struct ins_operands *ops) | |
450 | { | |
451 | return scnprintf(bf, size, "%-6.6s %s,%s", ins->name, | |
452 | ops->source.name ?: ops->source.raw, | |
453 | ops->target.name ?: ops->target.raw); | |
454 | } | |
455 | ||
456 | static struct ins_ops mov_ops = { | |
457 | .parse = mov__parse, | |
458 | .scnprintf = mov__scnprintf, | |
459 | }; | |
460 | ||
786c1b51 | 461 | static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused) |
a43712c4 ACM |
462 | { |
463 | char *target, *comment, *s, prev; | |
464 | ||
465 | target = s = ops->raw; | |
466 | ||
467 | while (s[0] != '\0' && !isspace(s[0])) | |
468 | ++s; | |
469 | prev = *s; | |
470 | *s = '\0'; | |
471 | ||
472 | ops->target.raw = strdup(target); | |
473 | *s = prev; | |
474 | ||
475 | if (ops->target.raw == NULL) | |
476 | return -1; | |
477 | ||
859afa6c | 478 | comment = strchr(s, arch->objdump.comment_char); |
a43712c4 ACM |
479 | if (comment == NULL) |
480 | return 0; | |
481 | ||
4597cf06 | 482 | comment = ltrim(comment); |
a43712c4 ACM |
483 | comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name); |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
488 | static int dec__scnprintf(struct ins *ins, char *bf, size_t size, | |
489 | struct ins_operands *ops) | |
490 | { | |
491 | return scnprintf(bf, size, "%-6.6s %s", ins->name, | |
492 | ops->target.name ?: ops->target.raw); | |
493 | } | |
494 | ||
495 | static struct ins_ops dec_ops = { | |
496 | .parse = dec__parse, | |
497 | .scnprintf = dec__scnprintf, | |
498 | }; | |
499 | ||
1d037ca1 IT |
500 | static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, |
501 | struct ins_operands *ops __maybe_unused) | |
b9818e93 ACM |
502 | { |
503 | return scnprintf(bf, size, "%-6.6s", "nop"); | |
504 | } | |
505 | ||
506 | static struct ins_ops nop_ops = { | |
507 | .scnprintf = nop__scnprintf, | |
508 | }; | |
509 | ||
6ef94929 NR |
510 | static struct ins_ops ret_ops = { |
511 | .scnprintf = ins__raw_scnprintf, | |
512 | }; | |
513 | ||
514 | bool ins__is_ret(const struct ins *ins) | |
515 | { | |
516 | return ins->ops == &ret_ops; | |
517 | } | |
518 | ||
7e63a13a JY |
519 | bool ins__is_lock(const struct ins *ins) |
520 | { | |
521 | return ins->ops == &lock_ops; | |
522 | } | |
523 | ||
7e4c1498 | 524 | static int ins__key_cmp(const void *name, const void *insp) |
4f9d0325 ACM |
525 | { |
526 | const struct ins *ins = insp; | |
527 | ||
528 | return strcmp(name, ins->name); | |
529 | } | |
530 | ||
7e4c1498 CR |
531 | static int ins__cmp(const void *a, const void *b) |
532 | { | |
533 | const struct ins *ia = a; | |
534 | const struct ins *ib = b; | |
535 | ||
536 | return strcmp(ia->name, ib->name); | |
537 | } | |
538 | ||
763d8960 | 539 | static void ins__sort(struct arch *arch) |
7e4c1498 | 540 | { |
763d8960 | 541 | const int nmemb = arch->nr_instructions; |
7e4c1498 | 542 | |
763d8960 | 543 | qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); |
7e4c1498 CR |
544 | } |
545 | ||
2a1ff812 | 546 | static struct ins_ops *__ins__find(struct arch *arch, const char *name) |
4f9d0325 | 547 | { |
75b49202 | 548 | struct ins *ins; |
763d8960 | 549 | const int nmemb = arch->nr_instructions; |
7e4c1498 | 550 | |
763d8960 ACM |
551 | if (!arch->sorted_instructions) { |
552 | ins__sort(arch); | |
553 | arch->sorted_instructions = true; | |
7e4c1498 | 554 | } |
4f9d0325 | 555 | |
75b49202 ACM |
556 | ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); |
557 | return ins ? ins->ops : NULL; | |
4f9d0325 ACM |
558 | } |
559 | ||
2a1ff812 ACM |
560 | static struct ins_ops *ins__find(struct arch *arch, const char *name) |
561 | { | |
562 | struct ins_ops *ops = __ins__find(arch, name); | |
563 | ||
564 | if (!ops && arch->associate_instruction_ops) | |
565 | ops = arch->associate_instruction_ops(arch, name); | |
566 | ||
567 | return ops; | |
568 | } | |
569 | ||
786c1b51 ACM |
570 | static int arch__key_cmp(const void *name, const void *archp) |
571 | { | |
572 | const struct arch *arch = archp; | |
573 | ||
574 | return strcmp(name, arch->name); | |
575 | } | |
576 | ||
577 | static int arch__cmp(const void *a, const void *b) | |
578 | { | |
579 | const struct arch *aa = a; | |
580 | const struct arch *ab = b; | |
581 | ||
582 | return strcmp(aa->name, ab->name); | |
583 | } | |
584 | ||
585 | static void arch__sort(void) | |
586 | { | |
587 | const int nmemb = ARRAY_SIZE(architectures); | |
588 | ||
589 | qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); | |
590 | } | |
591 | ||
592 | static struct arch *arch__find(const char *name) | |
593 | { | |
594 | const int nmemb = ARRAY_SIZE(architectures); | |
595 | static bool sorted; | |
596 | ||
597 | if (!sorted) { | |
598 | arch__sort(); | |
599 | sorted = true; | |
600 | } | |
601 | ||
602 | return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); | |
603 | } | |
604 | ||
d04b35f8 | 605 | int symbol__alloc_hist(struct symbol *sym) |
ce6f4fab ACM |
606 | { |
607 | struct annotation *notes = symbol__annotation(sym); | |
331c7cb3 | 608 | size_t size = symbol__size(sym); |
8696329b CS |
609 | size_t sizeof_sym_hist; |
610 | ||
331c7cb3 RB |
611 | /* |
612 | * Add buffer of one element for zero length symbol. | |
613 | * When sample is taken from first instruction of | |
614 | * zero length symbol, perf still resolves it and | |
615 | * shows symbol name in perf report and allows to | |
616 | * annotate it. | |
617 | */ | |
618 | if (size == 0) | |
619 | size = 1; | |
620 | ||
8696329b | 621 | /* Check for overflow when calculating sizeof_sym_hist */ |
896bccd3 | 622 | if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry)) |
8696329b CS |
623 | return -1; |
624 | ||
896bccd3 | 625 | sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry)); |
8696329b CS |
626 | |
627 | /* Check for overflow in zalloc argument */ | |
628 | if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src)) | |
629 | / symbol_conf.nr_events) | |
630 | return -1; | |
ce6f4fab | 631 | |
d04b35f8 | 632 | notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist); |
ce6f4fab ACM |
633 | if (notes->src == NULL) |
634 | return -1; | |
635 | notes->src->sizeof_sym_hist = sizeof_sym_hist; | |
d04b35f8 | 636 | notes->src->nr_histograms = symbol_conf.nr_events; |
ce6f4fab ACM |
637 | INIT_LIST_HEAD(¬es->src->source); |
638 | return 0; | |
78f7defe ACM |
639 | } |
640 | ||
d4957633 AK |
641 | /* The cycles histogram is lazily allocated. */ |
642 | static int symbol__alloc_hist_cycles(struct symbol *sym) | |
643 | { | |
644 | struct annotation *notes = symbol__annotation(sym); | |
645 | const size_t size = symbol__size(sym); | |
646 | ||
647 | notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); | |
648 | if (notes->src->cycles_hist == NULL) | |
649 | return -1; | |
650 | return 0; | |
651 | } | |
652 | ||
36532461 ACM |
653 | void symbol__annotate_zero_histograms(struct symbol *sym) |
654 | { | |
655 | struct annotation *notes = symbol__annotation(sym); | |
656 | ||
ce6f4fab | 657 | pthread_mutex_lock(¬es->lock); |
d4957633 | 658 | if (notes->src != NULL) { |
ce6f4fab ACM |
659 | memset(notes->src->histograms, 0, |
660 | notes->src->nr_histograms * notes->src->sizeof_sym_hist); | |
d4957633 AK |
661 | if (notes->src->cycles_hist) |
662 | memset(notes->src->cycles_hist, 0, | |
663 | symbol__size(sym) * sizeof(struct cyc_hist)); | |
664 | } | |
ce6f4fab | 665 | pthread_mutex_unlock(¬es->lock); |
36532461 ACM |
666 | } |
667 | ||
d4957633 AK |
668 | static int __symbol__account_cycles(struct annotation *notes, |
669 | u64 start, | |
670 | unsigned offset, unsigned cycles, | |
671 | unsigned have_start) | |
672 | { | |
673 | struct cyc_hist *ch; | |
674 | ||
675 | ch = notes->src->cycles_hist; | |
676 | /* | |
677 | * For now we can only account one basic block per | |
678 | * final jump. But multiple could be overlapping. | |
679 | * Always account the longest one. So when | |
680 | * a shorter one has been already seen throw it away. | |
681 | * | |
682 | * We separately always account the full cycles. | |
683 | */ | |
684 | ch[offset].num_aggr++; | |
685 | ch[offset].cycles_aggr += cycles; | |
686 | ||
687 | if (!have_start && ch[offset].have_start) | |
688 | return 0; | |
689 | if (ch[offset].num) { | |
690 | if (have_start && (!ch[offset].have_start || | |
691 | ch[offset].start > start)) { | |
692 | ch[offset].have_start = 0; | |
693 | ch[offset].cycles = 0; | |
694 | ch[offset].num = 0; | |
695 | if (ch[offset].reset < 0xffff) | |
696 | ch[offset].reset++; | |
697 | } else if (have_start && | |
698 | ch[offset].start < start) | |
699 | return 0; | |
700 | } | |
701 | ch[offset].have_start = have_start; | |
702 | ch[offset].start = start; | |
703 | ch[offset].cycles += cycles; | |
704 | ch[offset].num++; | |
705 | return 0; | |
706 | } | |
707 | ||
b66d8c0c | 708 | static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
bab89f6a | 709 | struct annotation *notes, int evidx, u64 addr, |
461c17f0 | 710 | struct perf_sample *sample) |
78f7defe | 711 | { |
2f525d01 | 712 | unsigned offset; |
78f7defe ACM |
713 | struct sym_hist *h; |
714 | ||
78f7defe ACM |
715 | pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); |
716 | ||
edee44be RB |
717 | if ((addr < sym->start || addr >= sym->end) && |
718 | (addr != sym->end || sym->start != sym->end)) { | |
e3d006ce ACM |
719 | pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", |
720 | __func__, __LINE__, sym->name, sym->start, addr, sym->end); | |
31d68e7b | 721 | return -ERANGE; |
e3d006ce | 722 | } |
78f7defe | 723 | |
2f525d01 ACM |
724 | offset = addr - sym->start; |
725 | h = annotation__histogram(notes, evidx); | |
8158683d | 726 | h->nr_samples++; |
896bccd3 | 727 | h->addr[offset].nr_samples++; |
461c17f0 TS |
728 | h->period += sample->period; |
729 | h->addr[offset].period += sample->period; | |
78f7defe ACM |
730 | |
731 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 | |
461c17f0 TS |
732 | ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", |
733 | sym->start, sym->name, addr, addr - sym->start, evidx, | |
734 | h->addr[offset].nr_samples, h->addr[offset].period); | |
78f7defe ACM |
735 | return 0; |
736 | } | |
737 | ||
d4957633 | 738 | static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles) |
83be34a7 AK |
739 | { |
740 | struct annotation *notes = symbol__annotation(sym); | |
741 | ||
742 | if (notes->src == NULL) { | |
743 | if (symbol__alloc_hist(sym) < 0) | |
744 | return NULL; | |
745 | } | |
d4957633 AK |
746 | if (!notes->src->cycles_hist && cycles) { |
747 | if (symbol__alloc_hist_cycles(sym) < 0) | |
748 | return NULL; | |
749 | } | |
83be34a7 AK |
750 | return notes; |
751 | } | |
752 | ||
44e83039 | 753 | static int symbol__inc_addr_samples(struct symbol *sym, struct map *map, |
bab89f6a TS |
754 | int evidx, u64 addr, |
755 | struct perf_sample *sample) | |
b66d8c0c ACM |
756 | { |
757 | struct annotation *notes; | |
758 | ||
48c65bda | 759 | if (sym == NULL) |
b66d8c0c | 760 | return 0; |
d4957633 | 761 | notes = symbol__get_annotation(sym, false); |
83be34a7 AK |
762 | if (notes == NULL) |
763 | return -ENOMEM; | |
bab89f6a | 764 | return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample); |
b66d8c0c ACM |
765 | } |
766 | ||
d4957633 AK |
767 | static int symbol__account_cycles(u64 addr, u64 start, |
768 | struct symbol *sym, unsigned cycles) | |
769 | { | |
770 | struct annotation *notes; | |
771 | unsigned offset; | |
772 | ||
773 | if (sym == NULL) | |
774 | return 0; | |
775 | notes = symbol__get_annotation(sym, true); | |
776 | if (notes == NULL) | |
777 | return -ENOMEM; | |
778 | if (addr < sym->start || addr >= sym->end) | |
779 | return -ERANGE; | |
780 | ||
781 | if (start) { | |
782 | if (start < sym->start || start >= sym->end) | |
783 | return -ERANGE; | |
784 | if (start >= addr) | |
785 | start = 0; | |
786 | } | |
787 | offset = addr - sym->start; | |
788 | return __symbol__account_cycles(notes, | |
789 | start ? start - sym->start : 0, | |
790 | offset, cycles, | |
791 | !!start); | |
792 | } | |
793 | ||
794 | int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, | |
795 | struct addr_map_symbol *start, | |
796 | unsigned cycles) | |
797 | { | |
3d7245b0 | 798 | u64 saddr = 0; |
d4957633 AK |
799 | int err; |
800 | ||
801 | if (!cycles) | |
802 | return 0; | |
803 | ||
804 | /* | |
805 | * Only set start when IPC can be computed. We can only | |
806 | * compute it when the basic block is completely in a single | |
807 | * function. | |
808 | * Special case the case when the jump is elsewhere, but | |
809 | * it starts on the function start. | |
810 | */ | |
811 | if (start && | |
812 | (start->sym == ams->sym || | |
813 | (ams->sym && | |
814 | start->addr == ams->sym->start + ams->map->start))) | |
815 | saddr = start->al_addr; | |
816 | if (saddr == 0) | |
3d7245b0 | 817 | pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n", |
d4957633 AK |
818 | ams->addr, |
819 | start ? start->addr : 0, | |
820 | ams->sym ? ams->sym->start + ams->map->start : 0, | |
821 | saddr); | |
822 | err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles); | |
823 | if (err) | |
824 | pr_debug2("account_cycles failed %d\n", err); | |
825 | return err; | |
826 | } | |
827 | ||
bab89f6a TS |
828 | int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, |
829 | int evidx) | |
0f4e7a24 | 830 | { |
bab89f6a | 831 | return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample); |
0f4e7a24 ACM |
832 | } |
833 | ||
bab89f6a TS |
834 | int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, |
835 | int evidx, u64 ip) | |
f626adff | 836 | { |
bab89f6a | 837 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample); |
f626adff ACM |
838 | } |
839 | ||
786c1b51 | 840 | static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map) |
4f9d0325 | 841 | { |
75b49202 | 842 | dl->ins.ops = ins__find(arch, dl->ins.name); |
4f9d0325 | 843 | |
75b49202 | 844 | if (!dl->ins.ops) |
4f9d0325 ACM |
845 | return; |
846 | ||
75b49202 ACM |
847 | if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0) |
848 | dl->ins.ops = NULL; | |
4f9d0325 ACM |
849 | } |
850 | ||
75b49202 | 851 | static int disasm_line__parse(char *line, const char **namep, char **rawp) |
7a997fe4 | 852 | { |
4597cf06 | 853 | char tmp, *name = ltrim(line); |
7a997fe4 ACM |
854 | |
855 | if (name[0] == '\0') | |
856 | return -1; | |
857 | ||
858 | *rawp = name + 1; | |
859 | ||
860 | while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) | |
861 | ++*rawp; | |
862 | ||
863 | tmp = (*rawp)[0]; | |
864 | (*rawp)[0] = '\0'; | |
865 | *namep = strdup(name); | |
866 | ||
867 | if (*namep == NULL) | |
868 | goto out_free_name; | |
869 | ||
870 | (*rawp)[0] = tmp; | |
4597cf06 | 871 | *rawp = ltrim(*rawp); |
7a997fe4 ACM |
872 | |
873 | return 0; | |
874 | ||
875 | out_free_name: | |
75b49202 ACM |
876 | free((void *)namep); |
877 | *namep = NULL; | |
7a997fe4 ACM |
878 | return -1; |
879 | } | |
880 | ||
ea07c5aa JO |
881 | struct annotate_args { |
882 | size_t privsize; | |
24fe7b88 | 883 | struct arch *arch; |
1a04db70 | 884 | struct map *map; |
d03a686e | 885 | struct perf_evsel *evsel; |
4748834f JO |
886 | s64 offset; |
887 | char *line; | |
888 | int line_nr; | |
ea07c5aa JO |
889 | }; |
890 | ||
c835e191 JO |
891 | static void annotation_line__delete(struct annotation_line *al) |
892 | { | |
893 | void *ptr = (void *) al - al->privsize; | |
894 | ||
895 | zfree(&al->line); | |
896 | free(ptr); | |
897 | } | |
898 | ||
899 | /* | |
900 | * Allocating the annotation line data with following | |
901 | * structure: | |
902 | * | |
903 | * -------------------------------------- | |
904 | * private space | struct annotation_line | |
905 | * -------------------------------------- | |
906 | * | |
907 | * Size of the private space is stored in 'struct annotation_line'. | |
908 | * | |
909 | */ | |
910 | static struct annotation_line * | |
911 | annotation_line__new(struct annotate_args *args, size_t privsize) | |
912 | { | |
913 | struct annotation_line *al; | |
7e304557 | 914 | struct perf_evsel *evsel = args->evsel; |
c835e191 | 915 | size_t size = privsize + sizeof(*al); |
7e304557 JO |
916 | int nr = 1; |
917 | ||
918 | if (perf_evsel__is_group_event(evsel)) | |
919 | nr = evsel->nr_members; | |
920 | ||
921 | size += sizeof(al->samples[0]) * nr; | |
c835e191 JO |
922 | |
923 | al = zalloc(size); | |
924 | if (al) { | |
925 | al = (void *) al + privsize; | |
926 | al->privsize = privsize; | |
927 | al->offset = args->offset; | |
928 | al->line = strdup(args->line); | |
929 | al->line_nr = args->line_nr; | |
7e304557 | 930 | al->samples_nr = nr; |
c835e191 JO |
931 | } |
932 | ||
933 | return al; | |
934 | } | |
935 | ||
936 | /* | |
937 | * Allocating the disasm annotation line data with | |
938 | * following structure: | |
939 | * | |
940 | * ------------------------------------------------------------ | |
941 | * privsize space | struct disasm_line | struct annotation_line | |
942 | * ------------------------------------------------------------ | |
943 | * | |
944 | * We have 'struct annotation_line' member as last member | |
945 | * of 'struct disasm_line' to have an easy access. | |
946 | * | |
947 | */ | |
4748834f | 948 | static struct disasm_line *disasm_line__new(struct annotate_args *args) |
78f7defe | 949 | { |
c835e191 JO |
950 | struct disasm_line *dl = NULL; |
951 | struct annotation_line *al; | |
952 | size_t privsize = args->privsize + offsetof(struct disasm_line, al); | |
78f7defe | 953 | |
c835e191 JO |
954 | al = annotation_line__new(args, privsize); |
955 | if (al != NULL) { | |
956 | dl = disasm_line(al); | |
d5490b96 JO |
957 | |
958 | if (dl->al.line == NULL) | |
058b4cc9 | 959 | goto out_delete; |
5145418b | 960 | |
4748834f | 961 | if (args->offset != -1) { |
d5490b96 | 962 | if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) |
5145418b ACM |
963 | goto out_free_line; |
964 | ||
1a04db70 | 965 | disasm_line__init_ins(dl, args->arch, args->map); |
5145418b | 966 | } |
78f7defe ACM |
967 | } |
968 | ||
29ed6e76 | 969 | return dl; |
5145418b ACM |
970 | |
971 | out_free_line: | |
d5490b96 | 972 | zfree(&dl->al.line); |
058b4cc9 | 973 | out_delete: |
29ed6e76 | 974 | free(dl); |
058b4cc9 | 975 | return NULL; |
78f7defe ACM |
976 | } |
977 | ||
29ed6e76 | 978 | void disasm_line__free(struct disasm_line *dl) |
78f7defe | 979 | { |
75b49202 ACM |
980 | if (dl->ins.ops && dl->ins.ops->free) |
981 | dl->ins.ops->free(&dl->ops); | |
c46219ac ACM |
982 | else |
983 | ins__delete(&dl->ops); | |
75b49202 ACM |
984 | free((void *)dl->ins.name); |
985 | dl->ins.name = NULL; | |
c835e191 | 986 | annotation_line__delete(&dl->al); |
78f7defe ACM |
987 | } |
988 | ||
5417072b ACM |
989 | int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) |
990 | { | |
75b49202 ACM |
991 | if (raw || !dl->ins.ops) |
992 | return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw); | |
5417072b | 993 | |
75b49202 | 994 | return ins__scnprintf(&dl->ins, bf, size, &dl->ops); |
5417072b ACM |
995 | } |
996 | ||
82b9d7ff | 997 | static void annotation_line__add(struct annotation_line *al, struct list_head *head) |
78f7defe | 998 | { |
82b9d7ff | 999 | list_add_tail(&al->node, head); |
78f7defe ACM |
1000 | } |
1001 | ||
c4c72436 JO |
1002 | struct annotation_line * |
1003 | annotation_line__next(struct annotation_line *pos, struct list_head *head) | |
78f7defe | 1004 | { |
c4c72436 JO |
1005 | list_for_each_entry_continue(pos, head, node) |
1006 | if (pos->offset >= 0) | |
78f7defe ACM |
1007 | return pos; |
1008 | ||
1009 | return NULL; | |
1010 | } | |
1011 | ||
e64aa75b | 1012 | double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset, |
896bccd3 | 1013 | s64 end, const char **path, struct sym_hist_entry *sample) |
e5ccf9f4 NK |
1014 | { |
1015 | struct source_line *src_line = notes->src->lines; | |
e5ccf9f4 | 1016 | double percent = 0.0; |
896bccd3 | 1017 | |
461c17f0 | 1018 | sample->nr_samples = sample->period = 0; |
e5ccf9f4 | 1019 | |
bd64fcb8 | 1020 | if (src_line) { |
1491c22a | 1021 | size_t sizeof_src_line = sizeof(*src_line) + |
276af92f | 1022 | sizeof(src_line->samples) * (src_line->nr_pcnt - 1); |
1491c22a | 1023 | |
bd64fcb8 | 1024 | while (offset < end) { |
1491c22a NK |
1025 | src_line = (void *)notes->src->lines + |
1026 | (sizeof_src_line * offset); | |
1027 | ||
e5ccf9f4 | 1028 | if (*path == NULL) |
1491c22a | 1029 | *path = src_line->path; |
e5ccf9f4 | 1030 | |
276af92f | 1031 | percent += src_line->samples[evidx].percent; |
896bccd3 | 1032 | sample->nr_samples += src_line->samples[evidx].nr; |
1491c22a | 1033 | offset++; |
bd64fcb8 NK |
1034 | } |
1035 | } else { | |
1491c22a NK |
1036 | struct sym_hist *h = annotation__histogram(notes, evidx); |
1037 | unsigned int hits = 0; | |
461c17f0 | 1038 | u64 period = 0; |
1491c22a | 1039 | |
461c17f0 | 1040 | while (offset < end) { |
48cc3308 ACM |
1041 | hits += h->addr[offset].nr_samples; |
1042 | period += h->addr[offset].period; | |
1043 | ++offset; | |
461c17f0 | 1044 | } |
e5ccf9f4 | 1045 | |
8158683d | 1046 | if (h->nr_samples) { |
461c17f0 | 1047 | sample->period = period; |
896bccd3 | 1048 | sample->nr_samples = hits; |
8158683d | 1049 | percent = 100.0 * hits / h->nr_samples; |
0c4a5bce | 1050 | } |
bd64fcb8 | 1051 | } |
e5ccf9f4 NK |
1052 | |
1053 | return percent; | |
1054 | } | |
1055 | ||
70fbe057 PZ |
1056 | static const char *annotate__address_color(struct block_range *br) |
1057 | { | |
1058 | double cov = block_range__coverage(br); | |
1059 | ||
1060 | if (cov >= 0) { | |
1061 | /* mark red for >75% coverage */ | |
1062 | if (cov > 0.75) | |
1063 | return PERF_COLOR_RED; | |
1064 | ||
1065 | /* mark dull for <1% coverage */ | |
1066 | if (cov < 0.01) | |
1067 | return PERF_COLOR_NORMAL; | |
1068 | } | |
1069 | ||
1070 | return PERF_COLOR_MAGENTA; | |
1071 | } | |
1072 | ||
1073 | static const char *annotate__asm_color(struct block_range *br) | |
1074 | { | |
1075 | double cov = block_range__coverage(br); | |
1076 | ||
1077 | if (cov >= 0) { | |
1078 | /* mark dull for <1% coverage */ | |
1079 | if (cov < 0.01) | |
1080 | return PERF_COLOR_NORMAL; | |
1081 | } | |
1082 | ||
1083 | return PERF_COLOR_BLUE; | |
1084 | } | |
1085 | ||
1086 | static void annotate__branch_printf(struct block_range *br, u64 addr) | |
1087 | { | |
1088 | bool emit_comment = true; | |
1089 | ||
1090 | if (!br) | |
1091 | return; | |
1092 | ||
1093 | #if 1 | |
1094 | if (br->is_target && br->start == addr) { | |
1095 | struct block_range *branch = br; | |
1096 | double p; | |
1097 | ||
1098 | /* | |
1099 | * Find matching branch to our target. | |
1100 | */ | |
1101 | while (!branch->is_branch) | |
1102 | branch = block_range__next(branch); | |
1103 | ||
1104 | p = 100 *(double)br->entry / branch->coverage; | |
1105 | ||
1106 | if (p > 0.1) { | |
1107 | if (emit_comment) { | |
1108 | emit_comment = false; | |
1109 | printf("\t#"); | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * The percentage of coverage joined at this target in relation | |
1114 | * to the next branch. | |
1115 | */ | |
1116 | printf(" +%.2f%%", p); | |
1117 | } | |
1118 | } | |
1119 | #endif | |
1120 | if (br->is_branch && br->end == addr) { | |
1121 | double p = 100*(double)br->taken / br->coverage; | |
1122 | ||
1123 | if (p > 0.1) { | |
1124 | if (emit_comment) { | |
1125 | emit_comment = false; | |
1126 | printf("\t#"); | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * The percentage of coverage leaving at this branch, and | |
1131 | * its prediction ratio. | |
1132 | */ | |
1133 | printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); | |
1134 | } | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | ||
29ed6e76 | 1139 | static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start, |
db8fd07a | 1140 | struct perf_evsel *evsel, u64 len, int min_pcnt, int printed, |
29ed6e76 | 1141 | int max_lines, struct disasm_line *queue) |
78f7defe ACM |
1142 | { |
1143 | static const char *prev_line; | |
1144 | static const char *prev_color; | |
1145 | ||
d5490b96 | 1146 | if (dl->al.offset != -1) { |
78f7defe | 1147 | const char *path = NULL; |
b1dd4432 NK |
1148 | double percent, max_percent = 0.0; |
1149 | double *ppercents = &percent; | |
896bccd3 TS |
1150 | struct sym_hist_entry sample; |
1151 | struct sym_hist_entry *psamples = &sample; | |
b1dd4432 | 1152 | int i, nr_percent = 1; |
78f7defe ACM |
1153 | const char *color; |
1154 | struct annotation *notes = symbol__annotation(sym); | |
d5490b96 | 1155 | s64 offset = dl->al.offset; |
058b4cc9 | 1156 | const u64 addr = start + offset; |
c4c72436 | 1157 | struct annotation_line *next; |
70fbe057 | 1158 | struct block_range *br; |
ce6f4fab | 1159 | |
c4c72436 | 1160 | next = annotation_line__next(&dl->al, ¬es->src->source); |
78f7defe | 1161 | |
759ff497 | 1162 | if (perf_evsel__is_group_event(evsel)) { |
b1dd4432 NK |
1163 | nr_percent = evsel->nr_members; |
1164 | ppercents = calloc(nr_percent, sizeof(double)); | |
896bccd3 | 1165 | psamples = calloc(nr_percent, sizeof(struct sym_hist_entry)); |
0c4a5bce | 1166 | if (ppercents == NULL || psamples == NULL) { |
b1dd4432 | 1167 | return -1; |
0c4a5bce | 1168 | } |
b1dd4432 NK |
1169 | } |
1170 | ||
1171 | for (i = 0; i < nr_percent; i++) { | |
1172 | percent = disasm__calc_percent(notes, | |
1491c22a NK |
1173 | notes->src->lines ? i : evsel->idx + i, |
1174 | offset, | |
c4c72436 | 1175 | next ? next->offset : (s64) len, |
896bccd3 | 1176 | &path, &sample); |
b1dd4432 NK |
1177 | |
1178 | ppercents[i] = percent; | |
896bccd3 | 1179 | psamples[i] = sample; |
b1dd4432 NK |
1180 | if (percent > max_percent) |
1181 | max_percent = percent; | |
1182 | } | |
1183 | ||
1184 | if (max_percent < min_pcnt) | |
36532461 ACM |
1185 | return -1; |
1186 | ||
e3087b80 | 1187 | if (max_lines && printed >= max_lines) |
36532461 | 1188 | return 1; |
d040bd36 | 1189 | |
d5e3d747 | 1190 | if (queue != NULL) { |
a17c4ca0 | 1191 | list_for_each_entry_from(queue, ¬es->src->source, al.node) { |
29ed6e76 | 1192 | if (queue == dl) |
d5e3d747 | 1193 | break; |
db8fd07a | 1194 | disasm_line__print(queue, sym, start, evsel, len, |
d5e3d747 ACM |
1195 | 0, 0, 1, NULL); |
1196 | } | |
1197 | } | |
1198 | ||
b1dd4432 | 1199 | color = get_percent_color(max_percent); |
78f7defe ACM |
1200 | |
1201 | /* | |
1202 | * Also color the filename and line if needed, with | |
1203 | * the same color than the percentage. Don't print it | |
1204 | * twice for close colored addr with the same filename:line | |
1205 | */ | |
1206 | if (path) { | |
1207 | if (!prev_line || strcmp(prev_line, path) | |
1208 | || color != prev_color) { | |
1209 | color_fprintf(stdout, color, " %s", path); | |
1210 | prev_line = path; | |
1211 | prev_color = color; | |
1212 | } | |
1213 | } | |
1214 | ||
b1dd4432 NK |
1215 | for (i = 0; i < nr_percent; i++) { |
1216 | percent = ppercents[i]; | |
896bccd3 | 1217 | sample = psamples[i]; |
b1dd4432 | 1218 | color = get_percent_color(percent); |
0c4a5bce ML |
1219 | |
1220 | if (symbol_conf.show_total_period) | |
ce9ee4a2 | 1221 | color_fprintf(stdout, color, " %11" PRIu64, |
585d93c5 | 1222 | sample.period); |
1ac39372 TS |
1223 | else if (symbol_conf.show_nr_samples) |
1224 | color_fprintf(stdout, color, " %7" PRIu64, | |
1225 | sample.nr_samples); | |
0c4a5bce ML |
1226 | else |
1227 | color_fprintf(stdout, color, " %7.2f", percent); | |
b1dd4432 NK |
1228 | } |
1229 | ||
78f7defe | 1230 | printf(" : "); |
70fbe057 PZ |
1231 | |
1232 | br = block_range__find(addr); | |
1233 | color_fprintf(stdout, annotate__address_color(br), " %" PRIx64 ":", addr); | |
d5490b96 | 1234 | color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); |
70fbe057 PZ |
1235 | annotate__branch_printf(br, addr); |
1236 | printf("\n"); | |
b1dd4432 NK |
1237 | |
1238 | if (ppercents != &percent) | |
1239 | free(ppercents); | |
1240 | ||
896bccd3 | 1241 | if (psamples != &sample) |
0c4a5bce ML |
1242 | free(psamples); |
1243 | ||
e3087b80 | 1244 | } else if (max_lines && printed >= max_lines) |
36532461 ACM |
1245 | return 1; |
1246 | else { | |
ce9ee4a2 | 1247 | int width = symbol_conf.show_total_period ? 12 : 8; |
b1dd4432 | 1248 | |
d5e3d747 ACM |
1249 | if (queue) |
1250 | return -1; | |
1251 | ||
759ff497 | 1252 | if (perf_evsel__is_group_event(evsel)) |
b1dd4432 NK |
1253 | width *= evsel->nr_members; |
1254 | ||
d5490b96 | 1255 | if (!*dl->al.line) |
b1dd4432 | 1256 | printf(" %*s:\n", width, " "); |
78f7defe | 1257 | else |
d5490b96 | 1258 | printf(" %*s: %s\n", width, " ", dl->al.line); |
78f7defe | 1259 | } |
36532461 ACM |
1260 | |
1261 | return 0; | |
78f7defe ACM |
1262 | } |
1263 | ||
3aec150a NK |
1264 | /* |
1265 | * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) | |
1266 | * which looks like following | |
1267 | * | |
1268 | * 0000000000415500 <_init>: | |
1269 | * 415500: sub $0x8,%rsp | |
1270 | * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> | |
1271 | * 41550b: test %rax,%rax | |
1272 | * 41550e: je 415515 <_init+0x15> | |
1273 | * 415510: callq 416e70 <__gmon_start__@plt> | |
1274 | * 415515: add $0x8,%rsp | |
1275 | * 415519: retq | |
1276 | * | |
1277 | * it will be parsed and saved into struct disasm_line as | |
1278 | * <offset> <name> <ops.raw> | |
1279 | * | |
1280 | * The offset will be a relative offset from the start of the symbol and -1 | |
1281 | * means that it's not a disassembly line so should be treated differently. | |
1282 | * The ops.raw part will be parsed further according to type of the instruction. | |
1283 | */ | |
1a04db70 | 1284 | static int symbol__parse_objdump_line(struct symbol *sym, FILE *file, |
ea07c5aa | 1285 | struct annotate_args *args, |
e592488c | 1286 | int *line_nr) |
78f7defe | 1287 | { |
1a04db70 | 1288 | struct map *map = args->map; |
ce6f4fab | 1289 | struct annotation *notes = symbol__annotation(sym); |
29ed6e76 | 1290 | struct disasm_line *dl; |
4597cf06 | 1291 | char *line = NULL, *parsed_line, *tmp, *tmp2; |
78f7defe ACM |
1292 | size_t line_len; |
1293 | s64 line_ip, offset = -1; | |
e592488c | 1294 | regmatch_t match[2]; |
78f7defe ACM |
1295 | |
1296 | if (getline(&line, &line_len, file) < 0) | |
1297 | return -1; | |
1298 | ||
1299 | if (!line) | |
1300 | return -1; | |
1301 | ||
78f7defe | 1302 | line_ip = -1; |
4597cf06 | 1303 | parsed_line = rtrim(line); |
78f7defe | 1304 | |
e592488c | 1305 | /* /filename:linenr ? Save line number and ignore. */ |
986a5bc0 TS |
1306 | if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { |
1307 | *line_nr = atoi(parsed_line + match[1].rm_so); | |
e592488c AK |
1308 | return 0; |
1309 | } | |
1310 | ||
4597cf06 | 1311 | tmp = ltrim(parsed_line); |
78f7defe ACM |
1312 | if (*tmp) { |
1313 | /* | |
1314 | * Parse hexa addresses followed by ':' | |
1315 | */ | |
1316 | line_ip = strtoull(tmp, &tmp2, 16); | |
1317 | if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') | |
1318 | line_ip = -1; | |
1319 | } | |
1320 | ||
1321 | if (line_ip != -1) { | |
1322 | u64 start = map__rip_2objdump(map, sym->start), | |
1323 | end = map__rip_2objdump(map, sym->end); | |
1324 | ||
1325 | offset = line_ip - start; | |
2c241bd3 | 1326 | if ((u64)line_ip < start || (u64)line_ip >= end) |
78f7defe | 1327 | offset = -1; |
058b4cc9 ACM |
1328 | else |
1329 | parsed_line = tmp2 + 1; | |
a31b7cc0 | 1330 | } |
78f7defe | 1331 | |
4748834f JO |
1332 | args->offset = offset; |
1333 | args->line = parsed_line; | |
1334 | args->line_nr = *line_nr; | |
1335 | ||
1336 | dl = disasm_line__new(args); | |
058b4cc9 | 1337 | free(line); |
e592488c | 1338 | (*line_nr)++; |
058b4cc9 | 1339 | |
29ed6e76 | 1340 | if (dl == NULL) |
78f7defe | 1341 | return -1; |
058b4cc9 | 1342 | |
e216874c | 1343 | if (!disasm_line__has_offset(dl)) { |
bbb7f846 AH |
1344 | dl->ops.target.offset = dl->ops.target.addr - |
1345 | map__rip_2objdump(map, sym->start); | |
e216874c RB |
1346 | dl->ops.target.offset_avail = true; |
1347 | } | |
bbb7f846 | 1348 | |
6e427ab0 | 1349 | /* kcore has no symbols, so add the call target name */ |
75b49202 | 1350 | if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) { |
6e427ab0 AH |
1351 | struct addr_map_symbol target = { |
1352 | .map = map, | |
1353 | .addr = dl->ops.target.addr, | |
1354 | }; | |
1355 | ||
be39db9f | 1356 | if (!map_groups__find_ams(&target) && |
6e427ab0 AH |
1357 | target.sym->start == target.al_addr) |
1358 | dl->ops.target.name = strdup(target.sym->name); | |
b178170a AH |
1359 | } |
1360 | ||
82b9d7ff | 1361 | annotation_line__add(&dl->al, ¬es->src->source); |
78f7defe ACM |
1362 | |
1363 | return 0; | |
1364 | } | |
1365 | ||
e592488c AK |
1366 | static __attribute__((constructor)) void symbol__init_regexpr(void) |
1367 | { | |
1368 | regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); | |
1369 | } | |
1370 | ||
484a5e74 AH |
1371 | static void delete_last_nop(struct symbol *sym) |
1372 | { | |
1373 | struct annotation *notes = symbol__annotation(sym); | |
1374 | struct list_head *list = ¬es->src->source; | |
1375 | struct disasm_line *dl; | |
1376 | ||
1377 | while (!list_empty(list)) { | |
a17c4ca0 | 1378 | dl = list_entry(list->prev, struct disasm_line, al.node); |
484a5e74 | 1379 | |
75b49202 ACM |
1380 | if (dl->ins.ops) { |
1381 | if (dl->ins.ops != &nop_ops) | |
484a5e74 AH |
1382 | return; |
1383 | } else { | |
d5490b96 JO |
1384 | if (!strstr(dl->al.line, " nop ") && |
1385 | !strstr(dl->al.line, " nopl ") && | |
1386 | !strstr(dl->al.line, " nopw ")) | |
484a5e74 AH |
1387 | return; |
1388 | } | |
1389 | ||
a17c4ca0 | 1390 | list_del(&dl->al.node); |
484a5e74 AH |
1391 | disasm_line__free(dl); |
1392 | } | |
1393 | } | |
1394 | ||
ee51d851 ACM |
1395 | int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map, |
1396 | int errnum, char *buf, size_t buflen) | |
1397 | { | |
1398 | struct dso *dso = map->dso; | |
1399 | ||
1400 | BUG_ON(buflen == 0); | |
1401 | ||
1402 | if (errnum >= 0) { | |
1403 | str_error_r(errnum, buf, buflen); | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | switch (errnum) { | |
1408 | case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { | |
1409 | char bf[SBUILD_ID_SIZE + 15] = " with build id "; | |
1410 | char *build_id_msg = NULL; | |
1411 | ||
1412 | if (dso->has_build_id) { | |
1413 | build_id__sprintf(dso->build_id, | |
1414 | sizeof(dso->build_id), bf + 15); | |
1415 | build_id_msg = bf; | |
1416 | } | |
1417 | scnprintf(buf, buflen, | |
1418 | "No vmlinux file%s\nwas found in the path.\n\n" | |
1419 | "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" | |
1420 | "Please use:\n\n" | |
1421 | " perf buildid-cache -vu vmlinux\n\n" | |
1422 | "or:\n\n" | |
1423 | " --vmlinux vmlinux\n", build_id_msg ?: ""); | |
1424 | } | |
1425 | break; | |
1426 | default: | |
1427 | scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); | |
1428 | break; | |
1429 | } | |
1430 | ||
1431 | return 0; | |
1432 | } | |
1433 | ||
05ed3ac9 | 1434 | static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) |
78f7defe | 1435 | { |
05ed3ac9 ACM |
1436 | char linkname[PATH_MAX]; |
1437 | char *build_id_filename; | |
6ebd2547 | 1438 | char *build_id_path = NULL; |
3619ef76 | 1439 | char *pos; |
78f7defe | 1440 | |
c12944f7 ACM |
1441 | if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && |
1442 | !dso__is_kcore(dso)) | |
05ed3ac9 | 1443 | return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; |
c12944f7 | 1444 | |
d2396999 | 1445 | build_id_filename = dso__build_id_filename(dso, NULL, 0, false); |
05ed3ac9 ACM |
1446 | if (build_id_filename) { |
1447 | __symbol__join_symfs(filename, filename_size, build_id_filename); | |
1448 | free(build_id_filename); | |
3caee094 | 1449 | } else { |
ee51d851 ACM |
1450 | if (dso->has_build_id) |
1451 | return ENOMEM; | |
78f7defe | 1452 | goto fallback; |
3caee094 ACM |
1453 | } |
1454 | ||
6ebd2547 TS |
1455 | build_id_path = strdup(filename); |
1456 | if (!build_id_path) | |
1457 | return -1; | |
1458 | ||
3619ef76 NK |
1459 | /* |
1460 | * old style build-id cache has name of XX/XXXXXXX.. while | |
1461 | * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. | |
1462 | * extract the build-id part of dirname in the new style only. | |
1463 | */ | |
1464 | pos = strrchr(build_id_path, '/'); | |
1465 | if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) | |
1466 | dirname(build_id_path); | |
6ebd2547 | 1467 | |
3caee094 | 1468 | if (dso__is_kcore(dso) || |
6ebd2547 | 1469 | readlink(build_id_path, linkname, sizeof(linkname)) < 0 || |
05ed3ac9 ACM |
1470 | strstr(linkname, DSO__NAME_KALLSYMS) || |
1471 | access(filename, R_OK)) { | |
78f7defe ACM |
1472 | fallback: |
1473 | /* | |
1474 | * If we don't have build-ids or the build-id file isn't in the | |
1475 | * cache, or is just a kallsyms file, well, lets hope that this | |
1476 | * DSO is the same as when 'perf record' ran. | |
1477 | */ | |
05ed3ac9 | 1478 | __symbol__join_symfs(filename, filename_size, dso->long_name); |
78f7defe ACM |
1479 | } |
1480 | ||
6ebd2547 | 1481 | free(build_id_path); |
05ed3ac9 ACM |
1482 | return 0; |
1483 | } | |
1484 | ||
786c1b51 ACM |
1485 | static const char *annotate__norm_arch(const char *arch_name) |
1486 | { | |
1487 | struct utsname uts; | |
1488 | ||
1489 | if (!arch_name) { /* Assume we are annotating locally. */ | |
1490 | if (uname(&uts) < 0) | |
1491 | return NULL; | |
1492 | arch_name = uts.machine; | |
1493 | } | |
1494 | return normalize_arch((char *)arch_name); | |
1495 | } | |
1496 | ||
1a04db70 | 1497 | static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) |
05ed3ac9 | 1498 | { |
1a04db70 | 1499 | struct map *map = args->map; |
05ed3ac9 ACM |
1500 | struct dso *dso = map->dso; |
1501 | char command[PATH_MAX * 2]; | |
1502 | FILE *file; | |
1503 | char symfs_filename[PATH_MAX]; | |
1504 | struct kcore_extract kce; | |
1505 | bool delete_extract = false; | |
1506 | int stdout_fd[2]; | |
1507 | int lineno = 0; | |
1508 | int nline; | |
1509 | pid_t pid; | |
1510 | int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); | |
1511 | ||
1512 | if (err) | |
1513 | return err; | |
1514 | ||
78f7defe | 1515 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, |
3caee094 | 1516 | symfs_filename, sym->name, map->unmap_ip(map, sym->start), |
78f7defe ACM |
1517 | map->unmap_ip(map, sym->end)); |
1518 | ||
78f7defe ACM |
1519 | pr_debug("annotating [%p] %30s : [%p] %30s\n", |
1520 | dso, dso->long_name, sym, sym->name); | |
1521 | ||
afba19d9 AH |
1522 | if (dso__is_kcore(dso)) { |
1523 | kce.kcore_filename = symfs_filename; | |
1524 | kce.addr = map__rip_2objdump(map, sym->start); | |
1525 | kce.offs = sym->start; | |
2c241bd3 | 1526 | kce.len = sym->end - sym->start; |
afba19d9 AH |
1527 | if (!kcore_extract__create(&kce)) { |
1528 | delete_extract = true; | |
1529 | strlcpy(symfs_filename, kce.extract_filename, | |
1530 | sizeof(symfs_filename)); | |
afba19d9 | 1531 | } |
2c7da8c5 | 1532 | } else if (dso__needs_decompress(dso)) { |
3c84fd53 | 1533 | char tmp[KMOD_DECOMP_LEN]; |
2c7da8c5 | 1534 | |
3c84fd53 NK |
1535 | if (dso__decompress_kmodule_path(dso, symfs_filename, |
1536 | tmp, sizeof(tmp)) < 0) | |
3caee094 | 1537 | goto out; |
2c7da8c5 JO |
1538 | |
1539 | strcpy(symfs_filename, tmp); | |
afba19d9 AH |
1540 | } |
1541 | ||
78f7defe | 1542 | snprintf(command, sizeof(command), |
7a4ec938 | 1543 | "%s %s%s --start-address=0x%016" PRIx64 |
3e6a2a7f | 1544 | " --stop-address=0x%016" PRIx64 |
7b4500bc | 1545 | " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand", |
7a4ec938 | 1546 | objdump_path ? objdump_path : "objdump", |
f69b64f7 AK |
1547 | disassembler_style ? "-M " : "", |
1548 | disassembler_style ? disassembler_style : "", | |
78f7defe | 1549 | map__rip_2objdump(map, sym->start), |
2c241bd3 | 1550 | map__rip_2objdump(map, sym->end), |
3e6a2a7f SE |
1551 | symbol_conf.annotate_asm_raw ? "" : "--no-show-raw", |
1552 | symbol_conf.annotate_src ? "-S" : "", | |
3caee094 | 1553 | symfs_filename, symfs_filename); |
78f7defe ACM |
1554 | |
1555 | pr_debug("Executing: %s\n", command); | |
1556 | ||
9955d0be ACM |
1557 | err = -1; |
1558 | if (pipe(stdout_fd) < 0) { | |
1559 | pr_err("Failure creating the pipe to run %s\n", command); | |
1560 | goto out_remove_tmp; | |
1561 | } | |
1562 | ||
1563 | pid = fork(); | |
1564 | if (pid < 0) { | |
1565 | pr_err("Failure forking to run %s\n", command); | |
1566 | goto out_close_stdout; | |
1567 | } | |
1568 | ||
1569 | if (pid == 0) { | |
1570 | close(stdout_fd[0]); | |
1571 | dup2(stdout_fd[1], 1); | |
1572 | close(stdout_fd[1]); | |
1573 | execl("/bin/sh", "sh", "-c", command, NULL); | |
1574 | perror(command); | |
1575 | exit(-1); | |
1576 | } | |
1577 | ||
1578 | close(stdout_fd[1]); | |
1579 | ||
1580 | file = fdopen(stdout_fd[0], "r"); | |
62ec9b3f | 1581 | if (!file) { |
9955d0be | 1582 | pr_err("Failure creating FILE stream for %s\n", command); |
62ec9b3f AK |
1583 | /* |
1584 | * If we were using debug info should retry with | |
1585 | * original binary. | |
1586 | */ | |
2c7da8c5 | 1587 | goto out_remove_tmp; |
62ec9b3f | 1588 | } |
78f7defe | 1589 | |
62ec9b3f AK |
1590 | nline = 0; |
1591 | while (!feof(file)) { | |
ed7b339f ACM |
1592 | /* |
1593 | * The source code line number (lineno) needs to be kept in | |
1594 | * accross calls to symbol__parse_objdump_line(), so that it | |
1595 | * can associate it with the instructions till the next one. | |
1596 | * See disasm_line__new() and struct disasm_line::line_nr. | |
1597 | */ | |
1a04db70 | 1598 | if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0) |
78f7defe | 1599 | break; |
62ec9b3f AK |
1600 | nline++; |
1601 | } | |
1602 | ||
1603 | if (nline == 0) | |
1604 | pr_err("No output from %s\n", command); | |
78f7defe | 1605 | |
484a5e74 AH |
1606 | /* |
1607 | * kallsyms does not have symbol sizes so there may a nop at the end. | |
1608 | * Remove it. | |
1609 | */ | |
1610 | if (dso__is_kcore(dso)) | |
1611 | delete_last_nop(sym); | |
1612 | ||
9955d0be ACM |
1613 | fclose(file); |
1614 | err = 0; | |
2c7da8c5 | 1615 | out_remove_tmp: |
9955d0be ACM |
1616 | close(stdout_fd[0]); |
1617 | ||
2c7da8c5 JO |
1618 | if (dso__needs_decompress(dso)) |
1619 | unlink(symfs_filename); | |
3caee094 | 1620 | |
afba19d9 AH |
1621 | if (delete_extract) |
1622 | kcore_extract__delete(&kce); | |
c12944f7 | 1623 | out: |
78f7defe | 1624 | return err; |
9955d0be ACM |
1625 | |
1626 | out_close_stdout: | |
1627 | close(stdout_fd[1]); | |
1628 | goto out_remove_tmp; | |
78f7defe ACM |
1629 | } |
1630 | ||
c34df25b | 1631 | int symbol__annotate(struct symbol *sym, struct map *map, |
d03a686e | 1632 | struct perf_evsel *evsel, size_t privsize, |
c34df25b JO |
1633 | struct arch **parch, char *cpuid) |
1634 | { | |
ea07c5aa JO |
1635 | struct annotate_args args = { |
1636 | .privsize = privsize, | |
1a04db70 | 1637 | .map = map, |
d03a686e | 1638 | .evsel = evsel, |
ea07c5aa | 1639 | }; |
d03a686e | 1640 | const char *arch_name = NULL; |
c34df25b JO |
1641 | struct arch *arch; |
1642 | int err; | |
1643 | ||
d03a686e JO |
1644 | if (evsel) |
1645 | arch_name = perf_evsel__env_arch(evsel); | |
1646 | ||
c34df25b JO |
1647 | arch_name = annotate__norm_arch(arch_name); |
1648 | if (!arch_name) | |
1649 | return -1; | |
1650 | ||
24fe7b88 | 1651 | args.arch = arch = arch__find(arch_name); |
c34df25b JO |
1652 | if (arch == NULL) |
1653 | return -ENOTSUP; | |
1654 | ||
1655 | if (parch) | |
1656 | *parch = arch; | |
1657 | ||
1658 | if (arch->init) { | |
1659 | err = arch->init(arch, cpuid); | |
1660 | if (err) { | |
1661 | pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name); | |
1662 | return err; | |
1663 | } | |
1664 | } | |
1665 | ||
1a04db70 | 1666 | return symbol__disassemble(sym, &args); |
c34df25b JO |
1667 | } |
1668 | ||
78f7defe ACM |
1669 | static void insert_source_line(struct rb_root *root, struct source_line *src_line) |
1670 | { | |
1671 | struct source_line *iter; | |
1672 | struct rb_node **p = &root->rb_node; | |
1673 | struct rb_node *parent = NULL; | |
1491c22a | 1674 | int i, ret; |
78f7defe ACM |
1675 | |
1676 | while (*p != NULL) { | |
1677 | parent = *p; | |
1678 | iter = rb_entry(parent, struct source_line, node); | |
1679 | ||
41127965 NK |
1680 | ret = strcmp(iter->path, src_line->path); |
1681 | if (ret == 0) { | |
1491c22a | 1682 | for (i = 0; i < src_line->nr_pcnt; i++) |
276af92f | 1683 | iter->samples[i].percent_sum += src_line->samples[i].percent; |
41127965 NK |
1684 | return; |
1685 | } | |
1686 | ||
1687 | if (ret < 0) | |
1688 | p = &(*p)->rb_left; | |
1689 | else | |
1690 | p = &(*p)->rb_right; | |
1691 | } | |
1692 | ||
1491c22a | 1693 | for (i = 0; i < src_line->nr_pcnt; i++) |
276af92f | 1694 | src_line->samples[i].percent_sum = src_line->samples[i].percent; |
41127965 NK |
1695 | |
1696 | rb_link_node(&src_line->node, parent, p); | |
1697 | rb_insert_color(&src_line->node, root); | |
1698 | } | |
1699 | ||
1491c22a NK |
1700 | static int cmp_source_line(struct source_line *a, struct source_line *b) |
1701 | { | |
1702 | int i; | |
1703 | ||
1704 | for (i = 0; i < a->nr_pcnt; i++) { | |
276af92f | 1705 | if (a->samples[i].percent_sum == b->samples[i].percent_sum) |
1491c22a | 1706 | continue; |
276af92f | 1707 | return a->samples[i].percent_sum > b->samples[i].percent_sum; |
1491c22a NK |
1708 | } |
1709 | ||
1710 | return 0; | |
1711 | } | |
1712 | ||
41127965 NK |
1713 | static void __resort_source_line(struct rb_root *root, struct source_line *src_line) |
1714 | { | |
1715 | struct source_line *iter; | |
1716 | struct rb_node **p = &root->rb_node; | |
1717 | struct rb_node *parent = NULL; | |
1718 | ||
1719 | while (*p != NULL) { | |
1720 | parent = *p; | |
1721 | iter = rb_entry(parent, struct source_line, node); | |
1722 | ||
1491c22a | 1723 | if (cmp_source_line(src_line, iter)) |
78f7defe ACM |
1724 | p = &(*p)->rb_left; |
1725 | else | |
1726 | p = &(*p)->rb_right; | |
1727 | } | |
1728 | ||
1729 | rb_link_node(&src_line->node, parent, p); | |
1730 | rb_insert_color(&src_line->node, root); | |
1731 | } | |
1732 | ||
41127965 NK |
1733 | static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) |
1734 | { | |
1735 | struct source_line *src_line; | |
1736 | struct rb_node *node; | |
1737 | ||
1738 | node = rb_first(src_root); | |
1739 | while (node) { | |
1740 | struct rb_node *next; | |
1741 | ||
1742 | src_line = rb_entry(node, struct source_line, node); | |
1743 | next = rb_next(node); | |
1744 | rb_erase(node, src_root); | |
1745 | ||
1746 | __resort_source_line(dest_root, src_line); | |
1747 | node = next; | |
1748 | } | |
1749 | } | |
1750 | ||
78f7defe ACM |
1751 | static void symbol__free_source_line(struct symbol *sym, int len) |
1752 | { | |
1753 | struct annotation *notes = symbol__annotation(sym); | |
ce6f4fab | 1754 | struct source_line *src_line = notes->src->lines; |
1491c22a | 1755 | size_t sizeof_src_line; |
78f7defe ACM |
1756 | int i; |
1757 | ||
1491c22a | 1758 | sizeof_src_line = sizeof(*src_line) + |
276af92f | 1759 | (sizeof(src_line->samples) * (src_line->nr_pcnt - 1)); |
78f7defe | 1760 | |
1491c22a | 1761 | for (i = 0; i < len; i++) { |
f048d548 | 1762 | free_srcline(src_line->path); |
1491c22a NK |
1763 | src_line = (void *)src_line + sizeof_src_line; |
1764 | } | |
1765 | ||
04662523 | 1766 | zfree(¬es->src->lines); |
78f7defe ACM |
1767 | } |
1768 | ||
1769 | /* Get the filename:line for the colored entries */ | |
1770 | static int symbol__get_source_line(struct symbol *sym, struct map *map, | |
db8fd07a | 1771 | struct perf_evsel *evsel, |
86c98cab | 1772 | struct rb_root *root, int len) |
78f7defe ACM |
1773 | { |
1774 | u64 start; | |
1491c22a NK |
1775 | int i, k; |
1776 | int evidx = evsel->idx; | |
78f7defe ACM |
1777 | struct source_line *src_line; |
1778 | struct annotation *notes = symbol__annotation(sym); | |
1491c22a | 1779 | struct sym_hist *h = annotation__histogram(notes, evidx); |
41127965 | 1780 | struct rb_root tmp_root = RB_ROOT; |
1491c22a | 1781 | int nr_pcnt = 1; |
8158683d | 1782 | u64 nr_samples = h->nr_samples; |
1491c22a NK |
1783 | size_t sizeof_src_line = sizeof(struct source_line); |
1784 | ||
1785 | if (perf_evsel__is_group_event(evsel)) { | |
1786 | for (i = 1; i < evsel->nr_members; i++) { | |
1787 | h = annotation__histogram(notes, evidx + i); | |
8158683d | 1788 | nr_samples += h->nr_samples; |
1491c22a NK |
1789 | } |
1790 | nr_pcnt = evsel->nr_members; | |
276af92f | 1791 | sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples); |
1491c22a | 1792 | } |
78f7defe | 1793 | |
8158683d | 1794 | if (!nr_samples) |
78f7defe ACM |
1795 | return 0; |
1796 | ||
1491c22a | 1797 | src_line = notes->src->lines = calloc(len, sizeof_src_line); |
ce6f4fab | 1798 | if (!notes->src->lines) |
78f7defe ACM |
1799 | return -1; |
1800 | ||
f40a0633 | 1801 | start = map__rip_2objdump(map, sym->start); |
78f7defe ACM |
1802 | |
1803 | for (i = 0; i < len; i++) { | |
8158683d | 1804 | u64 offset; |
1491c22a | 1805 | double percent_max = 0.0; |
78f7defe | 1806 | |
1491c22a NK |
1807 | src_line->nr_pcnt = nr_pcnt; |
1808 | ||
1809 | for (k = 0; k < nr_pcnt; k++) { | |
2e933b12 TS |
1810 | double percent = 0.0; |
1811 | ||
1491c22a | 1812 | h = annotation__histogram(notes, evidx + k); |
896bccd3 | 1813 | nr_samples = h->addr[i].nr_samples; |
8158683d TS |
1814 | if (h->nr_samples) |
1815 | percent = 100.0 * nr_samples / h->nr_samples; | |
1491c22a | 1816 | |
2e933b12 TS |
1817 | if (percent > percent_max) |
1818 | percent_max = percent; | |
1819 | src_line->samples[k].percent = percent; | |
99094a5e | 1820 | src_line->samples[k].nr = nr_samples; |
1491c22a NK |
1821 | } |
1822 | ||
1823 | if (percent_max <= 0.5) | |
1824 | goto next; | |
78f7defe ACM |
1825 | |
1826 | offset = start + i; | |
5dfa210e MW |
1827 | src_line->path = get_srcline(map->dso, offset, NULL, |
1828 | false, true); | |
1491c22a | 1829 | insert_source_line(&tmp_root, src_line); |
78f7defe | 1830 | |
1491c22a NK |
1831 | next: |
1832 | src_line = (void *)src_line + sizeof_src_line; | |
78f7defe ACM |
1833 | } |
1834 | ||
41127965 | 1835 | resort_source_line(root, &tmp_root); |
78f7defe ACM |
1836 | return 0; |
1837 | } | |
1838 | ||
1839 | static void print_summary(struct rb_root *root, const char *filename) | |
1840 | { | |
1841 | struct source_line *src_line; | |
1842 | struct rb_node *node; | |
1843 | ||
1844 | printf("\nSorted summary for file %s\n", filename); | |
1845 | printf("----------------------------------------------\n\n"); | |
1846 | ||
1847 | if (RB_EMPTY_ROOT(root)) { | |
1848 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); | |
1849 | return; | |
1850 | } | |
1851 | ||
1852 | node = rb_first(root); | |
1853 | while (node) { | |
1491c22a | 1854 | double percent, percent_max = 0.0; |
78f7defe ACM |
1855 | const char *color; |
1856 | char *path; | |
1491c22a | 1857 | int i; |
78f7defe ACM |
1858 | |
1859 | src_line = rb_entry(node, struct source_line, node); | |
1491c22a | 1860 | for (i = 0; i < src_line->nr_pcnt; i++) { |
276af92f | 1861 | percent = src_line->samples[i].percent_sum; |
1491c22a NK |
1862 | color = get_percent_color(percent); |
1863 | color_fprintf(stdout, color, " %7.2f", percent); | |
1864 | ||
1865 | if (percent > percent_max) | |
1866 | percent_max = percent; | |
1867 | } | |
1868 | ||
78f7defe | 1869 | path = src_line->path; |
1491c22a | 1870 | color = get_percent_color(percent_max); |
f048d548 | 1871 | color_fprintf(stdout, color, " %s\n", path); |
78f7defe | 1872 | |
78f7defe ACM |
1873 | node = rb_next(node); |
1874 | } | |
1875 | } | |
1876 | ||
db8fd07a | 1877 | static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel) |
78f7defe ACM |
1878 | { |
1879 | struct annotation *notes = symbol__annotation(sym); | |
db8fd07a | 1880 | struct sym_hist *h = annotation__histogram(notes, evsel->idx); |
1b2e2df4 | 1881 | u64 len = symbol__size(sym), offset; |
78f7defe ACM |
1882 | |
1883 | for (offset = 0; offset < len; ++offset) | |
896bccd3 | 1884 | if (h->addr[offset].nr_samples != 0) |
78f7defe | 1885 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
896bccd3 | 1886 | sym->start + offset, h->addr[offset].nr_samples); |
8158683d | 1887 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); |
78f7defe ACM |
1888 | } |
1889 | ||
db8fd07a NK |
1890 | int symbol__annotate_printf(struct symbol *sym, struct map *map, |
1891 | struct perf_evsel *evsel, bool full_paths, | |
1892 | int min_pcnt, int max_lines, int context) | |
78f7defe ACM |
1893 | { |
1894 | struct dso *dso = map->dso; | |
bfd14b9a DA |
1895 | char *filename; |
1896 | const char *d_filename; | |
9cdbadce | 1897 | const char *evsel_name = perf_evsel__name(evsel); |
ce6f4fab | 1898 | struct annotation *notes = symbol__annotation(sym); |
135cce1b | 1899 | struct sym_hist *h = annotation__histogram(notes, evsel->idx); |
29ed6e76 | 1900 | struct disasm_line *pos, *queue = NULL; |
058b4cc9 | 1901 | u64 start = map__rip_2objdump(map, sym->start); |
d5e3d747 | 1902 | int printed = 2, queue_len = 0; |
36532461 | 1903 | int more = 0; |
78f7defe | 1904 | u64 len; |
ce9ee4a2 | 1905 | int width = symbol_conf.show_total_period ? 12 : 8; |
53dd9b5f | 1906 | int graph_dotted_len; |
78f7defe | 1907 | |
bfd14b9a DA |
1908 | filename = strdup(dso->long_name); |
1909 | if (!filename) | |
1910 | return -ENOMEM; | |
1911 | ||
78f7defe ACM |
1912 | if (full_paths) |
1913 | d_filename = filename; | |
1914 | else | |
1915 | d_filename = basename(filename); | |
1916 | ||
1b2e2df4 | 1917 | len = symbol__size(sym); |
b1dd4432 | 1918 | |
759ff497 | 1919 | if (perf_evsel__is_group_event(evsel)) |
b1dd4432 | 1920 | width *= evsel->nr_members; |
78f7defe | 1921 | |
135cce1b | 1922 | graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n", |
1ac39372 TS |
1923 | width, width, symbol_conf.show_total_period ? "Period" : |
1924 | symbol_conf.show_nr_samples ? "Samples" : "Percent", | |
38d2dcd0 | 1925 | d_filename, evsel_name, h->nr_samples); |
9cdbadce | 1926 | |
53dd9b5f | 1927 | printf("%-*.*s----\n", |
9cdbadce | 1928 | graph_dotted_len, graph_dotted_len, graph_dotted_line); |
78f7defe | 1929 | |
bb963e16 | 1930 | if (verbose > 0) |
db8fd07a | 1931 | symbol__annotate_hits(sym, evsel); |
78f7defe | 1932 | |
a17c4ca0 | 1933 | list_for_each_entry(pos, ¬es->src->source, al.node) { |
d5e3d747 ACM |
1934 | if (context && queue == NULL) { |
1935 | queue = pos; | |
1936 | queue_len = 0; | |
1937 | } | |
1938 | ||
db8fd07a | 1939 | switch (disasm_line__print(pos, sym, start, evsel, len, |
058b4cc9 ACM |
1940 | min_pcnt, printed, max_lines, |
1941 | queue)) { | |
36532461 ACM |
1942 | case 0: |
1943 | ++printed; | |
d5e3d747 ACM |
1944 | if (context) { |
1945 | printed += queue_len; | |
1946 | queue = NULL; | |
1947 | queue_len = 0; | |
1948 | } | |
36532461 ACM |
1949 | break; |
1950 | case 1: | |
1951 | /* filtered by max_lines */ | |
1952 | ++more; | |
d040bd36 | 1953 | break; |
36532461 ACM |
1954 | case -1: |
1955 | default: | |
d5e3d747 ACM |
1956 | /* |
1957 | * Filtered by min_pcnt or non IP lines when | |
1958 | * context != 0 | |
1959 | */ | |
1960 | if (!context) | |
1961 | break; | |
1962 | if (queue_len == context) | |
a17c4ca0 | 1963 | queue = list_entry(queue->al.node.next, typeof(*queue), al.node); |
d5e3d747 ACM |
1964 | else |
1965 | ++queue_len; | |
36532461 ACM |
1966 | break; |
1967 | } | |
1968 | } | |
1969 | ||
bfd14b9a DA |
1970 | free(filename); |
1971 | ||
36532461 ACM |
1972 | return more; |
1973 | } | |
f1e2701d | 1974 | |
36532461 ACM |
1975 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
1976 | { | |
1977 | struct annotation *notes = symbol__annotation(sym); | |
1978 | struct sym_hist *h = annotation__histogram(notes, evidx); | |
1979 | ||
ce6f4fab | 1980 | memset(h, 0, notes->src->sizeof_sym_hist); |
36532461 ACM |
1981 | } |
1982 | ||
ce6f4fab | 1983 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
36532461 ACM |
1984 | { |
1985 | struct annotation *notes = symbol__annotation(sym); | |
1986 | struct sym_hist *h = annotation__histogram(notes, evidx); | |
1b2e2df4 | 1987 | int len = symbol__size(sym), offset; |
36532461 | 1988 | |
8158683d | 1989 | h->nr_samples = 0; |
8b84a568 | 1990 | for (offset = 0; offset < len; ++offset) { |
896bccd3 | 1991 | h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; |
8158683d | 1992 | h->nr_samples += h->addr[offset].nr_samples; |
f1e2701d ACM |
1993 | } |
1994 | } | |
1995 | ||
f8eb37bd | 1996 | void annotated_source__purge(struct annotated_source *as) |
f1e2701d | 1997 | { |
f8eb37bd | 1998 | struct annotation_line *al, *n; |
f1e2701d | 1999 | |
f8eb37bd JO |
2000 | list_for_each_entry_safe(al, n, &as->source, node) { |
2001 | list_del(&al->node); | |
2002 | disasm_line__free(disasm_line(al)); | |
f1e2701d ACM |
2003 | } |
2004 | } | |
2005 | ||
5145418b ACM |
2006 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
2007 | { | |
2008 | size_t printed; | |
2009 | ||
d5490b96 JO |
2010 | if (dl->al.offset == -1) |
2011 | return fprintf(fp, "%s\n", dl->al.line); | |
5145418b | 2012 | |
d5490b96 | 2013 | printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name); |
5145418b | 2014 | |
c7e6ead7 | 2015 | if (dl->ops.raw[0] != '\0') { |
5145418b | 2016 | printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", |
c7e6ead7 | 2017 | dl->ops.raw); |
5145418b ACM |
2018 | } |
2019 | ||
2020 | return printed + fprintf(fp, "\n"); | |
2021 | } | |
2022 | ||
2023 | size_t disasm__fprintf(struct list_head *head, FILE *fp) | |
2024 | { | |
2025 | struct disasm_line *pos; | |
2026 | size_t printed = 0; | |
2027 | ||
a17c4ca0 | 2028 | list_for_each_entry(pos, head, al.node) |
5145418b ACM |
2029 | printed += disasm_line__fprintf(pos, fp); |
2030 | ||
2031 | return printed; | |
2032 | } | |
2033 | ||
db8fd07a NK |
2034 | int symbol__tty_annotate(struct symbol *sym, struct map *map, |
2035 | struct perf_evsel *evsel, bool print_lines, | |
2036 | bool full_paths, int min_pcnt, int max_lines) | |
f1e2701d ACM |
2037 | { |
2038 | struct dso *dso = map->dso; | |
f1e2701d | 2039 | struct rb_root source_line = RB_ROOT; |
f1e2701d ACM |
2040 | u64 len; |
2041 | ||
d03a686e | 2042 | if (symbol__annotate(sym, map, evsel, 0, NULL, NULL) < 0) |
f1e2701d ACM |
2043 | return -1; |
2044 | ||
1b2e2df4 | 2045 | len = symbol__size(sym); |
f1e2701d ACM |
2046 | |
2047 | if (print_lines) { | |
4a4c03c1 | 2048 | srcline_full_filename = full_paths; |
86c98cab NK |
2049 | symbol__get_source_line(sym, map, evsel, &source_line, len); |
2050 | print_summary(&source_line, dso->long_name); | |
78f7defe ACM |
2051 | } |
2052 | ||
db8fd07a | 2053 | symbol__annotate_printf(sym, map, evsel, full_paths, |
d5e3d747 | 2054 | min_pcnt, max_lines, 0); |
78f7defe ACM |
2055 | if (print_lines) |
2056 | symbol__free_source_line(sym, len); | |
2057 | ||
f8eb37bd | 2058 | annotated_source__purge(symbol__annotation(sym)->src); |
f1e2701d | 2059 | |
78f7defe ACM |
2060 | return 0; |
2061 | } | |
f626adff | 2062 | |
48c65bda NK |
2063 | bool ui__has_annotation(void) |
2064 | { | |
2e0453af | 2065 | return use_browser == 1 && perf_hpp_list.sym; |
48c65bda | 2066 | } |