Subject: bug#26470: Feature Request: Autoload tracing function.

From: Keith David Bershatsky <[email-masked]>
Date: Wed, 12 Apr 2017 20:55:06 -0700

I like to see when an autoloaded function is requiring or loading a library; however, I did not find any built-in facility to accomplish that goal.  I also could not find a function to show me the initial constants vector of a byte-code function that had been autoloaded.  The disassemble library does not show the initial constants vector -- instead, the disassemble library goes to work in earnest after the function has already been fully loaded and the constants vector has already changed -- i.e., it no longer begins with [require ...].  The only way I found to see the initial byte-code constants vector of an autoloaded function before it was fully loaded was to use the following tracing function.

The Emacs team may wish to consider adding a function that can show a user all six (6) possible elements of a byte-code function without necessarily fully loading it if has been autoloaded.  And, the Emacs team may wish to offer a tracing mechanism to let a user know when a particular autoloaded function is loading a library.

(require 'help-fns)

(defun require--tracing-function (orig-fun &rest args)
  (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 "^\s+byte-code\(\\(\\(?:.\\)*?\\)\s\\(\\[.*\\]\\)\s\\([0-9]+\\)\)"))
          (goto-char (point-max))
          (while (re-search-backward regex nil t)
            (replace-match "BYTESTR" nil nil nil 1)
            (replace-match "MAXDEPTH" nil nil nil 3)))
        (let ((regex
                (concat
                  "^\s+\(let\\*\s\(\(standard-output.*\(current-buffer\)\)\)$"
                  "\\|"
                  "^\s+\(let\s\(\(res\s.*res\)\sres\)$"
                  "\\|"
                  "^\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))
        ;;; 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)