1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nick@nick.uklinux.net>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This mode acts as a graphical user interface to GDB. You can interact with
29 ;; GDB through the GUD buffer in the usual way, but there are also further
30 ;; buffers which control the execution and describe the state of your program.
31 ;; It separates the input/output of your program from that of GDB and displays
32 ;; expressions and their current values in their own buffers. It also uses
33 ;; features of Emacs 21 such as the display margin for breakpoints, and the
34 ;; toolbar (see the GDB Graphical Interface section in the Emacs info manual).
36 ;; Start the debugger with M-x gdba.
38 ;; This file is based on gdba.el from GDB 5.0 written by Tom Lord and Jim
39 ;; Kingdon and uses GDB's annotation interface. You don't need to know about
40 ;; annotations to use this mode as a debugger, but if you are interested
41 ;; developing the mode itself, then see the Annotations section in the GDB
45 ;; Does not auto-display arrays of structures or structures containing arrays.
46 ;; On MS Windows, Gdb 5.1.1 from MinGW 2.0 does not flush the output from the
53 (defcustom gdb-window-height
20
54 "Number of lines in a frame for a displayed expression in GDB-UI."
58 (defcustom gdb-window-width
30
59 "Width of a frame for a displayed expression in GDB-UI."
63 (defvar gdb-current-address
"main" "Initialisation for Assembler buffer.")
64 (defvar gdb-previous-address nil
)
65 (defvar gdb-previous-frame nil
)
66 (defvar gdb-current-frame
"main")
67 (defvar gdb-display-in-progress nil
)
69 (defvar gdb-view-source t
"Non-nil means that source code can be viewed")
70 (defvar gdb-selected-view
'source
"Code type that user wishes to view")
71 (defvar gdb-buffer-type nil
)
72 (defvar gdb-variables
'()
73 "A list of variables that are local to the GUD buffer.")
77 (defun gdba (command-line)
78 "Run gdb on program FILE in buffer *gud-FILE*.
79 The directory containing FILE becomes the initial working directory
80 and source-file directory for your debugger.
82 If `gdb-many-windows' is nil (the default value) then gdb starts with
83 just two windows : the GUD and the source buffer. If it is t the
84 following layout will appear (keybindings given in relevant buffer) :
86 ---------------------------------------------------------------------
88 ---------------------------------------------------------------------
89 GUD buffer (I/O of GDB) | Locals buffer
93 ---------------------------------------------------------------------
94 Source buffer | Input/Output (of debuggee) buffer
102 ---------------------------------------------------------------------
103 Stack buffer | Breakpoints buffer
104 RET gdb-frames-select | SPC gdb-toggle-breakpoint
105 | RET gdb-goto-breakpoint
106 | d gdb-delete-breakpoint
107 ---------------------------------------------------------------------
109 All the buffers share the toolbar and source should always display in the same
110 window e.g after typing g on a breakpoint in the breakpoints buffer. Breakpoint
111 icons are displayed both by setting a break with gud-break and by typing break
114 This works best (depending on the size of your monitor) using most of the
117 Displayed expressions appear in separate frames. Arrays may be displayed
118 as slices and visualised using the graph program from plotutils if installed.
119 Pointers in structures may be followed in a tree-like fashion.
121 The following interactive lisp functions help control operation :
123 `gdb-many-windows' - Toggle the number of windows gdb uses.
124 `gdb-restore-windows' - To restore the window layout."
126 (interactive (list (gud-query-cmdline 'gdba
)))
128 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
131 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
132 (set (make-local-variable 'gud-marker-filter
) 'gud-gdba-marker-filter
)
134 (gud-def gud-break
(if (not (string-equal mode-name
"Assembler"))
135 (gud-call "break %f:%l" arg
)
139 (gud-call "break *%a" arg
)))
140 "\C-b" "Set breakpoint at current line or address.")
142 (gud-def gud-remove
(if (not (string-equal mode-name
"Assembler"))
143 (gud-call "clear %f:%l" arg
)
147 (gud-call "clear *%a" arg
)))
148 "\C-d" "Remove breakpoint at current line or address.")
150 (gud-def gud-until
(if (not (string-equal mode-name
"Assembler"))
151 (gud-call "until %f:%l" arg
)
155 (gud-call "until *%a" arg
)))
156 "\C-u" "Continue to current line or address.")
158 (setq comint-input-sender
'gdb-send
)
161 (setq gdb-current-address
"main")
162 (setq gdb-previous-address nil
)
163 (setq gdb-previous-frame nil
)
164 (setq gdb-current-frame
"main")
165 (setq gdb-display-in-progress nil
)
167 (setq gdb-view-source t
)
168 (setq gdb-selected-view
'source
)
170 (mapc 'make-local-variable gdb-variables
)
171 (setq gdb-buffer-type
'gdba
)
173 (gdb-clear-inferior-io)
175 (if (eq window-system
'w32
)
176 (gdb-enqueue-input (list "set new-console off\n" 'ignore
)))
177 (gdb-enqueue-input (list "set height 0\n" 'ignore
))
178 ;; find source file and compilation directory here
179 (gdb-enqueue-input (list "server list main\n" 'ignore
)) ; C program
180 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore
)) ; Fortran program
181 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info
))
183 (run-hooks 'gdba-mode-hook
))
185 (defun gud-display ()
186 "Auto-display (possibly dereferenced) C expression at point."
189 (let ((expr (gud-find-c-expr)))
191 (list (concat "server ptype " expr
"\n")
192 `(lambda () (gud-display1 ,expr
)))))))
194 (defun gud-display1 (expr)
195 (goto-char (point-min))
196 (if (looking-at "No symbol")
198 (gdb-set-output-sink 'user
)
199 (gud-call (concat "server ptype " expr
)))
200 (goto-char (- (point-max) 1))
201 (if (equal (char-before) (string-to-char "\*"))
203 (list (concat "display* " expr
"\n") 'ignore
))
205 (list (concat "display " expr
"\n") 'ignore
)))))
207 ; this would messy because these bindings don't work with M-x gdb
208 ; (define-key global-map "\C-x\C-a\C-a" 'gud-display)
209 ; (define-key gud-minor-mode-map "\C-c\C-a" 'gud-display)
213 ;; ======================================================================
215 ;; In this world, there are gdb variables (of unspecified
216 ;; representation) and buffers associated with those objects.
217 ;; The list of variables is built up by the expansions of
220 (defmacro def-gdb-var
(root-symbol &optional default doc
)
221 (let* ((root (symbol-name root-symbol
))
222 (accessor (intern (concat "gdb-get-" root
)))
223 (setter (intern (concat "gdb-set-" root
)))
224 (name (intern (concat "gdb-" root
))))
226 (defvar ,name
,default
,doc
)
227 (if (not (memq ',name gdb-variables
))
228 (push ',name gdb-variables
))
230 (buffer-local-value ',name gud-comint-buffer
))
232 (with-current-buffer gud-comint-buffer
233 (setq ,name val
))))))
235 (def-gdb-var buffer-type nil
236 "One of the symbols bound in gdb-buffer-rules")
238 (def-gdb-var burst
""
239 "A string of characters from gdb that have not yet been processed.")
241 (def-gdb-var input-queue
()
242 "A list of high priority gdb command objects.")
244 (def-gdb-var idle-input-queue
()
245 "A list of low priority gdb command objects.")
247 (def-gdb-var prompting nil
248 "True when gdb is idle with no pending input.")
250 (def-gdb-var output-sink
'user
251 "The disposition of the output of the current gdb command.
252 Possible values are these symbols:
254 user -- gdb output should be copied to the GUD buffer
257 inferior -- gdb output should be copied to the inferior-io buffer
259 pre-emacs -- output should be ignored util the post-prompt
260 annotation is received. Then the output-sink
262 emacs -- output should be collected in the partial-output-buffer
263 for subsequent processing by a command. This is the
264 disposition of output generated by commands that
265 gdb mode sends to gdb on its own behalf.
266 post-emacs -- ignore input until the prompt annotation is
267 received, then go to USER disposition.
270 (def-gdb-var current-item nil
271 "The most recent command item sent to gdb.")
273 (def-gdb-var pending-triggers
'()
274 "A list of trigger functions that have run later than their output
277 ;; end of gdb variables
279 (defun gdb-get-target-string ()
280 (with-current-buffer gud-comint-buffer
287 ;; Each buffer has a TYPE -- a symbol that identifies the function
288 ;; of that particular buffer.
290 ;; The usual gdb interaction buffer is given the type `gdba' and
291 ;; is constructed specially.
293 ;; Others are constructed by gdb-get-create-buffer and
294 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
296 (defvar gdb-buffer-rules-assoc
'())
298 (defun gdb-get-buffer (key)
299 "Return the gdb buffer tagged with type KEY.
300 The key should be one of the cars in `gdb-buffer-rules-assoc'."
302 (gdb-look-for-tagged-buffer key
(buffer-list))))
304 (defun gdb-get-create-buffer (key)
305 "Create a new gdb buffer of the type specified by KEY.
306 The key should be one of the cars in `gdb-buffer-rules-assoc'."
307 (or (gdb-get-buffer key
)
308 (let* ((rules (assoc key gdb-buffer-rules-assoc
))
309 (name (funcall (gdb-rules-name-maker rules
)))
310 (new (get-buffer-create name
)))
311 (with-current-buffer new
312 ;; FIXME: This should be set after calling the function, since the
313 ;; function should run kill-all-local-variables.
314 (set (make-local-variable 'gdb-buffer-type
) key
)
315 (if (cdr (cdr rules
))
316 (funcall (car (cdr (cdr rules
)))))
317 (set (make-local-variable 'gud-comint-buffer
) gud-comint-buffer
)
318 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
319 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
322 (defun gdb-rules-name-maker (rules) (car (cdr rules
)))
324 (defun gdb-look-for-tagged-buffer (key bufs
)
326 (while (and (not retval
) bufs
)
327 (set-buffer (car bufs
))
328 (if (eq gdb-buffer-type key
)
329 (setq retval
(car bufs
)))
330 (setq bufs
(cdr bufs
)))
334 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
335 ;; at least one and possible more functions. The functions have these
336 ;; roles in defining a buffer type:
338 ;; NAME - Return a name for this buffer type.
340 ;; The remaining function(s) are optional:
342 ;; MODE - called in a new buffer with no arguments, should establish
343 ;; the proper mode for the buffer.
346 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
347 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc
)))
349 (setcdr binding rules
)
350 (push (cons buffer-type rules
)
351 gdb-buffer-rules-assoc
))))
353 ;; GUD buffers are an exception to the rules
354 (gdb-set-buffer-rules 'gdba
'error
)
357 ;; Partial-output buffer : This accumulates output from a command executed on
358 ;; behalf of emacs (rather than the user).
360 (gdb-set-buffer-rules 'gdb-partial-output-buffer
361 'gdb-partial-output-name
)
363 (defun gdb-partial-output-name ()
364 (concat "*partial-output-"
365 (gdb-get-target-string)
369 (gdb-set-buffer-rules 'gdb-inferior-io
370 'gdb-inferior-io-name
371 'gdb-inferior-io-mode
)
373 (defun gdb-inferior-io-name ()
374 (concat "*input/output of "
375 (gdb-get-target-string)
378 (defvar gdb-inferior-io-mode-map
379 (let ((map (make-sparse-keymap)))
380 (define-key map
"\C-c\C-c" 'gdb-inferior-io-interrupt
)
381 (define-key map
"\C-c\C-z" 'gdb-inferior-io-stop
)
382 (define-key map
"\C-c\C-\\" 'gdb-inferior-io-quit
)
383 (define-key map
"\C-c\C-d" 'gdb-inferior-io-eof
)
386 (define-derived-mode gdb-inferior-io-mode comint-mode
"Debuggee I/O"
387 "Major mode for gdb inferior-io."
388 :syntax-table nil
:abbrev-table nil
389 ;; We want to use comint because it has various nifty and familiar
390 ;; features. We don't need a process, but comint wants one, so create
392 (make-comint-in-buffer
393 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
394 (current-buffer) "hexl")
395 (setq comint-input-sender
'gdb-inferior-io-sender
))
397 (defun gdb-inferior-io-sender (proc string
)
398 ;; PROC is the pseudo-process created to satisfy comint.
399 (with-current-buffer (process-buffer proc
)
400 (setq proc
(get-buffer-process gud-comint-buffer
))
401 (process-send-string proc string
)
402 (process-send-string proc
"\n")))
404 (defun gdb-inferior-io-interrupt ()
405 "Interrupt the program being debugged."
408 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
410 (defun gdb-inferior-io-quit ()
411 "Send quit signal to the program being debugged."
414 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
416 (defun gdb-inferior-io-stop ()
417 "Stop the program being debugged."
420 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
422 (defun gdb-inferior-io-eof ()
423 "Send end-of-file to the program being debugged."
426 (get-buffer-process gud-comint-buffer
)))
430 ;; gdb communications
433 ;; INPUT: things sent to gdb
435 ;; There is a high and low priority input queue. Low priority input is sent
436 ;; only when the high priority queue is idle.
438 ;; The queues are lists. Each element is either a string (indicating user or
439 ;; user-like input) or a list of the form:
441 ;; (INPUT-STRING HANDLER-FN)
443 ;; The handler function will be called from the partial-output buffer when the
444 ;; command completes. This is the way to write commands which invoke gdb
445 ;; commands autonomously.
447 ;; These lists are consumed tail first.
450 (defun gdb-send (proc string
)
451 "A comint send filter for gdb.
452 This filter may simply queue output for a later time."
453 (gdb-enqueue-input (concat string
"\n")))
455 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
456 ;; is a query, or other non-top-level prompt. To guarantee stuff will get
457 ;; sent to the top-level prompt, currently it must be put in the idle queue.
459 ;; [This should encourage gdb extensions that invoke gdb commands to let
460 ;; the user go first; it is not a bug. -t]
463 (defun gdb-enqueue-input (item)
464 (if (gdb-get-prompting)
467 (gdb-set-prompting nil
))
469 (cons item
(gdb-get-input-queue)))))
471 (defun gdb-dequeue-input ()
472 (let ((queue (gdb-get-input-queue)))
474 (if (not (cdr queue
))
475 (let ((answer (car queue
)))
476 (gdb-set-input-queue '())
478 (gdb-take-last-elt queue
)))))
480 (defun gdb-enqueue-idle-input (item)
481 (if (and (gdb-get-prompting)
482 (not (gdb-get-input-queue)))
485 (gdb-set-prompting nil
))
486 (gdb-set-idle-input-queue
487 (cons item
(gdb-get-idle-input-queue)))))
489 (defun gdb-dequeue-idle-input ()
490 (let ((queue (gdb-get-idle-input-queue)))
492 (if (not (cdr queue
))
493 (let ((answer (car queue
)))
494 (gdb-set-idle-input-queue '())
496 (gdb-take-last-elt queue
)))))
498 ;; Don't use this in general.
499 (defun gdb-take-last-elt (l)
501 (gdb-take-last-elt (cdr l
))
502 (let ((answer (car (cdr l
))))
508 ;; output -- things gdb prints to emacs
510 ;; GDB output is a stream interrupted by annotations.
511 ;; Annotations can be recognized by their beginning
512 ;; with \C-j\C-z\C-z<tag><opt>\C-j
514 ;; The tag is a string obeying symbol syntax.
516 ;; The optional part `<opt>' can be either the empty string
517 ;; or a space followed by more data relating to the annotation.
518 ;; For example, the SOURCE annotation is followed by a filename,
519 ;; line number and various useless goo. This data must not include
523 (defcustom gud-gdba-command-name
"gdb -annotate=2 -noasync"
524 "Default command to execute an executable under the GDB-UI debugger."
528 (defvar gdb-annotation-rules
529 '(("pre-prompt" gdb-pre-prompt
)
530 ("prompt" gdb-prompt
)
531 ("commands" gdb-subprompt
)
532 ("overload-choice" gdb-subprompt
)
533 ("query" gdb-subprompt
)
534 ("prompt-for-continue" gdb-subprompt
)
535 ("post-prompt" gdb-post-prompt
)
536 ("source" gdb-source
)
537 ("starting" gdb-starting
)
538 ("exited" gdb-stopping
)
539 ("signalled" gdb-stopping
)
540 ("signal" gdb-stopping
)
541 ("breakpoint" gdb-stopping
)
542 ("watchpoint" gdb-stopping
)
543 ("frame-begin" gdb-frame-begin
)
544 ("stopped" gdb-stopped
)
545 ("display-begin" gdb-display-begin
)
546 ("display-end" gdb-display-end
)
547 ; GDB commands info stack, info locals and frame generate an error-begin
548 ; annotation at start when there is no stack but this is a quirk/bug in
550 ; ("error-begin" gdb-error-begin)
551 ("display-number-end" gdb-display-number-end
)
552 ("array-section-begin" gdb-array-section-begin
)
553 ("array-section-end" gdb-array-section-end
)
555 ("field-begin" gdb-field-begin
)
556 ("field-end" gdb-field-end
)
557 ) "An assoc mapping annotation tags to functions which process them.")
559 (defconst gdb-source-spec-regexp
560 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
562 ;; Do not use this except as an annotation handler.
563 (defun gdb-source (args)
564 (string-match gdb-source-spec-regexp args
)
565 ;; Extract the frame position from the marker.
568 (match-string 1 args
)
569 (string-to-int (match-string 2 args
))))
570 (setq gdb-current-address
(match-string 3 args
))
571 (setq gdb-view-source t
))
573 (defun gdb-send-item (item)
574 (gdb-set-current-item item
)
577 (gdb-set-output-sink 'user
)
578 (process-send-string (get-buffer-process gud-comint-buffer
) item
))
580 (gdb-clear-partial-output)
581 (gdb-set-output-sink 'pre-emacs
)
582 (process-send-string (get-buffer-process gud-comint-buffer
)
585 (defun gdb-pre-prompt (ignored)
586 "An annotation handler for `pre-prompt'. This terminates the collection of
587 output from a previous command if that happens to be in effect."
588 (let ((sink (gdb-get-output-sink)))
592 (gdb-set-output-sink 'post-emacs
)
594 (car (cdr (gdb-get-current-item)))))
595 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
598 (gdb-set-output-sink 'user
)
599 (error "Phase error in gdb-pre-prompt (got %s)" sink
)))))
601 (defun gdb-prompt (ignored)
602 "An annotation handler for `prompt'.
603 This sends the next command (if any) to gdb."
604 (let ((sink (gdb-get-output-sink)))
607 ((eq sink
'post-emacs
)
608 (gdb-set-output-sink 'user
))
610 (gdb-set-output-sink 'user
)
611 (error "Phase error in gdb-prompt (got %s)" sink
))))
612 (let ((highest (gdb-dequeue-input)))
614 (gdb-send-item highest
)
615 (let ((lowest (gdb-dequeue-idle-input)))
617 (gdb-send-item lowest
)
619 (gdb-set-prompting t
)
620 (gud-display-frame)))))))
622 (defun gdb-subprompt (ignored)
623 "An annotation handler for non-top-level prompts."
624 (let ((highest (gdb-dequeue-input)))
626 (gdb-send-item highest
)
627 (gdb-set-prompting t
))))
629 (defun gdb-starting (ignored)
630 "An annotation handler for `starting'. This says that I/O for the
631 subprocess is now the program being debugged, not GDB."
632 (let ((sink (gdb-get-output-sink)))
637 (gdb-set-output-sink 'inferior
)))
638 (t (error "Unexpected `starting' annotation")))))
640 (defun gdb-stopping (ignored)
641 "An annotation handler for `exited' and other annotations which say that I/O
642 for the subprocess is now GDB, not the program being debugged."
643 (let ((sink (gdb-get-output-sink)))
646 (gdb-set-output-sink 'user
))
647 (t (error "Unexpected stopping annotation")))))
649 (defun gdb-frame-begin (ignored)
650 (let ((sink (gdb-get-output-sink)))
653 (gdb-set-output-sink 'user
))
656 (t (error "Unexpected frame-begin annotation (%S)" sink
)))))
658 (defun gdb-stopped (ignored)
659 "An annotation handler for `stopped'. It is just like gdb-stopping, except
660 that if we already set the output sink to 'user in gdb-stopping, that is fine."
661 (setq gud-running nil
)
662 (let ((sink (gdb-get-output-sink)))
665 (gdb-set-output-sink 'user
))
667 (t (error "Unexpected stopped annotation")))))
669 (defun gdb-post-prompt (ignored)
670 "An annotation handler for `post-prompt'. This begins the collection of
671 output from the current command if that happens to be appropriate."
672 (if (not (gdb-get-pending-triggers))
674 (gdb-get-current-frame)
675 (gdb-invalidate-frames)
676 (gdb-invalidate-breakpoints)
677 (gdb-invalidate-assembler)
678 (gdb-invalidate-registers)
679 (gdb-invalidate-locals)
680 (gdb-invalidate-display)
681 (gdb-invalidate-threads)))
682 (let ((sink (gdb-get-output-sink)))
685 ((eq sink
'pre-emacs
)
686 (gdb-set-output-sink 'emacs
))
688 (gdb-set-output-sink 'user
)
689 (error "Phase error in gdb-post-prompt (got %s)" sink
)))))
691 ;; If we get an error whilst evaluating one of the expressions
692 ;; we won't get the display-end annotation. Set the sink back to
693 ;; user to make sure that the error message is seen.
694 ;; NOT USED: see annotation-rules for reason.
695 ;(defun gdb-error-begin (ignored)
696 ; (gdb-set-output-sink 'user))
698 (defun gdb-display-begin (ignored)
699 (gdb-set-output-sink 'emacs
)
700 (gdb-clear-partial-output)
701 (setq gdb-display-in-progress t
))
703 (defvar gdb-expression-buffer-name nil
)
704 (defvar gdb-display-number nil
)
705 (defvar gdb-dive-display-number nil
)
707 (defun gdb-display-number-end (ignored)
708 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer
))
709 (setq gdb-display-number
(buffer-string))
710 (setq gdb-expression-buffer-name
711 (concat "*display " gdb-display-number
"*"))
714 (set-buffer (window-buffer))
717 (let ((number gdb-display-number
))
719 (set-buffer (get-buffer-create gdb-expression-buffer-name
)))
720 (gdb-expressions-mode)
721 (setq gdb-dive-display-number number
)))
722 (set-buffer (get-buffer-create gdb-expression-buffer-name
))
723 (if (display-graphic-p)
725 (dolist (frame (frame-list))
726 (if (string-equal (frame-parameter frame
'name
)
727 gdb-expression-buffer-name
)
728 (throw 'frame-exists nil
)))
729 (gdb-expressions-mode)
730 (make-frame `((height .
,gdb-window-height
)
731 (width .
,gdb-window-width
)
732 (tool-bar-lines . nil
)
733 (menu-bar-lines . nil
)
734 (minibuffer . nil
))))
735 (gdb-expressions-mode)
736 (gdb-display-buffer (get-buffer gdb-expression-buffer-name
)))))
737 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer
))
740 (defvar gdb-nesting-level nil
)
741 (defvar gdb-expression nil
)
742 (defvar gdb-point nil
)
743 (defvar gdb-annotation-arg nil
)
745 (defun gdb-delete-line ()
746 "Delete the current line."
747 (delete-region (line-beginning-position) (line-beginning-position 2)))
749 (defun gdb-display-end (ignored)
750 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer
))
751 (goto-char (point-min))
752 (search-forward ": ")
753 (looking-at "\\(.*?\\) =")
755 (gdb-temp-value (match-string 1)))
756 ;;move * to front of expression if necessary
757 (if (looking-at ".*\\*")
760 (setq gdb-temp-value
(substring gdb-temp-value
1 nil
))))
761 (with-current-buffer gdb-expression-buffer-name
762 (setq gdb-expression gdb-temp-value
)
763 (if (not (string-match "::" gdb-expression
))
764 (setq gdb-expression
(concat char gdb-current-frame
765 "::" gdb-expression
))
766 ;;else put * back on if necessary
767 (setq gdb-expression
(concat char gdb-expression
)))
768 (if (not header-line-format
)
769 (setq header-line-format
(concat "-- " gdb-expression
" %-")))))
772 (if (not (re-search-forward "##" nil t
))
774 (with-current-buffer gdb-expression-buffer-name
775 (let ((buffer-read-only nil
))
776 (delete-region (point-min) (point-max))
777 (insert-buffer-substring
778 (gdb-get-buffer 'gdb-partial-output-buffer
)))))
779 ;; display expression name...
780 (goto-char (point-min))
781 (let ((start (progn (point)))
782 (end (progn (end-of-line) (point))))
783 (with-current-buffer gdb-expression-buffer-name
784 (let ((buffer-read-only nil
))
785 (delete-region (point-min) (point-max))
786 (insert-buffer-substring (gdb-get-buffer
787 'gdb-partial-output-buffer
)
790 (goto-char (point-min))
791 (re-search-forward "##" nil t
)
792 (setq gdb-nesting-level
0)
793 (if (looking-at "array-section-begin")
796 (setq gdb-point
(point))
798 (if (looking-at "field-begin \\(.\\)")
800 (setq gdb-annotation-arg
(match-string 1))
801 (gdb-field-format-begin))))
802 (with-current-buffer gdb-expression-buffer-name
803 (if gdb-dive-display-number
805 (let ((buffer-read-only nil
))
806 (goto-char (point-max))
808 (insert-text-button "[back]" 'type
'gdb-display-back
)))))
809 (gdb-clear-partial-output)
810 (gdb-set-output-sink 'user
)
811 (setq gdb-display-in-progress nil
))
813 (define-button-type 'gdb-display-back
814 'help-echo
"mouse-2, RET: go back to previous display buffer"
815 'action
(lambda (button) (gdb-display-go-back)))
817 (defun gdb-display-go-back ()
818 ;; delete display so they don't accumulate and delete buffer
819 (let ((number gdb-display-number
))
821 (list (concat "server delete display " number
"\n") 'ignore
))
822 (switch-to-buffer (concat "*display " gdb-dive-display-number
"*"))
823 (kill-buffer (get-buffer (concat "*display " number
"*")))))
825 ;; prefix annotations with ## and process whole output in one chunk
826 ;; in gdb-partial-output-buffer (to allow recursion).
828 ;; array-section flags are just removed again but after counting. They
829 ;; might also be useful for arrays of structures and structures with arrays.
830 (defun gdb-array-section-begin (args)
831 (if gdb-display-in-progress
833 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
834 (goto-char (point-max))
835 (insert (concat "\n##array-section-begin " args
"\n"))))))
837 (defun gdb-array-section-end (ignored)
838 (if gdb-display-in-progress
840 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
841 (goto-char (point-max))
842 (insert "\n##array-section-end\n")))))
844 (defun gdb-field-begin (args)
845 (if gdb-display-in-progress
847 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
848 (goto-char (point-max))
849 (insert (concat "\n##field-begin " args
"\n"))))))
851 (defun gdb-field-end (ignored)
852 (if gdb-display-in-progress
854 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer
)
855 (goto-char (point-max))
856 (insert "\n##field-end\n")))))
858 (defun gdb-elt (ignored)
859 (if gdb-display-in-progress
861 (goto-char (point-max))
862 (insert "\n##elt\n"))))
864 (defun gdb-field-format-begin ()
865 ;; get rid of ##field-begin
868 (setq gdb-nesting-level
(+ gdb-nesting-level
1))
869 (while (re-search-forward "##" nil t
)
870 ;; keep making recursive calls...
871 (if (looking-at "field-begin \\(.\\)")
873 (setq gdb-annotation-arg
(match-string 1))
874 (gdb-field-format-begin)))
876 (if (looking-at "field-end") (gdb-field-format-end))))
878 (defun gdb-field-format-end ()
879 ;; get rid of ##field-end and `,' or `}'
882 (setq gdb-nesting-level
(- gdb-nesting-level
1)))
885 (let ((map (make-sparse-keymap)))
886 (define-key map
[mouse-2
] 'gdb-dive
)
887 (define-key map
[S-mouse-2
] 'gdb-dive-new-frame
)
890 (defun gdb-dive (event)
891 "Dive into structure."
894 (gdb-dive-new-frame event
))
896 (defun gdb-dive-new-frame (event)
897 "Dive into structure and display in a new frame."
900 (mouse-set-point event
)
901 (let ((point (point)) (gdb-full-expression gdb-expression
)
902 (end (progn (end-of-line) (point)))
903 (gdb-part-expression "") (gdb-last-field nil
) (gdb-display-char nil
))
905 (if (looking-at "\*") (setq gdb-display-char
"*"))
906 (re-search-forward "\\(\\S-+\\) = " end t
)
907 (setq gdb-last-field
(match-string-no-properties 1))
908 (goto-char (match-beginning 1))
909 (let ((last-column (current-column)))
910 (while (re-search-backward "\\s-\\(\\S-+\\) = {" nil t
)
911 (goto-char (match-beginning 1))
912 (if (and (< (current-column) last-column
)
913 (> (count-lines 1 (point)) 1))
915 (setq gdb-part-expression
916 (concat "." (match-string-no-properties 1)
917 gdb-part-expression
))
918 (setq last-column
(current-column))))))
919 ;; * not needed for components of a pointer to a structure in gdb
920 (if (string-equal "*" (substring gdb-full-expression
0 1))
921 (setq gdb-full-expression
(substring gdb-full-expression
1 nil
)))
922 (setq gdb-full-expression
923 (concat gdb-full-expression gdb-part-expression
"." gdb-last-field
))
925 (list (concat "server display" gdb-display-char
926 " " gdb-full-expression
"\n")
929 (defun gdb-insert-field ()
930 (let ((start (progn (point)))
931 (end (progn (next-line) (point)))
933 (with-current-buffer gdb-expression-buffer-name
934 (let ((buffer-read-only nil
))
935 (if (string-equal gdb-annotation-arg
"\*") (insert "\*"))
936 (while (<= num gdb-nesting-level
)
938 (setq num
(+ num
1)))
939 (insert-buffer-substring (gdb-get-buffer
940 'gdb-partial-output-buffer
)
943 (- (point) (- end start
)) (- (point) 1)
944 `(mouse-face highlight
945 local-map
,gdb-dive-map
946 help-echo
"mouse-2: dive, S-mouse-2: dive in a new frame"))))
947 (delete-region start end
)))
949 (defvar gdb-values nil
)
951 (defun gdb-array-format ()
952 (while (re-search-forward "##" nil t
)
953 ;; keep making recursive calls...
954 (if (looking-at "array-section-begin")
956 ;;get rid of ##array-section-begin
958 (setq gdb-nesting-level
(+ gdb-nesting-level
1))
960 ;;until *matching* array-section-end is found
961 (if (looking-at "array-section-end")
962 (if (eq gdb-nesting-level
0)
964 (let ((values (buffer-substring gdb-point
(- (point) 2))))
965 (with-current-buffer gdb-expression-buffer-name
967 (concat "{" (replace-regexp-in-string "\n" "" values
)
969 (gdb-array-format1))))
970 ;;else get rid of ##array-section-end etc
972 (setq gdb-nesting-level
(- gdb-nesting-level
1))
973 (gdb-array-format)))))
975 (defvar gdb-array-start nil
)
976 (defvar gdb-array-stop nil
)
978 (defvar gdb-array-slice-map
979 (let ((map (make-sparse-keymap)))
980 (define-key map
"\r" 'gdb-array-slice
)
981 (define-key map
[mouse-2
] 'gdb-mouse-array-slice
)
984 (defun gdb-mouse-array-slice (event)
985 "Select an array slice to display."
987 (mouse-set-point event
)
990 (defun gdb-array-slice ()
993 (let ((n -
1) (stop 0) (start 0) (point (point)))
995 (while (search-forward "[" point t
)
997 (setq start
(string-to-int (read-string "Start index: ")))
998 (aset gdb-array-start n start
)
999 (setq stop
(string-to-int (read-string "Stop index: ")))
1000 (aset gdb-array-stop n stop
)))
1001 (gdb-array-format1))
1003 (defvar gdb-display-string nil
)
1004 (defvar gdb-array-size nil
)
1006 (defun gdb-array-format1 ()
1007 (setq gdb-display-string
"")
1008 (let ((buffer-read-only nil
))
1009 (delete-region (point-min) (point-max))
1010 (let ((gdb-value-list (split-string gdb-values
", ")))
1011 (string-match "\\({+\\)" (car gdb-value-list
))
1012 (let* ((depth (- (match-end 1) (match-beginning 1)))
1013 (indices (make-vector depth
'0))
1014 (index 0) (num 0) (array-start "")
1015 (array-stop "") (array-slice "") (array-range nil
)
1016 (flag t
) (indices-string ""))
1017 (dolist (gdb-value gdb-value-list
)
1018 (string-match "{*\\([^}]*\\)\\(}*\\)" gdb-value
)
1020 (while (< num depth
)
1021 (setq indices-string
1022 (concat indices-string
1023 "[" (int-to-string (aref indices num
)) "]"))
1024 (if (not (= (aref gdb-array-start num
) -
1))
1025 (if (or (< (aref indices num
) (aref gdb-array-start num
))
1026 (> (aref indices num
) (aref gdb-array-stop num
)))
1028 (aset gdb-array-size num
(aref indices num
)))
1029 (setq num
(+ num
1)))
1031 (let ((gdb-display-value (match-string 1 gdb-value
)))
1032 (setq gdb-display-string
(concat gdb-display-string
" "
1035 (concat indices-string
"\t" gdb-display-value
"\n"))))
1036 (setq indices-string
"")
1038 ;; 0<= index < depth, start at right : (- depth 1)
1039 (setq index
(- (- depth
1)
1040 (- (match-end 2) (match-beginning 2))))
1041 ;;don't set for very last brackets
1043 (aset indices index
(+ 1 (aref indices index
)))
1044 (setq num
(+ 1 index
))
1045 (while (< num depth
)
1046 (aset indices num
0)
1047 (setq num
(+ num
1)))))
1049 (while (< num depth
)
1050 (if (= (aref gdb-array-start num
) -
1)
1052 (aset gdb-array-start num
0)
1053 (aset gdb-array-stop num
(aref indices num
))))
1054 (setq array-start
(int-to-string (aref gdb-array-start num
)))
1055 (setq array-stop
(int-to-string (aref gdb-array-stop num
)))
1056 (setq array-range
(concat "[" array-start
1057 ":" array-stop
"]"))
1058 (add-text-properties
1059 1 (+ (length array-start
) (length array-stop
) 2)
1060 `(mouse-face highlight
1061 local-map
,gdb-array-slice-map
1062 help-echo
"mouse-2, RET: select slice for this index") array-range
)
1063 (goto-char (point-min))
1064 (setq array-slice
(concat array-slice array-range
))
1065 (setq num
(+ num
1)))
1066 (goto-char (point-min))
1067 (insert "Array Size : ")
1069 (while (< num depth
)
1072 (int-to-string (+ (aref gdb-array-size num
) 1)) "]"))
1073 (setq num
(+ num
1)))
1075 (concat "\n Slice : " array-slice
"\n\nIndex\tValues\n\n"))))))
1077 (defun gud-gdba-marker-filter (string)
1078 "A gud marker filter for gdb. Handle a burst of output from GDB."
1080 ;; Recall the left over burst from last time
1081 (burst (concat (gdb-get-burst) string
))
1082 ;; Start accumulating output for the GUD buffer
1085 ;; Process all the complete markers in this chunk.
1086 (while (string-match "\n\032\032\\(.*\\)\n" burst
)
1087 (let ((annotation (match-string 1 burst
)))
1089 ;; Stuff prior to the match is just ordinary output.
1090 ;; It is either concatenated to OUTPUT or directed
1095 (substring burst
0 (match-beginning 0))))
1097 ;; Take that stuff off the burst.
1098 (setq burst
(substring burst
(match-end 0)))
1100 ;; Parse the tag from the annotation, and maybe its arguments.
1101 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation
)
1102 (let* ((annotation-type (match-string 1 annotation
))
1103 (annotation-arguments (match-string 2 annotation
))
1104 (annotation-rule (assoc annotation-type
1105 gdb-annotation-rules
)))
1106 ;; Call the handler for this annotation.
1108 (funcall (car (cdr annotation-rule
))
1109 annotation-arguments
)
1110 ;; Else the annotation is not recognized. Ignore it silently,
1111 ;; so that GDB can add new annotations without causing
1115 ;; Does the remaining text end in a partial line?
1116 ;; If it does, then keep part of the burst until we get more.
1117 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1120 ;; Everything before the potential marker start can be output.
1122 (gdb-concat-output output
1123 (substring burst
0 (match-beginning 0))))
1125 ;; Everything after, we save, to combine with later input.
1126 (setq burst
(substring burst
(match-beginning 0))))
1128 ;; In case we know the burst contains no partial annotations:
1130 (setq output
(gdb-concat-output output burst
))
1133 ;; Save the remaining burst for the next call to this function.
1134 (gdb-set-burst burst
)
1137 (defun gdb-concat-output (so-far new
)
1138 (let ((sink (gdb-get-output-sink )))
1140 ((eq sink
'user
) (concat so-far new
))
1141 ((or (eq sink
'pre-emacs
) (eq sink
'post-emacs
)) so-far
)
1143 (gdb-append-to-partial-output new
)
1145 ((eq sink
'inferior
)
1146 (gdb-append-to-inferior-io new
)
1148 (t (error "Bogon output sink %S" sink
)))))
1150 (defun gdb-append-to-partial-output (string)
1151 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
1152 (goto-char (point-max))
1155 (defun gdb-clear-partial-output ()
1156 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
1157 (delete-region (point-min) (point-max))))
1159 (defun gdb-append-to-inferior-io (string)
1160 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io
)
1161 (goto-char (point-max))
1162 (insert-before-markers string
))
1163 (if (not (string-equal string
""))
1164 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io
))))
1166 (defun gdb-clear-inferior-io ()
1167 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io
)
1168 (delete-region (point-min) (point-max))))
1171 ;; One trick is to have a command who's output is always available in a buffer
1172 ;; of it's own, and is always up to date. We build several buffers of this
1175 ;; There are two aspects to this: gdb has to tell us when the output for that
1176 ;; command might have changed, and we have to be able to run the command
1177 ;; behind the user's back.
1179 ;; The idle input queue and the output phasing associated with the variable
1180 ;; gdb-output-sink help us to run commands behind the user's back.
1182 ;; Below is the code for specificly managing buffers of output from one
1186 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1187 ;; It adds an idle input for the command we are tracking. It should be the
1188 ;; annotation rule binding of whatever gdb sends to tell us this command
1189 ;; might have changed it's output.
1191 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1192 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1193 ;; input in the input queue (see comment about ``gdb communications'' above).
1195 (defmacro def-gdb-auto-update-trigger
(name demand-predicate gdb-command
1197 `(defun ,name
(&optional ignored
)
1198 (if (and (,demand-predicate
)
1200 (gdb-get-pending-triggers))))
1202 (gdb-enqueue-idle-input
1203 (list ,gdb-command
',output-handler
))
1204 (gdb-set-pending-triggers
1206 (gdb-get-pending-triggers)))))))
1208 (defmacro def-gdb-auto-update-handler
(name trigger buf-key custom-defun
)
1210 (gdb-set-pending-triggers
1212 (gdb-get-pending-triggers)))
1213 (let ((buf (gdb-get-buffer ',buf-key
)))
1215 (with-current-buffer buf
1217 (buffer-read-only nil
))
1218 (delete-region (point-min) (point-max))
1219 (insert-buffer-substring (gdb-get-create-buffer
1220 'gdb-partial-output-buffer
))
1222 ;; put customisation here
1225 (defmacro def-gdb-auto-updated-buffer
(buffer-key trigger-name gdb-command
1226 output-handler-name custom-defun
)
1228 (def-gdb-auto-update-trigger ,trigger-name
1229 ;; The demand predicate:
1230 (lambda () (gdb-get-buffer ',buffer-key
))
1232 ,output-handler-name
)
1233 (def-gdb-auto-update-handler ,output-handler-name
1234 ,trigger-name
,buffer-key
,custom-defun
)))
1238 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1240 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1241 'gdb-breakpoints-buffer-name
1242 'gdb-breakpoints-mode
)
1244 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1245 ;; This defines the auto update rule for buffers of type
1246 ;; `gdb-breakpoints-buffer'.
1248 ;; It defines a function to serve as the annotation handler that
1249 ;; handles the `foo-invalidated' message. That function is called:
1250 gdb-invalidate-breakpoints
1252 ;; To update the buffer, this command is sent to gdb.
1253 "server info breakpoints\n"
1255 ;; This also defines a function to be the handler for the output
1256 ;; from the command above. That function will copy the output into
1257 ;; the appropriately typed buffer. That function will be called:
1258 gdb-info-breakpoints-handler
1259 ;; buffer specific functions
1260 gdb-info-breakpoints-custom
)
1262 (defvar gdb-cdir nil
"Compilation directory.")
1264 (defconst breakpoint-xpm-data
"/* XPM */
1265 static char *magick[] = {
1266 /* columns rows colors chars-per-pixel */
1284 "XPM data used for breakpoint icon.")
1286 (defconst breakpoint-enabled-pbm-data
1289 0 0 0 0 0 0 0 0 0 0 0 0
1290 0 0 0 1 1 1 1 1 1 0 0 0
1291 0 0 1 1 1 1 1 1 1 1 0 0
1292 0 1 1 1 1 1 1 1 1 1 1 0
1293 0 1 1 1 1 1 1 1 1 1 1 0
1294 0 1 1 1 1 1 1 1 1 1 1 0
1295 0 1 1 1 1 1 1 1 1 1 1 0
1296 0 1 1 1 1 1 1 1 1 1 1 0
1297 0 1 1 1 1 1 1 1 1 1 1 0
1298 0 0 1 1 1 1 1 1 1 1 0 0
1299 0 0 0 1 1 1 1 1 1 0 0 0
1300 0 0 0 0 0 0 0 0 0 0 0 0"
1301 "PBM data used for enabled breakpoint icon.")
1303 (defconst breakpoint-disabled-pbm-data
1306 0 0 0 0 0 0 0 0 0 0 0 0
1307 0 0 0 1 0 1 0 1 0 0 0 0
1308 0 0 1 0 1 0 1 0 1 0 0 0
1309 0 1 0 1 0 1 0 1 0 1 0 0
1310 0 0 1 0 1 0 1 0 1 0 1 0
1311 0 1 0 1 0 1 0 1 0 1 0 0
1312 0 0 1 0 1 0 1 0 1 0 1 0
1313 0 1 0 1 0 1 0 1 0 1 0 0
1314 0 0 1 0 1 0 1 0 1 0 1 0
1315 0 0 0 1 0 1 0 1 0 1 0 0
1316 0 0 0 0 1 0 1 0 1 0 0 0
1317 0 0 0 0 0 0 0 0 0 0 0 0"
1318 "PBM data used for disabled breakpoint icon.")
1320 (defvar breakpoint-enabled-icon
1321 (find-image `((:type xpm
:data
,breakpoint-xpm-data
)
1322 (:type pbm
:data
,breakpoint-enabled-pbm-data
)))
1323 "Icon for enabled breakpoint in display margin")
1325 (defvar breakpoint-disabled-icon
1326 (find-image `((:type xpm
:data
,breakpoint-xpm-data
:conversion disabled
)
1327 (:type pbm
:data
,breakpoint-disabled-pbm-data
)))
1328 "Icon for disabled breakpoint in display margin")
1330 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1331 (defun gdb-info-breakpoints-custom ()
1332 (let ((flag)(address))
1334 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1335 (dolist (buffer (buffer-list))
1336 (with-current-buffer buffer
1337 (if (and (eq gud-minor-mode
'gdba
)
1338 (not (string-match "^\*" (buffer-name))))
1339 (if (display-images-p)
1340 (remove-images (point-min) (point-max))
1341 (gdb-remove-strings (point-min) (point-max))))))
1342 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer
)
1344 (goto-char (point-min))
1345 (while (< (point) (- (point-max) 1))
1347 (if (looking-at "[^\t].*breakpoint")
1349 (looking-at "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)")
1350 (setq flag
(char-after (match-beginning 1)))
1352 (if (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t
)
1354 (looking-at "\\(\\S-*\\):\\([0-9]+\\)")
1355 (let ((line (match-string 2)) (buffer-read-only nil
)
1356 (file (match-string 1)))
1357 (add-text-properties (point-at-bol) (point-at-eol)
1358 '(mouse-face highlight
1359 help-echo
"mouse-2, RET: visit breakpoint"))
1360 (with-current-buffer
1362 (if (file-exists-p file
) file
1363 (expand-file-name file gdb-cdir
)))
1364 (save-current-buffer
1365 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
1366 (set (make-local-variable 'tool-bar-map
)
1368 (setq left-margin-width
2)
1369 (if (get-buffer-window (current-buffer))
1370 (set-window-margins (get-buffer-window
1373 right-margin-width
)))
1374 ;; only want one breakpoint icon at each location
1376 (goto-line (string-to-number line
))
1377 (let ((start (progn (beginning-of-line)
1379 (end (progn (end-of-line) (+ (point) 1))))
1380 (if (display-images-p)
1382 (remove-images start end
)
1384 (put-image breakpoint-enabled-icon
1386 "breakpoint icon enabled"
1388 (put-image breakpoint-disabled-icon
1390 "breakpoint icon disabled"
1392 (gdb-remove-strings start end
)
1394 (gdb-put-string "B" (+ start
1))
1395 (gdb-put-string "b" (+ start
1))))))))))))
1397 (if (gdb-get-buffer 'gdb-assembler-buffer
) (gdb-assembler-custom)))
1399 (defun gdb-breakpoints-buffer-name ()
1400 (with-current-buffer gud-comint-buffer
1401 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1403 (defun gdb-display-breakpoints-buffer ()
1406 (gdb-get-create-buffer 'gdb-breakpoints-buffer
)))
1408 (defun gdb-frame-breakpoints-buffer ()
1410 (switch-to-buffer-other-frame
1411 (gdb-get-create-buffer 'gdb-breakpoints-buffer
)))
1413 (defvar gdb-breakpoints-mode-map
1414 (let ((map (make-sparse-keymap))
1415 (menu (make-sparse-keymap "Breakpoints")))
1416 (define-key menu
[toggle] '("Toggle" . gdb-toggle-breakpoint))
1417 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1418 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1420 (suppress-keymap map)
1421 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1422 (define-key map " " 'gdb-toggle-breakpoint)
1423 (define-key map "d" 'gdb-delete-breakpoint)
1424 (define-key map "\r" 'gdb-goto-breakpoint)
1425 (define-key map [mouse-2] 'gdb-mouse-goto-breakpoint)
1428 (defun gdb-breakpoints-mode ()
1429 "Major mode for gdb breakpoints.
1431 \\{gdb-breakpoints-mode-map}"
1432 (setq major-mode 'gdb-breakpoints-mode)
1433 (setq mode-name "Breakpoints")
1434 (use-local-map gdb-breakpoints-mode-map)
1435 (setq buffer-read-only t)
1436 (gdb-invalidate-breakpoints))
1438 (defun gdb-toggle-breakpoint ()
1439 "Enable/disable the breakpoint at current line."
1442 (beginning-of-line 1)
1443 (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
1444 (error "Not recognized as break/watchpoint line")
1448 (if (eq ?y (char-after (match-beginning 2)))
1451 (match-string 1) "\n")
1454 (defun gdb-delete-breakpoint ()
1455 "Delete the breakpoint at current line."
1457 (beginning-of-line 1)
1458 (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
1459 (error "Not recognized as break/watchpoint line")
1461 (list (concat "server delete " (match-string 1) "\n") 'ignore))))
1463 (defvar gdb-source-window nil)
1465 (defun gdb-goto-breakpoint ()
1466 "Display the file in the source buffer at the breakpoint specified on the
1470 (beginning-of-line 1)
1471 (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
1472 (looking-at "\\(\\S-*\\):\\([0-9]+\\)"))
1473 (if (match-string 2)
1474 (let ((line (match-string 2))
1475 (file (match-string 1)))
1476 (save-selected-window
1477 (select-window gdb-source-window)
1478 (switch-to-buffer (find-file-noselect
1479 (if (file-exists-p file)
1481 (expand-file-name file gdb-cdir))))
1482 (goto-line (string-to-number line))))))
1484 (defun gdb-mouse-goto-breakpoint (event)
1485 "Display the file in the source buffer at the selected breakpoint."
1487 (mouse-set-point event)
1488 (gdb-goto-breakpoint))
1491 ;; Frames buffer. This displays a perpetually correct bactracktrace
1492 ;; (from the command `where').
1494 ;; Alas, if your stack is deep, it is costly.
1496 (gdb-set-buffer-rules 'gdb-stack-buffer
1497 'gdb-stack-buffer-name
1500 (def-gdb-auto-updated-buffer gdb-stack-buffer
1501 gdb-invalidate-frames
1503 gdb-info-frames-handler
1504 gdb-info-frames-custom)
1506 (defun gdb-info-frames-custom ()
1507 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1509 (let ((buffer-read-only nil))
1510 (goto-char (point-min))
1511 (while (< (point) (point-max))
1512 (add-text-properties (point-at-bol) (point-at-eol)
1513 '(mouse-face highlight
1514 help-echo "mouse-2, RET: Select frame"))
1516 (if (or (looking-at "^#[0-9]*\\s-*\\S-* in \\(\\S-*\\)")
1517 (looking-at "^#[0-9]*\\s-*\\(\\S-*\\)"))
1518 (if (equal (match-string 1) gdb-current-frame)
1519 (put-text-property (point-at-bol) (point-at-eol)
1521 `(:background ,(face-attribute 'default :foreground)
1522 :foreground ,(face-attribute 'default :background)))))
1523 (forward-line 1))))))
1525 (defun gdb-stack-buffer-name ()
1526 (with-current-buffer gud-comint-buffer
1527 (concat "*stack frames of " (gdb-get-target-string) "*")))
1529 (defun gdb-display-stack-buffer ()
1532 (gdb-get-create-buffer 'gdb-stack-buffer)))
1534 (defun gdb-frame-stack-buffer ()
1536 (switch-to-buffer-other-frame
1537 (gdb-get-create-buffer 'gdb-stack-buffer)))
1539 (defvar gdb-frames-mode-map
1540 (let ((map (make-sparse-keymap)))
1541 (suppress-keymap map)
1542 (define-key map "\r" 'gdb-frames-select)
1543 (define-key map [mouse-2] 'gdb-frames-mouse-select)
1546 (defun gdb-frames-mode ()
1547 "Major mode for gdb frames.
1549 \\{gdb-frames-mode-map}"
1550 (setq major-mode 'gdb-frames-mode)
1551 (setq mode-name "Frames")
1552 (setq buffer-read-only t)
1553 (use-local-map gdb-frames-mode-map)
1555 (gdb-invalidate-frames))
1557 (defun gdb-get-frame-number ()
1559 (let* ((pos (re-search-backward "^#\\([0-9]*\\)" nil t))
1560 (n (or (and pos (match-string-no-properties 1)) "0")))
1563 (defun gdb-frames-select ()
1564 "Make the frame on the current line become the current frame and display the
1565 source in the source buffer."
1568 (list (concat "server frame " (gdb-get-frame-number) "\n") 'ignore))
1569 (gud-display-frame))
1571 (defun gdb-frames-mouse-select (event)
1572 "Make the selected frame become the current frame and display the source in
1575 (mouse-set-point event)
1576 (gdb-frames-select))
1579 ;; Threads buffer. This displays a selectable thread list.
1581 (gdb-set-buffer-rules 'gdb-threads-buffer
1582 'gdb-threads-buffer-name
1585 (def-gdb-auto-updated-buffer gdb-threads-buffer
1586 gdb-invalidate-threads
1588 gdb-info-threads-handler
1589 gdb-info-threads-custom)
1591 (defun gdb-info-threads-custom ()
1592 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1593 (let ((buffer-read-only nil))
1594 (goto-char (point-min))
1595 (while (< (point) (point-max))
1596 (add-text-properties (point-at-bol) (point-at-eol)
1597 '(mouse-face highlight
1598 help-echo "mouse-2, RET: select thread"))
1599 (forward-line 1)))))
1601 (defun gdb-threads-buffer-name ()
1602 (with-current-buffer gud-comint-buffer
1603 (concat "*threads of " (gdb-get-target-string) "*")))
1605 (defun gdb-display-threads-buffer ()
1608 (gdb-get-create-buffer 'gdb-threads-buffer)))
1610 (defun gdb-frame-threads-buffer ()
1612 (switch-to-buffer-other-frame
1613 (gdb-get-create-buffer 'gdb-threads-buffer)))
1615 (defvar gdb-threads-mode-map
1616 (let ((map (make-sparse-keymap)))
1617 (suppress-keymap map)
1618 (define-key map "\r" 'gdb-threads-select)
1619 (define-key map [mouse-2] 'gdb-threads-mouse-select)
1622 (defun gdb-threads-mode ()
1623 "Major mode for gdb frames.
1625 \\{gdb-frames-mode-map}"
1626 (setq major-mode 'gdb-threads-mode)
1627 (setq mode-name "Threads")
1628 (setq buffer-read-only t)
1629 (use-local-map gdb-threads-mode-map)
1630 (gdb-invalidate-threads))
1632 (defun gdb-get-thread-number ()
1634 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1635 (match-string-no-properties 1)))
1638 (defun gdb-threads-select ()
1639 "Make the thread on the current line become the current thread and display the
1640 source in the source buffer."
1643 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1644 (gud-display-frame))
1646 (defun gdb-threads-mouse-select (event)
1647 "Make the selected frame become the current frame and display the source in
1650 (mouse-set-point event)
1651 (gdb-threads-select))
1654 ;; Registers buffer.
1656 (gdb-set-buffer-rules 'gdb-registers-buffer
1657 'gdb-registers-buffer-name
1658 'gdb-registers-mode)
1660 (def-gdb-auto-updated-buffer gdb-registers-buffer
1661 gdb-invalidate-registers
1662 "server info registers\n"
1663 gdb-info-registers-handler
1664 gdb-info-registers-custom)
1666 (defun gdb-info-registers-custom ())
1668 (defvar gdb-registers-mode-map
1669 (let ((map (make-sparse-keymap)))
1670 (suppress-keymap map)
1673 (defun gdb-registers-mode ()
1674 "Major mode for gdb registers.
1676 \\{gdb-registers-mode-map}"
1677 (setq major-mode 'gdb-registers-mode)
1678 (setq mode-name "Registers")
1679 (setq buffer-read-only t)
1680 (use-local-map gdb-registers-mode-map)
1681 (gdb-invalidate-registers))
1683 (defun gdb-registers-buffer-name ()
1684 (with-current-buffer gud-comint-buffer
1685 (concat "*registers of " (gdb-get-target-string) "*")))
1687 (defun gdb-display-registers-buffer ()
1690 (gdb-get-create-buffer 'gdb-registers-buffer)))
1692 (defun gdb-frame-registers-buffer ()
1694 (switch-to-buffer-other-frame
1695 (gdb-get-create-buffer 'gdb-registers-buffer)))
1700 (gdb-set-buffer-rules 'gdb-locals-buffer
1701 'gdb-locals-buffer-name
1704 (def-gdb-auto-updated-buffer gdb-locals-buffer
1705 gdb-invalidate-locals
1706 "server info locals\n"
1707 gdb-info-locals-handler
1708 gdb-info-locals-custom)
1710 ;; Abbreviate for arrays and structures.
1711 ;; These can be expanded using gud-display.
1712 (defun gdb-info-locals-handler nil
1713 (gdb-set-pending-triggers (delq 'gdb-invalidate-locals
1714 (gdb-get-pending-triggers)))
1715 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1716 (with-current-buffer buf
1717 (goto-char (point-min))
1718 (while (re-search-forward "^ .*\n" nil t)
1719 (replace-match "" nil nil))
1720 (goto-char (point-min))
1721 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1722 (replace-match "(array);\n" nil nil))
1723 (goto-char (point-min))
1724 (while (re-search-forward "{.*=.*\n" nil t)
1725 (replace-match "(structure);\n" nil nil))))
1726 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1727 (and buf (with-current-buffer buf
1729 (buffer-read-only nil))
1730 (delete-region (point-min) (point-max))
1731 (insert-buffer-substring (gdb-get-create-buffer
1732 'gdb-partial-output-buffer))
1734 (run-hooks 'gdb-info-locals-hook))
1736 (defun gdb-info-locals-custom ()
1739 (defvar gdb-locals-mode-map
1740 (let ((map (make-sparse-keymap)))
1741 (suppress-keymap map)
1744 (defun gdb-locals-mode ()
1745 "Major mode for gdb locals.
1747 \\{gdb-locals-mode-map}"
1748 (setq major-mode 'gdb-locals-mode)
1749 (setq mode-name "Locals")
1750 (setq buffer-read-only t)
1751 (use-local-map gdb-locals-mode-map)
1752 (gdb-invalidate-locals))
1754 (defun gdb-locals-buffer-name ()
1755 (with-current-buffer gud-comint-buffer
1756 (concat "*locals of " (gdb-get-target-string) "*")))
1758 (defun gdb-display-locals-buffer ()
1761 (gdb-get-create-buffer 'gdb-locals-buffer)))
1763 (defun gdb-frame-locals-buffer ()
1765 (switch-to-buffer-other-frame
1766 (gdb-get-create-buffer 'gdb-locals-buffer)))
1769 ;; Display expression buffer.
1771 (gdb-set-buffer-rules 'gdb-display-buffer
1772 'gdb-display-buffer-name
1775 (def-gdb-auto-updated-buffer gdb-display-buffer
1776 ;; `gdb-display-buffer'.
1777 gdb-invalidate-display
1778 "server info display\n"
1779 gdb-info-display-handler
1780 gdb-info-display-custom)
1782 (defun gdb-info-display-custom ()
1783 (let ((display-list nil))
1784 (with-current-buffer (gdb-get-buffer 'gdb-display-buffer)
1785 (goto-char (point-min))
1786 (while (< (point) (- (point-max) 1))
1788 (if (looking-at "\\([0-9]+\\): \\([ny]\\)")
1790 (cons (string-to-int (match-string 1)) display-list)))
1792 (if (not (display-graphic-p))
1794 (dolist (buffer (buffer-list))
1795 (if (string-match "\\*display \\([0-9]+\\)\\*" (buffer-name buffer))
1798 (match-string 1 (buffer-name buffer))))
1799 (if (not (memq (string-to-int number) display-list))
1801 (get-buffer (concat "*display " number "*")))))))))
1802 (gdb-delete-frames display-list))))
1804 (defun gdb-delete-frames (display-list)
1805 (dolist (frame (frame-list))
1806 (let ((frame-name (frame-parameter frame 'name)))
1807 (if (string-match "\\*display \\([0-9]+\\)\\*" frame-name)
1809 (let ((number (match-string 1 frame-name)))
1810 (if (not (memq (string-to-int number) display-list))
1812 (get-buffer (concat "*display " number "*")))
1813 (delete-frame frame)))))))))
1815 (defvar gdb-display-mode-map
1816 (let ((map (make-sparse-keymap))
1817 (menu (make-sparse-keymap "Display")))
1818 (define-key menu [toggle] '("Toggle" . gdb-toggle-display
))
1819 (define-key menu
[delete] '("Delete" . gdb-delete-display))
1821 (suppress-keymap map)
1822 (define-key map [menu-bar display] (cons "Display" menu))
1823 (define-key map " " 'gdb-toggle-display)
1824 (define-key map "d" 'gdb-delete-display)
1827 (defun gdb-display-mode ()
1828 "Major mode for gdb display.
1830 \\{gdb-display-mode-map}"
1831 (setq major-mode 'gdb-display-mode)
1832 (setq mode-name "Display")
1833 (setq buffer-read-only t)
1834 (use-local-map gdb-display-mode-map)
1835 (gdb-invalidate-display))
1837 (defun gdb-display-buffer-name ()
1838 (with-current-buffer gud-comint-buffer
1839 (concat "*Displayed expressions of " (gdb-get-target-string) "*")))
1841 (defun gdb-display-display-buffer ()
1844 (gdb-get-create-buffer 'gdb-display-buffer)))
1846 (defun gdb-frame-display-buffer ()
1848 (switch-to-buffer-other-frame
1849 (gdb-get-create-buffer 'gdb-display-buffer)))
1851 (defun gdb-toggle-display ()
1852 "Enable/disable the displayed expression at current line."
1855 (beginning-of-line 1)
1856 (if (not (looking-at "\\([0-9]+\\): \\([ny]\\)"))
1857 (error "No expression on this line")
1861 (if (eq ?y (char-after (match-beginning 2)))
1862 "server disable display "
1863 "server enable display ")
1864 (match-string 1) "\n")
1867 (defun gdb-delete-display ()
1868 "Delete the displayed expression at current line."
1870 (with-current-buffer (gdb-get-buffer 'gdb-display-buffer)
1871 (beginning-of-line 1)
1872 (if (not (looking-at "\\([0-9]+\\): \\([ny]\\)"))
1873 (error "No expression on this line")
1874 (let ((number (match-string 1)))
1876 (list (concat "server delete display " number "\n") 'ignore))))))
1878 (defvar gdb-expressions-mode-map
1879 (let ((map (make-sparse-keymap)))
1880 (suppress-keymap map)
1881 (define-key map "v" 'gdb-array-visualise)
1882 (define-key map "q" 'gdb-delete-expression)
1883 (define-key map [mouse-3] 'gdb-expressions-popup-menu)
1886 (defvar gdb-expressions-mode-menu
1887 '("GDB Expressions Commands"
1889 ["Visualise" gdb-array-visualise t]
1890 ["Delete" gdb-delete-expression t])
1891 "Menu for `gdb-expressions-mode'.")
1893 (defun gdb-expressions-popup-menu (event)
1894 "Explicit Popup menu as this buffer doesn't have a menubar."
1896 (mouse-set-point event)
1897 (popup-menu gdb-expressions-mode-menu))
1899 (defun gdb-expressions-mode ()
1900 "Major mode for display expressions.
1902 \\{gdb-expressions-mode-map}"
1903 (setq major-mode 'gdb-expressions-mode)
1904 (setq mode-name "Expressions")
1905 (use-local-map gdb-expressions-mode-map)
1906 (make-local-variable 'gdb-display-number)
1907 (make-local-variable 'gdb-values)
1908 (make-local-variable 'gdb-expression)
1909 (set (make-local-variable 'gdb-display-string) nil)
1910 (set (make-local-variable 'gdb-dive-display-number) nil)
1911 (set (make-local-variable 'gud-minor-mode) 'gdba)
1912 (set (make-local-variable 'gdb-array-start) (make-vector 16 '-1))
1913 (set (make-local-variable 'gdb-array-stop) (make-vector 16 '-1))
1914 (set (make-local-variable 'gdb-array-size) (make-vector 16 '-1))
1915 (setq buffer-read-only t))
1918 ;;;; Window management
1920 ;;; The way we abuse the dedicated-p flag is pretty gross, but seems
1921 ;;; to do the right thing. Seeing as there is no way for Lisp code to
1922 ;;; get at the use_time field of a window, I'm not sure there exists a
1923 ;;; more elegant solution without writing C code.
1925 (defun gdb-display-buffer (buf &optional size)
1926 (let ((must-split nil)
1932 (if (or (eq gud-comint-buffer (window-buffer win))
1933 (eq gdb-source-window win))
1934 (set-window-dedicated-p win t))))
1935 (setq answer (get-buffer-window buf))
1937 (let ((window (get-lru-window)))
1940 (set-window-buffer window buf)
1941 (setq answer window))
1942 (setq must-split t)))))
1945 (if (or (eq gud-comint-buffer (window-buffer win))
1946 (eq gdb-source-window win))
1947 (set-window-dedicated-p win nil)))))
1949 (let* ((largest (get-largest-window))
1950 (cur-size (window-height largest))
1951 (new-size (and size (< size cur-size) (- cur-size size))))
1952 (setq answer (split-window largest new-size))
1953 (set-window-buffer answer buf)))
1956 (defun gdb-display-source-buffer (buffer)
1957 (if (eq gdb-selected-view 'source)
1959 (if (window-live-p gdb-source-window)
1960 (set-window-buffer gdb-source-window buffer)
1961 (gdb-display-buffer buffer)
1962 (setq gdb-source-window (get-buffer-window buffer)))
1964 (if (window-live-p gdb-source-window)
1965 (set-window-buffer gdb-source-window
1966 (gdb-get-buffer 'gdb-assembler-buffer))
1967 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
1968 (gdb-display-buffer buf)
1969 (setq gdb-source-window (get-buffer-window buf))))
1973 ;;; Shared keymap initialization:
1975 (let ((menu (make-sparse-keymap "GDB-Frames")))
1976 (define-key gud-menu-map [frames]
1977 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
1978 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
1979 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
1980 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
1981 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
1982 (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer))
1983 (define-key menu [display] '("Display" . gdb-frame-display-buffer))
1984 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
1985 ; (define-key menu [assembler] '("Assembler" . gdb-frame-assembler-buffer))
1988 (let ((menu (make-sparse-keymap "GDB-Windows")))
1989 (define-key gud-menu-map [displays]
1990 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
1991 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
1992 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
1993 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
1994 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
1995 (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer))
1996 (define-key menu [display] '("Display" . gdb-display-display-buffer))
1997 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
1998 ; (define-key menu [assembler] '("Assembler" . gdb-display-assembler-buffer))
2001 (let ((menu (make-sparse-keymap "View")))
2002 (define-key gud-menu-map [view]
2003 `(menu-item "View" ,menu :visible (eq gud-minor-mode 'gdba)))
2004 ; (define-key menu [both] '(menu-item "Both" gdb-view-both
2005 ; :help "Display both source and assembler"
2006 ; :button (:radio . (eq gdb-selected-view 'both))))
2007 (define-key menu [assembler] '(menu-item "Assembler" gdb-view-assembler
2008 :help "Display assembler only"
2009 :button (:radio . (eq gdb-selected-view 'assembler))))
2010 (define-key menu [source] '(menu-item "Source" gdb-view-source-function
2011 :help "Display source only"
2012 :button (:radio . (eq gdb-selected-view 'source)))))
2014 (let ((menu (make-sparse-keymap "GDB-UI")))
2015 (define-key gud-menu-map [ui]
2016 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
2017 (define-key menu [gdb-restore-windows]
2018 '("Restore window layout" . gdb-restore-windows))
2019 (define-key menu [gdb-many-windows]
2020 (menu-bar-make-toggle gdb-many-windows gdb-many-windows
2021 "Display other windows" "Many Windows %s"
2022 "Display locals, stack and breakpoint information")))
2024 (defun gdb-frame-gdb-buffer ()
2026 (switch-to-buffer-other-frame
2027 (gdb-get-create-buffer 'gdba)))
2029 (defun gdb-display-gdb-buffer ()
2032 (gdb-get-create-buffer 'gdba)))
2034 (defvar gdb-main-file nil "Source file from which program execution begins.")
2036 (defun gdb-view-source-function ()
2039 (if gud-last-last-frame
2040 (set-window-buffer gdb-source-window
2041 (gud-find-file (car gud-last-last-frame)))
2042 (set-window-buffer gdb-source-window (gud-find-file gdb-main-file))))
2043 (setq gdb-selected-view 'source))
2045 (defun gdb-view-assembler()
2047 (set-window-buffer gdb-source-window
2048 (gdb-get-create-buffer 'gdb-assembler-buffer))
2049 (setq gdb-selected-view 'assembler))
2051 ;(defun gdb-view-both()
2053 ;(setq gdb-selected-view 'both))
2055 ;; layout for all the windows
2056 (defun gdb-setup-windows ()
2057 (gdb-display-locals-buffer)
2058 (gdb-display-stack-buffer)
2059 (delete-other-windows)
2060 (gdb-display-breakpoints-buffer)
2061 (gdb-display-display-buffer)
2062 (delete-other-windows)
2063 (switch-to-buffer gud-comint-buffer)
2064 (split-window nil ( / ( * (window-height) 3) 4))
2065 (split-window nil ( / (window-height) 3))
2066 (split-window-horizontally)
2068 (switch-to-buffer (gdb-locals-buffer-name))
2070 (if (and gdb-view-source
2071 (eq gdb-selected-view 'source))
2073 (if gud-last-last-frame
2074 (gud-find-file (car gud-last-last-frame))
2075 (gud-find-file gdb-main-file)))
2076 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer)))
2077 (setq gdb-source-window (get-buffer-window (current-buffer)))
2078 (split-window-horizontally)
2080 (switch-to-buffer (gdb-inferior-io-name))
2082 (switch-to-buffer (gdb-stack-buffer-name))
2083 (split-window-horizontally)
2085 (switch-to-buffer (gdb-breakpoints-buffer-name))
2088 (defcustom gdb-many-windows nil
2089 "Nil means that gdb starts with just two windows : the GUD and
2094 (defun gdb-many-windows (arg)
2095 "Toggle the number of windows in the basic arrangement."
2097 (setq gdb-many-windows
2099 (not gdb-many-windows)
2100 (> (prefix-numeric-value arg) 0)))
2101 (gdb-restore-windows))
2103 (defun gdb-restore-windows ()
2104 "Restore the basic arrangement of windows used by gdba.
2105 This arrangement depends on the value of `gdb-many-windows'."
2107 (if gdb-many-windows
2109 (switch-to-buffer gud-comint-buffer)
2110 (delete-other-windows)
2111 (gdb-setup-windows))
2112 (switch-to-buffer gud-comint-buffer)
2113 (delete-other-windows)
2116 (if (and gdb-view-source
2117 (eq gdb-selected-view 'source))
2119 (if gud-last-last-frame
2120 (gud-find-file (car gud-last-last-frame))
2121 (gud-find-file gdb-main-file)))
2122 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer)))
2123 (setq gdb-source-window (get-buffer-window (current-buffer)))
2127 "Exit a debugging session cleanly by killing the gdb buffers and resetting
2128 the source buffers."
2129 (gdb-delete-frames '())
2130 (dolist (buffer (buffer-list))
2131 (if (not (eq buffer gud-comint-buffer))
2132 (with-current-buffer buffer
2133 (if (eq gud-minor-mode 'gdba)
2134 (if (string-match "^\*.+*$" (buffer-name))
2136 (if (display-images-p)
2137 (remove-images (point-min) (point-max))
2138 (gdb-remove-strings (point-min) (point-max)))
2139 (setq left-margin-width 0)
2140 (setq gud-minor-mode nil)
2141 (kill-local-variable 'tool-bar-map)
2142 (setq gud-running nil)
2143 (if (get-buffer-window (current-buffer))
2144 (set-window-margins (get-buffer-window
2147 right-margin-width))))))))
2149 (defun gdb-source-info ()
2150 "Find the source file where the program starts and displays it with related
2152 (goto-char (point-min))
2153 (if (search-forward "directory is " nil t)
2155 (if (looking-at "\\S-*:\\(\\S-*\\)")
2156 (setq gdb-cdir (match-string 1))
2157 (looking-at "\\S-*")
2158 (setq gdb-cdir (match-string 0)))
2159 (search-forward "Located in ")
2160 (looking-at "\\S-*")
2161 (setq gdb-main-file (match-string 0)))
2162 (setq gdb-view-source nil))
2163 (delete-other-windows)
2164 (switch-to-buffer gud-comint-buffer)
2165 (if gdb-many-windows
2167 (gdb-display-breakpoints-buffer)
2168 (gdb-display-display-buffer)
2169 (delete-other-windows)
2174 (if gud-last-last-frame
2175 (gud-find-file (car gud-last-last-frame))
2176 (gud-find-file gdb-main-file)))
2177 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer)))
2178 (setq gdb-source-window (get-buffer-window (current-buffer)))
2182 (defun gdb-put-string (putstring pos)
2183 "Put string PUTSTRING in front of POS in the current buffer.
2184 PUTSTRING is displayed by putting an overlay into the current buffer with a
2185 `before-string' STRING that has a `display' property whose value is
2187 (let ((gdb-string "x")
2188 (buffer (current-buffer)))
2189 (let ((overlay (make-overlay pos pos buffer))
2190 (prop (list (list 'margin 'left-margin) putstring)))
2191 (put-text-property 0 (length gdb-string) 'display prop gdb-string)
2192 (overlay-put overlay 'put-break t)
2193 (overlay-put overlay 'before-string gdb-string))))
2195 ;;from remove-images
2196 (defun gdb-remove-strings (start end &optional buffer)
2197 "Remove strings between START and END in BUFFER.
2198 Remove only strings that were put in BUFFER with calls to `put-string'.
2199 BUFFER nil or omitted means use the current buffer."
2201 (setq buffer (current-buffer)))
2202 (let ((overlays (overlays-in start end)))
2204 (let ((overlay (car overlays)))
2205 (when (overlay-get overlay 'put-break)
2206 (delete-overlay overlay)))
2207 (setq overlays (cdr overlays)))))
2209 (defun gdb-put-arrow (putstring pos)
2210 "Put arrow string PUTSTRING in the left margin in front of POS
2211 in the current buffer. PUTSTRING is displayed by putting an
2212 overlay into the current buffer with a `before-string'
2213 \"gdb-arrow\" that has a `display' property whose value is
2214 PUTSTRING. POS may be an integer or marker."
2215 (let ((gdb-string "gdb-arrow")
2216 (buffer (current-buffer)))
2217 (let ((overlay (make-overlay pos pos buffer))
2218 (prop (list (list 'margin 'left-margin) putstring)))
2219 (put-text-property 0 (length gdb-string) 'display prop gdb-string)
2220 (overlay-put overlay 'put-arrow t)
2221 (overlay-put overlay 'before-string gdb-string))))
2223 (defun gdb-remove-arrow (&optional buffer)
2224 "Remove arrow in BUFFER.
2225 Remove only images that were put in BUFFER with calls to `put-arrow'.
2226 BUFFER nil or omitted means use the current buffer."
2228 (setq buffer (current-buffer)))
2229 (let ((overlays (overlays-in (point-min) (point-max))))
2231 (let ((overlay (car overlays)))
2232 (when (overlay-get overlay 'put-arrow)
2233 (delete-overlay overlay)))
2234 (setq overlays (cdr overlays)))))
2236 (defun gdb-array-visualise ()
2237 "Visualise arrays and slices using graph program from plotutils."
2239 (when (and (display-graphic-p) gdb-display-string)
2241 (catch 'multi-dimensional
2242 (while (eq (aref gdb-array-start n) (aref gdb-array-stop n))
2245 (while (< m (length gdb-array-start))
2246 (if (not (eq (aref gdb-array-start m) (aref gdb-array-stop m)))
2249 t `(,(concat "Only one dimensional data can be visualised.\n"
2250 "Use an array slice to reduce the number of\n"
2251 "dimensions") ("OK" t)))
2252 (throw 'multi-dimensional nil))
2254 (shell-command (concat "echo" gdb-display-string " | graph -a 1 "
2255 (int-to-string (aref gdb-array-start n))
2257 (int-to-string (aref gdb-array-start n))
2259 (int-to-string (aref gdb-array-stop n))
2262 (defun gdb-delete-expression ()
2263 "Delete displayed expression and its frame."
2266 (list (concat "server delete display " gdb-display-number "\n")
2270 ;; Assembler buffer.
2272 (gdb-set-buffer-rules 'gdb-assembler-buffer
2273 'gdb-assembler-buffer-name
2274 'gdb-assembler-mode)
2276 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2277 gdb-invalidate-assembler
2278 (concat "server disassemble " gdb-current-address "\n")
2279 gdb-assembler-handler
2280 gdb-assembler-custom)
2282 (defun gdb-assembler-custom ()
2283 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2284 (gdb-arrow-position 1) (address) (flag))
2285 (with-current-buffer buffer
2286 (if (not (equal gdb-current-address "main"))
2289 (goto-char (point-min))
2290 (if (re-search-forward gdb-current-address nil t)
2292 (setq gdb-arrow-position (point))
2293 (gdb-put-arrow "=>" (point))))))
2294 ;; remove all breakpoint-icons in assembler buffer before updating.
2295 (if (display-images-p)
2296 (remove-images (point-min) (point-max))
2297 (gdb-remove-strings (point-min) (point-max))))
2298 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2299 (goto-char (point-min))
2300 (while (< (point) (- (point-max) 1))
2302 (if (looking-at "[^\t].*breakpoint")
2305 "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*0x\\(\\S-*\\)")
2306 (setq flag (char-after (match-beginning 1)))
2307 (setq address (match-string 2))
2308 ;; remove leading 0s from output of info break.
2309 (if (string-match "^0+\\(.*\\)" address)
2310 (setq address (match-string 1 address)))
2311 (with-current-buffer buffer
2312 (goto-char (point-min))
2313 (if (re-search-forward address nil t)
2314 (let ((start (progn (beginning-of-line) (- (point) 1)))
2315 (end (progn (end-of-line) (+ (point) 1))))
2316 (if (display-images-p)
2318 (remove-images start end)
2320 (put-image breakpoint-enabled-icon
2322 "breakpoint icon enabled"
2324 (put-image breakpoint-disabled-icon
2326 "breakpoint icon disabled"
2328 (gdb-remove-strings start end)
2330 (gdb-put-string "B" (+ start 1))
2331 (gdb-put-string "b" (+ start 1)))))))))))
2332 (if (not (equal gdb-current-address "main"))
2333 (set-window-point (get-buffer-window buffer) gdb-arrow-position))))
2335 (defvar gdb-assembler-mode-map
2336 (let ((map (make-sparse-keymap)))
2337 (suppress-keymap map)
2340 (defun gdb-assembler-mode ()
2341 "Major mode for viewing code assembler.
2343 \\{gdb-assembler-mode-map}"
2344 (setq major-mode 'gdb-assembler-mode)
2345 (setq mode-name "Assembler")
2346 (setq left-margin-width 2)
2347 (setq fringes-outside-margins t)
2348 (setq buffer-read-only t)
2349 (use-local-map gdb-assembler-mode-map)
2350 (gdb-invalidate-assembler))
2352 (defun gdb-assembler-buffer-name ()
2353 (with-current-buffer gud-comint-buffer
2354 (concat "*Machine Code " (gdb-get-target-string) "*")))
2356 (defun gdb-display-assembler-buffer ()
2359 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2361 (defun gdb-frame-assembler-buffer ()
2363 (switch-to-buffer-other-frame
2364 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2366 ;; modified because if gdb-current-address has changed value a new command
2367 ;; must be enqueued to update the buffer with the new output
2368 (defun gdb-invalidate-assembler (&optional ignored)
2369 (if (gdb-get-buffer 'gdb-assembler-buffer)
2371 (unless (string-equal gdb-current-frame gdb-previous-frame)
2372 (if (or (not (member 'gdb-invalidate-assembler
2373 (gdb-get-pending-triggers)))
2374 (not (string-equal gdb-current-address
2375 gdb-previous-address)))
2377 ;; take previous disassemble command off the queue
2378 (with-current-buffer gud-comint-buffer
2379 (let ((queue (gdb-get-idle-input-queue)) (item))
2380 (dolist (item queue)
2381 (if (equal (cdr item) '(gdb-assembler-handler))
2382 (gdb-set-idle-input-queue
2383 (delete item (gdb-get-idle-input-queue)))))))
2384 (gdb-enqueue-idle-input
2385 (list (concat "server disassemble " gdb-current-address "\n")
2386 'gdb-assembler-handler))
2387 (gdb-set-pending-triggers
2388 (cons 'gdb-invalidate-assembler
2389 (gdb-get-pending-triggers)))
2390 (setq gdb-previous-address gdb-current-address)
2391 (setq gdb-previous-frame gdb-current-frame)))))))
2393 (defun gdb-get-current-frame ()
2394 (if (not (member 'gdb-get-current-frame (gdb-get-pending-triggers)))
2396 (gdb-enqueue-idle-input
2397 (list (concat "server info frame\n") 'gdb-frame-handler))
2398 (gdb-set-pending-triggers
2399 (cons 'gdb-get-current-frame
2400 (gdb-get-pending-triggers))))))
2402 (defun gdb-frame-handler ()
2403 (gdb-set-pending-triggers
2404 (delq 'gdb-get-current-frame (gdb-get-pending-triggers)))
2405 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2406 (goto-char (point-min))
2408 (if (looking-at ".*= 0x\\(\\S-*\\) in \\(\\S-*\\)")
2410 (setq gdb-current-frame (match-string 2))
2411 (let ((address (match-string 1)))
2412 ;; remove leading 0s from output of info frame command.
2413 (if (string-match "^0+\\(.*\\)" address)
2414 (setq gdb-current-address
2415 (concat "0x" (match-string 1 address)))
2416 (setq gdb-current-address (concat "0x" address))))
2417 (if (or (if (not (looking-at ".*(\\S-*:[0-9]*)"))
2418 (progn (setq gdb-view-source nil) t))
2419 (eq gdb-selected-view 'assembler))
2423 (gdb-get-create-buffer 'gdb-assembler-buffer))
2424 ;;update with new frame for machine code if necessary
2425 (gdb-invalidate-assembler)))))))
2429 ;;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2430 ;;; gdb-ui.el ends here