在 gdb 中看不到变量

在 gdb 中看不到变量

我是 cygwin (和 *nix) 的新手,不了解以下 gdb 行为。我创建了一个故意导致 SIGSEGV 的可执行文件:

#include <iostream>

void Func(int i)
{
    int* pFoo = NULL;
    *pFoo = 1;
}

int main(int argc, char** argv)
{
    Func(-50);
    std::cout << "End of main()" << std::endl;
}

我通过执行以下操作来编译代码:

g++ test.cpp

使用此版本的 g++:

g++ --版本

g++ (GCC) 5.4.0 版权所有 (C) 2015 Free Software Foundation, Inc. 这是免费软件;请参阅来源以了解复制条件。不提供任何担保;甚至不提供适销性或特定用途适用性的担保。

当我尝试在 gdb 中运行该程序时,我无法“看到”任何变量:

>gdb ./a.exe
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 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 "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.exe...done.
(gdb) r
Starting program: /home/user/code/gdbtest/a.exe
[New Thread 6500.0x1c84]
[New Thread 6500.0x7a0]

Program received signal SIGSEGV, Segmentation fault.
0x000000010040111d in Func(int) ()
(gdb) bt
#0  0x000000010040111d in Func(int) ()
#1  0x000000010040113c in main ()
(gdb) f 0
#0  0x000000010040111d in Func(int) ()
(gdb) p i
No symbol "i" in current context.
(gdb) p pFoo
No symbol "pFoo" in current context.
(gdb) info locals
No symbol table info available.
(gdb) f 1
#1  0x000000010040113c in main ()
(gdb) p argc
No symbol "argc" in current context.
(gdb)

有人能解释为什么会发生这种情况以及如何解决这个问题吗?

我也尝试使用“g++ -Og test.cpp”进行编译,但结果并没有发生上述变化。

答案1

一般注意事项:调用程序测试是一个坏主意,因为已经有一个测试命令。

更改名称 ;-)

$ g++ -ggdb -O0 prova.cpp -o prova

在哪里:

  • -ggdb添加调试信息
  • -O0禁用所有优化
  • -o为二进制文件提供一个名称(而不是默认值a.out

调试部分将是:

$ gdb prova
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
...
Reading symbols from prova...done.
(gdb) run
Starting program: /tmp/prova/prova
[New Thread 6404.0x404]
[New Thread 6404.0x231c]
[New Thread 6404.0x1960]
[New Thread 6404.0x11b4]

Program received signal SIGSEGV, Segmentation fault.
0x00000001004010f7 in Func (i=-50) at prova.cpp:6
6           *pFoo = 1;
(gdb) bt
#0  0x00000001004010f7 in Func (i=-50) at prova.cpp:6
#1  0x0000000100401122 in main (argc=1, argv=0xffffcc30) at prova.cpp:11

答案2

正如所述GDB 手册(您可能应该先检查一下)您必须告诉编译器包含 GDB 所需的变量信息。-g在编译器行中添加开关:

g++ -g test.cpp

相关内容