1# Copyright 2002-2020 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
20standard_testfile
21
22if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
23    return -1
24}
25
26if { ! [ runto_main ] } then {
27    untested "couldn't run to main"
28    return -1
29}
30
31gdb_test "break terminal_func" "Breakpoint .* at .*${srcfile}, line .*" \
32	"set breakpoint at terminal_func"
33
34gdb_test "continue" "Breakpoint .* terminal_func.*" \
35	"continue to terminal_func"
36
37set print_prefix ".\[0123456789\]* = "
38
39set pre_corefile_backtrace [capture_command_output "backtrace" ""]
40set pre_corefile_regs [capture_command_output "info registers" ""]
41set pre_corefile_allregs [capture_command_output "info all-reg" ""]
42set pre_corefile_sysregs [capture_command_output "info reg system" ""]
43set pre_corefile_static_array \
44	[capture_command_output "print static_array" "$print_prefix"]
45set pre_corefile_uninit_array \
46	[capture_command_output "print un_initialized_array" "$print_prefix"]
47set pre_corefile_heap_string \
48	[capture_command_output "print heap_string" "$print_prefix"]
49set pre_corefile_local_array \
50	[capture_command_output "print array_func::local_array" "$print_prefix"]
51set pre_corefile_extern_array \
52	[capture_command_output "print extern_array" "$print_prefix"]
53
54set corefile [standard_output_file gcore.test]
55set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
56if {!$core_supported} {
57  return -1
58}
59
60# Now restart gdb and load the corefile.
61gdb_exit
62gdb_start
63gdb_reinitialize_dir $srcdir/$subdir
64gdb_load ${binfile}
65
66set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
67if { $core_loaded == -1 } {
68    # No use proceeding from here.
69    return
70}
71
72gdb_test_sequence "where" "where in corefile" {
73    "\[\r\n\]+#0 .* terminal_func \\(\\) at "
74    "\[\r\n\]+#1 .* array_func \\(\\) at "
75    "\[\r\n\]+#2 .* factorial_func \\(value=1\\) at "
76    "\[\r\n\]+#3 .* factorial_func \\(value=2\\) at "
77    "\[\r\n\]+#4 .* factorial_func \\(value=3\\) at "
78    "\[\r\n\]+#5 .* factorial_func \\(value=4\\) at "
79    "\[\r\n\]+#6 .* factorial_func \\(value=5\\) at "
80    "\[\r\n\]+#7 .* factorial_func \\(value=6\\) at "
81    "\[\r\n\]+#8 .* main \\(.*\\) at "
82}
83
84set post_corefile_regs [capture_command_output "info registers" ""]
85if ![string compare $pre_corefile_regs $post_corefile_regs] then {
86    pass "corefile restored general registers"
87} else {
88    fail "corefile restored general registers"
89}
90
91set post_corefile_allregs [capture_command_output "info all-reg" ""]
92if ![string compare $pre_corefile_allregs $post_corefile_allregs] then {
93    pass "corefile restored all registers"
94} else {
95    fail "corefile restored all registers"
96}
97
98set post_corefile_sysregs [capture_command_output "info reg system" ""]
99if ![string compare $pre_corefile_sysregs $post_corefile_sysregs] then {
100    pass "corefile restored system registers"
101} else {
102    fail "corefile restored system registers"
103}
104
105set post_corefile_extern_array \
106	[capture_command_output "print extern_array" "$print_prefix"]
107if ![string compare $pre_corefile_extern_array $post_corefile_extern_array]  {
108    pass "corefile restored extern array"
109} else {
110    fail "corefile restored extern array"
111}
112
113set post_corefile_static_array \
114	[capture_command_output "print static_array" "$print_prefix"]
115if ![string compare $pre_corefile_static_array $post_corefile_static_array]  {
116    pass "corefile restored static array"
117} else {
118    fail "corefile restored static array"
119}
120
121set post_corefile_uninit_array \
122	[capture_command_output "print un_initialized_array" "$print_prefix"]
123if ![string compare $pre_corefile_uninit_array $post_corefile_uninit_array]  {
124    pass "corefile restored un-initialized array"
125} else {
126    fail "corefile restored un-initialized array"
127}
128
129set post_corefile_heap_string \
130	[capture_command_output "print heap_string" "$print_prefix"]
131if ![string compare $pre_corefile_heap_string $post_corefile_heap_string]  {
132    pass "corefile restored heap array"
133} else {
134    fail "corefile restored heap array"
135}
136
137set post_corefile_local_array \
138	[capture_command_output "print array_func::local_array" "$print_prefix"]
139if ![string compare $pre_corefile_local_array $post_corefile_local_array]  {
140    pass "corefile restored stack array"
141} else {
142    fail "corefile restored stack array"
143}
144
145set post_corefile_backtrace [capture_command_output "backtrace" ""]
146if ![string compare $pre_corefile_backtrace $post_corefile_backtrace]  {
147    pass "corefile restored backtrace"
148} else {
149    fail "corefile restored backtrace"
150}
151