2008-12-31 Pedro Alves <pedro@codesourcery.com>
[external/binutils.git] / gdb / testsuite / gdb.threads / attach-stopped.exp
1 # Copyright 2008
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # This test was created by modifying attach.exp.
18 # This file was created by Jeff Johnston <jjohnstn@redhat.com>.
19 # This file was updated by Jan Kratochvil <jan.kratochvil@redhat.com>.
20
21 # This test only works on Linux
22 if { ![isnative] || [is_remote host] || ![istarget *-linux*] } {
23     continue
24 }
25
26 set testfile "attach-stopped"
27 set srcfile  ${testfile}.c
28 set binfile  ${objdir}/${subdir}/${testfile}
29 set escapedbinfile  [string_to_regexp ${objdir}/${subdir}/${testfile}]
30
31 #execute_anywhere "rm -f ${binfile}"
32 remote_exec build "rm -f ${binfile}"
33 # For debugging this test
34 #
35 #log_user 1
36
37 proc corefunc { threadtype } {
38     global srcfile
39     global binfile
40     global escapedbinfile
41     global srcdir
42     global subdir
43     global gdb_prompt
44
45     if [get_compiler_info ${binfile}] {
46         return -1
47     }
48
49     # Start the program running and then wait for a bit, to be sure
50     # that it can be attached to.
51
52     set testpid [eval exec $binfile &]
53
54     # Avoid some race:
55     sleep 2
56
57     # Stop the program 
58     remote_exec build "kill -s STOP ${testpid}"
59
60     # Start with clean gdb
61     gdb_exit
62     gdb_start
63     gdb_reinitialize_dir $srcdir/$subdir
64     gdb_load ${binfile}
65
66     # Verify that we can attach to the stopped process.
67        
68     set test "$threadtype: attach2 to stopped, after setting file"
69     gdb_test_multiple "attach $testpid" "$test" {
70         -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
71             pass "$test"
72         }
73     }
74
75     # ".*sleep.*clone.*" would fail on s390x as bt stops at START_THREAD there.
76     if {[string equal $threadtype threaded]} {
77         gdb_test "thread apply all bt" ".*sleep.*start_thread.*" "$threadtype: attach2 to stopped bt"
78     } else {
79         gdb_test "bt" ".*sleep.*main.*" "$threadtype: attach2 to stopped bt"
80     }
81     # This breakpoint is there for old/non-x86 kernels not restarting syscalls.
82     gdb_breakpoint [gdb_get_line_number "Second sleep"]
83     set test "$threadtype: attach2 continue"
84     send_gdb "continue\n"
85     gdb_expect {
86       -re "Continuing"
87         { pass "continue ($test)" }
88       timeout
89         { fail "continue ($test) (timeout)" }
90     }
91
92     # For this to work we must be sure to consume the "Continuing."
93     # message first, or GDB's signal handler may not be in place.
94     after 1000 {send_gdb "\003"}
95     set test "$threadtype: attach2 stop interrupt"
96     gdb_expect 10 {
97       -re "Program received signal SIGINT.*$gdb_prompt $"
98         {
99           pass $test
100         }
101       -re "Breakpoint \[0-9\].*$srcfile.*$gdb_prompt $"
102         {
103           pass $test
104         }
105       timeout
106         {
107           fail $test
108         }
109     }
110
111     gdb_exit
112
113     # Avoid some race:
114     sleep 2
115
116     # At this point, the process should be sleeping
117
118     if [catch {open /proc/${testpid}/status r} fileid2] {
119         set line2 "NOTFOUND"
120     } else {
121         gets $fileid2 line1;
122         gets $fileid2 line2;
123         close $fileid2;
124     }
125
126     set test "$threadtype: attach2, exit leaves process sleeping"
127     if {[string match "*(sleeping)*" $line2]} {
128       pass $test
129     } else {
130       fail $test
131     }
132
133     # Make sure we don't leave a process around to confuse
134     # the next test run (and prevent the compile by keeping
135     # the text file busy), in case the "set should_exit" didn't
136     # work.
137        
138     remote_exec build "kill -9 ${testpid}"
139 }
140
141 # build the test case first without threads
142 #
143 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
144     untested "attach-stopped.exp (unthreaded)"
145     return -1
146 }
147
148 corefunc nonthreaded
149
150 # build the test case first without threads
151 #
152 if  { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-DUSE_THREADS}] != "" } {
153     untested "attach-stopped.exp (threaded)"
154     return -1
155 }
156
157 corefunc threaded
158
159 return 0