]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gdb-fringe-width): New variable.
[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-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 | Input/Output (of inferior) buffer
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 ((p (window-point (get-buffer-window buf 0)))
1196 (buffer-read-only nil))
1197 (erase-buffer)
1198 (insert-buffer-substring (gdb-get-create-buffer
1199 'gdb-partial-output-buffer))
1200 (set-window-point (get-buffer-window buf 0) p)))))
1201 ;; put customisation here
1202 (,custom-defun)))
1203
1204 (defmacro def-gdb-auto-updated-buffer (buffer-key
1205 trigger-name gdb-command
1206 output-handler-name custom-defun)
1207 `(progn
1208 (def-gdb-auto-update-trigger ,trigger-name
1209 ;; The demand predicate:
1210 (lambda () (gdb-get-buffer ',buffer-key))
1211 ,gdb-command
1212 ,output-handler-name)
1213 (def-gdb-auto-update-handler ,output-handler-name
1214 ,trigger-name ,buffer-key ,custom-defun)))
1215
1216 \f
1217 ;;
1218 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1219 ;;
1220 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1221 'gdb-breakpoints-buffer-name
1222 'gdb-breakpoints-mode)
1223
1224 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1225 ;; This defines the auto update rule for buffers of type
1226 ;; `gdb-breakpoints-buffer'.
1227 ;;
1228 ;; It defines a function to serve as the annotation handler that
1229 ;; handles the `foo-invalidated' message. That function is called:
1230 gdb-invalidate-breakpoints
1231 ;;
1232 ;; To update the buffer, this command is sent to gdb.
1233 "server info breakpoints\n"
1234 ;;
1235 ;; This also defines a function to be the handler for the output
1236 ;; from the command above. That function will copy the output into
1237 ;; the appropriately typed buffer. That function will be called:
1238 gdb-info-breakpoints-handler
1239 ;; buffer specific functions
1240 gdb-info-breakpoints-custom)
1241
1242 (defconst breakpoint-xpm-data
1243 "/* XPM */
1244 static char *magick[] = {
1245 /* columns rows colors chars-per-pixel */
1246 \"10 10 2 1\",
1247 \" c red\",
1248 \"+ c None\",
1249 /* pixels */
1250 \"+++ +++\",
1251 \"++ ++\",
1252 \"+ +\",
1253 \" \",
1254 \" \",
1255 \" \",
1256 \" \",
1257 \"+ +\",
1258 \"++ ++\",
1259 \"+++ +++\",
1260 };"
1261 "XPM data used for breakpoint icon.")
1262
1263 (defconst breakpoint-enabled-pbm-data
1264 "P1
1265 10 10\",
1266 0 0 0 0 1 1 1 1 0 0 0 0
1267 0 0 0 1 1 1 1 1 1 0 0 0
1268 0 0 1 1 1 1 1 1 1 1 0 0
1269 0 1 1 1 1 1 1 1 1 1 1 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 0 1 1 1 1 1 1 1 1 0 0
1274 0 0 0 1 1 1 1 1 1 0 0 0
1275 0 0 0 0 1 1 1 1 0 0 0 0"
1276 "PBM data used for enabled breakpoint icon.")
1277
1278 (defconst breakpoint-disabled-pbm-data
1279 "P1
1280 10 10\",
1281 0 0 1 0 1 0 1 0 0 0
1282 0 1 0 1 0 1 0 1 0 0
1283 1 0 1 0 1 0 1 0 1 0
1284 0 1 0 1 0 1 0 1 0 1
1285 1 0 1 0 1 0 1 0 1 0
1286 0 1 0 1 0 1 0 1 0 1
1287 1 0 1 0 1 0 1 0 1 0
1288 0 1 0 1 0 1 0 1 0 1
1289 0 0 1 0 1 0 1 0 1 0
1290 0 0 0 1 0 1 0 1 0 0"
1291 "PBM data used for disabled breakpoint icon.")
1292
1293 (defvar breakpoint-enabled-icon nil
1294 "Icon for enabled breakpoint in display margin.")
1295
1296 (defvar breakpoint-disabled-icon nil
1297 "Icon for disabled breakpoint in display margin.")
1298
1299 ;; Bitmap for breakpoint in fringe
1300 (and (display-images-p)
1301 (define-fringe-bitmap 'breakpoint
1302 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c"))
1303
1304 (defface breakpoint-enabled
1305 '((t
1306 :foreground "red"
1307 :weight bold))
1308 "Face for enabled breakpoint icon in fringe."
1309 :group 'gud)
1310 ;; Compatibility alias for old name.
1311 (put 'breakpoint-enabled-bitmap-face 'face-alias 'breakpoint-enabled)
1312
1313 (defface breakpoint-disabled
1314 ;; We use different values of grey for different background types,
1315 ;; so that on low-color displays it will end up as something visible
1316 ;; if it has to be approximated.
1317 '((((background dark)) :foreground "grey60")
1318 (((background light)) :foreground "grey40"))
1319 "Face for disabled breakpoint icon in fringe."
1320 :group 'gud)
1321
1322 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1323 (defun gdb-info-breakpoints-custom ()
1324 (let ((flag) (bptno))
1325 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1326 (dolist (buffer (buffer-list))
1327 (with-current-buffer buffer
1328 (if (and (eq gud-minor-mode 'gdba)
1329 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1330 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1331 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1332 (save-excursion
1333 (goto-char (point-min))
1334 (while (< (point) (- (point-max) 1))
1335 (forward-line 1)
1336 (if (looking-at "[^\t].*?breakpoint")
1337 (progn
1338 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1339 (setq bptno (match-string 1))
1340 (setq flag (char-after (match-beginning 2)))
1341 (beginning-of-line)
1342 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1343 (progn
1344 (let ((buffer-read-only nil))
1345 (add-text-properties (match-beginning 1) (match-end 1)
1346 '(face font-lock-function-name-face)))
1347 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1348 (let ((line (match-string 2)) (buffer-read-only nil)
1349 (file (match-string 1)))
1350 (add-text-properties (line-beginning-position)
1351 (line-end-position)
1352 '(mouse-face highlight
1353 help-echo "mouse-2, RET: visit breakpoint"))
1354 (unless (file-exists-p file)
1355 (setq file (cdr (assoc bptno gdb-location-alist))))
1356 (if (and file
1357 (not (string-equal file "File not found")))
1358 (with-current-buffer
1359 (find-file-noselect file 'nowarn)
1360 (set (make-local-variable 'gud-minor-mode)
1361 'gdba)
1362 (set (make-local-variable 'tool-bar-map)
1363 gud-tool-bar-map)
1364 ;; Only want one breakpoint icon at each
1365 ;; location.
1366 (save-excursion
1367 (goto-line (string-to-number line))
1368 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1369 (gdb-enqueue-input
1370 (list
1371 (concat "list "
1372 (match-string-no-properties 1) ":1\n")
1373 'ignore))
1374 (gdb-enqueue-input
1375 (list "info source\n"
1376 `(lambda () (gdb-get-location
1377 ,bptno ,line ,flag))))))))))
1378 (end-of-line)))))
1379 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1380
1381 (defun gdb-mouse-set-clear-breakpoint (event)
1382 "Set/clear breakpoint in left fringe/margin with mouse click."
1383 (interactive "e")
1384 (mouse-minibuffer-check event)
1385 (let ((posn (event-end event)))
1386 (if (numberp (posn-point posn))
1387 (with-selected-window (posn-window posn)
1388 (save-excursion
1389 (goto-char (posn-point posn))
1390 (if (or (posn-object posn)
1391 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1392 'breakpoint))
1393 (gud-remove nil)
1394 (gud-break nil)))))))
1395
1396 (defun gdb-mouse-toggle-breakpoint (event)
1397 "Enable/disable breakpoint in left fringe/margin with mouse click."
1398 (interactive "e")
1399 (mouse-minibuffer-check event)
1400 (let ((posn (event-end event)))
1401 (if (numberp (posn-point posn))
1402 (with-selected-window (posn-window posn)
1403 (save-excursion
1404 (goto-char (posn-point posn))
1405 (if (posn-object posn)
1406 (gdb-enqueue-input
1407 (list
1408 (let ((bptno (get-text-property
1409 0 'gdb-bptno (car (posn-string posn)))))
1410 (concat
1411 (if (get-text-property
1412 0 'gdb-enabled (car (posn-string posn)))
1413 "disable "
1414 "enable ")
1415 bptno "\n")) 'ignore))))))))
1416
1417 (defun gdb-breakpoints-buffer-name ()
1418 (with-current-buffer gud-comint-buffer
1419 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1420
1421 (defun gdb-display-breakpoints-buffer ()
1422 "Display status of user-settable breakpoints."
1423 (interactive)
1424 (gdb-display-buffer
1425 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1426
1427 (defun gdb-frame-breakpoints-buffer ()
1428 "Display status of user-settable breakpoints in a new frame."
1429 (interactive)
1430 (let ((special-display-regexps (append special-display-regexps '(".*")))
1431 (special-display-frame-alist gdb-frame-parameters))
1432 (display-buffer (gdb-get-create-buffer 'gdb-breakpoints-buffer))))
1433
1434 (defvar gdb-breakpoints-mode-map
1435 (let ((map (make-sparse-keymap))
1436 (menu (make-sparse-keymap "Breakpoints")))
1437 (define-key menu [quit] '("Quit" . kill-this-buffer))
1438 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1439 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1440 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1441 (suppress-keymap map)
1442 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1443 (define-key map " " 'gdb-toggle-breakpoint)
1444 (define-key map "d" 'gdb-delete-breakpoint)
1445 (define-key map "q" 'kill-this-buffer)
1446 (define-key map "\r" 'gdb-goto-breakpoint)
1447 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1448 (define-key map [follow-link] 'mouse-face)
1449 map))
1450
1451 (defun gdb-breakpoints-mode ()
1452 "Major mode for gdb breakpoints.
1453
1454 \\{gdb-breakpoints-mode-map}"
1455 (kill-all-local-variables)
1456 (setq major-mode 'gdb-breakpoints-mode)
1457 (setq mode-name "Breakpoints")
1458 (use-local-map gdb-breakpoints-mode-map)
1459 (setq buffer-read-only t)
1460 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1461 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1462 'gdb-invalidate-breakpoints
1463 'gdbmi-invalidate-breakpoints))
1464
1465 (defun gdb-toggle-breakpoint ()
1466 "Enable/disable breakpoint at current line."
1467 (interactive)
1468 (save-excursion
1469 (beginning-of-line 1)
1470 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1471 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1472 (looking-at
1473 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1474 (gdb-enqueue-input
1475 (list
1476 (concat gdb-server-prefix
1477 (if (eq ?y (char-after (match-beginning 2)))
1478 "disable "
1479 "enable ")
1480 (match-string 1) "\n") 'ignore))
1481 (error "Not recognized as break/watchpoint line"))))
1482
1483 (defun gdb-delete-breakpoint ()
1484 "Delete the breakpoint at current line."
1485 (interactive)
1486 (beginning-of-line 1)
1487 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1488 (looking-at "\\([0-9]+\\).*?point\\s-+\\S-+\\s-+\\(.\\)")
1489 (looking-at
1490 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\s-+\\S-+\\s-+\\S-+:[0-9]+"))
1491 (gdb-enqueue-input
1492 (list
1493 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1494 (error "Not recognized as break/watchpoint line")))
1495
1496 (defun gdb-goto-breakpoint (&optional event)
1497 "Display the breakpoint location specified at current line."
1498 (interactive (list last-input-event))
1499 (if event (mouse-set-point event))
1500 (save-excursion
1501 (beginning-of-line 1)
1502 (if (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1503 (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1504 (looking-at
1505 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+.\\s-+\\S-+\\s-+\
1506 \\(\\S-+\\):\\([0-9]+\\)"))
1507 (let ((bptno (match-string 1))
1508 (file (match-string 2))
1509 (line (match-string 3)))
1510 (save-selected-window
1511 (let* ((buf (find-file-noselect
1512 (if (file-exists-p file) file
1513 (cdr (assoc bptno gdb-location-alist)))))
1514 (window (display-buffer buf)))
1515 (with-current-buffer buf
1516 (goto-line (string-to-number line))
1517 (set-window-point window (point))))))
1518 (error "Not recognized as break/watchpoint line"))))
1519 \f
1520
1521 ;; Frames buffer. This displays a perpetually correct bactracktrace
1522 ;; (from the command `where').
1523 ;;
1524 ;; Alas, if your stack is deep, it is costly.
1525 ;;
1526 (gdb-set-buffer-rules 'gdb-stack-buffer
1527 'gdb-stack-buffer-name
1528 'gdb-frames-mode)
1529
1530 (def-gdb-auto-updated-buffer gdb-stack-buffer
1531 gdb-invalidate-frames
1532 "server where\n"
1533 gdb-info-frames-handler
1534 gdb-info-frames-custom)
1535
1536 (defun gdb-info-frames-custom ()
1537 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1538 (save-excursion
1539 (let ((buffer-read-only nil)
1540 bl el)
1541 (goto-char (point-min))
1542 (while (< (point) (point-max))
1543 (setq bl (line-beginning-position)
1544 el (line-end-position))
1545 (add-text-properties bl el
1546 '(mouse-face highlight
1547 help-echo "mouse-2, RET: Select frame"))
1548 (goto-char bl)
1549 (when (looking-at "^#\\([0-9]+\\)")
1550 (if (equal (match-string 1) gdb-frame-number)
1551 (put-text-property bl el 'face '(:inverse-video t))
1552 (when (re-search-forward " in \\([^ ]+\\) (" el t)
1553 (put-text-property (match-beginning 1) (match-end 1)
1554 'face font-lock-function-name-face)
1555 (setq bl (match-end 0))
1556 (while (re-search-forward "<\\([^>]+\\)>" el t)
1557 (put-text-property (match-beginning 1) (match-end 1)
1558 'face font-lock-function-name-face))
1559 (goto-char bl)
1560 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
1561 (put-text-property (match-beginning 1) (match-end 1)
1562 'face font-lock-variable-name-face))
1563 )))
1564 (forward-line 1))))))
1565
1566 (defun gdb-stack-buffer-name ()
1567 (with-current-buffer gud-comint-buffer
1568 (concat "*stack frames of " (gdb-get-target-string) "*")))
1569
1570 (defun gdb-display-stack-buffer ()
1571 "Display backtrace of current stack."
1572 (interactive)
1573 (gdb-display-buffer
1574 (gdb-get-create-buffer 'gdb-stack-buffer)))
1575
1576 (defun gdb-frame-stack-buffer ()
1577 "Display backtrace of current stack in a new frame."
1578 (interactive)
1579 (let ((special-display-regexps (append special-display-regexps '(".*")))
1580 (special-display-frame-alist gdb-frame-parameters))
1581 (display-buffer (gdb-get-create-buffer 'gdb-stack-buffer))))
1582
1583 (defvar gdb-frames-mode-map
1584 (let ((map (make-sparse-keymap)))
1585 (suppress-keymap map)
1586 (define-key map "q" 'kill-this-buffer)
1587 (define-key map "\r" 'gdb-frames-select)
1588 (define-key map [mouse-2] 'gdb-frames-select)
1589 (define-key map [follow-link] 'mouse-face)
1590 map))
1591
1592 (defun gdb-frames-mode ()
1593 "Major mode for gdb frames.
1594
1595 \\{gdb-frames-mode-map}"
1596 (kill-all-local-variables)
1597 (setq major-mode 'gdb-frames-mode)
1598 (setq mode-name "Frames")
1599 (setq buffer-read-only t)
1600 (use-local-map gdb-frames-mode-map)
1601 (font-lock-mode -1)
1602 (run-mode-hooks 'gdb-frames-mode-hook)
1603 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1604 'gdb-invalidate-frames
1605 'gdbmi-invalidate-frames))
1606
1607 (defun gdb-get-frame-number ()
1608 (save-excursion
1609 (let* ((pos (re-search-backward "^#*\\([0-9]*\\)" nil t))
1610 (n (or (and pos (match-string-no-properties 1)) "0")))
1611 n)))
1612
1613 (defun gdb-frames-select (&optional event)
1614 "Select the frame and display the relevant source."
1615 (interactive (list last-input-event))
1616 (if event (mouse-set-point event))
1617 (gdb-enqueue-input
1618 (list (concat gdb-server-prefix "frame "
1619 (gdb-get-frame-number) "\n") 'ignore))
1620 (gud-display-frame))
1621 \f
1622
1623 ;; Threads buffer. This displays a selectable thread list.
1624 ;;
1625 (gdb-set-buffer-rules 'gdb-threads-buffer
1626 'gdb-threads-buffer-name
1627 'gdb-threads-mode)
1628
1629 (def-gdb-auto-updated-buffer gdb-threads-buffer
1630 gdb-invalidate-threads
1631 (concat gdb-server-prefix "info threads\n")
1632 gdb-info-threads-handler
1633 gdb-info-threads-custom)
1634
1635 (defun gdb-info-threads-custom ()
1636 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1637 (let ((buffer-read-only nil))
1638 (goto-char (point-min))
1639 (while (< (point) (point-max))
1640 (add-text-properties (line-beginning-position) (line-end-position)
1641 '(mouse-face highlight
1642 help-echo "mouse-2, RET: select thread"))
1643 (forward-line 1)))))
1644
1645 (defun gdb-threads-buffer-name ()
1646 (with-current-buffer gud-comint-buffer
1647 (concat "*threads of " (gdb-get-target-string) "*")))
1648
1649 (defun gdb-display-threads-buffer ()
1650 "Display IDs of currently known threads."
1651 (interactive)
1652 (gdb-display-buffer
1653 (gdb-get-create-buffer 'gdb-threads-buffer)))
1654
1655 (defun gdb-frame-threads-buffer ()
1656 "Display IDs of currently known threads in a new frame."
1657 (interactive)
1658 (let ((special-display-regexps (append special-display-regexps '(".*")))
1659 (special-display-frame-alist gdb-frame-parameters))
1660 (display-buffer (gdb-get-create-buffer 'gdb-threads-buffer))))
1661
1662 (defvar gdb-threads-mode-map
1663 (let ((map (make-sparse-keymap)))
1664 (suppress-keymap map)
1665 (define-key map "q" 'kill-this-buffer)
1666 (define-key map "\r" 'gdb-threads-select)
1667 (define-key map [mouse-2] 'gdb-threads-select)
1668 map))
1669
1670 (defvar gdb-threads-font-lock-keywords
1671 '(
1672 (") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
1673 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
1674 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face))
1675 )
1676 "Font lock keywords used in `gdb-threads-mode'.")
1677
1678 (defun gdb-threads-mode ()
1679 "Major mode for gdb frames.
1680
1681 \\{gdb-threads-mode-map}"
1682 (kill-all-local-variables)
1683 (setq major-mode 'gdb-threads-mode)
1684 (setq mode-name "Threads")
1685 (setq buffer-read-only t)
1686 (use-local-map gdb-threads-mode-map)
1687 (set (make-local-variable 'font-lock-defaults)
1688 '(gdb-threads-font-lock-keywords))
1689 (run-mode-hooks 'gdb-threads-mode-hook)
1690 'gdb-invalidate-threads)
1691
1692 (defun gdb-get-thread-number ()
1693 (save-excursion
1694 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1695 (match-string-no-properties 1)))
1696
1697 (defun gdb-threads-select (&optional event)
1698 "Select the thread and display the relevant source."
1699 (interactive (list last-input-event))
1700 (if event (mouse-set-point event))
1701 (gdb-enqueue-input
1702 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1703 (gud-display-frame))
1704 \f
1705
1706 ;; Registers buffer.
1707 ;;
1708 (defcustom gdb-all-registers nil
1709 "Non-nil means include floating-point registers."
1710 :type 'boolean
1711 :group 'gud
1712 :version "22.1")
1713
1714 (gdb-set-buffer-rules 'gdb-registers-buffer
1715 'gdb-registers-buffer-name
1716 'gdb-registers-mode)
1717
1718 (def-gdb-auto-updated-buffer gdb-registers-buffer
1719 gdb-invalidate-registers
1720 (concat
1721 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
1722 gdb-info-registers-handler
1723 gdb-info-registers-custom)
1724
1725 (defun gdb-info-registers-custom ())
1726
1727 (defvar gdb-registers-mode-map
1728 (let ((map (make-sparse-keymap)))
1729 (suppress-keymap map)
1730 (define-key map " " 'toggle-gdb-all-registers)
1731 (define-key map "q" 'kill-this-buffer)
1732 map))
1733
1734 (defvar gdb-registers-font-lock-keywords
1735 '(
1736 ("^[^ ]+" . font-lock-variable-name-face)
1737 )
1738 "Font lock keywords used in `gdb-registers-mode'.")
1739
1740 (defun gdb-registers-mode ()
1741 "Major mode for gdb registers.
1742
1743 \\{gdb-registers-mode-map}"
1744 (kill-all-local-variables)
1745 (setq major-mode 'gdb-registers-mode)
1746 (setq mode-name "Registers:")
1747 (setq buffer-read-only t)
1748 (use-local-map gdb-registers-mode-map)
1749 (set (make-local-variable 'font-lock-defaults)
1750 '(gdb-registers-font-lock-keywords))
1751 (run-mode-hooks 'gdb-registers-mode-hook)
1752 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
1753 'gdb-invalidate-registers
1754 'gdbmi-invalidate-registers))
1755
1756 (defun gdb-registers-buffer-name ()
1757 (with-current-buffer gud-comint-buffer
1758 (concat "*registers of " (gdb-get-target-string) "*")))
1759
1760 (defun gdb-display-registers-buffer ()
1761 "Display integer register contents."
1762 (interactive)
1763 (gdb-display-buffer
1764 (gdb-get-create-buffer 'gdb-registers-buffer)))
1765
1766 (defun gdb-frame-registers-buffer ()
1767 "Display integer register contents in a new frame."
1768 (interactive)
1769 (let ((special-display-regexps (append special-display-regexps '(".*")))
1770 (special-display-frame-alist gdb-frame-parameters))
1771 (display-buffer (gdb-get-create-buffer 'gdb-registers-buffer))))
1772
1773 (defun toggle-gdb-all-registers ()
1774 "Toggle the display of floating-point registers."
1775 (interactive)
1776 (if gdb-all-registers
1777 (progn
1778 (setq gdb-all-registers nil)
1779 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1780 (setq mode-name "Registers:")))
1781 (setq gdb-all-registers t)
1782 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
1783 (setq mode-name "Registers:All")))
1784 (gdb-invalidate-registers))
1785 \f
1786
1787 ;; Memory buffer.
1788 ;;
1789 (defcustom gdb-memory-repeat-count 32
1790 "Number of data items in memory window."
1791 :type 'integer
1792 :group 'gud
1793 :version "22.1")
1794
1795 (defcustom gdb-memory-format "x"
1796 "Display format of data items in memory window."
1797 :type '(choice (const :tag "Hexadecimal" "x")
1798 (const :tag "Signed decimal" "d")
1799 (const :tag "Unsigned decimal" "u")
1800 (const :tag "Octal" "o")
1801 (const :tag "Binary" "t"))
1802 :group 'gud
1803 :version "22.1")
1804
1805 (defcustom gdb-memory-unit "w"
1806 "Unit size of data items in memory window."
1807 :type '(choice (const :tag "Byte" "b")
1808 (const :tag "Halfword" "h")
1809 (const :tag "Word" "w")
1810 (const :tag "Giant word" "g"))
1811 :group 'gud
1812 :version "22.1")
1813
1814 (gdb-set-buffer-rules 'gdb-memory-buffer
1815 'gdb-memory-buffer-name
1816 'gdb-memory-mode)
1817
1818 (def-gdb-auto-updated-buffer gdb-memory-buffer
1819 gdb-invalidate-memory
1820 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
1821 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
1822 gdb-read-memory-handler
1823 gdb-read-memory-custom)
1824
1825 (defun gdb-read-memory-custom ()
1826 (save-excursion
1827 (goto-char (point-min))
1828 (if (looking-at "0x[[:xdigit:]]+")
1829 (setq gdb-memory-address (match-string 0)))))
1830
1831 (defvar gdb-memory-mode-map
1832 (let ((map (make-sparse-keymap)))
1833 (suppress-keymap map)
1834 (define-key map "q" 'kill-this-buffer)
1835 map))
1836
1837 (defun gdb-memory-set-address (event)
1838 "Set the start memory address."
1839 (interactive "e")
1840 (save-selected-window
1841 (select-window (posn-window (event-start event)))
1842 (let ((arg (read-from-minibuffer "Memory address: ")))
1843 (setq gdb-memory-address arg))
1844 (gdb-invalidate-memory)))
1845
1846 (defun gdb-memory-set-repeat-count (event)
1847 "Set the number of data items in memory window."
1848 (interactive "e")
1849 (save-selected-window
1850 (select-window (posn-window (event-start event)))
1851 (let* ((arg (read-from-minibuffer "Repeat count: "))
1852 (count (string-to-number arg)))
1853 (if (<= count 0)
1854 (error "Positive numbers only")
1855 (customize-set-variable 'gdb-memory-repeat-count count)
1856 (gdb-invalidate-memory)))))
1857
1858 (defun gdb-memory-format-binary ()
1859 "Set the display format to binary."
1860 (interactive)
1861 (customize-set-variable 'gdb-memory-format "t")
1862 (gdb-invalidate-memory))
1863
1864 (defun gdb-memory-format-octal ()
1865 "Set the display format to octal."
1866 (interactive)
1867 (customize-set-variable 'gdb-memory-format "o")
1868 (gdb-invalidate-memory))
1869
1870 (defun gdb-memory-format-unsigned ()
1871 "Set the display format to unsigned decimal."
1872 (interactive)
1873 (customize-set-variable 'gdb-memory-format "u")
1874 (gdb-invalidate-memory))
1875
1876 (defun gdb-memory-format-signed ()
1877 "Set the display format to decimal."
1878 (interactive)
1879 (customize-set-variable 'gdb-memory-format "d")
1880 (gdb-invalidate-memory))
1881
1882 (defun gdb-memory-format-hexadecimal ()
1883 "Set the display format to hexadecimal."
1884 (interactive)
1885 (customize-set-variable 'gdb-memory-format "x")
1886 (gdb-invalidate-memory))
1887
1888 (defvar gdb-memory-format-keymap
1889 (let ((map (make-sparse-keymap)))
1890 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
1891 map)
1892 "Keymap to select format in the header line.")
1893
1894 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
1895 "Menu of display formats in the header line.")
1896
1897 (define-key gdb-memory-format-menu [binary]
1898 '(menu-item "Binary" gdb-memory-format-binary
1899 :button (:radio . (equal gdb-memory-format "t"))))
1900 (define-key gdb-memory-format-menu [octal]
1901 '(menu-item "Octal" gdb-memory-format-octal
1902 :button (:radio . (equal gdb-memory-format "o"))))
1903 (define-key gdb-memory-format-menu [unsigned]
1904 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
1905 :button (:radio . (equal gdb-memory-format "u"))))
1906 (define-key gdb-memory-format-menu [signed]
1907 '(menu-item "Signed Decimal" gdb-memory-format-signed
1908 :button (:radio . (equal gdb-memory-format "d"))))
1909 (define-key gdb-memory-format-menu [hexadecimal]
1910 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
1911 :button (:radio . (equal gdb-memory-format "x"))))
1912
1913 (defun gdb-memory-format-menu (event)
1914 (interactive "@e")
1915 (x-popup-menu event gdb-memory-format-menu))
1916
1917 (defun gdb-memory-format-menu-1 (event)
1918 (interactive "e")
1919 (save-selected-window
1920 (select-window (posn-window (event-start event)))
1921 (let* ((selection (gdb-memory-format-menu event))
1922 (binding (and selection (lookup-key gdb-memory-format-menu
1923 (vector (car selection))))))
1924 (if binding (call-interactively binding)))))
1925
1926 (defun gdb-memory-unit-giant ()
1927 "Set the unit size to giant words (eight bytes)."
1928 (interactive)
1929 (customize-set-variable 'gdb-memory-unit "g")
1930 (gdb-invalidate-memory))
1931
1932 (defun gdb-memory-unit-word ()
1933 "Set the unit size to words (four bytes)."
1934 (interactive)
1935 (customize-set-variable 'gdb-memory-unit "w")
1936 (gdb-invalidate-memory))
1937
1938 (defun gdb-memory-unit-halfword ()
1939 "Set the unit size to halfwords (two bytes)."
1940 (interactive)
1941 (customize-set-variable 'gdb-memory-unit "h")
1942 (gdb-invalidate-memory))
1943
1944 (defun gdb-memory-unit-byte ()
1945 "Set the unit size to bytes."
1946 (interactive)
1947 (customize-set-variable 'gdb-memory-unit "b")
1948 (gdb-invalidate-memory))
1949
1950 (defvar gdb-memory-unit-keymap
1951 (let ((map (make-sparse-keymap)))
1952 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
1953 map)
1954 "Keymap to select units in the header line.")
1955
1956 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
1957 "Menu of units in the header line.")
1958
1959 (define-key gdb-memory-unit-menu [giantwords]
1960 '(menu-item "Giant words" gdb-memory-unit-giant
1961 :button (:radio . (equal gdb-memory-unit "g"))))
1962 (define-key gdb-memory-unit-menu [words]
1963 '(menu-item "Words" gdb-memory-unit-word
1964 :button (:radio . (equal gdb-memory-unit "w"))))
1965 (define-key gdb-memory-unit-menu [halfwords]
1966 '(menu-item "Halfwords" gdb-memory-unit-halfword
1967 :button (:radio . (equal gdb-memory-unit "h"))))
1968 (define-key gdb-memory-unit-menu [bytes]
1969 '(menu-item "Bytes" gdb-memory-unit-byte
1970 :button (:radio . (equal gdb-memory-unit "b"))))
1971
1972 (defun gdb-memory-unit-menu (event)
1973 (interactive "@e")
1974 (x-popup-menu event gdb-memory-unit-menu))
1975
1976 (defun gdb-memory-unit-menu-1 (event)
1977 (interactive "e")
1978 (save-selected-window
1979 (select-window (posn-window (event-start event)))
1980 (let* ((selection (gdb-memory-unit-menu event))
1981 (binding (and selection (lookup-key gdb-memory-unit-menu
1982 (vector (car selection))))))
1983 (if binding (call-interactively binding)))))
1984
1985 ;;from make-mode-line-mouse-map
1986 (defun gdb-make-header-line-mouse-map (mouse function) "\
1987 Return a keymap with single entry for mouse key MOUSE on the header line.
1988 MOUSE is defined to run function FUNCTION with no args in the buffer
1989 corresponding to the mode line clicked."
1990 (let ((map (make-sparse-keymap)))
1991 (define-key map (vector 'header-line mouse) function)
1992 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
1993 map))
1994
1995 (defvar gdb-memory-font-lock-keywords
1996 '(;; <__function.name+n>
1997 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
1998 )
1999 "Font lock keywords used in `gdb-memory-mode'.")
2000
2001 (defun gdb-memory-mode ()
2002 "Major mode for examining memory.
2003
2004 \\{gdb-memory-mode-map}"
2005 (kill-all-local-variables)
2006 (setq major-mode 'gdb-memory-mode)
2007 (setq mode-name "Memory")
2008 (setq buffer-read-only t)
2009 (use-local-map gdb-memory-mode-map)
2010 (setq header-line-format
2011 '(:eval
2012 (concat
2013 "Read address["
2014 (propertize
2015 "-"
2016 'face font-lock-warning-face
2017 'help-echo "mouse-1: Decrement address"
2018 'mouse-face 'mode-line-highlight
2019 'local-map
2020 (gdb-make-header-line-mouse-map
2021 'mouse-1
2022 #'(lambda () (interactive)
2023 (let ((gdb-memory-address
2024 ;; Let GDB do the arithmetic.
2025 (concat
2026 gdb-memory-address " - "
2027 (number-to-string
2028 (* gdb-memory-repeat-count
2029 (cond ((string= gdb-memory-unit "b") 1)
2030 ((string= gdb-memory-unit "h") 2)
2031 ((string= gdb-memory-unit "w") 4)
2032 ((string= gdb-memory-unit "g") 8)))))))
2033 (gdb-invalidate-memory)))))
2034 "|"
2035 (propertize "+"
2036 'face font-lock-warning-face
2037 'help-echo "mouse-1: Increment address"
2038 'mouse-face 'mode-line-highlight
2039 'local-map (gdb-make-header-line-mouse-map
2040 'mouse-1
2041 #'(lambda () (interactive)
2042 (let ((gdb-memory-address nil))
2043 (gdb-invalidate-memory)))))
2044 "]: "
2045 (propertize gdb-memory-address
2046 'face font-lock-warning-face
2047 'help-echo "mouse-1: Set memory address"
2048 'mouse-face 'mode-line-highlight
2049 'local-map (gdb-make-header-line-mouse-map
2050 'mouse-1
2051 #'gdb-memory-set-address))
2052 " Repeat Count: "
2053 (propertize (number-to-string gdb-memory-repeat-count)
2054 'face font-lock-warning-face
2055 'help-echo "mouse-1: Set repeat count"
2056 'mouse-face 'mode-line-highlight
2057 'local-map (gdb-make-header-line-mouse-map
2058 'mouse-1
2059 #'gdb-memory-set-repeat-count))
2060 " Display Format: "
2061 (propertize gdb-memory-format
2062 'face font-lock-warning-face
2063 'help-echo "mouse-3: Select display format"
2064 'mouse-face 'mode-line-highlight
2065 'local-map gdb-memory-format-keymap)
2066 " Unit Size: "
2067 (propertize gdb-memory-unit
2068 'face font-lock-warning-face
2069 'help-echo "mouse-3: Select unit size"
2070 'mouse-face 'mode-line-highlight
2071 'local-map gdb-memory-unit-keymap))))
2072 (set (make-local-variable 'font-lock-defaults)
2073 '(gdb-memory-font-lock-keywords))
2074 (run-mode-hooks 'gdb-memory-mode-hook)
2075 'gdb-invalidate-memory)
2076
2077 (defun gdb-memory-buffer-name ()
2078 (with-current-buffer gud-comint-buffer
2079 (concat "*memory of " (gdb-get-target-string) "*")))
2080
2081 (defun gdb-display-memory-buffer ()
2082 "Display memory contents."
2083 (interactive)
2084 (gdb-display-buffer
2085 (gdb-get-create-buffer 'gdb-memory-buffer)))
2086
2087 (defun gdb-frame-memory-buffer ()
2088 "Display memory contents in a new frame."
2089 (interactive)
2090 (let ((special-display-regexps (append special-display-regexps '(".*")))
2091 (special-display-frame-alist gdb-frame-parameters))
2092 (display-buffer (gdb-get-create-buffer 'gdb-memory-buffer))))
2093 \f
2094
2095 ;; Locals buffer.
2096 ;;
2097 (gdb-set-buffer-rules 'gdb-locals-buffer
2098 'gdb-locals-buffer-name
2099 'gdb-locals-mode)
2100
2101 (def-gdb-auto-updated-buffer gdb-locals-buffer
2102 gdb-invalidate-locals
2103 "server info locals\n"
2104 gdb-info-locals-handler
2105 gdb-info-locals-custom)
2106
2107 ;; Abbreviate for arrays and structures.
2108 ;; These can be expanded using gud-display.
2109 (defun gdb-info-locals-handler nil
2110 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2111 gdb-pending-triggers))
2112 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2113 (with-current-buffer buf
2114 (goto-char (point-min))
2115 (while (re-search-forward "^[ }].*\n" nil t)
2116 (replace-match "" nil nil))
2117 (goto-char (point-min))
2118 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2119 (replace-match "(structure);\n" nil nil))
2120 (goto-char (point-min))
2121 (while (re-search-forward "\\s-*{.*\n" nil t)
2122 (replace-match " (array);\n" nil nil))))
2123 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2124 (and buf (with-current-buffer buf
2125 (let ((p (window-point (get-buffer-window buf 0)))
2126 (buffer-read-only nil))
2127 (erase-buffer)
2128 (insert-buffer-substring (gdb-get-create-buffer
2129 'gdb-partial-output-buffer))
2130 (set-window-point (get-buffer-window buf 0) p)))))
2131 (run-hooks 'gdb-info-locals-hook))
2132
2133 (defun gdb-info-locals-custom ()
2134 nil)
2135
2136 (defvar gdb-locals-mode-map
2137 (let ((map (make-sparse-keymap)))
2138 (suppress-keymap map)
2139 (define-key map "q" 'kill-this-buffer)
2140 map))
2141
2142 (defvar gdb-local-font-lock-keywords
2143 '(
2144 ;; var = (struct struct_tag) value
2145 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(struct\\) \\(\\(\\sw\\|[_.]\\)+\\)"
2146 (1 font-lock-variable-name-face)
2147 (3 font-lock-keyword-face)
2148 (4 font-lock-type-face))
2149 ;; var = (type) value
2150 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +(\\(\\(\\sw\\|[_.]\\)+\\)"
2151 (1 font-lock-variable-name-face)
2152 (3 font-lock-type-face))
2153 ;; var = val
2154 ( "\\(^\\(\\sw\\|[_.]\\)+\\) += +[^(]"
2155 (1 font-lock-variable-name-face))
2156 )
2157 "Font lock keywords used in `gdb-local-mode'.")
2158
2159 (defun gdb-locals-mode ()
2160 "Major mode for gdb locals.
2161
2162 \\{gdb-locals-mode-map}"
2163 (kill-all-local-variables)
2164 (setq major-mode 'gdb-locals-mode)
2165 (setq mode-name (concat "Locals:" gdb-selected-frame))
2166 (setq buffer-read-only t)
2167 (use-local-map gdb-locals-mode-map)
2168 (set (make-local-variable 'font-lock-defaults)
2169 '(gdb-local-font-lock-keywords))
2170 (run-mode-hooks 'gdb-locals-mode-hook)
2171 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
2172 'gdb-invalidate-locals
2173 'gdbmi-invalidate-locals))
2174
2175 (defun gdb-locals-buffer-name ()
2176 (with-current-buffer gud-comint-buffer
2177 (concat "*locals of " (gdb-get-target-string) "*")))
2178
2179 (defun gdb-display-locals-buffer ()
2180 "Display local variables of current stack and their values."
2181 (interactive)
2182 (gdb-display-buffer
2183 (gdb-get-create-buffer 'gdb-locals-buffer)))
2184
2185 (defun gdb-frame-locals-buffer ()
2186 "Display local variables of current stack and their values in a new frame."
2187 (interactive)
2188 (let ((special-display-regexps (append special-display-regexps '(".*")))
2189 (special-display-frame-alist gdb-frame-parameters))
2190 (display-buffer (gdb-get-create-buffer 'gdb-locals-buffer))))
2191 \f
2192
2193 ;;;; Window management
2194 (defun gdb-display-buffer (buf &optional size)
2195 (let ((answer (get-buffer-window buf 0))
2196 (must-split nil))
2197 (if answer
2198 (display-buffer buf nil 0) ;Raise the frame if necessary.
2199 ;; The buffer is not yet displayed.
2200 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2201 (let ((window (get-lru-window)))
2202 (if (and window
2203 (not (eq window (get-buffer-window gud-comint-buffer))))
2204 (progn
2205 (set-window-buffer window buf)
2206 (setq answer window))
2207 (setq must-split t)))
2208 (if must-split
2209 (let* ((largest (get-largest-window))
2210 (cur-size (window-height largest))
2211 (new-size (and size (< size cur-size) (- cur-size size))))
2212 (setq answer (split-window largest new-size))
2213 (set-window-buffer answer buf)
2214 (set-window-dedicated-p answer t)))
2215 answer)))
2216
2217 \f
2218 ;;; Shared keymap initialization:
2219
2220 (let ((menu (make-sparse-keymap "GDB-Windows")))
2221 (define-key gud-menu-map [displays]
2222 `(menu-item "GDB-Windows" ,menu
2223 :visible (memq gud-minor-mode '(gdbmi gdba))))
2224 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2225 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2226 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2227 (define-key menu [disassembly]
2228 '("Disassembly" . gdb-display-assembler-buffer))
2229 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2230 (define-key menu [inferior]
2231 '(menu-item "Inferior IO" gdb-display-inferior-io-buffer
2232 :enable gdb-use-inferior-io-buffer))
2233 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2234 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2235 (define-key menu [breakpoints]
2236 '("Breakpoints" . gdb-display-breakpoints-buffer)))
2237
2238 (let ((menu (make-sparse-keymap "GDB-Frames")))
2239 (define-key gud-menu-map [frames]
2240 `(menu-item "GDB-Frames" ,menu
2241 :visible (memq gud-minor-mode '(gdbmi gdba))))
2242 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2243 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2244 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2245 (define-key menu [disassembly] '("Disassembiy" . gdb-frame-assembler-buffer))
2246 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2247 (define-key menu [inferior]
2248 '(menu-item "Inferior IO" gdb-frame-inferior-io-buffer
2249 :enable gdb-use-inferior-io-buffer))
2250 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2251 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2252 (define-key menu [breakpoints]
2253 '("Breakpoints" . gdb-frame-breakpoints-buffer)))
2254
2255 (let ((menu (make-sparse-keymap "GDB-UI")))
2256 (define-key gud-menu-map [ui]
2257 `(menu-item "GDB-UI" ,menu :visible (eq gud-minor-mode 'gdba)))
2258 (define-key menu [gdb-use-inferior-io]
2259 ;; See defadvice below.
2260 (menu-bar-make-toggle toggle-gdb-use-inferior-io-buffer
2261 gdb-use-inferior-io-buffer
2262 "Separate inferior IO" "Use separate IO %s"
2263 "Toggle separate IO for inferior."))
2264 (define-key menu [gdb-many-windows]
2265 '(menu-item "Display Other Windows" gdb-many-windows
2266 :help "Toggle display of locals, stack and breakpoint information"
2267 :button (:toggle . gdb-many-windows)))
2268 (define-key menu [gdb-restore-windows]
2269 '(menu-item "Restore Window Layout" gdb-restore-windows
2270 :help "Restore standard layout for debug session.")))
2271
2272 ;; This function is defined above through a macro.
2273 (defadvice toggle-gdb-use-inferior-io-buffer (after gdb-kill-io-buffer activate)
2274 (unless gdb-use-inferior-io-buffer
2275 (kill-buffer (gdb-inferior-io-name))))
2276
2277 (defun gdb-frame-gdb-buffer ()
2278 "Display GUD buffer in a new frame."
2279 (interactive)
2280 (select-frame (make-frame gdb-frame-parameters))
2281 (switch-to-buffer (gdb-get-create-buffer 'gdba))
2282 (set-window-dedicated-p (selected-window) t))
2283
2284 (defun gdb-display-gdb-buffer ()
2285 "Display GUD buffer."
2286 (interactive)
2287 (gdb-display-buffer
2288 (gdb-get-create-buffer 'gdba)))
2289
2290 (defun gdb-set-window-buffer (name)
2291 (set-window-buffer (selected-window) (get-buffer name))
2292 (set-window-dedicated-p (selected-window) t))
2293
2294 (defun gdb-setup-windows ()
2295 "Layout the window pattern for `gdb-many-windows'."
2296 (gdb-display-locals-buffer)
2297 (gdb-display-stack-buffer)
2298 (delete-other-windows)
2299 (gdb-display-breakpoints-buffer)
2300 (delete-other-windows)
2301 ; Don't dedicate.
2302 (pop-to-buffer gud-comint-buffer)
2303 (split-window nil ( / ( * (window-height) 3) 4))
2304 (split-window nil ( / (window-height) 3))
2305 (split-window-horizontally)
2306 (other-window 1)
2307 (gdb-set-window-buffer (gdb-locals-buffer-name))
2308 (other-window 1)
2309 (switch-to-buffer
2310 (if gud-last-last-frame
2311 (gud-find-file (car gud-last-last-frame))
2312 (gud-find-file gdb-main-file)))
2313 (when gdb-use-inferior-io-buffer
2314 (split-window-horizontally)
2315 (other-window 1)
2316 (gdb-set-window-buffer
2317 (gdb-get-create-buffer 'gdb-inferior-io)))
2318 (other-window 1)
2319 (gdb-set-window-buffer (gdb-stack-buffer-name))
2320 (split-window-horizontally)
2321 (other-window 1)
2322 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2323 (other-window 1))
2324
2325 (defcustom gdb-many-windows nil
2326 "Nil means just pop up the GUD buffer unless `gdb-show-main' is t.
2327 In this case it starts with two windows: one displaying the GUD
2328 buffer and the other with the source file with the main routine
2329 of the inferior. Non-nil means display the layout shown for
2330 `gdba'."
2331 :type 'boolean
2332 :group 'gud
2333 :version "22.1")
2334
2335 (defun gdb-many-windows (arg)
2336 "Toggle the number of windows in the basic arrangement."
2337 (interactive "P")
2338 (setq gdb-many-windows
2339 (if (null arg)
2340 (not gdb-many-windows)
2341 (> (prefix-numeric-value arg) 0)))
2342 (condition-case nil
2343 (gdb-restore-windows)
2344 (error nil)))
2345
2346 (defun gdb-restore-windows ()
2347 "Restore the basic arrangement of windows used by gdba.
2348 This arrangement depends on the value of `gdb-many-windows'."
2349 (interactive)
2350 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2351 (delete-other-windows)
2352 (if gdb-many-windows
2353 (gdb-setup-windows)
2354 (split-window)
2355 (other-window 1)
2356 (switch-to-buffer
2357 (if gud-last-last-frame
2358 (gud-find-file (car gud-last-last-frame))
2359 (gud-find-file gdb-main-file)))
2360 (other-window 1)))
2361
2362 (defun gdb-reset ()
2363 "Exit a debugging session cleanly.
2364 Kills the gdb buffers and resets the source buffers."
2365 (dolist (buffer (buffer-list))
2366 (unless (eq buffer gud-comint-buffer)
2367 (with-current-buffer buffer
2368 (if (memq gud-minor-mode '(gdbmi gdba))
2369 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2370 (kill-buffer nil)
2371 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2372 (setq gud-minor-mode nil)
2373 (kill-local-variable 'tool-bar-map)
2374 (kill-local-variable 'gdb-define-alist))))))
2375 (when (markerp gdb-overlay-arrow-position)
2376 (move-marker gdb-overlay-arrow-position nil)
2377 (setq gdb-overlay-arrow-position nil))
2378 (setq overlay-arrow-variable-list
2379 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2380 (setq gud-running nil)
2381 (setq gdb-active-process nil)
2382 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2383
2384 (defun gdb-source-info ()
2385 "Find the source file where the program starts and displays it with related
2386 buffers."
2387 (goto-char (point-min))
2388 (if (and (search-forward "Located in " nil t)
2389 (looking-at "\\S-+"))
2390 (setq gdb-main-file (match-string 0)))
2391 (goto-char (point-min))
2392 (if (search-forward "Includes preprocessor macro info." nil t)
2393 (setq gdb-macro-info t))
2394 (if gdb-many-windows
2395 (gdb-setup-windows)
2396 (gdb-get-create-buffer 'gdb-breakpoints-buffer)
2397 (if gdb-show-main
2398 (let ((pop-up-windows t))
2399 (display-buffer (gud-find-file gdb-main-file))))))
2400
2401 (defun gdb-get-location (bptno line flag)
2402 "Find the directory containing the relevant source file.
2403 Put in buffer and place breakpoint icon."
2404 (goto-char (point-min))
2405 (catch 'file-not-found
2406 (if (search-forward "Located in " nil t)
2407 (when (looking-at "\\S-+")
2408 (delete (cons bptno "File not found") gdb-location-alist)
2409 (push (cons bptno (match-string 0)) gdb-location-alist))
2410 (gdb-resync)
2411 (unless (assoc bptno gdb-location-alist)
2412 (push (cons bptno "File not found") gdb-location-alist)
2413 (message-box "Cannot find source file for breakpoint location.\n\
2414 Add directory to search path for source files using the GDB command, dir."))
2415 (throw 'file-not-found nil))
2416 (with-current-buffer
2417 (find-file-noselect (match-string 0))
2418 (save-current-buffer
2419 (set (make-local-variable 'gud-minor-mode) 'gdba)
2420 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2421 ;; only want one breakpoint icon at each location
2422 (save-excursion
2423 (goto-line (string-to-number line))
2424 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2425
2426 (add-hook 'find-file-hook 'gdb-find-file-hook)
2427
2428 (defun gdb-find-file-hook ()
2429 "Set up buffer for debugging if file is part of the source code
2430 of the current session."
2431 (if (and (not gdb-find-file-unhook)
2432 ;; in case gud or gdb-ui is just loaded
2433 gud-comint-buffer
2434 (buffer-name gud-comint-buffer)
2435 (with-current-buffer gud-comint-buffer
2436 (eq gud-minor-mode 'gdba)))
2437 (condition-case nil
2438 (gdb-enqueue-input
2439 (list (concat gdb-server-prefix "list "
2440 (file-name-nondirectory buffer-file-name)
2441 ":1\n")
2442 `(lambda () (gdb-set-gud-minor-mode ,(current-buffer)))))
2443 (error (setq gdb-find-file-unhook t)))))
2444
2445 ;;from put-image
2446 (defun gdb-put-string (putstring pos &optional dprop)
2447 "Put string PUTSTRING in front of POS in the current buffer.
2448 PUTSTRING is displayed by putting an overlay into the current buffer with a
2449 `before-string' string that has a `display' property whose value is
2450 PUTSTRING."
2451 (let ((string (make-string 1 ?x))
2452 (buffer (current-buffer)))
2453 (setq putstring (copy-sequence putstring))
2454 (let ((overlay (make-overlay pos pos buffer))
2455 (prop (or dprop
2456 (list (list 'margin 'left-margin) putstring))))
2457 (put-text-property 0 (length string) 'display prop string)
2458 (overlay-put overlay 'put-break t)
2459 (overlay-put overlay 'before-string string))))
2460
2461 ;;from remove-images
2462 (defun gdb-remove-strings (start end &optional buffer)
2463 "Remove strings between START and END in BUFFER.
2464 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2465 BUFFER nil or omitted means use the current buffer."
2466 (unless buffer
2467 (setq buffer (current-buffer)))
2468 (dolist (overlay (overlays-in start end))
2469 (when (overlay-get overlay 'put-break)
2470 (delete-overlay overlay))))
2471
2472 (defun gdb-put-breakpoint-icon (enabled bptno)
2473 (let ((start (- (line-beginning-position) 1))
2474 (end (+ (line-end-position) 1))
2475 (putstring (if enabled "B" "b"))
2476 (source-window (get-buffer-window (current-buffer) 0)))
2477 (add-text-properties
2478 0 1 '(help-echo "mouse-1: set/clear bkpt, mouse-3: enable/disable bkpt")
2479 putstring)
2480 (if enabled (add-text-properties
2481 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2482 (add-text-properties
2483 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2484 (gdb-remove-breakpoint-icons start end)
2485 (if (display-images-p)
2486 (if (>= (or left-fringe-width
2487 (if source-window (car (window-fringes source-window)))
2488 gdb-buffer-fringe-width) 8)
2489 (gdb-put-string
2490 nil (1+ start)
2491 `(left-fringe breakpoint
2492 ,(if enabled
2493 'breakpoint-enabled
2494 'breakpoint-disabled)))
2495 (when (< left-margin-width 2)
2496 (save-current-buffer
2497 (setq left-margin-width 2)
2498 (if source-window
2499 (set-window-margins
2500 source-window
2501 left-margin-width right-margin-width))))
2502 (put-image
2503 (if enabled
2504 (or breakpoint-enabled-icon
2505 (setq breakpoint-enabled-icon
2506 (find-image `((:type xpm :data
2507 ,breakpoint-xpm-data
2508 :ascent 100 :pointer hand)
2509 (:type pbm :data
2510 ,breakpoint-enabled-pbm-data
2511 :ascent 100 :pointer hand)))))
2512 (or breakpoint-disabled-icon
2513 (setq breakpoint-disabled-icon
2514 (find-image `((:type xpm :data
2515 ,breakpoint-xpm-data
2516 :conversion disabled
2517 :ascent 100)
2518 (:type pbm :data
2519 ,breakpoint-disabled-pbm-data
2520 :ascent 100))))))
2521 (+ start 1)
2522 putstring
2523 'left-margin))
2524 (when (< left-margin-width 2)
2525 (save-current-buffer
2526 (setq left-margin-width 2)
2527 (if (get-buffer-window (current-buffer) 0)
2528 (set-window-margins
2529 (get-buffer-window (current-buffer) 0)
2530 left-margin-width right-margin-width))))
2531 (gdb-put-string
2532 (propertize putstring
2533 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
2534 (1+ start)))))
2535
2536 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
2537 (gdb-remove-strings start end)
2538 (if (display-images-p)
2539 (remove-images start end))
2540 (when remove-margin
2541 (setq left-margin-width 0)
2542 (if (get-buffer-window (current-buffer) 0)
2543 (set-window-margins
2544 (get-buffer-window (current-buffer) 0)
2545 left-margin-width right-margin-width))))
2546
2547 \f
2548 ;;
2549 ;; Assembler buffer.
2550 ;;
2551 (gdb-set-buffer-rules 'gdb-assembler-buffer
2552 'gdb-assembler-buffer-name
2553 'gdb-assembler-mode)
2554
2555 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2556 gdb-invalidate-assembler
2557 (concat gdb-server-prefix "disassemble "
2558 (if (member gdb-frame-address '(nil "main")) nil "0x")
2559 gdb-frame-address "\n")
2560 gdb-assembler-handler
2561 gdb-assembler-custom)
2562
2563 (defun gdb-assembler-custom ()
2564 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2565 (pos 1) (address) (flag) (bptno))
2566 (with-current-buffer buffer
2567 (save-excursion
2568 (if (not (equal gdb-frame-address "main"))
2569 (progn
2570 (goto-char (point-min))
2571 (if (and gdb-frame-address
2572 (re-search-forward gdb-frame-address nil t))
2573 (progn
2574 (setq pos (point))
2575 (beginning-of-line)
2576 (or gdb-overlay-arrow-position
2577 (setq gdb-overlay-arrow-position (make-marker)))
2578 (set-marker gdb-overlay-arrow-position
2579 (point) (current-buffer))))))
2580 ;; remove all breakpoint-icons in assembler buffer before updating.
2581 (gdb-remove-breakpoint-icons (point-min) (point-max))))
2582 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2583 (goto-char (point-min))
2584 (while (< (point) (- (point-max) 1))
2585 (forward-line 1)
2586 (if (looking-at "[^\t].*?breakpoint")
2587 (progn
2588 (looking-at
2589 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
2590 (setq bptno (match-string 1))
2591 (setq flag (char-after (match-beginning 2)))
2592 (setq address (match-string 3))
2593 (with-current-buffer buffer
2594 (save-excursion
2595 (goto-char (point-min))
2596 (if (re-search-forward address nil t)
2597 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
2598 (if (not (equal gdb-frame-address "main"))
2599 (set-window-point (get-buffer-window buffer 0) pos))))
2600
2601 (defvar gdb-assembler-mode-map
2602 (let ((map (make-sparse-keymap)))
2603 (suppress-keymap map)
2604 (define-key map "q" 'kill-this-buffer)
2605 map))
2606
2607 (defvar gdb-assembler-font-lock-keywords
2608 '(;; <__function.name+n>
2609 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
2610 (1 font-lock-function-name-face))
2611 ;; 0xNNNNNNNN <__function.name+n>: opcode
2612 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
2613 (4 font-lock-keyword-face))
2614 ;; %register(at least i386)
2615 ("%\\sw+" . font-lock-variable-name-face)
2616 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
2617 (1 font-lock-comment-face)
2618 (2 font-lock-function-name-face))
2619 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
2620 "Font lock keywords used in `gdb-assembler-mode'.")
2621
2622 (defun gdb-assembler-mode ()
2623 "Major mode for viewing code assembler.
2624
2625 \\{gdb-assembler-mode-map}"
2626 (kill-all-local-variables)
2627 (setq major-mode 'gdb-assembler-mode)
2628 (setq mode-name (concat "Machine:" gdb-selected-frame))
2629 (setq gdb-overlay-arrow-position nil)
2630 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
2631 (setq fringes-outside-margins t)
2632 (setq buffer-read-only t)
2633 (use-local-map gdb-assembler-mode-map)
2634 (gdb-invalidate-assembler)
2635 (set (make-local-variable 'font-lock-defaults)
2636 '(gdb-assembler-font-lock-keywords))
2637 (run-mode-hooks 'gdb-assembler-mode-hook)
2638 'gdb-invalidate-assembler)
2639
2640 (defun gdb-assembler-buffer-name ()
2641 (with-current-buffer gud-comint-buffer
2642 (concat "*Disassembly of " (gdb-get-target-string) "*")))
2643
2644 (defun gdb-display-assembler-buffer ()
2645 "Display disassembly view."
2646 (interactive)
2647 (setq gdb-previous-frame nil)
2648 (gdb-display-buffer
2649 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2650
2651 (defun gdb-frame-assembler-buffer ()
2652 "Display disassembly view in a new frame."
2653 (interactive)
2654 (setq gdb-previous-frame nil)
2655 (let ((special-display-regexps (append special-display-regexps '(".*")))
2656 (special-display-frame-alist gdb-frame-parameters))
2657 (display-buffer (gdb-get-create-buffer 'gdb-assembler-buffer))))
2658
2659 ;; modified because if gdb-frame-address has changed value a new command
2660 ;; must be enqueued to update the buffer with the new output
2661 (defun gdb-invalidate-assembler (&optional ignored)
2662 (if (gdb-get-buffer 'gdb-assembler-buffer)
2663 (progn
2664 (unless (and gdb-selected-frame
2665 (string-equal gdb-selected-frame gdb-previous-frame))
2666 (if (or (not (member 'gdb-invalidate-assembler
2667 gdb-pending-triggers))
2668 (not (string-equal gdb-frame-address
2669 gdb-previous-frame-address)))
2670 (progn
2671 ;; take previous disassemble command, if any, off the queue
2672 (with-current-buffer gud-comint-buffer
2673 (let ((queue gdb-input-queue))
2674 (dolist (item queue)
2675 (if (equal (cdr item) '(gdb-assembler-handler))
2676 (setq gdb-input-queue
2677 (delete item gdb-input-queue))))))
2678 (gdb-enqueue-input
2679 (list
2680 (concat gdb-server-prefix "disassemble "
2681 (if (member gdb-frame-address '(nil "main")) nil "0x")
2682 gdb-frame-address "\n")
2683 'gdb-assembler-handler))
2684 (push 'gdb-invalidate-assembler gdb-pending-triggers)
2685 (setq gdb-previous-frame-address gdb-frame-address)
2686 (setq gdb-previous-frame gdb-selected-frame)))))))
2687
2688 (defun gdb-get-selected-frame ()
2689 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
2690 (progn
2691 (gdb-enqueue-input
2692 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
2693 (push 'gdb-get-selected-frame
2694 gdb-pending-triggers))))
2695
2696 (defun gdb-frame-handler ()
2697 (setq gdb-pending-triggers
2698 (delq 'gdb-get-selected-frame gdb-pending-triggers))
2699 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2700 (goto-char (point-min))
2701 (if (re-search-forward "Stack level \\([0-9]+\\)" nil t)
2702 (setq gdb-frame-number (match-string 1)))
2703 (goto-char (point-min))
2704 (if (re-search-forward
2705 ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(\\S-*?\\);? " nil t)
2706 (progn
2707 (setq gdb-selected-frame (match-string 2))
2708 (if (gdb-get-buffer 'gdb-locals-buffer)
2709 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
2710 (setq mode-name (concat "Locals:" gdb-selected-frame))))
2711 (if (gdb-get-buffer 'gdb-assembler-buffer)
2712 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
2713 (setq mode-name (concat "Machine:" gdb-selected-frame))))
2714 (setq gdb-frame-address (match-string 1))))
2715 (goto-char (point-min))
2716 (if (re-search-forward " source language \\(\\S-*\\)\." nil t)
2717 (setq gdb-current-language (match-string 1))))
2718 (gdb-invalidate-assembler))
2719
2720 (provide 'gdb-ui)
2721
2722 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
2723 ;;; gdb-ui.el ends here