Debian Codegraph @

src/lsp/trace.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1990, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 ;;;; Tracer package for Common Lisp
11
12 (in-package "SYSTEM")
13
14 (defparameter *trace-level* 0)
15 ;; (defparameter *trace-list* nil) ; In all_symbols.d !
16 (defparameter *trace-max-indent* 20)
17
18 (defmacro trace (&rest r)
19 "Syntax: (trace ({function-name | ({function-name}+)} {keyword [form]\}*)
20 Begins tracing the specified functions. With no FUNCTION-NAMEs, returns a
21 list of functions currently being traced. The printed information consists of
22 the name of function followed at entry by its arguments and on exit by its
23 return values.
24 The keywords allow to control when and how tracing is performed.
25 The possible keywords are:
26
27 :BREAK a breakpoint is entered after printing the entry trace
28 information, but before applying the traced function to its
29 arguments, if form evaluates to non-nil
30 :BREAK-AFTER like :BREAK but the breakpoint is entered after the function
31 has been executed and the exit trace information has been
32 printed and before control returns
33 :COND-BEFORE information is printed upon entry if form evaluates to non-nil
34 :COND-AFTER information is printed upon exit if form evaluates to non-nil
35 :COND specifies a single condition for both entry and exit
36 :PRINT prints the values of the forms in the list upon entry.
37 They are preceeded by a backslash (\\)
38 :PRINT-AFTER prints the values of the forms in the list upon exit from the
39 function. They are preceeded by a backslash (\\)
40 :STEP turns on the stepping facility
41
42 Forms can refer to the list of arguments of the function through the variable
43 SI::ARGS."
44 `(trace* ',r))
45
46 (defun trace* (r)
47 (if (null r)
48 (mapcar #'first *trace-list*)
49 (mapc #'trace-one r)))
50
51 (defmacro untrace (&rest r)
52 "Syntax: (untrace {function-name}*)
53
54 Ends tracing the specified functions. With no FUNCTION-NAMEs, ends tracing
55 all functions."
56 `(untrace* ',r))
57
58 (defun untrace* (r)
59 (mapc #'untrace-one (or r (trace* nil))))
60
61 (defparameter *inside-trace* nil)
62
63 (defun trace-one (spec)
64 (let* (break exitbreak (entrycond t) (exitcond t) entry exit
65 step (barfp t) fname oldf)
66 (cond ((si::valid-function-name-p spec)
67 (setq fname spec))
68 ((not (si::proper-list-p spec))
69 (error "Not a valid argument to TRACE: ~S" spec))
70 ((si::valid-function-name-p (first spec))
71 (setq fname (first spec))
72 (do ((specs (cdr spec) (cdr specs)))
73 ((null specs))
74 (case (car specs)
75 (:break (setq barfp specs specs (cdr specs) break (car specs)))
76 (:break-after (setq barfp specs specs (cdr specs) exitbreak (car specs)))
77 (:step (setq step t))
78 (:cond (setq barfp specs specs (cdr specs))
79 (setq exitcond (setq entrycond (car specs))))
80 (:cond-before (setq barfp specs specs (cdr specs) entrycond (car specs)))
81 (:cond-after (setq barfp specs specs (cdr specs) exitcond (car specs)))
82 (:print (setq barfp specs specs (cdr specs) entry (car specs)))
83 (:print-after (setq barfp specs specs (cdr specs) exit (car specs)))
84 (t (error "Meaningless TRACE keyword: ~S" (car specs))))
85 (unless barfp (error "Parameter missing"))))
86 ((si::proper-list-p (first spec))
87 (let (results)
88 (dolist (fname (first spec))
89 (push (trace-one (list* fname (rest spec))) results))
90 (return-from trace-one (nreverse results))))
91 (t
92 (error "Not a valid argument to TRACE: ~S" spec)))
93 (when (null (fboundp fname))
94 (warn "The function ~S is not defined." fname)
95 (return-from trace-one nil))
96 (when (symbolp fname)
97 (when (special-operator-p fname)
98 (warn "Unable to trace special form ~S." fname)
99 (return-from trace-one nil))
100 (when (macro-function fname)
101 (warn "Unable to trace macro ~S." fname)
102 (return-from trace-one nil)))
103 (let ((record (trace-record fname)))
104 (when record
105 (cond ((traced-and-redefined-p record)
106 (delete-from-trace-list fname))
107 (t
108 (warn "The function ~S is already traced. Statement has no effect." fname)
109 (return-from trace-one nil)))))
110 (setq oldf (fdefinition fname))
111 (eval
112 `(defun ,fname (&rest args)
113 (let* (values (*trace-level* (1+ *trace-level*)))
114 (if *inside-trace*
115 (setq values (multiple-value-list (apply ',oldf args)))
116 (let ((*inside-trace* t))
117 ,@(when entrycond
118 (if (eq t entrycond)
119 `((trace-print 'ENTER ',fname args ,@entry))
120 `((when ,entrycond
121 (trace-print 'ENTER ',fname args ,@entry)))))
122 ,@(when break
123 `((when ,break (let (*inside-trace*)
124 (break "tracing ~S" ',fname)))))
125 (setq values
126 (let (*inside-trace*)
127 (multiple-value-list
128 (apply ',oldf args)
129 #+nil
130 ,(if step
131 `(let (*step-quit*)
132 (applyhook ',oldf args #'stepper nil))
133 `(apply ',oldf args)))))
134 ,@(when exitcond
135 (if (eq t exitcond)
136 `((trace-print 'EXIT ',fname values ,@exit))
137 `((when ,exitcond
138 (trace-print 'EXIT ',fname values ,@exit)))))
139 ,@(when exitbreak
140 `((when ,exitbreak
141 (let (*inside-trace*)
142 (break "after tracing ~S" ',fname)))))))
143 (values-list values))))
144 (add-to-trace-list fname oldf)
145 (list fname)))
146
147 (defun trace-print (direction fname vals &rest extras)
148 (let ((indent (min (* (1- *trace-level*) 2) *trace-max-indent*))
149 (*print-circle* t))
150 (princ
151 (with-output-to-string (*trace-output*)
152 (fresh-line *trace-output*)
153 (case direction
154 (ENTER
155 (multiple-value-bind (bars rem)
156 (floor indent 4)
157 (dotimes (i bars) (princ (if (< i 10) "| " "| ") *trace-output*))
158 (when (plusp rem) (format *trace-output* "~V,,,' A" rem "|")))
159 (format *trace-output*
160 "~D> (~S~{ ~S~})~%"
161 *trace-level* fname vals))
162 (EXIT
163 (multiple-value-bind (bars rem)
164 (floor indent 4)
165 (dotimes (i bars) (princ "| " *trace-output*))
166 (when (plusp rem) (format *trace-output* "~V,,,' A" rem "|")))
167 (format *trace-output*
168 "<~D (~S~{ ~S~})~%"
169 *trace-level*
170 fname vals)
171 ))
172 (when extras
173 (multiple-value-bind (bars rem)
174 (floor indent 4)
175 (dotimes (i bars) (princ "| " *trace-output*))
176 (when (plusp rem) (format *trace-output* "~V,,,' A" rem "|")))
177 (format *trace-output*
178 "~0,4@T\\\\ ~{ ~S~}~%"
179 extras))
180 *trace-output*)
181 *trace-output*)))
182
183 (defun trace-record (fname)
184 (declare (si::c-local))
185 (find fname *trace-list* :key #'first :test #'equal))
186
187 (defun trace-record-name (record)
188 (declare (si::c-local))
189 (first record))
190
191 (defun trace-record-definition (record)
192 (declare (si::c-local))
193 (second record))
194
195 (defun trace-record-old-definition (record)
196 (declare (si::c-local))
197 (third record))
198
199 (defun traced-old-definition (fname)
200 (let ((record (trace-record fname)))
201 (when record
202 (unless (traced-and-redefined-p record)
203 (trace-record-old-definition record)))))
204
205 (defun delete-from-trace-list (fname)
206 (setq *trace-list* (delete fname *trace-list* :key #'first :test #'equal)))
207
208 (defun add-to-trace-list (fname old-definition)
209 (push (list fname (fdefinition fname) old-definition)
210 *trace-list*))
211
212 (defun traced-and-redefined-p (record)
213 (declare (si::c-local))
214 (and record (not (eq (trace-record-definition record)
215 (fdefinition (trace-record-name record))))))
216
217 (defun untrace-one (fname)
218 (let ((record (trace-record fname)))
219 (cond ((null record)
220 (warn "The function ~S was not traced." fname))
221 ((traced-and-redefined-p record)
222 (warn "The function ~S was traced, but redefined." fname))
223 (t
224 (sys:fset fname (trace-record-old-definition record))))
225 (delete-from-trace-list fname)
226 (values)))
227
228 (defparameter *step-level* 0)
229 (defparameter *step-action* nil)
230 (defparameter *step-form* nil)
231 (defparameter *step-tag* (cons nil nil))
232 (defparameter *step-functions* nil)
233 (defconstant step-commands
234 `("Stepper commands"
235 ((:newline) (step-next) :constant
236 "newline Advance to the next form"
237 "newline [Stepper command]~@
238 ~@
239 Step to next form.~%")
240 ((:s :skip) step-skip nil
241 ":s(kip) Skip current form or until function"
242 ":skip &optional arg [Stepper command]~@
243 :s &optional arg [Abbreviation]~@
244 ~@
245 Continue evaluation without stepping. Without argument, resume~@
246 stepping after the current form. With numeric argument (n),~@
247 resume stepping at the n-th level above. With function name, resume~@
248 when given function is called.~%")
249 ((:pr :print) (step-print) :constant
250 ":pr(int) Pretty print current form"
251 ":print [Stepper command]~@
252 :pr [Abbreviation]~@
253 ~@
254 Pretty print current form.~%")
255 ((:form) *step-form* :constant
256 ":form Current form"
257 ":form [Stepper command]~@
258 ~@
259 Return the current form. Nothing is done, but the current form~@
260 is returned as the value of this command. As a consequence,~@
261 it is printed by the top level in the usual way and saved in~@
262 the variable *. The main purpose of this command is to allow~@
263 the current form to be examined further by accessing *.~%")
264 ((:x :exit) (step-quit) :constant
265 ":x or :exit Finish evaluation and exit stepper"
266 ":exit [Stepper command]~@
267 :x [Abbreviation]~@
268 ~@
269 Finish evaluation without stepping.~%")
270 ))
271
272 (defun stepper-enter (form)
273 (etypecase *step-action*
274 ((eql t)
275 (incf *step-level*)
276 (stepper form))
277 ((integer 0)
278 (incf *step-action*))))
279
280 (defun stepper-leave (form)
281 (declare (ignore form))
282 (typecase *step-action*
283 ((eql t)
284 (decf *step-level*))
285 ((integer 1)
286 (decf *step-action*))
287 ((integer 0 0)
288 (setf *step-action* t))))
289
290 (defun stepper-call (form)
291 (if (eq *step-action* t)
292 (stepper form)
293 form))
294
295 (defun stepper-hook (form delta)
296 (cond
297 ((= delta +1)
298 (stepper-enter form))
299 ((= delta -1)
300 (stepper-leave form))
301 ((stepper-call form))))
302
303 (defmacro step (form)
304 "Syntax: (step form)
305 Evaluates FORM in the Stepper mode and returns all its values. See ECL Report
306 for Stepper mode commands."
307 `(step* ',form))
308
309 (defun step* (form)
310 (let* ((*step-action* t)
311 (*step-level* 0)
312 (*step-functions* (make-hash-table :size 128 :test 'eq)))
313 (catch *step-tag*
314 (si:eval-with-env form nil t))))
315
316 (defun steppable-function (form)
317 (let ((*step-action* nil))
318 (or (gethash form *step-functions*)
319 (multiple-value-bind (f env name)
320 (function-lambda-expression form)
321 (if (and (not (trace-record name)) f)
322 (setf (gethash form *step-functions*)
323 (eval-with-env `(function ,f) env t))
324 form)))))
325
326 (defun stepper (form)
327 (when (typep form '(or symbol function))
328 (return-from stepper (steppable-function (coerce form 'function))))
329 (let* ((*step-form* form)
330 (*step-action* nil)
331 (indent (min (* *tpl-level* 2) 20))
332 prompt)
333 (setq prompt
334 #'(lambda ()
335 (format *debug-io* "~VT" indent)
336 (write form :stream *debug-io* :pretty nil
337 :level 2 :length 2)
338 (princ #\space *debug-io*)
339 (princ #\- *debug-io*)))
340 (when (catch *step-tag*
341 (tpl :quiet t
342 :commands (adjoin step-commands
343 (adjoin break-commands *tpl-commands*))
344 :broken-at 'stepper
345 :prompt-hook prompt))
346 (throw *step-tag* t))))
347
348 (defun step-next ()
349 (throw *step-tag* nil))
350
351 (defun step-skip (&optional (when 0))
352 (declare (ignore when))
353 (setf *step-action* 0)
354 (throw *step-tag* nil))
355
356 (defun step-print ()
357 (write *step-form* :stream *debug-io* :pretty t :level nil :length nil)
358 (terpri)
359 (values))
360
361 (defun step-quit ()
362 (throw *step-tag* t))