xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/clone-attach-detach.exp (revision 73d56d5b0be8704e4f0a7e8221a2c7309572c9a1)
1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2016-2019 Free Software Foundation, Inc.
4
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.
9#
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.
14#
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/>.
17
18# Test attach / detach from a process that uses raw clone.  We use raw
19# clone as proxy for when libthread_db is not available.
20
21# This only works on targets with the Linux kernel.
22if ![istarget *-*-linux*] {
23    return
24}
25
26if {![can_spawn_for_attach]} {
27    return 0
28}
29
30standard_testfile
31
32if [prepare_for_testing "failed to prepare" $testfile $srcfile {debug}] {
33    return -1
34}
35
36clean_restart ${binfile}
37
38set test_spawn_id [spawn_wait_for_attach $binfile]
39set testpid [spawn_id_get_pid $test_spawn_id]
40
41# Native/gdbserver.
42set thread_re "(LWP $decimal|Thread )"
43
44# Try attach / detach a few times, in case GDB ends up with stale
45# state after detaching.
46
47set attempts 3
48for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
49    with_test_prefix "fg attach $attempt" {
50
51	gdb_test "attach $testpid" \
52	    "Attaching to program.*process $testpid.*" \
53	    "attach"
54
55	gdb_test "info threads" \
56	    "1.*${thread_re}.*\r\n.*2.*${thread_re}.*" \
57	    "info threads shows two LWPs"
58
59	gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]"
60    }
61}
62
63# Same, but use async/background attach.
64
65# If debugging with target remote, check whether the all-stop variant
66# of the RSP is being used.  If so, we can't run the background tests.
67if {[target_info exists gdb_protocol]
68    && ([target_info gdb_protocol] == "remote"
69	|| [target_info gdb_protocol] == "extended-remote")} {
70
71    set test "maint show target-non-stop"
72    gdb_test_multiple "maint show target-non-stop" $test {
73	-re "(is|currently) on.*$gdb_prompt $" {
74	}
75	-re "(is|currently) off.*$gdb_prompt $" {
76	    unsupported "bg attach: can't issue info threads while target is running"
77	    return 0
78	}
79    }
80}
81
82set attempts 3
83for {set attempt 1} {$attempt <= $attempts} {incr attempt} {
84    with_test_prefix "bg attach $attempt" {
85
86	gdb_test "attach $testpid &" \
87	    "Attaching to program.*process $testpid.*" \
88	    "attach"
89
90	gdb_test "info threads" \
91	    "1.*${thread_re}.*\\(running\\)\r\n.*2.*${thread_re}.*\\(running\\)" \
92	    "info threads shows two LWPs"
93
94	gdb_test "detach" "Detaching from .*, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]"
95    }
96}
97
98kill_wait_spawned_process $test_spawn_id
99