Fixup gdb.python/py-value.exp for bare-metal aarch64-elf
authorLuis Machado <lgustavo@codesourcery.com>
Wed, 12 Oct 2016 15:10:03 +0000 (10:10 -0500)
committerLuis Machado <lgustavo@codesourcery.com>
Wed, 12 Oct 2016 15:10:03 +0000 (10:10 -0500)
I noticed that testing aarch64-elf gdb with a physical board
ran into issues with gdb.python/py-value.exp. Further investigation showed
that we were actually trying to dereference a NULL pointer (argv) when trying
to access argv[0].

Being bare-metal, argv is not guaranteed to be valid. So we need to make sure
argv is sane before accessing argv[0].

The following patch fixes up the test program to check for a NULL argv and also
improves the testcase a bit so it doesn't have to work with a hardcoded argc
value.

Regression-tested on x86-64 Ubuntu 16.04.

gdb/testsuite/ChangeLog:

2016-10-12  Luis Machado  <lgustavo@codesourcery.com>

* gdb.python/py-value.c (main): Check if argv is NULL before using it.
* gdb.python/py-value.exp (test_value_in_inferior): Don't use hardcoded
argc values.
Add 1 to argc so we guarantee distinct initial/modified argc values.

gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-value.c
gdb/testsuite/gdb.python/py-value.exp

index b4ccd4aca57041400a9b3ec3e2779709406f4195..21ee095624b79fb77954cbcab1f99dbb092e856b 100644 (file)
@@ -1,3 +1,10 @@
+2016-10-12  Luis Machado  <lgustavo@codesourcery.com>
+
+       * gdb.python/py-value.c (main): Check if argv is NULL before using it.
+       * gdb.python/py-value.exp (test_value_in_inferior): Don't use hardcoded
+       argc values.
+       Add 1 to argc so we guarantee distinct initial/modified argc values.
+
 2016-10-11  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
        * gdb.arch/powerpc-prologue.c (optimized_1): New declaration.
index 586a0fd36959b3897905304316a6a3fd430f29c0..8a8ace680649194799dedf4fb2d1e385ba6c76c1 100644 (file)
@@ -82,7 +82,7 @@ char **save_argv;
 int
 main (int argc, char *argv[])
 {
-  char *cp = argv[0]; /* Prevent gcc from optimizing argv[] out.  */
+  char *cp;
   struct s s;
   union u u;
   PTR x = &s;
@@ -99,6 +99,14 @@ main (int argc, char *argv[])
   const char *sn = 0;
   struct str *xstr;
 
+  /* Prevent gcc from optimizing argv[] out.  */
+
+  /* We also check for a NULL argv in case we are dealing with a target
+     executing in a freestanding environment, therefore there are no
+     guarantees about argc or argv.  */
+  if (argv != NULL)
+    cp = argv[0];
+
   s.a = 3;
   s.b = 5;
   u.a = 7;
index 662c5b44696ce3ad760b3fbce348e54f271977a5..89be6598cbfe2d5f50cb283bcf834ae024604db5 100644 (file)
@@ -274,16 +274,17 @@ proc test_value_in_inferior {} {
     gdb_test "python inval2 = inval+1" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
     gdb_test "python inval.fetch_lazy ()" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test
   }
+  set argc_value [get_integer_valueof "argc" 0]
   gdb_test "python argc_lazy = gdb.parse_and_eval('argc')"
   gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')"
   gdb_test "python argc_notlazy.fetch_lazy()"
   gdb_test "python print (argc_lazy.is_lazy)" "True"
   gdb_test "python print (argc_notlazy.is_lazy)" "False"
-  gdb_test "print argc" " = 1" "sanity check argc"
+  gdb_test "print argc" " = $argc_value" "sanity check argc"
   gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue"
-  gdb_test_no_output "set argc=2"
-  gdb_test "python print (argc_notlazy)" "\r\n1"
-  gdb_test "python print (argc_lazy)" "\r\n2"
+  gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc"
+  gdb_test "python print (argc_notlazy)" "\r\n$argc_value"
+  gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]"
   gdb_test "python print (argc_lazy.is_lazy)" "False"
 
   # Test string fetches,  both partial and whole.
This page took 0.035794 seconds and 4 git commands to generate.