]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/lib/selftest-support.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / selftest-support.exp
1 # Copyright 2003-2024 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 # Find a pathname to a file that we would execute if the shell was asked
17 # to run $arg using the current PATH.
18
19 proc find_gdb { arg } {
20
21 # If the arg directly specifies an existing executable file, then
22 # simply use it.
23
24 if {[file executable $arg]} {
25 return $arg
26 }
27
28 set result [which $arg]
29 if {[string match "/" [ string range $result 0 0 ]]} {
30 return $result
31 }
32
33 # If everything fails, just return the unqualified pathname as default
34 # and hope for best.
35
36 return $arg
37 }
38
39 # A helper proc that sets up for self-testing.
40 # EXECUTABLE is the gdb to use.
41 # FUNCTION is the function to break in, either captured_main
42 # or captured_command_loop.
43 # Return 0 in case of success, -1 in case of failure, and -2 in case of
44 # skipping the test-case.
45
46 proc selftest_setup { executable function } {
47 global gdb_prompt
48 global INTERNAL_GDBFLAGS
49
50 # load yourself into the debugger
51
52 global gdb_file_cmd_debug_info
53 set gdb_file_cmd_debug_info "unset"
54
55 set result [gdb_load $executable]
56
57 if {$result != 0} {
58 return -1
59 }
60
61 if {$gdb_file_cmd_debug_info != "debug"} {
62 untested "no debug information, skipping testcase."
63 return -2
64 }
65
66 # Set a breakpoint at $function.
67 if { [gdb_breakpoint $function "no-message"] != 1 } {
68 untested "Cannot set breakpoint at $function, skipping testcase."
69 return -2
70 }
71
72 # run yourself
73
74 set description "run until breakpoint at $function"
75 gdb_test_multiple "run $INTERNAL_GDBFLAGS" "$description" {
76 -re "Starting program.*Breakpoint \[0-9\]+,.*$function \\(.*\\).* at .*main.c:.*$gdb_prompt $" {
77 pass "$description"
78 }
79 -re "Starting program.*Breakpoint \[0-9\]+,.*$function \\(.*\\).*$gdb_prompt $" {
80 xfail "$description (line numbers scrambled?)"
81 }
82 -re "vfork: No more processes.*$gdb_prompt $" {
83 fail "$description (out of virtual memory)"
84 return -1
85 }
86 -re ".*$gdb_prompt $" {
87 fail "$description"
88 return -1
89 }
90 }
91
92 return 0
93 }
94
95 # Prepare for running a self-test by moving the GDB executable to a
96 # location where we can use it as the inferior. Return the filename
97 # of the new location.
98 #
99 # If the current testing setup is not suitable for running a
100 # self-test, then return an empty string.
101 proc selftest_prepare {} {
102 # Are we testing with a remote board? In that case, the target
103 # won't have access to the GDB's auxilliary data files
104 # (data-directory, etc.). It's simpler to just skip.
105 if { [is_remote target] || [is_remote host] } {
106 return
107 }
108
109 # ... or seemingly testing with a cross debugger? Likely GDB
110 # wouldn't be able to debug itself then...
111 if ![isnative] {
112 return
113 }
114
115 # ... or with a stub-like server? I.e., gdbserver + "target
116 # remote"? In that case we won't be able to pass command line
117 # arguments to GDB, and selftest_setup wants to do exactly that.
118 if [use_gdb_stub] {
119 return
120 }
121
122 # Run the test with self. Copy the file executable file in case
123 # this OS doesn't like to edit its own text space.
124
125 set gdb_fullpath [find_gdb $::GDB]
126
127 if {[is_remote host]} {
128 set xgdb x$::tool
129 } else {
130 set xgdb [standard_output_file x$::tool]
131 }
132
133 # Remove any old copy lying around.
134 remote_file host delete $xgdb
135
136 set filename [remote_download host $gdb_fullpath $xgdb]
137
138 return $filename
139 }
140
141 # A simple way to run some self-tests.
142
143 proc do_self_tests {function body} {
144 set file [selftest_prepare]
145 if { $file eq "" } {
146 return
147 }
148
149 gdb_start
150
151 # When debugging GDB with GDB, some operations can take a relatively long
152 # time, especially if the build is non-optimized. Bump the timeout for the
153 # duration of the test.
154 with_timeout_factor 10 {
155 set result [selftest_setup $file $function]
156 if {$result == 0} {
157 set result [uplevel $body]
158 }
159 }
160
161 gdb_exit
162 catch "remote_file host delete $file"
163
164 if {$result == -1} {
165 warning "Couldn't test self"
166 }
167 }