Subject: Re: GDB busted, what did I do to deserve this?
To: Craig M. Chase <chase@ece.utexas.edu>
From: Jochen Pohl <jpo.drs@sni.de>
List: current-users
Date: 11/15/1995 17:49:47
Craig M. Chase writes:
> chase@orac>/usr/bin/cc -g -static -o hello hello.c
> chase@orac>/usr/bin/gdb hello
> GDB is free software and you are welcome to distribute copies of it
>  under certain conditions; type "show copying" to see the conditions.
> There is absolutely no warranty for GDB; type "show warranty" for details.
> GDB 4.11 (i386-netbsd), Copyright 1993 Free Software Foundation, Inc...
> (gdb) break main
> Breakpoint 1 at 0x1098: file hello.c, line 5.
> (gdb) run
> Starting program: /a/orac/home/orac/chase/src/hello 
> 
> Breakpoint 1, main () at hello.c:5
> 5               printf("hello world\n");
> (gdb) n
> 
> Program received signal SIGSEGV (11), Segmentation fault
> 0x1099 in main () at hello.c:5
> 5               printf("hello world\n");
> (gdb) print "ARRRRHHGGGG!"

The problem is that, after the traced program reached a breakpoint,
the instruction pointer must be decremented by one. gdb reads the
registers using ptrace(2), decrements eip and trys to write them
back. This fails because the struct reg as expected by ptrace(2) has
16 entries, but only the first 14 are initialized by gdb. The last two
(fs, gs) contain garbage.

There are already two PRs about this bug in gnats (1651, 1703), and at
least the first one contains the fix (among other things).

Here is my fix, which solved this problem for me:


--- src/gnu/usr.bin/gdb/gdb/arch/i386/i386b-nat.c.old	Wed Nov  1 03:11:47 1995
+++ src/gnu/usr.bin/gdb/gdb/arch/i386/i386b-nat.c	Wed Nov  1 03:10:41 1995
@@ -43,7 +43,7 @@
   ptrace (PT_GETREGS, inferior_pid,
 	  (PTRACE_ARG3_TYPE) &inferior_registers, 0);
 
-  memcpy (&registers[REGISTER_BYTE (0)], &inferior_registers, 4*14);
+  memcpy (&registers[REGISTER_BYTE (0)], &inferior_registers, NUM_REGS * 4);
 
   registers_fetched ();
 }
@@ -54,7 +54,7 @@
 {
   struct reg inferior_registers;
 
-  memcpy (&inferior_registers, &registers[REGISTER_BYTE (0)], 4*14);
+  memcpy (&inferior_registers, &registers[REGISTER_BYTE (0)], NUM_REGS * 4);
 
   ptrace (PT_SETREGS, inferior_pid,
 	  (PTRACE_ARG3_TYPE) &inferior_registers, 0);


Jochen

-------------------------------------------------------------------------------
Jochen Pohl, SNAT NC32			Email:	  jpo.drs@sni.de
Siemens-Nixdorf Advanced Technologies	
Scharfenberger Str. 66			Phone:    +49-351-844 2261
D-01133 Dresden				Fax:      +49-351-844 2011