]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gdb-info-breakpoints-custom): Use different faces for enable character.
[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 (defun gdb-stopped (ignored)
1362 "An annotation handler for `stopped'.
1363 It is just like `gdb-stopping', except that if we already set the output
1364 sink to `user' in `gdb-stopping', that is fine."
1365 (setq gud-running nil)
1366 (unless (or gud-overlay-arrow-position gud-last-frame)
1367 (if (and gdb-frame-begin gdb-printing)
1368 (setq gud-overlay-arrow-position gud-old-arrow)
1369 ;;Pop up GUD buffer to display current frame when it doesn't have source
1370 ;;information i.e id not compiled with -g as with libc routines generally.
1371 (if gdb-same-frame
1372 (gdb-display-gdb-buffer)
1373 (gdb-frame-gdb-buffer))
1374 ;;Try to find source further up stack e.g after signal.
1375 (setq gdb-look-up-stack
1376 (if (gdb-get-buffer 'gdb-stack-buffer)
1377 'keep
1378 (progn
1379 (gdb-get-buffer-create 'gdb-stack-buffer)
1380 (gdb-invalidate-frames)
1381 'delete)))))
1382 (unless (member gdb-inferior-status '("exited" "signal"))
1383 (setq gdb-inferior-status "stopped")
1384 (gdb-force-mode-line-update
1385 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1386 (let ((sink gdb-output-sink))
1387 (cond
1388 ((eq sink 'inferior)
1389 (setq gdb-output-sink 'user))
1390 ((eq sink 'user) t)
1391 (t
1392 (gdb-resync)
1393 (error "Unexpected stopped annotation"))))
1394 (if gdb-signalled (gdb-exited ignored)))
1395
1396 (defun gdb-error (ignored)
1397 (setq gdb-error (not gdb-error)))
1398
1399 (defun gdb-post-prompt (ignored)
1400 "An annotation handler for `post-prompt'.
1401 This begins the collection of output from the current command if that
1402 happens to be appropriate."
1403 ;; Don't add to queue if there outstanding items or gdb-version is not known
1404 ;; yet.
1405 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1406 (gdb-get-selected-frame)
1407 (gdb-invalidate-frames)
1408 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1409 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1410 (gdb-invalidate-breakpoints)
1411 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1412 ;; so gdb-pc-address is updated.
1413 ;; (gdb-invalidate-assembler)
1414
1415 (if (string-equal gdb-version "pre-6.4")
1416 (gdb-invalidate-registers)
1417 (gdb-get-changed-registers)
1418 (gdb-invalidate-registers-1))
1419
1420 (gdb-invalidate-memory)
1421 (if (string-equal gdb-version "pre-6.4")
1422 (gdb-invalidate-locals)
1423 (gdb-invalidate-locals-1))
1424
1425 (gdb-invalidate-threads)
1426 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1427 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1428 ;; Only needed/used with speedbar/watch expressions.
1429 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1430 (if (string-equal gdb-version "pre-6.4")
1431 (gdb-var-update)
1432 (gdb-var-update-1)))))
1433 (setq gdb-first-post-prompt nil)
1434 (let ((sink gdb-output-sink))
1435 (cond
1436 ((eq sink 'user) t)
1437 ((eq sink 'pre-emacs)
1438 (setq gdb-output-sink 'emacs))
1439 (t
1440 (gdb-resync)
1441 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1442
1443 (defconst gdb-buffer-list
1444 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1445
1446 (defun gdb-remove-text-properties ()
1447 (dolist (buffertype gdb-buffer-list)
1448 (let ((buffer (gdb-get-buffer buffertype)))
1449 (if buffer
1450 (with-current-buffer buffer
1451 (let ((inhibit-read-only t))
1452 (remove-text-properties
1453 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1454
1455 ;; GUD displays the selected GDB frame. This might might not be the current
1456 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1457 ;; visited breakpoint is, use that window.
1458 (defun gdb-display-source-buffer (buffer)
1459 (let* ((last-window (if gud-last-last-frame
1460 (get-buffer-window
1461 (gud-find-file (car gud-last-last-frame)))))
1462 (source-window (or last-window
1463 (if (and gdb-source-window
1464 (window-live-p gdb-source-window))
1465 gdb-source-window))))
1466 (when source-window
1467 (setq gdb-source-window source-window)
1468 (set-window-buffer source-window buffer))
1469 source-window))
1470
1471 (defun gud-gdba-marker-filter (string)
1472 "A gud marker filter for gdb. Handle a burst of output from GDB."
1473 (if gdb-flush-pending-output
1474 nil
1475 (when gdb-enable-debug
1476 (push (cons 'recv string) gdb-debug-ring)
1477 (if (> (length gdb-debug-ring) gdb-debug-ring-max)
1478 (setcdr (nthcdr (1- gdb-debug-ring-max) gdb-debug-ring) nil)))
1479 ;; Recall the left over gud-marker-acc from last time.
1480 (setq gud-marker-acc (concat gud-marker-acc string))
1481 ;; Start accumulating output for the GUD buffer.
1482 (let ((output ""))
1483 ;;
1484 ;; Process all the complete markers in this chunk.
1485 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1486 (let ((annotation (match-string 1 gud-marker-acc)))
1487 ;;
1488 ;; Stuff prior to the match is just ordinary output.
1489 ;; It is either concatenated to OUTPUT or directed
1490 ;; elsewhere.
1491 (setq output
1492 (gdb-concat-output
1493 output
1494 (substring gud-marker-acc 0 (match-beginning 0))))
1495 ;;
1496 ;; Take that stuff off the gud-marker-acc.
1497 (setq gud-marker-acc (substring gud-marker-acc (match-end 0)))
1498 ;;
1499 ;; Parse the tag from the annotation, and maybe its arguments.
1500 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1501 (let* ((annotation-type (match-string 1 annotation))
1502 (annotation-arguments (match-string 2 annotation))
1503 (annotation-rule (assoc annotation-type
1504 gdb-annotation-rules)))
1505 ;; Call the handler for this annotation.
1506 (if annotation-rule
1507 (funcall (car (cdr annotation-rule))
1508 annotation-arguments)
1509 ;; Else the annotation is not recognized. Ignore it silently,
1510 ;; so that GDB can add new annotations without causing
1511 ;; us to blow up.
1512 ))))
1513 ;;
1514 ;; Does the remaining text end in a partial line?
1515 ;; If it does, then keep part of the gud-marker-acc until we get more.
1516 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1517 gud-marker-acc)
1518 (progn
1519 ;; Everything before the potential marker start can be output.
1520 (setq output
1521 (gdb-concat-output output
1522 (substring gud-marker-acc 0
1523 (match-beginning 0))))
1524 ;;
1525 ;; Everything after, we save, to combine with later input.
1526 (setq gud-marker-acc (substring gud-marker-acc
1527 (match-beginning 0))))
1528 ;;
1529 ;; In case we know the gud-marker-acc contains no partial annotations:
1530 (progn
1531 (setq output (gdb-concat-output output gud-marker-acc))
1532 (setq gud-marker-acc "")))
1533 output)))
1534
1535 (defun gdb-concat-output (so-far new)
1536 (if gdb-error
1537 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1538 (let ((sink gdb-output-sink))
1539 (cond
1540 ((eq sink 'user) (concat so-far new))
1541 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1542 ((eq sink 'emacs)
1543 (gdb-append-to-partial-output new)
1544 so-far)
1545 ((eq sink 'inferior)
1546 (gdb-append-to-inferior-io new)
1547 so-far)
1548 (t
1549 (gdb-resync)
1550 (error "Bogon output sink %S" sink)))))
1551
1552 (defun gdb-append-to-partial-output (string)
1553 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1554 (goto-char (point-max))
1555 (insert string)))
1556
1557 (defun gdb-clear-partial-output ()
1558 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1559 (erase-buffer)))
1560
1561 (defun gdb-append-to-inferior-io (string)
1562 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1563 (goto-char (point-max))
1564 (insert-before-markers string))
1565 (if (not (string-equal string ""))
1566 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1567
1568 (defun gdb-clear-inferior-io ()
1569 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1570 (erase-buffer)))
1571 \f
1572
1573 ;; One trick is to have a command who's output is always available in a buffer
1574 ;; of it's own, and is always up to date. We build several buffers of this
1575 ;; type.
1576 ;;
1577 ;; There are two aspects to this: gdb has to tell us when the output for that
1578 ;; command might have changed, and we have to be able to run the command
1579 ;; behind the user's back.
1580 ;;
1581 ;; The output phasing associated with the variable gdb-output-sink
1582 ;; help us to run commands behind the user's back.
1583 ;;
1584 ;; Below is the code for specificly managing buffers of output from one
1585 ;; command.
1586 ;;
1587
1588 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1589 ;; It adds an input for the command we are tracking. It should be the
1590 ;; annotation rule binding of whatever gdb sends to tell us this command
1591 ;; might have changed it's output.
1592 ;;
1593 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1594 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1595 ;; input in the input queue (see comment about ``gdb communications'' above).
1596
1597 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1598 output-handler)
1599 `(defun ,name (&optional ignored)
1600 (if (and ,demand-predicate
1601 (not (member ',name
1602 gdb-pending-triggers)))
1603 (progn
1604 (gdb-enqueue-input
1605 (list ,gdb-command ',output-handler))
1606 (push ',name gdb-pending-triggers)))))
1607
1608 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1609 `(defun ,name ()
1610 (setq gdb-pending-triggers
1611 (delq ',trigger
1612 gdb-pending-triggers))
1613 (let ((buf (gdb-get-buffer ',buf-key)))
1614 (and buf
1615 (with-current-buffer buf
1616 (let* ((window (get-buffer-window buf 0))
1617 (start (window-start window))
1618 (p (window-point window))
1619 (buffer-read-only nil))
1620 (erase-buffer)
1621 (insert-buffer-substring (gdb-get-buffer-create
1622 'gdb-partial-output-buffer))
1623 (set-window-start window start)
1624 (set-window-point window p)))))
1625 ;; put customisation here
1626 (,custom-defun)))
1627
1628 (defmacro def-gdb-auto-updated-buffer (buffer-key
1629 trigger-name gdb-command
1630 output-handler-name custom-defun)
1631 `(progn
1632 (def-gdb-auto-update-trigger ,trigger-name
1633 ;; The demand predicate:
1634 (gdb-get-buffer ',buffer-key)
1635 ,gdb-command
1636 ,output-handler-name)
1637 (def-gdb-auto-update-handler ,output-handler-name
1638 ,trigger-name ,buffer-key ,custom-defun)))
1639
1640 \f
1641 ;;
1642 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1643 ;;
1644 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1645 'gdb-breakpoints-buffer-name
1646 'gdb-breakpoints-mode)
1647
1648 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1649 ;; This defines the auto update rule for buffers of type
1650 ;; `gdb-breakpoints-buffer'.
1651 ;;
1652 ;; It defines a function to serve as the annotation handler that
1653 ;; handles the `foo-invalidated' message. That function is called:
1654 gdb-invalidate-breakpoints
1655 ;;
1656 ;; To update the buffer, this command is sent to gdb.
1657 "server info breakpoints\n"
1658 ;;
1659 ;; This also defines a function to be the handler for the output
1660 ;; from the command above. That function will copy the output into
1661 ;; the appropriately typed buffer. That function will be called:
1662 gdb-info-breakpoints-handler
1663 ;; buffer specific functions
1664 gdb-info-breakpoints-custom)
1665
1666 (defconst breakpoint-xpm-data
1667 "/* XPM */
1668 static char *magick[] = {
1669 /* columns rows colors chars-per-pixel */
1670 \"10 10 2 1\",
1671 \" c red\",
1672 \"+ c None\",
1673 /* pixels */
1674 \"+++ +++\",
1675 \"++ ++\",
1676 \"+ +\",
1677 \" \",
1678 \" \",
1679 \" \",
1680 \" \",
1681 \"+ +\",
1682 \"++ ++\",
1683 \"+++ +++\",
1684 };"
1685 "XPM data used for breakpoint icon.")
1686
1687 (defconst breakpoint-enabled-pbm-data
1688 "P1
1689 10 10\",
1690 0 0 0 0 1 1 1 1 0 0 0 0
1691 0 0 0 1 1 1 1 1 1 0 0 0
1692 0 0 1 1 1 1 1 1 1 1 0 0
1693 0 1 1 1 1 1 1 1 1 1 1 0
1694 0 1 1 1 1 1 1 1 1 1 1 0
1695 0 1 1 1 1 1 1 1 1 1 1 0
1696 0 1 1 1 1 1 1 1 1 1 1 0
1697 0 0 1 1 1 1 1 1 1 1 0 0
1698 0 0 0 1 1 1 1 1 1 0 0 0
1699 0 0 0 0 1 1 1 1 0 0 0 0"
1700 "PBM data used for enabled breakpoint icon.")
1701
1702 (defconst breakpoint-disabled-pbm-data
1703 "P1
1704 10 10\",
1705 0 0 1 0 1 0 1 0 0 0
1706 0 1 0 1 0 1 0 1 0 0
1707 1 0 1 0 1 0 1 0 1 0
1708 0 1 0 1 0 1 0 1 0 1
1709 1 0 1 0 1 0 1 0 1 0
1710 0 1 0 1 0 1 0 1 0 1
1711 1 0 1 0 1 0 1 0 1 0
1712 0 1 0 1 0 1 0 1 0 1
1713 0 0 1 0 1 0 1 0 1 0
1714 0 0 0 1 0 1 0 1 0 0"
1715 "PBM data used for disabled breakpoint icon.")
1716
1717 (defvar breakpoint-enabled-icon nil
1718 "Icon for enabled breakpoint in display margin.")
1719
1720 (defvar breakpoint-disabled-icon nil
1721 "Icon for disabled breakpoint in display margin.")
1722
1723 (and (display-images-p)
1724 ;; Bitmap for breakpoint in fringe
1725 (define-fringe-bitmap 'breakpoint
1726 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1727 ;; Bitmap for gud-overlay-arrow in fringe
1728 (define-fringe-bitmap 'hollow-right-triangle
1729 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1730
1731 (defface breakpoint-enabled
1732 '((t
1733 :foreground "red"
1734 :weight bold))
1735 "Face for enabled breakpoint icon in fringe."
1736 :group 'gud)
1737
1738 (defface breakpoint-disabled
1739 ;; We use different values of grey for different background types,
1740 ;; so that on low-color displays it will end up as something visible
1741 ;; if it has to be approximated.
1742 '((t :foreground "grey70"))
1743 "Face for disabled breakpoint icon in fringe."
1744 :group 'gud)
1745
1746 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1747 (defun gdb-info-breakpoints-custom ()
1748 (let ((flag) (bptno))
1749 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1750 (dolist (buffer (buffer-list))
1751 (with-current-buffer buffer
1752 (if (and (memq gud-minor-mode '(gdba gdbmi))
1753 (not (string-match "\\`\\*.+\\*\\'" (buffer-name))))
1754 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1755 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1756 (save-excursion
1757 (let ((buffer-read-only nil))
1758 (goto-char (point-min))
1759 (while (< (point) (- (point-max) 1))
1760 (forward-line 1)
1761 (if (looking-at "[^\t].*?breakpoint")
1762 (progn
1763 (looking-at "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)")
1764 (setq bptno (match-string 1))
1765 (setq flag (char-after (match-beginning 2)))
1766 (add-text-properties
1767 (match-beginning 2) (match-end 2)
1768 (if (eq flag ?y)
1769 '(face font-lock-warning-face)
1770 '(face font-lock-type-face)))
1771 (beginning-of-line)
1772 (if (re-search-forward " in \\(.*\\) at\\s-+" nil t)
1773 (progn
1774 (add-text-properties
1775 (match-beginning 1) (match-end 1)
1776 '(face font-lock-function-name-face))
1777 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1778 (let ((line (match-string 2))
1779 (file (match-string 1)))
1780 (add-text-properties (line-beginning-position)
1781 (line-end-position)
1782 '(mouse-face highlight
1783 help-echo "mouse-2, RET: visit breakpoint"))
1784 (unless (file-exists-p file)
1785 (setq file (cdr (assoc bptno gdb-location-alist))))
1786 (if (and file
1787 (not (string-equal file "File not found")))
1788 (with-current-buffer
1789 (find-file-noselect file 'nowarn)
1790 (set (make-local-variable 'gud-minor-mode)
1791 'gdba)
1792 (set (make-local-variable 'tool-bar-map)
1793 gud-tool-bar-map)
1794 ;; Only want one breakpoint icon at each
1795 ;; location.
1796 (save-excursion
1797 (goto-line (string-to-number line))
1798 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1799 (gdb-enqueue-input
1800 (list
1801 (concat gdb-server-prefix "list "
1802 (match-string-no-properties 1) ":1\n")
1803 'ignore))
1804 (gdb-enqueue-input
1805 (list (concat gdb-server-prefix "info source\n")
1806 `(lambda () (gdb-get-location
1807 ,bptno ,line ,flag))))))))))
1808 (end-of-line))))))
1809 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1810
1811 (defun gdb-mouse-set-clear-breakpoint (event)
1812 "Set/clear breakpoint in left fringe/margin with mouse click."
1813 (interactive "e")
1814 (mouse-minibuffer-check event)
1815 (let ((posn (event-end event)))
1816 (if (numberp (posn-point posn))
1817 (with-selected-window (posn-window posn)
1818 (save-excursion
1819 (goto-char (posn-point posn))
1820 (if (or (posn-object posn)
1821 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1822 'breakpoint))
1823 (gud-remove nil)
1824 (gud-break nil)))))))
1825
1826 (defun gdb-mouse-toggle-breakpoint-margin (event)
1827 "Enable/disable breakpoint in left margin with mouse click."
1828 (interactive "e")
1829 (mouse-minibuffer-check event)
1830 (let ((posn (event-end event)))
1831 (if (numberp (posn-point posn))
1832 (with-selected-window (posn-window posn)
1833 (save-excursion
1834 (goto-char (posn-point posn))
1835 (if (posn-object posn)
1836 (gdb-enqueue-input
1837 (list
1838 (let ((bptno (get-text-property
1839 0 'gdb-bptno (car (posn-string posn)))))
1840 (concat gdb-server-prefix
1841 (if (get-text-property
1842 0 'gdb-enabled (car (posn-string posn)))
1843 "disable "
1844 "enable ")
1845 bptno "\n"))
1846 'ignore))))))))
1847
1848 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1849 "Enable/disable breakpoint in left fringe with mouse click."
1850 (interactive "e")
1851 (mouse-minibuffer-check event)
1852 (let* ((posn (event-end event))
1853 (pos (posn-point posn))
1854 obj)
1855 (when (numberp pos)
1856 (with-selected-window (posn-window posn)
1857 (save-excursion
1858 (set-buffer (window-buffer (selected-window)))
1859 (goto-char pos)
1860 (dolist (overlay (overlays-in pos pos))
1861 (when (overlay-get overlay 'put-break)
1862 (setq obj (overlay-get overlay 'before-string))))
1863 (when (stringp obj)
1864 (gdb-enqueue-input
1865 (list
1866 (concat gdb-server-prefix
1867 (if (get-text-property 0 'gdb-enabled obj)
1868 "disable "
1869 "enable ")
1870 (get-text-property 0 'gdb-bptno obj) "\n")
1871 'ignore))))))))
1872
1873 (defun gdb-breakpoints-buffer-name ()
1874 (with-current-buffer gud-comint-buffer
1875 (concat "*breakpoints of " (gdb-get-target-string) "*")))
1876
1877 (defun gdb-display-breakpoints-buffer ()
1878 "Display status of user-settable breakpoints."
1879 (interactive)
1880 (gdb-display-buffer
1881 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
1882
1883 (defun gdb-frame-breakpoints-buffer ()
1884 "Display status of user-settable breakpoints in a new frame."
1885 (interactive)
1886 (let ((special-display-regexps (append special-display-regexps '(".*")))
1887 (special-display-frame-alist gdb-frame-parameters))
1888 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
1889
1890 (defvar gdb-breakpoints-mode-map
1891 (let ((map (make-sparse-keymap))
1892 (menu (make-sparse-keymap "Breakpoints")))
1893 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
1894 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
1895 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
1896 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
1897 (suppress-keymap map)
1898 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
1899 (define-key map " " 'gdb-toggle-breakpoint)
1900 (define-key map "D" 'gdb-delete-breakpoint)
1901 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
1902 (define-key map "q" 'gdb-delete-frame-or-window)
1903 (define-key map "\r" 'gdb-goto-breakpoint)
1904 (define-key map [mouse-2] 'gdb-goto-breakpoint)
1905 (define-key map [follow-link] 'mouse-face)
1906 map))
1907
1908 (defun gdb-delete-frame-or-window ()
1909 "Delete frame if there is only one window. Otherwise delete the window."
1910 (interactive)
1911 (if (one-window-p) (delete-frame)
1912 (delete-window)))
1913
1914 (defun gdb-breakpoints-mode ()
1915 "Major mode for gdb breakpoints.
1916
1917 \\{gdb-breakpoints-mode-map}"
1918 (kill-all-local-variables)
1919 (setq major-mode 'gdb-breakpoints-mode)
1920 (setq mode-name "Breakpoints")
1921 (use-local-map gdb-breakpoints-mode-map)
1922 (setq buffer-read-only t)
1923 (run-mode-hooks 'gdb-breakpoints-mode-hook)
1924 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1925 'gdb-invalidate-breakpoints
1926 'gdbmi-invalidate-breakpoints))
1927
1928 (defconst gdb-breakpoint-regexp
1929 "\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\s-+\\(.\\)\\s-+")
1930
1931 (defun gdb-toggle-breakpoint ()
1932 "Enable/disable breakpoint at current line."
1933 (interactive)
1934 (save-excursion
1935 (beginning-of-line 1)
1936 (if (looking-at gdb-breakpoint-regexp)
1937 (gdb-enqueue-input
1938 (list
1939 (concat gdb-server-prefix
1940 (if (eq ?y (char-after (match-beginning 2)))
1941 "disable "
1942 "enable ")
1943 (match-string 1) "\n") 'ignore))
1944 (error "Not recognized as break/watchpoint line"))))
1945
1946 (defun gdb-delete-breakpoint ()
1947 "Delete the breakpoint at current line."
1948 (interactive)
1949 (beginning-of-line 1)
1950 (if (looking-at gdb-breakpoint-regexp)
1951 (gdb-enqueue-input
1952 (list
1953 (concat gdb-server-prefix "delete " (match-string 1) "\n") 'ignore))
1954 (error "Not recognized as break/watchpoint line")))
1955
1956 (defun gdb-goto-breakpoint (&optional event)
1957 "Display the breakpoint location specified at current line."
1958 (interactive (list last-input-event))
1959 (if event (posn-set-point (event-end event)))
1960 (save-excursion
1961 (beginning-of-line 1)
1962 (if (looking-at "\\([0-9]+\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
1963 (let ((bptno (match-string 1))
1964 (file (match-string 2))
1965 (line (match-string 3)))
1966 (save-selected-window
1967 (let* ((buffer (find-file-noselect
1968 (if (file-exists-p file) file
1969 (cdr (assoc bptno gdb-location-alist)))))
1970 (window (or (gdb-display-source-buffer buffer)
1971 (display-buffer buffer))))
1972 (setq gdb-source-window window)
1973 (with-current-buffer buffer
1974 (goto-line (string-to-number line))
1975 (set-window-point window (point))))))
1976 (error "No location specified."))))
1977 \f
1978
1979 ;; Frames buffer. This displays a perpetually correct bactracktrace
1980 ;; (from the command `where').
1981 ;;
1982 ;; Alas, if your stack is deep, it is costly.
1983 ;;
1984 (gdb-set-buffer-rules 'gdb-stack-buffer
1985 'gdb-stack-buffer-name
1986 'gdb-frames-mode)
1987
1988 (def-gdb-auto-updated-buffer gdb-stack-buffer
1989 gdb-invalidate-frames
1990 "server info stack\n"
1991 gdb-info-stack-handler
1992 gdb-info-stack-custom)
1993
1994 (defun gdb-info-stack-custom ()
1995 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
1996 (save-excursion
1997 (unless (eq gdb-look-up-stack 'delete)
1998 (let ((buffer-read-only nil)
1999 bl el)
2000 (goto-char (point-min))
2001 (while (< (point) (point-max))
2002 (setq bl (line-beginning-position)
2003 el (line-end-position))
2004 (when (looking-at "#")
2005 (add-text-properties bl el
2006 '(mouse-face highlight
2007 help-echo "mouse-2, RET: Select frame")))
2008 (goto-char bl)
2009 (when (looking-at "^#\\([0-9]+\\)")
2010 (when (string-equal (match-string 1) gdb-frame-number)
2011 (put-text-property bl (+ bl 4)
2012 'face '(:inverse-video t)))
2013 (when (re-search-forward
2014 (concat
2015 (if (string-equal (match-string 1) "0") "" " in ")
2016 "\\([^ ]+\\) (") el t)
2017 (put-text-property (match-beginning 1) (match-end 1)
2018 'face font-lock-function-name-face)
2019 (setq bl (match-end 0))
2020 (while (re-search-forward "<\\([^>]+\\)>" el t)
2021 (put-text-property (match-beginning 1) (match-end 1)
2022 'face font-lock-function-name-face))
2023 (goto-char bl)
2024 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2025 (put-text-property (match-beginning 1) (match-end 1)
2026 'face font-lock-variable-name-face))))
2027 (forward-line 1))))
2028 (when gdb-look-up-stack
2029 (goto-char (point-min))
2030 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2031 (let ((start (line-beginning-position))
2032 (file (match-string 1))
2033 (line (match-string 2)))
2034 (re-search-backward "^#*\\([0-9]+\\)" start t)
2035 (gdb-enqueue-input
2036 (list (concat gdb-server-prefix "frame "
2037 (match-string 1) "\n") 'gdb-set-hollow))
2038 (gdb-enqueue-input
2039 (list (concat gdb-server-prefix "frame 0\n") 'ignore)))))))
2040 (if (eq gdb-look-up-stack 'delete)
2041 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2042 (setq gdb-look-up-stack nil))
2043
2044 (defun gdb-set-hollow ()
2045 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2046 (setq fringe-indicator-alist
2047 '((overlay-arrow . hollow-right-triangle)))))
2048
2049 (defun gdb-stack-buffer-name ()
2050 (with-current-buffer gud-comint-buffer
2051 (concat "*stack frames of " (gdb-get-target-string) "*")))
2052
2053 (defun gdb-display-stack-buffer ()
2054 "Display backtrace of current stack."
2055 (interactive)
2056 (gdb-display-buffer
2057 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2058
2059 (defun gdb-frame-stack-buffer ()
2060 "Display backtrace of current stack in a new frame."
2061 (interactive)
2062 (let ((special-display-regexps (append special-display-regexps '(".*")))
2063 (special-display-frame-alist gdb-frame-parameters))
2064 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2065
2066 (defvar gdb-frames-mode-map
2067 (let ((map (make-sparse-keymap)))
2068 (suppress-keymap map)
2069 (define-key map "q" 'kill-this-buffer)
2070 (define-key map "\r" 'gdb-frames-select)
2071 (define-key map [mouse-2] 'gdb-frames-select)
2072 (define-key map [follow-link] 'mouse-face)
2073 map))
2074
2075 (defun gdb-frames-mode ()
2076 "Major mode for gdb call stack.
2077
2078 \\{gdb-frames-mode-map}"
2079 (kill-all-local-variables)
2080 (setq major-mode 'gdb-frames-mode)
2081 (setq mode-name "Frames")
2082 (setq buffer-read-only t)
2083 (use-local-map gdb-frames-mode-map)
2084 (run-mode-hooks 'gdb-frames-mode-hook)
2085 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2086 'gdb-invalidate-frames
2087 'gdbmi-invalidate-frames))
2088
2089 (defun gdb-get-frame-number ()
2090 (save-excursion
2091 (end-of-line)
2092 (let* ((start (line-beginning-position))
2093 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2094 (n (or (and pos (match-string-no-properties 1)) "0")))
2095 n)))
2096
2097 (defun gdb-frames-select (&optional event)
2098 "Select the frame and display the relevant source."
2099 (interactive (list last-input-event))
2100 (if event (posn-set-point (event-end event)))
2101 (gdb-enqueue-input
2102 (list (concat gdb-server-prefix "frame "
2103 (gdb-get-frame-number) "\n") 'ignore)))
2104 \f
2105
2106 ;; Threads buffer. This displays a selectable thread list.
2107 ;;
2108 (gdb-set-buffer-rules 'gdb-threads-buffer
2109 'gdb-threads-buffer-name
2110 'gdb-threads-mode)
2111
2112 (def-gdb-auto-updated-buffer gdb-threads-buffer
2113 gdb-invalidate-threads
2114 (concat gdb-server-prefix "info threads\n")
2115 gdb-info-threads-handler
2116 gdb-info-threads-custom)
2117
2118 (defun gdb-info-threads-custom ()
2119 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2120 (let ((buffer-read-only nil))
2121 (save-excursion
2122 (goto-char (point-min))
2123 (while (< (point) (point-max))
2124 (unless (looking-at "No ")
2125 (add-text-properties (line-beginning-position) (line-end-position)
2126 '(mouse-face highlight
2127 help-echo "mouse-2, RET: select thread")))
2128 (forward-line 1))))))
2129
2130 (defun gdb-threads-buffer-name ()
2131 (with-current-buffer gud-comint-buffer
2132 (concat "*threads of " (gdb-get-target-string) "*")))
2133
2134 (defun gdb-display-threads-buffer ()
2135 "Display IDs of currently known threads."
2136 (interactive)
2137 (gdb-display-buffer
2138 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2139
2140 (defun gdb-frame-threads-buffer ()
2141 "Display IDs of currently known threads in a new frame."
2142 (interactive)
2143 (let ((special-display-regexps (append special-display-regexps '(".*")))
2144 (special-display-frame-alist gdb-frame-parameters))
2145 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2146
2147 (defvar gdb-threads-mode-map
2148 (let ((map (make-sparse-keymap)))
2149 (suppress-keymap map)
2150 (define-key map "q" 'kill-this-buffer)
2151 (define-key map "\r" 'gdb-threads-select)
2152 (define-key map [mouse-2] 'gdb-threads-select)
2153 (define-key map [follow-link] 'mouse-face)
2154 map))
2155
2156 (defvar gdb-threads-font-lock-keywords
2157 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2158 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2159 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2160 "Font lock keywords used in `gdb-threads-mode'.")
2161
2162 (defun gdb-threads-mode ()
2163 "Major mode for gdb threads.
2164
2165 \\{gdb-threads-mode-map}"
2166 (kill-all-local-variables)
2167 (setq major-mode 'gdb-threads-mode)
2168 (setq mode-name "Threads")
2169 (setq buffer-read-only t)
2170 (use-local-map gdb-threads-mode-map)
2171 (set (make-local-variable 'font-lock-defaults)
2172 '(gdb-threads-font-lock-keywords))
2173 (run-mode-hooks 'gdb-threads-mode-hook)
2174 'gdb-invalidate-threads)
2175
2176 (defun gdb-get-thread-number ()
2177 (save-excursion
2178 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2179 (match-string-no-properties 1)))
2180
2181 (defun gdb-threads-select (&optional event)
2182 "Select the thread and display the relevant source."
2183 (interactive (list last-input-event))
2184 (if event (posn-set-point (event-end event)))
2185 (gdb-enqueue-input
2186 (list (concat gdb-server-prefix "thread "
2187 (gdb-get-thread-number) "\n") 'ignore))
2188 (gud-display-frame))
2189 \f
2190
2191 ;; Registers buffer.
2192 ;;
2193 (defcustom gdb-all-registers nil
2194 "Non-nil means include floating-point registers."
2195 :type 'boolean
2196 :group 'gud
2197 :version "22.1")
2198
2199 (gdb-set-buffer-rules 'gdb-registers-buffer
2200 'gdb-registers-buffer-name
2201 'gdb-registers-mode)
2202
2203 (def-gdb-auto-updated-buffer gdb-registers-buffer
2204 gdb-invalidate-registers
2205 (concat
2206 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2207 gdb-info-registers-handler
2208 gdb-info-registers-custom)
2209
2210 (defun gdb-info-registers-custom ()
2211 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2212 (save-excursion
2213 (let ((buffer-read-only nil)
2214 start end)
2215 (goto-char (point-min))
2216 (while (< (point) (point-max))
2217 (setq start (line-beginning-position))
2218 (setq end (line-end-position))
2219 (when (looking-at "^[^ ]+")
2220 (unless (string-equal (match-string 0) "The")
2221 (put-text-property start (match-end 0)
2222 'face font-lock-variable-name-face)
2223 (add-text-properties start end
2224 '(help-echo "mouse-2: edit value"
2225 mouse-face highlight))))
2226 (forward-line 1))))))
2227
2228 (defun gdb-edit-register-value (&optional event)
2229 (interactive (list last-input-event))
2230 (save-excursion
2231 (if event (posn-set-point (event-end event)))
2232 (beginning-of-line)
2233 (let* ((register (current-word))
2234 (value (read-string (format "New value (%s): " register))))
2235 (gdb-enqueue-input
2236 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2237 'ignore)))))
2238
2239 (defvar gdb-registers-mode-map
2240 (let ((map (make-sparse-keymap)))
2241 (suppress-keymap map)
2242 (define-key map "\r" 'gdb-edit-register-value)
2243 (define-key map [mouse-2] 'gdb-edit-register-value)
2244 (define-key map " " 'gdb-all-registers)
2245 (define-key map "q" 'kill-this-buffer)
2246 map))
2247
2248 (defun gdb-registers-mode ()
2249 "Major mode for gdb registers.
2250
2251 \\{gdb-registers-mode-map}"
2252 (kill-all-local-variables)
2253 (setq major-mode 'gdb-registers-mode)
2254 (setq mode-name "Registers")
2255 (setq buffer-read-only t)
2256 (use-local-map gdb-registers-mode-map)
2257 (run-mode-hooks 'gdb-registers-mode-hook)
2258 (if (string-equal gdb-version "pre-6.4")
2259 (progn
2260 (if gdb-all-registers (setq mode-name "Registers:All"))
2261 'gdb-invalidate-registers)
2262 'gdb-invalidate-registers-1))
2263
2264 (defun gdb-registers-buffer-name ()
2265 (with-current-buffer gud-comint-buffer
2266 (concat "*registers of " (gdb-get-target-string) "*")))
2267
2268 (defun gdb-display-registers-buffer ()
2269 "Display integer register contents."
2270 (interactive)
2271 (gdb-display-buffer
2272 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2273
2274 (defun gdb-frame-registers-buffer ()
2275 "Display integer register contents in a new frame."
2276 (interactive)
2277 (let ((special-display-regexps (append special-display-regexps '(".*")))
2278 (special-display-frame-alist gdb-frame-parameters))
2279 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2280
2281 (defun gdb-all-registers ()
2282 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2283 (interactive)
2284 (when (string-equal gdb-version "pre-6.4")
2285 (if gdb-all-registers
2286 (progn
2287 (setq gdb-all-registers nil)
2288 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2289 (setq mode-name "Registers")))
2290 (setq gdb-all-registers t)
2291 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2292 (setq mode-name "Registers:All")))
2293 (message (format "Display of floating-point registers %sabled"
2294 (if gdb-all-registers "en" "dis")))
2295 (gdb-invalidate-registers)))
2296 \f
2297
2298 ;; Memory buffer.
2299 ;;
2300 (defcustom gdb-memory-repeat-count 32
2301 "Number of data items in memory window."
2302 :type 'integer
2303 :group 'gud
2304 :version "22.1")
2305
2306 (defcustom gdb-memory-format "x"
2307 "Display format of data items in memory window."
2308 :type '(choice (const :tag "Hexadecimal" "x")
2309 (const :tag "Signed decimal" "d")
2310 (const :tag "Unsigned decimal" "u")
2311 (const :tag "Octal" "o")
2312 (const :tag "Binary" "t"))
2313 :group 'gud
2314 :version "22.1")
2315
2316 (defcustom gdb-memory-unit "w"
2317 "Unit size of data items in memory window."
2318 :type '(choice (const :tag "Byte" "b")
2319 (const :tag "Halfword" "h")
2320 (const :tag "Word" "w")
2321 (const :tag "Giant word" "g"))
2322 :group 'gud
2323 :version "22.1")
2324
2325 (gdb-set-buffer-rules 'gdb-memory-buffer
2326 'gdb-memory-buffer-name
2327 'gdb-memory-mode)
2328
2329 (def-gdb-auto-updated-buffer gdb-memory-buffer
2330 gdb-invalidate-memory
2331 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2332 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2333 gdb-read-memory-handler
2334 gdb-read-memory-custom)
2335
2336 (defun gdb-read-memory-custom ()
2337 (save-excursion
2338 (goto-char (point-min))
2339 (if (looking-at "0x[[:xdigit:]]+")
2340 (setq gdb-memory-address (match-string 0)))))
2341
2342 (defvar gdb-memory-mode-map
2343 (let ((map (make-sparse-keymap)))
2344 (suppress-keymap map)
2345 (define-key map "q" 'kill-this-buffer)
2346 map))
2347
2348 (defun gdb-memory-set-address (event)
2349 "Set the start memory address."
2350 (interactive "e")
2351 (save-selected-window
2352 (select-window (posn-window (event-start event)))
2353 (let ((arg (read-from-minibuffer "Memory address: ")))
2354 (setq gdb-memory-address arg))
2355 (gdb-invalidate-memory)))
2356
2357 (defun gdb-memory-set-repeat-count (event)
2358 "Set the number of data items in memory window."
2359 (interactive "e")
2360 (save-selected-window
2361 (select-window (posn-window (event-start event)))
2362 (let* ((arg (read-from-minibuffer "Repeat count: "))
2363 (count (string-to-number arg)))
2364 (if (<= count 0)
2365 (error "Positive numbers only")
2366 (customize-set-variable 'gdb-memory-repeat-count count)
2367 (gdb-invalidate-memory)))))
2368
2369 (defun gdb-memory-format-binary ()
2370 "Set the display format to binary."
2371 (interactive)
2372 (customize-set-variable 'gdb-memory-format "t")
2373 (gdb-invalidate-memory))
2374
2375 (defun gdb-memory-format-octal ()
2376 "Set the display format to octal."
2377 (interactive)
2378 (customize-set-variable 'gdb-memory-format "o")
2379 (gdb-invalidate-memory))
2380
2381 (defun gdb-memory-format-unsigned ()
2382 "Set the display format to unsigned decimal."
2383 (interactive)
2384 (customize-set-variable 'gdb-memory-format "u")
2385 (gdb-invalidate-memory))
2386
2387 (defun gdb-memory-format-signed ()
2388 "Set the display format to decimal."
2389 (interactive)
2390 (customize-set-variable 'gdb-memory-format "d")
2391 (gdb-invalidate-memory))
2392
2393 (defun gdb-memory-format-hexadecimal ()
2394 "Set the display format to hexadecimal."
2395 (interactive)
2396 (customize-set-variable 'gdb-memory-format "x")
2397 (gdb-invalidate-memory))
2398
2399 (defvar gdb-memory-format-map
2400 (let ((map (make-sparse-keymap)))
2401 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2402 map)
2403 "Keymap to select format in the header line.")
2404
2405 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2406 "Menu of display formats in the header line.")
2407
2408 (define-key gdb-memory-format-menu [binary]
2409 '(menu-item "Binary" gdb-memory-format-binary
2410 :button (:radio . (equal gdb-memory-format "t"))))
2411 (define-key gdb-memory-format-menu [octal]
2412 '(menu-item "Octal" gdb-memory-format-octal
2413 :button (:radio . (equal gdb-memory-format "o"))))
2414 (define-key gdb-memory-format-menu [unsigned]
2415 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2416 :button (:radio . (equal gdb-memory-format "u"))))
2417 (define-key gdb-memory-format-menu [signed]
2418 '(menu-item "Signed Decimal" gdb-memory-format-signed
2419 :button (:radio . (equal gdb-memory-format "d"))))
2420 (define-key gdb-memory-format-menu [hexadecimal]
2421 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2422 :button (:radio . (equal gdb-memory-format "x"))))
2423
2424 (defun gdb-memory-format-menu (event)
2425 (interactive "@e")
2426 (x-popup-menu event gdb-memory-format-menu))
2427
2428 (defun gdb-memory-format-menu-1 (event)
2429 (interactive "e")
2430 (save-selected-window
2431 (select-window (posn-window (event-start event)))
2432 (let* ((selection (gdb-memory-format-menu event))
2433 (binding (and selection (lookup-key gdb-memory-format-menu
2434 (vector (car selection))))))
2435 (if binding (call-interactively binding)))))
2436
2437 (defun gdb-memory-unit-giant ()
2438 "Set the unit size to giant words (eight bytes)."
2439 (interactive)
2440 (customize-set-variable 'gdb-memory-unit "g")
2441 (gdb-invalidate-memory))
2442
2443 (defun gdb-memory-unit-word ()
2444 "Set the unit size to words (four bytes)."
2445 (interactive)
2446 (customize-set-variable 'gdb-memory-unit "w")
2447 (gdb-invalidate-memory))
2448
2449 (defun gdb-memory-unit-halfword ()
2450 "Set the unit size to halfwords (two bytes)."
2451 (interactive)
2452 (customize-set-variable 'gdb-memory-unit "h")
2453 (gdb-invalidate-memory))
2454
2455 (defun gdb-memory-unit-byte ()
2456 "Set the unit size to bytes."
2457 (interactive)
2458 (customize-set-variable 'gdb-memory-unit "b")
2459 (gdb-invalidate-memory))
2460
2461 (defvar gdb-memory-unit-map
2462 (let ((map (make-sparse-keymap)))
2463 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2464 map)
2465 "Keymap to select units in the header line.")
2466
2467 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2468 "Menu of units in the header line.")
2469
2470 (define-key gdb-memory-unit-menu [giantwords]
2471 '(menu-item "Giant words" gdb-memory-unit-giant
2472 :button (:radio . (equal gdb-memory-unit "g"))))
2473 (define-key gdb-memory-unit-menu [words]
2474 '(menu-item "Words" gdb-memory-unit-word
2475 :button (:radio . (equal gdb-memory-unit "w"))))
2476 (define-key gdb-memory-unit-menu [halfwords]
2477 '(menu-item "Halfwords" gdb-memory-unit-halfword
2478 :button (:radio . (equal gdb-memory-unit "h"))))
2479 (define-key gdb-memory-unit-menu [bytes]
2480 '(menu-item "Bytes" gdb-memory-unit-byte
2481 :button (:radio . (equal gdb-memory-unit "b"))))
2482
2483 (defun gdb-memory-unit-menu (event)
2484 (interactive "@e")
2485 (x-popup-menu event gdb-memory-unit-menu))
2486
2487 (defun gdb-memory-unit-menu-1 (event)
2488 (interactive "e")
2489 (save-selected-window
2490 (select-window (posn-window (event-start event)))
2491 (let* ((selection (gdb-memory-unit-menu event))
2492 (binding (and selection (lookup-key gdb-memory-unit-menu
2493 (vector (car selection))))))
2494 (if binding (call-interactively binding)))))
2495
2496 ;;from make-mode-line-mouse-map
2497 (defun gdb-make-header-line-mouse-map (mouse function) "\
2498 Return a keymap with single entry for mouse key MOUSE on the header line.
2499 MOUSE is defined to run function FUNCTION with no args in the buffer
2500 corresponding to the mode line clicked."
2501 (let ((map (make-sparse-keymap)))
2502 (define-key map (vector 'header-line mouse) function)
2503 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2504 map))
2505
2506 (defvar gdb-memory-font-lock-keywords
2507 '(;; <__function.name+n>
2508 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2509 )
2510 "Font lock keywords used in `gdb-memory-mode'.")
2511
2512 (defun gdb-memory-mode ()
2513 "Major mode for examining memory.
2514
2515 \\{gdb-memory-mode-map}"
2516 (kill-all-local-variables)
2517 (setq major-mode 'gdb-memory-mode)
2518 (setq mode-name "Memory")
2519 (setq buffer-read-only t)
2520 (use-local-map gdb-memory-mode-map)
2521 (setq header-line-format
2522 '(:eval
2523 (concat
2524 "Read address["
2525 (propertize
2526 "-"
2527 'face font-lock-warning-face
2528 'help-echo "mouse-1: Decrement address"
2529 'mouse-face 'mode-line-highlight
2530 'local-map
2531 (gdb-make-header-line-mouse-map
2532 'mouse-1
2533 #'(lambda () (interactive)
2534 (let ((gdb-memory-address
2535 ;; Let GDB do the arithmetic.
2536 (concat
2537 gdb-memory-address " - "
2538 (number-to-string
2539 (* gdb-memory-repeat-count
2540 (cond ((string= gdb-memory-unit "b") 1)
2541 ((string= gdb-memory-unit "h") 2)
2542 ((string= gdb-memory-unit "w") 4)
2543 ((string= gdb-memory-unit "g") 8)))))))
2544 (gdb-invalidate-memory)))))
2545 "|"
2546 (propertize "+"
2547 'face font-lock-warning-face
2548 'help-echo "mouse-1: Increment address"
2549 'mouse-face 'mode-line-highlight
2550 'local-map (gdb-make-header-line-mouse-map
2551 'mouse-1
2552 #'(lambda () (interactive)
2553 (let ((gdb-memory-address nil))
2554 (gdb-invalidate-memory)))))
2555 "]: "
2556 (propertize gdb-memory-address
2557 'face font-lock-warning-face
2558 'help-echo "mouse-1: Set memory address"
2559 'mouse-face 'mode-line-highlight
2560 'local-map (gdb-make-header-line-mouse-map
2561 'mouse-1
2562 #'gdb-memory-set-address))
2563 " Repeat Count: "
2564 (propertize (number-to-string gdb-memory-repeat-count)
2565 'face font-lock-warning-face
2566 'help-echo "mouse-1: Set repeat count"
2567 'mouse-face 'mode-line-highlight
2568 'local-map (gdb-make-header-line-mouse-map
2569 'mouse-1
2570 #'gdb-memory-set-repeat-count))
2571 " Display Format: "
2572 (propertize gdb-memory-format
2573 'face font-lock-warning-face
2574 'help-echo "mouse-3: Select display format"
2575 'mouse-face 'mode-line-highlight
2576 'local-map gdb-memory-format-map)
2577 " Unit Size: "
2578 (propertize gdb-memory-unit
2579 'face font-lock-warning-face
2580 'help-echo "mouse-3: Select unit size"
2581 'mouse-face 'mode-line-highlight
2582 'local-map gdb-memory-unit-map))))
2583 (set (make-local-variable 'font-lock-defaults)
2584 '(gdb-memory-font-lock-keywords))
2585 (run-mode-hooks 'gdb-memory-mode-hook)
2586 'gdb-invalidate-memory)
2587
2588 (defun gdb-memory-buffer-name ()
2589 (with-current-buffer gud-comint-buffer
2590 (concat "*memory of " (gdb-get-target-string) "*")))
2591
2592 (defun gdb-display-memory-buffer ()
2593 "Display memory contents."
2594 (interactive)
2595 (gdb-display-buffer
2596 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2597
2598 (defun gdb-frame-memory-buffer ()
2599 "Display memory contents in a new frame."
2600 (interactive)
2601 (let ((special-display-regexps (append special-display-regexps '(".*")))
2602 (special-display-frame-alist gdb-frame-parameters))
2603 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2604 \f
2605
2606 ;; Locals buffer.
2607 ;;
2608 (gdb-set-buffer-rules 'gdb-locals-buffer
2609 'gdb-locals-buffer-name
2610 'gdb-locals-mode)
2611
2612 (def-gdb-auto-update-trigger gdb-invalidate-locals
2613 (gdb-get-buffer 'gdb-locals-buffer)
2614 "server info locals\n"
2615 gdb-info-locals-handler)
2616
2617 (defvar gdb-locals-watch-map
2618 (let ((map (make-sparse-keymap)))
2619 (define-key map "\r" '(lambda () (interactive)
2620 (beginning-of-line)
2621 (gud-watch)))
2622 (define-key map [mouse-2] '(lambda (event) (interactive "e")
2623 (mouse-set-point event)
2624 (beginning-of-line)
2625 (gud-watch)))
2626 map)
2627 "Keymap to create watch expression of a complex data type local variable.")
2628
2629 (defconst gdb-struct-string
2630 (concat (propertize "[struct/union]"
2631 'mouse-face 'highlight
2632 'help-echo "mouse-2: create watch expression"
2633 'local-map gdb-locals-watch-map) "\n"))
2634
2635 (defconst gdb-array-string
2636 (concat " " (propertize "[array]"
2637 'mouse-face 'highlight
2638 'help-echo "mouse-2: create watch expression"
2639 'local-map gdb-locals-watch-map) "\n"))
2640
2641 ;; Abbreviate for arrays and structures.
2642 ;; These can be expanded using gud-display.
2643 (defun gdb-info-locals-handler ()
2644 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2645 gdb-pending-triggers))
2646 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2647 (with-current-buffer buf
2648 (goto-char (point-min))
2649 (while (re-search-forward "^[ }].*\n" nil t)
2650 (replace-match "" nil nil))
2651 (goto-char (point-min))
2652 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2653 (replace-match gdb-struct-string nil nil))
2654 (goto-char (point-min))
2655 (while (re-search-forward "\\s-*{.*\n" nil t)
2656 (replace-match gdb-array-string nil nil))))
2657 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2658 (and buf
2659 (with-current-buffer buf
2660 (let* ((window (get-buffer-window buf 0))
2661 (start (window-start window))
2662 (p (window-point window))
2663 (buffer-read-only nil))
2664 (erase-buffer)
2665 (insert-buffer-substring (gdb-get-buffer-create
2666 'gdb-partial-output-buffer))
2667 (set-window-start window start)
2668 (set-window-point window p))
2669 )))
2670 (run-hooks 'gdb-info-locals-hook))
2671
2672 (defvar gdb-locals-mode-map
2673 (let ((map (make-sparse-keymap)))
2674 (suppress-keymap map)
2675 (define-key map "q" 'kill-this-buffer)
2676 map))
2677
2678 (defun gdb-locals-mode ()
2679 "Major mode for gdb locals.
2680
2681 \\{gdb-locals-mode-map}"
2682 (kill-all-local-variables)
2683 (setq major-mode 'gdb-locals-mode)
2684 (setq mode-name (concat "Locals:" gdb-selected-frame))
2685 (setq buffer-read-only t)
2686 (use-local-map gdb-locals-mode-map)
2687 (set (make-local-variable 'font-lock-defaults)
2688 '(gdb-locals-font-lock-keywords))
2689 (run-mode-hooks 'gdb-locals-mode-hook)
2690 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2691 (string-equal gdb-version "pre-6.4"))
2692 'gdb-invalidate-locals
2693 'gdb-invalidate-locals-1))
2694
2695 (defun gdb-locals-buffer-name ()
2696 (with-current-buffer gud-comint-buffer
2697 (concat "*locals of " (gdb-get-target-string) "*")))
2698
2699 (defun gdb-display-locals-buffer ()
2700 "Display local variables of current stack and their values."
2701 (interactive)
2702 (gdb-display-buffer
2703 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2704
2705 (defun gdb-frame-locals-buffer ()
2706 "Display local variables of current stack and their values in a new frame."
2707 (interactive)
2708 (let ((special-display-regexps (append special-display-regexps '(".*")))
2709 (special-display-frame-alist gdb-frame-parameters))
2710 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2711 \f
2712
2713 ;;;; Window management
2714 (defun gdb-display-buffer (buf dedicated &optional size)
2715 (let ((answer (get-buffer-window buf 0))
2716 (must-split nil))
2717 (if answer
2718 (display-buffer buf nil 0) ;Raise the frame if necessary.
2719 ;; The buffer is not yet displayed.
2720 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2721 (let ((window (get-lru-window)))
2722 (if (and window
2723 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2724 ,gdb-source-window))))
2725 (progn
2726 (set-window-buffer window buf)
2727 (setq answer window))
2728 (setq must-split t)))
2729 (if must-split
2730 (let* ((largest (get-largest-window))
2731 (cur-size (window-height largest))
2732 (new-size (and size (< size cur-size) (- cur-size size))))
2733 (setq answer (split-window largest new-size))
2734 (set-window-buffer answer buf)
2735 (set-window-dedicated-p answer dedicated)))
2736 answer)))
2737
2738 \f
2739 ;;; Shared keymap initialization:
2740
2741 (let ((menu (make-sparse-keymap "GDB-Windows")))
2742 (define-key gud-menu-map [displays]
2743 `(menu-item "GDB-Windows" ,menu
2744 :visible (memq gud-minor-mode '(gdbmi gdba))))
2745 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2746 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2747 (define-key menu [inferior]
2748 '(menu-item "Inferior IO" gdb-display-separate-io-buffer
2749 :enable gdb-use-separate-io-buffer))
2750 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2751 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2752 (define-key menu [disassembly]
2753 '("Disassembly" . gdb-display-assembler-buffer))
2754 (define-key menu [breakpoints]
2755 '("Breakpoints" . gdb-display-breakpoints-buffer))
2756 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2757 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2758
2759 (let ((menu (make-sparse-keymap "GDB-Frames")))
2760 (define-key gud-menu-map [frames]
2761 `(menu-item "GDB-Frames" ,menu
2762 :visible (memq gud-minor-mode '(gdbmi gdba))))
2763 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2764 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2765 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2766 (define-key menu [inferior]
2767 '(menu-item "Inferior IO" gdb-frame-separate-io-buffer
2768 :enable gdb-use-separate-io-buffer))
2769 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2770 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2771 (define-key menu [breakpoints]
2772 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2773 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2774 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2775
2776 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2777 (define-key gud-menu-map [ui]
2778 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2779 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2780 (define-key menu [gdb-use-separate-io]
2781 '(menu-item "Separate inferior IO" gdb-use-separate-io-buffer
2782 :visible (eq gud-minor-mode 'gdba)
2783 :help "Toggle separate IO for inferior."
2784 :button (:toggle . gdb-use-separate-io-buffer)))
2785 (define-key menu [gdb-many-windows]
2786 '(menu-item "Display Other Windows" gdb-many-windows
2787 :help "Toggle display of locals, stack and breakpoint information"
2788 :button (:toggle . gdb-many-windows)))
2789 (define-key menu [gdb-restore-windows]
2790 '(menu-item "Restore Window Layout" gdb-restore-windows
2791 :help "Restore standard layout for debug session.")))
2792
2793 (defun gdb-frame-gdb-buffer ()
2794 "Display GUD buffer in a new frame."
2795 (interactive)
2796 (let ((special-display-regexps (append special-display-regexps '(".*")))
2797 (special-display-frame-alist
2798 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2799 gdb-frame-parameters)))
2800 (same-window-regexps nil))
2801 (display-buffer gud-comint-buffer)))
2802
2803 (defun gdb-display-gdb-buffer ()
2804 "Display GUD buffer."
2805 (interactive)
2806 (let ((same-window-regexps nil))
2807 (pop-to-buffer gud-comint-buffer)))
2808
2809 (defun gdb-set-window-buffer (name)
2810 (set-window-buffer (selected-window) (get-buffer name))
2811 (set-window-dedicated-p (selected-window) t))
2812
2813 (defun gdb-setup-windows ()
2814 "Layout the window pattern for `gdb-many-windows'."
2815 (gdb-display-locals-buffer)
2816 (gdb-display-stack-buffer)
2817 (delete-other-windows)
2818 (gdb-display-breakpoints-buffer)
2819 (delete-other-windows)
2820 ; Don't dedicate.
2821 (pop-to-buffer gud-comint-buffer)
2822 (split-window nil ( / ( * (window-height) 3) 4))
2823 (split-window nil ( / (window-height) 3))
2824 (split-window-horizontally)
2825 (other-window 1)
2826 (gdb-set-window-buffer (gdb-locals-buffer-name))
2827 (other-window 1)
2828 (switch-to-buffer
2829 (if gud-last-last-frame
2830 (gud-find-file (car gud-last-last-frame))
2831 (if gdb-main-file
2832 (gud-find-file gdb-main-file)
2833 ;; Put buffer list in window if we
2834 ;; can't find a source file.
2835 (list-buffers-noselect))))
2836 (setq gdb-source-window (selected-window))
2837 (when gdb-use-separate-io-buffer
2838 (split-window-horizontally)
2839 (other-window 1)
2840 (gdb-set-window-buffer
2841 (gdb-get-buffer-create 'gdb-inferior-io)))
2842 (other-window 1)
2843 (gdb-set-window-buffer (gdb-stack-buffer-name))
2844 (split-window-horizontally)
2845 (other-window 1)
2846 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
2847 (other-window 1))
2848
2849 (defun gdb-restore-windows ()
2850 "Restore the basic arrangement of windows used by gdba.
2851 This arrangement depends on the value of `gdb-many-windows'."
2852 (interactive)
2853 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
2854 (delete-other-windows)
2855 (if gdb-many-windows
2856 (gdb-setup-windows)
2857 (when (or gud-last-last-frame gdb-show-main)
2858 (split-window)
2859 (other-window 1)
2860 (switch-to-buffer
2861 (if gud-last-last-frame
2862 (gud-find-file (car gud-last-last-frame))
2863 (gud-find-file gdb-main-file)))
2864 (setq gdb-source-window (selected-window))
2865 (other-window 1))))
2866
2867 (defun gdb-reset ()
2868 "Exit a debugging session cleanly.
2869 Kills the gdb buffers, and resets variables and the source buffers."
2870 (dolist (buffer (buffer-list))
2871 (unless (eq buffer gud-comint-buffer)
2872 (with-current-buffer buffer
2873 (if (memq gud-minor-mode '(gdbmi gdba))
2874 (if (string-match "\\`\\*.+\\*\\'" (buffer-name))
2875 (kill-buffer nil)
2876 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
2877 (setq gud-minor-mode nil)
2878 (kill-local-variable 'tool-bar-map)
2879 (kill-local-variable 'gdb-define-alist))))))
2880 (when (markerp gdb-overlay-arrow-position)
2881 (move-marker gdb-overlay-arrow-position nil)
2882 (setq gdb-overlay-arrow-position nil))
2883 (setq overlay-arrow-variable-list
2884 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
2885 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
2886 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
2887 (setq gud-running nil)
2888 (setq gdb-active-process nil)
2889 (setq gdb-var-list nil)
2890 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
2891
2892 (defun gdb-source-info ()
2893 "Find the source file where the program starts and displays it with related
2894 buffers."
2895 (goto-char (point-min))
2896 (if (and (search-forward "Located in " nil t)
2897 (looking-at "\\S-+"))
2898 (setq gdb-main-file (match-string 0)))
2899 (goto-char (point-min))
2900 (if (search-forward "Includes preprocessor macro info." nil t)
2901 (setq gdb-macro-info t))
2902 (if gdb-many-windows
2903 (gdb-setup-windows)
2904 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
2905 (if gdb-show-main
2906 (let ((pop-up-windows t))
2907 (display-buffer (gud-find-file gdb-main-file))))))
2908
2909 (defun gdb-get-location (bptno line flag)
2910 "Find the directory containing the relevant source file.
2911 Put in buffer and place breakpoint icon."
2912 (goto-char (point-min))
2913 (catch 'file-not-found
2914 (if (search-forward "Located in " nil t)
2915 (when (looking-at "\\S-+")
2916 (delete (cons bptno "File not found") gdb-location-alist)
2917 (push (cons bptno (match-string 0)) gdb-location-alist))
2918 (gdb-resync)
2919 (unless (assoc bptno gdb-location-alist)
2920 (push (cons bptno "File not found") gdb-location-alist)
2921 (message-box "Cannot find source file for breakpoint location.\n\
2922 Add directory to search path for source files using the GDB command, dir."))
2923 (throw 'file-not-found nil))
2924 (with-current-buffer
2925 (find-file-noselect (match-string 0))
2926 (save-current-buffer
2927 (set (make-local-variable 'gud-minor-mode) 'gdba)
2928 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
2929 ;; only want one breakpoint icon at each location
2930 (save-excursion
2931 (goto-line (string-to-number line))
2932 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
2933
2934 (add-hook 'find-file-hook 'gdb-find-file-hook)
2935
2936 (defun gdb-find-file-hook ()
2937 "Set up buffer for debugging if file is part of the source code
2938 of the current session."
2939 (if (and (buffer-name gud-comint-buffer)
2940 ;; in case gud or gdb-ui is just loaded
2941 gud-comint-buffer
2942 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2943 '(gdba gdbmi)))
2944 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
2945 (if (member (if (string-equal gdb-version "pre-6.4")
2946 (file-name-nondirectory buffer-file-name)
2947 buffer-file-name)
2948 gdb-source-file-list)
2949 (with-current-buffer (find-buffer-visiting buffer-file-name)
2950 (set (make-local-variable 'gud-minor-mode)
2951 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
2952 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
2953
2954 ;;from put-image
2955 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
2956 "Put string PUTSTRING in front of POS in the current buffer.
2957 PUTSTRING is displayed by putting an overlay into the current buffer with a
2958 `before-string' string that has a `display' property whose value is
2959 PUTSTRING."
2960 (let ((string (make-string 1 ?x))
2961 (buffer (current-buffer)))
2962 (setq putstring (copy-sequence putstring))
2963 (let ((overlay (make-overlay pos pos buffer))
2964 (prop (or dprop
2965 (list (list 'margin 'left-margin) putstring))))
2966 (put-text-property 0 1 'display prop string)
2967 (if sprops
2968 (add-text-properties 0 1 sprops string))
2969 (overlay-put overlay 'put-break t)
2970 (overlay-put overlay 'before-string string))))
2971
2972 ;;from remove-images
2973 (defun gdb-remove-strings (start end &optional buffer)
2974 "Remove strings between START and END in BUFFER.
2975 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
2976 BUFFER nil or omitted means use the current buffer."
2977 (unless buffer
2978 (setq buffer (current-buffer)))
2979 (dolist (overlay (overlays-in start end))
2980 (when (overlay-get overlay 'put-break)
2981 (delete-overlay overlay))))
2982
2983 (defun gdb-put-breakpoint-icon (enabled bptno)
2984 (let ((start (- (line-beginning-position) 1))
2985 (end (+ (line-end-position) 1))
2986 (putstring (if enabled "B" "b"))
2987 (source-window (get-buffer-window (current-buffer) 0)))
2988 (add-text-properties
2989 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
2990 putstring)
2991 (if enabled
2992 (add-text-properties
2993 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
2994 (add-text-properties
2995 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
2996 (gdb-remove-breakpoint-icons start end)
2997 (if (display-images-p)
2998 (if (>= (or left-fringe-width
2999 (if source-window (car (window-fringes source-window)))
3000 gdb-buffer-fringe-width) 8)
3001 (gdb-put-string
3002 nil (1+ start)
3003 `(left-fringe breakpoint
3004 ,(if enabled
3005 'breakpoint-enabled
3006 'breakpoint-disabled))
3007 'gdb-bptno bptno
3008 'gdb-enabled enabled)
3009 (when (< left-margin-width 2)
3010 (save-current-buffer
3011 (setq left-margin-width 2)
3012 (if source-window
3013 (set-window-margins
3014 source-window
3015 left-margin-width right-margin-width))))
3016 (put-image
3017 (if enabled
3018 (or breakpoint-enabled-icon
3019 (setq breakpoint-enabled-icon
3020 (find-image `((:type xpm :data
3021 ,breakpoint-xpm-data
3022 :ascent 100 :pointer hand)
3023 (:type pbm :data
3024 ,breakpoint-enabled-pbm-data
3025 :ascent 100 :pointer hand)))))
3026 (or breakpoint-disabled-icon
3027 (setq breakpoint-disabled-icon
3028 (find-image `((:type xpm :data
3029 ,breakpoint-xpm-data
3030 :conversion disabled
3031 :ascent 100 :pointer hand)
3032 (:type pbm :data
3033 ,breakpoint-disabled-pbm-data
3034 :ascent 100 :pointer hand))))))
3035 (+ start 1)
3036 putstring
3037 'left-margin))
3038 (when (< left-margin-width 2)
3039 (save-current-buffer
3040 (setq left-margin-width 2)
3041 (let ((window (get-buffer-window (current-buffer) 0)))
3042 (if window
3043 (set-window-margins
3044 window left-margin-width right-margin-width)))))
3045 (gdb-put-string
3046 (propertize putstring
3047 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3048 (1+ start)))))
3049
3050 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3051 (gdb-remove-strings start end)
3052 (if (display-images-p)
3053 (remove-images start end))
3054 (when remove-margin
3055 (setq left-margin-width 0)
3056 (let ((window (get-buffer-window (current-buffer) 0)))
3057 (if window
3058 (set-window-margins
3059 window left-margin-width right-margin-width)))))
3060
3061 \f
3062 ;;
3063 ;; Assembler buffer.
3064 ;;
3065 (gdb-set-buffer-rules 'gdb-assembler-buffer
3066 'gdb-assembler-buffer-name
3067 'gdb-assembler-mode)
3068
3069 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3070 ;; window-start but keep the overlay arrow/current line visible.
3071 (defun gdb-assembler-handler ()
3072 (setq gdb-pending-triggers
3073 (delq 'gdb-invalidate-assembler
3074 gdb-pending-triggers))
3075 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3076 (and buf
3077 (with-current-buffer buf
3078 (let* ((window (get-buffer-window buf 0))
3079 (p (window-point window))
3080 (buffer-read-only nil))
3081 (erase-buffer)
3082 (insert-buffer-substring (gdb-get-buffer-create
3083 'gdb-partial-output-buffer))
3084 (set-window-point window p)))))
3085 ;; put customisation here
3086 (gdb-assembler-custom))
3087
3088 (defun gdb-assembler-custom ()
3089 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3090 (pos 1) (address) (flag) (bptno))
3091 (with-current-buffer buffer
3092 (save-excursion
3093 (if (not (equal gdb-pc-address "main"))
3094 (progn
3095 (goto-char (point-min))
3096 (if (and gdb-pc-address
3097 (search-forward gdb-pc-address nil t))
3098 (progn
3099 (setq pos (point))
3100 (beginning-of-line)
3101 (setq fringe-indicator-alist
3102 (if (string-equal gdb-frame-number "0")
3103 nil
3104 '((overlay-arrow . hollow-right-triangle))))
3105 (or gdb-overlay-arrow-position
3106 (setq gdb-overlay-arrow-position (make-marker)))
3107 (set-marker gdb-overlay-arrow-position
3108 (point) (current-buffer))))))
3109 ;; remove all breakpoint-icons in assembler buffer before updating.
3110 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3111 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3112 (goto-char (point-min))
3113 (while (< (point) (- (point-max) 1))
3114 (forward-line 1)
3115 (if (looking-at "[^\t].*?breakpoint")
3116 (progn
3117 (looking-at
3118 "\\([0-9]+\\)\\s-+\\S-+\\s-+\\S-+\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3119 (setq bptno (match-string 1))
3120 (setq flag (char-after (match-beginning 2)))
3121 (setq address (match-string 3))
3122 (with-current-buffer buffer
3123 (save-excursion
3124 (goto-char (point-min))
3125 (if (search-forward address nil t)
3126 (gdb-put-breakpoint-icon (eq flag ?y) bptno))))))))
3127 (if (not (equal gdb-pc-address "main"))
3128 (with-current-buffer buffer
3129 (set-window-point (get-buffer-window buffer 0) pos)))))
3130
3131 (defvar gdb-assembler-mode-map
3132 (let ((map (make-sparse-keymap)))
3133 (suppress-keymap map)
3134 (define-key map "q" 'kill-this-buffer)
3135 map))
3136
3137 (defvar gdb-assembler-font-lock-keywords
3138 '(;; <__function.name+n>
3139 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3140 (1 font-lock-function-name-face))
3141 ;; 0xNNNNNNNN <__function.name+n>: opcode
3142 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3143 (4 font-lock-keyword-face))
3144 ;; %register(at least i386)
3145 ("%\\sw+" . font-lock-variable-name-face)
3146 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3147 (1 font-lock-comment-face)
3148 (2 font-lock-function-name-face))
3149 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3150 "Font lock keywords used in `gdb-assembler-mode'.")
3151
3152 (defun gdb-assembler-mode ()
3153 "Major mode for viewing code assembler.
3154
3155 \\{gdb-assembler-mode-map}"
3156 (kill-all-local-variables)
3157 (setq major-mode 'gdb-assembler-mode)
3158 (setq mode-name (concat "Machine:" gdb-selected-frame))
3159 (setq gdb-overlay-arrow-position nil)
3160 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3161 (setq fringes-outside-margins t)
3162 (setq buffer-read-only t)
3163 (use-local-map gdb-assembler-mode-map)
3164 (gdb-invalidate-assembler)
3165 (set (make-local-variable 'font-lock-defaults)
3166 '(gdb-assembler-font-lock-keywords))
3167 (run-mode-hooks 'gdb-assembler-mode-hook)
3168 'gdb-invalidate-assembler)
3169
3170 (defun gdb-assembler-buffer-name ()
3171 (with-current-buffer gud-comint-buffer
3172 (concat "*disassembly of " (gdb-get-target-string) "*")))
3173
3174 (defun gdb-display-assembler-buffer ()
3175 "Display disassembly view."
3176 (interactive)
3177 (setq gdb-previous-frame nil)
3178 (gdb-display-buffer
3179 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3180
3181 (defun gdb-frame-assembler-buffer ()
3182 "Display disassembly view in a new frame."
3183 (interactive)
3184 (setq gdb-previous-frame nil)
3185 (let ((special-display-regexps (append special-display-regexps '(".*")))
3186 (special-display-frame-alist gdb-frame-parameters))
3187 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3188
3189 ;; modified because if gdb-pc-address has changed value a new command
3190 ;; must be enqueued to update the buffer with the new output
3191 (defun gdb-invalidate-assembler (&optional ignored)
3192 (if (gdb-get-buffer 'gdb-assembler-buffer)
3193 (progn
3194 (unless (and gdb-selected-frame
3195 (string-equal gdb-selected-frame gdb-previous-frame))
3196 (if (or (not (member 'gdb-invalidate-assembler
3197 gdb-pending-triggers))
3198 (not (string-equal gdb-pc-address
3199 gdb-previous-frame-address)))
3200 (progn
3201 ;; take previous disassemble command, if any, off the queue
3202 (with-current-buffer gud-comint-buffer
3203 (let ((queue gdb-input-queue))
3204 (dolist (item queue)
3205 (if (equal (cdr item) '(gdb-assembler-handler))
3206 (setq gdb-input-queue
3207 (delete item gdb-input-queue))))))
3208 (gdb-enqueue-input
3209 (list
3210 (concat gdb-server-prefix "disassemble "
3211 (if (member gdb-pc-address '(nil "main")) nil "0x")
3212 gdb-pc-address "\n")
3213 'gdb-assembler-handler))
3214 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3215 (setq gdb-previous-frame-address gdb-pc-address)
3216 (setq gdb-previous-frame gdb-selected-frame)))))))
3217
3218 (defun gdb-get-selected-frame ()
3219 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3220 (progn
3221 (gdb-enqueue-input
3222 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3223 (push 'gdb-get-selected-frame
3224 gdb-pending-triggers))))
3225
3226 (defun gdb-frame-handler ()
3227 (setq gdb-pending-triggers
3228 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3229 (goto-char (point-min))
3230 (when (re-search-forward
3231 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3232 (setq gdb-frame-number (match-string 1))
3233 (setq gdb-frame-address (match-string 2)))
3234 (goto-char (point-min))
3235 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3236 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3237 nil t)
3238 (setq gdb-selected-frame (match-string 2))
3239 (if (gdb-get-buffer 'gdb-locals-buffer)
3240 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3241 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3242 (if (gdb-get-buffer 'gdb-assembler-buffer)
3243 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3244 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3245 (setq gdb-pc-address (match-string 1))
3246 (if (and (match-string 3) gud-overlay-arrow-position)
3247 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3248 (position (marker-position gud-overlay-arrow-position)))
3249 (when (and buffer
3250 (string-equal (buffer-name buffer)
3251 (file-name-nondirectory (match-string 3))))
3252 (with-current-buffer buffer
3253 (setq fringe-indicator-alist
3254 (if (string-equal gdb-frame-number "0")
3255 nil
3256 '((overlay-arrow . hollow-right-triangle))))
3257 (set-marker gud-overlay-arrow-position position))))))
3258 (goto-char (point-min))
3259 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3260 (setq gdb-current-language (match-string 1)))
3261 (gdb-invalidate-assembler))
3262
3263 \f
3264 ;; Code specific to GDB 6.4
3265 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3266
3267 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3268 "Create list of source files for current GDB session.
3269 If buffers already exist for any of these files, gud-minor-mode
3270 is set in them."
3271 (goto-char (point-min))
3272 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3273 (push (match-string 1) gdb-source-file-list))
3274 (dolist (buffer (buffer-list))
3275 (with-current-buffer buffer
3276 (when (member buffer-file-name gdb-source-file-list)
3277 (set (make-local-variable 'gud-minor-mode)
3278 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3279 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3280 (when gud-tooltip-mode
3281 (make-local-variable 'gdb-define-alist)
3282 (gdb-create-define-alist)
3283 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3284 (gdb-force-mode-line-update
3285 (propertize "ready" 'face font-lock-variable-name-face)))
3286
3287 ; Uses "-var-list-children --all-values". Needs GDB 6.1 onwards.
3288 (defun gdb-var-list-children-1 (varnum)
3289 (gdb-enqueue-input
3290 (list
3291 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3292 (concat "server interpreter mi \"-var-list-children --all-values "
3293 varnum "\"\n")
3294 (concat "-var-list-children --all-values " varnum "\n"))
3295 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3296
3297 (defconst gdb-var-list-children-regexp-1
3298 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3299 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\),.*?type=\"\\(.+?\\)\".*?}")
3300
3301 (defun gdb-var-list-children-handler-1 (varnum)
3302 (goto-char (point-min))
3303 (let ((var-list nil))
3304 (catch 'child-already-watched
3305 (dolist (var gdb-var-list)
3306 (if (string-equal varnum (car var))
3307 (progn
3308 (push var var-list)
3309 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3310 (let ((varchild (list (match-string 1)
3311 (match-string 2)
3312 (match-string 3)
3313 (match-string 5)
3314 (read (match-string 4))
3315 nil)))
3316 (if (assoc (car varchild) gdb-var-list)
3317 (throw 'child-already-watched nil))
3318 (push varchild var-list))))
3319 (push var var-list)))
3320 (setq gdb-var-list (nreverse var-list))))
3321 (gdb-speedbar-update))
3322
3323 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3324 (defun gdb-var-update-1 ()
3325 (if (not (member 'gdb-var-update gdb-pending-triggers))
3326 (progn
3327 (gdb-enqueue-input
3328 (list
3329 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3330 "server interpreter mi \"-var-update --all-values *\"\n"
3331 "-var-update --all-values *\n")
3332 'gdb-var-update-handler-1))
3333 (push 'gdb-var-update gdb-pending-triggers))))
3334
3335 (defconst gdb-var-update-regexp-1
3336 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3337 in_scope=\"\\(.*?\\)\".*?}")
3338
3339 (defun gdb-var-update-handler-1 ()
3340 (dolist (var gdb-var-list)
3341 (setcar (nthcdr 5 var) nil))
3342 (goto-char (point-min))
3343 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3344 (let* ((varnum (match-string 1))
3345 (var (assoc varnum gdb-var-list)))
3346 (when var
3347 (if (string-equal (match-string 3) "false")
3348 (setcar (nthcdr 5 var) 'out-of-scope)
3349 (setcar (nthcdr 5 var) 'changed)
3350 (setcar (nthcdr 4 var)
3351 (read (match-string 2)))))))
3352 (setq gdb-pending-triggers
3353 (delq 'gdb-var-update gdb-pending-triggers))
3354 (gdb-speedbar-update))
3355
3356 ;; Registers buffer.
3357 ;;
3358 (gdb-set-buffer-rules 'gdb-registers-buffer
3359 'gdb-registers-buffer-name
3360 'gdb-registers-mode)
3361
3362 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3363 (gdb-get-buffer 'gdb-registers-buffer)
3364 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3365 "server interpreter mi \"-data-list-register-values x\"\n"
3366 "-data-list-register-values x\n")
3367 gdb-data-list-register-values-handler)
3368
3369 (defconst gdb-data-list-register-values-regexp
3370 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3371
3372 (defun gdb-data-list-register-values-handler ()
3373 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3374 gdb-pending-triggers))
3375 (goto-char (point-min))
3376 (if (re-search-forward gdb-error-regexp nil t)
3377 (let ((err (match-string 1)))
3378 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3379 (let ((buffer-read-only nil))
3380 (erase-buffer)
3381 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3382 (insert err)
3383 (goto-char (point-min)))))
3384 (let ((register-list (reverse gdb-register-names))
3385 (register nil) (register-string nil) (register-values nil))
3386 (goto-char (point-min))
3387 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3388 (setq register (pop register-list))
3389 (setq register-string (concat register "\t" (match-string 2) "\n"))
3390 (if (member (match-string 1) gdb-changed-registers)
3391 (put-text-property 0 (length register-string)
3392 'face 'font-lock-warning-face
3393 register-string))
3394 (setq register-values
3395 (concat register-values register-string)))
3396 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3397 (with-current-buffer buf
3398 (let* ((window (get-buffer-window buf 0))
3399 (start (window-start window))
3400 (p (window-point window))
3401 (buffer-read-only nil))
3402 (erase-buffer)
3403 (insert register-values)
3404 (set-window-start window start)
3405 (set-window-point window p))))))
3406 (gdb-data-list-register-values-custom))
3407
3408 (defun gdb-data-list-register-values-custom ()
3409 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3410 (save-excursion
3411 (let ((buffer-read-only nil)
3412 start end)
3413 (goto-char (point-min))
3414 (while (< (point) (point-max))
3415 (setq start (line-beginning-position))
3416 (setq end (line-end-position))
3417 (when (looking-at "^[^\t]+")
3418 (unless (string-equal (match-string 0) "No registers.")
3419 (put-text-property start (match-end 0)
3420 'face font-lock-variable-name-face)
3421 (add-text-properties start end
3422 '(help-echo "mouse-2: edit value"
3423 mouse-face highlight))))
3424 (forward-line 1))))))
3425
3426 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3427 (defun gdb-get-changed-registers ()
3428 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3429 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3430 (progn
3431 (gdb-enqueue-input
3432 (list
3433 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3434 "server interpreter mi -data-list-changed-registers\n"
3435 "-data-list-changed-registers\n")
3436 'gdb-get-changed-registers-handler))
3437 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3438
3439 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3440
3441 (defun gdb-get-changed-registers-handler ()
3442 (setq gdb-pending-triggers
3443 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3444 (setq gdb-changed-registers nil)
3445 (goto-char (point-min))
3446 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3447 (push (match-string 1) gdb-changed-registers)))
3448 \f
3449
3450 ;; Locals buffer.
3451 ;;
3452 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3453 (gdb-set-buffer-rules 'gdb-locals-buffer
3454 'gdb-locals-buffer-name
3455 'gdb-locals-mode)
3456
3457 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3458 (gdb-get-buffer 'gdb-locals-buffer)
3459 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3460 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3461 "-stack-list-locals --simple-values\n")
3462 gdb-stack-list-locals-handler)
3463
3464 (defconst gdb-stack-list-locals-regexp
3465 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3466
3467 (defvar gdb-locals-watch-map-1
3468 (let ((map (make-sparse-keymap)))
3469 (define-key map [mouse-2] 'gud-watch)
3470 map)
3471 "Keymap to create watch expression of a complex data type local variable.")
3472
3473 ;; Dont display values of arrays or structures.
3474 ;; These can be expanded using gud-watch.
3475 (defun gdb-stack-list-locals-handler ()
3476 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3477 gdb-pending-triggers))
3478 (goto-char (point-min))
3479 (if (re-search-forward gdb-error-regexp nil t)
3480 (let ((err (match-string 1)))
3481 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3482 (let ((buffer-read-only nil))
3483 (erase-buffer)
3484 (insert err)
3485 (goto-char (point-min)))))
3486 (let (local locals-list)
3487 (goto-char (point-min))
3488 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3489 (let ((local (list (match-string 1)
3490 (match-string 2)
3491 nil)))
3492 (if (looking-at ",value=\\(\".*\"\\).*?}")
3493 (setcar (nthcdr 2 local) (read (match-string 1))))
3494 (push local locals-list)))
3495 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3496 (and buf (with-current-buffer buf
3497 (let* ((window (get-buffer-window buf 0))
3498 (start (window-start window))
3499 (p (window-point window))
3500 (buffer-read-only nil))
3501 (erase-buffer)
3502 (dolist (local locals-list)
3503 (setq name (car local))
3504 (if (or (not (nth 2 local))
3505 (string-match "^\\0x" (nth 2 local)))
3506 (add-text-properties 0 (length name)
3507 `(mouse-face highlight
3508 help-echo "mouse-2: create watch expression"
3509 local-map ,gdb-locals-watch-map-1)
3510 name))
3511 (insert
3512 (concat name "\t" (nth 1 local)
3513 "\t" (nth 2 local) "\n")))
3514 (set-window-start window start)
3515 (set-window-point window p))))))))
3516
3517 (defun gdb-get-register-names ()
3518 "Create a list of register names."
3519 (goto-char (point-min))
3520 (setq gdb-register-names nil)
3521 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3522 (push (match-string 1) gdb-register-names)))
3523
3524 (provide 'gdb-ui)
3525
3526 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3527 ;;; gdb-ui.el ends here