1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2004, 2005, 2007, 2008, 2009, 2010, 2011
4# Free Software Foundation, Inc.
5
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19# Check that GDB can trigger and backtrace SIGSEGV signal stacks
20# caused by both accessing (data) and executing (code) at address
21# zero.
22
23# On function descriptor architectures, a zero descriptor, instead of
24# a NULL pointer, is used.  That way the NULL code test always
25# contains a zero code reference.
26
27# For recovery, sigjmp/longjmp are used.
28
29# This also tests backtrace/gdb1476.
30
31if [target_info exists gdb,nosignals] {
32    verbose "Skipping signull.exp because of nosignals."
33    continue
34}
35
36if $tracelevel {
37    strace $tracelevel
38}
39
40
41set testfile "signull"
42set srcfile ${testfile}.c
43set binfile ${objdir}/${subdir}/${testfile}
44if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
45    untested signull.exp
46    return -1
47}
48
49gdb_exit
50gdb_start
51gdb_reinitialize_dir $srcdir/$subdir
52gdb_load ${binfile}
53
54#
55# Run to `main' where we begin our tests.
56#
57
58if ![runto_main] then {
59    gdb_suppress_tests
60}
61
62# If we can examine what's at memory address 0, it is possible that we
63# could also execute it.  This could probably make us run away,
64# executing random code, which could have all sorts of ill effects,
65# especially on targets without an MMU.  Don't run the tests in that
66# case.
67
68gdb_test_multiple "x 0" "memory at address 0" {
69    -re "0x0:.*Cannot access memory at address 0x0.*$gdb_prompt $" { }
70    -re "0x0:.*Error accessing memory address 0x0.*$gdb_prompt $" { }
71    -re ".*$gdb_prompt $" {
72	untested "Memory at address 0 is possibly executable"
73	return
74    }
75}
76
77# If an attempt to call a NULL pointer leaves the inferior in main,
78# then function pointers are descriptors, probe this and remember the
79# result.
80
81gdb_test_no_output "set test = code_entry_point" \
82    "set for function pointer probe"
83set test "probe function pointer"
84set function_pointer code_entry_point
85set signame "SIGSEGV"
86gdb_test_multiple "continue" "$test" {
87    -re "Program received signal SIGSEGV.*bowler .*$gdb_prompt $" {
88	set function_pointer code_descriptor
89	pass "$test (function descriptor)"
90    }
91    -re "Program received signal SIGSEGV.*0.*$gdb_prompt $" {
92	pass "$test (function entry-point)"
93    }
94    -re "Program received signal SIGBUS.*0.*$gdb_prompt $" {
95	set signame SIGBUS
96	pass "$test (function entry-point)"
97    }
98}
99
100# Re-start from scratch, breakpoint the bowler so that control is
101# regained after each test, and run up to that.
102rerun_to_main
103gdb_test "break bowler"
104gdb_test "break keeper"
105# By default Stop:Yes Print:Yes Pass:Yes
106gdb_test "handle SIGSEGV" "SIGSEGV.*Yes.*Yes.*Yes.*Segmentation fault"
107gdb_test "handle SIGBUS" "SIGBUS.*Yes.*Yes.*Yes.*Bus error"
108
109# For the given signal type, check that: the SIGSEGV occures; a
110# backtrace from the SEGV works; the sigsegv is delivered; a backtrace
111# through the SEGV works.
112
113proc test_segv { name tag bt_from_segv bt_from_keeper } {
114    global signame
115    gdb_test continue "Breakpoint.* bowler.*" "${name}; start with the bowler"
116    # NB: Don't use $tag in the testname - changes across systems.
117    gdb_test_no_output "set test = $tag" "${name}; select the pointer type"
118    gdb_test continue "Program received signal ${signame}.*" \
119	"${name}; take the ${signame}"
120    gdb_test backtrace $bt_from_segv "${name}; backtrace from ${signame}"
121    gdb_test continue "Breakpoint.* keeper.*" "${name}; continue to the keeper"
122    gdb_test backtrace $bt_from_keeper "${name}; backtrace from keeper through ${signame}"
123}
124
125test_segv "data read" data_read \
126    {#0 .* bowler .*#1  .* main .*} \
127    {#0 .* keeper .*#1  .* handler .*#2 .* bowler .*#3  .* main .*}
128test_segv "data write" data_write \
129    {#0 .* bowler .*#1  .* main .*} \
130    {#0 .* keeper .*#1  .* handler .*#2 .* bowler .*#3  .* main .*}
131test_segv code $function_pointer \
132    {#0 .* 0x0+ .*#1 .* bowler .*#2  .* main .*} \
133    {#0 .* keeper .*#1  .* handler .*#2 .* 0x0+ .*#3 .* bowler .*#4 .* main .*}
134