Skip to content

vscode-debug-specs/cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Debug C/C++ with VS Code

Summary

Basic

Spec

  • OS
    • âś… MacOS
    • âś… Windows  * âś… Linux
  • Break Point
    • âś… break point
    • âś… condition break point
    • âś… function breakpoint
  • Step Execution
    • âś… Step Over
    • âś… Step Into
    • âś… Step Out
    • âś… Continue
    • ❌ Step Back
    • ❌ Move To
    • ❌ Pause
  • Variables
    • âś… variables views
    • âś… watch variables
  • Call Stack
    • âś… call stack
  • Evaluation
    • âś… eval expression to show variables
    • âś… eval expression to change variables
  • Type of Execution
    • âś… debug unit test
    • âś… debug executable package
    • âś… attach debug process (but cannot work on MacOS)
    • âś… remote debugging (but cannot start server on MacOS)

Instruction

MacOS

  • install XCode
  • install Extension C/C++

Windows

  • install Visual Studio 2017
  • install Extension C/C++

Linux

  • install gcc: sudo apt get -y gcc
  • install Extension C/C++

debugging unit test (cunit)

MacOS instruction

brew install cunit

Ubuntu instruction

sudo apt install libcunit1 libcunit1-dev

Windows instruction

HELP Wanted

launch.json

menu: C/C++: Launch

{
  "version": "0.2.0",
  "configurations": [
    {
      // for MacOS
      "name": "(lldb) Launch cunit",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/bubble_sort_cunit",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "preLaunchTask": "build cunit",
      "MIMode": "lldb"
    },
    {
      // for Linux
      "name": "(gdb) Launch cunit",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/a.out",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "preLaunchTask": "build cunit",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

how-to

  1. build cunit
gcc bubble_sort.c bubble_sort_cunit.c -g -O0 -W -Wall -lcunit
  1. Start "launch cunit"

  2. new window is opened, and Run cunit

./a.out
***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: R

gcc option for debugging

  • -W and -Wall : show warnings (not must)
  • -g : debug
  • -O0 : no optimisation
  • -lcunit : load cunit

debugging executable file (MacOS, Linux)

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      // MacOS
      "name": "Launch Program(lldb)",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/a.out",
      "args": ["4", "3", "2", "1"],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "lldb"
    },
    {
      // Linux
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/a.out",
      "args": ["4", "3", "2", "1"],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

how-to

  1. build with -g -O0 option
gcc bubble_sort.c main.c -g -O0 -W -Wall
  1. Start "Launch Program"

debugging execute file (Windows)

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceRoot}/main.exe",
      "args": ["4", "3", "2", "1"],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true
    }
  ]
}

how-to

  1. Start Developer Command Prompt and build with /ZI option
cl main.c bubble_sort.c /ZI
  1. Start debug.

attach to process

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Linux gdb) Attach",
      "type": "cppdbg",
      "request": "attach",
      "program": "${workspaceFolder}/bubble_sort_cunit",
      "processId": "${command:pickProcess}",
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

debug to remote machine (Mac/Linux to Linux)

With pipe transport, you'll debug remote linux from macos.

launch.json

  • set gdb path with sudo
{
  "version": "0.2.0",
  "configurations": [
		{
			"name": "(Mac to Linux)pipe transport",
			"type": "cppdbg",
			"request": "launch",
			"program": "/home/nnyn/Documents/vscode-debug-specs/cpp/main",
			"args": [
				"4",
				"3",
				"2",
				"1"
			],
			"stopAtEntry": false,
			"cwd": "/home/nnyn/Documents/vscode-debug-specs/cpp",
			"environment": [],
			"externalConsole": true,
			"pipeTransport": {
				"pipeCwd": "/usr/bin",
				"pipeProgram": "/usr/bin/ssh",
				"pipeArgs": [
					"nnyn@192.168.56.101"
				],
				"debuggerPath": "sudo /usr/bin/gdb"
			},
			"sourceFileMap": {
				// "remote": "local"
				"/home/nnyn/Documents/vscode-debug-specs/cpp": "${workspaceFolder}"
			},
			"MIMode": "gdb"
		},
	}
 ]

how to

  1. build at remote machine
cd /home/nnyn/Documents/vscode-debug-specs/cpp
gcc -O0 -g -W -Wall -o main bubble_sort.c main.c
  1. launch debug.

attach to remote process (Mac/Linux to Linux)

With pipe transport, you'll attach remote linux process from macos.

launch.json

{
  "version": "0.2.0",
  "configurations": [
		{
			"name": "(Mac to Linux)pipe transport attach",
			"type": "cppdbg",
			"request": "attach",
			"program": "/home/nnyn/Documents/vscode-debug-specs/cpp/bubble_sort_cunit",
			"processId": "21073",
			"pipeTransport": {
				"pipeCwd": "",
				"pipeProgram": "/usr/bin/ssh",
				"pipeArgs": [
					"nnyn@192.168.56.101"
				],
				"debuggerPath": "sudo /usr/bin/gdb"
			},
			"sourceFileMap": {
				// "remote": "local"
				"/home/nnyn/Documents/vscode-debug-specs/cpp": "${workspaceFolder}"
			},
			"MIMode": "gdb"
    }
	}
 ]

how to

  1. build at remote machine
  2. launch program at remote machine
  3. check the process id of remote process
  4. set the process id to launch.json
  5. launch debug

About

How to Debug cpp with VS Code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published