1/* Part of LogicMOO Base Logicmoo Debug Tools
    2% ===================================================================
    3% File '$FILENAME.pl'
    4% Purpose: An Implementation in SWI-Prolog of certain debugging tools
    5% Maintainer: Douglas Miles
    6% Contact: $Author: dmiles $@users.sourceforge.net ;
    7% Version: '$FILENAME.pl' 1.0.0
    8% Revision: $Revision: 1.1 $
    9% Revised At:  $Date: 2002/07/11 21:57:28 $
   10% Licience: LGPL
   11% ===================================================================
   12*/
   13:- module(dumpst,[
   14          getPFA/3,getPFA1/3,getPFA2/3,get_m_opt/4,fdmsg/1,fdmsg1/1,
   15          neg1_numbervars/3,clauseST/2,
   16          dtrace/0,dbreak/0,
   17          dtrace/1,dtrace/2,
   18          dumptrace/1,dumptrace/2,dumptrace0/1,dumptrace1/1,
   19          dumptrace_ret/1,
   20          drain_framelist/1,
   21          drain_framelist_ele/1,
   22          printable_variable_name/2,
   23          v_name1/2,
   24          v_name2/2,
   25          dump_st/0,
   26          with_source_module/1,
   27          to_wmsg/2,
   28          fmsg_rout/1,
   29          simplify_goal_printed/2,
   30          dumpST/0,dumpST/1,dumpST1/0,
   31          dumpST0/0,dumpST0/1,dumpST0/2,
   32          dumpST9/0,dumpST9/1,dumpST9/2,dumpST_now/2,printFrame/3,frame_to_fmsg/4
   33
   34   ]).   35
   36:-  meta_predicate dumptrace_ret(?),
   37  neg1_numbervars(?, ?, 0),
   38  with_source_module(0),
   39  dumptrace_ret(0),
   40  dumptrace0(0),
   41  dumptrace1(0),
   42  dumptrace(0),
   43  dtrace(*,0).   44
   45:- set_module(class(library)).   46% % % OFF :- system:use_module(library(apply)).
   47% % % OFF :- system:reexport(library(logicmoo/util_strings)).
   48% % % OFF :- system:use_module(library(logicmoo_utils_all)).
   49% % % OFF :- system:use_module((ucatch)).
   50% % % OFF :- system:use_module(library(logicmoo/no_loops)).
   51% % % OFF :- system:use_module((rtrace)).
   52% % % OFF :- system:use_module(library(must_sanity)).
   53
   54
   55:- set_prolog_flag(backtrace_depth,      200).   56:- set_prolog_flag(backtrace_goal_depth, 20).   57:- set_prolog_flag(backtrace_show_lines, true).   58
   59:- module_transparent
   60          getPFA/3,getPFA1/3,getPFA2/3,get_m_opt/4,fdmsg/1,fdmsg1/1,
   61          neg1_numbervars/3,clauseST/2,
   62          % dtrace/0,
   63          dtrace/1,dtrace/2,
   64          dumptrace/1,dumptrace/2,
   65          dumptrace_ret/1,
   66          dump_st/0,
   67          dumpST/0,dumpST/1,
   68          dumpST0/0,dumpST0/1,dumpST0/2,
   69          dumpST9/0,dumpST9/1,dumpST9/2.   70
   71
   72:- ensure_loaded(library(debug)).   73% % % OFF :- system:use_module((dmsg)).% WAS OFF  :- system:use_module(library(logicmoo_util_strings)).
   74
   75
   76%= 	 	 
 dump_st is semidet
Dump Stack Trace.
   82dump_st:- prolog_current_frame(Frame),dumpST0(Frame,10).
   83
   84
   85%= 	 	 
 dumpST0 is semidet
Dump S True Stucture Primary Helper.
   91dumpST0:- dbreak, 
   92   prolog_current_frame(Frame),(tracing->zotrace((CU=dtrace,notrace));CU=true),dumpST0(Frame,800),!,CU.
   93
   94%= 	 	 
 dumpST0(?Opts) is semidet
Dump S True Stucture Primary Helper.
  100dumpST0(Opts):- once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)),dumpST0(Frame,Opts).
  101
  102%= 	 	 
 dumpST0(?Frame, ?MaxDepth) is semidet
Dump S True Stucture Primary Helper.
  108:- thread_local(tlbugger:ifHideTrace/0).  109dumpST0(_,_):- tlbugger:ifHideTrace,!.
  110dumpST0(Frame,MaxDepth):- ignore(MaxDepth=5000),Term = dumpST(MaxDepth),
  111   (var(Frame)->once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame));true),
  112   ignore(( get_prolog_backtrace(MaxDepth, Trace,[frame(Frame),goal_depth(13)]),
  113    format(user_error, '% dumpST ~p', [Term]), nl(user_error),
  114    attach_console,dtrace,
  115    dbreak,
  116
  117    print_prolog_backtrace(user_error, Trace,[subgoal_positions(true)]), nl(user_error), fail)),!.
  118
  119
  120
  121% dumpstack_arguments.
  122
  123%= 	 	 
 dumpST is semidet
Dump S True Stucture.
  129dumpST:- zotrace((prolog_current_frame(Frame),b_setval('$dump_frame',Frame),dumpST1)).
  130
  131
  132:- thread_local(tlbugger:no_slow_io/0).  133:- multifile(tlbugger:no_slow_io/0).  134
  135%= 	 	 
 dumpST1 is semidet
Dump S True Stucture Secondary Helper.
  141dumpST1:- current_prolog_flag(dmsg_level,never),!.
  142dumpST1:- tlbugger:no_slow_io,!,dumpST0,!.
  143dumpST1:- tlbugger:ifHideTrace,!.
  144dumpST1:- show_current_source_location,loop_check_early(dumpST9,dumpST0).
  145
  146%= 	 	 
 dumpST(?Depth) is semidet
Dump S True Stucture.
  152dumpST(Depth):- zotrace((prolog_current_frame(Frame),b_setval('$dump_frame',Frame))),
  153   loop_check_early(dumpST9(Depth),dumpST0(Depth)).
  154
  155
  156%= 	 	 
 get_m_opt(?Opts, ?Max_depth, ?D100, ?RetVal) is semidet
Get Module Opt.
  162get_m_opt(Opts,Max_depth,D100,RetVal):- univ_safe_2(E,[Max_depth,V]),(((member(E,Opts),nonvar(V)))->RetVal=V;RetVal=D100).
  163
  164
  165
  166%= 	 	 
 dumpST9 is semidet
Dump S T9.
  172dumpST9:- zotrace((once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)), dumpST9(Frame,5000))).
  173
  174%= 	 	 
 dumpST9(?Depth) is semidet
Dump S T9.
  180dumpST9(Depth):- once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)), dumpST9(Frame,Depth).
  181
  182
  183%= 	 	 
 dumpST9(?Frame, :TermMaxDepth) is semidet
Dump S T9.
  189dumpST9(_,_):- tlbugger:ifHideTrace,!.
  190dumpST9(Frame,MaxDepth):- integer(MaxDepth),!,dumpST_now(Frame,[max_depth(MaxDepth),numbervars(true),show([level,has_alternatives,hidden,context_module,goal,clause])]).
  191dumpST9(Frame,From-MaxDepth):- integer(MaxDepth),!,dumpST_now(Frame,[skip_depth(From),max_depth(MaxDepth),numbervars(true),show([level,has_alternatives,hidden,context_module,goal,clause])]).
  192dumpST9(Frame,List):- is_list(List),dumpST_now(Frame,[show([level,has_alternatives,hidden,context_module,goal,clause])|List]).
  193
  194
  195
  196%= 	 	 
 drain_framelist(?Opts) is semidet
Drain Framelist.
  202drain_framelist(Opts):- repeat, \+ drain_framelist_ele(Opts).
  203
  204
  205%= 	 	 
 drain_framelist_ele(?Opts) is semidet
Drain Framelist Ele.
  211drain_framelist_ele(Opts):- 
  212    nb_getval('$current_stack_frame_list',[N-Frame|Next]),
  213    nb_setval('$current_stack_frame_list',Next),!,
  214    printFrame(N,Frame,Opts),!.
  215    
  216        
  217
  218
  219%= 	 	 
 dumpST_now(?FrameIn, ?Opts) is semidet
Dump S True Stucture Now.
  225dumpST_now(FrameIn,Opts):-
  226  once(number(FrameIn);prolog_current_frame(FrameIn)),
  227   nb_setval('$hide_rest_frames',false),
  228   b_setval('$current_stack_frame_depth',0),
  229   b_setval('$current_stack_frame_list',[]),
  230   get_m_opt(Opts,max_depth,100,MD),
  231   b_setval('$current_stack_frame_handle',FrameIn),
  232  (repeat,  
  233     nb_getval('$current_stack_frame_depth',N),
  234     nb_getval('$current_stack_frame_handle',Frame),
  235    ((pushFrame(N,Frame,Opts),MD>N)-> 
  236     ((prolog_frame_attribute(Frame,parent,ParentFrame)->
  237       (nb_setval('$current_stack_frame_handle',ParentFrame),
  238       NN is N +1,nb_setval('$current_stack_frame_depth',NN),fail); !));
  239     (!))),
  240   drain_framelist(Opts),!.
 pushFrame(?N, ?Frame, ?Opts) is semidet
Push Frame.
  248pushFrame(N,Frame,_Opts):- nb_getval('$current_stack_frame_list',Current),nb_setval('$current_stack_frame_list',[N-Frame|Current]).
  249
  250
  251%= 	 	 
 printFrame(?N, ?Frame, ?Opts) is semidet
Print Frame.
  257printFrame(_,_,_):- nb_current('$hide_rest_frames',true),!.
  258printFrame(N,Frame,Opts):-
  259  ignore(((frame_to_fmsg(N,Frame,Opts,Out)),must(fmsg_rout(Out)))),!.
  260
  261
  262%= 	 	 
 frame_to_fmsg(?N, ?Frame, ?Opts, ?N) is semidet
Frame Converted To Functor Message.
  268frame_to_fmsg(N,Frame,Opts,[nf(max_depth,N,Frame,Opts)]):-get_m_opt(Opts,max_depth,100,MD),N>=MD,!,fail.
  269%  dumpST9(N,Frame,Opts,[nf(max_depth,N,Frame,Opts)]):-get_m_opt(Opts,skip_depth,100,SD),N=<SD,!.
  270frame_to_fmsg(_,Frame,Opts,[fr(Goal)]):- get_m_opt(Opts,show,goal,Ctrl),getPFA(Frame,Ctrl,Goal),!.
  271frame_to_fmsg(N,Frame,Opts,[nf(no(Ctrl),N,Frame,Opts)]):- get_m_opt(Opts,show,goal,Ctrl),!.
  272frame_to_fmsg(N,Frame,Opts,[nf(noFrame(N,Frame,Opts))]).
  273
  274 
  275
  276
  277%= 	 	 
 fmsg_rout(:TermRROut) is semidet
Functor Message Rout.
  283fmsg_rout([]):-!.
  284fmsg_rout([fr(E)|_]):- member(goal=GG,E),end_dump(GG),!,ignore(fdmsg(fr(E))),!.
  285fmsg_rout([fr(E)|_]):- member(goal=GG,E),end_dump(GG),!,ignore(fdmsg(fr(E))),!.
  286fmsg_rout([E|RROut]):- ignore(fdmsg(E)),!,fmsg_rout(RROut).
  287fmsg_rout(RROut):- show_call(why,forall(member(E,RROut),fdmsg(E))),!.
  288
  289
  290%= 	 	 
 neg1_numbervars(?Out, ?Start, :GoalROut) is semidet
Negated Secondary Helper Numbervars.
  296neg1_numbervars(T,-1,T):-!.
  297neg1_numbervars(Out,false,Out):-!.
  298neg1_numbervars(Out,true,ROut):-copy_term(Out,ROut),!,snumbervars(ROut,777,_).
  299neg1_numbervars(Out,Start,ROut):-copy_term(Out,ROut),integer(Start),!,snumbervars(ROut,Start,_).
  300neg1_numbervars(Out,safe,ROut):-copy_term(Out,ROut),safe_numbervars(ROut).
  301
  302if_defined_mesg_color(G,C):- current_predicate(mesg_color/2),mesg_color(G,C).
  303
  304%= 	 	 
 fdmsg1(?G) is semidet
Fdmsg Secondary Helper.
  310fdmsg1(txt(S)):-'format'(S,[]),!.
  311fdmsg1(level=L):-'format'('(~q)',[L]),!.
  312fdmsg1(context_module=G):- simplify_m(G,M),!,if_defined_mesg_color(G,Ctrl),ansicall(Ctrl,format('[~w]',[M])),!.
  313fdmsg1(has_alternatives=G):- (G==false->true;'format'('<*>',[])),!.
  314fdmsg1(hidden=G):- (G==false->true;'format'('$',[G])),!.
  315fdmsg1(goal=G):- do_fdmsg1(G).
  316fdmsg1(clause=[F,L]):- directory_file_path(_,FF,F),'format'('  %  ~w:~w: ',[FF,L]),!.
  317fdmsg1(clause=[F,L]):- fresh_line,'format'('%  ~w:~w: ',[F,L]),!.
  318fdmsg1(clause=[]):-'format'(' /*DYN*/ ',[]),!.
  319fdmsg1(G):- if_defined_mesg_color(G,Ctrl),ansicall(Ctrl,format(' ~q ',[G])),!.
  320fdmsg1(M):-dmsg(failed_fdmsg1(M)).
  321
  322do_fdmsg1(G):- 
  323  simplify_goal_printed(G,GG),!,
  324  (GG\==G->write('#');true),
  325  term_variables(GG,_Vars),
  326  copy_term_nat(GG,GGG), =(GG,GGG),
  327  numbervars(GGG,0,_,[attvar(skip)]),
  328  if_defined_mesg_color(GGG,Ctrl),ansicall(Ctrl,format(' ~q. ',[GGG])),!.
  329
  330
  331%= 	 	 
 simplify_m(?G, ?M) is semidet
Simplify Module.
  337simplify_m(G,M):-atom(G),sub_atom(G,_,6,0,M),!.
  338simplify_m(G,G).
  339
  340%= 	 	 
 fdmsg(?M) is semidet
Fdmsg.
  346fdmsg(fr(List)):-is_list(List),!,must((fresh_line,ignore(forall(member(E,List),fdmsg1(E))),nl)).
  347fdmsg(M):- logicmoo_util_catch:ddmsg(failed_fdmsg(M)).
  348
  349:- thread_local(tlbugger:plain_attvars/0).  350
  351:-export(simplify_goal_printed/2).  352
  353%= 	 	 
  354printable_variable_name(Var, Name) :- nonvar(Name),!,must(printable_variable_name(Var, NameO)),!,Name=NameO.
  355printable_variable_name(Var, Name) :- nonvar(Var),Var='$VAR'(_),format(atom(Name),"~w_",Var).
  356printable_variable_name(Var, Name) :- nonvar(Var),format(atom(Name),"(_~q_)",Var).
  357printable_variable_name(Var,Name):- (get_attr(Var, vn, Name1);
  358  get_attr(Var, varnames, Name1)),
  359 (var_property(Var,name(Name2))-> 
  360   (Name1==Name2-> atom_concat(Name1,'_VN',Name) ; Name=(Name1:Name2)); 
  361    (atom(Name1)->atom_concat('?',Name1,Name);
  362   format(atom(Name),"'$VaR'(~q)",Var))),!.
  363printable_variable_name(Var,Name):- v_name1(Var,Name),!.
  364printable_variable_name(Var,Name):- v_name2(Var,Name),!. % ,atom_concat(Name1,'_TL',Name).
  365
  366v_name1(Var,Name):- var_property(Var,name(Name)),!.
  367v_name1(Var,Name):- get_varname_list(Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  368v_name1(Var,Name):- nb_current('$old_variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  369v_name2(Var,Name):- get_varname_list(Vs),format(atom(Name),'~W',[Var, [variable_names(Vs)]]).
  370 
  371
  372%attrs_to_list(att(sk,_,ATTRS),[sk|List]):-!,attrs_to_list(ATTRS,List).
  373attrs_to_list(att(vn,_,ATTRS),List):-!,attrs_to_list(ATTRS,List).
  374attrs_to_list(att(M,V,ATTRS),[M=VV|List]):- locally(tlbugger:plain_attvars,simplify_goal_printed(V,VV)),!,attrs_to_list(ATTRS,List).
  375attrs_to_list([],[]).
  376attrs_to_list(_ATTRS,[]).
 simplify_goal_printed(:TermVar, :TermVar) is semidet
Simplify Goal Printed.
  383:- multifile(dumpst_hook:simple_rewrite/2).  384:- dynamic(dumpst_hook:simple_rewrite/2).  385
  386simplify_var_printed(Var,'$avar'('$VAR'(Name))):- tlbugger:plain_attvars,must(printable_variable_name(Var,Name)),!.
  387simplify_var_printed(Var,'$VAR'(Name)):- get_attrs(Var,att(vn, _, [])),printable_variable_name(Var, Name),!.
  388simplify_var_printed(Var,'$avar'('$VAR'(Name))):- tlbugger:plain_attvars,must(printable_variable_name(Var,Name)),!.
  389simplify_var_printed(Var,'$avar'(Dict)):- get_attrs(Var,ATTRS),must(printable_variable_name(Var,Name)),attrs_to_list(ATTRS,List),
  390                         dict_create(Dict,'$VAR'(Name),List).
  391simplify_var_printed(Var,'$VAR'(Name)):- is_ftVar(Var),!,printable_variable_name(Var, Name).
  392
  393simplify_goal_printed(Var,Var):-var(Var),!.
  394
  395simplify_goal_printed(I,O):- once(dumpst_hook:simple_rewrite(I,O)), I \== O.
  396simplify_goal_printed(Var,Name):-cyclic_term(Var),!,Name=Var.
  397simplify_goal_printed(Var,Name):-is_ftVar(Var),\+ current_prolog_flag(variable_names_bad,true),simplify_var_printed(Var,Name),!.
  398simplify_goal_printed(Var,Var):-var(Var),!.
  399simplify_goal_printed(setup_call_catcher_cleanup,scccu).
  400% simplify_goal_printed(existence_error(X,Y),existence_error(X,Y)):-nl,writeq(existence_error(X,Y)),nl,fail.
  401simplify_goal_printed(setup_call_cleanup,sccu).
  402simplify_goal_printed(existence_error,'existence_error_XXXXXXXXX__\e[0m\e[1;34m%-6s\e[m\'This is text\e[0mRED__existence_error_existence_error').
  403simplify_goal_printed(each_call_cleanup,eccu).
  404simplify_goal_printed(call_cleanup,ccu).
  405simplify_goal_printed([Var|_],'$'):-compound(Var),Var = (var_tracker(_) = _ ).
  406simplify_goal_printed([Var|_],'$'):-compound(Var),Var = (fbound(_) = _ ).
  407simplify_goal_printed(M:I,O):- atom(M),(M==user;M==system),!,simplify_goal_printed(I,O).
  408simplify_goal_printed(M:I,O):- atom(M),!,simplify_goal_printed(I,O).
  409%simplify_goal_printed(catch(I,V,_),O):- var(V),!,simplify_goal_printed(I,O).
  410simplify_goal_printed(always(I),O):- !,simplify_goal_printed(I,O).
  411simplify_goal_printed(must_det_lm(M,G),GS):-!,simplify_goal_printed(M:must_det_l(G),GS).
  412%simplify_goal_printed('<meta-call>'(G),GS):-!,simplify_goal_printed(G,GS).
  413%simplify_goal_printed(call(G),GS):-!,simplify_goal_printed(G,GS).
  414simplify_goal_printed(M:G,MS:GS):-atom(M), simplify_m(M,MS),!,simplify_goal_printed(G,GS).
  415simplify_goal_printed(dinterp(_,_,I,_),O):- !,simplify_goal_printed(I,O).
  416
  417
  418simplify_goal_printed(P,O):- compound(P),compound_name_arguments(P,F,[I]),
  419  atom_contains(F,must),!,simplify_goal_printed(I,O).
  420
  421simplify_goal_printed(call_term_expansion(_,A,_,B,_),O):- !, simplify_goal_printed(call_term_expansion_5('...',A,'...',B,'...'),O).
  422simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/',SA,A),!.
  423simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/home/dmiles/lib/swipl/pack/logicmoo_base/prolog/logicmoo/',SA,A),!.
  424simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/home/dmiles/lib/swipl/pack/logicmoo_base/t/',SA,A),!.
  425% simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/',_,A),!,directory_file_path(_,SA,A),!.
  426simplify_goal_printed(GOAL=A,AS):- goal==GOAL,!,simplify_goal_printed(A,AS).
  427simplify_goal_printed(Var,Var):- \+ compound(Var),!.
  428simplify_goal_printed(term_position(_,_,_,_,_),'$..term_position/4..$').
  429%simplify_goal_printed(user:G,GS):-!,simplify_goal_printed(G,GS).
  430%simplify_goal_printed(system:G,GS):-!,simplify_goal_printed(G,GS).
  431%simplify_goal_printed(catchv(G,_,_),GS):-!,simplify_goal_printed(G,GS).
  432%simplify_goal_printed(catch(G,_,_),GS):-!,simplify_goal_printed(G,GS).
  433%simplify_goal_printed(skolem(V,N,_F),GS):-!,simplify_goal_printed(skeq(V,N,'..'),GS).
  434
  435simplify_goal_printed(List,O):- current_prolog_flag(dmsg_len,Three),
  436  is_list(List),length(List,L),L>Three,
  437   append([A,B,C],[F|_],List),F \='...'(_), !, 
  438  simplify_goal_printed([A,B,C,'...'(_)],O).
  439
  440
  441simplify_goal_printed([E|OList],O):- \+ is_list(OList), 
  442   append(List,Open,OList),var(Open),!,
  443    current_prolog_flag(dmsg_len,Three),
  444   is_list(List),length(List,L),L>Three,
  445    append([A,B,C],[F|_],[E|List]),F \='...'(_), !, 
  446   simplify_goal_printed([A,B,C,'...'(_)],O).
  447
  448
  449simplify_goal_printed([F|A],[FS|AS]):- !,simplify_goal_printed(F,FS),simplify_goal_printed(A,AS).
  450simplify_goal_printed(G,GS):- univ_safe_2(G,[F|A]),maplist(simplify_goal_printed,[F|A],AA),univ_safe_2(GS,AA).
  451
  452
  453:-create_prolog_flag(dmsg_len,99,[keep(true)]).  454
  455:- multifile(user:portray/1).  456:- dynamic(user:portray/1).  457:- discontiguous(user:portray/1).  458% user:portray
  459
  460
  461
  462
  463%= 	 	 
 getPFA(?Frame, ?Ctrl, ?Goal) is semidet
Get Pred Functor A.
  469getPFA(Frame,[L|List],Goal):- !,findall(R, (member(A,[L|List]),getPFA1(Frame,A,R)) ,Goal).
  470getPFA(Frame,Ctrl,Goal):-getPFA1(Frame,Ctrl,Goal).
  471
  472
  473%= 	 	 
 getPFA1(?Frame, ?Txt, ?Txt) is semidet
Get Pred Functor A Secondary Helper.
  479getPFA1(_Frame,txt(Txt),txt(Txt)):-!.
  480getPFA1(Frame,clause,Goal):-getPFA2(Frame,clause,ClRef),clauseST(ClRef,Goal),!.
  481getPFA1(Frame,Ctrl,Ctrl=Goal):-getPFA2(Frame,Ctrl,Goal),!.
  482getPFA1(_,Ctrl,no(Ctrl)).
  483
  484
  485%= 	 	 
 getPFA2(?Frame, ?Ctrl, ?Goal) is semidet
Get Pred Functor A Extended Helper.
  491getPFA2(Frame,Ctrl,Goal):- catchv((prolog_frame_attribute(Frame,Ctrl,Goal)),E,Goal=[error(Ctrl,E)]),!.
  492
  493
  494%= 	 	 
 clauseST(?ClRef, :TermGoal) is semidet
Clause S True Stucture.
  500clauseST(ClRef,clause=Goal):- findall(V,(member(Prop,[file(V),line_count(V)]),clause_property(ClRef,Prop)),Goal).
  501
  502clauseST(ClRef,Goal = HB):- ignore(((clause(Head, Body, ClRef),copy_term(((Head :- Body)),HB)))),
  503   snumbervars(HB,0,_),
  504   findall(Prop,(member(Prop,[source(_),line_count(_),file(_),fact,erased]),clause_property(ClRef,Prop)),Goal).
  505
  506
  507:- thread_local(tlbugger:ifCanTrace/0).  508
  509
  510%= 	 	 
 end_dump(:TermGG) is semidet
End Dump.
  516end_dump(true):-!,fail.
  517end_dump(_:GG):-!,end_dump(GG).
  518end_dump(GG):-compound(GG),functor(GG,F,_),atom_concat(dump,_,F),nb_setval('$hide_rest_frames',true).
  519
  520% =====================
  521% dtrace/0/1/2
  522% =====================
  523
  524%:- redefine_system_predicate(system:dtrace()).
  525dtrace:- wdmsg("DUMP_TRACE/0"), (thread_self_main->(dumpST,rtrace);(dumpST(30),abort)).
  526%= 	 	 
 dtrace is semidet
(debug) Trace.

:- redefine_system_predicate(system:dbreak()).

  534:- thread_local(t_l:no_dbreak/0).  535dbreak:- wdmsg("DUMP_BREAK/0"),dumpST,wdmsg("DUMP_BREAK/0"),
  536  (t_l:no_dbreak -> wdmsg("NO__________________DUMP_BREAK/0") ;
  537   (thread_self_main->(dumpST,dtrace(system:break),break);true)).
  538
  539:- thread_local(tlbugger:has_auto_trace/1).  540:-meta_predicate(dtrace(0)).  541
  542%= 	 	 
 dtrace(:GoalG) is semidet
(debug) Trace.
  549dtrace(G):- zotrace((tlbugger:has_auto_trace(C),wdmsg(has_auto_trace(C,G)))),!,call(C,G). 
  550dtrace(G):- strip_module(G,_,dbreak),\+ thread_self_main,!.
  551% dtrace(G):- zotrace((tracing,notrace)),!,wdmsg(tracing_dtrace(G)),
  552%   scce_orig(notrace,restore_trace((leash(+all),dumptrace_or_cont(G))),trace).
  553
  554dtrace(G):- zotrace((once(((G=dmsg(GG);G=_:dmsg(GG);G=GG),nonvar(GG))),wdmsg(GG)))->true;
  555 catch(dumptrace1(G),E, handle_dumptrace_signal(G,E)),fail. %always fails
  556%dtrace(G):- \+ tlbugger:ifCanTrace,!,quietly((wdmsg((not(tlbugger:ifCanTrace(G)))))),!,badfood(G),!,dumpST.
  557%dtrace(G):- \+ tlbugger:ifCanTrace,!,quietly((wdmsg((not(tlbugger:ifCanTrace(G)))))),!,badfood(G),!,dumpST.
  558dtrace(G):- 
  559    catch(dumptrace1(G),E,handle_dumptrace_signal(G,E)).
  560
  561handle_dumptrace_signal(G,E):-arg(_,v(continue,abort),E),!,wdmsg(continuing(E,G)),notrace,nodebug.
  562handle_dumptrace_signal(_,E):-throw(E).
  563%:- export(dumptrace_or_cont/1).
  564%dumptrace_or_cont(G):- catch(dumptrace(G),E,handle_dumptrace_signal(G,E)).
  565
  566
  567
  568% :-meta_predicate(dtrace(+,?)).
  569
  570%= 	 	 
 dtrace(+MSG, ?G) is semidet
(debug) Trace.
  576dtrace(MSG,G):-wdmsg(MSG),dtrace(G).
  577
  578
  579%= 	 	 
 to_wmsg(:TermG, :TermWG) is semidet
Converted To Wmsg.
  585to_wmsg(G,WG):- \+ compound(G),!,WG=G.
  586to_wmsg(M:G,M:WG):-atom(M), to_wmsg(G,WG).
  587to_wmsg(dmsg(G),WG):-!, to_wmsg(G,WG).
  588to_wmsg(wdmsg(G),WG):-!, to_wmsg(G,WG).
  589to_wmsg(G,WG):- (G=WG).
  590
  591
  592with_source_module(G):-
  593  '$current_source_module'(M),
  594  '$current_typein_module'(WM),
  595  scce_orig('$set_typein_module'(M),G,'$set_typein_module'(WM)).
  596   
  597
  598
  599% =====================
  600% dumptrace/1/2
  601% =====================
  602% :-meta_predicate(dumptrace(?)).
  603
  604%= 	 	 
 dumptrace(?G) is semidet
Dump Trace.
  610dumptrace(G):- non_user_console,!,dumpST_error(non_user_console+dumptrace(G)),abort,fail.
  611dumptrace(G):-
  612  locally(set_prolog_flag(gui_tracer, false),
  613   locally(set_prolog_flag(gui, false),
  614    locally(set_prolog_flag(runtime_debug,0),
  615     dumptrace0(G)))).
  616
  617dumptrace0(G):- zotrace((tracing,notrace,wdmsg(tracing_dumptrace(G)))),!, catch(((dumptrace0(G) *-> dtrace ; (dtrace,fail))),_,true).
  618dumptrace0(G):-dumptrace1(G).
  619dumptrace1(G):-   
  620  catch(attach_console,_,true),
  621    repeat, 
  622    (tracing -> (!,fail) ; true),
  623    to_wmsg(G,WG),
  624    fmt(in_dumptrace(G)),
  625    wdmsg(WG),
  626    (get_single_char(C)->with_all_dmsg(dumptrace(G,C));throw(cant_get_single_char(!))).
  627
  628:-meta_predicate(dumptrace(0,+)).  629
  630ggtrace:-
  631  leash(+all),
  632  visible(+all),
  633  debug,
  634  maybe_leash(+exception).
  635
  636%= 	 	 
 dumptrace(:GoalG, +C) is semidet
Dump Trace.
  642dumptrace(_,0'h):- listing(dumptrace/2),!,fail.
  643dumptrace(_,0'g):-!,dumpST,!,fail.
  644dumptrace(_,0'G):-!,zotrace(dumpST0(500000)),!,fail.
  645dumptrace(_,0'D):-!,prolog_stack:backtrace(8000),!,fail.
  646dumptrace(_,0'd):-!,prolog_stack:backtrace(800),!,fail.
  647
  648dumptrace(G,0'l):-!, 
  649  restore_trace(( zotrace(ggtrace),G)),!,notrace.
  650%dumptrace(G,0's):-!,quietly(ggtrace),!,(quietly(G)*->true;true).
  651dumptrace(G,0'S):-!, wdmsg(skipping(G)),!.
  652dumptrace(_,0'c):-!, throw(continue).
  653%dumptrace(G,0'i):-!,quietly(ggtrace),!,ignore(G).
  654dumptrace(_,0'b):-!,debug,break,!,fail.
  655dumptrace(_,0'a):-!,abort,!,fail.
  656% dumptrace(_,0'x):-!,must(lex),!,fail.
  657dumptrace(_,0'e):-!,halt(1),!.
  658dumptrace(_,0'm):-!,make,fail.
  659dumptrace(G,0'L):-!,use_module(library(xlisting)),call(call,xlisting,G),!,fail.
  660dumptrace(G,0'l):-!,visible(+all),show_and_do(rtrace(G)).
  661% dumptrace(G,0'c):-!, show_and_do((G))*->true;true.
  662dumptrace(G,0'r):-!, stop_rtrace,notrace,nortrace,srtrace,(rtrace((trace,G,notrace))),!,fail.
  663dumptrace(G,0'f):-!, notrace,(ftrace((G,notrace))),!,fail.
  664dumptrace(G,0't):-!,visible(+all),leash(+all),trace,!,G.
  665dumptrace(G,10):-!,dumptrace_ret(G).
  666dumptrace(G,13):-!,dumptrace_ret(G).
  667dumptrace(G,Code):- number(Code),char_code(Char,Code),!,dumptrace(G,Char).
  668dumptrace(_G,'p'):- in_cmt(if_defined(pp_DB,fail)),!,fail.
  669
  670
  671dumptrace(_,C):-fmt(unused_keypress(C)),!,fail.
  672% )))))))))))))) %  '
  673
  674%= 	 	 
 dumptrace_ret(?G) is semidet
Dump Trace Ret.
  680dumptrace_ret(G):- zotrace((leash(+all),visible(+all),visible(+unify),trace)),G.
  681
  682% % % OFF :- system:use_module(library(logicmoo_utils_all)).
  683
  684% % % OFF :- system:use_module(library(logicmoo_startup)).
  685:- fixup_exports.