f15e1e9e81dd36262323abbaefc687ea04fb5544
[platform/upstream/binutils.git] / gdb / testsuite / gdb.base / corefile.exp
1 # Copyright 1992-2022 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]} {
21     return
22 }
23
24 standard_testfile coremaker.c
25
26 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
27     untested "failed to compile"
28     return -1
29 }
30
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 == ""} {
36     return 0
37 }
38
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.
45 #
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.
50
51 gdb_exit
52 if {$verbose>1} {
53     send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS -core=$corefile\n"
54 }
55
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"
60 expect {
61     -re "Couldn't find .* registers in core file.*$gdb_prompt $" {
62         fail "args: -core=[file tail $corefile] (couldn't find regs)"
63     }
64     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
65         pass "args: -core=[file tail $corefile]"
66     }
67     -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
68         pass "args: -core=[file tail $corefile] (with bad program name)"
69     }
70     -re ".*registers from core file: File in wrong format.* $" {
71         fail "args: -core=[file tail $corefile] (could not read registers from core file)"
72     }
73     -re ".*$gdb_prompt $"       { fail "args: -core=[file tail $corefile]" }
74     timeout             { fail "(timeout) starting with -core" }
75 }
76
77
78 #
79 # Test that startup with both an executable file and -core argument.
80 # See previous comments above, they are still applicable.
81 #
82
83 close
84
85 if {$verbose>1} {
86     send_user "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile\n"
87 }
88
89
90 eval "spawn $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $binfile -core=$corefile"
91 expect {
92     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
93         pass "args: execfile -core=[file tail $corefile]"
94     }
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)"
97     }
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)"
100     }
101     -re ".*$gdb_prompt $"       { fail "args: execfile -core=[file tail $corefile]" }
102     timeout             { fail "(timeout) starting with -core" }
103 }
104 set timeout $oldtimeout
105 verbose "Timeout is now $timeout seconds" 2
106
107 close
108
109 # Now restart normally.
110
111 gdb_start
112 gdb_reinitialize_dir $srcdir/$subdir
113 gdb_load ${binfile}
114
115 # Test basic corefile recognition via core-file command.
116
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.
120         send_gdb "y\n"
121         exp_continue
122     }
123     -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
124         pass "core-file command"
125     }
126     -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
127         pass "core-file command (with bad program name)"
128     }
129     -re ".*registers from core file: File in wrong format.* $" {
130         fail "core-file command (could not read registers from core file)"
131     }
132 }
133
134 # Test correct mapping of corefile sections by printing some variables.
135
136 gdb_test "print coremaker_data" "\\\$$decimal = 202"
137 gdb_test "print coremaker_bss" "\\\$$decimal = 10"
138 gdb_test "print coremaker_ro" "\\\$$decimal = 201"
139
140 gdb_test "print func2::coremaker_local" "\\\$$decimal = \\{0, 1, 2, 3, 4\\}"
141
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.
145
146 gdb_test "print \$_exitsignal" " = 6" \
147     "\$_exitsignal prints SIGABRT (6)"
148
149 gdb_test "print \$_exitcode" " = void" \
150     "\$_exitcode is void"
151
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.
154
155 gdb_test "bt" "abort.*func2.*func1.*main.*" "backtrace"
156 gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* \\(.*\\).*" "up"
157
158 # Test ability to read mmap'd data
159
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 $" {
165         pass "$test"
166     }
167     -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
168         fail "$test (mapping failed at runtime)"
169     }
170     -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
171         fail "$test (mapping address not found in core file)"
172     }
173 }
174
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 $" {
178         pass "$test"
179     }
180     -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
181         fail "$test (mapping failed at runtime)"
182     }
183     -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
184         fail "$test (mapping address not found in core file)"
185     }
186 }
187
188 # Test ability to read anonymous and, more importantly, unwritten-to
189 # mmap'd data.
190
191 if { ![istarget *-linux*] } {
192     setup_xfail "*-*-*"
193 }
194 gdb_test "x/wx buf3" "$hex:\[ \t\]+0x00000000" \
195          "accessing anonymous, unwritten-to mmap data"
196
197 # test reinit_frame_cache
198
199 gdb_load ${binfile}
200 gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* \\(.*\\).*" "up (reinit)"
201
202 gdb_test "core" "No core file now."
203
204 # Test that we can unload the core with the "detach" command.
205
206 proc_with_prefix corefile_detach {} {
207     clean_restart $::binfile
208
209     gdb_test "core-file $::corefile" "Core was generated by .*" "load core"
210     gdb_test "detach" "No core file now\\." "detach core"
211 }
212
213 corefile_detach
214
215 # Test a run (start) command will clear any loaded core file.
216
217 proc corefile_test_run {} {
218     global corefile gdb_prompt
219
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] != ""} {
226         return
227     }
228
229     clean_restart $::binfile
230
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"
233
234     set test "run: with core"
235     if [runto_main] {
236         pass $test
237     } else {
238         fail $test
239     }
240
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 $" {
244             fail $test
245         }
246         -re "\r\n$gdb_prompt $" {
247             pass $test
248         }
249     }
250
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\\) $" {
254             pass $test
255             gdb_test "n" {Not confirmed\.} "quit with processes: n"
256         }
257     }
258
259     gdb_exit
260 }
261
262 corefile_test_run
263
264 # Verify there is no question if only a core file is loaded.
265
266 gdb_start
267 gdb_test "core-file $corefile" "Core was generated by .*" "no question: load core"
268
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\\) $" {
272         fail $test
273         gdb_test "n" {Not confirmed\.} "quit with processes: n"
274     }
275     eof {
276         pass $test
277     }
278 }
279
280 gdb_exit
281
282 # Test an attach command will clear any loaded core file.
283
284 proc corefile_test_attach {} {
285     global binfile corefile gdb_prompt
286
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] != ""} {
293         return
294     }
295
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 == "" } {
300             fail $test
301             return
302         }
303         set pid [exp_pid -i $res]
304         # We don't care whether the program is still in the startup phase when we
305         # attach.
306
307         gdb_start
308
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"
311
312         gdb_test "attach $pid" "Attaching to process $pid\r\n.*" "attach: with core"
313
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 $" {
317                 fail $test
318             }
319             -re "\r\n$gdb_prompt $" {
320                 pass $test
321             }
322         }
323
324         gdb_exit
325     }
326 }
327
328 corefile_test_attach
329
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.
333 #
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.
337
338 clean_restart ${testfile}
339
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 $" {
343         xfail $test
344     }
345     -re "warning: .*\r\n.*\r\n$gdb_prompt $" {
346         fail $test
347     }
348     -re "\r\n$gdb_prompt $" {
349         pass $test
350     }
351 }