-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtrace.lisp
executable file
·417 lines (366 loc) · 13.7 KB
/
dtrace.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
;#!/usr/local/bin/sbcl --script
;;;; Hey, Emacs, this is a -*- Mode: Lisp; Syntax: Common-Lisp -*- file!
;;;;
;;;; Of all the languages I know, I like Lisp the best, simply because it's the most beautiful.
;;;; -- Paul Graham
;;;;
;;;; Name: dtrace.lisp
;;;;
;;;; Started: Sat Oct 3 22:00:00 2020
;;;; Modifications:
;;;;
;;;; Purpose:
;;;;
;;;;
;;;;
;;;; Calling Sequence:
;;;;
;;;;
;;;; Inputs:
;;;;
;;;; Outputs:
;;;;
;;;; Example:
;;;;
;;;; Notes:
;;;; https://www.cs.cmu.edu/~dst/Lisp/README
;;;; https://www.cs.cmu.edu/~dst/Lisp/dtrace/
;;;;
;; (load "/home/slytobias/lisp/packages/test.lisp")
(defpackage :dtrace
(:use :common-lisp)
(:export :dtrace :duntrace :*dtrace-print-length* :*dtrace-print-level* :*dtrace-print-circle* :*dtrace-print-pretty* :*dtrace-print-array*))
(in-package :dtrace)
;;; DTRACE is a portable alternative to the Common Lisp TRACE and UNTRACE
;;; macros. It offers a more detailed display than most tracing tools.
;;;
;;; From the book "Common Lisp: A Gentle Introduction to
;;; Symbolic Computation" by David S. Touretzky.
;;; The Benjamin/Cummings Publishing Co., 1990.
;;;
;;; This is the generic version. It should work in any legal Common Lisp.
;;;
;;; User-level routines:
;;; DTRACE - same syntax as TRACE
;;; DUNTRACE - same syntax as UNTRACE
;(in-package "DTRACE" :use '("LISP"))
;(shadowing-import '(dtrace::dtrace dtrace::duntrace) (find-package "USER"))
;(use-package "DTRACE" "USER")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; DTRACE and subordinate routines.
(defparameter *dtrace-print-length* 7)
(defparameter *dtrace-print-level* 4)
(defparameter *dtrace-print-circle* t)
(defparameter *dtrace-print-pretty* nil)
(defparameter *dtrace-print-array* *print-array*)
(defvar *traced-functions* '())
(defvar *trace-level* 0)
(defmacro dtrace (&rest function-names)
"Turns on detailed tracing for specified functions. Undo with DUNTRACE."
(if (null function-names)
(list 'quote *traced-functions*)
(list 'quote (mapcan #'dtrace1 function-names))))
(defun dtrace1 (name)
(unless (symbolp name)
(format *error-output* "~&~S is an invalid function name." name)
(return-from dtrace1 nil))
(unless (fboundp name)
(format *error-output* "~&~S undefined function." name)
(return-from dtrace1 nil))
(when (special-operator-p name)
(format *error-output* "~&Can't trace ~S because it's a special operator." name)
(return-from dtrace1 nil))
(when (member name (trace))
(eval `(untrace ,name))) ;; if they're tracing it, undo their trace
(when (member name (dtrace))
(duntrace1 name)) ;; if we're tracing it, undo our trace ?????
(if (macro-function name)
(trace-macro name)
(trace-function name))
(let ((result (list name)))
(setf *traced-functions* (nconc *traced-functions* result))
result))
;;; The functions below reference DISPLAY-xxx routines that can be made
;;; implementation specific for fancy graphics. Generic versions of
;;; these routines are defined later in this file.
(defmacro with-dtrace-printer-settings (&body body)
`(let ((*print-length* *dtrace-print-length*)
(*print-level* *dtrace-print-level*)
(*print-circle* *dtrace-print-circle*)
(*print-pretty* *dtrace-print-pretty*)
(*print-array* *dtrace-print-array*))
,@body))
;; (defun trace-function (name)
;; (let* ((formal-arglist (fetch-arglist name))
;; (old-defn (symbol-function name))
;; (new-defn
;; #'(lambda (&rest argument-list)
;; (let ((result nil))
;; (display-function-entry name)
;; (let ((*trace-level* (1+ *trace-level*)))
;; (with-dtrace-printer-settings
;; (show-function-args argument-list formal-arglist))
;; (setf result (multiple-value-list (apply old-defn argument-list))))
;; (display-function-return name result)
;; (values-list result)))) )
;; (setf (get name 'original-definition) old-defn
;; (get name 'traced-definition) new-defn
;; (get name 'traced-type) 'defun
;; (symbol-function name) new-defn)))
;; (defun trace-function (name)
;; (let* ((formal-arglist (fetch-arglist name))
;; (old-defn (symbol-function name))
;; (new-defn
;; #'(lambda (&rest argument-list)
;; (display-function-entry name)
;; (let ((result (let ((*trace-level* (1+ *trace-level*)))
;; (with-dtrace-printer-settings
;; (show-function-args argument-list formal-arglist))
;; (multiple-value-list (apply old-defn argument-list)))) )
;; (display-function-return name result)
;; (values-list result)))) )
;; (setf (get name 'original-definition) old-defn
;; (get name 'traced-definition) new-defn
;; (get name 'traced-type) 'defun
;; (symbol-function name) new-defn)))
(defun execute-function (f params args)
(let ((*trace-level* (1+ *trace-level*)))
(with-dtrace-printer-settings
(show-function-args args params))
(multiple-value-list (apply f args))))
(defun trace-function (name)
(let* ((formal-arglist (fetch-arglist name))
(old-defn (symbol-function name))
(new-defn
#'(lambda (&rest argument-list)
(display-function-entry name)
(let ((result (execute-function old-defn formal-arglist argument-list)))
(display-function-return name result)
(values-list result)))) )
(setf (get name 'original-definition) old-defn
(get name 'traced-definition) new-defn
(get name 'traced-type) 'defun
(symbol-function name) new-defn)))
;; (defun trace-macro (name)
;; (let* ((formal-arglist (fetch-arglist name))
;; (old-defn (macro-function name))
;; (new-defn
;; #'(lambda (macro-args env)
;; (let ((result nil))
;; (display-function-entry name 'macro)
;; (let ((*trace-level* (1+ *trace-level*)))
;; (with-dtrace-printer-settings
;; (show-function-args macro-args formal-arglist))
;; (setf result (funcall old-defn macro-args env)))
;; (display-function-return name (list result) 'macro)
;; (values result)))) )
;; (setf (get name 'original-definition) old-defn
;; (get name 'traced-definition) new-defn
;; (get name 'traced-type) 'defmacro
;; (macro-function name) new-defn)))
(defun execute-macro (m params args env)
(let ((*trace-level* (1+ *trace-level*)))
(with-dtrace-printer-settings
(show-function-args args params))
(funcall m args env)))
(defun trace-macro (name)
(let* ((formal-arglist (fetch-arglist name))
(old-defn (macro-function name))
(new-defn
#'(lambda (macro-args env)
(display-function-entry name 'macro)
(let ((result (execute-macro old-defn formal-arglist macro-args env)))
(display-function-return name (list result) 'macro)
(values result)))) )
(setf (get name 'original-definition) old-defn
(get name 'traced-definition) new-defn
(get name 'traced-type) 'defmacro
(macro-function name) new-defn)))
;;;
;;; Does not handle &body as multiple "args"
;;; (defmacro blunk (&body body) `(and ,@body))
;;;
;;; (blunk t nil nil t)
;;; ----Enter BLUNK macro
;;; | &BODY = BLUNK
;;; | BODY = T
;;; | Arg-3 = NIL
;;; | Arg-4 = NIL
;;; | Arg-5 = T
;;; \--BLUNK expanded to (AND T NIL NIL T)
(defun show-function-args (actuals formals &optional (argcount 0))
(cond ((null actuals) nil)
((null formals) (handle-args-numerically actuals argcount))
(t (case (first formals)
(&optional (show-function-args actuals (rest formals) argcount))
(&rest (show-function-args (list actuals) (rest formals) argcount))
(&key (handle-keyword-args actuals))
(&aux (show-function-args actuals nil argcount))
(t (handle-one-arg (first actuals) (first formals))
(show-function-args (rest actuals)
(rest formals)
(1+ argcount)))))))
;;;
;;; Not able to retrieve parameter names (formal args) in this implementation.
;;; Display as positional args.
;;;
(defun handle-args-numerically (actuals argcount)
(dolist (val actuals)
(incf argcount)
(display-arg-numeric val argcount)))
(defun handle-one-arg (val varspec)
(cond ((atom varspec) (display-one-arg val varspec))
(t (display-one-arg val (first varspec))
(if (third varspec)
(display-one-arg t (third varspec))))))
(defun handle-keyword-args (actuals)
(cond ((null actuals))
((keywordp (first actuals))
(display-one-arg (second actuals) (first actuals))
(handle-keyword-args (rest (rest actuals))))
(t (display-one-arg actuals "Extra args:"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; DUNTRACE and subordinate routines.
(defmacro duntrace (&rest function-names)
"Turns off tracing for specified functions.
With no args, turns off all tracing."
(setf *trace-level* 0) ;; safety precaution
(list 'quote
(mapcan #'duntrace1 (or function-names *traced-functions*))))
(defun duntrace1 (name)
(unless (symbolp name)
(format *error-output* "~&~S is an invalid function name." name)
(return-from duntrace1 nil))
(setf *traced-functions* (delete name *traced-functions*))
(let ((orig-defn (get name 'original-definition 'none))
(traced-defn (get name 'traced-definition))
(traced-type (get name 'traced-type 'none)))
(unless (or (eq orig-defn 'none)
(not (fboundp name))
(not (equal traced-defn ;; did it get redefined?
(ecase traced-type
(defun (symbol-function name))
(defmacro (macro-function name))))))
(ecase traced-type
(defun (setf (symbol-function name) orig-defn))
(defmacro (setf (macro-function name) orig-defn)))))
(remprop name 'traced-definition)
(remprop name 'traced-type)
(remprop name 'original-definition)
(list name))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Display routines.
;;;
;;; The code below generates vanilla character output for ordinary
;;; displays. It can be replaced with special graphics code if the
;;; implementation permits, e.g., on a PC you can use the IBM graphic
;;; character set to draw nicer-looking arrows. On a color PC you
;;; can use different colors for arrows, for function names, for
;;; argument values, and so on.
(defparameter *entry-arrow-string* "----")
(defparameter *vertical-string* "| ")
(defparameter *exit-arrow-string* " \\--")
(defparameter *trace-wraparound* 15)
(defun display-function-entry (name &optional ftype)
(space-over)
(draw-entry-arrow)
(format *trace-output* "Enter ~S" name)
(if (eq ftype 'macro)
(format *trace-output* " macro")))
(defun display-one-arg (val name)
(space-over)
(format *trace-output*
(typecase name
(keyword " ~S ~S")
(string " ~A ~S")
(t " ~S = ~S"))
name val))
(defun display-arg-numeric (val num)
(space-over)
(format *trace-output* " Arg-~D = ~S" num val))
(defun display-function-return (name results &optional ftype)
(with-dtrace-printer-settings
(space-over)
(draw-exit-arrow)
(format *trace-output* "~S ~A"
name
(if (eq ftype 'macro) "expanded to" "returned"))
(cond ((null results))
((null (rest results)) (format *trace-output* " ~S" (first results)))
(t (format *trace-output* " values ~{~S, ~}~s"
(butlast results)
(car (last results)))))))
;;;
;;; Show indentation of nested traced functions.
;;;
(defun space-over ()
(format *trace-output* "~&")
(dotimes (i (mod *trace-level* *trace-wraparound*))
(format *trace-output* "~A" *vertical-string*)))
(defun draw-entry-arrow ()
(format *trace-output* "~A" *entry-arrow-string*))
(defun draw-exit-arrow ()
(format *trace-output* "~A" *exit-arrow-string*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; The function FETCH-ARGLIST is implementation dependent. It
;;; returns the formal argument list of a function as it would
;;; appear in a DEFUN or lambda expression, including any lambda
;;; list keywords. Here are versions of FETCH-ARGLIST for three
;;; Lisp implementations.
;;; Minimal generic version
(defun fetch-arglist (fn)
(declare (ignore fn))
nil)
;;; Lucid version
#+LUCID
(defun fetch-arglist (fn)
(system::arglist fn))
;;; GCLisp 1.1 version
#+GCLISP
(defun fetch-arglist (fn)
(if (macro-function fn)
'(&rest "Form =")
(lambda-list fn)))
;;; KCL version
#+KCL
(defun fetch-arglist (fn)
(let ((x (symbol-function fn)))
(cond ((atom x) nil)
((eq (first x) 'macro) (list '&rest "Form ="))
(t (third x)))))
;;; CMU Common Lisp version. This version looks in a symbol's
;;; function cell and knows how to take apart lexical closures
;;; and compiled code objects found there.
#+CMU
(defun fetch-arglist (x &optional original-x) ; Broken for macros...
(cond ((symbolp x) (fetch-arglist (symbol-function x) x))
((compiled-function-p x) (eval:interpreted-function-arglist x))
(t (cerror (format nil "Use a reasonable default argument list for ~S" original-x)
"Unkown object in function cell of ~S: ~S" original-x x)
'())))
;; (defun fetch-arglist (x &optional original-x)
;; (cond ((symbolp x) (fetch-arglist (symbol-function x) x))
;; ((compiled-function-p x)
;; (read-from-string
;; (lisp::%primitive header-ref x
;; lisp::%function-arg-names-slot)))
;; ((listp x) (case (first x)
;; (lambda (second x))
;; (lisp::%lexical-closure% (fetch-arglist (second x)))
;; (system:macro '(&rest "Form ="))
;; (t '(&rest "Arglist:"))))
;; (t (cerror (format nil "Use a reasonable default argument list for ~S" original-x)
;; "Unkown object in function cell of ~S: ~S" original-x x)
;; '())))
#+sbcl
(require :sb-introspect)
#+sbcl
(defun fetch-arglist (fn)
(sb-introspect:function-lambda-list fn))
#+clisp
(defun fetch-arglist (fn)
(ext:arglist fn))