Update copyright year range in all GDB files.
[external/binutils.git] / gdb / testsuite / gdb.base / coredump-filter.exp
1 # Copyright 2015-2019 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 proc test_disasm { core address should_fail } {
85     global testfile hex
86
87     # Restart GDB without loading the binary.
88     with_test_prefix "no binary" {
89         gdb_exit
90         gdb_start
91
92         set core_loaded [gdb_core_cmd "$core" "load core"]
93         if { $core_loaded == -1 } {
94             fail "loading $core"
95             return
96         }
97
98         if { $should_fail == 1 } {
99             gdb_test "x/i \$pc" "=> $hex:\tCannot access memory at address $hex" \
100                 "disassemble function with corefile and without a binary"
101         } else {
102             gdb_test "x/i \$pc" "=> $hex:\t\[^C\].*" \
103                 "disassemble function with corefile and without a binary"
104         }
105     }
106
107     with_test_prefix "with binary" {
108         clean_restart $testfile
109
110         set core_loaded [gdb_core_cmd "$core" "load core"]
111         if { $core_loaded == -1 } {
112             fail "loading $core"
113             return
114         }
115
116         gdb_test "disassemble $address" "Dump of assembler code for function.*" \
117             "disassemble function with corefile and with a binary"
118     }
119 }
120
121 set non_private_anon_core [standard_output_file non-private-anon.gcore]
122 set non_shared_anon_core [standard_output_file non-shared-anon.gcore]
123 # A corefile without {private,shared} {anonymous,file-backed} pages
124 set non_private_shared_anon_file_core [standard_output_file non-private-shared-anon-file.gcore]
125 set dont_dump_core [standard_output_file dont-dump.gcore]
126 set dump_excluded_core [standard_output_file dump-excluded.gcore]
127
128 # We will generate a few corefiles.
129 #
130 # This list is composed by sub-lists, and their elements are (in
131 # order):
132 #
133 # - name of the test
134 # - hexadecimal value to be put in the /proc/PID/coredump_filter file
135 # - name of the variable that contains the name of the corefile to be
136 #   generated (including the initial $).
137 # - name of the variable in the C source code that points to the
138 #   memory mapping that will NOT be present in the corefile.
139 # - name of a variable in the C source code that points to a memory
140 #   mapping that WILL be present in the corefile
141 # - corresponding value expected for the above variable
142 # - whether to include mappings marked as VM_DONTDUMP in the
143 #   corefile (1) or not (0).
144 #
145 # This list refers to the corefiles generated by MAP_ANONYMOUS in the
146 # test program.
147
148 set all_anon_corefiles { { "non-Private-Anonymous" "0x7e" \
149                                $non_private_anon_core \
150                                "private_anon" \
151                                "shared_anon" "0x22" "0" }
152     { "non-Shared-Anonymous" "0x7d" \
153           $non_shared_anon_core "shared_anon" \
154           "private_anon" "0x11" "0" }
155     { "DoNotDump" "0x33" \
156           $dont_dump_core "dont_dump" \
157           "shared_anon" "0x22" "0" }
158     { "DoNotDump-DumpExcluded" "0x33" \
159           $dump_excluded_core "dont_dump" \
160           "shared_anon" "0x55" "1" } }
161
162 # If corefile loading is not supported, we do not even try to run the
163 # tests.
164 set core_supported [gdb_gcore_cmd "$non_private_anon_core" "save a corefile"]
165 if { !$core_supported } {
166     untested "corefile generation is not supported"
167     return -1
168 }
169
170 gdb_test_multiple "info inferiors" "getting inferior pid" {
171     -re "process $decimal.*\r\n$gdb_prompt $" {
172     }
173     -re "Remote target.*$gdb_prompt $" {
174         # If the target does not provide PID information (like usermode QEMU),
175         # just bail out as the rest of the test may rely on it, giving spurious
176         # failures.
177         return -1
178     }
179 }
180
181 # Get the main function's address.
182 set main_addr ""
183 gdb_test_multiple "print/x &main" "getting main's address" {
184     -re "$decimal = \($hex\)\r\n$gdb_prompt $" {
185         set main_addr $expect_out(1,string)
186     }
187 }
188
189 # Obtain the address of each variable that will be checked on each
190 # case.
191 unset -nocomplain coredump_var_addr
192 foreach item $all_anon_corefiles {
193     foreach name [list [lindex $item 3] [lindex $item 4]] {
194         set test "print/x $name"
195         gdb_test_multiple $test $test {
196             -re " = \($hex\)\r\n$gdb_prompt $" {
197                 set coredump_var_addr($name) $expect_out(1,string)
198             }
199         }
200     }
201 }
202
203 # Generate corefiles for the "anon" case.
204 foreach item $all_anon_corefiles {
205     with_test_prefix "saving corefile for [lindex $item 0]" {
206         do_save_core [lindex $item 1] [subst [lindex $item 2]] [lindex $item 6]
207     }
208 }
209
210 with_test_prefix "saving corefile for non-Private-Shared-Anon-File" {
211     do_save_core "0x60" $non_private_shared_anon_file_core "0"
212 }
213
214 clean_restart $testfile
215
216 foreach item $all_anon_corefiles {
217     with_test_prefix "loading and testing corefile for [lindex $item 0]" {
218         do_load_and_test_core [subst [lindex $item 2]] [lindex $item 3] \
219             [lindex $item 4] [lindex $item 5] [lindex $item 6]
220     }
221
222     with_test_prefix "disassembling function main for [lindex $item 0]" {
223         test_disasm [subst [lindex $item 2]] $main_addr 0
224     }
225 }
226
227 with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File" {
228     test_disasm $non_private_shared_anon_file_core $main_addr 1
229 }