summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/corefile2.exp
blob: 51732d61bf522c525769fd3eb3c7045b94a1adf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright 2020-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Tests of core file memory accesses when mmap() has been used to
# create a "hole" of zeroes over pre-existing memory regions.  See
# coremaker2.c for details.

# are we on a target board
require isnative

# Some of these tests will only work on GNU/Linux due to the
# fact that Linux core files includes a section describing
# memory address to file mappings.  We'll use set_up_xfail for the
# affected tests.  As other targets become supported, the condition
# can be changed accordingly.

set xfail 0
if { ![istarget *-linux*] } {
    set xfail 1
}

standard_testfile coremaker2.c

if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
    untested "failed to compile"
    return -1
}

set corefile [core_find $binfile {}]
if {$corefile == ""} {
    return 0
}

gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}

# Attempt to load the core file.

gdb_test_multiple "core-file $corefile" "core-file command" {
    -re ".* program is being debugged already.*y or n. $" {
	# gdb_load may connect us to a gdbserver.
	send_gdb "y\n"
	exp_continue
    }
    -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
	pass "core-file command"
    }
    -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
	pass "core-file command (with bad program name)"
    }
    -re ".*registers from core file: File in wrong format.* $" {
	fail "core-file command (could not read registers from core file)"
    }
}

# Perform the "interesting" tests which check the contents of certain
# memory regions.

proc do_tests { } {
    global xfail

    # Check contents of beginning of buf_rw and buf_ro.

    gdb_test {print/x buf_rw[0]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
    gdb_test {print/x buf_ro[0]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}

    # Check for correct contents at beginning of mbuf_rw and mbuf_ro.

    gdb_test {print/x mbuf_rw[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}

    if { $xfail } { setup_xfail "*-*-*" }
    gdb_test {print/x mbuf_ro[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}

    # Check contents of mbuf_rw and mbuf_ro at the end of these regions.

    gdb_test {print/x mbuf_rw[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}

    if { $xfail } { setup_xfail "*-*-*" }
    gdb_test {print/x mbuf_ro[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}

    # Check contents of mbuf_rw and mbuf_ro, right before the hole,
    # overlapping into the beginning of these mmap'd regions.

    gdb_test {print/x mbuf_rw[-3]@6} {\{0x6b, 0x6b, 0x6b, 0x0, 0x0, 0x0\}}

    if { $xfail } { setup_xfail "*-*-*" }
    gdb_test {print/x mbuf_ro[-3]@6} {\{0xc5, 0xc5, 0xc5, 0x0, 0x0, 0x0\}}

    # Likewise, at the end of the mbuf_rw and mbuf_ro, with overlap.

    # If this test FAILs, it's probably a genuine bug unrelated to whether
    # the core file includes a section describing memory address to file
    # mappings or not.  (So don't xfail it!)
    gdb_test {print/x mbuf_rw[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0x6b, 0x6b, 0x6b\}}

    if { $xfail } { setup_xfail "*-*-*" }
    gdb_test {print/x mbuf_ro[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0xc5, 0xc5, 0xc5\}}

    # Check contents of (what should be) buf_rw and buf_ro immediately after
    # mbuf_rw and mbuf_ro holes.

    gdb_test {print/x mbuf_rw[pagesize]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
    gdb_test {print/x mbuf_ro[pagesize]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}

    # Check contents at ends of buf_rw and buf_rw.

    gdb_test {print/x buf_rw[sizeof(buf_rw)-4]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
    gdb_test {print/x buf_ro[sizeof(buf_ro)-4]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
}

# Run tests with kernel-produced core file.

with_test_prefix "kernel core" {
    do_tests
}

# Verify that "maint print core-file-backed-mappings" exists and does
# not crash GDB.  If it produces any output at all, make sure that
# that output at least mentions binfile.

set test "maint print core-file-backed-mappings"
gdb_test_multiple $test "" {
    -re ".*$binfile.*$gdb_prompt $" {
	pass $test
    }
    -re "^$test\[\r\n\]*$gdb_prompt $" {
	pass "$test (no output)"
    }
}

# Test again with executable renamed during loading of core file.

with_test_prefix "renamed binfile" {
    # Don't load $binfile in this call to clean_restart.  (BFD will
    # complain that $binfile has disappeared after the rename if it's
    # loaded first.)
    clean_restart

    # Rename $binfile so that it won't be found during loading of
    # the core file.
    set hide_binfile [standard_output_file "${testfile}.hide"]
    remote_exec host "mv -f $binfile $hide_binfile"

    # Load core file - check that a warning is printed.
    global xfail
    if { $xfail } { setup_xfail "*-*-*" }
    gdb_test "core-file $corefile" \
	"warning: Can't open file.*during.* note processing.*Core was generated by .*\#0  .*\(\)" \
	"load core file without having first loaded binfile"

    # Restore $binfile and then load it.
    remote_exec host "mv -f $hide_binfile $binfile"
    gdb_load ${binfile}

    do_tests
}

# Restart and run to the abort call.

clean_restart $binfile

if {![runto_main]} {
    return
}

gdb_breakpoint [gdb_get_line_number "abort"]
gdb_continue_to_breakpoint "at abort"

# Do not execute abort call; instead, invoke gcore command to make a
# gdb-produced core file.

set corefile [standard_output_file gcore.test]
set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
if {!$core_supported} {
  return
}

# maint print-core-file-backed-mappings shouldn't produce any output
# when not debugging a core file.

gdb_test_no_output "maint print core-file-backed-mappings" \
    "maint print core-file-backed-mapping with no core file"

clean_restart $binfile

set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
if { $core_loaded == -1 } {
    # No use proceeding from here.
    return
}

# Run tests using gcore-produced core file.

with_test_prefix "gcore core" {
    do_tests
}