Debug embedded application over GDB Server by VS Code

 You can debug target Linux process in VS Code over remote gdbserver.

image-20231107-141631.png

This page is done base on chatGPT + manual tweak to make it work. :slight_smile:

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:

  1. An embedded target device connected to your development machine. (Our SAM9X60 Curiosity is connected by Ethernet 10.0.0.4)

  2. Follow vendor procedure to flash the image to target and make sure GDB Server installed and running on your target machine.

    1. Include GDB server by update /poky-distro/build/conf/local.conf ”EXTRA_IMAGE_FEATURES ?= "debug-tweaks tools-profile tools-debug"

    2. Or you can use a prebuild if you do not want to rebuild yocto image

  3. VS Code installed in host (mine is Ubuntu 22.04 WSL2)

  4. In host install the cross compiler for your target

    1. 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

  1. Open VS Code.

  2. Go to the Extensions view by clicking on the square icon in the left sidebar or using the shortcut Ctrl+Shift+X.

  3. Search for the "C/C++" extension by Microsoft, which includes c/c++ support. Install it.

  4. Search for the “webfreak.debug” extension which includes GDB support. Install it.

Step 2: Open Your Project in VS Code

  1. 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 link

  2. Open your embedded project in /source in VS Code.

sudo ln -s ~/git/your-project /source

Step 3: Configure Launch Settings

  1. Create a .vscode folder in your project directory if it doesn't already exist.

  2. Inside the .vscode folder, create a launch.json file to configure your debugging settings. Here's an example of a launch.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

  1. 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 example tasks.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… ->

  1. transferToTarget: copy your img to target

  2. startGDBServer: start gdb server on target with your application

  3. stopGDBServer: stop gdb server after debug

Step 5: Debug Your Application

  1. Set breakpoints in your code where you want to start debugging.

  2. Terminal - > Run Task… → stopGDBServer if you have already a debug session.

  3. Terminal - > Run Task… → transferToTarget if your application has been updated in host

  4. Terminal - > Run Task… → startGDBServer

  5. Click the Run “Start Debugging" icon in the left sidebar or use the shortcut F5.

  6. Select the "Attach to gdbserver" configuration from the dropdown.

  7. 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

Popular posts from this blog

Capture VLAN tag with wireshark

How to print your log in Vxworks6.x