1# Copyright 2009-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 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 {[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 "powerpc64*-*-*"] { 75 set march "-m64" 76 } elseif [istarget "s390*-*-*"] { 77 set march "-m31" 78 } else { 79 set march "-m32" 80 } 81 lappend options "additional_flags=${march}" 82 return 1 83} 84 85# Append the options necessary to build a program for architecture 86# ARCH to the OPTIONS_VAR list. Returns true on success. 87 88proc append_arch_options {arch options_var} { 89 upvar 1 $options_var options 90 91 if {$arch == 1} { 92 return [append_arch1_options options] 93 } elseif {$arch == 2} { 94 return [append_arch2_options options] 95 } else { 96 error "unhandled architecture: $arch" 97 } 98} 99 100# Build the executables for testing with FIRST_ARCH (either 1 or 2) as 101# the architecture before the exec. Returns true on success. 102 103proc build_executables { first_arch } { 104 105 # Can't use standard_testfile, we want executables with specialized 106 # names. 107 set from_exec "$first_arch-multi-arch-exec" 108 set from_srcfile multi-arch-exec.c 109 set from_binfile [standard_output_file ${from_exec}] 110 111 set to_exec "$first_arch-multi-arch-exec-hello" 112 set to_srcfile hello.c 113 set to_binfile [standard_output_file ${to_exec}] 114 115 # Build two executables, one for each arch. 116 117 if {$first_arch == 1} { 118 set from_arch 1 119 set to_arch 2 120 } elseif {$first_arch == 2} { 121 set from_arch 2 122 set to_arch 1 123 } else { 124 error "unhandled first architecture: $first_arch" 125 } 126 127 set from_options [list debug pthreads] 128 if {![append_arch_options $from_arch from_options]} { 129 return 0 130 } 131 132 if { [build_executable "failed to prepare" ${from_exec} "${from_srcfile}" \ 133 $from_options] } { 134 return 0 135 } 136 137 set to_options [list debug] 138 if {![append_arch_options $to_arch to_options]} { 139 return 0 140 } 141 142 if { [build_executable "failed to prepare" ${to_exec} "${to_srcfile}" \ 143 $to_options] } { 144 return 0 145 } 146 147 return 1 148} 149 150proc do_test { first_arch mode selected_thread } { 151 global bkptno_numopt_re 152 set from_exec "$first_arch-multi-arch-exec" 153 154 clean_restart ${from_exec} 155 if {![runto all_started]} { 156 return -1 157 } 158 159 # Delete the breakpoint at 'all_started' otherwise GDB may 160 # switch context back to thread 1 to step over the breakpoint. 161 delete_breakpoints 162 163 # A location for this breakpoint should be found in the new 164 # post-exec image too. 165 gdb_breakpoint main 166 167 gdb_test "thread $selected_thread" "Switching to thread $selected_thread .*" 168 169 gdb_test_no_output "set follow-exec-mode $mode" 170 171 # Test that GDB updates the target description / arch successfuly 172 # after the exec. 173 gdb_test "continue" "Breakpoint $bkptno_numopt_re, main.*" "continue across exec that changes architecture" 174} 175 176# Test both arch1=>arch2 and arch2=>arch1. 177foreach_with_prefix first_arch {1 2} { 178 if {![build_executables $first_arch]} { 179 continue 180 } 181 182 # Test handling the exec event with either the main thread or the 183 # second thread selected. This tries to ensure that GDB doesn't read 184 # registers off of the execing thread before figuring out its 185 # architecture. 186 foreach_with_prefix selected_thread {1 2} { 187 foreach_with_prefix follow_exec_mode {"same" "new"} { 188 do_test $first_arch $follow_exec_mode $selected_thread 189 } 190 } 191} 192