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 (defun gud-dbx-find-file (f)
355 (find-file-noselect f
))
358 (defun dbx (command-line)
359 "Run dbx on program FILE in buffer *gud-FILE*.
360 The directory containing FILE becomes the initial working directory
361 and source-file directory for your debugger."
363 (list (read-from-minibuffer "Run dbx (like this): "
364 (if (consp gud-dbx-history
)
365 (car gud-dbx-history
)
368 '(gud-dbx-history .
1))))
369 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args
)
370 (gud-marker-filter . gud-dbx-marker-filter
)
371 (gud-find-file . gud-dbx-find-file
)
374 (gud-common-init command-line
)
376 (gud-def gud-break
"file \"%d%f\"\nstop at %l"
377 "\C-b" "Set breakpoint at current line.")
378 ;; (gud-def gud-break "stop at \"%f\":%l"
379 ;; "\C-b" "Set breakpoint at current line.")
380 (gud-def gud-remove
"clear %l" "\C-d" "Remove breakpoint at current line")
381 (gud-def gud-step
"step %p" "\C-s" "Step one line with display.")
382 (gud-def gud-stepi
"stepi %p" "\C-i" "Step one instruction with display.")
383 (gud-def gud-next
"next %p" "\C-n" "Step one line (skip functions).")
384 (gud-def gud-cont
"cont" "\C-r" "Continue with display.")
385 (gud-def gud-up
"up %p" "<" "Up (numeric arg) stack frames.")
386 (gud-def gud-down
"down %p" ">" "Down (numeric arg) stack frames.")
387 (gud-def gud-print
"print %e" "\C-p" "Evaluate C expression at point.")
389 (setq comint-prompt-regexp
"^[^)]*dbx) *")
390 (run-hooks 'dbx-mode-hook
)
393 ;; ======================================================================
394 ;; xdb (HP PARISC debugger) functions
396 ;;; History of argument lists passed to xdb.
397 (defvar gud-xdb-history nil
)
399 (defvar gud-xdb-directories nil
400 "*A list of directories that xdb should search for source code.
401 If nil, only source files in the program directory
402 will be known to xdb.
404 The file names should be absolute, or relative to the directory
405 containing the executable being debugged.")
407 (defun gud-xdb-massage-args (file args
)
408 (nconc (let ((directories gud-xdb-directories
)
411 (setq result
(cons (car directories
) (cons "-d" result
)))
412 (setq directories
(cdr directories
)))
413 (nreverse (cons file result
)))
416 (defun gud-xdb-file-name (f)
417 "Transform a relative pathname to a full pathname in xdb mode"
419 (if (file-exists-p f
)
420 (setq result
(expand-file-name f
))
421 (let ((directories gud-xdb-directories
))
423 (let ((path (concat (car directories
) "/" f
)))
424 (if (file-exists-p path
)
425 (setq result
(expand-file-name path
)
427 (setq directories
(cdr directories
)))))
430 ;; xdb does not print the lines all at once, so we have to accumulate them
431 (defvar gud-xdb-accumulation
"")
433 (defun gud-xdb-marker-filter (string)
435 (if (or (string-match comint-prompt-regexp string
)
436 (string-match ".*\012" string
))
437 (setq result
(concat gud-xdb-accumulation string
)
438 gud-xdb-accumulation
"")
439 (setq gud-xdb-accumulation
(concat gud-xdb-accumulation string
)))
441 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result
)
442 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
444 (let ((line (string-to-int
445 (substring result
(match-beginning 2) (match-end 2))))
446 (file (gud-xdb-file-name
447 (substring result
(match-beginning 1) (match-end 1)))))
449 (setq gud-last-frame
(cons file line
))))))
452 (defun gud-xdb-find-file (f)
453 (let ((realf (gud-xdb-file-name f
)))
454 (if realf
(find-file-noselect realf
))))
457 (defun xdb (command-line)
458 "Run xdb on program FILE in buffer *gud-FILE*.
459 The directory containing FILE becomes the initial working directory
460 and source-file directory for your debugger.
462 You can set the variable 'gud-xdb-directories' to a list of program source
463 directories if your program contains sources from more than one directory."
465 (list (read-from-minibuffer "Run xdb (like this): "
466 (if (consp gud-xdb-history
)
467 (car gud-xdb-history
)
470 '(gud-xdb-history .
1))))
471 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args
)
472 (gud-marker-filter . gud-xdb-marker-filter
)
473 (gud-find-file . gud-xdb-find-file
)))
475 (gud-common-init command-line
)
477 (gud-def gud-break
"b %f:%l" "\C-b" "Set breakpoint at current line.")
478 (gud-def gud-tbreak
"b %f:%l\\t" "\C-t"
479 "Set temporary breakpoint at current line.")
480 (gud-def gud-remove
"db" "\C-d" "Remove breakpoint at current line")
481 (gud-def gud-step
"s %p" "\C-s" "Step one line with display.")
482 (gud-def gud-next
"S %p" "\C-n" "Step one line (skip functions).")
483 (gud-def gud-cont
"c" "\C-r" "Continue with display.")
484 (gud-def gud-up
"up %p" "<" "Up (numeric arg) stack frames.")
485 (gud-def gud-down
"down %p" ">" "Down (numeric arg) stack frames.")
486 (gud-def gud-finish
"bu\\t" "\C-f" "Finish executing current function.")
487 (gud-def gud-print
"p %e" "\C-p" "Evaluate C expression at point.")
489 (setq comint-prompt-regexp
"^>")
490 (make-local-variable 'gud-xdb-accumulation
)
491 (setq gud-xdb-accumulation
"")
492 (run-hooks 'xdb-mode-hook
))
494 ;; ======================================================================
497 ;;; History of argument lists passed to perldb.
498 (defvar gud-perldb-history nil
)
500 (defun gud-perldb-massage-args (file args
)
501 (cons "-d" (cons file
(cons "-emacs" args
))))
503 ;; There's no guarantee that Emacs will hand the filter the entire
504 ;; marker at once; it could be broken up across several strings. We
505 ;; might even receive a big chunk with several markers in it. If we
506 ;; receive a chunk of text which looks like it might contain the
507 ;; beginning of a marker, we save it here between calls to the
509 (defvar gud-perldb-marker-acc
"")
511 (defun gud-perldb-marker-filter (string)
513 (setq gud-perldb-marker-acc
(concat gud-perldb-marker-acc string
))
516 ;; Process all the complete markers in this chunk.
517 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
518 gud-perldb-marker-acc
)
521 ;; Extract the frame position from the marker.
523 (cons (substring gud-perldb-marker-acc
(match-beginning 1) (match-end 1))
524 (string-to-int (substring gud-perldb-marker-acc
528 ;; Append any text before the marker to the output we're going
529 ;; to return - we don't include the marker in this text.
530 output
(concat output
531 (substring gud-perldb-marker-acc
0 (match-beginning 0)))
533 ;; Set the accumulator to the remaining text.
534 gud-perldb-marker-acc
(substring gud-perldb-marker-acc
(match-end 0))))
536 ;; Does the remaining text look like it might end with the
537 ;; beginning of another marker? If it does, then keep it in
538 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
539 ;; know the full marker regexp above failed, it's pretty simple to
540 ;; test for marker starts.
541 (if (string-match "^\032.*\\'" gud-perldb-marker-acc
)
543 ;; Everything before the potential marker start can be output.
544 (setq output
(concat output
(substring gud-perldb-marker-acc
545 0 (match-beginning 0))))
547 ;; Everything after, we save, to combine with later input.
548 (setq gud-perldb-marker-acc
549 (substring gud-perldb-marker-acc
(match-beginning 0))))
551 (setq output
(concat output gud-perldb-marker-acc
)
552 gud-perldb-marker-acc
""))
556 (defun gud-perldb-find-file (f)
557 (find-file-noselect f
))
560 (defun perldb (command-line)
561 "Run perldb on program FILE in buffer *gud-FILE*.
562 The directory containing FILE becomes the initial working directory
563 and source-file directory for your debugger."
565 (list (read-from-minibuffer "Run perldb (like this): "
566 (if (consp gud-perldb-history
)
567 (car gud-perldb-history
)
570 '(gud-perldb-history .
1))))
571 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args
)
572 (gud-marker-filter . gud-perldb-marker-filter
)
573 (gud-find-file . gud-perldb-find-file
)
576 (gud-common-init command-line
)
578 (gud-def gud-break
"b %l" "\C-b" "Set breakpoint at current line.")
579 (gud-def gud-remove
"d %l" "\C-d" "Remove breakpoint at current line")
580 (gud-def gud-step
"s" "\C-s" "Step one source line with display.")
581 (gud-def gud-next
"n" "\C-n" "Step one line (skip functions).")
582 (gud-def gud-cont
"c" "\C-r" "Continue with display.")
583 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
584 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
585 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
586 (gud-def gud-print
"%e" "\C-p" "Evaluate perl expression at point.")
588 (setq comint-prompt-regexp
"^ DB<[0-9]+> ")
589 (run-hooks 'perldb-mode-hook
)
593 ;; End of debugger-specific information
597 ;;; When we send a command to the debugger via gud-call, it's annoying
598 ;;; to see the command and the new prompt inserted into the debugger's
599 ;;; buffer; we have other ways of knowing the command has completed.
601 ;;; If the buffer looks like this:
602 ;;; --------------------
603 ;;; (gdb) set args foo bar
605 ;;; --------------------
606 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
607 ;;; source file to set a breakpoint, we want the buffer to end up like
609 ;;; --------------------
610 ;;; (gdb) set args foo bar
611 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
613 ;;; --------------------
614 ;;; Essentially, the old prompt is deleted, and the command's output
615 ;;; and the new prompt take its place.
617 ;;; Not echoing the command is easy enough; you send it directly using
618 ;;; process-send-string, and it never enters the buffer. However,
619 ;;; getting rid of the old prompt is trickier; you don't want to do it
620 ;;; when you send the command, since that will result in an annoying
621 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
622 ;;; waits for a response from the debugger, and the new prompt is
623 ;;; inserted. Instead, we'll wait until we actually get some output
624 ;;; from the subprocess before we delete the prompt. If the command
625 ;;; produced no output other than a new prompt, that prompt will most
626 ;;; likely be in the first chunk of output received, so we will delete
627 ;;; the prompt and then replace it with an identical one. If the
628 ;;; command produces output, the prompt is moving anyway, so the
629 ;;; flicker won't be annoying.
631 ;;; So - when we want to delete the prompt upon receipt of the next
632 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
633 ;;; the start of the prompt; the process filter will notice this, and
634 ;;; delete all text between it and the process output marker. If
635 ;;; gud-delete-prompt-marker points nowhere, we leave the current
637 (defvar gud-delete-prompt-marker nil
)
641 "Major mode for interacting with an inferior debugger process.
643 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
644 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
645 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
647 After startup, the following commands are available in both the GUD
648 interaction buffer and any source buffer GUD visits due to a breakpoint stop
651 \\[gud-break] sets a breakpoint at the current file and line. In the
652 GUD buffer, the current file and line are those of the last breakpoint or
653 step. In a source buffer, they are the buffer's file and current line.
655 \\[gud-remove] removes breakpoints on the current file and line.
657 \\[gud-refresh] displays in the source window the last line referred to
660 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
661 step-one-line (not entering function calls), and step-one-instruction
662 and then update the source window with the current file and position.
663 \\[gud-cont] continues execution.
665 \\[gud-print] tries to find the largest C lvalue or function-call expression
666 around point, and sends it to the debugger for value display.
668 The above commands are common to all supported debuggers except xdb which
669 does not support stepping instructions.
671 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
672 except that the breakpoint is temporary; that is, it is removed when
673 execution stops on it.
675 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
676 frame. \\[gud-down] drops back down through one.
678 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
679 the current function and stops.
681 All the keystrokes above are accessible in the GUD buffer
682 with the prefix C-c, and in all buffers through the prefix C-x C-a.
684 All pre-defined functions for which the concept make sense repeat
685 themselves the appropriate number of times if you give a prefix
688 You may use the `gud-def' macro in the initialization hook to define other
691 Other commands for interacting with the debugger process are inherited from
692 comint mode, which see."
695 (setq major-mode
'gud-mode
)
696 (setq mode-name
"Debugger")
697 (setq mode-line-process
'(": %s"))
698 (use-local-map (copy-keymap comint-mode-map
))
699 (make-local-variable 'gud-last-frame
)
700 (setq gud-last-frame nil
)
701 (make-local-variable 'comint-prompt-regexp
)
702 (make-local-variable 'gud-delete-prompt-marker
)
703 (setq gud-delete-prompt-marker
(make-marker))
704 (run-hooks 'gud-mode-hook
)
707 (defvar gud-comint-buffer nil
)
709 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
710 (defun gud-chop-words (string)
712 (len (length string
))
715 (if (memq (aref string i
) '(?
\t ?
))
717 (setq words
(cons (substring string beg i
) words
)
719 (while (and (< beg len
) (memq (aref string beg
) '(?
\t ?
)))
724 (setq words
(cons (substring string beg
) words
)))
727 ;; Perform initializations common to all debuggers.
728 (defun gud-common-init (command-line)
729 (let* ((words (gud-chop-words command-line
))
730 (program (car words
))
731 (file-word (let ((w (cdr words
)))
732 (while (and w
(= ?-
(aref (car w
) 0)))
735 (args (delq file-word
(cdr words
)))
736 (file (expand-file-name file-word
))
737 (filepart (file-name-nondirectory file
)))
738 (switch-to-buffer (concat "*gud-" filepart
"*"))
739 (setq default-directory
(file-name-directory file
))
740 (or (bolp) (newline))
741 (insert "Current directory is " default-directory
"\n")
742 (apply 'make-comint
(concat "gud-" filepart
) program nil
743 (gud-massage-args file args
)))
745 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter
)
746 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel
)
750 (defun gud-set-buffer ()
751 (cond ((eq major-mode
'gud-mode
)
752 (setq gud-comint-buffer
(current-buffer)))))
754 ;; These functions are responsible for inserting output from your debugger
755 ;; into the buffer. The hard work is done by the method that is
756 ;; the value of gud-marker-filter.
758 (defun gud-filter (proc string
)
759 ;; Here's where the actual buffer insertion is done
760 (let ((inhibit-quit t
))
762 (set-buffer (process-buffer proc
))
763 (let (moving output-after-point
)
765 (goto-char (process-mark proc
))
766 ;; If we have been so requested, delete the debugger prompt.
767 (if (marker-buffer gud-delete-prompt-marker
)
769 (delete-region (point) gud-delete-prompt-marker
)
770 (set-marker gud-delete-prompt-marker nil
)))
771 (insert-before-markers (gud-marker-filter string
))
772 (setq moving
(= (point) (process-mark proc
)))
773 (setq output-after-point
(< (point) (process-mark proc
)))
774 ;; Check for a filename-and-line number.
775 ;; Don't display the specified file
776 ;; unless (1) point is at or after the position where output appears
777 ;; and (2) this buffer is on the screen.
778 (if (and gud-last-frame
779 (not output-after-point
)
780 (get-buffer-window (current-buffer)))
781 (gud-display-frame)))
782 (if moving
(goto-char (process-mark proc
)))))))
784 (defun gud-sentinel (proc msg
)
785 (cond ((null (buffer-name (process-buffer proc
)))
787 ;; Stop displaying an arrow in a source file.
788 (setq overlay-arrow-position nil
)
789 (set-process-buffer proc nil
))
790 ((memq (process-status proc
) '(signal exit
))
791 ;; Stop displaying an arrow in a source file.
792 (setq overlay-arrow-position nil
)
793 ;; Fix the mode line.
794 (setq mode-line-process
796 (symbol-name (process-status proc
))))
797 (let* ((obuf (current-buffer)))
798 ;; save-excursion isn't the right thing if
799 ;; process-buffer is current-buffer
802 ;; Write something in *compilation* and hack its mode line,
803 (set-buffer (process-buffer proc
))
804 ;; Force mode line redisplay soon
805 (set-buffer-modified-p (buffer-modified-p))
807 (insert ?
\n mode-name
" " msg
)
809 (goto-char (point-max))
810 (insert ?
\n mode-name
" " msg
)))
811 ;; If buffer and mode line will show that the process
812 ;; is dead, we can delete it now. Otherwise it
813 ;; will stay around until M-x list-processes.
814 (delete-process proc
))
815 ;; Restore old buffer, but don't restore old point
816 ;; if obuf is the gud buffer.
817 (set-buffer obuf
))))))
819 (defun gud-display-frame ()
820 "Find and obey the last filename-and-line marker from the debugger.
821 Obeying it means displaying in another window the specified file and line."
826 (gud-display-line (car gud-last-frame
) (cdr gud-last-frame
))
827 (setq gud-last-last-frame gud-last-frame
828 gud-last-frame nil
))))
830 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
831 ;; and that its line LINE is visible.
832 ;; Put the overlay-arrow on the line LINE in that buffer.
833 ;; Most of the trickiness in here comes from wanting to preserve the current
834 ;; region-restriction if that's possible. We use an explicit display-buffer
835 ;; to get around the fact that this is called inside a save-excursion.
837 (defun gud-display-line (true-file line
)
838 (let* ((buffer (gud-find-file true-file
))
839 (window (display-buffer buffer
))
841 ;;; (if (equal buffer (current-buffer))
843 ;;; (setq buffer-read-only nil))
845 ;;; (setq buffer-read-only t)
851 (setq overlay-arrow-string
"=>")
852 (or overlay-arrow-position
853 (setq overlay-arrow-position
(make-marker)))
854 (set-marker overlay-arrow-position
(point) (current-buffer)))
855 (cond ((or (< pos
(point-min)) (> pos
(point-max)))
858 (set-window-point window overlay-arrow-position
)))
860 ;;; The gud-call function must do the right thing whether its invoking
861 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
862 ;;; or a C buffer. In the former case, we want to supply data from
863 ;;; gud-last-frame. Here's how we do it:
865 (defun gud-format-command (str arg
)
866 (let ((insource (not (eq (current-buffer) gud-comint-buffer
))))
867 (if (string-match "\\(.*\\)%f\\(.*\\)" str
)
869 (substring str
(match-beginning 1) (match-end 1))
870 (file-name-nondirectory (if insource
872 (car gud-last-frame
)))
873 (substring str
(match-beginning 2) (match-end 2)))))
874 (if (string-match "\\(.*\\)%d\\(.*\\)" str
)
876 (substring str
(match-beginning 1) (match-end 1))
877 (file-name-directory (if insource
879 (car gud-last-frame
)))
880 (substring str
(match-beginning 2) (match-end 2)))))
881 (if (string-match "\\(.*\\)%l\\(.*\\)" str
)
883 (substring str
(match-beginning 1) (match-end 1))
887 (save-restriction (widen)
888 (1+ (count-lines 1 (point)))))
889 (cdr gud-last-frame
))
890 (substring str
(match-beginning 2) (match-end 2)))))
891 (if (string-match "\\(.*\\)%e\\(.*\\)" str
)
893 (substring str
(match-beginning 1) (match-end 1))
895 (substring str
(match-beginning 2) (match-end 2)))))
896 (if (string-match "\\(.*\\)%a\\(.*\\)" str
)
898 (substring str
(match-beginning 1) (match-end 1))
900 (substring str
(match-beginning 2) (match-end 2)))))
901 (if (string-match "\\(.*\\)%p\\(.*\\)" str
)
903 (substring str
(match-beginning 1) (match-end 1))
904 (if arg
(int-to-string arg
) "")
905 (substring str
(match-beginning 2) (match-end 2)))))
910 (defun gud-read-address ()
911 "Return a string containing the core-address found in the buffer at point."
913 (let ((pt (point)) found begin
)
914 (setq found
(if (search-backward "0x" (- pt
7) t
) (point)))
916 (found (forward-char 2)
917 (buffer-substring found
918 (progn (re-search-forward "[^0-9a-f]")
921 (t (setq begin
(progn (re-search-backward "[^0-9]")
925 (re-search-forward "[^0-9]")
927 (buffer-substring begin
(point)))))))
929 (defun gud-call (fmt &optional arg
)
930 (let ((msg (gud-format-command fmt arg
)))
931 (message "Command: %s" msg
)
933 (gud-basic-call msg
)))
935 (defun gud-basic-call (command)
936 "Invoke the debugger COMMAND displaying source in other window."
939 (let ((command (concat command
"\n"))
940 (proc (get-buffer-process gud-comint-buffer
)))
942 ;; Arrange for the current prompt to get deleted.
944 (set-buffer gud-comint-buffer
)
945 (goto-char (process-mark proc
))
947 (if (looking-at comint-prompt-regexp
)
948 (set-marker gud-delete-prompt-marker
(point))))
949 (process-send-string proc command
)))
951 (defun gud-refresh (&optional arg
)
952 "Fix up a possibly garbled display, and redraw the arrow."
955 (or gud-last-frame
(setq gud-last-frame gud-last-last-frame
))
958 ;;; Code for parsing expressions out of C code. The single entry point is
959 ;;; find-c-expr, which tries to return an lvalue expression from around point.
961 ;;; The rest of this file is a hacked version of gdbsrc.el by
962 ;;; Debby Ayers <ayers@asc.slb.com>,
963 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
965 (defun find-c-expr ()
966 "Returns the C expr that surrounds point."
969 (let ((p) (expr) (test-expr))
971 (setq expr
(expr-cur))
972 (setq test-expr
(expr-prev))
973 (while (expr-compound test-expr expr
)
974 (setq expr
(cons (car test-expr
) (cdr expr
)))
975 (goto-char (car expr
))
976 (setq test-expr
(expr-prev)))
978 (setq test-expr
(expr-next))
979 (while (expr-compound expr test-expr
)
980 (setq expr
(cons (car expr
) (cdr test-expr
)))
981 (setq test-expr
(expr-next))
983 (buffer-substring (car expr
) (cdr expr
)))))
986 "Returns the expr that point is in; point is set to beginning of expr.
987 The expr is represented as a cons cell, where the car specifies the point in
988 the current buffer that marks the beginning of the expr and the cdr specifies
989 the character after the end of the expr."
990 (let ((p (point)) (begin) (end))
1006 (defun expr-backward-sexp ()
1007 "Version of `backward-sexp' that catches errors."
1012 (defun expr-forward-sexp ()
1013 "Version of `forward-sexp' that catches errors."
1019 "Returns the previous expr, point is set to beginning of that expr.
1020 The expr is represented as a cons cell, where the car specifies the point in
1021 the current buffer that marks the beginning of the expr and the cdr specifies
1022 the character after the end of the expr"
1023 (let ((begin) (end))
1024 (expr-backward-sexp)
1025 (setq begin
(point))
1032 "Returns the following expr, point is set to beginning of that expr.
1033 The expr is represented as a cons cell, where the car specifies the point in
1034 the current buffer that marks the beginning of the expr and the cdr specifies
1035 the character after the end of the expr."
1036 (let ((begin) (end))
1040 (expr-backward-sexp)
1041 (setq begin
(point))
1044 (defun expr-compound-sep (span-start span-end
)
1045 "Returns '.' for '->' & '.', returns ' ' for white space,
1046 returns '?' for other punctuation."
1049 (while (< span-start span-end
)
1050 (setq syntax
(char-syntax (char-after span-start
)))
1053 ((= syntax ?.
) (setq syntax
(char-after span-start
))
1055 ((= syntax ?.
) (setq result ?.
))
1056 ((and (= syntax ?-
) (= (char-after (+ span-start
1)) ?
>))
1058 (setq span-start
(+ span-start
1)))
1059 (t (setq span-start span-end
)
1060 (setq result ??
)))))
1061 (setq span-start
(+ span-start
1)))
1064 (defun expr-compound (first second
)
1065 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1066 The two exprs are represented as a cons cells, where the car
1067 specifies the point in the current buffer that marks the beginning of the
1068 expr and the cdr specifies the character after the end of the expr.
1069 Link exprs of the form:
1076 (let ((span-start (cdr first
))
1077 (span-end (car second
))
1079 (setq syntax
(expr-compound-sep span-start span-end
))
1081 ((= (car first
) (car second
)) nil
)
1082 ((= (cdr first
) (cdr second
)) nil
)
1085 (setq span-start
(char-after (- span-start
1)))
1086 (setq span-end
(char-after span-end
))
1088 ((= span-start ?
) ) t
)
1089 ((= span-start ?
] ) t
)
1090 ((= span-end ?
( ) t
)
1091 ((= span-end ?
[ ) t
)
1098 ;;; gud.el ends here