aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/set-cwd.exp
blob: b4386be2cd5930721ab659ff7d2bd2068e275b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# This testcase is part of GDB, the GNU debugger.

# Copyright 2017-2019 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if { [use_gdb_stub] } {
    untested "skipping tests due to use_gdb_stub"
    return
}

standard_testfile

if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
    return -1
}

# Test that tilde expansion works fine.

proc_with_prefix test_tilde_expansion { } {
    global decimal gdb_prompt hex

    gdb_test_no_output "set cwd ~/" "set inferior cwd to ~/ dir"

    if { ![runto_main] } {
	untested "could not run to main"
	return -1
    }

    gdb_breakpoint [gdb_get_line_number "break-here"]
    gdb_continue_to_breakpoint "break-here" ".* break-here .*"

    set home ""
    set test "print home var"
    gdb_test_multiple "print home" $test {
    	-re "\\\$$decimal = $hex \"\(.+\)\"\r\n$gdb_prompt $" {
    	    set home $expect_out(1,string)
    	    pass $test
    	}
    }

    if { $home == "" } {
    	untested "could not retrieve home var"
    	return
    }

    set curdir ""
    set test "print dir var"
    gdb_test_multiple "print dir" $test {
	-re "\\\$$decimal = \"\(.+\)\"\(, .*repeats.*\)?\r\n$gdb_prompt $" {
	    set curdir $expect_out(1,string)
	    pass $test
	}
    }

    if { $curdir == "" } {
	untested "could not retrieve dir var"
	return
    }

    gdb_assert [string equal $curdir $home] \
	"successfully chdir'd into home"
}

# The temporary directory that we will use to start the inferior.
set tmpdir [standard_output_file ""]

# Test that when we "set cwd" the inferior will be started under the
# correct working directory and GDB will not be affected by this.

proc_with_prefix test_cd_into_dir { } {
    global decimal gdb_prompt tmpdir

    set gdb_cwd_before_run ""
    set test "pwd before run"
    gdb_test_multiple "pwd" $test {
	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
	    set gdb_cwd_before_run $expect_out(1,string)
	    pass $test
	}
    }

    if { $gdb_cwd_before_run == "" } {
	untested "could not obtain GDB cwd before run"
	return
    }

    # This test only makes sense if $tmpdir != $gdb_cwd_before_run
    if { ![gdb_assert ![string equal $tmpdir $gdb_cwd_before_run] \
	       "make sure that tmpdir and GDB's cwd are different"] } {
	return -1
    }

    gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir"

    if { ![runto_main] } {
	untested "could not run to main"
	return -1
    }

    gdb_breakpoint [gdb_get_line_number "break-here"]
    gdb_continue_to_breakpoint "break-here" ".* break-here .*"

    gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \
	"inferior cwd is correctly set"

    set gdb_cwd_after_run ""
    set test "pwd after run"
    gdb_test_multiple "pwd" $test {
	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
	    set gdb_cwd_after_run $expect_out(1,string)
	    pass $test
	}
    }

    if { $gdb_cwd_after_run == "" } {
	untested "could not obtain GDB cwd after run"
	return
    }

    gdb_assert [string equal $gdb_cwd_before_run $gdb_cwd_after_run] \
	"GDB cwd is unchanged after running inferior"
}

# Test that executing "set cwd" without arguments will reset the
# inferior's cwd setting to its previous state.

proc_with_prefix test_cwd_reset { } {
    global decimal gdb_prompt tmpdir

    set gdb_cwd ""
    set test "GDB cwd"
    gdb_test_multiple "pwd" $test {
	-re "Working directory \(.*\)\.\r\n$gdb_prompt $" {
	    set gdb_cwd $expect_out(1,string)
	}
    }

    if { $gdb_cwd == "" } {
	untested "could not obtain GDB cwd"
	return
    }

    # This test only makes sense if $tmpdir != $gdb_cwd.
    if { ![gdb_assert ![string equal $tmpdir $gdb_cwd] \
	       "make sure that tmpdir and GDB's cwd are different"] } {
	return -1
    }

    gdb_test_no_output "set cwd $tmpdir" "set inferior cwd to temp dir"

    with_test_prefix "running with set cwd" {
	if { ![runto_main] } {
	    untested "could not run to main"
	    return -1
	}
    }

    gdb_breakpoint [gdb_get_line_number "break-here"]
    gdb_continue_to_breakpoint "break-here" ".* break-here .*"

    gdb_test "print dir" "\\\$$decimal = \"$tmpdir\", .*" \
	"inferior cwd is correctly set"

    # Reset the inferior's cwd.
    gdb_test_no_output "set cwd" "resetting inferior cwd"

    with_test_prefix "running without set cwd" {
	if { ![runto_main] } {
	    untested "could not run to main"
	    return -1
	}
    }

    gdb_breakpoint [gdb_get_line_number "break-here"]
    gdb_continue_to_breakpoint "break-here" ".* break-here .*"

    gdb_test "print dir" "\\\$$decimal = \"$gdb_cwd\", .*" \
	"inferior cwd got reset correctly"
}

test_cd_into_dir
clean_restart $binfile
test_tilde_expansion
clean_restart $binfile
test_cwd_reset