Home | History | Annotate | Line # | Download | only in gdb.mi
      1 # Copyright 2016-2024 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 -exec-run works as expected.  Exercises various testing
     17 # axes:
     18 #
     19 # - MI running on main UI vs separate UI.
     20 #
     21 # - inferior tty set to main tty vs separate tty.
     22 #
     23 # - forking the child failing and sending output to the right inferior
     24 #   terminal, vs the child not failing to start.
     25 
     26 load_lib mi-support.exp
     27 set MIFLAGS "-i=mi"
     28 
     29 # The purpose of this testcase is to test the -exec-run command. If we
     30 # cannot use it, then there is no point in running this testcase.
     31 require !use_gdb_stub
     32 
     33 standard_testfile mi-start.c
     34 
     35 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
     36      untested "could not build mi-exec-run"
     37      return -1
     38 }
     39 
     40 # The test proper.  INFTTY_MODE determines whether "set inferior-tty"
     41 # is in effect.  MI_MODE determines whether MI is run on the main UI,
     42 # or as a separate UI.  FORCE_FAIL is true when we want -exec-run to
     43 # fail and cause inferior output be sent to the inferior tty.
     44 
     45 proc test {inftty_mode mi_mode force_fail} {
     46     global srcdir subdir binfile srcfile
     47     global gdb_spawn_id gdb_main_spawn_id mi_spawn_id inferior_spawn_id
     48     global decimal
     49 
     50     mi_gdb_exit
     51 
     52     set start_ops {}
     53     if {$inftty_mode == "separate"} {
     54 	lappend start_ops "separate-inferior-tty"
     55     }
     56     if {$mi_mode == "separate"} {
     57 	lappend start_ops "separate-mi-tty"
     58     }
     59 
     60     if [mi_gdb_start $start_ops] {
     61 	return
     62     }
     63 
     64     if {$force_fail} {
     65 	# Disable the shell so that it's the first exec that fails,
     66 	# instead of the shell starting and then failing with some
     67 	# unspecified output.
     68 	mi_gdb_test "-gdb-set startup-with-shell off" ".*"
     69 	set bin $binfile.nox
     70     } else {
     71 	set bin $binfile
     72     }
     73 
     74     mi_delete_breakpoints
     75     mi_gdb_reinitialize_dir $srcdir/$subdir
     76     mi_gdb_load ${bin}
     77 
     78     # Useful for debugging:
     79     verbose -log "Channels:"
     80     verbose -log " inferior_spawn_id=$inferior_spawn_id"
     81     verbose -log " gdb_spawn_id=$gdb_spawn_id"
     82     verbose -log " gdb_main_spawn_id=$gdb_main_spawn_id"
     83     verbose -log " mi_spawn_id=$mi_spawn_id"
     84 
     85     if {$force_fail} {
     86 	set saw_perm_error 0
     87 	set saw_mi_error 0
     88 	set already_failed 0
     89 	set test "run failure detected"
     90 	send_gdb "-exec-run --start\n"
     91 
     92 	# Redirect through SPAWN_LIST global.  If the
     93 	# inferior_spawn_id is not the same as gdb_spawn_id, e.g. when
     94 	# testing with gdbserver, the gdbserver can exit after
     95 	# emitting it's error message.
     96 	#
     97 	# If inferior_spawn_id exits then we may see the eof from that
     98 	# spawn-id before we see the pattern from the gdb_spawn_id,
     99 	# which will kick us out of the gdb_expect, and cause us to
    100 	# fail the test.
    101 	#
    102 	# Instead we clean SPAWN_LIST once we've seen the expected
    103 	# pattern from that spawn-id, and after that we no longer care
    104 	# when gdbserver exits.
    105 	global spawn_list
    106 	set spawn_list "$inferior_spawn_id"
    107 
    108 	gdb_expect {
    109 	    -i spawn_list
    110 	    -re ".*Cannot exec.*Permission denied" {
    111 		set saw_perm_error 1
    112 		set spawn_list ""
    113 		verbose -log "saw perm error"
    114 		if {!$saw_mi_error} {
    115 		    exp_continue
    116 		}
    117 	    }
    118 	    -i "$gdb_spawn_id"
    119 	    -re "\\^error,msg=\"(During startup program exited with code 127|Running .* on the remote target failed)" {
    120 		set saw_mi_error 1
    121 		verbose -log "saw mi error"
    122 		if {!$saw_perm_error} {
    123 		    exp_continue
    124 		}
    125 	    }
    126 	    timeout {
    127 		set already_failed 1
    128 		fail "$test (timeout)"
    129 	    }
    130 	    -i "$gdb_main_spawn_id"
    131 	    eof {
    132 		set already_failed 1
    133 		fail "$test (eof)"
    134 	    }
    135 	}
    136 
    137 	if {$saw_perm_error && $saw_mi_error} {
    138 	    pass $test
    139 	} elseif {!$already_failed} {
    140 	    verbose -log "saw_perm_error=$saw_perm_error; saw_mi_error=$saw_mi_error"
    141 	    fail $test
    142 	}
    143     } else {
    144 	mi_run_cmd "--start"
    145 	mi_expect_stop "breakpoint-hit" "main" "" ".*$srcfile" "$decimal" \
    146 	    { "" "disp=\"del\"" } "breakpoint hit reported on mi"
    147 
    148 	if {$mi_mode == "separate"} {
    149 	    # Check that the breakpoint hit is reported on the main
    150 	    # UI/CLI.  Note no prompt is expected.
    151 	    switch_gdb_spawn_id $gdb_main_spawn_id
    152 
    153 	    set test "breakpoint hit reported on console"
    154 	    gdb_test_multiple "" $test {
    155 		-re "Temporary breakpoint .*, main \\(\\) at .*$srcfile:$decimal.*return 0;" {
    156 		    pass $test
    157 		}
    158 	    }
    159 
    160 	    # Switch back to the MI UI.
    161 	    global mi_spawn_id
    162 	    switch_gdb_spawn_id $mi_spawn_id
    163 	}
    164     }
    165 }
    166 
    167 # Create a not-executable copy of the program, in order to exercise
    168 # vfork->exec failing.
    169 gdb_remote_download host $binfile $binfile.nox
    170 remote_exec target "chmod \"a-x\" $binfile.nox"
    171 
    172 foreach_with_prefix inferior-tty {"main" "separate"} {
    173     foreach_with_prefix mi {"main" "separate"} {
    174 	foreach_with_prefix force-fail {0 1} {
    175 	    test ${inferior-tty} ${mi} ${force-fail}
    176 	}
    177     }
    178 }
    179 
    180 mi_gdb_exit
    181