From 835486ccfaaf58476cbb0e097bed3b81adbcfebe Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Wed, 30 Aug 2017 08:12:18 -0400 Subject: [PATCH v1] New command to backtrace from assertion failure (Bug#28280) * lisp/emacs-lisp/cl-lib.el (cl-show-assert): New command. (cl-assert-last-backtrace): New variable. * lisp/emacs-lisp/cl-preloaded.el (cl--assertion-failed): Set it. TODO: keybinding? NEWS. --- lisp/emacs-lisp/cl-lib.el | 15 +++++++++++++++ lisp/emacs-lisp/cl-preloaded.el | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el index 243622a301..59bbdbca51 100644 --- a/lisp/emacs-lisp/cl-lib.el +++ b/lisp/emacs-lisp/cl-lib.el @@ -614,6 +614,21 @@ cl-pairlis ;;; Miscellaneous. +(defvar cl-assert-last-backtrace nil + "Holds a list of frames from the last assertion failure.") + +(declare-function debugger-insert-backtrace "debug" (backtrace doxrefs)) + +(defun cl-show-assert () + "Show backtrace from last assertion failure." + (interactive) + (unless cl-assert-last-backtrace + (user-error "There hasn't been an assertion failure yet")) + (require 'debug) + (with-current-buffer (get-buffer-create "*Assertion Failure*") + (debugger-insert-backtrace cl-assert-last-backtrace t) + (display-buffer (current-buffer)))) + (provide 'cl-lib) (unless (load "cl-loaddefs" 'noerror 'quiet) ;; When bootstrapping, cl-loaddefs hasn't been built yet! diff --git a/lisp/emacs-lisp/cl-preloaded.el b/lisp/emacs-lisp/cl-preloaded.el index ab6354de7c..d66a4b9726 100644 --- a/lisp/emacs-lisp/cl-preloaded.el +++ b/lisp/emacs-lisp/cl-preloaded.el @@ -41,13 +41,19 @@ ;; The `assert' macro from the cl package signals ;; `cl-assertion-failed' at runtime so always define it. -(define-error 'cl-assertion-failed (purecopy "Assertion failed")) +(define-error 'cl-assertion-failed (purecopy "Assertion failed (\\[cl-show-assert] for backtrace)")) + +(defvar cl-assert-last-backtrace) (defun cl--assertion-failed (form &optional string sargs args) (if debug-on-error (funcall debugger 'error `(cl-assertion-failed (,form ,string ,@sargs))) + (setq cl-assert-last-backtrace (backtrace-frames 'cl--assertion-failed)) + (autoload 'cl-show-assert "cl-lib" + "Show backtrace from last assertion failure." t) (if string - (apply #'error string (append sargs args)) + (apply #'error (concat string " (\\[cl-show-assert] for backtrace)") + (append sargs args)) (signal 'cl-assertion-failed `(,form ,@sargs))))) ;; When we load this (compiled) file during pre-loading, the cl--struct-class -- 2.14.1