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