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

标签 c gdb

系统

全新安装 codeblocks 12.11 + mingw 包。

  • win7 64位
  • 海湾合作委员会 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)

备注

  • 相同的操作没有函数的情况下完成时,a 和 b 作为 main 内部的局部变量,调试输出是正确的
  • 使用 gcc 4.4.1 编译时,调试输出正确

知道哪里出了问题吗? =)

最佳答案

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

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

使用 -fvar-tracking 编译允许 GDB 在赋值后为 b 打印正确的值。

关于c - gdb 在修改参数时打印错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14734078/

相关文章:

c - 为每个 mpi 进程使用不同的 View 写入文件

android - 我看不到使用 gdb 加载的 coredump 中的符号

c++ - 如何在 Eclipse CDT 中为 C++ STL 对象启用 gdb pretty-print ?

c - 位运算符 : Difference between << and <<= or >> and >>= in c

C-Lomuto Quicksort Exe 不起作用

c++ - 在不停止程序的情况下调试程序

gdb - 如何在交互式调试期间突出显示 gdb 输出并为其着色?

c++ - 很奇怪的GDB中的Segmentation fault分析

c - 在 fifo 中同时读取和写入(在后面)

c - 如何使用 Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC 用 C 语言编译此程序