1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, or dbx under Emacs
3 ;; Author: Eric S. Raymond <eric@snark.thyrsus.com>
4 ;; Keywords: unix, tools
8 ;; Copyright (C) 1992 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el.
34 ;; Note: use of this package with sdb requires that your tags.el support
35 ;; the find-tag-noselect entry point. Stock distributions up to 18.57 do
36 ;; *not* include this feature; if it's not included with this file, email
37 ;; esr@snark.thyrsus.com for it or get 18.58.
39 ;; Further note: due to lossage in the Emacs-18 byte compiler, compiled
40 ;; versions of this code will fail with a complaint about gud-step if
41 ;; you invoke the gdb or sdb initializers. This should be fixed in 19.
48 ;; ======================================================================
49 ;; the overloading mechanism
51 (defun gud-overload-functions (gud-overload-alist)
52 "Overload functions defined in GUD-OVERLOAD-ALIST.
53 This association list has elements of the form
54 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
56 (function (lambda (p) (fset (car p
) (symbol-function (cdr p
)))))
59 (defun gud-debugger-startup (f d
)
60 (error "GUD not properly entered."))
62 (defun gud-marker-filter (proc s
)
63 (error "GUD not properly entered."))
65 (defun gud-visit-file (f)
66 (error "GUD not properly entered."))
68 (defun gud-set-break (proc f n
)
69 (error "GUD not properly entered."))
71 ;; This macro is used below to define some basic debugger interface commands.
72 ;; Of course you may use `gud-def' with any other debugger command, including
75 (defmacro gud-def
(func name key
&optional doc
)
76 (let* ((cstr (list 'if
'(not (= 1 arg
))
77 (list 'format
"%s %s" name
'arg
) name
)))
79 (list 'defun
func '(arg)
82 (list 'gud-call cstr
))
84 (list 'define-key
'gud-mode-map key
(list 'quote func
))))))
86 ;; All debugger-specific information is collected here
87 ;; Here's how it works, in case you ever need to add a debugger to the table.
89 ;; Each entry must define the following at startup:
92 ;; comint-prompt-regexp
93 ;; gud-<name>-debugger-startup
94 ;; gud-<name>-marker-filter
95 ;; gud-<name>-visit-file
96 ;; gud-<name>-set-break
98 ;; The job of the startup-command method is to fire up a copy of the debugger,
99 ;; given an object file and source directory.
101 ;; The job of the marker-filter method is to detect file/line markers in
102 ;; strings and set the global gud-last-frame to indicate what display
103 ;; action (if any) should be triggered by the marker. Note that only
104 ;; whetever the method *returns* is displayed in the buffer; thus, you
105 ;; can filter the debugger's output, interpreting some and passing on
108 ;; The job of the visit-file method is to visit and return the buffer indicated
109 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
112 ;; The job of the gud-set-break method is to send the commands necessary
113 ;; to set a breakpoint at a given line in a given source file.
115 ;; Debugger-specific information begins here:
117 ;; ======================================================================
120 (defun gud-gdb-debugger-startup (f d
)
121 (make-comint (concat "gud-" f
) "gdb" nil
"-fullname" "-cd" d f
))
123 (defun gud-gdb-marker-filter (proc s
)
124 (if (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" s
)
128 (substring string
(match-beginning 1) (match-end 1))
130 (substring string
(match-beginning 2) (match-end 2)))))
131 ;; this computation means the ^Z^Z-initiated marker in the
132 ;; input string is never emitted.
134 (substring string
0 (match-beginning 0))
135 (substring string
(match-end 0))
139 (defun gud-gdb-visit-file (f)
140 (find-file-noselect f
))
142 (defun gud-gdb-set-break (proc f n
)
143 (gud-call "break %s:%d" f n
))
147 "Run gdb on program FILE in buffer *gud-FILE*.
148 The directory containing FILE becomes the initial working directory
149 and source-file directory for your debugger."
150 (interactive "fRun gdb on file: ")
151 (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup
)
152 (gud-marker-filter . gud-gdb-marker-filter
)
153 (gud-visit-file . gud-gdb-visit-file
)
154 (gud-set-break . gud-gdb-set-break
)))
156 (gud-def gud-step
"step" "\C-c\C-s" "Step one source line with display")
157 (gud-def gud-stepi
"stepi" "\C-c\C-i" "Step one instruction with display")
158 (gud-def gud-next
"next" "\C-c\C-n" "Step one line (skip functions)")
159 (gud-def gud-cont
"cont" "\C-c\C-c" "Continue with display")
161 (gud-def gud-finish
"finish" "\C-c\C-f" "Finish executing current function")
162 (gud-def gud-up
"up" "\C-c<" "Up N stack frames (numeric arg)")
163 (gud-def gud-down
"down" "\C-c>" "Down N stack frames (numeric arg)")
165 (gud-common-init path
)
167 (setq comint-prompt-regexp
"^(.*gdb[+]?) *")
168 (run-hooks 'gdb-mode-hook
)
172 ;; ======================================================================
175 (defun gud-sdb-debugger-startup (f d
)
176 (make-comint (concat "gud-" f
) "sdb" nil f
"-" d
))
178 (defun gud-sdb-marker-filter (proc str
)
179 (if (string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
183 (substring string
(match-beginning 2) (match-end 2))
185 (substring string
(match-beginning 3) (match-end 3))))))
188 (defun gud-sdb-visit-file (f)
189 (find-tag-noselect f
))
191 (defun gud-sdb-set-break (proc f n
)
192 (gud-queue-send (format "e %s" f
) (format "%d b" n
)))
196 "Run sdb on program FILE in buffer *gud-FILE*.
197 The directory containing FILE becomes the initial working directory
198 and source-file directory for your debugger."
199 (interactive "fRun sdb on file: ")
200 (if (not (and (boundp 'tags-file-name
) (file-exists-p tags-file-name
)))
201 (error "The sdb support requires a valid tags table to work."))
202 (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup
)
203 (gud-marker-filter . gud-sdb-marker-filter
)
204 (gud-visit-file . gud-sdb-visit-file
)
205 (gud-set-break . gud-sdb-set-break
)))
207 (gud-def gud-step
"s" "\C-c\C-s" "Step one source line with display")
208 (gud-def gud-stepi
"i" "\C-c\C-i" "Step one instruction with display")
209 (gud-def gud-next
"S" "\C-c\C-n" "Step one source line (skip functions)")
210 (gud-def gud-cont
"c" "\C-c\C-c" "Continue with display")
212 (gud-common-init path
)
214 (setq comint-prompt-pattern
"\\(^\\|\n\\)\\*")
215 (run-hooks 'sdb-mode-hook
)
218 ;; ======================================================================
221 (defun gud-dbx-debugger-startup (f d
)
222 (make-comint (concat "gud-" file
) "dbx" nil f
))
224 (defun gud-dbx-marker-filter (proc str
)
226 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" str
)
229 (substring string
(match-beginning 2) (match-end 2))
231 (substring string
(match-beginning 1) (match-end 1))))))
234 (defun gud-dbx-visit-file (f)
235 (find-file-noselect f
))
237 (defun gud-dbx-set-break (proc f n
)
238 (gud-call "stop at \"%s\":%d" f n
))
242 "Run dbx on program FILE in buffer *gud-FILE*.
243 The directory containing FILE becomes the initial working directory
244 and source-file directory for your debugger."
245 (interactive "fRun dbx on file: ")
246 (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup
)
247 (gud-marker-filter . gud-dbx-marker-filter
)
248 (gud-visit-file . gud-dbx-visit-file
)
249 (gud-set-break . gud-dbx-set-break
)))
251 (gud-def gud-step
"step" "\C-c\C-s" "Step one source line with display")
252 (gud-def gud-stepi
"stepi" "\C-c\C-i" "Step one instruction with display")
253 (gud-def gud-next
"next" "\C-c\C-n" "Step one line (skip functions)")
254 (gud-def gud-cont
"cont" "\C-c\C-c" "Continue with display")
256 (gud-def gud-up
"up" "\C-c<" "Up N stack frames (numeric arg)")
257 (gud-def gud-down
"down" "\C-c>" "Down N stack frames (numeric arg)")
259 (gud-common-init path
)
260 (setq comint-prompt-regexp
"^[^)]*dbx) *")
262 (run-hooks 'dbx-mode-hook
)
266 ;; End of debugger-specific information
269 (defvar gud-mode-map nil
270 "Keymap for gud-mode.")
272 (defvar gud-commands nil
273 "List of strings or functions used by send-gud-command.
274 It is for customization by you.")
276 (defvar gud-command-queue nil
)
280 (setq gud-mode-map
(copy-keymap comint-mode-map
))
281 (define-key gud-mode-map
"\C-cl" 'gud-refresh
))
283 ;; Global mappings --- we'll invoke these from a source buffer.
284 (define-key ctl-x-map
" " 'gud-break
)
285 (define-key ctl-x-map
"&" 'send-gud-command
)
289 "Major mode for interacting with an inferior debugger process.
291 You start it up with one of the commands M-x gdb, M-x sdb, or
292 M-x dbx. Each entry point finishes by executing a hook; gdb-mode-hook,
293 sdb-mode-hook or dbx-mode-hook respectively.
295 After startup, the following commands are available:
299 \\[gud-refresh] displays in the other window the last line referred to
302 \\[gud-step], \\[gud-next], and \\[gud-stepi] in the gud window,
303 do a step-one-line, step-one-line (not entering function calls), and
304 step-one-instruction and then update the other window
305 with the current file and position. \\[gud-cont] continues
308 The above commands are common to all supported debuggers. If you are
309 using gdb or dbx, the following additional commands will be available:
311 \\[gud-up] pops up through an enclosing stack frame. \\[gud-down] drops
312 back down through one.
314 If you are using gdb, \\[gdb-finish] runs execution to the return from
315 the current function and stops.
317 These functions repeat themselves the appropriate number of times if you give a
320 If you are in a source file, you may do the following:
322 Set a breakpoint at the current line by doing \\[gud-break]. This causes
323 an appropriate set-break to be send to the debugger; of course, if the file
324 you're visiting doesn't correspond to any code in the executable this will
325 have no effect or raise an error.
327 Execute a user-defined command at point with \\[send-gud-command]; the
328 prefix argument is taken as an index into the list of strings gud-commands.
329 A %s in a gud-commands string is substituted with a number or address picked
332 Other commands for interacting with the debugger process are inherited from
333 comint mode, which see."
336 ; (kill-all-local-variables)
337 (setq major-mode
'gud-mode
)
338 (setq mode-name
"Debugger")
339 (setq mode-line-process
'(": %s"))
340 (use-local-map gud-mode-map
)
341 (make-local-variable 'gud-last-frame
)
342 (setq gud-last-frame nil
)
343 (make-local-variable 'comint-prompt-regexp
)
344 (run-hooks 'gud-mode-hook
)
347 (defvar current-gud-buffer nil
)
349 (defun gud-common-init (path)
350 ;; perform initializations common to all debuggers
351 (setq path
(expand-file-name path
))
352 (let ((file (file-name-nondirectory path
)))
353 (switch-to-buffer (concat "*gud-" file
"*"))
354 (setq default-directory
(file-name-directory path
))
355 (or (bolp) (newline))
356 (insert "Current directory is " default-directory
"\n")
357 (gud-debugger-startup file default-directory
))
359 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter
)
360 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel
)
361 (setq gud-command-queue nil
)
365 (defun gud-set-buffer ()
366 (cond ((eq major-mode
'gud-mode
)
367 (setq current-gud-buffer
(current-buffer)))))
369 (defun gud-filter (proc string
)
370 ;; This function is responsible for inserting output from your debugger
371 ;; into the buffer. The hard work is done by the method that is
372 ;; the value of gud-marker-filter.
373 (let ((inhibit-quit t
))
374 (gud-filter-insert proc
(gud-marker-filter proc string
))
375 ;; If we've got queued commands and we see a prompt, pop one and send it.
376 ;; In theory we should check that a prompt has been issued before sending
377 ;; queued commands. In practice, command responses from the first through
378 ;; penultimate elements of a command sequence are short enough that we
379 ;; don't really have to bother.
380 (if gud-command-queue
382 (gud-call (car gud-command-queue
))
383 (setq gud-command-queue
(cdr gud-command-queue
))
387 (defun gud-filter-insert (proc string
)
388 ;; Here's where the actual buffer insertion is done
389 (let ((moving (= (point) (process-mark proc
)))
390 (output-after-point (< (point) (process-mark proc
)))
391 (old-buffer (current-buffer))
393 (set-buffer (process-buffer proc
))
396 ;; Insert the text, moving the process-marker.
397 (goto-char (process-mark proc
))
399 (insert-before-markers string
)
400 (set-marker (process-mark proc
) (point))
401 ;; Check for a filename-and-line number.
402 ;; Don't display the specified file
403 ;; unless (1) point is at or after the position where output appears
404 ;; and (2) this buffer is on the screen.
405 (if (and gud-last-frame
(not output-after-point
)
406 (get-buffer-window (current-buffer)))
409 (set-buffer old-buffer
))
410 (if moving
(goto-char (process-mark proc
)))))
412 (defun gud-sentinel (proc msg
)
413 (cond ((null (buffer-name (process-buffer proc
)))
415 ;; Stop displaying an arrow in a source file.
416 (setq overlay-arrow-position nil
)
417 (set-process-buffer proc nil
))
418 ((memq (process-status proc
) '(signal exit
))
419 ;; Stop displaying an arrow in a source file.
420 (setq overlay-arrow-position nil
)
421 ;; Fix the mode line.
422 (setq mode-line-process
424 (symbol-name (process-status proc
))))
425 (let* ((obuf (current-buffer)))
426 ;; save-excursion isn't the right thing if
427 ;; process-buffer is current-buffer
430 ;; Write something in *compilation* and hack its mode line,
431 (set-buffer (process-buffer proc
))
432 ;; Force mode line redisplay soon
433 (set-buffer-modified-p (buffer-modified-p))
435 (insert ?
\n mode-name
" " msg
)
437 (goto-char (point-max))
438 (insert ?
\n mode-name
" " msg
)))
439 ;; If buffer and mode line will show that the process
440 ;; is dead, we can delete it now. Otherwise it
441 ;; will stay around until M-x list-processes.
442 (delete-process proc
))
443 ;; Restore old buffer, but don't restore old point
444 ;; if obuf is the gud buffer.
445 (set-buffer obuf
))))))
448 (defun gud-refresh (&optional arg
)
449 "Fix up a possibly garbled display, and redraw the arrow."
454 (defun gud-display-frame ()
455 "Find and obey the last filename-and-line marker from the debugger.
456 Obeying it means displaying in another window the specified file and line."
461 (gud-display-line (car gud-last-frame
) (cdr gud-last-frame
))
462 (setq gud-last-frame nil
))))
464 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
465 ;; and that its line LINE is visible.
466 ;; Put the overlay-arrow on the line LINE in that buffer.
468 (defun gud-display-line (true-file line
)
469 (let* ((buffer (gud-visit-file true-file
))
470 (window (display-buffer buffer t
))
478 (setq overlay-arrow-string
"=>")
479 (or overlay-arrow-position
480 (setq overlay-arrow-position
(make-marker)))
481 (set-marker overlay-arrow-position
(point) (current-buffer)))
482 (cond ((or (< pos
(point-min)) (> pos
(point-max)))
485 (set-window-point window overlay-arrow-position
)))
487 (defun gud-call (command &rest args
)
488 "Invoke the debugger COMMAND displaying source in other window."
491 (goto-char (point-max))
492 (let ((command (concat (apply 'format command args
) "\n"))
493 (proc (get-buffer-process current-gud-buffer
)))
494 (gud-filter-insert proc command
)
495 (send-string proc command
)
498 (defun gud-queue-send (&rest cmdlist
)
499 ;; Send the first command, queue the rest for send after successive
500 ;; send on subsequent prompts
502 (gud-call (car cmdlist
))
503 (setq gud-command-queue
(append gud-command-queue
(cdr cmdlist
))))
505 (defun gud-apply-from-source (func)
506 ;; Apply a method from the gud buffer environment, passing it file and line.
507 ;; This is intended to be used for gud commands called from a source file.
508 (if (not buffer-file-name
)
509 (error "There is no file associated with this buffer"))
510 (let ((file (file-name-nondirectory buffer-file-name
))
511 (line (save-restriction (widen) (1+ (count-lines 1 (point))))))
515 (get-buffer-process current-gud-buffer
)
521 "Set breakpoint at this source line."
523 (gud-apply-from-source 'gud-set-break
))
525 (defun gud-read-address()
526 "Return a string containing the core-address found in the buffer at point."
528 (let ((pt (point)) found begin
)
529 (setq found
(if (search-backward "0x" (- pt
7) t
)(point)))
530 (cond (found (forward-char 2)
532 (buffer-substring found
533 (progn (re-search-forward "[^0-9a-f]")
536 (t (setq begin
(progn (re-search-backward "[^0-9]") (forward-char 1)
539 (re-search-forward "[^0-9]")
541 (buffer-substring begin
(point)))))))
544 (defun send-gud-command (arg)
545 "This command reads the number where the cursor is positioned. A numeric arg
546 selects the ARG'th member COMMAND of the list gud-commands. If COMMAND is a
547 string, (format COMMAND ADDR) is inserted at the end of the debugger buffer,
548 otherwise (funcall COMMAND ADDR) is inserted.
549 For example, \"p (rtx)%s->fld[0].rtint\" is a possible string to be a
550 member of gud-commands."
553 (if arg
(setq comm
(nth arg gud-commands
)))
554 (setq addr
(gud-read-address))
555 (if (eq (current-buffer) current-gud-buffer
)
559 (if (stringp comm
) (format comm addr
) (funcall comm addr
))))
560 (t (setq comm addr
)))
561 (switch-to-buffer current-gud-buffer
)
562 (goto-char (point-max))
563 (insert-string comm
)))