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