1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2007-2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Test attaching to already-running programs using extended-remote.
20 load_lib gdbserver-support.exp
21 load_lib trace-support.exp
25 require allow_gdbserver_tests
27 require can_spawn_for_attach
29 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
33 # Run the test. TARGET_NON_STOP and TARGET_ASYNC should be 'on'
34 # or 'off'. TO_DISABLE should be either the empty string, or
35 # something that can be passed to gdbserver's --disable-packet command
37 proc run_test { target_async target_non_stop to_disable } {
38 save_vars { ::GDBFLAGS } {
39 append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
40 append ::GDBFLAGS " -ex \"maintenance set target-async ${target_async}\""
42 # If GDB and GDBserver are both running locally, set the sysroot to avoid
43 # reading files via the remote protocol.
44 if { ![is_remote host] && ![is_remote target] } {
45 set ::GDBFLAGS "$::GDBFLAGS -ex \"set sysroot\""
48 clean_restart $::binfile
51 # Make sure we're disconnected, in case we're testing with an
52 # extended-remote board, therefore already connected.
53 gdb_test "disconnect" ".*"
55 if {[gdb_target_supports_trace]} {
56 # Test predefined TSVs are uploaded.
57 gdb_test_sequence "info tvariables" "check uploaded tsv" {
58 "\[\r\n\]+Name\[\t \]+Initial\[\t \]+Current"
59 "\[\r\n\]+\\\$trace_timestamp 0"
63 set target_exec [gdbserver_download_current_prog]
64 if { $to_disable != "" } {
65 set gdbserver_opts "--disable-packet=${to_disable}"
69 gdbserver_start_extended $gdbserver_opts
71 gdb_test_no_output "set remote exec-file $target_exec" "set remote exec-file"
73 set test_spawn_id [spawn_wait_for_attach $::binfile]
74 set testpid [spawn_id_get_pid $test_spawn_id]
76 gdb_test "attach $testpid" \
77 "Attaching to program: .*, process $testpid.*(in|at).*" \
78 "attach to remote program 1"
80 gdb_test "backtrace" ".*main.*" "backtrace 1"
82 gdb_test "detach" "Detaching from program.*process.*"
83 gdb_test "backtrace" "No stack\\." "backtrace with no program"
85 gdb_test "attach $testpid" \
86 "Attaching to program: .*, process $testpid.*(in|at).*" \
87 "attach to remote program 2"
88 gdb_test "backtrace" ".*main.*" "backtrace 2"
90 gdb_test "kill" "" "kill" "Kill the program being debugged. .y or n. " "y"
91 gdb_test_no_output "monitor exit"
93 kill_wait_spawned_process $test_spawn_id
96 foreach_with_prefix target_async {"on" "off" } {
97 foreach_with_prefix target_non_stop {"off" "on"} {
98 foreach_with_prefix to_disable { "" Tthread T } {
99 run_test ${target_async} ${target_non_stop} $to_disable