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