home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / src / mtrace.lisp < prev    next >
Encoding:
Text File  |  2003-02-09  |  28.2 KB  |  846 lines

  1. ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;     The data in this file contains enhancments.                    ;;;;;
  4. ;;;                                                                    ;;;;;
  5. ;;;  Copyright (c) 1984,1987 by William Schelter,University of Texas   ;;;;;
  6. ;;;     All rights reserved                                            ;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;;     (c) Copyright 1981 Massachusetts Institute of Technology         ;;;
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10.  
  11. (in-package "MAXIMA")
  12. (macsyma-module mtrace)
  13.  
  14. (declare-top(*lexpr trace-mprint) ;; forward references
  15.      (genprefix mtrace-)
  16.      (special $functions $transrun trace-allp))
  17.  
  18. ;;; a reasonable trace capability for macsyma users.
  19. ;;; 8:10pm  Saturday, 10 January 1981 -GJC.
  20.  
  21. ;; TRACE(F1,F2,...)   /* traces the functions */
  22. ;; TRACE()            /* returns a list of functions under trace */
  23. ;; UNTRACE(F1,F2,...) /* untraces the functions */
  24. ;; UNTRACE()          /* untraces all functions. */
  25. ;; TRACE_MAX_INDENT   /* The maximum indentation of trace printing. */
  26. ;;
  27. ;; TRACE_OPTIONS(F,option1,option2,...)  /* gives F options */
  28. ;;
  29. ;; TRACE_BREAK_ARG    /* Bound to list of argument during BREAK ENTER,
  30. ;;                      and the return value during BREAK EXIT.
  31. ;;                      This lets you change the arguments to a function,
  32. ;;                      or make a function return a different value,
  33. ;;                      which are both usefull debugging hacks.
  34. ;;
  35. ;;                      You probably want to give this a short alias
  36. ;;                      for typing convenience. 
  37. ;;                    */
  38. ;;
  39. ;; An option is either a keyword, FOO.
  40. ;; or an expression  FOO(PREDICATE_FUNCTION);
  41. ;;
  42. ;; A keyword means that the option is in effect, an keyword
  43. ;; expression means to apply the predicate function to some arguments
  44. ;; to determine if the option is in effect. The argument list is always
  45. ;; [LEVEL,DIRECTION, FUNCTION, ITEM] where
  46. ;; LEVEL      is the recursion level for the function.
  47. ;; DIRECTION  is either ENTER or EXIT.
  48. ;; FUNCTION   is the name of the function.
  49. ;; ITEM       is either the argument list or the return value.
  50. ;;
  51. ;; ----------------------------------------------
  52. ;; | Keyword    | Meaning of return value       |
  53. ;; ----------------------------------------------
  54. ;; | NOPRINT    | If TRUE do no printing.       |
  55. ;; | BREAK      | If TRUE give a breakpoint.    |
  56. ;; | LISP_PRINT | If TRUE use lisp printing.    |
  57. ;; | INFO       | Extra info to print           |
  58. ;; | ERRORCATCH | If TRUE errors are caught.    |
  59. ;; ----------------------------------------------
  60. ;;
  61. ;; General interface functions. These would be called by user debugging utilities.
  62. ;;
  63. ;; TRACE_IT('F)             /* Trace the function named F */
  64. ;; TRACE                    /* list of functions presently traced. */
  65. ;; UNTRACE_IT('F)           /* Untrace the function named F */
  66. ;; GET('F,'TRACE_OPTIONS)  /* Access the trace options of F */
  67. ;;
  68. ;; Sophisticated feature:
  69. ;; TRACE_SAFETY a variable with default value TRUE.
  70. ;; Example: F(X):=X; BREAKP([L]):=(PRINT("Hi!",L),FALSE),
  71. ;; TRACE(F,BREAKP); TRACE_OPTIONS(F,BREAK(BREAKP));
  72. ;; F(X); Note that even though BREAKP is traced, and it is called,
  73. ;; it does not print out as if it were traced. If you set
  74. ;; TRACE_SAFETY:FALSE; then F(X); will cause a normal trace-printing
  75. ;; for BREAKP. However, then consider TRACE_OPTIONS(BREAKP,BREAK(BREAKP));
  76. ;; When TRACE_SAFETY:FALSE; F(X); will give an infinite recursion,
  77. ;; which it would not if safety were turned on.
  78. ;; [Just thinking about this gives me a headache.]
  79.  
  80. ;; Internal notes on this package:                -jkf
  81. ;; Trace works by storing away the real definition of a function and
  82. ;; replacing it by a 'shadow' function.  The shadow function prints a
  83. ;; message, calls the real function, and then prints a message as it
  84. ;; leaves.  The type of the shadow function may differ from the
  85. ;; function being shadowed.  The chart below shows what type of shadow
  86. ;; function is needed for each type of Macsyma function.
  87. ;;
  88. ;; Macsyma function    shadow type    hook type    mget
  89. ;; ____________________________________________________________
  90. ;;    subr         expr          expr
  91. ;;    expr         expr          expr
  92. ;;    lsubr         expr          expr
  93. ;;    fsubr         fexpr          fexpr
  94. ;;    fexpr         fexpr          fexpr
  95. ;;    mexpr         expr          expr        t
  96. ;;    mfexpr*         mfexpr*      macro
  97. ;;    mfexpr*s     mfexpr*      macro
  98. ;;
  99. ;; The 'hook type' refers to the form of the shadow function.  'expr' types
  100. ;; are really lexprs, they expect any number of evaluated arguments.
  101. ;; 'fexpr' types expect one unevaluated argument which is the list of
  102. ;; arguments.  'macro' types expect one argument, the caar of which is the
  103. ;; name of the function, and the cdr of which is a list of arguments.
  104. ;;
  105. ;; For systems which store all function properties on the property list,
  106. ;; it is easy to shadow a function.  For systems with function cells,
  107. ;; the situation is a bit more difficult since the standard types of
  108. ;; functions are stored in the function cell (expr,fexpr,lexpr), whereas
  109. ;; the macsyma functions (mfexpr*,...) are stored on the property list.
  110. ;;
  111.  
  112. ;; Wolfgang Jenker says (in the maxima mailing list):
  113. ;; 1) The variety of maxima functions is much more restricted than what
  114. ;;    the table at the beginning of mtrace.lisp shows.  I think the
  115. ;;    following table gives the correct picture (like its counterpart in
  116. ;;    mtrace.lisp, it ignores maxima macros or functional arrays).
  117. ;;
  118. ;;
  119. ;; Maxima function    shadow type    hook type    mget
  120. ;; ____________________________________________________________
  121. ;;    expr         expr          expr
  122. ;;    mexpr         expr          expr        t
  123. ;;    mfexpr*         mfexpr*      expr
  124. ;;
  125. ;; These types have the following meaning: Suppose MFUN evaluates to some
  126. ;; symbol in the MAXIMA package.  That this symbol is of type
  127. ;;
  128. ;;  - EXPR (or SUBR) implies that it has a lisp function definition ==
  129. ;;    (SYMBOL-FUNCTION MFUN)
  130. ;;
  131. ;;  - MEXPR implies that it has a (parsed) maxima language definition ==
  132. ;;    (MGET MFUN 'MEXPR) and all arguments are evaluated by MEVAL.
  133. ;;
  134. ;;  - MFEXPR* implies that it has a lisp function definition == (GET MFUN
  135. ;;    'MFEXPR*) and its arguments are not automatically evaluated by
  136. ;;    MEVAL.
  137. ;;
  138. ;; Note that the shadow type has to agree with the original function's
  139. ;; type in the way arguments are evaluated.  On the other hand, I think
  140. ;; we are left with EXPR as the only hook type; as a matter of fact, this
  141. ;; is equivalent to the next point:
  142. ;;
  143. ;; 2) There is no need for MAKE-TRACE-HOOK to dispatch with respect to
  144. ;; HOOK-TYPE since, roughly speaking, proper handling of the traced
  145. ;; function's arguments is done by the trace handler in concert with
  146. ;; MEVAL*.
  147. ;;
  148. ;; Note that I also removed the COPY-LIST used to pass the traced
  149. ;; function's argument list to the trace handler.
  150. ;;
  151. ;; There remains an annoying problem with translated functions: tracing
  152. ;; some function of type MEXPR and then loading its translated version
  153. ;; (which is of type EXPR) will not cleanly untrace it (i.e., it is
  154. ;; effectively no longer traced but it remains on the list of traced
  155. ;; functions).  I think that this has to be fixed somewhere in the
  156. ;; translation package.
  157.  
  158.  
  159. ;;; Structures.
  160.  
  161. (eval-when (compile load eval)
  162. (defmacro trace-p (x) `(mget ,x 'trace))
  163. (defmacro trace-type (x) `(mget ,x 'trace-type))
  164. (defmacro trace-level (x) `(mget ,x 'trace-level))
  165. (defmacro trace-options (x) `($get ,x '$trace_options))
  166. #+(or Franz Cl)
  167. (defmacro trace-oldfun (x) `(mget ,x 'trace-oldfun))
  168. )
  169.  
  170.  
  171.  
  172. ;;; User interface functions.
  173.  
  174. (defmvar $trace (list '(mlist)) "List of functions actively traced")
  175. #-cl
  176. (putprop '$trace #'read-only-assign 'assign)
  177.  
  178. (defun mlistcan-$all (fun llist default)
  179.   "totally random utility function"
  180.   (let (trace-allp)
  181.     (if (null llist) default
  182.         `((mlist) ,@(mapcan fun
  183.                 (if (memq (car llist) '($all $functions))
  184.                     (prog2 (setq trace-allp t)
  185.                        (mapcar #'caar (cdr $functions)))
  186.                     llist))))))
  187.  
  188. (defmspec $trace (form)
  189.   (mlistcan-$all #'macsyma-trace (cdr form) $trace))
  190.   
  191. (defmfun $trace_it (function) `((mlist),@(macsyma-trace function)))
  192.  
  193.  
  194. (defmspec $untrace (form)
  195.   `((mlist) ,@(mapcan #'macsyma-untrace (or (cdr form)
  196.                         (cdr $trace)))))
  197.  
  198. (defmfun $untrace_it (function) `((mlist) ,@(macsyma-untrace function)))
  199.  
  200. (defmspec $trace_options (form)
  201.   (setf (trace-options (cadr form))
  202.     `((mlist) ,@(cddr form))))
  203.  
  204.  
  205.  
  206. ;;; System interface functions.
  207.  
  208. (defvar hard-to-trace '(trace-handler listify args setplist
  209.                      trace-apply
  210.                      *apply mapply))
  211.  
  212.  
  213. ;; A list of functions called by the TRACE-HANDLEr at times when
  214. ;; it cannot possibly shield itself from a continuation which would
  215. ;; cause infinite recursion. We are assuming the best-case of
  216. ;; compile code.
  217.  
  218. (defun macsyma-trace (fun) (macsyma-trace-sub fun 'trace-handler $trace))
  219.  
  220. (defun macsyma-trace-sub (fun handler ilist &aux temp)
  221.   (cond ((not (symbolp fun))
  222.      (mtell "~%Bad arg to TRACE: ~M" fun)
  223.      nil)
  224.     ((trace-p fun)
  225.      ;; Things which redefine should be expected to reset this
  226.      ;; to NIL.
  227.      (if (not trace-allp) (mtell "~%~@:M is already traced." fun))
  228.      nil)
  229.     ((memq fun hard-to-trace)
  230.      (mtell
  231.       "~%The function ~:@M cannot be traced because: ASK GJC~%"
  232.       fun)
  233.      nil)
  234.     ((null (setq temp (macsyma-fsymeval fun)))
  235.      (mtell "~%~@:M has no functional properties." fun)
  236.      nil)
  237.     ((memq (car temp) '(mmacro translated-mmacro))
  238.      (mtell "~%~@:M is a macro, won't trace well, so use ~
  239.              the MACROEXPAND function to debug it." fun)
  240.      nil)
  241.     ((get (car temp) 'shadow)
  242.      (put-trace-info fun (car temp) ilist)
  243.      (trace-fshadow fun (car temp)
  244.             (make-trace-hook fun (car temp) handler))
  245.      (list fun))
  246.     (t
  247.      (mtell "~%~@:M has functional properties not understood by TRACE"
  248.         fun)
  249.      nil)))
  250.  
  251. (defvar trace-handling-stack ())
  252.  
  253. (defun macsyma-untrace (fun) (macsyma-untrace-sub fun 'trace-handler $trace))
  254.  
  255. (defun macsyma-untrace-sub (fun handler ilist)
  256.   (prog1
  257.    (cond ((not (symbolp fun))
  258.       (mtell "~%Bad arg to UNTRACE: ~M" fun)
  259.       nil)
  260.      ((not (trace-p fun))
  261.       (mtell "~%~:@M is not traced." fun)
  262.       nil)
  263.      (t
  264.       (trace-unfshadow fun (trace-type fun))
  265.       (rem-trace-info fun ilist)
  266.       (list fun)))
  267.    (if (memq fun trace-handling-stack)
  268.        ;; yes, he has re-defined or untraced the function
  269.        ;; during the trace-handling application.
  270.        ;; This is not strange, in fact it happens all the
  271.        ;; time when the user is using the $ERRORCATCH option!
  272.        (macsyma-trace-sub fun handler ilist))))
  273.  
  274. (defun put-trace-info (fun type ilist)
  275.        (setf (trace-p fun) fun) ; needed for MEVAL at this time also.
  276.        (setf (trace-type fun) type)
  277. #+Franz(setf (trace-oldfun fun) (getd fun))
  278. #+cl(setf (trace-oldfun fun) (and (fboundp fun) (symbol-function fun)))
  279.        (LET ((SYM (GENSYM)))
  280.         (SET SYM 0)
  281.         (setf (trace-level fun) SYM))
  282.        (push fun (cdr ilist))
  283.        (list fun))
  284.  
  285. (defun rem-trace-info (fun ilist)
  286.        (setf (trace-p fun) nil)
  287.        (or (memq fun trace-handling-stack)
  288.        (setf (trace-level fun) nil))
  289.        (setf (trace-type fun) nil)
  290.        (delq fun ilist)
  291.        (list fun))
  292.  
  293.  
  294. ;; Placing the TRACE functional hook.
  295. ;; Because the function properties in macsyma are used by the EDITOR, SAVE,
  296. ;; and GRIND commands it is not possible to simply replace the function
  297. ;; being traced with a hook and to store the old definition someplace.
  298. ;; [We do know how to cons up machine-code hooks on the fly, so that
  299. ;;  is not stopping us].
  300.  
  301.  
  302. ;; This data should be formalized somehow at the time of
  303. ;; definition of the DEFining form.
  304.  
  305. (defprop subr expr shadow)
  306. (defprop lsubr expr shadow)
  307. (defprop expr expr shadow)
  308. (defprop mfexpr*s mfexpr* shadow)
  309. (defprop mfexpr* mfexpr* shadow)
  310. (defprop fsubr fexpr shadow)
  311. (defprop fexpr fexpr shadow)
  312.  
  313. #-Multics
  314. (progn
  315. ;; too slow to snap links on multics.
  316. (defprop subr t uuolinks)
  317. (defprop lsubr t uuolinks)
  318. (defprop fsubr t uuolinks) ; believe it or not. 
  319. )
  320.  
  321. (defprop mexpr t mget)
  322. (defprop mexpr expr shadow)
  323.  
  324. (defun get! (x y)
  325.        (or (get x y)
  326.        (get! (MAXIMA-ERROR (list "Undefined" y "property") x 'wrng-type-arg)
  327.          y)))
  328.  
  329. #+Maclisp
  330. (defun trace-fshadow (fun type value)
  331.        ;; the value is defined to be a lisp functional object, which
  332.        ;; might have to be compiled to be placed in certain locations.
  333.        (if (get type 'uuolinks)
  334.        (sstatus uuolinks))
  335.        (let ((shadow (get! type 'shadow)))
  336.         (setplist fun (list* shadow value (symbol-plist fun)))))
  337.  
  338. #+Franz
  339. (defun trace-fshadow (fun type value)
  340.    (cond ((and (get type 'uuolinks)
  341.            (status translink))
  342.       (sstatus translink nil)))
  343.    (let ((shadow (get! type 'shadow)))
  344.       (cond ((memq shadow '(expr subr))
  345.          (setf (trace-oldfun fun) (getd fun))
  346.          (putd fun value))
  347.         ((memq shadow '(fexpr fsubr))
  348.          (setf (trace-oldfun fun) (getd fun))
  349.          (putd fun (cons 'nlambda (cdr value))))
  350.         (t (setplist fun
  351.              `(,shadow ,value ,@(symbol-plist fun)))))))
  352.  
  353. #+cl
  354. (defun trace-fshadow (fun type value)
  355.    (let ((shadow (get! type 'shadow)))
  356.       (cond ((memq shadow '(expr subr))
  357.          (setf (trace-oldfun fun) (and (fboundp fun) (symbol-function fun)))
  358.          (setf (symbol-function  fun)  value))
  359.         ((memq shadow '(fexpr fsubr))
  360.          (setf (trace-oldfun fun) (symbol-function fun))
  361.          (setf (symbol-function  fun)  (cons 'nlambda (cdr value))))
  362.         (t (setplist fun
  363.              `(,shadow ,value ,@(symbol-plist fun)))))))
  364.  
  365. #+Maclisp
  366. (defun trace-unfshadow (fun type)
  367.        ;; what a hack.
  368.        (remprop fun (get! type 'shadow)))
  369.  
  370. #+Franz
  371. (defun trace-unfshadow (fun type)
  372.    (cond ((memq type '(expr subr fexpr fsubr))
  373.       (let ((oldf (trace-oldfun fun)))
  374.         (if (not (null oldf))
  375.         (putd fun oldf)
  376.         (putd fun nil))))
  377.      (t (remprop fun (get! type 'shadow))
  378.         (putd fun nil))))
  379.  
  380. #+cl
  381. (defun trace-unfshadow (fun type)
  382.   ;; At this point, we know that FUN is traced.
  383.   (cond ((memq type '(expr subr fexpr fsubr))
  384.      (let ((oldf (trace-oldfun fun)))
  385.        (if (not (null oldf))
  386.          (setf (symbol-function  fun)  oldf)
  387.          (fmakunbound fun))))
  388.      (t (remprop fun (get! type 'shadow))
  389.         (fmakunbound fun))))
  390.  
  391. ;--- trace-fsymeval :: find original function
  392. ;  fun : a function which is being traced.  The original defintion may
  393. ;     be hidden on the property list behind the shadow function.
  394. ;
  395. (defun trace-fsymeval (fun)
  396.    (or
  397.       (let ((type-of (trace-type fun)))
  398.      (cond ((get type-of 'mget)
  399.         (if (eq (get! type-of 'shadow) type-of)
  400.             (mget (cdr (mgetl fun (list type-of))) type-of)
  401.             (mget fun type-of)))
  402.            #+(or Franz Cl)
  403.            ((memq (get! type-of 'shadow) '(expr fexpr))
  404.         (trace-oldfun fun))
  405.            (t (if (eq (get! type-of 'shadow) type-of)
  406.               (cadr (getl (cdr (getl fun `(,type-of))) `(,type-of)))
  407.               (get fun type-of)))))
  408.       (trace-fsymeval
  409.      (merror "Macsyma BUG: Trace property for ~:@M went away without hook."
  410.          fun))))
  411.  
  412. ;;; The handling of a traced call.
  413.  
  414. (defvar trace-indent-level -1)
  415.  
  416. (defmacro bind-sym (symbol value . body)
  417.       #-Multics
  418.        ;; is by far the best dynamic binding generally available.
  419.       `(progv (list ,symbol)
  420.           (list ,value)
  421.           ,@body)
  422.       #+Multics ; PROGV is wedged on multics.
  423.       `(let ((the-symbol ,symbol)
  424.          (the-value ,value))
  425.         (let ((old-value (symbol-value the-symbol)))
  426.              (unwind-protect
  427.               (progn (set the-symbol the-value)
  428.                  ,@body)
  429.               (set the-symbol old-value)))))
  430.  
  431. ;; We really want to (BINDF (TRACE-LEVEL FUN) (f1+ (TRACE-LEVEL FUN)) ...)
  432. ;; (Think about PROGV and SETF and BINDF. If the trace object where
  433. ;; a closure, then we want to fluid bind instance variables.)
  434.  
  435. ;; From JPG;SUPRV
  436. ;;(DEFMFUN $ERRCATCH FEXPR (L)
  437. ;; (LET ((ERRCATCH (CONS BINDLIST LOCLIST)) RET)
  438. ;;      (IF (NULL (SETQ RET (ERRSET (MEVALN L) LISPERRPRINT)))
  439. ;;      (ERRLFUN1 ERRCATCH))
  440. ;;      (CONS '(MLIST) RET)))
  441. ;; ERRLFUN1 does the UNBINDING.
  442. ;; As soon as error handlers are written and signalling is
  443. ;; implemented, use the correct thing and get rid of this macro.
  444.  
  445. (declare-top(special errcatch lisperrprint bindlist loclist)
  446.      (*expr errlfun1))
  447.  
  448. (defmacro macsyma-errset (form &aux (ret (gensym)))
  449.       `(let ((errcatch (cons bindlist loclist)) ,ret)
  450.         (setq ,ret (errset ,form lisperrprint))
  451.         (or ,ret (errlfun1 errcatch))
  452.         ,ret))
  453.  
  454. (defvar predicate-arglist nil)
  455.  
  456. (defvar return-to-trace-handle nil)
  457.  
  458. (defun trace-handler (fun largs)
  459.        (If return-to-trace-handle
  460.        ;; we were called by the trace-handler.
  461.        (trace-apply fun largs)
  462.        (let ((trace-indent-level (f1+ trace-indent-level))
  463.          (return-to-trace-handle t)
  464.          (trace-handling-stack (cons fun trace-handling-stack))
  465.          (LEVEL-SYM (TRACE-LEVEL fun))(LEVEL))
  466.         (SETQ LEVEL (f1+ (SYMBOL-VALUE LEVEL-SYM)))
  467.         (BIND-SYM
  468.          LEVEL-SYM
  469.          LEVEL
  470.          (do ((ret-val)(continuation)(predicate-arglist))(nil)
  471.              (setq predicate-arglist `(,level $enter ,fun ((mlist) ,@largs)))
  472.              (setq largs (trace-enter-break fun level largs))
  473.              (trace-enter-print fun level largs)
  474.              (cond ((trace-option-p fun '$errorcatch)
  475.                 (setq ret-val (macsyma-errset (trace-apply fun largs)))
  476.                 (cond ((null ret-val)
  477.                    (setq ret-val (trace-error-break fun level largs))
  478.                    (setq continuation (car ret-val)
  479.                      ret-val (cdr ret-val)))
  480.                   (t
  481.                    (setq continuation 'exit
  482.                      ret-val (car ret-val)))))
  483.                (t
  484.                 (setq continuation 'exit
  485.                   ret-val (trace-apply fun largs))))
  486.              (case continuation
  487.                 ((exit)
  488.                  (setq predicate-arglist `(,level $exit ,fun ,ret-val))
  489.                  (setq ret-val (trace-exit-break fun level ret-val))
  490.                  (trace-exit-print fun level ret-val)
  491.                  (return ret-val))
  492.                 ((retry)
  493.                  (setq largs ret-val)
  494.                  (MTELL "~%Re applying the function ~:@M~%" fun))
  495.                 ((MAXIMA-ERROR)
  496.                  (MERROR "~%Signaling MAXIMA-ERROR for function ~:@M~%" fun))))))))
  497.  
  498.  
  499. ;; The (Trace-options function) access is not optimized to take place
  500. ;; only once per trace-handle call. This is so that the user may change
  501. ;; options during his break loops.
  502. ;; Question: Should we bind return-to-trace-handle to NIL when we
  503. ;; call the user's predicate? He has control over his own lossage.
  504.  
  505. (defmvar $trace_safety t "This is subtle")
  506.  
  507. (defun trace-option-p (function KEYWORD)
  508.   (do ((options                    
  509.     (LET ((OPTIONS (TRACE-OPTIONS FUNCTION)))
  510.       (COND ((NULL OPTIONS) NIL)
  511.         (($LISTP OPTIONS) (CDR OPTIONS))
  512.         (T
  513.          (mtell "Trace options for ~:@M not a list, so ignored."
  514.             function)
  515.          NIL)))
  516.     (CDR OPTIONS))
  517.        (OPTION))
  518.       ((null options) nil)
  519.     (setq OPTION (CAR OPTIONS))
  520.     (cond ((atom option)
  521.        (if (eq option keyword) (return t)))
  522.       ((eq (caar option) keyword)
  523.        (let ((return-to-trace-handle $trace_safety))
  524.          (return (mapply (cadr option) predicate-arglist
  525.                  "&A trace option predicate")))))))
  526.             
  527.  
  528. (defun trace-enter-print (fun lev largs &aux (mlargs `((mlist) ,@largs)))
  529.   (if (not (trace-option-p fun '$noprint))
  530.       (let ((info (trace-option-p fun '$info)))
  531.     (cond ((trace-option-p fun '$lisp_print)
  532.            (trace-print `(,lev enter ,fun ,largs ,@info)))
  533.           (t
  534.            (trace-mprint lev " Enter " (mopstringnam fun) " " mlargs
  535.                  (if info " -> " "")
  536.                  (if info info "")))))))
  537.  
  538. (defun mopstringnam (x) (maknam (mstring (getop x))))
  539.  
  540. (defun trace-exit-print (fun lev ret-val)
  541.        (if (not (trace-option-p fun '$noprint))
  542.        (let ((info (trace-option-p fun '$info)))
  543.         (cond ((trace-option-p fun '$lisp_print)
  544.                (trace-print `(,lev exit ,fun ,ret-val ,@info)))
  545.               (t
  546.                (trace-mprint lev " Exit  " (mopstringnam fun) " " ret-val
  547.                      (if info " -> " "")
  548.                      (if info info "")))))))
  549.  
  550. (defmvar $trace_break_arg '$TRACE_BREAK_ARG 
  551.      "During trace Breakpoints bound to the argument list or return value")
  552.  
  553. (defun trace-enter-break (fun lev largs)
  554.        (if (trace-option-p fun '$break)
  555.        (do ((return-to-trace-handle nil)
  556.         ($trace_break_arg `((mlist) ,@largs)))(nil)
  557.            ($break '|&Trace entering| fun '|&level| lev)
  558.            (cond (($listp $trace_break_arg)
  559.               (return (cdr $trace_break_arg)))
  560.              (t
  561.                (mtell "~%Trace_break_arg set to nonlist, ~
  562.                   please try again"))))
  563.        largs))
  564.  
  565. (defun trace-exit-break (fun lev ret-val)
  566.        (if (trace-option-p fun '$break)
  567.        (let (($trace_break_arg ret-val)
  568.          (return-to-trace-handle nil))
  569.         ($break '|&Trace exiting| fun '|&level| lev)
  570.         $trace_break_arg)
  571.        ret-val))
  572.  
  573. (defun pred-$read (predicate argl bad-message)
  574.        (do ((ans))(nil)
  575.        (setq ans (apply #'$read argl))
  576.        (if (funcall predicate ans) (return ans))
  577.        (mtell "~%Unacceptable input, ~A~%" bad-message)))
  578.  
  579. (declare-top(special upper))
  580.  
  581. (defun ask-choicep (llist &rest header-message)
  582.        (do ((j 0 (f1+ j))
  583.         (dlist nil
  584.            (list* "M" `((marrow) ,j ,(car ilist)) dlist))
  585.         (ilist llist (cdr ilist)))
  586.        ((null ilist)
  587.         (setq dlist (nconc header-message (cons "M" (nreverse dlist))))
  588.         (let ((upper (f1- j)))
  589.          (pred-$read #'(lambda (val)
  590.                       (and (integerp val)
  591.                        (>= val 0)
  592.                        (<= val upper)))
  593.                 dlist
  594.                 "please reply with an integer from the menue.")))))
  595.  
  596. (declare-top (unspecial upper))
  597.  
  598. (defun trace-error-break (fun level largs)
  599.        (case (ask-choicep '("Signal an MAXIMA-ERROR, i.e. PUNT?"
  600.                  "Retry with same arguments?"
  601.                  "Retry with new arguments?"
  602.                  "Exit with user supplied value")
  603.                "Error during application of" (mopstringnam fun)
  604.                "at level" level
  605.                "M" "Do you want to:")
  606.           ((0)
  607.            '(MAXIMA-ERROR))
  608.           ((1)
  609.            (cons 'retry largs))
  610.           ((2)
  611.            (cons 'retry (let (($trace_break_arg `((mlist) ,largs)))
  612.                  (cdr (pred-$read '$listp
  613.                          (list
  614.                           "Enter new argument list for"
  615.                           (mopstringnam fun))
  616.                          "please enter a list.")))))
  617.                        
  618.           ((3)
  619.            (cons 'exit ($read "Enter value to return")))))
  620.  
  621.  
  622. ;;; application dispatch, and the consing up of the trace hook.
  623.  
  624. (defun macsyma-fsymeval (fun)
  625.        (let ((try (macsyma-fsymeval-sub fun)))
  626.         (cond (try try)
  627.           ((get fun 'autoload)
  628.            (load-and-tell (get fun 'autoload))
  629.            (setq try (macsyma-fsymeval-sub fun))
  630.            (or try
  631.                (mtell "~%~:@M has no functional~
  632.                   properties after autoloading.~%"
  633.                   fun))
  634.            try)
  635.           (t try))))
  636.  
  637. (defun macsyma-fsymeval-sub (fun)
  638.        ;; The semantics of $TRANSRUN are herein taken from DESCRIBE,
  639.        ;; a carefull reading of MEVAL1 reveals, well... I've promised to watch
  640.        ;; my language in these comments.
  641.  
  642.        (let ((mprops (mgetl fun '(mexpr mmacro)))
  643.          (lprops (getl  fun '(translated-mmacro mfexpr* mfexpr*s)))
  644.          (fcell-props (getl-fun fun '(subr lsubr expr fexpr macro fsubr))))
  645.         (cond ($TRANSRUN
  646.            ;; the default, so its really a waste to have looked for
  647.            ;; those mprops. Its better to fix the crock than to
  648.            ;; optimize this though!
  649.            (or lprops fcell-props mprops))
  650.           (t
  651.            (or mprops lprops fcell-props)))))
  652.  
  653. (Defprop EXPR EXPR HOOK-TYPE)
  654. (DEFPROP MEXPR EXPR HOOK-TYPE)
  655. (Defprop SUBR EXPR HOOK-TYPE)
  656. (Defprop LSUBR EXPR HOOK-TYPE)
  657. (Defprop FEXPR FEXPR HOOK-TYPE)
  658. (Defprop FSUBR FEXPR HOOK-TYPE)
  659. (Defprop MFEXPR* MACRO HOOK-TYPE)
  660. (Defprop MFEXPR*S MACRO HOOK-TYPE)
  661.  
  662. #+Maclisp
  663. (defun make-trace-hook (fun type handler)
  664.   (CASE (GET! TYPE 'HOOK-TYPE)
  665.     ((EXPR)
  666.      `(lambda trace-nargs
  667.     (,handler ',fun (listify trace-nargs))))
  668.     ((FEXPR)
  669.      `(LAMBDA (TRACE-ARGL)
  670.     (,HANDLER ',FUN TRACE-ARGL)))
  671.     ((MACRO)
  672.      `(lambda (TRACE-FORM)
  673.     (,HANDLER (CAAR TRACE-FORM) (LIST TRACE-FORM))))))
  674.  
  675. #+Franz
  676. (defun make-trace-hook (fun type handler)
  677.   (CASE (GET! TYPE 'HOOK-TYPE)
  678.     ((EXPR)
  679.      `(lexpr (trace-nargs)
  680.          (,handler ',fun (listify trace-nargs))))
  681.     ((FEXPR)
  682.      `(LAMBDA (TRACE-ARGL)
  683.     (,HANDLER ',FUN TRACE-ARGL)))
  684.     ((MACRO)
  685.      `(lambda (TRACE-FORM)
  686.     (,HANDLER (CAAR TRACE-FORM) (LIST TRACE-FORM))))))
  687.  
  688. #+cl
  689. (defun make-trace-hook (fun type handler)
  690.   ;; Argument handling according to FUN's TYPE is already done
  691.   ;; elsewhere: HANDLER, meval...
  692.   (declare (ignore type))
  693.   #'(lambda (&rest trace-args)
  694.       (funcall handler fun trace-args)))
  695.        
  696. #+Maclisp
  697. (defmacro trace-setup-call (prop fun type)  
  698.   `(args 'the-trace-apply-hack (args ,fun))
  699.   `(setplist 'the-trace-apply-hack (list ,type ,prop)))
  700.  
  701. #+Franz
  702. (defmacro trace-setup-call (prop fun type)
  703.   `(putd 'the-trace-apply-hack ,prop))
  704.  
  705. #+cl
  706. (defmacro trace-setup-call (prop fun type) fun type
  707.   `(setf (symbol-function  'the-trace-apply-hack)  ,prop))
  708.  
  709. (defun trace-apply (fun largs)
  710.        (let ((prop (trace-fsymeval fun))
  711.          (type (trace-type fun))
  712.          (return-to-trace-handle nil))
  713.         (case type
  714.            ((mexpr)
  715.             (mapply prop largs "&A traced function"))
  716.            ((expr)
  717.             (apply prop largs))
  718.            ((subr lsubr)
  719.             ;; no need to be fast here.
  720.             (args 'the-trace-apply-hack (args fun))
  721.             (setplist 'the-trace-apply-hack (list type prop))
  722.             #-cl (apply 'the-trace-apply-hack largs)
  723.             #+cl (apply (second (getl 'the-trace-apply-hack '(subr lsubr))) largs)
  724.             )
  725.            ((MFEXPR*)
  726.             (FUNCALL PROP (CAR LARGS)))
  727.            ((MFEXPR*S)
  728.             (SUBRCALL NIL PROP (CAR LARGS)))
  729.            ((FEXPR)
  730.             (FUNCALL PROP LARGS))
  731.            ((FSUBR)
  732.             (SUBRCALL NIL PROP LARGS)))))
  733.  
  734. ;;; I/O cruft
  735.  
  736. (defmvar $trace_max_indent 15. "max number of spaces it will go right"
  737.      FIXNUM)
  738. (putprop '$trace_max_indent 'assign-mode-check 'assign)
  739. (putprop '$trace_max_indent '$fixnum 'mode)
  740.  
  741. (defun-prop (spaceout dimension) (form result)
  742.        (dimension-string (*make-list (cadr form) #\Space) result))
  743.  
  744. (defun trace-mprint (&rest l)
  745.        (mtell-open "~M"
  746.            `((mtext)
  747.              ((spaceout) ,(min $trace_max_indent trace-indent-level))
  748.              ,@ (copy-rest-arg l))))
  749.  
  750. (defun trace-print (form)
  751.        (terpri)
  752.        (do ((j (min $trace_max_indent trace-indent-level)
  753.            (f1- j)))
  754.        ((not (> j 0)))
  755.        (tyo #\Space))
  756.        (if prin1 (funcall prin1 form)
  757.        (prin1 form))
  758.        (tyo #\Space))
  759.  
  760.  
  761. ;; 9:02pm  Monday, 18 May 1981 -GJC
  762. ;; A function benchmark facility using trace utilities.
  763. ;; This provides medium accuracy, enough for most user needs.
  764.  
  765. (DEFMVAR $TIMER '((MLIST)) "List of functions under active timetrace")
  766. #-cl
  767. (PUTPROP '$TIMER #'READ-ONLY-ASSIGN 'ASSIGN)
  768.  
  769. (DEFMSPEC $TIMER (FORM)
  770.   (MLISTCAN-$ALL #'macsyma-timer (cdr form) $timer))
  771.  
  772. (DEFMSPEC $UNTIMER (FORM)
  773.   `((MLIST) ,@(MAPCAN #'MACSYMA-UNTIMER (OR (CDR FORM)
  774.                         (CDR $TIMER)))))
  775.  
  776. (DEFUN MICRO-TO-SEC (RUNTIME)
  777.   (MUL RUNTIME (float (/ internal-time-units-per-second)) '$SEC))
  778. (DEFUN MICRO-PER-CALL-TO-SEC (RUNTIME CALLS)
  779.   (DIV (MICRO-TO-SEC RUNTIME)
  780.        (IF (ZEROP CALLS) 1 CALLS)))
  781.  
  782. (DEFUN TIMER-MLIST (FUNCTION CALLS RUNTIME GCTIME)
  783.   `((MLIST SIMP) ,FUNCTION
  784.          ,(MICRO-PER-CALL-TO-SEC (PLUS RUNTIME GCTIME) CALLS)
  785.          ,CALLS
  786.          ,(MICRO-TO-SEC RUNTIME)
  787.          ,(MICRO-TO-SEC GCTIME)))
  788.  
  789. (DEFMSPEC $TIMER_INFO (FORM)
  790.   (DO ((L (OR (CDR FORM) (CDR $TIMER))
  791.       (CDR L))
  792.        (V NIL)
  793.        (TOTAL-RUNTIME 0)
  794.        (TOTAL-GCTIME 0)
  795.        (TOTAL-CALLS 0))
  796.       ((NULL L)
  797.        `(($matrix simp)
  798.      ((MLIST SIMP) $FUNCTION $TIME//CALL $CALLS $RUNTIME $GCTIME)
  799.      ,.(NREVERSE V)
  800.      ,(TIMER-MLIST '$TOTAL TOTAL-CALLS TOTAL-RUNTIME TOTAL-GCTIME)))
  801.       (LET ((RUNTIME ($GET (CAR L) '$RUNTIME))
  802.         (GCTIME  ($GET (CAR L) '$GCTIME))
  803.         (CALLS   ($GET (CAR L) '$CALLS)))
  804.     (WHEN RUNTIME
  805.           (SETQ TOTAL-CALLS (PLUS CALLS TOTAL-CALLS))
  806.           (SETQ TOTAL-RUNTIME (PLUS RUNTIME TOTAL-RUNTIME))
  807.           (SETQ TOTAL-GCTIME (PLUS GCTIME TOTAL-GCTIME))
  808.           (PUSH (TIMER-MLIST (CAR L) CALLS RUNTIME GCTIME) V)))))
  809.  
  810. (DEFUN macsyma-timer (fun)
  811.   (PROG1 (macsyma-trace-sub fun 'timer-handler $timer)
  812.      ($PUT FUN 0 '$RUNTIME)
  813.      ($PUT FUN 0 '$GCTIME)
  814.      ($PUT FUN 0 '$CALLS)
  815.      ))
  816.  
  817. (defun macsyma-untimer (fun) (macsyma-untrace-sub fun 'timer-handler $timer))
  818.  
  819. (DEFVAR RUNTIME-DEVALUE 0)
  820. (DEFVAR GCTIME-DEVALUE 0)
  821.  
  822. (DEFMVAR $TIMER_DEVALUE NIL
  823.   "If true, then time spent inside calls to other timed functions is
  824.   subtracted from the timing figure for a function.")
  825.  
  826. (DEFUN TIMER-HANDLER (FUN LARGS)
  827.   ;; N.B. Doesn't even try to account for use of DYNAMIC CONTROL
  828.   ;; such as ERRSET ERROR and CATCH and THROW, as these are
  829.   ;; rare and the overhead for the unwind-protect is high.
  830.   (LET ((RUNTIME (RUNTIME))
  831.     (GCTIME (STATUS GCTIME))
  832.     (OLD-RUNTIME-DEVALUE RUNTIME-DEVALUE)
  833.     (OLD-GCTIME-DEVALUE GCTIME-DEVALUE))
  834.     (PROG1 (TRACE-APPLY FUN LARGS)
  835.        (SETQ OLD-RUNTIME-DEVALUE (- RUNTIME-DEVALUE OLD-RUNTIME-DEVALUE))
  836.        (SETQ OLD-GCTIME-DEVALUE (- GCTIME-DEVALUE OLD-GCTIME-DEVALUE))
  837.        (SETQ RUNTIME (- (RUNTIME) RUNTIME OLD-RUNTIME-DEVALUE))
  838.        (SETQ GCTIME (- (STATUS GCTIME) GCTIME OLD-GCTIME-DEVALUE))
  839.        (WHEN $TIMER_DEVALUE
  840.          (SETQ RUNTIME-DEVALUE (f+ RUNTIME-DEVALUE RUNTIME))
  841.          (SETQ GCTIME-DEVALUE (f+ GCTIME-DEVALUE GCTIME)))
  842.        ($PUT FUN (f+ ($GET FUN '$RUNTIME) RUNTIME) '$RUNTIME)
  843.        ($PUT FUN (f+ ($GET FUN '$GCTIME) GCTIME) '$GCTIME)
  844.        ($PUT FUN (f1+ ($GET FUN '$CALLS)) '$CALLS))))
  845.  
  846.