2 (` (save-excursion (set-buffer "*gud-a.out*") (, form))))
4 (defun dbug (foo &optional fun)
6 (set-buffer (get-buffer-create "*trace*"))
7 (goto-char (point-max))
8 (insert "***" (symbol-name foo) "\n")
13 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
16 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
19 ;; Keywords: unix, tools
21 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
23 ;; This file is part of GNU Emacs.
25 ;; GNU Emacs is free software; you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation; either version 2, or (at your option)
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with GNU Emacs; see the file COPYING. If not, write to
37 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
41 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
42 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
43 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
44 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
45 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
46 ;; added support for xdb (HPUX debugger).
48 ;; Jim Kingdon added support for ^Z^Z^Z annotations.
55 ;; ======================================================================
56 ;; GUD commands must be visible in C buffers visited by GUD
58 (defvar gud-key-prefix "\C-x\C-a"
59 "Prefix of all GUD commands valid in C buffers.")
61 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
62 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
64 ;; ======================================================================
65 ;; the overloading mechanism
67 (defun gud-overload-functions (gud-overload-alist)
68 "Overload functions defined in GUD-OVERLOAD-ALIST.
69 This association list has elements of the form
70 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
72 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
75 (defun gud-massage-args (file args)
76 (error "GUD not properly entered."))
78 (defun gud-marker-filter (str)
79 (error "GUD not properly entered."))
81 (defun gud-find-file (f)
82 (error "GUD not properly entered."))
84 ;; ======================================================================
87 ;; This macro is used below to define some basic debugger interface commands.
88 ;; Of course you may use `gud-def' with any other debugger command, including
91 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
92 ;; which defines FUNC to send the command NAME to the debugger, gives
93 ;; it the docstring DOC, and binds that function to KEY in the GUD
94 ;; major mode. The function is also bound in the global keymap with the
97 (defmacro gud-def (func cmd key &optional doc)
98 "Define FUNC to be a command sending STR and bound to KEY, with
99 optional doc string DOC. Certain %-escapes in the string arguments
100 are interpreted specially if present. These are:
102 %f name (without directory) of current source file.
103 %d directory of current source file.
104 %l number of current source line
105 %e text of the C lvalue or function-call expression surrounding point.
106 %a text of the hexadecimal address surrounding point
107 %p prefix argument to the command (if any) as a number
109 The `current' source file is the file of the current buffer (if
110 we're in a C file) or the source file current at the last break or
111 step (if we're in the GUD buffer).
112 The `current' line is that of the current buffer (if we're in a
113 source file) or the source line number at the last break or step (if
114 we're in the GUD buffer)."
116 (list 'defun func '(arg)
119 (list 'gud-call cmd 'arg))
126 (list 'global-set-key
127 (list 'concat 'gud-key-prefix key)
128 (list 'quote func)))))
130 ;; Where gud-display-frame should put the debugging arrow. This is
131 ;; set by the marker-filter, which scans the debugger's output for
132 ;; indications of the current program counter.
133 (defvar gud-last-frame nil)
135 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
136 ;; the last frame, even if it's been called before and gud-last-frame has
138 (defvar gud-last-last-frame nil)
140 ;; All debugger-specific information is collected here.
141 ;; Here's how it works, in case you ever need to add a debugger to the mode.
143 ;; Each entry must define the following at startup:
146 ;; comint-prompt-regexp
147 ;; gud-<name>-massage-args
148 ;; gud-<name>-marker-filter
149 ;; gud-<name>-find-file
151 ;; The job of the massage-args method is to modify the given list of
152 ;; debugger arguments before running the debugger.
154 ;; The job of the marker-filter method is to detect file/line markers in
155 ;; strings and set the global gud-last-frame to indicate what display
156 ;; action (if any) should be triggered by the marker. Note that only
157 ;; whatever the method *returns* is displayed in the buffer; thus, you
158 ;; can filter the debugger's output, interpreting some and passing on
161 ;; The job of the find-file method is to visit and return the buffer indicated
162 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
165 ;; ======================================================================
168 ;;; History of argument lists passed to gdb.
169 (defvar gud-gdb-history nil)
171 (defun gud-gdb-massage-args (file args)
172 (cons "-annotate=2" (cons file args)))
176 ;; In this world, there are gdb instance objects (of unspecified
177 ;; representation) and buffers associated with those objects.
181 ;; gdb-instance objects
184 (defun make-gdb-instance (proc)
185 "Create a gdb instance object from a gdb process."
186 (let ((instance (cons 'gdb-instance proc)))
188 (set-buffer (process-buffer proc))
189 (if (not (equal gdb-buffer-instance instance))
191 (mapcar 'make-variable-buffer-local gdb-instance-variables)
192 (setq gdb-buffer-instance instance) ; These are both...
193 (setq gdb-buffer-type 'gud)))) ; ...instance variables
196 (defun gdb-instance-process (inst) (cdr inst))
198 (defvar gdb-instance-variables '()
199 "A list of variables that are local to the gud buffer associated
200 with a gdb instance.")
202 (defmacro def-gdb-variable
203 (name accessor setter &optional default doc)
206 (defvar (, name) (, default) (, (or doc "undocumented")))
207 (if (not (memq '(, name) gdb-instance-variables))
208 (setq gdb-instance-variables
209 (cons '(, name) gdb-instance-variables)))
212 (defun (, accessor) (instance)
214 ((buffer (gdb-get-instance-buffer instance 'gud)))
221 (defun (, setter) (instance val)
223 ((buffer (gdb-get-instance-buffer instance 'gud)))
227 (setq (, name) val)))))))))))
229 (defmacro def-gdb-var (root-symbol &optional default doc)
230 (let* ((root (symbol-name root-symbol))
231 (accessor (intern (concat "gdb-instance-" root)))
232 (setter (intern (concat "set-gdb-instance-" root)))
233 (var-name (intern (concat "gdb-" root))))
235 (, var-name) (, accessor) (, setter)
236 (, default) (, doc)))))
238 (def-gdb-var buffer-instance nil
239 "In an instance buffer, the buffer's instance.")
241 (def-gdb-var buffer-type nil
242 "One of the symbols bound in gdb-instance-buffer-rules")
244 (def-gdb-var burst ""
245 "A string of characters from gdb that have not yet been processed.")
247 (def-gdb-var input-queue ()
248 "A list of high priority gdb command objects.")
250 (def-gdb-var idle-input-queue ()
251 "A list of low priority gdb command objects.")
253 (def-gdb-var prompting nil
254 "True when gdb is idle with no pending input.")
256 (def-gdb-var output-sink 'user
257 "The disposition of the output of the current gdb command.")
259 (def-gdb-var current-item nil
260 "The most recent command item sent to gdb.")
262 (def-gdb-var pending-triggers '()
263 "A list of trigger functions that have run later than their output
266 (defun in-gdb-instance-context (instance form)
267 "Funcall `form' in the gud buffer of `instance'"
269 (set-buffer (gdb-get-instance-buffer instance 'gud))
272 ;; end of instance vars
278 (defun gdb-proc->instance (proc)
280 (set-buffer (process-buffer proc))
281 gdb-buffer-instance))
283 (defun gdb-mru-instance-buffer ()
284 "Return the most recently used (non-auxiliary) gdb gud buffer."
286 (gdb-goto-first-gdb-instance (buffer-list))))
288 (defun gdb-goto-first-gdb-instance (blist)
289 "Use gdb-mru-instance-buffer -- not this."
292 (set-buffer (car blist))
293 (or (and gdb-buffer-instance
294 (eq gdb-buffer-type 'gud)
296 (gdb-find-first-gdb-instance (cdr blist))))))
298 (defun buffer-gdb-instance (buf)
301 gdb-buffer-instance))
303 (defun gdb-needed-default-instance ()
304 "Return the most recently used gdb instance or signal an error."
305 (let ((buffer (gdb-mru-instance-buffer)))
306 (or (and buffer (buffer-gdb-instance buffer))
307 (error "No instance of gdb found."))))
309 (defun gdb-instance-target-string (instance)
310 "The apparent name of the program being debugged by a gdb instance.
311 For sure this the root string used in smashing together the gud
312 buffer's name, even if that doesn't happen to be the name of a
314 (in-gdb-instance-context
316 (function (lambda () gud-target-name))))
318 ;; More than one buffer can be associated with a gdb instance.
320 ;; Each buffer has a TYPE -- an atom that identifies the function
321 ;; of that particular buffer.
323 ;; The usual gud interaction buffer is given the type `gud' and
324 ;; is constructed specially.
326 ;; Others are constructed by gdb-get-create-instance-buffer and
327 ;; named according to the rules set forth here:
330 (defvar gdb-instance-buffer-rules-assoc
331 '((gud error) ; gud buffers construct specially
332 (gdb-partial-output-buffer
333 gdb-partial-output-name
335 (gdb-registers-buffer
336 gdb-registers-buffer-name)
337 (gdb-breakpoints-buffer
338 gdb-breakpoints-buffer-name)
340 gdb-frames-buffer-name)))
344 ;; gdb communications
347 ;; input: things sent to gdb
349 ;; Each instance has a high and low priority
350 ;; input queue. Low priority input is sent only
351 ;; when the high priority queue is idle.
353 ;; The queues are lists. Each element is either
354 ;; a string (indicating user or user-like input)
355 ;; or a list of the form:
357 ;; (INPUT-STRING HANDLER-FN)
360 ;; The handler function will be called from the
361 ;; partial-output buffer when the command completes.
363 ;; These lists are consumed tail first.
366 (defun gdb-send (proc string)
367 "A comint send filter for gdb.
368 This filter may simply queue output for a later time."
369 (let ((instance (gdb-proc->instance proc)))
370 (gdb-instance-enqueue-input instance (concat string "\n"))))
372 (defun gdb-instance-enqueue-input (instance item)
373 "Enqueue an input item (a string or a list) for a gdb instance."
374 (if (gdb-instance-prompting instance)
376 (gdb-send-item instance item)
377 (set-gdb-instance-prompting instance nil))
378 (set-gdb-instance-input-queue
380 (cons item (gdb-instance-input-queue instance)))))
382 (defun gdb-instance-dequeue-input (instance)
383 (let ((queue (gdb-instance-input-queue instance)))
385 (if (not (cdr queue))
386 (let ((answer (car queue)))
387 (set-gdb-instance-input-queue instance '())
389 (gdb-take-last-elt queue)))))
391 (defun gdb-instance-enqueue-idle-input (instance item)
392 "Enqueue idle input (a string or a list) for a gdb instance."
393 (if (gdb-instance-prompting instance)
395 (gdb-send-item instance item)
396 (set-gdb-instance-prompting instance nil))
397 (set-gdb-instance-idle-input-queue
399 (cons item (gdb-instance-idle-input-queue instance)))))
401 (defun gdb-instance-dequeue-idle-input (instance)
402 (let ((queue (gdb-instance-idle-input-queue instance)))
404 (if (not (cdr queue))
405 (let ((answer (car queue)))
406 (set-gdb-instance-idle-input-queue instance '())
408 (gdb-take-last-elt queue)))))
410 (defun gdb-take-last-elt (l)
411 "Don't use this in general."
413 (gdb-take-last-elt (cdr l))
414 (let ((answer (car (cdr l))))
420 ;; output -- things gdb prints to emacs
422 ;; GDB output is a stream interrupted by annotations.
423 ;; Annotations can be recognized by their beginning
424 ;; with \C-j\C-z\C-z<tag><opt>\C-j
426 ;; The tag is a string obeying symbol syntax.
428 ;; The optional part `<opt>' can be either the empty string
429 ;; or a space followed by more data relating to the annotation.
430 ;; For example, the SOURCE annotation is followed by a filename,
431 ;; line number and various useless goo. This data must not include
436 (defun gud-gdb-marker-filter (string)
437 "A gud marker filter for gdb."
438 ;; Bogons don't tell us the process except through scoping crud.
439 (let ((instance (gdb-proc->instance proc)))
440 (gdb-output-burst instance string)))
442 (defvar gdb-annotation-rules
443 '(("frames-invalid" gdb-invalidate-frames)
444 ("breakpoints-invalid" gdb-invalidate-breakpoints)
445 ("pre-prompt" gdb-pre-prompt)
446 ("prompt" gdb-prompt)
447 ("post-prompt" gdb-post-prompt)
448 ("source" gdb-source)
449 ("value-history-begin" gdb-ignore-annotation)
450 ("value-history-end" gdb-ignore-annotation)
451 ("arg-name-begin" gdb-ignore-annotation)
452 ("arg-name-end" gdb-ignore-annotation)
453 ("field-name-begin" gdb-ignore-annotation)
454 ("field-name-end" gdb-ignore-annotation)
455 ("field-begin" gdb-ignore-annotation)
456 ("field-end" gdb-ignore-annotation)
457 ("array-element-begin" gdb-ignore-annotation)
458 ("array-element-end" gdb-ignore-annotation))
459 "An assoc mapping annotation tags to functions which process them.")
462 (defun gdb-ignore-annotation (instance args)
465 (defconst gdb-source-spec-regexp
466 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x[a-f0-9]*")
468 (defun gdb-source (instance args)
469 "Do not use this except as an annotation handler."
470 (string-match gdb-source-spec-regexp args)
471 ;; Extract the frame position from the marker.
474 (substring args (match-beginning 1) (match-end 1))
475 (string-to-int (substring args
479 (defun gdb-prompt (instance ignored)
480 "An annotation handler for `prompt'.
481 This sends the next command (if any) to gdb."
482 (let ((sink (gdb-instance-output-sink instance)))
485 ((eq sink 'post-emacs)
486 (set-gdb-instance-output-sink instance 'user))
487 ((or (eq sink 'emacs)
488 (eq sink 'pre-emacs))
489 (set-gdb-instance-output-sink instance 'user)
490 (error "Phase error in gdb-prompt (got %s)" sink))
491 (t (set-gdb-instance-output-sink instance 'user))))
492 (let ((highest (gdb-instance-dequeue-input instance)))
494 (gdb-send-item instance highest)
495 (let ((lowest (gdb-instance-dequeue-idle-input instance)))
497 (gdb-send-item instance lowest)
498 (set-gdb-instance-prompting instance t))))))
500 (defun gdb-send-item (instance item)
504 (insert (format "%s\n" item)))))
505 (set-gdb-instance-current-item instance item)
508 (set-gdb-instance-output-sink instance 'user)
509 (process-send-string (gdb-instance-process instance)
512 (gdb-clear-partial-output instance)
513 (set-gdb-instance-output-sink instance 'pre-emacs)
514 (process-send-string (gdb-instance-process instance)
517 (defun gdb-pre-prompt (instance ignored)
518 "An annotation handler for `pre-prompt'.
519 This terminates the collection of output from a previous
520 command if that happens to be in effect."
521 (let ((sink (gdb-instance-output-sink instance)))
525 (set-gdb-instance-output-sink instance 'post-emacs)
527 (car (cdr (gdb-instance-current-item instance)))))
529 (set-buffer (gdb-get-create-instance-buffer
530 instance 'gdb-partial-output-buffer))
532 ((eq sink 'pre-emacs)
533 (set-gdb-instance-output-sink instance 'user)
534 (error "Output sink phase error 1."))
535 ((eq sink 'post-emacs)
536 (set-gdb-instance-output-sink instance 'user)
537 (error "Output sink phase error 2.")))))
539 (defun gdb-post-prompt (instance ignored)
540 "An annotation handler for `pre-prompt'.
541 This begins the collection of output from the current
542 command if that happens to be appropriate."
543 (gdb-invalidate-registers instance ignored)
544 (let ((sink (gdb-instance-output-sink instance)))
547 ((eq sink 'pre-emacs)
548 (set-gdb-instance-output-sink instance 'emacs))
551 (set-gdb-instance-output-sink instance 'user)
552 (error "Output sink phase error 3."))
554 ((eq sink 'post-emacs)
555 (set-gdb-instance-output-sink instance 'user)
556 (error "Output sink phase error 3.")))))
559 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command output-handler)
561 (defun (, name) (instance ignored)
562 (if (and ((, demand-predicate) instance)
563 (not (member '(, name)
564 (gdb-instance-pending-triggers instance))))
566 (gdb-instance-enqueue-idle-input
568 (list (, gdb-command) '(, output-handler)))
569 (set-gdb-instance-pending-triggers
572 (gdb-instance-pending-triggers instance))))))))
574 (defmacro def-gdb-auto-update-handler (name trigger buf-key)
577 (set-gdb-instance-pending-triggers
580 (gdb-instance-pending-triggers instance)))
581 (let ((buf (gdb-get-instance-buffer instance
587 (kill-region (point-min) (point-max))
588 (insert-buffer (gdb-get-create-instance-buffer
590 'gdb-partial-output-buffer))
593 (defmacro def-gdb-auto-updated-buffer
594 (buffer-key trigger-name gdb-command output-handler-name)
597 (def-gdb-auto-update-trigger (, trigger-name)
598 ;; The demand predicate:
600 (gdb-get-instance-buffer instance '(, buffer-key)))
602 (, output-handler-name))
603 (def-gdb-auto-update-handler (, output-handler-name)
604 (, trigger-name) (, buffer-key)))))
608 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
609 ;; This defines the auto update rule for buffers of type
610 ;; `gdb-breakpoints-buffer'.
612 ;; It defines a function to serve as the annotation handler that
613 ;; handles the `foo-invalidated' message. That function is called:
614 gdb-invalidate-breakpoints
616 ;; To update the buffer, this command is sent to gdb.
617 "server info breakpoints\n"
619 ;; This also defines a function to be the handler for the output
620 ;; from the command above. That function will copy the output into
621 ;; the appropriately typed buffer. That function will be called:
622 gdb-info-breakpoints-handler)
624 (def-gdb-auto-updated-buffer gdb-frames-buffer
625 gdb-invalidate-frames
627 gdb-info-frames-handler)
630 (def-gdb-auto-updated-buffer gdb-registers-buffer
631 gdb-invalidate-registers
632 "server info registers\n"
633 gdb-info-registers-handler)
636 ;; At any given time, output from gdb is being directed
637 ;; one of three places. By default, it goes into the gdb
638 ;; interaction buffer. For commands executed on behalf
639 ;; of emacs, it goes into a scratch buffer (not `the').
640 ;; Finally, some gdb output is simply thrown away; for example,
641 ;; the prompt that follows output from a command executed
645 (defvar gdb-output-sink 'user
646 "An buffer-local indication of how output from an inferior gdb
647 should be directed. Legit values are:
649 USER -- the output should be appended to the gud
652 PRE-EMACS -- throw away output preceding output for emacs.
653 EMACS -- redirect output to the partial-output buffer.
654 POST-EMACS -- throw away output following output for emacs.")
656 (defun gdb-output-burst (instance string)
657 "Handle a burst of output from a gdb instance.
658 This function is (indirectly) used as a gud-marker-filter.
659 It must return output (if any) to be insterted in the gud
664 ;; Recall the left over burst from last time
665 (burst (concat (gdb-instance-burst instance) string))
666 ;; Start accumulating output for the gud buffer
669 ;; Process all the complete markers in this chunk.
671 (while (string-match "\n\032\032\\(.*\\)\n" burst)
672 (let ((annotation (substring burst
676 ;; Stuff prior to the match is just ordinary output.
677 ;; It is either concatenated to OUTPUT or directed
683 (substring burst 0 (match-beginning 0))))
685 ;; Take that stuff off the burst.
686 (setq burst (substring burst (match-end 0)))
688 ;; Parse the tag from the annotation, and maybe its arguments.
689 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
690 (let* ((annotation-type (substring annotation
693 (annotation-arguments (substring annotation
696 (annotation-rule (assoc annotation-type
697 gdb-annotation-rules)))
698 ;; Call the handler for this annotation.
700 '(lambda () (insert annotation-type "\n")))
702 (funcall (car (cdr annotation-rule))
704 annotation-arguments)
705 (error "Bogon gdb annotation %s." annotation)))))
708 ;; Does the remaining text end in a partial line?
709 ;; If it does, then keep part of the burst until we get more.
710 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
713 ;; Everything before the potential marker start can be output.
718 (substring burst 0 (match-beginning 0))))
720 ;; Everything after, we save, to combine with later input.
721 (setq burst (substring burst (match-beginning 0))))
723 ;; In case we know the burst contains no partial annotations:
725 (setq output (gdb-concat-output instance output burst))
728 ;; Save the remaining burst for the next call to this function.
729 (set-gdb-instance-burst instance burst)
732 (defun gdb-concat-output (instance so-far new)
733 (let ((sink (gdb-instance-output-sink instance)))
735 ((eq sink 'user) (concat so-far new))
736 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
738 (gdb-append-to-partial-output instance new)
740 (t (error "Bogon output sink %d" sink)))))
742 (defun gdb-append-to-partial-output (instance string)
745 (gdb-get-create-instance-buffer
746 instance 'gdb-partial-output-buffer))
747 (goto-char (point-max))
750 (defun gdb-clear-partial-output (instance)
753 (gdb-get-create-instance-buffer
754 instance 'gdb-partial-output-buffer))
755 (kill-region (point-min) (point-max))))
760 ;; These are buffers that display output from gdb (or other
761 ;; information) that we want to filter out from the general gdb
762 ;; interaction buffer. (e.g. the backtrace buffer).
764 ;; The general pattern is that each kind of buffer is associated
765 ;; with a rule to refresh its contents. The rule includes one
766 ;; function to call when it is noticed that the buffer is out of
767 ;; date. Typically, that will queue up an idle command for gdb.
769 ;; Every type of instance buffer is identified by some atom
770 ;; such as gdb-frames-buffer. An instance and one of these
771 ;; atoms uniquely identifies a particular instance buffer.
774 (defun gdb-get-instance-buffer (instance key)
775 "Return the instance buffer for `instance' tagged with type `key'.
776 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
778 (gdb-look-for-tagged-buffer instance key (buffer-list))))
780 (defun gdb-get-create-instance-buffer (instance key)
781 "Create a new gdb instance buffer of the type specified by `key'.
782 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
783 (or (gdb-get-instance-buffer instance key)
784 (let* ((rules (assoc key gdb-instance-buffer-rules-assoc))
785 (name (funcall (gdb-rules-name-maker rules) instance))
786 (new (get-buffer-create name)))
789 (make-variable-buffer-local 'gdb-buffer-type)
790 (setq gdb-buffer-type key)
791 (make-variable-buffer-local 'gdb-buffer-instance)
792 (setq gdb-buffer-instance instance)
795 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
797 (defun gdb-look-for-tagged-buffer (instance key bufs)
799 (set-buffer (car bufs))
800 (or (and (eq gdb-buffer-instance instance)
801 (eq gdb-buffer-type key)
803 (gdb-look-for-tagged-buffer instance key (cdr bufs)))))
805 (defun gdb-instance-buffer-p (buf)
809 (not (eq gdb-buffer-type 'gud)))))
812 ;; partial-output buffers
814 ;; These accumulate output from a command executed on
815 ;; behalf of emacs (rather than the user). When the
816 ;; output is complete, the hooks bound to `gdb-command-complete-hooks'
817 ;; are called (and then cleared). Usually these hooks are not
818 ;; set directly but rather implicitly according to the
819 ;; instance-buffer rules.
822 (defun gdb-partial-output-name (instance)
823 (concat "*partial-output-"
824 (gdb-instance-target-string instance)
831 (defun gdb-frames-buffer-name (instance)
833 (set-buffer (process-buffer (gdb-instance-process instance)))
834 (concat "*stack frames of "
835 (gdb-instance-target-string instance) "*")))
837 (defun gud-display-frames-buffer (instance)
838 (interactive (list (gdb-needed-default-instance)))
840 (gdb-get-create-instance-buffer instance
841 'gdb-frames-buffer)))
844 ;; Breakpoint buffers
847 (defun gdb-breakpoints-buffer-name (instance)
849 (set-buffer (process-buffer (gdb-instance-process instance)))
850 (concat "*breakpoints of " (gdb-instance-target-string instance) "*")))
852 (defun gud-display-breakpoints-buffer (instance)
853 (interactive (list (gdb-needed-default-instance)))
855 (gdb-get-create-instance-buffer instance
856 'gdb-breakpoints-buffer)))
863 (defun gdb-registers-buffer-name (instance)
865 (set-buffer (process-buffer (gdb-instance-process instance)))
866 (concat "*registers of " (gdb-instance-target-string instance) "*")))
868 (defun gud-display-registers-buffer (instance)
869 (interactive (list (gdb-needed-default-instance)))
871 (gdb-get-create-instance-buffer instance
872 'gdb-registers-buffer)))
877 (defun gud-gdb-find-file (f)
878 (find-file-noselect f))
881 (defun gdb (command-line)
882 "Run gdb on program FILE in buffer *gud-FILE*.
883 The directory containing FILE becomes the initial working directory
884 and source-file directory for your debugger."
886 (list (read-from-minibuffer "Run gdb (like this): "
887 (if (consp gud-gdb-history)
888 (car gud-gdb-history)
889 "/offsite/kingdon/devo/s4/gdb/gdb ")
891 '(gud-gdb-history . 1))))
892 (gud-overload-functions
893 '((gud-massage-args . gud-gdb-massage-args)
894 (gud-marker-filter . gud-gdb-marker-filter)
895 (gud-find-file . gud-gdb-find-file)
898 (gud-common-init command-line)
900 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
901 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
902 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
903 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
904 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
905 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
906 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
907 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
908 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
909 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
910 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
912 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
913 (setq comint-input-sender 'gdb-send)
914 (run-hooks 'gdb-mode-hook)
915 (make-gdb-instance (get-buffer-process (current-buffer)))
919 ;; ======================================================================
922 ;;; History of argument lists passed to sdb.
923 (defvar gud-sdb-history nil)
925 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
926 "If nil, we're on a System V Release 4 and don't need the tags hack.")
928 (defvar gud-sdb-lastfile nil)
930 (defun gud-sdb-massage-args (file args)
933 (defun gud-sdb-marker-filter (string)
935 ;; System V Release 3.2 uses this format
936 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
940 (substring string (match-beginning 2) (match-end 2))
942 (substring string (match-beginning 3) (match-end 3))))))
943 ;; System V Release 4.0
944 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
946 (setq gud-sdb-lastfile
947 (substring string (match-beginning 2) (match-end 2))))
948 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
953 (substring string (match-beginning 1) (match-end 1))))))
955 (setq gud-sdb-lastfile nil)))
958 (defun gud-sdb-find-file (f)
959 (if gud-sdb-needs-tags
960 (find-tag-noselect f)
961 (find-file-noselect f)))
964 (defun sdb (command-line)
965 "Run sdb on program FILE in buffer *gud-FILE*.
966 The directory containing FILE becomes the initial working directory
967 and source-file directory for your debugger."
969 (list (read-from-minibuffer "Run sdb (like this): "
970 (if (consp gud-sdb-history)
971 (car gud-sdb-history)
974 '(gud-sdb-history . 1))))
975 (if (and gud-sdb-needs-tags
976 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
977 (error "The sdb support requires a valid tags table to work."))
978 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
979 (gud-marker-filter . gud-sdb-marker-filter)
980 (gud-find-file . gud-sdb-find-file)
983 (gud-common-init command-line)
985 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
986 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
987 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
988 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
989 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
990 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
991 (gud-def gud-cont "c" "\C-r" "Continue with display.")
992 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
994 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
995 (run-hooks 'sdb-mode-hook)
998 ;; ======================================================================
1001 ;;; History of argument lists passed to dbx.
1002 (defvar gud-dbx-history nil)
1004 (defun gud-dbx-massage-args (file args)
1007 (defun gud-dbx-marker-filter (string)
1008 (if (or (string-match
1009 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1012 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1014 (setq gud-last-frame
1016 (substring string (match-beginning 2) (match-end 2))
1018 (substring string (match-beginning 1) (match-end 1))))))
1021 (defun gud-dbx-find-file (f)
1022 (find-file-noselect f))
1025 (defun dbx (command-line)
1026 "Run dbx on program FILE in buffer *gud-FILE*.
1027 The directory containing FILE becomes the initial working directory
1028 and source-file directory for your debugger."
1030 (list (read-from-minibuffer "Run dbx (like this): "
1031 (if (consp gud-dbx-history)
1032 (car gud-dbx-history)
1035 '(gud-dbx-history . 1))))
1036 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args)
1037 (gud-marker-filter . gud-dbx-marker-filter)
1038 (gud-find-file . gud-dbx-find-file)
1041 (gud-common-init command-line)
1043 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1044 "\C-b" "Set breakpoint at current line.")
1045 ;; (gud-def gud-break "stop at \"%f\":%l"
1046 ;; "\C-b" "Set breakpoint at current line.")
1047 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1048 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1049 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1050 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1051 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1052 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1053 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1054 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1056 (setq comint-prompt-regexp "^[^)]*dbx) *")
1057 (run-hooks 'dbx-mode-hook)
1060 ;; ======================================================================
1061 ;; xdb (HP PARISC debugger) functions
1063 ;;; History of argument lists passed to xdb.
1064 (defvar gud-xdb-history nil)
1066 (defvar gud-xdb-directories nil
1067 "*A list of directories that xdb should search for source code.
1068 If nil, only source files in the program directory
1069 will be known to xdb.
1071 The file names should be absolute, or relative to the directory
1072 containing the executable being debugged.")
1074 (defun gud-xdb-massage-args (file args)
1075 (nconc (let ((directories gud-xdb-directories)
1078 (setq result (cons (car directories) (cons "-d" result)))
1079 (setq directories (cdr directories)))
1080 (nreverse (cons file result)))
1083 (defun gud-xdb-file-name (f)
1084 "Transform a relative pathname to a full pathname in xdb mode"
1086 (if (file-exists-p f)
1087 (setq result (expand-file-name f))
1088 (let ((directories gud-xdb-directories))
1090 (let ((path (concat (car directories) "/" f)))
1091 (if (file-exists-p path)
1092 (setq result (expand-file-name path)
1094 (setq directories (cdr directories)))))
1097 ;; xdb does not print the lines all at once, so we have to accumulate them
1098 (defvar gud-xdb-accumulation "")
1100 (defun gud-xdb-marker-filter (string)
1102 (if (or (string-match comint-prompt-regexp string)
1103 (string-match ".*\012" string))
1104 (setq result (concat gud-xdb-accumulation string)
1105 gud-xdb-accumulation "")
1106 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
1108 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
1109 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1111 (let ((line (string-to-int
1112 (substring result (match-beginning 2) (match-end 2))))
1113 (file (gud-xdb-file-name
1114 (substring result (match-beginning 1) (match-end 1)))))
1116 (setq gud-last-frame (cons file line))))))
1119 (defun gud-xdb-find-file (f)
1120 (let ((realf (gud-xdb-file-name f)))
1121 (if realf (find-file-noselect realf))))
1124 (defun xdb (command-line)
1125 "Run xdb on program FILE in buffer *gud-FILE*.
1126 The directory containing FILE becomes the initial working directory
1127 and source-file directory for your debugger.
1129 You can set the variable 'gud-xdb-directories' to a list of program source
1130 directories if your program contains sources from more than one directory."
1132 (list (read-from-minibuffer "Run xdb (like this): "
1133 (if (consp gud-xdb-history)
1134 (car gud-xdb-history)
1137 '(gud-xdb-history . 1))))
1138 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
1139 (gud-marker-filter . gud-xdb-marker-filter)
1140 (gud-find-file . gud-xdb-find-file)))
1142 (gud-common-init command-line)
1144 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1145 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1146 "Set temporary breakpoint at current line.")
1147 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1148 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1149 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1150 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1151 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1152 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1153 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1154 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1156 (setq comint-prompt-regexp "^>")
1157 (make-local-variable 'gud-xdb-accumulation)
1158 (setq gud-xdb-accumulation "")
1159 (run-hooks 'xdb-mode-hook))
1161 ;; ======================================================================
1164 ;;; History of argument lists passed to perldb.
1165 (defvar gud-perldb-history nil)
1167 (defun gud-perldb-massage-args (file args)
1168 (cons "-d" (cons file (cons "-emacs" args))))
1170 ;; There's no guarantee that Emacs will hand the filter the entire
1171 ;; marker at once; it could be broken up across several strings. We
1172 ;; might even receive a big chunk with several markers in it. If we
1173 ;; receive a chunk of text which looks like it might contain the
1174 ;; beginning of a marker, we save it here between calls to the
1176 (defvar gud-perldb-marker-acc "")
1178 (defun gud-perldb-marker-filter (string)
1180 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
1183 ;; Process all the complete markers in this chunk.
1184 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1185 gud-perldb-marker-acc)
1188 ;; Extract the frame position from the marker.
1190 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
1191 (string-to-int (substring gud-perldb-marker-acc
1195 ;; Append any text before the marker to the output we're going
1196 ;; to return - we don't include the marker in this text.
1197 output (concat output
1198 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
1200 ;; Set the accumulator to the remaining text.
1201 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
1203 ;; Does the remaining text look like it might end with the
1204 ;; beginning of another marker? If it does, then keep it in
1205 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
1206 ;; know the full marker regexp above failed, it's pretty simple to
1207 ;; test for marker starts.
1208 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
1210 ;; Everything before the potential marker start can be output.
1211 (setq output (concat output (substring gud-perldb-marker-acc
1212 0 (match-beginning 0))))
1214 ;; Everything after, we save, to combine with later input.
1215 (setq gud-perldb-marker-acc
1216 (substring gud-perldb-marker-acc (match-beginning 0))))
1218 (setq output (concat output gud-perldb-marker-acc)
1219 gud-perldb-marker-acc ""))
1223 (defun gud-perldb-find-file (f)
1224 (find-file-noselect f))
1227 (defun perldb (command-line)
1228 "Run perldb on program FILE in buffer *gud-FILE*.
1229 The directory containing FILE becomes the initial working directory
1230 and source-file directory for your debugger."
1232 (list (read-from-minibuffer "Run perldb (like this): "
1233 (if (consp gud-perldb-history)
1234 (car gud-perldb-history)
1237 '(gud-perldb-history . 1))))
1238 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
1239 (gud-marker-filter . gud-perldb-marker-filter)
1240 (gud-find-file . gud-perldb-find-file)
1243 (gud-common-init command-line)
1245 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1246 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1247 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1248 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1249 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1250 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1251 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1252 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1253 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1255 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1256 (run-hooks 'perldb-mode-hook)
1260 ;; End of debugger-specific information
1264 ;;; When we send a command to the debugger via gud-call, it's annoying
1265 ;;; to see the command and the new prompt inserted into the debugger's
1266 ;;; buffer; we have other ways of knowing the command has completed.
1268 ;;; If the buffer looks like this:
1269 ;;; --------------------
1270 ;;; (gdb) set args foo bar
1272 ;;; --------------------
1273 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1274 ;;; source file to set a breakpoint, we want the buffer to end up like
1276 ;;; --------------------
1277 ;;; (gdb) set args foo bar
1278 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1280 ;;; --------------------
1281 ;;; Essentially, the old prompt is deleted, and the command's output
1282 ;;; and the new prompt take its place.
1284 ;;; Not echoing the command is easy enough; you send it directly using
1285 ;;; process-send-string, and it never enters the buffer. However,
1286 ;;; getting rid of the old prompt is trickier; you don't want to do it
1287 ;;; when you send the command, since that will result in an annoying
1288 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1289 ;;; waits for a response from the debugger, and the new prompt is
1290 ;;; inserted. Instead, we'll wait until we actually get some output
1291 ;;; from the subprocess before we delete the prompt. If the command
1292 ;;; produced no output other than a new prompt, that prompt will most
1293 ;;; likely be in the first chunk of output received, so we will delete
1294 ;;; the prompt and then replace it with an identical one. If the
1295 ;;; command produces output, the prompt is moving anyway, so the
1296 ;;; flicker won't be annoying.
1298 ;;; So - when we want to delete the prompt upon receipt of the next
1299 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1300 ;;; the start of the prompt; the process filter will notice this, and
1301 ;;; delete all text between it and the process output marker. If
1302 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1304 (defvar gud-delete-prompt-marker nil)
1307 (defvar gdbish-comint-mode-map (copy-keymap comint-mode-map))
1308 (define-key gdbish-comint-mode-map "\C-c\M-\C-r" 'gud-display-registers-buffer)
1309 (define-key gdbish-comint-mode-map "\C-c\M-\C-f" 'gud-display-frames-buffer)
1310 (define-key gdbish-comint-mode-map "\C-c\M-\C-b" 'gud-display-breakpoints-buffer)
1313 "Major mode for interacting with an inferior debugger process.
1315 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1316 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
1317 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
1319 After startup, the following commands are available in both the GUD
1320 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1323 \\[gud-break] sets a breakpoint at the current file and line. In the
1324 GUD buffer, the current file and line are those of the last breakpoint or
1325 step. In a source buffer, they are the buffer's file and current line.
1327 \\[gud-remove] removes breakpoints on the current file and line.
1329 \\[gud-refresh] displays in the source window the last line referred to
1332 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1333 step-one-line (not entering function calls), and step-one-instruction
1334 and then update the source window with the current file and position.
1335 \\[gud-cont] continues execution.
1337 \\[gud-print] tries to find the largest C lvalue or function-call expression
1338 around point, and sends it to the debugger for value display.
1340 The above commands are common to all supported debuggers except xdb which
1341 does not support stepping instructions.
1343 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1344 except that the breakpoint is temporary; that is, it is removed when
1345 execution stops on it.
1347 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1348 frame. \\[gud-down] drops back down through one.
1350 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1351 the current function and stops.
1353 All the keystrokes above are accessible in the GUD buffer
1354 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1356 All pre-defined functions for which the concept make sense repeat
1357 themselves the appropriate number of times if you give a prefix
1360 You may use the `gud-def' macro in the initialization hook to define other
1363 Other commands for interacting with the debugger process are inherited from
1364 comint mode, which see."
1367 (setq major-mode 'gud-mode)
1368 (setq mode-name "Debugger")
1369 (setq mode-line-process '(": %s"))
1370 (use-local-map (copy-keymap gdbish-comint-mode-map))
1371 (make-local-variable 'gud-last-frame)
1372 (setq gud-last-frame nil)
1373 (make-local-variable 'comint-prompt-regexp)
1374 (make-local-variable 'gud-delete-prompt-marker)
1375 (setq gud-delete-prompt-marker (make-marker))
1376 (run-hooks 'gud-mode-hook)
1379 (defvar gud-comint-buffer nil)
1381 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1382 (defun gud-chop-words (string)
1384 (len (length string))
1387 (if (memq (aref string i) '(?\t ? ))
1389 (setq words (cons (substring string beg i) words)
1391 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1392 (setq beg (1+ beg)))
1396 (setq words (cons (substring string beg) words)))
1399 (defvar gud-target-name "--unknown--"
1400 "The apparent name of the program being debugged in a gud buffer.
1401 For sure this the root string used in smashing together the gud
1402 buffer's name, even if that doesn't happen to be the name of a
1405 ;; Perform initializations common to all debuggers.
1406 (defun gud-common-init (command-line)
1407 (let* ((words (gud-chop-words command-line))
1408 (program (car words))
1409 (file-word (let ((w (cdr words)))
1410 (while (and w (= ?- (aref (car w) 0)))
1413 (args (delq file-word (cdr words)))
1414 (file (expand-file-name file-word))
1415 (filepart (file-name-nondirectory file))
1416 (buffer-name (concat "*gud-" filepart "*")))
1417 (switch-to-buffer buffer-name)
1418 (setq default-directory (file-name-directory file))
1419 (or (bolp) (newline))
1420 (insert "Current directory is " default-directory "\n")
1421 (apply 'make-comint (concat "gud-" filepart) program nil
1422 (gud-massage-args file args))
1424 (make-variable-buffer-local 'gud-target-name)
1425 (setq gud-target-name filepart))
1426 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1427 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
1431 (defun gud-set-buffer ()
1432 (cond ((eq major-mode 'gud-mode)
1433 (setq gud-comint-buffer (current-buffer)))))
1435 ;; These functions are responsible for inserting output from your debugger
1436 ;; into the buffer. The hard work is done by the method that is
1437 ;; the value of gud-marker-filter.
1439 (defun gud-filter (proc string)
1440 ;; Here's where the actual buffer insertion is done
1441 (let ((inhibit-quit t))
1443 (set-buffer (process-buffer proc))
1444 (let (moving output-after-point)
1446 (goto-char (process-mark proc))
1447 ;; If we have been so requested, delete the debugger prompt.
1448 (if (marker-buffer gud-delete-prompt-marker)
1450 (delete-region (point) gud-delete-prompt-marker)
1451 (set-marker gud-delete-prompt-marker nil)))
1452 (insert-before-markers (gud-marker-filter string))
1453 (setq moving (= (point) (process-mark proc)))
1454 (setq output-after-point (< (point) (process-mark proc)))
1455 ;; Check for a filename-and-line number.
1456 ;; Don't display the specified file
1457 ;; unless (1) point is at or after the position where output appears
1458 ;; and (2) this buffer is on the screen.
1459 (if (and gud-last-frame
1460 (not output-after-point)
1461 (get-buffer-window (current-buffer)))
1462 (gud-display-frame)))
1463 (if moving (goto-char (process-mark proc)))))))
1465 (defun gud-sentinel (proc msg)
1466 (cond ((null (buffer-name (process-buffer proc)))
1468 ;; Stop displaying an arrow in a source file.
1469 (setq overlay-arrow-position nil)
1470 (set-process-buffer proc nil))
1471 ((memq (process-status proc) '(signal exit))
1472 ;; Stop displaying an arrow in a source file.
1473 (setq overlay-arrow-position nil)
1474 ;; Fix the mode line.
1475 (setq mode-line-process
1477 (symbol-name (process-status proc))))
1478 (let* ((obuf (current-buffer)))
1479 ;; save-excursion isn't the right thing if
1480 ;; process-buffer is current-buffer
1483 ;; Write something in *compilation* and hack its mode line,
1484 (set-buffer (process-buffer proc))
1485 ;; Force mode line redisplay soon
1486 (set-buffer-modified-p (buffer-modified-p))
1488 (insert ?\n mode-name " " msg)
1490 (goto-char (point-max))
1491 (insert ?\n mode-name " " msg)))
1492 ;; If buffer and mode line will show that the process
1493 ;; is dead, we can delete it now. Otherwise it
1494 ;; will stay around until M-x list-processes.
1495 (delete-process proc))
1496 ;; Restore old buffer, but don't restore old point
1497 ;; if obuf is the gud buffer.
1498 (set-buffer obuf))))))
1500 (defun gud-display-frame ()
1501 "Find and obey the last filename-and-line marker from the debugger.
1502 Obeying it means displaying in another window the specified file and line."
1507 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
1508 (setq gud-last-last-frame gud-last-frame
1509 gud-last-frame nil))))
1511 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1512 ;; and that its line LINE is visible.
1513 ;; Put the overlay-arrow on the line LINE in that buffer.
1514 ;; Most of the trickiness in here comes from wanting to preserve the current
1515 ;; region-restriction if that's possible. We use an explicit display-buffer
1516 ;; to get around the fact that this is called inside a save-excursion.
1518 (defun gud-display-line (true-file line)
1519 (let* ((buffer (gud-find-file true-file))
1520 (window (display-buffer buffer))
1522 ;;; (if (equal buffer (current-buffer))
1524 ;;; (setq buffer-read-only nil))
1526 ;;; (setq buffer-read-only t)
1532 (setq overlay-arrow-string "=>")
1533 (or overlay-arrow-position
1534 (setq overlay-arrow-position (make-marker)))
1535 (set-marker overlay-arrow-position (point) (current-buffer)))
1536 (cond ((or (< pos (point-min)) (> pos (point-max)))
1539 (set-window-point window overlay-arrow-position)))
1541 ;;; The gud-call function must do the right thing whether its invoking
1542 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1543 ;;; or a C buffer. In the former case, we want to supply data from
1544 ;;; gud-last-frame. Here's how we do it:
1546 (defun gud-format-command (str arg)
1547 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
1548 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
1550 (substring str (match-beginning 1) (match-end 1))
1551 (file-name-nondirectory (if insource
1553 (car gud-last-frame)))
1554 (substring str (match-beginning 2) (match-end 2)))))
1555 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
1557 (substring str (match-beginning 1) (match-end 1))
1558 (file-name-directory (if insource
1560 (car gud-last-frame)))
1561 (substring str (match-beginning 2) (match-end 2)))))
1562 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
1564 (substring str (match-beginning 1) (match-end 1))
1568 (save-restriction (widen)
1569 (1+ (count-lines 1 (point)))))
1570 (cdr gud-last-frame))
1571 (substring str (match-beginning 2) (match-end 2)))))
1572 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
1574 (substring str (match-beginning 1) (match-end 1))
1576 (substring str (match-beginning 2) (match-end 2)))))
1577 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
1579 (substring str (match-beginning 1) (match-end 1))
1581 (substring str (match-beginning 2) (match-end 2)))))
1582 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
1584 (substring str (match-beginning 1) (match-end 1))
1585 (if arg (int-to-string arg) "")
1586 (substring str (match-beginning 2) (match-end 2)))))
1591 (defun gud-read-address ()
1592 "Return a string containing the core-address found in the buffer at point."
1594 (let ((pt (point)) found begin)
1595 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1597 (found (forward-char 2)
1598 (buffer-substring found
1599 (progn (re-search-forward "[^0-9a-f]")
1602 (t (setq begin (progn (re-search-backward "[^0-9]")
1606 (re-search-forward "[^0-9]")
1608 (buffer-substring begin (point)))))))
1610 (defun gud-call (fmt &optional arg)
1611 (let ((msg (gud-format-command fmt arg)))
1612 (message "Command: %s" msg)
1614 (gud-basic-call msg)))
1616 (defun gud-basic-call (command)
1617 "Invoke the debugger COMMAND displaying source in other window."
1620 (let ((command (concat command "\n"))
1621 (proc (get-buffer-process gud-comint-buffer)))
1623 ;; Arrange for the current prompt to get deleted.
1625 (set-buffer gud-comint-buffer)
1626 (goto-char (process-mark proc))
1628 (if (looking-at comint-prompt-regexp)
1629 (set-marker gud-delete-prompt-marker (point))))
1630 (process-send-string proc command)))
1632 (defun gud-refresh (&optional arg)
1633 "Fix up a possibly garbled display, and redraw the arrow."
1636 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1637 (gud-display-frame))
1639 ;;; Code for parsing expressions out of C code. The single entry point is
1640 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1642 ;;; The rest of this file is a hacked version of gdbsrc.el by
1643 ;;; Debby Ayers <ayers@asc.slb.com>,
1644 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1646 (defun find-c-expr ()
1647 "Returns the C expr that surrounds point."
1650 (let ((p) (expr) (test-expr))
1652 (setq expr (expr-cur))
1653 (setq test-expr (expr-prev))
1654 (while (expr-compound test-expr expr)
1655 (setq expr (cons (car test-expr) (cdr expr)))
1656 (goto-char (car expr))
1657 (setq test-expr (expr-prev)))
1659 (setq test-expr (expr-next))
1660 (while (expr-compound expr test-expr)
1661 (setq expr (cons (car expr) (cdr test-expr)))
1662 (setq test-expr (expr-next))
1664 (buffer-substring (car expr) (cdr expr)))))
1667 "Returns the expr that point is in; point is set to beginning of expr.
1668 The expr is represented as a cons cell, where the car specifies the point in
1669 the current buffer that marks the beginning of the expr and the cdr specifies
1670 the character after the end of the expr."
1671 (let ((p (point)) (begin) (end))
1672 (expr-backward-sexp)
1673 (setq begin (point))
1687 (defun expr-backward-sexp ()
1688 "Version of `backward-sexp' that catches errors."
1693 (defun expr-forward-sexp ()
1694 "Version of `forward-sexp' that catches errors."
1700 "Returns the previous expr, point is set to beginning of that expr.
1701 The expr is represented as a cons cell, where the car specifies the point in
1702 the current buffer that marks the beginning of the expr and the cdr specifies
1703 the character after the end of the expr"
1704 (let ((begin) (end))
1705 (expr-backward-sexp)
1706 (setq begin (point))
1713 "Returns the following expr, point is set to beginning of that expr.
1714 The expr is represented as a cons cell, where the car specifies the point in
1715 the current buffer that marks the beginning of the expr and the cdr specifies
1716 the character after the end of the expr."
1717 (let ((begin) (end))
1721 (expr-backward-sexp)
1722 (setq begin (point))
1725 (defun expr-compound-sep (span-start span-end)
1726 "Returns '.' for '->' & '.', returns ' ' for white space,
1727 returns '?' for other punctuation."
1730 (while (< span-start span-end)
1731 (setq syntax (char-syntax (char-after span-start)))
1734 ((= syntax ?.) (setq syntax (char-after span-start))
1736 ((= syntax ?.) (setq result ?.))
1737 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1739 (setq span-start (+ span-start 1)))
1740 (t (setq span-start span-end)
1741 (setq result ??)))))
1742 (setq span-start (+ span-start 1)))
1745 (defun expr-compound (first second)
1746 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1747 The two exprs are represented as a cons cells, where the car
1748 specifies the point in the current buffer that marks the beginning of the
1749 expr and the cdr specifies the character after the end of the expr.
1750 Link exprs of the form:
1757 (let ((span-start (cdr first))
1758 (span-end (car second))
1760 (setq syntax (expr-compound-sep span-start span-end))
1762 ((= (car first) (car second)) nil)
1763 ((= (cdr first) (cdr second)) nil)
1766 (setq span-start (char-after (- span-start 1)))
1767 (setq span-end (char-after span-end))
1769 ((= span-start ?) ) t )
1770 ((= span-start ?] ) t )
1771 ((= span-end ?( ) t )
1772 ((= span-end ?[ ) t )
1779 ;;; gud.el ends here
1781 (defun toggle-bp-this-line ()
1784 (let (bp-num bp-state)
1785 (beginning-of-line 1)
1786 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
1787 (error "Not recognized as breakpoint line (demo foo).")
1788 (process-send-string
1789 gdb-buffer-process!!!
1791 (if (eq ?y (char-after (match-beginning 2)))
1794 (buffer-substring (match-beginning 0)