summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/bt-on-fatal-signal.exp
blob: 1af88d50c4cdb7ca2e5efaa05a7920a8794a2136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2021-2022 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test the 'maint set backtrace-on-fatal-signal' behaviour.  Start up
# GDB, turn on backtrace-on-fatal-signal, then send fatal signals to
# GDB and ensure we see the backtrace.

standard_testfile

# The logic for sending signals to GDB might now work when using a
# remote host (will the signal go to GDB, or the program that
# established the connection to the remote host?), so just skip this
# test for remote host setups.
if {[is_remote host]} {
    untested $testfile
    return -1
}

if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
    return -1
}

# Check we can run to main.  If this works this time then we just
# assume that it will work later on (when we repeatedly restart GDB).
if ![runto_main] then {
    return -1
}

# Check that the backtrace-on-fatal-signal feature is supported.  If
# this target doesn't have the backtrace function available then
# trying to turn this on will give an error, in which case we just
# skip this test.
gdb_test_multiple "maint set backtrace-on-fatal-signal on" "" {
    -re "support for this feature is not compiled into GDB" {
	untested $testfile
	return -1
    }
    -re "$gdb_prompt $" {
	pass $gdb_test_name
    }
}

# Now the actual test loop.
foreach test_data {{SEGV "Segmentation fault"} \
		       {FPE "Floating point exception"} \
		       {BUS "Bus error"} \
		       {ABRT "Aborted"}} {
    set sig [lindex ${test_data} 0]
    set msg [lindex ${test_data} 1]
    with_test_prefix ${sig} {

	# Restart GDB.
	save_vars { GDB } {
	    set GDB [gdb_no_core]
	    clean_restart $binfile
	}

	# Capture the pid of GDB.
	set testpid [spawn_id_get_pid $gdb_spawn_id]

	# Start the inferior.
	runto_main

	# Turn on the backtrace-on-fatal-signal feature.
	gdb_test_no_output "maint set backtrace-on-fatal-signal on"

	# Flags for various bits of the output we expect to see, we
	# check for these in the gdb_test_multiple below.
	set saw_fatal_msg false
	set saw_bt_start false
	set saw_bt_end false
	set internal_error_msg_count 0

	# Get the GDB core into the output directory, so that it
	# doesn't count as unexpected core in gdb.sum.
	gdb_test "cd [file dirname $binfile]" "Working directory .*" \
	    "cd to test directory"

	# Send the fatal signal to GDB.
	remote_exec host "kill -${sig} ${testpid}"

	# Scan GDB's output for the backtrace.  As the output we get
	# here includes the standard "internal error" message, which
	# gdb_test_multiple will usually handle, we are forced to make
	# extensive use of the "-early" flag here so that all our
	# patterns are applied before gdb_test_multiple can check for
	# the internal error pattern.
	gdb_test_multiple "" "scan for backtrace" {
	    -early -re "^\r\n" {
		exp_continue
	    }
	    -early -re "^Fatal signal: ${msg}\r\n" {
		set saw_fatal_msg true
		exp_continue
	    }
	    -early -re "^----- Backtrace -----\r\n" {
		set saw_bt_start true
		exp_continue
	    }
	    -early -re ".+\r\n---------------------\r\n" {
		set saw_bt_end true
		exp_continue
	    }
	    -early -re "^A fatal error internal to GDB has been detected, further\r\n" {
		incr internal_error_msg_count
		exp_continue
	    }
	    -early -re "^debugging is not possible.  GDB will now terminate\\.\r\n" {
		incr internal_error_msg_count
		exp_continue
	    }
	    eof {
		# Catch the eof case as this indicates that GDB has
		# gone away, which in this case, is what we expect to
		# happen.
		gdb_assert { $saw_fatal_msg }
		gdb_assert { $saw_bt_start }
		gdb_assert { $saw_bt_end }
		gdb_assert { [expr $internal_error_msg_count == 2] }
	    }
	    -re "$gdb_prompt $" {
		# GDB should terminate, we should never get back to
		# the prompt.
		fail $gdb_test_name
	    }
	}

	# GDB should be dead and gone by this point, but just to be
	# sure, force an exit.
	gdb_exit
    }
}