From 80062eb94959467fb0c27b988b87ac08dada4bd8 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Thu, 2 May 2019 00:40:01 +0100 Subject: [PATCH] gdb/rust: Handle printing structures containing strings When printing a rust structure that contains a string GDB can currently fail to read the fields that define the string. This is because GDB mistakenly treats a value that is the parent structure as though it is the structure that defines the string, and then fails to find the fields needed to extract a string. The solution is to create a new value to represent the string field of the parent value. gdb/ChangeLog: * rust-lang.c (val_print_struct): Handle printing structures containing strings. gdb/testsuite/ChangeLog: * gdb.rust/simple.exp: Add new test case. * gdb.rust/simple.rs (struct StringAtOffset): New struct. (main): Initialise an instance of the new struct. --- gdb/ChangeLog | 5 +++++ gdb/rust-lang.c | 8 ++++++++ gdb/testsuite/ChangeLog | 6 ++++++ gdb/testsuite/gdb.rust/simple.exp | 3 +++ gdb/testsuite/gdb.rust/simple.rs | 8 ++++++++ 5 files changed, 30 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index c36b45c9581..bef807d474c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2019-05-02 Andrew Burgess + + * rust-lang.c (val_print_struct): Handle printing structures + containing strings. + 2019-05-02 Tom Tromey * valarith.c (_initialize_valarith): Remove. diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 2fada465d65..79f13311cd8 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -378,6 +378,14 @@ val_print_struct (struct type *type, int embedded_offset, if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0) { + /* If what we are printing here is actually a string within a + structure then VAL will be the original parent value, while TYPE + will be the type of the structure representing the string we want + to print. + However, RUST_VAL_PRINT_STR looks up the fields of the string + inside VAL, assuming that VAL is the string. + So, recreate VAL as a value representing just the string. */ + val = value_at_lazy (type, value_address (val) + embedded_offset); rust_val_print_str (stream, val, options); return; } diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 0f96ea2eccf..93e2e41325b 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2019-05-02 Andrew Burgess + + * gdb.rust/simple.exp: Add new test case. + * gdb.rust/simple.rs (struct StringAtOffset): New struct. + (main): Initialise an instance of the new struct. + 2019-05-01 Tom Tromey * gdb.ada/packed_array_assign/aggregates.ads (Nested_Packed): New diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 91afe85cee7..7211bd29be2 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -239,6 +239,9 @@ gdb_test "print custom_some" \ " = simple::NonZeroOptimized::Value\\(\[a-z\]+::string::String .*" gdb_test "print custom_none" " = simple::NonZeroOptimized::Empty" +gdb_test "print st" \ + " = simple::StringAtOffset {field1: \"hello\", field2: 1, field3: \"world\"}" + proc test_one_slice {svar length base range} { global hex diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs index 19f5ef92241..e6e0efd3b16 100644 --- a/gdb/testsuite/gdb.rust/simple.rs +++ b/gdb/testsuite/gdb.rust/simple.rs @@ -85,6 +85,12 @@ union Union { f2: u8, } +struct StringAtOffset { + pub field1: &'static str, + pub field2: i32, + pub field3: &'static str, +} + // A simple structure whose layout won't be changed by the compiler, // so that ptype/o testing will work on any platform. struct SimpleLayout { @@ -146,6 +152,8 @@ fn main () { let to1 = &w[..3]; let to2 = &slice[..1]; + let st = StringAtOffset { field1: "hello", field2: 1, field3: "world" }; + // tests for enum optimizations let str_some = Some("hi".to_string()); -- 2.30.2