1#   Copyright 2017-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
16standard_testfile
17
18set gdbfile [standard_output_file ${testfile}.gdb]
19
20# Test rereading executable.  See PR gdb/21337.
21
22proc generate_cmd_file {gdbfile binfile} {
23    set ofd [open $gdbfile w]
24
25    puts $ofd "file ${binfile}"
26    puts $ofd "shell sleep 1; touch ${binfile}"
27    puts $ofd "run"
28    puts $ofd "file"
29    puts $ofd "file ${binfile}"
30    puts $ofd "shell sleep 1; touch ${binfile}"
31    puts $ofd "run"
32    puts $ofd "file"
33    puts $ofd "file ${binfile}"
34    puts $ofd "shell sleep 1; touch ${binfile}"
35    puts $ofd "run"
36    puts $ofd "file"
37    puts $ofd "p \"source-command-completed\""
38    close $ofd
39}
40
41if [use_gdb_stub] {
42    return 0
43}
44
45if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
46    return -1
47}
48
49# Start with a fresh gdb.
50clean_restart ${testfile}
51
52# Using the source command to read commands from a file is important,
53# otherwise section data is freed and reallocated using the same
54# memory locations and the bug is not exposed.
55generate_cmd_file $gdbfile $binfile
56
57gdb_test "source $gdbfile" ".*source-command-completed.*" \
58    "source $testfile.gdb 1"
59# Sometimes the failure only occurs on the second invocation.
60gdb_test "source $gdbfile" ".*source-command-completed.*" \
61    "source $testfile.gdb 2"
62