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