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