Harden gdb.base/coredump-filter.exp
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / coredump-filter.exp
1 # Copyright 2015 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 untested "could not compile test program"
26 return -1
27 }
28
29 if { ![runto_main] } {
30 untested "could not run to main"
31 return -1
32 }
33
34 gdb_breakpoint [gdb_get_line_number "break-here"]
35 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
36
37 proc do_save_core { filter_flag core ipid } {
38 verbose -log "writing $filter_flag to /proc/$ipid/coredump_filter"
39
40 remote_exec target "sh -c \"echo $filter_flag > /proc/$ipid/coredump_filter\""
41
42 # Generate a corefile.
43 gdb_gcore_cmd "$core" "save corefile"
44 }
45
46 proc do_load_and_test_core { core var working_var working_value } {
47 global hex decimal coredump_var_addr
48
49 set core_loaded [gdb_core_cmd "$core" "load core"]
50 if { $core_loaded == -1 } {
51 fail "loading $core"
52 return
53 }
54
55 # Access the memory the addresses point to.
56 gdb_test "print/x *(char *) $coredump_var_addr($var)" "\(\\\$$decimal = <error: \)?Cannot access memory at address $hex\(>\)?" \
57 "printing $var when core is loaded (should not work)"
58 gdb_test "print/x *(char *) $coredump_var_addr($working_var)" " = $working_value.*" \
59 "print/x *$working_var ( = $working_value)"
60 }
61
62 # We do not do file-backed mappings in the test program, but it is
63 # important to test this anyway. One way of performing the test is to
64 # load GDB with a corefile but without a binary, and then ask for the
65 # disassemble of a function (i.e., the binary's .text section). GDB
66 # should fail in this case. However, it must succeed if the binary is
67 # provided along with the corefile. This is what we test here.
68
69 proc test_disasm { core address should_fail } {
70 global testfile hex
71
72 # Restart GDB without loading the binary.
73 with_test_prefix "no binary" {
74 gdb_exit
75 gdb_start
76
77 set core_loaded [gdb_core_cmd "$core" "load core"]
78 if { $core_loaded == -1 } {
79 fail "loading $core"
80 return
81 }
82
83 if { $should_fail == 1 } {
84 gdb_test "x/i \$pc" "=> $hex:\tCannot access memory at address $hex" \
85 "disassemble function with corefile and without a binary"
86 } else {
87 gdb_test "x/i \$pc" "=> $hex:\t\[^C\].*" \
88 "disassemble function with corefile and without a binary"
89 }
90 }
91
92 with_test_prefix "with binary" {
93 clean_restart $testfile
94
95 set core_loaded [gdb_core_cmd "$core" "load core"]
96 if { $core_loaded == -1 } {
97 fail "loading $core"
98 return
99 }
100
101 gdb_test "disassemble $address" "Dump of assembler code for function.*" \
102 "disassemble function with corefile and with a binary"
103 }
104 }
105
106 set non_private_anon_core [standard_output_file non-private-anon.gcore]
107 set non_shared_anon_core [standard_output_file non-shared-anon.gcore]
108 # A corefile without {private,shared} {anonymous,file-backed} pages
109 set non_private_shared_anon_file_core [standard_output_file non-private-shared-anon-file.gcore]
110 set dont_dump_core [standard_output_file dont-dump.gcore]
111
112 # We will generate a few corefiles.
113 #
114 # This list is composed by sub-lists, and their elements are (in
115 # order):
116 #
117 # - name of the test
118 # - hexadecimal value to be put in the /proc/PID/coredump_filter file
119 # - name of the variable that contains the name of the corefile to be
120 # generated (including the initial $).
121 # - name of the variable in the C source code that points to the
122 # memory mapping that will NOT be present in the corefile.
123 # - name of a variable in the C source code that points to a memory
124 # mapping that WILL be present in the corefile
125 # - corresponding value expected for the above variable
126 #
127 # This list refers to the corefiles generated by MAP_ANONYMOUS in the
128 # test program.
129
130 set all_anon_corefiles { { "non-Private-Anonymous" "0x7e" \
131 $non_private_anon_core \
132 "private_anon" \
133 "shared_anon" "0x22" }
134 { "non-Shared-Anonymous" "0x7d" \
135 $non_shared_anon_core "shared_anon" \
136 "private_anon" "0x11" }
137 { "DoNotDump" "0x33" \
138 $dont_dump_core "dont_dump" \
139 "shared_anon" "0x22" } }
140
141 # If corefile loading is not supported, we do not even try to run the
142 # tests.
143 set core_supported [gdb_gcore_cmd "$non_private_anon_core" "save a corefile"]
144 if { !$core_supported } {
145 untested "corefile generation is not supported"
146 return -1
147 }
148
149 # Get the inferior's PID.
150 set infpid ""
151 gdb_test_multiple "info inferiors" "getting inferior pid" {
152 -re "process \($decimal\).*\r\n$gdb_prompt $" {
153 set infpid $expect_out(1,string)
154 }
155 -re "Remote target.*$gdb_prompt $" {
156 # If the target does not provide PID information (like usermode QEMU),
157 # just bail out as the rest of the test may rely on it, giving spurious
158 # failures.
159 return -1
160 }
161 }
162
163 # Get the main function's address.
164 set main_addr ""
165 gdb_test_multiple "print/x &main" "getting main's address" {
166 -re "$decimal = \($hex\)\r\n$gdb_prompt $" {
167 set main_addr $expect_out(1,string)
168 }
169 }
170
171 # Obtain the address of each variable that will be checked on each
172 # case.
173 foreach item $all_anon_corefiles {
174 foreach name [list [lindex $item 3] [lindex $item 4]] {
175 set test "print/x $name"
176 gdb_test_multiple $test $test {
177 -re " = \($hex\)\r\n$gdb_prompt $" {
178 set coredump_var_addr($name) $expect_out(1,string)
179 }
180 }
181 }
182 }
183
184 # Generate corefiles for the "anon" case.
185 foreach item $all_anon_corefiles {
186 with_test_prefix "saving corefile for [lindex $item 0]" {
187 do_save_core [lindex $item 1] [subst [lindex $item 2]] $infpid
188 }
189 }
190
191 with_test_prefix "saving corefile for non-Private-Shared-Anon-File" {
192 do_save_core "0x60" $non_private_shared_anon_file_core $infpid
193 }
194
195 clean_restart $testfile
196
197 foreach item $all_anon_corefiles {
198 with_test_prefix "loading and testing corefile for [lindex $item 0]" {
199 do_load_and_test_core [subst [lindex $item 2]] [lindex $item 3] \
200 [lindex $item 4] [lindex $item 5]
201 }
202
203 with_test_prefix "disassembling function main for [lindex $item 0]" {
204 test_disasm [subst [lindex $item 2]] $main_addr 0
205 }
206 }
207
208 with_test_prefix "loading and testing corefile for non-Private-Shared-Anon-File" {
209 test_disasm $non_private_shared_anon_file_core $main_addr 1
210 }
This page took 0.03376 seconds and 4 git commands to generate.