dwarf-mode.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; dwarf-mode.el --- Browser for DWARF information.
  2. ;; Version: 1.2
  3. ;; Copyright (C) 2012-2015 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-z_]*name\\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. ;; Expand a "..." to show all the child DIES. NEW-DEPTH controls how
  29. ;; deep to display the new dies; `nil' means display all of them.
  30. (defun dwarf-do-insert-substructure (new-depth die)
  31. (let ((inhibit-read-only t))
  32. (beginning-of-line)
  33. (delete-region (point) (progn
  34. (end-of-line)
  35. (forward-char)
  36. (point)))
  37. (save-excursion
  38. (apply #'call-process dwarf-objdump-program nil (current-buffer) nil
  39. "-Wi" (concat "--dwarf-start=0x" die)
  40. (expand-file-name dwarf-file)
  41. (if new-depth (list (concat "--dwarf-depth="
  42. (int-to-string new-depth))))))
  43. (set-buffer-modified-p nil)))
  44. (defun dwarf-insert-substructure-button (die)
  45. (beginning-of-line)
  46. (unless (looking-at "^ <\\([0-9]+\\)>")
  47. (error "Unrecognized line."))
  48. (let ((new-depth (1+ (string-to-number (match-string 1)))))
  49. (dwarf-do-insert-substructure new-depth die)))
  50. (defun dwarf-insert-substructure (arg)
  51. "Expand a `...' to show children of the current DIE.
  52. By default, expands just one level of children.
  53. A prefix argument means expand all children."
  54. (interactive "P")
  55. (beginning-of-line)
  56. (unless (looking-at "^ <\\([0-9]+\\)><\\([0-9a-f]+\\)>")
  57. (error "Unrecognized line."))
  58. (let ((die (match-string 2)))
  59. (if arg
  60. (dwarf-do-insert-substructure nil die)
  61. (dwarf-insert-substructure-button die))))
  62. ;; Called when a button is pressed.
  63. ;; Either follows a DIE reference, or expands a "...".
  64. (defun dwarf-die-button-action (button)
  65. (let* ((die (button-get button 'die))
  66. ;; Note that the first number can only be decimal.
  67. (die-rx (concat "^\\s *\\(<[0-9]+>\\)?<"
  68. die ">[^<]"))
  69. (old (point))
  70. (is-ref (button-get button 'die-ref)))
  71. (if is-ref
  72. (progn
  73. (goto-char (point-min))
  74. (if (re-search-forward die-rx nil 'move)
  75. (push-mark old)
  76. (goto-char old)
  77. (error "Could not find DIE <0x%s>" die)))
  78. (dwarf-insert-substructure-button die))))
  79. ;; Button definition.
  80. (define-button-type 'dwarf-die-button
  81. 'follow-link t
  82. 'action #'dwarf-die-button-action)
  83. ;; Helper regexp to match a DIE reference.
  84. (defconst dwarf-die-reference "\\(<0x\\([0-9a-f]+\\)>\\)")
  85. ;; Helper regexp to match a `...' indicating that there are hidden
  86. ;; children.
  87. (defconst dwarf-die-more "^ <[0-9]+><\\([0-9a-z]+\\)>: \\([.][.][.]\\)")
  88. ;; jit-lock callback function to fontify a region. This applies the
  89. ;; buttons, since AFAICT there is no good way to apply buttons via
  90. ;; font-lock.
  91. (defun dwarf-fontify-region (start end)
  92. (save-excursion
  93. (let ((beg-line (progn (goto-char start) (line-beginning-position)))
  94. (end-line (progn (goto-char end) (line-end-position))))
  95. (goto-char beg-line)
  96. (while (re-search-forward dwarf-die-reference end-line 'move)
  97. (let ((b-start (match-beginning 1))
  98. (b-end (match-end 1))
  99. (hex (match-string-no-properties 2)))
  100. (make-text-button b-start b-end :type 'dwarf-die-button
  101. 'die hex 'die-ref t)))
  102. ;; This is a bogus approach. Why can't we make buttons from the
  103. ;; font-lock defaults?
  104. (goto-char beg-line)
  105. (while (re-search-forward dwarf-die-more end-line 'move)
  106. (let ((hex (match-string-no-properties 1))
  107. (b-start (match-beginning 2))
  108. (b-end (match-end 2)))
  109. (make-text-button b-start b-end :type 'dwarf-die-button
  110. 'die hex 'die-ref nil))))))
  111. ;; Run objdump and insert the contents into the buffer. The arguments
  112. ;; are the way they are because this is also called as a
  113. ;; revert-buffer-function.
  114. (defun dwarf-do-refresh (&rest ignore)
  115. (let ((inhibit-read-only t))
  116. (erase-buffer)
  117. (save-excursion
  118. (call-process dwarf-objdump-program
  119. nil (current-buffer) nil
  120. "-Wi" "--dwarf-depth=1"
  121. (expand-file-name dwarf-file)))
  122. (set-buffer-modified-p nil)))
  123. (define-derived-mode dwarf-mode special-mode "DWARF"
  124. "Major mode for browsing DWARF output.
  125. \\{dwarf-mode-map}"
  126. (set (make-local-variable 'font-lock-defaults) '(dwarf-font-lock-keywords))
  127. ;; FIXME: we could be smarter and check the file time.
  128. (set (make-local-variable 'revert-buffer-function) #'dwarf-do-refresh)
  129. (jit-lock-register #'dwarf-fontify-region))
  130. (define-key dwarf-mode-map [(control ?m)] #'dwarf-insert-substructure)
  131. ;;;###autoload
  132. (defun dwarf-browse (file)
  133. "Invoke `objdump' and put output into a `dwarf-mode' buffer.
  134. This is the main interface to `dwarf-mode'."
  135. (interactive "fFile name: ")
  136. (let* ((base-name (file-name-nondirectory file))
  137. (buffer (generate-new-buffer (concat "*DWARF for " base-name "*"))))
  138. (pop-to-buffer buffer)
  139. (dwarf-mode)
  140. (set (make-local-variable 'dwarf-file) file)
  141. (dwarf-do-refresh)))
  142. (provide 'dwarf-mode)
  143. ;;; dwarf-mode.el ends here