1 # Copyright 1992-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file was written by Fred Fish. (fnf@cygnus.com)
19 # are we on a target board
24 standard_testfile coremaker.c
26 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
27 untested "failed to compile"
31 # Do not delete coremap.data when calling core_find. This file is
32 # required for GDB to find mmap'd data in the "accessing read-only
33 # mmapped data in core file" test.
34 set corefile [core_find $binfile {}]
35 if {$corefile == ""} {
39 # Test that we can simply startup with a "-core=$corefile" command line arg
40 # and recognize that the core file is a valid, usable core file.
41 # To do this, we must shutdown the currently running gdb and restart
42 # with the -core args. We can't use gdb_start because it looks for
43 # the first gdb prompt, and the message we are looking for occurs
44 # before the first prompt.
46 # Another problem is that on some systems (solaris for example), there
47 # is apparently a limit on the length of a fully specified path to
48 # the corefile executable, at about 80 chars. For this case, consider
49 # it a pass, but note that the program name is bad.
53 send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS -core=$corefile\n"
56 set oldtimeout $timeout
57 set timeout [expr "$timeout + 60"]
58 verbose "Timeout is now $timeout seconds" 2
59 eval "spawn $GDB $INTERNAL_GDBFLAGS $GDBFLAGS -core=$corefile"
61 -re "Couldn't find .* registers in core file.*$gdb_prompt $" {
62 fail "args: -core=[file tail $corefile] (couldn't find regs)"
64 -re "Core was generated by .*corefile.*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
65 pass "args: -core=[file tail $corefile]"
67 -re "Core was generated by .*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
68 pass "args: -core=[file tail $corefile] (with bad program name)"
70 -re ".*registers from core file: File in wrong format.* $" {
71 fail "args: -core=[file tail $corefile] (could not read registers from core file)"
73 -re ".*$gdb_prompt $" { fail "args: -core=[file tail $corefile]" }
74 timeout { fail "(timeout) starting with -core" }
79 # Test that startup with both an executable file and -core argument.
80 # See previous comments above, they are still applicable.
86 send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile\n"
90 eval "spawn $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile"
92 -re "Core was generated by .*corefile.*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
93 pass "args: execfile -core=[file tail $corefile]"
95 -re "Core was generated by .*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
96 pass "args: execfile -core=[file tail $corefile] (with bad program name)"
98 -re ".*registers from core file: File in wrong format.* $" {
99 fail "args: execfile -core=[file tail $corefile] (could not read registers from core file)"
101 -re ".*$gdb_prompt $" { fail "args: execfile -core=[file tail $corefile]" }
102 timeout { fail "(timeout) starting with -core" }
104 set timeout $oldtimeout
105 verbose "Timeout is now $timeout seconds" 2
109 # Now restart normally.
112 gdb_reinitialize_dir $srcdir/$subdir
115 # Test basic corefile recognition via core-file command.
117 gdb_test_multiple "core-file $corefile" "core-file command" {
118 -re ".* program is being debugged already.*y or n. $" {
119 # gdb_load may connect us to a gdbserver.
123 -re "Core was generated by .*corefile.*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
124 pass "core-file command"
126 -re "Core was generated by .*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
127 pass "core-file command (with bad program name)"
129 -re ".*registers from core file: File in wrong format.* $" {
130 fail "core-file command (could not read registers from core file)"
134 # Test correct mapping of corefile sections by printing some variables.
136 gdb_test "print coremaker_data" "\\\$$decimal = 202"
137 gdb_test "print coremaker_bss" "\\\$$decimal = 10"
138 gdb_test "print coremaker_ro" "\\\$$decimal = 201"
140 gdb_test "print func2::coremaker_local" "\\\$$decimal = \\{0, 1, 2, 3, 4\\}"
142 # Test the presence and the correct values of $_exitsignal and
143 # $_exitcode variables. The corefile is generated with a SIGABRT,
144 # which is "6" in the Linux kernel.
146 gdb_test "print \$_exitsignal" " = 6" \
147 "\$_exitsignal prints SIGABRT (6)"
149 gdb_test "print \$_exitcode" " = void" \
150 "\$_exitcode is void"
152 # Somehow we better test the ability to read the registers out of the core
153 # file correctly. I don't think the other tests do this.
155 gdb_test "bt" "abort.*func2.*func1.*main.*" "backtrace"
156 gdb_test "up" "#\[0-9\]* *(\[0-9xa-fH'\]* in)? .* \\(.*\\).*" "up"
158 # Test ability to read mmap'd data
160 gdb_test "x/8bd buf1" ".*:.*0.*1.*2.*3.*4.*5.*6.*7" "accessing original mmap data in core file"
161 setup_xfail "*-*-sunos*" "*-*-aix*"
162 set test "accessing mmapped data in core file"
163 gdb_test_multiple "x/8bd buf2" "$test" {
164 -re ".*:.*0.*1.*2.*3.*4.*5.*6.*7.*$gdb_prompt $" {
167 -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
168 fail "$test (mapping failed at runtime)"
170 -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
171 fail "$test (mapping address not found in core file)"
175 set test "accessing read-only mmapped data in core file"
176 gdb_test_multiple "x/8bd buf2ro" "$test" {
177 -re ".*:.*0.*1.*2.*3.*4.*5.*6.*7.*$gdb_prompt $" {
180 -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
181 fail "$test (mapping failed at runtime)"
183 -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
184 fail "$test (mapping address not found in core file)"
188 # Test ability to read anonymous and, more importantly, unwritten-to
191 if { ![istarget *-linux*] } {
194 gdb_test "x/wx buf3" "$hex:\[ \t\]+0x00000000" \
195 "accessing anonymous, unwritten-to mmap data"
197 # test reinit_frame_cache
200 gdb_test "up" "#\[0-9\]* *(\[0-9xa-fH'\]* in)? .* \\(.*\\).*" "up (reinit)"
202 gdb_test "core" "No core file now."
204 # Test that we can unload the core with the "detach" command.
206 proc_with_prefix corefile_detach {} {
207 clean_restart $::binfile
209 gdb_test "core-file $::corefile" "Core was generated by .*" "load core"
210 gdb_test "detach" "No core file now\\." "detach core"
215 # Test a run (start) command will clear any loaded core file.
217 proc corefile_test_run {} {
218 global corefile gdb_prompt
220 # This test is trying to check whether the "run" command finds the
221 # default run target when already debugging a core, so it would
222 # fail on boards that set auto-connect-native-target off. Since
223 # there's no real point in running the test but with the native
224 # target, it's easier to just skip elsewhere.
225 if {[target_info gdb_protocol] != ""} {
229 clean_restart $::binfile
231 gdb_test "core-file $corefile" "Core was generated by .*" "run: load core again"
232 gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "run: sanity check we see the core file"
234 set test "run: with core"
241 set test "run: core file is cleared"
242 gdb_test_multiple "info files" $test {
243 -re "\r\nLocal core dump file:\r\n.*\r\n$gdb_prompt $" {
246 -re "\r\n$gdb_prompt $" {
251 set test "quit with a process"
252 gdb_test_multiple "quit" $test {
253 -re "A debugging session is active.\r\n.*\r\nQuit anyway\\? \\(y or n\\) $" {
255 gdb_test "n" {Not confirmed\.} "quit with processes: n"
264 # Verify there is no question if only a core file is loaded.
267 gdb_test "core-file $corefile" "Core was generated by .*" "no question: load core"
269 set test "quit with a core file"
270 gdb_test_multiple "quit" $test {
271 -re "A debugging session is active.\r\n.*\r\nQuit anyway\\? \\(y or n\\) $" {
273 gdb_test "n" {Not confirmed\.} "quit with processes: n"
282 # Test an attach command will clear any loaded core file.
284 proc corefile_test_attach {} {
285 global binfile corefile gdb_prompt
287 # This test is checking whether the "attach" command finds the
288 # default run target when already debugging a core, so it would
289 # fail on boards that set auto-connect-native-target off. Since
290 # there's no real point in running the test but with the native
291 # target, it's easier to just skip elsewhere.
292 if {[target_info gdb_protocol] != ""} {
296 if [can_spawn_for_attach] {
297 set test "attach: spawn sleep"
298 set res [remote_spawn host "$binfile sleep"]
299 if { $res < 0 || $res == "" } {
303 set pid [exp_pid -i $res]
304 # We don't care whether the program is still in the startup phase when we
309 gdb_test "core-file $corefile" "Core was generated by .*" "attach: load core again"
310 gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "attach: sanity check we see the core file"
312 gdb_test "attach $pid" "Attaching to process $pid\r\n.*" "attach: with core"
314 set test "attach: core file is cleared"
315 gdb_test_multiple "info files" $test {
316 -re "\r\nLocal core dump file:\r\n.*\r\n$gdb_prompt $" {
319 -re "\r\n$gdb_prompt $" {
330 # Test warning-free core file load. E.g., a Linux vDSO used to
331 # trigger this warning:
332 # warning: Can't read pathname for load map: Input/output error.
334 # When testing in a docker container using the AUFS storage driver,
335 # the kernel places host paths in the core file's NT_FILE note. XFAIL
336 # this case since these paths make no sense in the container.
338 clean_restart ${testfile}
340 set test "core-file warning-free"
341 gdb_test_multiple "core-file $corefile" $test {
342 -re "warning: Can\'t open file.*\/docker\/aufs\/.*\r\n$gdb_prompt $" {
345 -re "warning: .*\r\n.*\r\n$gdb_prompt $" {
348 -re "\r\n$gdb_prompt $" {