OSDN Git Service

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