/
debug_test.clj
167 lines (156 loc) · 6.58 KB
/
debug_test.clj
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
(ns swank-clj.jpda.debug-test
(:require
[swank-clj.logging :as logging]
[swank-clj.jpda.debug :as debug]
[swank-clj.jpda.disassemble :as disassemble]
[swank-clj.inspect :as inspect]
[swank-clj.jpda.jdi :as jdi]
[swank-clj.jpda.jdi-clj :as jdi-clj]
[swank-clj.jpda.jdi-test-handler :as jdi-test-handler] ;; after jpda.debug
[swank-clj.jpda.jdi-vm :as jdi-vm]
[clojure.string :as string])
(:import
com.sun.jdi.event.BreakpointEvent
com.sun.jdi.event.ExceptionEvent
com.sun.jdi.event.StepEvent
com.sun.jdi.request.ExceptionRequest
com.sun.jdi.event.VMStartEvent
com.sun.jdi.event.VMDeathEvent
com.sun.jdi.VirtualMachine
com.sun.jdi.ObjectReference
(com.sun.jdi
Value BooleanValue ByteValue CharValue DoubleValue FloatValue IntegerValue
LongValue ShortValue))
(:use clojure.test))
;; (logging/set-level :trace)
(deftest vm-swank-main-test
(is (re-matches
#"\(try \(clojure.core/require \(quote swank-clj.socket-server\)\) \(\(clojure.core/resolve \(quote swank-clj.socket-server/start\)\) \{:a 1\}\) \(catch java.lang.Exception e__\d+__auto__ \(clojure.core/println e__\d+__auto__\) \(.printStackTrace e__\d+__auto__\)\)\)"
(pr-str (#'debug/vm-swank-main {:a 1})))))
(deftest inspect-test
(let [context (debug/launch-vm
{:main `(do (ns ~'fred)
(def ~'x 1)
(def ~'y (atom 2))
(defn ~'f [x#]
(+ x# 2)
(deref ~'y)
(case x#
1 true
0 false)))})
context (assoc context :current-thread (:control-thread context))
vm (:vm context)]
(.resume (:vm context))
(try
(is (= "1" (inspect/value-as-string context (jdi/mirror-of vm 1))))
(is (= "true" (inspect/value-as-string context (jdi/mirror-of vm true))))
(is (= "\"2\"" (inspect/value-as-string
(assoc context :current-thread (:control-thread context))
(jdi-clj/remote-str context "2"))))
(let [v (jdi-clj/control-eval-to-value context `[1 "a" '~'b])]
(is (= "[1 \"a\" b]" (inspect/value-as-string context v))))
(let [[f method] (jdi-clj/clojure-fn-deref
context (:control-thread context)
jdi/invoke-single-threaded
"fred" "f" 1)]
(let [ops (debug/disassemble-method
(disassemble/constant-pool
(.. f (referenceType) (constantPool)))
method)]
;; (clojure.pprint/pprint ops)
(is (seq ops))))
(is (debug/disassemble-symbol
context (:control-thread context) "fred" "f"))
(finally
(jdi/shutdown context)))))
(defn eval-in-frame-test [context thread]
(testing "atomic eval"
(is (re-matches
#"#<Atom@[0-9a-f]+: \{:a 1\}>"
(debug/eval-string-in-frame nil context thread "a" 0)))
(is (= "{:m 2}"
(debug/eval-string-in-frame nil context thread "m" 0)))
(is (= "1" (debug/eval-string-in-frame nil context thread "i" 0)))
(is (= "1.0" (debug/eval-string-in-frame nil context thread "d" 0)))
(is (= "nil" (debug/eval-string-in-frame nil context thread "n" 0)))
(is (= "#'clojure.core/slurp"
(debug/eval-string-in-frame nil context thread "v" 0)))
(is (= "\"a string\""
(debug/eval-string-in-frame nil context thread "s" 0)))
(is (= "1"
(debug/eval-string-in-frame nil context thread "w-dash" 0))))
(testing "form eval"
(is (= "{:m 3}"
(debug/eval-string-in-frame
nil context thread
"(zipmap (keys m) (map inc (vals m)))" 0)))
(is (= "2"
(debug/eval-string-in-frame nil context thread "(inc i)" 0)))
(is (= "2.0"
(debug/eval-string-in-frame nil context thread "(inc d)" 0)))
(is (= "2"
(debug/eval-string-in-frame nil context thread "(inc w-dash)" 0)))))
(defn pprint-eval-in-frame-test [context thread]
(testing "atomic eval"
(is (re-matches
#"#<Atom@[0-9a-f]+: \{:a 1\}>\n"
(debug/pprint-eval-string-in-frame nil context thread "a" 0)))
(is (= "{:m 2}\n"
(debug/pprint-eval-string-in-frame nil context thread "m" 0)))
(is (= "1\n"
(debug/pprint-eval-string-in-frame nil context thread "i" 0)))
(is (= "1.0\n"
(debug/pprint-eval-string-in-frame nil context thread "d" 0)))
(is (= "nil\n"
(debug/pprint-eval-string-in-frame nil context thread "n" 0)))
(is (re-matches
#"#<Var@[0-9a-f]+: #<core\$slurp clojure.core\$slurp@[0-9a-f]+>>\n"
(debug/pprint-eval-string-in-frame nil context thread "v" 0)))
(is (= "\"a string\"\n"
(debug/pprint-eval-string-in-frame nil context thread "s" 0))))
(testing "form eval"
(is (= "{:m 3}\n"
(debug/pprint-eval-string-in-frame
nil context thread
"(zipmap (keys m) (map inc (vals m)))" 0)))
(is (= "2\n"
(debug/pprint-eval-string-in-frame nil context thread "(inc i)" 0)))
(is (= "2.0\n"
(debug/pprint-eval-string-in-frame
nil context thread "(inc d)" 0)))))
(let [test-finished (promise)]
(defn handler-for-frame-test [^ExceptionEvent event context]
(try
(logging/trace "handler-for-frame-test")
(let [thread (.thread event)]
(eval-in-frame-test context thread)
(pprint-eval-in-frame-test context thread))
(finally
(jdi/resume-event-threads event)
(deliver test-finished nil))))
(deftest frame-test
(let [context (debug/launch-vm {:main `(deref (promise))})
context (assoc context :current-thread (:control-thread context))
vm (:vm context)]
(debug/add-exception-event-request context)
(.resume (:vm context))
(try
(jdi-test-handler/add-one-shot-event-handler
handler-for-frame-test "frame-test")
(jdi-clj/remote-thread
context (:control-thread context) jdi/invoke-single-threaded
`(let [~'a (atom {:a 1})
~'m {:m 2}
~'i 1
~'d 1.0
~'n nil
~'v #'clojure.core/slurp ; arbitrary var
~'s "a string"
~'w-dash 1]
(throw (Exception. "go do handler-for-frame-test"))
;; prevent local clearing before the exception
[~'a ~'m ~'i ~'d ~'n ~'v ~'s])
{:name "frame-test"})
@test-finished
(finally
(jdi/shutdown context))))))