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