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