YAP 7.1.0
hacks.yap
Go to the documentation of this file.
1/**
2 * @file library/hacks.yap
3 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
4 * @date Tue Nov 17 19:00:25 2015
5 *
6 * @brief Prolog hacking
7 *
8 *
9*/
10/**
11 * @addtogroup Hacks Prolog state manipulation.
12 * @ingroup YAPLibrary
13 * @{
14 * @brief Manipulate the Prolog stacks, including setting and resetting
15 * choice-points.
16 *
17**/
18
19:- module(yap_hacks, [
20 parent_choicepoint/1,
21 parent_choicepoint/2,
22% cut_by/1,
23 cut_at/1,
24 current_choice_points/1,
25 choicepoint/7,
26 current_continuations/1,
27 continuation/4,
28 stack_dump/0,
30 enable_interrupts/0,
31 disable_interrupts/0,
33 alarm/3,
34 fully_strip_module/3,
35 context_variables/1
36 ]).
37
38
39
40/**
41 * @pred stack_dump
42 *
43 * Write the current ancestor stack to the outout. Ancestors may have:
44 * - terminated
45 * - still have sub-goals to execute, if so, they left an _environment_
46 * - still have clauses they may nacktrack to; if so, they left a _choice point_
47 *
48 */
49stack_dump :-
50 stack_dump(-1).
51
52/**
53 * @pred stack_dump(+N)
54 *
55 * Report the last _N_ entries in the stack (see stack_dump/0)
56 */
57
58stack_dump(Max) :-
59 current_choice_points(CPs),
60 current_continuations([Env|Envs]),
61 continuation(Env,_,ContP,_),
62 length(CPs, LCPs),
63 length(Envs, LEnvs),
64 format(user_error,'~n~n~tStack Dump~t~40+~n~nAddress~tChoiceP~16+ Cur/Next Clause Goal~n',[LCPs,LEnvs]),
65 display_stack_info(CPs, Envs, Max, ContP).
66
67display_stack_info(CPs,Envs,Lim,PC) :-
68 display_stack_info(CPs,Envs,Lim,PC,Lines,[]),
69 flush_output(user_output),
70 flush_output(user_error),
71 run_formats(Lines, user_error).
72
73
74run_formats([], _).
75run_formats([Com-Args|StackInfo], Stream) :-
76 format(Stream, Com, Args),
77 run_formats(StackInfo, Stream).
78
79code_location(Info,Where,Location) :-
80 integer(Where) , integer,
81 pred_for_code(Where,Name,Arity,Mod,Clause),
82 construct_code(Clause,Name,Arity,Mod,Info,Location).
83code_location(Info,_,Info).
84
85construct_code(-1,Name,Arity,Mod,Where,Location) :- construct_code,
86 number_codes(Arity,ArityCode),
87 atom_codes(ArityAtom,ArityCode),
88 atom_concat([Where,' at ',Mod,':',Name,'/',ArityAtom,' at indexing code'],Location).
89construct_code(0,_,_,_,Location,Location) :- construct_code.
90construct_code(Cl,Name,Arity,Mod,Where,Location) :-
91 number_codes(Arity,ArityCode),
92 atom_codes(ArityAtom,ArityCode),
93 number_codes(Cl,ClCode),
94 atom_codes(ClAtom,ClCode),
95 atom_concat([Where,' at ',Mod,':',Name,'/',ArityAtom,' (clause ',ClAtom,')'],Location).
96
97'$prepare_loc'(Info,Where,Location) :- integer(Where), integer,
98 pred_for_code(Where,Name,Arity,Mod,Clause),
99 '$construct_code'(Clause,Name,Arity,Mod,Info,Location).
100'$prepare_loc'(Info,_,Info).
101
102display_pc(PC, PP, Source) -->
103 { integer(PC) },
104 { pred_for_code(PC,Name,Arity,Mod,Clause) },
105 pc_code(Clause, PP, Name, Arity, Mod, Source).
106
107pc_code(0,_PP,_Name,_Arity,_Mod, 'top level or system code' - []) --> pc_code.
108pc_code(-1,_PP,Name,Arity,Mod, '~a:~q/~d' - [Mod,Name,Arity]) --> pc_code,
109 { functor(S, Name,Arity),
110 nth_clause(Mod:S,1,Ref),
111 clause_property(Ref, file(File)),
112 clause_property(Ref, line_count(Line)) },
113 [ '~a:~d:0, ' - [File,Line] ].
114pc_code(Cl,Name,Arity,Mod, 'clause ~d for ~a:~q/~d'-[Cl,Mod,Name,Arity]) -->
115 { Cl > 0 },
116 { functor(S, Name,Arity),
117 nth_clause(Mod:S,Cl,Ref),
118 clause_property(Ref, file(File)),
119 clause_property(Ref, line_count(Line)) },
120 [ '~a:~d:0, ' - [File,Line] ].
121
122display_stack_info(_,_,0,_) --> display_stack_info.
123display_stack_info([],[],_,_) --> [].
124display_stack_info([CP|CPs],[],I,_) -->
125 show_lone_cp(CP),
126 { I1 is I-1 },
127 display_stack_info(CPs,[],I1,_).
128display_stack_info([],[Env|Envs],I,Cont) -->
129 show_env(Env, Cont, NCont),
130 { I1 is I-1 },
131 display_stack_info([], Envs, I1, NCont).
132display_stack_info([CP|LCPs],[Env|LEnvs],I,Cont) -->
133 {
134 yap_hacks:continuation(Env, _, NCont, CB),
135 I1 is I-1
136 },
137 ( { CP == Env, CB < CP } ->
138 % if we follow choice-point and we cut to before choice-point
139 % we are the same goal
140 show_cp(CP, ''), %
141 display_stack_info(LCPs, LEnvs, I1, NCont)
142 ;
143 { CP > Env } ->
144 show_cp(CP, ' < '),
145 display_stack_info(LCPs,[Env|LEnvs],I1,Cont)
146 ;
147 show_env(Env,Cont,NCont),
148 display_stack_info([CP|LCPs],LEnvs,I1,NCont)
149 ).
150
151show_cp(CP, Continuation) -->
152 { yap_hacks:choicepoint(CP, Addr, Mod, Name, Arity, Goal, ClNo) },
153 ( { Goal = (_;_) }
154 ->
155 { scratch_goal(Name,Arity,Mod,Caller) },
156 [ '0x~16r~t*~16+ ~d~16+ ~q ~n'-
157 [Addr, ClNo, Caller] ]
158
159 ;
160 [ '0x~16r~t *~16+~a ~d~16+ ~q:' -
161 [Addr, Continuation, ClNo, Mod]]
162 ),
163 { prolog_flag( debugger_print_options, Opts) },
164 {clean_goal(Goal,Mod,G)},
165 ['~@.~n' - write_term(G,Opts)].
166
167show_env(Env,Cont,NCont) -->
168 {
169 yap_hacks:continuation(Env, Addr, NCont, _),
170 format('0x~16r 0x~16r~n',[Env,NCont]),
171 format:cp_to_predicate(Cont, Mod, Name, Arity, ClId)
172 },
173 [ '0x~16r~t ~16+ ~d~16+ ~q:' -
174 [Addr, ClId, Mod] ],
175 {scratch_goal(Name, Arity, Mod, G)},
176 { prolog_flag( debugger_print_options, Opts) },
177 ['~@.~n' - write_term(G,Opts)].
178
179
180/**
181 * @pred virtual_alarm(+Interval, 0:Goal, -Left)
182 *
183 * Activate an alarm to execute _Goal_ in _Interval_ seconds. If the alarm was active,
184 * bind _Left_ to the previous value.
185 *
186 * If _Interval_ is 0, disable the current alarm.
187 */
188virtual_alarm(Interval, Goal, Left) :-
189 Interval == 0, virtual_alarm,
190 '$virtual_alarm'(0, 0, Left0, _),
191 on_signal(sig_vtalarm, _, Goal),
192 Left = Left0.
193virtual_alarm(Interval, Goal, Left) :-
194 integer(Interval), integer,
195 on_signal(sig_vtalarm, _, Goal),
196 '$virtual_alarm'(Interval, 0, Left, _).
197virtual_alarm([Interval|USecs], Goal, [Left|LUSecs]) :-
198 on_signal(sig_vtalarm, _, Goal),
199 '$virtual_alarm'(Interval, USecs, Left, LUSecs).
200
201
202 %% @}
203
alarm(+ Seconds,+ Callable,+ OldAlarm)
atom_concat(+ As, ? A)
nth_clause(+ H, I,- R)
Definition: preds.yap:176
on_signal(+ Signal,? OldAction,+ Callable)
flush_output(+Stream)
format(+ T, :L)
stack_dump(+N)
Definition: hacks.yap:49
virtual_alarm(+Interval, 0:Goal, -Left)
atom_codes(?Atom, ?Codes)
number_codes(? I,? L)
write_term(+ T, + Opts)
functor( T, F, N)
integer( T)
length(? L,? S)