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
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.
42 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
46 ;; added support for xdb (HPUX debugger).
48 ;; Cygnus Support added support for gdb's --annotate=2.
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 (setq last-proc proc)
187 (let ((instance (cons 'gdb-instance proc)))
189 (set-buffer (process-buffer proc))
190 (setq gdb-buffer-instance instance)
192 (mapcar 'make-variable-buffer-local gdb-instance-variables)
193 (setq gdb-buffer-type 'gud)
194 ;; If we're taking over the buffer of another process,
195 ;; take over it's ancillery buffers as well.
197 (let ((dead (or old-gdb-buffer-instance)))
203 (if (eq dead gdb-buffer-instance)
204 (setq gdb-buffer-instance instance)))))
208 (defun gdb-instance-process (inst) (cdr inst))
210 ;;; The list of instance variables is built up by the expansions of
213 (defvar gdb-instance-variables '()
214 "A list of variables that are local to the gud buffer associated
215 with a gdb instance.")
217 (defmacro def-gdb-variable
218 (name accessor setter &optional default doc)
221 (defvar (, name) (, default) (, (or doc "undocumented")))
222 (if (not (memq '(, name) gdb-instance-variables))
223 (setq gdb-instance-variables
224 (cons '(, name) gdb-instance-variables)))
227 (defun (, accessor) (instance)
229 ((buffer (gdb-get-instance-buffer instance 'gud)))
236 (defun (, setter) (instance val)
238 ((buffer (gdb-get-instance-buffer instance 'gud)))
242 (setq (, name) val)))))))))))
244 (defmacro def-gdb-var (root-symbol &optional default doc)
245 (let* ((root (symbol-name root-symbol))
246 (accessor (intern (concat "gdb-instance-" root)))
247 (setter (intern (concat "set-gdb-instance-" root)))
248 (var-name (intern (concat "gdb-" root))))
250 (, var-name) (, accessor) (, setter)
251 (, default) (, doc)))))
253 (def-gdb-var buffer-instance nil
254 "In an instance buffer, the buffer's instance.")
256 (def-gdb-var buffer-type nil
257 "One of the symbols bound in gdb-instance-buffer-rules")
259 (def-gdb-var burst ""
260 "A string of characters from gdb that have not yet been processed.")
262 (def-gdb-var input-queue ()
263 "A list of high priority gdb command objects.")
265 (def-gdb-var idle-input-queue ()
266 "A list of low priority gdb command objects.")
268 (def-gdb-var prompting nil
269 "True when gdb is idle with no pending input.")
271 (def-gdb-var output-sink 'user
272 "The disposition of the output of the current gdb command.
273 Possible values are these symbols:
275 user -- gdb output should be copied to the gud buffer
278 pre-emacs -- output should be ignored util the post-prompt
279 annotation is received. Then the output-sink
281 emacs -- output should be collected in the partial-output-buffer
282 for subsequent processing by a command. This is the
283 disposition of output generated by commands that
284 gud mode sends to gdb on its own behalf.
285 post-emacs -- ignore input until the prompt annotation is
286 received, then go to USER disposition.
289 (def-gdb-var current-item nil
290 "The most recent command item sent to gdb.")
292 (def-gdb-var pending-triggers '()
293 "A list of trigger functions that have run later than their output
296 (defun in-gdb-instance-context (instance form)
297 "Funcall `form' in the gud buffer of `instance'"
299 (set-buffer (gdb-get-instance-buffer instance 'gud))
302 ;; end of instance vars
308 (defun gdb-proc->instance (proc)
310 (set-buffer (process-buffer proc))
311 gdb-buffer-instance))
313 (defun gdb-mru-instance-buffer ()
314 "Return the most recently used (non-auxiliary) gdb gud buffer."
316 (gdb-goto-first-gdb-instance (buffer-list))))
318 (defun gdb-goto-first-gdb-instance (blist)
319 "Use gdb-mru-instance-buffer -- not this."
322 (set-buffer (car blist))
323 (or (and gdb-buffer-instance
324 (eq gdb-buffer-type 'gud)
326 (gdb-goto-first-gdb-instance (cdr blist))))))
328 (defun buffer-gdb-instance (buf)
331 gdb-buffer-instance))
333 (defun gdb-needed-default-instance ()
334 "Return the most recently used gdb instance or signal an error."
335 (let ((buffer (gdb-mru-instance-buffer)))
336 (or (and buffer (buffer-gdb-instance buffer))
337 (error "No instance of gdb found."))))
339 (defun gdb-instance-target-string (instance)
340 "The apparent name of the program being debugged by a gdb instance.
341 For sure this the root string used in smashing together the gud
342 buffer's name, even if that doesn't happen to be the name of a
344 (in-gdb-instance-context
346 (function (lambda () gud-target-name))))
354 ;; More than one buffer can be associated with a gdb instance.
356 ;; Each buffer has a TYPE -- a symbol that identifies the function
357 ;; of that particular buffer.
359 ;; The usual gud interaction buffer is given the type `gud' and
360 ;; is constructed specially.
362 ;; Others are constructed by gdb-get-create-instance-buffer and
363 ;; named according to the rules set forth in the gdb-instance-buffer-rules-assoc
365 (defun gdb-get-instance-buffer (instance key)
366 "Return the instance buffer for `instance' tagged with type `key'.
367 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
369 (gdb-look-for-tagged-buffer instance key (buffer-list))))
371 (defun gdb-get-create-instance-buffer (instance key)
372 "Create a new gdb instance buffer of the type specified by `key'.
373 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
374 (or (gdb-get-instance-buffer instance key)
375 (let* ((rules (assoc key gdb-instance-buffer-rules-assoc))
376 (name (funcall (gdb-rules-name-maker rules) instance))
377 (new (get-buffer-create name)))
380 (make-variable-buffer-local 'gdb-buffer-type)
381 (setq gdb-buffer-type key)
382 (make-variable-buffer-local 'gdb-buffer-instance)
383 (setq gdb-buffer-instance instance)
384 (if (cdr (cdr rules))
385 (funcall (car (cdr (cdr rules)))))
388 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
390 (defun gdb-look-for-tagged-buffer (instance key bufs)
392 (while (and (not retval) bufs)
393 (set-buffer (car bufs))
394 (if (and (eq gdb-buffer-instance instance)
395 (eq gdb-buffer-type key))
396 (setq retval (car bufs)))
397 (setq bufs (cdr bufs))
401 (defun gdb-instance-buffer-p (buf)
405 (not (eq gdb-buffer-type 'gud)))))
408 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
409 ;; at least one and possible more functions. The functions have these
410 ;; roles in defining a buffer type:
412 ;; NAME - take an instance, return a name for this type buffer for that
414 ;; The remaining function(s) are optional:
416 ;; MODE - called in new new buffer with no arguments, should establish
417 ;; the proper mode for the buffer.
420 (defvar gdb-instance-buffer-rules-assoc '())
422 (defun gdb-set-instance-buffer-rules (buffer-type &rest rules)
423 (let ((binding (assoc buffer-type gdb-instance-buffer-rules-assoc)))
425 (setcdr binding rules)
426 (setq gdb-instance-buffer-rules-assoc
427 (cons (cons buffer-type rules)
428 gdb-instance-buffer-rules-assoc)))))
430 (gdb-set-instance-buffer-rules 'gud 'error) ; gud buffers are an exception to the rules
433 ;; partial-output buffers
435 ;; These accumulate output from a command executed on
436 ;; behalf of emacs (rather than the user).
439 (gdb-set-instance-buffer-rules 'gdb-partial-output-buffer
440 'gdb-partial-output-name)
442 (defun gdb-partial-output-name (instance)
443 (concat "*partial-output-"
444 (gdb-instance-target-string instance)
450 ;; gdb communications
453 ;; INPUT: things sent to gdb
455 ;; Each instance has a high and low priority
456 ;; input queue. Low priority input is sent only
457 ;; when the high priority queue is idle.
459 ;; The queues are lists. Each element is either
460 ;; a string (indicating user or user-like input)
461 ;; or a list of the form:
463 ;; (INPUT-STRING HANDLER-FN)
466 ;; The handler function will be called from the
467 ;; partial-output buffer when the command completes.
468 ;; This is the way to write commands which
469 ;; invoke gdb commands autonomously.
471 ;; These lists are consumed tail first.
474 (defun gdb-send (proc string)
475 "A comint send filter for gdb.
476 This filter may simply queue output for a later time."
477 (let ((instance (gdb-proc->instance proc)))
478 (gdb-instance-enqueue-input instance (concat string "\n"))))
480 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
481 ;; is a query, or other non-top-level prompt. To guarantee stuff will get
482 ;; sent to the top-level prompt, currently it must be put in the idle queue.
484 ;; [This should encourage gud extentions that invoke gdb commands to let
485 ;; the user go first; it is not a bug. -t]
488 (defun gdb-instance-enqueue-input (instance item)
489 (if (gdb-instance-prompting instance)
491 (gdb-send-item instance item)
492 (set-gdb-instance-prompting instance nil))
493 (set-gdb-instance-input-queue
495 (cons item (gdb-instance-input-queue instance)))))
497 (defun gdb-instance-dequeue-input (instance)
498 (let ((queue (gdb-instance-input-queue instance)))
500 (if (not (cdr queue))
501 (let ((answer (car queue)))
502 (set-gdb-instance-input-queue instance '())
504 (gdb-take-last-elt queue)))))
506 (defun gdb-instance-enqueue-idle-input (instance item)
507 (if (and (gdb-instance-prompting instance)
508 (not (gdb-instance-input-queue instance)))
510 (gdb-send-item instance item)
511 (set-gdb-instance-prompting instance nil))
512 (set-gdb-instance-idle-input-queue
514 (cons item (gdb-instance-idle-input-queue instance)))))
516 (defun gdb-instance-dequeue-idle-input (instance)
517 (let ((queue (gdb-instance-idle-input-queue instance)))
519 (if (not (cdr queue))
520 (let ((answer (car queue)))
521 (set-gdb-instance-idle-input-queue instance '())
523 (gdb-take-last-elt queue)))))
525 ; Don't use this in general.
526 (defun gdb-take-last-elt (l)
528 (gdb-take-last-elt (cdr l))
529 (let ((answer (car (cdr l))))
535 ;; output -- things gdb prints to emacs
537 ;; GDB output is a stream interrupted by annotations.
538 ;; Annotations can be recognized by their beginning
539 ;; with \C-j\C-z\C-z<tag><opt>\C-j
541 ;; The tag is a string obeying symbol syntax.
543 ;; The optional part `<opt>' can be either the empty string
544 ;; or a space followed by more data relating to the annotation.
545 ;; For example, the SOURCE annotation is followed by a filename,
546 ;; line number and various useless goo. This data must not include
551 (defun gud-gdb-marker-filter (string)
552 "A gud marker filter for gdb."
553 ;; Bogons don't tell us the process except through scoping crud.
554 (let ((instance (gdb-proc->instance proc)))
555 (gdb-output-burst instance string)))
557 (defvar gdb-annotation-rules
558 '(("frames-invalid" gdb-invalidate-frames)
559 ("breakpoints-invalid" gdb-invalidate-breakpoints)
560 ("pre-prompt" gdb-pre-prompt)
561 ("prompt" gdb-prompt)
562 ("commands" gdb-subprompt)
563 ("overload-choice" gdb-subprompt)
564 ("query" gdb-subprompt)
565 ("prompt-for-continue" gdb-subprompt)
566 ("post-prompt" gdb-post-prompt)
567 ("source" gdb-source)
569 "An assoc mapping annotation tags to functions which process them.")
572 (defun gdb-ignore-annotation (instance args)
575 (defconst gdb-source-spec-regexp
576 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x[a-f0-9]*")
578 ;; Do not use this except as an annotation handler."
579 (defun gdb-source (instance args)
580 (string-match gdb-source-spec-regexp args)
581 ;; Extract the frame position from the marker.
584 (substring args (match-beginning 1) (match-end 1))
585 (string-to-int (substring args
589 ;; An annotation handler for `prompt'.
590 ;; This sends the next command (if any) to gdb.
591 (defun gdb-prompt (instance ignored)
592 (let ((sink (gdb-instance-output-sink instance)))
595 ((eq sink 'post-emacs)
596 (set-gdb-instance-output-sink instance 'user))
597 ((or (eq sink 'emacs)
598 (eq sink 'pre-emacs))
599 (set-gdb-instance-output-sink instance 'user)
600 (error "Phase error in gdb-prompt (got %s)" sink))
601 (t (set-gdb-instance-output-sink instance 'user))))
602 (let ((highest (gdb-instance-dequeue-input instance)))
604 (gdb-send-item instance highest)
605 (let ((lowest (gdb-instance-dequeue-idle-input instance)))
607 (gdb-send-item instance lowest)
609 (set-gdb-instance-prompting instance t)
610 (gud-display-frame)))))))
612 ;; An annotation handler for non-top-level prompts.
613 (defun gdb-subprompt (instance ignored)
614 (let ((highest (gdb-instance-dequeue-input instance)))
616 (gdb-send-item instance highest)
617 (set-gdb-instance-prompting instance t))))
619 (defun gdb-send-item (instance item)
620 (set-gdb-instance-current-item instance item)
623 (set-gdb-instance-output-sink instance 'user)
624 (process-send-string (gdb-instance-process instance)
627 (gdb-clear-partial-output instance)
628 (set-gdb-instance-output-sink instance 'pre-emacs)
629 (process-send-string (gdb-instance-process instance)
632 ;; An annotation handler for `pre-prompt'.
633 ;; This terminates the collection of output from a previous
634 ;; command if that happens to be in effect.
635 (defun gdb-pre-prompt (instance ignored)
636 (let ((sink (gdb-instance-output-sink instance)))
640 (set-gdb-instance-output-sink instance 'post-emacs)
642 (car (cdr (gdb-instance-current-item instance)))))
644 (set-buffer (gdb-get-create-instance-buffer
645 instance 'gdb-partial-output-buffer))
647 ((eq sink 'pre-emacs)
648 (set-gdb-instance-output-sink instance 'user)
649 (error "Output sink phase error 1."))
650 ((eq sink 'post-emacs)
651 (set-gdb-instance-output-sink instance 'user)
652 (error "Output sink phase error 2.")))))
654 ;; An annotation handler for `post-prompt'.
655 ;; This begins the collection of output from the current
656 ;; command if that happens to be appropriate."
657 (defun gdb-post-prompt (instance ignored)
658 (gdb-invalidate-registers instance ignored)
659 (let ((sink (gdb-instance-output-sink instance)))
662 ((eq sink 'pre-emacs)
663 (set-gdb-instance-output-sink instance 'emacs))
666 (set-gdb-instance-output-sink instance 'user)
667 (error "Output sink phase error 3."))
669 ((eq sink 'post-emacs)
670 (set-gdb-instance-output-sink instance 'user)
671 (error "Output sink phase error 3.")))))
673 ;; A buffer-local indication of how output from an inferior gdb
674 ;; should be directed. Legit values are:
676 ;; USER -- the output should be appended to the gud
679 ;; PRE-EMACS -- throw away output preceding output for emacs.
680 ;; EMACS -- redirect output to the partial-output buffer.
681 ;; POST-EMACS -- throw away output following output for emacs."
684 ;; Handle a burst of output from a gdb instance.
685 ;; This function is (indirectly) used as a gud-marker-filter.
686 ;; It must return output (if any) to be insterted in the gud
689 (defun gdb-output-burst (instance string)
690 "Handle a burst of output from a gdb instance.
691 This function is (indirectly) used as a gud-marker-filter.
692 It must return output (if any) to be insterted in the gud
697 ;; Recall the left over burst from last time
698 (burst (concat (gdb-instance-burst instance) string))
699 ;; Start accumulating output for the gud buffer
702 ;; Process all the complete markers in this chunk.
704 (while (string-match "\n\032\032\\(.*\\)\n" burst)
705 (let ((annotation (substring burst
709 ;; Stuff prior to the match is just ordinary output.
710 ;; It is either concatenated to OUTPUT or directed
716 (substring burst 0 (match-beginning 0))))
718 ;; Take that stuff off the burst.
719 (setq burst (substring burst (match-end 0)))
721 ;; Parse the tag from the annotation, and maybe its arguments.
722 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
723 (let* ((annotation-type (substring annotation
726 (annotation-arguments (substring annotation
729 (annotation-rule (assoc annotation-type
730 gdb-annotation-rules)))
731 ;; Call the handler for this annotation.
733 (funcall (car (cdr annotation-rule))
735 annotation-arguments)
736 ;; Else the annotation is not recognized. Ignore it silently,
737 ;; so that GDB can add new annotations without causing
742 ;; Does the remaining text end in a partial line?
743 ;; If it does, then keep part of the burst until we get more.
744 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
747 ;; Everything before the potential marker start can be output.
752 (substring burst 0 (match-beginning 0))))
754 ;; Everything after, we save, to combine with later input.
755 (setq burst (substring burst (match-beginning 0))))
757 ;; In case we know the burst contains no partial annotations:
759 (setq output (gdb-concat-output instance output burst))
762 ;; Save the remaining burst for the next call to this function.
763 (set-gdb-instance-burst instance burst)
766 (defun gdb-concat-output (instance so-far new)
767 (let ((sink (gdb-instance-output-sink instance)))
769 ((eq sink 'user) (concat so-far new))
770 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
772 (gdb-append-to-partial-output instance new)
774 (t (error "Bogon output sink %S" sink)))))
776 (defun gdb-append-to-partial-output (instance string)
779 (gdb-get-create-instance-buffer
780 instance 'gdb-partial-output-buffer))
781 (goto-char (point-max))
784 (defun gdb-clear-partial-output (instance)
787 (gdb-get-create-instance-buffer
788 instance 'gdb-partial-output-buffer))
789 (delete-region (point-min) (point-max))))
793 ;; One trick is to have a command who's output is always available in
794 ;; a buffer of it's own, and is always up to date. We build several
795 ;; buffers of this type.
797 ;; There are two aspects to this: gdb has to tell us when the output
798 ;; for that command might have changed, and we have to be able to run
799 ;; the command behind the user's back.
801 ;; The idle input queue and the output phasing associated with
802 ;; the instance variable `(gdb-instance-output-sink instance)' help
803 ;; us to run commands behind the user's back.
805 ;; Below is the code for specificly managing buffers of output from one
810 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
811 ;; It adds an idle input for the command we are tracking. It should be the
812 ;; annotation rule binding of whatever gdb sends to tell us this command
813 ;; might have changed it's output.
815 ;; NAME is the fucntion name. DEMAND-PREDICATE tests if output is really needed.
816 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
817 ;; input in the input queue (see comment about ``gdb communications'' above).
818 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command output-handler)
820 (defun (, name) (instance &optional ignored)
821 (if (and ((, demand-predicate) instance)
822 (not (member '(, name)
823 (gdb-instance-pending-triggers instance))))
825 (gdb-instance-enqueue-idle-input
827 (list (, gdb-command) '(, output-handler)))
828 (set-gdb-instance-pending-triggers
831 (gdb-instance-pending-triggers instance))))))))
833 (defmacro def-gdb-auto-update-handler (name trigger buf-key)
836 (set-gdb-instance-pending-triggers
839 (gdb-instance-pending-triggers instance)))
840 (let ((buf (gdb-get-instance-buffer instance
846 (buffer-read-only nil))
847 (delete-region (point-min) (point-max))
848 (insert-buffer (gdb-get-create-instance-buffer
850 'gdb-partial-output-buffer))
853 (defmacro def-gdb-auto-updated-buffer
854 (buffer-key trigger-name gdb-command output-handler-name)
857 (def-gdb-auto-update-trigger (, trigger-name)
858 ;; The demand predicate:
860 (gdb-get-instance-buffer instance '(, buffer-key)))
862 (, output-handler-name))
863 (def-gdb-auto-update-handler (, output-handler-name)
864 (, trigger-name) (, buffer-key)))))
869 ;; Breakpoint buffers
871 ;; These display the output of `info breakpoints'.
875 (gdb-set-instance-buffer-rules 'gdb-breakpoints-buffer
876 'gdb-breakpoints-buffer-name
877 'gud-breakpoints-mode)
879 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
880 ;; This defines the auto update rule for buffers of type
881 ;; `gdb-breakpoints-buffer'.
883 ;; It defines a function to serve as the annotation handler that
884 ;; handles the `foo-invalidated' message. That function is called:
885 gdb-invalidate-breakpoints
887 ;; To update the buffer, this command is sent to gdb.
888 "server info breakpoints\n"
890 ;; This also defines a function to be the handler for the output
891 ;; from the command above. That function will copy the output into
892 ;; the appropriately typed buffer. That function will be called:
893 gdb-info-breakpoints-handler)
895 (defun gdb-breakpoints-buffer-name (instance)
897 (set-buffer (process-buffer (gdb-instance-process instance)))
898 (concat "*breakpoints of " (gdb-instance-target-string instance) "*")))
900 (defun gud-display-breakpoints-buffer (instance)
901 (interactive (list (gdb-needed-default-instance)))
903 (gdb-get-create-instance-buffer instance
904 'gdb-breakpoints-buffer)))
906 (defun gud-frame-breakpoints-buffer (instance)
907 (interactive (list (gdb-needed-default-instance)))
909 (gdb-get-create-instance-buffer instance
910 'gdb-breakpoints-buffer)))
912 (defvar gud-breakpoints-mode-map nil)
913 (setq gud-breakpoints-mode-map (make-keymap))
914 (suppress-keymap gud-breakpoints-mode-map)
915 (define-key gud-breakpoints-mode-map " " 'gud-toggle-bp-this-line)
916 (define-key gud-breakpoints-mode-map "d" 'gud-delete-bp-this-line)
918 (defun gud-breakpoints-mode ()
919 "Major mode for gud breakpoints.
921 \\{gud-breakpoints-mode-map}"
922 (setq major-mode 'gud-breakpoints-mode)
923 (setq mode-name "Breakpoints")
924 (use-local-map gud-breakpoints-mode-map)
925 (setq buffer-read-only t)
926 (gdb-invalidate-breakpoints gdb-buffer-instance))
928 (defun gud-toggle-bp-this-line ()
931 (beginning-of-line 1)
932 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
933 (error "Not recognized as breakpoint line (demo foo).")
934 (gdb-instance-enqueue-idle-input
938 (if (eq ?y (char-after (match-beginning 2)))
941 (buffer-substring (match-beginning 0)
947 (defun gud-delete-bp-this-line ()
950 (beginning-of-line 1)
951 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
952 (error "Not recognized as breakpoint line (demo foo).")
953 (gdb-instance-enqueue-idle-input
958 (buffer-substring (match-beginning 0)
968 ;; Frames buffers. These display a perpetually correct bactracktrace
969 ;; (from the command `where').
971 ;; Alas, if your stack is deep, they are costly.
974 (gdb-set-instance-buffer-rules 'gdb-stack-buffer
975 'gdb-stack-buffer-name
978 (def-gdb-auto-updated-buffer gdb-stack-buffer
979 gdb-invalidate-frames
981 gdb-info-frames-handler)
983 (defun gdb-stack-buffer-name (instance)
985 (set-buffer (process-buffer (gdb-instance-process instance)))
986 (concat "*stack frames of "
987 (gdb-instance-target-string instance) "*")))
989 (defun gud-display-stack-buffer (instance)
990 (interactive (list (gdb-needed-default-instance)))
992 (gdb-get-create-instance-buffer instance
995 (defun gud-frame-stack-buffer (instance)
996 (interactive (list (gdb-needed-default-instance)))
998 (gdb-get-create-instance-buffer instance
1001 (defvar gud-frames-mode-map nil)
1002 (setq gud-frames-mode-map (make-keymap))
1003 (suppress-keymap gud-frames-mode-map)
1004 (define-key gud-frames-mode-map [mouse-2]
1005 'gud-frames-select-by-mouse)
1007 (defun gud-frames-mode ()
1008 "Major mode for gud frames.
1010 \\{gud-frames-mode-map}"
1011 (setq major-mode 'gud-frames-mode)
1012 (setq mode-name "Frames")
1013 (setq buffer-read-only t)
1014 (use-local-map gud-frames-mode-map)
1015 (gdb-invalidate-frames gdb-buffer-instance))
1017 (defun gud-get-frame-number ()
1019 (let* ((pos (re-search-backward "^#\\([0-9]*\\)" nil t))
1022 (buffer-substring (match-beginning 1)
1027 (defun gud-frames-select-by-mouse (e)
1031 (set-buffer (window-buffer (posn-window (event-end e))))
1033 (goto-char (posn-point (event-end e)))
1034 (setq selection (gud-get-frame-number))))
1035 (select-window (posn-window (event-end e)))
1037 (set-buffer (gdb-get-instance-buffer (gdb-needed-default-instance) 'gud))
1038 (gud-call "fr %p" selection)
1039 (gud-display-frame))))
1043 ;; Registers buffers
1046 (def-gdb-auto-updated-buffer gdb-registers-buffer
1047 gdb-invalidate-registers
1048 "server info registers\n"
1049 gdb-info-registers-handler)
1051 (gdb-set-instance-buffer-rules 'gdb-registers-buffer
1052 'gdb-registers-buffer-name
1053 'gud-registers-mode)
1055 (defvar gud-registers-mode-map nil)
1056 (setq gud-registers-mode-map (make-keymap))
1057 (suppress-keymap gud-registers-mode-map)
1059 (defun gud-registers-mode ()
1060 "Major mode for gud registers.
1062 \\{gud-registers-mode-map}"
1063 (setq major-mode 'gud-registers-mode)
1064 (setq mode-name "Registers")
1065 (setq buffer-read-only t)
1066 (use-local-map gud-registers-mode-map)
1067 (gdb-invalidate-registers gdb-buffer-instance))
1069 (defun gdb-registers-buffer-name (instance)
1071 (set-buffer (process-buffer (gdb-instance-process instance)))
1072 (concat "*registers of " (gdb-instance-target-string instance) "*")))
1074 (defun gud-display-registers-buffer (instance)
1075 (interactive (list (gdb-needed-default-instance)))
1077 (gdb-get-create-instance-buffer instance
1078 'gdb-registers-buffer)))
1080 (defun gud-frame-registers-buffer (instance)
1081 (interactive (list (gdb-needed-default-instance)))
1083 (gdb-get-create-instance-buffer instance
1084 'gdb-registers-buffer)))
1091 ;; MENU-LIST is ((option option option...) (option option ...)...)
1093 (defun gud-display-menu (menu-list)
1094 (setq fill-column (min 120 (- (window-width)
1095 (min 8 (window-width)))))
1097 (mapcar (function (lambda (x) (insert (symbol-name x) " "))) (car menu-list))
1098 (fill-paragraph nil)
1100 (setq menu-list (cdr menu-list)))
1101 (goto-char (point-min))
1102 (while (re-search-forward "\\([^ \n]+\\)\\(\n\\| \\)" nil t)
1103 (put-text-property (match-beginning 1) (match-end 1)
1104 'mouse-face 'highlight))
1105 (goto-char (point-min)))
1107 (defun gud-goto-menu (menu)
1108 (setq gud-menu-position menu)
1109 (let ((buffer-read-only nil))
1110 (delete-region (point-min) (point-max))
1111 (gud-display-menu menu)))
1113 (defun gud-menu-pick (event)
1114 "Choose an item from a gdb command menu."
1118 (set-buffer (window-buffer (posn-window (event-start event))))
1119 (goto-char (posn-point (event-start event)))
1121 (skip-chars-forward "^ \t\n")
1123 (skip-chars-backward "^ \t\n")
1125 (setq choice (buffer-substring beg end))
1127 (gud-invoke-menu (intern choice))))))
1129 (defun gud-invoke-menu (symbol)
1130 (let ((meaning (assoc symbol gud-menu-rules)))
1132 ((and (consp meaning)
1133 (consp (car (cdr meaning))))
1134 (gud-goto-menu (car (cdr meaning))))
1135 (meaning (call-interactively (car (cdr meaning)))))))
1139 (gdb-set-instance-buffer-rules 'gdb-command-buffer
1140 'gdb-command-buffer-name
1143 (defvar gud-command-mode-map nil)
1144 (setq gud-command-mode-map (make-keymap))
1145 (suppress-keymap gud-command-mode-map)
1146 (define-key gud-command-mode-map [mouse-2] 'gud-menu-pick)
1148 (defun gud-command-mode ()
1149 "Major mode for gud menu.
1151 \\{gud-command-mode-map}" (interactive) (setq major-mode 'gud-command-mode)
1152 (setq mode-name "Menu") (setq buffer-read-only t) (use-local-map
1153 gud-command-mode-map) (make-variable-buffer-local 'gud-menu-position)
1154 (if (not gud-menu-position) (gud-goto-menu gud-running-menu)))
1156 (defun gdb-command-buffer-name (instance)
1158 (set-buffer (process-buffer (gdb-instance-process instance)))
1159 (concat "*menu of " (gdb-instance-target-string instance) "*")))
1161 (defun gud-display-command-buffer (instance)
1162 (interactive (list (gdb-needed-default-instance)))
1164 (gdb-get-create-instance-buffer instance
1165 'gdb-command-buffer)
1168 (defun gud-frame-command-buffer (instance)
1169 (interactive (list (gdb-needed-default-instance)))
1171 (gdb-get-create-instance-buffer instance
1172 'gdb-command-buffer)))
1174 (defvar gud-selected-menu-titles ())
1175 (setq gud-selected-menu-titles
1176 '(RUNNING STACK DATA BREAKPOINTS FILES))
1178 (setq gud-running-menu
1180 '(RUNNING stack breakpoints files)
1181 '(target run next step continue finish stepi kill help-running)))
1183 (setq gud-stack-menu
1185 '(running STACK breakpoints files)
1186 '(up down frame backtrace return help-stack)))
1190 '(running stack DATA breakpoints files)
1191 '(whatis ptype print set display undisplay disassemble help-data)))
1193 (setq gud-breakpoints-menu
1195 '(running stack BREAKPOINTS files)
1196 '(awatch rwatch watch break delete enable disable condition ignore help-breakpoints)))
1198 (setq gud-files-menu
1200 '(running stack breakpoints FILES)
1201 '(file core-file help-files)
1202 '(exec-file load symbol-file add-symbol-file sharedlibrary)))
1204 (setq gud-menu-rules
1206 (list 'running gud-running-menu)
1207 (list 'RUNNING gud-running-menu)
1208 (list 'stack gud-stack-menu)
1209 (list 'STACK gud-stack-menu)
1210 (list 'data gud-data-menu)
1211 (list 'DATA gud-data-menu)
1212 (list 'breakpoints gud-breakpoints-menu)
1213 (list 'BREAKPOINTS gud-breakpoints-menu)
1214 (list 'files gud-files-menu)
1215 (list 'FILES gud-files-menu)
1217 (list 'target 'gud-target)
1218 (list 'kill 'gud-kill)
1219 (list 'stepi 'gud-stepi)
1220 (list 'step 'gud-step)
1221 (list 'next 'gud-next)
1222 (list 'finish 'gud-finish)
1223 (list 'continue 'gud-cont)
1224 (list 'run 'gud-run)
1226 (list 'backtrace 'gud-backtrace)
1227 (list 'frame 'gud-frame)
1228 (list 'down 'gud-down)
1230 (list 'return 'gud-return)
1232 (list 'file 'gud-file)
1233 (list 'core-file 'gud-core-file)
1236 (list 'exec-file 'gud-exec-file)
1237 (list 'load 'gud-load)
1238 (list 'symbol-file 'gud-symbol-file)
1239 (list 'add-symbol-file 'gud-add-symbol-file)
1240 (list 'sharedlibrary 'gud-sharedlibrary)
1246 (defun gdb-call-showing-gud (instance command)
1247 (gud-display-gud-buffer instance)
1248 (comint-input-sender (gdb-instance-process instance) command))
1250 (defvar gud-target-history ())
1252 (defun gud-temp-buffer-show (buf)
1253 (let ((ow (selected-window)))
1258 ;; This insertion works around a bug in emacs.
1259 ;; The bug is that all the empty space after a
1260 ;; highlighted word that terminates a buffer
1261 ;; gets highlighted. That's really ugly, so
1262 ;; make sure a highlighted word can't ever
1263 ;; terminate the buffer.
1264 (goto-char (point-max))
1266 (goto-char (point-min))
1268 (if (< (window-height) 10)
1269 (enlarge-window (- 10 (window-height)))))
1270 (select-window ow))))
1272 (defun gud-target (instance command)
1274 (let* ((instance (gdb-needed-default-instance))
1275 (temp-buffer-show-function (function gud-temp-buffer-show))
1276 (target-name (completing-read (format "Target type: ")
1284 'gud-target-history)))
1287 ((equal target-name "child") "run")
1289 ((equal target-name "core")
1290 (concat "target core "
1291 (read-file-name "core file: "
1296 ((equal target-name "exec")
1297 (concat "target exec "
1298 (read-file-name "exec file: "
1303 ((equal target-name "remote")
1304 (concat "target remote "
1305 (read-file-name "serial line for remote: "
1310 (t "echo No such target command!")))))
1312 (gud-display-gud-buffer instance)
1313 (apply comint-input-sender
1314 (list (gdb-instance-process instance) command)))
1316 (defun gud-backtrace ()
1318 (let ((instance (gdb-needed-default-instance)))
1319 (gud-display-gud-buffer instance)
1320 (apply comint-input-sender
1321 (list (gdb-instance-process instance)
1326 (let ((instance (gdb-needed-default-instance)))
1327 (apply comint-input-sender
1328 (list (gdb-instance-process instance)
1331 (defun gud-return (instance command)
1333 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1334 (list (gdb-needed-default-instance)
1335 (concat "return " (read-string "Expression to return: ")))))
1336 (gud-display-gud-buffer instance)
1337 (apply comint-input-sender
1338 (list (gdb-instance-process instance) command)))
1341 (defun gud-file (instance command)
1343 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1344 (list (gdb-needed-default-instance)
1345 (concat "file " (read-file-name "Executable to debug: "
1349 (gud-display-gud-buffer instance)
1350 (apply comint-input-sender
1351 (list (gdb-instance-process instance) command)))
1353 (defun gud-core-file (instance command)
1355 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1356 (list (gdb-needed-default-instance)
1357 (concat "core " (read-file-name "Core file to debug: "
1361 (gud-display-gud-buffer instance)
1362 (apply comint-input-sender
1363 (list (gdb-instance-process instance) command)))
1366 (interactive "FChange GDB's default directory: ")
1367 (let ((instance (gdb-needed-default-instance)))
1369 (set-buffer (gdb-get-instance-buffer instance 'gud))
1371 (gud-display-gud-buffer instance)
1372 (apply comint-input-sender
1373 (list (gdb-instance-process instance)
1374 (concat "cd " dir)))))
1377 (defun gud-exec-file (instance command)
1379 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1380 (list (gdb-needed-default-instance)
1381 (concat "exec-file " (read-file-name "Init memory from executable: "
1385 (gud-display-gud-buffer instance)
1386 (apply comint-input-sender
1387 (list (gdb-instance-process instance) command)))
1389 (defun gud-load (instance command)
1391 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1392 (list (gdb-needed-default-instance)
1393 (concat "load " (read-file-name "Dynamicly load from file: "
1397 (gud-display-gud-buffer instance)
1398 (apply comint-input-sender
1399 (list (gdb-instance-process instance) command)))
1401 (defun gud-symbol-file (instance command)
1403 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1404 (list (gdb-needed-default-instance)
1405 (concat "symbol-file " (read-file-name "Read symbol table from file: "
1409 (gud-display-gud-buffer instance)
1410 (apply comint-input-sender
1411 (list (gdb-instance-process instance) command)))
1414 (defun gud-add-symbol-file (instance command)
1416 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1417 (list (gdb-needed-default-instance)
1418 (concat "add-symbol-file "
1419 (read-file-name "Add symbols from file: "
1423 (gud-display-gud-buffer instance)
1424 (apply comint-input-sender
1425 (list (gdb-instance-process instance) command)))
1428 (defun gud-sharedlibrary (instance command)
1430 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1431 (list (gdb-needed-default-instance)
1432 (concat "sharedlibrary "
1433 (read-string "Load symbols for files matching regexp: ")))))
1434 (gud-display-gud-buffer instance)
1435 (apply comint-input-sender
1436 (list (gdb-instance-process instance) command)))
1442 ;;;; Window management
1445 ;;; FIXME: This should only return true for buffers in the current instance
1446 (defun gud-protected-buffer-p (buffer)
1447 "Is BUFFER a buffer which we want to leave displayed?"
1451 overlay-arrow-position)))
1453 ;;; The way we abuse the dedicated-p flag is pretty gross, but seems
1454 ;;; to do the right thing. Seeing as there is no way for Lisp code to
1455 ;;; get at the use_time field of a window, I'm not sure there exists a
1456 ;;; more elegant solution without writing C code.
1458 (defun gud-display-buffer (buf &optional size)
1459 (let ((must-split nil)
1465 (if (gud-protected-buffer-p (window-buffer win))
1466 (set-window-dedicated-p win t))))
1467 (setq answer (get-buffer-window buf))
1469 (let ((window (get-lru-window)))
1472 (set-window-buffer window buf)
1473 (setq answer window))
1474 (setq must-split t)))))
1477 (if (gud-protected-buffer-p (window-buffer win))
1478 (set-window-dedicated-p win nil)))))
1480 (let* ((largest (get-largest-window))
1481 (cur-size (window-height largest))
1482 (new-size (and size (< size cur-size) (- cur-size size))))
1483 (setq answer (split-window largest new-size))
1484 (set-window-buffer answer buf)))
1487 (defun existing-source-window (buffer)
1493 (if (and overlay-arrow-position
1494 (eq (window-buffer win)
1495 (marker-buffer overlay-arrow-position)))
1497 (set-window-buffer win buffer)
1498 (throw 'found win))))))
1501 (defun gud-display-source-buffer (buffer)
1502 (or (existing-source-window buffer)
1503 (gud-display-buffer buffer)))
1505 (defun gud-frame-buffer (buf)
1512 ;;; Shared keymap initialization:
1514 (defun make-windows-menu (map)
1515 (define-key map [menu-bar displays]
1516 (cons "GDB-Windows" (make-sparse-keymap "GDB-Windows")))
1517 (define-key map [menu-bar displays gdb]
1518 '("Gdb" . gud-display-gud-buffer))
1519 (define-key map [menu-bar displays registers]
1520 '("Registers" . gud-display-registers-buffer))
1521 (define-key map [menu-bar displays frames]
1522 '("Stack" . gud-display-stack-buffer))
1523 (define-key map [menu-bar displays breakpoints]
1524 '("Breakpoints" . gud-display-breakpoints-buffer))
1525 (define-key map [menu-bar displays commands]
1526 '("Commands" . gud-display-command-buffer)))
1528 (defun gud-display-gud-buffer (instance)
1529 (interactive (list (gdb-needed-default-instance)))
1531 (gdb-get-create-instance-buffer instance 'gud)))
1533 (make-windows-menu gud-breakpoints-mode-map)
1534 (make-windows-menu gud-frames-mode-map)
1535 (make-windows-menu gud-registers-mode-map)
1539 (defun make-frames-menu (map)
1540 (define-key map [menu-bar frames]
1541 (cons "GDB-Frames" (make-sparse-keymap "GDB-Frames")))
1542 (define-key map [menu-bar frames gdb]
1543 '("Gdb" . gud-frame-gud-buffer))
1544 (define-key map [menu-bar frames registers]
1545 '("Registers" . gud-frame-registers-buffer))
1546 (define-key map [menu-bar frames frames]
1547 '("Stack" . gud-frame-stack-buffer))
1548 (define-key map [menu-bar frames breakpoints]
1549 '("Breakpoints" . gud-frame-breakpoints-buffer))
1550 (define-key map [menu-bar displays commands]
1551 '("Commands" . gud-display-command-buffer)))
1553 (defun gud-frame-gud-buffer (instance)
1554 (interactive (list (gdb-needed-default-instance)))
1556 (gdb-get-create-instance-buffer instance 'gud)))
1558 (make-frames-menu gud-breakpoints-mode-map)
1559 (make-frames-menu gud-frames-mode-map)
1560 (make-frames-menu gud-registers-mode-map)
1563 (defun gud-gdb-find-file (f)
1564 (find-file-noselect f))
1567 (defun gdb (command-line)
1568 "Run gdb on program FILE in buffer *gud-FILE*.
1569 The directory containing FILE becomes the initial working directory
1570 and source-file directory for your debugger."
1572 (list (read-from-minibuffer "Run gdb (like this): "
1573 (if (consp gud-gdb-history)
1574 (car gud-gdb-history)
1577 '(gud-gdb-history . 1))))
1578 (gud-overload-functions
1579 '((gud-massage-args . gud-gdb-massage-args)
1580 (gud-marker-filter . gud-gdb-marker-filter)
1581 (gud-find-file . gud-gdb-find-file)
1584 (gud-common-init command-line)
1586 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
1587 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
1588 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1589 (gud-def gud-kill "kill" nil "Kill the program.")
1590 (gud-def gud-run "run" nil "Run the program.")
1591 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1592 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
1593 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1594 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1595 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1596 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1597 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1598 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1600 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
1601 (setq comint-input-sender 'gdb-send)
1602 (run-hooks 'gdb-mode-hook)
1603 (make-gdb-instance (get-buffer-process (current-buffer)))
1607 ;; ======================================================================
1610 ;;; History of argument lists passed to sdb.
1611 (defvar gud-sdb-history nil)
1613 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
1614 "If nil, we're on a System V Release 4 and don't need the tags hack.")
1616 (defvar gud-sdb-lastfile nil)
1618 (defun gud-sdb-massage-args (file args)
1621 (defun gud-sdb-marker-filter (string)
1623 ;; System V Release 3.2 uses this format
1624 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1626 (setq gud-last-frame
1628 (substring string (match-beginning 2) (match-end 2))
1630 (substring string (match-beginning 3) (match-end 3))))))
1631 ;; System V Release 4.0
1632 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
1634 (setq gud-sdb-lastfile
1635 (substring string (match-beginning 2) (match-end 2))))
1636 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
1637 (setq gud-last-frame
1641 (substring string (match-beginning 1) (match-end 1))))))
1643 (setq gud-sdb-lastfile nil)))
1646 (defun gud-sdb-find-file (f)
1647 (if gud-sdb-needs-tags
1648 (find-tag-noselect f)
1649 (find-file-noselect f)))
1652 (defun sdb (command-line)
1653 "Run sdb on program FILE in buffer *gud-FILE*.
1654 The directory containing FILE becomes the initial working directory
1655 and source-file directory for your debugger."
1657 (list (read-from-minibuffer "Run sdb (like this): "
1658 (if (consp gud-sdb-history)
1659 (car gud-sdb-history)
1662 '(gud-sdb-history . 1))))
1663 (if (and gud-sdb-needs-tags
1664 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
1665 (error "The sdb support requires a valid tags table to work."))
1666 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
1667 (gud-marker-filter . gud-sdb-marker-filter)
1668 (gud-find-file . gud-sdb-find-file)
1671 (gud-common-init command-line)
1673 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
1674 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
1675 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
1676 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
1677 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
1678 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1679 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1680 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
1682 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
1683 (run-hooks 'sdb-mode-hook)
1686 ;; ======================================================================
1689 ;;; History of argument lists passed to dbx.
1690 (defvar gud-dbx-history nil)
1692 (defun gud-dbx-massage-args (file args)
1695 (defun gud-dbx-marker-filter (string)
1696 (if (or (string-match
1697 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1700 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1702 (setq gud-last-frame
1704 (substring string (match-beginning 2) (match-end 2))
1706 (substring string (match-beginning 1) (match-end 1))))))
1709 (defun gud-dbx-find-file (f)
1710 (find-file-noselect f))
1713 (defun dbx (command-line)
1714 "Run dbx on program FILE in buffer *gud-FILE*.
1715 The directory containing FILE becomes the initial working directory
1716 and source-file directory for your debugger."
1718 (list (read-from-minibuffer "Run dbx (like this): "
1719 (if (consp gud-dbx-history)
1720 (car gud-dbx-history)
1723 '(gud-dbx-history . 1))))
1724 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args)
1725 (gud-marker-filter . gud-dbx-marker-filter)
1726 (gud-find-file . gud-dbx-find-file)
1729 (gud-common-init command-line)
1731 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1732 "\C-b" "Set breakpoint at current line.")
1733 ;; (gud-def gud-break "stop at \"%f\":%l"
1734 ;; "\C-b" "Set breakpoint at current line.")
1735 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1736 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1737 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1738 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1739 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1740 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1741 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1742 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1744 (setq comint-prompt-regexp "^[^)]*dbx) *")
1745 (run-hooks 'dbx-mode-hook)
1748 ;; ======================================================================
1749 ;; xdb (HP PARISC debugger) functions
1751 ;;; History of argument lists passed to xdb.
1752 (defvar gud-xdb-history nil)
1754 (defvar gud-xdb-directories nil
1755 "*A list of directories that xdb should search for source code.
1756 If nil, only source files in the program directory
1757 will be known to xdb.
1759 The file names should be absolute, or relative to the directory
1760 containing the executable being debugged.")
1762 (defun gud-xdb-massage-args (file args)
1763 (nconc (let ((directories gud-xdb-directories)
1766 (setq result (cons (car directories) (cons "-d" result)))
1767 (setq directories (cdr directories)))
1768 (nreverse (cons file result)))
1771 (defun gud-xdb-file-name (f)
1772 "Transform a relative pathname to a full pathname in xdb mode"
1774 (if (file-exists-p f)
1775 (setq result (expand-file-name f))
1776 (let ((directories gud-xdb-directories))
1778 (let ((path (concat (car directories) "/" f)))
1779 (if (file-exists-p path)
1780 (setq result (expand-file-name path)
1782 (setq directories (cdr directories)))))
1785 ;; xdb does not print the lines all at once, so we have to accumulate them
1786 (defvar gud-xdb-accumulation "")
1788 (defun gud-xdb-marker-filter (string)
1790 (if (or (string-match comint-prompt-regexp string)
1791 (string-match ".*\012" string))
1792 (setq result (concat gud-xdb-accumulation string)
1793 gud-xdb-accumulation "")
1794 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
1796 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
1797 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1799 (let ((line (string-to-int
1800 (substring result (match-beginning 2) (match-end 2))))
1801 (file (gud-xdb-file-name
1802 (substring result (match-beginning 1) (match-end 1)))))
1804 (setq gud-last-frame (cons file line))))))
1807 (defun gud-xdb-find-file (f)
1808 (let ((realf (gud-xdb-file-name f)))
1809 (if realf (find-file-noselect realf))))
1812 (defun xdb (command-line)
1813 "Run xdb on program FILE in buffer *gud-FILE*.
1814 The directory containing FILE becomes the initial working directory
1815 and source-file directory for your debugger.
1817 You can set the variable 'gud-xdb-directories' to a list of program source
1818 directories if your program contains sources from more than one directory."
1820 (list (read-from-minibuffer "Run xdb (like this): "
1821 (if (consp gud-xdb-history)
1822 (car gud-xdb-history)
1825 '(gud-xdb-history . 1))))
1826 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
1827 (gud-marker-filter . gud-xdb-marker-filter)
1828 (gud-find-file . gud-xdb-find-file)))
1830 (gud-common-init command-line)
1832 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1833 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1834 "Set temporary breakpoint at current line.")
1835 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1836 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1837 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1838 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1839 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1840 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1841 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1842 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1844 (setq comint-prompt-regexp "^>")
1845 (make-local-variable 'gud-xdb-accumulation)
1846 (setq gud-xdb-accumulation "")
1847 (run-hooks 'xdb-mode-hook))
1849 ;; ======================================================================
1852 ;;; History of argument lists passed to perldb.
1853 (defvar gud-perldb-history nil)
1855 (defun gud-perldb-massage-args (file args)
1856 (cons "-d" (cons file (cons "-emacs" args))))
1858 ;; There's no guarantee that Emacs will hand the filter the entire
1859 ;; marker at once; it could be broken up across several strings. We
1860 ;; might even receive a big chunk with several markers in it. If we
1861 ;; receive a chunk of text which looks like it might contain the
1862 ;; beginning of a marker, we save it here between calls to the
1864 (defvar gud-perldb-marker-acc "")
1866 (defun gud-perldb-marker-filter (string)
1868 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
1871 ;; Process all the complete markers in this chunk.
1872 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1873 gud-perldb-marker-acc)
1876 ;; Extract the frame position from the marker.
1878 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
1879 (string-to-int (substring gud-perldb-marker-acc
1883 ;; Append any text before the marker to the output we're going
1884 ;; to return - we don't include the marker in this text.
1885 output (concat output
1886 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
1888 ;; Set the accumulator to the remaining text.
1889 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
1891 ;; Does the remaining text look like it might end with the
1892 ;; beginning of another marker? If it does, then keep it in
1893 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
1894 ;; know the full marker regexp above failed, it's pretty simple to
1895 ;; test for marker starts.
1896 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
1898 ;; Everything before the potential marker start can be output.
1899 (setq output (concat output (substring gud-perldb-marker-acc
1900 0 (match-beginning 0))))
1902 ;; Everything after, we save, to combine with later input.
1903 (setq gud-perldb-marker-acc
1904 (substring gud-perldb-marker-acc (match-beginning 0))))
1906 (setq output (concat output gud-perldb-marker-acc)
1907 gud-perldb-marker-acc ""))
1911 (defun gud-perldb-find-file (f)
1912 (find-file-noselect f))
1915 (defun perldb (command-line)
1916 "Run perldb on program FILE in buffer *gud-FILE*.
1917 The directory containing FILE becomes the initial working directory
1918 and source-file directory for your debugger."
1920 (list (read-from-minibuffer "Run perldb (like this): "
1921 (if (consp gud-perldb-history)
1922 (car gud-perldb-history)
1925 '(gud-perldb-history . 1))))
1926 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
1927 (gud-marker-filter . gud-perldb-marker-filter)
1928 (gud-find-file . gud-perldb-find-file)
1931 (gud-common-init command-line)
1933 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1934 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1935 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1936 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1937 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1938 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1939 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1940 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1941 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1943 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1944 (run-hooks 'perldb-mode-hook)
1948 ;; End of debugger-specific information
1952 ;;; When we send a command to the debugger via gud-call, it's annoying
1953 ;;; to see the command and the new prompt inserted into the debugger's
1954 ;;; buffer; we have other ways of knowing the command has completed.
1956 ;;; If the buffer looks like this:
1957 ;;; --------------------
1958 ;;; (gdb) set args foo bar
1960 ;;; --------------------
1961 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1962 ;;; source file to set a breakpoint, we want the buffer to end up like
1964 ;;; --------------------
1965 ;;; (gdb) set args foo bar
1966 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1968 ;;; --------------------
1969 ;;; Essentially, the old prompt is deleted, and the command's output
1970 ;;; and the new prompt take its place.
1972 ;;; Not echoing the command is easy enough; you send it directly using
1973 ;;; comint-input-sender, and it never enters the buffer. However,
1974 ;;; getting rid of the old prompt is trickier; you don't want to do it
1975 ;;; when you send the command, since that will result in an annoying
1976 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1977 ;;; waits for a response from the debugger, and the new prompt is
1978 ;;; inserted. Instead, we'll wait until we actually get some output
1979 ;;; from the subprocess before we delete the prompt. If the command
1980 ;;; produced no output other than a new prompt, that prompt will most
1981 ;;; likely be in the first chunk of output received, so we will delete
1982 ;;; the prompt and then replace it with an identical one. If the
1983 ;;; command produces output, the prompt is moving anyway, so the
1984 ;;; flicker won't be annoying.
1986 ;;; So - when we want to delete the prompt upon receipt of the next
1987 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1988 ;;; the start of the prompt; the process filter will notice this, and
1989 ;;; delete all text between it and the process output marker. If
1990 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1992 (defvar gud-delete-prompt-marker nil)
1995 (defvar gdbish-comint-mode-map (copy-keymap comint-mode-map))
1996 (define-key gdbish-comint-mode-map "\C-c\M-\C-r" 'gud-display-registers-buffer)
1997 (define-key gdbish-comint-mode-map "\C-c\M-\C-f" 'gud-display-stack-buffer)
1998 (define-key gdbish-comint-mode-map "\C-c\M-\C-b" 'gud-display-breakpoints-buffer)
2000 (make-windows-menu gdbish-comint-mode-map)
2001 (make-frames-menu gdbish-comint-mode-map)
2004 "Major mode for interacting with an inferior debugger process.
2006 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2007 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
2008 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
2010 After startup, the following commands are available in both the GUD
2011 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2014 \\[gud-break] sets a breakpoint at the current file and line. In the
2015 GUD buffer, the current file and line are those of the last breakpoint or
2016 step. In a source buffer, they are the buffer's file and current line.
2018 \\[gud-remove] removes breakpoints on the current file and line.
2020 \\[gud-refresh] displays in the source window the last line referred to
2023 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2024 step-one-line (not entering function calls), and step-one-instruction
2025 and then update the source window with the current file and position.
2026 \\[gud-cont] continues execution.
2028 \\[gud-print] tries to find the largest C lvalue or function-call expression
2029 around point, and sends it to the debugger for value display.
2031 The above commands are common to all supported debuggers except xdb which
2032 does not support stepping instructions.
2034 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2035 except that the breakpoint is temporary; that is, it is removed when
2036 execution stops on it.
2038 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2039 frame. \\[gud-down] drops back down through one.
2041 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2042 the current function and stops.
2044 All the keystrokes above are accessible in the GUD buffer
2045 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2047 All pre-defined functions for which the concept make sense repeat
2048 themselves the appropriate number of times if you give a prefix
2051 You may use the `gud-def' macro in the initialization hook to define other
2054 Other commands for interacting with the debugger process are inherited from
2055 comint mode, which see."
2058 (setq major-mode 'gud-mode)
2059 (setq mode-name "Debugger")
2060 (setq mode-line-process '(": %s"))
2061 (use-local-map (copy-keymap gdbish-comint-mode-map))
2062 (setq gud-last-frame nil)
2063 (make-local-variable 'comint-prompt-regexp)
2064 (make-local-variable 'gud-delete-prompt-marker)
2065 (setq gud-delete-prompt-marker (make-marker))
2066 (run-hooks 'gud-mode-hook)
2069 (defvar gud-comint-buffer nil)
2071 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
2072 (defun gud-chop-words (string)
2074 (len (length string))
2077 (if (memq (aref string i) '(?\t ? ))
2079 (setq words (cons (substring string beg i) words)
2081 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2082 (setq beg (1+ beg)))
2086 (setq words (cons (substring string beg) words)))
2089 (defvar gud-target-name "--unknown--"
2090 "The apparent name of the program being debugged in a gud buffer.
2091 For sure this the root string used in smashing together the gud
2092 buffer's name, even if that doesn't happen to be the name of a
2095 ;; Perform initializations common to all debuggers.
2096 (defun gud-common-init (command-line)
2097 (let* ((words (gud-chop-words command-line))
2098 (program (car words))
2099 (file-word (let ((w (cdr words)))
2100 (while (and w (= ?- (aref (car w) 0)))
2103 (args (delq file-word (cdr words)))
2104 (file (expand-file-name file-word))
2105 (filepart (file-name-nondirectory file))
2106 (buffer-name (concat "*gud-" filepart "*")))
2107 (switch-to-buffer buffer-name)
2108 (setq default-directory (file-name-directory file))
2109 (or (bolp) (newline))
2110 (insert "Current directory is " default-directory "\n")
2111 (let ((old-instance gdb-buffer-instance))
2112 (apply 'make-comint (concat "gud-" filepart) program nil
2113 (gud-massage-args file args))
2115 (make-variable-buffer-local 'old-gdb-buffer-instance)
2116 (setq old-gdb-buffer-instance old-instance))
2117 (make-variable-buffer-local 'gud-target-name)
2118 (setq gud-target-name filepart))
2119 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2120 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2124 (defun gud-set-buffer ()
2125 (cond ((eq major-mode 'gud-mode)
2126 (setq gud-comint-buffer (current-buffer)))))
2128 ;; These functions are responsible for inserting output from your debugger
2129 ;; into the buffer. The hard work is done by the method that is
2130 ;; the value of gud-marker-filter.
2132 (defun gud-filter (proc string)
2133 ;; Here's where the actual buffer insertion is done
2134 (let ((inhibit-quit t))
2136 (set-buffer (process-buffer proc))
2137 (let (moving output-after-point)
2139 (goto-char (process-mark proc))
2140 ;; If we have been so requested, delete the debugger prompt.
2141 (if (marker-buffer gud-delete-prompt-marker)
2143 (delete-region (point) gud-delete-prompt-marker)
2144 (set-marker gud-delete-prompt-marker nil)))
2145 (insert-before-markers (gud-marker-filter string))
2146 (setq moving (= (point) (process-mark proc)))
2147 (setq output-after-point (< (point) (process-mark proc)))
2148 ;; Check for a filename-and-line number.
2149 ;; Don't display the specified file
2150 ;; unless (1) point is at or after the position where output appears
2151 ;; and (2) this buffer is on the screen.
2152 (if (and gud-last-frame
2153 (not output-after-point)
2154 (get-buffer-window (current-buffer)))
2155 (gud-display-frame)))
2156 (if moving (goto-char (process-mark proc)))))))
2158 (defun gud-sentinel (proc msg)
2159 (cond ((null (buffer-name (process-buffer proc)))
2161 ;; Stop displaying an arrow in a source file.
2162 (setq overlay-arrow-position nil)
2163 (set-process-buffer proc nil))
2164 ((memq (process-status proc) '(signal exit))
2165 ;; Stop displaying an arrow in a source file.
2166 (setq overlay-arrow-position nil)
2167 ;; Fix the mode line.
2168 (setq mode-line-process
2170 (symbol-name (process-status proc))))
2171 (let* ((obuf (current-buffer)))
2172 ;; save-excursion isn't the right thing if
2173 ;; process-buffer is current-buffer
2176 ;; Write something in *compilation* and hack its mode line,
2177 (set-buffer (process-buffer proc))
2178 ;; Force mode line redisplay soon
2179 (set-buffer-modified-p (buffer-modified-p))
2181 (insert ?\n mode-name " " msg)
2183 (goto-char (point-max))
2184 (insert ?\n mode-name " " msg)))
2185 ;; If buffer and mode line will show that the process
2186 ;; is dead, we can delete it now. Otherwise it
2187 ;; will stay around until M-x list-processes.
2188 (delete-process proc))
2189 ;; Restore old buffer, but don't restore old point
2190 ;; if obuf is the gud buffer.
2191 (set-buffer obuf))))))
2193 (defun gud-display-frame ()
2194 "Find and obey the last filename-and-line marker from the debugger.
2195 Obeying it means displaying in another window the specified file and line."
2200 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2201 (setq gud-last-last-frame gud-last-frame
2202 gud-last-frame nil))))
2204 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2205 ;; and that its line LINE is visible.
2206 ;; Put the overlay-arrow on the line LINE in that buffer.
2207 ;; Most of the trickiness in here comes from wanting to preserve the current
2208 ;; region-restriction if that's possible. We use an explicit display-buffer
2209 ;; to get around the fact that this is called inside a save-excursion.
2211 (defun gud-display-line (true-file line)
2212 (let* ((buffer (gud-find-file true-file))
2213 (window (gud-display-source-buffer buffer))
2216 (error "foo bar baz"))
2217 ;;; (if (equal buffer (current-buffer))
2219 ;;; (setq buffer-read-only nil))
2221 ;;; (setq buffer-read-only t)
2227 (setq overlay-arrow-string "=>")
2228 (or overlay-arrow-position
2229 (setq overlay-arrow-position (make-marker)))
2230 (set-marker overlay-arrow-position (point) (current-buffer)))
2231 (cond ((or (< pos (point-min)) (> pos (point-max)))
2234 (set-window-point window overlay-arrow-position)))
2236 ;;; The gud-call function must do the right thing whether its invoking
2237 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2238 ;;; or a C buffer. In the former case, we want to supply data from
2239 ;;; gud-last-frame. Here's how we do it:
2241 (defun gud-format-command (str arg)
2242 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
2243 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
2245 (substring str (match-beginning 1) (match-end 1))
2246 (file-name-nondirectory (if insource
2248 (car gud-last-frame)))
2249 (substring str (match-beginning 2) (match-end 2)))))
2250 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
2252 (substring str (match-beginning 1) (match-end 1))
2253 (file-name-directory (if insource
2255 (car gud-last-frame)))
2256 (substring str (match-beginning 2) (match-end 2)))))
2257 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
2259 (substring str (match-beginning 1) (match-end 1))
2263 (save-restriction (widen)
2264 (1+ (count-lines 1 (point)))))
2265 (cdr gud-last-frame))
2266 (substring str (match-beginning 2) (match-end 2)))))
2267 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
2269 (substring str (match-beginning 1) (match-end 1))
2271 (substring str (match-beginning 2) (match-end 2)))))
2272 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
2274 (substring str (match-beginning 1) (match-end 1))
2276 (substring str (match-beginning 2) (match-end 2)))))
2277 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
2279 (substring str (match-beginning 1) (match-end 1))
2280 (if arg (int-to-string arg) "")
2281 (substring str (match-beginning 2) (match-end 2)))))
2286 (defun gud-read-address ()
2287 "Return a string containing the core-address found in the buffer at point."
2289 (let ((pt (point)) found begin)
2290 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2292 (found (forward-char 2)
2293 (buffer-substring found
2294 (progn (re-search-forward "[^0-9a-f]")
2297 (t (setq begin (progn (re-search-backward "[^0-9]")
2301 (re-search-forward "[^0-9]")
2303 (buffer-substring begin (point)))))))
2305 (defun gud-call (fmt &optional arg)
2306 (let ((msg (gud-format-command fmt arg)))
2307 (message "Command: %s" msg)
2309 (gud-basic-call msg)))
2311 (defun gud-basic-call (command)
2312 "Invoke the debugger COMMAND displaying source in other window."
2315 (let ((proc (get-buffer-process gud-comint-buffer)))
2317 ;; Arrange for the current prompt to get deleted.
2319 (set-buffer gud-comint-buffer)
2320 (goto-char (process-mark proc))
2322 (if (looking-at comint-prompt-regexp)
2323 (set-marker gud-delete-prompt-marker (point)))
2324 (apply comint-input-sender (list proc command)))))
2326 (defun gud-refresh (&optional arg)
2327 "Fix up a possibly garbled display, and redraw the arrow."
2330 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2331 (gud-display-frame))
2333 ;;; Code for parsing expressions out of C code. The single entry point is
2334 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2336 ;;; The rest of this file is a hacked version of gdbsrc.el by
2340 (defun find-c-expr ()
2341 "Returns the C expr that surrounds point."
2344 (let ((p) (expr) (test-expr))
2346 (setq expr (expr-cur))
2347 (setq test-expr (expr-prev))
2348 (while (expr-compound test-expr expr)
2349 (setq expr (cons (car test-expr) (cdr expr)))
2350 (goto-char (car expr))
2351 (setq test-expr (expr-prev)))
2353 (setq test-expr (expr-next))
2354 (while (expr-compound expr test-expr)
2355 (setq expr (cons (car expr) (cdr test-expr)))
2356 (setq test-expr (expr-next))
2358 (buffer-substring (car expr) (cdr expr)))))
2361 "Returns the expr that point is in; point is set to beginning of expr.
2362 The expr is represented as a cons cell, where the car specifies the point in
2363 the current buffer that marks the beginning of the expr and the cdr specifies
2364 the character after the end of the expr."
2365 (let ((p (point)) (begin) (end))
2366 (expr-backward-sexp)
2367 (setq begin (point))
2381 (defun expr-backward-sexp ()
2382 "Version of `backward-sexp' that catches errors."
2387 (defun expr-forward-sexp ()
2388 "Version of `forward-sexp' that catches errors."
2394 "Returns the previous expr, point is set to beginning of that expr.
2395 The expr is represented as a cons cell, where the car specifies the point in
2396 the current buffer that marks the beginning of the expr and the cdr specifies
2397 the character after the end of the expr"
2398 (let ((begin) (end))
2399 (expr-backward-sexp)
2400 (setq begin (point))
2407 "Returns the following expr, point is set to beginning of that expr.
2408 The expr is represented as a cons cell, where the car specifies the point in
2409 the current buffer that marks the beginning of the expr and the cdr specifies
2410 the character after the end of the expr."
2411 (let ((begin) (end))
2415 (expr-backward-sexp)
2416 (setq begin (point))
2419 (defun expr-compound-sep (span-start span-end)
2420 "Returns '.' for '->' & '.', returns ' ' for white space,
2421 returns '?' for other punctuation."
2424 (while (< span-start span-end)
2425 (setq syntax (char-syntax (char-after span-start)))
2428 ((= syntax ?.) (setq syntax (char-after span-start))
2430 ((= syntax ?.) (setq result ?.))
2431 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2433 (setq span-start (+ span-start 1)))
2434 (t (setq span-start span-end)
2435 (setq result ??)))))
2436 (setq span-start (+ span-start 1)))
2439 (defun expr-compound (first second)
2440 "Non-nil if concatenating FIRST and SECOND makes a single C token.
2441 The two exprs are represented as a cons cells, where the car
2442 specifies the point in the current buffer that marks the beginning of the
2443 expr and the cdr specifies the character after the end of the expr.
2444 Link exprs of the form:
2451 (let ((span-start (cdr first))
2452 (span-end (car second))
2454 (setq syntax (expr-compound-sep span-start span-end))
2456 ((= (car first) (car second)) nil)
2457 ((= (cdr first) (cdr second)) nil)
2460 (setq span-start (char-after (- span-start 1)))
2461 (setq span-end (char-after span-end))
2463 ((= span-start ?) ) t )
2464 ((= span-start ?] ) t )
2465 ((= span-end ?( ) t )
2466 ((= span-end ?[ ) t )
2473 ;;; gud.el ends here