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