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