1 # Copyright 2020 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 # Tests of core file memory accesses when mmap() has been used to
17 # create a "hole" of zeroes over pre-existing memory regions. See
18 # coremaker2.c for details.
20 # are we on a target board
25 # Some of these tests will only work on GNU/Linux due to the
26 # fact that Linux core files includes a section describing
27 # memory address to file mappings. We'll use set_up_xfail for the
28 # affected tests. As other targets become supported, the condition
29 # can be changed accordingly.
32 if { ![istarget *-linux*] } {
36 standard_testfile coremaker2.c
38 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
39 untested "failed to compile"
43 set corefile [core_find $binfile {}]
44 if {$corefile == ""} {
49 gdb_reinitialize_dir $srcdir/$subdir
52 # Attempt to load the core file.
54 gdb_test_multiple "core-file $corefile" "core-file command" {
55 -re ".* program is being debugged already.*y or n. $" {
56 # gdb_load may connect us to a gdbserver.
60 -re "Core was generated by .*corefile.*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
61 pass "core-file command"
63 -re "Core was generated by .*\r\n\#0 .*\(\).*\r\n$gdb_prompt $" {
64 pass "core-file command (with bad program name)"
66 -re ".*registers from core file: File in wrong format.* $" {
67 fail "core-file command (could not read registers from core file)"
71 # Perform the "interesting" tests which check the contents of certain
77 # Check contents of beginning of buf_rw and buf_ro.
79 gdb_test {print/x buf_rw[0]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
80 gdb_test {print/x buf_ro[0]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
82 # Check for correct contents at beginning of mbuf_rw and mbuf_ro.
84 gdb_test {print/x mbuf_rw[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}
86 if { $xfail } { setup_xfail "*-*-*" }
87 gdb_test {print/x mbuf_ro[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}
89 # Check contents of mbuf_rw and mbuf_ro at the end of these regions.
91 gdb_test {print/x mbuf_rw[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}
93 if { $xfail } { setup_xfail "*-*-*" }
94 gdb_test {print/x mbuf_ro[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}
96 # Check contents of mbuf_rw and mbuf_ro, right before the hole,
97 # overlapping into the beginning of these mmap'd regions.
99 gdb_test {print/x mbuf_rw[-3]@6} {\{0x6b, 0x6b, 0x6b, 0x0, 0x0, 0x0\}}
101 if { $xfail } { setup_xfail "*-*-*" }
102 gdb_test {print/x mbuf_ro[-3]@6} {\{0xc5, 0xc5, 0xc5, 0x0, 0x0, 0x0\}}
104 # Likewise, at the end of the mbuf_rw and mbuf_ro, with overlap.
106 # If this test FAILs, it's probably a genuine bug unrelated to whether
107 # the core file includes a section describing memory address to file
108 # mappings or not. (So don't xfail it!)
109 gdb_test {print/x mbuf_rw[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0x6b, 0x6b, 0x6b\}}
111 if { $xfail } { setup_xfail "*-*-*" }
112 gdb_test {print/x mbuf_ro[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0xc5, 0xc5, 0xc5\}}
114 # Check contents of (what should be) buf_rw and buf_ro immediately after
115 # mbuf_rw and mbuf_ro holes.
117 gdb_test {print/x mbuf_rw[pagesize]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
118 gdb_test {print/x mbuf_ro[pagesize]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
120 # Check contents at ends of buf_rw and buf_rw.
122 gdb_test {print/x buf_rw[sizeof(buf_rw)-4]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
123 gdb_test {print/x buf_ro[sizeof(buf_ro)-4]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
126 # Run tests with kernel-produced core file.
128 with_test_prefix "kernel core" {
132 # Verify that "maint print core-file-backed-mappings" exists and does
133 # not crash GDB. If it produces any output at all, make sure that
134 # that output at least mentions binfile.
136 set test "maint print core-file-backed-mappings"
137 gdb_test_multiple $test "" {
138 -re ".*$binfile.*$gdb_prompt $" {
141 -re "^$test\[\r\n\]*$gdb_prompt $" {
142 pass "$test (no output)"
146 # Test again with executable renamed during loading of core file.
148 with_test_prefix "renamed binfile" {
149 # Don't load $binfile in this call to clean_restart. (BFD will
150 # complain that $binfile has disappeared after the rename if it's
154 # Rename $binfile so that it won't be found during loading of
156 set hide_binfile [standard_output_file "${testfile}.hide"]
157 remote_exec host "mv -f $binfile $hide_binfile"
159 # Load core file - check that a warning is printed.
161 if { $xfail } { setup_xfail "*-*-*" }
162 gdb_test "core-file $corefile" \
163 "warning: Can't open file.*during.* note processing.*Core was generated by .*\#0 .*\(\)" \
164 "load core file without having first loaded binfile"
166 # Restore $binfile and then load it.
167 remote_exec host "mv -f $hide_binfile $binfile"
173 # Restart and run to the abort call.
175 clean_restart $binfile
177 if ![runto_main] then {
178 fail "can't run to main"
182 gdb_breakpoint [gdb_get_line_number "abort"]
183 gdb_continue_to_breakpoint "at abort"
185 # Do not execute abort call; instead, invoke gcore command to make a
186 # gdb-produced core file.
188 set corefile [standard_output_file gcore.test]
189 set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
190 if {!$core_supported} {
194 # maint print-core-file-backed-mappings shouldn't produce any output
195 # when not debugging a core file.
197 gdb_test_no_output "maint print core-file-backed-mappings" \
198 "maint print core-file-backed-mapping with no core file"
200 clean_restart $binfile
202 set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
203 if { $core_loaded == -1 } {
204 # No use proceeding from here.
208 # Run tests using gcore-produced core file.
210 with_test_prefix "gcore core" {