1 # Copyright 2015 Free Software Foundation, Inc.
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.
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.
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/>.
18 if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
19 untested "could not compile test program"
23 if { ![runto_main] } {
24 untested "could not run to main"
28 gdb_breakpoint [gdb_get_line_number "break-here"]
29 gdb_continue_to_breakpoint "break-here" ".* break-here .*"
31 proc do_save_core { filter_flag core ipid } {
32 verbose -log "writing $filter_flag to /proc/$ipid/coredump_filter"
34 remote_exec target "sh -c \"echo $filter_flag > /proc/$ipid/coredump_filter\""
36 # Generate a corefile.
37 gdb_gcore_cmd "$core" "save corefile"
40 proc do_load_and_test_core { core var working_var working_value } {
41 global hex decimal addr
43 set core_loaded [gdb_core_cmd "$core" "load core"]
44 if { $core_loaded == -1 } {
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)"
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.
63 proc test_disasm { core address should_fail } {
66 # Restart GDB without loading the binary.
67 with_test_prefix "no binary" {
71 set core_loaded [gdb_core_cmd "$core" "load core"]
72 if { $core_loaded == -1 } {
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"
81 gdb_test "x/i \$pc" "=> $hex:\t\[^C\].*" \
82 "disassemble function with corefile and without a binary"
86 with_test_prefix "with binary" {
87 clean_restart $testfile
89 set core_loaded [gdb_core_cmd "$core" "load core"]
90 if { $core_loaded == -1 } {
95 gdb_test "disassemble $address" "Dump of assembler code for function.*" \
96 "disassemble function with corefile and with a binary"
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]
106 # We will generate a few corefiles.
108 # This list is composed by sub-lists, and their elements are (in
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
121 # This list refers to the corefiles generated by MAP_ANONYMOUS in the
124 set all_anon_corefiles { { "non-Private-Anonymous" "0x7e" \
125 $non_private_anon_core \
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" } }
135 # If corefile loading is not supported, we do not even try to run the
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"
143 # Get the inferior's PID.
145 gdb_test_multiple "info inferiors" "getting inferior pid" {
146 -re "process \($decimal\).*\r\n$gdb_prompt $" {
147 set infpid $expect_out(1,string)
151 # Get the main function's address.
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)
159 # Obtain the address of each variable that will be checked on each
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)
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
179 with_test_prefix "saving corefile for non-Private-Shared-Anon-File" {
180 do_save_core "0x60" $non_private_shared_anon_file_core $infpid
183 clean_restart $testfile
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]
191 with_test_prefix "disassembling function main for [lindex $item 0]" {
192 test_disasm [subst [lindex $item 2]] $main_addr 0
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