[patch] Cut memory address width

Jan Kratochvil jan.kratochvil@redhat.com
Wed Sep 27 16:15:00 GMT 2006


Hi,

`x/x $ebx' on gdb/amd64 debugging inferior/i386 causes Cannot access memory at
address 0xffffce70 (or so) as $ebx is considered `int' and sign-extended to
64-bit while the resulting address 0xffffffffffffce70 fails to be accessed.

$esp does not exhibit this problem as it is `builtin_type_void_data_ptr' not
`builtin_type_int' as $ebx is.  Therefore it gets extended as unsigned.

Simulate the part of paddress(); it is questionable how deep in the functions
calling stack the address width cut should be.


Regards,
Jan


As bugreported by John Reiser <jreiser(at)BitWagon.com>:

When debugging a 32-bit executable on x86_64, gdb does not allow examining the stack if pointed to by a non-$esp register.  For example,
-----foo.S
_start: .globl _start
        nop
        int3
        movl %esp,%ebx
        int3  # examining memory from $ebx fails, from $esp succeeds
        nop
        nop
-----
$ gcc -m32 -o foo -nostartfiles -nostdlib foo.S
$ gdb foo
Program received signal SIGTRAP, Trace/breakpoint trap.
0x08048076 in _start ()
(gdb) x/i $pc
0x8048076 <_start+2>:   mov    %esp,%ebx
(gdb) stepi
0x08048078 in _start ()
(gdb) x/x $esp
0xffffce70:     0x00000001
(gdb) x/x $ebx
0xffffce70:     Cannot access memory at address 0xffffce70
(gdb) x/x 0xffffce70
0xffffce70:     0x00000001

Expected Results:  "x/x $ebx" should have succeeded, too, when %ebx has the
same value as %esp and examining from $esp works.
-------------- next part --------------
2006-09-27  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* target.c (target_read_memory): Cut memory address to the target's
	address bit size, bugreport by John Reiser.
	(target_write_memory): Likewise.
		
		
Index: gdb/target.c
===================================================================
RCS file: /cvs/src/src/gdb/target.c,v
retrieving revision 1.126
diff -u -p -r1.126 target.c
--- gdb/target.c	21 Sep 2006 14:00:53 -0000	1.126
+++ gdb/target.c	27 Sep 2006 16:01:27 -0000
@@ -1032,6 +1032,16 @@ target_xfer_partial (struct target_ops *
 int
 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
 {
+  /* `x/x $ebx' on gdb/amd64 debugging inferior/i386 causes
+     Cannot access memory at address 0xffffce70
+     as $ebx is considered `int' and sign-extended to 64-bit.
+     $esp does not exhibit this problem as it is `builtin_type_void_data_ptr',
+     not `builtin_type_int' as $ebx is.
+     Simulate the part of paddress() here.  */
+  int addr_bit = TARGET_ADDR_BIT;
+  if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
+    memaddr &= ((CORE_ADDR) 1 << addr_bit) - 1;
+
   if (target_read (&current_target, TARGET_OBJECT_MEMORY, NULL,
 		   myaddr, memaddr, len) == len)
     return 0;
@@ -1042,6 +1052,11 @@ target_read_memory (CORE_ADDR memaddr, g
 int
 target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
 {
+  /* See `target_read_memory' above.  */
+  int addr_bit = TARGET_ADDR_BIT;
+  if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
+    memaddr &= ((CORE_ADDR) 1 << addr_bit) - 1;
+
   if (target_write (&current_target, TARGET_OBJECT_MEMORY, NULL,
 		    myaddr, memaddr, len) == len)
     return 0;


More information about the Gdb-patches mailing list