1 # This testcase is part of GDB, the GNU debugger. 2 3 # Copyright 2017-2024 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 18 require !use_gdb_stub 19 20 standard_testfile 21 22 if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } { 23 return -1 24 } 25 26 # Test that tilde expansion works fine. 27 28 proc_with_prefix test_tilde_expansion { } { 29 global decimal gdb_prompt hex 30 31 gdb_test_no_output "set cwd ~/" "set inferior cwd to ~/ dir" 32 33 if { ![runto_main] } { 34 return -1 35 } 36 37 gdb_breakpoint [gdb_get_line_number "break-here"] 38 gdb_continue_to_breakpoint "break-here" ".* break-here .*" 39 40 set home "" 41 set test "print home var" 42 gdb_test_multiple "print home" $test { 43 -re "\\\$$decimal = $hex \"\(.+\)\"\r\n$gdb_prompt $" { 44 set home $expect_out(1,string) 45 pass $test 46 } 47 } 48 49 if { $home == "" } { 50 untested "could not retrieve home var" 51 return 52 } 53 54 set curdir "" 55 set test "print dir var" 56 gdb_test_multiple "print dir" $test { 57 -re "\\\$$decimal = \"\(.+\)\"\(, .*repeats.*\)?\r\n$gdb_prompt $" { 58 set curdir $expect_out(1,string) 59 pass $test 60 } 61 } 62 63 if { $curdir == "" } { 64 untested "could not retrieve dir var" 65 return 66 } 67 68 gdb_assert [string equal $curdir $home] \ 69 "successfully chdir'd into home" 70 } 71 72 # The temporary directory that we will use to start the inferior. 73 set tmpdir [standard_output_file ""] 74 75 # Test that when we "set cwd" the inferior will be started under the 76 # correct working directory and GDB will not be affected by this. 77 78 proc_with_prefix test_cd_into_dir { } { 79 global decimal gdb_prompt tmpdir 80 81 set gdb_cwd_before_run "" 82 set test "pwd before run" 83 gdb_test_multiple "pwd" $test { 84 -re "Working directory \(.*\)\.\r\n$gdb_prompt $" { 85 set gdb_cwd_before_run $expect_out(1,string) 86 pass $test 87 } 88 } 89 90 if { $gdb_cwd_before_run == "" } { 91 untested "could not obtain GDB cwd before run" 92 return 93 } 94 95 # This test only makes sense if $tmpdir != $gdb_cwd_before_run 96 if { ![gdb_assert ![string equal $tmpdir $gdb_cwd_before_run] \ 97 "make sure that tmpdir and GDB's cwd are different"] } { 98 return -1 99 } 100 101 gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir" 102 103 if { ![runto_main] } { 104 return -1 105 } 106 107 gdb_breakpoint [gdb_get_line_number "break-here"] 108 gdb_continue_to_breakpoint "break-here" ".* break-here .*" 109 110 gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \ 111 "inferior cwd is correctly set" 112 113 set gdb_cwd_after_run "" 114 set test "pwd after run" 115 gdb_test_multiple "pwd" $test { 116 -re "Working directory \(.*\)\.\r\n$gdb_prompt $" { 117 set gdb_cwd_after_run $expect_out(1,string) 118 pass $test 119 } 120 } 121 122 if { $gdb_cwd_after_run == "" } { 123 untested "could not obtain GDB cwd after run" 124 return 125 } 126 127 gdb_assert [string equal $gdb_cwd_before_run $gdb_cwd_after_run] \ 128 "GDB cwd is unchanged after running inferior" 129 } 130 131 # Test that executing "set cwd" without arguments will reset the 132 # inferior's cwd setting to its previous state. 133 134 proc_with_prefix test_cwd_reset { } { 135 global decimal gdb_prompt tmpdir 136 137 set gdb_cwd "" 138 set test "GDB cwd" 139 gdb_test_multiple "pwd" $test { 140 -re "Working directory \(.*\)\.\r\n$gdb_prompt $" { 141 set gdb_cwd $expect_out(1,string) 142 } 143 } 144 145 if { $gdb_cwd == "" } { 146 untested "could not obtain GDB cwd" 147 return 148 } 149 150 # This test only makes sense if $tmpdir != $gdb_cwd. 151 if { ![gdb_assert ![string equal $tmpdir $gdb_cwd] \ 152 "make sure that tmpdir and GDB's cwd are different"] } { 153 return -1 154 } 155 156 gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir" 157 158 with_test_prefix "running with set cwd" { 159 if { ![runto_main] } { 160 return -1 161 } 162 } 163 164 gdb_breakpoint [gdb_get_line_number "break-here"] 165 gdb_continue_to_breakpoint "break-here" ".* break-here .*" 166 167 gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \ 168 "inferior cwd is correctly set" 169 170 # Reset the inferior's cwd. 171 gdb_test_no_output "set cwd" "resetting inferior cwd" 172 173 with_test_prefix "running without set cwd" { 174 if { ![runto_main] } { 175 return -1 176 } 177 178 gdb_breakpoint [gdb_get_line_number "break-here"] 179 gdb_continue_to_breakpoint "break-here" ".* break-here .*" 180 181 gdb_test "print dir" "\\\$$decimal = \"$gdb_cwd\", .*" \ 182 "inferior cwd got reset correctly" 183 } 184 } 185 186 test_cd_into_dir 187 clean_restart $binfile 188 test_tilde_expansion 189 clean_restart $binfile 190 test_cwd_reset 191