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