#   Copyright 2009, 2010 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/>.

if [target_info exists gdb,nosignals] {
    verbose "Skipping sigall-reverse.exp because of nosignals."
    return
}

if ![target_info exists gdb,can_reverse] {
    return
}

if $tracelevel then {
	strace $tracelevel
}


gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir

set testfile sigall-reverse
set srcfile ${testfile}.c
set binfile ${objdir}/${subdir}/${testfile}
if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
     untested sigall.exp
     return -1
}

# Make the first signal SIGABRT because it is always supported.
set sig_supported 1
set thissig "ABRT"

proc test_one_sig {nextsig} {
    global sig_supported
    global gdb_prompt
    global thissig

    set this_sig_supported $sig_supported
    gdb_test "handle SIG$thissig stop print" \
	"SIG$thissig\[ \t\]*Yes\[ \t\]*Yes\[ \t\]*Yes.*"
    gdb_test "b handle_$thissig" "Breakpoint \[0-9\]+ .*"
    gdb_test "b gen_$nextsig" "Breakpoint \[0-9\]+ .*"

    set need_another_continue 1
    set missed_handler 0
    if $this_sig_supported then {
	if { $thissig == "IO" } {
	    setup_xfail "i*86-pc-linuxoldld-gnu" "i*86-pc-linuxaout-gnu"
	}
	set testmsg "get signal $thissig"
	gdb_test_multiple "continue" $testmsg {
	    -re "Program received signal SIG$thissig.*handle_$thissig.*$gdb_prompt $" {
		fail "$testmsg (wrong location)"
	    }
	    -re "Program received signal SIG$thissig.*$gdb_prompt $" {
		pass $testmsg
	    }
	    -re "Breakpoint.* handle_$thissig.*$gdb_prompt $" {
		xfail $testmsg
		set need_another_continue 0
	    }
	}
    }
    if [ istarget "alpha-dec-osf3*" ] then {
	# OSF/1-3.x is unable to continue with a job control stop signal.
	# The inferior remains stopped without an event of interest
	# and GDB waits forever for the inferior to stop on an event
	# of interest. Work around the kernel bug.
	if { $thissig == "TSTP" || $thissig == "TTIN" || $thissig == "TTOU" } {
	    setup_xfail "alpha-dec-osf3*"
	    fail "cannot continue from signal $thissig"
	    set need_another_continue 0
	}
    }

    if $need_another_continue then {
	if { $thissig == "URG" } {
	    setup_xfail "i*86-pc-linuxoldld-gnu" "i*86-pc-linuxaout-gnu"
	}
        # Either Lynx or GDB screws up on SIGPRIO
	if { $thissig == "PRIO" } {
	    setup_xfail "*-*-*lynx*"
	}
	set testmsg "send signal $thissig"
	gdb_test_multiple "continue" $testmsg {
	    -re "Breakpoint.*handle_$thissig.*$gdb_prompt $" {
		pass $testmsg
	    }
	    -re "Breakpoint.*gen_$nextsig.*kill.*$gdb_prompt $" {
		fail "missed breakpoint at handle_$thissig"
		set missed_handler 1
	    }
	}
    }

    if { $missed_handler == "0" } then {
	set testmsg "advance to $nextsig"
        gdb_test_multiple "signal 0" $testmsg {
	    -re "Breakpoint.*gen_$nextsig.*kill.*$gdb_prompt $" {
	        pass $testmsg
	        set sig_supported 1
	    }
	    -re "Breakpoint.*gen_$nextsig.*handle.*$gdb_prompt $" {
	        pass $testmsg
	        set sig_supported 0
	    }
        }
    }
    set thissig $nextsig
}

proc test_one_sig_reverse {prevsig} {
    global gdb_prompt

    gdb_test "reverse-continue" "Breakpoint .* handle_$prevsig.*" \
	"reverse to handler of $prevsig"

    set saw_signal 0
    set testmsg "reverse to gen_$prevsig"
    gdb_test_multiple "reverse-continue" $testmsg {
	-re "Breakpoint.*handle_.*$gdb_prompt " {
	    pass "$testmsg (un-handled)"
	}
	-re "Program received signal SIG$prevsig.*$gdb_prompt " {
	    pass "reverse to signal event, $prevsig"

	    set nested_testmsg "reverse signal delivered"
	    gdb_test_multiple "frame" $nested_testmsg {
		-re ".*handle_$prevsig.*$gdb_prompt " {
		    fail "$nested_testmsg (wrong location)"
		}
		-re ".*$gdb_prompt " {
		    pass $nested_testmsg
		}
	    }

	    set saw_signal 1
	    send_gdb "reverse-continue\n"
	    exp_continue
	}
	-re "Breakpoint.*kill.*$gdb_prompt " {
	    if { $saw_signal } then {
		pass "$testmsg (handled)"
	    } else {
		xfail "$testmsg (handled)"
	    }
	}
	-re "No more reverse-execution history.*kill.*$gdb_prompt " {
	    if { $saw_signal } then {
		pass "$testmsg (handled)"
	    } else {
		xfail "$testmsg (handled)"
	    }
	}	    
    }
}

gdb_load $binfile

runto gen_ABRT

if [target_info exists gdb,use_precord] {
    # Activate process record/replay
    gdb_test_no_output "record" "Turn on process record"
}

# Run until end, then save execution log. 

set breakloc [gdb_get_line_number "end of main" "$srcfile"]
gdb_test "break $breakloc" \
    "Breakpoint $decimal at .*/$srcfile, line $breakloc\." \
    "BP at end of main"

# Signal handlers must be disabled
gdb_test "handle all nostop noprint"

gdb_test "continue" "Breakpoint .* end of main .*" "run to end of main"

gdb_test "record save sigall.precsave" \
    "Saved core file sigall.precsave with execution log\."  \
    "save process recfile"

gdb_test "kill" "" "Kill process, prepare to debug log file" \
    "Kill the program being debugged\\? \\(y or n\\) " "y"

gdb_test "record restore sigall.precsave" \
    "Program terminated with signal .*" \
    "reload precord save file"

# Signal handlers must be re-enabled
gdb_test "handle all stop print"

# test signal handling
test_one_sig HUP
test_one_sig QUIT
test_one_sig ILL
test_one_sig EMT
test_one_sig FPE
test_one_sig BUS
test_one_sig SEGV
test_one_sig SYS
test_one_sig PIPE
test_one_sig ALRM
test_one_sig URG
test_one_sig TSTP
test_one_sig CONT
test_one_sig CHLD
test_one_sig TTIN
test_one_sig TTOU
test_one_sig IO
test_one_sig XCPU
test_one_sig XFSZ
test_one_sig VTALRM
test_one_sig PROF
test_one_sig WINCH
test_one_sig LOST
test_one_sig USR1
test_one_sig USR2
test_one_sig PWR
test_one_sig POLL
test_one_sig WIND
test_one_sig PHONE
test_one_sig WAITING
test_one_sig LWP
test_one_sig DANGER
test_one_sig GRANT
test_one_sig RETRACT
test_one_sig MSG
test_one_sig SOUND
test_one_sig SAK
test_one_sig PRIO
test_one_sig 33
test_one_sig 34
test_one_sig 35
test_one_sig 36
test_one_sig 37
test_one_sig 38
test_one_sig 39
test_one_sig 40
test_one_sig 41
test_one_sig 42
test_one_sig 43
test_one_sig 44
test_one_sig 45
test_one_sig 46
test_one_sig 47
test_one_sig 48
test_one_sig 49
test_one_sig 50
test_one_sig 51
test_one_sig 52
test_one_sig 53
test_one_sig 54
test_one_sig 55
test_one_sig 56
test_one_sig 57
test_one_sig 58
test_one_sig 59
test_one_sig 60
test_one_sig 61
test_one_sig 62
test_one_sig 63
test_one_sig TERM

# The last signal (SIGTERM) gets handled slightly differently because
# we are not setting up for another test.
gdb_test "handle SIGTERM stop print" \
    "SIGTERM\[ \t\]*Yes\[ \t\]*Yes\[ \t\]*Yes.*"
gdb_test "b handle_TERM" "Breakpoint \[0-9\]+ .*"
gdb_test "continue" \
    "Continuing.*Program received signal SIGTERM.*" \
    "get signal TERM"
gdb_test "continue" "Breakpoint.*handle_TERM.*" "send signal TERM"

gdb_test "continue" " end of main .*" "continue to sigall exit" 

test_one_sig_reverse TERM
test_one_sig_reverse 63
test_one_sig_reverse 62
test_one_sig_reverse 61
test_one_sig_reverse 60
test_one_sig_reverse 59
test_one_sig_reverse 58
test_one_sig_reverse 57
test_one_sig_reverse 56
test_one_sig_reverse 55
test_one_sig_reverse 54
test_one_sig_reverse 53
test_one_sig_reverse 52
test_one_sig_reverse 51
test_one_sig_reverse 50
test_one_sig_reverse 49
test_one_sig_reverse 48
test_one_sig_reverse 47
test_one_sig_reverse 46
test_one_sig_reverse 45
test_one_sig_reverse 44
test_one_sig_reverse 43
test_one_sig_reverse 42
test_one_sig_reverse 41
test_one_sig_reverse 40
test_one_sig_reverse 39
test_one_sig_reverse 38
test_one_sig_reverse 37
test_one_sig_reverse 36
test_one_sig_reverse 35
test_one_sig_reverse 34
test_one_sig_reverse 33
test_one_sig_reverse PRIO
test_one_sig_reverse SAK
test_one_sig_reverse SOUND
test_one_sig_reverse MSG
test_one_sig_reverse RETRACT
test_one_sig_reverse GRANT
test_one_sig_reverse DANGER
test_one_sig_reverse LWP
test_one_sig_reverse WAITING
test_one_sig_reverse PHONE
test_one_sig_reverse WIND
test_one_sig_reverse POLL
test_one_sig_reverse PWR
test_one_sig_reverse USR2
test_one_sig_reverse USR1
test_one_sig_reverse LOST
test_one_sig_reverse WINCH
test_one_sig_reverse PROF
test_one_sig_reverse VTALRM
test_one_sig_reverse XFSZ
test_one_sig_reverse XCPU
test_one_sig_reverse IO
test_one_sig_reverse TTOU
test_one_sig_reverse TTIN
test_one_sig_reverse CHLD
test_one_sig_reverse CONT
test_one_sig_reverse TSTP
test_one_sig_reverse URG
test_one_sig_reverse ALRM
test_one_sig_reverse PIPE
test_one_sig_reverse SYS
test_one_sig_reverse SEGV
test_one_sig_reverse BUS
test_one_sig_reverse FPE
test_one_sig_reverse EMT
test_one_sig_reverse ILL
test_one_sig_reverse QUIT
test_one_sig_reverse HUP
test_one_sig_reverse ABRT

# Make the first signal SIGABRT because it is always supported.
set sig_supported 1
set thissig "ABRT"

test_one_sig HUP
test_one_sig QUIT
test_one_sig ILL
test_one_sig EMT
test_one_sig FPE
test_one_sig BUS
test_one_sig SEGV
test_one_sig SYS
test_one_sig PIPE
test_one_sig ALRM
test_one_sig URG
test_one_sig TSTP
test_one_sig CONT
test_one_sig CHLD
test_one_sig TTIN
test_one_sig TTOU
test_one_sig IO
test_one_sig XCPU
test_one_sig XFSZ
test_one_sig VTALRM
test_one_sig PROF
test_one_sig WINCH
test_one_sig LOST
test_one_sig USR1
test_one_sig USR2
test_one_sig PWR
test_one_sig POLL
test_one_sig WIND
test_one_sig PHONE
test_one_sig WAITING
test_one_sig LWP
test_one_sig DANGER
test_one_sig GRANT
test_one_sig RETRACT
test_one_sig MSG
test_one_sig SOUND
test_one_sig SAK
test_one_sig PRIO
test_one_sig 33
test_one_sig 34
test_one_sig 35
test_one_sig 36
test_one_sig 37
test_one_sig 38
test_one_sig 39
test_one_sig 40
test_one_sig 41
test_one_sig 42
test_one_sig 43
test_one_sig 44
test_one_sig 45
test_one_sig 46
test_one_sig 47
test_one_sig 48
test_one_sig 49
test_one_sig 50
test_one_sig 51
test_one_sig 52
test_one_sig 53
test_one_sig 54
test_one_sig 55
test_one_sig 56
test_one_sig 57
test_one_sig 58
test_one_sig 59
test_one_sig 60
test_one_sig 61
test_one_sig 62
test_one_sig 63
test_one_sig TERM