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