From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Lisp-friendly backtraces [was: Lispy backtraces] Date: Mon, 05 Dec 2016 18:23:39 +0200 Message-ID: <83twaijqes.fsf@gnu.org> References: <20160922231447.GA3833@odonien.localdomain> <98fbb582-3da4-bd83-a2e9-e341dd7f6140@gmail.com> <20160923075116.GA612@odonien.localdomain> <82e39377-f31b-698c-5a9a-343868686799@gmail.com> <20161202005226.GA4215@odonien.localdomain> <0a69afa7-e9e6-e75f-8e90-6438683db98d@gmail.com> <53ba4534-1ec3-4e4f-d929-1a72f79c1abe@gmail.com> <83mvgblniy.fsf@gnu.org> <143c480c-a9db-7053-4b70-175633197981@gmail.com> <83a8cbl94w.fsf@gnu.org> <233f14a0-7542-5d0c-d8da-209e7e5e54f7@gmail.com> <838trvkq72.fsf@gnu.org> <27574c57-658c-c87b-4ddc-b24f83e2867c@gmail.com> <81066f70-ceed-af88-43ce-c8baefde189a@gmail.com> Reply-To: Eli Zaretskii NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1480955022 24940 195.159.176.226 (5 Dec 2016 16:23:42 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 5 Dec 2016 16:23:42 +0000 (UTC) Cc: emacs-devel@gnu.org To: =?windows-1252?Q?Cl=E9ment?= Pit--Claudel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Dec 05 17:23:35 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cDw39-0005Ch-QI for ged-emacs-devel@m.gmane.org; Mon, 05 Dec 2016 17:23:32 +0100 Original-Received: from localhost ([::1]:42916 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cDw3C-0004NA-93 for ged-emacs-devel@m.gmane.org; Mon, 05 Dec 2016 11:23:34 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cDw33-0004Lc-N8 for emacs-devel@gnu.org; Mon, 05 Dec 2016 11:23:26 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cDw30-0006am-Ii for emacs-devel@gnu.org; Mon, 05 Dec 2016 11:23:25 -0500 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:46951) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cDw30-0006ag-Ex; Mon, 05 Dec 2016 11:23:22 -0500 Original-Received: from 84.94.185.246.cable.012.net.il ([84.94.185.246]:3296 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1cDw2z-0001bK-Lx; Mon, 05 Dec 2016 11:23:22 -0500 In-reply-to: <81066f70-ceed-af88-43ce-c8baefde189a@gmail.com> (message from =?windows-1252?Q?Cl=E9ment?= Pit--Claudel on Mon, 5 Dec 2016 09:14:38 -0500) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:210066 Archived-At: > From: Clément Pit--Claudel > Date: Mon, 5 Dec 2016 09:14:38 -0500 > > On 2016-12-05 08:20, Stefan Monnier wrote: > >> (defun backtrace () > >> "Print a trace of Lisp function calls currently active. > >> Output stream used is value of `standard-output'." > >> (mapbacktrace #'~/backtrace-1 1)) > > > > Have you tried it both byte-compiled and interpreted? Maybe this > > function is just simple enough that the result is the same in both > > cases, but in my experience, the stack is sufficiently different in the > > two cases that a constant nskip doesn't cut it (hence the use of `base` > > in backtrace-frame). > > Thanks; I attached an updated patch. Removing `backtrace' from eval.c makes the patch much harder to read, so I'll do that later. Thanks, allow me a few additional comments: > ;; -*- lexical-binding: t -*- > > (defun backtrace-1 (evald func args flags) > "Print a trace of a single stack frame to `standard-output'. > EVALD, FUNC, ARGS, FLAGS are as in `mapbacktrace'." > (let ((print-level (or print-level 8))) > (princ (if (plist-get flags :debug-on-exit) "* " " ")) > (cond > ((and evald (not debugger-stack-frame-as-list)) > (prin1 func) > (if args (prin1 args) (princ "()"))) > (t > (prin1 (cons func args)))) > (princ "\n"))) > > (defun backtrace () > "Print a trace of Lisp function calls currently active. > Output stream used is value of `standard-output'." > (mapbacktrace #'backtrace-1 'backtrace)) > > (backtrace) > > (defun backtrace-frames () > "Collect all frames of current backtrace into a list." > (let ((frames nil)) > (mapbacktrace (lambda (&rest frame) (push frame frames)) 'backtrace-frames) > (nreverse frames))) > > (backtrace-frames) > > (defun ~/backtrace-frame (nframes &optional base) > "Return the function and arguments NFRAMES up from current execution point. > If that frame has not evaluated the arguments yet (or is a special form), > the value is (nil FUNCTION ARG-FORMS...). > If that frame has evaluated its arguments and called its function already, > the value is (t FUNCTION ARG-VALUES...). > A &rest arg is represented as the tail of the list ARG-VALUES. > FUNCTION is whatever was supplied as car of evaluated list, > or a lambda expression for macro calls. > If NFRAMES is more than the number of frames, the value is nil. > If BASE is non-nil, it should be a function and NFRAMES counts from its > nearest activation frame." It is better to move the description of BASE to the 2nd line, as it's an argument of this function, while the rest describes the details of what the function does. It is plausible that someone would like to read the doc string just as a reminder of the API, so we had better not force them to read the entire doc string. > (let ((frame nil)) > (mapbacktrace (lambda (evald func args _) > (when (and base (eq func base)) > (setq base nil)) > (unless base > (when (eq nframes 0) > (setq frame `(,evald ,func ,@args))) > (setq nframes (1- nframes)))) > '~/backtrace-frame) > frame)) These functions should go to subr.el, I think. > +DEFUN ("mapbacktrace", Fmapbacktrace, Smapbacktrace, 1, 2, 0, > + doc: /* Call FUNCTION for each frame in backtrace. Likewise here: BASE is better described on the 2nd line. > +FUNCTION is called with 4 arguments EVALD FUNC ARGS FLAGS. If a frame ^ Colon ':' here, please. > + if (backtrace_debug_on_exit (pdl)) > + { > + flags = Fcons (QCdebug_on_exit, Fcons (Qt, Qnil)); > + } No need for braces if there's only one statement in the 'if' clause. > + if (backtrace_nargs (pdl) == UNEVALLED) > + { > + call4 (function, Qnil, backtrace_function (pdl), *backtrace_args (pdl), flags); > + } Same here. Last, but not least: it would be nice to have a couple of tests for this functionality. Thanks again for working on this.