fc5a3ccb0a5ee2eb08ded1afead345529df3ee3c
[external/binutils.git] / gdb / testsuite / gdb.threads / gcore-thread.exp
1 # Copyright 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
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 file was written by Michael Snyder (msnyder@redhat.com)
18 # This is a test for the gdb command "generate-core-file".
19
20 if $tracelevel then {
21         strace $tracelevel
22 }
23
24 # Single-threaded test case
25 set testfile "gcore-thread"
26 set srcfile  pthreads.c
27 set objfile  ${objdir}/${subdir}/${testfile}.o
28 set binfile  ${objdir}/${subdir}/${testfile}
29 set corefile ${objdir}/${subdir}/${testfile}.test
30 set core0file ${objdir}/${subdir}/${testfile}0.test
31
32 if [istarget "*-*-linux"] then {
33     set target_cflags "-D_MIT_POSIX_THREADS"
34 } else {
35     set target_cflags ""
36 }
37
38 # Attempt to prevent -Wl,-z,relro which happens by default at least on
39 # Kubuntu-10.10.  Due to PR corefiles/11804 will then GDB be unable to find
40 # libpthread, therefore libthread_db will not fail as expected
41 # on the test `zeroed-threads cannot be listed'.
42
43 set opts [list debug "incdir=${objdir}"]
44 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${objfile}" object $opts] != ""
45     || ([gdb_compile_pthreads "${objfile}" "${binfile}" executable [concat $opts {additional_flags=-Wl,-z,norelro}] ] != ""
46         && [gdb_compile_pthreads "${objfile}" "${binfile}" executable $opts] != "") } {
47     return -1
48 }
49
50 # Now we can proceed with the real testing.
51
52 # Start with a fresh gdb.
53
54 clean_restart ${testfile}
55
56 # regexp for "horizontal" text (i.e. doesn't include newline or
57 # carriage return)
58 set horiz "\[^\n\r\]*"
59
60 # regexp for newline
61 set nl "\[\r\n\]+"
62
63 set timeout 30
64
65 gdb_test_multiple "help gcore" "help gcore" {
66     -re "Undefined command: .gcore.*$gdb_prompt $" {
67         # gcore command not supported -- nothing to test here.
68         unsupported "gdb does not support gcore on this target"
69         return -1;
70     }
71     -re "Save a core file .*$gdb_prompt $" {
72         pass "help gcore"
73     }
74 }
75
76 if { ! [ runto_main ] } then {
77     untested gcore-thread.exp
78     return -1
79 }
80
81 gdb_test_multiple "info threads" "threads are supported" {
82     -re ".* main .*$gdb_prompt $" {
83         # OK, threads are supported.
84     }
85     -re "${nl}$gdb_prompt $" {
86         unsupported "gdb does not support threads on this target"
87         return -1;
88     }
89 }
90
91 # Make sure thread 1 is running
92 delete_breakpoints
93 gdb_breakpoint "thread1"
94 gdb_test "continue" "Continuing.*Breakpoint.* thread1 .*" "thread 1 is running"
95
96 # Make sure thread 2 is running
97 delete_breakpoints
98 gdb_breakpoint "thread2"
99 gdb_test "continue" "Continuing.*Breakpoint.* thread2 .*" "thread 2 is running"
100
101 set escapedfilename [string_to_regexp $corefile]
102 # Drop corefile
103 gdb_test_multiple "gcore $corefile" "save a corefile" \
104 {
105   -re "Saved corefile ${escapedfilename}\[\r\n\]+$gdb_prompt $" {
106     pass "save a corefile"
107     global core_supported
108     set core_supported 1
109   }
110   -re "Can't create a corefile\[\r\n\]+$gdb_prompt $" {
111     unsupported "save a corefile"
112     global core_supported
113     set core_supported 0
114   }
115 }
116
117 global core_supported
118 if {!$core_supported} {
119   return -1
120 }
121
122
123 # Test the uninitialized thread list.
124 # Provide the case of glibc td_thr_get_info handling of:
125 # /* Special case for the main thread before initialization.  */
126
127 foreach symbol {__stack_user stack_used} {
128     set test "clear ${symbol}.next"
129     gdb_test_multiple "p *(void **) &${symbol} = 0" $test {
130         -re " = \\(void \\*\\) 0x0\r\n$gdb_prompt $" {
131             pass $test
132         }
133         -re "No symbol \"${symbol}\" in current context\\.\r\n$gdb_prompt $" {
134             xfail $test
135             # Do not do the verification.
136             set core0file ""
137         }
138     }
139 }
140
141 if {"$core0file" != ""} {
142     gdb_test "gcore $core0file" "Saved corefile .*" "save a zeroed-threads corefile"
143 }
144
145
146 # Now restart gdb and load the corefile.
147 clean_restart ${testfile}
148
149 proc load_core { corefile } {
150     global gdb_prompt
151     global libthread_db_seen
152
153     set libthread_db_seen 0
154     gdb_test_multiple "core $corefile" \
155         "re-load generated corefile" {
156             -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" {
157                 set libthread_db_seen 1
158                 exp_continue
159             }
160             -re " is not a core dump:.*\r\n$gdb_prompt $" {
161                 fail "re-load generated corefile (bad file format)"
162                 # No use proceeding from here.
163                 return 0;
164             }
165             -re ": No such file or directory.*\r\n$gdb_prompt $" {
166                 fail "re-load generated corefile (file not found)"
167                 # No use proceeding from here.
168                 return 0;
169             }
170             -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" {
171                 fail "re-load generated corefile (incomplete note section)"
172             }
173             -re "Core was generated by .*\r\n$gdb_prompt $" {
174                 pass "re-load generated corefile"
175             }
176         }
177     return 1
178 }
179
180 if ![load_core $corefile] {
181     return
182 }
183
184 # FIXME: now what can we test about the thread state?
185 # We do not know for certain that there should be at least 
186 # three threads, because who knows what kind of many-to-one
187 # mapping various OS's may do?  Let's assume that there must
188 # be at least two threads:
189
190 gdb_test "info threads" ".*${nl}  2 ${horiz}${nl}\\* 1 .*" \
191         "corefile contains at least two threads"
192
193 # One thread in the corefile should be in the "thread2" function.
194
195 gdb_test "info threads" ".* thread2 .*" \
196         "a corefile thread is executing thread2"
197
198 # The thread2 thread should be marked as the current thread.
199
200 gdb_test "info threads" ".*${nl}\\* ${horiz} thread2 .*" \
201         "thread2 is current thread in corefile"
202
203
204 # Test the uninitialized thread list.
205
206 if {"$core0file" != "" && [load_core $core0file]} {
207     set test "zeroed-threads cannot be listed"
208
209     if {!$libthread_db_seen} {
210         verbose -log "No libthread_db loaded - -Wl,-z,relro compilation?"
211         xfail $test
212     } else {
213         gdb_test "info threads" "Cannot find new threads: .*" $test
214     }
215 }