From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 1E9003858016; Fri, 15 Apr 2022 17:35:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E9003858016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Reimplement Rust slice printing X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 925ea601f1892c6eb344e2c916f0ef448b42b606 X-Git-Newrev: 506ec52e8805d8edd538d6bd11750489a8c8bbee Message-Id: <20220415173557.1E9003858016@sourceware.org> Date: Fri, 15 Apr 2022 17:35:57 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Apr 2022 17:35:57 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D506ec52e8805= d8edd538d6bd11750489a8c8bbee commit 506ec52e8805d8edd538d6bd11750489a8c8bbee Author: Tom Tromey Date: Fri Mar 25 13:36:53 2022 -0600 Reimplement Rust slice printing =20 The current nightly Rust compiler (aka 1.61) added better DWARF representation for unsized types. Fixing this is PR rust/21466; but the code is actually the same as what is required to make slice printing more useful, which is PR rust/23871. This patch implements this. I tested this against various Rust compilers: 1.48, current stable, current beta, and current nightly. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D21466 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D23871 Diff: --- gdb/rust-lang.c | 85 ++++++++++++++++++++++++++--------= ---- gdb/testsuite/gdb.rust/simple.exp | 3 +- gdb/testsuite/gdb.rust/unsized.exp | 8 ++++ gdb/testsuite/gdb.rust/unsized.rs | 2 + 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index fdd6e3fbdbb..d4a221abb08 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -159,10 +159,19 @@ rust_tuple_struct_type_p (struct type *type) static bool rust_slice_type_p (struct type *type) { - return (type->code () =3D=3D TYPE_CODE_STRUCT - && type->name () !=3D NULL - && (strncmp (type->name (), "&[", 2) =3D=3D 0 - || strcmp (type->name (), "&str") =3D=3D 0)); + if (type->code () =3D=3D TYPE_CODE_STRUCT + && type->name () !=3D NULL + && type->num_fields () =3D=3D 2) + { + /* The order of fields doesn't matter. While it would be nice + to check for artificiality here, the Rust compiler doesn't + emit this information. */ + const char *n1 =3D type->field (0).name (); + const char *n2 =3D type->field (1).name (); + return ((streq (n1, "data_ptr") && streq (n2, "length")) + || (streq (n2, "data_ptr") && streq (n1, "length"))); + } + return false; } =20 /* Return true if TYPE is a range type, otherwise false. */ @@ -296,19 +305,57 @@ rust_language::printstr (struct ui_file *stream, stru= ct type *type, =20 =0C =20 -/* Helper function to print a string slice. */ +static const struct generic_val_print_decorations rust_decorations =3D +{ + /* Complex isn't used in Rust, but we provide C-ish values just in + case. */ + "", + " + ", + " * I", + "true", + "false", + "()", + "[", + "]" +}; + +/* Helper function to print a slice. */ =20 static void -rust_val_print_str (struct ui_file *stream, struct value *val, - const struct value_print_options *options) +rust_val_print_slice (struct value *val, struct ui_file *stream, int recur= se, + const struct value_print_options *options) { struct value *base =3D value_struct_elt (&val, {}, "data_ptr", NULL, "slice"); struct value *len =3D value_struct_elt (&val, {}, "length", NULL, "slice= "); =20 - val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8", - value_as_address (base), value_as_long (len), stream, - options); + struct type *type =3D check_typedef (value_type (val)); + if (strcmp (type->name (), "&str") =3D=3D 0) + val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8", + value_as_address (base), value_as_long (len), stream, + options); + else + { + LONGEST llen =3D value_as_long (len); + + type_print (value_type (val), "", stream, -1); + gdb_printf (stream, " "); + + if (llen =3D=3D 0) + gdb_printf (stream, "[]"); + else + { + struct type *elt_type =3D TYPE_TARGET_TYPE (value_type (base)); + struct type *array_type =3D lookup_array_range_type (elt_type, 0, + llen - 1); + struct value *array =3D allocate_value_lazy (array_type); + VALUE_LVAL (array) =3D lval_memory; + set_value_address (array, value_as_address (base)); + value_fetch_lazy (array); + generic_value_print (array, stream, recurse, options, + &rust_decorations); + } + } } =20 /* See rust-lang.h. */ @@ -322,9 +369,9 @@ rust_language::val_print_struct int first_field; struct type *type =3D check_typedef (value_type (val)); =20 - if (rust_slice_type_p (type) && strcmp (type->name (), "&str") =3D=3D 0) + if (rust_slice_type_p (type)) { - rust_val_print_str (stream, val, options); + rust_val_print_slice (val, stream, recurse, options); return; } =20 @@ -468,20 +515,6 @@ rust_language::print_enum (struct value *val, struct u= i_file *stream, gdb_puts ("}", stream); } =20 -static const struct generic_val_print_decorations rust_decorations =3D -{ - /* Complex isn't used in Rust, but we provide C-ish values just in - case. */ - "", - " + ", - " * I", - "true", - "false", - "()", - "[", - "]" -}; - /* See language.h. */ =20 void diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/sim= ple.exp index b6f2993c381..246aff634d3 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -314,7 +314,8 @@ proc test_one_slice {svar length base range} { with_test_prefix $range { global hex =20 - set result " =3D &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}" + # Just accept any array here. + set result " =3D &\\\[.*\\\] \\\[.*\\\]" =20 gdb_test "print $svar" $result gdb_test "print &${base}\[${range}\]" $result diff --git a/gdb/testsuite/gdb.rust/unsized.exp b/gdb/testsuite/gdb.rust/un= sized.exp index 76d0bbb739b..67ee49d0935 100644 --- a/gdb/testsuite/gdb.rust/unsized.exp +++ b/gdb/testsuite/gdb.rust/unsized.exp @@ -32,3 +32,11 @@ if {![runto ${srcfile}:$line]} { } =20 gdb_test "ptype us" " =3D .*V<\\\[u8\\\]>.*" + +set v [split [rust_compiler_version] .] +# The necessary debuginfo generation landed in 1.60, but had a bug +# that was fixed in 1.61. +if {[lindex $v 0] > 1 || [lindex $v 1] >=3D 61} { + gdb_test "print us2" " =3D .*Box<.*> \\\[1, 2, 3\\\]" + gdb_test "ptype us2" "type =3D .*" +} diff --git a/gdb/testsuite/gdb.rust/unsized.rs b/gdb/testsuite/gdb.rust/uns= ized.rs index f4897d6b8f8..af3192e8407 100644 --- a/gdb/testsuite/gdb.rust/unsized.rs +++ b/gdb/testsuite/gdb.rust/unsized.rs @@ -28,6 +28,8 @@ fn ignore(x: T) { } fn main() { let v: Box> =3D Box::new(V { data: [1, 2, 3] }); let us: Box =3D v; + let v2 : Box<[u8; 3]> =3D Box::new([1, 2, 3]); + let us2 : Box<[u8]> =3D v2; =20 ignore(us); // set breakpoint here }