web-dev-qa-db-ja.com

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 ++ --version

g ++(GCC)5.4.0 Copyright(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
StoneThrow

一般的な注意:すでにテストコマンドがあるため、プログラムテストを呼び出すことはお勧めできません。

名前の変更;-)

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

どこ

-ggdb is the option for adding the debug info
-O0 disable all the optimization
-o provide a name to the binary other than the default a.exe

デバッグセクションは次のようになります。

$ 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
matzeri