[PATCH v2] perf/annotate: Fix branch instruction with multiple operands

From: Kim Phillips
Date: Thu Jun 01 2017 - 10:30:11 EST


Perf annotate is dropping the cr* fields from branch instructions.
Fix it by adding support to display branch instructions having
multiple operands.

Power Arch objdump of int_sqrt:

20.36 | c0000000004d2694: subf r10,r10,r3
| c0000000004d2698: v bgt cr6,c0000000004d26a0 <int_sqrt+0x40>
1.82 | c0000000004d269c: mr r3,r10
29.18 | c0000000004d26a0: mr r10,r8
| c0000000004d26a4: v bgt cr7,c0000000004d26ac <int_sqrt+0x4c>
| c0000000004d26a8: mr r10,r7

Power Arch Before Patch:

20.36 | subf r10,r10,r3
| v bgt 40
1.82 | mr r3,r10
29.18 | 40: mr r10,r8
| v bgt 4c
| mr r10,r7

Power Arch After patch:

20.36 | subf r10,r10,r3
| v bgt cr6,40
1.82 | mr r3,r10
29.18 | 40: mr r10,r8
| v bgt cr7,4c
| mr r10,r7

Also support AArch64 conditional branch instructions, which can
have up to three operands:

Aarch64 Non-simplified (raw objdump) view:

âffff0000083cd11c: â cbz w0, ffff0000083cd100 <security_filâ
...
4.44 âffff000â083cd134: â tbnz w0, #26, ffff0000083cd190 <securitâ
...
1.37 âffff000â083cd144: â tbnz w22, #5, ffff0000083cd1a4 <securitâ
âffff000â083cd148: mov w19, #0x20000 //â
1.02 âffff000â083cd14c: â tbz w22, #2, ffff0000083cd1ac <securitâ
...
0.68 âffff000âââ3cd16c: â cbnz w0, ffff0000083cd120 <security_filâ

Aarch64 Simplified, before this patch:

â â cbz 40
...
4.44 â ââ tbnz w0, #26, ffff0000083cd190 <security_file_permissâ
...
1.37 â ââ tbnz w22, #5, ffff0000083cd1a4 <security_file_permissâ
â â mov w19, #0x20000 // #131072
1.02 â ââ tbz w22, #2, ffff0000083cd1ac <security_file_permissâ
...
0.68 â âââcbnz 60

the cbz operand is missing, and the tbz doesn't get simplified processing
at all because the parsing function failed to match an address.

Aarch64 Simplified, After this patch applied:

â â cbz w0, 40
...
4.44 â ââ tbnz w0, #26, d0
...
1.37 â ââ tbnz w22, #5, e4
â â mov w19, #0x20000 // #131072
1.02 â ââ tbz w22, #2, ec
...
0.68 â âââcbnz w0, 60

Reported-by: Anton Blanchard <anton@xxxxxxxxx>
Reported-by: Robin Murphy <robin.murphy@xxxxxxx>
Cc: Mark Rutland <mark.rutland@xxxxxxx>
Originally-by: Ravi Bangoria <ravi.bangoria@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Kim Phillips <kim.phillips@xxxxxxx>
---
v2: addressing Arnaldo's clean-this-up comment: merged commit texts
between Ravi's original, author changed in lieu of adding something I
hadn't known nor thought about possibly doing: Originally-by: Ravi.
Ravi, if that's not OK, provide an alternative means of keeping things
clean for Arnaldo, thanks.

tools/perf/util/annotate.c | 33 ++++++++++++++++++++++++++++++---
1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 683f8340460c..3174930e7cea 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -239,10 +239,20 @@ static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *op
const char *s = strchr(ops->raw, '+');
const char *c = strchr(ops->raw, ',');

- if (c++ != NULL)
+ /*
+ * skip over possible up to 2 operands to get to address, e.g.:
+ * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
+ */
+ if (c++ != NULL) {
ops->target.addr = strtoull(c, NULL, 16);
- else
+ if (!ops->target.addr) {
+ c = strchr(c, ',');
+ if (c++ != NULL)
+ ops->target.addr = strtoull(c, NULL, 16);
+ }
+ } else {
ops->target.addr = strtoull(ops->raw, NULL, 16);
+ }

if (s++ != NULL) {
ops->target.offset = strtoull(s, NULL, 16);
@@ -257,10 +267,27 @@ static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *op
static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
struct ins_operands *ops)
{
+ const char *c = strchr(ops->raw, ',');
+
if (!ops->target.addr || ops->target.offset < 0)
return ins__raw_scnprintf(ins, bf, size, ops);

- return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
+ if (c != NULL) {
+ const char *c2 = strchr(c + 1, ',');
+
+ /* check for 3-op insn */
+ if (c2 != NULL)
+ c = c2;
+ c++;
+
+ /* mirror arch objdump's space-after-comma style */
+ if (*c == ' ')
+ c++;
+ }
+
+ return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64,
+ ins->name, c ? c - ops->raw : 0, ops->raw,
+ ops->target.offset);
}

static struct ins_ops jump_ops = {
--
2.11.0