new, emacs gdb mode with annotation stuff
[binutils-gdb.git] / gdb / gdba.el
1 (defmacro gud (form)
2 (` (save-excursion (set-buffer "*gud-a.out*") (, form))))
3
4 (defun dbug (foo &optional fun)
5 (save-excursion
6 (set-buffer (get-buffer-create "*trace*"))
7 (goto-char (point-max))
8 (insert "***" (symbol-name foo) "\n")
9 (if fun
10 (funcall fun))))
11
12
13 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
14 ;;; under Emacs
15
16 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
17 ;; Maintainer: FSF
18 ;; Version: 1.3
19 ;; Keywords: unix, tools
20
21 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
22
23 ;; This file is part of GNU Emacs.
24
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)
28 ;; any later version.
29
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.
34
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.
38
39 ;;; Commentary:
40
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).
47
48 ;; Jim Kingdon added support for ^Z^Z^Z annotations.
49
50 ;;; Code:
51
52 (require 'comint)
53 (require 'etags)
54
55 ;; ======================================================================
56 ;; GUD commands must be visible in C buffers visited by GUD
57
58 (defvar gud-key-prefix "\C-x\C-a"
59 "Prefix of all GUD commands valid in C buffers.")
60
61 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
62 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
63
64 ;; ======================================================================
65 ;; the overloading mechanism
66
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)"
71 (mapcar
72 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
73 gud-overload-alist))
74
75 (defun gud-massage-args (file args)
76 (error "GUD not properly entered."))
77
78 (defun gud-marker-filter (str)
79 (error "GUD not properly entered."))
80
81 (defun gud-find-file (f)
82 (error "GUD not properly entered."))
83 \f
84 ;; ======================================================================
85 ;; command definition
86
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
89 ;; user defined ones.
90
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
95 ;; GUD prefix.
96
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:
101
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
108
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)."
115 (list 'progn
116 (list 'defun func '(arg)
117 (or doc "")
118 '(interactive "p")
119 (list 'gud-call cmd 'arg))
120 (if key
121 (list 'define-key
122 '(current-local-map)
123 (concat "\C-c" key)
124 (list 'quote func)))
125 (if key
126 (list 'global-set-key
127 (list 'concat 'gud-key-prefix key)
128 (list 'quote func)))))
129
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)
134
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
137 ;; been set to nil.
138 (defvar gud-last-last-frame nil)
139
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.
142 ;;
143 ;; Each entry must define the following at startup:
144 ;;
145 ;;<name>
146 ;; comint-prompt-regexp
147 ;; gud-<name>-massage-args
148 ;; gud-<name>-marker-filter
149 ;; gud-<name>-find-file
150 ;;
151 ;; The job of the massage-args method is to modify the given list of
152 ;; debugger arguments before running the debugger.
153 ;;
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
159 ;; the rest.
160 ;;
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
163 ;; something else.
164 \f
165 ;; ======================================================================
166 ;; gdb functions
167
168 ;;; History of argument lists passed to gdb.
169 (defvar gud-gdb-history nil)
170
171 (defun gud-gdb-massage-args (file args)
172 (cons "-annotate=2" (cons file args)))
173
174
175 ;;
176 ;; In this world, there are gdb instance objects (of unspecified
177 ;; representation) and buffers associated with those objects.
178 ;;
179
180 ;;
181 ;; gdb-instance objects
182 ;;
183
184 (defun make-gdb-instance (proc)
185 "Create a gdb instance object from a gdb process."
186 (let ((instance (cons 'gdb-instance proc)))
187 (save-excursion
188 (set-buffer (process-buffer proc))
189 (if (not (equal gdb-buffer-instance instance))
190 (progn
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
194 instance))
195
196 (defun gdb-instance-process (inst) (cdr inst))
197
198 (defvar gdb-instance-variables '()
199 "A list of variables that are local to the gud buffer associated
200 with a gdb instance.")
201
202 (defmacro def-gdb-variable
203 (name accessor setter &optional default doc)
204 (`
205 (progn
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)))
210 (, (and accessor
211 (`
212 (defun (, accessor) (instance)
213 (let
214 ((buffer (gdb-get-instance-buffer instance 'gud)))
215 (and buffer
216 (save-excursion
217 (set-buffer buffer)
218 (, name))))))))
219 (, (and setter
220 (`
221 (defun (, setter) (instance val)
222 (let
223 ((buffer (gdb-get-instance-buffer instance 'gud)))
224 (and buffer
225 (save-excursion
226 (set-buffer buffer)
227 (setq (, name) val)))))))))))
228
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))))
234 (` (def-gdb-variable
235 (, var-name) (, accessor) (, setter)
236 (, default) (, doc)))))
237
238 (def-gdb-var buffer-instance nil
239 "In an instance buffer, the buffer's instance.")
240
241 (def-gdb-var buffer-type nil
242 "One of the symbols bound in gdb-instance-buffer-rules")
243
244 (def-gdb-var burst ""
245 "A string of characters from gdb that have not yet been processed.")
246
247 (def-gdb-var input-queue ()
248 "A list of high priority gdb command objects.")
249
250 (def-gdb-var idle-input-queue ()
251 "A list of low priority gdb command objects.")
252
253 (def-gdb-var prompting nil
254 "True when gdb is idle with no pending input.")
255
256 (def-gdb-var output-sink 'user
257 "The disposition of the output of the current gdb command.")
258
259 (def-gdb-var current-item nil
260 "The most recent command item sent to gdb.")
261
262 (def-gdb-var pending-triggers '()
263 "A list of trigger functions that have run later than their output
264 handlers.")
265
266 (defun in-gdb-instance-context (instance form)
267 "Funcall `form' in the gud buffer of `instance'"
268 (save-excursion
269 (set-buffer (gdb-get-instance-buffer instance 'gud))
270 (funcall form)))
271
272 ;; end of instance vars
273
274 ;;
275 ;; finding instances
276 ;;
277
278 (defun gdb-proc->instance (proc)
279 (save-excursion
280 (set-buffer (process-buffer proc))
281 gdb-buffer-instance))
282
283 (defun gdb-mru-instance-buffer ()
284 "Return the most recently used (non-auxiliary) gdb gud buffer."
285 (save-excursion
286 (gdb-goto-first-gdb-instance (buffer-list))))
287
288 (defun gdb-goto-first-gdb-instance (blist)
289 "Use gdb-mru-instance-buffer -- not this."
290 (and blist
291 (progn
292 (set-buffer (car blist))
293 (or (and gdb-buffer-instance
294 (eq gdb-buffer-type 'gud)
295 (car blist))
296 (gdb-find-first-gdb-instance (cdr blist))))))
297
298 (defun buffer-gdb-instance (buf)
299 (save-excursion
300 (set-buffer buf)
301 gdb-buffer-instance))
302
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."))))
308
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
313 program."
314 (in-gdb-instance-context
315 instance
316 (function (lambda () gud-target-name))))
317
318 ;; More than one buffer can be associated with a gdb instance.
319 ;;
320 ;; Each buffer has a TYPE -- an atom that identifies the function
321 ;; of that particular buffer.
322 ;;
323 ;; The usual gud interaction buffer is given the type `gud' and
324 ;; is constructed specially.
325 ;;
326 ;; Others are constructed by gdb-get-create-instance-buffer and
327 ;; named according to the rules set forth here:
328 ;;
329
330 (defvar gdb-instance-buffer-rules-assoc
331 '((gud error) ; gud buffers construct specially
332 (gdb-partial-output-buffer
333 gdb-partial-output-name
334 )
335 (gdb-registers-buffer
336 gdb-registers-buffer-name)
337 (gdb-breakpoints-buffer
338 gdb-breakpoints-buffer-name)
339 (gdb-frames-buffer
340 gdb-frames-buffer-name)))
341
342
343 ;;
344 ;; gdb communications
345 ;;
346
347 ;; input: things sent to gdb
348 ;;
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.
352 ;;
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:
356 ;;
357 ;; (INPUT-STRING HANDLER-FN)
358 ;;
359 ;;
360 ;; The handler function will be called from the
361 ;; partial-output buffer when the command completes.
362 ;;
363 ;; These lists are consumed tail first.
364 ;;
365
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"))))
371
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)
375 (progn
376 (gdb-send-item instance item)
377 (set-gdb-instance-prompting instance nil))
378 (set-gdb-instance-input-queue
379 instance
380 (cons item (gdb-instance-input-queue instance)))))
381
382 (defun gdb-instance-dequeue-input (instance)
383 (let ((queue (gdb-instance-input-queue instance)))
384 (and queue
385 (if (not (cdr queue))
386 (let ((answer (car queue)))
387 (set-gdb-instance-input-queue instance '())
388 answer)
389 (gdb-take-last-elt queue)))))
390
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)
394 (progn
395 (gdb-send-item instance item)
396 (set-gdb-instance-prompting instance nil))
397 (set-gdb-instance-idle-input-queue
398 instance
399 (cons item (gdb-instance-idle-input-queue instance)))))
400
401 (defun gdb-instance-dequeue-idle-input (instance)
402 (let ((queue (gdb-instance-idle-input-queue instance)))
403 (and queue
404 (if (not (cdr queue))
405 (let ((answer (car queue)))
406 (set-gdb-instance-idle-input-queue instance '())
407 answer)
408 (gdb-take-last-elt queue)))))
409
410 (defun gdb-take-last-elt (l)
411 "Don't use this in general."
412 (if (cdr (cdr l))
413 (gdb-take-last-elt (cdr l))
414 (let ((answer (car (cdr l))))
415 (setcdr l '())
416 answer)))
417
418
419 ;;
420 ;; output -- things gdb prints to emacs
421 ;;
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
425 ;;
426 ;; The tag is a string obeying symbol syntax.
427 ;;
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
432 ;; any newlines.
433 ;;
434
435
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)))
441
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.")
460
461
462 (defun gdb-ignore-annotation (instance args)
463 nil)
464
465 (defconst gdb-source-spec-regexp
466 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x[a-f0-9]*")
467
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.
472 (setq gud-last-frame
473 (cons
474 (substring args (match-beginning 1) (match-end 1))
475 (string-to-int (substring args
476 (match-beginning 2)
477 (match-end 2))))))
478
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)))
483 (cond
484 ((eq sink 'user) t)
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)))
493 (if highest
494 (gdb-send-item instance highest)
495 (let ((lowest (gdb-instance-dequeue-idle-input instance)))
496 (if lowest
497 (gdb-send-item instance lowest)
498 (set-gdb-instance-prompting instance t))))))
499
500 (defun gdb-send-item (instance item)
501 (dbug 'sending
502 (function
503 (lambda ()
504 (insert (format "%s\n" item)))))
505 (set-gdb-instance-current-item instance item)
506 (if (stringp item)
507 (progn
508 (set-gdb-instance-output-sink instance 'user)
509 (process-send-string (gdb-instance-process instance)
510 item))
511 (progn
512 (gdb-clear-partial-output instance)
513 (set-gdb-instance-output-sink instance 'pre-emacs)
514 (process-send-string (gdb-instance-process instance)
515 (car item)))))
516
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)))
522 (cond
523 ((eq sink 'user) t)
524 ((eq sink 'emacs)
525 (set-gdb-instance-output-sink instance 'post-emacs)
526 (let ((handler
527 (car (cdr (gdb-instance-current-item instance)))))
528 (save-excursion
529 (set-buffer (gdb-get-create-instance-buffer
530 instance 'gdb-partial-output-buffer))
531 (funcall handler))))
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.")))))
538
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)))
545 (cond
546 ((eq sink 'user) t)
547 ((eq sink 'pre-emacs)
548 (set-gdb-instance-output-sink instance 'emacs))
549
550 ((eq sink 'emacs)
551 (set-gdb-instance-output-sink instance 'user)
552 (error "Output sink phase error 3."))
553
554 ((eq sink 'post-emacs)
555 (set-gdb-instance-output-sink instance 'user)
556 (error "Output sink phase error 3.")))))
557
558
559 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command output-handler)
560 (`
561 (defun (, name) (instance ignored)
562 (if (and ((, demand-predicate) instance)
563 (not (member '(, name)
564 (gdb-instance-pending-triggers instance))))
565 (progn
566 (gdb-instance-enqueue-idle-input
567 instance
568 (list (, gdb-command) '(, output-handler)))
569 (set-gdb-instance-pending-triggers
570 instance
571 (cons '(, name)
572 (gdb-instance-pending-triggers instance))))))))
573
574 (defmacro def-gdb-auto-update-handler (name trigger buf-key)
575 (`
576 (defun (, name) ()
577 (set-gdb-instance-pending-triggers
578 instance
579 (delq '(, trigger)
580 (gdb-instance-pending-triggers instance)))
581 (let ((buf (gdb-get-instance-buffer instance
582 '(, buf-key))))
583 (and buf
584 (save-excursion
585 (set-buffer buf)
586 (let ((p (point)))
587 (kill-region (point-min) (point-max))
588 (insert-buffer (gdb-get-create-instance-buffer
589 instance
590 'gdb-partial-output-buffer))
591 (goto-char p))))))))
592
593 (defmacro def-gdb-auto-updated-buffer
594 (buffer-key trigger-name gdb-command output-handler-name)
595 (`
596 (progn
597 (def-gdb-auto-update-trigger (, trigger-name)
598 ;; The demand predicate:
599 (lambda (instance)
600 (gdb-get-instance-buffer instance '(, buffer-key)))
601 (, gdb-command)
602 (, output-handler-name))
603 (def-gdb-auto-update-handler (, output-handler-name)
604 (, trigger-name) (, buffer-key)))))
605
606
607
608 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
609 ;; This defines the auto update rule for buffers of type
610 ;; `gdb-breakpoints-buffer'.
611 ;;
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
615
616 ;; To update the buffer, this command is sent to gdb.
617 "server info breakpoints\n"
618
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)
623
624 (def-gdb-auto-updated-buffer gdb-frames-buffer
625 gdb-invalidate-frames
626 "server where\n"
627 gdb-info-frames-handler)
628
629
630 (def-gdb-auto-updated-buffer gdb-registers-buffer
631 gdb-invalidate-registers
632 "server info registers\n"
633 gdb-info-registers-handler)
634
635 ;;
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
642 ;; for emacs.
643 ;;
644
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:
648
649 USER -- the output should be appended to the gud
650 buffer.
651
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.")
655
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
660 buffer."
661
662 (save-match-data
663 (let (
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
667 (output ""))
668
669 ;; Process all the complete markers in this chunk.
670
671 (while (string-match "\n\032\032\\(.*\\)\n" burst)
672 (let ((annotation (substring burst
673 (match-beginning 1)
674 (match-end 1))))
675
676 ;; Stuff prior to the match is just ordinary output.
677 ;; It is either concatenated to OUTPUT or directed
678 ;; elsewhere.
679 (setq output
680 (gdb-concat-output
681 instance
682 output
683 (substring burst 0 (match-beginning 0))))
684
685 ;; Take that stuff off the burst.
686 (setq burst (substring burst (match-end 0)))
687
688 ;; Parse the tag from the annotation, and maybe its arguments.
689 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
690 (let* ((annotation-type (substring annotation
691 (match-beginning 1)
692 (match-end 1)))
693 (annotation-arguments (substring annotation
694 (match-beginning 2)
695 (match-end 2)))
696 (annotation-rule (assoc annotation-type
697 gdb-annotation-rules)))
698 ;; Call the handler for this annotation.
699 (dbug 'annotation
700 '(lambda () (insert annotation-type "\n")))
701 (if annotation-rule
702 (funcall (car (cdr annotation-rule))
703 instance
704 annotation-arguments)
705 (error "Bogon gdb annotation %s." annotation)))))
706
707
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.*\\'"
711 burst)
712 (progn
713 ;; Everything before the potential marker start can be output.
714 (setq output
715 (gdb-concat-output
716 instance
717 output
718 (substring burst 0 (match-beginning 0))))
719
720 ;; Everything after, we save, to combine with later input.
721 (setq burst (substring burst (match-beginning 0))))
722
723 ;; In case we know the burst contains no partial annotations:
724 (progn
725 (setq output (gdb-concat-output instance output burst))
726 (setq burst "")))
727
728 ;; Save the remaining burst for the next call to this function.
729 (set-gdb-instance-burst instance burst)
730 output)))
731
732 (defun gdb-concat-output (instance so-far new)
733 (let ((sink (gdb-instance-output-sink instance)))
734 (cond
735 ((eq sink 'user) (concat so-far new))
736 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
737 ((eq sink 'emacs)
738 (gdb-append-to-partial-output instance new)
739 so-far)
740 (t (error "Bogon output sink %d" sink)))))
741
742 (defun gdb-append-to-partial-output (instance string)
743 (save-excursion
744 (set-buffer
745 (gdb-get-create-instance-buffer
746 instance 'gdb-partial-output-buffer))
747 (goto-char (point-max))
748 (insert string)))
749
750 (defun gdb-clear-partial-output (instance)
751 (save-excursion
752 (set-buffer
753 (gdb-get-create-instance-buffer
754 instance 'gdb-partial-output-buffer))
755 (kill-region (point-min) (point-max))))
756
757 ;;
758 ;; Instance Buffers.
759 ;;
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).
763 ;;
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.
768 ;;
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.
772 ;;
773
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'."
777 (save-excursion
778 (gdb-look-for-tagged-buffer instance key (buffer-list))))
779
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)))
787 (save-excursion
788 (set-buffer new)
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)
793 new))))
794
795 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
796
797 (defun gdb-look-for-tagged-buffer (instance key bufs)
798 (and bufs
799 (set-buffer (car bufs))
800 (or (and (eq gdb-buffer-instance instance)
801 (eq gdb-buffer-type key)
802 (car bufs))
803 (gdb-look-for-tagged-buffer instance key (cdr bufs)))))
804
805 (defun gdb-instance-buffer-p (buf)
806 (save-excursion
807 (set-buffer buf)
808 (and gdb-buffer-type
809 (not (eq gdb-buffer-type 'gud)))))
810
811 ;;
812 ;; partial-output buffers
813 ;;
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.
820 ;;
821
822 (defun gdb-partial-output-name (instance)
823 (concat "*partial-output-"
824 (gdb-instance-target-string instance)
825 "*"))
826
827 ;;
828 ;; Backtrace buffers
829 ;;
830
831 (defun gdb-frames-buffer-name (instance)
832 (save-excursion
833 (set-buffer (process-buffer (gdb-instance-process instance)))
834 (concat "*stack frames of "
835 (gdb-instance-target-string instance) "*")))
836
837 (defun gud-display-frames-buffer (instance)
838 (interactive (list (gdb-needed-default-instance)))
839 (display-buffer
840 (gdb-get-create-instance-buffer instance
841 'gdb-frames-buffer)))
842
843 ;;
844 ;; Breakpoint buffers
845 ;;
846
847 (defun gdb-breakpoints-buffer-name (instance)
848 (save-excursion
849 (set-buffer (process-buffer (gdb-instance-process instance)))
850 (concat "*breakpoints of " (gdb-instance-target-string instance) "*")))
851
852 (defun gud-display-breakpoints-buffer (instance)
853 (interactive (list (gdb-needed-default-instance)))
854 (display-buffer
855 (gdb-get-create-instance-buffer instance
856 'gdb-breakpoints-buffer)))
857
858
859 ;;
860 ;; Registers buffers
861 ;;
862
863 (defun gdb-registers-buffer-name (instance)
864 (save-excursion
865 (set-buffer (process-buffer (gdb-instance-process instance)))
866 (concat "*registers of " (gdb-instance-target-string instance) "*")))
867
868 (defun gud-display-registers-buffer (instance)
869 (interactive (list (gdb-needed-default-instance)))
870 (display-buffer
871 (gdb-get-create-instance-buffer instance
872 'gdb-registers-buffer)))
873
874
875
876
877 (defun gud-gdb-find-file (f)
878 (find-file-noselect f))
879
880 ;;;###autoload
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."
885 (interactive
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 ")
890 nil nil
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)
896 ))
897
898 (gud-common-init command-line)
899
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.")
911
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)))
916 )
917
918 \f
919 ;; ======================================================================
920 ;; sdb functions
921
922 ;;; History of argument lists passed to sdb.
923 (defvar gud-sdb-history nil)
924
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.")
927
928 (defvar gud-sdb-lastfile nil)
929
930 (defun gud-sdb-massage-args (file args)
931 (cons file args))
932
933 (defun gud-sdb-marker-filter (string)
934 (cond
935 ;; System V Release 3.2 uses this format
936 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
937 string)
938 (setq gud-last-frame
939 (cons
940 (substring string (match-beginning 2) (match-end 2))
941 (string-to-int
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"
945 string)
946 (setq gud-sdb-lastfile
947 (substring string (match-beginning 2) (match-end 2))))
948 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
949 (setq gud-last-frame
950 (cons
951 gud-sdb-lastfile
952 (string-to-int
953 (substring string (match-beginning 1) (match-end 1))))))
954 (t
955 (setq gud-sdb-lastfile nil)))
956 string)
957
958 (defun gud-sdb-find-file (f)
959 (if gud-sdb-needs-tags
960 (find-tag-noselect f)
961 (find-file-noselect f)))
962
963 ;;;###autoload
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."
968 (interactive
969 (list (read-from-minibuffer "Run sdb (like this): "
970 (if (consp gud-sdb-history)
971 (car gud-sdb-history)
972 "sdb ")
973 nil nil
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)
981 ))
982
983 (gud-common-init command-line)
984
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.")
993
994 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
995 (run-hooks 'sdb-mode-hook)
996 )
997 \f
998 ;; ======================================================================
999 ;; dbx functions
1000
1001 ;;; History of argument lists passed to dbx.
1002 (defvar gud-dbx-history nil)
1003
1004 (defun gud-dbx-massage-args (file args)
1005 (cons file args))
1006
1007 (defun gud-dbx-marker-filter (string)
1008 (if (or (string-match
1009 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1010 string)
1011 (string-match
1012 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1013 string))
1014 (setq gud-last-frame
1015 (cons
1016 (substring string (match-beginning 2) (match-end 2))
1017 (string-to-int
1018 (substring string (match-beginning 1) (match-end 1))))))
1019 string)
1020
1021 (defun gud-dbx-find-file (f)
1022 (find-file-noselect f))
1023
1024 ;;;###autoload
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."
1029 (interactive
1030 (list (read-from-minibuffer "Run dbx (like this): "
1031 (if (consp gud-dbx-history)
1032 (car gud-dbx-history)
1033 "dbx ")
1034 nil nil
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)
1039 ))
1040
1041 (gud-common-init command-line)
1042
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.")
1055
1056 (setq comint-prompt-regexp "^[^)]*dbx) *")
1057 (run-hooks 'dbx-mode-hook)
1058 )
1059 \f
1060 ;; ======================================================================
1061 ;; xdb (HP PARISC debugger) functions
1062
1063 ;;; History of argument lists passed to xdb.
1064 (defvar gud-xdb-history nil)
1065
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.
1070
1071 The file names should be absolute, or relative to the directory
1072 containing the executable being debugged.")
1073
1074 (defun gud-xdb-massage-args (file args)
1075 (nconc (let ((directories gud-xdb-directories)
1076 (result nil))
1077 (while directories
1078 (setq result (cons (car directories) (cons "-d" result)))
1079 (setq directories (cdr directories)))
1080 (nreverse (cons file result)))
1081 args))
1082
1083 (defun gud-xdb-file-name (f)
1084 "Transform a relative pathname to a full pathname in xdb mode"
1085 (let ((result nil))
1086 (if (file-exists-p f)
1087 (setq result (expand-file-name f))
1088 (let ((directories gud-xdb-directories))
1089 (while directories
1090 (let ((path (concat (car directories) "/" f)))
1091 (if (file-exists-p path)
1092 (setq result (expand-file-name path)
1093 directories nil)))
1094 (setq directories (cdr directories)))))
1095 result))
1096
1097 ;; xdb does not print the lines all at once, so we have to accumulate them
1098 (defvar gud-xdb-accumulation "")
1099
1100 (defun gud-xdb-marker-filter (string)
1101 (let (result)
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)))
1107 (if result
1108 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
1109 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1110 result))
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)))))
1115 (if file
1116 (setq gud-last-frame (cons file line))))))
1117 (or result "")))
1118
1119 (defun gud-xdb-find-file (f)
1120 (let ((realf (gud-xdb-file-name f)))
1121 (if realf (find-file-noselect realf))))
1122
1123 ;;;###autoload
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.
1128
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."
1131 (interactive
1132 (list (read-from-minibuffer "Run xdb (like this): "
1133 (if (consp gud-xdb-history)
1134 (car gud-xdb-history)
1135 "xdb ")
1136 nil nil
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)))
1141
1142 (gud-common-init command-line)
1143
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.")
1155
1156 (setq comint-prompt-regexp "^>")
1157 (make-local-variable 'gud-xdb-accumulation)
1158 (setq gud-xdb-accumulation "")
1159 (run-hooks 'xdb-mode-hook))
1160 \f
1161 ;; ======================================================================
1162 ;; perldb functions
1163
1164 ;;; History of argument lists passed to perldb.
1165 (defvar gud-perldb-history nil)
1166
1167 (defun gud-perldb-massage-args (file args)
1168 (cons "-d" (cons file (cons "-emacs" args))))
1169
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
1175 ;; filter.
1176 (defvar gud-perldb-marker-acc "")
1177
1178 (defun gud-perldb-marker-filter (string)
1179 (save-match-data
1180 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
1181 (let ((output ""))
1182
1183 ;; Process all the complete markers in this chunk.
1184 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1185 gud-perldb-marker-acc)
1186 (setq
1187
1188 ;; Extract the frame position from the marker.
1189 gud-last-frame
1190 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
1191 (string-to-int (substring gud-perldb-marker-acc
1192 (match-beginning 2)
1193 (match-end 2))))
1194
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)))
1199
1200 ;; Set the accumulator to the remaining text.
1201 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
1202
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)
1209 (progn
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))))
1213
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))))
1217
1218 (setq output (concat output gud-perldb-marker-acc)
1219 gud-perldb-marker-acc ""))
1220
1221 output)))
1222
1223 (defun gud-perldb-find-file (f)
1224 (find-file-noselect f))
1225
1226 ;;;###autoload
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."
1231 (interactive
1232 (list (read-from-minibuffer "Run perldb (like this): "
1233 (if (consp gud-perldb-history)
1234 (car gud-perldb-history)
1235 "perl ")
1236 nil nil
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)
1241 ))
1242
1243 (gud-common-init command-line)
1244
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.")
1254
1255 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1256 (run-hooks 'perldb-mode-hook)
1257 )
1258
1259 ;;
1260 ;; End of debugger-specific information
1261 ;;
1262
1263 \f
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.
1267 ;;;
1268 ;;; If the buffer looks like this:
1269 ;;; --------------------
1270 ;;; (gdb) set args foo bar
1271 ;;; (gdb) -!-
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
1275 ;;; this:
1276 ;;; --------------------
1277 ;;; (gdb) set args foo bar
1278 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1279 ;;; (gdb) -!-
1280 ;;; --------------------
1281 ;;; Essentially, the old prompt is deleted, and the command's output
1282 ;;; and the new prompt take its place.
1283 ;;;
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.
1297 ;;;
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
1303 ;;; prompt alone.
1304 (defvar gud-delete-prompt-marker nil)
1305
1306 \f
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)
1311
1312 (defun gud-mode ()
1313 "Major mode for interacting with an inferior debugger process.
1314
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.
1318
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
1321 or step operation:
1322
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.
1326
1327 \\[gud-remove] removes breakpoints on the current file and line.
1328
1329 \\[gud-refresh] displays in the source window the last line referred to
1330 in the gud buffer.
1331
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.
1336
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.
1339
1340 The above commands are common to all supported debuggers except xdb which
1341 does not support stepping instructions.
1342
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.
1346
1347 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1348 frame. \\[gud-down] drops back down through one.
1349
1350 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1351 the current function and stops.
1352
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.
1355
1356 All pre-defined functions for which the concept make sense repeat
1357 themselves the appropriate number of times if you give a prefix
1358 argument.
1359
1360 You may use the `gud-def' macro in the initialization hook to define other
1361 commands.
1362
1363 Other commands for interacting with the debugger process are inherited from
1364 comint mode, which see."
1365 (interactive)
1366 (comint-mode)
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)
1377 )
1378
1379 (defvar gud-comint-buffer nil)
1380
1381 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1382 (defun gud-chop-words (string)
1383 (let ((i 0) (beg 0)
1384 (len (length string))
1385 (words nil))
1386 (while (< i len)
1387 (if (memq (aref string i) '(?\t ? ))
1388 (progn
1389 (setq words (cons (substring string beg i) words)
1390 beg (1+ i))
1391 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1392 (setq beg (1+ beg)))
1393 (setq i (1+ beg)))
1394 (setq i (1+ i))))
1395 (if (< beg len)
1396 (setq words (cons (substring string beg) words)))
1397 (nreverse words)))
1398
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
1403 program.")
1404
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)))
1411 (setq w (cdr w)))
1412 (car w)))
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))
1423 (gud-mode)
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)
1428 (gud-set-buffer)
1429 )
1430
1431 (defun gud-set-buffer ()
1432 (cond ((eq major-mode 'gud-mode)
1433 (setq gud-comint-buffer (current-buffer)))))
1434
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.
1438
1439 (defun gud-filter (proc string)
1440 ;; Here's where the actual buffer insertion is done
1441 (let ((inhibit-quit t))
1442 (save-excursion
1443 (set-buffer (process-buffer proc))
1444 (let (moving output-after-point)
1445 (save-excursion
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)
1449 (progn
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)))))))
1464
1465 (defun gud-sentinel (proc msg)
1466 (cond ((null (buffer-name (process-buffer proc)))
1467 ;; buffer killed
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
1476 (concat ": "
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
1481 (unwind-protect
1482 (progn
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))
1487 (if (eobp)
1488 (insert ?\n mode-name " " msg)
1489 (save-excursion
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))))))
1499
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."
1503 (interactive)
1504 (if gud-last-frame
1505 (progn
1506 (gud-set-buffer)
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))))
1510
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.
1517
1518 (defun gud-display-line (true-file line)
1519 (let* ((buffer (gud-find-file true-file))
1520 (window (display-buffer buffer))
1521 (pos))
1522 ;;; (if (equal buffer (current-buffer))
1523 ;;; nil
1524 ;;; (setq buffer-read-only nil))
1525 (save-excursion
1526 ;;; (setq buffer-read-only t)
1527 (set-buffer buffer)
1528 (save-restriction
1529 (widen)
1530 (goto-line line)
1531 (setq pos (point))
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)))
1537 (widen)
1538 (goto-char pos))))
1539 (set-window-point window overlay-arrow-position)))
1540
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:
1545
1546 (defun gud-format-command (str arg)
1547 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
1548 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
1549 (setq str (concat
1550 (substring str (match-beginning 1) (match-end 1))
1551 (file-name-nondirectory (if insource
1552 (buffer-file-name)
1553 (car gud-last-frame)))
1554 (substring str (match-beginning 2) (match-end 2)))))
1555 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
1556 (setq str (concat
1557 (substring str (match-beginning 1) (match-end 1))
1558 (file-name-directory (if insource
1559 (buffer-file-name)
1560 (car gud-last-frame)))
1561 (substring str (match-beginning 2) (match-end 2)))))
1562 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
1563 (setq str (concat
1564 (substring str (match-beginning 1) (match-end 1))
1565 (if insource
1566 (save-excursion
1567 (beginning-of-line)
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)
1573 (setq str (concat
1574 (substring str (match-beginning 1) (match-end 1))
1575 (find-c-expr)
1576 (substring str (match-beginning 2) (match-end 2)))))
1577 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
1578 (setq str (concat
1579 (substring str (match-beginning 1) (match-end 1))
1580 (gud-read-address)
1581 (substring str (match-beginning 2) (match-end 2)))))
1582 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
1583 (setq str (concat
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)))))
1587 )
1588 str
1589 )
1590
1591 (defun gud-read-address ()
1592 "Return a string containing the core-address found in the buffer at point."
1593 (save-excursion
1594 (let ((pt (point)) found begin)
1595 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1596 (cond
1597 (found (forward-char 2)
1598 (buffer-substring found
1599 (progn (re-search-forward "[^0-9a-f]")
1600 (forward-char -1)
1601 (point))))
1602 (t (setq begin (progn (re-search-backward "[^0-9]")
1603 (forward-char 1)
1604 (point)))
1605 (forward-char 1)
1606 (re-search-forward "[^0-9]")
1607 (forward-char -1)
1608 (buffer-substring begin (point)))))))
1609
1610 (defun gud-call (fmt &optional arg)
1611 (let ((msg (gud-format-command fmt arg)))
1612 (message "Command: %s" msg)
1613 (sit-for 0)
1614 (gud-basic-call msg)))
1615
1616 (defun gud-basic-call (command)
1617 "Invoke the debugger COMMAND displaying source in other window."
1618 (interactive)
1619 (gud-set-buffer)
1620 (let ((command (concat command "\n"))
1621 (proc (get-buffer-process gud-comint-buffer)))
1622
1623 ;; Arrange for the current prompt to get deleted.
1624 (save-excursion
1625 (set-buffer gud-comint-buffer)
1626 (goto-char (process-mark proc))
1627 (beginning-of-line)
1628 (if (looking-at comint-prompt-regexp)
1629 (set-marker gud-delete-prompt-marker (point))))
1630 (process-send-string proc command)))
1631
1632 (defun gud-refresh (&optional arg)
1633 "Fix up a possibly garbled display, and redraw the arrow."
1634 (interactive "P")
1635 (recenter arg)
1636 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1637 (gud-display-frame))
1638 \f
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.
1641 ;;;
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.
1645
1646 (defun find-c-expr ()
1647 "Returns the C expr that surrounds point."
1648 (interactive)
1649 (save-excursion
1650 (let ((p) (expr) (test-expr))
1651 (setq p (point))
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)))
1658 (goto-char p)
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))
1663 )
1664 (buffer-substring (car expr) (cdr expr)))))
1665
1666 (defun expr-cur ()
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))
1674 (expr-forward-sexp)
1675 (setq end (point))
1676 (if (>= p end)
1677 (progn
1678 (setq begin p)
1679 (goto-char p)
1680 (expr-forward-sexp)
1681 (setq end (point))
1682 )
1683 )
1684 (goto-char begin)
1685 (cons begin end)))
1686
1687 (defun expr-backward-sexp ()
1688 "Version of `backward-sexp' that catches errors."
1689 (condition-case nil
1690 (backward-sexp)
1691 (error t)))
1692
1693 (defun expr-forward-sexp ()
1694 "Version of `forward-sexp' that catches errors."
1695 (condition-case nil
1696 (forward-sexp)
1697 (error t)))
1698
1699 (defun expr-prev ()
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))
1707 (expr-forward-sexp)
1708 (setq end (point))
1709 (goto-char begin)
1710 (cons begin end)))
1711
1712 (defun expr-next ()
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))
1718 (expr-forward-sexp)
1719 (expr-forward-sexp)
1720 (setq end (point))
1721 (expr-backward-sexp)
1722 (setq begin (point))
1723 (cons begin end)))
1724
1725 (defun expr-compound-sep (span-start span-end)
1726 "Returns '.' for '->' & '.', returns ' ' for white space,
1727 returns '?' for other punctuation."
1728 (let ((result ? )
1729 (syntax))
1730 (while (< span-start span-end)
1731 (setq syntax (char-syntax (char-after span-start)))
1732 (cond
1733 ((= syntax ? ) t)
1734 ((= syntax ?.) (setq syntax (char-after span-start))
1735 (cond
1736 ((= syntax ?.) (setq result ?.))
1737 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1738 (setq result ?.)
1739 (setq span-start (+ span-start 1)))
1740 (t (setq span-start span-end)
1741 (setq result ??)))))
1742 (setq span-start (+ span-start 1)))
1743 result))
1744
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:
1751 Expr -> Expr
1752 Expr . Expr
1753 Expr (Expr)
1754 Expr [Expr]
1755 (Expr) Expr
1756 [Expr] Expr"
1757 (let ((span-start (cdr first))
1758 (span-end (car second))
1759 (syntax))
1760 (setq syntax (expr-compound-sep span-start span-end))
1761 (cond
1762 ((= (car first) (car second)) nil)
1763 ((= (cdr first) (cdr second)) nil)
1764 ((= syntax ?.) t)
1765 ((= syntax ? )
1766 (setq span-start (char-after (- span-start 1)))
1767 (setq span-end (char-after span-end))
1768 (cond
1769 ((= span-start ?) ) t )
1770 ((= span-start ?] ) t )
1771 ((= span-end ?( ) t )
1772 ((= span-end ?[ ) t )
1773 (t nil))
1774 )
1775 (t nil))))
1776
1777 (provide 'gud)
1778
1779 ;;; gud.el ends here
1780
1781 (defun toggle-bp-this-line ()
1782 (interactive)
1783 (save-excursion
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!!!
1790 (concat
1791 (if (eq ?y (char-after (match-beginning 2)))
1792 "server disable "
1793 "server enable ")
1794 (buffer-substring (match-beginning 0)
1795 (match-end 1))
1796 "\n"))))))
1797
1798