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