Skip to content

GDB cast raw pointer to &str #65272

Closed
Closed
@TjeuKayim

Description

@TjeuKayim

The &str type is not available in rust-gdb.

When debugging using rust-gdb, local variables with &str type are pretty printed.

(gdb) p my_string_local
$6 = "my string"
(gdb) ptype my_string_local
type = struct &str {
  data_ptr: *mut u8,
  length: usize,
}

I expect that a raw pointer referring to the same memory location on the stack can be casted to &str and printed. However, gdb throws an exception:

(gdb) p *(($rsp + 168) as *mut &[u8])
$15 = &[*gdb*](len: 14) = {109, 121, 32, 115, 116, 114, 105, 110, 103}
(gdb) p *(($rsp + 168) as *mut &str)
No typed name 'str' in current context

# STR SLICE
if unqualified_type_name == "&str":
return TYPE_KIND_STR_SLICE
.

Workaround

You can use a helper function to cast and dereference the pointer:

unsafe fn print_str(ptr: *const &str) -> &str {
    return *ptr;
}
(gdb) p my_lib::print_str($rsp + 168)
$15 = "my string"

Meta

rustc --version --verbose
rustc 1.38.0 (625451e37 2019-09-23)
binary: rustc
commit-hash: 625451e376bb2e5283fc4741caa0a3e8a2ca4d54
commit-date: 2019-09-23
host: x86_64-unknown-linux-gnu
release: 1.38.0
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions