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