1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002, 2003, 2004, 2005 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, if
32 ;; required, and watches expressions in the speedbar. It also uses features of
33 ;; Emacs 21 such as the fringe/display margin for breakpoints, and the toolbar
34 ;; (see the GDB Graphical Interface section in the Emacs info manual).
36 ;; By default, M-x gdb will start the debugger. However, if you have customised
37 ;; gud-gdb-command-name, then start it with M-x gdba.
39 ;; This file has evolved from gdba.el that was included with GDB 5.0 and
40 ;; written by Tom Lord and Jim Kingdon. It uses GDB's annotation interface.
41 ;; You don't need to know about annotations to use this mode as a debugger,
42 ;; but if you are interested developing the mode itself, then see the
43 ;; Annotations section in the GDB info manual.
45 ;; GDB developers plan to make the annotation interface obsolete. A new
46 ;; interface called GDB/MI (machine interface) has been designed to replace
47 ;; it. Some GDB/MI commands are used in this file through the CLI command
48 ;; 'interpreter mi <mi-command>'. A file called gdb-mi.el is included with
49 ;; GDB (6.2 onwards) that uses GDB/MI as the primary interface to GDB. It is
50 ;; still under development and is part of a process to migrate Emacs from
51 ;; annotations to GDB/MI.
56 ;; 1) Use MI command -data-read-memory for memory window.
57 ;; 2) Highlight changed register values (use MI commands
58 ;; -data-list-register-values and -data-list-changed-registers instead
59 ;; of 'info registers'.
60 ;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
61 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
62 ;; 5) After release of 22.1 use '-var-list-children --all-values'
63 ;; and '-stack-list-locals 2' which need GDB 6.1 onwards.
69 (defvar gdb-current-address
"main" "Initialisation for Assembler buffer.")
70 (defvar gdb-previous-address nil
)
71 (defvar gdb-memory-address
"main")
72 (defvar gdb-previous-frame nil
)
73 (defvar gdb-current-frame nil
)
74 (defvar gdb-current-stack-level nil
)
75 (defvar gdb-current-language nil
)
76 (defvar gdb-var-list nil
"List of variables in watch window.")
77 (defvar gdb-var-changed nil
"Non-nil means that gdb-var-list has changed.")
78 (defvar gdb-buffer-type nil
)
79 (defvar gdb-overlay-arrow-position nil
)
80 (defvar gdb-server-prefix nil
)
81 (defvar gdb-flush-pending-output nil
)
83 (defvar gdb-buffer-type nil
84 "One of the symbols bound in `gdb-buffer-rules'.")
86 (defvar gdb-input-queue
()
87 "A list of gdb command objects.")
89 (defvar gdb-prompting nil
90 "True when gdb is idle with no pending input.")
92 (defvar gdb-output-sink
'user
93 "The disposition of the output of the current gdb command.
94 Possible values are these symbols:
96 `user' -- gdb output should be copied to the GUD buffer
99 `inferior' -- gdb output should be copied to the inferior-io buffer
101 `pre-emacs' -- output should be ignored util the post-prompt
102 annotation is received. Then the output-sink
104 `emacs' -- output should be collected in the partial-output-buffer
105 for subsequent processing by a command. This is the
106 disposition of output generated by commands that
107 gdb mode sends to gdb on its own behalf.
108 `post-emacs' -- ignore output until the prompt annotation is
109 received, then go to USER disposition.
111 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
112 \(`user' and `emacs').")
114 (defvar gdb-current-item nil
115 "The most recent command item sent to gdb.")
117 (defvar gdb-pending-triggers
'()
118 "A list of trigger functions that have run later than their output
121 ;; end of gdb variables
124 (defun gdba (command-line)
125 "Run gdb on program FILE in buffer *gud-FILE*.
126 The directory containing FILE becomes the initial working directory
127 and source-file directory for your debugger.
129 If `gdb-many-windows' is nil (the default value) then gdb just
130 pops up the GUD buffer unless `gdb-show-main' is t. In this case
131 it starts with two windows: one displaying the GUD buffer and the
132 other with the source file with the main routine of the inferior.
134 If `gdb-many-windows' is t, regardless of the value of
135 `gdb-show-main', the layout below will appear unless
136 `gdb-use-inferior-io-buffer' is nil when the source buffer
137 occupies the full width of the frame. Keybindings are given in
140 Watch expressions appear in the speedbar/slowbar.
142 The following commands help control operation :
144 `gdb-many-windows' - Toggle the number of windows gdb uses.
145 `gdb-restore-windows' - To restore the window layout.
147 See Info node `(emacs)GDB Graphical Interface' for a more
148 detailed description of this mode.
151 ---------------------------------------------------------------------
153 ---------------------------------------------------------------------
154 GUD buffer (I/O of GDB) | Locals buffer
158 ---------------------------------------------------------------------
159 Source buffer | Input/Output (of inferior) buffer
167 ---------------------------------------------------------------------
168 Stack buffer | Breakpoints buffer
169 RET gdb-frames-select | SPC gdb-toggle-breakpoint
170 | RET gdb-goto-breakpoint
171 | d gdb-delete-breakpoint
172 ---------------------------------------------------------------------"
174 (interactive (list (gud-query-cmdline 'gdba
)))
176 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
180 (defvar gdb-debug-log nil
)
182 (defcustom gdb-enable-debug-log nil
183 "Non-nil means record the process input and output in `gdb-debug-log'."
188 (defcustom gdb-use-inferior-io-buffer nil
189 "Non-nil means display output from the inferior in a separate buffer."
195 (setq gdb-debug-log nil
)
196 (set (make-local-variable 'gud-minor-mode
) 'gdba
)
197 (set (make-local-variable 'gud-marker-filter
) 'gud-gdba-marker-filter
)
199 (gud-def gud-break
(if (not (string-equal mode-name
"Machine"))
200 (gud-call "break %f:%l" arg
)
204 (gud-call "break *%a" arg
)))
205 "\C-b" "Set breakpoint at current line or address.")
207 (gud-def gud-remove
(if (not (string-equal mode-name
"Machine"))
208 (gud-call "clear %f:%l" arg
)
212 (gud-call "clear *%a" arg
)))
213 "\C-d" "Remove breakpoint at current line or address.")
215 (gud-def gud-until
(if (not (string-equal mode-name
"Machine"))
216 (gud-call "until %f:%l" arg
)
220 (gud-call "until *%a" arg
)))
221 "\C-u" "Continue to current line or address.")
223 (define-key gud-minor-mode-map
[left-margin mouse-1
]
224 'gdb-mouse-set-clear-breakpoint
)
225 (define-key gud-minor-mode-map
[left-fringe mouse-1
]
226 'gdb-mouse-set-clear-breakpoint
)
227 (define-key gud-minor-mode-map
[left-margin mouse-3
]
228 'gdb-mouse-toggle-breakpoint
)
229 ; (define-key gud-minor-mode-map [left-fringe mouse-3]
230 ; 'gdb-mouse-toggle-breakpoint)
232 (setq comint-input-sender
'gdb-send
)
235 (setq gdb-current-address
"main")
236 (setq gdb-previous-address nil
)
237 (setq gdb-memory-address
"main")
238 (setq gdb-previous-frame nil
)
239 (setq gdb-current-frame nil
)
240 (setq gdb-current-stack-level nil
)
241 (setq gdb-var-list nil
)
242 (setq gdb-var-changed nil
)
243 (setq gdb-first-prompt nil
)
244 (setq gdb-prompting nil
)
245 (setq gdb-input-queue nil
)
246 (setq gdb-current-item nil
)
247 (setq gdb-pending-triggers nil
)
248 (setq gdb-output-sink
'user
)
249 (setq gdb-server-prefix
"server ")
250 (setq gdb-flush-pending-output nil
)
251 (setq gdb-location-list nil
)
253 (setq gdb-buffer-type
'gdba
)
255 (if gdb-use-inferior-io-buffer
(gdb-clear-inferior-io))
257 (if (eq window-system
'w32
)
258 (gdb-enqueue-input (list "set new-console off\n" 'ignore
)))
259 (gdb-enqueue-input (list "set height 0\n" 'ignore
))
260 ;; find source file and compilation directory here
261 (gdb-enqueue-input (list "server list main\n" 'ignore
)) ; C program
262 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore
)) ; Fortran program
263 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info
))
265 (run-hooks 'gdba-mode-hook
))
267 (defcustom gdb-use-colon-colon-notation nil
268 "If non-nil use FUN::VAR format to display variables in the speedbar." ;
274 "Watch expression at point."
277 (let ((expr (tooltip-identifier-from-point (point))))
278 (if (and (string-equal gdb-current-language
"c")
279 gdb-use-colon-colon-notation gdb-current-frame
)
280 (setq expr
(concat gdb-current-frame
"::" expr
)))
281 (catch 'already-watched
282 (dolist (var gdb-var-list
)
283 (if (string-equal expr
(car var
)) (throw 'already-watched nil
)))
284 (set-text-properties 0 (length expr
) nil expr
)
287 (if (eq gud-minor-mode
'gdba
)
288 (concat "server interpreter mi \"-var-create - * " expr
"\"\n")
289 (concat"-var-create - * " expr
"\n"))
290 `(lambda () (gdb-var-create-handler ,expr
))))))
291 (select-window (get-buffer-window gud-comint-buffer
0)))
293 (defconst gdb-var-create-regexp
294 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
296 (defun gdb-var-create-handler (expr)
297 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
298 (goto-char (point-min))
299 (if (re-search-forward gdb-var-create-regexp nil t
)
300 (let ((var (list expr
305 (push var gdb-var-list
)
306 (setq speedbar-update-flag t
)
308 (if (equal (nth 2 var
) "0")
310 (list (concat "server interpreter mi \"-var-evaluate-expression "
312 `(lambda () (gdb-var-evaluate-expression-handler
314 (setq gdb-var-changed t
)))
315 (if (re-search-forward "Undefined command" nil t
)
316 (message "Watching expressions requires gdb 6.0 onwards")
317 (message "No symbol %s in current context." expr
)))))
319 (defun gdb-var-evaluate-expression-handler (varnum changed
)
320 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
321 (goto-char (point-min))
322 (re-search-forward ".*value=\"\\(.*?\\)\"" nil t
)
325 (dolist (var gdb-var-list
)
326 (if (string-equal varnum
(cadr var
))
328 (if changed
(setcar (nthcdr 5 var
) t
))
329 (setcar (nthcdr 4 var
) (match-string 1))
330 (setcar (nthcdr num gdb-var-list
) var
)
331 (throw 'var-found nil
)))
332 (setq num
(+ num
1))))))
333 (setq gdb-var-changed t
))
335 (defun gdb-var-list-children (varnum)
337 (list (concat "server interpreter mi \"-var-list-children " varnum
"\"\n")
338 `(lambda () (gdb-var-list-children-handler ,varnum
)))))
340 (defconst gdb-var-list-children-regexp
341 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\"")
343 (defun gdb-var-list-children-handler (varnum)
344 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
345 (goto-char (point-min))
346 (let ((var-list nil
))
347 (catch 'child-already-watched
348 (dolist (var gdb-var-list
)
349 (if (string-equal varnum
(cadr var
))
352 (while (re-search-forward gdb-var-list-children-regexp nil t
)
353 (let ((varchild (list (match-string 2)
357 (if (looking-at ",type=\"\\(.*?\\)\"")
358 (setcar (nthcdr 3 varchild
) (match-string 1)))
359 (dolist (var1 gdb-var-list
)
360 (if (string-equal (cadr var1
) (cadr varchild
))
361 (throw 'child-already-watched nil
)))
362 (push varchild var-list
)
363 (if (equal (nth 2 varchild
) "0")
367 "server interpreter mi \"-var-evaluate-expression "
368 (nth 1 varchild
) "\"\n")
369 `(lambda () (gdb-var-evaluate-expression-handler
370 ,(nth 1 varchild
) nil
))))))))
371 (push var var-list
)))
372 (setq gdb-var-list
(nreverse var-list
))))))
374 (defun gdb-var-update ()
375 (if (not (member 'gdb-var-update gdb-pending-triggers
))
379 (if (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
))
380 "server interpreter mi \"-var-update *\"\n"
382 'gdb-var-update-handler
))
383 (push 'gdb-var-update gdb-pending-triggers
))))
385 (defconst gdb-var-update-regexp
"name=\"\\(.*?\\)\"")
387 (defun gdb-var-update-handler ()
388 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
389 (goto-char (point-min))
390 (while (re-search-forward gdb-var-update-regexp nil t
)
391 (let ((varnum (match-string 1)))
394 (if (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
))
395 (concat "server interpreter mi \"-var-evaluate-expression "
397 (concat "-var-evaluate-expression " varnum
"\n"))
398 `(lambda () (gdb-var-evaluate-expression-handler
400 (setq gdb-pending-triggers
401 (delq 'gdb-var-update gdb-pending-triggers
)))
403 (defun gdb-var-delete ()
404 "Delete watched expression from the speedbar."
406 (if (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
))
407 (let ((text (speedbar-line-text)))
408 (string-match "\\(\\S-+\\)" text
)
409 (let* ((expr (match-string 1 text
))
410 (var (assoc expr gdb-var-list
))
412 (unless (string-match "\\." varnum
)
415 (if (with-current-buffer gud-comint-buffer
416 (eq gud-minor-mode
'gdba
))
417 (concat "server interpreter mi \"-var-delete " varnum
"\"\n")
418 (concat "-var-delete " varnum
"\n"))
420 (setq gdb-var-list
(delq var gdb-var-list
))
421 (dolist (varchild gdb-var-list
)
422 (if (string-match (concat (nth 1 var
) "\\.") (nth 1 varchild
))
423 (setq gdb-var-list
(delq varchild gdb-var-list
))))
424 (setq gdb-var-changed t
))))))
426 (defun gdb-edit-value (text token indent
)
427 "Assign a value to a variable displayed in the speedbar."
428 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list
))
429 (varnum (cadr var
)) (value))
430 (setq value
(read-string "New value: "))
433 (if (with-current-buffer gud-comint-buffer
434 (eq gud-minor-mode
'gdba
))
435 (concat "server interpreter mi \"-var-assign " varnum
" " value
"\"\n")
436 (concat "-var-assign " varnum
" " value
"\n"))
439 (defcustom gdb-show-changed-values t
440 "If non-nil highlight values that have recently changed in the speedbar.
441 The highlighting is done with `font-lock-warning-face'."
446 (defun gdb-speedbar-expand-node (text token indent
)
447 "Expand the node the user clicked on.
448 TEXT is the text of the button we clicked on, a + or - item.
449 TOKEN is data related to this node.
450 INDENT is the current indentation depth."
451 (cond ((string-match "+" text
) ;expand this node
452 (if (with-current-buffer gud-comint-buffer
(eq gud-minor-mode
'gdba
))
453 (gdb-var-list-children token
)
454 (gdbmi-var-list-children token
)))
455 ((string-match "-" text
) ;contract this node
456 (dolist (var gdb-var-list
)
457 (if (string-match (concat token
"\\.") (nth 1 var
))
458 (setq gdb-var-list
(delq var gdb-var-list
))))
459 (setq gdb-var-changed t
))))
461 (defun gdb-get-target-string ()
462 (with-current-buffer gud-comint-buffer
469 ;; Each buffer has a TYPE -- a symbol that identifies the function
470 ;; of that particular buffer.
472 ;; The usual gdb interaction buffer is given the type `gdba' and
473 ;; is constructed specially.
475 ;; Others are constructed by gdb-get-create-buffer and
476 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
478 (defvar gdb-buffer-rules-assoc
'())
480 (defun gdb-get-buffer (key)
481 "Return the gdb buffer tagged with type KEY.
482 The key should be one of the cars in `gdb-buffer-rules-assoc'."
484 (gdb-look-for-tagged-buffer key
(buffer-list))))
486 (defun gdb-get-create-buffer (key)
487 "Create a new gdb buffer of the type specified by KEY.
488 The key should be one of the cars in `gdb-buffer-rules-assoc'."
489 (or (gdb-get-buffer key
)
490 (let* ((rules (assoc key gdb-buffer-rules-assoc
))
491 (name (funcall (gdb-rules-name-maker rules
)))
492 (new (get-buffer-create name
)))
493 (with-current-buffer new
495 (if (cdr (cdr rules
))
496 (setq trigger
(funcall (car (cdr (cdr rules
))))))
497 (set (make-local-variable 'gdb-buffer-type
) key
)
498 (set (make-local-variable 'gud-minor-mode
)
499 (with-current-buffer gud-comint-buffer gud-minor-mode
))
500 (set (make-local-variable 'tool-bar-map
) gud-tool-bar-map
)
501 (if trigger
(funcall trigger
)))
504 (defun gdb-rules-name-maker (rules) (car (cdr rules
)))
506 (defun gdb-look-for-tagged-buffer (key bufs
)
508 (while (and (not retval
) bufs
)
509 (set-buffer (car bufs
))
510 (if (eq gdb-buffer-type key
)
511 (setq retval
(car bufs
)))
512 (setq bufs
(cdr bufs
)))
516 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
517 ;; at least one and possible more functions. The functions have these
518 ;; roles in defining a buffer type:
520 ;; NAME - Return a name for this buffer type.
522 ;; The remaining function(s) are optional:
524 ;; MODE - called in a new buffer with no arguments, should establish
525 ;; the proper mode for the buffer.
528 (defun gdb-set-buffer-rules (buffer-type &rest rules
)
529 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc
)))
531 (setcdr binding rules
)
532 (push (cons buffer-type rules
)
533 gdb-buffer-rules-assoc
))))
535 ;; GUD buffers are an exception to the rules
536 (gdb-set-buffer-rules 'gdba
'error
)
539 ;; Partial-output buffer : This accumulates output from a command executed on
540 ;; behalf of emacs (rather than the user).
542 (gdb-set-buffer-rules 'gdb-partial-output-buffer
543 'gdb-partial-output-name
)
545 (defun gdb-partial-output-name ()
546 (concat "*partial-output-"
547 (gdb-get-target-string)
551 (gdb-set-buffer-rules 'gdb-inferior-io
552 'gdb-inferior-io-name
553 'gdb-inferior-io-mode
)
555 (defun gdb-inferior-io-name ()
556 (concat "*input/output of "
557 (gdb-get-target-string)
560 (defvar gdb-inferior-io-mode-map
561 (let ((map (make-sparse-keymap)))
562 (define-key map
"\C-c\C-c" 'gdb-inferior-io-interrupt
)
563 (define-key map
"\C-c\C-z" 'gdb-inferior-io-stop
)
564 (define-key map
"\C-c\C-\\" 'gdb-inferior-io-quit
)
565 (define-key map
"\C-c\C-d" 'gdb-inferior-io-eof
)
568 (define-derived-mode gdb-inferior-io-mode comint-mode
"Inferior I/O"
569 "Major mode for gdb inferior-io."
570 :syntax-table nil
:abbrev-table nil
571 ;; We want to use comint because it has various nifty and familiar
572 ;; features. We don't need a process, but comint wants one, so create
574 (make-comint-in-buffer
575 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
576 (current-buffer) "hexl")
577 (setq comint-input-sender
'gdb-inferior-io-sender
))
579 (defun gdb-inferior-io-sender (proc string
)
580 ;; PROC is the pseudo-process created to satisfy comint.
581 (with-current-buffer (process-buffer proc
)
582 (setq proc
(get-buffer-process gud-comint-buffer
))
583 (process-send-string proc string
)
584 (process-send-string proc
"\n")))
586 (defun gdb-inferior-io-interrupt ()
587 "Interrupt the program being debugged."
590 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
592 (defun gdb-inferior-io-quit ()
593 "Send quit signal to the program being debugged."
596 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
598 (defun gdb-inferior-io-stop ()
599 "Stop the program being debugged."
602 (get-buffer-process gud-comint-buffer
) comint-ptyp
))
604 (defun gdb-inferior-io-eof ()
605 "Send end-of-file to the program being debugged."
608 (get-buffer-process gud-comint-buffer
)))
612 ;; gdb communications
615 ;; INPUT: things sent to gdb
617 ;; The queues are lists. Each element is either a string (indicating user or
618 ;; user-like input) or a list of the form:
620 ;; (INPUT-STRING HANDLER-FN)
622 ;; The handler function will be called from the partial-output buffer when the
623 ;; command completes. This is the way to write commands which invoke gdb
624 ;; commands autonomously.
626 ;; These lists are consumed tail first.
629 (defun gdb-send (proc string
)
630 "A comint send filter for gdb.
631 This filter may simply queue input for a later time."
633 (process-send-string proc
(concat string
"\n"))
634 (gdb-enqueue-input (concat string
"\n"))))
636 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
637 ;; is a query, or other non-top-level prompt.
639 (defun gdb-enqueue-input (item)
643 (setq gdb-prompting nil
))
644 (push item gdb-input-queue
)))
646 (defun gdb-dequeue-input ()
647 (let ((queue gdb-input-queue
))
649 (let ((last (car (last queue
))))
650 (unless (nbutlast queue
) (setq gdb-input-queue
'()))
653 (defun gdb-send-item (item)
654 (setq gdb-flush-pending-output nil
)
655 (if gdb-enable-debug-log
(push (cons 'send item
) gdb-debug-log
))
656 (setq gdb-current-item item
)
657 (with-current-buffer gud-comint-buffer
658 (if (eq gud-minor-mode
'gdba
)
661 (setq gdb-output-sink
'user
)
662 (process-send-string (get-buffer-process gud-comint-buffer
) item
))
664 (gdb-clear-partial-output)
665 (setq gdb-output-sink
'pre-emacs
)
666 (process-send-string (get-buffer-process gud-comint-buffer
)
668 ;; case: eq gud-minor-mode 'gdbmi
669 (gdb-clear-partial-output)
670 (setq gdb-output-sink
'emacs
)
671 (process-send-string (get-buffer-process gud-comint-buffer
)
675 ;; output -- things gdb prints to emacs
677 ;; GDB output is a stream interrupted by annotations.
678 ;; Annotations can be recognized by their beginning
679 ;; with \C-j\C-z\C-z<tag><opt>\C-j
681 ;; The tag is a string obeying symbol syntax.
683 ;; The optional part `<opt>' can be either the empty string
684 ;; or a space followed by more data relating to the annotation.
685 ;; For example, the SOURCE annotation is followed by a filename,
686 ;; line number and various useless goo. This data must not include
690 (defcustom gud-gdba-command-name
"gdb -annotate=3"
691 "Default command to execute an executable under the GDB-UI debugger."
696 (defvar gdb-annotation-rules
697 '(("pre-prompt" gdb-pre-prompt
)
698 ("prompt" gdb-prompt
)
699 ("commands" gdb-subprompt
)
700 ("overload-choice" gdb-subprompt
)
701 ("query" gdb-subprompt
)
702 ;; Need this prompt for GDB 6.1
703 ("nquery" gdb-subprompt
)
704 ("prompt-for-continue" gdb-subprompt
)
705 ("post-prompt" gdb-post-prompt
)
706 ("source" gdb-source
)
707 ("starting" gdb-starting
)
708 ("exited" gdb-stopping
)
709 ("signalled" gdb-stopping
)
710 ("signal" gdb-stopping
)
711 ("breakpoint" gdb-stopping
)
712 ("watchpoint" gdb-stopping
)
713 ("frame-begin" gdb-frame-begin
)
714 ("stopped" gdb-stopped
)
715 ) "An assoc mapping annotation tags to functions which process them.")
718 (setq gdb-flush-pending-output t
)
719 (setq gud-running nil
)
720 (setq gdb-output-sink
'user
)
721 (setq gdb-input-queue nil
)
722 (setq gdb-pending-triggers nil
)
723 (setq gdb-prompting t
))
725 (defconst gdb-source-spec-regexp
726 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
728 ;; Do not use this except as an annotation handler.
729 (defun gdb-source (args)
730 (string-match gdb-source-spec-regexp args
)
731 ;; Extract the frame position from the marker.
734 (match-string 1 args
)
735 (string-to-int (match-string 2 args
))))
736 (setq gdb-current-address
(match-string 3 args
))
737 ;; cover for auto-display output which comes *before*
738 ;; stopped annotation
739 (if (eq gdb-output-sink
'inferior
) (setq gdb-output-sink
'user
)))
741 (defun gdb-pre-prompt (ignored)
742 "An annotation handler for `pre-prompt'.
743 This terminates the collection of output from a previous command if that
744 happens to be in effect."
745 (let ((sink gdb-output-sink
))
749 (setq gdb-output-sink
'post-emacs
))
752 (error "Phase error in gdb-pre-prompt (got %s)" sink
)))))
754 (defun gdb-prompt (ignored)
755 "An annotation handler for `prompt'.
756 This sends the next command (if any) to gdb."
757 (when gdb-first-prompt
(gdb-ann3))
758 (let ((sink gdb-output-sink
))
761 ((eq sink
'post-emacs
)
762 (setq gdb-output-sink
'user
)
764 (car (cdr gdb-current-item
))))
765 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
769 (error "Phase error in gdb-prompt (got %s)" sink
))))
770 (let ((input (gdb-dequeue-input)))
772 (gdb-send-item input
)
774 (setq gdb-prompting t
)
775 (gud-display-frame)))))
777 (defun gdb-subprompt (ignored)
778 "An annotation handler for non-top-level prompts."
779 (setq gdb-prompting t
))
781 (defun gdb-starting (ignored)
782 "An annotation handler for `starting'.
783 This says that I/O for the subprocess is now the program being debugged,
785 (let ((sink gdb-output-sink
))
790 (if gdb-use-inferior-io-buffer
791 (setq gdb-output-sink
'inferior
))))
794 (error "Unexpected `starting' annotation")))))
796 (defun gdb-stopping (ignored)
797 "An annotation handler for `exited' and other annotations.
798 They say that I/O for the subprocess is now GDB, not the program
800 (if gdb-use-inferior-io-buffer
801 (let ((sink gdb-output-sink
))
804 (setq gdb-output-sink
'user
))
807 (error "Unexpected stopping annotation"))))))
809 (defun gdb-frame-begin (ignored)
810 (let ((sink gdb-output-sink
))
813 (setq gdb-output-sink
'user
))
818 (error "Unexpected frame-begin annotation (%S)" sink
)))))
820 (defun gdb-stopped (ignored)
821 "An annotation handler for `stopped'.
822 It is just like `gdb-stopping', except that if we already set the output
823 sink to `user' in `gdb-stopping', that is fine."
824 (setq gud-running nil
)
825 (let ((sink gdb-output-sink
))
828 (setq gdb-output-sink
'user
))
832 (error "Unexpected stopped annotation")))))
834 (defun gdb-post-prompt (ignored)
835 "An annotation handler for `post-prompt'.
836 This begins the collection of output from the current command if that
837 happens to be appropriate."
838 (if (not gdb-pending-triggers
)
840 (gdb-get-current-frame)
841 (gdb-invalidate-frames)
842 (gdb-invalidate-breakpoints)
843 (gdb-invalidate-assembler)
844 (gdb-invalidate-registers)
845 (gdb-invalidate-memory)
846 (gdb-invalidate-locals)
847 (gdb-invalidate-threads)
848 (unless (eq system-type
'darwin
) ;Breaks on Darwin's GDB-5.3.
849 ;; FIXME: with GDB-6 on Darwin, this might very well work.
850 (dolist (frame (frame-list))
851 (when (string-equal (frame-parameter frame
'name
) "Speedbar")
852 (setq gdb-var-changed t
) ; force update
853 (dolist (var gdb-var-list
)
854 (setcar (nthcdr 5 var
) nil
))))
856 (let ((sink gdb-output-sink
))
859 ((eq sink
'pre-emacs
)
860 (setq gdb-output-sink
'emacs
))
863 (error "Phase error in gdb-post-prompt (got %s)" sink
)))))
865 (defun gud-gdba-marker-filter (string)
866 "A gud marker filter for gdb. Handle a burst of output from GDB."
867 (if gdb-flush-pending-output
869 (if gdb-enable-debug-log
(push (cons 'recv string
) gdb-debug-log
))
870 ;; Recall the left over gud-marker-acc from last time
871 (setq gud-marker-acc
(concat gud-marker-acc string
))
872 ;; Start accumulating output for the GUD buffer
875 ;; Process all the complete markers in this chunk.
876 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc
)
877 (let ((annotation (match-string 1 gud-marker-acc
)))
879 ;; Stuff prior to the match is just ordinary output.
880 ;; It is either concatenated to OUTPUT or directed
885 (substring gud-marker-acc
0 (match-beginning 0))))
887 ;; Take that stuff off the gud-marker-acc.
888 (setq gud-marker-acc
(substring gud-marker-acc
(match-end 0)))
890 ;; Parse the tag from the annotation, and maybe its arguments.
891 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation
)
892 (let* ((annotation-type (match-string 1 annotation
))
893 (annotation-arguments (match-string 2 annotation
))
894 (annotation-rule (assoc annotation-type
895 gdb-annotation-rules
)))
896 ;; Call the handler for this annotation.
898 (funcall (car (cdr annotation-rule
))
899 annotation-arguments
)
900 ;; Else the annotation is not recognized. Ignore it silently,
901 ;; so that GDB can add new annotations without causing
905 ;; Does the remaining text end in a partial line?
906 ;; If it does, then keep part of the gud-marker-acc until we get more.
907 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
910 ;; Everything before the potential marker start can be output.
912 (gdb-concat-output output
913 (substring gud-marker-acc
0
914 (match-beginning 0))))
916 ;; Everything after, we save, to combine with later input.
917 (setq gud-marker-acc
(substring gud-marker-acc
(match-beginning 0))))
919 ;; In case we know the gud-marker-acc contains no partial annotations:
921 (setq output
(gdb-concat-output output gud-marker-acc
))
922 (setq gud-marker-acc
"")))
925 (defun gdb-concat-output (so-far new
)
926 (let ((sink gdb-output-sink
))
928 ((eq sink
'user
) (concat so-far new
))
929 ((or (eq sink
'pre-emacs
) (eq sink
'post-emacs
)) so-far
)
931 (gdb-append-to-partial-output new
)
934 (gdb-append-to-inferior-io new
)
938 (error "Bogon output sink %S" sink
)))))
940 (defun gdb-append-to-partial-output (string)
941 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
942 (goto-char (point-max))
945 (defun gdb-clear-partial-output ()
946 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer
)
949 (defun gdb-append-to-inferior-io (string)
950 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io
)
951 (goto-char (point-max))
952 (insert-before-markers string
))
953 (if (not (string-equal string
""))
954 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io
))))
956 (defun gdb-clear-inferior-io ()
957 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io
)
961 ;; One trick is to have a command who's output is always available in a buffer
962 ;; of it's own, and is always up to date. We build several buffers of this
965 ;; There are two aspects to this: gdb has to tell us when the output for that
966 ;; command might have changed, and we have to be able to run the command
967 ;; behind the user's back.
969 ;; The output phasing associated with the variable gdb-output-sink
970 ;; help us to run commands behind the user's back.
972 ;; Below is the code for specificly managing buffers of output from one
976 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
977 ;; It adds an input for the command we are tracking. It should be the
978 ;; annotation rule binding of whatever gdb sends to tell us this command
979 ;; might have changed it's output.
981 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
982 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
983 ;; input in the input queue (see comment about ``gdb communications'' above).
985 (defmacro def-gdb-auto-update-trigger
(name demand-predicate gdb-command
987 `(defun ,name
(&optional ignored
)
988 (if (and (,demand-predicate
)
990 gdb-pending-triggers
)))
993 (list ,gdb-command
',output-handler
))
994 (push ',name gdb-pending-triggers
)))))
996 (defmacro def-gdb-auto-update-handler
(name trigger buf-key custom-defun
)
998 (setq gdb-pending-triggers
1000 gdb-pending-triggers
))
1001 (let ((buf (gdb-get-buffer ',buf-key
)))
1003 (with-current-buffer buf
1005 (buffer-read-only nil
))
1007 (insert-buffer-substring (gdb-get-create-buffer
1008 'gdb-partial-output-buffer
))
1010 ;; put customisation here
1013 (defmacro def-gdb-auto-updated-buffer
(buffer-key trigger-name gdb-command
1014 output-handler-name custom-defun
)
1016 (def-gdb-auto-update-trigger ,trigger-name
1017 ;; The demand predicate:
1018 (lambda () (gdb-get-buffer ',buffer-key
))
1020 ,output-handler-name
)
1021 (def-gdb-auto-update-handler ,output-handler-name
1022 ,trigger-name
,buffer-key
,custom-defun
)))
1026 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1028 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1029 'gdb-breakpoints-buffer-name
1030 'gdb-breakpoints-mode
)
1032 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1033 ;; This defines the auto update rule for buffers of type
1034 ;; `gdb-breakpoints-buffer'.
1036 ;; It defines a function to serve as the annotation handler that
1037 ;; handles the `foo-invalidated' message. That function is called:
1038 gdb-invalidate-breakpoints
1040 ;; To update the buffer, this command is sent to gdb.
1041 "server info breakpoints\n"
1043 ;; This also defines a function to be the handler for the output
1044 ;; from the command above. That function will copy the output into
1045 ;; the appropriately typed buffer. That function will be called:
1046 gdb-info-breakpoints-handler
1047 ;; buffer specific functions
1048 gdb-info-breakpoints-custom
)
1050 (defvar gdb-location-list nil
"List of directories for source files.")
1052 (defconst breakpoint-xpm-data
1054 static char *magick[] = {
1055 /* columns rows colors chars-per-pixel */
1071 "XPM data used for breakpoint icon.")
1073 (defconst breakpoint-enabled-pbm-data
1076 0 0 0 0 1 1 1 1 0 0 0 0
1077 0 0 0 1 1 1 1 1 1 0 0 0
1078 0 0 1 1 1 1 1 1 1 1 0 0
1079 0 1 1 1 1 1 1 1 1 1 1 0
1080 0 1 1 1 1 1 1 1 1 1 1 0
1081 0 1 1 1 1 1 1 1 1 1 1 0
1082 0 1 1 1 1 1 1 1 1 1 1 0
1083 0 0 1 1 1 1 1 1 1 1 0 0
1084 0 0 0 1 1 1 1 1 1 0 0 0
1085 0 0 0 0 1 1 1 1 0 0 0 0"
1086 "PBM data used for enabled breakpoint icon.")
1088 (defconst breakpoint-disabled-pbm-data
1100 0 0 0 1 0 1 0 1 0 0"
1101 "PBM data used for disabled breakpoint icon.")
1103 (defvar breakpoint-enabled-icon nil
1104 "Icon for enabled breakpoint in display margin.")
1106 (defvar breakpoint-disabled-icon nil
1107 "Icon for disabled breakpoint in display margin.")
1109 ;; Bitmap for breakpoint in fringe
1110 (and (display-images-p)
1111 (define-fringe-bitmap 'breakpoint
1112 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1114 (defface breakpoint-enabled-bitmap-face
1118 "Face for enabled breakpoint icon in fringe."
1121 (defface breakpoint-disabled-bitmap-face
1124 :foreground
"grey60"))
1125 "Face for disabled breakpoint icon in fringe."
1129 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1130 (defun gdb-info-breakpoints-custom ()
1131 (let ((flag) (bptno))
1132 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1133 (dolist (buffer (buffer-list))
1134 (with-current-buffer buffer
1135 (if (and (eq gud-minor-mode
'gdba
)
1136 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1137 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1138 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer
)
1140 (goto-char (point-min))
1141 (while (< (point) (- (point-max) 1))
1143 (if (looking-at "[^\t].*breakpoint")
1145 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1146 (setq bptno
(match-string 1))
1147 (setq flag
(char-after (match-beginning 2)))
1149 (if (re-search-forward " in .* at\\s-+" nil t
)
1151 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1152 (let ((line (match-string 2)) (buffer-read-only nil
)
1153 (file (match-string 1)))
1154 (add-text-properties (point-at-bol) (point-at-eol)
1155 '(mouse-face highlight
1156 help-echo
"mouse-2, RET: visit breakpoint"))
1157 (unless (file-exists-p file
)
1158 (setq file
(cdr (assoc bptno gdb-location-list
))))
1159 (unless (string-equal file
"File not found")
1161 (with-current-buffer
1162 (find-file-noselect file
)
1163 (save-current-buffer
1164 (set (make-local-variable 'gud-minor-mode
)
1166 (set (make-local-variable 'tool-bar-map
)
1168 ;; only want one breakpoint icon at each location
1170 (goto-line (string-to-number line
))
1171 (gdb-put-breakpoint-icon (eq flag ?y
) bptno
)))
1173 (list (concat gdb-server-prefix
"list "
1174 (match-string-no-properties 1) ":1\n")
1177 (list (concat gdb-server-prefix
"info source\n")
1180 ,bptno
,line
,flag
)))))))))))
1182 (if (gdb-get-buffer 'gdb-assembler-buffer
) (gdb-assembler-custom)))
1184 (defun gdb-mouse-set-clear-breakpoint (event)
1185 "Set/clear breakpoint in left fringe/margin with mouse click."
1187 (mouse-minibuffer-check event
)
1188 (let ((posn (event-end event
)))
1189 (if (numberp (posn-point posn
))
1190 (with-selected-window (posn-window posn
)
1192 (goto-char (posn-point posn
))
1193 (if (or (posn-object posn
)
1194 (eq (car (fringe-bitmaps-at-pos (posn-point posn
)))
1197 (gud-break nil
)))))))
1199 (defun gdb-mouse-toggle-breakpoint (event)
1200 "Enable/disable breakpoint in left fringe/margin with mouse click."
1202 (mouse-minibuffer-check event
)
1203 (let ((posn (event-end event
)))
1204 (if (numberp (posn-point posn
))
1205 (with-selected-window (posn-window posn
)
1207 (goto-char (posn-point posn
))
1211 ; (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1215 (let ((bptno (get-text-property
1216 0 'gdb-bptno
(car (posn-string posn
)))))
1218 (if (get-text-property
1219 0 'gdb-enabled
(car (posn-string posn
)))
1222 bptno
"\n")) 'ignore
))))))))
1224 (defun gdb-breakpoints-buffer-name ()
1225 (with-current-buffer gud-comint-buffer
1226 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1228 (defun gdb-display-breakpoints-buffer ()
1229 "Display status of user-settable breakpoints."
1232 (gdb-get-create-buffer 'gdb-breakpoints-buffer
)))
1234 (defconst gdb-frame-parameters
1235 '((height .
14) (width .
80)
1237 (tool-bar-lines . nil
)
1238 (menu-bar-lines . nil
)
1239 (minibuffer . nil
)))
1241 (defun gdb-frame-breakpoints-buffer ()
1242 "Display status of user-settable breakpoints in a new frame."
1244 (let ((special-display-regexps (append special-display-regexps
'(".*")))
1245 (special-display-frame-alist gdb-frame-parameters
))
1246 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer
))))
1248 (defvar gdb-breakpoints-mode-map
1249 (let ((map (make-sparse-keymap))
1250 (menu (make-sparse-keymap "Breakpoints")))
1251 (define-key menu
[toggle] '("Toggle" . gdb-toggle-breakpoint))
1252 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1253 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1255 (suppress-keymap map)
1256 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1257 (define-key map " " 'gdb-toggle-breakpoint)
1258 (define-key map "d" 'gdb-delete-breakpoint)
1259 (define-key map "q" 'kill-this-buffer)
1260 (define-key map "\r" 'gdb-goto-breakpoint)
1261 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1264 (defun gdb-breakpoints-mode ()
1265 "Major mode for gdb breakpoints.
1267 \\{gdb-breakpoints-mode-map}"
1268 (kill-all-local-variables)
1269 (setq major-mode 'gdb-breakpoints-mode)
1270 (setq mode-name "Breakpoints")
1271 (use-local-map gdb-breakpoints-mode-map)
1272 (setq buffer-read-only t)
1273 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1274 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1275 'gdb-invalidate-breakpoints
1276 'gdbmi-invalidate-breakpoints))
1278 (defun gdb-toggle-breakpoint ()
1279 "Enable/disable breakpoint at current line."
1282 (beginning-of-line 1)
1283 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1284 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1286 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1289 (concat gdb-server-prefix
1290 (if (eq ?y (char-after (match-beginning 2)))
1293 (match-string 1) "\n") 'ignore))
1294 (error "Not recognized as break/watchpoint line"))))
1296 (defun gdb-delete-breakpoint ()
1297 "Delete the breakpoint at current line."
1299 (beginning-of-line 1)
1300 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1301 (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)")
1303 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\S-*:[0-9]+"))
1306 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1307 (error "Not recognized as break/watchpoint line")))
1309 (defun gdb-goto-breakpoint (&optional event)
1310 "Display the breakpoint location specified at current line."
1311 (interactive (list last-input-event))
1312 (if event (mouse-set-point event))
1314 (beginning-of-line 1)
1315 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1316 (looking-at "\\([0-9]+\\) .* in .* at\\s-+\\(\\S-*\\):\\([0-9]+\\)")
1318 "\\([0-9]+\\)\\s-*\\S-*\\s-*\\S-*\\s-*.\\s-*\\S-*\\s-*\\(\\S-*\\):\\([0-9]+\\)"))
1319 (let ((bptno (match-string 1))
1320 (file (match-string 2))
1321 (line (match-string 3)))
1322 (save-selected-window
1323 (let* ((buf (find-file-noselect (if (file-exists-p file)
1325 (cdr (assoc bptno gdb-location-list)))))
1326 (window (display-buffer buf)))
1327 (with-current-buffer buf
1328 (goto-line (string-to-number line))
1329 (set-window-point window (point))))))
1330 (error "Not recognized as break/watchpoint line"))))
1333 ;; Frames buffer. This displays a perpetually correct bactracktrace
1334 ;; (from the command `where').
1336 ;; Alas, if your stack is deep, it is costly.
1338 (gdb-set-buffer-rules 'gdb-stack-buffer
1339 'gdb-stack-buffer-name
1342 (def-gdb-auto-updated-buffer gdb-stack-buffer
1343 gdb-invalidate-frames
1345 gdb-info-frames-handler
1346 gdb-info-frames-custom)
1348 (defun gdb-info-frames-custom ()
1349 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1351 (let ((buffer-read-only nil))
1352 (goto-char (point-min))
1353 (while (< (point) (point-max))
1354 (add-text-properties (point-at-bol) (point-at-eol)
1355 '(mouse-face highlight
1356 help-echo "mouse-2, RET: Select frame"))
1358 (when (and (looking-at "^#\\([0-9]+\\)")
1359 (equal (match-string 1) gdb-current-stack-level))
1360 (put-text-property (point-at-bol) (point-at-eol)
1361 'face '(:inverse-video t)))
1362 (forward-line 1))))))
1364 (defun gdb-stack-buffer-name ()
1365 (with-current-buffer gud-comint-buffer
1366 (concat "*stack frames of " (gdb-get-target-string) "*")))
1368 (defun gdb-display-stack-buffer ()
1369 "Display backtrace of current stack."
1372 (gdb-get-create-buffer 'gdb-stack-buffer)))
1374 (defun gdb-frame-stack-buffer ()
1375 "Display backtrace of current stack in a new frame."
1377 (let ((special-display-regexps (append special-display-regexps '(".*")))
1378 (special-display-frame-alist gdb-frame-parameters))
1379 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1381 (defvar gdb-frames-mode-map
1382 (let ((map (make-sparse-keymap)))
1383 (suppress-keymap map)
1384 (define-key map "q" 'kill-this-buffer)
1385 (define-key map "\r" 'gdb-frames-select)
1386 (define-key map [mouse-2] 'gdb-frames-select)
1389 (defun gdb-frames-mode ()
1390 "Major mode for gdb frames.
1392 \\{gdb-frames-mode-map}"
1393 (kill-all-local-variables)
1394 (setq major-mode 'gdb-frames-mode)
1395 (setq mode-name "Frames")
1396 (setq buffer-read-only t)
1397 (use-local-map gdb-frames-mode-map)
1399 (run-mode-hooks 'gdb-frames-mode-hook)
1400 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1401 'gdb-invalidate-frames
1402 'gdbmi-invalidate-frames))
1404 (defun gdb-get-frame-number ()
1406 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1407 (n (or (and pos (match-string-no-properties 1)) "0")))
1410 (defun gdb-frames-select (&optional event)
1411 "Select the frame and display the relevant source."
1412 (interactive (list last-input-event))
1413 (if event (mouse-set-point event))
1415 (list (concat gdb-server-prefix "frame " (gdb-get-frame-number) "\n") 'ignore))
1416 (gud-display-frame))
1419 ;; Threads buffer. This displays a selectable thread list.
1421 (gdb-set-buffer-rules 'gdb-threads-buffer
1422 'gdb-threads-buffer-name
1425 (def-gdb-auto-updated-buffer gdb-threads-buffer
1426 gdb-invalidate-threads
1427 (concat gdb-server-prefix "info threads\n")
1428 gdb-info-threads-handler
1429 gdb-info-threads-custom)
1431 (defun gdb-info-threads-custom ()
1432 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1433 (let ((buffer-read-only nil))
1434 (goto-char (point-min))
1435 (while (< (point) (point-max))
1436 (add-text-properties (point-at-bol) (point-at-eol)
1437 '(mouse-face highlight
1438 help-echo "mouse-2, RET: select thread"))
1439 (forward-line 1)))))
1441 (defun gdb-threads-buffer-name ()
1442 (with-current-buffer gud-comint-buffer
1443 (concat "*threads of " (gdb-get-target-string) "*")))
1445 (defun gdb-display-threads-buffer ()
1446 "Display IDs of currently known threads."
1449 (gdb-get-create-buffer 'gdb-threads-buffer)))
1451 (defun gdb-frame-threads-buffer ()
1452 "Display IDs of currently known threads in a new frame."
1454 (let ((special-display-regexps (append special-display-regexps '(".*")))
1455 (special-display-frame-alist gdb-frame-parameters))
1456 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1458 (defvar gdb-threads-mode-map
1459 (let ((map (make-sparse-keymap)))
1460 (suppress-keymap map)
1461 (define-key map "q" 'kill-this-buffer)
1462 (define-key map "\r" 'gdb-threads-select)
1463 (define-key map [mouse-2] 'gdb-threads-select)
1466 (defun gdb-threads-mode ()
1467 "Major mode for gdb frames.
1469 \\{gdb-threads-mode-map}"
1470 (kill-all-local-variables)
1471 (setq major-mode 'gdb-threads-mode)
1472 (setq mode-name "Threads")
1473 (setq buffer-read-only t)
1474 (use-local-map gdb-threads-mode-map)
1475 (run-mode-hooks 'gdb-threads-mode-hook)
1476 'gdb-invalidate-threads)
1478 (defun gdb-get-thread-number ()
1480 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1481 (match-string-no-properties 1)))
1483 (defun gdb-threads-select (&optional event)
1484 "Select the thread and display the relevant source."
1485 (interactive (list last-input-event))
1486 (if event (mouse-set-point event))
1488 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1489 (gud-display-frame))
1492 ;; Registers buffer.
1494 (gdb-set-buffer-rules 'gdb-registers-buffer
1495 'gdb-registers-buffer-name
1496 'gdb-registers-mode)
1498 (def-gdb-auto-updated-buffer gdb-registers-buffer
1499 gdb-invalidate-registers
1500 (concat gdb-server-prefix "info registers\n")
1501 gdb-info-registers-handler
1502 gdb-info-registers-custom)
1504 (defun gdb-info-registers-custom ())
1506 (defvar gdb-registers-mode-map
1507 (let ((map (make-sparse-keymap)))
1508 (suppress-keymap map)
1509 (define-key map "q" 'kill-this-buffer)
1512 (defun gdb-registers-mode ()
1513 "Major mode for gdb registers.
1515 \\{gdb-registers-mode-map}"
1516 (kill-all-local-variables)
1517 (setq major-mode 'gdb-registers-mode)
1518 (setq mode-name "Registers")
1519 (setq buffer-read-only t)
1520 (use-local-map gdb-registers-mode-map)
1521 (run-mode-hooks 'gdb-registers-mode-hook)
1522 'gdb-invalidate-registers)
1524 (defun gdb-registers-buffer-name ()
1525 (with-current-buffer gud-comint-buffer
1526 (concat "*registers of " (gdb-get-target-string) "*")))
1528 (defun gdb-display-registers-buffer ()
1529 "Display integer register contents."
1532 (gdb-get-create-buffer 'gdb-registers-buffer)))
1534 (defun gdb-frame-registers-buffer ()
1535 "Display integer register contents in a new frame."
1537 (let ((special-display-regexps (append special-display-regexps '(".*")))
1538 (special-display-frame-alist gdb-frame-parameters))
1539 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1543 (defcustom gdb-memory-repeat-count 32
1544 "Number of data items in memory window."
1549 (defcustom gdb-memory-format "x"
1550 "Display format of data items in memory window."
1551 :type '(choice (const :tag "Hexadecimal" "x")
1552 (const :tag "Signed decimal" "d")
1553 (const :tag "Unsigned decimal" "u")
1554 (const :tag "Octal" "o")
1555 (const :tag "Binary" "t"))
1559 (defcustom gdb-memory-unit "w"
1560 "Unit size of data items in memory window."
1561 :type '(choice (const :tag "Byte" "b")
1562 (const :tag "Halfword" "h")
1563 (const :tag "Word" "w")
1564 (const :tag "Giant word" "g"))
1568 (gdb-set-buffer-rules 'gdb-memory-buffer
1569 'gdb-memory-buffer-name
1572 (def-gdb-auto-updated-buffer gdb-memory-buffer
1573 gdb-invalidate-memory
1574 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1575 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1576 gdb-read-memory-handler
1577 gdb-read-memory-custom)
1579 (defun gdb-read-memory-custom ())
1581 (defvar gdb-memory-mode-map
1582 (let ((map (make-sparse-keymap)))
1583 (suppress-keymap map)
1584 (define-key map "q" 'kill-this-buffer)
1587 (defun gdb-memory-set-address (event)
1588 "Set the start memory address."
1590 (save-selected-window
1591 (select-window (posn-window (event-start event)))
1592 (let ((arg (read-from-minibuffer "Memory address: ")))
1593 (setq gdb-memory-address arg))
1594 (gdb-invalidate-memory)))
1596 (defun gdb-memory-set-repeat-count (event)
1597 "Set the number of data items in memory window."
1599 (save-selected-window
1600 (select-window (posn-window (event-start event)))
1601 (let* ((arg (read-from-minibuffer "Repeat count: "))
1602 (count (string-to-int arg)))
1604 (error "Non-negative numbers only")
1605 (customize-set-variable 'gdb-memory-repeat-count count)
1606 (gdb-invalidate-memory)))))
1608 (defun gdb-memory-format-binary ()
1609 "Set the display format to binary."
1611 (customize-set-variable 'gdb-memory-format "t")
1612 (gdb-invalidate-memory))
1614 (defun gdb-memory-format-octal ()
1615 "Set the display format to octal."
1617 (customize-set-variable 'gdb-memory-format "o")
1618 (gdb-invalidate-memory))
1620 (defun gdb-memory-format-unsigned ()
1621 "Set the display format to unsigned decimal."
1623 (customize-set-variable 'gdb-memory-format "u")
1624 (gdb-invalidate-memory))
1626 (defun gdb-memory-format-signed ()
1627 "Set the display format to decimal."
1629 (customize-set-variable 'gdb-memory-format "d")
1630 (gdb-invalidate-memory))
1632 (defun gdb-memory-format-hexadecimal ()
1633 "Set the display format to hexadecimal."
1635 (customize-set-variable 'gdb-memory-format "x")
1636 (gdb-invalidate-memory))
1638 (defvar gdb-memory-format-keymap
1639 (let ((map (make-sparse-keymap)))
1640 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
1642 "Keymap to select format in the header line.")
1644 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
1645 "Menu of display formats in the header line.")
1647 (define-key gdb-memory-format-menu [binary]
1648 '(menu-item "Binary" gdb-memory-format-binary
1649 :button (:radio . (equal gdb-memory-format "t"))))
1650 (define-key gdb-memory-format-menu [octal]
1651 '(menu-item "Octal" gdb-memory-format-octal
1652 :button (:radio . (equal gdb-memory-format "o"))))
1653 (define-key gdb-memory-format-menu [unsigned]
1654 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
1655 :button (:radio . (equal gdb-memory-format "u"))))
1656 (define-key gdb-memory-format-menu [signed]
1657 '(menu-item "Signed Decimal" gdb-memory-format-signed
1658 :button (:radio . (equal gdb-memory-format "d"))))
1659 (define-key gdb-memory-format-menu [hexadecimal]
1660 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
1661 :button (:radio . (equal gdb-memory-format "x"))))
1663 (defun gdb-memory-format-menu (event)
1665 (x-popup-menu event gdb-memory-format-menu))
1667 (defun gdb-memory-format-menu-1 (event)
1669 (save-selected-window
1670 (select-window (posn-window (event-start event)))
1671 (let* ((selection (gdb-memory-format-menu event))
1672 (binding (and selection (lookup-key gdb-memory-format-menu
1673 (vector (car selection))))))
1674 (if binding (call-interactively binding)))))
1676 (defun gdb-memory-unit-giant ()
1677 "Set the unit size to giant words (eight bytes)."
1679 (customize-set-variable 'gdb-memory-unit "g")
1680 (gdb-invalidate-memory))
1682 (defun gdb-memory-unit-word ()
1683 "Set the unit size to words (four bytes)."
1685 (customize-set-variable 'gdb-memory-unit "w")
1686 (gdb-invalidate-memory))
1688 (defun gdb-memory-unit-halfword ()
1689 "Set the unit size to halfwords (two bytes)."
1691 (customize-set-variable 'gdb-memory-unit "h")
1692 (gdb-invalidate-memory))
1694 (defun gdb-memory-unit-byte ()
1695 "Set the unit size to bytes."
1697 (customize-set-variable 'gdb-memory-unit "b")
1698 (gdb-invalidate-memory))
1700 (defvar gdb-memory-unit-keymap
1701 (let ((map (make-sparse-keymap)))
1702 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
1704 "Keymap to select units in the header line.")
1706 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
1707 "Menu of units in the header line.")
1709 (define-key gdb-memory-unit-menu [giantwords]
1710 '(menu-item "Giant words" gdb-memory-unit-giant
1711 :button (:radio . (equal gdb-memory-unit "g"))))
1712 (define-key gdb-memory-unit-menu [words]
1713 '(menu-item "Words" gdb-memory-unit-word
1714 :button (:radio . (equal gdb-memory-unit "w"))))
1715 (define-key gdb-memory-unit-menu [halfwords]
1716 '(menu-item "Halfwords" gdb-memory-unit-halfword
1717 :button (:radio . (equal gdb-memory-unit "h"))))
1718 (define-key gdb-memory-unit-menu [bytes]
1719 '(menu-item "Bytes" gdb-memory-unit-byte
1720 :button (:radio . (equal gdb-memory-unit "b"))))
1722 (defun gdb-memory-unit-menu (event)
1724 (x-popup-menu event gdb-memory-unit-menu))
1726 (defun gdb-memory-unit-menu-1 (event)
1728 (save-selected-window
1729 (select-window (posn-window (event-start event)))
1730 (let* ((selection (gdb-memory-unit-menu event))
1731 (binding (and selection (lookup-key gdb-memory-unit-menu
1732 (vector (car selection))))))
1733 (if binding (call-interactively binding)))))
1735 ;;from make-mode-line-mouse-map
1736 (defun gdb-make-header-line-mouse-map (mouse function) "\
1737 Return a keymap with single entry for mouse key MOUSE on the header line.
1738 MOUSE is defined to run function FUNCTION with no args in the buffer
1739 corresponding to the mode line clicked."
1740 (let ((map (make-sparse-keymap)))
1741 (define-key map (vector 'header-line mouse) function)
1742 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1745 (defun gdb-memory-mode ()
1746 "Major mode for examining memory.
1748 \\{gdb-memory-mode-map}"
1749 (kill-all-local-variables)
1750 (setq major-mode 'gdb-memory-mode)
1751 (setq mode-name "Memory")
1752 (setq buffer-read-only t)
1753 (use-local-map gdb-memory-mode-map)
1754 (setq header-line-format
1758 (propertize gdb-memory-address
1759 'face font-lock-warning-face
1760 'help-echo (purecopy "mouse-1: Set memory address")
1761 'local-map (purecopy (gdb-make-header-line-mouse-map
1763 #'gdb-memory-set-address)))
1765 (propertize (number-to-string gdb-memory-repeat-count)
1766 'face font-lock-warning-face
1767 'help-echo (purecopy "mouse-1: Set repeat count")
1768 'local-map (purecopy (gdb-make-header-line-mouse-map
1770 #'gdb-memory-set-repeat-count)))
1772 (propertize gdb-memory-format
1773 'face font-lock-warning-face
1774 'help-echo (purecopy "mouse-3: Select display format")
1775 'local-map gdb-memory-format-keymap)
1777 (propertize gdb-memory-unit
1778 'face font-lock-warning-face
1779 'help-echo (purecopy "mouse-3: Select unit size")
1780 'local-map gdb-memory-unit-keymap))))
1781 (run-mode-hooks 'gdb-memory-mode-hook)
1782 'gdb-invalidate-memory)
1784 (defun gdb-memory-buffer-name ()
1785 (with-current-buffer gud-comint-buffer
1786 (concat "*memory of " (gdb-get-target-string) "*")))
1788 (defun gdb-display-memory-buffer ()
1789 "Display memory contents."
1792 (gdb-get-create-buffer 'gdb-memory-buffer)))
1794 (defun gdb-frame-memory-buffer ()
1795 "Display memory contents in a new frame."
1797 (let ((special-display-regexps (append special-display-regexps '(".*")))
1798 (special-display-frame-alist gdb-frame-parameters))
1799 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
1804 (gdb-set-buffer-rules 'gdb-locals-buffer
1805 'gdb-locals-buffer-name
1808 (def-gdb-auto-updated-buffer gdb-locals-buffer
1809 gdb-invalidate-locals
1810 "server info locals\n"
1811 gdb-info-locals-handler
1812 gdb-info-locals-custom)
1814 ;; Abbreviate for arrays and structures.
1815 ;; These can be expanded using gud-display.
1816 (defun gdb-info-locals-handler nil
1817 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
1818 gdb-pending-triggers))
1819 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1820 (with-current-buffer buf
1821 (goto-char (point-min))
1822 (while (re-search-forward "^ .*\n" nil t)
1823 (replace-match "" nil nil))
1824 (goto-char (point-min))
1825 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1826 (replace-match "(array);\n" nil nil))
1827 (goto-char (point-min))
1828 (while (re-search-forward "{.*=.*\n" nil t)
1829 (replace-match "(structure);\n" nil nil))))
1830 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1831 (and buf (with-current-buffer buf
1833 (buffer-read-only nil))
1834 (delete-region (point-min) (point-max))
1835 (insert-buffer-substring (gdb-get-create-buffer
1836 'gdb-partial-output-buffer))
1838 (run-hooks 'gdb-info-locals-hook))
1840 (defun gdb-info-locals-custom ()
1843 (defvar gdb-locals-mode-map
1844 (let ((map (make-sparse-keymap)))
1845 (suppress-keymap map)
1846 (define-key map "q" 'kill-this-buffer)
1849 (defun gdb-locals-mode ()
1850 "Major mode for gdb locals.
1852 \\{gdb-locals-mode-map}"
1853 (kill-all-local-variables)
1854 (setq major-mode 'gdb-locals-mode)
1855 (setq mode-name (concat "Locals:" gdb-current-frame))
1856 (setq buffer-read-only t)
1857 (use-local-map gdb-locals-mode-map)
1858 (run-mode-hooks 'gdb-locals-mode-hook)
1859 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1860 'gdb-invalidate-locals
1861 'gdbmi-invalidate-locals))
1863 (defun gdb-locals-buffer-name ()
1864 (with-current-buffer gud-comint-buffer
1865 (concat "*locals of " (gdb-get-target-string) "*")))
1867 (defun gdb-display-locals-buffer ()
1868 "Display local variables of current stack and their values."
1871 (gdb-get-create-buffer 'gdb-locals-buffer)))
1873 (defun gdb-frame-locals-buffer ()
1874 "Display local variables of current stack and their values in a new frame."
1876 (let ((special-display-regexps (append special-display-regexps '(".*")))
1877 (special-display-frame-alist gdb-frame-parameters))
1878 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
1881 ;;;; Window management
1882 (defun gdb-display-buffer (buf &optional size)
1883 (let ((answer (get-buffer-window buf 0))
1886 (display-buffer buf) ;Raise the frame if necessary.
1887 ;; The buffer is not yet displayed.
1888 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
1889 (let ((window (get-lru-window)))
1892 (set-window-buffer window buf)
1893 (setq answer window))
1894 (setq must-split t)))
1896 (let* ((largest (get-largest-window))
1897 (cur-size (window-height largest))
1898 (new-size (and size (< size cur-size) (- cur-size size))))
1899 (setq answer (split-window largest new-size))
1900 (set-window-buffer answer buf)
1901 (set-window-dedicated-p answer t)))
1905 ;;; Shared keymap initialization:
1907 (let ((menu (make-sparse-keymap "GDB-Windows")))
1908 (define-key gud-menu-map [displays]
1909 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
1910 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
1911 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
1912 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
1913 (define-key menu [assembler] '("Machine" . gdb-display-assembler-buffer))
1914 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
1915 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
1916 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
1917 (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer)))
1919 (let ((menu (make-sparse-keymap "GDB-Frames")))
1920 (define-key gud-menu-map [frames]
1921 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
1922 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
1923 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
1924 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
1925 (define-key menu [assembler] '("Machine" . gdb-frame-assembler-buffer))
1926 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
1927 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
1928 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
1929 (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer)))
1931 (let ((menu (make-sparse-keymap "GDB-UI")))
1932 (define-key gud-menu-map [ui]
1933 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
1934 (define-key menu [gdb-restore-windows]
1935 '("Restore Window Layout" . gdb-restore-windows))
1936 (define-key menu [gdb-many-windows]
1937 (menu-bar-make-toggle gdb-many-windows gdb-many-windows
1938 "Display Other Windows" "Many windows %s"
1939 "Toggle display of locals, stack and breakpoint information")))
1941 (defun gdb-frame-gdb-buffer ()
1942 "Display GUD buffer in a new frame."
1944 (select-frame (make-frame gdb-frame-parameters))
1945 (switch-to-buffer (gdb-get-create-buffer 'gdba))
1946 (set-window-dedicated-p (selected-window) t))
1948 (defun gdb-display-gdb-buffer ()
1949 "Display GUD buffer."
1952 (gdb-get-create-buffer 'gdba)))
1954 (defvar gdb-main-file nil "Source file from which program execution begins.")
1956 (defcustom gdb-show-main nil
1957 "Nil means don't display source file containing the main routine."
1962 (defun gdb-set-window-buffer (name)
1963 (set-window-buffer (selected-window) (get-buffer name))
1964 (set-window-dedicated-p (selected-window) t))
1966 (defun gdb-setup-windows ()
1967 "Layout the window pattern for gdb-many-windows."
1968 (gdb-display-locals-buffer)
1969 (gdb-display-stack-buffer)
1970 (delete-other-windows)
1971 (gdb-display-breakpoints-buffer)
1972 (delete-other-windows)
1974 (pop-to-buffer gud-comint-buffer)
1975 (split-window nil ( / ( * (window-height) 3) 4))
1976 (split-window nil ( / (window-height) 3))
1977 (split-window-horizontally)
1979 (gdb-set-window-buffer (gdb-locals-buffer-name))
1982 (if gud-last-last-frame
1983 (gud-find-file (car gud-last-last-frame))
1984 (gud-find-file gdb-main-file)))
1985 (when gdb-use-inferior-io-buffer
1986 (split-window-horizontally)
1988 (gdb-set-window-buffer (gdb-inferior-io-name)))
1990 (gdb-set-window-buffer (gdb-stack-buffer-name))
1991 (split-window-horizontally)
1993 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
1996 (defcustom gdb-many-windows nil
1997 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
1998 In this case it starts with two windows: one displaying the GUD
1999 buffer and the other with the source file with the main routine
2000 of the inferior. Non-nil means display the layout shown for
2006 (defun gdb-many-windows (arg)
2007 "Toggle the number of windows in the basic arrangement."
2009 (setq gdb-many-windows
2011 (not gdb-many-windows)
2012 (> (prefix-numeric-value arg) 0)))
2014 (gdb-restore-windows)
2017 (defun gdb-restore-windows ()
2018 "Restore the basic arrangement of windows used by gdba.
2019 This arrangement depends on the value of `gdb-many-windows'."
2021 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2022 (delete-other-windows)
2023 (if gdb-many-windows
2028 (if gud-last-last-frame
2029 (gud-find-file (car gud-last-last-frame))
2030 (gud-find-file gdb-main-file)))
2034 "Exit a debugging session cleanly.
2035 Kills the gdb buffers and resets the source buffers."
2036 (dolist (buffer (buffer-list))
2037 (unless (eq buffer gud-comint-buffer)
2038 (with-current-buffer buffer
2039 (if (memq gud-minor-mode '(gdbmi gdba))
2040 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2042 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2043 (setq gud-minor-mode nil)
2044 (kill-local-variable 'tool-bar-map)
2045 (setq gud-running nil))))))
2046 (when (markerp gdb-overlay-arrow-position)
2047 (move-marker gdb-overlay-arrow-position nil)
2048 (setq gdb-overlay-arrow-position nil))
2049 (setq overlay-arrow-variable-list
2050 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list)))
2052 (defun gdb-source-info ()
2053 "Find the source file where the program starts and displays it with related
2055 (goto-char (point-min))
2056 (if (search-forward "Located in " nil t)
2057 (if (looking-at "\\S-*")
2058 (setq gdb-main-file (match-string 0))))
2059 (if gdb-many-windows
2061 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2063 (let ((pop-up-windows t))
2064 (display-buffer (gud-find-file gdb-main-file))))))
2066 (defun gdb-get-location (bptno line flag)
2067 "Find the directory containing the relevant source file.
2068 Put in buffer and place breakpoint icon."
2069 (goto-char (point-min))
2070 (if (search-forward "Located in " nil t)
2071 (if (looking-at "\\S-*")
2072 (push (cons bptno (match-string 0)) gdb-location-list))
2074 (push (cons bptno "File not found") gdb-location-list)
2075 (error "Cannot find source file for breakpoint location.
2076 Add directory to search path for source files using the GDB command, dir."))
2077 (with-current-buffer
2078 (find-file-noselect (match-string 0))
2079 (save-current-buffer
2080 (set (make-local-variable 'gud-minor-mode) 'gdba)
2081 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2082 ;; only want one breakpoint icon at each location
2084 (goto-line (string-to-number line))
2085 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))
2088 (defun gdb-put-string (putstring pos &optional dprop)
2089 "Put string PUTSTRING in front of POS in the current buffer.
2090 PUTSTRING is displayed by putting an overlay into the current buffer with a
2091 `before-string' STRING that has a `display' property whose value is
2093 (let ((string (make-string 1 ?x))
2094 (buffer (current-buffer)))
2095 (setq putstring (copy-sequence putstring))
2096 (let ((overlay (make-overlay pos pos buffer))
2098 (list (list 'margin 'left-margin) putstring))))
2099 (put-text-property 0 (length string) 'display prop string)
2100 (overlay-put overlay 'put-break t)
2101 (overlay-put overlay 'before-string string))))
2103 ;;from remove-images
2104 (defun gdb-remove-strings (start end &optional buffer)
2105 "Remove strings between START and END in BUFFER.
2106 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2107 BUFFER nil or omitted means use the current buffer."
2109 (setq buffer (current-buffer)))
2110 (dolist (overlay (overlays-in start end))
2111 (when (overlay-get overlay 'put-break)
2112 (delete-overlay overlay))))
2114 (defun gdb-put-breakpoint-icon (enabled bptno)
2115 (let ((start (progn (beginning-of-line) (- (point) 1)))
2116 (end (progn (end-of-line) (+ (point) 1)))
2117 (putstring (if enabled "B" "b")))
2118 (add-text-properties
2119 0 1 '(help-echo "mouse-1: set/clear bkpt, mouse-3: enable/disable bkpt")
2121 (if enabled (add-text-properties
2122 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2123 (add-text-properties
2124 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2125 (gdb-remove-breakpoint-icons start end)
2126 (if (display-images-p)
2127 (if (>= (car (window-fringes)) 8)
2130 `(left-fringe breakpoint
2132 'breakpoint-enabled-bitmap-face
2133 'breakpoint-disabled-bitmap-face)))
2134 (when (< left-margin-width 2)
2135 (save-current-buffer
2136 (setq left-margin-width 2)
2137 (if (get-buffer-window (current-buffer) 0)
2139 (get-buffer-window (current-buffer) 0)
2140 left-margin-width right-margin-width))))
2143 (or breakpoint-enabled-icon
2144 (setq breakpoint-enabled-icon
2145 (find-image `((:type xpm :data
2146 ,breakpoint-xpm-data
2147 :ascent 100 :pointer hand)
2149 ,breakpoint-enabled-pbm-data
2150 :ascent 100 :pointer hand)))))
2151 (or breakpoint-disabled-icon
2152 (setq breakpoint-disabled-icon
2153 (find-image `((:type xpm :data
2154 ,breakpoint-xpm-data
2155 :conversion disabled
2158 ,breakpoint-disabled-pbm-data
2163 (when (< left-margin-width 2)
2164 (save-current-buffer
2165 (setq left-margin-width 2)
2166 (if (get-buffer-window (current-buffer) 0)
2168 (get-buffer-window (current-buffer) 0)
2169 left-margin-width right-margin-width))))
2170 (gdb-put-string putstring (1+ start)))))
2172 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2173 (gdb-remove-strings start end)
2174 (if (display-images-p)
2175 (remove-images start end))
2177 (setq left-margin-width 0)
2178 (if (get-buffer-window (current-buffer) 0)
2180 (get-buffer-window (current-buffer) 0)
2181 left-margin-width right-margin-width))))
2185 ;; Assembler buffer.
2187 (gdb-set-buffer-rules 'gdb-assembler-buffer
2188 'gdb-assembler-buffer-name
2189 'gdb-assembler-mode)
2191 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2192 gdb-invalidate-assembler
2193 (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
2194 gdb-assembler-handler
2195 gdb-assembler-custom)
2197 (defun gdb-assembler-custom ()
2198 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2199 (pos 1) (address) (flag) (bptno))
2200 (with-current-buffer buffer
2201 (if (not (equal gdb-current-address "main"))
2203 (goto-char (point-min))
2204 (if (re-search-forward gdb-current-address nil t)
2208 (or gdb-overlay-arrow-position
2209 (setq gdb-overlay-arrow-position (make-marker)))
2210 (set-marker gdb-overlay-arrow-position
2211 (point) (current-buffer))))))
2212 ;; remove all breakpoint-icons in assembler buffer before updating.
2213 (gdb-remove-breakpoint-icons (point-min) (point-max)))
2214 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2215 (goto-char (point-min))
2216 (while (< (point) (- (point-max) 1))
2218 (if (looking-at "[^\t].*breakpoint")
2221 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x\\(\\S-+\\)")
2222 (setq bptno (match-string 1))
2223 (setq flag (char-after (match-beginning 2)))
2224 (setq address (match-string 3))
2225 ;; remove leading 0s from output of info break.
2226 (if (string-match "^0+\\(.*\\)" address)
2227 (setq address (match-string 1 address)))
2228 (with-current-buffer buffer
2229 (goto-char (point-min))
2230 (if (re-search-forward address nil t)
2231 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
2232 (if (not (equal gdb-current-address "main"))
2233 (set-window-point (get-buffer-window buffer 0) pos))))
2235 (defvar gdb-assembler-mode-map
2236 (let ((map (make-sparse-keymap)))
2237 (suppress-keymap map)
2238 (define-key map "q" 'kill-this-buffer)
2241 (defvar gdb-assembler-font-lock-keywords
2242 '(;; <__function.name+n>
2243 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2244 (1 font-lock-function-name-face))
2245 ;; 0xNNNNNNNN <__function.name+n>: opcode
2246 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2247 (4 font-lock-keyword-face))
2248 ;; %register(at least i386)
2249 ("%\\sw+" . font-lock-variable-name-face)
2250 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2251 (1 font-lock-comment-face)
2252 (2 font-lock-function-name-face))
2253 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2254 "Font lock keywords used in `gdb-assembler-mode'.")
2256 (defun gdb-assembler-mode ()
2257 "Major mode for viewing code assembler.
2259 \\{gdb-assembler-mode-map}"
2260 (kill-all-local-variables)
2261 (setq major-mode 'gdb-assembler-mode)
2262 (setq mode-name "Machine")
2263 (setq gdb-overlay-arrow-position nil)
2264 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2265 (put 'gdb-overlay-arrow-position 'overlay-arrow-string "=>")
2266 (setq fringes-outside-margins t)
2267 (setq buffer-read-only t)
2268 (use-local-map gdb-assembler-mode-map)
2269 (gdb-invalidate-assembler)
2270 (set (make-local-variable 'font-lock-defaults)
2271 '(gdb-assembler-font-lock-keywords))
2272 (run-mode-hooks 'gdb-assembler-mode-hook)
2273 'gdb-invalidate-assembler)
2275 (defun gdb-assembler-buffer-name ()
2276 (with-current-buffer gud-comint-buffer
2277 (concat "*Machine Code " (gdb-get-target-string) "*")))
2279 (defun gdb-display-assembler-buffer ()
2280 "Display disassembly view."
2283 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2285 (defun gdb-frame-assembler-buffer ()
2286 "Display disassembly view in a new frame."
2288 (let ((special-display-regexps (append special-display-regexps '(".*")))
2289 (special-display-frame-alist gdb-frame-parameters))
2290 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2292 ;; modified because if gdb-current-address has changed value a new command
2293 ;; must be enqueued to update the buffer with the new output
2294 (defun gdb-invalidate-assembler (&optional ignored)
2295 (if (gdb-get-buffer 'gdb-assembler-buffer)
2297 (unless (string-equal gdb-current-frame gdb-previous-frame)
2298 (if (or (not (member 'gdb-invalidate-assembler
2299 gdb-pending-triggers))
2300 (not (string-equal gdb-current-address
2301 gdb-previous-address)))
2303 ;; take previous disassemble command off the queue
2304 (with-current-buffer gud-comint-buffer
2305 (let ((queue gdb-input-queue))
2306 (dolist (item queue)
2307 (if (equal (cdr item) '(gdb-assembler-handler))
2308 (setq gdb-input-queue
2309 (delete item gdb-input-queue))))))
2311 (list (concat gdb-server-prefix "disassemble " gdb-current-address "\n")
2312 'gdb-assembler-handler))
2313 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2314 (setq gdb-previous-address gdb-current-address)
2315 (setq gdb-previous-frame gdb-current-frame)))))))
2317 (defun gdb-get-current-frame ()
2318 (if (not (member 'gdb-get-current-frame gdb-pending-triggers))
2321 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2322 (push 'gdb-get-current-frame
2323 gdb-pending-triggers))))
2325 (defun gdb-frame-handler ()
2326 (setq gdb-pending-triggers
2327 (delq 'gdb-get-current-frame gdb-pending-triggers))
2328 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2329 (goto-char (point-min))
2330 (if (looking-at "Stack level \\([0-9]+\\)")
2331 (setq gdb-current-stack-level (match-string 1)))
2333 (if (looking-at ".*=\\s-+0x\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? ")
2335 (setq gdb-current-frame (match-string 2))
2336 (if (gdb-get-buffer 'gdb-locals-buffer)
2337 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2338 (setq mode-name (concat "Locals:" gdb-current-frame))))
2339 (let ((address (match-string 1)))
2340 ;; remove leading 0s from output of info frame command.
2341 (if (string-match "^0+\\(.*\\)" address)
2342 (setq gdb-current-address
2343 (concat "0x" (match-string 1 address)))
2344 (setq gdb-current-address (concat "0x" address))))
2345 (if (not (re-search-forward "(\\S-*:[0-9]*);" nil t))
2346 ;;update with new frame for machine code if necessary
2347 (gdb-invalidate-assembler)))))
2348 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2349 (setq gdb-current-language (match-string 1))))
2353 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2354 ;;; gdb-ui.el ends here