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