(gdb-var-create-handler)
[emacs.git] / lisp / progmodes / gdb-ui.el
blob3e4f7a4447b54a05d2f32bf410b11af33cb78aa9
1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nickrob@gnu.org>
4 ;; Maintainer: FSF
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)
14 ;; any later version.
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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.
53 ;; This mode SHOULD WORK WITH GDB 5.0 onwards but you will NEED GDB 6.0
54 ;; onwards to use watch expressions.
56 ;;; Windows Platforms:
58 ;; If you are using Emacs and GDB on Windows you will need to flush the buffer
59 ;; explicitly in your program if you want timely display of I/O in Emacs.
60 ;; Alternatively you can make the output stream unbuffered, for example, by
61 ;; using a macro:
63 ;; #ifdef UNBUFFERED
64 ;; setvbuf (stdout, (char *) NULL, _IONBF, 0);
65 ;; #endif
67 ;; and compiling with -DUNBUFFERED while debugging.
69 ;;; Known Bugs:
71 ;; 1) Strings that are watched don't update in the speedbar when their
72 ;; contents change.
73 ;; 2) Watch expressions go out of scope when the inferior is re-run.
74 ;; 3) Cannot handle multiple debug sessions.
76 ;;; TODO:
78 ;; 1) Use MI command -data-read-memory for memory window.
79 ;; 2) Highlight changed register values (use MI commands
80 ;; -data-list-register-values and -data-list-changed-registers instead
81 ;; of 'info registers' after release of 22.1.
82 ;; 3) Use tree-widget.el instead of the speedbar for watch-expressions?
83 ;; 4) Mark breakpoint locations on scroll-bar of source buffer?
84 ;; 5) After release of 22.1, use "-var-list-children --all-values"
85 ;; and "-stack-list-locals --simple-values" which need GDB 6.1 onwards.
86 ;; 6) After release of 22.1, use "-var-update --all-values" which needs
87 ;; GDB 6.4 onwards.
88 ;; 7) With gud-print and gud-pstar, print the variable name in the GUD
89 ;; buffer instead of the value's history number.
91 ;;; Code:
93 (require 'gud)
95 (defvar tool-bar-map)
97 (defvar gdb-frame-address "main" "Initialization for Assembler buffer.")
98 (defvar gdb-previous-frame-address nil)
99 (defvar gdb-memory-address "main")
100 (defvar gdb-previous-frame nil)
101 (defvar gdb-selected-frame nil)
102 (defvar gdb-frame-number nil)
103 (defvar gdb-current-language nil)
104 (defvar gdb-var-list nil "List of variables in watch window.")
105 (defvar gdb-var-changed nil "Non-nil means that `gdb-var-list' has changed.")
106 (defvar gdb-main-file nil "Source file from which program execution begins.")
107 (defvar gdb-overlay-arrow-position nil)
108 (defvar gdb-server-prefix nil)
109 (defvar gdb-flush-pending-output nil)
110 (defvar gdb-location-alist nil
111 "Alist of breakpoint numbers and full filenames.")
112 (defvar gdb-find-file-unhook nil)
113 (defvar gdb-active-process nil "GUD tooltips display variable values when t, \
114 and #define directives otherwise.")
115 (defvar gdb-error "Non-nil when GDB is reporting an error.")
116 (defvar gdb-macro-info nil
117 "Non-nil if GDB knows that the inferior includes preprocessor macro info.")
118 (defvar gdb-buffer-fringe-width nil)
120 (defvar gdb-buffer-type nil
121 "One of the symbols bound in `gdb-buffer-rules'.")
122 (make-variable-buffer-local 'gdb-buffer-type)
124 (defvar gdb-input-queue ()
125 "A list of gdb command objects.")
127 (defvar gdb-prompting nil
128 "True when gdb is idle with no pending input.")
130 (defvar gdb-output-sink 'user
131 "The disposition of the output of the current gdb command.
132 Possible values are these symbols:
134 `user' -- gdb output should be copied to the GUD buffer
135 for the user to see.
137 `inferior' -- gdb output should be copied to the inferior-io buffer.
139 `pre-emacs' -- output should be ignored util the post-prompt
140 annotation is received. Then the output-sink
141 becomes:...
142 `emacs' -- output should be collected in the partial-output-buffer
143 for subsequent processing by a command. This is the
144 disposition of output generated by commands that
145 gdb mode sends to gdb on its own behalf.
146 `post-emacs' -- ignore output until the prompt annotation is
147 received, then go to USER disposition.
149 gdba (gdb-ui.el) uses all five values, gdbmi (gdb-mi.el) only two
150 \(`user' and `emacs').")
152 (defvar gdb-current-item nil
153 "The most recent command item sent to gdb.")
155 (defvar gdb-pending-triggers '()
156 "A list of trigger functions that have run later than their output
157 handlers.")
159 ;; end of gdb variables
161 ;;;###autoload
162 (defun gdba (command-line)
163 "Run gdb on program FILE in buffer *gud-FILE*.
164 The directory containing FILE becomes the initial working directory
165 and source-file directory for your debugger.
167 If `gdb-many-windows' is nil (the default value) then gdb just
168 pops up the GUD buffer unless `gdb-show-main' is t. In this case
169 it starts with two windows: one displaying the GUD buffer and the
170 other with the source file with the main routine of the inferior.
172 If `gdb-many-windows' is t, regardless of the value of
173 `gdb-show-main', the layout below will appear unless
174 `gdb-use-inferior-io-buffer' is nil when the source buffer
175 occupies the full width of the frame. Keybindings are given in
176 relevant buffer.
178 Watch expressions appear in the speedbar/slowbar.
180 The following commands help control operation :
182 `gdb-many-windows' - Toggle the number of windows gdb uses.
183 `gdb-restore-windows' - To restore the window layout.
185 See Info node `(emacs)GDB Graphical Interface' for a more
186 detailed description of this mode.
189 +--------------------------------------------------------------+
190 | GDB Toolbar |
191 +-------------------------------+------------------------------+
192 | GUD buffer (I/O of GDB) | Locals buffer |
193 | | |
194 | | |
195 | | |
196 +-------------------------------+------------------------------+
197 | Source buffer | I/O buffer (of inferior) |
198 | | (comint-mode) |
199 | | |
200 | | |
201 | | |
202 | | |
203 | | |
204 | | |
205 +-------------------------------+------------------------------+
206 | Stack buffer | Breakpoints buffer |
207 | RET gdb-frames-select | SPC gdb-toggle-breakpoint |
208 | | RET gdb-goto-breakpoint |
209 | | d gdb-delete-breakpoint |
210 +-------------------------------+------------------------------+"
212 (interactive (list (gud-query-cmdline 'gdba)))
214 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
215 (gdb command-line)
216 (gdb-ann3))
218 (defvar gdb-debug-log nil)
220 ;;;###autoload
221 (defcustom gdb-enable-debug-log nil
222 "Non-nil means record the process input and output in `gdb-debug-log'."
223 :type 'boolean
224 :group 'gud
225 :version "22.1")
227 (defcustom gdb-cpp-define-alist-program "gcc -E -dM -"
228 "Shell command for generating a list of defined macros in a source file.
229 This list is used to display the #define directive associated
230 with an identifier as a tooltip. It works in a debug session with
231 GDB, when gud-tooltip-mode is t.
233 Set `gdb-cpp-define-alist-flags' for any include paths or
234 predefined macros."
235 :type 'string
236 :group 'gud
237 :version "22.1")
239 (defcustom gdb-cpp-define-alist-flags ""
240 "Preprocessor flags for `gdb-cpp-define-alist-program'."
241 :type 'string
242 :group 'gud
243 :version "22.1")
245 (defcustom gdb-show-main nil
246 "Non-nil means display source file containing the main routine at startup.
247 Also display the main routine in the disassembly buffer if present."
248 :type 'boolean
249 :group 'gud
250 :version "22.1")
252 (defcustom gdb-use-inferior-io-buffer nil
253 "Non-nil means display output from the inferior in a separate buffer."
254 :type 'boolean
255 :group 'gud
256 :version "22.1")
258 (defun gdb-use-inferior-io-buffer (arg)
259 "Toggle separate IO for inferior.
260 With arg, use separate IO iff arg is positive."
261 (interactive "P")
262 (setq gdb-use-inferior-io-buffer
263 (if (null arg)
264 (not gdb-use-inferior-io-buffer)
265 (> (prefix-numeric-value arg) 0)))
266 (message (format "Separate inferior IO %sabled"
267 (if gdb-use-inferior-io-buffer "en" "dis")))
268 (if (and gud-comint-buffer
269 (buffer-name gud-comint-buffer))
270 (condition-case nil
271 (if gdb-use-inferior-io-buffer
272 (gdb-restore-windows)
273 (kill-buffer (gdb-inferior-io-name)))
274 (error nil))))
276 (defvar gdb-define-alist nil "Alist of #define directives for GUD tooltips.")
278 (defun gdb-create-define-alist ()
279 "Create an alist of #define directives for GUD tooltips."
280 (let* ((file (buffer-file-name))
281 (output
282 (with-output-to-string
283 (with-current-buffer standard-output
284 (call-process shell-file-name
285 (if (file-exists-p file) file nil)
286 (list t nil) nil "-c"
287 (concat gdb-cpp-define-alist-program " "
288 gdb-cpp-define-alist-flags)))))
289 (define-list (split-string output "\n" t))
290 (name))
291 (setq gdb-define-alist nil)
292 (dolist (define define-list)
293 (setq name (nth 1 (split-string define "[( ]")))
294 (push (cons name define) gdb-define-alist))))
296 (defun gdb-tooltip-print ()
297 (tooltip-show
298 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
299 (let ((string (buffer-string)))
300 ;; remove newline for gud-tooltip-echo-area
301 (substring string 0 (- (length string) 1))))
302 (or gud-tooltip-echo-area tooltip-use-echo-area)))
304 ;; If expr is a macro for a function don't print because of possible dangerous
305 ;; side-effects. Also printing a function within a tooltip generates an
306 ;; unexpected starting annotation (phase error).
307 (defun gdb-tooltip-print-1 (expr)
308 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
309 (goto-char (point-min))
310 (if (search-forward "expands to: " nil t)
311 (unless (looking-at "\\S-+.*(.*).*")
312 (gdb-enqueue-input
313 (list (concat gdb-server-prefix "print " expr "\n")
314 'gdb-tooltip-print))))))
316 (defun gdb-set-gud-minor-mode (buffer)
317 "Set `gud-minor-mode' from find-file if appropriate."
318 (goto-char (point-min))
319 (unless (search-forward "No source file named " nil t)
320 (condition-case nil
321 (gdb-enqueue-input
322 (list (concat gdb-server-prefix "info source\n")
323 `(lambda () (gdb-set-gud-minor-mode-1 ,buffer))))
324 (error (setq gdb-find-file-unhook t)))))
326 (defun gdb-set-gud-minor-mode-1 (buffer)
327 (goto-char (point-min))
328 (when (and (search-forward "Located in " nil t)
329 (looking-at "\\S-+")
330 (string-equal (buffer-file-name buffer)
331 (match-string 0)))
332 (with-current-buffer buffer
333 (set (make-local-variable 'gud-minor-mode) 'gdba)
334 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
335 (when gud-tooltip-mode
336 (make-local-variable 'gdb-define-alist)
337 (gdb-create-define-alist)
338 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
340 (defun gdb-set-gud-minor-mode-existing-buffers ()
341 (dolist (buffer (buffer-list))
342 (let ((file (buffer-file-name buffer)))
343 (if file
344 (progn
345 (gdb-enqueue-input
346 (list (concat gdb-server-prefix "list "
347 (file-name-nondirectory file) ":1\n")
348 `(lambda () (gdb-set-gud-minor-mode ,buffer)))))))))
350 (defun gdb-find-watch-expression ()
351 (let* ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list))
352 (varno (nth 1 var)) (expr))
353 (string-match "\\(var[0-9]+\\)\\.\\(.*\\)" varno)
354 (dolist (var1 gdb-var-list)
355 (if (string-equal (nth 1 var1) (match-string 1 varno))
356 (setq expr (concat (car var1) "." (match-string 2 varno)))))
357 expr))
359 (defun gdb-ann3 ()
360 (setq gdb-debug-log nil)
361 (set (make-local-variable 'gud-minor-mode) 'gdba)
362 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
364 (gud-def gud-break (if (not (string-match "Machine" mode-name))
365 (gud-call "break %f:%l" arg)
366 (save-excursion
367 (beginning-of-line)
368 (forward-char 2)
369 (gud-call "break *%a" arg)))
370 "\C-b" "Set breakpoint at current line or address.")
372 (gud-def gud-remove (if (not (string-match "Machine" mode-name))
373 (gud-call "clear %f:%l" arg)
374 (save-excursion
375 (beginning-of-line)
376 (forward-char 2)
377 (gud-call "clear *%a" arg)))
378 "\C-d" "Remove breakpoint at current line or address.")
380 (gud-def gud-until (if (not (string-match "Machine" mode-name))
381 (gud-call "until %f:%l" arg)
382 (save-excursion
383 (beginning-of-line)
384 (forward-char 2)
385 (gud-call "until *%a" arg)))
386 "\C-u" "Continue to current line or address.")
388 (gud-def gud-go (gud-call (if gdb-active-process "continue" "run") arg)
389 nil "Start or continue execution.")
391 ;; For debugging Emacs only.
392 (gud-def gud-pp
393 (gud-call
394 (concat
395 "pp1 " (if (eq (buffer-local-value
396 'major-mode (window-buffer)) 'speedbar-mode)
397 (gdb-find-watch-expression) "%e")) arg)
398 nil "Print the emacs s-expression.")
400 (define-key gud-minor-mode-map [left-margin mouse-1]
401 'gdb-mouse-set-clear-breakpoint)
402 (define-key gud-minor-mode-map [left-fringe mouse-1]
403 'gdb-mouse-set-clear-breakpoint)
404 (define-key gud-minor-mode-map [left-fringe mouse-2]
405 'gdb-mouse-until)
406 (define-key gud-minor-mode-map [left-fringe drag-mouse-1]
407 'gdb-mouse-until)
408 (define-key gud-minor-mode-map [left-margin mouse-2]
409 'gdb-mouse-until)
410 (define-key gud-minor-mode-map [left-margin mouse-3]
411 'gdb-mouse-toggle-breakpoint-margin)
412 (define-key gud-minor-mode-map [left-fringe mouse-3]
413 'gdb-mouse-toggle-breakpoint-fringe)
415 (setq comint-input-sender 'gdb-send)
417 ;; (re-)initialize
418 (setq gdb-frame-address (if gdb-show-main "main" nil))
419 (setq gdb-previous-frame-address nil
420 gdb-memory-address "main"
421 gdb-previous-frame nil
422 gdb-selected-frame nil
423 gdb-current-language nil
424 gdb-frame-number nil
425 gdb-var-list nil
426 gdb-var-changed nil
427 gdb-first-prompt nil
428 gdb-prompting nil
429 gdb-input-queue nil
430 gdb-current-item nil
431 gdb-pending-triggers nil
432 gdb-output-sink 'user
433 gdb-server-prefix "server "
434 gdb-flush-pending-output nil
435 gdb-location-alist nil
436 gdb-find-file-unhook nil
437 gdb-error nil
438 gdb-macro-info nil
439 gdb-buffer-fringe-width (car (window-fringes)))
441 (setq gdb-buffer-type 'gdba)
443 (if gdb-use-inferior-io-buffer (gdb-clear-inferior-io))
445 (if (eq window-system 'w32)
446 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
447 (gdb-enqueue-input (list "set height 0\n" 'ignore))
448 (gdb-enqueue-input (list "set width 0\n" 'ignore))
449 ;; find source file and compilation directory here
450 (gdb-enqueue-input (list "server list main\n" 'ignore)) ; C program
451 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
452 (gdb-enqueue-input (list "server info source\n" 'gdb-source-info))
454 (gdb-set-gud-minor-mode-existing-buffers)
455 (run-hooks 'gdba-mode-hook))
457 (defun gdb-mouse-until (event)
458 "Execute source lines by dragging the overlay arrow (fringe) with the mouse."
459 (interactive "e")
460 (if gud-overlay-arrow-position
461 (let ((start (event-start event))
462 (end (event-end event))
463 (buffer (marker-buffer gud-overlay-arrow-position)) (line))
464 (if (not (string-match "Machine" mode-name))
465 (if (equal buffer (window-buffer (posn-window end)))
466 (with-current-buffer buffer
467 (when (or (equal start end)
468 (equal (posn-point start)
469 (marker-position
470 gud-overlay-arrow-position)))
471 (setq line (line-number-at-pos (posn-point end)))
472 (gud-call (concat "until " (number-to-string line))))))
473 (if (equal (marker-buffer gdb-overlay-arrow-position)
474 (window-buffer (posn-window end)))
475 (when (or (equal start end)
476 (equal (posn-point start)
477 (marker-position
478 gdb-overlay-arrow-position)))
479 (save-excursion
480 (goto-line (line-number-at-pos (posn-point end)))
481 (forward-char 2)
482 (gud-call (concat "until *%a")))))))))
484 (defcustom gdb-speedbar-auto-raise t
485 "If non-nil raise speedbar every time display of watch expressions is\
486 updated."
487 :type 'boolean
488 :group 'gud
489 :version "22.1")
491 (defun gdb-speedbar-auto-raise (arg)
492 "Toggle automatic raising of the speedbar for watch expressions."
493 (interactive "P")
494 (setq gdb-speedbar-auto-raise
495 (if (null arg)
496 (not gdb-speedbar-auto-raise)
497 (> (prefix-numeric-value arg) 0)))
498 (message (format "Auto raising %sabled"
499 (if gdb-speedbar-auto-raise "en" "dis"))))
501 (defcustom gdb-use-colon-colon-notation nil
502 "If non-nil use FUN::VAR format to display variables in the speedbar."
503 :type 'boolean
504 :group 'gud
505 :version "22.1")
507 (defun gud-watch ()
508 "Watch expression at point."
509 (interactive)
510 (require 'tooltip)
511 (save-selected-window
512 (let ((expr (tooltip-identifier-from-point (point))))
513 (if (and (string-equal gdb-current-language "c")
514 gdb-use-colon-colon-notation gdb-selected-frame)
515 (setq expr (concat gdb-selected-frame "::" expr)))
516 (catch 'already-watched
517 (dolist (var gdb-var-list)
518 (if (string-equal expr (car var)) (throw 'already-watched nil)))
519 (set-text-properties 0 (length expr) nil expr)
520 (gdb-enqueue-input
521 (list
522 (if (eq gud-minor-mode 'gdba)
523 (concat "server interpreter mi \"-var-create - * " expr "\"\n")
524 (concat"-var-create - * " expr "\n"))
525 `(lambda () (gdb-var-create-handler ,expr))))))))
527 (defconst gdb-var-create-regexp
528 "name=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",type=\"\\(.*?\\)\"")
530 (defun gdb-var-create-handler (expr)
531 (goto-char (point-min))
532 (if (re-search-forward gdb-var-create-regexp nil t)
533 (let ((var (list expr
534 (match-string 1)
535 (match-string 2)
536 (match-string 3)
537 nil nil)))
538 (push var gdb-var-list)
539 (speedbar 1)
540 (unless (string-equal
541 speedbar-initial-expansion-list-name "GUD")
542 (speedbar-change-initial-expansion-list "GUD"))
543 (gdb-enqueue-input
544 (list
545 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
546 'gdba)
547 (concat "server interpreter mi \"-var-evaluate-expression "
548 (nth 1 var) "\"\n")
549 (concat "-var-evaluate-expression " (nth 1 var) "\n"))
550 `(lambda () (gdb-var-evaluate-expression-handler
551 ,(nth 1 var) nil))))
552 (setq gdb-var-changed t))
553 (if (search-forward "Undefined command" nil t)
554 (message-box "Watching expressions requires gdb 6.0 onwards")
555 (message "No symbol \"%s\" in current context." expr))))
557 (defun gdb-var-evaluate-expression-handler (varnum changed)
558 (goto-char (point-min))
559 (re-search-forward ".*value=\\(\".*\"\\)" nil t)
560 (catch 'var-found
561 (let ((num 0))
562 (dolist (var gdb-var-list)
563 (if (string-equal varnum (cadr var))
564 (progn
565 (if changed (setcar (nthcdr 5 var) t))
566 (setcar (nthcdr 4 var) (read (match-string 1)))
567 (setcar (nthcdr num gdb-var-list) var)
568 (throw 'var-found nil)))
569 (setq num (+ num 1)))))
570 (setq gdb-var-changed t))
572 (defun gdb-var-list-children (varnum)
573 (gdb-enqueue-input
574 (list (concat "server interpreter mi \"-var-list-children " varnum "\"\n")
575 `(lambda () (gdb-var-list-children-handler ,varnum)))))
577 (defconst gdb-var-list-children-regexp
578 "name=\"\\(.*?\\)\",exp=\"\\(.*?\\)\",numchild=\"\\(.*?\\)\",\
579 type=\"\\(.*?\\)\"")
581 (defun gdb-var-list-children-handler (varnum)
582 (goto-char (point-min))
583 (let ((var-list nil))
584 (catch 'child-already-watched
585 (dolist (var gdb-var-list)
586 (if (string-equal varnum (cadr var))
587 (progn
588 (push var var-list)
589 (while (re-search-forward gdb-var-list-children-regexp nil t)
590 (let ((varchild (list (match-string 2)
591 (match-string 1)
592 (match-string 3)
593 (match-string 4)
594 nil nil)))
595 (dolist (var1 gdb-var-list)
596 (if (string-equal (cadr var1) (cadr varchild))
597 (throw 'child-already-watched nil)))
598 (push varchild var-list)
599 (gdb-enqueue-input
600 (list
601 (concat
602 "server interpreter mi \"-var-evaluate-expression "
603 (nth 1 varchild) "\"\n")
604 `(lambda () (gdb-var-evaluate-expression-handler
605 ,(nth 1 varchild) nil)))))))
606 (push var var-list)))
607 (setq gdb-var-list (nreverse var-list)))))
609 (defun gdb-var-update ()
610 (when (not (member 'gdb-var-update gdb-pending-triggers))
611 (gdb-enqueue-input
612 (list "server interpreter mi \"-var-update *\"\n"
613 'gdb-var-update-handler))
614 (push 'gdb-var-update gdb-pending-triggers)))
616 (defconst gdb-var-update-regexp "name=\"\\(.*?\\)\"")
618 (defun gdb-var-update-handler ()
619 (goto-char (point-min))
620 (while (re-search-forward gdb-var-update-regexp nil t)
621 (catch 'var-found-1
622 (let ((varnum (match-string 1)))
623 (dolist (var gdb-var-list)
624 (gdb-enqueue-input
625 (list
626 (concat "server interpreter mi \"-var-evaluate-expression "
627 varnum "\"\n")
628 `(lambda () (gdb-var-evaluate-expression-handler ,varnum t))))
629 (throw 'var-found-1 nil)))))
630 (setq gdb-pending-triggers
631 (delq 'gdb-var-update gdb-pending-triggers))
632 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
633 ;; Dummy command to update speedbar at right time.
634 (gdb-enqueue-input (list "server pwd\n" 'gdb-speedbar-timer-fn))
635 ;; Keep gdb-pending-triggers non-nil till end.
636 (push 'gdb-speedbar-timer gdb-pending-triggers)))
638 (defun gdb-speedbar-timer-fn ()
639 (setq gdb-pending-triggers
640 (delq 'gdb-speedbar-timer gdb-pending-triggers))
641 (with-current-buffer gud-comint-buffer
642 (speedbar-timer-fn)))
644 (defun gdb-var-delete ()
645 "Delete watch expression at point from the speedbar."
646 (interactive)
647 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
648 '(gdbmi gdba))
649 (let ((text (speedbar-line-text)))
650 (string-match "\\(\\S-+\\)" text)
651 (let* ((expr (match-string 1 text))
652 (var (assoc expr gdb-var-list))
653 (varnum (cadr var)))
654 (unless (string-match "\\." varnum)
655 (gdb-enqueue-input
656 (list
657 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
658 'gdba)
659 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
660 (concat "-var-delete " varnum "\n"))
661 'ignore))
662 (setq gdb-var-list (delq var gdb-var-list))
663 (dolist (varchild gdb-var-list)
664 (if (string-match (concat (nth 1 var) "\\.") (nth 1 varchild))
665 (setq gdb-var-list (delq varchild gdb-var-list))))
666 (setq gdb-var-changed t))))))
668 (defun gdb-edit-value (text token indent)
669 "Assign a value to a variable displayed in the speedbar."
670 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
671 (varnum (cadr var)) (value))
672 (setq value (read-string "New value: "))
673 (gdb-enqueue-input
674 (list
675 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
676 (concat "server interpreter mi \"-var-assign "
677 varnum " " value "\"\n")
678 (concat "-var-assign " varnum " " value "\n"))
679 'ignore))))
681 (defcustom gdb-show-changed-values t
682 "If non-nil highlight values that have recently changed in the speedbar.
683 The highlighting is done with `font-lock-warning-face'."
684 :type 'boolean
685 :group 'gud
686 :version "22.1")
688 (defun gdb-speedbar-expand-node (text token indent)
689 "Expand the node the user clicked on.
690 TEXT is the text of the button we clicked on, a + or - item.
691 TOKEN is data related to this node.
692 INDENT is the current indentation depth."
693 (cond ((string-match "+" text) ;expand this node
694 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
695 (gdb-var-list-children token)
696 (progn
697 (gdbmi-var-update)
698 (gdbmi-var-list-children token))))
699 ((string-match "-" text) ;contract this node
700 (dolist (var gdb-var-list)
701 (if (string-match (concat token "\\.") (nth 1 var))
702 (setq gdb-var-list (delq var gdb-var-list))))
703 (setq gdb-var-changed t)
704 (with-current-buffer gud-comint-buffer
705 (speedbar-timer-fn)))))
707 (defun gdb-get-target-string ()
708 (with-current-buffer gud-comint-buffer
709 gud-target-name))
713 ;; gdb buffers.
715 ;; Each buffer has a TYPE -- a symbol that identifies the function
716 ;; of that particular buffer.
718 ;; The usual gdb interaction buffer is given the type `gdba' and
719 ;; is constructed specially.
721 ;; Others are constructed by gdb-get-create-buffer and
722 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
724 (defvar gdb-buffer-rules-assoc '())
726 (defun gdb-get-buffer (key)
727 "Return the gdb buffer tagged with type KEY.
728 The key should be one of the cars in `gdb-buffer-rules-assoc'."
729 (save-excursion
730 (gdb-look-for-tagged-buffer key (buffer-list))))
732 (defun gdb-get-create-buffer (key)
733 "Create a new gdb buffer of the type specified by KEY.
734 The key should be one of the cars in `gdb-buffer-rules-assoc'."
735 (or (gdb-get-buffer key)
736 (let* ((rules (assoc key gdb-buffer-rules-assoc))
737 (name (funcall (gdb-rules-name-maker rules)))
738 (new (get-buffer-create name)))
739 (with-current-buffer new
740 (let ((trigger))
741 (if (cdr (cdr rules))
742 (setq trigger (funcall (car (cdr (cdr rules))))))
743 (setq gdb-buffer-type key)
744 (set (make-local-variable 'gud-minor-mode)
745 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
746 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
747 (if trigger (funcall trigger)))
748 new))))
750 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
752 (defun gdb-look-for-tagged-buffer (key bufs)
753 (let ((retval nil))
754 (while (and (not retval) bufs)
755 (set-buffer (car bufs))
756 (if (eq gdb-buffer-type key)
757 (setq retval (car bufs)))
758 (setq bufs (cdr bufs)))
759 retval))
762 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
763 ;; at least one and possible more functions. The functions have these
764 ;; roles in defining a buffer type:
766 ;; NAME - Return a name for this buffer type.
768 ;; The remaining function(s) are optional:
770 ;; MODE - called in a new buffer with no arguments, should establish
771 ;; the proper mode for the buffer.
774 (defun gdb-set-buffer-rules (buffer-type &rest rules)
775 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
776 (if binding
777 (setcdr binding rules)
778 (push (cons buffer-type rules)
779 gdb-buffer-rules-assoc))))
781 ;; GUD buffers are an exception to the rules
782 (gdb-set-buffer-rules 'gdba 'error)
785 ;; Partial-output buffer : This accumulates output from a command executed on
786 ;; behalf of emacs (rather than the user).
788 (gdb-set-buffer-rules 'gdb-partial-output-buffer
789 'gdb-partial-output-name)
791 (defun gdb-partial-output-name ()
792 (concat "*partial-output-"
793 (gdb-get-target-string)
794 "*"))
797 (gdb-set-buffer-rules 'gdb-inferior-io
798 'gdb-inferior-io-name
799 'gdb-inferior-io-mode)
801 (defun gdb-inferior-io-name ()
802 (concat "*input/output of "
803 (gdb-get-target-string)
804 "*"))
806 (defun gdb-display-inferior-io-buffer ()
807 "Display IO of inferior in a separate window."
808 (interactive)
809 (if gdb-use-inferior-io-buffer
810 (gdb-display-buffer
811 (gdb-get-create-buffer 'gdb-inferior-io))))
813 (defconst gdb-frame-parameters
814 '((height . 14) (width . 80)
815 (unsplittable . t)
816 (tool-bar-lines . nil)
817 (menu-bar-lines . nil)
818 (minibuffer . nil)))
820 (defun gdb-frame-inferior-io-buffer ()
821 "Display IO of inferior in a new frame."
822 (interactive)
823 (if gdb-use-inferior-io-buffer
824 (let ((special-display-regexps (append special-display-regexps '(".*")))
825 (special-display-frame-alist gdb-frame-parameters))
826 (display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
828 (defvar gdb-inferior-io-mode-map
829 (let ((map (make-sparse-keymap)))
830 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
831 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
832 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
833 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
834 (define-key map "\C-d" 'gdb-inferior-io-eof)
835 map))
837 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
838 "Major mode for gdb inferior-io."
839 :syntax-table nil :abbrev-table nil
840 ;; We want to use comint because it has various nifty and familiar
841 ;; features. We don't need a process, but comint wants one, so create
842 ;; a dummy one.
843 (make-comint-in-buffer
844 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
845 (current-buffer) "hexl")
846 (setq comint-input-sender 'gdb-inferior-io-sender))
848 (defun gdb-inferior-io-sender (proc string)
849 ;; PROC is the pseudo-process created to satisfy comint.
850 (with-current-buffer (process-buffer proc)
851 (setq proc (get-buffer-process gud-comint-buffer))
852 (process-send-string proc string)
853 (process-send-string proc "\n")))
855 (defun gdb-inferior-io-interrupt ()
856 "Interrupt the program being debugged."
857 (interactive)
858 (interrupt-process
859 (get-buffer-process gud-comint-buffer) comint-ptyp))
861 (defun gdb-inferior-io-quit ()
862 "Send quit signal to the program being debugged."
863 (interactive)
864 (quit-process
865 (get-buffer-process gud-comint-buffer) comint-ptyp))
867 (defun gdb-inferior-io-stop ()
868 "Stop the program being debugged."
869 (interactive)
870 (stop-process
871 (get-buffer-process gud-comint-buffer) comint-ptyp))
873 (defun gdb-inferior-io-eof ()
874 "Send end-of-file to the program being debugged."
875 (interactive)
876 (process-send-eof
877 (get-buffer-process gud-comint-buffer)))
881 ;; gdb communications
884 ;; INPUT: things sent to gdb
886 ;; The queues are lists. Each element is either a string (indicating user or
887 ;; user-like input) or a list of the form:
889 ;; (INPUT-STRING HANDLER-FN)
891 ;; The handler function will be called from the partial-output buffer when the
892 ;; command completes. This is the way to write commands which invoke gdb
893 ;; commands autonomously.
895 ;; These lists are consumed tail first.
898 (defun gdb-send (proc string)
899 "A comint send filter for gdb.
900 This filter may simply queue input for a later time."
901 (with-current-buffer gud-comint-buffer
902 (let ((inhibit-read-only t))
903 (remove-text-properties (point-min) (point-max) '(face))))
904 (let ((item (concat string "\n")))
905 (if gud-running
906 (progn
907 (if gdb-enable-debug-log (push (cons 'send item) gdb-debug-log))
908 (process-send-string proc item))
909 (gdb-enqueue-input item))))
911 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
912 ;; is a query, or other non-top-level prompt.
914 (defun gdb-enqueue-input (item)
915 (if gdb-prompting
916 (progn
917 (gdb-send-item item)
918 (setq gdb-prompting nil))
919 (push item gdb-input-queue)))
921 (defun gdb-dequeue-input ()
922 (let ((queue gdb-input-queue))
923 (and queue
924 (let ((last (car (last queue))))
925 (unless (nbutlast queue) (setq gdb-input-queue '()))
926 last))))
928 (defun gdb-send-item (item)
929 (setq gdb-flush-pending-output nil)
930 (if gdb-enable-debug-log (push (cons 'send-item item) gdb-debug-log))
931 (setq gdb-current-item item)
932 (let ((process (get-buffer-process gud-comint-buffer)))
933 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
934 (if (stringp item)
935 (progn
936 (setq gdb-output-sink 'user)
937 (process-send-string process item))
938 (progn
939 (gdb-clear-partial-output)
940 (setq gdb-output-sink 'pre-emacs)
941 (process-send-string process
942 (car item))))
943 ;; case: eq gud-minor-mode 'gdbmi
944 (gdb-clear-partial-output)
945 (setq gdb-output-sink 'emacs)
946 (process-send-string process (car item)))))
949 ;; output -- things gdb prints to emacs
951 ;; GDB output is a stream interrupted by annotations.
952 ;; Annotations can be recognized by their beginning
953 ;; with \C-j\C-z\C-z<tag><opt>\C-j
955 ;; The tag is a string obeying symbol syntax.
957 ;; The optional part `<opt>' can be either the empty string
958 ;; or a space followed by more data relating to the annotation.
959 ;; For example, the SOURCE annotation is followed by a filename,
960 ;; line number and various useless goo. This data must not include
961 ;; any newlines.
964 (defcustom gud-gdba-command-name "gdb -annotate=3"
965 "Default command to execute an executable under the GDB-UI debugger."
966 :type 'string
967 :group 'gud
968 :version "22.1")
970 (defvar gdb-annotation-rules
971 '(("pre-prompt" gdb-pre-prompt)
972 ("prompt" gdb-prompt)
973 ("commands" gdb-subprompt)
974 ("overload-choice" gdb-subprompt)
975 ("query" gdb-subprompt)
976 ;; Need this prompt for GDB 6.1
977 ("nquery" gdb-subprompt)
978 ("prompt-for-continue" gdb-subprompt)
979 ("post-prompt" gdb-post-prompt)
980 ("source" gdb-source)
981 ("starting" gdb-starting)
982 ("exited" gdb-exited)
983 ("signalled" gdb-exited)
984 ("signal" gdb-stopping)
985 ("breakpoint" gdb-stopping)
986 ("watchpoint" gdb-stopping)
987 ("frame-begin" gdb-frame-begin)
988 ("stopped" gdb-stopped)
989 ("error-begin" gdb-error)
990 ("error" gdb-error)
991 ) "An assoc mapping annotation tags to functions which process them.")
993 (defun gdb-resync()
994 (setq gdb-flush-pending-output t)
995 (setq gud-running nil)
996 (setq gdb-output-sink 'user)
997 (setq gdb-input-queue nil)
998 (setq gdb-pending-triggers nil)
999 (setq gdb-prompting t))
1001 (defconst gdb-source-spec-regexp
1002 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1004 ;; Do not use this except as an annotation handler.
1005 (defun gdb-source (args)
1006 (string-match gdb-source-spec-regexp args)
1007 ;; Extract the frame position from the marker.
1008 (setq gud-last-frame
1009 (cons
1010 (match-string 1 args)
1011 (string-to-number (match-string 2 args))))
1012 (setq gdb-frame-address (match-string 3 args))
1013 ;; cover for auto-display output which comes *before*
1014 ;; stopped annotation
1015 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1017 (defun gdb-pre-prompt (ignored)
1018 "An annotation handler for `pre-prompt'.
1019 This terminates the collection of output from a previous command if that
1020 happens to be in effect."
1021 (setq gdb-error nil)
1022 (let ((sink gdb-output-sink))
1023 (cond
1024 ((eq sink 'user) t)
1025 ((eq sink 'emacs)
1026 (setq gdb-output-sink 'post-emacs))
1028 (gdb-resync)
1029 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1031 (defun gdb-prompt (ignored)
1032 "An annotation handler for `prompt'.
1033 This sends the next command (if any) to gdb."
1034 (when gdb-first-prompt (gdb-ann3))
1035 (let ((sink gdb-output-sink))
1036 (cond
1037 ((eq sink 'user) t)
1038 ((eq sink 'post-emacs)
1039 (setq gdb-output-sink 'user)
1040 (let ((handler
1041 (car (cdr gdb-current-item))))
1042 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1043 (funcall handler))))
1045 (gdb-resync)
1046 (error "Phase error in gdb-prompt (got %s)" sink))))
1047 (let ((input (gdb-dequeue-input)))
1048 (if input
1049 (gdb-send-item input)
1050 (progn
1051 (setq gdb-prompting t)
1052 (gud-display-frame)))))
1054 (defun gdb-subprompt (ignored)
1055 "An annotation handler for non-top-level prompts."
1056 (setq gdb-prompting t))
1058 (defun gdb-starting (ignored)
1059 "An annotation handler for `starting'.
1060 This says that I/O for the subprocess is now the program being debugged,
1061 not GDB."
1062 (setq gdb-active-process t)
1063 (let ((sink gdb-output-sink))
1064 (cond
1065 ((eq sink 'user)
1066 (progn
1067 (setq gud-running t)
1068 (if gdb-use-inferior-io-buffer
1069 (setq gdb-output-sink 'inferior))))
1071 (gdb-resync)
1072 (error "Unexpected `starting' annotation")))))
1074 (defun gdb-stopping (ignored)
1075 "An annotation handler for `breakpoint' and other annotations.
1076 They say that I/O for the subprocess is now GDB, not the program
1077 being debugged."
1078 (if gdb-use-inferior-io-buffer
1079 (let ((sink gdb-output-sink))
1080 (cond
1081 ((eq sink 'inferior)
1082 (setq gdb-output-sink 'user))
1084 (gdb-resync)
1085 (error "Unexpected stopping annotation"))))))
1087 (defun gdb-exited (ignored)
1088 "An annotation handler for `exited' and `signalled'.
1089 They say that I/O for the subprocess is now GDB, not the program
1090 being debugged and that the program is no longer running. This
1091 function is used to change the focus of GUD tooltips to #define
1092 directives."
1093 (setq gdb-active-process nil)
1094 (setq gud-overlay-arrow-position nil)
1095 (setq gdb-overlay-arrow-position nil)
1096 (gdb-stopping ignored))
1098 (defun gdb-frame-begin (ignored)
1099 (let ((sink gdb-output-sink))
1100 (cond
1101 ((eq sink 'inferior)
1102 (setq gdb-output-sink 'user))
1103 ((eq sink 'user) t)
1104 ((eq sink 'emacs) t)
1106 (gdb-resync)
1107 (error "Unexpected frame-begin annotation (%S)" sink)))))
1109 (defun gdb-stopped (ignored)
1110 "An annotation handler for `stopped'.
1111 It is just like `gdb-stopping', except that if we already set the output
1112 sink to `user' in `gdb-stopping', that is fine."
1113 (setq gud-running nil)
1114 (setq gdb-active-process t)
1115 (let ((sink gdb-output-sink))
1116 (cond
1117 ((eq sink 'inferior)
1118 (setq gdb-output-sink 'user))
1119 ((eq sink 'user) t)
1121 (gdb-resync)
1122 (error "Unexpected stopped annotation")))))
1124 (defun gdb-error (ignored)
1125 (setq gdb-error (not gdb-error)))
1127 (defun gdb-post-prompt (ignored)
1128 "An annotation handler for `post-prompt'.
1129 This begins the collection of output from the current command if that
1130 happens to be appropriate."
1131 (unless gdb-pending-triggers
1132 (gdb-get-selected-frame)
1133 (gdb-invalidate-frames)
1134 (gdb-invalidate-breakpoints)
1135 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1136 ;; so gdb-frame-address is updated.
1137 ;; (gdb-invalidate-assembler)
1138 (gdb-invalidate-registers)
1139 (gdb-invalidate-memory)
1140 (gdb-invalidate-locals)
1141 (gdb-invalidate-threads)
1142 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1143 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1144 ;; Only needed/used with speedbar/watch expressions.
1145 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1146 (setq gdb-var-changed t) ; force update
1147 (dolist (var gdb-var-list)
1148 (setcar (nthcdr 5 var) nil))
1149 (gdb-var-update))))
1150 (let ((sink gdb-output-sink))
1151 (cond
1152 ((eq sink 'user) t)
1153 ((eq sink 'pre-emacs)
1154 (setq gdb-output-sink 'emacs))
1156 (gdb-resync)
1157 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1159 (defun gud-gdba-marker-filter (string)
1160 "A gud marker filter for gdb. Handle a burst of output from GDB."
1161 (if gdb-flush-pending-output
1163 (if gdb-enable-debug-log (push (cons 'recv string) gdb-debug-log))
1164 ;; Recall the left over gud-marker-acc from last time.
1165 (setq gud-marker-acc (concat gud-marker-acc string))
1166 ;; Start accumulating output for the GUD buffer.
1167 (let ((output ""))
1169 ;; Process all the complete markers in this chunk.
1170 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1171 (let ((annotation (match-string 1 gud-marker-acc)))
1173 ;; Stuff prior to the match is just ordinary output.
1174 ;; It is either concatenated to OUTPUT or directed
1175 ;; elsewhere.
1176 (setq output
1177 (gdb-concat-output
1178 output
1179 (substring gud-marker-acc 0 (match-beginning 0))))
1181 ;; Take that stuff off the gud-marker-acc.
1182 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1184 ;; Parse the tag from the annotation, and maybe its arguments.
1185 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1186 (let* ((annotation-type (match-string 1 annotation))
1187 (annotation-arguments (match-string 2 annotation))
1188 (annotation-rule (assoc annotation-type
1189 gdb-annotation-rules)))
1190 ;; Call the handler for this annotation.
1191 (if annotation-rule
1192 (funcall (car (cdr annotation-rule))
1193 annotation-arguments)
1194 ;; Else the annotation is not recognized. Ignore it silently,
1195 ;; so that GDB can add new annotations without causing
1196 ;; us to blow up.
1197 ))))
1199 ;; Does the remaining text end in a partial line?
1200 ;; If it does, then keep part of the gud-marker-acc until we get more.
1201 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1202 gud-marker-acc)
1203 (progn
1204 ;; Everything before the potential marker start can be output.
1205 (setq output
1206 (gdb-concat-output output
1207 (substring gud-marker-acc 0
1208 (match-beginning 0))))
1210 ;; Everything after, we save, to combine with later input.
1211 (setq gud-marker-acc (substring gud-marker-acc
1212 (match-beginning 0))))
1214 ;; In case we know the gud-marker-acc contains no partial annotations:
1215 (progn
1216 (setq output (gdb-concat-output output gud-marker-acc))
1217 (setq gud-marker-acc "")))
1218 output)))
1220 (defun gdb-concat-output (so-far new)
1221 (if gdb-error
1222 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1223 (let ((sink gdb-output-sink))
1224 (cond
1225 ((eq sink 'user) (concat so-far new))
1226 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1227 ((eq sink 'emacs)
1228 (gdb-append-to-partial-output new)
1229 so-far)
1230 ((eq sink 'inferior)
1231 (gdb-append-to-inferior-io new)
1232 so-far)
1234 (gdb-resync)
1235 (error "Bogon output sink %S" sink)))))
1237 (defun gdb-append-to-partial-output (string)
1238 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1239 (goto-char (point-max))
1240 (insert string)))
1242 (defun gdb-clear-partial-output ()
1243 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1244 (erase-buffer)))
1246 (defun gdb-append-to-inferior-io (string)
1247 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1248 (goto-char (point-max))
1249 (insert-before-markers string))
1250 (if (not (string-equal string ""))
1251 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io))))
1253 (defun gdb-clear-inferior-io ()
1254 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1255 (erase-buffer)))
1258 ;; One trick is to have a command who's output is always available in a buffer
1259 ;; of it's own, and is always up to date. We build several buffers of this
1260 ;; type.
1262 ;; There are two aspects to this: gdb has to tell us when the output for that
1263 ;; command might have changed, and we have to be able to run the command
1264 ;; behind the user's back.
1266 ;; The output phasing associated with the variable gdb-output-sink
1267 ;; help us to run commands behind the user's back.
1269 ;; Below is the code for specificly managing buffers of output from one
1270 ;; command.
1273 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1274 ;; It adds an input for the command we are tracking. It should be the
1275 ;; annotation rule binding of whatever gdb sends to tell us this command
1276 ;; might have changed it's output.
1278 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1279 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1280 ;; input in the input queue (see comment about ``gdb communications'' above).
1282 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1283 output-handler)
1284 `(defun ,name (&optional ignored)
1285 (if (and ,demand-predicate
1286 (not (member ',name
1287 gdb-pending-triggers)))
1288 (progn
1289 (gdb-enqueue-input
1290 (list ,gdb-command ',output-handler))
1291 (push ',name gdb-pending-triggers)))))
1293 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1294 `(defun ,name ()
1295 (setq gdb-pending-triggers
1296 (delq ',trigger
1297 gdb-pending-triggers))
1298 (let ((buf (gdb-get-buffer ',buf-key)))
1299 (and buf
1300 (with-current-buffer buf
1301 (let* ((window (get-buffer-window buf 0))
1302 (p (window-point window))
1303 (buffer-read-only nil))
1304 (erase-buffer)
1305 (insert-buffer-substring (gdb-get-create-buffer
1306 'gdb-partial-output-buffer))
1307 (set-window-point window p)))))
1308 ;; put customisation here
1309 (,custom-defun)))
1311 (defmacro def-gdb-auto-updated-buffer (buffer-key
1312 trigger-name gdb-command
1313 output-handler-name custom-defun)
1314 `(progn
1315 (def-gdb-auto-update-trigger ,trigger-name
1316 ;; The demand predicate:
1317 (gdb-get-buffer ',buffer-key)
1318 ,gdb-command
1319 ,output-handler-name)
1320 (def-gdb-auto-update-handler ,output-handler-name
1321 ,trigger-name ,buffer-key ,custom-defun)))
1325 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1327 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1328 'gdb-breakpoints-buffer-name
1329 'gdb-breakpoints-mode)
1331 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1332 ;; This defines the auto update rule for buffers of type
1333 ;; `gdb-breakpoints-buffer'.
1335 ;; It defines a function to serve as the annotation handler that
1336 ;; handles the `foo-invalidated' message. That function is called:
1337 gdb-invalidate-breakpoints
1339 ;; To update the buffer, this command is sent to gdb.
1340 "server info breakpoints\n"
1342 ;; This also defines a function to be the handler for the output
1343 ;; from the command above. That function will copy the output into
1344 ;; the appropriately typed buffer. That function will be called:
1345 gdb-info-breakpoints-handler
1346 ;; buffer specific functions
1347 gdb-info-breakpoints-custom)
1349 (defconst breakpoint-xpm-data
1350 "/* XPM */
1351 static char *magick[] = {
1352 /* columns rows colors chars-per-pixel */
1353 \"10 10 2 1\",
1354 \" c red\",
1355 \"+ c None\",
1356 /* pixels */
1357 \"+++ +++\",
1358 \"++ ++\",
1359 \"+ +\",
1360 \" \",
1361 \" \",
1362 \" \",
1363 \" \",
1364 \"+ +\",
1365 \"++ ++\",
1366 \"+++ +++\",
1368 "XPM data used for breakpoint icon.")
1370 (defconst breakpoint-enabled-pbm-data
1372 10 10\",
1373 0 0 0 0 1 1 1 1 0 0 0 0
1374 0 0 0 1 1 1 1 1 1 0 0 0
1375 0 0 1 1 1 1 1 1 1 1 0 0
1376 0 1 1 1 1 1 1 1 1 1 1 0
1377 0 1 1 1 1 1 1 1 1 1 1 0
1378 0 1 1 1 1 1 1 1 1 1 1 0
1379 0 1 1 1 1 1 1 1 1 1 1 0
1380 0 0 1 1 1 1 1 1 1 1 0 0
1381 0 0 0 1 1 1 1 1 1 0 0 0
1382 0 0 0 0 1 1 1 1 0 0 0 0"
1383 "PBM data used for enabled breakpoint icon.")
1385 (defconst breakpoint-disabled-pbm-data
1387 10 10\",
1388 0 0 1 0 1 0 1 0 0 0
1389 0 1 0 1 0 1 0 1 0 0
1390 1 0 1 0 1 0 1 0 1 0
1391 0 1 0 1 0 1 0 1 0 1
1392 1 0 1 0 1 0 1 0 1 0
1393 0 1 0 1 0 1 0 1 0 1
1394 1 0 1 0 1 0 1 0 1 0
1395 0 1 0 1 0 1 0 1 0 1
1396 0 0 1 0 1 0 1 0 1 0
1397 0 0 0 1 0 1 0 1 0 0"
1398 "PBM data used for disabled breakpoint icon.")
1400 (defvar breakpoint-enabled-icon nil
1401 "Icon for enabled breakpoint in display margin.")
1403 (defvar breakpoint-disabled-icon nil
1404 "Icon for disabled breakpoint in display margin.")
1406 ;; Bitmap for breakpoint in fringe
1407 (and (display-images-p)
1408 (define-fringe-bitmap 'breakpoint
1409 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1411 (defface breakpoint-enabled
1412 '((t
1413 :foreground "red"
1414 :weight bold))
1415 "Face for enabled breakpoint icon in fringe."
1416 :group 'gud)
1418 (defface breakpoint-disabled
1419 ;; We use different values of grey for different background types,
1420 ;; so that on low-color displays it will end up as something visible
1421 ;; if it has to be approximated.
1422 '((((background dark)) :foreground "grey60")
1423 (((background light)) :foreground "grey40"))
1424 "Face for disabled breakpoint icon in fringe."
1425 :group 'gud)
1427 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1428 (defun gdb-info-breakpoints-custom ()
1429 (let ((flag) (bptno))
1430 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1431 (dolist (buffer (buffer-list))
1432 (with-current-buffer buffer
1433 (if (and (eq gud-minor-mode 'gdba)
1434 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1435 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1436 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1437 (save-excursion
1438 (goto-char (point-min))
1439 (while (< (point) (- (point-max) 1))
1440 (forward-line 1)
1441 (if (looking-at "[^\t].*?breakpoint")
1442 (progn
1443 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1444 (setq bptno (match-string 1))
1445 (setq flag (char-after (match-beginning 2)))
1446 (beginning-of-line)
1447 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1448 (progn
1449 (let ((buffer-read-only nil))
1450 (add-text-properties (match-beginning 1) (match-end 1)
1451 '(face font-lock-function-name-face)))
1452 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1453 (let ((line (match-string 2)) (buffer-read-only nil)
1454 (file (match-string 1)))
1455 (add-text-properties (line-beginning-position)
1456 (line-end-position)
1457 '(mouse-face highlight
1458 help-echo "mouse-2, RET: visit breakpoint"))
1459 (unless (file-exists-p file)
1460 (setq file (cdr (assoc bptno gdb-location-alist))))
1461 (if (and file
1462 (not (string-equal file "File not found")))
1463 (with-current-buffer
1464 (find-file-noselect file 'nowarn)
1465 (set (make-local-variable 'gud-minor-mode)
1466 'gdba)
1467 (set (make-local-variable 'tool-bar-map)
1468 gud-tool-bar-map)
1469 ;; Only want one breakpoint icon at each
1470 ;; location.
1471 (save-excursion
1472 (goto-line (string-to-number line))
1473 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1474 (gdb-enqueue-input
1475 (list
1476 (concat gdb-server-prefix "list "
1477 (match-string-no-properties 1) ":1\n")
1478 'ignore))
1479 (gdb-enqueue-input
1480 (list (concat gdb-server-prefix "info source\n")
1481 `(lambda () (gdb-get-location
1482 ,bptno ,line ,flag))))))))))
1483 (end-of-line)))))
1484 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1486 (defun gdb-mouse-set-clear-breakpoint (event)
1487 "Set/clear breakpoint in left fringe/margin with mouse click."
1488 (interactive "e")
1489 (mouse-minibuffer-check event)
1490 (let ((posn (event-end event)))
1491 (if (numberp (posn-point posn))
1492 (with-selected-window (posn-window posn)
1493 (save-excursion
1494 (goto-char (posn-point posn))
1495 (if (or (posn-object posn)
1496 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1497 'breakpoint))
1498 (gud-remove nil)
1499 (gud-break nil)))))))
1501 (defun gdb-mouse-toggle-breakpoint-margin (event)
1502 "Enable/disable breakpoint in left margin with mouse click."
1503 (interactive "e")
1504 (mouse-minibuffer-check event)
1505 (let ((posn (event-end event)))
1506 (if (numberp (posn-point posn))
1507 (with-selected-window (posn-window posn)
1508 (save-excursion
1509 (goto-char (posn-point posn))
1510 (if (posn-object posn)
1511 (gdb-enqueue-input
1512 (list
1513 (let ((bptno (get-text-property
1514 0 'gdb-bptno (car (posn-string posn)))))
1515 (concat gdb-server-prefix
1516 (if (get-text-property
1517 0 'gdb-enabled (car (posn-string posn)))
1518 "disable "
1519 "enable ")
1520 bptno "\n"))
1521 'ignore))))))))
1523 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1524 "Enable/disable breakpoint in left fringe with mouse click."
1525 (interactive "e")
1526 (mouse-minibuffer-check event)
1527 (let* ((posn (event-end event))
1528 (pos (posn-point posn))
1529 obj)
1530 (when (numberp pos)
1531 (with-selected-window (posn-window posn)
1532 (save-excursion
1533 (set-buffer (window-buffer (selected-window)))
1534 (goto-char pos)
1535 (dolist (overlay (overlays-in pos pos))
1536 (when (overlay-get overlay 'put-break)
1537 (setq obj (overlay-get overlay 'before-string))))
1538 (when (stringp obj)
1539 (gdb-enqueue-input
1540 (list
1541 (concat gdb-server-prefix
1542 (if (get-text-property 0 'gdb-enabled obj)
1543 "disable "
1544 "enable ")
1545 (get-text-property 0 'gdb-bptno obj) "\n")
1546 'ignore))))))))
1548 (defun gdb-breakpoints-buffer-name ()
1549 (with-current-buffer gud-comint-buffer
1550 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1552 (defun gdb-display-breakpoints-buffer ()
1553 "Display status of user-settable breakpoints."
1554 (interactive)
1555 (gdb-display-buffer
1556 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1558 (defun gdb-frame-breakpoints-buffer ()
1559 "Display status of user-settable breakpoints in a new frame."
1560 (interactive)
1561 (let ((special-display-regexps (append special-display-regexps '(".*")))
1562 (special-display-frame-alist gdb-frame-parameters))
1563 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1565 (defvar gdb-breakpoints-mode-map
1566 (let ((map (make-sparse-keymap))
1567 (menu (make-sparse-keymap "Breakpoints")))
1568 (define-key menu [quit] '("Quit" . kill-this-buffer))
1569 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1570 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1571 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1572 (suppress-keymap map)
1573 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1574 (define-key map " " 'gdb-toggle-breakpoint)
1575 (define-key map "D" 'gdb-delete-breakpoint)
1576 (define-key map "q" 'kill-this-buffer)
1577 (define-key map "\r" 'gdb-goto-breakpoint)
1578 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1579 (define-key map [follow-link] 'mouse-face)
1580 map))
1582 (defun gdb-breakpoints-mode ()
1583 "Major mode for gdb breakpoints.
1585 \\{gdb-breakpoints-mode-map}"
1586 (kill-all-local-variables)
1587 (setq major-mode 'gdb-breakpoints-mode)
1588 (setq mode-name "Breakpoints")
1589 (use-local-map gdb-breakpoints-mode-map)
1590 (setq buffer-read-only t)
1591 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1592 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1593 'gdb-invalidate-breakpoints
1594 'gdbmi-invalidate-breakpoints))
1596 (defun gdb-toggle-breakpoint ()
1597 "Enable/disable breakpoint at current line."
1598 (interactive)
1599 (save-excursion
1600 (beginning-of-line 1)
1601 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1602 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1603 (looking-at
1604 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1605 (gdb-enqueue-input
1606 (list
1607 (concat gdb-server-prefix
1608 (if (eq ?y (char-after (match-beginning 2)))
1609 "disable "
1610 "enable ")
1611 (match-string 1) "\n") 'ignore))
1612 (error "Not recognized as break/watchpoint line"))))
1614 (defun gdb-delete-breakpoint ()
1615 "Delete the breakpoint at current line."
1616 (interactive)
1617 (beginning-of-line 1)
1618 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1619 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
1620 (looking-at
1621 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1622 (gdb-enqueue-input
1623 (list
1624 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1625 (error "Not recognized as break/watchpoint line")))
1627 (defun gdb-goto-breakpoint (&optional event)
1628 "Display the breakpoint location specified at current line."
1629 (interactive (list last-input-event))
1630 (if event (posn-set-point (event-end event)))
1631 ;; Hack to stop gdb-goto-breakpoint displaying in GUD buffer.
1632 (let ((window (get-buffer-window gud-comint-buffer)))
1633 (if window (save-selected-window (select-window window))))
1634 (save-excursion
1635 (beginning-of-line 1)
1636 (if (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1637 (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1638 (looking-at
1639 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
1640 \\(\\S-+\\):\\([0-9]+\\)"))
1641 (let ((bptno (match-string 1))
1642 (file (match-string 2))
1643 (line (match-string 3)))
1644 (save-selected-window
1645 (let* ((buf (find-file-noselect
1646 (if (file-exists-p file) file
1647 (cdr (assoc bptno gdb-location-alist)))))
1648 (window (display-buffer buf)))
1649 (with-current-buffer buf
1650 (goto-line (string-to-number line))
1651 (set-window-point window (point))))))
1652 (error "Not recognized as break/watchpoint line"))))
1655 ;; Frames buffer. This displays a perpetually correct bactracktrace
1656 ;; (from the command `where').
1658 ;; Alas, if your stack is deep, it is costly.
1660 (gdb-set-buffer-rules 'gdb-stack-buffer
1661 'gdb-stack-buffer-name
1662 'gdb-frames-mode)
1664 (def-gdb-auto-updated-buffer gdb-stack-buffer
1665 gdb-invalidate-frames
1666 "server where\n"
1667 gdb-info-frames-handler
1668 gdb-info-frames-custom)
1670 (defun gdb-info-frames-custom ()
1671 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1672 (save-excursion
1673 (let ((buffer-read-only nil)
1674 bl el)
1675 (goto-char (point-min))
1676 (while (< (point) (point-max))
1677 (setq bl (line-beginning-position)
1678 el (line-end-position))
1679 (when (looking-at "#")
1680 (add-text-properties bl el
1681 '(mouse-face highlight
1682 help-echo "mouse-2, RET: Select frame")))
1683 (goto-char bl)
1684 (when (looking-at "^#\\([0-9]+\\)")
1685 (when (string-equal (match-string 1) gdb-frame-number)
1686 (put-text-property bl (+ bl 4)
1687 'face '(:inverse-video t)))
1688 (when (re-search-forward
1689 (concat
1690 (if (string-equal (match-string 1) "0") "" " in ")
1691 "\\([^ ]+\\) (") el t)
1692 (put-text-property (match-beginning 1) (match-end 1)
1693 'face font-lock-function-name-face)
1694 (setq bl (match-end 0))
1695 (while (re-search-forward "<\\([^>]+\\)>" el t)
1696 (put-text-property (match-beginning 1) (match-end 1)
1697 'face font-lock-function-name-face))
1698 (goto-char bl)
1699 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1700 (put-text-property (match-beginning 1) (match-end 1)
1701 'face font-lock-variable-name-face))))
1702 (forward-line 1))))))
1704 (defun gdb-stack-buffer-name ()
1705 (with-current-buffer gud-comint-buffer
1706 (concat "*stack frames of " (gdb-get-target-string) "*")))
1708 (defun gdb-display-stack-buffer ()
1709 "Display backtrace of current stack."
1710 (interactive)
1711 (gdb-display-buffer
1712 (gdb-get-create-buffer 'gdb-stack-buffer)))
1714 (defun gdb-frame-stack-buffer ()
1715 "Display backtrace of current stack in a new frame."
1716 (interactive)
1717 (let ((special-display-regexps (append special-display-regexps '(".*")))
1718 (special-display-frame-alist gdb-frame-parameters))
1719 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1721 (defvar gdb-frames-mode-map
1722 (let ((map (make-sparse-keymap)))
1723 (suppress-keymap map)
1724 (define-key map "q" 'kill-this-buffer)
1725 (define-key map "\r" 'gdb-frames-select)
1726 (define-key map [mouse-2] 'gdb-frames-select)
1727 (define-key map [follow-link] 'mouse-face)
1728 map))
1730 (defun gdb-frames-mode ()
1731 "Major mode for gdb frames.
1733 \\{gdb-frames-mode-map}"
1734 (kill-all-local-variables)
1735 (setq major-mode 'gdb-frames-mode)
1736 (setq mode-name "Frames")
1737 (setq buffer-read-only t)
1738 (use-local-map gdb-frames-mode-map)
1739 (run-mode-hooks 'gdb-frames-mode-hook)
1740 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1741 'gdb-invalidate-frames
1742 'gdbmi-invalidate-frames))
1744 (defun gdb-get-frame-number ()
1745 (save-excursion
1746 (end-of-line)
1747 (let* ((start (line-beginning-position))
1748 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
1749 (n (or (and pos (match-string-no-properties 1)) "0")))
1750 n)))
1752 (defun gdb-frames-select (&optional event)
1753 "Select the frame and display the relevant source."
1754 (interactive (list last-input-event))
1755 (if event (posn-set-point (event-end event)))
1756 (gdb-enqueue-input
1757 (list (concat gdb-server-prefix "frame "
1758 (gdb-get-frame-number) "\n") 'ignore))
1759 (gud-display-frame))
1762 ;; Threads buffer. This displays a selectable thread list.
1764 (gdb-set-buffer-rules 'gdb-threads-buffer
1765 'gdb-threads-buffer-name
1766 'gdb-threads-mode)
1768 (def-gdb-auto-updated-buffer gdb-threads-buffer
1769 gdb-invalidate-threads
1770 (concat gdb-server-prefix "info threads\n")
1771 gdb-info-threads-handler
1772 gdb-info-threads-custom)
1774 (defun gdb-info-threads-custom ()
1775 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1776 (let ((buffer-read-only nil))
1777 (goto-char (point-min))
1778 (while (< (point) (point-max))
1779 (unless (looking-at "No ")
1780 (add-text-properties (line-beginning-position) (line-end-position)
1781 '(mouse-face highlight
1782 help-echo "mouse-2, RET: select thread")))
1783 (forward-line 1)))))
1785 (defun gdb-threads-buffer-name ()
1786 (with-current-buffer gud-comint-buffer
1787 (concat "*threads of " (gdb-get-target-string) "*")))
1789 (defun gdb-display-threads-buffer ()
1790 "Display IDs of currently known threads."
1791 (interactive)
1792 (gdb-display-buffer
1793 (gdb-get-create-buffer 'gdb-threads-buffer)))
1795 (defun gdb-frame-threads-buffer ()
1796 "Display IDs of currently known threads in a new frame."
1797 (interactive)
1798 (let ((special-display-regexps (append special-display-regexps '(".*")))
1799 (special-display-frame-alist gdb-frame-parameters))
1800 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1802 (defvar gdb-threads-mode-map
1803 (let ((map (make-sparse-keymap)))
1804 (suppress-keymap map)
1805 (define-key map "q" 'kill-this-buffer)
1806 (define-key map "\r" 'gdb-threads-select)
1807 (define-key map [mouse-2] 'gdb-threads-select)
1808 (define-key map [follow-link] 'mouse-face)
1809 map))
1811 (defvar gdb-threads-font-lock-keywords
1813 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1814 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1815 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1817 "Font lock keywords used in `gdb-threads-mode'.")
1819 (defun gdb-threads-mode ()
1820 "Major mode for gdb frames.
1822 \\{gdb-threads-mode-map}"
1823 (kill-all-local-variables)
1824 (setq major-mode 'gdb-threads-mode)
1825 (setq mode-name "Threads")
1826 (setq buffer-read-only t)
1827 (use-local-map gdb-threads-mode-map)
1828 (set (make-local-variable 'font-lock-defaults)
1829 '(gdb-threads-font-lock-keywords))
1830 (run-mode-hooks 'gdb-threads-mode-hook)
1831 'gdb-invalidate-threads)
1833 (defun gdb-get-thread-number ()
1834 (save-excursion
1835 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1836 (match-string-no-properties 1)))
1838 (defun gdb-threads-select (&optional event)
1839 "Select the thread and display the relevant source."
1840 (interactive (list last-input-event))
1841 (if event (posn-set-point (event-end event)))
1842 (gdb-enqueue-input
1843 (list (concat gdb-server-prefix "thread "
1844 (gdb-get-thread-number) "\n") 'ignore))
1845 (gud-display-frame))
1848 ;; Registers buffer.
1850 (defcustom gdb-all-registers nil
1851 "Non-nil means include floating-point registers."
1852 :type 'boolean
1853 :group 'gud
1854 :version "22.1")
1856 (gdb-set-buffer-rules 'gdb-registers-buffer
1857 'gdb-registers-buffer-name
1858 'gdb-registers-mode)
1860 (def-gdb-auto-updated-buffer gdb-registers-buffer
1861 gdb-invalidate-registers
1862 (concat
1863 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1864 gdb-info-registers-handler
1865 gdb-info-registers-custom)
1867 (defun gdb-info-registers-custom ()
1868 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1869 (save-excursion
1870 (let ((buffer-read-only nil)
1871 start end)
1872 (goto-char (point-min))
1873 (while (< (point) (point-max))
1874 (setq start (line-beginning-position))
1875 (setq end (line-end-position))
1876 (when (looking-at "^[^ ]+")
1877 (unless (string-equal (match-string 0) "The")
1878 (put-text-property start (match-end 0)
1879 'face font-lock-variable-name-face)
1880 (add-text-properties start end
1881 '(help-echo "mouse-2: edit value"
1882 mouse-face highlight))))
1883 (forward-line 1))))))
1885 (defun gdb-edit-register-value (&optional event)
1886 (interactive (list last-input-event))
1887 (save-excursion
1888 (if event (posn-set-point (event-end event)))
1889 (beginning-of-line)
1890 (let* ((register (current-word))
1891 (value (read-string (format "New value (%s): " register))))
1892 (gdb-enqueue-input
1893 (list (concat gdb-server-prefix "set $" register "=" value "\n")
1894 'ignore)))))
1896 (defvar gdb-registers-mode-map
1897 (let ((map (make-sparse-keymap)))
1898 (suppress-keymap map)
1899 (define-key map "\r" 'gdb-edit-register-value)
1900 (define-key map [mouse-2] 'gdb-edit-register-value)
1901 (define-key map " " 'gdb-all-registers)
1902 (define-key map "q" 'kill-this-buffer)
1903 map))
1905 (defun gdb-registers-mode ()
1906 "Major mode for gdb registers.
1908 \\{gdb-registers-mode-map}"
1909 (kill-all-local-variables)
1910 (setq major-mode 'gdb-registers-mode)
1911 (setq mode-name (if gdb-all-registers "Registers:All" "Registers:"))
1912 (setq buffer-read-only t)
1913 (use-local-map gdb-registers-mode-map)
1914 (run-mode-hooks 'gdb-registers-mode-hook)
1915 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1916 'gdb-invalidate-registers
1917 'gdbmi-invalidate-registers))
1919 (defun gdb-registers-buffer-name ()
1920 (with-current-buffer gud-comint-buffer
1921 (concat "*registers of " (gdb-get-target-string) "*")))
1923 (defun gdb-display-registers-buffer ()
1924 "Display integer register contents."
1925 (interactive)
1926 (gdb-display-buffer
1927 (gdb-get-create-buffer 'gdb-registers-buffer)))
1929 (defun gdb-frame-registers-buffer ()
1930 "Display integer register contents in a new frame."
1931 (interactive)
1932 (let ((special-display-regexps (append special-display-regexps '(".*")))
1933 (special-display-frame-alist gdb-frame-parameters))
1934 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1936 (defun gdb-all-registers ()
1937 "Toggle the display of floating-point registers."
1938 (interactive)
1939 (if gdb-all-registers
1940 (progn
1941 (setq gdb-all-registers nil)
1942 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
1943 (setq mode-name "Registers:")))
1944 (setq gdb-all-registers t)
1945 (with-current-buffer (gdb-get-create-buffer 'gdb-registers-buffer)
1946 (setq mode-name "Registers:All")))
1947 (message (format "Display of floating-point registers %sabled"
1948 (if gdb-all-registers "en" "dis")))
1949 (gdb-invalidate-registers))
1952 ;; Memory buffer.
1954 (defcustom gdb-memory-repeat-count 32
1955 "Number of data items in memory window."
1956 :type 'integer
1957 :group 'gud
1958 :version "22.1")
1960 (defcustom gdb-memory-format "x"
1961 "Display format of data items in memory window."
1962 :type '(choice (const :tag "Hexadecimal" "x")
1963 (const :tag "Signed decimal" "d")
1964 (const :tag "Unsigned decimal" "u")
1965 (const :tag "Octal" "o")
1966 (const :tag "Binary" "t"))
1967 :group 'gud
1968 :version "22.1")
1970 (defcustom gdb-memory-unit "w"
1971 "Unit size of data items in memory window."
1972 :type '(choice (const :tag "Byte" "b")
1973 (const :tag "Halfword" "h")
1974 (const :tag "Word" "w")
1975 (const :tag "Giant word" "g"))
1976 :group 'gud
1977 :version "22.1")
1979 (gdb-set-buffer-rules 'gdb-memory-buffer
1980 'gdb-memory-buffer-name
1981 'gdb-memory-mode)
1983 (def-gdb-auto-updated-buffer gdb-memory-buffer
1984 gdb-invalidate-memory
1985 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1986 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1987 gdb-read-memory-handler
1988 gdb-read-memory-custom)
1990 (defun gdb-read-memory-custom ()
1991 (save-excursion
1992 (goto-char (point-min))
1993 (if (looking-at "0x[[:xdigit:]]+")
1994 (setq gdb-memory-address (match-string 0)))))
1996 (defvar gdb-memory-mode-map
1997 (let ((map (make-sparse-keymap)))
1998 (suppress-keymap map)
1999 (define-key map "q" 'kill-this-buffer)
2000 map))
2002 (defun gdb-memory-set-address (event)
2003 "Set the start memory address."
2004 (interactive "e")
2005 (save-selected-window
2006 (select-window (posn-window (event-start event)))
2007 (let ((arg (read-from-minibuffer "Memory address: ")))
2008 (setq gdb-memory-address arg))
2009 (gdb-invalidate-memory)))
2011 (defun gdb-memory-set-repeat-count (event)
2012 "Set the number of data items in memory window."
2013 (interactive "e")
2014 (save-selected-window
2015 (select-window (posn-window (event-start event)))
2016 (let* ((arg (read-from-minibuffer "Repeat count: "))
2017 (count (string-to-number arg)))
2018 (if (<= count 0)
2019 (error "Positive numbers only")
2020 (customize-set-variable 'gdb-memory-repeat-count count)
2021 (gdb-invalidate-memory)))))
2023 (defun gdb-memory-format-binary ()
2024 "Set the display format to binary."
2025 (interactive)
2026 (customize-set-variable 'gdb-memory-format "t")
2027 (gdb-invalidate-memory))
2029 (defun gdb-memory-format-octal ()
2030 "Set the display format to octal."
2031 (interactive)
2032 (customize-set-variable 'gdb-memory-format "o")
2033 (gdb-invalidate-memory))
2035 (defun gdb-memory-format-unsigned ()
2036 "Set the display format to unsigned decimal."
2037 (interactive)
2038 (customize-set-variable 'gdb-memory-format "u")
2039 (gdb-invalidate-memory))
2041 (defun gdb-memory-format-signed ()
2042 "Set the display format to decimal."
2043 (interactive)
2044 (customize-set-variable 'gdb-memory-format "d")
2045 (gdb-invalidate-memory))
2047 (defun gdb-memory-format-hexadecimal ()
2048 "Set the display format to hexadecimal."
2049 (interactive)
2050 (customize-set-variable 'gdb-memory-format "x")
2051 (gdb-invalidate-memory))
2053 (defvar gdb-memory-format-keymap
2054 (let ((map (make-sparse-keymap)))
2055 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2056 map)
2057 "Keymap to select format in the header line.")
2059 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2060 "Menu of display formats in the header line.")
2062 (define-key gdb-memory-format-menu [binary]
2063 '(menu-item "Binary" gdb-memory-format-binary
2064 :button (:radio . (equal gdb-memory-format "t"))))
2065 (define-key gdb-memory-format-menu [octal]
2066 '(menu-item "Octal" gdb-memory-format-octal
2067 :button (:radio . (equal gdb-memory-format "o"))))
2068 (define-key gdb-memory-format-menu [unsigned]
2069 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2070 :button (:radio . (equal gdb-memory-format "u"))))
2071 (define-key gdb-memory-format-menu [signed]
2072 '(menu-item "Signed Decimal" gdb-memory-format-signed
2073 :button (:radio . (equal gdb-memory-format "d"))))
2074 (define-key gdb-memory-format-menu [hexadecimal]
2075 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2076 :button (:radio . (equal gdb-memory-format "x"))))
2078 (defun gdb-memory-format-menu (event)
2079 (interactive "@e")
2080 (x-popup-menu event gdb-memory-format-menu))
2082 (defun gdb-memory-format-menu-1 (event)
2083 (interactive "e")
2084 (save-selected-window
2085 (select-window (posn-window (event-start event)))
2086 (let* ((selection (gdb-memory-format-menu event))
2087 (binding (and selection (lookup-key gdb-memory-format-menu
2088 (vector (car selection))))))
2089 (if binding (call-interactively binding)))))
2091 (defun gdb-memory-unit-giant ()
2092 "Set the unit size to giant words (eight bytes)."
2093 (interactive)
2094 (customize-set-variable 'gdb-memory-unit "g")
2095 (gdb-invalidate-memory))
2097 (defun gdb-memory-unit-word ()
2098 "Set the unit size to words (four bytes)."
2099 (interactive)
2100 (customize-set-variable 'gdb-memory-unit "w")
2101 (gdb-invalidate-memory))
2103 (defun gdb-memory-unit-halfword ()
2104 "Set the unit size to halfwords (two bytes)."
2105 (interactive)
2106 (customize-set-variable 'gdb-memory-unit "h")
2107 (gdb-invalidate-memory))
2109 (defun gdb-memory-unit-byte ()
2110 "Set the unit size to bytes."
2111 (interactive)
2112 (customize-set-variable 'gdb-memory-unit "b")
2113 (gdb-invalidate-memory))
2115 (defvar gdb-memory-unit-keymap
2116 (let ((map (make-sparse-keymap)))
2117 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2118 map)
2119 "Keymap to select units in the header line.")
2121 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2122 "Menu of units in the header line.")
2124 (define-key gdb-memory-unit-menu [giantwords]
2125 '(menu-item "Giant words" gdb-memory-unit-giant
2126 :button (:radio . (equal gdb-memory-unit "g"))))
2127 (define-key gdb-memory-unit-menu [words]
2128 '(menu-item "Words" gdb-memory-unit-word
2129 :button (:radio . (equal gdb-memory-unit "w"))))
2130 (define-key gdb-memory-unit-menu [halfwords]
2131 '(menu-item "Halfwords" gdb-memory-unit-halfword
2132 :button (:radio . (equal gdb-memory-unit "h"))))
2133 (define-key gdb-memory-unit-menu [bytes]
2134 '(menu-item "Bytes" gdb-memory-unit-byte
2135 :button (:radio . (equal gdb-memory-unit "b"))))
2137 (defun gdb-memory-unit-menu (event)
2138 (interactive "@e")
2139 (x-popup-menu event gdb-memory-unit-menu))
2141 (defun gdb-memory-unit-menu-1 (event)
2142 (interactive "e")
2143 (save-selected-window
2144 (select-window (posn-window (event-start event)))
2145 (let* ((selection (gdb-memory-unit-menu event))
2146 (binding (and selection (lookup-key gdb-memory-unit-menu
2147 (vector (car selection))))))
2148 (if binding (call-interactively binding)))))
2150 ;;from make-mode-line-mouse-map
2151 (defun gdb-make-header-line-mouse-map (mouse function) "\
2152 Return a keymap with single entry for mouse key MOUSE on the header line.
2153 MOUSE is defined to run function FUNCTION with no args in the buffer
2154 corresponding to the mode line clicked."
2155 (let ((map (make-sparse-keymap)))
2156 (define-key map (vector 'header-line mouse) function)
2157 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2158 map))
2160 (defvar gdb-memory-font-lock-keywords
2161 '(;; <__function.name+n>
2162 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2164 "Font lock keywords used in `gdb-memory-mode'.")
2166 (defun gdb-memory-mode ()
2167 "Major mode for examining memory.
2169 \\{gdb-memory-mode-map}"
2170 (kill-all-local-variables)
2171 (setq major-mode 'gdb-memory-mode)
2172 (setq mode-name "Memory")
2173 (setq buffer-read-only t)
2174 (use-local-map gdb-memory-mode-map)
2175 (setq header-line-format
2176 '(:eval
2177 (concat
2178 "Read address["
2179 (propertize
2181 'face font-lock-warning-face
2182 'help-echo "mouse-1: Decrement address"
2183 'mouse-face 'mode-line-highlight
2184 'local-map
2185 (gdb-make-header-line-mouse-map
2186 'mouse-1
2187 #'(lambda () (interactive)
2188 (let ((gdb-memory-address
2189 ;; Let GDB do the arithmetic.
2190 (concat
2191 gdb-memory-address " - "
2192 (number-to-string
2193 (* gdb-memory-repeat-count
2194 (cond ((string= gdb-memory-unit "b") 1)
2195 ((string= gdb-memory-unit "h") 2)
2196 ((string= gdb-memory-unit "w") 4)
2197 ((string= gdb-memory-unit "g") 8)))))))
2198 (gdb-invalidate-memory)))))
2200 (propertize "+"
2201 'face font-lock-warning-face
2202 'help-echo "mouse-1: Increment address"
2203 'mouse-face 'mode-line-highlight
2204 'local-map (gdb-make-header-line-mouse-map
2205 'mouse-1
2206 #'(lambda () (interactive)
2207 (let ((gdb-memory-address nil))
2208 (gdb-invalidate-memory)))))
2209 "]: "
2210 (propertize gdb-memory-address
2211 'face font-lock-warning-face
2212 'help-echo "mouse-1: Set memory address"
2213 'mouse-face 'mode-line-highlight
2214 'local-map (gdb-make-header-line-mouse-map
2215 'mouse-1
2216 #'gdb-memory-set-address))
2217 " Repeat Count: "
2218 (propertize (number-to-string gdb-memory-repeat-count)
2219 'face font-lock-warning-face
2220 'help-echo "mouse-1: Set repeat count"
2221 'mouse-face 'mode-line-highlight
2222 'local-map (gdb-make-header-line-mouse-map
2223 'mouse-1
2224 #'gdb-memory-set-repeat-count))
2225 " Display Format: "
2226 (propertize gdb-memory-format
2227 'face font-lock-warning-face
2228 'help-echo "mouse-3: Select display format"
2229 'mouse-face 'mode-line-highlight
2230 'local-map gdb-memory-format-keymap)
2231 " Unit Size: "
2232 (propertize gdb-memory-unit
2233 'face font-lock-warning-face
2234 'help-echo "mouse-3: Select unit size"
2235 'mouse-face 'mode-line-highlight
2236 'local-map gdb-memory-unit-keymap))))
2237 (set (make-local-variable 'font-lock-defaults)
2238 '(gdb-memory-font-lock-keywords))
2239 (run-mode-hooks 'gdb-memory-mode-hook)
2240 'gdb-invalidate-memory)
2242 (defun gdb-memory-buffer-name ()
2243 (with-current-buffer gud-comint-buffer
2244 (concat "*memory of " (gdb-get-target-string) "*")))
2246 (defun gdb-display-memory-buffer ()
2247 "Display memory contents."
2248 (interactive)
2249 (gdb-display-buffer
2250 (gdb-get-create-buffer 'gdb-memory-buffer)))
2252 (defun gdb-frame-memory-buffer ()
2253 "Display memory contents in a new frame."
2254 (interactive)
2255 (let ((special-display-regexps (append special-display-regexps '(".*")))
2256 (special-display-frame-alist gdb-frame-parameters))
2257 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2260 ;; Locals buffer.
2262 (gdb-set-buffer-rules 'gdb-locals-buffer
2263 'gdb-locals-buffer-name
2264 'gdb-locals-mode)
2266 (def-gdb-auto-update-trigger gdb-invalidate-locals
2267 (gdb-get-buffer 'gdb-locals-buffer)
2268 "server info locals\n"
2269 gdb-info-locals-handler)
2271 (defvar gdb-locals-watch-keymap
2272 (let ((map (make-sparse-keymap)))
2273 (define-key map "\r" '(lambda () (interactive)
2274 (beginning-of-line)
2275 (gud-watch)))
2276 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2277 (mouse-set-point event)
2278 (beginning-of-line)
2279 (gud-watch)))
2280 map)
2281 "Keymap to create watch expression of a complex data type local variable.")
2283 (defconst gdb-struct-string
2284 (concat (propertize "[struct/union]"
2285 'mouse-face 'highlight
2286 'help-echo "mouse-2: create watch expression"
2287 'local-map gdb-locals-watch-keymap) "\n"))
2289 (defconst gdb-array-string
2290 (concat " " (propertize "[array]"
2291 'mouse-face 'highlight
2292 'help-echo "mouse-2: create watch expression"
2293 'local-map gdb-locals-watch-keymap) "\n"))
2295 ;; Abbreviate for arrays and structures.
2296 ;; These can be expanded using gud-display.
2297 (defun gdb-info-locals-handler ()
2298 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2299 gdb-pending-triggers))
2300 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2301 (with-current-buffer buf
2302 (goto-char (point-min))
2303 (while (re-search-forward "^[ }].*\n" nil t)
2304 (replace-match "" nil nil))
2305 (goto-char (point-min))
2306 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2307 (replace-match gdb-struct-string nil nil))
2308 (goto-char (point-min))
2309 (while (re-search-forward "\\s-*{.*\n" nil t)
2310 (replace-match gdb-array-string nil nil))))
2311 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2312 (and buf
2313 (with-current-buffer buf
2314 (let* ((window (get-buffer-window buf 0))
2315 (p (window-point window))
2316 (buffer-read-only nil))
2317 (erase-buffer)
2318 (insert-buffer-substring (gdb-get-create-buffer
2319 'gdb-partial-output-buffer))
2320 (set-window-point window p)))))
2321 (run-hooks 'gdb-info-locals-hook))
2323 (defvar gdb-locals-mode-map
2324 (let ((map (make-sparse-keymap)))
2325 (suppress-keymap map)
2326 (define-key map "q" 'kill-this-buffer)
2327 map))
2329 (defvar gdb-locals-font-lock-keywords
2331 ;; var = (struct struct_tag) value
2332 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
2333 (1 font-lock-variable-name-face)
2334 (3 font-lock-keyword-face)
2335 (4 font-lock-type-face))
2336 ;; var = (type) value
2337 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
2338 (1 font-lock-variable-name-face)
2339 (3 font-lock-type-face))
2340 ;; var = val
2341 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
2342 (1 font-lock-variable-name-face))
2344 "Font lock keywords used in `gdb-local-mode'.")
2346 (defun gdb-locals-mode ()
2347 "Major mode for gdb locals.
2349 \\{gdb-locals-mode-map}"
2350 (kill-all-local-variables)
2351 (setq major-mode 'gdb-locals-mode)
2352 (setq mode-name (concat "Locals:" gdb-selected-frame))
2353 (setq buffer-read-only t)
2354 (use-local-map gdb-locals-mode-map)
2355 (set (make-local-variable 'font-lock-defaults)
2356 '(gdb-locals-font-lock-keywords))
2357 (run-mode-hooks 'gdb-locals-mode-hook)
2358 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2359 'gdb-invalidate-locals
2360 'gdbmi-invalidate-locals))
2362 (defun gdb-locals-buffer-name ()
2363 (with-current-buffer gud-comint-buffer
2364 (concat "*locals of " (gdb-get-target-string) "*")))
2366 (defun gdb-display-locals-buffer ()
2367 "Display local variables of current stack and their values."
2368 (interactive)
2369 (gdb-display-buffer
2370 (gdb-get-create-buffer 'gdb-locals-buffer)))
2372 (defun gdb-frame-locals-buffer ()
2373 "Display local variables of current stack and their values in a new frame."
2374 (interactive)
2375 (let ((special-display-regexps (append special-display-regexps '(".*")))
2376 (special-display-frame-alist gdb-frame-parameters))
2377 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2380 ;;;; Window management
2381 (defun gdb-display-buffer (buf &optional size)
2382 (let ((answer (get-buffer-window buf 0))
2383 (must-split nil))
2384 (if answer
2385 (display-buffer buf nil 0) ;Raise the frame if necessary.
2386 ;; The buffer is not yet displayed.
2387 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2388 (let ((window (get-lru-window)))
2389 (if (and window
2390 (not (eq window (get-buffer-window gud-comint-buffer))))
2391 (progn
2392 (set-window-buffer window buf)
2393 (setq answer window))
2394 (setq must-split t)))
2395 (if must-split
2396 (let* ((largest (get-largest-window))
2397 (cur-size (window-height largest))
2398 (new-size (and size (< size cur-size) (- cur-size size))))
2399 (setq answer (split-window largest new-size))
2400 (set-window-buffer answer buf)
2401 (set-window-dedicated-p answer t)))
2402 answer)))
2405 ;;; Shared keymap initialization:
2407 (let ((menu (make-sparse-keymap "GDB-Windows")))
2408 (define-key gud-menu-map [displays]
2409 `(menu-item "GDB-Windows" ,menu
2410 :visible (memq gud-minor-mode '(gdbmi gdba))))
2411 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2412 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2413 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2414 (define-key menu [disassembly]
2415 '("Disassembly" . gdb-display-assembler-buffer))
2416 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2417 (define-key menu [inferior]
2418 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2419 :enable gdb-use-inferior-io-buffer))
2420 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2421 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2422 (define-key menu [breakpoints]
2423 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2425 (let ((menu (make-sparse-keymap "GDB-Frames")))
2426 (define-key gud-menu-map [frames]
2427 `(menu-item "GDB-Frames" ,menu
2428 :visible (memq gud-minor-mode '(gdbmi gdba))))
2429 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2430 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2431 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2432 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2433 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2434 (define-key menu [inferior]
2435 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2436 :enable gdb-use-inferior-io-buffer))
2437 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2438 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2439 (define-key menu [breakpoints]
2440 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2442 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2443 (define-key gud-menu-map [ui]
2444 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2445 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2446 (define-key menu [gdb-use-inferior-io]
2447 '(menu-item "Separate inferior IO" gdb-use-inferior-io-buffer
2448 :visible (eq gud-minor-mode 'gdba)
2449 :help "Toggle separate IO for inferior."
2450 :button (:toggle . gdb-use-inferior-io-buffer)))
2451 (define-key menu [gdb-many-windows]
2452 '(menu-item "Display Other Windows" gdb-many-windows
2453 :help "Toggle display of locals, stack and breakpoint information"
2454 :button (:toggle . gdb-many-windows)))
2455 (define-key menu [gdb-restore-windows]
2456 '(menu-item "Restore Window Layout" gdb-restore-windows
2457 :help "Restore standard layout for debug session.")))
2459 (defun gdb-frame-gdb-buffer ()
2460 "Display GUD buffer in a new frame."
2461 (interactive)
2462 (let ((special-display-regexps (append special-display-regexps '(".*")))
2463 (special-display-frame-alist gdb-frame-parameters)
2464 (same-window-regexps nil))
2465 (display-buffer gud-comint-buffer)))
2467 (defun gdb-display-gdb-buffer ()
2468 "Display GUD buffer."
2469 (interactive)
2470 (let ((same-window-regexps nil))
2471 (pop-to-buffer gud-comint-buffer)))
2473 (defun gdb-set-window-buffer (name)
2474 (set-window-buffer (selected-window) (get-buffer name))
2475 (set-window-dedicated-p (selected-window) t))
2477 (defun gdb-setup-windows ()
2478 "Layout the window pattern for `gdb-many-windows'."
2479 (gdb-display-locals-buffer)
2480 (gdb-display-stack-buffer)
2481 (delete-other-windows)
2482 (gdb-display-breakpoints-buffer)
2483 (delete-other-windows)
2484 ; Don't dedicate.
2485 (pop-to-buffer gud-comint-buffer)
2486 (split-window nil ( / ( * (window-height) 3) 4))
2487 (split-window nil ( / (window-height) 3))
2488 (split-window-horizontally)
2489 (other-window 1)
2490 (gdb-set-window-buffer (gdb-locals-buffer-name))
2491 (other-window 1)
2492 (switch-to-buffer
2493 (if gud-last-last-frame
2494 (gud-find-file (car gud-last-last-frame))
2495 (gud-find-file gdb-main-file)))
2496 (when gdb-use-inferior-io-buffer
2497 (split-window-horizontally)
2498 (other-window 1)
2499 (gdb-set-window-buffer
2500 (gdb-get-create-buffer 'gdb-inferior-io)))
2501 (other-window 1)
2502 (gdb-set-window-buffer (gdb-stack-buffer-name))
2503 (split-window-horizontally)
2504 (other-window 1)
2505 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2506 (other-window 1))
2508 (defcustom gdb-many-windows nil
2509 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2510 In this case it starts with two windows: one displaying the GUD
2511 buffer and the other with the source file with the main routine
2512 of the inferior. Non-nil means display the layout shown for
2513 `gdba'."
2514 :type 'boolean
2515 :group 'gud
2516 :version "22.1")
2518 (defun gdb-many-windows (arg)
2519 "Toggle the number of windows in the basic arrangement.
2520 With arg, display additional buffers iff arg is positive."
2521 (interactive "P")
2522 (setq gdb-many-windows
2523 (if (null arg)
2524 (not gdb-many-windows)
2525 (> (prefix-numeric-value arg) 0)))
2526 (if (and gud-comint-buffer
2527 (buffer-name gud-comint-buffer))
2528 (condition-case nil
2529 (gdb-restore-windows)
2530 (error nil))))
2532 (defun gdb-restore-windows ()
2533 "Restore the basic arrangement of windows used by gdba.
2534 This arrangement depends on the value of `gdb-many-windows'."
2535 (interactive)
2536 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2537 (delete-other-windows)
2538 (if gdb-many-windows
2539 (gdb-setup-windows)
2540 (when (or gud-last-last-frame gdb-show-main)
2541 (split-window)
2542 (other-window 1)
2543 (switch-to-buffer
2544 (if gud-last-last-frame
2545 (gud-find-file (car gud-last-last-frame))
2546 (gud-find-file gdb-main-file)))
2547 (other-window 1))))
2549 (defun gdb-reset ()
2550 "Exit a debugging session cleanly.
2551 Kills the gdb buffers and resets the source buffers."
2552 (dolist (buffer (buffer-list))
2553 (unless (eq buffer gud-comint-buffer)
2554 (with-current-buffer buffer
2555 (if (memq gud-minor-mode '(gdbmi gdba))
2556 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2557 (kill-buffer nil)
2558 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2559 (setq gud-minor-mode nil)
2560 (kill-local-variable 'tool-bar-map)
2561 (kill-local-variable 'gdb-define-alist))))))
2562 (when (markerp gdb-overlay-arrow-position)
2563 (move-marker gdb-overlay-arrow-position nil)
2564 (setq gdb-overlay-arrow-position nil))
2565 (setq overlay-arrow-variable-list
2566 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2567 (setq gud-running nil)
2568 (setq gdb-active-process nil)
2569 (setq gdb-var-list nil)
2570 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2572 (defun gdb-source-info ()
2573 "Find the source file where the program starts and displays it with related
2574 buffers."
2575 (goto-char (point-min))
2576 (if (and (search-forward "Located in " nil t)
2577 (looking-at "\\S-+"))
2578 (setq gdb-main-file (match-string 0)))
2579 (goto-char (point-min))
2580 (if (search-forward "Includes preprocessor macro info." nil t)
2581 (setq gdb-macro-info t))
2582 (if gdb-many-windows
2583 (gdb-setup-windows)
2584 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2585 (if gdb-show-main
2586 (let ((pop-up-windows t))
2587 (display-buffer (gud-find-file gdb-main-file))))))
2589 (defun gdb-get-location (bptno line flag)
2590 "Find the directory containing the relevant source file.
2591 Put in buffer and place breakpoint icon."
2592 (goto-char (point-min))
2593 (catch 'file-not-found
2594 (if (search-forward "Located in " nil t)
2595 (when (looking-at "\\S-+")
2596 (delete (cons bptno "File not found") gdb-location-alist)
2597 (push (cons bptno (match-string 0)) gdb-location-alist))
2598 (gdb-resync)
2599 (unless (assoc bptno gdb-location-alist)
2600 (push (cons bptno "File not found") gdb-location-alist)
2601 (message-box "Cannot find source file for breakpoint location.\n\
2602 Add directory to search path for source files using the GDB command, dir."))
2603 (throw 'file-not-found nil))
2604 (with-current-buffer
2605 (find-file-noselect (match-string 0))
2606 (save-current-buffer
2607 (set (make-local-variable 'gud-minor-mode) 'gdba)
2608 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2609 ;; only want one breakpoint icon at each location
2610 (save-excursion
2611 (goto-line (string-to-number line))
2612 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2614 (add-hook 'find-file-hook 'gdb-find-file-hook)
2616 (defun gdb-find-file-hook ()
2617 "Set up buffer for debugging if file is part of the source code
2618 of the current session."
2619 (if (and (not gdb-find-file-unhook)
2620 ;; in case gud or gdb-ui is just loaded
2621 gud-comint-buffer
2622 (buffer-name gud-comint-buffer)
2623 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2624 'gdba))
2625 (condition-case nil
2626 (gdb-enqueue-input
2627 (list (concat gdb-server-prefix "list "
2628 (file-name-nondirectory buffer-file-name)
2629 ":1\n")
2630 `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
2631 (error (setq gdb-find-file-unhook t)))))
2633 ;;from put-image
2634 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2635 "Put string PUTSTRING in front of POS in the current buffer.
2636 PUTSTRING is displayed by putting an overlay into the current buffer with a
2637 `before-string' string that has a `display' property whose value is
2638 PUTSTRING."
2639 (let ((string (make-string 1 ?x))
2640 (buffer (current-buffer)))
2641 (setq putstring (copy-sequence putstring))
2642 (let ((overlay (make-overlay pos pos buffer))
2643 (prop (or dprop
2644 (list (list 'margin 'left-margin) putstring))))
2645 (put-text-property 0 1 'display prop string)
2646 (if sprops
2647 (add-text-properties 0 1 sprops string))
2648 (overlay-put overlay 'put-break t)
2649 (overlay-put overlay 'before-string string))))
2651 ;;from remove-images
2652 (defun gdb-remove-strings (start end &optional buffer)
2653 "Remove strings between START and END in BUFFER.
2654 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2655 BUFFER nil or omitted means use the current buffer."
2656 (unless buffer
2657 (setq buffer (current-buffer)))
2658 (dolist (overlay (overlays-in start end))
2659 (when (overlay-get overlay 'put-break)
2660 (delete-overlay overlay))))
2662 (defun gdb-put-breakpoint-icon (enabled bptno)
2663 (let ((start (- (line-beginning-position) 1))
2664 (end (+ (line-end-position) 1))
2665 (putstring (if enabled "B" "b"))
2666 (source-window (get-buffer-window (current-buffer) 0)))
2667 (add-text-properties
2668 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2669 putstring)
2670 (if enabled
2671 (add-text-properties
2672 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2673 (add-text-properties
2674 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2675 (gdb-remove-breakpoint-icons start end)
2676 (if (display-images-p)
2677 (if (>= (or left-fringe-width
2678 (if source-window (car (window-fringes source-window)))
2679 gdb-buffer-fringe-width) 8)
2680 (gdb-put-string
2681 nil (1+ start)
2682 `(left-fringe breakpoint
2683 ,(if enabled
2684 'breakpoint-enabled
2685 'breakpoint-disabled))
2686 'gdb-bptno bptno
2687 'gdb-enabled enabled)
2688 (when (< left-margin-width 2)
2689 (save-current-buffer
2690 (setq left-margin-width 2)
2691 (if source-window
2692 (set-window-margins
2693 source-window
2694 left-margin-width right-margin-width))))
2695 (put-image
2696 (if enabled
2697 (or breakpoint-enabled-icon
2698 (setq breakpoint-enabled-icon
2699 (find-image `((:type xpm :data
2700 ,breakpoint-xpm-data
2701 :ascent 100 :pointer hand)
2702 (:type pbm :data
2703 ,breakpoint-enabled-pbm-data
2704 :ascent 100 :pointer hand)))))
2705 (or breakpoint-disabled-icon
2706 (setq breakpoint-disabled-icon
2707 (find-image `((:type xpm :data
2708 ,breakpoint-xpm-data
2709 :conversion disabled
2710 :ascent 100 :pointer hand)
2711 (:type pbm :data
2712 ,breakpoint-disabled-pbm-data
2713 :ascent 100 :pointer hand))))))
2714 (+ start 1)
2715 putstring
2716 'left-margin))
2717 (when (< left-margin-width 2)
2718 (save-current-buffer
2719 (setq left-margin-width 2)
2720 (let ((window (get-buffer-window (current-buffer) 0)))
2721 (if window
2722 (set-window-margins
2723 window left-margin-width right-margin-width)))))
2724 (gdb-put-string
2725 (propertize putstring
2726 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2727 (1+ start)))))
2729 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2730 (gdb-remove-strings start end)
2731 (if (display-images-p)
2732 (remove-images start end))
2733 (when remove-margin
2734 (setq left-margin-width 0)
2735 (let ((window (get-buffer-window (current-buffer) 0)))
2736 (if window
2737 (set-window-margins
2738 window left-margin-width right-margin-width)))))
2742 ;; Assembler buffer.
2744 (gdb-set-buffer-rules 'gdb-assembler-buffer
2745 'gdb-assembler-buffer-name
2746 'gdb-assembler-mode)
2748 (def-gdb-auto-update-handler gdb-assembler-handler
2749 gdb-invalidate-assembler
2750 gdb-assembler-buffer
2751 gdb-assembler-custom)
2753 (defun gdb-assembler-custom ()
2754 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2755 (pos 1) (address) (flag) (bptno))
2756 (with-current-buffer buffer
2757 (save-excursion
2758 (if (not (equal gdb-frame-address "main"))
2759 (progn
2760 (goto-char (point-min))
2761 (if (and gdb-frame-address
2762 (search-forward gdb-frame-address nil t))
2763 (progn
2764 (setq pos (point))
2765 (beginning-of-line)
2766 (or gdb-overlay-arrow-position
2767 (setq gdb-overlay-arrow-position (make-marker)))
2768 (set-marker gdb-overlay-arrow-position
2769 (point) (current-buffer))))))
2770 ;; remove all breakpoint-icons in assembler buffer before updating.
2771 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2772 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2773 (goto-char (point-min))
2774 (while (< (point) (- (point-max) 1))
2775 (forward-line 1)
2776 (if (looking-at "[^\t].*?breakpoint")
2777 (progn
2778 (looking-at
2779 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2780 (setq bptno (match-string 1))
2781 (setq flag (char-after (match-beginning 2)))
2782 (setq address (match-string 3))
2783 (with-current-buffer buffer
2784 (save-excursion
2785 (goto-char (point-min))
2786 (if (search-forward address nil t)
2787 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2788 (if (not (equal gdb-frame-address "main"))
2789 (with-current-buffer buffer
2790 (set-window-point (get-buffer-window buffer 0) pos)))))
2792 (defvar gdb-assembler-mode-map
2793 (let ((map (make-sparse-keymap)))
2794 (suppress-keymap map)
2795 (define-key map "q" 'kill-this-buffer)
2796 map))
2798 (defvar gdb-assembler-font-lock-keywords
2799 '(;; <__function.name+n>
2800 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2801 (1 font-lock-function-name-face))
2802 ;; 0xNNNNNNNN <__function.name+n>: opcode
2803 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2804 (4 font-lock-keyword-face))
2805 ;; %register(at least i386)
2806 ("%\\sw+" . font-lock-variable-name-face)
2807 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2808 (1 font-lock-comment-face)
2809 (2 font-lock-function-name-face))
2810 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2811 "Font lock keywords used in `gdb-assembler-mode'.")
2813 (defun gdb-assembler-mode ()
2814 "Major mode for viewing code assembler.
2816 \\{gdb-assembler-mode-map}"
2817 (kill-all-local-variables)
2818 (setq major-mode 'gdb-assembler-mode)
2819 (setq mode-name (concat "Machine:" gdb-selected-frame))
2820 (setq gdb-overlay-arrow-position nil)
2821 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2822 (setq fringes-outside-margins t)
2823 (setq buffer-read-only t)
2824 (use-local-map gdb-assembler-mode-map)
2825 (gdb-invalidate-assembler)
2826 (set (make-local-variable 'font-lock-defaults)
2827 '(gdb-assembler-font-lock-keywords))
2828 (run-mode-hooks 'gdb-assembler-mode-hook)
2829 'gdb-invalidate-assembler)
2831 (defun gdb-assembler-buffer-name ()
2832 (with-current-buffer gud-comint-buffer
2833 (concat "*disassembly of " (gdb-get-target-string) "*")))
2835 (defun gdb-display-assembler-buffer ()
2836 "Display disassembly view."
2837 (interactive)
2838 (setq gdb-previous-frame nil)
2839 (gdb-display-buffer
2840 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2842 (defun gdb-frame-assembler-buffer ()
2843 "Display disassembly view in a new frame."
2844 (interactive)
2845 (setq gdb-previous-frame nil)
2846 (let ((special-display-regexps (append special-display-regexps '(".*")))
2847 (special-display-frame-alist gdb-frame-parameters))
2848 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2850 ;; modified because if gdb-frame-address has changed value a new command
2851 ;; must be enqueued to update the buffer with the new output
2852 (defun gdb-invalidate-assembler (&optional ignored)
2853 (if (gdb-get-buffer 'gdb-assembler-buffer)
2854 (progn
2855 (unless (and gdb-selected-frame
2856 (string-equal gdb-selected-frame gdb-previous-frame))
2857 (if (or (not (member 'gdb-invalidate-assembler
2858 gdb-pending-triggers))
2859 (not (string-equal gdb-frame-address
2860 gdb-previous-frame-address)))
2861 (progn
2862 ;; take previous disassemble command, if any, off the queue
2863 (with-current-buffer gud-comint-buffer
2864 (let ((queue gdb-input-queue))
2865 (dolist (item queue)
2866 (if (equal (cdr item) '(gdb-assembler-handler))
2867 (setq gdb-input-queue
2868 (delete item gdb-input-queue))))))
2869 (gdb-enqueue-input
2870 (list
2871 (concat gdb-server-prefix "disassemble "
2872 (if (member gdb-frame-address '(nil "main")) nil "0x")
2873 gdb-frame-address "\n")
2874 'gdb-assembler-handler))
2875 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2876 (setq gdb-previous-frame-address gdb-frame-address)
2877 (setq gdb-previous-frame gdb-selected-frame)))))))
2879 (defun gdb-get-selected-frame ()
2880 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2881 (progn
2882 (gdb-enqueue-input
2883 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2884 (push 'gdb-get-selected-frame
2885 gdb-pending-triggers))))
2887 (defun gdb-frame-handler ()
2888 (setq gdb-pending-triggers
2889 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2890 (goto-char (point-min))
2891 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2892 (setq gdb-frame-number (match-string 1)))
2893 (goto-char (point-min))
2894 (if (re-search-forward
2895 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2896 (progn
2897 (setq gdb-selected-frame (match-string 2))
2898 (if (gdb-get-buffer 'gdb-locals-buffer)
2899 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2900 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2901 (if (gdb-get-buffer 'gdb-assembler-buffer)
2902 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2903 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2904 (setq gdb-frame-address (match-string 1))))
2905 (goto-char (point-min))
2906 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2907 (setq gdb-current-language (match-string 1)))
2908 (gdb-invalidate-assembler))
2910 (provide 'gdb-ui)
2912 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2913 ;;; gdb-ui.el ends here