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)
(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)
(advice-add 'require :around #'require-tracing-function)

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.

  • Thank you for the ideas. I'll spend some time working with them over the next few days. – lawlist Mar 30 '17 at 7:05
(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)
        (insert "===============================\n")
        (setq end (point))
        (narrow-to-region beg end)
        (let ((regex
              (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
                   ;;; argdesc
                   ;;; byte-code
                   ;;; constants
                   ;;; stacksize
                   ;;; docstring
                   ;;; interactive
                (propertize "ARGDESC" 'face '(:foreground "orange")))
                (propertize "BYTE-CODE" 'face '(:foreground "orange")))
                (propertize "STACKSIZE" 'face '(:foreground "orange")))
                (propertize "DOCSTRING" 'face '(:foreground "orange")))
                (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
                        (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+\(save-current-buffer\s\(set-buffer.*"
                  "^\s+apply\(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)))
                   (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.
                            (or (and advised
                                       (advice--symbol-function function)))
                          ;; Get the real definition.
                         (def (if (symbolp real-function)
                                 (or (symbol-function real-function)
                                     (signal 'void-function (list real-function)))
                         (aliased (or (symbolp def)
                               ;; Advised & aliased function.
                               (and advised (symbolp real-function)
                              (not (eq 'autoload (car-safe def))))))
                             function (if aliased 'defun def))))
                  (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))
      (display-buffer (current-buffer))))
  (let ((res (apply orig-fun args)))
    (message "`require' returned %S" 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


Insert (backtrace) at the start of the library.

  • 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 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.