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