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