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