gdb/testsuite: Work around clang fails in gdb.base/watchpoint.exp

Message ID 20231026090448.1182959-1-blarsen@redhat.com
State New
Headers
Series gdb/testsuite: Work around clang fails in gdb.base/watchpoint.exp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Guinevere Larsen Oct. 26, 2023, 9:04 a.m. UTC
  as mentioned in commit def86538a46f7ce6fbb215cfb184e23015b5d538, clang
doesn't use the CFA information for variable locations, making it so
software watchpoints get false hits when exiting a function. Differently
to that commit, however, gdb.base/watchpoint.exp also needs to be
explicitly continued so the inferior will remain in sync with what the
exp file expects, so this commit uses gdb_test_multiple to identify that
situation.

I also chose to keep the test passing in that scenario because the GDB
feature being tested, software watchpoints, is working as expected.
---
 gdb/testsuite/gdb.base/watchpoint.exp | 82 +++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 6 deletions(-)
  

Comments

Andrew Burgess Oct. 27, 2023, 1:56 p.m. UTC | #1
Guinevere Larsen <blarsen@redhat.com> writes:

> as mentioned in commit def86538a46f7ce6fbb215cfb184e23015b5d538, clang
> doesn't use the CFA information for variable locations, making it so
> software watchpoints get false hits when exiting a function. Differently
> to that commit, however, gdb.base/watchpoint.exp also needs to be
> explicitly continued so the inferior will remain in sync with what the
> exp file expects, so this commit uses gdb_test_multiple to identify that
> situation.
>
> I also chose to keep the test passing in that scenario because the GDB
> feature being tested, software watchpoints, is working as expected.

I'm not sure I 100% agree with this conclusion.  The test is placing on
a watchpoint that isn't expected to change ... yet we see the watchpoint
trigger.  I'd say the test is failing.

I wonder if we could just set a flag when we see an unexpected stop, and
then xfail rather than pass once the watchpoint goes out of scope?

I think the right fix would be to implement the
gdbarch_stack_frame_destroyed_p method for amd64.  Looking in
amd64-tdep.c I noticed we actually already have
amd64_stack_frame_destroyed_p, we just don't register it with the
gdbarch ... I guess there's some history here.

Anyway, the patch below (untested except for watchpoint.exp) seems to
remove the need for any changes to watchpoint.exp, what are your
thoughts?


> ---
>  gdb/testsuite/gdb.base/watchpoint.exp | 82 +++++++++++++++++++++++++--
>  1 file changed, 76 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/testsuite/gdb.base/watchpoint.exp b/gdb/testsuite/gdb.base/watchpoint.exp
> index 70864655c6d..5d8d6b9cb31 100644
> --- a/gdb/testsuite/gdb.base/watchpoint.exp
> +++ b/gdb/testsuite/gdb.base/watchpoint.exp
> @@ -30,6 +30,11 @@ if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
>       return -1
>  }
>  
> +set using_clang 0
> +if {[test_compiler_info "clang-*"]} {
> +    set using_clang 1
> +}

You can actually use 'true' and 'false' here, given this is a boolean.

> +
>  # True if we're forcing no hardware watchpoints.
>  set no_hw 0
>  
> @@ -486,6 +491,20 @@ proc test_complex_watchpoint {} {
>  		}
>  		fail $test
>  	    }
> +	    -re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
> +		global no_hw
> +		global using_clang
> +		# Clang doesn't use the CFA, so software watchpoints get one
> +		# false hit here.  Detect if we're in that situation and
> +		# ignore the false hit.  For more info, see:
> +		# https://github.com/llvm/llvm-project/issues/64390
> +		if {$using_clang == 1 && $no_hw == 1} {

And here (and elsewhere) you can write:

  if {$using_clang && $no_hw} {

which better reflects the boolean nature of these flags.

> +		    send_gdb "cont\n"
> +		    exp_continue
> +		} else {
> +		    fail $gdb_test_name
> +		}
> +	    }
>  	}
>  
>  	gdb_continue_to_breakpoint "func2 breakpoint here, second time"
> @@ -501,8 +520,25 @@ proc test_complex_watchpoint {} {
>                   "trigger1 partially local watch"
>          gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
>                   "trigger2 partially local watch"
> -        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
> -                 "self-delete partially local watch"
> +        gdb_test_multiple "cont" "self-delete partially local watch" {
> +	    -re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
> +		pass $gdb_test_name
> +	    }
> +	    -re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
> +		global no_hw
> +		global using_clang
> +		# Clang doesn't use the CFA, so software watchpoints get one
> +		# false hit here.  Detect if we're in that situation and
> +		# ignore the false hit.  For more info, see:
> +		# https://github.com/llvm/llvm-project/issues/64390
> +		if {$using_clang == 1 && $no_hw == 1} {
> +		    send_gdb "cont\n"
> +		    exp_continue
> +		} else {
> +		    fail $gdb_test_name
> +		}
> +	    }
> +	}
>  
>          # We should be in "func2" again now.  Test a watch of a
>          # static (non-stack-based) local.  Since this has scope
> @@ -535,8 +571,25 @@ proc test_complex_watchpoint {} {
>  		"set local watch in recursive call"
>  	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_x.*New value = 2.*" \
>  		"trigger local watch in recursive call"
> -	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
> -		"self-delete local watch in recursive call"
> +	    gdb_test_multiple "cont" "self-delete local watch in recursive call" {
> +		-re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
> +		    pass $gdb_test_name
> +		}
> +		-re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
> +		    global no_hw
> +		    global using_clang
> +		    # Clang doesn't use the CFA, so software watchpoints get one
> +		    # false hit here.  Detect if we're in that situation and
> +		    # ignore the false hit.  For more info, see:
> +		    # https://github.com/llvm/llvm-project/issues/64390
> +		    if {$using_clang == 1 && $no_hw == 1} {
> +			send_gdb "cont\n"
> +			exp_continue
> +		    } else {
> +			fail $gdb_test_name
> +		    }
> +		}
> +	    }
>  	}
>  
>          # Repeat the preceding test, but this time use "recurser::local_x" as
> @@ -551,8 +604,25 @@ proc test_complex_watchpoint {} {
>  		"set local watch in recursive call with explicit scope"
>  	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: recurser::local_x.*New value = 2.*" \
>  		"trigger local watch with explicit scope in recursive call"
> -	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
> -		"self-delete local watch with explicit scope in recursive call (2)"
> +	    gdb_test_multiple "cont" "self-delete local watch with explicit scope in recursive call (2)" {
> +		-re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
> +		    pass $gdb_test_name
> +		}
> +		-re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
> +		    global no_hw
> +		    global using_clang
> +		    # Clang doesn't use the CFA, so software watchpoints get one
> +		    # false hit here.  Detect if we're in that situation and
> +		    # ignore the false hit.  For more info, see:
> +		    # https://github.com/llvm/llvm-project/issues/64390
> +		    if {$using_clang == 1 && $no_hw == 1} {
> +			send_gdb "cont\n"
> +			exp_continue
> +		    } else {
> +			fail $gdb_test_name
> +		    }
> +		}
> +	    }
>  	}
>  
>  	# Disable everything so we can finish the program at full speed
> -- 
> 2.41.0

Thanks,
Andrew

---

diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index e6feee677b3..15120d55976 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -2888,7 +2888,7 @@ static const struct frame_base amd64_frame_base =
 
 /* Normal frames, but in a function epilogue.  */
 
-/* Implement the stack_frame_destroyed_p gdbarch method.
+/* Implement core of the stack_frame_destroyed_p gdbarch method.
 
    The epilogue is defined here as the 'ret' instruction, which will
    follow any instruction such as 'leave' or 'pop %ebp' that destroys
@@ -2908,6 +2908,33 @@ amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
   return 1;
 }
 
+/* Implement the gdbarch_stack_frame_destroyed_p method.
+
+   This wrapper delegates to amd64_stack_frame_destroyed_p_1 for compilers
+   that we know get the debug information for stack local variables wrong
+   (i.e. Clang) during the function epilogue.
+
+   For other compilers (i.e. not Clang) we trust the debug information.  */
+
+static int
+amd64_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
+{
+  struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
+
+  /* LLVM backend (Clang/Flang) doesn't use CFA based locations for stack
+     local variables.  As a consequence, when we enter the function
+     epilogue GDB will calculate the wrong location for stack local
+     variables, and watchpoints will trigger.  */
+  if (cust != nullptr
+      && cust->producer () != nullptr
+      && producer_is_llvm (cust->producer ()))
+    return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
+
+  /* For other producers, trust the debug information.  */
+  return 0;
+}
+
+
 static int
 amd64_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
 				frame_info_ptr this_frame,
@@ -2938,7 +2965,7 @@ amd64_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
     }
 
   /* Check whether we're in an epilogue.  */
-  return amd64_stack_frame_destroyed_p (gdbarch, pc);
+  return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
 }
 
 static int
@@ -3310,6 +3337,8 @@ amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
 
   set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
 
+  set_gdbarch_stack_frame_destroyed_p (gdbarch, amd64_stack_frame_destroyed_p);
+
   /* SystemTap variables and functions.  */
   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
  

Patch

diff --git a/gdb/testsuite/gdb.base/watchpoint.exp b/gdb/testsuite/gdb.base/watchpoint.exp
index 70864655c6d..5d8d6b9cb31 100644
--- a/gdb/testsuite/gdb.base/watchpoint.exp
+++ b/gdb/testsuite/gdb.base/watchpoint.exp
@@ -30,6 +30,11 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
+set using_clang 0
+if {[test_compiler_info "clang-*"]} {
+    set using_clang 1
+}
+
 # True if we're forcing no hardware watchpoints.
 set no_hw 0
 
@@ -486,6 +491,20 @@  proc test_complex_watchpoint {} {
 		}
 		fail $test
 	    }
+	    -re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
+		global no_hw
+		global using_clang
+		# Clang doesn't use the CFA, so software watchpoints get one
+		# false hit here.  Detect if we're in that situation and
+		# ignore the false hit.  For more info, see:
+		# https://github.com/llvm/llvm-project/issues/64390
+		if {$using_clang == 1 && $no_hw == 1} {
+		    send_gdb "cont\n"
+		    exp_continue
+		} else {
+		    fail $gdb_test_name
+		}
+	    }
 	}
 
 	gdb_continue_to_breakpoint "func2 breakpoint here, second time"
@@ -501,8 +520,25 @@  proc test_complex_watchpoint {} {
                  "trigger1 partially local watch"
         gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
                  "trigger2 partially local watch"
-        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
-                 "self-delete partially local watch"
+        gdb_test_multiple "cont" "self-delete partially local watch" {
+	    -re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
+		pass $gdb_test_name
+	    }
+	    -re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
+		global no_hw
+		global using_clang
+		# Clang doesn't use the CFA, so software watchpoints get one
+		# false hit here.  Detect if we're in that situation and
+		# ignore the false hit.  For more info, see:
+		# https://github.com/llvm/llvm-project/issues/64390
+		if {$using_clang == 1 && $no_hw == 1} {
+		    send_gdb "cont\n"
+		    exp_continue
+		} else {
+		    fail $gdb_test_name
+		}
+	    }
+	}
 
         # We should be in "func2" again now.  Test a watch of a
         # static (non-stack-based) local.  Since this has scope
@@ -535,8 +571,25 @@  proc test_complex_watchpoint {} {
 		"set local watch in recursive call"
 	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_x.*New value = 2.*" \
 		"trigger local watch in recursive call"
-	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
-		"self-delete local watch in recursive call"
+	    gdb_test_multiple "cont" "self-delete local watch in recursive call" {
+		-re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
+		    pass $gdb_test_name
+		}
+		-re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
+		    global no_hw
+		    global using_clang
+		    # Clang doesn't use the CFA, so software watchpoints get one
+		    # false hit here.  Detect if we're in that situation and
+		    # ignore the false hit.  For more info, see:
+		    # https://github.com/llvm/llvm-project/issues/64390
+		    if {$using_clang == 1 && $no_hw == 1} {
+			send_gdb "cont\n"
+			exp_continue
+		    } else {
+			fail $gdb_test_name
+		    }
+		}
+	    }
 	}
 
         # Repeat the preceding test, but this time use "recurser::local_x" as
@@ -551,8 +604,25 @@  proc test_complex_watchpoint {} {
 		"set local watch in recursive call with explicit scope"
 	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: recurser::local_x.*New value = 2.*" \
 		"trigger local watch with explicit scope in recursive call"
-	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
-		"self-delete local watch with explicit scope in recursive call (2)"
+	    gdb_test_multiple "cont" "self-delete local watch with explicit scope in recursive call (2)" {
+		-re -wrap "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" {
+		    pass $gdb_test_name
+		}
+		-re -wrap ".*Continuing.*\[Ww\]atchpoint.*" {
+		    global no_hw
+		    global using_clang
+		    # Clang doesn't use the CFA, so software watchpoints get one
+		    # false hit here.  Detect if we're in that situation and
+		    # ignore the false hit.  For more info, see:
+		    # https://github.com/llvm/llvm-project/issues/64390
+		    if {$using_clang == 1 && $no_hw == 1} {
+			send_gdb "cont\n"
+			exp_continue
+		    } else {
+			fail $gdb_test_name
+		    }
+		}
+	    }
 	}
 
 	# Disable everything so we can finish the program at full speed