当使用repl时,调试Clojure代码的最佳方法是什么?
当前回答
Emacs的CIDER有一个源调试器,您可以在Emacs缓冲区中逐步执行表达式,甚至可以注入新值。你可以在这里读到所有的信息。演示截图:
其他回答
def-let的函数版本,它将一个let转换为一系列defs。有些功劳要归功于这里
(defn def-let [aVec]
(if-not (even? (count aVec))
aVec
(let [aKey (atom "")
counter (atom 0)]
(doseq [item aVec]
(if (even? @counter)
(reset! aKey item)
(intern *ns* (symbol @aKey) (eval item)))
; (prn item)
(swap! counter inc)))))
用法:需要用引文来引用内容。
(def-let '[a 1 b 2 c (atom 0)])
我最喜欢的方法是在整个代码中自由地散布printlns。多亏了#_ reader宏,打开和关闭它们很容易(它使阅读器以下面的形式阅读,然后假装从未见过它)。或者你可以使用宏扩展为传入体或nil,这取决于一些特殊变量的值,例如*debug*:
(defmacro debug-do [& body]
(when *debug*
`(do ~@body)))
这里有一个(def *debug* false),它将扩展为nil。如果是真的,它就会扩张到裹着do的身体。
对这个SO问题的公认答案:用于进度报告的Idiomatic Clojure ?在调试序列操作时非常有用。
Then there's something which is currently incompatible with swank-clojure's REPL, but is too good not to mention: debug-repl. You can use it in a standalone REPL, which is easy to get e.g. with Leiningen (lein repl); and if you're launching your programme from the command line, then it's going to bring its own REPL up right in your terminal. The idea is that you can drop the debug-repl macro in anywhere you like and have it bring up its own REPL when the programme's execution reaches that point, with all locals in scope etc. A couple of relevant links: The Clojure debug-repl, Clojure debug-repl tricks, how 'bout a debug-repl (on the Clojure Google group), debug-repl on Clojars.
swank-clojure在使SLIME的内置调试器在处理Clojure代码时发挥了足够的作用——注意stacktrace中不相关的部分是如何被灰色化的,因此很容易找到正在调试的代码中的实际问题。需要记住的一件事是,没有“名称标签”的匿名函数出现在堆栈跟踪中,基本上没有附加任何有用的信息;当添加“name标签”时,它确实会出现在stacktrace中,并且一切正常:
(fn [& args] ...)
vs.
(fn tag [& args] ...)
example stacktrace entries:
1: user$eval__3130$fn__3131.invoke(NO_SOURCE_FILE:1)
vs. ^^
1: user$eval__3138$tag__3139.invoke(NO_SOURCE_FILE:1)
^^^
Emacs的CIDER有一个源调试器,您可以在Emacs缓冲区中逐步执行表达式,甚至可以注入新值。你可以在这里读到所有的信息。演示截图:
如果你使用emacs/slime/swank,那么在REPL试试这个:
(defn factorial [n]
(cond (< n 2) n
(= n 23) (swank.core/break)
:else (* n (factorial (dec n)))))
(factorial 30)
它不像在LISP下那样为您提供完整的堆栈跟踪,但它很适合进行探查 周围。
这是优秀的工作:
http://hugoduncan.org/post/2010/swank_clojure_gets_a_break_with_the_local_environment.xhtml
正如上面的评论所提到的。
从2016年开始,你可以使用Debux,这是Clojure/Script的一个简单调试库,可以与repl以及浏览器的控制台一起工作。您可以在代码中添加dbg (debug)或clog (console.log)宏,并轻松观察各个函数的结果,等等,打印到您的REPL和/或控制台。
来自项目的Readme:
Basic usage This is a simple example. The macro dbg prints an original form and pretty-prints the evaluated value on the REPL window. Then it returns the value without interfering with the code execution. If you wrap the code with dbg like this, (* 2 (dbg (+ 10 20))) ; => 60 the following will be printed in the REPL window. REPL output: dbg: (+ 10 20) => 30 Nested dbg The dbg macro can be nested. (dbg (* 2 (dbg (+ 10 20)))) ; => 60 REPL output: `dbg: (+ 10 20) => 30` dbg: (* 2 (dbg (+ 10 20))) => 60
推荐文章
- 确保您的项目构建设置正在生成一个dSYM文件。对于所有配置,DEBUG_INFORMATION_FORMAT都应该设置为dwarf-with-dsym
- 如何获得GDB中所有线程的回溯?
- 如何检测IE11?
- 如何通过参数和重定向stdin从一个文件到程序运行在gdb?
- 如何在Clojure中创建web应用程序?
- 我如何调试git/git-shell相关的问题?
- Visual Studio拒绝忘记断点?
- 如何在Python自己的调试器(PDB)中执行多行语句
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- Linux有c++ gdb图形用户界面吗?
- 是什么让Visual Studio调试器停止评估ToString重写?
- 打印一个可变内存地址在swift
- 访问控制台和扩展的后台开发工具
- Python内存泄漏
- 使用Visual Studio调试器在值更改时中断