Subject: Re: [SWIPL] on_signal

From: Jan Wielemaker <[email-masked]>
Date: Tue, 19 Jun 2001 10:20:52 +0200

Andrew,

On Tue, 19 Jun 2001, Andrew V. Diatchkov wrote:
>Hello.
>I'm using on_signal for handling SEGV signal and halt my
>program in this case (SWI-Prolog 4.0.7, RedHat Linux 7.1).
>
>There is the test program (see below) which doesn't handles SIGSEGV. I
>would like to use it as runtime application. Can you help?

For two reasons.  First of all, you make a saved-state, but signal
handling state isn't part of the state, so you need

  :- initialization on_signal(segv, _, sigsegv).

Next, SEGV is used internally for stack-guarding on many Unix systems,
including Linux.  This means that, after receiving a SIGSEGV it checks
whether there are any stacks `low'.  If so it expands them and returns.
If there is no stack low it behaves as with any other signal, checking
the on_signal declaration and then mapping the signal onto a foreign
routine, Prolog exception or Prolog routine.

>main.pl :
>
>:- on_signal(segv,_,sigsegv).
>
>sigsegv(Context) :- writef('Done. Context=%w',[Context]),nl,halt.
>main :-
>           repeat,
>	   sleep(1.0),
>	   fail.
>
>Makefile :
>prog:	main.pl
>	pl -o prog -t
>'[main],qsave_program(prog,[goal=true,toplevel=main,stand_alone=true])'

P.s. This gives the same result and looks a bit prettier in your
Makefile:

  pl -o prog --toplevel=main --stand_alone=true --goal=true -c main.pl

These days I normally use the #! script alternative.  Unless
your program is huge it works quick and easy.

#!/usr/bin/pl -g true -t main -q -s
<script>

>Type this:
>
>kill -11 `pidof prog`
>
>Result: 'ERROR: sleep/1: Caught signal 11 (segv)'
>
>Why not 'Done. Context=segv'?

	Regards --- Jan