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