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