Skip to contentSkip to conversation

gdbstub: Unbreak i386 with gdb remote

PATCH Applied Peter Xu 3 participants 9 messages Apr 9, 2020

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover gdbstub: Unbreak i386 with gdb remote

It's silently broken on master during an refactoring. Let's fix it.

Please feel free to drop patch 1 if it's not rc material, but I guess we want patch 2...

Thanks,

Peter Xu (2):
gdbstub: Assert len read from each register
gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

 gdbstub.c             | 11 ++++++++---
 target/i386/gdbstub.c |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)
Signature · 1 line
2.24.1
author
#1 lore raw

PATCH 1/2 gdbstub: Assert len read from each register

This can expose the issue earlier on which register returned the wrong result.

---
 gdbstub.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
gdbstub.c +8 -3
index 171e150950..f763545e81 100644
@@ -911,17 +911,22 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
     CPUClass *cc = CPU_GET_CLASS(cpu);
     CPUArchState *env = cpu->env_ptr;
     GDBRegisterState *r;
+    int len = 0, orig_len = buf->len;
 
     if (reg < cc->gdb_num_core_regs) {
-        return cc->gdb_read_register(cpu, buf, reg);
+        len = cc->gdb_read_register(cpu, buf, reg);
     }
 
     for (r = cpu->gdb_regs; r; r = r->next) {
         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
-            return r->get_reg(env, buf, reg - r->base_reg);
+            len = r->get_reg(env, buf, reg - r->base_reg);
+            break;
         }
     }
-    return 0;
+
+    assert(len == buf->len - orig_len);
+
+    return len;
 }
 
 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH 1/2] gdbstub: Assert len read from each register

Quoted 13 lines · Peter Xu

This can expose the issue earlier on which register returned the wrong result.

---
  gdbstub.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
gdbstub.c +0 -0
index 171e150950..f763545e81 100644

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 917

Quoted 8 lines
gdbstub.c +2 -1
@@ -911,17 +911,22 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
      CPUClass *cc = CPU_GET_CLASS(cpu);
      CPUArchState *env = cpu->env_ptr;
      GDBRegisterState *r;
+    int len = 0, orig_len = buf->len;
 
      if (reg < cc->gdb_num_core_regs) {
-        return cc->gdb_read_register(cpu, buf, reg);
+        len = cc->gdb_read_register(cpu, buf, reg);

This change the code flow. We could add ...:

           goto out;

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 918

Quoted 1 line
gdbstub.c +0 -0
      }

... or use else?

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 926

Quoted 10 lines
gdbstub.c +3 -2
      for (r = cpu->gdb_regs; r; r = r->next) {
          if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
-            return r->get_reg(env, buf, reg - r->base_reg);
+            len = r->get_reg(env, buf, reg - r->base_reg);
+            break;
          }
      }
-    return 0;
+

out:

Quoted 7 lines
gdbstub.c +3 -0
+    assert(len == buf->len - orig_len);
+
+    return len;
  }
 
  static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
 
author
#3 lore raw

Re: [PATCH 1/2] gdbstub: Assert len read from each register

Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Peter Xu

This can expose the issue earlier on which register returned the wrong result.

---
  gdbstub.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
Quoted 14 lines

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 916

Quoted 12 lines
gdbstub.c +2 -1
index 171e150950..f763545e81 100644
@@ -911,17 +911,22 @@ static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
      CPUClass *cc = CPU_GET_CLASS(cpu);
      CPUArchState *env = cpu->env_ptr;
      GDBRegisterState *r;
+    int len = 0, orig_len = buf->len;
      if (reg < cc->gdb_num_core_regs) {
-        return cc->gdb_read_register(cpu, buf, reg);
+        len = cc->gdb_read_register(cpu, buf, reg);

This change the code flow. We could add ...:

I didn't expect the "if" and "for" would collapse each other, but yeah that could still be better.

Thanks,

Quoted 25 lines
           goto out;

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 917

Quoted 1 line
gdbstub.c +0 -0
      }

... or use else?

gdbstub.c static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) · line 926

Quoted 9 lines
gdbstub.c +3 -2
      for (r = cpu->gdb_regs; r; r = r->next) {
          if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
-            return r->get_reg(env, buf, reg - r->base_reg);
+            len = r->get_reg(env, buf, reg - r->base_reg);
+            break;
          }
      }
-    return 0;
+

out:

Quoted 6 lines
gdbstub.c +3 -0
+    assert(len == buf->len - orig_len);
+
+    return len;
  }
  static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
 
Signature · 1 line
Peter Xu
author
#4 lore raw

PATCH 2/2 gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

In reply to Peter Xu

We should only pass in gdb_get_reg16() with the GByteArray* object itself, no need to shift. Without this patch, gdb remote attach will crash QEMU.

Cc Alex Bennée <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 target/i386/gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/gdbstub.c +1 -1
index f3d23b614e..b98a99500a 100644
@@ -106,7 +106,7 @@ int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
         floatx80 *fp = (floatx80 *) &env->fpregs[n - IDX_FP_REGS];
         int len = gdb_get_reg64(mem_buf, cpu_to_le64(fp->low));
-        len += gdb_get_reg16(mem_buf + len, cpu_to_le16(fp->high));
+        len += gdb_get_reg16(mem_buf, cpu_to_le16(fp->high));
         return len;
     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
         n -= IDX_XMM_REGS;
Signature · 1 line
2.24.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

Quoted 3 lines · Peter Xu

We should only pass in gdb_get_reg16() with the GByteArray* object itself, no need to shift. Without this patch, gdb remote attach will crash QEMU.

You are correct.

Fixes a010bdbe719 ("extend GByteArray to read register helpers")

Same problem in m68k_fpu_gdb_get_reg().

TODO for 5.1, rename mem_buf -> array.

Quoted 22 lines
Cc Alex Bennée <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
  target/i386/gdbstub.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/gdbstub.c +1 -1
index f3d23b614e..b98a99500a 100644
@@ -106,7 +106,7 @@ int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
      } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
          floatx80 *fp = (floatx80 *) &env->fpregs[n - IDX_FP_REGS];
          int len = gdb_get_reg64(mem_buf, cpu_to_le64(fp->low));
-        len += gdb_get_reg16(mem_buf + len, cpu_to_le16(fp->high));
+        len += gdb_get_reg16(mem_buf, cpu_to_le16(fp->high));
          return len;
      } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
          n -= IDX_XMM_REGS;
author
#6 lore raw

Re: [PATCH 2/2] gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

Hi, Phil,

Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Peter Xu

We should only pass in gdb_get_reg16() with the GByteArray* object itself, no need to shift. Without this patch, gdb remote attach will crash QEMU.

You are correct.

Fixes a010bdbe719 ("extend GByteArray to read register helpers")

Oh I forgot to paste the fix line. However, is it b7b8756a9c ("target/i386: use gdb_get_reg helpers", 2020-03-17) instead?

Quoted 2 lines

Thanks!

Quoted 4 lines

Same problem in m68k_fpu_gdb_get_reg().

TODO for 5.1, rename mem_buf -> array.

Signature · 1 line
Peter Xu
#7 lore raw

Re: [PATCH 2/2] gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

Quoted 8 lines · Peter Xu

Hi, Phil,

Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Peter Xu

We should only pass in gdb_get_reg16() with the GByteArray* object itself, no need to shift. Without this patch, gdb remote attach will crash QEMU.

Quoted 6 lines
Quoted 3 lines

You are correct.

Fixes a010bdbe719 ("extend GByteArray to read register helpers")

Oh I forgot to paste the fix line. However, is it b7b8756a9c ("target/i386: use gdb_get_reg helpers", 2020-03-17) instead?

b7b8756a9c is correct, at that time the codebase was using the correct API. the next commit updated the API but missed to update the lines you are fixed. So I think "fixes a010bdbe719" is correct.

Quoted 11 lines
Quoted 3 lines

Thanks!

Quoted 4 lines

Same problem in m68k_fpu_gdb_get_reg().

TODO for 5.1, rename mem_buf -> array.

Applied
#8 lore raw

Re: [PATCH 2/2] gdbstub: i386: Fix gdb_get_reg16() parameter to unbreak gdb

In reply to Peter Xu
Quoted 7 lines · Peter Xu

We should only pass in gdb_get_reg16() with the GByteArray* object itself, no need to shift. Without this patch, gdb remote attach will crash QEMU.

Cc Alex Bennée <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>

Queued to for-5.0/more-random-fixes, thanks.

Quoted 17 lines
---
 target/i386/gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/gdbstub.c +1 -1
index f3d23b614e..b98a99500a 100644
@@ -106,7 +106,7 @@ int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
         floatx80 *fp = (floatx80 *) &env->fpregs[n - IDX_FP_REGS];
         int len = gdb_get_reg64(mem_buf, cpu_to_le64(fp->low));
-        len += gdb_get_reg16(mem_buf + len, cpu_to_le16(fp->high));
+        len += gdb_get_reg16(mem_buf, cpu_to_le16(fp->high));
         return len;
     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
         n -= IDX_XMM_REGS;
Signature · 1 line
Alex Bennée