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