]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/gdbinit-history.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / gdbinit-history.exp
1 # Copyright 2015-2024 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the gdb testsuite.
17
18 # Test the setting of "history size" via $HOME/.gdbinit
19
20 # This test depends on being able to set $HOME and $GDBHISTSIZE.
21
22 # Do not run if gdb debug is enabled - it interferes with the command history.
23 require !gdb_debug_enabled
24
25 # We cannot expect remote hosts to see environment variables set on the
26 # local machine.
27 require {!is_remote host}
28
29 # Check that the history size is properly set to SIZE when reading the .gdbinit
30 # file located in HOME with the environment variable GDBHISTSIZE optionally
31 # set to GDBHISTSIZE_VAL.
32
33 proc test_gdbinit_history_setting { home size { gdbhistsize_val "-" } } {
34 global env
35 global INTERNAL_GDBFLAGS
36 global srcdir
37 global subdir
38
39 save_vars { INTERNAL_GDBFLAGS env(GDBHISTFILE) env(GDBHISTSIZE) env(HOME) } {
40 set env(HOME) "$srcdir/$subdir/$home"
41
42 # These environment variables take precedence over whatever
43 # history size is set in .gdbinit. Make sure the former is not
44 # set.
45 unset -nocomplain env(GDBHISTFILE)
46 unset -nocomplain env(GDBHISTSIZE)
47
48 if { $gdbhistsize_val != "-" } {
49 set env(GDBHISTSIZE) $gdbhistsize_val
50 }
51
52 set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
53
54 set prefix "home=$home"
55 if { $gdbhistsize_val != "-" } {
56 append prefix " gdbhistsize=$gdbhistsize_val"
57 }
58
59 with_test_prefix $prefix {
60 gdb_exit
61 gdb_start
62
63 gdb_test "show history size" "The size of the command history is $size."
64
65 if { $size == "0" } {
66 gdb_test_no_output "show commands"
67 } elseif { $size != "1" } {
68 gdb_test "show commands" " . show history size\r\n . show commands"
69 }
70 }
71 }
72 }
73
74 # Check that the history file does not get truncated to zero when a gdbinit
75 # file sets the history size to unlimited.
76
77 proc test_no_truncation_of_unlimited_history_file { } {
78 global env
79 global INTERNAL_GDBFLAGS
80
81 save_vars { INTERNAL_GDBFLAGS env(GDBHISTFILE) env(GDBHISTSIZE) } {
82 # These environment variables take precedence over whatever
83 # history size is set in .gdbinit. Make sure the former is not
84 # set.
85 unset -nocomplain env(GDBHISTFILE)
86 unset -nocomplain env(GDBHISTSIZE)
87
88 set temp_gdbinit [standard_output_file "gdbinit-history.gdbinit"]
89 set temp_histfile [standard_output_file "gdbinit-history.gdb_history"]
90 file delete $temp_gdbinit
91 file delete $temp_histfile
92
93 set fd [open $temp_gdbinit "w"]
94 puts $fd "set history size unlimited\n"
95 puts $fd "set history filename $temp_histfile\n"
96 puts $fd "set history save\n"
97 close $fd
98
99 append INTERNAL_GDBFLAGS " -x $temp_gdbinit"
100
101 # We have to start then exit GDB twice: the first time to test the creation
102 # of the initial history file, and the second time to test appending to it.
103 # In either case the initial "print 1" command should persist through the
104 # history file.
105 with_test_prefix "truncation" {
106 gdb_exit
107 gdb_start
108 gdb_test "print 1"
109
110 with_test_prefix "creating" {
111 gdb_exit
112 gdb_start
113 gdb_test "server show commands" " . print 1.*"
114 }
115
116 with_test_prefix "appending" {
117 gdb_exit
118 gdb_start
119 gdb_test "server show commands" " . print 1.*"
120 }
121 }
122 }
123 }
124
125 # Check that the current command history matches HIST, which is a list
126 # of commands, oldest fist.
127 proc check_history { hist } {
128
129 # The show commands we issue here always appears last in the
130 # commands list.
131 lappend hist "show commands"
132
133 # Number all of the entries in the HIST list and convert the list
134 # into a pattern to match against GDB.
135 set hist_lines [list]
136 set idx 1
137 foreach h $hist {
138 lappend hist_lines " $idx $h"
139 incr idx
140 }
141 if { [llength $hist_lines] == 1 } {
142 set pattern [lindex $hist_lines 0]
143 } else {
144 set pattern [eval multi_line $hist_lines]
145 }
146
147 # Check the history.
148 gdb_test "show commands" "$pattern.*"
149 }
150
151 # Run 'show history filename' and check the output contains the
152 # filename matching PATTERN, unless, PATTERN is the empty string, in
153 # which case match a different output that GDB will give if the
154 # history filename is the empty string.
155 #
156 # TESTNAME is the name for the test, which defaults to the command run
157 # in the test.
158 proc check_history_filename { pattern {testname ""} } {
159
160 set cmd "show history filename"
161 if { $testname == "" } {
162 set testname $cmd
163 }
164
165 if { $pattern == "" } {
166 gdb_test $cmd \
167 "There is no filename currently set for recording the command history in." \
168 $testname
169 } else {
170 gdb_test $cmd \
171 "The filename in which to record the command history is \"$pattern\"\." \
172 $testname
173 }
174 }
175
176 # Tests for how GDB handles setting the history filename to the empty
177 # string.
178 proc test_empty_history_filename { } {
179 global env
180 global gdb_prompt
181
182 set common_history [list "set height 0" "set width 0"]
183
184 set test_dir [standard_output_file history_test]
185 remote_exec host "mkdir -p $test_dir"
186 foreach entry { { ".gdb_history" "xxxxx" } \
187 { "_gdb_history" "xxxxx" } \
188 { "alt_history" "yyyyy" } } {
189 set fn [lindex $entry 0]
190 set content [lindex $entry 1]
191 set fd [open [standard_output_file "$test_dir/$fn"] w]
192 puts $fd "$content"
193 close $fd
194 }
195
196 with_cwd "$test_dir" {
197 with_test_prefix "load default history file" {
198 # Start GDB and see that the history file was loaded
199 # correctly.
200 gdb_exit
201 gdb_start
202 check_history [concat "xxxxx" $common_history]
203 check_history_filename ".*/.gdb_history"
204 }
205
206 with_test_prefix "load GDBHISTFILE history file" {
207 # Now restart GDB with GDBHISTFILE set to see that the
208 # "other" history file is loaded.
209 save_vars { env(GDBHISTFILE) } {
210 setenv GDBHISTFILE \
211 "$test_dir/alt_history"
212 gdb_exit
213 gdb_start
214 check_history [concat "yyyyy" $common_history]
215 check_history_filename ".*/alt_history"
216 }
217 }
218
219 with_test_prefix "GDBHISTFILE is empty" {
220 # Now restart GDB with GDBHISTFILE set to indicate don't
221 # load any history file, check none was loaded.
222 save_vars { env(GDBHISTFILE) } {
223 setenv GDBHISTFILE ""
224 gdb_exit
225 gdb_start
226 check_history $common_history
227 check_history_filename ""
228 }
229
230 # Check that 'show history save' does the right thing when
231 # the history filename is the empty string.
232 gdb_test_no_output "set history save off" \
233 "ensure history save is off initially"
234 gdb_test "show history save" \
235 "Saving of the history record on exit is off." \
236 "Check history save is off"
237 gdb_test_no_output "set history save on"
238 gdb_test "show history save" \
239 "Saving of the history is disabled due to the value of 'history filename'." \
240 "Check history save is off due to filename"
241 gdb_test_no_output \
242 "set history filename $test_dir/alt_history" \
243 "set history filename at the command line"
244 check_history_filename ".*/alt_history" \
245 "check filename after setting at the command line"
246 gdb_test "show history save" \
247 "Saving of the history record on exit is on." \
248 "Check history save is on"
249 gdb_test_no_output "set history filename"
250 gdb_test "show history save" \
251 "Saving of the history is disabled due to the value of 'history filename'." \
252 "Check history save is off due to filename again"
253 gdb_test_no_output "set history save off"
254 }
255
256 with_test_prefix "Use -ex to clear history file" {
257 # Now restart GDB with the command line '-ex' to indicate
258 # no history file should be loaded.
259 gdb_exit
260 if {[gdb_spawn_with_cmdline_opts \
261 "-ex \"set history filename\""] != 0} {
262 fail "spawn"
263 return
264 }
265 set test "initial prompt"
266 gdb_test_multiple "" $test {
267 -re ".*$gdb_prompt $" {
268 pass "$test"
269 }
270 }
271 check_history [list]
272 check_history_filename ""
273 }
274 }
275 }
276
277 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited"
278 test_gdbinit_history_setting "gdbinit-history/zero" "0"
279
280 test_no_truncation_of_unlimited_history_file
281
282 # A valid GDBHISTSIZE value overrides the setting inside the .gdbinit file; an
283 # invalid GDBHISTSIZE value is ignored, falling back on the setting inside the
284 # .gdbinit file.
285 test_gdbinit_history_setting "gdbinit-history/unlimited" "1000" "1000"
286 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited" "foo"
287
288 # Check handling of empty history filename.
289 test_empty_history_filename