5f7d52a7cf705aa27e4374387ed98d072e4d637e
[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 ;; Cygnus Support added support for gdb's --annotate=2.
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-goto-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 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
373 ;; is a query, or other non-top-level prompt. To guarantee stuff will get
374 ;; sent to the top-level prompt, currently it must be put in the idle queue.
375 (defun gdb-instance-enqueue-input (instance item)
376 "Enqueue an input item (a string or a list) for a gdb instance."
377 (if (gdb-instance-prompting instance)
378 (progn
379 (gdb-send-item instance item)
380 (set-gdb-instance-prompting instance nil))
381 (set-gdb-instance-input-queue
382 instance
383 (cons item (gdb-instance-input-queue instance)))))
384
385 (defun gdb-instance-dequeue-input (instance)
386 (let ((queue (gdb-instance-input-queue instance)))
387 (and queue
388 (if (not (cdr queue))
389 (let ((answer (car queue)))
390 (set-gdb-instance-input-queue instance '())
391 answer)
392 (gdb-take-last-elt queue)))))
393
394 (defun gdb-instance-enqueue-idle-input (instance item)
395 "Enqueue idle input (a string or a list) for a gdb instance."
396 (if (gdb-instance-prompting instance)
397 (progn
398 (gdb-send-item instance item)
399 (set-gdb-instance-prompting instance nil))
400 (set-gdb-instance-idle-input-queue
401 instance
402 (cons item (gdb-instance-idle-input-queue instance)))))
403
404 (defun gdb-instance-dequeue-idle-input (instance)
405 (let ((queue (gdb-instance-idle-input-queue instance)))
406 (and queue
407 (if (not (cdr queue))
408 (let ((answer (car queue)))
409 (set-gdb-instance-idle-input-queue instance '())
410 answer)
411 (gdb-take-last-elt queue)))))
412
413 (defun gdb-take-last-elt (l)
414 "Don't use this in general."
415 (if (cdr (cdr l))
416 (gdb-take-last-elt (cdr l))
417 (let ((answer (car (cdr l))))
418 (setcdr l '())
419 answer)))
420
421
422 ;;
423 ;; output -- things gdb prints to emacs
424 ;;
425 ;; GDB output is a stream interrupted by annotations.
426 ;; Annotations can be recognized by their beginning
427 ;; with \C-j\C-z\C-z<tag><opt>\C-j
428 ;;
429 ;; The tag is a string obeying symbol syntax.
430 ;;
431 ;; The optional part `<opt>' can be either the empty string
432 ;; or a space followed by more data relating to the annotation.
433 ;; For example, the SOURCE annotation is followed by a filename,
434 ;; line number and various useless goo. This data must not include
435 ;; any newlines.
436 ;;
437
438
439 (defun gud-gdb-marker-filter (string)
440 "A gud marker filter for gdb."
441 ;; Bogons don't tell us the process except through scoping crud.
442 (let ((instance (gdb-proc->instance proc)))
443 (gdb-output-burst instance string)))
444
445 (defvar gdb-annotation-rules
446 '(("frames-invalid" gdb-invalidate-frames)
447 ("breakpoints-invalid" gdb-invalidate-breakpoints)
448 ("pre-prompt" gdb-pre-prompt)
449 ("prompt" gdb-prompt)
450 ("commands" gdb-subprompt)
451 ("overload-choice" gdb-subprompt)
452 ("query" gdb-subprompt)
453 ("prompt-for-continue" gdb-subprompt)
454 ("post-prompt" gdb-post-prompt)
455 ("source" gdb-source)
456 )
457 "An assoc mapping annotation tags to functions which process them.")
458
459
460 (defun gdb-ignore-annotation (instance args)
461 nil)
462
463 (defconst gdb-source-spec-regexp
464 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x[a-f0-9]*")
465
466 (defun gdb-source (instance args)
467 "Do not use this except as an annotation handler."
468 (string-match gdb-source-spec-regexp args)
469 ;; Extract the frame position from the marker.
470 (setq gud-last-frame
471 (cons
472 (substring args (match-beginning 1) (match-end 1))
473 (string-to-int (substring args
474 (match-beginning 2)
475 (match-end 2))))))
476
477 (defun gdb-prompt (instance ignored)
478 "An annotation handler for `prompt'.
479 This sends the next command (if any) to gdb."
480 (let ((sink (gdb-instance-output-sink instance)))
481 (cond
482 ((eq sink 'user) t)
483 ((eq sink 'post-emacs)
484 (set-gdb-instance-output-sink instance 'user))
485 ((or (eq sink 'emacs)
486 (eq sink 'pre-emacs))
487 (set-gdb-instance-output-sink instance 'user)
488 (error "Phase error in gdb-prompt (got %s)" sink))
489 (t (set-gdb-instance-output-sink instance 'user))))
490 (let ((highest (gdb-instance-dequeue-input instance)))
491 (if highest
492 (gdb-send-item instance highest)
493 (let ((lowest (gdb-instance-dequeue-idle-input instance)))
494 (if lowest
495 (gdb-send-item instance lowest)
496 (set-gdb-instance-prompting instance t))))))
497
498 (defun gdb-subprompt (instance ignored)
499 "An annotation handler for non-top-level prompts."
500 (let ((highest (gdb-instance-dequeue-input instance)))
501 (if highest
502 (gdb-send-item instance highest)
503 (set-gdb-instance-prompting instance t))))
504
505 (defun gdb-send-item (instance item)
506 (dbug 'sending
507 (function
508 (lambda ()
509 (insert (format "%s\n" item)))))
510 (set-gdb-instance-current-item instance item)
511 (if (stringp item)
512 (progn
513 (set-gdb-instance-output-sink instance 'user)
514 (process-send-string (gdb-instance-process instance)
515 item))
516 (progn
517 (gdb-clear-partial-output instance)
518 (set-gdb-instance-output-sink instance 'pre-emacs)
519 (process-send-string (gdb-instance-process instance)
520 (car item)))))
521
522 (defun gdb-pre-prompt (instance ignored)
523 "An annotation handler for `pre-prompt'.
524 This terminates the collection of output from a previous
525 command if that happens to be in effect."
526 (let ((sink (gdb-instance-output-sink instance)))
527 (cond
528 ((eq sink 'user) t)
529 ((eq sink 'emacs)
530 (set-gdb-instance-output-sink instance 'post-emacs)
531 (let ((handler
532 (car (cdr (gdb-instance-current-item instance)))))
533 (save-excursion
534 (set-buffer (gdb-get-create-instance-buffer
535 instance 'gdb-partial-output-buffer))
536 (funcall handler))))
537 ((eq sink 'pre-emacs)
538 (set-gdb-instance-output-sink instance 'user)
539 (error "Output sink phase error 1."))
540 ((eq sink 'post-emacs)
541 (set-gdb-instance-output-sink instance 'user)
542 (error "Output sink phase error 2.")))))
543
544 (defun gdb-post-prompt (instance ignored)
545 "An annotation handler for `post-prompt'.
546 This begins the collection of output from the current
547 command if that happens to be appropriate."
548 (gdb-invalidate-registers instance ignored)
549 (let ((sink (gdb-instance-output-sink instance)))
550 (cond
551 ((eq sink 'user) t)
552 ((eq sink 'pre-emacs)
553 (set-gdb-instance-output-sink instance 'emacs))
554
555 ((eq sink 'emacs)
556 (set-gdb-instance-output-sink instance 'user)
557 (error "Output sink phase error 3."))
558
559 ((eq sink 'post-emacs)
560 (set-gdb-instance-output-sink instance 'user)
561 (error "Output sink phase error 3.")))))
562
563
564 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command output-handler)
565 (`
566 (defun (, name) (instance ignored)
567 (if (and ((, demand-predicate) instance)
568 (not (member '(, name)
569 (gdb-instance-pending-triggers instance))))
570 (progn
571 (gdb-instance-enqueue-idle-input
572 instance
573 (list (, gdb-command) '(, output-handler)))
574 (set-gdb-instance-pending-triggers
575 instance
576 (cons '(, name)
577 (gdb-instance-pending-triggers instance))))))))
578
579 (defmacro def-gdb-auto-update-handler (name trigger buf-key)
580 (`
581 (defun (, name) ()
582 (set-gdb-instance-pending-triggers
583 instance
584 (delq '(, trigger)
585 (gdb-instance-pending-triggers instance)))
586 (let ((buf (gdb-get-instance-buffer instance
587 '(, buf-key))))
588 (and buf
589 (save-excursion
590 (set-buffer buf)
591 (let ((p (point)))
592 (kill-region (point-min) (point-max))
593 (insert-buffer (gdb-get-create-instance-buffer
594 instance
595 'gdb-partial-output-buffer))
596 (goto-char p))))))))
597
598 (defmacro def-gdb-auto-updated-buffer
599 (buffer-key trigger-name gdb-command output-handler-name)
600 (`
601 (progn
602 (def-gdb-auto-update-trigger (, trigger-name)
603 ;; The demand predicate:
604 (lambda (instance)
605 (gdb-get-instance-buffer instance '(, buffer-key)))
606 (, gdb-command)
607 (, output-handler-name))
608 (def-gdb-auto-update-handler (, output-handler-name)
609 (, trigger-name) (, buffer-key)))))
610
611
612
613 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
614 ;; This defines the auto update rule for buffers of type
615 ;; `gdb-breakpoints-buffer'.
616 ;;
617 ;; It defines a function to serve as the annotation handler that
618 ;; handles the `foo-invalidated' message. That function is called:
619 gdb-invalidate-breakpoints
620
621 ;; To update the buffer, this command is sent to gdb.
622 "server info breakpoints\n"
623
624 ;; This also defines a function to be the handler for the output
625 ;; from the command above. That function will copy the output into
626 ;; the appropriately typed buffer. That function will be called:
627 gdb-info-breakpoints-handler)
628
629 (def-gdb-auto-updated-buffer gdb-frames-buffer
630 gdb-invalidate-frames
631 "server where\n"
632 gdb-info-frames-handler)
633
634
635 (def-gdb-auto-updated-buffer gdb-registers-buffer
636 gdb-invalidate-registers
637 "server info registers\n"
638 gdb-info-registers-handler)
639
640 ;;
641 ;; At any given time, output from gdb is being directed
642 ;; one of three places. By default, it goes into the gdb
643 ;; interaction buffer. For commands executed on behalf
644 ;; of emacs, it goes into a scratch buffer (not `the').
645 ;; Finally, some gdb output is simply thrown away; for example,
646 ;; the prompt that follows output from a command executed
647 ;; for emacs.
648 ;;
649
650 (defvar gdb-output-sink 'user
651 "An buffer-local indication of how output from an inferior gdb
652 should be directed. Legit values are:
653
654 USER -- the output should be appended to the gud
655 buffer.
656
657 PRE-EMACS -- throw away output preceding output for emacs.
658 EMACS -- redirect output to the partial-output buffer.
659 POST-EMACS -- throw away output following output for emacs.")
660
661 (defun gdb-output-burst (instance string)
662 "Handle a burst of output from a gdb instance.
663 This function is (indirectly) used as a gud-marker-filter.
664 It must return output (if any) to be insterted in the gud
665 buffer."
666
667 (save-match-data
668 (let (
669 ;; Recall the left over burst from last time
670 (burst (concat (gdb-instance-burst instance) string))
671 ;; Start accumulating output for the gud buffer
672 (output ""))
673
674 ;; Process all the complete markers in this chunk.
675
676 (while (string-match "\n\032\032\\(.*\\)\n" burst)
677 (let ((annotation (substring burst
678 (match-beginning 1)
679 (match-end 1))))
680
681 ;; Stuff prior to the match is just ordinary output.
682 ;; It is either concatenated to OUTPUT or directed
683 ;; elsewhere.
684 (setq output
685 (gdb-concat-output
686 instance
687 output
688 (substring burst 0 (match-beginning 0))))
689
690 ;; Take that stuff off the burst.
691 (setq burst (substring burst (match-end 0)))
692
693 ;; Parse the tag from the annotation, and maybe its arguments.
694 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
695 (let* ((annotation-type (substring annotation
696 (match-beginning 1)
697 (match-end 1)))
698 (annotation-arguments (substring annotation
699 (match-beginning 2)
700 (match-end 2)))
701 (annotation-rule (assoc annotation-type
702 gdb-annotation-rules)))
703 ;; Call the handler for this annotation.
704 (dbug 'annotation
705 '(lambda () (insert annotation-type "\n")))
706 (if annotation-rule
707 (funcall (car (cdr annotation-rule))
708 instance
709 annotation-arguments)
710 ;; Else the annotation is not recognized. Ignore it silently,
711 ;; so that GDB can add new annotations without causing
712 ;; us to blow up.
713 ))))
714
715
716 ;; Does the remaining text end in a partial line?
717 ;; If it does, then keep part of the burst until we get more.
718 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
719 burst)
720 (progn
721 ;; Everything before the potential marker start can be output.
722 (setq output
723 (gdb-concat-output
724 instance
725 output
726 (substring burst 0 (match-beginning 0))))
727
728 ;; Everything after, we save, to combine with later input.
729 (setq burst (substring burst (match-beginning 0))))
730
731 ;; In case we know the burst contains no partial annotations:
732 (progn
733 (setq output (gdb-concat-output instance output burst))
734 (setq burst "")))
735
736 ;; Save the remaining burst for the next call to this function.
737 (set-gdb-instance-burst instance burst)
738 output)))
739
740 (defun gdb-concat-output (instance so-far new)
741 (let ((sink (gdb-instance-output-sink instance)))
742 (cond
743 ((eq sink 'user) (concat so-far new))
744 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
745 ((eq sink 'emacs)
746 (gdb-append-to-partial-output instance new)
747 so-far)
748 (t (error "Bogon output sink %d" sink)))))
749
750 (defun gdb-append-to-partial-output (instance string)
751 (save-excursion
752 (set-buffer
753 (gdb-get-create-instance-buffer
754 instance 'gdb-partial-output-buffer))
755 (goto-char (point-max))
756 (insert string)))
757
758 (defun gdb-clear-partial-output (instance)
759 (save-excursion
760 (set-buffer
761 (gdb-get-create-instance-buffer
762 instance 'gdb-partial-output-buffer))
763 (kill-region (point-min) (point-max))))
764
765 ;;
766 ;; Instance Buffers.
767 ;;
768 ;; These are buffers that display output from gdb (or other
769 ;; information) that we want to filter out from the general gdb
770 ;; interaction buffer. (e.g. the backtrace buffer).
771 ;;
772 ;; The general pattern is that each kind of buffer is associated
773 ;; with a rule to refresh its contents. The rule includes one
774 ;; function to call when it is noticed that the buffer is out of
775 ;; date. Typically, that will queue up an idle command for gdb.
776 ;;
777 ;; Every type of instance buffer is identified by some atom
778 ;; such as gdb-frames-buffer. An instance and one of these
779 ;; atoms uniquely identifies a particular instance buffer.
780 ;;
781
782 (defun gdb-get-instance-buffer (instance key)
783 "Return the instance buffer for `instance' tagged with type `key'.
784 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
785 (save-excursion
786 (gdb-look-for-tagged-buffer instance key (buffer-list))))
787
788 (defun gdb-get-create-instance-buffer (instance key)
789 "Create a new gdb instance buffer of the type specified by `key'.
790 The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
791 (or (gdb-get-instance-buffer instance key)
792 (let* ((rules (assoc key gdb-instance-buffer-rules-assoc))
793 (name (funcall (gdb-rules-name-maker rules) instance))
794 (new (get-buffer-create name)))
795 (save-excursion
796 (set-buffer new)
797 (make-variable-buffer-local 'gdb-buffer-type)
798 (setq gdb-buffer-type key)
799 (make-variable-buffer-local 'gdb-buffer-instance)
800 (setq gdb-buffer-instance instance)
801 new))))
802
803 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
804
805 (defun gdb-look-for-tagged-buffer (instance key bufs)
806 (let ((retval nil))
807 (while (and (not retval) bufs)
808 (set-buffer (car bufs))
809 (if (and (eq gdb-buffer-instance instance)
810 (eq gdb-buffer-type key))
811 (setq retval (car bufs)))
812 (setq bufs (cdr bufs))
813 )
814 retval))
815
816 (defun gdb-instance-buffer-p (buf)
817 (save-excursion
818 (set-buffer buf)
819 (and gdb-buffer-type
820 (not (eq gdb-buffer-type 'gud)))))
821
822 ;;
823 ;; partial-output buffers
824 ;;
825 ;; These accumulate output from a command executed on
826 ;; behalf of emacs (rather than the user). When the
827 ;; output is complete, the hooks bound to `gdb-command-complete-hooks'
828 ;; are called (and then cleared). Usually these hooks are not
829 ;; set directly but rather implicitly according to the
830 ;; instance-buffer rules.
831 ;;
832
833 (defun gdb-partial-output-name (instance)
834 (concat "*partial-output-"
835 (gdb-instance-target-string instance)
836 "*"))
837
838 ;;
839 ;; Backtrace buffers
840 ;;
841
842 (defun gdb-frames-buffer-name (instance)
843 (save-excursion
844 (set-buffer (process-buffer (gdb-instance-process instance)))
845 (concat "*stack frames of "
846 (gdb-instance-target-string instance) "*")))
847
848 (defun gud-display-frames-buffer (instance)
849 (interactive (list (gdb-needed-default-instance)))
850 (gud-display-buffer
851 (gdb-get-create-instance-buffer instance
852 'gdb-frames-buffer)))
853
854 ;;
855 ;; Breakpoint buffers
856 ;;
857
858 (defun gdb-breakpoints-buffer-name (instance)
859 (save-excursion
860 (set-buffer (process-buffer (gdb-instance-process instance)))
861 (concat "*breakpoints of " (gdb-instance-target-string instance) "*")))
862
863 (defun gud-display-breakpoints-buffer (instance)
864 (interactive (list (gdb-needed-default-instance)))
865 (gud-display-buffer
866 (gdb-get-create-instance-buffer instance
867 'gdb-breakpoints-buffer)))
868
869
870 ;;
871 ;; Registers buffers
872 ;;
873
874 (defun gdb-registers-buffer-name (instance)
875 (save-excursion
876 (set-buffer (process-buffer (gdb-instance-process instance)))
877 (concat "*registers of " (gdb-instance-target-string instance) "*")))
878
879 (defun gud-display-registers-buffer (instance)
880 (interactive (list (gdb-needed-default-instance)))
881 (gud-display-buffer
882 (gdb-get-create-instance-buffer instance
883 'gdb-registers-buffer)))
884
885 \f
886 ;;; FIXME: This should only return true for buffers in the current instance
887 (defun gud-protected-buffer-p (buffer)
888 "Is BUFFER a buffer which we want to leave displayed?"
889 (save-excursion
890 (set-buffer buffer)
891 (or (eq gdb-buffer-type 'gdb-registers-buffer)
892 (eq gdb-buffer-type 'gdb-breakpoints-buffer)
893 (eq gdb-buffer-type 'gdb-frames-buffer)
894 (eq gdb-buffer-type 'gud))))
895 ;;;
896 ;;; The way we abuse the dedicated-p flag is pretty gross, but seems
897 ;;; to do the right thing. Seeing as there is no way for Lisp code to
898 ;;; get at the use_time field of a window, I'm not sure there exists a
899 ;;; more elegant solution without writing C code.
900
901 (defun gud-display-buffer (buf)
902 (let ((must-split nil))
903 (unwind-protect
904 (progn
905 (walk-windows
906 '(lambda (win)
907 (if (gud-protected-buffer-p (window-buffer win))
908 (set-window-dedicated-p win t)
909 )))
910 ;; This is more or less just the same as display-buffer; the
911 ;; big difference is that we split the largest window rather
912 ;; than the lru window. Various settings and hair which
913 ;; display-buffer has are omitted, for simplicity.
914 (if (not (get-buffer-window buf nil))
915 (let ((window (get-lru-window nil)))
916 (if window
917 (set-window-buffer window buf)
918 (setq must-split t)
919 )))
920 )
921 (walk-windows
922 '(lambda (win)
923 (if (gud-protected-buffer-p (window-buffer win))
924 (set-window-dedicated-p win nil)
925 )))
926 )
927 (if must-split
928 (set-window-buffer (split-window (get-largest-window)) buf))
929 ))
930 \f
931 (defun gud-gdb-find-file (f)
932 (find-file-noselect f))
933
934 ;;;###autoload
935 (defun gdb (command-line)
936 "Run gdb on program FILE in buffer *gud-FILE*.
937 The directory containing FILE becomes the initial working directory
938 and source-file directory for your debugger."
939 (interactive
940 (list (read-from-minibuffer "Run gdb (like this): "
941 (if (consp gud-gdb-history)
942 (car gud-gdb-history)
943 "gdb ")
944 nil nil
945 '(gud-gdb-history . 1))))
946 (gud-overload-functions
947 '((gud-massage-args . gud-gdb-massage-args)
948 (gud-marker-filter . gud-gdb-marker-filter)
949 (gud-find-file . gud-gdb-find-file)
950 ))
951
952 (gud-common-init command-line)
953
954 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
955 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
956 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
957 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
958 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
959 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
960 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
961 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
962 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
963 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
964 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
965
966 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
967 (setq comint-input-sender 'gdb-send)
968 (run-hooks 'gdb-mode-hook)
969 (make-gdb-instance (get-buffer-process (current-buffer)))
970 )
971
972 \f
973 ;; ======================================================================
974 ;; sdb functions
975
976 ;;; History of argument lists passed to sdb.
977 (defvar gud-sdb-history nil)
978
979 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
980 "If nil, we're on a System V Release 4 and don't need the tags hack.")
981
982 (defvar gud-sdb-lastfile nil)
983
984 (defun gud-sdb-massage-args (file args)
985 (cons file args))
986
987 (defun gud-sdb-marker-filter (string)
988 (cond
989 ;; System V Release 3.2 uses this format
990 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
991 string)
992 (setq gud-last-frame
993 (cons
994 (substring string (match-beginning 2) (match-end 2))
995 (string-to-int
996 (substring string (match-beginning 3) (match-end 3))))))
997 ;; System V Release 4.0
998 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
999 string)
1000 (setq gud-sdb-lastfile
1001 (substring string (match-beginning 2) (match-end 2))))
1002 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
1003 (setq gud-last-frame
1004 (cons
1005 gud-sdb-lastfile
1006 (string-to-int
1007 (substring string (match-beginning 1) (match-end 1))))))
1008 (t
1009 (setq gud-sdb-lastfile nil)))
1010 string)
1011
1012 (defun gud-sdb-find-file (f)
1013 (if gud-sdb-needs-tags
1014 (find-tag-noselect f)
1015 (find-file-noselect f)))
1016
1017 ;;;###autoload
1018 (defun sdb (command-line)
1019 "Run sdb on program FILE in buffer *gud-FILE*.
1020 The directory containing FILE becomes the initial working directory
1021 and source-file directory for your debugger."
1022 (interactive
1023 (list (read-from-minibuffer "Run sdb (like this): "
1024 (if (consp gud-sdb-history)
1025 (car gud-sdb-history)
1026 "sdb ")
1027 nil nil
1028 '(gud-sdb-history . 1))))
1029 (if (and gud-sdb-needs-tags
1030 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
1031 (error "The sdb support requires a valid tags table to work."))
1032 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
1033 (gud-marker-filter . gud-sdb-marker-filter)
1034 (gud-find-file . gud-sdb-find-file)
1035 ))
1036
1037 (gud-common-init command-line)
1038
1039 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
1040 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
1041 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
1042 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
1043 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
1044 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1045 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1046 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
1047
1048 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
1049 (run-hooks 'sdb-mode-hook)
1050 )
1051 \f
1052 ;; ======================================================================
1053 ;; dbx functions
1054
1055 ;;; History of argument lists passed to dbx.
1056 (defvar gud-dbx-history nil)
1057
1058 (defun gud-dbx-massage-args (file args)
1059 (cons file args))
1060
1061 (defun gud-dbx-marker-filter (string)
1062 (if (or (string-match
1063 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1064 string)
1065 (string-match
1066 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1067 string))
1068 (setq gud-last-frame
1069 (cons
1070 (substring string (match-beginning 2) (match-end 2))
1071 (string-to-int
1072 (substring string (match-beginning 1) (match-end 1))))))
1073 string)
1074
1075 (defun gud-dbx-find-file (f)
1076 (find-file-noselect f))
1077
1078 ;;;###autoload
1079 (defun dbx (command-line)
1080 "Run dbx on program FILE in buffer *gud-FILE*.
1081 The directory containing FILE becomes the initial working directory
1082 and source-file directory for your debugger."
1083 (interactive
1084 (list (read-from-minibuffer "Run dbx (like this): "
1085 (if (consp gud-dbx-history)
1086 (car gud-dbx-history)
1087 "dbx ")
1088 nil nil
1089 '(gud-dbx-history . 1))))
1090 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args)
1091 (gud-marker-filter . gud-dbx-marker-filter)
1092 (gud-find-file . gud-dbx-find-file)
1093 ))
1094
1095 (gud-common-init command-line)
1096
1097 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1098 "\C-b" "Set breakpoint at current line.")
1099 ;; (gud-def gud-break "stop at \"%f\":%l"
1100 ;; "\C-b" "Set breakpoint at current line.")
1101 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1102 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1103 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1104 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1105 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1106 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1107 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1108 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1109
1110 (setq comint-prompt-regexp "^[^)]*dbx) *")
1111 (run-hooks 'dbx-mode-hook)
1112 )
1113 \f
1114 ;; ======================================================================
1115 ;; xdb (HP PARISC debugger) functions
1116
1117 ;;; History of argument lists passed to xdb.
1118 (defvar gud-xdb-history nil)
1119
1120 (defvar gud-xdb-directories nil
1121 "*A list of directories that xdb should search for source code.
1122 If nil, only source files in the program directory
1123 will be known to xdb.
1124
1125 The file names should be absolute, or relative to the directory
1126 containing the executable being debugged.")
1127
1128 (defun gud-xdb-massage-args (file args)
1129 (nconc (let ((directories gud-xdb-directories)
1130 (result nil))
1131 (while directories
1132 (setq result (cons (car directories) (cons "-d" result)))
1133 (setq directories (cdr directories)))
1134 (nreverse (cons file result)))
1135 args))
1136
1137 (defun gud-xdb-file-name (f)
1138 "Transform a relative pathname to a full pathname in xdb mode"
1139 (let ((result nil))
1140 (if (file-exists-p f)
1141 (setq result (expand-file-name f))
1142 (let ((directories gud-xdb-directories))
1143 (while directories
1144 (let ((path (concat (car directories) "/" f)))
1145 (if (file-exists-p path)
1146 (setq result (expand-file-name path)
1147 directories nil)))
1148 (setq directories (cdr directories)))))
1149 result))
1150
1151 ;; xdb does not print the lines all at once, so we have to accumulate them
1152 (defvar gud-xdb-accumulation "")
1153
1154 (defun gud-xdb-marker-filter (string)
1155 (let (result)
1156 (if (or (string-match comint-prompt-regexp string)
1157 (string-match ".*\012" string))
1158 (setq result (concat gud-xdb-accumulation string)
1159 gud-xdb-accumulation "")
1160 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
1161 (if result
1162 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
1163 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1164 result))
1165 (let ((line (string-to-int
1166 (substring result (match-beginning 2) (match-end 2))))
1167 (file (gud-xdb-file-name
1168 (substring result (match-beginning 1) (match-end 1)))))
1169 (if file
1170 (setq gud-last-frame (cons file line))))))
1171 (or result "")))
1172
1173 (defun gud-xdb-find-file (f)
1174 (let ((realf (gud-xdb-file-name f)))
1175 (if realf (find-file-noselect realf))))
1176
1177 ;;;###autoload
1178 (defun xdb (command-line)
1179 "Run xdb on program FILE in buffer *gud-FILE*.
1180 The directory containing FILE becomes the initial working directory
1181 and source-file directory for your debugger.
1182
1183 You can set the variable 'gud-xdb-directories' to a list of program source
1184 directories if your program contains sources from more than one directory."
1185 (interactive
1186 (list (read-from-minibuffer "Run xdb (like this): "
1187 (if (consp gud-xdb-history)
1188 (car gud-xdb-history)
1189 "xdb ")
1190 nil nil
1191 '(gud-xdb-history . 1))))
1192 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
1193 (gud-marker-filter . gud-xdb-marker-filter)
1194 (gud-find-file . gud-xdb-find-file)))
1195
1196 (gud-common-init command-line)
1197
1198 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1199 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1200 "Set temporary breakpoint at current line.")
1201 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1202 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1203 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1204 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1205 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1206 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1207 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1208 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1209
1210 (setq comint-prompt-regexp "^>")
1211 (make-local-variable 'gud-xdb-accumulation)
1212 (setq gud-xdb-accumulation "")
1213 (run-hooks 'xdb-mode-hook))
1214 \f
1215 ;; ======================================================================
1216 ;; perldb functions
1217
1218 ;;; History of argument lists passed to perldb.
1219 (defvar gud-perldb-history nil)
1220
1221 (defun gud-perldb-massage-args (file args)
1222 (cons "-d" (cons file (cons "-emacs" args))))
1223
1224 ;; There's no guarantee that Emacs will hand the filter the entire
1225 ;; marker at once; it could be broken up across several strings. We
1226 ;; might even receive a big chunk with several markers in it. If we
1227 ;; receive a chunk of text which looks like it might contain the
1228 ;; beginning of a marker, we save it here between calls to the
1229 ;; filter.
1230 (defvar gud-perldb-marker-acc "")
1231
1232 (defun gud-perldb-marker-filter (string)
1233 (save-match-data
1234 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
1235 (let ((output ""))
1236
1237 ;; Process all the complete markers in this chunk.
1238 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1239 gud-perldb-marker-acc)
1240 (setq
1241
1242 ;; Extract the frame position from the marker.
1243 gud-last-frame
1244 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
1245 (string-to-int (substring gud-perldb-marker-acc
1246 (match-beginning 2)
1247 (match-end 2))))
1248
1249 ;; Append any text before the marker to the output we're going
1250 ;; to return - we don't include the marker in this text.
1251 output (concat output
1252 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
1253
1254 ;; Set the accumulator to the remaining text.
1255 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
1256
1257 ;; Does the remaining text look like it might end with the
1258 ;; beginning of another marker? If it does, then keep it in
1259 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
1260 ;; know the full marker regexp above failed, it's pretty simple to
1261 ;; test for marker starts.
1262 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
1263 (progn
1264 ;; Everything before the potential marker start can be output.
1265 (setq output (concat output (substring gud-perldb-marker-acc
1266 0 (match-beginning 0))))
1267
1268 ;; Everything after, we save, to combine with later input.
1269 (setq gud-perldb-marker-acc
1270 (substring gud-perldb-marker-acc (match-beginning 0))))
1271
1272 (setq output (concat output gud-perldb-marker-acc)
1273 gud-perldb-marker-acc ""))
1274
1275 output)))
1276
1277 (defun gud-perldb-find-file (f)
1278 (find-file-noselect f))
1279
1280 ;;;###autoload
1281 (defun perldb (command-line)
1282 "Run perldb on program FILE in buffer *gud-FILE*.
1283 The directory containing FILE becomes the initial working directory
1284 and source-file directory for your debugger."
1285 (interactive
1286 (list (read-from-minibuffer "Run perldb (like this): "
1287 (if (consp gud-perldb-history)
1288 (car gud-perldb-history)
1289 "perl ")
1290 nil nil
1291 '(gud-perldb-history . 1))))
1292 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
1293 (gud-marker-filter . gud-perldb-marker-filter)
1294 (gud-find-file . gud-perldb-find-file)
1295 ))
1296
1297 (gud-common-init command-line)
1298
1299 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1300 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1301 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1302 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1303 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1304 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1305 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1306 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1307 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1308
1309 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1310 (run-hooks 'perldb-mode-hook)
1311 )
1312
1313 ;;
1314 ;; End of debugger-specific information
1315 ;;
1316
1317 \f
1318 ;;; When we send a command to the debugger via gud-call, it's annoying
1319 ;;; to see the command and the new prompt inserted into the debugger's
1320 ;;; buffer; we have other ways of knowing the command has completed.
1321 ;;;
1322 ;;; If the buffer looks like this:
1323 ;;; --------------------
1324 ;;; (gdb) set args foo bar
1325 ;;; (gdb) -!-
1326 ;;; --------------------
1327 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1328 ;;; source file to set a breakpoint, we want the buffer to end up like
1329 ;;; this:
1330 ;;; --------------------
1331 ;;; (gdb) set args foo bar
1332 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1333 ;;; (gdb) -!-
1334 ;;; --------------------
1335 ;;; Essentially, the old prompt is deleted, and the command's output
1336 ;;; and the new prompt take its place.
1337 ;;;
1338 ;;; Not echoing the command is easy enough; you send it directly using
1339 ;;; process-send-string, and it never enters the buffer. However,
1340 ;;; getting rid of the old prompt is trickier; you don't want to do it
1341 ;;; when you send the command, since that will result in an annoying
1342 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1343 ;;; waits for a response from the debugger, and the new prompt is
1344 ;;; inserted. Instead, we'll wait until we actually get some output
1345 ;;; from the subprocess before we delete the prompt. If the command
1346 ;;; produced no output other than a new prompt, that prompt will most
1347 ;;; likely be in the first chunk of output received, so we will delete
1348 ;;; the prompt and then replace it with an identical one. If the
1349 ;;; command produces output, the prompt is moving anyway, so the
1350 ;;; flicker won't be annoying.
1351 ;;;
1352 ;;; So - when we want to delete the prompt upon receipt of the next
1353 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1354 ;;; the start of the prompt; the process filter will notice this, and
1355 ;;; delete all text between it and the process output marker. If
1356 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1357 ;;; prompt alone.
1358 (defvar gud-delete-prompt-marker nil)
1359
1360 \f
1361 (defvar gdbish-comint-mode-map (copy-keymap comint-mode-map))
1362 (define-key gdbish-comint-mode-map "\C-c\M-\C-r" 'gud-display-registers-buffer)
1363 (define-key gdbish-comint-mode-map "\C-c\M-\C-f" 'gud-display-frames-buffer)
1364 (define-key gdbish-comint-mode-map "\C-c\M-\C-b" 'gud-display-breakpoints-buffer)
1365
1366 (defun gud-mode ()
1367 "Major mode for interacting with an inferior debugger process.
1368
1369 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1370 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
1371 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
1372
1373 After startup, the following commands are available in both the GUD
1374 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1375 or step operation:
1376
1377 \\[gud-break] sets a breakpoint at the current file and line. In the
1378 GUD buffer, the current file and line are those of the last breakpoint or
1379 step. In a source buffer, they are the buffer's file and current line.
1380
1381 \\[gud-remove] removes breakpoints on the current file and line.
1382
1383 \\[gud-refresh] displays in the source window the last line referred to
1384 in the gud buffer.
1385
1386 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1387 step-one-line (not entering function calls), and step-one-instruction
1388 and then update the source window with the current file and position.
1389 \\[gud-cont] continues execution.
1390
1391 \\[gud-print] tries to find the largest C lvalue or function-call expression
1392 around point, and sends it to the debugger for value display.
1393
1394 The above commands are common to all supported debuggers except xdb which
1395 does not support stepping instructions.
1396
1397 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1398 except that the breakpoint is temporary; that is, it is removed when
1399 execution stops on it.
1400
1401 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1402 frame. \\[gud-down] drops back down through one.
1403
1404 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1405 the current function and stops.
1406
1407 All the keystrokes above are accessible in the GUD buffer
1408 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1409
1410 All pre-defined functions for which the concept make sense repeat
1411 themselves the appropriate number of times if you give a prefix
1412 argument.
1413
1414 You may use the `gud-def' macro in the initialization hook to define other
1415 commands.
1416
1417 Other commands for interacting with the debugger process are inherited from
1418 comint mode, which see."
1419 (interactive)
1420 (comint-mode)
1421 (setq major-mode 'gud-mode)
1422 (setq mode-name "Debugger")
1423 (setq mode-line-process '(": %s"))
1424 (use-local-map (copy-keymap gdbish-comint-mode-map))
1425 (make-local-variable 'gud-last-frame)
1426 (setq gud-last-frame nil)
1427 (make-local-variable 'comint-prompt-regexp)
1428 (make-local-variable 'gud-delete-prompt-marker)
1429 (setq gud-delete-prompt-marker (make-marker))
1430 (run-hooks 'gud-mode-hook)
1431 )
1432
1433 (defvar gud-comint-buffer nil)
1434
1435 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1436 (defun gud-chop-words (string)
1437 (let ((i 0) (beg 0)
1438 (len (length string))
1439 (words nil))
1440 (while (< i len)
1441 (if (memq (aref string i) '(?\t ? ))
1442 (progn
1443 (setq words (cons (substring string beg i) words)
1444 beg (1+ i))
1445 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1446 (setq beg (1+ beg)))
1447 (setq i (1+ beg)))
1448 (setq i (1+ i))))
1449 (if (< beg len)
1450 (setq words (cons (substring string beg) words)))
1451 (nreverse words)))
1452
1453 (defvar gud-target-name "--unknown--"
1454 "The apparent name of the program being debugged in a gud buffer.
1455 For sure this the root string used in smashing together the gud
1456 buffer's name, even if that doesn't happen to be the name of a
1457 program.")
1458
1459 ;; Perform initializations common to all debuggers.
1460 (defun gud-common-init (command-line)
1461 (let* ((words (gud-chop-words command-line))
1462 (program (car words))
1463 (file-word (let ((w (cdr words)))
1464 (while (and w (= ?- (aref (car w) 0)))
1465 (setq w (cdr w)))
1466 (car w)))
1467 (args (delq file-word (cdr words)))
1468 (file (expand-file-name file-word))
1469 (filepart (file-name-nondirectory file))
1470 (buffer-name (concat "*gud-" filepart "*")))
1471 (switch-to-buffer buffer-name)
1472 (setq default-directory (file-name-directory file))
1473 (or (bolp) (newline))
1474 (insert "Current directory is " default-directory "\n")
1475 (apply 'make-comint (concat "gud-" filepart) program nil
1476 (gud-massage-args file args))
1477 (gud-mode)
1478 (make-variable-buffer-local 'gud-target-name)
1479 (setq gud-target-name filepart))
1480 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1481 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
1482 (gud-set-buffer)
1483 )
1484
1485 (defun gud-set-buffer ()
1486 (cond ((eq major-mode 'gud-mode)
1487 (setq gud-comint-buffer (current-buffer)))))
1488
1489 ;; These functions are responsible for inserting output from your debugger
1490 ;; into the buffer. The hard work is done by the method that is
1491 ;; the value of gud-marker-filter.
1492
1493 (defun gud-filter (proc string)
1494 ;; Here's where the actual buffer insertion is done
1495 (let ((inhibit-quit t))
1496 (save-excursion
1497 (set-buffer (process-buffer proc))
1498 (let (moving output-after-point)
1499 (save-excursion
1500 (goto-char (process-mark proc))
1501 ;; If we have been so requested, delete the debugger prompt.
1502 (if (marker-buffer gud-delete-prompt-marker)
1503 (progn
1504 (delete-region (point) gud-delete-prompt-marker)
1505 (set-marker gud-delete-prompt-marker nil)))
1506 (insert-before-markers (gud-marker-filter string))
1507 (setq moving (= (point) (process-mark proc)))
1508 (setq output-after-point (< (point) (process-mark proc)))
1509 ;; Check for a filename-and-line number.
1510 ;; Don't display the specified file
1511 ;; unless (1) point is at or after the position where output appears
1512 ;; and (2) this buffer is on the screen.
1513 (if (and gud-last-frame
1514 (not output-after-point)
1515 (get-buffer-window (current-buffer)))
1516 (gud-display-frame)))
1517 (if moving (goto-char (process-mark proc)))))))
1518
1519 (defun gud-sentinel (proc msg)
1520 (cond ((null (buffer-name (process-buffer proc)))
1521 ;; buffer killed
1522 ;; Stop displaying an arrow in a source file.
1523 (setq overlay-arrow-position nil)
1524 (set-process-buffer proc nil))
1525 ((memq (process-status proc) '(signal exit))
1526 ;; Stop displaying an arrow in a source file.
1527 (setq overlay-arrow-position nil)
1528 ;; Fix the mode line.
1529 (setq mode-line-process
1530 (concat ": "
1531 (symbol-name (process-status proc))))
1532 (let* ((obuf (current-buffer)))
1533 ;; save-excursion isn't the right thing if
1534 ;; process-buffer is current-buffer
1535 (unwind-protect
1536 (progn
1537 ;; Write something in *compilation* and hack its mode line,
1538 (set-buffer (process-buffer proc))
1539 ;; Force mode line redisplay soon
1540 (set-buffer-modified-p (buffer-modified-p))
1541 (if (eobp)
1542 (insert ?\n mode-name " " msg)
1543 (save-excursion
1544 (goto-char (point-max))
1545 (insert ?\n mode-name " " msg)))
1546 ;; If buffer and mode line will show that the process
1547 ;; is dead, we can delete it now. Otherwise it
1548 ;; will stay around until M-x list-processes.
1549 (delete-process proc))
1550 ;; Restore old buffer, but don't restore old point
1551 ;; if obuf is the gud buffer.
1552 (set-buffer obuf))))))
1553
1554 (defun gud-display-frame ()
1555 "Find and obey the last filename-and-line marker from the debugger.
1556 Obeying it means displaying in another window the specified file and line."
1557 (interactive)
1558 (if gud-last-frame
1559 (progn
1560 (gud-set-buffer)
1561 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
1562 (setq gud-last-last-frame gud-last-frame
1563 gud-last-frame nil))))
1564
1565 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1566 ;; and that its line LINE is visible.
1567 ;; Put the overlay-arrow on the line LINE in that buffer.
1568 ;; Most of the trickiness in here comes from wanting to preserve the current
1569 ;; region-restriction if that's possible. We use an explicit display-buffer
1570 ;; to get around the fact that this is called inside a save-excursion.
1571
1572 (defun gud-display-line (true-file line)
1573 (let* ((buffer (gud-find-file true-file))
1574 (window (gud-display-buffer buffer))
1575 (pos))
1576 ;;; (if (equal buffer (current-buffer))
1577 ;;; nil
1578 ;;; (setq buffer-read-only nil))
1579 (save-excursion
1580 ;;; (setq buffer-read-only t)
1581 (set-buffer buffer)
1582 (save-restriction
1583 (widen)
1584 (goto-line line)
1585 (setq pos (point))
1586 (setq overlay-arrow-string "=>")
1587 (or overlay-arrow-position
1588 (setq overlay-arrow-position (make-marker)))
1589 (set-marker overlay-arrow-position (point) (current-buffer)))
1590 (cond ((or (< pos (point-min)) (> pos (point-max)))
1591 (widen)
1592 (goto-char pos))))
1593 (set-window-point window overlay-arrow-position)))
1594
1595 ;;; The gud-call function must do the right thing whether its invoking
1596 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1597 ;;; or a C buffer. In the former case, we want to supply data from
1598 ;;; gud-last-frame. Here's how we do it:
1599
1600 (defun gud-format-command (str arg)
1601 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
1602 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
1603 (setq str (concat
1604 (substring str (match-beginning 1) (match-end 1))
1605 (file-name-nondirectory (if insource
1606 (buffer-file-name)
1607 (car gud-last-frame)))
1608 (substring str (match-beginning 2) (match-end 2)))))
1609 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
1610 (setq str (concat
1611 (substring str (match-beginning 1) (match-end 1))
1612 (file-name-directory (if insource
1613 (buffer-file-name)
1614 (car gud-last-frame)))
1615 (substring str (match-beginning 2) (match-end 2)))))
1616 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
1617 (setq str (concat
1618 (substring str (match-beginning 1) (match-end 1))
1619 (if insource
1620 (save-excursion
1621 (beginning-of-line)
1622 (save-restriction (widen)
1623 (1+ (count-lines 1 (point)))))
1624 (cdr gud-last-frame))
1625 (substring str (match-beginning 2) (match-end 2)))))
1626 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
1627 (setq str (concat
1628 (substring str (match-beginning 1) (match-end 1))
1629 (find-c-expr)
1630 (substring str (match-beginning 2) (match-end 2)))))
1631 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
1632 (setq str (concat
1633 (substring str (match-beginning 1) (match-end 1))
1634 (gud-read-address)
1635 (substring str (match-beginning 2) (match-end 2)))))
1636 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
1637 (setq str (concat
1638 (substring str (match-beginning 1) (match-end 1))
1639 (if arg (int-to-string arg) "")
1640 (substring str (match-beginning 2) (match-end 2)))))
1641 )
1642 str
1643 )
1644
1645 (defun gud-read-address ()
1646 "Return a string containing the core-address found in the buffer at point."
1647 (save-excursion
1648 (let ((pt (point)) found begin)
1649 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1650 (cond
1651 (found (forward-char 2)
1652 (buffer-substring found
1653 (progn (re-search-forward "[^0-9a-f]")
1654 (forward-char -1)
1655 (point))))
1656 (t (setq begin (progn (re-search-backward "[^0-9]")
1657 (forward-char 1)
1658 (point)))
1659 (forward-char 1)
1660 (re-search-forward "[^0-9]")
1661 (forward-char -1)
1662 (buffer-substring begin (point)))))))
1663
1664 (defun gud-call (fmt &optional arg)
1665 (let ((msg (gud-format-command fmt arg)))
1666 (message "Command: %s" msg)
1667 (sit-for 0)
1668 (gud-basic-call msg)))
1669
1670 (defun gud-basic-call (command)
1671 "Invoke the debugger COMMAND displaying source in other window."
1672 (interactive)
1673 (gud-set-buffer)
1674 (let ((command (concat command "\n"))
1675 (proc (get-buffer-process gud-comint-buffer)))
1676
1677 ;; Arrange for the current prompt to get deleted.
1678 (save-excursion
1679 (set-buffer gud-comint-buffer)
1680 (goto-char (process-mark proc))
1681 (beginning-of-line)
1682 (if (looking-at comint-prompt-regexp)
1683 (set-marker gud-delete-prompt-marker (point))))
1684 (process-send-string proc command)))
1685
1686 (defun gud-refresh (&optional arg)
1687 "Fix up a possibly garbled display, and redraw the arrow."
1688 (interactive "P")
1689 (recenter arg)
1690 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1691 (gud-display-frame))
1692 \f
1693 ;;; Code for parsing expressions out of C code. The single entry point is
1694 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1695 ;;;
1696 ;;; The rest of this file is a hacked version of gdbsrc.el by
1697 ;;; Debby Ayers <ayers@asc.slb.com>,
1698 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1699
1700 (defun find-c-expr ()
1701 "Returns the C expr that surrounds point."
1702 (interactive)
1703 (save-excursion
1704 (let ((p) (expr) (test-expr))
1705 (setq p (point))
1706 (setq expr (expr-cur))
1707 (setq test-expr (expr-prev))
1708 (while (expr-compound test-expr expr)
1709 (setq expr (cons (car test-expr) (cdr expr)))
1710 (goto-char (car expr))
1711 (setq test-expr (expr-prev)))
1712 (goto-char p)
1713 (setq test-expr (expr-next))
1714 (while (expr-compound expr test-expr)
1715 (setq expr (cons (car expr) (cdr test-expr)))
1716 (setq test-expr (expr-next))
1717 )
1718 (buffer-substring (car expr) (cdr expr)))))
1719
1720 (defun expr-cur ()
1721 "Returns the expr that point is in; point is set to beginning of expr.
1722 The expr is represented as a cons cell, where the car specifies the point in
1723 the current buffer that marks the beginning of the expr and the cdr specifies
1724 the character after the end of the expr."
1725 (let ((p (point)) (begin) (end))
1726 (expr-backward-sexp)
1727 (setq begin (point))
1728 (expr-forward-sexp)
1729 (setq end (point))
1730 (if (>= p end)
1731 (progn
1732 (setq begin p)
1733 (goto-char p)
1734 (expr-forward-sexp)
1735 (setq end (point))
1736 )
1737 )
1738 (goto-char begin)
1739 (cons begin end)))
1740
1741 (defun expr-backward-sexp ()
1742 "Version of `backward-sexp' that catches errors."
1743 (condition-case nil
1744 (backward-sexp)
1745 (error t)))
1746
1747 (defun expr-forward-sexp ()
1748 "Version of `forward-sexp' that catches errors."
1749 (condition-case nil
1750 (forward-sexp)
1751 (error t)))
1752
1753 (defun expr-prev ()
1754 "Returns the previous expr, point is set to beginning of that expr.
1755 The expr is represented as a cons cell, where the car specifies the point in
1756 the current buffer that marks the beginning of the expr and the cdr specifies
1757 the character after the end of the expr"
1758 (let ((begin) (end))
1759 (expr-backward-sexp)
1760 (setq begin (point))
1761 (expr-forward-sexp)
1762 (setq end (point))
1763 (goto-char begin)
1764 (cons begin end)))
1765
1766 (defun expr-next ()
1767 "Returns the following expr, point is set to beginning of that expr.
1768 The expr is represented as a cons cell, where the car specifies the point in
1769 the current buffer that marks the beginning of the expr and the cdr specifies
1770 the character after the end of the expr."
1771 (let ((begin) (end))
1772 (expr-forward-sexp)
1773 (expr-forward-sexp)
1774 (setq end (point))
1775 (expr-backward-sexp)
1776 (setq begin (point))
1777 (cons begin end)))
1778
1779 (defun expr-compound-sep (span-start span-end)
1780 "Returns '.' for '->' & '.', returns ' ' for white space,
1781 returns '?' for other punctuation."
1782 (let ((result ? )
1783 (syntax))
1784 (while (< span-start span-end)
1785 (setq syntax (char-syntax (char-after span-start)))
1786 (cond
1787 ((= syntax ? ) t)
1788 ((= syntax ?.) (setq syntax (char-after span-start))
1789 (cond
1790 ((= syntax ?.) (setq result ?.))
1791 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1792 (setq result ?.)
1793 (setq span-start (+ span-start 1)))
1794 (t (setq span-start span-end)
1795 (setq result ??)))))
1796 (setq span-start (+ span-start 1)))
1797 result))
1798
1799 (defun expr-compound (first second)
1800 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1801 The two exprs are represented as a cons cells, where the car
1802 specifies the point in the current buffer that marks the beginning of the
1803 expr and the cdr specifies the character after the end of the expr.
1804 Link exprs of the form:
1805 Expr -> Expr
1806 Expr . Expr
1807 Expr (Expr)
1808 Expr [Expr]
1809 (Expr) Expr
1810 [Expr] Expr"
1811 (let ((span-start (cdr first))
1812 (span-end (car second))
1813 (syntax))
1814 (setq syntax (expr-compound-sep span-start span-end))
1815 (cond
1816 ((= (car first) (car second)) nil)
1817 ((= (cdr first) (cdr second)) nil)
1818 ((= syntax ?.) t)
1819 ((= syntax ? )
1820 (setq span-start (char-after (- span-start 1)))
1821 (setq span-end (char-after span-end))
1822 (cond
1823 ((= span-start ?) ) t )
1824 ((= span-start ?] ) t )
1825 ((= span-end ?( ) t )
1826 ((= span-end ?[ ) t )
1827 (t nil))
1828 )
1829 (t nil))))
1830
1831 (provide 'gud)
1832
1833 ;;; gud.el ends here
1834
1835 (defun gdb-toggle-bp-this-line ()
1836 (interactive)
1837 (save-excursion
1838 (beginning-of-line 1)
1839 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
1840 (error "Not recognized as breakpoint line (demo foo).")
1841 (gdb-instance-enqueue-idle-input
1842 gdb-buffer-instance
1843 (list
1844 (concat
1845 (if (eq ?y (char-after (match-beginning 2)))
1846 "server disable "
1847 "server enable ")
1848 (buffer-substring (match-beginning 0)
1849 (match-end 1))
1850 "\n")
1851 '(lambda () nil)))
1852 )))