This is the mail archive of the
gdb-cvs@sourceware.org
mailing list for the GDB project.
[binutils-gdb] gdb: Ensure compiler doesn't optimise variable out in test
- From: Andrew Burgess <aburgess at sourceware dot org>
- To: gdb-cvs at sourceware dot org
- Date: 30 Aug 2018 15:51:57 -0000
- Subject: [binutils-gdb] gdb: Ensure compiler doesn't optimise variable out in test
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c67f2e1518a829c4ae4f346c316701ef5d56a30f
commit c67f2e1518a829c4ae4f346c316701ef5d56a30f
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date: Wed Aug 29 18:49:51 2018 +0100
gdb: Ensure compiler doesn't optimise variable out in test
In the test gdb.base/funcargs.exp, there's this function:
void recurse (SVAL a, int depth)
{
a.s = a.i = a.l = --depth;
if (depth == 0)
hitbottom ();
else
recurse (a, depth);
}
The test script places a breakpoint in hitbottom, and runs the
executable which calls recurse with an initial depth of 4.
When GDB hits the breakpoint in hitbottom the testscript performs a
backtrace, and examines 'a' at each level.
The problem is that 'a' is not live after either the call to
'hitbottom' or the call to 'recurse', and as a result the test fails.
In the particular case I was looking at GCC for RISC-V 32-bit, the
variable 'a' is on the stack and GCC selects the register $ra (the
return address register) to hold the pointer to 'a'. This is fine,
because, by the time the $ra register is needed to hold a return
address (calling hitbottom or recurse) then 'a' is dead.
In this patch I propose that a use of 'a' is added after the calls to
hitbottom and recurse, this should cause the compiler to keep 'a'
around, which should ensure GDB can find it.
gdb/testsuite/ChangeLog:
* gdb.base/funcargs.c (use_a): New function.
(recurse): Call use_a.
Diff:
---
gdb/testsuite/ChangeLog | 5 +++++
gdb/testsuite/gdb.base/funcargs.c | 9 +++++++++
2 files changed, 14 insertions(+)
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 0164558..23a41d4 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2018-08-30 Andrew Burgess <andrew.burgess@embecosm.com>
+
+ * gdb.base/funcargs.c (use_a): New function.
+ (recurse): Call use_a.
+
2018-08-29 Keith Seitz <keiths@redhat.com>
* gdb.compile/compile-cplus-anonymous.cc: New file.
diff --git a/gdb/testsuite/gdb.base/funcargs.c b/gdb/testsuite/gdb.base/funcargs.c
index 600792f..d5ff192 100644
--- a/gdb/testsuite/gdb.base/funcargs.c
+++ b/gdb/testsuite/gdb.base/funcargs.c
@@ -424,6 +424,12 @@ void hitbottom ()
{
}
+void use_a (SVAL a)
+{
+ /* Trick the compiler into thinking A is important. */
+ volatile SVAL dummy = a;
+}
+
void recurse (SVAL a, int depth)
{
a.s = a.i = a.l = --depth;
@@ -431,6 +437,9 @@ void recurse (SVAL a, int depth)
hitbottom ();
else
recurse (a, depth);
+
+ /* Ensure A is not discarded after the above calls. */
+ use_a (a);
}
void test_struct_args ()