Skip to content

Commit

Permalink
added deeptrace for cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Domagala committed Nov 29, 2021
1 parent 769ecee commit eec09e6
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 30 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ or just through github source:

![Screenshot](docs/demo.gif)

# experimental deeptrace(clojure only for now)
# experimental deeptrace
```clojure
(require '[cyrik.omni-trace :as o])

;; uncomment to also trace clojure.core, tested with testing-ns only
;; (require '[cyrik.omni-trace.instrument :as i])
;; (reset! i/ns-blacklist [])
(o/run-traced 'cyrik.omni-trace.testing-ns/run-machine)

;; run this for cljs
;; (o/run-traced-cljs 'cyrik.omni-trace.testing-ns/run-machine)
(tap> (o/rooted-flamegraph 'cyrik.omni-trace.testing-ns/run-machine))
```

Expand All @@ -65,7 +72,9 @@ It then runs the function with any supplied args and untraces everything. This r

![Screenshot](docs/deep-trace.png)

Currently there are still a few problems with recursion and cljs needs to be implemented as well.
Currently there are still a few problems with recursion, will have to rewrite the deps graph for it.
I'm guessing a lot of code will stil explode when allowing clojure.core trace, since the tracing code itself uses those.
Will probably cleanup the blacklist and try to use local function copies for tracing.


## Features
Expand Down
95 changes: 95 additions & 0 deletions dev/testing.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion dev/user.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[portal.console :as console]
[cyrik.omni-trace.instrument :as i]
[test-console :as tc]
[cljs.analyzer :as ana]
[cyrik.omni-trace.graph :as flame]
[portal.web :as p]))

Expand Down Expand Up @@ -33,6 +34,7 @@
(o/instrument-ns 'cyrik.omni-trace.testing-ns)
;or instrument a single function
(o/instrument-fn 'user/test-inc)

(test-inc 5)
;run functions in that namespace
(-> e/machine-init
Expand Down Expand Up @@ -64,11 +66,74 @@
(alter-meta! (var cyrik.omni-trace.testing-ns/insert-coin) assoc-in [:stuffs] "yeah123")
(.log js/console (meta (var cyrik.omni-trace.testing-ns/insert-coin)))

(macro/cljs-macroexpand-all '(o/instrument-fn 'cljs.core/keep))
(reset! i/ns-blacklist [])
(o/run-traced-cljs "cyrik.omni-trace.testing-ns" "run-machine")
(macro/cljs-macroexpand-all '(o/run-traced-cljs "cyrik.omni-trace.testing-ns" "run-machine"))

(mapv #(first %) #{['cljs.core '+]})
(Math/round 1.5)
(when-let [open js/OpenFileInEditor] (open "some/file.cljs" 10 1))
(.log js/console (throw (js/Error. "Oops")))
((fn [] (/ 1 0)))
(try (test-throw-full 5) (catch :default e (.log js/console e)))
(try (test-throw-full 5) (catch :default e (doto (js->clj e) (.log js/console e))))
.)



;; getting multi arity to work
(o/instrument-fn 'cljs.core/+)
(let*
[temp__5753__auto__
(cyrik.omni-trace.instrument/instrumented
'cljs.core/+
#'cljs.core/+
"/Users/lukas/Workspace/clojure/omni-trace/src/cyrik/omni_trace/testing_ns.cljc"
#:cyrik.omni-trace{:workspace cyrik.omni-trace.instrument/workspace})]
(if
temp__5753__auto__
(do
(let*
[instrumented__134281__auto__ temp__5753__auto__
ayyyy (.assign js/Object (js/Object.) cljs.core/keep instrumented__134281__auto__)]
;; (.log js/console (.-__proto__ instrumented__134281__auto__))
;; (.log js/console instrumented__134281__auto__)
;; (.log js/console ayyyy)
(.log js/console (.assign js/Object instrumented__134281__auto__ cljs.core/+))
;; (.log js/console (set! (.-__proto__ (.assign js/Object (js/Object.) instrumented__134281__auto__ cljs.core/keep))
;; instrumented__134281__auto__))
;; (set! cljs.core/keep (.assign js/Object (js/Object.) instrumented__134281__auto__ cljs.core/keep))
;; (set! (.-__proto__ ayyyy) (.-__proto__ instrumented__134281__auto__))
;; (set! (.-prototype ayyyy) (.-prototype instrumented__134281__auto__))
(set! cljs.core/+ instrumented__134281__auto__)
'cljs.core/+))))
(let*
[temp__5753__auto__
(cyrik.omni-trace.instrument/instrumented
'cljs.core/+
#'cljs.core/+
"/Users/lukas/Workspace/clojure/omni-trace/src/cyrik/omni_trace/testing_ns.cljc"
#:cyrik.omni-trace{:workspace cyrik.omni-trace.instrument/workspace})]
(if
temp__5753__auto__
(do
(let*
[instrumented__134281__auto__ temp__5753__auto__
ayyyy (.assign js/Object (js/Object.) cljs.core/keep instrumented__134281__auto__)]
;; (.log js/console (.-__proto__ instrumented__134281__auto__))
;; (.log js/console instrumented__134281__auto__)
;; (.log js/console ayyyy)
(.log js/console (.assign js/Object instrumented__134281__auto__ cljs.core/+))
;; (.log js/console (set! (.-__proto__ (.assign js/Object (js/Object.) instrumented__134281__auto__ cljs.core/keep))
;; instrumented__134281__auto__))
;; (set! cljs.core/keep (.assign js/Object (js/Object.) instrumented__134281__auto__ cljs.core/keep))
;; (set! (.-__proto__ ayyyy) (.-__proto__ instrumented__134281__auto__))
;; (set! (.-prototype ayyyy) (.-prototype instrumented__134281__auto__))
(set! cljs.core/+ instrumented__134281__auto__)
'cljs.core/+))))
(defn arity ([] (set! alter-meta! 1)) ([a] (inc 1)))
(set! cljs.core/keep arity)
.
)


34 changes: 27 additions & 7 deletions src/cyrik/omni_trace.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
(:require
#?(:clj [cyrik.omni-trace.deep-trace :as deep])
[cyrik.omni-trace.instrument :as i]
[cyrik.omni-trace.graph :as flame])
[cyrik.omni-trace.graph :as flame]
[net.cgrand.macrovich :as macros])

#?(:cljs (:require-macros [net.cgrand.macrovich :as macros]
#?(:cljs (:require-macros
[cyrik.omni-trace :refer [instrument-fn uninstrument-fn instrument-ns uninstrument-ns]])))

(defmacro instrument-fn
Expand Down Expand Up @@ -63,20 +64,39 @@

#?(:clj
(defn run-traced [s & args]
(apply #'deep/run-traced (into [s] args))))
(apply #'deep/run-traced (into [s] args)))
)


(defmacro run-traced-cljs [ns f & args]
(let [ns (symbol (name ns))
f (symbol (name f))
dep-list (deep/transitive-deps (deep/deps (deep/analysis ["dev" "src"]) :cljs)
ns f)
sym-list (mapv #(symbol (name (first %)) (name (second %))) (filter first dep-list)) ;;fix nil namespaces
instrumenters (mapv (fn [sym] `#(cyrik.omni-trace.instrument.cljs/cljs-instrument-fn '~sym {:cyrik.omni-trace/workspace cyrik.omni-trace.instrument/workspace} cyrik.omni-trace.instrument/instrumented)) sym-list)
deinstrumenters (mapv (fn [sym] `#(cyrik.omni-trace.instrument.cljs/cljs-instrument-fn '~sym {:cyrik.omni-trace/workspace cyrik.omni-trace.instrument/workspace} cyrik.omni-trace.instrument/uninstrumented)) sym-list)
runner `#(~(symbol (name ns) (name f)))
merged (into [](concat instrumenters [runner] deinstrumenters))
]
`(doseq [f# ~merged
]
(f#))
)
)

(comment
(require '[portal.api :as p])
(def portal (p/open))
(add-tap #'p/submit)


(name (:ns (meta (resolve 'user/run-machine))))
(name 'user/run-machine)


(filter #(and (= (:lang %) :cljs) (= (:from %) 'cyrik.omni-trace.testing-ns))(:var-usages(deep/analysis ["dev" "src"])))





.)
.
)
Loading

0 comments on commit eec09e6

Please sign in to comment.