This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Force array coercion in c_get_string


Pedro> So with the patch, we coerce the array to inferior memory, in order
Pedro> to read its contents back from the inferior?  Is that right?
Pedro> Why not skip the detour and save the memory allocation in the inferior?

Tom> Yeah, that's weird, isn't it.

Plan B is a bit more complicated, but may be nicer because in some cases
it will require fewer memory reads, and also doesn't force coercion.

Let me know what you think.

Tom

commit 9acd65e278c569f47e73f9d455127b3379792c34
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Apr 25 12:14:58 2019 -0600

    Correctly handle non-C-style arrays in c_get_string
    
    A user here noticed that the Python Value.string method did not work
    for Ada arrays.  I tracked this down to an oddity in value_as_address
    -- namely, it calls coerce_array, but that function will not force
    array coercion when the language has c_style_arrays=false, as Ada
    does.
    
    This patch fixes the problem by changing c_get_string so that arrays
    take the "in GDB's memory" branch.  The actual patch is somewhat more
    complicated than you might think, because the caller can request more
    array elements than the type allows.  This is normal when the type is
    using the C struct hack.
    
    Tested on x86-64 Fedora 29.
    
    gdb/ChangeLog
    2019-04-25  Tom Tromey  <tromey@adacore.com>
    
            * c-lang.c (c_get_string): Handle non-C-style arrays.
    
    gdb/testsuite/ChangeLog
    2019-04-25  Tom Tromey  <tromey@adacore.com>
    
            * gdb.python/py-value.exp (test_value_in_inferior): Add Ada test.

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 17e10e869b8..b8e018a97e5 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2019-04-25  Tom Tromey  <tromey@adacore.com>
+
+	* c-lang.c (c_get_string): Handle non-C-style arrays.
+
 2019-04-25  Tom Tromey  <tromey@adacore.com>
 
 	PR gdb/24475:
diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 33506f1d1ed..0c189aae606 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -279,9 +279,18 @@ c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
   /* If the string lives in GDB's memory instead of the inferior's,
      then we just need to copy it to BUFFER.  Also, since such strings
      are arrays with known size, FETCHLIMIT will hold the size of the
-     array.  */
+     array.
+
+     An array is assumed to live in GDB's memory, so we take this path
+     here.  However, it's possible for the caller to request more
+     array elements than apparently exist -- this can happen when
+     using the C struct hack.  So, only do this for an array if either
+     no length was specified, or the length is within the existing
+     bounds.  */
   if ((VALUE_LVAL (value) == not_lval
-       || VALUE_LVAL (value) == lval_internalvar)
+       || VALUE_LVAL (value) == lval_internalvar
+       || (TYPE_CODE (type) == TYPE_CODE_ARRAY
+	   && (*length < 0 || *length <= fetchlimit)))
       && fetchlimit != UINT_MAX)
     {
       int i;
@@ -306,7 +315,14 @@ c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
     }
   else
     {
-      CORE_ADDR addr = value_as_address (value);
+      /* value_as_address calls coerce_array, but that won't actually
+	 coerce the array if c_style_arrays is false.  In this case,
+	 get the address of the array directly.  */
+      CORE_ADDR addr;
+      if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
+	addr = value_address (value);
+      else
+	addr = value_as_address (value);
 
       /* Prior to the fix for PR 16196 read_string would ignore fetchlimit
 	 if length > 0.  The old "broken" behaviour is the behaviour we want:
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index c81d841e93a..7127cbdc75f 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2019-04-25  Tom Tromey  <tromey@adacore.com>
+
+	* gdb.python/py-value.exp (test_value_in_inferior): Add Ada test.
+
 2019-04-25  Sergio Durigan Junior  <sergiodj@redhat.com>
 
 	PR corefiles/11608
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index b3d90b52272..7f42d31572a 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -315,6 +315,13 @@ proc test_value_in_inferior {} {
   gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "test string (length = 0) is empty"
   gdb_test "python print (len(st.string (length = 0)))" "0" "test length is 0"
 
+  # We choose Ada here to test a language where c_style_arrays is
+  # false.
+  gdb_test "set lang ada" \
+      "Warning: the current language does not match this frame."
+  gdb_test "python print (st.string ())"  "divide et impera"  \
+      "Test string with no length in ada"
+  gdb_test_no_output "set lang auto"
 
   # Fetch a string that has embedded nulls.
   gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*"


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]