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