*** empty log message ***
[emacs.git] / lisp / gdb-ui.el
blobc20145b4ebf85abee0fd41e84788063d6d397beb
1 ;;; gdb-ui.el --- User Interface for running GDB
3 ;; Author: Nick Roberts <nick@nick.uklinux.net>
4 ;; Maintainer: FSF
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 2002 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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.
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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
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 and displays
32 ;; expressions and their current values in their own buffers. It also uses
33 ;; features of Emacs 21 such as the display margin for breakpoints, and the
34 ;; toolbar (see the GDB User Interface section in the Emacs info manual).
36 ;; Start the debugger with M-x gdba.
38 ;; This file is based on gdba.el from GDB 5.0 written by Tom Lord and Jim
39 ;; Kingdon and uses GDB's annotation interface. You don't need to know about
40 ;; annotations to use this mode as a debugger, but if you are interested
41 ;; developing the mode itself, then see the Annotations section in the GDB
42 ;; info manual.
44 ;; Known Bugs:
45 ;; Does not auto-display arrays of structures or structures containing arrays.
46 ;; On MS Windows, Gdb 5.1.1 from MinGW 2.0 does not flush the output from the
47 ;; inferior.
49 ;;; Code:
51 (require 'gud)
53 (defcustom gdb-window-height 20
54 "Number of lines in a frame for a displayed expression in GDB-UI."
55 :type 'integer
56 :group 'gud)
58 (defcustom gdb-window-width 30
59 "Width of a frame for a displayed expression in GDB-UI."
60 :type 'integer
61 :group 'gud)
63 (defvar gdb-current-address nil "Initialisation for Assembler buffer.")
64 (defvar gdb-previous-address nil)
65 (defvar gdb-display-in-progress nil)
66 (defvar gdb-dive nil)
67 (defvar gdb-view-source t "Non-nil means that source code can be viewed")
68 (defvar gdb-selected-view 'source "Code type that user wishes to view")
69 (defvar gdb-buffer-type nil)
70 (defvar gdb-variables '()
71 "A list of variables that are local to the GUD buffer.")
74 ;;;###autoload
75 (defun gdba (command-line)
76 "Run gdb on program FILE in buffer *gud-FILE*.
77 The directory containing FILE becomes the initial working directory
78 and source-file directory for your debugger.
80 If `gdb-many-windows' is nil (the default value) then gdb starts with
81 just two windows : the GUD and the source buffer. If it is t the
82 following layout will appear (keybindings given in relevant buffer) :
84 ---------------------------------------------------------------------
85 GDB Toolbar
86 ---------------------------------------------------------------------
87 GUD buffer (I/O of GDB) | Locals buffer
91 ---------------------------------------------------------------------
92 Source buffer | Input/Output (of debuggee) buffer
93 | (comint-mode)
100 ---------------------------------------------------------------------
101 Stack buffer | Breakpoints buffer
102 RET gdb-frames-select | SPC gdb-toggle-breakpoint
103 | RET gdb-goto-breakpoint
104 | d gdb-delete-breakpoint
105 ---------------------------------------------------------------------
107 All the buffers share the toolbar and source should always display in the same
108 window e.g after typing g on a breakpoint in the breakpoints buffer. Breakpoint
109 icons are displayed both by setting a break with gud-break and by typing break
110 in the GUD buffer.
112 This works best (depending on the size of your monitor) using most of the
113 screen.
115 Displayed expressions appear in separate frames. Arrays may be displayed
116 as slices and visualised using the graph program from plotutils if installed.
117 Pointers in structures may be followed in a tree-like fashion.
119 The following interactive lisp functions help control operation :
121 `gdb-many-windows' - Toggle the number of windows gdb uses.
122 `gdb-restore-windows' - To restore the window layout.
123 `gdb-quit' - To delete (most) of the buffers used by GDB-UI and
124 reset variables."
126 (interactive (list (gud-query-cmdline 'gdba)))
128 ;; Let's start with a basic gud-gdb buffer and then modify it a bit.
129 (gdb command-line)
131 (set (make-local-variable 'gud-minor-mode) 'gdba)
132 (set (make-local-variable 'gud-marker-filter) 'gud-gdba-marker-filter)
134 (gud-def gud-break (if (not (string-equal mode-name "Assembler"))
135 (gud-call "break %f:%l" arg)
136 (save-excursion
137 (beginning-of-line)
138 (forward-char 2)
139 (gud-call "break *%a" arg)))
140 "\C-b" "Set breakpoint at current line or address.")
142 (gud-def gud-remove (if (not (string-equal mode-name "Assembler"))
143 (gud-call "clear %f:%l" arg)
144 (save-excursion
145 (beginning-of-line)
146 (forward-char 2)
147 (gud-call "clear *%a" arg)))
148 "\C-d" "Remove breakpoint at current line or address.")
150 (gud-def gud-until (if (not (string-equal mode-name "Assembler"))
151 (gud-call "until %f:%l" arg)
152 (save-excursion
153 (beginning-of-line)
154 (forward-char 2)
155 (gud-call "until *%a" arg)))
156 "\C-u" "Continue to current line or address.")
158 (setq comint-input-sender 'gdb-send)
160 ;; (re-)initialise
161 (setq gdb-current-address "main")
162 (setq gdb-previous-address nil)
163 (setq gdb-display-in-progress nil)
164 (setq gdb-dive nil)
165 (setq gdb-view-source t)
166 (setq gdb-selected-view 'source)
168 (mapc 'make-local-variable gdb-variables)
169 (setq gdb-buffer-type 'gdba)
171 (gdb-clear-inferior-io)
173 (if (eq window-system 'w32)
174 (gdb-enqueue-input (list "set new-console off\n" 'ignore)))
175 (gdb-enqueue-input (list "set height 0\n" 'ignore))
176 ;; find source file and compilation directory here
177 (gdb-enqueue-input (list "server list\n" 'ignore)) ; C program
178 (gdb-enqueue-input (list "server list MAIN__\n" 'ignore)) ; Fortran program
179 (gdb-enqueue-input (list "server info source\n"
180 'gdb-source-info))
182 (run-hooks 'gdba-mode-hook))
184 (defun gud-display ()
185 "Auto-display (possibly dereferenced) C expression at point."
186 (interactive)
187 (save-excursion
188 (let ((expr (gud-find-c-expr)))
189 (gdb-enqueue-input
190 (list (concat "server ptype " expr "\n")
191 `(lambda () (gud-display1 ,expr)))))))
193 (defun gud-display1 (expr)
194 (goto-char (point-min))
195 (if (looking-at "No symbol")
196 (progn
197 (gdb-set-output-sink 'user)
198 (gud-call (concat "server ptype " expr)))
199 (goto-char (- (point-max) 1))
200 (if (equal (char-before) (string-to-char "\*"))
201 (gdb-enqueue-input
202 (list (concat "server display* " expr "\n") 'ignore))
203 (gdb-enqueue-input
204 (list (concat "server display " expr "\n") 'ignore)))))
206 ; this would messy because these bindings don't work with M-x gdb
207 ; (define-key global-map "\C-x\C-a\C-a" 'gud-display)
208 ; (define-key gud-minor-mode-map "\C-c\C-a" 'gud-display)
212 ;; ======================================================================
214 ;; In this world, there are gdb variables (of unspecified
215 ;; representation) and buffers associated with those objects.
216 ;; The list of variables is built up by the expansions of
217 ;; def-gdb-variable
219 (defmacro def-gdb-var (root-symbol &optional default doc)
220 (let* ((root (symbol-name root-symbol))
221 (accessor (intern (concat "gdb-get-" root)))
222 (setter (intern (concat "gdb-set-" root)))
223 (name (intern (concat "gdb-" root))))
224 `(progn
225 (defvar ,name ,default ,doc)
226 (if (not (memq ',name gdb-variables))
227 (push ',name gdb-variables))
228 (defun ,accessor ()
229 (buffer-local-value ',name gud-comint-buffer))
230 (defun ,setter (val)
231 (with-current-buffer gud-comint-buffer
232 (setq ,name val))))))
234 (def-gdb-var buffer-type nil
235 "One of the symbols bound in gdb-buffer-rules")
237 (def-gdb-var burst ""
238 "A string of characters from gdb that have not yet been processed.")
240 (def-gdb-var input-queue ()
241 "A list of high priority gdb command objects.")
243 (def-gdb-var idle-input-queue ()
244 "A list of low priority gdb command objects.")
246 (def-gdb-var prompting nil
247 "True when gdb is idle with no pending input.")
249 (def-gdb-var output-sink 'user
250 "The disposition of the output of the current gdb command.
251 Possible values are these symbols:
253 user -- gdb output should be copied to the GUD buffer
254 for the user to see.
256 inferior -- gdb output should be copied to the inferior-io buffer
258 pre-emacs -- output should be ignored util the post-prompt
259 annotation is received. Then the output-sink
260 becomes:...
261 emacs -- output should be collected in the partial-output-buffer
262 for subsequent processing by a command. This is the
263 disposition of output generated by commands that
264 gdb mode sends to gdb on its own behalf.
265 post-emacs -- ignore input until the prompt annotation is
266 received, then go to USER disposition.
269 (def-gdb-var current-item nil
270 "The most recent command item sent to gdb.")
272 (def-gdb-var pending-triggers '()
273 "A list of trigger functions that have run later than their output
274 handlers.")
276 ;; end of gdb variables
278 (defun gdb-get-target-string ()
279 (with-current-buffer gud-comint-buffer
280 gud-target-name))
284 ;; gdb buffers.
286 ;; Each buffer has a TYPE -- a symbol that identifies the function
287 ;; of that particular buffer.
289 ;; The usual gdb interaction buffer is given the type `gdba' and
290 ;; is constructed specially.
292 ;; Others are constructed by gdb-get-create-buffer and
293 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
295 (defvar gdb-buffer-rules-assoc '())
297 (defun gdb-get-buffer (key)
298 "Return the gdb buffer tagged with type KEY.
299 The key should be one of the cars in `gdb-buffer-rules-assoc'."
300 (save-excursion
301 (gdb-look-for-tagged-buffer key (buffer-list))))
303 (defun gdb-get-create-buffer (key)
304 "Create a new gdb buffer of the type specified by KEY.
305 The key should be one of the cars in `gdb-buffer-rules-assoc'."
306 (or (gdb-get-buffer key)
307 (let* ((rules (assoc key gdb-buffer-rules-assoc))
308 (name (funcall (gdb-rules-name-maker rules)))
309 (new (get-buffer-create name)))
310 (with-current-buffer new
311 ;; FIXME: This should be set after calling the function, since the
312 ;; function should run kill-all-local-variables.
313 (set (make-local-variable 'gdb-buffer-type) key)
314 (if (cdr (cdr rules))
315 (funcall (car (cdr (cdr rules)))))
316 (set (make-local-variable 'gud-comint-buffer) gud-comint-buffer)
317 (set (make-local-variable 'gud-minor-mode) 'gdba)
318 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
319 new))))
321 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
323 (defun gdb-look-for-tagged-buffer (key bufs)
324 (let ((retval nil))
325 (while (and (not retval) bufs)
326 (set-buffer (car bufs))
327 (if (eq gdb-buffer-type key)
328 (setq retval (car bufs)))
329 (setq bufs (cdr bufs)))
330 retval))
333 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
334 ;; at least one and possible more functions. The functions have these
335 ;; roles in defining a buffer type:
337 ;; NAME - Return a name for this buffer type.
339 ;; The remaining function(s) are optional:
341 ;; MODE - called in a new buffer with no arguments, should establish
342 ;; the proper mode for the buffer.
345 (defun gdb-set-buffer-rules (buffer-type &rest rules)
346 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
347 (if binding
348 (setcdr binding rules)
349 (push (cons buffer-type rules)
350 gdb-buffer-rules-assoc))))
352 ;; GUD buffers are an exception to the rules
353 (gdb-set-buffer-rules 'gdba 'error)
356 ;; Partial-output buffer : This accumulates output from a command executed on
357 ;; behalf of emacs (rather than the user).
359 (gdb-set-buffer-rules 'gdb-partial-output-buffer
360 'gdb-partial-output-name)
362 (defun gdb-partial-output-name ()
363 (concat "*partial-output-"
364 (gdb-get-target-string)
365 "*"))
368 (gdb-set-buffer-rules 'gdb-inferior-io
369 'gdb-inferior-io-name
370 'gdb-inferior-io-mode)
372 (defun gdb-inferior-io-name ()
373 (concat "*input/output of "
374 (gdb-get-target-string)
375 "*"))
377 (defvar gdb-inferior-io-mode-map
378 (let ((map (make-sparse-keymap)))
379 (define-key map "\C-c\C-c" 'gdb-inferior-io-interrupt)
380 (define-key map "\C-c\C-z" 'gdb-inferior-io-stop)
381 (define-key map "\C-c\C-\\" 'gdb-inferior-io-quit)
382 (define-key map "\C-c\C-d" 'gdb-inferior-io-eof)
383 map))
385 (define-derived-mode gdb-inferior-io-mode comint-mode "Debuggee I/O"
386 "Major mode for gdb inferior-io."
387 :syntax-table nil :abbrev-table nil
388 ;; We want to use comint because it has various nifty and familiar
389 ;; features. We don't need a process, but comint wants one, so create
390 ;; a dummy one.
391 (make-comint-in-buffer
392 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
393 (current-buffer) "hexl")
394 (setq comint-input-sender 'gdb-inferior-io-sender))
396 (defun gdb-inferior-io-sender (proc string)
397 ;; PROC is the pseudo-process created to satisfy comint.
398 (with-current-buffer (process-buffer proc)
399 (setq proc (get-buffer-process gud-comint-buffer))
400 (process-send-string proc string)
401 (process-send-string proc "\n")))
403 (defun gdb-inferior-io-interrupt ()
404 "Interrupt the program being debugged."
405 (interactive)
406 (interrupt-process
407 (get-buffer-process gud-comint-buffer) comint-ptyp))
409 (defun gdb-inferior-io-quit ()
410 "Send quit signal to the program being debugged."
411 (interactive)
412 (quit-process
413 (get-buffer-process gud-comint-buffer) comint-ptyp))
415 (defun gdb-inferior-io-stop ()
416 "Stop the program being debugged."
417 (interactive)
418 (stop-process
419 (get-buffer-process gud-comint-buffer) comint-ptyp))
421 (defun gdb-inferior-io-eof ()
422 "Send end-of-file to the program being debugged."
423 (interactive)
424 (process-send-eof
425 (get-buffer-process gud-comint-buffer)))
429 ;; gdb communications
432 ;; INPUT: things sent to gdb
434 ;; There is a high and low priority input queue. Low priority input is sent
435 ;; only when the high priority queue is idle.
437 ;; The queues are lists. Each element is either a string (indicating user or
438 ;; user-like input) or a list of the form:
440 ;; (INPUT-STRING HANDLER-FN)
442 ;; The handler function will be called from the partial-output buffer when the
443 ;; command completes. This is the way to write commands which invoke gdb
444 ;; commands autonomously.
446 ;; These lists are consumed tail first.
449 (defun gdb-send (proc string)
450 "A comint send filter for gdb.
451 This filter may simply queue output for a later time."
452 (gdb-enqueue-input (concat string "\n")))
454 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
455 ;; is a query, or other non-top-level prompt. To guarantee stuff will get
456 ;; sent to the top-level prompt, currently it must be put in the idle queue.
457 ;; ^^^^^^^^^
458 ;; [This should encourage gdb extensions that invoke gdb commands to let
459 ;; the user go first; it is not a bug. -t]
462 (defun gdb-enqueue-input (item)
463 (if (gdb-get-prompting)
464 (progn
465 (gdb-send-item item)
466 (gdb-set-prompting nil))
467 (gdb-set-input-queue
468 (cons item (gdb-get-input-queue)))))
470 (defun gdb-dequeue-input ()
471 (let ((queue (gdb-get-input-queue)))
472 (and queue
473 (if (not (cdr queue))
474 (let ((answer (car queue)))
475 (gdb-set-input-queue '())
476 answer)
477 (gdb-take-last-elt queue)))))
479 (defun gdb-enqueue-idle-input (item)
480 (if (and (gdb-get-prompting)
481 (not (gdb-get-input-queue)))
482 (progn
483 (gdb-send-item item)
484 (gdb-set-prompting nil))
485 (gdb-set-idle-input-queue
486 (cons item (gdb-get-idle-input-queue)))))
488 (defun gdb-dequeue-idle-input ()
489 (let ((queue (gdb-get-idle-input-queue)))
490 (and queue
491 (if (not (cdr queue))
492 (let ((answer (car queue)))
493 (gdb-set-idle-input-queue '())
494 answer)
495 (gdb-take-last-elt queue)))))
497 ;; Don't use this in general.
498 (defun gdb-take-last-elt (l)
499 (if (cdr (cdr l))
500 (gdb-take-last-elt (cdr l))
501 (let ((answer (car (cdr l))))
502 (setcdr l '())
503 answer)))
507 ;; output -- things gdb prints to emacs
509 ;; GDB output is a stream interrupted by annotations.
510 ;; Annotations can be recognized by their beginning
511 ;; with \C-j\C-z\C-z<tag><opt>\C-j
513 ;; The tag is a string obeying symbol syntax.
515 ;; The optional part `<opt>' can be either the empty string
516 ;; or a space followed by more data relating to the annotation.
517 ;; For example, the SOURCE annotation is followed by a filename,
518 ;; line number and various useless goo. This data must not include
519 ;; any newlines.
522 (defcustom gud-gdba-command-name "gdb -annotate=2 -noasync"
523 "Default command to execute an executable under the GDB-UI debugger."
524 :type 'string
525 :group 'gud)
527 (defvar gdb-annotation-rules
528 '(("pre-prompt" gdb-pre-prompt)
529 ("prompt" gdb-prompt)
530 ("commands" gdb-subprompt)
531 ("overload-choice" gdb-subprompt)
532 ("query" gdb-subprompt)
533 ("prompt-for-continue" gdb-subprompt)
534 ("post-prompt" gdb-post-prompt)
535 ("source" gdb-source)
536 ("starting" gdb-starting)
537 ("exited" gdb-stopping)
538 ("signalled" gdb-stopping)
539 ("signal" gdb-stopping)
540 ("breakpoint" gdb-stopping)
541 ("watchpoint" gdb-stopping)
542 ("frame-begin" gdb-frame-begin)
543 ("stopped" gdb-stopped)
544 ("display-begin" gdb-display-begin)
545 ("display-end" gdb-display-end)
546 ; GDB commands info stack, info locals and frame generate an error-begin
547 ; annotation at start when there is no stack but this is a quirk/bug in
548 ; annotations.
549 ; ("error-begin" gdb-error-begin)
550 ("display-number-end" gdb-display-number-end)
551 ("array-section-begin" gdb-array-section-begin)
552 ("array-section-end" gdb-array-section-end)
553 ;; ("elt" gdb-elt)
554 ("field-begin" gdb-field-begin)
555 ("field-end" gdb-field-end)
556 ) "An assoc mapping annotation tags to functions which process them.")
558 (defun gdb-ignore-annotation (args)
559 nil)
561 (defconst gdb-source-spec-regexp
562 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:\\(0x[a-f0-9]*\\)")
564 ;; Do not use this except as an annotation handler.
565 (defun gdb-source (args)
566 (string-match gdb-source-spec-regexp args)
567 ;; Extract the frame position from the marker.
568 (setq gud-last-frame
569 (cons
570 (match-string 1 args)
571 (string-to-int (match-string 2 args))))
572 (setq gdb-current-address (match-string 3 args))
573 (setq gdb-view-source t)
574 ;;update with new frame for machine code if necessary
575 (gdb-invalidate-assembler))
577 (defun gdb-send-item (item)
578 (gdb-set-current-item item)
579 (if (stringp item)
580 (progn
581 (gdb-set-output-sink 'user)
582 (process-send-string (get-buffer-process gud-comint-buffer) item))
583 (progn
584 (gdb-clear-partial-output)
585 (gdb-set-output-sink 'pre-emacs)
586 (process-send-string (get-buffer-process gud-comint-buffer)
587 (car item)))))
589 (defun gdb-pre-prompt (ignored)
590 "An annotation handler for `pre-prompt'. This terminates the collection of
591 output from a previous command if that happens to be in effect."
592 (let ((sink (gdb-get-output-sink)))
593 (cond
594 ((eq sink 'user) t)
595 ((eq sink 'emacs)
596 (gdb-set-output-sink 'post-emacs)
597 (let ((handler
598 (car (cdr (gdb-get-current-item)))))
599 (save-excursion
600 (set-buffer (gdb-get-create-buffer
601 'gdb-partial-output-buffer))
602 (funcall handler))))
604 (gdb-set-output-sink 'user)
605 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
607 (defun gdb-prompt (ignored)
608 "An annotation handler for `prompt'.
609 This sends the next command (if any) to gdb."
610 (let ((sink (gdb-get-output-sink)))
611 (cond
612 ((eq sink 'user) t)
613 ((eq sink 'post-emacs)
614 (gdb-set-output-sink 'user))
616 (gdb-set-output-sink 'user)
617 (error "Phase error in gdb-prompt (got %s)" sink))))
618 (let ((highest (gdb-dequeue-input)))
619 (if highest
620 (gdb-send-item highest)
621 (let ((lowest (gdb-dequeue-idle-input)))
622 (if lowest
623 (gdb-send-item lowest)
624 (progn
625 (gdb-set-prompting t)
626 (gud-display-frame)))))))
628 (defun gdb-subprompt (ignored)
629 "An annotation handler for non-top-level prompts."
630 (let ((highest (gdb-dequeue-input)))
631 (if highest
632 (gdb-send-item highest)
633 (gdb-set-prompting t))))
635 (defun gdb-starting (ignored)
636 "An annotation handler for `starting'. This says that I/O for the
637 subprocess is now the program being debugged, not GDB."
638 (let ((sink (gdb-get-output-sink)))
639 (cond
640 ((eq sink 'user)
641 (progn
642 (setq gud-running t)
643 (gdb-set-output-sink 'inferior)))
644 (t (error "Unexpected `starting' annotation")))))
646 (defun gdb-stopping (ignored)
647 "An annotation handler for `exited' and other annotations which say that I/O
648 for the subprocess is now GDB, not the program being debugged."
649 (let ((sink (gdb-get-output-sink)))
650 (cond
651 ((eq sink 'inferior)
652 (gdb-set-output-sink 'user))
653 (t (error "Unexpected stopping annotation")))))
655 (defun gdb-frame-begin (ignored)
656 (let ((sink (gdb-get-output-sink)))
657 (cond
658 ((eq sink 'inferior)
659 (gdb-set-output-sink 'user))
660 ((eq sink 'user) t)
661 ((eq sink 'emacs) t)
662 (t (error "Unexpected frame-begin annotation (%S)" sink)))))
664 (defun gdb-stopped (ignored)
665 "An annotation handler for `stopped'. It is just like gdb-stopping, except
666 that if we already set the output sink to 'user in gdb-stopping, that is fine."
667 (setq gud-running nil)
668 (let ((sink (gdb-get-output-sink)))
669 (cond
670 ((eq sink 'inferior)
671 (gdb-set-output-sink 'user))
672 ((eq sink 'user) t)
673 (t (error "Unexpected stopped annotation")))))
675 (defun gdb-post-prompt (ignored)
676 "An annotation handler for `post-prompt'. This begins the collection of
677 output from the current command if that happens to be appropriate."
678 (if (not (gdb-get-pending-triggers))
679 (progn
680 (gdb-get-current-frame)
681 (gdb-invalidate-frames)
682 (gdb-invalidate-breakpoints)
683 (gdb-invalidate-assembler)
684 (gdb-invalidate-registers)
685 (gdb-invalidate-locals)
686 (gdb-invalidate-display)
687 (gdb-invalidate-threads)))
688 (let ((sink (gdb-get-output-sink)))
689 (cond
690 ((eq sink 'user) t)
691 ((eq sink 'pre-emacs)
692 (gdb-set-output-sink 'emacs))
694 (gdb-set-output-sink 'user)
695 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
697 ;; If we get an error whilst evaluating one of the expressions
698 ;; we won't get the display-end annotation. Set the sink back to
699 ;; user to make sure that the error message is seen.
700 ;; NOT USED: see annotation-rules for reason.
701 ;(defun gdb-error-begin (ignored)
702 ; (gdb-set-output-sink 'user))
704 (defun gdb-display-begin (ignored)
705 (gdb-set-output-sink 'emacs)
706 (gdb-clear-partial-output)
707 (setq gdb-display-in-progress t))
709 (defvar gdb-expression-buffer-name)
710 (defvar gdb-display-number)
711 (defvar gdb-dive-display-number)
713 (defun gdb-display-number-end (ignored)
714 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer))
715 (setq gdb-display-number (buffer-string))
716 (setq gdb-expression-buffer-name
717 (concat "*display " gdb-display-number "*"))
718 (save-excursion
719 (if (progn
720 (set-buffer (window-buffer))
721 gdb-dive)
722 (progn
723 (let ((number gdb-display-number))
724 (switch-to-buffer
725 (set-buffer (get-buffer-create gdb-expression-buffer-name)))
726 (gdb-expressions-mode)
727 (setq gdb-dive-display-number number)))
728 (set-buffer (get-buffer-create gdb-expression-buffer-name))
729 (gdb-expressions-mode)
730 (if (and (display-graphic-p) (not gdb-dive))
731 (catch 'frame-exists
732 (dolist (frame (frame-list))
733 (if (string-equal (frame-parameter frame 'name)
734 gdb-expression-buffer-name)
735 (throw 'frame-exists nil)))
736 (make-frame `((height . ,gdb-window-height)
737 (width . ,gdb-window-width)
738 (tool-bar-lines . nil)
739 (menu-bar-lines . nil)
740 (minibuffer . nil))))
741 (gdb-display-buffer (get-buffer gdb-expression-buffer-name)))))
742 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer))
743 (setq gdb-dive nil))
745 (defvar gdb-current-frame nil)
746 (defvar gdb-nesting-level)
747 (defvar gdb-expression)
748 (defvar gdb-point)
749 (defvar gdb-annotation-arg)
751 (defun gdb-delete-line ()
752 "Delete the current line."
753 (delete-region (line-beginning-position) (line-beginning-position 2)))
755 (defun gdb-display-end (ignored)
756 (set-buffer (gdb-get-buffer 'gdb-partial-output-buffer))
757 (goto-char (point-min))
758 (search-forward ": ")
759 (looking-at "\\(.*?\\) =")
760 (let ((char "")
761 (gdb-temp-value (match-string 1)))
762 ;;move * to front of expression if necessary
763 (if (looking-at ".*\\*")
764 (progn
765 (setq char "*")
766 (setq gdb-temp-value (substring gdb-temp-value 1 nil))))
767 (with-current-buffer gdb-expression-buffer-name
768 (setq gdb-expression gdb-temp-value)
769 (if (not (string-match "::" gdb-expression))
770 (setq gdb-expression (concat char gdb-current-frame
771 "::" gdb-expression))
772 ;;else put * back on if necessary
773 (setq gdb-expression (concat char gdb-expression)))
774 (if (not header-line-format)
775 (setq header-line-format (concat "-- " gdb-expression " %-")))))
777 ;;-if scalar/string
778 (if (not (re-search-forward "##" nil t))
779 (progn
780 (with-current-buffer gdb-expression-buffer-name
781 (let ((buffer-read-only nil))
782 (delete-region (point-min) (point-max))
783 (insert-buffer-substring
784 (gdb-get-buffer 'gdb-partial-output-buffer)))))
785 ;; display expression name...
786 (goto-char (point-min))
787 (let ((start (progn (point)))
788 (end (progn (end-of-line) (point))))
789 (with-current-buffer gdb-expression-buffer-name
790 (let ((buffer-read-only nil))
791 (delete-region (point-min) (point-max))
792 (insert-buffer-substring (gdb-get-buffer
793 'gdb-partial-output-buffer)
794 start end)
795 (insert "\n"))))
796 (goto-char (point-min))
797 (re-search-forward "##" nil t)
798 (setq gdb-nesting-level 0)
799 (if (looking-at "array-section-begin")
800 (progn
801 (gdb-delete-line)
802 (setq gdb-point (point))
803 (gdb-array-format)))
804 (if (looking-at "field-begin \\(.\\)")
805 (progn
806 (setq gdb-annotation-arg (match-string 1))
807 (gdb-field-format-begin))))
808 (with-current-buffer gdb-expression-buffer-name
809 (if gdb-dive-display-number
810 (progn
811 (let ((buffer-read-only nil))
812 (goto-char (point-max))
813 (insert "\n")
814 (insert-text-button "[back]" 'type 'gdb-display-back)))))
815 (gdb-clear-partial-output)
816 (gdb-set-output-sink 'user)
817 (setq gdb-display-in-progress nil))
819 (define-button-type 'gdb-display-back
820 'help-echo (purecopy "mouse-2, RET: go back to previous display buffer")
821 'action (lambda (button) (gdb-display-go-back)))
823 (defun gdb-display-go-back ()
824 ;; delete display so they don't accumulate and delete buffer
825 (let ((number gdb-display-number))
826 (gdb-enqueue-input
827 (list (concat "server delete display " number "\n") 'ignore))
828 (switch-to-buffer (concat "*display " gdb-dive-display-number "*"))
829 (kill-buffer (get-buffer (concat "*display " number "*")))))
831 ;; prefix annotations with ## and process whole output in one chunk
832 ;; in gdb-partial-output-buffer (to allow recursion).
834 ;; array-section flags are just removed again but after counting. They
835 ;; might also be useful for arrays of structures and structures with arrays.
836 (defun gdb-array-section-begin (args)
837 (if gdb-display-in-progress
838 (progn
839 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
840 (goto-char (point-max))
841 (insert (concat "\n##array-section-begin " args "\n"))))))
843 (defun gdb-array-section-end (ignored)
844 (if gdb-display-in-progress
845 (progn
846 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
847 (goto-char (point-max))
848 (insert "\n##array-section-end\n")))))
850 (defun gdb-field-begin (args)
851 (if gdb-display-in-progress
852 (progn
853 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
854 (goto-char (point-max))
855 (insert (concat "\n##field-begin " args "\n"))))))
857 (defun gdb-field-end (ignored)
858 (if gdb-display-in-progress
859 (progn
860 (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
861 (goto-char (point-max))
862 (insert "\n##field-end\n")))))
864 (defun gdb-elt (ignored)
865 (if gdb-display-in-progress
866 (progn
867 (goto-char (point-max))
868 (insert "\n##elt\n"))))
870 (defun gdb-field-format-begin ()
871 ;; get rid of ##field-begin
872 (gdb-delete-line)
873 (gdb-insert-field)
874 (setq gdb-nesting-level (+ gdb-nesting-level 1))
875 (while (re-search-forward "##" nil t)
876 ;; keep making recursive calls...
877 (if (looking-at "field-begin \\(.\\)")
878 (progn
879 (setq gdb-annotation-arg (match-string 1))
880 (gdb-field-format-begin)))
881 ;; until field-end.
882 (if (looking-at "field-end") (gdb-field-format-end))))
884 (defun gdb-field-format-end ()
885 ;; get rid of ##field-end and `,' or `}'
886 (gdb-delete-line)
887 (gdb-delete-line)
888 (setq gdb-nesting-level (- gdb-nesting-level 1)))
890 (defvar gdb-dive-map
891 (let ((map (make-sparse-keymap)))
892 (define-key map [mouse-2] 'gdb-dive)
893 (define-key map [S-mouse-2] 'gdb-dive-new-frame)
894 map))
896 (defun gdb-dive (event)
897 "Dive into structure."
898 (interactive "e")
899 (setq gdb-dive t)
900 (gdb-dive-new-frame event))
902 (defun gdb-dive-new-frame (event)
903 "Dive into structure and display in a new frame."
904 (interactive "e")
905 (save-excursion
906 (mouse-set-point event)
907 (let ((point (point)) (gdb-full-expression gdb-expression)
908 (end (progn (end-of-line) (point)))
909 (gdb-part-expression "") (gdb-last-field nil) (gdb-display-char nil))
910 (beginning-of-line)
911 (if (looking-at "\*") (setq gdb-display-char "*"))
912 (re-search-forward "\\(\\S-+\\) = " end t)
913 (setq gdb-last-field (match-string-no-properties 1))
914 (goto-char (match-beginning 1))
915 (let ((last-column (current-column)))
916 (while (re-search-backward "\\s-\\(\\S-+\\) = {" nil t)
917 (goto-char (match-beginning 1))
918 (if (and (< (current-column) last-column)
919 (> (count-lines 1 (point)) 1))
920 (progn
921 (setq gdb-part-expression
922 (concat "." (match-string-no-properties 1)
923 gdb-part-expression))
924 (setq last-column (current-column))))))
925 ;; * not needed for components of a pointer to a structure in gdb
926 (if (string-equal "*" (substring gdb-full-expression 0 1))
927 (setq gdb-full-expression (substring gdb-full-expression 1 nil)))
928 (setq gdb-full-expression
929 (concat gdb-full-expression gdb-part-expression "." gdb-last-field))
930 (gdb-enqueue-input
931 (list (concat "server display" gdb-display-char
932 " " gdb-full-expression "\n")
933 'ignore)))))
935 (defun gdb-insert-field ()
936 (let ((start (progn (point)))
937 (end (progn (next-line) (point)))
938 (num 0))
939 (with-current-buffer gdb-expression-buffer-name
940 (let ((buffer-read-only nil))
941 (if (string-equal gdb-annotation-arg "\*") (insert "\*"))
942 (while (<= num gdb-nesting-level)
943 (insert "\t")
944 (setq num (+ num 1)))
945 (insert-buffer-substring (gdb-get-buffer
946 'gdb-partial-output-buffer)
947 start end)
948 (put-text-property (- (point) (- end start)) (- (point) 1)
949 'mouse-face 'highlight)
950 (put-text-property (- (point) (- end start)) (- (point) 1)
951 'local-map gdb-dive-map)))
952 (delete-region start end)))
954 (defvar gdb-values)
956 (defun gdb-array-format ()
957 (while (re-search-forward "##" nil t)
958 ;; keep making recursive calls...
959 (if (looking-at "array-section-begin")
960 (progn
961 ;;get rid of ##array-section-begin
962 (gdb-delete-line)
963 (setq gdb-nesting-level (+ gdb-nesting-level 1))
964 (gdb-array-format)))
965 ;;until *matching* array-section-end is found
966 (if (looking-at "array-section-end")
967 (if (eq gdb-nesting-level 0)
968 (progn
969 (let ((values (buffer-substring gdb-point (- (point) 2))))
970 (with-current-buffer gdb-expression-buffer-name
971 (setq gdb-values
972 (concat "{" (replace-regexp-in-string "\n" "" values)
973 "}"))
974 (gdb-array-format1))))
975 ;;else get rid of ##array-section-end etc
976 (gdb-delete-line)
977 (setq gdb-nesting-level (- gdb-nesting-level 1))
978 (gdb-array-format)))))
980 (defvar gdb-array-start)
981 (defvar gdb-array-stop)
983 (defvar gdb-array-slice-map
984 (let ((map (make-sparse-keymap)))
985 (define-key map [mouse-2] 'gdb-array-slice)
986 map))
988 (defun gdb-array-slice (event)
989 "Select an array slice to display."
990 (interactive "e")
991 (mouse-set-point event)
992 (save-excursion
993 (let ((n -1) (stop 0) (start 0) (point (point)))
994 (beginning-of-line)
995 (while (search-forward "[" point t)
996 (setq n (+ n 1)))
997 (setq start (string-to-int (read-string "Start index: ")))
998 (aset gdb-array-start n start)
999 (setq stop (string-to-int (read-string "Stop index: ")))
1000 (aset gdb-array-stop n stop)))
1001 (gdb-array-format1))
1003 (defvar gdb-display-string)
1004 (defvar gdb-array-size)
1006 (defun gdb-array-format1 ()
1007 (setq gdb-display-string "")
1008 (let ((buffer-read-only nil))
1009 (delete-region (point-min) (point-max))
1010 (let ((gdb-value-list (split-string gdb-values ", ")))
1011 (string-match "\\({+\\)" (car gdb-value-list))
1012 (let* ((depth (- (match-end 1) (match-beginning 1)))
1013 (indices (make-vector depth '0))
1014 (index 0) (num 0) (array-start "")
1015 (array-stop "") (array-slice "") (array-range nil)
1016 (flag t) (indices-string ""))
1017 (dolist (gdb-value gdb-value-list)
1018 (string-match "{*\\([^}]*\\)\\(}*\\)" gdb-value)
1019 (setq num 0)
1020 (while (< num depth)
1021 (setq indices-string
1022 (concat indices-string
1023 "[" (int-to-string (aref indices num)) "]"))
1024 (if (not (= (aref gdb-array-start num) -1))
1025 (if (or (< (aref indices num) (aref gdb-array-start num))
1026 (> (aref indices num) (aref gdb-array-stop num)))
1027 (setq flag nil))
1028 (aset gdb-array-size num (aref indices num)))
1029 (setq num (+ num 1)))
1030 (if flag
1031 (let ((gdb-display-value (match-string 1 gdb-value)))
1032 (setq gdb-display-string (concat gdb-display-string " "
1033 gdb-display-value))
1034 (insert
1035 (concat indices-string "\t" gdb-display-value "\n"))))
1036 (setq indices-string "")
1037 (setq flag t)
1038 ;; 0<= index < depth, start at right : (- depth 1)
1039 (setq index (- (- depth 1)
1040 (- (match-end 2) (match-beginning 2))))
1041 ;;don't set for very last brackets
1042 (when (>= index 0)
1043 (aset indices index (+ 1 (aref indices index)))
1044 (setq num (+ 1 index))
1045 (while (< num depth)
1046 (aset indices num 0)
1047 (setq num (+ num 1)))))
1048 (setq num 0)
1049 (while (< num depth)
1050 (if (= (aref gdb-array-start num) -1)
1051 (progn
1052 (aset gdb-array-start num 0)
1053 (aset gdb-array-stop num (aref indices num))))
1054 (setq array-start (int-to-string (aref gdb-array-start num)))
1055 (setq array-stop (int-to-string (aref gdb-array-stop num)))
1056 (setq array-range (concat "[" array-start
1057 ":" array-stop "]"))
1058 (put-text-property 1 (+ (length array-start)
1059 (length array-stop) 2)
1060 'mouse-face 'highlight array-range)
1061 (put-text-property 1 (+ (length array-start)
1062 (length array-stop) 2)
1063 'local-map gdb-array-slice-map array-range)
1064 (goto-char (point-min))
1065 (setq array-slice (concat array-slice array-range))
1066 (setq num (+ num 1)))
1067 (goto-char (point-min))
1068 (insert "Array Size : ")
1069 (setq num 0)
1070 (while (< num depth)
1071 (insert
1072 (concat "["
1073 (int-to-string (+ (aref gdb-array-size num) 1)) "]"))
1074 (setq num (+ num 1)))
1075 (insert
1076 (concat "\n Slice : " array-slice "\n\nIndex\tValues\n\n"))))))
1078 (defun gud-gdba-marker-filter (string)
1079 "A gud marker filter for gdb. Handle a burst of output from GDB."
1080 (let (
1081 ;; Recall the left over burst from last time
1082 (burst (concat (gdb-get-burst) string))
1083 ;; Start accumulating output for the GUD buffer
1084 (output ""))
1086 ;; Process all the complete markers in this chunk.
1087 (while (string-match "\n\032\032\\(.*\\)\n" burst)
1088 (let ((annotation (match-string 1 burst)))
1090 ;; Stuff prior to the match is just ordinary output.
1091 ;; It is either concatenated to OUTPUT or directed
1092 ;; elsewhere.
1093 (setq output
1094 (gdb-concat-output
1095 output
1096 (substring burst 0 (match-beginning 0))))
1098 ;; Take that stuff off the burst.
1099 (setq burst (substring burst (match-end 0)))
1101 ;; Parse the tag from the annotation, and maybe its arguments.
1102 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1103 (let* ((annotation-type (match-string 1 annotation))
1104 (annotation-arguments (match-string 2 annotation))
1105 (annotation-rule (assoc annotation-type
1106 gdb-annotation-rules)))
1107 ;; Call the handler for this annotation.
1108 (if annotation-rule
1109 (funcall (car (cdr annotation-rule))
1110 annotation-arguments)
1111 ;; Else the annotation is not recognized. Ignore it silently,
1112 ;; so that GDB can add new annotations without causing
1113 ;; us to blow up.
1114 ))))
1116 ;; Does the remaining text end in a partial line?
1117 ;; If it does, then keep part of the burst until we get more.
1118 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1119 burst)
1120 (progn
1121 ;; Everything before the potential marker start can be output.
1122 (setq output
1123 (gdb-concat-output output
1124 (substring burst 0 (match-beginning 0))))
1126 ;; Everything after, we save, to combine with later input.
1127 (setq burst (substring burst (match-beginning 0))))
1129 ;; In case we know the burst contains no partial annotations:
1130 (progn
1131 (setq output (gdb-concat-output output burst))
1132 (setq burst "")))
1134 ;; Save the remaining burst for the next call to this function.
1135 (gdb-set-burst burst)
1136 output))
1138 (defun gdb-concat-output (so-far new)
1139 (let ((sink (gdb-get-output-sink )))
1140 (cond
1141 ((eq sink 'user) (concat so-far new))
1142 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1143 ((eq sink 'emacs)
1144 (gdb-append-to-partial-output new)
1145 so-far)
1146 ((eq sink 'inferior)
1147 (gdb-append-to-inferior-io new)
1148 so-far)
1149 (t (error "Bogon output sink %S" sink)))))
1151 (defun gdb-append-to-partial-output (string)
1152 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1153 (goto-char (point-max))
1154 (insert string)))
1156 (defun gdb-clear-partial-output ()
1157 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
1158 (delete-region (point-min) (point-max))))
1160 (defun gdb-append-to-inferior-io (string)
1161 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1162 (goto-char (point-max))
1163 (insert-before-markers string))
1164 (if (not (string-equal string ""))
1165 (select-window
1166 (gdb-display-buffer (gdb-get-create-buffer 'gdb-inferior-io)))))
1168 (defun gdb-clear-inferior-io ()
1169 (with-current-buffer (gdb-get-create-buffer 'gdb-inferior-io)
1170 (delete-region (point-min) (point-max))))
1173 ;; One trick is to have a command who's output is always available in a buffer
1174 ;; of it's own, and is always up to date. We build several buffers of this
1175 ;; type.
1177 ;; There are two aspects to this: gdb has to tell us when the output for that
1178 ;; command might have changed, and we have to be able to run the command
1179 ;; behind the user's back.
1181 ;; The idle input queue and the output phasing associated with the variable
1182 ;; gdb-output-sink help us to run commands behind the user's back.
1184 ;; Below is the code for specificly managing buffers of output from one
1185 ;; command.
1188 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1189 ;; It adds an idle input for the command we are tracking. It should be the
1190 ;; annotation rule binding of whatever gdb sends to tell us this command
1191 ;; might have changed it's output.
1193 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1194 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1195 ;; input in the input queue (see comment about ``gdb communications'' above).
1197 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1198 output-handler)
1199 `(defun ,name (&optional ignored)
1200 (if (and (,demand-predicate)
1201 (not (member ',name
1202 (gdb-get-pending-triggers))))
1203 (progn
1204 (gdb-enqueue-idle-input
1205 (list ,gdb-command ',output-handler))
1206 (gdb-set-pending-triggers
1207 (cons ',name
1208 (gdb-get-pending-triggers)))))))
1210 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1211 `(defun ,name ()
1212 (gdb-set-pending-triggers
1213 (delq ',trigger
1214 (gdb-get-pending-triggers)))
1215 (let ((buf (gdb-get-buffer ',buf-key)))
1216 (and buf
1217 (with-current-buffer buf
1218 (let ((p (point))
1219 (buffer-read-only nil))
1220 (delete-region (point-min) (point-max))
1221 (insert-buffer-substring (gdb-get-create-buffer
1222 'gdb-partial-output-buffer))
1223 (goto-char p)))))
1224 ;; put customisation here
1225 (,custom-defun)))
1227 (defmacro def-gdb-auto-updated-buffer (buffer-key trigger-name gdb-command
1228 output-handler-name custom-defun)
1229 `(progn
1230 (def-gdb-auto-update-trigger ,trigger-name
1231 ;; The demand predicate:
1232 (lambda () (gdb-get-buffer ',buffer-key))
1233 ,gdb-command
1234 ,output-handler-name)
1235 (def-gdb-auto-update-handler ,output-handler-name
1236 ,trigger-name ,buffer-key ,custom-defun)))
1240 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1242 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1243 'gdb-breakpoints-buffer-name
1244 'gdb-breakpoints-mode)
1246 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1247 ;; This defines the auto update rule for buffers of type
1248 ;; `gdb-breakpoints-buffer'.
1250 ;; It defines a function to serve as the annotation handler that
1251 ;; handles the `foo-invalidated' message. That function is called:
1252 gdb-invalidate-breakpoints
1254 ;; To update the buffer, this command is sent to gdb.
1255 "server info breakpoints\n"
1257 ;; This also defines a function to be the handler for the output
1258 ;; from the command above. That function will copy the output into
1259 ;; the appropriately typed buffer. That function will be called:
1260 gdb-info-breakpoints-handler
1261 ;; buffer specific functions
1262 gdb-info-breakpoints-custom)
1264 (defvar gdb-cdir nil "Compilation directory.")
1266 (defconst breakpoint-xpm-data "/* XPM */
1267 static char *magick[] = {
1268 /* columns rows colors chars-per-pixel */
1269 \"12 12 2 1\",
1270 \" c red\",
1271 \"+ c None\",
1272 /* pixels */
1273 \"++++++++++++\",
1274 \"+++ +++\",
1275 \"++ ++\",
1276 \"+ +\",
1277 \"+ +\",
1278 \"+ +\",
1279 \"+ +\",
1280 \"+ +\",
1281 \"+ +\",
1282 \"++ ++\",
1283 \"+++ +++\",
1284 \"++++++++++++\"
1286 "XPM data used for breakpoint icon.")
1288 (defconst breakpoint-enabled-pbm-data
1290 12 12\",
1291 0 0 0 0 0 0 0 0 0 0 0 0
1292 0 0 0 1 1 1 1 1 1 0 0 0
1293 0 0 1 1 1 1 1 1 1 1 0 0
1294 0 1 1 1 1 1 1 1 1 1 1 0
1295 0 1 1 1 1 1 1 1 1 1 1 0
1296 0 1 1 1 1 1 1 1 1 1 1 0
1297 0 1 1 1 1 1 1 1 1 1 1 0
1298 0 1 1 1 1 1 1 1 1 1 1 0
1299 0 1 1 1 1 1 1 1 1 1 1 0
1300 0 0 1 1 1 1 1 1 1 1 0 0
1301 0 0 0 1 1 1 1 1 1 0 0 0
1302 0 0 0 0 0 0 0 0 0 0 0 0"
1303 "PBM data used for enabled breakpoint icon.")
1305 (defconst breakpoint-disabled-pbm-data
1307 12 12\",
1308 0 0 0 0 0 0 0 0 0 0 0 0
1309 0 0 0 1 0 1 0 1 0 0 0 0
1310 0 0 1 0 1 0 1 0 1 0 0 0
1311 0 1 0 1 0 1 0 1 0 1 0 0
1312 0 0 1 0 1 0 1 0 1 0 1 0
1313 0 1 0 1 0 1 0 1 0 1 0 0
1314 0 0 1 0 1 0 1 0 1 0 1 0
1315 0 1 0 1 0 1 0 1 0 1 0 0
1316 0 0 1 0 1 0 1 0 1 0 1 0
1317 0 0 0 1 0 1 0 1 0 1 0 0
1318 0 0 0 0 1 0 1 0 1 0 0 0
1319 0 0 0 0 0 0 0 0 0 0 0 0"
1320 "PBM data used for disabled breakpoint icon.")
1322 (defvar breakpoint-enabled-icon
1323 (find-image `((:type xpm :data ,breakpoint-xpm-data)
1324 (:type pbm :data ,breakpoint-enabled-pbm-data)))
1325 "Icon for enabled breakpoint in display margin")
1327 (defvar breakpoint-disabled-icon
1328 (find-image `((:type xpm :data ,breakpoint-xpm-data :conversion disabled)
1329 (:type pbm :data ,breakpoint-disabled-pbm-data)))
1330 "Icon for disabled breakpoint in display margin")
1332 ;;-put breakpoint icons in relevant margins (even those set in the GUD buffer)
1333 (defun gdb-info-breakpoints-custom ()
1334 (let ((flag)(address))
1336 ;; remove all breakpoint-icons in source buffers but not assembler buffer
1337 (dolist (buffer (buffer-list))
1338 (with-current-buffer buffer
1339 (if (and (eq gud-minor-mode 'gdba)
1340 (not (string-match "^\*" (buffer-name))))
1341 (if (eq window-system 'x)
1342 (remove-images (point-min) (point-max))
1343 (gdb-remove-strings (point-min) (point-max))))))
1344 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1345 (save-excursion
1346 (goto-char (point-min))
1347 (while (< (point) (- (point-max) 1))
1348 (forward-line 1)
1349 (if (looking-at "[^\t].*breakpoint")
1350 (progn
1351 (looking-at "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)")
1352 (setq flag (char-after (match-beginning 1)))
1353 (beginning-of-line)
1354 (if (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
1355 (progn
1356 (looking-at "\\(\\S-*\\):\\([0-9]+\\)")
1357 (let ((line (match-string 2)) (buffer-read-only nil)
1358 (file (match-string 1)))
1359 (put-text-property (progn (beginning-of-line) (point))
1360 (progn (end-of-line) (point))
1361 'mouse-face 'highlight)
1362 (with-current-buffer
1363 (find-file-noselect
1364 (if (file-exists-p file) file
1365 (expand-file-name file gdb-cdir)))
1366 (save-current-buffer
1367 (set (make-local-variable 'gud-minor-mode) 'gdba)
1368 (set (make-local-variable 'tool-bar-map)
1369 gud-tool-bar-map)
1370 (setq left-margin-width 2)
1371 (if (get-buffer-window (current-buffer))
1372 (set-window-margins (get-buffer-window
1373 (current-buffer))
1374 left-margin-width
1375 right-margin-width)))
1376 ;; only want one breakpoint icon at each location
1377 (save-excursion
1378 (goto-line (string-to-number line))
1379 (let ((start (progn (beginning-of-line)
1380 (- (point) 1)))
1381 (end (progn (end-of-line) (+ (point) 1))))
1382 (if (eq window-system 'x)
1383 (progn
1384 (remove-images start end)
1385 (if (eq ?y flag)
1386 (put-image breakpoint-enabled-icon
1387 (+ start 1)
1388 "breakpoint icon enabled"
1389 'left-margin)
1390 (put-image breakpoint-disabled-icon
1391 (+ start 1)
1392 "breakpoint icon disabled"
1393 'left-margin)))
1394 (gdb-remove-strings start end)
1395 (if (eq ?y flag)
1396 (gdb-put-string "B" (+ start 1))
1397 (gdb-put-string "b" (+ start 1))))))))))))
1398 (end-of-line))))))
1400 (defun gdb-breakpoints-buffer-name ()
1401 (with-current-buffer gud-comint-buffer
1402 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1404 (defun gdb-display-breakpoints-buffer ()
1405 (interactive)
1406 (gdb-display-buffer
1407 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1409 (defun gdb-frame-breakpoints-buffer ()
1410 (interactive)
1411 (switch-to-buffer-other-frame
1412 (gdb-get-create-buffer 'gdb-breakpoints-buffer)))
1414 (defvar gdb-breakpoints-mode-map
1415 (let ((map (make-sparse-keymap))
1416 (menu (make-sparse-keymap "Breakpoints")))
1417 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1418 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1419 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1421 (suppress-keymap map)
1422 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1423 (define-key map " " 'gdb-toggle-breakpoint)
1424 (define-key map "d" 'gdb-delete-breakpoint)
1425 (define-key map "\r" 'gdb-goto-breakpoint)
1426 (define-key map [mouse-2] 'gdb-mouse-goto-breakpoint)
1427 map))
1429 (defun gdb-breakpoints-mode ()
1430 "Major mode for gdb breakpoints.
1432 \\{gdb-breakpoints-mode-map}"
1433 (setq major-mode 'gdb-breakpoints-mode)
1434 (setq mode-name "Breakpoints")
1435 (use-local-map gdb-breakpoints-mode-map)
1436 (setq buffer-read-only t)
1437 (gdb-invalidate-breakpoints))
1439 (defun gdb-toggle-breakpoint ()
1440 "Enable/disable the breakpoint at current line."
1441 (interactive)
1442 (save-excursion
1443 (beginning-of-line 1)
1444 (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
1445 (error "Not recognized as break/watchpoint line")
1446 (gdb-enqueue-input
1447 (list
1448 (concat
1449 (if (eq ?y (char-after (match-beginning 2)))
1450 "server disable "
1451 "server enable ")
1452 (match-string 1) "\n")
1453 'ignore)))))
1455 (defun gdb-delete-breakpoint ()
1456 "Delete the breakpoint at current line."
1457 (interactive)
1458 (beginning-of-line 1)
1459 (if (not (looking-at "\\([0-9]+\\).*point\\s-*\\S-*\\s-*\\(.\\)"))
1460 (error "Not recognized as break/watchpoint line")
1461 (gdb-enqueue-input
1462 (list (concat "server delete " (match-string 1) "\n") 'ignore))))
1464 (defvar gdb-source-window nil)
1466 (defun gdb-goto-breakpoint ()
1467 "Display the file in the source buffer at the breakpoint specified on the
1468 current line."
1469 (interactive)
1470 (save-excursion
1471 (beginning-of-line 1)
1472 (re-search-forward "in\\s-+\\S-+\\s-+at\\s-+" nil t)
1473 (looking-at "\\(\\S-*\\):\\([0-9]+\\)"))
1474 (if (match-string 2)
1475 (let ((line (match-string 2))
1476 (file (match-string 1)))
1477 (save-selected-window
1478 (select-window gdb-source-window)
1479 (switch-to-buffer (find-file-noselect
1480 (if (file-exists-p file)
1481 file
1482 (expand-file-name file gdb-cdir))))
1483 (goto-line (string-to-number line))))))
1484 ;; I'll get this to work one day!
1485 ;; (defun gdb-goto-breakpoint ()
1486 ;; "Display the file in the source buffer at the breakpoint specified on the
1487 ;; current line."
1488 ;; (interactive)
1489 ;; (save-excursion
1490 ;; (let ((eol (progn (end-of-line) (point))))
1491 ;; (beginning-of-line 1)
1492 ;; (if (re-search-forward "\\(\\S-*\\):\\([0-9]+\\)" eol t)
1493 ;; (let ((line (match-string 2))
1494 ;; (file (match-string 1)))
1495 ;; (save-selected-window
1496 ;; (select-window gdb-source-window)
1497 ;; (switch-to-buffer (find-file-noselect
1498 ;; (if (file-exists-p file)
1499 ;; file
1500 ;; (expand-file-name file gdb-cdir))))
1501 ;; (goto-line (string-to-number line))))))
1502 ;; (let ((eol (progn (end-of-line) (point))))
1503 ;; (beginning-of-line 1)
1504 ;; (if (re-search-forward "<\\(\\S-*?\\)\\(\\+*[0-9]*\\)>" eol t)
1505 ;; (save-selected-window
1506 ;; (select-window gdb-source-window)
1507 ;; (gdb-get-create-buffer 'gdb-assembler-buffer)
1508 ;; (gdb-enqueue-input
1509 ;; (list (concat "server disassemble " (match-string 1) "\n")
1510 ;; 'gdb-assembler-handler))
1511 ;; (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
1512 ;; (re-search-forward
1513 ;; (concat (match-string 1) (match-string 2)))))))))
1515 (defun gdb-mouse-goto-breakpoint (event)
1516 "Display the file in the source buffer at the selected breakpoint."
1517 (interactive "e")
1518 (mouse-set-point event)
1519 (gdb-goto-breakpoint))
1522 ;; Frames buffer. This displays a perpetually correct bactracktrace
1523 ;; (from the command `where').
1525 ;; Alas, if your stack is deep, it is costly.
1527 (gdb-set-buffer-rules 'gdb-stack-buffer
1528 'gdb-stack-buffer-name
1529 'gdb-frames-mode)
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)
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 (goto-char (point-min))
1542 (while (< (point) (point-max))
1543 (put-text-property (progn (beginning-of-line) (point))
1544 (progn (end-of-line) (point))
1545 'mouse-face 'highlight)
1546 (beginning-of-line)
1547 (if (or (looking-at "^#[0-9]*\\s-*\\S-* in \\(\\S-*\\)")
1548 (looking-at "^#[0-9]*\\s-*\\(\\S-*\\)"))
1549 (if (equal (match-string 1) gdb-current-frame)
1550 (put-text-property (progn (beginning-of-line) (point))
1551 (progn (end-of-line) (point))
1552 'face
1553 `(:background ,(face-attribute 'default :foreground)
1554 :foreground ,(face-attribute 'default :background)))))
1555 (forward-line 1))))))
1557 (defun gdb-stack-buffer-name ()
1558 (with-current-buffer gud-comint-buffer
1559 (concat "*stack frames of " (gdb-get-target-string) "*")))
1561 (defun gdb-display-stack-buffer ()
1562 (interactive)
1563 (gdb-display-buffer
1564 (gdb-get-create-buffer 'gdb-stack-buffer)))
1566 (defun gdb-frame-stack-buffer ()
1567 (interactive)
1568 (switch-to-buffer-other-frame
1569 (gdb-get-create-buffer 'gdb-stack-buffer)))
1571 (defvar gdb-frames-mode-map
1572 (let ((map (make-sparse-keymap)))
1573 (suppress-keymap map)
1574 (define-key map "\r" 'gdb-frames-select)
1575 (define-key map [mouse-2] 'gdb-frames-mouse-select)
1576 map))
1578 (defun gdb-frames-mode ()
1579 "Major mode for gdb frames.
1581 \\{gdb-frames-mode-map}"
1582 (setq major-mode 'gdb-frames-mode)
1583 (setq mode-name "Frames")
1584 (setq buffer-read-only t)
1585 (use-local-map gdb-frames-mode-map)
1586 (font-lock-mode -1)
1587 (gdb-invalidate-frames))
1589 (defun gdb-get-frame-number ()
1590 (save-excursion
1591 (let* ((pos (re-search-backward "^#\\([0-9]*\\)" nil t))
1592 (n (or (and pos (match-string-no-properties 1)) "0")))
1593 n)))
1595 (defun gdb-frames-select ()
1596 "Make the frame on the current line become the current frame and display the
1597 source in the source buffer."
1598 (interactive)
1599 (gdb-enqueue-input
1600 (list (concat "server frame " (gdb-get-frame-number) "\n") 'ignore))
1601 (gud-display-frame))
1603 (defun gdb-frames-mouse-select (event)
1604 "Make the selected frame become the current frame and display the source in
1605 the source buffer."
1606 (interactive "e")
1607 (mouse-set-point event)
1608 (gdb-frames-select))
1611 ;; Threads buffer. This displays a selectable thread list.
1613 (gdb-set-buffer-rules 'gdb-threads-buffer
1614 'gdb-threads-buffer-name
1615 'gdb-threads-mode)
1617 (def-gdb-auto-updated-buffer gdb-threads-buffer
1618 gdb-invalidate-threads
1619 "info threads\n"
1620 gdb-info-threads-handler
1621 gdb-info-threads-custom)
1623 (defun gdb-info-threads-custom ()
1624 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
1625 (let ((buffer-read-only nil))
1626 (goto-char (point-min))
1627 (while (< (point) (point-max))
1628 (put-text-property (progn (beginning-of-line) (point))
1629 (progn (end-of-line) (point))
1630 'mouse-face 'highlight)
1631 (forward-line 1)))))
1633 (defun gdb-threads-buffer-name ()
1634 (with-current-buffer gud-comint-buffer
1635 (concat "*threads of " (gdb-get-target-string) "*")))
1637 (defun gdb-display-threads-buffer ()
1638 (interactive)
1639 (gdb-display-buffer
1640 (gdb-get-create-buffer 'gdb-threads-buffer)))
1642 (defun gdb-frame-threads-buffer ()
1643 (interactive)
1644 (switch-to-buffer-other-frame
1645 (gdb-get-create-buffer 'gdb-threads-buffer)))
1647 (defvar gdb-threads-mode-map
1648 (let ((map (make-sparse-keymap)))
1649 (suppress-keymap map)
1650 (define-key map "\r" 'gdb-threads-select)
1651 (define-key map [mouse-2] 'gdb-threads-mouse-select)
1652 map))
1654 (defun gdb-threads-mode ()
1655 "Major mode for gdb frames.
1657 \\{gdb-frames-mode-map}"
1658 (setq major-mode 'gdb-threads-mode)
1659 (setq mode-name "Threads")
1660 (setq buffer-read-only t)
1661 (use-local-map gdb-threads-mode-map)
1662 (gdb-invalidate-threads))
1664 (defun gdb-get-thread-number ()
1665 (save-excursion
1666 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
1667 (match-string-no-properties 1)))
1670 (defun gdb-threads-select ()
1671 "Make the thread on the current line become the current thread and display the
1672 source in the source buffer."
1673 (interactive)
1674 (gdb-enqueue-input
1675 (list (concat "thread " (gdb-get-thread-number) "\n") 'ignore))
1676 (gud-display-frame))
1678 (defun gdb-threads-mouse-select (event)
1679 "Make the selected frame become the current frame and display the source in
1680 the source buffer."
1681 (interactive "e")
1682 (mouse-set-point event)
1683 (gdb-threads-select))
1686 ;; Registers buffer.
1688 (gdb-set-buffer-rules 'gdb-registers-buffer
1689 'gdb-registers-buffer-name
1690 'gdb-registers-mode)
1692 (def-gdb-auto-updated-buffer gdb-registers-buffer
1693 gdb-invalidate-registers
1694 "server info registers\n"
1695 gdb-info-registers-handler
1696 gdb-info-registers-custom)
1698 (defun gdb-info-registers-custom ())
1700 (defvar gdb-registers-mode-map
1701 (let ((map (make-sparse-keymap)))
1702 (suppress-keymap map)
1703 map))
1705 (defun gdb-registers-mode ()
1706 "Major mode for gdb registers.
1708 \\{gdb-registers-mode-map}"
1709 (setq major-mode 'gdb-registers-mode)
1710 (setq mode-name "Registers")
1711 (setq buffer-read-only t)
1712 (use-local-map gdb-registers-mode-map)
1713 (gdb-invalidate-registers))
1715 (defun gdb-registers-buffer-name ()
1716 (with-current-buffer gud-comint-buffer
1717 (concat "*registers of " (gdb-get-target-string) "*")))
1719 (defun gdb-display-registers-buffer ()
1720 (interactive)
1721 (gdb-display-buffer
1722 (gdb-get-create-buffer 'gdb-registers-buffer)))
1724 (defun gdb-frame-registers-buffer ()
1725 (interactive)
1726 (switch-to-buffer-other-frame
1727 (gdb-get-create-buffer 'gdb-registers-buffer)))
1730 ;; Locals buffer.
1732 (gdb-set-buffer-rules 'gdb-locals-buffer
1733 'gdb-locals-buffer-name
1734 'gdb-locals-mode)
1736 (def-gdb-auto-updated-buffer gdb-locals-buffer
1737 gdb-invalidate-locals
1738 "server info locals\n"
1739 gdb-info-locals-handler
1740 gdb-info-locals-custom)
1742 ;; Abbreviate for arrays and structures.
1743 ;; These can be expanded using gud-display.
1744 (defun gdb-info-locals-handler nil
1745 (gdb-set-pending-triggers (delq 'gdb-invalidate-locals
1746 (gdb-get-pending-triggers)))
1747 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
1748 (with-current-buffer buf
1749 (goto-char (point-min))
1750 (while (re-search-forward "^ .*\n" nil t)
1751 (replace-match "" nil nil))
1752 (goto-char (point-min))
1753 (while (re-search-forward "{[-0-9, {}\]*\n" nil t)
1754 (replace-match "(array);\n" nil nil))
1755 (goto-char (point-min))
1756 (while (re-search-forward "{.*=.*\n" nil t)
1757 (replace-match "(structure);\n" nil nil))))
1758 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
1759 (and buf (with-current-buffer buf
1760 (let ((p (point))
1761 (buffer-read-only nil))
1762 (delete-region (point-min) (point-max))
1763 (insert-buffer-substring (gdb-get-create-buffer
1764 'gdb-partial-output-buffer))
1765 (goto-char p)))))
1766 (run-hooks 'gdb-info-locals-hook))
1768 (defun gdb-info-locals-custom ()
1769 nil)
1771 (defvar gdb-locals-mode-map
1772 (let ((map (make-sparse-keymap)))
1773 (suppress-keymap map)
1774 map))
1776 (defun gdb-locals-mode ()
1777 "Major mode for gdb locals.
1779 \\{gdb-locals-mode-map}"
1780 (setq major-mode 'gdb-locals-mode)
1781 (setq mode-name "Locals")
1782 (setq buffer-read-only t)
1783 (use-local-map gdb-locals-mode-map)
1784 (gdb-invalidate-locals))
1786 (defun gdb-locals-buffer-name ()
1787 (with-current-buffer gud-comint-buffer
1788 (concat "*locals of " (gdb-get-target-string) "*")))
1790 (defun gdb-display-locals-buffer ()
1791 (interactive)
1792 (gdb-display-buffer
1793 (gdb-get-create-buffer 'gdb-locals-buffer)))
1795 (defun gdb-frame-locals-buffer ()
1796 (interactive)
1797 (switch-to-buffer-other-frame
1798 (gdb-get-create-buffer 'gdb-locals-buffer)))
1801 ;; Display expression buffer.
1803 (gdb-set-buffer-rules 'gdb-display-buffer
1804 'gdb-display-buffer-name
1805 'gdb-display-mode)
1807 (def-gdb-auto-updated-buffer gdb-display-buffer
1808 ;; `gdb-display-buffer'.
1809 gdb-invalidate-display
1810 "server info display\n"
1811 gdb-info-display-handler
1812 gdb-info-display-custom)
1814 (defun gdb-info-display-custom ()
1815 (let ((display-list nil))
1816 (with-current-buffer (gdb-get-buffer 'gdb-display-buffer)
1817 (goto-char (point-min))
1818 (while (< (point) (- (point-max) 1))
1819 (forward-line 1)
1820 (if (looking-at "\\([0-9]+\\): \\([ny]\\)")
1821 (setq display-list
1822 (cons (string-to-int (match-string 1)) display-list)))
1823 (end-of-line)))
1824 (if (not (display-graphic-p))
1825 (progn
1826 (dolist (buffer (buffer-list))
1827 (if (string-match "\\*display \\([0-9]+\\)\\*" (buffer-name buffer))
1828 (progn
1829 (let ((number
1830 (match-string 1 (buffer-name buffer))))
1831 (if (not (memq (string-to-int number) display-list))
1832 (kill-buffer
1833 (get-buffer (concat "*display " number "*")))))))))
1834 (gdb-delete-frames display-list))))
1836 (defun gdb-delete-frames (display-list)
1837 (dolist (frame (frame-list))
1838 (let ((frame-name (frame-parameter frame 'name)))
1839 (if (string-match "\\*display \\([0-9]+\\)\\*" frame-name)
1840 (progn
1841 (let ((number (match-string 1 frame-name)))
1842 (if (not (memq (string-to-int number) display-list))
1843 (progn (kill-buffer
1844 (get-buffer (concat "*display " number "*")))
1845 (delete-frame frame)))))))))
1847 (defvar gdb-display-mode-map
1848 (let ((map (make-sparse-keymap))
1849 (menu (make-sparse-keymap "Display")))
1850 (define-key menu [toggle] '("Toggle" . gdb-toggle-display))
1851 (define-key menu [delete] '("Delete" . gdb-delete-display))
1853 (suppress-keymap map)
1854 (define-key map [menu-bar display] (cons "Display" menu))
1855 (define-key map " " 'gdb-toggle-display)
1856 (define-key map "d" 'gdb-delete-display)
1857 map))
1859 (defun gdb-display-mode ()
1860 "Major mode for gdb display.
1862 \\{gdb-display-mode-map}"
1863 (setq major-mode 'gdb-display-mode)
1864 (setq mode-name "Display")
1865 (setq buffer-read-only t)
1866 (use-local-map gdb-display-mode-map)
1867 (gdb-invalidate-display))
1869 (defun gdb-display-buffer-name ()
1870 (with-current-buffer gud-comint-buffer
1871 (concat "*Displayed expressions of " (gdb-get-target-string) "*")))
1873 (defun gdb-display-display-buffer ()
1874 (interactive)
1875 (gdb-display-buffer
1876 (gdb-get-create-buffer 'gdb-display-buffer)))
1878 (defun gdb-frame-display-buffer ()
1879 (interactive)
1880 (switch-to-buffer-other-frame
1881 (gdb-get-create-buffer 'gdb-display-buffer)))
1883 (defun gdb-toggle-display ()
1884 "Enable/disable the displayed expression at current line."
1885 (interactive)
1886 (save-excursion
1887 (beginning-of-line 1)
1888 (if (not (looking-at "\\([0-9]+\\): \\([ny]\\)"))
1889 (error "No expression on this line")
1890 (gdb-enqueue-input
1891 (list
1892 (concat
1893 (if (eq ?y (char-after (match-beginning 2)))
1894 "server disable display "
1895 "server enable display ")
1896 (match-string 1) "\n")
1897 'ignore)))))
1899 (defun gdb-delete-display ()
1900 "Delete the displayed expression at current line."
1901 (interactive)
1902 (with-current-buffer (gdb-get-buffer 'gdb-display-buffer)
1903 (beginning-of-line 1)
1904 (if (not (looking-at "\\([0-9]+\\): \\([ny]\\)"))
1905 (error "No expression on this line")
1906 (let ((number (match-string 1)))
1907 (gdb-enqueue-input
1908 (list (concat "server delete display " number "\n") 'ignore))))))
1910 (defvar gdb-expressions-mode-map
1911 (let ((map (make-sparse-keymap)))
1912 (suppress-keymap map)
1913 (define-key map "v" 'gdb-array-visualise)
1914 (define-key map "q" 'gdb-delete-expression)
1915 (define-key map [mouse-3] 'gdb-expressions-popup-menu)
1916 map))
1918 (defvar gdb-expressions-mode-menu
1919 '("GDB Expressions Commands"
1920 "----"
1921 ["Visualise" gdb-array-visualise t]
1922 ["Delete" gdb-delete-expression t])
1923 "Menu for `gdb-expressions-mode'.")
1925 (defun gdb-expressions-popup-menu (event)
1926 "Explicit Popup menu as this buffer doesn't have a menubar."
1927 (interactive "@e")
1928 (mouse-set-point event)
1929 (popup-menu gdb-expressions-mode-menu))
1931 (defun gdb-expressions-mode ()
1932 "Major mode for display expressions.
1934 \\{gdb-expressions-mode-map}"
1935 (setq major-mode 'gdb-expressions-mode)
1936 (setq mode-name "Expressions")
1937 (use-local-map gdb-expressions-mode-map)
1938 (make-local-variable 'gdb-display-number)
1939 (make-local-variable 'gdb-values)
1940 (make-local-variable 'gdb-expression)
1941 (set (make-local-variable 'gdb-display-string) nil)
1942 (set (make-local-variable 'gdb-dive-display-number) nil)
1943 (set (make-local-variable 'gud-minor-mode) 'gdba)
1944 (set (make-local-variable 'gdb-array-start) (make-vector 16 '-1))
1945 (set (make-local-variable 'gdb-array-stop) (make-vector 16 '-1))
1946 (set (make-local-variable 'gdb-array-size) (make-vector 16 '-1))
1947 (setq buffer-read-only t))
1950 ;;;; Window management
1952 ;;; The way we abuse the dedicated-p flag is pretty gross, but seems
1953 ;;; to do the right thing. Seeing as there is no way for Lisp code to
1954 ;;; get at the use_time field of a window, I'm not sure there exists a
1955 ;;; more elegant solution without writing C code.
1957 (defun gdb-display-buffer (buf &optional size)
1958 (let ((must-split nil)
1959 (answer nil))
1960 (unwind-protect
1961 (progn
1962 (walk-windows
1963 '(lambda (win)
1964 (if (or (eq gud-comint-buffer (window-buffer win))
1965 (eq gdb-source-window win))
1966 (set-window-dedicated-p win t))))
1967 (setq answer (get-buffer-window buf))
1968 (if (not answer)
1969 (let ((window (get-lru-window)))
1970 (if window
1971 (progn
1972 (set-window-buffer window buf)
1973 (setq answer window))
1974 (setq must-split t)))))
1975 (walk-windows
1976 '(lambda (win)
1977 (if (or (eq gud-comint-buffer (window-buffer win))
1978 (eq gdb-source-window win))
1979 (set-window-dedicated-p win nil)))))
1980 (if must-split
1981 (let* ((largest (get-largest-window))
1982 (cur-size (window-height largest))
1983 (new-size (and size (< size cur-size) (- cur-size size))))
1984 (setq answer (split-window largest new-size))
1985 (set-window-buffer answer buf)))
1986 answer))
1988 (defun gdb-display-source-buffer (buffer)
1989 (if (eq gdb-selected-view 'source)
1990 (set-window-buffer gdb-source-window buffer)
1991 (set-window-buffer gdb-source-window
1992 (gdb-get-buffer 'gdb-assembler-buffer)))
1993 gdb-source-window)
1996 ;;; Shared keymap initialization:
1998 (define-key gud-menu-map [gdb-many-windows]
1999 (menu-bar-make-toggle gdb-many-windows gdb-many-windows
2000 "Display other windows" "Many Windows %s"
2001 "Display locals, stack and breakpoint information"))
2003 (let ((menu (make-sparse-keymap "GDB-Frames")))
2004 (define-key gud-menu-map [frames]
2005 `(menu-item "GDB-Frames" ,menu :visible (eq gud-minor-mode 'gdba)))
2006 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2007 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2008 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2009 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer))
2010 (define-key menu [breakpoints] '("Breakpoints" . gdb-frame-breakpoints-buffer))
2011 (define-key menu [display] '("Display" . gdb-frame-display-buffer))
2012 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2013 ; (define-key menu [assembler] '("Assembler" . gdb-frame-assembler-buffer))
2016 (let ((menu (make-sparse-keymap "GDB-Windows")))
2017 (define-key gud-menu-map [displays]
2018 `(menu-item "GDB-Windows" ,menu :visible (eq gud-minor-mode 'gdba)))
2019 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2020 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2021 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2022 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer))
2023 (define-key menu [breakpoints] '("Breakpoints" . gdb-display-breakpoints-buffer))
2024 (define-key menu [display] '("Display" . gdb-display-display-buffer))
2025 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2026 ; (define-key menu [assembler] '("Assembler" . gdb-display-assembler-buffer))
2029 (let ((menu (make-sparse-keymap "View")))
2030 (define-key gud-menu-map [view] `(menu-item "View" ,menu))
2031 ; (define-key menu [both] '(menu-item "Both" gdb-view-both
2032 ; :help "Display both source and assembler"
2033 ; :button (:radio . (eq gdb-selected-view 'both))))
2034 (define-key menu [assembler] '(menu-item "Assembler" gdb-view-assembler
2035 :help "Display assembler only"
2036 :button (:radio . (eq gdb-selected-view 'assembler))))
2037 (define-key menu [source] '(menu-item "Source" gdb-view-source
2038 :help "Display source only"
2039 :button (:radio . (eq gdb-selected-view 'source)))))
2041 (defun gdb-frame-gdb-buffer ()
2042 (interactive)
2043 (switch-to-buffer-other-frame
2044 (gdb-get-create-buffer 'gdba)))
2046 (defun gdb-display-gdb-buffer ()
2047 (interactive)
2048 (gdb-display-buffer
2049 (gdb-get-create-buffer 'gdba)))
2051 (defun gdb-view-source()
2052 (interactive)
2053 (if gdb-view-source
2054 (if gud-last-last-frame
2055 (set-window-buffer gdb-source-window
2056 (gud-find-file (car gud-last-last-frame)))
2057 (set-window-buffer gdb-source-window (gud-find-file gdb-main-file))))
2058 (setq gdb-selected-view 'source))
2060 (defun gdb-view-assembler()
2061 (interactive)
2062 (set-window-buffer gdb-source-window
2063 (gdb-get-create-buffer 'gdb-assembler-buffer))
2064 (setq gdb-selected-view 'assembler))
2066 ;(defun gdb-view-both()
2067 ;(interactive)
2068 ;(setq gdb-selected-view 'both))
2070 (defvar gdb-main-file nil "Source file from which program execution begins.")
2072 ;; layout for all the windows
2073 (defun gdb-setup-windows ()
2074 (gdb-display-locals-buffer)
2075 (gdb-display-stack-buffer)
2076 (delete-other-windows)
2077 (gdb-display-breakpoints-buffer)
2078 (gdb-display-display-buffer)
2079 (delete-other-windows)
2080 (split-window nil ( / ( * (window-height) 3) 4))
2081 (split-window nil ( / (window-height) 3))
2082 (split-window-horizontally)
2083 (other-window 1)
2084 (switch-to-buffer (gdb-locals-buffer-name))
2085 (other-window 1)
2086 (if gdb-view-source
2087 (switch-to-buffer
2088 (if gud-last-last-frame
2089 (gud-find-file (car gud-last-last-frame))
2090 (gud-find-file gdb-main-file)))
2091 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer)))
2092 (setq gdb-source-window (get-buffer-window (current-buffer)))
2093 (split-window-horizontally)
2094 (other-window 1)
2095 (switch-to-buffer (gdb-inferior-io-name))
2096 (other-window 1)
2097 (switch-to-buffer (gdb-stack-buffer-name))
2098 (split-window-horizontally)
2099 (other-window 1)
2100 (switch-to-buffer (gdb-breakpoints-buffer-name))
2101 (other-window 1))
2103 (defcustom gdb-many-windows nil
2104 "Nil means that gdb starts with just two windows : the GUD and
2105 the source buffer."
2106 :type 'boolean
2107 :group 'gud)
2109 (defun gdb-many-windows (arg)
2110 "Toggle the number of windows in the basic arrangement."
2111 (interactive "P")
2112 (setq gdb-many-windows
2113 (if (null arg)
2114 (not gdb-many-windows)
2115 (> (prefix-numeric-value arg) 0)))
2116 (gdb-restore-windows))
2118 (defun gdb-restore-windows ()
2119 "Restore the basic arrangement of windows used by gdba.
2120 This arrangement depends on the value of `gdb-many-windows'."
2121 (interactive)
2122 (if gdb-many-windows
2123 (progn
2124 (switch-to-buffer gud-comint-buffer)
2125 (delete-other-windows)
2126 (gdb-setup-windows))
2127 (switch-to-buffer gud-comint-buffer)
2128 (delete-other-windows)
2129 (split-window)
2130 (other-window 1)
2131 (if gdb-view-source
2132 (switch-to-buffer
2133 (if gud-last-last-frame
2134 (gud-find-file (car gud-last-last-frame))
2135 (gud-find-file gdb-main-file)))
2136 (switch-to-buffer (gdb-get-create-buffer 'gdb-assembler-buffer)))
2137 (other-window 1)))
2139 (defun gdb-reset ()
2140 "Exit a debugging session cleanly by killing the gdb buffers and resetting
2141 the source buffers."
2142 (gdb-delete-frames '())
2143 (dolist (buffer (buffer-list))
2144 (if (not (eq buffer gud-comint-buffer))
2145 (with-current-buffer buffer
2146 (if (eq gud-minor-mode 'gdba)
2147 (if (string-match "^\*.+*$" (buffer-name))
2148 (kill-buffer nil)
2149 (if (eq window-system 'x)
2150 (remove-images (point-min) (point-max))
2151 (gdb-remove-strings (point-min) (point-max)))
2152 (setq left-margin-width 0)
2153 (setq gud-minor-mode nil)
2154 (kill-local-variable 'tool-bar-map)
2155 (setq gud-running nil)
2156 (if (get-buffer-window (current-buffer))
2157 (set-window-margins (get-buffer-window
2158 (current-buffer))
2159 left-margin-width
2160 right-margin-width))))))))
2162 (defun gdb-source-info ()
2163 "Find the source file where the program starts and displays it with related
2164 buffers."
2165 (goto-char (point-min))
2166 (when (search-forward "directory is " nil t)
2167 (if (looking-at "\\S-*:\\(\\S-*\\)")
2168 (setq gdb-cdir (match-string 1))
2169 (looking-at "\\S-*")
2170 (setq gdb-cdir (match-string 0)))
2171 (search-forward "Located in ")
2172 (looking-at "\\S-*")
2173 (setq gdb-main-file (match-string 0))
2174 ;; Make sure we are not in the minibuffer window when we try to delete
2175 ;; all other windows.
2176 (if (window-minibuffer-p (selected-window))
2177 (other-window 1))
2178 (delete-other-windows)
2179 (if gdb-many-windows
2180 (gdb-setup-windows)
2181 (gdb-display-breakpoints-buffer)
2182 (gdb-display-display-buffer)
2183 (delete-other-windows)
2184 (split-window)
2185 (other-window 1)
2186 (switch-to-buffer (gud-find-file gdb-main-file))
2187 (setq gdb-source-window (get-buffer-window (current-buffer)))
2188 (other-window 1))))
2190 ;;from put-image
2191 (defun gdb-put-string (putstring pos)
2192 "Put string PUTSTRING in front of POS in the current buffer.
2193 PUTSTRING is displayed by putting an overlay into the current buffer with a
2194 `before-string' STRING that has a `display' property whose value is
2195 PUTSTRING."
2196 (setq string "x")
2197 (let ((buffer (current-buffer)))
2198 (setq string (copy-sequence string))
2199 (let ((overlay (make-overlay pos pos buffer))
2200 (prop (list (list 'margin 'left-margin) putstring)))
2201 (put-text-property 0 (length string) 'display prop string)
2202 (overlay-put overlay 'put-break t)
2203 (overlay-put overlay 'before-string string))))
2205 ;;from remove-images
2206 (defun gdb-remove-strings (start end &optional buffer)
2207 "Remove strings between START and END in BUFFER.
2208 Remove only strings that were put in BUFFER with calls to `put-string'.
2209 BUFFER nil or omitted means use the current buffer."
2210 (unless buffer
2211 (setq buffer (current-buffer)))
2212 (let ((overlays (overlays-in start end)))
2213 (while overlays
2214 (let ((overlay (car overlays)))
2215 (when (overlay-get overlay 'put-break)
2216 (delete-overlay overlay)))
2217 (setq overlays (cdr overlays)))))
2219 (defun gdb-put-arrow (putstring pos)
2220 "Put arrow string PUTSTRING in the left margin in front of POS
2221 in the current buffer. PUTSTRING is displayed by putting an
2222 overlay into the current buffer with a `before-string'
2223 \"gdb-arrow\" that has a `display' property whose value is
2224 PUTSTRING. STRING is defaulted if you omit it. POS may be an
2225 integer or marker."
2226 (setq string "gdb-arrow")
2227 (let ((buffer (current-buffer)))
2228 (setq string (copy-sequence string))
2229 (let ((overlay (make-overlay pos pos buffer))
2230 (prop (list (list 'margin 'left-margin) putstring)))
2231 (put-text-property 0 (length string) 'display prop string)
2232 (overlay-put overlay 'put-arrow t)
2233 (overlay-put overlay 'before-string string))))
2235 (defun gdb-remove-arrow (&optional buffer)
2236 "Remove arrow in BUFFER.
2237 Remove only images that were put in BUFFER with calls to `put-arrow'.
2238 BUFFER nil or omitted means use the current buffer."
2239 (unless buffer
2240 (setq buffer (current-buffer)))
2241 (let ((overlays (overlays-in (point-min) (point-max))))
2242 (while overlays
2243 (let ((overlay (car overlays)))
2244 (when (overlay-get overlay 'put-arrow)
2245 (delete-overlay overlay)))
2246 (setq overlays (cdr overlays)))))
2248 (defun gdb-array-visualise ()
2249 "Visualise arrays and slices using graph program from plotutils."
2250 (interactive)
2251 (when (and (display-graphic-p) gdb-display-string)
2252 (let ((n 0) m)
2253 (catch 'multi-dimensional
2254 (while (eq (aref gdb-array-start n) (aref gdb-array-stop n))
2255 (setq n (+ n 1)))
2256 (setq m (+ n 1))
2257 (while (< m (length gdb-array-start))
2258 (if (not (eq (aref gdb-array-start m) (aref gdb-array-stop m)))
2259 (progn
2260 (x-popup-dialog
2261 t `(,(concat "Only one dimensional data can be visualised.\n"
2262 "Use an array slice to reduce the number of\n"
2263 "dimensions") ("OK" t)))
2264 (throw 'multi-dimensional nil))
2265 (setq m (+ m 1))))
2266 (shell-command (concat "echo" gdb-display-string " | graph -a 1 "
2267 (int-to-string (aref gdb-array-start n))
2268 " -x "
2269 (int-to-string (aref gdb-array-start n))
2271 (int-to-string (aref gdb-array-stop n))
2272 " 1 -T X"))))))
2274 (defun gdb-delete-expression ()
2275 "Delete displayed expression and its frame."
2276 (interactive)
2277 (gdb-enqueue-input
2278 (list (concat "server delete display " gdb-display-number "\n")
2279 'ignore)))
2282 ;; Assembler buffer.
2284 (gdb-set-buffer-rules 'gdb-assembler-buffer
2285 'gdb-assembler-buffer-name
2286 'gdb-assembler-mode)
2288 (def-gdb-auto-updated-buffer gdb-assembler-buffer
2289 gdb-invalidate-assembler
2290 (concat "server disassemble " gdb-current-address "\n")
2291 gdb-assembler-handler
2292 gdb-assembler-custom)
2294 (defun gdb-assembler-custom ()
2295 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
2296 (address) (flag))
2297 (with-current-buffer buffer
2298 (if (not (equal gdb-current-address "main"))
2299 (progn
2300 (gdb-remove-arrow)
2301 (goto-char (point-min))
2302 (if (re-search-forward gdb-current-address nil t)
2303 (gdb-put-arrow "=>" (point)))))
2304 ;; remove all breakpoint-icons in assembler buffer before updating.
2305 (if (eq window-system 'x)
2306 (remove-images (point-min) (point-max))
2307 (gdb-remove-strings (point-min) (point-max))))
2308 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
2309 (goto-char (point-min))
2310 (while (< (point) (- (point-max) 1))
2311 (forward-line 1)
2312 (if (looking-at "[^\t].*breakpoint")
2313 (progn
2314 (looking-at
2315 "[0-9]*\\s-*\\S-*\\s-*\\S-*\\s-*\\(.\\)\\s-*0x\\(\\S-*\\)")
2316 (setq flag (char-after (match-beginning 1)))
2317 (setq address (match-string 2))
2318 ;; remove leading 0s from output of info break.
2319 (if (string-match "0+\\(.*\\)" address)
2320 (setq address (match-string 1 address)))
2321 (with-current-buffer buffer
2322 (goto-char (point-min))
2323 (if (re-search-forward address nil t)
2324 (let ((start (progn (beginning-of-line) (- (point) 1)))
2325 (end (progn (end-of-line) (+ (point) 1))))
2326 (if (eq window-system 'x)
2327 (progn
2328 (remove-images start end)
2329 (if (eq ?y flag)
2330 (put-image breakpoint-enabled-icon
2331 (+ start 1)
2332 "breakpoint icon enabled"
2333 'left-margin)
2334 (put-image breakpoint-disabled-icon
2335 (+ start 1)
2336 "breakpoint icon disabled"
2337 'left-margin)))
2338 (gdb-remove-strings start end)
2339 (if (eq ?y flag)
2340 (gdb-put-string "B" (+ start 1))
2341 (gdb-put-string "b" (+ start 1)))))))))))))
2343 (defvar gdb-assembler-mode-map
2344 (let ((map (make-sparse-keymap)))
2345 (suppress-keymap map)
2346 map))
2348 (defun gdb-assembler-mode ()
2349 "Major mode for viewing code assembler.
2351 \\{gdb-assembler-mode-map}"
2352 (setq major-mode 'gdb-assembler-mode)
2353 (setq mode-name "Assembler")
2354 (setq left-margin-width 2)
2355 (setq fringes-outside-margins t)
2356 (setq buffer-read-only t)
2357 (use-local-map gdb-assembler-mode-map)
2358 (gdb-invalidate-assembler)
2359 (gdb-invalidate-breakpoints))
2361 (defun gdb-assembler-buffer-name ()
2362 (with-current-buffer gud-comint-buffer
2363 (concat "*Machine Code " (gdb-get-target-string) "*")))
2365 (defun gdb-display-assembler-buffer ()
2366 (interactive)
2367 (gdb-display-buffer
2368 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2370 (defun gdb-frame-assembler-buffer ()
2371 (interactive)
2372 (switch-to-buffer-other-frame
2373 (gdb-get-create-buffer 'gdb-assembler-buffer)))
2375 ;; modified because if gdb-current-address has changed value a new command
2376 ;; must be enqueued to update the buffer with the new output
2377 (defun gdb-invalidate-assembler (&optional ignored)
2378 (if (and (gdb-get-buffer 'gdb-assembler-buffer)
2379 (or (not (member 'gdb-invalidate-assembler
2380 (gdb-get-pending-triggers)))
2381 (not (string-equal gdb-current-address gdb-previous-address))))
2382 (progn
2383 ;; take previous disassemble command off the queue
2384 (with-current-buffer gud-comint-buffer
2385 (let ((queue (gdb-get-idle-input-queue)) (item))
2386 (dolist (item queue)
2387 (if (equal (cdr item) '(gdb-assembler-handler))
2388 (gdb-set-idle-input-queue
2389 (delete item (gdb-get-idle-input-queue)))))))
2390 (gdb-enqueue-idle-input
2391 (list (concat "server disassemble " gdb-current-address "\n")
2392 'gdb-assembler-handler))
2393 (gdb-set-pending-triggers
2394 (cons 'gdb-invalidate-assembler
2395 (gdb-get-pending-triggers)))
2396 (setq gdb-previous-address gdb-current-address))))
2398 (defun gdb-get-current-frame ()
2399 (if (not (member 'gdb-get-current-frame (gdb-get-pending-triggers)))
2400 (progn
2401 (gdb-enqueue-idle-input
2402 (list (concat "server frame\n") 'gdb-frame-handler))
2403 (gdb-set-pending-triggers
2404 (cons 'gdb-get-current-frame
2405 (gdb-get-pending-triggers))))))
2407 (defun gdb-frame-handler ()
2408 (gdb-set-pending-triggers
2409 (delq 'gdb-get-current-frame (gdb-get-pending-triggers)))
2410 (with-current-buffer (gdb-get-create-buffer 'gdb-partial-output-buffer)
2411 (goto-char (point-min))
2412 (if (looking-at "^#[0-9]*\\s-*\\(\\S-*\\) in \\(\\S-*\\)")
2413 (progn
2414 (setq gdb-current-frame (match-string 2))
2415 (let ((address (match-string 1)))
2416 ;; remove leading 0s from output of frame command.
2417 (if (string-match "0x0+\\(.*\\)" address)
2418 (setq gdb-current-address
2419 (concat "0x" (match-string 1 address)))
2420 (setq gdb-current-address address)))
2421 (if (or (if (not (looking-at ".*) at "))
2422 (progn (setq gdb-view-source nil) t))
2423 (eq gdb-selected-view 'assembler))
2424 (progn
2425 (set-window-buffer
2426 gdb-source-window
2427 (gdb-get-create-buffer 'gdb-assembler-buffer))
2428 (gdb-invalidate-assembler))))
2429 (if (looking-at "^#0\\s-*\\(\\S-*\\)")
2430 (setq gdb-current-frame (match-string 1))))))
2432 (provide 'gdb-ui)
2434 ;;; gdb-ui.el ends here