[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#26470: Feature Request: Autoload tracing function.

From: Keith David Bershatsky
Subject: bug#26470: Feature Request: Autoload tracing function.
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 

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)
        (insert "===============================\n")
        (setq end (point))
        (narrow-to-region beg end)
        (let ((regex 
          (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
                  "^\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))
        ;;; 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--cd*r (advice--symbol-function 
                          ;; Get the real definition.
                         (def (if (symbolp real-function)
                                 (or (symbol-function real-function)
                                     (signal 'void-function (list 
                         (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))))
                  (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)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]