public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/4] Some Rust value-printing improvements
@ 2022-04-01 20:08 Tom Tromey
  2022-04-01 20:08 ` [PATCH 1/4] Match rustc beta versions Tom Tromey
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-01 20:08 UTC (permalink / raw)
  To: gdb-patches

I started this series when I noticed some upstream Rust compiler
changes to emit better debuginfo for unsized types.  However, it
expanded a bit after that to include a couple other minor changes I
noticed, and then I tacked on a fix for a minor, long-standing bug as
well.

I tested this on x86-64 Fedora 34 using several Rust compiler
versions.

Tom



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] Match rustc beta versions
  2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
@ 2022-04-01 20:08 ` Tom Tromey
  2022-04-01 20:08 ` [PATCH 2/4] Remove some dead code from the Rust value printer Tom Tromey
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-01 20:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

The rust_compiler_version proc extracts the Rust compiler version from
the "rustc --version" output.  For a beta compiler, the output looks
like:

    rustc 1.60.0-beta.6 (7bccde197 2022-03-22)

This patch slightly relaxes the regexp -- removing a space -- so that
this can be understood by this proc.
---
 gdb/testsuite/lib/rust-support.exp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/testsuite/lib/rust-support.exp b/gdb/testsuite/lib/rust-support.exp
index 0d39566df73..85e5b021e21 100644
--- a/gdb/testsuite/lib/rust-support.exp
+++ b/gdb/testsuite/lib/rust-support.exp
@@ -104,7 +104,7 @@ gdb_caching_proc rust_compiler_version {
     } else {
 	set output [lindex [remote_exec host "$rustc --version --verbose"] 1]
 	foreach line [split $output \n] {
-	    if {[regexp "rustc (\[0-9.\]+) .*\$" $output ignore version]} {
+	    if {[regexp "rustc (\[0-9.\]+).*\$" $output ignore version]} {
 		return $version
 	    }
 	}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/4] Remove some dead code from the Rust value printer
  2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
  2022-04-01 20:08 ` [PATCH 1/4] Match rustc beta versions Tom Tromey
@ 2022-04-01 20:08 ` Tom Tromey
  2022-04-01 20:08 ` [PATCH 3/4] Reimplement Rust slice printing Tom Tromey
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-01 20:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes a bit of dead code from the Rust value printer.  This
code wasn't always dead -- it fixed a real bug, and a test case was
added for it.  However, once val_print was removed, it became
unnecessary.
---
 gdb/rust-lang.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 836ea37f153..fdd6e3fbdbb 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -324,14 +324,6 @@ rust_language::val_print_struct
 
   if (rust_slice_type_p (type) && strcmp (type->name (), "&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));
       rust_val_print_str (stream, val, options);
       return;
     }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/4] Reimplement Rust slice printing
  2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
  2022-04-01 20:08 ` [PATCH 1/4] Match rustc beta versions Tom Tromey
  2022-04-01 20:08 ` [PATCH 2/4] Remove some dead code from the Rust value printer Tom Tromey
@ 2022-04-01 20:08 ` Tom Tromey
  2022-04-01 20:08 ` [PATCH 4/4] Implement value_print for Rust Tom Tromey
  2022-04-15 16:33 ` [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-01 20:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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
---
 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
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/4] Implement value_print for Rust
  2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
                   ` (2 preceding siblings ...)
  2022-04-01 20:08 ` [PATCH 3/4] Reimplement Rust slice printing Tom Tromey
@ 2022-04-01 20:08 ` Tom Tromey
  2022-04-15 16:33 ` [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-01 20:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This adds an implementation of the value_print method to Rust.  As
described in PR rust/22254, this removes a bit of weird-looking output
from some "print"s -- because c_value_print is bypassed.  I don't have
a test for the bug that inspired this patch, because I only know how
to reproduce it when using a relatively old Rust compiler.  However,
the new "cast-printing" code in value_print is required, because
omitting this causes some existing tests to fail.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22254
---
 gdb/rust-lang.c | 21 +++++++++++++++++++++
 gdb/rust-lang.h |  5 +++++
 2 files changed, 26 insertions(+)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index d4a221abb08..bf8dba99ecd 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -624,6 +624,27 @@ rust_language::value_print_inner
     }
 }
 
+/* See language.h.  */
+
+void
+rust_language::value_print
+	(struct value *val, struct ui_file *stream,
+	 const struct value_print_options *options) const
+{
+  value_print_options opts = *options;
+  opts.deref_ref = true;
+
+  struct type *type = check_typedef (value_type (val));
+  if (type->is_pointer_or_reference ())
+    {
+      gdb_printf (stream, "(");
+      type_print (value_type (val), "", stream, -1);
+      gdb_printf (stream, ") ");
+    }
+
+  return common_val_print (val, stream, 0, &opts, this);
+}
+
 \f
 
 static void
diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h
index 0e89b8822cb..514494a2c82 100644
--- a/gdb/rust-lang.h
+++ b/gdb/rust-lang.h
@@ -125,6 +125,11 @@ class rust_language : public language_defn
 
   /* See language.h.  */
 
+  void value_print (struct value *val, struct ui_file *stream,
+		    const struct value_print_options *options) const override;
+
+  /* See language.h.  */
+
   struct block_symbol lookup_symbol_nonlocal
 	(const char *name, const struct block *block,
 	 const domain_enum domain) const override
-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] Some Rust value-printing improvements
  2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
                   ` (3 preceding siblings ...)
  2022-04-01 20:08 ` [PATCH 4/4] Implement value_print for Rust Tom Tromey
@ 2022-04-15 16:33 ` Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2022-04-15 16:33 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> I started this series when I noticed some upstream Rust compiler
Tom> changes to emit better debuginfo for unsized types.  However, it
Tom> expanded a bit after that to include a couple other minor changes I
Tom> noticed, and then I tacked on a fix for a minor, long-standing bug as
Tom> well.

Tom> I tested this on x86-64 Fedora 34 using several Rust compiler
Tom> versions.

I'm checking this in now.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-04-15 16:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-01 20:08 [PATCH 0/4] Some Rust value-printing improvements Tom Tromey
2022-04-01 20:08 ` [PATCH 1/4] Match rustc beta versions Tom Tromey
2022-04-01 20:08 ` [PATCH 2/4] Remove some dead code from the Rust value printer Tom Tromey
2022-04-01 20:08 ` [PATCH 3/4] Reimplement Rust slice printing Tom Tromey
2022-04-01 20:08 ` [PATCH 4/4] Implement value_print for Rust Tom Tromey
2022-04-15 16:33 ` [PATCH 0/4] Some Rust value-printing improvements 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).