From Joachim.Katzer@icn.siemens.de  Mon Sep 11 16:01:50 2000
Received: from beamer.mchh.siemens.de (beamer.mchh.siemens.de [194.138.158.163])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id QAA26756
	for <prolog@swi.psy.uva.nl>; Mon, 11 Sep 2000 16:01:50 +0200 (MET DST)
Received: from moody.mchh.siemens.de (mail2.mchh.siemens.de [194.138.158.226])
	by beamer.mchh.siemens.de (8.9.3/8.9.3) with ESMTP id QAA13248
	for <prolog@swi.psy.uva.nl>; Mon, 11 Sep 2000 16:01:36 +0200 (MET DST)
Received: from mchh246e.demchh201e.icn.siemens.de ([139.21.200.56])
	by moody.mchh.siemens.de (8.9.1/8.9.1) with ESMTP id QAA23129
	for <prolog@swi.psy.uva.nl>; Mon, 11 Sep 2000 16:01:27 +0200 (MET DST)
Received: by MCHH246E with Internet Mail Service (5.5.2650.21)
	id <SV0WRJW6>; Mon, 11 Sep 2000 16:02:01 +0200
Message-ID: <7B4C96A066E0D21192180060976EBFA1D905B4@MCHH236E>
From: Katzer Joachim <Joachim.Katzer@icn.siemens.de>
To: prolog@swi.psy.uva.nl
Subject: Raising exceptions with goal and call stack info
Date: Mon, 11 Sep 2000 16:01:49 +0200
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2650.21)
Content-Type: text/plain

Hi,

i have written an extended throw predicate that raises an exception term like error(Type,Info)
where Info is a Key=Value list for additional information. I want to add the calling goal and the
call stack to this list. 

These are the predicates:

%% File exception.pl
exception(Error,Info) :-
	( is_list(Info) -> append(Info,SysInfo,XInfo)
	; XInfo = [Info|SysInfo]
	),
            prolog_current_frame(This),
	prolog_frame_attribute(This,parent,Parent),
	prolog_frame_attribute(Parent,goal,Goal),
	prolog_frame_attribute(Parent,parent,GParent),
	prolog_call_stack(GParent, Stack),
	SysInfo=[backtrace=Stack,goal=Goal],
	throw(error(Error,XInfo)).

prolog_call_stack(Frame, Stack) :- prolog_call_stack(Frame, [], Stack), !.
prolog_call_stack(Frame, ChildCall, Stack) :-
	prolog_frame_attribute(Frame,parent,Parent),
	prolog_frame_attribute(Frame,goal,Goal),
	( Goal = M:G -> M \== '$toplevel', functor(G,F,A), C = M:F/A
	; functor(Goal,F,A) -> C = F/A
	; C = Goal
	),
	( C = ChildCall -> Stack = Cs ; Stack = [C|Cs] ),
	!, prolog_call_stack(Parent,C,Cs).
prolog_call_stack(_,_,[]).

%% This is an example:

foo(1) :- bar(1).
bar(1) :- exception(foo(bar),[info=test]).

%% End of file 

In debug mode this works fine:


?- debug, catch(foo(1),E,writeq(E)).
error(foo(bar), [info=test, backtrace=[foo/1, system:catch/3, system:'$call'/1], goal=bar(1)])

E = error(foo(bar), [info=test, backtrace=[foo/1, system:catch/3, system:'$call'/1], goal=bar(1)]) 

Yes


But in nodebug mode SWI-Prolog crashs (segmentation fault on Solaris, stack overflow on WindowsNT):

?- catch(foo(1),E,writeq(E)).
Segmentation fault (core dumped)

Is there something wrong with this idea, or is this an SWI bug?
I'm using SWI-Prolog 3.3.10, on Solaris the additional exception/catch patch.

Thx 

Joachim Katzer

