dwarf-mode.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ;;; dwarf-mode.el --- Browser for DWARF information. -*-lexical-binding:t-*-
  2. ;; Version: 1.7
  3. ;; Copyright (C) 2012-2022 Free Software Foundation, Inc.
  4. ;; This file is not part of GNU Emacs, but is distributed under the
  5. ;; same terms:
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (defvar dwarf-objdump-program "objdump")
  18. (defconst dwarf-font-lock-keywords
  19. '(
  20. ;; Name and linkage name.
  21. ("DW_AT_[a-zA-Z_]*name\\s *:\\(?:\\s *(.*):\\)?\\s *\\(.*\\)\\s *$"
  22. (1 font-lock-function-name-face))
  23. ("Compilation Unit @ offset 0x[0-9a-f]+"
  24. (0 font-lock-string-face))
  25. ))
  26. (defvar dwarf-file nil
  27. "Buffer-local variable holding the file name passed to objdump.")
  28. (defvar dwarf--process nil
  29. "Running objdump process, or nil.")
  30. (defvar dwarf--deletion-region nil
  31. "Region to delete before inserting text in `dwarf--filter'.")
  32. (defun dwarf--check-running ()
  33. "Throw an exception if an objdump process is already running."
  34. (when dwarf--process
  35. (error "An objdump process is still running in this buffer")))
  36. (defun dwarf--filter (proc string)
  37. "Filter function for objdump processes."
  38. (when (buffer-live-p (process-buffer proc))
  39. (with-current-buffer (process-buffer proc)
  40. (save-excursion
  41. (let ((inhibit-read-only t))
  42. (when dwarf--deletion-region
  43. (apply #'delete-region dwarf--deletion-region)
  44. (setq dwarf--deletion-region nil))
  45. (goto-char (process-mark proc))
  46. (insert string)
  47. (set-marker (process-mark proc) (point))
  48. (set-buffer-modified-p nil))))))
  49. (defun dwarf--sentinel (proc _status)
  50. (when (buffer-live-p (process-buffer proc))
  51. (with-current-buffer (process-buffer proc)
  52. (setq mode-line-process nil)
  53. (setq dwarf--process nil))))
  54. (defun dwarf--invoke (start end &rest command)
  55. "Invoke a command and arrange to insert output into the current buffer."
  56. (setq mode-line-process "[Running]")
  57. (setq dwarf--deletion-region (list start end))
  58. (setq dwarf--process (make-process :name "objdump"
  59. :buffer (current-buffer)
  60. :command command
  61. :connection-type 'pipe
  62. :noquery t
  63. :filter #'dwarf--filter
  64. :sentinel #'dwarf--sentinel))
  65. (set-marker (process-mark dwarf--process) (point)))
  66. ;; Expand a "..." to show all the child DIES. NEW-DEPTH controls how
  67. ;; deep to display the new dies; `nil' means display all of them.
  68. (defun dwarf-do-insert-substructure (new-depth die)
  69. (dwarf--check-running)
  70. (let ((inhibit-read-only t))
  71. (beginning-of-line)
  72. (apply #'dwarf--invoke
  73. (point) (save-excursion
  74. (end-of-line)
  75. (forward-char)
  76. (point))
  77. dwarf-objdump-program "-Wi" (concat "--dwarf-start=0x" die)
  78. (expand-file-name dwarf-file)
  79. (if new-depth (list (concat "--dwarf-depth="
  80. (int-to-string new-depth)))))
  81. (set-buffer-modified-p nil)))
  82. (defun dwarf-insert-substructure-button (die)
  83. (beginning-of-line)
  84. (unless (looking-at "^ <\\([0-9]+\\)>")
  85. (error "Unrecognized line."))
  86. (let ((new-depth (1+ (string-to-number (match-string 1)))))
  87. (dwarf-do-insert-substructure new-depth die)))
  88. (defun dwarf-insert-substructure (arg)
  89. "Expand a `...' to show children of the current DIE.
  90. By default, expands just one level of children.
  91. A prefix argument means expand all children."
  92. (interactive "P")
  93. (beginning-of-line)
  94. (unless (looking-at "^ <\\([0-9]+\\)><\\([0-9a-f]+\\)>")
  95. (error "Unrecognized line."))
  96. (let ((die (match-string 2)))
  97. (if arg
  98. (dwarf-do-insert-substructure nil die)
  99. (dwarf-insert-substructure-button die))))
  100. ;; Called when a button is pressed.
  101. ;; Either follows a DIE reference, or expands a "...".
  102. (defun dwarf-die-button-action (button)
  103. (let* ((die (button-get button 'die))
  104. ;; Note that the first number can only be decimal. It is
  105. ;; included in this search because otherwise following a ref
  106. ;; might lead to a zero-length boolean attribute in the
  107. ;; previous DIE.
  108. (die-rx (concat "^\\s *<[0-9]+><" die ">:"))
  109. (old (point))
  110. (is-ref (button-get button 'die-ref)))
  111. (if is-ref
  112. (progn
  113. (goto-char (point-min))
  114. (if (re-search-forward die-rx nil 'move)
  115. (push-mark old)
  116. (goto-char old)
  117. (error "Could not find DIE <0x%s>" die)))
  118. (dwarf-insert-substructure-button die))))
  119. ;; Button definition.
  120. (define-button-type 'dwarf-die-button
  121. 'follow-link t
  122. 'action #'dwarf-die-button-action)
  123. ;; Helper regexp to match a DIE reference.
  124. (defconst dwarf-die-reference "\\(<0x\\([0-9a-f]+\\)>\\)")
  125. ;; Helper regexp to match a `...' indicating that there are hidden
  126. ;; children.
  127. (defconst dwarf-die-more "^ <[0-9]+><\\([0-9a-z]+\\)>: \\([.][.][.]\\)")
  128. ;; jit-lock callback function to fontify a region. This applies the
  129. ;; buttons, since AFAICT there is no good way to apply buttons via
  130. ;; font-lock.
  131. (defun dwarf-fontify-region (start end)
  132. (save-excursion
  133. (let ((beg-line (progn (goto-char start) (line-beginning-position)))
  134. (end-line (progn (goto-char end) (line-end-position))))
  135. (goto-char beg-line)
  136. (while (re-search-forward dwarf-die-reference end-line 'move)
  137. (let ((b-start (match-beginning 1))
  138. (b-end (match-end 1))
  139. (hex (match-string-no-properties 2)))
  140. (make-text-button b-start b-end :type 'dwarf-die-button
  141. 'die hex 'die-ref t)))
  142. ;; This is a bogus approach. Why can't we make buttons from the
  143. ;; font-lock defaults?
  144. (goto-char beg-line)
  145. (while (re-search-forward dwarf-die-more end-line 'move)
  146. (let ((hex (match-string-no-properties 1))
  147. (b-start (match-beginning 2))
  148. (b-end (match-end 2)))
  149. (make-text-button b-start b-end :type 'dwarf-die-button
  150. 'die hex 'die-ref nil))))))
  151. ;; Run objdump and insert the contents into the buffer. The arguments
  152. ;; are the way they are because this is also called as a
  153. ;; revert-buffer-function.
  154. (defun dwarf-do-refresh (&rest ignore)
  155. (dwarf--check-running)
  156. (let ((inhibit-read-only t))
  157. (dwarf--invoke (point-min) (point-max)
  158. dwarf-objdump-program "-Wi" "--dwarf-depth=1"
  159. (expand-file-name dwarf-file))
  160. (set-buffer-modified-p nil)))
  161. (defvar dwarf-mode-syntax-table
  162. (let ((table (make-syntax-table)))
  163. ;; This at least makes it so mark-sexp on some hex digits inside
  164. ;; <...> does not also copy the ">".
  165. (modify-syntax-entry ?< "(>" table)
  166. (modify-syntax-entry ?> ")<" table)
  167. table)
  168. "Syntax table for dwarf-mode buffers.")
  169. (defvar dwarf-mode-map
  170. (let ((map (make-sparse-keymap)))
  171. (set-keymap-parent map special-mode-map)
  172. (define-key map [(control ?m)] #'dwarf-insert-substructure)
  173. map)
  174. "Keymap for dwarf-mode buffers.")
  175. (define-derived-mode dwarf-mode special-mode "DWARF"
  176. "Major mode for browsing DWARF output.
  177. \\{dwarf-mode-map}"
  178. (set (make-local-variable 'font-lock-defaults) '(dwarf-font-lock-keywords))
  179. ;; FIXME: we could be smarter and check the file time.
  180. (set (make-local-variable 'revert-buffer-function) #'dwarf-do-refresh)
  181. (jit-lock-register #'dwarf-fontify-region))
  182. ;;;###autoload
  183. (defun dwarf-browse (file)
  184. "Invoke `objdump' and put output into a `dwarf-mode' buffer.
  185. This is the main interface to `dwarf-mode'."
  186. (interactive "fFile name: ")
  187. (let* ((base-name (file-name-nondirectory file))
  188. (buffer (generate-new-buffer (concat "*DWARF for " base-name "*"))))
  189. (pop-to-buffer buffer)
  190. (dwarf-mode)
  191. (setq default-directory (file-name-directory file))
  192. (set (make-local-variable 'dwarf-file) file)
  193. (set (make-local-variable 'dwarf--process) nil)
  194. (set (make-local-variable 'dwarf--deletion-region) nil)
  195. (dwarf-do-refresh)))
  196. (provide 'dwarf-mode)
  197. ;;; dwarf-mode.el ends here