1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Keywords: unix, tools
9 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
30 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
31 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
32 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
33 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
34 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
35 ;; wrote the GDB command completion code.
42 ;; ======================================================================
43 ;; GUD commands must be visible in C buffers visited by GUD
45 (defvar gud-key-prefix
"\C-x\C-a"
46 "Prefix of all GUD commands valid in C buffers.")
48 (global-set-key (concat gud-key-prefix
"\C-l") 'gud-refresh
)
49 (global-set-key "\C-x " 'gud-break
) ;; backward compatibility hack
51 ;; ======================================================================
52 ;; the overloading mechanism
54 (defun gud-overload-functions (gud-overload-alist)
55 "Overload functions defined in GUD-OVERLOAD-ALIST.
56 This association list has elements of the form
57 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
59 (function (lambda (p) (fset (car p
) (symbol-function (cdr p
)))))
62 (defun gud-massage-args (file args
)
63 (error "GUD not properly entered."))
65 (defun gud-marker-filter (str)
66 (error "GUD not properly entered."))
68 (defun gud-find-file (f)
69 (error "GUD not properly entered."))
71 ;; ======================================================================
74 ;; This macro is used below to define some basic debugger interface commands.
75 ;; Of course you may use `gud-def' with any other debugger command, including
78 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
79 ;; which defines FUNC to send the command NAME to the debugger, gives
80 ;; it the docstring DOC, and binds that function to KEY in the GUD
81 ;; major mode. The function is also bound in the global keymap with the
84 (defmacro gud-def
(func cmd key
&optional doc
)
85 "Define FUNC to be a command sending STR and bound to KEY, with
86 optional doc string DOC. Certain %-escapes in the string arguments
87 are interpreted specially if present. These are:
89 %f name (without directory) of current source file.
90 %d directory of current source file.
91 %l number of current source line
92 %e text of the C lvalue or function-call expression surrounding point.
93 %a text of the hexadecimal address surrounding point
94 %p prefix argument to the command (if any) as a number
96 The `current' source file is the file of the current buffer (if
97 we're in a C file) or the source file current at the last break or
98 step (if we're in the GUD buffer).
99 The `current' line is that of the current buffer (if we're in a
100 source file) or the source line number at the last break or step (if
101 we're in the GUD buffer)."
103 (list 'defun
func '(arg)
106 (list 'gud-call cmd
'arg
))
113 (list 'global-set-key
114 (list 'concat
'gud-key-prefix key
)
115 (list 'quote func
)))))
117 ;; Where gud-display-frame should put the debugging arrow. This is
118 ;; set by the marker-filter, which scans the debugger's output for
119 ;; indications of the current program counter.
120 (defvar gud-last-frame nil
)
122 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
123 ;; the last frame, even if it's been called before and gud-last-frame has
125 (defvar gud-last-last-frame nil
)
127 ;; All debugger-specific information is collected here.
128 ;; Here's how it works, in case you ever need to add a debugger to the mode.
130 ;; Each entry must define the following at startup:
133 ;; comint-prompt-regexp
134 ;; gud-<name>-massage-args
135 ;; gud-<name>-marker-filter
136 ;; gud-<name>-find-file
138 ;; The job of the massage-args method is to modify the given list of
139 ;; debugger arguments before running the debugger.
141 ;; The job of the marker-filter method is to detect file/line markers in
142 ;; strings and set the global gud-last-frame to indicate what display
143 ;; action (if any) should be triggered by the marker. Note that only
144 ;; whatever the method *returns* is displayed in the buffer; thus, you
145 ;; can filter the debugger's output, interpreting some and passing on
148 ;; The job of the find-file method is to visit and return the buffer indicated
149 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
152 ;; ======================================================================
155 ;;; History of argument lists passed to gdb.
156 (defvar gud-gdb-history nil
)
158 (defun gud-gdb-massage-args (file args
)
159 (cons "-fullname" (cons file args
)))
161 ;; There's no guarantee that Emacs will hand the filter the entire
162 ;; marker at once; it could be broken up across several strings. We
163 ;; might even receive a big chunk with several markers in it. If we
164 ;; receive a chunk of text which looks like it might contain the
165 ;; beginning of a marker, we save it here between calls to the
167 (defvar gud-gdb-marker-acc
"")
169 (defun gud-gdb-marker-filter (string)
171 (setq gud-gdb-marker-acc
(concat gud-gdb-marker-acc string
))
174 ;; Process all the complete markers in this chunk.
175 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
179 ;; Extract the frame position from the marker.
181 (cons (substring gud-gdb-marker-acc
(match-beginning 1) (match-end 1))
182 (string-to-int (substring gud-gdb-marker-acc
186 ;; Append any text before the marker to the output we're going
187 ;; to return - we don't include the marker in this text.
188 output
(concat output
189 (substring gud-gdb-marker-acc
0 (match-beginning 0)))
191 ;; Set the accumulator to the remaining text.
192 gud-gdb-marker-acc
(substring gud-gdb-marker-acc
(match-end 0))))
194 ;; Does the remaining text look like it might end with the
195 ;; beginning of another marker? If it does, then keep it in
196 ;; gud-gdb-marker-acc until we receive the rest of it. Since we
197 ;; know the full marker regexp above failed, it's pretty simple to
198 ;; test for marker starts.
199 (if (string-match "^\032.*\\'" gud-gdb-marker-acc
)
201 ;; Everything before the potential marker start can be output.
202 (setq output
(concat output
(substring gud-gdb-marker-acc
203 0 (match-beginning 0))))
205 ;; Everything after, we save, to combine with later input.
206 (setq gud-gdb-marker-acc
207 (substring gud-gdb-marker-acc
(match-beginning 0))))
209 (setq output
(concat output gud-gdb-marker-acc
)
210 gud-gdb-marker-acc
""))
214 (defun gud-gdb-find-file (f)
215 (find-file-noselect f
))
217 (defvar gdb-minibuffer-local-map nil
218 "Keymap for minibuffer prompting of gdb startup command.")
219 (if gdb-minibuffer-local-map
221 (setq gdb-minibuffer-local-map
(copy-keymap minibuffer-local-map
))
223 gdb-minibuffer-local-map
"\C-i" 'comint-dynamic-complete-filename
))
226 (defun gdb (command-line)
227 "Run gdb on program FILE in buffer *gud-FILE*.
228 The directory containing FILE becomes the initial working directory
229 and source-file directory for your debugger."
231 (list (read-from-minibuffer "Run gdb (like this): "
232 (if (consp gud-gdb-history
)
233 (car gud-gdb-history
)
235 gdb-minibuffer-local-map nil
236 '(gud-gdb-history .
1))))
237 (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args
)
238 (gud-marker-filter . gud-gdb-marker-filter
)
239 (gud-find-file . gud-gdb-find-file
)
242 (gud-common-init command-line
)
244 (gud-def gud-break
"break %f:%l" "\C-b" "Set breakpoint at current line.")
245 (gud-def gud-tbreak
"tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
246 (gud-def gud-remove
"clear %l" "\C-d" "Remove breakpoint at current line")
247 (gud-def gud-step
"step %p" "\C-s" "Step one source line with display.")
248 (gud-def gud-stepi
"stepi %p" "\C-i" "Step one instruction with display.")
249 (gud-def gud-next
"next %p" "\C-n" "Step one line (skip functions).")
250 (gud-def gud-cont
"cont" "\C-r" "Continue with display.")
251 (gud-def gud-finish
"finish" "\C-f" "Finish executing current function.")
252 (gud-def gud-up
"up %p" "<" "Up N stack frames (numeric arg).")
253 (gud-def gud-down
"down %p" ">" "Down N stack frames (numeric arg).")
254 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
256 (local-set-key "\C-i" 'gud-gdb-complete-command
)
257 (setq comint-prompt-regexp
"^(.*gdb[+]?) *")
258 (setq paragraph-start comint-prompt-regexp
)
259 (run-hooks 'gdb-mode-hook
)
262 ;; One of the nice features of GDB is its impressive support for
263 ;; context-sensitive command completion. We preserve that feature
264 ;; in the GUD buffer by using a GDB command designed just for Emacs.
266 ;; The completion process filter indicates when it is finished.
267 (defvar gud-gdb-complete-in-progress
)
269 ;; Since output may arrive in fragments we accumulate partials strings here.
270 (defvar gud-gdb-complete-string
)
272 ;; We need to know how much of the completion to chop off.
273 (defvar gud-gdb-complete-break
)
275 ;; The completion list is constructed by the process filter.
276 (defvar gud-gdb-complete-list
)
278 (defvar gud-comint-buffer nil
)
280 (defun gud-gdb-complete-command ()
281 "Perform completion on the GDB command preceding point.
282 This is implemented using the GDB `complete' command which isn't
283 available with older versions of GDB."
286 (command (save-excursion
288 (and (looking-at comint-prompt-regexp
)
289 (goto-char (match-end 0)))
290 (buffer-substring (point) end
)))
292 ;; Find the word break. This match will always succeed.
293 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command
)
294 (setq gud-gdb-complete-break
(match-beginning 2)
295 command-word
(substring command gud-gdb-complete-break
))
298 ;; Temporarily install our filter function.
299 (gud-overload-functions
300 '((gud-marker-filter . gud-gdb-complete-filter
)))
301 ;; Issue the command to GDB.
302 (gud-basic-call (concat "complete " command
))
303 (setq gud-gdb-complete-in-progress t
304 gud-gdb-complete-string nil
305 gud-gdb-complete-list nil
)
307 (while gud-gdb-complete-in-progress
308 (accept-process-output (get-buffer-process gud-comint-buffer
))))
309 ;; Restore the old filter function.
310 (gud-overload-functions '((gud-marker-filter . gud-gdb-marker-filter
))))
311 ;; Protect against old versions of GDB.
312 (and gud-gdb-complete-list
313 (string-match "^Undefined command: \"complete\""
314 (car gud-gdb-complete-list
))
315 (error "This version of GDB doesn't support the `complete' command."))
316 ;; Sort the list like readline.
317 (setq gud-gdb-complete-list
318 (sort gud-gdb-complete-list
(function string-lessp
)))
319 ;; Remove duplicates.
320 (let ((first gud-gdb-complete-list
)
321 (second (cdr gud-gdb-complete-list
)))
323 (if (string-equal (car first
) (car second
))
324 (setcdr first
(setq second
(cdr second
)))
326 second
(cdr second
)))))
327 ;; Let comint handle the rest.
328 (comint-dynamic-simple-complete command-word gud-gdb-complete-list
)))
330 ;; The completion process filter is installed temporarily to slurp the
331 ;; output of GDB up to the next prompt and build the completion list.
332 (defun gud-gdb-complete-filter (string)
333 (setq string
(concat gud-gdb-complete-string string
))
334 (while (string-match "\n" string
)
335 (setq gud-gdb-complete-list
336 (cons (substring string gud-gdb-complete-break
(match-beginning 0))
337 gud-gdb-complete-list
))
338 (setq string
(substring string
(match-end 0))))
339 (if (string-match comint-prompt-regexp string
)
341 (setq gud-gdb-complete-in-progress nil
)
344 (setq gud-gdb-complete-string string
)
348 ;; ======================================================================
351 ;;; History of argument lists passed to sdb.
352 (defvar gud-sdb-history nil
)
354 (defvar gud-sdb-needs-tags
(not (file-exists-p "/var"))
355 "If nil, we're on a System V Release 4 and don't need the tags hack.")
357 (defvar gud-sdb-lastfile nil
)
359 (defun gud-sdb-massage-args (file args
)
362 (defun gud-sdb-marker-filter (string)
364 ;; System V Release 3.2 uses this format
365 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
369 (substring string
(match-beginning 2) (match-end 2))
371 (substring string
(match-beginning 3) (match-end 3))))))
372 ;; System V Release 4.0
373 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
375 (setq gud-sdb-lastfile
376 (substring string
(match-beginning 2) (match-end 2))))
377 ((and gud-sdb-lastfile
(string-match "^\\([0-9]+\\):" string
))
382 (substring string
(match-beginning 1) (match-end 1))))))
384 (setq gud-sdb-lastfile nil
)))
387 (defun gud-sdb-find-file (f)
388 (if gud-sdb-needs-tags
389 (find-tag-noselect f
)
390 (find-file-noselect f
)))
393 (defun sdb (command-line)
394 "Run sdb on program FILE in buffer *gud-FILE*.
395 The directory containing FILE becomes the initial working directory
396 and source-file directory for your debugger."
398 (list (read-from-minibuffer "Run sdb (like this): "
399 (if (consp gud-sdb-history
)
400 (car gud-sdb-history
)
403 '(gud-sdb-history .
1))))
404 (if (and gud-sdb-needs-tags
405 (not (and (boundp 'tags-file-name
) (file-exists-p tags-file-name
))))
406 (error "The sdb support requires a valid tags table to work."))
407 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args
)
408 (gud-marker-filter . gud-sdb-marker-filter
)
409 (gud-find-file . gud-sdb-find-file
)
412 (gud-common-init command-line
)
414 (gud-def gud-break
"%l b" "\C-b" "Set breakpoint at current line.")
415 (gud-def gud-tbreak
"%l c" "\C-t" "Set temporary breakpoint at current line.")
416 (gud-def gud-remove
"%l d" "\C-d" "Remove breakpoint at current line")
417 (gud-def gud-step
"s %p" "\C-s" "Step one source line with display.")
418 (gud-def gud-stepi
"i %p" "\C-i" "Step one instruction with display.")
419 (gud-def gud-next
"S %p" "\C-n" "Step one line (skip functions).")
420 (gud-def gud-cont
"c" "\C-r" "Continue with display.")
421 (gud-def gud-print
"%e/" "\C-p" "Evaluate C expression at point.")
423 (setq comint-prompt-regexp
"\\(^\\|\n\\)\\*")
424 (setq paragraph-start comint-prompt-regexp
)
425 (run-hooks 'sdb-mode-hook
)
428 ;; ======================================================================
431 ;;; History of argument lists passed to dbx.
432 (defvar gud-dbx-history nil
)
434 (defun gud-dbx-massage-args (file args
)
437 (defun gud-dbx-marker-filter (string)
438 (if (or (string-match
439 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
442 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
446 (substring string
(match-beginning 2) (match-end 2))
448 (substring string
(match-beginning 1) (match-end 1))))))
451 ;; Functions for dbx on Mips/Ultrix.
452 ;; This is very similar to the code for gdb. The trick is to start dbx
453 ;; with the (undocumented) option `-emacs'.
455 ;; Are we running on a Mips system under Ultrix?
456 (defvar gud-dbx-mips-p
(file-exists-p "/usr/include/mips"))
458 (defun gud-mipsdbx-massage-args (file args
)
459 (cons "-emacs" (cons file args
)))
461 ;; There's no guarantee that Emacs will hand the filter the entire
462 ;; marker at once; it could be broken up across several strings. We
463 ;; might even receive a big chunk with several markers in it. If we
464 ;; receive a chunk of text which looks like it might contain the
465 ;; beginning of a marker, we save it here between calls to the
467 (defvar gud-mipsdbx-marker-acc
"")
469 (defun gud-mipsdbx-marker-filter (string)
471 (setq gud-mipsdbx-marker-acc
(concat gud-mipsdbx-marker-acc string
))
474 ;; Process all the complete markers in this chunk.
476 "^[] [0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
477 gud-mipsdbx-marker-acc
)
480 ;; Extract the frame position from the marker.
482 (substring gud-mipsdbx-marker-acc
483 (match-beginning 1) (match-end 1))
485 (substring gud-mipsdbx-marker-acc
486 (match-beginning 2) (match-end 2))))
488 ;; Append any text before the marker to the output we're going
489 ;; to return - we don't include the marker in this text.
490 output
(concat output
(substring gud-mipsdbx-marker-acc
491 0 (match-beginning 0)))
493 ;; Set the accumulator to the remaining text.
494 gud-mipsdbx-marker-acc
(substring gud-mipsdbx-marker-acc
497 ;; Does the remaining text look like it might end with the
498 ;; beginning of another marker? If it does, then keep it in
499 ;; gud-mipsdbx-marker-acc until we receive the rest of it.
500 ;; Since we know the full marker regexp above failed, it's pretty
501 ;; simple to test for marker starts.
502 (if (string-match "^[] [0-9]*\032.*\\'" gud-mipsdbx-marker-acc
)
504 ;; Everything before the potential marker start can be output.
505 output
(concat output
(substring gud-mipsdbx-marker-acc
506 0 (match-beginning 0)))
507 ;; Everything after, we save, to combine with later input.
508 gud-mipsdbx-marker-acc
(substring gud-mipsdbx-marker-acc
509 (match-beginning 0)))
510 (setq output
(concat output gud-mipsdbx-marker-acc
)
511 gud-mipsdbx-marker-acc
""))
515 (defun gud-dbx-find-file (f)
516 (find-file-noselect f
))
519 (defun dbx (command-line)
520 "Run dbx on program FILE in buffer *gud-FILE*.
521 The directory containing FILE becomes the initial working directory
522 and source-file directory for your debugger."
524 (list (read-from-minibuffer "Run dbx (like this): "
525 (if (consp gud-dbx-history
)
526 (car gud-dbx-history
)
529 '(gud-dbx-history .
1))))
531 (gud-overload-functions
534 '((gud-massage-args . gud-mipsdbx-massage-args
)
535 (gud-marker-filter . gud-mipsdbx-marker-filter
)
536 (gud-find-file . gud-dbx-find-file
)))
538 '((gud-massage-args . gud-dbx-massage-args
)
539 (gud-marker-filter . gud-dbx-marker-filter
)
540 (gud-find-file . gud-dbx-find-file
)))))
542 (gud-common-init command-line
)
546 (gud-def gud-break
"stop at \"%f\":%l"
547 "\C-b" "Set breakpoint at current line.")
548 (gud-def gud-finish
"return" "\C-f" "Finish executing current function."))
550 (gud-def gud-break
"file \"%d%f\"\nstop at %l"
551 "\C-b" "Set breakpoint at current line.")))
553 (gud-def gud-remove
"clear %l" "\C-d" "Remove breakpoint at current line")
554 (gud-def gud-step
"step %p" "\C-s" "Step one line with display.")
555 (gud-def gud-stepi
"stepi %p" "\C-i" "Step one instruction with display.")
556 (gud-def gud-next
"next %p" "\C-n" "Step one line (skip functions).")
557 (gud-def gud-cont
"cont" "\C-r" "Continue with display.")
558 (gud-def gud-up
"up %p" "<" "Up (numeric arg) stack frames.")
559 (gud-def gud-down
"down %p" ">" "Down (numeric arg) stack frames.")
560 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
562 (setq comint-prompt-regexp
"^[^)\n]*dbx) *")
563 (setq paragraph-start comint-prompt-regexp
)
564 (run-hooks 'dbx-mode-hook
)
567 ;; ======================================================================
568 ;; xdb (HP PARISC debugger) functions
570 ;;; History of argument lists passed to xdb.
571 (defvar gud-xdb-history nil
)
573 (defvar gud-xdb-directories nil
574 "*A list of directories that xdb should search for source code.
575 If nil, only source files in the program directory
576 will be known to xdb.
578 The file names should be absolute, or relative to the directory
579 containing the executable being debugged.")
581 (defun gud-xdb-massage-args (file args
)
582 (nconc (let ((directories gud-xdb-directories
)
585 (setq result
(cons (car directories
) (cons "-d" result
)))
586 (setq directories
(cdr directories
)))
587 (nreverse (cons file result
)))
590 (defun gud-xdb-file-name (f)
591 "Transform a relative pathname to a full pathname in xdb mode"
593 (if (file-exists-p f
)
594 (setq result
(expand-file-name f
))
595 (let ((directories gud-xdb-directories
))
597 (let ((path (concat (car directories
) "/" f
)))
598 (if (file-exists-p path
)
599 (setq result
(expand-file-name path
)
601 (setq directories
(cdr directories
)))))
604 ;; xdb does not print the lines all at once, so we have to accumulate them
605 (defvar gud-xdb-accumulation
"")
607 (defun gud-xdb-marker-filter (string)
609 (if (or (string-match comint-prompt-regexp string
)
610 (string-match ".*\012" string
))
611 (setq result
(concat gud-xdb-accumulation string
)
612 gud-xdb-accumulation
"")
613 (setq gud-xdb-accumulation
(concat gud-xdb-accumulation string
)))
615 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result
)
616 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
618 (let ((line (string-to-int
619 (substring result
(match-beginning 2) (match-end 2))))
620 (file (gud-xdb-file-name
621 (substring result
(match-beginning 1) (match-end 1)))))
623 (setq gud-last-frame
(cons file line
))))))
626 (defun gud-xdb-find-file (f)
627 (let ((realf (gud-xdb-file-name f
)))
628 (if realf
(find-file-noselect realf
))))
631 (defun xdb (command-line)
632 "Run xdb on program FILE in buffer *gud-FILE*.
633 The directory containing FILE becomes the initial working directory
634 and source-file directory for your debugger.
636 You can set the variable 'gud-xdb-directories' to a list of program source
637 directories if your program contains sources from more than one directory."
639 (list (read-from-minibuffer "Run xdb (like this): "
640 (if (consp gud-xdb-history
)
641 (car gud-xdb-history
)
644 '(gud-xdb-history .
1))))
645 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args
)
646 (gud-marker-filter . gud-xdb-marker-filter
)
647 (gud-find-file . gud-xdb-find-file
)))
649 (gud-common-init command-line
)
651 (gud-def gud-break
"b %f:%l" "\C-b" "Set breakpoint at current line.")
652 (gud-def gud-tbreak
"b %f:%l\\t" "\C-t"
653 "Set temporary breakpoint at current line.")
654 (gud-def gud-remove
"db" "\C-d" "Remove breakpoint at current line")
655 (gud-def gud-step
"s %p" "\C-s" "Step one line with display.")
656 (gud-def gud-next
"S %p" "\C-n" "Step one line (skip functions).")
657 (gud-def gud-cont
"c" "\C-r" "Continue with display.")
658 (gud-def gud-up
"up %p" "<" "Up (numeric arg) stack frames.")
659 (gud-def gud-down
"down %p" ">" "Down (numeric arg) stack frames.")
660 (gud-def gud-finish
"bu\\t" "\C-f" "Finish executing current function.")
661 (gud-def gud-print
"p %e" "\C-p" "Evaluate C expression at point.")
663 (setq comint-prompt-regexp
"^>")
664 (setq paragraph-start comint-prompt-regexp
)
665 (make-local-variable 'gud-xdb-accumulation
)
666 (setq gud-xdb-accumulation
"")
667 (run-hooks 'xdb-mode-hook
))
669 ;; ======================================================================
672 ;;; History of argument lists passed to perldb.
673 (defvar gud-perldb-history nil
)
675 (defun gud-perldb-massage-args (file args
)
676 (cons "-d" (cons file
(cons "-emacs" args
))))
678 ;; There's no guarantee that Emacs will hand the filter the entire
679 ;; marker at once; it could be broken up across several strings. We
680 ;; might even receive a big chunk with several markers in it. If we
681 ;; receive a chunk of text which looks like it might contain the
682 ;; beginning of a marker, we save it here between calls to the
684 (defvar gud-perldb-marker-acc
"")
686 (defun gud-perldb-marker-filter (string)
688 (setq gud-perldb-marker-acc
(concat gud-perldb-marker-acc string
))
691 ;; Process all the complete markers in this chunk.
692 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
693 gud-perldb-marker-acc
)
696 ;; Extract the frame position from the marker.
698 (cons (substring gud-perldb-marker-acc
(match-beginning 1) (match-end 1))
699 (string-to-int (substring gud-perldb-marker-acc
703 ;; Append any text before the marker to the output we're going
704 ;; to return - we don't include the marker in this text.
705 output
(concat output
706 (substring gud-perldb-marker-acc
0 (match-beginning 0)))
708 ;; Set the accumulator to the remaining text.
709 gud-perldb-marker-acc
(substring gud-perldb-marker-acc
(match-end 0))))
711 ;; Does the remaining text look like it might end with the
712 ;; beginning of another marker? If it does, then keep it in
713 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
714 ;; know the full marker regexp above failed, it's pretty simple to
715 ;; test for marker starts.
716 (if (string-match "^\032.*\\'" gud-perldb-marker-acc
)
718 ;; Everything before the potential marker start can be output.
719 (setq output
(concat output
(substring gud-perldb-marker-acc
720 0 (match-beginning 0))))
722 ;; Everything after, we save, to combine with later input.
723 (setq gud-perldb-marker-acc
724 (substring gud-perldb-marker-acc
(match-beginning 0))))
726 (setq output
(concat output gud-perldb-marker-acc
)
727 gud-perldb-marker-acc
""))
731 (defun gud-perldb-find-file (f)
732 (find-file-noselect f
))
735 (defun perldb (command-line)
736 "Run perldb on program FILE in buffer *gud-FILE*.
737 The directory containing FILE becomes the initial working directory
738 and source-file directory for your debugger."
740 (list (read-from-minibuffer "Run perldb (like this): "
741 (if (consp gud-perldb-history
)
742 (car gud-perldb-history
)
745 '(gud-perldb-history .
1))))
746 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args
)
747 (gud-marker-filter . gud-perldb-marker-filter
)
748 (gud-find-file . gud-perldb-find-file
)
751 (gud-common-init command-line
)
753 (gud-def gud-break
"b %l" "\C-b" "Set breakpoint at current line.")
754 (gud-def gud-remove
"d %l" "\C-d" "Remove breakpoint at current line")
755 (gud-def gud-step
"s" "\C-s" "Step one source line with display.")
756 (gud-def gud-next
"n" "\C-n" "Step one line (skip functions).")
757 (gud-def gud-cont
"c" "\C-r" "Continue with display.")
758 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
759 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
760 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
761 (gud-def gud-print
"%e" "\C-p" "Evaluate perl expression at point.")
763 (setq comint-prompt-regexp
"^ DB<[0-9]+> ")
764 (setq paragraph-start comint-prompt-regexp
)
765 (run-hooks 'perldb-mode-hook
)
769 ;; End of debugger-specific information
773 ;;; When we send a command to the debugger via gud-call, it's annoying
774 ;;; to see the command and the new prompt inserted into the debugger's
775 ;;; buffer; we have other ways of knowing the command has completed.
777 ;;; If the buffer looks like this:
778 ;;; --------------------
779 ;;; (gdb) set args foo bar
781 ;;; --------------------
782 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
783 ;;; source file to set a breakpoint, we want the buffer to end up like
785 ;;; --------------------
786 ;;; (gdb) set args foo bar
787 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
789 ;;; --------------------
790 ;;; Essentially, the old prompt is deleted, and the command's output
791 ;;; and the new prompt take its place.
793 ;;; Not echoing the command is easy enough; you send it directly using
794 ;;; process-send-string, and it never enters the buffer. However,
795 ;;; getting rid of the old prompt is trickier; you don't want to do it
796 ;;; when you send the command, since that will result in an annoying
797 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
798 ;;; waits for a response from the debugger, and the new prompt is
799 ;;; inserted. Instead, we'll wait until we actually get some output
800 ;;; from the subprocess before we delete the prompt. If the command
801 ;;; produced no output other than a new prompt, that prompt will most
802 ;;; likely be in the first chunk of output received, so we will delete
803 ;;; the prompt and then replace it with an identical one. If the
804 ;;; command produces output, the prompt is moving anyway, so the
805 ;;; flicker won't be annoying.
807 ;;; So - when we want to delete the prompt upon receipt of the next
808 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
809 ;;; the start of the prompt; the process filter will notice this, and
810 ;;; delete all text between it and the process output marker. If
811 ;;; gud-delete-prompt-marker points nowhere, we leave the current
813 (defvar gud-delete-prompt-marker nil
)
817 "Major mode for interacting with an inferior debugger process.
819 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
820 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
821 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
823 After startup, the following commands are available in both the GUD
824 interaction buffer and any source buffer GUD visits due to a breakpoint stop
827 \\[gud-break] sets a breakpoint at the current file and line. In the
828 GUD buffer, the current file and line are those of the last breakpoint or
829 step. In a source buffer, they are the buffer's file and current line.
831 \\[gud-remove] removes breakpoints on the current file and line.
833 \\[gud-refresh] displays in the source window the last line referred to
836 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
837 step-one-line (not entering function calls), and step-one-instruction
838 and then update the source window with the current file and position.
839 \\[gud-cont] continues execution.
841 \\[gud-print] tries to find the largest C lvalue or function-call expression
842 around point, and sends it to the debugger for value display.
844 The above commands are common to all supported debuggers except xdb which
845 does not support stepping instructions.
847 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
848 except that the breakpoint is temporary; that is, it is removed when
849 execution stops on it.
851 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
852 frame. \\[gud-down] drops back down through one.
854 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
855 the current function and stops.
857 All the keystrokes above are accessible in the GUD buffer
858 with the prefix C-c, and in all buffers through the prefix C-x C-a.
860 All pre-defined functions for which the concept make sense repeat
861 themselves the appropriate number of times if you give a prefix
864 You may use the `gud-def' macro in the initialization hook to define other
867 Other commands for interacting with the debugger process are inherited from
868 comint mode, which see."
871 (setq major-mode
'gud-mode
)
872 (setq mode-name
"Debugger")
873 (setq mode-line-process
'(":%s"))
874 (use-local-map (copy-keymap comint-mode-map
))
875 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh
)
876 (make-local-variable 'gud-last-frame
)
877 (setq gud-last-frame nil
)
878 (make-local-variable 'comint-prompt-regexp
)
879 (make-local-variable 'paragraph-start
)
880 (make-local-variable 'gud-delete-prompt-marker
)
881 (setq gud-delete-prompt-marker
(make-marker))
882 (run-hooks 'gud-mode-hook
)
885 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
886 (defun gud-chop-words (string)
888 (len (length string
))
891 (if (memq (aref string i
) '(?
\t ?
))
893 (setq words
(cons (substring string beg i
) words
)
895 (while (and (< beg len
) (memq (aref string beg
) '(?
\t ?
)))
900 (setq words
(cons (substring string beg
) words
)))
903 ;; Perform initializations common to all debuggers.
904 (defun gud-common-init (command-line)
905 (let* ((words (gud-chop-words command-line
))
906 (program (car words
))
907 (file-word (let ((w (cdr words
)))
908 (while (and w
(= ?-
(aref (car w
) 0)))
911 (args (delq file-word
(cdr words
)))
913 (expand-file-name (substitute-in-file-name file-word
))))
914 (filepart (and file-word
(file-name-nondirectory file
))))
915 (switch-to-buffer (concat "*gud-" filepart
"*"))
916 (and file-word
(setq default-directory
(file-name-directory file
)))
917 (or (bolp) (newline))
918 (insert "Current directory is " default-directory
"\n")
919 (apply 'make-comint
(concat "gud-" filepart
) program nil
920 (if file-word
(gud-massage-args file args
))))
922 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter
)
923 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel
)
927 (defun gud-set-buffer ()
928 (cond ((eq major-mode
'gud-mode
)
929 (setq gud-comint-buffer
(current-buffer)))))
931 ;; These functions are responsible for inserting output from your debugger
932 ;; into the buffer. The hard work is done by the method that is
933 ;; the value of gud-marker-filter.
935 (defun gud-filter (proc string
)
936 ;; Here's where the actual buffer insertion is done
937 (let ((inhibit-quit t
)
940 (set-buffer (process-buffer proc
))
941 ;; If we have been so requested, delete the debugger prompt.
942 (if (marker-buffer gud-delete-prompt-marker
)
944 (delete-region (process-mark proc
) gud-delete-prompt-marker
)
945 (set-marker gud-delete-prompt-marker nil
)))
946 ;; Save the process output, checking for source file markers.
947 (setq output
(gud-marker-filter string
))
948 ;; Check for a filename-and-line number.
949 ;; Don't display the specified file
950 ;; unless (1) point is at or after the position where output appears
951 ;; and (2) this buffer is on the screen.
952 (if (and gud-last-frame
953 (>= (point) (process-mark proc
))
954 (get-buffer-window (current-buffer)))
956 ;; Let the comint filter do the actual insertion.
957 ;; That lets us inherit various comint features.
958 (comint-output-filter proc output
))))
960 (defun gud-sentinel (proc msg
)
961 (cond ((null (buffer-name (process-buffer proc
)))
963 ;; Stop displaying an arrow in a source file.
964 (setq overlay-arrow-position nil
)
965 (set-process-buffer proc nil
))
966 ((memq (process-status proc
) '(signal exit
))
967 ;; Stop displaying an arrow in a source file.
968 (setq overlay-arrow-position nil
)
969 ;; Fix the mode line.
970 (setq mode-line-process
972 (symbol-name (process-status proc
))))
973 (let* ((obuf (current-buffer)))
974 ;; save-excursion isn't the right thing if
975 ;; process-buffer is current-buffer
978 ;; Write something in *compilation* and hack its mode line,
979 (set-buffer (process-buffer proc
))
980 ;; Force mode line redisplay soon
981 (set-buffer-modified-p (buffer-modified-p))
983 (insert ?
\n mode-name
" " msg
)
985 (goto-char (point-max))
986 (insert ?
\n mode-name
" " msg
)))
987 ;; If buffer and mode line will show that the process
988 ;; is dead, we can delete it now. Otherwise it
989 ;; will stay around until M-x list-processes.
990 (delete-process proc
))
991 ;; Restore old buffer, but don't restore old point
992 ;; if obuf is the gud buffer.
993 (set-buffer obuf
))))))
995 (defun gud-display-frame ()
996 "Find and obey the last filename-and-line marker from the debugger.
997 Obeying it means displaying in another window the specified file and line."
1002 (gud-display-line (car gud-last-frame
) (cdr gud-last-frame
))
1003 (setq gud-last-last-frame gud-last-frame
1004 gud-last-frame nil
))))
1006 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1007 ;; and that its line LINE is visible.
1008 ;; Put the overlay-arrow on the line LINE in that buffer.
1009 ;; Most of the trickiness in here comes from wanting to preserve the current
1010 ;; region-restriction if that's possible. We use an explicit display-buffer
1011 ;; to get around the fact that this is called inside a save-excursion.
1013 (defun gud-display-line (true-file line
)
1014 (let* ((buffer (gud-find-file true-file
))
1015 (window (display-buffer buffer
))
1017 ;;; (if (equal buffer (current-buffer))
1019 ;;; (setq buffer-read-only nil))
1021 ;;; (setq buffer-read-only t)
1027 (setq overlay-arrow-string
"=>")
1028 (or overlay-arrow-position
1029 (setq overlay-arrow-position
(make-marker)))
1030 (set-marker overlay-arrow-position
(point) (current-buffer)))
1031 (cond ((or (< pos
(point-min)) (> pos
(point-max)))
1034 (set-window-point window overlay-arrow-position
)))
1036 ;;; The gud-call function must do the right thing whether its invoking
1037 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1038 ;;; or a C buffer. In the former case, we want to supply data from
1039 ;;; gud-last-frame. Here's how we do it:
1041 (defun gud-format-command (str arg
)
1042 (let ((insource (not (eq (current-buffer) gud-comint-buffer
)))
1043 (frame (or gud-last-frame gud-last-last-frame
))
1045 (while (and str
(string-match "\\([^%]*\\)%\\([adeflp]\\)" str
))
1046 (let ((key (string-to-char (substring str
(match-beginning 2))))
1050 (setq subst
(file-name-nondirectory (if insource
1054 (setq subst
(file-name-directory (if insource
1058 (setq subst
(if insource
1061 (save-restriction (widen)
1062 (1+ (count-lines 1 (point)))))
1065 (setq subst
(find-c-expr)))
1067 (setq subst
(gud-read-address)))
1069 (setq subst
(if arg
(int-to-string arg
) ""))))
1070 (setq result
(concat result
1071 (substring str
(match-beginning 1) (match-end 1))
1073 (setq str
(substring str
(match-end 2))))
1074 ;; There might be text left in STR when the loop ends.
1075 (concat result str
)))
1077 (defun gud-read-address ()
1078 "Return a string containing the core-address found in the buffer at point."
1080 (let ((pt (point)) found begin
)
1081 (setq found
(if (search-backward "0x" (- pt
7) t
) (point)))
1083 (found (forward-char 2)
1084 (buffer-substring found
1085 (progn (re-search-forward "[^0-9a-f]")
1088 (t (setq begin
(progn (re-search-backward "[^0-9]")
1092 (re-search-forward "[^0-9]")
1094 (buffer-substring begin
(point)))))))
1096 (defun gud-call (fmt &optional arg
)
1097 (let ((msg (gud-format-command fmt arg
)))
1098 (message "Command: %s" msg
)
1100 (gud-basic-call msg
)))
1102 (defun gud-basic-call (command)
1103 "Invoke the debugger COMMAND displaying source in other window."
1106 (let ((command (concat command
"\n"))
1107 (proc (get-buffer-process gud-comint-buffer
)))
1109 ;; Arrange for the current prompt to get deleted.
1111 (set-buffer gud-comint-buffer
)
1112 (goto-char (process-mark proc
))
1114 (if (looking-at comint-prompt-regexp
)
1115 (set-marker gud-delete-prompt-marker
(point))))
1116 (process-send-string proc command
)))
1118 (defun gud-refresh (&optional arg
)
1119 "Fix up a possibly garbled display, and redraw the arrow."
1122 (or gud-last-frame
(setq gud-last-frame gud-last-last-frame
))
1123 (gud-display-frame))
1125 ;;; Code for parsing expressions out of C code. The single entry point is
1126 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1128 ;;; The rest of this file is a hacked version of gdbsrc.el by
1129 ;;; Debby Ayers <ayers@asc.slb.com>,
1130 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1132 (defun find-c-expr ()
1133 "Returns the C expr that surrounds point."
1136 (let ((p) (expr) (test-expr))
1138 (setq expr
(expr-cur))
1139 (setq test-expr
(expr-prev))
1140 (while (expr-compound test-expr expr
)
1141 (setq expr
(cons (car test-expr
) (cdr expr
)))
1142 (goto-char (car expr
))
1143 (setq test-expr
(expr-prev)))
1145 (setq test-expr
(expr-next))
1146 (while (expr-compound expr test-expr
)
1147 (setq expr
(cons (car expr
) (cdr test-expr
)))
1148 (setq test-expr
(expr-next))
1150 (buffer-substring (car expr
) (cdr expr
)))))
1153 "Returns the expr that point is in; point is set to beginning of expr.
1154 The expr is represented as a cons cell, where the car specifies the point in
1155 the current buffer that marks the beginning of the expr and the cdr specifies
1156 the character after the end of the expr."
1157 (let ((p (point)) (begin) (end))
1158 (expr-backward-sexp)
1159 (setq begin
(point))
1173 (defun expr-backward-sexp ()
1174 "Version of `backward-sexp' that catches errors."
1179 (defun expr-forward-sexp ()
1180 "Version of `forward-sexp' that catches errors."
1186 "Returns the previous expr, point is set to beginning of that expr.
1187 The expr is represented as a cons cell, where the car specifies the point in
1188 the current buffer that marks the beginning of the expr and the cdr specifies
1189 the character after the end of the expr"
1190 (let ((begin) (end))
1191 (expr-backward-sexp)
1192 (setq begin
(point))
1199 "Returns the following expr, point is set to beginning of that expr.
1200 The expr is represented as a cons cell, where the car specifies the point in
1201 the current buffer that marks the beginning of the expr and the cdr specifies
1202 the character after the end of the expr."
1203 (let ((begin) (end))
1207 (expr-backward-sexp)
1208 (setq begin
(point))
1211 (defun expr-compound-sep (span-start span-end
)
1212 "Returns '.' for '->' & '.', returns ' ' for white space,
1213 returns '?' for other punctuation."
1216 (while (< span-start span-end
)
1217 (setq syntax
(char-syntax (char-after span-start
)))
1220 ((= syntax ?.
) (setq syntax
(char-after span-start
))
1222 ((= syntax ?.
) (setq result ?.
))
1223 ((and (= syntax ?-
) (= (char-after (+ span-start
1)) ?
>))
1225 (setq span-start
(+ span-start
1)))
1226 (t (setq span-start span-end
)
1227 (setq result ??
)))))
1228 (setq span-start
(+ span-start
1)))
1231 (defun expr-compound (first second
)
1232 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1233 The two exprs are represented as a cons cells, where the car
1234 specifies the point in the current buffer that marks the beginning of the
1235 expr and the cdr specifies the character after the end of the expr.
1236 Link exprs of the form:
1243 (let ((span-start (cdr first
))
1244 (span-end (car second
))
1246 (setq syntax
(expr-compound-sep span-start span-end
))
1248 ((= (car first
) (car second
)) nil
)
1249 ((= (cdr first
) (cdr second
)) nil
)
1252 (setq span-start
(char-after (- span-start
1)))
1253 (setq span-end
(char-after span-end
))
1255 ((= span-start ?
) ) t
)
1256 ((= span-start ?
] ) t
)
1257 ((= span-end ?
( ) t
)
1258 ((= span-end ?
[ ) t
)
1265 ;;; gud.el ends here