Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / coredump-filter.exp
CommitLineData
88b9d363 1# Copyright 2015-2022 Free Software Foundation, Inc.
df8411da
SDJ
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
16standard_testfile
17
7578d5f7
LM
18# This test is Linux-only.
19if ![istarget *-*-linux*] then {
20 untested "coredump-filter.exp"
21 return -1
22}
23
df8411da 24if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
df8411da
SDJ
25 return -1
26}
27
28if { ![runto_main] } {
29 untested "could not run to main"
30 return -1
31}
32
33gdb_breakpoint [gdb_get_line_number "break-here"]
34gdb_continue_to_breakpoint "break-here" ".* break-here .*"
35
3e1a70a0 36proc do_save_core { filter_flag core dump_excluded } {
422349a3 37 verbose -log "writing $filter_flag to /proc/<inferior pid>/coredump_filter"
df8411da 38
422349a3 39 gdb_test "p set_coredump_filter ($filter_flag)" " = 0"
df8411da 40
3e1a70a0
SL
41 # Enable dumping of excluded mappings (i.e. VM_DONTDUMP).
42 if { $dump_excluded == 1 } {
43 gdb_test_no_output "set dump-excluded-mappings on"
44 }
45
df8411da
SDJ
46 # Generate a corefile.
47 gdb_gcore_cmd "$core" "save corefile"
3e1a70a0
SL
48
49 # Restore original status.
50 if { $dump_excluded == 1 } {
51 gdb_test_no_output "set dump-excluded-mappings off"
52 }
df8411da
SDJ
53}
54
3e1a70a0 55proc do_load_and_test_core { core var working_var working_value dump_excluded } {
8cd8f2f8 56 global hex decimal coredump_var_addr
df8411da
SDJ
57
58 set core_loaded [gdb_core_cmd "$core" "load core"]
59 if { $core_loaded == -1 } {
60 fail "loading $core"
61 return
62 }
63
64 # Access the memory the addresses point to.
3e1a70a0
SL
65 if { $dump_excluded == 0 } {
66 gdb_test "print/x *(char *) $coredump_var_addr($var)" "\(\\\$$decimal = <error: \)?Cannot access memory at address $hex\(>\)?" \
67 "printing $var when core is loaded (should not work)"
68 gdb_test "print/x *(char *) $coredump_var_addr($working_var)" " = $working_value.*" \
69 "print/x *$working_var ( = $working_value)"
70 } else {
71 # Check if VM_DONTDUMP mappings are present in the core file.
72 gdb_test "print/x *(char *) $coredump_var_addr($var)" " = $working_value.*" \
73 "print/x *$var ( = $working_value)"
74 }
df8411da
SDJ
75}
76
77# We do not do file-backed mappings in the test program, but it is
78# important to test this anyway. One way of performing the test is to
79# load GDB with a corefile but without a binary, and then ask for the
80# disassemble of a function (i.e., the binary's .text section). GDB
81# should fail in this case. However, it must succeed if the binary is
82# provided along with the corefile. This is what we test here.
9c5ec5c2
KB
83#
84# A further complication is that Linux NT_FILE notes are now read from
85# the corefile. This note allows GDB to find the binary for file
86# backed mappings even though the binary wasn't loaded by GDB in the
87# conventional manner. In order to see the expected failure for this
88# case, we rename the binary in order to perform this test.
df8411da
SDJ
89
90proc test_disasm { core address should_fail } {
9c5ec5c2 91 global testfile hex binfile
df8411da
SDJ
92
93 # Restart GDB without loading the binary.
94 with_test_prefix "no binary" {
95 gdb_exit
96 gdb_start
97
9c5ec5c2
KB
98 set hide_binfile [standard_output_file "${testfile}.hide"]
99 if { $should_fail == 1 } {
100 remote_exec host "mv -f $binfile $hide_binfile"
101 }
102
df8411da
SDJ
103 set core_loaded [gdb_core_cmd "$core" "load core"]
104 if { $core_loaded == -1 } {
105 fail "loading $core"
106 return
107 }
108
109 if { $should_fail == 1 } {
9c5ec5c2 110 remote_exec host "mv -f $hide_binfile $binfile"
df8411da
SDJ
111 gdb_test "x/i \$pc" "=> $hex:\tCannot access memory at address $hex" \
112 "disassemble function with corefile and without a binary"
113 } else {
114 gdb_test "x/i \$pc" "=> $hex:\t\[^C\].*" \
115 "disassemble function with corefile and without a binary"
116 }
117 }
118
119 with_test_prefix "with binary" {
120 clean_restart $testfile
121
122 set core_loaded [gdb_core_cmd "$core" "load core"]
123 if { $core_loaded == -1 } {
124 fail "loading $core"
125 return
126 }
127
128 gdb_test "disassemble $address" "Dump of assembler code for function.*" \
129 "disassemble function with corefile and with a binary"
130 }
131}
132
133set non_private_anon_core [standard_output_file non-private-anon.gcore]
134set non_shared_anon_core [standard_output_file non-shared-anon.gcore]
135# A corefile without {private,shared} {anonymous,file-backed} pages
136set non_private_shared_anon_file_core [standard_output_file non-private-shared-anon-file.gcore]
137set dont_dump_core [standard_output_file dont-dump.gcore]
3e1a70a0 138set dump_excluded_core [standard_output_file dump-excluded.gcore]
df8411da
SDJ
139
140# We will generate a few corefiles.
141#
142# This list is composed by sub-lists, and their elements are (in
143# order):
144#
145# - name of the test
146# - hexadecimal value to be put in the /proc/PID/coredump_filter file
147# - name of the variable that contains the name of the corefile to be
148# generated (including the initial $).
149# - name of the variable in the C source code that points to the
150# memory mapping that will NOT be present in the corefile.
151# - name of a variable in the C source code that points to a memory
152# mapping that WILL be present in the corefile
153# - corresponding value expected for the above variable
3e1a70a0
SL
154# - whether to include mappings marked as VM_DONTDUMP in the
155# corefile (1) or not (0).
df8411da
SDJ
156#
157# This list refers to the corefiles generated by MAP_ANONYMOUS in the
158# test program.
159
160set all_anon_corefiles { { "non-Private-Anonymous" "0x7e" \
161 $non_private_anon_core \
162 "private_anon" \
3e1a70a0 163 "shared_anon" "0x22" "0" }
df8411da
SDJ
164 { "non-Shared-Anonymous" "0x7d" \
165 $non_shared_anon_core "shared_anon" \
3e1a70a0 166 "private_anon" "0x11" "0" }
df8411da
SDJ
167 { "DoNotDump" "0x33" \
168 $dont_dump_core "dont_dump" \
3e1a70a0
SL
169 "shared_anon" "0x22" "0" }
170 { "DoNotDump-DumpExcluded" "0x33" \
171 $dump_excluded_core "dont_dump" \
172 "shared_anon" "0x55" "1" } }
df8411da
SDJ
173
174# If corefile loading is not supported, we do not even try to run the
175# tests.
176set core_supported [gdb_gcore_cmd "$non_private_anon_core" "save a corefile"]
177if { !$core_supported } {
178 untested "corefile generation is not supported"
179 return -1
180}
181
df8411da 182gdb_test_multiple "info inferiors" "getting inferior pid" {
422349a3 183 -re "process $decimal.*\r\n$gdb_prompt $" {
df8411da 184 }
7578d5f7
LM
185 -re "Remote target.*$gdb_prompt $" {
186 # If the target does not provide PID information (like usermode QEMU),
187 # just bail out as the rest of the test may rely on it, giving spurious
188 # failures.
189 return -1
190 }
df8411da
SDJ
191}
192
193# Get the main function's address.
194set main_addr ""
195gdb_test_multiple "print/x &main" "getting main's address" {
196 -re "$decimal = \($hex\)\r\n$gdb_prompt $" {
197 set main_addr $expect_out(1,string)
198 }
199}
200
201# Obtain the address of each variable that will be checked on each
202# case.
f7797074 203unset -nocomplain coredump_var_addr
df8411da
SDJ
204foreach item $all_anon_corefiles {
205 foreach name [list [lindex $item 3] [lindex $item 4]] {
206 set test "print/x $name"
207 gdb_test_multiple $test $test {
208 -re " = \($hex\)\r\n$gdb_prompt $" {
8cd8f2f8 209 set coredump_var_addr($name) $expect_out(1,string)
df8411da
SDJ
210 }
211 }
212 }
213}
214
215# Generate corefiles for the "anon" case.
216foreach item $all_anon_corefiles {
217 with_test_prefix "saving corefile for [lindex $item 0]" {
3e1a70a0 218 do_save_core [lindex $item 1] [subst [lindex $item 2]] [lindex $item 6]
df8411da
SDJ
219 }
220}
221
222with_test_prefix "saving corefile for non-Private-Shared-Anon-File" {
3e1a70a0 223 do_save_core "0x60" $non_private_shared_anon_file_core "0"
df8411da
SDJ
224}
225
226clean_restart $testfile
227
228foreach item $all_anon_corefiles {
229 with_test_prefix "loading and testing corefile for [lindex $item 0]" {
230 do_load_and_test_core [subst [lindex $item 2]] [lindex $item 3] \
3e1a70a0 231 [lindex $item 4] [lindex $item 5] [lindex $item 6]
df8411da
SDJ
232 }
233
234 with_test_prefix "disassembling function main for [lindex $item 0]" {
235 test_disasm [subst [lindex $item 2]] $main_addr 0
236 }
237}
238
239with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File" {
9c5ec5c2
KB
240 test_disasm $non_private_shared_anon_file_core $main_addr 0
241}
242
243with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File with renamed binary" {
df8411da
SDJ
244 test_disasm $non_private_shared_anon_file_core $main_addr 1
245}
This page took 0.865119 seconds and 4 git commands to generate.