]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gdb-find-source-frame): Make nil the default value.
[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 inferior 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 inferior.
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 inferior 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 inferior 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 "[^\t].*?breakpoint")
1780 (progn
1781 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1782 (setq bptno (match-string 1))
1783 (setq flag (char-after (match-beginning 2)))
1784 (add-text-properties
1785 (match-beginning 2) (match-end 2)
1786 (if (eq flag ?y)
1787 '(face font-lock-warning-face)
1788 '(face font-lock-type-face)))
1789 (beginning-of-line)
1790 (if (re-search-forward " in \\(.*\\) at\\s-+" nil 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 (line-beginning-position)
1799 (line-end-position)
1800 '(mouse-face highlight
1801 help-echo "mouse-2, RET: visit breakpoint"))
1802 (unless (file-exists-p file)
1803 (setq file (cdr (assoc bptno gdb-location-alist))))
1804 (if (and file
1805 (not (string-equal file "File not found")))
1806 (with-current-buffer
1807 (find-file-noselect file 'nowarn)
1808 (set (make-local-variable 'gud-minor-mode)
1809 'gdba)
1810 (set (make-local-variable 'tool-bar-map)
1811 gud-tool-bar-map)
1812 ;; Only want one breakpoint icon at each
1813 ;; location.
1814 (save-excursion
1815 (goto-line (string-to-number line))
1816 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1817 (gdb-enqueue-input
1818 (list
1819 (concat gdb-server-prefix "list "
1820 (match-string-no-properties 1) ":1\n")
1821 'ignore))
1822 (gdb-enqueue-input
1823 (list (concat gdb-server-prefix "info source\n")
1824 `(lambda () (gdb-get-location
1825 ,bptno ,line ,flag))))))))))
1826 (end-of-line))))))
1827 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1828
1829 (defun gdb-mouse-set-clear-breakpoint (event)
1830 "Set/clear breakpoint in left fringe/margin with mouse click."
1831 (interactive "e")
1832 (mouse-minibuffer-check event)
1833 (let ((posn (event-end event)))
1834 (if (numberp (posn-point posn))
1835 (with-selected-window (posn-window posn)
1836 (save-excursion
1837 (goto-char (posn-point posn))
1838 (if (or (posn-object posn)
1839 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1840 'breakpoint))
1841 (gud-remove nil)
1842 (gud-break nil)))))))
1843
1844 (defun gdb-mouse-toggle-breakpoint-margin (event)
1845 "Enable/disable breakpoint in left margin with mouse click."
1846 (interactive "e")
1847 (mouse-minibuffer-check event)
1848 (let ((posn (event-end event)))
1849 (if (numberp (posn-point posn))
1850 (with-selected-window (posn-window posn)
1851 (save-excursion
1852 (goto-char (posn-point posn))
1853 (if (posn-object posn)
1854 (gdb-enqueue-input
1855 (list
1856 (let ((bptno (get-text-property
1857 0 'gdb-bptno (car (posn-string posn)))))
1858 (concat gdb-server-prefix
1859 (if (get-text-property
1860 0 'gdb-enabled (car (posn-string posn)))
1861 "disable "
1862 "enable ")
1863 bptno "\n"))
1864 'ignore))))))))
1865
1866 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1867 "Enable/disable breakpoint in left fringe with mouse click."
1868 (interactive "e")
1869 (mouse-minibuffer-check event)
1870 (let* ((posn (event-end event))
1871 (pos (posn-point posn))
1872 obj)
1873 (when (numberp pos)
1874 (with-selected-window (posn-window posn)
1875 (save-excursion
1876 (set-buffer (window-buffer (selected-window)))
1877 (goto-char pos)
1878 (dolist (overlay (overlays-in pos pos))
1879 (when (overlay-get overlay 'put-break)
1880 (setq obj (overlay-get overlay 'before-string))))
1881 (when (stringp obj)
1882 (gdb-enqueue-input
1883 (list
1884 (concat gdb-server-prefix
1885 (if (get-text-property 0 'gdb-enabled obj)
1886 "disable "
1887 "enable ")
1888 (get-text-property 0 'gdb-bptno obj) "\n")
1889 'ignore))))))))
1890
1891 (defun gdb-breakpoints-buffer-name ()
1892 (with-current-buffer gud-comint-buffer
1893 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1894
1895 (defun gdb-display-breakpoints-buffer ()
1896 "Display status of user-settable breakpoints."
1897 (interactive)
1898 (gdb-display-buffer
1899 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1900
1901 (defun gdb-frame-breakpoints-buffer ()
1902 "Display status of user-settable breakpoints in a new frame."
1903 (interactive)
1904 (let ((special-display-regexps (append special-display-regexps '(".*")))
1905 (special-display-frame-alist gdb-frame-parameters))
1906 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1907
1908 (defvar gdb-breakpoints-mode-map
1909 (let ((map (make-sparse-keymap))
1910 (menu (make-sparse-keymap "Breakpoints")))
1911 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1912 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1913 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1914 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1915 (suppress-keymap map)
1916 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1917 (define-key map " " 'gdb-toggle-breakpoint)
1918 (define-key map "D" 'gdb-delete-breakpoint)
1919 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1920 (define-key map "q" 'gdb-delete-frame-or-window)
1921 (define-key map "\r" 'gdb-goto-breakpoint)
1922 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1923 (define-key map [follow-link] 'mouse-face)
1924 map))
1925
1926 (defun gdb-delete-frame-or-window ()
1927 "Delete frame if there is only one window. Otherwise delete the window."
1928 (interactive)
1929 (if (one-window-p) (delete-frame)
1930 (delete-window)))
1931
1932 (defun gdb-breakpoints-mode ()
1933 "Major mode for gdb breakpoints.
1934
1935 \\{gdb-breakpoints-mode-map}"
1936 (kill-all-local-variables)
1937 (setq major-mode 'gdb-breakpoints-mode)
1938 (setq mode-name "Breakpoints")
1939 (use-local-map gdb-breakpoints-mode-map)
1940 (setq buffer-read-only t)
1941 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1942 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1943 'gdb-invalidate-breakpoints
1944 'gdbmi-invalidate-breakpoints))
1945
1946 (defconst gdb-breakpoint-regexp
1947 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1948
1949 (defun gdb-toggle-breakpoint ()
1950 "Enable/disable breakpoint at current line."
1951 (interactive)
1952 (save-excursion
1953 (beginning-of-line 1)
1954 (if (looking-at gdb-breakpoint-regexp)
1955 (gdb-enqueue-input
1956 (list
1957 (concat gdb-server-prefix
1958 (if (eq ?y (char-after (match-beginning 2)))
1959 "disable "
1960 "enable ")
1961 (match-string 1) "\n") 'ignore))
1962 (error "Not recognized as break/watchpoint line"))))
1963
1964 (defun gdb-delete-breakpoint ()
1965 "Delete the breakpoint at current line."
1966 (interactive)
1967 (beginning-of-line 1)
1968 (if (looking-at gdb-breakpoint-regexp)
1969 (gdb-enqueue-input
1970 (list
1971 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1972 (error "Not recognized as break/watchpoint line")))
1973
1974 (defun gdb-goto-breakpoint (&optional event)
1975 "Display the breakpoint location specified at current line."
1976 (interactive (list last-input-event))
1977 (if event (posn-set-point (event-end event)))
1978 (save-excursion
1979 (beginning-of-line 1)
1980 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1981 (let ((bptno (match-string 1))
1982 (file (match-string 2))
1983 (line (match-string 3)))
1984 (save-selected-window
1985 (let* ((buffer (find-file-noselect
1986 (if (file-exists-p file) file
1987 (cdr (assoc bptno gdb-location-alist)))))
1988 (window (or (gdb-display-source-buffer buffer)
1989 (display-buffer buffer))))
1990 (setq gdb-source-window window)
1991 (with-current-buffer buffer
1992 (goto-line (string-to-number line))
1993 (set-window-point window (point))))))
1994 (error "No location specified."))))
1995 \f
1996
1997 ;; Frames buffer. This displays a perpetually correct bactracktrace
1998 ;; (from the command `where').
1999 ;;
2000 ;; Alas, if your stack is deep, it is costly.
2001 ;;
2002 (gdb-set-buffer-rules 'gdb-stack-buffer
2003 'gdb-stack-buffer-name
2004 'gdb-frames-mode)
2005
2006 (def-gdb-auto-updated-buffer gdb-stack-buffer
2007 gdb-invalidate-frames
2008 "server info stack\n"
2009 gdb-info-stack-handler
2010 gdb-info-stack-custom)
2011
2012 (defun gdb-info-stack-custom ()
2013 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2014 (save-excursion
2015 (unless (eq gdb-look-up-stack 'delete)
2016 (let ((buffer-read-only nil)
2017 bl el)
2018 (goto-char (point-min))
2019 (while (< (point) (point-max))
2020 (setq bl (line-beginning-position)
2021 el (line-end-position))
2022 (when (looking-at "#")
2023 (add-text-properties bl el
2024 '(mouse-face highlight
2025 help-echo "mouse-2, RET: Select frame")))
2026 (goto-char bl)
2027 (when (looking-at "^#\\([0-9]+\\)")
2028 (when (string-equal (match-string 1) gdb-frame-number)
2029 (put-text-property bl (+ bl 4)
2030 'face '(:inverse-video t)))
2031 (when (re-search-forward
2032 (concat
2033 (if (string-equal (match-string 1) "0") "" " in ")
2034 "\\([^ ]+\\) (") el t)
2035 (put-text-property (match-beginning 1) (match-end 1)
2036 'face font-lock-function-name-face)
2037 (setq bl (match-end 0))
2038 (while (re-search-forward "<\\([^>]+\\)>" el t)
2039 (put-text-property (match-beginning 1) (match-end 1)
2040 'face font-lock-function-name-face))
2041 (goto-char bl)
2042 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2043 (put-text-property (match-beginning 1) (match-end 1)
2044 'face font-lock-variable-name-face))))
2045 (forward-line 1))))
2046 (when gdb-look-up-stack
2047 (goto-char (point-min))
2048 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2049 (let ((start (line-beginning-position))
2050 (file (match-string 1))
2051 (line (match-string 2)))
2052 (re-search-backward "^#*\\([0-9]+\\)" start t)
2053 (gdb-enqueue-input
2054 (list (concat gdb-server-prefix "frame "
2055 (match-string 1) "\n") 'gdb-set-hollow))
2056 (gdb-enqueue-input
2057 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2058 (if (eq gdb-look-up-stack 'delete)
2059 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2060 (setq gdb-look-up-stack nil))
2061
2062 (defun gdb-set-hollow ()
2063 (if gud-last-last-frame
2064 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2065 (setq fringe-indicator-alist
2066 '((overlay-arrow . hollow-right-triangle))))))
2067
2068 (defun gdb-stack-buffer-name ()
2069 (with-current-buffer gud-comint-buffer
2070 (concat "*stack frames of " (gdb-get-target-string) "*")))
2071
2072 (defun gdb-display-stack-buffer ()
2073 "Display backtrace of current stack."
2074 (interactive)
2075 (gdb-display-buffer
2076 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2077
2078 (defun gdb-frame-stack-buffer ()
2079 "Display backtrace of current stack in a new frame."
2080 (interactive)
2081 (let ((special-display-regexps (append special-display-regexps '(".*")))
2082 (special-display-frame-alist gdb-frame-parameters))
2083 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2084
2085 (defvar gdb-frames-mode-map
2086 (let ((map (make-sparse-keymap)))
2087 (suppress-keymap map)
2088 (define-key map "q" 'kill-this-buffer)
2089 (define-key map "\r" 'gdb-frames-select)
2090 (define-key map [mouse-2] 'gdb-frames-select)
2091 (define-key map [follow-link] 'mouse-face)
2092 map))
2093
2094 (defun gdb-frames-mode ()
2095 "Major mode for gdb call stack.
2096
2097 \\{gdb-frames-mode-map}"
2098 (kill-all-local-variables)
2099 (setq major-mode 'gdb-frames-mode)
2100 (setq mode-name "Frames")
2101 (setq buffer-read-only t)
2102 (use-local-map gdb-frames-mode-map)
2103 (run-mode-hooks 'gdb-frames-mode-hook)
2104 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2105 'gdb-invalidate-frames
2106 'gdbmi-invalidate-frames))
2107
2108 (defun gdb-get-frame-number ()
2109 (save-excursion
2110 (end-of-line)
2111 (let* ((start (line-beginning-position))
2112 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2113 (n (or (and pos (match-string-no-properties 1)) "0")))
2114 n)))
2115
2116 (defun gdb-frames-select (&optional event)
2117 "Select the frame and display the relevant source."
2118 (interactive (list last-input-event))
2119 (if event (posn-set-point (event-end event)))
2120 (gdb-enqueue-input
2121 (list (concat gdb-server-prefix "frame "
2122 (gdb-get-frame-number) "\n") 'ignore)))
2123 \f
2124
2125 ;; Threads buffer. This displays a selectable thread list.
2126 ;;
2127 (gdb-set-buffer-rules 'gdb-threads-buffer
2128 'gdb-threads-buffer-name
2129 'gdb-threads-mode)
2130
2131 (def-gdb-auto-updated-buffer gdb-threads-buffer
2132 gdb-invalidate-threads
2133 (concat gdb-server-prefix "info threads\n")
2134 gdb-info-threads-handler
2135 gdb-info-threads-custom)
2136
2137 (defun gdb-info-threads-custom ()
2138 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2139 (let ((buffer-read-only nil))
2140 (save-excursion
2141 (goto-char (point-min))
2142 (while (< (point) (point-max))
2143 (unless (looking-at "No ")
2144 (add-text-properties (line-beginning-position) (line-end-position)
2145 '(mouse-face highlight
2146 help-echo "mouse-2, RET: select thread")))
2147 (forward-line 1))))))
2148
2149 (defun gdb-threads-buffer-name ()
2150 (with-current-buffer gud-comint-buffer
2151 (concat "*threads of " (gdb-get-target-string) "*")))
2152
2153 (defun gdb-display-threads-buffer ()
2154 "Display IDs of currently known threads."
2155 (interactive)
2156 (gdb-display-buffer
2157 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2158
2159 (defun gdb-frame-threads-buffer ()
2160 "Display IDs of currently known threads in a new frame."
2161 (interactive)
2162 (let ((special-display-regexps (append special-display-regexps '(".*")))
2163 (special-display-frame-alist gdb-frame-parameters))
2164 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2165
2166 (defvar gdb-threads-mode-map
2167 (let ((map (make-sparse-keymap)))
2168 (suppress-keymap map)
2169 (define-key map "q" 'kill-this-buffer)
2170 (define-key map "\r" 'gdb-threads-select)
2171 (define-key map [mouse-2] 'gdb-threads-select)
2172 (define-key map [follow-link] 'mouse-face)
2173 map))
2174
2175 (defvar gdb-threads-font-lock-keywords
2176 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2177 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2178 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2179 "Font lock keywords used in `gdb-threads-mode'.")
2180
2181 (defun gdb-threads-mode ()
2182 "Major mode for gdb threads.
2183
2184 \\{gdb-threads-mode-map}"
2185 (kill-all-local-variables)
2186 (setq major-mode 'gdb-threads-mode)
2187 (setq mode-name "Threads")
2188 (setq buffer-read-only t)
2189 (use-local-map gdb-threads-mode-map)
2190 (set (make-local-variable 'font-lock-defaults)
2191 '(gdb-threads-font-lock-keywords))
2192 (run-mode-hooks 'gdb-threads-mode-hook)
2193 'gdb-invalidate-threads)
2194
2195 (defun gdb-get-thread-number ()
2196 (save-excursion
2197 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2198 (match-string-no-properties 1)))
2199
2200 (defun gdb-threads-select (&optional event)
2201 "Select the thread and display the relevant source."
2202 (interactive (list last-input-event))
2203 (if event (posn-set-point (event-end event)))
2204 (gdb-enqueue-input
2205 (list (concat gdb-server-prefix "thread "
2206 (gdb-get-thread-number) "\n") 'ignore))
2207 (gud-display-frame))
2208 \f
2209
2210 ;; Registers buffer.
2211 ;;
2212 (defcustom gdb-all-registers nil
2213 "Non-nil means include floating-point registers."
2214 :type 'boolean
2215 :group 'gud
2216 :version "22.1")
2217
2218 (gdb-set-buffer-rules 'gdb-registers-buffer
2219 'gdb-registers-buffer-name
2220 'gdb-registers-mode)
2221
2222 (def-gdb-auto-updated-buffer gdb-registers-buffer
2223 gdb-invalidate-registers
2224 (concat
2225 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2226 gdb-info-registers-handler
2227 gdb-info-registers-custom)
2228
2229 (defun gdb-info-registers-custom ()
2230 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2231 (save-excursion
2232 (let ((buffer-read-only nil)
2233 start end)
2234 (goto-char (point-min))
2235 (while (< (point) (point-max))
2236 (setq start (line-beginning-position))
2237 (setq end (line-end-position))
2238 (when (looking-at "^[^ ]+")
2239 (unless (string-equal (match-string 0) "The")
2240 (put-text-property start (match-end 0)
2241 'face font-lock-variable-name-face)
2242 (add-text-properties start end
2243 '(help-echo "mouse-2: edit value"
2244 mouse-face highlight))))
2245 (forward-line 1))))))
2246
2247 (defun gdb-edit-register-value (&optional event)
2248 (interactive (list last-input-event))
2249 (save-excursion
2250 (if event (posn-set-point (event-end event)))
2251 (beginning-of-line)
2252 (let* ((register (current-word))
2253 (value (read-string (format "New value (%s): " register))))
2254 (gdb-enqueue-input
2255 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2256 'ignore)))))
2257
2258 (defvar gdb-registers-mode-map
2259 (let ((map (make-sparse-keymap)))
2260 (suppress-keymap map)
2261 (define-key map "\r" 'gdb-edit-register-value)
2262 (define-key map [mouse-2] 'gdb-edit-register-value)
2263 (define-key map " " 'gdb-all-registers)
2264 (define-key map "q" 'kill-this-buffer)
2265 map))
2266
2267 (defun gdb-registers-mode ()
2268 "Major mode for gdb registers.
2269
2270 \\{gdb-registers-mode-map}"
2271 (kill-all-local-variables)
2272 (setq major-mode 'gdb-registers-mode)
2273 (setq mode-name "Registers")
2274 (setq buffer-read-only t)
2275 (use-local-map gdb-registers-mode-map)
2276 (run-mode-hooks 'gdb-registers-mode-hook)
2277 (if (string-equal gdb-version "pre-6.4")
2278 (progn
2279 (if gdb-all-registers (setq mode-name "Registers:All"))
2280 'gdb-invalidate-registers)
2281 'gdb-invalidate-registers-1))
2282
2283 (defun gdb-registers-buffer-name ()
2284 (with-current-buffer gud-comint-buffer
2285 (concat "*registers of " (gdb-get-target-string) "*")))
2286
2287 (defun gdb-display-registers-buffer ()
2288 "Display integer register contents."
2289 (interactive)
2290 (gdb-display-buffer
2291 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2292
2293 (defun gdb-frame-registers-buffer ()
2294 "Display integer register contents in a new frame."
2295 (interactive)
2296 (let ((special-display-regexps (append special-display-regexps '(".*")))
2297 (special-display-frame-alist gdb-frame-parameters))
2298 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2299
2300 (defun gdb-all-registers ()
2301 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2302 (interactive)
2303 (when (string-equal gdb-version "pre-6.4")
2304 (if gdb-all-registers
2305 (progn
2306 (setq gdb-all-registers nil)
2307 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2308 (setq mode-name "Registers")))
2309 (setq gdb-all-registers t)
2310 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2311 (setq mode-name "Registers:All")))
2312 (message (format "Display of floating-point registers %sabled"
2313 (if gdb-all-registers "en" "dis")))
2314 (gdb-invalidate-registers)))
2315 \f
2316
2317 ;; Memory buffer.
2318 ;;
2319 (defcustom gdb-memory-repeat-count 32
2320 "Number of data items in memory window."
2321 :type 'integer
2322 :group 'gud
2323 :version "22.1")
2324
2325 (defcustom gdb-memory-format "x"
2326 "Display format of data items in memory window."
2327 :type '(choice (const :tag "Hexadecimal" "x")
2328 (const :tag "Signed decimal" "d")
2329 (const :tag "Unsigned decimal" "u")
2330 (const :tag "Octal" "o")
2331 (const :tag "Binary" "t"))
2332 :group 'gud
2333 :version "22.1")
2334
2335 (defcustom gdb-memory-unit "w"
2336 "Unit size of data items in memory window."
2337 :type '(choice (const :tag "Byte" "b")
2338 (const :tag "Halfword" "h")
2339 (const :tag "Word" "w")
2340 (const :tag "Giant word" "g"))
2341 :group 'gud
2342 :version "22.1")
2343
2344 (gdb-set-buffer-rules 'gdb-memory-buffer
2345 'gdb-memory-buffer-name
2346 'gdb-memory-mode)
2347
2348 (def-gdb-auto-updated-buffer gdb-memory-buffer
2349 gdb-invalidate-memory
2350 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2351 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2352 gdb-read-memory-handler
2353 gdb-read-memory-custom)
2354
2355 (defun gdb-read-memory-custom ()
2356 (save-excursion
2357 (goto-char (point-min))
2358 (if (looking-at "0x[[:xdigit:]]+")
2359 (setq gdb-memory-address (match-string 0)))))
2360
2361 (defvar gdb-memory-mode-map
2362 (let ((map (make-sparse-keymap)))
2363 (suppress-keymap map)
2364 (define-key map "q" 'kill-this-buffer)
2365 map))
2366
2367 (defun gdb-memory-set-address (event)
2368 "Set the start memory address."
2369 (interactive "e")
2370 (save-selected-window
2371 (select-window (posn-window (event-start event)))
2372 (let ((arg (read-from-minibuffer "Memory address: ")))
2373 (setq gdb-memory-address arg))
2374 (gdb-invalidate-memory)))
2375
2376 (defun gdb-memory-set-repeat-count (event)
2377 "Set the number of data items in memory window."
2378 (interactive "e")
2379 (save-selected-window
2380 (select-window (posn-window (event-start event)))
2381 (let* ((arg (read-from-minibuffer "Repeat count: "))
2382 (count (string-to-number arg)))
2383 (if (<= count 0)
2384 (error "Positive numbers only")
2385 (customize-set-variable 'gdb-memory-repeat-count count)
2386 (gdb-invalidate-memory)))))
2387
2388 (defun gdb-memory-format-binary ()
2389 "Set the display format to binary."
2390 (interactive)
2391 (customize-set-variable 'gdb-memory-format "t")
2392 (gdb-invalidate-memory))
2393
2394 (defun gdb-memory-format-octal ()
2395 "Set the display format to octal."
2396 (interactive)
2397 (customize-set-variable 'gdb-memory-format "o")
2398 (gdb-invalidate-memory))
2399
2400 (defun gdb-memory-format-unsigned ()
2401 "Set the display format to unsigned decimal."
2402 (interactive)
2403 (customize-set-variable 'gdb-memory-format "u")
2404 (gdb-invalidate-memory))
2405
2406 (defun gdb-memory-format-signed ()
2407 "Set the display format to decimal."
2408 (interactive)
2409 (customize-set-variable 'gdb-memory-format "d")
2410 (gdb-invalidate-memory))
2411
2412 (defun gdb-memory-format-hexadecimal ()
2413 "Set the display format to hexadecimal."
2414 (interactive)
2415 (customize-set-variable 'gdb-memory-format "x")
2416 (gdb-invalidate-memory))
2417
2418 (defvar gdb-memory-format-map
2419 (let ((map (make-sparse-keymap)))
2420 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2421 map)
2422 "Keymap to select format in the header line.")
2423
2424 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2425 "Menu of display formats in the header line.")
2426
2427 (define-key gdb-memory-format-menu [binary]
2428 '(menu-item "Binary" gdb-memory-format-binary
2429 :button (:radio . (equal gdb-memory-format "t"))))
2430 (define-key gdb-memory-format-menu [octal]
2431 '(menu-item "Octal" gdb-memory-format-octal
2432 :button (:radio . (equal gdb-memory-format "o"))))
2433 (define-key gdb-memory-format-menu [unsigned]
2434 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2435 :button (:radio . (equal gdb-memory-format "u"))))
2436 (define-key gdb-memory-format-menu [signed]
2437 '(menu-item "Signed Decimal" gdb-memory-format-signed
2438 :button (:radio . (equal gdb-memory-format "d"))))
2439 (define-key gdb-memory-format-menu [hexadecimal]
2440 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2441 :button (:radio . (equal gdb-memory-format "x"))))
2442
2443 (defun gdb-memory-format-menu (event)
2444 (interactive "@e")
2445 (x-popup-menu event gdb-memory-format-menu))
2446
2447 (defun gdb-memory-format-menu-1 (event)
2448 (interactive "e")
2449 (save-selected-window
2450 (select-window (posn-window (event-start event)))
2451 (let* ((selection (gdb-memory-format-menu event))
2452 (binding (and selection (lookup-key gdb-memory-format-menu
2453 (vector (car selection))))))
2454 (if binding (call-interactively binding)))))
2455
2456 (defun gdb-memory-unit-giant ()
2457 "Set the unit size to giant words (eight bytes)."
2458 (interactive)
2459 (customize-set-variable 'gdb-memory-unit "g")
2460 (gdb-invalidate-memory))
2461
2462 (defun gdb-memory-unit-word ()
2463 "Set the unit size to words (four bytes)."
2464 (interactive)
2465 (customize-set-variable 'gdb-memory-unit "w")
2466 (gdb-invalidate-memory))
2467
2468 (defun gdb-memory-unit-halfword ()
2469 "Set the unit size to halfwords (two bytes)."
2470 (interactive)
2471 (customize-set-variable 'gdb-memory-unit "h")
2472 (gdb-invalidate-memory))
2473
2474 (defun gdb-memory-unit-byte ()
2475 "Set the unit size to bytes."
2476 (interactive)
2477 (customize-set-variable 'gdb-memory-unit "b")
2478 (gdb-invalidate-memory))
2479
2480 (defvar gdb-memory-unit-map
2481 (let ((map (make-sparse-keymap)))
2482 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2483 map)
2484 "Keymap to select units in the header line.")
2485
2486 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2487 "Menu of units in the header line.")
2488
2489 (define-key gdb-memory-unit-menu [giantwords]
2490 '(menu-item "Giant words" gdb-memory-unit-giant
2491 :button (:radio . (equal gdb-memory-unit "g"))))
2492 (define-key gdb-memory-unit-menu [words]
2493 '(menu-item "Words" gdb-memory-unit-word
2494 :button (:radio . (equal gdb-memory-unit "w"))))
2495 (define-key gdb-memory-unit-menu [halfwords]
2496 '(menu-item "Halfwords" gdb-memory-unit-halfword
2497 :button (:radio . (equal gdb-memory-unit "h"))))
2498 (define-key gdb-memory-unit-menu [bytes]
2499 '(menu-item "Bytes" gdb-memory-unit-byte
2500 :button (:radio . (equal gdb-memory-unit "b"))))
2501
2502 (defun gdb-memory-unit-menu (event)
2503 (interactive "@e")
2504 (x-popup-menu event gdb-memory-unit-menu))
2505
2506 (defun gdb-memory-unit-menu-1 (event)
2507 (interactive "e")
2508 (save-selected-window
2509 (select-window (posn-window (event-start event)))
2510 (let* ((selection (gdb-memory-unit-menu event))
2511 (binding (and selection (lookup-key gdb-memory-unit-menu
2512 (vector (car selection))))))
2513 (if binding (call-interactively binding)))))
2514
2515 ;;from make-mode-line-mouse-map
2516 (defun gdb-make-header-line-mouse-map (mouse function) "\
2517 Return a keymap with single entry for mouse key MOUSE on the header line.
2518 MOUSE is defined to run function FUNCTION with no args in the buffer
2519 corresponding to the mode line clicked."
2520 (let ((map (make-sparse-keymap)))
2521 (define-key map (vector 'header-line mouse) function)
2522 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2523 map))
2524
2525 (defvar gdb-memory-font-lock-keywords
2526 '(;; <__function.name+n>
2527 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2528 )
2529 "Font lock keywords used in `gdb-memory-mode'.")
2530
2531 (defun gdb-memory-mode ()
2532 "Major mode for examining memory.
2533
2534 \\{gdb-memory-mode-map}"
2535 (kill-all-local-variables)
2536 (setq major-mode 'gdb-memory-mode)
2537 (setq mode-name "Memory")
2538 (setq buffer-read-only t)
2539 (use-local-map gdb-memory-mode-map)
2540 (setq header-line-format
2541 '(:eval
2542 (concat
2543 "Read address["
2544 (propertize
2545 "-"
2546 'face font-lock-warning-face
2547 'help-echo "mouse-1: Decrement address"
2548 'mouse-face 'mode-line-highlight
2549 'local-map
2550 (gdb-make-header-line-mouse-map
2551 'mouse-1
2552 #'(lambda () (interactive)
2553 (let ((gdb-memory-address
2554 ;; Let GDB do the arithmetic.
2555 (concat
2556 gdb-memory-address " - "
2557 (number-to-string
2558 (* gdb-memory-repeat-count
2559 (cond ((string= gdb-memory-unit "b") 1)
2560 ((string= gdb-memory-unit "h") 2)
2561 ((string= gdb-memory-unit "w") 4)
2562 ((string= gdb-memory-unit "g") 8)))))))
2563 (gdb-invalidate-memory)))))
2564 "|"
2565 (propertize "+"
2566 'face font-lock-warning-face
2567 'help-echo "mouse-1: Increment address"
2568 'mouse-face 'mode-line-highlight
2569 'local-map (gdb-make-header-line-mouse-map
2570 'mouse-1
2571 #'(lambda () (interactive)
2572 (let ((gdb-memory-address nil))
2573 (gdb-invalidate-memory)))))
2574 "]: "
2575 (propertize gdb-memory-address
2576 'face font-lock-warning-face
2577 'help-echo "mouse-1: Set memory address"
2578 'mouse-face 'mode-line-highlight
2579 'local-map (gdb-make-header-line-mouse-map
2580 'mouse-1
2581 #'gdb-memory-set-address))
2582 " Repeat Count: "
2583 (propertize (number-to-string gdb-memory-repeat-count)
2584 'face font-lock-warning-face
2585 'help-echo "mouse-1: Set repeat count"
2586 'mouse-face 'mode-line-highlight
2587 'local-map (gdb-make-header-line-mouse-map
2588 'mouse-1
2589 #'gdb-memory-set-repeat-count))
2590 " Display Format: "
2591 (propertize gdb-memory-format
2592 'face font-lock-warning-face
2593 'help-echo "mouse-3: Select display format"
2594 'mouse-face 'mode-line-highlight
2595 'local-map gdb-memory-format-map)
2596 " Unit Size: "
2597 (propertize gdb-memory-unit
2598 'face font-lock-warning-face
2599 'help-echo "mouse-3: Select unit size"
2600 'mouse-face 'mode-line-highlight
2601 'local-map gdb-memory-unit-map))))
2602 (set (make-local-variable 'font-lock-defaults)
2603 '(gdb-memory-font-lock-keywords))
2604 (run-mode-hooks 'gdb-memory-mode-hook)
2605 'gdb-invalidate-memory)
2606
2607 (defun gdb-memory-buffer-name ()
2608 (with-current-buffer gud-comint-buffer
2609 (concat "*memory of " (gdb-get-target-string) "*")))
2610
2611 (defun gdb-display-memory-buffer ()
2612 "Display memory contents."
2613 (interactive)
2614 (gdb-display-buffer
2615 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2616
2617 (defun gdb-frame-memory-buffer ()
2618 "Display memory contents in a new frame."
2619 (interactive)
2620 (let ((special-display-regexps (append special-display-regexps '(".*")))
2621 (special-display-frame-alist gdb-frame-parameters))
2622 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2623 \f
2624
2625 ;; Locals buffer.
2626 ;;
2627 (gdb-set-buffer-rules 'gdb-locals-buffer
2628 'gdb-locals-buffer-name
2629 'gdb-locals-mode)
2630
2631 (def-gdb-auto-update-trigger gdb-invalidate-locals
2632 (gdb-get-buffer 'gdb-locals-buffer)
2633 "server info locals\n"
2634 gdb-info-locals-handler)
2635
2636 (defvar gdb-locals-watch-map
2637 (let ((map (make-sparse-keymap)))
2638 (define-key map "\r" '(lambda () (interactive)
2639 (beginning-of-line)
2640 (gud-watch)))
2641 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2642 (mouse-set-point event)
2643 (beginning-of-line)
2644 (gud-watch)))
2645 map)
2646 "Keymap to create watch expression of a complex data type local variable.")
2647
2648 (defconst gdb-struct-string
2649 (concat (propertize "[struct/union]"
2650 'mouse-face 'highlight
2651 'help-echo "mouse-2: create watch expression"
2652 'local-map gdb-locals-watch-map) "\n"))
2653
2654 (defconst gdb-array-string
2655 (concat " " (propertize "[array]"
2656 'mouse-face 'highlight
2657 'help-echo "mouse-2: create watch expression"
2658 'local-map gdb-locals-watch-map) "\n"))
2659
2660 ;; Abbreviate for arrays and structures.
2661 ;; These can be expanded using gud-display.
2662 (defun gdb-info-locals-handler ()
2663 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2664 gdb-pending-triggers))
2665 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2666 (with-current-buffer buf
2667 (goto-char (point-min))
2668 (while (re-search-forward "^[ }].*\n" nil t)
2669 (replace-match "" nil nil))
2670 (goto-char (point-min))
2671 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2672 (replace-match gdb-struct-string nil nil))
2673 (goto-char (point-min))
2674 (while (re-search-forward "\\s-*{.*\n" nil t)
2675 (replace-match gdb-array-string nil nil))))
2676 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2677 (and buf
2678 (with-current-buffer buf
2679 (let* ((window (get-buffer-window buf 0))
2680 (start (window-start window))
2681 (p (window-point window))
2682 (buffer-read-only nil))
2683 (erase-buffer)
2684 (insert-buffer-substring (gdb-get-buffer-create
2685 'gdb-partial-output-buffer))
2686 (set-window-start window start)
2687 (set-window-point window p))
2688 )))
2689 (run-hooks 'gdb-info-locals-hook))
2690
2691 (defvar gdb-locals-mode-map
2692 (let ((map (make-sparse-keymap)))
2693 (suppress-keymap map)
2694 (define-key map "q" 'kill-this-buffer)
2695 map))
2696
2697 (defun gdb-locals-mode ()
2698 "Major mode for gdb locals.
2699
2700 \\{gdb-locals-mode-map}"
2701 (kill-all-local-variables)
2702 (setq major-mode 'gdb-locals-mode)
2703 (setq mode-name (concat "Locals:" gdb-selected-frame))
2704 (setq buffer-read-only t)
2705 (use-local-map gdb-locals-mode-map)
2706 (set (make-local-variable 'font-lock-defaults)
2707 '(gdb-locals-font-lock-keywords))
2708 (run-mode-hooks 'gdb-locals-mode-hook)
2709 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2710 (string-equal gdb-version "pre-6.4"))
2711 'gdb-invalidate-locals
2712 'gdb-invalidate-locals-1))
2713
2714 (defun gdb-locals-buffer-name ()
2715 (with-current-buffer gud-comint-buffer
2716 (concat "*locals of " (gdb-get-target-string) "*")))
2717
2718 (defun gdb-display-locals-buffer ()
2719 "Display local variables of current stack and their values."
2720 (interactive)
2721 (gdb-display-buffer
2722 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2723
2724 (defun gdb-frame-locals-buffer ()
2725 "Display local variables of current stack and their values in a new frame."
2726 (interactive)
2727 (let ((special-display-regexps (append special-display-regexps '(".*")))
2728 (special-display-frame-alist gdb-frame-parameters))
2729 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2730 \f
2731
2732 ;;;; Window management
2733 (defun gdb-display-buffer (buf dedicated &optional size)
2734 (let ((answer (get-buffer-window buf 0))
2735 (must-split nil))
2736 (if answer
2737 (display-buffer buf nil 0) ;Raise the frame if necessary.
2738 ;; The buffer is not yet displayed.
2739 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2740 (let ((window (get-lru-window)))
2741 (if (and window
2742 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2743 ,gdb-source-window))))
2744 (progn
2745 (set-window-buffer window buf)
2746 (setq answer window))
2747 (setq must-split t)))
2748 (if must-split
2749 (let* ((largest (get-largest-window))
2750 (cur-size (window-height largest))
2751 (new-size (and size (< size cur-size) (- cur-size size))))
2752 (setq answer (split-window largest new-size))
2753 (set-window-buffer answer buf)
2754 (set-window-dedicated-p answer dedicated)))
2755 answer)))
2756
2757 \f
2758 ;;; Shared keymap initialization:
2759
2760 (let ((menu (make-sparse-keymap "GDB-Windows")))
2761 (define-key gud-menu-map [displays]
2762 `(menu-item "GDB-Windows" ,menu
2763 :visible (memq gud-minor-mode '(gdbmi gdba))))
2764 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2765 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2766 (define-key menu [inferior]
2767 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2768 :enable gdb-use-separate-io-buffer))
2769 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2770 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2771 (define-key menu [disassembly]
2772 '("Disassembly" . gdb-display-assembler-buffer))
2773 (define-key menu [breakpoints]
2774 '("Breakpoints" . gdb-display-breakpoints-buffer))
2775 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2776 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2777
2778 (let ((menu (make-sparse-keymap "GDB-Frames")))
2779 (define-key gud-menu-map [frames]
2780 `(menu-item "GDB-Frames" ,menu
2781 :visible (memq gud-minor-mode '(gdbmi gdba))))
2782 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2783 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2784 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2785 (define-key menu [inferior]
2786 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2787 :enable gdb-use-separate-io-buffer))
2788 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2789 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2790 (define-key menu [breakpoints]
2791 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2792 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2793 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2794
2795 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2796 (define-key gud-menu-map [ui]
2797 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2798 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2799 (define-key menu [gdb-find-source-frame]
2800 '(menu-item "Look For Source Frame" gdb-find-source-frame
2801 :visible (eq gud-minor-mode 'gdba)
2802 :help "Toggle look for source frame."
2803 :button (:toggle . gdb-find-source-frame)))
2804 (define-key menu [gdb-use-separate-io]
2805 '(menu-item "Separate Inferior IO" gdb-use-separate-io-buffer
2806 :visible (eq gud-minor-mode 'gdba)
2807 :help "Toggle separate IO for inferior."
2808 :button (:toggle . gdb-use-separate-io-buffer)))
2809 (define-key menu [gdb-many-windows]
2810 '(menu-item "Display Other Windows" gdb-many-windows
2811 :help "Toggle display of locals, stack and breakpoint information"
2812 :button (:toggle . gdb-many-windows)))
2813 (define-key menu [gdb-restore-windows]
2814 '(menu-item "Restore Window Layout" gdb-restore-windows
2815 :help "Restore standard layout for debug session.")))
2816
2817 (defun gdb-frame-gdb-buffer ()
2818 "Display GUD buffer in a new frame."
2819 (interactive)
2820 (let ((special-display-regexps (append special-display-regexps '(".*")))
2821 (special-display-frame-alist
2822 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2823 gdb-frame-parameters)))
2824 (same-window-regexps nil))
2825 (display-buffer gud-comint-buffer)))
2826
2827 (defun gdb-display-gdb-buffer ()
2828 "Display GUD buffer."
2829 (interactive)
2830 (let ((same-window-regexps nil))
2831 (pop-to-buffer gud-comint-buffer)))
2832
2833 (defun gdb-set-window-buffer (name)
2834 (set-window-buffer (selected-window) (get-buffer name))
2835 (set-window-dedicated-p (selected-window) t))
2836
2837 (defun gdb-setup-windows ()
2838 "Layout the window pattern for `gdb-many-windows'."
2839 (gdb-display-locals-buffer)
2840 (gdb-display-stack-buffer)
2841 (delete-other-windows)
2842 (gdb-display-breakpoints-buffer)
2843 (delete-other-windows)
2844 ; Don't dedicate.
2845 (pop-to-buffer gud-comint-buffer)
2846 (split-window nil ( / ( * (window-height) 3) 4))
2847 (split-window nil ( / (window-height) 3))
2848 (split-window-horizontally)
2849 (other-window 1)
2850 (gdb-set-window-buffer (gdb-locals-buffer-name))
2851 (other-window 1)
2852 (switch-to-buffer
2853 (if gud-last-last-frame
2854 (gud-find-file (car gud-last-last-frame))
2855 (if gdb-main-file
2856 (gud-find-file gdb-main-file)
2857 ;; Put buffer list in window if we
2858 ;; can't find a source file.
2859 (list-buffers-noselect))))
2860 (setq gdb-source-window (selected-window))
2861 (when gdb-use-separate-io-buffer
2862 (split-window-horizontally)
2863 (other-window 1)
2864 (gdb-set-window-buffer
2865 (gdb-get-buffer-create 'gdb-inferior-io)))
2866 (other-window 1)
2867 (gdb-set-window-buffer (gdb-stack-buffer-name))
2868 (split-window-horizontally)
2869 (other-window 1)
2870 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2871 (other-window 1))
2872
2873 (defun gdb-restore-windows ()
2874 "Restore the basic arrangement of windows used by gdba.
2875 This arrangement depends on the value of `gdb-many-windows'."
2876 (interactive)
2877 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2878 (delete-other-windows)
2879 (if gdb-many-windows
2880 (gdb-setup-windows)
2881 (when (or gud-last-last-frame gdb-show-main)
2882 (split-window)
2883 (other-window 1)
2884 (switch-to-buffer
2885 (if gud-last-last-frame
2886 (gud-find-file (car gud-last-last-frame))
2887 (gud-find-file gdb-main-file)))
2888 (setq gdb-source-window (selected-window))
2889 (other-window 1))))
2890
2891 (defun gdb-reset ()
2892 "Exit a debugging session cleanly.
2893 Kills the gdb buffers, and resets variables and the source buffers."
2894 (dolist (buffer (buffer-list))
2895 (unless (eq buffer gud-comint-buffer)
2896 (with-current-buffer buffer
2897 (if (memq gud-minor-mode '(gdbmi gdba))
2898 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2899 (kill-buffer nil)
2900 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2901 (setq gud-minor-mode nil)
2902 (kill-local-variable 'tool-bar-map)
2903 (kill-local-variable 'gdb-define-alist))))))
2904 (when (markerp gdb-overlay-arrow-position)
2905 (move-marker gdb-overlay-arrow-position nil)
2906 (setq gdb-overlay-arrow-position nil))
2907 (setq overlay-arrow-variable-list
2908 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2909 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2910 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2911 (setq gud-running nil)
2912 (setq gdb-active-process nil)
2913 (setq gdb-var-list nil)
2914 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2915
2916 (defun gdb-source-info ()
2917 "Find the source file where the program starts and displays it with related
2918 buffers."
2919 (goto-char (point-min))
2920 (if (and (search-forward "Located in " nil t)
2921 (looking-at "\\S-+"))
2922 (setq gdb-main-file (match-string 0)))
2923 (goto-char (point-min))
2924 (if (search-forward "Includes preprocessor macro info." nil t)
2925 (setq gdb-macro-info t))
2926 (if gdb-many-windows
2927 (gdb-setup-windows)
2928 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2929 (if gdb-show-main
2930 (let ((pop-up-windows t))
2931 (display-buffer (gud-find-file gdb-main-file))))))
2932
2933 (defun gdb-get-location (bptno line flag)
2934 "Find the directory containing the relevant source file.
2935 Put in buffer and place breakpoint icon."
2936 (goto-char (point-min))
2937 (catch 'file-not-found
2938 (if (search-forward "Located in " nil t)
2939 (when (looking-at "\\S-+")
2940 (delete (cons bptno "File not found") gdb-location-alist)
2941 (push (cons bptno (match-string 0)) gdb-location-alist))
2942 (gdb-resync)
2943 (unless (assoc bptno gdb-location-alist)
2944 (push (cons bptno "File not found") gdb-location-alist)
2945 (message-box "Cannot find source file for breakpoint location.\n\
2946 Add directory to search path for source files using the GDB command, dir."))
2947 (throw 'file-not-found nil))
2948 (with-current-buffer
2949 (find-file-noselect (match-string 0))
2950 (save-current-buffer
2951 (set (make-local-variable 'gud-minor-mode) 'gdba)
2952 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2953 ;; only want one breakpoint icon at each location
2954 (save-excursion
2955 (goto-line (string-to-number line))
2956 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2957
2958 (add-hook 'find-file-hook 'gdb-find-file-hook)
2959
2960 (defun gdb-find-file-hook ()
2961 "Set up buffer for debugging if file is part of the source code
2962 of the current session."
2963 (if (and (buffer-name gud-comint-buffer)
2964 ;; in case gud or gdb-ui is just loaded
2965 gud-comint-buffer
2966 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2967 '(gdba gdbmi)))
2968 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2969 (if (member (if (string-equal gdb-version "pre-6.4")
2970 (file-name-nondirectory buffer-file-name)
2971 buffer-file-name)
2972 gdb-source-file-list)
2973 (with-current-buffer (find-buffer-visiting buffer-file-name)
2974 (set (make-local-variable 'gud-minor-mode)
2975 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2976 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2977
2978 ;;from put-image
2979 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2980 "Put string PUTSTRING in front of POS in the current buffer.
2981 PUTSTRING is displayed by putting an overlay into the current buffer with a
2982 `before-string' string that has a `display' property whose value is
2983 PUTSTRING."
2984 (let ((string (make-string 1 ?x))
2985 (buffer (current-buffer)))
2986 (setq putstring (copy-sequence putstring))
2987 (let ((overlay (make-overlay pos pos buffer))
2988 (prop (or dprop
2989 (list (list 'margin 'left-margin) putstring))))
2990 (put-text-property 0 1 'display prop string)
2991 (if sprops
2992 (add-text-properties 0 1 sprops string))
2993 (overlay-put overlay 'put-break t)
2994 (overlay-put overlay 'before-string string))))
2995
2996 ;;from remove-images
2997 (defun gdb-remove-strings (start end &optional buffer)
2998 "Remove strings between START and END in BUFFER.
2999 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3000 BUFFER nil or omitted means use the current buffer."
3001 (unless buffer
3002 (setq buffer (current-buffer)))
3003 (dolist (overlay (overlays-in start end))
3004 (when (overlay-get overlay 'put-break)
3005 (delete-overlay overlay))))
3006
3007 (defun gdb-put-breakpoint-icon (enabled bptno)
3008 (let ((start (- (line-beginning-position) 1))
3009 (end (+ (line-end-position) 1))
3010 (putstring (if enabled "B" "b"))
3011 (source-window (get-buffer-window (current-buffer) 0)))
3012 (add-text-properties
3013 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3014 putstring)
3015 (if enabled
3016 (add-text-properties
3017 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3018 (add-text-properties
3019 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3020 (gdb-remove-breakpoint-icons start end)
3021 (if (display-images-p)
3022 (if (>= (or left-fringe-width
3023 (if source-window (car (window-fringes source-window)))
3024 gdb-buffer-fringe-width) 8)
3025 (gdb-put-string
3026 nil (1+ start)
3027 `(left-fringe breakpoint
3028 ,(if enabled
3029 'breakpoint-enabled
3030 'breakpoint-disabled))
3031 'gdb-bptno bptno
3032 'gdb-enabled enabled)
3033 (when (< left-margin-width 2)
3034 (save-current-buffer
3035 (setq left-margin-width 2)
3036 (if source-window
3037 (set-window-margins
3038 source-window
3039 left-margin-width right-margin-width))))
3040 (put-image
3041 (if enabled
3042 (or breakpoint-enabled-icon
3043 (setq breakpoint-enabled-icon
3044 (find-image `((:type xpm :data
3045 ,breakpoint-xpm-data
3046 :ascent 100 :pointer hand)
3047 (:type pbm :data
3048 ,breakpoint-enabled-pbm-data
3049 :ascent 100 :pointer hand)))))
3050 (or breakpoint-disabled-icon
3051 (setq breakpoint-disabled-icon
3052 (find-image `((:type xpm :data
3053 ,breakpoint-xpm-data
3054 :conversion disabled
3055 :ascent 100 :pointer hand)
3056 (:type pbm :data
3057 ,breakpoint-disabled-pbm-data
3058 :ascent 100 :pointer hand))))))
3059 (+ start 1)
3060 putstring
3061 'left-margin))
3062 (when (< left-margin-width 2)
3063 (save-current-buffer
3064 (setq left-margin-width 2)
3065 (let ((window (get-buffer-window (current-buffer) 0)))
3066 (if window
3067 (set-window-margins
3068 window left-margin-width right-margin-width)))))
3069 (gdb-put-string
3070 (propertize putstring
3071 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3072 (1+ start)))))
3073
3074 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3075 (gdb-remove-strings start end)
3076 (if (display-images-p)
3077 (remove-images start end))
3078 (when remove-margin
3079 (setq left-margin-width 0)
3080 (let ((window (get-buffer-window (current-buffer) 0)))
3081 (if window
3082 (set-window-margins
3083 window left-margin-width right-margin-width)))))
3084
3085 \f
3086 ;;
3087 ;; Assembler buffer.
3088 ;;
3089 (gdb-set-buffer-rules 'gdb-assembler-buffer
3090 'gdb-assembler-buffer-name
3091 'gdb-assembler-mode)
3092
3093 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3094 ;; window-start but keep the overlay arrow/current line visible.
3095 (defun gdb-assembler-handler ()
3096 (setq gdb-pending-triggers
3097 (delq 'gdb-invalidate-assembler
3098 gdb-pending-triggers))
3099 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3100 (and buf
3101 (with-current-buffer buf
3102 (let* ((window (get-buffer-window buf 0))
3103 (p (window-point window))
3104 (buffer-read-only nil))
3105 (erase-buffer)
3106 (insert-buffer-substring (gdb-get-buffer-create
3107 'gdb-partial-output-buffer))
3108 (set-window-point window p)))))
3109 ;; put customisation here
3110 (gdb-assembler-custom))
3111
3112 (defun gdb-assembler-custom ()
3113 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3114 (pos 1) (address) (flag) (bptno))
3115 (with-current-buffer buffer
3116 (save-excursion
3117 (if (not (equal gdb-pc-address "main"))
3118 (progn
3119 (goto-char (point-min))
3120 (if (and gdb-pc-address
3121 (search-forward gdb-pc-address nil t))
3122 (progn
3123 (setq pos (point))
3124 (beginning-of-line)
3125 (setq fringe-indicator-alist
3126 (if (string-equal gdb-frame-number "0")
3127 nil
3128 '((overlay-arrow . hollow-right-triangle))))
3129 (or gdb-overlay-arrow-position
3130 (setq gdb-overlay-arrow-position (make-marker)))
3131 (set-marker gdb-overlay-arrow-position
3132 (point) (current-buffer))))))
3133 ;; remove all breakpoint-icons in assembler buffer before updating.
3134 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3135 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3136 (goto-char (point-min))
3137 (while (< (point) (- (point-max) 1))
3138 (forward-line 1)
3139 (if (looking-at "[^\t].*?breakpoint")
3140 (progn
3141 (looking-at
3142 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3143 (setq bptno (match-string 1))
3144 (setq flag (char-after (match-beginning 2)))
3145 (setq address (match-string 3))
3146 (with-current-buffer buffer
3147 (save-excursion
3148 (goto-char (point-min))
3149 (if (search-forward address nil t)
3150 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3151 (if (not (equal gdb-pc-address "main"))
3152 (with-current-buffer buffer
3153 (set-window-point (get-buffer-window buffer 0) pos)))))
3154
3155 (defvar gdb-assembler-mode-map
3156 (let ((map (make-sparse-keymap)))
3157 (suppress-keymap map)
3158 (define-key map "q" 'kill-this-buffer)
3159 map))
3160
3161 (defvar gdb-assembler-font-lock-keywords
3162 '(;; <__function.name+n>
3163 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3164 (1 font-lock-function-name-face))
3165 ;; 0xNNNNNNNN <__function.name+n>: opcode
3166 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3167 (4 font-lock-keyword-face))
3168 ;; %register(at least i386)
3169 ("%\\sw+" . font-lock-variable-name-face)
3170 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3171 (1 font-lock-comment-face)
3172 (2 font-lock-function-name-face))
3173 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3174 "Font lock keywords used in `gdb-assembler-mode'.")
3175
3176 (defun gdb-assembler-mode ()
3177 "Major mode for viewing code assembler.
3178
3179 \\{gdb-assembler-mode-map}"
3180 (kill-all-local-variables)
3181 (setq major-mode 'gdb-assembler-mode)
3182 (setq mode-name (concat "Machine:" gdb-selected-frame))
3183 (setq gdb-overlay-arrow-position nil)
3184 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3185 (setq fringes-outside-margins t)
3186 (setq buffer-read-only t)
3187 (use-local-map gdb-assembler-mode-map)
3188 (gdb-invalidate-assembler)
3189 (set (make-local-variable 'font-lock-defaults)
3190 '(gdb-assembler-font-lock-keywords))
3191 (run-mode-hooks 'gdb-assembler-mode-hook)
3192 'gdb-invalidate-assembler)
3193
3194 (defun gdb-assembler-buffer-name ()
3195 (with-current-buffer gud-comint-buffer
3196 (concat "*disassembly of " (gdb-get-target-string) "*")))
3197
3198 (defun gdb-display-assembler-buffer ()
3199 "Display disassembly view."
3200 (interactive)
3201 (setq gdb-previous-frame nil)
3202 (gdb-display-buffer
3203 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3204
3205 (defun gdb-frame-assembler-buffer ()
3206 "Display disassembly view in a new frame."
3207 (interactive)
3208 (setq gdb-previous-frame nil)
3209 (let ((special-display-regexps (append special-display-regexps '(".*")))
3210 (special-display-frame-alist gdb-frame-parameters))
3211 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3212
3213 ;; modified because if gdb-pc-address has changed value a new command
3214 ;; must be enqueued to update the buffer with the new output
3215 (defun gdb-invalidate-assembler (&optional ignored)
3216 (if (gdb-get-buffer 'gdb-assembler-buffer)
3217 (progn
3218 (unless (and gdb-selected-frame
3219 (string-equal gdb-selected-frame gdb-previous-frame))
3220 (if (or (not (member 'gdb-invalidate-assembler
3221 gdb-pending-triggers))
3222 (not (string-equal gdb-pc-address
3223 gdb-previous-frame-address)))
3224 (progn
3225 ;; take previous disassemble command, if any, off the queue
3226 (with-current-buffer gud-comint-buffer
3227 (let ((queue gdb-input-queue))
3228 (dolist (item queue)
3229 (if (equal (cdr item) '(gdb-assembler-handler))
3230 (setq gdb-input-queue
3231 (delete item gdb-input-queue))))))
3232 (gdb-enqueue-input
3233 (list
3234 (concat gdb-server-prefix "disassemble "
3235 (if (member gdb-pc-address '(nil "main")) nil "0x")
3236 gdb-pc-address "\n")
3237 'gdb-assembler-handler))
3238 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3239 (setq gdb-previous-frame-address gdb-pc-address)
3240 (setq gdb-previous-frame gdb-selected-frame)))))))
3241
3242 (defun gdb-get-selected-frame ()
3243 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3244 (progn
3245 (gdb-enqueue-input
3246 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3247 (push 'gdb-get-selected-frame
3248 gdb-pending-triggers))))
3249
3250 (defun gdb-frame-handler ()
3251 (setq gdb-pending-triggers
3252 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3253 (goto-char (point-min))
3254 (when (re-search-forward
3255 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3256 (setq gdb-frame-number (match-string 1))
3257 (setq gdb-frame-address (match-string 2)))
3258 (goto-char (point-min))
3259 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3260 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3261 nil t)
3262 (setq gdb-selected-frame (match-string 2))
3263 (if (gdb-get-buffer 'gdb-locals-buffer)
3264 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3265 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3266 (if (gdb-get-buffer 'gdb-assembler-buffer)
3267 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3268 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3269 (setq gdb-pc-address (match-string 1))
3270 (if (and (match-string 3) gud-overlay-arrow-position)
3271 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3272 (position (marker-position gud-overlay-arrow-position)))
3273 (when (and buffer
3274 (string-equal (buffer-name buffer)
3275 (file-name-nondirectory (match-string 3))))
3276 (with-current-buffer buffer
3277 (setq fringe-indicator-alist
3278 (if (string-equal gdb-frame-number "0")
3279 nil
3280 '((overlay-arrow . hollow-right-triangle))))
3281 (set-marker gud-overlay-arrow-position position))))))
3282 (goto-char (point-min))
3283 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3284 (setq gdb-current-language (match-string 1)))
3285 (gdb-invalidate-assembler))
3286
3287 \f
3288 ;; Code specific to GDB 6.4
3289 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3290
3291 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3292 "Create list of source files for current GDB session.
3293 If buffers already exist for any of these files, gud-minor-mode
3294 is set in them."
3295 (goto-char (point-min))
3296 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3297 (push (match-string 1) gdb-source-file-list))
3298 (dolist (buffer (buffer-list))
3299 (with-current-buffer buffer
3300 (when (member buffer-file-name gdb-source-file-list)
3301 (set (make-local-variable 'gud-minor-mode)
3302 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3303 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3304 (when gud-tooltip-mode
3305 (make-local-variable 'gdb-define-alist)
3306 (gdb-create-define-alist)
3307 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3308 (gdb-force-mode-line-update
3309 (propertize "ready" 'face font-lock-variable-name-face)))
3310
3311 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3312 (defun gdb-var-list-children-1 (varnum)
3313 (gdb-enqueue-input
3314 (list
3315 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3316 (concat "server interpreter mi \"-var-list-children --all-values "
3317 varnum "\"\n")
3318 (concat "-var-list-children --all-values " varnum "\n"))
3319 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3320
3321 (defconst gdb-var-list-children-regexp-1
3322 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3323 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3324
3325 (defun gdb-var-list-children-handler-1 (varnum)
3326 (goto-char (point-min))
3327 (let ((var-list nil))
3328 (catch 'child-already-watched
3329 (dolist (var gdb-var-list)
3330 (if (string-equal varnum (car var))
3331 (progn
3332 (push var var-list)
3333 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3334 (let ((varchild (list (match-string 1)
3335 (match-string 2)
3336 (match-string 3)
3337 (match-string 5)
3338 (read (match-string 4))
3339 nil)))
3340 (if (assoc (car varchild) gdb-var-list)
3341 (throw 'child-already-watched nil))
3342 (push varchild var-list))))
3343 (push var var-list)))
3344 (setq gdb-var-list (nreverse var-list))))
3345 (gdb-speedbar-update))
3346
3347 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3348 (defun gdb-var-update-1 ()
3349 (if (not (member 'gdb-var-update gdb-pending-triggers))
3350 (progn
3351 (gdb-enqueue-input
3352 (list
3353 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3354 "server interpreter mi \"-var-update --all-values *\"\n"
3355 "-var-update --all-values *\n")
3356 'gdb-var-update-handler-1))
3357 (push 'gdb-var-update gdb-pending-triggers))))
3358
3359 (defconst gdb-var-update-regexp-1
3360 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3361 in_scope=\"\\(.*?\\)\".*?}")
3362
3363 (defun gdb-var-update-handler-1 ()
3364 (dolist (var gdb-var-list)
3365 (setcar (nthcdr 5 var) nil))
3366 (goto-char (point-min))
3367 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3368 (let* ((varnum (match-string 1))
3369 (var (assoc varnum gdb-var-list)))
3370 (when var
3371 (if (string-equal (match-string 3) "false")
3372 (setcar (nthcdr 5 var) 'out-of-scope)
3373 (setcar (nthcdr 5 var) 'changed)
3374 (setcar (nthcdr 4 var)
3375 (read (match-string 2)))))))
3376 (setq gdb-pending-triggers
3377 (delq 'gdb-var-update gdb-pending-triggers))
3378 (gdb-speedbar-update))
3379
3380 ;; Registers buffer.
3381 ;;
3382 (gdb-set-buffer-rules 'gdb-registers-buffer
3383 'gdb-registers-buffer-name
3384 'gdb-registers-mode)
3385
3386 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3387 (gdb-get-buffer 'gdb-registers-buffer)
3388 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3389 "server interpreter mi \"-data-list-register-values x\"\n"
3390 "-data-list-register-values x\n")
3391 gdb-data-list-register-values-handler)
3392
3393 (defconst gdb-data-list-register-values-regexp
3394 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3395
3396 (defun gdb-data-list-register-values-handler ()
3397 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3398 gdb-pending-triggers))
3399 (goto-char (point-min))
3400 (if (re-search-forward gdb-error-regexp nil t)
3401 (let ((err (match-string 1)))
3402 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3403 (let ((buffer-read-only nil))
3404 (erase-buffer)
3405 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3406 (insert err)
3407 (goto-char (point-min)))))
3408 (let ((register-list (reverse gdb-register-names))
3409 (register nil) (register-string nil) (register-values nil))
3410 (goto-char (point-min))
3411 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3412 (setq register (pop register-list))
3413 (setq register-string (concat register "\t" (match-string 2) "\n"))
3414 (if (member (match-string 1) gdb-changed-registers)
3415 (put-text-property 0 (length register-string)
3416 'face 'font-lock-warning-face
3417 register-string))
3418 (setq register-values
3419 (concat register-values register-string)))
3420 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3421 (with-current-buffer buf
3422 (let* ((window (get-buffer-window buf 0))
3423 (start (window-start window))
3424 (p (window-point window))
3425 (buffer-read-only nil))
3426 (erase-buffer)
3427 (insert register-values)
3428 (set-window-start window start)
3429 (set-window-point window p))))))
3430 (gdb-data-list-register-values-custom))
3431
3432 (defun gdb-data-list-register-values-custom ()
3433 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3434 (save-excursion
3435 (let ((buffer-read-only nil)
3436 start end)
3437 (goto-char (point-min))
3438 (while (< (point) (point-max))
3439 (setq start (line-beginning-position))
3440 (setq end (line-end-position))
3441 (when (looking-at "^[^\t]+")
3442 (unless (string-equal (match-string 0) "No registers.")
3443 (put-text-property start (match-end 0)
3444 'face font-lock-variable-name-face)
3445 (add-text-properties start end
3446 '(help-echo "mouse-2: edit value"
3447 mouse-face highlight))))
3448 (forward-line 1))))))
3449
3450 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3451 (defun gdb-get-changed-registers ()
3452 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3453 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3454 (progn
3455 (gdb-enqueue-input
3456 (list
3457 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3458 "server interpreter mi -data-list-changed-registers\n"
3459 "-data-list-changed-registers\n")
3460 'gdb-get-changed-registers-handler))
3461 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3462
3463 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3464
3465 (defun gdb-get-changed-registers-handler ()
3466 (setq gdb-pending-triggers
3467 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3468 (setq gdb-changed-registers nil)
3469 (goto-char (point-min))
3470 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3471 (push (match-string 1) gdb-changed-registers)))
3472 \f
3473
3474 ;; Locals buffer.
3475 ;;
3476 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3477 (gdb-set-buffer-rules 'gdb-locals-buffer
3478 'gdb-locals-buffer-name
3479 'gdb-locals-mode)
3480
3481 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3482 (gdb-get-buffer 'gdb-locals-buffer)
3483 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3484 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3485 "-stack-list-locals --simple-values\n")
3486 gdb-stack-list-locals-handler)
3487
3488 (defconst gdb-stack-list-locals-regexp
3489 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3490
3491 (defvar gdb-locals-watch-map-1
3492 (let ((map (make-sparse-keymap)))
3493 (define-key map [mouse-2] 'gud-watch)
3494 map)
3495 "Keymap to create watch expression of a complex data type local variable.")
3496
3497 ;; Dont display values of arrays or structures.
3498 ;; These can be expanded using gud-watch.
3499 (defun gdb-stack-list-locals-handler ()
3500 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3501 gdb-pending-triggers))
3502 (goto-char (point-min))
3503 (if (re-search-forward gdb-error-regexp nil t)
3504 (let ((err (match-string 1)))
3505 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3506 (let ((buffer-read-only nil))
3507 (erase-buffer)
3508 (insert err)
3509 (goto-char (point-min)))))
3510 (let (local locals-list)
3511 (goto-char (point-min))
3512 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3513 (let ((local (list (match-string 1)
3514 (match-string 2)
3515 nil)))
3516 (if (looking-at ",value=\\(\".*\"\\).*?}")
3517 (setcar (nthcdr 2 local) (read (match-string 1))))
3518 (push local locals-list)))
3519 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3520 (and buf (with-current-buffer buf
3521 (let* ((window (get-buffer-window buf 0))
3522 (start (window-start window))
3523 (p (window-point window))
3524 (buffer-read-only nil))
3525 (erase-buffer)
3526 (dolist (local locals-list)
3527 (setq name (car local))
3528 (if (or (not (nth 2 local))
3529 (string-match "^\\0x" (nth 2 local)))
3530 (add-text-properties 0 (length name)
3531 `(mouse-face highlight
3532 help-echo "mouse-2: create watch expression"
3533 local-map ,gdb-locals-watch-map-1)
3534 name))
3535 (insert
3536 (concat name "\t" (nth 1 local)
3537 "\t" (nth 2 local) "\n")))
3538 (set-window-start window start)
3539 (set-window-point window p))))))))
3540
3541 (defun gdb-get-register-names ()
3542 "Create a list of register names."
3543 (goto-char (point-min))
3544 (setq gdb-register-names nil)
3545 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3546 (push (match-string 1) gdb-register-names)))
3547
3548 (provide 'gdb-ui)
3549
3550 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3551 ;;; gdb-ui.el ends here