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