Skip to content

Instantly share code, notes, and snippets.

@aakashpk
Last active March 21, 2024 08:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aakashpk/e90d4651b074248b4823f6d2dc3373a0 to your computer and use it in GitHub Desktop.
Save aakashpk/e90d4651b074248b4823f6d2dc3373a0 to your computer and use it in GitHub Desktop.
VScode setup to do visual debugging

Installing VSCode

Post Install Extensions to be added

  • Add C/C++ Intellisense extension
  • Git extension for easy commits
  • Doxygen commenting extension is also pretty cool.
  • There is VIM key support extensions

Configuring Build

  • In VScode menu bar, Go to Terminal > Configure Tasks and click on the create tasks.json file > and select external command
  • This will add a default tasks.json file in the .vscode directory in your project folder.

Modify the tasks.json to something similar to, most of it is already populated you just have to change the commands for build with your makefile

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"label": "HOST BUILD",
			"type": "shell",
			"command": "make all"
		},
		{
			"label": "BBB BUILD",
			"type": "shell",
			"command": "make all PLATFORM=BBB",
			"group": {
				"kind": "build",
				"isDefault": true
			}
		},
		{
			"label": "Clean",
			"type": "shell",
			"command": "make clean"
		}
	]
}
  • This gives you 3 tasks HOST_BUILD, BBB_BUILD and Clean. Ctrl+Shift+B to run default build task.

  • Run the tasks from Terminal Run task and select task to run.

  • Can also set up shortcut keys for the tasks.

Configuring GDB Debug

Go to Debug, Open Configurations, Choose C++.

Change file to something like this for native debugging.

{
	// 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": "(gdb) Launch",
			"type": "cppdbg",
			"request": "launch",
			"program": "${workspaceFolder}/target",
			"args": [],
			"stopAtEntry": true,
			"cwd": "${workspaceFolder}",
			"environment": [],
			"externalConsole": true,
			"MIMode": "gdb",
			"setupCommands": [
				{
					"description": "Enable pretty-printing for gdb",
					"text": "-enable-pretty-printing",
					"ignoreFailures": true
				}
			]
		}
	]
}

For remote debugging we need 2 additional parameters

  • The gdb we are using has to be gdb multi-arch “miDebuggerPath”
  • And also the server address “miDebuggerServerAddress” set this to gdb server address:port.
  • The resulting config.js file is something similar
{
	// 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": "GDB-HOST",
			"type": "cppdbg",
			"request": "launch",
			"program": "${workspaceFolder}/target",
			"args": [],
			"stopAtEntry": true,
			"cwd": "${workspaceFolder}",
			"environment": [],
			"externalConsole": false,
			"MIMode": "gdb",
			"setupCommands": [
				{
					"description": "Enable pretty-printing for gdb",
					"text": "-enable-pretty-printing",
					"ignoreFailures": true
				}
			]
		},
		{
			"name": "GDB-REMOTE-BBB",
			"type": "cppdbg",
			"request": "launch",
			"program": "${workspaceFolder}/target",
			"miDebuggerServerAddress": "192.168.7.2:4000",
			"miDebuggerPath": "/usr/bin/gdb-multiarch",
			"args": [],
			"stopAtEntry": true,
			"cwd": "${workspaceFolder}",
			"environment": [],
			"externalConsole": true,
			"MIMode": "gdb",
			"setupCommands": [
				{
					"description": "Enable pretty-printing for gdb",
					"text": "-enable-pretty-printing",
					"ignoreFailures": true
				}
			]
		}
	]
}

Add this command at the end of your build (Makefile) or as a prebuild task in vscode to remotely start gdb server on target device

ssh -t root@BBB_ip_address "cd /target_location/ && gdbserver localhost:port target_name"

References:

https://code.visualstudio.com/docs/languages/cpp#_debugging-your-code

https://stackoverflow.com/questions/34057798/how-does-one-set-up-the-visual-studio-code-compiler-debugger-to-gcc

@astrosep
Copy link

astrosep commented Aug 17, 2023

Hello, I am trying to remote debug the same way with gdb multiarch but it stops at listening on port and it does not start the remote debug session. the one you wrote includes two different configs. does that mean you should run the host and then the remote client?

`{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "MyProgram",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/build/MyProgram",
        "miDebuggerServerAddress": "172.16.19.111:2345",
        "miDebuggerPath": "/usr/bin/gdb-multiarch",
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "args": ["-c", "/opt/codes.bix", "/opt/etc/init_srx.cfg"],
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ],
        "preLaunchTask": "startgdbserver"
      },
      {
            "name": "MyProgram-client",
            "type": "cppdbg",
            "request": "launch",
            "program": "/opt/MyProgram",
            "miDebuggerServerAddress": "172.16.19.111:2345",
            "miDebuggerPath": "/usr/bin/gdb-multiarch",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
  }

`

@aakashpk
Copy link
Author

the first config is for local gdb run, the second config for remote gdb. to enable remote debugging you also need to start the gdb server on the remote target using gdbserver localhost:port target_name . that's the command in the gist after the config.

@astrosep
Copy link

Thank you. I am remote debugging. My problem is though that I wanna automate the process. I don’t wanna start the gdbserver from a terminal. I tried to add a task in tasks.json file to do this for me and added in as a preLaunch config but it start listening to the gdbserver and does not start the debug session anymore. Otherwise it works. I wanna see my program running in the vscode instead of terminal.

@aakashpk
Copy link
Author

you can start the gdb server as part of your compile and load process, ie when you copy your executable to the remote target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment