Debugging Rust

Rust compiles into machine code the same as C and benefits from sharing the same ABI and compiler backend formats as C/C++.

So you can debug Rust in the same way as C/C++. If you built your Rust executable in a gcc compatible binary format you can just invoke gdb on it:

  1. gdb my_executable

Rust comes with a gdb wrapper script called rust-gdb that loads macros which perform syntax highlighting.

Enabling backtrace

If your code is crashing because of a panic!() you can get a backtrace on the console by setting the RUST_BACKTRACE environment variable.

  1. # Windows
  2. set RUST_BACKTRACE=1
  3. # Unix/Linux
  4. export RUST_BACKTRACE=1

Find out your target binary format

If you are in doubt what you are targeting, you may use rustup to show you.

  1. c:\dev\visu>rustup show
  2. Default host: x86_64-pc-windows-msvc
  3. stable-x86_64-pc-windows-msvc (default)
  4. rustc 1.13.0 (2c6933acc 2016-11-07)

Or perhaps:

  1. [foo@localhost ~]$ rustup show
  2. Default host: x86_64-unknown-linux-gnu
  3. stable-x86_64-unknown-linux-gnu (default)
  4. rustc 1.13.0 (2c6933acc 2016-11-07)

The information will tell you which debugger you can use to debug your code.

Microsoft Visual Studio

If you have the MSVC toolchain (32 or 64-bit) or the LLVM backend will generate a .pdb file and binaries will be compatible with the standard MSVC runtime.

To debug your code:

  1. Open Visual Studio
  2. Choose File | Open | Project/Solution…
  3. Select the compiled executable
  4. Open a source file to debug and set a breakpoint
  5. Click the “Start” button

GDB

GDB can be invoked directly from the command line or through a plugin / IDE. From the command line it’s a

TODO

LLDB

TODO