Emacs和Emacs Lisp的故障排除技术

时间:2010-01-18 16:31:16

标签: emacs elisp

我已经是一个相当规律的emacs用户大约4年了,但在定制emacs和故障排除elisp方面,我仍然是新手。最近,我开始将emacs定制为我的ruby开发环境,我从StackOverflow中的人员那里学到了一些技巧。 例如,有人在这里告诉我有关C-u C-M-x用edebug检测功能然后逐步执行代码。我还发现,emacs中的大多数命令和模式都提供了大量的钩子(函数或正则表达式或可自定义的变量),这些钩子将提供新手所需的大部分内容。
现在我很贪心 - 我正在寻找你曾经使用并且过去发现有用的更多技巧和技巧。

6 个答案:

答案 0 :(得分:13)

 (setq debug-on-error t)
 (setq debug-on-quit t)

当您想要调试任意深度问题时,这些帮助。您已经发现了edebug(这是我找出其他人代码的首选工具)。 describe-function通常会为您提供指向.el文件的链接(以及行号)。这对于跳转到问题的根源非常有用。我经常这样做,复制该函数,输入一些message个调用并重新评估C-x C-e以使其运行而不是原始运行。

更新:这是我从presentation by John Wiegley获取的一些内容。

(global-set-key (kbd "C-c C-d")
        (lambda () (interactive)
          (setq debug-on-error (if debug-on-error nil t))
          (message (format "debug-on-error : %s" debug-on-error))))

让你用一次击键切换debug-on-error

答案 1 :(得分:6)

C-x Esc Esc为您提供了已运行的M-x命令的可浏览历史记录,但显示了elisp代码。

IELM是emacs lisp的代表。

Speedbar是浏览.el文件的绝佳方式,我发现自己经常使用C-h i(浏览elisp手册)并使用speedbar浏览主题节点树。

信息浏览器中的C-s / C-r增量搜索实际上将搜索过去的分页符。

我经常运行M-:测试快速的代码,而不必切换到我的* ielm *缓冲区。

对于特别棘手的代码,我在桌面上创建一个快捷方式来运行emacs -q -l development-init.el (这对于处理缓冲区和外部进程的低级操作的代码尤其方便,可以轻松挂起emacs或使用segv杀死它的那种东西)。

答案 2 :(得分:2)

如果攻击您的.emacs文件,请始终保持emacs进程运行,并通过启动第二个Emacs进程来测试更改(使用--debug-init)。这样,如果出现问题,您仍然可以使用编辑器。

答案 3 :(得分:1)

就我而言,我更喜欢(推荐)debug而不是edebug,但这可能只是一个品味问题。

除了Noufal提到的debug-on-*变量之外,您还可以通过M-x debug-on-entry输入给定函数的调试器。

您可以在代码中的断点处明确调用debug(debug)(debug nil sexp-to-print)

答案 4 :(得分:1)

我的init文件评论调试工具:

;;;; * Debugging, Tracing, and Profiling

;; M-: (info "(elisp) Debugging") RET

;; Standard debugger:
;; M-x debug-on-entry FUNCTION
;; M-x cancel-debug-on-entry &optional FUNCTION
;; debug &rest DEBUGGER-ARGS
;; M-x toggle-debug-on-error
;; M-x toggle-debug-on-quit
;; setq debug-on-signal
;; setq debug-on-next-call
;; setq debug-on-event
;; setq debug-on-message REGEXP

;; Edebug -- a source-level debugger for Emacs Lisp
;; M-x edebug-defun (C-u C-M-x) Cancel with eval-defun (C-M-x)
;; M-x edebug-all-defs -- Toggle edebugging of all definitions
;; M-x edebug-all-forms -- Toggle edebugging of all forms
;; M-x edebug-eval-top-level-form

;; Tracing:
;; M-x trace-function FUNCTION &optional BUFFER
;; M-x untrace-function FUNCTION
;; M-x untrace-all

;; Timing and benchmarking:
;; (benchmark-run &optional REPETITIONS &rest FORMS)

;; Emacs Lisp Profiler (ELP)
;; M-x elp-instrument-package
;; M-x elp-instrument-list
;; M-x elp-instrument-function
;; M-x elp-reset-*
;; M-x elp-results
;; M-x elp-restore-all
;;
;; "There's a built-in profiler called ELP. You can try something like
;; M-x elp-instrument-package, enter "vc", and then try finding a file
;; Afterwards, M-x elp-results will show you a profile report.
;; (Note that if the time is instead being spent in non-vc-related
;; functions, this technique will not show it, but you can instrument
;; further packages if you like.)" http://stackoverflow.com/a/6732810/324105

;; CPU & Memory Profiler ('Native Profiler')
;; M-x profiler-start
;; M-x profiler-report
;; M-x profiler-reset
;; M-x profiler-stop
;; M-x profiler-*

;; Dope ("DOtemacs ProfilEr. A per-sexp-evaltime profiler.")
;; https://raw.github.com/emacsmirror/dope/master/dope.el
;; M-x dope-quick-start will show a little introduction tutorial.

;; Spinning:
;; Set debug-on-quit to t
;; When the problem happens, hit C-g for a backtrace.

答案 5 :(得分:0)

欢迎来到启蒙的前几步。 ;)

首先,一些简单的快速点击:

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)

这会给你一些关于迷你缓冲区的提示。它非常方便!这个提示不是关于故障排除,而是关于使其易于编写的更多信息,但仍然如此。

从MELPA获取erefactor包,并查看它的功能。我注意到在对函数执行C-x C-e之后,它将通过elint运行结果。节省了很多麻烦。

来自emacs入门套件的这个钩子真棒。它会删除任何无效的.elc文件。如果你不小心,老elc文件可能是你身边真正的刺。反过来看一下自动编译。

(add-hook 'emacs-lisp-mode-hook 'starter-kit-remove-elc-on-save)

(defun starter-kit-remove-elc-on-save ()
  "If you're saving an elisp file, likely the .elc is no longer valid."
  (make-local-variable 'after-save-hook)
  (add-hook 'after-save-hook
            (lambda ()
              (if (file-exists-p (concat buffer-file-name "c"))
                  (delete-file (concat buffer-file-name "c"))))))

最后,在编辑lisp,emacs-lisp或scheme时,请确保尝试使用paredit。太棒了。