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