xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/gdbinit-history.exp (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1# Copyright 2015-2020 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# We cannot expect remote hosts to see environment variables set on the
22# local machine.
23
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
30if { [is_remote host] } {
31    unsupported "can't set environment variables on remote host"
32    return -1
33}
34
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.
38
39proc test_gdbinit_history_setting { home size { gdbhistsize_val "-" } } {
40    global env
41    global INTERNAL_GDBFLAGS
42    global srcdir
43    global subdir
44
45    save_vars { INTERNAL_GDBFLAGS env(GDBHISTFILE) env(GDBHISTSIZE) env(HOME) } {
46	set env(HOME) "$srcdir/$subdir/$home"
47
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)
53
54	if { $gdbhistsize_val != "-" } {
55	    set env(GDBHISTSIZE) $gdbhistsize_val
56	}
57
58	set INTERNAL_GDBFLAGS [string map {"-nx" ""} $INTERNAL_GDBFLAGS]
59
60	set prefix "home=$home"
61	if { $gdbhistsize_val != "-" } {
62	    append prefix " gdbhistsize=$gdbhistsize_val"
63	}
64
65	with_test_prefix $prefix {
66	    gdb_exit
67	    gdb_start
68
69	    gdb_test "show history size" "The size of the command history is $size."
70
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    }
78}
79
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
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)
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" {
117		gdb_exit
118		gdb_start
119		gdb_test "server show commands" "    .  print 1.*"
120	    }
121
122	    with_test_prefix "appending" {
123		gdb_exit
124		gdb_start
125		gdb_test "server show commands" "    .  print 1.*"
126	    }
127        }
128    }
129}
130
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    }
147    set pattern [eval multi_line $hist_lines]
148
149    # Check the history.
150    gdb_test "show commands" "$pattern.*"
151}
152
153# Run 'show history filename' and check the output contains the
154# filename matching PATTERN, unless, PATTERN is the empty string, in
155# which case match a different output that GDB will give if the
156# history filename is the empty string.
157#
158# TESTNAME is the name for the test, which defaults to the command run
159# in the test.
160proc check_history_filename { pattern {testname ""} } {
161
162    set cmd "show history filename"
163    if { $testname == "" } {
164	set testname $cmd
165    }
166
167    if { $pattern == "" } {
168	gdb_test $cmd \
169	    "There is no filename currently set for recording the command history in." \
170	    $testname
171    } else {
172	gdb_test $cmd \
173	    "The filename in which to record the command history is \"$pattern\"\." \
174	    $testname
175    }
176}
177
178# Tests for how GDB handles setting the history filename to the empty
179# string.
180proc test_empty_history_filename { } {
181    global env
182    global gdb_prompt
183
184    set common_history [list "set height 0" "set width 0"]
185
186    set test_dir [standard_output_file history_test]
187    remote_exec host "mkdir -p $test_dir"
188    foreach entry { { ".gdb_history" "xxxxx" } \
189			{ "_gdb_history" "xxxxx" } \
190			{ "alt_history" "yyyyy" } } {
191	set fn [lindex $entry 0]
192	set content [lindex $entry 1]
193	set fd [open [standard_output_file "$test_dir/$fn"] w]
194	puts $fd "$content"
195	close $fd
196    }
197
198    with_cwd "$test_dir" {
199	with_test_prefix "load default history file" {
200	    # Start GDB and see that the history file was loaded
201	    # correctly.
202	    gdb_exit
203	    gdb_start
204	    check_history [concat "xxxxx" $common_history]
205	    check_history_filename ".*/.gdb_history"
206	}
207
208	with_test_prefix "load GDBHISTFILE history file" {
209	    # Now restart GDB with GDBHISTFILE set to see that the
210	    # "other" history file is loaded.
211	    save_vars { env(GDBHISTFILE) } {
212		setenv GDBHISTFILE \
213		    "$test_dir/alt_history"
214		gdb_exit
215		gdb_start
216		check_history [concat "yyyyy" $common_history]
217		check_history_filename ".*/alt_history"
218	    }
219	}
220
221	with_test_prefix "GDBHISTFILE is empty" {
222	    # Now restart GDB with GDBHISTFILE set to indicate don't
223	    # load any history file, check none was loaded.
224	    save_vars { env(GDBHISTFILE) } {
225	    setenv GDBHISTFILE ""
226		gdb_exit
227		gdb_start
228		check_history $common_history
229		check_history_filename ""
230	    }
231
232	    # Check that 'show history save' does the right thing when
233	    # the history filename is the empty string.
234	    gdb_test_no_output "set history save off" \
235		"ensure history save is off initially"
236	    gdb_test "show history save" \
237		"Saving of the history record on exit is off." \
238		"Check history save is off"
239	    gdb_test_no_output "set history save on"
240	    gdb_test "show history save" \
241		"Saving of the history is disabled due to the value of 'history filename'." \
242		"Check history save is off due to filename"
243	    gdb_test_no_output \
244		"set history filename $test_dir/alt_history" \
245		"set history filename at the command line"
246	    check_history_filename ".*/alt_history" \
247		"check filename after setting at the command line"
248	    gdb_test "show history save" \
249		"Saving of the history record on exit is on." \
250		"Check history save is on"
251	    gdb_test_no_output "set history filename"
252	    gdb_test "show history save" \
253		"Saving of the history is disabled due to the value of 'history filename'." \
254		"Check history save is off due to filename again"
255	    gdb_test_no_output "set history save off"
256	}
257
258	with_test_prefix "Use -ex to clear history file" {
259	    # Now restart GDB with the command line '-ex' to indicate
260	    # no history file should be loaded.
261	    gdb_exit
262	    if {[gdb_spawn_with_cmdline_opts \
263		     "-ex \"set history filename\""] != 0} {
264		fail "spawn"
265		return
266	    }
267	    set test "initial prompt"
268	    gdb_test_multiple "" $test {
269		-re ".*$gdb_prompt $" {
270		    pass "$test"
271		}
272	    }
273	    check_history [list]
274	    check_history_filename ""
275	}
276    }
277}
278
279test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited"
280test_gdbinit_history_setting "gdbinit-history/zero" "0"
281
282test_no_truncation_of_unlimited_history_file
283
284# A valid GDBHISTSIZE value overrides the setting inside the .gdbinit file; an
285# invalid GDBHISTSIZE value is ignored, falling back on the setting inside the
286# .gdbinit file.
287test_gdbinit_history_setting "gdbinit-history/unlimited" "1000" "1000"
288test_gdbinit_history_setting "gdbinit-history/unlimited" "unlimited" "foo"
289
290# Check handling of empty history filename.
291test_empty_history_filename
292