errno.exp revision 1.1.1.1 1 # Copyright 2024 Free Software Foundation, Inc.
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 3 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11
12 # Check that errno can be accessed by GDB under a variety of
13 # circumstances.
14 #
15 # The challenge with GDB accessing errno is that, on modern systems,
16 # errno is a variable in thread-local storage. So, if GDB's access to
17 # thread local storage is broken or unavailable, some of these tests
18 # could fail. On Linux, this is/was known to happen on systems with
19 # older versions of glibc as well as when debugging statically linked
20 # binaries.
21 #
22 # Another possibility is that the environment lacks sufficient
23 # type information to print errno. This can happen for the errno
24 # variable itself or when the debuginfo contains a macro for errno
25 # which refers to a function lacking type information.
26 #
27 # When debugging core files, access to errno might not be possible
28 # both due to the situations described earlier along with the fact
29 # that inferior function calls are not possible (for the cases in
30 # which errno is a macro which calls a function returning errno's
31 # address).
32 #
33 # It's also possible for a program to declare errno in an inner scope
34 # causing the thread-local errno to be shadowed. GDB should still
35 # correctly print the masking errno for this case.
36 #
37 # At the time that this test was written, on GNU/Linux and on FreeBSD,
38 # there were always scenarios in which printing errno was problematic.
39 # This test attempts to identify the problem cases and set up xfails
40 # for them. So, hopefully, there should be no actual failures. But
41 # the "expected" failures encountered by running this test do
42 # genuinely illustrate problems that a user might encounter while
43 # attempting to print errno.
44
45 standard_testfile
46
47 proc do_tests {{do_xfail_cast 0} {do_xfail 0} {do_xfail_core_test 0}} {
48 clean_restart $::binfile
49 if ![runto_main] {
50 return
51 }
52
53 gdb_breakpoint [gdb_get_line_number "main-breakpoint"]
54 gdb_continue_to_breakpoint "main-breakpoint"
55
56 # Whether or not "print errno" will work often depends on the
57 # debuginfo available. We can make some inferences about whether
58 # some of the tests should have xfail set-up by looking at the
59 # output of "ptype errno". This test is set up to always pass
60 # even for less than ideal outputs, because the point is to set up
61 # the xfail(s).
62 gdb_test_multiple "ptype errno" "check errno type availability" {
63 -re -wrap "type = int" {
64 pass $gdb_test_name
65 }
66 -re -wrap "type = .*no debug info.*" {
67 pass $gdb_test_name
68 set do_xfail 1
69 set do_xfail_core_test 1
70 }
71 -re -wrap "Cannot find thread-local variables on this target.*" {
72 pass $gdb_test_name
73 set do_xfail 1
74 set do_xfail_core_test 1
75 set do_xfail_cast 1
76 }
77 -re -wrap "Cannot find thread-local storage.*" {
78 pass $gdb_test_name
79 set do_xfail 1
80 set do_xfail_core_test 1
81 set do_xfail_cast 1
82 }
83 -re -wrap "has unknown return type; cast the call to its declared return type.*" {
84
85 # On systems which glibc as the C library, using -g3,
86 # which causes macro information to be included in the
87 # debuginfo, errno might be defined as follows:
88 #
89 # #define errno (*__errno_location ())
90 #
91 # So, when we do "ptype errno", due to macro expansion,
92 # this ends up being "ptype (*__errno_location ())". So
93 # the call to __errno_location (or something similar on
94 # other OSes) is the call mentioned in the error message.
95
96 pass $gdb_test_name
97 set do_xfail 1
98 set do_xfail_core_test 1
99 set do_xfail_cast 1
100 }
101 }
102
103 # If errno is defined as a macro that contains an obvious function
104 # call, it won't work when debugging a core file.
105 gdb_test_multiple "info macro errno" "check if errno is a macro" {
106 -re -wrap "Defined at.*\[\r\n\]#define.*\\\(\\\).*" {
107 set do_xfail_core_test 1
108 pass $gdb_test_name
109 }
110 -re -wrap "Defined at.*\[\r\n\]#define.*" {
111 pass $gdb_test_name
112 }
113 -re -wrap "The symbol .errno. has no definition.*" {
114 pass $gdb_test_name
115 }
116 }
117
118 # Sometimes, "ptype errno" will ferret out that thread local
119 # variables aren't accessible, but sometimes it won't. Dig deeper
120 # by trying to access memory using the "x/d" command. Again, the
121 # point here is to set up an xfail for the later tests, so we pass
122 # this test for other known outputs.
123 gdb_test_multiple "x/d &errno" "attempt to access errno memory" {
124 -re -wrap "Cannot find thread-local variables on this target.*" {
125 pass $gdb_test_name
126 set do_xfail 1
127 set do_xfail_core_test 1
128 set do_xfail_cast 1
129 }
130 -re -wrap "Cannot find thread-local storage.*" {
131 pass $gdb_test_name
132 set do_xfail 1
133 set do_xfail_core_test 1
134 set do_xfail_cast 1
135 }
136 -re -wrap "has unknown return type; cast the call to its declared return type.*" {
137 set do_xfail 1
138 set do_xfail_core_test 1
139 set do_xfail_cast 1
140 pass $gdb_test_name
141 }
142 -re -wrap "$::hex.*?:\[\t \]$::decimal" {
143 pass $gdb_test_name
144 }
145 }
146
147 if $do_xfail {
148 setup_xfail *-*-*
149 }
150 gdb_test "print errno" ".* = 42"
151
152 if $do_xfail_cast {
153 setup_xfail *-*-*
154 }
155 gdb_test "print (int) errno" ".* = 42"
156
157 set corefile ${::binfile}.core
158 set core_supported 0
159 if { ![is_remote host] } {
160 set core_supported [gdb_gcore_cmd $corefile "save corefile"]
161 }
162 # Normally, we'd check core_supported here and return if it's
163 # not, but we'll defer that until after the shadow test.
164
165 gdb_breakpoint [gdb_get_line_number "shadow_errno-breakpoint"]
166 gdb_continue_to_breakpoint "shadow_errno-breakpoint"
167
168 # This test demonstrates why a simple hack to GDB for printing
169 # errno is a bad idea. (The hack was to intercept the string
170 # "errno" in process_print_command_args() and replace it with
171 # "*(*(int *(*)(void)) __errno_location) ()".)
172 gdb_test "print errno" ".* = 36" "print masking errno"
173
174 # Finish test early if no core file was made.
175 if !$core_supported {
176 return
177 }
178
179 clean_restart $::binfile
180
181 set core_loaded [gdb_core_cmd $corefile "load corefile"]
182 if { $core_loaded == -1 } {
183 return
184 }
185 if $do_xfail_core_test {
186 setup_xfail *-*-*
187 }
188 gdb_test "print errno" ".* = 42" "check errno value from corefile"
189 }
190
191 set binprefix $binfile
192
193 with_test_prefix "default" {
194 set binfile $binprefix-default
195 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
196 untested "failed to compile"
197 } else {
198 do_tests
199 }
200 }
201
202 with_test_prefix "macros" {
203 set binfile $binprefix-macros
204 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug macros}] != "" } {
205 untested "failed to compile"
206 } else {
207 do_tests
208 }
209 }
210
211 with_test_prefix "static" {
212 set binfile $binprefix-static
213 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug "additional_flags=-static"}] != "" } {
214 untested "failed to compile"
215 } else {
216 do_tests
217 }
218 }
219
220 with_test_prefix "static-macros" {
221 set binfile $binprefix-static-macros
222 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug macros "additional_flags=-static"}] != "" } {
223 untested "failed to compile"
224 } else {
225 do_tests
226 }
227 }
228
229 with_test_prefix "pthreads" {
230 set binfile $binprefix-pthreads
231 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
232 untested "failed to compile"
233 } else {
234 do_tests
235 }
236 }
237
238 with_test_prefix "pthreads-macros" {
239 set binfile $binprefix-pthreads-macros
240 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug macros}] != "" } {
241 untested "failed to compile"
242 } else {
243 do_tests
244 }
245 }
246
247 with_test_prefix "pthreads-static" {
248 set binfile $binprefix-pthreads-static
249 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug "additional_flags=-static"}] != "" } {
250 untested "failed to compile"
251 } else {
252 do_tests
253 }
254 }
255
256 with_test_prefix "pthreads-static-macros" {
257 set binfile $binprefix-pthreads-static-macros
258 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug macros "additional_flags=-static"}] != "" } {
259 untested "failed to compile"
260 } else {
261 do_tests
262 }
263 }
264