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