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