[2/3] perf annotate: Parse x86 SIB addressing properly

Message ID 20230511062725.514752-2-namhyung@kernel.org
State New
Headers
Series [1/3] perf annotate: Handle more instructions on x86 |

Commit Message

Namhyung Kim May 11, 2023, 6:27 a.m. UTC
  When the source argument of mov instruction is look like below, it didn't
parse the whole operand and just stopped at the first comma.

  mov    (%rbx,%rax,1),%rcx

Fix it by checking the parentheses and move it to the closing one.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/annotate.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
  

Comments

Ian Rogers May 15, 2023, 5:08 p.m. UTC | #1
On Wed, May 10, 2023 at 11:27 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> When the source argument of mov instruction is look like below, it didn't
> parse the whole operand and just stopped at the first comma.
>
>   mov    (%rbx,%rax,1),%rcx
>
> Fix it by checking the parentheses and move it to the closing one.
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
>  tools/perf/util/annotate.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
> index ca9f0add68f4..6053ddf9c32d 100644
> --- a/tools/perf/util/annotate.c
> +++ b/tools/perf/util/annotate.c
> @@ -550,6 +550,19 @@ static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_sy
>                 return -1;
>
>         *s = '\0';
> +
> +       /*
> +        * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
> +        * then it needs to have the closing parenthesis.
> +        */
> +       if (strchr(ops->raw, '(')) {
> +               *s = ',';
> +               s = strchr(ops->raw, ')');
> +               if (s == NULL || s[1] != ',')
> +                       return -1;
> +               *++s = '\0';
> +       }
> +
>         ops->source.raw = strdup(ops->raw);
>         *s = ',';
>
> --
> 2.40.1.521.gf1e218fcd8-goog
>
  

Patch

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index ca9f0add68f4..6053ddf9c32d 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -550,6 +550,19 @@  static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_sy
 		return -1;
 
 	*s = '\0';
+
+	/*
+	 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
+	 * then it needs to have the closing parenthesis.
+	 */
+	if (strchr(ops->raw, '(')) {
+		*s = ',';
+		s = strchr(ops->raw, ')');
+		if (s == NULL || s[1] != ',')
+			return -1;
+		*++s = '\0';
+	}
+
 	ops->source.raw = strdup(ops->raw);
 	*s = ',';