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:- system:use_module(library(apply)).   47:- reexport(library(logicmoo/util_strings)).   48:- use_module(library(logicmoo_utils_all)).   49:- use_module((ucatch)).   50:- use_module(library(logicmoo/no_loops)).   51:- use_module((rtrace)).   52:- 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:- use_module((dmsg)).   74%:- use_module(library(logicmoo_util_strings)).
   75
   76
   77%= 	 	 
 dump_st is semidet
Dump Stack Trace.
   83dump_st:- prolog_current_frame(Frame),dumpST0(Frame,10).
   84
   85
   86%= 	 	 
 dumpST0 is semidet
Dump S True Stucture Primary Helper.
   92dumpST0:- dbreak, 
   93   prolog_current_frame(Frame),(tracing->zotrace((CU=dtrace,notrace));CU=true),dumpST0(Frame,800),!,CU.
   94
   95%= 	 	 
 dumpST0(?Opts) is semidet
Dump S True Stucture Primary Helper.
  101dumpST0(Opts):- once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)),dumpST0(Frame,Opts).
  102
  103%= 	 	 
 dumpST0(?Frame, ?MaxDepth) is semidet
Dump S True Stucture Primary Helper.
  109:- thread_local(tlbugger:ifHideTrace/0).  110dumpST0(_,_):- tlbugger:ifHideTrace,!.
  111dumpST0(Frame,MaxDepth):- ignore(MaxDepth=5000),Term = dumpST(MaxDepth),
  112   (var(Frame)->once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame));true),
  113   ignore(( get_prolog_backtrace(MaxDepth, Trace,[frame(Frame),goal_depth(13)]),
  114    format(user_error, '% dumpST ~p', [Term]), nl(user_error),
  115    attach_console,dtrace,
  116    dbreak,
  117
  118    print_prolog_backtrace(user_error, Trace,[subgoal_positions(true)]), nl(user_error), fail)),!.
  119
  120
  121
  122% dumpstack_arguments.
  123
  124%= 	 	 
 dumpST is semidet
Dump S True Stucture.
  130dumpST:- zotrace((prolog_current_frame(Frame),b_setval('$dump_frame',Frame),dumpST1)).
  131
  132
  133:- thread_local(tlbugger:no_slow_io/0).  134:- multifile(tlbugger:no_slow_io/0).  135
  136%= 	 	 
 dumpST1 is semidet
Dump S True Stucture Secondary Helper.
  142dumpST1:- current_prolog_flag(dmsg_level,never),!.
  143dumpST1:- tlbugger:no_slow_io,!,dumpST0,!.
  144dumpST1:- tlbugger:ifHideTrace,!.
  145dumpST1:- show_current_source_location,loop_check_early(dumpST9,dumpST0).
  146
  147%= 	 	 
 dumpST(?Depth) is semidet
Dump S True Stucture.
  153dumpST(Depth):- zotrace((prolog_current_frame(Frame),b_setval('$dump_frame',Frame))),
  154   loop_check_early(dumpST9(Depth),dumpST0(Depth)).
  155
  156
  157%= 	 	 
 get_m_opt(?Opts, ?Max_depth, ?D100, ?RetVal) is semidet
Get Module Opt.
  163get_m_opt(Opts,Max_depth,D100,RetVal):- univ_safe_2(E,[Max_depth,V]),(((member(E,Opts),nonvar(V)))->RetVal=V;RetVal=D100).
  164
  165
  166
  167%= 	 	 
 dumpST9 is semidet
Dump S T9.
  173dumpST9:- zotrace((once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)), dumpST9(Frame,5000))).
  174
  175%= 	 	 
 dumpST9(?Depth) is semidet
Dump S T9.
  181dumpST9(Depth):- once(nb_current('$dump_frame',Frame);prolog_current_frame(Frame)), dumpST9(Frame,Depth).
  182
  183
  184%= 	 	 
 dumpST9(?Frame, :TermMaxDepth) is semidet
Dump S T9.
  190dumpST9(_,_):- tlbugger:ifHideTrace,!.
  191dumpST9(Frame,MaxDepth):- integer(MaxDepth),!,dumpST_now(Frame,[max_depth(MaxDepth),numbervars(true),show([level,has_alternatives,hidden,context_module,goal,clause])]).
  192dumpST9(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])]).
  193dumpST9(Frame,List):- is_list(List),dumpST_now(Frame,[show([level,has_alternatives,hidden,context_module,goal,clause])|List]).
  194
  195
  196
  197%= 	 	 
 drain_framelist(?Opts) is semidet
Drain Framelist.
  203drain_framelist(Opts):- repeat, \+ drain_framelist_ele(Opts).
  204
  205
  206%= 	 	 
 drain_framelist_ele(?Opts) is semidet
Drain Framelist Ele.
  212drain_framelist_ele(Opts):- 
  213    nb_getval('$current_stack_frame_list',[N-Frame|Next]),
  214    nb_setval('$current_stack_frame_list',Next),!,
  215    printFrame(N,Frame,Opts),!.
  216    
  217        
  218
  219
  220%= 	 	 
 dumpST_now(?FrameIn, ?Opts) is semidet
Dump S True Stucture Now.
  226dumpST_now(FrameIn,Opts):-
  227  once(number(FrameIn);prolog_current_frame(FrameIn)),
  228   nb_setval('$hide_rest_frames',false),
  229   b_setval('$current_stack_frame_depth',0),
  230   b_setval('$current_stack_frame_list',[]),
  231   get_m_opt(Opts,max_depth,100,MD),
  232   b_setval('$current_stack_frame_handle',FrameIn),
  233  (repeat,  
  234     nb_getval('$current_stack_frame_depth',N),
  235     nb_getval('$current_stack_frame_handle',Frame),
  236    ((pushFrame(N,Frame,Opts),MD>N)-> 
  237     ((prolog_frame_attribute(Frame,parent,ParentFrame)->
  238       (nb_setval('$current_stack_frame_handle',ParentFrame),
  239       NN is N +1,nb_setval('$current_stack_frame_depth',NN),fail); !));
  240     (!))),
  241   drain_framelist(Opts),!.
 pushFrame(?N, ?Frame, ?Opts) is semidet
Push Frame.
  249pushFrame(N,Frame,_Opts):- nb_getval('$current_stack_frame_list',Current),nb_setval('$current_stack_frame_list',[N-Frame|Current]).
  250
  251
  252%= 	 	 
 printFrame(?N, ?Frame, ?Opts) is semidet
Print Frame.
  258printFrame(_,_,_):- nb_current('$hide_rest_frames',true),!.
  259printFrame(N,Frame,Opts):-
  260  ignore(((frame_to_fmsg(N,Frame,Opts,Out)),must(fmsg_rout(Out)))),!.
  261
  262
  263%= 	 	 
 frame_to_fmsg(?N, ?Frame, ?Opts, ?N) is semidet
Frame Converted To Functor Message.
  269frame_to_fmsg(N,Frame,Opts,[nf(max_depth,N,Frame,Opts)]):-get_m_opt(Opts,max_depth,100,MD),N>=MD,!,fail.
  270%  dumpST9(N,Frame,Opts,[nf(max_depth,N,Frame,Opts)]):-get_m_opt(Opts,skip_depth,100,SD),N=<SD,!.
  271frame_to_fmsg(_,Frame,Opts,[fr(Goal)]):- get_m_opt(Opts,show,goal,Ctrl),getPFA(Frame,Ctrl,Goal),!.
  272frame_to_fmsg(N,Frame,Opts,[nf(no(Ctrl),N,Frame,Opts)]):- get_m_opt(Opts,show,goal,Ctrl),!.
  273frame_to_fmsg(N,Frame,Opts,[nf(noFrame(N,Frame,Opts))]).
  274
  275 
  276
  277
  278%= 	 	 
 fmsg_rout(:TermRROut) is semidet
Functor Message Rout.
  284fmsg_rout([]):-!.
  285fmsg_rout([fr(E)|_]):- member(goal=GG,E),end_dump(GG),!,ignore(fdmsg(fr(E))),!.
  286fmsg_rout([fr(E)|_]):- member(goal=GG,E),end_dump(GG),!,ignore(fdmsg(fr(E))),!.
  287fmsg_rout([E|RROut]):- ignore(fdmsg(E)),!,fmsg_rout(RROut).
  288fmsg_rout(RROut):- show_call(why,forall(member(E,RROut),fdmsg(E))),!.
  289
  290
  291%= 	 	 
 neg1_numbervars(?Out, ?Start, :GoalROut) is semidet
Negated Secondary Helper Numbervars.
  297neg1_numbervars(T,-1,T):-!.
  298neg1_numbervars(Out,false,Out):-!.
  299neg1_numbervars(Out,true,ROut):-copy_term(Out,ROut),!,snumbervars(ROut,777,_).
  300neg1_numbervars(Out,Start,ROut):-copy_term(Out,ROut),integer(Start),!,snumbervars(ROut,Start,_).
  301neg1_numbervars(Out,safe,ROut):-copy_term(Out,ROut),safe_numbervars(ROut).
  302
  303if_defined_mesg_color(G,C):- current_predicate(mesg_color/2),mesg_color(G,C).
  304
  305%= 	 	 
 fdmsg1(?G) is semidet
Fdmsg Secondary Helper.
  311fdmsg1(txt(S)):-'format'(S,[]),!.
  312fdmsg1(level=L):-'format'('(~q)',[L]),!.
  313fdmsg1(context_module=G):- simplify_m(G,M),!,if_defined_mesg_color(G,Ctrl),ansicall(Ctrl,format('[~w]',[M])),!.
  314fdmsg1(has_alternatives=G):- (G==false->true;'format'('<*>',[])),!.
  315fdmsg1(hidden=G):- (G==false->true;'format'('$',[G])),!.
  316fdmsg1(goal=G):- do_fdmsg1(G).
  317fdmsg1(clause=[F,L]):- directory_file_path(_,FF,F),'format'('  %  ~w:~w: ',[FF,L]),!.
  318fdmsg1(clause=[F,L]):- fresh_line,'format'('%  ~w:~w: ',[F,L]),!.
  319fdmsg1(clause=[]):-'format'(' /*DYN*/ ',[]),!.
  320fdmsg1(G):- if_defined_mesg_color(G,Ctrl),ansicall(Ctrl,format(' ~q ',[G])),!.
  321fdmsg1(M):-dmsg(failed_fdmsg1(M)).
  322
  323do_fdmsg1(G):- 
  324  simplify_goal_printed(G,GG),!,
  325  (GG\==G->write('#');true),
  326  term_variables(GG,_Vars),
  327  copy_term_nat(GG,GGG), =(GG,GGG),
  328  numbervars(GGG,0,_,[attvar(skip)]),
  329  if_defined_mesg_color(GGG,Ctrl),ansicall(Ctrl,format(' ~q. ',[GGG])),!.
  330
  331
  332%= 	 	 
 simplify_m(?G, ?M) is semidet
Simplify Module.
  338simplify_m(G,M):-atom(G),sub_atom(G,_,6,0,M),!.
  339simplify_m(G,G).
  340
  341%= 	 	 
 fdmsg(?M) is semidet
Fdmsg.
  347fdmsg(fr(List)):-is_list(List),!,must((fresh_line,ignore(forall(member(E,List),fdmsg1(E))),nl)).
  348fdmsg(M):- logicmoo_util_catch:ddmsg(failed_fdmsg(M)).
  349
  350:- thread_local(tlbugger:plain_attvars/0).  351
  352:-export(simplify_goal_printed/2).  353
  354%= 	 	 
  355printable_variable_name(Var, Name) :- nonvar(Name),!,must(printable_variable_name(Var, NameO)),!,Name=NameO.
  356printable_variable_name(Var, Name) :- nonvar(Var),Var='$VAR'(_),format(atom(Name),"~w_VAR",Var).
  357printable_variable_name(Var, Name) :- nonvar(Var),format(atom(Name),"(_~q_)",Var).
  358printable_variable_name(Var,Name):- (get_attr(Var, vn, Name1);
  359  get_attr(Var, varnames, Name1)),
  360 (var_property(Var,name(Name2))-> 
  361   (Name1==Name2-> atom_concat(Name1,'_VN',Name) ; Name=(Name1:Name2)); 
  362    (atom(Name1)->atom_concat('?',Name1,Name);
  363   format(atom(Name),"'$VaR'(~q)",Var))),!.
  364printable_variable_name(Var,Name):- v_name1(Var,Name),!.
  365printable_variable_name(Var,Name):- v_name2(Var,Name),!. % ,atom_concat(Name1,'_TL',Name).
  366
  367v_name1(Var,Name):- var_property(Var,name(Name)),!.
  368v_name1(Var,Name):- get_varname_list(Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  369v_name1(Var,Name):- nb_current('$old_variable_names', Vs),member(Name=V,Vs),atomic(Name),V==Var,!.
  370v_name2(Var,Name):- get_varname_list(Vs),format(atom(Name),'~W',[Var, [variable_names(Vs)]]).
  371 
  372
  373%attrs_to_list(att(sk,_,ATTRS),[sk|List]):-!,attrs_to_list(ATTRS,List).
  374attrs_to_list(att(vn,_,ATTRS),List):-!,attrs_to_list(ATTRS,List).
  375attrs_to_list(att(M,V,ATTRS),[M=VV|List]):- locally(tlbugger:plain_attvars,simplify_goal_printed(V,VV)),!,attrs_to_list(ATTRS,List).
  376attrs_to_list([],[]).
  377attrs_to_list(_ATTRS,[]).
 simplify_goal_printed(:TermVar, :TermVar) is semidet
Simplify Goal Printed.
  384simplify_var_printed(Var,'$avar'('$VAR'(Name))):- tlbugger:plain_attvars,must(printable_variable_name(Var,Name)),!.
  385simplify_var_printed(Var,'$VAR'(Name)):- get_attrs(Var,att(vn, _, [])),printable_variable_name(Var, Name),!.
  386simplify_var_printed(Var,'$avar'('$VAR'(Name))):- tlbugger:plain_attvars,must(printable_variable_name(Var,Name)),!.
  387simplify_var_printed(Var,'$avar'(Dict)):- get_attrs(Var,ATTRS),must(printable_variable_name(Var,Name)),attrs_to_list(ATTRS,List),
  388                         dict_create(Dict,'$VAR'(Name),List).
  389simplify_var_printed(Var,'$VAR'(Name)):- is_ftVar(Var),!,printable_variable_name(Var, Name).
  390
  391simplify_goal_printed(Var,Var):-var(Var),!.
  392simplify_goal_printed(Var,Name):-cyclic_term(Var),!,Name=Var.
  393simplify_goal_printed(Var,Name):-is_ftVar(Var),\+ current_prolog_flag(variable_names_bad,true),simplify_var_printed(Var,Name),!.
  394simplify_goal_printed(Var,Var):-var(Var),!.
  395simplify_goal_printed(setup_call_catcher_cleanup,scccu).
  396simplify_goal_printed(existence_error(X,Y),existence_error(X,Y)):-nl,writeq(existence_error(X,Y)),nl,fail.
  397simplify_goal_printed(setup_call_cleanup,sccu).
  398simplify_goal_printed(existence_error,'existence_error_XXXXXXXXX__\e[0m\e[1;34m%-6s\e[m\'This is text\e[0mRED__existence_error_existence_error').
  399simplify_goal_printed(each_call_cleanup,eccu).
  400simplify_goal_printed(call_cleanup,ccu).
  401simplify_goal_printed([Var|_],'$'):-compound(Var),Var = (var_tracker(_) = _ ).
  402simplify_goal_printed([Var|_],'$'):-compound(Var),Var = (fbound(_) = _ ).
  403simplify_goal_printed(M:I,O):- atom(M),(M==user;M==system),!,simplify_goal_printed(I,O).
  404simplify_goal_printed(M:I,O):- atom(M),!,simplify_goal_printed(I,O).
  405%simplify_goal_printed(catch(I,V,_),O):- var(V),!,simplify_goal_printed(I,O).
  406simplify_goal_printed(always(I),O):- !,simplify_goal_printed(I,O).
  407simplify_goal_printed(must_det_lm(M,G),GS):-!,simplify_goal_printed(M:must_det_l(G),GS).
  408%simplify_goal_printed('<meta-call>'(G),GS):-!,simplify_goal_printed(G,GS).
  409%simplify_goal_printed(call(G),GS):-!,simplify_goal_printed(G,GS).
  410simplify_goal_printed(M:G,MS:GS):-atom(M), simplify_m(M,MS),!,simplify_goal_printed(G,GS).
  411simplify_goal_printed(dinterp(_,_,I,_),O):- !,simplify_goal_printed(I,O).
  412
  413
  414simplify_goal_printed(P,O):- compound(P),compound_name_arguments(P,F,[I]),
  415  atom_contains(F,must),!,simplify_goal_printed(I,O).
  416
  417simplify_goal_printed(call_term_expansion(_,A,_,B,_),O):- !, simplify_goal_printed(call_term_expansion_5('...',A,'...',B,'...'),O).
  418simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/opt/PrologMUD/pack/logicmoo_base/prolog/logicmoo/',SA,A),!.
  419simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/home/dmiles/lib/swipl/pack/logicmoo_base/prolog/logicmoo/',SA,A),!.
  420simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/home/dmiles/lib/swipl/pack/logicmoo_base/t/',SA,A),!.
  421% simplify_goal_printed(A,'...'(SA)):- atom(A),atom_concat('/',_,A),!,directory_file_path(_,SA,A),!.
  422simplify_goal_printed(GOAL=A,AS):- goal==GOAL,!,simplify_goal_printed(A,AS).
  423simplify_goal_printed(Var,Var):- \+ compound(Var),!.
  424simplify_goal_printed(term_position(_,_,_,_,_),'$..term_position/4..$').
  425%simplify_goal_printed(user:G,GS):-!,simplify_goal_printed(G,GS).
  426%simplify_goal_printed(system:G,GS):-!,simplify_goal_printed(G,GS).
  427%simplify_goal_printed(catchv(G,_,_),GS):-!,simplify_goal_printed(G,GS).
  428%simplify_goal_printed(catch(G,_,_),GS):-!,simplify_goal_printed(G,GS).
  429%simplify_goal_printed(skolem(V,N,_F),GS):-!,simplify_goal_printed(skeq(V,N,'..'),GS).
  430
  431simplify_goal_printed(List,O):- 
  432  is_list(List),length(List,L),L>3,
  433   append([A,B,C],[F|_],List),F \='...'(_), !, 
  434  simplify_goal_printed([A,B,C,'...'(_)],O).
  435
  436
  437simplify_goal_printed([E|OList],O):- \+ is_list(OList), 
  438   append(List,Open,OList),var(Open),!,
  439   is_list(List),length(List,L),L>3,
  440    append([A,B,C],[F|_],[E|List]),F \='...'(_), !, 
  441   simplify_goal_printed([A,B,C,'...'(_)],O).
  442
  443
  444simplify_goal_printed([F|A],[FS|AS]):- !,simplify_goal_printed(F,FS),simplify_goal_printed(A,AS).
  445simplify_goal_printed(G,GS):- univ_safe_2(G,[F|A]),maplist(simplify_goal_printed,[F|A],AA),univ_safe_2(GS,AA).
  446
  447:- multifile(user:portray/1).  448:- dynamic(user:portray/1).  449:- discontiguous(user:portray/1).  450% user:portray
  451
  452
  453
  454
  455%= 	 	 
 getPFA(?Frame, ?Ctrl, ?Goal) is semidet
Get Pred Functor A.
  461getPFA(Frame,[L|List],Goal):- !,findall(R, (member(A,[L|List]),getPFA1(Frame,A,R)) ,Goal).
  462getPFA(Frame,Ctrl,Goal):-getPFA1(Frame,Ctrl,Goal).
  463
  464
  465%= 	 	 
 getPFA1(?Frame, ?Txt, ?Txt) is semidet
Get Pred Functor A Secondary Helper.
  471getPFA1(_Frame,txt(Txt),txt(Txt)):-!.
  472getPFA1(Frame,clause,Goal):-getPFA2(Frame,clause,ClRef),clauseST(ClRef,Goal),!.
  473getPFA1(Frame,Ctrl,Ctrl=Goal):-getPFA2(Frame,Ctrl,Goal),!.
  474getPFA1(_,Ctrl,no(Ctrl)).
  475
  476
  477%= 	 	 
 getPFA2(?Frame, ?Ctrl, ?Goal) is semidet
Get Pred Functor A Extended Helper.
  483getPFA2(Frame,Ctrl,Goal):- catchv((prolog_frame_attribute(Frame,Ctrl,Goal)),E,Goal=[error(Ctrl,E)]),!.
  484
  485
  486%= 	 	 
 clauseST(?ClRef, :TermGoal) is semidet
Clause S True Stucture.
  492clauseST(ClRef,clause=Goal):- findall(V,(member(Prop,[file(V),line_count(V)]),clause_property(ClRef,Prop)),Goal).
  493
  494clauseST(ClRef,Goal = HB):- ignore(((clause(Head, Body, ClRef),copy_term(((Head :- Body)),HB)))),
  495   snumbervars(HB,0,_),
  496   findall(Prop,(member(Prop,[source(_),line_count(_),file(_),fact,erased]),clause_property(ClRef,Prop)),Goal).
  497
  498
  499:- thread_local(tlbugger:ifCanTrace/0).  500
  501
  502%= 	 	 
 end_dump(:TermGG) is semidet
End Dump.
  508end_dump(true):-!,fail.
  509end_dump(_:GG):-!,end_dump(GG).
  510end_dump(GG):-compound(GG),functor(GG,F,_),atom_concat(dump,_,F),nb_setval('$hide_rest_frames',true).
  511
  512% =====================
  513% dtrace/0/1/2
  514% =====================
  515
  516%:- redefine_system_predicate(system:dtrace()).
  517dtrace:- wdmsg("DUMP_TRACE/0"), (thread_self_main->(dumpST,rtrace);(dumpST(30),abort)).
  518%= 	 	 
 dtrace is semidet
(debug) Trace.

:- redefine_system_predicate(system:dbreak()).

  526:- thread_local(t_l:no_dbreak/0).  527dbreak:- wdmsg("DUMP_BREAK/0"),dumpST,wdmsg("DUMP_BREAK/0"),
  528  (t_l:no_dbreak -> wdmsg("NO__________________DUMP_BREAK/0") ;
  529   (thread_self_main->(dumpST,dtrace(system:break),break);true)).
  530
  531:- thread_local(tlbugger:has_auto_trace/1).  532:-meta_predicate(dtrace(0)).  533
  534%= 	 	 
 dtrace(:GoalG) is semidet
(debug) Trace.
  541dtrace(G):- zotrace((tlbugger:has_auto_trace(C),wdmsg(has_auto_trace(C,G)))),!,call(C,G). 
  542dtrace(G):- strip_module(G,_,dbreak),\+ thread_self_main,!.
  543% dtrace(G):- zotrace((tracing,notrace)),!,wdmsg(tracing_dtrace(G)),
  544%   scce_orig(notrace,restore_trace((leash(+all),dumptrace_or_cont(G))),trace).
  545
  546dtrace(G):- zotrace((once(((G=dmsg(GG);G=_:dmsg(GG);G=GG),nonvar(GG))),wdmsg(GG)))->true;
  547 catch(dumptrace1(G),E, handle_dumptrace_signal(G,E)),fail. %always fails
  548%dtrace(G):- \+ tlbugger:ifCanTrace,!,quietly((wdmsg((not(tlbugger:ifCanTrace(G)))))),!,badfood(G),!,dumpST.
  549%dtrace(G):- \+ tlbugger:ifCanTrace,!,quietly((wdmsg((not(tlbugger:ifCanTrace(G)))))),!,badfood(G),!,dumpST.
  550dtrace(G):- 
  551    catch(dumptrace1(G),E,handle_dumptrace_signal(G,E)).
  552
  553handle_dumptrace_signal(G,E):-arg(_,v(continue,abort),E),!,wdmsg(continuing(E,G)),notrace,nodebug.
  554handle_dumptrace_signal(_,E):-throw(E).
  555%:- export(dumptrace_or_cont/1).
  556%dumptrace_or_cont(G):- catch(dumptrace(G),E,handle_dumptrace_signal(G,E)).
  557
  558
  559
  560% :-meta_predicate(dtrace(+,?)).
  561
  562%= 	 	 
 dtrace(+MSG, ?G) is semidet
(debug) Trace.
  568dtrace(MSG,G):-wdmsg(MSG),dtrace(G).
  569
  570
  571%= 	 	 
 to_wmsg(:TermG, :TermWG) is semidet
Converted To Wmsg.
  577to_wmsg(G,WG):- \+ compound(G),!,WG=G.
  578to_wmsg(M:G,M:WG):-atom(M), to_wmsg(G,WG).
  579to_wmsg(dmsg(G),WG):-!, to_wmsg(G,WG).
  580to_wmsg(wdmsg(G),WG):-!, to_wmsg(G,WG).
  581to_wmsg(G,WG):- (G=WG).
  582
  583
  584with_source_module(G):-
  585  '$current_source_module'(M),
  586  '$current_typein_module'(WM),
  587  scce_orig('$set_typein_module'(M),G,'$set_typein_module'(WM)).
  588   
  589
  590
  591% =====================
  592% dumptrace/1/2
  593% =====================
  594% :-meta_predicate(dumptrace(?)).
  595
  596%= 	 	 
 dumptrace(?G) is semidet
Dump Trace.
  602dumptrace(G):- non_user_console,!,dumpST_error(non_user_console+dumptrace(G)),abort,fail.
  603dumptrace(G):-
  604  locally(set_prolog_flag(gui_tracer, false),
  605   locally(set_prolog_flag(gui, false),
  606    locally(set_prolog_flag(runtime_debug,0),
  607     dumptrace0(G)))).
  608
  609dumptrace0(G):- zotrace((tracing,notrace,wdmsg(tracing_dumptrace(G)))),!, catch(((dumptrace0(G) *-> dtrace ; (dtrace,fail))),_,true).
  610dumptrace0(G):-dumptrace1(G).
  611dumptrace1(G):-   
  612  catch(attach_console,_,true),
  613    repeat, 
  614    (tracing -> (!,fail) ; true),
  615    to_wmsg(G,WG),
  616    fmt(in_dumptrace(G)),
  617    wdmsg(WG),
  618    (get_single_char(C)->with_all_dmsg(dumptrace(G,C));throw(cant_get_single_char(!))).
  619
  620:-meta_predicate(dumptrace(0,+)).  621
  622ggtrace:-
  623  leash(+all),
  624  visible(+all),
  625  debug,
  626  maybe_leash(+exception).
  627
  628%= 	 	 
 dumptrace(:GoalG, +C) is semidet
Dump Trace.
  634dumptrace(_,0'h):- listing(dumptrace/2),!,fail.
  635dumptrace(_,0'g):-!,dumpST,!,fail.
  636dumptrace(_,0'G):-!,zotrace(dumpST0(500000)),!,fail.
  637dumptrace(_,0'D):-!,prolog_stack:backtrace(8000),!,fail.
  638dumptrace(_,0'd):-!,prolog_stack:backtrace(800),!,fail.
  639
  640dumptrace(G,0'l):-!, 
  641  restore_trace(( zotrace(ggtrace),G)),!,notrace.
  642%dumptrace(G,0's):-!,quietly(ggtrace),!,(quietly(G)*->true;true).
  643dumptrace(G,0'S):-!, wdmsg(skipping(G)),!.
  644dumptrace(_,0'c):-!, throw(continue).
  645%dumptrace(G,0'i):-!,quietly(ggtrace),!,ignore(G).
  646dumptrace(_,0'b):-!,debug,break,!,fail.
  647dumptrace(_,0'a):-!,abort,!,fail.
  648% dumptrace(_,0'x):-!,must(lex),!,fail.
  649dumptrace(_,0'e):-!,halt(1),!.
  650dumptrace(_,0'm):-!,make,fail.
  651dumptrace(G,0'L):-!,use_module(library(xlisting)),call(call,xlisting,G),!,fail.
  652dumptrace(G,0'l):-!,visible(+all),show_and_do(rtrace(G)).
  653% dumptrace(G,0'c):-!, show_and_do((G))*->true;true.
  654dumptrace(G,0'r):-!, stop_rtrace,notrace,nortrace,srtrace,(rtrace((trace,G,notrace))),!,fail.
  655dumptrace(G,0'f):-!, notrace,(ftrace((G,notrace))),!,fail.
  656dumptrace(G,0't):-!,visible(+all),leash(+all),trace,!,G.
  657dumptrace(G,10):-!,dumptrace_ret(G).
  658dumptrace(G,13):-!,dumptrace_ret(G).
  659dumptrace(G,Code):- number(Code),char_code(Char,Code),!,dumptrace(G,Char).
  660dumptrace(_G,'p'):- in_cmt(if_defined(pp_DB,fail)),!,fail.
  661
  662
  663dumptrace(_,C):-fmt(unused_keypress(C)),!,fail.
  664% )))))))))))))) %  '
  665
  666%= 	 	 
 dumptrace_ret(?G) is semidet
Dump Trace Ret.
  672dumptrace_ret(G):- zotrace((leash(+all),visible(+all),visible(+unify),trace)),G.
  673
  674:- use_module(library(logicmoo_utils_all)).  675
  676:- use_module(library(logicmoo_startup)).  677:- fixup_exports.