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