GDB in Rust Web Development

Overview

GDB (GNU Debugger) is a powerful tool used to inspect and interact with Rust applications. It allows developers to set breakpoints, run applications, and print variables, providing a deeper understanding of the application's behavior during execution. However, it's important to note that GDB is not supported on ARM-based macOS systems, where LLDB is recommended instead.

Installation

GDB is not included with the Rust installation by default. It is an independent tool that can be installed in various ways depending on your operating system. For Intel-based macOS systems, GDB can be installed using Homebrew with the following command:

brew install gdb

On Arch Linux, GDB can be installed using the package manager with:

pacman -S gdb

For ARM-based macOS systems, such as those with Apple M1 chips, GDB is not supported. In these cases, LLDB should be used as it is the default debugger on macOS.

Using GDB with Rust

Rust provides a command-line tool called rust-gdb, which is a wrapper around GDB. This tool is part of the default Rust installation and is designed to work seamlessly with Rust applications. If GDB is not installed on your system, attempting to run rust-gdb will result in an error message indicating that GDB is not found.

Basic Workflow with GDB

To start debugging a Rust application with GDB, follow these steps:

  1. Build the Application: First, compile your application using Cargo.

    $ cargo build
    
  2. Start GDB: Use the rust-gdb command to start GDB with your application's binary.

    $ rust-gdb target/debug/NAME_OF_YOUR_APPLICATION
    
  3. Set Breakpoints: Use the b command to set breakpoints at specific lines or functions.

    (gdb) b main:11
    (gdb) b src/routes/question.rs:11
    
  4. Run the Application: Use the r command to run the application. It will stop at the first breakpoint.

    (gdb) r
    
  5. Continue Execution: After hitting a breakpoint, you can continue execution with the c command or inspect variables using the p command.

    (gdb) c
    (gdb) p store
    

This workflow provides a basic run-through of your application using GDB. For a more visual debugging experience, consider using an Integrated Development Environment (IDE) like Visual Studio Code, which offers a built-in debugger interface.

LLDB as an Alternative

For ARM-based macOS systems, LLDB is the recommended debugger. The following listing demonstrates starting a web service using LLDB and setting breakpoints:

Listing 6.29 Starting our web service via LLDB and using breakpoints

$ cargo build                                                          #1
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
 
$ lldb target/debug/practical-rust-book                                #2
(lldb) target create "target/debug/practical-rust-book"
Current executable set to '/target/debug/practical-rust-book' (arm64).
(lldb) b add_question                                                  #3
Breakpoint 1: where = practical-rust-... + 48 at question.rs:58:48, 
    address = 0x0000000100102f54
(lldb) breakpoint list                                                 #4
Current breakpoints:
1: name = 'add_question', locations = 1
  1.1: where = practical-rust-... 48 at question.rs:58:48, 
    address = practical-rust-book[0x0000000100102f54], 
    unresolved, hit count = 0
 
(lldb) r                                                               #5
Process 96335 launched: '..../target/debug/practical-rust-book' (arm64)
2022-05-02T17:37:44.327462Z  INFO get_questions request
    {method=POST path=/questions id=8feb7e42-cc15-4bde-a34e-97878c2d3cc4}:
    practical_rust_book: close time.busy=61.0µs time.idle=89.6µs
Process 96335 stopped                                                  #6
* thread #11, name = 'tokio-runtime-worker', stop reason = breakpoint 1.1
    frame #0: 0x0000000100102f54 practical-rust-book`practical_rust_book
        ::routes::question::add_question::hd2aa84941d704842
        (store=Store @ 0x000000017125cee0, 
            question=Question @ 0x000000017125cf28
        ) at question.rs:58:48
   55       pub async fn add_question(
   56           store: Store,
   57           question: Question,
-> 58       ) -> Result<impl warp::Reply, warp::Rejection> {
   59           store
   60               .questions
   61               .write()
Target 0: (practical-rust-book) stopped.
(lldb) frame variable                                                  #7
(practical_rust_book::store::Store) store = {
...
 
  title = {
    vec = {
      buf = {
        ptr = (pointer = "question title", 
       _marker = core::marker::PhantomData<unsigned char> 
            @ 0x000000017125cf40)
        cap = 9
        alloc = {}
      }
      len = 9
    }
  }
  content = {
    vec = {
      buf = {
        ptr = (pointer = "question content", _
        marker = core::marker::PhantomData<unsigned char> 
            @ 0x000000017125cf58)
        cap = 19
        alloc = {}
      }
      len = 19
    }
  }
  tags = {}
}
(lldb) process continue                                                #8

This listing illustrates the use of LLDB to debug a Rust application, highlighting the process of setting breakpoints, running the application, and inspecting variables.