手順
コマンド
g++ -g -O0 hello.cpp -o hello_debug -mwindows
gdb hello_debug
上記の例は、g++でデバッグ情報を付与してコンパイルしている。 2行目はgdbで指定のexeをデバッグする。
オプション | 意味 |
---|---|
-g | exeにデバッグ情報を追加する |
-g3 | GDB内でマクロを使えるようにする |
-O0 | 最適化を無効にする |
こちらを参考にさせていただきました。
デバッグ
(gdb)
と表示されたとき、break main
と入力してEnterキーを押下する。これでmain関数にブレークポイントを張ったことになる。(gdb)
と表示されたとき、run
と入力してEnterキーを押下する(gdb)
と表示されたとき、next
と入力してEnterキーを押下する。ステップアウト実行する(gdb)
と表示されたとき、quit
と入力してEnterキーを押下する。デバッグを終了する
実行ログ
>g++ -g -O0 hello.cpp -o hello_debug -mwindows
>gdb hello_debug
GNU gdb (GDB) 7.4
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/>...
Reading symbols from C:\...\hello_debug.exe...done.
(gdb) break main
Breakpoint 1 at 0x4079fe
(gdb) run
Starting program: C:\...\hello_debug.exe
[New Thread 2264.0xf00]
Breakpoint 1, 0x004079fe in main ()
(gdb) next
Single stepping until exit from function main,
which has no line number information.
warning: Call C++ Class Function.(OutputDebugString)
0x004013e2 in __tmainCRTStartup ()
(gdb) next
Single stepping until exit from function __tmainCRTStartup,
which has no line number information.
[Inferior 1 (process 2264) exited normally]
(gdb) next
The program is not being run.
(gdb) next
The program is not being run.
(gdb) next
The program is not being run.
(gdb) quit
所感
今回のログは何のデバッグ作業にもなっていないのでわかりにくい。
もっと詳しいことは、いずれまたやろう。 今はとりあえず、MinGWにもGDBというデバッガが存在するということを把握しておく。