-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdb-comint-mode.el
224 lines (183 loc) · 6.96 KB
/
cdb-comint-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
;; cdb-comint-mode.el --- Emacs major-mode for inferior cdb/windbg process.
;; Author : Jostein Kjønigsen <[email protected]>
;; Created : October 2016
;; Version : 0.0.1
;; Keywords : inferior-mode, convenience
;; This file is NOT part of GNU Emacs.
;;; License:
;; cdb-comint-mode.el is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; at your option any later version.
;; cdb-comint-mode.el is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;; Commentary:
;; cdb-comint-mode is a comint mode for Emacs which allows you to run a
;; cdb.exe (command line version of windbg) in a inferior process,
;;
;; Main features:
;; - Syntax highlighting.
;; - Automatic SOS loading, across .NET versions.
;; - Clickable pointers.
;; Usage:
;; Put cdb-comint-mode.el in your load path
;; Add (require 'cdb-comint-mode) to your .emacs or ~/.emacs.d/init.el
;;
;; Do: `M-x cdb-dump'
;; Away you go.
;;; Code:
(require 'comint)
(defvar cdb-custom-update-function nil
"Function to call upon cdb-comint-mode buffer changes.")
(defvar cdb--pointer-update-pos nil)
(defvar cdb--file-link-update-pos nil)
(defvar cdb--latest-buffer nil)
(defun cdb--update-hightlight (&optional string)
;; highlight typical commands
(hi-lock-face-buffer "\s!\\w+" 'hi-green-b)
(hi-lock-face-buffer "\s\\.\\w+" 'hi-green-b)
;; monitors are always ugly
(hi-lock-face-buffer "Monitor.Enter" 'hi-red-b)
;; exceptions
(hi-lock-face-buffer "[A-Za-z]+Exception" 'hi-red-b)
(cdb--update-pointers)
(cdb--update-file-links)
(when cdb-custom-update-function
(funcall cdb-custom-update-function)))
;; TODO: make configurable instead.
;; TODO: make option to launch 32-bit?
(defun cdb--get-exe-path ()
"C:/Program Files (x86)/Windows Kits/8.1/Debuggers/x64/cdb.exe")
(defun cdb-debug-process (pid)
(interactive "nProcess ID: ")
(cdb-run (number-to-string pid) "-p" (number-to-string pid)))
(defun cdb-run (&optional identifier &rest arguments)
(interactive)
(let* ((full-name (concat "cdb " identifier))
(buffer-name (concat "*" full-name "*"))
(buffer (get-buffer-create buffer-name)))
(setq cdb--latest-buffer buffer-name)
(switch-to-buffer buffer)
(cdb-comint-mode)
(setq-local comint-prompt-regexp "[0-9]+:[0-9]+>")
(apply #'make-comint full-name (cdb--get-exe-path)
nil arguments)))
(defun cdb-dump (filename)
(interactive "fCrash dump file: ")
(cdb-run (file-name-base filename)
"-z" (expand-file-name filename)))
;; actual major mode
(define-derived-mode cdb-comint-mode comint-mode "CDB"
(make-local-variable 'comint-output-filter-function)
(setq-local comint-output-filter-functions #'cdb--update-hightlight)
(setq-local cdb--pointer-update-pos nil)
(setq-local cdb--file-link-update-pos nil)
;; this makes it read only; a contentious subject as some prefer the
;; buffer to be overwritable.
(setq comint-prompt-read-only t)
(cdb--update-hightlight))
;;
;; make pointers clicky
;;
(defconst cdb--pointer-regexp "[a-fA-F0-9]\\{8,16\\}")
(defun cdb--update-pointers ()
(interactive)
(save-excursion
;; optimized update
(if cdb--pointer-update-pos
(goto-char cdb--pointer-update-pos)
(goto-char (point-min)))
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] #'cdb--dump-object)
(define-key map [mouse-3] #'cdb--write-pointer)
(while (re-search-forward cdb--pointer-regexp nil t)
(let ((start (match-beginning 0))
(end (match-end 0)))
(add-text-properties
start end (list 'mouse-face 'highlight
'help-echo "mouse-2: dump the object represented by this link"
'keymap map)))))
(setq cdb--pointer-update-pos (point-max))))
(defun cdb--pointer-at-point ()
(interactive)
(substring-no-properties (thing-at-point 'symbol)))
(defun cdb--write-pointer (event)
(interactive "e")
(let ((window (posn-window (event-end event)))
(pos (posn-point (event-end event)))
file)
(if (not (windowp window))
(error "No pointer found"))
(with-current-buffer (window-buffer window)
(goto-char pos)
(let ((pointer (cdb--pointer-at-point)))
(goto-char (point-max))
(insert pointer)))))
(defun cdb--dump-object (event)
(interactive "e")
(let ((window (posn-window (event-end event)))
(pos (posn-point (event-end event)))
file)
(if (not (windowp window))
(error "No pointer found"))
(with-current-buffer (window-buffer window)
(goto-char pos)
(let ((pointer (cdb--pointer-at-point)))
(goto-char (point-max))
(insert (concat "!do " pointer)))
(comint-send-input))))
;;
;; make source-line references clicky
;;
;; should match [D:\Source\foo bar\mega.baz.cs @ 234]
(defconst cdb--source-line-regexp
"\\[\\([A-Z]:[\\A-Za-z0-9\\ \.]+\\) @ \\([0-9]+\\)\\]")
(defun cdb--update-file-links ()
(interactive)
(save-excursion
;; optimized update
(if cdb--file-link-update-pos
(goto-char cdb--file-link-update-pos)
(goto-char (point-min)))
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] #'cdb--visit-file)
(while (re-search-forward cdb--source-line-regexp nil t)
(let ((start (match-beginning 0))
(end (match-end 2))
(filename (match-string 1))
(line-num (match-string 2)))
(add-text-properties
start end (list 'mouse-face 'highlight
'help-echo "mouse-2: dump the object represented by this link"
'keymap map
'filename filename
'line-num line-num)))))
(setq cdb--file-link-update-pos (point-max))))
(defun cdb--visit-file (event)
(interactive "e")
(let ((window (posn-window (event-end event)))
(pos (posn-point (event-end event)))
file)
(if (not (windowp window))
(error "No pointer found"))
(with-current-buffer (window-buffer window)
(goto-char pos)
(let ((filename (get-text-property (point) 'filename))
(line-num (get-text-property (point) 'line-num)))
(find-file filename)
(goto-line (string-to-number line-num))))))
;; utility functions
(defun cdb-send-command (string)
(let ((buffer (get-buffer-create cdb--latest-buffer)))
(comint-send-string (get-buffer-process buffer)
(concat string "\n"))))
(defun cdb-load-sos ()
(interactive)
(cdb-send-command ".sympath %temp%\\symbolcache")
(cdb-send-command ".symfix+")
(cdb-send-command ".cordll -ve -u -l")
(cdb-send-command ".reload"))
(provide 'cdb-comint-mode)
;;; cdb-comint-mode.el ends here