Cross Compilation & Manual Remote Debugging

Note

This is written by a Manjaro user. The package names is a bit different for other distros. If you know some package names in other distros, please use the repos GitHub issues page to tell me about it.

Cross Compiling the Binary

This is written in 2024. Currently most desktop PCs/laptops are running x86_64 CPUs and when dealing with embedded Linux it’s mostly some version of ARM. That’s what we’re gonna set up the project for.

For a rust project Cargo.toml is the place for platform independent things, while .cargo/config.toml is for thins that are platform specific. Place these lines in .cargo/config.toml inside of your Rust repository.

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

[build]
target = "aarch64-unknown-linux-gnu"

For the compilation to work you need the ARM tools installed. On manjaro this can be acquired with pacman -S aarch64-linux-gnu-binutils aarch64-linux-gnu-gcc aarch64-linux-gnu-gdb aarch64-linux-gnu-glibc aarch64-linux-gnu-linux-api-headers.

You may also need aarch64-linux-gnu-pkg-config from AUR.

With this you should now be able to compile into an ARM binary with

$ cargo build

Verify that you have the binary, and check its size:

$ ls -lh target/aarch64-unknown-linux-gnu/debug/

Preparing the Remote Debug Session

On the Remote

If your using some sort of Ubuntu/Debian/Armbian systems then run

$ sudo apt install gdbserver openssh-server

On the Host

A version of Gdb which can handle the binary format of your target platform is needed. For Manjaro/Arch users it’s available as aarch64-linux-gnu-gdb.

Creating a Session

The numbers used here match the the example session in the image at the bottom of the page.

  1. First the binary needs to get onto the remote:
$ scp target/aarch64-unknown-linux-gnu/debug/rust-proj user@10.23.34.62:~/
  1. Then, in an ssh session to your remote machine, start a gdbserver session with the binary. 2000 is the port number.
$ gdbserver :2000 ~/rust-proj
  1. Launch gdb at the host, using the version matching your binaries format. The option --silent removes the start-up message.
$ aarch64-linux-gnu-gdb target/aarch64-unknown-linux-gnu/debug/rust-proj --silent
  1. Don’t know what this is. Tried saying yes a few times, never seemed to work.

  2. Set up a breakpoint.

  3. Run to the breakpoint.

  4. Note that the execution is happening on the remote machine. If you let the program run to the end you’ll see the output of uname -a.

This is Rachel. Look at her. She clearly knows things.

This is Rachel. Look at her. She clearly knows things.

VS Code Configuration \(\rightarrow\)