From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Lennart Borgman (gmail)" Newsgroups: gmane.emacs.devel Subject: Re: Feature request: A way to get a traceback to a buffer or a string Date: Tue, 11 Dec 2007 11:12:46 +0100 Message-ID: <475E629E.4040302@gmail.com> References: <475DE033.6060403@gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1197368327 7392 80.91.229.12 (11 Dec 2007 10:18:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 11 Dec 2007 10:18:47 +0000 (UTC) To: Emacs Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Dec 11 11:18:57 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J22CV-0001st-Ch for ged-emacs-devel@m.gmane.org; Tue, 11 Dec 2007 11:18:55 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J22CB-0003z2-UQ for ged-emacs-devel@m.gmane.org; Tue, 11 Dec 2007 05:18:35 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J226e-0006dt-Kp for emacs-devel@gnu.org; Tue, 11 Dec 2007 05:12:52 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J226c-0006aI-Hj for emacs-devel@gnu.org; Tue, 11 Dec 2007 05:12:51 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J226b-0006a4-MC for emacs-devel@gnu.org; Tue, 11 Dec 2007 05:12:49 -0500 Original-Received: from ch-smtp02.sth.basefarm.net ([80.76.149.213]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J226b-00027l-Jl for emacs-devel@gnu.org; Tue, 11 Dec 2007 05:12:49 -0500 Original-Received: from c83-254-148-228.bredband.comhem.se ([83.254.148.228]:60185 helo=[127.0.0.1]) by ch-smtp02.sth.basefarm.net with esmtp (Exim 4.68) (envelope-from ) id 1J226Y-00059E-8o for emacs-devel@gnu.org; Tue, 11 Dec 2007 11:12:47 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071031 Thunderbird/2.0.0.9 Mnenhy/0.7.5.666 In-Reply-To: <475DE033.6060403@gmail.com> X-Antivirus: avast! (VPS 071210-0, 2007-12-10), Outbound message X-Antivirus-Status: Clean X-Originating-IP: 83.254.148.228 X-Scan-Result: No virus found in message 1J226Y-00059E-8o. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1J226Y-00059E-8o f87b84a9bf7052bd6edf471c1e7bdb30 X-detected-kernel: by monty-python.gnu.org: Linux 2.6? (barebone, rare!) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:85037 Archived-At: Lennart Borgman (gmail) wrote: > In some situations you may know that a function call will cause an > error. You may then want to get a traceback from this particular call, > without user intervention. I have attached a macro that can give this. I > would find it practical if this were included in Emacs. > > (defmacro mumamo-get-backtrace (bodyform) > "Evaluate BODYFORM, return backtrace as a string. > If there is an error in BODYFORM then return the backtrace as a > string, otherwise return nil." > `(let ((debugger (lambda (&rest debugger-args) > (setq debugger-ret (with-output-to-string > (backtrace))))) > (debug-on-error t) > (debug-on-signal t) > (debugger-ret nil)) > (condition-case err > (progn > ,bodyform > nil) > (error > (let* ((errmsg (error-message-string err)) > (debugger-lines (split-string debugger-ret "\n")) > (dbg-ret (mapconcat 'identity (nthcdr 6 debugger-lines) > "\n"))) > (concat errmsg "\n" dbg-ret)))))) The code above does not give a backtrace always, but I think this will: (defmacro mumamo-get-backtrace-if-error (bodyform) "Evaluate BODYFORM, return a list with error message and backtrace. If there is an error in BODYFORM then return a list with the error message and the backtrace as a string. Otherwise return nil." `(let* ((debugger (lambda (&rest debugger-args) (let ((debugger-ret (with-output-to-string (backtrace)))) ;; I believe we must put the result in a buffer, ;; otherwise `condition-case' might erase it: (with-current-buffer (get-buffer-create "TEMP GET BACKTRACE") (erase-buffer) (insert debugger-ret))))) (debug-on-error t) (debug-on-signal t)) (condition-case err (progn ,bodyform nil) (error (let* ((errmsg (error-message-string err)) (dbg1-ret (with-current-buffer (get-buffer "TEMP GET BACKTRACE") (buffer-string))) ;; Remove lines from this routine: (debugger-lines (split-string dbg1-ret "\n")) (dbg-ret (mapconcat 'identity (nthcdr 6 debugger-lines) "\n")) ) (list errmsg (concat errmsg "\n" dbg-ret)))))))