public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Reimplement Rust slice printing
@ 2022-04-15 17:35 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2022-04-15 17:35 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=506ec52e8805d8edd538d6bd11750489a8c8bbee

commit 506ec52e8805d8edd538d6bd11750489a8c8bbee
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Mar 25 13:36:53 2022 -0600

    Reimplement Rust slice printing
    
    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.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=21466
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23871

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 () == TYPE_CODE_STRUCT
-	  && type->name () != NULL
-	  && (strncmp (type->name (), "&[", 2) == 0
-	      || strcmp (type->name (), "&str") == 0));
+  if (type->code () == TYPE_CODE_STRUCT
+      && type->name () != NULL
+      && type->num_fields () == 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 = type->field (0).name ();
+      const char *n2 = type->field (1).name ();
+      return ((streq (n1, "data_ptr") && streq (n2, "length"))
+	      || (streq (n2, "data_ptr") && streq (n1, "length")));
+    }
+  return false;
 }
 
 /* Return true if TYPE is a range type, otherwise false.  */
@@ -296,19 +305,57 @@ rust_language::printstr (struct ui_file *stream, struct type *type,
 
 \f
 
-/* Helper function to print a string slice.  */
+static const struct generic_val_print_decorations rust_decorations =
+{
+  /* Complex isn't used in Rust, but we provide C-ish values just in
+     case.  */
+  "",
+  " + ",
+  " * I",
+  "true",
+  "false",
+  "()",
+  "[",
+  "]"
+};
+
+/* Helper function to print a slice.  */
 
 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 recurse,
+		      const struct value_print_options *options)
 {
   struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
 					 "slice");
   struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
 
-  val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
-		    value_as_address (base), value_as_long (len), stream,
-		    options);
+  struct type *type = check_typedef (value_type (val));
+  if (strcmp (type->name (), "&str") == 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 = value_as_long (len);
+
+      type_print (value_type (val), "", stream, -1);
+      gdb_printf (stream, " ");
+
+      if (llen == 0)
+	gdb_printf (stream, "[]");
+      else
+	{
+	  struct type *elt_type = TYPE_TARGET_TYPE (value_type (base));
+	  struct type *array_type = lookup_array_range_type (elt_type, 0,
+							     llen - 1);
+	  struct value *array = allocate_value_lazy (array_type);
+	  VALUE_LVAL (array) = lval_memory;
+	  set_value_address (array, value_as_address (base));
+	  value_fetch_lazy (array);
+	  generic_value_print (array, stream, recurse, options,
+			       &rust_decorations);
+	}
+    }
 }
 
 /* See rust-lang.h.  */
@@ -322,9 +369,9 @@ rust_language::val_print_struct
   int first_field;
   struct type *type = check_typedef (value_type (val));
 
-  if (rust_slice_type_p (type) && strcmp (type->name (), "&str") == 0)
+  if (rust_slice_type_p (type))
     {
-      rust_val_print_str (stream, val, options);
+      rust_val_print_slice (val, stream, recurse, options);
       return;
     }
 
@@ -468,20 +515,6 @@ rust_language::print_enum (struct value *val, struct ui_file *stream,
     gdb_puts ("}", stream);
 }
 
-static const struct generic_val_print_decorations rust_decorations =
-{
-  /* Complex isn't used in Rust, but we provide C-ish values just in
-     case.  */
-  "",
-  " + ",
-  " * I",
-  "true",
-  "false",
-  "()",
-  "[",
-  "]"
-};
-
 /* See language.h.  */
 
 void
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.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
 
-	set result " = &\\\[.*\\\] \\{data_ptr: $hex, length: $length\\}"
+	# Just accept any array here.
+	set result " = &\\\[.*\\\] \\\[.*\\\]"
 
 	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/unsized.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]} {
 }
 
 gdb_test "ptype us" " = .*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] >= 61} {
+    gdb_test "print us2" " = .*Box<.*> \\\[1, 2, 3\\\]"
+    gdb_test "ptype us2" "type = .*"
+}
diff --git a/gdb/testsuite/gdb.rust/unsized.rs b/gdb/testsuite/gdb.rust/unsized.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<T>(x: T) { }
 fn main() {
     let v: Box<V<[u8; 3]>> = Box::new(V { data: [1, 2, 3] });
     let us: Box<Unsized> = v;
+    let v2 : Box<[u8; 3]> = Box::new([1, 2, 3]);
+    let us2 : Box<[u8]> = v2;
 
     ignore(us);     // set breakpoint here
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-15 17:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-15 17:35 [binutils-gdb] Reimplement Rust slice printing Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).