unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#27397: [PATCH] New commands for bulk tracing of elisp functions
@ 2017-06-16 13:32 Phil Sainty
  2017-06-16 14:58 ` Dmitry Gutov
                   ` (2 more replies)
  0 siblings, 3 replies; 40+ messages in thread
From: Phil Sainty @ 2017-06-16 13:32 UTC (permalink / raw)
  To: 27397

[-- Attachment #1: Type: text/plain, Size: 1476 bytes --]

trace.el currently only has commands for adding a trace to a single
function, and for untracing either a single function or ALL traced
functions.

This patch adds commands for tracing and untracing functions in bulk,
either by function name prefix (`trace-package' and `untrace-package')
or by regexp (`trace-regexp' and `untrace-regexp').

The use of the term "package" for the prefix-based commands was chosen
for consistency with `elp-instrument-package', from which this code is
partly derived, and from which I have also maintained a warning and
basic protection against attempts to trace too many functions at once
(which can indeed render Emacs unusable).  It it still possible to do
this, but I feel the checks which are currently in place are probably
sufficient to protect against accidents.

`trace-is-traceable-p' is using Stefan's suggestion in his comment on
my initial implementation at https://stackoverflow.com/a/44241058 and
I have changed the completing-read filter predicate in
`trace--read-args' to also use this (instead of `fboundp'), on the
assumption that this makes more sense.

(I note that the `trace-is-traced' function does not follow the usual
naming convention for predicates.  Should this be renamed to
`trace-is-traced-p' ?)

This patch also removes some outdated docstring comments for
`untrace-function' which were clearly remnants of the earlier
implementation using advice.el (whereas the current library is based
on nadvice.el).


-Phil

[-- Attachment #2: 0001-New-commands-for-bulk-tracing-of-elisp-functions.patch --]
[-- Type: text/x-patch, Size: 5559 bytes --]

From c7262056db33722bc84dd94fbadaa71ed8fbf5fc Mon Sep 17 00:00:00 2001
From: Phil Sainty <psainty@orcon.net.nz>
Date: Sun, 11 Jun 2017 17:29:53 +1200
Subject: [PATCH] New commands for bulk tracing of elisp functions

* lisp/emacs-lisp/trace.el (trace-package, untrace-package)
(trace-regexp, untrace-regexp, trace-is-traceable-p): New functions.
(trace--read-args): Use new trace-is-traceable-p predicate.
(trace-is-traced, untrace-function, untrace-all): Doc updates/fixes.
---
 lisp/emacs-lisp/trace.el | 98 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 92 insertions(+), 6 deletions(-)

diff --git a/lisp/emacs-lisp/trace.el b/lisp/emacs-lisp/trace.el
index 1c57d73..5dba151 100644
--- a/lisp/emacs-lisp/trace.el
+++ b/lisp/emacs-lisp/trace.el
@@ -257,7 +257,12 @@ trace-function-internal
                       (or context (lambda () "")))
    `((name . ,trace-advice-name) (depth . -100))))
 
+(defun trace-is-traceable-p (sym)
+  "Whether the given symbol is a traceable function."
+  (or (functionp sym) (macrop sym)))
+
 (defun trace-is-traced (function)
+  "Whether FUNCTION is currently traced."
   (advice-member-p trace-advice-name function))
 
 (defun trace--read-args (prompt)
@@ -274,7 +279,7 @@ trace--read-args
                                    default
                                    (if beg (substring prompt beg) ": "))
                                 prompt)
-                              obarray 'fboundp t nil nil
+                              obarray 'trace-is-traceable-p t nil nil
                               (if default (symbol-name default)))))
    (when current-prefix-arg
      (list
@@ -321,17 +326,98 @@ trace-function-background
 (defalias 'trace-function 'trace-function-foreground)
 
 (defun untrace-function (function)
-  "Untraces FUNCTION and possibly activates all remaining advice.
-Activation is performed with `ad-update', hence remaining advice will get
-activated only if the advice of FUNCTION is currently active.  If FUNCTION
-was not traced this is a noop."
+  "Remove trace from FUNCTION.  If FUNCTION was not traced this is a noop."
   (interactive
    (list (intern (completing-read "Untrace function: "
                                   obarray #'trace-is-traced t))))
   (advice-remove function trace-advice-name))
 
+;;;###autoload
+(defun trace-package (prefix)
+  "Trace all functions with names starting with PREFIX.
+For example, to trace all diff functions, do the following:
+
+\\[trace-package] RET diff- RET
+
+Background tracing is used.  Switch to the trace output buffer to
+view the results.
+
+See also `untrace-package'."
+  (interactive ;; derived from `elp-instrument-package'.
+   (list (completing-read "Prefix of package to trace: "
+                          obarray #'trace-is-traceable-p)))
+  (when (zerop (length prefix))
+    (error "Tracing all Emacs functions would render Emacs unusable"))
+  (mapc (lambda (name)
+          (trace-function-background (intern name)))
+        (all-completions prefix obarray #'trace-is-traceable-p))
+  (message
+   "Tracing to %s.  Use %s to untrace a package, or %s to remove all traces."
+   trace-buffer
+   (substitute-command-keys "\\[untrace-package]")
+   (substitute-command-keys "\\[untrace-all]")))
+
+(defun untrace-package (prefix)
+  "Remove traces from all functions with names starting with PREFIX.
+
+See also `trace-package'."
+  (interactive
+   (list (completing-read "Prefix of package to untrace: "
+                          obarray #'trace-is-traced)))
+  (if (and (zerop (length prefix))
+           (y-or-n-p "Remove all function traces?"))
+      (untrace-all)
+    (mapc (lambda (name)
+            (untrace-function (intern name)))
+          (all-completions prefix obarray #'trace-is-traceable-p))))
+
+;;;###autoload
+(defun trace-regexp (regexp)
+  "Trace all functions with names matching REGEXP.
+For example, to trace indentation-related functions, you could try:
+
+\\[trace-regexp] RET indent\\|offset RET
+
+Warning: Do not attempt to trace all functions.  Tracing too many
+functions at one time will render Emacs unusable.
+
+Background tracing is used.  Switch to the trace output buffer to
+view the results.
+
+See also `untrace-regexp'."
+  (interactive
+   (list (read-regexp "Regexp matching functions to trace: ")))
+  (when (member regexp '("" "." ".+" ".*"))
+    ;; Not comprehensive, but it catches the most likely attempts.
+    (error "Tracing all Emacs functions would render Emacs unusable"))
+  (mapatoms
+   (lambda (sym)
+     (and (trace-is-traceable-p sym)
+          (string-match-p regexp (symbol-name sym))
+          (trace-function-background sym))))
+  (message
+   "Tracing to %s.  Use %s to untrace by regexp, or %s to remove all traces."
+   trace-buffer
+   (substitute-command-keys "\\[untrace-regexp]")
+   (substitute-command-keys "\\[untrace-all]")))
+
+(defun untrace-regexp (regexp)
+  "Remove traces from all functions with names matching REGEXP.
+
+See also `trace-regexp'."
+  (interactive
+   (list (read-regexp "Regexp matching functions to untrace: ")))
+  (if (and (zerop (length regexp))
+           (y-or-n-p "Remove all function traces?"))
+      (untrace-all)
+    (mapatoms
+     (lambda (sym)
+       (and (trace-is-traceable-p sym)
+            (string-match-p regexp (symbol-name sym))
+            (untrace-function sym))))))
+
 (defun untrace-all ()
-  "Untraces all currently traced functions."
+  "Remove traces from all currently traced functions."
   (interactive)
   (mapatoms #'untrace-function))
 
-- 
2.8.3


^ permalink raw reply related	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2022-09-13 11:11 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-16 13:32 bug#27397: [PATCH] New commands for bulk tracing of elisp functions Phil Sainty
2017-06-16 14:58 ` Dmitry Gutov
2017-06-17  8:43   ` Phil Sainty
2017-06-17  9:13     ` Dmitry Gutov
2017-06-19  7:45     ` Michael Albinus
2017-06-19  9:35       ` Phil Sainty
2017-06-19  9:56         ` Michael Albinus
2017-06-19 11:00           ` Phil Sainty
2017-06-19 12:05             ` Michael Albinus
2017-06-19 12:17               ` Phil Sainty
2017-06-19 12:50               ` Dmitry Gutov
2017-06-19 13:07                 ` Michael Albinus
2017-06-19 11:27           ` Dmitry Gutov
2017-06-19 11:36             ` Michael Albinus
2017-06-19 12:04               ` Dmitry Gutov
2017-06-19 12:08                 ` Michael Albinus
2017-06-19 12:24                 ` Phil Sainty
2017-06-16 15:43 ` Kaushal Modi
2017-06-17  8:48   ` Phil Sainty
2017-06-17  9:20 ` Phil Sainty
2017-06-17 12:31   ` Phil Sainty
2017-06-17 22:59     ` Dmitry Gutov
2017-06-18  1:06       ` Phil Sainty
2017-06-18  6:32         ` Dmitry Gutov
2017-06-18 11:22         ` Phil Sainty
2019-06-27 18:01           ` bug#1343: " Lars Ingebrigtsen
2019-06-28 11:25             ` bug#27397: " Phil Sainty
2021-04-17  5:51               ` bug#1343: [PATCH] trace package Stefan Kangas
2021-04-17 14:01                 ` bug#27397: " Phil Sainty
2021-10-21 20:29                   ` Stefan Kangas
2022-07-15  4:08               ` bug#27397: bug#1343: bug#27397: [PATCH] New commands for bulk tracing of elisp functions Phil Sainty
2022-07-15  6:23                 ` Eli Zaretskii
2022-07-15 11:09                   ` Phil Sainty
2022-09-11 11:49                 ` Lars Ingebrigtsen
2022-09-11 12:33                   ` Eli Zaretskii
2022-09-12 13:26                   ` Michael Albinus
2022-09-12 22:17                     ` bug#1343: " Phil Sainty
2022-09-13  7:34                       ` bug#27397: " Michael Albinus
2022-09-13 11:11                     ` Lars Ingebrigtsen
2017-06-17 23:03   ` Dmitry Gutov

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).