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