Skip to contentSkip to conversation

gdbstub.c: fix GDB connection segfault caused by empty machines

PATCH Superseded Ziyue Yang 2 participants 3 messages Jan 18, 2017

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ziyue Yang

author
#0 lore raw

PATCH v3 1/1 gdbstub.c: fix GDB connection segfault caused by empty machines

From: Ziyue Yang <[email protected]>

This patch is to fix the segmentation fault caused by attaching GDB to a QEMU instance initialized with "-M none" option.

The bug can be reproduced by

Quoted 1 line

./qemu-system-x86_64 -M none -nographic -S -s

and attach a GDB to it by

Quoted 1 line

gdb -ex 'target remote :1234

The segmentation fault was originally caused by trying to read the information about CPU when communicating with GDB. However, it's impossible for any control flow to exist on an empty machine, nor can CPU's be hot plugged to an empty machine later by QOM commands. So I think simply disabling GDB connections on empty machines makes sense.

Also some updates from fprintf(stderr, ...) to error_report.

---
 gdbstub.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
gdbstub.c +12 -5
index de62d26..fe1d0f8 100644
@@ -18,6 +18,7 @@
  */
 #include "qemu/osdep.h"
 #include "qapi/error.h"
+#include "qemu/error-report.h"
 #include "qemu/cutils.h"
 #include "cpu.h"
 #ifdef CONFIG_USER_ONLY
@@ -636,8 +637,8 @@ void gdb_register_coprocessor(CPUState *cpu,
     *p = s;
     if (g_pos) {
         if (g_pos != s->base_reg) {
-            fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
-                    "Expected %d got %d\n", xml, g_pos, s->base_reg);
+            error_report("Error: Bad gdb register numbering for '%s', "
+                         "Expected %d got %d", xml, g_pos, s->base_reg);
         } else {
             cpu->gdb_num_g_regs = cpu->gdb_num_regs;
         }
@@ -889,7 +890,7 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
         }
     case 'k':
         /* Kill the target */
-        fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
+        error_report("QEMU: Terminated via GDBstub");
         exit(0);
     case 'D':
         /* Detach packet */
@@ -1357,8 +1358,8 @@ void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
                 break;
             default:
             bad_format:
-                fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
-                        fmt - 1);
+                error_report("gdbstub: Bad syscall format string '%s'",
+                             fmt - 1);
                 break;
             }
         } else {
@@ -1731,6 +1732,12 @@ int gdbserver_start(const char *device)
     CharDriverState *mon_chr;
     ChardevCommon common = { 0 };
 
+    if (!first_cpu) {
+        error_report("gdbstub: meaningless to attach gdb to a "
+                     "machine without any CPU.");
+        return -1;
+    }
+
     if (!device)
         return -1;
     if (strcmp(device, "none") != 0) {

--
2.7.4

#1 lore raw
Quoted 1 line · Ziyue Yang

Also some updates from fprintf(stderr, ...) to error_report.

Looks like they belong to a separate patch.

Fam

author
#2 lore raw

Hi Fam,

I've split the patch into 2 smaller ones and submitted them tagged with "v4". Thanks!

Ziyue Yang

2017-01-18 10:15 GMT+08:00 Fam Zheng <[email protected]>:

Quoted 6 lines · Ziyue Yang
Quoted 1 line

Also some updates from fprintf(stderr, ...) to error_report.

Looks like they belong to a separate patch.

Fam