all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Clément Pit--Claudel" <clement.pit@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Alan Mackenzie <acm@muc.de>, emacs-devel@gnu.org
Subject: Lisp-friendly backtraces [was: Lispy backtraces]
Date: Mon, 5 Dec 2016 01:02:23 -0500	[thread overview]
Message-ID: <27574c57-658c-c87b-4ddc-b24f83e2867c@gmail.com> (raw)
In-Reply-To: <838trvkq72.fsf@gnu.org>


[-- Attachment #1.1.1: Type: text/plain, Size: 1035 bytes --]

On 2016-12-04 22:30, Eli Zaretskii wrote:
>>> So would it perhaps make sense to rename 'backtrace' into something
>>> like 'backtrace--internal', and make it accept one more argument, the
>>> function to apply to each frame, which is now hard-coded as 'prin1'?
>>> Would that allow you to implement 'backtrace' in Lisp and also
>>> implement whatever application you had in mind, by calling
>>> 'backtrace--internal' passing it your own function instead of 'prin1'?
>>
>> Quite possibly!
>
> Will you be working on this idea at some point?

Yup.  I've attached a rough patch, and an ELisp file showing what calling the new function (mapbacktrace) looks like.

Let me know if this direction makes sense.  If so, I will write the corresponding documentation, a Changelog entry, and a proper commit message.

(CC Alan, since you recently expressed frustration with the contents of the *Backtrace* buffer; hopefully this new function will help with implementing a more flexible *Backtrace* buffer?)

Cheers,
Clément.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.1.2: 0001-New-function-mapbacktrace.patch --]
[-- Type: text/x-diff; name="0001-New-function-mapbacktrace.patch", Size: 2753 bytes --]

From 861c841eb242c474c66421ce4d9964940033ff31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit--Claudel?= <clement.pitclaudel@live.com>
Date: Mon, 5 Dec 2016 00:52:14 -0500
Subject: [PATCH] New function mapbacktrace

---
 src/eval.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/src/eval.c b/src/eval.c
index 724f001..dcda51c 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -3420,6 +3420,53 @@ The debugger is entered when that frame exits, if the flag is non-nil.  */)
   return flag;
 }
 
+DEFUN ("mapbacktrace", Fmapbacktrace, Smapbacktrace, 1, 2, 0,
+       doc: /* Call FUNCTION for each frame in backtrace.
+FUNCTION is called with 4 arguments EVALD FUNC ARGS FLAGS.  If a frame
+has not evaluated its arguments yet or is a special form, EVALD is nil
+and ARGS is a list of forms.  If a frame has evaluated its arguments
+and called its function already, EVALD is t and ARGS is a list of
+values.  FLAGS is a plist of properties of the current frame:
+currently, the only supported property is :debug-on-exit.
+If NSKIP is non-nil, the top NSKIP frames are skipped.
+`mapbacktrace' always returns nil.  */)
+     (Lisp_Object function, Lisp_Object nskip)
+{
+  union specbinding *pdl = backtrace_top ();
+
+  if (!NILP (nskip))
+    {
+      CHECK_NUMBER(nskip);
+      EMACS_INT to_skip = XINT(nskip);
+      while (to_skip > 0 && backtrace_p (pdl)) {
+        to_skip--;
+        pdl = backtrace_next (pdl);
+      }
+    }
+
+  while (backtrace_p (pdl))
+    {
+      Lisp_Object flags = Qnil;
+      if (backtrace_debug_on_exit (pdl))
+        {
+          flags = Fcons (QCdebug_on_exit, Fcons (Qt, Qnil));
+        }
+
+      if (backtrace_nargs (pdl) == UNEVALLED)
+        {
+          call4 (function, Qnil, backtrace_function (pdl), *backtrace_args (pdl), flags);
+        }
+      else
+        {
+          Lisp_Object tem = Flist (backtrace_nargs (pdl), backtrace_args (pdl));
+          call4 (function, Qt, backtrace_function (pdl), tem, flags);
+        }
+      pdl = backtrace_next (pdl);
+    }
+
+  return Qnil;
+}
+
 DEFUN ("backtrace", Fbacktrace, Sbacktrace, 0, 0, "",
        doc: /* Print a trace of Lisp function calls currently active.
 Output stream used is value of `standard-output'.  */)
@@ -3973,7 +4020,8 @@ alist of active lexical bindings.  */);
   defsubr (&Srun_hook_wrapped);
   defsubr (&Sfetch_bytecode);
   defsubr (&Sbacktrace_debug);
-  defsubr (&Sbacktrace);
+  DEFSYM (QCdebug_on_exit, ":debug-on-exit");
+  defsubr (&Smapbacktrace);
   defsubr (&Sbacktrace_frame);
   defsubr (&Sbacktrace_eval);
   defsubr (&Sbacktrace__locals);
-- 
2.7.4


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.1.3: bt.el --]
[-- Type: text/x-emacs-lisp; name="bt.el", Size: 1931 bytes --]

;; -*- 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 1))

(defun backtrace-frames ()
  "Collect all frames of current backtrace into a list."
  (let ((frames nil))
    (mapbacktrace (lambda (&rest frame) (push frame frames)) 2)
    (nreverse 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."
  (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)))))
    frame))

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2016-12-05  6:02 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-22 23:14 bug#24514: 24.5; [WIP][PATCH] Lispy backtraces Vasilij Schneidermann
2016-09-23  2:22 ` Clément Pit--Claudel
2016-09-23  7:51   ` Vasilij Schneidermann
2016-09-23 13:22     ` Clément Pit--Claudel
     [not found]     ` <82e39377-f31b-698c-5a9a-343868686799@gmail.com>
     [not found]       ` <20161202005226.GA4215@odonien.localdomain>
2016-12-02  1:23         ` bug#24514: 24.5; " Clément Pit--Claudel
2016-12-02  2:24           ` Stefan Monnier
2016-12-03 22:15             ` Clément Pit--Claudel
2016-12-04 15:30               ` Eli Zaretskii
2016-12-04 19:27                 ` Clément Pit--Claudel
2016-12-04 20:41                   ` Eli Zaretskii
2016-12-04 22:14                     ` Clément Pit--Claudel
2016-12-05  3:30                       ` Eli Zaretskii
2016-12-05  6:02                         ` Clément Pit--Claudel [this message]
2016-12-05 13:20                           ` Lisp-friendly backtraces [was: Lispy backtraces] Stefan Monnier
2016-12-05 14:14                             ` Clément Pit--Claudel
2016-12-05 14:37                               ` Stefan Monnier
2016-12-05 16:31                                 ` Clément Pit--Claudel
2016-12-05 16:54                                   ` Eli Zaretskii
2016-12-05 16:23                               ` Eli Zaretskii
2016-12-05 18:59                                 ` Clément Pit--Claudel
2016-12-06 18:55                                   ` Eli Zaretskii
2016-12-07  8:27                                     ` Clément Pit--Claudel
2016-12-12 22:42                                       ` Clément Pit--Claudel
2016-09-23  8:12   ` bug#24514: 24.5; [WIP][PATCH] Lispy backtraces Vasilij Schneidermann
2016-09-23  9:44   ` Eli Zaretskii
2016-09-23  9:55     ` bug#24515: " Vasilij Schneidermann
2016-09-23 10:06       ` Eli Zaretskii
2016-09-23 13:25       ` Clément Pit--Claudel
2016-09-23 16:33         ` John Wiegley
     [not found] ` <mailman.2864.1474586229.22741.bug-gnu-emacs@gnu.org>
2016-09-23 18:47   ` Alan Mackenzie
2016-09-23 20:43 ` Richard Stallman
2016-09-27 19:16 ` Vasilij Schneidermann
2016-09-28 15:28   ` Eli Zaretskii
2016-09-30 10:29     ` Vasilij Schneidermann
2016-09-30 13:26       ` Eli Zaretskii
2016-10-12 15:34   ` Vasilij Schneidermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=27574c57-658c-c87b-4ddc-b24f83e2867c@gmail.com \
    --to=clement.pit@gmail.com \
    --cc=acm@muc.de \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.