* linker.c (_bfd_generic_link_add_archive_symbols): Initialize
[deliverable/binutils-gdb.git] / gdb / gdba.el
CommitLineData
c9d4b5ef
JK
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
f2db7337 48;; Cygnus Support added support for gdb's --annotate=2.
c9d4b5ef
JK
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.
69This 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
99optional doc string DOC. Certain %-escapes in the string arguments
100are 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
110we're in a C file) or the source file current at the last break or
111step (if we're in the GUD buffer).
112 The `current' line is that of the current buffer (if we're in a
113source file) or the source line number at the last break or step (if
114we'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)
f2db7337 172 (cons "--annotate=2" (cons file args)))
c9d4b5ef 173
e91c1816 174\f
c9d4b5ef
JK
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."
e91c1816 186 (setq last-proc proc)
c9d4b5ef
JK
187 (let ((instance (cons 'gdb-instance proc)))
188 (save-excursion
189 (set-buffer (process-buffer proc))
e91c1816
TL
190 (setq gdb-buffer-instance instance)
191 (progn
192 (mapcar 'make-variable-buffer-local gdb-instance-variables)
193 (setq gdb-buffer-type 'gud)
194 ;; If we're taking over the buffer of another process,
195 ;; take over it's ancillery buffers as well.
196 ;;
197 (let ((dead (or old-gdb-buffer-instance)))
198 (mapcar
199 (function
200 (lambda (b)
201 (progn
202 (set-buffer b)
203 (if (eq dead gdb-buffer-instance)
204 (setq gdb-buffer-instance instance)))))
205 (buffer-list)))))
c9d4b5ef
JK
206 instance))
207
208(defun gdb-instance-process (inst) (cdr inst))
209
e91c1816
TL
210;;; The list of instance variables is built up by the expansions of
211;;; DEF-GDB-VARIABLE
212;;;
c9d4b5ef
JK
213(defvar gdb-instance-variables '()
214 "A list of variables that are local to the gud buffer associated
215with a gdb instance.")
216
217(defmacro def-gdb-variable
218 (name accessor setter &optional default doc)
219 (`
220 (progn
221 (defvar (, name) (, default) (, (or doc "undocumented")))
222 (if (not (memq '(, name) gdb-instance-variables))
223 (setq gdb-instance-variables
224 (cons '(, name) gdb-instance-variables)))
225 (, (and accessor
226 (`
227 (defun (, accessor) (instance)
228 (let
229 ((buffer (gdb-get-instance-buffer instance 'gud)))
230 (and buffer
231 (save-excursion
232 (set-buffer buffer)
233 (, name))))))))
234 (, (and setter
235 (`
236 (defun (, setter) (instance val)
237 (let
238 ((buffer (gdb-get-instance-buffer instance 'gud)))
239 (and buffer
240 (save-excursion
241 (set-buffer buffer)
242 (setq (, name) val)))))))))))
243
244(defmacro def-gdb-var (root-symbol &optional default doc)
245 (let* ((root (symbol-name root-symbol))
246 (accessor (intern (concat "gdb-instance-" root)))
247 (setter (intern (concat "set-gdb-instance-" root)))
248 (var-name (intern (concat "gdb-" root))))
249 (` (def-gdb-variable
250 (, var-name) (, accessor) (, setter)
251 (, default) (, doc)))))
252
253(def-gdb-var buffer-instance nil
254 "In an instance buffer, the buffer's instance.")
255
256(def-gdb-var buffer-type nil
257 "One of the symbols bound in gdb-instance-buffer-rules")
258
259(def-gdb-var burst ""
260 "A string of characters from gdb that have not yet been processed.")
261
262(def-gdb-var input-queue ()
263 "A list of high priority gdb command objects.")
264
265(def-gdb-var idle-input-queue ()
266 "A list of low priority gdb command objects.")
267
268(def-gdb-var prompting nil
269 "True when gdb is idle with no pending input.")
270
271(def-gdb-var output-sink 'user
e91c1816
TL
272 "The disposition of the output of the current gdb command.
273Possible values are these symbols:
274
275 user -- gdb output should be copied to the gud buffer
276 for the user to see.
277
278 pre-emacs -- output should be ignored util the post-prompt
279 annotation is received. Then the output-sink
280 becomes:...
281 emacs -- output should be collected in the partial-output-buffer
282 for subsequent processing by a command. This is the
283 disposition of output generated by commands that
284 gud mode sends to gdb on its own behalf.
285 post-emacs -- ignore input until the prompt annotation is
286 received, then go to USER disposition.
287")
c9d4b5ef
JK
288
289(def-gdb-var current-item nil
290 "The most recent command item sent to gdb.")
291
292(def-gdb-var pending-triggers '()
293 "A list of trigger functions that have run later than their output
294handlers.")
295
296(defun in-gdb-instance-context (instance form)
297 "Funcall `form' in the gud buffer of `instance'"
298 (save-excursion
299 (set-buffer (gdb-get-instance-buffer instance 'gud))
300 (funcall form)))
301
302;; end of instance vars
303
304;;
305;; finding instances
306;;
307
308(defun gdb-proc->instance (proc)
309 (save-excursion
310 (set-buffer (process-buffer proc))
311 gdb-buffer-instance))
312
313(defun gdb-mru-instance-buffer ()
314 "Return the most recently used (non-auxiliary) gdb gud buffer."
315 (save-excursion
316 (gdb-goto-first-gdb-instance (buffer-list))))
317
318(defun gdb-goto-first-gdb-instance (blist)
319 "Use gdb-mru-instance-buffer -- not this."
320 (and blist
321 (progn
322 (set-buffer (car blist))
323 (or (and gdb-buffer-instance
324 (eq gdb-buffer-type 'gud)
325 (car blist))
3e873a96 326 (gdb-goto-first-gdb-instance (cdr blist))))))
c9d4b5ef
JK
327
328(defun buffer-gdb-instance (buf)
329 (save-excursion
330 (set-buffer buf)
331 gdb-buffer-instance))
332
333(defun gdb-needed-default-instance ()
334 "Return the most recently used gdb instance or signal an error."
335 (let ((buffer (gdb-mru-instance-buffer)))
336 (or (and buffer (buffer-gdb-instance buffer))
337 (error "No instance of gdb found."))))
338
339(defun gdb-instance-target-string (instance)
340 "The apparent name of the program being debugged by a gdb instance.
341For sure this the root string used in smashing together the gud
342buffer's name, even if that doesn't happen to be the name of a
343program."
344 (in-gdb-instance-context
345 instance
346 (function (lambda () gud-target-name))))
347
e91c1816
TL
348\f
349
350;;
351;; Instance Buffers.
352;;
353
c9d4b5ef
JK
354;; More than one buffer can be associated with a gdb instance.
355;;
e91c1816 356;; Each buffer has a TYPE -- a symbol that identifies the function
c9d4b5ef
JK
357;; of that particular buffer.
358;;
359;; The usual gud interaction buffer is given the type `gud' and
360;; is constructed specially.
361;;
362;; Others are constructed by gdb-get-create-instance-buffer and
e91c1816
TL
363;; named according to the rules set forth in the gdb-instance-buffer-rules-assoc
364
365(defun gdb-get-instance-buffer (instance key)
366 "Return the instance buffer for `instance' tagged with type `key'.
367The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
368 (save-excursion
369 (gdb-look-for-tagged-buffer instance key (buffer-list))))
370
371(defun gdb-get-create-instance-buffer (instance key)
372 "Create a new gdb instance buffer of the type specified by `key'.
373The key should be one of the cars in `gdb-instance-buffer-rules-assoc'."
374 (or (gdb-get-instance-buffer instance key)
375 (let* ((rules (assoc key gdb-instance-buffer-rules-assoc))
376 (name (funcall (gdb-rules-name-maker rules) instance))
377 (new (get-buffer-create name)))
378 (save-excursion
379 (set-buffer new)
380 (make-variable-buffer-local 'gdb-buffer-type)
381 (setq gdb-buffer-type key)
382 (make-variable-buffer-local 'gdb-buffer-instance)
383 (setq gdb-buffer-instance instance)
384 (if (cdr (cdr rules))
385 (funcall (car (cdr (cdr rules)))))
386 new))))
387
388(defun gdb-rules-name-maker (rules) (car (cdr rules)))
389
390(defun gdb-look-for-tagged-buffer (instance key bufs)
391 (let ((retval nil))
392 (while (and (not retval) bufs)
393 (set-buffer (car bufs))
394 (if (and (eq gdb-buffer-instance instance)
395 (eq gdb-buffer-type key))
396 (setq retval (car bufs)))
397 (setq bufs (cdr bufs))
398 )
399 retval))
400
401(defun gdb-instance-buffer-p (buf)
402 (save-excursion
403 (set-buffer buf)
404 (and gdb-buffer-type
405 (not (eq gdb-buffer-type 'gud)))))
406
407;;
408;; This assoc maps buffer type symbols to rules. Each rule is a list of
409;; at least one and possible more functions. The functions have these
410;; roles in defining a buffer type:
411;;
412;; NAME - take an instance, return a name for this type buffer for that
413;; instance.
414;; The remaining function(s) are optional:
415;;
416;; MODE - called in new new buffer with no arguments, should establish
417;; the proper mode for the buffer.
418;;
419
420(defvar gdb-instance-buffer-rules-assoc '())
421
422(defun gdb-set-instance-buffer-rules (buffer-type &rest rules)
423 (let ((binding (assoc buffer-type gdb-instance-buffer-rules-assoc)))
424 (if binding
425 (setcdr binding rules)
426 (setq gdb-instance-buffer-rules-assoc
427 (cons (cons buffer-type rules)
428 gdb-instance-buffer-rules-assoc)))))
429
430(gdb-set-instance-buffer-rules 'gud 'error) ; gud buffers are an exception to the rules
431
432;;
433;; partial-output buffers
434;;
435;; These accumulate output from a command executed on
436;; behalf of emacs (rather than the user).
c9d4b5ef
JK
437;;
438
e91c1816
TL
439(gdb-set-instance-buffer-rules 'gdb-partial-output-buffer
440 'gdb-partial-output-name)
c9d4b5ef 441
e91c1816
TL
442(defun gdb-partial-output-name (instance)
443 (concat "*partial-output-"
444 (gdb-instance-target-string instance)
445 "*"))
446
447\f
c9d4b5ef
JK
448
449;;
450;; gdb communications
451;;
452
e91c1816 453;; INPUT: things sent to gdb
c9d4b5ef
JK
454;;
455;; Each instance has a high and low priority
456;; input queue. Low priority input is sent only
457;; when the high priority queue is idle.
458;;
459;; The queues are lists. Each element is either
460;; a string (indicating user or user-like input)
461;; or a list of the form:
462;;
463;; (INPUT-STRING HANDLER-FN)
464;;
465;;
466;; The handler function will be called from the
467;; partial-output buffer when the command completes.
e91c1816
TL
468;; This is the way to write commands which
469;; invoke gdb commands autonomously.
c9d4b5ef
JK
470;;
471;; These lists are consumed tail first.
472;;
473
474(defun gdb-send (proc string)
475 "A comint send filter for gdb.
476This filter may simply queue output for a later time."
477 (let ((instance (gdb-proc->instance proc)))
478 (gdb-instance-enqueue-input instance (concat string "\n"))))
479
f2db7337
JK
480;; Note: Stuff enqueued here will be sent to the next prompt, even if it
481;; is a query, or other non-top-level prompt. To guarantee stuff will get
482;; sent to the top-level prompt, currently it must be put in the idle queue.
e91c1816
TL
483;; ^^^^^^^^^
484;; [This should encourage gud extentions that invoke gdb commands to let
485;; the user go first; it is not a bug. -t]
486;;
487
c9d4b5ef 488(defun gdb-instance-enqueue-input (instance item)
c9d4b5ef
JK
489 (if (gdb-instance-prompting instance)
490 (progn
491 (gdb-send-item instance item)
492 (set-gdb-instance-prompting instance nil))
493 (set-gdb-instance-input-queue
494 instance
495 (cons item (gdb-instance-input-queue instance)))))
496
497(defun gdb-instance-dequeue-input (instance)
498 (let ((queue (gdb-instance-input-queue instance)))
499 (and queue
500 (if (not (cdr queue))
501 (let ((answer (car queue)))
502 (set-gdb-instance-input-queue instance '())
503 answer)
504 (gdb-take-last-elt queue)))))
505
506(defun gdb-instance-enqueue-idle-input (instance item)
e91c1816
TL
507 (if (and (gdb-instance-prompting instance)
508 (not (gdb-instance-input-queue instance)))
c9d4b5ef
JK
509 (progn
510 (gdb-send-item instance item)
511 (set-gdb-instance-prompting instance nil))
512 (set-gdb-instance-idle-input-queue
513 instance
514 (cons item (gdb-instance-idle-input-queue instance)))))
515
516(defun gdb-instance-dequeue-idle-input (instance)
517 (let ((queue (gdb-instance-idle-input-queue instance)))
518 (and queue
519 (if (not (cdr queue))
520 (let ((answer (car queue)))
521 (set-gdb-instance-idle-input-queue instance '())
522 answer)
523 (gdb-take-last-elt queue)))))
524
e91c1816 525; Don't use this in general.
c9d4b5ef 526(defun gdb-take-last-elt (l)
c9d4b5ef
JK
527 (if (cdr (cdr l))
528 (gdb-take-last-elt (cdr l))
529 (let ((answer (car (cdr l))))
530 (setcdr l '())
531 answer)))
532
e91c1816 533\f
c9d4b5ef
JK
534;;
535;; output -- things gdb prints to emacs
536;;
537;; GDB output is a stream interrupted by annotations.
538;; Annotations can be recognized by their beginning
539;; with \C-j\C-z\C-z<tag><opt>\C-j
540;;
541;; The tag is a string obeying symbol syntax.
542;;
543;; The optional part `<opt>' can be either the empty string
544;; or a space followed by more data relating to the annotation.
545;; For example, the SOURCE annotation is followed by a filename,
546;; line number and various useless goo. This data must not include
547;; any newlines.
548;;
549
550
551(defun gud-gdb-marker-filter (string)
552 "A gud marker filter for gdb."
553 ;; Bogons don't tell us the process except through scoping crud.
554 (let ((instance (gdb-proc->instance proc)))
555 (gdb-output-burst instance string)))
556
557(defvar gdb-annotation-rules
558 '(("frames-invalid" gdb-invalidate-frames)
559 ("breakpoints-invalid" gdb-invalidate-breakpoints)
560 ("pre-prompt" gdb-pre-prompt)
561 ("prompt" gdb-prompt)
f2db7337
JK
562 ("commands" gdb-subprompt)
563 ("overload-choice" gdb-subprompt)
564 ("query" gdb-subprompt)
565 ("prompt-for-continue" gdb-subprompt)
c9d4b5ef
JK
566 ("post-prompt" gdb-post-prompt)
567 ("source" gdb-source)
f2db7337 568 )
c9d4b5ef
JK
569 "An assoc mapping annotation tags to functions which process them.")
570
571
572(defun gdb-ignore-annotation (instance args)
573 nil)
574
575(defconst gdb-source-spec-regexp
576 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x[a-f0-9]*")
577
e91c1816 578;; Do not use this except as an annotation handler."
c9d4b5ef 579(defun gdb-source (instance args)
c9d4b5ef
JK
580 (string-match gdb-source-spec-regexp args)
581 ;; Extract the frame position from the marker.
582 (setq gud-last-frame
583 (cons
584 (substring args (match-beginning 1) (match-end 1))
585 (string-to-int (substring args
586 (match-beginning 2)
587 (match-end 2))))))
588
e91c1816
TL
589;; An annotation handler for `prompt'.
590;; This sends the next command (if any) to gdb.
c9d4b5ef 591(defun gdb-prompt (instance ignored)
c9d4b5ef
JK
592 (let ((sink (gdb-instance-output-sink instance)))
593 (cond
594 ((eq sink 'user) t)
595 ((eq sink 'post-emacs)
596 (set-gdb-instance-output-sink instance 'user))
597 ((or (eq sink 'emacs)
598 (eq sink 'pre-emacs))
599 (set-gdb-instance-output-sink instance 'user)
600 (error "Phase error in gdb-prompt (got %s)" sink))
601 (t (set-gdb-instance-output-sink instance 'user))))
602 (let ((highest (gdb-instance-dequeue-input instance)))
603 (if highest
604 (gdb-send-item instance highest)
605 (let ((lowest (gdb-instance-dequeue-idle-input instance)))
606 (if lowest
607 (gdb-send-item instance lowest)
e91c1816
TL
608 (progn
609 (set-gdb-instance-prompting instance t)
610 (gud-display-frame)))))))
c9d4b5ef 611
e91c1816 612;; An annotation handler for non-top-level prompts.
f2db7337 613(defun gdb-subprompt (instance ignored)
f2db7337
JK
614 (let ((highest (gdb-instance-dequeue-input instance)))
615 (if highest
616 (gdb-send-item instance highest)
617 (set-gdb-instance-prompting instance t))))
618
c9d4b5ef 619(defun gdb-send-item (instance item)
c9d4b5ef
JK
620 (set-gdb-instance-current-item instance item)
621 (if (stringp item)
622 (progn
623 (set-gdb-instance-output-sink instance 'user)
624 (process-send-string (gdb-instance-process instance)
625 item))
626 (progn
627 (gdb-clear-partial-output instance)
628 (set-gdb-instance-output-sink instance 'pre-emacs)
629 (process-send-string (gdb-instance-process instance)
630 (car item)))))
631
e91c1816
TL
632;; An annotation handler for `pre-prompt'.
633;; This terminates the collection of output from a previous
634;; command if that happens to be in effect.
c9d4b5ef 635(defun gdb-pre-prompt (instance ignored)
c9d4b5ef
JK
636 (let ((sink (gdb-instance-output-sink instance)))
637 (cond
638 ((eq sink 'user) t)
639 ((eq sink 'emacs)
640 (set-gdb-instance-output-sink instance 'post-emacs)
641 (let ((handler
642 (car (cdr (gdb-instance-current-item instance)))))
643 (save-excursion
644 (set-buffer (gdb-get-create-instance-buffer
645 instance 'gdb-partial-output-buffer))
646 (funcall handler))))
647 ((eq sink 'pre-emacs)
648 (set-gdb-instance-output-sink instance 'user)
649 (error "Output sink phase error 1."))
650 ((eq sink 'post-emacs)
651 (set-gdb-instance-output-sink instance 'user)
652 (error "Output sink phase error 2.")))))
653
e91c1816
TL
654;; An annotation handler for `post-prompt'.
655;; This begins the collection of output from the current
656;; command if that happens to be appropriate."
c9d4b5ef 657(defun gdb-post-prompt (instance ignored)
c9d4b5ef
JK
658 (gdb-invalidate-registers instance ignored)
659 (let ((sink (gdb-instance-output-sink instance)))
660 (cond
661 ((eq sink 'user) t)
662 ((eq sink 'pre-emacs)
663 (set-gdb-instance-output-sink instance 'emacs))
664
665 ((eq sink 'emacs)
666 (set-gdb-instance-output-sink instance 'user)
667 (error "Output sink phase error 3."))
668
669 ((eq sink 'post-emacs)
670 (set-gdb-instance-output-sink instance 'user)
671 (error "Output sink phase error 3.")))))
672
e91c1816
TL
673;; A buffer-local indication of how output from an inferior gdb
674;; should be directed. Legit values are:
c9d4b5ef 675;;
e91c1816
TL
676;; USER -- the output should be appended to the gud
677;; buffer.
678;;
679;; PRE-EMACS -- throw away output preceding output for emacs.
680;; EMACS -- redirect output to the partial-output buffer.
681;; POST-EMACS -- throw away output following output for emacs."
c9d4b5ef
JK
682;;
683
e91c1816
TL
684;; Handle a burst of output from a gdb instance.
685;; This function is (indirectly) used as a gud-marker-filter.
686;; It must return output (if any) to be insterted in the gud
687;; buffer.
c9d4b5ef
JK
688
689(defun gdb-output-burst (instance string)
690 "Handle a burst of output from a gdb instance.
691This function is (indirectly) used as a gud-marker-filter.
692It must return output (if any) to be insterted in the gud
693buffer."
694
695 (save-match-data
696 (let (
697 ;; Recall the left over burst from last time
698 (burst (concat (gdb-instance-burst instance) string))
699 ;; Start accumulating output for the gud buffer
700 (output ""))
701
702 ;; Process all the complete markers in this chunk.
703
704 (while (string-match "\n\032\032\\(.*\\)\n" burst)
705 (let ((annotation (substring burst
706 (match-beginning 1)
707 (match-end 1))))
708
709 ;; Stuff prior to the match is just ordinary output.
710 ;; It is either concatenated to OUTPUT or directed
711 ;; elsewhere.
712 (setq output
713 (gdb-concat-output
714 instance
715 output
716 (substring burst 0 (match-beginning 0))))
717
718 ;; Take that stuff off the burst.
719 (setq burst (substring burst (match-end 0)))
720
721 ;; Parse the tag from the annotation, and maybe its arguments.
722 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
723 (let* ((annotation-type (substring annotation
724 (match-beginning 1)
725 (match-end 1)))
726 (annotation-arguments (substring annotation
727 (match-beginning 2)
728 (match-end 2)))
729 (annotation-rule (assoc annotation-type
730 gdb-annotation-rules)))
731 ;; Call the handler for this annotation.
c9d4b5ef
JK
732 (if annotation-rule
733 (funcall (car (cdr annotation-rule))
734 instance
735 annotation-arguments)
f2db7337
JK
736 ;; Else the annotation is not recognized. Ignore it silently,
737 ;; so that GDB can add new annotations without causing
738 ;; us to blow up.
739 ))))
c9d4b5ef
JK
740
741
742 ;; Does the remaining text end in a partial line?
743 ;; If it does, then keep part of the burst until we get more.
744 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
745 burst)
746 (progn
747 ;; Everything before the potential marker start can be output.
748 (setq output
749 (gdb-concat-output
750 instance
751 output
752 (substring burst 0 (match-beginning 0))))
753
754 ;; Everything after, we save, to combine with later input.
755 (setq burst (substring burst (match-beginning 0))))
756
757 ;; In case we know the burst contains no partial annotations:
758 (progn
759 (setq output (gdb-concat-output instance output burst))
760 (setq burst "")))
761
762 ;; Save the remaining burst for the next call to this function.
763 (set-gdb-instance-burst instance burst)
764 output)))
765
766(defun gdb-concat-output (instance so-far new)
767 (let ((sink (gdb-instance-output-sink instance)))
768 (cond
769 ((eq sink 'user) (concat so-far new))
770 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
771 ((eq sink 'emacs)
772 (gdb-append-to-partial-output instance new)
773 so-far)
e91c1816 774 (t (error "Bogon output sink %S" sink)))))
c9d4b5ef
JK
775
776(defun gdb-append-to-partial-output (instance string)
777 (save-excursion
778 (set-buffer
779 (gdb-get-create-instance-buffer
780 instance 'gdb-partial-output-buffer))
781 (goto-char (point-max))
782 (insert string)))
783
784(defun gdb-clear-partial-output (instance)
785 (save-excursion
786 (set-buffer
787 (gdb-get-create-instance-buffer
788 instance 'gdb-partial-output-buffer))
1cfc93e1 789 (delete-region (point-min) (point-max))))
e91c1816 790\f
c9d4b5ef 791
e91c1816
TL
792
793;; One trick is to have a command who's output is always available in
794;; a buffer of it's own, and is always up to date. We build several
795;; buffers of this type.
c9d4b5ef 796;;
e91c1816
TL
797;; There are two aspects to this: gdb has to tell us when the output
798;; for that command might have changed, and we have to be able to run
799;; the command behind the user's back.
c9d4b5ef 800;;
e91c1816
TL
801;; The idle input queue and the output phasing associated with
802;; the instance variable `(gdb-instance-output-sink instance)' help
803;; us to run commands behind the user's back.
c9d4b5ef 804;;
e91c1816
TL
805;; Below is the code for specificly managing buffers of output from one
806;; command.
c9d4b5ef
JK
807;;
808
c9d4b5ef 809
e91c1816
TL
810;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
811;; It adds an idle input for the command we are tracking. It should be the
812;; annotation rule binding of whatever gdb sends to tell us this command
813;; might have changed it's output.
814;;
815;; NAME is the fucntion name. DEMAND-PREDICATE tests if output is really needed.
816;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
817;; input in the input queue (see comment about ``gdb communications'' above).
818(defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command output-handler)
819 (`
820 (defun (, name) (instance &optional ignored)
821 (if (and ((, demand-predicate) instance)
822 (not (member '(, name)
823 (gdb-instance-pending-triggers instance))))
824 (progn
825 (gdb-instance-enqueue-idle-input
826 instance
827 (list (, gdb-command) '(, output-handler)))
828 (set-gdb-instance-pending-triggers
829 instance
830 (cons '(, name)
831 (gdb-instance-pending-triggers instance))))))))
832
833(defmacro def-gdb-auto-update-handler (name trigger buf-key)
834 (`
835 (defun (, name) ()
836 (set-gdb-instance-pending-triggers
837 instance
838 (delq '(, trigger)
839 (gdb-instance-pending-triggers instance)))
840 (let ((buf (gdb-get-instance-buffer instance
841 '(, buf-key))))
842 (and buf
843 (save-excursion
844 (set-buffer buf)
845 (let ((p (point))
846 (buffer-read-only nil))
847 (delete-region (point-min) (point-max))
848 (insert-buffer (gdb-get-create-instance-buffer
849 instance
850 'gdb-partial-output-buffer))
851 (goto-char p))))))))
c9d4b5ef 852
e91c1816
TL
853(defmacro def-gdb-auto-updated-buffer
854 (buffer-key trigger-name gdb-command output-handler-name)
855 (`
856 (progn
857 (def-gdb-auto-update-trigger (, trigger-name)
858 ;; The demand predicate:
859 (lambda (instance)
860 (gdb-get-instance-buffer instance '(, buffer-key)))
861 (, gdb-command)
862 (, output-handler-name))
863 (def-gdb-auto-update-handler (, output-handler-name)
864 (, trigger-name) (, buffer-key)))))
c9d4b5ef 865
c9d4b5ef 866
e91c1816 867\f
c9d4b5ef 868;;
e91c1816
TL
869;; Breakpoint buffers
870;;
871;; These display the output of `info breakpoints'.
c9d4b5ef
JK
872;;
873
e91c1816
TL
874
875(gdb-set-instance-buffer-rules 'gdb-breakpoints-buffer
876 'gdb-breakpoints-buffer-name
877 'gud-breakpoints-mode)
c9d4b5ef 878
e91c1816
TL
879(def-gdb-auto-updated-buffer gdb-breakpoints-buffer
880 ;; This defines the auto update rule for buffers of type
881 ;; `gdb-breakpoints-buffer'.
882 ;;
883 ;; It defines a function to serve as the annotation handler that
884 ;; handles the `foo-invalidated' message. That function is called:
885 gdb-invalidate-breakpoints
c9d4b5ef 886
e91c1816
TL
887 ;; To update the buffer, this command is sent to gdb.
888 "server info breakpoints\n"
c9d4b5ef 889
e91c1816
TL
890 ;; This also defines a function to be the handler for the output
891 ;; from the command above. That function will copy the output into
892 ;; the appropriately typed buffer. That function will be called:
893 gdb-info-breakpoints-handler)
c9d4b5ef
JK
894
895(defun gdb-breakpoints-buffer-name (instance)
896 (save-excursion
897 (set-buffer (process-buffer (gdb-instance-process instance)))
898 (concat "*breakpoints of " (gdb-instance-target-string instance) "*")))
899
900(defun gud-display-breakpoints-buffer (instance)
901 (interactive (list (gdb-needed-default-instance)))
f3bfec59 902 (gud-display-buffer
c9d4b5ef
JK
903 (gdb-get-create-instance-buffer instance
904 'gdb-breakpoints-buffer)))
905
e91c1816
TL
906(defun gud-frame-breakpoints-buffer (instance)
907 (interactive (list (gdb-needed-default-instance)))
908 (gud-frame-buffer
909 (gdb-get-create-instance-buffer instance
910 'gdb-breakpoints-buffer)))
911
912(defvar gud-breakpoints-mode-map nil)
913(setq gud-breakpoints-mode-map (make-keymap))
914(suppress-keymap gud-breakpoints-mode-map)
915(define-key gud-breakpoints-mode-map " " 'gud-toggle-bp-this-line)
916(define-key gud-breakpoints-mode-map "d" 'gud-delete-bp-this-line)
917
918(defun gud-breakpoints-mode ()
919 "Major mode for gud breakpoints.
920
921\\{gud-breakpoints-mode-map}"
922 (setq major-mode 'gud-breakpoints-mode)
923 (setq mode-name "Breakpoints")
924 (use-local-map gud-breakpoints-mode-map)
925 (setq buffer-read-only t)
926 (gdb-invalidate-breakpoints gdb-buffer-instance))
c9d4b5ef 927
296fa52f
JK
928(defun gud-toggle-bp-this-line ()
929 (interactive)
930 (save-excursion
931 (beginning-of-line 1)
932 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
933 (error "Not recognized as breakpoint line (demo foo).")
934 (gdb-instance-enqueue-idle-input
935 gdb-buffer-instance
936 (list
937 (concat
938 (if (eq ?y (char-after (match-beginning 2)))
939 "server disable "
940 "server enable ")
941 (buffer-substring (match-beginning 0)
942 (match-end 1))
943 "\n")
944 '(lambda () nil)))
945 )))
946
947(defun gud-delete-bp-this-line ()
948 (interactive)
949 (save-excursion
950 (beginning-of-line 1)
951 (if (not (looking-at "\\([0-9]*\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)"))
952 (error "Not recognized as breakpoint line (demo foo).")
953 (gdb-instance-enqueue-idle-input
954 gdb-buffer-instance
955 (list
956 (concat
957 "server delete "
958 (buffer-substring (match-beginning 0)
959 (match-end 1))
960 "\n")
961 '(lambda () nil)))
962 )))
963
296fa52f 964
296fa52f 965
e91c1816
TL
966\f
967;;
968;; Frames buffers. These display a perpetually correct bactracktrace
969;; (from the command `where').
970;;
971;; Alas, if your stack is deep, they are costly.
972;;
973
974(gdb-set-instance-buffer-rules 'gdb-stack-buffer
975 'gdb-stack-buffer-name
976 'gud-frames-mode)
977
978(def-gdb-auto-updated-buffer gdb-stack-buffer
979 gdb-invalidate-frames
980 "server where\n"
981 gdb-info-frames-handler)
982
983(defun gdb-stack-buffer-name (instance)
984 (save-excursion
985 (set-buffer (process-buffer (gdb-instance-process instance)))
986 (concat "*stack frames of "
987 (gdb-instance-target-string instance) "*")))
296fa52f 988
e91c1816
TL
989(defun gud-display-stack-buffer (instance)
990 (interactive (list (gdb-needed-default-instance)))
991 (gud-display-buffer
992 (gdb-get-create-instance-buffer instance
993 'gdb-stack-buffer)))
994
995(defun gud-frame-stack-buffer (instance)
996 (interactive (list (gdb-needed-default-instance)))
997 (gud-frame-buffer
998 (gdb-get-create-instance-buffer instance
999 'gdb-stack-buffer)))
1000
1001(defvar gud-frames-mode-map nil)
1002(setq gud-frames-mode-map (make-keymap))
1003(suppress-keymap gud-frames-mode-map)
1004(define-key gud-frames-mode-map [mouse-2]
1005 'gud-frames-select-by-mouse)
1006
1007(defun gud-frames-mode ()
1008 "Major mode for gud frames.
1009
1010\\{gud-frames-mode-map}"
1011 (setq major-mode 'gud-frames-mode)
1012 (setq mode-name "Frames")
1013 (setq buffer-read-only t)
1014 (use-local-map gud-frames-mode-map)
1015 (gdb-invalidate-frames gdb-buffer-instance))
1016
1017(defun gud-get-frame-number ()
1018 (save-excursion
1019 (let* ((pos (re-search-backward "^#\\([0-9]*\\)" nil t))
1020 (n (or (and pos
1021 (string-to-int
1022 (buffer-substring (match-beginning 1)
1023 (match-end 1))))
1024 0)))
1025 n)))
1026
1027(defun gud-frames-select-by-mouse (e)
1028 (interactive "e")
1029 (let (selection)
1030 (save-excursion
1031 (set-buffer (window-buffer (posn-window (event-end e))))
1032 (save-excursion
1033 (goto-char (posn-point (event-end e)))
1034 (setq selection (gud-get-frame-number))))
1035 (select-window (posn-window (event-end e)))
1036 (save-excursion
1037 (set-buffer (gdb-get-instance-buffer (gdb-needed-default-instance) 'gud))
1038 (gud-call "fr %p" selection)
1039 (gud-display-frame))))
296fa52f 1040
e91c1816 1041\f
c9d4b5ef
JK
1042;;
1043;; Registers buffers
1044;;
1045
e91c1816
TL
1046(def-gdb-auto-updated-buffer gdb-registers-buffer
1047 gdb-invalidate-registers
1048 "server info registers\n"
1049 gdb-info-registers-handler)
1050
1051(gdb-set-instance-buffer-rules 'gdb-registers-buffer
1052 'gdb-registers-buffer-name
1053 'gud-registers-mode)
1054
1055(defvar gud-registers-mode-map nil)
1056(setq gud-registers-mode-map (make-keymap))
1057(suppress-keymap gud-registers-mode-map)
1058
1059(defun gud-registers-mode ()
1060 "Major mode for gud registers.
1061
1062\\{gud-registers-mode-map}"
1063 (setq major-mode 'gud-registers-mode)
1064 (setq mode-name "Registers")
1065 (setq buffer-read-only t)
1066 (use-local-map gud-registers-mode-map)
1067 (gdb-invalidate-registers gdb-buffer-instance))
1068
c9d4b5ef
JK
1069(defun gdb-registers-buffer-name (instance)
1070 (save-excursion
1071 (set-buffer (process-buffer (gdb-instance-process instance)))
1072 (concat "*registers of " (gdb-instance-target-string instance) "*")))
1073
1074(defun gud-display-registers-buffer (instance)
1075 (interactive (list (gdb-needed-default-instance)))
f3bfec59 1076 (gud-display-buffer
c9d4b5ef
JK
1077 (gdb-get-create-instance-buffer instance
1078 'gdb-registers-buffer)))
1079
e91c1816
TL
1080(defun gud-frame-registers-buffer (instance)
1081 (interactive (list (gdb-needed-default-instance)))
1082 (gud-frame-buffer
1083 (gdb-get-create-instance-buffer instance
1084 'gdb-registers-buffer)))
1085
f3bfec59 1086\f
e91c1816
TL
1087
1088;;;; Menu windows:
1089
1090
1091;; MENU-LIST is ((option option option...) (option option ...)...)
1092;;
1093(defun gud-display-menu (menu-list)
1094 (setq fill-column (min 120 (- (window-width)
1095 (min 8 (window-width)))))
1096 (while menu-list
1097 (mapcar (function (lambda (x) (insert (symbol-name x) " "))) (car menu-list))
1098 (fill-paragraph nil)
1099 (insert "\n\n")
1100 (setq menu-list (cdr menu-list)))
1101 (goto-char (point-min))
1102 (while (re-search-forward "\\([^ \n]+\\)\\(\n\\| \\)" nil t)
1103 (put-text-property (match-beginning 1) (match-end 1)
1104 'mouse-face 'highlight))
1105 (goto-char (point-min)))
1106
1107(defun gud-goto-menu (menu)
1108 (setq gud-menu-position menu)
1109 (let ((buffer-read-only nil))
1110 (delete-region (point-min) (point-max))
1111 (gud-display-menu menu)))
1112
1113(defun gud-menu-pick (event)
1114 "Choose an item from a gdb command menu."
1115 (interactive "e")
1116 (let (choice)
1117 (save-excursion
1118 (set-buffer (window-buffer (posn-window (event-start event))))
1119 (goto-char (posn-point (event-start event)))
1120 (let (beg end)
1121 (skip-chars-forward "^ \t\n")
1122 (setq end (point))
1123 (skip-chars-backward "^ \t\n")
1124 (setq beg (point))
1125 (setq choice (buffer-substring beg end))
1126 (message choice)
1127 (gud-invoke-menu (intern choice))))))
1128
1129(defun gud-invoke-menu (symbol)
1130 (let ((meaning (assoc symbol gud-menu-rules)))
1131 (cond
1132 ((and (consp meaning)
1133 (consp (car (cdr meaning))))
1134 (gud-goto-menu (car (cdr meaning))))
1135 (meaning (call-interactively (car (cdr meaning)))))))
1136
1137\f
1138
1139(gdb-set-instance-buffer-rules 'gdb-command-buffer
1140 'gdb-command-buffer-name
1141 'gud-command-mode)
1142
1143(defvar gud-command-mode-map nil)
1144(setq gud-command-mode-map (make-keymap))
1145(suppress-keymap gud-command-mode-map)
1146(define-key gud-command-mode-map [mouse-2] 'gud-menu-pick)
1147
1148(defun gud-command-mode ()
1149 "Major mode for gud menu.
1150
1151\\{gud-command-mode-map}" (interactive) (setq major-mode 'gud-command-mode)
1152 (setq mode-name "Menu") (setq buffer-read-only t) (use-local-map
1153 gud-command-mode-map) (make-variable-buffer-local 'gud-menu-position)
1154 (if (not gud-menu-position) (gud-goto-menu gud-running-menu)))
1155
1156(defun gdb-command-buffer-name (instance)
1157 (save-excursion
1158 (set-buffer (process-buffer (gdb-instance-process instance)))
1159 (concat "*menu of " (gdb-instance-target-string instance) "*")))
1160
1161(defun gud-display-command-buffer (instance)
1162 (interactive (list (gdb-needed-default-instance)))
1163 (gud-display-buffer
1164 (gdb-get-create-instance-buffer instance
1165 'gdb-command-buffer)
1166 6))
1167
1168(defun gud-frame-command-buffer (instance)
1169 (interactive (list (gdb-needed-default-instance)))
1170 (gud-frame-buffer
1171 (gdb-get-create-instance-buffer instance
1172 'gdb-command-buffer)))
1173
1174(defvar gud-selected-menu-titles ())
1175(setq gud-selected-menu-titles
1176 '(RUNNING STACK DATA BREAKPOINTS FILES))
1177
1178(setq gud-running-menu
1179 (list
1180 '(RUNNING stack breakpoints files)
1181 '(target run next step continue finish stepi kill help-running)))
1182
1183(setq gud-stack-menu
1184 (list
1185 '(running STACK breakpoints files)
1186 '(up down frame backtrace return help-stack)))
1187
1188(setq gud-data-menu
1189 (list
1190 '(running stack DATA breakpoints files)
1191 '(whatis ptype print set display undisplay disassemble help-data)))
1192
1193(setq gud-breakpoints-menu
1194 (list
1195 '(running stack BREAKPOINTS files)
1196 '(awatch rwatch watch break delete enable disable condition ignore help-breakpoints)))
1197
1198(setq gud-files-menu
1199 (list
1200 '(running stack breakpoints FILES)
1201 '(file core-file help-files)
1202 '(exec-file load symbol-file add-symbol-file sharedlibrary)))
1203
1204(setq gud-menu-rules
1205 (list
1206 (list 'running gud-running-menu)
1207 (list 'RUNNING gud-running-menu)
1208 (list 'stack gud-stack-menu)
1209 (list 'STACK gud-stack-menu)
1210 (list 'data gud-data-menu)
1211 (list 'DATA gud-data-menu)
1212 (list 'breakpoints gud-breakpoints-menu)
1213 (list 'BREAKPOINTS gud-breakpoints-menu)
1214 (list 'files gud-files-menu)
1215 (list 'FILES gud-files-menu)
1216
1217 (list 'target 'gud-target)
1218 (list 'kill 'gud-kill)
1219 (list 'stepi 'gud-stepi)
1220 (list 'step 'gud-step)
1221 (list 'next 'gud-next)
1222 (list 'finish 'gud-finish)
1223 (list 'continue 'gud-cont)
1224 (list 'run 'gud-run)
1225
1226 (list 'backtrace 'gud-backtrace)
1227 (list 'frame 'gud-frame)
1228 (list 'down 'gud-down)
1229 (list 'up 'gud-up)
1230 (list 'return 'gud-return)
1231
1232 (list 'file 'gud-file)
1233 (list 'core-file 'gud-core-file)
1234 (list 'cd 'gud-cd)
1235
1236 (list 'exec-file 'gud-exec-file)
1237 (list 'load 'gud-load)
1238 (list 'symbol-file 'gud-symbol-file)
1239 (list 'add-symbol-file 'gud-add-symbol-file)
1240 (list 'sharedlibrary 'gud-sharedlibrary)
1241 ))
1242
1243
1244\f
1245
1246(defun gdb-call-showing-gud (instance command)
1247 (gud-display-gud-buffer instance)
1248 (comint-input-sender (gdb-instance-process instance) command))
1249
1250(defvar gud-target-history ())
1251
1252(defun gud-temp-buffer-show (buf)
1253 (let ((ow (selected-window)))
1254 (unwind-protect
1255 (progn
1256 (pop-to-buffer buf)
1257
1258 ;; This insertion works around a bug in emacs.
1259 ;; The bug is that all the empty space after a
1260 ;; highlighted word that terminates a buffer
1261 ;; gets highlighted. That's really ugly, so
1262 ;; make sure a highlighted word can't ever
1263 ;; terminate the buffer.
1264 (goto-char (point-max))
1265 (insert "\n")
1266 (goto-char (point-min))
1267
1268 (if (< (window-height) 10)
1269 (enlarge-window (- 10 (window-height)))))
1270 (select-window ow))))
1271
1272(defun gud-target (instance command)
1273 (interactive
1274 (let* ((instance (gdb-needed-default-instance))
1275 (temp-buffer-show-function (function gud-temp-buffer-show))
1276 (target-name (completing-read (format "Target type: ")
1277 '(("remote")
1278 ("core")
1279 ("child")
1280 ("exec"))
1281 nil
1282 t
1283 nil
1284 'gud-target-history)))
1285 (list instance
1286 (cond
1287 ((equal target-name "child") "run")
1288
1289 ((equal target-name "core")
1290 (concat "target core "
1291 (read-file-name "core file: "
1292 nil
1293 "core"
1294 t)))
1295
1296 ((equal target-name "exec")
1297 (concat "target exec "
1298 (read-file-name "exec file: "
1299 nil
1300 "a.out"
1301 t)))
1302
1303 ((equal target-name "remote")
1304 (concat "target remote "
1305 (read-file-name "serial line for remote: "
1306 "/dev/"
1307 "ttya"
1308 t)))
1309
1310 (t "echo No such target command!")))))
1311
1312 (gud-display-gud-buffer instance)
1313 (apply comint-input-sender
1314 (list (gdb-instance-process instance) command)))
1315
1316(defun gud-backtrace ()
1317 (interactive)
1318 (let ((instance (gdb-needed-default-instance)))
1319 (gud-display-gud-buffer instance)
1320 (apply comint-input-sender
1321 (list (gdb-instance-process instance)
1322 "backtrace"))))
1323
1324(defun gud-frame ()
1325 (interactive)
1326 (let ((instance (gdb-needed-default-instance)))
1327 (apply comint-input-sender
1328 (list (gdb-instance-process instance)
1329 "frame"))))
1330
1331(defun gud-return (instance command)
1332 (interactive
1333 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1334 (list (gdb-needed-default-instance)
1335 (concat "return " (read-string "Expression to return: ")))))
1336 (gud-display-gud-buffer instance)
1337 (apply comint-input-sender
1338 (list (gdb-instance-process instance) command)))
1339
1340
1341(defun gud-file (instance command)
1342 (interactive
1343 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1344 (list (gdb-needed-default-instance)
1345 (concat "file " (read-file-name "Executable to debug: "
1346 nil
1347 "a.out"
1348 t)))))
1349 (gud-display-gud-buffer instance)
1350 (apply comint-input-sender
1351 (list (gdb-instance-process instance) command)))
1352
1353(defun gud-core-file (instance command)
1354 (interactive
1355 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1356 (list (gdb-needed-default-instance)
1357 (concat "core " (read-file-name "Core file to debug: "
1358 nil
1359 "core-file"
1360 t)))))
1361 (gud-display-gud-buffer instance)
1362 (apply comint-input-sender
1363 (list (gdb-instance-process instance) command)))
1364
1365(defun gud-cd (dir)
1366 (interactive "FChange GDB's default directory: ")
1367 (let ((instance (gdb-needed-default-instance)))
1368 (save-excursion
1369 (set-buffer (gdb-get-instance-buffer instance 'gud))
1370 (cd dir))
1371 (gud-display-gud-buffer instance)
1372 (apply comint-input-sender
1373 (list (gdb-instance-process instance)
1374 (concat "cd " dir)))))
1375
1376
1377(defun gud-exec-file (instance command)
1378 (interactive
1379 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1380 (list (gdb-needed-default-instance)
1381 (concat "exec-file " (read-file-name "Init memory from executable: "
1382 nil
1383 "a.out"
1384 t)))))
1385 (gud-display-gud-buffer instance)
1386 (apply comint-input-sender
1387 (list (gdb-instance-process instance) command)))
1388
1389(defun gud-load (instance command)
1390 (interactive
1391 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1392 (list (gdb-needed-default-instance)
1393 (concat "load " (read-file-name "Dynamicly load from file: "
1394 nil
1395 "a.out"
1396 t)))))
1397 (gud-display-gud-buffer instance)
1398 (apply comint-input-sender
1399 (list (gdb-instance-process instance) command)))
1400
1401(defun gud-symbol-file (instance command)
1402 (interactive
1403 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1404 (list (gdb-needed-default-instance)
1405 (concat "symbol-file " (read-file-name "Read symbol table from file: "
1406 nil
1407 "a.out"
1408 t)))))
1409 (gud-display-gud-buffer instance)
1410 (apply comint-input-sender
1411 (list (gdb-instance-process instance) command)))
1412
1413
1414(defun gud-add-symbol-file (instance command)
1415 (interactive
1416 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1417 (list (gdb-needed-default-instance)
1418 (concat "add-symbol-file "
1419 (read-file-name "Add symbols from file: "
1420 nil
1421 "a.out"
1422 t)))))
1423 (gud-display-gud-buffer instance)
1424 (apply comint-input-sender
1425 (list (gdb-instance-process instance) command)))
1426
1427
1428(defun gud-sharedlibrary (instance command)
1429 (interactive
1430 (let ((temp-buffer-show-function (function gud-temp-buffer-show)))
1431 (list (gdb-needed-default-instance)
1432 (concat "sharedlibrary "
1433 (read-string "Load symbols for files matching regexp: ")))))
1434 (gud-display-gud-buffer instance)
1435 (apply comint-input-sender
1436 (list (gdb-instance-process instance) command)))
1437
1438
1439
1440\f
1441
1442;;;; Window management
1443
1444
f3bfec59
JK
1445;;; FIXME: This should only return true for buffers in the current instance
1446(defun gud-protected-buffer-p (buffer)
1447 "Is BUFFER a buffer which we want to leave displayed?"
1448 (save-excursion
1449 (set-buffer buffer)
e91c1816
TL
1450 (or gdb-buffer-type
1451 overlay-arrow-position)))
1452
f3bfec59
JK
1453;;; The way we abuse the dedicated-p flag is pretty gross, but seems
1454;;; to do the right thing. Seeing as there is no way for Lisp code to
1455;;; get at the use_time field of a window, I'm not sure there exists a
1456;;; more elegant solution without writing C code.
1457
e91c1816
TL
1458(defun gud-display-buffer (buf &optional size)
1459 (let ((must-split nil)
1460 (answer nil))
f3bfec59
JK
1461 (unwind-protect
1462 (progn
1463 (walk-windows
1464 '(lambda (win)
1465 (if (gud-protected-buffer-p (window-buffer win))
e91c1816
TL
1466 (set-window-dedicated-p win t))))
1467 (setq answer (get-buffer-window buf))
1468 (if (not answer)
1469 (let ((window (get-lru-window)))
f3bfec59 1470 (if window
e91c1816
TL
1471 (progn
1472 (set-window-buffer window buf)
1473 (setq answer window))
1474 (setq must-split t)))))
1475 (walk-windows
1476 '(lambda (win)
1477 (if (gud-protected-buffer-p (window-buffer win))
1478 (set-window-dedicated-p win nil)))))
f3bfec59 1479 (if must-split
e91c1816
TL
1480 (let* ((largest (get-largest-window))
1481 (cur-size (window-height largest))
1482 (new-size (and size (< size cur-size) (- cur-size size))))
1483 (setq answer (split-window largest new-size))
1484 (set-window-buffer answer buf)))
1485 answer))
1486
1487(defun existing-source-window (buffer)
1488 (catch 'found
1489 (save-excursion
1490 (walk-windows
1491 (function
1492 (lambda (win)
1493 (if (and overlay-arrow-position
1494 (eq (window-buffer win)
1495 (marker-buffer overlay-arrow-position)))
1496 (progn
1497 (set-window-buffer win buffer)
1498 (throw 'found win))))))
1499 nil)))
1500
1501(defun gud-display-source-buffer (buffer)
1502 (or (existing-source-window buffer)
1503 (gud-display-buffer buffer)))
1504
1505(defun gud-frame-buffer (buf)
1506 (save-excursion
1507 (set-buffer buf)
1508 (make-frame)))
1509
1510\f
1511
1512;;; Shared keymap initialization:
1513
1514(defun make-windows-menu (map)
1515 (define-key map [menu-bar displays]
1516 (cons "GDB-Windows" (make-sparse-keymap "GDB-Windows")))
1517 (define-key map [menu-bar displays gdb]
1518 '("Gdb" . gud-display-gud-buffer))
1519 (define-key map [menu-bar displays registers]
1520 '("Registers" . gud-display-registers-buffer))
1521 (define-key map [menu-bar displays frames]
1522 '("Stack" . gud-display-stack-buffer))
1523 (define-key map [menu-bar displays breakpoints]
1524 '("Breakpoints" . gud-display-breakpoints-buffer))
1525 (define-key map [menu-bar displays commands]
1526 '("Commands" . gud-display-command-buffer)))
1527
1528(defun gud-display-gud-buffer (instance)
1529 (interactive (list (gdb-needed-default-instance)))
1530 (gud-display-buffer
1531 (gdb-get-create-instance-buffer instance 'gud)))
1532
1533(make-windows-menu gud-breakpoints-mode-map)
1534(make-windows-menu gud-frames-mode-map)
1535(make-windows-menu gud-registers-mode-map)
1536
1537
1538
1539(defun make-frames-menu (map)
1540 (define-key map [menu-bar frames]
1541 (cons "GDB-Frames" (make-sparse-keymap "GDB-Frames")))
1542 (define-key map [menu-bar frames gdb]
1543 '("Gdb" . gud-frame-gud-buffer))
1544 (define-key map [menu-bar frames registers]
1545 '("Registers" . gud-frame-registers-buffer))
1546 (define-key map [menu-bar frames frames]
1547 '("Stack" . gud-frame-stack-buffer))
1548 (define-key map [menu-bar frames breakpoints]
1549 '("Breakpoints" . gud-frame-breakpoints-buffer))
1550 (define-key map [menu-bar displays commands]
1551 '("Commands" . gud-display-command-buffer)))
1552
1553(defun gud-frame-gud-buffer (instance)
1554 (interactive (list (gdb-needed-default-instance)))
1555 (gud-frame-buffer
1556 (gdb-get-create-instance-buffer instance 'gud)))
1557
1558(make-frames-menu gud-breakpoints-mode-map)
1559(make-frames-menu gud-frames-mode-map)
1560(make-frames-menu gud-registers-mode-map)
1561
f3bfec59 1562\f
c9d4b5ef
JK
1563(defun gud-gdb-find-file (f)
1564 (find-file-noselect f))
1565
1566;;;###autoload
1567(defun gdb (command-line)
1568 "Run gdb on program FILE in buffer *gud-FILE*.
1569The directory containing FILE becomes the initial working directory
1570and source-file directory for your debugger."
1571 (interactive
1572 (list (read-from-minibuffer "Run gdb (like this): "
1573 (if (consp gud-gdb-history)
1574 (car gud-gdb-history)
f2db7337 1575 "gdb ")
c9d4b5ef
JK
1576 nil nil
1577 '(gud-gdb-history . 1))))
1578 (gud-overload-functions
1579 '((gud-massage-args . gud-gdb-massage-args)
1580 (gud-marker-filter . gud-gdb-marker-filter)
1581 (gud-find-file . gud-gdb-find-file)
1582 ))
1583
1584 (gud-common-init command-line)
1585
1586 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
1587 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
1588 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
e91c1816
TL
1589 (gud-def gud-kill "kill" nil "Kill the program.")
1590 (gud-def gud-run "run" nil "Run the program.")
c9d4b5ef 1591 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
e91c1816 1592 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
c9d4b5ef 1593 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
c9d4b5ef 1594 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
e91c1816 1595 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
c9d4b5ef
JK
1596 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1597 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1598 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1599
1600 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
1601 (setq comint-input-sender 'gdb-send)
1602 (run-hooks 'gdb-mode-hook)
1603 (make-gdb-instance (get-buffer-process (current-buffer)))
1604 )
1605
1606\f
1607;; ======================================================================
1608;; sdb functions
1609
1610;;; History of argument lists passed to sdb.
1611(defvar gud-sdb-history nil)
1612
1613(defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
1614 "If nil, we're on a System V Release 4 and don't need the tags hack.")
1615
1616(defvar gud-sdb-lastfile nil)
1617
1618(defun gud-sdb-massage-args (file args)
1619 (cons file args))
1620
1621(defun gud-sdb-marker-filter (string)
1622 (cond
1623 ;; System V Release 3.2 uses this format
1624 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1625 string)
1626 (setq gud-last-frame
1627 (cons
1628 (substring string (match-beginning 2) (match-end 2))
1629 (string-to-int
1630 (substring string (match-beginning 3) (match-end 3))))))
1631 ;; System V Release 4.0
1632 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
1633 string)
1634 (setq gud-sdb-lastfile
1635 (substring string (match-beginning 2) (match-end 2))))
1636 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
1637 (setq gud-last-frame
1638 (cons
1639 gud-sdb-lastfile
1640 (string-to-int
1641 (substring string (match-beginning 1) (match-end 1))))))
1642 (t
1643 (setq gud-sdb-lastfile nil)))
1644 string)
1645
1646(defun gud-sdb-find-file (f)
1647 (if gud-sdb-needs-tags
1648 (find-tag-noselect f)
1649 (find-file-noselect f)))
1650
1651;;;###autoload
1652(defun sdb (command-line)
1653 "Run sdb on program FILE in buffer *gud-FILE*.
1654The directory containing FILE becomes the initial working directory
1655and source-file directory for your debugger."
1656 (interactive
1657 (list (read-from-minibuffer "Run sdb (like this): "
1658 (if (consp gud-sdb-history)
1659 (car gud-sdb-history)
1660 "sdb ")
1661 nil nil
1662 '(gud-sdb-history . 1))))
1663 (if (and gud-sdb-needs-tags
1664 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
1665 (error "The sdb support requires a valid tags table to work."))
1666 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
1667 (gud-marker-filter . gud-sdb-marker-filter)
1668 (gud-find-file . gud-sdb-find-file)
1669 ))
1670
1671 (gud-common-init command-line)
1672
1673 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
1674 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
1675 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
1676 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
1677 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
1678 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1679 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1680 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
1681
1682 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
1683 (run-hooks 'sdb-mode-hook)
1684 )
1685\f
1686;; ======================================================================
1687;; dbx functions
1688
1689;;; History of argument lists passed to dbx.
1690(defvar gud-dbx-history nil)
1691
1692(defun gud-dbx-massage-args (file args)
1693 (cons file args))
1694
1695(defun gud-dbx-marker-filter (string)
1696 (if (or (string-match
1697 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1698 string)
1699 (string-match
1700 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1701 string))
1702 (setq gud-last-frame
1703 (cons
1704 (substring string (match-beginning 2) (match-end 2))
1705 (string-to-int
1706 (substring string (match-beginning 1) (match-end 1))))))
1707 string)
1708
1709(defun gud-dbx-find-file (f)
1710 (find-file-noselect f))
1711
1712;;;###autoload
1713(defun dbx (command-line)
1714 "Run dbx on program FILE in buffer *gud-FILE*.
1715The directory containing FILE becomes the initial working directory
1716and source-file directory for your debugger."
1717 (interactive
1718 (list (read-from-minibuffer "Run dbx (like this): "
1719 (if (consp gud-dbx-history)
1720 (car gud-dbx-history)
1721 "dbx ")
1722 nil nil
1723 '(gud-dbx-history . 1))))
1724 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args)
1725 (gud-marker-filter . gud-dbx-marker-filter)
1726 (gud-find-file . gud-dbx-find-file)
1727 ))
1728
1729 (gud-common-init command-line)
1730
1731 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1732 "\C-b" "Set breakpoint at current line.")
1733;; (gud-def gud-break "stop at \"%f\":%l"
1734;; "\C-b" "Set breakpoint at current line.")
1735 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1736 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1737 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1738 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1739 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1740 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1741 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1742 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1743
1744 (setq comint-prompt-regexp "^[^)]*dbx) *")
1745 (run-hooks 'dbx-mode-hook)
1746 )
1747\f
1748;; ======================================================================
1749;; xdb (HP PARISC debugger) functions
1750
1751;;; History of argument lists passed to xdb.
1752(defvar gud-xdb-history nil)
1753
1754(defvar gud-xdb-directories nil
1755 "*A list of directories that xdb should search for source code.
1756If nil, only source files in the program directory
1757will be known to xdb.
1758
1759The file names should be absolute, or relative to the directory
1760containing the executable being debugged.")
1761
1762(defun gud-xdb-massage-args (file args)
1763 (nconc (let ((directories gud-xdb-directories)
1764 (result nil))
1765 (while directories
1766 (setq result (cons (car directories) (cons "-d" result)))
1767 (setq directories (cdr directories)))
1768 (nreverse (cons file result)))
1769 args))
1770
1771(defun gud-xdb-file-name (f)
1772 "Transform a relative pathname to a full pathname in xdb mode"
1773 (let ((result nil))
1774 (if (file-exists-p f)
1775 (setq result (expand-file-name f))
1776 (let ((directories gud-xdb-directories))
1777 (while directories
1778 (let ((path (concat (car directories) "/" f)))
1779 (if (file-exists-p path)
1780 (setq result (expand-file-name path)
1781 directories nil)))
1782 (setq directories (cdr directories)))))
1783 result))
1784
1785;; xdb does not print the lines all at once, so we have to accumulate them
1786(defvar gud-xdb-accumulation "")
1787
1788(defun gud-xdb-marker-filter (string)
1789 (let (result)
1790 (if (or (string-match comint-prompt-regexp string)
1791 (string-match ".*\012" string))
1792 (setq result (concat gud-xdb-accumulation string)
1793 gud-xdb-accumulation "")
1794 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
1795 (if result
1796 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
1797 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1798 result))
1799 (let ((line (string-to-int
1800 (substring result (match-beginning 2) (match-end 2))))
1801 (file (gud-xdb-file-name
1802 (substring result (match-beginning 1) (match-end 1)))))
1803 (if file
1804 (setq gud-last-frame (cons file line))))))
1805 (or result "")))
1806
1807(defun gud-xdb-find-file (f)
1808 (let ((realf (gud-xdb-file-name f)))
1809 (if realf (find-file-noselect realf))))
1810
1811;;;###autoload
1812(defun xdb (command-line)
1813 "Run xdb on program FILE in buffer *gud-FILE*.
1814The directory containing FILE becomes the initial working directory
1815and source-file directory for your debugger.
1816
1817You can set the variable 'gud-xdb-directories' to a list of program source
1818directories if your program contains sources from more than one directory."
1819 (interactive
1820 (list (read-from-minibuffer "Run xdb (like this): "
1821 (if (consp gud-xdb-history)
1822 (car gud-xdb-history)
1823 "xdb ")
1824 nil nil
1825 '(gud-xdb-history . 1))))
1826 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
1827 (gud-marker-filter . gud-xdb-marker-filter)
1828 (gud-find-file . gud-xdb-find-file)))
1829
1830 (gud-common-init command-line)
1831
1832 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1833 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1834 "Set temporary breakpoint at current line.")
1835 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1836 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1837 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1838 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1839 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1840 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1841 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1842 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1843
1844 (setq comint-prompt-regexp "^>")
1845 (make-local-variable 'gud-xdb-accumulation)
1846 (setq gud-xdb-accumulation "")
1847 (run-hooks 'xdb-mode-hook))
1848\f
1849;; ======================================================================
1850;; perldb functions
1851
1852;;; History of argument lists passed to perldb.
1853(defvar gud-perldb-history nil)
1854
1855(defun gud-perldb-massage-args (file args)
1856 (cons "-d" (cons file (cons "-emacs" args))))
1857
1858;; There's no guarantee that Emacs will hand the filter the entire
1859;; marker at once; it could be broken up across several strings. We
1860;; might even receive a big chunk with several markers in it. If we
1861;; receive a chunk of text which looks like it might contain the
1862;; beginning of a marker, we save it here between calls to the
1863;; filter.
1864(defvar gud-perldb-marker-acc "")
1865
1866(defun gud-perldb-marker-filter (string)
1867 (save-match-data
1868 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
1869 (let ((output ""))
1870
1871 ;; Process all the complete markers in this chunk.
1872 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1873 gud-perldb-marker-acc)
1874 (setq
1875
1876 ;; Extract the frame position from the marker.
1877 gud-last-frame
1878 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
1879 (string-to-int (substring gud-perldb-marker-acc
1880 (match-beginning 2)
1881 (match-end 2))))
1882
1883 ;; Append any text before the marker to the output we're going
1884 ;; to return - we don't include the marker in this text.
1885 output (concat output
1886 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
1887
1888 ;; Set the accumulator to the remaining text.
1889 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
1890
1891 ;; Does the remaining text look like it might end with the
1892 ;; beginning of another marker? If it does, then keep it in
1893 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
1894 ;; know the full marker regexp above failed, it's pretty simple to
1895 ;; test for marker starts.
1896 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
1897 (progn
1898 ;; Everything before the potential marker start can be output.
1899 (setq output (concat output (substring gud-perldb-marker-acc
1900 0 (match-beginning 0))))
1901
1902 ;; Everything after, we save, to combine with later input.
1903 (setq gud-perldb-marker-acc
1904 (substring gud-perldb-marker-acc (match-beginning 0))))
1905
1906 (setq output (concat output gud-perldb-marker-acc)
1907 gud-perldb-marker-acc ""))
1908
1909 output)))
1910
1911(defun gud-perldb-find-file (f)
1912 (find-file-noselect f))
1913
1914;;;###autoload
1915(defun perldb (command-line)
1916 "Run perldb on program FILE in buffer *gud-FILE*.
1917The directory containing FILE becomes the initial working directory
1918and source-file directory for your debugger."
1919 (interactive
1920 (list (read-from-minibuffer "Run perldb (like this): "
1921 (if (consp gud-perldb-history)
1922 (car gud-perldb-history)
1923 "perl ")
1924 nil nil
1925 '(gud-perldb-history . 1))))
1926 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
1927 (gud-marker-filter . gud-perldb-marker-filter)
1928 (gud-find-file . gud-perldb-find-file)
1929 ))
1930
1931 (gud-common-init command-line)
1932
1933 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1934 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1935 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1936 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1937 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1938; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1939; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1940; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1941 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1942
1943 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1944 (run-hooks 'perldb-mode-hook)
1945 )
1946
1947;;
1948;; End of debugger-specific information
1949;;
1950
1951\f
1952;;; When we send a command to the debugger via gud-call, it's annoying
1953;;; to see the command and the new prompt inserted into the debugger's
1954;;; buffer; we have other ways of knowing the command has completed.
1955;;;
1956;;; If the buffer looks like this:
1957;;; --------------------
1958;;; (gdb) set args foo bar
1959;;; (gdb) -!-
1960;;; --------------------
1961;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1962;;; source file to set a breakpoint, we want the buffer to end up like
1963;;; this:
1964;;; --------------------
1965;;; (gdb) set args foo bar
1966;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1967;;; (gdb) -!-
1968;;; --------------------
1969;;; Essentially, the old prompt is deleted, and the command's output
1970;;; and the new prompt take its place.
1971;;;
1972;;; Not echoing the command is easy enough; you send it directly using
e91c1816 1973;;; comint-input-sender, and it never enters the buffer. However,
c9d4b5ef
JK
1974;;; getting rid of the old prompt is trickier; you don't want to do it
1975;;; when you send the command, since that will result in an annoying
1976;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1977;;; waits for a response from the debugger, and the new prompt is
1978;;; inserted. Instead, we'll wait until we actually get some output
1979;;; from the subprocess before we delete the prompt. If the command
1980;;; produced no output other than a new prompt, that prompt will most
1981;;; likely be in the first chunk of output received, so we will delete
1982;;; the prompt and then replace it with an identical one. If the
1983;;; command produces output, the prompt is moving anyway, so the
1984;;; flicker won't be annoying.
1985;;;
1986;;; So - when we want to delete the prompt upon receipt of the next
1987;;; chunk of debugger output, we position gud-delete-prompt-marker at
1988;;; the start of the prompt; the process filter will notice this, and
1989;;; delete all text between it and the process output marker. If
1990;;; gud-delete-prompt-marker points nowhere, we leave the current
1991;;; prompt alone.
1992(defvar gud-delete-prompt-marker nil)
1993
1994\f
1995(defvar gdbish-comint-mode-map (copy-keymap comint-mode-map))
1996(define-key gdbish-comint-mode-map "\C-c\M-\C-r" 'gud-display-registers-buffer)
e91c1816 1997(define-key gdbish-comint-mode-map "\C-c\M-\C-f" 'gud-display-stack-buffer)
c9d4b5ef
JK
1998(define-key gdbish-comint-mode-map "\C-c\M-\C-b" 'gud-display-breakpoints-buffer)
1999
e91c1816
TL
2000(make-windows-menu gdbish-comint-mode-map)
2001(make-frames-menu gdbish-comint-mode-map)
2002
c9d4b5ef
JK
2003(defun gud-mode ()
2004 "Major mode for interacting with an inferior debugger process.
2005
2006 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2007or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
2008`sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
2009
2010After startup, the following commands are available in both the GUD
2011interaction buffer and any source buffer GUD visits due to a breakpoint stop
2012or step operation:
2013
2014\\[gud-break] sets a breakpoint at the current file and line. In the
2015GUD buffer, the current file and line are those of the last breakpoint or
2016step. In a source buffer, they are the buffer's file and current line.
2017
2018\\[gud-remove] removes breakpoints on the current file and line.
2019
2020\\[gud-refresh] displays in the source window the last line referred to
2021in the gud buffer.
2022
2023\\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2024step-one-line (not entering function calls), and step-one-instruction
2025and then update the source window with the current file and position.
2026\\[gud-cont] continues execution.
2027
2028\\[gud-print] tries to find the largest C lvalue or function-call expression
2029around point, and sends it to the debugger for value display.
2030
2031The above commands are common to all supported debuggers except xdb which
2032does not support stepping instructions.
2033
2034Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2035except that the breakpoint is temporary; that is, it is removed when
2036execution stops on it.
2037
2038Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2039frame. \\[gud-down] drops back down through one.
2040
2041If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2042the current function and stops.
2043
2044All the keystrokes above are accessible in the GUD buffer
2045with the prefix C-c, and in all buffers through the prefix C-x C-a.
2046
2047All pre-defined functions for which the concept make sense repeat
2048themselves the appropriate number of times if you give a prefix
2049argument.
2050
2051You may use the `gud-def' macro in the initialization hook to define other
2052commands.
2053
2054Other commands for interacting with the debugger process are inherited from
2055comint mode, which see."
2056 (interactive)
2057 (comint-mode)
2058 (setq major-mode 'gud-mode)
2059 (setq mode-name "Debugger")
2060 (setq mode-line-process '(": %s"))
2061 (use-local-map (copy-keymap gdbish-comint-mode-map))
c9d4b5ef
JK
2062 (setq gud-last-frame nil)
2063 (make-local-variable 'comint-prompt-regexp)
2064 (make-local-variable 'gud-delete-prompt-marker)
2065 (setq gud-delete-prompt-marker (make-marker))
2066 (run-hooks 'gud-mode-hook)
2067)
2068
2069(defvar gud-comint-buffer nil)
2070
2071;; Chop STRING into words separated by SPC or TAB and return a list of them.
2072(defun gud-chop-words (string)
2073 (let ((i 0) (beg 0)
2074 (len (length string))
2075 (words nil))
2076 (while (< i len)
2077 (if (memq (aref string i) '(?\t ? ))
2078 (progn
2079 (setq words (cons (substring string beg i) words)
2080 beg (1+ i))
2081 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2082 (setq beg (1+ beg)))
2083 (setq i (1+ beg)))
2084 (setq i (1+ i))))
2085 (if (< beg len)
2086 (setq words (cons (substring string beg) words)))
2087 (nreverse words)))
2088
2089(defvar gud-target-name "--unknown--"
2090 "The apparent name of the program being debugged in a gud buffer.
2091For sure this the root string used in smashing together the gud
2092buffer's name, even if that doesn't happen to be the name of a
2093program.")
2094
2095;; Perform initializations common to all debuggers.
2096(defun gud-common-init (command-line)
2097 (let* ((words (gud-chop-words command-line))
2098 (program (car words))
2099 (file-word (let ((w (cdr words)))
2100 (while (and w (= ?- (aref (car w) 0)))
2101 (setq w (cdr w)))
2102 (car w)))
2103 (args (delq file-word (cdr words)))
2104 (file (expand-file-name file-word))
2105 (filepart (file-name-nondirectory file))
2106 (buffer-name (concat "*gud-" filepart "*")))
2107 (switch-to-buffer buffer-name)
2108 (setq default-directory (file-name-directory file))
2109 (or (bolp) (newline))
2110 (insert "Current directory is " default-directory "\n")
e91c1816
TL
2111 (let ((old-instance gdb-buffer-instance))
2112 (apply 'make-comint (concat "gud-" filepart) program nil
2113 (gud-massage-args file args))
2114 (gud-mode)
2115 (make-variable-buffer-local 'old-gdb-buffer-instance)
2116 (setq old-gdb-buffer-instance old-instance))
c9d4b5ef
JK
2117 (make-variable-buffer-local 'gud-target-name)
2118 (setq gud-target-name filepart))
2119 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2120 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2121 (gud-set-buffer)
2122 )
2123
2124(defun gud-set-buffer ()
2125 (cond ((eq major-mode 'gud-mode)
2126 (setq gud-comint-buffer (current-buffer)))))
2127
2128;; These functions are responsible for inserting output from your debugger
2129;; into the buffer. The hard work is done by the method that is
2130;; the value of gud-marker-filter.
2131
2132(defun gud-filter (proc string)
2133 ;; Here's where the actual buffer insertion is done
2134 (let ((inhibit-quit t))
2135 (save-excursion
2136 (set-buffer (process-buffer proc))
2137 (let (moving output-after-point)
2138 (save-excursion
2139 (goto-char (process-mark proc))
2140 ;; If we have been so requested, delete the debugger prompt.
2141 (if (marker-buffer gud-delete-prompt-marker)
2142 (progn
2143 (delete-region (point) gud-delete-prompt-marker)
2144 (set-marker gud-delete-prompt-marker nil)))
2145 (insert-before-markers (gud-marker-filter string))
2146 (setq moving (= (point) (process-mark proc)))
2147 (setq output-after-point (< (point) (process-mark proc)))
2148 ;; Check for a filename-and-line number.
2149 ;; Don't display the specified file
2150 ;; unless (1) point is at or after the position where output appears
2151 ;; and (2) this buffer is on the screen.
2152 (if (and gud-last-frame
2153 (not output-after-point)
2154 (get-buffer-window (current-buffer)))
2155 (gud-display-frame)))
2156 (if moving (goto-char (process-mark proc)))))))
2157
2158(defun gud-sentinel (proc msg)
2159 (cond ((null (buffer-name (process-buffer proc)))
2160 ;; buffer killed
2161 ;; Stop displaying an arrow in a source file.
2162 (setq overlay-arrow-position nil)
2163 (set-process-buffer proc nil))
2164 ((memq (process-status proc) '(signal exit))
2165 ;; Stop displaying an arrow in a source file.
2166 (setq overlay-arrow-position nil)
2167 ;; Fix the mode line.
2168 (setq mode-line-process
2169 (concat ": "
2170 (symbol-name (process-status proc))))
2171 (let* ((obuf (current-buffer)))
2172 ;; save-excursion isn't the right thing if
2173 ;; process-buffer is current-buffer
2174 (unwind-protect
2175 (progn
2176 ;; Write something in *compilation* and hack its mode line,
2177 (set-buffer (process-buffer proc))
2178 ;; Force mode line redisplay soon
2179 (set-buffer-modified-p (buffer-modified-p))
2180 (if (eobp)
2181 (insert ?\n mode-name " " msg)
2182 (save-excursion
2183 (goto-char (point-max))
2184 (insert ?\n mode-name " " msg)))
2185 ;; If buffer and mode line will show that the process
2186 ;; is dead, we can delete it now. Otherwise it
2187 ;; will stay around until M-x list-processes.
2188 (delete-process proc))
2189 ;; Restore old buffer, but don't restore old point
2190 ;; if obuf is the gud buffer.
2191 (set-buffer obuf))))))
2192
2193(defun gud-display-frame ()
2194 "Find and obey the last filename-and-line marker from the debugger.
2195Obeying it means displaying in another window the specified file and line."
2196 (interactive)
2197 (if gud-last-frame
2198 (progn
e91c1816 2199; (gud-set-buffer)
c9d4b5ef
JK
2200 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2201 (setq gud-last-last-frame gud-last-frame
2202 gud-last-frame nil))))
2203
2204;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2205;; and that its line LINE is visible.
2206;; Put the overlay-arrow on the line LINE in that buffer.
2207;; Most of the trickiness in here comes from wanting to preserve the current
2208;; region-restriction if that's possible. We use an explicit display-buffer
2209;; to get around the fact that this is called inside a save-excursion.
2210
2211(defun gud-display-line (true-file line)
2212 (let* ((buffer (gud-find-file true-file))
e91c1816 2213 (window (gud-display-source-buffer buffer))
c9d4b5ef 2214 (pos))
e91c1816
TL
2215 (if (not window)
2216 (error "foo bar baz"))
c9d4b5ef
JK
2217;;; (if (equal buffer (current-buffer))
2218;;; nil
2219;;; (setq buffer-read-only nil))
2220 (save-excursion
2221;;; (setq buffer-read-only t)
2222 (set-buffer buffer)
2223 (save-restriction
2224 (widen)
2225 (goto-line line)
2226 (setq pos (point))
2227 (setq overlay-arrow-string "=>")
2228 (or overlay-arrow-position
2229 (setq overlay-arrow-position (make-marker)))
2230 (set-marker overlay-arrow-position (point) (current-buffer)))
2231 (cond ((or (< pos (point-min)) (> pos (point-max)))
2232 (widen)
2233 (goto-char pos))))
2234 (set-window-point window overlay-arrow-position)))
2235
2236;;; The gud-call function must do the right thing whether its invoking
2237;;; keystroke is from the GUD buffer itself (via major-mode binding)
2238;;; or a C buffer. In the former case, we want to supply data from
2239;;; gud-last-frame. Here's how we do it:
2240
2241(defun gud-format-command (str arg)
2242 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
2243 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
2244 (setq str (concat
2245 (substring str (match-beginning 1) (match-end 1))
2246 (file-name-nondirectory (if insource
2247 (buffer-file-name)
2248 (car gud-last-frame)))
2249 (substring str (match-beginning 2) (match-end 2)))))
2250 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
2251 (setq str (concat
2252 (substring str (match-beginning 1) (match-end 1))
2253 (file-name-directory (if insource
2254 (buffer-file-name)
2255 (car gud-last-frame)))
2256 (substring str (match-beginning 2) (match-end 2)))))
2257 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
2258 (setq str (concat
2259 (substring str (match-beginning 1) (match-end 1))
2260 (if insource
2261 (save-excursion
2262 (beginning-of-line)
2263 (save-restriction (widen)
2264 (1+ (count-lines 1 (point)))))
2265 (cdr gud-last-frame))
2266 (substring str (match-beginning 2) (match-end 2)))))
2267 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
2268 (setq str (concat
2269 (substring str (match-beginning 1) (match-end 1))
2270 (find-c-expr)
2271 (substring str (match-beginning 2) (match-end 2)))))
2272 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
2273 (setq str (concat
2274 (substring str (match-beginning 1) (match-end 1))
2275 (gud-read-address)
2276 (substring str (match-beginning 2) (match-end 2)))))
2277 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
2278 (setq str (concat
2279 (substring str (match-beginning 1) (match-end 1))
2280 (if arg (int-to-string arg) "")
2281 (substring str (match-beginning 2) (match-end 2)))))
2282 )
2283 str
2284 )
2285
2286(defun gud-read-address ()
2287 "Return a string containing the core-address found in the buffer at point."
2288 (save-excursion
2289 (let ((pt (point)) found begin)
2290 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2291 (cond
2292 (found (forward-char 2)
2293 (buffer-substring found
2294 (progn (re-search-forward "[^0-9a-f]")
2295 (forward-char -1)
2296 (point))))
2297 (t (setq begin (progn (re-search-backward "[^0-9]")
2298 (forward-char 1)
2299 (point)))
2300 (forward-char 1)
2301 (re-search-forward "[^0-9]")
2302 (forward-char -1)
2303 (buffer-substring begin (point)))))))
2304
2305(defun gud-call (fmt &optional arg)
2306 (let ((msg (gud-format-command fmt arg)))
2307 (message "Command: %s" msg)
2308 (sit-for 0)
2309 (gud-basic-call msg)))
2310
2311(defun gud-basic-call (command)
2312 "Invoke the debugger COMMAND displaying source in other window."
2313 (interactive)
2314 (gud-set-buffer)
e91c1816 2315 (let ((proc (get-buffer-process gud-comint-buffer)))
c9d4b5ef
JK
2316
2317 ;; Arrange for the current prompt to get deleted.
2318 (save-excursion
2319 (set-buffer gud-comint-buffer)
2320 (goto-char (process-mark proc))
2321 (beginning-of-line)
2322 (if (looking-at comint-prompt-regexp)
e91c1816
TL
2323 (set-marker gud-delete-prompt-marker (point)))
2324 (apply comint-input-sender (list proc command)))))
c9d4b5ef
JK
2325
2326(defun gud-refresh (&optional arg)
2327 "Fix up a possibly garbled display, and redraw the arrow."
2328 (interactive "P")
2329 (recenter arg)
2330 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2331 (gud-display-frame))
2332\f
2333;;; Code for parsing expressions out of C code. The single entry point is
2334;;; find-c-expr, which tries to return an lvalue expression from around point.
2335;;;
2336;;; The rest of this file is a hacked version of gdbsrc.el by
2337;;; Debby Ayers <ayers@asc.slb.com>,
2338;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2339
2340(defun find-c-expr ()
2341 "Returns the C expr that surrounds point."
2342 (interactive)
2343 (save-excursion
2344 (let ((p) (expr) (test-expr))
2345 (setq p (point))
2346 (setq expr (expr-cur))
2347 (setq test-expr (expr-prev))
2348 (while (expr-compound test-expr expr)
2349 (setq expr (cons (car test-expr) (cdr expr)))
2350 (goto-char (car expr))
2351 (setq test-expr (expr-prev)))
2352 (goto-char p)
2353 (setq test-expr (expr-next))
2354 (while (expr-compound expr test-expr)
2355 (setq expr (cons (car expr) (cdr test-expr)))
2356 (setq test-expr (expr-next))
2357 )
2358 (buffer-substring (car expr) (cdr expr)))))
2359
2360(defun expr-cur ()
2361 "Returns the expr that point is in; point is set to beginning of expr.
2362The expr is represented as a cons cell, where the car specifies the point in
2363the current buffer that marks the beginning of the expr and the cdr specifies
2364the character after the end of the expr."
2365 (let ((p (point)) (begin) (end))
2366 (expr-backward-sexp)
2367 (setq begin (point))
2368 (expr-forward-sexp)
2369 (setq end (point))
2370 (if (>= p end)
2371 (progn
2372 (setq begin p)
2373 (goto-char p)
2374 (expr-forward-sexp)
2375 (setq end (point))
2376 )
2377 )
2378 (goto-char begin)
2379 (cons begin end)))
2380
2381(defun expr-backward-sexp ()
2382 "Version of `backward-sexp' that catches errors."
2383 (condition-case nil
2384 (backward-sexp)
2385 (error t)))
2386
2387(defun expr-forward-sexp ()
2388 "Version of `forward-sexp' that catches errors."
2389 (condition-case nil
2390 (forward-sexp)
2391 (error t)))
2392
2393(defun expr-prev ()
2394 "Returns the previous expr, point is set to beginning of that expr.
2395The expr is represented as a cons cell, where the car specifies the point in
2396the current buffer that marks the beginning of the expr and the cdr specifies
2397the character after the end of the expr"
2398 (let ((begin) (end))
2399 (expr-backward-sexp)
2400 (setq begin (point))
2401 (expr-forward-sexp)
2402 (setq end (point))
2403 (goto-char begin)
2404 (cons begin end)))
2405
2406(defun expr-next ()
2407 "Returns the following expr, point is set to beginning of that expr.
2408The expr is represented as a cons cell, where the car specifies the point in
2409the current buffer that marks the beginning of the expr and the cdr specifies
2410the character after the end of the expr."
2411 (let ((begin) (end))
2412 (expr-forward-sexp)
2413 (expr-forward-sexp)
2414 (setq end (point))
2415 (expr-backward-sexp)
2416 (setq begin (point))
2417 (cons begin end)))
2418
2419(defun expr-compound-sep (span-start span-end)
2420 "Returns '.' for '->' & '.', returns ' ' for white space,
2421returns '?' for other punctuation."
2422 (let ((result ? )
2423 (syntax))
2424 (while (< span-start span-end)
2425 (setq syntax (char-syntax (char-after span-start)))
2426 (cond
2427 ((= syntax ? ) t)
2428 ((= syntax ?.) (setq syntax (char-after span-start))
2429 (cond
2430 ((= syntax ?.) (setq result ?.))
2431 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2432 (setq result ?.)
2433 (setq span-start (+ span-start 1)))
2434 (t (setq span-start span-end)
2435 (setq result ??)))))
2436 (setq span-start (+ span-start 1)))
2437 result))
2438
2439(defun expr-compound (first second)
2440 "Non-nil if concatenating FIRST and SECOND makes a single C token.
2441The two exprs are represented as a cons cells, where the car
2442specifies the point in the current buffer that marks the beginning of the
2443expr and the cdr specifies the character after the end of the expr.
2444Link exprs of the form:
2445 Expr -> Expr
2446 Expr . Expr
2447 Expr (Expr)
2448 Expr [Expr]
2449 (Expr) Expr
2450 [Expr] Expr"
2451 (let ((span-start (cdr first))
2452 (span-end (car second))
2453 (syntax))
2454 (setq syntax (expr-compound-sep span-start span-end))
2455 (cond
2456 ((= (car first) (car second)) nil)
2457 ((= (cdr first) (cdr second)) nil)
2458 ((= syntax ?.) t)
2459 ((= syntax ? )
2460 (setq span-start (char-after (- span-start 1)))
2461 (setq span-end (char-after span-end))
2462 (cond
2463 ((= span-start ?) ) t )
2464 ((= span-start ?] ) t )
2465 ((= span-end ?( ) t )
2466 ((= span-end ?[ ) t )
2467 (t nil))
2468 )
2469 (t nil))))
2470
2471(provide 'gud)
2472
2473;;; gud.el ends here
This page took 0.120837 seconds and 4 git commands to generate.