やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

MinGWでデバッグする(GDB)

MinGWデバッグするにはgdb.exeを使う。

手順

  1. ソースコードを書く(前回のを使う)
  2. デバッグ情報を付与してコンパイルする
  3. gdb.exeでコンパイルしたexeを実行する

コマンド

g++ -g -O0 hello.cpp -o hello_debug -mwindows
gdb hello_debug

上記の例は、g++でデバッグ情報を付与してコンパイルしている。 2行目はgdbで指定のexeをデバッグする。

オプション 意味
-g exeにデバッグ情報を追加する
-g3 GDB内でマクロを使えるようにする
-O0 最適化を無効にする

こちらを参考にさせていただきました。

デバッグ

gdbを開始すると、特定のコマンドを入力してデバッグする。

  1. (gdb)と表示されたとき、break mainと入力してEnterキーを押下する。これでmain関数にブレークポイントを張ったことになる。
  2. (gdb)と表示されたとき、runと入力してEnterキーを押下する
  3. (gdb)と表示されたとき、nextと入力してEnterキーを押下する。ステップアウト実行する
  4. (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というデバッガが存在するということを把握しておく。

MAKE、コンパイラ、デバッガを知っておけば、VC++のようなIDEがなくても何とか開発できそうな気がする。