1 # Copyright 2021-2023 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 the 'maint set backtrace-on-fatal-signal' behaviour. Start up
17 # GDB, turn on backtrace-on-fatal-signal, then send fatal signals to
18 # GDB and ensure we see the backtrace.
22 # The logic for sending signals to GDB might now work when using a
23 # remote host (will the signal go to GDB, or the program that
24 # established the connection to the remote host?), so just skip this
25 # test for remote host setups.
26 if {[is_remote host]} {
31 if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
35 # Check we can run to main. If this works this time then we just
36 # assume that it will work later on (when we repeatedly restart GDB).
41 # Check that the backtrace-on-fatal-signal feature is supported. If
42 # this target doesn't have the backtrace function available then
43 # trying to turn this on will give an error, in which case we just
45 gdb_test_multiple "maint set backtrace-on-fatal-signal on" "" {
46 -re "support for this feature is not compiled into GDB" {
55 # Now the actual test loop.
56 foreach test_data {{SEGV "Segmentation fault"} \
57 {FPE "Floating point exception"} \
60 set sig [lindex ${test_data} 0]
61 set msg [lindex ${test_data} 1]
62 with_test_prefix ${sig} {
67 clean_restart $binfile
70 # Capture the pid of GDB.
71 set testpid [spawn_id_get_pid $gdb_spawn_id]
76 # Turn on the backtrace-on-fatal-signal feature.
77 gdb_test_no_output "maint set backtrace-on-fatal-signal on"
79 # Flags for various bits of the output we expect to see, we
80 # check for these in the gdb_test_multiple below.
81 set saw_fatal_msg false
82 set saw_bt_start false
84 set internal_error_msg_count 0
86 # Get the GDB core into the output directory, so that it
87 # doesn't count as unexpected core in gdb.sum.
88 gdb_test "cd [file dirname $binfile]" "Working directory .*" \
89 "cd to test directory"
91 # Send the fatal signal to GDB.
92 remote_exec host "kill -${sig} ${testpid}"
94 # Scan GDB's output for the backtrace. As the output we get
95 # here includes the standard "internal error" message, which
96 # gdb_test_multiple will usually handle, we are forced to make
97 # extensive use of the "-early" flag here so that all our
98 # patterns are applied before gdb_test_multiple can check for
99 # the internal error pattern.
100 gdb_test_multiple "" "scan for backtrace" {
104 -early -re "^Fatal signal: ${msg}\r\n" {
105 set saw_fatal_msg true
108 -early -re "^----- Backtrace -----\r\n" {
109 set saw_bt_start true
112 -early -re ".+\r\n---------------------\r\n" {
116 -early -re "^A fatal error internal to GDB has been detected, further\r\n" {
117 incr internal_error_msg_count
120 -early -re "^debugging is not possible. GDB will now terminate\\.\r\n" {
121 incr internal_error_msg_count
125 # Catch the eof case as this indicates that GDB has
126 # gone away, which in this case, is what we expect to
128 gdb_assert { $saw_fatal_msg }
129 gdb_assert { $saw_bt_start }
130 gdb_assert { $saw_bt_end }
131 gdb_assert { [expr $internal_error_msg_count == 2] }
133 -re "$gdb_prompt $" {
134 # GDB should terminate, we should never get back to
140 # GDB should be dead and gone by this point, but just to be
141 # sure, force an exit.