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