Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB + Rust, how to print a std::path::Path function call

Tags:

rust

gdb

I want to print the result of a calling a function of a std::path::Path instance.

I'm using Rust 1.43 and GDB 9.1

main.rs

use std::path::Path;

fn main() {
    let path = Path::new("/home/sweet/home");
    println!("The path: {}", path.display());
}

When I try to debug it with rust-gdb I get this:

$ rust-gdb target/debug/poc-gdb-pathbuf
...
>>> start
>>> n
>>> print path
$1 = (*mut std::path::Path) 0x55555557c000
>>> print &path
$2 = (*mut *mut std::path::Path) 0x7fffffffe0c0
>>> print *path
$3 = std::path::Path {
  inner: std::ffi::os_str::OsStr {
    inner: std::sys_common::os_str_bytes::Slice {
      inner: 0x55555557c000
    }
  }
}
>>> print *path.is_absolute()
Could not find function named 'std::path::Path::is_absolute'
>>> print path.is_absolute()
Could not find function named 'std::path::Path::is_absolute'
>>> print path.display()
Function 'std::path::Path::display' takes no arguments
>>> print *path.display()
Function 'std::path::Path::display' takes no arguments

As you can see I cannot manage to call a single Path instance function correctly, either it tells me the function doesn't exists (it does) or that it takes no arguments (I haven't passed any).

I have no problems with a simple, minimal struct function call, here's an example that works.

main.rs

struct Something {
    x: u8
}

impl Something {
    pub fn new(x: u8) -> Self {
        Something { x }
    }

    pub fn show(&self) -> u8 {
        return self.x
    }
}

fn main() {
    let something = Something::new(10);
    something.show();
}

The rust-gdb session:

$ rust-gdb target/debug/poc-gdb-something
...
>>> start
>>> n
>>> print something
$1 = poc_gdb_pathbuf::Something {
  x: 10
}
>>> print something.show()
$2 = 10

I suspect it may be something about the Path instance being a pointer but dereferencing it doesn't help...

Why the Path calls doesn't work? What am I missing?

like image 598
Terseus Avatar asked Jan 31 '26 06:01

Terseus


1 Answers

The Rust compiler does not emit complete debugging information describing everything it emits. This, ultimately, is what hampers gdb's ability to do these things.

gdb can't make calls via traits, because traits aren't described. There is a Rust bug for this.

Printing a Path is likewise difficult because the Rust compiler doesn't emit debug information for dynamically sized types. I was reluctant to hack this into gdb directly; in this case one could write custom Python code to decode these objects, by making (perhaps invalid) assumptions about their layout. For this case, see this Rust bug.

like image 126
Tom Tromey Avatar answered Feb 01 '26 22:02

Tom Tromey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!