1# Copyright 2014-2023 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# Test that when following an exec, we don't try to insert breakpoints
17# in the new image at the addresses the symbols had before the exec.
18
19standard_testfile
20
21# Build two copies of the program, each linked at a different address.
22# The address of "main" in the first binary should end up being an
23# unmapped address in the second binary.
24
25set objfile ${binfile}.o
26set exec1 ${binfile}1
27set exec2 ${binfile}2
28
29if { [gdb_compile [file join $srcdir $subdir $srcfile] $objfile \
30	  object [list debug]] != "" } {
31    untested "failed to compile"
32    return -1
33}
34
35if { [gdb_compile $objfile $exec1 executable {debug text_segment=0x1000000}] != ""
36     || [gdb_compile $objfile $exec2 executable {debug text_segment=0x2000000}] != ""} {
37    untested "link failed"
38    return -1
39}
40
41# First check whether the address of "main" in exec1 is readable in
42# exec2.  If it is, then skip the test as unsupported.
43
44clean_restart ${exec1}
45if {![runto_main]} {
46    return -1
47}
48
49set addr ""
50set test "main address first"
51gdb_test_multiple "p/x &main" $test {
52    -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
53	set addr $expect_out(1,string)
54	pass $test
55    }
56}
57
58clean_restart ${exec2}
59if {![runto_main]} {
60    return -1
61}
62
63set cannot_access 0
64set test "probe memory access"
65gdb_test_multiple "x $addr" $test {
66    -re "Cannot access memory at address .*$gdb_prompt $" {
67	set cannot_access 1
68	pass $test
69    }
70    -re ".*$gdb_prompt $" {
71	pass $test
72    }
73}
74
75if {!$cannot_access} {
76    unsupported "main address is readable in second binary"
77    return
78}
79
80# The test proper.  ALWAYS_INSERTED indicates whether testing in
81# "breakpoint always-inserted" mode.
82
83proc test { always_inserted } {
84    global exec1
85    global gdb_prompt
86
87    clean_restart ${exec1}
88
89    gdb_test_no_output "set breakpoint always-inserted $always_inserted"
90
91    if {![runto_main]} {
92	return -1
93    }
94
95    # Set a second breakpoint (whose original address also ends up
96    # unmmapped after the exec), for PR 19548.
97    gdb_test "break some_function" "Breakpoint .*"
98
99    # PR17431: with always-inserted on, we'd see:
100    #  (gdb) continue
101    #  Continuing.
102    #  Warning:
103    #  Cannot insert breakpoint 1.
104    #  Cannot access memory at address 0x10000ff
105
106    # PR 19548: with more than one breakpoint, we'd see:
107    #  (gdb) continue
108    #  Continuing.
109    #  process (...) is executing new program: (...)/execl-update-breakpoints2
110    #  Error in re-setting breakpoint 1: Warning:
111    #  Cannot insert breakpoint 2.
112    #  Cannot access memory at address 0x1000764
113    set not_nl "\[^\r\n\]*"
114    set regex ""
115    append regex \
116	"^continue\r\n" \
117	"Continuing\\.\r\n" \
118	"${not_nl} is executing new program: ${not_nl}\r\n" \
119	"(Reading ${not_nl} from remote target\\.\\.\\.\r\n)*" \
120	"(?:.Thread debugging using .*? enabled.\r\nUsing .*? library .*?\\.\r\n)?" \
121	"\r\n" \
122	"Breakpoint 1, main.*$gdb_prompt $"
123    set message "continue across exec"
124    gdb_test_multiple "continue" $message {
125	-re $regex {
126	    pass $message
127	}
128    }
129}
130
131foreach always_inserted { "off" "on" } {
132    with_test_prefix "always-inserted $always_inserted" {
133	test $always_inserted
134    }
135}
136