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