1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
4 | * |
5 | * Parts came from builtin-annotate.c, see those files for further |
6 | * copyright notes. |
7 | */ |
8 | |
9 | #include <errno.h> |
10 | #include <inttypes.h> |
11 | #include <libgen.h> |
12 | #include <stdlib.h> |
13 | #include "util.h" // hex_width() |
14 | #include "ui/ui.h" |
15 | #include "sort.h" |
16 | #include "build-id.h" |
17 | #include "color.h" |
18 | #include "config.h" |
19 | #include "dso.h" |
20 | #include "env.h" |
21 | #include "map.h" |
22 | #include "maps.h" |
23 | #include "symbol.h" |
24 | #include "srcline.h" |
25 | #include "units.h" |
26 | #include "debug.h" |
27 | #include "annotate.h" |
28 | #include "annotate-data.h" |
29 | #include "evsel.h" |
30 | #include "evlist.h" |
31 | #include "bpf-event.h" |
32 | #include "bpf-utils.h" |
33 | #include "block-range.h" |
34 | #include "string2.h" |
35 | #include "dwarf-regs.h" |
36 | #include "util/event.h" |
37 | #include "util/sharded_mutex.h" |
38 | #include "arch/common.h" |
39 | #include "namespaces.h" |
40 | #include "thread.h" |
41 | #include "hashmap.h" |
42 | #include <regex.h> |
43 | #include <linux/bitops.h> |
44 | #include <linux/kernel.h> |
45 | #include <linux/string.h> |
46 | #include <linux/zalloc.h> |
47 | #include <subcmd/parse-options.h> |
48 | #include <subcmd/run-command.h> |
49 | |
50 | /* FIXME: For the HE_COLORSET */ |
51 | #include "ui/browser.h" |
52 | |
53 | /* |
54 | * FIXME: Using the same values as slang.h, |
55 | * but that header may not be available everywhere |
56 | */ |
57 | #define LARROW_CHAR ((unsigned char)',') |
58 | #define RARROW_CHAR ((unsigned char)'+') |
59 | #define DARROW_CHAR ((unsigned char)'.') |
60 | #define UARROW_CHAR ((unsigned char)'-') |
61 | |
62 | #include <linux/ctype.h> |
63 | |
64 | /* global annotation options */ |
65 | struct annotation_options annotate_opts; |
66 | |
67 | static regex_t file_lineno; |
68 | |
69 | static struct ins_ops *ins__find(struct arch *arch, const char *name); |
70 | static void ins__sort(struct arch *arch); |
71 | static int disasm_line__parse(char *line, const char **namep, char **rawp); |
72 | static int call__scnprintf(struct ins *ins, char *bf, size_t size, |
73 | struct ins_operands *ops, int max_ins_name); |
74 | static int jump__scnprintf(struct ins *ins, char *bf, size_t size, |
75 | struct ins_operands *ops, int max_ins_name); |
76 | |
77 | struct arch { |
78 | const char *name; |
79 | struct ins *instructions; |
80 | size_t nr_instructions; |
81 | size_t nr_instructions_allocated; |
82 | struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); |
83 | bool sorted_instructions; |
84 | bool initialized; |
85 | const char *insn_suffix; |
86 | void *priv; |
87 | unsigned int model; |
88 | unsigned int family; |
89 | int (*init)(struct arch *arch, char *cpuid); |
90 | bool (*ins_is_fused)(struct arch *arch, const char *ins1, |
91 | const char *ins2); |
92 | struct { |
93 | char ; |
94 | char skip_functions_char; |
95 | char register_char; |
96 | char memory_ref_char; |
97 | } objdump; |
98 | }; |
99 | |
100 | static struct ins_ops call_ops; |
101 | static struct ins_ops dec_ops; |
102 | static struct ins_ops jump_ops; |
103 | static struct ins_ops mov_ops; |
104 | static struct ins_ops nop_ops; |
105 | static struct ins_ops lock_ops; |
106 | static struct ins_ops ret_ops; |
107 | |
108 | /* Data type collection debug statistics */ |
109 | struct annotated_data_stat ann_data_stat; |
110 | LIST_HEAD(ann_insn_stat); |
111 | |
112 | /* Pseudo data types */ |
113 | struct annotated_data_type stackop_type = { |
114 | .self = { |
115 | .type_name = (char *)"(stack operation)" , |
116 | .children = LIST_HEAD_INIT(stackop_type.self.children), |
117 | }, |
118 | }; |
119 | |
120 | static int arch__grow_instructions(struct arch *arch) |
121 | { |
122 | struct ins *new_instructions; |
123 | size_t new_nr_allocated; |
124 | |
125 | if (arch->nr_instructions_allocated == 0 && arch->instructions) |
126 | goto grow_from_non_allocated_table; |
127 | |
128 | new_nr_allocated = arch->nr_instructions_allocated + 128; |
129 | new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); |
130 | if (new_instructions == NULL) |
131 | return -1; |
132 | |
133 | out_update_instructions: |
134 | arch->instructions = new_instructions; |
135 | arch->nr_instructions_allocated = new_nr_allocated; |
136 | return 0; |
137 | |
138 | grow_from_non_allocated_table: |
139 | new_nr_allocated = arch->nr_instructions + 128; |
140 | new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); |
141 | if (new_instructions == NULL) |
142 | return -1; |
143 | |
144 | memcpy(new_instructions, arch->instructions, arch->nr_instructions); |
145 | goto out_update_instructions; |
146 | } |
147 | |
148 | static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) |
149 | { |
150 | struct ins *ins; |
151 | |
152 | if (arch->nr_instructions == arch->nr_instructions_allocated && |
153 | arch__grow_instructions(arch)) |
154 | return -1; |
155 | |
156 | ins = &arch->instructions[arch->nr_instructions]; |
157 | ins->name = strdup(name); |
158 | if (!ins->name) |
159 | return -1; |
160 | |
161 | ins->ops = ops; |
162 | arch->nr_instructions++; |
163 | |
164 | ins__sort(arch); |
165 | return 0; |
166 | } |
167 | |
168 | #include "arch/arc/annotate/instructions.c" |
169 | #include "arch/arm/annotate/instructions.c" |
170 | #include "arch/arm64/annotate/instructions.c" |
171 | #include "arch/csky/annotate/instructions.c" |
172 | #include "arch/loongarch/annotate/instructions.c" |
173 | #include "arch/mips/annotate/instructions.c" |
174 | #include "arch/x86/annotate/instructions.c" |
175 | #include "arch/powerpc/annotate/instructions.c" |
176 | #include "arch/riscv64/annotate/instructions.c" |
177 | #include "arch/s390/annotate/instructions.c" |
178 | #include "arch/sparc/annotate/instructions.c" |
179 | |
180 | static struct arch architectures[] = { |
181 | { |
182 | .name = "arc" , |
183 | .init = arc__annotate_init, |
184 | }, |
185 | { |
186 | .name = "arm" , |
187 | .init = arm__annotate_init, |
188 | }, |
189 | { |
190 | .name = "arm64" , |
191 | .init = arm64__annotate_init, |
192 | }, |
193 | { |
194 | .name = "csky" , |
195 | .init = csky__annotate_init, |
196 | }, |
197 | { |
198 | .name = "mips" , |
199 | .init = mips__annotate_init, |
200 | .objdump = { |
201 | .comment_char = '#', |
202 | }, |
203 | }, |
204 | { |
205 | .name = "x86" , |
206 | .init = x86__annotate_init, |
207 | .instructions = x86__instructions, |
208 | .nr_instructions = ARRAY_SIZE(x86__instructions), |
209 | .insn_suffix = "bwlq" , |
210 | .objdump = { |
211 | .comment_char = '#', |
212 | .register_char = '%', |
213 | .memory_ref_char = '(', |
214 | }, |
215 | }, |
216 | { |
217 | .name = "powerpc" , |
218 | .init = powerpc__annotate_init, |
219 | }, |
220 | { |
221 | .name = "riscv64" , |
222 | .init = riscv64__annotate_init, |
223 | }, |
224 | { |
225 | .name = "s390" , |
226 | .init = s390__annotate_init, |
227 | .objdump = { |
228 | .comment_char = '#', |
229 | }, |
230 | }, |
231 | { |
232 | .name = "sparc" , |
233 | .init = sparc__annotate_init, |
234 | .objdump = { |
235 | .comment_char = '#', |
236 | }, |
237 | }, |
238 | { |
239 | .name = "loongarch" , |
240 | .init = loongarch__annotate_init, |
241 | .objdump = { |
242 | .comment_char = '#', |
243 | }, |
244 | }, |
245 | }; |
246 | |
247 | static void ins__delete(struct ins_operands *ops) |
248 | { |
249 | if (ops == NULL) |
250 | return; |
251 | zfree(&ops->source.raw); |
252 | zfree(&ops->source.name); |
253 | zfree(&ops->target.raw); |
254 | zfree(&ops->target.name); |
255 | } |
256 | |
257 | static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, |
258 | struct ins_operands *ops, int max_ins_name) |
259 | { |
260 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, ins->name, ops->raw); |
261 | } |
262 | |
263 | int ins__scnprintf(struct ins *ins, char *bf, size_t size, |
264 | struct ins_operands *ops, int max_ins_name) |
265 | { |
266 | if (ins->ops->scnprintf) |
267 | return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name); |
268 | |
269 | return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); |
270 | } |
271 | |
272 | bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) |
273 | { |
274 | if (!arch || !arch->ins_is_fused) |
275 | return false; |
276 | |
277 | return arch->ins_is_fused(arch, ins1, ins2); |
278 | } |
279 | |
280 | static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) |
281 | { |
282 | char *endptr, *tok, *name; |
283 | struct map *map = ms->map; |
284 | struct addr_map_symbol target = { |
285 | .ms = { .map = map, }, |
286 | }; |
287 | |
288 | ops->target.addr = strtoull(ops->raw, &endptr, 16); |
289 | |
290 | name = strchr(endptr, '<'); |
291 | if (name == NULL) |
292 | goto indirect_call; |
293 | |
294 | name++; |
295 | |
296 | if (arch->objdump.skip_functions_char && |
297 | strchr(name, arch->objdump.skip_functions_char)) |
298 | return -1; |
299 | |
300 | tok = strchr(name, '>'); |
301 | if (tok == NULL) |
302 | return -1; |
303 | |
304 | *tok = '\0'; |
305 | ops->target.name = strdup(name); |
306 | *tok = '>'; |
307 | |
308 | if (ops->target.name == NULL) |
309 | return -1; |
310 | find_target: |
311 | target.addr = map__objdump_2mem(map, ip: ops->target.addr); |
312 | |
313 | if (maps__find_ams(maps: ms->maps, ams: &target) == 0 && |
314 | map__rip_2objdump(map: target.ms.map, rip: map__map_ip(map: target.ms.map, ip_or_rip: target.addr)) == ops->target.addr) |
315 | ops->target.sym = target.ms.sym; |
316 | |
317 | return 0; |
318 | |
319 | indirect_call: |
320 | tok = strchr(endptr, '*'); |
321 | if (tok != NULL) { |
322 | endptr++; |
323 | |
324 | /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx). |
325 | * Do not parse such instruction. */ |
326 | if (strstr(endptr, "(%r" ) == NULL) |
327 | ops->target.addr = strtoull(endptr, NULL, 16); |
328 | } |
329 | goto find_target; |
330 | } |
331 | |
332 | static int call__scnprintf(struct ins *ins, char *bf, size_t size, |
333 | struct ins_operands *ops, int max_ins_name) |
334 | { |
335 | if (ops->target.sym) |
336 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, ins->name, ops->target.sym->name); |
337 | |
338 | if (ops->target.addr == 0) |
339 | return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); |
340 | |
341 | if (ops->target.name) |
342 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, ins->name, ops->target.name); |
343 | |
344 | return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr); |
345 | } |
346 | |
347 | static struct ins_ops call_ops = { |
348 | .parse = call__parse, |
349 | .scnprintf = call__scnprintf, |
350 | }; |
351 | |
352 | bool ins__is_call(const struct ins *ins) |
353 | { |
354 | return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops; |
355 | } |
356 | |
357 | /* |
358 | * Prevents from matching commas in the comment section, e.g.: |
359 | * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast |
360 | * |
361 | * and skip comma as part of function arguments, e.g.: |
362 | * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc> |
363 | */ |
364 | static inline const char *validate_comma(const char *c, struct ins_operands *ops) |
365 | { |
366 | if (ops->jump.raw_comment && c > ops->jump.raw_comment) |
367 | return NULL; |
368 | |
369 | if (ops->jump.raw_func_start && c > ops->jump.raw_func_start) |
370 | return NULL; |
371 | |
372 | return c; |
373 | } |
374 | |
375 | static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) |
376 | { |
377 | struct map *map = ms->map; |
378 | struct symbol *sym = ms->sym; |
379 | struct addr_map_symbol target = { |
380 | .ms = { .map = map, }, |
381 | }; |
382 | const char *c = strchr(ops->raw, ','); |
383 | u64 start, end; |
384 | |
385 | ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char); |
386 | ops->jump.raw_func_start = strchr(ops->raw, '<'); |
387 | |
388 | c = validate_comma(c, ops); |
389 | |
390 | /* |
391 | * Examples of lines to parse for the _cpp_lex_token@@Base |
392 | * function: |
393 | * |
394 | * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> |
395 | * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> |
396 | * |
397 | * The first is a jump to an offset inside the same function, |
398 | * the second is to another function, i.e. that 0xa72 is an |
399 | * offset in the cpp_named_operator2name@@base function. |
400 | */ |
401 | /* |
402 | * skip over possible up to 2 operands to get to address, e.g.: |
403 | * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> |
404 | */ |
405 | if (c++ != NULL) { |
406 | ops->target.addr = strtoull(c, NULL, 16); |
407 | if (!ops->target.addr) { |
408 | c = strchr(c, ','); |
409 | c = validate_comma(c, ops); |
410 | if (c++ != NULL) |
411 | ops->target.addr = strtoull(c, NULL, 16); |
412 | } |
413 | } else { |
414 | ops->target.addr = strtoull(ops->raw, NULL, 16); |
415 | } |
416 | |
417 | target.addr = map__objdump_2mem(map, ip: ops->target.addr); |
418 | start = map__unmap_ip(map, ip_or_rip: sym->start); |
419 | end = map__unmap_ip(map, ip_or_rip: sym->end); |
420 | |
421 | ops->target.outside = target.addr < start || target.addr > end; |
422 | |
423 | /* |
424 | * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): |
425 | |
426 | cpp_named_operator2name@@Base+0xa72 |
427 | |
428 | * Point to a place that is after the cpp_named_operator2name |
429 | * boundaries, i.e. in the ELF symbol table for cc1 |
430 | * cpp_named_operator2name is marked as being 32-bytes long, but it in |
431 | * fact is much larger than that, so we seem to need a symbols__find() |
432 | * routine that looks for >= current->start and < next_symbol->start, |
433 | * possibly just for C++ objects? |
434 | * |
435 | * For now lets just make some progress by marking jumps to outside the |
436 | * current function as call like. |
437 | * |
438 | * Actual navigation will come next, with further understanding of how |
439 | * the symbol searching and disassembly should be done. |
440 | */ |
441 | if (maps__find_ams(maps: ms->maps, ams: &target) == 0 && |
442 | map__rip_2objdump(map: target.ms.map, rip: map__map_ip(map: target.ms.map, ip_or_rip: target.addr)) == ops->target.addr) |
443 | ops->target.sym = target.ms.sym; |
444 | |
445 | if (!ops->target.outside) { |
446 | ops->target.offset = target.addr - start; |
447 | ops->target.offset_avail = true; |
448 | } else { |
449 | ops->target.offset_avail = false; |
450 | } |
451 | |
452 | return 0; |
453 | } |
454 | |
455 | static int jump__scnprintf(struct ins *ins, char *bf, size_t size, |
456 | struct ins_operands *ops, int max_ins_name) |
457 | { |
458 | const char *c; |
459 | |
460 | if (!ops->target.addr || ops->target.offset < 0) |
461 | return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); |
462 | |
463 | if (ops->target.outside && ops->target.sym != NULL) |
464 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, ins->name, ops->target.sym->name); |
465 | |
466 | c = strchr(ops->raw, ','); |
467 | c = validate_comma(c, ops); |
468 | |
469 | if (c != NULL) { |
470 | const char *c2 = strchr(c + 1, ','); |
471 | |
472 | c2 = validate_comma(c: c2, ops); |
473 | /* check for 3-op insn */ |
474 | if (c2 != NULL) |
475 | c = c2; |
476 | c++; |
477 | |
478 | /* mirror arch objdump's space-after-comma style */ |
479 | if (*c == ' ') |
480 | c++; |
481 | } |
482 | |
483 | return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name, |
484 | ins->name, c ? c - ops->raw : 0, ops->raw, |
485 | ops->target.offset); |
486 | } |
487 | |
488 | static void jump__delete(struct ins_operands *ops __maybe_unused) |
489 | { |
490 | /* |
491 | * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the |
492 | * raw string, don't free them. |
493 | */ |
494 | } |
495 | |
496 | static struct ins_ops jump_ops = { |
497 | .free = jump__delete, |
498 | .parse = jump__parse, |
499 | .scnprintf = jump__scnprintf, |
500 | }; |
501 | |
502 | bool ins__is_jump(const struct ins *ins) |
503 | { |
504 | return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops; |
505 | } |
506 | |
507 | static int (char *raw, char *, u64 *addrp, char **namep) |
508 | { |
509 | char *endptr, *name, *t; |
510 | |
511 | if (strstr(raw, "(%rip)" ) == NULL) |
512 | return 0; |
513 | |
514 | *addrp = strtoull(comment, &endptr, 16); |
515 | if (endptr == comment) |
516 | return 0; |
517 | name = strchr(endptr, '<'); |
518 | if (name == NULL) |
519 | return -1; |
520 | |
521 | name++; |
522 | |
523 | t = strchr(name, '>'); |
524 | if (t == NULL) |
525 | return 0; |
526 | |
527 | *t = '\0'; |
528 | *namep = strdup(name); |
529 | *t = '>'; |
530 | |
531 | return 0; |
532 | } |
533 | |
534 | static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) |
535 | { |
536 | ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); |
537 | if (ops->locked.ops == NULL) |
538 | return 0; |
539 | |
540 | if (disasm_line__parse(line: ops->raw, namep: &ops->locked.ins.name, rawp: &ops->locked.ops->raw) < 0) |
541 | goto out_free_ops; |
542 | |
543 | ops->locked.ins.ops = ins__find(arch, name: ops->locked.ins.name); |
544 | |
545 | if (ops->locked.ins.ops == NULL) |
546 | goto out_free_ops; |
547 | |
548 | if (ops->locked.ins.ops->parse && |
549 | ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0) |
550 | goto out_free_ops; |
551 | |
552 | return 0; |
553 | |
554 | out_free_ops: |
555 | zfree(&ops->locked.ops); |
556 | return 0; |
557 | } |
558 | |
559 | static int lock__scnprintf(struct ins *ins, char *bf, size_t size, |
560 | struct ins_operands *ops, int max_ins_name) |
561 | { |
562 | int printed; |
563 | |
564 | if (ops->locked.ins.ops == NULL) |
565 | return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); |
566 | |
567 | printed = scnprintf(buf: bf, size, fmt: "%-*s " , max_ins_name, ins->name); |
568 | return printed + ins__scnprintf(ins: &ops->locked.ins, bf: bf + printed, |
569 | size: size - printed, ops: ops->locked.ops, max_ins_name); |
570 | } |
571 | |
572 | static void lock__delete(struct ins_operands *ops) |
573 | { |
574 | struct ins *ins = &ops->locked.ins; |
575 | |
576 | if (ins->ops && ins->ops->free) |
577 | ins->ops->free(ops->locked.ops); |
578 | else |
579 | ins__delete(ops: ops->locked.ops); |
580 | |
581 | zfree(&ops->locked.ops); |
582 | zfree(&ops->target.raw); |
583 | zfree(&ops->target.name); |
584 | } |
585 | |
586 | static struct ins_ops lock_ops = { |
587 | .free = lock__delete, |
588 | .parse = lock__parse, |
589 | .scnprintf = lock__scnprintf, |
590 | }; |
591 | |
592 | /* |
593 | * Check if the operand has more than one registers like x86 SIB addressing: |
594 | * 0x1234(%rax, %rbx, 8) |
595 | * |
596 | * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check |
597 | * the input string after 'memory_ref_char' if exists. |
598 | */ |
599 | static bool check_multi_regs(struct arch *arch, const char *op) |
600 | { |
601 | int count = 0; |
602 | |
603 | if (arch->objdump.register_char == 0) |
604 | return false; |
605 | |
606 | if (arch->objdump.memory_ref_char) { |
607 | op = strchr(op, arch->objdump.memory_ref_char); |
608 | if (op == NULL) |
609 | return false; |
610 | } |
611 | |
612 | while ((op = strchr(op, arch->objdump.register_char)) != NULL) { |
613 | count++; |
614 | op++; |
615 | } |
616 | |
617 | return count > 1; |
618 | } |
619 | |
620 | static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) |
621 | { |
622 | char *s = strchr(ops->raw, ','), *target, *, prev; |
623 | |
624 | if (s == NULL) |
625 | return -1; |
626 | |
627 | *s = '\0'; |
628 | |
629 | /* |
630 | * x86 SIB addressing has something like 0x8(%rax, %rcx, 1) |
631 | * then it needs to have the closing parenthesis. |
632 | */ |
633 | if (strchr(ops->raw, '(')) { |
634 | *s = ','; |
635 | s = strchr(ops->raw, ')'); |
636 | if (s == NULL || s[1] != ',') |
637 | return -1; |
638 | *++s = '\0'; |
639 | } |
640 | |
641 | ops->source.raw = strdup(ops->raw); |
642 | *s = ','; |
643 | |
644 | if (ops->source.raw == NULL) |
645 | return -1; |
646 | |
647 | ops->source.multi_regs = check_multi_regs(arch, op: ops->source.raw); |
648 | |
649 | target = skip_spaces(++s); |
650 | comment = strchr(s, arch->objdump.comment_char); |
651 | |
652 | if (comment != NULL) |
653 | s = comment - 1; |
654 | else |
655 | s = strchr(s, '\0') - 1; |
656 | |
657 | while (s > target && isspace(s[0])) |
658 | --s; |
659 | s++; |
660 | prev = *s; |
661 | *s = '\0'; |
662 | |
663 | ops->target.raw = strdup(target); |
664 | *s = prev; |
665 | |
666 | if (ops->target.raw == NULL) |
667 | goto out_free_source; |
668 | |
669 | ops->target.multi_regs = check_multi_regs(arch, op: ops->target.raw); |
670 | |
671 | if (comment == NULL) |
672 | return 0; |
673 | |
674 | comment = skip_spaces(comment); |
675 | comment__symbol(raw: ops->source.raw, comment: comment + 1, addrp: &ops->source.addr, namep: &ops->source.name); |
676 | comment__symbol(raw: ops->target.raw, comment: comment + 1, addrp: &ops->target.addr, namep: &ops->target.name); |
677 | |
678 | return 0; |
679 | |
680 | out_free_source: |
681 | zfree(&ops->source.raw); |
682 | return -1; |
683 | } |
684 | |
685 | static int mov__scnprintf(struct ins *ins, char *bf, size_t size, |
686 | struct ins_operands *ops, int max_ins_name) |
687 | { |
688 | return scnprintf(buf: bf, size, fmt: "%-*s %s,%s" , max_ins_name, ins->name, |
689 | ops->source.name ?: ops->source.raw, |
690 | ops->target.name ?: ops->target.raw); |
691 | } |
692 | |
693 | static struct ins_ops mov_ops = { |
694 | .parse = mov__parse, |
695 | .scnprintf = mov__scnprintf, |
696 | }; |
697 | |
698 | static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) |
699 | { |
700 | char *target, *, *s, prev; |
701 | |
702 | target = s = ops->raw; |
703 | |
704 | while (s[0] != '\0' && !isspace(s[0])) |
705 | ++s; |
706 | prev = *s; |
707 | *s = '\0'; |
708 | |
709 | ops->target.raw = strdup(target); |
710 | *s = prev; |
711 | |
712 | if (ops->target.raw == NULL) |
713 | return -1; |
714 | |
715 | comment = strchr(s, arch->objdump.comment_char); |
716 | if (comment == NULL) |
717 | return 0; |
718 | |
719 | comment = skip_spaces(comment); |
720 | comment__symbol(raw: ops->target.raw, comment: comment + 1, addrp: &ops->target.addr, namep: &ops->target.name); |
721 | |
722 | return 0; |
723 | } |
724 | |
725 | static int dec__scnprintf(struct ins *ins, char *bf, size_t size, |
726 | struct ins_operands *ops, int max_ins_name) |
727 | { |
728 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, ins->name, |
729 | ops->target.name ?: ops->target.raw); |
730 | } |
731 | |
732 | static struct ins_ops dec_ops = { |
733 | .parse = dec__parse, |
734 | .scnprintf = dec__scnprintf, |
735 | }; |
736 | |
737 | static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, |
738 | struct ins_operands *ops __maybe_unused, int max_ins_name) |
739 | { |
740 | return scnprintf(buf: bf, size, fmt: "%-*s" , max_ins_name, "nop" ); |
741 | } |
742 | |
743 | static struct ins_ops nop_ops = { |
744 | .scnprintf = nop__scnprintf, |
745 | }; |
746 | |
747 | static struct ins_ops ret_ops = { |
748 | .scnprintf = ins__raw_scnprintf, |
749 | }; |
750 | |
751 | bool ins__is_ret(const struct ins *ins) |
752 | { |
753 | return ins->ops == &ret_ops; |
754 | } |
755 | |
756 | bool ins__is_lock(const struct ins *ins) |
757 | { |
758 | return ins->ops == &lock_ops; |
759 | } |
760 | |
761 | static int ins__key_cmp(const void *name, const void *insp) |
762 | { |
763 | const struct ins *ins = insp; |
764 | |
765 | return strcmp(name, ins->name); |
766 | } |
767 | |
768 | static int ins__cmp(const void *a, const void *b) |
769 | { |
770 | const struct ins *ia = a; |
771 | const struct ins *ib = b; |
772 | |
773 | return strcmp(ia->name, ib->name); |
774 | } |
775 | |
776 | static void ins__sort(struct arch *arch) |
777 | { |
778 | const int nmemb = arch->nr_instructions; |
779 | |
780 | qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); |
781 | } |
782 | |
783 | static struct ins_ops *__ins__find(struct arch *arch, const char *name) |
784 | { |
785 | struct ins *ins; |
786 | const int nmemb = arch->nr_instructions; |
787 | |
788 | if (!arch->sorted_instructions) { |
789 | ins__sort(arch); |
790 | arch->sorted_instructions = true; |
791 | } |
792 | |
793 | ins = bsearch(key: name, base: arch->instructions, num: nmemb, size: sizeof(struct ins), cmp: ins__key_cmp); |
794 | if (ins) |
795 | return ins->ops; |
796 | |
797 | if (arch->insn_suffix) { |
798 | char tmp[32]; |
799 | char suffix; |
800 | size_t len = strlen(name); |
801 | |
802 | if (len == 0 || len >= sizeof(tmp)) |
803 | return NULL; |
804 | |
805 | suffix = name[len - 1]; |
806 | if (strchr(arch->insn_suffix, suffix) == NULL) |
807 | return NULL; |
808 | |
809 | strcpy(p: tmp, q: name); |
810 | tmp[len - 1] = '\0'; /* remove the suffix and check again */ |
811 | |
812 | ins = bsearch(key: tmp, base: arch->instructions, num: nmemb, size: sizeof(struct ins), cmp: ins__key_cmp); |
813 | } |
814 | return ins ? ins->ops : NULL; |
815 | } |
816 | |
817 | static struct ins_ops *ins__find(struct arch *arch, const char *name) |
818 | { |
819 | struct ins_ops *ops = __ins__find(arch, name); |
820 | |
821 | if (!ops && arch->associate_instruction_ops) |
822 | ops = arch->associate_instruction_ops(arch, name); |
823 | |
824 | return ops; |
825 | } |
826 | |
827 | static int arch__key_cmp(const void *name, const void *archp) |
828 | { |
829 | const struct arch *arch = archp; |
830 | |
831 | return strcmp(name, arch->name); |
832 | } |
833 | |
834 | static int arch__cmp(const void *a, const void *b) |
835 | { |
836 | const struct arch *aa = a; |
837 | const struct arch *ab = b; |
838 | |
839 | return strcmp(aa->name, ab->name); |
840 | } |
841 | |
842 | static void arch__sort(void) |
843 | { |
844 | const int nmemb = ARRAY_SIZE(architectures); |
845 | |
846 | qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); |
847 | } |
848 | |
849 | static struct arch *arch__find(const char *name) |
850 | { |
851 | const int nmemb = ARRAY_SIZE(architectures); |
852 | static bool sorted; |
853 | |
854 | if (!sorted) { |
855 | arch__sort(); |
856 | sorted = true; |
857 | } |
858 | |
859 | return bsearch(key: name, base: architectures, num: nmemb, size: sizeof(struct arch), cmp: arch__key_cmp); |
860 | } |
861 | |
862 | bool arch__is(struct arch *arch, const char *name) |
863 | { |
864 | return !strcmp(arch->name, name); |
865 | } |
866 | |
867 | /* symbol histogram: key = offset << 16 | evsel->core.idx */ |
868 | static size_t sym_hist_hash(long key, void *ctx __maybe_unused) |
869 | { |
870 | return (key >> 16) + (key & 0xffff); |
871 | } |
872 | |
873 | static bool sym_hist_equal(long key1, long key2, void *ctx __maybe_unused) |
874 | { |
875 | return key1 == key2; |
876 | } |
877 | |
878 | static struct annotated_source *annotated_source__new(void) |
879 | { |
880 | struct annotated_source *src = zalloc(sizeof(*src)); |
881 | |
882 | if (src != NULL) |
883 | INIT_LIST_HEAD(list: &src->source); |
884 | |
885 | return src; |
886 | } |
887 | |
888 | static __maybe_unused void annotated_source__delete(struct annotated_source *src) |
889 | { |
890 | if (src == NULL) |
891 | return; |
892 | |
893 | hashmap__free(map: src->samples); |
894 | zfree(&src->histograms); |
895 | free(src); |
896 | } |
897 | |
898 | static int annotated_source__alloc_histograms(struct annotated_source *src, |
899 | int nr_hists) |
900 | { |
901 | src->nr_histograms = nr_hists; |
902 | src->histograms = calloc(nr_hists, sizeof(*src->histograms)); |
903 | |
904 | if (src->histograms == NULL) |
905 | return -1; |
906 | |
907 | src->samples = hashmap__new(hash_fn: sym_hist_hash, equal_fn: sym_hist_equal, NULL); |
908 | if (src->samples == NULL) |
909 | zfree(&src->histograms); |
910 | |
911 | return src->histograms ? 0 : -1; |
912 | } |
913 | |
914 | void symbol__annotate_zero_histograms(struct symbol *sym) |
915 | { |
916 | struct annotation *notes = symbol__annotation(sym); |
917 | |
918 | annotation__lock(notes); |
919 | if (notes->src != NULL) { |
920 | memset(notes->src->histograms, 0, |
921 | notes->src->nr_histograms * sizeof(*notes->src->histograms)); |
922 | hashmap__clear(map: notes->src->samples); |
923 | } |
924 | if (notes->branch && notes->branch->cycles_hist) { |
925 | memset(notes->branch->cycles_hist, 0, |
926 | symbol__size(sym) * sizeof(struct cyc_hist)); |
927 | } |
928 | annotation__unlock(notes); |
929 | } |
930 | |
931 | static int __symbol__account_cycles(struct cyc_hist *ch, |
932 | u64 start, |
933 | unsigned offset, unsigned cycles, |
934 | unsigned have_start) |
935 | { |
936 | /* |
937 | * For now we can only account one basic block per |
938 | * final jump. But multiple could be overlapping. |
939 | * Always account the longest one. So when |
940 | * a shorter one has been already seen throw it away. |
941 | * |
942 | * We separately always account the full cycles. |
943 | */ |
944 | ch[offset].num_aggr++; |
945 | ch[offset].cycles_aggr += cycles; |
946 | |
947 | if (cycles > ch[offset].cycles_max) |
948 | ch[offset].cycles_max = cycles; |
949 | |
950 | if (ch[offset].cycles_min) { |
951 | if (cycles && cycles < ch[offset].cycles_min) |
952 | ch[offset].cycles_min = cycles; |
953 | } else |
954 | ch[offset].cycles_min = cycles; |
955 | |
956 | if (!have_start && ch[offset].have_start) |
957 | return 0; |
958 | if (ch[offset].num) { |
959 | if (have_start && (!ch[offset].have_start || |
960 | ch[offset].start > start)) { |
961 | ch[offset].have_start = 0; |
962 | ch[offset].cycles = 0; |
963 | ch[offset].num = 0; |
964 | if (ch[offset].reset < 0xffff) |
965 | ch[offset].reset++; |
966 | } else if (have_start && |
967 | ch[offset].start < start) |
968 | return 0; |
969 | } |
970 | |
971 | if (ch[offset].num < NUM_SPARKS) |
972 | ch[offset].cycles_spark[ch[offset].num] = cycles; |
973 | |
974 | ch[offset].have_start = have_start; |
975 | ch[offset].start = start; |
976 | ch[offset].cycles += cycles; |
977 | ch[offset].num++; |
978 | return 0; |
979 | } |
980 | |
981 | static int __symbol__inc_addr_samples(struct map_symbol *ms, |
982 | struct annotated_source *src, int evidx, u64 addr, |
983 | struct perf_sample *sample) |
984 | { |
985 | struct symbol *sym = ms->sym; |
986 | long hash_key; |
987 | u64 offset; |
988 | struct sym_hist *h; |
989 | struct sym_hist_entry *entry; |
990 | |
991 | pr_debug3("%s: addr=%#" PRIx64 "\n" , __func__, map__unmap_ip(ms->map, addr)); |
992 | |
993 | if ((addr < sym->start || addr >= sym->end) && |
994 | (addr != sym->end || sym->start != sym->end)) { |
995 | pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n" , |
996 | __func__, __LINE__, sym->name, sym->start, addr, sym->end); |
997 | return -ERANGE; |
998 | } |
999 | |
1000 | offset = addr - sym->start; |
1001 | h = annotated_source__histogram(src, idx: evidx); |
1002 | if (h == NULL) { |
1003 | pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n" , |
1004 | __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC); |
1005 | return -ENOMEM; |
1006 | } |
1007 | |
1008 | hash_key = offset << 16 | evidx; |
1009 | if (!hashmap__find(src->samples, hash_key, &entry)) { |
1010 | entry = zalloc(sizeof(*entry)); |
1011 | if (entry == NULL) |
1012 | return -ENOMEM; |
1013 | |
1014 | if (hashmap__add(src->samples, hash_key, entry) < 0) |
1015 | return -ENOMEM; |
1016 | } |
1017 | |
1018 | h->nr_samples++; |
1019 | h->period += sample->period; |
1020 | entry->nr_samples++; |
1021 | entry->period += sample->period; |
1022 | |
1023 | pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 |
1024 | ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n" , |
1025 | sym->start, sym->name, addr, addr - sym->start, evidx, |
1026 | entry->nr_samples, entry->period); |
1027 | return 0; |
1028 | } |
1029 | |
1030 | struct annotated_branch *annotation__get_branch(struct annotation *notes) |
1031 | { |
1032 | if (notes == NULL) |
1033 | return NULL; |
1034 | |
1035 | if (notes->branch == NULL) |
1036 | notes->branch = zalloc(sizeof(*notes->branch)); |
1037 | |
1038 | return notes->branch; |
1039 | } |
1040 | |
1041 | static struct cyc_hist *symbol__cycles_hist(struct symbol *sym) |
1042 | { |
1043 | struct annotation *notes = symbol__annotation(sym); |
1044 | struct annotated_branch *branch; |
1045 | |
1046 | branch = annotation__get_branch(notes); |
1047 | if (branch == NULL) |
1048 | return NULL; |
1049 | |
1050 | if (branch->cycles_hist == NULL) { |
1051 | const size_t size = symbol__size(sym); |
1052 | |
1053 | branch->cycles_hist = calloc(size, sizeof(struct cyc_hist)); |
1054 | } |
1055 | |
1056 | return branch->cycles_hist; |
1057 | } |
1058 | |
1059 | struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) |
1060 | { |
1061 | struct annotation *notes = symbol__annotation(sym); |
1062 | |
1063 | if (notes->src == NULL) { |
1064 | notes->src = annotated_source__new(); |
1065 | if (notes->src == NULL) |
1066 | return NULL; |
1067 | goto alloc_histograms; |
1068 | } |
1069 | |
1070 | if (notes->src->histograms == NULL) { |
1071 | alloc_histograms: |
1072 | annotated_source__alloc_histograms(src: notes->src, nr_hists); |
1073 | } |
1074 | |
1075 | return notes->src; |
1076 | } |
1077 | |
1078 | static int symbol__inc_addr_samples(struct map_symbol *ms, |
1079 | struct evsel *evsel, u64 addr, |
1080 | struct perf_sample *sample) |
1081 | { |
1082 | struct symbol *sym = ms->sym; |
1083 | struct annotated_source *src; |
1084 | |
1085 | if (sym == NULL) |
1086 | return 0; |
1087 | src = symbol__hists(sym, nr_hists: evsel->evlist->core.nr_entries); |
1088 | return src ? __symbol__inc_addr_samples(ms, src, evidx: evsel->core.idx, addr, sample) : 0; |
1089 | } |
1090 | |
1091 | static int symbol__account_cycles(u64 addr, u64 start, |
1092 | struct symbol *sym, unsigned cycles) |
1093 | { |
1094 | struct cyc_hist *cycles_hist; |
1095 | unsigned offset; |
1096 | |
1097 | if (sym == NULL) |
1098 | return 0; |
1099 | cycles_hist = symbol__cycles_hist(sym); |
1100 | if (cycles_hist == NULL) |
1101 | return -ENOMEM; |
1102 | if (addr < sym->start || addr >= sym->end) |
1103 | return -ERANGE; |
1104 | |
1105 | if (start) { |
1106 | if (start < sym->start || start >= sym->end) |
1107 | return -ERANGE; |
1108 | if (start >= addr) |
1109 | start = 0; |
1110 | } |
1111 | offset = addr - sym->start; |
1112 | return __symbol__account_cycles(ch: cycles_hist, |
1113 | start: start ? start - sym->start : 0, |
1114 | offset, cycles, |
1115 | have_start: !!start); |
1116 | } |
1117 | |
1118 | int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, |
1119 | struct addr_map_symbol *start, |
1120 | unsigned cycles) |
1121 | { |
1122 | u64 saddr = 0; |
1123 | int err; |
1124 | |
1125 | if (!cycles) |
1126 | return 0; |
1127 | |
1128 | /* |
1129 | * Only set start when IPC can be computed. We can only |
1130 | * compute it when the basic block is completely in a single |
1131 | * function. |
1132 | * Special case the case when the jump is elsewhere, but |
1133 | * it starts on the function start. |
1134 | */ |
1135 | if (start && |
1136 | (start->ms.sym == ams->ms.sym || |
1137 | (ams->ms.sym && |
1138 | start->addr == ams->ms.sym->start + map__start(map: ams->ms.map)))) |
1139 | saddr = start->al_addr; |
1140 | if (saddr == 0) |
1141 | pr_debug2("BB with bad start: addr %" PRIx64" start %" PRIx64" sym %" PRIx64" saddr %" PRIx64"\n" , |
1142 | ams->addr, |
1143 | start ? start->addr : 0, |
1144 | ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0, |
1145 | saddr); |
1146 | err = symbol__account_cycles(addr: ams->al_addr, start: saddr, sym: ams->ms.sym, cycles); |
1147 | if (err) |
1148 | pr_debug2("account_cycles failed %d\n" , err); |
1149 | return err; |
1150 | } |
1151 | |
1152 | static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) |
1153 | { |
1154 | unsigned n_insn = 0; |
1155 | u64 offset; |
1156 | |
1157 | for (offset = start; offset <= end; offset++) { |
1158 | if (notes->src->offsets[offset]) |
1159 | n_insn++; |
1160 | } |
1161 | return n_insn; |
1162 | } |
1163 | |
1164 | static void annotated_branch__delete(struct annotated_branch *branch) |
1165 | { |
1166 | if (branch) { |
1167 | zfree(&branch->cycles_hist); |
1168 | free(branch); |
1169 | } |
1170 | } |
1171 | |
1172 | static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) |
1173 | { |
1174 | unsigned n_insn; |
1175 | unsigned int cover_insn = 0; |
1176 | u64 offset; |
1177 | |
1178 | n_insn = annotation__count_insn(notes, start, end); |
1179 | if (n_insn && ch->num && ch->cycles) { |
1180 | struct annotated_branch *branch; |
1181 | float ipc = n_insn / ((double)ch->cycles / (double)ch->num); |
1182 | |
1183 | /* Hide data when there are too many overlaps. */ |
1184 | if (ch->reset >= 0x7fff) |
1185 | return; |
1186 | |
1187 | for (offset = start; offset <= end; offset++) { |
1188 | struct annotation_line *al = notes->src->offsets[offset]; |
1189 | |
1190 | if (al && al->cycles && al->cycles->ipc == 0.0) { |
1191 | al->cycles->ipc = ipc; |
1192 | cover_insn++; |
1193 | } |
1194 | } |
1195 | |
1196 | branch = annotation__get_branch(notes); |
1197 | if (cover_insn && branch) { |
1198 | branch->hit_cycles += ch->cycles; |
1199 | branch->hit_insn += n_insn * ch->num; |
1200 | branch->cover_insn += cover_insn; |
1201 | } |
1202 | } |
1203 | } |
1204 | |
1205 | static int annotation__compute_ipc(struct annotation *notes, size_t size) |
1206 | { |
1207 | int err = 0; |
1208 | s64 offset; |
1209 | |
1210 | if (!notes->branch || !notes->branch->cycles_hist) |
1211 | return 0; |
1212 | |
1213 | notes->branch->total_insn = annotation__count_insn(notes, start: 0, end: size - 1); |
1214 | notes->branch->hit_cycles = 0; |
1215 | notes->branch->hit_insn = 0; |
1216 | notes->branch->cover_insn = 0; |
1217 | |
1218 | annotation__lock(notes); |
1219 | for (offset = size - 1; offset >= 0; --offset) { |
1220 | struct cyc_hist *ch; |
1221 | |
1222 | ch = ¬es->branch->cycles_hist[offset]; |
1223 | if (ch && ch->cycles) { |
1224 | struct annotation_line *al; |
1225 | |
1226 | al = notes->src->offsets[offset]; |
1227 | if (al && al->cycles == NULL) { |
1228 | al->cycles = zalloc(sizeof(*al->cycles)); |
1229 | if (al->cycles == NULL) { |
1230 | err = ENOMEM; |
1231 | break; |
1232 | } |
1233 | } |
1234 | if (ch->have_start) |
1235 | annotation__count_and_fill(notes, start: ch->start, end: offset, ch); |
1236 | if (al && ch->num_aggr) { |
1237 | al->cycles->avg = ch->cycles_aggr / ch->num_aggr; |
1238 | al->cycles->max = ch->cycles_max; |
1239 | al->cycles->min = ch->cycles_min; |
1240 | } |
1241 | } |
1242 | } |
1243 | |
1244 | if (err) { |
1245 | while (++offset < (s64)size) { |
1246 | struct cyc_hist *ch = ¬es->branch->cycles_hist[offset]; |
1247 | |
1248 | if (ch && ch->cycles) { |
1249 | struct annotation_line *al = notes->src->offsets[offset]; |
1250 | if (al) |
1251 | zfree(&al->cycles); |
1252 | } |
1253 | } |
1254 | } |
1255 | |
1256 | annotation__unlock(notes); |
1257 | return 0; |
1258 | } |
1259 | |
1260 | int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, |
1261 | struct evsel *evsel) |
1262 | { |
1263 | return symbol__inc_addr_samples(ms: &ams->ms, evsel, addr: ams->al_addr, sample); |
1264 | } |
1265 | |
1266 | int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, |
1267 | struct evsel *evsel, u64 ip) |
1268 | { |
1269 | return symbol__inc_addr_samples(ms: &he->ms, evsel, addr: ip, sample); |
1270 | } |
1271 | |
1272 | static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) |
1273 | { |
1274 | dl->ins.ops = ins__find(arch, name: dl->ins.name); |
1275 | |
1276 | if (!dl->ins.ops) |
1277 | return; |
1278 | |
1279 | if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0) |
1280 | dl->ins.ops = NULL; |
1281 | } |
1282 | |
1283 | static int disasm_line__parse(char *line, const char **namep, char **rawp) |
1284 | { |
1285 | char tmp, *name = skip_spaces(line); |
1286 | |
1287 | if (name[0] == '\0') |
1288 | return -1; |
1289 | |
1290 | *rawp = name + 1; |
1291 | |
1292 | while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) |
1293 | ++*rawp; |
1294 | |
1295 | tmp = (*rawp)[0]; |
1296 | (*rawp)[0] = '\0'; |
1297 | *namep = strdup(name); |
1298 | |
1299 | if (*namep == NULL) |
1300 | goto out; |
1301 | |
1302 | (*rawp)[0] = tmp; |
1303 | *rawp = strim(*rawp); |
1304 | |
1305 | return 0; |
1306 | |
1307 | out: |
1308 | return -1; |
1309 | } |
1310 | |
1311 | struct annotate_args { |
1312 | struct arch *arch; |
1313 | struct map_symbol ms; |
1314 | struct evsel *evsel; |
1315 | struct annotation_options *options; |
1316 | s64 offset; |
1317 | char *line; |
1318 | int line_nr; |
1319 | char *fileloc; |
1320 | }; |
1321 | |
1322 | static void annotation_line__init(struct annotation_line *al, |
1323 | struct annotate_args *args, |
1324 | int nr) |
1325 | { |
1326 | al->offset = args->offset; |
1327 | al->line = strdup(args->line); |
1328 | al->line_nr = args->line_nr; |
1329 | al->fileloc = args->fileloc; |
1330 | al->data_nr = nr; |
1331 | } |
1332 | |
1333 | static void annotation_line__exit(struct annotation_line *al) |
1334 | { |
1335 | zfree_srcline(srcline: &al->path); |
1336 | zfree(&al->line); |
1337 | zfree(&al->cycles); |
1338 | } |
1339 | |
1340 | static size_t disasm_line_size(int nr) |
1341 | { |
1342 | struct annotation_line *al; |
1343 | |
1344 | return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr)); |
1345 | } |
1346 | |
1347 | /* |
1348 | * Allocating the disasm annotation line data with |
1349 | * following structure: |
1350 | * |
1351 | * ------------------------------------------- |
1352 | * struct disasm_line | struct annotation_line |
1353 | * ------------------------------------------- |
1354 | * |
1355 | * We have 'struct annotation_line' member as last member |
1356 | * of 'struct disasm_line' to have an easy access. |
1357 | */ |
1358 | static struct disasm_line *disasm_line__new(struct annotate_args *args) |
1359 | { |
1360 | struct disasm_line *dl = NULL; |
1361 | int nr = 1; |
1362 | |
1363 | if (evsel__is_group_event(evsel: args->evsel)) |
1364 | nr = args->evsel->core.nr_members; |
1365 | |
1366 | dl = zalloc(disasm_line_size(nr)); |
1367 | if (!dl) |
1368 | return NULL; |
1369 | |
1370 | annotation_line__init(al: &dl->al, args, nr); |
1371 | if (dl->al.line == NULL) |
1372 | goto out_delete; |
1373 | |
1374 | if (args->offset != -1) { |
1375 | if (disasm_line__parse(line: dl->al.line, namep: &dl->ins.name, rawp: &dl->ops.raw) < 0) |
1376 | goto out_free_line; |
1377 | |
1378 | disasm_line__init_ins(dl, arch: args->arch, ms: &args->ms); |
1379 | } |
1380 | |
1381 | return dl; |
1382 | |
1383 | out_free_line: |
1384 | zfree(&dl->al.line); |
1385 | out_delete: |
1386 | free(dl); |
1387 | return NULL; |
1388 | } |
1389 | |
1390 | void disasm_line__free(struct disasm_line *dl) |
1391 | { |
1392 | if (dl->ins.ops && dl->ins.ops->free) |
1393 | dl->ins.ops->free(&dl->ops); |
1394 | else |
1395 | ins__delete(ops: &dl->ops); |
1396 | zfree(&dl->ins.name); |
1397 | annotation_line__exit(al: &dl->al); |
1398 | free(dl); |
1399 | } |
1400 | |
1401 | int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name) |
1402 | { |
1403 | if (raw || !dl->ins.ops) |
1404 | return scnprintf(buf: bf, size, fmt: "%-*s %s" , max_ins_name, dl->ins.name, dl->ops.raw); |
1405 | |
1406 | return ins__scnprintf(ins: &dl->ins, bf, size, ops: &dl->ops, max_ins_name); |
1407 | } |
1408 | |
1409 | void annotation__exit(struct annotation *notes) |
1410 | { |
1411 | annotated_source__delete(src: notes->src); |
1412 | annotated_branch__delete(branch: notes->branch); |
1413 | } |
1414 | |
1415 | static struct sharded_mutex *sharded_mutex; |
1416 | |
1417 | static void annotation__init_sharded_mutex(void) |
1418 | { |
1419 | /* As many mutexes as there are CPUs. */ |
1420 | sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu); |
1421 | } |
1422 | |
1423 | static size_t annotation__hash(const struct annotation *notes) |
1424 | { |
1425 | return (size_t)notes; |
1426 | } |
1427 | |
1428 | static struct mutex *annotation__get_mutex(const struct annotation *notes) |
1429 | { |
1430 | static pthread_once_t once = PTHREAD_ONCE_INIT; |
1431 | |
1432 | pthread_once(&once, annotation__init_sharded_mutex); |
1433 | if (!sharded_mutex) |
1434 | return NULL; |
1435 | |
1436 | return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes)); |
1437 | } |
1438 | |
1439 | void annotation__lock(struct annotation *notes) |
1440 | NO_THREAD_SAFETY_ANALYSIS |
1441 | { |
1442 | struct mutex *mutex = annotation__get_mutex(notes); |
1443 | |
1444 | if (mutex) |
1445 | mutex_lock(mutex); |
1446 | } |
1447 | |
1448 | void annotation__unlock(struct annotation *notes) |
1449 | NO_THREAD_SAFETY_ANALYSIS |
1450 | { |
1451 | struct mutex *mutex = annotation__get_mutex(notes); |
1452 | |
1453 | if (mutex) |
1454 | mutex_unlock(mtx: mutex); |
1455 | } |
1456 | |
1457 | bool annotation__trylock(struct annotation *notes) |
1458 | { |
1459 | struct mutex *mutex = annotation__get_mutex(notes); |
1460 | |
1461 | if (!mutex) |
1462 | return false; |
1463 | |
1464 | return mutex_trylock(lock: mutex); |
1465 | } |
1466 | |
1467 | |
1468 | static void annotation_line__add(struct annotation_line *al, struct list_head *head) |
1469 | { |
1470 | list_add_tail(new: &al->node, head); |
1471 | } |
1472 | |
1473 | struct annotation_line * |
1474 | annotation_line__next(struct annotation_line *pos, struct list_head *head) |
1475 | { |
1476 | list_for_each_entry_continue(pos, head, node) |
1477 | if (pos->offset >= 0) |
1478 | return pos; |
1479 | |
1480 | return NULL; |
1481 | } |
1482 | |
1483 | static const char *annotate__address_color(struct block_range *br) |
1484 | { |
1485 | double cov = block_range__coverage(br); |
1486 | |
1487 | if (cov >= 0) { |
1488 | /* mark red for >75% coverage */ |
1489 | if (cov > 0.75) |
1490 | return PERF_COLOR_RED; |
1491 | |
1492 | /* mark dull for <1% coverage */ |
1493 | if (cov < 0.01) |
1494 | return PERF_COLOR_NORMAL; |
1495 | } |
1496 | |
1497 | return PERF_COLOR_MAGENTA; |
1498 | } |
1499 | |
1500 | static const char *annotate__asm_color(struct block_range *br) |
1501 | { |
1502 | double cov = block_range__coverage(br); |
1503 | |
1504 | if (cov >= 0) { |
1505 | /* mark dull for <1% coverage */ |
1506 | if (cov < 0.01) |
1507 | return PERF_COLOR_NORMAL; |
1508 | } |
1509 | |
1510 | return PERF_COLOR_BLUE; |
1511 | } |
1512 | |
1513 | static void annotate__branch_printf(struct block_range *br, u64 addr) |
1514 | { |
1515 | bool = true; |
1516 | |
1517 | if (!br) |
1518 | return; |
1519 | |
1520 | #if 1 |
1521 | if (br->is_target && br->start == addr) { |
1522 | struct block_range *branch = br; |
1523 | double p; |
1524 | |
1525 | /* |
1526 | * Find matching branch to our target. |
1527 | */ |
1528 | while (!branch->is_branch) |
1529 | branch = block_range__next(br: branch); |
1530 | |
1531 | p = 100 *(double)br->entry / branch->coverage; |
1532 | |
1533 | if (p > 0.1) { |
1534 | if (emit_comment) { |
1535 | emit_comment = false; |
1536 | printf("\t#" ); |
1537 | } |
1538 | |
1539 | /* |
1540 | * The percentage of coverage joined at this target in relation |
1541 | * to the next branch. |
1542 | */ |
1543 | printf(" +%.2f%%" , p); |
1544 | } |
1545 | } |
1546 | #endif |
1547 | if (br->is_branch && br->end == addr) { |
1548 | double p = 100*(double)br->taken / br->coverage; |
1549 | |
1550 | if (p > 0.1) { |
1551 | if (emit_comment) { |
1552 | emit_comment = false; |
1553 | printf("\t#" ); |
1554 | } |
1555 | |
1556 | /* |
1557 | * The percentage of coverage leaving at this branch, and |
1558 | * its prediction ratio. |
1559 | */ |
1560 | printf(" -%.2f%% (p:%.2f%%)" , p, 100*(double)br->pred / br->taken); |
1561 | } |
1562 | } |
1563 | } |
1564 | |
1565 | static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width) |
1566 | { |
1567 | s64 offset = dl->al.offset; |
1568 | const u64 addr = start + offset; |
1569 | struct block_range *br; |
1570 | |
1571 | br = block_range__find(addr); |
1572 | color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":" , addr_fmt_width, addr); |
1573 | color_fprintf(stdout, annotate__asm_color(br), "%s" , dl->al.line); |
1574 | annotate__branch_printf(br, addr); |
1575 | return 0; |
1576 | } |
1577 | |
1578 | static int |
1579 | annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start, |
1580 | struct evsel *evsel, u64 len, int min_pcnt, int printed, |
1581 | int max_lines, struct annotation_line *queue, int addr_fmt_width, |
1582 | int percent_type) |
1583 | { |
1584 | struct disasm_line *dl = container_of(al, struct disasm_line, al); |
1585 | static const char *prev_line; |
1586 | |
1587 | if (al->offset != -1) { |
1588 | double max_percent = 0.0; |
1589 | int i, nr_percent = 1; |
1590 | const char *color; |
1591 | struct annotation *notes = symbol__annotation(sym); |
1592 | |
1593 | for (i = 0; i < al->data_nr; i++) { |
1594 | double percent; |
1595 | |
1596 | percent = annotation_data__percent(data: &al->data[i], |
1597 | which: percent_type); |
1598 | |
1599 | if (percent > max_percent) |
1600 | max_percent = percent; |
1601 | } |
1602 | |
1603 | if (al->data_nr > nr_percent) |
1604 | nr_percent = al->data_nr; |
1605 | |
1606 | if (max_percent < min_pcnt) |
1607 | return -1; |
1608 | |
1609 | if (max_lines && printed >= max_lines) |
1610 | return 1; |
1611 | |
1612 | if (queue != NULL) { |
1613 | list_for_each_entry_from(queue, ¬es->src->source, node) { |
1614 | if (queue == al) |
1615 | break; |
1616 | annotation_line__print(al: queue, sym, start, evsel, len, |
1617 | min_pcnt: 0, printed: 0, max_lines: 1, NULL, addr_fmt_width, |
1618 | percent_type); |
1619 | } |
1620 | } |
1621 | |
1622 | color = get_percent_color(percent: max_percent); |
1623 | |
1624 | for (i = 0; i < nr_percent; i++) { |
1625 | struct annotation_data *data = &al->data[i]; |
1626 | double percent; |
1627 | |
1628 | percent = annotation_data__percent(data, which: percent_type); |
1629 | color = get_percent_color(percent); |
1630 | |
1631 | if (symbol_conf.show_total_period) |
1632 | color_fprintf(stdout, color, " %11" PRIu64, |
1633 | data->he.period); |
1634 | else if (symbol_conf.show_nr_samples) |
1635 | color_fprintf(stdout, color, " %7" PRIu64, |
1636 | data->he.nr_samples); |
1637 | else |
1638 | color_fprintf(stdout, color, " %7.2f" , percent); |
1639 | } |
1640 | |
1641 | printf(" : " ); |
1642 | |
1643 | disasm_line__print(dl, start, addr_fmt_width); |
1644 | |
1645 | /* |
1646 | * Also color the filename and line if needed, with |
1647 | * the same color than the percentage. Don't print it |
1648 | * twice for close colored addr with the same filename:line |
1649 | */ |
1650 | if (al->path) { |
1651 | if (!prev_line || strcmp(prev_line, al->path)) { |
1652 | color_fprintf(stdout, color, " // %s" , al->path); |
1653 | prev_line = al->path; |
1654 | } |
1655 | } |
1656 | |
1657 | printf("\n" ); |
1658 | } else if (max_lines && printed >= max_lines) |
1659 | return 1; |
1660 | else { |
1661 | int width = symbol_conf.show_total_period ? 12 : 8; |
1662 | |
1663 | if (queue) |
1664 | return -1; |
1665 | |
1666 | if (evsel__is_group_event(evsel)) |
1667 | width *= evsel->core.nr_members; |
1668 | |
1669 | if (!*al->line) |
1670 | printf(" %*s:\n" , width, " " ); |
1671 | else |
1672 | printf(" %*s: %-*d %s\n" , width, " " , addr_fmt_width, al->line_nr, al->line); |
1673 | } |
1674 | |
1675 | return 0; |
1676 | } |
1677 | |
1678 | /* |
1679 | * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) |
1680 | * which looks like following |
1681 | * |
1682 | * 0000000000415500 <_init>: |
1683 | * 415500: sub $0x8,%rsp |
1684 | * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> |
1685 | * 41550b: test %rax,%rax |
1686 | * 41550e: je 415515 <_init+0x15> |
1687 | * 415510: callq 416e70 <__gmon_start__@plt> |
1688 | * 415515: add $0x8,%rsp |
1689 | * 415519: retq |
1690 | * |
1691 | * it will be parsed and saved into struct disasm_line as |
1692 | * <offset> <name> <ops.raw> |
1693 | * |
1694 | * The offset will be a relative offset from the start of the symbol and -1 |
1695 | * means that it's not a disassembly line so should be treated differently. |
1696 | * The ops.raw part will be parsed further according to type of the instruction. |
1697 | */ |
1698 | static int symbol__parse_objdump_line(struct symbol *sym, |
1699 | struct annotate_args *args, |
1700 | char *parsed_line, int *line_nr, char **fileloc) |
1701 | { |
1702 | struct map *map = args->ms.map; |
1703 | struct annotation *notes = symbol__annotation(sym); |
1704 | struct disasm_line *dl; |
1705 | char *tmp; |
1706 | s64 line_ip, offset = -1; |
1707 | regmatch_t match[2]; |
1708 | |
1709 | /* /filename:linenr ? Save line number and ignore. */ |
1710 | if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { |
1711 | *line_nr = atoi(parsed_line + match[1].rm_so); |
1712 | free(*fileloc); |
1713 | *fileloc = strdup(parsed_line); |
1714 | return 0; |
1715 | } |
1716 | |
1717 | /* Process hex address followed by ':'. */ |
1718 | line_ip = strtoull(parsed_line, &tmp, 16); |
1719 | if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') { |
1720 | u64 start = map__rip_2objdump(map, rip: sym->start), |
1721 | end = map__rip_2objdump(map, rip: sym->end); |
1722 | |
1723 | offset = line_ip - start; |
1724 | if ((u64)line_ip < start || (u64)line_ip >= end) |
1725 | offset = -1; |
1726 | else |
1727 | parsed_line = tmp + 1; |
1728 | } |
1729 | |
1730 | args->offset = offset; |
1731 | args->line = parsed_line; |
1732 | args->line_nr = *line_nr; |
1733 | args->fileloc = *fileloc; |
1734 | args->ms.sym = sym; |
1735 | |
1736 | dl = disasm_line__new(args); |
1737 | (*line_nr)++; |
1738 | |
1739 | if (dl == NULL) |
1740 | return -1; |
1741 | |
1742 | if (!disasm_line__has_local_offset(dl)) { |
1743 | dl->ops.target.offset = dl->ops.target.addr - |
1744 | map__rip_2objdump(map, rip: sym->start); |
1745 | dl->ops.target.offset_avail = true; |
1746 | } |
1747 | |
1748 | /* kcore has no symbols, so add the call target symbol */ |
1749 | if (dl->ins.ops && ins__is_call(ins: &dl->ins) && !dl->ops.target.sym) { |
1750 | struct addr_map_symbol target = { |
1751 | .addr = dl->ops.target.addr, |
1752 | .ms = { .map = map, }, |
1753 | }; |
1754 | |
1755 | if (!maps__find_ams(maps: args->ms.maps, ams: &target) && |
1756 | target.ms.sym->start == target.al_addr) |
1757 | dl->ops.target.sym = target.ms.sym; |
1758 | } |
1759 | |
1760 | annotation_line__add(al: &dl->al, head: ¬es->src->source); |
1761 | return 0; |
1762 | } |
1763 | |
1764 | static __attribute__((constructor)) void symbol__init_regexpr(void) |
1765 | { |
1766 | regcomp(&file_lineno, "^/[^:]+:([0-9]+)" , REG_EXTENDED); |
1767 | } |
1768 | |
1769 | static void delete_last_nop(struct symbol *sym) |
1770 | { |
1771 | struct annotation *notes = symbol__annotation(sym); |
1772 | struct list_head *list = ¬es->src->source; |
1773 | struct disasm_line *dl; |
1774 | |
1775 | while (!list_empty(head: list)) { |
1776 | dl = list_entry(list->prev, struct disasm_line, al.node); |
1777 | |
1778 | if (dl->ins.ops) { |
1779 | if (dl->ins.ops != &nop_ops) |
1780 | return; |
1781 | } else { |
1782 | if (!strstr(dl->al.line, " nop " ) && |
1783 | !strstr(dl->al.line, " nopl " ) && |
1784 | !strstr(dl->al.line, " nopw " )) |
1785 | return; |
1786 | } |
1787 | |
1788 | list_del_init(entry: &dl->al.node); |
1789 | disasm_line__free(dl); |
1790 | } |
1791 | } |
1792 | |
1793 | int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen) |
1794 | { |
1795 | struct dso *dso = map__dso(map: ms->map); |
1796 | |
1797 | BUG_ON(buflen == 0); |
1798 | |
1799 | if (errnum >= 0) { |
1800 | str_error_r(errnum, buf, buflen); |
1801 | return 0; |
1802 | } |
1803 | |
1804 | switch (errnum) { |
1805 | case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { |
1806 | char bf[SBUILD_ID_SIZE + 15] = " with build id " ; |
1807 | char *build_id_msg = NULL; |
1808 | |
1809 | if (dso->has_build_id) { |
1810 | build_id__sprintf(build_id: &dso->bid, bf: bf + 15); |
1811 | build_id_msg = bf; |
1812 | } |
1813 | scnprintf(buf, size: buflen, |
1814 | fmt: "No vmlinux file%s\nwas found in the path.\n\n" |
1815 | "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" |
1816 | "Please use:\n\n" |
1817 | " perf buildid-cache -vu vmlinux\n\n" |
1818 | "or:\n\n" |
1819 | " --vmlinux vmlinux\n" , build_id_msg ?: "" ); |
1820 | } |
1821 | break; |
1822 | case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: |
1823 | scnprintf(buf, size: buflen, fmt: "Please link with binutils's libopcode to enable BPF annotation" ); |
1824 | break; |
1825 | case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: |
1826 | scnprintf(buf, size: buflen, fmt: "Problems with arch specific instruction name regular expressions." ); |
1827 | break; |
1828 | case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING: |
1829 | scnprintf(buf, size: buflen, fmt: "Problems while parsing the CPUID in the arch specific initialization." ); |
1830 | break; |
1831 | case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE: |
1832 | scnprintf(buf, size: buflen, fmt: "Invalid BPF file: %s." , dso->long_name); |
1833 | break; |
1834 | case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF: |
1835 | scnprintf(buf, size: buflen, fmt: "The %s BPF file has no BTF section, compile with -g or use pahole -J." , |
1836 | dso->long_name); |
1837 | break; |
1838 | default: |
1839 | scnprintf(buf, size: buflen, fmt: "Internal error: Invalid %d error code\n" , errnum); |
1840 | break; |
1841 | } |
1842 | |
1843 | return 0; |
1844 | } |
1845 | |
1846 | static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) |
1847 | { |
1848 | char linkname[PATH_MAX]; |
1849 | char *build_id_filename; |
1850 | char *build_id_path = NULL; |
1851 | char *pos; |
1852 | int len; |
1853 | |
1854 | if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && |
1855 | !dso__is_kcore(dso)) |
1856 | return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; |
1857 | |
1858 | build_id_filename = dso__build_id_filename(dso, NULL, size: 0, is_debug: false); |
1859 | if (build_id_filename) { |
1860 | __symbol__join_symfs(bf: filename, size: filename_size, path: build_id_filename); |
1861 | free(build_id_filename); |
1862 | } else { |
1863 | if (dso->has_build_id) |
1864 | return ENOMEM; |
1865 | goto fallback; |
1866 | } |
1867 | |
1868 | build_id_path = strdup(filename); |
1869 | if (!build_id_path) |
1870 | return ENOMEM; |
1871 | |
1872 | /* |
1873 | * old style build-id cache has name of XX/XXXXXXX.. while |
1874 | * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. |
1875 | * extract the build-id part of dirname in the new style only. |
1876 | */ |
1877 | pos = strrchr(build_id_path, '/'); |
1878 | if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) |
1879 | dirname(build_id_path); |
1880 | |
1881 | if (dso__is_kcore(dso)) |
1882 | goto fallback; |
1883 | |
1884 | len = readlink(build_id_path, linkname, sizeof(linkname) - 1); |
1885 | if (len < 0) |
1886 | goto fallback; |
1887 | |
1888 | linkname[len] = '\0'; |
1889 | if (strstr(linkname, DSO__NAME_KALLSYMS) || |
1890 | access(filename, R_OK)) { |
1891 | fallback: |
1892 | /* |
1893 | * If we don't have build-ids or the build-id file isn't in the |
1894 | * cache, or is just a kallsyms file, well, lets hope that this |
1895 | * DSO is the same as when 'perf record' ran. |
1896 | */ |
1897 | if (dso->kernel && dso->long_name[0] == '/') |
1898 | snprintf(buf: filename, size: filename_size, fmt: "%s" , dso->long_name); |
1899 | else |
1900 | __symbol__join_symfs(bf: filename, size: filename_size, path: dso->long_name); |
1901 | |
1902 | mutex_lock(&dso->lock); |
1903 | if (access(filename, R_OK) && errno == ENOENT && dso->nsinfo) { |
1904 | char *new_name = dso__filename_with_chroot(dso, filename); |
1905 | if (new_name) { |
1906 | strlcpy(filename, new_name, filename_size); |
1907 | free(new_name); |
1908 | } |
1909 | } |
1910 | mutex_unlock(mtx: &dso->lock); |
1911 | } |
1912 | |
1913 | free(build_id_path); |
1914 | return 0; |
1915 | } |
1916 | |
1917 | #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) |
1918 | #define PACKAGE "perf" |
1919 | #include <bfd.h> |
1920 | #include <dis-asm.h> |
1921 | #include <bpf/bpf.h> |
1922 | #include <bpf/btf.h> |
1923 | #include <bpf/libbpf.h> |
1924 | #include <linux/btf.h> |
1925 | #include <tools/dis-asm-compat.h> |
1926 | |
1927 | static int symbol__disassemble_bpf(struct symbol *sym, |
1928 | struct annotate_args *args) |
1929 | { |
1930 | struct annotation *notes = symbol__annotation(sym); |
1931 | struct bpf_prog_linfo *prog_linfo = NULL; |
1932 | struct bpf_prog_info_node *info_node; |
1933 | int len = sym->end - sym->start; |
1934 | disassembler_ftype disassemble; |
1935 | struct map *map = args->ms.map; |
1936 | struct perf_bpil *info_linear; |
1937 | struct disassemble_info info; |
1938 | struct dso *dso = map__dso(map); |
1939 | int pc = 0, count, sub_id; |
1940 | struct btf *btf = NULL; |
1941 | char tpath[PATH_MAX]; |
1942 | size_t buf_size; |
1943 | int nr_skip = 0; |
1944 | char *buf; |
1945 | bfd *bfdf; |
1946 | int ret; |
1947 | FILE *s; |
1948 | |
1949 | if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO) |
1950 | return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE; |
1951 | |
1952 | pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n" , __func__, |
1953 | sym->name, sym->start, sym->end - sym->start); |
1954 | |
1955 | memset(tpath, 0, sizeof(tpath)); |
1956 | perf_exe(tpath, sizeof(tpath)); |
1957 | |
1958 | bfdf = bfd_openr(tpath, NULL); |
1959 | if (bfdf == NULL) |
1960 | abort(); |
1961 | |
1962 | if (!bfd_check_format(bfdf, bfd_object)) |
1963 | abort(); |
1964 | |
1965 | s = open_memstream(&buf, &buf_size); |
1966 | if (!s) { |
1967 | ret = errno; |
1968 | goto out; |
1969 | } |
1970 | init_disassemble_info_compat(&info, s, |
1971 | (fprintf_ftype) fprintf, |
1972 | fprintf_styled); |
1973 | info.arch = bfd_get_arch(bfdf); |
1974 | info.mach = bfd_get_mach(bfdf); |
1975 | |
1976 | info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, |
1977 | dso->bpf_prog.id); |
1978 | if (!info_node) { |
1979 | ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF; |
1980 | goto out; |
1981 | } |
1982 | info_linear = info_node->info_linear; |
1983 | sub_id = dso->bpf_prog.sub_id; |
1984 | |
1985 | info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns); |
1986 | info.buffer_length = info_linear->info.jited_prog_len; |
1987 | |
1988 | if (info_linear->info.nr_line_info) |
1989 | prog_linfo = bpf_prog_linfo__new(&info_linear->info); |
1990 | |
1991 | if (info_linear->info.btf_id) { |
1992 | struct btf_node *node; |
1993 | |
1994 | node = perf_env__find_btf(dso->bpf_prog.env, |
1995 | info_linear->info.btf_id); |
1996 | if (node) |
1997 | btf = btf__new((__u8 *)(node->data), |
1998 | node->data_size); |
1999 | } |
2000 | |
2001 | disassemble_init_for_target(&info); |
2002 | |
2003 | #ifdef DISASM_FOUR_ARGS_SIGNATURE |
2004 | disassemble = disassembler(info.arch, |
2005 | bfd_big_endian(bfdf), |
2006 | info.mach, |
2007 | bfdf); |
2008 | #else |
2009 | disassemble = disassembler(bfdf); |
2010 | #endif |
2011 | if (disassemble == NULL) |
2012 | abort(); |
2013 | |
2014 | fflush(s); |
2015 | do { |
2016 | const struct bpf_line_info *linfo = NULL; |
2017 | struct disasm_line *dl; |
2018 | size_t prev_buf_size; |
2019 | const char *srcline; |
2020 | u64 addr; |
2021 | |
2022 | addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id]; |
2023 | count = disassemble(pc, &info); |
2024 | |
2025 | if (prog_linfo) |
2026 | linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo, |
2027 | addr, sub_id, |
2028 | nr_skip); |
2029 | |
2030 | if (linfo && btf) { |
2031 | srcline = btf__name_by_offset(btf, linfo->line_off); |
2032 | nr_skip++; |
2033 | } else |
2034 | srcline = NULL; |
2035 | |
2036 | fprintf(s, "\n" ); |
2037 | prev_buf_size = buf_size; |
2038 | fflush(s); |
2039 | |
2040 | if (!annotate_opts.hide_src_code && srcline) { |
2041 | args->offset = -1; |
2042 | args->line = strdup(srcline); |
2043 | args->line_nr = 0; |
2044 | args->fileloc = NULL; |
2045 | args->ms.sym = sym; |
2046 | dl = disasm_line__new(args); |
2047 | if (dl) { |
2048 | annotation_line__add(&dl->al, |
2049 | ¬es->src->source); |
2050 | } |
2051 | } |
2052 | |
2053 | args->offset = pc; |
2054 | args->line = buf + prev_buf_size; |
2055 | args->line_nr = 0; |
2056 | args->fileloc = NULL; |
2057 | args->ms.sym = sym; |
2058 | dl = disasm_line__new(args); |
2059 | if (dl) |
2060 | annotation_line__add(&dl->al, ¬es->src->source); |
2061 | |
2062 | pc += count; |
2063 | } while (count > 0 && pc < len); |
2064 | |
2065 | ret = 0; |
2066 | out: |
2067 | free(prog_linfo); |
2068 | btf__free(btf); |
2069 | fclose(s); |
2070 | bfd_close(bfdf); |
2071 | return ret; |
2072 | } |
2073 | #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) |
2074 | static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, |
2075 | struct annotate_args *args __maybe_unused) |
2076 | { |
2077 | return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF; |
2078 | } |
2079 | #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT) |
2080 | |
2081 | static int |
2082 | symbol__disassemble_bpf_image(struct symbol *sym, |
2083 | struct annotate_args *args) |
2084 | { |
2085 | struct annotation *notes = symbol__annotation(sym); |
2086 | struct disasm_line *dl; |
2087 | |
2088 | args->offset = -1; |
2089 | args->line = strdup("to be implemented" ); |
2090 | args->line_nr = 0; |
2091 | args->fileloc = NULL; |
2092 | dl = disasm_line__new(args); |
2093 | if (dl) |
2094 | annotation_line__add(al: &dl->al, head: ¬es->src->source); |
2095 | |
2096 | zfree(&args->line); |
2097 | return 0; |
2098 | } |
2099 | |
2100 | /* |
2101 | * Possibly create a new version of line with tabs expanded. Returns the |
2102 | * existing or new line, storage is updated if a new line is allocated. If |
2103 | * allocation fails then NULL is returned. |
2104 | */ |
2105 | static char *expand_tabs(char *line, char **storage, size_t *storage_len) |
2106 | { |
2107 | size_t i, src, dst, len, new_storage_len, num_tabs; |
2108 | char *new_line; |
2109 | size_t line_len = strlen(line); |
2110 | |
2111 | for (num_tabs = 0, i = 0; i < line_len; i++) |
2112 | if (line[i] == '\t') |
2113 | num_tabs++; |
2114 | |
2115 | if (num_tabs == 0) |
2116 | return line; |
2117 | |
2118 | /* |
2119 | * Space for the line and '\0', less the leading and trailing |
2120 | * spaces. Each tab may introduce 7 additional spaces. |
2121 | */ |
2122 | new_storage_len = line_len + 1 + (num_tabs * 7); |
2123 | |
2124 | new_line = malloc(new_storage_len); |
2125 | if (new_line == NULL) { |
2126 | pr_err("Failure allocating memory for tab expansion\n" ); |
2127 | return NULL; |
2128 | } |
2129 | |
2130 | /* |
2131 | * Copy regions starting at src and expand tabs. If there are two |
2132 | * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces |
2133 | * are inserted. |
2134 | */ |
2135 | for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) { |
2136 | if (line[i] == '\t') { |
2137 | len = i - src; |
2138 | memcpy(&new_line[dst], &line[src], len); |
2139 | dst += len; |
2140 | new_line[dst++] = ' '; |
2141 | while (dst % 8 != 0) |
2142 | new_line[dst++] = ' '; |
2143 | src = i + 1; |
2144 | num_tabs--; |
2145 | } |
2146 | } |
2147 | |
2148 | /* Expand the last region. */ |
2149 | len = line_len - src; |
2150 | memcpy(&new_line[dst], &line[src], len); |
2151 | dst += len; |
2152 | new_line[dst] = '\0'; |
2153 | |
2154 | free(*storage); |
2155 | *storage = new_line; |
2156 | *storage_len = new_storage_len; |
2157 | return new_line; |
2158 | |
2159 | } |
2160 | |
2161 | static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) |
2162 | { |
2163 | struct annotation_options *opts = &annotate_opts; |
2164 | struct map *map = args->ms.map; |
2165 | struct dso *dso = map__dso(map); |
2166 | char *command; |
2167 | FILE *file; |
2168 | char symfs_filename[PATH_MAX]; |
2169 | struct kcore_extract kce; |
2170 | bool = false; |
2171 | bool decomp = false; |
2172 | int lineno = 0; |
2173 | char *fileloc = NULL; |
2174 | int nline; |
2175 | char *line; |
2176 | size_t line_len; |
2177 | const char *objdump_argv[] = { |
2178 | "/bin/sh" , |
2179 | "-c" , |
2180 | NULL, /* Will be the objdump command to run. */ |
2181 | "--" , |
2182 | NULL, /* Will be the symfs path. */ |
2183 | NULL, |
2184 | }; |
2185 | struct child_process objdump_process; |
2186 | int err = dso__disassemble_filename(dso, filename: symfs_filename, filename_size: sizeof(symfs_filename)); |
2187 | |
2188 | if (err) |
2189 | return err; |
2190 | |
2191 | pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n" , __func__, |
2192 | symfs_filename, sym->name, map__unmap_ip(map, sym->start), |
2193 | map__unmap_ip(map, sym->end)); |
2194 | |
2195 | pr_debug("annotating [%p] %30s : [%p] %30s\n" , |
2196 | dso, dso->long_name, sym, sym->name); |
2197 | |
2198 | if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) { |
2199 | return symbol__disassemble_bpf(sym, args); |
2200 | } else if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE) { |
2201 | return symbol__disassemble_bpf_image(sym, args); |
2202 | } else if (dso__is_kcore(dso)) { |
2203 | kce.kcore_filename = symfs_filename; |
2204 | kce.addr = map__rip_2objdump(map, rip: sym->start); |
2205 | kce.offs = sym->start; |
2206 | kce.len = sym->end - sym->start; |
2207 | if (!kcore_extract__create(kce: &kce)) { |
2208 | delete_extract = true; |
2209 | strlcpy(symfs_filename, kce.extract_filename, |
2210 | sizeof(symfs_filename)); |
2211 | } |
2212 | } else if (dso__needs_decompress(dso)) { |
2213 | char tmp[KMOD_DECOMP_LEN]; |
2214 | |
2215 | if (dso__decompress_kmodule_path(dso, name: symfs_filename, |
2216 | pathname: tmp, len: sizeof(tmp)) < 0) |
2217 | return -1; |
2218 | |
2219 | decomp = true; |
2220 | strcpy(p: symfs_filename, q: tmp); |
2221 | } |
2222 | |
2223 | err = asprintf(&command, |
2224 | "%s %s%s --start-address=0x%016" PRIx64 |
2225 | " --stop-address=0x%016" PRIx64 |
2226 | " %s -d %s %s %s %c%s%c %s%s -C \"$1\"" , |
2227 | opts->objdump_path ?: "objdump" , |
2228 | opts->disassembler_style ? "-M " : "" , |
2229 | opts->disassembler_style ?: "" , |
2230 | map__rip_2objdump(map, sym->start), |
2231 | map__rip_2objdump(map, sym->end), |
2232 | opts->show_linenr ? "-l" : "" , |
2233 | opts->show_asm_raw ? "" : "--no-show-raw-insn" , |
2234 | opts->annotate_src ? "-S" : "" , |
2235 | opts->prefix ? "--prefix " : "" , |
2236 | opts->prefix ? '"' : ' ', |
2237 | opts->prefix ?: "" , |
2238 | opts->prefix ? '"' : ' ', |
2239 | opts->prefix_strip ? "--prefix-strip=" : "" , |
2240 | opts->prefix_strip ?: "" ); |
2241 | |
2242 | if (err < 0) { |
2243 | pr_err("Failure allocating memory for the command to run\n" ); |
2244 | goto out_remove_tmp; |
2245 | } |
2246 | |
2247 | pr_debug("Executing: %s\n" , command); |
2248 | |
2249 | objdump_argv[2] = command; |
2250 | objdump_argv[4] = symfs_filename; |
2251 | |
2252 | /* Create a pipe to read from for stdout */ |
2253 | memset(&objdump_process, 0, sizeof(objdump_process)); |
2254 | objdump_process.argv = objdump_argv; |
2255 | objdump_process.out = -1; |
2256 | objdump_process.err = -1; |
2257 | objdump_process.no_stderr = 1; |
2258 | if (start_command(&objdump_process)) { |
2259 | pr_err("Failure starting to run %s\n" , command); |
2260 | err = -1; |
2261 | goto out_free_command; |
2262 | } |
2263 | |
2264 | file = fdopen(objdump_process.out, "r" ); |
2265 | if (!file) { |
2266 | pr_err("Failure creating FILE stream for %s\n" , command); |
2267 | /* |
2268 | * If we were using debug info should retry with |
2269 | * original binary. |
2270 | */ |
2271 | err = -1; |
2272 | goto out_close_stdout; |
2273 | } |
2274 | |
2275 | /* Storage for getline. */ |
2276 | line = NULL; |
2277 | line_len = 0; |
2278 | |
2279 | nline = 0; |
2280 | while (!feof(file)) { |
2281 | const char *match; |
2282 | char *expanded_line; |
2283 | |
2284 | if (getline(&line, &line_len, file) < 0 || !line) |
2285 | break; |
2286 | |
2287 | /* Skip lines containing "filename:" */ |
2288 | match = strstr(line, symfs_filename); |
2289 | if (match && match[strlen(symfs_filename)] == ':') |
2290 | continue; |
2291 | |
2292 | expanded_line = strim(line); |
2293 | expanded_line = expand_tabs(line: expanded_line, storage: &line, storage_len: &line_len); |
2294 | if (!expanded_line) |
2295 | break; |
2296 | |
2297 | /* |
2298 | * The source code line number (lineno) needs to be kept in |
2299 | * across calls to symbol__parse_objdump_line(), so that it |
2300 | * can associate it with the instructions till the next one. |
2301 | * See disasm_line__new() and struct disasm_line::line_nr. |
2302 | */ |
2303 | if (symbol__parse_objdump_line(sym, args, parsed_line: expanded_line, |
2304 | line_nr: &lineno, fileloc: &fileloc) < 0) |
2305 | break; |
2306 | nline++; |
2307 | } |
2308 | free(line); |
2309 | free(fileloc); |
2310 | |
2311 | err = finish_command(&objdump_process); |
2312 | if (err) |
2313 | pr_err("Error running %s\n" , command); |
2314 | |
2315 | if (nline == 0) { |
2316 | err = -1; |
2317 | pr_err("No output from %s\n" , command); |
2318 | } |
2319 | |
2320 | /* |
2321 | * kallsyms does not have symbol sizes so there may a nop at the end. |
2322 | * Remove it. |
2323 | */ |
2324 | if (dso__is_kcore(dso)) |
2325 | delete_last_nop(sym); |
2326 | |
2327 | fclose(file); |
2328 | |
2329 | out_close_stdout: |
2330 | close(objdump_process.out); |
2331 | |
2332 | out_free_command: |
2333 | free(command); |
2334 | |
2335 | out_remove_tmp: |
2336 | if (decomp) |
2337 | unlink(symfs_filename); |
2338 | |
2339 | if (delete_extract) |
2340 | kcore_extract__delete(kce: &kce); |
2341 | |
2342 | return err; |
2343 | } |
2344 | |
2345 | static void calc_percent(struct annotation *notes, |
2346 | struct evsel *evsel, |
2347 | struct annotation_data *data, |
2348 | s64 offset, s64 end) |
2349 | { |
2350 | struct hists *hists = evsel__hists(evsel); |
2351 | int evidx = evsel->core.idx; |
2352 | struct sym_hist *sym_hist = annotation__histogram(notes, idx: evidx); |
2353 | unsigned int hits = 0; |
2354 | u64 period = 0; |
2355 | |
2356 | while (offset < end) { |
2357 | struct sym_hist_entry *entry; |
2358 | |
2359 | entry = annotated_source__hist_entry(src: notes->src, idx: evidx, offset); |
2360 | if (entry) { |
2361 | hits += entry->nr_samples; |
2362 | period += entry->period; |
2363 | } |
2364 | ++offset; |
2365 | } |
2366 | |
2367 | if (sym_hist->nr_samples) { |
2368 | data->he.period = period; |
2369 | data->he.nr_samples = hits; |
2370 | data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples; |
2371 | } |
2372 | |
2373 | if (hists->stats.nr_non_filtered_samples) |
2374 | data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples; |
2375 | |
2376 | if (sym_hist->period) |
2377 | data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period; |
2378 | |
2379 | if (hists->stats.total_period) |
2380 | data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period; |
2381 | } |
2382 | |
2383 | static void annotation__calc_percent(struct annotation *notes, |
2384 | struct evsel *leader, s64 len) |
2385 | { |
2386 | struct annotation_line *al, *next; |
2387 | struct evsel *evsel; |
2388 | |
2389 | list_for_each_entry(al, ¬es->src->source, node) { |
2390 | s64 end; |
2391 | int i = 0; |
2392 | |
2393 | if (al->offset == -1) |
2394 | continue; |
2395 | |
2396 | next = annotation_line__next(pos: al, head: ¬es->src->source); |
2397 | end = next ? next->offset : len; |
2398 | |
2399 | for_each_group_evsel(evsel, leader) { |
2400 | struct annotation_data *data; |
2401 | |
2402 | BUG_ON(i >= al->data_nr); |
2403 | |
2404 | data = &al->data[i++]; |
2405 | |
2406 | calc_percent(notes, evsel, data, offset: al->offset, end); |
2407 | } |
2408 | } |
2409 | } |
2410 | |
2411 | void symbol__calc_percent(struct symbol *sym, struct evsel *evsel) |
2412 | { |
2413 | struct annotation *notes = symbol__annotation(sym); |
2414 | |
2415 | annotation__calc_percent(notes, leader: evsel, len: symbol__size(sym)); |
2416 | } |
2417 | |
2418 | static int evsel__get_arch(struct evsel *evsel, struct arch **parch) |
2419 | { |
2420 | struct perf_env *env = evsel__env(evsel); |
2421 | const char *arch_name = perf_env__arch(env); |
2422 | struct arch *arch; |
2423 | int err; |
2424 | |
2425 | if (!arch_name) |
2426 | return errno; |
2427 | |
2428 | *parch = arch = arch__find(name: arch_name); |
2429 | if (arch == NULL) { |
2430 | pr_err("%s: unsupported arch %s\n" , __func__, arch_name); |
2431 | return ENOTSUP; |
2432 | } |
2433 | |
2434 | if (arch->init) { |
2435 | err = arch->init(arch, env ? env->cpuid : NULL); |
2436 | if (err) { |
2437 | pr_err("%s: failed to initialize %s arch priv area\n" , |
2438 | __func__, arch->name); |
2439 | return err; |
2440 | } |
2441 | } |
2442 | return 0; |
2443 | } |
2444 | |
2445 | int symbol__annotate(struct map_symbol *ms, struct evsel *evsel, |
2446 | struct arch **parch) |
2447 | { |
2448 | struct symbol *sym = ms->sym; |
2449 | struct annotation *notes = symbol__annotation(sym); |
2450 | struct annotate_args args = { |
2451 | .evsel = evsel, |
2452 | .options = &annotate_opts, |
2453 | }; |
2454 | struct arch *arch = NULL; |
2455 | int err; |
2456 | |
2457 | err = evsel__get_arch(evsel, parch: &arch); |
2458 | if (err < 0) |
2459 | return err; |
2460 | |
2461 | if (parch) |
2462 | *parch = arch; |
2463 | |
2464 | if (!list_empty(head: ¬es->src->source)) |
2465 | return 0; |
2466 | |
2467 | args.arch = arch; |
2468 | args.ms = *ms; |
2469 | if (annotate_opts.full_addr) |
2470 | notes->start = map__objdump_2mem(map: ms->map, ip: ms->sym->start); |
2471 | else |
2472 | notes->start = map__rip_2objdump(map: ms->map, rip: ms->sym->start); |
2473 | |
2474 | return symbol__disassemble(sym, args: &args); |
2475 | } |
2476 | |
2477 | static void insert_source_line(struct rb_root *root, struct annotation_line *al) |
2478 | { |
2479 | struct annotation_line *iter; |
2480 | struct rb_node **p = &root->rb_node; |
2481 | struct rb_node *parent = NULL; |
2482 | unsigned int percent_type = annotate_opts.percent_type; |
2483 | int i, ret; |
2484 | |
2485 | while (*p != NULL) { |
2486 | parent = *p; |
2487 | iter = rb_entry(parent, struct annotation_line, rb_node); |
2488 | |
2489 | ret = strcmp(iter->path, al->path); |
2490 | if (ret == 0) { |
2491 | for (i = 0; i < al->data_nr; i++) { |
2492 | iter->data[i].percent_sum += annotation_data__percent(data: &al->data[i], |
2493 | which: percent_type); |
2494 | } |
2495 | return; |
2496 | } |
2497 | |
2498 | if (ret < 0) |
2499 | p = &(*p)->rb_left; |
2500 | else |
2501 | p = &(*p)->rb_right; |
2502 | } |
2503 | |
2504 | for (i = 0; i < al->data_nr; i++) { |
2505 | al->data[i].percent_sum = annotation_data__percent(data: &al->data[i], |
2506 | which: percent_type); |
2507 | } |
2508 | |
2509 | rb_link_node(node: &al->rb_node, parent, rb_link: p); |
2510 | rb_insert_color(&al->rb_node, root); |
2511 | } |
2512 | |
2513 | static int cmp_source_line(struct annotation_line *a, struct annotation_line *b) |
2514 | { |
2515 | int i; |
2516 | |
2517 | for (i = 0; i < a->data_nr; i++) { |
2518 | if (a->data[i].percent_sum == b->data[i].percent_sum) |
2519 | continue; |
2520 | return a->data[i].percent_sum > b->data[i].percent_sum; |
2521 | } |
2522 | |
2523 | return 0; |
2524 | } |
2525 | |
2526 | static void __resort_source_line(struct rb_root *root, struct annotation_line *al) |
2527 | { |
2528 | struct annotation_line *iter; |
2529 | struct rb_node **p = &root->rb_node; |
2530 | struct rb_node *parent = NULL; |
2531 | |
2532 | while (*p != NULL) { |
2533 | parent = *p; |
2534 | iter = rb_entry(parent, struct annotation_line, rb_node); |
2535 | |
2536 | if (cmp_source_line(a: al, b: iter)) |
2537 | p = &(*p)->rb_left; |
2538 | else |
2539 | p = &(*p)->rb_right; |
2540 | } |
2541 | |
2542 | rb_link_node(node: &al->rb_node, parent, rb_link: p); |
2543 | rb_insert_color(&al->rb_node, root); |
2544 | } |
2545 | |
2546 | static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) |
2547 | { |
2548 | struct annotation_line *al; |
2549 | struct rb_node *node; |
2550 | |
2551 | node = rb_first(src_root); |
2552 | while (node) { |
2553 | struct rb_node *next; |
2554 | |
2555 | al = rb_entry(node, struct annotation_line, rb_node); |
2556 | next = rb_next(node); |
2557 | rb_erase(node, src_root); |
2558 | |
2559 | __resort_source_line(root: dest_root, al); |
2560 | node = next; |
2561 | } |
2562 | } |
2563 | |
2564 | static void print_summary(struct rb_root *root, const char *filename) |
2565 | { |
2566 | struct annotation_line *al; |
2567 | struct rb_node *node; |
2568 | |
2569 | printf("\nSorted summary for file %s\n" , filename); |
2570 | printf("----------------------------------------------\n\n" ); |
2571 | |
2572 | if (RB_EMPTY_ROOT(root)) { |
2573 | printf(" Nothing higher than %1.1f%%\n" , MIN_GREEN); |
2574 | return; |
2575 | } |
2576 | |
2577 | node = rb_first(root); |
2578 | while (node) { |
2579 | double percent, percent_max = 0.0; |
2580 | const char *color; |
2581 | char *path; |
2582 | int i; |
2583 | |
2584 | al = rb_entry(node, struct annotation_line, rb_node); |
2585 | for (i = 0; i < al->data_nr; i++) { |
2586 | percent = al->data[i].percent_sum; |
2587 | color = get_percent_color(percent); |
2588 | color_fprintf(stdout, color, " %7.2f" , percent); |
2589 | |
2590 | if (percent > percent_max) |
2591 | percent_max = percent; |
2592 | } |
2593 | |
2594 | path = al->path; |
2595 | color = get_percent_color(percent: percent_max); |
2596 | color_fprintf(stdout, color, " %s\n" , path); |
2597 | |
2598 | node = rb_next(node); |
2599 | } |
2600 | } |
2601 | |
2602 | static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel) |
2603 | { |
2604 | int evidx = evsel->core.idx; |
2605 | struct annotation *notes = symbol__annotation(sym); |
2606 | struct sym_hist *h = annotation__histogram(notes, idx: evidx); |
2607 | u64 len = symbol__size(sym), offset; |
2608 | |
2609 | for (offset = 0; offset < len; ++offset) { |
2610 | struct sym_hist_entry *entry; |
2611 | |
2612 | entry = annotated_source__hist_entry(src: notes->src, idx: evidx, offset); |
2613 | if (entry && entry->nr_samples != 0) |
2614 | printf("%*" PRIx64 ": %" PRIu64 "\n" , BITS_PER_LONG / 2, |
2615 | sym->start + offset, entry->nr_samples); |
2616 | } |
2617 | printf("%*s: %" PRIu64 "\n" , BITS_PER_LONG / 2, "h->nr_samples" , h->nr_samples); |
2618 | } |
2619 | |
2620 | static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) |
2621 | { |
2622 | char bf[32]; |
2623 | struct annotation_line *line; |
2624 | |
2625 | list_for_each_entry_reverse(line, lines, node) { |
2626 | if (line->offset != -1) |
2627 | return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); |
2628 | } |
2629 | |
2630 | return 0; |
2631 | } |
2632 | |
2633 | int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel) |
2634 | { |
2635 | struct map *map = ms->map; |
2636 | struct symbol *sym = ms->sym; |
2637 | struct dso *dso = map__dso(map); |
2638 | char *filename; |
2639 | const char *d_filename; |
2640 | const char *evsel_name = evsel__name(evsel); |
2641 | struct annotation *notes = symbol__annotation(sym); |
2642 | struct sym_hist *h = annotation__histogram(notes, idx: evsel->core.idx); |
2643 | struct annotation_line *pos, *queue = NULL; |
2644 | struct annotation_options *opts = &annotate_opts; |
2645 | u64 start = map__rip_2objdump(map, rip: sym->start); |
2646 | int printed = 2, queue_len = 0, addr_fmt_width; |
2647 | int more = 0; |
2648 | bool context = opts->context; |
2649 | u64 len; |
2650 | int width = symbol_conf.show_total_period ? 12 : 8; |
2651 | int graph_dotted_len; |
2652 | char buf[512]; |
2653 | |
2654 | filename = strdup(dso->long_name); |
2655 | if (!filename) |
2656 | return -ENOMEM; |
2657 | |
2658 | if (opts->full_path) |
2659 | d_filename = filename; |
2660 | else |
2661 | d_filename = basename(filename); |
2662 | |
2663 | len = symbol__size(sym); |
2664 | |
2665 | if (evsel__is_group_event(evsel)) { |
2666 | width *= evsel->core.nr_members; |
2667 | evsel__group_desc(evsel, buf, size: sizeof(buf)); |
2668 | evsel_name = buf; |
2669 | } |
2670 | |
2671 | graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, " |
2672 | "percent: %s)\n" , |
2673 | width, width, symbol_conf.show_total_period ? "Period" : |
2674 | symbol_conf.show_nr_samples ? "Samples" : "Percent" , |
2675 | d_filename, evsel_name, h->nr_samples, |
2676 | percent_type_str(opts->percent_type)); |
2677 | |
2678 | printf("%-*.*s----\n" , |
2679 | graph_dotted_len, graph_dotted_len, graph_dotted_line); |
2680 | |
2681 | if (verbose > 0) |
2682 | symbol__annotate_hits(sym, evsel); |
2683 | |
2684 | addr_fmt_width = annotated_source__addr_fmt_width(lines: ¬es->src->source, start); |
2685 | |
2686 | list_for_each_entry(pos, ¬es->src->source, node) { |
2687 | int err; |
2688 | |
2689 | if (context && queue == NULL) { |
2690 | queue = pos; |
2691 | queue_len = 0; |
2692 | } |
2693 | |
2694 | err = annotation_line__print(al: pos, sym, start, evsel, len, |
2695 | min_pcnt: opts->min_pcnt, printed, max_lines: opts->max_lines, |
2696 | queue, addr_fmt_width, percent_type: opts->percent_type); |
2697 | |
2698 | switch (err) { |
2699 | case 0: |
2700 | ++printed; |
2701 | if (context) { |
2702 | printed += queue_len; |
2703 | queue = NULL; |
2704 | queue_len = 0; |
2705 | } |
2706 | break; |
2707 | case 1: |
2708 | /* filtered by max_lines */ |
2709 | ++more; |
2710 | break; |
2711 | case -1: |
2712 | default: |
2713 | /* |
2714 | * Filtered by min_pcnt or non IP lines when |
2715 | * context != 0 |
2716 | */ |
2717 | if (!context) |
2718 | break; |
2719 | if (queue_len == context) |
2720 | queue = list_entry(queue->node.next, typeof(*queue), node); |
2721 | else |
2722 | ++queue_len; |
2723 | break; |
2724 | } |
2725 | } |
2726 | |
2727 | free(filename); |
2728 | |
2729 | return more; |
2730 | } |
2731 | |
2732 | static void FILE__set_percent_color(void *fp __maybe_unused, |
2733 | double percent __maybe_unused, |
2734 | bool current __maybe_unused) |
2735 | { |
2736 | } |
2737 | |
2738 | static int FILE__set_jumps_percent_color(void *fp __maybe_unused, |
2739 | int nr __maybe_unused, bool current __maybe_unused) |
2740 | { |
2741 | return 0; |
2742 | } |
2743 | |
2744 | static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) |
2745 | { |
2746 | return 0; |
2747 | } |
2748 | |
2749 | static void FILE__printf(void *fp, const char *fmt, ...) |
2750 | { |
2751 | va_list args; |
2752 | |
2753 | va_start(args, fmt); |
2754 | vfprintf(fp, fmt, args); |
2755 | va_end(args); |
2756 | } |
2757 | |
2758 | static void FILE__write_graph(void *fp, int graph) |
2759 | { |
2760 | const char *s; |
2761 | switch (graph) { |
2762 | |
2763 | case DARROW_CHAR: s = "↓" ; break; |
2764 | case UARROW_CHAR: s = "↑" ; break; |
2765 | case LARROW_CHAR: s = "←" ; break; |
2766 | case RARROW_CHAR: s = "→" ; break; |
2767 | default: s = "?" ; break; |
2768 | } |
2769 | |
2770 | fputs(s, fp); |
2771 | } |
2772 | |
2773 | static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp) |
2774 | { |
2775 | struct annotation *notes = symbol__annotation(sym); |
2776 | struct annotation_write_ops wops = { |
2777 | .first_line = true, |
2778 | .obj = fp, |
2779 | .set_color = FILE__set_color, |
2780 | .set_percent_color = FILE__set_percent_color, |
2781 | .set_jumps_percent_color = FILE__set_jumps_percent_color, |
2782 | .printf = FILE__printf, |
2783 | .write_graph = FILE__write_graph, |
2784 | }; |
2785 | struct annotation_line *al; |
2786 | |
2787 | list_for_each_entry(al, ¬es->src->source, node) { |
2788 | if (annotation_line__filter(al)) |
2789 | continue; |
2790 | annotation_line__write(al, notes, ops: &wops); |
2791 | fputc('\n', fp); |
2792 | wops.first_line = false; |
2793 | } |
2794 | |
2795 | return 0; |
2796 | } |
2797 | |
2798 | int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel) |
2799 | { |
2800 | const char *ev_name = evsel__name(evsel); |
2801 | char buf[1024]; |
2802 | char *filename; |
2803 | int err = -1; |
2804 | FILE *fp; |
2805 | |
2806 | if (asprintf(&filename, "%s.annotation" , ms->sym->name) < 0) |
2807 | return -1; |
2808 | |
2809 | fp = fopen(filename, "w" ); |
2810 | if (fp == NULL) |
2811 | goto out_free_filename; |
2812 | |
2813 | if (evsel__is_group_event(evsel)) { |
2814 | evsel__group_desc(evsel, buf, size: sizeof(buf)); |
2815 | ev_name = buf; |
2816 | } |
2817 | |
2818 | fprintf(fp, "%s() %s\nEvent: %s\n\n" , |
2819 | ms->sym->name, map__dso(ms->map)->long_name, ev_name); |
2820 | symbol__annotate_fprintf2(ms->sym, fp); |
2821 | |
2822 | fclose(fp); |
2823 | err = 0; |
2824 | out_free_filename: |
2825 | free(filename); |
2826 | return err; |
2827 | } |
2828 | |
2829 | void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) |
2830 | { |
2831 | struct annotation *notes = symbol__annotation(sym); |
2832 | struct sym_hist *h = annotation__histogram(notes, idx: evidx); |
2833 | |
2834 | memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms); |
2835 | } |
2836 | |
2837 | void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) |
2838 | { |
2839 | struct annotation *notes = symbol__annotation(sym); |
2840 | struct sym_hist *h = annotation__histogram(notes, idx: evidx); |
2841 | int len = symbol__size(sym), offset; |
2842 | |
2843 | h->nr_samples = 0; |
2844 | for (offset = 0; offset < len; ++offset) { |
2845 | struct sym_hist_entry *entry; |
2846 | |
2847 | entry = annotated_source__hist_entry(src: notes->src, idx: evidx, offset); |
2848 | if (entry == NULL) |
2849 | continue; |
2850 | |
2851 | entry->nr_samples = entry->nr_samples * 7 / 8; |
2852 | h->nr_samples += entry->nr_samples; |
2853 | } |
2854 | } |
2855 | |
2856 | void annotated_source__purge(struct annotated_source *as) |
2857 | { |
2858 | struct annotation_line *al, *n; |
2859 | |
2860 | list_for_each_entry_safe(al, n, &as->source, node) { |
2861 | list_del_init(entry: &al->node); |
2862 | disasm_line__free(dl: disasm_line(al)); |
2863 | } |
2864 | } |
2865 | |
2866 | static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) |
2867 | { |
2868 | size_t printed; |
2869 | |
2870 | if (dl->al.offset == -1) |
2871 | return fprintf(fp, "%s\n" , dl->al.line); |
2872 | |
2873 | printed = fprintf(fp, "%#" PRIx64 " %s" , dl->al.offset, dl->ins.name); |
2874 | |
2875 | if (dl->ops.raw[0] != '\0') { |
2876 | printed += fprintf(fp, "%.*s %s\n" , 6 - (int)printed, " " , |
2877 | dl->ops.raw); |
2878 | } |
2879 | |
2880 | return printed + fprintf(fp, "\n" ); |
2881 | } |
2882 | |
2883 | size_t disasm__fprintf(struct list_head *head, FILE *fp) |
2884 | { |
2885 | struct disasm_line *pos; |
2886 | size_t printed = 0; |
2887 | |
2888 | list_for_each_entry(pos, head, al.node) |
2889 | printed += disasm_line__fprintf(pos, fp); |
2890 | |
2891 | return printed; |
2892 | } |
2893 | |
2894 | bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) |
2895 | { |
2896 | if (!dl || !dl->ins.ops || !ins__is_jump(ins: &dl->ins) || |
2897 | !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || |
2898 | dl->ops.target.offset >= (s64)symbol__size(sym)) |
2899 | return false; |
2900 | |
2901 | return true; |
2902 | } |
2903 | |
2904 | void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) |
2905 | { |
2906 | u64 offset, size = symbol__size(sym); |
2907 | |
2908 | /* PLT symbols contain external offsets */ |
2909 | if (strstr(sym->name, "@plt" )) |
2910 | return; |
2911 | |
2912 | for (offset = 0; offset < size; ++offset) { |
2913 | struct annotation_line *al = notes->src->offsets[offset]; |
2914 | struct disasm_line *dl; |
2915 | |
2916 | dl = disasm_line(al); |
2917 | |
2918 | if (!disasm_line__is_valid_local_jump(dl, sym)) |
2919 | continue; |
2920 | |
2921 | al = notes->src->offsets[dl->ops.target.offset]; |
2922 | |
2923 | /* |
2924 | * FIXME: Oops, no jump target? Buggy disassembler? Or do we |
2925 | * have to adjust to the previous offset? |
2926 | */ |
2927 | if (al == NULL) |
2928 | continue; |
2929 | |
2930 | if (++al->jump_sources > notes->max_jump_sources) |
2931 | notes->max_jump_sources = al->jump_sources; |
2932 | } |
2933 | } |
2934 | |
2935 | void annotation__set_offsets(struct annotation *notes, s64 size) |
2936 | { |
2937 | struct annotation_line *al; |
2938 | struct annotated_source *src = notes->src; |
2939 | |
2940 | src->max_line_len = 0; |
2941 | src->nr_entries = 0; |
2942 | src->nr_asm_entries = 0; |
2943 | |
2944 | list_for_each_entry(al, &src->source, node) { |
2945 | size_t line_len = strlen(al->line); |
2946 | |
2947 | if (src->max_line_len < line_len) |
2948 | src->max_line_len = line_len; |
2949 | al->idx = src->nr_entries++; |
2950 | if (al->offset != -1) { |
2951 | al->idx_asm = src->nr_asm_entries++; |
2952 | /* |
2953 | * FIXME: short term bandaid to cope with assembly |
2954 | * routines that comes with labels in the same column |
2955 | * as the address in objdump, sigh. |
2956 | * |
2957 | * E.g. copy_user_generic_unrolled |
2958 | */ |
2959 | if (al->offset < size) |
2960 | notes->src->offsets[al->offset] = al; |
2961 | } else |
2962 | al->idx_asm = -1; |
2963 | } |
2964 | } |
2965 | |
2966 | static inline int width_jumps(int n) |
2967 | { |
2968 | if (n >= 100) |
2969 | return 5; |
2970 | if (n / 10) |
2971 | return 2; |
2972 | return 1; |
2973 | } |
2974 | |
2975 | static int annotation__max_ins_name(struct annotation *notes) |
2976 | { |
2977 | int max_name = 0, len; |
2978 | struct annotation_line *al; |
2979 | |
2980 | list_for_each_entry(al, ¬es->src->source, node) { |
2981 | if (al->offset == -1) |
2982 | continue; |
2983 | |
2984 | len = strlen(disasm_line(al)->ins.name); |
2985 | if (max_name < len) |
2986 | max_name = len; |
2987 | } |
2988 | |
2989 | return max_name; |
2990 | } |
2991 | |
2992 | void annotation__init_column_widths(struct annotation *notes, struct symbol *sym) |
2993 | { |
2994 | notes->widths.addr = notes->widths.target = |
2995 | notes->widths.min_addr = hex_width(v: symbol__size(sym)); |
2996 | notes->widths.max_addr = hex_width(v: sym->end); |
2997 | notes->widths.jumps = width_jumps(n: notes->max_jump_sources); |
2998 | notes->widths.max_ins_name = annotation__max_ins_name(notes); |
2999 | } |
3000 | |
3001 | void annotation__update_column_widths(struct annotation *notes) |
3002 | { |
3003 | if (annotate_opts.use_offset) |
3004 | notes->widths.target = notes->widths.min_addr; |
3005 | else if (annotate_opts.full_addr) |
3006 | notes->widths.target = BITS_PER_LONG / 4; |
3007 | else |
3008 | notes->widths.target = notes->widths.max_addr; |
3009 | |
3010 | notes->widths.addr = notes->widths.target; |
3011 | |
3012 | if (annotate_opts.show_nr_jumps) |
3013 | notes->widths.addr += notes->widths.jumps + 1; |
3014 | } |
3015 | |
3016 | void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms) |
3017 | { |
3018 | annotate_opts.full_addr = !annotate_opts.full_addr; |
3019 | |
3020 | if (annotate_opts.full_addr) |
3021 | notes->start = map__objdump_2mem(map: ms->map, ip: ms->sym->start); |
3022 | else |
3023 | notes->start = map__rip_2objdump(map: ms->map, rip: ms->sym->start); |
3024 | |
3025 | annotation__update_column_widths(notes); |
3026 | } |
3027 | |
3028 | static void annotation__calc_lines(struct annotation *notes, struct map *map, |
3029 | struct rb_root *root) |
3030 | { |
3031 | struct annotation_line *al; |
3032 | struct rb_root tmp_root = RB_ROOT; |
3033 | |
3034 | list_for_each_entry(al, ¬es->src->source, node) { |
3035 | double percent_max = 0.0; |
3036 | int i; |
3037 | |
3038 | for (i = 0; i < al->data_nr; i++) { |
3039 | double percent; |
3040 | |
3041 | percent = annotation_data__percent(data: &al->data[i], |
3042 | which: annotate_opts.percent_type); |
3043 | |
3044 | if (percent > percent_max) |
3045 | percent_max = percent; |
3046 | } |
3047 | |
3048 | if (percent_max <= 0.5) |
3049 | continue; |
3050 | |
3051 | al->path = get_srcline(dso: map__dso(map), addr: notes->start + al->offset, NULL, |
3052 | show_sym: false, show_addr: true, ip: notes->start + al->offset); |
3053 | insert_source_line(root: &tmp_root, al); |
3054 | } |
3055 | |
3056 | resort_source_line(dest_root: root, src_root: &tmp_root); |
3057 | } |
3058 | |
3059 | static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root) |
3060 | { |
3061 | struct annotation *notes = symbol__annotation(sym: ms->sym); |
3062 | |
3063 | annotation__calc_lines(notes, map: ms->map, root); |
3064 | } |
3065 | |
3066 | int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel) |
3067 | { |
3068 | struct dso *dso = map__dso(map: ms->map); |
3069 | struct symbol *sym = ms->sym; |
3070 | struct rb_root source_line = RB_ROOT; |
3071 | struct hists *hists = evsel__hists(evsel); |
3072 | char buf[1024]; |
3073 | int err; |
3074 | |
3075 | err = symbol__annotate2(ms, evsel, NULL); |
3076 | if (err) { |
3077 | char msg[BUFSIZ]; |
3078 | |
3079 | dso->annotate_warned = true; |
3080 | symbol__strerror_disassemble(ms, errnum: err, buf: msg, buflen: sizeof(msg)); |
3081 | ui__error(format: "Couldn't annotate %s:\n%s" , sym->name, msg); |
3082 | return -1; |
3083 | } |
3084 | |
3085 | if (annotate_opts.print_lines) { |
3086 | srcline_full_filename = annotate_opts.full_path; |
3087 | symbol__calc_lines(ms, root: &source_line); |
3088 | print_summary(root: &source_line, filename: dso->long_name); |
3089 | } |
3090 | |
3091 | hists__scnprintf_title(hists, bf: buf, size: sizeof(buf)); |
3092 | fprintf(stdout, "%s, [percent: %s]\n%s() %s\n" , |
3093 | buf, percent_type_str(annotate_opts.percent_type), sym->name, |
3094 | dso->long_name); |
3095 | symbol__annotate_fprintf2(sym, stdout); |
3096 | |
3097 | annotated_source__purge(as: symbol__annotation(sym)->src); |
3098 | |
3099 | return 0; |
3100 | } |
3101 | |
3102 | int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel) |
3103 | { |
3104 | struct dso *dso = map__dso(map: ms->map); |
3105 | struct symbol *sym = ms->sym; |
3106 | struct rb_root source_line = RB_ROOT; |
3107 | int err; |
3108 | |
3109 | err = symbol__annotate(ms, evsel, NULL); |
3110 | if (err) { |
3111 | char msg[BUFSIZ]; |
3112 | |
3113 | dso->annotate_warned = true; |
3114 | symbol__strerror_disassemble(ms, errnum: err, buf: msg, buflen: sizeof(msg)); |
3115 | ui__error(format: "Couldn't annotate %s:\n%s" , sym->name, msg); |
3116 | return -1; |
3117 | } |
3118 | |
3119 | symbol__calc_percent(sym, evsel); |
3120 | |
3121 | if (annotate_opts.print_lines) { |
3122 | srcline_full_filename = annotate_opts.full_path; |
3123 | symbol__calc_lines(ms, root: &source_line); |
3124 | print_summary(root: &source_line, filename: dso->long_name); |
3125 | } |
3126 | |
3127 | symbol__annotate_printf(ms, evsel); |
3128 | |
3129 | annotated_source__purge(as: symbol__annotation(sym)->src); |
3130 | |
3131 | return 0; |
3132 | } |
3133 | |
3134 | bool ui__has_annotation(void) |
3135 | { |
3136 | return use_browser == 1 && perf_hpp_list.sym; |
3137 | } |
3138 | |
3139 | |
3140 | static double annotation_line__max_percent(struct annotation_line *al, |
3141 | struct annotation *notes, |
3142 | unsigned int percent_type) |
3143 | { |
3144 | double percent_max = 0.0; |
3145 | int i; |
3146 | |
3147 | for (i = 0; i < notes->nr_events; i++) { |
3148 | double percent; |
3149 | |
3150 | percent = annotation_data__percent(data: &al->data[i], |
3151 | which: percent_type); |
3152 | |
3153 | if (percent > percent_max) |
3154 | percent_max = percent; |
3155 | } |
3156 | |
3157 | return percent_max; |
3158 | } |
3159 | |
3160 | static void disasm_line__write(struct disasm_line *dl, struct annotation *notes, |
3161 | void *obj, char *bf, size_t size, |
3162 | void (*obj__printf)(void *obj, const char *fmt, ...), |
3163 | void (*obj__write_graph)(void *obj, int graph)) |
3164 | { |
3165 | if (dl->ins.ops && dl->ins.ops->scnprintf) { |
3166 | if (ins__is_jump(ins: &dl->ins)) { |
3167 | bool fwd; |
3168 | |
3169 | if (dl->ops.target.outside) |
3170 | goto call_like; |
3171 | fwd = dl->ops.target.offset > dl->al.offset; |
3172 | obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); |
3173 | obj__printf(obj, " " ); |
3174 | } else if (ins__is_call(ins: &dl->ins)) { |
3175 | call_like: |
3176 | obj__write_graph(obj, RARROW_CHAR); |
3177 | obj__printf(obj, " " ); |
3178 | } else if (ins__is_ret(ins: &dl->ins)) { |
3179 | obj__write_graph(obj, LARROW_CHAR); |
3180 | obj__printf(obj, " " ); |
3181 | } else { |
3182 | obj__printf(obj, " " ); |
3183 | } |
3184 | } else { |
3185 | obj__printf(obj, " " ); |
3186 | } |
3187 | |
3188 | disasm_line__scnprintf(dl, bf, size, raw: !annotate_opts.use_offset, max_ins_name: notes->widths.max_ins_name); |
3189 | } |
3190 | |
3191 | static void ipc_coverage_string(char *bf, int size, struct annotation *notes) |
3192 | { |
3193 | double ipc = 0.0, coverage = 0.0; |
3194 | struct annotated_branch *branch = annotation__get_branch(notes); |
3195 | |
3196 | if (branch && branch->hit_cycles) |
3197 | ipc = branch->hit_insn / ((double)branch->hit_cycles); |
3198 | |
3199 | if (branch && branch->total_insn) { |
3200 | coverage = branch->cover_insn * 100.0 / |
3201 | ((double)branch->total_insn); |
3202 | } |
3203 | |
3204 | scnprintf(buf: bf, size, fmt: "(Average IPC: %.2f, IPC Coverage: %.1f%%)" , |
3205 | ipc, coverage); |
3206 | } |
3207 | |
3208 | static void __annotation_line__write(struct annotation_line *al, struct annotation *notes, |
3209 | bool first_line, bool current_entry, bool change_color, int width, |
3210 | void *obj, unsigned int percent_type, |
3211 | int (*obj__set_color)(void *obj, int color), |
3212 | void (*obj__set_percent_color)(void *obj, double percent, bool current), |
3213 | int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current), |
3214 | void (*obj__printf)(void *obj, const char *fmt, ...), |
3215 | void (*obj__write_graph)(void *obj, int graph)) |
3216 | |
3217 | { |
3218 | double percent_max = annotation_line__max_percent(al, notes, percent_type); |
3219 | int pcnt_width = annotation__pcnt_width(notes), |
3220 | cycles_width = annotation__cycles_width(notes); |
3221 | bool show_title = false; |
3222 | char bf[256]; |
3223 | int printed; |
3224 | |
3225 | if (first_line && (al->offset == -1 || percent_max == 0.0)) { |
3226 | if (notes->branch && al->cycles) { |
3227 | if (al->cycles->ipc == 0.0 && al->cycles->avg == 0) |
3228 | show_title = true; |
3229 | } else |
3230 | show_title = true; |
3231 | } |
3232 | |
3233 | if (al->offset != -1 && percent_max != 0.0) { |
3234 | int i; |
3235 | |
3236 | for (i = 0; i < notes->nr_events; i++) { |
3237 | double percent; |
3238 | |
3239 | percent = annotation_data__percent(data: &al->data[i], which: percent_type); |
3240 | |
3241 | obj__set_percent_color(obj, percent, current_entry); |
3242 | if (symbol_conf.show_total_period) { |
3243 | obj__printf(obj, "%11" PRIu64 " " , al->data[i].he.period); |
3244 | } else if (symbol_conf.show_nr_samples) { |
3245 | obj__printf(obj, "%6" PRIu64 " " , |
3246 | al->data[i].he.nr_samples); |
3247 | } else { |
3248 | obj__printf(obj, "%6.2f " , percent); |
3249 | } |
3250 | } |
3251 | } else { |
3252 | obj__set_percent_color(obj, 0, current_entry); |
3253 | |
3254 | if (!show_title) |
3255 | obj__printf(obj, "%-*s" , pcnt_width, " " ); |
3256 | else { |
3257 | obj__printf(obj, "%-*s" , pcnt_width, |
3258 | symbol_conf.show_total_period ? "Period" : |
3259 | symbol_conf.show_nr_samples ? "Samples" : "Percent" ); |
3260 | } |
3261 | } |
3262 | |
3263 | if (notes->branch) { |
3264 | if (al->cycles && al->cycles->ipc) |
3265 | obj__printf(obj, "%*.2f " , ANNOTATION__IPC_WIDTH - 1, al->cycles->ipc); |
3266 | else if (!show_title) |
3267 | obj__printf(obj, "%*s" , ANNOTATION__IPC_WIDTH, " " ); |
3268 | else |
3269 | obj__printf(obj, "%*s " , ANNOTATION__IPC_WIDTH - 1, "IPC" ); |
3270 | |
3271 | if (!annotate_opts.show_minmax_cycle) { |
3272 | if (al->cycles && al->cycles->avg) |
3273 | obj__printf(obj, "%*" PRIu64 " " , |
3274 | ANNOTATION__CYCLES_WIDTH - 1, al->cycles->avg); |
3275 | else if (!show_title) |
3276 | obj__printf(obj, "%*s" , |
3277 | ANNOTATION__CYCLES_WIDTH, " " ); |
3278 | else |
3279 | obj__printf(obj, "%*s " , |
3280 | ANNOTATION__CYCLES_WIDTH - 1, |
3281 | "Cycle" ); |
3282 | } else { |
3283 | if (al->cycles) { |
3284 | char str[32]; |
3285 | |
3286 | scnprintf(str, sizeof(str), |
3287 | "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")" , |
3288 | al->cycles->avg, al->cycles->min, |
3289 | al->cycles->max); |
3290 | |
3291 | obj__printf(obj, "%*s " , |
3292 | ANNOTATION__MINMAX_CYCLES_WIDTH - 1, |
3293 | str); |
3294 | } else if (!show_title) |
3295 | obj__printf(obj, "%*s" , |
3296 | ANNOTATION__MINMAX_CYCLES_WIDTH, |
3297 | " " ); |
3298 | else |
3299 | obj__printf(obj, "%*s " , |
3300 | ANNOTATION__MINMAX_CYCLES_WIDTH - 1, |
3301 | "Cycle(min/max)" ); |
3302 | } |
3303 | |
3304 | if (show_title && !*al->line) { |
3305 | ipc_coverage_string(bf, size: sizeof(bf), notes); |
3306 | obj__printf(obj, "%*s" , ANNOTATION__AVG_IPC_WIDTH, bf); |
3307 | } |
3308 | } |
3309 | |
3310 | obj__printf(obj, " " ); |
3311 | |
3312 | if (!*al->line) |
3313 | obj__printf(obj, "%-*s" , width - pcnt_width - cycles_width, " " ); |
3314 | else if (al->offset == -1) { |
3315 | if (al->line_nr && annotate_opts.show_linenr) |
3316 | printed = scnprintf(buf: bf, size: sizeof(bf), fmt: "%-*d " , notes->widths.addr + 1, al->line_nr); |
3317 | else |
3318 | printed = scnprintf(buf: bf, size: sizeof(bf), fmt: "%-*s " , notes->widths.addr, " " ); |
3319 | obj__printf(obj, bf); |
3320 | obj__printf(obj, "%-*s" , width - printed - pcnt_width - cycles_width + 1, al->line); |
3321 | } else { |
3322 | u64 addr = al->offset; |
3323 | int color = -1; |
3324 | |
3325 | if (!annotate_opts.use_offset) |
3326 | addr += notes->start; |
3327 | |
3328 | if (!annotate_opts.use_offset) { |
3329 | printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": " , addr); |
3330 | } else { |
3331 | if (al->jump_sources && |
3332 | annotate_opts.offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { |
3333 | if (annotate_opts.show_nr_jumps) { |
3334 | int prev; |
3335 | printed = scnprintf(buf: bf, size: sizeof(bf), fmt: "%*d " , |
3336 | notes->widths.jumps, |
3337 | al->jump_sources); |
3338 | prev = obj__set_jumps_percent_color(obj, al->jump_sources, |
3339 | current_entry); |
3340 | obj__printf(obj, bf); |
3341 | obj__set_color(obj, prev); |
3342 | } |
3343 | print_addr: |
3344 | printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": " , |
3345 | notes->widths.target, addr); |
3346 | } else if (ins__is_call(ins: &disasm_line(al)->ins) && |
3347 | annotate_opts.offset_level >= ANNOTATION__OFFSET_CALL) { |
3348 | goto print_addr; |
3349 | } else if (annotate_opts.offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { |
3350 | goto print_addr; |
3351 | } else { |
3352 | printed = scnprintf(buf: bf, size: sizeof(bf), fmt: "%-*s " , |
3353 | notes->widths.addr, " " ); |
3354 | } |
3355 | } |
3356 | |
3357 | if (change_color) |
3358 | color = obj__set_color(obj, HE_COLORSET_ADDR); |
3359 | obj__printf(obj, bf); |
3360 | if (change_color) |
3361 | obj__set_color(obj, color); |
3362 | |
3363 | disasm_line__write(dl: disasm_line(al), notes, obj, bf, size: sizeof(bf), obj__printf, obj__write_graph); |
3364 | |
3365 | obj__printf(obj, "%-*s" , width - pcnt_width - cycles_width - 3 - printed, bf); |
3366 | } |
3367 | |
3368 | } |
3369 | |
3370 | void annotation_line__write(struct annotation_line *al, struct annotation *notes, |
3371 | struct annotation_write_ops *wops) |
3372 | { |
3373 | __annotation_line__write(al, notes, first_line: wops->first_line, current_entry: wops->current_entry, |
3374 | change_color: wops->change_color, width: wops->width, obj: wops->obj, |
3375 | percent_type: annotate_opts.percent_type, |
3376 | obj__set_color: wops->set_color, obj__set_percent_color: wops->set_percent_color, |
3377 | obj__set_jumps_percent_color: wops->set_jumps_percent_color, obj__printf: wops->printf, |
3378 | obj__write_graph: wops->write_graph); |
3379 | } |
3380 | |
3381 | int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel, |
3382 | struct arch **parch) |
3383 | { |
3384 | struct symbol *sym = ms->sym; |
3385 | struct annotation *notes = symbol__annotation(sym); |
3386 | size_t size = symbol__size(sym); |
3387 | int nr_pcnt = 1, err; |
3388 | |
3389 | notes->src->offsets = zalloc(size * sizeof(struct annotation_line *)); |
3390 | if (notes->src->offsets == NULL) |
3391 | return ENOMEM; |
3392 | |
3393 | if (evsel__is_group_event(evsel)) |
3394 | nr_pcnt = evsel->core.nr_members; |
3395 | |
3396 | err = symbol__annotate(ms, evsel, parch); |
3397 | if (err) |
3398 | goto out_free_offsets; |
3399 | |
3400 | symbol__calc_percent(sym, evsel); |
3401 | |
3402 | annotation__set_offsets(notes, size); |
3403 | annotation__mark_jump_targets(notes, sym); |
3404 | |
3405 | err = annotation__compute_ipc(notes, size); |
3406 | if (err) |
3407 | goto out_free_offsets; |
3408 | |
3409 | annotation__init_column_widths(notes, sym); |
3410 | notes->nr_events = nr_pcnt; |
3411 | |
3412 | annotation__update_column_widths(notes); |
3413 | sym->annotate2 = 1; |
3414 | |
3415 | return 0; |
3416 | |
3417 | out_free_offsets: |
3418 | zfree(¬es->src->offsets); |
3419 | return err; |
3420 | } |
3421 | |
3422 | static int annotation__config(const char *var, const char *value, void *data) |
3423 | { |
3424 | struct annotation_options *opt = data; |
3425 | |
3426 | if (!strstarts(str: var, prefix: "annotate." )) |
3427 | return 0; |
3428 | |
3429 | if (!strcmp(var, "annotate.offset_level" )) { |
3430 | perf_config_u8(dest: &opt->offset_level, name: "offset_level" , value); |
3431 | |
3432 | if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL) |
3433 | opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL; |
3434 | else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL) |
3435 | opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL; |
3436 | } else if (!strcmp(var, "annotate.hide_src_code" )) { |
3437 | opt->hide_src_code = perf_config_bool("hide_src_code" , value); |
3438 | } else if (!strcmp(var, "annotate.jump_arrows" )) { |
3439 | opt->jump_arrows = perf_config_bool("jump_arrows" , value); |
3440 | } else if (!strcmp(var, "annotate.show_linenr" )) { |
3441 | opt->show_linenr = perf_config_bool("show_linenr" , value); |
3442 | } else if (!strcmp(var, "annotate.show_nr_jumps" )) { |
3443 | opt->show_nr_jumps = perf_config_bool("show_nr_jumps" , value); |
3444 | } else if (!strcmp(var, "annotate.show_nr_samples" )) { |
3445 | symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples" , |
3446 | value); |
3447 | } else if (!strcmp(var, "annotate.show_total_period" )) { |
3448 | symbol_conf.show_total_period = perf_config_bool("show_total_period" , |
3449 | value); |
3450 | } else if (!strcmp(var, "annotate.use_offset" )) { |
3451 | opt->use_offset = perf_config_bool("use_offset" , value); |
3452 | } else if (!strcmp(var, "annotate.disassembler_style" )) { |
3453 | opt->disassembler_style = strdup(value); |
3454 | if (!opt->disassembler_style) { |
3455 | pr_err("Not enough memory for annotate.disassembler_style\n" ); |
3456 | return -1; |
3457 | } |
3458 | } else if (!strcmp(var, "annotate.objdump" )) { |
3459 | opt->objdump_path = strdup(value); |
3460 | if (!opt->objdump_path) { |
3461 | pr_err("Not enough memory for annotate.objdump\n" ); |
3462 | return -1; |
3463 | } |
3464 | } else if (!strcmp(var, "annotate.addr2line" )) { |
3465 | symbol_conf.addr2line_path = strdup(value); |
3466 | if (!symbol_conf.addr2line_path) { |
3467 | pr_err("Not enough memory for annotate.addr2line\n" ); |
3468 | return -1; |
3469 | } |
3470 | } else if (!strcmp(var, "annotate.demangle" )) { |
3471 | symbol_conf.demangle = perf_config_bool("demangle" , value); |
3472 | } else if (!strcmp(var, "annotate.demangle_kernel" )) { |
3473 | symbol_conf.demangle_kernel = perf_config_bool("demangle_kernel" , value); |
3474 | } else { |
3475 | pr_debug("%s variable unknown, ignoring..." , var); |
3476 | } |
3477 | |
3478 | return 0; |
3479 | } |
3480 | |
3481 | void annotation_options__init(void) |
3482 | { |
3483 | struct annotation_options *opt = &annotate_opts; |
3484 | |
3485 | memset(opt, 0, sizeof(*opt)); |
3486 | |
3487 | /* Default values. */ |
3488 | opt->use_offset = true; |
3489 | opt->jump_arrows = true; |
3490 | opt->annotate_src = true; |
3491 | opt->offset_level = ANNOTATION__OFFSET_JUMP_TARGETS; |
3492 | opt->percent_type = PERCENT_PERIOD_LOCAL; |
3493 | } |
3494 | |
3495 | void annotation_options__exit(void) |
3496 | { |
3497 | zfree(&annotate_opts.disassembler_style); |
3498 | zfree(&annotate_opts.objdump_path); |
3499 | } |
3500 | |
3501 | void annotation_config__init(void) |
3502 | { |
3503 | perf_config(fn: annotation__config, &annotate_opts); |
3504 | } |
3505 | |
3506 | static unsigned int parse_percent_type(char *str1, char *str2) |
3507 | { |
3508 | unsigned int type = (unsigned int) -1; |
3509 | |
3510 | if (!strcmp("period" , str1)) { |
3511 | if (!strcmp("local" , str2)) |
3512 | type = PERCENT_PERIOD_LOCAL; |
3513 | else if (!strcmp("global" , str2)) |
3514 | type = PERCENT_PERIOD_GLOBAL; |
3515 | } |
3516 | |
3517 | if (!strcmp("hits" , str1)) { |
3518 | if (!strcmp("local" , str2)) |
3519 | type = PERCENT_HITS_LOCAL; |
3520 | else if (!strcmp("global" , str2)) |
3521 | type = PERCENT_HITS_GLOBAL; |
3522 | } |
3523 | |
3524 | return type; |
3525 | } |
3526 | |
3527 | int annotate_parse_percent_type(const struct option *opt __maybe_unused, const char *_str, |
3528 | int unset __maybe_unused) |
3529 | { |
3530 | unsigned int type; |
3531 | char *str1, *str2; |
3532 | int err = -1; |
3533 | |
3534 | str1 = strdup(_str); |
3535 | if (!str1) |
3536 | return -ENOMEM; |
3537 | |
3538 | str2 = strchr(str1, '-'); |
3539 | if (!str2) |
3540 | goto out; |
3541 | |
3542 | *str2++ = 0; |
3543 | |
3544 | type = parse_percent_type(str1, str2); |
3545 | if (type == (unsigned int) -1) |
3546 | type = parse_percent_type(str1: str2, str2: str1); |
3547 | if (type != (unsigned int) -1) { |
3548 | annotate_opts.percent_type = type; |
3549 | err = 0; |
3550 | } |
3551 | |
3552 | out: |
3553 | free(str1); |
3554 | return err; |
3555 | } |
3556 | |
3557 | int annotate_check_args(void) |
3558 | { |
3559 | struct annotation_options *args = &annotate_opts; |
3560 | |
3561 | if (args->prefix_strip && !args->prefix) { |
3562 | pr_err("--prefix-strip requires --prefix\n" ); |
3563 | return -1; |
3564 | } |
3565 | return 0; |
3566 | } |
3567 | |
3568 | /* |
3569 | * Get register number and access offset from the given instruction. |
3570 | * It assumes AT&T x86 asm format like OFFSET(REG). Maybe it needs |
3571 | * to revisit the format when it handles different architecture. |
3572 | * Fills @reg and @offset when return 0. |
3573 | */ |
3574 | static int (struct arch *arch, const char *str, |
3575 | struct annotated_op_loc *op_loc) |
3576 | { |
3577 | char *p; |
3578 | char *regname; |
3579 | |
3580 | if (arch->objdump.register_char == 0) |
3581 | return -1; |
3582 | |
3583 | /* |
3584 | * It should start from offset, but it's possible to skip 0 |
3585 | * in the asm. So 0(%rax) should be same as (%rax). |
3586 | * |
3587 | * However, it also start with a segment select register like |
3588 | * %gs:0x18(%rbx). In that case it should skip the part. |
3589 | */ |
3590 | if (*str == arch->objdump.register_char) { |
3591 | while (*str && !isdigit(c: *str) && |
3592 | *str != arch->objdump.memory_ref_char) |
3593 | str++; |
3594 | } |
3595 | |
3596 | op_loc->offset = strtol(str, &p, 0); |
3597 | |
3598 | p = strchr(p, arch->objdump.register_char); |
3599 | if (p == NULL) |
3600 | return -1; |
3601 | |
3602 | regname = strdup(p); |
3603 | if (regname == NULL) |
3604 | return -1; |
3605 | |
3606 | op_loc->reg1 = get_dwarf_regnum(regname, 0); |
3607 | free(regname); |
3608 | |
3609 | /* Get the second register */ |
3610 | if (op_loc->multi_regs) { |
3611 | p = strchr(p + 1, arch->objdump.register_char); |
3612 | if (p == NULL) |
3613 | return -1; |
3614 | |
3615 | regname = strdup(p); |
3616 | if (regname == NULL) |
3617 | return -1; |
3618 | |
3619 | op_loc->reg2 = get_dwarf_regnum(regname, 0); |
3620 | free(regname); |
3621 | } |
3622 | return 0; |
3623 | } |
3624 | |
3625 | /** |
3626 | * annotate_get_insn_location - Get location of instruction |
3627 | * @arch: the architecture info |
3628 | * @dl: the target instruction |
3629 | * @loc: a buffer to save the data |
3630 | * |
3631 | * Get detailed location info (register and offset) in the instruction. |
3632 | * It needs both source and target operand and whether it accesses a |
3633 | * memory location. The offset field is meaningful only when the |
3634 | * corresponding mem flag is set. The reg2 field is meaningful only |
3635 | * when multi_regs flag is set. |
3636 | * |
3637 | * Some examples on x86: |
3638 | * |
3639 | * mov (%rax), %rcx # src_reg1 = rax, src_mem = 1, src_offset = 0 |
3640 | * # dst_reg1 = rcx, dst_mem = 0 |
3641 | * |
3642 | * mov 0x18, %r8 # src_reg1 = -1, src_mem = 0 |
3643 | * # dst_reg1 = r8, dst_mem = 0 |
3644 | * |
3645 | * mov %rsi, 8(%rbx,%rcx,4) # src_reg1 = rsi, src_mem = 0, dst_multi_regs = 0 |
3646 | * # dst_reg1 = rbx, dst_reg2 = rcx, dst_mem = 1 |
3647 | * # dst_multi_regs = 1, dst_offset = 8 |
3648 | */ |
3649 | int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, |
3650 | struct annotated_insn_loc *loc) |
3651 | { |
3652 | struct ins_operands *ops; |
3653 | struct annotated_op_loc *op_loc; |
3654 | int i; |
3655 | |
3656 | if (!strcmp(dl->ins.name, "lock" )) |
3657 | ops = dl->ops.locked.ops; |
3658 | else |
3659 | ops = &dl->ops; |
3660 | |
3661 | if (ops == NULL) |
3662 | return -1; |
3663 | |
3664 | memset(loc, 0, sizeof(*loc)); |
3665 | |
3666 | for_each_insn_op_loc(loc, i, op_loc) { |
3667 | const char *insn_str = ops->source.raw; |
3668 | bool multi_regs = ops->source.multi_regs; |
3669 | |
3670 | if (i == INSN_OP_TARGET) { |
3671 | insn_str = ops->target.raw; |
3672 | multi_regs = ops->target.multi_regs; |
3673 | } |
3674 | |
3675 | /* Invalidate the register by default */ |
3676 | op_loc->reg1 = -1; |
3677 | op_loc->reg2 = -1; |
3678 | |
3679 | if (insn_str == NULL) |
3680 | continue; |
3681 | |
3682 | if (strchr(insn_str, arch->objdump.memory_ref_char)) { |
3683 | op_loc->mem_ref = true; |
3684 | op_loc->multi_regs = multi_regs; |
3685 | extract_reg_offset(arch, str: insn_str, op_loc); |
3686 | } else { |
3687 | char *s = strdup(insn_str); |
3688 | |
3689 | if (s) { |
3690 | op_loc->reg1 = get_dwarf_regnum(s, 0); |
3691 | free(s); |
3692 | } |
3693 | } |
3694 | } |
3695 | |
3696 | return 0; |
3697 | } |
3698 | |
3699 | static void symbol__ensure_annotate(struct map_symbol *ms, struct evsel *evsel) |
3700 | { |
3701 | struct disasm_line *dl, *tmp_dl; |
3702 | struct annotation *notes; |
3703 | |
3704 | notes = symbol__annotation(sym: ms->sym); |
3705 | if (!list_empty(head: ¬es->src->source)) |
3706 | return; |
3707 | |
3708 | if (symbol__annotate(ms, evsel, NULL) < 0) |
3709 | return; |
3710 | |
3711 | /* remove non-insn disasm lines for simplicity */ |
3712 | list_for_each_entry_safe(dl, tmp_dl, ¬es->src->source, al.node) { |
3713 | if (dl->al.offset == -1) { |
3714 | list_del(entry: &dl->al.node); |
3715 | free(dl); |
3716 | } |
3717 | } |
3718 | } |
3719 | |
3720 | static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip) |
3721 | { |
3722 | struct disasm_line *dl; |
3723 | struct annotation *notes; |
3724 | |
3725 | notes = symbol__annotation(sym); |
3726 | |
3727 | list_for_each_entry(dl, ¬es->src->source, al.node) { |
3728 | if (sym->start + dl->al.offset == ip) { |
3729 | /* |
3730 | * llvm-objdump places "lock" in a separate line and |
3731 | * in that case, we want to get the next line. |
3732 | */ |
3733 | if (!strcmp(dl->ins.name, "lock" ) && *dl->ops.raw == '\0') { |
3734 | ip++; |
3735 | continue; |
3736 | } |
3737 | return dl; |
3738 | } |
3739 | } |
3740 | return NULL; |
3741 | } |
3742 | |
3743 | static struct annotated_item_stat *annotate_data_stat(struct list_head *head, |
3744 | const char *name) |
3745 | { |
3746 | struct annotated_item_stat *istat; |
3747 | |
3748 | list_for_each_entry(istat, head, list) { |
3749 | if (!strcmp(istat->name, name)) |
3750 | return istat; |
3751 | } |
3752 | |
3753 | istat = zalloc(sizeof(*istat)); |
3754 | if (istat == NULL) |
3755 | return NULL; |
3756 | |
3757 | istat->name = strdup(name); |
3758 | if (istat->name == NULL) { |
3759 | free(istat); |
3760 | return NULL; |
3761 | } |
3762 | |
3763 | list_add_tail(new: &istat->list, head); |
3764 | return istat; |
3765 | } |
3766 | |
3767 | static bool is_stack_operation(struct arch *arch, struct disasm_line *dl) |
3768 | { |
3769 | if (arch__is(arch, name: "x86" )) { |
3770 | if (!strncmp(dl->ins.name, "push" , 4) || |
3771 | !strncmp(dl->ins.name, "pop" , 3) || |
3772 | !strncmp(dl->ins.name, "ret" , 3)) |
3773 | return true; |
3774 | } |
3775 | |
3776 | return false; |
3777 | } |
3778 | |
3779 | u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, |
3780 | struct disasm_line *dl) |
3781 | { |
3782 | struct annotation *notes; |
3783 | struct disasm_line *next; |
3784 | u64 addr; |
3785 | |
3786 | notes = symbol__annotation(sym: ms->sym); |
3787 | /* |
3788 | * PC-relative addressing starts from the next instruction address |
3789 | * But the IP is for the current instruction. Since disasm_line |
3790 | * doesn't have the instruction size, calculate it using the next |
3791 | * disasm_line. If it's the last one, we can use symbol's end |
3792 | * address directly. |
3793 | */ |
3794 | if (&dl->al.node == notes->src->source.prev) |
3795 | addr = ms->sym->end + offset; |
3796 | else { |
3797 | next = list_next_entry(dl, al.node); |
3798 | addr = ip + (next->al.offset - dl->al.offset) + offset; |
3799 | } |
3800 | return map__rip_2objdump(map: ms->map, rip: addr); |
3801 | } |
3802 | |
3803 | /** |
3804 | * hist_entry__get_data_type - find data type for given hist entry |
3805 | * @he: hist entry |
3806 | * |
3807 | * This function first annotates the instruction at @he->ip and extracts |
3808 | * register and offset info from it. Then it searches the DWARF debug |
3809 | * info to get a variable and type information using the address, register, |
3810 | * and offset. |
3811 | */ |
3812 | struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) |
3813 | { |
3814 | struct map_symbol *ms = &he->ms; |
3815 | struct evsel *evsel = hists_to_evsel(hists: he->hists); |
3816 | struct arch *arch; |
3817 | struct disasm_line *dl; |
3818 | struct annotated_insn_loc loc; |
3819 | struct annotated_op_loc *op_loc; |
3820 | struct annotated_data_type *mem_type; |
3821 | struct annotated_item_stat *istat; |
3822 | u64 ip = he->ip, addr = 0; |
3823 | const char *var_name = NULL; |
3824 | int var_offset; |
3825 | int i; |
3826 | |
3827 | ann_data_stat.total++; |
3828 | |
3829 | if (ms->map == NULL || ms->sym == NULL) { |
3830 | ann_data_stat.no_sym++; |
3831 | return NULL; |
3832 | } |
3833 | |
3834 | if (!symbol_conf.init_annotation) { |
3835 | ann_data_stat.no_sym++; |
3836 | return NULL; |
3837 | } |
3838 | |
3839 | if (evsel__get_arch(evsel, parch: &arch) < 0) { |
3840 | ann_data_stat.no_insn++; |
3841 | return NULL; |
3842 | } |
3843 | |
3844 | /* Make sure it runs objdump to get disasm of the function */ |
3845 | symbol__ensure_annotate(ms, evsel); |
3846 | |
3847 | /* |
3848 | * Get a disasm to extract the location from the insn. |
3849 | * This is too slow... |
3850 | */ |
3851 | dl = find_disasm_line(sym: ms->sym, ip); |
3852 | if (dl == NULL) { |
3853 | ann_data_stat.no_insn++; |
3854 | return NULL; |
3855 | } |
3856 | |
3857 | retry: |
3858 | istat = annotate_data_stat(head: &ann_insn_stat, name: dl->ins.name); |
3859 | if (istat == NULL) { |
3860 | ann_data_stat.no_insn++; |
3861 | return NULL; |
3862 | } |
3863 | |
3864 | if (annotate_get_insn_location(arch, dl, loc: &loc) < 0) { |
3865 | ann_data_stat.no_insn_ops++; |
3866 | istat->bad++; |
3867 | return NULL; |
3868 | } |
3869 | |
3870 | if (is_stack_operation(arch, dl)) { |
3871 | istat->good++; |
3872 | he->mem_type_off = 0; |
3873 | return &stackop_type; |
3874 | } |
3875 | |
3876 | for_each_insn_op_loc(&loc, i, op_loc) { |
3877 | if (!op_loc->mem_ref) |
3878 | continue; |
3879 | |
3880 | /* Recalculate IP because of LOCK prefix or insn fusion */ |
3881 | ip = ms->sym->start + dl->al.offset; |
3882 | |
3883 | var_offset = op_loc->offset; |
3884 | |
3885 | /* PC-relative addressing */ |
3886 | if (op_loc->reg1 == DWARF_REG_PC) { |
3887 | struct addr_location al; |
3888 | struct symbol *var; |
3889 | u64 map_addr; |
3890 | |
3891 | addr = annotate_calc_pcrel(ms, ip, offset: op_loc->offset, dl); |
3892 | /* Kernel symbols might be relocated */ |
3893 | map_addr = addr + map__reloc(map: ms->map); |
3894 | |
3895 | addr_location__init(al: &al); |
3896 | var = thread__find_symbol_fb(thread: he->thread, cpumode: he->cpumode, |
3897 | addr: map_addr, al: &al); |
3898 | if (var) { |
3899 | var_name = var->name; |
3900 | /* Calculate type offset from the start of variable */ |
3901 | var_offset = map_addr - map__unmap_ip(map: al.map, ip_or_rip: var->start); |
3902 | } |
3903 | addr_location__exit(al: &al); |
3904 | } |
3905 | |
3906 | mem_type = find_data_type(ms, ip, loc: op_loc, addr, var_name); |
3907 | if (mem_type) |
3908 | istat->good++; |
3909 | else |
3910 | istat->bad++; |
3911 | |
3912 | if (mem_type && var_name) |
3913 | op_loc->offset = var_offset; |
3914 | |
3915 | if (symbol_conf.annotate_data_sample) { |
3916 | annotated_data_type__update_samples(adt: mem_type, evsel, |
3917 | offset: op_loc->offset, |
3918 | nr_samples: he->stat.nr_events, |
3919 | period: he->stat.period); |
3920 | } |
3921 | he->mem_type_off = op_loc->offset; |
3922 | return mem_type; |
3923 | } |
3924 | |
3925 | /* |
3926 | * Some instructions can be fused and the actual memory access came |
3927 | * from the previous instruction. |
3928 | */ |
3929 | if (dl->al.offset > 0) { |
3930 | struct disasm_line *prev_dl; |
3931 | |
3932 | prev_dl = list_prev_entry(dl, al.node); |
3933 | if (ins__is_fused(arch, ins1: prev_dl->ins.name, ins2: dl->ins.name)) { |
3934 | dl = prev_dl; |
3935 | goto retry; |
3936 | } |
3937 | } |
3938 | |
3939 | ann_data_stat.no_mem_ops++; |
3940 | istat->bad++; |
3941 | return NULL; |
3942 | } |
3943 | |