Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

c - gdb prints wrong values when modifying arguments

System

Fresh install of codeblocks 12.11 + mingw pack.

  • win7 64
  • gcc 4.7.1
  • gdb 7.5

Example code

Compiled with -g and no optimization.

#include <stdio.h>

void foo(int a, int b);

int main() {

    foo(400, 42);

    return 0;
}

void foo(int a, int b) {

    a = a - 10;
    b = a + 1;

    printf("y2 %d
", b);

}

Problem

I put a breakpoint on "void foo(int a, int b)" and I look value of b as I step through the 3 lines. Either with the codeblocks debugging features or with the gdb command line, the value of b is 42 instead of being 391. The console output is correct, 391.

GDB commands

C:DebugTest>"C:Program Files (x86)CodeBlocksMinGWingdb.exe"
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32".
For bug reporting instructions, please see:  
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file bin/Debug/DebugTest.exe
Reading symbols from C:DebugTestinDebugDebugTest.exe...done.
(gdb) break foo
Breakpoint 1 at 0x401363: file C:DebugTestmain.c, line 14.
(gdb) run
Starting program: C:DebugTestinDebugDebugTest.exe
[New Thread 3596.0x658]

Breakpoint 1, foo (a=400, b=42) at C:DebugTestmain.c:14
14          a = a - 10;
(gdb) print b
$1 = 42
(gdb) step
15          b = a + 1;
(gdb) print b
$2 = 42
(gdb) step
17          printf("y2 %d
", b);
(gdb) print b
$3 = 42
(gdb)

Remarks

  • When the same operations are done without a function, with a and b as local variables inside main, the debug output is correct.
  • When compiled with gcc 4.4.1, the debug output is correct.

Any idea what could be wrong ? =)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I searched on gcc bugzilla and found this bug report :

Althoug the report is about gcc 4.8 and I'm using 4.7, I tried the proposed workaround and it works !

Compiling with -fvar-tracking allows GDB to print the correct value for b after assignment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...