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).
41 ;; ======================================================================
42 ;; GUD commands must be visible in C buffers visited by GUD
44 (defvar gud-key-prefix "\C-x\C-a"
45 "Prefix of all GUD commands valid in C buffers.")
47 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
48 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
50 ;; ======================================================================
51 ;; the overloading mechanism
53 (defun gud-overload-functions (gud-overload-alist)
54 "Overload functions defined in GUD-OVERLOAD-ALIST.
55 This association list has elements of the form
56 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
58 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
61 (defun gud-massage-args (file args)
62 (error "GUD not properly entered."))
64 (defun gud-marker-filter (str)
65 (error "GUD not properly entered."))
67 (defun gud-find-file (f)
68 (error "GUD not properly entered."))
70 ;; ======================================================================
73 ;; This macro is used below to define some basic debugger interface commands.
74 ;; Of course you may use `gud-def' with any other debugger command, including
77 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
78 ;; which defines FUNC to send the command NAME to the debugger, gives
79 ;; it the docstring DOC, and binds that function to KEY in the GUD
80 ;; major mode. The function is also bound in the global keymap with the
83 (defmacro gud-def (func cmd key &optional doc)
84 "Define FUNC to be a command sending STR and bound to KEY, with
85 optional doc string DOC. Certain %-escapes in the string arguments
86 are interpreted specially if present. These are:
88 %f name (without directory) of current source file.
89 %d directory of current source file.
90 %l number of current source line
91 %e text of the C lvalue or function-call expression surrounding point.
92 %a text of the hexadecimal address surrounding point
93 %p prefix argument to the command (if any) as a number
95 The `current' source file is the file of the current buffer (if
96 we're in a C file) or the source file current at the last break or
97 step (if we're in the GUD buffer).
98 The `current' line is that of the current buffer (if we're in a
99 source file) or the source line number at the last break or step (if
100 we're in the GUD buffer)."
102 (list 'defun func '(arg)
105 (list 'gud-call cmd 'arg))
112 (list 'global-set-key
113 (list 'concat 'gud-key-prefix key)
114 (list 'quote func)))))
116 ;; Where gud-display-frame should put the debugging arrow. This is
117 ;; set by the marker-filter, which scans the debugger's output for
118 ;; indications of the current program counter.
119 (defvar gud-last-frame nil)
121 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
122 ;; the last frame, even if it's been called before and gud-last-frame has
124 (defvar gud-last-last-frame nil)
126 ;; All debugger-specific information is collected here.
127 ;; Here's how it works, in case you ever need to add a debugger to the mode.
129 ;; Each entry must define the following at startup:
132 ;; comint-prompt-regexp
133 ;; gud-<name>-massage-args
134 ;; gud-<name>-marker-filter
135 ;; gud-<name>-find-file
137 ;; The job of the massage-args method is to modify the given list of
138 ;; debugger arguments before running the debugger.
140 ;; The job of the marker-filter method is to detect file/line markers in
141 ;; strings and set the global gud-last-frame to indicate what display
142 ;; action (if any) should be triggered by the marker. Note that only
143 ;; whatever the method *returns* is displayed in the buffer; thus, you
144 ;; can filter the debugger's output, interpreting some and passing on
147 ;; The job of the find-file method is to visit and return the buffer indicated
148 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
151 ;; ======================================================================
154 ;;; History of argument lists passed to gdb.
155 (defvar gud-gdb-history nil)
157 (defun gud-gdb-massage-args (file args)
158 (cons "-fullname" (cons file args)))
160 ;; There's no guarantee that Emacs will hand the filter the entire
161 ;; marker at once; it could be broken up across several strings. We
162 ;; might even receive a big chunk with several markers in it. If we
163 ;; receive a chunk of text which looks like it might contain the
164 ;; beginning of a marker, we save it here between calls to the
166 (defvar gud-gdb-marker-acc "")
168 (defun gud-gdb-marker-filter (string)
170 (setq gud-gdb-marker-acc (concat gud-gdb-marker-acc string))
173 ;; Process all the complete markers in this chunk.
174 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
178 ;; Extract the frame position from the marker.
180 (cons (substring gud-gdb-marker-acc (match-beginning 1) (match-end 1))
181 (string-to-int (substring gud-gdb-marker-acc
185 ;; Append any text before the marker to the output we're going
186 ;; to return - we don't include the marker in this text.
187 output (concat output
188 (substring gud-gdb-marker-acc 0 (match-beginning 0)))
190 ;; Set the accumulator to the remaining text.
191 gud-gdb-marker-acc (substring gud-gdb-marker-acc (match-end 0))))
193 ;; Does the remaining text look like it might end with the
194 ;; beginning of another marker? If it does, then keep it in
195 ;; gud-gdb-marker-acc until we receive the rest of it. Since we
196 ;; know the full marker regexp above failed, it's pretty simple to
197 ;; test for marker starts.
198 (if (string-match "^\032.*\\'" gud-gdb-marker-acc)
200 ;; Everything before the potential marker start can be output.
201 (setq output (concat output (substring gud-gdb-marker-acc
202 0 (match-beginning 0))))
204 ;; Everything after, we save, to combine with later input.
205 (setq gud-gdb-marker-acc
206 (substring gud-gdb-marker-acc (match-beginning 0))))
208 (setq output (concat output gud-gdb-marker-acc)
209 gud-gdb-marker-acc ""))
213 (defun gud-gdb-find-file (f)
214 (find-file-noselect f))
217 (defun gdb (command-line)
218 "Run gdb on program FILE in buffer *gud-FILE*.
219 The directory containing FILE becomes the initial working directory
220 and source-file directory for your debugger."
222 (list (read-from-minibuffer "Run gdb (like this): "
223 (if (consp gud-gdb-history)
224 (car gud-gdb-history)
227 '(gud-gdb-history . 1))))
228 (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args)
229 (gud-marker-filter . gud-gdb-marker-filter)
230 (gud-find-file . gud-gdb-find-file)
233 (gud-common-init command-line)
235 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
236 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
237 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
238 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
239 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
240 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
241 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
242 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
243 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
244 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
245 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
247 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
248 (run-hooks 'gdb-mode-hook)
252 ;; ======================================================================
255 ;;; History of argument lists passed to sdb.
256 (defvar gud-sdb-history nil)
258 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
259 "If nil, we're on a System V Release 4 and don't need the tags hack.")
261 (defvar gud-sdb-lastfile nil)
263 (defun gud-sdb-massage-args (file args)
266 (defun gud-sdb-marker-filter (string)
268 ;; System V Release 3.2 uses this format
269 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
273 (substring string (match-beginning 2) (match-end 2))
275 (substring string (match-beginning 3) (match-end 3))))))
276 ;; System V Release 4.0
277 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
279 (setq gud-sdb-lastfile
280 (substring string (match-beginning 2) (match-end 2))))
281 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
286 (substring string (match-beginning 1) (match-end 1))))))
288 (setq gud-sdb-lastfile nil)))
291 (defun gud-sdb-find-file (f)
292 (if gud-sdb-needs-tags
293 (find-tag-noselect f)
294 (find-file-noselect f)))
297 (defun sdb (command-line)
298 "Run sdb on program FILE in buffer *gud-FILE*.
299 The directory containing FILE becomes the initial working directory
300 and source-file directory for your debugger."
302 (list (read-from-minibuffer "Run sdb (like this): "
303 (if (consp gud-sdb-history)
304 (car gud-sdb-history)
307 '(gud-sdb-history . 1))))
308 (if (and gud-sdb-needs-tags
309 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
310 (error "The sdb support requires a valid tags table to work."))
311 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
312 (gud-marker-filter . gud-sdb-marker-filter)
313 (gud-find-file . gud-sdb-find-file)
316 (gud-common-init command-line)
318 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
319 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
320 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
321 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
322 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
323 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
324 (gud-def gud-cont "c" "\C-r" "Continue with display.")
325 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
327 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
328 (run-hooks 'sdb-mode-hook)
331 ;; ======================================================================
334 ;;; History of argument lists passed to dbx.
335 (defvar gud-dbx-history nil)
337 (defun gud-dbx-massage-args (file args)
340 (defun gud-dbx-marker-filter (string)
341 (if (or (string-match
342 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
345 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
349 (substring string (match-beginning 2) (match-end 2))
351 (substring string (match-beginning 1) (match-end 1))))))
354 ;; Functions for dbx on Mips/Ultrix.
355 ;; This is very similar to the code for gdb. The trick is to start dbx
356 ;; with the (undocumented) option `-emacs'.
358 ;; Are we running on a Mips system under Ultrix?
359 (defvar gud-dbx-mips-p (file-exists-p "/usr/include/mips"))
361 (defun gud-mipsdbx-massage-args (file args)
362 (cons "-emacs" (cons file args)))
364 ;; There's no guarantee that Emacs will hand the filter the entire
365 ;; marker at once; it could be broken up across several strings. We
366 ;; might even receive a big chunk with several markers in it. If we
367 ;; receive a chunk of text which looks like it might contain the
368 ;; beginning of a marker, we save it here between calls to the
370 (defvar gud-mipsdbx-marker-acc "")
372 (defun gud-mipsdbx-marker-filter (string)
374 (setq gud-mipsdbx-marker-acc (concat gud-mipsdbx-marker-acc string))
377 ;; Process all the complete markers in this chunk.
379 "^[] [0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
380 gud-mipsdbx-marker-acc)
383 ;; Extract the frame position from the marker.
385 (substring gud-mipsdbx-marker-acc
386 (match-beginning 1) (match-end 1))
388 (substring gud-mipsdbx-marker-acc
389 (match-beginning 2) (match-end 2))))
391 ;; Append any text before the marker to the output we're going
392 ;; to return - we don't include the marker in this text.
393 output (concat output (substring gud-mipsdbx-marker-acc
394 0 (match-beginning 0)))
396 ;; Set the accumulator to the remaining text.
397 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
400 ;; Does the remaining text look like it might end with the
401 ;; beginning of another marker? If it does, then keep it in
402 ;; gud-mipsdbx-marker-acc until we receive the rest of it.
403 ;; Since we know the full marker regexp above failed, it's pretty
404 ;; simple to test for marker starts.
405 (if (string-match "^[] [0-9]*\032.*\\'" gud-mipsdbx-marker-acc)
407 ;; Everything before the potential marker start can be output.
408 output (concat output (substring gud-mipsdbx-marker-acc
409 0 (match-beginning 0)))
410 ;; Everything after, we save, to combine with later input.
411 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
412 (match-beginning 0)))
413 (setq output (concat output gud-mipsdbx-marker-acc)
414 gud-mipsdbx-marker-acc ""))
418 (defun gud-dbx-find-file (f)
419 (find-file-noselect f))
422 (defun dbx (command-line)
423 "Run dbx on program FILE in buffer *gud-FILE*.
424 The directory containing FILE becomes the initial working directory
425 and source-file directory for your debugger."
427 (list (read-from-minibuffer "Run dbx (like this): "
428 (if (consp gud-dbx-history)
429 (car gud-dbx-history)
432 '(gud-dbx-history . 1))))
434 (gud-overload-functions
437 '((gud-massage-args . gud-mipsdbx-massage-args)
438 (gud-marker-filter . gud-mipsdbx-marker-filter)
439 (gud-find-file . gud-dbx-find-file)))
441 '((gud-massage-args . gud-dbx-massage-args)
442 (gud-marker-filter . gud-dbx-marker-filter)
443 (gud-find-file . gud-dbx-find-file)))))
445 (gud-common-init command-line)
449 (gud-def gud-break "stop at \"%f\":%l"
450 "\C-b" "Set breakpoint at current line.")
451 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
453 (gud-def gud-break "file \"%d%f\"\nstop at %l"
454 "\C-b" "Set breakpoint at current line.")))
456 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
457 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
458 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
459 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
460 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
461 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
462 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
463 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
465 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
466 (run-hooks 'dbx-mode-hook)
469 ;; ======================================================================
470 ;; xdb (HP PARISC debugger) functions
472 ;;; History of argument lists passed to xdb.
473 (defvar gud-xdb-history nil)
475 (defvar gud-xdb-directories nil
476 "*A list of directories that xdb should search for source code.
477 If nil, only source files in the program directory
478 will be known to xdb.
480 The file names should be absolute, or relative to the directory
481 containing the executable being debugged.")
483 (defun gud-xdb-massage-args (file args)
484 (nconc (let ((directories gud-xdb-directories)
487 (setq result (cons (car directories) (cons "-d" result)))
488 (setq directories (cdr directories)))
489 (nreverse (cons file result)))
492 (defun gud-xdb-file-name (f)
493 "Transform a relative pathname to a full pathname in xdb mode"
495 (if (file-exists-p f)
496 (setq result (expand-file-name f))
497 (let ((directories gud-xdb-directories))
499 (let ((path (concat (car directories) "/" f)))
500 (if (file-exists-p path)
501 (setq result (expand-file-name path)
503 (setq directories (cdr directories)))))
506 ;; xdb does not print the lines all at once, so we have to accumulate them
507 (defvar gud-xdb-accumulation "")
509 (defun gud-xdb-marker-filter (string)
511 (if (or (string-match comint-prompt-regexp string)
512 (string-match ".*\012" string))
513 (setq result (concat gud-xdb-accumulation string)
514 gud-xdb-accumulation "")
515 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
517 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
518 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
520 (let ((line (string-to-int
521 (substring result (match-beginning 2) (match-end 2))))
522 (file (gud-xdb-file-name
523 (substring result (match-beginning 1) (match-end 1)))))
525 (setq gud-last-frame (cons file line))))))
528 (defun gud-xdb-find-file (f)
529 (let ((realf (gud-xdb-file-name f)))
530 (if realf (find-file-noselect realf))))
533 (defun xdb (command-line)
534 "Run xdb on program FILE in buffer *gud-FILE*.
535 The directory containing FILE becomes the initial working directory
536 and source-file directory for your debugger.
538 You can set the variable 'gud-xdb-directories' to a list of program source
539 directories if your program contains sources from more than one directory."
541 (list (read-from-minibuffer "Run xdb (like this): "
542 (if (consp gud-xdb-history)
543 (car gud-xdb-history)
546 '(gud-xdb-history . 1))))
547 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
548 (gud-marker-filter . gud-xdb-marker-filter)
549 (gud-find-file . gud-xdb-find-file)))
551 (gud-common-init command-line)
553 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
554 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
555 "Set temporary breakpoint at current line.")
556 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
557 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
558 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
559 (gud-def gud-cont "c" "\C-r" "Continue with display.")
560 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
561 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
562 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
563 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
565 (setq comint-prompt-regexp "^>")
566 (make-local-variable 'gud-xdb-accumulation)
567 (setq gud-xdb-accumulation "")
568 (run-hooks 'xdb-mode-hook))
570 ;; ======================================================================
573 ;;; History of argument lists passed to perldb.
574 (defvar gud-perldb-history nil)
576 (defun gud-perldb-massage-args (file args)
577 (cons "-d" (cons file (cons "-emacs" args))))
579 ;; There's no guarantee that Emacs will hand the filter the entire
580 ;; marker at once; it could be broken up across several strings. We
581 ;; might even receive a big chunk with several markers in it. If we
582 ;; receive a chunk of text which looks like it might contain the
583 ;; beginning of a marker, we save it here between calls to the
585 (defvar gud-perldb-marker-acc "")
587 (defun gud-perldb-marker-filter (string)
589 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
592 ;; Process all the complete markers in this chunk.
593 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
594 gud-perldb-marker-acc)
597 ;; Extract the frame position from the marker.
599 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
600 (string-to-int (substring gud-perldb-marker-acc
604 ;; Append any text before the marker to the output we're going
605 ;; to return - we don't include the marker in this text.
606 output (concat output
607 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
609 ;; Set the accumulator to the remaining text.
610 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
612 ;; Does the remaining text look like it might end with the
613 ;; beginning of another marker? If it does, then keep it in
614 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
615 ;; know the full marker regexp above failed, it's pretty simple to
616 ;; test for marker starts.
617 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
619 ;; Everything before the potential marker start can be output.
620 (setq output (concat output (substring gud-perldb-marker-acc
621 0 (match-beginning 0))))
623 ;; Everything after, we save, to combine with later input.
624 (setq gud-perldb-marker-acc
625 (substring gud-perldb-marker-acc (match-beginning 0))))
627 (setq output (concat output gud-perldb-marker-acc)
628 gud-perldb-marker-acc ""))
632 (defun gud-perldb-find-file (f)
633 (find-file-noselect f))
636 (defun perldb (command-line)
637 "Run perldb on program FILE in buffer *gud-FILE*.
638 The directory containing FILE becomes the initial working directory
639 and source-file directory for your debugger."
641 (list (read-from-minibuffer "Run perldb (like this): "
642 (if (consp gud-perldb-history)
643 (car gud-perldb-history)
646 '(gud-perldb-history . 1))))
647 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
648 (gud-marker-filter . gud-perldb-marker-filter)
649 (gud-find-file . gud-perldb-find-file)
652 (gud-common-init command-line)
654 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
655 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
656 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
657 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
658 (gud-def gud-cont "c" "\C-r" "Continue with display.")
659 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
660 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
661 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
662 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
664 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
665 (run-hooks 'perldb-mode-hook)
669 ;; End of debugger-specific information
673 ;;; When we send a command to the debugger via gud-call, it's annoying
674 ;;; to see the command and the new prompt inserted into the debugger's
675 ;;; buffer; we have other ways of knowing the command has completed.
677 ;;; If the buffer looks like this:
678 ;;; --------------------
679 ;;; (gdb) set args foo bar
681 ;;; --------------------
682 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
683 ;;; source file to set a breakpoint, we want the buffer to end up like
685 ;;; --------------------
686 ;;; (gdb) set args foo bar
687 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
689 ;;; --------------------
690 ;;; Essentially, the old prompt is deleted, and the command's output
691 ;;; and the new prompt take its place.
693 ;;; Not echoing the command is easy enough; you send it directly using
694 ;;; process-send-string, and it never enters the buffer. However,
695 ;;; getting rid of the old prompt is trickier; you don't want to do it
696 ;;; when you send the command, since that will result in an annoying
697 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
698 ;;; waits for a response from the debugger, and the new prompt is
699 ;;; inserted. Instead, we'll wait until we actually get some output
700 ;;; from the subprocess before we delete the prompt. If the command
701 ;;; produced no output other than a new prompt, that prompt will most
702 ;;; likely be in the first chunk of output received, so we will delete
703 ;;; the prompt and then replace it with an identical one. If the
704 ;;; command produces output, the prompt is moving anyway, so the
705 ;;; flicker won't be annoying.
707 ;;; So - when we want to delete the prompt upon receipt of the next
708 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
709 ;;; the start of the prompt; the process filter will notice this, and
710 ;;; delete all text between it and the process output marker. If
711 ;;; gud-delete-prompt-marker points nowhere, we leave the current
713 (defvar gud-delete-prompt-marker nil)
717 "Major mode for interacting with an inferior debugger process.
719 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
720 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
721 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
723 After startup, the following commands are available in both the GUD
724 interaction buffer and any source buffer GUD visits due to a breakpoint stop
727 \\[gud-break] sets a breakpoint at the current file and line. In the
728 GUD buffer, the current file and line are those of the last breakpoint or
729 step. In a source buffer, they are the buffer's file and current line.
731 \\[gud-remove] removes breakpoints on the current file and line.
733 \\[gud-refresh] displays in the source window the last line referred to
736 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
737 step-one-line (not entering function calls), and step-one-instruction
738 and then update the source window with the current file and position.
739 \\[gud-cont] continues execution.
741 \\[gud-print] tries to find the largest C lvalue or function-call expression
742 around point, and sends it to the debugger for value display.
744 The above commands are common to all supported debuggers except xdb which
745 does not support stepping instructions.
747 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
748 except that the breakpoint is temporary; that is, it is removed when
749 execution stops on it.
751 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
752 frame. \\[gud-down] drops back down through one.
754 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
755 the current function and stops.
757 All the keystrokes above are accessible in the GUD buffer
758 with the prefix C-c, and in all buffers through the prefix C-x C-a.
760 All pre-defined functions for which the concept make sense repeat
761 themselves the appropriate number of times if you give a prefix
764 You may use the `gud-def' macro in the initialization hook to define other
767 Other commands for interacting with the debugger process are inherited from
768 comint mode, which see."
771 (setq major-mode 'gud-mode)
772 (setq mode-name "Debugger")
773 (setq mode-line-process '(": %s"))
774 (use-local-map (copy-keymap comint-mode-map))
775 (make-local-variable 'gud-last-frame)
776 (setq gud-last-frame nil)
777 (make-local-variable 'comint-prompt-regexp)
778 (make-local-variable 'gud-delete-prompt-marker)
779 (setq gud-delete-prompt-marker (make-marker))
780 (run-hooks 'gud-mode-hook)
783 (defvar gud-comint-buffer nil)
785 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
786 (defun gud-chop-words (string)
788 (len (length string))
791 (if (memq (aref string i) '(?\t ? ))
793 (setq words (cons (substring string beg i) words)
795 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
800 (setq words (cons (substring string beg) words)))
803 ;; Perform initializations common to all debuggers.
804 (defun gud-common-init (command-line)
805 (let* ((words (gud-chop-words command-line))
806 (program (car words))
807 (file-word (let ((w (cdr words)))
808 (while (and w (= ?- (aref (car w) 0)))
811 (args (delq file-word (cdr words)))
812 (file (expand-file-name (substitute-in-file-name file-word)))
813 (filepart (file-name-nondirectory file)))
814 (switch-to-buffer (concat "*gud-" filepart "*"))
815 (setq default-directory (file-name-directory file))
816 (or (bolp) (newline))
817 (insert "Current directory is " default-directory "\n")
818 (apply 'make-comint (concat "gud-" filepart) program nil
819 (gud-massage-args file args)))
821 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
822 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
826 (defun gud-set-buffer ()
827 (cond ((eq major-mode 'gud-mode)
828 (setq gud-comint-buffer (current-buffer)))))
830 ;; These functions are responsible for inserting output from your debugger
831 ;; into the buffer. The hard work is done by the method that is
832 ;; the value of gud-marker-filter.
834 (defun gud-filter (proc string)
835 ;; Here's where the actual buffer insertion is done
836 (let ((inhibit-quit t))
838 (set-buffer (process-buffer proc))
839 (let (moving output-after-point)
841 (goto-char (process-mark proc))
842 ;; If we have been so requested, delete the debugger prompt.
843 (if (marker-buffer gud-delete-prompt-marker)
845 (delete-region (point) gud-delete-prompt-marker)
846 (set-marker gud-delete-prompt-marker nil)))
847 (insert-before-markers (gud-marker-filter string))
848 (setq moving (= (point) (process-mark proc)))
849 (setq output-after-point (< (point) (process-mark proc)))
850 ;; Check for a filename-and-line number.
851 ;; Don't display the specified file
852 ;; unless (1) point is at or after the position where output appears
853 ;; and (2) this buffer is on the screen.
854 (if (and gud-last-frame
855 (not output-after-point)
856 (get-buffer-window (current-buffer)))
857 (gud-display-frame)))
858 (if moving (goto-char (process-mark proc)))))))
860 (defun gud-sentinel (proc msg)
861 (cond ((null (buffer-name (process-buffer proc)))
863 ;; Stop displaying an arrow in a source file.
864 (setq overlay-arrow-position nil)
865 (set-process-buffer proc nil))
866 ((memq (process-status proc) '(signal exit))
867 ;; Stop displaying an arrow in a source file.
868 (setq overlay-arrow-position nil)
869 ;; Fix the mode line.
870 (setq mode-line-process
872 (symbol-name (process-status proc))))
873 (let* ((obuf (current-buffer)))
874 ;; save-excursion isn't the right thing if
875 ;; process-buffer is current-buffer
878 ;; Write something in *compilation* and hack its mode line,
879 (set-buffer (process-buffer proc))
880 ;; Force mode line redisplay soon
881 (set-buffer-modified-p (buffer-modified-p))
883 (insert ?\n mode-name " " msg)
885 (goto-char (point-max))
886 (insert ?\n mode-name " " msg)))
887 ;; If buffer and mode line will show that the process
888 ;; is dead, we can delete it now. Otherwise it
889 ;; will stay around until M-x list-processes.
890 (delete-process proc))
891 ;; Restore old buffer, but don't restore old point
892 ;; if obuf is the gud buffer.
893 (set-buffer obuf))))))
895 (defun gud-display-frame ()
896 "Find and obey the last filename-and-line marker from the debugger.
897 Obeying it means displaying in another window the specified file and line."
902 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
903 (setq gud-last-last-frame gud-last-frame
904 gud-last-frame nil))))
906 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
907 ;; and that its line LINE is visible.
908 ;; Put the overlay-arrow on the line LINE in that buffer.
909 ;; Most of the trickiness in here comes from wanting to preserve the current
910 ;; region-restriction if that's possible. We use an explicit display-buffer
911 ;; to get around the fact that this is called inside a save-excursion.
913 (defun gud-display-line (true-file line)
914 (let* ((buffer (gud-find-file true-file))
915 (window (display-buffer buffer))
917 ;;; (if (equal buffer (current-buffer))
919 ;;; (setq buffer-read-only nil))
921 ;;; (setq buffer-read-only t)
927 (setq overlay-arrow-string "=>")
928 (or overlay-arrow-position
929 (setq overlay-arrow-position (make-marker)))
930 (set-marker overlay-arrow-position (point) (current-buffer)))
931 (cond ((or (< pos (point-min)) (> pos (point-max)))
934 (set-window-point window overlay-arrow-position)))
936 ;;; The gud-call function must do the right thing whether its invoking
937 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
938 ;;; or a C buffer. In the former case, we want to supply data from
939 ;;; gud-last-frame. Here's how we do it:
941 (defun gud-format-command (str arg)
942 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
943 (frame (or gud-last-frame gud-last-last-frame))
945 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
946 (let ((key (string-to-char (substring str (match-beginning 2))))
950 (setq subst (file-name-nondirectory (if insource
954 (setq subst (file-name-directory (if insource
958 (setq subst (if insource
961 (save-restriction (widen)
962 (1+ (count-lines 1 (point)))))
965 (setq subst (find-c-expr)))
967 (setq subst (gud-read-address)))
969 (setq subst (if arg (int-to-string arg) ""))))
970 (setq result (concat result
971 (substring str (match-beginning 1) (match-end 1))
973 (setq str (substring str (match-end 2))))
974 ;; There might be text left in STR when the loop ends.
975 (concat result str)))
977 (defun gud-read-address ()
978 "Return a string containing the core-address found in the buffer at point."
980 (let ((pt (point)) found begin)
981 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
983 (found (forward-char 2)
984 (buffer-substring found
985 (progn (re-search-forward "[^0-9a-f]")
988 (t (setq begin (progn (re-search-backward "[^0-9]")
992 (re-search-forward "[^0-9]")
994 (buffer-substring begin (point)))))))
996 (defun gud-call (fmt &optional arg)
997 (let ((msg (gud-format-command fmt arg)))
998 (message "Command: %s" msg)
1000 (gud-basic-call msg)))
1002 (defun gud-basic-call (command)
1003 "Invoke the debugger COMMAND displaying source in other window."
1006 (let ((command (concat command "\n"))
1007 (proc (get-buffer-process gud-comint-buffer)))
1009 ;; Arrange for the current prompt to get deleted.
1011 (set-buffer gud-comint-buffer)
1012 (goto-char (process-mark proc))
1014 (if (looking-at comint-prompt-regexp)
1015 (set-marker gud-delete-prompt-marker (point))))
1016 (process-send-string proc command)))
1018 (defun gud-refresh (&optional arg)
1019 "Fix up a possibly garbled display, and redraw the arrow."
1022 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1023 (gud-display-frame))
1025 ;;; Code for parsing expressions out of C code. The single entry point is
1026 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1028 ;;; The rest of this file is a hacked version of gdbsrc.el by
1029 ;;; Debby Ayers <ayers@asc.slb.com>,
1030 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1032 (defun find-c-expr ()
1033 "Returns the C expr that surrounds point."
1036 (let ((p) (expr) (test-expr))
1038 (setq expr (expr-cur))
1039 (setq test-expr (expr-prev))
1040 (while (expr-compound test-expr expr)
1041 (setq expr (cons (car test-expr) (cdr expr)))
1042 (goto-char (car expr))
1043 (setq test-expr (expr-prev)))
1045 (setq test-expr (expr-next))
1046 (while (expr-compound expr test-expr)
1047 (setq expr (cons (car expr) (cdr test-expr)))
1048 (setq test-expr (expr-next))
1050 (buffer-substring (car expr) (cdr expr)))))
1053 "Returns the expr that point is in; point is set to beginning of expr.
1054 The expr is represented as a cons cell, where the car specifies the point in
1055 the current buffer that marks the beginning of the expr and the cdr specifies
1056 the character after the end of the expr."
1057 (let ((p (point)) (begin) (end))
1058 (expr-backward-sexp)
1059 (setq begin (point))
1073 (defun expr-backward-sexp ()
1074 "Version of `backward-sexp' that catches errors."
1079 (defun expr-forward-sexp ()
1080 "Version of `forward-sexp' that catches errors."
1086 "Returns the previous expr, point is set to beginning of that expr.
1087 The expr is represented as a cons cell, where the car specifies the point in
1088 the current buffer that marks the beginning of the expr and the cdr specifies
1089 the character after the end of the expr"
1090 (let ((begin) (end))
1091 (expr-backward-sexp)
1092 (setq begin (point))
1099 "Returns the following expr, point is set to beginning of that expr.
1100 The expr is represented as a cons cell, where the car specifies the point in
1101 the current buffer that marks the beginning of the expr and the cdr specifies
1102 the character after the end of the expr."
1103 (let ((begin) (end))
1107 (expr-backward-sexp)
1108 (setq begin (point))
1111 (defun expr-compound-sep (span-start span-end)
1112 "Returns '.' for '->' & '.', returns ' ' for white space,
1113 returns '?' for other punctuation."
1116 (while (< span-start span-end)
1117 (setq syntax (char-syntax (char-after span-start)))
1120 ((= syntax ?.) (setq syntax (char-after span-start))
1122 ((= syntax ?.) (setq result ?.))
1123 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1125 (setq span-start (+ span-start 1)))
1126 (t (setq span-start span-end)
1127 (setq result ??)))))
1128 (setq span-start (+ span-start 1)))
1131 (defun expr-compound (first second)
1132 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1133 The two exprs are represented as a cons cells, where the car
1134 specifies the point in the current buffer that marks the beginning of the
1135 expr and the cdr specifies the character after the end of the expr.
1136 Link exprs of the form:
1143 (let ((span-start (cdr first))
1144 (span-end (car second))
1146 (setq syntax (expr-compound-sep span-start span-end))
1148 ((= (car first) (car second)) nil)
1149 ((= (cdr first) (cdr second)) nil)
1152 (setq span-start (char-after (- span-start 1)))
1153 (setq span-end (char-after span-end))
1155 ((= span-start ?) ) t )
1156 ((= span-start ?] ) t )
1157 ((= span-end ?( ) t )
1158 ((= span-end ?[ ) t )
1165 ;;; gud.el ends here