修改参数时gdb打印错误的值

时间:2013-02-06 16:33:06

标签: c gdb

系统

全新安装代码块12.11 + mingw pack。

  • win7 64
  • gcc 4.7.1
  • gdb 7.5

示例代码

使用-g编译,没有优化。

#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\n", b);

}

问题

我在“void foo(int a,int b)”上放了一个断点,当我逐步浏览3行时,我看起来是b的值。 无论是使用代码块调试功能还是使用gdb命令行, b的值为42而不是391 。 控制台输出正确,391。

GDB命令

C:\DebugTest>"C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb.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:\DebugTest\bin\Debug\DebugTest.exe...done.
(gdb) break foo
Breakpoint 1 at 0x401363: file C:\DebugTest\main.c, line 14.
(gdb) run
Starting program: C:\DebugTest\bin\Debug\DebugTest.exe
[New Thread 3596.0x658]

Breakpoint 1, foo (a=400, b=42) at C:\DebugTest\main.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\n", b);
(gdb) print b
$3 = 42
(gdb)

说明

  • 相同的操作完成而没有一个函数时,在main中有a和b作为局部变量,调试输出正确
  • 使用gcc 4.4.1编译时,调试输出正确

知道可能出现什么问题吗? =)

2 个答案:

答案 0 :(得分:8)

我在gcc bugzilla上搜索并发现了这个错误报告:

Althoug报告是关于gcc 4.8而我正在使用4.7,我尝试了建议的解决方法并且它有效!

使用-fvar-tracking进行编译可让GDB在分配后为b打印正确的值。

答案 1 :(得分:0)

有时优化器比调试器更智能。尝试调试未经优化的代码,或者直接进行反汇编并直接观察硬件寄存器,而不是单步执行C源代码行并观察调试器对变量的概念。