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