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