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