xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.multi/multi-arch-exec.exp (revision a04395531661c5e8d314125d5ae77d4cbedd5d73)
1# Copyright 2009-2019 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 multi-exec / multi-process features that work for all configurations,
17# even ones that cannot run multiple processes simultaneously.
18
19set testfile "multi-arch-exec"
20
21# The plain remote target can't do multiple inferiors.
22if [use_gdb_stub] {
23    return
24}
25
26# The 64-bit compile may succeed for i386-linux, but gdb won't be able
27# to load the file.
28if [istarget "i?86-*linux*"] {
29    return
30}
31
32# The testcase builds two programs, each of its own architecture.  For
33# example, one built with -m64, another with -m32.  The exact compiler
34# options depends on target triplet.  We generically refer to the
35# architectures simply as 'architecture 1' and 'architecture 2'.  Each
36# program is actually built twice, once for each architecture, because
37# we test both execing from arch1 to arch2 and from arch2 to arch1.
38# The architecture of the executable that execs is encoded in the
39# binaries' names, like so:
40#
41#    $first_arch-multi-arch-exec         # execing program
42#    $first_arch-multi-arch-exec-hello   # execed program
43
44# Append the options necessary to build a program for architecture 1
45# to the OPTIONS_VAR list.
46
47proc append_arch1_options {options_var} {
48    upvar 1 $options_var options
49
50    if { [istarget "aarch64*-*-*"] } {
51	return 1
52    }
53
54    lappend options "additional_flags=-m64"
55    return 1
56}
57
58# Append the options necessary to build a program for architecture 2
59# to the OPTIONS_VAR list.
60
61proc append_arch2_options {options_var} {
62    upvar 1 $options_var options
63
64    if { [istarget "aarch64*-*-*"] } {
65	if {[info exists ARM_CC_FOR_TARGET]} {
66	    lappend options "compiler=${ARM_CC_FOR_TARGET}"
67	    return 1
68	} else {
69	    unsupported "ARM compiler is not known"
70	    return 0
71	}
72    }
73
74    if [istarget "s390*-*-*"] {
75	set march "-m31"
76    } else {
77	set march "-m32"
78    }
79    lappend options "additional_flags=${march}"
80    return 1
81}
82
83# Append the options necessary to build a program for architecture
84# ARCH to the OPTIONS_VAR list.  Returns true on success.
85
86proc append_arch_options {arch options_var} {
87    upvar 1 $options_var options
88
89    if {$arch == 1} {
90	return [append_arch1_options options]
91    } elseif {$arch == 2} {
92	return [append_arch2_options options]
93    } else {
94	error "unhandled architecture: $arch"
95    }
96}
97
98# Build the executables for testing with FIRST_ARCH (either 1 or 2) as
99# the architecture before the exec.  Returns true on success.
100
101proc build_executables { first_arch } {
102
103    # Can't use standard_testfile, we want executables with specialized
104    # names.
105    set from_exec "$first_arch-multi-arch-exec"
106    set from_srcfile multi-arch-exec.c
107    set from_binfile [standard_output_file ${from_exec}]
108
109    set to_exec "$first_arch-multi-arch-exec-hello"
110    set to_srcfile hello.c
111    set to_binfile [standard_output_file ${to_exec}]
112
113    # Build two executables, one for each arch.
114
115    if {$first_arch == 1} {
116	set from_arch 1
117	set to_arch 2
118    } elseif {$first_arch == 2} {
119	set from_arch 2
120	set to_arch 1
121    } else {
122	error "unhandled first architecture: $first_arch"
123    }
124
125    set from_options [list debug pthreads]
126    if {![append_arch_options $from_arch from_options]} {
127	return 0
128    }
129
130    if { [build_executable "failed to prepare" ${from_exec} "${from_srcfile}" \
131	      $from_options] } {
132	return 0
133    }
134
135    set to_options [list debug]
136    if {![append_arch_options $to_arch to_options]} {
137	return 0
138    }
139
140    if { [build_executable "failed to prepare" ${to_exec} "${to_srcfile}" \
141	      $to_options] } {
142	return 0
143    }
144
145    return 1
146}
147
148proc do_test { first_arch mode selected_thread } {
149	set from_exec "$first_arch-multi-arch-exec"
150
151	clean_restart ${from_exec}
152	if ![runto all_started] then {
153	    fail "couldn't run to all_started"
154	    return -1
155	}
156
157	# Delete the breakpoint at 'all_started' otherwise GDB may
158	# switch context back to thread 1 to step over the breakpoint.
159	delete_breakpoints
160
161	# A location for this breakpoint should be found in the new
162	# post-exec image too.
163	gdb_breakpoint main
164
165	gdb_test "thread $selected_thread" "Switching to thread $selected_thread .*"
166
167	gdb_test_no_output "set follow-exec-mode $mode"
168
169	# Test that GDB updates the target description / arch successfuly
170	# after the exec.
171	gdb_test "continue" "Breakpoint 2, main.*" "continue across exec that changes architecture"
172}
173
174# Test both arch1=>arch2 and arch2=>arch1.
175foreach_with_prefix first_arch {1 2} {
176    if {![build_executables $first_arch]} {
177	continue
178    }
179
180    # Test handling the exec event with either the main thread or the
181    # second thread selected.  This tries to ensure that GDB doesn't read
182    # registers off of the execing thread before figuring out its
183    # architecture.
184    foreach_with_prefix selected_thread {1 2} {
185	foreach_with_prefix follow_exec_mode {"same" "new"} {
186	    do_test $first_arch $follow_exec_mode $selected_thread
187	}
188    }
189}
190