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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: jan@swi-prolog.org
WWW: http://www.swi-prolog.org
Copyright (c) 2021-2022, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(prolog_debug_tools,
[ (spy)/1, % :Spec (some users tend to define these as
(nospy)/1, % :Spec an operator)
nospyall/0,
debugging/0,
trap/1, % +Exception
notrap/1 % +Exception
]).
:- use_module(library(broadcast), [broadcast/1]).
:- autoload(library(edinburgh), [debug/0]).
:- autoload(library(gensym), [gensym/2]).
:- set_prolog_flag(generate_debug_info, false).
/** <module> User level debugging tools
This library provides tools to control the Prolog debuggers.
Traditionally this code was built-in. Because these tools are only
required in (interactive) debugging sessions they have been moved into
the library.
*/
%! prolog:debug_control_hook(+Action)
%
% Allow user-hooks in the Prolog debugger interaction. See the calls
% below for the provided hooks. We use a single predicate with action
% argument to avoid an uncontrolled poliferation of hooks.
:- multifile
prolog:debug_control_hook/1. % +Action
:- meta_predicate
spy(:),
nospy(:).
%! spy(:Spec) is det.
%! nospy(:Spec) is det.
%! nospyall is det.
%
% Set/clear spy-points. A successfully set or cleared spy-point is
% reported using print_message/2, level `informational`, with one of
% the following terms, where Spec is of the form M:Head.
%
% - spy(Spec)
% - nospy(Spec)
%
% @see spy/1 and nospy/1 call the hook prolog:debug_control_hook/1
% to allow for alternative specifications of the thing to
% debug.
spy(Spec) :-
'$notrace'(spy_(Spec)).
spy_(_:X) :-
var(X),
throw(error(instantiation_error, _)).
spy_(_:[]) :- !.
spy_(M:[H|T]) :-
!,
spy(M:H),
spy(M:T).
spy_(Spec) :-
prolog:debug_control_hook(spy(Spec)),
!.
spy_(Spec) :-
'$find_predicate'(Spec, Preds),
'$member'(PI, Preds),
pi_to_head(PI, Head),
'$define_predicate'(Head),
'$spy'(Head),
fail.
spy_(_).
nospy(Spec) :-
'$notrace'(nospy_(Spec)).
nospy_(_:X) :-
var(X),
throw(error(instantiation_error, _)).
nospy_(_:[]) :- !.
nospy_(M:[H|T]) :-
!,
nospy(M:H),
nospy(M:T).
nospy_(Spec) :-
prolog:debug_control_hook(nospy(Spec)),
!.
nospy_(Spec) :-
'$find_predicate'(Spec, Preds),
'$member'(PI, Preds),
pi_to_head(PI, Head),
'$nospy'(Head),
fail.
nospy_(_).
nospyall :-
'$notrace'(nospyall_).
nospyall_ :-
prolog:debug_control_hook(nospyall),
fail.
nospyall_ :-
spy_point(Head),
'$nospy'(Head),
fail.
nospyall_.
pi_to_head(M:PI, M:Head) :-
!,
pi_to_head(PI, Head).
pi_to_head(Name/Arity, Head) :-
functor(Head, Name, Arity).
%! debugging is det.
%
% Report current status of the debugger.
debugging :-
'$notrace'(debugging_).
debugging_ :-
prolog:debug_control_hook(debugging),
!.
debugging_ :-
( current_prolog_flag(debug, true)
-> print_message(informational, debugging(on)),
findall(H, spy_point(H), SpyPoints),
print_message(informational, spying(SpyPoints))
; print_message(informational, debugging(off))
),
trapping,
forall(debugging_hook, true).
spy_point(Module:Head) :-
current_predicate(_, Module:Head),
'$get_predicate_attribute'(Module:Head, spy, 1),
\+ predicate_property(Module:Head, imported_from(_)).
%! debugging_hook
%
% Multifile hook that is called as forall(debugging_hook, true) and
% that may be used to extend the information printed from other
% debugging libraries.
:- multifile debugging_hook/0.
/*******************************
* EXCEPTIONS *
*******************************/
%! trap(+Formal) is det.
%! notrap(+Formal) is det.
%
% Install a trap on error(Formal, Context) exceptions that unify. The
% tracer is started when a matching exception is raised. This
% predicate enables _debug mode_ using debug/0 to get more context
% about the exception. Even with debug mode disabled exceptions are
% still trapped and thus one may call nodebug/0 to run in normal mode
% after installing a trap. Exceptions are trapped in any thread. Debug
% mode is only enabled in the calling thread. To enable debug mode in
% all threads use tdebug/0.
%
% Calling debugging/0 lists the enabled traps. The predicate notrap/1
% removes matching (unifying) traps.
%
% In many cases debugging an exception that is caught is as simple as
% below (assuming run/0 starts your program).
%
% ```
% ?- gtrap(_).
% ?- run.
% ```
%
% @see gtrap/1 to trap using the graphical debugger.
% @see _Edit exceptions_ menu in PceEmacs and the graphical debugger
% that provide a graphical frontend to trap exceptions.
:- dynamic
exception/4, % Name, Term, NotCaught, Caught
installed/1. % ClauseRef
trap(Error) :-
'$notrace'(trap_(Error)).
trap_(Error) :-
gensym(ex, Rule),
asserta(exception(Rule, error(Error, _), true, true)),
print_message(informational, trap(Rule, error(Error, _), true, true)),
install_exception_hook,
debug.
notrap(Error) :-
'$notrace'(notrap_(Error)).
notrap_(Error) :-
Exception = error(Error, _),
findall(exception(Name, Exception, NotCaught, Caught),
retract(exception(Name, error(Error, _), Caught, NotCaught)),
Trapping),
print_message(informational, notrap(Trapping)).
trapping :-
findall(exception(Name, Term, NotCaught, Caught),
exception(Name, Term, NotCaught, Caught),
Trapping),
print_message(information, trapping(Trapping)).
:- dynamic
user:prolog_exception_hook/4.
%! exception_hook(+ExIn, -ExOut, +Frame, +Catcher) is failure.
%
% Trap exceptions and consider whether or not to start the tracer.
:- public exception_hook/4.
exception_hook(Ex, Ex, _Frame, Catcher) :-
thread_self(Me),
thread_property(Me, debug(true)),
broadcast(debug(exception(Ex))),
exception(_, Ex, NotCaught, Caught),
!,
( Caught == true
-> true
; Catcher == none,
NotCaught == true
),
trace, fail.
%! install_exception_hook
%
% Make sure our handler is the first of the hook predicate.
install_exception_hook :-
installed(Ref),
( nth_clause(_, I, Ref)
-> I == 1, ! % Ok, we are the first
; retractall(installed(Ref)),
erase(Ref), % Someone before us!
fail
).
install_exception_hook :-
asserta((user:prolog_exception_hook(Ex, Out, Frame, Catcher) :-
exception_hook(Ex, Out, Frame, Catcher)), Ref),
assert(installed(Ref)).
/*******************************
* MESSAGES *
*******************************/
:- multifile
prolog:message//1.
prolog:message(trapping([])) -->
[ 'No exception traps'-[] ].
prolog:message(trapping(Trapping)) -->
[ 'Exception traps on'-[], nl ],
trapping(Trapping).
prolog:message(trap(_Rule, Error, _Caught, _NotCaught)) -->
[ 'Installed trap for exception '-[] ],
exception(Error),
[ nl ].
prolog:message(notrap([])) -->
[ 'No matching traps'-[] ].
prolog:message(notrap(Trapping)) -->
[ 'Removed traps from exceptions'-[], nl ],
trapping(Trapping).
trapping([]) --> [].
trapping([exception(_Rule, Error, _Caught, _NotCaught)|T]) -->
[ ' '-[] ],
exception(Error),
[ nl ],
trapping(T).
exception(Term) -->
{ copy_term(Term, T2),
numbervars(T2, 0, _, [singletons(true)])
},
[ '~p'-[T2] ].
|