Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a statement while debugging in Rust

Tags:

debugging

rust

While Coding in Python, I often do something like

test.py

x = []
breakpoint()
± |master U:5 ?:4 ✗| → python3 test.py 
-> breakpoint()
(Pdb) x
[]
(Pdb) x.append(1)
(Pdb) x
[1]

Is it possible to execute the statement while debugging Rust?

use std::collections::HashMap;

fn main() { 
    let mut contacts = HashMap::new();

    contacts.insert("Daniel", "798-1364");
    contacts.insert("Ashley", "645-7689");
    //set breakpoint here
}

So far, I can execute p contacts in the debug console, the meaning of this output isn't straight to me. What if I want to know the outcome of println!("{:?}", contacts); without writting this line of code in the source file.

And also I want to know the outcome of contacts.insert("Robert", "956-1742"), If I execute expr contacts.insert("Robert", "956-1742") in the debug console, it says error: no field named insert.

like image 379
ComplicatedPhenomenon Avatar asked Jul 03 '21 03:07

ComplicatedPhenomenon


People also ask

Can you debug Rust?

In CLion, Rust debugging works out-of-the-box; in our other IDEs, IntelliJ Rust prompts you to install the Native Debugging plugin and downloads the debugger binary upon the first session. CLion supports two debugger backends, GDB and LLDB, which you can switch between. The other IDEs work with LLDB only.

How do I debug Rust or code?

Open main.rs and click the left gutter in the editor to set a break point on the println! line. It should display as a red dot. To start debugging, use either the Rust Analyzer: Debug command or select the Debug CodeLens about main() .

Can you use GDB with Rust?

GDB has support for several languages, such as C/C++, but also modern languages such as Go and Rust.

What is Rust LLDB?

LLDB provides a modern, high performance debugger framework and is the default debugger for macOS and iOS.


Video Answer


1 Answers

Rust debugging support is currently quite limited with both lldb and gdb. Simple expressions work, anything more complex is likely to cause problems.

My experience while testing this with rust-lldb:

  • The expression parser only understands a limited subset of Rust. Macros are not supported.
  • Functions that are not used are not included in the resulting binary. E.g. you cannot call capacity() on the HashMap in the debugger since that function is not included in the binary.
  • Methods have to be called like this: struct_value.method(&struct_value)
  • I haven't found a way to call monomorphized methods on generic structs (like HashMap).
  • String constants "..." in lldb expressions are created as C-style string constants, e.g. "abcdef" is a const char [7] including the trailing NUL byte. Thus they cannot be easily passed to Rust functions expecting &str arguments.

Trying to use a helper function like this:

pub fn print_contacts(contacts: &HashMap<&str, &str>) {
    println!("{:?}", contacts);
}

causes lldb to crash:

(lldb) expr print_contacts(&contacts)
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.  Program arguments: [...]
Segmentation fault: 11

So it seems that what you want to do is not possible at this time.

like image 105
HHK Avatar answered Oct 21 '22 04:10

HHK