8b220d68958cad759a77bda71fa816e391b320a3
[platform/upstream/binutils.git] / gdb / testsuite / gdb.base / gcore.exp
1 # Copyright 2002-2004, 2007-2012 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 # This file was written by Michael Snyder (msnyder@redhat.com)
17 # This is a test for the gdb command "generate-core-file".
18
19 if $tracelevel then {
20         strace $tracelevel
21 }
22
23
24 set testfile "gcore"
25 set srcfile  ${testfile}.c
26 set binfile  ${objdir}/${subdir}/${testfile}
27
28 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
29      untested gcore.exp
30      return -1
31 }
32
33 # Start with a fresh gdb.
34
35 gdb_exit
36 gdb_start
37 gdb_reinitialize_dir $srcdir/$subdir
38 gdb_load ${binfile}
39
40 # Does this gdb support gcore?
41 gdb_test_multiple "help gcore" "help gcore" {
42     -re "Undefined command: .gcore.*$gdb_prompt $" {
43         # gcore command not supported -- nothing to test here.
44         unsupported "gdb does not support gcore on this target"
45         return -1;
46     }
47     -re "Save a core file .*$gdb_prompt $" {
48         pass "help gcore"
49     }
50 }
51
52 if { ! [ runto_main ] } then {
53     untested gcore.exp
54     return -1
55 }
56
57 proc capture_command_output { command prefix } {
58     global gdb_prompt
59     global expect_out
60
61     set output_string ""
62     gdb_test_multiple "$command" "capture_command_output for $command" {
63         -re "${command}\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
64             set output_string $expect_out(1,string)
65         }
66     }
67     return $output_string
68 }
69
70 gdb_test "break terminal_func" "Breakpoint .* at .*${srcfile}, line .*" \
71         "set breakpoint at terminal_func"
72
73 gdb_test "continue" "Breakpoint .* terminal_func.*" \
74         "continue to terminal_func"
75
76 set print_prefix ".\[0123456789\]* = "
77
78 set pre_corefile_backtrace [capture_command_output "backtrace" ""]
79 set pre_corefile_regs [capture_command_output "info registers" ""]
80 set pre_corefile_allregs [capture_command_output "info all-reg" ""]
81 set pre_corefile_static_array \
82         [capture_command_output "print static_array" "$print_prefix"]
83 set pre_corefile_uninit_array \
84         [capture_command_output "print un_initialized_array" "$print_prefix"]
85 set pre_corefile_heap_string \
86         [capture_command_output "print heap_string" "$print_prefix"]
87 set pre_corefile_local_array \
88         [capture_command_output "print array_func::local_array" "$print_prefix"]
89 set pre_corefile_extern_array \
90         [capture_command_output "print extern_array" "$print_prefix"]
91
92 set escapedfilename [string_to_regexp ${objdir}/${subdir}/gcore.test]
93
94 set core_supported 0
95 gdb_test_multiple "gcore ${objdir}/${subdir}/gcore.test" \
96         "save a corefile" \
97 {
98   -re "Saved corefile ${escapedfilename}\[\r\n\]+$gdb_prompt $" {
99     pass "save a corefile"
100     global core_supported
101     set core_supported 1
102   }
103   -re "Can't create a corefile\[\r\n\]+$gdb_prompt $" {
104     unsupported "save a corefile"
105     global core_supported
106     set core_supported 0
107   }
108 }
109
110 if {!$core_supported} {
111   return -1
112 }
113
114 # Now restart gdb and load the corefile.
115 gdb_exit
116 gdb_start
117 gdb_reinitialize_dir $srcdir/$subdir
118 gdb_load ${binfile}
119
120 gdb_test_multiple "core ${objdir}/${subdir}/gcore.test" \
121     "re-load generated corefile" {
122     -re ".* is not a core dump:.*$gdb_prompt $" {
123         fail "re-load generated corefile (bad file format)"
124         # No use proceeding from here.
125         return; 
126     }
127     -re ".*: No such file or directory.*$gdb_prompt $" {
128         fail "re-load generated corefile (file not found)"
129         # No use proceeding from here.
130         return; 
131     }
132     -re ".*Couldn't find .* registers in core file.*$gdb_prompt $" {
133         fail "re-load generated corefile (incomplete note section)"
134     }
135     -re "Core was generated by .*$gdb_prompt $" {
136         pass "re-load generated corefile"
137     }
138 }
139
140 gdb_test_sequence "where" "where in corefile" {
141     "\[\r\n\]+#0 .* terminal_func \\(\\) at "
142     "\[\r\n\]+#1 .* array_func \\(\\) at "
143     "\[\r\n\]+#2 .* factorial_func \\(value=1\\) at "
144     "\[\r\n\]+#3 .* factorial_func \\(value=2\\) at "
145     "\[\r\n\]+#4 .* factorial_func \\(value=3\\) at "
146     "\[\r\n\]+#5 .* factorial_func \\(value=4\\) at "
147     "\[\r\n\]+#6 .* factorial_func \\(value=5\\) at "
148     "\[\r\n\]+#7 .* factorial_func \\(value=6\\) at "
149     "\[\r\n\]+#8 .* main \\(.*\\) at "
150 }
151
152 set post_corefile_regs [capture_command_output "info registers" ""]
153 if ![string compare $pre_corefile_regs $post_corefile_regs] then {
154     pass "corefile restored general registers"
155 } else {
156     fail "corefile restored general registers"
157 }
158
159 set post_corefile_allregs [capture_command_output "info all-reg" ""]
160 if ![string compare $pre_corefile_allregs $post_corefile_allregs] then {
161     pass "corefile restored all registers"
162 } else {
163     fail "corefile restored all registers"
164 }
165
166 set post_corefile_extern_array \
167         [capture_command_output "print extern_array" "$print_prefix"]
168 if ![string compare $pre_corefile_extern_array $post_corefile_extern_array]  {
169     pass "corefile restored extern array"
170 } else {
171     fail "corefile restored extern array"
172 }
173
174 set post_corefile_static_array \
175         [capture_command_output "print static_array" "$print_prefix"]
176 if ![string compare $pre_corefile_static_array $post_corefile_static_array]  {
177     pass "corefile restored static array"
178 } else {
179     fail "corefile restored static array"
180 }
181
182 set post_corefile_uninit_array \
183         [capture_command_output "print un_initialized_array" "$print_prefix"]
184 if ![string compare $pre_corefile_uninit_array $post_corefile_uninit_array]  {
185     pass "corefile restored un-initialized array"
186 } else {
187     fail "corefile restored un-initialized array"
188 }
189
190 set post_corefile_heap_string \
191         [capture_command_output "print heap_string" "$print_prefix"]
192 if ![string compare $pre_corefile_heap_string $post_corefile_heap_string]  {
193     pass "corefile restored heap array"
194 } else {
195     fail "corefile restored heap array"
196 }
197
198 set post_corefile_local_array \
199         [capture_command_output "print array_func::local_array" "$print_prefix"]
200 if ![string compare $pre_corefile_local_array $post_corefile_local_array]  {
201     pass "corefile restored stack array"
202 } else {
203     fail "corefile restored stack array"
204 }
205
206 set post_corefile_backtrace [capture_command_output "backtrace" ""]
207 if ![string compare $pre_corefile_backtrace $post_corefile_backtrace]  {
208     pass "corefile restored backtrace"
209 } else {
210     fail "corefile restored backtrace"
211 }