739ff09353b170ea42de08e0f52bcb56df0697a0
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / coredump-filter.exp
1 # Copyright 2015-2021 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 standard_testfile
17
18 # This test is Linux-only.
19 if ![istarget *-*-linux*] then {
20 untested "coredump-filter.exp"
21 return -1
22 }
23
24 if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
25 return -1
26 }
27
28 if { ![runto_main] } {
29 untested "could not run to main"
30 return -1
31 }
32
33 gdb_breakpoint [gdb_get_line_number "break-here"]
34 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
35
36 proc do_save_core { filter_flag core dump_excluded } {
37 verbose -log "writing $filter_flag to /proc/<inferior pid>/coredump_filter"
38
39 gdb_test "p set_coredump_filter ($filter_flag)" " = 0"
40
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
46 # Generate a corefile.
47 gdb_gcore_cmd "$core" "save corefile"
48
49 # Restore original status.
50 if { $dump_excluded == 1 } {
51 gdb_test_no_output "set dump-excluded-mappings off"
52 }
53 }
54
55 proc do_load_and_test_core { core var working_var working_value dump_excluded } {
56 global hex decimal coredump_var_addr
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.
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 }
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.
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.
89
90 proc test_disasm { core address should_fail } {
91 global testfile hex binfile
92
93 # Restart GDB without loading the binary.
94 with_test_prefix "no binary" {
95 gdb_exit
96 gdb_start
97
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
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 } {
110 remote_exec host "mv -f $hide_binfile $binfile"
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
133 set non_private_anon_core [standard_output_file non-private-anon.gcore]
134 set non_shared_anon_core [standard_output_file non-shared-anon.gcore]
135 # A corefile without {private,shared} {anonymous,file-backed} pages
136 set non_private_shared_anon_file_core [standard_output_file non-private-shared-anon-file.gcore]
137 set dont_dump_core [standard_output_file dont-dump.gcore]
138 set dump_excluded_core [standard_output_file dump-excluded.gcore]
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
154 # - whether to include mappings marked as VM_DONTDUMP in the
155 # corefile (1) or not (0).
156 #
157 # This list refers to the corefiles generated by MAP_ANONYMOUS in the
158 # test program.
159
160 set all_anon_corefiles { { "non-Private-Anonymous" "0x7e" \
161 $non_private_anon_core \
162 "private_anon" \
163 "shared_anon" "0x22" "0" }
164 { "non-Shared-Anonymous" "0x7d" \
165 $non_shared_anon_core "shared_anon" \
166 "private_anon" "0x11" "0" }
167 { "DoNotDump" "0x33" \
168 $dont_dump_core "dont_dump" \
169 "shared_anon" "0x22" "0" }
170 { "DoNotDump-DumpExcluded" "0x33" \
171 $dump_excluded_core "dont_dump" \
172 "shared_anon" "0x55" "1" } }
173
174 # If corefile loading is not supported, we do not even try to run the
175 # tests.
176 set core_supported [gdb_gcore_cmd "$non_private_anon_core" "save a corefile"]
177 if { !$core_supported } {
178 untested "corefile generation is not supported"
179 return -1
180 }
181
182 gdb_test_multiple "info inferiors" "getting inferior pid" {
183 -re "process $decimal.*\r\n$gdb_prompt $" {
184 }
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 }
191 }
192
193 # Get the main function's address.
194 set main_addr ""
195 gdb_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.
203 unset -nocomplain coredump_var_addr
204 foreach 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 $" {
209 set coredump_var_addr($name) $expect_out(1,string)
210 }
211 }
212 }
213 }
214
215 # Generate corefiles for the "anon" case.
216 foreach item $all_anon_corefiles {
217 with_test_prefix "saving corefile for [lindex $item 0]" {
218 do_save_core [lindex $item 1] [subst [lindex $item 2]] [lindex $item 6]
219 }
220 }
221
222 with_test_prefix "saving corefile for non-Private-Shared-Anon-File" {
223 do_save_core "0x60" $non_private_shared_anon_file_core "0"
224 }
225
226 clean_restart $testfile
227
228 foreach 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] \
231 [lindex $item 4] [lindex $item 5] [lindex $item 6]
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
239 with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File" {
240 test_disasm $non_private_shared_anon_file_core $main_addr 0
241 }
242
243 with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File with renamed binary" {
244 test_disasm $non_private_shared_anon_file_core $main_addr 1
245 }
This page took 0.034273 seconds and 3 git commands to generate.