binutils-gdb/gdb/testsuite/gdb.multi/inferior-specific-bp.exp
Andrew Burgess 6cce025114 gdb: only insert thread-specific breakpoints in the relevant inferior
This commit updates GDB so that thread or inferior specific
breakpoints are only inserted into the program space in which the
specific thread or inferior is running.

In terms of implementation, getting this basically working is easy
enough, now that a breakpoint's thread or inferior field is setup
prior to GDB looking for locations, we can easily use this information
to find a suitable program_space and pass this to as a filter when
creating the sals.

Or we could if breakpoint_ops::create_sals_from_location_spec allowed
us to pass in a filter program_space.

So, this commit extends breakpoint_ops::create_sals_from_location_spec
to take a program_space argument, and uses this to filter the set of
returned sals.  This accounts for about half the change in this patch.

The second set of changes starts from breakpoint_set_thread and
breakpoint_set_inferior, this is called when the thread or inferior
for a breakpoint changes, e.g. from the Python API.

Previously this call would never result in the locations of a
breakpoint changing, after all, locations were inserted in every
program space, and we just use the thread or inferior variable to
decide when we should stop.  Now though, changing a breakpoint's
thread or inferior can mean we need to figure out a new set of
breakpoint locations.

To support this I've added a new breakpoint_re_set_one function, which
is like breakpoint_re_set, but takes a single breakpoint, and just
updates the locations for that one breakpoint.  We only need to call
this function if the program_space in which a breakpoint's thread (or
inferior) is running actually changes.  If the program_space does
change then we call the new breakpoint_re_set_one function passing in
the program_space which should be used to filter the new locations (or
nullptr to indicate we should set locations in all program spaces).
This filter program_space needs to propagate down to all the re_set
methods, this accounts for the remaining half of the changes in this
patch.

There were a couple of existing tests that created thread or inferior
specific breakpoints and then checked the 'info breakpoints' output,
these needed updating.  These were:

  gdb.mi/user-selected-context-sync.exp
  gdb.multi/bp-thread-specific.exp
  gdb.multi/multi-target-continue.exp
  gdb.multi/multi-target-ping-pong-next.exp
  gdb.multi/tids.exp
  gdb.mi/new-ui-bp-deleted.exp
  gdb.multi/inferior-specific-bp.exp
  gdb.multi/pending-bp-del-inferior.exp

I've also added some additional tests to:

  gdb.multi/pending-bp.exp

I've updated the documentation and added a NEWS entry.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2024-09-07 21:48:35 +01:00

172 lines
5.7 KiB
Plaintext

# Copyright 2022-2024 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Test inferior-specific breakpoints.
standard_testfile -1.c -2.c
if {[use_gdb_stub]} {
return
}
set srcfile1 ${srcfile}
set binfile1 ${binfile}-1
set binfile2 ${binfile}-2
if {[build_executable ${testfile}.exp ${binfile1} "${srcfile1}"] != 0} {
return -1
}
if {[build_executable ${testfile}.exp ${binfile2} "${srcfile2}"] != 0} {
return -1
}
# Start the first inferior.
clean_restart ${binfile1}
if {![runto_main]} {
return
}
# Add a second inferior, and start this one too.
gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2"
gdb_load $binfile2
if {![runto_main]} {
return
}
# Try to create a breakpoint using both the 'inferior' and 'thread' keywords,
# this should fail. Try with the keywords in both orders just in case the
# parser has a bug.
gdb_test "break foo thread 1.1 inferior 1" \
"You can specify only one of thread, inferior, or task\\."
gdb_test "break foo inferior 1 thread 1.1" \
"You can specify only one of thread, inferior, or task\\."
# Try to create a breakpoint using the 'inferior' keyword multiple times.
gdb_test "break foo inferior 1 inferior 2" \
"You can specify only one inferior\\."
# Clear out any other breakpoints.
delete_breakpoints
# Use 'info breakpoint' to check that the inferior specific breakpoint is
# present in the breakpoint list. TESTNAME is the name used for this test,
# BP_NUMBER is the number for the breakpoint, and EXPECTED_LOC_COUNT is the
# number of locations we expect for that breakpoint.
proc check_info_breakpoints { testname bp_number expected_loc_count } {
gdb_test_multiple "info breakpoints $bp_number" $testname {
-re "\r\nNum\\s+\[^\r\n\]+\r\n" {
exp_continue
}
-re "^$bp_number\\s+breakpoint\\s+keep\\s+y\\s+<MULTIPLE>\\s*\r\n" {
set saw_header true
exp_continue
}
-re "^\\s+stop only in inferior 1\r\n" {
set saw_inf_cond true
exp_continue
}
-re "^\\s+breakpoint already hit $::decimal times\r\n" {
exp_continue
}
-re "^$bp_number\\.\[123\]\\s+y\\s+ $::hex in foo at \[^\r\n\]+(?: inf \[12\])?\r\n" {
incr location_count
exp_continue
}
-re "^$::gdb_prompt $" {
with_test_prefix $gdb_test_name {
gdb_assert { $saw_header \
&& $location_count == $expected_loc_count \
&& $saw_inf_cond } \
$gdb_test_name
}
}
}
}
# Create an inferior-specific breakpoint. Use gdb_test instead of
# gdb_breakpoint here as we want to check the breakpoint was placed in
# multiple locations.
gdb_test "break foo inferior 1" \
"Breakpoint $decimal at $hex: foo\\. \\(2 locations\\)"
set bp_number [get_integer_valueof "\$bpnum" "INVALID" \
"get b/p number for inferior specific breakpoint"]
set saw_header false
set location_count 0
set saw_inf_cond false
check_info_breakpoints "first check for inferior specific breakpoint" \
$bp_number 2
# Create a multi-inferior breakpoint to stop at.
gdb_breakpoint "stop_breakpt" message
set stop_bp_num [get_integer_valueof "\$bpnum" "INVALID" \
"get b/p number for stop_breakpt"]
# Now resume inferior 2, this should reach 'stop_breakpt'.
gdb_test "continue" \
"hit Breakpoint $stop_bp_num\.$decimal, stop_breakpt \\(\\) .*" \
"continue in inferior 2"
# Switch to inferior 1, and try there.
gdb_test "inferior 1" ".*" \
"select inferior 1 to check the inferior-specific b/p works"
gdb_test "continue " \
"Thread 1\\.${decimal}\[^\r\n\]* hit Breakpoint\
$bp_number\.$decimal, foo \\(\\) .*" \
"first continue in inferior 1"
# Now back to inferior 2, let the inferior exit, and then remove the
# inferior, the inferior-specific breakpoint should not be deleted.
gdb_test "inferior 2" ".*" \
"switch back to allow inferior 2 to exit"
gdb_test "continue" "\\\[Inferior 2 \[^\r\n\]+ exited normally\\\]" \
"allow inferior 2 to exit"
gdb_test "inferior 1" ".*" \
"back to inferior 1 so inferior 2 can be deleted"
gdb_test_no_output "remove-inferiors 2"
gdb_test "continue " "hit Breakpoint $bp_number\.$decimal, foo \\(\\) .*" \
"second continue in inferior 1"
gdb_test "continue " "hit Breakpoint $stop_bp_num, stop_breakpt \\(\\) .*" \
"third continue in inferior 1"
# Now allow inferior 1 to exit, the inferior specific breakpoint
# should not be deleted.
gdb_test "continue" \
"\\\[Inferior 1 \[^\r\n\]+ exited normally\\\]" \
"allow inferior 1 to exit"
check_info_breakpoints "second check for inferior specific breakpoint" \
$bp_number 2
# Now create another new inferior, then remove inferior 1. As a result of
# this removal, the inferior specific breakpoint should be deleted.
gdb_test "add-inferior" "Added inferior 3.*" "add empty inferior 3"
gdb_test "inferior 3" "Switching to inferior 3.*" "switch to inferior 3"
gdb_test "remove-inferiors 1" \
"Inferior-specific breakpoint $bp_number deleted - inferior 1 has been removed\\."
# Now check 'info breakpoints' to ensure the breakpoint is gone.
gdb_test "info breakpoints $bp_number" \
"No breakpoint, watchpoint, tracepoint, or catchpoint matching '$bp_number'\\."