summaryrefslogtreecommitdiffstats
path: root/stdlib/trace.tl
blob: 6d3bf6008782a7de6c3dd7ce1d67295fa1d1b972 (plain)
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
;; Copyright 2016-2024
;; Kaz Kylheku <kaz@kylheku.com>
;; Vancouver, Canada
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice,
;;    this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice,
;;    this list of conditions and the following disclaimer in the documentation
;;    and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.

(defvar *trace-output* *stdout*)

(defvar sys:*trace-hash* (hash :equal-based))
(defvar sys:*trace-level* -1)

(defvarl sys:tr* (fun *))
(defvarl sys:trfm (fun format))

(defun sys:trace-enter (name args)
  [sys:trfm *trace-output* "~*a(~s ~s\n" [sys:tr* sys:*trace-level* 2] "" name args])

(defun sys:trace-leave (val)
  [sys:trfm *trace-output* "~*a  ~s)\n" [sys:tr* sys:*trace-level* 2] "" val])

(defun sys:trace-canonicalize-name (name)
  (if (and (consp name)
           (eq (car name) 'meth))
    (let* ((req-type-sym (cadr name))
           (slot-sym (caddr name))
           (req-type (find-struct-type req-type-sym))
           (s-s-p (if req-type
                    (static-slot-p req-type slot-sym)))
           (actual-type-sym (if s-s-p
                              (static-slot-home req-type-sym slot-sym))))
      (if (and s-s-p (neq req-type-sym actual-type-sym))
        ^(meth ,actual-type-sym ,slot-sym)
        name))
    name))

(defun sys:trace (names)
  (cond
    ((null names) (hash-keys sys:*trace-hash*))
    (t
      (each ((orig-n names)
             (n [mapcar sys:trace-canonicalize-name names]))
        (unless [sys:*trace-hash* n]
          (when (neq n orig-n)
            (usr:catch
              (throwf 'warning "~s: ~s is actually ~s: tracing that instead"
                      'trace orig-n n)
              (continue ())))
          (let* ((prev (or (symbol-function n)
                           (throwf 'eval-error
                                   "~s: ~s does not name a function" 'trace n)))
                 (lex-n n)
                 (hook (lambda (. args)
                         (let ((abandoned t)
                               (sys:*trace-level* (succ sys:*trace-level*)))
                           (unwind-protect
                             (progn
                               (sys:trace-enter lex-n args)
                               (let ((val (apply prev args)))
                                 (sys:trace-leave val)
                                 (set abandoned nil)
                                 val))
                             (if abandoned
                               (sys:trace-leave :abandoned)))))))
            (set (symbol-function n) hook
                 [sys:*trace-hash* n] prev)))))))

(defun sys:untrace (names)
  (flet ((disable (name-orig name)
           (let ((prev (del [sys:*trace-hash* name])))
             (when prev
               (when (neq name-orig name)
                 (usr:catch
                   (throwf 'warning "~s: ~s is actually ~s: untracing that instead"
                           'trace name-orig name)
                   (continue ())))
               (set (symbol-function name) prev)))))
    (if names
      (each ((n-orig names)
             (n [mapcar sys:trace-canonicalize-name names]))
        (disable n-orig n))
      (dohash (n #:v sys:*trace-hash*)
        (disable n n)))))

(defun sys:trace-redefine-check (orig-name)
  (let ((name (sys:trace-canonicalize-name orig-name)))
    (when [sys:*trace-hash* name]
      (usr:catch
        (cond
          ((neq name orig-name)
             (throwf 'warning "~!~s won't be traced, though it overrides\n\
                               ~s which is currently traced"
                      name orig-name))
          (t (sys:untrace (list name))
             (throwf 'warning "previously traced ~s is redefined and no\ \
                               longer traced"
                      name)))
        (continue ())))))

(defmacro usr:trace (. names)
  ^(sys:trace ',names))

(defmacro usr:untrace (. names)
  ^(sys:untrace ',names))