1 % (c) 2009-2025 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(debug,
6 [(~~)/1,
7 debug_print/1, debug_nl/0,
8 debug_print/2, debug_nl/1, /* extra argument with urgency of message 0: not urgent */
9 debug_println/2, debug_println/1,
10 debug_format/3,
11 debug_format_flush/3, % same with flushing output
12 println/2, println/1, % allow easy commenting in of debug_println statements
13 log/1,
14 debug_stats/1, print_debug_stats/0,
15
16 tcltk_turn_debugging_on/0, tcltk_turn_debugging_on/1,
17 tcltk_turn_debugging_off/0,
18 debug_mode/1,
19 debug_level/1,
20 debug_level_active_for/1,
21
22 silent_mode/1, set_silent_mode/1,
23 printsilent/1, nls/0, println_silent/1,
24 formatsilent/2, formatsilent/3, printsilent_message/1,
25 formatsilent_with_colour/4,
26
27 print_quoted/1, print_quoted/2,
28 print_quoted_with_max_depth/2,
29 time/1, % call with timing information, time_call
30 time_if_debug/1, time_with_msg/2,
31 watch/1, watch/2, watch_det/2, det_check/1, det_check/2,
32 nl_time/0, debug_nl_time/1,
33 hit_counter/1,
34
35 new_pp/2, new_sol/2, reset_pp/1,
36 if_det_check/3,
37
38 trace_in_debug_mode/0,
39
40 (timer_call)/1, (timer_call)/2, timer_statistics/0,
41
42
43 bisect/2
44 ]).
45
46 :- use_module(module_information,[module_info/2]).
47 :- module_info(group,infrastructure).
48 :- module_info(description,'This module provides predicates to output debugging information when needed.').
49
50 :- meta_predicate time_if_debug(0).
51 :- meta_predicate time(0).
52 :- meta_predicate time_with_msg(-,0).
53
54 :- meta_predicate time_raw(0,-,-,-).
55
56 :- meta_predicate watch_det(*,0).
57 :- meta_predicate watch(0).
58 :- meta_predicate watch(*,0).
59
60 :- meta_predicate det_check(0).
61 :- meta_predicate det_check(0,0).
62
63 :- meta_predicate if_det_check(0,0,0).
64 :- meta_predicate if_det_check_pp(0,0,0,*).
65
66 :- meta_predicate ~~(0).
67 ~~(G) :- call(G).
68
69 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
70 :- if(environ(prob_enter_debugger_upon_error,true)).
71 % A useful definition that ensures that all standard error exceptions causes the debugger to enter trace mode, is as follows:
72 :- multifile user:error_exception/1.
73 user:error_exception(error(_,_)). % then use ?- debug,go. e.g. for Tcl/Tk Version
74 :- endif.
75
76
77 :- if(environ(prob_use_timer,true)).
78 :- use_module(extension('timer/timer'),[timer_call/1,timer_call/2,timer_init/0,timer_statistics/0]).
79 :- timer_init.
80 :- print('Microsecond timer initialised. Use debug:timer_statistics. to obtain statistics.'),nl.
81 :- else.
82 :- op(300,fx,timer_call).
83 :- meta_predicate timer_call(0).
84 :- meta_predicate timer_call(-,0).
85 timer_call(X) :- call(X).
86 timer_call(_PP,Call) :- call(Call).
87 timer_statistics :- print('*** timer_statistics NOT available; set enable_timer in debug.pl to true'),nl.
88 :- endif.
89
90
91 println(_,X) :- print_with_max_depth(X,10),nl.
92 println(X) :- print_with_max_depth(X,10),nl.
93
94 debug_print(X) :- debug_print(6,X).
95 debug_nl :- debug_nl(6).
96 debug_println(X) :- debug_println(6,X).
97
98 :- dynamic debug_print/2, debug_format/3, debug_nl/1, debug_println/2.
99
100 debug_print(_,_).
101 debug_println(_,_).
102 debug_format(_,_,_).
103 debug_nl(_).
104
105 :- dynamic debug_mode/1.
106 debug_mode(off).
107 % TO DO: use DEBUG environment variable for default value
108 %:- use_module(library(system),[environ/2]).
109 %reset_debug_mode_to_default :-
110 % environ('DEBUG',Val),
111 % (Val=1 ; Val='1' ; Val='TRUE' ; Val = 'YES'),!,
112
113
114
115 % additional flag that can be checked : any non-essential prints should not be printed in silent mode
116 :- dynamic silent_mode/1.
117 silent_mode(off).
118
119 set_silent_mode(X) :- retractall(silent_mode(_)),assertz(silent_mode(X)).
120
121
122 printsilent(S) :- silent_mode(off) -> print(user_output,S) ; true. % print_silent
123 println_silent(S) :- silent_mode(off) -> print(user_output,S),nl(user_output) ; true.
124 printsilent_message(S) :- silent_mode(off) -> format(user_output,'~w~n',[S]) ; true.
125 nls :- silent_mode(off) -> nl(user_output) ; true.
126 formatsilent(FS,Args) :- silent_mode(off) -> format(user_output,FS,Args) ; true.
127 formatsilent(Stream,FS,Args) :- silent_mode(off) -> format(Stream,FS,Args) ; true.
128 :- use_module(tools_printing,[format_with_colour/4]).
129 formatsilent_with_colour(Stream,Colours,FS,Args) :-
130 silent_mode(off) -> format_with_colour(Stream,Colours,FS,Args) ; true.
131
132 debug_format_flush(Level,Msg,Args) :-
133 (debug_mode(on) -> debug_format(Level,Msg,Args), flush_output(user_output)
134 ; true).
135
136
137 reset_debug :-
138 retractall(debug_level(_)), assertz(debug_level(5)),
139 tcltk_turn_debugging_off,
140 set_silent_mode(off).
141
142
143 :- dynamic debug_level/1.
144
145 debug_level(5). /* only messages with priority of 5 or higher are printed */
146
147 % does the debug_level print messages of level X.
148 debug_level_active_for(X) :- debug_mode(on), debug_level(L), X >= L.
149
150 tcltk_turn_debugging_on :- debug_level(X),tcltk_turn_debugging_on(X).
151
152 tcltk_turn_debugging_on(Level) :-
153 debug_mode(on), debug_level(Level),!. % we are already at this level, no change
154 tcltk_turn_debugging_on(Level) :- number(Level),
155 retract(debug_level(_)),
156 assertz(debug_level(Level)),
157 retract(debug_mode(_)), !, assertz(debug_mode(on)),
158 retractall(debug_nl(_)),
159 assertz((debug_nl(U) :- (U < Level -> true ; nl))),
160 retractall(debug_print(_,_)),
161 assertz((debug_print(U,X) :- (U < Level -> true ; print_with_max_depth(X,10)))),
162 retractall(debug_println(_,_)),
163 assertz((debug_println(U,X) :- (U < Level -> true ; print_with_max_depth(X,10),nl(user_output)))),
164 retractall(debug_format(_,_,_)),
165 assertz((debug_format(U,A,V) :- (U < Level -> true ; format(user_output,A,V)))),
166 format(user_output,'Debugging mode: On : ~w~n',[Level]).
167 tcltk_turn_debugging_on(_).
168
169 tcltk_turn_debugging_off :-
170 retract(debug_mode(on)), !, assertz(debug_mode(off)),
171 retractall(debug_nl(_)),assertz(debug_nl(_)),
172 retractall(debug_print(_,_)), assertz(debug_print(_,_)),
173 retractall(debug_println(_,_)), assertz(debug_println(_,_)),
174 retractall(debug_format(_,_,_)), assertz(debug_format(_,_,_)),
175 format(user_output,'Debugging mode: Off~n',[]).
176 tcltk_turn_debugging_off.
177
178 trace_in_debug_mode :- (debug_mode(off) -> true ; trace).
179
180 print_with_max_depth(X,Max) :- write_term(user_output,X,
181 [max_depth(Max),portrayed(true)]).
182 print_quoted_with_max_depth(X,Max) :- write_term(user_output,X,
183 [quoted(true),ignore_ops(true),
184 max_depth(Max),portrayed(true)]).
185 print_quoted(X) :- write_term(user_output,X,
186 [quoted(true),ignore_ops(true),
187 max_depth(0),
188 numbervars(true),portrayed(true)]).
189 print_quoted(Stream,X) :- write_term(Stream,X,
190 [quoted(true),ignore_ops(true), % we could add quoted_charset(portable)
191 max_depth(0),
192 numbervars(true),portrayed(true)]).
193
194
195 debug_stats(Info) :- debug_mode(on),!,print(Info),print_debug_stats.
196 debug_stats(_).
197
198 print_debug_stats :-
199 tools:print_memory_used_wo_gc,
200 statistics(runtime,[RT,_]), print(' run/wall = '),print(RT),
201 statistics(walltime,[WT,_]), print('/'),print(WT),print(' ms'),nl.
202
203
204 time_if_debug(Call) :- debug_mode(off),!,Call.
205 time_if_debug(Call) :- time(Call).
206
207 :- use_module(tools_printing,[print_term_summary/1]).
208 time(Call) :-
209 nl,write('Calling: '),print_term_summary(Call),
210 time_raw(Call,Tot,TotT,TotW),
211 write('Exit: '), print_term_summary(Call),
212 format('Runtime: ~w ms, (~w ms total, ~w ms walltime)~n',[Tot,TotT,TotW]),
213 tools:print_memory_used_wo_gc,nl.
214
215
216 time_with_msg(Msg,Call) :-
217 format(user_output,'Starting: ~w~n',[Msg]),
218 time_raw(Call,Tot,TotT,TotW),
219 format(user_output,'Finished: ~w, runtime: ~w ms, (~w ms total, ~w ms walltime)~n',[Msg,Tot,TotT,TotW]),
220 tools:print_memory_used_wo_gc,nl.
221
222
223 time_raw(Call,Tot,TotT,TotW) :-
224 statistics(runtime,[Start,_]),
225 statistics(total_runtime,[StartT,_]),
226 statistics(walltime,[StartW,_]),
227 call(Call),
228 statistics(runtime,[End,_]),
229 statistics(total_runtime,[EndT,_]),
230 statistics(walltime,[EndW,_]),
231 Tot is End-Start,
232 TotT is EndT-StartT,
233 TotW is EndW-StartW.
234
235 :- volatile sol_found/2.
236 :- dynamic det_id/1, sol_found/2.
237 det_id(0).
238 gen_det_id(X) :- retract(det_id(X)), X1 is X+1,
239 assertz(det_id(X1)).
240
241 watch_det(Limit,Call) :-
242 gen_det_id(ID),
243 watch(Limit,Call),
244 (retract(sol_found(ID,Nr))
245 -> nl,print('*** '), print_term_summary(Call),
246 print('*** NON-DETERMINATE SOLUTION #'),
247 N1 is Nr+1, print(N1),nl,nl,
248 assertz(sol_found(ID,N1)) %,trace
249 ; assertz(sol_found(ID,1))
250 ).
251
252 det_check(Call) :- det_check(Call,true).
253
254 det_check(Call,ErrCode) :-
255 gen_det_id(ID),
256 call(Call), %tools:print_bt_message(det_check_sol(ID,Call)),
257 (retract(sol_found(ID,Nr))
258 -> nl,print('*** '), print_term_summary(Call),
259 print('*** NON-DETERMINATE SOLUTION #'),
260 N1 is Nr+1, print(N1),nl,
261 assertz(sol_found(ID,N1)),
262 call(ErrCode),
263 nl
264 ; assertz(sol_found(ID,1))
265 ).
266
267 watch(Call) :- watch(30,Call).
268
269 watch(Limit,Call) :-
270 statistics(runtime,[Start,_]),
271 call(Call),
272 statistics(runtime,[End,_]),
273 Tot is End-Start,
274 (Tot>Limit -> nl, %nl,print(Call),nl,
275 print('*** '),print_term_summary(Call),
276 print('*** exceeded limit: '), print(Tot), print(' ms'),nl
277 ; true).
278
279 % print new line with time info
280 nl_time :- statistics(runtime,[Start,SinceLast]), statistics(walltime,[WStart,WSinceLast]),
281 (WSinceLast >= 1000 -> Xs = ' ***long***'
282 ; WSinceLast >= 100 -> Xs = ' **'
283 ; Xs = ''),
284 format(user_output,' [total runtime ~w ms, delta: ~w ms (total walltime: ~w ms, delta: ~w ms)~w]~n',[Start,SinceLast,WStart,WSinceLast,Xs]).
285
286 debug_nl_time(Msg) :- (debug_mode(on) -> print(Msg), nl_time ; true).
287
288 % ------------------------
289 % utility similar to det_check; but allows to set spy point for tracing and checks redo
290
291 :- volatile pp_nr/1, spy_pp/1, pp_goal/2.
292 :- dynamic pp_nr/1, spy_pp/1, pp_goal/2.
293 % debug:reset_pp(XX).
294 pp_nr(0). pp_goal(0,true).
295 %spy_pp(115).
296 reset_pp(Spy) :- retract(pp_nr(_)), assertz(pp_nr(0)),
297 retractall(pp_goal(_,_)), assertz(spy_pp(Spy)).
298 :- volatile sol_found/1.
299 :- dynamic sol_found/1.
300 new_pp(C,Nr) :- retract(pp_nr(X)), Nr is X+1, assertz(pp_nr(Nr)),
301 assertz(pp_goal(Nr,C)),
302 (spy_pp(Nr) -> trace ; true).
303 new_sol(S,Nr) :- new_sol_no_redo(S,Nr).
304 new_sol(S,Nr) :- %spy_pp(Nr),
305 print('### REDO PP: '), print(Nr),nl,
306 print('### SOL: '), print(S),nl,spy_trace(Nr),
307 fail.
308
309 new_sol_no_redo(S,Nr) :-
310 (sol_found(Nr) -> nl,print('### Non-Deterministic Program Point !'),nl,
311 print('### '), print(Nr),nl,
312 print('### GOAL: '), pp_goal(Nr,G), print(G),nl,
313 print('### SOL: '), print(S),nl,
314 nl,trace
315 ; assertz(sol_found(Nr)), % print(sol_found(Nr,S)),nl,nl,
316 spy_trace(Nr)
317 ).
318
319 spy_trace(Nr) :- (spy_pp(Nr) -> trace ; true).
320
321 % an if predicate which checks if the Test part is deterministic
322 if_det_check(Test,Then,Else) :- new_pp(if_det_check_test(Test),PP),
323 if_det_check_pp(Test,Then,Else,PP).
324 if_det_check_pp(Test,Then,_Else,PP) :-
325 Test,
326 new_sol_no_redo(Test,PP),
327 Then.
328 if_det_check_pp(_Test,_Then,Else,PP) :- \+ sol_found(PP), Else.
329
330 % -------------------------------------------
331 :- dynamic hit_counter_fact/1.
332 hit_counter_fact(1).
333
334 % call to get and increase hit_counter; can be used to set trace spy points
335 hit_counter(X) :- retract(hit_counter_fact(X)), X1 is X+1, assertz(hit_counter_fact(X1)).
336
337 % -------------------------------------------
338
339
340 :- dynamic tdepth/1.
341 tdepth(0).
342 :- public trace_point/1.
343 trace_point(E) :- retract(tdepth(T)), T1 is T+1, assertz(tdepth(T1)),
344 print_tab(T), print('> ENTER '),print(T), print(' : '),print_term_summary(E),nl.
345 trace_point(E) :- retract(tdepth(T1)), T is T1-1, assertz(tdepth(T)),
346 print_tab(T), print('> exit '), print(T), print(' : '), print_term_summary(E),nl,fail.
347 :- public print_tabs/0.
348 print_tabs :- tdepth(T), print_tab(T).
349 print_tab(0) :- !, print(' ').
350 print_tab(N) :- N>0, print('|-+-'), N1 is N-1, print_tab(N1).
351
352 % -------------------------------------------
353
354 % use to log terms into a file (alternative to writeln_log)
355 log(Term) :- F='~/problog.pl',
356 open(F,append,S),
357 write_term(S,Term,[quoted(true)]), write(S,'.'),nl(S),
358 close(S).
359
360
361 % -------------------------------------------
362 :- use_module(library(terms),[term_hash/2]).
363 % succeeds or fails depending on Term and on bisect_list (a list of 0s and 1s)
364 bisect(Term,List) :- term_hash(Term,Hash),
365 (mods_match(List,Hash) -> print(match(List,Hash)),nl ; print(no_match(List,Hash)),nl,fail).
366
367 mods_match([],_).
368 mods_match(['*'|T],Hash) :- !, Hash2 is Hash//2, mods_match(T,Hash2).
369 mods_match([H|T],Hash) :- H is Hash mod 2,
370 Hash2 is Hash//2, mods_match(T,Hash2).
371
372
373 % -------------------------------------------
374
375 :- use_module(eventhandling,[register_event_listener/3]).
376 :- register_event_listener(reset_prob,reset_debug,
377 'Reset Debugging Mode just like after starup_prob').