From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12692 invoked by alias); 10 Jan 2007 23:33:08 -0000 Received: (qmail 12670 invoked by uid 22791); 10 Jan 2007 23:33:06 -0000 X-Spam-Check-By: sourceware.org Received: from elasmtp-masked.atl.sa.earthlink.net (HELO elasmtp-masked.atl.sa.earthlink.net) (209.86.89.68) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 10 Jan 2007 23:33:01 +0000 Received: from [68.166.114.35] (helo=[jb?_??IPv6:::1]) by elasmtp-masked.atl.sa.earthlink.net with asmtp (Exim 4.34) id 1H4mwB-0003Jx-JP; Wed, 10 Jan 2007 18:32:56 -0500 Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <7E9915AE-D47F-45B0-A922-D0C0C3A1D39A@computer.org> Cc: gdb@sources.redhat.com Content-Transfer-Encoding: 7bit From: Greg Watson Subject: main(), registers and gdb Date: Wed, 10 Jan 2007 23:33:00 -0000 To: gcc@gcc.gnu.org X-Mailer: Apple Mail (2.752.3) X-ELNK-Trace: b18dadd04c208faa1aa676d7e74259b7b3291a7d08dfec793a1385ff634716aa911493820291f81a350badd9bab72f9c350badd9bab72f9c350badd9bab72f9c Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2007-01/txt/msg00184.txt.bz2 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 #include 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