This file is indexed.

/usr/lib/swi-prolog/library/prolog_stack.pl is in swi-prolog-nox 5.10.4-3ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        wielemak@science.uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2007, University of Amsterdam

    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 2
    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 Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(prolog_stack,
	  [ get_prolog_backtrace/2,	% +MaxDepth, -Stack
	    get_prolog_backtrace/3,	% +Frame, +MaxDepth, -Stack
	    print_prolog_backtrace/2,	% +Stream, +Stack
	    backtrace/1			% +MaxDepth
	  ]).
:- use_module(library(prolog_clause)).
:- use_module(library(debug)).
:- use_module(library(lists)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This module defines more high-level primitives for examining the Prolog
stack.  It is defined for debugging purposes.

Status
------

This module is in an early development  status. Please be aware that the
Prolog representation of a backtrace as  well   as  the printed form are
subject to change.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

%%	get_prolog_backtrace(+MaxDepth, -Backtrace)
%
%	Return a Prolog structure representing a backtrace from the
%	current location.  The backtrace is a list of frames.  Each
%	frame is represented as one of
%
%		* frame(Level, Clause, PC)
%		* frame(Level, foreign(Name/Arity), foreign)
%
%	MaxDepth defines the maximum number of frames returned.

get_prolog_backtrace(MaxDepth, Stack) :-
	prolog_current_frame(Fr),
	prolog_frame_attribute(Fr, pc, PC),
	prolog_frame_attribute(Fr, parent, Parent),
	backtrace(MaxDepth, Parent, PC, Stack).

get_prolog_backtrace(Fr, MaxDepth, Stack) :-
	backtrace(MaxDepth, Fr, call, Stack).

backtrace(0, _, _, []) :- !.
backtrace(MaxDepth, Fr, PC, [frame(Level, Where)|Stack]) :-
	prolog_frame_attribute(Fr, level, Level),
	(   PC == foreign
	->  prolog_frame_attribute(Fr, predicate_indicator, Pred),
	    Where = foreign(Pred)
	;   PC == call
	->  prolog_frame_attribute(Fr, predicate_indicator, Pred),
	    Where = call(Pred)
	;   prolog_frame_attribute(Fr, clause, Clause)
	->  Where = clause(Clause, PC)
	;   Where = meta_call
	),
	(   prolog_frame_attribute(Fr, pc, PC2)
	->  true
	;   PC2 = foreign
	),
	(   prolog_frame_attribute(Fr, parent, Parent)
	->  D2 is MaxDepth - 1,
	    backtrace(D2, Parent, PC2, Stack)
	;   Stack = []
	).


%%	print_prolog_backtrace(+Stream, +Backtrace)
%
%	Print a stacktrace in human readable form.

print_prolog_backtrace(Stream, Backtrace) :-
	phrase(message(Backtrace), Lines),
	print_message_lines(Stream, '', Lines).

message([]) -->
	[].
message([H|T]) -->
	message(H),
	(   {T == []}
	->  []
	;   [nl],
	    message(T)
	).

message(frame(Level, Where)) -->
	level(Level),
	where(Where).

where(foreign(PI)) -->
	[ '~w <foreign>'-[PI] ].
where(call(PI)) -->
	[ '~w'-[PI] ].
where(clause(Clause, PC)) -->
	{ subgoal_position(Clause, PC, File, CharA, _CharZ),
	  File \= @(_),			% XPCE Object reference
	  lineno(File, CharA, Line),
	  clause_predicate_name(Clause, PredName)
	}, !,
	[ '~w at ~w:~d'-[PredName, File, Line] ].
where(clause(Clause, _PC)) -->
	{ clause_property(Clause, file(File)),
	  clause_property(Clause, line_count(Line)),
	  clause_predicate_name(Clause, PredName)
	}, !,
	[ '~w at ~w:~d'-[PredName, File, Line] ].
where(clause(Clause, _PC)) -->
	{ clause_name(Clause, ClauseName)
	},
	[ '~w <no source>'-[ClauseName] ].
where(meta_call) -->
	[ '<meta call>' ].

level(Level) -->
	[ '~|~t[~D]~8+ '-[Level] ].


%%	clause_predicate_name(+ClauseRef, -Predname) is det.
%
%	Produce a name (typically  Functor/Arity)   for  a  predicate to
%	which Clause belongs.

clause_predicate_name(Clause, PredName) :-
	user:prolog_clause_name(Clause, PredName), !.
clause_predicate_name(Clause, PredName) :-
	nth_clause(Head, _N, Clause),
	predicate_name(user:Head, PredName).


%%	backtrace(+MaxDepth)
%
%	Get and print a stacktrace to the user_error stream.

backtrace(MaxDepth) :-
	get_prolog_backtrace(MaxDepth, Stack),
	print_prolog_backtrace(user_error, Stack).


subgoal_position(ClauseRef, PC, File, CharA, CharZ) :-
	debug(clause, 'Term-position in ~w at PC=~w:', [ClauseRef, PC]),
	clause_info(ClauseRef, File, TPos, _),
	'$clause_term_position'(ClauseRef, PC, List),
	debug(clause, '\t~w~n', [List]),
	find_subgoal(List, TPos, PosTerm),
	arg(1, PosTerm, CharA),
	arg(2, PosTerm, CharZ).

find_subgoal([], Pos, Pos).
find_subgoal([A|T], term_position(_, _, _, _, PosL), SPos) :-
	nth1(A, PosL, Pos),
	find_subgoal(T, Pos, SPos).


%%	lineno(+File, +Char, -Line)
%
%	Translate a character location to a line-number.

lineno(File, Char, Line) :-
	open(File, read, Fd),
	lineno_(Fd, Char, Line0),
	close(Fd),
	Line = Line0.

lineno_(Fd, Char, L) :-
	stream_property(Fd, position('$stream_position'(C,L0,_,_))),
	C > Char, !,
	L is L0-1.
lineno_(Fd, Char, L) :-
	skip(Fd, 10),
	lineno_(Fd, Char, L).