]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix printf of a convenience variable holding an inferior address
authorSergio Durigan Junior <sergiodj@redhat.com>
Mon, 9 Mar 2020 22:47:47 +0000 (18:47 -0400)
committerSergio Durigan Junior <sergiodj@redhat.com>
Mon, 9 Mar 2020 22:47:47 +0000 (18:47 -0400)
Back at:

commit 1f6f6e21fa86dc3411a6498608f32e9eb24b7851
Author: Philippe Waroquiers <philippe.waroquiers@skynet.be>
Date:   Mon Jun 10 21:41:51 2019 +0200

    Ensure GDB printf command can print convenience var strings without a target.

GDB was extended in order to allow the printing of convenience
variables that are strings without a target.  However, this introduced
a regression that hasn't been caught by our testsuite (because there
were no tests for it).

The problem happens when we try to print a convenience variable that
holds the address of a string in the inferior.  The following
two-liners can reproduce the issue:

$ echo -e 'int main(){const char a[]="test";return 0;}' | gcc -x c - -O0-g3
$ ./gdb/gdb --data-directory ./gdb/data-directory -q ./a.out -ex 'start' -ex 'set $x = (const char *) (&a[0] + 2)' -ex 'printf "%s\n", $x'

After some investigation, I found that the problem happens on
printcmd.c:printf_c_string.  In the case above, we're taking the first
branch of the 'if' condition, which assumes that there will be a value
to be printed at "value_contents (value)".  There isn't.  We actually
need to obtain the address that the variable points to, and read the
contents from memory.

It seems to me that we should avoid this branch if the TYPE_CODE of
"value_type (value)" is TYPE_CODE_PTR (i.e., a pointer to the
inferior's memory).  This is what this patch does.

I took the liberty to extend the current testcase under
gdb.base/printcmds.exp and create a test that exercises this scenario.

No regressions have been found on Buildbot.

gdb/ChangeLog:
2020-03-03  Sergio Durigan Junior  <sergiodj@redhat.com>

PR gdb/25650
* printcmd.c (print_c_string): Check also for TYPE_CODE_PTR
when verifying if dealing with a convenience variable.

gdb/testsuite/ChangeLog:
2020-03-03  Sergio Durigan Junior  <sergiodj@redhat.com>

PR gdb/25650
* gdb.base/printcmds.exp: Add test to verify printf of a
variable holding an address.

gdb/ChangeLog
gdb/printcmd.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/printcmds.exp

index d642165c59a924958dbc29bd9ee006e0d3d10752..d67864511bad9fb5d9f9cadcefff40681909b919 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-03  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       PR gdb/25650
+       * printcmd.c (print_c_string): Check also for TYPE_CODE_PTR
+       when verifying if dealing with a convenience variable.
+
 2020-02-22  Eli Zaretskii  <eliz@gnu.org>
 
        PRE gdb/25586:
index ed2c8d47add3ff980c403625cbdf4a86f47fc43e..0df9826888a84d230cf4166b4566207463b7d1de 100644 (file)
@@ -2260,7 +2260,8 @@ printf_c_string (struct ui_file *stream, const char *format,
 {
   const gdb_byte *str;
 
-  if (VALUE_LVAL (value) == lval_internalvar
+  if (TYPE_CODE (value_type (value)) != TYPE_CODE_PTR
+      && VALUE_LVAL (value) == lval_internalvar
       && c_is_string_type_p (value_type (value)))
     {
       size_t len = TYPE_LENGTH (value_type (value));
index ec507fd1b2151b912727e3d1d3a8f17e00133eb0..90e26950c4e31df0f9914b81fb9fc5f83c9f073c 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-03  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       PR gdb/25650
+       * gdb.base/printcmds.exp: Add test to verify printf of a
+       variable holding an address.
+
 2020-02-07  Tom Tromey  <tromey@adacore.com>
 
        PR breakpoints/24915:
index 6e98b7943ba3924126e968d4a5086aa02a187caa..f1816e637b21de9a57cef1b0ec88ea4f7ad88dfb 100644 (file)
@@ -1025,6 +1025,14 @@ gdb_test_no_output "set may-call-functions off"
 test_printf_convenience_var "with target, may-call-functions off"
 gdb_test_no_output "set may-call-functions on"
 
+# Test printf of a variable that holds the address to a substring in
+# the inferior.  This test will not work without a target.
+gdb_test_no_output "set var \$test_substr = \(char \*\) \(&teststring\[0\] + 4\)" \
+    "set \$test_substr var"
+gdb_test "printf \"test_substr val = %s\\n\", \$test_substr" \
+    "test_substr val = string contents" \
+    "print \$test_substr"
+
 test_integer_literals_accepted
 test_integer_literals_rejected
 test_float_accepted