1# Copyright 2011-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# This file was written by Pierre Muller <muller@ics.u-strasbg.fr> 17# 18# Check if environment variables are correctly passed to inferiors 19# 20 21# Can't pass environment variables to the inferior if when we connect, 22# the inferior is already running. 23require !use_gdb_stub 24 25standard_testfile .c 26 27# Compile binary 28# and start with a fresh gdb 29 30if { [prepare_for_testing "failed to prepare" ${binfile} ${srcfile}] } { 31 return -1 32} 33 34# Test that the the inferior sees EXPECTED env vars starting with 35# "TEST_GDB". 36proc test_num_test_vars {expected message} { 37 set num [get_integer_valueof "j" -1 "$message, get num vars"] 38 gdb_assert {$num == $expected} "$message, confirmed" 39} 40 41set bp_line [gdb_get_line_number "set breakpoint here"] 42gdb_breakpoint $bp_line 43 44# Restart test program, and prepare for another test sequence. 45# Returns true on success. 46proc run_and_count_vars {} { 47 global srcfile bp_line 48 49 return [runto "$srcfile:$bp_line"] 50} 51 52# Find environment variable named VARNAME (peeking inferior variables 53# directly), and return its value. Returns "<not found>" if not 54# found. 55 56proc find_env {varname} { 57 global gdb_prompt 58 59 for {set i 0} {1} {incr i} { 60 set test "printf \"var: %s\\n\", envp\[$i\] ? envp\[$i\] : \"\"" 61 set var "" 62 gdb_test_multiple $test $test { 63 -re "var: \r\n$gdb_prompt $" { 64 return "<not found>" 65 } 66 -re "var: \(\[^\r\n\]*\)\r\n$gdb_prompt $" { 67 set var $expect_out(1,string) 68 } 69 -re "$gdb_prompt $" { 70 # If this fails, bail out, otherwise we get stuck in 71 # an infinite loop. The caller will end up emiting a 72 # FAIL. 73 return "<fail>" 74 } 75 } 76 77 if {[string match "$varname=*" $var]} { 78 set from [expr [string first "=" $var] + 1] 79 set to [string length $var] 80 return [string range $var $from $to] 81 } 82 } 83} 84 85# 86# Test gdb set/unset environment commands. 87# The executable lists and counts all environment variables 88# starting with TEST_GDB. 89 90proc_with_prefix test_set_unset_env {} { 91 global binfile 92 93 clean_restart $binfile 94 95 # First test with no TEST_GDB_VAR. 96 with_test_prefix "test1" { 97 if ![run_and_count_vars] { 98 return 99 } 100 test_num_test_vars 0 "no TEST_GDB vars" 101 } 102 103 # Second test with one TEST_GDB_VAR. 104 with_test_prefix "test2" { 105 gdb_test_no_output "set env TEST_GDB_VAR1 test1" \ 106 "set TEST_GDB_VAR1" 107 108 if ![run_and_count_vars] { 109 return 110 } 111 test_num_test_vars 1 "one TEST_GDB var" 112 } 113 114 # Third test with two TEST_GDB_VAR. 115 with_test_prefix "test3" { 116 gdb_test_no_output "set env TEST_GDB_VAR2 test2" \ 117 "set TEST_GDB_VAR2" 118 119 if ![run_and_count_vars] { 120 return 121 } 122 123 test_num_test_vars 2 "two TEST_GDB var" 124 } 125 126 # Fourth test with one TEST_GDB_VAR left, after one was removed 127 # with unset command. 128 with_test_prefix "test4" { 129 gdb_test_no_output "unset env TEST_GDB_VAR1" \ 130 "unset TEST_GDB_VAR1" 131 132 if ![run_and_count_vars] { 133 return 134 } 135 136 test_num_test_vars 1 "one TEST_GDB var, after unset" 137 } 138} 139 140proc_with_prefix test_inherit_env_var {} { 141 global binfile 142 global bp_line 143 global env 144 145 # This test assumes that the build's environ (where dejagnu runs) 146 # is the same as the host's (where gdb runs) environ. 147 if [is_remote host] { 148 return 149 } 150 151 save_vars {env(TEST_GDB_GLOBAL)} { 152 set env(TEST_GDB_GLOBAL) "Global environment value" 153 154 clean_restart $binfile 155 156 gdb_breakpoint $bp_line 157 158 # First test with only inherited TEST_GDB_GLOBAL. 159 with_test_prefix "test1" { 160 if ![run_and_count_vars] { 161 return 162 } 163 164 gdb_test "show env" ".*TEST_GDB_GLOBAL=.*" \ 165 "test passing TEST_GDB_GLOBAL to GDB" 166 167 test_num_test_vars 1 "TEST_GDB_GLOBAL" 168 169 set var [find_env "TEST_GDB_GLOBAL"] 170 171 gdb_assert {[string equal $var "Global environment value"]} \ 172 "TEST_GDB_GLOBAL found with right value" 173 } 174 175 # Second test with one TEST_GDB_VAR. 176 with_test_prefix "test2" { 177 gdb_test_no_output "unset env TEST_GDB_GLOBAL" \ 178 "unset TEST_GDB_GLOBAL" 179 180 if ![run_and_count_vars] { 181 return 182 } 183 184 test_num_test_vars 0 "TEST_GDB_GLOBAL is unset" 185 } 186 } 187} 188 189test_set_unset_env 190test_inherit_env_var 191