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