]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/dwarf-mode.el
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / dwarf-mode.el
index 4c995ef763b30f4965dac5f2ab5e12270df605ed..a1b2fdb75eb8526a22078333aa92b3957c0cf4ec 100644 (file)
@@ -1,6 +1,8 @@
-;;; dwarf-mode.el --- Browser for DWARF information.
+;;; dwarf-mode.el --- Browser for DWARF information. -*-lexical-binding:t-*-
 
-;; Version: 1.1
+;; Version: 1.5
+
+;; Copyright (C) 2012-2020 Free Software Foundation, Inc.
 
 ;; This file is not part of GNU Emacs, but is distributed under the
 ;; same terms:
 (defvar dwarf-file nil
   "Buffer-local variable holding the file name passed to objdump.")
 
+(defvar dwarf--process nil
+  "Running objdump process, or nil.")
+
+(defvar dwarf--deletion-region nil
+  "Region to delete before inserting text in `dwarf--filter'.")
+
+(defun dwarf--check-running ()
+  "Throw an exception if an objdump process is already running."
+  (when dwarf--process
+    (error "An objdump process is still running in this buffer")))
+
+(defun dwarf--filter (proc string)
+  "Filter function for objdump processes."
+  (when (buffer-live-p (process-buffer proc))
+    (with-current-buffer (process-buffer proc)
+      (save-excursion
+       (let ((inhibit-read-only t))
+         (when dwarf--deletion-region
+           (apply #'delete-region dwarf--deletion-region)
+           (setq dwarf--deletion-region nil))
+          (goto-char (process-mark proc))
+          (insert string)
+          (set-marker (process-mark proc) (point))
+         (set-buffer-modified-p nil))))))
+
+(defun dwarf--sentinel (_proc _status)
+  (setq mode-line-process nil)
+  (setq dwarf--process nil))
+
+(defun dwarf--invoke (start end &rest command)
+  "Invoke a command and arrange to insert output into the current buffer."
+  (setq mode-line-process "[Running]")
+  (setq dwarf--deletion-region (list start end))
+  (setq dwarf--process (make-process :name "objdump"
+                                    :buffer (current-buffer)
+                                    :command command
+                                    :connection-type 'pipe
+                                    :noquery t
+                                    :filter #'dwarf--filter
+                                    :sentinel #'dwarf--sentinel))
+  (set-marker (process-mark dwarf--process) (point)))
+
 ;; Expand a "..." to show all the child DIES.  NEW-DEPTH controls how
 ;; deep to display the new dies; `nil' means display all of them.
 (defun dwarf-do-insert-substructure (new-depth die)
+  (dwarf--check-running)
   (let ((inhibit-read-only t))
     (beginning-of-line)
-    (delete-region (point) (progn
-                            (end-of-line)
-                            (forward-char)
-                            (point)))
-    (save-excursion
-      (apply #'call-process dwarf-objdump-program nil (current-buffer) nil
-            "-Wi" (concat "--dwarf-start=0x" die)
-            (expand-file-name dwarf-file)
-            (if new-depth (list (concat "--dwarf-depth="
-                                        (int-to-string new-depth))))))
+    (apply #'dwarf--invoke
+          (point) (save-excursion
+                    (end-of-line)
+                    (forward-char)
+                    (point))
+          dwarf-objdump-program "-Wi" (concat "--dwarf-start=0x" die)
+          (expand-file-name dwarf-file)
+          (if new-depth (list (concat "--dwarf-depth="
+                                      (int-to-string new-depth)))))
     (set-buffer-modified-p nil)))
 
 (defun dwarf-insert-substructure-button (die)
@@ -96,7 +140,7 @@ A prefix argument means expand all children."
   'action #'dwarf-die-button-action)
 
 ;; Helper regexp to match a DIE reference.
-(defconst dwarf-die-reference ": \\(<0x\\([0-9a-f]+\\)>\\)\\s *$")
+(defconst dwarf-die-reference "\\(<0x\\([0-9a-f]+\\)>\\)")
 
 ;; Helper regexp to match a `...' indicating that there are hidden
 ;; children.
@@ -130,16 +174,29 @@ A prefix argument means expand all children."
 ;; are the way they are because this is also called as a
 ;; revert-buffer-function.
 (defun dwarf-do-refresh (&rest ignore)
+  (dwarf--check-running)
   (let ((inhibit-read-only t))
-    (erase-buffer)
-    (save-excursion
-      (call-process dwarf-objdump-program
-                   nil (current-buffer) nil
-                   "-Wi" "--dwarf-depth=1"
-                   (expand-file-name dwarf-file)))
+    (dwarf--invoke (point-min) (point-max)
+                  dwarf-objdump-program "-Wi" "--dwarf-depth=1"
+                  (expand-file-name dwarf-file))
     (set-buffer-modified-p nil)))
 
-;;;###autoload
+(defvar dwarf-mode-syntax-table
+  (let ((table (make-syntax-table)))
+    ;; This at least makes it so mark-sexp on some hex digits inside
+    ;; <...> does not also copy the ">".
+    (modify-syntax-entry ?< "(>" table)
+    (modify-syntax-entry ?> ")<" table)
+    table)
+  "Syntax table for dwarf-mode buffers.")
+
+(defvar dwarf-mode-map
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map special-mode-map)
+    (define-key map [(control ?m)] #'dwarf-insert-substructure)
+    map)
+  "Keymap for dwarf-mode buffers.")
+
 (define-derived-mode dwarf-mode special-mode "DWARF"
   "Major mode for browsing DWARF output.
 
@@ -150,8 +207,6 @@ A prefix argument means expand all children."
   (set (make-local-variable 'revert-buffer-function) #'dwarf-do-refresh)
   (jit-lock-register #'dwarf-fontify-region))
 
-(define-key dwarf-mode-map [(control ?m)] #'dwarf-insert-substructure)
-
 ;;;###autoload
 (defun dwarf-browse (file)
   "Invoke `objdump' and put output into a `dwarf-mode' buffer.
@@ -161,7 +216,10 @@ This is the main interface to `dwarf-mode'."
         (buffer (generate-new-buffer (concat "*DWARF for " base-name "*"))))
     (pop-to-buffer buffer)
     (dwarf-mode)
+    (setq default-directory (file-name-directory file))
     (set (make-local-variable 'dwarf-file) file)
+    (set (make-local-variable 'dwarf--process) nil)
+    (set (make-local-variable 'dwarf--deletion-region) nil)
     (dwarf-do-refresh)))
 
 (provide 'dwarf-mode)