1 # Copyright 1997-2015 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This is a test of gdb's follow-exec-mode.
18 # It first checks that exec events are supported by using a catchpoint,
19 # then tests multiple scenarios for follow-exec-mode using parameters
22 # - different commands to execute past the exec
23 # - re-running both the original and new inferiors.
25 # Note that we can't single-step past an exec call. There has to
26 # be a breakpoint in order to stop after the exec, even if we use
27 # a single-step command to execute past the exec.
29 if { [is_remote target] || ![isnative] } then {
33 # Until "catch exec" is implemented on other targets...
35 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
39 standard_testfile foll-exec-mode.c
41 set testfile2 "execd-prog"
42 set srcfile2 ${testfile2}.c
43 set binfile2 [standard_output_file ${testfile2}]
45 set compile_options debug
46 set dirname [relative_filename [pwd] [file dirname $binfile]]
47 lappend compile_options "additional_flags=-DBASEDIR=\"$dirname\""
49 # build the first test case
50 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
51 untested foll-exec-mode.exp
55 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
56 untested foll-exec-mode.exp
60 # Test exec catchpoints to ensure exec events are supported.
62 proc do_catch_exec_test { } {
66 clean_restart $testfile
68 # Start the program running, and stop at main.
70 if ![runto_main] then {
71 fail "Couldn't run ${testfile}"
75 # Verify that the system supports "catch exec".
76 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
77 set has_exec_catchpoints 0
78 gdb_test_multiple "continue" "continue to first exec catchpoint" {
79 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
80 unsupported "continue to first exec catchpoint"
82 -re ".*Catchpoint.*$gdb_prompt $" {
83 set has_exec_catchpoints 1
84 pass "continue to first exec catchpoint"
88 if {$has_exec_catchpoints == 0} {
89 unsupported "exec catchpoints"
94 # Test follow-exec-mode in the specified scenario.
95 # MODE determines whether follow-exec-mode is "same" or "new".
96 # CMD determines the command used to execute past the exec call.
97 # INFSWITCH is ignored for MODE == "same", and for "new" it is
98 # used to determine whether to switch to the original inferior
101 proc do_follow_exec_mode_tests { mode cmd infswitch } {
102 global binfile srcfile srcfile2 testfile testfile2
105 with_test_prefix "$mode,$cmd,$infswitch" {
106 clean_restart $testfile
108 # Start the program running, and stop at main.
110 if ![runto_main] then {
111 fail "Couldn't run ${testfile}"
115 # Set the follow-exec mode.
117 gdb_test_no_output "set follow-exec-mode $mode"
119 # Run to the line of the exec call.
121 gdb_breakpoint [gdb_get_line_number "Set breakpoint here"]
122 gdb_continue_to_breakpoint "continue to line of exec call"
124 # Set up the output we expect to see after we execute past the exec.
126 set execd_line [gdb_get_line_number "after-exec" $srcfile2]
127 set expected_re ".*xecuting new program: .*${testfile2}.*Breakpoint .,.*${srcfile2}:${execd_line}.*$gdb_prompt $"
129 # Set a breakpoint after the exec call if we aren't single-stepping
132 if {$cmd == "continue"} {
133 gdb_breakpoint "$execd_line"
136 # Execute past the exec call.
138 set test "$cmd past exec"
139 gdb_test_multiple $cmd $test {
145 # Set expected output, given the test parameters.
147 if {$mode == "same"} {
148 set expected_re "\\* 1.*process.*"
150 set expected_re "\\* 2.*process.*$testfile2 \r\n 1.*null.*$testfile.*"
153 # Check that the inferior list is correct:
154 # - one inferior for MODE == "same"
155 # - two inferiors for MODE == "new", current is execd program
157 gdb_test "info inferiors" $expected_re "Check inferior list"
160 if {$mode == "same"} {
161 # One inferior, the execd program.
162 set expected_inf $testfile2
163 } elseif {$infswitch == "infswitch"} {
164 # Two inferiors, we have switched to the original program.
165 set expected_inf $testfile
166 gdb_test "inferior 1" "Switching to inferior 1.*$testfile.*" "Switch inferiors"
168 # Two inferiors, run the execd program
169 set expected_inf $testfile2
172 # Now check that a 'run' command will run the correct inferior.
174 set test "use correct executable ($expected_inf) for run after follow exec"
176 gdb_test_multiple "" $test {
177 -re {Start it from the beginning\? \(y or n\) $} {
181 -re "Starting program: .*$expected_inf.*Breakpoint .,.*\r\n$gdb_prompt $" {
190 foreach cmd {"next" "continue"} {
191 foreach mode {"same" "new"} {
192 # Test basic follow-exec-mode.
193 do_follow_exec_mode_tests $mode $cmd "no_infswitch"
194 if {$mode == "new"} {
195 # Test that when we do 'run' we get the correct executable.
196 do_follow_exec_mode_tests $mode $cmd "infswitch"