Update copyright year range in all GDB files
[external/binutils.git] / gdb / testsuite / gdb.base / corefile.exp
1 # Copyright 1992-2018 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 Fred Fish. (fnf@cygnus.com)
17
18
19 # are we on a target board
20 if ![isnative] then {
21     return
22 }
23
24 standard_testfile coremaker.c
25
26 # Create and source the file that provides information about the compiler
27 # used to compile the test case.
28 if [get_compiler_info] {
29     return -1
30 }
31
32 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
33     untested "failed to compile"
34     return -1
35 }
36
37 set corefile [core_find $binfile {coremmap.data}]
38 if {$corefile == ""} {
39     return 0
40 }
41
42 # Test that we can simply startup with a "-core=$corefile" command line arg
43 # and recognize that the core file is a valid, usable core file.
44 # To do this, we must shutdown the currently running gdb and restart
45 # with the -core args.  We can't use gdb_start because it looks for
46 # the first gdb prompt, and the message we are looking for occurs
47 # before the first prompt.
48 #
49 # Another problem is that on some systems (solaris for example), there
50 # is apparently a limit on the length of a fully specified path to 
51 # the corefile executable, at about 80 chars.  For this case, consider
52 # it a pass, but note that the program name is bad.
53
54 gdb_exit
55 if $verbose>1 then {
56     send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS -core=$corefile\n"
57 }
58
59 set oldtimeout $timeout
60 set timeout [expr "$timeout + 60"]
61 verbose "Timeout is now $timeout seconds" 2
62 eval "spawn $GDB $INTERNAL_GDBFLAGS $GDBFLAGS -core=$corefile"
63 expect {
64     -re "Couldn't find .* registers in core file.*$gdb_prompt $" {
65         fail "args: -core=[file tail $corefile] (couldn't find regs)"
66     }
67     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
68         pass "args: -core=[file tail $corefile]"
69     }
70     -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
71         pass "args: -core=[file tail $corefile] (with bad program name)"
72     }
73     -re ".*registers from core file: File in wrong format.* $" {
74         fail "args: -core=[file tail $corefile] (could not read registers from core file)"
75     }
76     -re ".*$gdb_prompt $"       { fail "args: -core=[file tail $corefile]" }
77     timeout             { fail "(timeout) starting with -core" }
78 }
79
80
81 #
82 # Test that startup with both an executable file and -core argument.
83 # See previous comments above, they are still applicable.
84 #
85
86 close
87
88 if $verbose>1 then {
89     send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile\n"
90 }
91
92
93 eval "spawn $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile"
94 expect {
95     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
96         pass "args: execfile -core=[file tail $corefile]"
97     }
98     -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $"     {
99         pass "args: execfile -core=[file tail $corefile] (with bad program name)"
100     }
101     -re ".*registers from core file: File in wrong format.* $" {
102         fail "args: execfile -core=[file tail $corefile] (could not read registers from core file)"
103     }
104     -re ".*$gdb_prompt $"       { fail "args: execfile -core=[file tail $corefile]" }
105     timeout             { fail "(timeout) starting with -core" }
106 }
107 set timeout $oldtimeout
108 verbose "Timeout is now $timeout seconds" 2
109
110 close
111
112 # Now restart normally.
113
114 gdb_start
115 gdb_reinitialize_dir $srcdir/$subdir
116 gdb_load ${binfile}
117
118 # Test basic corefile recognition via core-file command.
119
120 gdb_test_multiple "core-file $corefile" "core-file command" {
121     -re ".* program is being debugged already.*y or n. $" {
122         # gdb_load may connect us to a gdbserver.
123         send_gdb "y\n"
124         exp_continue
125     }
126     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
127         pass "core-file command"
128     }
129     -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
130         pass "core-file command (with bad program name)"
131     }
132     -re ".*registers from core file: File in wrong format.* $" {
133         fail "core-file command (could not read registers from core file)"
134     }
135 }
136
137 # Test correct mapping of corefile sections by printing some variables.
138
139 gdb_test "print coremaker_data" "\\\$$decimal = 202"
140 gdb_test "print coremaker_bss" "\\\$$decimal = 10"
141 gdb_test "print coremaker_ro" "\\\$$decimal = 201"
142
143 gdb_test "print func2::coremaker_local" "\\\$$decimal = \\{0, 1, 2, 3, 4\\}"
144
145 # Test the presence and the correct values of $_exitsignal and
146 # $_exitcode variables.  The corefile is generated with a SIGABRT,
147 # which is "6" in the Linux kernel.
148
149 gdb_test "print \$_exitsignal" " = 6" \
150     "\$_exitsignal prints SIGABRT (6)"
151
152 gdb_test "print \$_exitcode" " = void" \
153     "\$_exitcode is void"
154
155 # Somehow we better test the ability to read the registers out of the core
156 # file correctly.  I don't think the other tests do this.
157
158 gdb_test "bt" "abort.*func2.*func1.*main.*" "backtrace in corefile.exp"
159 gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* \\(.*\\).*" "up in corefile.exp"
160
161 # Test ability to read mmap'd data
162
163 gdb_test "x/8bd buf1" ".*:.*0.*1.*2.*3.*4.*5.*6.*7" "accessing original mmap data in core file"
164 setup_xfail "*-*-sunos*" "*-*-aix*"
165 set test "accessing mmapped data in core file"
166 gdb_test_multiple "x/8bd buf2" "$test" {
167     -re ".*:.*0.*1.*2.*3.*4.*5.*6.*7.*$gdb_prompt $" {
168         pass "$test"
169     }
170     -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
171         fail "$test (mapping failed at runtime)"
172     }
173     -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
174         fail "$test (mapping address not found in core file)"
175     }
176 }
177
178 # test reinit_frame_cache
179
180 gdb_load ${binfile}
181 gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* \\(.*\\).*" "up in corefile.exp (reinit)"
182
183 gdb_test "core" "No core file now."
184
185
186 # Test a run (start) command will clear any loaded core file.
187
188 proc corefile_test_run {} {
189     global corefile gdb_prompt
190
191     # This test is trying to check whether the "run" command finds the
192     # default run target when already debugging a core, so it would
193     # fail on boards that set auto-connect-native-target off.  Since
194     # there's no real point in running the test but with the native
195     # target, it's easier to just skip elsewhere.
196     if {[target_info gdb_protocol] != ""} {
197         return
198     }
199
200     gdb_test "core-file $corefile" "Core was generated by .*" "run: load core again"
201     gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "run: sanity check we see the core file"
202
203     set test "run: with core"
204     if [runto_main] {
205         pass $test
206     } else {
207         fail $test
208     }
209
210     set test "run: core file is cleared"
211     gdb_test_multiple "info files" $test {
212         -re "\r\nLocal core dump file:\r\n.*\r\n$gdb_prompt $" {
213             fail $test
214         }
215         -re "\r\n$gdb_prompt $" {
216             pass $test
217         }
218     }
219
220     set test "quit with a process"
221     gdb_test_multiple "quit" $test {
222         -re "A debugging session is active.\r\n.*\r\nQuit anyway\\? \\(y or n\\) $" {
223             pass $test
224             gdb_test "n" {Not confirmed\.} "quit with processes: n"
225         }
226     }
227
228     gdb_exit
229 }
230
231 corefile_test_run
232
233 # Verify there is no question if only a core file is loaded.
234
235 gdb_start
236 gdb_test "core-file $corefile" "Core was generated by .*" "no question: load core"
237
238 set test "quit with a core file"
239 gdb_test_multiple "quit" $test {
240     -re "A debugging session is active.\r\n.*\r\nQuit anyway\\? \\(y or n\\) $" {
241         fail $test
242         gdb_test "n" {Not confirmed\.} "quit with processes: n"
243     }
244     eof {
245         pass $test
246     }
247 }
248
249 gdb_exit
250
251 # Test an attach command will clear any loaded core file.
252
253 proc corefile_test_attach {} {
254     global binfile corefile gdb_prompt
255
256     # This test is checking whether the "attach" command finds the
257     # default run target when already debugging a core, so it would
258     # fail on boards that set auto-connect-native-target off.  Since
259     # there's no real point in running the test but with the native
260     # target, it's easier to just skip elsewhere.
261     if {[target_info gdb_protocol] != ""} {
262         return
263     }
264
265     if [can_spawn_for_attach] {
266         set test "attach: spawn sleep"
267         set res [remote_spawn host "$binfile sleep"]
268         if { $res < 0 || $res == "" } {
269             fail $test
270             return
271         }
272         set pid [exp_pid -i $res]
273         # We don't care whether the program is still in the startup phase when we
274         # attach.
275
276         gdb_start
277
278         gdb_test "core-file $corefile" "Core was generated by .*" "attach: load core again"
279         gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "attach: sanity check we see the core file"
280
281         gdb_test "attach $pid" "Attaching to process $pid\r\n.*" "attach: with core"
282
283         set test "attach: core file is cleared"
284         gdb_test_multiple "info files" $test {
285             -re "\r\nLocal core dump file:\r\n.*\r\n$gdb_prompt $" {
286                 fail $test
287             }
288             -re "\r\n$gdb_prompt $" {
289                 pass $test
290             }
291         }
292
293         gdb_exit
294     }
295 }
296
297 corefile_test_attach
298
299 # Test warning-free core file load.  E.g., a Linux vDSO used to
300 # trigger this warning:
301 #     warning: Can't read pathname for load map: Input/output error.
302
303 clean_restart ${testfile}
304
305 set test "core-file warning-free"
306 gdb_test_multiple "core-file $corefile" $test {
307     -re "warning: .*\r\n.*\r\n$gdb_prompt $" {
308         fail $test
309     }
310     -re "\r\n$gdb_prompt $" {
311         pass $test
312     }
313 }