1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2017-2020 Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18if { [use_gdb_stub] } {
19    untested "skipping tests due to use_gdb_stub"
20    return
21}
22
23standard_testfile
24
25if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
26    return -1
27}
28
29# Test that tilde expansion works fine.
30
31proc_with_prefix test_tilde_expansion { } {
32    global decimal gdb_prompt hex
33
34    gdb_test_no_output "set cwd ~/" "set inferior cwd to ~/ dir"
35
36    if { ![runto_main] } {
37	untested "could not run to main"
38	return -1
39    }
40
41    gdb_breakpoint [gdb_get_line_number "break-here"]
42    gdb_continue_to_breakpoint "break-here" ".* break-here .*"
43
44    set home ""
45    set test "print home var"
46    gdb_test_multiple "print home" $test {
47    	-re "\\\$$decimal = $hex \"\(.+\)\"\r\n$gdb_prompt $" {
48    	    set home $expect_out(1,string)
49    	    pass $test
50    	}
51    }
52
53    if { $home == "" } {
54    	untested "could not retrieve home var"
55    	return
56    }
57
58    set curdir ""
59    set test "print dir var"
60    gdb_test_multiple "print dir" $test {
61	-re "\\\$$decimal = \"\(.+\)\"\(, .*repeats.*\)?\r\n$gdb_prompt $" {
62	    set curdir $expect_out(1,string)
63	    pass $test
64	}
65    }
66
67    if { $curdir == "" } {
68	untested "could not retrieve dir var"
69	return
70    }
71
72    gdb_assert [string equal $curdir $home] \
73	"successfully chdir'd into home"
74}
75
76# The temporary directory that we will use to start the inferior.
77set tmpdir [standard_output_file ""]
78
79# Test that when we "set cwd" the inferior will be started under the
80# correct working directory and GDB will not be affected by this.
81
82proc_with_prefix test_cd_into_dir { } {
83    global decimal gdb_prompt tmpdir
84
85    set gdb_cwd_before_run ""
86    set test "pwd before run"
87    gdb_test_multiple "pwd" $test {
88	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
89	    set gdb_cwd_before_run $expect_out(1,string)
90	    pass $test
91	}
92    }
93
94    if { $gdb_cwd_before_run == "" } {
95	untested "could not obtain GDB cwd before run"
96	return
97    }
98
99    # This test only makes sense if $tmpdir != $gdb_cwd_before_run
100    if { ![gdb_assert ![string equal $tmpdir $gdb_cwd_before_run] \
101	       "make sure that tmpdir and GDB's cwd are different"] } {
102	return -1
103    }
104
105    gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir"
106
107    if { ![runto_main] } {
108	untested "could not run to main"
109	return -1
110    }
111
112    gdb_breakpoint [gdb_get_line_number "break-here"]
113    gdb_continue_to_breakpoint "break-here" ".* break-here .*"
114
115    gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \
116	"inferior cwd is correctly set"
117
118    set gdb_cwd_after_run ""
119    set test "pwd after run"
120    gdb_test_multiple "pwd" $test {
121	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
122	    set gdb_cwd_after_run $expect_out(1,string)
123	    pass $test
124	}
125    }
126
127    if { $gdb_cwd_after_run == "" } {
128	untested "could not obtain GDB cwd after run"
129	return
130    }
131
132    gdb_assert [string equal $gdb_cwd_before_run $gdb_cwd_after_run] \
133	"GDB cwd is unchanged after running inferior"
134}
135
136# Test that executing "set cwd" without arguments will reset the
137# inferior's cwd setting to its previous state.
138
139proc_with_prefix test_cwd_reset { } {
140    global decimal gdb_prompt tmpdir
141
142    set gdb_cwd ""
143    set test "GDB cwd"
144    gdb_test_multiple "pwd" $test {
145	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
146	    set gdb_cwd $expect_out(1,string)
147	}
148    }
149
150    if { $gdb_cwd == "" } {
151	untested "could not obtain GDB cwd"
152	return
153    }
154
155    # This test only makes sense if $tmpdir != $gdb_cwd.
156    if { ![gdb_assert ![string equal $tmpdir $gdb_cwd] \
157	       "make sure that tmpdir and GDB's cwd are different"] } {
158	return -1
159    }
160
161    gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir"
162
163    with_test_prefix "running with set cwd" {
164	if { ![runto_main] } {
165	    untested "could not run to main"
166	    return -1
167	}
168    }
169
170    gdb_breakpoint [gdb_get_line_number "break-here"]
171    gdb_continue_to_breakpoint "break-here" ".* break-here .*"
172
173    gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \
174	"inferior cwd is correctly set"
175
176    # Reset the inferior's cwd.
177    gdb_test_no_output "set cwd" "resetting inferior cwd"
178
179    with_test_prefix "running without set cwd" {
180	if { ![runto_main] } {
181	    untested "could not run to main"
182	    return -1
183	}
184    }
185
186    gdb_breakpoint [gdb_get_line_number "break-here"]
187    gdb_continue_to_breakpoint "break-here" ".* break-here .*"
188
189    gdb_test "print dir" "\\\$$decimal = \"$gdb_cwd\", .*" \
190	"inferior cwd got reset correctly"
191}
192
193test_cd_into_dir
194clean_restart $binfile
195test_tilde_expansion
196clean_restart $binfile
197test_cwd_reset
198