Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* main(), registers and gdb
@ 2007-01-10 23:33 Greg Watson
  2007-01-10 23:39 ` Daniel Jacobowitz
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Watson @ 2007-01-10 23:33 UTC (permalink / raw)
  To: gcc; +Cc: gdb

I have an issue (I hesitate to say a problem) related to register  
saving and debugging on the linux/x86 platform using gdb 4.1.0.

If the following code is compiled with 'gcc -g -O0 -o test test.c',  
the address of argc is passed into func() in the ecx register. Since  
ecx is not preserved after the call to printf(), the address of argc  
is corrupted on return from func(). Normally this would not be a  
problem, since argc is never used in the code.

     #include <stdio.h>
     #include <stdlib.h>

     int func(int *x)
     {
         printf("in func\n");
         return 0;
     }

     int main(int argc, char *argv[])
     {
         func(&argc);
         //func(&argc);;
         printf("hello\n");
         return 0;
     }

However, when run under gdb, commands that view the stack frame  
produce strange results, and some commands (e.g. -var-update)  
actually crash the debugger.

     Breakpoint 1, main (argc=1, argv=0xbffcef14) at test.c:14
     12              func(&argc);
     (gdb) n
     in func
     14              printf("hello\n");
     (gdb) where
     #0  main (argc=Cannot access memory at address 0x4
     ) at test.c:16
     (gdb)

If line 13 is uncommented the problem goes away, apparently because  
the compiler recognizes that argc is used and so must be preserved.  
Also, this problem is not apparent on other x86 platforms (at least  
Darwin), because eax is used instead of ecx.

This problem is of concern when debugging programs because it  
introduces unexpected behavior, even with optimization disabled.

I would appreciate any comments from the gcc and gdb communities on  
this issue, and would be interested to know if there are any compiler  
options and/or other means of disabling this behavior.

Thanks,

Greg



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: main(), registers and gdb
  2007-01-10 23:33 main(), registers and gdb Greg Watson
@ 2007-01-10 23:39 ` Daniel Jacobowitz
  2007-01-10 23:51   ` Greg Watson
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Jacobowitz @ 2007-01-10 23:39 UTC (permalink / raw)
  To: Greg Watson; +Cc: gcc, gdb

On Wed, Jan 10, 2007 at 04:32:48PM -0700, Greg Watson wrote:
> If the following code is compiled with 'gcc -g -O0 -o test test.c',  
> the address of argc is passed into func() in the ecx register. Since  
> ecx is not preserved after the call to printf(), the address of argc  
> is corrupted on return from func(). Normally this would not be a  
> problem, since argc is never used in the code.

This is a typical problem.  There is not much that can be done about
it, although I remember once hearing a proposal that GCC should
forcibly extend the live ranges of local variables (or at least
arguments) at -O0 to improve debugging.  That seems sensible to me.

> However, when run under gdb, commands that view the stack frame  
> produce strange results, and some commands (e.g. -var-update)  
> actually crash the debugger.

A crash is always a bug.

>     Breakpoint 1, main (argc=1, argv=0xbffcef14) at test.c:14
>     12              func(&argc);
>     (gdb) n
>     in func
>     14              printf("hello\n");
>     (gdb) where
>     #0  main (argc=Cannot access memory at address 0x4
>     ) at test.c:16

And honestly, I have no idea how that happened.  Does it happen
with a current GDB?  I suspect from the error message that this
one is not too recent.

-- 
Daniel Jacobowitz
CodeSourcery


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: main(), registers and gdb
  2007-01-10 23:39 ` Daniel Jacobowitz
@ 2007-01-10 23:51   ` Greg Watson
  2007-01-11  2:01     ` Daniel Jacobowitz
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Watson @ 2007-01-10 23:51 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gcc, gdb

On Jan 10, 2007, at 4:38 PM, Daniel Jacobowitz wrote:

> On Wed, Jan 10, 2007 at 04:32:48PM -0700, Greg Watson wrote:
>> If the following code is compiled with 'gcc -g -O0 -o test test.c',
>> the address of argc is passed into func() in the ecx register. Since
>> ecx is not preserved after the call to printf(), the address of argc
>> is corrupted on return from func(). Normally this would not be a
>> problem, since argc is never used in the code.
>
> This is a typical problem.  There is not much that can be done about
> it, although I remember once hearing a proposal that GCC should
> forcibly extend the live ranges of local variables (or at least
> arguments) at -O0 to improve debugging.  That seems sensible to me.

That would be nice. Although this seems like a trivial issue, it can  
potentially effect debugging all MPI programs since they always start  
with 'MPI_Init(&argc, &argv)'.

>
>> However, when run under gdb, commands that view the stack frame
>> produce strange results, and some commands (e.g. -var-update)
>> actually crash the debugger.
>
> A crash is always a bug.

I believe it's gdb bug #2188.

>
>>     Breakpoint 1, main (argc=1, argv=0xbffcef14) at test.c:14
>>     12              func(&argc);
>>     (gdb) n
>>     in func
>>     14              printf("hello\n");
>>     (gdb) where
>>     #0  main (argc=Cannot access memory at address 0x4
>>     ) at test.c:16
>
> And honestly, I have no idea how that happened.  Does it happen
> with a current GDB?  I suspect from the error message that this
> one is not too recent.

It's gdb 6.5, so reasonably recent.

Greg


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: main(), registers and gdb
  2007-01-10 23:51   ` Greg Watson
@ 2007-01-11  2:01     ` Daniel Jacobowitz
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Jacobowitz @ 2007-01-11  2:01 UTC (permalink / raw)
  To: Greg Watson; +Cc: gcc, gdb

On Wed, Jan 10, 2007 at 04:50:59PM -0700, Greg Watson wrote:
> That would be nice. Although this seems like a trivial issue, it can  
> potentially effect debugging all MPI programs since they always start  
> with 'MPI_Init(&argc, &argv)'.

See my reply to the bug.  This is specific to i686 32-bit compilers,
arguments, and functions named "main".

> >And honestly, I have no idea how that happened.  Does it happen
> >with a current GDB?  I suspect from the error message that this
> >one is not too recent.
> 
> It's gdb 6.5, so reasonably recent.

Please try a current snapshot.  Thanks.

-- 
Daniel Jacobowitz
CodeSourcery


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-01-11  2:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-10 23:33 main(), registers and gdb Greg Watson
2007-01-10 23:39 ` Daniel Jacobowitz
2007-01-10 23:51   ` Greg Watson
2007-01-11  2:01     ` Daniel Jacobowitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox