1# Copyright 2020 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 running an inferior with arguments. 17 18# This does not work on boards that don't support inferior arguments. 19if [target_info exists noargs] then { 20 verbose "skipping gdb.base/inferior-args.exp because of noargs" 21 return 22} 23 24standard_testfile .c 25 26if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} { 27 return 28} 29 30proc do_test { method } { 31 global binfile hex 32 33 # The second arg is an empty string on purpose. 34 set inferior_args { "first arg" "" "third-arg" } 35 36 clean_restart $binfile 37 38 if { $method == "start" } { 39 # The start command does not make sense for a stub. 40 if { [use_gdb_stub] } { 41 return; 42 } 43 44 if { [gdb_start_cmd $inferior_args] < 0 } { 45 fail "could not issue start command" 46 return -1 47 } 48 49 # Consume up to the GDB prompt after the stop. 50 gdb_test "" ".*main.*" "stop at main" 51 52 } elseif { $method == "starti" } { 53 # The starti command does not make sense for a stub. 54 if { [use_gdb_stub] } { 55 return; 56 } 57 58 if { [gdb_starti_cmd $inferior_args] < 0 } { 59 fail "could not issue start command" 60 return -1 61 } 62 63 # Consume up to the GDB prompt after the stop. 64 gdb_test "" "" "stop at first instruction" 65 66 # Put a breakpoint and continue until main. 67 if { ![gdb_breakpoint "main" message] } { 68 fail "could not set breakpoint on main" 69 return -1 70 } 71 72 if { [gdb_continue "main"] != 0 } { 73 fail "could not continue to main" 74 return -1 75 } 76 77 } elseif { $method == "run" } { 78 if { ![gdb_breakpoint "main" message] } { 79 fail "could not set breakpoint on main" 80 return -1 81 } 82 83 # The run command does not make sense for a stub, but GDB_RUN_CMD 84 # does the right thing when the target is a stub (start the stub, 85 # connect to it, and "continue"). 86 # 87 # This allows us to test arguments passed on the gdbserver command 88 # line. 89 if { [gdb_run_cmd $inferior_args] < 0 } { 90 fail "could not run" 91 return -1 92 } 93 94 # Consume up to the GDB prompt after the stop. 95 gdb_test "" ".*main.*" "stop at main" 96 97 } elseif { $method == "set args" } { 98 # Using "set args" does not make sense with a stub. 99 if { [use_gdb_stub] } { 100 return; 101 } 102 103 gdb_test_no_output "set args $inferior_args" 104 105 if { ![runto_main] } { 106 fail "could not run to main" 107 return -1 108 } 109 110 } else { 111 error "invalid method $method" 112 } 113 114 # Now that we are stopped at main, inspect argc/argv. 115 gdb_test "print argc" " = 4" 116 gdb_test "print argv\[0\]" " = $hex \".*\"" 117 gdb_test "print argv\[1\]" " = $hex \"first arg\"" 118 gdb_test "print argv\[2\]" " = $hex \"\"" 119 gdb_test "print argv\[3\]" " = $hex \"third-arg\"" 120} 121 122foreach_with_prefix method { "start" "starti" "run" "set args" } { 123 do_test $method 124} 125