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