From: Greg Watson <g.watson@computer.org>
To: gcc@gcc.gnu.org
Cc: gdb@sources.redhat.com
Subject: main(), registers and gdb
Date: Wed, 10 Jan 2007 23:33:00 -0000 [thread overview]
Message-ID: <7E9915AE-D47F-45B0-A922-D0C0C3A1D39A@computer.org> (raw)
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
next reply other threads:[~2007-01-10 23:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-10 23:33 Greg Watson [this message]
2007-01-10 23:39 ` Daniel Jacobowitz
2007-01-10 23:51 ` Greg Watson
2007-01-11 2:01 ` Daniel Jacobowitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7E9915AE-D47F-45B0-A922-D0C0C3A1D39A@computer.org \
--to=g.watson@computer.org \
--cc=gcc@gcc.gnu.org \
--cc=gdb@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox