Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.base / gdbinit-history.exp
blobb4f0d1b6eef23dc45ed1a1d70f1f30ad224bbd0d
1 # Copyright 2015-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the gdb testsuite.
18 # Test the setting of "history size" via $HOME/.gdbinit
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.
24 # Do not run if gdb debug is enabled - it interferes with the command history.
25 if [gdb_debug_enabled] {
26     untested "debug is enabled"
27     return 0
30 if { [is_remote host] } {
31     unsupported "can't set environment variables on remote host"
32     return -1
35 # Check that the history size is properly set to SIZE when reading the .gdbinit
36 # file located in HOME with the environment variable GDBHISTSIZE optionally
37 # set to GDBHISTSIZE_VAL.
39 proc test_gdbinit_history_setting { home size { gdbhistsize_val "-" } } {
40     global env
41     global INTERNAL_GDBFLAGS
42     global srcdir
43     global subdir
45     save_vars { INTERNAL_GDBFLAGS env(GDBHISTFILE) env(GDBHISTSIZE) env(HOME) } {
46         set env(HOME) "$srcdir/$subdir/$home"
48         # These environment variables take precedence over whatever
49         # history size is set in .gdbinit.  Make sure the former is not
50         # set.
51         unset -nocomplain env(GDBHISTFILE)
52         unset -nocomplain env(GDBHISTSIZE)
54         if { $gdbhistsize_val != "-" } {
55             set env(GDBHISTSIZE) $gdbhistsize_val
56         }
58         set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
60         set prefix "home=$home"
61         if { $gdbhistsize_val != "-" } {
62             append prefix " gdbhistsize=$gdbhistsize_val"
63         }
65         with_test_prefix $prefix {
66             gdb_exit
67             gdb_start
69             gdb_test "show history size" "The size of the command history is $size."
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             }
76         }
77     }
80 # Check that the history file does not get truncated to zero when a gdbinit
81 # file sets the history size to unlimited.
83 proc test_no_truncation_of_unlimited_history_file { } {
84     global env
85     global INTERNAL_GDBFLAGS
87     save_vars { INTERNAL_GDBFLAGS env(GDBHISTFILE) env(GDBHISTSIZE) } {
88         # These environment variables take precedence over whatever
89         # history size is set in .gdbinit.  Make sure the former is not
90         # set.
91         unset -nocomplain env(GDBHISTFILE)
92         unset -nocomplain env(GDBHISTSIZE)
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
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
105         append INTERNAL_GDBFLAGS " -x $temp_gdbinit"
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"
116             with_test_prefix "creating" {
117                 gdb_exit
118                 gdb_start
119                 gdb_test "server show commands" "    .  print 1.*"
120             }
122             with_test_prefix "appending" {
123                 gdb_exit
124                 gdb_start
125                 gdb_test "server show commands" "    .  print 1.*"
126             }
127         }
128     }
131 # Check that the current command history matches HIST, which is a list
132 # of commands, oldest fist.
133 proc check_history { hist } {
135     # The show commands we issue here always appears last in the
136     # commands list.
137     lappend hist "show commands"
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     }
147     if { [llength $hist_lines] == 1 } {
148         set pattern [lindex $hist_lines 0]
149     } else {
150         set pattern [eval multi_line $hist_lines]
151     }
153     # Check the history.
154     gdb_test "show commands" "$pattern.*"
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.
162 # TESTNAME is the name for the test, which defaults to the command run
163 # in the test.
164 proc check_history_filename { pattern {testname ""} } {
166     set cmd "show history filename"
167     if { $testname == "" } {
168         set testname $cmd
169     }
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     }
182 # Tests for how GDB handles setting the history filename to the empty
183 # string.
184 proc test_empty_history_filename { } {
185     global env
186     global gdb_prompt
188     set common_history [list "set height 0" "set width 0"]
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     }
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         }
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         }
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             }
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         }
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     }
283 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited"
284 test_gdbinit_history_setting "gdbinit-history/zero" "0"
286 test_no_truncation_of_unlimited_history_file
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.
291 test_gdbinit_history_setting "gdbinit-history/unlimited" "1000" "1000"
292 test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited" "foo"
294 # Check handling of empty history filename.
295 test_empty_history_filename