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