2008-02-22 15:08:37 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: utilities for messing around in YAP internals. *
|
|
|
|
* comments: error messages for YAP *
|
|
|
|
* *
|
2008-03-24 23:48:47 +00:00
|
|
|
* Last rev: $Date: 2008-03-24 23:48:47 $,$Author: vsc $ *
|
2008-02-22 15:08:37 +00:00
|
|
|
* *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2017-10-27 13:50:40 +01:00
|
|
|
/**
|
2018-05-02 17:04:14 +01:00
|
|
|
* @file pl/hacks.yap
|
2017-10-27 13:50:40 +01:00
|
|
|
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
|
|
|
|
* @date Thu Oct 19 12:02:56 2017
|
2018-02-07 21:48:37 +00:00
|
|
|
*
|
2017-10-27 13:50:40 +01:00
|
|
|
* @brief Low-level access
|
|
|
|
*
|
|
|
|
* @defgroup Hacks Low-level access
|
|
|
|
* @ingroup builtins
|
2018-02-07 21:48:37 +00:00
|
|
|
*
|
2017-10-27 13:50:40 +01:00
|
|
|
*/
|
2016-01-20 22:36:16 +00:00
|
|
|
|
|
|
|
|
2008-02-22 15:08:37 +00:00
|
|
|
:- module('$hacks',
|
|
|
|
[display_stack_info/4,
|
|
|
|
display_stack_info/6,
|
2015-08-18 21:06:36 +01:00
|
|
|
display_pc/4,
|
2015-12-15 09:28:43 +00:00
|
|
|
fully_strip_module/3,
|
2014-08-04 15:45:30 +01:00
|
|
|
code_location/3]).
|
2008-02-22 15:08:37 +00:00
|
|
|
|
2015-03-04 09:40:21 +00:00
|
|
|
/** hacks:context_variables(-NamedVariables)
|
|
|
|
Access variable names.
|
|
|
|
|
|
|
|
Unify NamedVariables with a list of terms _Name_=_V_
|
|
|
|
giving the names of the variables occurring in the last term read.
|
|
|
|
Notice that variable names option must have been on.
|
|
|
|
*/
|
|
|
|
|
2018-04-27 13:01:08 +01:00
|
|
|
yap_hacks:context_variables(NamedVariables) :-
|
2015-03-04 09:40:21 +00:00
|
|
|
'$context_variables'(NamedVariables).
|
|
|
|
|
|
|
|
|
2014-08-04 15:45:30 +01:00
|
|
|
prolog:'$stack_dump' :-
|
2010-04-29 10:29:24 +01:00
|
|
|
yap_hacks:current_choicepoints(CPs),
|
|
|
|
yap_hacks:current_continuations([Env|Envs]),
|
|
|
|
yap_hacks:continuation(Env,_,ContP,_),
|
|
|
|
length(CPs, LCPs),
|
|
|
|
length(Envs, LEnvs),
|
|
|
|
format(user_error,'~n~n~tStack Dump~t~40+~n~nAddress~tChoiceP~16+ Clause Goal~n',[LCPs,LEnvs]),
|
|
|
|
display_stack_info(CPs, Envs, 20, ContP, StackInfo, []),
|
|
|
|
run_formats(StackInfo, user_error).
|
|
|
|
|
|
|
|
run_formats([], _).
|
|
|
|
run_formats([Com-Args|StackInfo], Stream) :-
|
|
|
|
format(Stream, Com, Args),
|
2014-02-09 10:47:44 +00:00
|
|
|
run_formats(StackInfo, Stream).
|
2010-04-29 10:29:24 +01:00
|
|
|
|
2008-02-22 15:08:37 +00:00
|
|
|
display_stack_info(CPs,Envs,Lim,PC) :-
|
2014-10-07 01:35:41 +01:00
|
|
|
display_stack_info(CPs,Envs,Lim,PC,Lines,[]),
|
2008-02-22 15:08:37 +00:00
|
|
|
flush_output(user_output),
|
|
|
|
flush_output(user_error),
|
|
|
|
print_message_lines(user_error, '', Lines).
|
|
|
|
|
|
|
|
code_location(Info,Where,Location) :-
|
|
|
|
integer(Where) , !,
|
2015-09-21 23:05:36 +01:00
|
|
|
pred_for_code(Where,Name,Arity,Mod,Clause),
|
2008-02-22 15:08:37 +00:00
|
|
|
construct_code(Clause,Name,Arity,Mod,Info,Location).
|
2014-10-07 01:35:41 +01:00
|
|
|
code_location(Info,_,Info).
|
2008-02-22 15:08:37 +00:00
|
|
|
|
|
|
|
construct_code(-1,Name,Arity,Mod,Where,Location) :- !,
|
|
|
|
number_codes(Arity,ArityCode),
|
|
|
|
atom_codes(ArityAtom,ArityCode),
|
|
|
|
atom_concat([Where,' at ',Mod,':',Name,'/',ArityAtom,' at indexing code'],Location).
|
|
|
|
construct_code(0,_,_,_,Location,Location) :- !.
|
|
|
|
construct_code(Cl,Name,Arity,Mod,Where,Location) :-
|
|
|
|
number_codes(Arity,ArityCode),
|
|
|
|
atom_codes(ArityAtom,ArityCode),
|
|
|
|
number_codes(Cl,ClCode),
|
|
|
|
atom_codes(ClAtom,ClCode),
|
|
|
|
atom_concat([Where,' at ',Mod,':',Name,'/',ArityAtom,' (clause ',ClAtom,')'],Location).
|
|
|
|
|
|
|
|
'$prepare_loc'(Info,Where,Location) :- integer(Where), !,
|
2015-09-21 23:05:36 +01:00
|
|
|
pred_for_code(Where,Name,Arity,Mod,Clause),
|
2008-02-22 15:08:37 +00:00
|
|
|
'$construct_code'(Clause,Name,Arity,Mod,Info,Location).
|
|
|
|
'$prepare_loc'(Info,_,Info).
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
display_pc(PC, PP, Source) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
{ integer(PC) },
|
2015-09-21 23:05:36 +01:00
|
|
|
{ pred_for_code(PC,Name,Arity,Mod,Clause) },
|
|
|
|
pc_code(Clause, PP, Name, Arity, Mod, Source).
|
2008-02-22 15:08:37 +00:00
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
pc_code(0,_PP,_Name,_Arity,_Mod, 'top level or system code' - []) --> !.
|
|
|
|
pc_code(-1,_PP,Name,Arity,Mod, '~a:~q/~d' - [Mod,Name,Arity]) --> !,
|
2015-08-18 21:06:36 +01:00
|
|
|
{ functor(S, Name,Arity),
|
|
|
|
nth_clause(Mod:S,1,Ref),
|
|
|
|
clause_property(Ref, file(File)),
|
2016-08-20 03:34:24 +01:00
|
|
|
clause_property(Ref, line_count(Line)) },
|
2015-08-18 21:06:36 +01:00
|
|
|
[ '~a:~d:0, ' - [File,Line] ].
|
|
|
|
pc_code(Cl,Name,Arity,Mod, 'clause ~d for ~a:~q/~d'-[Cl,Mod,Name,Arity]) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
{ Cl > 0 },
|
2015-08-18 21:06:36 +01:00
|
|
|
{ functor(S, Name,Arity),
|
|
|
|
nth_clause(Mod:S,Cl,Ref),
|
|
|
|
clause_property(Ref, file(File)),
|
2016-08-20 03:34:24 +01:00
|
|
|
clause_property(Ref, line_count(Line)) },
|
2015-08-18 21:06:36 +01:00
|
|
|
[ '~a:~d:0, ' - [File,Line] ].
|
2008-02-22 15:08:37 +00:00
|
|
|
|
|
|
|
display_stack_info(_,_,0,_) --> !.
|
|
|
|
display_stack_info([],[],_,_) --> [].
|
|
|
|
display_stack_info([CP|CPs],[],I,_) -->
|
|
|
|
show_lone_cp(CP),
|
|
|
|
{ I1 is I-1 },
|
|
|
|
display_stack_info(CPs,[],I1,_).
|
|
|
|
display_stack_info([],[Env|Envs],I,Cont) -->
|
|
|
|
show_env(Env, Cont, NCont),
|
|
|
|
{ I1 is I-1 },
|
|
|
|
display_stack_info([], Envs, I1, NCont).
|
|
|
|
display_stack_info([CP|LCPs],[Env|LEnvs],I,Cont) -->
|
2008-03-24 23:48:47 +00:00
|
|
|
{
|
|
|
|
yap_hacks:continuation(Env, _, NCont, CB),
|
|
|
|
I1 is I-1
|
|
|
|
},
|
2008-02-22 15:08:37 +00:00
|
|
|
( { CP == Env, CB < CP } ->
|
|
|
|
% if we follow choice-point and we cut to before choice-point
|
|
|
|
% we are the same goal
|
2010-04-29 10:29:24 +01:00
|
|
|
show_cp(CP, ''), %
|
2008-02-22 15:08:37 +00:00
|
|
|
display_stack_info(LCPs, LEnvs, I1, NCont)
|
|
|
|
;
|
|
|
|
{ CP > Env } ->
|
2010-04-29 10:29:24 +01:00
|
|
|
show_cp(CP, ' < '),
|
2008-02-22 15:08:37 +00:00
|
|
|
display_stack_info(LCPs,[Env|LEnvs],I1,Cont)
|
|
|
|
;
|
|
|
|
show_env(Env,Cont,NCont),
|
|
|
|
display_stack_info([CP|LCPs],LEnvs,I1,NCont)
|
|
|
|
).
|
|
|
|
|
|
|
|
show_cp(CP, Continuation) -->
|
2008-03-17 18:31:16 +00:00
|
|
|
{ yap_hacks:choicepoint(CP, Addr, Mod, Name, Arity, Goal, ClNo) },
|
2008-02-22 15:08:37 +00:00
|
|
|
( { Goal = (_;_) }
|
|
|
|
->
|
2010-04-29 10:29:24 +01:00
|
|
|
{ scratch_goal(Name,Arity,Mod,Caller) },
|
|
|
|
[ '0x~16r~t*~16+ ~d~16+ ~q ~n'-
|
|
|
|
[Addr, ClNo, Caller] ]
|
2016-08-20 03:34:24 +01:00
|
|
|
|
2008-02-22 15:08:37 +00:00
|
|
|
;
|
2010-04-29 10:29:24 +01:00
|
|
|
[ '0x~16r~t *~16+~a ~d~16+ ~q:' -
|
2008-02-22 15:08:37 +00:00
|
|
|
[Addr, Continuation, ClNo, Mod]]
|
|
|
|
),
|
2010-04-29 10:29:24 +01:00
|
|
|
{ prolog_flag( debugger_print_options, Opts) },
|
2008-02-22 15:08:37 +00:00
|
|
|
{clean_goal(Goal,Mod,G)},
|
2008-08-25 15:20:18 +01:00
|
|
|
['~@.~n' - write_term(G,Opts)].
|
2008-02-22 15:08:37 +00:00
|
|
|
|
|
|
|
show_env(Env,Cont,NCont) -->
|
|
|
|
{
|
2008-03-17 18:31:16 +00:00
|
|
|
yap_hacks:continuation(Env, Addr, NCont, _),
|
2013-02-08 16:36:45 +00:00
|
|
|
format('0x~16r 0x~16r~n',[Env,NCont]),
|
2008-03-17 18:31:16 +00:00
|
|
|
yap_hacks:cp_to_predicate(Cont, Mod, Name, Arity, ClId)
|
2008-02-22 15:08:37 +00:00
|
|
|
},
|
2010-04-29 10:29:24 +01:00
|
|
|
[ '0x~16r~t ~16+ ~d~16+ ~q:' -
|
2008-02-22 15:08:37 +00:00
|
|
|
[Addr, ClId, Mod] ],
|
|
|
|
{scratch_goal(Name, Arity, Mod, G)},
|
2010-04-29 10:29:24 +01:00
|
|
|
{ prolog_flag( debugger_print_options, Opts) },
|
2008-08-25 15:20:18 +01:00
|
|
|
['~@.~n' - write_term(G,Opts)].
|
2008-02-22 15:08:37 +00:00
|
|
|
|
|
|
|
clean_goal(G,Mod,NG) :-
|
|
|
|
beautify_hidden_goal(G,Mod,[NG],[]), !.
|
|
|
|
clean_goal(G,_,G).
|
|
|
|
|
2010-04-29 10:29:24 +01:00
|
|
|
scratch_goal(N,0,Mod,Mod:N) :-
|
|
|
|
!.
|
2008-02-22 15:08:37 +00:00
|
|
|
scratch_goal(N,A,Mod,NG) :-
|
|
|
|
list_of_qmarks(A,L),
|
|
|
|
G=..[N|L],
|
2008-03-17 18:31:16 +00:00
|
|
|
(
|
|
|
|
beautify_hidden_goal(G,Mod,[NG],[])
|
|
|
|
;
|
|
|
|
G = NG
|
|
|
|
),
|
|
|
|
!.
|
2008-02-22 15:08:37 +00:00
|
|
|
|
2008-03-17 18:31:16 +00:00
|
|
|
list_of_qmarks(0,[]) :- !.
|
2008-02-22 15:08:37 +00:00
|
|
|
list_of_qmarks(I,[?|L]) :-
|
|
|
|
I1 is I-1,
|
|
|
|
list_of_qmarks(I1,L).
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
fully_strip_module( T, M, TF) :-
|
|
|
|
'$yap_strip_module'( T, M, TF).
|
|
|
|
|
2008-02-22 15:08:37 +00:00
|
|
|
|
2014-10-07 01:35:41 +01:00
|
|
|
beautify_hidden_goal('$yes_no'(G,_Query), prolog) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
!,
|
|
|
|
{ Call =.. [(?), G] },
|
|
|
|
[Call].
|
|
|
|
beautify_hidden_goal('$do_yes_no'(G,Mod), prolog) -->
|
|
|
|
[Mod:G].
|
|
|
|
beautify_hidden_goal('$query'(G,VarList), prolog) -->
|
|
|
|
[query(G,VarList)].
|
|
|
|
beautify_hidden_goal('$enter_top_level', prolog) -->
|
|
|
|
['TopLevel'].
|
|
|
|
% The user should never know these exist.
|
|
|
|
beautify_hidden_goal('$csult'(Files,Mod),prolog) -->
|
|
|
|
[reconsult(Mod:Files)].
|
|
|
|
beautify_hidden_goal('$use_module'(Files,Mod,Is),prolog) -->
|
|
|
|
[use_module(Mod,Files,Is)].
|
2008-10-23 22:17:45 +01:00
|
|
|
beautify_hidden_goal('$continue_with_command'(reconsult,V,P,G,Source),prolog) -->
|
|
|
|
['Assert'(G,V,P,Source)].
|
|
|
|
beautify_hidden_goal('$continue_with_command'(consult,V,P,G,Source),prolog) -->
|
|
|
|
['Assert'(G,V,P,Source)].
|
|
|
|
beautify_hidden_goal('$continue_with_command'(top,V,P,G,_),prolog) -->
|
|
|
|
['Query'(G,V,P)].
|
|
|
|
beautify_hidden_goal('$continue_with_command'(Command,V,P,G,Source),prolog) -->
|
|
|
|
['TopLevel'(Command,G,V,P,Source)].
|
2008-02-22 15:08:37 +00:00
|
|
|
beautify_hidden_goal('$system_catch'(G,Mod,Exc,Handler),prolog) -->
|
|
|
|
[catch(Mod:G, Exc, Handler)].
|
|
|
|
beautify_hidden_goal('$catch'(G,Exc,Handler),prolog) -->
|
|
|
|
[catch(G, Exc, Handler)].
|
2018-02-07 21:48:37 +00:00
|
|
|
beautify_hidden_goal('$execute_command'(Query,M,V,P,Option,Source),prolog) -->
|
|
|
|
[toplevel_query(M:Query, V, P, Option, Source)].
|
2016-08-20 03:34:24 +01:00
|
|
|
beautify_hidden_goal('$process_directive'(Gs,_Mode,_VL),prolog) -->
|
|
|
|
[(:- Gs)].
|
2008-02-22 15:08:37 +00:00
|
|
|
beautify_hidden_goal('$loop'(Stream,Option),prolog) -->
|
|
|
|
[execute_load_file(Stream, consult=Option)].
|
|
|
|
beautify_hidden_goal('$load_files'(Files,Opts,?),prolog) -->
|
|
|
|
[load_files(Files,Opts)].
|
|
|
|
beautify_hidden_goal('$load_files'(_,_,Name),prolog) -->
|
|
|
|
[Name].
|
|
|
|
beautify_hidden_goal('$reconsult'(Files,Mod),prolog) -->
|
|
|
|
[reconsult(Mod:Files)].
|
2014-10-07 01:35:41 +01:00
|
|
|
beautify_hidden_goal('$undefp'([Mod|G]),prolog) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
['CallUndefined'(Mod:G)].
|
|
|
|
beautify_hidden_goal('$undefp'(?),prolog) -->
|
|
|
|
['CallUndefined'(?:?)].
|
|
|
|
beautify_hidden_goal(repeat,prolog) -->
|
|
|
|
[repeat].
|
|
|
|
beautify_hidden_goal('$recorded_with_key'(A,B,C),prolog) -->
|
2014-08-20 15:13:19 +01:00
|
|
|
[recorded(A,B,C)].
|
2008-02-22 15:08:37 +00:00
|
|
|
beautify_hidden_goal('$findall_with_common_vars'(Templ,Gen,Answ),prolog) -->
|
|
|
|
[findall(Templ,Gen,Answ)].
|
|
|
|
beautify_hidden_goal('$bagof'(Templ,Gen,Answ),prolog) -->
|
|
|
|
[bagof(Templ,Gen,Answ)].
|
|
|
|
beautify_hidden_goal('$setof'(Templ,Gen,Answ),prolog) -->
|
|
|
|
[setof(Templ,Gen,Answ)].
|
|
|
|
beautify_hidden_goal('$findall'(T,G,S,A),prolog) -->
|
|
|
|
[findall(T,G,S,A)].
|
|
|
|
beautify_hidden_goal('$listing'(G,M,_Stream),prolog) -->
|
|
|
|
[listing(M:G)].
|
2014-10-07 01:35:41 +01:00
|
|
|
beautify_hidden_goal('$call'(G,_CP,?,M),prolog) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
[call(M:G)].
|
2014-10-07 01:35:41 +01:00
|
|
|
beautify_hidden_goal('$call'(_G,_CP,G0,M),prolog) -->
|
2008-02-22 15:08:37 +00:00
|
|
|
[call(M:G0)].
|
2014-11-25 12:03:48 +00:00
|
|
|
beautify_hidden_goal('$current_predicate'(Na,M,S,_),prolog) -->
|
|
|
|
[current_predicate(Na,M:S)].
|
2008-02-22 15:08:37 +00:00
|
|
|
beautify_hidden_goal('$list_clauses'(Stream,M,Pred),prolog) -->
|
2014-10-07 01:35:41 +01:00
|
|
|
[listing(Stream,M:Pred)].
|