LoginSignup
2
4

More than 5 years have passed since last update.

macOS High Sierra以降でvscode + gdb

Last updated at Posted at 2019-02-19

今更ではあるが新環境を用意する際に忘れたりするので書いておく

gdbをインストールする

gdb doesn't work on macos High Sierra 10.13.3 に記述の通り^8.1では動作しないので
8.0.1を持ってくる

既にgdbをインストールしていた場合
brew unlink gdbでunlinkしておく

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/9ec9fb27a33698fc7636afce5c1c16787e9ce3f3/Formula/gdb.rb

brewのアップデート対象からgdbを除外

brew pin gdb

System Integrity Protectionを無効化していない場合は以下を実行する

echo "set startup-with-shell off" >> ~/.gdbinit

gdbを証明する

こちらの記事 の通りに行えばOK

動作チェック

適当にgistからHello Worldを拾ってきて走らせてみる

pushd `mktemp -d`
wget https://gist.githubusercontent.com/jonathonw/1725966/raw/492f0313ab609d27fec543042d58903d57f55689/HelloWorld.cpp
g++ -g ./HelloWorld.cpp -o test
sudo gdb ./test
(gdb) run

無事Hello Worldが出ればgdbの準備は完了

vscodeでデバッグ

単純にcodeで起動してもgdbは使えないのでsudo権限で起動する必要がある
ちょっと厄介なのでaliasにしておくと良い

bash

alias code='sudo code --user-data-dir="$HOME/.vscode-root" $@'

fish

function code
    sudo code --user-data-dir='$HOME/.vscode-root' $argv
end

aliasを設定したら、先ほどのHelloWorldがあるディレクトリでvscodeを開く

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb"
        }
    ]
}

${workspaceFolder}/.vscode/launch.jsonに上記設定を貼り付けてF5で実行
DEBUG CONSOLEに以下のように出力されれば成功

...省略
Thread 2 hit Breakpoint 1, main () at ./HelloWorld.cpp:5
5     std::cout << "Hello World!" << std::endl;
Execute debugger commands using "-exec <command>", for example "-exec info registers" will list registers in use (when GDB is the debugger)
Hello World!
[Inferior 1 (process 74786) exited normally]
...省略
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4