Table of Contents list

Installing gdbserver to the Raspberry Pi

The toolchain includes a tool called gdbserver. This program will run on the Raspberry Pi, and we'll connect to it using gdb on our computer. First install gdbserver to the Pi:

scp ~/GitHub/RPi-Cpp-Toolchain/toolchain/x-tools/armv6-rpi-linux-gnueabihf/armv6-rpi-linux-gnueabihf/debug-root/usr/bin/gdbserver RPi0:~
ssh RPi0 sudo mv gdbserver /usr/local/bin
ssh RPi0 gdbserver --version
GNU gdbserver (crosstool-NG UNKNOWN) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
gdbserver is free software, covered by the GNU General Public License.
This gdbserver was configured as "armv6-rpi-linux-gnueabihf"

Installing extra libraries to the sysroot

By default, Raspberry Pi OS preloads a custom memcpy implementation. When debugging, GDB needs that library in the sysroot as well. Install it using:

echo "deb http://archive.raspberrypi.org/debian/ buster main" | sudo tee /var/lib/schroot/chroots/rpizero-buster-armhf/etc/apt/sources.list.d/raspi.list
wget -qO - https://archive.raspberrypi.org/debian/raspberrypi.gpg.key | sudo schroot -c source:rpizero-buster-armhf -u root -d / -- apt-key add -
sudo sbuild-apt rpizero-buster-armhf apt-get update
sudo sbuild-apt rpizero-buster-armhf apt-get install raspi-copies-and-fills

Installing GDB on your computer

To debug ARM devices, you need an ARM version of GDB. You can install one using:

sudo apt install gdb-multiarch

The toolchain includes armv6-rpi-linux-gnueabihf-gdb, but for some reason, this version of GDB doesn't play well with VSCode.

Running GDB manually from the command line

To make sure everything works correctly, let's start GDB from a command line and debug the program we compiled and copied to the Pi on the previous page:

armv6-rpi-linux-gnueabihf-gdb ./build/hello

Set the sysroot and start gdbserver over SSH. Then type c or continue to run the program:

set sysroot /var/lib/schroot/chroots/rpizero-buster-armhf
target remote | ssh RPi0 gdbserver - '~/hello' --name Pieter
continue

Use q or Ctrl+D to quit GDB.

images/gdb-cmd.png

Debugging using VSCode

The example project includes task.json and launch.json files that automatically copy the binary to the Raspberry Pi and start gdbserver when you hit F5. This allows you to set breakpoints, inspect the call stack and variables, and so on, like you would during a normal native debug session.
The “C/C++” extension by Microsoft is required for debugging support.

images/gdb-vscode.png

For some reason, the standard output of the program is not visible in VSCode. I'll probably open an issue to try and figure this out.