5

How can I monitor when Emacs loads a library because of an autoloaded function, and also know the name of that function?

EXAMPLE:  I type M-x eval-buffer in a buffer where one of the functions uses setf (and I have not previously used this function during my Emacs session). I would like to see a message similar to: "setf called within name-of-function, causing it to load macroexp.el by virtue of a require statement within gv.el."

Here is a start ...

(defun load-tracing-function (orig-fun &rest args)
  (message "`load' called with args %S" args)
  (let ((res (apply orig-fun args)))
    (message "`load' returned %S" res)
    res))
(advice-add 'load :around #'load-tracing-function)

(defun require-tracing-function (orig-fun &rest args)
  (message "`require' called with args %S" args)
  (let ((res (apply orig-fun args)))
    (message "`require' returned %S" res)
    res))
(advice-add 'require :around #'require-tracing-function)
lawlist
  • 18,826
  • 5
  • 37
  • 118

3 Answers3

3

Currently, autoloading a function is done all in C code with fairly few Lisp-visible effects (other than the loading itself). As you have discovered advising load may not catch it, for example.

I can see two ways to attack the problem:

  • use hooks (e.g. after-load-functions) and advice to detect loads, and then use the backtrace mechanism to try and discover what triggered the load (see around macroexp--trim-backtrace-frame for code messing with the backtrace for a somewhat related purpose).
  • redefine the autoloaded functions you're interested in so they use your own autoloading mechanism rather than the built-in one. It's not terribly hard to do, you can start with:

    (defun my-autoload (fun file)
      (defalias fun (lambda (&rest args) (load file) (apply fun args))))
    

    which you can then refine to look at the backtrace to find the caller.

Stefan
  • 26,154
  • 3
  • 46
  • 84
1
(require 'help-fns)

(defun require--tracing-function (orig-fun &rest args)
"When testing with `emacs -q`, start by requiring `help-fns.el`."
  (message "`require' called with args %S" args)
  (with-current-buffer (get-buffer-create "*TRACE*")
    (let* ((standard-output (current-buffer))
           (print-escape-newlines t)
           (print-level 8)
           (print-length 50)
           beg end)
        (goto-char (point-max))
        (setq beg (point))
        (setq truncate-lines t)
        (set-buffer-multibyte t)
        (setq buffer-undo-list t)
        (backtrace)
        (insert "===============================\n")
        (setq end (point))
        (narrow-to-region beg end)
        (let ((regex
                (concat
                  "^\s+byte-code\("
                  "\\(\\(?:.\\)*?\\)"
                  "\s"
                  "\\[\\(.*\\)\\]"
                  "\s"
                  "\\([0-9]+\\)"
                  "\)"))
              (bytestr (propertize "BYTESTR" 'face '(:foreground "RoyalBlue")))
              (maxdepth (propertize "MAXDEPTH" 'face '(:foreground "RoyalBlue"))))
          (goto-char (point-max))
          (while (re-search-backward regex nil t)
            (when (match-string 1)
              (replace-match bytestr nil nil nil 1))
            (when (match-string 2)
              (let ((constants
                     (propertize (match-string 2) 'face '(:foreground "purple"))))
                (replace-match constants nil 'literal nil 2)))
            (when (match-string 3)
              (replace-match maxdepth nil nil nil 3))))
        ;;; See the Emacs Lisp manual:  Byte-Code Function Objects
        (let ((regex
                (concat
                   "#\\["
                   ;;; argdesc
                   "\\([0-9]+\\)"
                   ;;; byte-code
                   "\\(?:\s\\(.*?\\)\\)?"
                   "\s"
                   ;;; constants
                   "\\[\\(.*\\)\\]"
                   "\s"
                   ;;; stacksize
                   "\\([0-9]+\\)"
                   ;;; docstring
                   "\\(?:\s\\(.*?\\)\\)?"
                   ;;; interactive
                   "\\(?:\s\\(.*?\\)\\)?"
                   "\\]"))
              (argdesc
                (propertize "ARGDESC" 'face '(:foreground "orange")))
              (byte-code
                (propertize "BYTE-CODE" 'face '(:foreground "orange")))
              (stacksize
                (propertize "STACKSIZE" 'face '(:foreground "orange")))
              (docstring
                (propertize "DOCSTRING" 'face '(:foreground "orange")))
              (interactive
                (propertize "INTERACTIVE" 'face '(:foreground "orange"))))
          (goto-char (point-max))
          (while (re-search-backward regex nil t)
            (when (match-string 1)
              (replace-match argdesc nil nil nil 1))
            (when (match-string 2)
              (replace-match byte-code nil nil nil 2))
            (when (match-string 3)
              (let ((constants
                      (propertize
                        (match-string 3) 'face '(:foreground "ForestGreen"))))
                (replace-match constants nil 'literal nil 3)))
            (when (match-string 4)
              (replace-match stacksize nil nil nil 4))
            (when (match-string 5)
              (replace-match docstring nil nil nil 5))
            (when (match-string 6)
              (replace-match interactive nil nil nil 6))))
        (let ((regex
                (concat
                  "^\s+\(let\\*\s\(\(standard-output.*\(current-buffer\)\)\)$"
                  "\\|"
                  "^\s+\(let\s\(\(res\s.*res\)\sres\)$"
                  "\\|"
                  (concat "^\s+\(save-current-buffer\s\(set-buffer.*"
                          "\(current-buffer\)\)\)\)$")
                  "\\|"
                  "^\s+backtrace\(\)$"
                  "\\|"
                  "^\s+apply\(require--tracing-function .*\)$"
                  "\\|"
                  "^\s+require--tracing-function\(.*\)$")))
          (goto-char (point-max))
          (while (re-search-backward regex nil t)
            (delete-region (match-beginning 0) (1+ (match-end 0)))))
        (goto-char (point-min))
        ;;; A slight variation of the built-in `debugger-make-xrefs'.
        (while (progn
           (goto-char (+ (point) 2))
           (skip-syntax-forward "^w_")
           (not (eobp)))
          (let* ((beg (point))
                 (end (progn (skip-syntax-forward "w_") (point)))
                 (fn (function-called-at-point))
                 (sym (intern-soft (buffer-substring-no-properties beg end)))
                 (file
                   (if fn
                     (let* (
                          (function fn)
                          (advised (and (symbolp function)
                              (featurep 'nadvice)
                              (advice--p (advice--symbol-function function))))
                          ;; If the function is advised, use the symbol that has the
                          ;; real definition, if that symbol is already set up.
                          (real-function
                            (or (and advised
                                     (advice--cd*r
                                       (advice--symbol-function function)))
                               function))
                          ;; Get the real definition.
                         (def (if (symbolp real-function)
                                 (or (symbol-function real-function)
                                     (signal 'void-function (list real-function)))
                                 real-function))
                         (aliased (or (symbolp def)
                               ;; Advised & aliased function.
                               (and advised (symbolp real-function)
                              (not (eq 'autoload (car-safe def))))))
                         (file-name
                           (find-lisp-object-file-name
                             function (if aliased 'defun def))))
                      file-name)
                  (and sym (symbol-file sym 'defun)))))
            (when file
              (goto-char beg)
              ;; help-xref-button needs to operate on something matched
              ;; by a regexp, so set that up for it.
              (re-search-forward "\\(\\sw\\|\\s_\\)+")
              (help-xref-button 0 'help-function-def sym file)))
          (forward-line 1))
        (widen)
      (display-buffer (current-buffer))))
  (let ((res (apply orig-fun args)))
    (message "`require' returned %S" res)
    res))

(advice-add 'require :around #'require--tracing-function)
  • Highlighting C source code functions in the debugging buffer was addressed in the answer to "Debugging — debugger-mode — how to highlight the culprit": https://emacs.stackexchange.com/a/29879/2287 It entails a slight variation of the built-in debugger-make-xrefs.

I have submitted a feature request regarding tracing autoloaded functions and here is the tracking link: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26470

lawlist
  • 18,826
  • 5
  • 37
  • 118
0

Insert (backtrace) at the start of the library.

Daanturo
  • 180
  • 8
  • This would not be practical for all of the built-in libraries that contain `autoload`, but perhaps it could be used for a few libraries that have already been identified as possible likely suspects .... The accepted solution (as of 04/12/2017) worked for all libraries without the need to modify them by inserting `(backtrace)`. It is possible that a tweak or two may be needed for later versions of Emacs .... – lawlist Nov 14 '20 at 04:36