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