1# Copyright 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# Test the case where we have so many completions that we require the
17# completions hash table within GDB to grow.  Make sure that afte the
18# hash table has grown we try to add duplicate entries into the
19# hash. This checks that GDB doesn't corrupt the hash table when
20# resizing it.
21#
22# In this case we create a test with more functions than the default
23# number of entires in the completion hash table (which is 200), then
24# complete on all function names.
25#
26# GDB will add all the function names from the DWARF, and then from
27# the ELF symbol table, this ensures that we should have duplicates
28# added after resizing the table.
29
30# Create a test source file and return the name of the file.  COUNT is
31# the number of dummy functions to create, this should be more than
32# the default number of entries in the completion hash table within
33# GDB (see gdb/completer.c).
34proc prepare_test_source_file { count } {
35    global gdb_test_file_name
36
37    set filename [standard_output_file "$gdb_test_file_name.c"]
38    set outfile [open $filename w]
39
40    puts $outfile "
41#define MAKE_FUNC(NUM) \\
42  void                 \\
43  func_ ## NUM (void)  \\
44  { /* Nothing.  */ }
45
46#define CALL_FUNC(NUM) \\
47  func_ ## NUM ()
48"
49
50    for { set i 0 } { $i < $count } { incr i } {
51	puts $outfile "MAKE_FUNC ([format {%03d} $i])"
52    }
53
54    puts $outfile "\nint\nmain ()\n{"
55    for { set i 0 } { $i < $count } { incr i } {
56	puts $outfile "  CALL_FUNC ([format {%03d} $i]);"
57    }
58
59    puts $outfile "  return 0;\n}"
60    close $outfile
61
62    return $filename
63}
64
65# Build a source file and compile it.
66set filename [prepare_test_source_file 250]
67standard_testfile $filename
68if {[prepare_for_testing "failed to prepare" "$testfile" $srcfile \
69	 { debug }]} {
70    return -1
71}
72
73# Start the test.
74if {![runto_main]} {
75    fail "couldn't run to main"
76    return
77}
78
79# We don't want to stop gathering completions too early.
80gdb_test_no_output "set max-completions unlimited"
81
82# Collect all possible completions, and check for duplictes.
83set completions [capture_command_output "complete break func_" ""]
84set duplicates 0
85foreach {-> name} [regexp -all -inline -line {^break (\w+\S*)} $completions] {
86    incr all_funcs($name)
87    if { $all_funcs($name) > 1 } {
88	incr duplicates
89	verbose -log "Duplicate entry for '$name' found"
90    }
91}
92gdb_assert { $duplicates == 0 } "duplicate check"
93