From f99de75d394a7accfa8c9b285fd0a2e3a37d4017 Mon Sep 17 00:00:00 2001
From: chiles <chiles>
Date: Sun, 3 Nov 1991 17:21:16 +0000
Subject: [PATCH] Added CLEAR-TRACE-BREAKPOINT-RECORD to
 ext:*setf-fdefinition-hook*.

Fixed package system, so the old TRACE and new one didn't conflict with
variable names.

Added debug:*trace-frame* to support users who want to enter expressions for
evaluation in the frame.  This is described in the TRACE doc string.

Fixed TRACE interface to support any function name, not just symbols.
Underlying support allowed this, but TRACE complained when it inspected its
arguments.

Added full support for documented interface of TRACE since before it only
allowed users to see arguments and output without conditionalization, printing,
etc.
---
 code/ntrace.lisp | 195 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 144 insertions(+), 51 deletions(-)

diff --git a/code/ntrace.lisp b/code/ntrace.lisp
index a8306ea7d..425b27bb3 100644
--- a/code/ntrace.lisp
+++ b/code/ntrace.lisp
@@ -7,7 +7,7 @@
 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
 ;;;
 (ext:file-comment
-  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/ntrace.lisp,v 1.1 1991/10/13 14:29:30 chiles Exp $")
+  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/ntrace.lisp,v 1.2 1991/11/03 17:21:16 chiles Exp $")
 ;;;
 ;;; **********************************************************************
 ;;;
@@ -20,30 +20,37 @@
 ;;; **********************************************************************
 ;;;
 
-(in-package "DEBUG")
+(in-package "LISP")
 
-;;; BECAUSE SOMEONE SLOPPILY MADE THE "DEBUG" PACKAGE USE "EXT", I HAVE TO BEND
-;;; OVER BACKWARDS NOW TO PREVENT STEPPING ON STUFF EXPORTED BY THE OLD TRACE
-;;; CODE FROM "EXT".  THAT'S WHY THERE ARE ALL THE FUNNY NAMES.  NO, I DON'T
-;;; FEEL LIKE CLEANING UP THE "DEBUG" PACKAGE ONCE AGAIN.
-;;;
+(export '(trace untrace))
+
+
+(in-package "DEBUG")
 
-(export '(*n-trace-print-level* *n-trace-print-length* *n-traced-function-list*
-	  n-trace n-untrace))
+(export '(*trace-print-level* *trace-print-length* *traced-function-list*
+	  *trace-frame* *max-trace-indentation*))
 
-(defvar *n-traced-function-list* ()
+(defvar *traced-function-list* nil
   "A list of function names which are traced.")
 
-(defvar *n-trace-print-length* ()
-  "*Print-length* will be bound to this value when trace is printing.")
+(defvar *trace-print-length* nil
+  "Tracing output occurs with *print-length* bound to this value.")
+
+(defvar *trace-print-level* nil
+  "Tracing output occurs with *print-level* bound to this value.")
 
-(defvar *n-trace-print-level* ()
-  "*Print-level* will be bound to this value when trace is printing.")
+(defvar *trace-frame* nil
+  "TRACE causes expressions for its switches to evaluate within a context
+   where this is bound to the appropriate control stack frame.")
 
+(defvar *max-trace-indentation* nil
+  "This is currently unused.")
 
 
 ;;;; TRACE.
 
+(eval-when (compile eval)
+
 ;;; WITH-KEYWORDS -- Internal.
 ;;;
 ;;; This takes an options list of the following form:
@@ -62,11 +69,14 @@
 		 key-list)
      ,@body))
 
-;;; N-TRACE -- Public.
+) ;EVAL-WHEN
+
+
+;;; TRACE -- Public.
 ;;;
-(defmacro n-trace (&rest specs)
+(defmacro trace (&rest specs)
   "Establishes tracing for specified functions and pushes their names on
-   *n-traced-function-list*.  Each specification is either the name of a function
+   *traced-function-list*.  Each specification is either the name of a function
    or a list of the form:
       (function-name <trace-option> <value> <trace-option> <value> ...)
    If you supply no specifications, TRACE returns the list of traced functions.
@@ -87,9 +97,15 @@
       :print-after
          Like :print, but takes effect after the call.
       :print-all
-         Like :print, but takes effect before and after the call."
+         Like :print, but takes effect before and after the call.
+
+   While the provided expression evaluate, debug:*trace-frame* is bound to the
+   appropriate frame for accessing locals of the TRACE'd function.  Therefore,
+   you can use DI:PREPROCESS-FOR-EVAL (and DI:DEBUG-FUNCTION-START-LOCATION if
+   necessary) and with its resulting function, provide an expression including
+   a FUNCALL of the function and debug:*trace-frame*."
   (cond
-   ((not specs) '*n-traced-function-list*)
+   ((not specs) '*traced-function-list*)
    (t
     (let ((name-list nil)
 	  (trace-1-forms nil))
@@ -104,11 +120,16 @@
 	      (symbol
 	       (values spec nil))
 	      (list
-	       (unless (symbolp (car spec))
-		 (error "Illegal function name:  ~S." (car spec)))
-	       (when (eq (car spec) 'quote)
-		 (error "I bet you don't want to trace QUOTE."))
-	       (values (car spec) (cdr spec)))
+	       (let ((fun (car spec)))
+		 (cond ((eq fun 'quote)
+			(error "Do NOT quote function names."))
+		       ((symbolp fun)
+			(values fun (cdr spec)))
+		       ((not (and (consp fun) (= (length fun) 2)))
+			(error "Illegal function name:  ~S." fun))
+		       ((eq (car fun) 'setf)
+			(values fun (cdr spec)))
+		       (t (error "Illegal function name:  ~S." fun)))))
 	      (t (error "Illegal trace spec:  ~S." spec)))
 	  (push name name-list)
 	  (with-keywords options
@@ -130,7 +151,10 @@
 		    (null nil)
 		    (symbol (list wherein))
 		    (list (dolist (fun wherein wherein)
-			    (unless (symbolp fun)
+			    (unless (or (symbolp fun)
+					(and (consp fun)
+					     (= (length fun) 2)
+					     (eq (car fun) 'setf)))
 			      (error "Illegal function name, ~S, in :wherein."
 				     fun))))
 		    (t (error "Illegal :wherein option:  ~S." wherein))))
@@ -139,7 +163,7 @@
 	      (error "Illegal form list, ~S, for :print." print))
 	    (unless (listp print-after)
 	      (error "Illegal form list, ~S, for :print-after." print-after))
-	    (push `(n-trace-1 ',name ',condition ',break ',break-after
+	    (push `(trace-1 ',name ',condition ',break ',break-after
 			    ',wherein ',print ',print-after)
 		  trace-1-forms))))))))
 
@@ -162,46 +186,101 @@
 ;;; can get rid of them in UNTRACE-1.
 ;;;
 (defvar *trace-breakpoints* (make-hash-table :test #'eq))
+;;;
+(defun clear-trace-breakpoint-record (fname new-value)
+  (declare (ignore new-value))
+  (let ((bpts (gethash fname *trace-breakpoints*)))
+    (when bpts
+      ;; Free breakpoint bookkeeping data.
+      (di:delete-breakpoint (car bpts))
+      (di:delete-breakpoint (cdr bpts))
+      ;; Free TRACE bookkeeping data.
+      (setf (gethash fname *trace-breakpoints*) nil))))
+;;;
+(push #'clear-trace-breakpoint-record ext:*setf-fdefinition-hook*)
 
-;;; N-TRACE-1 -- Internal.
+;;; TRACE-1 -- Internal.
 ;;;
 ;;; This establishes :function-start and :function-end breakpoints with
 ;;; appropriate hook functions to TRACE function-name as described by the user.
 ;;;
-(defun n-trace-1 (function-name condition break break-after wherein print
+(defun trace-1 (function-name condition break break-after where-in print
 		 print-after)
-  (declare (ignore condition break break-after wherein print print-after))
   (cond
-   ((member function-name *n-traced-function-list*)
+   ((member function-name *traced-function-list*)
     (warn "Function ~S already TRACE'd, ignoring this request."
 	  function-name))
    (t
+    (when where-in
+      (dolist (f where-in)
+	(unless (fboundp f)
+	  (error "Undefined :where-in name -- ~S." f))))
     (let* ((debug-fun (di:function-debug-function (fdefinition function-name)))
-	   (start (di:make-breakpoint #'print-trace-start
-				      debug-fun
-				      :kind :function-start))
-	   (end (di:make-breakpoint #'print-trace-end
-				    debug-fun
-				    :kind :function-end
-				    :function-end-cookie
-				    #'(lambda (x) (push x *traced-entries*)))))
+	   ;; The start and end hooks use conditionp for communication.
+	   (conditionp nil)
+	   (start (di:make-breakpoint
+		   #'(lambda (frame bpt)
+		       (let ((*trace-frame* frame))
+			 (cond ((and (or (not condition) ;Save a call to EVAL
+					 (eval condition))
+				     (or (not where-in)
+					 (trace-where-in-p frame where-in)))
+				(setf conditionp t)
+				(print-trace-start frame bpt print))
+			       (t (setf conditionp nil)))
+			 (when (and break (eval break))
+			   (break "Breaking before TRACE'd call to ~S."
+				  (di:debug-function-name
+				   (di:frame-debug-function frame))))))
+		   debug-fun :kind :function-start))
+	   (end (di:make-breakpoint
+		 #'(lambda (frame bpt values cookie)
+		     (let ((*trace-frame* frame))
+		       (when conditionp
+			 (print-trace-end frame bpt values cookie print-after))
+		       (pop *traced-entries*)
+		       (when (and break-after (eval break-after))
+			 (break "Breaking after TRACE'd call to ~S."
+				(di:debug-function-name
+				 (di:frame-debug-function frame))))))
+		 debug-fun :kind :function-end
+		 :function-end-cookie
+		 #'(lambda (frame x)
+		     (when (and *traced-entries*
+				(not (di:function-end-cookie-valid-p
+				      frame (car *traced-entries*))))
+		       (format t "~&WARNING: dynamic flow of control occurred ~
+				  while TRACE'ing.~%")
+		       (loop
+			 (pop *traced-entries*)
+			 (when (or (not *traced-entries*)
+				   (di:function-end-cookie-valid-p
+				    frame (car *traced-entries*)))
+			   (return))))
+		     (push x *traced-entries*)))))
       (setf (gethash function-name *trace-breakpoints*) (cons start end))
-      ;; This works TOTALLY SLIMY by the order of the next two calls.
+      ;; The next two forms must be in the order in which they appear.  They
+      ;; rely on a documented property that later activated breakpoint hooks
+      ;; run first, and the end breakpoint establishes a starting helper bpt.
       (di:activate-breakpoint start)
       (di:activate-breakpoint end))
-    (push function-name *n-traced-function-list*))))
+    (push function-name *traced-function-list*))))
 
 ;;; PRINT-TRACE-START -- Internal.
 ;;;
 ;;; This prints a representation of the call establishing frame.
 ;;;
-(defun print-trace-start (frame bpt)
+(defun print-trace-start (frame bpt &optional print)
   (declare (ignore bpt))
-  (let ((*print-length* (or *n-trace-print-length* *print-length*))
-	(*print-level* (or *n-trace-print-level* *print-level*)))
+  (let ((*print-length* (or *trace-print-length* *print-length*))
+	(*print-level* (or *trace-print-level* *print-level*)))
     (fresh-line)
     (print-trace-indentation)
     (print-frame-call-1 frame nil)
+    (dolist (ele print)
+      (fresh-line)
+      (print-trace-indentation)
+      (prin1 (eval ele)))
     (terpri)))
 
 ;;; PRINT-TRACE-END -- Internal.
@@ -211,18 +290,21 @@
 ;;; to see that cookie is at the top of *traced-entries*; if it is not, then we
 ;;; need to adjust this list to determine the correct indentation for output.
 ;;;
-(defun print-trace-end (frame bpt values cookie)
+(defun print-trace-end (frame bpt values cookie &optional print-after)
   (declare (ignore frame bpt))
   (unless (eq cookie (car *traced-entries*))
     (setf *traced-entries* (member cookie *traced-entries*))
     (fresh-line)
     (write-line "WARNING: dynamic flow of control occurred while TRACE'ing."))
   (print-trace-indentation)
-  (pop *traced-entries*)
   (write-string "returned ")
   (dolist (v values)
     (prin1 v)
     (write-char #\space))
+  (dolist (ele print-after)
+    (terpri)
+    (print-trace-indentation)
+    (prin1 (eval ele)))
   (terpri))
 
 (defun print-trace-indentation ()
@@ -231,25 +313,36 @@
     (prin1 len)
     (write-string ": ")))
 
+;;; TRACE-WHERE-IN-P -- Internal.
+;;;
+;;; The TRACE hooks use this for the :where-in arg.
+;;;
+(defun trace-where-in-p (frame names)
+  (do ((frame (di:frame-down frame) (di:frame-down frame)))
+      ((not frame) nil)
+    (when (member (di:debug-function-name (di:frame-debug-function frame))
+		  names :test #'equal)
+      (return t))))
+
 
 
 ;;;; N-UNTRACE.
 
-(defmacro n-untrace (&rest names)
+(defmacro untrace (&rest names)
   "Removes tracing from the functions named.  With no args, untraces all
    functions."
-  (let ((names (or names *n-traced-function-list*))
+  (let ((names (or names *traced-function-list*))
 	(untrace-1-forms nil))
     (dolist (name names `(progn ,@(nreverse untrace-1-forms) t))
       (if (symbolp name)
-	  (push `(n-untrace-1 ',name) untrace-1-forms)
+	  (push `(untrace-1 ',name) untrace-1-forms)
 	  (error "Illegal function name -- ~S." name)))))
 
-(defun n-untrace-1 (name)
-  (cond ((member name *n-traced-function-list*)
+(defun untrace-1 (name)
+  (cond ((member name *traced-function-list*)
 	 (let ((breakpoints (gethash name *trace-breakpoints*)))
 	   (di:delete-breakpoint (car breakpoints))
 	   (di:delete-breakpoint (cdr breakpoints))
 	   (setf (gethash name *trace-breakpoints*) nil))
-	 (setf *n-traced-function-list* (delete name *n-traced-function-list*)))
+	 (setf *traced-function-list* (delete name *traced-function-list*)))
 	(t (warn "Function is not TRACE'd -- ~S." name))))
-- 
GitLab