]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gdb-ui.el
(gud-remove, gud-break): Declare as functions.
[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
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 (call-process shell-file-name
439 (if (file-exists-p file) file nil)
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-delete-1 (varnum)
885 (gdb-enqueue-input
886 (list
887 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
888 'gdba)
889 (concat "server interpreter mi \"-var-delete " varnum "\"\n")
890 (concat "-var-delete " varnum "\n"))
891 'ignore))
892 (setq gdb-var-list (delq var gdb-var-list))
893 (dolist (varchild gdb-var-list)
894 (if (string-match (concat (car var) "\\.") (car varchild))
895 (setq gdb-var-list (delq varchild gdb-var-list)))))
896
897 (defun gdb-var-delete ()
898 "Delete watch expression at point from the speedbar."
899 (interactive)
900 (if (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
901 '(gdbmi gdba))
902 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
903 (varnum (car var)))
904 (if (string-match "\\." (car var))
905 (message-box "Can only delete a root expression")
906 (gdb-var-delete-1 varnum)))))
907
908 (defun gdb-var-delete-children (varnum)
909 "Delete children of variable object at point from the speedbar."
910 (gdb-enqueue-input
911 (list
912 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
913 (concat "server interpreter mi \"-var-delete -c " varnum "\"\n")
914 (concat "-var-delete -c " varnum "\n")) 'ignore)))
915
916 (defun gdb-edit-value (text token indent)
917 "Assign a value to a variable displayed in the speedbar."
918 (let* ((var (nth (- (count-lines (point-min) (point)) 2) gdb-var-list))
919 (varnum (car var)) (value))
920 (setq value (read-string "New value: "))
921 (gdb-enqueue-input
922 (list
923 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
924 (concat "server interpreter mi \"-var-assign "
925 varnum " " value "\"\n")
926 (concat "-var-assign " varnum " " value "\n"))
927 `(lambda () (gdb-edit-value-handler ,value))))))
928
929 (defun gdb-edit-value-handler (value)
930 (goto-char (point-min))
931 (if (re-search-forward gdb-error-regexp nil t)
932 (message-box "Invalid number or expression (%s)" value)))
933
934 (defcustom gdb-show-changed-values t
935 "If non-nil change the face of out of scope variables and changed values.
936 Out of scope variables are suppressed with `shadow' face.
937 Changed values are highlighted with the face `font-lock-warning-face'."
938 :type 'boolean
939 :group 'gud
940 :version "22.1")
941
942 (defcustom gdb-max-children 40
943 "Maximum number of children before expansion requires confirmation."
944 :type 'integer
945 :group 'gud
946 :version "22.1")
947
948 (defcustom gdb-delete-out-of-scope t
949 "If non-nil delete watch expressions automatically when they go out of scope."
950 :type 'boolean
951 :group 'gud
952 :version "22.2")
953
954 (defun gdb-speedbar-expand-node (text token indent)
955 "Expand the node the user clicked on.
956 TEXT is the text of the button we clicked on, a + or - item.
957 TOKEN is data related to this node.
958 INDENT is the current indentation depth."
959 (if (and gud-comint-buffer (buffer-name gud-comint-buffer))
960 (progn
961 (cond ((string-match "+" text) ;expand this node
962 (let* ((var (assoc token gdb-var-list))
963 (expr (nth 1 var)) (children (nth 2 var)))
964 (if (or (<= (string-to-number children) gdb-max-children)
965 (y-or-n-p
966 (format
967 "%s has %s children. Continue? " expr children)))
968 (if (and (eq (buffer-local-value
969 'gud-minor-mode gud-comint-buffer) 'gdba)
970 (string-equal gdb-version "pre-6.4"))
971 (gdb-var-list-children token)
972 (gdb-var-list-children-1 token)))))
973 ((string-match "-" text) ;contract this node
974 (dolist (var gdb-var-list)
975 (if (string-match (concat token "\\.") (car var))
976 (setq gdb-var-list (delq var gdb-var-list))))
977 (gdb-var-delete-children token)
978 (speedbar-change-expand-button-char ?+)
979 (speedbar-delete-subblock indent))
980 (t (error "Ooops... not sure what to do")))
981 (speedbar-center-buffer-smartly))
982 (message-box "GUD session has been killed")))
983
984 (defun gdb-get-target-string ()
985 (with-current-buffer gud-comint-buffer
986 gud-target-name))
987 \f
988
989 ;;
990 ;; gdb buffers.
991 ;;
992 ;; Each buffer has a TYPE -- a symbol that identifies the function
993 ;; of that particular buffer.
994 ;;
995 ;; The usual gdb interaction buffer is given the type `gdba' and
996 ;; is constructed specially.
997 ;;
998 ;; Others are constructed by gdb-get-buffer-create and
999 ;; named according to the rules set forth in the gdb-buffer-rules-assoc
1000
1001 (defvar gdb-buffer-rules-assoc '())
1002
1003 (defun gdb-get-buffer (key)
1004 "Return the gdb buffer tagged with type KEY.
1005 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1006 (save-excursion
1007 (gdb-look-for-tagged-buffer key (buffer-list))))
1008
1009 (defun gdb-get-buffer-create (key)
1010 "Create a new gdb buffer of the type specified by KEY.
1011 The key should be one of the cars in `gdb-buffer-rules-assoc'."
1012 (or (gdb-get-buffer key)
1013 (let* ((rules (assoc key gdb-buffer-rules-assoc))
1014 (name (funcall (gdb-rules-name-maker rules)))
1015 (new (get-buffer-create name)))
1016 (with-current-buffer new
1017 (let ((trigger))
1018 (if (cdr (cdr rules))
1019 (setq trigger (funcall (car (cdr (cdr rules))))))
1020 (setq gdb-buffer-type key)
1021 (set (make-local-variable 'gud-minor-mode)
1022 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
1023 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
1024 (if trigger (funcall trigger)))
1025 new))))
1026
1027 (defun gdb-rules-name-maker (rules) (car (cdr rules)))
1028
1029 (defun gdb-look-for-tagged-buffer (key bufs)
1030 (let ((retval nil))
1031 (while (and (not retval) bufs)
1032 (set-buffer (car bufs))
1033 (if (eq gdb-buffer-type key)
1034 (setq retval (car bufs)))
1035 (setq bufs (cdr bufs)))
1036 retval))
1037
1038 ;;
1039 ;; This assoc maps buffer type symbols to rules. Each rule is a list of
1040 ;; at least one and possible more functions. The functions have these
1041 ;; roles in defining a buffer type:
1042 ;;
1043 ;; NAME - Return a name for this buffer type.
1044 ;;
1045 ;; The remaining function(s) are optional:
1046 ;;
1047 ;; MODE - called in a new buffer with no arguments, should establish
1048 ;; the proper mode for the buffer.
1049 ;;
1050
1051 (defun gdb-set-buffer-rules (buffer-type &rest rules)
1052 (let ((binding (assoc buffer-type gdb-buffer-rules-assoc)))
1053 (if binding
1054 (setcdr binding rules)
1055 (push (cons buffer-type rules)
1056 gdb-buffer-rules-assoc))))
1057
1058 ;; GUD buffers are an exception to the rules
1059 (gdb-set-buffer-rules 'gdba 'error)
1060
1061 ;; Partial-output buffer : This accumulates output from a command executed on
1062 ;; behalf of emacs (rather than the user).
1063 ;;
1064 (gdb-set-buffer-rules 'gdb-partial-output-buffer
1065 'gdb-partial-output-name)
1066
1067 (defun gdb-partial-output-name ()
1068 (concat " *partial-output-"
1069 (gdb-get-target-string)
1070 "*"))
1071
1072 \f
1073 (gdb-set-buffer-rules 'gdb-inferior-io
1074 'gdb-inferior-io-name
1075 'gdb-inferior-io-mode)
1076
1077 (defun gdb-inferior-io-name ()
1078 (concat "*input/output of "
1079 (gdb-get-target-string)
1080 "*"))
1081
1082 (defun gdb-display-separate-io-buffer ()
1083 "Display IO of debugged program in a separate window."
1084 (interactive)
1085 (if gdb-use-separate-io-buffer
1086 (gdb-display-buffer
1087 (gdb-get-buffer-create 'gdb-inferior-io) t)))
1088
1089 (defconst gdb-frame-parameters
1090 '((height . 14) (width . 80)
1091 (unsplittable . t)
1092 (tool-bar-lines . nil)
1093 (menu-bar-lines . nil)
1094 (minibuffer . nil)))
1095
1096 (defun gdb-frame-separate-io-buffer ()
1097 "Display IO of debugged program in a new frame."
1098 (interactive)
1099 (if gdb-use-separate-io-buffer
1100 (let ((special-display-regexps (append special-display-regexps '(".*")))
1101 (special-display-frame-alist gdb-frame-parameters))
1102 (display-buffer (gdb-get-buffer-create 'gdb-inferior-io)))))
1103
1104 (defvar gdb-inferior-io-mode-map
1105 (let ((map (make-sparse-keymap)))
1106 (define-key map "\C-c\C-c" 'gdb-separate-io-interrupt)
1107 (define-key map "\C-c\C-z" 'gdb-separate-io-stop)
1108 (define-key map "\C-c\C-\\" 'gdb-separate-io-quit)
1109 (define-key map "\C-c\C-d" 'gdb-separate-io-eof)
1110 (define-key map "\C-d" 'gdb-separate-io-eof)
1111 map))
1112
1113 (define-derived-mode gdb-inferior-io-mode comint-mode "Inferior I/O"
1114 "Major mode for gdb inferior-io."
1115 :syntax-table nil :abbrev-table nil
1116 ;; We want to use comint because it has various nifty and familiar
1117 ;; features. We don't need a process, but comint wants one, so create
1118 ;; a dummy one.
1119 (make-comint-in-buffer
1120 (substring (buffer-name) 1 (- (length (buffer-name)) 1))
1121 (current-buffer) "hexl")
1122 (setq comint-input-sender 'gdb-inferior-io-sender))
1123
1124 (defun gdb-inferior-io-sender (proc string)
1125 ;; PROC is the pseudo-process created to satisfy comint.
1126 (with-current-buffer (process-buffer proc)
1127 (setq proc (get-buffer-process gud-comint-buffer))
1128 (process-send-string proc string)
1129 (process-send-string proc "\n")))
1130
1131 (defun gdb-separate-io-interrupt ()
1132 "Interrupt the program being debugged."
1133 (interactive)
1134 (interrupt-process
1135 (get-buffer-process gud-comint-buffer) comint-ptyp))
1136
1137 (defun gdb-separate-io-quit ()
1138 "Send quit signal to the program being debugged."
1139 (interactive)
1140 (quit-process
1141 (get-buffer-process gud-comint-buffer) comint-ptyp))
1142
1143 (defun gdb-separate-io-stop ()
1144 "Stop the program being debugged."
1145 (interactive)
1146 (stop-process
1147 (get-buffer-process gud-comint-buffer) comint-ptyp))
1148
1149 (defun gdb-separate-io-eof ()
1150 "Send end-of-file to the program being debugged."
1151 (interactive)
1152 (process-send-eof
1153 (get-buffer-process gud-comint-buffer)))
1154 \f
1155
1156 ;; gdb communications
1157 ;;
1158
1159 ;; INPUT: things sent to gdb
1160 ;;
1161 ;; The queues are lists. Each element is either a string (indicating user or
1162 ;; user-like input) or a list of the form:
1163 ;;
1164 ;; (INPUT-STRING HANDLER-FN)
1165 ;;
1166 ;; The handler function will be called from the partial-output buffer when the
1167 ;; command completes. This is the way to write commands which invoke gdb
1168 ;; commands autonomously.
1169 ;;
1170 ;; These lists are consumed tail first.
1171 ;;
1172
1173 (defun gdb-send (proc string)
1174 "A comint send filter for gdb.
1175 This filter may simply queue input for a later time."
1176 (when gdb-ready
1177 (with-current-buffer gud-comint-buffer
1178 (let ((inhibit-read-only t))
1179 (remove-text-properties (point-min) (point-max) '(face))))
1180 (if gud-running
1181 (progn
1182 (let ((item (concat string "\n")))
1183 (if gdb-enable-debug (push (cons 'send item) gdb-debug-log))
1184 (process-send-string proc item)))
1185 (if (string-match "\\\\\\'" string)
1186 (setq gdb-continuation (concat gdb-continuation string "\n"))
1187 (let ((item (concat gdb-continuation string
1188 (if (not comint-input-sender-no-newline) "\n"))))
1189 (gdb-enqueue-input item)
1190 (setq gdb-continuation nil))))))
1191
1192 ;; Note: Stuff enqueued here will be sent to the next prompt, even if it
1193 ;; is a query, or other non-top-level prompt.
1194
1195 (defun gdb-enqueue-input (item)
1196 (if (not gud-running)
1197 (if gdb-prompting
1198 (progn
1199 (gdb-send-item item)
1200 (setq gdb-prompting nil))
1201 (push item gdb-input-queue))))
1202
1203 (defun gdb-dequeue-input ()
1204 (let ((queue gdb-input-queue))
1205 (and queue
1206 (let ((last (car (last queue))))
1207 (unless (nbutlast queue) (setq gdb-input-queue '()))
1208 last))))
1209
1210 (defun gdb-send-item (item)
1211 (setq gdb-flush-pending-output nil)
1212 (if gdb-enable-debug (push (cons 'send-item item) gdb-debug-log))
1213 (setq gdb-current-item item)
1214 (let ((process (get-buffer-process gud-comint-buffer)))
1215 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
1216 (if (stringp item)
1217 (progn
1218 (setq gdb-output-sink 'user)
1219 (process-send-string process item))
1220 (progn
1221 (gdb-clear-partial-output)
1222 (setq gdb-output-sink 'pre-emacs)
1223 (process-send-string process
1224 (car item))))
1225 ;; case: eq gud-minor-mode 'gdbmi
1226 (gdb-clear-partial-output)
1227 (setq gdb-output-sink 'emacs)
1228 (process-send-string process (car item)))))
1229 \f
1230 ;;
1231 ;; output -- things gdb prints to emacs
1232 ;;
1233 ;; GDB output is a stream interrupted by annotations.
1234 ;; Annotations can be recognized by their beginning
1235 ;; with \C-j\C-z\C-z<tag><opt>\C-j
1236 ;;
1237 ;; The tag is a string obeying symbol syntax.
1238 ;;
1239 ;; The optional part `<opt>' can be either the empty string
1240 ;; or a space followed by more data relating to the annotation.
1241 ;; For example, the SOURCE annotation is followed by a filename,
1242 ;; line number and various useless goo. This data must not include
1243 ;; any newlines.
1244 ;;
1245
1246 (defcustom gud-gdb-command-name "gdb --annotate=3"
1247 "Default command to execute an executable under the GDB debugger."
1248 :type 'string
1249 :group 'gud
1250 :version "22.1")
1251
1252 (defvar gdb-annotation-rules
1253 '(("pre-prompt" gdb-pre-prompt)
1254 ("prompt" gdb-prompt)
1255 ("commands" gdb-subprompt)
1256 ("overload-choice" gdb-subprompt)
1257 ("query" gdb-subprompt)
1258 ;; Need this prompt for GDB 6.1
1259 ("nquery" gdb-subprompt)
1260 ("prompt-for-continue" gdb-subprompt)
1261 ("post-prompt" gdb-post-prompt)
1262 ("source" gdb-source)
1263 ("starting" gdb-starting)
1264 ("exited" gdb-exited)
1265 ("signalled" gdb-signalled)
1266 ("signal" gdb-signal)
1267 ("breakpoint" gdb-stopping)
1268 ("watchpoint" gdb-stopping)
1269 ("frame-begin" gdb-frame-begin)
1270 ("stopped" gdb-stopped)
1271 ("error-begin" gdb-error)
1272 ("error" gdb-error)
1273 ) "An assoc mapping annotation tags to functions which process them.")
1274
1275 (defun gdb-resync()
1276 (setq gdb-flush-pending-output t)
1277 (setq gud-running nil)
1278 (gdb-force-mode-line-update
1279 (propertize "stopped"'face font-lock-warning-face))
1280 (setq gdb-output-sink 'user)
1281 (setq gdb-input-queue nil)
1282 (setq gdb-pending-triggers nil)
1283 (setq gdb-prompting t))
1284
1285 (defconst gdb-source-spec-regexp
1286 "\\(.*\\):\\([0-9]*\\):[0-9]*:[a-z]*:0x0*\\([a-f0-9]*\\)")
1287
1288 ;; Do not use this except as an annotation handler.
1289 (defun gdb-source (args)
1290 (string-match gdb-source-spec-regexp args)
1291 ;; Extract the frame position from the marker.
1292 (setq gud-last-frame
1293 (cons
1294 (match-string 1 args)
1295 (string-to-number (match-string 2 args))))
1296 (setq gdb-pc-address (match-string 3 args))
1297 ;; cover for auto-display output which comes *before*
1298 ;; stopped annotation
1299 (if (eq gdb-output-sink 'inferior) (setq gdb-output-sink 'user)))
1300
1301 (defun gdb-pre-prompt (ignored)
1302 "An annotation handler for `pre-prompt'.
1303 This terminates the collection of output from a previous command if that
1304 happens to be in effect."
1305 (setq gdb-error nil)
1306 (let ((sink gdb-output-sink))
1307 (cond
1308 ((eq sink 'user) t)
1309 ((eq sink 'emacs)
1310 (setq gdb-output-sink 'post-emacs))
1311 (t
1312 (gdb-resync)
1313 (error "Phase error in gdb-pre-prompt (got %s)" sink)))))
1314
1315 (defun gdb-prompt (ignored)
1316 "An annotation handler for `prompt'.
1317 This sends the next command (if any) to gdb."
1318 (when gdb-first-prompt
1319 (gdb-force-mode-line-update
1320 (propertize "initializing..." 'face font-lock-variable-name-face))
1321 (gdb-init-1)
1322 (setq gdb-first-prompt nil))
1323 (let ((sink gdb-output-sink))
1324 (cond
1325 ((eq sink 'user) t)
1326 ((eq sink 'post-emacs)
1327 (setq gdb-output-sink 'user)
1328 (let ((handler
1329 (car (cdr gdb-current-item))))
1330 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1331 (funcall handler))))
1332 (t
1333 (gdb-resync)
1334 (error "Phase error in gdb-prompt (got %s)" sink))))
1335 (let ((input (gdb-dequeue-input)))
1336 (if input
1337 (gdb-send-item input)
1338 (progn
1339 (setq gdb-prompting t)
1340 (gud-display-frame)))))
1341
1342 (defun gdb-subprompt (ignored)
1343 "An annotation handler for non-top-level prompts."
1344 (setq gdb-prompting t))
1345
1346 (defun gdb-starting (ignored)
1347 "An annotation handler for `starting'.
1348 This says that I/O for the subprocess is now the program being debugged,
1349 not GDB."
1350 (setq gdb-active-process t)
1351 (setq gdb-printing t)
1352 (let ((sink gdb-output-sink))
1353 (cond
1354 ((eq sink 'user)
1355 (progn
1356 (setq gud-running t)
1357 (setq gdb-inferior-status "running")
1358 (setq gdb-signalled nil)
1359 (gdb-force-mode-line-update
1360 (propertize gdb-inferior-status 'face font-lock-type-face))
1361 (gdb-remove-text-properties)
1362 (setq gud-old-arrow gud-overlay-arrow-position)
1363 (setq gud-overlay-arrow-position nil)
1364 (setq gdb-overlay-arrow-position nil)
1365 (setq gdb-stack-position nil)
1366 (if gdb-use-separate-io-buffer
1367 (setq gdb-output-sink 'inferior))))
1368 (t
1369 (gdb-resync)
1370 (error "Unexpected `starting' annotation")))))
1371
1372 (defun gdb-signal (ignored)
1373 (setq gdb-inferior-status "signal")
1374 (gdb-force-mode-line-update
1375 (propertize gdb-inferior-status 'face font-lock-warning-face))
1376 (gdb-stopping ignored))
1377
1378 (defun gdb-stopping (ignored)
1379 "An annotation handler for `breakpoint' and other annotations.
1380 They say that I/O for the subprocess is now GDB, not the program
1381 being debugged."
1382 (if gdb-use-separate-io-buffer
1383 (let ((sink gdb-output-sink))
1384 (cond
1385 ((eq sink 'inferior)
1386 (setq gdb-output-sink 'user))
1387 (t
1388 (gdb-resync)
1389 (error "Unexpected stopping annotation"))))))
1390
1391 (defun gdb-exited (ignored)
1392 "An annotation handler for `exited' and `signalled'.
1393 They say that I/O for the subprocess is now GDB, not the program
1394 being debugged and that the program is no longer running. This
1395 function is used to change the focus of GUD tooltips to #define
1396 directives."
1397 (setq gdb-active-process nil)
1398 (setq gud-overlay-arrow-position nil)
1399 (setq gdb-overlay-arrow-position nil)
1400 (setq gdb-stack-position nil)
1401 (setq gud-old-arrow nil)
1402 (setq gdb-inferior-status "exited")
1403 (gdb-force-mode-line-update
1404 (propertize gdb-inferior-status 'face font-lock-warning-face))
1405 (gdb-stopping ignored))
1406
1407 (defun gdb-signalled (ignored)
1408 (setq gdb-signalled t))
1409
1410 (defun gdb-frame-begin (ignored)
1411 (setq gdb-frame-begin t)
1412 (setq gdb-printing nil)
1413 (let ((sink gdb-output-sink))
1414 (cond
1415 ((eq sink 'inferior)
1416 (setq gdb-output-sink 'user))
1417 ((eq sink 'user) t)
1418 ((eq sink 'emacs) t)
1419 (t
1420 (gdb-resync)
1421 (error "Unexpected frame-begin annotation (%S)" sink)))))
1422
1423 (defcustom gdb-same-frame focus-follows-mouse
1424 "Non-nil means pop up GUD buffer in same frame."
1425 :group 'gud
1426 :type 'boolean
1427 :version "22.1")
1428
1429 (defcustom gdb-find-source-frame nil
1430 "Non-nil means try to find a source frame further up stack e.g after signal."
1431 :group 'gud
1432 :type 'boolean
1433 :version "22.1")
1434
1435 (defun gdb-find-source-frame (arg)
1436 "Toggle trying to find a source frame further up stack.
1437 With prefix argument ARG, look for a source frame further up
1438 stack if ARG is positive, otherwise don't look further up."
1439 (interactive "P")
1440 (setq gdb-find-source-frame
1441 (if (null arg)
1442 (not gdb-find-source-frame)
1443 (> (prefix-numeric-value arg) 0)))
1444 (message (format "Looking for source frame %sabled"
1445 (if gdb-find-source-frame "en" "dis"))))
1446
1447 (defun gdb-stopped (ignored)
1448 "An annotation handler for `stopped'.
1449 It is just like `gdb-stopping', except that if we already set the output
1450 sink to `user' in `gdb-stopping', that is fine."
1451 (setq gud-running nil)
1452 (unless (or gud-overlay-arrow-position gud-last-frame)
1453 (if (and gdb-frame-begin gdb-printing)
1454 (setq gud-overlay-arrow-position gud-old-arrow)
1455 ;;Pop up GUD buffer to display current frame when it doesn't have source
1456 ;;information i.e if not compiled with -g as with libc routines generally.
1457 (if gdb-same-frame
1458 (gdb-display-gdb-buffer)
1459 (gdb-frame-gdb-buffer))
1460 (if gdb-find-source-frame
1461 ;;Try to find source further up stack e.g after signal.
1462 (setq gdb-look-up-stack
1463 (if (gdb-get-buffer 'gdb-stack-buffer)
1464 'keep
1465 (progn
1466 (gdb-get-buffer-create 'gdb-stack-buffer)
1467 (gdb-invalidate-frames)
1468 'delete))))))
1469 (unless (member gdb-inferior-status '("exited" "signal"))
1470 (setq gdb-active-process t) ;Just for attaching case.
1471 (setq gdb-inferior-status "stopped")
1472 (gdb-force-mode-line-update
1473 (propertize gdb-inferior-status 'face font-lock-warning-face)))
1474 (let ((sink gdb-output-sink))
1475 (cond
1476 ((eq sink 'inferior)
1477 (setq gdb-output-sink 'user))
1478 ((eq sink 'user) t)
1479 (t
1480 (gdb-resync)
1481 (error "Unexpected stopped annotation"))))
1482 (if gdb-signalled (gdb-exited ignored)))
1483
1484 (defun gdb-error (ignored)
1485 (setq gdb-error (not gdb-error)))
1486
1487 (defun gdb-post-prompt (ignored)
1488 "An annotation handler for `post-prompt'.
1489 This begins the collection of output from the current command if that
1490 happens to be appropriate."
1491 ;; Don't add to queue if there outstanding items or gdb-version is not known
1492 ;; yet.
1493 (unless (or gdb-pending-triggers gdb-first-post-prompt)
1494 (gdb-get-selected-frame)
1495 (gdb-invalidate-frames)
1496 ;; Regenerate breakpoints buffer in case it has been inadvertantly deleted.
1497 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
1498 (gdb-invalidate-breakpoints)
1499 ;; Do this through gdb-get-selected-frame -> gdb-frame-handler
1500 ;; so gdb-pc-address is updated.
1501 ;; (gdb-invalidate-assembler)
1502
1503 (if (string-equal gdb-version "pre-6.4")
1504 (gdb-invalidate-registers)
1505 (gdb-get-changed-registers)
1506 (gdb-invalidate-registers-1))
1507
1508 (gdb-invalidate-memory)
1509 (if (string-equal gdb-version "pre-6.4")
1510 (gdb-invalidate-locals)
1511 (gdb-invalidate-locals-1))
1512
1513 (gdb-invalidate-threads)
1514 (unless (eq system-type 'darwin) ;Breaks on Darwin's GDB-5.3.
1515 ;; FIXME: with GDB-6 on Darwin, this might very well work.
1516 ;; Only needed/used with speedbar/watch expressions.
1517 (when (and (boundp 'speedbar-frame) (frame-live-p speedbar-frame))
1518 (if (string-equal gdb-version "pre-6.4")
1519 (gdb-var-update)
1520 (gdb-var-update-1)))))
1521 (setq gdb-first-post-prompt nil)
1522 (let ((sink gdb-output-sink))
1523 (cond
1524 ((eq sink 'user) t)
1525 ((eq sink 'pre-emacs)
1526 (setq gdb-output-sink 'emacs))
1527 (t
1528 (gdb-resync)
1529 (error "Phase error in gdb-post-prompt (got %s)" sink)))))
1530
1531 (defconst gdb-buffer-list
1532 '(gdb-stack-buffer gdb-locals-buffer gdb-registers-buffer gdb-threads-buffer))
1533
1534 (defun gdb-remove-text-properties ()
1535 (dolist (buffertype gdb-buffer-list)
1536 (let ((buffer (gdb-get-buffer buffertype)))
1537 (if buffer
1538 (with-current-buffer buffer
1539 (let ((inhibit-read-only t))
1540 (remove-text-properties
1541 (point-min) (point-max) '(mouse-face nil help-echo nil))))))))
1542
1543 ;; GUD displays the selected GDB frame. This might might not be the current
1544 ;; GDB frame (after up, down etc). If no GDB frame is visible but the last
1545 ;; visited breakpoint is, use that window.
1546 (defun gdb-display-source-buffer (buffer)
1547 (let* ((last-window (if gud-last-last-frame
1548 (get-buffer-window
1549 (gud-find-file (car gud-last-last-frame)))))
1550 (source-window (or last-window
1551 (if (and gdb-source-window
1552 (window-live-p gdb-source-window))
1553 gdb-source-window))))
1554 (when source-window
1555 (setq gdb-source-window source-window)
1556 (set-window-buffer source-window buffer))
1557 source-window))
1558
1559 ;; Derived from gud-gdb-marker-regexp
1560 (defvar gdb-fullname-regexp
1561 (concat "\\(.:?[^" ":" "\n]*\\)" ":" "\\([0-9]*\\)" ":" ".*"))
1562
1563 (defun gud-gdba-marker-filter (string)
1564 "A gud marker filter for gdb. Handle a burst of output from GDB."
1565 (if gdb-flush-pending-output
1566 nil
1567 (when gdb-enable-debug
1568 (push (cons 'recv string) gdb-debug-log)
1569 (if (and gdb-debug-log-max
1570 (> (length gdb-debug-log) gdb-debug-log-max))
1571 (setcdr (nthcdr (1- gdb-debug-log-max) gdb-debug-log) nil)))
1572 ;; Recall the left over gud-marker-acc from last time.
1573 (setq gud-marker-acc (concat gud-marker-acc string))
1574 ;; Start accumulating output for the GUD buffer.
1575 (let ((output ""))
1576 ;;
1577 ;; Process all the complete markers in this chunk.
1578 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
1579 (let ((annotation (match-string 1 gud-marker-acc))
1580 (before (substring gud-marker-acc 0 (match-beginning 0)))
1581 (after (substring gud-marker-acc (match-end 0))))
1582 ;;
1583 ;; Parse the tag from the annotation, and maybe its arguments.
1584 (string-match "\\(\\S-*\\) ?\\(.*\\)" annotation)
1585 (let* ((annotation-type (match-string 1 annotation))
1586 (annotation-arguments (match-string 2 annotation))
1587 (annotation-rule (assoc annotation-type
1588 gdb-annotation-rules))
1589 (fullname (string-match gdb-fullname-regexp annotation-type)))
1590
1591 ;; Stuff prior to the match is just ordinary output.
1592 ;; It is either concatenated to OUTPUT or directed
1593 ;; elsewhere.
1594 (setq output
1595 (gdb-concat-output output
1596 (concat before (if fullname "\n"))))
1597
1598 ;; Take that stuff off the gud-marker-acc.
1599 (setq gud-marker-acc after)
1600
1601 ;; Call the handler for this annotation.
1602 (if annotation-rule
1603 (funcall (car (cdr annotation-rule))
1604 annotation-arguments)
1605
1606 ;; Switch to gud-gdb-marker-filter if appropriate.
1607 (when fullname
1608
1609 ;; Extract the frame position from the marker.
1610 (setq gud-last-frame (cons (match-string 1 annotation)
1611 (string-to-number
1612 (match-string 2 annotation))))
1613
1614 (set (make-local-variable 'gud-minor-mode) 'gdb)
1615 (set (make-local-variable 'gud-marker-filter)
1616 'gud-gdb-marker-filter)))
1617
1618 ;; Else the annotation is not recognized. Ignore it silently,
1619 ;; so that GDB can add new annotations without causing
1620 ;; us to blow up.
1621 )))
1622
1623 ;; Does the remaining text end in a partial line?
1624 ;; If it does, then keep part of the gud-marker-acc until we get more.
1625 (if (string-match "\n\\'\\|\n\032\\'\\|\n\032\032.*\\'"
1626 gud-marker-acc)
1627 (progn
1628 ;; Everything before the potential marker start can be output.
1629 (setq output
1630 (gdb-concat-output output
1631 (substring gud-marker-acc 0
1632 (match-beginning 0))))
1633 ;;
1634 ;; Everything after, we save, to combine with later input.
1635 (setq gud-marker-acc (substring gud-marker-acc
1636 (match-beginning 0))))
1637 ;;
1638 ;; In case we know the gud-marker-acc contains no partial annotations:
1639 (progn
1640 (setq output (gdb-concat-output output gud-marker-acc))
1641 (setq gud-marker-acc "")))
1642 output)))
1643
1644 (defun gdb-concat-output (so-far new)
1645 (if gdb-error
1646 (put-text-property 0 (length new) 'face font-lock-warning-face new))
1647 (let ((sink gdb-output-sink))
1648 (cond
1649 ((eq sink 'user) (concat so-far new))
1650 ((or (eq sink 'pre-emacs) (eq sink 'post-emacs)) so-far)
1651 ((eq sink 'emacs)
1652 (gdb-append-to-partial-output new)
1653 so-far)
1654 ((eq sink 'inferior)
1655 (gdb-append-to-inferior-io new)
1656 so-far)
1657 (t
1658 (gdb-resync)
1659 (error "Bogon output sink %S" sink)))))
1660
1661 (defun gdb-append-to-partial-output (string)
1662 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1663 (goto-char (point-max))
1664 (insert string)))
1665
1666 (defun gdb-clear-partial-output ()
1667 (with-current-buffer (gdb-get-buffer-create 'gdb-partial-output-buffer)
1668 (erase-buffer)))
1669
1670 (defun gdb-append-to-inferior-io (string)
1671 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1672 (goto-char (point-max))
1673 (insert-before-markers string))
1674 (if (not (string-equal string ""))
1675 (gdb-display-buffer (gdb-get-buffer-create 'gdb-inferior-io) t)))
1676
1677 (defun gdb-clear-inferior-io ()
1678 (with-current-buffer (gdb-get-buffer-create 'gdb-inferior-io)
1679 (erase-buffer)))
1680 \f
1681
1682 ;; One trick is to have a command who's output is always available in a buffer
1683 ;; of it's own, and is always up to date. We build several buffers of this
1684 ;; type.
1685 ;;
1686 ;; There are two aspects to this: gdb has to tell us when the output for that
1687 ;; command might have changed, and we have to be able to run the command
1688 ;; behind the user's back.
1689 ;;
1690 ;; The output phasing associated with the variable gdb-output-sink
1691 ;; help us to run commands behind the user's back.
1692 ;;
1693 ;; Below is the code for specificly managing buffers of output from one
1694 ;; command.
1695 ;;
1696
1697 ;; The trigger function is suitable for use in the assoc GDB-ANNOTATION-RULES
1698 ;; It adds an input for the command we are tracking. It should be the
1699 ;; annotation rule binding of whatever gdb sends to tell us this command
1700 ;; might have changed it's output.
1701 ;;
1702 ;; NAME is the function name. DEMAND-PREDICATE tests if output is really needed.
1703 ;; GDB-COMMAND is a string of such. OUTPUT-HANDLER is the function bound to the
1704 ;; input in the input queue (see comment about ``gdb communications'' above).
1705
1706 (defmacro def-gdb-auto-update-trigger (name demand-predicate gdb-command
1707 output-handler)
1708 `(defun ,name (&optional ignored)
1709 (if (and ,demand-predicate
1710 (not (member ',name
1711 gdb-pending-triggers)))
1712 (progn
1713 (gdb-enqueue-input
1714 (list ,gdb-command ',output-handler))
1715 (push ',name gdb-pending-triggers)))))
1716
1717 (defmacro def-gdb-auto-update-handler (name trigger buf-key custom-defun)
1718 `(defun ,name ()
1719 (setq gdb-pending-triggers
1720 (delq ',trigger
1721 gdb-pending-triggers))
1722 (let ((buf (gdb-get-buffer ',buf-key)))
1723 (and buf
1724 (with-current-buffer buf
1725 (let* ((window (get-buffer-window buf 0))
1726 (start (window-start window))
1727 (p (window-point window))
1728 (buffer-read-only nil))
1729 (erase-buffer)
1730 (insert-buffer-substring (gdb-get-buffer-create
1731 'gdb-partial-output-buffer))
1732 (set-window-start window start)
1733 (set-window-point window p)))))
1734 ;; put customisation here
1735 (,custom-defun)))
1736
1737 (defmacro def-gdb-auto-updated-buffer (buffer-key
1738 trigger-name gdb-command
1739 output-handler-name custom-defun)
1740 `(progn
1741 (def-gdb-auto-update-trigger ,trigger-name
1742 ;; The demand predicate:
1743 (gdb-get-buffer ',buffer-key)
1744 ,gdb-command
1745 ,output-handler-name)
1746 (def-gdb-auto-update-handler ,output-handler-name
1747 ,trigger-name ,buffer-key ,custom-defun)))
1748
1749 \f
1750 ;;
1751 ;; Breakpoint buffer : This displays the output of `info breakpoints'.
1752 ;;
1753 (gdb-set-buffer-rules 'gdb-breakpoints-buffer
1754 'gdb-breakpoints-buffer-name
1755 'gdb-breakpoints-mode)
1756
1757 (def-gdb-auto-updated-buffer gdb-breakpoints-buffer
1758 ;; This defines the auto update rule for buffers of type
1759 ;; `gdb-breakpoints-buffer'.
1760 ;;
1761 ;; It defines a function to serve as the annotation handler that
1762 ;; handles the `foo-invalidated' message. That function is called:
1763 gdb-invalidate-breakpoints
1764 ;;
1765 ;; To update the buffer, this command is sent to gdb.
1766 "server info breakpoints\n"
1767 ;;
1768 ;; This also defines a function to be the handler for the output
1769 ;; from the command above. That function will copy the output into
1770 ;; the appropriately typed buffer. That function will be called:
1771 gdb-info-breakpoints-handler
1772 ;; buffer specific functions
1773 gdb-info-breakpoints-custom)
1774
1775 (defconst breakpoint-xpm-data
1776 "/* XPM */
1777 static char *magick[] = {
1778 /* columns rows colors chars-per-pixel */
1779 \"10 10 2 1\",
1780 \" c red\",
1781 \"+ c None\",
1782 /* pixels */
1783 \"+++ +++\",
1784 \"++ ++\",
1785 \"+ +\",
1786 \" \",
1787 \" \",
1788 \" \",
1789 \" \",
1790 \"+ +\",
1791 \"++ ++\",
1792 \"+++ +++\",
1793 };"
1794 "XPM data used for breakpoint icon.")
1795
1796 (defconst breakpoint-enabled-pbm-data
1797 "P1
1798 10 10\",
1799 0 0 0 0 1 1 1 1 0 0 0 0
1800 0 0 0 1 1 1 1 1 1 0 0 0
1801 0 0 1 1 1 1 1 1 1 1 0 0
1802 0 1 1 1 1 1 1 1 1 1 1 0
1803 0 1 1 1 1 1 1 1 1 1 1 0
1804 0 1 1 1 1 1 1 1 1 1 1 0
1805 0 1 1 1 1 1 1 1 1 1 1 0
1806 0 0 1 1 1 1 1 1 1 1 0 0
1807 0 0 0 1 1 1 1 1 1 0 0 0
1808 0 0 0 0 1 1 1 1 0 0 0 0"
1809 "PBM data used for enabled breakpoint icon.")
1810
1811 (defconst breakpoint-disabled-pbm-data
1812 "P1
1813 10 10\",
1814 0 0 1 0 1 0 1 0 0 0
1815 0 1 0 1 0 1 0 1 0 0
1816 1 0 1 0 1 0 1 0 1 0
1817 0 1 0 1 0 1 0 1 0 1
1818 1 0 1 0 1 0 1 0 1 0
1819 0 1 0 1 0 1 0 1 0 1
1820 1 0 1 0 1 0 1 0 1 0
1821 0 1 0 1 0 1 0 1 0 1
1822 0 0 1 0 1 0 1 0 1 0
1823 0 0 0 1 0 1 0 1 0 0"
1824 "PBM data used for disabled breakpoint icon.")
1825
1826 (defvar breakpoint-enabled-icon nil
1827 "Icon for enabled breakpoint in display margin.")
1828
1829 (defvar breakpoint-disabled-icon nil
1830 "Icon for disabled breakpoint in display margin.")
1831
1832 (and (display-images-p)
1833 ;; Bitmap for breakpoint in fringe
1834 (define-fringe-bitmap 'breakpoint
1835 "\x3c\x7e\xff\xff\xff\xff\x7e\x3c")
1836 ;; Bitmap for gud-overlay-arrow in fringe
1837 (define-fringe-bitmap 'hollow-right-triangle
1838 "\xe0\x90\x88\x84\x84\x88\x90\xe0"))
1839
1840 (defface breakpoint-enabled
1841 '((t
1842 :foreground "red1"
1843 :weight bold))
1844 "Face for enabled breakpoint icon in fringe."
1845 :group 'gud)
1846
1847 (defface breakpoint-disabled
1848 '((((class color) (min-colors 88)) :foreground "grey70")
1849 ;; Ensure that on low-color displays that we end up something visible.
1850 (((class color) (min-colors 8) (background light))
1851 :foreground "black")
1852 (((class color) (min-colors 8) (background dark))
1853 :foreground "white")
1854 (((type tty) (class mono))
1855 :inverse-video t)
1856 (t :background "gray"))
1857 "Face for disabled breakpoint icon in fringe."
1858 :group 'gud)
1859
1860 (defconst gdb-breakpoint-regexp
1861 "\\(?:\\([0-9]+\\).*?\\(?:point\\|catch\\s-+\\S-+\\)\\s-+\\S-+\\|\\([0-9]+\\.[0-9]+\\)\\)\\s-+\\(.\\)\\s-+")
1862
1863 ;; Put breakpoint icons in relevant margins (even those set in the GUD buffer).
1864 (defun gdb-info-breakpoints-custom ()
1865 (let ((flag) (bptno))
1866 ;; Remove all breakpoint-icons in source buffers but not assembler buffer.
1867 (dolist (buffer (buffer-list))
1868 (with-current-buffer buffer
1869 (if (and (memq gud-minor-mode '(gdba gdbmi))
1870 (not (string-match "\\` ?\\*.+\\*\\'" (buffer-name))))
1871 (gdb-remove-breakpoint-icons (point-min) (point-max)))))
1872 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
1873 (save-excursion
1874 (let ((buffer-read-only nil))
1875 (goto-char (point-min))
1876 (while (< (point) (- (point-max) 1))
1877 (forward-line 1)
1878 (if (looking-at gdb-breakpoint-regexp)
1879 (progn
1880 (setq bptno (or (match-string 1) (match-string 2)))
1881 (setq flag (char-after (match-beginning 3)))
1882 (if (match-string 1)
1883 (setq gdb-parent-bptno-enabled (eq flag ?y)))
1884 (add-text-properties
1885 (match-beginning 3) (match-end 3)
1886 (if (eq flag ?y)
1887 '(face font-lock-warning-face)
1888 '(face font-lock-type-face)))
1889 (let ((bl (point))
1890 (el (line-end-position)))
1891 (if (re-search-forward " in \\(.*\\) at\\s-+" el t)
1892 (progn
1893 (add-text-properties
1894 (match-beginning 1) (match-end 1)
1895 '(face font-lock-function-name-face))
1896 (looking-at "\\(\\S-+\\):\\([0-9]+\\)")
1897 (let ((line (match-string 2))
1898 (file (match-string 1)))
1899 (add-text-properties bl el
1900 '(mouse-face highlight
1901 help-echo "mouse-2, RET: visit breakpoint"))
1902 (unless (file-exists-p file)
1903 (setq file (cdr (assoc bptno gdb-location-alist))))
1904 (if (and file
1905 (not (string-equal file "File not found")))
1906 (with-current-buffer
1907 (find-file-noselect file 'nowarn)
1908 (set (make-local-variable 'gud-minor-mode)
1909 'gdba)
1910 (set (make-local-variable 'tool-bar-map)
1911 gud-tool-bar-map)
1912 ;; Only want one breakpoint icon at each
1913 ;; location.
1914 (save-excursion
1915 (goto-line (string-to-number line))
1916 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))
1917 (gdb-enqueue-input
1918 (list
1919 (concat gdb-server-prefix "list "
1920 (match-string-no-properties 1) ":1\n")
1921 'ignore))
1922 (gdb-enqueue-input
1923 (list (concat gdb-server-prefix "info source\n")
1924 `(lambda () (gdb-get-location
1925 ,bptno ,line ,flag)))))))
1926 (if (re-search-forward
1927 "<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
1928 el t)
1929 (add-text-properties
1930 (match-beginning 1) (match-end 1)
1931 '(face font-lock-function-name-face))
1932 (end-of-line)
1933 (re-search-backward "\\s-\\(\\S-*\\)"
1934 bl t)
1935 (add-text-properties
1936 (match-beginning 1) (match-end 1)
1937 '(face font-lock-variable-name-face)))))))
1938 (end-of-line))))))
1939 (if (gdb-get-buffer 'gdb-assembler-buffer) (gdb-assembler-custom)))
1940
1941 (declare-function gud-remove "gdb-ui" t t) ; gud-def
1942 (declare-function gud-break "gdb-ui" t t) ; gud-def
1943
1944 (defun gdb-mouse-set-clear-breakpoint (event)
1945 "Set/clear breakpoint in left fringe/margin with mouse click."
1946 (interactive "e")
1947 (mouse-minibuffer-check event)
1948 (let ((posn (event-end event)))
1949 (if (numberp (posn-point posn))
1950 (with-selected-window (posn-window posn)
1951 (save-excursion
1952 (goto-char (posn-point posn))
1953 (if (or (posn-object posn)
1954 (eq (car (fringe-bitmaps-at-pos (posn-point posn)))
1955 'breakpoint))
1956 (gud-remove nil)
1957 (gud-break nil)))))))
1958
1959 (defun gdb-mouse-toggle-breakpoint-margin (event)
1960 "Enable/disable breakpoint in left margin with mouse click."
1961 (interactive "e")
1962 (mouse-minibuffer-check event)
1963 (let ((posn (event-end event)))
1964 (if (numberp (posn-point posn))
1965 (with-selected-window (posn-window posn)
1966 (save-excursion
1967 (goto-char (posn-point posn))
1968 (if (posn-object posn)
1969 (let* ((bptno (get-text-property
1970 0 'gdb-bptno (car (posn-string posn)))))
1971 (string-match "\\([0-9+]\\)*" bptno)
1972 (gdb-enqueue-input
1973 (list
1974 (concat gdb-server-prefix
1975 (if (get-text-property
1976 0 'gdb-enabled (car (posn-string posn)))
1977 "disable "
1978 "enable ")
1979 (match-string 1 bptno) "\n")
1980 'ignore)))))))))
1981
1982 (defun gdb-mouse-toggle-breakpoint-fringe (event)
1983 "Enable/disable breakpoint in left fringe with mouse click."
1984 (interactive "e")
1985 (mouse-minibuffer-check event)
1986 (let* ((posn (event-end event))
1987 (pos (posn-point posn))
1988 obj)
1989 (when (numberp pos)
1990 (with-selected-window (posn-window posn)
1991 (save-excursion
1992 (set-buffer (window-buffer (selected-window)))
1993 (goto-char pos)
1994 (dolist (overlay (overlays-in pos pos))
1995 (when (overlay-get overlay 'put-break)
1996 (setq obj (overlay-get overlay 'before-string))))
1997 (when (stringp obj)
1998 (let* ((bptno (get-text-property 0 'gdb-bptno obj)))
1999 (string-match "\\([0-9+]\\)*" bptno)
2000 (gdb-enqueue-input
2001 (list
2002 (concat gdb-server-prefix
2003 (if (get-text-property 0 'gdb-enabled obj)
2004 "disable "
2005 "enable ")
2006 (match-string 1 bptno) "\n")
2007 'ignore)))))))))
2008
2009 (defun gdb-breakpoints-buffer-name ()
2010 (with-current-buffer gud-comint-buffer
2011 (concat "*breakpoints of " (gdb-get-target-string) "*")))
2012
2013 (defun gdb-display-breakpoints-buffer ()
2014 "Display status of user-settable breakpoints."
2015 (interactive)
2016 (gdb-display-buffer
2017 (gdb-get-buffer-create 'gdb-breakpoints-buffer) t))
2018
2019 (defun gdb-frame-breakpoints-buffer ()
2020 "Display status of user-settable breakpoints in a new frame."
2021 (interactive)
2022 (let ((special-display-regexps (append special-display-regexps '(".*")))
2023 (special-display-frame-alist gdb-frame-parameters))
2024 (display-buffer (gdb-get-buffer-create 'gdb-breakpoints-buffer))))
2025
2026 (defvar gdb-breakpoints-mode-map
2027 (let ((map (make-sparse-keymap))
2028 (menu (make-sparse-keymap "Breakpoints")))
2029 (define-key menu [quit] '("Quit" . gdb-delete-frame-or-window))
2030 (define-key menu [goto] '("Goto" . gdb-goto-breakpoint))
2031 (define-key menu [delete] '("Delete" . gdb-delete-breakpoint))
2032 (define-key menu [toggle] '("Toggle" . gdb-toggle-breakpoint))
2033 (suppress-keymap map)
2034 (define-key map [menu-bar breakpoints] (cons "Breakpoints" menu))
2035 (define-key map " " 'gdb-toggle-breakpoint)
2036 (define-key map "D" 'gdb-delete-breakpoint)
2037 ;; Don't bind "q" to kill-this-buffer as we need it for breakpoint icons.
2038 (define-key map "q" 'gdb-delete-frame-or-window)
2039 (define-key map "\r" 'gdb-goto-breakpoint)
2040 (define-key map [mouse-2] 'gdb-goto-breakpoint)
2041 (define-key map [follow-link] 'mouse-face)
2042 map))
2043
2044 (defun gdb-delete-frame-or-window ()
2045 "Delete frame if there is only one window. Otherwise delete the window."
2046 (interactive)
2047 (if (one-window-p) (delete-frame)
2048 (delete-window)))
2049
2050 (defun gdb-breakpoints-mode ()
2051 "Major mode for gdb breakpoints.
2052
2053 \\{gdb-breakpoints-mode-map}"
2054 (kill-all-local-variables)
2055 (setq major-mode 'gdb-breakpoints-mode)
2056 (setq mode-name "Breakpoints")
2057 (use-local-map gdb-breakpoints-mode-map)
2058 (setq buffer-read-only t)
2059 (run-mode-hooks 'gdb-breakpoints-mode-hook)
2060 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2061 'gdb-invalidate-breakpoints
2062 'gdbmi-invalidate-breakpoints))
2063
2064 (defun gdb-toggle-breakpoint ()
2065 "Enable/disable breakpoint at current line."
2066 (interactive)
2067 (save-excursion
2068 (beginning-of-line 1)
2069 (if (looking-at gdb-breakpoint-regexp)
2070 (gdb-enqueue-input
2071 (list
2072 (concat gdb-server-prefix
2073 (if (eq ?y (char-after (match-beginning 3)))
2074 "disable "
2075 "enable ")
2076 (or (match-string 1) (match-string 2)) "\n") 'ignore))
2077 (error "Not recognized as break/watchpoint line"))))
2078
2079 (defun gdb-delete-breakpoint ()
2080 "Delete the breakpoint at current line."
2081 (interactive)
2082 (save-excursion
2083 (beginning-of-line 1)
2084 (if (looking-at gdb-breakpoint-regexp)
2085 (if (match-string 1)
2086 (gdb-enqueue-input
2087 (list
2088 (concat gdb-server-prefix "delete " (match-string 1) "\n")
2089 'ignore))
2090 (message-box "This breakpoint cannot be deleted on its own."))
2091 (error "Not recognized as break/watchpoint line"))))
2092
2093 (defun gdb-goto-breakpoint (&optional event)
2094 "Display the breakpoint location specified at current line."
2095 (interactive (list last-input-event))
2096 (if event (posn-set-point (event-end event)))
2097 (save-excursion
2098 (beginning-of-line 1)
2099 (if (looking-at "\\([0-9]+\\.?[0-9]*\\) .+ in .+ at\\s-+\\(\\S-+\\):\\([0-9]+\\)")
2100 (let ((bptno (match-string 1))
2101 (file (match-string 2))
2102 (line (match-string 3)))
2103 (save-selected-window
2104 (let* ((buffer (find-file-noselect
2105 (if (file-exists-p file) file
2106 (cdr (assoc bptno gdb-location-alist)))))
2107 (window (or (gdb-display-source-buffer buffer)
2108 (display-buffer buffer))))
2109 (setq gdb-source-window window)
2110 (with-current-buffer buffer
2111 (goto-line (string-to-number line))
2112 (set-window-point window (point))))))
2113 (error "No location specified."))))
2114 \f
2115
2116 ;; Frames buffer. This displays a perpetually correct bactracktrace
2117 ;; (from the command `where').
2118 ;;
2119 ;; Alas, if your stack is deep, it is costly.
2120 ;;
2121 (defcustom gdb-max-frames 40
2122 "Maximum number of frames displayed in call stack."
2123 :type 'integer
2124 :group 'gud
2125 :version "22.1")
2126
2127 (gdb-set-buffer-rules 'gdb-stack-buffer
2128 'gdb-stack-buffer-name
2129 'gdb-frames-mode)
2130
2131 (def-gdb-auto-updated-buffer gdb-stack-buffer
2132 gdb-invalidate-frames
2133 (concat "server info stack " (number-to-string gdb-max-frames) "\n")
2134 gdb-info-stack-handler
2135 gdb-info-stack-custom)
2136
2137 (defun gdb-info-stack-custom ()
2138 (with-current-buffer (gdb-get-buffer 'gdb-stack-buffer)
2139 (let (move-to)
2140 (save-excursion
2141 (unless (eq gdb-look-up-stack 'delete)
2142 (let ((buffer-read-only nil)
2143 bl el)
2144 (goto-char (point-min))
2145 (while (< (point) (point-max))
2146 (setq bl (line-beginning-position)
2147 el (line-end-position))
2148 (when (looking-at "#")
2149 (add-text-properties bl el
2150 '(mouse-face highlight
2151 help-echo "mouse-2, RET: Select frame")))
2152 (goto-char bl)
2153 (when (looking-at "^#\\([0-9]+\\)")
2154 (when (string-equal (match-string 1) gdb-frame-number)
2155 (if (> (car (window-fringes)) 0)
2156 (progn
2157 (or gdb-stack-position
2158 (setq gdb-stack-position (make-marker)))
2159 (set-marker gdb-stack-position (point))
2160 (setq move-to gdb-stack-position))
2161 (put-text-property bl (+ bl 4)
2162 'face '(:inverse-video t))
2163 (setq move-to bl)))
2164 (when (re-search-forward
2165 (concat
2166 (if (string-equal (match-string 1) "0") "" " in ")
2167 "\\([^ ]+\\) (") el t)
2168 (put-text-property (match-beginning 1) (match-end 1)
2169 'face font-lock-function-name-face)
2170 (setq bl (match-end 0))
2171 (while (re-search-forward "<\\([^>]+\\)>" el t)
2172 (put-text-property (match-beginning 1) (match-end 1)
2173 'face font-lock-function-name-face))
2174 (goto-char bl)
2175 (while (re-search-forward "\\(\\(\\sw\\|[_.]\\)+\\)=" el t)
2176 (put-text-property (match-beginning 1) (match-end 1)
2177 'face font-lock-variable-name-face))))
2178 (forward-line 1))
2179 (forward-line -1)
2180 (when (looking-at "(More stack frames follow...)")
2181 (add-text-properties (match-beginning 0) (match-end 0)
2182 '(mouse-face highlight
2183 gdb-max-frames t
2184 help-echo
2185 "mouse-2, RET: customize gdb-max-frames to see more frames")))))
2186 (when gdb-look-up-stack
2187 (goto-char (point-min))
2188 (when (re-search-forward "\\(\\S-+?\\):\\([0-9]+\\)" nil t)
2189 (let ((start (line-beginning-position))
2190 (file (match-string 1))
2191 (line (match-string 2)))
2192 (re-search-backward "^#*\\([0-9]+\\)" start t)
2193 (gdb-enqueue-input
2194 (list (concat gdb-server-prefix "frame "
2195 (match-string 1) "\n") 'gdb-set-hollow))
2196 (gdb-enqueue-input
2197 (list (concat gdb-server-prefix "frame 0\n") 'ignore))))))
2198 (when move-to
2199 (let ((window (get-buffer-window (current-buffer) 0)))
2200 (when window
2201 (with-selected-window window
2202 (goto-char move-to)
2203 (unless (pos-visible-in-window-p)
2204 (recenter '(center)))))))))
2205 (if (eq gdb-look-up-stack 'delete)
2206 (kill-buffer (gdb-get-buffer 'gdb-stack-buffer)))
2207 (setq gdb-look-up-stack nil))
2208
2209 (defun gdb-set-hollow ()
2210 (if gud-last-last-frame
2211 (with-current-buffer (gud-find-file (car gud-last-last-frame))
2212 (setq fringe-indicator-alist
2213 '((overlay-arrow . hollow-right-triangle))))))
2214
2215 (defun gdb-stack-buffer-name ()
2216 (with-current-buffer gud-comint-buffer
2217 (concat "*stack frames of " (gdb-get-target-string) "*")))
2218
2219 (defun gdb-display-stack-buffer ()
2220 "Display backtrace of current stack."
2221 (interactive)
2222 (gdb-display-buffer
2223 (gdb-get-buffer-create 'gdb-stack-buffer) t))
2224
2225 (defun gdb-frame-stack-buffer ()
2226 "Display backtrace of current stack in a new frame."
2227 (interactive)
2228 (let ((special-display-regexps (append special-display-regexps '(".*")))
2229 (special-display-frame-alist gdb-frame-parameters))
2230 (display-buffer (gdb-get-buffer-create 'gdb-stack-buffer))))
2231
2232 (defvar gdb-frames-mode-map
2233 (let ((map (make-sparse-keymap)))
2234 (suppress-keymap map)
2235 (define-key map "q" 'kill-this-buffer)
2236 (define-key map "\r" 'gdb-frames-select)
2237 (define-key map [mouse-2] 'gdb-frames-select)
2238 (define-key map [follow-link] 'mouse-face)
2239 map))
2240
2241 (defun gdb-frames-mode ()
2242 "Major mode for gdb call stack.
2243
2244 \\{gdb-frames-mode-map}"
2245 (kill-all-local-variables)
2246 (setq major-mode 'gdb-frames-mode)
2247 (setq mode-name "Frames")
2248 (setq gdb-stack-position nil)
2249 (add-to-list 'overlay-arrow-variable-list 'gdb-stack-position)
2250 (setq truncate-lines t) ;; Make it easier to see overlay arrow.
2251 (setq buffer-read-only t)
2252 (use-local-map gdb-frames-mode-map)
2253 (run-mode-hooks 'gdb-frames-mode-hook)
2254 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2255 'gdb-invalidate-frames
2256 'gdbmi-invalidate-frames))
2257
2258 (defun gdb-get-frame-number ()
2259 (save-excursion
2260 (end-of-line)
2261 (let* ((start (line-beginning-position))
2262 (pos (re-search-backward "^#*\\([0-9]+\\)" start t))
2263 (n (or (and pos (match-string 1)) "0")))
2264 n)))
2265
2266 (defun gdb-frames-select (&optional event)
2267 "Select the frame and display the relevant source."
2268 (interactive (list last-input-event))
2269 (if event (posn-set-point (event-end event)))
2270 (if (get-text-property (point) 'gdb-max-frames)
2271 (progn
2272 (message-box "After setting gdb-max-frames, you need to enter\n\
2273 another GDB command e.g pwd, to see new frames")
2274 (customize-variable-other-window 'gdb-max-frames))
2275 (gdb-enqueue-input
2276 (list (concat gdb-server-prefix "frame "
2277 (gdb-get-frame-number) "\n") 'ignore))))
2278 \f
2279
2280 ;; Threads buffer. This displays a selectable thread list.
2281 ;;
2282 (gdb-set-buffer-rules 'gdb-threads-buffer
2283 'gdb-threads-buffer-name
2284 'gdb-threads-mode)
2285
2286 (def-gdb-auto-updated-buffer gdb-threads-buffer
2287 gdb-invalidate-threads
2288 (concat gdb-server-prefix "info threads\n")
2289 gdb-info-threads-handler
2290 gdb-info-threads-custom)
2291
2292 (defun gdb-info-threads-custom ()
2293 (with-current-buffer (gdb-get-buffer 'gdb-threads-buffer)
2294 (let ((buffer-read-only nil))
2295 (save-excursion
2296 (goto-char (point-min))
2297 (while (< (point) (point-max))
2298 (unless (looking-at "No ")
2299 (add-text-properties (line-beginning-position) (line-end-position)
2300 '(mouse-face highlight
2301 help-echo "mouse-2, RET: select thread")))
2302 (forward-line 1))))))
2303
2304 (defun gdb-threads-buffer-name ()
2305 (with-current-buffer gud-comint-buffer
2306 (concat "*threads of " (gdb-get-target-string) "*")))
2307
2308 (defun gdb-display-threads-buffer ()
2309 "Display IDs of currently known threads."
2310 (interactive)
2311 (gdb-display-buffer
2312 (gdb-get-buffer-create 'gdb-threads-buffer) t))
2313
2314 (defun gdb-frame-threads-buffer ()
2315 "Display IDs of currently known threads in a new frame."
2316 (interactive)
2317 (let ((special-display-regexps (append special-display-regexps '(".*")))
2318 (special-display-frame-alist gdb-frame-parameters))
2319 (display-buffer (gdb-get-buffer-create 'gdb-threads-buffer))))
2320
2321 (defvar gdb-threads-mode-map
2322 (let ((map (make-sparse-keymap)))
2323 (suppress-keymap map)
2324 (define-key map "q" 'kill-this-buffer)
2325 (define-key map "\r" 'gdb-threads-select)
2326 (define-key map [mouse-2] 'gdb-threads-select)
2327 (define-key map [follow-link] 'mouse-face)
2328 map))
2329
2330 (defvar gdb-threads-font-lock-keywords
2331 '((") +\\([^ ]+\\) (" (1 font-lock-function-name-face))
2332 ("in \\([^ ]+\\) (" (1 font-lock-function-name-face))
2333 ("\\(\\(\\sw\\|[_.]\\)+\\)=" (1 font-lock-variable-name-face)))
2334 "Font lock keywords used in `gdb-threads-mode'.")
2335
2336 (defun gdb-threads-mode ()
2337 "Major mode for gdb threads.
2338
2339 \\{gdb-threads-mode-map}"
2340 (kill-all-local-variables)
2341 (setq major-mode 'gdb-threads-mode)
2342 (setq mode-name "Threads")
2343 (setq buffer-read-only t)
2344 (use-local-map gdb-threads-mode-map)
2345 (set (make-local-variable 'font-lock-defaults)
2346 '(gdb-threads-font-lock-keywords))
2347 (run-mode-hooks 'gdb-threads-mode-hook)
2348 'gdb-invalidate-threads)
2349
2350 (defun gdb-get-thread-number ()
2351 (save-excursion
2352 (re-search-backward "^\\s-*\\([0-9]*\\)" nil t)
2353 (match-string-no-properties 1)))
2354
2355 (defun gdb-threads-select (&optional event)
2356 "Select the thread and display the relevant source."
2357 (interactive (list last-input-event))
2358 (if event (posn-set-point (event-end event)))
2359 (gdb-enqueue-input
2360 (list (concat gdb-server-prefix "thread "
2361 (gdb-get-thread-number) "\n") 'ignore))
2362 (gud-display-frame))
2363 \f
2364
2365 ;; Registers buffer.
2366 ;;
2367 (defcustom gdb-all-registers nil
2368 "Non-nil means include floating-point registers."
2369 :type 'boolean
2370 :group 'gud
2371 :version "22.1")
2372
2373 (gdb-set-buffer-rules 'gdb-registers-buffer
2374 'gdb-registers-buffer-name
2375 'gdb-registers-mode)
2376
2377 (def-gdb-auto-updated-buffer gdb-registers-buffer
2378 gdb-invalidate-registers
2379 (concat
2380 gdb-server-prefix "info " (if gdb-all-registers "all-") "registers\n")
2381 gdb-info-registers-handler
2382 gdb-info-registers-custom)
2383
2384 (defun gdb-info-registers-custom ()
2385 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
2386 (save-excursion
2387 (let ((buffer-read-only nil)
2388 start end)
2389 (goto-char (point-min))
2390 (while (< (point) (point-max))
2391 (setq start (line-beginning-position))
2392 (setq end (line-end-position))
2393 (when (looking-at "^[^ ]+")
2394 (unless (string-equal (match-string 0) "The")
2395 (put-text-property start (match-end 0)
2396 'face font-lock-variable-name-face)
2397 (add-text-properties start end
2398 '(help-echo "mouse-2: edit value"
2399 mouse-face highlight))))
2400 (forward-line 1))))))
2401
2402 (defun gdb-edit-register-value (&optional event)
2403 (interactive (list last-input-event))
2404 (save-excursion
2405 (if event (posn-set-point (event-end event)))
2406 (beginning-of-line)
2407 (let* ((register (current-word))
2408 (value (read-string (format "New value (%s): " register))))
2409 (gdb-enqueue-input
2410 (list (concat gdb-server-prefix "set $" register "=" value "\n")
2411 'ignore)))))
2412
2413 (defvar gdb-registers-mode-map
2414 (let ((map (make-sparse-keymap)))
2415 (suppress-keymap map)
2416 (define-key map "\r" 'gdb-edit-register-value)
2417 (define-key map [mouse-2] 'gdb-edit-register-value)
2418 (define-key map " " 'gdb-all-registers)
2419 (define-key map "q" 'kill-this-buffer)
2420 map))
2421
2422 (defun gdb-registers-mode ()
2423 "Major mode for gdb registers.
2424
2425 \\{gdb-registers-mode-map}"
2426 (kill-all-local-variables)
2427 (setq major-mode 'gdb-registers-mode)
2428 (setq mode-name "Registers")
2429 (setq buffer-read-only t)
2430 (use-local-map gdb-registers-mode-map)
2431 (run-mode-hooks 'gdb-registers-mode-hook)
2432 (if (string-equal gdb-version "pre-6.4")
2433 (progn
2434 (if gdb-all-registers (setq mode-name "Registers:All"))
2435 'gdb-invalidate-registers)
2436 'gdb-invalidate-registers-1))
2437
2438 (defun gdb-registers-buffer-name ()
2439 (with-current-buffer gud-comint-buffer
2440 (concat "*registers of " (gdb-get-target-string) "*")))
2441
2442 (defun gdb-display-registers-buffer ()
2443 "Display integer register contents."
2444 (interactive)
2445 (gdb-display-buffer
2446 (gdb-get-buffer-create 'gdb-registers-buffer) t))
2447
2448 (defun gdb-frame-registers-buffer ()
2449 "Display integer register contents in a new frame."
2450 (interactive)
2451 (let ((special-display-regexps (append special-display-regexps '(".*")))
2452 (special-display-frame-alist gdb-frame-parameters))
2453 (display-buffer (gdb-get-buffer-create 'gdb-registers-buffer))))
2454
2455 (defun gdb-all-registers ()
2456 "Toggle the display of floating-point registers (pre GDB 6.4 only)."
2457 (interactive)
2458 (when (string-equal gdb-version "pre-6.4")
2459 (if gdb-all-registers
2460 (progn
2461 (setq gdb-all-registers nil)
2462 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2463 (setq mode-name "Registers")))
2464 (setq gdb-all-registers t)
2465 (with-current-buffer (gdb-get-buffer-create 'gdb-registers-buffer)
2466 (setq mode-name "Registers:All")))
2467 (message (format "Display of floating-point registers %sabled"
2468 (if gdb-all-registers "en" "dis")))
2469 (gdb-invalidate-registers)))
2470 \f
2471
2472 ;; Memory buffer.
2473 ;;
2474 (defcustom gdb-memory-repeat-count 32
2475 "Number of data items in memory window."
2476 :type 'integer
2477 :group 'gud
2478 :version "22.1")
2479
2480 (defcustom gdb-memory-format "x"
2481 "Display format of data items in memory window."
2482 :type '(choice (const :tag "Hexadecimal" "x")
2483 (const :tag "Signed decimal" "d")
2484 (const :tag "Unsigned decimal" "u")
2485 (const :tag "Octal" "o")
2486 (const :tag "Binary" "t"))
2487 :group 'gud
2488 :version "22.1")
2489
2490 (defcustom gdb-memory-unit "w"
2491 "Unit size of data items in memory window."
2492 :type '(choice (const :tag "Byte" "b")
2493 (const :tag "Halfword" "h")
2494 (const :tag "Word" "w")
2495 (const :tag "Giant word" "g"))
2496 :group 'gud
2497 :version "22.1")
2498
2499 (gdb-set-buffer-rules 'gdb-memory-buffer
2500 'gdb-memory-buffer-name
2501 'gdb-memory-mode)
2502
2503 (def-gdb-auto-updated-buffer gdb-memory-buffer
2504 gdb-invalidate-memory
2505 (concat gdb-server-prefix "x/" (number-to-string gdb-memory-repeat-count)
2506 gdb-memory-format gdb-memory-unit " " gdb-memory-address "\n")
2507 gdb-read-memory-handler
2508 gdb-read-memory-custom)
2509
2510 (defun gdb-read-memory-custom ()
2511 (save-excursion
2512 (goto-char (point-min))
2513 (if (looking-at "0x[[:xdigit:]]+")
2514 (setq gdb-memory-address (match-string 0)))))
2515
2516 (defvar gdb-memory-mode-map
2517 (let ((map (make-sparse-keymap)))
2518 (suppress-keymap map)
2519 (define-key map "q" 'kill-this-buffer)
2520 map))
2521
2522 (defun gdb-memory-set-address (event)
2523 "Set the start memory address."
2524 (interactive "e")
2525 (save-selected-window
2526 (select-window (posn-window (event-start event)))
2527 (let ((arg (read-from-minibuffer "Memory address: ")))
2528 (setq gdb-memory-address arg))
2529 (gdb-invalidate-memory)))
2530
2531 (defun gdb-memory-set-repeat-count (event)
2532 "Set the number of data items in memory window."
2533 (interactive "e")
2534 (save-selected-window
2535 (select-window (posn-window (event-start event)))
2536 (let* ((arg (read-from-minibuffer "Repeat count: "))
2537 (count (string-to-number arg)))
2538 (if (<= count 0)
2539 (error "Positive numbers only")
2540 (customize-set-variable 'gdb-memory-repeat-count count)
2541 (gdb-invalidate-memory)))))
2542
2543 (defun gdb-memory-format-binary ()
2544 "Set the display format to binary."
2545 (interactive)
2546 (customize-set-variable 'gdb-memory-format "t")
2547 (gdb-invalidate-memory))
2548
2549 (defun gdb-memory-format-octal ()
2550 "Set the display format to octal."
2551 (interactive)
2552 (customize-set-variable 'gdb-memory-format "o")
2553 (gdb-invalidate-memory))
2554
2555 (defun gdb-memory-format-unsigned ()
2556 "Set the display format to unsigned decimal."
2557 (interactive)
2558 (customize-set-variable 'gdb-memory-format "u")
2559 (gdb-invalidate-memory))
2560
2561 (defun gdb-memory-format-signed ()
2562 "Set the display format to decimal."
2563 (interactive)
2564 (customize-set-variable 'gdb-memory-format "d")
2565 (gdb-invalidate-memory))
2566
2567 (defun gdb-memory-format-hexadecimal ()
2568 "Set the display format to hexadecimal."
2569 (interactive)
2570 (customize-set-variable 'gdb-memory-format "x")
2571 (gdb-invalidate-memory))
2572
2573 (defvar gdb-memory-format-map
2574 (let ((map (make-sparse-keymap)))
2575 (define-key map [header-line down-mouse-3] 'gdb-memory-format-menu-1)
2576 map)
2577 "Keymap to select format in the header line.")
2578
2579 (defvar gdb-memory-format-menu (make-sparse-keymap "Format")
2580 "Menu of display formats in the header line.")
2581
2582 (define-key gdb-memory-format-menu [binary]
2583 '(menu-item "Binary" gdb-memory-format-binary
2584 :button (:radio . (equal gdb-memory-format "t"))))
2585 (define-key gdb-memory-format-menu [octal]
2586 '(menu-item "Octal" gdb-memory-format-octal
2587 :button (:radio . (equal gdb-memory-format "o"))))
2588 (define-key gdb-memory-format-menu [unsigned]
2589 '(menu-item "Unsigned Decimal" gdb-memory-format-unsigned
2590 :button (:radio . (equal gdb-memory-format "u"))))
2591 (define-key gdb-memory-format-menu [signed]
2592 '(menu-item "Signed Decimal" gdb-memory-format-signed
2593 :button (:radio . (equal gdb-memory-format "d"))))
2594 (define-key gdb-memory-format-menu [hexadecimal]
2595 '(menu-item "Hexadecimal" gdb-memory-format-hexadecimal
2596 :button (:radio . (equal gdb-memory-format "x"))))
2597
2598 (defun gdb-memory-format-menu (event)
2599 (interactive "@e")
2600 (x-popup-menu event gdb-memory-format-menu))
2601
2602 (defun gdb-memory-format-menu-1 (event)
2603 (interactive "e")
2604 (save-selected-window
2605 (select-window (posn-window (event-start event)))
2606 (let* ((selection (gdb-memory-format-menu event))
2607 (binding (and selection (lookup-key gdb-memory-format-menu
2608 (vector (car selection))))))
2609 (if binding (call-interactively binding)))))
2610
2611 (defun gdb-memory-unit-giant ()
2612 "Set the unit size to giant words (eight bytes)."
2613 (interactive)
2614 (customize-set-variable 'gdb-memory-unit "g")
2615 (gdb-invalidate-memory))
2616
2617 (defun gdb-memory-unit-word ()
2618 "Set the unit size to words (four bytes)."
2619 (interactive)
2620 (customize-set-variable 'gdb-memory-unit "w")
2621 (gdb-invalidate-memory))
2622
2623 (defun gdb-memory-unit-halfword ()
2624 "Set the unit size to halfwords (two bytes)."
2625 (interactive)
2626 (customize-set-variable 'gdb-memory-unit "h")
2627 (gdb-invalidate-memory))
2628
2629 (defun gdb-memory-unit-byte ()
2630 "Set the unit size to bytes."
2631 (interactive)
2632 (customize-set-variable 'gdb-memory-unit "b")
2633 (gdb-invalidate-memory))
2634
2635 (defvar gdb-memory-unit-map
2636 (let ((map (make-sparse-keymap)))
2637 (define-key map [header-line down-mouse-3] 'gdb-memory-unit-menu-1)
2638 map)
2639 "Keymap to select units in the header line.")
2640
2641 (defvar gdb-memory-unit-menu (make-sparse-keymap "Unit")
2642 "Menu of units in the header line.")
2643
2644 (define-key gdb-memory-unit-menu [giantwords]
2645 '(menu-item "Giant words" gdb-memory-unit-giant
2646 :button (:radio . (equal gdb-memory-unit "g"))))
2647 (define-key gdb-memory-unit-menu [words]
2648 '(menu-item "Words" gdb-memory-unit-word
2649 :button (:radio . (equal gdb-memory-unit "w"))))
2650 (define-key gdb-memory-unit-menu [halfwords]
2651 '(menu-item "Halfwords" gdb-memory-unit-halfword
2652 :button (:radio . (equal gdb-memory-unit "h"))))
2653 (define-key gdb-memory-unit-menu [bytes]
2654 '(menu-item "Bytes" gdb-memory-unit-byte
2655 :button (:radio . (equal gdb-memory-unit "b"))))
2656
2657 (defun gdb-memory-unit-menu (event)
2658 (interactive "@e")
2659 (x-popup-menu event gdb-memory-unit-menu))
2660
2661 (defun gdb-memory-unit-menu-1 (event)
2662 (interactive "e")
2663 (save-selected-window
2664 (select-window (posn-window (event-start event)))
2665 (let* ((selection (gdb-memory-unit-menu event))
2666 (binding (and selection (lookup-key gdb-memory-unit-menu
2667 (vector (car selection))))))
2668 (if binding (call-interactively binding)))))
2669
2670 ;;from make-mode-line-mouse-map
2671 (defun gdb-make-header-line-mouse-map (mouse function) "\
2672 Return a keymap with single entry for mouse key MOUSE on the header line.
2673 MOUSE is defined to run function FUNCTION with no args in the buffer
2674 corresponding to the mode line clicked."
2675 (let ((map (make-sparse-keymap)))
2676 (define-key map (vector 'header-line mouse) function)
2677 (define-key map (vector 'header-line 'down-mouse-1) 'ignore)
2678 map))
2679
2680 (defvar gdb-memory-font-lock-keywords
2681 '(;; <__function.name+n>
2682 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>" (1 font-lock-function-name-face))
2683 )
2684 "Font lock keywords used in `gdb-memory-mode'.")
2685
2686 (defun gdb-memory-mode ()
2687 "Major mode for examining memory.
2688
2689 \\{gdb-memory-mode-map}"
2690 (kill-all-local-variables)
2691 (setq major-mode 'gdb-memory-mode)
2692 (setq mode-name "Memory")
2693 (setq buffer-read-only t)
2694 (use-local-map gdb-memory-mode-map)
2695 (setq header-line-format
2696 '(:eval
2697 (concat
2698 "Read address["
2699 (propertize
2700 "-"
2701 'face font-lock-warning-face
2702 'help-echo "mouse-1: decrement address"
2703 'mouse-face 'mode-line-highlight
2704 'local-map
2705 (gdb-make-header-line-mouse-map
2706 'mouse-1
2707 (lambda () (interactive)
2708 (let ((gdb-memory-address
2709 ;; Let GDB do the arithmetic.
2710 (concat
2711 gdb-memory-address " - "
2712 (number-to-string
2713 (* gdb-memory-repeat-count
2714 (cond ((string= gdb-memory-unit "b") 1)
2715 ((string= gdb-memory-unit "h") 2)
2716 ((string= gdb-memory-unit "w") 4)
2717 ((string= gdb-memory-unit "g") 8)))))))
2718 (gdb-invalidate-memory)))))
2719 "|"
2720 (propertize "+"
2721 'face font-lock-warning-face
2722 'help-echo "mouse-1: increment address"
2723 'mouse-face 'mode-line-highlight
2724 'local-map (gdb-make-header-line-mouse-map
2725 'mouse-1
2726 (lambda () (interactive)
2727 (let ((gdb-memory-address nil))
2728 (gdb-invalidate-memory)))))
2729 "]: "
2730 (propertize gdb-memory-address
2731 'face font-lock-warning-face
2732 'help-echo "mouse-1: set memory address"
2733 'mouse-face 'mode-line-highlight
2734 'local-map (gdb-make-header-line-mouse-map
2735 'mouse-1
2736 #'gdb-memory-set-address))
2737 " Repeat Count: "
2738 (propertize (number-to-string gdb-memory-repeat-count)
2739 'face font-lock-warning-face
2740 'help-echo "mouse-1: set repeat count"
2741 'mouse-face 'mode-line-highlight
2742 'local-map (gdb-make-header-line-mouse-map
2743 'mouse-1
2744 #'gdb-memory-set-repeat-count))
2745 " Display Format: "
2746 (propertize gdb-memory-format
2747 'face font-lock-warning-face
2748 'help-echo "mouse-3: select display format"
2749 'mouse-face 'mode-line-highlight
2750 'local-map gdb-memory-format-map)
2751 " Unit Size: "
2752 (propertize gdb-memory-unit
2753 'face font-lock-warning-face
2754 'help-echo "mouse-3: select unit size"
2755 'mouse-face 'mode-line-highlight
2756 'local-map gdb-memory-unit-map))))
2757 (set (make-local-variable 'font-lock-defaults)
2758 '(gdb-memory-font-lock-keywords))
2759 (run-mode-hooks 'gdb-memory-mode-hook)
2760 'gdb-invalidate-memory)
2761
2762 (defun gdb-memory-buffer-name ()
2763 (with-current-buffer gud-comint-buffer
2764 (concat "*memory of " (gdb-get-target-string) "*")))
2765
2766 (defun gdb-display-memory-buffer ()
2767 "Display memory contents."
2768 (interactive)
2769 (gdb-display-buffer
2770 (gdb-get-buffer-create 'gdb-memory-buffer) t))
2771
2772 (defun gdb-frame-memory-buffer ()
2773 "Display memory contents in a new frame."
2774 (interactive)
2775 (let* ((special-display-regexps (append special-display-regexps '(".*")))
2776 (special-display-frame-alist
2777 (cons '(left-fringe . 0)
2778 (cons '(right-fringe . 0)
2779 (cons '(width . 83) gdb-frame-parameters)))))
2780 (display-buffer (gdb-get-buffer-create 'gdb-memory-buffer))))
2781 \f
2782
2783 ;; Locals buffer.
2784 ;;
2785 (gdb-set-buffer-rules 'gdb-locals-buffer
2786 'gdb-locals-buffer-name
2787 'gdb-locals-mode)
2788
2789 (def-gdb-auto-update-trigger gdb-invalidate-locals
2790 (gdb-get-buffer 'gdb-locals-buffer)
2791 "server info locals\n"
2792 gdb-info-locals-handler)
2793
2794 (defvar gdb-locals-watch-map
2795 (let ((map (make-sparse-keymap)))
2796 (suppress-keymap map)
2797 (define-key map "\r" (lambda () (interactive)
2798 (beginning-of-line)
2799 (gud-watch)))
2800 (define-key map [mouse-2] (lambda (event) (interactive "e")
2801 (mouse-set-point event)
2802 (beginning-of-line)
2803 (gud-watch)))
2804 map)
2805 "Keymap to create watch expression of a complex data type local variable.")
2806
2807 (defconst gdb-struct-string
2808 (concat (propertize "[struct/union]"
2809 'mouse-face 'highlight
2810 'help-echo "mouse-2: create watch expression"
2811 'local-map gdb-locals-watch-map) "\n"))
2812
2813 (defconst gdb-array-string
2814 (concat " " (propertize "[array]"
2815 'mouse-face 'highlight
2816 'help-echo "mouse-2: create watch expression"
2817 'local-map gdb-locals-watch-map) "\n"))
2818
2819 ;; Abbreviate for arrays and structures.
2820 ;; These can be expanded using gud-display.
2821 (defun gdb-info-locals-handler ()
2822 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals
2823 gdb-pending-triggers))
2824 (let ((buf (gdb-get-buffer 'gdb-partial-output-buffer)))
2825 (with-current-buffer buf
2826 (goto-char (point-min))
2827 (while (re-search-forward "^[ }].*\n" nil t)
2828 (replace-match "" nil nil))
2829 (goto-char (point-min))
2830 (while (re-search-forward "{\\(.*=.*\n\\|\n\\)" nil t)
2831 (replace-match gdb-struct-string nil nil))
2832 (goto-char (point-min))
2833 (while (re-search-forward "\\s-*{.*\n" nil t)
2834 (replace-match gdb-array-string nil nil))))
2835 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
2836 (and buf
2837 (with-current-buffer buf
2838 (let* ((window (get-buffer-window buf 0))
2839 (start (window-start window))
2840 (p (window-point window))
2841 (buffer-read-only nil))
2842 (erase-buffer)
2843 (insert-buffer-substring (gdb-get-buffer-create
2844 'gdb-partial-output-buffer))
2845 (set-window-start window start)
2846 (set-window-point window p))
2847 )))
2848 (run-hooks 'gdb-info-locals-hook))
2849
2850 (defvar gdb-locals-mode-map
2851 (let ((map (make-sparse-keymap)))
2852 (suppress-keymap map)
2853 (define-key map "q" 'kill-this-buffer)
2854 map))
2855
2856 (defun gdb-locals-mode ()
2857 "Major mode for gdb locals.
2858
2859 \\{gdb-locals-mode-map}"
2860 (kill-all-local-variables)
2861 (setq major-mode 'gdb-locals-mode)
2862 (setq mode-name (concat "Locals:" gdb-selected-frame))
2863 (setq buffer-read-only t)
2864 (use-local-map gdb-locals-mode-map)
2865 (set (make-local-variable 'font-lock-defaults)
2866 '(gdb-locals-font-lock-keywords))
2867 (run-mode-hooks 'gdb-locals-mode-hook)
2868 (if (and (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
2869 (string-equal gdb-version "pre-6.4"))
2870 'gdb-invalidate-locals
2871 'gdb-invalidate-locals-1))
2872
2873 (defun gdb-locals-buffer-name ()
2874 (with-current-buffer gud-comint-buffer
2875 (concat "*locals of " (gdb-get-target-string) "*")))
2876
2877 (defun gdb-display-locals-buffer ()
2878 "Display local variables of current stack and their values."
2879 (interactive)
2880 (gdb-display-buffer
2881 (gdb-get-buffer-create 'gdb-locals-buffer) t))
2882
2883 (defun gdb-frame-locals-buffer ()
2884 "Display local variables of current stack and their values in a new frame."
2885 (interactive)
2886 (let ((special-display-regexps (append special-display-regexps '(".*")))
2887 (special-display-frame-alist gdb-frame-parameters))
2888 (display-buffer (gdb-get-buffer-create 'gdb-locals-buffer))))
2889 \f
2890
2891 ;;;; Window management
2892 (defun gdb-display-buffer (buf dedicated &optional size)
2893 (let ((answer (get-buffer-window buf 0))
2894 (must-split nil))
2895 (if answer
2896 (display-buffer buf nil 0) ;Deiconify the frame if necessary.
2897 ;; The buffer is not yet displayed.
2898 (pop-to-buffer gud-comint-buffer) ;Select the right frame.
2899 (let ((window (get-lru-window)))
2900 (if (and window
2901 (not (memq window `(,(get-buffer-window gud-comint-buffer)
2902 ,gdb-source-window))))
2903 (progn
2904 (set-window-buffer window buf)
2905 (setq answer window))
2906 (setq must-split t)))
2907 (if must-split
2908 (let* ((largest (get-largest-window))
2909 (cur-size (window-height largest))
2910 (new-size (and size (< size cur-size) (- cur-size size))))
2911 (setq answer (split-window largest new-size))
2912 (set-window-buffer answer buf)
2913 (set-window-dedicated-p answer dedicated)))
2914 answer)))
2915
2916 \f
2917 ;;; Shared keymap initialization:
2918
2919 (let ((menu (make-sparse-keymap "GDB-Windows")))
2920 (define-key gud-menu-map [displays]
2921 `(menu-item "GDB-Windows" ,menu
2922 :visible (memq gud-minor-mode '(gdbmi gdba))))
2923 (define-key menu [gdb] '("Gdb" . gdb-display-gdb-buffer))
2924 (define-key menu [threads] '("Threads" . gdb-display-threads-buffer))
2925 (define-key menu [inferior]
2926 '(menu-item "Separate IO" gdb-display-separate-io-buffer
2927 :enable gdb-use-separate-io-buffer))
2928 (define-key menu [memory] '("Memory" . gdb-display-memory-buffer))
2929 (define-key menu [registers] '("Registers" . gdb-display-registers-buffer))
2930 (define-key menu [disassembly]
2931 '("Disassembly" . gdb-display-assembler-buffer))
2932 (define-key menu [breakpoints]
2933 '("Breakpoints" . gdb-display-breakpoints-buffer))
2934 (define-key menu [locals] '("Locals" . gdb-display-locals-buffer))
2935 (define-key menu [frames] '("Stack" . gdb-display-stack-buffer)))
2936
2937 (let ((menu (make-sparse-keymap "GDB-Frames")))
2938 (define-key gud-menu-map [frames]
2939 `(menu-item "GDB-Frames" ,menu
2940 :visible (memq gud-minor-mode '(gdbmi gdba))))
2941 (define-key menu [gdb] '("Gdb" . gdb-frame-gdb-buffer))
2942 (define-key menu [threads] '("Threads" . gdb-frame-threads-buffer))
2943 (define-key menu [memory] '("Memory" . gdb-frame-memory-buffer))
2944 (define-key menu [inferior]
2945 '(menu-item "Separate IO" gdb-frame-separate-io-buffer
2946 :enable gdb-use-separate-io-buffer))
2947 (define-key menu [registers] '("Registers" . gdb-frame-registers-buffer))
2948 (define-key menu [disassembly] '("Disassembly" . gdb-frame-assembler-buffer))
2949 (define-key menu [breakpoints]
2950 '("Breakpoints" . gdb-frame-breakpoints-buffer))
2951 (define-key menu [locals] '("Locals" . gdb-frame-locals-buffer))
2952 (define-key menu [frames] '("Stack" . gdb-frame-stack-buffer)))
2953
2954 (let ((menu (make-sparse-keymap "GDB-UI/MI")))
2955 (define-key gud-menu-map [ui]
2956 `(menu-item (if (eq gud-minor-mode 'gdba) "GDB-UI" "GDB-MI")
2957 ,menu :visible (memq gud-minor-mode '(gdbmi gdba))))
2958 (define-key menu [gdb-find-source-frame]
2959 '(menu-item "Look For Source Frame" gdb-find-source-frame
2960 :visible (eq gud-minor-mode 'gdba)
2961 :help "Toggle look for source frame."
2962 :button (:toggle . gdb-find-source-frame)))
2963 (define-key menu [gdb-use-separate-io]
2964 '(menu-item "Separate IO" gdb-use-separate-io-buffer
2965 :visible (eq gud-minor-mode 'gdba)
2966 :help "Toggle separate IO for debugged program."
2967 :button (:toggle . gdb-use-separate-io-buffer)))
2968 (define-key menu [gdb-many-windows]
2969 '(menu-item "Display Other Windows" gdb-many-windows
2970 :help "Toggle display of locals, stack and breakpoint information"
2971 :button (:toggle . gdb-many-windows)))
2972 (define-key menu [gdb-restore-windows]
2973 '(menu-item "Restore Window Layout" gdb-restore-windows
2974 :help "Restore standard layout for debug session.")))
2975
2976 (defun gdb-frame-gdb-buffer ()
2977 "Display GUD buffer in a new frame."
2978 (interactive)
2979 (let ((special-display-regexps (append special-display-regexps '(".*")))
2980 (special-display-frame-alist
2981 (remove '(menu-bar-lines) (remove '(tool-bar-lines)
2982 gdb-frame-parameters)))
2983 (same-window-regexps nil))
2984 (display-buffer gud-comint-buffer)))
2985
2986 (defun gdb-display-gdb-buffer ()
2987 "Display GUD buffer."
2988 (interactive)
2989 (let ((same-window-regexps nil))
2990 (pop-to-buffer gud-comint-buffer)))
2991
2992 (defun gdb-set-window-buffer (name)
2993 (set-window-buffer (selected-window) (get-buffer name))
2994 (set-window-dedicated-p (selected-window) t))
2995
2996 (defun gdb-setup-windows ()
2997 "Layout the window pattern for `gdb-many-windows'."
2998 (gdb-display-locals-buffer)
2999 (gdb-display-stack-buffer)
3000 (delete-other-windows)
3001 (gdb-display-breakpoints-buffer)
3002 (delete-other-windows)
3003 ; Don't dedicate.
3004 (pop-to-buffer gud-comint-buffer)
3005 (split-window nil ( / ( * (window-height) 3) 4))
3006 (split-window nil ( / (window-height) 3))
3007 (split-window-horizontally)
3008 (other-window 1)
3009 (gdb-set-window-buffer (gdb-locals-buffer-name))
3010 (other-window 1)
3011 (switch-to-buffer
3012 (if gud-last-last-frame
3013 (gud-find-file (car gud-last-last-frame))
3014 (if gdb-main-file
3015 (gud-find-file gdb-main-file)
3016 ;; Put buffer list in window if we
3017 ;; can't find a source file.
3018 (list-buffers-noselect))))
3019 (setq gdb-source-window (selected-window))
3020 (when gdb-use-separate-io-buffer
3021 (split-window-horizontally)
3022 (other-window 1)
3023 (gdb-set-window-buffer
3024 (gdb-get-buffer-create 'gdb-inferior-io)))
3025 (other-window 1)
3026 (gdb-set-window-buffer (gdb-stack-buffer-name))
3027 (split-window-horizontally)
3028 (other-window 1)
3029 (gdb-set-window-buffer (gdb-breakpoints-buffer-name))
3030 (other-window 1))
3031
3032 (defun gdb-restore-windows ()
3033 "Restore the basic arrangement of windows used by gdba.
3034 This arrangement depends on the value of `gdb-many-windows'."
3035 (interactive)
3036 (pop-to-buffer gud-comint-buffer) ;Select the right window and frame.
3037 (delete-other-windows)
3038 (if gdb-many-windows
3039 (gdb-setup-windows)
3040 (when (or gud-last-last-frame gdb-show-main)
3041 (split-window)
3042 (other-window 1)
3043 (switch-to-buffer
3044 (if gud-last-last-frame
3045 (gud-find-file (car gud-last-last-frame))
3046 (gud-find-file gdb-main-file)))
3047 (setq gdb-source-window (selected-window))
3048 (other-window 1))))
3049
3050 (defun gdb-reset ()
3051 "Exit a debugging session cleanly.
3052 Kills the gdb buffers, and resets variables and the source buffers."
3053 (dolist (buffer (buffer-list))
3054 (unless (eq buffer gud-comint-buffer)
3055 (with-current-buffer buffer
3056 (if (memq gud-minor-mode '(gdbmi gdba))
3057 (if (string-match "\\` ?\\*.+\\*\\'" (buffer-name))
3058 (kill-buffer nil)
3059 (gdb-remove-breakpoint-icons (point-min) (point-max) t)
3060 (setq gud-minor-mode nil)
3061 (kill-local-variable 'tool-bar-map)
3062 (kill-local-variable 'gdb-define-alist))))))
3063 (setq gdb-overlay-arrow-position nil)
3064 (setq overlay-arrow-variable-list
3065 (delq 'gdb-overlay-arrow-position overlay-arrow-variable-list))
3066 (setq fringe-indicator-alist '((overlay-arrow . right-triangle)))
3067 (setq gdb-stack-position nil)
3068 (setq overlay-arrow-variable-list
3069 (delq 'gdb-stack-position overlay-arrow-variable-list))
3070 (if (boundp 'speedbar-frame) (speedbar-timer-fn))
3071 (setq gud-running nil)
3072 (setq gdb-active-process nil)
3073 (setq gdb-var-list nil)
3074 (remove-hook 'after-save-hook 'gdb-create-define-alist t))
3075
3076 (defun gdb-source-info ()
3077 "Find the source file where the program starts and displays it with related
3078 buffers."
3079 (goto-char (point-min))
3080 (if (and (search-forward "Located in " nil t)
3081 (looking-at "\\S-+"))
3082 (setq gdb-main-file (match-string 0)))
3083 (goto-char (point-min))
3084 (if (search-forward "Includes preprocessor macro info." nil t)
3085 (setq gdb-macro-info t))
3086 (if gdb-many-windows
3087 (gdb-setup-windows)
3088 (gdb-get-buffer-create 'gdb-breakpoints-buffer)
3089 (if gdb-show-main
3090 (let ((pop-up-windows t))
3091 (display-buffer (gud-find-file gdb-main-file)))))
3092 (setq gdb-ready t))
3093
3094 (defun gdb-get-location (bptno line flag)
3095 "Find the directory containing the relevant source file.
3096 Put in buffer and place breakpoint icon."
3097 (goto-char (point-min))
3098 (catch 'file-not-found
3099 (if (search-forward "Located in " nil t)
3100 (when (looking-at "\\S-+")
3101 (delete (cons bptno "File not found") gdb-location-alist)
3102 (push (cons bptno (match-string 0)) gdb-location-alist))
3103 (gdb-resync)
3104 (unless (assoc bptno gdb-location-alist)
3105 (push (cons bptno "File not found") gdb-location-alist)
3106 (message-box "Cannot find source file for breakpoint location.\n\
3107 Add directory to search path for source files using the GDB command, dir."))
3108 (throw 'file-not-found nil))
3109 (with-current-buffer
3110 (find-file-noselect (match-string 0))
3111 (save-current-buffer
3112 (set (make-local-variable 'gud-minor-mode) 'gdba)
3113 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
3114 ;; only want one breakpoint icon at each location
3115 (save-excursion
3116 (goto-line (string-to-number line))
3117 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))
3118
3119 (add-hook 'find-file-hook 'gdb-find-file-hook)
3120
3121 (defun gdb-find-file-hook ()
3122 "Set up buffer for debugging if file is part of the source code
3123 of the current session."
3124 (if (and (buffer-name gud-comint-buffer)
3125 ;; in case gud or gdb-ui is just loaded
3126 gud-comint-buffer
3127 (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3128 '(gdba gdbmi)))
3129 ;;Pre GDB 6.3 "info sources" doesn't give absolute file name.
3130 (if (member (if (string-equal gdb-version "pre-6.4")
3131 (file-name-nondirectory buffer-file-name)
3132 buffer-file-name)
3133 gdb-source-file-list)
3134 (with-current-buffer (find-buffer-visiting buffer-file-name)
3135 (set (make-local-variable 'gud-minor-mode)
3136 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3137 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)))))
3138
3139 ;;from put-image
3140 (defun gdb-put-string (putstring pos &optional dprop &rest sprops)
3141 "Put string PUTSTRING in front of POS in the current buffer.
3142 PUTSTRING is displayed by putting an overlay into the current buffer with a
3143 `before-string' string that has a `display' property whose value is
3144 PUTSTRING."
3145 (let ((string (make-string 1 ?x))
3146 (buffer (current-buffer)))
3147 (setq putstring (copy-sequence putstring))
3148 (let ((overlay (make-overlay pos pos buffer))
3149 (prop (or dprop
3150 (list (list 'margin 'left-margin) putstring))))
3151 (put-text-property 0 1 'display prop string)
3152 (if sprops
3153 (add-text-properties 0 1 sprops string))
3154 (overlay-put overlay 'put-break t)
3155 (overlay-put overlay 'before-string string))))
3156
3157 ;;from remove-images
3158 (defun gdb-remove-strings (start end &optional buffer)
3159 "Remove strings between START and END in BUFFER.
3160 Remove only strings that were put in BUFFER with calls to `gdb-put-string'.
3161 BUFFER nil or omitted means use the current buffer."
3162 (unless buffer
3163 (setq buffer (current-buffer)))
3164 (dolist (overlay (overlays-in start end))
3165 (when (overlay-get overlay 'put-break)
3166 (delete-overlay overlay))))
3167
3168 (defun gdb-put-breakpoint-icon (enabled bptno)
3169 (if (string-match "[0-9+]+\\." bptno)
3170 (setq enabled gdb-parent-bptno-enabled))
3171 (let ((start (- (line-beginning-position) 1))
3172 (end (+ (line-end-position) 1))
3173 (putstring (if enabled "B" "b"))
3174 (source-window (get-buffer-window (current-buffer) 0)))
3175 (add-text-properties
3176 0 1 '(help-echo "mouse-1: clear bkpt, mouse-3: enable/disable bkpt")
3177 putstring)
3178 (if enabled
3179 (add-text-properties
3180 0 1 `(gdb-bptno ,bptno gdb-enabled t) putstring)
3181 (add-text-properties
3182 0 1 `(gdb-bptno ,bptno gdb-enabled nil) putstring))
3183 (gdb-remove-breakpoint-icons start end)
3184 (if (display-images-p)
3185 (if (>= (or left-fringe-width
3186 (if source-window (car (window-fringes source-window)))
3187 gdb-buffer-fringe-width) 8)
3188 (gdb-put-string
3189 nil (1+ start)
3190 `(left-fringe breakpoint
3191 ,(if enabled
3192 'breakpoint-enabled
3193 'breakpoint-disabled))
3194 'gdb-bptno bptno
3195 'gdb-enabled enabled)
3196 (when (< left-margin-width 2)
3197 (save-current-buffer
3198 (setq left-margin-width 2)
3199 (if source-window
3200 (set-window-margins
3201 source-window
3202 left-margin-width right-margin-width))))
3203 (put-image
3204 (if enabled
3205 (or breakpoint-enabled-icon
3206 (setq breakpoint-enabled-icon
3207 (find-image `((:type xpm :data
3208 ,breakpoint-xpm-data
3209 :ascent 100 :pointer hand)
3210 (:type pbm :data
3211 ,breakpoint-enabled-pbm-data
3212 :ascent 100 :pointer hand)))))
3213 (or breakpoint-disabled-icon
3214 (setq breakpoint-disabled-icon
3215 (find-image `((:type xpm :data
3216 ,breakpoint-xpm-data
3217 :conversion disabled
3218 :ascent 100 :pointer hand)
3219 (:type pbm :data
3220 ,breakpoint-disabled-pbm-data
3221 :ascent 100 :pointer hand))))))
3222 (+ start 1)
3223 putstring
3224 'left-margin))
3225 (when (< left-margin-width 2)
3226 (save-current-buffer
3227 (setq left-margin-width 2)
3228 (let ((window (get-buffer-window (current-buffer) 0)))
3229 (if window
3230 (set-window-margins
3231 window left-margin-width right-margin-width)))))
3232 (gdb-put-string
3233 (propertize putstring
3234 'face (if enabled 'breakpoint-enabled 'breakpoint-disabled))
3235 (1+ start)))))
3236
3237 (defun gdb-remove-breakpoint-icons (start end &optional remove-margin)
3238 (gdb-remove-strings start end)
3239 (if (display-images-p)
3240 (remove-images start end))
3241 (when remove-margin
3242 (setq left-margin-width 0)
3243 (let ((window (get-buffer-window (current-buffer) 0)))
3244 (if window
3245 (set-window-margins
3246 window left-margin-width right-margin-width)))))
3247
3248 \f
3249 ;;
3250 ;; Assembler buffer.
3251 ;;
3252 (gdb-set-buffer-rules 'gdb-assembler-buffer
3253 'gdb-assembler-buffer-name
3254 'gdb-assembler-mode)
3255
3256 ;; We can't use def-gdb-auto-update-handler because we don't want to use
3257 ;; window-start but keep the overlay arrow/current line visible.
3258 (defun gdb-assembler-handler ()
3259 (setq gdb-pending-triggers
3260 (delq 'gdb-invalidate-assembler
3261 gdb-pending-triggers))
3262 (let ((buf (gdb-get-buffer 'gdb-assembler-buffer)))
3263 (and buf
3264 (with-current-buffer buf
3265 (let* ((window (get-buffer-window buf 0))
3266 (p (window-point window))
3267 (buffer-read-only nil))
3268 (erase-buffer)
3269 (insert-buffer-substring (gdb-get-buffer-create
3270 'gdb-partial-output-buffer))
3271 (set-window-point window p)))))
3272 ;; put customisation here
3273 (gdb-assembler-custom))
3274
3275 (defun gdb-assembler-custom ()
3276 (let ((buffer (gdb-get-buffer 'gdb-assembler-buffer))
3277 (pos 1) (address) (flag) (bptno))
3278 (with-current-buffer buffer
3279 (save-excursion
3280 (if (not (equal gdb-pc-address "main"))
3281 (progn
3282 (goto-char (point-min))
3283 (if (and gdb-pc-address
3284 (search-forward gdb-pc-address nil t))
3285 (progn
3286 (setq pos (point))
3287 (beginning-of-line)
3288 (setq fringe-indicator-alist
3289 (if (string-equal gdb-frame-number "0")
3290 nil
3291 '((overlay-arrow . hollow-right-triangle))))
3292 (or gdb-overlay-arrow-position
3293 (setq gdb-overlay-arrow-position (make-marker)))
3294 (set-marker gdb-overlay-arrow-position (point))))))
3295 ;; remove all breakpoint-icons in assembler buffer before updating.
3296 (gdb-remove-breakpoint-icons (point-min) (point-max))))
3297 (with-current-buffer (gdb-get-buffer 'gdb-breakpoints-buffer)
3298 (goto-char (point-min))
3299 (while (< (point) (- (point-max) 1))
3300 (forward-line 1)
3301 (when (looking-at
3302 "\\([0-9]+\\.?[0-9]*\\).*?\\s-+\\(.\\)\\s-+0x0*\\(\\S-+\\)")
3303 (setq bptno (match-string 1))
3304 (setq flag (char-after (match-beginning 2)))
3305 (setq address (match-string 3))
3306 (with-current-buffer buffer
3307 (save-excursion
3308 (goto-char (point-min))
3309 (if (search-forward address nil t)
3310 (gdb-put-breakpoint-icon (eq flag ?y) bptno)))))))
3311 (if (not (equal gdb-pc-address "main"))
3312 (with-current-buffer buffer
3313 (set-window-point (get-buffer-window buffer 0) pos)))))
3314
3315 (defvar gdb-assembler-mode-map
3316 (let ((map (make-sparse-keymap)))
3317 (suppress-keymap map)
3318 (define-key map "q" 'kill-this-buffer)
3319 map))
3320
3321 (defvar gdb-assembler-font-lock-keywords
3322 '(;; <__function.name+n>
3323 ("<\\(\\(\\sw\\|[_.]\\)+\\)\\(\\+[0-9]+\\)?>"
3324 (1 font-lock-function-name-face))
3325 ;; 0xNNNNNNNN <__function.name+n>: opcode
3326 ("^0x[0-9a-f]+ \\(<\\(\\(\\sw\\|[_.]\\)+\\)\\+[0-9]+>\\)?:[ \t]+\\(\\sw+\\)"
3327 (4 font-lock-keyword-face))
3328 ;; %register(at least i386)
3329 ("%\\sw+" . font-lock-variable-name-face)
3330 ("^\\(Dump of assembler code for function\\) \\(.+\\):"
3331 (1 font-lock-comment-face)
3332 (2 font-lock-function-name-face))
3333 ("^\\(End of assembler dump\\.\\)" . font-lock-comment-face))
3334 "Font lock keywords used in `gdb-assembler-mode'.")
3335
3336 (defun gdb-assembler-mode ()
3337 "Major mode for viewing code assembler.
3338
3339 \\{gdb-assembler-mode-map}"
3340 (kill-all-local-variables)
3341 (setq major-mode 'gdb-assembler-mode)
3342 (setq mode-name (concat "Machine:" gdb-selected-frame))
3343 (setq gdb-overlay-arrow-position nil)
3344 (add-to-list 'overlay-arrow-variable-list 'gdb-overlay-arrow-position)
3345 (setq fringes-outside-margins t)
3346 (setq buffer-read-only t)
3347 (use-local-map gdb-assembler-mode-map)
3348 (gdb-invalidate-assembler)
3349 (set (make-local-variable 'font-lock-defaults)
3350 '(gdb-assembler-font-lock-keywords))
3351 (run-mode-hooks 'gdb-assembler-mode-hook)
3352 'gdb-invalidate-assembler)
3353
3354 (defun gdb-assembler-buffer-name ()
3355 (with-current-buffer gud-comint-buffer
3356 (concat "*disassembly of " (gdb-get-target-string) "*")))
3357
3358 (defun gdb-display-assembler-buffer ()
3359 "Display disassembly view."
3360 (interactive)
3361 (setq gdb-previous-frame nil)
3362 (gdb-display-buffer
3363 (gdb-get-buffer-create 'gdb-assembler-buffer) t))
3364
3365 (defun gdb-frame-assembler-buffer ()
3366 "Display disassembly view in a new frame."
3367 (interactive)
3368 (setq gdb-previous-frame nil)
3369 (let ((special-display-regexps (append special-display-regexps '(".*")))
3370 (special-display-frame-alist gdb-frame-parameters))
3371 (display-buffer (gdb-get-buffer-create 'gdb-assembler-buffer))))
3372
3373 ;; modified because if gdb-pc-address has changed value a new command
3374 ;; must be enqueued to update the buffer with the new output
3375 (defun gdb-invalidate-assembler (&optional ignored)
3376 (if (gdb-get-buffer 'gdb-assembler-buffer)
3377 (progn
3378 (unless (and gdb-selected-frame
3379 (string-equal gdb-selected-frame gdb-previous-frame))
3380 (if (or (not (member 'gdb-invalidate-assembler
3381 gdb-pending-triggers))
3382 (not (string-equal gdb-pc-address
3383 gdb-previous-frame-address)))
3384 (progn
3385 ;; take previous disassemble command, if any, off the queue
3386 (with-current-buffer gud-comint-buffer
3387 (let ((queue gdb-input-queue))
3388 (dolist (item queue)
3389 (if (equal (cdr item) '(gdb-assembler-handler))
3390 (setq gdb-input-queue
3391 (delete item gdb-input-queue))))))
3392 (gdb-enqueue-input
3393 (list
3394 (concat gdb-server-prefix "disassemble "
3395 (if (member gdb-pc-address '(nil "main")) nil "0x")
3396 gdb-pc-address "\n")
3397 'gdb-assembler-handler))
3398 (push 'gdb-invalidate-assembler gdb-pending-triggers)
3399 (setq gdb-previous-frame-address gdb-pc-address)
3400 (setq gdb-previous-frame gdb-selected-frame)))))))
3401
3402 (defun gdb-get-selected-frame ()
3403 (if (not (member 'gdb-get-selected-frame gdb-pending-triggers))
3404 (progn
3405 (gdb-enqueue-input
3406 (list (concat gdb-server-prefix "info frame\n") 'gdb-frame-handler))
3407 (push 'gdb-get-selected-frame
3408 gdb-pending-triggers))))
3409
3410 (defun gdb-frame-handler ()
3411 (setq gdb-pending-triggers
3412 (delq 'gdb-get-selected-frame gdb-pending-triggers))
3413 (goto-char (point-min))
3414 (when (re-search-forward
3415 "Stack level \\([0-9]+\\), frame at \\(0x[[:xdigit:]]+\\)" nil t)
3416 (setq gdb-frame-number (match-string 1))
3417 (setq gdb-frame-address (match-string 2)))
3418 (goto-char (point-min))
3419 (when (re-search-forward ".*=\\s-+0x0*\\(\\S-*\\)\\s-+in\\s-+\\(.*?\\)\
3420 \\(?: (\\(\\S-+?\\):[0-9]+?)\\)*; "
3421 nil t)
3422 (setq gdb-selected-frame (match-string 2))
3423 (if (gdb-get-buffer 'gdb-locals-buffer)
3424 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3425 (setq mode-name (concat "Locals:" gdb-selected-frame))))
3426 (if (gdb-get-buffer 'gdb-assembler-buffer)
3427 (with-current-buffer (gdb-get-buffer 'gdb-assembler-buffer)
3428 (setq mode-name (concat "Machine:" gdb-selected-frame))))
3429 (setq gdb-pc-address (match-string 1))
3430 (if (and (match-string 3) gud-overlay-arrow-position)
3431 (let ((buffer (marker-buffer gud-overlay-arrow-position))
3432 (position (marker-position gud-overlay-arrow-position)))
3433 (when (and buffer
3434 (string-equal (buffer-name buffer)
3435 (file-name-nondirectory (match-string 3))))
3436 (with-current-buffer buffer
3437 (setq fringe-indicator-alist
3438 (if (string-equal gdb-frame-number "0")
3439 nil
3440 '((overlay-arrow . hollow-right-triangle))))
3441 (set-marker gud-overlay-arrow-position position))))))
3442 (goto-char (point-min))
3443 (if (re-search-forward " source language \\(\\S-+\\)\." nil t)
3444 (setq gdb-current-language (match-string 1)))
3445 (gdb-invalidate-assembler))
3446
3447 \f
3448 ;; Code specific to GDB 6.4
3449 (defconst gdb-source-file-regexp-1 "fullname=\"\\(.*?\\)\"")
3450
3451 (defun gdb-set-gud-minor-mode-existing-buffers-1 ()
3452 "Create list of source files for current GDB session.
3453 If buffers already exist for any of these files, gud-minor-mode
3454 is set in them."
3455 (goto-char (point-min))
3456 (while (re-search-forward gdb-source-file-regexp-1 nil t)
3457 (push (match-string 1) gdb-source-file-list))
3458 (dolist (buffer (buffer-list))
3459 (with-current-buffer buffer
3460 (when (member buffer-file-name gdb-source-file-list)
3461 (set (make-local-variable 'gud-minor-mode)
3462 (buffer-local-value 'gud-minor-mode gud-comint-buffer))
3463 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
3464 (when gud-tooltip-mode
3465 (make-local-variable 'gdb-define-alist)
3466 (gdb-create-define-alist)
3467 (add-hook 'after-save-hook 'gdb-create-define-alist nil t)))))
3468 (gdb-force-mode-line-update
3469 (propertize "ready" 'face font-lock-variable-name-face)))
3470
3471 ; Uses "-var-list-children --all-values". Needs GDB 6.4 onwards.
3472 (defun gdb-var-list-children-1 (varnum)
3473 (gdb-enqueue-input
3474 (list
3475 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3476 (concat "server interpreter mi \"-var-list-children --all-values "
3477 varnum "\"\n")
3478 (concat "-var-list-children --all-values " varnum "\n"))
3479 `(lambda () (gdb-var-list-children-handler-1 ,varnum)))))
3480
3481 (defconst gdb-var-list-children-regexp-1
3482 "child={.*?name=\"\\(.+?\\)\",.*?exp=\"\\(.+?\\)\",.*?\
3483 numchild=\"\\(.+?\\)\",.*?value=\\(\".*?\"\\)\
3484 \\(}\\|,.*?\\(type=\"\\(.+?\\)\"\\)?.*?}\\)")
3485
3486 (defun gdb-var-list-children-handler-1 (varnum)
3487 (goto-char (point-min))
3488 (let ((var-list nil))
3489 (catch 'child-already-watched
3490 (dolist (var gdb-var-list)
3491 (if (string-equal varnum (car var))
3492 (progn
3493 (push var var-list)
3494 (while (re-search-forward gdb-var-list-children-regexp-1 nil t)
3495 (let ((varchild (list (match-string 1)
3496 (match-string 2)
3497 (match-string 3)
3498 (match-string 7)
3499 (read (match-string 4))
3500 nil)))
3501 (if (assoc (car varchild) gdb-var-list)
3502 (throw 'child-already-watched nil))
3503 (push varchild var-list))))
3504 (push var var-list)))
3505 (setq gdb-var-list (nreverse var-list))))
3506 (gdb-speedbar-update))
3507
3508 ; Uses "-var-update --all-values". Needs GDB 6.4 onwards.
3509 (defun gdb-var-update-1 ()
3510 (if (not (member 'gdb-var-update gdb-pending-triggers))
3511 (progn
3512 (gdb-enqueue-input
3513 (list
3514 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3515 "server interpreter mi \"-var-update --all-values *\"\n"
3516 "-var-update --all-values *\n")
3517 'gdb-var-update-handler-1))
3518 (push 'gdb-var-update gdb-pending-triggers))))
3519
3520 (defconst gdb-var-update-regexp-1
3521 "{.*?name=\"\\(.*?\\)\",.*?\\(?:value=\\(\".*?\"\\),\\)?.*?\
3522 in_scope=\"\\(.*?\\)\".*?}")
3523
3524 (defun gdb-var-update-handler-1 ()
3525 (dolist (var gdb-var-list)
3526 (setcar (nthcdr 5 var) nil))
3527 (goto-char (point-min))
3528 (while (re-search-forward gdb-var-update-regexp-1 nil t)
3529 (let* ((varnum (match-string 1))
3530 (var (assoc varnum gdb-var-list)))
3531 (when var
3532 (let ((match (match-string 3)))
3533 (cond ((string-equal match "false")
3534 (if gdb-delete-out-of-scope
3535 (gdb-var-delete-1 varnum)
3536 (setcar (nthcdr 5 var) 'out-of-scope)))
3537 ((string-equal match "true")
3538 (setcar (nthcdr 5 var) 'changed)
3539 (setcar (nthcdr 4 var)
3540 (read (match-string 2))))
3541 ((string-equal match "invalid")
3542 (gdb-var-delete-1 varnum)))))))
3543 (setq gdb-pending-triggers
3544 (delq 'gdb-var-update gdb-pending-triggers))
3545 (gdb-speedbar-update))
3546
3547 ;; Registers buffer.
3548 ;;
3549 (gdb-set-buffer-rules 'gdb-registers-buffer
3550 'gdb-registers-buffer-name
3551 'gdb-registers-mode)
3552
3553 (def-gdb-auto-update-trigger gdb-invalidate-registers-1
3554 (gdb-get-buffer 'gdb-registers-buffer)
3555 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3556 "server interpreter mi \"-data-list-register-values x\"\n"
3557 "-data-list-register-values x\n")
3558 gdb-data-list-register-values-handler)
3559
3560 (defconst gdb-data-list-register-values-regexp
3561 "{.*?number=\"\\(.*?\\)\",.*?value=\"\\(.*?\\)\".*?}")
3562
3563 (defun gdb-data-list-register-values-handler ()
3564 (setq gdb-pending-triggers (delq 'gdb-invalidate-registers-1
3565 gdb-pending-triggers))
3566 (goto-char (point-min))
3567 (if (re-search-forward gdb-error-regexp nil t)
3568 (let ((err (match-string 1)))
3569 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3570 (let ((buffer-read-only nil))
3571 (erase-buffer)
3572 (put-text-property 0 (length err) 'face font-lock-warning-face err)
3573 (insert err)
3574 (goto-char (point-min)))))
3575 (let ((register-list (reverse gdb-register-names))
3576 (register nil) (register-string nil) (register-values nil))
3577 (goto-char (point-min))
3578 (while (re-search-forward gdb-data-list-register-values-regexp nil t)
3579 (setq register (pop register-list))
3580 (setq register-string (concat register "\t" (match-string 2) "\n"))
3581 (if (member (match-string 1) gdb-changed-registers)
3582 (put-text-property 0 (length register-string)
3583 'face 'font-lock-warning-face
3584 register-string))
3585 (setq register-values
3586 (concat register-values register-string)))
3587 (let ((buf (gdb-get-buffer 'gdb-registers-buffer)))
3588 (with-current-buffer buf
3589 (let* ((window (get-buffer-window buf 0))
3590 (start (window-start window))
3591 (p (window-point window))
3592 (buffer-read-only nil))
3593 (erase-buffer)
3594 (insert register-values)
3595 (set-window-start window start)
3596 (set-window-point window p))))))
3597 (gdb-data-list-register-values-custom))
3598
3599 (defun gdb-data-list-register-values-custom ()
3600 (with-current-buffer (gdb-get-buffer 'gdb-registers-buffer)
3601 (save-excursion
3602 (let ((buffer-read-only nil)
3603 start end)
3604 (goto-char (point-min))
3605 (while (< (point) (point-max))
3606 (setq start (line-beginning-position))
3607 (setq end (line-end-position))
3608 (when (looking-at "^[^\t]+")
3609 (unless (string-equal (match-string 0) "No registers.")
3610 (put-text-property start (match-end 0)
3611 'face font-lock-variable-name-face)
3612 (add-text-properties start end
3613 '(help-echo "mouse-2: edit value"
3614 mouse-face highlight))))
3615 (forward-line 1))))))
3616
3617 ;; Needs GDB 6.4 onwards (used to fail with no stack).
3618 (defun gdb-get-changed-registers ()
3619 (if (and (gdb-get-buffer 'gdb-registers-buffer)
3620 (not (member 'gdb-get-changed-registers gdb-pending-triggers)))
3621 (progn
3622 (gdb-enqueue-input
3623 (list
3624 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3625 "server interpreter mi -data-list-changed-registers\n"
3626 "-data-list-changed-registers\n")
3627 'gdb-get-changed-registers-handler))
3628 (push 'gdb-get-changed-registers gdb-pending-triggers))))
3629
3630 (defconst gdb-data-list-register-names-regexp "\"\\(.*?\\)\"")
3631
3632 (defun gdb-get-changed-registers-handler ()
3633 (setq gdb-pending-triggers
3634 (delq 'gdb-get-changed-registers gdb-pending-triggers))
3635 (setq gdb-changed-registers nil)
3636 (goto-char (point-min))
3637 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3638 (push (match-string 1) gdb-changed-registers)))
3639 \f
3640
3641 ;; Locals buffer.
3642 ;;
3643 ;; uses "-stack-list-locals --simple-values". Needs GDB 6.1 onwards.
3644 (gdb-set-buffer-rules 'gdb-locals-buffer
3645 'gdb-locals-buffer-name
3646 'gdb-locals-mode)
3647
3648 (def-gdb-auto-update-trigger gdb-invalidate-locals-1
3649 (gdb-get-buffer 'gdb-locals-buffer)
3650 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer) 'gdba)
3651 "server interpreter mi -\"stack-list-locals --simple-values\"\n"
3652 "-stack-list-locals --simple-values\n")
3653 gdb-stack-list-locals-handler)
3654
3655 (defconst gdb-stack-list-locals-regexp
3656 "{.*?name=\"\\(.*?\\)\",.*?type=\"\\(.*?\\)\"")
3657
3658 (defvar gdb-locals-watch-map-1
3659 (let ((map (make-sparse-keymap)))
3660 (suppress-keymap map)
3661 (define-key map "\r" 'gud-watch)
3662 (define-key map [mouse-2] 'gud-watch)
3663 map)
3664 "Keymap to create watch expression of a complex data type local variable.")
3665
3666 (defvar gdb-edit-locals-map-1
3667 (let ((map (make-sparse-keymap)))
3668 (suppress-keymap map)
3669 (define-key map "\r" 'gdb-edit-locals-value)
3670 (define-key map [mouse-2] 'gdb-edit-locals-value)
3671 map)
3672 "Keymap to edit value of a simple data type local variable.")
3673
3674 (defun gdb-edit-locals-value (&optional event)
3675 "Assign a value to a variable displayed in the locals buffer."
3676 (interactive (list last-input-event))
3677 (save-excursion
3678 (if event (posn-set-point (event-end event)))
3679 (beginning-of-line)
3680 (let* ((var (current-word))
3681 (value (read-string (format "New value (%s): " var))))
3682 (gdb-enqueue-input
3683 (list (concat gdb-server-prefix"set variable " var " = " value "\n")
3684 'ignore)))))
3685
3686 ;; Dont display values of arrays or structures.
3687 ;; These can be expanded using gud-watch.
3688 (defun gdb-stack-list-locals-handler ()
3689 (setq gdb-pending-triggers (delq 'gdb-invalidate-locals-1
3690 gdb-pending-triggers))
3691 (goto-char (point-min))
3692 (if (re-search-forward gdb-error-regexp nil t)
3693 (let ((err (match-string 1)))
3694 (with-current-buffer (gdb-get-buffer 'gdb-locals-buffer)
3695 (let ((buffer-read-only nil))
3696 (erase-buffer)
3697 (insert err)
3698 (goto-char (point-min)))))
3699 (let (local locals-list)
3700 (goto-char (point-min))
3701 (while (re-search-forward gdb-stack-list-locals-regexp nil t)
3702 (let ((local (list (match-string 1)
3703 (match-string 2)
3704 nil)))
3705 (if (looking-at ",value=\\(\".*\"\\).*?}")
3706 (setcar (nthcdr 2 local) (read (match-string 1))))
3707 (push local locals-list)))
3708 (let ((buf (gdb-get-buffer 'gdb-locals-buffer)))
3709 (and buf (with-current-buffer buf
3710 (let* ((window (get-buffer-window buf 0))
3711 (start (window-start window))
3712 (p (window-point window))
3713 (buffer-read-only nil) (name) (value))
3714 (erase-buffer)
3715 (dolist (local locals-list)
3716 (setq name (car local))
3717 (setq value (nth 2 local))
3718 (if (or (not value)
3719 (string-match "^\\0x" value))
3720 (add-text-properties 0 (length name)
3721 `(mouse-face highlight
3722 help-echo "mouse-2: create watch expression"
3723 local-map ,gdb-locals-watch-map-1)
3724 name)
3725 (add-text-properties 0 (length value)
3726 `(mouse-face highlight
3727 help-echo "mouse-2: edit value"
3728 local-map ,gdb-edit-locals-map-1)
3729 value))
3730 (insert
3731 (concat name "\t" (nth 1 local)
3732 "\t" value "\n")))
3733 (set-window-start window start)
3734 (set-window-point window p))))))))
3735
3736 (defun gdb-get-register-names ()
3737 "Create a list of register names."
3738 (goto-char (point-min))
3739 (setq gdb-register-names nil)
3740 (while (re-search-forward gdb-data-list-register-names-regexp nil t)
3741 (push (match-string 1) gdb-register-names)))
3742
3743 (provide 'gdb-ui)
3744
3745 ;; arch-tag: e9fb00c5-74ef-469f-a088-37384caae352
3746 ;;; gdb-ui.el ends here