[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / early-init-file.exp
blob7c6e9e253faab7cd7b88e7a8456e079c1447a24d
1 # Copyright 2021-2024 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 # Test GDB's early init file mechanism.
18 # Test assumes host == build.
19 require {!is_remote host}
21 standard_testfile
23 # Compile the test executable.
24 if {[build_executable "failed to build" $testfile $srcfile]} {
25     return -1
28 set custom_signal_handle_re \
29     "warning: Found custom handler for signal $decimal \(\[^\r\n\]+\) preinstalled\."
30 set signal_dispositions_re \
31     [multi_line \
32          "Some signal dispositions inherited from the environment \(\[^\r\n\]+\)" \
33          "won't be propagated to spawned programs\." ]
34 set gdb_sanitizer_msg_re \
35     [multi_line \
36          "($custom_signal_handle_re" \
37          ")+$signal_dispositions_re" \
38          ""]
40 # Start gdb and ensure that the initial version string is styled in
41 # STYLE, use MESSAGE as the name of the test.
42 proc check_gdb_startup_version_string { style { message "" } } {
43     global gdb_sanitizer_msg_re
45     if { $message == "" } {
46         set message "check startup version string has style $style"
47     }
49     gdb_exit
50     gdb_spawn
51     set vers [style "GNU gdb.*" $style]
52     gdb_test "" "^(${gdb_sanitizer_msg_re})?${vers}.*" $message
55 # Return a list containing two directory paths for newly created home
56 # directories.
58 # The first directory is a HOME style home directory, it contains a
59 # .gdbearlyinit file containing CONTENT.
61 # The second directory is an XDG_CONFIG_HOME style home directory, it
62 # contains a sub-directory gdb/, inside which is a file gdbearlyinit
63 # that also contains CONTENT.
65 # The PREFIX is used in both directory names and should be unique for
66 # each call to this function.
67 proc setup_home_directories { prefix content } {
68     set home_dir [standard_output_file "${prefix}-home"]
69     set xdg_home_dir [standard_output_file "${prefix}-xdg"]
71     file mkdir $home_dir
72     file mkdir "$xdg_home_dir/gdb"
74     # Write the content into the HOME directory.
75     set fd [open "$home_dir/.gdbearlyinit" w]
76     puts $fd $content
77     close $fd
79     # Copy this from the HOME directory into the XDG_CONFIG_HOME
80     # directory.
81     file copy -force "$home_dir/.gdbearlyinit" "$xdg_home_dir/gdb/gdbearlyinit"
83     return [list $home_dir $xdg_home_dir]
86 # Restart GDB and ensure that there's no license text, we should just
87 # drop straight to the prompt.
88 proc check_gdb_startups_up_quietly { message } {
89     global gdb_prompt
90     global gdb_sanitizer_msg_re
92     gdb_exit
93     gdb_spawn
95     gdb_test_multiple "" $message {
96         -re "^(${gdb_sanitizer_msg_re})?$gdb_prompt $" {
97             pass $gdb_test_name
98         }
99     }
102 # Restart GDB and check that the size of the thread pool has not been
103 # adjusted to match the number of machine cores at early init time.
104 proc check_gdb_maint_show { message } {
105     global gdb_prompt
106     global gdb_sanitizer_msg_re
107     gdb_exit
108     gdb_spawn
109     set setshowprefix "The number of worker threads GDB can use is"
110     set unset "$setshowprefix the default \\\(currently 0\\\)."
111     set final "$setshowprefix 1."
112     # Output when CXX_STD_THREAD is undefined.
113     set off "$setshowprefix 0."
114     gdb_test_multiple "" $message {
115         -re "^(${gdb_sanitizer_msg_re})?($unset|$off)\r\n($final|$off)\r\n$gdb_prompt $" {
116             pass $gdb_test_name
117         }
118     }
121 with_ansi_styling_terminal {
123     # Start GDB and confirm that the version string is styled.
124     save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
125         set INTERNAL_GDBFLAGS [string map {"-q" ""} $INTERNAL_GDBFLAGS]
126         check_gdb_startup_version_string version
127     }
129     # Create an empty directory we can use as HOME for some of the
130     # tests below.  When we set XDG_CONFIG_HOME we still need to point
131     # HOME at something otherwise GDB complains that it doesn't know
132     # where to create the index cache.
133     set empty_home_dir [standard_output_file fake-empty-home]
135     # Create two directories to use for the style setting test.
136     set dirs [setup_home_directories "style" \
137                   [multi_line_input \
138                        "set style version foreground none" \
139                        "set style version background none" \
140                        "set style version intensity normal"]]
141     set home_dir [lindex $dirs 0]
142     set xdg_home_dir [lindex $dirs 1]
144     # Now arrange to use the fake home directory early init file.
145     save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
146         set INTERNAL_GDBFLAGS [string map {"-nx" "" "-q" ""} $INTERNAL_GDBFLAGS]
148         # Now test GDB when using the HOME directory.
149         set env(HOME) $home_dir
150         unset -nocomplain env(XDG_CONFIG_HOME)
151         check_gdb_startup_version_string none \
152             "check version string is unstyled using HOME"
154         # Now test using the XDG_CONFIG_HOME folder.  We still need to
155         # have a HOME directory set otherwise GDB will issue an error
156         # about not knowing where to place the index cache.
157         set env(XDG_CONFIG_HOME) $xdg_home_dir
158         set env(HOME) $empty_home_dir
159         check_gdb_startup_version_string none \
160             "check version string is unstyled using XDG_CONFIG_HOME"
161     }
163     # Create two directories to use for the quiet startup test.
164     set dirs [setup_home_directories "quiet" "set startup-quietly on"]
165     set home_dir [lindex $dirs 0]
166     set xdg_home_dir [lindex $dirs 1]
168     # Now arrange to use the fake home directory startup file.
169     save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
170         set INTERNAL_GDBFLAGS [string map {"-nx" "" "-q" ""} $INTERNAL_GDBFLAGS]
172         # Now test GDB when using the HOME directory.
173         set env(HOME) $home_dir
174         unset -nocomplain env(XDG_CONFIG_HOME)
175         check_gdb_startups_up_quietly \
176             "check GDB starts quietly using HOME"
178         # Now test using the XDG_CONFIG_HOME folder.  We still need to
179         # have a HOME directory set otherwise GDB will issue an error
180         # about not knowing where to place the index cache.
181         set env(XDG_CONFIG_HOME) $xdg_home_dir
182         set env(HOME) $empty_home_dir
183         check_gdb_startups_up_quietly \
184             "check GDB starts quietly using XDG_CONFIG_HOME"
185     }
187     # Create fake home directory for the thread pool size check.
188     set dirs [setup_home_directories "maint-show" \
189                   [multi_line_input \
190                        "set startup-quietly on" \
191                        "maint show worker-threads" \
192                        "maint set worker-threads 1" \
193                        "maint show worker-threads"]]
195     set home_dir [lindex $dirs 0]
197     # Now arrange to use the fake home directory startup file.
198     save_vars { INTERNAL_GDBFLAGS env(HOME) env(XDG_CONFIG_HOME) } {
199         set INTERNAL_GDBFLAGS [string map {"-nx" "" "-q" ""} $INTERNAL_GDBFLAGS]
201         # Now test GDB when using the HOME directory.
202         set env(HOME) $home_dir
203         unset -nocomplain env(XDG_CONFIG_HOME)
204         check_gdb_maint_show \
205             "check early init of thread pool size"
206     }