Debug embedded application over GDB Server by VS Code
You can debug target Linux process in VS Code over remote gdbserver.
This page is done base on chatGPT + manual tweak to make it work.
Debugging an embedded application over GDB Server in Visual Studio Code (VS Code) involves setting up the necessary configurations and using the GDB extension for VS Code. Here's an example of how to debug an embedded application using VS Code and GDB Server (my setup is windows vs code -> wsl2 ubuntu 22.04 -> SAM9X60 Curiosity at 10.0.0.4
Prerequisites:
An embedded target device connected to your development machine. (Our SAM9X60 Curiosity is connected by Ethernet 10.0.0.4)
Follow vendor procedure to flash the image to target and make sure GDB Server installed and running on your target machine.
Include GDB server by update /poky-distro/build/conf/local.conf ”
EXTRA_IMAGE_FEATURES ?= "debug-tweaks tools-profile tools-debug"
”Or you can use a prebuild if you do not want to rebuild yocto image
VS Code installed in host (mine is Ubuntu 22.04 WSL2)
In host install the cross compiler for your target
Install the SDK (recommended) from yocto build
# download the sdk $ wget path-to-your-sdk.sh $ chmod +x your-sdk.sh # install the SDK $ ./your-sdk.sh Poky (Yocto Project Reference Distro) SDK installer version 4.0.6 ================================================================= Enter target directory for SDK (default: /opt/poky-atmel/4.0.6): You are about to install the SDK to "/opt/poky-atmel/4.0.6". Proceed [Y/n]? Y [sudo] password for your-user-name: Extracting SDK.......................................................................................................................................................................................done Setting it up...done SDK has been successfully set up and is ready to be used. Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g. # configure the shell session $ . /opt/poky-atmel/4.0.6/environment-setup-armv5e-poky-linux-gnueabi # verify the gdb version $ arm-poky-linux-gnueabi-gdb --version GNU gdb (GDB) 11.2 Copyright (C) 2022 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.
Step 1: Install GDB Debug Extension in VS Code
Open VS Code.
Go to the Extensions view by clicking on the square icon in the left sidebar or using the shortcut
Ctrl+Shift+X
.Search for the "C/C++" extension by Microsoft, which includes c/c++ support. Install it.
Search for the “webfreak.debug” extension which includes GDB support. Install it.
Step 2: Open Your Project in VS Code
Because our code is build in docker at /source, so we need open our git local copy in the same path.(my code is within
~/git/
your-project ) by create a symbol linkOpen your embedded project in /source in VS Code.
sudo ln -s ~/git/your-project /source
Step 3: Configure Launch Settings
Create a
.vscode
folder in your project directory if it doesn't already exist.Inside the
.vscode
folder, create alaunch.json
file to configure your debugging settings. Here's an example of alaunch.json
file:
{
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "attach",
"name": "Attach to gdbserver",
"executable": "/source/build/OSImage/home/your-binary, //your local path to the binary
"target": "10.0.0.4:9999", //your target's IP and port
"remote": true,
"gdbpath": "/opt/poky-atmel/4.0.6/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gdb", //path to cross-compiler gdb
"cwd": "${workspaceFolder}",
//"preLaunchTask": "startGDBServer",
"postDebugTask": "stopGDBServer",
"valuesFormatting": "parseText",
"showDevDebugOutput": true
}
]
}
Make sure to replace "your local path to the binary"
with the path to your compiled embedded application and "path to cross-compiler gdb"
with the path to your GDB executable. You should also replace "target's IP and port"
with the appropriate GDB Server host and port information.
Step 4: Configure a Debug Task
Create a
tasks.json
file in the.vscode
folder of your project. This file will define a task to start the GDB Server. Here's an exampletasks.json
file:
{
"version": "2.0.0",
"tasks": [
{
"label": "cleanRebuild",
"type": "shell",
"command": "make -f MakefileDocker clean build",
"args": [
],
"group": {
"kind": "build",
"isDefault": true //default build task
},
"isBackground": false,
"problemMatcher": []
},
{
"label": "transferToTarget", //Terminal -> Run Task... -> transferToTarget
"type": "shell",
"command": "scp",
"args": [
"/source/build/your-binary",
"root@10.0.0.4:/home/your-binary"
],
"group": {
"kind": "build",
"isDefault": false
},
"isBackground": true
},
{
"label": "startGDBServer", //Terminal -> Run Task... -> startGDBServer
"type": "shell",
"command": "ssh",
"args": [
"-o StrictHostKeychecking=no",
"root@10.0.0.4",
"gdbserver",
":9999",
"/home/your-binary"
],
"group": {
"kind": "build",
"isDefault": false
},
"dependsOn": "transferToTarget",
"isBackground": true
},
{
"label": "stopGDBServer", //Terminal -> Run Task... -> stopGDBServer
"type": "shell",
"command": "ssh",
"args": [
"-o StrictHostKeychecking=no",
"root@10.0.0.4",
"pkill",
"-f",
"gdbserver"
],
"group": {
"kind": "build",
"isDefault": false //will be executed when stop debugging
}
}
]
}
You can manually run the tasks by
Terminal - > Run Task… ->
transferToTarget
: copy your img to targetstartGDBServer
: start gdb server on target with your applicationstopGDBServer
: stop gdb server after debug
Step 5: Debug Your Application
Set breakpoints in your code where you want to start debugging.
Terminal - > Run Task… →
stopGDBServer
if you have already a debug session.Terminal - > Run Task… →
transferToTarget
if your application has been updated in hostTerminal - > Run Task… →
startGDBServer
Click the Run “Start Debugging" icon in the left sidebar or use the shortcut
F5
.Select the "
Attach to gdbserver
" configuration from the dropdown.Click the green play button to start debugging.
VS Code will connect to the GDB Server, load your application, and stop at breakpoints, allowing you to inspect variables, step through code, and debug your embedded application.
Remember to customize the configurations and paths according to your specific setup and hardware.
Comments
Post a Comment