xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.base/reread.exp (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1# Copyright 1998-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# Build programs in PIE mode, to reproduce PR 21555.
17foreach_with_prefix opts {
18    { "" "" }
19    { "-fPIE" "ldflags=-pie" } } {
20
21	# build the first test case
22
23	set testfile1 "reread1"
24	set srcfile1 ${testfile1}.c
25	# Cygwin needs $EXEEXT.
26	set binfile1 [standard_output_file ${testfile1}$EXEEXT]
27
28	set testfile1_opt [list debug nowarnings \
29			       additional_flags=[lindex $opts 0] \
30			       [lindex $opts 1] ]
31	if  { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile1}" \
32		   executable ${testfile1_opt}] != "" } {
33	    untested "failed to compile first testcase"
34	    return -1
35	}
36
37	# build the second test case
38
39	set testfile2 "reread2"
40	set srcfile2 ${testfile2}.c
41	set binfile2 [standard_output_file ${testfile2}$EXEEXT]
42
43	set testfile2_opt1 [list debug nowarnings \
44				additional_flags=[lindex $opts 0] \
45				[lindex $opts 1]]
46	set testfile2_op2 [list debug nowarnings \
47			       "additional_flags=-DNO_SECTIONS [lindex $opts 0]" \
48			       [lindex $opts 1]]
49	if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" \
50		   executable ${testfile2_opt1}] != ""
51	      && [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" \
52		      executable ${testfile2_opt2}] != ""} {
53	    untested "failed to compile second testcase"
54	    return -1
55	}
56
57	# Start with a fresh gdb.
58
59	set testfile "reread"
60	set binfile [standard_output_file ${testfile}$EXEEXT]
61
62	gdb_start
63	gdb_reinitialize_dir $srcdir/$subdir
64
65	# Load the first executable.
66
67	gdb_rename_execfile ${binfile1} ${binfile}
68	gdb_load ${binfile}
69
70	# Set a breakpoint at foo
71
72	gdb_test "break foo" \
73	    "Breakpoint.*at.* file .*$srcfile1, line 14.*" \
74	    "breakpoint foo in first file"
75
76
77	# Run, should see "Breakpoint 1, foo () at hello1.c:14"
78
79	gdb_run_cmd
80	gdb_test "" "Breakpoint.* foo .* at .*$srcfile1:14.*" "run to foo()"
81
82	# Restore first executable to its original name, and move
83	# second executable into its place.  Ensure that the new
84	# executable is at least a second newer than the old.
85
86	gdb_rename_execfile ${binfile} ${binfile1}
87	gdb_rename_execfile ${binfile2} ${binfile}
88	gdb_test "shell sleep 1" ".*" ""
89	gdb_touch_execfile ${binfile}
90
91	# Run a second time; GDB should detect that the executable has changed
92	# and reset the breakpoints correctly.
93	# Should see "Breakpoint 1, foo () at reread2.c:9"
94
95	set test "run to foo() second time"
96	if [is_remote target] {
97	    unsupported $test
98	} else {
99	    gdb_run_cmd
100	    gdb_test "" "Breakpoint.* foo .* at .*:9.*" $test
101	}
102
103
104	### Second pass: verify that GDB checks the executable file's
105	### timestamp when the program is *restarted*, not just when it exits.
106
107	if [is_remote target] {
108	    unsupported "second pass: GDB should check for changes before running"
109	} else {
110
111	    # Put the older executable back in place.
112	    gdb_rename_execfile ${binfile} ${binfile2}
113	    gdb_rename_execfile ${binfile1} ${binfile}
114
115	    # Restart GDB entirely.
116	    clean_restart ${binfile}
117
118	    # Set a breakpoint on foo and run to it.
119	    gdb_test "break foo" \
120		"Breakpoint.*at.* file .*$srcfile1, line 14.*" \
121		"second pass: breakpoint foo in first file"
122	    gdb_run_cmd
123	    gdb_test "" "Breakpoint.* foo .* at .*$srcfile1:14.*" \
124		"second pass: run to foo()"
125
126	    # This time, let the program run to completion.  If GDB checks the
127	    # executable file's timestamp now, it won't notice any change.
128	    gdb_continue_to_end "second pass"
129
130	    # Now move the newer executable into place, and re-run.  GDB
131	    # should still notice that the executable file has changed,
132	    # and still re-set the breakpoint appropriately.
133	    gdb_rename_execfile ${binfile} ${binfile1}
134	    gdb_rename_execfile ${binfile2} ${binfile}
135	    gdb_run_cmd
136	    gdb_test "" "Breakpoint.* foo .* at .*:9.*" \
137		"second pass: run to foo() second time"
138	}
139
140    }
141# End of tests.
142
143return 0
144