From patchwork Wed May 1 23:52:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 32481 Received: (qmail 44099 invoked by alias); 1 May 2019 23:52:54 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 44091 invoked by uid 89); 1 May 2019 23:52:54 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=rustlangc, rust-lang.c, UD:rust-lang.c, type_name X-HELO: mail-wr1-f66.google.com Received: from mail-wr1-f66.google.com (HELO mail-wr1-f66.google.com) (209.85.221.66) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 01 May 2019 23:52:52 +0000 Received: by mail-wr1-f66.google.com with SMTP id o4so674815wra.3 for ; Wed, 01 May 2019 16:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id; bh=n1sX53zhImNy5SyfgoRcEn8OF53UCy9qmjw6YwQ5WM8=; b=KBOmtEcCZDqsojH3JIma6ibO/8AgUMutaMAX6VI9EVe8Uu0Oeiv++pVsiL8mFkwB+i 82KHs/XN/niseAEWhyvTmuLvlEtWjnDh6fJwbRQ2Sfoev9i3IhEhxLo0yz+qgvhhY+8R i2Z31zBL0fEvxhIg+0B+T+bGBBzHuOPuJQEfPIqMiNbnANvUfomDJkgat4w0QG5ntjJI U42KkeW140XZib4D4w/E5c6w6pBu1TTbt4bq9brekD55hC7WCPwAEupdogOszQa8v++G pyl7Vy1edfFg1BLuoLbtz/gS4VioT3n/1u2lH26LO4IWsJJPwEZTj9JyFa3C/PFcqyMM VhFw== Return-Path: Received: from localhost (host109-154-100-57.range109-154.btcentralplus.com. [109.154.100.57]) by smtp.gmail.com with ESMTPSA id b11sm11900860wmh.29.2019.05.01.16.52.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 01 May 2019 16:52:49 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/rust: Handle printing structures containing strings Date: Thu, 2 May 2019 00:52:46 +0100 Message-Id: <20190501235246.7982-1-andrew.burgess@embecosm.com> X-IsSubscribed: yes 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 | 2 ++ gdb/testsuite/gdb.rust/simple.rs | 8 ++++++++ 5 files changed, 29 insertions(+) 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/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 91afe85cee7..869b16ce33c 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -239,6 +239,8 @@ 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());