VSCodeでc言語のデバッグ_Remote-WSLを利用


環境構築

下記のリンクに従い、Remote-WSL環境でC言語プログラムが出来るまでの環境構築を行います。

Visual Studio Codeで競プロ環境構築(導入編)
VSCodeのインストール、WSL・Remote-WSLの導入、WSLにコンパイラ・デバッグをインストール

VSCodeでの競プロ向けC++環境をWSLにRemote Developmentする形で作る
C++拡張機能をWSLにインストール

C言語用デバッグ設定

launch.jsonとtasks.jsonを作成

フォルダ、ソースを準備します。
.vscodeが作成されていない状態です。
    
F5を押下後、C++(GDB/LLDB)をクリックします。
    
gcc-~をクリックします。
    
.vscodeが作成され、その中にlaunch.jsonとtask.jsonの2ファイルが作成されます。
    

launch.jsonとtasks.jsonの内容(実行形式ファイル名:ソース名の拡張子を除いた名称)

ソース名がmain.cの場合、実行形式ファイルの名称は、mainになります。
launch.jsonとtasks.jsonは自動生成されたファイルから変更する必要はありません。

launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

launch.jsonとtasks.jsonの内容(実行形式ファイル名:a.out)

launch.json 修正後(programのみ修正)
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

最適化をオフにし、コンパイルが早くなる様に、-O0オプションを付けています。

tasks.json 修正後(argsのみ修正)
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "-O0",
                "${file}",
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

デバッグ

ブレークポイントを設定し、F5、F11、F10を押下することでデバッグ可能です。
以下に、デバッグ時に発生した問題について記します。

scanf行でデバッグ停止している時にステップインをするとエラーが発生

下記状態でステップイン(F11押下)
    
下記、エラーが出ます。
    
ステップインではなく、ステップオーバー(F10押下)するとエラーは出ません。