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